1 /* 2 * Scanning implementation 3 * 4 * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi> 5 * Copyright 2004, Instant802 Networks, Inc. 6 * Copyright 2005, Devicescape Software, Inc. 7 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> 8 * Copyright 2007, Michael Wu <flamingice@sourmilk.net> 9 * Copyright 2013-2015 Intel Mobile Communications GmbH 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License version 2 as 13 * published by the Free Software Foundation. 14 */ 15 16 #include <linux/if_arp.h> 17 #include <linux/etherdevice.h> 18 #include <linux/rtnetlink.h> 19 #include <net/sch_generic.h> 20 #include <linux/slab.h> 21 #include <linux/export.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 ieee80211_bss * 59 ieee80211_bss_info_update(struct ieee80211_local *local, 60 struct ieee80211_rx_status *rx_status, 61 struct ieee80211_mgmt *mgmt, size_t len, 62 struct ieee802_11_elems *elems, 63 struct ieee80211_channel *channel) 64 { 65 bool beacon = ieee80211_is_beacon(mgmt->frame_control); 66 struct cfg80211_bss *cbss; 67 struct ieee80211_bss *bss; 68 int clen, srlen; 69 struct cfg80211_inform_bss bss_meta = { 70 .boottime_ns = rx_status->boottime_ns, 71 }; 72 bool signal_valid; 73 74 if (ieee80211_hw_check(&local->hw, SIGNAL_DBM)) 75 bss_meta.signal = rx_status->signal * 100; 76 else if (ieee80211_hw_check(&local->hw, SIGNAL_UNSPEC)) 77 bss_meta.signal = (rx_status->signal * 100) / local->hw.max_signal; 78 79 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_20; 80 if (rx_status->flag & RX_FLAG_5MHZ) 81 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_5; 82 if (rx_status->flag & RX_FLAG_10MHZ) 83 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_10; 84 85 bss_meta.chan = channel; 86 cbss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta, 87 mgmt, len, GFP_ATOMIC); 88 if (!cbss) 89 return NULL; 90 /* In case the signal is invalid update the status */ 91 signal_valid = abs(channel->center_freq - cbss->channel->center_freq) 92 <= local->hw.wiphy->max_adj_channel_rssi_comp; 93 if (!signal_valid) 94 rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL; 95 96 bss = (void *)cbss->priv; 97 98 if (beacon) 99 bss->device_ts_beacon = rx_status->device_timestamp; 100 else 101 bss->device_ts_presp = rx_status->device_timestamp; 102 103 if (elems->parse_error) { 104 if (beacon) 105 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON; 106 else 107 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP; 108 } else { 109 if (beacon) 110 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON; 111 else 112 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP; 113 } 114 115 /* save the ERP value so that it is available at association time */ 116 if (elems->erp_info && (!elems->parse_error || 117 !(bss->valid_data & IEEE80211_BSS_VALID_ERP))) { 118 bss->erp_value = elems->erp_info[0]; 119 bss->has_erp_value = true; 120 if (!elems->parse_error) 121 bss->valid_data |= IEEE80211_BSS_VALID_ERP; 122 } 123 124 /* replace old supported rates if we get new values */ 125 if (!elems->parse_error || 126 !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) { 127 srlen = 0; 128 if (elems->supp_rates) { 129 clen = IEEE80211_MAX_SUPP_RATES; 130 if (clen > elems->supp_rates_len) 131 clen = elems->supp_rates_len; 132 memcpy(bss->supp_rates, elems->supp_rates, clen); 133 srlen += clen; 134 } 135 if (elems->ext_supp_rates) { 136 clen = IEEE80211_MAX_SUPP_RATES - srlen; 137 if (clen > elems->ext_supp_rates_len) 138 clen = elems->ext_supp_rates_len; 139 memcpy(bss->supp_rates + srlen, elems->ext_supp_rates, 140 clen); 141 srlen += clen; 142 } 143 if (srlen) { 144 bss->supp_rates_len = srlen; 145 if (!elems->parse_error) 146 bss->valid_data |= IEEE80211_BSS_VALID_RATES; 147 } 148 } 149 150 if (!elems->parse_error || 151 !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) { 152 bss->wmm_used = elems->wmm_param || elems->wmm_info; 153 bss->uapsd_supported = is_uapsd_supported(elems); 154 if (!elems->parse_error) 155 bss->valid_data |= IEEE80211_BSS_VALID_WMM; 156 } 157 158 if (beacon) { 159 struct ieee80211_supported_band *sband = 160 local->hw.wiphy->bands[rx_status->band]; 161 if (!(rx_status->flag & RX_FLAG_HT) && 162 !(rx_status->flag & RX_FLAG_VHT)) 163 bss->beacon_rate = 164 &sband->bitrates[rx_status->rate_idx]; 165 } 166 167 return bss; 168 } 169 170 void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb) 171 { 172 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb); 173 struct ieee80211_sub_if_data *sdata1, *sdata2; 174 struct ieee80211_mgmt *mgmt = (void *)skb->data; 175 struct ieee80211_bss *bss; 176 u8 *elements; 177 struct ieee80211_channel *channel; 178 size_t baselen; 179 struct ieee802_11_elems elems; 180 181 if (skb->len < 24 || 182 (!ieee80211_is_probe_resp(mgmt->frame_control) && 183 !ieee80211_is_beacon(mgmt->frame_control))) 184 return; 185 186 sdata1 = rcu_dereference(local->scan_sdata); 187 sdata2 = rcu_dereference(local->sched_scan_sdata); 188 189 if (likely(!sdata1 && !sdata2)) 190 return; 191 192 if (ieee80211_is_probe_resp(mgmt->frame_control)) { 193 struct cfg80211_scan_request *scan_req; 194 struct cfg80211_sched_scan_request *sched_scan_req; 195 196 scan_req = rcu_dereference(local->scan_req); 197 sched_scan_req = rcu_dereference(local->sched_scan_req); 198 199 /* ignore ProbeResp to foreign address unless scanning 200 * with randomised address 201 */ 202 if (!(sdata1 && 203 (ether_addr_equal(mgmt->da, sdata1->vif.addr) || 204 scan_req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)) && 205 !(sdata2 && 206 (ether_addr_equal(mgmt->da, sdata2->vif.addr) || 207 sched_scan_req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR))) 208 return; 209 210 elements = mgmt->u.probe_resp.variable; 211 baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable); 212 } else { 213 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable); 214 elements = mgmt->u.beacon.variable; 215 } 216 217 if (baselen > skb->len) 218 return; 219 220 ieee802_11_parse_elems(elements, skb->len - baselen, false, &elems); 221 222 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq); 223 224 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED) 225 return; 226 227 bss = ieee80211_bss_info_update(local, rx_status, 228 mgmt, skb->len, &elems, 229 channel); 230 if (bss) 231 ieee80211_rx_bss_put(local, bss); 232 } 233 234 static void 235 ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef, 236 enum nl80211_bss_scan_width scan_width) 237 { 238 memset(chandef, 0, sizeof(*chandef)); 239 switch (scan_width) { 240 case NL80211_BSS_CHAN_WIDTH_5: 241 chandef->width = NL80211_CHAN_WIDTH_5; 242 break; 243 case NL80211_BSS_CHAN_WIDTH_10: 244 chandef->width = NL80211_CHAN_WIDTH_10; 245 break; 246 default: 247 chandef->width = NL80211_CHAN_WIDTH_20_NOHT; 248 break; 249 } 250 } 251 252 /* return false if no more work */ 253 static bool ieee80211_prep_hw_scan(struct ieee80211_local *local) 254 { 255 struct cfg80211_scan_request *req; 256 struct cfg80211_chan_def chandef; 257 u8 bands_used = 0; 258 int i, ielen, n_chans; 259 260 req = rcu_dereference_protected(local->scan_req, 261 lockdep_is_held(&local->mtx)); 262 263 if (test_bit(SCAN_HW_CANCELLED, &local->scanning)) 264 return false; 265 266 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) { 267 for (i = 0; i < req->n_channels; i++) { 268 local->hw_scan_req->req.channels[i] = req->channels[i]; 269 bands_used |= BIT(req->channels[i]->band); 270 } 271 272 n_chans = req->n_channels; 273 } else { 274 do { 275 if (local->hw_scan_band == NUM_NL80211_BANDS) 276 return false; 277 278 n_chans = 0; 279 280 for (i = 0; i < req->n_channels; i++) { 281 if (req->channels[i]->band != 282 local->hw_scan_band) 283 continue; 284 local->hw_scan_req->req.channels[n_chans] = 285 req->channels[i]; 286 n_chans++; 287 bands_used |= BIT(req->channels[i]->band); 288 } 289 290 local->hw_scan_band++; 291 } while (!n_chans); 292 } 293 294 local->hw_scan_req->req.n_channels = n_chans; 295 ieee80211_prepare_scan_chandef(&chandef, req->scan_width); 296 297 ielen = ieee80211_build_preq_ies(local, 298 (u8 *)local->hw_scan_req->req.ie, 299 local->hw_scan_ies_bufsize, 300 &local->hw_scan_req->ies, 301 req->ie, req->ie_len, 302 bands_used, req->rates, &chandef); 303 local->hw_scan_req->req.ie_len = ielen; 304 local->hw_scan_req->req.no_cck = req->no_cck; 305 ether_addr_copy(local->hw_scan_req->req.mac_addr, req->mac_addr); 306 ether_addr_copy(local->hw_scan_req->req.mac_addr_mask, 307 req->mac_addr_mask); 308 ether_addr_copy(local->hw_scan_req->req.bssid, req->bssid); 309 310 return true; 311 } 312 313 static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted) 314 { 315 struct ieee80211_local *local = hw_to_local(hw); 316 bool hw_scan = local->ops->hw_scan; 317 bool was_scanning = local->scanning; 318 struct cfg80211_scan_request *scan_req; 319 struct ieee80211_sub_if_data *scan_sdata; 320 struct ieee80211_sub_if_data *sdata; 321 322 lockdep_assert_held(&local->mtx); 323 324 /* 325 * It's ok to abort a not-yet-running scan (that 326 * we have one at all will be verified by checking 327 * local->scan_req next), but not to complete it 328 * successfully. 329 */ 330 if (WARN_ON(!local->scanning && !aborted)) 331 aborted = true; 332 333 if (WARN_ON(!local->scan_req)) 334 return; 335 336 if (hw_scan && !aborted && 337 !ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS) && 338 ieee80211_prep_hw_scan(local)) { 339 int rc; 340 341 rc = drv_hw_scan(local, 342 rcu_dereference_protected(local->scan_sdata, 343 lockdep_is_held(&local->mtx)), 344 local->hw_scan_req); 345 346 if (rc == 0) 347 return; 348 } 349 350 kfree(local->hw_scan_req); 351 local->hw_scan_req = NULL; 352 353 scan_req = rcu_dereference_protected(local->scan_req, 354 lockdep_is_held(&local->mtx)); 355 356 if (scan_req != local->int_scan_req) { 357 struct cfg80211_scan_info info = { 358 .aborted = aborted, 359 }; 360 361 cfg80211_scan_done(scan_req, &info); 362 } 363 RCU_INIT_POINTER(local->scan_req, NULL); 364 365 scan_sdata = rcu_dereference_protected(local->scan_sdata, 366 lockdep_is_held(&local->mtx)); 367 RCU_INIT_POINTER(local->scan_sdata, NULL); 368 369 local->scanning = 0; 370 local->scan_chandef.chan = NULL; 371 372 /* Set power back to normal operating levels. */ 373 ieee80211_hw_config(local, 0); 374 375 if (!hw_scan) { 376 ieee80211_configure_filter(local); 377 drv_sw_scan_complete(local, scan_sdata); 378 ieee80211_offchannel_return(local); 379 } 380 381 ieee80211_recalc_idle(local); 382 383 ieee80211_mlme_notify_scan_completed(local); 384 ieee80211_ibss_notify_scan_completed(local); 385 386 /* Requeue all the work that might have been ignored while 387 * the scan was in progress; if there was none this will 388 * just be a no-op for the particular interface. 389 */ 390 list_for_each_entry_rcu(sdata, &local->interfaces, list) { 391 if (ieee80211_sdata_running(sdata)) 392 ieee80211_queue_work(&sdata->local->hw, &sdata->work); 393 } 394 395 if (was_scanning) 396 ieee80211_start_next_roc(local); 397 } 398 399 void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted) 400 { 401 struct ieee80211_local *local = hw_to_local(hw); 402 403 trace_api_scan_completed(local, aborted); 404 405 set_bit(SCAN_COMPLETED, &local->scanning); 406 if (aborted) 407 set_bit(SCAN_ABORTED, &local->scanning); 408 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0); 409 } 410 EXPORT_SYMBOL(ieee80211_scan_completed); 411 412 static int ieee80211_start_sw_scan(struct ieee80211_local *local, 413 struct ieee80211_sub_if_data *sdata) 414 { 415 /* Software scan is not supported in multi-channel cases */ 416 if (local->use_chanctx) 417 return -EOPNOTSUPP; 418 419 /* 420 * Hardware/driver doesn't support hw_scan, so use software 421 * scanning instead. First send a nullfunc frame with power save 422 * bit on so that AP will buffer the frames for us while we are not 423 * listening, then send probe requests to each channel and wait for 424 * the responses. After all channels are scanned, tune back to the 425 * original channel and send a nullfunc frame with power save bit 426 * off to trigger the AP to send us all the buffered frames. 427 * 428 * Note that while local->sw_scanning is true everything else but 429 * nullfunc frames and probe requests will be dropped in 430 * ieee80211_tx_h_check_assoc(). 431 */ 432 drv_sw_scan_start(local, sdata, local->scan_addr); 433 434 local->leave_oper_channel_time = jiffies; 435 local->next_scan_state = SCAN_DECISION; 436 local->scan_channel_idx = 0; 437 438 ieee80211_offchannel_stop_vifs(local); 439 440 /* ensure nullfunc is transmitted before leaving operating channel */ 441 ieee80211_flush_queues(local, NULL, false); 442 443 ieee80211_configure_filter(local); 444 445 /* We need to set power level at maximum rate for scanning. */ 446 ieee80211_hw_config(local, 0); 447 448 ieee80211_queue_delayed_work(&local->hw, 449 &local->scan_work, 0); 450 451 return 0; 452 } 453 454 static bool ieee80211_can_scan(struct ieee80211_local *local, 455 struct ieee80211_sub_if_data *sdata) 456 { 457 if (ieee80211_is_radar_required(local)) 458 return false; 459 460 if (!list_empty(&local->roc_list)) 461 return false; 462 463 if (sdata->vif.type == NL80211_IFTYPE_STATION && 464 sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL) 465 return false; 466 467 return true; 468 } 469 470 void ieee80211_run_deferred_scan(struct ieee80211_local *local) 471 { 472 lockdep_assert_held(&local->mtx); 473 474 if (!local->scan_req || local->scanning) 475 return; 476 477 if (!ieee80211_can_scan(local, 478 rcu_dereference_protected( 479 local->scan_sdata, 480 lockdep_is_held(&local->mtx)))) 481 return; 482 483 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 484 round_jiffies_relative(0)); 485 } 486 487 static void ieee80211_scan_state_send_probe(struct ieee80211_local *local, 488 unsigned long *next_delay) 489 { 490 int i; 491 struct ieee80211_sub_if_data *sdata; 492 struct cfg80211_scan_request *scan_req; 493 enum nl80211_band band = local->hw.conf.chandef.chan->band; 494 u32 tx_flags; 495 496 scan_req = rcu_dereference_protected(local->scan_req, 497 lockdep_is_held(&local->mtx)); 498 499 tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK; 500 if (scan_req->no_cck) 501 tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE; 502 503 sdata = rcu_dereference_protected(local->scan_sdata, 504 lockdep_is_held(&local->mtx)); 505 506 for (i = 0; i < scan_req->n_ssids; i++) 507 ieee80211_send_probe_req( 508 sdata, local->scan_addr, scan_req->bssid, 509 scan_req->ssids[i].ssid, scan_req->ssids[i].ssid_len, 510 scan_req->ie, scan_req->ie_len, 511 scan_req->rates[band], false, 512 tx_flags, local->hw.conf.chandef.chan, true); 513 514 /* 515 * After sending probe requests, wait for probe responses 516 * on the channel. 517 */ 518 *next_delay = IEEE80211_CHANNEL_TIME; 519 local->next_scan_state = SCAN_DECISION; 520 } 521 522 static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata, 523 struct cfg80211_scan_request *req) 524 { 525 struct ieee80211_local *local = sdata->local; 526 int rc; 527 528 lockdep_assert_held(&local->mtx); 529 530 if (local->scan_req || ieee80211_is_radar_required(local)) 531 return -EBUSY; 532 533 if (!ieee80211_can_scan(local, sdata)) { 534 /* wait for the work to finish/time out */ 535 rcu_assign_pointer(local->scan_req, req); 536 rcu_assign_pointer(local->scan_sdata, sdata); 537 return 0; 538 } 539 540 if (local->ops->hw_scan) { 541 u8 *ies; 542 543 local->hw_scan_ies_bufsize = local->scan_ies_len + req->ie_len; 544 545 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) { 546 int i, n_bands = 0; 547 u8 bands_counted = 0; 548 549 for (i = 0; i < req->n_channels; i++) { 550 if (bands_counted & BIT(req->channels[i]->band)) 551 continue; 552 bands_counted |= BIT(req->channels[i]->band); 553 n_bands++; 554 } 555 556 local->hw_scan_ies_bufsize *= n_bands; 557 } 558 559 local->hw_scan_req = kmalloc( 560 sizeof(*local->hw_scan_req) + 561 req->n_channels * sizeof(req->channels[0]) + 562 local->hw_scan_ies_bufsize, GFP_KERNEL); 563 if (!local->hw_scan_req) 564 return -ENOMEM; 565 566 local->hw_scan_req->req.ssids = req->ssids; 567 local->hw_scan_req->req.n_ssids = req->n_ssids; 568 ies = (u8 *)local->hw_scan_req + 569 sizeof(*local->hw_scan_req) + 570 req->n_channels * sizeof(req->channels[0]); 571 local->hw_scan_req->req.ie = ies; 572 local->hw_scan_req->req.flags = req->flags; 573 eth_broadcast_addr(local->hw_scan_req->req.bssid); 574 575 local->hw_scan_band = 0; 576 577 /* 578 * After allocating local->hw_scan_req, we must 579 * go through until ieee80211_prep_hw_scan(), so 580 * anything that might be changed here and leave 581 * this function early must not go after this 582 * allocation. 583 */ 584 } 585 586 rcu_assign_pointer(local->scan_req, req); 587 rcu_assign_pointer(local->scan_sdata, sdata); 588 589 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) 590 get_random_mask_addr(local->scan_addr, 591 req->mac_addr, 592 req->mac_addr_mask); 593 else 594 memcpy(local->scan_addr, sdata->vif.addr, ETH_ALEN); 595 596 if (local->ops->hw_scan) { 597 __set_bit(SCAN_HW_SCANNING, &local->scanning); 598 } else if ((req->n_channels == 1) && 599 (req->channels[0] == local->_oper_chandef.chan)) { 600 /* 601 * If we are scanning only on the operating channel 602 * then we do not need to stop normal activities 603 */ 604 unsigned long next_delay; 605 606 __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning); 607 608 ieee80211_recalc_idle(local); 609 610 /* Notify driver scan is starting, keep order of operations 611 * same as normal software scan, in case that matters. */ 612 drv_sw_scan_start(local, sdata, local->scan_addr); 613 614 ieee80211_configure_filter(local); /* accept probe-responses */ 615 616 /* We need to ensure power level is at max for scanning. */ 617 ieee80211_hw_config(local, 0); 618 619 if ((req->channels[0]->flags & (IEEE80211_CHAN_NO_IR | 620 IEEE80211_CHAN_RADAR)) || 621 !req->n_ssids) { 622 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 623 } else { 624 ieee80211_scan_state_send_probe(local, &next_delay); 625 next_delay = IEEE80211_CHANNEL_TIME; 626 } 627 628 /* Now, just wait a bit and we are all done! */ 629 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 630 next_delay); 631 return 0; 632 } else { 633 /* Do normal software scan */ 634 __set_bit(SCAN_SW_SCANNING, &local->scanning); 635 } 636 637 ieee80211_recalc_idle(local); 638 639 if (local->ops->hw_scan) { 640 WARN_ON(!ieee80211_prep_hw_scan(local)); 641 rc = drv_hw_scan(local, sdata, local->hw_scan_req); 642 } else { 643 rc = ieee80211_start_sw_scan(local, sdata); 644 } 645 646 if (rc) { 647 kfree(local->hw_scan_req); 648 local->hw_scan_req = NULL; 649 local->scanning = 0; 650 651 ieee80211_recalc_idle(local); 652 653 local->scan_req = NULL; 654 RCU_INIT_POINTER(local->scan_sdata, NULL); 655 } 656 657 return rc; 658 } 659 660 static unsigned long 661 ieee80211_scan_get_channel_time(struct ieee80211_channel *chan) 662 { 663 /* 664 * TODO: channel switching also consumes quite some time, 665 * add that delay as well to get a better estimation 666 */ 667 if (chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) 668 return IEEE80211_PASSIVE_CHANNEL_TIME; 669 return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME; 670 } 671 672 static void ieee80211_scan_state_decision(struct ieee80211_local *local, 673 unsigned long *next_delay) 674 { 675 bool associated = false; 676 bool tx_empty = true; 677 bool bad_latency; 678 struct ieee80211_sub_if_data *sdata; 679 struct ieee80211_channel *next_chan; 680 enum mac80211_scan_state next_scan_state; 681 struct cfg80211_scan_request *scan_req; 682 683 /* 684 * check if at least one STA interface is associated, 685 * check if at least one STA interface has pending tx frames 686 * and grab the lowest used beacon interval 687 */ 688 mutex_lock(&local->iflist_mtx); 689 list_for_each_entry(sdata, &local->interfaces, list) { 690 if (!ieee80211_sdata_running(sdata)) 691 continue; 692 693 if (sdata->vif.type == NL80211_IFTYPE_STATION) { 694 if (sdata->u.mgd.associated) { 695 associated = true; 696 697 if (!qdisc_all_tx_empty(sdata->dev)) { 698 tx_empty = false; 699 break; 700 } 701 } 702 } 703 } 704 mutex_unlock(&local->iflist_mtx); 705 706 scan_req = rcu_dereference_protected(local->scan_req, 707 lockdep_is_held(&local->mtx)); 708 709 next_chan = scan_req->channels[local->scan_channel_idx]; 710 711 /* 712 * we're currently scanning a different channel, let's 713 * see if we can scan another channel without interfering 714 * with the current traffic situation. 715 * 716 * Keep good latency, do not stay off-channel more than 125 ms. 717 */ 718 719 bad_latency = time_after(jiffies + 720 ieee80211_scan_get_channel_time(next_chan), 721 local->leave_oper_channel_time + HZ / 8); 722 723 if (associated && !tx_empty) { 724 if (scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) 725 next_scan_state = SCAN_ABORT; 726 else 727 next_scan_state = SCAN_SUSPEND; 728 } else if (associated && bad_latency) { 729 next_scan_state = SCAN_SUSPEND; 730 } else { 731 next_scan_state = SCAN_SET_CHANNEL; 732 } 733 734 local->next_scan_state = next_scan_state; 735 736 *next_delay = 0; 737 } 738 739 static void ieee80211_scan_state_set_channel(struct ieee80211_local *local, 740 unsigned long *next_delay) 741 { 742 int skip; 743 struct ieee80211_channel *chan; 744 enum nl80211_bss_scan_width oper_scan_width; 745 struct cfg80211_scan_request *scan_req; 746 747 scan_req = rcu_dereference_protected(local->scan_req, 748 lockdep_is_held(&local->mtx)); 749 750 skip = 0; 751 chan = scan_req->channels[local->scan_channel_idx]; 752 753 local->scan_chandef.chan = chan; 754 local->scan_chandef.center_freq1 = chan->center_freq; 755 local->scan_chandef.center_freq2 = 0; 756 switch (scan_req->scan_width) { 757 case NL80211_BSS_CHAN_WIDTH_5: 758 local->scan_chandef.width = NL80211_CHAN_WIDTH_5; 759 break; 760 case NL80211_BSS_CHAN_WIDTH_10: 761 local->scan_chandef.width = NL80211_CHAN_WIDTH_10; 762 break; 763 case NL80211_BSS_CHAN_WIDTH_20: 764 /* If scanning on oper channel, use whatever channel-type 765 * is currently in use. 766 */ 767 oper_scan_width = cfg80211_chandef_to_scan_width( 768 &local->_oper_chandef); 769 if (chan == local->_oper_chandef.chan && 770 oper_scan_width == scan_req->scan_width) 771 local->scan_chandef = local->_oper_chandef; 772 else 773 local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT; 774 break; 775 } 776 777 if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL)) 778 skip = 1; 779 780 /* advance state machine to next channel/band */ 781 local->scan_channel_idx++; 782 783 if (skip) { 784 /* if we skip this channel return to the decision state */ 785 local->next_scan_state = SCAN_DECISION; 786 return; 787 } 788 789 /* 790 * Probe delay is used to update the NAV, cf. 11.1.3.2.2 791 * (which unfortunately doesn't say _why_ step a) is done, 792 * but it waits for the probe delay or until a frame is 793 * received - and the received frame would update the NAV). 794 * For now, we do not support waiting until a frame is 795 * received. 796 * 797 * In any case, it is not necessary for a passive scan. 798 */ 799 if ((chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) || 800 !scan_req->n_ssids) { 801 *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME; 802 local->next_scan_state = SCAN_DECISION; 803 return; 804 } 805 806 /* active scan, send probes */ 807 *next_delay = IEEE80211_PROBE_DELAY; 808 local->next_scan_state = SCAN_SEND_PROBE; 809 } 810 811 static void ieee80211_scan_state_suspend(struct ieee80211_local *local, 812 unsigned long *next_delay) 813 { 814 /* switch back to the operating channel */ 815 local->scan_chandef.chan = NULL; 816 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); 817 818 /* disable PS */ 819 ieee80211_offchannel_return(local); 820 821 *next_delay = HZ / 5; 822 /* afterwards, resume scan & go to next channel */ 823 local->next_scan_state = SCAN_RESUME; 824 } 825 826 static void ieee80211_scan_state_resume(struct ieee80211_local *local, 827 unsigned long *next_delay) 828 { 829 ieee80211_offchannel_stop_vifs(local); 830 831 if (local->ops->flush) { 832 ieee80211_flush_queues(local, NULL, false); 833 *next_delay = 0; 834 } else 835 *next_delay = HZ / 10; 836 837 /* remember when we left the operating channel */ 838 local->leave_oper_channel_time = jiffies; 839 840 /* advance to the next channel to be scanned */ 841 local->next_scan_state = SCAN_SET_CHANNEL; 842 } 843 844 void ieee80211_scan_work(struct work_struct *work) 845 { 846 struct ieee80211_local *local = 847 container_of(work, struct ieee80211_local, scan_work.work); 848 struct ieee80211_sub_if_data *sdata; 849 struct cfg80211_scan_request *scan_req; 850 unsigned long next_delay = 0; 851 bool aborted; 852 853 mutex_lock(&local->mtx); 854 855 if (!ieee80211_can_run_worker(local)) { 856 aborted = true; 857 goto out_complete; 858 } 859 860 sdata = rcu_dereference_protected(local->scan_sdata, 861 lockdep_is_held(&local->mtx)); 862 scan_req = rcu_dereference_protected(local->scan_req, 863 lockdep_is_held(&local->mtx)); 864 865 /* When scanning on-channel, the first-callback means completed. */ 866 if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) { 867 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 868 goto out_complete; 869 } 870 871 if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) { 872 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning); 873 goto out_complete; 874 } 875 876 if (!sdata || !scan_req) 877 goto out; 878 879 if (!local->scanning) { 880 int rc; 881 882 RCU_INIT_POINTER(local->scan_req, NULL); 883 RCU_INIT_POINTER(local->scan_sdata, NULL); 884 885 rc = __ieee80211_start_scan(sdata, scan_req); 886 if (rc) { 887 /* need to complete scan in cfg80211 */ 888 rcu_assign_pointer(local->scan_req, scan_req); 889 aborted = true; 890 goto out_complete; 891 } else 892 goto out; 893 } 894 895 /* 896 * as long as no delay is required advance immediately 897 * without scheduling a new work 898 */ 899 do { 900 if (!ieee80211_sdata_running(sdata)) { 901 aborted = true; 902 goto out_complete; 903 } 904 905 switch (local->next_scan_state) { 906 case SCAN_DECISION: 907 /* if no more bands/channels left, complete scan */ 908 if (local->scan_channel_idx >= scan_req->n_channels) { 909 aborted = false; 910 goto out_complete; 911 } 912 ieee80211_scan_state_decision(local, &next_delay); 913 break; 914 case SCAN_SET_CHANNEL: 915 ieee80211_scan_state_set_channel(local, &next_delay); 916 break; 917 case SCAN_SEND_PROBE: 918 ieee80211_scan_state_send_probe(local, &next_delay); 919 break; 920 case SCAN_SUSPEND: 921 ieee80211_scan_state_suspend(local, &next_delay); 922 break; 923 case SCAN_RESUME: 924 ieee80211_scan_state_resume(local, &next_delay); 925 break; 926 case SCAN_ABORT: 927 aborted = true; 928 goto out_complete; 929 } 930 } while (next_delay == 0); 931 932 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay); 933 goto out; 934 935 out_complete: 936 __ieee80211_scan_completed(&local->hw, aborted); 937 out: 938 mutex_unlock(&local->mtx); 939 } 940 941 int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata, 942 struct cfg80211_scan_request *req) 943 { 944 int res; 945 946 mutex_lock(&sdata->local->mtx); 947 res = __ieee80211_start_scan(sdata, req); 948 mutex_unlock(&sdata->local->mtx); 949 950 return res; 951 } 952 953 int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata, 954 const u8 *ssid, u8 ssid_len, 955 struct ieee80211_channel **channels, 956 unsigned int n_channels, 957 enum nl80211_bss_scan_width scan_width) 958 { 959 struct ieee80211_local *local = sdata->local; 960 int ret = -EBUSY, i, n_ch = 0; 961 enum nl80211_band band; 962 963 mutex_lock(&local->mtx); 964 965 /* busy scanning */ 966 if (local->scan_req) 967 goto unlock; 968 969 /* fill internal scan request */ 970 if (!channels) { 971 int max_n; 972 973 for (band = 0; band < NUM_NL80211_BANDS; band++) { 974 if (!local->hw.wiphy->bands[band]) 975 continue; 976 977 max_n = local->hw.wiphy->bands[band]->n_channels; 978 for (i = 0; i < max_n; i++) { 979 struct ieee80211_channel *tmp_ch = 980 &local->hw.wiphy->bands[band]->channels[i]; 981 982 if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR | 983 IEEE80211_CHAN_DISABLED)) 984 continue; 985 986 local->int_scan_req->channels[n_ch] = tmp_ch; 987 n_ch++; 988 } 989 } 990 991 if (WARN_ON_ONCE(n_ch == 0)) 992 goto unlock; 993 994 local->int_scan_req->n_channels = n_ch; 995 } else { 996 for (i = 0; i < n_channels; i++) { 997 if (channels[i]->flags & (IEEE80211_CHAN_NO_IR | 998 IEEE80211_CHAN_DISABLED)) 999 continue; 1000 1001 local->int_scan_req->channels[n_ch] = channels[i]; 1002 n_ch++; 1003 } 1004 1005 if (WARN_ON_ONCE(n_ch == 0)) 1006 goto unlock; 1007 1008 local->int_scan_req->n_channels = n_ch; 1009 } 1010 1011 local->int_scan_req->ssids = &local->scan_ssid; 1012 local->int_scan_req->n_ssids = 1; 1013 local->int_scan_req->scan_width = scan_width; 1014 memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN); 1015 local->int_scan_req->ssids[0].ssid_len = ssid_len; 1016 1017 ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req); 1018 unlock: 1019 mutex_unlock(&local->mtx); 1020 return ret; 1021 } 1022 1023 /* 1024 * Only call this function when a scan can't be queued -- under RTNL. 1025 */ 1026 void ieee80211_scan_cancel(struct ieee80211_local *local) 1027 { 1028 /* 1029 * We are canceling software scan, or deferred scan that was not 1030 * yet really started (see __ieee80211_start_scan ). 1031 * 1032 * Regarding hardware scan: 1033 * - we can not call __ieee80211_scan_completed() as when 1034 * SCAN_HW_SCANNING bit is set this function change 1035 * local->hw_scan_req to operate on 5G band, what race with 1036 * driver which can use local->hw_scan_req 1037 * 1038 * - we can not cancel scan_work since driver can schedule it 1039 * by ieee80211_scan_completed(..., true) to finish scan 1040 * 1041 * Hence we only call the cancel_hw_scan() callback, but the low-level 1042 * driver is still responsible for calling ieee80211_scan_completed() 1043 * after the scan was completed/aborted. 1044 */ 1045 1046 mutex_lock(&local->mtx); 1047 if (!local->scan_req) 1048 goto out; 1049 1050 /* 1051 * We have a scan running and the driver already reported completion, 1052 * but the worker hasn't run yet or is stuck on the mutex - mark it as 1053 * cancelled. 1054 */ 1055 if (test_bit(SCAN_HW_SCANNING, &local->scanning) && 1056 test_bit(SCAN_COMPLETED, &local->scanning)) { 1057 set_bit(SCAN_HW_CANCELLED, &local->scanning); 1058 goto out; 1059 } 1060 1061 if (test_bit(SCAN_HW_SCANNING, &local->scanning)) { 1062 /* 1063 * Make sure that __ieee80211_scan_completed doesn't trigger a 1064 * scan on another band. 1065 */ 1066 set_bit(SCAN_HW_CANCELLED, &local->scanning); 1067 if (local->ops->cancel_hw_scan) 1068 drv_cancel_hw_scan(local, 1069 rcu_dereference_protected(local->scan_sdata, 1070 lockdep_is_held(&local->mtx))); 1071 goto out; 1072 } 1073 1074 /* 1075 * If the work is currently running, it must be blocked on 1076 * the mutex, but we'll set scan_sdata = NULL and it'll 1077 * simply exit once it acquires the mutex. 1078 */ 1079 cancel_delayed_work(&local->scan_work); 1080 /* and clean up */ 1081 __ieee80211_scan_completed(&local->hw, true); 1082 out: 1083 mutex_unlock(&local->mtx); 1084 } 1085 1086 int __ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata, 1087 struct cfg80211_sched_scan_request *req) 1088 { 1089 struct ieee80211_local *local = sdata->local; 1090 struct ieee80211_scan_ies sched_scan_ies = {}; 1091 struct cfg80211_chan_def chandef; 1092 int ret, i, iebufsz, num_bands = 0; 1093 u32 rate_masks[NUM_NL80211_BANDS] = {}; 1094 u8 bands_used = 0; 1095 u8 *ie; 1096 size_t len; 1097 1098 iebufsz = local->scan_ies_len + req->ie_len; 1099 1100 lockdep_assert_held(&local->mtx); 1101 1102 if (!local->ops->sched_scan_start) 1103 return -ENOTSUPP; 1104 1105 for (i = 0; i < NUM_NL80211_BANDS; i++) { 1106 if (local->hw.wiphy->bands[i]) { 1107 bands_used |= BIT(i); 1108 rate_masks[i] = (u32) -1; 1109 num_bands++; 1110 } 1111 } 1112 1113 ie = kzalloc(num_bands * iebufsz, GFP_KERNEL); 1114 if (!ie) { 1115 ret = -ENOMEM; 1116 goto out; 1117 } 1118 1119 ieee80211_prepare_scan_chandef(&chandef, req->scan_width); 1120 1121 len = ieee80211_build_preq_ies(local, ie, num_bands * iebufsz, 1122 &sched_scan_ies, req->ie, 1123 req->ie_len, bands_used, 1124 rate_masks, &chandef); 1125 1126 ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies); 1127 if (ret == 0) { 1128 rcu_assign_pointer(local->sched_scan_sdata, sdata); 1129 rcu_assign_pointer(local->sched_scan_req, req); 1130 } 1131 1132 kfree(ie); 1133 1134 out: 1135 if (ret) { 1136 /* Clean in case of failure after HW restart or upon resume. */ 1137 RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 1138 RCU_INIT_POINTER(local->sched_scan_req, NULL); 1139 } 1140 1141 return ret; 1142 } 1143 1144 int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata, 1145 struct cfg80211_sched_scan_request *req) 1146 { 1147 struct ieee80211_local *local = sdata->local; 1148 int ret; 1149 1150 mutex_lock(&local->mtx); 1151 1152 if (rcu_access_pointer(local->sched_scan_sdata)) { 1153 mutex_unlock(&local->mtx); 1154 return -EBUSY; 1155 } 1156 1157 ret = __ieee80211_request_sched_scan_start(sdata, req); 1158 1159 mutex_unlock(&local->mtx); 1160 return ret; 1161 } 1162 1163 int ieee80211_request_sched_scan_stop(struct ieee80211_local *local) 1164 { 1165 struct ieee80211_sub_if_data *sched_scan_sdata; 1166 int ret = -ENOENT; 1167 1168 mutex_lock(&local->mtx); 1169 1170 if (!local->ops->sched_scan_stop) { 1171 ret = -ENOTSUPP; 1172 goto out; 1173 } 1174 1175 /* We don't want to restart sched scan anymore. */ 1176 RCU_INIT_POINTER(local->sched_scan_req, NULL); 1177 1178 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata, 1179 lockdep_is_held(&local->mtx)); 1180 if (sched_scan_sdata) { 1181 ret = drv_sched_scan_stop(local, sched_scan_sdata); 1182 if (!ret) 1183 RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 1184 } 1185 out: 1186 mutex_unlock(&local->mtx); 1187 1188 return ret; 1189 } 1190 1191 void ieee80211_sched_scan_results(struct ieee80211_hw *hw) 1192 { 1193 struct ieee80211_local *local = hw_to_local(hw); 1194 1195 trace_api_sched_scan_results(local); 1196 1197 cfg80211_sched_scan_results(hw->wiphy); 1198 } 1199 EXPORT_SYMBOL(ieee80211_sched_scan_results); 1200 1201 void ieee80211_sched_scan_end(struct ieee80211_local *local) 1202 { 1203 mutex_lock(&local->mtx); 1204 1205 if (!rcu_access_pointer(local->sched_scan_sdata)) { 1206 mutex_unlock(&local->mtx); 1207 return; 1208 } 1209 1210 RCU_INIT_POINTER(local->sched_scan_sdata, NULL); 1211 1212 /* If sched scan was aborted by the driver. */ 1213 RCU_INIT_POINTER(local->sched_scan_req, NULL); 1214 1215 mutex_unlock(&local->mtx); 1216 1217 cfg80211_sched_scan_stopped(local->hw.wiphy); 1218 } 1219 1220 void ieee80211_sched_scan_stopped_work(struct work_struct *work) 1221 { 1222 struct ieee80211_local *local = 1223 container_of(work, struct ieee80211_local, 1224 sched_scan_stopped_work); 1225 1226 ieee80211_sched_scan_end(local); 1227 } 1228 1229 void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw) 1230 { 1231 struct ieee80211_local *local = hw_to_local(hw); 1232 1233 trace_api_sched_scan_stopped(local); 1234 1235 /* 1236 * this shouldn't really happen, so for simplicity 1237 * simply ignore it, and let mac80211 reconfigure 1238 * the sched scan later on. 1239 */ 1240 if (local->in_reconfig) 1241 return; 1242 1243 schedule_work(&local->sched_scan_stopped_work); 1244 } 1245 EXPORT_SYMBOL(ieee80211_sched_scan_stopped); 1246