1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * BSS client mode implementation 4 * Copyright 2003-2008, Jouni Malinen <j@w1.fi> 5 * Copyright 2004, Instant802 Networks, Inc. 6 * Copyright 2005, Devicescape Software, Inc. 7 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> 8 * Copyright 2007, Michael Wu <flamingice@sourmilk.net> 9 * Copyright 2013-2014 Intel Mobile Communications GmbH 10 * Copyright (C) 2015 - 2017 Intel Deutschland GmbH 11 * Copyright (C) 2018 - 2023 Intel Corporation 12 */ 13 14 #include <linux/delay.h> 15 #include <linux/fips.h> 16 #include <linux/if_ether.h> 17 #include <linux/skbuff.h> 18 #include <linux/if_arp.h> 19 #include <linux/etherdevice.h> 20 #include <linux/moduleparam.h> 21 #include <linux/rtnetlink.h> 22 #include <linux/crc32.h> 23 #include <linux/slab.h> 24 #include <linux/export.h> 25 #include <net/mac80211.h> 26 #include <asm/unaligned.h> 27 28 #include "ieee80211_i.h" 29 #include "driver-ops.h" 30 #include "rate.h" 31 #include "led.h" 32 #include "fils_aead.h" 33 34 #define IEEE80211_AUTH_TIMEOUT (HZ / 5) 35 #define IEEE80211_AUTH_TIMEOUT_LONG (HZ / 2) 36 #define IEEE80211_AUTH_TIMEOUT_SHORT (HZ / 10) 37 #define IEEE80211_AUTH_TIMEOUT_SAE (HZ * 2) 38 #define IEEE80211_AUTH_MAX_TRIES 3 39 #define IEEE80211_AUTH_WAIT_ASSOC (HZ * 5) 40 #define IEEE80211_AUTH_WAIT_SAE_RETRY (HZ * 2) 41 #define IEEE80211_ASSOC_TIMEOUT (HZ / 5) 42 #define IEEE80211_ASSOC_TIMEOUT_LONG (HZ / 2) 43 #define IEEE80211_ASSOC_TIMEOUT_SHORT (HZ / 10) 44 #define IEEE80211_ASSOC_MAX_TRIES 3 45 46 #define IEEE80211_ADV_TTLM_SAFETY_BUFFER_MS msecs_to_jiffies(100) 47 #define IEEE80211_ADV_TTLM_ST_UNDERFLOW 0xff00 48 49 static int max_nullfunc_tries = 2; 50 module_param(max_nullfunc_tries, int, 0644); 51 MODULE_PARM_DESC(max_nullfunc_tries, 52 "Maximum nullfunc tx tries before disconnecting (reason 4)."); 53 54 static int max_probe_tries = 5; 55 module_param(max_probe_tries, int, 0644); 56 MODULE_PARM_DESC(max_probe_tries, 57 "Maximum probe tries before disconnecting (reason 4)."); 58 59 /* 60 * Beacon loss timeout is calculated as N frames times the 61 * advertised beacon interval. This may need to be somewhat 62 * higher than what hardware might detect to account for 63 * delays in the host processing frames. But since we also 64 * probe on beacon miss before declaring the connection lost 65 * default to what we want. 66 */ 67 static int beacon_loss_count = 7; 68 module_param(beacon_loss_count, int, 0644); 69 MODULE_PARM_DESC(beacon_loss_count, 70 "Number of beacon intervals before we decide beacon was lost."); 71 72 /* 73 * Time the connection can be idle before we probe 74 * it to see if we can still talk to the AP. 75 */ 76 #define IEEE80211_CONNECTION_IDLE_TIME (30 * HZ) 77 /* 78 * Time we wait for a probe response after sending 79 * a probe request because of beacon loss or for 80 * checking the connection still works. 81 */ 82 static int probe_wait_ms = 500; 83 module_param(probe_wait_ms, int, 0644); 84 MODULE_PARM_DESC(probe_wait_ms, 85 "Maximum time(ms) to wait for probe response" 86 " before disconnecting (reason 4)."); 87 88 /* 89 * How many Beacon frames need to have been used in average signal strength 90 * before starting to indicate signal change events. 91 */ 92 #define IEEE80211_SIGNAL_AVE_MIN_COUNT 4 93 94 /* 95 * Extract from the given disabled subchannel bitmap (raw format 96 * from the EHT Operation Element) the bits for the subchannel 97 * we're using right now. 98 */ 99 static u16 100 ieee80211_extract_dis_subch_bmap(const struct ieee80211_eht_operation *eht_oper, 101 struct cfg80211_chan_def *chandef, u16 bitmap) 102 { 103 struct ieee80211_eht_operation_info *info = (void *)eht_oper->optional; 104 struct cfg80211_chan_def ap_chandef = *chandef; 105 u32 ap_center_freq, local_center_freq; 106 u32 ap_bw, local_bw; 107 int ap_start_freq, local_start_freq; 108 u16 shift, mask; 109 110 if (!(eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT) || 111 !(eht_oper->params & 112 IEEE80211_EHT_OPER_DISABLED_SUBCHANNEL_BITMAP_PRESENT)) 113 return 0; 114 115 /* set 160/320 supported to get the full AP definition */ 116 ieee80211_chandef_eht_oper((const void *)eht_oper->optional, 117 true, true, &ap_chandef); 118 ap_center_freq = ap_chandef.center_freq1; 119 ap_bw = 20 * BIT(u8_get_bits(info->control, 120 IEEE80211_EHT_OPER_CHAN_WIDTH)); 121 ap_start_freq = ap_center_freq - ap_bw / 2; 122 local_center_freq = chandef->center_freq1; 123 local_bw = 20 * BIT(ieee80211_chan_width_to_rx_bw(chandef->width)); 124 local_start_freq = local_center_freq - local_bw / 2; 125 shift = (local_start_freq - ap_start_freq) / 20; 126 mask = BIT(local_bw / 20) - 1; 127 128 return (bitmap >> shift) & mask; 129 } 130 131 /* 132 * Handle the puncturing bitmap, possibly downgrading bandwidth to get a 133 * valid bitmap. 134 */ 135 static void 136 ieee80211_handle_puncturing_bitmap(struct ieee80211_link_data *link, 137 const struct ieee80211_eht_operation *eht_oper, 138 u16 bitmap, u64 *changed) 139 { 140 struct cfg80211_chan_def *chandef = &link->conf->chandef; 141 struct ieee80211_local *local = link->sdata->local; 142 u16 extracted; 143 u64 _changed = 0; 144 145 if (!changed) 146 changed = &_changed; 147 148 while (chandef->width > NL80211_CHAN_WIDTH_40) { 149 extracted = 150 ieee80211_extract_dis_subch_bmap(eht_oper, chandef, 151 bitmap); 152 153 if (cfg80211_valid_disable_subchannel_bitmap(&bitmap, 154 chandef) && 155 !(bitmap && ieee80211_hw_check(&local->hw, 156 DISALLOW_PUNCTURING))) 157 break; 158 link->u.mgd.conn_flags |= 159 ieee80211_chandef_downgrade(chandef); 160 *changed |= BSS_CHANGED_BANDWIDTH; 161 } 162 163 if (chandef->width <= NL80211_CHAN_WIDTH_40) 164 extracted = 0; 165 166 if (link->conf->eht_puncturing != extracted) { 167 link->conf->eht_puncturing = extracted; 168 *changed |= BSS_CHANGED_EHT_PUNCTURING; 169 } 170 } 171 172 /* 173 * We can have multiple work items (and connection probing) 174 * scheduling this timer, but we need to take care to only 175 * reschedule it when it should fire _earlier_ than it was 176 * asked for before, or if it's not pending right now. This 177 * function ensures that. Note that it then is required to 178 * run this function for all timeouts after the first one 179 * has happened -- the work that runs from this timer will 180 * do that. 181 */ 182 static void run_again(struct ieee80211_sub_if_data *sdata, 183 unsigned long timeout) 184 { 185 lockdep_assert_wiphy(sdata->local->hw.wiphy); 186 187 if (!timer_pending(&sdata->u.mgd.timer) || 188 time_before(timeout, sdata->u.mgd.timer.expires)) 189 mod_timer(&sdata->u.mgd.timer, timeout); 190 } 191 192 void ieee80211_sta_reset_beacon_monitor(struct ieee80211_sub_if_data *sdata) 193 { 194 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER) 195 return; 196 197 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR)) 198 return; 199 200 mod_timer(&sdata->u.mgd.bcn_mon_timer, 201 round_jiffies_up(jiffies + sdata->u.mgd.beacon_timeout)); 202 } 203 204 void ieee80211_sta_reset_conn_monitor(struct ieee80211_sub_if_data *sdata) 205 { 206 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 207 208 if (unlikely(!ifmgd->associated)) 209 return; 210 211 if (ifmgd->probe_send_count) 212 ifmgd->probe_send_count = 0; 213 214 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR)) 215 return; 216 217 mod_timer(&ifmgd->conn_mon_timer, 218 round_jiffies_up(jiffies + IEEE80211_CONNECTION_IDLE_TIME)); 219 } 220 221 static int ecw2cw(int ecw) 222 { 223 return (1 << ecw) - 1; 224 } 225 226 static ieee80211_conn_flags_t 227 ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata, 228 struct ieee80211_link_data *link, 229 ieee80211_conn_flags_t conn_flags, 230 struct ieee80211_supported_band *sband, 231 struct ieee80211_channel *channel, 232 u32 vht_cap_info, 233 const struct ieee80211_ht_operation *ht_oper, 234 const struct ieee80211_vht_operation *vht_oper, 235 const struct ieee80211_he_operation *he_oper, 236 const struct ieee80211_eht_operation *eht_oper, 237 const struct ieee80211_s1g_oper_ie *s1g_oper, 238 struct cfg80211_chan_def *chandef, bool tracking) 239 { 240 struct cfg80211_chan_def vht_chandef; 241 struct ieee80211_sta_ht_cap sta_ht_cap; 242 ieee80211_conn_flags_t ret; 243 u32 ht_cfreq; 244 245 memset(chandef, 0, sizeof(struct cfg80211_chan_def)); 246 chandef->chan = channel; 247 chandef->width = NL80211_CHAN_WIDTH_20_NOHT; 248 chandef->center_freq1 = channel->center_freq; 249 chandef->freq1_offset = channel->freq_offset; 250 251 if (channel->band == NL80211_BAND_6GHZ) { 252 if (!ieee80211_chandef_he_6ghz_oper(sdata, he_oper, eht_oper, 253 chandef)) { 254 mlme_dbg(sdata, 255 "bad 6 GHz operation, disabling HT/VHT/HE/EHT\n"); 256 ret = IEEE80211_CONN_DISABLE_HT | 257 IEEE80211_CONN_DISABLE_VHT | 258 IEEE80211_CONN_DISABLE_HE | 259 IEEE80211_CONN_DISABLE_EHT; 260 } else { 261 ret = 0; 262 } 263 vht_chandef = *chandef; 264 goto out; 265 } else if (sband->band == NL80211_BAND_S1GHZ) { 266 if (!ieee80211_chandef_s1g_oper(s1g_oper, chandef)) { 267 sdata_info(sdata, 268 "Missing S1G Operation Element? Trying operating == primary\n"); 269 chandef->width = ieee80211_s1g_channel_width(channel); 270 } 271 272 ret = IEEE80211_CONN_DISABLE_HT | IEEE80211_CONN_DISABLE_40MHZ | 273 IEEE80211_CONN_DISABLE_VHT | 274 IEEE80211_CONN_DISABLE_80P80MHZ | 275 IEEE80211_CONN_DISABLE_160MHZ; 276 goto out; 277 } 278 279 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap)); 280 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap); 281 282 if (!ht_oper || !sta_ht_cap.ht_supported) { 283 mlme_dbg(sdata, "HT operation missing / HT not supported\n"); 284 ret = IEEE80211_CONN_DISABLE_HT | 285 IEEE80211_CONN_DISABLE_VHT | 286 IEEE80211_CONN_DISABLE_HE | 287 IEEE80211_CONN_DISABLE_EHT; 288 goto out; 289 } 290 291 chandef->width = NL80211_CHAN_WIDTH_20; 292 293 ht_cfreq = ieee80211_channel_to_frequency(ht_oper->primary_chan, 294 channel->band); 295 /* check that channel matches the right operating channel */ 296 if (!tracking && channel->center_freq != ht_cfreq) { 297 /* 298 * It's possible that some APs are confused here; 299 * Netgear WNDR3700 sometimes reports 4 higher than 300 * the actual channel in association responses, but 301 * since we look at probe response/beacon data here 302 * it should be OK. 303 */ 304 sdata_info(sdata, 305 "Wrong control channel: center-freq: %d ht-cfreq: %d ht->primary_chan: %d band: %d - Disabling HT\n", 306 channel->center_freq, ht_cfreq, 307 ht_oper->primary_chan, channel->band); 308 ret = IEEE80211_CONN_DISABLE_HT | 309 IEEE80211_CONN_DISABLE_VHT | 310 IEEE80211_CONN_DISABLE_HE | 311 IEEE80211_CONN_DISABLE_EHT; 312 goto out; 313 } 314 315 /* check 40 MHz support, if we have it */ 316 if (sta_ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) { 317 ieee80211_chandef_ht_oper(ht_oper, chandef); 318 } else { 319 mlme_dbg(sdata, "40 MHz not supported\n"); 320 /* 40 MHz (and 80 MHz) must be supported for VHT */ 321 ret = IEEE80211_CONN_DISABLE_VHT; 322 /* also mark 40 MHz disabled */ 323 ret |= IEEE80211_CONN_DISABLE_40MHZ; 324 goto out; 325 } 326 327 if (!vht_oper || !sband->vht_cap.vht_supported) { 328 mlme_dbg(sdata, "VHT operation missing / VHT not supported\n"); 329 ret = IEEE80211_CONN_DISABLE_VHT; 330 goto out; 331 } 332 333 vht_chandef = *chandef; 334 if (!(conn_flags & IEEE80211_CONN_DISABLE_HE) && 335 he_oper && 336 (le32_to_cpu(he_oper->he_oper_params) & 337 IEEE80211_HE_OPERATION_VHT_OPER_INFO)) { 338 struct ieee80211_vht_operation he_oper_vht_cap; 339 340 /* 341 * Set only first 3 bytes (other 2 aren't used in 342 * ieee80211_chandef_vht_oper() anyway) 343 */ 344 memcpy(&he_oper_vht_cap, he_oper->optional, 3); 345 he_oper_vht_cap.basic_mcs_set = cpu_to_le16(0); 346 347 if (!ieee80211_chandef_vht_oper(&sdata->local->hw, vht_cap_info, 348 &he_oper_vht_cap, ht_oper, 349 &vht_chandef)) { 350 if (!(conn_flags & IEEE80211_CONN_DISABLE_HE)) 351 sdata_info(sdata, 352 "HE AP VHT information is invalid, disabling HE\n"); 353 ret = IEEE80211_CONN_DISABLE_HE | IEEE80211_CONN_DISABLE_EHT; 354 goto out; 355 } 356 } else if (!ieee80211_chandef_vht_oper(&sdata->local->hw, 357 vht_cap_info, 358 vht_oper, ht_oper, 359 &vht_chandef)) { 360 if (!(conn_flags & IEEE80211_CONN_DISABLE_VHT)) 361 sdata_info(sdata, 362 "AP VHT information is invalid, disabling VHT\n"); 363 ret = IEEE80211_CONN_DISABLE_VHT; 364 goto out; 365 } 366 367 if (!cfg80211_chandef_valid(&vht_chandef)) { 368 if (!(conn_flags & IEEE80211_CONN_DISABLE_VHT)) 369 sdata_info(sdata, 370 "AP VHT information is invalid, disabling VHT\n"); 371 ret = IEEE80211_CONN_DISABLE_VHT; 372 goto out; 373 } 374 375 if (cfg80211_chandef_identical(chandef, &vht_chandef)) { 376 ret = 0; 377 goto out; 378 } 379 380 if (!cfg80211_chandef_compatible(chandef, &vht_chandef)) { 381 if (!(conn_flags & IEEE80211_CONN_DISABLE_VHT)) 382 sdata_info(sdata, 383 "AP VHT information doesn't match HT, disabling VHT\n"); 384 ret = IEEE80211_CONN_DISABLE_VHT; 385 goto out; 386 } 387 388 *chandef = vht_chandef; 389 390 /* 391 * handle the case that the EHT operation indicates that it holds EHT 392 * operation information (in case that the channel width differs from 393 * the channel width reported in HT/VHT/HE). 394 */ 395 if (eht_oper && (eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT)) { 396 struct cfg80211_chan_def eht_chandef = *chandef; 397 398 ieee80211_chandef_eht_oper((const void *)eht_oper->optional, 399 eht_chandef.width == 400 NL80211_CHAN_WIDTH_160, 401 false, &eht_chandef); 402 403 if (!cfg80211_chandef_valid(&eht_chandef)) { 404 if (!(conn_flags & IEEE80211_CONN_DISABLE_EHT)) 405 sdata_info(sdata, 406 "AP EHT information is invalid, disabling EHT\n"); 407 ret = IEEE80211_CONN_DISABLE_EHT; 408 goto out; 409 } 410 411 if (!cfg80211_chandef_compatible(chandef, &eht_chandef)) { 412 if (!(conn_flags & IEEE80211_CONN_DISABLE_EHT)) 413 sdata_info(sdata, 414 "AP EHT information is incompatible, disabling EHT\n"); 415 ret = IEEE80211_CONN_DISABLE_EHT; 416 goto out; 417 } 418 419 *chandef = eht_chandef; 420 } 421 422 ret = 0; 423 424 out: 425 /* 426 * When tracking the current AP, don't do any further checks if the 427 * new chandef is identical to the one we're currently using for the 428 * connection. This keeps us from playing ping-pong with regulatory, 429 * without it the following can happen (for example): 430 * - connect to an AP with 80 MHz, world regdom allows 80 MHz 431 * - AP advertises regdom US 432 * - CRDA loads regdom US with 80 MHz prohibited (old database) 433 * - the code below detects an unsupported channel, downgrades, and 434 * we disconnect from the AP in the caller 435 * - disconnect causes CRDA to reload world regdomain and the game 436 * starts anew. 437 * (see https://bugzilla.kernel.org/show_bug.cgi?id=70881) 438 * 439 * It seems possible that there are still scenarios with CSA or real 440 * bandwidth changes where a this could happen, but those cases are 441 * less common and wouldn't completely prevent using the AP. 442 */ 443 if (tracking && 444 cfg80211_chandef_identical(chandef, &link->conf->chandef)) 445 return ret; 446 447 /* don't print the message below for VHT mismatch if VHT is disabled */ 448 if (ret & IEEE80211_CONN_DISABLE_VHT) 449 vht_chandef = *chandef; 450 451 /* 452 * Ignore the DISABLED flag when we're already connected and only 453 * tracking the APs beacon for bandwidth changes - otherwise we 454 * might get disconnected here if we connect to an AP, update our 455 * regulatory information based on the AP's country IE and the 456 * information we have is wrong/outdated and disables the channel 457 * that we're actually using for the connection to the AP. 458 */ 459 while (!cfg80211_chandef_usable(sdata->local->hw.wiphy, chandef, 460 tracking ? 0 : 461 IEEE80211_CHAN_DISABLED)) { 462 if (WARN_ON(chandef->width == NL80211_CHAN_WIDTH_20_NOHT)) { 463 ret = IEEE80211_CONN_DISABLE_HT | 464 IEEE80211_CONN_DISABLE_VHT | 465 IEEE80211_CONN_DISABLE_HE | 466 IEEE80211_CONN_DISABLE_EHT; 467 break; 468 } 469 470 ret |= ieee80211_chandef_downgrade(chandef); 471 } 472 473 if (!he_oper || !cfg80211_chandef_usable(sdata->wdev.wiphy, chandef, 474 IEEE80211_CHAN_NO_HE)) 475 ret |= IEEE80211_CONN_DISABLE_HE | IEEE80211_CONN_DISABLE_EHT; 476 477 if (!eht_oper || !cfg80211_chandef_usable(sdata->wdev.wiphy, chandef, 478 IEEE80211_CHAN_NO_EHT)) 479 ret |= IEEE80211_CONN_DISABLE_EHT; 480 481 if (chandef->width != vht_chandef.width && !tracking) 482 sdata_info(sdata, 483 "capabilities/regulatory prevented using AP HT/VHT configuration, downgraded\n"); 484 485 WARN_ON_ONCE(!cfg80211_chandef_valid(chandef)); 486 return ret; 487 } 488 489 static int ieee80211_config_bw(struct ieee80211_link_data *link, 490 const struct ieee80211_ht_cap *ht_cap, 491 const struct ieee80211_vht_cap *vht_cap, 492 const struct ieee80211_ht_operation *ht_oper, 493 const struct ieee80211_vht_operation *vht_oper, 494 const struct ieee80211_he_operation *he_oper, 495 const struct ieee80211_eht_operation *eht_oper, 496 const struct ieee80211_s1g_oper_ie *s1g_oper, 497 const u8 *bssid, u64 *changed) 498 { 499 struct ieee80211_sub_if_data *sdata = link->sdata; 500 struct ieee80211_local *local = sdata->local; 501 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 502 struct ieee80211_channel *chan = link->conf->chandef.chan; 503 struct ieee80211_supported_band *sband = 504 local->hw.wiphy->bands[chan->band]; 505 struct cfg80211_chan_def chandef; 506 u16 ht_opmode; 507 ieee80211_conn_flags_t flags; 508 u32 vht_cap_info = 0; 509 int ret; 510 511 /* if HT was/is disabled, don't track any bandwidth changes */ 512 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HT || !ht_oper) 513 return 0; 514 515 /* don't check VHT if we associated as non-VHT station */ 516 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_VHT) 517 vht_oper = NULL; 518 519 /* don't check HE if we associated as non-HE station */ 520 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HE || 521 !ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif)) { 522 he_oper = NULL; 523 eht_oper = NULL; 524 } 525 526 /* don't check EHT if we associated as non-EHT station */ 527 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_EHT || 528 !ieee80211_get_eht_iftype_cap_vif(sband, &sdata->vif)) 529 eht_oper = NULL; 530 531 /* 532 * if bss configuration changed store the new one - 533 * this may be applicable even if channel is identical 534 */ 535 ht_opmode = le16_to_cpu(ht_oper->operation_mode); 536 if (link->conf->ht_operation_mode != ht_opmode) { 537 *changed |= BSS_CHANGED_HT; 538 link->conf->ht_operation_mode = ht_opmode; 539 } 540 541 if (vht_cap) 542 vht_cap_info = le32_to_cpu(vht_cap->vht_cap_info); 543 544 /* calculate new channel (type) based on HT/VHT/HE operation IEs */ 545 flags = ieee80211_determine_chantype(sdata, link, 546 link->u.mgd.conn_flags, 547 sband, chan, vht_cap_info, 548 ht_oper, vht_oper, 549 he_oper, eht_oper, 550 s1g_oper, &chandef, true); 551 552 /* 553 * Downgrade the new channel if we associated with restricted 554 * capabilities. For example, if we associated as a 20 MHz STA 555 * to a 40 MHz AP (due to regulatory, capabilities or config 556 * reasons) then switching to a 40 MHz channel now won't do us 557 * any good -- we couldn't use it with the AP. 558 */ 559 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_80P80MHZ && 560 chandef.width == NL80211_CHAN_WIDTH_80P80) 561 flags |= ieee80211_chandef_downgrade(&chandef); 562 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_160MHZ && 563 chandef.width == NL80211_CHAN_WIDTH_160) 564 flags |= ieee80211_chandef_downgrade(&chandef); 565 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_40MHZ && 566 chandef.width > NL80211_CHAN_WIDTH_20) 567 flags |= ieee80211_chandef_downgrade(&chandef); 568 569 if (cfg80211_chandef_identical(&chandef, &link->conf->chandef)) 570 return 0; 571 572 link_info(link, 573 "AP %pM changed bandwidth, new config is %d.%03d MHz, width %d (%d.%03d/%d MHz)\n", 574 link->u.mgd.bssid, chandef.chan->center_freq, 575 chandef.chan->freq_offset, chandef.width, 576 chandef.center_freq1, chandef.freq1_offset, 577 chandef.center_freq2); 578 579 if (flags != (link->u.mgd.conn_flags & 580 (IEEE80211_CONN_DISABLE_HT | 581 IEEE80211_CONN_DISABLE_VHT | 582 IEEE80211_CONN_DISABLE_HE | 583 IEEE80211_CONN_DISABLE_EHT | 584 IEEE80211_CONN_DISABLE_40MHZ | 585 IEEE80211_CONN_DISABLE_80P80MHZ | 586 IEEE80211_CONN_DISABLE_160MHZ | 587 IEEE80211_CONN_DISABLE_320MHZ)) || 588 !cfg80211_chandef_valid(&chandef)) { 589 sdata_info(sdata, 590 "AP %pM changed caps/bw in a way we can't support (0x%x/0x%x) - disconnect\n", 591 link->u.mgd.bssid, flags, ifmgd->flags); 592 return -EINVAL; 593 } 594 595 ret = ieee80211_link_change_bandwidth(link, &chandef, changed); 596 597 if (ret) { 598 sdata_info(sdata, 599 "AP %pM changed bandwidth to incompatible one - disconnect\n", 600 link->u.mgd.bssid); 601 return ret; 602 } 603 604 cfg80211_schedule_channels_check(&sdata->wdev); 605 return 0; 606 } 607 608 /* frame sending functions */ 609 610 static void ieee80211_add_ht_ie(struct ieee80211_sub_if_data *sdata, 611 struct sk_buff *skb, u8 ap_ht_param, 612 struct ieee80211_supported_band *sband, 613 struct ieee80211_channel *channel, 614 enum ieee80211_smps_mode smps, 615 ieee80211_conn_flags_t conn_flags) 616 { 617 u8 *pos; 618 u32 flags = channel->flags; 619 u16 cap; 620 struct ieee80211_sta_ht_cap ht_cap; 621 622 BUILD_BUG_ON(sizeof(ht_cap) != sizeof(sband->ht_cap)); 623 624 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap)); 625 ieee80211_apply_htcap_overrides(sdata, &ht_cap); 626 627 /* determine capability flags */ 628 cap = ht_cap.cap; 629 630 switch (ap_ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) { 631 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE: 632 if (flags & IEEE80211_CHAN_NO_HT40PLUS) { 633 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40; 634 cap &= ~IEEE80211_HT_CAP_SGI_40; 635 } 636 break; 637 case IEEE80211_HT_PARAM_CHA_SEC_BELOW: 638 if (flags & IEEE80211_CHAN_NO_HT40MINUS) { 639 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40; 640 cap &= ~IEEE80211_HT_CAP_SGI_40; 641 } 642 break; 643 } 644 645 /* 646 * If 40 MHz was disabled associate as though we weren't 647 * capable of 40 MHz -- some broken APs will never fall 648 * back to trying to transmit in 20 MHz. 649 */ 650 if (conn_flags & IEEE80211_CONN_DISABLE_40MHZ) { 651 cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40; 652 cap &= ~IEEE80211_HT_CAP_SGI_40; 653 } 654 655 /* set SM PS mode properly */ 656 cap &= ~IEEE80211_HT_CAP_SM_PS; 657 switch (smps) { 658 case IEEE80211_SMPS_AUTOMATIC: 659 case IEEE80211_SMPS_NUM_MODES: 660 WARN_ON(1); 661 fallthrough; 662 case IEEE80211_SMPS_OFF: 663 cap |= WLAN_HT_CAP_SM_PS_DISABLED << 664 IEEE80211_HT_CAP_SM_PS_SHIFT; 665 break; 666 case IEEE80211_SMPS_STATIC: 667 cap |= WLAN_HT_CAP_SM_PS_STATIC << 668 IEEE80211_HT_CAP_SM_PS_SHIFT; 669 break; 670 case IEEE80211_SMPS_DYNAMIC: 671 cap |= WLAN_HT_CAP_SM_PS_DYNAMIC << 672 IEEE80211_HT_CAP_SM_PS_SHIFT; 673 break; 674 } 675 676 /* reserve and fill IE */ 677 pos = skb_put(skb, sizeof(struct ieee80211_ht_cap) + 2); 678 ieee80211_ie_build_ht_cap(pos, &ht_cap, cap); 679 } 680 681 /* This function determines vht capability flags for the association 682 * and builds the IE. 683 * Note - the function returns true to own the MU-MIMO capability 684 */ 685 static bool ieee80211_add_vht_ie(struct ieee80211_sub_if_data *sdata, 686 struct sk_buff *skb, 687 struct ieee80211_supported_band *sband, 688 struct ieee80211_vht_cap *ap_vht_cap, 689 ieee80211_conn_flags_t conn_flags) 690 { 691 struct ieee80211_local *local = sdata->local; 692 u8 *pos; 693 u32 cap; 694 struct ieee80211_sta_vht_cap vht_cap; 695 u32 mask, ap_bf_sts, our_bf_sts; 696 bool mu_mimo_owner = false; 697 698 BUILD_BUG_ON(sizeof(vht_cap) != sizeof(sband->vht_cap)); 699 700 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap)); 701 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap); 702 703 /* determine capability flags */ 704 cap = vht_cap.cap; 705 706 if (conn_flags & IEEE80211_CONN_DISABLE_80P80MHZ) { 707 u32 bw = cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK; 708 709 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK; 710 if (bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ || 711 bw == IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) 712 cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ; 713 } 714 715 if (conn_flags & IEEE80211_CONN_DISABLE_160MHZ) { 716 cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160; 717 cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK; 718 } 719 720 /* 721 * Some APs apparently get confused if our capabilities are better 722 * than theirs, so restrict what we advertise in the assoc request. 723 */ 724 if (!(ap_vht_cap->vht_cap_info & 725 cpu_to_le32(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE))) 726 cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE | 727 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE); 728 else if (!(ap_vht_cap->vht_cap_info & 729 cpu_to_le32(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE))) 730 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE; 731 732 /* 733 * If some other vif is using the MU-MIMO capability we cannot associate 734 * using MU-MIMO - this will lead to contradictions in the group-id 735 * mechanism. 736 * Ownership is defined since association request, in order to avoid 737 * simultaneous associations with MU-MIMO. 738 */ 739 if (cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) { 740 bool disable_mu_mimo = false; 741 struct ieee80211_sub_if_data *other; 742 743 list_for_each_entry_rcu(other, &local->interfaces, list) { 744 if (other->vif.bss_conf.mu_mimo_owner) { 745 disable_mu_mimo = true; 746 break; 747 } 748 } 749 if (disable_mu_mimo) 750 cap &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE; 751 else 752 mu_mimo_owner = true; 753 } 754 755 mask = IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK; 756 757 ap_bf_sts = le32_to_cpu(ap_vht_cap->vht_cap_info) & mask; 758 our_bf_sts = cap & mask; 759 760 if (ap_bf_sts < our_bf_sts) { 761 cap &= ~mask; 762 cap |= ap_bf_sts; 763 } 764 765 /* reserve and fill IE */ 766 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2); 767 ieee80211_ie_build_vht_cap(pos, &vht_cap, cap); 768 769 return mu_mimo_owner; 770 } 771 772 /* This function determines HE capability flags for the association 773 * and builds the IE. 774 */ 775 static void ieee80211_add_he_ie(struct ieee80211_sub_if_data *sdata, 776 struct sk_buff *skb, 777 struct ieee80211_supported_band *sband, 778 enum ieee80211_smps_mode smps_mode, 779 ieee80211_conn_flags_t conn_flags) 780 { 781 u8 *pos, *pre_he_pos; 782 const struct ieee80211_sta_he_cap *he_cap; 783 u8 he_cap_size; 784 785 he_cap = ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif); 786 if (WARN_ON(!he_cap)) 787 return; 788 789 /* get a max size estimate */ 790 he_cap_size = 791 2 + 1 + sizeof(he_cap->he_cap_elem) + 792 ieee80211_he_mcs_nss_size(&he_cap->he_cap_elem) + 793 ieee80211_he_ppe_size(he_cap->ppe_thres[0], 794 he_cap->he_cap_elem.phy_cap_info); 795 pos = skb_put(skb, he_cap_size); 796 pre_he_pos = pos; 797 pos = ieee80211_ie_build_he_cap(conn_flags, 798 pos, he_cap, pos + he_cap_size); 799 /* trim excess if any */ 800 skb_trim(skb, skb->len - (pre_he_pos + he_cap_size - pos)); 801 802 ieee80211_ie_build_he_6ghz_cap(sdata, smps_mode, skb); 803 } 804 805 static void ieee80211_add_eht_ie(struct ieee80211_sub_if_data *sdata, 806 struct sk_buff *skb, 807 struct ieee80211_supported_band *sband) 808 { 809 u8 *pos; 810 const struct ieee80211_sta_he_cap *he_cap; 811 const struct ieee80211_sta_eht_cap *eht_cap; 812 u8 eht_cap_size; 813 814 he_cap = ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif); 815 eht_cap = ieee80211_get_eht_iftype_cap_vif(sband, &sdata->vif); 816 817 /* 818 * EHT capabilities element is only added if the HE capabilities element 819 * was added so assume that 'he_cap' is valid and don't check it. 820 */ 821 if (WARN_ON(!he_cap || !eht_cap)) 822 return; 823 824 eht_cap_size = 825 2 + 1 + sizeof(eht_cap->eht_cap_elem) + 826 ieee80211_eht_mcs_nss_size(&he_cap->he_cap_elem, 827 &eht_cap->eht_cap_elem, 828 false) + 829 ieee80211_eht_ppe_size(eht_cap->eht_ppe_thres[0], 830 eht_cap->eht_cap_elem.phy_cap_info); 831 pos = skb_put(skb, eht_cap_size); 832 ieee80211_ie_build_eht_cap(pos, he_cap, eht_cap, pos + eht_cap_size, 833 false); 834 } 835 836 static void ieee80211_assoc_add_rates(struct sk_buff *skb, 837 enum nl80211_chan_width width, 838 struct ieee80211_supported_band *sband, 839 struct ieee80211_mgd_assoc_data *assoc_data) 840 { 841 unsigned int rates_len, supp_rates_len; 842 u32 rates = 0; 843 int i, count; 844 u8 *pos; 845 846 if (assoc_data->supp_rates_len) { 847 /* 848 * Get all rates supported by the device and the AP as 849 * some APs don't like getting a superset of their rates 850 * in the association request (e.g. D-Link DAP 1353 in 851 * b-only mode)... 852 */ 853 rates_len = ieee80211_parse_bitrates(width, sband, 854 assoc_data->supp_rates, 855 assoc_data->supp_rates_len, 856 &rates); 857 } else { 858 /* 859 * In case AP not provide any supported rates information 860 * before association, we send information element(s) with 861 * all rates that we support. 862 */ 863 rates_len = sband->n_bitrates; 864 for (i = 0; i < sband->n_bitrates; i++) 865 rates |= BIT(i); 866 } 867 868 supp_rates_len = rates_len; 869 if (supp_rates_len > 8) 870 supp_rates_len = 8; 871 872 pos = skb_put(skb, supp_rates_len + 2); 873 *pos++ = WLAN_EID_SUPP_RATES; 874 *pos++ = supp_rates_len; 875 876 count = 0; 877 for (i = 0; i < sband->n_bitrates; i++) { 878 if (BIT(i) & rates) { 879 int rate = DIV_ROUND_UP(sband->bitrates[i].bitrate, 5); 880 *pos++ = (u8)rate; 881 if (++count == 8) 882 break; 883 } 884 } 885 886 if (rates_len > count) { 887 pos = skb_put(skb, rates_len - count + 2); 888 *pos++ = WLAN_EID_EXT_SUPP_RATES; 889 *pos++ = rates_len - count; 890 891 for (i++; i < sband->n_bitrates; i++) { 892 if (BIT(i) & rates) { 893 int rate; 894 895 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate, 5); 896 *pos++ = (u8)rate; 897 } 898 } 899 } 900 } 901 902 static size_t ieee80211_add_before_ht_elems(struct sk_buff *skb, 903 const u8 *elems, 904 size_t elems_len, 905 size_t offset) 906 { 907 size_t noffset; 908 909 static const u8 before_ht[] = { 910 WLAN_EID_SSID, 911 WLAN_EID_SUPP_RATES, 912 WLAN_EID_EXT_SUPP_RATES, 913 WLAN_EID_PWR_CAPABILITY, 914 WLAN_EID_SUPPORTED_CHANNELS, 915 WLAN_EID_RSN, 916 WLAN_EID_QOS_CAPA, 917 WLAN_EID_RRM_ENABLED_CAPABILITIES, 918 WLAN_EID_MOBILITY_DOMAIN, 919 WLAN_EID_FAST_BSS_TRANSITION, /* reassoc only */ 920 WLAN_EID_RIC_DATA, /* reassoc only */ 921 WLAN_EID_SUPPORTED_REGULATORY_CLASSES, 922 }; 923 static const u8 after_ric[] = { 924 WLAN_EID_SUPPORTED_REGULATORY_CLASSES, 925 WLAN_EID_HT_CAPABILITY, 926 WLAN_EID_BSS_COEX_2040, 927 /* luckily this is almost always there */ 928 WLAN_EID_EXT_CAPABILITY, 929 WLAN_EID_QOS_TRAFFIC_CAPA, 930 WLAN_EID_TIM_BCAST_REQ, 931 WLAN_EID_INTERWORKING, 932 /* 60 GHz (Multi-band, DMG, MMS) can't happen */ 933 WLAN_EID_VHT_CAPABILITY, 934 WLAN_EID_OPMODE_NOTIF, 935 }; 936 937 if (!elems_len) 938 return offset; 939 940 noffset = ieee80211_ie_split_ric(elems, elems_len, 941 before_ht, 942 ARRAY_SIZE(before_ht), 943 after_ric, 944 ARRAY_SIZE(after_ric), 945 offset); 946 skb_put_data(skb, elems + offset, noffset - offset); 947 948 return noffset; 949 } 950 951 static size_t ieee80211_add_before_vht_elems(struct sk_buff *skb, 952 const u8 *elems, 953 size_t elems_len, 954 size_t offset) 955 { 956 static const u8 before_vht[] = { 957 /* 958 * no need to list the ones split off before HT 959 * or generated here 960 */ 961 WLAN_EID_BSS_COEX_2040, 962 WLAN_EID_EXT_CAPABILITY, 963 WLAN_EID_QOS_TRAFFIC_CAPA, 964 WLAN_EID_TIM_BCAST_REQ, 965 WLAN_EID_INTERWORKING, 966 /* 60 GHz (Multi-band, DMG, MMS) can't happen */ 967 }; 968 size_t noffset; 969 970 if (!elems_len) 971 return offset; 972 973 /* RIC already taken care of in ieee80211_add_before_ht_elems() */ 974 noffset = ieee80211_ie_split(elems, elems_len, 975 before_vht, ARRAY_SIZE(before_vht), 976 offset); 977 skb_put_data(skb, elems + offset, noffset - offset); 978 979 return noffset; 980 } 981 982 static size_t ieee80211_add_before_he_elems(struct sk_buff *skb, 983 const u8 *elems, 984 size_t elems_len, 985 size_t offset) 986 { 987 static const u8 before_he[] = { 988 /* 989 * no need to list the ones split off before VHT 990 * or generated here 991 */ 992 WLAN_EID_OPMODE_NOTIF, 993 WLAN_EID_EXTENSION, WLAN_EID_EXT_FUTURE_CHAN_GUIDANCE, 994 /* 11ai elements */ 995 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_SESSION, 996 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_PUBLIC_KEY, 997 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_KEY_CONFIRM, 998 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_HLP_CONTAINER, 999 WLAN_EID_EXTENSION, WLAN_EID_EXT_FILS_IP_ADDR_ASSIGN, 1000 /* TODO: add 11ah/11aj/11ak elements */ 1001 }; 1002 size_t noffset; 1003 1004 if (!elems_len) 1005 return offset; 1006 1007 /* RIC already taken care of in ieee80211_add_before_ht_elems() */ 1008 noffset = ieee80211_ie_split(elems, elems_len, 1009 before_he, ARRAY_SIZE(before_he), 1010 offset); 1011 skb_put_data(skb, elems + offset, noffset - offset); 1012 1013 return noffset; 1014 } 1015 1016 #define PRESENT_ELEMS_MAX 8 1017 #define PRESENT_ELEM_EXT_OFFS 0x100 1018 1019 static void ieee80211_assoc_add_ml_elem(struct ieee80211_sub_if_data *sdata, 1020 struct sk_buff *skb, u16 capab, 1021 const struct element *ext_capa, 1022 const u16 *present_elems); 1023 1024 static size_t ieee80211_assoc_link_elems(struct ieee80211_sub_if_data *sdata, 1025 struct sk_buff *skb, u16 *capab, 1026 const struct element *ext_capa, 1027 const u8 *extra_elems, 1028 size_t extra_elems_len, 1029 unsigned int link_id, 1030 struct ieee80211_link_data *link, 1031 u16 *present_elems) 1032 { 1033 enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif); 1034 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 1035 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data; 1036 struct cfg80211_bss *cbss = assoc_data->link[link_id].bss; 1037 struct ieee80211_channel *chan = cbss->channel; 1038 const struct ieee80211_sband_iftype_data *iftd; 1039 struct ieee80211_local *local = sdata->local; 1040 struct ieee80211_supported_band *sband; 1041 enum nl80211_chan_width width = NL80211_CHAN_WIDTH_20; 1042 struct ieee80211_chanctx_conf *chanctx_conf; 1043 enum ieee80211_smps_mode smps_mode; 1044 u16 orig_capab = *capab; 1045 size_t offset = 0; 1046 int present_elems_len = 0; 1047 u8 *pos; 1048 int i; 1049 1050 #define ADD_PRESENT_ELEM(id) do { \ 1051 /* need a last for termination - we use 0 == SSID */ \ 1052 if (!WARN_ON(present_elems_len >= PRESENT_ELEMS_MAX - 1)) \ 1053 present_elems[present_elems_len++] = (id); \ 1054 } while (0) 1055 #define ADD_PRESENT_EXT_ELEM(id) ADD_PRESENT_ELEM(PRESENT_ELEM_EXT_OFFS | (id)) 1056 1057 if (link) 1058 smps_mode = link->smps_mode; 1059 else if (sdata->u.mgd.powersave) 1060 smps_mode = IEEE80211_SMPS_DYNAMIC; 1061 else 1062 smps_mode = IEEE80211_SMPS_OFF; 1063 1064 if (link) { 1065 /* 1066 * 5/10 MHz scenarios are only viable without MLO, in which 1067 * case this pointer should be used ... All of this is a bit 1068 * unclear though, not sure this even works at all. 1069 */ 1070 rcu_read_lock(); 1071 chanctx_conf = rcu_dereference(link->conf->chanctx_conf); 1072 if (chanctx_conf) 1073 width = chanctx_conf->def.width; 1074 rcu_read_unlock(); 1075 } 1076 1077 sband = local->hw.wiphy->bands[chan->band]; 1078 iftd = ieee80211_get_sband_iftype_data(sband, iftype); 1079 1080 if (sband->band == NL80211_BAND_2GHZ) { 1081 *capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME; 1082 *capab |= WLAN_CAPABILITY_SHORT_PREAMBLE; 1083 } 1084 1085 if ((cbss->capability & WLAN_CAPABILITY_SPECTRUM_MGMT) && 1086 ieee80211_hw_check(&local->hw, SPECTRUM_MGMT)) 1087 *capab |= WLAN_CAPABILITY_SPECTRUM_MGMT; 1088 1089 if (sband->band != NL80211_BAND_S1GHZ) 1090 ieee80211_assoc_add_rates(skb, width, sband, assoc_data); 1091 1092 if (*capab & WLAN_CAPABILITY_SPECTRUM_MGMT || 1093 *capab & WLAN_CAPABILITY_RADIO_MEASURE) { 1094 struct cfg80211_chan_def chandef = { 1095 .width = width, 1096 .chan = chan, 1097 }; 1098 1099 pos = skb_put(skb, 4); 1100 *pos++ = WLAN_EID_PWR_CAPABILITY; 1101 *pos++ = 2; 1102 *pos++ = 0; /* min tx power */ 1103 /* max tx power */ 1104 *pos++ = ieee80211_chandef_max_power(&chandef); 1105 ADD_PRESENT_ELEM(WLAN_EID_PWR_CAPABILITY); 1106 } 1107 1108 /* 1109 * Per spec, we shouldn't include the list of channels if we advertise 1110 * support for extended channel switching, but we've always done that; 1111 * (for now?) apply this restriction only on the (new) 6 GHz band. 1112 */ 1113 if (*capab & WLAN_CAPABILITY_SPECTRUM_MGMT && 1114 (sband->band != NL80211_BAND_6GHZ || 1115 !ext_capa || ext_capa->datalen < 1 || 1116 !(ext_capa->data[0] & WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING))) { 1117 /* TODO: get this in reg domain format */ 1118 pos = skb_put(skb, 2 * sband->n_channels + 2); 1119 *pos++ = WLAN_EID_SUPPORTED_CHANNELS; 1120 *pos++ = 2 * sband->n_channels; 1121 for (i = 0; i < sband->n_channels; i++) { 1122 int cf = sband->channels[i].center_freq; 1123 1124 *pos++ = ieee80211_frequency_to_channel(cf); 1125 *pos++ = 1; /* one channel in the subband*/ 1126 } 1127 ADD_PRESENT_ELEM(WLAN_EID_SUPPORTED_CHANNELS); 1128 } 1129 1130 /* if present, add any custom IEs that go before HT */ 1131 offset = ieee80211_add_before_ht_elems(skb, extra_elems, 1132 extra_elems_len, 1133 offset); 1134 1135 if (sband->band != NL80211_BAND_6GHZ && 1136 !(assoc_data->link[link_id].conn_flags & IEEE80211_CONN_DISABLE_HT)) { 1137 ieee80211_add_ht_ie(sdata, skb, 1138 assoc_data->link[link_id].ap_ht_param, 1139 sband, chan, smps_mode, 1140 assoc_data->link[link_id].conn_flags); 1141 ADD_PRESENT_ELEM(WLAN_EID_HT_CAPABILITY); 1142 } 1143 1144 /* if present, add any custom IEs that go before VHT */ 1145 offset = ieee80211_add_before_vht_elems(skb, extra_elems, 1146 extra_elems_len, 1147 offset); 1148 1149 if (sband->band != NL80211_BAND_6GHZ && 1150 !(assoc_data->link[link_id].conn_flags & IEEE80211_CONN_DISABLE_VHT)) { 1151 bool mu_mimo_owner = 1152 ieee80211_add_vht_ie(sdata, skb, sband, 1153 &assoc_data->link[link_id].ap_vht_cap, 1154 assoc_data->link[link_id].conn_flags); 1155 1156 if (link) 1157 link->conf->mu_mimo_owner = mu_mimo_owner; 1158 ADD_PRESENT_ELEM(WLAN_EID_VHT_CAPABILITY); 1159 } 1160 1161 /* 1162 * If AP doesn't support HT, mark HE and EHT as disabled. 1163 * If on the 5GHz band, make sure it supports VHT. 1164 */ 1165 if (assoc_data->link[link_id].conn_flags & IEEE80211_CONN_DISABLE_HT || 1166 (sband->band == NL80211_BAND_5GHZ && 1167 assoc_data->link[link_id].conn_flags & IEEE80211_CONN_DISABLE_VHT)) 1168 assoc_data->link[link_id].conn_flags |= 1169 IEEE80211_CONN_DISABLE_HE | 1170 IEEE80211_CONN_DISABLE_EHT; 1171 1172 /* if present, add any custom IEs that go before HE */ 1173 offset = ieee80211_add_before_he_elems(skb, extra_elems, 1174 extra_elems_len, 1175 offset); 1176 1177 if (!(assoc_data->link[link_id].conn_flags & IEEE80211_CONN_DISABLE_HE)) { 1178 ieee80211_add_he_ie(sdata, skb, sband, smps_mode, 1179 assoc_data->link[link_id].conn_flags); 1180 ADD_PRESENT_EXT_ELEM(WLAN_EID_EXT_HE_CAPABILITY); 1181 } 1182 1183 /* 1184 * careful - need to know about all the present elems before 1185 * calling ieee80211_assoc_add_ml_elem(), so add this one if 1186 * we're going to put it after the ML element 1187 */ 1188 if (!(assoc_data->link[link_id].conn_flags & IEEE80211_CONN_DISABLE_EHT)) 1189 ADD_PRESENT_EXT_ELEM(WLAN_EID_EXT_EHT_CAPABILITY); 1190 1191 if (link_id == assoc_data->assoc_link_id) 1192 ieee80211_assoc_add_ml_elem(sdata, skb, orig_capab, ext_capa, 1193 present_elems); 1194 1195 /* crash if somebody gets it wrong */ 1196 present_elems = NULL; 1197 1198 if (!(assoc_data->link[link_id].conn_flags & IEEE80211_CONN_DISABLE_EHT)) 1199 ieee80211_add_eht_ie(sdata, skb, sband); 1200 1201 if (sband->band == NL80211_BAND_S1GHZ) { 1202 ieee80211_add_aid_request_ie(sdata, skb); 1203 ieee80211_add_s1g_capab_ie(sdata, &sband->s1g_cap, skb); 1204 } 1205 1206 if (iftd && iftd->vendor_elems.data && iftd->vendor_elems.len) 1207 skb_put_data(skb, iftd->vendor_elems.data, iftd->vendor_elems.len); 1208 1209 if (link) 1210 link->u.mgd.conn_flags = assoc_data->link[link_id].conn_flags; 1211 1212 return offset; 1213 } 1214 1215 static void ieee80211_add_non_inheritance_elem(struct sk_buff *skb, 1216 const u16 *outer, 1217 const u16 *inner) 1218 { 1219 unsigned int skb_len = skb->len; 1220 bool at_extension = false; 1221 bool added = false; 1222 int i, j; 1223 u8 *len, *list_len = NULL; 1224 1225 skb_put_u8(skb, WLAN_EID_EXTENSION); 1226 len = skb_put(skb, 1); 1227 skb_put_u8(skb, WLAN_EID_EXT_NON_INHERITANCE); 1228 1229 for (i = 0; i < PRESENT_ELEMS_MAX && outer[i]; i++) { 1230 u16 elem = outer[i]; 1231 bool have_inner = false; 1232 1233 /* should at least be sorted in the sense of normal -> ext */ 1234 WARN_ON(at_extension && elem < PRESENT_ELEM_EXT_OFFS); 1235 1236 /* switch to extension list */ 1237 if (!at_extension && elem >= PRESENT_ELEM_EXT_OFFS) { 1238 at_extension = true; 1239 if (!list_len) 1240 skb_put_u8(skb, 0); 1241 list_len = NULL; 1242 } 1243 1244 for (j = 0; j < PRESENT_ELEMS_MAX && inner[j]; j++) { 1245 if (elem == inner[j]) { 1246 have_inner = true; 1247 break; 1248 } 1249 } 1250 1251 if (have_inner) 1252 continue; 1253 1254 if (!list_len) { 1255 list_len = skb_put(skb, 1); 1256 *list_len = 0; 1257 } 1258 *list_len += 1; 1259 skb_put_u8(skb, (u8)elem); 1260 added = true; 1261 } 1262 1263 /* if we added a list but no extension list, make a zero-len one */ 1264 if (added && (!at_extension || !list_len)) 1265 skb_put_u8(skb, 0); 1266 1267 /* if nothing added remove extension element completely */ 1268 if (!added) 1269 skb_trim(skb, skb_len); 1270 else 1271 *len = skb->len - skb_len - 2; 1272 } 1273 1274 static void ieee80211_assoc_add_ml_elem(struct ieee80211_sub_if_data *sdata, 1275 struct sk_buff *skb, u16 capab, 1276 const struct element *ext_capa, 1277 const u16 *outer_present_elems) 1278 { 1279 struct ieee80211_local *local = sdata->local; 1280 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 1281 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data; 1282 struct ieee80211_multi_link_elem *ml_elem; 1283 struct ieee80211_mle_basic_common_info *common; 1284 const struct wiphy_iftype_ext_capab *ift_ext_capa; 1285 __le16 eml_capa = 0, mld_capa_ops = 0; 1286 unsigned int link_id; 1287 u8 *ml_elem_len; 1288 void *capab_pos; 1289 1290 if (!ieee80211_vif_is_mld(&sdata->vif)) 1291 return; 1292 1293 ift_ext_capa = cfg80211_get_iftype_ext_capa(local->hw.wiphy, 1294 ieee80211_vif_type_p2p(&sdata->vif)); 1295 if (ift_ext_capa) { 1296 eml_capa = cpu_to_le16(ift_ext_capa->eml_capabilities); 1297 mld_capa_ops = cpu_to_le16(ift_ext_capa->mld_capa_and_ops); 1298 } 1299 1300 skb_put_u8(skb, WLAN_EID_EXTENSION); 1301 ml_elem_len = skb_put(skb, 1); 1302 skb_put_u8(skb, WLAN_EID_EXT_EHT_MULTI_LINK); 1303 ml_elem = skb_put(skb, sizeof(*ml_elem)); 1304 ml_elem->control = 1305 cpu_to_le16(IEEE80211_ML_CONTROL_TYPE_BASIC | 1306 IEEE80211_MLC_BASIC_PRES_MLD_CAPA_OP); 1307 common = skb_put(skb, sizeof(*common)); 1308 common->len = sizeof(*common) + 1309 2; /* MLD capa/ops */ 1310 memcpy(common->mld_mac_addr, sdata->vif.addr, ETH_ALEN); 1311 1312 /* add EML_CAPA only if needed, see Draft P802.11be_D2.1, 35.3.17 */ 1313 if (eml_capa & 1314 cpu_to_le16((IEEE80211_EML_CAP_EMLSR_SUPP | 1315 IEEE80211_EML_CAP_EMLMR_SUPPORT))) { 1316 common->len += 2; /* EML capabilities */ 1317 ml_elem->control |= 1318 cpu_to_le16(IEEE80211_MLC_BASIC_PRES_EML_CAPA); 1319 skb_put_data(skb, &eml_capa, sizeof(eml_capa)); 1320 } 1321 /* need indication from userspace to support this */ 1322 mld_capa_ops &= ~cpu_to_le16(IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP); 1323 skb_put_data(skb, &mld_capa_ops, sizeof(mld_capa_ops)); 1324 1325 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) { 1326 u16 link_present_elems[PRESENT_ELEMS_MAX] = {}; 1327 const u8 *extra_elems; 1328 size_t extra_elems_len; 1329 size_t extra_used; 1330 u8 *subelem_len = NULL; 1331 __le16 ctrl; 1332 1333 if (!assoc_data->link[link_id].bss || 1334 link_id == assoc_data->assoc_link_id) 1335 continue; 1336 1337 extra_elems = assoc_data->link[link_id].elems; 1338 extra_elems_len = assoc_data->link[link_id].elems_len; 1339 1340 skb_put_u8(skb, IEEE80211_MLE_SUBELEM_PER_STA_PROFILE); 1341 subelem_len = skb_put(skb, 1); 1342 1343 ctrl = cpu_to_le16(link_id | 1344 IEEE80211_MLE_STA_CONTROL_COMPLETE_PROFILE | 1345 IEEE80211_MLE_STA_CONTROL_STA_MAC_ADDR_PRESENT); 1346 skb_put_data(skb, &ctrl, sizeof(ctrl)); 1347 skb_put_u8(skb, 1 + ETH_ALEN); /* STA Info Length */ 1348 skb_put_data(skb, assoc_data->link[link_id].addr, 1349 ETH_ALEN); 1350 /* 1351 * Now add the contents of the (re)association request, 1352 * but the "listen interval" and "current AP address" 1353 * (if applicable) are skipped. So we only have 1354 * the capability field (remember the position and fill 1355 * later), followed by the elements added below by 1356 * calling ieee80211_assoc_link_elems(). 1357 */ 1358 capab_pos = skb_put(skb, 2); 1359 1360 extra_used = ieee80211_assoc_link_elems(sdata, skb, &capab, 1361 ext_capa, 1362 extra_elems, 1363 extra_elems_len, 1364 link_id, NULL, 1365 link_present_elems); 1366 if (extra_elems) 1367 skb_put_data(skb, extra_elems + extra_used, 1368 extra_elems_len - extra_used); 1369 1370 put_unaligned_le16(capab, capab_pos); 1371 1372 ieee80211_add_non_inheritance_elem(skb, outer_present_elems, 1373 link_present_elems); 1374 1375 ieee80211_fragment_element(skb, subelem_len, 1376 IEEE80211_MLE_SUBELEM_FRAGMENT); 1377 } 1378 1379 ieee80211_fragment_element(skb, ml_elem_len, WLAN_EID_FRAGMENT); 1380 } 1381 1382 static int ieee80211_send_assoc(struct ieee80211_sub_if_data *sdata) 1383 { 1384 struct ieee80211_local *local = sdata->local; 1385 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 1386 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data; 1387 struct ieee80211_link_data *link; 1388 struct sk_buff *skb; 1389 struct ieee80211_mgmt *mgmt; 1390 u8 *pos, qos_info, *ie_start; 1391 size_t offset, noffset; 1392 u16 capab = 0, link_capab; 1393 __le16 listen_int; 1394 struct element *ext_capa = NULL; 1395 enum nl80211_iftype iftype = ieee80211_vif_type_p2p(&sdata->vif); 1396 struct ieee80211_prep_tx_info info = {}; 1397 unsigned int link_id, n_links = 0; 1398 u16 present_elems[PRESENT_ELEMS_MAX] = {}; 1399 void *capab_pos; 1400 size_t size; 1401 int ret; 1402 1403 /* we know it's writable, cast away the const */ 1404 if (assoc_data->ie_len) 1405 ext_capa = (void *)cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, 1406 assoc_data->ie, 1407 assoc_data->ie_len); 1408 1409 lockdep_assert_wiphy(sdata->local->hw.wiphy); 1410 1411 size = local->hw.extra_tx_headroom + 1412 sizeof(*mgmt) + /* bit too much but doesn't matter */ 1413 2 + assoc_data->ssid_len + /* SSID */ 1414 assoc_data->ie_len + /* extra IEs */ 1415 (assoc_data->fils_kek_len ? 16 /* AES-SIV */ : 0) + 1416 9; /* WMM */ 1417 1418 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) { 1419 struct cfg80211_bss *cbss = assoc_data->link[link_id].bss; 1420 const struct ieee80211_sband_iftype_data *iftd; 1421 struct ieee80211_supported_band *sband; 1422 1423 if (!cbss) 1424 continue; 1425 1426 sband = local->hw.wiphy->bands[cbss->channel->band]; 1427 1428 n_links++; 1429 /* add STA profile elements length */ 1430 size += assoc_data->link[link_id].elems_len; 1431 /* and supported rates length */ 1432 size += 4 + sband->n_bitrates; 1433 /* supported channels */ 1434 size += 2 + 2 * sband->n_channels; 1435 1436 iftd = ieee80211_get_sband_iftype_data(sband, iftype); 1437 if (iftd) 1438 size += iftd->vendor_elems.len; 1439 1440 /* power capability */ 1441 size += 4; 1442 1443 /* HT, VHT, HE, EHT */ 1444 size += 2 + sizeof(struct ieee80211_ht_cap); 1445 size += 2 + sizeof(struct ieee80211_vht_cap); 1446 size += 2 + 1 + sizeof(struct ieee80211_he_cap_elem) + 1447 sizeof(struct ieee80211_he_mcs_nss_supp) + 1448 IEEE80211_HE_PPE_THRES_MAX_LEN; 1449 1450 if (sband->band == NL80211_BAND_6GHZ) 1451 size += 2 + 1 + sizeof(struct ieee80211_he_6ghz_capa); 1452 1453 size += 2 + 1 + sizeof(struct ieee80211_eht_cap_elem) + 1454 sizeof(struct ieee80211_eht_mcs_nss_supp) + 1455 IEEE80211_EHT_PPE_THRES_MAX_LEN; 1456 1457 /* non-inheritance element */ 1458 size += 2 + 2 + PRESENT_ELEMS_MAX; 1459 1460 /* should be the same across all BSSes */ 1461 if (cbss->capability & WLAN_CAPABILITY_PRIVACY) 1462 capab |= WLAN_CAPABILITY_PRIVACY; 1463 } 1464 1465 if (ieee80211_vif_is_mld(&sdata->vif)) { 1466 /* consider the multi-link element with STA profile */ 1467 size += sizeof(struct ieee80211_multi_link_elem); 1468 /* max common info field in basic multi-link element */ 1469 size += sizeof(struct ieee80211_mle_basic_common_info) + 1470 2 + /* capa & op */ 1471 2; /* EML capa */ 1472 1473 /* 1474 * The capability elements were already considered above; 1475 * note this over-estimates a bit because there's no 1476 * STA profile for the assoc link. 1477 */ 1478 size += (n_links - 1) * 1479 (1 + 1 + /* subelement ID/length */ 1480 2 + /* STA control */ 1481 1 + ETH_ALEN + 2 /* STA Info field */); 1482 } 1483 1484 link = sdata_dereference(sdata->link[assoc_data->assoc_link_id], sdata); 1485 if (WARN_ON(!link)) 1486 return -EINVAL; 1487 1488 if (WARN_ON(!assoc_data->link[assoc_data->assoc_link_id].bss)) 1489 return -EINVAL; 1490 1491 skb = alloc_skb(size, GFP_KERNEL); 1492 if (!skb) 1493 return -ENOMEM; 1494 1495 skb_reserve(skb, local->hw.extra_tx_headroom); 1496 1497 if (ifmgd->flags & IEEE80211_STA_ENABLE_RRM) 1498 capab |= WLAN_CAPABILITY_RADIO_MEASURE; 1499 1500 /* Set MBSSID support for HE AP if needed */ 1501 if (ieee80211_hw_check(&local->hw, SUPPORTS_ONLY_HE_MULTI_BSSID) && 1502 !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HE) && 1503 ext_capa && ext_capa->datalen >= 3) 1504 ext_capa->data[2] |= WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT; 1505 1506 mgmt = skb_put_zero(skb, 24); 1507 memcpy(mgmt->da, sdata->vif.cfg.ap_addr, ETH_ALEN); 1508 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN); 1509 memcpy(mgmt->bssid, sdata->vif.cfg.ap_addr, ETH_ALEN); 1510 1511 listen_int = cpu_to_le16(assoc_data->s1g ? 1512 ieee80211_encode_usf(local->hw.conf.listen_interval) : 1513 local->hw.conf.listen_interval); 1514 if (!is_zero_ether_addr(assoc_data->prev_ap_addr)) { 1515 skb_put(skb, 10); 1516 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | 1517 IEEE80211_STYPE_REASSOC_REQ); 1518 capab_pos = &mgmt->u.reassoc_req.capab_info; 1519 mgmt->u.reassoc_req.listen_interval = listen_int; 1520 memcpy(mgmt->u.reassoc_req.current_ap, 1521 assoc_data->prev_ap_addr, ETH_ALEN); 1522 info.subtype = IEEE80211_STYPE_REASSOC_REQ; 1523 } else { 1524 skb_put(skb, 4); 1525 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | 1526 IEEE80211_STYPE_ASSOC_REQ); 1527 capab_pos = &mgmt->u.assoc_req.capab_info; 1528 mgmt->u.assoc_req.listen_interval = listen_int; 1529 info.subtype = IEEE80211_STYPE_ASSOC_REQ; 1530 } 1531 1532 /* SSID */ 1533 pos = skb_put(skb, 2 + assoc_data->ssid_len); 1534 ie_start = pos; 1535 *pos++ = WLAN_EID_SSID; 1536 *pos++ = assoc_data->ssid_len; 1537 memcpy(pos, assoc_data->ssid, assoc_data->ssid_len); 1538 1539 /* 1540 * This bit is technically reserved, so it shouldn't matter for either 1541 * the AP or us, but it also means we shouldn't set it. However, we've 1542 * always set it in the past, and apparently some EHT APs check that 1543 * we don't set it. To avoid interoperability issues with old APs that 1544 * for some reason check it and want it to be set, set the bit for all 1545 * pre-EHT connections as we used to do. 1546 */ 1547 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_EHT) 1548 capab |= WLAN_CAPABILITY_ESS; 1549 1550 /* add the elements for the assoc (main) link */ 1551 link_capab = capab; 1552 offset = ieee80211_assoc_link_elems(sdata, skb, &link_capab, 1553 ext_capa, 1554 assoc_data->ie, 1555 assoc_data->ie_len, 1556 assoc_data->assoc_link_id, link, 1557 present_elems); 1558 put_unaligned_le16(link_capab, capab_pos); 1559 1560 /* if present, add any custom non-vendor IEs */ 1561 if (assoc_data->ie_len) { 1562 noffset = ieee80211_ie_split_vendor(assoc_data->ie, 1563 assoc_data->ie_len, 1564 offset); 1565 skb_put_data(skb, assoc_data->ie + offset, noffset - offset); 1566 offset = noffset; 1567 } 1568 1569 if (assoc_data->wmm) { 1570 if (assoc_data->uapsd) { 1571 qos_info = ifmgd->uapsd_queues; 1572 qos_info |= (ifmgd->uapsd_max_sp_len << 1573 IEEE80211_WMM_IE_STA_QOSINFO_SP_SHIFT); 1574 } else { 1575 qos_info = 0; 1576 } 1577 1578 pos = ieee80211_add_wmm_info_ie(skb_put(skb, 9), qos_info); 1579 } 1580 1581 /* add any remaining custom (i.e. vendor specific here) IEs */ 1582 if (assoc_data->ie_len) { 1583 noffset = assoc_data->ie_len; 1584 skb_put_data(skb, assoc_data->ie + offset, noffset - offset); 1585 } 1586 1587 if (assoc_data->fils_kek_len) { 1588 ret = fils_encrypt_assoc_req(skb, assoc_data); 1589 if (ret < 0) { 1590 dev_kfree_skb(skb); 1591 return ret; 1592 } 1593 } 1594 1595 pos = skb_tail_pointer(skb); 1596 kfree(ifmgd->assoc_req_ies); 1597 ifmgd->assoc_req_ies = kmemdup(ie_start, pos - ie_start, GFP_ATOMIC); 1598 if (!ifmgd->assoc_req_ies) { 1599 dev_kfree_skb(skb); 1600 return -ENOMEM; 1601 } 1602 1603 ifmgd->assoc_req_ies_len = pos - ie_start; 1604 1605 info.link_id = assoc_data->assoc_link_id; 1606 drv_mgd_prepare_tx(local, sdata, &info); 1607 1608 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT; 1609 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) 1610 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS | 1611 IEEE80211_TX_INTFL_MLME_CONN_TX; 1612 ieee80211_tx_skb(sdata, skb); 1613 1614 return 0; 1615 } 1616 1617 void ieee80211_send_pspoll(struct ieee80211_local *local, 1618 struct ieee80211_sub_if_data *sdata) 1619 { 1620 struct ieee80211_pspoll *pspoll; 1621 struct sk_buff *skb; 1622 1623 skb = ieee80211_pspoll_get(&local->hw, &sdata->vif); 1624 if (!skb) 1625 return; 1626 1627 pspoll = (struct ieee80211_pspoll *) skb->data; 1628 pspoll->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM); 1629 1630 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT; 1631 ieee80211_tx_skb(sdata, skb); 1632 } 1633 1634 void ieee80211_send_nullfunc(struct ieee80211_local *local, 1635 struct ieee80211_sub_if_data *sdata, 1636 bool powersave) 1637 { 1638 struct sk_buff *skb; 1639 struct ieee80211_hdr_3addr *nullfunc; 1640 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 1641 1642 skb = ieee80211_nullfunc_get(&local->hw, &sdata->vif, -1, 1643 !ieee80211_hw_check(&local->hw, 1644 DOESNT_SUPPORT_QOS_NDP)); 1645 if (!skb) 1646 return; 1647 1648 nullfunc = (struct ieee80211_hdr_3addr *) skb->data; 1649 if (powersave) 1650 nullfunc->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM); 1651 1652 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT | 1653 IEEE80211_TX_INTFL_OFFCHAN_TX_OK; 1654 1655 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) 1656 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS; 1657 1658 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) 1659 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE; 1660 1661 ieee80211_tx_skb(sdata, skb); 1662 } 1663 1664 void ieee80211_send_4addr_nullfunc(struct ieee80211_local *local, 1665 struct ieee80211_sub_if_data *sdata) 1666 { 1667 struct sk_buff *skb; 1668 struct ieee80211_hdr *nullfunc; 1669 __le16 fc; 1670 1671 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION)) 1672 return; 1673 1674 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 30); 1675 if (!skb) 1676 return; 1677 1678 skb_reserve(skb, local->hw.extra_tx_headroom); 1679 1680 nullfunc = skb_put_zero(skb, 30); 1681 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC | 1682 IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS); 1683 nullfunc->frame_control = fc; 1684 memcpy(nullfunc->addr1, sdata->deflink.u.mgd.bssid, ETH_ALEN); 1685 memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN); 1686 memcpy(nullfunc->addr3, sdata->deflink.u.mgd.bssid, ETH_ALEN); 1687 memcpy(nullfunc->addr4, sdata->vif.addr, ETH_ALEN); 1688 1689 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT; 1690 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE; 1691 ieee80211_tx_skb(sdata, skb); 1692 } 1693 1694 /* spectrum management related things */ 1695 static void ieee80211_chswitch_work(struct wiphy *wiphy, 1696 struct wiphy_work *work) 1697 { 1698 struct ieee80211_link_data *link = 1699 container_of(work, struct ieee80211_link_data, 1700 u.mgd.chswitch_work.work); 1701 struct ieee80211_sub_if_data *sdata = link->sdata; 1702 struct ieee80211_local *local = sdata->local; 1703 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 1704 int ret; 1705 1706 if (!ieee80211_sdata_running(sdata)) 1707 return; 1708 1709 lockdep_assert_wiphy(local->hw.wiphy); 1710 1711 if (!ifmgd->associated) 1712 return; 1713 1714 if (!link->conf->csa_active) 1715 return; 1716 1717 /* 1718 * using reservation isn't immediate as it may be deferred until later 1719 * with multi-vif. once reservation is complete it will re-schedule the 1720 * work with no reserved_chanctx so verify chandef to check if it 1721 * completed successfully 1722 */ 1723 1724 if (link->reserved_chanctx) { 1725 /* 1726 * with multi-vif csa driver may call ieee80211_csa_finish() 1727 * many times while waiting for other interfaces to use their 1728 * reservations 1729 */ 1730 if (link->reserved_ready) 1731 return; 1732 1733 ret = ieee80211_link_use_reserved_context(link); 1734 if (ret) { 1735 sdata_info(sdata, 1736 "failed to use reserved channel context, disconnecting (err=%d)\n", 1737 ret); 1738 wiphy_work_queue(sdata->local->hw.wiphy, 1739 &ifmgd->csa_connection_drop_work); 1740 } 1741 return; 1742 } 1743 1744 if (!cfg80211_chandef_identical(&link->conf->chandef, 1745 &link->csa_chandef)) { 1746 sdata_info(sdata, 1747 "failed to finalize channel switch, disconnecting\n"); 1748 wiphy_work_queue(sdata->local->hw.wiphy, 1749 &ifmgd->csa_connection_drop_work); 1750 return; 1751 } 1752 1753 link->u.mgd.csa_waiting_bcn = true; 1754 1755 ieee80211_sta_reset_beacon_monitor(sdata); 1756 ieee80211_sta_reset_conn_monitor(sdata); 1757 } 1758 1759 static void ieee80211_chswitch_post_beacon(struct ieee80211_link_data *link) 1760 { 1761 struct ieee80211_sub_if_data *sdata = link->sdata; 1762 struct ieee80211_local *local = sdata->local; 1763 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 1764 int ret; 1765 1766 lockdep_assert_wiphy(sdata->local->hw.wiphy); 1767 1768 WARN_ON(!link->conf->csa_active); 1769 1770 if (link->csa_block_tx) { 1771 ieee80211_wake_vif_queues(local, sdata, 1772 IEEE80211_QUEUE_STOP_REASON_CSA); 1773 link->csa_block_tx = false; 1774 } 1775 1776 link->conf->csa_active = false; 1777 link->u.mgd.csa_waiting_bcn = false; 1778 /* 1779 * If the CSA IE is still present on the beacon after the switch, 1780 * we need to consider it as a new CSA (possibly to self). 1781 */ 1782 link->u.mgd.beacon_crc_valid = false; 1783 1784 ret = drv_post_channel_switch(link); 1785 if (ret) { 1786 sdata_info(sdata, 1787 "driver post channel switch failed, disconnecting\n"); 1788 wiphy_work_queue(sdata->local->hw.wiphy, 1789 &ifmgd->csa_connection_drop_work); 1790 return; 1791 } 1792 1793 cfg80211_ch_switch_notify(sdata->dev, &link->reserved_chandef, 1794 link->link_id, 0); 1795 } 1796 1797 void ieee80211_chswitch_done(struct ieee80211_vif *vif, bool success, 1798 unsigned int link_id) 1799 { 1800 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 1801 1802 trace_api_chswitch_done(sdata, success, link_id); 1803 1804 rcu_read_lock(); 1805 1806 if (!success) { 1807 sdata_info(sdata, 1808 "driver channel switch failed, disconnecting\n"); 1809 wiphy_work_queue(sdata->local->hw.wiphy, 1810 &sdata->u.mgd.csa_connection_drop_work); 1811 } else { 1812 struct ieee80211_link_data *link = 1813 rcu_dereference(sdata->link[link_id]); 1814 1815 if (WARN_ON(!link)) { 1816 rcu_read_unlock(); 1817 return; 1818 } 1819 1820 wiphy_delayed_work_queue(sdata->local->hw.wiphy, 1821 &link->u.mgd.chswitch_work, 0); 1822 } 1823 1824 rcu_read_unlock(); 1825 } 1826 EXPORT_SYMBOL(ieee80211_chswitch_done); 1827 1828 static void 1829 ieee80211_sta_abort_chanswitch(struct ieee80211_link_data *link) 1830 { 1831 struct ieee80211_sub_if_data *sdata = link->sdata; 1832 struct ieee80211_local *local = sdata->local; 1833 1834 lockdep_assert_wiphy(local->hw.wiphy); 1835 1836 if (!local->ops->abort_channel_switch) 1837 return; 1838 1839 ieee80211_link_unreserve_chanctx(link); 1840 1841 if (link->csa_block_tx) 1842 ieee80211_wake_vif_queues(local, sdata, 1843 IEEE80211_QUEUE_STOP_REASON_CSA); 1844 1845 link->csa_block_tx = false; 1846 link->conf->csa_active = false; 1847 1848 drv_abort_channel_switch(sdata); 1849 } 1850 1851 static void 1852 ieee80211_sta_process_chanswitch(struct ieee80211_link_data *link, 1853 u64 timestamp, u32 device_timestamp, 1854 struct ieee802_11_elems *elems, 1855 bool beacon) 1856 { 1857 struct ieee80211_sub_if_data *sdata = link->sdata; 1858 struct ieee80211_local *local = sdata->local; 1859 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 1860 struct cfg80211_bss *cbss = link->u.mgd.bss; 1861 struct ieee80211_chanctx_conf *conf; 1862 struct ieee80211_chanctx *chanctx; 1863 enum nl80211_band current_band; 1864 struct ieee80211_csa_ie csa_ie; 1865 struct ieee80211_channel_switch ch_switch; 1866 struct ieee80211_bss *bss; 1867 unsigned long timeout; 1868 int res; 1869 1870 lockdep_assert_wiphy(local->hw.wiphy); 1871 1872 if (!cbss) 1873 return; 1874 1875 current_band = cbss->channel->band; 1876 bss = (void *)cbss->priv; 1877 res = ieee80211_parse_ch_switch_ie(sdata, elems, current_band, 1878 bss->vht_cap_info, 1879 link->u.mgd.conn_flags, 1880 link->u.mgd.bssid, &csa_ie); 1881 1882 if (!res) { 1883 ch_switch.timestamp = timestamp; 1884 ch_switch.device_timestamp = device_timestamp; 1885 ch_switch.block_tx = csa_ie.mode; 1886 ch_switch.chandef = csa_ie.chandef; 1887 ch_switch.count = csa_ie.count; 1888 ch_switch.delay = csa_ie.max_switch_time; 1889 } 1890 1891 if (res < 0) 1892 goto drop_connection; 1893 1894 if (beacon && link->conf->csa_active && 1895 !link->u.mgd.csa_waiting_bcn) { 1896 if (res) 1897 ieee80211_sta_abort_chanswitch(link); 1898 else 1899 drv_channel_switch_rx_beacon(sdata, &ch_switch); 1900 return; 1901 } else if (link->conf->csa_active || res) { 1902 /* disregard subsequent announcements if already processing */ 1903 return; 1904 } 1905 1906 if (link->conf->chandef.chan->band != 1907 csa_ie.chandef.chan->band) { 1908 sdata_info(sdata, 1909 "AP %pM switches to different band (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n", 1910 link->u.mgd.bssid, 1911 csa_ie.chandef.chan->center_freq, 1912 csa_ie.chandef.width, csa_ie.chandef.center_freq1, 1913 csa_ie.chandef.center_freq2); 1914 goto drop_connection; 1915 } 1916 1917 if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef, 1918 IEEE80211_CHAN_DISABLED)) { 1919 sdata_info(sdata, 1920 "AP %pM switches to unsupported channel " 1921 "(%d.%03d MHz, width:%d, CF1/2: %d.%03d/%d MHz), " 1922 "disconnecting\n", 1923 link->u.mgd.bssid, 1924 csa_ie.chandef.chan->center_freq, 1925 csa_ie.chandef.chan->freq_offset, 1926 csa_ie.chandef.width, csa_ie.chandef.center_freq1, 1927 csa_ie.chandef.freq1_offset, 1928 csa_ie.chandef.center_freq2); 1929 goto drop_connection; 1930 } 1931 1932 if (cfg80211_chandef_identical(&csa_ie.chandef, 1933 &link->conf->chandef) && 1934 (!csa_ie.mode || !beacon)) { 1935 if (link->u.mgd.csa_ignored_same_chan) 1936 return; 1937 sdata_info(sdata, 1938 "AP %pM tries to chanswitch to same channel, ignore\n", 1939 link->u.mgd.bssid); 1940 link->u.mgd.csa_ignored_same_chan = true; 1941 return; 1942 } 1943 1944 /* 1945 * Drop all TDLS peers - either we disconnect or move to a different 1946 * channel from this point on. There's no telling what our peer will do. 1947 * The TDLS WIDER_BW scenario is also problematic, as peers might now 1948 * have an incompatible wider chandef. 1949 */ 1950 ieee80211_teardown_tdls_peers(sdata); 1951 1952 conf = rcu_dereference_protected(link->conf->chanctx_conf, 1953 lockdep_is_held(&local->hw.wiphy->mtx)); 1954 if (!conf) { 1955 sdata_info(sdata, 1956 "no channel context assigned to vif?, disconnecting\n"); 1957 goto drop_connection; 1958 } 1959 1960 chanctx = container_of(conf, struct ieee80211_chanctx, conf); 1961 1962 if (local->use_chanctx && 1963 !ieee80211_hw_check(&local->hw, CHANCTX_STA_CSA)) { 1964 sdata_info(sdata, 1965 "driver doesn't support chan-switch with channel contexts\n"); 1966 goto drop_connection; 1967 } 1968 1969 if (drv_pre_channel_switch(sdata, &ch_switch)) { 1970 sdata_info(sdata, 1971 "preparing for channel switch failed, disconnecting\n"); 1972 goto drop_connection; 1973 } 1974 1975 res = ieee80211_link_reserve_chanctx(link, &csa_ie.chandef, 1976 chanctx->mode, false); 1977 if (res) { 1978 sdata_info(sdata, 1979 "failed to reserve channel context for channel switch, disconnecting (err=%d)\n", 1980 res); 1981 goto drop_connection; 1982 } 1983 1984 link->conf->csa_active = true; 1985 link->csa_chandef = csa_ie.chandef; 1986 link->csa_block_tx = csa_ie.mode; 1987 link->u.mgd.csa_ignored_same_chan = false; 1988 link->u.mgd.beacon_crc_valid = false; 1989 1990 if (link->csa_block_tx) 1991 ieee80211_stop_vif_queues(local, sdata, 1992 IEEE80211_QUEUE_STOP_REASON_CSA); 1993 1994 cfg80211_ch_switch_started_notify(sdata->dev, &csa_ie.chandef, 1995 link->link_id, csa_ie.count, 1996 csa_ie.mode, 0); 1997 1998 if (local->ops->channel_switch) { 1999 /* use driver's channel switch callback */ 2000 drv_channel_switch(local, sdata, &ch_switch); 2001 return; 2002 } 2003 2004 /* channel switch handled in software */ 2005 timeout = TU_TO_JIFFIES((max_t(int, csa_ie.count, 1) - 1) * 2006 cbss->beacon_interval); 2007 wiphy_delayed_work_queue(local->hw.wiphy, 2008 &link->u.mgd.chswitch_work, 2009 timeout); 2010 return; 2011 drop_connection: 2012 /* 2013 * This is just so that the disconnect flow will know that 2014 * we were trying to switch channel and failed. In case the 2015 * mode is 1 (we are not allowed to Tx), we will know not to 2016 * send a deauthentication frame. Those two fields will be 2017 * reset when the disconnection worker runs. 2018 */ 2019 link->conf->csa_active = true; 2020 link->csa_block_tx = csa_ie.mode; 2021 2022 wiphy_work_queue(sdata->local->hw.wiphy, 2023 &ifmgd->csa_connection_drop_work); 2024 } 2025 2026 static bool 2027 ieee80211_find_80211h_pwr_constr(struct ieee80211_sub_if_data *sdata, 2028 struct ieee80211_channel *channel, 2029 const u8 *country_ie, u8 country_ie_len, 2030 const u8 *pwr_constr_elem, 2031 int *chan_pwr, int *pwr_reduction) 2032 { 2033 struct ieee80211_country_ie_triplet *triplet; 2034 int chan = ieee80211_frequency_to_channel(channel->center_freq); 2035 int i, chan_increment; 2036 bool have_chan_pwr = false; 2037 2038 /* Invalid IE */ 2039 if (country_ie_len % 2 || country_ie_len < IEEE80211_COUNTRY_IE_MIN_LEN) 2040 return false; 2041 2042 triplet = (void *)(country_ie + 3); 2043 country_ie_len -= 3; 2044 2045 switch (channel->band) { 2046 default: 2047 WARN_ON_ONCE(1); 2048 fallthrough; 2049 case NL80211_BAND_2GHZ: 2050 case NL80211_BAND_60GHZ: 2051 case NL80211_BAND_LC: 2052 chan_increment = 1; 2053 break; 2054 case NL80211_BAND_5GHZ: 2055 chan_increment = 4; 2056 break; 2057 case NL80211_BAND_6GHZ: 2058 /* 2059 * In the 6 GHz band, the "maximum transmit power level" 2060 * field in the triplets is reserved, and thus will be 2061 * zero and we shouldn't use it to control TX power. 2062 * The actual TX power will be given in the transmit 2063 * power envelope element instead. 2064 */ 2065 return false; 2066 } 2067 2068 /* find channel */ 2069 while (country_ie_len >= 3) { 2070 u8 first_channel = triplet->chans.first_channel; 2071 2072 if (first_channel >= IEEE80211_COUNTRY_EXTENSION_ID) 2073 goto next; 2074 2075 for (i = 0; i < triplet->chans.num_channels; i++) { 2076 if (first_channel + i * chan_increment == chan) { 2077 have_chan_pwr = true; 2078 *chan_pwr = triplet->chans.max_power; 2079 break; 2080 } 2081 } 2082 if (have_chan_pwr) 2083 break; 2084 2085 next: 2086 triplet++; 2087 country_ie_len -= 3; 2088 } 2089 2090 if (have_chan_pwr && pwr_constr_elem) 2091 *pwr_reduction = *pwr_constr_elem; 2092 else 2093 *pwr_reduction = 0; 2094 2095 return have_chan_pwr; 2096 } 2097 2098 static void ieee80211_find_cisco_dtpc(struct ieee80211_sub_if_data *sdata, 2099 struct ieee80211_channel *channel, 2100 const u8 *cisco_dtpc_ie, 2101 int *pwr_level) 2102 { 2103 /* From practical testing, the first data byte of the DTPC element 2104 * seems to contain the requested dBm level, and the CLI on Cisco 2105 * APs clearly state the range is -127 to 127 dBm, which indicates 2106 * a signed byte, although it seemingly never actually goes negative. 2107 * The other byte seems to always be zero. 2108 */ 2109 *pwr_level = (__s8)cisco_dtpc_ie[4]; 2110 } 2111 2112 static u64 ieee80211_handle_pwr_constr(struct ieee80211_link_data *link, 2113 struct ieee80211_channel *channel, 2114 struct ieee80211_mgmt *mgmt, 2115 const u8 *country_ie, u8 country_ie_len, 2116 const u8 *pwr_constr_ie, 2117 const u8 *cisco_dtpc_ie) 2118 { 2119 struct ieee80211_sub_if_data *sdata = link->sdata; 2120 bool has_80211h_pwr = false, has_cisco_pwr = false; 2121 int chan_pwr = 0, pwr_reduction_80211h = 0; 2122 int pwr_level_cisco, pwr_level_80211h; 2123 int new_ap_level; 2124 __le16 capab = mgmt->u.probe_resp.capab_info; 2125 2126 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) 2127 return 0; /* TODO */ 2128 2129 if (country_ie && 2130 (capab & cpu_to_le16(WLAN_CAPABILITY_SPECTRUM_MGMT) || 2131 capab & cpu_to_le16(WLAN_CAPABILITY_RADIO_MEASURE))) { 2132 has_80211h_pwr = ieee80211_find_80211h_pwr_constr( 2133 sdata, channel, country_ie, country_ie_len, 2134 pwr_constr_ie, &chan_pwr, &pwr_reduction_80211h); 2135 pwr_level_80211h = 2136 max_t(int, 0, chan_pwr - pwr_reduction_80211h); 2137 } 2138 2139 if (cisco_dtpc_ie) { 2140 ieee80211_find_cisco_dtpc( 2141 sdata, channel, cisco_dtpc_ie, &pwr_level_cisco); 2142 has_cisco_pwr = true; 2143 } 2144 2145 if (!has_80211h_pwr && !has_cisco_pwr) 2146 return 0; 2147 2148 /* If we have both 802.11h and Cisco DTPC, apply both limits 2149 * by picking the smallest of the two power levels advertised. 2150 */ 2151 if (has_80211h_pwr && 2152 (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) { 2153 new_ap_level = pwr_level_80211h; 2154 2155 if (link->ap_power_level == new_ap_level) 2156 return 0; 2157 2158 sdata_dbg(sdata, 2159 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n", 2160 pwr_level_80211h, chan_pwr, pwr_reduction_80211h, 2161 link->u.mgd.bssid); 2162 } else { /* has_cisco_pwr is always true here. */ 2163 new_ap_level = pwr_level_cisco; 2164 2165 if (link->ap_power_level == new_ap_level) 2166 return 0; 2167 2168 sdata_dbg(sdata, 2169 "Limiting TX power to %d dBm as advertised by %pM\n", 2170 pwr_level_cisco, link->u.mgd.bssid); 2171 } 2172 2173 link->ap_power_level = new_ap_level; 2174 if (__ieee80211_recalc_txpower(sdata)) 2175 return BSS_CHANGED_TXPOWER; 2176 return 0; 2177 } 2178 2179 /* powersave */ 2180 static void ieee80211_enable_ps(struct ieee80211_local *local, 2181 struct ieee80211_sub_if_data *sdata) 2182 { 2183 struct ieee80211_conf *conf = &local->hw.conf; 2184 2185 /* 2186 * If we are scanning right now then the parameters will 2187 * take effect when scan finishes. 2188 */ 2189 if (local->scanning) 2190 return; 2191 2192 if (conf->dynamic_ps_timeout > 0 && 2193 !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) { 2194 mod_timer(&local->dynamic_ps_timer, jiffies + 2195 msecs_to_jiffies(conf->dynamic_ps_timeout)); 2196 } else { 2197 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) 2198 ieee80211_send_nullfunc(local, sdata, true); 2199 2200 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) && 2201 ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) 2202 return; 2203 2204 conf->flags |= IEEE80211_CONF_PS; 2205 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS); 2206 } 2207 } 2208 2209 static void ieee80211_change_ps(struct ieee80211_local *local) 2210 { 2211 struct ieee80211_conf *conf = &local->hw.conf; 2212 2213 if (local->ps_sdata) { 2214 ieee80211_enable_ps(local, local->ps_sdata); 2215 } else if (conf->flags & IEEE80211_CONF_PS) { 2216 conf->flags &= ~IEEE80211_CONF_PS; 2217 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS); 2218 del_timer_sync(&local->dynamic_ps_timer); 2219 wiphy_work_cancel(local->hw.wiphy, 2220 &local->dynamic_ps_enable_work); 2221 } 2222 } 2223 2224 static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata) 2225 { 2226 struct ieee80211_local *local = sdata->local; 2227 struct ieee80211_if_managed *mgd = &sdata->u.mgd; 2228 struct sta_info *sta = NULL; 2229 bool authorized = false; 2230 2231 if (!mgd->powersave) 2232 return false; 2233 2234 if (mgd->broken_ap) 2235 return false; 2236 2237 if (!mgd->associated) 2238 return false; 2239 2240 if (mgd->flags & IEEE80211_STA_CONNECTION_POLL) 2241 return false; 2242 2243 if (!(local->hw.wiphy->flags & WIPHY_FLAG_SUPPORTS_MLO) && 2244 !sdata->deflink.u.mgd.have_beacon) 2245 return false; 2246 2247 rcu_read_lock(); 2248 sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr); 2249 if (sta) 2250 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED); 2251 rcu_read_unlock(); 2252 2253 return authorized; 2254 } 2255 2256 /* need to hold RTNL or interface lock */ 2257 void ieee80211_recalc_ps(struct ieee80211_local *local) 2258 { 2259 struct ieee80211_sub_if_data *sdata, *found = NULL; 2260 int count = 0; 2261 int timeout; 2262 2263 if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS) || 2264 ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS)) { 2265 local->ps_sdata = NULL; 2266 return; 2267 } 2268 2269 list_for_each_entry(sdata, &local->interfaces, list) { 2270 if (!ieee80211_sdata_running(sdata)) 2271 continue; 2272 if (sdata->vif.type == NL80211_IFTYPE_AP) { 2273 /* If an AP vif is found, then disable PS 2274 * by setting the count to zero thereby setting 2275 * ps_sdata to NULL. 2276 */ 2277 count = 0; 2278 break; 2279 } 2280 if (sdata->vif.type != NL80211_IFTYPE_STATION) 2281 continue; 2282 found = sdata; 2283 count++; 2284 } 2285 2286 if (count == 1 && ieee80211_powersave_allowed(found)) { 2287 u8 dtimper = found->deflink.u.mgd.dtim_period; 2288 2289 timeout = local->dynamic_ps_forced_timeout; 2290 if (timeout < 0) 2291 timeout = 100; 2292 local->hw.conf.dynamic_ps_timeout = timeout; 2293 2294 /* If the TIM IE is invalid, pretend the value is 1 */ 2295 if (!dtimper) 2296 dtimper = 1; 2297 2298 local->hw.conf.ps_dtim_period = dtimper; 2299 local->ps_sdata = found; 2300 } else { 2301 local->ps_sdata = NULL; 2302 } 2303 2304 ieee80211_change_ps(local); 2305 } 2306 2307 void ieee80211_recalc_ps_vif(struct ieee80211_sub_if_data *sdata) 2308 { 2309 bool ps_allowed = ieee80211_powersave_allowed(sdata); 2310 2311 if (sdata->vif.cfg.ps != ps_allowed) { 2312 sdata->vif.cfg.ps = ps_allowed; 2313 ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_PS); 2314 } 2315 } 2316 2317 void ieee80211_dynamic_ps_disable_work(struct wiphy *wiphy, 2318 struct wiphy_work *work) 2319 { 2320 struct ieee80211_local *local = 2321 container_of(work, struct ieee80211_local, 2322 dynamic_ps_disable_work); 2323 2324 if (local->hw.conf.flags & IEEE80211_CONF_PS) { 2325 local->hw.conf.flags &= ~IEEE80211_CONF_PS; 2326 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS); 2327 } 2328 2329 ieee80211_wake_queues_by_reason(&local->hw, 2330 IEEE80211_MAX_QUEUE_MAP, 2331 IEEE80211_QUEUE_STOP_REASON_PS, 2332 false); 2333 } 2334 2335 void ieee80211_dynamic_ps_enable_work(struct wiphy *wiphy, 2336 struct wiphy_work *work) 2337 { 2338 struct ieee80211_local *local = 2339 container_of(work, struct ieee80211_local, 2340 dynamic_ps_enable_work); 2341 struct ieee80211_sub_if_data *sdata = local->ps_sdata; 2342 struct ieee80211_if_managed *ifmgd; 2343 unsigned long flags; 2344 int q; 2345 2346 /* can only happen when PS was just disabled anyway */ 2347 if (!sdata) 2348 return; 2349 2350 ifmgd = &sdata->u.mgd; 2351 2352 if (local->hw.conf.flags & IEEE80211_CONF_PS) 2353 return; 2354 2355 if (local->hw.conf.dynamic_ps_timeout > 0) { 2356 /* don't enter PS if TX frames are pending */ 2357 if (drv_tx_frames_pending(local)) { 2358 mod_timer(&local->dynamic_ps_timer, jiffies + 2359 msecs_to_jiffies( 2360 local->hw.conf.dynamic_ps_timeout)); 2361 return; 2362 } 2363 2364 /* 2365 * transmission can be stopped by others which leads to 2366 * dynamic_ps_timer expiry. Postpone the ps timer if it 2367 * is not the actual idle state. 2368 */ 2369 spin_lock_irqsave(&local->queue_stop_reason_lock, flags); 2370 for (q = 0; q < local->hw.queues; q++) { 2371 if (local->queue_stop_reasons[q]) { 2372 spin_unlock_irqrestore(&local->queue_stop_reason_lock, 2373 flags); 2374 mod_timer(&local->dynamic_ps_timer, jiffies + 2375 msecs_to_jiffies( 2376 local->hw.conf.dynamic_ps_timeout)); 2377 return; 2378 } 2379 } 2380 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 2381 } 2382 2383 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) && 2384 !(ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) { 2385 if (drv_tx_frames_pending(local)) { 2386 mod_timer(&local->dynamic_ps_timer, jiffies + 2387 msecs_to_jiffies( 2388 local->hw.conf.dynamic_ps_timeout)); 2389 } else { 2390 ieee80211_send_nullfunc(local, sdata, true); 2391 /* Flush to get the tx status of nullfunc frame */ 2392 ieee80211_flush_queues(local, sdata, false); 2393 } 2394 } 2395 2396 if (!(ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS) && 2397 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK)) || 2398 (ifmgd->flags & IEEE80211_STA_NULLFUNC_ACKED)) { 2399 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED; 2400 local->hw.conf.flags |= IEEE80211_CONF_PS; 2401 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS); 2402 } 2403 } 2404 2405 void ieee80211_dynamic_ps_timer(struct timer_list *t) 2406 { 2407 struct ieee80211_local *local = from_timer(local, t, dynamic_ps_timer); 2408 2409 wiphy_work_queue(local->hw.wiphy, &local->dynamic_ps_enable_work); 2410 } 2411 2412 void ieee80211_dfs_cac_timer_work(struct wiphy *wiphy, struct wiphy_work *work) 2413 { 2414 struct ieee80211_link_data *link = 2415 container_of(work, struct ieee80211_link_data, 2416 dfs_cac_timer_work.work); 2417 struct cfg80211_chan_def chandef = link->conf->chandef; 2418 struct ieee80211_sub_if_data *sdata = link->sdata; 2419 2420 lockdep_assert_wiphy(sdata->local->hw.wiphy); 2421 2422 if (sdata->wdev.cac_started) { 2423 ieee80211_link_release_channel(link); 2424 cfg80211_cac_event(sdata->dev, &chandef, 2425 NL80211_RADAR_CAC_FINISHED, 2426 GFP_KERNEL); 2427 } 2428 } 2429 2430 static bool 2431 __ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata) 2432 { 2433 struct ieee80211_local *local = sdata->local; 2434 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 2435 bool ret = false; 2436 int ac; 2437 2438 if (local->hw.queues < IEEE80211_NUM_ACS) 2439 return false; 2440 2441 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 2442 struct ieee80211_sta_tx_tspec *tx_tspec = &ifmgd->tx_tspec[ac]; 2443 int non_acm_ac; 2444 unsigned long now = jiffies; 2445 2446 if (tx_tspec->action == TX_TSPEC_ACTION_NONE && 2447 tx_tspec->admitted_time && 2448 time_after(now, tx_tspec->time_slice_start + HZ)) { 2449 tx_tspec->consumed_tx_time = 0; 2450 tx_tspec->time_slice_start = now; 2451 2452 if (tx_tspec->downgraded) 2453 tx_tspec->action = 2454 TX_TSPEC_ACTION_STOP_DOWNGRADE; 2455 } 2456 2457 switch (tx_tspec->action) { 2458 case TX_TSPEC_ACTION_STOP_DOWNGRADE: 2459 /* take the original parameters */ 2460 if (drv_conf_tx(local, &sdata->deflink, ac, 2461 &sdata->deflink.tx_conf[ac])) 2462 link_err(&sdata->deflink, 2463 "failed to set TX queue parameters for queue %d\n", 2464 ac); 2465 tx_tspec->action = TX_TSPEC_ACTION_NONE; 2466 tx_tspec->downgraded = false; 2467 ret = true; 2468 break; 2469 case TX_TSPEC_ACTION_DOWNGRADE: 2470 if (time_after(now, tx_tspec->time_slice_start + HZ)) { 2471 tx_tspec->action = TX_TSPEC_ACTION_NONE; 2472 ret = true; 2473 break; 2474 } 2475 /* downgrade next lower non-ACM AC */ 2476 for (non_acm_ac = ac + 1; 2477 non_acm_ac < IEEE80211_NUM_ACS; 2478 non_acm_ac++) 2479 if (!(sdata->wmm_acm & BIT(7 - 2 * non_acm_ac))) 2480 break; 2481 /* Usually the loop will result in using BK even if it 2482 * requires admission control, but such a configuration 2483 * makes no sense and we have to transmit somehow - the 2484 * AC selection does the same thing. 2485 * If we started out trying to downgrade from BK, then 2486 * the extra condition here might be needed. 2487 */ 2488 if (non_acm_ac >= IEEE80211_NUM_ACS) 2489 non_acm_ac = IEEE80211_AC_BK; 2490 if (drv_conf_tx(local, &sdata->deflink, ac, 2491 &sdata->deflink.tx_conf[non_acm_ac])) 2492 link_err(&sdata->deflink, 2493 "failed to set TX queue parameters for queue %d\n", 2494 ac); 2495 tx_tspec->action = TX_TSPEC_ACTION_NONE; 2496 ret = true; 2497 wiphy_delayed_work_queue(local->hw.wiphy, 2498 &ifmgd->tx_tspec_wk, 2499 tx_tspec->time_slice_start + 2500 HZ - now + 1); 2501 break; 2502 case TX_TSPEC_ACTION_NONE: 2503 /* nothing now */ 2504 break; 2505 } 2506 } 2507 2508 return ret; 2509 } 2510 2511 void ieee80211_sta_handle_tspec_ac_params(struct ieee80211_sub_if_data *sdata) 2512 { 2513 if (__ieee80211_sta_handle_tspec_ac_params(sdata)) 2514 ieee80211_link_info_change_notify(sdata, &sdata->deflink, 2515 BSS_CHANGED_QOS); 2516 } 2517 2518 static void ieee80211_sta_handle_tspec_ac_params_wk(struct wiphy *wiphy, 2519 struct wiphy_work *work) 2520 { 2521 struct ieee80211_sub_if_data *sdata; 2522 2523 sdata = container_of(work, struct ieee80211_sub_if_data, 2524 u.mgd.tx_tspec_wk.work); 2525 ieee80211_sta_handle_tspec_ac_params(sdata); 2526 } 2527 2528 void ieee80211_mgd_set_link_qos_params(struct ieee80211_link_data *link) 2529 { 2530 struct ieee80211_sub_if_data *sdata = link->sdata; 2531 struct ieee80211_local *local = sdata->local; 2532 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 2533 struct ieee80211_tx_queue_params *params = link->tx_conf; 2534 u8 ac; 2535 2536 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 2537 mlme_dbg(sdata, 2538 "WMM AC=%d acm=%d aifs=%d cWmin=%d cWmax=%d txop=%d uapsd=%d, downgraded=%d\n", 2539 ac, params[ac].acm, 2540 params[ac].aifs, params[ac].cw_min, params[ac].cw_max, 2541 params[ac].txop, params[ac].uapsd, 2542 ifmgd->tx_tspec[ac].downgraded); 2543 if (!ifmgd->tx_tspec[ac].downgraded && 2544 drv_conf_tx(local, link, ac, ¶ms[ac])) 2545 link_err(link, 2546 "failed to set TX queue parameters for AC %d\n", 2547 ac); 2548 } 2549 } 2550 2551 /* MLME */ 2552 static bool 2553 ieee80211_sta_wmm_params(struct ieee80211_local *local, 2554 struct ieee80211_link_data *link, 2555 const u8 *wmm_param, size_t wmm_param_len, 2556 const struct ieee80211_mu_edca_param_set *mu_edca) 2557 { 2558 struct ieee80211_sub_if_data *sdata = link->sdata; 2559 struct ieee80211_tx_queue_params params[IEEE80211_NUM_ACS]; 2560 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 2561 size_t left; 2562 int count, mu_edca_count, ac; 2563 const u8 *pos; 2564 u8 uapsd_queues = 0; 2565 2566 if (!local->ops->conf_tx) 2567 return false; 2568 2569 if (local->hw.queues < IEEE80211_NUM_ACS) 2570 return false; 2571 2572 if (!wmm_param) 2573 return false; 2574 2575 if (wmm_param_len < 8 || wmm_param[5] /* version */ != 1) 2576 return false; 2577 2578 if (ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) 2579 uapsd_queues = ifmgd->uapsd_queues; 2580 2581 count = wmm_param[6] & 0x0f; 2582 /* -1 is the initial value of ifmgd->mu_edca_last_param_set. 2583 * if mu_edca was preset before and now it disappeared tell 2584 * the driver about it. 2585 */ 2586 mu_edca_count = mu_edca ? mu_edca->mu_qos_info & 0x0f : -1; 2587 if (count == link->u.mgd.wmm_last_param_set && 2588 mu_edca_count == link->u.mgd.mu_edca_last_param_set) 2589 return false; 2590 link->u.mgd.wmm_last_param_set = count; 2591 link->u.mgd.mu_edca_last_param_set = mu_edca_count; 2592 2593 pos = wmm_param + 8; 2594 left = wmm_param_len - 8; 2595 2596 memset(¶ms, 0, sizeof(params)); 2597 2598 sdata->wmm_acm = 0; 2599 for (; left >= 4; left -= 4, pos += 4) { 2600 int aci = (pos[0] >> 5) & 0x03; 2601 int acm = (pos[0] >> 4) & 0x01; 2602 bool uapsd = false; 2603 2604 switch (aci) { 2605 case 1: /* AC_BK */ 2606 ac = IEEE80211_AC_BK; 2607 if (acm) 2608 sdata->wmm_acm |= BIT(1) | BIT(2); /* BK/- */ 2609 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK) 2610 uapsd = true; 2611 params[ac].mu_edca = !!mu_edca; 2612 if (mu_edca) 2613 params[ac].mu_edca_param_rec = mu_edca->ac_bk; 2614 break; 2615 case 2: /* AC_VI */ 2616 ac = IEEE80211_AC_VI; 2617 if (acm) 2618 sdata->wmm_acm |= BIT(4) | BIT(5); /* CL/VI */ 2619 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI) 2620 uapsd = true; 2621 params[ac].mu_edca = !!mu_edca; 2622 if (mu_edca) 2623 params[ac].mu_edca_param_rec = mu_edca->ac_vi; 2624 break; 2625 case 3: /* AC_VO */ 2626 ac = IEEE80211_AC_VO; 2627 if (acm) 2628 sdata->wmm_acm |= BIT(6) | BIT(7); /* VO/NC */ 2629 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) 2630 uapsd = true; 2631 params[ac].mu_edca = !!mu_edca; 2632 if (mu_edca) 2633 params[ac].mu_edca_param_rec = mu_edca->ac_vo; 2634 break; 2635 case 0: /* AC_BE */ 2636 default: 2637 ac = IEEE80211_AC_BE; 2638 if (acm) 2639 sdata->wmm_acm |= BIT(0) | BIT(3); /* BE/EE */ 2640 if (uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE) 2641 uapsd = true; 2642 params[ac].mu_edca = !!mu_edca; 2643 if (mu_edca) 2644 params[ac].mu_edca_param_rec = mu_edca->ac_be; 2645 break; 2646 } 2647 2648 params[ac].aifs = pos[0] & 0x0f; 2649 2650 if (params[ac].aifs < 2) { 2651 link_info(link, 2652 "AP has invalid WMM params (AIFSN=%d for ACI %d), will use 2\n", 2653 params[ac].aifs, aci); 2654 params[ac].aifs = 2; 2655 } 2656 params[ac].cw_max = ecw2cw((pos[1] & 0xf0) >> 4); 2657 params[ac].cw_min = ecw2cw(pos[1] & 0x0f); 2658 params[ac].txop = get_unaligned_le16(pos + 2); 2659 params[ac].acm = acm; 2660 params[ac].uapsd = uapsd; 2661 2662 if (params[ac].cw_min == 0 || 2663 params[ac].cw_min > params[ac].cw_max) { 2664 link_info(link, 2665 "AP has invalid WMM params (CWmin/max=%d/%d for ACI %d), using defaults\n", 2666 params[ac].cw_min, params[ac].cw_max, aci); 2667 return false; 2668 } 2669 ieee80211_regulatory_limit_wmm_params(sdata, ¶ms[ac], ac); 2670 } 2671 2672 /* WMM specification requires all 4 ACIs. */ 2673 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 2674 if (params[ac].cw_min == 0) { 2675 link_info(link, 2676 "AP has invalid WMM params (missing AC %d), using defaults\n", 2677 ac); 2678 return false; 2679 } 2680 } 2681 2682 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 2683 link->tx_conf[ac] = params[ac]; 2684 2685 ieee80211_mgd_set_link_qos_params(link); 2686 2687 /* enable WMM or activate new settings */ 2688 link->conf->qos = true; 2689 return true; 2690 } 2691 2692 static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata) 2693 { 2694 lockdep_assert_wiphy(sdata->local->hw.wiphy); 2695 2696 sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL; 2697 ieee80211_run_deferred_scan(sdata->local); 2698 } 2699 2700 static void ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata) 2701 { 2702 lockdep_assert_wiphy(sdata->local->hw.wiphy); 2703 2704 __ieee80211_stop_poll(sdata); 2705 } 2706 2707 static u64 ieee80211_handle_bss_capability(struct ieee80211_link_data *link, 2708 u16 capab, bool erp_valid, u8 erp) 2709 { 2710 struct ieee80211_bss_conf *bss_conf = link->conf; 2711 struct ieee80211_supported_band *sband; 2712 u64 changed = 0; 2713 bool use_protection; 2714 bool use_short_preamble; 2715 bool use_short_slot; 2716 2717 sband = ieee80211_get_link_sband(link); 2718 if (!sband) 2719 return changed; 2720 2721 if (erp_valid) { 2722 use_protection = (erp & WLAN_ERP_USE_PROTECTION) != 0; 2723 use_short_preamble = (erp & WLAN_ERP_BARKER_PREAMBLE) == 0; 2724 } else { 2725 use_protection = false; 2726 use_short_preamble = !!(capab & WLAN_CAPABILITY_SHORT_PREAMBLE); 2727 } 2728 2729 use_short_slot = !!(capab & WLAN_CAPABILITY_SHORT_SLOT_TIME); 2730 if (sband->band == NL80211_BAND_5GHZ || 2731 sband->band == NL80211_BAND_6GHZ) 2732 use_short_slot = true; 2733 2734 if (use_protection != bss_conf->use_cts_prot) { 2735 bss_conf->use_cts_prot = use_protection; 2736 changed |= BSS_CHANGED_ERP_CTS_PROT; 2737 } 2738 2739 if (use_short_preamble != bss_conf->use_short_preamble) { 2740 bss_conf->use_short_preamble = use_short_preamble; 2741 changed |= BSS_CHANGED_ERP_PREAMBLE; 2742 } 2743 2744 if (use_short_slot != bss_conf->use_short_slot) { 2745 bss_conf->use_short_slot = use_short_slot; 2746 changed |= BSS_CHANGED_ERP_SLOT; 2747 } 2748 2749 return changed; 2750 } 2751 2752 static u64 ieee80211_link_set_associated(struct ieee80211_link_data *link, 2753 struct cfg80211_bss *cbss) 2754 { 2755 struct ieee80211_sub_if_data *sdata = link->sdata; 2756 struct ieee80211_bss_conf *bss_conf = link->conf; 2757 struct ieee80211_bss *bss = (void *)cbss->priv; 2758 u64 changed = BSS_CHANGED_QOS; 2759 2760 /* not really used in MLO */ 2761 sdata->u.mgd.beacon_timeout = 2762 usecs_to_jiffies(ieee80211_tu_to_usec(beacon_loss_count * 2763 bss_conf->beacon_int)); 2764 2765 changed |= ieee80211_handle_bss_capability(link, 2766 bss_conf->assoc_capability, 2767 bss->has_erp_value, 2768 bss->erp_value); 2769 2770 ieee80211_check_rate_mask(link); 2771 2772 link->u.mgd.bss = cbss; 2773 memcpy(link->u.mgd.bssid, cbss->bssid, ETH_ALEN); 2774 2775 if (sdata->vif.p2p || 2776 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) { 2777 const struct cfg80211_bss_ies *ies; 2778 2779 rcu_read_lock(); 2780 ies = rcu_dereference(cbss->ies); 2781 if (ies) { 2782 int ret; 2783 2784 ret = cfg80211_get_p2p_attr( 2785 ies->data, ies->len, 2786 IEEE80211_P2P_ATTR_ABSENCE_NOTICE, 2787 (u8 *) &bss_conf->p2p_noa_attr, 2788 sizeof(bss_conf->p2p_noa_attr)); 2789 if (ret >= 2) { 2790 link->u.mgd.p2p_noa_index = 2791 bss_conf->p2p_noa_attr.index; 2792 changed |= BSS_CHANGED_P2P_PS; 2793 } 2794 } 2795 rcu_read_unlock(); 2796 } 2797 2798 if (link->u.mgd.have_beacon) { 2799 bss_conf->beacon_rate = bss->beacon_rate; 2800 changed |= BSS_CHANGED_BEACON_INFO; 2801 } else { 2802 bss_conf->beacon_rate = NULL; 2803 } 2804 2805 /* Tell the driver to monitor connection quality (if supported) */ 2806 if (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI && 2807 bss_conf->cqm_rssi_thold) 2808 changed |= BSS_CHANGED_CQM; 2809 2810 return changed; 2811 } 2812 2813 static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata, 2814 struct ieee80211_mgd_assoc_data *assoc_data, 2815 u64 changed[IEEE80211_MLD_MAX_NUM_LINKS]) 2816 { 2817 struct ieee80211_local *local = sdata->local; 2818 struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg; 2819 u64 vif_changed = BSS_CHANGED_ASSOC; 2820 unsigned int link_id; 2821 2822 lockdep_assert_wiphy(local->hw.wiphy); 2823 2824 sdata->u.mgd.associated = true; 2825 2826 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) { 2827 struct cfg80211_bss *cbss = assoc_data->link[link_id].bss; 2828 struct ieee80211_link_data *link; 2829 2830 if (!cbss || 2831 assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS) 2832 continue; 2833 2834 if (ieee80211_vif_is_mld(&sdata->vif) && 2835 !(ieee80211_vif_usable_links(&sdata->vif) & BIT(link_id))) 2836 continue; 2837 2838 link = sdata_dereference(sdata->link[link_id], sdata); 2839 if (WARN_ON(!link)) 2840 return; 2841 2842 changed[link_id] |= ieee80211_link_set_associated(link, cbss); 2843 } 2844 2845 /* just to be sure */ 2846 ieee80211_stop_poll(sdata); 2847 2848 ieee80211_led_assoc(local, 1); 2849 2850 vif_cfg->assoc = 1; 2851 2852 /* Enable ARP filtering */ 2853 if (vif_cfg->arp_addr_cnt) 2854 vif_changed |= BSS_CHANGED_ARP_FILTER; 2855 2856 if (ieee80211_vif_is_mld(&sdata->vif)) { 2857 for (link_id = 0; 2858 link_id < IEEE80211_MLD_MAX_NUM_LINKS; 2859 link_id++) { 2860 struct ieee80211_link_data *link; 2861 struct cfg80211_bss *cbss = assoc_data->link[link_id].bss; 2862 2863 if (!cbss || 2864 !(BIT(link_id) & 2865 ieee80211_vif_usable_links(&sdata->vif)) || 2866 assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS) 2867 continue; 2868 2869 link = sdata_dereference(sdata->link[link_id], sdata); 2870 if (WARN_ON(!link)) 2871 return; 2872 2873 ieee80211_link_info_change_notify(sdata, link, 2874 changed[link_id]); 2875 2876 ieee80211_recalc_smps(sdata, link); 2877 } 2878 2879 ieee80211_vif_cfg_change_notify(sdata, vif_changed); 2880 } else { 2881 ieee80211_bss_info_change_notify(sdata, 2882 vif_changed | changed[0]); 2883 } 2884 2885 ieee80211_recalc_ps(local); 2886 2887 /* leave this here to not change ordering in non-MLO cases */ 2888 if (!ieee80211_vif_is_mld(&sdata->vif)) 2889 ieee80211_recalc_smps(sdata, &sdata->deflink); 2890 ieee80211_recalc_ps_vif(sdata); 2891 2892 netif_carrier_on(sdata->dev); 2893 } 2894 2895 static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata, 2896 u16 stype, u16 reason, bool tx, 2897 u8 *frame_buf) 2898 { 2899 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 2900 struct ieee80211_local *local = sdata->local; 2901 unsigned int link_id; 2902 u64 changed = 0; 2903 struct ieee80211_prep_tx_info info = { 2904 .subtype = stype, 2905 }; 2906 2907 lockdep_assert_wiphy(local->hw.wiphy); 2908 2909 if (WARN_ON_ONCE(tx && !frame_buf)) 2910 return; 2911 2912 if (WARN_ON(!ifmgd->associated)) 2913 return; 2914 2915 ieee80211_stop_poll(sdata); 2916 2917 ifmgd->associated = false; 2918 2919 /* other links will be destroyed */ 2920 sdata->deflink.u.mgd.bss = NULL; 2921 2922 netif_carrier_off(sdata->dev); 2923 2924 /* 2925 * if we want to get out of ps before disassoc (why?) we have 2926 * to do it before sending disassoc, as otherwise the null-packet 2927 * won't be valid. 2928 */ 2929 if (local->hw.conf.flags & IEEE80211_CONF_PS) { 2930 local->hw.conf.flags &= ~IEEE80211_CONF_PS; 2931 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS); 2932 } 2933 local->ps_sdata = NULL; 2934 2935 /* disable per-vif ps */ 2936 ieee80211_recalc_ps_vif(sdata); 2937 2938 /* make sure ongoing transmission finishes */ 2939 synchronize_net(); 2940 2941 /* 2942 * drop any frame before deauth/disassoc, this can be data or 2943 * management frame. Since we are disconnecting, we should not 2944 * insist sending these frames which can take time and delay 2945 * the disconnection and possible the roaming. 2946 */ 2947 if (tx) 2948 ieee80211_flush_queues(local, sdata, true); 2949 2950 /* deauthenticate/disassociate now */ 2951 if (tx || frame_buf) { 2952 /* 2953 * In multi channel scenarios guarantee that the virtual 2954 * interface is granted immediate airtime to transmit the 2955 * deauthentication frame by calling mgd_prepare_tx, if the 2956 * driver requested so. 2957 */ 2958 if (ieee80211_hw_check(&local->hw, DEAUTH_NEED_MGD_TX_PREP)) { 2959 for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); 2960 link_id++) { 2961 struct ieee80211_link_data *link; 2962 2963 link = sdata_dereference(sdata->link[link_id], 2964 sdata); 2965 if (!link) 2966 continue; 2967 if (link->u.mgd.have_beacon) 2968 break; 2969 } 2970 if (link_id == IEEE80211_MLD_MAX_NUM_LINKS) { 2971 info.link_id = ffs(sdata->vif.active_links) - 1; 2972 drv_mgd_prepare_tx(sdata->local, sdata, &info); 2973 } 2974 } 2975 2976 ieee80211_send_deauth_disassoc(sdata, sdata->vif.cfg.ap_addr, 2977 sdata->vif.cfg.ap_addr, stype, 2978 reason, tx, frame_buf); 2979 } 2980 2981 /* flush out frame - make sure the deauth was actually sent */ 2982 if (tx) 2983 ieee80211_flush_queues(local, sdata, false); 2984 2985 drv_mgd_complete_tx(sdata->local, sdata, &info); 2986 2987 /* clear AP addr only after building the needed mgmt frames */ 2988 eth_zero_addr(sdata->deflink.u.mgd.bssid); 2989 eth_zero_addr(sdata->vif.cfg.ap_addr); 2990 2991 sdata->vif.cfg.ssid_len = 0; 2992 2993 /* remove AP and TDLS peers */ 2994 sta_info_flush(sdata); 2995 2996 /* finally reset all BSS / config parameters */ 2997 if (!ieee80211_vif_is_mld(&sdata->vif)) 2998 changed |= ieee80211_reset_erp_info(sdata); 2999 3000 ieee80211_led_assoc(local, 0); 3001 changed |= BSS_CHANGED_ASSOC; 3002 sdata->vif.cfg.assoc = false; 3003 3004 sdata->deflink.u.mgd.p2p_noa_index = -1; 3005 memset(&sdata->vif.bss_conf.p2p_noa_attr, 0, 3006 sizeof(sdata->vif.bss_conf.p2p_noa_attr)); 3007 3008 /* on the next assoc, re-program HT/VHT parameters */ 3009 memset(&ifmgd->ht_capa, 0, sizeof(ifmgd->ht_capa)); 3010 memset(&ifmgd->ht_capa_mask, 0, sizeof(ifmgd->ht_capa_mask)); 3011 memset(&ifmgd->vht_capa, 0, sizeof(ifmgd->vht_capa)); 3012 memset(&ifmgd->vht_capa_mask, 0, sizeof(ifmgd->vht_capa_mask)); 3013 3014 /* 3015 * reset MU-MIMO ownership and group data in default link, 3016 * if used, other links are destroyed 3017 */ 3018 memset(sdata->vif.bss_conf.mu_group.membership, 0, 3019 sizeof(sdata->vif.bss_conf.mu_group.membership)); 3020 memset(sdata->vif.bss_conf.mu_group.position, 0, 3021 sizeof(sdata->vif.bss_conf.mu_group.position)); 3022 if (!ieee80211_vif_is_mld(&sdata->vif)) 3023 changed |= BSS_CHANGED_MU_GROUPS; 3024 sdata->vif.bss_conf.mu_mimo_owner = false; 3025 3026 sdata->deflink.ap_power_level = IEEE80211_UNSET_POWER_LEVEL; 3027 3028 del_timer_sync(&local->dynamic_ps_timer); 3029 wiphy_work_cancel(local->hw.wiphy, &local->dynamic_ps_enable_work); 3030 3031 /* Disable ARP filtering */ 3032 if (sdata->vif.cfg.arp_addr_cnt) 3033 changed |= BSS_CHANGED_ARP_FILTER; 3034 3035 sdata->vif.bss_conf.qos = false; 3036 if (!ieee80211_vif_is_mld(&sdata->vif)) { 3037 changed |= BSS_CHANGED_QOS; 3038 /* The BSSID (not really interesting) and HT changed */ 3039 changed |= BSS_CHANGED_BSSID | BSS_CHANGED_HT; 3040 ieee80211_bss_info_change_notify(sdata, changed); 3041 } else { 3042 ieee80211_vif_cfg_change_notify(sdata, changed); 3043 } 3044 3045 /* disassociated - set to defaults now */ 3046 ieee80211_set_wmm_default(&sdata->deflink, false, false); 3047 3048 del_timer_sync(&sdata->u.mgd.conn_mon_timer); 3049 del_timer_sync(&sdata->u.mgd.bcn_mon_timer); 3050 del_timer_sync(&sdata->u.mgd.timer); 3051 3052 sdata->vif.bss_conf.dtim_period = 0; 3053 sdata->vif.bss_conf.beacon_rate = NULL; 3054 3055 sdata->deflink.u.mgd.have_beacon = false; 3056 sdata->deflink.u.mgd.tracking_signal_avg = false; 3057 sdata->deflink.u.mgd.disable_wmm_tracking = false; 3058 3059 ifmgd->flags = 0; 3060 sdata->deflink.u.mgd.conn_flags = 0; 3061 3062 for (link_id = 0; link_id < ARRAY_SIZE(sdata->link); link_id++) { 3063 struct ieee80211_link_data *link; 3064 3065 link = sdata_dereference(sdata->link[link_id], sdata); 3066 if (!link) 3067 continue; 3068 ieee80211_link_release_channel(link); 3069 } 3070 3071 sdata->vif.bss_conf.csa_active = false; 3072 sdata->deflink.u.mgd.csa_waiting_bcn = false; 3073 sdata->deflink.u.mgd.csa_ignored_same_chan = false; 3074 if (sdata->deflink.csa_block_tx) { 3075 ieee80211_wake_vif_queues(local, sdata, 3076 IEEE80211_QUEUE_STOP_REASON_CSA); 3077 sdata->deflink.csa_block_tx = false; 3078 } 3079 3080 /* existing TX TSPEC sessions no longer exist */ 3081 memset(ifmgd->tx_tspec, 0, sizeof(ifmgd->tx_tspec)); 3082 wiphy_delayed_work_cancel(local->hw.wiphy, &ifmgd->tx_tspec_wk); 3083 3084 sdata->vif.bss_conf.pwr_reduction = 0; 3085 sdata->vif.bss_conf.tx_pwr_env_num = 0; 3086 memset(sdata->vif.bss_conf.tx_pwr_env, 0, 3087 sizeof(sdata->vif.bss_conf.tx_pwr_env)); 3088 3089 memset(&sdata->u.mgd.ttlm_info, 0, 3090 sizeof(sdata->u.mgd.ttlm_info)); 3091 wiphy_delayed_work_cancel(sdata->local->hw.wiphy, &ifmgd->ttlm_work); 3092 ieee80211_vif_set_links(sdata, 0, 0); 3093 } 3094 3095 static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata) 3096 { 3097 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 3098 struct ieee80211_local *local = sdata->local; 3099 3100 lockdep_assert_wiphy(local->hw.wiphy); 3101 3102 if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)) 3103 return; 3104 3105 __ieee80211_stop_poll(sdata); 3106 3107 ieee80211_recalc_ps(local); 3108 3109 if (ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR)) 3110 return; 3111 3112 /* 3113 * We've received a probe response, but are not sure whether 3114 * we have or will be receiving any beacons or data, so let's 3115 * schedule the timers again, just in case. 3116 */ 3117 ieee80211_sta_reset_beacon_monitor(sdata); 3118 3119 mod_timer(&ifmgd->conn_mon_timer, 3120 round_jiffies_up(jiffies + 3121 IEEE80211_CONNECTION_IDLE_TIME)); 3122 } 3123 3124 static void ieee80211_sta_tx_wmm_ac_notify(struct ieee80211_sub_if_data *sdata, 3125 struct ieee80211_hdr *hdr, 3126 u16 tx_time) 3127 { 3128 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 3129 u16 tid; 3130 int ac; 3131 struct ieee80211_sta_tx_tspec *tx_tspec; 3132 unsigned long now = jiffies; 3133 3134 if (!ieee80211_is_data_qos(hdr->frame_control)) 3135 return; 3136 3137 tid = ieee80211_get_tid(hdr); 3138 ac = ieee80211_ac_from_tid(tid); 3139 tx_tspec = &ifmgd->tx_tspec[ac]; 3140 3141 if (likely(!tx_tspec->admitted_time)) 3142 return; 3143 3144 if (time_after(now, tx_tspec->time_slice_start + HZ)) { 3145 tx_tspec->consumed_tx_time = 0; 3146 tx_tspec->time_slice_start = now; 3147 3148 if (tx_tspec->downgraded) { 3149 tx_tspec->action = TX_TSPEC_ACTION_STOP_DOWNGRADE; 3150 wiphy_delayed_work_queue(sdata->local->hw.wiphy, 3151 &ifmgd->tx_tspec_wk, 0); 3152 } 3153 } 3154 3155 if (tx_tspec->downgraded) 3156 return; 3157 3158 tx_tspec->consumed_tx_time += tx_time; 3159 3160 if (tx_tspec->consumed_tx_time >= tx_tspec->admitted_time) { 3161 tx_tspec->downgraded = true; 3162 tx_tspec->action = TX_TSPEC_ACTION_DOWNGRADE; 3163 wiphy_delayed_work_queue(sdata->local->hw.wiphy, 3164 &ifmgd->tx_tspec_wk, 0); 3165 } 3166 } 3167 3168 void ieee80211_sta_tx_notify(struct ieee80211_sub_if_data *sdata, 3169 struct ieee80211_hdr *hdr, bool ack, u16 tx_time) 3170 { 3171 ieee80211_sta_tx_wmm_ac_notify(sdata, hdr, tx_time); 3172 3173 if (!ieee80211_is_any_nullfunc(hdr->frame_control) || 3174 !sdata->u.mgd.probe_send_count) 3175 return; 3176 3177 if (ack) 3178 sdata->u.mgd.probe_send_count = 0; 3179 else 3180 sdata->u.mgd.nullfunc_failed = true; 3181 wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work); 3182 } 3183 3184 static void ieee80211_mlme_send_probe_req(struct ieee80211_sub_if_data *sdata, 3185 const u8 *src, const u8 *dst, 3186 const u8 *ssid, size_t ssid_len, 3187 struct ieee80211_channel *channel) 3188 { 3189 struct sk_buff *skb; 3190 3191 skb = ieee80211_build_probe_req(sdata, src, dst, (u32)-1, channel, 3192 ssid, ssid_len, NULL, 0, 3193 IEEE80211_PROBE_FLAG_DIRECTED); 3194 if (skb) 3195 ieee80211_tx_skb(sdata, skb); 3196 } 3197 3198 static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata) 3199 { 3200 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 3201 u8 *dst = sdata->vif.cfg.ap_addr; 3202 u8 unicast_limit = max(1, max_probe_tries - 3); 3203 struct sta_info *sta; 3204 3205 lockdep_assert_wiphy(sdata->local->hw.wiphy); 3206 3207 if (WARN_ON(ieee80211_vif_is_mld(&sdata->vif))) 3208 return; 3209 3210 /* 3211 * Try sending broadcast probe requests for the last three 3212 * probe requests after the first ones failed since some 3213 * buggy APs only support broadcast probe requests. 3214 */ 3215 if (ifmgd->probe_send_count >= unicast_limit) 3216 dst = NULL; 3217 3218 /* 3219 * When the hardware reports an accurate Tx ACK status, it's 3220 * better to send a nullfunc frame instead of a probe request, 3221 * as it will kick us off the AP quickly if we aren't associated 3222 * anymore. The timeout will be reset if the frame is ACKed by 3223 * the AP. 3224 */ 3225 ifmgd->probe_send_count++; 3226 3227 if (dst) { 3228 sta = sta_info_get(sdata, dst); 3229 if (!WARN_ON(!sta)) 3230 ieee80211_check_fast_rx(sta); 3231 } 3232 3233 if (ieee80211_hw_check(&sdata->local->hw, REPORTS_TX_ACK_STATUS)) { 3234 ifmgd->nullfunc_failed = false; 3235 ieee80211_send_nullfunc(sdata->local, sdata, false); 3236 } else { 3237 ieee80211_mlme_send_probe_req(sdata, sdata->vif.addr, dst, 3238 sdata->vif.cfg.ssid, 3239 sdata->vif.cfg.ssid_len, 3240 sdata->deflink.u.mgd.bss->channel); 3241 } 3242 3243 ifmgd->probe_timeout = jiffies + msecs_to_jiffies(probe_wait_ms); 3244 run_again(sdata, ifmgd->probe_timeout); 3245 } 3246 3247 static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata, 3248 bool beacon) 3249 { 3250 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 3251 bool already = false; 3252 3253 lockdep_assert_wiphy(sdata->local->hw.wiphy); 3254 3255 if (WARN_ON_ONCE(ieee80211_vif_is_mld(&sdata->vif))) 3256 return; 3257 3258 if (!ieee80211_sdata_running(sdata)) 3259 return; 3260 3261 if (!ifmgd->associated) 3262 return; 3263 3264 if (sdata->local->tmp_channel || sdata->local->scanning) 3265 return; 3266 3267 if (sdata->local->suspending) { 3268 /* reschedule after resume */ 3269 ieee80211_reset_ap_probe(sdata); 3270 return; 3271 } 3272 3273 if (beacon) { 3274 mlme_dbg_ratelimited(sdata, 3275 "detected beacon loss from AP (missed %d beacons) - probing\n", 3276 beacon_loss_count); 3277 3278 ieee80211_cqm_beacon_loss_notify(&sdata->vif, GFP_KERNEL); 3279 } 3280 3281 /* 3282 * The driver/our work has already reported this event or the 3283 * connection monitoring has kicked in and we have already sent 3284 * a probe request. Or maybe the AP died and the driver keeps 3285 * reporting until we disassociate... 3286 * 3287 * In either case we have to ignore the current call to this 3288 * function (except for setting the correct probe reason bit) 3289 * because otherwise we would reset the timer every time and 3290 * never check whether we received a probe response! 3291 */ 3292 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) 3293 already = true; 3294 3295 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL; 3296 3297 if (already) 3298 return; 3299 3300 ieee80211_recalc_ps(sdata->local); 3301 3302 ifmgd->probe_send_count = 0; 3303 ieee80211_mgd_probe_ap_send(sdata); 3304 } 3305 3306 struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw, 3307 struct ieee80211_vif *vif) 3308 { 3309 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 3310 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 3311 struct cfg80211_bss *cbss; 3312 struct sk_buff *skb; 3313 const struct element *ssid; 3314 int ssid_len; 3315 3316 lockdep_assert_wiphy(sdata->local->hw.wiphy); 3317 3318 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION || 3319 ieee80211_vif_is_mld(&sdata->vif))) 3320 return NULL; 3321 3322 if (ifmgd->associated) 3323 cbss = sdata->deflink.u.mgd.bss; 3324 else if (ifmgd->auth_data) 3325 cbss = ifmgd->auth_data->bss; 3326 else if (ifmgd->assoc_data && ifmgd->assoc_data->link[0].bss) 3327 cbss = ifmgd->assoc_data->link[0].bss; 3328 else 3329 return NULL; 3330 3331 rcu_read_lock(); 3332 ssid = ieee80211_bss_get_elem(cbss, WLAN_EID_SSID); 3333 if (WARN_ONCE(!ssid || ssid->datalen > IEEE80211_MAX_SSID_LEN, 3334 "invalid SSID element (len=%d)", 3335 ssid ? ssid->datalen : -1)) 3336 ssid_len = 0; 3337 else 3338 ssid_len = ssid->datalen; 3339 3340 skb = ieee80211_build_probe_req(sdata, sdata->vif.addr, cbss->bssid, 3341 (u32) -1, cbss->channel, 3342 ssid->data, ssid_len, 3343 NULL, 0, IEEE80211_PROBE_FLAG_DIRECTED); 3344 rcu_read_unlock(); 3345 3346 return skb; 3347 } 3348 EXPORT_SYMBOL(ieee80211_ap_probereq_get); 3349 3350 static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata, 3351 const u8 *buf, size_t len, bool tx, 3352 u16 reason, bool reconnect) 3353 { 3354 struct ieee80211_event event = { 3355 .type = MLME_EVENT, 3356 .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT, 3357 .u.mlme.reason = reason, 3358 }; 3359 3360 if (tx) 3361 cfg80211_tx_mlme_mgmt(sdata->dev, buf, len, reconnect); 3362 else 3363 cfg80211_rx_mlme_mgmt(sdata->dev, buf, len); 3364 3365 drv_event_callback(sdata->local, sdata, &event); 3366 } 3367 3368 static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata) 3369 { 3370 struct ieee80211_local *local = sdata->local; 3371 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 3372 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN]; 3373 bool tx; 3374 3375 lockdep_assert_wiphy(local->hw.wiphy); 3376 3377 if (!ifmgd->associated) 3378 return; 3379 3380 /* in MLO assume we have a link where we can TX the frame */ 3381 tx = ieee80211_vif_is_mld(&sdata->vif) || 3382 !sdata->deflink.csa_block_tx; 3383 3384 if (!ifmgd->driver_disconnect) { 3385 unsigned int link_id; 3386 3387 /* 3388 * AP is probably out of range (or not reachable for another 3389 * reason) so remove the bss structs for that AP. In the case 3390 * of multi-link, it's not clear that all of them really are 3391 * out of range, but if they weren't the driver likely would 3392 * have switched to just have a single link active? 3393 */ 3394 for (link_id = 0; 3395 link_id < ARRAY_SIZE(sdata->link); 3396 link_id++) { 3397 struct ieee80211_link_data *link; 3398 3399 link = sdata_dereference(sdata->link[link_id], sdata); 3400 if (!link) 3401 continue; 3402 cfg80211_unlink_bss(local->hw.wiphy, link->u.mgd.bss); 3403 link->u.mgd.bss = NULL; 3404 } 3405 } 3406 3407 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, 3408 ifmgd->driver_disconnect ? 3409 WLAN_REASON_DEAUTH_LEAVING : 3410 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, 3411 tx, frame_buf); 3412 /* the other links will be destroyed */ 3413 sdata->vif.bss_conf.csa_active = false; 3414 sdata->deflink.u.mgd.csa_waiting_bcn = false; 3415 if (sdata->deflink.csa_block_tx) { 3416 ieee80211_wake_vif_queues(local, sdata, 3417 IEEE80211_QUEUE_STOP_REASON_CSA); 3418 sdata->deflink.csa_block_tx = false; 3419 } 3420 3421 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), tx, 3422 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, 3423 ifmgd->reconnect); 3424 ifmgd->reconnect = false; 3425 } 3426 3427 static void ieee80211_beacon_connection_loss_work(struct wiphy *wiphy, 3428 struct wiphy_work *work) 3429 { 3430 struct ieee80211_sub_if_data *sdata = 3431 container_of(work, struct ieee80211_sub_if_data, 3432 u.mgd.beacon_connection_loss_work); 3433 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 3434 3435 if (ifmgd->connection_loss) { 3436 sdata_info(sdata, "Connection to AP %pM lost\n", 3437 sdata->vif.cfg.ap_addr); 3438 __ieee80211_disconnect(sdata); 3439 ifmgd->connection_loss = false; 3440 } else if (ifmgd->driver_disconnect) { 3441 sdata_info(sdata, 3442 "Driver requested disconnection from AP %pM\n", 3443 sdata->vif.cfg.ap_addr); 3444 __ieee80211_disconnect(sdata); 3445 ifmgd->driver_disconnect = false; 3446 } else { 3447 if (ifmgd->associated) 3448 sdata->deflink.u.mgd.beacon_loss_count++; 3449 ieee80211_mgd_probe_ap(sdata, true); 3450 } 3451 } 3452 3453 static void ieee80211_csa_connection_drop_work(struct wiphy *wiphy, 3454 struct wiphy_work *work) 3455 { 3456 struct ieee80211_sub_if_data *sdata = 3457 container_of(work, struct ieee80211_sub_if_data, 3458 u.mgd.csa_connection_drop_work); 3459 3460 __ieee80211_disconnect(sdata); 3461 } 3462 3463 void ieee80211_beacon_loss(struct ieee80211_vif *vif) 3464 { 3465 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 3466 struct ieee80211_hw *hw = &sdata->local->hw; 3467 3468 trace_api_beacon_loss(sdata); 3469 3470 sdata->u.mgd.connection_loss = false; 3471 wiphy_work_queue(hw->wiphy, &sdata->u.mgd.beacon_connection_loss_work); 3472 } 3473 EXPORT_SYMBOL(ieee80211_beacon_loss); 3474 3475 void ieee80211_connection_loss(struct ieee80211_vif *vif) 3476 { 3477 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 3478 struct ieee80211_hw *hw = &sdata->local->hw; 3479 3480 trace_api_connection_loss(sdata); 3481 3482 sdata->u.mgd.connection_loss = true; 3483 wiphy_work_queue(hw->wiphy, &sdata->u.mgd.beacon_connection_loss_work); 3484 } 3485 EXPORT_SYMBOL(ieee80211_connection_loss); 3486 3487 void ieee80211_disconnect(struct ieee80211_vif *vif, bool reconnect) 3488 { 3489 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 3490 struct ieee80211_hw *hw = &sdata->local->hw; 3491 3492 trace_api_disconnect(sdata, reconnect); 3493 3494 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION)) 3495 return; 3496 3497 sdata->u.mgd.driver_disconnect = true; 3498 sdata->u.mgd.reconnect = reconnect; 3499 wiphy_work_queue(hw->wiphy, &sdata->u.mgd.beacon_connection_loss_work); 3500 } 3501 EXPORT_SYMBOL(ieee80211_disconnect); 3502 3503 static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata, 3504 bool assoc) 3505 { 3506 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data; 3507 3508 lockdep_assert_wiphy(sdata->local->hw.wiphy); 3509 3510 if (!assoc) { 3511 /* 3512 * we are not authenticated yet, the only timer that could be 3513 * running is the timeout for the authentication response which 3514 * which is not relevant anymore. 3515 */ 3516 del_timer_sync(&sdata->u.mgd.timer); 3517 sta_info_destroy_addr(sdata, auth_data->ap_addr); 3518 3519 /* other links are destroyed */ 3520 sdata->deflink.u.mgd.conn_flags = 0; 3521 eth_zero_addr(sdata->deflink.u.mgd.bssid); 3522 ieee80211_link_info_change_notify(sdata, &sdata->deflink, 3523 BSS_CHANGED_BSSID); 3524 sdata->u.mgd.flags = 0; 3525 3526 ieee80211_link_release_channel(&sdata->deflink); 3527 ieee80211_vif_set_links(sdata, 0, 0); 3528 } 3529 3530 cfg80211_put_bss(sdata->local->hw.wiphy, auth_data->bss); 3531 kfree(auth_data); 3532 sdata->u.mgd.auth_data = NULL; 3533 } 3534 3535 enum assoc_status { 3536 ASSOC_SUCCESS, 3537 ASSOC_REJECTED, 3538 ASSOC_TIMEOUT, 3539 ASSOC_ABANDON, 3540 }; 3541 3542 static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata, 3543 enum assoc_status status) 3544 { 3545 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data; 3546 3547 lockdep_assert_wiphy(sdata->local->hw.wiphy); 3548 3549 if (status != ASSOC_SUCCESS) { 3550 /* 3551 * we are not associated yet, the only timer that could be 3552 * running is the timeout for the association response which 3553 * which is not relevant anymore. 3554 */ 3555 del_timer_sync(&sdata->u.mgd.timer); 3556 sta_info_destroy_addr(sdata, assoc_data->ap_addr); 3557 3558 sdata->deflink.u.mgd.conn_flags = 0; 3559 eth_zero_addr(sdata->deflink.u.mgd.bssid); 3560 ieee80211_link_info_change_notify(sdata, &sdata->deflink, 3561 BSS_CHANGED_BSSID); 3562 sdata->u.mgd.flags = 0; 3563 sdata->vif.bss_conf.mu_mimo_owner = false; 3564 3565 if (status != ASSOC_REJECTED) { 3566 struct cfg80211_assoc_failure data = { 3567 .timeout = status == ASSOC_TIMEOUT, 3568 }; 3569 int i; 3570 3571 BUILD_BUG_ON(ARRAY_SIZE(data.bss) != 3572 ARRAY_SIZE(assoc_data->link)); 3573 3574 for (i = 0; i < ARRAY_SIZE(data.bss); i++) 3575 data.bss[i] = assoc_data->link[i].bss; 3576 3577 if (ieee80211_vif_is_mld(&sdata->vif)) 3578 data.ap_mld_addr = assoc_data->ap_addr; 3579 3580 cfg80211_assoc_failure(sdata->dev, &data); 3581 } 3582 3583 ieee80211_link_release_channel(&sdata->deflink); 3584 ieee80211_vif_set_links(sdata, 0, 0); 3585 } 3586 3587 kfree(assoc_data); 3588 sdata->u.mgd.assoc_data = NULL; 3589 } 3590 3591 static void ieee80211_auth_challenge(struct ieee80211_sub_if_data *sdata, 3592 struct ieee80211_mgmt *mgmt, size_t len) 3593 { 3594 struct ieee80211_local *local = sdata->local; 3595 struct ieee80211_mgd_auth_data *auth_data = sdata->u.mgd.auth_data; 3596 const struct element *challenge; 3597 u8 *pos; 3598 u32 tx_flags = 0; 3599 struct ieee80211_prep_tx_info info = { 3600 .subtype = IEEE80211_STYPE_AUTH, 3601 .link_id = auth_data->link_id, 3602 }; 3603 3604 pos = mgmt->u.auth.variable; 3605 challenge = cfg80211_find_elem(WLAN_EID_CHALLENGE, pos, 3606 len - (pos - (u8 *)mgmt)); 3607 if (!challenge) 3608 return; 3609 auth_data->expected_transaction = 4; 3610 drv_mgd_prepare_tx(sdata->local, sdata, &info); 3611 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) 3612 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS | 3613 IEEE80211_TX_INTFL_MLME_CONN_TX; 3614 ieee80211_send_auth(sdata, 3, auth_data->algorithm, 0, 3615 (void *)challenge, 3616 challenge->datalen + sizeof(*challenge), 3617 auth_data->ap_addr, auth_data->ap_addr, 3618 auth_data->key, auth_data->key_len, 3619 auth_data->key_idx, tx_flags); 3620 } 3621 3622 static bool ieee80211_mark_sta_auth(struct ieee80211_sub_if_data *sdata) 3623 { 3624 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 3625 const u8 *ap_addr = ifmgd->auth_data->ap_addr; 3626 struct sta_info *sta; 3627 3628 lockdep_assert_wiphy(sdata->local->hw.wiphy); 3629 3630 sdata_info(sdata, "authenticated\n"); 3631 ifmgd->auth_data->done = true; 3632 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC; 3633 ifmgd->auth_data->timeout_started = true; 3634 run_again(sdata, ifmgd->auth_data->timeout); 3635 3636 /* move station state to auth */ 3637 sta = sta_info_get(sdata, ap_addr); 3638 if (!sta) { 3639 WARN_ONCE(1, "%s: STA %pM not found", sdata->name, ap_addr); 3640 return false; 3641 } 3642 if (sta_info_move_state(sta, IEEE80211_STA_AUTH)) { 3643 sdata_info(sdata, "failed moving %pM to auth\n", ap_addr); 3644 return false; 3645 } 3646 3647 return true; 3648 } 3649 3650 static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata, 3651 struct ieee80211_mgmt *mgmt, size_t len) 3652 { 3653 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 3654 u16 auth_alg, auth_transaction, status_code; 3655 struct ieee80211_event event = { 3656 .type = MLME_EVENT, 3657 .u.mlme.data = AUTH_EVENT, 3658 }; 3659 struct ieee80211_prep_tx_info info = { 3660 .subtype = IEEE80211_STYPE_AUTH, 3661 }; 3662 3663 lockdep_assert_wiphy(sdata->local->hw.wiphy); 3664 3665 if (len < 24 + 6) 3666 return; 3667 3668 if (!ifmgd->auth_data || ifmgd->auth_data->done) 3669 return; 3670 3671 if (!ether_addr_equal(ifmgd->auth_data->ap_addr, mgmt->bssid)) 3672 return; 3673 3674 auth_alg = le16_to_cpu(mgmt->u.auth.auth_alg); 3675 auth_transaction = le16_to_cpu(mgmt->u.auth.auth_transaction); 3676 status_code = le16_to_cpu(mgmt->u.auth.status_code); 3677 3678 if (auth_alg != ifmgd->auth_data->algorithm || 3679 (auth_alg != WLAN_AUTH_SAE && 3680 auth_transaction != ifmgd->auth_data->expected_transaction) || 3681 (auth_alg == WLAN_AUTH_SAE && 3682 (auth_transaction < ifmgd->auth_data->expected_transaction || 3683 auth_transaction > 2))) { 3684 sdata_info(sdata, "%pM unexpected authentication state: alg %d (expected %d) transact %d (expected %d)\n", 3685 mgmt->sa, auth_alg, ifmgd->auth_data->algorithm, 3686 auth_transaction, 3687 ifmgd->auth_data->expected_transaction); 3688 goto notify_driver; 3689 } 3690 3691 if (status_code != WLAN_STATUS_SUCCESS) { 3692 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len); 3693 3694 if (auth_alg == WLAN_AUTH_SAE && 3695 (status_code == WLAN_STATUS_ANTI_CLOG_REQUIRED || 3696 (auth_transaction == 1 && 3697 (status_code == WLAN_STATUS_SAE_HASH_TO_ELEMENT || 3698 status_code == WLAN_STATUS_SAE_PK)))) { 3699 /* waiting for userspace now */ 3700 ifmgd->auth_data->waiting = true; 3701 ifmgd->auth_data->timeout = 3702 jiffies + IEEE80211_AUTH_WAIT_SAE_RETRY; 3703 ifmgd->auth_data->timeout_started = true; 3704 run_again(sdata, ifmgd->auth_data->timeout); 3705 goto notify_driver; 3706 } 3707 3708 sdata_info(sdata, "%pM denied authentication (status %d)\n", 3709 mgmt->sa, status_code); 3710 ieee80211_destroy_auth_data(sdata, false); 3711 event.u.mlme.status = MLME_DENIED; 3712 event.u.mlme.reason = status_code; 3713 drv_event_callback(sdata->local, sdata, &event); 3714 goto notify_driver; 3715 } 3716 3717 switch (ifmgd->auth_data->algorithm) { 3718 case WLAN_AUTH_OPEN: 3719 case WLAN_AUTH_LEAP: 3720 case WLAN_AUTH_FT: 3721 case WLAN_AUTH_SAE: 3722 case WLAN_AUTH_FILS_SK: 3723 case WLAN_AUTH_FILS_SK_PFS: 3724 case WLAN_AUTH_FILS_PK: 3725 break; 3726 case WLAN_AUTH_SHARED_KEY: 3727 if (ifmgd->auth_data->expected_transaction != 4) { 3728 ieee80211_auth_challenge(sdata, mgmt, len); 3729 /* need another frame */ 3730 return; 3731 } 3732 break; 3733 default: 3734 WARN_ONCE(1, "invalid auth alg %d", 3735 ifmgd->auth_data->algorithm); 3736 goto notify_driver; 3737 } 3738 3739 event.u.mlme.status = MLME_SUCCESS; 3740 info.success = 1; 3741 drv_event_callback(sdata->local, sdata, &event); 3742 if (ifmgd->auth_data->algorithm != WLAN_AUTH_SAE || 3743 (auth_transaction == 2 && 3744 ifmgd->auth_data->expected_transaction == 2)) { 3745 if (!ieee80211_mark_sta_auth(sdata)) 3746 return; /* ignore frame -- wait for timeout */ 3747 } else if (ifmgd->auth_data->algorithm == WLAN_AUTH_SAE && 3748 auth_transaction == 2) { 3749 sdata_info(sdata, "SAE peer confirmed\n"); 3750 ifmgd->auth_data->peer_confirmed = true; 3751 } 3752 3753 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len); 3754 notify_driver: 3755 drv_mgd_complete_tx(sdata->local, sdata, &info); 3756 } 3757 3758 #define case_WLAN(type) \ 3759 case WLAN_REASON_##type: return #type 3760 3761 const char *ieee80211_get_reason_code_string(u16 reason_code) 3762 { 3763 switch (reason_code) { 3764 case_WLAN(UNSPECIFIED); 3765 case_WLAN(PREV_AUTH_NOT_VALID); 3766 case_WLAN(DEAUTH_LEAVING); 3767 case_WLAN(DISASSOC_DUE_TO_INACTIVITY); 3768 case_WLAN(DISASSOC_AP_BUSY); 3769 case_WLAN(CLASS2_FRAME_FROM_NONAUTH_STA); 3770 case_WLAN(CLASS3_FRAME_FROM_NONASSOC_STA); 3771 case_WLAN(DISASSOC_STA_HAS_LEFT); 3772 case_WLAN(STA_REQ_ASSOC_WITHOUT_AUTH); 3773 case_WLAN(DISASSOC_BAD_POWER); 3774 case_WLAN(DISASSOC_BAD_SUPP_CHAN); 3775 case_WLAN(INVALID_IE); 3776 case_WLAN(MIC_FAILURE); 3777 case_WLAN(4WAY_HANDSHAKE_TIMEOUT); 3778 case_WLAN(GROUP_KEY_HANDSHAKE_TIMEOUT); 3779 case_WLAN(IE_DIFFERENT); 3780 case_WLAN(INVALID_GROUP_CIPHER); 3781 case_WLAN(INVALID_PAIRWISE_CIPHER); 3782 case_WLAN(INVALID_AKMP); 3783 case_WLAN(UNSUPP_RSN_VERSION); 3784 case_WLAN(INVALID_RSN_IE_CAP); 3785 case_WLAN(IEEE8021X_FAILED); 3786 case_WLAN(CIPHER_SUITE_REJECTED); 3787 case_WLAN(DISASSOC_UNSPECIFIED_QOS); 3788 case_WLAN(DISASSOC_QAP_NO_BANDWIDTH); 3789 case_WLAN(DISASSOC_LOW_ACK); 3790 case_WLAN(DISASSOC_QAP_EXCEED_TXOP); 3791 case_WLAN(QSTA_LEAVE_QBSS); 3792 case_WLAN(QSTA_NOT_USE); 3793 case_WLAN(QSTA_REQUIRE_SETUP); 3794 case_WLAN(QSTA_TIMEOUT); 3795 case_WLAN(QSTA_CIPHER_NOT_SUPP); 3796 case_WLAN(MESH_PEER_CANCELED); 3797 case_WLAN(MESH_MAX_PEERS); 3798 case_WLAN(MESH_CONFIG); 3799 case_WLAN(MESH_CLOSE); 3800 case_WLAN(MESH_MAX_RETRIES); 3801 case_WLAN(MESH_CONFIRM_TIMEOUT); 3802 case_WLAN(MESH_INVALID_GTK); 3803 case_WLAN(MESH_INCONSISTENT_PARAM); 3804 case_WLAN(MESH_INVALID_SECURITY); 3805 case_WLAN(MESH_PATH_ERROR); 3806 case_WLAN(MESH_PATH_NOFORWARD); 3807 case_WLAN(MESH_PATH_DEST_UNREACHABLE); 3808 case_WLAN(MAC_EXISTS_IN_MBSS); 3809 case_WLAN(MESH_CHAN_REGULATORY); 3810 case_WLAN(MESH_CHAN); 3811 default: return "<unknown>"; 3812 } 3813 } 3814 3815 static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata, 3816 struct ieee80211_mgmt *mgmt, size_t len) 3817 { 3818 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 3819 u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code); 3820 3821 lockdep_assert_wiphy(sdata->local->hw.wiphy); 3822 3823 if (len < 24 + 2) 3824 return; 3825 3826 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) { 3827 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code); 3828 return; 3829 } 3830 3831 if (ifmgd->associated && 3832 ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr)) { 3833 sdata_info(sdata, "deauthenticated from %pM (Reason: %u=%s)\n", 3834 sdata->vif.cfg.ap_addr, reason_code, 3835 ieee80211_get_reason_code_string(reason_code)); 3836 3837 ieee80211_set_disassoc(sdata, 0, 0, false, NULL); 3838 3839 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, 3840 reason_code, false); 3841 return; 3842 } 3843 3844 if (ifmgd->assoc_data && 3845 ether_addr_equal(mgmt->bssid, ifmgd->assoc_data->ap_addr)) { 3846 sdata_info(sdata, 3847 "deauthenticated from %pM while associating (Reason: %u=%s)\n", 3848 ifmgd->assoc_data->ap_addr, reason_code, 3849 ieee80211_get_reason_code_string(reason_code)); 3850 3851 ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON); 3852 3853 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len); 3854 return; 3855 } 3856 } 3857 3858 3859 static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata, 3860 struct ieee80211_mgmt *mgmt, size_t len) 3861 { 3862 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 3863 u16 reason_code; 3864 3865 lockdep_assert_wiphy(sdata->local->hw.wiphy); 3866 3867 if (len < 24 + 2) 3868 return; 3869 3870 if (!ifmgd->associated || 3871 !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr)) 3872 return; 3873 3874 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code); 3875 3876 if (!ether_addr_equal(mgmt->bssid, mgmt->sa)) { 3877 ieee80211_tdls_handle_disconnect(sdata, mgmt->sa, reason_code); 3878 return; 3879 } 3880 3881 sdata_info(sdata, "disassociated from %pM (Reason: %u=%s)\n", 3882 sdata->vif.cfg.ap_addr, reason_code, 3883 ieee80211_get_reason_code_string(reason_code)); 3884 3885 ieee80211_set_disassoc(sdata, 0, 0, false, NULL); 3886 3887 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code, 3888 false); 3889 } 3890 3891 static void ieee80211_get_rates(struct ieee80211_supported_band *sband, 3892 u8 *supp_rates, unsigned int supp_rates_len, 3893 u32 *rates, u32 *basic_rates, 3894 bool *have_higher_than_11mbit, 3895 int *min_rate, int *min_rate_index) 3896 { 3897 int i, j; 3898 3899 for (i = 0; i < supp_rates_len; i++) { 3900 int rate = supp_rates[i] & 0x7f; 3901 bool is_basic = !!(supp_rates[i] & 0x80); 3902 3903 if ((rate * 5) > 110) 3904 *have_higher_than_11mbit = true; 3905 3906 /* 3907 * Skip HT, VHT, HE, EHT and SAE H2E only BSS membership 3908 * selectors since they're not rates. 3909 * 3910 * Note: Even though the membership selector and the basic 3911 * rate flag share the same bit, they are not exactly 3912 * the same. 3913 */ 3914 if (supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY) || 3915 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY) || 3916 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_HE_PHY) || 3917 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_EHT_PHY) || 3918 supp_rates[i] == (0x80 | BSS_MEMBERSHIP_SELECTOR_SAE_H2E)) 3919 continue; 3920 3921 for (j = 0; j < sband->n_bitrates; j++) { 3922 struct ieee80211_rate *br; 3923 int brate; 3924 3925 br = &sband->bitrates[j]; 3926 3927 brate = DIV_ROUND_UP(br->bitrate, 5); 3928 if (brate == rate) { 3929 *rates |= BIT(j); 3930 if (is_basic) 3931 *basic_rates |= BIT(j); 3932 if ((rate * 5) < *min_rate) { 3933 *min_rate = rate * 5; 3934 *min_rate_index = j; 3935 } 3936 break; 3937 } 3938 } 3939 } 3940 } 3941 3942 static bool ieee80211_twt_req_supported(struct ieee80211_sub_if_data *sdata, 3943 struct ieee80211_supported_band *sband, 3944 const struct link_sta_info *link_sta, 3945 const struct ieee802_11_elems *elems) 3946 { 3947 const struct ieee80211_sta_he_cap *own_he_cap = 3948 ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif); 3949 3950 if (elems->ext_capab_len < 10) 3951 return false; 3952 3953 if (!(elems->ext_capab[9] & WLAN_EXT_CAPA10_TWT_RESPONDER_SUPPORT)) 3954 return false; 3955 3956 return link_sta->pub->he_cap.he_cap_elem.mac_cap_info[0] & 3957 IEEE80211_HE_MAC_CAP0_TWT_RES && 3958 own_he_cap && 3959 (own_he_cap->he_cap_elem.mac_cap_info[0] & 3960 IEEE80211_HE_MAC_CAP0_TWT_REQ); 3961 } 3962 3963 static u64 ieee80211_recalc_twt_req(struct ieee80211_sub_if_data *sdata, 3964 struct ieee80211_supported_band *sband, 3965 struct ieee80211_link_data *link, 3966 struct link_sta_info *link_sta, 3967 struct ieee802_11_elems *elems) 3968 { 3969 bool twt = ieee80211_twt_req_supported(sdata, sband, link_sta, elems); 3970 3971 if (link->conf->twt_requester != twt) { 3972 link->conf->twt_requester = twt; 3973 return BSS_CHANGED_TWT; 3974 } 3975 return 0; 3976 } 3977 3978 static bool ieee80211_twt_bcast_support(struct ieee80211_sub_if_data *sdata, 3979 struct ieee80211_bss_conf *bss_conf, 3980 struct ieee80211_supported_band *sband, 3981 struct link_sta_info *link_sta) 3982 { 3983 const struct ieee80211_sta_he_cap *own_he_cap = 3984 ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif); 3985 3986 return bss_conf->he_support && 3987 (link_sta->pub->he_cap.he_cap_elem.mac_cap_info[2] & 3988 IEEE80211_HE_MAC_CAP2_BCAST_TWT) && 3989 own_he_cap && 3990 (own_he_cap->he_cap_elem.mac_cap_info[2] & 3991 IEEE80211_HE_MAC_CAP2_BCAST_TWT); 3992 } 3993 3994 static bool ieee80211_assoc_config_link(struct ieee80211_link_data *link, 3995 struct link_sta_info *link_sta, 3996 struct cfg80211_bss *cbss, 3997 struct ieee80211_mgmt *mgmt, 3998 const u8 *elem_start, 3999 unsigned int elem_len, 4000 u64 *changed) 4001 { 4002 struct ieee80211_sub_if_data *sdata = link->sdata; 4003 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data; 4004 struct ieee80211_bss_conf *bss_conf = link->conf; 4005 struct ieee80211_local *local = sdata->local; 4006 unsigned int link_id = link->link_id; 4007 struct ieee80211_elems_parse_params parse_params = { 4008 .start = elem_start, 4009 .len = elem_len, 4010 .link_id = link_id == assoc_data->assoc_link_id ? -1 : link_id, 4011 .from_ap = true, 4012 }; 4013 bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ; 4014 bool is_s1g = cbss->channel->band == NL80211_BAND_S1GHZ; 4015 const struct cfg80211_bss_ies *bss_ies = NULL; 4016 struct ieee80211_supported_band *sband; 4017 struct ieee802_11_elems *elems; 4018 const __le16 prof_bss_param_ch_present = 4019 cpu_to_le16(IEEE80211_MLE_STA_CONTROL_BSS_PARAM_CHANGE_CNT_PRESENT); 4020 u16 capab_info; 4021 bool ret; 4022 4023 elems = ieee802_11_parse_elems_full(&parse_params); 4024 if (!elems) 4025 return false; 4026 4027 if (link_id == assoc_data->assoc_link_id) { 4028 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info); 4029 4030 /* 4031 * we should not get to this flow unless the association was 4032 * successful, so set the status directly to success 4033 */ 4034 assoc_data->link[link_id].status = WLAN_STATUS_SUCCESS; 4035 if (elems->ml_basic) { 4036 if (!(elems->ml_basic->control & 4037 cpu_to_le16(IEEE80211_MLC_BASIC_PRES_BSS_PARAM_CH_CNT))) { 4038 ret = false; 4039 goto out; 4040 } 4041 link->u.mgd.bss_param_ch_cnt = 4042 ieee80211_mle_get_bss_param_ch_cnt(elems->ml_basic); 4043 } 4044 } else if (!elems->prof || 4045 !(elems->prof->control & prof_bss_param_ch_present)) { 4046 ret = false; 4047 goto out; 4048 } else { 4049 const u8 *ptr = elems->prof->variable + 4050 elems->prof->sta_info_len - 1; 4051 4052 /* 4053 * During parsing, we validated that these fields exist, 4054 * otherwise elems->prof would have been set to NULL. 4055 */ 4056 capab_info = get_unaligned_le16(ptr); 4057 assoc_data->link[link_id].status = get_unaligned_le16(ptr + 2); 4058 link->u.mgd.bss_param_ch_cnt = 4059 ieee80211_mle_basic_sta_prof_bss_param_ch_cnt(elems->prof); 4060 4061 if (assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS) { 4062 link_info(link, "association response status code=%u\n", 4063 assoc_data->link[link_id].status); 4064 ret = true; 4065 goto out; 4066 } 4067 } 4068 4069 if (!is_s1g && !elems->supp_rates) { 4070 sdata_info(sdata, "no SuppRates element in AssocResp\n"); 4071 ret = false; 4072 goto out; 4073 } 4074 4075 link->u.mgd.tdls_chan_switch_prohibited = 4076 elems->ext_capab && elems->ext_capab_len >= 5 && 4077 (elems->ext_capab[4] & WLAN_EXT_CAPA5_TDLS_CH_SW_PROHIBITED); 4078 4079 /* 4080 * Some APs are erroneously not including some information in their 4081 * (re)association response frames. Try to recover by using the data 4082 * from the beacon or probe response. This seems to afflict mobile 4083 * 2G/3G/4G wifi routers, reported models include the "Onda PN51T", 4084 * "Vodafone PocketWiFi 2", "ZTE MF60" and a similar T-Mobile device. 4085 */ 4086 if (!is_6ghz && 4087 ((assoc_data->wmm && !elems->wmm_param) || 4088 (!(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HT) && 4089 (!elems->ht_cap_elem || !elems->ht_operation)) || 4090 (!(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_VHT) && 4091 (!elems->vht_cap_elem || !elems->vht_operation)))) { 4092 const struct cfg80211_bss_ies *ies; 4093 struct ieee802_11_elems *bss_elems; 4094 4095 rcu_read_lock(); 4096 ies = rcu_dereference(cbss->ies); 4097 if (ies) 4098 bss_ies = kmemdup(ies, sizeof(*ies) + ies->len, 4099 GFP_ATOMIC); 4100 rcu_read_unlock(); 4101 if (!bss_ies) { 4102 ret = false; 4103 goto out; 4104 } 4105 4106 parse_params.start = bss_ies->data; 4107 parse_params.len = bss_ies->len; 4108 parse_params.bss = cbss; 4109 bss_elems = ieee802_11_parse_elems_full(&parse_params); 4110 if (!bss_elems) { 4111 ret = false; 4112 goto out; 4113 } 4114 4115 if (assoc_data->wmm && 4116 !elems->wmm_param && bss_elems->wmm_param) { 4117 elems->wmm_param = bss_elems->wmm_param; 4118 sdata_info(sdata, 4119 "AP bug: WMM param missing from AssocResp\n"); 4120 } 4121 4122 /* 4123 * Also check if we requested HT/VHT, otherwise the AP doesn't 4124 * have to include the IEs in the (re)association response. 4125 */ 4126 if (!elems->ht_cap_elem && bss_elems->ht_cap_elem && 4127 !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HT)) { 4128 elems->ht_cap_elem = bss_elems->ht_cap_elem; 4129 sdata_info(sdata, 4130 "AP bug: HT capability missing from AssocResp\n"); 4131 } 4132 if (!elems->ht_operation && bss_elems->ht_operation && 4133 !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HT)) { 4134 elems->ht_operation = bss_elems->ht_operation; 4135 sdata_info(sdata, 4136 "AP bug: HT operation missing from AssocResp\n"); 4137 } 4138 if (!elems->vht_cap_elem && bss_elems->vht_cap_elem && 4139 !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_VHT)) { 4140 elems->vht_cap_elem = bss_elems->vht_cap_elem; 4141 sdata_info(sdata, 4142 "AP bug: VHT capa missing from AssocResp\n"); 4143 } 4144 if (!elems->vht_operation && bss_elems->vht_operation && 4145 !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_VHT)) { 4146 elems->vht_operation = bss_elems->vht_operation; 4147 sdata_info(sdata, 4148 "AP bug: VHT operation missing from AssocResp\n"); 4149 } 4150 4151 kfree(bss_elems); 4152 } 4153 4154 /* 4155 * We previously checked these in the beacon/probe response, so 4156 * they should be present here. This is just a safety net. 4157 */ 4158 if (!is_6ghz && !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HT) && 4159 (!elems->wmm_param || !elems->ht_cap_elem || !elems->ht_operation)) { 4160 sdata_info(sdata, 4161 "HT AP is missing WMM params or HT capability/operation\n"); 4162 ret = false; 4163 goto out; 4164 } 4165 4166 if (!is_6ghz && !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_VHT) && 4167 (!elems->vht_cap_elem || !elems->vht_operation)) { 4168 sdata_info(sdata, 4169 "VHT AP is missing VHT capability/operation\n"); 4170 ret = false; 4171 goto out; 4172 } 4173 4174 if (is_6ghz && !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HE) && 4175 !elems->he_6ghz_capa) { 4176 sdata_info(sdata, 4177 "HE 6 GHz AP is missing HE 6 GHz band capability\n"); 4178 ret = false; 4179 goto out; 4180 } 4181 4182 if (WARN_ON(!link->conf->chandef.chan)) { 4183 ret = false; 4184 goto out; 4185 } 4186 sband = local->hw.wiphy->bands[link->conf->chandef.chan->band]; 4187 4188 if (!(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HE) && 4189 (!elems->he_cap || !elems->he_operation)) { 4190 sdata_info(sdata, 4191 "HE AP is missing HE capability/operation\n"); 4192 ret = false; 4193 goto out; 4194 } 4195 4196 /* Set up internal HT/VHT capabilities */ 4197 if (elems->ht_cap_elem && !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HT)) 4198 ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband, 4199 elems->ht_cap_elem, 4200 link_sta); 4201 4202 if (elems->vht_cap_elem && 4203 !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_VHT)) { 4204 const struct ieee80211_vht_cap *bss_vht_cap = NULL; 4205 const struct cfg80211_bss_ies *ies; 4206 4207 /* 4208 * Cisco AP module 9115 with FW 17.3 has a bug and sends a 4209 * too large maximum MPDU length in the association response 4210 * (indicating 12k) that it cannot actually process ... 4211 * Work around that. 4212 */ 4213 rcu_read_lock(); 4214 ies = rcu_dereference(cbss->ies); 4215 if (ies) { 4216 const struct element *elem; 4217 4218 elem = cfg80211_find_elem(WLAN_EID_VHT_CAPABILITY, 4219 ies->data, ies->len); 4220 if (elem && elem->datalen >= sizeof(*bss_vht_cap)) 4221 bss_vht_cap = (const void *)elem->data; 4222 } 4223 4224 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband, 4225 elems->vht_cap_elem, 4226 bss_vht_cap, link_sta); 4227 rcu_read_unlock(); 4228 } 4229 4230 if (elems->he_operation && !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HE) && 4231 elems->he_cap) { 4232 ieee80211_he_cap_ie_to_sta_he_cap(sdata, sband, 4233 elems->he_cap, 4234 elems->he_cap_len, 4235 elems->he_6ghz_capa, 4236 link_sta); 4237 4238 bss_conf->he_support = link_sta->pub->he_cap.has_he; 4239 if (elems->rsnx && elems->rsnx_len && 4240 (elems->rsnx[0] & WLAN_RSNX_CAPA_PROTECTED_TWT) && 4241 wiphy_ext_feature_isset(local->hw.wiphy, 4242 NL80211_EXT_FEATURE_PROTECTED_TWT)) 4243 bss_conf->twt_protected = true; 4244 else 4245 bss_conf->twt_protected = false; 4246 4247 *changed |= ieee80211_recalc_twt_req(sdata, sband, link, 4248 link_sta, elems); 4249 4250 if (elems->eht_operation && elems->eht_cap && 4251 !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_EHT)) { 4252 ieee80211_eht_cap_ie_to_sta_eht_cap(sdata, sband, 4253 elems->he_cap, 4254 elems->he_cap_len, 4255 elems->eht_cap, 4256 elems->eht_cap_len, 4257 link_sta); 4258 4259 bss_conf->eht_support = link_sta->pub->eht_cap.has_eht; 4260 *changed |= BSS_CHANGED_EHT_PUNCTURING; 4261 } else { 4262 bss_conf->eht_support = false; 4263 } 4264 } else { 4265 bss_conf->he_support = false; 4266 bss_conf->twt_requester = false; 4267 bss_conf->twt_protected = false; 4268 bss_conf->eht_support = false; 4269 } 4270 4271 bss_conf->twt_broadcast = 4272 ieee80211_twt_bcast_support(sdata, bss_conf, sband, link_sta); 4273 4274 if (bss_conf->he_support) { 4275 bss_conf->he_bss_color.color = 4276 le32_get_bits(elems->he_operation->he_oper_params, 4277 IEEE80211_HE_OPERATION_BSS_COLOR_MASK); 4278 bss_conf->he_bss_color.partial = 4279 le32_get_bits(elems->he_operation->he_oper_params, 4280 IEEE80211_HE_OPERATION_PARTIAL_BSS_COLOR); 4281 bss_conf->he_bss_color.enabled = 4282 !le32_get_bits(elems->he_operation->he_oper_params, 4283 IEEE80211_HE_OPERATION_BSS_COLOR_DISABLED); 4284 4285 if (bss_conf->he_bss_color.enabled) 4286 *changed |= BSS_CHANGED_HE_BSS_COLOR; 4287 4288 bss_conf->htc_trig_based_pkt_ext = 4289 le32_get_bits(elems->he_operation->he_oper_params, 4290 IEEE80211_HE_OPERATION_DFLT_PE_DURATION_MASK); 4291 bss_conf->frame_time_rts_th = 4292 le32_get_bits(elems->he_operation->he_oper_params, 4293 IEEE80211_HE_OPERATION_RTS_THRESHOLD_MASK); 4294 4295 bss_conf->uora_exists = !!elems->uora_element; 4296 if (elems->uora_element) 4297 bss_conf->uora_ocw_range = elems->uora_element[0]; 4298 4299 ieee80211_he_op_ie_to_bss_conf(&sdata->vif, elems->he_operation); 4300 ieee80211_he_spr_ie_to_bss_conf(&sdata->vif, elems->he_spr); 4301 /* TODO: OPEN: what happens if BSS color disable is set? */ 4302 } 4303 4304 if (cbss->transmitted_bss) { 4305 bss_conf->nontransmitted = true; 4306 ether_addr_copy(bss_conf->transmitter_bssid, 4307 cbss->transmitted_bss->bssid); 4308 bss_conf->bssid_indicator = cbss->max_bssid_indicator; 4309 bss_conf->bssid_index = cbss->bssid_index; 4310 } 4311 4312 /* 4313 * Some APs, e.g. Netgear WNDR3700, report invalid HT operation data 4314 * in their association response, so ignore that data for our own 4315 * configuration. If it changed since the last beacon, we'll get the 4316 * next beacon and update then. 4317 */ 4318 4319 /* 4320 * If an operating mode notification IE is present, override the 4321 * NSS calculation (that would be done in rate_control_rate_init()) 4322 * and use the # of streams from that element. 4323 */ 4324 if (elems->opmode_notif && 4325 !(*elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_TYPE_BF)) { 4326 u8 nss; 4327 4328 nss = *elems->opmode_notif & IEEE80211_OPMODE_NOTIF_RX_NSS_MASK; 4329 nss >>= IEEE80211_OPMODE_NOTIF_RX_NSS_SHIFT; 4330 nss += 1; 4331 link_sta->pub->rx_nss = nss; 4332 } 4333 4334 /* 4335 * Always handle WMM once after association regardless 4336 * of the first value the AP uses. Setting -1 here has 4337 * that effect because the AP values is an unsigned 4338 * 4-bit value. 4339 */ 4340 link->u.mgd.wmm_last_param_set = -1; 4341 link->u.mgd.mu_edca_last_param_set = -1; 4342 4343 if (link->u.mgd.disable_wmm_tracking) { 4344 ieee80211_set_wmm_default(link, false, false); 4345 } else if (!ieee80211_sta_wmm_params(local, link, elems->wmm_param, 4346 elems->wmm_param_len, 4347 elems->mu_edca_param_set)) { 4348 /* still enable QoS since we might have HT/VHT */ 4349 ieee80211_set_wmm_default(link, false, true); 4350 /* disable WMM tracking in this case to disable 4351 * tracking WMM parameter changes in the beacon if 4352 * the parameters weren't actually valid. Doing so 4353 * avoids changing parameters very strangely when 4354 * the AP is going back and forth between valid and 4355 * invalid parameters. 4356 */ 4357 link->u.mgd.disable_wmm_tracking = true; 4358 } 4359 4360 if (elems->max_idle_period_ie) { 4361 bss_conf->max_idle_period = 4362 le16_to_cpu(elems->max_idle_period_ie->max_idle_period); 4363 bss_conf->protected_keep_alive = 4364 !!(elems->max_idle_period_ie->idle_options & 4365 WLAN_IDLE_OPTIONS_PROTECTED_KEEP_ALIVE); 4366 *changed |= BSS_CHANGED_KEEP_ALIVE; 4367 } else { 4368 bss_conf->max_idle_period = 0; 4369 bss_conf->protected_keep_alive = false; 4370 } 4371 4372 /* set assoc capability (AID was already set earlier), 4373 * ieee80211_set_associated() will tell the driver */ 4374 bss_conf->assoc_capability = capab_info; 4375 4376 ret = true; 4377 out: 4378 kfree(elems); 4379 kfree(bss_ies); 4380 return ret; 4381 } 4382 4383 static int ieee80211_mgd_setup_link_sta(struct ieee80211_link_data *link, 4384 struct sta_info *sta, 4385 struct link_sta_info *link_sta, 4386 struct cfg80211_bss *cbss) 4387 { 4388 struct ieee80211_sub_if_data *sdata = link->sdata; 4389 struct ieee80211_local *local = sdata->local; 4390 struct ieee80211_bss *bss = (void *)cbss->priv; 4391 u32 rates = 0, basic_rates = 0; 4392 bool have_higher_than_11mbit = false; 4393 int min_rate = INT_MAX, min_rate_index = -1; 4394 struct ieee80211_supported_band *sband; 4395 4396 memcpy(link_sta->addr, cbss->bssid, ETH_ALEN); 4397 memcpy(link_sta->pub->addr, cbss->bssid, ETH_ALEN); 4398 4399 /* TODO: S1G Basic Rate Set is expressed elsewhere */ 4400 if (cbss->channel->band == NL80211_BAND_S1GHZ) { 4401 ieee80211_s1g_sta_rate_init(sta); 4402 return 0; 4403 } 4404 4405 sband = local->hw.wiphy->bands[cbss->channel->band]; 4406 4407 ieee80211_get_rates(sband, bss->supp_rates, bss->supp_rates_len, 4408 &rates, &basic_rates, &have_higher_than_11mbit, 4409 &min_rate, &min_rate_index); 4410 4411 /* 4412 * This used to be a workaround for basic rates missing 4413 * in the association response frame. Now that we no 4414 * longer use the basic rates from there, it probably 4415 * doesn't happen any more, but keep the workaround so 4416 * in case some *other* APs are buggy in different ways 4417 * we can connect -- with a warning. 4418 * Allow this workaround only in case the AP provided at least 4419 * one rate. 4420 */ 4421 if (min_rate_index < 0) { 4422 link_info(link, "No legacy rates in association response\n"); 4423 return -EINVAL; 4424 } else if (!basic_rates) { 4425 link_info(link, "No basic rates, using min rate instead\n"); 4426 basic_rates = BIT(min_rate_index); 4427 } 4428 4429 if (rates) 4430 link_sta->pub->supp_rates[cbss->channel->band] = rates; 4431 else 4432 link_info(link, "No rates found, keeping mandatory only\n"); 4433 4434 link->conf->basic_rates = basic_rates; 4435 4436 /* cf. IEEE 802.11 9.2.12 */ 4437 link->operating_11g_mode = sband->band == NL80211_BAND_2GHZ && 4438 have_higher_than_11mbit; 4439 4440 return 0; 4441 } 4442 4443 static u8 ieee80211_max_rx_chains(struct ieee80211_link_data *link, 4444 struct cfg80211_bss *cbss) 4445 { 4446 struct ieee80211_he_mcs_nss_supp *he_mcs_nss_supp; 4447 const struct element *ht_cap_elem, *vht_cap_elem; 4448 const struct cfg80211_bss_ies *ies; 4449 const struct ieee80211_ht_cap *ht_cap; 4450 const struct ieee80211_vht_cap *vht_cap; 4451 const struct ieee80211_he_cap_elem *he_cap; 4452 const struct element *he_cap_elem; 4453 u16 mcs_80_map, mcs_160_map; 4454 int i, mcs_nss_size; 4455 bool support_160; 4456 u8 chains = 1; 4457 4458 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HT) 4459 return chains; 4460 4461 ht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_HT_CAPABILITY); 4462 if (ht_cap_elem && ht_cap_elem->datalen >= sizeof(*ht_cap)) { 4463 ht_cap = (void *)ht_cap_elem->data; 4464 chains = ieee80211_mcs_to_chains(&ht_cap->mcs); 4465 /* 4466 * TODO: use "Tx Maximum Number Spatial Streams Supported" and 4467 * "Tx Unequal Modulation Supported" fields. 4468 */ 4469 } 4470 4471 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_VHT) 4472 return chains; 4473 4474 vht_cap_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_VHT_CAPABILITY); 4475 if (vht_cap_elem && vht_cap_elem->datalen >= sizeof(*vht_cap)) { 4476 u8 nss; 4477 u16 tx_mcs_map; 4478 4479 vht_cap = (void *)vht_cap_elem->data; 4480 tx_mcs_map = le16_to_cpu(vht_cap->supp_mcs.tx_mcs_map); 4481 for (nss = 8; nss > 0; nss--) { 4482 if (((tx_mcs_map >> (2 * (nss - 1))) & 3) != 4483 IEEE80211_VHT_MCS_NOT_SUPPORTED) 4484 break; 4485 } 4486 /* TODO: use "Tx Highest Supported Long GI Data Rate" field? */ 4487 chains = max(chains, nss); 4488 } 4489 4490 if (link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_HE) 4491 return chains; 4492 4493 ies = rcu_dereference(cbss->ies); 4494 he_cap_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, 4495 ies->data, ies->len); 4496 4497 if (!he_cap_elem || he_cap_elem->datalen < sizeof(*he_cap)) 4498 return chains; 4499 4500 /* skip one byte ext_tag_id */ 4501 he_cap = (void *)(he_cap_elem->data + 1); 4502 mcs_nss_size = ieee80211_he_mcs_nss_size(he_cap); 4503 4504 /* invalid HE IE */ 4505 if (he_cap_elem->datalen < 1 + mcs_nss_size + sizeof(*he_cap)) 4506 return chains; 4507 4508 /* mcs_nss is right after he_cap info */ 4509 he_mcs_nss_supp = (void *)(he_cap + 1); 4510 4511 mcs_80_map = le16_to_cpu(he_mcs_nss_supp->tx_mcs_80); 4512 4513 for (i = 7; i >= 0; i--) { 4514 u8 mcs_80 = mcs_80_map >> (2 * i) & 3; 4515 4516 if (mcs_80 != IEEE80211_VHT_MCS_NOT_SUPPORTED) { 4517 chains = max_t(u8, chains, i + 1); 4518 break; 4519 } 4520 } 4521 4522 support_160 = he_cap->phy_cap_info[0] & 4523 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G; 4524 4525 if (!support_160) 4526 return chains; 4527 4528 mcs_160_map = le16_to_cpu(he_mcs_nss_supp->tx_mcs_160); 4529 for (i = 7; i >= 0; i--) { 4530 u8 mcs_160 = mcs_160_map >> (2 * i) & 3; 4531 4532 if (mcs_160 != IEEE80211_VHT_MCS_NOT_SUPPORTED) { 4533 chains = max_t(u8, chains, i + 1); 4534 break; 4535 } 4536 } 4537 4538 return chains; 4539 } 4540 4541 static bool 4542 ieee80211_verify_peer_he_mcs_support(struct ieee80211_sub_if_data *sdata, 4543 const struct cfg80211_bss_ies *ies, 4544 const struct ieee80211_he_operation *he_op) 4545 { 4546 const struct element *he_cap_elem; 4547 const struct ieee80211_he_cap_elem *he_cap; 4548 struct ieee80211_he_mcs_nss_supp *he_mcs_nss_supp; 4549 u16 mcs_80_map_tx, mcs_80_map_rx; 4550 u16 ap_min_req_set; 4551 int mcs_nss_size; 4552 int nss; 4553 4554 he_cap_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_HE_CAPABILITY, 4555 ies->data, ies->len); 4556 4557 if (!he_cap_elem) 4558 return false; 4559 4560 /* invalid HE IE */ 4561 if (he_cap_elem->datalen < 1 + sizeof(*he_cap)) { 4562 sdata_info(sdata, 4563 "Invalid HE elem, Disable HE\n"); 4564 return false; 4565 } 4566 4567 /* skip one byte ext_tag_id */ 4568 he_cap = (void *)(he_cap_elem->data + 1); 4569 mcs_nss_size = ieee80211_he_mcs_nss_size(he_cap); 4570 4571 /* invalid HE IE */ 4572 if (he_cap_elem->datalen < 1 + sizeof(*he_cap) + mcs_nss_size) { 4573 sdata_info(sdata, 4574 "Invalid HE elem with nss size, Disable HE\n"); 4575 return false; 4576 } 4577 4578 /* mcs_nss is right after he_cap info */ 4579 he_mcs_nss_supp = (void *)(he_cap + 1); 4580 4581 mcs_80_map_tx = le16_to_cpu(he_mcs_nss_supp->tx_mcs_80); 4582 mcs_80_map_rx = le16_to_cpu(he_mcs_nss_supp->rx_mcs_80); 4583 4584 /* P802.11-REVme/D0.3 4585 * 27.1.1 Introduction to the HE PHY 4586 * ... 4587 * An HE STA shall support the following features: 4588 * ... 4589 * Single spatial stream HE-MCSs 0 to 7 (transmit and receive) in all 4590 * supported channel widths for HE SU PPDUs 4591 */ 4592 if ((mcs_80_map_tx & 0x3) == IEEE80211_HE_MCS_NOT_SUPPORTED || 4593 (mcs_80_map_rx & 0x3) == IEEE80211_HE_MCS_NOT_SUPPORTED) { 4594 sdata_info(sdata, 4595 "Missing mandatory rates for 1 Nss, rx 0x%x, tx 0x%x, disable HE\n", 4596 mcs_80_map_tx, mcs_80_map_rx); 4597 return false; 4598 } 4599 4600 if (!he_op) 4601 return true; 4602 4603 ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set); 4604 4605 /* 4606 * Apparently iPhone 13 (at least iOS version 15.3.1) sets this to all 4607 * zeroes, which is nonsense, and completely inconsistent with itself 4608 * (it doesn't have 8 streams). Accept the settings in this case anyway. 4609 */ 4610 if (!ap_min_req_set) 4611 return true; 4612 4613 /* make sure the AP is consistent with itself 4614 * 4615 * P802.11-REVme/D0.3 4616 * 26.17.1 Basic HE BSS operation 4617 * 4618 * A STA that is operating in an HE BSS shall be able to receive and 4619 * transmit at each of the <HE-MCS, NSS> tuple values indicated by the 4620 * Basic HE-MCS And NSS Set field of the HE Operation parameter of the 4621 * MLME-START.request primitive and shall be able to receive at each of 4622 * the <HE-MCS, NSS> tuple values indicated by the Supported HE-MCS and 4623 * NSS Set field in the HE Capabilities parameter of the MLMESTART.request 4624 * primitive 4625 */ 4626 for (nss = 8; nss > 0; nss--) { 4627 u8 ap_op_val = (ap_min_req_set >> (2 * (nss - 1))) & 3; 4628 u8 ap_rx_val; 4629 u8 ap_tx_val; 4630 4631 if (ap_op_val == IEEE80211_HE_MCS_NOT_SUPPORTED) 4632 continue; 4633 4634 ap_rx_val = (mcs_80_map_rx >> (2 * (nss - 1))) & 3; 4635 ap_tx_val = (mcs_80_map_tx >> (2 * (nss - 1))) & 3; 4636 4637 if (ap_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED || 4638 ap_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED || 4639 ap_rx_val < ap_op_val || ap_tx_val < ap_op_val) { 4640 sdata_info(sdata, 4641 "Invalid rates for %d Nss, rx %d, tx %d oper %d, disable HE\n", 4642 nss, ap_rx_val, ap_rx_val, ap_op_val); 4643 return false; 4644 } 4645 } 4646 4647 return true; 4648 } 4649 4650 static bool 4651 ieee80211_verify_sta_he_mcs_support(struct ieee80211_sub_if_data *sdata, 4652 struct ieee80211_supported_band *sband, 4653 const struct ieee80211_he_operation *he_op) 4654 { 4655 const struct ieee80211_sta_he_cap *sta_he_cap = 4656 ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif); 4657 u16 ap_min_req_set; 4658 int i; 4659 4660 if (!sta_he_cap || !he_op) 4661 return false; 4662 4663 ap_min_req_set = le16_to_cpu(he_op->he_mcs_nss_set); 4664 4665 /* 4666 * Apparently iPhone 13 (at least iOS version 15.3.1) sets this to all 4667 * zeroes, which is nonsense, and completely inconsistent with itself 4668 * (it doesn't have 8 streams). Accept the settings in this case anyway. 4669 */ 4670 if (!ap_min_req_set) 4671 return true; 4672 4673 /* Need to go over for 80MHz, 160MHz and for 80+80 */ 4674 for (i = 0; i < 3; i++) { 4675 const struct ieee80211_he_mcs_nss_supp *sta_mcs_nss_supp = 4676 &sta_he_cap->he_mcs_nss_supp; 4677 u16 sta_mcs_map_rx = 4678 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i]); 4679 u16 sta_mcs_map_tx = 4680 le16_to_cpu(((__le16 *)sta_mcs_nss_supp)[2 * i + 1]); 4681 u8 nss; 4682 bool verified = true; 4683 4684 /* 4685 * For each band there is a maximum of 8 spatial streams 4686 * possible. Each of the sta_mcs_map_* is a 16-bit struct built 4687 * of 2 bits per NSS (1-8), with the values defined in enum 4688 * ieee80211_he_mcs_support. Need to make sure STA TX and RX 4689 * capabilities aren't less than the AP's minimum requirements 4690 * for this HE BSS per SS. 4691 * It is enough to find one such band that meets the reqs. 4692 */ 4693 for (nss = 8; nss > 0; nss--) { 4694 u8 sta_rx_val = (sta_mcs_map_rx >> (2 * (nss - 1))) & 3; 4695 u8 sta_tx_val = (sta_mcs_map_tx >> (2 * (nss - 1))) & 3; 4696 u8 ap_val = (ap_min_req_set >> (2 * (nss - 1))) & 3; 4697 4698 if (ap_val == IEEE80211_HE_MCS_NOT_SUPPORTED) 4699 continue; 4700 4701 /* 4702 * Make sure the HE AP doesn't require MCSs that aren't 4703 * supported by the client as required by spec 4704 * 4705 * P802.11-REVme/D0.3 4706 * 26.17.1 Basic HE BSS operation 4707 * 4708 * An HE STA shall not attempt to join * (MLME-JOIN.request primitive) 4709 * a BSS, unless it supports (i.e., is able to both transmit and 4710 * receive using) all of the <HE-MCS, NSS> tuples in the basic 4711 * HE-MCS and NSS set. 4712 */ 4713 if (sta_rx_val == IEEE80211_HE_MCS_NOT_SUPPORTED || 4714 sta_tx_val == IEEE80211_HE_MCS_NOT_SUPPORTED || 4715 (ap_val > sta_rx_val) || (ap_val > sta_tx_val)) { 4716 verified = false; 4717 break; 4718 } 4719 } 4720 4721 if (verified) 4722 return true; 4723 } 4724 4725 /* If here, STA doesn't meet AP's HE min requirements */ 4726 return false; 4727 } 4728 4729 static u8 4730 ieee80211_get_eht_cap_mcs_nss(const struct ieee80211_sta_he_cap *sta_he_cap, 4731 const struct ieee80211_sta_eht_cap *sta_eht_cap, 4732 unsigned int idx, int bw) 4733 { 4734 u8 he_phy_cap0 = sta_he_cap->he_cap_elem.phy_cap_info[0]; 4735 u8 eht_phy_cap0 = sta_eht_cap->eht_cap_elem.phy_cap_info[0]; 4736 4737 /* handle us being a 20 MHz-only EHT STA - with four values 4738 * for MCS 0-7, 8-9, 10-11, 12-13. 4739 */ 4740 if (!(he_phy_cap0 & IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_MASK_ALL)) 4741 return sta_eht_cap->eht_mcs_nss_supp.only_20mhz.rx_tx_max_nss[idx]; 4742 4743 /* the others have MCS 0-9 together, rather than separately from 0-7 */ 4744 if (idx > 0) 4745 idx--; 4746 4747 switch (bw) { 4748 case 0: 4749 return sta_eht_cap->eht_mcs_nss_supp.bw._80.rx_tx_max_nss[idx]; 4750 case 1: 4751 if (!(he_phy_cap0 & 4752 (IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G | 4753 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G))) 4754 return 0xff; /* pass check */ 4755 return sta_eht_cap->eht_mcs_nss_supp.bw._160.rx_tx_max_nss[idx]; 4756 case 2: 4757 if (!(eht_phy_cap0 & IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ)) 4758 return 0xff; /* pass check */ 4759 return sta_eht_cap->eht_mcs_nss_supp.bw._320.rx_tx_max_nss[idx]; 4760 } 4761 4762 WARN_ON(1); 4763 return 0; 4764 } 4765 4766 static bool 4767 ieee80211_verify_sta_eht_mcs_support(struct ieee80211_sub_if_data *sdata, 4768 struct ieee80211_supported_band *sband, 4769 const struct ieee80211_eht_operation *eht_op) 4770 { 4771 const struct ieee80211_sta_he_cap *sta_he_cap = 4772 ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif); 4773 const struct ieee80211_sta_eht_cap *sta_eht_cap = 4774 ieee80211_get_eht_iftype_cap_vif(sband, &sdata->vif); 4775 const struct ieee80211_eht_mcs_nss_supp_20mhz_only *req; 4776 unsigned int i; 4777 4778 if (!sta_he_cap || !sta_eht_cap || !eht_op) 4779 return false; 4780 4781 req = &eht_op->basic_mcs_nss; 4782 4783 for (i = 0; i < ARRAY_SIZE(req->rx_tx_max_nss); i++) { 4784 u8 req_rx_nss, req_tx_nss; 4785 unsigned int bw; 4786 4787 req_rx_nss = u8_get_bits(req->rx_tx_max_nss[i], 4788 IEEE80211_EHT_MCS_NSS_RX); 4789 req_tx_nss = u8_get_bits(req->rx_tx_max_nss[i], 4790 IEEE80211_EHT_MCS_NSS_TX); 4791 4792 for (bw = 0; bw < 3; bw++) { 4793 u8 have, have_rx_nss, have_tx_nss; 4794 4795 have = ieee80211_get_eht_cap_mcs_nss(sta_he_cap, 4796 sta_eht_cap, 4797 i, bw); 4798 have_rx_nss = u8_get_bits(have, 4799 IEEE80211_EHT_MCS_NSS_RX); 4800 have_tx_nss = u8_get_bits(have, 4801 IEEE80211_EHT_MCS_NSS_TX); 4802 4803 if (req_rx_nss > have_rx_nss || 4804 req_tx_nss > have_tx_nss) 4805 return false; 4806 } 4807 } 4808 4809 return true; 4810 } 4811 4812 static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata, 4813 struct ieee80211_link_data *link, 4814 struct cfg80211_bss *cbss, 4815 bool mlo, 4816 ieee80211_conn_flags_t *conn_flags) 4817 { 4818 struct ieee80211_local *local = sdata->local; 4819 const struct ieee80211_ht_cap *ht_cap = NULL; 4820 const struct ieee80211_ht_operation *ht_oper = NULL; 4821 const struct ieee80211_vht_operation *vht_oper = NULL; 4822 const struct ieee80211_he_operation *he_oper = NULL; 4823 const struct ieee80211_eht_operation *eht_oper = NULL; 4824 const struct ieee80211_s1g_oper_ie *s1g_oper = NULL; 4825 struct ieee80211_supported_band *sband; 4826 struct cfg80211_chan_def chandef; 4827 bool is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ; 4828 bool is_5ghz = cbss->channel->band == NL80211_BAND_5GHZ; 4829 bool supports_mlo = false; 4830 struct ieee80211_bss *bss = (void *)cbss->priv; 4831 struct ieee80211_elems_parse_params parse_params = { 4832 .link_id = -1, 4833 .from_ap = true, 4834 }; 4835 struct ieee802_11_elems *elems; 4836 const struct cfg80211_bss_ies *ies; 4837 int ret; 4838 u32 i; 4839 bool have_80mhz; 4840 4841 lockdep_assert_wiphy(local->hw.wiphy); 4842 4843 rcu_read_lock(); 4844 4845 ies = rcu_dereference(cbss->ies); 4846 parse_params.start = ies->data; 4847 parse_params.len = ies->len; 4848 elems = ieee802_11_parse_elems_full(&parse_params); 4849 if (!elems) { 4850 rcu_read_unlock(); 4851 return -ENOMEM; 4852 } 4853 4854 sband = local->hw.wiphy->bands[cbss->channel->band]; 4855 4856 *conn_flags &= ~(IEEE80211_CONN_DISABLE_40MHZ | 4857 IEEE80211_CONN_DISABLE_80P80MHZ | 4858 IEEE80211_CONN_DISABLE_160MHZ); 4859 4860 /* disable HT/VHT/HE if we don't support them */ 4861 if (!sband->ht_cap.ht_supported && !is_6ghz) { 4862 mlme_dbg(sdata, "HT not supported, disabling HT/VHT/HE/EHT\n"); 4863 *conn_flags |= IEEE80211_CONN_DISABLE_HT; 4864 *conn_flags |= IEEE80211_CONN_DISABLE_VHT; 4865 *conn_flags |= IEEE80211_CONN_DISABLE_HE; 4866 *conn_flags |= IEEE80211_CONN_DISABLE_EHT; 4867 } 4868 4869 if (!sband->vht_cap.vht_supported && is_5ghz) { 4870 mlme_dbg(sdata, "VHT not supported, disabling VHT/HE/EHT\n"); 4871 *conn_flags |= IEEE80211_CONN_DISABLE_VHT; 4872 *conn_flags |= IEEE80211_CONN_DISABLE_HE; 4873 *conn_flags |= IEEE80211_CONN_DISABLE_EHT; 4874 } 4875 4876 if (!ieee80211_get_he_iftype_cap_vif(sband, &sdata->vif)) { 4877 mlme_dbg(sdata, "HE not supported, disabling HE and EHT\n"); 4878 *conn_flags |= IEEE80211_CONN_DISABLE_HE; 4879 *conn_flags |= IEEE80211_CONN_DISABLE_EHT; 4880 } 4881 4882 if (!ieee80211_get_eht_iftype_cap_vif(sband, &sdata->vif)) { 4883 mlme_dbg(sdata, "EHT not supported, disabling EHT\n"); 4884 *conn_flags |= IEEE80211_CONN_DISABLE_EHT; 4885 } 4886 4887 if (!(*conn_flags & IEEE80211_CONN_DISABLE_HT) && !is_6ghz) { 4888 ht_oper = elems->ht_operation; 4889 ht_cap = elems->ht_cap_elem; 4890 4891 if (!ht_cap) { 4892 *conn_flags |= IEEE80211_CONN_DISABLE_HT; 4893 ht_oper = NULL; 4894 } 4895 } 4896 4897 if (!(*conn_flags & IEEE80211_CONN_DISABLE_VHT) && !is_6ghz) { 4898 vht_oper = elems->vht_operation; 4899 if (vht_oper && !ht_oper) { 4900 vht_oper = NULL; 4901 sdata_info(sdata, 4902 "AP advertised VHT without HT, disabling HT/VHT/HE\n"); 4903 *conn_flags |= IEEE80211_CONN_DISABLE_HT; 4904 *conn_flags |= IEEE80211_CONN_DISABLE_VHT; 4905 *conn_flags |= IEEE80211_CONN_DISABLE_HE; 4906 *conn_flags |= IEEE80211_CONN_DISABLE_EHT; 4907 } 4908 4909 if (!elems->vht_cap_elem) { 4910 *conn_flags |= IEEE80211_CONN_DISABLE_VHT; 4911 vht_oper = NULL; 4912 } 4913 } 4914 4915 if (!(*conn_flags & IEEE80211_CONN_DISABLE_HE)) { 4916 he_oper = elems->he_operation; 4917 4918 if (link && is_6ghz) { 4919 struct ieee80211_bss_conf *bss_conf; 4920 u8 j = 0; 4921 4922 bss_conf = link->conf; 4923 4924 if (elems->pwr_constr_elem) 4925 bss_conf->pwr_reduction = *elems->pwr_constr_elem; 4926 4927 BUILD_BUG_ON(ARRAY_SIZE(bss_conf->tx_pwr_env) != 4928 ARRAY_SIZE(elems->tx_pwr_env)); 4929 4930 for (i = 0; i < elems->tx_pwr_env_num; i++) { 4931 if (elems->tx_pwr_env_len[i] > 4932 sizeof(bss_conf->tx_pwr_env[j])) 4933 continue; 4934 4935 bss_conf->tx_pwr_env_num++; 4936 memcpy(&bss_conf->tx_pwr_env[j], elems->tx_pwr_env[i], 4937 elems->tx_pwr_env_len[i]); 4938 j++; 4939 } 4940 } 4941 4942 if (!ieee80211_verify_peer_he_mcs_support(sdata, ies, he_oper) || 4943 !ieee80211_verify_sta_he_mcs_support(sdata, sband, he_oper)) 4944 *conn_flags |= IEEE80211_CONN_DISABLE_HE | 4945 IEEE80211_CONN_DISABLE_EHT; 4946 } 4947 4948 /* 4949 * EHT requires HE to be supported as well. Specifically for 6 GHz 4950 * channels, the operation channel information can only be deduced from 4951 * both the 6 GHz operation information (from the HE operation IE) and 4952 * EHT operation. 4953 */ 4954 if (!(*conn_flags & 4955 (IEEE80211_CONN_DISABLE_HE | 4956 IEEE80211_CONN_DISABLE_EHT)) && 4957 he_oper) { 4958 const struct cfg80211_bss_ies *cbss_ies; 4959 const struct element *eht_ml_elem; 4960 const u8 *eht_oper_ie; 4961 4962 cbss_ies = rcu_dereference(cbss->ies); 4963 eht_oper_ie = cfg80211_find_ext_ie(WLAN_EID_EXT_EHT_OPERATION, 4964 cbss_ies->data, cbss_ies->len); 4965 if (eht_oper_ie && eht_oper_ie[1] >= 4966 1 + sizeof(struct ieee80211_eht_operation)) 4967 eht_oper = (void *)(eht_oper_ie + 3); 4968 else 4969 eht_oper = NULL; 4970 4971 if (!ieee80211_verify_sta_eht_mcs_support(sdata, sband, eht_oper)) 4972 *conn_flags |= IEEE80211_CONN_DISABLE_EHT; 4973 4974 eht_ml_elem = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_MULTI_LINK, 4975 cbss_ies->data, cbss_ies->len); 4976 4977 /* data + 1 / datalen - 1 since it's an extended element */ 4978 if (!(*conn_flags & IEEE80211_CONN_DISABLE_EHT) && 4979 eht_ml_elem && 4980 ieee80211_mle_type_ok(eht_ml_elem->data + 1, 4981 IEEE80211_ML_CONTROL_TYPE_BASIC, 4982 eht_ml_elem->datalen - 1)) { 4983 supports_mlo = true; 4984 4985 sdata->vif.cfg.eml_cap = 4986 ieee80211_mle_get_eml_cap(eht_ml_elem->data + 1); 4987 sdata->vif.cfg.eml_med_sync_delay = 4988 ieee80211_mle_get_eml_med_sync_delay(eht_ml_elem->data + 1); 4989 } 4990 } 4991 4992 /* Allow VHT if at least one channel on the sband supports 80 MHz */ 4993 have_80mhz = false; 4994 for (i = 0; i < sband->n_channels; i++) { 4995 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED | 4996 IEEE80211_CHAN_NO_80MHZ)) 4997 continue; 4998 4999 have_80mhz = true; 5000 break; 5001 } 5002 5003 if (!have_80mhz) { 5004 sdata_info(sdata, "80 MHz not supported, disabling VHT\n"); 5005 *conn_flags |= IEEE80211_CONN_DISABLE_VHT; 5006 } 5007 5008 if (sband->band == NL80211_BAND_S1GHZ) { 5009 s1g_oper = elems->s1g_oper; 5010 if (!s1g_oper) 5011 sdata_info(sdata, 5012 "AP missing S1G operation element?\n"); 5013 } 5014 5015 *conn_flags |= 5016 ieee80211_determine_chantype(sdata, link, *conn_flags, 5017 sband, 5018 cbss->channel, 5019 bss->vht_cap_info, 5020 ht_oper, vht_oper, 5021 he_oper, eht_oper, 5022 s1g_oper, 5023 &chandef, false); 5024 5025 if (link) 5026 link->needed_rx_chains = 5027 min(ieee80211_max_rx_chains(link, cbss), 5028 local->rx_chains); 5029 5030 rcu_read_unlock(); 5031 /* the element data was RCU protected so no longer valid anyway */ 5032 kfree(elems); 5033 elems = NULL; 5034 5035 if (*conn_flags & IEEE80211_CONN_DISABLE_HE && is_6ghz) { 5036 sdata_info(sdata, "Rejecting non-HE 6/7 GHz connection"); 5037 return -EINVAL; 5038 } 5039 5040 if (mlo && !supports_mlo) { 5041 sdata_info(sdata, "Rejecting MLO as it is not supported by AP\n"); 5042 return -EINVAL; 5043 } 5044 5045 if (!link) 5046 return 0; 5047 5048 /* will change later if needed */ 5049 link->smps_mode = IEEE80211_SMPS_OFF; 5050 5051 /* 5052 * If this fails (possibly due to channel context sharing 5053 * on incompatible channels, e.g. 80+80 and 160 sharing the 5054 * same control channel) try to use a smaller bandwidth. 5055 */ 5056 ret = ieee80211_link_use_channel(link, &chandef, 5057 IEEE80211_CHANCTX_SHARED); 5058 5059 /* don't downgrade for 5 and 10 MHz channels, though. */ 5060 if (chandef.width == NL80211_CHAN_WIDTH_5 || 5061 chandef.width == NL80211_CHAN_WIDTH_10) 5062 goto out; 5063 5064 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) { 5065 *conn_flags |= 5066 ieee80211_chandef_downgrade(&chandef); 5067 ret = ieee80211_link_use_channel(link, &chandef, 5068 IEEE80211_CHANCTX_SHARED); 5069 } 5070 out: 5071 return ret; 5072 } 5073 5074 static bool ieee80211_get_dtim(const struct cfg80211_bss_ies *ies, 5075 u8 *dtim_count, u8 *dtim_period) 5076 { 5077 const u8 *tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies->data, ies->len); 5078 const u8 *idx_ie = cfg80211_find_ie(WLAN_EID_MULTI_BSSID_IDX, ies->data, 5079 ies->len); 5080 const struct ieee80211_tim_ie *tim = NULL; 5081 const struct ieee80211_bssid_index *idx; 5082 bool valid = tim_ie && tim_ie[1] >= 2; 5083 5084 if (valid) 5085 tim = (void *)(tim_ie + 2); 5086 5087 if (dtim_count) 5088 *dtim_count = valid ? tim->dtim_count : 0; 5089 5090 if (dtim_period) 5091 *dtim_period = valid ? tim->dtim_period : 0; 5092 5093 /* Check if value is overridden by non-transmitted profile */ 5094 if (!idx_ie || idx_ie[1] < 3) 5095 return valid; 5096 5097 idx = (void *)(idx_ie + 2); 5098 5099 if (dtim_count) 5100 *dtim_count = idx->dtim_count; 5101 5102 if (dtim_period) 5103 *dtim_period = idx->dtim_period; 5104 5105 return true; 5106 } 5107 5108 static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata, 5109 struct ieee80211_mgmt *mgmt, 5110 struct ieee802_11_elems *elems, 5111 const u8 *elem_start, unsigned int elem_len) 5112 { 5113 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 5114 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data; 5115 struct ieee80211_local *local = sdata->local; 5116 unsigned int link_id; 5117 struct sta_info *sta; 5118 u64 changed[IEEE80211_MLD_MAX_NUM_LINKS] = {}; 5119 u16 valid_links = 0, dormant_links = 0; 5120 int err; 5121 5122 lockdep_assert_wiphy(sdata->local->hw.wiphy); 5123 /* 5124 * station info was already allocated and inserted before 5125 * the association and should be available to us 5126 */ 5127 sta = sta_info_get(sdata, assoc_data->ap_addr); 5128 if (WARN_ON(!sta)) 5129 goto out_err; 5130 5131 if (ieee80211_vif_is_mld(&sdata->vif)) { 5132 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) { 5133 if (!assoc_data->link[link_id].bss) 5134 continue; 5135 5136 valid_links |= BIT(link_id); 5137 if (assoc_data->link[link_id].disabled) 5138 dormant_links |= BIT(link_id); 5139 5140 if (link_id != assoc_data->assoc_link_id) { 5141 err = ieee80211_sta_allocate_link(sta, link_id); 5142 if (err) 5143 goto out_err; 5144 } 5145 } 5146 5147 ieee80211_vif_set_links(sdata, valid_links, dormant_links); 5148 } 5149 5150 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) { 5151 struct cfg80211_bss *cbss = assoc_data->link[link_id].bss; 5152 struct ieee80211_link_data *link; 5153 struct link_sta_info *link_sta; 5154 5155 if (!cbss) 5156 continue; 5157 5158 link = sdata_dereference(sdata->link[link_id], sdata); 5159 if (WARN_ON(!link)) 5160 goto out_err; 5161 5162 if (ieee80211_vif_is_mld(&sdata->vif)) 5163 link_info(link, 5164 "local address %pM, AP link address %pM%s\n", 5165 link->conf->addr, 5166 assoc_data->link[link_id].bss->bssid, 5167 link_id == assoc_data->assoc_link_id ? 5168 " (assoc)" : ""); 5169 5170 link_sta = rcu_dereference_protected(sta->link[link_id], 5171 lockdep_is_held(&local->hw.wiphy->mtx)); 5172 if (WARN_ON(!link_sta)) 5173 goto out_err; 5174 5175 if (!link->u.mgd.have_beacon) { 5176 const struct cfg80211_bss_ies *ies; 5177 5178 rcu_read_lock(); 5179 ies = rcu_dereference(cbss->beacon_ies); 5180 if (ies) 5181 link->u.mgd.have_beacon = true; 5182 else 5183 ies = rcu_dereference(cbss->ies); 5184 ieee80211_get_dtim(ies, 5185 &link->conf->sync_dtim_count, 5186 &link->u.mgd.dtim_period); 5187 link->conf->beacon_int = cbss->beacon_interval; 5188 rcu_read_unlock(); 5189 } 5190 5191 link->conf->dtim_period = link->u.mgd.dtim_period ?: 1; 5192 5193 if (link_id != assoc_data->assoc_link_id) { 5194 err = ieee80211_prep_channel(sdata, link, cbss, true, 5195 &link->u.mgd.conn_flags); 5196 if (err) { 5197 link_info(link, "prep_channel failed\n"); 5198 goto out_err; 5199 } 5200 } 5201 5202 err = ieee80211_mgd_setup_link_sta(link, sta, link_sta, 5203 assoc_data->link[link_id].bss); 5204 if (err) 5205 goto out_err; 5206 5207 if (!ieee80211_assoc_config_link(link, link_sta, 5208 assoc_data->link[link_id].bss, 5209 mgmt, elem_start, elem_len, 5210 &changed[link_id])) 5211 goto out_err; 5212 5213 if (assoc_data->link[link_id].status != WLAN_STATUS_SUCCESS) { 5214 valid_links &= ~BIT(link_id); 5215 ieee80211_sta_remove_link(sta, link_id); 5216 continue; 5217 } 5218 5219 if (link_id != assoc_data->assoc_link_id) { 5220 err = ieee80211_sta_activate_link(sta, link_id); 5221 if (err) 5222 goto out_err; 5223 } 5224 } 5225 5226 /* links might have changed due to rejected ones, set them again */ 5227 ieee80211_vif_set_links(sdata, valid_links, dormant_links); 5228 5229 rate_control_rate_init(sta); 5230 5231 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) { 5232 set_sta_flag(sta, WLAN_STA_MFP); 5233 sta->sta.mfp = true; 5234 } else { 5235 sta->sta.mfp = false; 5236 } 5237 5238 ieee80211_sta_set_max_amsdu_subframes(sta, elems->ext_capab, 5239 elems->ext_capab_len); 5240 5241 sta->sta.wme = (elems->wmm_param || elems->s1g_capab) && 5242 local->hw.queues >= IEEE80211_NUM_ACS; 5243 5244 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC); 5245 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT)) 5246 err = sta_info_move_state(sta, IEEE80211_STA_AUTHORIZED); 5247 if (err) { 5248 sdata_info(sdata, 5249 "failed to move station %pM to desired state\n", 5250 sta->sta.addr); 5251 WARN_ON(__sta_info_destroy(sta)); 5252 goto out_err; 5253 } 5254 5255 if (sdata->wdev.use_4addr) 5256 drv_sta_set_4addr(local, sdata, &sta->sta, true); 5257 5258 ieee80211_set_associated(sdata, assoc_data, changed); 5259 5260 /* 5261 * If we're using 4-addr mode, let the AP know that we're 5262 * doing so, so that it can create the STA VLAN on its side 5263 */ 5264 if (ifmgd->use_4addr) 5265 ieee80211_send_4addr_nullfunc(local, sdata); 5266 5267 /* 5268 * Start timer to probe the connection to the AP now. 5269 * Also start the timer that will detect beacon loss. 5270 */ 5271 ieee80211_sta_reset_beacon_monitor(sdata); 5272 ieee80211_sta_reset_conn_monitor(sdata); 5273 5274 return true; 5275 out_err: 5276 eth_zero_addr(sdata->vif.cfg.ap_addr); 5277 return false; 5278 } 5279 5280 static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata, 5281 struct ieee80211_mgmt *mgmt, 5282 size_t len) 5283 { 5284 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 5285 struct ieee80211_mgd_assoc_data *assoc_data = ifmgd->assoc_data; 5286 u16 capab_info, status_code, aid; 5287 struct ieee80211_elems_parse_params parse_params = { 5288 .bss = NULL, 5289 .link_id = -1, 5290 .from_ap = true, 5291 }; 5292 struct ieee802_11_elems *elems; 5293 int ac; 5294 const u8 *elem_start; 5295 unsigned int elem_len; 5296 bool reassoc; 5297 struct ieee80211_event event = { 5298 .type = MLME_EVENT, 5299 .u.mlme.data = ASSOC_EVENT, 5300 }; 5301 struct ieee80211_prep_tx_info info = {}; 5302 struct cfg80211_rx_assoc_resp_data resp = { 5303 .uapsd_queues = -1, 5304 }; 5305 u8 ap_mld_addr[ETH_ALEN] __aligned(2); 5306 unsigned int link_id; 5307 5308 lockdep_assert_wiphy(sdata->local->hw.wiphy); 5309 5310 if (!assoc_data) 5311 return; 5312 5313 if (!ether_addr_equal(assoc_data->ap_addr, mgmt->bssid) || 5314 !ether_addr_equal(assoc_data->ap_addr, mgmt->sa)) 5315 return; 5316 5317 /* 5318 * AssocResp and ReassocResp have identical structure, so process both 5319 * of them in this function. 5320 */ 5321 5322 if (len < 24 + 6) 5323 return; 5324 5325 reassoc = ieee80211_is_reassoc_resp(mgmt->frame_control); 5326 capab_info = le16_to_cpu(mgmt->u.assoc_resp.capab_info); 5327 status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code); 5328 if (assoc_data->s1g) 5329 elem_start = mgmt->u.s1g_assoc_resp.variable; 5330 else 5331 elem_start = mgmt->u.assoc_resp.variable; 5332 5333 /* 5334 * Note: this may not be perfect, AP might misbehave - if 5335 * anyone needs to rely on perfect complete notification 5336 * with the exact right subtype, then we need to track what 5337 * we actually transmitted. 5338 */ 5339 info.subtype = reassoc ? IEEE80211_STYPE_REASSOC_REQ : 5340 IEEE80211_STYPE_ASSOC_REQ; 5341 5342 if (assoc_data->fils_kek_len && 5343 fils_decrypt_assoc_resp(sdata, (u8 *)mgmt, &len, assoc_data) < 0) 5344 return; 5345 5346 elem_len = len - (elem_start - (u8 *)mgmt); 5347 parse_params.start = elem_start; 5348 parse_params.len = elem_len; 5349 elems = ieee802_11_parse_elems_full(&parse_params); 5350 if (!elems) 5351 goto notify_driver; 5352 5353 if (elems->aid_resp) 5354 aid = le16_to_cpu(elems->aid_resp->aid); 5355 else if (assoc_data->s1g) 5356 aid = 0; /* TODO */ 5357 else 5358 aid = le16_to_cpu(mgmt->u.assoc_resp.aid); 5359 5360 /* 5361 * The 5 MSB of the AID field are reserved 5362 * (802.11-2016 9.4.1.8 AID field) 5363 */ 5364 aid &= 0x7ff; 5365 5366 sdata_info(sdata, 5367 "RX %sssocResp from %pM (capab=0x%x status=%d aid=%d)\n", 5368 reassoc ? "Rea" : "A", assoc_data->ap_addr, 5369 capab_info, status_code, (u16)(aid & ~(BIT(15) | BIT(14)))); 5370 5371 ifmgd->broken_ap = false; 5372 5373 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY && 5374 elems->timeout_int && 5375 elems->timeout_int->type == WLAN_TIMEOUT_ASSOC_COMEBACK) { 5376 u32 tu, ms; 5377 5378 cfg80211_assoc_comeback(sdata->dev, assoc_data->ap_addr, 5379 le32_to_cpu(elems->timeout_int->value)); 5380 5381 tu = le32_to_cpu(elems->timeout_int->value); 5382 ms = tu * 1024 / 1000; 5383 sdata_info(sdata, 5384 "%pM rejected association temporarily; comeback duration %u TU (%u ms)\n", 5385 assoc_data->ap_addr, tu, ms); 5386 assoc_data->timeout = jiffies + msecs_to_jiffies(ms); 5387 assoc_data->timeout_started = true; 5388 assoc_data->comeback = true; 5389 if (ms > IEEE80211_ASSOC_TIMEOUT) 5390 run_again(sdata, assoc_data->timeout); 5391 goto notify_driver; 5392 } 5393 5394 if (status_code != WLAN_STATUS_SUCCESS) { 5395 sdata_info(sdata, "%pM denied association (code=%d)\n", 5396 assoc_data->ap_addr, status_code); 5397 event.u.mlme.status = MLME_DENIED; 5398 event.u.mlme.reason = status_code; 5399 drv_event_callback(sdata->local, sdata, &event); 5400 } else { 5401 if (aid == 0 || aid > IEEE80211_MAX_AID) { 5402 sdata_info(sdata, 5403 "invalid AID value %d (out of range), turn off PS\n", 5404 aid); 5405 aid = 0; 5406 ifmgd->broken_ap = true; 5407 } 5408 5409 if (ieee80211_vif_is_mld(&sdata->vif)) { 5410 if (!elems->ml_basic) { 5411 sdata_info(sdata, 5412 "MLO association with %pM but no multi-link element in response!\n", 5413 assoc_data->ap_addr); 5414 goto abandon_assoc; 5415 } 5416 5417 if (le16_get_bits(elems->ml_basic->control, 5418 IEEE80211_ML_CONTROL_TYPE) != 5419 IEEE80211_ML_CONTROL_TYPE_BASIC) { 5420 sdata_info(sdata, 5421 "bad multi-link element (control=0x%x)\n", 5422 le16_to_cpu(elems->ml_basic->control)); 5423 goto abandon_assoc; 5424 } else { 5425 struct ieee80211_mle_basic_common_info *common; 5426 5427 common = (void *)elems->ml_basic->variable; 5428 5429 if (memcmp(assoc_data->ap_addr, 5430 common->mld_mac_addr, ETH_ALEN)) { 5431 sdata_info(sdata, 5432 "AP MLD MAC address mismatch: got %pM expected %pM\n", 5433 common->mld_mac_addr, 5434 assoc_data->ap_addr); 5435 goto abandon_assoc; 5436 } 5437 } 5438 } 5439 5440 sdata->vif.cfg.aid = aid; 5441 5442 if (!ieee80211_assoc_success(sdata, mgmt, elems, 5443 elem_start, elem_len)) { 5444 /* oops -- internal error -- send timeout for now */ 5445 ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT); 5446 goto notify_driver; 5447 } 5448 event.u.mlme.status = MLME_SUCCESS; 5449 drv_event_callback(sdata->local, sdata, &event); 5450 sdata_info(sdata, "associated\n"); 5451 5452 info.success = 1; 5453 } 5454 5455 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) { 5456 struct ieee80211_link_data *link; 5457 5458 if (!assoc_data->link[link_id].bss) 5459 continue; 5460 5461 resp.links[link_id].bss = assoc_data->link[link_id].bss; 5462 ether_addr_copy(resp.links[link_id].addr, 5463 assoc_data->link[link_id].addr); 5464 resp.links[link_id].status = assoc_data->link[link_id].status; 5465 5466 link = sdata_dereference(sdata->link[link_id], sdata); 5467 if (!link) 5468 continue; 5469 5470 /* get uapsd queues configuration - same for all links */ 5471 resp.uapsd_queues = 0; 5472 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 5473 if (link->tx_conf[ac].uapsd) 5474 resp.uapsd_queues |= ieee80211_ac_to_qos_mask[ac]; 5475 } 5476 5477 if (ieee80211_vif_is_mld(&sdata->vif)) { 5478 ether_addr_copy(ap_mld_addr, sdata->vif.cfg.ap_addr); 5479 resp.ap_mld_addr = ap_mld_addr; 5480 } 5481 5482 ieee80211_destroy_assoc_data(sdata, 5483 status_code == WLAN_STATUS_SUCCESS ? 5484 ASSOC_SUCCESS : 5485 ASSOC_REJECTED); 5486 5487 resp.buf = (u8 *)mgmt; 5488 resp.len = len; 5489 resp.req_ies = ifmgd->assoc_req_ies; 5490 resp.req_ies_len = ifmgd->assoc_req_ies_len; 5491 cfg80211_rx_assoc_resp(sdata->dev, &resp); 5492 notify_driver: 5493 drv_mgd_complete_tx(sdata->local, sdata, &info); 5494 kfree(elems); 5495 return; 5496 abandon_assoc: 5497 ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON); 5498 goto notify_driver; 5499 } 5500 5501 static void ieee80211_rx_bss_info(struct ieee80211_link_data *link, 5502 struct ieee80211_mgmt *mgmt, size_t len, 5503 struct ieee80211_rx_status *rx_status) 5504 { 5505 struct ieee80211_sub_if_data *sdata = link->sdata; 5506 struct ieee80211_local *local = sdata->local; 5507 struct ieee80211_bss *bss; 5508 struct ieee80211_channel *channel; 5509 5510 lockdep_assert_wiphy(sdata->local->hw.wiphy); 5511 5512 channel = ieee80211_get_channel_khz(local->hw.wiphy, 5513 ieee80211_rx_status_to_khz(rx_status)); 5514 if (!channel) 5515 return; 5516 5517 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, channel); 5518 if (bss) { 5519 link->conf->beacon_rate = bss->beacon_rate; 5520 ieee80211_rx_bss_put(local, bss); 5521 } 5522 } 5523 5524 5525 static void ieee80211_rx_mgmt_probe_resp(struct ieee80211_link_data *link, 5526 struct sk_buff *skb) 5527 { 5528 struct ieee80211_sub_if_data *sdata = link->sdata; 5529 struct ieee80211_mgmt *mgmt = (void *)skb->data; 5530 struct ieee80211_if_managed *ifmgd; 5531 struct ieee80211_rx_status *rx_status = (void *) skb->cb; 5532 struct ieee80211_channel *channel; 5533 size_t baselen, len = skb->len; 5534 5535 ifmgd = &sdata->u.mgd; 5536 5537 lockdep_assert_wiphy(sdata->local->hw.wiphy); 5538 5539 /* 5540 * According to Draft P802.11ax D6.0 clause 26.17.2.3.2: 5541 * "If a 6 GHz AP receives a Probe Request frame and responds with 5542 * a Probe Response frame [..], the Address 1 field of the Probe 5543 * Response frame shall be set to the broadcast address [..]" 5544 * So, on 6GHz band we should also accept broadcast responses. 5545 */ 5546 channel = ieee80211_get_channel(sdata->local->hw.wiphy, 5547 rx_status->freq); 5548 if (!channel) 5549 return; 5550 5551 if (!ether_addr_equal(mgmt->da, sdata->vif.addr) && 5552 (channel->band != NL80211_BAND_6GHZ || 5553 !is_broadcast_ether_addr(mgmt->da))) 5554 return; /* ignore ProbeResp to foreign address */ 5555 5556 baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt; 5557 if (baselen > len) 5558 return; 5559 5560 ieee80211_rx_bss_info(link, mgmt, len, rx_status); 5561 5562 if (ifmgd->associated && 5563 ether_addr_equal(mgmt->bssid, link->u.mgd.bssid)) 5564 ieee80211_reset_ap_probe(sdata); 5565 } 5566 5567 /* 5568 * This is the canonical list of information elements we care about, 5569 * the filter code also gives us all changes to the Microsoft OUI 5570 * (00:50:F2) vendor IE which is used for WMM which we need to track, 5571 * as well as the DTPC IE (part of the Cisco OUI) used for signaling 5572 * changes to requested client power. 5573 * 5574 * We implement beacon filtering in software since that means we can 5575 * avoid processing the frame here and in cfg80211, and userspace 5576 * will not be able to tell whether the hardware supports it or not. 5577 * 5578 * XXX: This list needs to be dynamic -- userspace needs to be able to 5579 * add items it requires. It also needs to be able to tell us to 5580 * look out for other vendor IEs. 5581 */ 5582 static const u64 care_about_ies = 5583 (1ULL << WLAN_EID_COUNTRY) | 5584 (1ULL << WLAN_EID_ERP_INFO) | 5585 (1ULL << WLAN_EID_CHANNEL_SWITCH) | 5586 (1ULL << WLAN_EID_PWR_CONSTRAINT) | 5587 (1ULL << WLAN_EID_HT_CAPABILITY) | 5588 (1ULL << WLAN_EID_HT_OPERATION) | 5589 (1ULL << WLAN_EID_EXT_CHANSWITCH_ANN); 5590 5591 static void ieee80211_handle_beacon_sig(struct ieee80211_link_data *link, 5592 struct ieee80211_if_managed *ifmgd, 5593 struct ieee80211_bss_conf *bss_conf, 5594 struct ieee80211_local *local, 5595 struct ieee80211_rx_status *rx_status) 5596 { 5597 struct ieee80211_sub_if_data *sdata = link->sdata; 5598 5599 /* Track average RSSI from the Beacon frames of the current AP */ 5600 5601 if (!link->u.mgd.tracking_signal_avg) { 5602 link->u.mgd.tracking_signal_avg = true; 5603 ewma_beacon_signal_init(&link->u.mgd.ave_beacon_signal); 5604 link->u.mgd.last_cqm_event_signal = 0; 5605 link->u.mgd.count_beacon_signal = 1; 5606 link->u.mgd.last_ave_beacon_signal = 0; 5607 } else { 5608 link->u.mgd.count_beacon_signal++; 5609 } 5610 5611 ewma_beacon_signal_add(&link->u.mgd.ave_beacon_signal, 5612 -rx_status->signal); 5613 5614 if (ifmgd->rssi_min_thold != ifmgd->rssi_max_thold && 5615 link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) { 5616 int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal); 5617 int last_sig = link->u.mgd.last_ave_beacon_signal; 5618 struct ieee80211_event event = { 5619 .type = RSSI_EVENT, 5620 }; 5621 5622 /* 5623 * if signal crosses either of the boundaries, invoke callback 5624 * with appropriate parameters 5625 */ 5626 if (sig > ifmgd->rssi_max_thold && 5627 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) { 5628 link->u.mgd.last_ave_beacon_signal = sig; 5629 event.u.rssi.data = RSSI_EVENT_HIGH; 5630 drv_event_callback(local, sdata, &event); 5631 } else if (sig < ifmgd->rssi_min_thold && 5632 (last_sig >= ifmgd->rssi_max_thold || 5633 last_sig == 0)) { 5634 link->u.mgd.last_ave_beacon_signal = sig; 5635 event.u.rssi.data = RSSI_EVENT_LOW; 5636 drv_event_callback(local, sdata, &event); 5637 } 5638 } 5639 5640 if (bss_conf->cqm_rssi_thold && 5641 link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT && 5642 !(sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) { 5643 int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal); 5644 int last_event = link->u.mgd.last_cqm_event_signal; 5645 int thold = bss_conf->cqm_rssi_thold; 5646 int hyst = bss_conf->cqm_rssi_hyst; 5647 5648 if (sig < thold && 5649 (last_event == 0 || sig < last_event - hyst)) { 5650 link->u.mgd.last_cqm_event_signal = sig; 5651 ieee80211_cqm_rssi_notify( 5652 &sdata->vif, 5653 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW, 5654 sig, GFP_KERNEL); 5655 } else if (sig > thold && 5656 (last_event == 0 || sig > last_event + hyst)) { 5657 link->u.mgd.last_cqm_event_signal = sig; 5658 ieee80211_cqm_rssi_notify( 5659 &sdata->vif, 5660 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH, 5661 sig, GFP_KERNEL); 5662 } 5663 } 5664 5665 if (bss_conf->cqm_rssi_low && 5666 link->u.mgd.count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) { 5667 int sig = -ewma_beacon_signal_read(&link->u.mgd.ave_beacon_signal); 5668 int last_event = link->u.mgd.last_cqm_event_signal; 5669 int low = bss_conf->cqm_rssi_low; 5670 int high = bss_conf->cqm_rssi_high; 5671 5672 if (sig < low && 5673 (last_event == 0 || last_event >= low)) { 5674 link->u.mgd.last_cqm_event_signal = sig; 5675 ieee80211_cqm_rssi_notify( 5676 &sdata->vif, 5677 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW, 5678 sig, GFP_KERNEL); 5679 } else if (sig > high && 5680 (last_event == 0 || last_event <= high)) { 5681 link->u.mgd.last_cqm_event_signal = sig; 5682 ieee80211_cqm_rssi_notify( 5683 &sdata->vif, 5684 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH, 5685 sig, GFP_KERNEL); 5686 } 5687 } 5688 } 5689 5690 static bool ieee80211_rx_our_beacon(const u8 *tx_bssid, 5691 struct cfg80211_bss *bss) 5692 { 5693 if (ether_addr_equal(tx_bssid, bss->bssid)) 5694 return true; 5695 if (!bss->transmitted_bss) 5696 return false; 5697 return ether_addr_equal(tx_bssid, bss->transmitted_bss->bssid); 5698 } 5699 5700 static bool ieee80211_config_puncturing(struct ieee80211_link_data *link, 5701 const struct ieee80211_eht_operation *eht_oper, 5702 u64 *changed) 5703 { 5704 struct ieee80211_local *local = link->sdata->local; 5705 u16 bitmap = 0, extracted; 5706 5707 if ((eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT) && 5708 (eht_oper->params & 5709 IEEE80211_EHT_OPER_DISABLED_SUBCHANNEL_BITMAP_PRESENT)) { 5710 const struct ieee80211_eht_operation_info *info = 5711 (void *)eht_oper->optional; 5712 const u8 *disable_subchannel_bitmap = info->optional; 5713 5714 bitmap = get_unaligned_le16(disable_subchannel_bitmap); 5715 } 5716 5717 extracted = ieee80211_extract_dis_subch_bmap(eht_oper, 5718 &link->conf->chandef, 5719 bitmap); 5720 5721 /* accept if there are no changes */ 5722 if (!(*changed & BSS_CHANGED_BANDWIDTH) && 5723 extracted == link->conf->eht_puncturing) 5724 return true; 5725 5726 if (!cfg80211_valid_disable_subchannel_bitmap(&bitmap, 5727 &link->conf->chandef)) { 5728 link_info(link, 5729 "Got an invalid disable subchannel bitmap from AP %pM: bitmap = 0x%x, bw = 0x%x. disconnect\n", 5730 link->u.mgd.bssid, 5731 bitmap, 5732 link->conf->chandef.width); 5733 return false; 5734 } 5735 5736 if (bitmap && ieee80211_hw_check(&local->hw, DISALLOW_PUNCTURING)) 5737 return false; 5738 5739 ieee80211_handle_puncturing_bitmap(link, eht_oper, bitmap, changed); 5740 return true; 5741 } 5742 5743 static void ieee80211_ml_reconf_work(struct wiphy *wiphy, 5744 struct wiphy_work *work) 5745 { 5746 struct ieee80211_sub_if_data *sdata = 5747 container_of(work, struct ieee80211_sub_if_data, 5748 u.mgd.ml_reconf_work.work); 5749 u16 new_valid_links, new_active_links, new_dormant_links; 5750 int ret; 5751 5752 if (!sdata->u.mgd.removed_links) 5753 return; 5754 5755 sdata_info(sdata, 5756 "MLO Reconfiguration: work: valid=0x%x, removed=0x%x\n", 5757 sdata->vif.valid_links, sdata->u.mgd.removed_links); 5758 5759 new_valid_links = sdata->vif.valid_links & ~sdata->u.mgd.removed_links; 5760 if (new_valid_links == sdata->vif.valid_links) 5761 return; 5762 5763 if (!new_valid_links || 5764 !(new_valid_links & ~sdata->vif.dormant_links)) { 5765 sdata_info(sdata, "No valid links after reconfiguration\n"); 5766 ret = -EINVAL; 5767 goto out; 5768 } 5769 5770 new_active_links = sdata->vif.active_links & ~sdata->u.mgd.removed_links; 5771 if (new_active_links != sdata->vif.active_links) { 5772 if (!new_active_links) 5773 new_active_links = 5774 BIT(ffs(new_valid_links & 5775 ~sdata->vif.dormant_links) - 1); 5776 5777 ret = ieee80211_set_active_links(&sdata->vif, new_active_links); 5778 if (ret) { 5779 sdata_info(sdata, 5780 "Failed setting active links\n"); 5781 goto out; 5782 } 5783 } 5784 5785 new_dormant_links = sdata->vif.dormant_links & ~sdata->u.mgd.removed_links; 5786 5787 ret = ieee80211_vif_set_links(sdata, new_valid_links, 5788 new_dormant_links); 5789 if (ret) 5790 sdata_info(sdata, "Failed setting valid links\n"); 5791 5792 ieee80211_vif_cfg_change_notify(sdata, BSS_CHANGED_MLD_VALID_LINKS); 5793 5794 out: 5795 if (!ret) 5796 cfg80211_links_removed(sdata->dev, sdata->u.mgd.removed_links); 5797 else 5798 __ieee80211_disconnect(sdata); 5799 5800 sdata->u.mgd.removed_links = 0; 5801 } 5802 5803 static void ieee80211_ml_reconfiguration(struct ieee80211_sub_if_data *sdata, 5804 struct ieee802_11_elems *elems) 5805 { 5806 const struct ieee80211_multi_link_elem *ml; 5807 const struct element *sub; 5808 size_t ml_len; 5809 unsigned long removed_links = 0; 5810 u16 link_removal_timeout[IEEE80211_MLD_MAX_NUM_LINKS] = {}; 5811 u8 link_id; 5812 u32 delay; 5813 5814 if (!ieee80211_vif_is_mld(&sdata->vif) || !elems->ml_reconf) 5815 return; 5816 5817 ml_len = cfg80211_defragment_element(elems->ml_reconf_elem, 5818 elems->ie_start, 5819 elems->total_len, 5820 elems->scratch_pos, 5821 elems->scratch + elems->scratch_len - 5822 elems->scratch_pos, 5823 WLAN_EID_FRAGMENT); 5824 5825 elems->ml_reconf = (const void *)elems->scratch_pos; 5826 elems->ml_reconf_len = ml_len; 5827 ml = elems->ml_reconf; 5828 5829 /* Directly parse the sub elements as the common information doesn't 5830 * hold any useful information. 5831 */ 5832 for_each_mle_subelement(sub, (u8 *)ml, ml_len) { 5833 struct ieee80211_mle_per_sta_profile *prof = (void *)sub->data; 5834 u8 *pos = prof->variable; 5835 u16 control; 5836 5837 if (sub->id != IEEE80211_MLE_SUBELEM_PER_STA_PROFILE) 5838 continue; 5839 5840 if (!ieee80211_mle_reconf_sta_prof_size_ok(sub->data, 5841 sub->datalen)) 5842 return; 5843 5844 control = le16_to_cpu(prof->control); 5845 link_id = control & IEEE80211_MLE_STA_RECONF_CONTROL_LINK_ID; 5846 5847 removed_links |= BIT(link_id); 5848 5849 /* the MAC address should not be included, but handle it */ 5850 if (control & 5851 IEEE80211_MLE_STA_RECONF_CONTROL_STA_MAC_ADDR_PRESENT) 5852 pos += 6; 5853 5854 /* According to Draft P802.11be_D3.0, the control should 5855 * include the AP Removal Timer present. If the AP Removal Timer 5856 * is not present assume immediate removal. 5857 */ 5858 if (control & 5859 IEEE80211_MLE_STA_RECONF_CONTROL_AP_REM_TIMER_PRESENT) 5860 link_removal_timeout[link_id] = le16_to_cpu(*(__le16 *)pos); 5861 } 5862 5863 removed_links &= sdata->vif.valid_links; 5864 if (!removed_links) { 5865 /* In case the removal was cancelled, abort it */ 5866 if (sdata->u.mgd.removed_links) { 5867 sdata->u.mgd.removed_links = 0; 5868 wiphy_delayed_work_cancel(sdata->local->hw.wiphy, 5869 &sdata->u.mgd.ml_reconf_work); 5870 } 5871 return; 5872 } 5873 5874 delay = 0; 5875 for_each_set_bit(link_id, &removed_links, IEEE80211_MLD_MAX_NUM_LINKS) { 5876 struct ieee80211_bss_conf *link_conf = 5877 sdata_dereference(sdata->vif.link_conf[link_id], sdata); 5878 u32 link_delay; 5879 5880 if (!link_conf) { 5881 removed_links &= ~BIT(link_id); 5882 continue; 5883 } 5884 5885 link_delay = link_conf->beacon_int * 5886 link_removal_timeout[link_id]; 5887 5888 if (!delay) 5889 delay = link_delay; 5890 else 5891 delay = min(delay, link_delay); 5892 } 5893 5894 sdata->u.mgd.removed_links = removed_links; 5895 wiphy_delayed_work_queue(sdata->local->hw.wiphy, 5896 &sdata->u.mgd.ml_reconf_work, 5897 TU_TO_JIFFIES(delay)); 5898 } 5899 5900 static void ieee80211_tid_to_link_map_work(struct wiphy *wiphy, 5901 struct wiphy_work *work) 5902 { 5903 u16 new_active_links, new_dormant_links; 5904 struct ieee80211_sub_if_data *sdata = 5905 container_of(work, struct ieee80211_sub_if_data, 5906 u.mgd.ttlm_work.work); 5907 int ret; 5908 5909 new_active_links = sdata->u.mgd.ttlm_info.map & 5910 sdata->vif.valid_links; 5911 new_dormant_links = ~sdata->u.mgd.ttlm_info.map & 5912 sdata->vif.valid_links; 5913 if (!new_active_links) { 5914 ieee80211_disconnect(&sdata->vif, false); 5915 return; 5916 } 5917 5918 ieee80211_vif_set_links(sdata, sdata->vif.valid_links, 0); 5919 new_active_links = BIT(ffs(new_active_links) - 1); 5920 ieee80211_set_active_links(&sdata->vif, new_active_links); 5921 5922 ret = ieee80211_vif_set_links(sdata, sdata->vif.valid_links, 5923 new_dormant_links); 5924 5925 sdata->u.mgd.ttlm_info.active = true; 5926 sdata->u.mgd.ttlm_info.switch_time = 0; 5927 5928 if (!ret) 5929 ieee80211_vif_cfg_change_notify(sdata, 5930 BSS_CHANGED_MLD_VALID_LINKS); 5931 } 5932 5933 static u16 ieee80211_get_ttlm(u8 bm_size, u8 *data) 5934 { 5935 if (bm_size == 1) 5936 return *data; 5937 else 5938 return get_unaligned_le16(data); 5939 } 5940 5941 static int 5942 ieee80211_parse_adv_t2l(struct ieee80211_sub_if_data *sdata, 5943 const struct ieee80211_ttlm_elem *ttlm, 5944 struct ieee80211_adv_ttlm_info *ttlm_info) 5945 { 5946 /* The element size was already validated in 5947 * ieee80211_tid_to_link_map_size_ok() 5948 */ 5949 u8 control, link_map_presence, map_size, tid; 5950 u8 *pos; 5951 5952 memset(ttlm_info, 0, sizeof(*ttlm_info)); 5953 pos = (void *)ttlm->optional; 5954 control = ttlm->control; 5955 5956 if ((control & IEEE80211_TTLM_CONTROL_DEF_LINK_MAP) || 5957 !(control & IEEE80211_TTLM_CONTROL_SWITCH_TIME_PRESENT)) 5958 return 0; 5959 5960 if ((control & IEEE80211_TTLM_CONTROL_DIRECTION) != 5961 IEEE80211_TTLM_DIRECTION_BOTH) { 5962 sdata_info(sdata, "Invalid advertised T2L map direction\n"); 5963 return -EINVAL; 5964 } 5965 5966 link_map_presence = *pos; 5967 pos++; 5968 5969 ttlm_info->switch_time = get_unaligned_le16(pos); 5970 5971 /* Since ttlm_info->switch_time == 0 means no switch time, bump it 5972 * by 1. 5973 */ 5974 if (!ttlm_info->switch_time) 5975 ttlm_info->switch_time = 1; 5976 5977 pos += 2; 5978 5979 if (control & IEEE80211_TTLM_CONTROL_EXPECTED_DUR_PRESENT) { 5980 ttlm_info->duration = pos[0] | pos[1] << 8 | pos[2] << 16; 5981 pos += 3; 5982 } 5983 5984 if (control & IEEE80211_TTLM_CONTROL_LINK_MAP_SIZE) 5985 map_size = 1; 5986 else 5987 map_size = 2; 5988 5989 /* According to Draft P802.11be_D3.0 clause 35.3.7.1.7, an AP MLD shall 5990 * not advertise a TID-to-link mapping that does not map all TIDs to the 5991 * same link set, reject frame if not all links have mapping 5992 */ 5993 if (link_map_presence != 0xff) { 5994 sdata_info(sdata, 5995 "Invalid advertised T2L mapping presence indicator\n"); 5996 return -EINVAL; 5997 } 5998 5999 ttlm_info->map = ieee80211_get_ttlm(map_size, pos); 6000 if (!ttlm_info->map) { 6001 sdata_info(sdata, 6002 "Invalid advertised T2L map for TID 0\n"); 6003 return -EINVAL; 6004 } 6005 6006 pos += map_size; 6007 6008 for (tid = 1; tid < 8; tid++) { 6009 u16 map = ieee80211_get_ttlm(map_size, pos); 6010 6011 if (map != ttlm_info->map) { 6012 sdata_info(sdata, "Invalid advertised T2L map for tid %d\n", 6013 tid); 6014 return -EINVAL; 6015 } 6016 6017 pos += map_size; 6018 } 6019 return 0; 6020 } 6021 6022 static void ieee80211_process_adv_ttlm(struct ieee80211_sub_if_data *sdata, 6023 struct ieee802_11_elems *elems, 6024 u64 beacon_ts) 6025 { 6026 u8 i; 6027 int ret; 6028 6029 if (!ieee80211_vif_is_mld(&sdata->vif)) 6030 return; 6031 6032 if (!elems->ttlm_num) { 6033 if (sdata->u.mgd.ttlm_info.switch_time) { 6034 /* if a planned TID-to-link mapping was cancelled - 6035 * abort it 6036 */ 6037 wiphy_delayed_work_cancel(sdata->local->hw.wiphy, 6038 &sdata->u.mgd.ttlm_work); 6039 } else if (sdata->u.mgd.ttlm_info.active) { 6040 /* if no TID-to-link element, set to default mapping in 6041 * which all TIDs are mapped to all setup links 6042 */ 6043 ret = ieee80211_vif_set_links(sdata, 6044 sdata->vif.valid_links, 6045 0); 6046 if (ret) { 6047 sdata_info(sdata, "Failed setting valid/dormant links\n"); 6048 return; 6049 } 6050 ieee80211_vif_cfg_change_notify(sdata, 6051 BSS_CHANGED_MLD_VALID_LINKS); 6052 } 6053 memset(&sdata->u.mgd.ttlm_info, 0, 6054 sizeof(sdata->u.mgd.ttlm_info)); 6055 return; 6056 } 6057 6058 for (i = 0; i < elems->ttlm_num; i++) { 6059 struct ieee80211_adv_ttlm_info ttlm_info; 6060 u32 res; 6061 6062 res = ieee80211_parse_adv_t2l(sdata, elems->ttlm[i], 6063 &ttlm_info); 6064 6065 if (res) { 6066 __ieee80211_disconnect(sdata); 6067 return; 6068 } 6069 6070 if (ttlm_info.switch_time) { 6071 u16 beacon_ts_tu, st_tu, delay; 6072 u32 delay_jiffies; 6073 u64 mask; 6074 6075 /* The t2l map switch time is indicated with a partial 6076 * TSF value (bits 10 to 25), get the partial beacon TS 6077 * as well, and calc the delay to the start time. 6078 */ 6079 mask = GENMASK_ULL(25, 10); 6080 beacon_ts_tu = (beacon_ts & mask) >> 10; 6081 st_tu = ttlm_info.switch_time; 6082 delay = st_tu - beacon_ts_tu; 6083 6084 /* 6085 * If the switch time is far in the future, then it 6086 * could also be the previous switch still being 6087 * announced. 6088 * We can simply ignore it for now, if it is a future 6089 * switch the AP will continue to announce it anyway. 6090 */ 6091 if (delay > IEEE80211_ADV_TTLM_ST_UNDERFLOW) 6092 return; 6093 6094 delay_jiffies = TU_TO_JIFFIES(delay); 6095 6096 /* Link switching can take time, so schedule it 6097 * 100ms before to be ready on time 6098 */ 6099 if (delay_jiffies > IEEE80211_ADV_TTLM_SAFETY_BUFFER_MS) 6100 delay_jiffies -= 6101 IEEE80211_ADV_TTLM_SAFETY_BUFFER_MS; 6102 else 6103 delay_jiffies = 0; 6104 6105 sdata->u.mgd.ttlm_info = ttlm_info; 6106 wiphy_delayed_work_cancel(sdata->local->hw.wiphy, 6107 &sdata->u.mgd.ttlm_work); 6108 wiphy_delayed_work_queue(sdata->local->hw.wiphy, 6109 &sdata->u.mgd.ttlm_work, 6110 delay_jiffies); 6111 return; 6112 } 6113 } 6114 } 6115 6116 static void ieee80211_rx_mgmt_beacon(struct ieee80211_link_data *link, 6117 struct ieee80211_hdr *hdr, size_t len, 6118 struct ieee80211_rx_status *rx_status) 6119 { 6120 struct ieee80211_sub_if_data *sdata = link->sdata; 6121 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 6122 struct ieee80211_bss_conf *bss_conf = &sdata->vif.bss_conf; 6123 struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg; 6124 struct ieee80211_mgmt *mgmt = (void *) hdr; 6125 size_t baselen; 6126 struct ieee802_11_elems *elems; 6127 struct ieee80211_local *local = sdata->local; 6128 struct ieee80211_chanctx_conf *chanctx_conf; 6129 struct ieee80211_supported_band *sband; 6130 struct ieee80211_channel *chan; 6131 struct link_sta_info *link_sta; 6132 struct sta_info *sta; 6133 u64 changed = 0; 6134 bool erp_valid; 6135 u8 erp_value = 0; 6136 u32 ncrc = 0; 6137 u8 *bssid, *variable = mgmt->u.beacon.variable; 6138 u8 deauth_buf[IEEE80211_DEAUTH_FRAME_LEN]; 6139 struct ieee80211_elems_parse_params parse_params = { 6140 .link_id = -1, 6141 .from_ap = true, 6142 }; 6143 6144 lockdep_assert_wiphy(local->hw.wiphy); 6145 6146 /* Process beacon from the current BSS */ 6147 bssid = ieee80211_get_bssid(hdr, len, sdata->vif.type); 6148 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) { 6149 struct ieee80211_ext *ext = (void *) mgmt; 6150 6151 if (ieee80211_is_s1g_short_beacon(ext->frame_control)) 6152 variable = ext->u.s1g_short_beacon.variable; 6153 else 6154 variable = ext->u.s1g_beacon.variable; 6155 } 6156 6157 baselen = (u8 *) variable - (u8 *) mgmt; 6158 if (baselen > len) 6159 return; 6160 6161 parse_params.start = variable; 6162 parse_params.len = len - baselen; 6163 6164 rcu_read_lock(); 6165 chanctx_conf = rcu_dereference(link->conf->chanctx_conf); 6166 if (!chanctx_conf) { 6167 rcu_read_unlock(); 6168 return; 6169 } 6170 6171 if (ieee80211_rx_status_to_khz(rx_status) != 6172 ieee80211_channel_to_khz(chanctx_conf->def.chan)) { 6173 rcu_read_unlock(); 6174 return; 6175 } 6176 chan = chanctx_conf->def.chan; 6177 rcu_read_unlock(); 6178 6179 if (ifmgd->assoc_data && ifmgd->assoc_data->need_beacon && 6180 !WARN_ON(ieee80211_vif_is_mld(&sdata->vif)) && 6181 ieee80211_rx_our_beacon(bssid, ifmgd->assoc_data->link[0].bss)) { 6182 parse_params.bss = ifmgd->assoc_data->link[0].bss; 6183 elems = ieee802_11_parse_elems_full(&parse_params); 6184 if (!elems) 6185 return; 6186 6187 ieee80211_rx_bss_info(link, mgmt, len, rx_status); 6188 6189 if (elems->dtim_period) 6190 link->u.mgd.dtim_period = elems->dtim_period; 6191 link->u.mgd.have_beacon = true; 6192 ifmgd->assoc_data->need_beacon = false; 6193 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) { 6194 link->conf->sync_tsf = 6195 le64_to_cpu(mgmt->u.beacon.timestamp); 6196 link->conf->sync_device_ts = 6197 rx_status->device_timestamp; 6198 link->conf->sync_dtim_count = elems->dtim_count; 6199 } 6200 6201 if (elems->mbssid_config_ie) 6202 bss_conf->profile_periodicity = 6203 elems->mbssid_config_ie->profile_periodicity; 6204 else 6205 bss_conf->profile_periodicity = 0; 6206 6207 if (elems->ext_capab_len >= 11 && 6208 (elems->ext_capab[10] & WLAN_EXT_CAPA11_EMA_SUPPORT)) 6209 bss_conf->ema_ap = true; 6210 else 6211 bss_conf->ema_ap = false; 6212 6213 /* continue assoc process */ 6214 ifmgd->assoc_data->timeout = jiffies; 6215 ifmgd->assoc_data->timeout_started = true; 6216 run_again(sdata, ifmgd->assoc_data->timeout); 6217 kfree(elems); 6218 return; 6219 } 6220 6221 if (!ifmgd->associated || 6222 !ieee80211_rx_our_beacon(bssid, link->u.mgd.bss)) 6223 return; 6224 bssid = link->u.mgd.bssid; 6225 6226 if (!(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL)) 6227 ieee80211_handle_beacon_sig(link, ifmgd, bss_conf, 6228 local, rx_status); 6229 6230 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) { 6231 mlme_dbg_ratelimited(sdata, 6232 "cancelling AP probe due to a received beacon\n"); 6233 ieee80211_reset_ap_probe(sdata); 6234 } 6235 6236 /* 6237 * Push the beacon loss detection into the future since 6238 * we are processing a beacon from the AP just now. 6239 */ 6240 ieee80211_sta_reset_beacon_monitor(sdata); 6241 6242 /* TODO: CRC urrently not calculated on S1G Beacon Compatibility 6243 * element (which carries the beacon interval). Don't forget to add a 6244 * bit to care_about_ies[] above if mac80211 is interested in a 6245 * changing S1G element. 6246 */ 6247 if (!ieee80211_is_s1g_beacon(hdr->frame_control)) 6248 ncrc = crc32_be(0, (void *)&mgmt->u.beacon.beacon_int, 4); 6249 parse_params.bss = link->u.mgd.bss; 6250 parse_params.filter = care_about_ies; 6251 parse_params.crc = ncrc; 6252 elems = ieee802_11_parse_elems_full(&parse_params); 6253 if (!elems) 6254 return; 6255 ncrc = elems->crc; 6256 6257 if (ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK) && 6258 ieee80211_check_tim(elems->tim, elems->tim_len, vif_cfg->aid)) { 6259 if (local->hw.conf.dynamic_ps_timeout > 0) { 6260 if (local->hw.conf.flags & IEEE80211_CONF_PS) { 6261 local->hw.conf.flags &= ~IEEE80211_CONF_PS; 6262 ieee80211_hw_config(local, 6263 IEEE80211_CONF_CHANGE_PS); 6264 } 6265 ieee80211_send_nullfunc(local, sdata, false); 6266 } else if (!local->pspolling && sdata->u.mgd.powersave) { 6267 local->pspolling = true; 6268 6269 /* 6270 * Here is assumed that the driver will be 6271 * able to send ps-poll frame and receive a 6272 * response even though power save mode is 6273 * enabled, but some drivers might require 6274 * to disable power save here. This needs 6275 * to be investigated. 6276 */ 6277 ieee80211_send_pspoll(local, sdata); 6278 } 6279 } 6280 6281 if (sdata->vif.p2p || 6282 sdata->vif.driver_flags & IEEE80211_VIF_GET_NOA_UPDATE) { 6283 struct ieee80211_p2p_noa_attr noa = {}; 6284 int ret; 6285 6286 ret = cfg80211_get_p2p_attr(variable, 6287 len - baselen, 6288 IEEE80211_P2P_ATTR_ABSENCE_NOTICE, 6289 (u8 *) &noa, sizeof(noa)); 6290 if (ret >= 2) { 6291 if (link->u.mgd.p2p_noa_index != noa.index) { 6292 /* valid noa_attr and index changed */ 6293 link->u.mgd.p2p_noa_index = noa.index; 6294 memcpy(&bss_conf->p2p_noa_attr, &noa, sizeof(noa)); 6295 changed |= BSS_CHANGED_P2P_PS; 6296 /* 6297 * make sure we update all information, the CRC 6298 * mechanism doesn't look at P2P attributes. 6299 */ 6300 link->u.mgd.beacon_crc_valid = false; 6301 } 6302 } else if (link->u.mgd.p2p_noa_index != -1) { 6303 /* noa_attr not found and we had valid noa_attr before */ 6304 link->u.mgd.p2p_noa_index = -1; 6305 memset(&bss_conf->p2p_noa_attr, 0, sizeof(bss_conf->p2p_noa_attr)); 6306 changed |= BSS_CHANGED_P2P_PS; 6307 link->u.mgd.beacon_crc_valid = false; 6308 } 6309 } 6310 6311 if (link->u.mgd.csa_waiting_bcn) 6312 ieee80211_chswitch_post_beacon(link); 6313 6314 /* 6315 * Update beacon timing and dtim count on every beacon appearance. This 6316 * will allow the driver to use the most updated values. Do it before 6317 * comparing this one with last received beacon. 6318 * IMPORTANT: These parameters would possibly be out of sync by the time 6319 * the driver will use them. The synchronized view is currently 6320 * guaranteed only in certain callbacks. 6321 */ 6322 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY) && 6323 !ieee80211_is_s1g_beacon(hdr->frame_control)) { 6324 link->conf->sync_tsf = 6325 le64_to_cpu(mgmt->u.beacon.timestamp); 6326 link->conf->sync_device_ts = 6327 rx_status->device_timestamp; 6328 link->conf->sync_dtim_count = elems->dtim_count; 6329 } 6330 6331 if ((ncrc == link->u.mgd.beacon_crc && link->u.mgd.beacon_crc_valid) || 6332 ieee80211_is_s1g_short_beacon(mgmt->frame_control)) 6333 goto free; 6334 link->u.mgd.beacon_crc = ncrc; 6335 link->u.mgd.beacon_crc_valid = true; 6336 6337 ieee80211_rx_bss_info(link, mgmt, len, rx_status); 6338 6339 ieee80211_sta_process_chanswitch(link, rx_status->mactime, 6340 rx_status->device_timestamp, 6341 elems, true); 6342 6343 if (!link->u.mgd.disable_wmm_tracking && 6344 ieee80211_sta_wmm_params(local, link, elems->wmm_param, 6345 elems->wmm_param_len, 6346 elems->mu_edca_param_set)) 6347 changed |= BSS_CHANGED_QOS; 6348 6349 /* 6350 * If we haven't had a beacon before, tell the driver about the 6351 * DTIM period (and beacon timing if desired) now. 6352 */ 6353 if (!link->u.mgd.have_beacon) { 6354 /* a few bogus AP send dtim_period = 0 or no TIM IE */ 6355 bss_conf->dtim_period = elems->dtim_period ?: 1; 6356 6357 changed |= BSS_CHANGED_BEACON_INFO; 6358 link->u.mgd.have_beacon = true; 6359 6360 ieee80211_recalc_ps(local); 6361 6362 ieee80211_recalc_ps_vif(sdata); 6363 } 6364 6365 if (elems->erp_info) { 6366 erp_valid = true; 6367 erp_value = elems->erp_info[0]; 6368 } else { 6369 erp_valid = false; 6370 } 6371 6372 if (!ieee80211_is_s1g_beacon(hdr->frame_control)) 6373 changed |= ieee80211_handle_bss_capability(link, 6374 le16_to_cpu(mgmt->u.beacon.capab_info), 6375 erp_valid, erp_value); 6376 6377 sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr); 6378 if (WARN_ON(!sta)) { 6379 goto free; 6380 } 6381 link_sta = rcu_dereference_protected(sta->link[link->link_id], 6382 lockdep_is_held(&local->hw.wiphy->mtx)); 6383 if (WARN_ON(!link_sta)) { 6384 goto free; 6385 } 6386 6387 if (WARN_ON(!link->conf->chandef.chan)) 6388 goto free; 6389 6390 sband = local->hw.wiphy->bands[link->conf->chandef.chan->band]; 6391 6392 changed |= ieee80211_recalc_twt_req(sdata, sband, link, link_sta, elems); 6393 6394 if (ieee80211_config_bw(link, elems->ht_cap_elem, 6395 elems->vht_cap_elem, elems->ht_operation, 6396 elems->vht_operation, elems->he_operation, 6397 elems->eht_operation, 6398 elems->s1g_oper, bssid, &changed)) { 6399 sdata_info(sdata, 6400 "failed to follow AP %pM bandwidth change, disconnect\n", 6401 bssid); 6402 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, 6403 WLAN_REASON_DEAUTH_LEAVING, 6404 true, deauth_buf); 6405 ieee80211_report_disconnect(sdata, deauth_buf, 6406 sizeof(deauth_buf), true, 6407 WLAN_REASON_DEAUTH_LEAVING, 6408 false); 6409 goto free; 6410 } 6411 6412 if (elems->opmode_notif) 6413 ieee80211_vht_handle_opmode(sdata, link_sta, 6414 *elems->opmode_notif, 6415 rx_status->band); 6416 6417 changed |= ieee80211_handle_pwr_constr(link, chan, mgmt, 6418 elems->country_elem, 6419 elems->country_elem_len, 6420 elems->pwr_constr_elem, 6421 elems->cisco_dtpc_elem); 6422 6423 if (elems->eht_operation && 6424 !(link->u.mgd.conn_flags & IEEE80211_CONN_DISABLE_EHT)) { 6425 if (!ieee80211_config_puncturing(link, elems->eht_operation, 6426 &changed)) { 6427 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, 6428 WLAN_REASON_DEAUTH_LEAVING, 6429 true, deauth_buf); 6430 ieee80211_report_disconnect(sdata, deauth_buf, 6431 sizeof(deauth_buf), true, 6432 WLAN_REASON_DEAUTH_LEAVING, 6433 false); 6434 goto free; 6435 } 6436 } 6437 6438 ieee80211_ml_reconfiguration(sdata, elems); 6439 ieee80211_process_adv_ttlm(sdata, elems, 6440 le64_to_cpu(mgmt->u.beacon.timestamp)); 6441 6442 ieee80211_link_info_change_notify(sdata, link, changed); 6443 free: 6444 kfree(elems); 6445 } 6446 6447 void ieee80211_sta_rx_queued_ext(struct ieee80211_sub_if_data *sdata, 6448 struct sk_buff *skb) 6449 { 6450 struct ieee80211_link_data *link = &sdata->deflink; 6451 struct ieee80211_rx_status *rx_status; 6452 struct ieee80211_hdr *hdr; 6453 u16 fc; 6454 6455 lockdep_assert_wiphy(sdata->local->hw.wiphy); 6456 6457 rx_status = (struct ieee80211_rx_status *) skb->cb; 6458 hdr = (struct ieee80211_hdr *) skb->data; 6459 fc = le16_to_cpu(hdr->frame_control); 6460 6461 switch (fc & IEEE80211_FCTL_STYPE) { 6462 case IEEE80211_STYPE_S1G_BEACON: 6463 ieee80211_rx_mgmt_beacon(link, hdr, skb->len, rx_status); 6464 break; 6465 } 6466 } 6467 6468 void ieee80211_sta_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata, 6469 struct sk_buff *skb) 6470 { 6471 struct ieee80211_link_data *link = &sdata->deflink; 6472 struct ieee80211_rx_status *rx_status; 6473 struct ieee80211_mgmt *mgmt; 6474 u16 fc; 6475 int ies_len; 6476 6477 lockdep_assert_wiphy(sdata->local->hw.wiphy); 6478 6479 rx_status = (struct ieee80211_rx_status *) skb->cb; 6480 mgmt = (struct ieee80211_mgmt *) skb->data; 6481 fc = le16_to_cpu(mgmt->frame_control); 6482 6483 if (rx_status->link_valid) { 6484 link = sdata_dereference(sdata->link[rx_status->link_id], 6485 sdata); 6486 if (!link) 6487 return; 6488 } 6489 6490 switch (fc & IEEE80211_FCTL_STYPE) { 6491 case IEEE80211_STYPE_BEACON: 6492 ieee80211_rx_mgmt_beacon(link, (void *)mgmt, 6493 skb->len, rx_status); 6494 break; 6495 case IEEE80211_STYPE_PROBE_RESP: 6496 ieee80211_rx_mgmt_probe_resp(link, skb); 6497 break; 6498 case IEEE80211_STYPE_AUTH: 6499 ieee80211_rx_mgmt_auth(sdata, mgmt, skb->len); 6500 break; 6501 case IEEE80211_STYPE_DEAUTH: 6502 ieee80211_rx_mgmt_deauth(sdata, mgmt, skb->len); 6503 break; 6504 case IEEE80211_STYPE_DISASSOC: 6505 ieee80211_rx_mgmt_disassoc(sdata, mgmt, skb->len); 6506 break; 6507 case IEEE80211_STYPE_ASSOC_RESP: 6508 case IEEE80211_STYPE_REASSOC_RESP: 6509 ieee80211_rx_mgmt_assoc_resp(sdata, mgmt, skb->len); 6510 break; 6511 case IEEE80211_STYPE_ACTION: 6512 if (!sdata->u.mgd.associated || 6513 !ether_addr_equal(mgmt->bssid, sdata->vif.cfg.ap_addr)) 6514 break; 6515 6516 if (mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT) { 6517 struct ieee802_11_elems *elems; 6518 6519 ies_len = skb->len - 6520 offsetof(struct ieee80211_mgmt, 6521 u.action.u.chan_switch.variable); 6522 6523 if (ies_len < 0) 6524 break; 6525 6526 /* CSA IE cannot be overridden, no need for BSSID */ 6527 elems = ieee802_11_parse_elems( 6528 mgmt->u.action.u.chan_switch.variable, 6529 ies_len, true, NULL); 6530 6531 if (elems && !elems->parse_error) 6532 ieee80211_sta_process_chanswitch(link, 6533 rx_status->mactime, 6534 rx_status->device_timestamp, 6535 elems, false); 6536 kfree(elems); 6537 } else if (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC) { 6538 struct ieee802_11_elems *elems; 6539 6540 ies_len = skb->len - 6541 offsetof(struct ieee80211_mgmt, 6542 u.action.u.ext_chan_switch.variable); 6543 6544 if (ies_len < 0) 6545 break; 6546 6547 /* 6548 * extended CSA IE can't be overridden, no need for 6549 * BSSID 6550 */ 6551 elems = ieee802_11_parse_elems( 6552 mgmt->u.action.u.ext_chan_switch.variable, 6553 ies_len, true, NULL); 6554 6555 if (elems && !elems->parse_error) { 6556 /* for the handling code pretend it was an IE */ 6557 elems->ext_chansw_ie = 6558 &mgmt->u.action.u.ext_chan_switch.data; 6559 6560 ieee80211_sta_process_chanswitch(link, 6561 rx_status->mactime, 6562 rx_status->device_timestamp, 6563 elems, false); 6564 } 6565 6566 kfree(elems); 6567 } 6568 break; 6569 } 6570 } 6571 6572 static void ieee80211_sta_timer(struct timer_list *t) 6573 { 6574 struct ieee80211_sub_if_data *sdata = 6575 from_timer(sdata, t, u.mgd.timer); 6576 6577 wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work); 6578 } 6579 6580 void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata, 6581 u8 reason, bool tx) 6582 { 6583 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN]; 6584 6585 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason, 6586 tx, frame_buf); 6587 6588 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true, 6589 reason, false); 6590 } 6591 6592 static int ieee80211_auth(struct ieee80211_sub_if_data *sdata) 6593 { 6594 struct ieee80211_local *local = sdata->local; 6595 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 6596 struct ieee80211_mgd_auth_data *auth_data = ifmgd->auth_data; 6597 u32 tx_flags = 0; 6598 u16 trans = 1; 6599 u16 status = 0; 6600 struct ieee80211_prep_tx_info info = { 6601 .subtype = IEEE80211_STYPE_AUTH, 6602 }; 6603 6604 lockdep_assert_wiphy(sdata->local->hw.wiphy); 6605 6606 if (WARN_ON_ONCE(!auth_data)) 6607 return -EINVAL; 6608 6609 auth_data->tries++; 6610 6611 if (auth_data->tries > IEEE80211_AUTH_MAX_TRIES) { 6612 sdata_info(sdata, "authentication with %pM timed out\n", 6613 auth_data->ap_addr); 6614 6615 /* 6616 * Most likely AP is not in the range so remove the 6617 * bss struct for that AP. 6618 */ 6619 cfg80211_unlink_bss(local->hw.wiphy, auth_data->bss); 6620 6621 return -ETIMEDOUT; 6622 } 6623 6624 if (auth_data->algorithm == WLAN_AUTH_SAE) 6625 info.duration = jiffies_to_msecs(IEEE80211_AUTH_TIMEOUT_SAE); 6626 6627 info.link_id = auth_data->link_id; 6628 drv_mgd_prepare_tx(local, sdata, &info); 6629 6630 sdata_info(sdata, "send auth to %pM (try %d/%d)\n", 6631 auth_data->ap_addr, auth_data->tries, 6632 IEEE80211_AUTH_MAX_TRIES); 6633 6634 auth_data->expected_transaction = 2; 6635 6636 if (auth_data->algorithm == WLAN_AUTH_SAE) { 6637 trans = auth_data->sae_trans; 6638 status = auth_data->sae_status; 6639 auth_data->expected_transaction = trans; 6640 } 6641 6642 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) 6643 tx_flags = IEEE80211_TX_CTL_REQ_TX_STATUS | 6644 IEEE80211_TX_INTFL_MLME_CONN_TX; 6645 6646 ieee80211_send_auth(sdata, trans, auth_data->algorithm, status, 6647 auth_data->data, auth_data->data_len, 6648 auth_data->ap_addr, auth_data->ap_addr, 6649 NULL, 0, 0, tx_flags); 6650 6651 if (tx_flags == 0) { 6652 if (auth_data->algorithm == WLAN_AUTH_SAE) 6653 auth_data->timeout = jiffies + 6654 IEEE80211_AUTH_TIMEOUT_SAE; 6655 else 6656 auth_data->timeout = jiffies + IEEE80211_AUTH_TIMEOUT; 6657 } else { 6658 auth_data->timeout = 6659 round_jiffies_up(jiffies + IEEE80211_AUTH_TIMEOUT_LONG); 6660 } 6661 6662 auth_data->timeout_started = true; 6663 run_again(sdata, auth_data->timeout); 6664 6665 return 0; 6666 } 6667 6668 static int ieee80211_do_assoc(struct ieee80211_sub_if_data *sdata) 6669 { 6670 struct ieee80211_mgd_assoc_data *assoc_data = sdata->u.mgd.assoc_data; 6671 struct ieee80211_local *local = sdata->local; 6672 int ret; 6673 6674 lockdep_assert_wiphy(sdata->local->hw.wiphy); 6675 6676 assoc_data->tries++; 6677 if (assoc_data->tries > IEEE80211_ASSOC_MAX_TRIES) { 6678 sdata_info(sdata, "association with %pM timed out\n", 6679 assoc_data->ap_addr); 6680 6681 /* 6682 * Most likely AP is not in the range so remove the 6683 * bss struct for that AP. 6684 */ 6685 cfg80211_unlink_bss(local->hw.wiphy, 6686 assoc_data->link[assoc_data->assoc_link_id].bss); 6687 6688 return -ETIMEDOUT; 6689 } 6690 6691 sdata_info(sdata, "associate with %pM (try %d/%d)\n", 6692 assoc_data->ap_addr, assoc_data->tries, 6693 IEEE80211_ASSOC_MAX_TRIES); 6694 ret = ieee80211_send_assoc(sdata); 6695 if (ret) 6696 return ret; 6697 6698 if (!ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) { 6699 assoc_data->timeout = jiffies + IEEE80211_ASSOC_TIMEOUT; 6700 assoc_data->timeout_started = true; 6701 run_again(sdata, assoc_data->timeout); 6702 } else { 6703 assoc_data->timeout = 6704 round_jiffies_up(jiffies + 6705 IEEE80211_ASSOC_TIMEOUT_LONG); 6706 assoc_data->timeout_started = true; 6707 run_again(sdata, assoc_data->timeout); 6708 } 6709 6710 return 0; 6711 } 6712 6713 void ieee80211_mgd_conn_tx_status(struct ieee80211_sub_if_data *sdata, 6714 __le16 fc, bool acked) 6715 { 6716 struct ieee80211_local *local = sdata->local; 6717 6718 sdata->u.mgd.status_fc = fc; 6719 sdata->u.mgd.status_acked = acked; 6720 sdata->u.mgd.status_received = true; 6721 6722 wiphy_work_queue(local->hw.wiphy, &sdata->work); 6723 } 6724 6725 void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata) 6726 { 6727 struct ieee80211_local *local = sdata->local; 6728 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 6729 6730 lockdep_assert_wiphy(sdata->local->hw.wiphy); 6731 6732 if (ifmgd->status_received) { 6733 __le16 fc = ifmgd->status_fc; 6734 bool status_acked = ifmgd->status_acked; 6735 6736 ifmgd->status_received = false; 6737 if (ifmgd->auth_data && ieee80211_is_auth(fc)) { 6738 if (status_acked) { 6739 if (ifmgd->auth_data->algorithm == 6740 WLAN_AUTH_SAE) 6741 ifmgd->auth_data->timeout = 6742 jiffies + 6743 IEEE80211_AUTH_TIMEOUT_SAE; 6744 else 6745 ifmgd->auth_data->timeout = 6746 jiffies + 6747 IEEE80211_AUTH_TIMEOUT_SHORT; 6748 run_again(sdata, ifmgd->auth_data->timeout); 6749 } else { 6750 ifmgd->auth_data->timeout = jiffies - 1; 6751 } 6752 ifmgd->auth_data->timeout_started = true; 6753 } else if (ifmgd->assoc_data && 6754 !ifmgd->assoc_data->comeback && 6755 (ieee80211_is_assoc_req(fc) || 6756 ieee80211_is_reassoc_req(fc))) { 6757 /* 6758 * Update association timeout based on the TX status 6759 * for the (Re)Association Request frame. Skip this if 6760 * we have already processed a (Re)Association Response 6761 * frame that indicated need for association comeback 6762 * at a specific time in the future. This could happen 6763 * if the TX status information is delayed enough for 6764 * the response to be received and processed first. 6765 */ 6766 if (status_acked) { 6767 ifmgd->assoc_data->timeout = 6768 jiffies + IEEE80211_ASSOC_TIMEOUT_SHORT; 6769 run_again(sdata, ifmgd->assoc_data->timeout); 6770 } else { 6771 ifmgd->assoc_data->timeout = jiffies - 1; 6772 } 6773 ifmgd->assoc_data->timeout_started = true; 6774 } 6775 } 6776 6777 if (ifmgd->auth_data && ifmgd->auth_data->timeout_started && 6778 time_after(jiffies, ifmgd->auth_data->timeout)) { 6779 if (ifmgd->auth_data->done || ifmgd->auth_data->waiting) { 6780 /* 6781 * ok ... we waited for assoc or continuation but 6782 * userspace didn't do it, so kill the auth data 6783 */ 6784 ieee80211_destroy_auth_data(sdata, false); 6785 } else if (ieee80211_auth(sdata)) { 6786 u8 ap_addr[ETH_ALEN]; 6787 struct ieee80211_event event = { 6788 .type = MLME_EVENT, 6789 .u.mlme.data = AUTH_EVENT, 6790 .u.mlme.status = MLME_TIMEOUT, 6791 }; 6792 6793 memcpy(ap_addr, ifmgd->auth_data->ap_addr, ETH_ALEN); 6794 6795 ieee80211_destroy_auth_data(sdata, false); 6796 6797 cfg80211_auth_timeout(sdata->dev, ap_addr); 6798 drv_event_callback(sdata->local, sdata, &event); 6799 } 6800 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started) 6801 run_again(sdata, ifmgd->auth_data->timeout); 6802 6803 if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started && 6804 time_after(jiffies, ifmgd->assoc_data->timeout)) { 6805 if ((ifmgd->assoc_data->need_beacon && 6806 !sdata->deflink.u.mgd.have_beacon) || 6807 ieee80211_do_assoc(sdata)) { 6808 struct ieee80211_event event = { 6809 .type = MLME_EVENT, 6810 .u.mlme.data = ASSOC_EVENT, 6811 .u.mlme.status = MLME_TIMEOUT, 6812 }; 6813 6814 ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT); 6815 drv_event_callback(sdata->local, sdata, &event); 6816 } 6817 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started) 6818 run_again(sdata, ifmgd->assoc_data->timeout); 6819 6820 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL && 6821 ifmgd->associated) { 6822 u8 *bssid = sdata->deflink.u.mgd.bssid; 6823 int max_tries; 6824 6825 if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) 6826 max_tries = max_nullfunc_tries; 6827 else 6828 max_tries = max_probe_tries; 6829 6830 /* ACK received for nullfunc probing frame */ 6831 if (!ifmgd->probe_send_count) 6832 ieee80211_reset_ap_probe(sdata); 6833 else if (ifmgd->nullfunc_failed) { 6834 if (ifmgd->probe_send_count < max_tries) { 6835 mlme_dbg(sdata, 6836 "No ack for nullfunc frame to AP %pM, try %d/%i\n", 6837 bssid, ifmgd->probe_send_count, 6838 max_tries); 6839 ieee80211_mgd_probe_ap_send(sdata); 6840 } else { 6841 mlme_dbg(sdata, 6842 "No ack for nullfunc frame to AP %pM, disconnecting.\n", 6843 bssid); 6844 ieee80211_sta_connection_lost(sdata, 6845 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, 6846 false); 6847 } 6848 } else if (time_is_after_jiffies(ifmgd->probe_timeout)) 6849 run_again(sdata, ifmgd->probe_timeout); 6850 else if (ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) { 6851 mlme_dbg(sdata, 6852 "Failed to send nullfunc to AP %pM after %dms, disconnecting\n", 6853 bssid, probe_wait_ms); 6854 ieee80211_sta_connection_lost(sdata, 6855 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false); 6856 } else if (ifmgd->probe_send_count < max_tries) { 6857 mlme_dbg(sdata, 6858 "No probe response from AP %pM after %dms, try %d/%i\n", 6859 bssid, probe_wait_ms, 6860 ifmgd->probe_send_count, max_tries); 6861 ieee80211_mgd_probe_ap_send(sdata); 6862 } else { 6863 /* 6864 * We actually lost the connection ... or did we? 6865 * Let's make sure! 6866 */ 6867 mlme_dbg(sdata, 6868 "No probe response from AP %pM after %dms, disconnecting.\n", 6869 bssid, probe_wait_ms); 6870 6871 ieee80211_sta_connection_lost(sdata, 6872 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, false); 6873 } 6874 } 6875 } 6876 6877 static void ieee80211_sta_bcn_mon_timer(struct timer_list *t) 6878 { 6879 struct ieee80211_sub_if_data *sdata = 6880 from_timer(sdata, t, u.mgd.bcn_mon_timer); 6881 6882 if (WARN_ON(ieee80211_vif_is_mld(&sdata->vif))) 6883 return; 6884 6885 if (sdata->vif.bss_conf.csa_active && 6886 !sdata->deflink.u.mgd.csa_waiting_bcn) 6887 return; 6888 6889 if (sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER) 6890 return; 6891 6892 sdata->u.mgd.connection_loss = false; 6893 wiphy_work_queue(sdata->local->hw.wiphy, 6894 &sdata->u.mgd.beacon_connection_loss_work); 6895 } 6896 6897 static void ieee80211_sta_conn_mon_timer(struct timer_list *t) 6898 { 6899 struct ieee80211_sub_if_data *sdata = 6900 from_timer(sdata, t, u.mgd.conn_mon_timer); 6901 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 6902 struct ieee80211_local *local = sdata->local; 6903 struct sta_info *sta; 6904 unsigned long timeout; 6905 6906 if (WARN_ON(ieee80211_vif_is_mld(&sdata->vif))) 6907 return; 6908 6909 if (sdata->vif.bss_conf.csa_active && 6910 !sdata->deflink.u.mgd.csa_waiting_bcn) 6911 return; 6912 6913 sta = sta_info_get(sdata, sdata->vif.cfg.ap_addr); 6914 if (!sta) 6915 return; 6916 6917 timeout = sta->deflink.status_stats.last_ack; 6918 if (time_before(sta->deflink.status_stats.last_ack, sta->deflink.rx_stats.last_rx)) 6919 timeout = sta->deflink.rx_stats.last_rx; 6920 timeout += IEEE80211_CONNECTION_IDLE_TIME; 6921 6922 /* If timeout is after now, then update timer to fire at 6923 * the later date, but do not actually probe at this time. 6924 */ 6925 if (time_is_after_jiffies(timeout)) { 6926 mod_timer(&ifmgd->conn_mon_timer, round_jiffies_up(timeout)); 6927 return; 6928 } 6929 6930 wiphy_work_queue(local->hw.wiphy, &sdata->u.mgd.monitor_work); 6931 } 6932 6933 static void ieee80211_sta_monitor_work(struct wiphy *wiphy, 6934 struct wiphy_work *work) 6935 { 6936 struct ieee80211_sub_if_data *sdata = 6937 container_of(work, struct ieee80211_sub_if_data, 6938 u.mgd.monitor_work); 6939 6940 ieee80211_mgd_probe_ap(sdata, false); 6941 } 6942 6943 static void ieee80211_restart_sta_timer(struct ieee80211_sub_if_data *sdata) 6944 { 6945 if (sdata->vif.type == NL80211_IFTYPE_STATION) { 6946 __ieee80211_stop_poll(sdata); 6947 6948 /* let's probe the connection once */ 6949 if (!ieee80211_hw_check(&sdata->local->hw, CONNECTION_MONITOR)) 6950 wiphy_work_queue(sdata->local->hw.wiphy, 6951 &sdata->u.mgd.monitor_work); 6952 } 6953 } 6954 6955 #ifdef CONFIG_PM 6956 void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata) 6957 { 6958 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 6959 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN]; 6960 6961 lockdep_assert_wiphy(sdata->local->hw.wiphy); 6962 6963 if (ifmgd->auth_data || ifmgd->assoc_data) { 6964 const u8 *ap_addr = ifmgd->auth_data ? 6965 ifmgd->auth_data->ap_addr : 6966 ifmgd->assoc_data->ap_addr; 6967 6968 /* 6969 * If we are trying to authenticate / associate while suspending, 6970 * cfg80211 won't know and won't actually abort those attempts, 6971 * thus we need to do that ourselves. 6972 */ 6973 ieee80211_send_deauth_disassoc(sdata, ap_addr, ap_addr, 6974 IEEE80211_STYPE_DEAUTH, 6975 WLAN_REASON_DEAUTH_LEAVING, 6976 false, frame_buf); 6977 if (ifmgd->assoc_data) 6978 ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON); 6979 if (ifmgd->auth_data) 6980 ieee80211_destroy_auth_data(sdata, false); 6981 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf, 6982 IEEE80211_DEAUTH_FRAME_LEN, 6983 false); 6984 } 6985 6986 /* This is a bit of a hack - we should find a better and more generic 6987 * solution to this. Normally when suspending, cfg80211 will in fact 6988 * deauthenticate. However, it doesn't (and cannot) stop an ongoing 6989 * auth (not so important) or assoc (this is the problem) process. 6990 * 6991 * As a consequence, it can happen that we are in the process of both 6992 * associating and suspending, and receive an association response 6993 * after cfg80211 has checked if it needs to disconnect, but before 6994 * we actually set the flag to drop incoming frames. This will then 6995 * cause the workqueue flush to process the association response in 6996 * the suspend, resulting in a successful association just before it 6997 * tries to remove the interface from the driver, which now though 6998 * has a channel context assigned ... this results in issues. 6999 * 7000 * To work around this (for now) simply deauth here again if we're 7001 * now connected. 7002 */ 7003 if (ifmgd->associated && !sdata->local->wowlan) { 7004 u8 bssid[ETH_ALEN]; 7005 struct cfg80211_deauth_request req = { 7006 .reason_code = WLAN_REASON_DEAUTH_LEAVING, 7007 .bssid = bssid, 7008 }; 7009 7010 memcpy(bssid, sdata->vif.cfg.ap_addr, ETH_ALEN); 7011 ieee80211_mgd_deauth(sdata, &req); 7012 } 7013 } 7014 #endif 7015 7016 void ieee80211_sta_restart(struct ieee80211_sub_if_data *sdata) 7017 { 7018 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 7019 7020 lockdep_assert_wiphy(sdata->local->hw.wiphy); 7021 7022 if (!ifmgd->associated) 7023 return; 7024 7025 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_RESUME) { 7026 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_RESUME; 7027 mlme_dbg(sdata, "driver requested disconnect after resume\n"); 7028 ieee80211_sta_connection_lost(sdata, 7029 WLAN_REASON_UNSPECIFIED, 7030 true); 7031 return; 7032 } 7033 7034 if (sdata->flags & IEEE80211_SDATA_DISCONNECT_HW_RESTART) { 7035 sdata->flags &= ~IEEE80211_SDATA_DISCONNECT_HW_RESTART; 7036 mlme_dbg(sdata, "driver requested disconnect after hardware restart\n"); 7037 ieee80211_sta_connection_lost(sdata, 7038 WLAN_REASON_UNSPECIFIED, 7039 true); 7040 return; 7041 } 7042 } 7043 7044 static void ieee80211_request_smps_mgd_work(struct wiphy *wiphy, 7045 struct wiphy_work *work) 7046 { 7047 struct ieee80211_link_data *link = 7048 container_of(work, struct ieee80211_link_data, 7049 u.mgd.request_smps_work); 7050 7051 __ieee80211_request_smps_mgd(link->sdata, link, 7052 link->u.mgd.driver_smps_mode); 7053 } 7054 7055 /* interface setup */ 7056 void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata) 7057 { 7058 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 7059 7060 wiphy_work_init(&ifmgd->monitor_work, ieee80211_sta_monitor_work); 7061 wiphy_work_init(&ifmgd->beacon_connection_loss_work, 7062 ieee80211_beacon_connection_loss_work); 7063 wiphy_work_init(&ifmgd->csa_connection_drop_work, 7064 ieee80211_csa_connection_drop_work); 7065 wiphy_delayed_work_init(&ifmgd->tdls_peer_del_work, 7066 ieee80211_tdls_peer_del_work); 7067 wiphy_delayed_work_init(&ifmgd->ml_reconf_work, 7068 ieee80211_ml_reconf_work); 7069 timer_setup(&ifmgd->timer, ieee80211_sta_timer, 0); 7070 timer_setup(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 0); 7071 timer_setup(&ifmgd->conn_mon_timer, ieee80211_sta_conn_mon_timer, 0); 7072 wiphy_delayed_work_init(&ifmgd->tx_tspec_wk, 7073 ieee80211_sta_handle_tspec_ac_params_wk); 7074 wiphy_delayed_work_init(&ifmgd->ttlm_work, 7075 ieee80211_tid_to_link_map_work); 7076 7077 ifmgd->flags = 0; 7078 ifmgd->powersave = sdata->wdev.ps; 7079 ifmgd->uapsd_queues = sdata->local->hw.uapsd_queues; 7080 ifmgd->uapsd_max_sp_len = sdata->local->hw.uapsd_max_sp_len; 7081 /* Setup TDLS data */ 7082 spin_lock_init(&ifmgd->teardown_lock); 7083 ifmgd->teardown_skb = NULL; 7084 ifmgd->orig_teardown_skb = NULL; 7085 } 7086 7087 static void ieee80211_recalc_smps_work(struct wiphy *wiphy, 7088 struct wiphy_work *work) 7089 { 7090 struct ieee80211_link_data *link = 7091 container_of(work, struct ieee80211_link_data, 7092 u.mgd.recalc_smps); 7093 7094 ieee80211_recalc_smps(link->sdata, link); 7095 } 7096 7097 void ieee80211_mgd_setup_link(struct ieee80211_link_data *link) 7098 { 7099 struct ieee80211_sub_if_data *sdata = link->sdata; 7100 struct ieee80211_local *local = sdata->local; 7101 unsigned int link_id = link->link_id; 7102 7103 link->u.mgd.p2p_noa_index = -1; 7104 link->u.mgd.conn_flags = 0; 7105 link->conf->bssid = link->u.mgd.bssid; 7106 7107 wiphy_work_init(&link->u.mgd.request_smps_work, 7108 ieee80211_request_smps_mgd_work); 7109 wiphy_work_init(&link->u.mgd.recalc_smps, 7110 ieee80211_recalc_smps_work); 7111 if (local->hw.wiphy->features & NL80211_FEATURE_DYNAMIC_SMPS) 7112 link->u.mgd.req_smps = IEEE80211_SMPS_AUTOMATIC; 7113 else 7114 link->u.mgd.req_smps = IEEE80211_SMPS_OFF; 7115 7116 wiphy_delayed_work_init(&link->u.mgd.chswitch_work, 7117 ieee80211_chswitch_work); 7118 7119 if (sdata->u.mgd.assoc_data) 7120 ether_addr_copy(link->conf->addr, 7121 sdata->u.mgd.assoc_data->link[link_id].addr); 7122 else if (!is_valid_ether_addr(link->conf->addr)) 7123 eth_random_addr(link->conf->addr); 7124 } 7125 7126 /* scan finished notification */ 7127 void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local) 7128 { 7129 struct ieee80211_sub_if_data *sdata; 7130 7131 /* Restart STA timers */ 7132 rcu_read_lock(); 7133 list_for_each_entry_rcu(sdata, &local->interfaces, list) { 7134 if (ieee80211_sdata_running(sdata)) 7135 ieee80211_restart_sta_timer(sdata); 7136 } 7137 rcu_read_unlock(); 7138 } 7139 7140 static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata, 7141 struct cfg80211_bss *cbss, s8 link_id, 7142 const u8 *ap_mld_addr, bool assoc, 7143 bool override) 7144 { 7145 struct ieee80211_local *local = sdata->local; 7146 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 7147 struct ieee80211_bss *bss = (void *)cbss->priv; 7148 struct sta_info *new_sta = NULL; 7149 struct ieee80211_link_data *link; 7150 bool have_sta = false; 7151 bool mlo; 7152 int err; 7153 7154 if (link_id >= 0) { 7155 mlo = true; 7156 if (WARN_ON(!ap_mld_addr)) 7157 return -EINVAL; 7158 err = ieee80211_vif_set_links(sdata, BIT(link_id), 0); 7159 } else { 7160 if (WARN_ON(ap_mld_addr)) 7161 return -EINVAL; 7162 ap_mld_addr = cbss->bssid; 7163 err = ieee80211_vif_set_links(sdata, 0, 0); 7164 link_id = 0; 7165 mlo = false; 7166 } 7167 7168 if (err) 7169 return err; 7170 7171 link = sdata_dereference(sdata->link[link_id], sdata); 7172 if (WARN_ON(!link)) { 7173 err = -ENOLINK; 7174 goto out_err; 7175 } 7176 7177 if (WARN_ON(!ifmgd->auth_data && !ifmgd->assoc_data)) { 7178 err = -EINVAL; 7179 goto out_err; 7180 } 7181 7182 /* If a reconfig is happening, bail out */ 7183 if (local->in_reconfig) { 7184 err = -EBUSY; 7185 goto out_err; 7186 } 7187 7188 if (assoc) { 7189 rcu_read_lock(); 7190 have_sta = sta_info_get(sdata, ap_mld_addr); 7191 rcu_read_unlock(); 7192 } 7193 7194 if (!have_sta) { 7195 if (mlo) 7196 new_sta = sta_info_alloc_with_link(sdata, ap_mld_addr, 7197 link_id, cbss->bssid, 7198 GFP_KERNEL); 7199 else 7200 new_sta = sta_info_alloc(sdata, ap_mld_addr, GFP_KERNEL); 7201 7202 if (!new_sta) { 7203 err = -ENOMEM; 7204 goto out_err; 7205 } 7206 7207 new_sta->sta.mlo = mlo; 7208 } 7209 7210 /* 7211 * Set up the information for the new channel before setting the 7212 * new channel. We can't - completely race-free - change the basic 7213 * rates bitmap and the channel (sband) that it refers to, but if 7214 * we set it up before we at least avoid calling into the driver's 7215 * bss_info_changed() method with invalid information (since we do 7216 * call that from changing the channel - only for IDLE and perhaps 7217 * some others, but ...). 7218 * 7219 * So to avoid that, just set up all the new information before the 7220 * channel, but tell the driver to apply it only afterwards, since 7221 * it might need the new channel for that. 7222 */ 7223 if (new_sta) { 7224 const struct cfg80211_bss_ies *ies; 7225 struct link_sta_info *link_sta; 7226 7227 rcu_read_lock(); 7228 link_sta = rcu_dereference(new_sta->link[link_id]); 7229 if (WARN_ON(!link_sta)) { 7230 rcu_read_unlock(); 7231 sta_info_free(local, new_sta); 7232 err = -EINVAL; 7233 goto out_err; 7234 } 7235 7236 err = ieee80211_mgd_setup_link_sta(link, new_sta, 7237 link_sta, cbss); 7238 if (err) { 7239 rcu_read_unlock(); 7240 sta_info_free(local, new_sta); 7241 goto out_err; 7242 } 7243 7244 memcpy(link->u.mgd.bssid, cbss->bssid, ETH_ALEN); 7245 7246 /* set timing information */ 7247 link->conf->beacon_int = cbss->beacon_interval; 7248 ies = rcu_dereference(cbss->beacon_ies); 7249 if (ies) { 7250 link->conf->sync_tsf = ies->tsf; 7251 link->conf->sync_device_ts = 7252 bss->device_ts_beacon; 7253 7254 ieee80211_get_dtim(ies, 7255 &link->conf->sync_dtim_count, 7256 NULL); 7257 } else if (!ieee80211_hw_check(&sdata->local->hw, 7258 TIMING_BEACON_ONLY)) { 7259 ies = rcu_dereference(cbss->proberesp_ies); 7260 /* must be non-NULL since beacon IEs were NULL */ 7261 link->conf->sync_tsf = ies->tsf; 7262 link->conf->sync_device_ts = 7263 bss->device_ts_presp; 7264 link->conf->sync_dtim_count = 0; 7265 } else { 7266 link->conf->sync_tsf = 0; 7267 link->conf->sync_device_ts = 0; 7268 link->conf->sync_dtim_count = 0; 7269 } 7270 rcu_read_unlock(); 7271 } 7272 7273 if (new_sta || override) { 7274 err = ieee80211_prep_channel(sdata, link, cbss, mlo, 7275 &link->u.mgd.conn_flags); 7276 if (err) { 7277 if (new_sta) 7278 sta_info_free(local, new_sta); 7279 goto out_err; 7280 } 7281 } 7282 7283 if (new_sta) { 7284 /* 7285 * tell driver about BSSID, basic rates and timing 7286 * this was set up above, before setting the channel 7287 */ 7288 ieee80211_link_info_change_notify(sdata, link, 7289 BSS_CHANGED_BSSID | 7290 BSS_CHANGED_BASIC_RATES | 7291 BSS_CHANGED_BEACON_INT); 7292 7293 if (assoc) 7294 sta_info_pre_move_state(new_sta, IEEE80211_STA_AUTH); 7295 7296 err = sta_info_insert(new_sta); 7297 new_sta = NULL; 7298 if (err) { 7299 sdata_info(sdata, 7300 "failed to insert STA entry for the AP (error %d)\n", 7301 err); 7302 goto out_err; 7303 } 7304 } else 7305 WARN_ON_ONCE(!ether_addr_equal(link->u.mgd.bssid, cbss->bssid)); 7306 7307 /* Cancel scan to ensure that nothing interferes with connection */ 7308 if (local->scanning) 7309 ieee80211_scan_cancel(local); 7310 7311 return 0; 7312 7313 out_err: 7314 ieee80211_link_release_channel(&sdata->deflink); 7315 ieee80211_vif_set_links(sdata, 0, 0); 7316 return err; 7317 } 7318 7319 /* config hooks */ 7320 int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata, 7321 struct cfg80211_auth_request *req) 7322 { 7323 struct ieee80211_local *local = sdata->local; 7324 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 7325 struct ieee80211_mgd_auth_data *auth_data; 7326 struct ieee80211_link_data *link; 7327 const struct element *csa_elem, *ecsa_elem; 7328 u16 auth_alg; 7329 int err; 7330 bool cont_auth; 7331 7332 lockdep_assert_wiphy(sdata->local->hw.wiphy); 7333 7334 /* prepare auth data structure */ 7335 7336 switch (req->auth_type) { 7337 case NL80211_AUTHTYPE_OPEN_SYSTEM: 7338 auth_alg = WLAN_AUTH_OPEN; 7339 break; 7340 case NL80211_AUTHTYPE_SHARED_KEY: 7341 if (fips_enabled) 7342 return -EOPNOTSUPP; 7343 auth_alg = WLAN_AUTH_SHARED_KEY; 7344 break; 7345 case NL80211_AUTHTYPE_FT: 7346 auth_alg = WLAN_AUTH_FT; 7347 break; 7348 case NL80211_AUTHTYPE_NETWORK_EAP: 7349 auth_alg = WLAN_AUTH_LEAP; 7350 break; 7351 case NL80211_AUTHTYPE_SAE: 7352 auth_alg = WLAN_AUTH_SAE; 7353 break; 7354 case NL80211_AUTHTYPE_FILS_SK: 7355 auth_alg = WLAN_AUTH_FILS_SK; 7356 break; 7357 case NL80211_AUTHTYPE_FILS_SK_PFS: 7358 auth_alg = WLAN_AUTH_FILS_SK_PFS; 7359 break; 7360 case NL80211_AUTHTYPE_FILS_PK: 7361 auth_alg = WLAN_AUTH_FILS_PK; 7362 break; 7363 default: 7364 return -EOPNOTSUPP; 7365 } 7366 7367 if (ifmgd->assoc_data) 7368 return -EBUSY; 7369 7370 rcu_read_lock(); 7371 csa_elem = ieee80211_bss_get_elem(req->bss, WLAN_EID_CHANNEL_SWITCH); 7372 ecsa_elem = ieee80211_bss_get_elem(req->bss, 7373 WLAN_EID_EXT_CHANSWITCH_ANN); 7374 if ((csa_elem && 7375 csa_elem->datalen == sizeof(struct ieee80211_channel_sw_ie) && 7376 ((struct ieee80211_channel_sw_ie *)csa_elem->data)->count != 0) || 7377 (ecsa_elem && 7378 ecsa_elem->datalen == sizeof(struct ieee80211_ext_chansw_ie) && 7379 ((struct ieee80211_ext_chansw_ie *)ecsa_elem->data)->count != 0)) { 7380 rcu_read_unlock(); 7381 sdata_info(sdata, "AP is in CSA process, reject auth\n"); 7382 return -EINVAL; 7383 } 7384 rcu_read_unlock(); 7385 7386 auth_data = kzalloc(sizeof(*auth_data) + req->auth_data_len + 7387 req->ie_len, GFP_KERNEL); 7388 if (!auth_data) 7389 return -ENOMEM; 7390 7391 memcpy(auth_data->ap_addr, 7392 req->ap_mld_addr ?: req->bss->bssid, 7393 ETH_ALEN); 7394 auth_data->bss = req->bss; 7395 auth_data->link_id = req->link_id; 7396 7397 if (req->auth_data_len >= 4) { 7398 if (req->auth_type == NL80211_AUTHTYPE_SAE) { 7399 __le16 *pos = (__le16 *) req->auth_data; 7400 7401 auth_data->sae_trans = le16_to_cpu(pos[0]); 7402 auth_data->sae_status = le16_to_cpu(pos[1]); 7403 } 7404 memcpy(auth_data->data, req->auth_data + 4, 7405 req->auth_data_len - 4); 7406 auth_data->data_len += req->auth_data_len - 4; 7407 } 7408 7409 /* Check if continuing authentication or trying to authenticate with the 7410 * same BSS that we were in the process of authenticating with and avoid 7411 * removal and re-addition of the STA entry in 7412 * ieee80211_prep_connection(). 7413 */ 7414 cont_auth = ifmgd->auth_data && req->bss == ifmgd->auth_data->bss && 7415 ifmgd->auth_data->link_id == req->link_id; 7416 7417 if (req->ie && req->ie_len) { 7418 memcpy(&auth_data->data[auth_data->data_len], 7419 req->ie, req->ie_len); 7420 auth_data->data_len += req->ie_len; 7421 } 7422 7423 if (req->key && req->key_len) { 7424 auth_data->key_len = req->key_len; 7425 auth_data->key_idx = req->key_idx; 7426 memcpy(auth_data->key, req->key, req->key_len); 7427 } 7428 7429 auth_data->algorithm = auth_alg; 7430 7431 /* try to authenticate/probe */ 7432 7433 if (ifmgd->auth_data) { 7434 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE) { 7435 auth_data->peer_confirmed = 7436 ifmgd->auth_data->peer_confirmed; 7437 } 7438 ieee80211_destroy_auth_data(sdata, cont_auth); 7439 } 7440 7441 /* prep auth_data so we don't go into idle on disassoc */ 7442 ifmgd->auth_data = auth_data; 7443 7444 /* If this is continuation of an ongoing SAE authentication exchange 7445 * (i.e., request to send SAE Confirm) and the peer has already 7446 * confirmed, mark authentication completed since we are about to send 7447 * out SAE Confirm. 7448 */ 7449 if (cont_auth && req->auth_type == NL80211_AUTHTYPE_SAE && 7450 auth_data->peer_confirmed && auth_data->sae_trans == 2) 7451 ieee80211_mark_sta_auth(sdata); 7452 7453 if (ifmgd->associated) { 7454 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN]; 7455 7456 sdata_info(sdata, 7457 "disconnect from AP %pM for new auth to %pM\n", 7458 sdata->vif.cfg.ap_addr, auth_data->ap_addr); 7459 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, 7460 WLAN_REASON_UNSPECIFIED, 7461 false, frame_buf); 7462 7463 ieee80211_report_disconnect(sdata, frame_buf, 7464 sizeof(frame_buf), true, 7465 WLAN_REASON_UNSPECIFIED, 7466 false); 7467 } 7468 7469 /* needed for transmitting the auth frame(s) properly */ 7470 memcpy(sdata->vif.cfg.ap_addr, auth_data->ap_addr, ETH_ALEN); 7471 7472 err = ieee80211_prep_connection(sdata, req->bss, req->link_id, 7473 req->ap_mld_addr, cont_auth, false); 7474 if (err) 7475 goto err_clear; 7476 7477 if (req->link_id > 0) 7478 link = sdata_dereference(sdata->link[req->link_id], sdata); 7479 else 7480 link = sdata_dereference(sdata->link[0], sdata); 7481 7482 if (WARN_ON(!link)) { 7483 err = -ENOLINK; 7484 goto err_clear; 7485 } 7486 7487 sdata_info(sdata, "authenticate with %pM (local address=%pM)\n", 7488 auth_data->ap_addr, link->conf->addr); 7489 7490 err = ieee80211_auth(sdata); 7491 if (err) { 7492 sta_info_destroy_addr(sdata, auth_data->ap_addr); 7493 goto err_clear; 7494 } 7495 7496 /* hold our own reference */ 7497 cfg80211_ref_bss(local->hw.wiphy, auth_data->bss); 7498 return 0; 7499 7500 err_clear: 7501 if (!ieee80211_vif_is_mld(&sdata->vif)) { 7502 eth_zero_addr(sdata->deflink.u.mgd.bssid); 7503 ieee80211_link_info_change_notify(sdata, &sdata->deflink, 7504 BSS_CHANGED_BSSID); 7505 ieee80211_link_release_channel(&sdata->deflink); 7506 } 7507 ifmgd->auth_data = NULL; 7508 kfree(auth_data); 7509 return err; 7510 } 7511 7512 static ieee80211_conn_flags_t 7513 ieee80211_setup_assoc_link(struct ieee80211_sub_if_data *sdata, 7514 struct ieee80211_mgd_assoc_data *assoc_data, 7515 struct cfg80211_assoc_request *req, 7516 ieee80211_conn_flags_t conn_flags, 7517 unsigned int link_id) 7518 { 7519 struct ieee80211_local *local = sdata->local; 7520 const struct cfg80211_bss_ies *bss_ies; 7521 struct ieee80211_supported_band *sband; 7522 const struct element *ht_elem, *vht_elem; 7523 struct ieee80211_link_data *link; 7524 struct cfg80211_bss *cbss; 7525 struct ieee80211_bss *bss; 7526 bool is_5ghz, is_6ghz; 7527 7528 cbss = assoc_data->link[link_id].bss; 7529 if (WARN_ON(!cbss)) 7530 return 0; 7531 7532 bss = (void *)cbss->priv; 7533 7534 sband = local->hw.wiphy->bands[cbss->channel->band]; 7535 if (WARN_ON(!sband)) 7536 return 0; 7537 7538 link = sdata_dereference(sdata->link[link_id], sdata); 7539 if (WARN_ON(!link)) 7540 return 0; 7541 7542 is_5ghz = cbss->channel->band == NL80211_BAND_5GHZ; 7543 is_6ghz = cbss->channel->band == NL80211_BAND_6GHZ; 7544 7545 /* for MLO connections assume advertising all rates is OK */ 7546 if (!req->ap_mld_addr) { 7547 assoc_data->supp_rates = bss->supp_rates; 7548 assoc_data->supp_rates_len = bss->supp_rates_len; 7549 } 7550 7551 /* copy and link elems for the STA profile */ 7552 if (req->links[link_id].elems_len) { 7553 memcpy(assoc_data->ie_pos, req->links[link_id].elems, 7554 req->links[link_id].elems_len); 7555 assoc_data->link[link_id].elems = assoc_data->ie_pos; 7556 assoc_data->link[link_id].elems_len = req->links[link_id].elems_len; 7557 assoc_data->ie_pos += req->links[link_id].elems_len; 7558 } 7559 7560 rcu_read_lock(); 7561 ht_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_HT_OPERATION); 7562 if (ht_elem && ht_elem->datalen >= sizeof(struct ieee80211_ht_operation)) 7563 assoc_data->link[link_id].ap_ht_param = 7564 ((struct ieee80211_ht_operation *)(ht_elem->data))->ht_param; 7565 else if (!is_6ghz) 7566 conn_flags |= IEEE80211_CONN_DISABLE_HT; 7567 vht_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_VHT_CAPABILITY); 7568 if (vht_elem && vht_elem->datalen >= sizeof(struct ieee80211_vht_cap)) { 7569 memcpy(&assoc_data->link[link_id].ap_vht_cap, vht_elem->data, 7570 sizeof(struct ieee80211_vht_cap)); 7571 } else if (is_5ghz) { 7572 link_info(link, 7573 "VHT capa missing/short, disabling VHT/HE/EHT\n"); 7574 conn_flags |= IEEE80211_CONN_DISABLE_VHT | 7575 IEEE80211_CONN_DISABLE_HE | 7576 IEEE80211_CONN_DISABLE_EHT; 7577 } 7578 rcu_read_unlock(); 7579 7580 link->u.mgd.beacon_crc_valid = false; 7581 link->u.mgd.dtim_period = 0; 7582 link->u.mgd.have_beacon = false; 7583 7584 /* override HT/VHT configuration only if the AP and we support it */ 7585 if (!(conn_flags & IEEE80211_CONN_DISABLE_HT)) { 7586 struct ieee80211_sta_ht_cap sta_ht_cap; 7587 7588 memcpy(&sta_ht_cap, &sband->ht_cap, sizeof(sta_ht_cap)); 7589 ieee80211_apply_htcap_overrides(sdata, &sta_ht_cap); 7590 } 7591 7592 link->conf->eht_puncturing = 0; 7593 7594 rcu_read_lock(); 7595 bss_ies = rcu_dereference(cbss->beacon_ies); 7596 if (bss_ies) { 7597 u8 dtim_count = 0; 7598 7599 ieee80211_get_dtim(bss_ies, &dtim_count, 7600 &link->u.mgd.dtim_period); 7601 7602 sdata->deflink.u.mgd.have_beacon = true; 7603 7604 if (ieee80211_hw_check(&local->hw, TIMING_BEACON_ONLY)) { 7605 link->conf->sync_tsf = bss_ies->tsf; 7606 link->conf->sync_device_ts = bss->device_ts_beacon; 7607 link->conf->sync_dtim_count = dtim_count; 7608 } 7609 } else { 7610 bss_ies = rcu_dereference(cbss->ies); 7611 } 7612 7613 if (bss_ies) { 7614 const struct ieee80211_eht_operation *eht_oper; 7615 const struct element *elem; 7616 7617 elem = cfg80211_find_ext_elem(WLAN_EID_EXT_MULTIPLE_BSSID_CONFIGURATION, 7618 bss_ies->data, bss_ies->len); 7619 if (elem && elem->datalen >= 3) 7620 link->conf->profile_periodicity = elem->data[2]; 7621 else 7622 link->conf->profile_periodicity = 0; 7623 7624 elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, 7625 bss_ies->data, bss_ies->len); 7626 if (elem && elem->datalen >= 11 && 7627 (elem->data[10] & WLAN_EXT_CAPA11_EMA_SUPPORT)) 7628 link->conf->ema_ap = true; 7629 else 7630 link->conf->ema_ap = false; 7631 7632 elem = cfg80211_find_ext_elem(WLAN_EID_EXT_EHT_OPERATION, 7633 bss_ies->data, bss_ies->len); 7634 eht_oper = (const void *)(elem->data + 1); 7635 7636 if (elem && 7637 ieee80211_eht_oper_size_ok((const void *)(elem->data + 1), 7638 elem->datalen - 1) && 7639 (eht_oper->params & IEEE80211_EHT_OPER_INFO_PRESENT) && 7640 (eht_oper->params & IEEE80211_EHT_OPER_DISABLED_SUBCHANNEL_BITMAP_PRESENT)) { 7641 const struct ieee80211_eht_operation_info *info = 7642 (void *)eht_oper->optional; 7643 const u8 *disable_subchannel_bitmap = info->optional; 7644 u16 bitmap; 7645 7646 bitmap = get_unaligned_le16(disable_subchannel_bitmap); 7647 if (cfg80211_valid_disable_subchannel_bitmap(&bitmap, 7648 &link->conf->chandef) && 7649 !(bitmap && ieee80211_hw_check(&local->hw, DISALLOW_PUNCTURING))) 7650 ieee80211_handle_puncturing_bitmap(link, 7651 eht_oper, 7652 bitmap, 7653 NULL); 7654 else 7655 conn_flags |= IEEE80211_CONN_DISABLE_EHT; 7656 } 7657 } 7658 rcu_read_unlock(); 7659 7660 if (bss->corrupt_data) { 7661 char *corrupt_type = "data"; 7662 7663 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_BEACON) { 7664 if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP) 7665 corrupt_type = "beacon and probe response"; 7666 else 7667 corrupt_type = "beacon"; 7668 } else if (bss->corrupt_data & IEEE80211_BSS_CORRUPT_PROBE_RESP) { 7669 corrupt_type = "probe response"; 7670 } 7671 sdata_info(sdata, "associating to AP %pM with corrupt %s\n", 7672 cbss->bssid, corrupt_type); 7673 } 7674 7675 if (link->u.mgd.req_smps == IEEE80211_SMPS_AUTOMATIC) { 7676 if (sdata->u.mgd.powersave) 7677 link->smps_mode = IEEE80211_SMPS_DYNAMIC; 7678 else 7679 link->smps_mode = IEEE80211_SMPS_OFF; 7680 } else { 7681 link->smps_mode = link->u.mgd.req_smps; 7682 } 7683 7684 return conn_flags; 7685 } 7686 7687 int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata, 7688 struct cfg80211_assoc_request *req) 7689 { 7690 unsigned int assoc_link_id = req->link_id < 0 ? 0 : req->link_id; 7691 struct ieee80211_local *local = sdata->local; 7692 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 7693 struct ieee80211_mgd_assoc_data *assoc_data; 7694 const struct element *ssid_elem, *csa_elem, *ecsa_elem; 7695 struct ieee80211_vif_cfg *vif_cfg = &sdata->vif.cfg; 7696 ieee80211_conn_flags_t conn_flags = 0; 7697 struct ieee80211_link_data *link; 7698 struct cfg80211_bss *cbss; 7699 struct ieee80211_bss *bss; 7700 bool override; 7701 int i, err; 7702 size_t size = sizeof(*assoc_data) + req->ie_len; 7703 7704 for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) 7705 size += req->links[i].elems_len; 7706 7707 /* FIXME: no support for 4-addr MLO yet */ 7708 if (sdata->u.mgd.use_4addr && req->link_id >= 0) 7709 return -EOPNOTSUPP; 7710 7711 assoc_data = kzalloc(size, GFP_KERNEL); 7712 if (!assoc_data) 7713 return -ENOMEM; 7714 7715 cbss = req->link_id < 0 ? req->bss : req->links[req->link_id].bss; 7716 7717 rcu_read_lock(); 7718 ssid_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_SSID); 7719 if (!ssid_elem || ssid_elem->datalen > sizeof(assoc_data->ssid)) { 7720 rcu_read_unlock(); 7721 kfree(assoc_data); 7722 return -EINVAL; 7723 } 7724 7725 csa_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_CHANNEL_SWITCH); 7726 ecsa_elem = ieee80211_bss_get_elem(cbss, WLAN_EID_EXT_CHANSWITCH_ANN); 7727 if ((csa_elem && 7728 csa_elem->datalen == sizeof(struct ieee80211_channel_sw_ie) && 7729 ((struct ieee80211_channel_sw_ie *)csa_elem->data)->count != 0) || 7730 (ecsa_elem && 7731 ecsa_elem->datalen == sizeof(struct ieee80211_ext_chansw_ie) && 7732 ((struct ieee80211_ext_chansw_ie *)ecsa_elem->data)->count != 0)) { 7733 sdata_info(sdata, "AP is in CSA process, reject assoc\n"); 7734 rcu_read_unlock(); 7735 kfree(assoc_data); 7736 return -EINVAL; 7737 } 7738 7739 memcpy(assoc_data->ssid, ssid_elem->data, ssid_elem->datalen); 7740 assoc_data->ssid_len = ssid_elem->datalen; 7741 memcpy(vif_cfg->ssid, assoc_data->ssid, assoc_data->ssid_len); 7742 vif_cfg->ssid_len = assoc_data->ssid_len; 7743 rcu_read_unlock(); 7744 7745 if (req->ap_mld_addr) { 7746 for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) { 7747 if (!req->links[i].bss) 7748 continue; 7749 link = sdata_dereference(sdata->link[i], sdata); 7750 if (link) 7751 ether_addr_copy(assoc_data->link[i].addr, 7752 link->conf->addr); 7753 else 7754 eth_random_addr(assoc_data->link[i].addr); 7755 } 7756 } else { 7757 memcpy(assoc_data->link[0].addr, sdata->vif.addr, ETH_ALEN); 7758 } 7759 7760 assoc_data->s1g = cbss->channel->band == NL80211_BAND_S1GHZ; 7761 7762 memcpy(assoc_data->ap_addr, 7763 req->ap_mld_addr ?: req->bss->bssid, 7764 ETH_ALEN); 7765 7766 if (ifmgd->associated) { 7767 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN]; 7768 7769 sdata_info(sdata, 7770 "disconnect from AP %pM for new assoc to %pM\n", 7771 sdata->vif.cfg.ap_addr, assoc_data->ap_addr); 7772 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, 7773 WLAN_REASON_UNSPECIFIED, 7774 false, frame_buf); 7775 7776 ieee80211_report_disconnect(sdata, frame_buf, 7777 sizeof(frame_buf), true, 7778 WLAN_REASON_UNSPECIFIED, 7779 false); 7780 } 7781 7782 if (ifmgd->auth_data && !ifmgd->auth_data->done) { 7783 err = -EBUSY; 7784 goto err_free; 7785 } 7786 7787 if (ifmgd->assoc_data) { 7788 err = -EBUSY; 7789 goto err_free; 7790 } 7791 7792 if (ifmgd->auth_data) { 7793 bool match; 7794 7795 /* keep sta info, bssid if matching */ 7796 match = ether_addr_equal(ifmgd->auth_data->ap_addr, 7797 assoc_data->ap_addr) && 7798 ifmgd->auth_data->link_id == req->link_id; 7799 7800 /* Cleanup is delayed if auth_data matches */ 7801 if (!match) 7802 ieee80211_destroy_auth_data(sdata, false); 7803 } 7804 7805 /* prepare assoc data */ 7806 7807 bss = (void *)cbss->priv; 7808 assoc_data->wmm = bss->wmm_used && 7809 (local->hw.queues >= IEEE80211_NUM_ACS); 7810 7811 /* 7812 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode. 7813 * We still associate in non-HT mode (11a/b/g) if any one of these 7814 * ciphers is configured as pairwise. 7815 * We can set this to true for non-11n hardware, that'll be checked 7816 * separately along with the peer capabilities. 7817 */ 7818 for (i = 0; i < req->crypto.n_ciphers_pairwise; i++) { 7819 if (req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP40 || 7820 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_TKIP || 7821 req->crypto.ciphers_pairwise[i] == WLAN_CIPHER_SUITE_WEP104) { 7822 conn_flags |= IEEE80211_CONN_DISABLE_HT; 7823 conn_flags |= IEEE80211_CONN_DISABLE_VHT; 7824 conn_flags |= IEEE80211_CONN_DISABLE_HE; 7825 conn_flags |= IEEE80211_CONN_DISABLE_EHT; 7826 netdev_info(sdata->dev, 7827 "disabling HT/VHT/HE due to WEP/TKIP use\n"); 7828 } 7829 } 7830 7831 /* also disable HT/VHT/HE/EHT if the AP doesn't use WMM */ 7832 if (!bss->wmm_used) { 7833 conn_flags |= IEEE80211_CONN_DISABLE_HT; 7834 conn_flags |= IEEE80211_CONN_DISABLE_VHT; 7835 conn_flags |= IEEE80211_CONN_DISABLE_HE; 7836 conn_flags |= IEEE80211_CONN_DISABLE_EHT; 7837 netdev_info(sdata->dev, 7838 "disabling HT/VHT/HE as WMM/QoS is not supported by the AP\n"); 7839 } 7840 7841 if (req->flags & ASSOC_REQ_DISABLE_HT) { 7842 mlme_dbg(sdata, "HT disabled by flag, disabling HT/VHT/HE\n"); 7843 conn_flags |= IEEE80211_CONN_DISABLE_HT; 7844 conn_flags |= IEEE80211_CONN_DISABLE_VHT; 7845 conn_flags |= IEEE80211_CONN_DISABLE_HE; 7846 conn_flags |= IEEE80211_CONN_DISABLE_EHT; 7847 } 7848 7849 if (req->flags & ASSOC_REQ_DISABLE_VHT) { 7850 mlme_dbg(sdata, "VHT disabled by flag, disabling VHT\n"); 7851 conn_flags |= IEEE80211_CONN_DISABLE_VHT; 7852 } 7853 7854 if (req->flags & ASSOC_REQ_DISABLE_HE) { 7855 mlme_dbg(sdata, "HE disabled by flag, disabling HE/EHT\n"); 7856 conn_flags |= IEEE80211_CONN_DISABLE_HE; 7857 conn_flags |= IEEE80211_CONN_DISABLE_EHT; 7858 } 7859 7860 if (req->flags & ASSOC_REQ_DISABLE_EHT) 7861 conn_flags |= IEEE80211_CONN_DISABLE_EHT; 7862 7863 memcpy(&ifmgd->ht_capa, &req->ht_capa, sizeof(ifmgd->ht_capa)); 7864 memcpy(&ifmgd->ht_capa_mask, &req->ht_capa_mask, 7865 sizeof(ifmgd->ht_capa_mask)); 7866 7867 memcpy(&ifmgd->vht_capa, &req->vht_capa, sizeof(ifmgd->vht_capa)); 7868 memcpy(&ifmgd->vht_capa_mask, &req->vht_capa_mask, 7869 sizeof(ifmgd->vht_capa_mask)); 7870 7871 memcpy(&ifmgd->s1g_capa, &req->s1g_capa, sizeof(ifmgd->s1g_capa)); 7872 memcpy(&ifmgd->s1g_capa_mask, &req->s1g_capa_mask, 7873 sizeof(ifmgd->s1g_capa_mask)); 7874 7875 if (req->ie && req->ie_len) { 7876 memcpy(assoc_data->ie, req->ie, req->ie_len); 7877 assoc_data->ie_len = req->ie_len; 7878 assoc_data->ie_pos = assoc_data->ie + assoc_data->ie_len; 7879 } else { 7880 assoc_data->ie_pos = assoc_data->ie; 7881 } 7882 7883 if (req->fils_kek) { 7884 /* should already be checked in cfg80211 - so warn */ 7885 if (WARN_ON(req->fils_kek_len > FILS_MAX_KEK_LEN)) { 7886 err = -EINVAL; 7887 goto err_free; 7888 } 7889 memcpy(assoc_data->fils_kek, req->fils_kek, 7890 req->fils_kek_len); 7891 assoc_data->fils_kek_len = req->fils_kek_len; 7892 } 7893 7894 if (req->fils_nonces) 7895 memcpy(assoc_data->fils_nonces, req->fils_nonces, 7896 2 * FILS_NONCE_LEN); 7897 7898 /* default timeout */ 7899 assoc_data->timeout = jiffies; 7900 assoc_data->timeout_started = true; 7901 7902 assoc_data->assoc_link_id = assoc_link_id; 7903 7904 if (req->ap_mld_addr) { 7905 for (i = 0; i < ARRAY_SIZE(assoc_data->link); i++) { 7906 assoc_data->link[i].conn_flags = conn_flags; 7907 assoc_data->link[i].bss = req->links[i].bss; 7908 assoc_data->link[i].disabled = req->links[i].disabled; 7909 } 7910 7911 /* if there was no authentication, set up the link */ 7912 err = ieee80211_vif_set_links(sdata, BIT(assoc_link_id), 0); 7913 if (err) 7914 goto err_clear; 7915 } else { 7916 assoc_data->link[0].conn_flags = conn_flags; 7917 assoc_data->link[0].bss = cbss; 7918 } 7919 7920 link = sdata_dereference(sdata->link[assoc_link_id], sdata); 7921 if (WARN_ON(!link)) { 7922 err = -EINVAL; 7923 goto err_clear; 7924 } 7925 7926 /* keep old conn_flags from ieee80211_prep_channel() from auth */ 7927 conn_flags |= link->u.mgd.conn_flags; 7928 conn_flags |= ieee80211_setup_assoc_link(sdata, assoc_data, req, 7929 conn_flags, assoc_link_id); 7930 override = link->u.mgd.conn_flags != conn_flags; 7931 link->u.mgd.conn_flags |= conn_flags; 7932 7933 if (WARN((sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD) && 7934 ieee80211_hw_check(&local->hw, PS_NULLFUNC_STACK), 7935 "U-APSD not supported with HW_PS_NULLFUNC_STACK\n")) 7936 sdata->vif.driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD; 7937 7938 if (bss->wmm_used && bss->uapsd_supported && 7939 (sdata->vif.driver_flags & IEEE80211_VIF_SUPPORTS_UAPSD)) { 7940 assoc_data->uapsd = true; 7941 ifmgd->flags |= IEEE80211_STA_UAPSD_ENABLED; 7942 } else { 7943 assoc_data->uapsd = false; 7944 ifmgd->flags &= ~IEEE80211_STA_UAPSD_ENABLED; 7945 } 7946 7947 if (req->prev_bssid) 7948 memcpy(assoc_data->prev_ap_addr, req->prev_bssid, ETH_ALEN); 7949 7950 if (req->use_mfp) { 7951 ifmgd->mfp = IEEE80211_MFP_REQUIRED; 7952 ifmgd->flags |= IEEE80211_STA_MFP_ENABLED; 7953 } else { 7954 ifmgd->mfp = IEEE80211_MFP_DISABLED; 7955 ifmgd->flags &= ~IEEE80211_STA_MFP_ENABLED; 7956 } 7957 7958 if (req->flags & ASSOC_REQ_USE_RRM) 7959 ifmgd->flags |= IEEE80211_STA_ENABLE_RRM; 7960 else 7961 ifmgd->flags &= ~IEEE80211_STA_ENABLE_RRM; 7962 7963 if (req->crypto.control_port) 7964 ifmgd->flags |= IEEE80211_STA_CONTROL_PORT; 7965 else 7966 ifmgd->flags &= ~IEEE80211_STA_CONTROL_PORT; 7967 7968 sdata->control_port_protocol = req->crypto.control_port_ethertype; 7969 sdata->control_port_no_encrypt = req->crypto.control_port_no_encrypt; 7970 sdata->control_port_over_nl80211 = 7971 req->crypto.control_port_over_nl80211; 7972 sdata->control_port_no_preauth = req->crypto.control_port_no_preauth; 7973 7974 /* kick off associate process */ 7975 ifmgd->assoc_data = assoc_data; 7976 7977 for (i = 0; i < ARRAY_SIZE(assoc_data->link); i++) { 7978 if (!assoc_data->link[i].bss) 7979 continue; 7980 if (i == assoc_data->assoc_link_id) 7981 continue; 7982 /* only calculate the flags, hence link == NULL */ 7983 err = ieee80211_prep_channel(sdata, NULL, 7984 assoc_data->link[i].bss, true, 7985 &assoc_data->link[i].conn_flags); 7986 if (err) { 7987 req->links[i].error = err; 7988 goto err_clear; 7989 } 7990 } 7991 7992 /* needed for transmitting the assoc frames properly */ 7993 memcpy(sdata->vif.cfg.ap_addr, assoc_data->ap_addr, ETH_ALEN); 7994 7995 err = ieee80211_prep_connection(sdata, cbss, req->link_id, 7996 req->ap_mld_addr, true, override); 7997 if (err) 7998 goto err_clear; 7999 8000 assoc_data->link[assoc_data->assoc_link_id].conn_flags = 8001 link->u.mgd.conn_flags; 8002 8003 if (ieee80211_hw_check(&sdata->local->hw, NEED_DTIM_BEFORE_ASSOC)) { 8004 const struct cfg80211_bss_ies *beacon_ies; 8005 8006 rcu_read_lock(); 8007 beacon_ies = rcu_dereference(req->bss->beacon_ies); 8008 8009 if (beacon_ies) { 8010 /* 8011 * Wait up to one beacon interval ... 8012 * should this be more if we miss one? 8013 */ 8014 sdata_info(sdata, "waiting for beacon from %pM\n", 8015 link->u.mgd.bssid); 8016 assoc_data->timeout = TU_TO_EXP_TIME(req->bss->beacon_interval); 8017 assoc_data->timeout_started = true; 8018 assoc_data->need_beacon = true; 8019 } 8020 rcu_read_unlock(); 8021 } 8022 8023 run_again(sdata, assoc_data->timeout); 8024 8025 /* We are associating, clean up auth_data */ 8026 if (ifmgd->auth_data) 8027 ieee80211_destroy_auth_data(sdata, true); 8028 8029 return 0; 8030 err_clear: 8031 if (!ifmgd->auth_data) { 8032 eth_zero_addr(sdata->deflink.u.mgd.bssid); 8033 ieee80211_link_info_change_notify(sdata, &sdata->deflink, 8034 BSS_CHANGED_BSSID); 8035 } 8036 ifmgd->assoc_data = NULL; 8037 err_free: 8038 kfree(assoc_data); 8039 return err; 8040 } 8041 8042 int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata, 8043 struct cfg80211_deauth_request *req) 8044 { 8045 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 8046 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN]; 8047 bool tx = !req->local_state_change; 8048 struct ieee80211_prep_tx_info info = { 8049 .subtype = IEEE80211_STYPE_DEAUTH, 8050 }; 8051 8052 if (ifmgd->auth_data && 8053 ether_addr_equal(ifmgd->auth_data->ap_addr, req->bssid)) { 8054 sdata_info(sdata, 8055 "aborting authentication with %pM by local choice (Reason: %u=%s)\n", 8056 req->bssid, req->reason_code, 8057 ieee80211_get_reason_code_string(req->reason_code)); 8058 8059 info.link_id = ifmgd->auth_data->link_id; 8060 drv_mgd_prepare_tx(sdata->local, sdata, &info); 8061 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid, 8062 IEEE80211_STYPE_DEAUTH, 8063 req->reason_code, tx, 8064 frame_buf); 8065 ieee80211_destroy_auth_data(sdata, false); 8066 ieee80211_report_disconnect(sdata, frame_buf, 8067 sizeof(frame_buf), true, 8068 req->reason_code, false); 8069 drv_mgd_complete_tx(sdata->local, sdata, &info); 8070 return 0; 8071 } 8072 8073 if (ifmgd->assoc_data && 8074 ether_addr_equal(ifmgd->assoc_data->ap_addr, req->bssid)) { 8075 sdata_info(sdata, 8076 "aborting association with %pM by local choice (Reason: %u=%s)\n", 8077 req->bssid, req->reason_code, 8078 ieee80211_get_reason_code_string(req->reason_code)); 8079 8080 info.link_id = ifmgd->assoc_data->assoc_link_id; 8081 drv_mgd_prepare_tx(sdata->local, sdata, &info); 8082 ieee80211_send_deauth_disassoc(sdata, req->bssid, req->bssid, 8083 IEEE80211_STYPE_DEAUTH, 8084 req->reason_code, tx, 8085 frame_buf); 8086 ieee80211_destroy_assoc_data(sdata, ASSOC_ABANDON); 8087 ieee80211_report_disconnect(sdata, frame_buf, 8088 sizeof(frame_buf), true, 8089 req->reason_code, false); 8090 return 0; 8091 } 8092 8093 if (ifmgd->associated && 8094 ether_addr_equal(sdata->vif.cfg.ap_addr, req->bssid)) { 8095 sdata_info(sdata, 8096 "deauthenticating from %pM by local choice (Reason: %u=%s)\n", 8097 req->bssid, req->reason_code, 8098 ieee80211_get_reason_code_string(req->reason_code)); 8099 8100 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, 8101 req->reason_code, tx, frame_buf); 8102 ieee80211_report_disconnect(sdata, frame_buf, 8103 sizeof(frame_buf), true, 8104 req->reason_code, false); 8105 drv_mgd_complete_tx(sdata->local, sdata, &info); 8106 return 0; 8107 } 8108 8109 return -ENOTCONN; 8110 } 8111 8112 int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata, 8113 struct cfg80211_disassoc_request *req) 8114 { 8115 u8 frame_buf[IEEE80211_DEAUTH_FRAME_LEN]; 8116 8117 if (!sdata->u.mgd.associated || 8118 memcmp(sdata->vif.cfg.ap_addr, req->ap_addr, ETH_ALEN)) 8119 return -ENOTCONN; 8120 8121 sdata_info(sdata, 8122 "disassociating from %pM by local choice (Reason: %u=%s)\n", 8123 req->ap_addr, req->reason_code, 8124 ieee80211_get_reason_code_string(req->reason_code)); 8125 8126 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DISASSOC, 8127 req->reason_code, !req->local_state_change, 8128 frame_buf); 8129 8130 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true, 8131 req->reason_code, false); 8132 8133 return 0; 8134 } 8135 8136 void ieee80211_mgd_stop_link(struct ieee80211_link_data *link) 8137 { 8138 wiphy_work_cancel(link->sdata->local->hw.wiphy, 8139 &link->u.mgd.request_smps_work); 8140 wiphy_work_cancel(link->sdata->local->hw.wiphy, 8141 &link->u.mgd.recalc_smps); 8142 wiphy_delayed_work_cancel(link->sdata->local->hw.wiphy, 8143 &link->u.mgd.chswitch_work); 8144 } 8145 8146 void ieee80211_mgd_stop(struct ieee80211_sub_if_data *sdata) 8147 { 8148 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 8149 8150 /* 8151 * Make sure some work items will not run after this, 8152 * they will not do anything but might not have been 8153 * cancelled when disconnecting. 8154 */ 8155 wiphy_work_cancel(sdata->local->hw.wiphy, 8156 &ifmgd->monitor_work); 8157 wiphy_work_cancel(sdata->local->hw.wiphy, 8158 &ifmgd->beacon_connection_loss_work); 8159 wiphy_work_cancel(sdata->local->hw.wiphy, 8160 &ifmgd->csa_connection_drop_work); 8161 wiphy_delayed_work_cancel(sdata->local->hw.wiphy, 8162 &ifmgd->tdls_peer_del_work); 8163 wiphy_delayed_work_cancel(sdata->local->hw.wiphy, 8164 &ifmgd->ml_reconf_work); 8165 wiphy_delayed_work_cancel(sdata->local->hw.wiphy, &ifmgd->ttlm_work); 8166 8167 if (ifmgd->assoc_data) 8168 ieee80211_destroy_assoc_data(sdata, ASSOC_TIMEOUT); 8169 if (ifmgd->auth_data) 8170 ieee80211_destroy_auth_data(sdata, false); 8171 spin_lock_bh(&ifmgd->teardown_lock); 8172 if (ifmgd->teardown_skb) { 8173 kfree_skb(ifmgd->teardown_skb); 8174 ifmgd->teardown_skb = NULL; 8175 ifmgd->orig_teardown_skb = NULL; 8176 } 8177 kfree(ifmgd->assoc_req_ies); 8178 ifmgd->assoc_req_ies = NULL; 8179 ifmgd->assoc_req_ies_len = 0; 8180 spin_unlock_bh(&ifmgd->teardown_lock); 8181 del_timer_sync(&ifmgd->timer); 8182 } 8183 8184 void ieee80211_cqm_rssi_notify(struct ieee80211_vif *vif, 8185 enum nl80211_cqm_rssi_threshold_event rssi_event, 8186 s32 rssi_level, 8187 gfp_t gfp) 8188 { 8189 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 8190 8191 trace_api_cqm_rssi_notify(sdata, rssi_event, rssi_level); 8192 8193 cfg80211_cqm_rssi_notify(sdata->dev, rssi_event, rssi_level, gfp); 8194 } 8195 EXPORT_SYMBOL(ieee80211_cqm_rssi_notify); 8196 8197 void ieee80211_cqm_beacon_loss_notify(struct ieee80211_vif *vif, gfp_t gfp) 8198 { 8199 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 8200 8201 trace_api_cqm_beacon_loss_notify(sdata->local, sdata); 8202 8203 cfg80211_cqm_beacon_loss_notify(sdata->dev, gfp); 8204 } 8205 EXPORT_SYMBOL(ieee80211_cqm_beacon_loss_notify); 8206 8207 static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata, 8208 int rssi_min_thold, 8209 int rssi_max_thold) 8210 { 8211 trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold); 8212 8213 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION)) 8214 return; 8215 8216 /* 8217 * Scale up threshold values before storing it, as the RSSI averaging 8218 * algorithm uses a scaled up value as well. Change this scaling 8219 * factor if the RSSI averaging algorithm changes. 8220 */ 8221 sdata->u.mgd.rssi_min_thold = rssi_min_thold*16; 8222 sdata->u.mgd.rssi_max_thold = rssi_max_thold*16; 8223 } 8224 8225 void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif, 8226 int rssi_min_thold, 8227 int rssi_max_thold) 8228 { 8229 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 8230 8231 WARN_ON(rssi_min_thold == rssi_max_thold || 8232 rssi_min_thold > rssi_max_thold); 8233 8234 _ieee80211_enable_rssi_reports(sdata, rssi_min_thold, 8235 rssi_max_thold); 8236 } 8237 EXPORT_SYMBOL(ieee80211_enable_rssi_reports); 8238 8239 void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif) 8240 { 8241 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 8242 8243 _ieee80211_enable_rssi_reports(sdata, 0, 0); 8244 } 8245 EXPORT_SYMBOL(ieee80211_disable_rssi_reports); 8246