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