WPS: Moved UUID configuration from phase1 into global config area
[libeap.git] / wpa_supplicant / wpas_glue.c
1 /*
2  * WPA Supplicant - Glue code to setup EAPOL and RSN modules
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
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eapol_supp/eapol_supp_sm.h"
19 #include "wpa.h"
20 #include "eloop.h"
21 #include "config.h"
22 #include "l2_packet/l2_packet.h"
23 #include "wpa_common.h"
24 #include "wpa_supplicant_i.h"
25 #include "pmksa_cache.h"
26 #include "mlme.h"
27 #include "ieee802_11_defs.h"
28 #include "wps/wps.h"
29 #include "wps/wps_defs.h"
30 #include "wpa_ctrl.h"
31 #include "wpas_glue.h"
32
33
34 #ifndef CONFIG_NO_CONFIG_BLOBS
35 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
36 static void wpa_supplicant_set_config_blob(void *ctx,
37                                            struct wpa_config_blob *blob)
38 {
39         struct wpa_supplicant *wpa_s = ctx;
40         wpa_config_set_blob(wpa_s->conf, blob);
41         if (wpa_s->conf->update_config) {
42                 int ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
43                 if (ret) {
44                         wpa_printf(MSG_DEBUG, "Failed to update config after "
45                                    "blob set");
46                 }
47         }
48 }
49
50
51 static const struct wpa_config_blob *
52 wpa_supplicant_get_config_blob(void *ctx, const char *name)
53 {
54         struct wpa_supplicant *wpa_s = ctx;
55         return wpa_config_get_blob(wpa_s->conf, name);
56 }
57 #endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
58 #endif /* CONFIG_NO_CONFIG_BLOBS */
59
60
61 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
62 static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
63                             const void *data, u16 data_len,
64                             size_t *msg_len, void **data_pos)
65 {
66         struct ieee802_1x_hdr *hdr;
67
68         *msg_len = sizeof(*hdr) + data_len;
69         hdr = os_malloc(*msg_len);
70         if (hdr == NULL)
71                 return NULL;
72
73         hdr->version = wpa_s->conf->eapol_version;
74         hdr->type = type;
75         hdr->length = host_to_be16(data_len);
76
77         if (data)
78                 os_memcpy(hdr + 1, data, data_len);
79         else
80                 os_memset(hdr + 1, 0, data_len);
81
82         if (data_pos)
83                 *data_pos = hdr + 1;
84
85         return (u8 *) hdr;
86 }
87
88
89 /**
90  * wpa_ether_send - Send Ethernet frame
91  * @wpa_s: Pointer to wpa_supplicant data
92  * @dest: Destination MAC address
93  * @proto: Ethertype in host byte order
94  * @buf: Frame payload starting from IEEE 802.1X header
95  * @len: Frame payload length
96  * Returns: >=0 on success, <0 on failure
97  */
98 static int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest,
99                           u16 proto, const u8 *buf, size_t len)
100 {
101         if (wpa_s->l2) {
102                 return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
103         }
104
105         return wpa_drv_send_eapol(wpa_s, dest, proto, buf, len);
106 }
107 #endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
108
109
110 #ifdef IEEE8021X_EAPOL
111
112 /**
113  * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
114  * @ctx: Pointer to wpa_supplicant data (wpa_s)
115  * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
116  * @buf: EAPOL payload (after IEEE 802.1X header)
117  * @len: EAPOL payload length
118  * Returns: >=0 on success, <0 on failure
119  *
120  * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
121  * to the current Authenticator.
122  */
123 static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
124                                      size_t len)
125 {
126         struct wpa_supplicant *wpa_s = ctx;
127         u8 *msg, *dst, bssid[ETH_ALEN];
128         size_t msglen;
129         int res;
130
131         /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
132          * extra copy here */
133
134         if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
135             wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
136                 /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
137                  * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
138                  * machines. */
139                 wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
140                            "mode (type=%d len=%lu)", type,
141                            (unsigned long) len);
142                 return -1;
143         }
144
145         if (pmksa_cache_get_current(wpa_s->wpa) &&
146             type == IEEE802_1X_TYPE_EAPOL_START) {
147                 /* Trying to use PMKSA caching - do not send EAPOL-Start frames
148                  * since they will trigger full EAPOL authentication. */
149                 wpa_printf(MSG_DEBUG, "RSN: PMKSA caching - do not send "
150                            "EAPOL-Start");
151                 return -1;
152         }
153
154         if (is_zero_ether_addr(wpa_s->bssid)) {
155                 wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
156                            "EAPOL frame");
157                 if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
158                     !is_zero_ether_addr(bssid)) {
159                         dst = bssid;
160                         wpa_printf(MSG_DEBUG, "Using current BSSID " MACSTR
161                                    " from the driver as the EAPOL destination",
162                                    MAC2STR(dst));
163                 } else {
164                         dst = wpa_s->last_eapol_src;
165                         wpa_printf(MSG_DEBUG, "Using the source address of the"
166                                    " last received EAPOL frame " MACSTR " as "
167                                    "the EAPOL destination",
168                                    MAC2STR(dst));
169                 }
170         } else {
171                 /* BSSID was already set (from (Re)Assoc event, so use it as
172                  * the EAPOL destination. */
173                 dst = wpa_s->bssid;
174         }
175
176         msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
177         if (msg == NULL)
178                 return -1;
179
180         wpa_printf(MSG_DEBUG, "TX EAPOL: dst=" MACSTR, MAC2STR(dst));
181         wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
182         res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
183         os_free(msg);
184         return res;
185 }
186
187
188 /**
189  * wpa_eapol_set_wep_key - set WEP key for the driver
190  * @ctx: Pointer to wpa_supplicant data (wpa_s)
191  * @unicast: 1 = individual unicast key, 0 = broadcast key
192  * @keyidx: WEP key index (0..3)
193  * @key: Pointer to key data
194  * @keylen: Key length in bytes
195  * Returns: 0 on success or < 0 on error.
196  */
197 static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
198                                  const u8 *key, size_t keylen)
199 {
200         struct wpa_supplicant *wpa_s = ctx;
201         if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
202                 int cipher = (keylen == 5) ? WPA_CIPHER_WEP40 :
203                         WPA_CIPHER_WEP104;
204                 if (unicast)
205                         wpa_s->pairwise_cipher = cipher;
206                 else
207                         wpa_s->group_cipher = cipher;
208         }
209         return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
210                                unicast ? wpa_s->bssid :
211                                (u8 *) "\xff\xff\xff\xff\xff\xff",
212                                keyidx, unicast, (u8 *) "", 0, key, keylen);
213 }
214
215
216 static void wpa_supplicant_aborted_cached(void *ctx)
217 {
218         struct wpa_supplicant *wpa_s = ctx;
219         wpa_sm_aborted_cached(wpa_s->wpa);
220 }
221
222
223 static void wpa_supplicant_eapol_cb(struct eapol_sm *eapol, int success,
224                                     void *ctx)
225 {
226         struct wpa_supplicant *wpa_s = ctx;
227         int res, pmk_len;
228         u8 pmk[PMK_LEN];
229
230         wpa_printf(MSG_DEBUG, "EAPOL authentication completed %ssuccessfully",
231                    success ? "" : "un");
232
233 #ifdef CONFIG_WPS
234         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
235             !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
236                 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
237                            "try to associate with the received credential");
238                 wpa_supplicant_deauthenticate(wpa_s,
239                                               WLAN_REASON_DEAUTH_LEAVING);
240                 wpa_s->reassociate = 1;
241                 wpa_supplicant_req_scan(wpa_s, 0, 0);
242                 return;
243         }
244 #endif /* CONFIG_WPS */
245
246         if (!success) {
247                 /*
248                  * Make sure we do not get stuck here waiting for long EAPOL
249                  * timeout if the AP does not disconnect in case of
250                  * authentication failure.
251                  */
252                 wpa_supplicant_req_auth_timeout(wpa_s, 2, 0);
253         }
254
255         if (!success || !wpa_s->driver_4way_handshake)
256                 return;
257
258         if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt))
259                 return;
260
261         wpa_printf(MSG_DEBUG, "Configure PMK for driver-based RSN 4-way "
262                    "handshake");
263
264         pmk_len = PMK_LEN;
265         res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
266         if (res) {
267                 /*
268                  * EAP-LEAP is an exception from other EAP methods: it
269                  * uses only 16-byte PMK.
270                  */
271                 res = eapol_sm_get_key(eapol, pmk, 16);
272                 pmk_len = 16;
273         }
274
275         if (res) {
276                 wpa_printf(MSG_DEBUG, "Failed to get PMK from EAPOL state "
277                            "machines");
278                 return;
279         }
280
281         if (wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0, NULL, 0, pmk,
282                             pmk_len)) {
283                 wpa_printf(MSG_DEBUG, "Failed to set PMK to the driver");
284         }
285
286         wpa_supplicant_cancel_scan(wpa_s);
287         wpa_supplicant_cancel_auth_timeout(wpa_s);
288         wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
289
290 }
291
292
293 static void wpa_supplicant_notify_eapol_done(void *ctx)
294 {
295         struct wpa_supplicant *wpa_s = ctx;
296         wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
297         if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
298                 wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
299         } else {
300                 wpa_supplicant_cancel_auth_timeout(wpa_s);
301                 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
302         }
303 }
304
305 #endif /* IEEE8021X_EAPOL */
306
307
308 #ifndef CONFIG_NO_WPA
309
310 static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
311 {
312         size_t i;
313         int ret = 0;
314         struct wpa_scan_res *curr = NULL;
315         struct wpa_ssid *ssid = wpa_s->current_ssid;
316         const u8 *ie;
317
318         if (wpa_s->scan_res == NULL)
319                 return -1;
320
321         for (i = 0; i < wpa_s->scan_res->num; i++) {
322                 struct wpa_scan_res *r = wpa_s->scan_res->res[i];
323                 if (os_memcmp(r->bssid, wpa_s->bssid, ETH_ALEN) != 0)
324                         continue;
325                 ie = wpa_scan_get_ie(r, WLAN_EID_SSID);
326                 if (ssid == NULL ||
327                     ((ie && ie[1] == ssid->ssid_len &&
328                       os_memcmp(ie + 2, ssid->ssid, ssid->ssid_len) == 0) ||
329                      ssid->ssid_len == 0)) {
330                         curr = r;
331                         break;
332                 }
333         }
334
335         if (curr) {
336                 ie = wpa_scan_get_vendor_ie(curr, WPA_IE_VENDOR_TYPE);
337                 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
338                         ret = -1;
339
340                 ie = wpa_scan_get_ie(curr, WLAN_EID_RSN);
341                 if (wpa_sm_set_ap_rsn_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
342                         ret = -1;
343         } else {
344                 ret = -1;
345         }
346
347         return ret;
348 }
349
350
351 static int wpa_supplicant_get_beacon_ie(void *ctx)
352 {
353         struct wpa_supplicant *wpa_s = ctx;
354         if (wpa_get_beacon_ie(wpa_s) == 0) {
355                 return 0;
356         }
357
358         /* No WPA/RSN IE found in the cached scan results. Try to get updated
359          * scan results from the driver. */
360         if (wpa_supplicant_get_scan_results(wpa_s) < 0) {
361                 return -1;
362         }
363
364         return wpa_get_beacon_ie(wpa_s);
365 }
366
367
368 static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
369                              const void *data, u16 data_len,
370                              size_t *msg_len, void **data_pos)
371 {
372         return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
373 }
374
375
376 static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
377                            const u8 *buf, size_t len)
378 {
379         return wpa_ether_send(wpa_s, dest, proto, buf, len);
380 }
381
382
383 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
384 {
385         wpa_supplicant_cancel_auth_timeout(wpa_s);
386 }
387
388
389 static void _wpa_supplicant_set_state(void *wpa_s, wpa_states state)
390 {
391         wpa_supplicant_set_state(wpa_s, state);
392 }
393
394
395 /**
396  * wpa_supplicant_get_state - Get the connection state
397  * @wpa_s: Pointer to wpa_supplicant data
398  * Returns: The current connection state (WPA_*)
399  */
400 static wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
401 {
402         return wpa_s->wpa_state;
403 }
404
405
406 static wpa_states _wpa_supplicant_get_state(void *wpa_s)
407 {
408         return wpa_supplicant_get_state(wpa_s);
409 }
410
411
412 static void _wpa_supplicant_disassociate(void *wpa_s, int reason_code)
413 {
414         wpa_supplicant_disassociate(wpa_s, reason_code);
415         /* Schedule a scan to make sure we continue looking for networks */
416         wpa_supplicant_req_scan(wpa_s, 0, 0);
417 }
418
419
420 static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
421 {
422         wpa_supplicant_deauthenticate(wpa_s, reason_code);
423         /* Schedule a scan to make sure we continue looking for networks */
424         wpa_supplicant_req_scan(wpa_s, 0, 0);
425 }
426
427
428 static void * wpa_supplicant_get_network_ctx(void *wpa_s)
429 {
430         return wpa_supplicant_get_ssid(wpa_s);
431 }
432
433
434 static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
435 {
436         struct wpa_supplicant *wpa_s = ctx;
437         if (wpa_s->use_client_mlme) {
438                 os_memcpy(bssid, wpa_s->bssid, ETH_ALEN);
439                 return 0;
440         }
441         return wpa_drv_get_bssid(wpa_s, bssid);
442 }
443
444
445 static int wpa_supplicant_set_key(void *_wpa_s, wpa_alg alg,
446                                   const u8 *addr, int key_idx, int set_tx,
447                                   const u8 *seq, size_t seq_len,
448                                   const u8 *key, size_t key_len)
449 {
450         struct wpa_supplicant *wpa_s = _wpa_s;
451         if (alg == WPA_ALG_TKIP && key_idx == 0 && key_len == 32) {
452                 /* Clear the MIC error counter when setting a new PTK. */
453                 wpa_s->mic_errors_seen = 0;
454         }
455         return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
456                                key, key_len);
457 }
458
459
460 static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
461                                              int protection_type,
462                                              int key_type)
463 {
464         return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
465                                           key_type);
466 }
467
468
469 static int wpa_supplicant_add_pmkid(void *wpa_s,
470                                     const u8 *bssid, const u8 *pmkid)
471 {
472         return wpa_drv_add_pmkid(wpa_s, bssid, pmkid);
473 }
474
475
476 static int wpa_supplicant_remove_pmkid(void *wpa_s,
477                                        const u8 *bssid, const u8 *pmkid)
478 {
479         return wpa_drv_remove_pmkid(wpa_s, bssid, pmkid);
480 }
481
482
483 #ifdef CONFIG_IEEE80211R
484 static int wpa_supplicant_update_ft_ies(void *ctx, const u8 *md,
485                                         const u8 *ies, size_t ies_len)
486 {
487         struct wpa_supplicant *wpa_s = ctx;
488         if (wpa_s->use_client_mlme)
489                 return ieee80211_sta_update_ft_ies(wpa_s, md, ies, ies_len);
490         return wpa_drv_update_ft_ies(wpa_s, md, ies, ies_len);
491 }
492
493
494 static int wpa_supplicant_send_ft_action(void *ctx, u8 action,
495                                          const u8 *target_ap,
496                                          const u8 *ies, size_t ies_len)
497 {
498         struct wpa_supplicant *wpa_s = ctx;
499         if (wpa_s->use_client_mlme)
500                 return ieee80211_sta_send_ft_action(wpa_s, action, target_ap,
501                                                     ies, ies_len);
502         return wpa_drv_send_ft_action(wpa_s, action, target_ap, ies, ies_len);
503 }
504 #endif /* CONFIG_IEEE80211R */
505
506 #endif /* CONFIG_NO_WPA */
507
508
509 #ifdef CONFIG_WPS
510 static int wpa_supplicant_wps_cred(void *ctx, struct wps_credential *cred)
511 {
512         struct wpa_supplicant *wpa_s = ctx;
513         struct wpa_ssid *ssid = wpa_s->current_ssid;
514
515         wpa_msg(wpa_s, MSG_INFO, "WPS: New credential received");
516
517         if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
518                 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
519                            "on the received credential");
520                 os_free(ssid->eap.identity);
521                 ssid->eap.identity = NULL;
522                 ssid->eap.identity_len = 0;
523                 os_free(ssid->eap.phase1);
524                 ssid->eap.phase1 = NULL;
525                 os_free(ssid->eap.eap_methods);
526                 ssid->eap.eap_methods = NULL;
527         } else {
528                 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
529                            "received credential");
530                 ssid = wpa_config_add_network(wpa_s->conf);
531                 if (ssid == NULL)
532                         return -1;
533         }
534
535         wpa_config_set_network_defaults(ssid);
536
537         os_free(ssid->ssid);
538         ssid->ssid = os_malloc(cred->ssid_len);
539         if (ssid->ssid) {
540                 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
541                 ssid->ssid_len = cred->ssid_len;
542         }
543
544         switch (cred->encr_type) {
545         case WPS_ENCR_NONE:
546                 ssid->pairwise_cipher = ssid->group_cipher = WPA_CIPHER_NONE;
547                 break;
548         case WPS_ENCR_WEP:
549                 ssid->pairwise_cipher = ssid->group_cipher =
550                         WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104;
551                 if (cred->key_len > 0 && cred->key_len <= MAX_WEP_KEY_LEN &&
552                     cred->key_idx < NUM_WEP_KEYS) {
553                         os_memcpy(ssid->wep_key[cred->key_idx], cred->key,
554                                   cred->key_len);
555                         ssid->wep_key_len[cred->key_idx] = cred->key_len;
556                         ssid->wep_tx_keyidx = cred->key_idx;
557                 }
558                 break;
559         case WPS_ENCR_TKIP:
560                 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
561                 ssid->group_cipher = WPA_CIPHER_TKIP;
562                 break;
563         case WPS_ENCR_AES:
564                 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
565                 ssid->group_cipher = WPA_CIPHER_CCMP | WPA_CIPHER_TKIP;
566                 break;
567         }
568
569         switch (cred->auth_type) {
570         case WPS_AUTH_OPEN:
571                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
572                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
573                 ssid->proto = 0;
574                 break;
575         case WPS_AUTH_SHARED:
576                 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
577                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
578                 ssid->proto = 0;
579                 break;
580         case WPS_AUTH_WPAPSK:
581                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
582                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
583                 ssid->proto = WPA_PROTO_WPA;
584                 break;
585         case WPS_AUTH_WPA:
586                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
587                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
588                 ssid->proto = WPA_PROTO_WPA;
589                 break;
590         case WPS_AUTH_WPA2:
591                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
592                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
593                 ssid->proto = WPA_PROTO_RSN;
594                 break;
595         case WPS_AUTH_WPA2PSK:
596                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
597                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
598                 ssid->proto = WPA_PROTO_RSN;
599                 break;
600         }
601
602         if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
603                 if (cred->key_len == 2 * PMK_LEN) {
604                         if (hexstr2bin((const char *) cred->key, ssid->psk,
605                                        PMK_LEN)) {
606                                 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
607                                            "Key");
608                                 return -1;
609                         }
610                         ssid->psk_set = 1;
611                 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
612                         os_free(ssid->passphrase);
613                         ssid->passphrase = os_malloc(cred->key_len + 1);
614                         if (ssid->passphrase == NULL)
615                                 return -1;
616                         os_memcpy(ssid->passphrase, cred->key, cred->key_len);
617                         ssid->passphrase[cred->key_len] = '\0';
618                         wpa_config_update_psk(ssid);
619                 } else {
620                         wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
621                                    "length %lu",
622                                    (unsigned long) cred->key_len);
623                         return -1;
624                 }
625         }
626
627 #ifndef CONFIG_NO_CONFIG_WRITE
628         if (wpa_s->conf->update_config &&
629             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
630                 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
631                 return -1;
632         }
633 #endif /* CONFIG_NO_CONFIG_WRITE */
634
635         return 0;
636 }
637 #else /* CONFIG_WPS */
638 #define wpa_supplicant_wps_cred NULL
639 #endif /* CONFIG_WPS */
640
641
642 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
643 static void wpa_supplicant_eap_param_needed(void *ctx, const char *field,
644                                             const char *txt)
645 {
646         struct wpa_supplicant *wpa_s = ctx;
647         struct wpa_ssid *ssid = wpa_s->current_ssid;
648         char *buf;
649         size_t buflen;
650         int len;
651
652         if (ssid == NULL)
653                 return;
654
655         buflen = 100 + os_strlen(txt) + ssid->ssid_len;
656         buf = os_malloc(buflen);
657         if (buf == NULL)
658                 return;
659         len = os_snprintf(buf, buflen,
660                           WPA_CTRL_REQ "%s-%d:%s needed for SSID ",
661                           field, ssid->id, txt);
662         if (len < 0 || (size_t) len >= buflen) {
663                 os_free(buf);
664                 return;
665         }
666         if (ssid->ssid && buflen > len + ssid->ssid_len) {
667                 os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
668                 len += ssid->ssid_len;
669                 buf[len] = '\0';
670         }
671         buf[buflen - 1] = '\0';
672         wpa_msg(wpa_s, MSG_INFO, "%s", buf);
673         os_free(buf);
674 }
675 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
676 #define wpa_supplicant_eap_param_needed NULL
677 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
678
679
680 int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
681 {
682 #ifdef IEEE8021X_EAPOL
683         struct eapol_ctx *ctx;
684         ctx = os_zalloc(sizeof(*ctx));
685         if (ctx == NULL) {
686                 wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
687                 return -1;
688         }
689
690         ctx->ctx = wpa_s;
691         ctx->msg_ctx = wpa_s;
692         ctx->eapol_send_ctx = wpa_s;
693         ctx->preauth = 0;
694         ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
695         ctx->eapol_send = wpa_supplicant_eapol_send;
696         ctx->set_wep_key = wpa_eapol_set_wep_key;
697         ctx->set_config_blob = wpa_supplicant_set_config_blob;
698         ctx->get_config_blob = wpa_supplicant_get_config_blob;
699         ctx->aborted_cached = wpa_supplicant_aborted_cached;
700 #ifdef EAP_TLS_OPENSSL
701         ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
702         ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
703         ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
704 #endif /* EAP_TLS_OPENSSL */
705         ctx->mac_addr = wpa_s->own_addr;
706         ctx->uuid = wpa_s->conf->uuid;
707         ctx->wps_cred = wpa_supplicant_wps_cred;
708         ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
709         ctx->cb = wpa_supplicant_eapol_cb;
710         ctx->cb_ctx = wpa_s;
711         wpa_s->eapol = eapol_sm_init(ctx);
712         if (wpa_s->eapol == NULL) {
713                 os_free(ctx);
714                 wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
715                            "machines.");
716                 return -1;
717         }
718 #endif /* IEEE8021X_EAPOL */
719
720         return 0;
721 }
722
723
724 int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
725 {
726 #ifndef CONFIG_NO_WPA
727         struct wpa_sm_ctx *ctx;
728         ctx = os_zalloc(sizeof(*ctx));
729         if (ctx == NULL) {
730                 wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
731                 return -1;
732         }
733
734         ctx->ctx = wpa_s;
735         ctx->set_state = _wpa_supplicant_set_state;
736         ctx->get_state = _wpa_supplicant_get_state;
737         ctx->deauthenticate = _wpa_supplicant_deauthenticate;
738         ctx->disassociate = _wpa_supplicant_disassociate;
739         ctx->set_key = wpa_supplicant_set_key;
740         ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
741         ctx->get_bssid = wpa_supplicant_get_bssid;
742         ctx->ether_send = _wpa_ether_send;
743         ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
744         ctx->alloc_eapol = _wpa_alloc_eapol;
745         ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
746         ctx->add_pmkid = wpa_supplicant_add_pmkid;
747         ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
748 #ifndef CONFIG_NO_CONFIG_BLOBS
749         ctx->set_config_blob = wpa_supplicant_set_config_blob;
750         ctx->get_config_blob = wpa_supplicant_get_config_blob;
751 #endif /* CONFIG_NO_CONFIG_BLOBS */
752         ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
753 #ifdef CONFIG_IEEE80211R
754         ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
755         ctx->send_ft_action = wpa_supplicant_send_ft_action;
756 #endif /* CONFIG_IEEE80211R */
757
758         wpa_s->wpa = wpa_sm_init(ctx);
759         if (wpa_s->wpa == NULL) {
760                 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
761                            "machine");
762                 return -1;
763         }
764 #endif /* CONFIG_NO_WPA */
765
766         return 0;
767 }
768
769
770 void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
771                                         struct wpa_ssid *ssid)
772 {
773         struct rsn_supp_config conf;
774         if (ssid) {
775                 os_memset(&conf, 0, sizeof(conf));
776                 conf.network_ctx = ssid;
777                 conf.peerkey_enabled = ssid->peerkey;
778                 conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
779 #ifdef IEEE8021X_EAPOL
780                 conf.eap_workaround = ssid->eap_workaround;
781                 conf.eap_conf_ctx = &ssid->eap;
782 #endif /* IEEE8021X_EAPOL */
783                 conf.ssid = ssid->ssid;
784                 conf.ssid_len = ssid->ssid_len;
785                 conf.wpa_ptk_rekey = ssid->wpa_ptk_rekey;
786         }
787         wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);
788 }