b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * STA list |
| 3 | * Copyright (c) 2010-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 "utils/includes.h" |
| 10 | |
| 11 | #include "utils/common.h" |
| 12 | #include "common/defs.h" |
| 13 | #include "common/ieee802_11_defs.h" |
| 14 | #include "common/ieee802_11_common.h" |
| 15 | #include "wlantest.h" |
| 16 | |
| 17 | |
| 18 | struct wlantest_sta * sta_find(struct wlantest_bss *bss, const u8 *addr) |
| 19 | { |
| 20 | struct wlantest_sta *sta; |
| 21 | |
| 22 | dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) { |
| 23 | if (os_memcmp(sta->addr, addr, ETH_ALEN) == 0) |
| 24 | return sta; |
| 25 | } |
| 26 | |
| 27 | return NULL; |
| 28 | } |
| 29 | |
| 30 | |
| 31 | struct wlantest_sta * sta_find_mlo(struct wlantest *wt, |
| 32 | struct wlantest_bss *bss, const u8 *addr) |
| 33 | { |
| 34 | struct wlantest_sta *sta; |
| 35 | struct wlantest_bss *obss; |
| 36 | int link_id; |
| 37 | |
| 38 | dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) { |
| 39 | if (os_memcmp(sta->addr, addr, ETH_ALEN) == 0) |
| 40 | return sta; |
| 41 | } |
| 42 | |
| 43 | if (is_zero_ether_addr(addr)) |
| 44 | return NULL; |
| 45 | |
| 46 | dl_list_for_each(sta, &bss->sta, struct wlantest_sta, list) { |
| 47 | for (link_id = 0; link_id < MAX_NUM_MLO_LINKS; link_id++) { |
| 48 | if (os_memcmp(sta->link_addr[link_id], addr, |
| 49 | ETH_ALEN) == 0) |
| 50 | return sta; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | dl_list_for_each(obss, &wt->bss, struct wlantest_bss, list) { |
| 55 | if (obss == bss) |
| 56 | continue; |
| 57 | dl_list_for_each(sta, &obss->sta, struct wlantest_sta, list) { |
| 58 | if (os_memcmp(sta->addr, addr, ETH_ALEN) == 0) |
| 59 | return sta; |
| 60 | for (link_id = 0; link_id < MAX_NUM_MLO_LINKS; |
| 61 | link_id++) { |
| 62 | if (os_memcmp(sta->link_addr[link_id], addr, |
| 63 | ETH_ALEN) == 0) |
| 64 | return sta; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return NULL; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | struct wlantest_sta * sta_get(struct wlantest_bss *bss, const u8 *addr) |
| 74 | { |
| 75 | struct wlantest_sta *sta; |
| 76 | |
| 77 | if (addr[0] & 0x01) |
| 78 | return NULL; /* Skip group addressed frames */ |
| 79 | |
| 80 | sta = sta_find(bss, addr); |
| 81 | if (sta) |
| 82 | return sta; |
| 83 | |
| 84 | sta = os_zalloc(sizeof(*sta)); |
| 85 | if (sta == NULL) |
| 86 | return NULL; |
| 87 | os_memset(sta->seq_ctrl_to_sta, 0xff, sizeof(sta->seq_ctrl_to_sta)); |
| 88 | os_memset(sta->seq_ctrl_to_ap, 0xff, sizeof(sta->seq_ctrl_to_ap)); |
| 89 | sta->bss = bss; |
| 90 | os_memcpy(sta->addr, addr, ETH_ALEN); |
| 91 | dl_list_add(&bss->sta, &sta->list); |
| 92 | wpa_printf(MSG_DEBUG, "Discovered new STA " MACSTR " in BSS " MACSTR, |
| 93 | MAC2STR(sta->addr), MAC2STR(bss->bssid)); |
| 94 | return sta; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void sta_deinit(struct wlantest_sta *sta) |
| 99 | { |
| 100 | dl_list_del(&sta->list); |
| 101 | os_free(sta->assocreq_ies); |
| 102 | os_free(sta); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | static void sta_update_assoc_ml(struct wlantest_sta *sta, |
| 107 | struct ieee802_11_elems *elems) |
| 108 | { |
| 109 | const u8 *mld_addr; |
| 110 | |
| 111 | if (!elems->basic_mle) |
| 112 | return; |
| 113 | |
| 114 | mld_addr = get_basic_mle_mld_addr(elems->basic_mle, |
| 115 | elems->basic_mle_len); |
| 116 | if (!mld_addr) { |
| 117 | wpa_printf(MSG_INFO, "MLO: Invalid Basic Multi-Link element"); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | wpa_printf(MSG_DEBUG, "STA MLD Address: " MACSTR, MAC2STR(mld_addr)); |
| 122 | os_memcpy(sta->mld_mac_addr, mld_addr, ETH_ALEN); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | void sta_update_assoc(struct wlantest_sta *sta, struct ieee802_11_elems *elems) |
| 127 | { |
| 128 | struct wpa_ie_data data; |
| 129 | struct wlantest_bss *bss = sta->bss; |
| 130 | |
| 131 | if (elems->wpa_ie && !bss->wpaie[0] && |
| 132 | (bss->beacon_seen || bss->proberesp_seen)) { |
| 133 | wpa_printf(MSG_INFO, "WPA IE included in Association Request " |
| 134 | "frame from " MACSTR " even though BSS does not " |
| 135 | "use WPA - ignore IE", |
| 136 | MAC2STR(sta->addr)); |
| 137 | elems->wpa_ie = NULL; |
| 138 | } |
| 139 | |
| 140 | if (elems->rsn_ie && !bss->rsnie[0] && |
| 141 | (bss->beacon_seen || bss->proberesp_seen)) { |
| 142 | wpa_printf(MSG_INFO, "RSN IE included in Association Request " |
| 143 | "frame from " MACSTR " even though BSS does not " |
| 144 | "use RSN - ignore IE", |
| 145 | MAC2STR(sta->addr)); |
| 146 | elems->rsn_ie = NULL; |
| 147 | } |
| 148 | |
| 149 | if (elems->osen && !bss->osenie[0] && |
| 150 | (bss->beacon_seen || bss->proberesp_seen)) { |
| 151 | wpa_printf(MSG_INFO, "OSEN IE included in Association Request " |
| 152 | "frame from " MACSTR " even though BSS does not " |
| 153 | "use OSEN - ignore IE", |
| 154 | MAC2STR(sta->addr)); |
| 155 | elems->osen = NULL; |
| 156 | } |
| 157 | |
| 158 | if (elems->wpa_ie && elems->rsn_ie) { |
| 159 | wpa_printf(MSG_INFO, "Both WPA IE and RSN IE included in " |
| 160 | "Association Request frame from " MACSTR, |
| 161 | MAC2STR(sta->addr)); |
| 162 | } |
| 163 | |
| 164 | if (elems->rsn_ie) { |
| 165 | wpa_hexdump(MSG_DEBUG, "RSN IE", elems->rsn_ie - 2, |
| 166 | elems->rsn_ie_len + 2); |
| 167 | os_memcpy(sta->rsnie, elems->rsn_ie - 2, |
| 168 | elems->rsn_ie_len + 2); |
| 169 | if (wpa_parse_wpa_ie_rsn(sta->rsnie, 2 + sta->rsnie[1], &data) |
| 170 | < 0) { |
| 171 | wpa_printf(MSG_INFO, "Failed to parse RSN IE from " |
| 172 | MACSTR, MAC2STR(sta->addr)); |
| 173 | } |
| 174 | } else if (elems->wpa_ie) { |
| 175 | wpa_hexdump(MSG_DEBUG, "WPA IE", elems->wpa_ie - 2, |
| 176 | elems->wpa_ie_len + 2); |
| 177 | os_memcpy(sta->rsnie, elems->wpa_ie - 2, |
| 178 | elems->wpa_ie_len + 2); |
| 179 | if (wpa_parse_wpa_ie_wpa(sta->rsnie, 2 + sta->rsnie[1], &data) |
| 180 | < 0) { |
| 181 | wpa_printf(MSG_INFO, "Failed to parse WPA IE from " |
| 182 | MACSTR, MAC2STR(sta->addr)); |
| 183 | } |
| 184 | } else if (elems->osen) { |
| 185 | wpa_hexdump(MSG_DEBUG, "OSEN IE", elems->osen - 2, |
| 186 | elems->osen_len + 2); |
| 187 | os_memcpy(sta->osenie, elems->osen - 2, elems->osen_len + 2); |
| 188 | sta->proto = WPA_PROTO_OSEN; |
| 189 | sta->pairwise_cipher = WPA_CIPHER_CCMP; |
| 190 | sta->key_mgmt = WPA_KEY_MGMT_OSEN; |
| 191 | sta->rsn_capab = 0; |
| 192 | goto skip_rsn_wpa; |
| 193 | } else { |
| 194 | sta->rsnie[0] = 0; |
| 195 | sta->proto = 0; |
| 196 | sta->pairwise_cipher = 0; |
| 197 | sta->key_mgmt = 0; |
| 198 | sta->rsn_capab = 0; |
| 199 | if (sta->assocreq_capab_info & WLAN_CAPABILITY_PRIVACY) |
| 200 | sta->pairwise_cipher = WPA_CIPHER_WEP40; |
| 201 | goto skip_rsn_wpa; |
| 202 | } |
| 203 | |
| 204 | sta->proto = data.proto; |
| 205 | sta->pairwise_cipher = data.pairwise_cipher; |
| 206 | sta->key_mgmt = data.key_mgmt; |
| 207 | sta->rsn_capab = data.capabilities; |
| 208 | if (bss->proto && (sta->proto & bss->proto) == 0) { |
| 209 | wpa_printf(MSG_INFO, "Mismatch in WPA/WPA2 proto: STA " |
| 210 | MACSTR " 0x%x BSS " MACSTR " 0x%x", |
| 211 | MAC2STR(sta->addr), sta->proto, |
| 212 | MAC2STR(bss->bssid), bss->proto); |
| 213 | } |
| 214 | if (bss->pairwise_cipher && |
| 215 | (sta->pairwise_cipher & bss->pairwise_cipher) == 0) { |
| 216 | wpa_printf(MSG_INFO, "Mismatch in pairwise cipher: STA " |
| 217 | MACSTR " 0x%x BSS " MACSTR " 0x%x", |
| 218 | MAC2STR(sta->addr), sta->pairwise_cipher, |
| 219 | MAC2STR(bss->bssid), bss->pairwise_cipher); |
| 220 | } |
| 221 | if (sta->proto && data.group_cipher != bss->group_cipher && |
| 222 | bss->ies_set) { |
| 223 | wpa_printf(MSG_INFO, "Mismatch in group cipher: STA " |
| 224 | MACSTR " 0x%x != BSS " MACSTR " 0x%x", |
| 225 | MAC2STR(sta->addr), data.group_cipher, |
| 226 | MAC2STR(bss->bssid), bss->group_cipher); |
| 227 | } |
| 228 | if ((bss->rsn_capab & WPA_CAPABILITY_MFPR) && |
| 229 | !(sta->rsn_capab & WPA_CAPABILITY_MFPC)) { |
| 230 | wpa_printf(MSG_INFO, "STA " MACSTR " tries to associate " |
| 231 | "without MFP to BSS " MACSTR " that advertises " |
| 232 | "MFPR", MAC2STR(sta->addr), MAC2STR(bss->bssid)); |
| 233 | } |
| 234 | if ((sta->rsn_capab & WPA_CAPABILITY_OCVC) && |
| 235 | !(sta->rsn_capab & WPA_CAPABILITY_MFPC)) { |
| 236 | wpa_printf(MSG_INFO, "STA " MACSTR " tries to associate " |
| 237 | "without MFP to BSS " MACSTR " while supporting " |
| 238 | "OCV", MAC2STR(sta->addr), MAC2STR(bss->bssid)); |
| 239 | } |
| 240 | |
| 241 | skip_rsn_wpa: |
| 242 | wpa_printf(MSG_INFO, "STA " MACSTR |
| 243 | " proto=%s%s%s%s" |
| 244 | "pairwise=%s%s%s%s%s%s%s" |
| 245 | "key_mgmt=%s%s%s%s%s%s%s%s%s%s%s%s%s%s" |
| 246 | "rsn_capab=%s%s%s%s%s%s%s%s%s%s", |
| 247 | MAC2STR(sta->addr), |
| 248 | sta->proto == 0 ? "OPEN " : "", |
| 249 | sta->proto & WPA_PROTO_WPA ? "WPA " : "", |
| 250 | sta->proto & WPA_PROTO_RSN ? "WPA2 " : "", |
| 251 | sta->proto & WPA_PROTO_OSEN ? "OSEN " : "", |
| 252 | sta->pairwise_cipher == 0 ? "N/A " : "", |
| 253 | sta->pairwise_cipher & WPA_CIPHER_NONE ? "NONE " : "", |
| 254 | sta->pairwise_cipher & WPA_CIPHER_TKIP ? "TKIP " : "", |
| 255 | sta->pairwise_cipher & WPA_CIPHER_CCMP ? "CCMP " : "", |
| 256 | bss->pairwise_cipher & WPA_CIPHER_CCMP_256 ? "CCMP-256 " : |
| 257 | "", |
| 258 | bss->pairwise_cipher & WPA_CIPHER_GCMP ? "GCMP " : "", |
| 259 | bss->pairwise_cipher & WPA_CIPHER_GCMP_256 ? "GCMP-256 " : |
| 260 | "", |
| 261 | sta->key_mgmt == 0 ? "N/A " : "", |
| 262 | sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X ? "EAP " : "", |
| 263 | sta->key_mgmt & WPA_KEY_MGMT_PSK ? "PSK " : "", |
| 264 | sta->key_mgmt & WPA_KEY_MGMT_WPA_NONE ? "WPA-NONE " : "", |
| 265 | sta->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X ? "FT-EAP " : "", |
| 266 | sta->key_mgmt & WPA_KEY_MGMT_FT_PSK ? "FT-PSK " : "", |
| 267 | sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256 ? |
| 268 | "EAP-SHA256 " : "", |
| 269 | sta->key_mgmt & WPA_KEY_MGMT_PSK_SHA256 ? |
| 270 | "PSK-SHA256 " : "", |
| 271 | sta->key_mgmt & WPA_KEY_MGMT_OWE ? "OWE " : "", |
| 272 | sta->key_mgmt & WPA_KEY_MGMT_PASN ? "PASN " : "", |
| 273 | sta->key_mgmt & WPA_KEY_MGMT_OSEN ? "OSEN " : "", |
| 274 | sta->key_mgmt & WPA_KEY_MGMT_DPP ? "DPP " : "", |
| 275 | sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B ? |
| 276 | "EAP-SUITE-B " : "", |
| 277 | sta->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 ? |
| 278 | "EAP-SUITE-B-192 " : "", |
| 279 | sta->rsn_capab & WPA_CAPABILITY_PREAUTH ? "PREAUTH " : "", |
| 280 | sta->rsn_capab & WPA_CAPABILITY_NO_PAIRWISE ? |
| 281 | "NO_PAIRWISE " : "", |
| 282 | sta->rsn_capab & WPA_CAPABILITY_MFPR ? "MFPR " : "", |
| 283 | sta->rsn_capab & WPA_CAPABILITY_MFPC ? "MFPC " : "", |
| 284 | sta->rsn_capab & WPA_CAPABILITY_PEERKEY_ENABLED ? |
| 285 | "PEERKEY " : "", |
| 286 | sta->rsn_capab & WPA_CAPABILITY_SPP_A_MSDU_CAPABLE ? |
| 287 | "SPP-A-MSDU-CAPAB " : "", |
| 288 | sta->rsn_capab & WPA_CAPABILITY_SPP_A_MSDU_REQUIRED ? |
| 289 | "SPP-A-MSDU-REQUIRED " : "", |
| 290 | sta->rsn_capab & WPA_CAPABILITY_PBAC ? "PBAC " : "", |
| 291 | sta->rsn_capab & WPA_CAPABILITY_OCVC ? "OCVC " : "", |
| 292 | sta->rsn_capab & WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST ? |
| 293 | "ExtKeyID " : ""); |
| 294 | |
| 295 | sta_update_assoc_ml(sta, elems); |
| 296 | } |
| 297 | |
| 298 | |
| 299 | static void sta_copy_ptk(struct wlantest_sta *sta, struct wpa_ptk *ptk) |
| 300 | { |
| 301 | os_memcpy(&sta->ptk, ptk, sizeof(*ptk)); |
| 302 | sta->ptk_set = 1; |
| 303 | os_memset(sta->rsc_tods, 0, sizeof(sta->rsc_tods)); |
| 304 | os_memset(sta->rsc_fromds, 0, sizeof(sta->rsc_fromds)); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | void sta_new_ptk(struct wlantest *wt, struct wlantest_sta *sta, |
| 309 | struct wpa_ptk *ptk) |
| 310 | { |
| 311 | struct wlantest_bss *bss; |
| 312 | struct wlantest_sta *osta; |
| 313 | |
| 314 | add_note(wt, MSG_DEBUG, "Derived new PTK"); |
| 315 | sta_copy_ptk(sta, ptk); |
| 316 | wpa_hexdump(MSG_DEBUG, "PTK:KCK", sta->ptk.kck, sta->ptk.kck_len); |
| 317 | wpa_hexdump(MSG_DEBUG, "PTK:KEK", sta->ptk.kek, sta->ptk.kek_len); |
| 318 | wpa_hexdump(MSG_DEBUG, "PTK:TK", sta->ptk.tk, sta->ptk.tk_len); |
| 319 | |
| 320 | dl_list_for_each(bss, &wt->bss, struct wlantest_bss, list) { |
| 321 | dl_list_for_each(osta, &bss->sta, struct wlantest_sta, list) { |
| 322 | bool match = false; |
| 323 | int link_id; |
| 324 | |
| 325 | if (osta == sta) |
| 326 | continue; |
| 327 | if (os_memcmp(sta->addr, osta->addr, ETH_ALEN) == 0) |
| 328 | match = true; |
| 329 | for (link_id = 0; !match && link_id < MAX_NUM_MLO_LINKS; |
| 330 | link_id++) { |
| 331 | if (os_memcmp(osta->link_addr[link_id], |
| 332 | sta->addr, ETH_ALEN) == 0) |
| 333 | match = true; |
| 334 | } |
| 335 | |
| 336 | if (!match) |
| 337 | continue; |
| 338 | wpa_printf(MSG_DEBUG, |
| 339 | "Add PTK to another MLO STA entry " MACSTR |
| 340 | " (MLD " MACSTR " --> " MACSTR ") in BSS " |
| 341 | MACSTR " (MLD " MACSTR " --> " MACSTR ")", |
| 342 | MAC2STR(osta->addr), |
| 343 | MAC2STR(osta->mld_mac_addr), |
| 344 | MAC2STR(sta->mld_mac_addr), |
| 345 | MAC2STR(bss->bssid), |
| 346 | MAC2STR(bss->mld_mac_addr), |
| 347 | MAC2STR(sta->bss->mld_mac_addr)); |
| 348 | sta_copy_ptk(osta, ptk); |
| 349 | os_memcpy(osta->mld_mac_addr, sta->mld_mac_addr, |
| 350 | ETH_ALEN); |
| 351 | os_memcpy(osta->bss->mld_mac_addr, |
| 352 | sta->bss->mld_mac_addr, ETH_ALEN); |
| 353 | } |
| 354 | } |
| 355 | } |