Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * RSN pre-authentication (supplicant) |
Dmitry Shmidt | 40b0720 | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 3 | * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "includes.h" |
| 10 | |
| 11 | #include "common.h" |
| 12 | #include "wpa.h" |
| 13 | #include "eloop.h" |
| 14 | #include "l2_packet/l2_packet.h" |
| 15 | #include "eapol_supp/eapol_supp_sm.h" |
| 16 | #include "preauth.h" |
| 17 | #include "pmksa_cache.h" |
| 18 | #include "wpa_i.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 19 | |
| 20 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 21 | #if defined(IEEE8021X_EAPOL) && !defined(CONFIG_NO_WPA) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 22 | |
| 23 | #define PMKID_CANDIDATE_PRIO_SCAN 1000 |
| 24 | |
| 25 | |
| 26 | struct rsn_pmksa_candidate { |
| 27 | struct dl_list list; |
| 28 | u8 bssid[ETH_ALEN]; |
| 29 | int priority; |
| 30 | }; |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * pmksa_candidate_free - Free all entries in PMKSA candidate list |
| 35 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 36 | */ |
| 37 | void pmksa_candidate_free(struct wpa_sm *sm) |
| 38 | { |
| 39 | struct rsn_pmksa_candidate *entry, *n; |
| 40 | |
| 41 | if (sm == NULL) |
| 42 | return; |
| 43 | |
| 44 | dl_list_for_each_safe(entry, n, &sm->pmksa_candidates, |
| 45 | struct rsn_pmksa_candidate, list) { |
| 46 | dl_list_del(&entry->list); |
| 47 | os_free(entry); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 52 | static int rsn_preauth_key_mgmt(int akmp) |
| 53 | { |
| 54 | return !!(akmp & (WPA_KEY_MGMT_IEEE8021X | |
| 55 | WPA_KEY_MGMT_IEEE8021X_SHA256 | |
| 56 | WPA_KEY_MGMT_IEEE8021X_SUITE_B | |
| 57 | WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)); |
| 58 | } |
| 59 | |
| 60 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 61 | static void rsn_preauth_receive(void *ctx, const u8 *src_addr, |
| 62 | const u8 *buf, size_t len) |
| 63 | { |
| 64 | struct wpa_sm *sm = ctx; |
| 65 | |
| 66 | wpa_printf(MSG_DEBUG, "RX pre-auth from " MACSTR, MAC2STR(src_addr)); |
| 67 | wpa_hexdump(MSG_MSGDUMP, "RX pre-auth", buf, len); |
| 68 | |
| 69 | if (sm->preauth_eapol == NULL || |
| 70 | is_zero_ether_addr(sm->preauth_bssid) || |
| 71 | os_memcmp(sm->preauth_bssid, src_addr, ETH_ALEN) != 0) { |
| 72 | wpa_printf(MSG_WARNING, "RSN pre-auth frame received from " |
| 73 | "unexpected source " MACSTR " - dropped", |
| 74 | MAC2STR(src_addr)); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | eapol_sm_rx_eapol(sm->preauth_eapol, src_addr, buf, len); |
| 79 | } |
| 80 | |
| 81 | |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 82 | static void rsn_preauth_eapol_cb(struct eapol_sm *eapol, |
| 83 | enum eapol_supp_result result, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 84 | void *ctx) |
| 85 | { |
| 86 | struct wpa_sm *sm = ctx; |
| 87 | u8 pmk[PMK_LEN]; |
| 88 | |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 89 | if (result == EAPOL_SUPP_RESULT_SUCCESS) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 90 | int res, pmk_len; |
| 91 | pmk_len = PMK_LEN; |
| 92 | res = eapol_sm_get_key(eapol, pmk, PMK_LEN); |
| 93 | if (res) { |
| 94 | /* |
| 95 | * EAP-LEAP is an exception from other EAP methods: it |
| 96 | * uses only 16-byte PMK. |
| 97 | */ |
| 98 | res = eapol_sm_get_key(eapol, pmk, 16); |
| 99 | pmk_len = 16; |
| 100 | } |
| 101 | if (res == 0) { |
| 102 | wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from pre-auth", |
| 103 | pmk, pmk_len); |
| 104 | sm->pmk_len = pmk_len; |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 105 | pmksa_cache_add(sm->pmksa, pmk, pmk_len, NULL, |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 106 | NULL, 0, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 107 | sm->preauth_bssid, sm->own_addr, |
| 108 | sm->network_ctx, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 109 | WPA_KEY_MGMT_IEEE8021X, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 110 | } else { |
| 111 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 112 | "RSN: failed to get master session key from " |
| 113 | "pre-auth EAPOL state machines"); |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 114 | result = EAPOL_SUPP_RESULT_FAILURE; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "RSN: pre-authentication with " |
| 119 | MACSTR " %s", MAC2STR(sm->preauth_bssid), |
Dmitry Shmidt | 344abd3 | 2014-01-14 13:17:00 -0800 | [diff] [blame] | 120 | result == EAPOL_SUPP_RESULT_SUCCESS ? "completed successfully" : |
| 121 | "failed"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 122 | |
| 123 | rsn_preauth_deinit(sm); |
| 124 | rsn_preauth_candidate_process(sm); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | static void rsn_preauth_timeout(void *eloop_ctx, void *timeout_ctx) |
| 129 | { |
| 130 | struct wpa_sm *sm = eloop_ctx; |
| 131 | |
| 132 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "RSN: pre-authentication with " |
| 133 | MACSTR " timed out", MAC2STR(sm->preauth_bssid)); |
| 134 | rsn_preauth_deinit(sm); |
| 135 | rsn_preauth_candidate_process(sm); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | static int rsn_preauth_eapol_send(void *ctx, int type, const u8 *buf, |
| 140 | size_t len) |
| 141 | { |
| 142 | struct wpa_sm *sm = ctx; |
| 143 | u8 *msg; |
| 144 | size_t msglen; |
| 145 | int res; |
| 146 | |
| 147 | /* TODO: could add l2_packet_sendmsg that allows fragments to avoid |
| 148 | * extra copy here */ |
| 149 | |
| 150 | if (sm->l2_preauth == NULL) |
| 151 | return -1; |
| 152 | |
| 153 | msg = wpa_sm_alloc_eapol(sm, type, buf, len, &msglen, NULL); |
| 154 | if (msg == NULL) |
| 155 | return -1; |
| 156 | |
| 157 | wpa_hexdump(MSG_MSGDUMP, "TX EAPOL (preauth)", msg, msglen); |
| 158 | res = l2_packet_send(sm->l2_preauth, sm->preauth_bssid, |
| 159 | ETH_P_RSN_PREAUTH, msg, msglen); |
| 160 | os_free(msg); |
| 161 | return res; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | /** |
| 166 | * rsn_preauth_init - Start new RSN pre-authentication |
| 167 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 168 | * @dst: Authenticator address (BSSID) with which to preauthenticate |
| 169 | * @eap_conf: Current EAP configuration |
| 170 | * Returns: 0 on success, -1 on another pre-authentication is in progress, |
| 171 | * -2 on layer 2 packet initialization failure, -3 on EAPOL state machine |
| 172 | * initialization failure, -4 on memory allocation failure |
| 173 | * |
| 174 | * This function request an RSN pre-authentication with a given destination |
| 175 | * address. This is usually called for PMKSA candidates found from scan results |
| 176 | * or from driver reports. In addition, ctrl_iface PREAUTH command can trigger |
| 177 | * pre-authentication. |
| 178 | */ |
| 179 | int rsn_preauth_init(struct wpa_sm *sm, const u8 *dst, |
| 180 | struct eap_peer_config *eap_conf) |
| 181 | { |
| 182 | struct eapol_config eapol_conf; |
| 183 | struct eapol_ctx *ctx; |
Dmitry Shmidt | aff761d | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 184 | int ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 185 | |
| 186 | if (sm->preauth_eapol) |
| 187 | return -1; |
| 188 | |
| 189 | wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 190 | "RSN: starting pre-authentication with " MACSTR, MAC2STR(dst)); |
| 191 | |
| 192 | sm->l2_preauth = l2_packet_init(sm->ifname, sm->own_addr, |
| 193 | ETH_P_RSN_PREAUTH, |
| 194 | rsn_preauth_receive, sm, 0); |
| 195 | if (sm->l2_preauth == NULL) { |
| 196 | wpa_printf(MSG_WARNING, "RSN: Failed to initialize L2 packet " |
| 197 | "processing for pre-authentication"); |
| 198 | return -2; |
| 199 | } |
| 200 | |
| 201 | if (sm->bridge_ifname) { |
| 202 | sm->l2_preauth_br = l2_packet_init(sm->bridge_ifname, |
| 203 | sm->own_addr, |
| 204 | ETH_P_RSN_PREAUTH, |
| 205 | rsn_preauth_receive, sm, 0); |
| 206 | if (sm->l2_preauth_br == NULL) { |
| 207 | wpa_printf(MSG_WARNING, "RSN: Failed to initialize L2 " |
| 208 | "packet processing (bridge) for " |
| 209 | "pre-authentication"); |
Dmitry Shmidt | aff761d | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 210 | ret = -2; |
| 211 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | ctx = os_zalloc(sizeof(*ctx)); |
| 216 | if (ctx == NULL) { |
| 217 | wpa_printf(MSG_WARNING, "Failed to allocate EAPOL context."); |
Dmitry Shmidt | aff761d | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 218 | ret = -4; |
| 219 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 220 | } |
| 221 | ctx->ctx = sm->ctx->ctx; |
| 222 | ctx->msg_ctx = sm->ctx->ctx; |
| 223 | ctx->preauth = 1; |
| 224 | ctx->cb = rsn_preauth_eapol_cb; |
| 225 | ctx->cb_ctx = sm; |
| 226 | ctx->scard_ctx = sm->scard_ctx; |
| 227 | ctx->eapol_send = rsn_preauth_eapol_send; |
| 228 | ctx->eapol_send_ctx = sm; |
| 229 | ctx->set_config_blob = sm->ctx->set_config_blob; |
| 230 | ctx->get_config_blob = sm->ctx->get_config_blob; |
| 231 | |
| 232 | sm->preauth_eapol = eapol_sm_init(ctx); |
| 233 | if (sm->preauth_eapol == NULL) { |
| 234 | os_free(ctx); |
| 235 | wpa_printf(MSG_WARNING, "RSN: Failed to initialize EAPOL " |
| 236 | "state machines for pre-authentication"); |
Dmitry Shmidt | aff761d | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 237 | ret = -3; |
| 238 | goto fail; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 239 | } |
| 240 | os_memset(&eapol_conf, 0, sizeof(eapol_conf)); |
| 241 | eapol_conf.accept_802_1x_keys = 0; |
| 242 | eapol_conf.required_keys = 0; |
| 243 | eapol_conf.fast_reauth = sm->fast_reauth; |
| 244 | eapol_conf.workaround = sm->eap_workaround; |
| 245 | eapol_sm_notify_config(sm->preauth_eapol, eap_conf, &eapol_conf); |
| 246 | /* |
| 247 | * Use a shorter startPeriod with preauthentication since the first |
| 248 | * preauth EAPOL-Start frame may end up being dropped due to race |
| 249 | * condition in the AP between the data receive and key configuration |
| 250 | * after the 4-Way Handshake. |
| 251 | */ |
| 252 | eapol_sm_configure(sm->preauth_eapol, -1, -1, 5, 6); |
| 253 | os_memcpy(sm->preauth_bssid, dst, ETH_ALEN); |
| 254 | |
Hai Shalom | e21d4e8 | 2020-04-29 16:34:06 -0700 | [diff] [blame^] | 255 | eapol_sm_notify_portValid(sm->preauth_eapol, true); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 256 | /* 802.1X::portControl = Auto */ |
Hai Shalom | e21d4e8 | 2020-04-29 16:34:06 -0700 | [diff] [blame^] | 257 | eapol_sm_notify_portEnabled(sm->preauth_eapol, true); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 258 | |
| 259 | eloop_register_timeout(sm->dot11RSNAConfigSATimeout, 0, |
| 260 | rsn_preauth_timeout, sm, NULL); |
| 261 | |
| 262 | return 0; |
Dmitry Shmidt | aff761d | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 263 | |
| 264 | fail: |
| 265 | if (sm->l2_preauth_br) { |
| 266 | l2_packet_deinit(sm->l2_preauth_br); |
| 267 | sm->l2_preauth_br = NULL; |
| 268 | } |
| 269 | l2_packet_deinit(sm->l2_preauth); |
| 270 | sm->l2_preauth = NULL; |
| 271 | return ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | |
| 275 | /** |
| 276 | * rsn_preauth_deinit - Abort RSN pre-authentication |
| 277 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 278 | * |
| 279 | * This function aborts the current RSN pre-authentication (if one is started) |
| 280 | * and frees resources allocated for it. |
| 281 | */ |
| 282 | void rsn_preauth_deinit(struct wpa_sm *sm) |
| 283 | { |
| 284 | if (sm == NULL || !sm->preauth_eapol) |
| 285 | return; |
| 286 | |
| 287 | eloop_cancel_timeout(rsn_preauth_timeout, sm, NULL); |
| 288 | eapol_sm_deinit(sm->preauth_eapol); |
| 289 | sm->preauth_eapol = NULL; |
| 290 | os_memset(sm->preauth_bssid, 0, ETH_ALEN); |
| 291 | |
| 292 | l2_packet_deinit(sm->l2_preauth); |
| 293 | sm->l2_preauth = NULL; |
| 294 | if (sm->l2_preauth_br) { |
| 295 | l2_packet_deinit(sm->l2_preauth_br); |
| 296 | sm->l2_preauth_br = NULL; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | |
| 301 | /** |
| 302 | * rsn_preauth_candidate_process - Process PMKSA candidates |
| 303 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 304 | * |
| 305 | * Go through the PMKSA candidates and start pre-authentication if a candidate |
| 306 | * without an existing PMKSA cache entry is found. Processed candidates will be |
| 307 | * removed from the list. |
| 308 | */ |
| 309 | void rsn_preauth_candidate_process(struct wpa_sm *sm) |
| 310 | { |
| 311 | struct rsn_pmksa_candidate *candidate, *n; |
| 312 | |
| 313 | if (dl_list_empty(&sm->pmksa_candidates)) |
| 314 | return; |
| 315 | |
| 316 | /* TODO: drop priority for old candidate entries */ |
| 317 | |
| 318 | wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: processing PMKSA candidate " |
| 319 | "list"); |
| 320 | if (sm->preauth_eapol || |
| 321 | sm->proto != WPA_PROTO_RSN || |
| 322 | wpa_sm_get_state(sm) != WPA_COMPLETED || |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 323 | !rsn_preauth_key_mgmt(sm->key_mgmt)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 324 | wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: not in suitable " |
| 325 | "state for new pre-authentication"); |
| 326 | return; /* invalid state for new pre-auth */ |
| 327 | } |
| 328 | |
| 329 | dl_list_for_each_safe(candidate, n, &sm->pmksa_candidates, |
| 330 | struct rsn_pmksa_candidate, list) { |
| 331 | struct rsn_pmksa_cache_entry *p = NULL; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 332 | p = pmksa_cache_get(sm->pmksa, candidate->bssid, NULL, NULL, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 333 | if (os_memcmp(sm->bssid, candidate->bssid, ETH_ALEN) != 0 && |
| 334 | (p == NULL || p->opportunistic)) { |
| 335 | wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA " |
| 336 | "candidate " MACSTR |
| 337 | " selected for pre-authentication", |
| 338 | MAC2STR(candidate->bssid)); |
| 339 | dl_list_del(&candidate->list); |
| 340 | rsn_preauth_init(sm, candidate->bssid, |
| 341 | sm->eap_conf_ctx); |
| 342 | os_free(candidate); |
| 343 | return; |
| 344 | } |
| 345 | wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA candidate " |
| 346 | MACSTR " does not need pre-authentication anymore", |
| 347 | MAC2STR(candidate->bssid)); |
| 348 | /* Some drivers (e.g., NDIS) expect to get notified about the |
| 349 | * PMKIDs again, so report the existing data now. */ |
| 350 | if (p) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 351 | wpa_sm_add_pmkid(sm, NULL, candidate->bssid, p->pmkid, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 352 | NULL, p->pmk, p->pmk_len, 0, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | dl_list_del(&candidate->list); |
| 356 | os_free(candidate); |
| 357 | } |
| 358 | wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: no more pending PMKSA " |
| 359 | "candidates"); |
| 360 | } |
| 361 | |
| 362 | |
| 363 | /** |
| 364 | * pmksa_candidate_add - Add a new PMKSA candidate |
| 365 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 366 | * @bssid: BSSID (authenticator address) of the candidate |
| 367 | * @prio: Priority (the smaller number, the higher priority) |
| 368 | * @preauth: Whether the candidate AP advertises support for pre-authentication |
| 369 | * |
| 370 | * This function is used to add PMKSA candidates for RSN pre-authentication. It |
| 371 | * is called from scan result processing and from driver events for PMKSA |
| 372 | * candidates, i.e., EVENT_PMKID_CANDIDATE events to wpa_supplicant_event(). |
| 373 | */ |
| 374 | void pmksa_candidate_add(struct wpa_sm *sm, const u8 *bssid, |
| 375 | int prio, int preauth) |
| 376 | { |
| 377 | struct rsn_pmksa_candidate *cand, *pos; |
| 378 | |
| 379 | if (sm->network_ctx && sm->proactive_key_caching) |
| 380 | pmksa_cache_get_opportunistic(sm->pmksa, sm->network_ctx, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 381 | bssid, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 382 | |
| 383 | if (!preauth) { |
| 384 | wpa_printf(MSG_DEBUG, "RSN: Ignored PMKID candidate without " |
| 385 | "preauth flag"); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | /* If BSSID already on candidate list, update the priority of the old |
| 390 | * entry. Do not override priority based on normal scan results. */ |
| 391 | cand = NULL; |
| 392 | dl_list_for_each(pos, &sm->pmksa_candidates, |
| 393 | struct rsn_pmksa_candidate, list) { |
| 394 | if (os_memcmp(pos->bssid, bssid, ETH_ALEN) == 0) { |
| 395 | cand = pos; |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if (cand) { |
| 401 | dl_list_del(&cand->list); |
| 402 | if (prio < PMKID_CANDIDATE_PRIO_SCAN) |
| 403 | cand->priority = prio; |
| 404 | } else { |
| 405 | cand = os_zalloc(sizeof(*cand)); |
| 406 | if (cand == NULL) |
| 407 | return; |
| 408 | os_memcpy(cand->bssid, bssid, ETH_ALEN); |
| 409 | cand->priority = prio; |
| 410 | } |
| 411 | |
| 412 | /* Add candidate to the list; order by increasing priority value. i.e., |
| 413 | * highest priority (smallest value) first. */ |
| 414 | dl_list_for_each(pos, &sm->pmksa_candidates, |
| 415 | struct rsn_pmksa_candidate, list) { |
| 416 | if (cand->priority <= pos->priority) { |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 417 | if (!pos->list.prev) { |
| 418 | /* |
| 419 | * This cannot really happen in pracrice since |
| 420 | * pos was fetched from the list and the prev |
| 421 | * pointer must be set. It looks like clang |
| 422 | * static analyzer gets confused with the |
| 423 | * dl_list_del(&cand->list) call above and ends |
| 424 | * up assuming pos->list.prev could be NULL. |
| 425 | */ |
| 426 | os_free(cand); |
| 427 | return; |
| 428 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 429 | dl_list_add(pos->list.prev, &cand->list); |
| 430 | cand = NULL; |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | if (cand) |
| 435 | dl_list_add_tail(&sm->pmksa_candidates, &cand->list); |
| 436 | |
| 437 | wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: added PMKSA cache " |
| 438 | "candidate " MACSTR " prio %d", MAC2STR(bssid), prio); |
| 439 | rsn_preauth_candidate_process(sm); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | /* TODO: schedule periodic scans if current AP supports preauth */ |
| 444 | |
| 445 | /** |
| 446 | * rsn_preauth_scan_results - Start processing scan results for canditates |
| 447 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 448 | * Returns: 0 if ready to process results or -1 to skip processing |
| 449 | * |
| 450 | * This functions is used to notify RSN code about start of new scan results |
| 451 | * processing. The actual scan results will be provided by calling |
| 452 | * rsn_preauth_scan_result() for each BSS if this function returned 0. |
| 453 | */ |
| 454 | int rsn_preauth_scan_results(struct wpa_sm *sm) |
| 455 | { |
| 456 | if (sm->ssid_len == 0) |
| 457 | return -1; |
| 458 | |
| 459 | /* |
| 460 | * TODO: is it ok to free all candidates? What about the entries |
| 461 | * received from EVENT_PMKID_CANDIDATE? |
| 462 | */ |
| 463 | pmksa_candidate_free(sm); |
| 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | |
| 469 | /** |
| 470 | * rsn_preauth_scan_result - Processing scan result for PMKSA canditates |
| 471 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 472 | * |
| 473 | * Add all suitable APs (Authenticators) from scan results into PMKSA |
| 474 | * candidate list. |
| 475 | */ |
| 476 | void rsn_preauth_scan_result(struct wpa_sm *sm, const u8 *bssid, |
| 477 | const u8 *ssid, const u8 *rsn) |
| 478 | { |
| 479 | struct wpa_ie_data ie; |
| 480 | struct rsn_pmksa_cache_entry *pmksa; |
| 481 | |
| 482 | if (ssid[1] != sm->ssid_len || |
| 483 | os_memcmp(ssid + 2, sm->ssid, sm->ssid_len) != 0) |
| 484 | return; /* Not for the current SSID */ |
| 485 | |
| 486 | if (os_memcmp(bssid, sm->bssid, ETH_ALEN) == 0) |
| 487 | return; /* Ignore current AP */ |
| 488 | |
| 489 | if (wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie)) |
| 490 | return; |
| 491 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 492 | pmksa = pmksa_cache_get(sm->pmksa, bssid, NULL, NULL, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 493 | if (pmksa && (!pmksa->opportunistic || |
| 494 | !(ie.capabilities & WPA_CAPABILITY_PREAUTH))) |
| 495 | return; |
| 496 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 497 | if (!rsn_preauth_key_mgmt(ie.key_mgmt)) |
| 498 | return; |
| 499 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 500 | /* Give less priority to candidates found from normal scan results. */ |
| 501 | pmksa_candidate_add(sm, bssid, PMKID_CANDIDATE_PRIO_SCAN, |
| 502 | ie.capabilities & WPA_CAPABILITY_PREAUTH); |
| 503 | } |
| 504 | |
| 505 | |
| 506 | #ifdef CONFIG_CTRL_IFACE |
| 507 | /** |
| 508 | * rsn_preauth_get_status - Get pre-authentication status |
| 509 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 510 | * @buf: Buffer for status information |
| 511 | * @buflen: Maximum buffer length |
| 512 | * @verbose: Whether to include verbose status information |
| 513 | * Returns: Number of bytes written to buf. |
| 514 | * |
| 515 | * Query WPA2 pre-authentication for status information. This function fills in |
| 516 | * a text area with current status information. If the buffer (buf) is not |
| 517 | * large enough, status information will be truncated to fit the buffer. |
| 518 | */ |
| 519 | int rsn_preauth_get_status(struct wpa_sm *sm, char *buf, size_t buflen, |
| 520 | int verbose) |
| 521 | { |
| 522 | char *pos = buf, *end = buf + buflen; |
| 523 | int res, ret; |
| 524 | |
| 525 | if (sm->preauth_eapol) { |
| 526 | ret = os_snprintf(pos, end - pos, "Pre-authentication " |
| 527 | "EAPOL state machines:\n"); |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 528 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 529 | return pos - buf; |
| 530 | pos += ret; |
| 531 | res = eapol_sm_get_status(sm->preauth_eapol, |
| 532 | pos, end - pos, verbose); |
| 533 | if (res >= 0) |
| 534 | pos += res; |
| 535 | } |
| 536 | |
| 537 | return pos - buf; |
| 538 | } |
| 539 | #endif /* CONFIG_CTRL_IFACE */ |
| 540 | |
| 541 | |
| 542 | /** |
| 543 | * rsn_preauth_in_progress - Verify whether pre-authentication is in progress |
| 544 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 545 | */ |
| 546 | int rsn_preauth_in_progress(struct wpa_sm *sm) |
| 547 | { |
| 548 | return sm->preauth_eapol != NULL; |
| 549 | } |
| 550 | |
Dmitry Shmidt | 1d6bf42 | 2016-01-19 15:51:35 -0800 | [diff] [blame] | 551 | #endif /* IEEE8021X_EAPOL && !CONFIG_NO_WPA */ |