71542f0ff3c103e92e6dc4eaad4403000cccd0d7
[mech_eap.git] / wpa_supplicant / wpa_supplicant.c
1 /*
2  * WPA Supplicant
3  * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  *
8  * This file implements functions for registering and unregistering
9  * %wpa_supplicant interfaces. In addition, this file contains number of
10  * functions for managing network connections.
11  */
12
13 #include "includes.h"
14
15 #include "common.h"
16 #include "crypto/random.h"
17 #include "crypto/sha1.h"
18 #include "eapol_supp/eapol_supp_sm.h"
19 #include "eap_peer/eap.h"
20 #include "eap_server/eap_methods.h"
21 #include "rsn_supp/wpa.h"
22 #include "eloop.h"
23 #include "config.h"
24 #include "l2_packet/l2_packet.h"
25 #include "wpa_supplicant_i.h"
26 #include "driver_i.h"
27 #include "ctrl_iface.h"
28 #include "pcsc_funcs.h"
29 #include "common/version.h"
30 #include "rsn_supp/preauth.h"
31 #include "rsn_supp/pmksa_cache.h"
32 #include "common/wpa_ctrl.h"
33 #include "common/ieee802_11_defs.h"
34 #include "p2p/p2p.h"
35 #include "blacklist.h"
36 #include "wpas_glue.h"
37 #include "wps_supplicant.h"
38 #include "ibss_rsn.h"
39 #include "sme.h"
40 #include "gas_query.h"
41 #include "ap.h"
42 #include "p2p_supplicant.h"
43 #include "notify.h"
44 #include "bgscan.h"
45 #include "bss.h"
46 #include "scan.h"
47 #include "offchannel.h"
48
49 const char *wpa_supplicant_version =
50 "wpa_supplicant v" VERSION_STR "\n"
51 "Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi> and contributors";
52
53 const char *wpa_supplicant_license =
54 "This software may be distributed under the terms of the BSD license.\n"
55 "See README for more details.\n"
56 #ifdef EAP_TLS_OPENSSL
57 "\nThis product includes software developed by the OpenSSL Project\n"
58 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
59 #endif /* EAP_TLS_OPENSSL */
60 ;
61
62 #ifndef CONFIG_NO_STDOUT_DEBUG
63 /* Long text divided into parts in order to fit in C89 strings size limits. */
64 const char *wpa_supplicant_full_license1 =
65 "";
66 const char *wpa_supplicant_full_license2 =
67 "This software may be distributed under the terms of the BSD license.\n"
68 "\n"
69 "Redistribution and use in source and binary forms, with or without\n"
70 "modification, are permitted provided that the following conditions are\n"
71 "met:\n"
72 "\n";
73 const char *wpa_supplicant_full_license3 =
74 "1. Redistributions of source code must retain the above copyright\n"
75 "   notice, this list of conditions and the following disclaimer.\n"
76 "\n"
77 "2. Redistributions in binary form must reproduce the above copyright\n"
78 "   notice, this list of conditions and the following disclaimer in the\n"
79 "   documentation and/or other materials provided with the distribution.\n"
80 "\n";
81 const char *wpa_supplicant_full_license4 =
82 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
83 "   names of its contributors may be used to endorse or promote products\n"
84 "   derived from this software without specific prior written permission.\n"
85 "\n"
86 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
87 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
88 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
89 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
90 const char *wpa_supplicant_full_license5 =
91 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
92 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
93 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
94 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
95 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
96 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
97 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
98 "\n";
99 #endif /* CONFIG_NO_STDOUT_DEBUG */
100
101 extern int wpa_debug_level;
102 extern int wpa_debug_show_keys;
103 extern int wpa_debug_timestamp;
104 extern struct wpa_driver_ops *wpa_drivers[];
105
106 /* Configure default/group WEP keys for static WEP */
107 int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
108 {
109         int i, set = 0;
110
111         for (i = 0; i < NUM_WEP_KEYS; i++) {
112                 if (ssid->wep_key_len[i] == 0)
113                         continue;
114
115                 set = 1;
116                 wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
117                                 i, i == ssid->wep_tx_keyidx, NULL, 0,
118                                 ssid->wep_key[i], ssid->wep_key_len[i]);
119         }
120
121         return set;
122 }
123
124
125 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
126                                            struct wpa_ssid *ssid)
127 {
128         u8 key[32];
129         size_t keylen;
130         enum wpa_alg alg;
131         u8 seq[6] = { 0 };
132
133         /* IBSS/WPA-None uses only one key (Group) for both receiving and
134          * sending unicast and multicast packets. */
135
136         if (ssid->mode != WPAS_MODE_IBSS) {
137                 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
138                         "IBSS/ad-hoc) for WPA-None", ssid->mode);
139                 return -1;
140         }
141
142         if (!ssid->psk_set) {
143                 wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
144                         "WPA-None");
145                 return -1;
146         }
147
148         switch (wpa_s->group_cipher) {
149         case WPA_CIPHER_CCMP:
150                 os_memcpy(key, ssid->psk, 16);
151                 keylen = 16;
152                 alg = WPA_ALG_CCMP;
153                 break;
154         case WPA_CIPHER_TKIP:
155                 /* WPA-None uses the same Michael MIC key for both TX and RX */
156                 os_memcpy(key, ssid->psk, 16 + 8);
157                 os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
158                 keylen = 32;
159                 alg = WPA_ALG_TKIP;
160                 break;
161         default:
162                 wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
163                         "WPA-None", wpa_s->group_cipher);
164                 return -1;
165         }
166
167         /* TODO: should actually remember the previously used seq#, both for TX
168          * and RX from each STA.. */
169
170         return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
171 }
172
173
174 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
175 {
176         struct wpa_supplicant *wpa_s = eloop_ctx;
177         const u8 *bssid = wpa_s->bssid;
178         if (is_zero_ether_addr(bssid))
179                 bssid = wpa_s->pending_bssid;
180         wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
181                 MAC2STR(bssid));
182         wpa_blacklist_add(wpa_s, bssid);
183         wpa_sm_notify_disassoc(wpa_s->wpa);
184         wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
185         wpa_s->reassociate = 1;
186
187         /*
188          * If we timed out, the AP or the local radio may be busy.
189          * So, wait a second until scanning again.
190          */
191         wpa_supplicant_req_scan(wpa_s, 1, 0);
192 }
193
194
195 /**
196  * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
197  * @wpa_s: Pointer to wpa_supplicant data
198  * @sec: Number of seconds after which to time out authentication
199  * @usec: Number of microseconds after which to time out authentication
200  *
201  * This function is used to schedule a timeout for the current authentication
202  * attempt.
203  */
204 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
205                                      int sec, int usec)
206 {
207         if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
208             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
209                 return;
210
211         wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
212                 "%d usec", sec, usec);
213         eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
214         eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
215 }
216
217
218 /**
219  * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
220  * @wpa_s: Pointer to wpa_supplicant data
221  *
222  * This function is used to cancel authentication timeout scheduled with
223  * wpa_supplicant_req_auth_timeout() and it is called when authentication has
224  * been completed.
225  */
226 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
227 {
228         wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
229         eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
230         wpa_blacklist_del(wpa_s, wpa_s->bssid);
231 }
232
233
234 /**
235  * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
236  * @wpa_s: Pointer to wpa_supplicant data
237  *
238  * This function is used to configure EAPOL state machine based on the selected
239  * authentication mode.
240  */
241 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
242 {
243 #ifdef IEEE8021X_EAPOL
244         struct eapol_config eapol_conf;
245         struct wpa_ssid *ssid = wpa_s->current_ssid;
246
247 #ifdef CONFIG_IBSS_RSN
248         if (ssid->mode == WPAS_MODE_IBSS &&
249             wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
250             wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
251                 /*
252                  * RSN IBSS authentication is per-STA and we can disable the
253                  * per-BSSID EAPOL authentication.
254                  */
255                 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
256                 eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
257                 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
258                 return;
259         }
260 #endif /* CONFIG_IBSS_RSN */
261
262         eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
263         eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
264
265         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
266             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
267                 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
268         else
269                 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
270
271         os_memset(&eapol_conf, 0, sizeof(eapol_conf));
272         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
273                 eapol_conf.accept_802_1x_keys = 1;
274                 eapol_conf.required_keys = 0;
275                 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
276                         eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
277                 }
278                 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
279                         eapol_conf.required_keys |=
280                                 EAPOL_REQUIRE_KEY_BROADCAST;
281                 }
282
283                 if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
284                         eapol_conf.required_keys = 0;
285         }
286         if (wpa_s->conf)
287                 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
288         eapol_conf.workaround = ssid->eap_workaround;
289         eapol_conf.eap_disabled =
290                 !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
291                 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
292                 wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
293         eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
294 #endif /* IEEE8021X_EAPOL */
295 }
296
297
298 /**
299  * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
300  * @wpa_s: Pointer to wpa_supplicant data
301  * @ssid: Configuration data for the network
302  *
303  * This function is used to configure WPA state machine and related parameters
304  * to a mode where WPA is not enabled. This is called as part of the
305  * authentication configuration when the selected network does not use WPA.
306  */
307 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
308                                        struct wpa_ssid *ssid)
309 {
310         int i;
311
312         if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
313                 wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
314         else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
315                 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
316         else
317                 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
318         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
319         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
320         wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
321         wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
322         wpa_s->group_cipher = WPA_CIPHER_NONE;
323         wpa_s->mgmt_group_cipher = 0;
324
325         for (i = 0; i < NUM_WEP_KEYS; i++) {
326                 if (ssid->wep_key_len[i] > 5) {
327                         wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
328                         wpa_s->group_cipher = WPA_CIPHER_WEP104;
329                         break;
330                 } else if (ssid->wep_key_len[i] > 0) {
331                         wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
332                         wpa_s->group_cipher = WPA_CIPHER_WEP40;
333                         break;
334                 }
335         }
336
337         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
338         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
339         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
340                          wpa_s->pairwise_cipher);
341         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
342 #ifdef CONFIG_IEEE80211W
343         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
344                          wpa_s->mgmt_group_cipher);
345 #endif /* CONFIG_IEEE80211W */
346
347         pmksa_cache_clear_current(wpa_s->wpa);
348 }
349
350
351 void free_hw_features(struct wpa_supplicant *wpa_s)
352 {
353         int i;
354         if (wpa_s->hw.modes == NULL)
355                 return;
356
357         for (i = 0; i < wpa_s->hw.num_modes; i++) {
358                 os_free(wpa_s->hw.modes[i].channels);
359                 os_free(wpa_s->hw.modes[i].rates);
360         }
361
362         os_free(wpa_s->hw.modes);
363         wpa_s->hw.modes = NULL;
364 }
365
366
367 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
368 {
369         bgscan_deinit(wpa_s);
370         scard_deinit(wpa_s->scard);
371         wpa_s->scard = NULL;
372         wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
373         eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
374         l2_packet_deinit(wpa_s->l2);
375         wpa_s->l2 = NULL;
376         if (wpa_s->l2_br) {
377                 l2_packet_deinit(wpa_s->l2_br);
378                 wpa_s->l2_br = NULL;
379         }
380
381         if (wpa_s->conf != NULL) {
382                 struct wpa_ssid *ssid;
383                 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
384                         wpas_notify_network_removed(wpa_s, ssid);
385         }
386
387         os_free(wpa_s->confname);
388         wpa_s->confname = NULL;
389
390         wpa_sm_set_eapol(wpa_s->wpa, NULL);
391         eapol_sm_deinit(wpa_s->eapol);
392         wpa_s->eapol = NULL;
393
394         rsn_preauth_deinit(wpa_s->wpa);
395
396 #ifdef CONFIG_TDLS
397         wpa_tdls_deinit(wpa_s->wpa);
398 #endif /* CONFIG_TDLS */
399
400         pmksa_candidate_free(wpa_s->wpa);
401         wpa_sm_deinit(wpa_s->wpa);
402         wpa_s->wpa = NULL;
403         wpa_blacklist_clear(wpa_s);
404
405         wpa_bss_deinit(wpa_s);
406
407         wpa_supplicant_cancel_scan(wpa_s);
408         wpa_supplicant_cancel_auth_timeout(wpa_s);
409         eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
410 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
411         eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
412                              wpa_s, NULL);
413 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
414
415         wpas_wps_deinit(wpa_s);
416
417         wpabuf_free(wpa_s->pending_eapol_rx);
418         wpa_s->pending_eapol_rx = NULL;
419
420 #ifdef CONFIG_IBSS_RSN
421         ibss_rsn_deinit(wpa_s->ibss_rsn);
422         wpa_s->ibss_rsn = NULL;
423 #endif /* CONFIG_IBSS_RSN */
424
425         sme_deinit(wpa_s);
426
427 #ifdef CONFIG_AP
428         wpa_supplicant_ap_deinit(wpa_s);
429 #endif /* CONFIG_AP */
430
431 #ifdef CONFIG_P2P
432         wpas_p2p_deinit(wpa_s);
433 #endif /* CONFIG_P2P */
434
435 #ifdef CONFIG_OFFCHANNEL
436         offchannel_deinit(wpa_s);
437 #endif /* CONFIG_OFFCHANNEL */
438
439         wpa_supplicant_cancel_sched_scan(wpa_s);
440
441         os_free(wpa_s->next_scan_freqs);
442         wpa_s->next_scan_freqs = NULL;
443
444         gas_query_deinit(wpa_s->gas);
445         wpa_s->gas = NULL;
446
447         free_hw_features(wpa_s);
448
449         os_free(wpa_s->bssid_filter);
450         wpa_s->bssid_filter = NULL;
451 }
452
453
454 /**
455  * wpa_clear_keys - Clear keys configured for the driver
456  * @wpa_s: Pointer to wpa_supplicant data
457  * @addr: Previously used BSSID or %NULL if not available
458  *
459  * This function clears the encryption keys that has been previously configured
460  * for the driver.
461  */
462 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
463 {
464         if (wpa_s->keys_cleared) {
465                 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
466                  * timing issues with keys being cleared just before new keys
467                  * are set or just after association or something similar. This
468                  * shows up in group key handshake failing often because of the
469                  * client not receiving the first encrypted packets correctly.
470                  * Skipping some of the extra key clearing steps seems to help
471                  * in completing group key handshake more reliably. */
472                 wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
473                         "skip key clearing");
474                 return;
475         }
476
477         /* MLME-DELETEKEYS.request */
478         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
479         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
480         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
481         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
482 #ifdef CONFIG_IEEE80211W
483         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
484         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
485 #endif /* CONFIG_IEEE80211W */
486         if (addr) {
487                 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
488                                 0);
489                 /* MLME-SETPROTECTION.request(None) */
490                 wpa_drv_mlme_setprotection(
491                         wpa_s, addr,
492                         MLME_SETPROTECTION_PROTECT_TYPE_NONE,
493                         MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
494         }
495         wpa_s->keys_cleared = 1;
496 }
497
498
499 /**
500  * wpa_supplicant_state_txt - Get the connection state name as a text string
501  * @state: State (wpa_state; WPA_*)
502  * Returns: The state name as a printable text string
503  */
504 const char * wpa_supplicant_state_txt(enum wpa_states state)
505 {
506         switch (state) {
507         case WPA_DISCONNECTED:
508                 return "DISCONNECTED";
509         case WPA_INACTIVE:
510                 return "INACTIVE";
511         case WPA_INTERFACE_DISABLED:
512                 return "INTERFACE_DISABLED";
513         case WPA_SCANNING:
514                 return "SCANNING";
515         case WPA_AUTHENTICATING:
516                 return "AUTHENTICATING";
517         case WPA_ASSOCIATING:
518                 return "ASSOCIATING";
519         case WPA_ASSOCIATED:
520                 return "ASSOCIATED";
521         case WPA_4WAY_HANDSHAKE:
522                 return "4WAY_HANDSHAKE";
523         case WPA_GROUP_HANDSHAKE:
524                 return "GROUP_HANDSHAKE";
525         case WPA_COMPLETED:
526                 return "COMPLETED";
527         default:
528                 return "UNKNOWN";
529         }
530 }
531
532
533 #ifdef CONFIG_BGSCAN
534
535 static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
536 {
537         if (wpas_driver_bss_selection(wpa_s))
538                 return;
539         if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
540                 return;
541
542         bgscan_deinit(wpa_s);
543         if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
544                 if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
545                         wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
546                                 "bgscan");
547                         /*
548                          * Live without bgscan; it is only used as a roaming
549                          * optimization, so the initial connection is not
550                          * affected.
551                          */
552                 } else
553                         wpa_s->bgscan_ssid = wpa_s->current_ssid;
554         } else
555                 wpa_s->bgscan_ssid = NULL;
556 }
557
558
559 static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
560 {
561         if (wpa_s->bgscan_ssid != NULL) {
562                 bgscan_deinit(wpa_s);
563                 wpa_s->bgscan_ssid = NULL;
564         }
565 }
566
567 #endif /* CONFIG_BGSCAN */
568
569
570 /**
571  * wpa_supplicant_set_state - Set current connection state
572  * @wpa_s: Pointer to wpa_supplicant data
573  * @state: The new connection state
574  *
575  * This function is called whenever the connection state changes, e.g.,
576  * association is completed for WPA/WPA2 4-Way Handshake is started.
577  */
578 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
579                               enum wpa_states state)
580 {
581         enum wpa_states old_state = wpa_s->wpa_state;
582
583         wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
584                 wpa_supplicant_state_txt(wpa_s->wpa_state),
585                 wpa_supplicant_state_txt(state));
586
587         if (state != WPA_SCANNING)
588                 wpa_supplicant_notify_scanning(wpa_s, 0);
589
590         if (state == WPA_COMPLETED && wpa_s->new_connection) {
591 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
592                 struct wpa_ssid *ssid = wpa_s->current_ssid;
593                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
594                         MACSTR " completed %s [id=%d id_str=%s]",
595                         MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
596                         "(reauth)" : "(auth)",
597                         ssid ? ssid->id : -1,
598                         ssid && ssid->id_str ? ssid->id_str : "");
599 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
600                 wpa_s->new_connection = 0;
601                 wpa_s->reassociated_connection = 1;
602                 wpa_drv_set_operstate(wpa_s, 1);
603 #ifndef IEEE8021X_EAPOL
604                 wpa_drv_set_supp_port(wpa_s, 1);
605 #endif /* IEEE8021X_EAPOL */
606                 wpa_s->after_wps = 0;
607 #ifdef CONFIG_P2P
608                 wpas_p2p_completed(wpa_s);
609 #endif /* CONFIG_P2P */
610         } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
611                    state == WPA_ASSOCIATED) {
612                 wpa_s->new_connection = 1;
613                 wpa_drv_set_operstate(wpa_s, 0);
614 #ifndef IEEE8021X_EAPOL
615                 wpa_drv_set_supp_port(wpa_s, 0);
616 #endif /* IEEE8021X_EAPOL */
617         }
618         wpa_s->wpa_state = state;
619
620 #ifdef CONFIG_BGSCAN
621         if (state == WPA_COMPLETED)
622                 wpa_supplicant_start_bgscan(wpa_s);
623         else
624                 wpa_supplicant_stop_bgscan(wpa_s);
625 #endif /* CONFIG_BGSCAN */
626
627         if (wpa_s->wpa_state != old_state) {
628                 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
629
630                 if (wpa_s->wpa_state == WPA_COMPLETED ||
631                     old_state == WPA_COMPLETED)
632                         wpas_notify_auth_changed(wpa_s);
633         }
634 }
635
636
637 void wpa_supplicant_terminate_proc(struct wpa_global *global)
638 {
639         int pending = 0;
640 #ifdef CONFIG_WPS
641         struct wpa_supplicant *wpa_s = global->ifaces;
642         while (wpa_s) {
643                 if (wpas_wps_terminate_pending(wpa_s) == 1)
644                         pending = 1;
645                 wpa_s = wpa_s->next;
646         }
647 #endif /* CONFIG_WPS */
648         if (pending)
649                 return;
650         eloop_terminate();
651 }
652
653
654 static void wpa_supplicant_terminate(int sig, void *signal_ctx)
655 {
656         struct wpa_global *global = signal_ctx;
657         wpa_supplicant_terminate_proc(global);
658 }
659
660
661 void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
662 {
663         enum wpa_states old_state = wpa_s->wpa_state;
664
665         wpa_s->pairwise_cipher = 0;
666         wpa_s->group_cipher = 0;
667         wpa_s->mgmt_group_cipher = 0;
668         wpa_s->key_mgmt = 0;
669         if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
670                 wpa_s->wpa_state = WPA_DISCONNECTED;
671
672         if (wpa_s->wpa_state != old_state)
673                 wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
674 }
675
676
677 /**
678  * wpa_supplicant_reload_configuration - Reload configuration data
679  * @wpa_s: Pointer to wpa_supplicant data
680  * Returns: 0 on success or -1 if configuration parsing failed
681  *
682  * This function can be used to request that the configuration data is reloaded
683  * (e.g., after configuration file change). This function is reloading
684  * configuration only for one interface, so this may need to be called multiple
685  * times if %wpa_supplicant is controlling multiple interfaces and all
686  * interfaces need reconfiguration.
687  */
688 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
689 {
690         struct wpa_config *conf;
691         int reconf_ctrl;
692         int old_ap_scan;
693
694         if (wpa_s->confname == NULL)
695                 return -1;
696         conf = wpa_config_read(wpa_s->confname);
697         if (conf == NULL) {
698                 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
699                         "file '%s' - exiting", wpa_s->confname);
700                 return -1;
701         }
702         conf->changed_parameters = (unsigned int) -1;
703
704         reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
705                 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
706                     os_strcmp(conf->ctrl_interface,
707                               wpa_s->conf->ctrl_interface) != 0);
708
709         if (reconf_ctrl && wpa_s->ctrl_iface) {
710                 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
711                 wpa_s->ctrl_iface = NULL;
712         }
713
714         eapol_sm_invalidate_cached_session(wpa_s->eapol);
715         if (wpa_s->current_ssid) {
716                 wpa_supplicant_deauthenticate(wpa_s,
717                                               WLAN_REASON_DEAUTH_LEAVING);
718         }
719
720         /*
721          * TODO: should notify EAPOL SM about changes in opensc_engine_path,
722          * pkcs11_engine_path, pkcs11_module_path.
723          */
724         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
725                 /*
726                  * Clear forced success to clear EAP state for next
727                  * authentication.
728                  */
729                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
730         }
731         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
732         wpa_sm_set_config(wpa_s->wpa, NULL);
733         wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
734         wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
735         rsn_preauth_deinit(wpa_s->wpa);
736
737         old_ap_scan = wpa_s->conf->ap_scan;
738         wpa_config_free(wpa_s->conf);
739         wpa_s->conf = conf;
740         if (old_ap_scan != wpa_s->conf->ap_scan)
741                 wpas_notify_ap_scan_changed(wpa_s);
742
743         if (reconf_ctrl)
744                 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
745
746         wpa_supplicant_update_config(wpa_s);
747
748         wpa_supplicant_clear_status(wpa_s);
749         if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
750                 wpa_s->reassociate = 1;
751                 wpa_supplicant_req_scan(wpa_s, 0, 0);
752         }
753         wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
754         return 0;
755 }
756
757
758 static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
759 {
760         struct wpa_global *global = signal_ctx;
761         struct wpa_supplicant *wpa_s;
762         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
763                 wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
764                         sig);
765                 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
766                         wpa_supplicant_terminate_proc(global);
767                 }
768         }
769 }
770
771
772 enum wpa_cipher cipher_suite2driver(int cipher)
773 {
774         switch (cipher) {
775         case WPA_CIPHER_NONE:
776                 return CIPHER_NONE;
777         case WPA_CIPHER_WEP40:
778                 return CIPHER_WEP40;
779         case WPA_CIPHER_WEP104:
780                 return CIPHER_WEP104;
781         case WPA_CIPHER_CCMP:
782                 return CIPHER_CCMP;
783         case WPA_CIPHER_TKIP:
784         default:
785                 return CIPHER_TKIP;
786         }
787 }
788
789
790 enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
791 {
792         switch (key_mgmt) {
793         case WPA_KEY_MGMT_NONE:
794                 return KEY_MGMT_NONE;
795         case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
796                 return KEY_MGMT_802_1X_NO_WPA;
797         case WPA_KEY_MGMT_IEEE8021X:
798                 return KEY_MGMT_802_1X;
799         case WPA_KEY_MGMT_WPA_NONE:
800                 return KEY_MGMT_WPA_NONE;
801         case WPA_KEY_MGMT_FT_IEEE8021X:
802                 return KEY_MGMT_FT_802_1X;
803         case WPA_KEY_MGMT_FT_PSK:
804                 return KEY_MGMT_FT_PSK;
805         case WPA_KEY_MGMT_IEEE8021X_SHA256:
806                 return KEY_MGMT_802_1X_SHA256;
807         case WPA_KEY_MGMT_PSK_SHA256:
808                 return KEY_MGMT_PSK_SHA256;
809         case WPA_KEY_MGMT_WPS:
810                 return KEY_MGMT_WPS;
811         case WPA_KEY_MGMT_PSK:
812         default:
813                 return KEY_MGMT_PSK;
814         }
815 }
816
817
818 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
819                                          struct wpa_ssid *ssid,
820                                          struct wpa_ie_data *ie)
821 {
822         int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
823         if (ret) {
824                 if (ret == -2) {
825                         wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
826                                 "from association info");
827                 }
828                 return -1;
829         }
830
831         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
832                 "cipher suites");
833         if (!(ie->group_cipher & ssid->group_cipher)) {
834                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
835                         "cipher 0x%x (mask 0x%x) - reject",
836                         ie->group_cipher, ssid->group_cipher);
837                 return -1;
838         }
839         if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
840                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
841                         "cipher 0x%x (mask 0x%x) - reject",
842                         ie->pairwise_cipher, ssid->pairwise_cipher);
843                 return -1;
844         }
845         if (!(ie->key_mgmt & ssid->key_mgmt)) {
846                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
847                         "management 0x%x (mask 0x%x) - reject",
848                         ie->key_mgmt, ssid->key_mgmt);
849                 return -1;
850         }
851
852 #ifdef CONFIG_IEEE80211W
853         if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
854             ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
855                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
856                         "that does not support management frame protection - "
857                         "reject");
858                 return -1;
859         }
860 #endif /* CONFIG_IEEE80211W */
861
862         return 0;
863 }
864
865
866 /**
867  * wpa_supplicant_set_suites - Set authentication and encryption parameters
868  * @wpa_s: Pointer to wpa_supplicant data
869  * @bss: Scan results for the selected BSS, or %NULL if not available
870  * @ssid: Configuration data for the selected network
871  * @wpa_ie: Buffer for the WPA/RSN IE
872  * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
873  * used buffer length in case the functions returns success.
874  * Returns: 0 on success or -1 on failure
875  *
876  * This function is used to configure authentication and encryption parameters
877  * based on the network configuration and scan result for the selected BSS (if
878  * available).
879  */
880 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
881                               struct wpa_bss *bss, struct wpa_ssid *ssid,
882                               u8 *wpa_ie, size_t *wpa_ie_len)
883 {
884         struct wpa_ie_data ie;
885         int sel, proto;
886         const u8 *bss_wpa, *bss_rsn;
887
888         if (bss) {
889                 bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
890                 bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
891         } else
892                 bss_wpa = bss_rsn = NULL;
893
894         if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
895             wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
896             (ie.group_cipher & ssid->group_cipher) &&
897             (ie.pairwise_cipher & ssid->pairwise_cipher) &&
898             (ie.key_mgmt & ssid->key_mgmt)) {
899                 wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
900                 proto = WPA_PROTO_RSN;
901         } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
902                    wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
903                    (ie.group_cipher & ssid->group_cipher) &&
904                    (ie.pairwise_cipher & ssid->pairwise_cipher) &&
905                    (ie.key_mgmt & ssid->key_mgmt)) {
906                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
907                 proto = WPA_PROTO_WPA;
908         } else if (bss) {
909                 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
910                 return -1;
911         } else {
912                 if (ssid->proto & WPA_PROTO_RSN)
913                         proto = WPA_PROTO_RSN;
914                 else
915                         proto = WPA_PROTO_WPA;
916                 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
917                         os_memset(&ie, 0, sizeof(ie));
918                         ie.group_cipher = ssid->group_cipher;
919                         ie.pairwise_cipher = ssid->pairwise_cipher;
920                         ie.key_mgmt = ssid->key_mgmt;
921 #ifdef CONFIG_IEEE80211W
922                         ie.mgmt_group_cipher =
923                                 ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
924                                 WPA_CIPHER_AES_128_CMAC : 0;
925 #endif /* CONFIG_IEEE80211W */
926                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
927                                 "based on configuration");
928                 } else
929                         proto = ie.proto;
930         }
931
932         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
933                 "pairwise %d key_mgmt %d proto %d",
934                 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
935 #ifdef CONFIG_IEEE80211W
936         if (ssid->ieee80211w) {
937                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
938                         ie.mgmt_group_cipher);
939         }
940 #endif /* CONFIG_IEEE80211W */
941
942         wpa_s->wpa_proto = proto;
943         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
944         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
945                          !!(ssid->proto & WPA_PROTO_RSN));
946
947         if (bss || !wpa_s->ap_ies_from_associnfo) {
948                 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
949                                          bss_wpa ? 2 + bss_wpa[1] : 0) ||
950                     wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
951                                          bss_rsn ? 2 + bss_rsn[1] : 0))
952                         return -1;
953         }
954
955         sel = ie.group_cipher & ssid->group_cipher;
956         if (sel & WPA_CIPHER_CCMP) {
957                 wpa_s->group_cipher = WPA_CIPHER_CCMP;
958                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
959         } else if (sel & WPA_CIPHER_TKIP) {
960                 wpa_s->group_cipher = WPA_CIPHER_TKIP;
961                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
962         } else if (sel & WPA_CIPHER_WEP104) {
963                 wpa_s->group_cipher = WPA_CIPHER_WEP104;
964                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
965         } else if (sel & WPA_CIPHER_WEP40) {
966                 wpa_s->group_cipher = WPA_CIPHER_WEP40;
967                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
968         } else {
969                 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
970                         "cipher");
971                 return -1;
972         }
973
974         sel = ie.pairwise_cipher & ssid->pairwise_cipher;
975         if (sel & WPA_CIPHER_CCMP) {
976                 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
977                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
978         } else if (sel & WPA_CIPHER_TKIP) {
979                 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
980                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
981         } else if (sel & WPA_CIPHER_NONE) {
982                 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
983                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
984         } else {
985                 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
986                         "cipher");
987                 return -1;
988         }
989
990         sel = ie.key_mgmt & ssid->key_mgmt;
991         if (0) {
992 #ifdef CONFIG_IEEE80211R
993         } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
994                 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
995                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
996         } else if (sel & WPA_KEY_MGMT_FT_PSK) {
997                 wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
998                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
999 #endif /* CONFIG_IEEE80211R */
1000 #ifdef CONFIG_IEEE80211W
1001         } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
1002                 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
1003                 wpa_dbg(wpa_s, MSG_DEBUG,
1004                         "WPA: using KEY_MGMT 802.1X with SHA256");
1005         } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
1006                 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
1007                 wpa_dbg(wpa_s, MSG_DEBUG,
1008                         "WPA: using KEY_MGMT PSK with SHA256");
1009 #endif /* CONFIG_IEEE80211W */
1010         } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
1011                 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1012                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1013         } else if (sel & WPA_KEY_MGMT_PSK) {
1014                 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1015                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1016         } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1017                 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1018                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
1019         } else {
1020                 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
1021                         "authenticated key management type");
1022                 return -1;
1023         }
1024
1025         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1026         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1027                          wpa_s->pairwise_cipher);
1028         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1029
1030 #ifdef CONFIG_IEEE80211W
1031         sel = ie.mgmt_group_cipher;
1032         if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
1033             !(ie.capabilities & WPA_CAPABILITY_MFPC))
1034                 sel = 0;
1035         if (sel & WPA_CIPHER_AES_128_CMAC) {
1036                 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1037                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1038                         "AES-128-CMAC");
1039         } else {
1040                 wpa_s->mgmt_group_cipher = 0;
1041                 wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1042         }
1043         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1044                          wpa_s->mgmt_group_cipher);
1045         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
1046 #endif /* CONFIG_IEEE80211W */
1047
1048         if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1049                 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
1050                 return -1;
1051         }
1052
1053         if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
1054                 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
1055 #ifndef CONFIG_NO_PBKDF2
1056                 if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
1057                     ssid->passphrase) {
1058                         u8 psk[PMK_LEN];
1059                         pbkdf2_sha1(ssid->passphrase, (char *) bss->ssid,
1060                                     bss->ssid_len, 4096, psk, PMK_LEN);
1061                         wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
1062                                         psk, PMK_LEN);
1063                         wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
1064                 }
1065 #endif /* CONFIG_NO_PBKDF2 */
1066         } else
1067                 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1068
1069         return 0;
1070 }
1071
1072
1073 /**
1074  * wpa_supplicant_associate - Request association
1075  * @wpa_s: Pointer to wpa_supplicant data
1076  * @bss: Scan results for the selected BSS, or %NULL if not available
1077  * @ssid: Configuration data for the selected network
1078  *
1079  * This function is used to request %wpa_supplicant to associate with a BSS.
1080  */
1081 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1082                               struct wpa_bss *bss, struct wpa_ssid *ssid)
1083 {
1084         u8 wpa_ie[200];
1085         size_t wpa_ie_len;
1086         int use_crypt, ret, i, bssid_changed;
1087         int algs = WPA_AUTH_ALG_OPEN;
1088         enum wpa_cipher cipher_pairwise, cipher_group;
1089         struct wpa_driver_associate_params params;
1090         int wep_keys_set = 0;
1091         struct wpa_driver_capa capa;
1092         int assoc_failed = 0;
1093         struct wpa_ssid *old_ssid;
1094 #ifdef CONFIG_HT_OVERRIDES
1095         struct ieee80211_ht_capabilities htcaps;
1096         struct ieee80211_ht_capabilities htcaps_mask;
1097 #endif /* CONFIG_HT_OVERRIDES */
1098
1099 #ifdef CONFIG_IBSS_RSN
1100         ibss_rsn_deinit(wpa_s->ibss_rsn);
1101         wpa_s->ibss_rsn = NULL;
1102 #endif /* CONFIG_IBSS_RSN */
1103
1104         if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
1105             ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
1106 #ifdef CONFIG_AP
1107                 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
1108                         wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
1109                                 "mode");
1110                         return;
1111                 }
1112                 wpa_supplicant_create_ap(wpa_s, ssid);
1113                 wpa_s->current_bss = bss;
1114 #else /* CONFIG_AP */
1115                 wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
1116                         "the build");
1117 #endif /* CONFIG_AP */
1118                 return;
1119         }
1120
1121 #ifdef CONFIG_TDLS
1122         if (bss)
1123                 wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
1124                                 bss->ie_len);
1125 #endif /* CONFIG_TDLS */
1126
1127         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
1128             ssid->mode == IEEE80211_MODE_INFRA) {
1129                 sme_authenticate(wpa_s, bss, ssid);
1130                 return;
1131         }
1132
1133         os_memset(&params, 0, sizeof(params));
1134         wpa_s->reassociate = 0;
1135         if (bss && !wpas_driver_bss_selection(wpa_s)) {
1136 #ifdef CONFIG_IEEE80211R
1137                 const u8 *ie, *md = NULL;
1138 #endif /* CONFIG_IEEE80211R */
1139                 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1140                         " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1141                         wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1142                 bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
1143                 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1144                 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1145                 if (bssid_changed)
1146                         wpas_notify_bssid_changed(wpa_s);
1147 #ifdef CONFIG_IEEE80211R
1148                 ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
1149                 if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
1150                         md = ie + 2;
1151                 wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
1152                 if (md) {
1153                         /* Prepare for the next transition */
1154                         wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
1155                 }
1156 #endif /* CONFIG_IEEE80211R */
1157 #ifdef CONFIG_WPS
1158         } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
1159                    wpa_s->conf->ap_scan == 2 &&
1160                    (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
1161                 /* Use ap_scan==1 style network selection to find the network
1162                  */
1163                 wpa_s->scan_req = 2;
1164                 wpa_s->reassociate = 1;
1165                 wpa_supplicant_req_scan(wpa_s, 0, 0);
1166                 return;
1167 #endif /* CONFIG_WPS */
1168         } else {
1169                 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1170                         wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1171                 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1172         }
1173         wpa_supplicant_cancel_sched_scan(wpa_s);
1174         wpa_supplicant_cancel_scan(wpa_s);
1175
1176         /* Starting new association, so clear the possibly used WPA IE from the
1177          * previous association. */
1178         wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1179
1180 #ifdef IEEE8021X_EAPOL
1181         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1182                 if (ssid->leap) {
1183                         if (ssid->non_leap == 0)
1184                                 algs = WPA_AUTH_ALG_LEAP;
1185                         else
1186                                 algs |= WPA_AUTH_ALG_LEAP;
1187                 }
1188         }
1189 #endif /* IEEE8021X_EAPOL */
1190         wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1191         if (ssid->auth_alg) {
1192                 algs = ssid->auth_alg;
1193                 wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
1194                         "0x%x", algs);
1195         }
1196
1197         if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
1198                     wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
1199             wpa_key_mgmt_wpa(ssid->key_mgmt)) {
1200                 int try_opportunistic;
1201                 try_opportunistic = ssid->proactive_key_caching &&
1202                         (ssid->proto & WPA_PROTO_RSN);
1203                 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1204                                             wpa_s->current_ssid,
1205                                             try_opportunistic) == 0)
1206                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1207                 wpa_ie_len = sizeof(wpa_ie);
1208                 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1209                                               wpa_ie, &wpa_ie_len)) {
1210                         wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1211                                 "key management and encryption suites");
1212                         return;
1213                 }
1214         } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
1215                 wpa_ie_len = sizeof(wpa_ie);
1216                 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1217                                               wpa_ie, &wpa_ie_len)) {
1218                         wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
1219                                 "key management and encryption suites (no "
1220                                 "scan results)");
1221                         return;
1222                 }
1223 #ifdef CONFIG_WPS
1224         } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
1225                 struct wpabuf *wps_ie;
1226                 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
1227                 if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
1228                         wpa_ie_len = wpabuf_len(wps_ie);
1229                         os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
1230                 } else
1231                         wpa_ie_len = 0;
1232                 wpabuf_free(wps_ie);
1233                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1234                 if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
1235                         params.wps = WPS_MODE_PRIVACY;
1236                 else
1237                         params.wps = WPS_MODE_OPEN;
1238                 wpa_s->wpa_proto = 0;
1239 #endif /* CONFIG_WPS */
1240         } else {
1241                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1242                 wpa_ie_len = 0;
1243                 wpa_s->wpa_proto = 0;
1244         }
1245
1246 #ifdef CONFIG_P2P
1247         if (wpa_s->global->p2p) {
1248                 u8 *pos;
1249                 size_t len;
1250                 int res;
1251                 pos = wpa_ie + wpa_ie_len;
1252                 len = sizeof(wpa_ie) - wpa_ie_len;
1253                 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
1254                                             ssid->p2p_group);
1255                 if (res >= 0)
1256                         wpa_ie_len += res;
1257         }
1258
1259         wpa_s->cross_connect_disallowed = 0;
1260         if (bss) {
1261                 struct wpabuf *p2p;
1262                 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
1263                 if (p2p) {
1264                         wpa_s->cross_connect_disallowed =
1265                                 p2p_get_cross_connect_disallowed(p2p);
1266                         wpabuf_free(p2p);
1267                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
1268                                 "connection",
1269                                 wpa_s->cross_connect_disallowed ?
1270                                 "disallows" : "allows");
1271                 }
1272         }
1273 #endif /* CONFIG_P2P */
1274
1275 #ifdef CONFIG_INTERWORKING
1276         if (wpa_s->conf->interworking) {
1277                 u8 *pos = wpa_ie;
1278                 if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
1279                         pos += 2 + pos[1];
1280                 os_memmove(pos + 6, pos, wpa_ie_len - (pos - wpa_ie));
1281                 wpa_ie_len += 6;
1282                 *pos++ = WLAN_EID_EXT_CAPAB;
1283                 *pos++ = 4;
1284                 *pos++ = 0x00;
1285                 *pos++ = 0x00;
1286                 *pos++ = 0x00;
1287                 *pos++ = 0x80; /* Bit 31 - Interworking */
1288         }
1289 #endif /* CONFIG_INTERWORKING */
1290
1291         wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1292         use_crypt = 1;
1293         cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1294         cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1295         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1296             wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1297                 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1298                         use_crypt = 0;
1299                 if (wpa_set_wep_keys(wpa_s, ssid)) {
1300                         use_crypt = 1;
1301                         wep_keys_set = 1;
1302                 }
1303         }
1304         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
1305                 use_crypt = 0;
1306
1307 #ifdef IEEE8021X_EAPOL
1308         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1309                 if ((ssid->eapol_flags &
1310                      (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1311                       EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1312                     !wep_keys_set) {
1313                         use_crypt = 0;
1314                 } else {
1315                         /* Assume that dynamic WEP-104 keys will be used and
1316                          * set cipher suites in order for drivers to expect
1317                          * encryption. */
1318                         cipher_pairwise = cipher_group = CIPHER_WEP104;
1319                 }
1320         }
1321 #endif /* IEEE8021X_EAPOL */
1322
1323         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1324                 /* Set the key before (and later after) association */
1325                 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1326         }
1327
1328         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1329         if (bss) {
1330                 params.ssid = bss->ssid;
1331                 params.ssid_len = bss->ssid_len;
1332                 if (!wpas_driver_bss_selection(wpa_s)) {
1333                         params.bssid = bss->bssid;
1334                         params.freq = bss->freq;
1335                 }
1336         } else {
1337                 params.ssid = ssid->ssid;
1338                 params.ssid_len = ssid->ssid_len;
1339         }
1340
1341         if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
1342             wpa_s->conf->ap_scan == 2) {
1343                 params.bssid = ssid->bssid;
1344                 params.fixed_bssid = 1;
1345         }
1346
1347         if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
1348             params.freq == 0)
1349                 params.freq = ssid->frequency; /* Initial channel for IBSS */
1350         params.wpa_ie = wpa_ie;
1351         params.wpa_ie_len = wpa_ie_len;
1352         params.pairwise_suite = cipher_pairwise;
1353         params.group_suite = cipher_group;
1354         params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1355         params.wpa_proto = wpa_s->wpa_proto;
1356         params.auth_alg = algs;
1357         params.mode = ssid->mode;
1358         params.bg_scan_period = ssid->bg_scan_period;
1359         for (i = 0; i < NUM_WEP_KEYS; i++) {
1360                 if (ssid->wep_key_len[i])
1361                         params.wep_key[i] = ssid->wep_key[i];
1362                 params.wep_key_len[i] = ssid->wep_key_len[i];
1363         }
1364         params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1365
1366         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
1367             (params.key_mgmt_suite == KEY_MGMT_PSK ||
1368              params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
1369                 params.passphrase = ssid->passphrase;
1370                 if (ssid->psk_set)
1371                         params.psk = ssid->psk;
1372         }
1373
1374         params.drop_unencrypted = use_crypt;
1375
1376 #ifdef CONFIG_IEEE80211W
1377         params.mgmt_frame_protection = ssid->ieee80211w;
1378         if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
1379                 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
1380                 struct wpa_ie_data ie;
1381                 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
1382                     ie.capabilities &
1383                     (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
1384                         wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
1385                                 "MFP: require MFP");
1386                         params.mgmt_frame_protection =
1387                                 MGMT_FRAME_PROTECTION_REQUIRED;
1388                 }
1389         }
1390 #endif /* CONFIG_IEEE80211W */
1391
1392         params.p2p = ssid->p2p_group;
1393
1394         if (wpa_s->parent->set_sta_uapsd)
1395                 params.uapsd = wpa_s->parent->sta_uapsd;
1396         else
1397                 params.uapsd = -1;
1398
1399 #ifdef CONFIG_HT_OVERRIDES
1400         os_memset(&htcaps, 0, sizeof(htcaps));
1401         os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
1402         params.htcaps = (u8 *) &htcaps;
1403         params.htcaps_mask = (u8 *) &htcaps_mask;
1404         wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
1405 #endif /* CONFIG_HT_OVERRIDES */
1406
1407         ret = wpa_drv_associate(wpa_s, &params);
1408         if (ret < 0) {
1409                 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1410                         "failed");
1411                 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
1412                         /*
1413                          * The driver is known to mean what is saying, so we
1414                          * can stop right here; the association will not
1415                          * succeed.
1416                          */
1417                         wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
1418                         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1419                         return;
1420                 }
1421                 /* try to continue anyway; new association will be tried again
1422                  * after timeout */
1423                 assoc_failed = 1;
1424         }
1425
1426         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1427                 /* Set the key after the association just in case association
1428                  * cleared the previously configured key. */
1429                 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1430                 /* No need to timeout authentication since there is no key
1431                  * management. */
1432                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1433                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1434 #ifdef CONFIG_IBSS_RSN
1435         } else if (ssid->mode == WPAS_MODE_IBSS &&
1436                    wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
1437                    wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
1438                 /*
1439                  * RSN IBSS authentication is per-STA and we can disable the
1440                  * per-BSSID authentication.
1441                  */
1442                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1443 #endif /* CONFIG_IBSS_RSN */
1444         } else {
1445                 /* Timeout for IEEE 802.11 authentication and association */
1446                 int timeout = 60;
1447
1448                 if (assoc_failed) {
1449                         /* give IBSS a bit more time */
1450                         timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
1451                 } else if (wpa_s->conf->ap_scan == 1) {
1452                         /* give IBSS a bit more time */
1453                         timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
1454                 }
1455                 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1456         }
1457
1458         if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1459             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1460                 /* Set static WEP keys again */
1461                 wpa_set_wep_keys(wpa_s, ssid);
1462         }
1463
1464         if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1465                 /*
1466                  * Do not allow EAP session resumption between different
1467                  * network configurations.
1468                  */
1469                 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1470         }
1471         old_ssid = wpa_s->current_ssid;
1472         wpa_s->current_ssid = ssid;
1473         wpa_s->current_bss = bss;
1474         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
1475         wpa_supplicant_initiate_eapol(wpa_s);
1476         if (old_ssid != wpa_s->current_ssid)
1477                 wpas_notify_network_changed(wpa_s);
1478 }
1479
1480
1481 static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
1482                                             const u8 *addr)
1483 {
1484         struct wpa_ssid *old_ssid;
1485
1486         wpa_clear_keys(wpa_s, addr);
1487         wpa_supplicant_mark_disassoc(wpa_s);
1488         old_ssid = wpa_s->current_ssid;
1489         wpa_s->current_ssid = NULL;
1490         wpa_s->current_bss = NULL;
1491         wpa_sm_set_config(wpa_s->wpa, NULL);
1492         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1493         if (old_ssid != wpa_s->current_ssid)
1494                 wpas_notify_network_changed(wpa_s);
1495         eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
1496 }
1497
1498
1499 /**
1500  * wpa_supplicant_disassociate - Disassociate the current connection
1501  * @wpa_s: Pointer to wpa_supplicant data
1502  * @reason_code: IEEE 802.11 reason code for the disassociate frame
1503  *
1504  * This function is used to request %wpa_supplicant to disassociate with the
1505  * current AP.
1506  */
1507 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1508                                  int reason_code)
1509 {
1510         u8 *addr = NULL;
1511         union wpa_event_data event;
1512
1513         if (!is_zero_ether_addr(wpa_s->bssid)) {
1514                 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1515                 addr = wpa_s->bssid;
1516                 os_memset(&event, 0, sizeof(event));
1517                 event.disassoc_info.reason_code = (u16) reason_code;
1518                 event.disassoc_info.locally_generated = 1;
1519                 wpa_supplicant_event(wpa_s, EVENT_DISASSOC, &event);
1520         }
1521
1522         wpa_supplicant_clear_connection(wpa_s, addr);
1523 }
1524
1525
1526 /**
1527  * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1528  * @wpa_s: Pointer to wpa_supplicant data
1529  * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1530  *
1531  * This function is used to request %wpa_supplicant to deauthenticate from the
1532  * current AP.
1533  */
1534 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1535                                    int reason_code)
1536 {
1537         u8 *addr = NULL;
1538         union wpa_event_data event;
1539
1540         if (!is_zero_ether_addr(wpa_s->bssid)) {
1541                 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid, reason_code);
1542                 addr = wpa_s->bssid;
1543                 os_memset(&event, 0, sizeof(event));
1544                 event.deauth_info.reason_code = (u16) reason_code;
1545                 event.deauth_info.locally_generated = 1;
1546                 wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
1547         }
1548
1549         wpa_supplicant_clear_connection(wpa_s, addr);
1550 }
1551
1552
1553 /**
1554  * wpa_supplicant_enable_network - Mark a configured network as enabled
1555  * @wpa_s: wpa_supplicant structure for a network interface
1556  * @ssid: wpa_ssid structure for a configured network or %NULL
1557  *
1558  * Enables the specified network or all networks if no network specified.
1559  */
1560 void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
1561                                    struct wpa_ssid *ssid)
1562 {
1563         struct wpa_ssid *other_ssid;
1564         int was_disabled;
1565
1566         if (ssid == NULL) {
1567                 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1568                      other_ssid = other_ssid->next) {
1569                         if (other_ssid->disabled == 2)
1570                                 continue; /* do not change persistent P2P group
1571                                            * data */
1572                         if (other_ssid == wpa_s->current_ssid &&
1573                             other_ssid->disabled)
1574                                 wpa_s->reassociate = 1;
1575
1576                         was_disabled = other_ssid->disabled;
1577
1578                         other_ssid->disabled = 0;
1579
1580                         if (was_disabled != other_ssid->disabled)
1581                                 wpas_notify_network_enabled_changed(
1582                                         wpa_s, other_ssid);
1583                 }
1584                 if (wpa_s->reassociate)
1585                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1586         } else if (ssid->disabled && ssid->disabled != 2) {
1587                 if (wpa_s->current_ssid == NULL) {
1588                         /*
1589                          * Try to reassociate since there is no current
1590                          * configuration and a new network was made available.
1591                          */
1592                         wpa_s->reassociate = 1;
1593                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1594                 }
1595
1596                 was_disabled = ssid->disabled;
1597
1598                 ssid->disabled = 0;
1599
1600                 if (was_disabled != ssid->disabled)
1601                         wpas_notify_network_enabled_changed(wpa_s, ssid);
1602         }
1603 }
1604
1605
1606 /**
1607  * wpa_supplicant_disable_network - Mark a configured network as disabled
1608  * @wpa_s: wpa_supplicant structure for a network interface
1609  * @ssid: wpa_ssid structure for a configured network or %NULL
1610  *
1611  * Disables the specified network or all networks if no network specified.
1612  */
1613 void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
1614                                     struct wpa_ssid *ssid)
1615 {
1616         struct wpa_ssid *other_ssid;
1617         int was_disabled;
1618
1619         if (ssid == NULL) {
1620                 for (other_ssid = wpa_s->conf->ssid; other_ssid;
1621                      other_ssid = other_ssid->next) {
1622                         was_disabled = other_ssid->disabled;
1623                         if (was_disabled == 2)
1624                                 continue; /* do not change persistent P2P group
1625                                            * data */
1626
1627                         other_ssid->disabled = 1;
1628
1629                         if (was_disabled != other_ssid->disabled)
1630                                 wpas_notify_network_enabled_changed(
1631                                         wpa_s, other_ssid);
1632                 }
1633                 if (wpa_s->current_ssid)
1634                         wpa_supplicant_disassociate(
1635                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1636         } else if (ssid->disabled != 2) {
1637                 if (ssid == wpa_s->current_ssid)
1638                         wpa_supplicant_disassociate(
1639                                 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1640
1641                 was_disabled = ssid->disabled;
1642
1643                 ssid->disabled = 1;
1644
1645                 if (was_disabled != ssid->disabled)
1646                         wpas_notify_network_enabled_changed(wpa_s, ssid);
1647         }
1648 }
1649
1650
1651 /**
1652  * wpa_supplicant_select_network - Attempt association with a network
1653  * @wpa_s: wpa_supplicant structure for a network interface
1654  * @ssid: wpa_ssid structure for a configured network or %NULL for any network
1655  */
1656 void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
1657                                    struct wpa_ssid *ssid)
1658 {
1659
1660         struct wpa_ssid *other_ssid;
1661         int disconnected = 0;
1662
1663         if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
1664                 wpa_supplicant_disassociate(
1665                         wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1666                 disconnected = 1;
1667         }
1668
1669         /*
1670          * Mark all other networks disabled or mark all networks enabled if no
1671          * network specified.
1672          */
1673         for (other_ssid = wpa_s->conf->ssid; other_ssid;
1674              other_ssid = other_ssid->next) {
1675                 int was_disabled = other_ssid->disabled;
1676                 if (was_disabled == 2)
1677                         continue; /* do not change persistent P2P group data */
1678
1679                 other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
1680
1681                 if (was_disabled != other_ssid->disabled)
1682                         wpas_notify_network_enabled_changed(wpa_s, other_ssid);
1683         }
1684
1685         if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
1686                 /* We are already associated with the selected network */
1687                 wpa_printf(MSG_DEBUG, "Already associated with the "
1688                            "selected network - do nothing");
1689                 return;
1690         }
1691
1692         if (ssid)
1693                 wpa_s->current_ssid = ssid;
1694         wpa_s->connect_without_scan = NULL;
1695         wpa_s->disconnected = 0;
1696         wpa_s->reassociate = 1;
1697         wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
1698
1699         if (ssid)
1700                 wpas_notify_network_selected(wpa_s, ssid);
1701 }
1702
1703
1704 /**
1705  * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
1706  * @wpa_s: wpa_supplicant structure for a network interface
1707  * @ap_scan: AP scan mode
1708  * Returns: 0 if succeed or -1 if ap_scan has an invalid value
1709  *
1710  */
1711 int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
1712 {
1713
1714         int old_ap_scan;
1715
1716         if (ap_scan < 0 || ap_scan > 2)
1717                 return -1;
1718
1719 #ifdef ANDROID
1720         if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
1721             wpa_s->wpa_state >= WPA_ASSOCIATING &&
1722             wpa_s->wpa_state < WPA_COMPLETED) {
1723                 wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
1724                            "associating", wpa_s->conf->ap_scan, ap_scan);
1725                 return 0;
1726         }
1727 #endif /* ANDROID */
1728
1729         old_ap_scan = wpa_s->conf->ap_scan;
1730         wpa_s->conf->ap_scan = ap_scan;
1731
1732         if (old_ap_scan != wpa_s->conf->ap_scan)
1733                 wpas_notify_ap_scan_changed(wpa_s);
1734
1735         return 0;
1736 }
1737
1738
1739 /**
1740  * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
1741  * @wpa_s: wpa_supplicant structure for a network interface
1742  * @expire_age: Expiration age in seconds
1743  * Returns: 0 if succeed or -1 if expire_age has an invalid value
1744  *
1745  */
1746 int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
1747                                           unsigned int bss_expire_age)
1748 {
1749         if (bss_expire_age < 10) {
1750                 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
1751                         bss_expire_age);
1752                 return -1;
1753         }
1754         wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
1755                 bss_expire_age);
1756         wpa_s->conf->bss_expiration_age = bss_expire_age;
1757
1758         return 0;
1759 }
1760
1761
1762 /**
1763  * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
1764  * @wpa_s: wpa_supplicant structure for a network interface
1765  * @expire_count: number of scans after which an unseen BSS is reclaimed
1766  * Returns: 0 if succeed or -1 if expire_count has an invalid value
1767  *
1768  */
1769 int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
1770                                             unsigned int bss_expire_count)
1771 {
1772         if (bss_expire_count < 1) {
1773                 wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
1774                         bss_expire_count);
1775                 return -1;
1776         }
1777         wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
1778                 bss_expire_count);
1779         wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
1780
1781         return 0;
1782 }
1783
1784
1785 /**
1786  * wpa_supplicant_set_debug_params - Set global debug params
1787  * @global: wpa_global structure
1788  * @debug_level: debug level
1789  * @debug_timestamp: determines if show timestamp in debug data
1790  * @debug_show_keys: determines if show keys in debug data
1791  * Returns: 0 if succeed or -1 if debug_level has wrong value
1792  */
1793 int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
1794                                     int debug_timestamp, int debug_show_keys)
1795 {
1796
1797         int old_level, old_timestamp, old_show_keys;
1798
1799         /* check for allowed debuglevels */
1800         if (debug_level != MSG_EXCESSIVE &&
1801             debug_level != MSG_MSGDUMP &&
1802             debug_level != MSG_DEBUG &&
1803             debug_level != MSG_INFO &&
1804             debug_level != MSG_WARNING &&
1805             debug_level != MSG_ERROR)
1806                 return -1;
1807
1808         old_level = wpa_debug_level;
1809         old_timestamp = wpa_debug_timestamp;
1810         old_show_keys = wpa_debug_show_keys;
1811
1812         wpa_debug_level = debug_level;
1813         wpa_debug_timestamp = debug_timestamp ? 1 : 0;
1814         wpa_debug_show_keys = debug_show_keys ? 1 : 0;
1815
1816         if (wpa_debug_level != old_level)
1817                 wpas_notify_debug_level_changed(global);
1818         if (wpa_debug_timestamp != old_timestamp)
1819                 wpas_notify_debug_timestamp_changed(global);
1820         if (wpa_debug_show_keys != old_show_keys)
1821                 wpas_notify_debug_show_keys_changed(global);
1822
1823         return 0;
1824 }
1825
1826
1827 /**
1828  * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1829  * @wpa_s: Pointer to wpa_supplicant data
1830  * Returns: A pointer to the current network structure or %NULL on failure
1831  */
1832 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1833 {
1834         struct wpa_ssid *entry;
1835         u8 ssid[MAX_SSID_LEN];
1836         int res;
1837         size_t ssid_len;
1838         u8 bssid[ETH_ALEN];
1839         int wired;
1840
1841         res = wpa_drv_get_ssid(wpa_s, ssid);
1842         if (res < 0) {
1843                 wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
1844                         "driver");
1845                 return NULL;
1846         }
1847         ssid_len = res;
1848
1849         if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1850                 wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
1851                         "driver");
1852                 return NULL;
1853         }
1854
1855         wired = wpa_s->conf->ap_scan == 0 &&
1856                 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
1857
1858         entry = wpa_s->conf->ssid;
1859         while (entry) {
1860                 if (!entry->disabled &&
1861                     ((ssid_len == entry->ssid_len &&
1862                       os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1863                     (!entry->bssid_set ||
1864                      os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1865                         return entry;
1866 #ifdef CONFIG_WPS
1867                 if (!entry->disabled &&
1868                     (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
1869                     (entry->ssid == NULL || entry->ssid_len == 0) &&
1870                     (!entry->bssid_set ||
1871                      os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1872                         return entry;
1873 #endif /* CONFIG_WPS */
1874
1875                 if (!entry->disabled && entry->bssid_set &&
1876                     entry->ssid_len == 0 &&
1877                     os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
1878                         return entry;
1879
1880                 entry = entry->next;
1881         }
1882
1883         return NULL;
1884 }
1885
1886
1887 static int select_driver(struct wpa_supplicant *wpa_s, int i)
1888 {
1889         struct wpa_global *global = wpa_s->global;
1890
1891         if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
1892                 global->drv_priv[i] = wpa_drivers[i]->global_init();
1893                 if (global->drv_priv[i] == NULL) {
1894                         wpa_printf(MSG_ERROR, "Failed to initialize driver "
1895                                    "'%s'", wpa_drivers[i]->name);
1896                         return -1;
1897                 }
1898         }
1899
1900         wpa_s->driver = wpa_drivers[i];
1901         wpa_s->global_drv_priv = global->drv_priv[i];
1902
1903         return 0;
1904 }
1905
1906
1907 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1908                                      const char *name)
1909 {
1910         int i;
1911         size_t len;
1912         const char *pos, *driver = name;
1913
1914         if (wpa_s == NULL)
1915                 return -1;
1916
1917         if (wpa_drivers[0] == NULL) {
1918                 wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
1919                         "wpa_supplicant");
1920                 return -1;
1921         }
1922
1923         if (name == NULL) {
1924                 /* default to first driver in the list */
1925                 return select_driver(wpa_s, 0);
1926         }
1927
1928         do {
1929                 pos = os_strchr(driver, ',');
1930                 if (pos)
1931                         len = pos - driver;
1932                 else
1933                         len = os_strlen(driver);
1934
1935                 for (i = 0; wpa_drivers[i]; i++) {
1936                         if (os_strlen(wpa_drivers[i]->name) == len &&
1937                             os_strncmp(driver, wpa_drivers[i]->name, len) ==
1938                             0) {
1939                                 /* First driver that succeeds wins */
1940                                 if (select_driver(wpa_s, i) == 0)
1941                                         return 0;
1942                         }
1943                 }
1944
1945                 driver = pos + 1;
1946         } while (pos);
1947
1948         wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
1949         return -1;
1950 }
1951
1952
1953 /**
1954  * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
1955  * @ctx: Context pointer (wpa_s); this is the ctx variable registered
1956  *      with struct wpa_driver_ops::init()
1957  * @src_addr: Source address of the EAPOL frame
1958  * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
1959  * @len: Length of the EAPOL data
1960  *
1961  * This function is called for each received EAPOL frame. Most driver
1962  * interfaces rely on more generic OS mechanism for receiving frames through
1963  * l2_packet, but if such a mechanism is not available, the driver wrapper may
1964  * take care of received EAPOL frames and deliver them to the core supplicant
1965  * code by calling this function.
1966  */
1967 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1968                              const u8 *buf, size_t len)
1969 {
1970         struct wpa_supplicant *wpa_s = ctx;
1971
1972         wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1973         wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1974
1975         if (wpa_s->wpa_state < WPA_ASSOCIATED) {
1976                 /*
1977                  * There is possible race condition between receiving the
1978                  * association event and the EAPOL frame since they are coming
1979                  * through different paths from the driver. In order to avoid
1980                  * issues in trying to process the EAPOL frame before receiving
1981                  * association information, lets queue it for processing until
1982                  * the association event is received.
1983                  */
1984                 wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
1985                         "of received EAPOL frame");
1986                 wpabuf_free(wpa_s->pending_eapol_rx);
1987                 wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
1988                 if (wpa_s->pending_eapol_rx) {
1989                         os_get_time(&wpa_s->pending_eapol_rx_time);
1990                         os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
1991                                   ETH_ALEN);
1992                 }
1993                 return;
1994         }
1995
1996 #ifdef CONFIG_AP
1997         if (wpa_s->ap_iface) {
1998                 wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
1999                 return;
2000         }
2001 #endif /* CONFIG_AP */
2002
2003         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
2004                 wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
2005                         "no key management is configured");
2006                 return;
2007         }
2008
2009         if (wpa_s->eapol_received == 0 &&
2010             (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
2011              !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
2012              wpa_s->wpa_state != WPA_COMPLETED) &&
2013             (wpa_s->current_ssid == NULL ||
2014              wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
2015                 /* Timeout for completing IEEE 802.1X and WPA authentication */
2016                 wpa_supplicant_req_auth_timeout(
2017                         wpa_s,
2018                         (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
2019                          wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
2020                          wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
2021                         70 : 10, 0);
2022         }
2023         wpa_s->eapol_received++;
2024
2025         if (wpa_s->countermeasures) {
2026                 wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
2027                         "EAPOL packet");
2028                 return;
2029         }
2030
2031 #ifdef CONFIG_IBSS_RSN
2032         if (wpa_s->current_ssid &&
2033             wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
2034                 ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
2035                 return;
2036         }
2037 #endif /* CONFIG_IBSS_RSN */
2038
2039         /* Source address of the incoming EAPOL frame could be compared to the
2040          * current BSSID. However, it is possible that a centralized
2041          * Authenticator could be using another MAC address than the BSSID of
2042          * an AP, so just allow any address to be used for now. The replies are
2043          * still sent to the current BSSID (if available), though. */
2044
2045         os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
2046         if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
2047             eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
2048                 return;
2049         wpa_drv_poll(wpa_s);
2050         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
2051                 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
2052         else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
2053                 /*
2054                  * Set portValid = TRUE here since we are going to skip 4-way
2055                  * handshake processing which would normally set portValid. We
2056                  * need this to allow the EAPOL state machines to be completed
2057                  * without going through EAPOL-Key handshake.
2058                  */
2059                 eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
2060         }
2061 }
2062
2063
2064 int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
2065 {
2066         if (wpa_s->driver->send_eapol) {
2067                 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2068                 if (addr)
2069                         os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2070         } else if (!(wpa_s->drv_flags &
2071                      WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
2072                 l2_packet_deinit(wpa_s->l2);
2073                 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2074                                            wpa_drv_get_mac_addr(wpa_s),
2075                                            ETH_P_EAPOL,
2076                                            wpa_supplicant_rx_eapol, wpa_s, 0);
2077                 if (wpa_s->l2 == NULL)
2078                         return -1;
2079         } else {
2080                 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2081                 if (addr)
2082                         os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2083         }
2084
2085         if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
2086                 wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
2087                 return -1;
2088         }
2089
2090         wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
2091                 MAC2STR(wpa_s->own_addr));
2092         wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2093
2094         return 0;
2095 }
2096
2097
2098 static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
2099                                            const u8 *buf, size_t len)
2100 {
2101         struct wpa_supplicant *wpa_s = ctx;
2102         const struct l2_ethhdr *eth;
2103
2104         if (len < sizeof(*eth))
2105                 return;
2106         eth = (const struct l2_ethhdr *) buf;
2107
2108         if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
2109             !(eth->h_dest[0] & 0x01)) {
2110                 wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2111                         " (bridge - not for this interface - ignore)",
2112                         MAC2STR(src_addr), MAC2STR(eth->h_dest));
2113                 return;
2114         }
2115
2116         wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
2117                 " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
2118         wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
2119                                 len - sizeof(*eth));
2120 }
2121
2122
2123 /**
2124  * wpa_supplicant_driver_init - Initialize driver interface parameters
2125  * @wpa_s: Pointer to wpa_supplicant data
2126  * Returns: 0 on success, -1 on failure
2127  *
2128  * This function is called to initialize driver interface parameters.
2129  * wpa_drv_init() must have been called before this function to initialize the
2130  * driver interface.
2131  */
2132 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
2133 {
2134         static int interface_count = 0;
2135
2136         if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
2137                 return -1;
2138
2139         if (wpa_s->bridge_ifname[0]) {
2140                 wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
2141                         "interface '%s'", wpa_s->bridge_ifname);
2142                 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2143                                               wpa_s->own_addr,
2144                                               ETH_P_EAPOL,
2145                                               wpa_supplicant_rx_eapol_bridge,
2146                                               wpa_s, 1);
2147                 if (wpa_s->l2_br == NULL) {
2148                         wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
2149                                 "connection for the bridge interface '%s'",
2150                                 wpa_s->bridge_ifname);
2151                         return -1;
2152                 }
2153         }
2154
2155         wpa_clear_keys(wpa_s, NULL);
2156
2157         /* Make sure that TKIP countermeasures are not left enabled (could
2158          * happen if wpa_supplicant is killed during countermeasures. */
2159         wpa_drv_set_countermeasures(wpa_s, 0);
2160
2161         wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
2162         wpa_drv_flush_pmkid(wpa_s);
2163
2164         wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
2165         wpa_s->prev_scan_wildcard = 0;
2166
2167         if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
2168                 if (wpa_supplicant_delayed_sched_scan(wpa_s, interface_count,
2169                                                       100000))
2170                         wpa_supplicant_req_scan(wpa_s, interface_count,
2171                                                 100000);
2172                 interface_count++;
2173         } else
2174                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
2175
2176         return 0;
2177 }
2178
2179
2180 static int wpa_supplicant_daemon(const char *pid_file)
2181 {
2182         wpa_printf(MSG_DEBUG, "Daemonize..");
2183         return os_daemonize(pid_file);
2184 }
2185
2186
2187 static struct wpa_supplicant * wpa_supplicant_alloc(void)
2188 {
2189         struct wpa_supplicant *wpa_s;
2190
2191         wpa_s = os_zalloc(sizeof(*wpa_s));
2192         if (wpa_s == NULL)
2193                 return NULL;
2194         wpa_s->scan_req = 1;
2195         wpa_s->scan_interval = 5;
2196         wpa_s->new_connection = 1;
2197         wpa_s->parent = wpa_s;
2198         wpa_s->sched_scanning = 0;
2199
2200         return wpa_s;
2201 }
2202
2203
2204 #ifdef CONFIG_HT_OVERRIDES
2205
2206 static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
2207                              struct ieee80211_ht_capabilities *htcaps,
2208                              struct ieee80211_ht_capabilities *htcaps_mask,
2209                              const char *ht_mcs)
2210 {
2211         /* parse ht_mcs into hex array */
2212         int i;
2213         const char *tmp = ht_mcs;
2214         char *end = NULL;
2215
2216         /* If ht_mcs is null, do not set anything */
2217         if (!ht_mcs)
2218                 return 0;
2219
2220         /* This is what we are setting in the kernel */
2221         os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
2222
2223         wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
2224
2225         for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
2226                 errno = 0;
2227                 long v = strtol(tmp, &end, 16);
2228                 if (errno == 0) {
2229                         wpa_msg(wpa_s, MSG_DEBUG,
2230                                 "htcap value[%i]: %ld end: %p  tmp: %p",
2231                                 i, v, end, tmp);
2232                         if (end == tmp)
2233                                 break;
2234
2235                         htcaps->supported_mcs_set[i] = v;
2236                         tmp = end;
2237                 } else {
2238                         wpa_msg(wpa_s, MSG_ERROR,
2239                                 "Failed to parse ht-mcs: %s, error: %s\n",
2240                                 ht_mcs, strerror(errno));
2241                         return -1;
2242                 }
2243         }
2244
2245         /*
2246          * If we were able to parse any values, then set mask for the MCS set.
2247          */
2248         if (i) {
2249                 os_memset(&htcaps_mask->supported_mcs_set, 0xff,
2250                           IEEE80211_HT_MCS_MASK_LEN - 1);
2251                 /* skip the 3 reserved bits */
2252                 htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
2253                         0x1f;
2254         }
2255
2256         return 0;
2257 }
2258
2259
2260 static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
2261                                  struct ieee80211_ht_capabilities *htcaps,
2262                                  struct ieee80211_ht_capabilities *htcaps_mask,
2263                                  int disabled)
2264 {
2265         u16 msk;
2266
2267         wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
2268
2269         if (disabled == -1)
2270                 return 0;
2271
2272         msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
2273         htcaps_mask->ht_capabilities_info |= msk;
2274         if (disabled)
2275                 htcaps->ht_capabilities_info &= msk;
2276         else
2277                 htcaps->ht_capabilities_info |= msk;
2278
2279         return 0;
2280 }
2281
2282
2283 static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
2284                                 struct ieee80211_ht_capabilities *htcaps,
2285                                 struct ieee80211_ht_capabilities *htcaps_mask,
2286                                 int factor)
2287 {
2288         wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
2289
2290         if (factor == -1)
2291                 return 0;
2292
2293         if (factor < 0 || factor > 3) {
2294                 wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
2295                         "Must be 0-3 or -1", factor);
2296                 return -EINVAL;
2297         }
2298
2299         htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
2300         htcaps->a_mpdu_params &= ~0x3;
2301         htcaps->a_mpdu_params |= factor & 0x3;
2302
2303         return 0;
2304 }
2305
2306
2307 static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
2308                                  struct ieee80211_ht_capabilities *htcaps,
2309                                  struct ieee80211_ht_capabilities *htcaps_mask,
2310                                  int density)
2311 {
2312         wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
2313
2314         if (density == -1)
2315                 return 0;
2316
2317         if (density < 0 || density > 7) {
2318                 wpa_msg(wpa_s, MSG_ERROR,
2319                         "ampdu_density: %d out of range. Must be 0-7 or -1.",
2320                         density);
2321                 return -EINVAL;
2322         }
2323
2324         htcaps_mask->a_mpdu_params |= 0x1C;
2325         htcaps->a_mpdu_params &= ~(0x1C);
2326         htcaps->a_mpdu_params |= (density << 2) & 0x1C;
2327
2328         return 0;
2329 }
2330
2331
2332 static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
2333                                 struct ieee80211_ht_capabilities *htcaps,
2334                                 struct ieee80211_ht_capabilities *htcaps_mask,
2335                                 int disabled)
2336 {
2337         /* Masking these out disables HT40 */
2338         u16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
2339                                HT_CAP_INFO_SHORT_GI40MHZ);
2340
2341         wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
2342
2343         if (disabled)
2344                 htcaps->ht_capabilities_info &= ~msk;
2345         else
2346                 htcaps->ht_capabilities_info |= msk;
2347
2348         htcaps_mask->ht_capabilities_info |= msk;
2349
2350         return 0;
2351 }
2352
2353
2354 void wpa_supplicant_apply_ht_overrides(
2355         struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
2356         struct wpa_driver_associate_params *params)
2357 {
2358         struct ieee80211_ht_capabilities *htcaps;
2359         struct ieee80211_ht_capabilities *htcaps_mask;
2360
2361         if (!ssid)
2362                 return;
2363
2364         params->disable_ht = ssid->disable_ht;
2365         if (!params->htcaps || !params->htcaps_mask)
2366                 return;
2367
2368         htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
2369         htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
2370         wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
2371         wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
2372                               ssid->disable_max_amsdu);
2373         wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
2374         wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
2375         wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
2376 }
2377
2378 #endif /* CONFIG_HT_OVERRIDES */
2379
2380
2381 static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
2382 {
2383 #ifdef PCSC_FUNCS
2384         size_t len;
2385
2386         if (!wpa_s->conf->pcsc_reader)
2387                 return 0;
2388
2389         wpa_s->scard = scard_init(SCARD_TRY_BOTH, wpa_s->conf->pcsc_reader);
2390         if (!wpa_s->scard)
2391                 return 1;
2392
2393         if (wpa_s->conf->pcsc_pin &&
2394             scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
2395                 scard_deinit(wpa_s->scard);
2396                 wpa_s->scard = NULL;
2397                 wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
2398                 return -1;
2399         }
2400
2401         len = sizeof(wpa_s->imsi) - 1;
2402         if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
2403                 scard_deinit(wpa_s->scard);
2404                 wpa_s->scard = NULL;
2405                 wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
2406                 return -1;
2407         }
2408         wpa_s->imsi[len] = '\0';
2409
2410         wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
2411
2412         wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
2413                    wpa_s->imsi, wpa_s->mnc_len);
2414
2415         wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
2416         eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
2417 #endif /* PCSC_FUNCS */
2418
2419         return 0;
2420 }
2421
2422
2423 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
2424                                      struct wpa_interface *iface)
2425 {
2426         const char *ifname, *driver;
2427         struct wpa_driver_capa capa;
2428
2429         wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
2430                    "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
2431                    iface->confname ? iface->confname : "N/A",
2432                    iface->driver ? iface->driver : "default",
2433                    iface->ctrl_interface ? iface->ctrl_interface : "N/A",
2434                    iface->bridge_ifname ? iface->bridge_ifname : "N/A");
2435
2436         if (iface->confname) {
2437 #ifdef CONFIG_BACKEND_FILE
2438                 wpa_s->confname = os_rel2abs_path(iface->confname);
2439                 if (wpa_s->confname == NULL) {
2440                         wpa_printf(MSG_ERROR, "Failed to get absolute path "
2441                                    "for configuration file '%s'.",
2442                                    iface->confname);
2443                         return -1;
2444                 }
2445                 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
2446                            iface->confname, wpa_s->confname);
2447 #else /* CONFIG_BACKEND_FILE */
2448                 wpa_s->confname = os_strdup(iface->confname);
2449 #endif /* CONFIG_BACKEND_FILE */
2450                 wpa_s->conf = wpa_config_read(wpa_s->confname);
2451                 if (wpa_s->conf == NULL) {
2452                         wpa_printf(MSG_ERROR, "Failed to read or parse "
2453                                    "configuration '%s'.", wpa_s->confname);
2454                         return -1;
2455                 }
2456
2457                 /*
2458                  * Override ctrl_interface and driver_param if set on command
2459                  * line.
2460                  */
2461                 if (iface->ctrl_interface) {
2462                         os_free(wpa_s->conf->ctrl_interface);
2463                         wpa_s->conf->ctrl_interface =
2464                                 os_strdup(iface->ctrl_interface);
2465                 }
2466
2467                 if (iface->driver_param) {
2468                         os_free(wpa_s->conf->driver_param);
2469                         wpa_s->conf->driver_param =
2470                                 os_strdup(iface->driver_param);
2471                 }
2472         } else
2473                 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2474                                                      iface->driver_param);
2475
2476         if (wpa_s->conf == NULL) {
2477                 wpa_printf(MSG_ERROR, "\nNo configuration found.");
2478                 return -1;
2479         }
2480
2481         if (iface->ifname == NULL) {
2482                 wpa_printf(MSG_ERROR, "\nInterface name is required.");
2483                 return -1;
2484         }
2485         if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2486                 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2487                            iface->ifname);
2488                 return -1;
2489         }
2490         os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2491
2492         if (iface->bridge_ifname) {
2493                 if (os_strlen(iface->bridge_ifname) >=
2494                     sizeof(wpa_s->bridge_ifname)) {
2495                         wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2496                                    "name '%s'.", iface->bridge_ifname);
2497                         return -1;
2498                 }
2499                 os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2500                            sizeof(wpa_s->bridge_ifname));
2501         }
2502
2503         /* RSNA Supplicant Key Management - INITIALIZE */
2504         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2505         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2506
2507         /* Initialize driver interface and register driver event handler before
2508          * L2 receive handler so that association events are processed before
2509          * EAPOL-Key packets if both become available for the same select()
2510          * call. */
2511         driver = iface->driver;
2512 next_driver:
2513         if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
2514                 return -1;
2515
2516         wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2517         if (wpa_s->drv_priv == NULL) {
2518                 const char *pos;
2519                 pos = driver ? os_strchr(driver, ',') : NULL;
2520                 if (pos) {
2521                         wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
2522                                 "driver interface - try next driver wrapper");
2523                         driver = pos + 1;
2524                         goto next_driver;
2525                 }
2526                 wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
2527                         "interface");
2528                 return -1;
2529         }
2530         if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2531                 wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
2532                         "driver_param '%s'", wpa_s->conf->driver_param);
2533                 return -1;
2534         }
2535
2536         ifname = wpa_drv_get_ifname(wpa_s);
2537         if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2538                 wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
2539                         "interface name with '%s'", ifname);
2540                 os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2541         }
2542
2543         if (wpa_supplicant_init_wpa(wpa_s) < 0)
2544                 return -1;
2545
2546         wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2547                           wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2548                           NULL);
2549         wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2550
2551         if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2552             wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2553                              wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2554                 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2555                         "dot11RSNAConfigPMKLifetime");
2556                 return -1;
2557         }
2558
2559         if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2560             wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2561                              wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2562                 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2563                         "dot11RSNAConfigPMKReauthThreshold");
2564                 return -1;
2565         }
2566
2567         if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2568             wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2569                              wpa_s->conf->dot11RSNAConfigSATimeout)) {
2570                 wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
2571                         "dot11RSNAConfigSATimeout");
2572                 return -1;
2573         }
2574
2575         wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
2576                                                       &wpa_s->hw.num_modes,
2577                                                       &wpa_s->hw.flags);
2578
2579         if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
2580                 wpa_s->drv_capa_known = 1;
2581                 wpa_s->drv_flags = capa.flags;
2582                 wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
2583                 wpa_s->max_scan_ssids = capa.max_scan_ssids;
2584                 wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
2585                 wpa_s->sched_scan_supported = capa.sched_scan_supported;
2586                 wpa_s->max_match_sets = capa.max_match_sets;
2587                 wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
2588                 wpa_s->max_stations = capa.max_stations;
2589         }
2590         if (wpa_s->max_remain_on_chan == 0)
2591                 wpa_s->max_remain_on_chan = 1000;
2592
2593         if (wpa_supplicant_driver_init(wpa_s) < 0)
2594                 return -1;
2595
2596 #ifdef CONFIG_TDLS
2597         if (wpa_tdls_init(wpa_s->wpa))
2598                 return -1;
2599 #endif /* CONFIG_TDLS */
2600
2601         if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
2602             wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
2603                 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
2604                 return -1;
2605         }
2606
2607         if (wpas_wps_init(wpa_s))
2608                 return -1;
2609
2610         if (wpa_supplicant_init_eapol(wpa_s) < 0)
2611                 return -1;
2612         wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2613
2614         wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2615         if (wpa_s->ctrl_iface == NULL) {
2616                 wpa_printf(MSG_ERROR,
2617                            "Failed to initialize control interface '%s'.\n"
2618                            "You may have another wpa_supplicant process "
2619                            "already running or the file was\n"
2620                            "left by an unclean termination of wpa_supplicant "
2621                            "in which case you will need\n"
2622                            "to manually remove this file before starting "
2623                            "wpa_supplicant again.\n",
2624                            wpa_s->conf->ctrl_interface);
2625                 return -1;
2626         }
2627
2628         wpa_s->gas = gas_query_init(wpa_s);
2629         if (wpa_s->gas == NULL) {
2630                 wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
2631                 return -1;
2632         }
2633
2634 #ifdef CONFIG_P2P
2635         if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
2636                 wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
2637                 return -1;
2638         }
2639 #endif /* CONFIG_P2P */
2640
2641         if (wpa_bss_init(wpa_s) < 0)
2642                 return -1;
2643
2644         if (pcsc_reader_init(wpa_s) < 0)
2645                 return -1;
2646
2647         return 0;
2648 }
2649
2650
2651 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
2652                                         int notify, int terminate)
2653 {
2654         if (wpa_s->drv_priv) {
2655                 wpa_supplicant_deauthenticate(wpa_s,
2656                                               WLAN_REASON_DEAUTH_LEAVING);
2657
2658                 wpa_drv_set_countermeasures(wpa_s, 0);
2659                 wpa_clear_keys(wpa_s, NULL);
2660         }
2661
2662         wpa_supplicant_cleanup(wpa_s);
2663
2664         if (wpa_s->drv_priv)
2665                 wpa_drv_deinit(wpa_s);
2666
2667         if (notify)
2668                 wpas_notify_iface_removed(wpa_s);
2669
2670         if (terminate)
2671                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
2672
2673         if (wpa_s->ctrl_iface) {
2674                 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
2675                 wpa_s->ctrl_iface = NULL;
2676         }
2677
2678         if (wpa_s->conf != NULL) {
2679                 wpa_config_free(wpa_s->conf);
2680                 wpa_s->conf = NULL;
2681         }
2682 }
2683
2684
2685 /**
2686  * wpa_supplicant_add_iface - Add a new network interface
2687  * @global: Pointer to global data from wpa_supplicant_init()
2688  * @iface: Interface configuration options
2689  * Returns: Pointer to the created interface or %NULL on failure
2690  *
2691  * This function is used to add new network interfaces for %wpa_supplicant.
2692  * This can be called before wpa_supplicant_run() to add interfaces before the
2693  * main event loop has been started. In addition, new interfaces can be added
2694  * dynamically while %wpa_supplicant is already running. This could happen,
2695  * e.g., when a hotplug network adapter is inserted.
2696  */
2697 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2698                                                  struct wpa_interface *iface)
2699 {
2700         struct wpa_supplicant *wpa_s;
2701         struct wpa_interface t_iface;
2702         struct wpa_ssid *ssid;
2703
2704         if (global == NULL || iface == NULL)
2705                 return NULL;
2706
2707         wpa_s = wpa_supplicant_alloc();
2708         if (wpa_s == NULL)
2709                 return NULL;
2710
2711         wpa_s->global = global;
2712
2713         t_iface = *iface;
2714         if (global->params.override_driver) {
2715                 wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
2716                            "('%s' -> '%s')",
2717                            iface->driver, global->params.override_driver);
2718                 t_iface.driver = global->params.override_driver;
2719         }
2720         if (global->params.override_ctrl_interface) {
2721                 wpa_printf(MSG_DEBUG, "Override interface parameter: "
2722                            "ctrl_interface ('%s' -> '%s')",
2723                            iface->ctrl_interface,
2724                            global->params.override_ctrl_interface);
2725                 t_iface.ctrl_interface =
2726                         global->params.override_ctrl_interface;
2727         }
2728         if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
2729                 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2730                            iface->ifname);
2731                 wpa_supplicant_deinit_iface(wpa_s, 0, 0);
2732                 os_free(wpa_s);
2733                 return NULL;
2734         }
2735
2736         /* Notify the control interfaces about new iface */
2737         if (wpas_notify_iface_added(wpa_s)) {
2738                 wpa_supplicant_deinit_iface(wpa_s, 1, 0);
2739                 os_free(wpa_s);
2740                 return NULL;
2741         }
2742
2743         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2744                 wpas_notify_network_added(wpa_s, ssid);
2745
2746         wpa_s->next = global->ifaces;
2747         global->ifaces = wpa_s;
2748
2749         wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2750
2751         return wpa_s;
2752 }
2753
2754
2755 /**
2756  * wpa_supplicant_remove_iface - Remove a network interface
2757  * @global: Pointer to global data from wpa_supplicant_init()
2758  * @wpa_s: Pointer to the network interface to be removed
2759  * Returns: 0 if interface was removed, -1 if interface was not found
2760  *
2761  * This function can be used to dynamically remove network interfaces from
2762  * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2763  * addition, this function is used to remove all remaining interfaces when
2764  * %wpa_supplicant is terminated.
2765  */
2766 int wpa_supplicant_remove_iface(struct wpa_global *global,
2767                                 struct wpa_supplicant *wpa_s,
2768                                 int terminate)
2769 {
2770         struct wpa_supplicant *prev;
2771
2772         /* Remove interface from the global list of interfaces */
2773         prev = global->ifaces;
2774         if (prev == wpa_s) {
2775                 global->ifaces = wpa_s->next;
2776         } else {
2777                 while (prev && prev->next != wpa_s)
2778                         prev = prev->next;
2779                 if (prev == NULL)
2780                         return -1;
2781                 prev->next = wpa_s->next;
2782         }
2783
2784         wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2785
2786         if (global->p2p_group_formation == wpa_s)
2787                 global->p2p_group_formation = NULL;
2788         wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
2789         os_free(wpa_s);
2790
2791         return 0;
2792 }
2793
2794
2795 /**
2796  * wpa_supplicant_get_eap_mode - Get the current EAP mode
2797  * @wpa_s: Pointer to the network interface
2798  * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
2799  */
2800 const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
2801 {
2802         const char *eapol_method;
2803
2804         if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
2805             wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
2806                 return "NO-EAP";
2807         }
2808
2809         eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
2810         if (eapol_method == NULL)
2811                 return "UNKNOWN-EAP";
2812
2813         return eapol_method;
2814 }
2815
2816
2817 /**
2818  * wpa_supplicant_get_iface - Get a new network interface
2819  * @global: Pointer to global data from wpa_supplicant_init()
2820  * @ifname: Interface name
2821  * Returns: Pointer to the interface or %NULL if not found
2822  */
2823 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2824                                                  const char *ifname)
2825 {
2826         struct wpa_supplicant *wpa_s;
2827
2828         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2829                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2830                         return wpa_s;
2831         }
2832         return NULL;
2833 }
2834
2835
2836 #ifndef CONFIG_NO_WPA_MSG
2837 static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
2838 {
2839         struct wpa_supplicant *wpa_s = ctx;
2840         if (wpa_s == NULL)
2841                 return NULL;
2842         return wpa_s->ifname;
2843 }
2844 #endif /* CONFIG_NO_WPA_MSG */
2845
2846
2847 /**
2848  * wpa_supplicant_init - Initialize %wpa_supplicant
2849  * @params: Parameters for %wpa_supplicant
2850  * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2851  *
2852  * This function is used to initialize %wpa_supplicant. After successful
2853  * initialization, the returned data pointer can be used to add and remove
2854  * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2855  */
2856 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2857 {
2858         struct wpa_global *global;
2859         int ret, i;
2860
2861         if (params == NULL)
2862                 return NULL;
2863
2864 #ifdef CONFIG_DRIVER_NDIS
2865         {
2866                 void driver_ndis_init_ops(void);
2867                 driver_ndis_init_ops();
2868         }
2869 #endif /* CONFIG_DRIVER_NDIS */
2870
2871 #ifndef CONFIG_NO_WPA_MSG
2872         wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
2873 #endif /* CONFIG_NO_WPA_MSG */
2874
2875         wpa_debug_open_file(params->wpa_debug_file_path);
2876         if (params->wpa_debug_syslog)
2877                 wpa_debug_open_syslog();
2878
2879         ret = eap_register_methods();
2880         if (ret) {
2881                 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2882                 if (ret == -2)
2883                         wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2884                                    "the same EAP type.");
2885                 return NULL;
2886         }
2887
2888         global = os_zalloc(sizeof(*global));
2889         if (global == NULL)
2890                 return NULL;
2891         dl_list_init(&global->p2p_srv_bonjour);
2892         dl_list_init(&global->p2p_srv_upnp);
2893         global->params.daemonize = params->daemonize;
2894         global->params.wait_for_monitor = params->wait_for_monitor;
2895         global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2896         if (params->pid_file)
2897                 global->params.pid_file = os_strdup(params->pid_file);
2898         if (params->ctrl_interface)
2899                 global->params.ctrl_interface =
2900                         os_strdup(params->ctrl_interface);
2901         if (params->override_driver)
2902                 global->params.override_driver =
2903                         os_strdup(params->override_driver);
2904         if (params->override_ctrl_interface)
2905                 global->params.override_ctrl_interface =
2906                         os_strdup(params->override_ctrl_interface);
2907         wpa_debug_level = global->params.wpa_debug_level =
2908                 params->wpa_debug_level;
2909         wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2910                 params->wpa_debug_show_keys;
2911         wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2912                 params->wpa_debug_timestamp;
2913
2914         wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
2915
2916         if (eloop_init()) {
2917                 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2918                 wpa_supplicant_deinit(global);
2919                 return NULL;
2920         }
2921
2922         random_init(params->entropy_file);
2923
2924         global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2925         if (global->ctrl_iface == NULL) {
2926                 wpa_supplicant_deinit(global);
2927                 return NULL;
2928         }
2929
2930         if (wpas_notify_supplicant_initialized(global)) {
2931                 wpa_supplicant_deinit(global);
2932                 return NULL;
2933         }
2934
2935         for (i = 0; wpa_drivers[i]; i++)
2936                 global->drv_count++;
2937         if (global->drv_count == 0) {
2938                 wpa_printf(MSG_ERROR, "No drivers enabled");
2939                 wpa_supplicant_deinit(global);
2940                 return NULL;
2941         }
2942         global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
2943         if (global->drv_priv == NULL) {
2944                 wpa_supplicant_deinit(global);
2945                 return NULL;
2946         }
2947
2948         return global;
2949 }
2950
2951
2952 /**
2953  * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2954  * @global: Pointer to global data from wpa_supplicant_init()
2955  * Returns: 0 after successful event loop run, -1 on failure
2956  *
2957  * This function starts the main event loop and continues running as long as
2958  * there are any remaining events. In most cases, this function is running as
2959  * long as the %wpa_supplicant process in still in use.
2960  */
2961 int wpa_supplicant_run(struct wpa_global *global)
2962 {
2963         struct wpa_supplicant *wpa_s;
2964
2965         if (global->params.daemonize &&
2966             wpa_supplicant_daemon(global->params.pid_file))
2967                 return -1;
2968
2969         if (global->params.wait_for_monitor) {
2970                 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2971                         if (wpa_s->ctrl_iface)
2972                                 wpa_supplicant_ctrl_iface_wait(
2973                                         wpa_s->ctrl_iface);
2974         }
2975
2976         eloop_register_signal_terminate(wpa_supplicant_terminate, global);
2977         eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
2978
2979         eloop_run();
2980
2981         return 0;
2982 }
2983
2984
2985 /**
2986  * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2987  * @global: Pointer to global data from wpa_supplicant_init()
2988  *
2989  * This function is called to deinitialize %wpa_supplicant and to free all
2990  * allocated resources. Remaining network interfaces will also be removed.
2991  */
2992 void wpa_supplicant_deinit(struct wpa_global *global)
2993 {
2994         int i;
2995
2996         if (global == NULL)
2997                 return;
2998
2999 #ifdef CONFIG_P2P
3000         wpas_p2p_deinit_global(global);
3001 #endif /* CONFIG_P2P */
3002
3003         while (global->ifaces)
3004                 wpa_supplicant_remove_iface(global, global->ifaces, 1);
3005
3006         if (global->ctrl_iface)
3007                 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
3008
3009         wpas_notify_supplicant_deinitialized(global);
3010
3011         eap_peer_unregister_methods();
3012 #ifdef CONFIG_AP
3013         eap_server_unregister_methods();
3014 #endif /* CONFIG_AP */
3015
3016         for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
3017                 if (!global->drv_priv[i])
3018                         continue;
3019                 wpa_drivers[i]->global_deinit(global->drv_priv[i]);
3020         }
3021         os_free(global->drv_priv);
3022
3023         random_deinit();
3024
3025         eloop_destroy();
3026
3027         if (global->params.pid_file) {
3028                 os_daemonize_terminate(global->params.pid_file);
3029                 os_free(global->params.pid_file);
3030         }
3031         os_free(global->params.ctrl_interface);
3032         os_free(global->params.override_driver);
3033         os_free(global->params.override_ctrl_interface);
3034
3035         os_free(global->p2p_disallow_freq);
3036
3037         os_free(global);
3038         wpa_debug_close_syslog();
3039         wpa_debug_close_file();
3040 }
3041
3042
3043 void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
3044 {
3045         if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
3046             wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3047                 char country[3];
3048                 country[0] = wpa_s->conf->country[0];
3049                 country[1] = wpa_s->conf->country[1];
3050                 country[2] = '\0';
3051                 if (wpa_drv_set_country(wpa_s, country) < 0) {
3052                         wpa_printf(MSG_ERROR, "Failed to set country code "
3053                                    "'%s'", country);
3054                 }
3055         }
3056
3057 #ifdef CONFIG_WPS
3058         wpas_wps_update_config(wpa_s);
3059 #endif /* CONFIG_WPS */
3060
3061 #ifdef CONFIG_P2P
3062         wpas_p2p_update_config(wpa_s);
3063 #endif /* CONFIG_P2P */
3064
3065         wpa_s->conf->changed_parameters = 0;
3066 }
3067
3068
3069 static void add_freq(int *freqs, int *num_freqs, int freq)
3070 {
3071         int i;
3072
3073         for (i = 0; i < *num_freqs; i++) {
3074                 if (freqs[i] == freq)
3075                         return;
3076         }
3077
3078         freqs[*num_freqs] = freq;
3079         (*num_freqs)++;
3080 }
3081
3082
3083 static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
3084 {
3085         struct wpa_bss *bss, *cbss;
3086         const int max_freqs = 10;
3087         int *freqs;
3088         int num_freqs = 0;
3089
3090         freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
3091         if (freqs == NULL)
3092                 return NULL;
3093
3094         cbss = wpa_s->current_bss;
3095
3096         dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
3097                 if (bss == cbss)
3098                         continue;
3099                 if (bss->ssid_len == cbss->ssid_len &&
3100                     os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
3101                     wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
3102                         add_freq(freqs, &num_freqs, bss->freq);
3103                         if (num_freqs == max_freqs)
3104                                 break;
3105                 }
3106         }
3107
3108         if (num_freqs == 0) {
3109                 os_free(freqs);
3110                 freqs = NULL;
3111         }
3112
3113         return freqs;
3114 }
3115
3116
3117 void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
3118 {
3119         int timeout;
3120         int count;
3121         int *freqs = NULL;
3122
3123         /*
3124          * Remove possible authentication timeout since the connection failed.
3125          */
3126         eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
3127
3128         /*
3129          * Add the failed BSSID into the blacklist and speed up next scan
3130          * attempt if there could be other APs that could accept association.
3131          * The current blacklist count indicates how many times we have tried
3132          * connecting to this AP and multiple attempts mean that other APs are
3133          * either not available or has already been tried, so that we can start
3134          * increasing the delay here to avoid constant scanning.
3135          */
3136         count = wpa_blacklist_add(wpa_s, bssid);
3137         if (count == 1 && wpa_s->current_bss) {
3138                 /*
3139                  * This BSS was not in the blacklist before. If there is
3140                  * another BSS available for the same ESS, we should try that
3141                  * next. Otherwise, we may as well try this one once more
3142                  * before allowing other, likely worse, ESSes to be considered.
3143                  */
3144                 freqs = get_bss_freqs_in_ess(wpa_s);
3145                 if (freqs) {
3146                         wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
3147                                 "has been seen; try it next");
3148                         wpa_blacklist_add(wpa_s, bssid);
3149                         /*
3150                          * On the next scan, go through only the known channels
3151                          * used in this ESS based on previous scans to speed up
3152                          * common load balancing use case.
3153                          */
3154                         os_free(wpa_s->next_scan_freqs);
3155                         wpa_s->next_scan_freqs = freqs;
3156                 }
3157         }
3158
3159         switch (count) {
3160         case 1:
3161                 timeout = 100;
3162                 break;
3163         case 2:
3164                 timeout = 500;
3165                 break;
3166         case 3:
3167                 timeout = 1000;
3168                 break;
3169         default:
3170                 timeout = 5000;
3171         }
3172
3173         /*
3174          * TODO: if more than one possible AP is available in scan results,
3175          * could try the other ones before requesting a new scan.
3176          */
3177         wpa_supplicant_req_scan(wpa_s, timeout / 1000,
3178                                 1000 * (timeout % 1000));
3179 }
3180
3181
3182 int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
3183 {
3184         return wpa_s->conf->ap_scan == 2 ||
3185                 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
3186 }