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