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