]> git.ipfire.org Git - thirdparty/hostap.git/blame - wpa_supplicant/wpas_glue.c
wpa_supplicant configuration for Beacon protection
[thirdparty/hostap.git] / wpa_supplicant / wpas_glue.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant - Glue code to setup EAPOL and RSN modules
98cd3d1c 3 * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eapol_supp/eapol_supp_sm.h"
c167662d 13#include "eap_peer/eap.h"
3acb5005 14#include "rsn_supp/wpa.h"
6fc6879b
JM
15#include "eloop.h"
16#include "config.h"
17#include "l2_packet/l2_packet.h"
90973fb2 18#include "common/wpa_common.h"
6fc6879b 19#include "wpa_supplicant_i.h"
2d5b792d 20#include "driver_i.h"
3acb5005 21#include "rsn_supp/pmksa_cache.h"
c2a04078 22#include "sme.h"
90973fb2
JM
23#include "common/ieee802_11_defs.h"
24#include "common/wpa_ctrl.h"
6fc6879b 25#include "wpas_glue.h"
fa201b69 26#include "wps_supplicant.h"
e48f0fb6 27#include "bss.h"
9ba9fa07 28#include "scan.h"
ade74830 29#include "notify.h"
dd10abcc 30#include "wpas_kay.h"
6fc6879b
JM
31
32
33#ifndef CONFIG_NO_CONFIG_BLOBS
34#if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
35static void wpa_supplicant_set_config_blob(void *ctx,
36 struct wpa_config_blob *blob)
37{
38 struct wpa_supplicant *wpa_s = ctx;
39 wpa_config_set_blob(wpa_s->conf, blob);
c08b9180
JM
40 if (wpa_s->conf->update_config) {
41 int ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
42 if (ret) {
43 wpa_printf(MSG_DEBUG, "Failed to update config after "
44 "blob set");
45 }
46 }
6fc6879b
JM
47}
48
49
50static const struct wpa_config_blob *
51wpa_supplicant_get_config_blob(void *ctx, const char *name)
52{
53 struct wpa_supplicant *wpa_s = ctx;
54 return wpa_config_get_blob(wpa_s->conf, name);
55}
56#endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
57#endif /* CONFIG_NO_CONFIG_BLOBS */
58
59
60#if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
61static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
62 const void *data, u16 data_len,
63 size_t *msg_len, void **data_pos)
64{
65 struct ieee802_1x_hdr *hdr;
66
67 *msg_len = sizeof(*hdr) + data_len;
68 hdr = os_malloc(*msg_len);
69 if (hdr == NULL)
70 return NULL;
71
72 hdr->version = wpa_s->conf->eapol_version;
73 hdr->type = type;
74 hdr->length = host_to_be16(data_len);
75
76 if (data)
77 os_memcpy(hdr + 1, data, data_len);
78 else
79 os_memset(hdr + 1, 0, data_len);
80
81 if (data_pos)
82 *data_pos = hdr + 1;
83
84 return (u8 *) hdr;
85}
86
87
88/**
89 * wpa_ether_send - Send Ethernet frame
90 * @wpa_s: Pointer to wpa_supplicant data
91 * @dest: Destination MAC address
92 * @proto: Ethertype in host byte order
93 * @buf: Frame payload starting from IEEE 802.1X header
94 * @len: Frame payload length
95 * Returns: >=0 on success, <0 on failure
96 */
97static int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest,
98 u16 proto, const u8 *buf, size_t len)
99{
9d4ff04a
JM
100#ifdef CONFIG_TESTING_OPTIONS
101 if (wpa_s->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
102 size_t hex_len = 2 * len + 1;
103 char *hex = os_malloc(hex_len);
104
105 if (hex == NULL)
106 return -1;
107 wpa_snprintf_hex(hex, hex_len, buf, len);
108 wpa_msg(wpa_s, MSG_INFO, "EAPOL-TX " MACSTR " %s",
109 MAC2STR(dest), hex);
110 os_free(hex);
111 return 0;
112 }
113#endif /* CONFIG_TESTING_OPTIONS */
114
a79ed068
MT
115 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_CONTROL_PORT) {
116 int encrypt = wpa_s->wpa &&
117 wpa_sm_has_ptk_installed(wpa_s->wpa);
118
119 return wpa_drv_tx_control_port(wpa_s, dest, proto, buf, len,
120 !encrypt);
121 }
144314ea 122
6fc6879b
JM
123 if (wpa_s->l2) {
124 return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
125 }
126
2961bfa8 127 return -1;
6fc6879b
JM
128}
129#endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
130
131
132#ifdef IEEE8021X_EAPOL
133
134/**
135 * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
136 * @ctx: Pointer to wpa_supplicant data (wpa_s)
137 * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
138 * @buf: EAPOL payload (after IEEE 802.1X header)
139 * @len: EAPOL payload length
140 * Returns: >=0 on success, <0 on failure
141 *
142 * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
143 * to the current Authenticator.
144 */
145static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
146 size_t len)
147{
148 struct wpa_supplicant *wpa_s = ctx;
149 u8 *msg, *dst, bssid[ETH_ALEN];
150 size_t msglen;
151 int res;
152
153 /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
154 * extra copy here */
155
56586197 156 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
a1ea1b45 157 wpa_s->key_mgmt == WPA_KEY_MGMT_OWE ||
567da5bb 158 wpa_s->key_mgmt == WPA_KEY_MGMT_DPP ||
6fc6879b
JM
159 wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
160 /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
161 * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
162 * machines. */
163 wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
164 "mode (type=%d len=%lu)", type,
165 (unsigned long) len);
166 return -1;
167 }
168
169 if (pmksa_cache_get_current(wpa_s->wpa) &&
170 type == IEEE802_1X_TYPE_EAPOL_START) {
76db5b6b
JM
171 /*
172 * We were trying to use PMKSA caching and sending EAPOL-Start
173 * would abort that and trigger full EAPOL authentication.
174 * However, we've already waited for the AP/Authenticator to
175 * start 4-way handshake or EAP authentication, and apparently
176 * it has not done so since the startWhen timer has reached zero
177 * to get the state machine sending EAPOL-Start. This is not
178 * really supposed to happen, but an interoperability issue with
179 * a deployed AP has been identified where the connection fails
180 * due to that AP failing to operate correctly if PMKID is
181 * included in the Association Request frame. To work around
182 * this, assume PMKSA caching failed and try to initiate full
183 * EAP authentication.
184 */
185 if (!wpa_s->current_ssid ||
186 wpa_s->current_ssid->eap_workaround) {
187 wpa_printf(MSG_DEBUG,
188 "RSN: Timeout on waiting for the AP to initiate 4-way handshake for PMKSA caching or EAP authentication - try to force it to start EAP authentication");
189 } else {
190 wpa_printf(MSG_DEBUG,
191 "RSN: PMKSA caching - do not send EAPOL-Start");
192 return -1;
193 }
6fc6879b
JM
194 }
195
a8e16edc 196 if (is_zero_ether_addr(wpa_s->bssid)) {
6fc6879b
JM
197 wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
198 "EAPOL frame");
199 if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
a8e16edc 200 !is_zero_ether_addr(bssid)) {
6fc6879b
JM
201 dst = bssid;
202 wpa_printf(MSG_DEBUG, "Using current BSSID " MACSTR
203 " from the driver as the EAPOL destination",
204 MAC2STR(dst));
205 } else {
206 dst = wpa_s->last_eapol_src;
207 wpa_printf(MSG_DEBUG, "Using the source address of the"
208 " last received EAPOL frame " MACSTR " as "
209 "the EAPOL destination",
210 MAC2STR(dst));
211 }
212 } else {
213 /* BSSID was already set (from (Re)Assoc event, so use it as
214 * the EAPOL destination. */
215 dst = wpa_s->bssid;
216 }
217
218 msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
219 if (msg == NULL)
220 return -1;
221
222 wpa_printf(MSG_DEBUG, "TX EAPOL: dst=" MACSTR, MAC2STR(dst));
223 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
224 res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
225 os_free(msg);
226 return res;
227}
228
229
230/**
231 * wpa_eapol_set_wep_key - set WEP key for the driver
232 * @ctx: Pointer to wpa_supplicant data (wpa_s)
233 * @unicast: 1 = individual unicast key, 0 = broadcast key
234 * @keyidx: WEP key index (0..3)
235 * @key: Pointer to key data
236 * @keylen: Key length in bytes
237 * Returns: 0 on success or < 0 on error.
238 */
239static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
240 const u8 *key, size_t keylen)
241{
242 struct wpa_supplicant *wpa_s = ctx;
243 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
244 int cipher = (keylen == 5) ? WPA_CIPHER_WEP40 :
245 WPA_CIPHER_WEP104;
246 if (unicast)
247 wpa_s->pairwise_cipher = cipher;
248 else
249 wpa_s->group_cipher = cipher;
250 }
251 return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
0382097e 252 unicast ? wpa_s->bssid : NULL,
a919a260
AW
253 keyidx, unicast, NULL, 0, key, keylen,
254 unicast ? KEY_FLAG_PAIRWISE_RX_TX :
255 KEY_FLAG_GROUP_RX_TX_DEFAULT);
6fc6879b
JM
256}
257
258
259static void wpa_supplicant_aborted_cached(void *ctx)
260{
261 struct wpa_supplicant *wpa_s = ctx;
262 wpa_sm_aborted_cached(wpa_s->wpa);
263}
264
265
c60ba9f7
JM
266static const char * result_str(enum eapol_supp_result result)
267{
268 switch (result) {
269 case EAPOL_SUPP_RESULT_FAILURE:
270 return "FAILURE";
271 case EAPOL_SUPP_RESULT_SUCCESS:
272 return "SUCCESS";
273 case EAPOL_SUPP_RESULT_EXPECTED_FAILURE:
274 return "EXPECTED_FAILURE";
275 }
276 return "?";
277}
278
279
280static void wpa_supplicant_eapol_cb(struct eapol_sm *eapol,
281 enum eapol_supp_result result,
6fc6879b
JM
282 void *ctx)
283{
284 struct wpa_supplicant *wpa_s = ctx;
285 int res, pmk_len;
286 u8 pmk[PMK_LEN];
287
c60ba9f7
JM
288 wpa_printf(MSG_DEBUG, "EAPOL authentication completed - result=%s",
289 result_str(result));
6fc6879b 290
fa201b69 291 if (wpas_wps_eapol_cb(wpa_s) > 0)
ad08c363 292 return;
ad08c363 293
c60ba9f7
JM
294 wpa_s->eap_expected_failure = result ==
295 EAPOL_SUPP_RESULT_EXPECTED_FAILURE;
296
297 if (result != EAPOL_SUPP_RESULT_SUCCESS) {
0930209d
JM
298 /*
299 * Make sure we do not get stuck here waiting for long EAPOL
300 * timeout if the AP does not disconnect in case of
301 * authentication failure.
302 */
303 wpa_supplicant_req_auth_timeout(wpa_s, 2, 0);
dd10abcc
HW
304 } else {
305 ieee802_1x_notify_create_actor(wpa_s, wpa_s->last_eapol_src);
0930209d
JM
306 }
307
c60ba9f7 308 if (result != EAPOL_SUPP_RESULT_SUCCESS ||
436ee2fd 309 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X))
6fc6879b
JM
310 return;
311
56586197 312 if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt))
6fc6879b
JM
313 return;
314
315 wpa_printf(MSG_DEBUG, "Configure PMK for driver-based RSN 4-way "
316 "handshake");
317
318 pmk_len = PMK_LEN;
09c395b4
JM
319 if (wpa_key_mgmt_ft(wpa_s->key_mgmt)) {
320#ifdef CONFIG_IEEE80211R
321 u8 buf[2 * PMK_LEN];
322 wpa_printf(MSG_DEBUG, "RSN: Use FT XXKey as PMK for "
323 "driver-based 4-way hs and FT");
324 res = eapol_sm_get_key(eapol, buf, 2 * PMK_LEN);
325 if (res == 0) {
326 os_memcpy(pmk, buf + PMK_LEN, PMK_LEN);
327 os_memset(buf, 0, sizeof(buf));
328 }
329#else /* CONFIG_IEEE80211R */
330 res = -1;
331#endif /* CONFIG_IEEE80211R */
332 } else {
333 res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
334 if (res) {
335 /*
336 * EAP-LEAP is an exception from other EAP methods: it
337 * uses only 16-byte PMK.
338 */
339 res = eapol_sm_get_key(eapol, pmk, 16);
340 pmk_len = 16;
341 }
6fc6879b
JM
342 }
343
344 if (res) {
345 wpa_printf(MSG_DEBUG, "Failed to get PMK from EAPOL state "
346 "machines");
347 return;
348 }
349
09c395b4
JM
350 wpa_hexdump_key(MSG_DEBUG, "RSN: Configure PMK for driver-based 4-way "
351 "handshake", pmk, pmk_len);
352
6fc6879b 353 if (wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0, NULL, 0, pmk,
a919a260 354 pmk_len, KEY_FLAG_PMK)) {
6fc6879b
JM
355 wpa_printf(MSG_DEBUG, "Failed to set PMK to the driver");
356 }
357
358 wpa_supplicant_cancel_scan(wpa_s);
359 wpa_supplicant_cancel_auth_timeout(wpa_s);
360 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
361
362}
363
364
365static void wpa_supplicant_notify_eapol_done(void *ctx)
366{
367 struct wpa_supplicant *wpa_s = ctx;
368 wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
56586197 369 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
6fc6879b
JM
370 wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
371 } else {
6fc6879b
JM
372 wpa_supplicant_cancel_auth_timeout(wpa_s);
373 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
374 }
375}
376
377#endif /* IEEE8021X_EAPOL */
378
379
380#ifndef CONFIG_NO_WPA
381
382static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
383{
6fc6879b 384 int ret = 0;
e48f0fb6 385 struct wpa_bss *curr = NULL, *bss;
6fc6879b
JM
386 struct wpa_ssid *ssid = wpa_s->current_ssid;
387 const u8 *ie;
388
e48f0fb6
JM
389 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
390 if (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) != 0)
6fc6879b 391 continue;
6fc6879b 392 if (ssid == NULL ||
e48f0fb6
JM
393 ((bss->ssid_len == ssid->ssid_len &&
394 os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) == 0) ||
6fc6879b 395 ssid->ssid_len == 0)) {
e48f0fb6 396 curr = bss;
6fc6879b
JM
397 break;
398 }
399 }
400
401 if (curr) {
e48f0fb6 402 ie = wpa_bss_get_vendor_ie(curr, WPA_IE_VENDOR_TYPE);
6fc6879b
JM
403 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
404 ret = -1;
405
e48f0fb6 406 ie = wpa_bss_get_ie(curr, WLAN_EID_RSN);
6fc6879b
JM
407 if (wpa_sm_set_ap_rsn_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
408 ret = -1;
146889e3
JM
409
410 ie = wpa_bss_get_ie(curr, WLAN_EID_RSNX);
411 if (wpa_sm_set_ap_rsnxe(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
412 ret = -1;
6fc6879b
JM
413 } else {
414 ret = -1;
415 }
416
417 return ret;
418}
419
420
421static int wpa_supplicant_get_beacon_ie(void *ctx)
422{
423 struct wpa_supplicant *wpa_s = ctx;
424 if (wpa_get_beacon_ie(wpa_s) == 0) {
425 return 0;
426 }
427
428 /* No WPA/RSN IE found in the cached scan results. Try to get updated
429 * scan results from the driver. */
a1fd2ce5 430 if (wpa_supplicant_update_scan_results(wpa_s) < 0)
6fc6879b 431 return -1;
6fc6879b
JM
432
433 return wpa_get_beacon_ie(wpa_s);
434}
435
436
437static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
438 const void *data, u16 data_len,
439 size_t *msg_len, void **data_pos)
440{
441 return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
442}
443
444
445static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
446 const u8 *buf, size_t len)
447{
448 return wpa_ether_send(wpa_s, dest, proto, buf, len);
449}
450
451
6fc6879b
JM
452static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
453{
454 wpa_supplicant_cancel_auth_timeout(wpa_s);
455}
456
457
71934751 458static void _wpa_supplicant_set_state(void *wpa_s, enum wpa_states state)
6fc6879b
JM
459{
460 wpa_supplicant_set_state(wpa_s, state);
461}
462
463
464/**
465 * wpa_supplicant_get_state - Get the connection state
466 * @wpa_s: Pointer to wpa_supplicant data
467 * Returns: The current connection state (WPA_*)
468 */
71934751 469static enum wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
6fc6879b
JM
470{
471 return wpa_s->wpa_state;
472}
473
474
71934751 475static enum wpa_states _wpa_supplicant_get_state(void *wpa_s)
6fc6879b
JM
476{
477 return wpa_supplicant_get_state(wpa_s);
478}
479
480
4be17ffb 481static void _wpa_supplicant_deauthenticate(void *wpa_s, u16 reason_code)
6fc6879b
JM
482{
483 wpa_supplicant_deauthenticate(wpa_s, reason_code);
3e2ad1b9 484 /* Schedule a scan to make sure we continue looking for networks */
83935317 485 wpa_supplicant_req_scan(wpa_s, 5, 0);
6fc6879b
JM
486}
487
488
489static void * wpa_supplicant_get_network_ctx(void *wpa_s)
490{
491 return wpa_supplicant_get_ssid(wpa_s);
492}
493
494
495static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
496{
497 struct wpa_supplicant *wpa_s = ctx;
6fc6879b
JM
498 return wpa_drv_get_bssid(wpa_s, bssid);
499}
500
501
71934751 502static int wpa_supplicant_set_key(void *_wpa_s, enum wpa_alg alg,
6fc6879b
JM
503 const u8 *addr, int key_idx, int set_tx,
504 const u8 *seq, size_t seq_len,
a919a260
AW
505 const u8 *key, size_t key_len,
506 enum key_flag key_flag)
6fc6879b 507{
46690a3b
JM
508 struct wpa_supplicant *wpa_s = _wpa_s;
509 if (alg == WPA_ALG_TKIP && key_idx == 0 && key_len == 32) {
510 /* Clear the MIC error counter when setting a new PTK. */
511 wpa_s->mic_errors_seen = 0;
512 }
fa7ae950
JM
513#ifdef CONFIG_TESTING_GET_GTK
514 if (key_idx > 0 && addr && is_broadcast_ether_addr(addr) &&
515 alg != WPA_ALG_NONE && key_len <= sizeof(wpa_s->last_gtk)) {
516 os_memcpy(wpa_s->last_gtk, key, key_len);
517 wpa_s->last_gtk_len = key_len;
518 }
519#endif /* CONFIG_TESTING_GET_GTK */
16579769
JM
520#ifdef CONFIG_TESTING_OPTIONS
521 if (addr && !is_broadcast_ether_addr(addr)) {
522 wpa_s->last_tk_alg = alg;
523 os_memcpy(wpa_s->last_tk_addr, addr, ETH_ALEN);
524 wpa_s->last_tk_key_idx = key_idx;
525 if (key)
526 os_memcpy(wpa_s->last_tk, key, key_len);
527 wpa_s->last_tk_len = key_len;
528 }
529#endif /* CONFIG_TESTING_OPTIONS */
6fc6879b 530 return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
a919a260 531 key, key_len, key_flag);
6fc6879b
JM
532}
533
534
535static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
536 int protection_type,
537 int key_type)
538{
539 return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
540 key_type);
541}
542
543
c5793127
JM
544static struct wpa_ssid * wpas_get_network_ctx(struct wpa_supplicant *wpa_s,
545 void *network_ctx)
546{
547 struct wpa_ssid *ssid;
548
549 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
550 if (network_ctx == ssid)
551 return ssid;
552 }
553
554 return NULL;
555}
556
557
558static int wpa_supplicant_add_pmkid(void *_wpa_s, void *network_ctx,
42e69bda
VK
559 const u8 *bssid, const u8 *pmkid,
560 const u8 *fils_cache_id,
561 const u8 *pmk, size_t pmk_len)
6fc6879b 562{
c5793127
JM
563 struct wpa_supplicant *wpa_s = _wpa_s;
564 struct wpa_ssid *ssid;
6fbb5414 565 struct wpa_pmkid_params params;
c5793127 566
6fbb5414 567 os_memset(&params, 0, sizeof(params));
c5793127
JM
568 ssid = wpas_get_network_ctx(wpa_s, network_ctx);
569 if (ssid)
570 wpa_msg(wpa_s, MSG_INFO, PMKSA_CACHE_ADDED MACSTR " %d",
571 MAC2STR(bssid), ssid->id);
42e69bda
VK
572 if (ssid && fils_cache_id) {
573 params.ssid = ssid->ssid;
574 params.ssid_len = ssid->ssid_len;
575 params.fils_cache_id = fils_cache_id;
576 } else {
577 params.bssid = bssid;
578 }
579
6fbb5414 580 params.pmkid = pmkid;
42e69bda
VK
581 params.pmk = pmk;
582 params.pmk_len = pmk_len;
6fbb5414
VK
583
584 return wpa_drv_add_pmkid(wpa_s, &params);
6fc6879b
JM
585}
586
587
c5793127 588static int wpa_supplicant_remove_pmkid(void *_wpa_s, void *network_ctx,
42e69bda
VK
589 const u8 *bssid, const u8 *pmkid,
590 const u8 *fils_cache_id)
6fc6879b 591{
c5793127
JM
592 struct wpa_supplicant *wpa_s = _wpa_s;
593 struct wpa_ssid *ssid;
6fbb5414 594 struct wpa_pmkid_params params;
c5793127 595
6fbb5414 596 os_memset(&params, 0, sizeof(params));
c5793127
JM
597 ssid = wpas_get_network_ctx(wpa_s, network_ctx);
598 if (ssid)
599 wpa_msg(wpa_s, MSG_INFO, PMKSA_CACHE_REMOVED MACSTR " %d",
600 MAC2STR(bssid), ssid->id);
42e69bda
VK
601 if (ssid && fils_cache_id) {
602 params.ssid = ssid->ssid;
603 params.ssid_len = ssid->ssid_len;
604 params.fils_cache_id = fils_cache_id;
605 } else {
606 params.bssid = bssid;
607 }
6fbb5414 608
6fbb5414
VK
609 params.pmkid = pmkid;
610
611 return wpa_drv_remove_pmkid(wpa_s, &params);
6fc6879b
JM
612}
613
614
615#ifdef CONFIG_IEEE80211R
616static int wpa_supplicant_update_ft_ies(void *ctx, const u8 *md,
617 const u8 *ies, size_t ies_len)
618{
619 struct wpa_supplicant *wpa_s = ctx;
c2a04078
JM
620 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
621 return sme_update_ft_ies(wpa_s, md, ies, ies_len);
6fc6879b
JM
622 return wpa_drv_update_ft_ies(wpa_s, md, ies, ies_len);
623}
624
625
626static int wpa_supplicant_send_ft_action(void *ctx, u8 action,
627 const u8 *target_ap,
628 const u8 *ies, size_t ies_len)
629{
630 struct wpa_supplicant *wpa_s = ctx;
8105821b
JM
631 int ret;
632 u8 *data, *pos;
633 size_t data_len;
634
635 if (action != 1) {
636 wpa_printf(MSG_ERROR, "Unsupported send_ft_action action %d",
637 action);
638 return -1;
639 }
640
641 /*
642 * Action frame payload:
643 * Category[1] = 6 (Fast BSS Transition)
644 * Action[1] = 1 (Fast BSS Transition Request)
645 * STA Address
646 * Target AP Address
647 * FT IEs
648 */
649
650 data_len = 2 + 2 * ETH_ALEN + ies_len;
651 data = os_malloc(data_len);
652 if (data == NULL)
653 return -1;
654 pos = data;
655 *pos++ = 0x06; /* FT Action category */
656 *pos++ = action;
657 os_memcpy(pos, wpa_s->own_addr, ETH_ALEN);
658 pos += ETH_ALEN;
659 os_memcpy(pos, target_ap, ETH_ALEN);
660 pos += ETH_ALEN;
661 os_memcpy(pos, ies, ies_len);
662
663 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
664 wpa_s->bssid, wpa_s->own_addr, wpa_s->bssid,
665 data, data_len, 0);
666 os_free(data);
667
668 return ret;
6fc6879b 669}
2a7e7f4e
JM
670
671
672static int wpa_supplicant_mark_authenticated(void *ctx, const u8 *target_ap)
673{
674 struct wpa_supplicant *wpa_s = ctx;
675 struct wpa_driver_auth_params params;
676 struct wpa_bss *bss;
677
2a7e7f4e
JM
678 bss = wpa_bss_get_bssid(wpa_s, target_ap);
679 if (bss == NULL)
680 return -1;
681
682 os_memset(&params, 0, sizeof(params));
683 params.bssid = target_ap;
684 params.freq = bss->freq;
685 params.ssid = bss->ssid;
686 params.ssid_len = bss->ssid_len;
687 params.auth_alg = WPA_AUTH_ALG_FT;
688 params.local_state_change = 1;
689 return wpa_drv_authenticate(wpa_s, &params);
690}
6fc6879b
JM
691#endif /* CONFIG_IEEE80211R */
692
6fc6879b 693
281ff0aa
GP
694#ifdef CONFIG_TDLS
695
c58ab8f2 696static int wpa_supplicant_tdls_get_capa(void *ctx, int *tdls_supported,
4daa5729
AN
697 int *tdls_ext_setup,
698 int *tdls_chan_switch)
c58ab8f2
AN
699{
700 struct wpa_supplicant *wpa_s = ctx;
701
702 *tdls_supported = 0;
703 *tdls_ext_setup = 0;
4daa5729 704 *tdls_chan_switch = 0;
c58ab8f2
AN
705
706 if (!wpa_s->drv_capa_known)
707 return -1;
708
709 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)
710 *tdls_supported = 1;
711
712 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP)
713 *tdls_ext_setup = 1;
714
4daa5729
AN
715 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH)
716 *tdls_chan_switch = 1;
717
c58ab8f2
AN
718 return 0;
719}
720
721
281ff0aa
GP
722static int wpa_supplicant_send_tdls_mgmt(void *ctx, const u8 *dst,
723 u8 action_code, u8 dialog_token,
96ecea5e 724 u16 status_code, u32 peer_capab,
984dadc2
AN
725 int initiator, const u8 *buf,
726 size_t len)
281ff0aa
GP
727{
728 struct wpa_supplicant *wpa_s = ctx;
729 return wpa_drv_send_tdls_mgmt(wpa_s, dst, action_code, dialog_token,
984dadc2
AN
730 status_code, peer_capab, initiator, buf,
731 len);
281ff0aa
GP
732}
733
734
735static int wpa_supplicant_tdls_oper(void *ctx, int oper, const u8 *peer)
736{
737 struct wpa_supplicant *wpa_s = ctx;
738 return wpa_drv_tdls_oper(wpa_s, oper, peer);
739}
740
45b722f1
AN
741
742static int wpa_supplicant_tdls_peer_addset(
78533699 743 void *ctx, const u8 *peer, int add, u16 aid, u16 capability,
ff4178d5
SD
744 const u8 *supp_rates, size_t supp_rates_len,
745 const struct ieee80211_ht_capabilities *ht_capab,
f8361e3d 746 const struct ieee80211_vht_capabilities *vht_capab,
08d7665c 747 u8 qosinfo, int wmm, const u8 *ext_capab, size_t ext_capab_len,
3ed97271
SD
748 const u8 *supp_channels, size_t supp_channels_len,
749 const u8 *supp_oper_classes, size_t supp_oper_classes_len)
45b722f1
AN
750{
751 struct wpa_supplicant *wpa_s = ctx;
752 struct hostapd_sta_add_params params;
753
b8df43de
JM
754 os_memset(&params, 0, sizeof(params));
755
45b722f1 756 params.addr = peer;
f11b72c3 757 params.aid = aid;
45b722f1
AN
758 params.capability = capability;
759 params.flags = WPA_STA_TDLS_PEER | WPA_STA_AUTHORIZED;
ff4178d5
SD
760
761 /*
08d7665c
AN
762 * Don't rely only on qosinfo for WMM capability. It may be 0 even when
763 * present. Allow the WMM IE to also indicate QoS support.
ff4178d5 764 */
08d7665c 765 if (wmm || qosinfo)
ff4178d5
SD
766 params.flags |= WPA_STA_WMM;
767
768 params.ht_capabilities = ht_capab;
f8361e3d 769 params.vht_capabilities = vht_capab;
ff4178d5 770 params.qosinfo = qosinfo;
45b722f1
AN
771 params.listen_interval = 0;
772 params.supp_rates = supp_rates;
773 params.supp_rates_len = supp_rates_len;
774 params.set = !add;
d16531c4
SD
775 params.ext_capab = ext_capab;
776 params.ext_capab_len = ext_capab_len;
3ed97271
SD
777 params.supp_channels = supp_channels;
778 params.supp_channels_len = supp_channels_len;
779 params.supp_oper_classes = supp_oper_classes;
780 params.supp_oper_classes_len = supp_oper_classes_len;
45b722f1
AN
781
782 return wpa_drv_sta_add(wpa_s, &params);
783}
784
6b90deae
AN
785
786static int wpa_supplicant_tdls_enable_channel_switch(
787 void *ctx, const u8 *addr, u8 oper_class,
788 const struct hostapd_freq_params *params)
789{
790 struct wpa_supplicant *wpa_s = ctx;
791
792 return wpa_drv_tdls_enable_channel_switch(wpa_s, addr, oper_class,
793 params);
794}
795
796
797static int wpa_supplicant_tdls_disable_channel_switch(void *ctx, const u8 *addr)
798{
799 struct wpa_supplicant *wpa_s = ctx;
800
801 return wpa_drv_tdls_disable_channel_switch(wpa_s, addr);
802}
803
281ff0aa
GP
804#endif /* CONFIG_TDLS */
805
9aaa6955
JM
806#endif /* CONFIG_NO_WPA */
807
281ff0aa 808
81c57e22
DW
809enum wpa_ctrl_req_type wpa_supplicant_ctrl_req_from_string(const char *field)
810{
811 if (os_strcmp(field, "IDENTITY") == 0)
812 return WPA_CTRL_REQ_EAP_IDENTITY;
813 else if (os_strcmp(field, "PASSWORD") == 0)
814 return WPA_CTRL_REQ_EAP_PASSWORD;
815 else if (os_strcmp(field, "NEW_PASSWORD") == 0)
816 return WPA_CTRL_REQ_EAP_NEW_PASSWORD;
817 else if (os_strcmp(field, "PIN") == 0)
818 return WPA_CTRL_REQ_EAP_PIN;
819 else if (os_strcmp(field, "OTP") == 0)
820 return WPA_CTRL_REQ_EAP_OTP;
821 else if (os_strcmp(field, "PASSPHRASE") == 0)
822 return WPA_CTRL_REQ_EAP_PASSPHRASE;
a5d44ac0
JM
823 else if (os_strcmp(field, "SIM") == 0)
824 return WPA_CTRL_REQ_SIM;
a52410c2
JM
825 else if (os_strcmp(field, "PSK_PASSPHRASE") == 0)
826 return WPA_CTRL_REQ_PSK_PASSPHRASE;
3c108b75
JM
827 else if (os_strcmp(field, "EXT_CERT_CHECK") == 0)
828 return WPA_CTRL_REQ_EXT_CERT_CHECK;
81c57e22
DW
829 return WPA_CTRL_REQ_UNKNOWN;
830}
831
832
9ef1aaae
DW
833const char * wpa_supplicant_ctrl_req_to_string(enum wpa_ctrl_req_type field,
834 const char *default_txt,
835 const char **txt)
836{
837 const char *ret = NULL;
838
839 *txt = default_txt;
840
841 switch (field) {
842 case WPA_CTRL_REQ_EAP_IDENTITY:
843 *txt = "Identity";
844 ret = "IDENTITY";
845 break;
846 case WPA_CTRL_REQ_EAP_PASSWORD:
847 *txt = "Password";
848 ret = "PASSWORD";
849 break;
850 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
851 *txt = "New Password";
852 ret = "NEW_PASSWORD";
853 break;
854 case WPA_CTRL_REQ_EAP_PIN:
855 *txt = "PIN";
856 ret = "PIN";
857 break;
858 case WPA_CTRL_REQ_EAP_OTP:
859 ret = "OTP";
860 break;
861 case WPA_CTRL_REQ_EAP_PASSPHRASE:
862 *txt = "Private key passphrase";
863 ret = "PASSPHRASE";
864 break;
a5d44ac0
JM
865 case WPA_CTRL_REQ_SIM:
866 ret = "SIM";
867 break;
a52410c2
JM
868 case WPA_CTRL_REQ_PSK_PASSPHRASE:
869 *txt = "PSK or passphrase";
870 ret = "PSK_PASSPHRASE";
871 break;
3c108b75
JM
872 case WPA_CTRL_REQ_EXT_CERT_CHECK:
873 *txt = "External server certificate validation";
874 ret = "EXT_CERT_CHECK";
875 break;
9ef1aaae
DW
876 default:
877 break;
878 }
879
880 /* txt needs to be something */
881 if (*txt == NULL) {
882 wpa_printf(MSG_WARNING, "No message for request %d", field);
883 ret = NULL;
884 }
885
886 return ret;
887}
888
a52410c2
JM
889
890void wpas_send_ctrl_req(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
891 const char *field_name, const char *txt)
892{
893 char *buf;
894 size_t buflen;
895 int len;
896
897 buflen = 100 + os_strlen(txt) + ssid->ssid_len;
898 buf = os_malloc(buflen);
899 if (buf == NULL)
900 return;
901 len = os_snprintf(buf, buflen, "%s-%d:%s needed for SSID ",
902 field_name, ssid->id, txt);
903 if (os_snprintf_error(buflen, len)) {
904 os_free(buf);
905 return;
906 }
907 if (ssid->ssid && buflen > len + ssid->ssid_len) {
908 os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
909 len += ssid->ssid_len;
910 buf[len] = '\0';
911 }
912 buf[buflen - 1] = '\0';
913 wpa_msg(wpa_s, MSG_INFO, WPA_CTRL_REQ "%s", buf);
914 os_free(buf);
915}
916
917
b1491202 918#ifdef IEEE8021X_EAPOL
6fc6879b 919#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
9ef1aaae
DW
920static void wpa_supplicant_eap_param_needed(void *ctx,
921 enum wpa_ctrl_req_type field,
922 const char *default_txt)
6fc6879b
JM
923{
924 struct wpa_supplicant *wpa_s = ctx;
925 struct wpa_ssid *ssid = wpa_s->current_ssid;
9ef1aaae 926 const char *field_name, *txt = NULL;
6fc6879b
JM
927
928 if (ssid == NULL)
929 return;
930
3c108b75
JM
931 if (field == WPA_CTRL_REQ_EXT_CERT_CHECK)
932 ssid->eap.pending_ext_cert_check = PENDING_CHECK;
a9022616
DW
933 wpas_notify_network_request(wpa_s, ssid, field, default_txt);
934
9ef1aaae
DW
935 field_name = wpa_supplicant_ctrl_req_to_string(field, default_txt,
936 &txt);
937 if (field_name == NULL) {
938 wpa_printf(MSG_WARNING, "Unhandled EAP param %d needed",
939 field);
940 return;
941 }
942
93c7e332
PS
943 wpas_notify_eap_status(wpa_s, "eap parameter needed", field_name);
944
a52410c2 945 wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
6fc6879b
JM
946}
947#else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
948#define wpa_supplicant_eap_param_needed NULL
949#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
950
951
dd5c155e 952#ifdef CONFIG_EAP_PROXY
a6f3761f 953
dd5c155e
SD
954static void wpa_supplicant_eap_proxy_cb(void *ctx)
955{
956 struct wpa_supplicant *wpa_s = ctx;
957 size_t len;
958
b5db6e5d 959 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
dd5c155e
SD
960 wpa_s->imsi, &len);
961 if (wpa_s->mnc_len > 0) {
962 wpa_s->imsi[len] = '\0';
963 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
964 wpa_s->imsi, wpa_s->mnc_len);
965 } else {
966 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
967 }
968}
a6f3761f
PK
969
970
c167662d
PK
971static void wpa_sm_sim_state_error_handler(struct wpa_supplicant *wpa_s)
972{
973 int i;
974 struct wpa_ssid *ssid;
975 const struct eap_method_type *eap_methods;
976
977 if (!wpa_s->conf)
978 return;
979
980 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
981 eap_methods = ssid->eap.eap_methods;
982 if (!eap_methods)
983 continue;
984
985 for (i = 0; eap_methods[i].method != EAP_TYPE_NONE; i++) {
986 if (eap_methods[i].vendor == EAP_VENDOR_IETF &&
987 (eap_methods[i].method == EAP_TYPE_SIM ||
988 eap_methods[i].method == EAP_TYPE_AKA ||
989 eap_methods[i].method == EAP_TYPE_AKA_PRIME)) {
990 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
991 break;
992 }
993 }
994 }
995}
996
997
a6f3761f
PK
998static void
999wpa_supplicant_eap_proxy_notify_sim_status(void *ctx,
1000 enum eap_proxy_sim_state sim_state)
1001{
1002 struct wpa_supplicant *wpa_s = ctx;
1003
1004 wpa_printf(MSG_DEBUG, "eap_proxy: SIM card status %u", sim_state);
1005 switch (sim_state) {
1006 case SIM_STATE_ERROR:
c167662d 1007 wpa_sm_sim_state_error_handler(wpa_s);
a6f3761f
PK
1008 break;
1009 default:
1010 wpa_printf(MSG_DEBUG, "eap_proxy: SIM card status unknown");
1011 break;
1012 }
1013}
1014
dd5c155e
SD
1015#endif /* CONFIG_EAP_PROXY */
1016
1017
4bc181ec
JM
1018static void wpa_supplicant_port_cb(void *ctx, int authorized)
1019{
1020 struct wpa_supplicant *wpa_s = ctx;
69a6b47a
JM
1021#ifdef CONFIG_AP
1022 if (wpa_s->ap_iface) {
1023 wpa_printf(MSG_DEBUG, "AP mode active - skip EAPOL Supplicant "
1024 "port status: %s",
1025 authorized ? "Authorized" : "Unauthorized");
1026 return;
1027 }
1028#endif /* CONFIG_AP */
4bc181ec
JM
1029 wpa_printf(MSG_DEBUG, "EAPOL: Supplicant port status: %s",
1030 authorized ? "Authorized" : "Unauthorized");
1031 wpa_drv_set_supp_port(wpa_s, authorized);
1032}
ade74830
MC
1033
1034
bc0634da
JM
1035static void wpa_supplicant_cert_cb(void *ctx, struct tls_cert_data *cert,
1036 const char *cert_hash)
ade74830
MC
1037{
1038 struct wpa_supplicant *wpa_s = ctx;
1039
bc0634da 1040 wpas_notify_certification(wpa_s, cert, cert_hash);
ade74830 1041}
dd7fec1f
PS
1042
1043
1044static void wpa_supplicant_status_cb(void *ctx, const char *status,
1045 const char *parameter)
1046{
1047 struct wpa_supplicant *wpa_s = ctx;
1048
1049 wpas_notify_eap_status(wpa_s, status, parameter);
1050}
e026159a
JM
1051
1052
45f7574d
AE
1053static void wpa_supplicant_eap_error_cb(void *ctx, int error_code)
1054{
1055 struct wpa_supplicant *wpa_s = ctx;
1056
1057 wpas_notify_eap_error(wpa_s, error_code);
1058}
1059
1060
e026159a
JM
1061static void wpa_supplicant_set_anon_id(void *ctx, const u8 *id, size_t len)
1062{
1063 struct wpa_supplicant *wpa_s = ctx;
1064 char *str;
1065 int res;
1066
1067 wpa_hexdump_ascii(MSG_DEBUG, "EAP method updated anonymous_identity",
1068 id, len);
1069
1070 if (wpa_s->current_ssid == NULL)
1071 return;
1072
1073 if (id == NULL) {
1074 if (wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
1075 "NULL", 0) < 0)
1076 return;
1077 } else {
1078 str = os_malloc(len * 2 + 1);
1079 if (str == NULL)
1080 return;
1081 wpa_snprintf_hex(str, len * 2 + 1, id, len);
1082 res = wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
1083 str, 0);
1084 os_free(str);
1085 if (res < 0)
1086 return;
1087 }
1088
1089 if (wpa_s->conf->update_config) {
1090 res = wpa_config_write(wpa_s->confname, wpa_s->conf);
1091 if (res) {
1092 wpa_printf(MSG_DEBUG, "Failed to update config after "
1093 "anonymous_id update");
1094 }
1095 }
1096}
b1491202 1097#endif /* IEEE8021X_EAPOL */
4bc181ec
JM
1098
1099
6fc6879b
JM
1100int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
1101{
1102#ifdef IEEE8021X_EAPOL
1103 struct eapol_ctx *ctx;
1104 ctx = os_zalloc(sizeof(*ctx));
1105 if (ctx == NULL) {
1106 wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
1107 return -1;
1108 }
1109
1110 ctx->ctx = wpa_s;
1111 ctx->msg_ctx = wpa_s;
1112 ctx->eapol_send_ctx = wpa_s;
1113 ctx->preauth = 0;
1114 ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
1115 ctx->eapol_send = wpa_supplicant_eapol_send;
1116 ctx->set_wep_key = wpa_eapol_set_wep_key;
21f01a8e 1117#ifndef CONFIG_NO_CONFIG_BLOBS
6fc6879b
JM
1118 ctx->set_config_blob = wpa_supplicant_set_config_blob;
1119 ctx->get_config_blob = wpa_supplicant_get_config_blob;
21f01a8e 1120#endif /* CONFIG_NO_CONFIG_BLOBS */
6fc6879b 1121 ctx->aborted_cached = wpa_supplicant_aborted_cached;
6fc6879b
JM
1122 ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
1123 ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
1124 ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
07e2de31 1125 ctx->openssl_ciphers = wpa_s->conf->openssl_ciphers;
116654ce 1126 ctx->wps = wpa_s->wps;
6fc6879b 1127 ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
dd5c155e
SD
1128#ifdef CONFIG_EAP_PROXY
1129 ctx->eap_proxy_cb = wpa_supplicant_eap_proxy_cb;
a6f3761f
PK
1130 ctx->eap_proxy_notify_sim_status =
1131 wpa_supplicant_eap_proxy_notify_sim_status;
dd5c155e 1132#endif /* CONFIG_EAP_PROXY */
4bc181ec 1133 ctx->port_cb = wpa_supplicant_port_cb;
6fc6879b 1134 ctx->cb = wpa_supplicant_eapol_cb;
ade74830 1135 ctx->cert_cb = wpa_supplicant_cert_cb;
483dd6a5 1136 ctx->cert_in_cb = wpa_s->conf->cert_in_cb;
dd7fec1f 1137 ctx->status_cb = wpa_supplicant_status_cb;
45f7574d 1138 ctx->eap_error_cb = wpa_supplicant_eap_error_cb;
e026159a 1139 ctx->set_anon_id = wpa_supplicant_set_anon_id;
6fc6879b
JM
1140 ctx->cb_ctx = wpa_s;
1141 wpa_s->eapol = eapol_sm_init(ctx);
1142 if (wpa_s->eapol == NULL) {
1143 os_free(ctx);
1144 wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
1145 "machines.");
1146 return -1;
1147 }
1148#endif /* IEEE8021X_EAPOL */
1149
1150 return 0;
1151}
1152
1153
77d7b090 1154#ifndef CONFIG_NO_WPA
124ddfa1 1155
98cd3d1c
JM
1156static void wpa_supplicant_set_rekey_offload(void *ctx,
1157 const u8 *kek, size_t kek_len,
1158 const u8 *kck, size_t kck_len,
b14a210c
JB
1159 const u8 *replay_ctr)
1160{
1161 struct wpa_supplicant *wpa_s = ctx;
1162
98cd3d1c 1163 wpa_drv_set_rekey_info(wpa_s, kek, kek_len, kck, kck_len, replay_ctr);
b14a210c
JB
1164}
1165
1166
b41f2684
CL
1167static int wpa_supplicant_key_mgmt_set_pmk(void *ctx, const u8 *pmk,
1168 size_t pmk_len)
1169{
1170 struct wpa_supplicant *wpa_s = ctx;
1171
a250722f
JM
1172 if (wpa_s->conf->key_mgmt_offload &&
1173 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
b41f2684 1174 return wpa_drv_set_key(wpa_s, WPA_ALG_PMK, NULL, 0, 0,
a919a260 1175 NULL, 0, pmk, pmk_len, KEY_FLAG_PMK);
b41f2684
CL
1176 else
1177 return 0;
1178}
124ddfa1
JM
1179
1180
1181static void wpa_supplicant_fils_hlp_rx(void *ctx, const u8 *dst, const u8 *src,
1182 const u8 *pkt, size_t pkt_len)
1183{
1184 struct wpa_supplicant *wpa_s = ctx;
1185 char *hex;
1186 size_t hexlen;
1187
1188 hexlen = pkt_len * 2 + 1;
1189 hex = os_malloc(hexlen);
1190 if (!hex)
1191 return;
1192 wpa_snprintf_hex(hex, hexlen, pkt, pkt_len);
1193 wpa_msg(wpa_s, MSG_INFO, FILS_HLP_RX "dst=" MACSTR " src=" MACSTR
1194 " frame=%s", MAC2STR(dst), MAC2STR(src), hex);
1195 os_free(hex);
1196}
1197
4b62b52e
MV
1198
1199static int wpa_supplicant_channel_info(void *_wpa_s,
1200 struct wpa_channel_info *ci)
1201{
1202 struct wpa_supplicant *wpa_s = _wpa_s;
1203
1204 return wpa_drv_channel_info(wpa_s, ci);
1205}
1206
9e68742e 1207#endif /* CONFIG_NO_WPA */
b41f2684
CL
1208
1209
6fc6879b
JM
1210int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
1211{
1212#ifndef CONFIG_NO_WPA
1213 struct wpa_sm_ctx *ctx;
1214 ctx = os_zalloc(sizeof(*ctx));
1215 if (ctx == NULL) {
1216 wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
1217 return -1;
1218 }
1219
1220 ctx->ctx = wpa_s;
0f057fb2 1221 ctx->msg_ctx = wpa_s;
6fc6879b
JM
1222 ctx->set_state = _wpa_supplicant_set_state;
1223 ctx->get_state = _wpa_supplicant_get_state;
6fc6879b 1224 ctx->deauthenticate = _wpa_supplicant_deauthenticate;
6fc6879b
JM
1225 ctx->set_key = wpa_supplicant_set_key;
1226 ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
1227 ctx->get_bssid = wpa_supplicant_get_bssid;
1228 ctx->ether_send = _wpa_ether_send;
1229 ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
1230 ctx->alloc_eapol = _wpa_alloc_eapol;
1231 ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
1232 ctx->add_pmkid = wpa_supplicant_add_pmkid;
1233 ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
1234#ifndef CONFIG_NO_CONFIG_BLOBS
1235 ctx->set_config_blob = wpa_supplicant_set_config_blob;
1236 ctx->get_config_blob = wpa_supplicant_get_config_blob;
1237#endif /* CONFIG_NO_CONFIG_BLOBS */
1238 ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
1239#ifdef CONFIG_IEEE80211R
1240 ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
1241 ctx->send_ft_action = wpa_supplicant_send_ft_action;
2a7e7f4e 1242 ctx->mark_authenticated = wpa_supplicant_mark_authenticated;
6fc6879b 1243#endif /* CONFIG_IEEE80211R */
281ff0aa 1244#ifdef CONFIG_TDLS
c58ab8f2 1245 ctx->tdls_get_capa = wpa_supplicant_tdls_get_capa;
281ff0aa
GP
1246 ctx->send_tdls_mgmt = wpa_supplicant_send_tdls_mgmt;
1247 ctx->tdls_oper = wpa_supplicant_tdls_oper;
45b722f1 1248 ctx->tdls_peer_addset = wpa_supplicant_tdls_peer_addset;
6b90deae
AN
1249 ctx->tdls_enable_channel_switch =
1250 wpa_supplicant_tdls_enable_channel_switch;
1251 ctx->tdls_disable_channel_switch =
1252 wpa_supplicant_tdls_disable_channel_switch;
281ff0aa 1253#endif /* CONFIG_TDLS */
b14a210c 1254 ctx->set_rekey_offload = wpa_supplicant_set_rekey_offload;
b41f2684 1255 ctx->key_mgmt_set_pmk = wpa_supplicant_key_mgmt_set_pmk;
124ddfa1 1256 ctx->fils_hlp_rx = wpa_supplicant_fils_hlp_rx;
4b62b52e 1257 ctx->channel_info = wpa_supplicant_channel_info;
6fc6879b
JM
1258
1259 wpa_s->wpa = wpa_sm_init(ctx);
1260 if (wpa_s->wpa == NULL) {
1261 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
1262 "machine");
ef035578 1263 os_free(ctx);
6fc6879b
JM
1264 return -1;
1265 }
1266#endif /* CONFIG_NO_WPA */
1267
1268 return 0;
1269}
1270
1271
1272void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
1273 struct wpa_ssid *ssid)
1274{
1275 struct rsn_supp_config conf;
1276 if (ssid) {
1277 os_memset(&conf, 0, sizeof(conf));
886a807f 1278 conf.network_ctx = ssid;
6fc6879b 1279 conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
a6a89fea 1280#ifdef IEEE8021X_EAPOL
6e202021
JM
1281 conf.proactive_key_caching = ssid->proactive_key_caching < 0 ?
1282 wpa_s->conf->okc : ssid->proactive_key_caching;
6fc6879b
JM
1283 conf.eap_workaround = ssid->eap_workaround;
1284 conf.eap_conf_ctx = &ssid->eap;
a6a89fea 1285#endif /* IEEE8021X_EAPOL */
6fc6879b
JM
1286 conf.ssid = ssid->ssid;
1287 conf.ssid_len = ssid->ssid_len;
581a8cde 1288 conf.wpa_ptk_rekey = ssid->wpa_ptk_rekey;
8b138d28 1289 conf.owe_ptk_workaround = ssid->owe_ptk_workaround;
25ef8529 1290#ifdef CONFIG_P2P
201b0f5f
JM
1291 if (ssid->p2p_group && wpa_s->current_bss &&
1292 !wpa_s->p2p_disable_ip_addr_req) {
25ef8529
JM
1293 struct wpabuf *p2p;
1294 p2p = wpa_bss_get_vendor_ie_multi(wpa_s->current_bss,
1295 P2P_IE_VENDOR_TYPE);
1296 if (p2p) {
1297 u8 group_capab;
1298 group_capab = p2p_get_group_capab(p2p);
1299 if (group_capab &
1300 P2P_GROUP_CAPAB_IP_ADDR_ALLOCATION)
1301 conf.p2p = 1;
1302 wpabuf_free(p2p);
1303 }
1304 }
1305#endif /* CONFIG_P2P */
73ed03f3 1306 conf.wpa_rsc_relaxation = wpa_s->conf->wpa_rsc_relaxation;
869af307
JM
1307#ifdef CONFIG_FILS
1308 if (wpa_key_mgmt_fils(wpa_s->key_mgmt))
1309 conf.fils_cache_id =
1310 wpa_bss_get_fils_cache_id(wpa_s->current_bss);
1311#endif /* CONFIG_FILS */
ecbf59e6 1312 conf.beacon_prot = ssid->beacon_prot;
6fc6879b
JM
1313 }
1314 wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);
1315}