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