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