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