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 }
538
539
wpa_supplicant_timeout(void * eloop_ctx,void * timeout_ctx)540 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
541 {
542 struct wpa_supplicant *wpa_s = eloop_ctx;
543 const u8 *bssid = wpa_s->bssid;
544 if (os_memcmp(bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0)
545 bssid = wpa_s->pending_bssid;
546 wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
547 MAC2STR(bssid));
548 wpa_blacklist_add(wpa_s, bssid);
549 wpa_sm_notify_disassoc(wpa_s->wpa);
550 wpa_supplicant_disassociate(wpa_s, REASON_DEAUTH_LEAVING);
551 wpa_s->reassociate = 1;
552 wpa_supplicant_req_scan(wpa_s, 0, 0);
553 }
554
555
556 /**
557 * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
558 * @wpa_s: Pointer to wpa_supplicant data
559 * @sec: Number of seconds after which to time out authentication
560 * @usec: Number of microseconds after which to time out authentication
561 *
562 * This function is used to schedule a timeout for the current authentication
563 * attempt.
564 */
wpa_supplicant_req_auth_timeout(struct wpa_supplicant * wpa_s,int sec,int usec)565 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
566 int sec, int usec)
567 {
568 if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
569 wpa_s->driver && os_strcmp(wpa_s->driver->name, "wired") == 0)
570 return;
571
572 wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
573 "%d usec", sec, usec);
574 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
575 eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
576 }
577
578
579 /**
580 * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
581 * @wpa_s: Pointer to wpa_supplicant data
582 *
583 * This function is used to cancel authentication timeout scheduled with
584 * wpa_supplicant_req_auth_timeout() and it is called when authentication has
585 * been completed.
586 */
wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant * wpa_s)587 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
588 {
589 wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
590 eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
591 wpa_blacklist_del(wpa_s, wpa_s->bssid);
592 }
593
594
595 /**
596 * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
597 * @wpa_s: Pointer to wpa_supplicant data
598 *
599 * This function is used to configure EAPOL state machine based on the selected
600 * authentication mode.
601 */
wpa_supplicant_initiate_eapol(struct wpa_supplicant * wpa_s)602 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
603 {
604 #ifdef IEEE8021X_EAPOL
605 struct eapol_config eapol_conf;
606 struct wpa_ssid *ssid = wpa_s->current_ssid;
607
608 if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK) {
609 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
610 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
611 }
612 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
613 wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
614 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
615 else
616 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
617
618 os_memset(&eapol_conf, 0, sizeof(eapol_conf));
619 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
620 eapol_conf.accept_802_1x_keys = 1;
621 eapol_conf.required_keys = 0;
622 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
623 eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
624 }
625 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
626 eapol_conf.required_keys |=
627 EAPOL_REQUIRE_KEY_BROADCAST;
628 }
629
630 if (wpa_s->conf && wpa_s->driver &&
631 os_strcmp(wpa_s->driver->name, "wired") == 0) {
632 eapol_conf.required_keys = 0;
633 }
634 }
635 if (wpa_s->conf)
636 eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
637 eapol_conf.workaround = ssid->eap_workaround;
638 eapol_conf.eap_disabled = wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X &&
639 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA;
640 eapol_sm_notify_config(wpa_s->eapol, ssid, &eapol_conf);
641 #endif /* IEEE8021X_EAPOL */
642 }
643
644
645 /**
646 * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
647 * @wpa_s: Pointer to wpa_supplicant data
648 * @ssid: Configuration data for the network
649 *
650 * This function is used to configure WPA state machine and related parameters
651 * to a mode where WPA is not enabled. This is called as part of the
652 * authentication configuration when the selected network does not use WPA.
653 */
wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)654 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
655 struct wpa_ssid *ssid)
656 {
657 int i;
658
659 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
660 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
661 else
662 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
663 wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
664 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
665 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
666 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
667 wpa_s->group_cipher = WPA_CIPHER_NONE;
668 wpa_s->mgmt_group_cipher = 0;
669
670 for (i = 0; i < NUM_WEP_KEYS; i++) {
671 if (ssid->wep_key_len[i] > 5) {
672 wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
673 wpa_s->group_cipher = WPA_CIPHER_WEP104;
674 break;
675 } else if (ssid->wep_key_len[i] > 0) {
676 wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
677 wpa_s->group_cipher = WPA_CIPHER_WEP40;
678 break;
679 }
680 }
681
682 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
683 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
684 wpa_s->pairwise_cipher);
685 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
686 #ifdef CONFIG_IEEE80211W
687 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
688 wpa_s->mgmt_group_cipher);
689 #endif /* CONFIG_IEEE80211W */
690
691 pmksa_cache_clear_current(wpa_s->wpa);
692 }
693
694
wpa_supplicant_cleanup(struct wpa_supplicant * wpa_s)695 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
696 {
697 scard_deinit(wpa_s->scard);
698 wpa_s->scard = NULL;
699 wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
700 eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
701 l2_packet_deinit(wpa_s->l2);
702 wpa_s->l2 = NULL;
703 if (wpa_s->l2_br) {
704 l2_packet_deinit(wpa_s->l2_br);
705 wpa_s->l2_br = NULL;
706 }
707
708 if (wpa_s->ctrl_iface) {
709 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
710 wpa_s->ctrl_iface = NULL;
711 }
712 if (wpa_s->conf != NULL) {
713 wpa_config_free(wpa_s->conf);
714 wpa_s->conf = NULL;
715 }
716
717 os_free(wpa_s->confname);
718 wpa_s->confname = NULL;
719
720 wpa_sm_set_eapol(wpa_s->wpa, NULL);
721 eapol_sm_deinit(wpa_s->eapol);
722 wpa_s->eapol = NULL;
723
724 rsn_preauth_deinit(wpa_s->wpa);
725
726 pmksa_candidate_free(wpa_s->wpa);
727 wpa_sm_deinit(wpa_s->wpa);
728 wpa_s->wpa = NULL;
729 wpa_blacklist_clear(wpa_s);
730
731 os_free(wpa_s->scan_results);
732 wpa_s->scan_results = NULL;
733 wpa_s->num_scan_results = 0;
734
735 wpa_supplicant_cancel_scan(wpa_s);
736 wpa_supplicant_cancel_auth_timeout(wpa_s);
737
738 ieee80211_sta_deinit(wpa_s);
739 }
740
741
742 /**
743 * wpa_clear_keys - Clear keys configured for the driver
744 * @wpa_s: Pointer to wpa_supplicant data
745 * @addr: Previously used BSSID or %NULL if not available
746 *
747 * This function clears the encryption keys that has been previously configured
748 * for the driver.
749 */
wpa_clear_keys(struct wpa_supplicant * wpa_s,const u8 * addr)750 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
751 {
752 u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
753
754 if (wpa_s->keys_cleared) {
755 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
756 * timing issues with keys being cleared just before new keys
757 * are set or just after association or something similar. This
758 * shows up in group key handshake failing often because of the
759 * client not receiving the first encrypted packets correctly.
760 * Skipping some of the extra key clearing steps seems to help
761 * in completing group key handshake more reliably. */
762 wpa_printf(MSG_DEBUG, "No keys have been configured - "
763 "skip key clearing");
764 return;
765 }
766
767 /* MLME-DELETEKEYS.request */
768 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
769 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
770 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
771 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
772 if (addr) {
773 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
774 0);
775 /* MLME-SETPROTECTION.request(None) */
776 wpa_drv_mlme_setprotection(
777 wpa_s, addr,
778 MLME_SETPROTECTION_PROTECT_TYPE_NONE,
779 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
780 }
781 wpa_s->keys_cleared = 1;
782 }
783
784
785 /**
786 * wpa_supplicant_state_txt - Get the connection state name as a text string
787 * @state: State (wpa_state; WPA_*)
788 * Returns: The state name as a printable text string
789 */
wpa_supplicant_state_txt(int state)790 const char * wpa_supplicant_state_txt(int state)
791 {
792 switch (state) {
793 case WPA_DISCONNECTED:
794 return "DISCONNECTED";
795 case WPA_INACTIVE:
796 return "INACTIVE";
797 case WPA_SCANNING:
798 return "SCANNING";
799 case WPA_ASSOCIATING:
800 return "ASSOCIATING";
801 case WPA_ASSOCIATED:
802 return "ASSOCIATED";
803 case WPA_4WAY_HANDSHAKE:
804 return "4WAY_HANDSHAKE";
805 case WPA_GROUP_HANDSHAKE:
806 return "GROUP_HANDSHAKE";
807 case WPA_COMPLETED:
808 return "COMPLETED";
809 default:
810 return "UNKNOWN";
811 }
812 }
813
814
815 /**
816 * wpa_supplicant_set_state - Set current connection state
817 * @wpa_s: Pointer to wpa_supplicant data
818 * @state: The new connection state
819 *
820 * This function is called whenever the connection state changes, e.g.,
821 * association is completed for WPA/WPA2 4-Way Handshake is started.
822 */
wpa_supplicant_set_state(struct wpa_supplicant * wpa_s,wpa_states state)823 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
824 {
825 #ifdef ANDROID
826 int network_id = -1;
827 if (wpa_s && wpa_s->current_ssid) {
828 network_id = wpa_s->current_ssid->id;
829 }
830 wpa_states reported_state = state;
831 if (state == WPA_DISCONNECTED && wpa_s->disconnected) {
832 reported_state = WPA_IDLE;
833 }
834 #endif
835 wpa_printf(MSG_DEBUG, "State: %s -> %s",
836 wpa_supplicant_state_txt(wpa_s->wpa_state),
837 wpa_supplicant_state_txt(state));
838
839 wpa_supplicant_dbus_notify_state_change(wpa_s, state,
840 wpa_s->wpa_state);
841 #ifdef ANDROID
842 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE "id=%d state=%d",
843 network_id, reported_state); /* Dm: */
844 #endif
845 if (state == WPA_COMPLETED && wpa_s->new_connection) {
846 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
847 struct wpa_ssid *ssid = wpa_s->current_ssid;
848 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
849 MACSTR " completed %s [id=%d id_str=%s]",
850 MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
851 "(reauth)" : "(auth)",
852 ssid ? ssid->id : -1,
853 ssid && ssid->id_str ? ssid->id_str : "");
854 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
855 wpa_s->new_connection = 0;
856 wpa_s->reassociated_connection = 1;
857 wpa_drv_set_operstate(wpa_s, 1);
858 } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
859 state == WPA_ASSOCIATED) {
860 wpa_s->new_connection = 1;
861 wpa_drv_set_operstate(wpa_s, 0);
862 }
863 wpa_s->wpa_state = state;
864 }
865
866
867 /**
868 * wpa_supplicant_get_state - Get the connection state
869 * @wpa_s: Pointer to wpa_supplicant data
870 * Returns: The current connection state (WPA_*)
871 */
wpa_supplicant_get_state(struct wpa_supplicant * wpa_s)872 wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
873 {
874 return wpa_s->wpa_state;
875 }
876
877
wpa_supplicant_terminate(int sig,void * eloop_ctx,void * signal_ctx)878 static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
879 void *signal_ctx)
880 {
881 struct wpa_global *global = eloop_ctx;
882 struct wpa_supplicant *wpa_s;
883 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
884 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
885 "received", sig);
886 }
887 eloop_terminate();
888 }
889
890
wpa_supplicant_clear_status(struct wpa_supplicant * wpa_s)891 static void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
892 {
893 wpa_s->pairwise_cipher = 0;
894 wpa_s->group_cipher = 0;
895 wpa_s->mgmt_group_cipher = 0;
896 wpa_s->key_mgmt = 0;
897 wpa_s->wpa_state = WPA_DISCONNECTED;
898 }
899
900
901 /**
902 * wpa_supplicant_reload_configuration - Reload configuration data
903 * @wpa_s: Pointer to wpa_supplicant data
904 * Returns: 0 on success or -1 if configuration parsing failed
905 *
906 * This function can be used to request that the configuration data is reloaded
907 * (e.g., after configuration file change). This function is reloading
908 * configuration only for one interface, so this may need to be called multiple
909 * times if %wpa_supplicant is controlling multiple interfaces and all
910 * interfaces need reconfiguration.
911 */
wpa_supplicant_reload_configuration(struct wpa_supplicant * wpa_s)912 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
913 {
914 struct wpa_config *conf;
915 int reconf_ctrl;
916 if (wpa_s->confname == NULL)
917 return -1;
918 conf = wpa_config_read(wpa_s->confname);
919 if (conf == NULL) {
920 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
921 "file '%s' - exiting", wpa_s->confname);
922 return -1;
923 }
924
925 reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
926 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
927 os_strcmp(conf->ctrl_interface,
928 wpa_s->conf->ctrl_interface) != 0);
929
930 if (reconf_ctrl && wpa_s->ctrl_iface) {
931 wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
932 wpa_s->ctrl_iface = NULL;
933 }
934
935 eapol_sm_invalidate_cached_session(wpa_s->eapol);
936 wpa_s->current_ssid = NULL;
937 /*
938 * TODO: should notify EAPOL SM about changes in opensc_engine_path,
939 * pkcs11_engine_path, pkcs11_module_path.
940 */
941 if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK) {
942 /*
943 * Clear forced success to clear EAP state for next
944 * authentication.
945 */
946 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
947 }
948 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
949 wpa_sm_set_config(wpa_s->wpa, NULL);
950 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
951 rsn_preauth_deinit(wpa_s->wpa);
952 wpa_config_free(wpa_s->conf);
953 wpa_s->conf = conf;
954 if (reconf_ctrl)
955 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
956
957 wpa_supplicant_clear_status(wpa_s);
958 wpa_s->reassociate = 1;
959 #ifdef ANDROID
960 wpa_supplicant_req_scan(wpa_s, 2, 0);
961 #else
962 wpa_supplicant_req_scan(wpa_s, 0, 0);
963 #endif
964 wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
965 return 0;
966 }
967
968
wpa_supplicant_reconfig(int sig,void * eloop_ctx,void * signal_ctx)969 static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
970 void *signal_ctx)
971 {
972 struct wpa_global *global = eloop_ctx;
973 struct wpa_supplicant *wpa_s;
974 wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
975 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
976 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
977 eloop_terminate();
978 }
979 }
980 }
981
982
wpa_supplicant_gen_assoc_event(struct wpa_supplicant * wpa_s)983 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
984 {
985 struct wpa_ssid *ssid;
986 union wpa_event_data data;
987
988 ssid = wpa_supplicant_get_ssid(wpa_s);
989 if (ssid == NULL)
990 return;
991
992 if (wpa_s->current_ssid == NULL)
993 wpa_s->current_ssid = ssid;
994 wpa_supplicant_initiate_eapol(wpa_s);
995 wpa_printf(MSG_DEBUG, "Already associated with a configured network - "
996 "generating associated event");
997 os_memset(&data, 0, sizeof(data));
998 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
999 }
1000
1001
wpa_supplicant_scan(void * eloop_ctx,void * timeout_ctx)1002 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
1003 {
1004 struct wpa_supplicant *wpa_s = eloop_ctx;
1005 struct wpa_ssid *ssid;
1006 int enabled, scan_req = 0, ret;
1007
1008 if (wpa_s->disconnected && !wpa_s->scan_req)
1009 return;
1010
1011 enabled = 0;
1012 ssid = wpa_s->conf->ssid;
1013 while (ssid) {
1014 if (!ssid->disabled) {
1015 enabled++;
1016 break;
1017 }
1018 ssid = ssid->next;
1019 }
1020 if (!enabled && !wpa_s->scan_req) {
1021 wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
1022 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
1023 return;
1024 }
1025 scan_req = wpa_s->scan_req;
1026 wpa_s->scan_req = 0;
1027
1028 if (wpa_s->conf->ap_scan != 0 &&
1029 wpa_s->driver && os_strcmp(wpa_s->driver->name, "wired") == 0) {
1030 wpa_printf(MSG_DEBUG, "Using wired driver - overriding "
1031 "ap_scan configuration");
1032 wpa_s->conf->ap_scan = 0;
1033 }
1034
1035 if (wpa_s->conf->ap_scan == 0) {
1036 wpa_supplicant_gen_assoc_event(wpa_s);
1037 return;
1038 }
1039
1040 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1041 wpa_s->wpa_state == WPA_INACTIVE)
1042 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1043
1044 ssid = wpa_s->conf->ssid;
1045 if (wpa_s->prev_scan_ssid != BROADCAST_SSID_SCAN) {
1046 while (ssid) {
1047 if (ssid == wpa_s->prev_scan_ssid) {
1048 ssid = ssid->next;
1049 break;
1050 }
1051 ssid = ssid->next;
1052 }
1053 }
1054 while (ssid) {
1055 if (!ssid->disabled &&
1056 (ssid->scan_ssid || wpa_s->conf->ap_scan == 2))
1057 break;
1058 ssid = ssid->next;
1059 }
1060
1061 if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
1062 /*
1063 * ap_scan=2 mode - try to associate with each SSID instead of
1064 * scanning for each scan_ssid=1 network.
1065 */
1066 if (ssid == NULL) {
1067 wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
1068 "end of scan list - go back to beginning");
1069 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
1070 wpa_supplicant_req_scan(wpa_s, 0, 0);
1071 return;
1072 }
1073 if (ssid->next) {
1074 /* Continue from the next SSID on the next attempt. */
1075 wpa_s->prev_scan_ssid = ssid;
1076 } else {
1077 /* Start from the beginning of the SSID list. */
1078 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
1079 }
1080 wpa_supplicant_associate(wpa_s, NULL, ssid);
1081 return;
1082 }
1083
1084 wpa_printf(MSG_DEBUG, "Starting AP scan (%s SSID)",
1085 ssid ? "specific": "broadcast");
1086 if (ssid) {
1087 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
1088 ssid->ssid, ssid->ssid_len);
1089 wpa_s->prev_scan_ssid = ssid;
1090 } else
1091 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
1092
1093 if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1) {
1094 wpa_s->scan_res_tried++;
1095 wpa_s->scan_req = scan_req;
1096 wpa_printf(MSG_DEBUG, "Trying to get current scan results "
1097 "first without requesting a new scan to speed up "
1098 "initial association");
1099 wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
1100 return;
1101 }
1102
1103 if (wpa_s->use_client_mlme) {
1104 ret = ieee80211_sta_req_scan(wpa_s, ssid ? ssid->ssid : NULL,
1105 ssid ? ssid->ssid_len : 0);
1106 } else {
1107 ret = wpa_drv_scan(wpa_s, ssid ? ssid->ssid : NULL,
1108 ssid ? ssid->ssid_len : 0);
1109 }
1110 if (ret) {
1111 wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
1112 wpa_supplicant_req_scan(wpa_s, 10, 0);
1113 }
1114 else
1115 wpa_s->scan_ongoing = 1;
1116 }
1117
1118
cipher_suite2driver(int cipher)1119 static wpa_cipher cipher_suite2driver(int cipher)
1120 {
1121 switch (cipher) {
1122 case WPA_CIPHER_NONE:
1123 return CIPHER_NONE;
1124 case WPA_CIPHER_WEP40:
1125 return CIPHER_WEP40;
1126 case WPA_CIPHER_WEP104:
1127 return CIPHER_WEP104;
1128 case WPA_CIPHER_CCMP:
1129 return CIPHER_CCMP;
1130 case WPA_CIPHER_TKIP:
1131 default:
1132 return CIPHER_TKIP;
1133 }
1134 }
1135
1136
key_mgmt2driver(int key_mgmt)1137 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
1138 {
1139 switch (key_mgmt) {
1140 case WPA_KEY_MGMT_NONE:
1141 return KEY_MGMT_NONE;
1142 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
1143 return KEY_MGMT_802_1X_NO_WPA;
1144 case WPA_KEY_MGMT_IEEE8021X:
1145 return KEY_MGMT_802_1X;
1146 case WPA_KEY_MGMT_WPA_NONE:
1147 return KEY_MGMT_WPA_NONE;
1148 case WPA_KEY_MGMT_PSK:
1149 default:
1150 return KEY_MGMT_PSK;
1151 }
1152 }
1153
1154
wpa_supplicant_suites_from_ai(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,struct wpa_ie_data * ie)1155 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
1156 struct wpa_ssid *ssid,
1157 struct wpa_ie_data *ie)
1158 {
1159 int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
1160 if (ret) {
1161 if (ret == -2) {
1162 wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
1163 "from association info");
1164 }
1165 return -1;
1166 }
1167
1168 wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
1169 "suites");
1170 if (!(ie->group_cipher & ssid->group_cipher)) {
1171 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
1172 "cipher 0x%x (mask 0x%x) - reject",
1173 ie->group_cipher, ssid->group_cipher);
1174 return -1;
1175 }
1176 if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
1177 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
1178 "cipher 0x%x (mask 0x%x) - reject",
1179 ie->pairwise_cipher, ssid->pairwise_cipher);
1180 return -1;
1181 }
1182 if (!(ie->key_mgmt & ssid->key_mgmt)) {
1183 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
1184 "management 0x%x (mask 0x%x) - reject",
1185 ie->key_mgmt, ssid->key_mgmt);
1186 return -1;
1187 }
1188
1189 #ifdef CONFIG_IEEE80211W
1190 if (!(ie->capabilities & WPA_CAPABILITY_MGMT_FRAME_PROTECTION) &&
1191 ssid->ieee80211w == IEEE80211W_REQUIRED) {
1192 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
1193 "that does not support management frame protection - "
1194 "reject");
1195 return -1;
1196 }
1197 #endif /* CONFIG_IEEE80211W */
1198
1199 return 0;
1200 }
1201
1202
1203 /**
1204 * wpa_supplicant_set_suites - Set authentication and encryption parameters
1205 * @wpa_s: Pointer to wpa_supplicant data
1206 * @bss: Scan results for the selected BSS, or %NULL if not available
1207 * @ssid: Configuration data for the selected network
1208 * @wpa_ie: Buffer for the WPA/RSN IE
1209 * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
1210 * used buffer length in case the functions returns success.
1211 * Returns: 0 on success or -1 on failure
1212 *
1213 * This function is used to configure authentication and encryption parameters
1214 * based on the network configuration and scan result for the selected BSS (if
1215 * available).
1216 */
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)1217 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
1218 struct wpa_scan_result *bss,
1219 struct wpa_ssid *ssid,
1220 u8 *wpa_ie, size_t *wpa_ie_len)
1221 {
1222 struct wpa_ie_data ie;
1223 int sel, proto;
1224
1225 if (bss && bss->rsn_ie_len && (ssid->proto & WPA_PROTO_RSN) &&
1226 wpa_parse_wpa_ie(bss->rsn_ie, bss->rsn_ie_len, &ie) == 0 &&
1227 (ie.group_cipher & ssid->group_cipher) &&
1228 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1229 (ie.key_mgmt & ssid->key_mgmt)) {
1230 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
1231 proto = WPA_PROTO_RSN;
1232 } else if (bss && bss->wpa_ie_len && (ssid->proto & WPA_PROTO_WPA) &&
1233 wpa_parse_wpa_ie(bss->wpa_ie, bss->wpa_ie_len, &ie) == 0 &&
1234 (ie.group_cipher & ssid->group_cipher) &&
1235 (ie.pairwise_cipher & ssid->pairwise_cipher) &&
1236 (ie.key_mgmt & ssid->key_mgmt)) {
1237 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
1238 proto = WPA_PROTO_WPA;
1239 } else if (bss) {
1240 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
1241 return -1;
1242 } else {
1243 if (ssid->proto & WPA_PROTO_RSN)
1244 proto = WPA_PROTO_RSN;
1245 else
1246 proto = WPA_PROTO_WPA;
1247 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
1248 os_memset(&ie, 0, sizeof(ie));
1249 ie.group_cipher = ssid->group_cipher;
1250 ie.pairwise_cipher = ssid->pairwise_cipher;
1251 ie.key_mgmt = ssid->key_mgmt;
1252 #ifdef CONFIG_IEEE80211W
1253 ie.mgmt_group_cipher =
1254 ssid->ieee80211w != NO_IEEE80211W ?
1255 WPA_CIPHER_AES_128_CMAC : 0;
1256 #endif /* CONFIG_IEEE80211W */
1257 wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
1258 "on configuration");
1259 } else
1260 proto = ie.proto;
1261 }
1262
1263 wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
1264 "pairwise %d key_mgmt %d proto %d",
1265 ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
1266 #ifdef CONFIG_IEEE80211W
1267 if (ssid->ieee80211w) {
1268 wpa_printf(MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
1269 ie.mgmt_group_cipher);
1270 }
1271 #endif /* CONFIG_IEEE80211W */
1272
1273 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1274
1275 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss ? bss->wpa_ie : NULL,
1276 bss ? bss->wpa_ie_len : 0) ||
1277 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss ? bss->rsn_ie : NULL,
1278 bss ? bss->rsn_ie_len : 0))
1279 return -1;
1280
1281 sel = ie.group_cipher & ssid->group_cipher;
1282 if (sel & WPA_CIPHER_CCMP) {
1283 wpa_s->group_cipher = WPA_CIPHER_CCMP;
1284 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
1285 } else if (sel & WPA_CIPHER_TKIP) {
1286 wpa_s->group_cipher = WPA_CIPHER_TKIP;
1287 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
1288 } else if (sel & WPA_CIPHER_WEP104) {
1289 wpa_s->group_cipher = WPA_CIPHER_WEP104;
1290 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
1291 } else if (sel & WPA_CIPHER_WEP40) {
1292 wpa_s->group_cipher = WPA_CIPHER_WEP40;
1293 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
1294 } else {
1295 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
1296 return -1;
1297 }
1298
1299 sel = ie.pairwise_cipher & ssid->pairwise_cipher;
1300 if (sel & WPA_CIPHER_CCMP) {
1301 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
1302 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
1303 } else if (sel & WPA_CIPHER_TKIP) {
1304 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
1305 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
1306 } else if (sel & WPA_CIPHER_NONE) {
1307 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
1308 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
1309 } else {
1310 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
1311 "cipher.");
1312 return -1;
1313 }
1314
1315 sel = ie.key_mgmt & ssid->key_mgmt;
1316 if (sel & WPA_KEY_MGMT_IEEE8021X) {
1317 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1318 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1319 } else if (sel & WPA_KEY_MGMT_PSK) {
1320 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1321 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1322 } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1323 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1324 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
1325 } else {
1326 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
1327 "key management type.");
1328 return -1;
1329 }
1330
1331 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1332 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1333 wpa_s->pairwise_cipher);
1334 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1335
1336 #ifdef CONFIG_IEEE80211W
1337 sel = ie.mgmt_group_cipher;
1338 if (ssid->ieee80211w == NO_IEEE80211W ||
1339 !(ie.capabilities & WPA_CAPABILITY_MGMT_FRAME_PROTECTION))
1340 sel = 0;
1341 if (sel & WPA_CIPHER_AES_128_CMAC) {
1342 wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
1343 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
1344 "AES-128-CMAC");
1345 } else {
1346 wpa_s->mgmt_group_cipher = 0;
1347 wpa_msg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
1348 }
1349 wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
1350 wpa_s->mgmt_group_cipher);
1351 #endif /* CONFIG_IEEE80211W */
1352
1353 if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1354 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
1355 return -1;
1356 }
1357
1358 if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
1359 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
1360 else
1361 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1362
1363 return 0;
1364 }
1365
1366
1367 /**
1368 * wpa_supplicant_associate - Request association
1369 * @wpa_s: Pointer to wpa_supplicant data
1370 * @bss: Scan results for the selected BSS, or %NULL if not available
1371 * @ssid: Configuration data for the selected network
1372 *
1373 * This function is used to request %wpa_supplicant to associate with a BSS.
1374 */
wpa_supplicant_associate(struct wpa_supplicant * wpa_s,struct wpa_scan_result * bss,struct wpa_ssid * ssid)1375 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1376 struct wpa_scan_result *bss,
1377 struct wpa_ssid *ssid)
1378 {
1379 u8 wpa_ie[80];
1380 size_t wpa_ie_len;
1381 int use_crypt, ret, i;
1382 int algs = AUTH_ALG_OPEN_SYSTEM;
1383 wpa_cipher cipher_pairwise, cipher_group;
1384 struct wpa_driver_associate_params params;
1385 int wep_keys_set = 0;
1386 struct wpa_driver_capa capa;
1387 int assoc_failed = 0;
1388
1389 wpa_s->reassociate = 0;
1390 if (bss) {
1391 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1392 " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1393 wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1394 os_memset(wpa_s->bssid, 0, ETH_ALEN);
1395 os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
1396 wpa_s->link_speed = bss->maxrate;
1397 wpa_s->rssi = bss->level;
1398 } else {
1399 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1400 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1401 os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
1402 }
1403 wpa_supplicant_cancel_scan(wpa_s);
1404
1405 /* Starting new association, so clear the possibly used WPA IE from the
1406 * previous association. */
1407 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1408
1409 #ifdef IEEE8021X_EAPOL
1410 if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1411 if (ssid->leap) {
1412 if (ssid->non_leap == 0)
1413 algs = AUTH_ALG_LEAP;
1414 else
1415 algs |= AUTH_ALG_LEAP;
1416 }
1417 }
1418 #endif /* IEEE8021X_EAPOL */
1419 wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1420 if (ssid->auth_alg) {
1421 algs = 0;
1422 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
1423 algs |= AUTH_ALG_OPEN_SYSTEM;
1424 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
1425 algs |= AUTH_ALG_SHARED_KEY;
1426 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
1427 algs |= AUTH_ALG_LEAP;
1428 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
1429 algs);
1430 }
1431 wpa_drv_set_auth_alg(wpa_s, algs);
1432
1433 if (bss && (bss->wpa_ie_len || bss->rsn_ie_len) &&
1434 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK))) {
1435 int try_opportunistic;
1436 try_opportunistic = ssid->proactive_key_caching &&
1437 (ssid->proto & WPA_PROTO_RSN);
1438 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1439 wpa_s->current_ssid,
1440 try_opportunistic) == 0)
1441 eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1442 wpa_ie_len = sizeof(wpa_ie);
1443 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1444 wpa_ie, &wpa_ie_len)) {
1445 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1446 "management and encryption suites");
1447 return;
1448 }
1449 } else if (ssid->key_mgmt &
1450 (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1451 WPA_KEY_MGMT_WPA_NONE)) {
1452 wpa_ie_len = sizeof(wpa_ie);
1453 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1454 wpa_ie, &wpa_ie_len)) {
1455 wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1456 "management and encryption suites (no scan "
1457 "results)");
1458 return;
1459 }
1460 } else {
1461 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1462 wpa_ie_len = 0;
1463 }
1464
1465 wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1466 use_crypt = 1;
1467 cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1468 cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1469 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1470 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1471 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1472 use_crypt = 0;
1473 for (i = 0; i < NUM_WEP_KEYS; i++) {
1474 if (ssid->wep_key_len[i]) {
1475 use_crypt = 1;
1476 wep_keys_set = 1;
1477 wpa_set_wep_key(wpa_s,
1478 i == ssid->wep_tx_keyidx,
1479 i, ssid->wep_key[i],
1480 ssid->wep_key_len[i]);
1481 }
1482 }
1483 }
1484
1485 #ifdef IEEE8021X_EAPOL
1486 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1487 if ((ssid->eapol_flags &
1488 (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1489 EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1490 !wep_keys_set) {
1491 use_crypt = 0;
1492 } else {
1493 /* Assume that dynamic WEP-104 keys will be used and
1494 * set cipher suites in order for drivers to expect
1495 * encryption. */
1496 cipher_pairwise = cipher_group = CIPHER_WEP104;
1497 }
1498 }
1499 #endif /* IEEE8021X_EAPOL */
1500
1501 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1502 /* Set the key before (and later after) association */
1503 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1504 }
1505
1506 wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);
1507 wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1508 os_memset(¶ms, 0, sizeof(params));
1509 if (bss) {
1510 params.bssid = bss->bssid;
1511 params.ssid = bss->ssid;
1512 params.ssid_len = bss->ssid_len;
1513 params.freq = bss->freq;
1514 } else {
1515 params.ssid = ssid->ssid;
1516 params.ssid_len = ssid->ssid_len;
1517 }
1518 if (ssid->mode == 1 && ssid->frequency > 0 && params.freq == 0)
1519 params.freq = ssid->frequency; /* Initial channel for IBSS */
1520 params.wpa_ie = wpa_ie;
1521 params.wpa_ie_len = wpa_ie_len;
1522 params.pairwise_suite = cipher_pairwise;
1523 params.group_suite = cipher_group;
1524 params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1525 params.auth_alg = algs;
1526 params.mode = ssid->mode;
1527 for (i = 0; i < NUM_WEP_KEYS; i++) {
1528 if (ssid->wep_key_len[i])
1529 params.wep_key[i] = ssid->wep_key[i];
1530 params.wep_key_len[i] = ssid->wep_key_len[i];
1531 }
1532 params.wep_tx_keyidx = ssid->wep_tx_keyidx;
1533
1534 #ifdef CONFIG_IEEE80211W
1535 switch (ssid->ieee80211w) {
1536 case NO_IEEE80211W:
1537 params.mgmt_frame_protection = NO_MGMT_FRAME_PROTECTION;
1538 break;
1539 case IEEE80211W_OPTIONAL:
1540 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_OPTIONAL;
1541 break;
1542 case IEEE80211W_REQUIRED:
1543 params.mgmt_frame_protection = MGMT_FRAME_PROTECTION_REQUIRED;
1544 break;
1545 }
1546 #endif /* CONFIG_IEEE80211W */
1547
1548 if (wpa_s->use_client_mlme)
1549 ret = ieee80211_sta_associate(wpa_s, ¶ms);
1550 else
1551 ret = wpa_drv_associate(wpa_s, ¶ms);
1552 if (ret < 0) {
1553 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1554 "failed");
1555 /* try to continue anyway; new association will be tried again
1556 * after timeout */
1557 assoc_failed = 1;
1558 }
1559
1560 if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1561 /* Set the key after the association just in case association
1562 * cleared the previously configured key. */
1563 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1564 /* No need to timeout authentication since there is no key
1565 * management. */
1566 wpa_supplicant_cancel_auth_timeout(wpa_s);
1567 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1568 } else {
1569 /* Timeout for IEEE 802.11 authentication and association */
1570 int timeout = 60;
1571
1572 if (assoc_failed) {
1573 /* give IBSS a bit more time */
1574 timeout = ssid->mode ? 10 : 5;
1575 } else if (wpa_s->conf->ap_scan == 1) {
1576 /* give IBSS a bit more time */
1577 timeout = ssid->mode ? 20 : 10;
1578 }
1579 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1580 }
1581
1582 if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1583 capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1584 /* Set static WEP keys again */
1585 int j;
1586 for (j = 0; j < NUM_WEP_KEYS; j++) {
1587 if (ssid->wep_key_len[j]) {
1588 wpa_set_wep_key(wpa_s,
1589 j == ssid->wep_tx_keyidx,
1590 j, ssid->wep_key[j],
1591 ssid->wep_key_len[j]);
1592 }
1593 }
1594 }
1595
1596 if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
1597 /*
1598 * Do not allow EAP session resumption between different
1599 * network configurations.
1600 */
1601 eapol_sm_invalidate_cached_session(wpa_s->eapol);
1602 }
1603 wpa_s->current_ssid = ssid;
1604 wpa_sm_set_config(wpa_s->wpa, wpa_s->current_ssid);
1605 wpa_supplicant_initiate_eapol(wpa_s);
1606 }
1607
1608
1609 /**
1610 * wpa_supplicant_disassociate - Disassociate the current connection
1611 * @wpa_s: Pointer to wpa_supplicant data
1612 * @reason_code: IEEE 802.11 reason code for the disassociate frame
1613 *
1614 * This function is used to request %wpa_supplicant to disassociate with the
1615 * current AP.
1616 */
wpa_supplicant_disassociate(struct wpa_supplicant * wpa_s,int reason_code)1617 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1618 int reason_code)
1619 {
1620 u8 *addr = NULL;
1621 if (os_memcmp(wpa_s->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) != 0)
1622 {
1623 if (wpa_s->use_client_mlme)
1624 ieee80211_sta_disassociate(wpa_s, reason_code);
1625 else
1626 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1627 addr = wpa_s->bssid;
1628 }
1629 wpa_clear_keys(wpa_s, addr);
1630 wpa_supplicant_mark_disassoc(wpa_s);
1631 wpa_s->current_ssid = NULL;
1632 wpa_sm_set_config(wpa_s->wpa, NULL);
1633 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1634 }
1635
1636
1637 /**
1638 * wpa_supplicant_deauthenticate - Deauthenticate the current connection
1639 * @wpa_s: Pointer to wpa_supplicant data
1640 * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
1641 *
1642 * This function is used to request %wpa_supplicant to disassociate with the
1643 * current AP.
1644 */
wpa_supplicant_deauthenticate(struct wpa_supplicant * wpa_s,int reason_code)1645 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1646 int reason_code)
1647 {
1648 u8 *addr = NULL;
1649 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1650 if (os_memcmp(wpa_s->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) != 0)
1651 {
1652 if (wpa_s->use_client_mlme)
1653 ieee80211_sta_deauthenticate(wpa_s, reason_code);
1654 else
1655 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
1656 reason_code);
1657 addr = wpa_s->bssid;
1658 }
1659 wpa_clear_keys(wpa_s, addr);
1660 wpa_s->current_ssid = NULL;
1661 wpa_sm_set_config(wpa_s->wpa, NULL);
1662 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1663 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1664 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1665 }
1666
1667
1668 /**
1669 * wpa_supplicant_get_scan_results - Get scan results
1670 * @wpa_s: Pointer to wpa_supplicant data
1671 * Returns: 0 on success, -1 on failure
1672 *
1673 * This function is request the current scan results from the driver and stores
1674 * a local copy of the results in wpa_s->scan_results.
1675 */
wpa_supplicant_get_scan_results(struct wpa_supplicant * wpa_s)1676 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
1677 {
1678 #define SCAN_AP_LIMIT 128
1679 struct wpa_scan_result *results, *tmp;
1680 int num;
1681
1682 results = os_malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
1683 if (results == NULL) {
1684 wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
1685 "results");
1686 return -1;
1687 }
1688
1689 if (wpa_s->use_client_mlme) {
1690 num = ieee80211_sta_get_scan_results(wpa_s, results,
1691 SCAN_AP_LIMIT);
1692 } else
1693 num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
1694 wpa_printf(MSG_DEBUG, "Scan results: %d", num);
1695 if (num < 0) {
1696 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1697 os_free(results);
1698 return -1;
1699 }
1700 if (num > SCAN_AP_LIMIT) {
1701 wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
1702 num, SCAN_AP_LIMIT);
1703 num = SCAN_AP_LIMIT;
1704 }
1705
1706 /* Free unneeded memory for unused scan result entries */
1707 tmp = os_realloc(results, num * sizeof(struct wpa_scan_result));
1708 if (tmp || num == 0) {
1709 results = tmp;
1710 }
1711
1712 os_free(wpa_s->scan_results);
1713 wpa_s->scan_results = results;
1714 wpa_s->num_scan_results = num;
1715
1716 return 0;
1717 }
1718
1719
1720 #ifndef CONFIG_NO_WPA
wpa_get_beacon_ie(struct wpa_supplicant * wpa_s)1721 static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
1722 {
1723 int i, ret = 0;
1724 struct wpa_scan_result *results, *curr = NULL;
1725
1726 results = wpa_s->scan_results;
1727 if (results == NULL) {
1728 return -1;
1729 }
1730
1731 for (i = 0; i < wpa_s->num_scan_results; i++) {
1732 struct wpa_ssid *ssid = wpa_s->current_ssid;
1733 if (os_memcmp(results[i].bssid, wpa_s->bssid, ETH_ALEN) != 0)
1734 continue;
1735 if (ssid == NULL ||
1736 ((results[i].ssid_len == ssid->ssid_len &&
1737 os_memcmp(results[i].ssid, ssid->ssid, ssid->ssid_len)
1738 == 0) ||
1739 ssid->ssid_len == 0)) {
1740 curr = &results[i];
1741 break;
1742 }
1743 }
1744
1745 if (curr) {
1746 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, curr->wpa_ie,
1747 curr->wpa_ie_len) ||
1748 wpa_sm_set_ap_rsn_ie(wpa_s->wpa, curr->rsn_ie,
1749 curr->rsn_ie_len))
1750 ret = -1;
1751 } else {
1752 ret = -1;
1753 }
1754
1755 return ret;
1756 }
1757
1758
wpa_supplicant_get_beacon_ie(void * ctx)1759 static int wpa_supplicant_get_beacon_ie(void *ctx)
1760 {
1761 struct wpa_supplicant *wpa_s = ctx;
1762 if (wpa_get_beacon_ie(wpa_s) == 0) {
1763 return 0;
1764 }
1765
1766 /* No WPA/RSN IE found in the cached scan results. Try to get updated
1767 * scan results from the driver. */
1768 if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
1769 return -1;
1770 }
1771
1772 return wpa_get_beacon_ie(wpa_s);
1773 }
1774 #endif /* CONFIG_NO_WPA */
1775
1776
1777 /**
1778 * wpa_supplicant_get_ssid - Get a pointer to the current network structure
1779 * @wpa_s: Pointer to wpa_supplicant data
1780 * Returns: A pointer to the current network structure or %NULL on failure
1781 */
wpa_supplicant_get_ssid(struct wpa_supplicant * wpa_s)1782 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1783 {
1784 struct wpa_ssid *entry;
1785 u8 ssid[MAX_SSID_LEN];
1786 int res;
1787 size_t ssid_len;
1788 u8 bssid[ETH_ALEN];
1789 int wired;
1790
1791 if (wpa_s->use_client_mlme) {
1792 if (ieee80211_sta_get_ssid(wpa_s, ssid, &ssid_len)) {
1793 wpa_printf(MSG_WARNING, "Could not read SSID from "
1794 "MLME.");
1795 return NULL;
1796 }
1797 } else {
1798 res = wpa_drv_get_ssid(wpa_s, ssid);
1799 if (res < 0) {
1800 wpa_printf(MSG_WARNING, "Could not read SSID from "
1801 "driver.");
1802 return NULL;
1803 }
1804 ssid_len = res;
1805 }
1806
1807 if (wpa_s->use_client_mlme)
1808 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1809 else if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1810 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1811 return NULL;
1812 }
1813
1814 wired = wpa_s->conf->ap_scan == 0 && wpa_s->driver &&
1815 os_strcmp(wpa_s->driver->name, "wired") == 0;
1816
1817 entry = wpa_s->conf->ssid;
1818 while (entry) {
1819 if (!entry->disabled &&
1820 ((ssid_len == entry->ssid_len &&
1821 os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
1822 (!entry->bssid_set ||
1823 os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1824 return entry;
1825 entry = entry->next;
1826 }
1827
1828 return NULL;
1829 }
1830
1831
1832 #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)1833 static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
1834 const void *data, u16 data_len,
1835 size_t *msg_len, void **data_pos)
1836 {
1837 return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
1838 }
1839
1840
_wpa_ether_send(void * wpa_s,const u8 * dest,u16 proto,const u8 * buf,size_t len)1841 static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
1842 const u8 *buf, size_t len)
1843 {
1844 return wpa_ether_send(wpa_s, dest, proto, buf, len);
1845 }
1846
1847
_wpa_supplicant_cancel_auth_timeout(void * wpa_s)1848 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
1849 {
1850 wpa_supplicant_cancel_auth_timeout(wpa_s);
1851 }
1852
1853
_wpa_supplicant_set_state(void * wpa_s,wpa_states state)1854 static void _wpa_supplicant_set_state(void *wpa_s, wpa_states state)
1855 {
1856 wpa_supplicant_set_state(wpa_s, state);
1857 }
1858
1859
_wpa_supplicant_get_state(void * wpa_s)1860 static wpa_states _wpa_supplicant_get_state(void *wpa_s)
1861 {
1862 return wpa_supplicant_get_state(wpa_s);
1863 }
1864
1865
_wpa_supplicant_disassociate(void * wpa_s,int reason_code)1866 static void _wpa_supplicant_disassociate(void *wpa_s, int reason_code)
1867 {
1868 wpa_supplicant_disassociate(wpa_s, reason_code);
1869 /* Schedule a scan to make sure we continue looking for networks */
1870 wpa_supplicant_req_scan(wpa_s, 0, 0);
1871 }
1872
1873
_wpa_supplicant_deauthenticate(void * wpa_s,int reason_code)1874 static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
1875 {
1876 wpa_supplicant_deauthenticate(wpa_s, reason_code);
1877 /* Schedule a scan to make sure we continue looking for networks */
1878 wpa_supplicant_req_scan(wpa_s, 0, 0);
1879 }
1880
1881
_wpa_supplicant_get_ssid(void * wpa_s)1882 static struct wpa_ssid * _wpa_supplicant_get_ssid(void *wpa_s)
1883 {
1884 return wpa_supplicant_get_ssid(wpa_s);
1885 }
1886
1887
wpa_supplicant_get_bssid(void * ctx,u8 * bssid)1888 static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
1889 {
1890 struct wpa_supplicant *wpa_s = ctx;
1891 if (wpa_s->use_client_mlme) {
1892 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
1893 return 0;
1894 }
1895 return wpa_drv_get_bssid(wpa_s, bssid);
1896 }
1897
1898
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)1899 static int wpa_supplicant_set_key(void *wpa_s, wpa_alg alg,
1900 const u8 *addr, int key_idx, int set_tx,
1901 const u8 *seq, size_t seq_len,
1902 const u8 *key, size_t key_len)
1903 {
1904 return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
1905 key, key_len);
1906 }
1907
1908
wpa_supplicant_mlme_setprotection(void * wpa_s,const u8 * addr,int protection_type,int key_type)1909 static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
1910 int protection_type,
1911 int key_type)
1912 {
1913 return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
1914 key_type);
1915 }
1916
1917
wpa_supplicant_add_pmkid(void * wpa_s,const u8 * bssid,const u8 * pmkid)1918 static int wpa_supplicant_add_pmkid(void *wpa_s,
1919 const u8 *bssid, const u8 *pmkid)
1920 {
1921 return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
1922 }
1923
1924
wpa_supplicant_remove_pmkid(void * wpa_s,const u8 * bssid,const u8 * pmkid)1925 static int wpa_supplicant_remove_pmkid(void *wpa_s,
1926 const u8 *bssid, const u8 *pmkid)
1927 {
1928 return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
1929 }
1930 #endif /* CONFIG_NO_WPA */
1931
1932
wpa_supplicant_set_driver(struct wpa_supplicant * wpa_s,const char * name)1933 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1934 const char *name)
1935 {
1936 int i;
1937
1938 if (wpa_s == NULL)
1939 return -1;
1940
1941 if (wpa_supplicant_drivers[0] == NULL) {
1942 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1943 "wpa_supplicant.");
1944 return -1;
1945 }
1946
1947 if (name == NULL) {
1948 /* default to first driver in the list */
1949 wpa_s->driver = wpa_supplicant_drivers[0];
1950 return 0;
1951 }
1952
1953 for (i = 0; wpa_supplicant_drivers[i]; i++) {
1954 if (os_strcmp(name, wpa_supplicant_drivers[i]->name) == 0) {
1955 wpa_s->driver = wpa_supplicant_drivers[i];
1956 return 0;
1957 }
1958 }
1959
1960 wpa_printf(MSG_ERROR, "Unsupported driver '%s'.\n", name);
1961 return -1;
1962 }
1963
1964
wpa_supplicant_rx_eapol(void * ctx,const u8 * src_addr,const u8 * buf,size_t len)1965 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1966 const u8 *buf, size_t len)
1967 {
1968 struct wpa_supplicant *wpa_s = ctx;
1969
1970 wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1971 wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1972
1973 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1974 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1975 "no key management is configured");
1976 return;
1977 }
1978
1979 if (wpa_s->eapol_received == 0) {
1980 /* Timeout for completing IEEE 802.1X and WPA authentication */
1981 wpa_supplicant_req_auth_timeout(
1982 wpa_s,
1983 (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
1984 wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) ?
1985 70 : 10, 0);
1986 }
1987 wpa_s->eapol_received++;
1988
1989 if (wpa_s->countermeasures) {
1990 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1991 "packet");
1992 return;
1993 }
1994
1995 /* Source address of the incoming EAPOL frame could be compared to the
1996 * current BSSID. However, it is possible that a centralized
1997 * Authenticator could be using another MAC address than the BSSID of
1998 * an AP, so just allow any address to be used for now. The replies are
1999 * still sent to the current BSSID (if available), though. */
2000
2001 os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
2002 if (wpa_s->key_mgmt != WPA_KEY_MGMT_PSK &&
2003 eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
2004 return;
2005 wpa_drv_poll(wpa_s);
2006 wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
2007 }
2008
2009
2010 /**
2011 * wpa_supplicant_driver_init - Initialize driver interface parameters
2012 * @wpa_s: Pointer to wpa_supplicant data
2013 * @wait_for_interface: 0 = do not wait for the interface (reports a failure if
2014 * the interface is not present), 1 = wait until the interface is available
2015 * Returns: 0 on success, -1 on failure
2016 *
2017 * This function is called to initialize driver interface parameters.
2018 * wpa_drv_init() must have been called before this function to initialize the
2019 * driver interface.
2020 */
wpa_supplicant_driver_init(struct wpa_supplicant * wpa_s,int wait_for_interface)2021 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s,
2022 int wait_for_interface)
2023 {
2024 static int interface_count = 0;
2025
2026 for (;;) {
2027 if (wpa_s->driver->send_eapol) {
2028 const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
2029 if (addr)
2030 os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
2031 break;
2032 }
2033 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
2034 wpa_drv_get_mac_addr(wpa_s),
2035 ETH_P_EAPOL,
2036 wpa_supplicant_rx_eapol, wpa_s, 0);
2037 if (wpa_s->l2)
2038 break;
2039 else if (!wait_for_interface)
2040 return -1;
2041 wpa_printf(MSG_DEBUG, "Waiting for interface..");
2042 os_sleep(5, 0);
2043 }
2044
2045 if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
2046 wpa_printf(MSG_ERROR, "Failed to get own L2 address");
2047 return -1;
2048 }
2049
2050 wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
2051 MAC2STR(wpa_s->own_addr));
2052
2053 if (wpa_s->bridge_ifname[0]) {
2054 wpa_printf(MSG_DEBUG, "Receiving packets from bridge interface"
2055 " '%s'", wpa_s->bridge_ifname);
2056 wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
2057 wpa_s->own_addr,
2058 ETH_P_EAPOL,
2059 wpa_supplicant_rx_eapol, wpa_s,
2060 0);
2061 if (wpa_s->l2_br == NULL) {
2062 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
2063 "connection for the bridge interface '%s'",
2064 wpa_s->bridge_ifname);
2065 return -1;
2066 }
2067 }
2068
2069 /* Backwards compatibility call to set_wpa() handler. This is called
2070 * only just after init and just before deinit, so these handler can be
2071 * used to implement same functionality. */
2072 if (wpa_drv_set_wpa(wpa_s, 1) < 0) {
2073 struct wpa_driver_capa capa;
2074 if (wpa_drv_get_capa(wpa_s, &capa) < 0 ||
2075 !(capa.flags & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2076 WPA_DRIVER_CAPA_KEY_MGMT_WPA2))) {
2077 wpa_printf(MSG_DEBUG, "Driver does not support WPA.");
2078 /* Continue to allow non-WPA modes to be used. */
2079 } else {
2080 wpa_printf(MSG_ERROR, "Failed to enable WPA in the "
2081 "driver.");
2082 return -1;
2083 }
2084 }
2085
2086 wpa_clear_keys(wpa_s, NULL);
2087
2088 /* Make sure that TKIP countermeasures are not left enabled (could
2089 * happen if wpa_supplicant is killed during countermeasures. */
2090 wpa_drv_set_countermeasures(wpa_s, 0);
2091
2092 wpa_drv_set_drop_unencrypted(wpa_s, 1);
2093
2094 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
2095 wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
2096 interface_count++;
2097
2098 return 0;
2099 }
2100
2101
wpa_supplicant_daemon(const char * pid_file)2102 static int wpa_supplicant_daemon(const char *pid_file)
2103 {
2104 wpa_printf(MSG_DEBUG, "Daemonize..");
2105 return os_daemonize(pid_file);
2106 }
2107
2108
wpa_supplicant_alloc(void)2109 static struct wpa_supplicant * wpa_supplicant_alloc(void)
2110 {
2111 struct wpa_supplicant *wpa_s;
2112
2113 wpa_s = os_zalloc(sizeof(*wpa_s));
2114 if (wpa_s == NULL)
2115 return NULL;
2116 wpa_s->scan_req = 1;
2117
2118 return wpa_s;
2119 }
2120
2121
wpa_supplicant_init_iface(struct wpa_supplicant * wpa_s,struct wpa_interface * iface)2122 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
2123 struct wpa_interface *iface)
2124 {
2125 wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
2126 "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
2127 iface->confname ? iface->confname : "N/A",
2128 iface->driver ? iface->driver : "default",
2129 iface->ctrl_interface ? iface->ctrl_interface : "N/A",
2130 iface->bridge_ifname ? iface->bridge_ifname : "N/A");
2131
2132 if (wpa_supplicant_set_driver(wpa_s, iface->driver) < 0) {
2133 return -1;
2134 }
2135
2136 if (iface->confname) {
2137 #ifdef CONFIG_BACKEND_FILE
2138 wpa_s->confname = os_rel2abs_path(iface->confname);
2139 if (wpa_s->confname == NULL) {
2140 wpa_printf(MSG_ERROR, "Failed to get absolute path "
2141 "for configuration file '%s'.",
2142 iface->confname);
2143 return -1;
2144 }
2145 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
2146 iface->confname, wpa_s->confname);
2147 #else /* CONFIG_BACKEND_FILE */
2148 wpa_s->confname = os_strdup(iface->confname);
2149 #endif /* CONFIG_BACKEND_FILE */
2150 wpa_s->conf = wpa_config_read(wpa_s->confname);
2151 if (wpa_s->conf == NULL) {
2152 wpa_printf(MSG_ERROR, "Failed to read or parse "
2153 "configuration '%s'.", wpa_s->confname);
2154 return -1;
2155 }
2156
2157 /*
2158 * Override ctrl_interface and driver_param if set on command
2159 * line.
2160 */
2161 if (iface->ctrl_interface) {
2162 os_free(wpa_s->conf->ctrl_interface);
2163 wpa_s->conf->ctrl_interface =
2164 os_strdup(iface->ctrl_interface);
2165 }
2166
2167 if (iface->driver_param) {
2168 os_free(wpa_s->conf->driver_param);
2169 wpa_s->conf->driver_param =
2170 os_strdup(iface->driver_param);
2171 }
2172 } else
2173 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
2174 iface->driver_param);
2175
2176 if (wpa_s->conf == NULL) {
2177 wpa_printf(MSG_ERROR, "\nNo configuration found.");
2178 return -1;
2179 }
2180
2181 if (iface->ifname == NULL) {
2182 wpa_printf(MSG_ERROR, "\nInterface name is required.");
2183 return -1;
2184 }
2185 if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
2186 wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
2187 iface->ifname);
2188 return -1;
2189 }
2190 os_strncpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
2191
2192 if (iface->bridge_ifname) {
2193 if (os_strlen(iface->bridge_ifname) >=
2194 sizeof(wpa_s->bridge_ifname)) {
2195 wpa_printf(MSG_ERROR, "\nToo long bridge interface "
2196 "name '%s'.", iface->bridge_ifname);
2197 return -1;
2198 }
2199 os_strncpy(wpa_s->bridge_ifname, iface->bridge_ifname,
2200 sizeof(wpa_s->bridge_ifname));
2201 }
2202
2203 return 0;
2204 }
2205
2206
wpa_supplicant_init_eapol(struct wpa_supplicant * wpa_s)2207 static int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
2208 {
2209 #ifdef IEEE8021X_EAPOL
2210 struct eapol_ctx *ctx;
2211 ctx = os_zalloc(sizeof(*ctx));
2212 if (ctx == NULL) {
2213 wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
2214 return -1;
2215 }
2216
2217 ctx->ctx = wpa_s;
2218 ctx->msg_ctx = wpa_s;
2219 ctx->eapol_send_ctx = wpa_s;
2220 ctx->preauth = 0;
2221 ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
2222 ctx->eapol_send = wpa_supplicant_eapol_send;
2223 ctx->set_wep_key = wpa_eapol_set_wep_key;
2224 ctx->set_config_blob = wpa_supplicant_set_config_blob;
2225 ctx->get_config_blob = wpa_supplicant_get_config_blob;
2226 ctx->aborted_cached = wpa_supplicant_aborted_cached;
2227 ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
2228 ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
2229 ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
2230 wpa_s->eapol = eapol_sm_init(ctx);
2231 if (wpa_s->eapol == NULL) {
2232 os_free(ctx);
2233 wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
2234 "machines.");
2235 return -1;
2236 }
2237 #endif /* IEEE8021X_EAPOL */
2238
2239 return 0;
2240 }
2241
2242
wpa_supplicant_init_wpa(struct wpa_supplicant * wpa_s)2243 static int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
2244 {
2245 #ifndef CONFIG_NO_WPA
2246 struct wpa_sm_ctx *ctx;
2247 ctx = os_zalloc(sizeof(*ctx));
2248 if (ctx == NULL) {
2249 wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
2250 return -1;
2251 }
2252
2253 ctx->ctx = wpa_s;
2254 ctx->set_state = _wpa_supplicant_set_state;
2255 ctx->get_state = _wpa_supplicant_get_state;
2256 ctx->deauthenticate = _wpa_supplicant_deauthenticate;
2257 ctx->disassociate = _wpa_supplicant_disassociate;
2258 ctx->set_key = wpa_supplicant_set_key;
2259 ctx->scan = wpa_supplicant_scan;
2260 ctx->get_ssid = _wpa_supplicant_get_ssid;
2261 ctx->get_bssid = wpa_supplicant_get_bssid;
2262 ctx->ether_send = _wpa_ether_send;
2263 ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
2264 ctx->alloc_eapol = _wpa_alloc_eapol;
2265 ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
2266 ctx->add_pmkid = wpa_supplicant_add_pmkid;
2267 ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
2268 ctx->set_config_blob = wpa_supplicant_set_config_blob;
2269 ctx->get_config_blob = wpa_supplicant_get_config_blob;
2270 ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
2271
2272 wpa_s->wpa = wpa_sm_init(ctx);
2273 if (wpa_s->wpa == NULL) {
2274 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
2275 "machine");
2276 return -1;
2277 }
2278 #endif /* CONFIG_NO_WPA */
2279
2280 return 0;
2281 }
2282
2283
wpa_supplicant_init_iface2(struct wpa_supplicant * wpa_s,int wait_for_interface)2284 static int wpa_supplicant_init_iface2(struct wpa_supplicant *wpa_s,
2285 int wait_for_interface)
2286 {
2287 const char *ifname;
2288 struct wpa_driver_capa capa;
2289
2290 wpa_printf(MSG_DEBUG, "Initializing interface (2) '%s'",
2291 wpa_s->ifname);
2292
2293 if (wpa_supplicant_init_eapol(wpa_s) < 0)
2294 return -1;
2295
2296 /* RSNA Supplicant Key Management - INITIALIZE */
2297 eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
2298 eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
2299
2300 /* Initialize driver interface and register driver event handler before
2301 * L2 receive handler so that association events are processed before
2302 * EAPOL-Key packets if both become available for the same select()
2303 * call. */
2304 wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
2305 if (wpa_s->drv_priv == NULL) {
2306 wpa_printf(MSG_ERROR, "Failed to initialize driver interface");
2307 return -1;
2308 }
2309 if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
2310 wpa_printf(MSG_ERROR, "Driver interface rejected "
2311 "driver_param '%s'", wpa_s->conf->driver_param);
2312 return -1;
2313 }
2314
2315 ifname = wpa_drv_get_ifname(wpa_s);
2316 if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
2317 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
2318 "name with '%s'", ifname);
2319 os_strncpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
2320 }
2321
2322 if (wpa_supplicant_init_wpa(wpa_s) < 0)
2323 return -1;
2324
2325 wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
2326 wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
2327 NULL);
2328 wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
2329 wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
2330
2331 if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
2332 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
2333 wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
2334 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2335 "dot11RSNAConfigPMKLifetime");
2336 return -1;
2337 }
2338
2339 if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
2340 wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
2341 wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
2342 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2343 "dot11RSNAConfigPMKReauthThreshold");
2344 return -1;
2345 }
2346
2347 if (wpa_s->conf->dot11RSNAConfigSATimeout &&
2348 wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
2349 wpa_s->conf->dot11RSNAConfigSATimeout)) {
2350 wpa_printf(MSG_ERROR, "Invalid WPA parameter value for "
2351 "dot11RSNAConfigSATimeout");
2352 return -1;
2353 }
2354
2355 if (wpa_supplicant_driver_init(wpa_s, wait_for_interface) < 0) {
2356 return -1;
2357 }
2358 wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
2359
2360 wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
2361 if (wpa_s->ctrl_iface == NULL) {
2362 wpa_printf(MSG_ERROR,
2363 "Failed to initialize control interface '%s'.\n"
2364 "You may have another wpa_supplicant process "
2365 "already running or the file was\n"
2366 "left by an unclean termination of wpa_supplicant "
2367 "in which case you will need\n"
2368 "to manually remove this file before starting "
2369 "wpa_supplicant again.\n",
2370 wpa_s->conf->ctrl_interface);
2371 return -1;
2372 }
2373
2374 if (wpa_drv_get_capa(wpa_s, &capa) == 0 &&
2375 capa.flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
2376 wpa_s->use_client_mlme = 1;
2377 if (ieee80211_sta_init(wpa_s))
2378 return -1;
2379 }
2380
2381 return 0;
2382 }
2383
2384
wpa_supplicant_deinit_iface(struct wpa_supplicant * wpa_s)2385 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
2386 {
2387 if (wpa_s->drv_priv) {
2388 wpa_supplicant_deauthenticate(wpa_s, REASON_DEAUTH_LEAVING);
2389
2390 /* Backwards compatibility call to set_wpa() handler. This is
2391 * called only just after init and just before deinit, so these
2392 * handler can be used to implement same functionality. */
2393 if (wpa_drv_set_wpa(wpa_s, 0) < 0) {
2394 wpa_printf(MSG_ERROR, "Failed to disable WPA in the "
2395 "driver.");
2396 }
2397
2398 wpa_drv_set_drop_unencrypted(wpa_s, 0);
2399 wpa_drv_set_countermeasures(wpa_s, 0);
2400 wpa_clear_keys(wpa_s, NULL);
2401 }
2402
2403 wpas_dbus_unregister_iface(wpa_s);
2404
2405 wpa_supplicant_cleanup(wpa_s);
2406
2407 if (wpa_s->drv_priv)
2408 wpa_drv_deinit(wpa_s);
2409 }
2410
2411
2412 /**
2413 * wpa_supplicant_add_iface - Add a new network interface
2414 * @global: Pointer to global data from wpa_supplicant_init()
2415 * @iface: Interface configuration options
2416 * Returns: Pointer to the created interface or %NULL on failure
2417 *
2418 * This function is used to add new network interfaces for %wpa_supplicant.
2419 * This can be called before wpa_supplicant_run() to add interfaces before the
2420 * main event loop has been started. In addition, new interfaces can be added
2421 * dynamically while %wpa_supplicant is already running. This could happen,
2422 * e.g., when a hotplug network adapter is inserted.
2423 */
wpa_supplicant_add_iface(struct wpa_global * global,struct wpa_interface * iface)2424 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
2425 struct wpa_interface *iface)
2426 {
2427 struct wpa_supplicant *wpa_s;
2428
2429 if (global == NULL || iface == NULL)
2430 return NULL;
2431
2432 wpa_s = wpa_supplicant_alloc();
2433 if (wpa_s == NULL)
2434 return NULL;
2435
2436 if (wpa_supplicant_init_iface(wpa_s, iface) ||
2437 wpa_supplicant_init_iface2(wpa_s,
2438 global->params.wait_for_interface)) {
2439 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2440 iface->ifname);
2441 wpa_supplicant_deinit_iface(wpa_s);
2442 os_free(wpa_s);
2443 return NULL;
2444 }
2445
2446 wpa_s->global = global;
2447
2448 /* Register the interface with the dbus control interface */
2449 if (wpas_dbus_register_iface(wpa_s)) {
2450 wpa_supplicant_deinit_iface(wpa_s);
2451 os_free(wpa_s);
2452 return NULL;
2453 }
2454
2455 #ifdef ANDROID
2456 char scan_prop[PROPERTY_VALUE_MAX];
2457 char *endp;
2458 if (property_get("wifi.supplicant_scan_interval", scan_prop, "5") != 0) {
2459 wpa_s->scan_interval = (int)strtol(scan_prop, &endp, 0);
2460 if (endp == scan_prop) {
2461 wpa_s->scan_interval = 5;
2462 }
2463 }
2464 #endif
2465 wpa_s->next = global->ifaces;
2466 global->ifaces = wpa_s;
2467
2468 wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2469
2470 return wpa_s;
2471 }
2472
2473
2474 /**
2475 * wpa_supplicant_remove_iface - Remove a network interface
2476 * @global: Pointer to global data from wpa_supplicant_init()
2477 * @wpa_s: Pointer to the network interface to be removed
2478 * Returns: 0 if interface was removed, -1 if interface was not found
2479 *
2480 * This function can be used to dynamically remove network interfaces from
2481 * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2482 * addition, this function is used to remove all remaining interfaces when
2483 * %wpa_supplicant is terminated.
2484 */
wpa_supplicant_remove_iface(struct wpa_global * global,struct wpa_supplicant * wpa_s)2485 int wpa_supplicant_remove_iface(struct wpa_global *global,
2486 struct wpa_supplicant *wpa_s)
2487 {
2488 struct wpa_supplicant *prev;
2489
2490 /* Remove interface from the global list of interfaces */
2491 prev = global->ifaces;
2492 if (prev == wpa_s) {
2493 global->ifaces = wpa_s->next;
2494 } else {
2495 while (prev && prev->next != wpa_s)
2496 prev = prev->next;
2497 if (prev == NULL)
2498 return -1;
2499 prev->next = wpa_s->next;
2500 }
2501
2502 wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2503
2504 wpa_supplicant_deinit_iface(wpa_s);
2505 os_free(wpa_s);
2506
2507 return 0;
2508 }
2509
2510
2511 /**
2512 * wpa_supplicant_get_iface - Get a new network interface
2513 * @global: Pointer to global data from wpa_supplicant_init()
2514 * @ifname: Interface name
2515 * Returns: Pointer to the interface or %NULL if not found
2516 */
wpa_supplicant_get_iface(struct wpa_global * global,const char * ifname)2517 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2518 const char *ifname)
2519 {
2520 struct wpa_supplicant *wpa_s;
2521
2522 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2523 if (os_strcmp(wpa_s->ifname, ifname) == 0)
2524 return wpa_s;
2525 }
2526 return NULL;
2527 }
2528
2529
2530 /**
2531 * wpa_supplicant_init - Initialize %wpa_supplicant
2532 * @params: Parameters for %wpa_supplicant
2533 * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2534 *
2535 * This function is used to initialize %wpa_supplicant. After successful
2536 * initialization, the returned data pointer can be used to add and remove
2537 * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2538 */
wpa_supplicant_init(struct wpa_params * params)2539 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2540 {
2541 struct wpa_global *global;
2542 int ret;
2543
2544 if (params == NULL)
2545 return NULL;
2546
2547 wpa_debug_open_file(params->wpa_debug_file_path);
2548
2549 ret = eap_peer_register_methods();
2550 if (ret) {
2551 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2552 if (ret == -2)
2553 wpa_printf(MSG_ERROR, "Two or more EAP methods used "
2554 "the same EAP type.");
2555 return NULL;
2556 }
2557
2558 global = os_zalloc(sizeof(*global));
2559 if (global == NULL)
2560 return NULL;
2561 global->params.daemonize = params->daemonize;
2562 global->params.wait_for_interface = params->wait_for_interface;
2563 global->params.wait_for_monitor = params->wait_for_monitor;
2564 global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
2565 if (params->pid_file)
2566 global->params.pid_file = os_strdup(params->pid_file);
2567 if (params->ctrl_interface)
2568 global->params.ctrl_interface =
2569 os_strdup(params->ctrl_interface);
2570 wpa_debug_level = global->params.wpa_debug_level =
2571 params->wpa_debug_level;
2572 wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2573 params->wpa_debug_show_keys;
2574 wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2575 params->wpa_debug_timestamp;
2576
2577 if (eloop_init(global)) {
2578 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2579 wpa_supplicant_deinit(global);
2580 return NULL;
2581 }
2582
2583 global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
2584 if (global->ctrl_iface == NULL) {
2585 wpa_supplicant_deinit(global);
2586 return NULL;
2587 }
2588
2589 if (global->params.dbus_ctrl_interface) {
2590 global->dbus_ctrl_iface =
2591 wpa_supplicant_dbus_ctrl_iface_init(global);
2592 if (global->dbus_ctrl_iface == NULL) {
2593 wpa_supplicant_deinit(global);
2594 return NULL;
2595 }
2596 }
2597
2598 if (global->params.wait_for_interface && global->params.daemonize &&
2599 wpa_supplicant_daemon(global->params.pid_file)) {
2600 wpa_supplicant_deinit(global);
2601 return NULL;
2602 }
2603
2604 return global;
2605 }
2606
2607
2608 /**
2609 * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2610 * @global: Pointer to global data from wpa_supplicant_init()
2611 * Returns: 0 after successful event loop run, -1 on failure
2612 *
2613 * This function starts the main event loop and continues running as long as
2614 * there are any remaining events. In most cases, this function is running as
2615 * long as the %wpa_supplicant process in still in use.
2616 */
wpa_supplicant_run(struct wpa_global * global)2617 int wpa_supplicant_run(struct wpa_global *global)
2618 {
2619 struct wpa_supplicant *wpa_s;
2620
2621 if (!global->params.wait_for_interface && global->params.daemonize &&
2622 wpa_supplicant_daemon(global->params.pid_file))
2623 return -1;
2624
2625 if (global->params.wait_for_monitor) {
2626 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2627 if (wpa_s->ctrl_iface)
2628 wpa_supplicant_ctrl_iface_wait(
2629 wpa_s->ctrl_iface);
2630 }
2631
2632 eloop_register_signal_terminate(wpa_supplicant_terminate, NULL);
2633 eloop_register_signal_reconfig(wpa_supplicant_reconfig, NULL);
2634
2635 eloop_run();
2636
2637 return 0;
2638 }
2639
2640
2641 /**
2642 * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2643 * @global: Pointer to global data from wpa_supplicant_init()
2644 *
2645 * This function is called to deinitialize %wpa_supplicant and to free all
2646 * allocated resources. Remaining network interfaces will also be removed.
2647 */
wpa_supplicant_deinit(struct wpa_global * global)2648 void wpa_supplicant_deinit(struct wpa_global *global)
2649 {
2650 if (global == NULL)
2651 return;
2652
2653 wpa_supplicant_terminate(0, global, NULL);
2654
2655 while (global->ifaces)
2656 wpa_supplicant_remove_iface(global, global->ifaces);
2657
2658 if (global->ctrl_iface)
2659 wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
2660 if (global->dbus_ctrl_iface)
2661 wpa_supplicant_dbus_ctrl_iface_deinit(global->dbus_ctrl_iface);
2662
2663 eap_peer_unregister_methods();
2664
2665 eloop_destroy();
2666
2667 if (global->params.pid_file) {
2668 os_daemonize_terminate(global->params.pid_file);
2669 os_free(global->params.pid_file);
2670 }
2671 os_free(global->params.ctrl_interface);
2672
2673 os_free(global);
2674 wpa_debug_close_file();
2675 }
2676