]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/wpa_supplicant/wpa_supplicant.c
This commit was generated by cvs2svn to compensate for changes in r168609,
[FreeBSD/FreeBSD.git] / contrib / wpa_supplicant / wpa_supplicant.c
1 /*
2  * WPA Supplicant
3  * Copyright (c) 2003-2006, Jouni Malinen <jkmaline@cc.hut.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  * $FreeBSD$
15  */
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <time.h>
23 #include <signal.h>
24 #ifndef CONFIG_NATIVE_WINDOWS
25 #include <netinet/in.h>
26 #endif /* CONFIG_NATIVE_WINDOWS */
27
28 #include "common.h"
29 #include "eapol_sm.h"
30 #include "eap.h"
31 #include "wpa.h"
32 #include "eloop.h"
33 #include "wpa_supplicant.h"
34 #include "config.h"
35 #include "l2_packet.h"
36 #include "wpa_supplicant_i.h"
37 #include "ctrl_iface.h"
38 #include "pcsc_funcs.h"
39 #include "version.h"
40 #include "preauth.h"
41 #include "wpa_ctrl.h"
42
43 const char *wpa_supplicant_version =
44 "wpa_supplicant v" VERSION_STR "\n"
45 "Copyright (c) 2003-2006, Jouni Malinen <jkmaline@cc.hut.fi> and contributors";
46
47 const char *wpa_supplicant_license =
48 "This program is free software. You can distribute it and/or modify it\n"
49 "under the terms of the GNU General Public License version 2.\n"
50 "\n"
51 "Alternatively, this software may be distributed under the terms of the\n"
52 "BSD license. See README and COPYING for more details.\n"
53 #ifdef EAP_TLS_FUNCS
54 "\nThis product includes software developed by the OpenSSL Project\n"
55 "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
56 #endif /* EAP_TLS_FUNCS */
57 ;
58
59 #ifndef CONFIG_NO_STDOUT_DEBUG
60 const char *wpa_supplicant_full_license =
61 "This program is free software; you can redistribute it and/or modify\n"
62 "it under the terms of the GNU General Public License version 2 as\n"
63 "published by the Free Software Foundation.\n"
64 "\n"
65 "This program is distributed in the hope that it will be useful,\n"
66 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
67 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
68 "GNU General Public License for more details.\n"
69 "\n"
70 "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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "1. Redistributions of source code must retain the above copyright\n"
82 "   notice, this list of conditions and the following disclaimer.\n"
83 "\n"
84 "2. Redistributions in binary form must reproduce the above copyright\n"
85 "   notice, this list of conditions and the following disclaimer in the\n"
86 "   documentation and/or other materials provided with the distribution.\n"
87 "\n"
88 "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
89 "   names of its contributors may be used to endorse or promote products\n"
90 "   derived from this software without specific prior written permission.\n"
91 "\n"
92 "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
93 "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
94 "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
95 "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n"
96 "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
97 "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
98 "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
99 "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
100 "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
101 "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
102 "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
103 "\n";
104 #endif /* CONFIG_NO_STDOUT_DEBUG */
105
106 extern struct wpa_driver_ops *wpa_supplicant_drivers[];
107
108 extern int wpa_debug_level;
109 extern int wpa_debug_show_keys;
110 extern int wpa_debug_timestamp;
111
112 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx);
113
114 void wpa_msg(struct wpa_supplicant *wpa_s, int level, char *fmt, ...)
115 {
116         va_list ap;
117         char *buf;
118         const int buflen = 2048;
119         int len;
120
121         buf = malloc(buflen);
122         if (buf == NULL) {
123                 printf("Failed to allocate message buffer for:\n");
124                 va_start(ap, fmt);
125                 vprintf(fmt, ap);
126                 printf("\n");
127                 va_end(ap);
128                 return;
129         }
130         va_start(ap, fmt);
131         len = vsnprintf(buf, buflen, fmt, ap);
132         va_end(ap);
133         wpa_printf(level, "%s", buf);
134         wpa_supplicant_ctrl_iface_send(wpa_s, level, buf, len);
135         free(buf);
136 }
137
138
139 static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
140                             const void *data, u16 data_len,
141                             size_t *msg_len, void **data_pos)
142 {
143         struct ieee802_1x_hdr *hdr;
144
145         *msg_len = sizeof(*hdr) + data_len;
146         hdr = malloc(*msg_len);
147         if (hdr == NULL)
148                 return NULL;
149
150         hdr->version = wpa_s->conf->eapol_version;
151         hdr->type = type;
152         hdr->length = htons(data_len);
153
154         if (data)
155                 memcpy(hdr + 1, data, data_len);
156         else
157                 memset(hdr + 1, 0, data_len);
158
159         if (data_pos)
160                 *data_pos = hdr + 1;
161
162         return (u8 *) hdr;
163 }
164
165
166 /**
167  * wpa_ether_send - Send Ethernet frame
168  * @wpa_s: pointer to wpa_supplicant data
169  * @dest: Destination MAC address
170  * @proto: Ethertype
171  * @buf: Frame payload starting from IEEE 802.1X header
172  * @len: Frame payload length
173  */
174 int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest, u16 proto,
175                    const u8 *buf, size_t len)
176 {
177         if (wpa_s->l2) {
178                 return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
179         }
180
181         return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
182 }
183
184
185 #ifdef IEEE8021X_EAPOL
186 /**
187  * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
188  * @ctx: pointer to wpa_supplicant data
189  * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
190  * @buf: EAPOL payload (after IEEE 802.1X header)
191  * @len: EAPOL payload length
192  *
193  * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
194  * to the current Authenticator.
195  */
196 static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
197                                      size_t len)
198 {
199         struct wpa_supplicant *wpa_s = ctx;
200         u8 *msg, *dst, bssid[ETH_ALEN];
201         size_t msglen;
202         int res;
203
204         /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
205          * extra copy here */
206
207         if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK ||
208             wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
209                 /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
210                  * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
211                  * machines. */
212                 wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
213                            "mode (type=%d len=%lu)", type,
214                            (unsigned long) len);
215                 return -1;
216         }
217
218         if (pmksa_cache_get_current(wpa_s->wpa) &&
219             type == IEEE802_1X_TYPE_EAPOL_START) {
220                 /* Trying to use PMKSA caching - do not send EAPOL-Start frames
221                  * since they will trigger full EAPOL authentication. */
222                 wpa_printf(MSG_DEBUG, "RSN: PMKSA caching - do not send "
223                            "EAPOL-Start");
224                 return -1;
225         }
226
227         if (memcmp(wpa_s->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0) {
228                 wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
229                            "EAPOL frame");
230                 if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
231                     memcmp(bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) != 0) {
232                         dst = bssid;
233                         wpa_printf(MSG_DEBUG, "Using current BSSID " MACSTR
234                                    " from the driver as the EAPOL destination",
235                                    MAC2STR(dst));
236                 } else {
237                         dst = wpa_s->last_eapol_src;
238                         wpa_printf(MSG_DEBUG, "Using the source address of the"
239                                    " last received EAPOL frame " MACSTR " as "
240                                    "the EAPOL destination",
241                                    MAC2STR(dst));
242                 }
243         } else {
244                 /* BSSID was already set (from (Re)Assoc event, so use it as
245                  * the EAPOL destination. */
246                 dst = wpa_s->bssid;
247         }
248
249         msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
250         if (msg == NULL)
251                 return -1;
252
253         wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
254         res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
255         free(msg);
256         return res;
257 }
258
259
260 /**
261  * wpa_eapol_set_wep_key - set WEP key for the driver
262  * @ctx: pointer to wpa_supplicant data
263  * @unicast: 1 = individual unicast key, 0 = broadcast key
264  * @keyidx: WEP key index (0..3)
265  * @key: pointer to key data
266  * @keylen: key length in bytes
267  *
268  * Returns 0 on success or < 0 on error.
269  */
270 static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
271                                  const u8 *key, size_t keylen)
272 {
273         struct wpa_supplicant *wpa_s = ctx;
274         return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
275                                unicast ? wpa_s->bssid :
276                                (u8 *) "\xff\xff\xff\xff\xff\xff",
277                                keyidx, unicast, (u8 *) "", 0, key, keylen);
278 }
279 #endif /* IEEE8021X_EAPOL */
280
281
282 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
283 static void wpa_supplicant_set_config_blob(void *ctx,
284                                            struct wpa_config_blob *blob)
285 {
286         struct wpa_supplicant *wpa_s = ctx;
287         wpa_config_set_blob(wpa_s->conf, blob);
288 }
289
290
291 static const struct wpa_config_blob *
292 wpa_supplicant_get_config_blob(void *ctx, const char *name)
293 {
294         struct wpa_supplicant *wpa_s = ctx;
295         return wpa_config_get_blob(wpa_s->conf, name);
296 }
297 #endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
298
299
300 /* Configure default/group WEP key for static WEP */
301 static int wpa_set_wep_key(void *ctx, int set_tx, int keyidx, const u8 *key,
302                            size_t keylen)
303 {
304         struct wpa_supplicant *wpa_s = ctx;
305         return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
306                                (u8 *) "\xff\xff\xff\xff\xff\xff",
307                                keyidx, set_tx, (u8 *) "", 0, key, keylen);
308 }
309
310
311 static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
312                                            struct wpa_ssid *ssid)
313 {
314         u8 key[32];
315         size_t keylen;
316         wpa_alg alg;
317         u8 seq[6] = { 0 };
318
319         /* IBSS/WPA-None uses only one key (Group) for both receiving and
320          * sending unicast and multicast packets. */
321
322         if (ssid->mode != IEEE80211_MODE_IBSS) {
323                 wpa_printf(MSG_INFO, "WPA: Invalid mode %d (not IBSS/ad-hoc) "
324                            "for WPA-None", ssid->mode);
325                 return -1;
326         }
327
328         if (!ssid->psk_set) {
329                 wpa_printf(MSG_INFO, "WPA: No PSK configured for WPA-None");
330                 return -1;
331         }
332
333         switch (wpa_s->group_cipher) {
334         case WPA_CIPHER_CCMP:
335                 memcpy(key, ssid->psk, 16);
336                 keylen = 16;
337                 alg = WPA_ALG_CCMP;
338                 break;
339         case WPA_CIPHER_TKIP:
340                 /* WPA-None uses the same Michael MIC key for both TX and RX */
341                 memcpy(key, ssid->psk, 16 + 8);
342                 memcpy(key + 16 + 8, ssid->psk + 16, 8);
343                 keylen = 32;
344                 alg = WPA_ALG_TKIP;
345                 break;
346         default:
347                 wpa_printf(MSG_INFO, "WPA: Invalid group cipher %d for "
348                            "WPA-None", wpa_s->group_cipher);
349                 return -1;
350         }
351
352         /* TODO: should actually remember the previously used seq#, both for TX
353          * and RX from each STA.. */
354
355         return wpa_drv_set_key(wpa_s, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
356                                0, 1, seq, 6, key, keylen);
357 }
358
359
360 #ifdef IEEE8021X_EAPOL
361 static void wpa_supplicant_notify_eapol_done(void *ctx)
362 {
363         struct wpa_supplicant *wpa_s = ctx;
364         wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
365         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X) {
366                 wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
367         } else {
368                 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
369                 wpa_supplicant_cancel_auth_timeout(wpa_s);
370                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
371         }
372 }
373 #endif /* IEEE8021X_EAPOL */
374
375
376 struct wpa_blacklist * wpa_blacklist_get(struct wpa_supplicant *wpa_s,
377                                          const u8 *bssid)
378 {
379         struct wpa_blacklist *e;
380
381         e = wpa_s->blacklist;
382         while (e) {
383                 if (memcmp(e->bssid, bssid, ETH_ALEN) == 0)
384                         return e;
385                 e = e->next;
386         }
387
388         return NULL;
389 }
390
391
392 int wpa_blacklist_add(struct wpa_supplicant *wpa_s, const u8 *bssid)
393 {
394         struct wpa_blacklist *e;
395
396         e = wpa_blacklist_get(wpa_s, bssid);
397         if (e) {
398                 e->count++;
399                 wpa_printf(MSG_DEBUG, "BSSID " MACSTR " blacklist count "
400                            "incremented to %d",
401                            MAC2STR(bssid), e->count);
402                 return 0;
403         }
404
405         e = malloc(sizeof(*e));
406         if (e == NULL)
407                 return -1;
408         memset(e, 0, sizeof(*e));
409         memcpy(e->bssid, bssid, ETH_ALEN);
410         e->count = 1;
411         e->next = wpa_s->blacklist;
412         wpa_s->blacklist = e;
413         wpa_printf(MSG_DEBUG, "Added BSSID " MACSTR " into blacklist",
414                    MAC2STR(bssid));
415
416         return 0;
417 }
418
419
420 int wpa_blacklist_del(struct wpa_supplicant *wpa_s, const u8 *bssid)
421 {
422         struct wpa_blacklist *e, *prev = NULL;
423
424         e = wpa_s->blacklist;
425         while (e) {
426                 if (memcmp(e->bssid, bssid, ETH_ALEN) == 0) {
427                         if (prev == NULL) {
428                                 wpa_s->blacklist = e->next;
429                         } else {
430                                 prev->next = e->next;
431                         }
432                         wpa_printf(MSG_DEBUG, "Removed BSSID " MACSTR " from "
433                                    "blacklist", MAC2STR(bssid));
434                         free(e);
435                         return 0;
436                 }
437                 prev = e;
438                 e = e->next;
439         }
440         return -1;
441 }
442
443
444 void wpa_blacklist_clear(struct wpa_supplicant *wpa_s)
445 {
446         struct wpa_blacklist *e, *prev;
447
448         e = wpa_s->blacklist;
449         wpa_s->blacklist = NULL;
450         while (e) {
451                 prev = e;
452                 e = e->next;
453                 wpa_printf(MSG_DEBUG, "Removed BSSID " MACSTR " from "
454                            "blacklist (clear)", MAC2STR(prev->bssid));
455                 free(prev);
456         }
457 }
458
459
460 const char * wpa_ssid_txt(u8 *ssid, size_t ssid_len)
461 {
462         static char ssid_txt[MAX_SSID_LEN + 1];
463         char *pos;
464
465         if (ssid_len > MAX_SSID_LEN)
466                 ssid_len = MAX_SSID_LEN;
467         memcpy(ssid_txt, ssid, ssid_len);
468         ssid_txt[ssid_len] = '\0';
469         for (pos = ssid_txt; *pos != '\0'; pos++) {
470                 if ((u8) *pos < 32 || (u8) *pos >= 127)
471                         *pos = '_';
472         }
473         return ssid_txt;
474 }
475
476
477 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
478 {
479         wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
480                 sec, usec);
481         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
482         eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
483 }
484
485
486 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
487 {
488         wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
489         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
490 }
491
492
493 static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
494 {
495         struct wpa_supplicant *wpa_s = eloop_ctx;
496         wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
497                 MAC2STR(wpa_s->bssid));
498         wpa_blacklist_add(wpa_s, wpa_s->bssid);
499         wpa_sm_notify_disassoc(wpa_s->wpa);
500         wpa_supplicant_disassociate(wpa_s, REASON_DEAUTH_LEAVING);
501         wpa_s->reassociate = 1;
502         wpa_supplicant_req_scan(wpa_s, 0, 0);
503 }
504
505
506 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
507                                      int sec, int usec)
508 {
509         if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
510             wpa_s->driver && strcmp(wpa_s->driver->name, "wired") == 0)
511                 return;
512
513         wpa_msg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
514                 "%d usec", sec, usec);
515         eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
516         eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
517 }
518
519
520 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
521 {
522         wpa_msg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
523         eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
524         wpa_blacklist_del(wpa_s, wpa_s->bssid);
525 }
526
527
528 void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
529 {
530         struct eapol_config eapol_conf;
531         struct wpa_ssid *ssid = wpa_s->current_ssid;
532
533         if (wpa_s->key_mgmt == WPA_KEY_MGMT_PSK) {
534                 eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
535                 eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
536         }
537         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
538             wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
539                 eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
540         else
541                 eapol_sm_notify_portControl(wpa_s->eapol, Auto);
542
543         memset(&eapol_conf, 0, sizeof(eapol_conf));
544         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
545                 eapol_conf.accept_802_1x_keys = 1;
546                 eapol_conf.required_keys = 0;
547                 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
548                         eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
549                 }
550                 if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
551                         eapol_conf.required_keys |=
552                                 EAPOL_REQUIRE_KEY_BROADCAST;
553                 }
554
555                 if (wpa_s->conf && wpa_s->driver &&
556                     strcmp(wpa_s->driver->name, "wired") == 0) {
557                         eapol_conf.required_keys = 0;
558                 }
559         }
560         eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
561         eapol_conf.workaround = ssid->eap_workaround;
562         eapol_conf.eap_disabled = wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X &&
563                 wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA;
564         eapol_sm_notify_config(wpa_s->eapol, ssid, &eapol_conf);
565 }
566
567
568 void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
569                                        struct wpa_ssid *ssid)
570 {
571         int i;
572
573         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
574                 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
575         else
576                 wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
577         wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
578         wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
579         wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
580         wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
581         wpa_s->group_cipher = WPA_CIPHER_NONE;
582
583         for (i = 0; i < NUM_WEP_KEYS; i++) {
584                 if (ssid->wep_key_len[i] > 5) {
585                         wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
586                         wpa_s->group_cipher = WPA_CIPHER_WEP104;
587                         break;
588                 } else if (ssid->wep_key_len[i] > 0) {
589                         wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
590                         wpa_s->group_cipher = WPA_CIPHER_WEP40;
591                         break;
592                 }
593         }
594
595         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
596         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
597                          wpa_s->pairwise_cipher);
598         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
599
600         pmksa_cache_clear_current(wpa_s->wpa);
601 }
602
603
604 static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
605 {
606         scard_deinit(wpa_s->scard);
607         wpa_s->scard = NULL;
608         wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
609         eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
610         l2_packet_deinit(wpa_s->l2);
611         wpa_s->l2 = NULL;
612
613         wpa_supplicant_ctrl_iface_deinit(wpa_s);
614         if (wpa_s->conf != NULL) {
615                 wpa_config_free(wpa_s->conf);
616                 wpa_s->conf = NULL;
617         }
618
619         free(wpa_s->confname);
620         wpa_s->confname = NULL;
621
622         wpa_sm_set_eapol(wpa_s->wpa, NULL);
623         eapol_sm_deinit(wpa_s->eapol);
624         wpa_s->eapol = NULL;
625
626         rsn_preauth_deinit(wpa_s->wpa);
627
628         pmksa_candidate_free(wpa_s->wpa);
629         pmksa_cache_free(wpa_s->wpa);
630         wpa_sm_deinit(wpa_s->wpa);
631         wpa_s->wpa = NULL;
632         wpa_blacklist_clear(wpa_s);
633
634         free(wpa_s->scan_results);
635         wpa_s->scan_results = NULL;
636         wpa_s->num_scan_results = 0;
637
638         wpa_supplicant_cancel_scan(wpa_s);
639 }
640
641
642 void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
643 {
644         u8 *bcast = (u8 *) "\xff\xff\xff\xff\xff\xff";
645
646         if (wpa_s->keys_cleared) {
647                 /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
648                  * timing issues with keys being cleared just before new keys
649                  * are set or just after association or something similar. This
650                  * shows up in group key handshake failing often because of the
651                  * client not receiving the first encrypted packets correctly.
652                  * Skipping some of the extra key clearing steps seems to help
653                  * in completing group key handshake more reliably. */
654                 wpa_printf(MSG_DEBUG, "No keys have been configured - "
655                            "skip key clearing");
656                 return;
657         }
658
659         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 0, 0, NULL, 0, NULL, 0);
660         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 1, 0, NULL, 0, NULL, 0);
661         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 2, 0, NULL, 0, NULL, 0);
662         wpa_drv_set_key(wpa_s, WPA_ALG_NONE, bcast, 3, 0, NULL, 0, NULL, 0);
663         if (addr) {
664                 wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
665                                 0);
666         }
667         wpa_s->keys_cleared = 1;
668 }
669
670
671 const char * wpa_supplicant_state_txt(int state)
672 {
673         switch (state) {
674         case WPA_DISCONNECTED:
675                 return "DISCONNECTED";
676         case WPA_INACTIVE:
677                 return "INACTIVE";
678         case WPA_SCANNING:
679                 return "SCANNING";
680         case WPA_ASSOCIATING:
681                 return "ASSOCIATING";
682         case WPA_ASSOCIATED:
683                 return "ASSOCIATED";
684         case WPA_4WAY_HANDSHAKE:
685                 return "4WAY_HANDSHAKE";
686         case WPA_GROUP_HANDSHAKE:
687                 return "GROUP_HANDSHAKE";
688         case WPA_COMPLETED:
689                 return "COMPLETED";
690         default:
691                 return "UNKNOWN";
692         }
693 }
694
695
696 void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s, wpa_states state)
697 {
698         wpa_printf(MSG_DEBUG, "State: %s -> %s",
699                    wpa_supplicant_state_txt(wpa_s->wpa_state),
700                    wpa_supplicant_state_txt(state));
701         if (state == WPA_COMPLETED && wpa_s->new_connection) {
702                 wpa_s->new_connection = 0;
703                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
704                         MACSTR " completed %s",
705                         MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
706                         "(reauth)" : "(auth)");
707                 wpa_s->reassociated_connection = 1;
708         } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
709                    state == WPA_ASSOCIATED) {
710                 wpa_s->new_connection = 1;
711         }
712         wpa_s->wpa_state = state;
713 }
714
715
716 wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
717 {
718         return wpa_s->wpa_state;
719 }
720
721
722 static void wpa_supplicant_terminate(int sig, void *eloop_ctx,
723                                      void *signal_ctx)
724 {
725         struct wpa_global *global = eloop_ctx;
726         struct wpa_supplicant *wpa_s;
727         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
728                 wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING "- signal %d "
729                         "received", sig);
730         }
731         eloop_terminate();
732 }
733
734
735 int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
736 {
737         struct wpa_config *conf;
738         int reconf_ctrl;
739         if (wpa_s->confname == NULL)
740                 return -1;
741         conf = wpa_config_read(wpa_s->confname);
742         if (conf == NULL) {
743                 wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
744                         "file '%s' - exiting", wpa_s->confname);
745                 return -1;
746         }
747
748         reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
749                 || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
750                     strcmp(conf->ctrl_interface, wpa_s->conf->ctrl_interface)
751                     != 0);
752
753         if (reconf_ctrl)
754                 wpa_supplicant_ctrl_iface_deinit(wpa_s);
755
756         wpa_s->current_ssid = NULL;
757         /*
758          * TODO: should notify EAPOL SM about changes in opensc_engine_path,
759          * pkcs11_engine_path, pkcs11_module_path.
760          */ 
761         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
762         wpa_sm_set_config(wpa_s->wpa, NULL);
763         wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
764         pmksa_cache_notify_reconfig(wpa_s->wpa);
765         rsn_preauth_deinit(wpa_s->wpa);
766         wpa_config_free(wpa_s->conf);
767         wpa_s->conf = conf;
768         if (reconf_ctrl)
769                 wpa_supplicant_ctrl_iface_init(wpa_s);
770         wpa_s->reassociate = 1;
771         wpa_supplicant_req_scan(wpa_s, 0, 0);
772         wpa_msg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
773         return 0;
774 }
775
776
777 #ifndef CONFIG_NATIVE_WINDOWS
778 static void wpa_supplicant_reconfig(int sig, void *eloop_ctx,
779                                     void *signal_ctx)
780 {
781         struct wpa_global *global = eloop_ctx;
782         struct wpa_supplicant *wpa_s;
783         wpa_printf(MSG_DEBUG, "Signal %d received - reconfiguring", sig);
784         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
785                 if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
786                         eloop_terminate();
787                 }
788         }
789 }
790 #endif /* CONFIG_NATIVE_WINDOWS */
791
792
793 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
794 {
795         struct wpa_ssid *ssid;
796         union wpa_event_data data;
797
798         ssid = wpa_supplicant_get_ssid(wpa_s);
799         if (ssid == NULL)
800                 return;
801
802         if (wpa_s->current_ssid == NULL)
803                 wpa_s->current_ssid = ssid;
804         wpa_supplicant_initiate_eapol(wpa_s);
805         wpa_printf(MSG_DEBUG, "Already associated with a configured network - "
806                    "generating associated event");
807         memset(&data, 0, sizeof(data));
808         wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
809 }
810
811
812 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
813 {
814         struct wpa_supplicant *wpa_s = eloop_ctx;
815         struct wpa_ssid *ssid;
816         int enabled, scan_req = 0;
817
818         if (wpa_s->disconnected)
819                 return;
820
821         enabled = 0;
822         ssid = wpa_s->conf->ssid;
823         while (ssid) {
824                 if (!ssid->disabled) {
825                         enabled++;
826                         break;
827                 }
828                 ssid = ssid->next;
829         }
830         if (!enabled && !wpa_s->scan_req) {
831                 wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
832                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
833                 return;
834         }
835         scan_req = wpa_s->scan_req;
836         wpa_s->scan_req = 0;
837
838         if (wpa_s->conf->ap_scan == 0) {
839                 wpa_supplicant_gen_assoc_event(wpa_s);
840                 return;
841         }
842
843         if (wpa_s->wpa_state == WPA_DISCONNECTED ||
844             wpa_s->wpa_state == WPA_INACTIVE)
845                 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
846
847         ssid = wpa_s->conf->ssid;
848         if (wpa_s->prev_scan_ssid != BROADCAST_SSID_SCAN) {
849                 while (ssid) {
850                         if (ssid == wpa_s->prev_scan_ssid) {
851                                 ssid = ssid->next;
852                                 break;
853                         }
854                         ssid = ssid->next;
855                 }
856         }
857         while (ssid) {
858                 if (!ssid->disabled &&
859                     (ssid->scan_ssid || wpa_s->conf->ap_scan == 2))
860                         break;
861                 ssid = ssid->next;
862         }
863
864         if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
865                 /*
866                  * ap_scan=2 mode - try to associate with each SSID instead of
867                  * scanning for each scan_ssid=1 network.
868                  */
869                 if (ssid == NULL)
870                         return;
871                 if (ssid->next) {
872                         /* Continue from the next SSID on the next attempt. */
873                         wpa_s->prev_scan_ssid = ssid;
874                 } else {
875                         /* Start from the beginning of the SSID list. */
876                         wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
877                 }
878                 wpa_supplicant_associate(wpa_s, NULL, ssid);
879                 return;
880         }
881
882         wpa_printf(MSG_DEBUG, "Starting AP scan (%s SSID)",
883                    ssid ? "specific": "broadcast");
884         if (ssid) {
885                 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
886                                   ssid->ssid, ssid->ssid_len);
887                 wpa_s->prev_scan_ssid = ssid;
888         } else
889                 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
890
891         if (wpa_drv_scan(wpa_s, ssid ? ssid->ssid : NULL,
892                          ssid ? ssid->ssid_len : 0)) {
893                 wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
894                 wpa_supplicant_req_scan(wpa_s, 10, 0);
895         }
896 }
897
898
899 static wpa_cipher cipher_suite2driver(int cipher)
900 {
901         switch (cipher) {
902         case WPA_CIPHER_NONE:
903                 return CIPHER_NONE;
904         case WPA_CIPHER_WEP40:
905                 return CIPHER_WEP40;
906         case WPA_CIPHER_WEP104:
907                 return CIPHER_WEP104;
908         case WPA_CIPHER_CCMP:
909                 return CIPHER_CCMP;
910         case WPA_CIPHER_TKIP:
911         default:
912                 return CIPHER_TKIP;
913         }
914 }
915
916
917 static wpa_key_mgmt key_mgmt2driver(int key_mgmt)
918 {
919         switch (key_mgmt) {
920         case WPA_KEY_MGMT_NONE:
921                 return KEY_MGMT_NONE;
922         case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
923                 return KEY_MGMT_802_1X_NO_WPA;
924         case WPA_KEY_MGMT_IEEE8021X:
925                 return KEY_MGMT_802_1X;
926         case WPA_KEY_MGMT_WPA_NONE:
927                 return KEY_MGMT_WPA_NONE;
928         case WPA_KEY_MGMT_PSK:
929         default:
930                 return KEY_MGMT_PSK;
931         }
932 }
933
934
935 static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
936                                          struct wpa_ssid *ssid,
937                                          struct wpa_ie_data *ie)
938 {
939         int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
940         if (ret) {
941                 if (ret == -2) {
942                         wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
943                                 "from association info");
944                 }
945                 return -1;
946         }
947
948         wpa_printf(MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set cipher "
949                    "suites");
950         if (!(ie->group_cipher & ssid->group_cipher)) {
951                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
952                         "cipher 0x%x (mask 0x%x) - reject",
953                         ie->group_cipher, ssid->group_cipher);
954                 return -1;
955         }
956         if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
957                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
958                         "cipher 0x%x (mask 0x%x) - reject",
959                         ie->pairwise_cipher, ssid->pairwise_cipher);
960                 return -1;
961         }
962         if (!(ie->key_mgmt & ssid->key_mgmt)) {
963                 wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
964                         "management 0x%x (mask 0x%x) - reject",
965                         ie->key_mgmt, ssid->key_mgmt);
966                 return -1;
967         }
968
969         return 0;
970 }
971
972
973 int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
974                               struct wpa_scan_result *bss,
975                               struct wpa_ssid *ssid,
976                               u8 *wpa_ie, size_t *wpa_ie_len)
977 {
978         struct wpa_ie_data ie;
979         int sel, proto;
980
981         if (bss && bss->rsn_ie_len && (ssid->proto & WPA_PROTO_RSN) &&
982             wpa_parse_wpa_ie(bss->rsn_ie, bss->rsn_ie_len, &ie) == 0 &&
983             (ie.group_cipher & ssid->group_cipher) &&
984             (ie.pairwise_cipher & ssid->pairwise_cipher) &&
985             (ie.key_mgmt & ssid->key_mgmt)) {
986                 wpa_msg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
987                 proto = WPA_PROTO_RSN;
988         } else if (bss && bss->wpa_ie_len && (ssid->proto & WPA_PROTO_WPA) &&
989                    wpa_parse_wpa_ie(bss->wpa_ie, bss->wpa_ie_len, &ie) == 0 &&
990                    (ie.group_cipher & ssid->group_cipher) &&
991                    (ie.pairwise_cipher & ssid->pairwise_cipher) &&
992                    (ie.key_mgmt & ssid->key_mgmt)) {
993                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
994                 proto = WPA_PROTO_WPA;
995         } else if (bss) {
996                 wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
997                 return -1;
998         } else {
999                 if (ssid->proto & WPA_PROTO_RSN)
1000                         proto = WPA_PROTO_RSN;
1001                 else
1002                         proto = WPA_PROTO_WPA;
1003                 if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
1004                         memset(&ie, 0, sizeof(ie));
1005                         ie.group_cipher = ssid->group_cipher;
1006                         ie.pairwise_cipher = ssid->pairwise_cipher;
1007                         ie.key_mgmt = ssid->key_mgmt;
1008                         wpa_printf(MSG_DEBUG, "WPA: Set cipher suites based "
1009                                    "on configuration");
1010                 }
1011         }
1012
1013         wpa_printf(MSG_DEBUG, "WPA: Selected cipher suites: group %d "
1014                    "pairwise %d key_mgmt %d",
1015                    ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt);
1016
1017         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
1018
1019         if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss ? bss->wpa_ie : NULL,
1020                                  bss ? bss->wpa_ie_len : 0) ||
1021             wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss ? bss->rsn_ie : NULL,
1022                                  bss ? bss->rsn_ie_len : 0))
1023                 return -1;
1024
1025         sel = ie.group_cipher & ssid->group_cipher;
1026         if (sel & WPA_CIPHER_CCMP) {
1027                 wpa_s->group_cipher = WPA_CIPHER_CCMP;
1028                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
1029         } else if (sel & WPA_CIPHER_TKIP) {
1030                 wpa_s->group_cipher = WPA_CIPHER_TKIP;
1031                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
1032         } else if (sel & WPA_CIPHER_WEP104) {
1033                 wpa_s->group_cipher = WPA_CIPHER_WEP104;
1034                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
1035         } else if (sel & WPA_CIPHER_WEP40) {
1036                 wpa_s->group_cipher = WPA_CIPHER_WEP40;
1037                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
1038         } else {
1039                 wpa_printf(MSG_WARNING, "WPA: Failed to select group cipher.");
1040                 return -1;
1041         }
1042
1043         sel = ie.pairwise_cipher & ssid->pairwise_cipher;
1044         if (sel & WPA_CIPHER_CCMP) {
1045                 wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
1046                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
1047         } else if (sel & WPA_CIPHER_TKIP) {
1048                 wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
1049                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
1050         } else if (sel & WPA_CIPHER_NONE) {
1051                 wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
1052                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
1053         } else {
1054                 wpa_printf(MSG_WARNING, "WPA: Failed to select pairwise "
1055                            "cipher.");
1056                 return -1;
1057         }
1058
1059         sel = ie.key_mgmt & ssid->key_mgmt;
1060         if (sel & WPA_KEY_MGMT_IEEE8021X) {
1061                 wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1062                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
1063         } else if (sel & WPA_KEY_MGMT_PSK) {
1064                 wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
1065                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
1066         } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
1067                 wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
1068                 wpa_msg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
1069         } else {
1070                 wpa_printf(MSG_WARNING, "WPA: Failed to select authenticated "
1071                            "key management type.");
1072                 return -1;
1073         }
1074
1075         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
1076         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
1077                          wpa_s->pairwise_cipher);
1078         wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
1079
1080         if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
1081                 wpa_printf(MSG_WARNING, "WPA: Failed to generate WPA IE.");
1082                 return -1;
1083         }
1084
1085         if (ssid->key_mgmt & WPA_KEY_MGMT_PSK)
1086                 wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
1087         else
1088                 wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
1089
1090         return 0;
1091 }
1092
1093
1094 void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
1095                               struct wpa_scan_result *bss,
1096                               struct wpa_ssid *ssid)
1097 {
1098         u8 wpa_ie[80];
1099         size_t wpa_ie_len;
1100         int use_crypt;
1101         int algs = AUTH_ALG_OPEN_SYSTEM;
1102         int cipher_pairwise, cipher_group;
1103         struct wpa_driver_associate_params params;
1104         int wep_keys_set = 0;
1105         struct wpa_driver_capa capa;
1106         int assoc_failed = 0;
1107
1108         wpa_s->reassociate = 0;
1109         if (bss) {
1110                 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
1111                         " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
1112                         wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
1113                 memset(wpa_s->bssid, 0, ETH_ALEN);
1114         } else {
1115                 wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
1116                         wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1117         }
1118         wpa_supplicant_cancel_scan(wpa_s);
1119
1120         /* Starting new association, so clear the possibly used WPA IE from the
1121          * previous association. */
1122         wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
1123
1124         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1125                 if (ssid->leap) {
1126                         if (ssid->non_leap == 0)
1127                                 algs = AUTH_ALG_LEAP;
1128                         else
1129                                 algs |= AUTH_ALG_LEAP;
1130                 }
1131         }
1132         wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
1133         if (ssid->auth_alg) {
1134                 algs = 0;
1135                 if (ssid->auth_alg & WPA_AUTH_ALG_OPEN)
1136                         algs |= AUTH_ALG_OPEN_SYSTEM;
1137                 if (ssid->auth_alg & WPA_AUTH_ALG_SHARED)
1138                         algs |= AUTH_ALG_SHARED_KEY;
1139                 if (ssid->auth_alg & WPA_AUTH_ALG_LEAP)
1140                         algs |= AUTH_ALG_LEAP;
1141                 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
1142                            algs);
1143         }
1144         wpa_drv_set_auth_alg(wpa_s, algs);
1145
1146         if (bss && (bss->wpa_ie_len || bss->rsn_ie_len) &&
1147             (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK))) {
1148                 int try_opportunistic;
1149                 try_opportunistic = ssid->proactive_key_caching &&
1150                         (ssid->proto & WPA_PROTO_RSN);
1151                 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
1152                                             wpa_s->current_ssid,
1153                                             try_opportunistic) == 0)
1154                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
1155                 wpa_ie_len = sizeof(wpa_ie);
1156                 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
1157                                               wpa_ie, &wpa_ie_len)) {
1158                         wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1159                                    "management and encryption suites");
1160                         return;
1161                 }
1162         } else if (ssid->key_mgmt &
1163                    (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
1164                     WPA_KEY_MGMT_WPA_NONE)) {
1165                 wpa_ie_len = sizeof(wpa_ie);
1166                 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
1167                                               wpa_ie, &wpa_ie_len)) {
1168                         wpa_printf(MSG_WARNING, "WPA: Failed to set WPA key "
1169                                    "management and encryption suites (no scan "
1170                                    "results)");
1171                         return;
1172                 }
1173         } else {
1174                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
1175                 wpa_ie_len = 0;
1176         }
1177
1178         wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
1179         use_crypt = 1;
1180         cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
1181         cipher_group = cipher_suite2driver(wpa_s->group_cipher);
1182         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
1183             wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1184                 int i;
1185                 if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
1186                         use_crypt = 0;
1187                 for (i = 0; i < NUM_WEP_KEYS; i++) {
1188                         if (ssid->wep_key_len[i]) {
1189                                 use_crypt = 1;
1190                                 wep_keys_set = 1;
1191                                 wpa_set_wep_key(wpa_s,
1192                                                 i == ssid->wep_tx_keyidx,
1193                                                 i, ssid->wep_key[i],
1194                                                 ssid->wep_key_len[i]);
1195                         }
1196                 }
1197         }
1198
1199         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
1200                 if ((ssid->eapol_flags &
1201                      (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
1202                       EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
1203                     !wep_keys_set) {
1204                         use_crypt = 0;
1205                 } else {
1206                         /* Assume that dynamic WEP-104 keys will be used and
1207                          * set cipher suites in order for drivers to expect
1208                          * encryption. */
1209                         cipher_pairwise = cipher_group = CIPHER_WEP104;
1210                 }
1211         }
1212
1213         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1214                 /* Set the key before (and later after) association */
1215                 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1216         }
1217
1218         wpa_drv_set_drop_unencrypted(wpa_s, use_crypt);
1219         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
1220         memset(&params, 0, sizeof(params));
1221         if (bss) {
1222                 params.bssid = bss->bssid;
1223                 params.ssid = bss->ssid;
1224                 params.ssid_len = bss->ssid_len;
1225                 params.freq = bss->freq;
1226         } else {
1227                 params.ssid = ssid->ssid;
1228                 params.ssid_len = ssid->ssid_len;
1229         }
1230         params.wpa_ie = wpa_ie;
1231         params.wpa_ie_len = wpa_ie_len;
1232         params.pairwise_suite = cipher_pairwise;
1233         params.group_suite = cipher_group;
1234         params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
1235         params.auth_alg = algs;
1236         params.mode = ssid->mode;
1237         if (wpa_drv_associate(wpa_s, &params) < 0) {
1238                 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
1239                         "failed");
1240                 /* try to continue anyway; new association will be tried again
1241                  * after timeout */
1242                 assoc_failed = 1;
1243         }
1244
1245         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
1246                 /* Set the key after the association just in case association
1247                  * cleared the previously configured key. */
1248                 wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
1249                 /* No need to timeout authentication since there is no key
1250                  * management. */
1251                 wpa_supplicant_cancel_auth_timeout(wpa_s);
1252                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
1253         } else {
1254                 /* Timeout for IEEE 802.11 authentication and association */
1255                 int timeout;
1256                 if (assoc_failed)
1257                         timeout = 5;
1258                 else if (wpa_s->conf->ap_scan == 1)
1259                         timeout = 10;
1260                 else
1261                         timeout = 60;
1262                 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
1263         }
1264
1265         if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
1266             capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
1267                 /* Set static WEP keys again */
1268                 int i;
1269                 for (i = 0; i < NUM_WEP_KEYS; i++) {
1270                         if (ssid->wep_key_len[i]) {
1271                                 wpa_set_wep_key(wpa_s,
1272                                                 i == ssid->wep_tx_keyidx,
1273                                                 i, ssid->wep_key[i],
1274                                                 ssid->wep_key_len[i]);
1275                         }
1276                 }
1277         }
1278
1279         wpa_s->current_ssid = ssid;
1280         wpa_sm_set_config(wpa_s->wpa, wpa_s->current_ssid);
1281         wpa_supplicant_initiate_eapol(wpa_s);
1282 }
1283
1284
1285 void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
1286                                  int reason_code)
1287 {
1288         u8 *addr = NULL;
1289         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1290         if (memcmp(wpa_s->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) != 0) {
1291                 wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
1292                 addr = wpa_s->bssid;
1293         }
1294         wpa_clear_keys(wpa_s, addr);
1295         wpa_s->current_ssid = NULL;
1296         wpa_sm_set_config(wpa_s->wpa, NULL);
1297         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1298         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1299         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1300 }
1301
1302
1303 void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
1304                                    int reason_code)
1305 {
1306         u8 *addr = NULL;
1307         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
1308         if (memcmp(wpa_s->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) != 0) {
1309                 wpa_drv_deauthenticate(wpa_s, wpa_s->bssid, reason_code);
1310                 addr = wpa_s->bssid;
1311         }
1312         wpa_clear_keys(wpa_s, addr);
1313         wpa_s->current_ssid = NULL;
1314         wpa_sm_set_config(wpa_s->wpa, NULL);
1315         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
1316         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1317         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1318 }
1319
1320
1321 int wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s)
1322 {
1323 #define SCAN_AP_LIMIT 128
1324         struct wpa_scan_result *results, *tmp;
1325         int num;
1326
1327         results = malloc(SCAN_AP_LIMIT * sizeof(struct wpa_scan_result));
1328         if (results == NULL) {
1329                 wpa_printf(MSG_WARNING, "Failed to allocate memory for scan "
1330                            "results");
1331                 return -1;
1332         }
1333
1334         num = wpa_drv_get_scan_results(wpa_s, results, SCAN_AP_LIMIT);
1335         wpa_printf(MSG_DEBUG, "Scan results: %d", num);
1336         if (num < 0) {
1337                 wpa_printf(MSG_DEBUG, "Failed to get scan results");
1338                 free(results);
1339                 return -1;
1340         }
1341         if (num > SCAN_AP_LIMIT) {
1342                 wpa_printf(MSG_INFO, "Not enough room for all APs (%d < %d)",
1343                            num, SCAN_AP_LIMIT);
1344                 num = SCAN_AP_LIMIT;
1345         }
1346
1347         /* Free unneeded memory for unused scan result entries */
1348         tmp = realloc(results, num * sizeof(struct wpa_scan_result));
1349         if (tmp || num == 0) {
1350                 results = tmp;
1351         }
1352
1353         free(wpa_s->scan_results);
1354         wpa_s->scan_results = results;
1355         wpa_s->num_scan_results = num;
1356
1357         return 0;
1358 }
1359
1360
1361 #ifndef CONFIG_NO_WPA
1362 static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
1363 {
1364         int i, ret = 0;
1365         struct wpa_scan_result *results, *curr = NULL;
1366
1367         results = wpa_s->scan_results;
1368         if (results == NULL) {
1369                 return -1;
1370         }
1371
1372         for (i = 0; i < wpa_s->num_scan_results; i++) {
1373                 if (memcmp(results[i].bssid, wpa_s->bssid, ETH_ALEN) == 0) {
1374                         curr = &results[i];
1375                         break;
1376                 }
1377         }
1378
1379         if (curr) {
1380                 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, curr->wpa_ie,
1381                                          curr->wpa_ie_len) ||
1382                     wpa_sm_set_ap_rsn_ie(wpa_s->wpa, curr->rsn_ie,
1383                                          curr->rsn_ie_len))
1384                         ret = -1;
1385         } else {
1386                 ret = -1;
1387         }
1388
1389         return ret;
1390 }
1391
1392
1393 static int wpa_supplicant_get_beacon_ie(void *ctx)
1394 {
1395         struct wpa_supplicant *wpa_s = ctx;
1396         if (wpa_get_beacon_ie(wpa_s) == 0) {
1397                 return 0;
1398         }
1399
1400         /* No WPA/RSN IE found in the cached scan results. Try to get updated
1401          * scan results from the driver. */
1402         if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
1403                 return -1;
1404         }
1405
1406         return wpa_get_beacon_ie(wpa_s);
1407 }
1408 #endif /* CONFIG_NO_WPA */
1409
1410
1411 /**
1412  * wpa_supplicant_get_ssid - get a pointer to the current network structure
1413  * @wpa_s: pointer to wpa_supplicant data
1414  *
1415  * Returns: a pointer to the current network structure or %NULL on failure
1416  */
1417 struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
1418 {
1419         struct wpa_ssid *entry;
1420         u8 ssid[MAX_SSID_LEN];
1421         int ssid_len;
1422         u8 bssid[ETH_ALEN];
1423
1424         ssid_len = wpa_drv_get_ssid(wpa_s, ssid);
1425         if (ssid_len < 0) {
1426                 wpa_printf(MSG_WARNING, "Could not read SSID from driver.");
1427                 return NULL;
1428         }
1429
1430         if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
1431                 wpa_printf(MSG_WARNING, "Could not read BSSID from driver.");
1432                 return NULL;
1433         }
1434
1435         entry = wpa_s->conf->ssid;
1436         while (entry) {
1437                 if (!entry->disabled &&
1438                     ssid_len == entry->ssid_len &&
1439                     memcmp(ssid, entry->ssid, ssid_len) == 0 &&
1440                     (!entry->bssid_set ||
1441                      memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
1442                         return entry;
1443                 entry = entry->next;
1444         }
1445
1446         return NULL;
1447 }
1448
1449
1450 #ifndef CONFIG_NO_WPA
1451 static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
1452                              const void *data, u16 data_len,
1453                              size_t *msg_len, void **data_pos)
1454 {
1455         return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
1456 }
1457
1458
1459 static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
1460                            const u8 *buf, size_t len)
1461 {
1462         return wpa_ether_send(wpa_s, dest, proto, buf, len);
1463 }
1464
1465
1466 static void _wpa_supplicant_req_scan(void *wpa_s, int sec, int usec)
1467 {
1468         wpa_supplicant_req_scan(wpa_s, sec, usec);
1469 }
1470
1471
1472 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
1473 {
1474         wpa_supplicant_cancel_auth_timeout(wpa_s);
1475 }
1476
1477
1478 static void _wpa_supplicant_set_state(void *wpa_s, wpa_states state)
1479 {
1480         wpa_supplicant_set_state(wpa_s, state);
1481 }
1482
1483
1484 static wpa_states _wpa_supplicant_get_state(void *wpa_s)
1485 {
1486         return wpa_supplicant_get_state(wpa_s);
1487 }
1488
1489
1490 static void _wpa_supplicant_disassociate(void *wpa_s, int reason_code)
1491 {
1492         wpa_supplicant_disassociate(wpa_s, reason_code);
1493 }
1494
1495
1496 static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
1497 {
1498         wpa_supplicant_deauthenticate(wpa_s, reason_code);
1499 }
1500
1501
1502 static struct wpa_ssid * _wpa_supplicant_get_ssid(void *wpa_s)
1503 {
1504         return wpa_supplicant_get_ssid(wpa_s);
1505 }
1506
1507
1508 static int wpa_supplicant_get_bssid(void *wpa_s, u8 *bssid)
1509 {
1510         return wpa_drv_get_bssid(wpa_s, bssid);
1511 }
1512
1513
1514 static int wpa_supplicant_set_key(void *wpa_s, wpa_alg alg,
1515                                   const u8 *addr, int key_idx, int set_tx,
1516                                   const u8 *seq, size_t seq_len,
1517                                   const u8 *key, size_t key_len)
1518 {
1519         return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
1520                                key, key_len);
1521 }
1522
1523
1524 static int wpa_supplicant_add_pmkid(void *wpa_s,
1525                                     const u8 *bssid, const u8 *pmkid)
1526 {
1527         return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
1528 }
1529
1530
1531 static int wpa_supplicant_remove_pmkid(void *wpa_s,
1532                                        const u8 *bssid, const u8 *pmkid)
1533 {
1534         return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
1535 }
1536 #endif /* CONFIG_NO_WPA */
1537
1538
1539 static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
1540                                      const char *name)
1541 {
1542         int i;
1543
1544         if (wpa_s == NULL)
1545                 return -1;
1546
1547         if (wpa_supplicant_drivers[0] == NULL) {
1548                 wpa_printf(MSG_ERROR, "No driver interfaces build into "
1549                            "wpa_supplicant.");
1550                 return -1;
1551         }
1552
1553         if (name == NULL) {
1554                 /* default to first driver in the list */
1555                 wpa_s->driver = wpa_supplicant_drivers[0];
1556                 return 0;
1557         }
1558
1559         for (i = 0; wpa_supplicant_drivers[i]; i++) {
1560                 if (strcmp(name, wpa_supplicant_drivers[i]->name) == 0) {
1561                         wpa_s->driver = wpa_supplicant_drivers[i];
1562                         return 0;
1563                 }
1564         }
1565
1566         printf("Unsupported driver '%s'.\n", name);
1567         return -1;
1568 }
1569
1570
1571 void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
1572                              const u8 *buf, size_t len)
1573 {
1574         struct wpa_supplicant *wpa_s = ctx;
1575
1576         wpa_printf(MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
1577         wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
1578
1579         if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
1580                 wpa_printf(MSG_DEBUG, "Ignored received EAPOL frame since "
1581                            "no key management is configured");
1582                 return;
1583         }
1584
1585         if (wpa_s->eapol_received == 0) {
1586                 /* Timeout for completing IEEE 802.1X and WPA authentication */
1587                 wpa_supplicant_req_auth_timeout(
1588                         wpa_s,
1589                         (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
1590                          wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) ?
1591                         70 : 10, 0);
1592         }
1593         wpa_s->eapol_received++;
1594
1595         if (wpa_s->countermeasures) {
1596                 wpa_printf(MSG_INFO, "WPA: Countermeasures - dropped EAPOL "
1597                            "packet");
1598                 return;
1599         }
1600
1601         /* Source address of the incoming EAPOL frame could be compared to the
1602          * current BSSID. However, it is possible that a centralized
1603          * Authenticator could be using another MAC address than the BSSID of
1604          * an AP, so just allow any address to be used for now. The replies are
1605          * still sent to the current BSSID (if available), though. */
1606
1607         memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
1608         if (wpa_s->key_mgmt != WPA_KEY_MGMT_PSK &&
1609             eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
1610                 return;
1611         wpa_drv_poll(wpa_s);
1612         wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
1613 }
1614
1615
1616 int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s,
1617                                int wait_for_interface)
1618 {
1619         static int interface_count = 0;
1620
1621         for (;;) {
1622                 if (wpa_s->driver->send_eapol) {
1623                         const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
1624                         if (addr)
1625                                 memcpy(wpa_s->own_addr, addr, ETH_ALEN);
1626                         break;
1627                 }
1628                 wpa_s->l2 = l2_packet_init(wpa_s->ifname,
1629                                            wpa_drv_get_mac_addr(wpa_s),
1630                                            ETH_P_EAPOL,
1631                                            wpa_supplicant_rx_eapol, wpa_s, 0);
1632                 if (wpa_s->l2)
1633                         break;
1634                 else if (!wait_for_interface)
1635                         return -1;
1636                 printf("Waiting for interface..\n");
1637                 sleep(5);
1638         }
1639
1640         if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
1641                 fprintf(stderr, "Failed to get own L2 address\n");
1642                 return -1;
1643         }
1644
1645         wpa_printf(MSG_DEBUG, "Own MAC address: " MACSTR,
1646                    MAC2STR(wpa_s->own_addr));
1647
1648         /* Backwards compatibility call to set_wpa() handler. This is called
1649          * only just after init and just before deinit, so these handler can be
1650          * used to implement same functionality. */
1651         if (wpa_drv_set_wpa(wpa_s, 1) < 0) {
1652                 struct wpa_driver_capa capa;
1653                 if (wpa_drv_get_capa(wpa_s, &capa) < 0 ||
1654                     !(capa.flags & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1655                                     WPA_DRIVER_CAPA_KEY_MGMT_WPA2))) {
1656                         wpa_printf(MSG_DEBUG, "Driver does not support WPA.");
1657                         /* Continue to allow non-WPA modes to be used. */
1658                 } else {
1659                         fprintf(stderr, "Failed to enable WPA in the "
1660                                 "driver.\n");
1661                         return -1;
1662                 }
1663         }
1664
1665         wpa_clear_keys(wpa_s, NULL);
1666
1667         /* Make sure that TKIP countermeasures are not left enabled (could
1668          * happen if wpa_supplicant is killed during countermeasures. */
1669         wpa_drv_set_countermeasures(wpa_s, 0);
1670
1671         wpa_drv_set_drop_unencrypted(wpa_s, 1);
1672
1673         wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
1674         wpa_supplicant_req_scan(wpa_s, interface_count, 100000);
1675         interface_count++;
1676
1677         return 0;
1678 }
1679
1680
1681 static int wpa_supplicant_daemon(const char *pid_file)
1682 {
1683         wpa_printf(MSG_DEBUG, "Daemonize..");
1684         if (daemon(0, 0)) {
1685                 perror("daemon");
1686                 return -1;
1687         }
1688
1689         if (pid_file) {
1690                 FILE *f = fopen(pid_file, "w");
1691                 if (f) {
1692                         fprintf(f, "%u\n", getpid());
1693                         fclose(f);
1694                 }
1695         }
1696
1697         return 0;
1698 }
1699
1700
1701 static struct wpa_supplicant * wpa_supplicant_alloc(void)
1702 {
1703         struct wpa_supplicant *wpa_s;
1704
1705         wpa_s = malloc(sizeof(*wpa_s));
1706         if (wpa_s == NULL)
1707                 return NULL;
1708         memset(wpa_s, 0, sizeof(*wpa_s));
1709         wpa_s->ctrl_sock = -1;
1710         wpa_s->scan_req = 1;
1711
1712         return wpa_s;
1713 }
1714
1715
1716 static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
1717                                      struct wpa_interface *iface)
1718 {
1719         wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
1720                    "'%s' ctrl_interface '%s'", iface->ifname,
1721                    iface->confname ? iface->confname : "N/A",
1722                    iface->driver ? iface->driver : "default",
1723                    iface->ctrl_interface ? iface->ctrl_interface : "N/A");
1724
1725         if (wpa_supplicant_set_driver(wpa_s, iface->driver) < 0) {
1726                 return -1;
1727         }
1728
1729         if (iface->confname) {
1730 #ifdef CONFIG_BACKEND_FILE
1731                 wpa_s->confname = rel2abs_path(iface->confname);
1732                 if (wpa_s->confname == NULL) {
1733                         wpa_printf(MSG_ERROR, "Failed to get absolute path "
1734                                    "for configuration file '%s'.",
1735                                    iface->confname);
1736                         return -1;
1737                 }
1738                 wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
1739                            iface->confname, wpa_s->confname);
1740 #else /* CONFIG_BACKEND_FILE */
1741                 wpa_s->confname = strdup(iface->confname);
1742 #endif /* CONFIG_BACKEND_FILE */
1743                 wpa_s->conf = wpa_config_read(wpa_s->confname);
1744                 if (wpa_s->conf == NULL) {
1745                         printf("Failed to read read or parse configuration "
1746                                "'%s'.\n", wpa_s->confname);
1747                         return -1;
1748                 }
1749
1750                 /*
1751                  * Override ctrl_interface and driver_param if set on command
1752                  * line.
1753                  */
1754                 if (iface->ctrl_interface) {
1755                         free(wpa_s->conf->ctrl_interface);
1756                         wpa_s->conf->ctrl_interface =
1757                                 strdup(iface->ctrl_interface);
1758                 }
1759
1760                 if (iface->driver_param) {
1761                         free(wpa_s->conf->driver_param);
1762                         wpa_s->conf->driver_param =
1763                                 strdup(iface->driver_param);
1764                 }
1765         } else
1766                 wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
1767                                                      iface->driver_param);
1768
1769         if (wpa_s->conf == NULL) {
1770                 printf("\nNo configuration found.\n");
1771                 return -1;
1772         }
1773
1774         if (iface->ifname == NULL) {
1775                 printf("\nInterface name is required.\n");
1776                 return -1;
1777         }
1778         if (strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
1779                 printf("Too long interface name '%s'.\n", iface->ifname);
1780                 return -1;
1781         }
1782         strncpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
1783
1784         return 0;
1785 }
1786
1787
1788 static int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
1789 {
1790 #ifdef IEEE8021X_EAPOL
1791         struct eapol_ctx *ctx;
1792         ctx = malloc(sizeof(*ctx));
1793         if (ctx == NULL) {
1794                 printf("Failed to allocate EAPOL context.\n");
1795                 return -1;
1796         }
1797
1798         memset(ctx, 0, sizeof(*ctx));
1799         ctx->ctx = wpa_s;
1800         ctx->msg_ctx = wpa_s;
1801         ctx->eapol_send_ctx = wpa_s;
1802         ctx->preauth = 0;
1803         ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
1804         ctx->eapol_send = wpa_supplicant_eapol_send;
1805         ctx->set_wep_key = wpa_eapol_set_wep_key;
1806         ctx->set_config_blob = wpa_supplicant_set_config_blob;
1807         ctx->get_config_blob = wpa_supplicant_get_config_blob;
1808         ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
1809         ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
1810         ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
1811         wpa_s->eapol = eapol_sm_init(ctx);
1812         if (wpa_s->eapol == NULL) {
1813                 free(ctx);
1814                 printf("Failed to initialize EAPOL state machines.\n");
1815                 return -1;
1816         }
1817 #endif /* IEEE8021X_EAPOL */
1818
1819         return 0;
1820 }
1821
1822
1823 static int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
1824 {
1825 #ifndef CONFIG_NO_WPA
1826         struct wpa_sm_ctx *ctx;
1827         ctx = malloc(sizeof(*ctx));
1828         if (ctx == NULL) {
1829                 printf("Failed to allocate WPA context.\n");
1830                 return -1;
1831         }
1832
1833         memset(ctx, 0, sizeof(*ctx));
1834         ctx->ctx = wpa_s;
1835         ctx->set_state = _wpa_supplicant_set_state;
1836         ctx->get_state = _wpa_supplicant_get_state;
1837         ctx->req_scan = _wpa_supplicant_req_scan;
1838         ctx->deauthenticate = _wpa_supplicant_deauthenticate;
1839         ctx->disassociate = _wpa_supplicant_disassociate;
1840         ctx->set_key = wpa_supplicant_set_key;
1841         ctx->scan = wpa_supplicant_scan;
1842         ctx->get_ssid = _wpa_supplicant_get_ssid;
1843         ctx->get_bssid = wpa_supplicant_get_bssid;
1844         ctx->ether_send = _wpa_ether_send;
1845         ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
1846         ctx->alloc_eapol = _wpa_alloc_eapol;
1847         ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
1848         ctx->add_pmkid = wpa_supplicant_add_pmkid;
1849         ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
1850         ctx->set_config_blob = wpa_supplicant_set_config_blob;
1851         ctx->get_config_blob = wpa_supplicant_get_config_blob;
1852
1853         wpa_s->wpa = wpa_sm_init(ctx);
1854         if (wpa_s->wpa == NULL) {
1855                 fprintf(stderr, "Failed to initialize WPA state machine\n");
1856                 return -1;
1857         }
1858 #endif /* CONFIG_NO_WPA */
1859
1860         return 0;
1861 }
1862
1863
1864 static int wpa_supplicant_init_iface2(struct wpa_supplicant *wpa_s,
1865                                       int wait_for_interface)
1866 {
1867         const char *ifname;
1868
1869         wpa_printf(MSG_DEBUG, "Initializing interface (2) '%s'",
1870                    wpa_s->ifname);
1871
1872         if (wpa_supplicant_init_eapol(wpa_s) < 0)
1873                 return -1;
1874
1875         /* RSNA Supplicant Key Management - INITIALIZE */
1876         eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
1877         eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
1878
1879         /* Initialize driver interface and register driver event handler before
1880          * L2 receive handler so that association events are processed before
1881          * EAPOL-Key packets if both become available for the same select()
1882          * call. */
1883         wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
1884         if (wpa_s->drv_priv == NULL) {
1885                 fprintf(stderr, "Failed to initialize driver interface\n");
1886                 return -1;
1887         }
1888         if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
1889                 fprintf(stderr, "Driver interface rejected driver_param "
1890                         "'%s'\n", wpa_s->conf->driver_param);
1891                 return -1;
1892         }
1893
1894         ifname = wpa_drv_get_ifname(wpa_s);
1895         if (ifname && strcmp(ifname, wpa_s->ifname) != 0) {
1896                 wpa_printf(MSG_DEBUG, "Driver interface replaced interface "
1897                            "name with '%s'", ifname);
1898                 strncpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
1899         }
1900
1901         if (wpa_supplicant_init_wpa(wpa_s) < 0)
1902                 return -1;
1903
1904         wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname);
1905         wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
1906         wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
1907
1908         if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
1909             wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
1910                              wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
1911                 fprintf(stderr, "Invalid WPA parameter value for "
1912                         "dot11RSNAConfigPMKLifetime\n");
1913                 return -1;
1914         }
1915
1916         if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
1917             wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
1918                              wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
1919                 fprintf(stderr, "Invalid WPA parameter value for "
1920                         "dot11RSNAConfigPMKReauthThreshold\n");
1921                 return -1;
1922         }
1923
1924         if (wpa_s->conf->dot11RSNAConfigSATimeout &&
1925             wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
1926                              wpa_s->conf->dot11RSNAConfigSATimeout)) {
1927                 fprintf(stderr, "Invalid WPA parameter value for "
1928                         "dot11RSNAConfigSATimeout\n");
1929                 return -1;
1930         }
1931
1932         if (wpa_supplicant_driver_init(wpa_s, wait_for_interface) < 0) {
1933                 return -1;
1934         }
1935         wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
1936
1937         if (wpa_supplicant_ctrl_iface_init(wpa_s)) {
1938                 printf("Failed to initialize control interface '%s'.\n"
1939                        "You may have another wpa_supplicant process already "
1940                        "running or the file was\n"
1941                        "left by an unclean termination of wpa_supplicant in "
1942                        "which case you will need\n"
1943                        "to manually remove this file before starting "
1944                        "wpa_supplicant again.\n",
1945                        wpa_s->conf->ctrl_interface);
1946                 return -1;
1947         }
1948
1949         return 0;
1950 }
1951
1952
1953 static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s)
1954 {
1955         if (wpa_s->drv_priv) {
1956                 wpa_supplicant_deauthenticate(wpa_s, REASON_DEAUTH_LEAVING);
1957
1958                 /* Backwards compatibility call to set_wpa() handler. This is
1959                  * called only just after init and just before deinit, so these
1960                  * handler can be used to implement same functionality. */
1961                 if (wpa_drv_set_wpa(wpa_s, 0) < 0) {
1962                         fprintf(stderr, "Failed to disable WPA in the "
1963                                 "driver.\n");
1964                 }
1965
1966                 wpa_drv_set_drop_unencrypted(wpa_s, 0);
1967                 wpa_drv_set_countermeasures(wpa_s, 0);
1968                 wpa_clear_keys(wpa_s, NULL);
1969
1970                 wpa_drv_deinit(wpa_s);
1971         }
1972         wpa_supplicant_cleanup(wpa_s);
1973 }
1974
1975
1976 /**
1977  * wpa_supplicant_add_iface - Add a new network interface
1978  * @global: Pointer to global data from wpa_supplicant_init()
1979  * @iface: Interface configuration options
1980  * Returns: Pointer to the created interface or %NULL on failure
1981  *
1982  * This function is used to add new network interfaces for %wpa_supplicant.
1983  * This can be called before wpa_supplicant_run() to add interfaces before the
1984  * main event loop has been started. In addition, new interfaces can be added
1985  * dynamically while %wpa_supplicant is already running. This could happen,
1986  * e.g., when a hotplug network adapter is inserted.
1987  */
1988 struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
1989                                                  struct wpa_interface *iface)
1990 {
1991         struct wpa_supplicant *wpa_s;
1992
1993         if (global == NULL || iface == NULL)
1994                 return NULL;
1995
1996         wpa_s = wpa_supplicant_alloc();
1997         if (wpa_s == NULL)
1998                 return NULL;
1999
2000         if (wpa_supplicant_init_iface(wpa_s, iface) ||
2001             wpa_supplicant_init_iface2(wpa_s,
2002                                        global->params.wait_for_interface)) {
2003                 wpa_printf(MSG_DEBUG, "Failed to add interface %s",
2004                            iface->ifname);
2005                 wpa_supplicant_deinit_iface(wpa_s);
2006                 free(wpa_s);
2007                 return NULL;
2008         }
2009
2010         wpa_s->global = global;
2011         wpa_s->next = global->ifaces;
2012         global->ifaces = wpa_s;
2013
2014         wpa_printf(MSG_DEBUG, "Added interface %s", wpa_s->ifname);
2015
2016         return wpa_s;
2017 }
2018
2019
2020 /**
2021  * wpa_supplicant_remove_iface - Remove a network interface
2022  * @global: Pointer to global data from wpa_supplicant_init()
2023  * @wpa_s: Pointer to the network interface to be removed
2024  * Returns: 0 if interface was removed, -1 if interface was not found
2025  *
2026  * This function can be used to dynamically remove network interfaces from
2027  * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
2028  * addition, this function is used to remove all remaining interdaces when
2029  * %wpa_supplicant is terminated.
2030  */
2031 int wpa_supplicant_remove_iface(struct wpa_global *global,
2032                                 struct wpa_supplicant *wpa_s)
2033 {
2034         struct wpa_supplicant *prev;
2035
2036         /* Remove interface from the global list of interfaces */
2037         prev = global->ifaces;
2038         if (prev == wpa_s) {
2039                 global->ifaces = wpa_s->next;
2040         } else {
2041                 while (prev && prev->next != wpa_s)
2042                         prev = prev->next;
2043                 if (prev == NULL)
2044                         return -1;
2045                 prev->next = wpa_s->next;
2046         }
2047
2048         wpa_printf(MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
2049
2050         wpa_supplicant_deinit_iface(wpa_s);
2051         free(wpa_s);
2052
2053         return 0;
2054 }
2055
2056
2057 /**
2058  * wpa_supplicant_get_iface - Get a new network interface
2059  * @global: Pointer to global data from wpa_supplicant_init()
2060  * @ifname: Interface name
2061  * Returns: Pointer to the interface or %NULL if not found
2062  */
2063 struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
2064                                                  const char *ifname)
2065 {
2066         struct wpa_supplicant *wpa_s;
2067
2068         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2069                 if (strcmp(wpa_s->ifname, ifname) == 0)
2070                         return wpa_s;
2071         }
2072         return NULL;
2073 }
2074
2075
2076 /**
2077  * wpa_supplicant_init - Initialize %wpa_supplicant
2078  * @params: Parameters for %wpa_supplicant
2079  * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
2080  *
2081  * This function is used to initialize %wpa_supplicant. After successful
2082  * initialization, the returned data pointer can be used to add and remove
2083  * network interfaces, and eventually, to deinitialize %wpa_supplicant.
2084  */
2085 struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
2086 {
2087         struct wpa_global *global;
2088
2089         if (params == NULL)
2090                 return NULL;
2091         global = malloc(sizeof(*global));
2092         if (global == NULL)
2093                 return NULL;
2094         memset(global, 0, sizeof(*global));
2095         global->params.daemonize = params->daemonize;
2096         global->params.wait_for_interface = params->wait_for_interface;
2097         global->params.wait_for_monitor = params->wait_for_monitor;
2098         if (params->pid_file)
2099                 global->params.pid_file = strdup(params->pid_file);
2100         if (params->ctrl_interface)
2101                 global->params.ctrl_interface = strdup(params->ctrl_interface);
2102         wpa_debug_level = global->params.wpa_debug_level =
2103                 params->wpa_debug_level;
2104         wpa_debug_show_keys = global->params.wpa_debug_show_keys =
2105                 params->wpa_debug_show_keys;
2106         wpa_debug_timestamp = global->params.wpa_debug_timestamp =
2107                 params->wpa_debug_timestamp;
2108
2109         eloop_init(global);
2110
2111         if (wpa_supplicant_global_ctrl_iface_init(global)) {
2112                 eloop_destroy();
2113                 return NULL;
2114         }
2115
2116         if (global->params.wait_for_interface && global->params.daemonize &&
2117             wpa_supplicant_daemon(global->params.pid_file)) {
2118                 eloop_destroy();
2119                 return NULL;
2120         }
2121
2122         return global;
2123 }
2124
2125
2126 /**
2127  * wpa_supplicant_run - Run the %wpa_supplicant main event loop
2128  * @global: Pointer to global data from wpa_supplicant_init()
2129  * Returns: 0 after successful event loop run, -1 on failure
2130  *
2131  * This function starts the main event loop and continues running as long as
2132  * there are any remaining events. In most cases, this function is running as
2133  * long as the %wpa_supplicant process in still in use.
2134  */
2135 int wpa_supplicant_run(struct wpa_global *global)
2136 {
2137         struct wpa_supplicant *wpa_s;
2138
2139         if (!global->params.wait_for_interface && global->params.daemonize &&
2140             wpa_supplicant_daemon(global->params.pid_file))
2141                 return -1;
2142
2143         if (global->params.wait_for_monitor) {
2144                 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
2145                         wpa_supplicant_ctrl_iface_wait(wpa_s);
2146         }
2147
2148         eloop_register_signal(SIGINT, wpa_supplicant_terminate, NULL);
2149         eloop_register_signal(SIGTERM, wpa_supplicant_terminate, NULL);
2150 #ifndef CONFIG_NATIVE_WINDOWS
2151         eloop_register_signal(SIGHUP, wpa_supplicant_reconfig, NULL);
2152 #endif /* CONFIG_NATIVE_WINDOWS */
2153
2154         eloop_run();
2155
2156         return 0;
2157 }
2158
2159
2160 /**
2161  * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
2162  * @global: Pointer to global data from wpa_supplicant_init()
2163  *
2164  * This function is called to deinitialize %wpa_supplicant and to free all
2165  * allocated resources. Remaining network interfaces will also be removed.
2166  */
2167 void wpa_supplicant_deinit(struct wpa_global *global)
2168 {
2169         if (global == NULL)
2170                 return;
2171
2172         while (global->ifaces)
2173                 wpa_supplicant_remove_iface(global, global->ifaces);
2174
2175         wpa_supplicant_global_ctrl_iface_deinit(global);
2176
2177         eloop_destroy();
2178
2179         if (global->params.pid_file) {
2180                 unlink(global->params.pid_file);
2181                 free(global->params.pid_file);
2182         }
2183         free(global->params.ctrl_interface);
2184
2185         free(global);
2186 }