2 * Scanning implementation
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 * Copyright 2016-2017 Intel Deutschland GmbH
11 * Copyright (C) 2018-2019 Intel Corporation
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #include <linux/if_arp.h>
19 #include <linux/etherdevice.h>
20 #include <linux/rtnetlink.h>
21 #include <net/sch_generic.h>
22 #include <linux/slab.h>
23 #include <linux/export.h>
24 #include <linux/random.h>
25 #include <net/mac80211.h>
27 #include "ieee80211_i.h"
28 #include "driver-ops.h"
31 #define IEEE80211_PROBE_DELAY (HZ / 33)
32 #define IEEE80211_CHANNEL_TIME (HZ / 33)
33 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 9)
35 void ieee80211_rx_bss_put(struct ieee80211_local *local,
36 struct ieee80211_bss *bss)
40 cfg80211_put_bss(local->hw.wiphy,
41 container_of((void *)bss, struct cfg80211_bss, priv));
44 static bool is_uapsd_supported(struct ieee802_11_elems *elems)
48 if (elems->wmm_info && elems->wmm_info_len == 7
49 && elems->wmm_info[5] == 1)
50 qos_info = elems->wmm_info[6];
51 else if (elems->wmm_param && elems->wmm_param_len == 24
52 && elems->wmm_param[5] == 1)
53 qos_info = elems->wmm_param[6];
55 /* no valid wmm information or parameter element found */
58 return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD;
61 struct ieee80211_bss *
62 ieee80211_bss_info_update(struct ieee80211_local *local,
63 struct ieee80211_rx_status *rx_status,
64 struct ieee80211_mgmt *mgmt, size_t len,
65 struct ieee80211_channel *channel)
67 bool beacon = ieee80211_is_beacon(mgmt->frame_control);
68 struct cfg80211_bss *cbss;
69 struct ieee80211_bss *bss;
71 struct cfg80211_inform_bss bss_meta = {
72 .boottime_ns = rx_status->boottime_ns,
75 struct ieee80211_sub_if_data *scan_sdata;
76 struct ieee802_11_elems elems;
80 if (rx_status->flag & RX_FLAG_NO_SIGNAL_VAL)
81 bss_meta.signal = 0; /* invalid signal indication */
82 else if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
83 bss_meta.signal = rx_status->signal * 100;
84 else if (ieee80211_hw_check(&local->hw, SIGNAL_UNSPEC))
85 bss_meta.signal = (rx_status->signal * 100) / local->hw.max_signal;
87 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_20;
88 if (rx_status->bw == RATE_INFO_BW_5)
89 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_5;
90 else if (rx_status->bw == RATE_INFO_BW_10)
91 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_10;
93 bss_meta.chan = channel;
96 scan_sdata = rcu_dereference(local->scan_sdata);
97 if (scan_sdata && scan_sdata->vif.type == NL80211_IFTYPE_STATION &&
98 scan_sdata->vif.bss_conf.assoc &&
99 ieee80211_have_rx_timestamp(rx_status)) {
100 bss_meta.parent_tsf =
101 ieee80211_calculate_rx_timestamp(local, rx_status,
103 ether_addr_copy(bss_meta.parent_bssid,
104 scan_sdata->vif.bss_conf.bssid);
108 cbss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta,
109 mgmt, len, GFP_ATOMIC);
113 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
114 elements = mgmt->u.probe_resp.variable;
115 baselen = offsetof(struct ieee80211_mgmt,
116 u.probe_resp.variable);
118 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
119 elements = mgmt->u.beacon.variable;
125 ieee802_11_parse_elems(elements, len - baselen, false, &elems,
126 mgmt->bssid, cbss->bssid);
128 /* In case the signal is invalid update the status */
129 signal_valid = abs(channel->center_freq - cbss->channel->center_freq)
130 <= local->hw.wiphy->max_adj_channel_rssi_comp;
132 rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
134 bss = (void *)cbss->priv;
137 bss->device_ts_beacon = rx_status->device_timestamp;
139 bss->device_ts_presp = rx_status->device_timestamp;
141 if (elems.parse_error) {
143 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON;
145 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP;
148 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON;
150 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP;
153 /* save the ERP value so that it is available at association time */
154 if (elems.erp_info && (!elems.parse_error ||
155 !(bss->valid_data & IEEE80211_BSS_VALID_ERP))) {
156 bss->erp_value = elems.erp_info[0];
157 bss->has_erp_value = true;
158 if (!elems.parse_error)
159 bss->valid_data |= IEEE80211_BSS_VALID_ERP;
162 /* replace old supported rates if we get new values */
163 if (!elems.parse_error ||
164 !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) {
166 if (elems.supp_rates) {
167 clen = IEEE80211_MAX_SUPP_RATES;
168 if (clen > elems.supp_rates_len)
169 clen = elems.supp_rates_len;
170 memcpy(bss->supp_rates, elems.supp_rates, clen);
173 if (elems.ext_supp_rates) {
174 clen = IEEE80211_MAX_SUPP_RATES - srlen;
175 if (clen > elems.ext_supp_rates_len)
176 clen = elems.ext_supp_rates_len;
177 memcpy(bss->supp_rates + srlen, elems.ext_supp_rates,
182 bss->supp_rates_len = srlen;
183 if (!elems.parse_error)
184 bss->valid_data |= IEEE80211_BSS_VALID_RATES;
188 if (!elems.parse_error ||
189 !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) {
190 bss->wmm_used = elems.wmm_param || elems.wmm_info;
191 bss->uapsd_supported = is_uapsd_supported(&elems);
192 if (!elems.parse_error)
193 bss->valid_data |= IEEE80211_BSS_VALID_WMM;
197 struct ieee80211_supported_band *sband =
198 local->hw.wiphy->bands[rx_status->band];
199 if (!(rx_status->encoding == RX_ENC_HT) &&
200 !(rx_status->encoding == RX_ENC_VHT))
202 &sband->bitrates[rx_status->rate_idx];
208 static bool ieee80211_scan_accept_presp(struct ieee80211_sub_if_data *sdata,
209 u32 scan_flags, const u8 *da)
213 /* accept broadcast for OCE */
214 if (scan_flags & NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP &&
215 is_broadcast_ether_addr(da))
217 if (scan_flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
219 return ether_addr_equal(da, sdata->vif.addr);
222 void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb)
224 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
225 struct ieee80211_sub_if_data *sdata1, *sdata2;
226 struct ieee80211_mgmt *mgmt = (void *)skb->data;
227 struct ieee80211_bss *bss;
228 struct ieee80211_channel *channel;
231 (!ieee80211_is_probe_resp(mgmt->frame_control) &&
232 !ieee80211_is_beacon(mgmt->frame_control)))
235 sdata1 = rcu_dereference(local->scan_sdata);
236 sdata2 = rcu_dereference(local->sched_scan_sdata);
238 if (likely(!sdata1 && !sdata2))
241 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
242 struct cfg80211_scan_request *scan_req;
243 struct cfg80211_sched_scan_request *sched_scan_req;
244 u32 scan_req_flags = 0, sched_scan_req_flags = 0;
246 scan_req = rcu_dereference(local->scan_req);
247 sched_scan_req = rcu_dereference(local->sched_scan_req);
250 scan_req_flags = scan_req->flags;
253 sched_scan_req_flags = sched_scan_req->flags;
255 /* ignore ProbeResp to foreign address or non-bcast (OCE)
256 * unless scanning with randomised address
258 if (!ieee80211_scan_accept_presp(sdata1, scan_req_flags,
260 !ieee80211_scan_accept_presp(sdata2, sched_scan_req_flags,
265 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
267 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
270 bss = ieee80211_bss_info_update(local, rx_status,
274 ieee80211_rx_bss_put(local, bss);
278 ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef,
279 enum nl80211_bss_scan_width scan_width)
281 memset(chandef, 0, sizeof(*chandef));
282 switch (scan_width) {
283 case NL80211_BSS_CHAN_WIDTH_5:
284 chandef->width = NL80211_CHAN_WIDTH_5;
286 case NL80211_BSS_CHAN_WIDTH_10:
287 chandef->width = NL80211_CHAN_WIDTH_10;
290 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
295 /* return false if no more work */
296 static bool ieee80211_prep_hw_scan(struct ieee80211_local *local)
298 struct cfg80211_scan_request *req;
299 struct cfg80211_chan_def chandef;
301 int i, ielen, n_chans;
304 req = rcu_dereference_protected(local->scan_req,
305 lockdep_is_held(&local->mtx));
307 if (test_bit(SCAN_HW_CANCELLED, &local->scanning))
310 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {
311 for (i = 0; i < req->n_channels; i++) {
312 local->hw_scan_req->req.channels[i] = req->channels[i];
313 bands_used |= BIT(req->channels[i]->band);
316 n_chans = req->n_channels;
319 if (local->hw_scan_band == NUM_NL80211_BANDS)
324 for (i = 0; i < req->n_channels; i++) {
325 if (req->channels[i]->band !=
328 local->hw_scan_req->req.channels[n_chans] =
331 bands_used |= BIT(req->channels[i]->band);
334 local->hw_scan_band++;
338 local->hw_scan_req->req.n_channels = n_chans;
339 ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
341 if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
342 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
344 ielen = ieee80211_build_preq_ies(local,
345 (u8 *)local->hw_scan_req->req.ie,
346 local->hw_scan_ies_bufsize,
347 &local->hw_scan_req->ies,
348 req->ie, req->ie_len,
349 bands_used, req->rates, &chandef,
351 local->hw_scan_req->req.ie_len = ielen;
352 local->hw_scan_req->req.no_cck = req->no_cck;
353 ether_addr_copy(local->hw_scan_req->req.mac_addr, req->mac_addr);
354 ether_addr_copy(local->hw_scan_req->req.mac_addr_mask,
356 ether_addr_copy(local->hw_scan_req->req.bssid, req->bssid);
361 static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
363 struct ieee80211_local *local = hw_to_local(hw);
364 bool hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
365 bool was_scanning = local->scanning;
366 struct cfg80211_scan_request *scan_req;
367 struct ieee80211_sub_if_data *scan_sdata;
368 struct ieee80211_sub_if_data *sdata;
370 lockdep_assert_held(&local->mtx);
373 * It's ok to abort a not-yet-running scan (that
374 * we have one at all will be verified by checking
375 * local->scan_req next), but not to complete it
378 if (WARN_ON(!local->scanning && !aborted))
381 if (WARN_ON(!local->scan_req))
384 if (hw_scan && !aborted &&
385 !ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS) &&
386 ieee80211_prep_hw_scan(local)) {
389 rc = drv_hw_scan(local,
390 rcu_dereference_protected(local->scan_sdata,
391 lockdep_is_held(&local->mtx)),
397 /* HW scan failed and is going to be reported as aborted,
398 * so clear old scan info.
400 memset(&local->scan_info, 0, sizeof(local->scan_info));
404 kfree(local->hw_scan_req);
405 local->hw_scan_req = NULL;
407 scan_req = rcu_dereference_protected(local->scan_req,
408 lockdep_is_held(&local->mtx));
410 if (scan_req != local->int_scan_req) {
411 local->scan_info.aborted = aborted;
412 cfg80211_scan_done(scan_req, &local->scan_info);
414 RCU_INIT_POINTER(local->scan_req, NULL);
416 scan_sdata = rcu_dereference_protected(local->scan_sdata,
417 lockdep_is_held(&local->mtx));
418 RCU_INIT_POINTER(local->scan_sdata, NULL);
421 local->scan_chandef.chan = NULL;
423 /* Set power back to normal operating levels. */
424 ieee80211_hw_config(local, 0);
427 ieee80211_configure_filter(local);
428 drv_sw_scan_complete(local, scan_sdata);
429 ieee80211_offchannel_return(local);
432 ieee80211_recalc_idle(local);
434 ieee80211_mlme_notify_scan_completed(local);
435 ieee80211_ibss_notify_scan_completed(local);
437 /* Requeue all the work that might have been ignored while
438 * the scan was in progress; if there was none this will
439 * just be a no-op for the particular interface.
441 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
442 if (ieee80211_sdata_running(sdata))
443 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
447 ieee80211_start_next_roc(local);
450 void ieee80211_scan_completed(struct ieee80211_hw *hw,
451 struct cfg80211_scan_info *info)
453 struct ieee80211_local *local = hw_to_local(hw);
455 trace_api_scan_completed(local, info->aborted);
457 set_bit(SCAN_COMPLETED, &local->scanning);
459 set_bit(SCAN_ABORTED, &local->scanning);
461 memcpy(&local->scan_info, info, sizeof(*info));
463 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
465 EXPORT_SYMBOL(ieee80211_scan_completed);
467 static int ieee80211_start_sw_scan(struct ieee80211_local *local,
468 struct ieee80211_sub_if_data *sdata)
470 /* Software scan is not supported in multi-channel cases */
471 if (local->use_chanctx)
475 * Hardware/driver doesn't support hw_scan, so use software
476 * scanning instead. First send a nullfunc frame with power save
477 * bit on so that AP will buffer the frames for us while we are not
478 * listening, then send probe requests to each channel and wait for
479 * the responses. After all channels are scanned, tune back to the
480 * original channel and send a nullfunc frame with power save bit
481 * off to trigger the AP to send us all the buffered frames.
483 * Note that while local->sw_scanning is true everything else but
484 * nullfunc frames and probe requests will be dropped in
485 * ieee80211_tx_h_check_assoc().
487 drv_sw_scan_start(local, sdata, local->scan_addr);
489 local->leave_oper_channel_time = jiffies;
490 local->next_scan_state = SCAN_DECISION;
491 local->scan_channel_idx = 0;
493 ieee80211_offchannel_stop_vifs(local);
495 /* ensure nullfunc is transmitted before leaving operating channel */
496 ieee80211_flush_queues(local, NULL, false);
498 ieee80211_configure_filter(local);
500 /* We need to set power level at maximum rate for scanning. */
501 ieee80211_hw_config(local, 0);
503 ieee80211_queue_delayed_work(&local->hw,
504 &local->scan_work, 0);
509 static bool ieee80211_can_scan(struct ieee80211_local *local,
510 struct ieee80211_sub_if_data *sdata)
512 if (ieee80211_is_radar_required(local))
515 if (!list_empty(&local->roc_list))
518 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
519 sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL)
525 void ieee80211_run_deferred_scan(struct ieee80211_local *local)
527 lockdep_assert_held(&local->mtx);
529 if (!local->scan_req || local->scanning)
532 if (!ieee80211_can_scan(local,
533 rcu_dereference_protected(
535 lockdep_is_held(&local->mtx))))
538 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
539 round_jiffies_relative(0));
542 static void ieee80211_send_scan_probe_req(struct ieee80211_sub_if_data *sdata,
543 const u8 *src, const u8 *dst,
544 const u8 *ssid, size_t ssid_len,
545 const u8 *ie, size_t ie_len,
546 u32 ratemask, u32 flags, u32 tx_flags,
547 struct ieee80211_channel *channel)
550 u32 txdata_flags = 0;
552 skb = ieee80211_build_probe_req(sdata, src, dst, ratemask, channel,
557 if (flags & IEEE80211_PROBE_FLAG_RANDOM_SN) {
558 struct ieee80211_hdr *hdr = (void *)skb->data;
559 u16 sn = get_random_u32();
561 txdata_flags |= IEEE80211_TX_NO_SEQNO;
563 cpu_to_le16(IEEE80211_SN_TO_SEQ(sn));
565 IEEE80211_SKB_CB(skb)->flags |= tx_flags;
566 ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band,
571 static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
572 unsigned long *next_delay)
575 struct ieee80211_sub_if_data *sdata;
576 struct cfg80211_scan_request *scan_req;
577 enum nl80211_band band = local->hw.conf.chandef.chan->band;
578 u32 flags = 0, tx_flags;
580 scan_req = rcu_dereference_protected(local->scan_req,
581 lockdep_is_held(&local->mtx));
583 tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
584 if (scan_req->no_cck)
585 tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
586 if (scan_req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
587 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
588 if (scan_req->flags & NL80211_SCAN_FLAG_RANDOM_SN)
589 flags |= IEEE80211_PROBE_FLAG_RANDOM_SN;
591 sdata = rcu_dereference_protected(local->scan_sdata,
592 lockdep_is_held(&local->mtx));
594 for (i = 0; i < scan_req->n_ssids; i++)
595 ieee80211_send_scan_probe_req(
596 sdata, local->scan_addr, scan_req->bssid,
597 scan_req->ssids[i].ssid, scan_req->ssids[i].ssid_len,
598 scan_req->ie, scan_req->ie_len,
599 scan_req->rates[band], flags,
600 tx_flags, local->hw.conf.chandef.chan);
603 * After sending probe requests, wait for probe responses
606 *next_delay = IEEE80211_CHANNEL_TIME;
607 local->next_scan_state = SCAN_DECISION;
610 static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
611 struct cfg80211_scan_request *req)
613 struct ieee80211_local *local = sdata->local;
614 bool hw_scan = local->ops->hw_scan;
617 lockdep_assert_held(&local->mtx);
619 if (local->scan_req || ieee80211_is_radar_required(local))
622 if (!ieee80211_can_scan(local, sdata)) {
623 /* wait for the work to finish/time out */
624 rcu_assign_pointer(local->scan_req, req);
625 rcu_assign_pointer(local->scan_sdata, sdata);
633 local->hw_scan_ies_bufsize = local->scan_ies_len + req->ie_len;
635 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {
637 u8 bands_counted = 0;
639 for (i = 0; i < req->n_channels; i++) {
640 if (bands_counted & BIT(req->channels[i]->band))
642 bands_counted |= BIT(req->channels[i]->band);
646 local->hw_scan_ies_bufsize *= n_bands;
649 local->hw_scan_req = kmalloc(
650 sizeof(*local->hw_scan_req) +
651 req->n_channels * sizeof(req->channels[0]) +
652 local->hw_scan_ies_bufsize, GFP_KERNEL);
653 if (!local->hw_scan_req)
656 local->hw_scan_req->req.ssids = req->ssids;
657 local->hw_scan_req->req.n_ssids = req->n_ssids;
658 ies = (u8 *)local->hw_scan_req +
659 sizeof(*local->hw_scan_req) +
660 req->n_channels * sizeof(req->channels[0]);
661 local->hw_scan_req->req.ie = ies;
662 local->hw_scan_req->req.flags = req->flags;
663 eth_broadcast_addr(local->hw_scan_req->req.bssid);
664 local->hw_scan_req->req.duration = req->duration;
665 local->hw_scan_req->req.duration_mandatory =
666 req->duration_mandatory;
668 local->hw_scan_band = 0;
671 * After allocating local->hw_scan_req, we must
672 * go through until ieee80211_prep_hw_scan(), so
673 * anything that might be changed here and leave
674 * this function early must not go after this
679 rcu_assign_pointer(local->scan_req, req);
680 rcu_assign_pointer(local->scan_sdata, sdata);
682 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
683 get_random_mask_addr(local->scan_addr,
687 memcpy(local->scan_addr, sdata->vif.addr, ETH_ALEN);
690 __set_bit(SCAN_HW_SCANNING, &local->scanning);
691 } else if ((req->n_channels == 1) &&
692 (req->channels[0] == local->_oper_chandef.chan)) {
694 * If we are scanning only on the operating channel
695 * then we do not need to stop normal activities
697 unsigned long next_delay;
699 __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
701 ieee80211_recalc_idle(local);
703 /* Notify driver scan is starting, keep order of operations
704 * same as normal software scan, in case that matters. */
705 drv_sw_scan_start(local, sdata, local->scan_addr);
707 ieee80211_configure_filter(local); /* accept probe-responses */
709 /* We need to ensure power level is at max for scanning. */
710 ieee80211_hw_config(local, 0);
712 if ((req->channels[0]->flags & (IEEE80211_CHAN_NO_IR |
713 IEEE80211_CHAN_RADAR)) ||
715 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
717 ieee80211_scan_state_send_probe(local, &next_delay);
718 next_delay = IEEE80211_CHANNEL_TIME;
721 /* Now, just wait a bit and we are all done! */
722 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
726 /* Do normal software scan */
727 __set_bit(SCAN_SW_SCANNING, &local->scanning);
730 ieee80211_recalc_idle(local);
733 WARN_ON(!ieee80211_prep_hw_scan(local));
734 rc = drv_hw_scan(local, sdata, local->hw_scan_req);
736 rc = ieee80211_start_sw_scan(local, sdata);
740 kfree(local->hw_scan_req);
741 local->hw_scan_req = NULL;
744 ieee80211_recalc_idle(local);
746 local->scan_req = NULL;
747 RCU_INIT_POINTER(local->scan_sdata, NULL);
750 if (hw_scan && rc == 1) {
752 * we can't fall back to software for P2P-GO
753 * as it must update NoA etc.
755 if (ieee80211_vif_type_p2p(&sdata->vif) ==
756 NL80211_IFTYPE_P2P_GO)
766 ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
769 * TODO: channel switching also consumes quite some time,
770 * add that delay as well to get a better estimation
772 if (chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR))
773 return IEEE80211_PASSIVE_CHANNEL_TIME;
774 return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
777 static void ieee80211_scan_state_decision(struct ieee80211_local *local,
778 unsigned long *next_delay)
780 bool associated = false;
781 bool tx_empty = true;
783 struct ieee80211_sub_if_data *sdata;
784 struct ieee80211_channel *next_chan;
785 enum mac80211_scan_state next_scan_state;
786 struct cfg80211_scan_request *scan_req;
789 * check if at least one STA interface is associated,
790 * check if at least one STA interface has pending tx frames
791 * and grab the lowest used beacon interval
793 mutex_lock(&local->iflist_mtx);
794 list_for_each_entry(sdata, &local->interfaces, list) {
795 if (!ieee80211_sdata_running(sdata))
798 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
799 if (sdata->u.mgd.associated) {
802 if (!qdisc_all_tx_empty(sdata->dev)) {
809 mutex_unlock(&local->iflist_mtx);
811 scan_req = rcu_dereference_protected(local->scan_req,
812 lockdep_is_held(&local->mtx));
814 next_chan = scan_req->channels[local->scan_channel_idx];
817 * we're currently scanning a different channel, let's
818 * see if we can scan another channel without interfering
819 * with the current traffic situation.
821 * Keep good latency, do not stay off-channel more than 125 ms.
824 bad_latency = time_after(jiffies +
825 ieee80211_scan_get_channel_time(next_chan),
826 local->leave_oper_channel_time + HZ / 8);
828 if (associated && !tx_empty) {
829 if (scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
830 next_scan_state = SCAN_ABORT;
832 next_scan_state = SCAN_SUSPEND;
833 } else if (associated && bad_latency) {
834 next_scan_state = SCAN_SUSPEND;
836 next_scan_state = SCAN_SET_CHANNEL;
839 local->next_scan_state = next_scan_state;
844 static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
845 unsigned long *next_delay)
848 struct ieee80211_channel *chan;
849 enum nl80211_bss_scan_width oper_scan_width;
850 struct cfg80211_scan_request *scan_req;
852 scan_req = rcu_dereference_protected(local->scan_req,
853 lockdep_is_held(&local->mtx));
856 chan = scan_req->channels[local->scan_channel_idx];
858 local->scan_chandef.chan = chan;
859 local->scan_chandef.center_freq1 = chan->center_freq;
860 local->scan_chandef.center_freq2 = 0;
861 switch (scan_req->scan_width) {
862 case NL80211_BSS_CHAN_WIDTH_5:
863 local->scan_chandef.width = NL80211_CHAN_WIDTH_5;
865 case NL80211_BSS_CHAN_WIDTH_10:
866 local->scan_chandef.width = NL80211_CHAN_WIDTH_10;
868 case NL80211_BSS_CHAN_WIDTH_20:
869 /* If scanning on oper channel, use whatever channel-type
870 * is currently in use.
872 oper_scan_width = cfg80211_chandef_to_scan_width(
873 &local->_oper_chandef);
874 if (chan == local->_oper_chandef.chan &&
875 oper_scan_width == scan_req->scan_width)
876 local->scan_chandef = local->_oper_chandef;
878 local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
882 if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
885 /* advance state machine to next channel/band */
886 local->scan_channel_idx++;
889 /* if we skip this channel return to the decision state */
890 local->next_scan_state = SCAN_DECISION;
895 * Probe delay is used to update the NAV, cf. 11.1.3.2.2
896 * (which unfortunately doesn't say _why_ step a) is done,
897 * but it waits for the probe delay or until a frame is
898 * received - and the received frame would update the NAV).
899 * For now, we do not support waiting until a frame is
902 * In any case, it is not necessary for a passive scan.
904 if ((chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) ||
905 !scan_req->n_ssids) {
906 *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
907 local->next_scan_state = SCAN_DECISION;
911 /* active scan, send probes */
912 *next_delay = IEEE80211_PROBE_DELAY;
913 local->next_scan_state = SCAN_SEND_PROBE;
916 static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
917 unsigned long *next_delay)
919 /* switch back to the operating channel */
920 local->scan_chandef.chan = NULL;
921 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
924 ieee80211_offchannel_return(local);
926 *next_delay = HZ / 5;
927 /* afterwards, resume scan & go to next channel */
928 local->next_scan_state = SCAN_RESUME;
931 static void ieee80211_scan_state_resume(struct ieee80211_local *local,
932 unsigned long *next_delay)
934 ieee80211_offchannel_stop_vifs(local);
936 if (local->ops->flush) {
937 ieee80211_flush_queues(local, NULL, false);
940 *next_delay = HZ / 10;
942 /* remember when we left the operating channel */
943 local->leave_oper_channel_time = jiffies;
945 /* advance to the next channel to be scanned */
946 local->next_scan_state = SCAN_SET_CHANNEL;
949 void ieee80211_scan_work(struct work_struct *work)
951 struct ieee80211_local *local =
952 container_of(work, struct ieee80211_local, scan_work.work);
953 struct ieee80211_sub_if_data *sdata;
954 struct cfg80211_scan_request *scan_req;
955 unsigned long next_delay = 0;
958 mutex_lock(&local->mtx);
960 if (!ieee80211_can_run_worker(local)) {
965 sdata = rcu_dereference_protected(local->scan_sdata,
966 lockdep_is_held(&local->mtx));
967 scan_req = rcu_dereference_protected(local->scan_req,
968 lockdep_is_held(&local->mtx));
970 /* When scanning on-channel, the first-callback means completed. */
971 if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) {
972 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
976 if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
977 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
981 if (!sdata || !scan_req)
984 if (!local->scanning) {
987 RCU_INIT_POINTER(local->scan_req, NULL);
988 RCU_INIT_POINTER(local->scan_sdata, NULL);
990 rc = __ieee80211_start_scan(sdata, scan_req);
992 /* need to complete scan in cfg80211 */
993 rcu_assign_pointer(local->scan_req, scan_req);
1001 * as long as no delay is required advance immediately
1002 * without scheduling a new work
1005 if (!ieee80211_sdata_running(sdata)) {
1010 switch (local->next_scan_state) {
1012 /* if no more bands/channels left, complete scan */
1013 if (local->scan_channel_idx >= scan_req->n_channels) {
1017 ieee80211_scan_state_decision(local, &next_delay);
1019 case SCAN_SET_CHANNEL:
1020 ieee80211_scan_state_set_channel(local, &next_delay);
1022 case SCAN_SEND_PROBE:
1023 ieee80211_scan_state_send_probe(local, &next_delay);
1026 ieee80211_scan_state_suspend(local, &next_delay);
1029 ieee80211_scan_state_resume(local, &next_delay);
1035 } while (next_delay == 0);
1037 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
1041 __ieee80211_scan_completed(&local->hw, aborted);
1043 mutex_unlock(&local->mtx);
1046 int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
1047 struct cfg80211_scan_request *req)
1051 mutex_lock(&sdata->local->mtx);
1052 res = __ieee80211_start_scan(sdata, req);
1053 mutex_unlock(&sdata->local->mtx);
1058 int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
1059 const u8 *ssid, u8 ssid_len,
1060 struct ieee80211_channel **channels,
1061 unsigned int n_channels,
1062 enum nl80211_bss_scan_width scan_width)
1064 struct ieee80211_local *local = sdata->local;
1065 int ret = -EBUSY, i, n_ch = 0;
1066 enum nl80211_band band;
1068 mutex_lock(&local->mtx);
1071 if (local->scan_req)
1074 /* fill internal scan request */
1078 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1079 if (!local->hw.wiphy->bands[band])
1082 max_n = local->hw.wiphy->bands[band]->n_channels;
1083 for (i = 0; i < max_n; i++) {
1084 struct ieee80211_channel *tmp_ch =
1085 &local->hw.wiphy->bands[band]->channels[i];
1087 if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR |
1088 IEEE80211_CHAN_DISABLED))
1091 local->int_scan_req->channels[n_ch] = tmp_ch;
1096 if (WARN_ON_ONCE(n_ch == 0))
1099 local->int_scan_req->n_channels = n_ch;
1101 for (i = 0; i < n_channels; i++) {
1102 if (channels[i]->flags & (IEEE80211_CHAN_NO_IR |
1103 IEEE80211_CHAN_DISABLED))
1106 local->int_scan_req->channels[n_ch] = channels[i];
1110 if (WARN_ON_ONCE(n_ch == 0))
1113 local->int_scan_req->n_channels = n_ch;
1116 local->int_scan_req->ssids = &local->scan_ssid;
1117 local->int_scan_req->n_ssids = 1;
1118 local->int_scan_req->scan_width = scan_width;
1119 memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
1120 local->int_scan_req->ssids[0].ssid_len = ssid_len;
1122 ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
1124 mutex_unlock(&local->mtx);
1129 * Only call this function when a scan can't be queued -- under RTNL.
1131 void ieee80211_scan_cancel(struct ieee80211_local *local)
1134 * We are canceling software scan, or deferred scan that was not
1135 * yet really started (see __ieee80211_start_scan ).
1137 * Regarding hardware scan:
1138 * - we can not call __ieee80211_scan_completed() as when
1139 * SCAN_HW_SCANNING bit is set this function change
1140 * local->hw_scan_req to operate on 5G band, what race with
1141 * driver which can use local->hw_scan_req
1143 * - we can not cancel scan_work since driver can schedule it
1144 * by ieee80211_scan_completed(..., true) to finish scan
1146 * Hence we only call the cancel_hw_scan() callback, but the low-level
1147 * driver is still responsible for calling ieee80211_scan_completed()
1148 * after the scan was completed/aborted.
1151 mutex_lock(&local->mtx);
1152 if (!local->scan_req)
1156 * We have a scan running and the driver already reported completion,
1157 * but the worker hasn't run yet or is stuck on the mutex - mark it as
1160 if (test_bit(SCAN_HW_SCANNING, &local->scanning) &&
1161 test_bit(SCAN_COMPLETED, &local->scanning)) {
1162 set_bit(SCAN_HW_CANCELLED, &local->scanning);
1166 if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
1168 * Make sure that __ieee80211_scan_completed doesn't trigger a
1169 * scan on another band.
1171 set_bit(SCAN_HW_CANCELLED, &local->scanning);
1172 if (local->ops->cancel_hw_scan)
1173 drv_cancel_hw_scan(local,
1174 rcu_dereference_protected(local->scan_sdata,
1175 lockdep_is_held(&local->mtx)));
1180 * If the work is currently running, it must be blocked on
1181 * the mutex, but we'll set scan_sdata = NULL and it'll
1182 * simply exit once it acquires the mutex.
1184 cancel_delayed_work(&local->scan_work);
1186 memset(&local->scan_info, 0, sizeof(local->scan_info));
1187 __ieee80211_scan_completed(&local->hw, true);
1189 mutex_unlock(&local->mtx);
1192 int __ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
1193 struct cfg80211_sched_scan_request *req)
1195 struct ieee80211_local *local = sdata->local;
1196 struct ieee80211_scan_ies sched_scan_ies = {};
1197 struct cfg80211_chan_def chandef;
1198 int ret, i, iebufsz, num_bands = 0;
1199 u32 rate_masks[NUM_NL80211_BANDS] = {};
1204 iebufsz = local->scan_ies_len + req->ie_len;
1206 lockdep_assert_held(&local->mtx);
1208 if (!local->ops->sched_scan_start)
1211 for (i = 0; i < NUM_NL80211_BANDS; i++) {
1212 if (local->hw.wiphy->bands[i]) {
1213 bands_used |= BIT(i);
1214 rate_masks[i] = (u32) -1;
1219 if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
1220 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
1222 ie = kcalloc(iebufsz, num_bands, GFP_KERNEL);
1228 ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
1230 ieee80211_build_preq_ies(local, ie, num_bands * iebufsz,
1231 &sched_scan_ies, req->ie,
1232 req->ie_len, bands_used, rate_masks, &chandef,
1235 ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies);
1237 rcu_assign_pointer(local->sched_scan_sdata, sdata);
1238 rcu_assign_pointer(local->sched_scan_req, req);
1245 /* Clean in case of failure after HW restart or upon resume. */
1246 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
1247 RCU_INIT_POINTER(local->sched_scan_req, NULL);
1253 int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
1254 struct cfg80211_sched_scan_request *req)
1256 struct ieee80211_local *local = sdata->local;
1259 mutex_lock(&local->mtx);
1261 if (rcu_access_pointer(local->sched_scan_sdata)) {
1262 mutex_unlock(&local->mtx);
1266 ret = __ieee80211_request_sched_scan_start(sdata, req);
1268 mutex_unlock(&local->mtx);
1272 int ieee80211_request_sched_scan_stop(struct ieee80211_local *local)
1274 struct ieee80211_sub_if_data *sched_scan_sdata;
1277 mutex_lock(&local->mtx);
1279 if (!local->ops->sched_scan_stop) {
1284 /* We don't want to restart sched scan anymore. */
1285 RCU_INIT_POINTER(local->sched_scan_req, NULL);
1287 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
1288 lockdep_is_held(&local->mtx));
1289 if (sched_scan_sdata) {
1290 ret = drv_sched_scan_stop(local, sched_scan_sdata);
1292 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
1295 mutex_unlock(&local->mtx);
1300 void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
1302 struct ieee80211_local *local = hw_to_local(hw);
1304 trace_api_sched_scan_results(local);
1306 cfg80211_sched_scan_results(hw->wiphy, 0);
1308 EXPORT_SYMBOL(ieee80211_sched_scan_results);
1310 void ieee80211_sched_scan_end(struct ieee80211_local *local)
1312 mutex_lock(&local->mtx);
1314 if (!rcu_access_pointer(local->sched_scan_sdata)) {
1315 mutex_unlock(&local->mtx);
1319 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
1321 /* If sched scan was aborted by the driver. */
1322 RCU_INIT_POINTER(local->sched_scan_req, NULL);
1324 mutex_unlock(&local->mtx);
1326 cfg80211_sched_scan_stopped(local->hw.wiphy, 0);
1329 void ieee80211_sched_scan_stopped_work(struct work_struct *work)
1331 struct ieee80211_local *local =
1332 container_of(work, struct ieee80211_local,
1333 sched_scan_stopped_work);
1335 ieee80211_sched_scan_end(local);
1338 void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
1340 struct ieee80211_local *local = hw_to_local(hw);
1342 trace_api_sched_scan_stopped(local);
1345 * this shouldn't really happen, so for simplicity
1346 * simply ignore it, and let mac80211 reconfigure
1347 * the sched scan later on.
1349 if (local->in_reconfig)
1352 schedule_work(&local->sched_scan_stopped_work);
1354 EXPORT_SYMBOL(ieee80211_sched_scan_stopped);