1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Scanning implementation 4 * 5 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi> 6 * Copyright 2004, Instant802 Networks, Inc. 7 * Copyright 2005, Devicescape Software, Inc. 8 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> 9 * Copyright 2007, Michael Wu <flamingice@sourmilk.net> 10 * Copyright 2013-2015 Intel Mobile Communications GmbH 11 * Copyright 2016-2017 Intel Deutschland GmbH 12 * Copyright (C) 2018-2024 Intel Corporation 13 */ 14 15 #include <linux/if_arp.h> 16 #include <linux/etherdevice.h> 17 #include <linux/rtnetlink.h> 18 #include <net/sch_generic.h> 19 #include <linux/slab.h> 20 #include <linux/export.h> 21 #include <linux/random.h> 22 #include <net/mac80211.h> 23 24 #include "ieee80211_i.h" 25 #include "driver-ops.h" 26 #include "mesh.h" 27 28 #define IEEE80211_PROBE_DELAY (HZ / 33) 29 #define IEEE80211_CHANNEL_TIME (HZ / 33) 30 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 9) 31 32 void ieee80211_rx_bss_put(struct ieee80211_local *local, 33 struct ieee80211_bss *bss) 34 { 35 if (!bss) 36 return; 37 cfg80211_put_bss(local->hw.wiphy, 38 container_of((void *)bss, struct cfg80211_bss, priv)); 39 } 40 41 static bool is_uapsd_supported(struct ieee802_11_elems *elems) 42 { 43 u8 qos_info; 44 45 if (elems->wmm_info && elems->wmm_info_len == 7 46 && elems->wmm_info[5] == 1) 47 qos_info = elems->wmm_info[6]; 48 else if (elems->wmm_param && elems->wmm_param_len == 24 49 && elems->wmm_param[5] == 1) 50 qos_info = elems->wmm_param[6]; 51 else 52 /* no valid wmm information or parameter element found */ 53 return false; 54 55 return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD; 56 } 57 58 struct inform_bss_update_data { 59 struct ieee80211_rx_status *rx_status; 60 bool beacon; 61 }; 62 63 void ieee80211_inform_bss(struct wiphy *wiphy, 64 struct cfg80211_bss *cbss, 65 const struct cfg80211_bss_ies *ies, 66 void *data) 67 { 68 struct ieee80211_local *local = wiphy_priv(wiphy); 69 struct inform_bss_update_data *update_data = data; 70 struct ieee80211_bss *bss = (void *)cbss->priv; 71 struct ieee80211_rx_status *rx_status; 72 struct ieee802_11_elems *elems; 73 int clen, srlen; 74 75 /* This happens while joining an IBSS */ 76 if (!update_data) 77 return; 78 79 elems = ieee802_11_parse_elems(ies->data, ies->len, false, NULL); 80 if (!elems) 81 return; 82 83 rx_status = update_data->rx_status; 84 85 if (update_data->beacon) 86 bss->device_ts_beacon = rx_status->device_timestamp; 87 else 88 bss->device_ts_presp = rx_status->device_timestamp; 89 90 if (elems->parse_error) { 91 if (update_data->beacon) 92 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON; 93 else 94 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP; 95 } else { 96 if (update_data->beacon) 97 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON; 98 else 99 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP; 100 } 101 102 /* save the ERP value so that it is available at association time */ 103 if (elems->erp_info && (!elems->parse_error || 104 !(bss->valid_data & IEEE80211_BSS_VALID_ERP))) { 105 bss->erp_value = elems->erp_info[0]; 106 bss->has_erp_value = true; 107 if (!elems->parse_error) 108 bss->valid_data |= IEEE80211_BSS_VALID_ERP; 109 } 110 111 /* replace old supported rates if we get new values */ 112 if (!elems->parse_error || 113 !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) { 114 srlen = 0; 115 if (elems->supp_rates) { 116 clen = IEEE80211_MAX_SUPP_RATES; 117 if (clen > elems->supp_rates_len) 118 clen = elems->supp_rates_len; 119 memcpy(bss->supp_rates, elems->supp_rates, clen); 120 srlen += clen; 121 } 122 if (elems->ext_supp_rates) { 123 clen = IEEE80211_MAX_SUPP_RATES - srlen; 124 if (clen > elems->ext_supp_rates_len) 125 clen = elems->ext_supp_rates_len; 126 memcpy(bss->supp_rates + srlen, elems->ext_supp_rates, 127 clen); 128 srlen += clen; 129 } 130 if (srlen) { 131 bss->supp_rates_len = srlen; 132 if (!elems->parse_error) 133 bss->valid_data |= IEEE80211_BSS_VALID_RATES; 134 } 135 } 136 137 if (!elems->parse_error || 138 !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) { 139 bss->wmm_used = elems->wmm_param || elems->wmm_info; 140 bss->uapsd_supported = is_uapsd_supported(elems); 141 if (!elems->parse_error) 142 bss->valid_data |= IEEE80211_BSS_VALID_WMM; 143 } 144 145 if (update_data->beacon) { 146 struct ieee80211_supported_band *sband = 147 local->hw.wiphy->bands[rx_status->band]; 148 if (!(rx_status->encoding == RX_ENC_HT) && 149 !(rx_status->encoding == RX_ENC_VHT)) 150 bss->beacon_rate = 151 &sband->bitrates[rx_status->rate_idx]; 152 } 153 154 if (elems->vht_cap_elem) 155 bss->vht_cap_info = 156 le32_to_cpu(elems->vht_cap_elem->vht_cap_info); 157 else 158 bss->vht_cap_info = 0; 159 160 kfree(elems); 161 } 162 163 struct ieee80211_bss * 164 ieee80211_bss_info_update(struct ieee80211_local *local, 165 struct ieee80211_rx_status *rx_status, 166 struct ieee80211_mgmt *mgmt, size_t len, 167 struct ieee80211_channel *channel) 168 { 169 bool beacon = ieee80211_is_beacon(mgmt->frame_control) || 170 ieee80211_is_s1g_beacon(mgmt->frame_control); 171 struct cfg80211_bss *cbss; 172 struct inform_bss_update_data update_data = { 173 .rx_status = rx_status, 174 .beacon = beacon, 175 }; 176 struct cfg80211_inform_bss bss_meta = { 177 .boottime_ns = rx_status->boottime_ns, 178 .drv_data = (void *)&update_data, 179 }; 180 bool signal_valid; 181 struct ieee80211_sub_if_data *scan_sdata; 182 183 if (rx_status->flag & RX_FLAG_NO_SIGNAL_VAL) 184 bss_meta.signal = 0; /* invalid signal indication */ 185 else if (ieee80211_hw_check(&local->hw, SIGNAL_DBM)) 186 bss_meta.signal = rx_status->signal * 100; 187 else if (ieee80211_hw_check(&local->hw, SIGNAL_UNSPEC)) 188 bss_meta.signal = (rx_status->signal * 100) / local->hw.max_signal; 189 190 bss_meta.chan = channel; 191 192 rcu_read_lock(); 193 scan_sdata = rcu_dereference(local->scan_sdata); 194 if (scan_sdata && scan_sdata->vif.type == NL80211_IFTYPE_STATION && 195 scan_sdata->vif.cfg.assoc && 196 ieee80211_have_rx_timestamp(rx_status)) { 197 struct ieee80211_bss_conf *link_conf = NULL; 198 199 /* for an MLO connection, set the TSF data only in case we have 200 * an indication on which of the links the frame was received 201 */ 202 if (ieee80211_vif_is_mld(&scan_sdata->vif)) { 203 if (rx_status->link_valid) { 204 s8 link_id = rx_status->link_id; 205 206 link_conf = 207 rcu_dereference(scan_sdata->vif.link_conf[link_id]); 208 } 209 } else { 210 link_conf = &scan_sdata->vif.bss_conf; 211 } 212 213 if (link_conf) { 214 bss_meta.parent_tsf = 215 ieee80211_calculate_rx_timestamp(local, 216 rx_status, 217 len + FCS_LEN, 218 24); 219 220 ether_addr_copy(bss_meta.parent_bssid, 221 link_conf->bssid); 222 } 223 } 224 rcu_read_unlock(); 225 226 cbss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta, 227 mgmt, len, GFP_ATOMIC); 228 if (!cbss) 229 return NULL; 230 231 /* In case the signal is invalid update the status */ 232 signal_valid = channel == cbss->channel; 233 if (!signal_valid) 234 rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL; 235 236 return (void *)cbss->priv; 237 } 238 239 static bool ieee80211_scan_accept_presp(struct ieee80211_sub_if_data *sdata, 240 struct ieee80211_channel *channel, 241 u32 scan_flags, const u8 *da) 242 { 243 struct ieee80211_link_data *link_sdata; 244 u8 link_id; 245 246 if (!sdata) 247 return false; 248 249 /* accept broadcast on 6 GHz and for OCE */ 250 if (is_broadcast_ether_addr(da) && 251 (channel->band == NL80211_BAND_6GHZ || 252 scan_flags & NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP)) 253 return true; 254 255 if (scan_flags & NL80211_SCAN_FLAG_RANDOM_ADDR) 256 return true; 257 258 if (ether_addr_equal(da, sdata->vif.addr)) 259 return true; 260 261 for (link_id = 0; link_id < IEEE80211_MLD_MAX_NUM_LINKS; link_id++) { 262 link_sdata = rcu_dereference(sdata->link[link_id]); 263 if (!link_sdata) 264 continue; 265 266 if (ether_addr_equal(da, link_sdata->conf->addr)) 267 return true; 268 } 269 270 return false; 271 } 272 273 void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb) 274 { 275 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb); 276 struct ieee80211_mgmt *mgmt = (void *)skb->data; 277 struct ieee80211_bss *bss; 278 struct ieee80211_channel *channel; 279 size_t min_hdr_len = offsetof(struct ieee80211_mgmt, 280 u.probe_resp.variable); 281 282 if (!ieee80211_is_probe_resp(mgmt->frame_control) && 283 !ieee80211_is_beacon(mgmt->frame_control) && 284 !ieee80211_is_s1g_beacon(mgmt->frame_control)) 285 return; 286 287 if (ieee80211_is_s1g_beacon(mgmt->frame_control)) { 288 if (ieee80211_is_s1g_short_beacon(mgmt->frame_control)) 289 min_hdr_len = offsetof(struct ieee80211_ext, 290 u.s1g_short_beacon.variable); 291 else 292 min_hdr_len = offsetof(struct ieee80211_ext, 293 u.s1g_beacon); 294 } 295 296 if (skb->len < min_hdr_len) 297 return; 298 299 if (test_and_clear_bit(SCAN_BEACON_WAIT, &local->scanning)) { 300 /* 301 * we were passive scanning because of radar/no-IR, but 302 * the beacon/proberesp rx gives us an opportunity to upgrade 303 * to active scan 304 */ 305 set_bit(SCAN_BEACON_DONE, &local->scanning); 306 wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work, 0); 307 } 308 309 channel = ieee80211_get_channel_khz(local->hw.wiphy, 310 ieee80211_rx_status_to_khz(rx_status)); 311 312 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED) 313 return; 314 315 if (ieee80211_is_probe_resp(mgmt->frame_control)) { 316 struct ieee80211_sub_if_data *sdata1, *sdata2; 317 struct cfg80211_scan_request *scan_req; 318 struct cfg80211_sched_scan_request *sched_scan_req; 319 u32 scan_req_flags = 0, sched_scan_req_flags = 0; 320 321 sdata1 = rcu_dereference(local->scan_sdata); 322 sdata2 = rcu_dereference(local->sched_scan_sdata); 323 324 if (likely(!sdata1 && !sdata2)) 325 return; 326 327 scan_req = rcu_dereference(local->scan_req); 328 sched_scan_req = rcu_dereference(local->sched_scan_req); 329 330 if (scan_req) 331 scan_req_flags = scan_req->flags; 332 333 if (sched_scan_req) 334 sched_scan_req_flags = sched_scan_req->flags; 335 336 /* ignore ProbeResp to foreign address or non-bcast (OCE) 337 * unless scanning with randomised address 338 */ 339 if (!ieee80211_scan_accept_presp(sdata1, channel, 340 scan_req_flags, 341 mgmt->da) && 342 !ieee80211_scan_accept_presp(sdata2, channel, 343 sched_scan_req_flags, 344 mgmt->da)) 345 return; 346 } else { 347 /* Beacons are expected only with broadcast address */ 348 if (!is_broadcast_ether_addr(mgmt->da)) 349 return; 350 } 351 352 /* Do not update the BSS table in case of only monitor interfaces */ 353 if (local->open_count == local->monitors) 354 return; 355 356 bss = ieee80211_bss_info_update(local, rx_status, 357 mgmt, skb->len, 358 channel); 359 if (bss) 360 ieee80211_rx_bss_put(local, bss); 361 } 362 363 static void ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef) 364 { 365 memset(chandef, 0, sizeof(*chandef)); 366 367 chandef->width = NL80211_CHAN_WIDTH_20_NOHT; 368 } 369 370 /* return false if no more work */ 371 static bool ieee80211_prep_hw_scan(struct ieee80211_sub_if_data *sdata) 372 { 373 struct ieee80211_local *local = sdata->local; 374 struct cfg80211_scan_request *req; 375 struct cfg80211_chan_def chandef; 376 u8 bands_used = 0; 377 int i, ielen; 378 u32 *n_chans; 379 u32 flags = 0; 380 381 req = rcu_dereference_protected(local->scan_req, 382 lockdep_is_held(&local->hw.wiphy->mtx)); 383 384 if (test_bit(SCAN_HW_CANCELLED, &local->scanning)) 385 return false; 386 387 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) { 388 local->hw_scan_req->req.n_channels = req->n_channels; 389 390 for (i = 0; i < req->n_channels; i++) { 391 local->hw_scan_req->req.channels[i] = req->channels[i]; 392 bands_used |= BIT(req->channels[i]->band); 393 } 394 } else { 395 do { 396 if (local->hw_scan_band == NUM_NL80211_BANDS) 397 return false; 398 399 n_chans = &local->hw_scan_req->req.n_channels; 400 *n_chans = 0; 401 402 for (i = 0; i < req->n_channels; i++) { 403 if (req->channels[i]->band != 404 local->hw_scan_band) 405 continue; 406 local->hw_scan_req->req.channels[(*n_chans)++] = 407 req->channels[i]; 408 409 bands_used |= BIT(req->channels[i]->band); 410 } 411 412 local->hw_scan_band++; 413 } while (!*n_chans); 414 } 415 416 ieee80211_prepare_scan_chandef(&chandef); 417 418 if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT) 419 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT; 420 421 ielen = ieee80211_build_preq_ies(sdata, 422 (u8 *)local->hw_scan_req->req.ie, 423 local->hw_scan_ies_bufsize, 424 &local->hw_scan_req->ies, 425 req->ie, req->ie_len, 426 bands_used, req->rates, &chandef, 427 flags); 428 if (ielen < 0) 429 return false; 430 local->hw_scan_req->req.ie_len = ielen; 431 local->hw_scan_req->req.no_cck = req->no_cck; 432 ether_addr_copy(local->hw_scan_req->req.mac_addr, req->mac_addr); 433 ether_addr_copy(local->hw_scan_req->req.mac_addr_mask, 434 req->mac_addr_mask); 435 ether_addr_copy(local->hw_scan_req->req.bssid, req->bssid); 436 437 return true; 438 } 439 440 static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted) 441 { 442 struct ieee80211_local *local = hw_to_local(hw); 443 bool hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning); 444 bool was_scanning = local->scanning; 445 struct cfg80211_scan_request *scan_req; 446 struct ieee80211_sub_if_data *scan_sdata; 447 struct ieee80211_sub_if_data *sdata; 448 449 lockdep_assert_wiphy(local->hw.wiphy); 450 451 /* 452 * It's ok to abort a not-yet-running scan (that 453 * we have one at all will be verified by checking 454 * local->scan_req next), but not to complete it 455 * successfully. 456 */ 457 if (WARN_ON(!local->scanning && !aborted)) 458 aborted = true; 459 460 if (WARN_ON(!local->scan_req)) 461 return; 462 463 scan_sdata = rcu_dereference_protected(local->scan_sdata, 464 lockdep_is_held(&local->hw.wiphy->mtx)); 465 466 if (hw_scan && !aborted && 467 !ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS) && 468 ieee80211_prep_hw_scan(scan_sdata)) { 469 int rc; 470 471 rc = drv_hw_scan(local, 472 rcu_dereference_protected(local->scan_sdata, 473 lockdep_is_held(&local->hw.wiphy->mtx)), 474 local->hw_scan_req); 475 476 if (rc == 0) 477 return; 478 479 /* HW scan failed and is going to be reported as aborted, 480 * so clear old scan info. 481 */ 482 memset(&local->scan_info, 0, sizeof(local->scan_info)); 483 aborted = true; 484 } 485 486 kfree(local->hw_scan_req); 487 local->hw_scan_req = NULL; 488 489 scan_req = rcu_dereference_protected(local->scan_req, 490 lockdep_is_held(&local->hw.wiphy->mtx)); 491 492 RCU_INIT_POINTER(local->scan_req, NULL); 493 RCU_INIT_POINTER(local->scan_sdata, NULL); 494 495 local->scanning = 0; 496 local->scan_chandef.chan = NULL; 497 498 synchronize_rcu(); 499 500 if (scan_req != local->int_scan_req) { 501 local->scan_info.aborted = aborted; 502 cfg80211_scan_done(scan_req, &local->scan_info); 503 } 504 505 /* Set power back to normal operating levels. */ 506 ieee80211_hw_conf_chan(local); 507 508 if (!hw_scan && was_scanning) { 509 ieee80211_configure_filter(local); 510 drv_sw_scan_complete(local, scan_sdata); 511 ieee80211_offchannel_return(local); 512 } 513 514 ieee80211_recalc_idle(local); 515 516 ieee80211_mlme_notify_scan_completed(local); 517 ieee80211_ibss_notify_scan_completed(local); 518 519 /* Requeue all the work that might have been ignored while 520 * the scan was in progress; if there was none this will 521 * just be a no-op for the particular interface. 522 */ 523 list_for_each_entry(sdata, &local->interfaces, list) { 524 if (ieee80211_sdata_running(sdata)) 525 wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work); 526 } 527 528 if (was_scanning) 529 ieee80211_start_next_roc(local); 530 } 531 532 void ieee80211_scan_completed(struct ieee80211_hw *hw, 533 struct cfg80211_scan_info *info) 534 { 535 struct ieee80211_local *local = hw_to_local(hw); 536 537 trace_api_scan_completed(local, info->aborted); 538 539 set_bit(SCAN_COMPLETED, &local->scanning); 540 if (info->aborted) 541 set_bit(SCAN_ABORTED, &local->scanning); 542 543 memcpy(&local->scan_info, info, sizeof(*info)); 544 545 wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work, 0); 546 } 547 EXPORT_SYMBOL(ieee80211_scan_completed); 548 549 static int ieee80211_start_sw_scan(struct ieee80211_local *local, 550 struct ieee80211_sub_if_data *sdata) 551 { 552 /* Software scan is not supported in multi-channel cases */ 553 if (!local->emulate_chanctx) 554 return -EOPNOTSUPP; 555 556 /* 557 * Hardware/driver doesn't support hw_scan, so use software 558 * scanning instead. First send a nullfunc frame with power save 559 * bit on so that AP will buffer the frames for us while we are not 560 * listening, then send probe requests to each channel and wait for 561 * the responses. After all channels are scanned, tune back to the 562 * original channel and send a nullfunc frame with power save bit 563 * off to trigger the AP to send us all the buffered frames. 564 * 565 * Note that while local->sw_scanning is true everything else but 566 * nullfunc frames and probe requests will be dropped in 567 * ieee80211_tx_h_check_assoc(). 568 */ 569 drv_sw_scan_start(local, sdata, local->scan_addr); 570 571 local->leave_oper_channel_time = jiffies; 572 local->next_scan_state = SCAN_DECISION; 573 local->scan_channel_idx = 0; 574 575 ieee80211_offchannel_stop_vifs(local); 576 577 /* ensure nullfunc is transmitted before leaving operating channel */ 578 ieee80211_flush_queues(local, NULL, false); 579 580 ieee80211_configure_filter(local); 581 582 /* We need to set power level at maximum rate for scanning. */ 583 ieee80211_hw_conf_chan(local); 584 585 wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work, 0); 586 587 return 0; 588 } 589 590 static bool __ieee80211_can_leave_ch(struct ieee80211_sub_if_data *sdata) 591 { 592 struct ieee80211_local *local = sdata->local; 593 struct ieee80211_sub_if_data *sdata_iter; 594 unsigned int link_id; 595 596 lockdep_assert_wiphy(local->hw.wiphy); 597 598 if (!ieee80211_is_radar_required(local)) 599 return true; 600 601 if (!regulatory_pre_cac_allowed(local->hw.wiphy)) 602 return false; 603 604 list_for_each_entry(sdata_iter, &local->interfaces, list) { 605 for_each_valid_link(&sdata_iter->wdev, link_id) 606 if (sdata_iter->wdev.links[link_id].cac_started) 607 return false; 608 } 609 610 return true; 611 } 612 613 static bool ieee80211_can_scan(struct ieee80211_local *local, 614 struct ieee80211_sub_if_data *sdata) 615 { 616 if (!__ieee80211_can_leave_ch(sdata)) 617 return false; 618 619 if (!list_empty(&local->roc_list)) 620 return false; 621 622 if (sdata->vif.type == NL80211_IFTYPE_STATION && 623 sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL) 624 return false; 625 626 return true; 627 } 628 629 void ieee80211_run_deferred_scan(struct ieee80211_local *local) 630 { 631 lockdep_assert_wiphy(local->hw.wiphy); 632 633 if (!local->scan_req || local->scanning) 634 return; 635 636 if (!ieee80211_can_scan(local, 637 rcu_dereference_protected( 638 local->scan_sdata, 639 lockdep_is_held(&local->hw.wiphy->mtx)))) 640 return; 641 642 wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work, 643 round_jiffies_relative(0)); 644 } 645 646 static void ieee80211_send_scan_probe_req(struct ieee80211_sub_if_data *sdata, 647 const u8 *src, const u8 *dst, 648 const u8 *ssid, size_t ssid_len, 649 const u8 *ie, size_t ie_len, 650 u32 ratemask, u32 flags, u32 tx_flags, 651 struct ieee80211_channel *channel) 652 { 653 struct sk_buff *skb; 654 655 skb = ieee80211_build_probe_req(sdata, src, dst, ratemask, channel, 656 ssid, ssid_len, 657 ie, ie_len, flags); 658 659 if (skb) { 660 if (flags & IEEE80211_PROBE_FLAG_RANDOM_SN) { 661 struct ieee80211_hdr *hdr = (void *)skb->data; 662 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 663 u16 sn = get_random_u16(); 664 665 info->control.flags |= IEEE80211_TX_CTRL_NO_SEQNO; 666 hdr->seq_ctrl = 667 cpu_to_le16(IEEE80211_SN_TO_SEQ(sn)); 668 } 669 IEEE80211_SKB_CB(skb)->flags |= tx_flags; 670 IEEE80211_SKB_CB(skb)->control.flags |= IEEE80211_TX_CTRL_DONT_USE_RATE_MASK; 671 ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band); 672 } 673 } 674 675 static void ieee80211_scan_state_send_probe(struct ieee80211_local *local, 676 unsigned long *next_delay) 677 { 678 int i; 679 struct ieee80211_sub_if_data *sdata; 680 struct cfg80211_scan_request *scan_req; 681 enum nl80211_band band = local->hw.conf.chandef.chan->band; 682 u32 flags = 0, tx_flags; 683 684 scan_req = rcu_dereference_protected(local->scan_req, 685 lockdep_is_held(&local->hw.wiphy->mtx)); 686 687 tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK; 688 if (scan_req->no_cck) 689 tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE; 690 if (scan_req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT) 691 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT; 692 if (scan_req->flags & NL80211_SCAN_FLAG_RANDOM_SN) 693 flags |= IEEE80211_PROBE_FLAG_RANDOM_SN; 694 695 sdata = rcu_dereference_protected(local->scan_sdata, 696 lockdep_is_held(&local->hw.wiphy->mtx)); 697 698 for (i = 0; i < scan_req->n_ssids; i++) 699 ieee80211_send_scan_probe_req( 700 sdata, local->scan_addr, scan_req->bssid, 701 scan_req->ssids[i].ssid, scan_req->ssids[i].ssid_len, 702 scan_req->ie, scan_req->ie_len, 703 scan_req->rates[band], flags, 704 tx_flags, local->hw.conf.chandef.chan); 705 706 /* 707 * After sending probe requests, wait for probe responses 708 * on the channel. 709 */ 710 *next_delay = msecs_to_jiffies(scan_req->duration) > 711 IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME ? 712 msecs_to_jiffies(scan_req->duration) - IEEE80211_PROBE_DELAY : 713 IEEE80211_CHANNEL_TIME; 714 local->next_scan_state = SCAN_DECISION; 715 } 716 717 static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata, 718 struct cfg80211_scan_request *req) 719 { 720 struct ieee80211_local *local = sdata->local; 721 bool hw_scan = local->ops->hw_scan; 722 int rc; 723 724 lockdep_assert_wiphy(local->hw.wiphy); 725 726 if (local->scan_req) 727 return -EBUSY; 728 729 /* For an MLO connection, if a link ID was specified, validate that it 730 * is indeed active. 731 */ 732 if (ieee80211_vif_is_mld(&sdata->vif) && req->tsf_report_link_id >= 0 && 733 !(sdata->vif.active_links & BIT(req->tsf_report_link_id))) 734 return -EINVAL; 735 736 if (!__ieee80211_can_leave_ch(sdata)) 737 return -EBUSY; 738 739 if (!ieee80211_can_scan(local, sdata)) { 740 /* wait for the work to finish/time out */ 741 rcu_assign_pointer(local->scan_req, req); 742 rcu_assign_pointer(local->scan_sdata, sdata); 743 return 0; 744 } 745 746 again: 747 if (hw_scan) { 748 u8 *ies; 749 750 local->hw_scan_ies_bufsize = local->scan_ies_len + req->ie_len; 751 752 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) { 753 int i, n_bands = 0; 754 u8 bands_counted = 0; 755 756 for (i = 0; i < req->n_channels; i++) { 757 if (bands_counted & BIT(req->channels[i]->band)) 758 continue; 759 bands_counted |= BIT(req->channels[i]->band); 760 n_bands++; 761 } 762 763 local->hw_scan_ies_bufsize *= n_bands; 764 } 765 766 local->hw_scan_req = kmalloc(struct_size(local->hw_scan_req, 767 req.channels, 768 req->n_channels) + 769 local->hw_scan_ies_bufsize, 770 GFP_KERNEL); 771 if (!local->hw_scan_req) 772 return -ENOMEM; 773 774 local->hw_scan_req->req.ssids = req->ssids; 775 local->hw_scan_req->req.n_ssids = req->n_ssids; 776 /* None of the channels are actually set 777 * up but let UBSAN know the boundaries. 778 */ 779 local->hw_scan_req->req.n_channels = req->n_channels; 780 781 ies = (u8 *)local->hw_scan_req + 782 sizeof(*local->hw_scan_req) + 783 req->n_channels * sizeof(req->channels[0]); 784 local->hw_scan_req->req.ie = ies; 785 local->hw_scan_req->req.flags = req->flags; 786 eth_broadcast_addr(local->hw_scan_req->req.bssid); 787 local->hw_scan_req->req.duration = req->duration; 788 local->hw_scan_req->req.duration_mandatory = 789 req->duration_mandatory; 790 local->hw_scan_req->req.tsf_report_link_id = 791 req->tsf_report_link_id; 792 793 local->hw_scan_band = 0; 794 local->hw_scan_req->req.n_6ghz_params = req->n_6ghz_params; 795 local->hw_scan_req->req.scan_6ghz_params = 796 req->scan_6ghz_params; 797 local->hw_scan_req->req.scan_6ghz = req->scan_6ghz; 798 799 /* 800 * After allocating local->hw_scan_req, we must 801 * go through until ieee80211_prep_hw_scan(), so 802 * anything that might be changed here and leave 803 * this function early must not go after this 804 * allocation. 805 */ 806 } 807 808 rcu_assign_pointer(local->scan_req, req); 809 rcu_assign_pointer(local->scan_sdata, sdata); 810 811 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) 812 get_random_mask_addr(local->scan_addr, 813 req->mac_addr, 814 req->mac_addr_mask); 815 else 816 memcpy(local->scan_addr, sdata->vif.addr, ETH_ALEN); 817 818 if (hw_scan) { 819 __set_bit(SCAN_HW_SCANNING, &local->scanning); 820 } else if ((req->n_channels == 1) && 821 (req->channels[0] == local->hw.conf.chandef.chan)) { 822 /* 823 * If we are scanning only on the operating channel 824 * then we do not need to stop normal activities 825 */ 826 unsigned long next_delay; 827 828 __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning); 829 830 ieee80211_recalc_idle(local); 831 832 /* Notify driver scan is starting, keep order of operations 833 * same as normal software scan, in case that matters. */ 834 drv_sw_scan_start(local, sdata, local->scan_addr); 835 836 ieee80211_configure_filter(local); /* accept probe-responses */ 837 838 /* We need to ensure power level is at max for scanning. */ 839 ieee80211_hw_conf_chan(local); 840 841 if ((req->channels[0]->flags & (IEEE80211_CHAN_NO_IR | 842 IEEE80211_CHAN_RADAR)) || 843 !req->n_ssids) { 844 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 845 if (req->n_ssids) 846 set_bit(SCAN_BEACON_WAIT, &local->scanning); 847 } else { 848 ieee80211_scan_state_send_probe(local, &next_delay); 849 next_delay = IEEE80211_CHANNEL_TIME; 850 } 851 852 /* Now, just wait a bit and we are all done! */ 853 wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work, 854 next_delay); 855 return 0; 856 } else { 857 /* Do normal software scan */ 858 __set_bit(SCAN_SW_SCANNING, &local->scanning); 859 } 860 861 ieee80211_recalc_idle(local); 862 863 if (hw_scan) { 864 WARN_ON(!ieee80211_prep_hw_scan(sdata)); 865 rc = drv_hw_scan(local, sdata, local->hw_scan_req); 866 } else { 867 rc = ieee80211_start_sw_scan(local, sdata); 868 } 869 870 if (rc) { 871 kfree(local->hw_scan_req); 872 local->hw_scan_req = NULL; 873 local->scanning = 0; 874 875 ieee80211_recalc_idle(local); 876 877 local->scan_req = NULL; 878 RCU_INIT_POINTER(local->scan_sdata, NULL); 879 } 880 881 if (hw_scan && rc == 1) { 882 /* 883 * we can't fall back to software for P2P-GO 884 * as it must update NoA etc. 885 */ 886 if (ieee80211_vif_type_p2p(&sdata->vif) == 887 NL80211_IFTYPE_P2P_GO) 888 return -EOPNOTSUPP; 889 hw_scan = false; 890 goto again; 891 } 892 893 return rc; 894 } 895 896 static unsigned long 897 ieee80211_scan_get_channel_time(struct ieee80211_channel *chan) 898 { 899 /* 900 * TODO: channel switching also consumes quite some time, 901 * add that delay as well to get a better estimation 902 */ 903 if (chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) 904 return IEEE80211_PASSIVE_CHANNEL_TIME; 905 return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME; 906 } 907 908 static void ieee80211_scan_state_decision(struct ieee80211_local *local, 909 unsigned long *next_delay) 910 { 911 bool associated = false; 912 bool tx_empty = true; 913 bool bad_latency; 914 struct ieee80211_sub_if_data *sdata; 915 struct ieee80211_channel *next_chan; 916 enum mac80211_scan_state next_scan_state; 917 struct cfg80211_scan_request *scan_req; 918 919 lockdep_assert_wiphy(local->hw.wiphy); 920 921 /* 922 * check if at least one STA interface is associated, 923 * check if at least one STA interface has pending tx frames 924 * and grab the lowest used beacon interval 925 */ 926 list_for_each_entry(sdata, &local->interfaces, list) { 927 if (!ieee80211_sdata_running(sdata)) 928 continue; 929 930 if (sdata->vif.type == NL80211_IFTYPE_STATION) { 931 if (sdata->u.mgd.associated) { 932 associated = true; 933 934 if (!qdisc_all_tx_empty(sdata->dev)) { 935 tx_empty = false; 936 break; 937 } 938 } 939 } 940 } 941 942 scan_req = rcu_dereference_protected(local->scan_req, 943 lockdep_is_held(&local->hw.wiphy->mtx)); 944 945 next_chan = scan_req->channels[local->scan_channel_idx]; 946 947 /* 948 * we're currently scanning a different channel, let's 949 * see if we can scan another channel without interfering 950 * with the current traffic situation. 951 * 952 * Keep good latency, do not stay off-channel more than 125 ms. 953 */ 954 955 bad_latency = time_after(jiffies + 956 ieee80211_scan_get_channel_time(next_chan), 957 local->leave_oper_channel_time + HZ / 8); 958 959 if (associated && !tx_empty) { 960 if (scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) 961 next_scan_state = SCAN_ABORT; 962 else 963 next_scan_state = SCAN_SUSPEND; 964 } else if (associated && bad_latency) { 965 next_scan_state = SCAN_SUSPEND; 966 } else { 967 next_scan_state = SCAN_SET_CHANNEL; 968 } 969 970 local->next_scan_state = next_scan_state; 971 972 *next_delay = 0; 973 } 974 975 static void ieee80211_scan_state_set_channel(struct ieee80211_local *local, 976 unsigned long *next_delay) 977 { 978 int skip; 979 struct ieee80211_channel *chan; 980 struct cfg80211_scan_request *scan_req; 981 982 scan_req = rcu_dereference_protected(local->scan_req, 983 lockdep_is_held(&local->hw.wiphy->mtx)); 984 985 skip = 0; 986 chan = scan_req->channels[local->scan_channel_idx]; 987 988 local->scan_chandef.chan = chan; 989 local->scan_chandef.center_freq1 = chan->center_freq; 990 local->scan_chandef.freq1_offset = chan->freq_offset; 991 local->scan_chandef.center_freq2 = 0; 992 993 /* For scanning on the S1G band, detect the channel width according to 994 * the channel being scanned. 995 */ 996 if (chan->band == NL80211_BAND_S1GHZ) { 997 local->scan_chandef.width = ieee80211_s1g_channel_width(chan); 998 goto set_channel; 999 } 1000 1001 /* If scanning on oper channel, use whatever channel-type 1002 * is currently in use. 1003 */ 1004 if (chan == local->hw.conf.chandef.chan) 1005 local->scan_chandef = local->hw.conf.chandef; 1006 else 1007 local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT; 1008 1009 set_channel: 1010 if (ieee80211_hw_conf_chan(local)) 1011 skip = 1; 1012 1013 /* advance state machine to next channel/band */ 1014 local->scan_channel_idx++; 1015 1016 if (skip) { 1017 /* if we skip this channel return to the decision state */ 1018 local->next_scan_state = SCAN_DECISION; 1019 return; 1020 } 1021 1022 /* 1023 * Probe delay is used to update the NAV, cf. 11.1.3.2.2 1024 * (which unfortunately doesn't say _why_ step a) is done, 1025 * but it waits for the probe delay or until a frame is 1026 * received - and the received frame would update the NAV). 1027 * For now, we do not support waiting until a frame is 1028 * received. 1029 * 1030 * In any case, it is not necessary for a passive scan. 1031 */ 1032 if ((chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) || 1033 !scan_req->n_ssids) { 1034 *next_delay = max(msecs_to_jiffies(scan_req->duration), 1035 IEEE80211_PASSIVE_CHANNEL_TIME); 1036 local->next_scan_state = SCAN_DECISION; 1037 if (scan_req->n_ssids) 1038 set_bit(SCAN_BEACON_WAIT, &local->scanning); 1039 return; 1040 } 1041 1042 /* active scan, send probes */ 1043 *next_delay = IEEE80211_PROBE_DELAY; 1044 local->next_scan_state = SCAN_SEND_PROBE; 1045 } 1046 1047 static void ieee80211_scan_state_suspend(struct ieee80211_local *local, 1048 unsigned long *next_delay) 1049 { 1050 /* switch back to the operating channel */ 1051 local->scan_chandef.chan = NULL; 1052 ieee80211_hw_conf_chan(local); 1053 1054 /* disable PS */ 1055 ieee80211_offchannel_return(local); 1056 1057 *next_delay = HZ / 5; 1058 /* afterwards, resume scan & go to next channel */ 1059 local->next_scan_state = SCAN_RESUME; 1060 } 1061 1062 static void ieee80211_scan_state_resume(struct ieee80211_local *local, 1063 unsigned long *next_delay) 1064 { 1065 ieee80211_offchannel_stop_vifs(local); 1066 1067 if (local->ops->flush) { 1068 ieee80211_flush_queues(local, NULL, false); 1069 *next_delay = 0; 1070 } else 1071 *next_delay = HZ / 10; 1072 1073 /* remember when we left the operating channel */ 1074 local->leave_oper_channel_time = jiffies; 1075 1076 /* advance to the next channel to be scanned */ 1077 local->next_scan_state = SCAN_SET_CHANNEL; 1078 } 1079 1080 void ieee80211_scan_work(struct wiphy *wiphy, struct wiphy_work *work) 1081 { 1082 struct ieee80211_local *local = 1083 container_of(work, struct ieee80211_local, scan_work.work); 1084 struct ieee80211_sub_if_data *sdata; 1085 struct cfg80211_scan_request *scan_req; 1086 unsigned long next_delay = 0; 1087 bool aborted; 1088 1089 lockdep_assert_wiphy(local->hw.wiphy); 1090 1091 if (!ieee80211_can_run_worker(local)) { 1092 aborted = true; 1093 goto out_complete; 1094 } 1095 1096 sdata = rcu_dereference_protected(local->scan_sdata, 1097 lockdep_is_held(&local->hw.wiphy->mtx)); 1098 scan_req = rcu_dereference_protected(local->scan_req, 1099 lockdep_is_held(&local->hw.wiphy->mtx)); 1100 1101 /* When scanning on-channel, the first-callback means completed. */ 1102 if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) { 1103 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 1104 goto out_complete; 1105 } 1106 1107 if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) { 1108 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 1109 goto out_complete; 1110 } 1111 1112 if (!sdata || !scan_req) 1113 return; 1114 1115 if (!local->scanning) { 1116 int rc; 1117 1118 RCU_INIT_POINTER(local->scan_req, NULL); 1119 RCU_INIT_POINTER(local->scan_sdata, NULL); 1120 1121 rc = __ieee80211_start_scan(sdata, scan_req); 1122 if (!rc) 1123 return; 1124 /* need to complete scan in cfg80211 */ 1125 rcu_assign_pointer(local->scan_req, scan_req); 1126 aborted = true; 1127 goto out_complete; 1128 } 1129 1130 clear_bit(SCAN_BEACON_WAIT, &local->scanning); 1131 1132 /* 1133 * as long as no delay is required advance immediately 1134 * without scheduling a new work 1135 */ 1136 do { 1137 if (!ieee80211_sdata_running(sdata)) { 1138 aborted = true; 1139 goto out_complete; 1140 } 1141 1142 if (test_and_clear_bit(SCAN_BEACON_DONE, &local->scanning) && 1143 local->next_scan_state == SCAN_DECISION) 1144 local->next_scan_state = SCAN_SEND_PROBE; 1145 1146 switch (local->next_scan_state) { 1147 case SCAN_DECISION: 1148 /* if no more bands/channels left, complete scan */ 1149 if (local->scan_channel_idx >= scan_req->n_channels) { 1150 aborted = false; 1151 goto out_complete; 1152 } 1153 ieee80211_scan_state_decision(local, &next_delay); 1154 break; 1155 case SCAN_SET_CHANNEL: 1156 ieee80211_scan_state_set_channel(local, &next_delay); 1157 break; 1158 case SCAN_SEND_PROBE: 1159 ieee80211_scan_state_send_probe(local, &next_delay); 1160 break; 1161 case SCAN_SUSPEND: 1162 ieee80211_scan_state_suspend(local, &next_delay); 1163 break; 1164 case SCAN_RESUME: 1165 ieee80211_scan_state_resume(local, &next_delay); 1166 break; 1167 case SCAN_ABORT: 1168 aborted = true; 1169 goto out_complete; 1170 } 1171 } while (next_delay == 0); 1172 1173 wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work, 1174 next_delay); 1175 return; 1176 1177 out_complete: 1178 __ieee80211_scan_completed(&local->hw, aborted); 1179 } 1180 1181 int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata, 1182 struct cfg80211_scan_request *req) 1183 { 1184 lockdep_assert_wiphy(sdata->local->hw.wiphy); 1185 1186 return __ieee80211_start_scan(sdata, req); 1187 } 1188 1189 int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata, 1190 const u8 *ssid, u8 ssid_len, 1191 struct ieee80211_channel **channels, 1192 unsigned int n_channels) 1193 { 1194 struct ieee80211_local *local = sdata->local; 1195 int i, n_ch = 0; 1196 enum nl80211_band band; 1197 1198 lockdep_assert_wiphy(local->hw.wiphy); 1199 1200 /* busy scanning */ 1201 if (local->scan_req) 1202 return -EBUSY; 1203 1204 /* fill internal scan request */ 1205 if (!channels) { 1206 int max_n; 1207 1208 for (band = 0; band < NUM_NL80211_BANDS; band++) { 1209 if (!local->hw.wiphy->bands[band] || 1210 band == NL80211_BAND_6GHZ) 1211 continue; 1212 1213 max_n = local->hw.wiphy->bands[band]->n_channels; 1214 for (i = 0; i < max_n; i++) { 1215 struct ieee80211_channel *tmp_ch = 1216 &local->hw.wiphy->bands[band]->channels[i]; 1217 1218 if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR | 1219 IEEE80211_CHAN_DISABLED) || 1220 !cfg80211_wdev_channel_allowed(&sdata->wdev, 1221 tmp_ch)) 1222 continue; 1223 1224 local->int_scan_req->channels[n_ch] = tmp_ch; 1225 n_ch++; 1226 } 1227 } 1228 1229 if (WARN_ON_ONCE(n_ch == 0)) 1230 return -EINVAL; 1231 1232 local->int_scan_req->n_channels = n_ch; 1233 } else { 1234 for (i = 0; i < n_channels; i++) { 1235 if (channels[i]->flags & (IEEE80211_CHAN_NO_IR | 1236 IEEE80211_CHAN_DISABLED) || 1237 !cfg80211_wdev_channel_allowed(&sdata->wdev, 1238 channels[i])) 1239 continue; 1240 1241 local->int_scan_req->channels[n_ch] = channels[i]; 1242 n_ch++; 1243 } 1244 1245 if (n_ch == 0) 1246 return -EINVAL; 1247 1248 local->int_scan_req->n_channels = n_ch; 1249 } 1250 1251 local->int_scan_req->ssids = &local->scan_ssid; 1252 local->int_scan_req->n_ssids = 1; 1253 memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN); 1254 local->int_scan_req->ssids[0].ssid_len = ssid_len; 1255 1256 return __ieee80211_start_scan(sdata, sdata->local->int_scan_req); 1257 } 1258 1259 void ieee80211_scan_cancel(struct ieee80211_local *local) 1260 { 1261 /* ensure a new scan cannot be queued */ 1262 lockdep_assert_wiphy(local->hw.wiphy); 1263 1264 /* 1265 * We are canceling software scan, or deferred scan that was not 1266 * yet really started (see __ieee80211_start_scan ). 1267 * 1268 * Regarding hardware scan: 1269 * - we can not call __ieee80211_scan_completed() as when 1270 * SCAN_HW_SCANNING bit is set this function change 1271 * local->hw_scan_req to operate on 5G band, what race with 1272 * driver which can use local->hw_scan_req 1273 * 1274 * - we can not cancel scan_work since driver can schedule it 1275 * by ieee80211_scan_completed(..., true) to finish scan 1276 * 1277 * Hence we only call the cancel_hw_scan() callback, but the low-level 1278 * driver is still responsible for calling ieee80211_scan_completed() 1279 * after the scan was completed/aborted. 1280 */ 1281 1282 if (!local->scan_req) 1283 return; 1284 1285 /* 1286 * We have a scan running and the driver already reported completion, 1287 * but the worker hasn't run yet or is stuck on the mutex - mark it as 1288 * cancelled. 1289 */ 1290 if (test_bit(SCAN_HW_SCANNING, &local->scanning) && 1291 test_bit(SCAN_COMPLETED, &local->scanning)) { 1292 set_bit(SCAN_HW_CANCELLED, &local->scanning); 1293 return; 1294 } 1295 1296 if (test_bit(SCAN_HW_SCANNING, &local->scanning)) { 1297 /* 1298 * Make sure that __ieee80211_scan_completed doesn't trigger a 1299 * scan on another band. 1300 */ 1301 set_bit(SCAN_HW_CANCELLED, &local->scanning); 1302 if (local->ops->cancel_hw_scan) 1303 drv_cancel_hw_scan(local, 1304 rcu_dereference_protected(local->scan_sdata, 1305 lockdep_is_held(&local->hw.wiphy->mtx))); 1306 return; 1307 } 1308 1309 wiphy_delayed_work_cancel(local->hw.wiphy, &local->scan_work); 1310 /* and clean up */ 1311 memset(&local->scan_info, 0, sizeof(local->scan_info)); 1312 __ieee80211_scan_completed(&local->hw, true); 1313 } 1314 1315 int __ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata, 1316 struct cfg80211_sched_scan_request *req) 1317 { 1318 struct ieee80211_local *local = sdata->local; 1319 struct ieee80211_scan_ies sched_scan_ies = {}; 1320 struct cfg80211_chan_def chandef; 1321 int ret, i, iebufsz, num_bands = 0; 1322 u32 rate_masks[NUM_NL80211_BANDS] = {}; 1323 u8 bands_used = 0; 1324 u8 *ie; 1325 u32 flags = 0; 1326 1327 lockdep_assert_wiphy(local->hw.wiphy); 1328 1329 iebufsz = local->scan_ies_len + req->ie_len; 1330 1331 if (!local->ops->sched_scan_start) 1332 return -EOPNOTSUPP; 1333 1334 for (i = 0; i < NUM_NL80211_BANDS; i++) { 1335 if (local->hw.wiphy->bands[i]) { 1336 bands_used |= BIT(i); 1337 rate_masks[i] = (u32) -1; 1338 num_bands++; 1339 } 1340 } 1341 1342 if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT) 1343 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT; 1344 1345 ie = kcalloc(iebufsz, num_bands, GFP_KERNEL); 1346 if (!ie) { 1347 ret = -ENOMEM; 1348 goto out; 1349 } 1350 1351 ieee80211_prepare_scan_chandef(&chandef); 1352 1353 ret = ieee80211_build_preq_ies(sdata, ie, num_bands * iebufsz, 1354 &sched_scan_ies, req->ie, 1355 req->ie_len, bands_used, rate_masks, 1356 &chandef, flags); 1357 if (ret < 0) 1358 goto error; 1359 1360 ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies); 1361 if (ret == 0) { 1362 rcu_assign_pointer(local->sched_scan_sdata, sdata); 1363 rcu_assign_pointer(local->sched_scan_req, req); 1364 } 1365 1366 error: 1367 kfree(ie); 1368 out: 1369 if (ret) { 1370 /* Clean in case of failure after HW restart or upon resume. */ 1371 RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 1372 RCU_INIT_POINTER(local->sched_scan_req, NULL); 1373 } 1374 1375 return ret; 1376 } 1377 1378 int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata, 1379 struct cfg80211_sched_scan_request *req) 1380 { 1381 struct ieee80211_local *local = sdata->local; 1382 1383 lockdep_assert_wiphy(local->hw.wiphy); 1384 1385 if (rcu_access_pointer(local->sched_scan_sdata)) 1386 return -EBUSY; 1387 1388 return __ieee80211_request_sched_scan_start(sdata, req); 1389 } 1390 1391 int ieee80211_request_sched_scan_stop(struct ieee80211_local *local) 1392 { 1393 struct ieee80211_sub_if_data *sched_scan_sdata; 1394 int ret = -ENOENT; 1395 1396 lockdep_assert_wiphy(local->hw.wiphy); 1397 1398 if (!local->ops->sched_scan_stop) 1399 return -EOPNOTSUPP; 1400 1401 /* We don't want to restart sched scan anymore. */ 1402 RCU_INIT_POINTER(local->sched_scan_req, NULL); 1403 1404 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata, 1405 lockdep_is_held(&local->hw.wiphy->mtx)); 1406 if (sched_scan_sdata) { 1407 ret = drv_sched_scan_stop(local, sched_scan_sdata); 1408 if (!ret) 1409 RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 1410 } 1411 1412 return ret; 1413 } 1414 1415 void ieee80211_sched_scan_results(struct ieee80211_hw *hw) 1416 { 1417 struct ieee80211_local *local = hw_to_local(hw); 1418 1419 trace_api_sched_scan_results(local); 1420 1421 cfg80211_sched_scan_results(hw->wiphy, 0); 1422 } 1423 EXPORT_SYMBOL(ieee80211_sched_scan_results); 1424 1425 void ieee80211_sched_scan_end(struct ieee80211_local *local) 1426 { 1427 lockdep_assert_wiphy(local->hw.wiphy); 1428 1429 if (!rcu_access_pointer(local->sched_scan_sdata)) 1430 return; 1431 1432 RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 1433 1434 /* If sched scan was aborted by the driver. */ 1435 RCU_INIT_POINTER(local->sched_scan_req, NULL); 1436 1437 cfg80211_sched_scan_stopped_locked(local->hw.wiphy, 0); 1438 } 1439 1440 void ieee80211_sched_scan_stopped_work(struct wiphy *wiphy, 1441 struct wiphy_work *work) 1442 { 1443 struct ieee80211_local *local = 1444 container_of(work, struct ieee80211_local, 1445 sched_scan_stopped_work); 1446 1447 ieee80211_sched_scan_end(local); 1448 } 1449 1450 void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw) 1451 { 1452 struct ieee80211_local *local = hw_to_local(hw); 1453 1454 trace_api_sched_scan_stopped(local); 1455 1456 /* 1457 * this shouldn't really happen, so for simplicity 1458 * simply ignore it, and let mac80211 reconfigure 1459 * the sched scan later on. 1460 */ 1461 if (local->in_reconfig) 1462 return; 1463 1464 wiphy_work_queue(hw->wiphy, &local->sched_scan_stopped_work); 1465 } 1466 EXPORT_SYMBOL(ieee80211_sched_scan_stopped); 1467