1 /*
2 * WPA Supplicant - Glue code to setup EAPOL and RSN modules
3 * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "eapol_supp/eapol_supp_sm.h"
13 #include "eap_peer/eap.h"
14 #include "rsn_supp/wpa.h"
15 #include "eloop.h"
16 #include "config.h"
17 #include "l2_packet/l2_packet.h"
18 #include "common/wpa_common.h"
19 #include "common/ptksa_cache.h"
20 #include "wpa_supplicant_i.h"
21 #include "driver_i.h"
22 #include "rsn_supp/pmksa_cache.h"
23 #include "sme.h"
24 #include "common/ieee802_11_defs.h"
25 #include "common/wpa_ctrl.h"
26 #include "wpas_glue.h"
27 #include "wps_supplicant.h"
28 #include "bss.h"
29 #include "scan.h"
30 #include "notify.h"
31 #include "wpas_kay.h"
32
33
34 #ifndef CONFIG_NO_CONFIG_BLOBS
35 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
wpa_supplicant_set_config_blob(void * ctx,struct wpa_config_blob * blob)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 *
wpa_supplicant_get_config_blob(void * ctx,const char * name)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)
wpa_alloc_eapol(const struct wpa_supplicant * wpa_s,u8 type,const void * data,u16 data_len,size_t * msg_len,void ** data_pos)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 */
wpa_ether_send(struct wpa_supplicant * wpa_s,const u8 * dest,u16 proto,const u8 * buf,size_t len)98 int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest,
99 u16 proto, const u8 *buf, size_t len)
100 {
101 #ifdef CONFIG_TESTING_OPTIONS
102 if (wpa_s->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
103 size_t hex_len = 2 * len + 1;
104 char *hex = os_malloc(hex_len);
105
106 if (hex == NULL)
107 return -1;
108 wpa_snprintf_hex(hex, hex_len, buf, len);
109 wpa_msg(wpa_s, MSG_INFO, "EAPOL-TX " MACSTR " %s",
110 MAC2STR(dest), hex);
111 os_free(hex);
112 return 0;
113 }
114 #endif /* CONFIG_TESTING_OPTIONS */
115
116 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_CONTROL_PORT) {
117 int encrypt = wpa_s->wpa &&
118 wpa_sm_has_ptk_installed(wpa_s->wpa);
119
120 return wpa_drv_tx_control_port(wpa_s, dest, proto, buf, len,
121 !encrypt);
122 }
123
124 if (wpa_s->l2) {
125 return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
126 }
127
128 return -1;
129 }
130 #endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
131
132
133 #ifdef IEEE8021X_EAPOL
134
135 /**
136 * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
137 * @ctx: Pointer to wpa_supplicant data (wpa_s)
138 * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
139 * @buf: EAPOL payload (after IEEE 802.1X header)
140 * @len: EAPOL payload length
141 * Returns: >=0 on success, <0 on failure
142 *
143 * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
144 * to the current Authenticator.
145 */
wpa_supplicant_eapol_send(void * ctx,int type,const u8 * buf,size_t len)146 static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
147 size_t len)
148 {
149 struct wpa_supplicant *wpa_s = ctx;
150 u8 *msg, *dst, bssid[ETH_ALEN];
151 size_t msglen;
152 int res;
153
154 /* TODO: could add l2_packet_sendmsg that allows fragments to avoid
155 * extra copy here */
156
157 if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
158 wpa_s->key_mgmt == WPA_KEY_MGMT_OWE ||
159 wpa_s->key_mgmt == WPA_KEY_MGMT_DPP ||
160 wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
161 /* Current SSID is not using IEEE 802.1X/EAP, so drop possible
162 * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
163 * machines. */
164 wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
165 "mode (type=%d len=%lu)", type,
166 (unsigned long) len);
167 return -1;
168 }
169
170 if (pmksa_cache_get_current(wpa_s->wpa) &&
171 type == IEEE802_1X_TYPE_EAPOL_START) {
172 /*
173 * We were trying to use PMKSA caching and sending EAPOL-Start
174 * would abort that and trigger full EAPOL authentication.
175 * However, we've already waited for the AP/Authenticator to
176 * start 4-way handshake or EAP authentication, and apparently
177 * it has not done so since the startWhen timer has reached zero
178 * to get the state machine sending EAPOL-Start. This is not
179 * really supposed to happen, but an interoperability issue with
180 * a deployed AP has been identified where the connection fails
181 * due to that AP failing to operate correctly if PMKID is
182 * included in the Association Request frame. To work around
183 * this, assume PMKSA caching failed and try to initiate full
184 * EAP authentication.
185 */
186 if (!wpa_s->current_ssid ||
187 wpa_s->current_ssid->eap_workaround) {
188 wpa_printf(MSG_DEBUG,
189 "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");
190 } else {
191 wpa_printf(MSG_DEBUG,
192 "RSN: PMKSA caching - do not send EAPOL-Start");
193 return -1;
194 }
195 }
196
197 if (is_zero_ether_addr(wpa_s->bssid)) {
198 wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
199 "EAPOL frame");
200 if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
201 !is_zero_ether_addr(bssid)) {
202 dst = bssid;
203 wpa_printf(MSG_DEBUG, "Using current BSSID " MACSTR
204 " from the driver as the EAPOL destination",
205 MAC2STR(dst));
206 } else {
207 dst = wpa_s->last_eapol_src;
208 wpa_printf(MSG_DEBUG, "Using the source address of the"
209 " last received EAPOL frame " MACSTR " as "
210 "the EAPOL destination",
211 MAC2STR(dst));
212 }
213 } else {
214 /* BSSID was already set (from (Re)Assoc event, so use it as
215 * the EAPOL destination. */
216 dst = wpa_s->bssid;
217 }
218
219 msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
220 if (msg == NULL)
221 return -1;
222
223 wpa_printf(MSG_DEBUG, "TX EAPOL: dst=" MACSTR, MAC2STR(dst));
224 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
225 res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
226 os_free(msg);
227 return res;
228 }
229
230
231 #ifdef CONFIG_WEP
232 /**
233 * wpa_eapol_set_wep_key - set WEP key for the driver
234 * @ctx: Pointer to wpa_supplicant data (wpa_s)
235 * @unicast: 1 = individual unicast key, 0 = broadcast key
236 * @keyidx: WEP key index (0..3)
237 * @key: Pointer to key data
238 * @keylen: Key length in bytes
239 * Returns: 0 on success or < 0 on error.
240 */
wpa_eapol_set_wep_key(void * ctx,int unicast,int keyidx,const u8 * key,size_t keylen)241 static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
242 const u8 *key, size_t keylen)
243 {
244 struct wpa_supplicant *wpa_s = ctx;
245 if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
246 int cipher = (keylen == 5) ? WPA_CIPHER_WEP40 :
247 WPA_CIPHER_WEP104;
248 if (unicast)
249 wpa_s->pairwise_cipher = cipher;
250 else
251 wpa_s->group_cipher = cipher;
252 }
253 return wpa_drv_set_key(wpa_s, WPA_ALG_WEP,
254 unicast ? wpa_s->bssid : NULL,
255 keyidx, unicast, NULL, 0, key, keylen,
256 unicast ? KEY_FLAG_PAIRWISE_RX_TX :
257 KEY_FLAG_GROUP_RX_TX_DEFAULT);
258 }
259 #endif /* CONFIG_WEP */
260
261
wpa_supplicant_aborted_cached(void * ctx)262 static void wpa_supplicant_aborted_cached(void *ctx)
263 {
264 struct wpa_supplicant *wpa_s = ctx;
265 wpa_sm_aborted_cached(wpa_s->wpa);
266 }
267
268
result_str(enum eapol_supp_result result)269 static const char * result_str(enum eapol_supp_result result)
270 {
271 switch (result) {
272 case EAPOL_SUPP_RESULT_FAILURE:
273 return "FAILURE";
274 case EAPOL_SUPP_RESULT_SUCCESS:
275 return "SUCCESS";
276 case EAPOL_SUPP_RESULT_EXPECTED_FAILURE:
277 return "EXPECTED_FAILURE";
278 }
279 return "?";
280 }
281
282
wpa_supplicant_eapol_cb(struct eapol_sm * eapol,enum eapol_supp_result result,void * ctx)283 static void wpa_supplicant_eapol_cb(struct eapol_sm *eapol,
284 enum eapol_supp_result result,
285 void *ctx)
286 {
287 struct wpa_supplicant *wpa_s = ctx;
288 int res, pmk_len;
289 u8 pmk[PMK_LEN];
290
291 wpa_printf(MSG_DEBUG, "EAPOL authentication completed - result=%s",
292 result_str(result));
293
294 if (wpas_wps_eapol_cb(wpa_s) > 0)
295 return;
296
297 wpa_s->eap_expected_failure = result ==
298 EAPOL_SUPP_RESULT_EXPECTED_FAILURE;
299
300 if (result != EAPOL_SUPP_RESULT_SUCCESS) {
301 int timeout = 2;
302 /*
303 * Make sure we do not get stuck here waiting for long EAPOL
304 * timeout if the AP does not disconnect in case of
305 * authentication failure.
306 */
307 if (wpa_s->eapol_failed) {
308 wpa_printf(MSG_DEBUG,
309 "EAPOL authentication failed again and AP did not disconnect us");
310 timeout = 0;
311 }
312 wpa_s->eapol_failed = 1;
313 wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
314 } else {
315 wpa_s->eapol_failed = 0;
316 ieee802_1x_notify_create_actor(wpa_s, wpa_s->last_eapol_src);
317 }
318
319 #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
320 if (result != EAPOL_SUPP_RESULT_SUCCESS)
321 #else
322 if (result != EAPOL_SUPP_RESULT_SUCCESS ||
323 !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X))
324 #endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
325 return;
326
327 #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
328 if (wpa_ft_is_ft_protocol(wpa_s->wpa)) {
329 return;
330 }
331 #endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
332
333 if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt))
334 return;
335
336 wpa_printf(MSG_DEBUG, "Configure PMK for driver-based RSN 4-way "
337 "handshake");
338
339 pmk_len = PMK_LEN;
340 if (wpa_key_mgmt_ft(wpa_s->key_mgmt)) {
341 #ifdef CONFIG_IEEE80211R
342 u8 buf[2 * PMK_LEN];
343 wpa_printf(MSG_DEBUG, "RSN: Use FT XXKey as PMK for "
344 "driver-based 4-way hs and FT");
345 res = eapol_sm_get_key(eapol, buf, 2 * PMK_LEN);
346 if (res == 0) {
347 os_memcpy(pmk, buf + PMK_LEN, PMK_LEN);
348 os_memset(buf, 0, sizeof(buf));
349 }
350 #else /* CONFIG_IEEE80211R */
351 res = -1;
352 #endif /* CONFIG_IEEE80211R */
353 } else {
354 res = eapol_sm_get_key(eapol, pmk, PMK_LEN);
355 if (res) {
356 /*
357 * EAP-LEAP is an exception from other EAP methods: it
358 * uses only 16-byte PMK.
359 */
360 res = eapol_sm_get_key(eapol, pmk, 16);
361 pmk_len = 16;
362 }
363 }
364
365 if (res) {
366 wpa_printf(MSG_DEBUG, "Failed to get PMK from EAPOL state "
367 "machines");
368 return;
369 }
370
371 wpa_hexdump_key(MSG_DEBUG, "RSN: Configure PMK for driver-based 4-way "
372 "handshake", pmk, pmk_len);
373
374 if (wpa_drv_set_key(wpa_s, 0, NULL, 0, 0, NULL, 0, pmk,
375 pmk_len, KEY_FLAG_PMK)) {
376 wpa_printf(MSG_DEBUG, "Failed to set PMK to the driver");
377 }
378
379 wpa_supplicant_cancel_scan(wpa_s);
380 wpa_supplicant_cancel_auth_timeout(wpa_s);
381 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
382
383 }
384
385
wpa_supplicant_notify_eapol_done(void * ctx)386 static void wpa_supplicant_notify_eapol_done(void *ctx)
387 {
388 struct wpa_supplicant *wpa_s = ctx;
389 wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
390 if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
391 wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
392 } else {
393 wpa_supplicant_cancel_auth_timeout(wpa_s);
394 wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
395 }
396 }
397
398 #endif /* IEEE8021X_EAPOL */
399
400
401 #ifndef CONFIG_NO_WPA
402
wpa_get_beacon_ie(struct wpa_supplicant * wpa_s)403 static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
404 {
405 int ret = 0;
406 struct wpa_bss *curr = NULL, *bss;
407 struct wpa_ssid *ssid = wpa_s->current_ssid;
408 const u8 *ie;
409
410 dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
411 if (os_memcmp(bss->bssid, wpa_s->bssid, ETH_ALEN) != 0)
412 continue;
413 if (ssid == NULL ||
414 ((bss->ssid_len == ssid->ssid_len &&
415 os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) == 0) ||
416 ssid->ssid_len == 0)) {
417 curr = bss;
418 break;
419 }
420 #ifdef CONFIG_OWE
421 if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
422 (bss->flags & WPA_BSS_OWE_TRANSITION)) {
423 curr = bss;
424 break;
425 }
426 #endif /* CONFIG_OWE */
427 }
428
429 if (curr) {
430 ie = wpa_bss_get_vendor_ie(curr, WPA_IE_VENDOR_TYPE);
431 if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
432 ret = -1;
433
434 ie = wpa_bss_get_ie(curr, WLAN_EID_RSN);
435 if (wpa_sm_set_ap_rsn_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
436 ret = -1;
437
438 ie = wpa_bss_get_ie(curr, WLAN_EID_RSNX);
439 if (wpa_sm_set_ap_rsnxe(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
440 ret = -1;
441 } else {
442 ret = -1;
443 }
444
445 return ret;
446 }
447
448
wpa_supplicant_get_beacon_ie(void * ctx)449 static int wpa_supplicant_get_beacon_ie(void *ctx)
450 {
451 struct wpa_supplicant *wpa_s = ctx;
452 if (wpa_get_beacon_ie(wpa_s) == 0) {
453 return 0;
454 }
455
456 /* No WPA/RSN IE found in the cached scan results. Try to get updated
457 * scan results from the driver. */
458 if (wpa_supplicant_update_scan_results(wpa_s) < 0)
459 return -1;
460
461 return wpa_get_beacon_ie(wpa_s);
462 }
463
464
_wpa_alloc_eapol(void * wpa_s,u8 type,const void * data,u16 data_len,size_t * msg_len,void ** data_pos)465 static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
466 const void *data, u16 data_len,
467 size_t *msg_len, void **data_pos)
468 {
469 return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
470 }
471
472
_wpa_ether_send(void * wpa_s,const u8 * dest,u16 proto,const u8 * buf,size_t len)473 static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
474 const u8 *buf, size_t len)
475 {
476 return wpa_ether_send(wpa_s, dest, proto, buf, len);
477 }
478
479
_wpa_supplicant_cancel_auth_timeout(void * wpa_s)480 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
481 {
482 wpa_supplicant_cancel_auth_timeout(wpa_s);
483 }
484
485
_wpa_supplicant_set_state(void * wpa_s,enum wpa_states state)486 static void _wpa_supplicant_set_state(void *wpa_s, enum wpa_states state)
487 {
488 wpa_supplicant_set_state(wpa_s, state);
489 }
490
491
492 /**
493 * wpa_supplicant_get_state - Get the connection state
494 * @wpa_s: Pointer to wpa_supplicant data
495 * Returns: The current connection state (WPA_*)
496 */
wpa_supplicant_get_state(struct wpa_supplicant * wpa_s)497 static enum wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
498 {
499 return wpa_s->wpa_state;
500 }
501
502
_wpa_supplicant_get_state(void * wpa_s)503 static enum wpa_states _wpa_supplicant_get_state(void *wpa_s)
504 {
505 return wpa_supplicant_get_state(wpa_s);
506 }
507
508
_wpa_supplicant_deauthenticate(void * wpa_s,u16 reason_code)509 static void _wpa_supplicant_deauthenticate(void *wpa_s, u16 reason_code)
510 {
511 wpa_supplicant_deauthenticate(wpa_s, reason_code);
512 /* Schedule a scan to make sure we continue looking for networks */
513 wpa_supplicant_req_scan(wpa_s, 5, 0);
514 }
515
516
_wpa_supplicant_reconnect(void * wpa_s)517 static void _wpa_supplicant_reconnect(void *wpa_s)
518 {
519 wpa_supplicant_reconnect(wpa_s);
520 }
521
522
wpa_supplicant_get_network_ctx(void * wpa_s)523 static void * wpa_supplicant_get_network_ctx(void *wpa_s)
524 {
525 return wpa_supplicant_get_ssid(wpa_s);
526 }
527
528
wpa_supplicant_get_bssid(void * ctx,u8 * bssid)529 static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
530 {
531 struct wpa_supplicant *wpa_s = ctx;
532 return wpa_drv_get_bssid(wpa_s, bssid);
533 }
534
535
wpa_supplicant_set_key(void * _wpa_s,enum wpa_alg alg,const u8 * addr,int key_idx,int set_tx,const u8 * seq,size_t seq_len,const u8 * key,size_t key_len,enum key_flag key_flag)536 static int wpa_supplicant_set_key(void *_wpa_s, enum wpa_alg alg,
537 const u8 *addr, int key_idx, int set_tx,
538 const u8 *seq, size_t seq_len,
539 const u8 *key, size_t key_len,
540 enum key_flag key_flag)
541 {
542 struct wpa_supplicant *wpa_s = _wpa_s;
543 if (alg == WPA_ALG_TKIP && key_idx == 0 && key_len == 32) {
544 /* Clear the MIC error counter when setting a new PTK. */
545 wpa_s->mic_errors_seen = 0;
546 }
547 #ifdef CONFIG_TESTING_GET_GTK
548 if (key_idx > 0 && addr && is_broadcast_ether_addr(addr) &&
549 alg != WPA_ALG_NONE && key_len <= sizeof(wpa_s->last_gtk)) {
550 os_memcpy(wpa_s->last_gtk, key, key_len);
551 wpa_s->last_gtk_len = key_len;
552 }
553 #endif /* CONFIG_TESTING_GET_GTK */
554 #ifdef CONFIG_TESTING_OPTIONS
555 if (addr && !is_broadcast_ether_addr(addr) &&
556 !(key_flag & KEY_FLAG_MODIFY)) {
557 wpa_s->last_tk_alg = alg;
558 os_memcpy(wpa_s->last_tk_addr, addr, ETH_ALEN);
559 wpa_s->last_tk_key_idx = key_idx;
560 if (key)
561 os_memcpy(wpa_s->last_tk, key, key_len);
562 wpa_s->last_tk_len = key_len;
563 }
564 #endif /* CONFIG_TESTING_OPTIONS */
565 return wpa_drv_set_key(wpa_s, alg, addr, key_idx, set_tx, seq, seq_len,
566 key, key_len, key_flag);
567 }
568
569
wpa_supplicant_mlme_setprotection(void * wpa_s,const u8 * addr,int protection_type,int key_type)570 static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
571 int protection_type,
572 int key_type)
573 {
574 return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
575 key_type);
576 }
577
578
wpas_get_network_ctx(struct wpa_supplicant * wpa_s,void * network_ctx)579 static struct wpa_ssid * wpas_get_network_ctx(struct wpa_supplicant *wpa_s,
580 void *network_ctx)
581 {
582 struct wpa_ssid *ssid;
583
584 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
585 if (network_ctx == ssid)
586 return ssid;
587 }
588
589 return NULL;
590 }
591
592
wpa_supplicant_add_pmkid(void * _wpa_s,void * network_ctx,const u8 * bssid,const u8 * pmkid,const u8 * fils_cache_id,const u8 * pmk,size_t pmk_len,u32 pmk_lifetime,u8 pmk_reauth_threshold,int akmp)593 static int wpa_supplicant_add_pmkid(void *_wpa_s, void *network_ctx,
594 const u8 *bssid, const u8 *pmkid,
595 const u8 *fils_cache_id,
596 const u8 *pmk, size_t pmk_len,
597 u32 pmk_lifetime, u8 pmk_reauth_threshold,
598 int akmp)
599 {
600 struct wpa_supplicant *wpa_s = _wpa_s;
601 struct wpa_ssid *ssid;
602 struct wpa_pmkid_params params;
603
604 os_memset(¶ms, 0, sizeof(params));
605 ssid = wpas_get_network_ctx(wpa_s, network_ctx);
606 if (ssid) {
607 wpa_msg(wpa_s, MSG_INFO, PMKSA_CACHE_ADDED MACSTR " %d",
608 MAC2STR(bssid), ssid->id);
609 if ((akmp == WPA_KEY_MGMT_FT_IEEE8021X ||
610 akmp == WPA_KEY_MGMT_FT_IEEE8021X_SHA384) &&
611 !ssid->ft_eap_pmksa_caching) {
612 /* Since we will not be using PMKSA caching for FT-EAP
613 * within wpa_supplicant to avoid known interop issues
614 * with APs, do not add this PMKID to the driver either
615 * so that we won't be hitting those interop issues
616 * with driver-based RSNE generation. */
617 wpa_printf(MSG_DEBUG,
618 "FT: Do not add PMKID entry to the driver since FT-EAP PMKSA caching is not enabled in configuration");
619 return 0;
620 }
621 }
622 if (ssid && fils_cache_id) {
623 params.ssid = ssid->ssid;
624 params.ssid_len = ssid->ssid_len;
625 params.fils_cache_id = fils_cache_id;
626 } else {
627 params.bssid = bssid;
628 }
629
630 params.pmkid = pmkid;
631 params.pmk = pmk;
632 params.pmk_len = pmk_len;
633 params.pmk_lifetime = pmk_lifetime;
634 params.pmk_reauth_threshold = pmk_reauth_threshold;
635
636 return wpa_drv_add_pmkid(wpa_s, ¶ms);
637 }
638
639
wpa_supplicant_remove_pmkid(void * _wpa_s,void * network_ctx,const u8 * bssid,const u8 * pmkid,const u8 * fils_cache_id)640 static int wpa_supplicant_remove_pmkid(void *_wpa_s, void *network_ctx,
641 const u8 *bssid, const u8 *pmkid,
642 const u8 *fils_cache_id)
643 {
644 struct wpa_supplicant *wpa_s = _wpa_s;
645 struct wpa_ssid *ssid;
646 struct wpa_pmkid_params params;
647
648 os_memset(¶ms, 0, sizeof(params));
649 ssid = wpas_get_network_ctx(wpa_s, network_ctx);
650 if (ssid)
651 wpa_msg(wpa_s, MSG_INFO, PMKSA_CACHE_REMOVED MACSTR " %d",
652 MAC2STR(bssid), ssid->id);
653 if (ssid && fils_cache_id) {
654 params.ssid = ssid->ssid;
655 params.ssid_len = ssid->ssid_len;
656 params.fils_cache_id = fils_cache_id;
657 } else {
658 params.bssid = bssid;
659 }
660
661 params.pmkid = pmkid;
662
663 return wpa_drv_remove_pmkid(wpa_s, ¶ms);
664 }
665
666
667 #ifdef CONFIG_IEEE80211R
wpa_supplicant_update_ft_ies(void * ctx,const u8 * md,const u8 * ies,size_t ies_len)668 static int wpa_supplicant_update_ft_ies(void *ctx, const u8 *md,
669 const u8 *ies, size_t ies_len)
670 {
671 struct wpa_supplicant *wpa_s = ctx;
672 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
673 return sme_update_ft_ies(wpa_s, md, ies, ies_len);
674 return wpa_drv_update_ft_ies(wpa_s, md, ies, ies_len);
675 }
676
677
wpa_supplicant_send_ft_action(void * ctx,u8 action,const u8 * target_ap,const u8 * ies,size_t ies_len)678 static int wpa_supplicant_send_ft_action(void *ctx, u8 action,
679 const u8 *target_ap,
680 const u8 *ies, size_t ies_len)
681 {
682 struct wpa_supplicant *wpa_s = ctx;
683 int ret;
684 u8 *data, *pos;
685 size_t data_len;
686
687 if (action != 1) {
688 wpa_printf(MSG_ERROR, "Unsupported send_ft_action action %d",
689 action);
690 return -1;
691 }
692
693 /*
694 * Action frame payload:
695 * Category[1] = 6 (Fast BSS Transition)
696 * Action[1] = 1 (Fast BSS Transition Request)
697 * STA Address
698 * Target AP Address
699 * FT IEs
700 */
701
702 data_len = 2 + 2 * ETH_ALEN + ies_len;
703 data = os_malloc(data_len);
704 if (data == NULL)
705 return -1;
706 pos = data;
707 *pos++ = 0x06; /* FT Action category */
708 *pos++ = action;
709 os_memcpy(pos, wpa_s->own_addr, ETH_ALEN);
710 pos += ETH_ALEN;
711 os_memcpy(pos, target_ap, ETH_ALEN);
712 pos += ETH_ALEN;
713 os_memcpy(pos, ies, ies_len);
714
715 ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
716 wpa_s->bssid, wpa_s->own_addr, wpa_s->bssid,
717 data, data_len, 0);
718 os_free(data);
719
720 return ret;
721 }
722
723
wpa_supplicant_mark_authenticated(void * ctx,const u8 * target_ap)724 static int wpa_supplicant_mark_authenticated(void *ctx, const u8 *target_ap)
725 {
726 struct wpa_supplicant *wpa_s = ctx;
727 struct wpa_driver_auth_params params;
728 struct wpa_bss *bss;
729
730 bss = wpa_bss_get_bssid(wpa_s, target_ap);
731 if (bss == NULL)
732 return -1;
733
734 os_memset(¶ms, 0, sizeof(params));
735 params.bssid = target_ap;
736 params.freq = bss->freq;
737 params.ssid = bss->ssid;
738 params.ssid_len = bss->ssid_len;
739 params.auth_alg = WPA_AUTH_ALG_FT;
740 params.local_state_change = 1;
741 return wpa_drv_authenticate(wpa_s, ¶ms);
742 }
743 #endif /* CONFIG_IEEE80211R */
744
745
746 #ifdef CONFIG_TDLS
747
wpa_supplicant_tdls_get_capa(void * ctx,int * tdls_supported,int * tdls_ext_setup,int * tdls_chan_switch)748 static int wpa_supplicant_tdls_get_capa(void *ctx, int *tdls_supported,
749 int *tdls_ext_setup,
750 int *tdls_chan_switch)
751 {
752 struct wpa_supplicant *wpa_s = ctx;
753
754 *tdls_supported = 0;
755 *tdls_ext_setup = 0;
756 *tdls_chan_switch = 0;
757
758 if (!wpa_s->drv_capa_known)
759 return -1;
760
761 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)
762 *tdls_supported = 1;
763
764 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP)
765 *tdls_ext_setup = 1;
766
767 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH)
768 *tdls_chan_switch = 1;
769
770 return 0;
771 }
772
773
wpa_supplicant_send_tdls_mgmt(void * ctx,const u8 * dst,u8 action_code,u8 dialog_token,u16 status_code,u32 peer_capab,int initiator,const u8 * buf,size_t len)774 static int wpa_supplicant_send_tdls_mgmt(void *ctx, const u8 *dst,
775 u8 action_code, u8 dialog_token,
776 u16 status_code, u32 peer_capab,
777 int initiator, const u8 *buf,
778 size_t len)
779 {
780 struct wpa_supplicant *wpa_s = ctx;
781 return wpa_drv_send_tdls_mgmt(wpa_s, dst, action_code, dialog_token,
782 status_code, peer_capab, initiator, buf,
783 len);
784 }
785
786
wpa_supplicant_tdls_oper(void * ctx,int oper,const u8 * peer)787 static int wpa_supplicant_tdls_oper(void *ctx, int oper, const u8 *peer)
788 {
789 struct wpa_supplicant *wpa_s = ctx;
790 return wpa_drv_tdls_oper(wpa_s, oper, peer);
791 }
792
793
wpa_supplicant_tdls_peer_addset(void * ctx,const u8 * peer,int add,u16 aid,u16 capability,const u8 * supp_rates,size_t supp_rates_len,const struct ieee80211_ht_capabilities * ht_capab,const struct ieee80211_vht_capabilities * vht_capab,const struct ieee80211_he_capabilities * he_capab,size_t he_capab_len,const struct ieee80211_he_6ghz_band_cap * he_6ghz_he_capab,u8 qosinfo,int wmm,const u8 * ext_capab,size_t ext_capab_len,const u8 * supp_channels,size_t supp_channels_len,const u8 * supp_oper_classes,size_t supp_oper_classes_len)794 static int wpa_supplicant_tdls_peer_addset(
795 void *ctx, const u8 *peer, int add, u16 aid, u16 capability,
796 const u8 *supp_rates, size_t supp_rates_len,
797 const struct ieee80211_ht_capabilities *ht_capab,
798 const struct ieee80211_vht_capabilities *vht_capab,
799 const struct ieee80211_he_capabilities *he_capab,
800 size_t he_capab_len,
801 const struct ieee80211_he_6ghz_band_cap *he_6ghz_he_capab,
802 u8 qosinfo, int wmm, const u8 *ext_capab, size_t ext_capab_len,
803 const u8 *supp_channels, size_t supp_channels_len,
804 const u8 *supp_oper_classes, size_t supp_oper_classes_len)
805 {
806 struct wpa_supplicant *wpa_s = ctx;
807 struct hostapd_sta_add_params params;
808
809 os_memset(¶ms, 0, sizeof(params));
810
811 params.addr = peer;
812 params.aid = aid;
813 params.capability = capability;
814 params.flags = WPA_STA_TDLS_PEER | WPA_STA_AUTHORIZED;
815
816 /*
817 * Don't rely only on qosinfo for WMM capability. It may be 0 even when
818 * present. Allow the WMM IE to also indicate QoS support.
819 */
820 if (wmm || qosinfo)
821 params.flags |= WPA_STA_WMM;
822
823 params.ht_capabilities = ht_capab;
824 params.vht_capabilities = vht_capab;
825 params.he_capab = he_capab;
826 params.he_capab_len = he_capab_len;
827 params.he_6ghz_capab = he_6ghz_he_capab;
828 params.qosinfo = qosinfo;
829 params.listen_interval = 0;
830 params.supp_rates = supp_rates;
831 params.supp_rates_len = supp_rates_len;
832 params.set = !add;
833 params.ext_capab = ext_capab;
834 params.ext_capab_len = ext_capab_len;
835 params.supp_channels = supp_channels;
836 params.supp_channels_len = supp_channels_len;
837 params.supp_oper_classes = supp_oper_classes;
838 params.supp_oper_classes_len = supp_oper_classes_len;
839
840 return wpa_drv_sta_add(wpa_s, ¶ms);
841 }
842
843
wpa_supplicant_tdls_enable_channel_switch(void * ctx,const u8 * addr,u8 oper_class,const struct hostapd_freq_params * params)844 static int wpa_supplicant_tdls_enable_channel_switch(
845 void *ctx, const u8 *addr, u8 oper_class,
846 const struct hostapd_freq_params *params)
847 {
848 struct wpa_supplicant *wpa_s = ctx;
849
850 return wpa_drv_tdls_enable_channel_switch(wpa_s, addr, oper_class,
851 params);
852 }
853
854
wpa_supplicant_tdls_disable_channel_switch(void * ctx,const u8 * addr)855 static int wpa_supplicant_tdls_disable_channel_switch(void *ctx, const u8 *addr)
856 {
857 struct wpa_supplicant *wpa_s = ctx;
858
859 return wpa_drv_tdls_disable_channel_switch(wpa_s, addr);
860 }
861
862 #endif /* CONFIG_TDLS */
863
864 #endif /* CONFIG_NO_WPA */
865
866
wpa_supplicant_ctrl_req_from_string(const char * field)867 enum wpa_ctrl_req_type wpa_supplicant_ctrl_req_from_string(const char *field)
868 {
869 if (os_strcmp(field, "IDENTITY") == 0)
870 return WPA_CTRL_REQ_EAP_IDENTITY;
871 else if (os_strcmp(field, "PASSWORD") == 0)
872 return WPA_CTRL_REQ_EAP_PASSWORD;
873 else if (os_strcmp(field, "NEW_PASSWORD") == 0)
874 return WPA_CTRL_REQ_EAP_NEW_PASSWORD;
875 else if (os_strcmp(field, "PIN") == 0)
876 return WPA_CTRL_REQ_EAP_PIN;
877 else if (os_strcmp(field, "OTP") == 0)
878 return WPA_CTRL_REQ_EAP_OTP;
879 else if (os_strcmp(field, "PASSPHRASE") == 0)
880 return WPA_CTRL_REQ_EAP_PASSPHRASE;
881 else if (os_strcmp(field, "SIM") == 0)
882 return WPA_CTRL_REQ_SIM;
883 else if (os_strcmp(field, "PSK_PASSPHRASE") == 0)
884 return WPA_CTRL_REQ_PSK_PASSPHRASE;
885 else if (os_strcmp(field, "EXT_CERT_CHECK") == 0)
886 return WPA_CTRL_REQ_EXT_CERT_CHECK;
887 return WPA_CTRL_REQ_UNKNOWN;
888 }
889
890
wpa_supplicant_ctrl_req_to_string(enum wpa_ctrl_req_type field,const char * default_txt,const char ** txt)891 const char * wpa_supplicant_ctrl_req_to_string(enum wpa_ctrl_req_type field,
892 const char *default_txt,
893 const char **txt)
894 {
895 const char *ret = NULL;
896
897 *txt = default_txt;
898
899 switch (field) {
900 case WPA_CTRL_REQ_EAP_IDENTITY:
901 *txt = "Identity";
902 ret = "IDENTITY";
903 break;
904 case WPA_CTRL_REQ_EAP_PASSWORD:
905 *txt = "Password";
906 ret = "PASSWORD";
907 break;
908 case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
909 *txt = "New Password";
910 ret = "NEW_PASSWORD";
911 break;
912 case WPA_CTRL_REQ_EAP_PIN:
913 *txt = "PIN";
914 ret = "PIN";
915 break;
916 case WPA_CTRL_REQ_EAP_OTP:
917 ret = "OTP";
918 break;
919 case WPA_CTRL_REQ_EAP_PASSPHRASE:
920 *txt = "Private key passphrase";
921 ret = "PASSPHRASE";
922 break;
923 case WPA_CTRL_REQ_SIM:
924 ret = "SIM";
925 break;
926 case WPA_CTRL_REQ_PSK_PASSPHRASE:
927 *txt = "PSK or passphrase";
928 ret = "PSK_PASSPHRASE";
929 break;
930 case WPA_CTRL_REQ_EXT_CERT_CHECK:
931 *txt = "External server certificate validation";
932 ret = "EXT_CERT_CHECK";
933 break;
934 default:
935 break;
936 }
937
938 /* txt needs to be something */
939 if (*txt == NULL) {
940 wpa_printf(MSG_WARNING, "No message for request %d", field);
941 ret = NULL;
942 }
943
944 return ret;
945 }
946
947
wpas_send_ctrl_req(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,const char * field_name,const char * txt)948 void wpas_send_ctrl_req(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
949 const char *field_name, const char *txt)
950 {
951 char *buf;
952 size_t buflen;
953 int len;
954
955 buflen = 100 + os_strlen(txt) + ssid->ssid_len;
956 buf = os_malloc(buflen);
957 if (buf == NULL)
958 return;
959 len = os_snprintf(buf, buflen, "%s-%d:%s needed for SSID ",
960 field_name, ssid->id, txt);
961 if (os_snprintf_error(buflen, len)) {
962 os_free(buf);
963 return;
964 }
965 if (ssid->ssid && buflen > len + ssid->ssid_len) {
966 os_memcpy(buf + len, ssid->ssid, ssid->ssid_len);
967 len += ssid->ssid_len;
968 buf[len] = '\0';
969 }
970 buf[buflen - 1] = '\0';
971 wpa_msg(wpa_s, MSG_INFO, WPA_CTRL_REQ "%s", buf);
972 os_free(buf);
973 }
974
975
976 #ifdef IEEE8021X_EAPOL
977 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
wpa_supplicant_eap_param_needed(void * ctx,enum wpa_ctrl_req_type field,const char * default_txt)978 static void wpa_supplicant_eap_param_needed(void *ctx,
979 enum wpa_ctrl_req_type field,
980 const char *default_txt)
981 {
982 struct wpa_supplicant *wpa_s = ctx;
983 struct wpa_ssid *ssid = wpa_s->current_ssid;
984 const char *field_name, *txt = NULL;
985
986 if (ssid == NULL)
987 return;
988
989 if (field == WPA_CTRL_REQ_EXT_CERT_CHECK)
990 ssid->eap.pending_ext_cert_check = PENDING_CHECK;
991 wpas_notify_network_request(wpa_s, ssid, field, default_txt);
992
993 field_name = wpa_supplicant_ctrl_req_to_string(field, default_txt,
994 &txt);
995 if (field_name == NULL) {
996 wpa_printf(MSG_WARNING, "Unhandled EAP param %d needed",
997 field);
998 return;
999 }
1000
1001 wpas_notify_eap_status(wpa_s, "eap parameter needed", field_name);
1002
1003 wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
1004 }
1005 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1006 #define wpa_supplicant_eap_param_needed NULL
1007 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1008
1009
1010 #ifdef CONFIG_EAP_PROXY
1011
wpa_supplicant_eap_proxy_cb(void * ctx)1012 static void wpa_supplicant_eap_proxy_cb(void *ctx)
1013 {
1014 struct wpa_supplicant *wpa_s = ctx;
1015 size_t len;
1016
1017 wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
1018 wpa_s->imsi, &len);
1019 if (wpa_s->mnc_len > 0) {
1020 wpa_s->imsi[len] = '\0';
1021 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
1022 wpa_s->imsi, wpa_s->mnc_len);
1023 } else {
1024 wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
1025 }
1026 }
1027
1028
wpa_sm_sim_state_error_handler(struct wpa_supplicant * wpa_s)1029 static void wpa_sm_sim_state_error_handler(struct wpa_supplicant *wpa_s)
1030 {
1031 int i;
1032 struct wpa_ssid *ssid;
1033 const struct eap_method_type *eap_methods;
1034
1035 if (!wpa_s->conf)
1036 return;
1037
1038 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
1039 eap_methods = ssid->eap.eap_methods;
1040 if (!eap_methods)
1041 continue;
1042
1043 for (i = 0; eap_methods[i].method != EAP_TYPE_NONE; i++) {
1044 if (eap_methods[i].vendor == EAP_VENDOR_IETF &&
1045 (eap_methods[i].method == EAP_TYPE_SIM ||
1046 eap_methods[i].method == EAP_TYPE_AKA ||
1047 eap_methods[i].method == EAP_TYPE_AKA_PRIME)) {
1048 wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
1049 break;
1050 }
1051 }
1052 }
1053 }
1054
1055
1056 static void
wpa_supplicant_eap_proxy_notify_sim_status(void * ctx,enum eap_proxy_sim_state sim_state)1057 wpa_supplicant_eap_proxy_notify_sim_status(void *ctx,
1058 enum eap_proxy_sim_state sim_state)
1059 {
1060 struct wpa_supplicant *wpa_s = ctx;
1061
1062 wpa_printf(MSG_DEBUG, "eap_proxy: SIM card status %u", sim_state);
1063 switch (sim_state) {
1064 case SIM_STATE_ERROR:
1065 wpa_sm_sim_state_error_handler(wpa_s);
1066 break;
1067 default:
1068 wpa_printf(MSG_DEBUG, "eap_proxy: SIM card status unknown");
1069 break;
1070 }
1071 }
1072
1073 #endif /* CONFIG_EAP_PROXY */
1074
1075
wpa_supplicant_port_cb(void * ctx,int authorized)1076 static void wpa_supplicant_port_cb(void *ctx, int authorized)
1077 {
1078 struct wpa_supplicant *wpa_s = ctx;
1079 #ifdef CONFIG_AP
1080 if (wpa_s->ap_iface) {
1081 wpa_printf(MSG_DEBUG, "AP mode active - skip EAPOL Supplicant "
1082 "port status: %s",
1083 authorized ? "Authorized" : "Unauthorized");
1084 return;
1085 }
1086 #endif /* CONFIG_AP */
1087 wpa_printf(MSG_DEBUG, "EAPOL: Supplicant port status: %s",
1088 authorized ? "Authorized" : "Unauthorized");
1089 wpa_drv_set_supp_port(wpa_s, authorized);
1090 }
1091
1092
wpa_supplicant_cert_cb(void * ctx,struct tls_cert_data * cert,const char * cert_hash)1093 static void wpa_supplicant_cert_cb(void *ctx, struct tls_cert_data *cert,
1094 const char *cert_hash)
1095 {
1096 struct wpa_supplicant *wpa_s = ctx;
1097
1098 wpas_notify_certification(wpa_s, cert, cert_hash);
1099 }
1100
1101
wpa_supplicant_status_cb(void * ctx,const char * status,const char * parameter)1102 static void wpa_supplicant_status_cb(void *ctx, const char *status,
1103 const char *parameter)
1104 {
1105 struct wpa_supplicant *wpa_s = ctx;
1106
1107 wpas_notify_eap_status(wpa_s, status, parameter);
1108 }
1109
1110
wpa_supplicant_eap_error_cb(void * ctx,int error_code)1111 static void wpa_supplicant_eap_error_cb(void *ctx, int error_code)
1112 {
1113 struct wpa_supplicant *wpa_s = ctx;
1114
1115 wpas_notify_eap_error(wpa_s, error_code);
1116 }
1117
1118
wpa_supplicant_eap_auth_start_cb(void * ctx)1119 static int wpa_supplicant_eap_auth_start_cb(void *ctx)
1120 {
1121 struct wpa_supplicant *wpa_s = ctx;
1122
1123 if (!wpa_s->new_connection && wpa_s->deny_ptk0_rekey &&
1124 !wpa_sm_ext_key_id_active(wpa_s->wpa)) {
1125 wpa_msg(wpa_s, MSG_INFO,
1126 "WPA: PTK0 rekey not allowed, reconnecting");
1127 wpa_supplicant_reconnect(wpa_s);
1128 return -1;
1129 }
1130 return 0;
1131 }
1132
1133
wpa_supplicant_set_anon_id(void * ctx,const u8 * id,size_t len)1134 static void wpa_supplicant_set_anon_id(void *ctx, const u8 *id, size_t len)
1135 {
1136 struct wpa_supplicant *wpa_s = ctx;
1137 char *str;
1138 int res;
1139
1140 wpa_hexdump_ascii(MSG_DEBUG, "EAP method updated anonymous_identity",
1141 id, len);
1142
1143 if (wpa_s->current_ssid == NULL)
1144 return;
1145
1146 if (id == NULL) {
1147 if (wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
1148 "NULL", 0) < 0)
1149 return;
1150 } else {
1151 str = os_malloc(len * 2 + 1);
1152 if (str == NULL)
1153 return;
1154 wpa_snprintf_hex(str, len * 2 + 1, id, len);
1155 res = wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
1156 str, 0);
1157 os_free(str);
1158 if (res < 0)
1159 return;
1160 }
1161
1162 if (wpa_s->conf->update_config) {
1163 res = wpa_config_write(wpa_s->confname, wpa_s->conf);
1164 if (res) {
1165 wpa_printf(MSG_DEBUG, "Failed to update config after "
1166 "anonymous_id update");
1167 }
1168 }
1169 }
1170
wpa_supplicant_eap_method_selected_cb(void * ctx,const char * reason_string)1171 static void wpa_supplicant_eap_method_selected_cb(void *ctx,
1172 const char* reason_string)
1173 {
1174 struct wpa_supplicant *wpa_s = ctx;
1175
1176 wpas_notify_eap_method_selected(wpa_s, reason_string);
1177 }
1178
wpa_supplicant_open_ssl_failure_cb(void * ctx,const char * reason_string)1179 static void wpa_supplicant_open_ssl_failure_cb(void *ctx,
1180 const char* reason_string)
1181 {
1182 struct wpa_supplicant *wpa_s = ctx;
1183
1184 wpas_notify_open_ssl_failure(wpa_s, reason_string);
1185 }
1186 #endif /* IEEE8021X_EAPOL */
1187
1188
wpa_supplicant_init_eapol(struct wpa_supplicant * wpa_s)1189 int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
1190 {
1191 #ifdef IEEE8021X_EAPOL
1192 struct eapol_ctx *ctx;
1193 ctx = os_zalloc(sizeof(*ctx));
1194 if (ctx == NULL) {
1195 wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
1196 return -1;
1197 }
1198
1199 ctx->ctx = wpa_s;
1200 ctx->msg_ctx = wpa_s;
1201 ctx->eapol_send_ctx = wpa_s;
1202 ctx->preauth = 0;
1203 ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
1204 ctx->eapol_send = wpa_supplicant_eapol_send;
1205 #ifdef CONFIG_WEP
1206 ctx->set_wep_key = wpa_eapol_set_wep_key;
1207 #endif /* CONFIG_WEP */
1208 #ifndef CONFIG_NO_CONFIG_BLOBS
1209 ctx->set_config_blob = wpa_supplicant_set_config_blob;
1210 ctx->get_config_blob = wpa_supplicant_get_config_blob;
1211 #endif /* CONFIG_NO_CONFIG_BLOBS */
1212 ctx->aborted_cached = wpa_supplicant_aborted_cached;
1213 ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
1214 ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
1215 ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
1216 ctx->openssl_ciphers = wpa_s->conf->openssl_ciphers;
1217 ctx->wps = wpa_s->wps;
1218 ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
1219 #ifdef CONFIG_EAP_PROXY
1220 ctx->eap_proxy_cb = wpa_supplicant_eap_proxy_cb;
1221 ctx->eap_proxy_notify_sim_status =
1222 wpa_supplicant_eap_proxy_notify_sim_status;
1223 #endif /* CONFIG_EAP_PROXY */
1224 ctx->port_cb = wpa_supplicant_port_cb;
1225 ctx->cb = wpa_supplicant_eapol_cb;
1226 ctx->cert_cb = wpa_supplicant_cert_cb;
1227 ctx->cert_in_cb = wpa_s->conf->cert_in_cb;
1228 ctx->status_cb = wpa_supplicant_status_cb;
1229 ctx->eap_error_cb = wpa_supplicant_eap_error_cb;
1230 ctx->confirm_auth_cb = wpa_supplicant_eap_auth_start_cb;
1231 ctx->set_anon_id = wpa_supplicant_set_anon_id;
1232 ctx->eap_method_selected_cb = wpa_supplicant_eap_method_selected_cb;
1233 ctx->open_ssl_failure_cb = wpa_supplicant_open_ssl_failure_cb;
1234 ctx->cb_ctx = wpa_s;
1235 wpa_s->eapol = eapol_sm_init(ctx);
1236 if (wpa_s->eapol == NULL) {
1237 os_free(ctx);
1238 wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
1239 "machines.");
1240 return -1;
1241 }
1242 #endif /* IEEE8021X_EAPOL */
1243
1244 return 0;
1245 }
1246
1247
1248 #ifndef CONFIG_NO_WPA
1249
wpa_supplicant_set_rekey_offload(void * ctx,const u8 * kek,size_t kek_len,const u8 * kck,size_t kck_len,const u8 * replay_ctr)1250 static void wpa_supplicant_set_rekey_offload(void *ctx,
1251 const u8 *kek, size_t kek_len,
1252 const u8 *kck, size_t kck_len,
1253 const u8 *replay_ctr)
1254 {
1255 struct wpa_supplicant *wpa_s = ctx;
1256
1257 wpa_drv_set_rekey_info(wpa_s, kek, kek_len, kck, kck_len, replay_ctr);
1258 }
1259
1260
wpa_supplicant_key_mgmt_set_pmk(void * ctx,const u8 * pmk,size_t pmk_len)1261 static int wpa_supplicant_key_mgmt_set_pmk(void *ctx, const u8 *pmk,
1262 size_t pmk_len)
1263 {
1264 struct wpa_supplicant *wpa_s = ctx;
1265
1266 if (wpa_s->conf->key_mgmt_offload &&
1267 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
1268 return wpa_drv_set_key(wpa_s, 0, NULL, 0, 0,
1269 NULL, 0, pmk, pmk_len, KEY_FLAG_PMK);
1270 else
1271 return 0;
1272 }
1273
1274
wpa_supplicant_fils_hlp_rx(void * ctx,const u8 * dst,const u8 * src,const u8 * pkt,size_t pkt_len)1275 static void wpa_supplicant_fils_hlp_rx(void *ctx, const u8 *dst, const u8 *src,
1276 const u8 *pkt, size_t pkt_len)
1277 {
1278 struct wpa_supplicant *wpa_s = ctx;
1279 char *hex;
1280 size_t hexlen;
1281
1282 hexlen = pkt_len * 2 + 1;
1283 hex = os_malloc(hexlen);
1284 if (!hex)
1285 return;
1286 wpa_snprintf_hex(hex, hexlen, pkt, pkt_len);
1287 wpa_msg(wpa_s, MSG_INFO, FILS_HLP_RX "dst=" MACSTR " src=" MACSTR
1288 " frame=%s", MAC2STR(dst), MAC2STR(src), hex);
1289 os_free(hex);
1290 }
1291
1292
wpa_supplicant_channel_info(void * _wpa_s,struct wpa_channel_info * ci)1293 static int wpa_supplicant_channel_info(void *_wpa_s,
1294 struct wpa_channel_info *ci)
1295 {
1296 struct wpa_supplicant *wpa_s = _wpa_s;
1297
1298 return wpa_drv_channel_info(wpa_s, ci);
1299 }
1300
1301
disable_wpa_wpa2(struct wpa_ssid * ssid)1302 static void disable_wpa_wpa2(struct wpa_ssid *ssid)
1303 {
1304 ssid->proto &= ~WPA_PROTO_WPA;
1305 ssid->proto |= WPA_PROTO_RSN;
1306 ssid->key_mgmt &= ~(WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK |
1307 WPA_KEY_MGMT_PSK_SHA256);
1308 ssid->group_cipher &= ~WPA_CIPHER_TKIP;
1309 if (!(ssid->group_cipher & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
1310 WPA_CIPHER_GCMP_256 | WPA_CIPHER_CCMP_256)))
1311 ssid->group_cipher |= WPA_CIPHER_CCMP;
1312 ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
1313 }
1314
1315
wpa_supplicant_transition_disable(void * _wpa_s,u8 bitmap)1316 static void wpa_supplicant_transition_disable(void *_wpa_s, u8 bitmap)
1317 {
1318 struct wpa_supplicant *wpa_s = _wpa_s;
1319 struct wpa_ssid *ssid;
1320 int changed = 0;
1321
1322 wpa_msg(wpa_s, MSG_INFO, TRANSITION_DISABLE "%02x", bitmap);
1323
1324 ssid = wpa_s->current_ssid;
1325 if (!ssid)
1326 return;
1327
1328 #ifdef CONFIG_SAE
1329 if ((bitmap & TRANSITION_DISABLE_WPA3_PERSONAL) &&
1330 wpa_key_mgmt_sae(wpa_s->key_mgmt) &&
1331 (ssid->key_mgmt & (WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE)) &&
1332 (ssid->ieee80211w != MGMT_FRAME_PROTECTION_REQUIRED ||
1333 (ssid->group_cipher & WPA_CIPHER_TKIP))) {
1334 wpa_printf(MSG_DEBUG,
1335 "WPA3-Personal transition mode disabled based on AP notification");
1336 disable_wpa_wpa2(ssid);
1337 changed = 1;
1338 }
1339
1340 if ((bitmap & TRANSITION_DISABLE_SAE_PK) &&
1341 wpa_key_mgmt_sae(wpa_s->key_mgmt) &&
1342 #ifdef CONFIG_SME
1343 wpa_s->sme.sae.state == SAE_ACCEPTED &&
1344 wpa_s->sme.sae.pk &&
1345 #endif /* CONFIG_SME */
1346 (ssid->key_mgmt & (WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE)) &&
1347 (ssid->sae_pk != SAE_PK_MODE_ONLY ||
1348 ssid->ieee80211w != MGMT_FRAME_PROTECTION_REQUIRED ||
1349 (ssid->group_cipher & WPA_CIPHER_TKIP))) {
1350 wpa_printf(MSG_DEBUG,
1351 "SAE-PK: SAE authentication without PK disabled based on AP notification");
1352 disable_wpa_wpa2(ssid);
1353 ssid->sae_pk = SAE_PK_MODE_ONLY;
1354 changed = 1;
1355 }
1356 #endif /* CONFIG_SAE */
1357
1358 if ((bitmap & TRANSITION_DISABLE_WPA3_ENTERPRISE) &&
1359 wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
1360 (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X |
1361 WPA_KEY_MGMT_FT_IEEE8021X |
1362 WPA_KEY_MGMT_IEEE8021X_SHA256)) &&
1363 (ssid->ieee80211w != MGMT_FRAME_PROTECTION_REQUIRED ||
1364 (ssid->group_cipher & WPA_CIPHER_TKIP))) {
1365 disable_wpa_wpa2(ssid);
1366 changed = 1;
1367 }
1368
1369 if ((bitmap & TRANSITION_DISABLE_ENHANCED_OPEN) &&
1370 wpa_s->key_mgmt == WPA_KEY_MGMT_OWE &&
1371 (ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
1372 !ssid->owe_only) {
1373 ssid->owe_only = 1;
1374 changed = 1;
1375 }
1376
1377 #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
1378 /* driver call for transition disable */
1379 {
1380 struct wpa_driver_associate_params params;
1381
1382 os_memset(¶ms, 0, sizeof(params));
1383 params.td_policy = bitmap;
1384 wpa_drv_update_connect_params(wpa_s, ¶ms, WPA_DRV_UPDATE_TD_POLICY);
1385 }
1386 #endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
1387
1388 wpas_notify_transition_disable(wpa_s, ssid, bitmap);
1389
1390 if (!changed)
1391 return;
1392
1393 #ifndef CONFIG_NO_CONFIG_WRITE
1394 if (wpa_s->conf->update_config &&
1395 wpa_config_write(wpa_s->confname, wpa_s->conf))
1396 wpa_printf(MSG_DEBUG, "Failed to update configuration");
1397 #endif /* CONFIG_NO_CONFIG_WRITE */
1398 }
1399
1400
wpa_supplicant_store_ptk(void * ctx,u8 * addr,int cipher,u32 life_time,const struct wpa_ptk * ptk)1401 static void wpa_supplicant_store_ptk(void *ctx, u8 *addr, int cipher,
1402 u32 life_time, const struct wpa_ptk *ptk)
1403 {
1404 struct wpa_supplicant *wpa_s = ctx;
1405
1406 ptksa_cache_add(wpa_s->ptksa, addr, cipher, life_time, ptk);
1407 }
1408
1409 #endif /* CONFIG_NO_WPA */
1410
1411
wpa_supplicant_init_wpa(struct wpa_supplicant * wpa_s)1412 int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
1413 {
1414 #ifndef CONFIG_NO_WPA
1415 struct wpa_sm_ctx *ctx;
1416
1417 wpa_s->ptksa = ptksa_cache_init();
1418 if (!wpa_s->ptksa) {
1419 wpa_printf(MSG_ERROR, "Failed to allocate PTKSA");
1420 return -1;
1421 }
1422
1423 ctx = os_zalloc(sizeof(*ctx));
1424 if (ctx == NULL) {
1425 wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
1426
1427 ptksa_cache_deinit(wpa_s->ptksa);
1428 wpa_s->ptksa = NULL;
1429
1430 return -1;
1431 }
1432
1433 ctx->ctx = wpa_s;
1434 ctx->msg_ctx = wpa_s;
1435 ctx->set_state = _wpa_supplicant_set_state;
1436 ctx->get_state = _wpa_supplicant_get_state;
1437 ctx->deauthenticate = _wpa_supplicant_deauthenticate;
1438 ctx->reconnect = _wpa_supplicant_reconnect;
1439 ctx->set_key = wpa_supplicant_set_key;
1440 ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
1441 ctx->get_bssid = wpa_supplicant_get_bssid;
1442 ctx->ether_send = _wpa_ether_send;
1443 ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
1444 ctx->alloc_eapol = _wpa_alloc_eapol;
1445 ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
1446 ctx->add_pmkid = wpa_supplicant_add_pmkid;
1447 ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
1448 #ifndef CONFIG_NO_CONFIG_BLOBS
1449 ctx->set_config_blob = wpa_supplicant_set_config_blob;
1450 ctx->get_config_blob = wpa_supplicant_get_config_blob;
1451 #endif /* CONFIG_NO_CONFIG_BLOBS */
1452 ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
1453 #ifdef CONFIG_IEEE80211R
1454 ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
1455 ctx->send_ft_action = wpa_supplicant_send_ft_action;
1456 ctx->mark_authenticated = wpa_supplicant_mark_authenticated;
1457 #endif /* CONFIG_IEEE80211R */
1458 #ifdef CONFIG_TDLS
1459 ctx->tdls_get_capa = wpa_supplicant_tdls_get_capa;
1460 ctx->send_tdls_mgmt = wpa_supplicant_send_tdls_mgmt;
1461 ctx->tdls_oper = wpa_supplicant_tdls_oper;
1462 ctx->tdls_peer_addset = wpa_supplicant_tdls_peer_addset;
1463 ctx->tdls_enable_channel_switch =
1464 wpa_supplicant_tdls_enable_channel_switch;
1465 ctx->tdls_disable_channel_switch =
1466 wpa_supplicant_tdls_disable_channel_switch;
1467 #endif /* CONFIG_TDLS */
1468 ctx->set_rekey_offload = wpa_supplicant_set_rekey_offload;
1469 ctx->key_mgmt_set_pmk = wpa_supplicant_key_mgmt_set_pmk;
1470 ctx->fils_hlp_rx = wpa_supplicant_fils_hlp_rx;
1471 ctx->channel_info = wpa_supplicant_channel_info;
1472 ctx->transition_disable = wpa_supplicant_transition_disable;
1473 ctx->store_ptk = wpa_supplicant_store_ptk;
1474
1475 wpa_s->wpa = wpa_sm_init(ctx);
1476 if (wpa_s->wpa == NULL) {
1477 wpa_printf(MSG_ERROR,
1478 "Failed to initialize WPA state machine");
1479 os_free(ctx);
1480 ptksa_cache_deinit(wpa_s->ptksa);
1481 wpa_s->ptksa = NULL;
1482 return -1;
1483 }
1484 #endif /* CONFIG_NO_WPA */
1485
1486 return 0;
1487 }
1488
1489
wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)1490 void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
1491 struct wpa_ssid *ssid)
1492 {
1493 struct rsn_supp_config conf;
1494 if (ssid) {
1495 os_memset(&conf, 0, sizeof(conf));
1496 conf.network_ctx = ssid;
1497 conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
1498 #ifdef IEEE8021X_EAPOL
1499 conf.proactive_key_caching = ssid->proactive_key_caching < 0 ?
1500 wpa_s->conf->okc : ssid->proactive_key_caching;
1501 conf.eap_workaround = ssid->eap_workaround;
1502 conf.eap_conf_ctx = &ssid->eap;
1503 #endif /* IEEE8021X_EAPOL */
1504 conf.ssid = ssid->ssid;
1505 conf.ssid_len = ssid->ssid_len;
1506 conf.wpa_ptk_rekey = ssid->wpa_ptk_rekey;
1507 conf.wpa_deny_ptk0_rekey = ssid->wpa_deny_ptk0_rekey;
1508 conf.owe_ptk_workaround = ssid->owe_ptk_workaround;
1509 #ifdef CONFIG_P2P
1510 if (ssid->p2p_group && wpa_s->current_bss &&
1511 !wpa_s->p2p_disable_ip_addr_req) {
1512 struct wpabuf *p2p;
1513 p2p = wpa_bss_get_vendor_ie_multi(wpa_s->current_bss,
1514 P2P_IE_VENDOR_TYPE);
1515 if (p2p) {
1516 u8 group_capab;
1517 group_capab = p2p_get_group_capab(p2p);
1518 if (group_capab &
1519 P2P_GROUP_CAPAB_IP_ADDR_ALLOCATION)
1520 conf.p2p = 1;
1521 wpabuf_free(p2p);
1522 }
1523 }
1524 #endif /* CONFIG_P2P */
1525 conf.wpa_rsc_relaxation = wpa_s->conf->wpa_rsc_relaxation;
1526 #ifdef CONFIG_FILS
1527 if (wpa_key_mgmt_fils(wpa_s->key_mgmt))
1528 conf.fils_cache_id =
1529 wpa_bss_get_fils_cache_id(wpa_s->current_bss);
1530 #endif /* CONFIG_FILS */
1531 if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_BEACON_PROTECTION) ||
1532 (wpa_s->drv_flags2 &
1533 WPA_DRIVER_FLAGS2_BEACON_PROTECTION_CLIENT))
1534 conf.beacon_prot = ssid->beacon_prot;
1535
1536 #ifdef CONFIG_PASN
1537 #ifdef CONFIG_TESTING_OPTIONS
1538 conf.force_kdk_derivation = wpa_s->conf->force_kdk_derivation;
1539 #endif /* CONFIG_TESTING_OPTIONS */
1540 #endif /* CONFIG_PASN */
1541 }
1542 wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);
1543 }
1544