blob: 0cf0667006236ded89bfd3f9946a4ef3e3588ed1 [file] [log] [blame]
Johannes Berg0a51b272008-09-08 17:44:25 +02001/*
Johannes Berg5484e232008-09-08 17:44:27 +02002 * Scanning implementation
3 *
Johannes Berg0a51b272008-09-08 17:44:25 +02004 * 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>
Sara Sharon74d803b2015-06-03 10:44:17 +03009 * Copyright 2013-2015 Intel Mobile Communications GmbH
Roee Zamir40b0bd22017-08-06 11:38:23 +030010 * Copyright 2016-2017 Intel Deutschland GmbH
Sara Sharon4abb52a2019-01-16 12:14:41 +020011 * Copyright (C) 2018-2019 Intel Corporation
Johannes Berg0a51b272008-09-08 17:44:25 +020012 *
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.
16 */
17
Johannes Berg0a51b272008-09-08 17:44:25 +020018#include <linux/if_arp.h>
Felix Fietkau888d04d2012-03-01 15:22:09 +010019#include <linux/etherdevice.h>
Johannes Berg078e1e62009-01-22 18:07:31 +010020#include <linux/rtnetlink.h>
Helmut Schaadf13cce2010-02-24 14:19:21 +010021#include <net/sch_generic.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040023#include <linux/export.h>
Johannes Bergb9771d42018-05-28 15:47:41 +020024#include <linux/random.h>
Johannes Berg0a51b272008-09-08 17:44:25 +020025#include <net/mac80211.h>
Johannes Berg0a51b272008-09-08 17:44:25 +020026
27#include "ieee80211_i.h"
Johannes Berg24487982009-04-23 18:52:52 +020028#include "driver-ops.h"
Johannes Berg5484e232008-09-08 17:44:27 +020029#include "mesh.h"
Johannes Berg0a51b272008-09-08 17:44:25 +020030
31#define IEEE80211_PROBE_DELAY (HZ / 33)
32#define IEEE80211_CHANNEL_TIME (HZ / 33)
Stanislaw Gruszka3f892b62013-01-23 12:32:45 +010033#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 9)
Johannes Berg0a51b272008-09-08 17:44:25 +020034
Johannes Berg5484e232008-09-08 17:44:27 +020035void ieee80211_rx_bss_put(struct ieee80211_local *local,
Johannes Bergc2b13452008-09-11 00:01:55 +020036 struct ieee80211_bss *bss)
Johannes Berg5484e232008-09-08 17:44:27 +020037{
Johannes Berg0c1ad2c2009-12-23 13:15:39 +010038 if (!bss)
39 return;
Johannes Berg5b112d32013-02-01 01:49:58 +010040 cfg80211_put_bss(local->hw.wiphy,
41 container_of((void *)bss, struct cfg80211_bss, priv));
Johannes Berg5484e232008-09-08 17:44:27 +020042}
43
Kalle Valoab133152010-01-12 10:42:31 +020044static bool is_uapsd_supported(struct ieee802_11_elems *elems)
45{
46 u8 qos_info;
47
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];
54 else
55 /* no valid wmm information or parameter element found */
56 return false;
57
58 return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD;
59}
60
Sara Sharonfcea7db2019-01-16 20:35:38 +020061static void
62ieee80211_update_bss_from_elems(struct ieee80211_local *local,
63 struct ieee80211_bss *bss,
64 struct ieee802_11_elems *elems,
65 struct ieee80211_rx_status *rx_status,
66 bool beacon)
67{
68 int clen, srlen;
69
70 if (beacon)
71 bss->device_ts_beacon = rx_status->device_timestamp;
72 else
73 bss->device_ts_presp = rx_status->device_timestamp;
74
75 if (elems->parse_error) {
76 if (beacon)
77 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON;
78 else
79 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP;
80 } else {
81 if (beacon)
82 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON;
83 else
84 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP;
85 }
86
87 /* save the ERP value so that it is available at association time */
88 if (elems->erp_info && (!elems->parse_error ||
89 !(bss->valid_data & IEEE80211_BSS_VALID_ERP))) {
90 bss->erp_value = elems->erp_info[0];
91 bss->has_erp_value = true;
92 if (!elems->parse_error)
93 bss->valid_data |= IEEE80211_BSS_VALID_ERP;
94 }
95
96 /* replace old supported rates if we get new values */
97 if (!elems->parse_error ||
98 !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) {
99 srlen = 0;
100 if (elems->supp_rates) {
101 clen = IEEE80211_MAX_SUPP_RATES;
102 if (clen > elems->supp_rates_len)
103 clen = elems->supp_rates_len;
104 memcpy(bss->supp_rates, elems->supp_rates, clen);
105 srlen += clen;
106 }
107 if (elems->ext_supp_rates) {
108 clen = IEEE80211_MAX_SUPP_RATES - srlen;
109 if (clen > elems->ext_supp_rates_len)
110 clen = elems->ext_supp_rates_len;
111 memcpy(bss->supp_rates + srlen, elems->ext_supp_rates,
112 clen);
113 srlen += clen;
114 }
115 if (srlen) {
116 bss->supp_rates_len = srlen;
117 if (!elems->parse_error)
118 bss->valid_data |= IEEE80211_BSS_VALID_RATES;
119 }
120 }
121
122 if (!elems->parse_error ||
123 !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) {
124 bss->wmm_used = elems->wmm_param || elems->wmm_info;
125 bss->uapsd_supported = is_uapsd_supported(elems);
126 if (!elems->parse_error)
127 bss->valid_data |= IEEE80211_BSS_VALID_WMM;
128 }
129
130 if (beacon) {
131 struct ieee80211_supported_band *sband =
132 local->hw.wiphy->bands[rx_status->band];
133 if (!(rx_status->encoding == RX_ENC_HT) &&
134 !(rx_status->encoding == RX_ENC_VHT))
135 bss->beacon_rate =
136 &sband->bitrates[rx_status->rate_idx];
137 }
138}
139
Johannes Bergc2b13452008-09-11 00:01:55 +0200140struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +0200141ieee80211_bss_info_update(struct ieee80211_local *local,
142 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200143 struct ieee80211_mgmt *mgmt, size_t len,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200144 struct ieee80211_channel *channel)
Johannes Berg5484e232008-09-08 17:44:27 +0200145{
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200146 bool beacon = ieee80211_is_beacon(mgmt->frame_control);
Sara Sharon78ac51f2019-01-16 18:22:56 +0200147 struct cfg80211_bss *cbss, *non_tx_cbss;
148 struct ieee80211_bss *bss, *non_tx_bss;
Johannes Berg162dd6a2016-02-23 23:05:06 +0200149 struct cfg80211_inform_bss bss_meta = {
150 .boottime_ns = rx_status->boottime_ns,
151 };
Sara Sharon74d803b2015-06-03 10:44:17 +0300152 bool signal_valid;
Avraham Stern7947d3e2016-07-05 15:23:12 +0300153 struct ieee80211_sub_if_data *scan_sdata;
Sara Sharon4abb52a2019-01-16 12:14:41 +0200154 struct ieee802_11_elems elems;
155 size_t baselen;
156 u8 *elements;
Johannes Berg2a519312009-02-10 21:25:55 +0100157
Tosoni1ad22fb2018-03-14 16:58:34 +0000158 if (rx_status->flag & RX_FLAG_NO_SIGNAL_VAL)
159 bss_meta.signal = 0; /* invalid signal indication */
160 else if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
Johannes Berg61f6bba2015-10-13 11:36:21 +0200161 bss_meta.signal = rx_status->signal * 100;
Johannes Berg30686bf2015-06-02 21:39:54 +0200162 else if (ieee80211_hw_check(&local->hw, SIGNAL_UNSPEC))
Johannes Berg61f6bba2015-10-13 11:36:21 +0200163 bss_meta.signal = (rx_status->signal * 100) / local->hw.max_signal;
Johannes Berg2a519312009-02-10 21:25:55 +0100164
Johannes Berg61f6bba2015-10-13 11:36:21 +0200165 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_20;
Johannes Bergda6a4352017-04-26 12:14:59 +0200166 if (rx_status->bw == RATE_INFO_BW_5)
Johannes Berg61f6bba2015-10-13 11:36:21 +0200167 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_5;
Johannes Bergda6a4352017-04-26 12:14:59 +0200168 else if (rx_status->bw == RATE_INFO_BW_10)
Johannes Berg61f6bba2015-10-13 11:36:21 +0200169 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_10;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200170
Johannes Berg61f6bba2015-10-13 11:36:21 +0200171 bss_meta.chan = channel;
Avraham Stern7947d3e2016-07-05 15:23:12 +0300172
173 rcu_read_lock();
174 scan_sdata = rcu_dereference(local->scan_sdata);
175 if (scan_sdata && scan_sdata->vif.type == NL80211_IFTYPE_STATION &&
176 scan_sdata->vif.bss_conf.assoc &&
177 ieee80211_have_rx_timestamp(rx_status)) {
178 bss_meta.parent_tsf =
179 ieee80211_calculate_rx_timestamp(local, rx_status,
180 len + FCS_LEN, 24);
181 ether_addr_copy(bss_meta.parent_bssid,
182 scan_sdata->vif.bss_conf.bssid);
183 }
184 rcu_read_unlock();
185
Johannes Berg61f6bba2015-10-13 11:36:21 +0200186 cbss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta,
187 mgmt, len, GFP_ATOMIC);
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100188 if (!cbss)
Johannes Berg00d3f142009-02-10 21:26:00 +0100189 return NULL;
Sara Sharon4abb52a2019-01-16 12:14:41 +0200190
191 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
192 elements = mgmt->u.probe_resp.variable;
193 baselen = offsetof(struct ieee80211_mgmt,
194 u.probe_resp.variable);
195 } else {
196 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
197 elements = mgmt->u.beacon.variable;
198 }
199
200 if (baselen > len)
201 return NULL;
202
203 ieee802_11_parse_elems(elements, len - baselen, false, &elems,
204 mgmt->bssid, cbss->bssid);
205
Sara Sharon74d803b2015-06-03 10:44:17 +0300206 /* In case the signal is invalid update the status */
207 signal_valid = abs(channel->center_freq - cbss->channel->center_freq)
208 <= local->hw.wiphy->max_adj_channel_rssi_comp;
209 if (!signal_valid)
210 rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
Johannes Berg00d3f142009-02-10 21:26:00 +0100211
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100212 bss = (void *)cbss->priv;
Sara Sharonfcea7db2019-01-16 20:35:38 +0200213 ieee80211_update_bss_from_elems(local, bss, &elems, rx_status, beacon);
Alexander Bondar817cee72013-05-19 14:23:57 +0300214
Sara Sharon78ac51f2019-01-16 18:22:56 +0200215 list_for_each_entry(non_tx_cbss, &cbss->nontrans_list, nontrans_list) {
216 non_tx_bss = (void *)non_tx_cbss->priv;
217
218 ieee80211_update_bss_from_elems(local, non_tx_bss, &elems,
219 rx_status, beacon);
220 }
221
Johannes Berg5484e232008-09-08 17:44:27 +0200222 return bss;
223}
Johannes Berg0a51b272008-09-08 17:44:25 +0200224
Roee Zamir40b0bd22017-08-06 11:38:23 +0300225static bool ieee80211_scan_accept_presp(struct ieee80211_sub_if_data *sdata,
226 u32 scan_flags, const u8 *da)
227{
228 if (!sdata)
229 return false;
230 /* accept broadcast for OCE */
231 if (scan_flags & NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP &&
232 is_broadcast_ether_addr(da))
233 return true;
234 if (scan_flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
235 return true;
236 return ether_addr_equal(da, sdata->vif.addr);
237}
238
Johannes Bergd48b2962012-07-06 22:19:27 +0200239void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb)
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200240{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200241 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
Johannes Bergd48b2962012-07-06 22:19:27 +0200242 struct ieee80211_sub_if_data *sdata1, *sdata2;
243 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Johannes Bergc2b13452008-09-11 00:01:55 +0200244 struct ieee80211_bss *bss;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200245 struct ieee80211_channel *channel;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200246
Johannes Bergd48b2962012-07-06 22:19:27 +0200247 if (skb->len < 24 ||
248 (!ieee80211_is_probe_resp(mgmt->frame_control) &&
249 !ieee80211_is_beacon(mgmt->frame_control)))
250 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200251
Johannes Bergd48b2962012-07-06 22:19:27 +0200252 sdata1 = rcu_dereference(local->scan_sdata);
253 sdata2 = rcu_dereference(local->sched_scan_sdata);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200254
Johannes Bergd48b2962012-07-06 22:19:27 +0200255 if (likely(!sdata1 && !sdata2))
256 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200257
Johannes Bergd48b2962012-07-06 22:19:27 +0200258 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
Johannes Berga344d672014-06-12 22:24:31 +0200259 struct cfg80211_scan_request *scan_req;
260 struct cfg80211_sched_scan_request *sched_scan_req;
Roee Zamir40b0bd22017-08-06 11:38:23 +0300261 u32 scan_req_flags = 0, sched_scan_req_flags = 0;
Johannes Berga344d672014-06-12 22:24:31 +0200262
263 scan_req = rcu_dereference(local->scan_req);
264 sched_scan_req = rcu_dereference(local->sched_scan_req);
265
Roee Zamir40b0bd22017-08-06 11:38:23 +0300266 if (scan_req)
267 scan_req_flags = scan_req->flags;
268
269 if (sched_scan_req)
270 sched_scan_req_flags = sched_scan_req->flags;
271
272 /* ignore ProbeResp to foreign address or non-bcast (OCE)
273 * unless scanning with randomised address
Johannes Berga344d672014-06-12 22:24:31 +0200274 */
Roee Zamir40b0bd22017-08-06 11:38:23 +0300275 if (!ieee80211_scan_accept_presp(sdata1, scan_req_flags,
276 mgmt->da) &&
277 !ieee80211_scan_accept_presp(sdata2, sched_scan_req_flags,
278 mgmt->da))
Johannes Bergd48b2962012-07-06 22:19:27 +0200279 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200280 }
281
Johannes Berg0172bb72012-11-23 14:23:30 +0100282 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200283
284 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
Johannes Bergd48b2962012-07-06 22:19:27 +0200285 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200286
Johannes Bergd48b2962012-07-06 22:19:27 +0200287 bss = ieee80211_bss_info_update(local, rx_status,
Sara Sharon4abb52a2019-01-16 12:14:41 +0200288 mgmt, skb->len,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200289 channel);
Jouni Malinend048e502008-10-11 03:29:55 +0300290 if (bss)
Johannes Bergd48b2962012-07-06 22:19:27 +0200291 ieee80211_rx_bss_put(local, bss);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200292}
293
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200294static void
295ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef,
296 enum nl80211_bss_scan_width scan_width)
297{
298 memset(chandef, 0, sizeof(*chandef));
299 switch (scan_width) {
300 case NL80211_BSS_CHAN_WIDTH_5:
301 chandef->width = NL80211_CHAN_WIDTH_5;
302 break;
303 case NL80211_BSS_CHAN_WIDTH_10:
304 chandef->width = NL80211_CHAN_WIDTH_10;
305 break;
306 default:
307 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
308 break;
309 }
310}
311
Johannes Berg4d36ec52009-10-27 20:59:55 +0100312/* return false if no more work */
313static bool ieee80211_prep_hw_scan(struct ieee80211_local *local)
314{
Johannes Berg6ea0a692014-11-19 11:55:49 +0100315 struct cfg80211_scan_request *req;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200316 struct cfg80211_chan_def chandef;
David Spinadelc56ef672014-02-05 15:21:13 +0200317 u8 bands_used = 0;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100318 int i, ielen, n_chans;
Johannes Bergb9771d42018-05-28 15:47:41 +0200319 u32 flags = 0;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100320
Johannes Berg6ea0a692014-11-19 11:55:49 +0100321 req = rcu_dereference_protected(local->scan_req,
322 lockdep_is_held(&local->mtx));
323
Emmanuel Grumbacha7540552013-09-16 11:12:07 +0300324 if (test_bit(SCAN_HW_CANCELLED, &local->scanning))
325 return false;
326
Johannes Berg30686bf2015-06-02 21:39:54 +0200327 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {
Johannes Berg4d36ec52009-10-27 20:59:55 +0100328 for (i = 0; i < req->n_channels; i++) {
David Spinadelc56ef672014-02-05 15:21:13 +0200329 local->hw_scan_req->req.channels[i] = req->channels[i];
330 bands_used |= BIT(req->channels[i]->band);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100331 }
332
David Spinadelc56ef672014-02-05 15:21:13 +0200333 n_chans = req->n_channels;
334 } else {
335 do {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200336 if (local->hw_scan_band == NUM_NL80211_BANDS)
David Spinadelc56ef672014-02-05 15:21:13 +0200337 return false;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100338
David Spinadelc56ef672014-02-05 15:21:13 +0200339 n_chans = 0;
340
341 for (i = 0; i < req->n_channels; i++) {
342 if (req->channels[i]->band !=
343 local->hw_scan_band)
344 continue;
345 local->hw_scan_req->req.channels[n_chans] =
346 req->channels[i];
347 n_chans++;
348 bands_used |= BIT(req->channels[i]->band);
349 }
350
351 local->hw_scan_band++;
352 } while (!n_chans);
353 }
354
355 local->hw_scan_req->req.n_channels = n_chans;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200356 ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100357
Johannes Bergb9771d42018-05-28 15:47:41 +0200358 if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
359 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
360
David Spinadelc56ef672014-02-05 15:21:13 +0200361 ielen = ieee80211_build_preq_ies(local,
362 (u8 *)local->hw_scan_req->req.ie,
Johannes Bergc604b9f2012-11-29 12:45:18 +0100363 local->hw_scan_ies_bufsize,
David Spinadelc56ef672014-02-05 15:21:13 +0200364 &local->hw_scan_req->ies,
365 req->ie, req->ie_len,
Johannes Bergb9771d42018-05-28 15:47:41 +0200366 bands_used, req->rates, &chandef,
367 flags);
David Spinadelc56ef672014-02-05 15:21:13 +0200368 local->hw_scan_req->req.ie_len = ielen;
369 local->hw_scan_req->req.no_cck = req->no_cck;
Johannes Berga344d672014-06-12 22:24:31 +0200370 ether_addr_copy(local->hw_scan_req->req.mac_addr, req->mac_addr);
371 ether_addr_copy(local->hw_scan_req->req.mac_addr_mask,
372 req->mac_addr_mask);
Jouni Malinene345f442016-02-26 22:12:48 +0200373 ether_addr_copy(local->hw_scan_req->req.bssid, req->bssid);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100374
375 return true;
376}
377
Eliad Peller8bd2a242013-12-05 11:21:27 +0200378static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
Johannes Berg0a51b272008-09-08 17:44:25 +0200379{
380 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berge9da68d2018-10-18 10:35:47 +0200381 bool hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
Eliad Pellera2b70e82013-12-05 11:21:28 +0200382 bool was_scanning = local->scanning;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100383 struct cfg80211_scan_request *scan_req;
Johannes Berga344d672014-06-12 22:24:31 +0200384 struct ieee80211_sub_if_data *scan_sdata;
Sachin Kulkarni4fa11ec2016-01-12 14:30:19 +0530385 struct ieee80211_sub_if_data *sdata;
Johannes Berg0a51b272008-09-08 17:44:25 +0200386
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200387 lockdep_assert_held(&local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +0200388
Johannes Berg6d3560d2009-10-31 07:44:08 +0100389 /*
390 * It's ok to abort a not-yet-running scan (that
391 * we have one at all will be verified by checking
392 * local->scan_req next), but not to complete it
393 * successfully.
394 */
395 if (WARN_ON(!local->scanning && !aborted))
396 aborted = true;
Johannes Bergde95a542009-04-01 11:58:36 +0200397
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200398 if (WARN_ON(!local->scan_req))
Johannes Bergd07bfd82011-03-07 15:48:41 +0100399 return;
Johannes Bergf3b85252009-04-23 16:01:47 +0200400
David Spinadelc56ef672014-02-05 15:21:13 +0200401 if (hw_scan && !aborted &&
Johannes Berg30686bf2015-06-02 21:39:54 +0200402 !ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS) &&
David Spinadelc56ef672014-02-05 15:21:13 +0200403 ieee80211_prep_hw_scan(local)) {
Johannes Berge2fd5db2012-07-06 21:39:28 +0200404 int rc;
405
406 rc = drv_hw_scan(local,
407 rcu_dereference_protected(local->scan_sdata,
408 lockdep_is_held(&local->mtx)),
409 local->hw_scan_req);
410
Stanislaw Gruszka6eb11a92010-10-06 11:22:11 +0200411 if (rc == 0)
Johannes Bergd07bfd82011-03-07 15:48:41 +0100412 return;
Avraham Stern7947d3e2016-07-05 15:23:12 +0300413
Johannes Berg7d10f6b2016-07-05 15:23:13 +0300414 /* HW scan failed and is going to be reported as aborted,
415 * so clear old scan info.
Avraham Stern7947d3e2016-07-05 15:23:12 +0300416 */
417 memset(&local->scan_info, 0, sizeof(local->scan_info));
Johannes Berg7d10f6b2016-07-05 15:23:13 +0300418 aborted = true;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100419 }
420
421 kfree(local->hw_scan_req);
422 local->hw_scan_req = NULL;
Johannes Bergf3b85252009-04-23 16:01:47 +0200423
Johannes Berg6ea0a692014-11-19 11:55:49 +0100424 scan_req = rcu_dereference_protected(local->scan_req,
425 lockdep_is_held(&local->mtx));
426
Avraham Stern1d762502016-07-05 17:10:13 +0300427 if (scan_req != local->int_scan_req) {
Avraham Stern7947d3e2016-07-05 15:23:12 +0300428 local->scan_info.aborted = aborted;
429 cfg80211_scan_done(scan_req, &local->scan_info);
Avraham Stern1d762502016-07-05 17:10:13 +0300430 }
Johannes Berg6ea0a692014-11-19 11:55:49 +0100431 RCU_INIT_POINTER(local->scan_req, NULL);
Johannes Berga344d672014-06-12 22:24:31 +0200432
433 scan_sdata = rcu_dereference_protected(local->scan_sdata,
434 lockdep_is_held(&local->mtx));
Monam Agarwal0c2bef462014-03-24 00:51:43 +0530435 RCU_INIT_POINTER(local->scan_sdata, NULL);
Johannes Berg2a519312009-02-10 21:25:55 +0100436
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200437 local->scanning = 0;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200438 local->scan_chandef.chan = NULL;
Johannes Bergf3b85252009-04-23 16:01:47 +0200439
Johannes Berg07ef03e2011-11-08 16:21:21 +0100440 /* Set power back to normal operating levels. */
441 ieee80211_hw_config(local, 0);
Ben Greearb23b0252011-02-04 11:54:17 -0800442
Eliad Peller8bd2a242013-12-05 11:21:27 +0200443 if (!hw_scan) {
Ben Greearb23b0252011-02-04 11:54:17 -0800444 ieee80211_configure_filter(local);
Johannes Berga344d672014-06-12 22:24:31 +0200445 drv_sw_scan_complete(local, scan_sdata);
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +0100446 ieee80211_offchannel_return(local);
Ben Greearb23b0252011-02-04 11:54:17 -0800447 }
448
Johannes Berg5cff20e2009-04-29 12:26:17 +0200449 ieee80211_recalc_idle(local);
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200450
Johannes Berg0a51b272008-09-08 17:44:25 +0200451 ieee80211_mlme_notify_scan_completed(local);
Johannes Berg46900292009-02-15 12:44:28 +0100452 ieee80211_ibss_notify_scan_completed(local);
Sachin Kulkarni4fa11ec2016-01-12 14:30:19 +0530453
454 /* Requeue all the work that might have been ignored while
455 * the scan was in progress; if there was none this will
456 * just be a no-op for the particular interface.
457 */
458 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
459 if (ieee80211_sdata_running(sdata))
460 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
461 }
462
Eliad Pellera2b70e82013-12-05 11:21:28 +0200463 if (was_scanning)
464 ieee80211_start_next_roc(local);
Johannes Berg0a51b272008-09-08 17:44:25 +0200465}
Johannes Berg8789d452010-08-26 13:30:26 +0200466
Avraham Stern7947d3e2016-07-05 15:23:12 +0300467void ieee80211_scan_completed(struct ieee80211_hw *hw,
468 struct cfg80211_scan_info *info)
Johannes Berg8789d452010-08-26 13:30:26 +0200469{
470 struct ieee80211_local *local = hw_to_local(hw);
471
Johannes Berg58bd7f12016-09-14 09:37:54 +0200472 trace_api_scan_completed(local, info->aborted);
Johannes Berg8789d452010-08-26 13:30:26 +0200473
474 set_bit(SCAN_COMPLETED, &local->scanning);
Avraham Stern7947d3e2016-07-05 15:23:12 +0300475 if (info->aborted)
Johannes Berg8789d452010-08-26 13:30:26 +0200476 set_bit(SCAN_ABORTED, &local->scanning);
Avraham Stern7947d3e2016-07-05 15:23:12 +0300477
478 memcpy(&local->scan_info, info, sizeof(*info));
479
Johannes Berg8789d452010-08-26 13:30:26 +0200480 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
481}
Johannes Berg0a51b272008-09-08 17:44:25 +0200482EXPORT_SYMBOL(ieee80211_scan_completed);
483
Johannes Berga344d672014-06-12 22:24:31 +0200484static int ieee80211_start_sw_scan(struct ieee80211_local *local,
485 struct ieee80211_sub_if_data *sdata)
Johannes Bergf3b85252009-04-23 16:01:47 +0200486{
Johannes Bergfe57d9f2012-07-26 14:55:08 +0200487 /* Software scan is not supported in multi-channel cases */
488 if (local->use_chanctx)
489 return -EOPNOTSUPP;
490
Johannes Bergf3b85252009-04-23 16:01:47 +0200491 /*
492 * Hardware/driver doesn't support hw_scan, so use software
493 * scanning instead. First send a nullfunc frame with power save
494 * bit on so that AP will buffer the frames for us while we are not
495 * listening, then send probe requests to each channel and wait for
496 * the responses. After all channels are scanned, tune back to the
497 * original channel and send a nullfunc frame with power save bit
498 * off to trigger the AP to send us all the buffered frames.
499 *
500 * Note that while local->sw_scanning is true everything else but
501 * nullfunc frames and probe requests will be dropped in
502 * ieee80211_tx_h_check_assoc().
503 */
Johannes Berga344d672014-06-12 22:24:31 +0200504 drv_sw_scan_start(local, sdata, local->scan_addr);
Johannes Bergf3b85252009-04-23 16:01:47 +0200505
Rajkumar Manoharande312db2012-03-27 11:01:06 +0530506 local->leave_oper_channel_time = jiffies;
Helmut Schaa977923b2009-07-23 12:14:20 +0200507 local->next_scan_state = SCAN_DECISION;
Johannes Bergf3b85252009-04-23 16:01:47 +0200508 local->scan_channel_idx = 0;
509
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +0100510 ieee80211_offchannel_stop_vifs(local);
Johannes Berga80f7c02009-12-23 13:15:32 +0100511
Seth Forshee9c35d7d2013-02-11 11:21:08 -0600512 /* ensure nullfunc is transmitted before leaving operating channel */
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +0200513 ieee80211_flush_queues(local, NULL, false);
Seth Forshee9c35d7d2013-02-11 11:21:08 -0600514
Johannes Berg3ac64be2009-08-17 16:16:53 +0200515 ieee80211_configure_filter(local);
Johannes Bergf3b85252009-04-23 16:01:47 +0200516
Ben Greear59bdf3b2011-02-07 13:44:38 -0800517 /* We need to set power level at maximum rate for scanning. */
518 ieee80211_hw_config(local, 0);
519
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400520 ieee80211_queue_delayed_work(&local->hw,
Johannes Berg07ef03e2011-11-08 16:21:21 +0100521 &local->scan_work, 0);
Johannes Bergf3b85252009-04-23 16:01:47 +0200522
523 return 0;
524}
525
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200526static bool ieee80211_can_scan(struct ieee80211_local *local,
527 struct ieee80211_sub_if_data *sdata)
528{
Eliad Peller5cbc95a2015-01-07 17:50:09 +0200529 if (ieee80211_is_radar_required(local))
Simon Wunderlich164eb022013-02-08 18:16:20 +0100530 return false;
531
Johannes Berg2eb278e2012-06-05 14:28:42 +0200532 if (!list_empty(&local->roc_list))
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200533 return false;
534
535 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +0200536 sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200537 return false;
538
539 return true;
540}
541
542void ieee80211_run_deferred_scan(struct ieee80211_local *local)
543{
544 lockdep_assert_held(&local->mtx);
545
546 if (!local->scan_req || local->scanning)
547 return;
548
Johannes Berge2fd5db2012-07-06 21:39:28 +0200549 if (!ieee80211_can_scan(local,
550 rcu_dereference_protected(
551 local->scan_sdata,
552 lockdep_is_held(&local->mtx))))
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200553 return;
554
555 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
556 round_jiffies_relative(0));
557}
Johannes Bergf3b85252009-04-23 16:01:47 +0200558
Johannes Berg45ad6832018-05-28 15:47:39 +0200559static void ieee80211_send_scan_probe_req(struct ieee80211_sub_if_data *sdata,
560 const u8 *src, const u8 *dst,
561 const u8 *ssid, size_t ssid_len,
562 const u8 *ie, size_t ie_len,
563 u32 ratemask, u32 flags, u32 tx_flags,
564 struct ieee80211_channel *channel)
565{
566 struct sk_buff *skb;
Johannes Bergb9771d42018-05-28 15:47:41 +0200567 u32 txdata_flags = 0;
Johannes Berg45ad6832018-05-28 15:47:39 +0200568
569 skb = ieee80211_build_probe_req(sdata, src, dst, ratemask, channel,
570 ssid, ssid_len,
571 ie, ie_len, flags);
Johannes Bergb9771d42018-05-28 15:47:41 +0200572
Johannes Berg45ad6832018-05-28 15:47:39 +0200573 if (skb) {
Johannes Bergb9771d42018-05-28 15:47:41 +0200574 if (flags & IEEE80211_PROBE_FLAG_RANDOM_SN) {
575 struct ieee80211_hdr *hdr = (void *)skb->data;
576 u16 sn = get_random_u32();
577
578 txdata_flags |= IEEE80211_TX_NO_SEQNO;
579 hdr->seq_ctrl =
580 cpu_to_le16(IEEE80211_SN_TO_SEQ(sn));
581 }
Johannes Berg45ad6832018-05-28 15:47:39 +0200582 IEEE80211_SKB_CB(skb)->flags |= tx_flags;
Johannes Bergb9771d42018-05-28 15:47:41 +0200583 ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band,
584 txdata_flags);
Johannes Berg45ad6832018-05-28 15:47:39 +0200585 }
586}
587
Ben Greear8a690672012-04-17 10:54:16 -0700588static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
589 unsigned long *next_delay)
590{
591 int i;
Johannes Berge2fd5db2012-07-06 21:39:28 +0200592 struct ieee80211_sub_if_data *sdata;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100593 struct cfg80211_scan_request *scan_req;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200594 enum nl80211_band band = local->hw.conf.chandef.chan->band;
Johannes Bergb9771d42018-05-28 15:47:41 +0200595 u32 flags = 0, tx_flags;
Seth Forshee6c17b772013-02-11 11:21:07 -0600596
Johannes Berg6ea0a692014-11-19 11:55:49 +0100597 scan_req = rcu_dereference_protected(local->scan_req,
598 lockdep_is_held(&local->mtx));
599
Seth Forshee6c17b772013-02-11 11:21:07 -0600600 tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100601 if (scan_req->no_cck)
Seth Forshee6c17b772013-02-11 11:21:07 -0600602 tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
Johannes Bergb9771d42018-05-28 15:47:41 +0200603 if (scan_req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
604 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
605 if (scan_req->flags & NL80211_SCAN_FLAG_RANDOM_SN)
606 flags |= IEEE80211_PROBE_FLAG_RANDOM_SN;
Ben Greear8a690672012-04-17 10:54:16 -0700607
Johannes Berge2fd5db2012-07-06 21:39:28 +0200608 sdata = rcu_dereference_protected(local->scan_sdata,
Peter Senna Tschudin316b6b52012-09-06 18:09:16 +0200609 lockdep_is_held(&local->mtx));
Johannes Berge2fd5db2012-07-06 21:39:28 +0200610
Johannes Berg6ea0a692014-11-19 11:55:49 +0100611 for (i = 0; i < scan_req->n_ssids; i++)
Johannes Berg45ad6832018-05-28 15:47:39 +0200612 ieee80211_send_scan_probe_req(
Jouni Malinene345f442016-02-26 22:12:48 +0200613 sdata, local->scan_addr, scan_req->bssid,
Johannes Berg6ea0a692014-11-19 11:55:49 +0100614 scan_req->ssids[i].ssid, scan_req->ssids[i].ssid_len,
615 scan_req->ie, scan_req->ie_len,
Johannes Bergb9771d42018-05-28 15:47:41 +0200616 scan_req->rates[band], flags,
Johannes Berg45ad6832018-05-28 15:47:39 +0200617 tx_flags, local->hw.conf.chandef.chan);
Ben Greear8a690672012-04-17 10:54:16 -0700618
619 /*
620 * After sending probe requests, wait for probe responses
621 * on the channel.
622 */
623 *next_delay = IEEE80211_CHANNEL_TIME;
624 local->next_scan_state = SCAN_DECISION;
625}
626
Johannes Bergf3b85252009-04-23 16:01:47 +0200627static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
628 struct cfg80211_scan_request *req)
629{
630 struct ieee80211_local *local = sdata->local;
Johannes Berge9da68d2018-10-18 10:35:47 +0200631 bool hw_scan = local->ops->hw_scan;
Johannes Bergf3b85252009-04-23 16:01:47 +0200632 int rc;
633
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200634 lockdep_assert_held(&local->mtx);
635
Eliad Peller2726f232015-01-07 17:50:11 +0200636 if (local->scan_req || ieee80211_is_radar_required(local))
Johannes Bergf3b85252009-04-23 16:01:47 +0200637 return -EBUSY;
638
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200639 if (!ieee80211_can_scan(local, sdata)) {
John W. Linville6e7e6212010-02-08 16:38:38 -0500640 /* wait for the work to finish/time out */
Johannes Berg6ea0a692014-11-19 11:55:49 +0100641 rcu_assign_pointer(local->scan_req, req);
Johannes Berge2fd5db2012-07-06 21:39:28 +0200642 rcu_assign_pointer(local->scan_sdata, sdata);
Johannes Bergc0ce77b2010-02-03 10:22:31 +0100643 return 0;
644 }
645
Johannes Berge9da68d2018-10-18 10:35:47 +0200646 again:
647 if (hw_scan) {
Johannes Bergf3b85252009-04-23 16:01:47 +0200648 u8 *ies;
Johannes Bergf3b85252009-04-23 16:01:47 +0200649
David Spinadele4dcbb32014-02-11 13:45:41 +0200650 local->hw_scan_ies_bufsize = local->scan_ies_len + req->ie_len;
David Spinadelc56ef672014-02-05 15:21:13 +0200651
Johannes Berg30686bf2015-06-02 21:39:54 +0200652 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {
David Spinadelc56ef672014-02-05 15:21:13 +0200653 int i, n_bands = 0;
654 u8 bands_counted = 0;
655
656 for (i = 0; i < req->n_channels; i++) {
657 if (bands_counted & BIT(req->channels[i]->band))
658 continue;
659 bands_counted |= BIT(req->channels[i]->band);
660 n_bands++;
661 }
662
663 local->hw_scan_ies_bufsize *= n_bands;
664 }
665
Johannes Berg4d36ec52009-10-27 20:59:55 +0100666 local->hw_scan_req = kmalloc(
667 sizeof(*local->hw_scan_req) +
668 req->n_channels * sizeof(req->channels[0]) +
Johannes Bergc604b9f2012-11-29 12:45:18 +0100669 local->hw_scan_ies_bufsize, GFP_KERNEL);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100670 if (!local->hw_scan_req)
Johannes Bergf3b85252009-04-23 16:01:47 +0200671 return -ENOMEM;
672
David Spinadelc56ef672014-02-05 15:21:13 +0200673 local->hw_scan_req->req.ssids = req->ssids;
674 local->hw_scan_req->req.n_ssids = req->n_ssids;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100675 ies = (u8 *)local->hw_scan_req +
676 sizeof(*local->hw_scan_req) +
677 req->n_channels * sizeof(req->channels[0]);
David Spinadelc56ef672014-02-05 15:21:13 +0200678 local->hw_scan_req->req.ie = ies;
679 local->hw_scan_req->req.flags = req->flags;
Jouni Malinene345f442016-02-26 22:12:48 +0200680 eth_broadcast_addr(local->hw_scan_req->req.bssid);
Avraham Stern7947d3e2016-07-05 15:23:12 +0300681 local->hw_scan_req->req.duration = req->duration;
682 local->hw_scan_req->req.duration_mandatory =
683 req->duration_mandatory;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100684
685 local->hw_scan_band = 0;
John W. Linville6e7e6212010-02-08 16:38:38 -0500686
687 /*
688 * After allocating local->hw_scan_req, we must
689 * go through until ieee80211_prep_hw_scan(), so
690 * anything that might be changed here and leave
691 * this function early must not go after this
692 * allocation.
693 */
Johannes Bergf3b85252009-04-23 16:01:47 +0200694 }
695
Johannes Berg6ea0a692014-11-19 11:55:49 +0100696 rcu_assign_pointer(local->scan_req, req);
Johannes Berge2fd5db2012-07-06 21:39:28 +0200697 rcu_assign_pointer(local->scan_sdata, sdata);
Johannes Bergf3b85252009-04-23 16:01:47 +0200698
Johannes Berga344d672014-06-12 22:24:31 +0200699 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
700 get_random_mask_addr(local->scan_addr,
701 req->mac_addr,
702 req->mac_addr_mask);
703 else
704 memcpy(local->scan_addr, sdata->vif.addr, ETH_ALEN);
705
Johannes Berge9da68d2018-10-18 10:35:47 +0200706 if (hw_scan) {
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200707 __set_bit(SCAN_HW_SCANNING, &local->scanning);
Ben Greear8a690672012-04-17 10:54:16 -0700708 } else if ((req->n_channels == 1) &&
Karl Beldan675a0b02013-03-25 16:26:57 +0100709 (req->channels[0] == local->_oper_chandef.chan)) {
Johannes Berg9b864872012-07-26 14:38:32 +0200710 /*
711 * If we are scanning only on the operating channel
712 * then we do not need to stop normal activities
Ben Greear8a690672012-04-17 10:54:16 -0700713 */
714 unsigned long next_delay;
715
716 __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
717
718 ieee80211_recalc_idle(local);
719
720 /* Notify driver scan is starting, keep order of operations
721 * same as normal software scan, in case that matters. */
Johannes Berga344d672014-06-12 22:24:31 +0200722 drv_sw_scan_start(local, sdata, local->scan_addr);
Ben Greear8a690672012-04-17 10:54:16 -0700723
724 ieee80211_configure_filter(local); /* accept probe-responses */
725
726 /* We need to ensure power level is at max for scanning. */
727 ieee80211_hw_config(local, 0);
728
Antonio Quartulli4e39cca2015-11-21 18:13:40 +0800729 if ((req->channels[0]->flags & (IEEE80211_CHAN_NO_IR |
730 IEEE80211_CHAN_RADAR)) ||
Johannes Berg6ea0a692014-11-19 11:55:49 +0100731 !req->n_ssids) {
Ben Greear8a690672012-04-17 10:54:16 -0700732 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
733 } else {
734 ieee80211_scan_state_send_probe(local, &next_delay);
735 next_delay = IEEE80211_CHANNEL_TIME;
736 }
737
738 /* Now, just wait a bit and we are all done! */
739 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
740 next_delay);
741 return 0;
742 } else {
743 /* Do normal software scan */
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200744 __set_bit(SCAN_SW_SCANNING, &local->scanning);
Ben Greear8a690672012-04-17 10:54:16 -0700745 }
John W. Linville6e7e6212010-02-08 16:38:38 -0500746
Johannes Berg5cff20e2009-04-29 12:26:17 +0200747 ieee80211_recalc_idle(local);
Johannes Bergf3b85252009-04-23 16:01:47 +0200748
Johannes Berge9da68d2018-10-18 10:35:47 +0200749 if (hw_scan) {
Johannes Berg4d36ec52009-10-27 20:59:55 +0100750 WARN_ON(!ieee80211_prep_hw_scan(local));
Johannes Berga060bbf2010-04-27 11:59:34 +0200751 rc = drv_hw_scan(local, sdata, local->hw_scan_req);
Johannes Berga344d672014-06-12 22:24:31 +0200752 } else {
753 rc = ieee80211_start_sw_scan(local, sdata);
754 }
Johannes Bergf3b85252009-04-23 16:01:47 +0200755
Johannes Bergf3b85252009-04-23 16:01:47 +0200756 if (rc) {
Johannes Berg4d36ec52009-10-27 20:59:55 +0100757 kfree(local->hw_scan_req);
758 local->hw_scan_req = NULL;
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200759 local->scanning = 0;
Johannes Bergf3b85252009-04-23 16:01:47 +0200760
Johannes Berg5cff20e2009-04-29 12:26:17 +0200761 ieee80211_recalc_idle(local);
762
Johannes Bergf3b85252009-04-23 16:01:47 +0200763 local->scan_req = NULL;
Monam Agarwal0c2bef462014-03-24 00:51:43 +0530764 RCU_INIT_POINTER(local->scan_sdata, NULL);
Johannes Bergf3b85252009-04-23 16:01:47 +0200765 }
766
Johannes Berge9da68d2018-10-18 10:35:47 +0200767 if (hw_scan && rc == 1) {
768 /*
769 * we can't fall back to software for P2P-GO
770 * as it must update NoA etc.
771 */
772 if (ieee80211_vif_type_p2p(&sdata->vif) ==
773 NL80211_IFTYPE_P2P_GO)
774 return -EOPNOTSUPP;
775 hw_scan = false;
776 goto again;
777 }
778
Johannes Bergf3b85252009-04-23 16:01:47 +0200779 return rc;
780}
781
Helmut Schaadf13cce2010-02-24 14:19:21 +0100782static unsigned long
783ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
784{
785 /*
786 * TODO: channel switching also consumes quite some time,
787 * add that delay as well to get a better estimation
788 */
Antonio Quartulli4e39cca2015-11-21 18:13:40 +0800789 if (chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR))
Helmut Schaadf13cce2010-02-24 14:19:21 +0100790 return IEEE80211_PASSIVE_CHANNEL_TIME;
791 return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
792}
793
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200794static void ieee80211_scan_state_decision(struct ieee80211_local *local,
795 unsigned long *next_delay)
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200796{
Helmut Schaa142b9f52009-07-23 13:18:01 +0200797 bool associated = false;
Helmut Schaadf13cce2010-02-24 14:19:21 +0100798 bool tx_empty = true;
799 bool bad_latency;
Helmut Schaa142b9f52009-07-23 13:18:01 +0200800 struct ieee80211_sub_if_data *sdata;
Helmut Schaadf13cce2010-02-24 14:19:21 +0100801 struct ieee80211_channel *next_chan;
Sam Lefflercd2bb512012-10-11 21:03:35 -0700802 enum mac80211_scan_state next_scan_state;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100803 struct cfg80211_scan_request *scan_req;
Helmut Schaa142b9f52009-07-23 13:18:01 +0200804
Helmut Schaadf13cce2010-02-24 14:19:21 +0100805 /*
806 * check if at least one STA interface is associated,
807 * check if at least one STA interface has pending tx frames
808 * and grab the lowest used beacon interval
809 */
Helmut Schaa142b9f52009-07-23 13:18:01 +0200810 mutex_lock(&local->iflist_mtx);
811 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +0100812 if (!ieee80211_sdata_running(sdata))
Helmut Schaa142b9f52009-07-23 13:18:01 +0200813 continue;
814
815 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
816 if (sdata->u.mgd.associated) {
817 associated = true;
Helmut Schaadf13cce2010-02-24 14:19:21 +0100818
Helmut Schaadf13cce2010-02-24 14:19:21 +0100819 if (!qdisc_all_tx_empty(sdata->dev)) {
820 tx_empty = false;
821 break;
822 }
Helmut Schaa142b9f52009-07-23 13:18:01 +0200823 }
824 }
825 }
826 mutex_unlock(&local->iflist_mtx);
827
Johannes Berg6ea0a692014-11-19 11:55:49 +0100828 scan_req = rcu_dereference_protected(local->scan_req,
829 lockdep_is_held(&local->mtx));
830
831 next_chan = scan_req->channels[local->scan_channel_idx];
Ben Greearb23b0252011-02-04 11:54:17 -0800832
Johannes Berg07ef03e2011-11-08 16:21:21 +0100833 /*
834 * we're currently scanning a different channel, let's
835 * see if we can scan another channel without interfering
836 * with the current traffic situation.
837 *
Stanislaw Gruszka3f892b62013-01-23 12:32:45 +0100838 * Keep good latency, do not stay off-channel more than 125 ms.
Johannes Berg07ef03e2011-11-08 16:21:21 +0100839 */
Helmut Schaadf13cce2010-02-24 14:19:21 +0100840
Johannes Berg07ef03e2011-11-08 16:21:21 +0100841 bad_latency = time_after(jiffies +
Stanislaw Gruszka3f892b62013-01-23 12:32:45 +0100842 ieee80211_scan_get_channel_time(next_chan),
843 local->leave_oper_channel_time + HZ / 8);
Helmut Schaadf13cce2010-02-24 14:19:21 +0100844
Sam Lefflercd2bb512012-10-11 21:03:35 -0700845 if (associated && !tx_empty) {
Johannes Berg6ea0a692014-11-19 11:55:49 +0100846 if (scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
Sam Lefflercd2bb512012-10-11 21:03:35 -0700847 next_scan_state = SCAN_ABORT;
848 else
849 next_scan_state = SCAN_SUSPEND;
Stanislaw Gruszka3f892b62013-01-23 12:32:45 +0100850 } else if (associated && bad_latency) {
Sam Lefflercd2bb512012-10-11 21:03:35 -0700851 next_scan_state = SCAN_SUSPEND;
852 } else {
853 next_scan_state = SCAN_SET_CHANNEL;
854 }
855
856 local->next_scan_state = next_scan_state;
Helmut Schaa142b9f52009-07-23 13:18:01 +0200857
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200858 *next_delay = 0;
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200859}
860
861static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
862 unsigned long *next_delay)
863{
864 int skip;
865 struct ieee80211_channel *chan;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200866 enum nl80211_bss_scan_width oper_scan_width;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100867 struct cfg80211_scan_request *scan_req;
868
869 scan_req = rcu_dereference_protected(local->scan_req,
870 lockdep_is_held(&local->mtx));
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200871
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200872 skip = 0;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100873 chan = scan_req->channels[local->scan_channel_idx];
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200874
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200875 local->scan_chandef.chan = chan;
876 local->scan_chandef.center_freq1 = chan->center_freq;
877 local->scan_chandef.center_freq2 = 0;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100878 switch (scan_req->scan_width) {
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200879 case NL80211_BSS_CHAN_WIDTH_5:
880 local->scan_chandef.width = NL80211_CHAN_WIDTH_5;
881 break;
882 case NL80211_BSS_CHAN_WIDTH_10:
883 local->scan_chandef.width = NL80211_CHAN_WIDTH_10;
884 break;
885 case NL80211_BSS_CHAN_WIDTH_20:
886 /* If scanning on oper channel, use whatever channel-type
887 * is currently in use.
888 */
889 oper_scan_width = cfg80211_chandef_to_scan_width(
890 &local->_oper_chandef);
891 if (chan == local->_oper_chandef.chan &&
Johannes Berg6ea0a692014-11-19 11:55:49 +0100892 oper_scan_width == scan_req->scan_width)
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200893 local->scan_chandef = local->_oper_chandef;
894 else
895 local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
896 break;
897 }
Ben Greearb23b0252011-02-04 11:54:17 -0800898
Johannes Berg07ef03e2011-11-08 16:21:21 +0100899 if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
900 skip = 1;
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200901
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200902 /* advance state machine to next channel/band */
903 local->scan_channel_idx++;
904
Helmut Schaa0ee9c132009-07-25 17:25:51 +0200905 if (skip) {
906 /* if we skip this channel return to the decision state */
907 local->next_scan_state = SCAN_DECISION;
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200908 return;
Helmut Schaa0ee9c132009-07-25 17:25:51 +0200909 }
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200910
911 /*
912 * Probe delay is used to update the NAV, cf. 11.1.3.2.2
913 * (which unfortunately doesn't say _why_ step a) is done,
914 * but it waits for the probe delay or until a frame is
915 * received - and the received frame would update the NAV).
916 * For now, we do not support waiting until a frame is
917 * received.
918 *
919 * In any case, it is not necessary for a passive scan.
920 */
Antonio Quartulli4e39cca2015-11-21 18:13:40 +0800921 if ((chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) ||
922 !scan_req->n_ssids) {
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200923 *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Helmut Schaa977923b2009-07-23 12:14:20 +0200924 local->next_scan_state = SCAN_DECISION;
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200925 return;
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200926 }
927
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200928 /* active scan, send probes */
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200929 *next_delay = IEEE80211_PROBE_DELAY;
Helmut Schaa977923b2009-07-23 12:14:20 +0200930 local->next_scan_state = SCAN_SEND_PROBE;
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200931}
932
Johannes Berg07ef03e2011-11-08 16:21:21 +0100933static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
934 unsigned long *next_delay)
935{
936 /* switch back to the operating channel */
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200937 local->scan_chandef.chan = NULL;
Johannes Berg07ef03e2011-11-08 16:21:21 +0100938 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
939
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +0100940 /* disable PS */
941 ieee80211_offchannel_return(local);
Johannes Berg07ef03e2011-11-08 16:21:21 +0100942
943 *next_delay = HZ / 5;
944 /* afterwards, resume scan & go to next channel */
945 local->next_scan_state = SCAN_RESUME;
946}
947
948static void ieee80211_scan_state_resume(struct ieee80211_local *local,
949 unsigned long *next_delay)
950{
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +0100951 ieee80211_offchannel_stop_vifs(local);
Johannes Berg07ef03e2011-11-08 16:21:21 +0100952
953 if (local->ops->flush) {
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +0200954 ieee80211_flush_queues(local, NULL, false);
Johannes Berg07ef03e2011-11-08 16:21:21 +0100955 *next_delay = 0;
956 } else
957 *next_delay = HZ / 10;
958
959 /* remember when we left the operating channel */
960 local->leave_oper_channel_time = jiffies;
961
962 /* advance to the next channel to be scanned */
Mohammed Shafi Shajakhande2ee842011-12-24 18:43:28 +0530963 local->next_scan_state = SCAN_SET_CHANNEL;
Johannes Berg07ef03e2011-11-08 16:21:21 +0100964}
965
Johannes Bergc2b13452008-09-11 00:01:55 +0200966void ieee80211_scan_work(struct work_struct *work)
Johannes Berg0a51b272008-09-08 17:44:25 +0200967{
968 struct ieee80211_local *local =
969 container_of(work, struct ieee80211_local, scan_work.work);
Johannes Bergd07bfd82011-03-07 15:48:41 +0100970 struct ieee80211_sub_if_data *sdata;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100971 struct cfg80211_scan_request *scan_req;
Johannes Berg0a51b272008-09-08 17:44:25 +0200972 unsigned long next_delay = 0;
Eliad Peller8bd2a242013-12-05 11:21:27 +0200973 bool aborted;
Johannes Berg8789d452010-08-26 13:30:26 +0200974
Johannes Berga1699b72010-07-30 16:46:07 +0200975 mutex_lock(&local->mtx);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +0200976
Luciano Coelho332ff7f2015-01-22 23:34:10 +0200977 if (!ieee80211_can_run_worker(local)) {
978 aborted = true;
979 goto out_complete;
980 }
981
Johannes Berge2fd5db2012-07-06 21:39:28 +0200982 sdata = rcu_dereference_protected(local->scan_sdata,
983 lockdep_is_held(&local->mtx));
Johannes Berg6ea0a692014-11-19 11:55:49 +0100984 scan_req = rcu_dereference_protected(local->scan_req,
985 lockdep_is_held(&local->mtx));
Johannes Bergd07bfd82011-03-07 15:48:41 +0100986
Ben Greear8a690672012-04-17 10:54:16 -0700987 /* When scanning on-channel, the first-callback means completed. */
988 if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) {
989 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
990 goto out_complete;
991 }
992
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +0200993 if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
994 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
995 goto out_complete;
Johannes Bergf3b85252009-04-23 16:01:47 +0200996 }
997
Johannes Berg6ea0a692014-11-19 11:55:49 +0100998 if (!sdata || !scan_req)
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +0200999 goto out;
1000
Eliad Pellerff5db432014-11-12 10:08:29 +02001001 if (!local->scanning) {
Johannes Bergf3b85252009-04-23 16:01:47 +02001002 int rc;
1003
Johannes Berg6ea0a692014-11-19 11:55:49 +01001004 RCU_INIT_POINTER(local->scan_req, NULL);
Monam Agarwal0c2bef462014-03-24 00:51:43 +05301005 RCU_INIT_POINTER(local->scan_sdata, NULL);
Johannes Bergf3b85252009-04-23 16:01:47 +02001006
Johannes Berg6ea0a692014-11-19 11:55:49 +01001007 rc = __ieee80211_start_scan(sdata, scan_req);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001008 if (rc) {
Stanislaw Gruszka3aed49e2010-10-06 11:22:12 +02001009 /* need to complete scan in cfg80211 */
Johannes Berg6ea0a692014-11-19 11:55:49 +01001010 rcu_assign_pointer(local->scan_req, scan_req);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001011 aborted = true;
1012 goto out_complete;
1013 } else
1014 goto out;
Johannes Bergf3b85252009-04-23 16:01:47 +02001015 }
1016
Johannes Berg5bc75722008-09-11 00:01:51 +02001017 /*
Helmut Schaaf502d092009-07-23 12:13:48 +02001018 * as long as no delay is required advance immediately
1019 * without scheduling a new work
1020 */
1021 do {
Rajkumar Manoharanc29acf22011-05-14 09:43:28 +05301022 if (!ieee80211_sdata_running(sdata)) {
1023 aborted = true;
1024 goto out_complete;
1025 }
1026
Helmut Schaa977923b2009-07-23 12:14:20 +02001027 switch (local->next_scan_state) {
Helmut Schaa2fb3f022009-07-23 12:13:56 +02001028 case SCAN_DECISION:
Stanislaw Gruszkae229f842010-10-06 11:22:09 +02001029 /* if no more bands/channels left, complete scan */
Johannes Berg6ea0a692014-11-19 11:55:49 +01001030 if (local->scan_channel_idx >= scan_req->n_channels) {
Stanislaw Gruszkae229f842010-10-06 11:22:09 +02001031 aborted = false;
1032 goto out_complete;
1033 }
1034 ieee80211_scan_state_decision(local, &next_delay);
Helmut Schaaf502d092009-07-23 12:13:48 +02001035 break;
Helmut Schaa2fb3f022009-07-23 12:13:56 +02001036 case SCAN_SET_CHANNEL:
1037 ieee80211_scan_state_set_channel(local, &next_delay);
1038 break;
Helmut Schaaf502d092009-07-23 12:13:48 +02001039 case SCAN_SEND_PROBE:
1040 ieee80211_scan_state_send_probe(local, &next_delay);
1041 break;
Johannes Berg07ef03e2011-11-08 16:21:21 +01001042 case SCAN_SUSPEND:
1043 ieee80211_scan_state_suspend(local, &next_delay);
Helmut Schaa142b9f52009-07-23 13:18:01 +02001044 break;
Johannes Berg07ef03e2011-11-08 16:21:21 +01001045 case SCAN_RESUME:
1046 ieee80211_scan_state_resume(local, &next_delay);
Helmut Schaa142b9f52009-07-23 13:18:01 +02001047 break;
Sam Lefflercd2bb512012-10-11 21:03:35 -07001048 case SCAN_ABORT:
1049 aborted = true;
1050 goto out_complete;
Helmut Schaaf502d092009-07-23 12:13:48 +02001051 }
1052 } while (next_delay == 0);
Johannes Berg0a51b272008-09-08 17:44:25 +02001053
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001054 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
Johannes Bergd07bfd82011-03-07 15:48:41 +01001055 goto out;
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001056
1057out_complete:
Eliad Peller8bd2a242013-12-05 11:21:27 +02001058 __ieee80211_scan_completed(&local->hw, aborted);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001059out:
1060 mutex_unlock(&local->mtx);
Johannes Berg0a51b272008-09-08 17:44:25 +02001061}
1062
Johannes Bergc2b13452008-09-11 00:01:55 +02001063int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
Johannes Berg2a519312009-02-10 21:25:55 +01001064 struct cfg80211_scan_request *req)
Johannes Berg0a51b272008-09-08 17:44:25 +02001065{
Johannes Bergf3b85252009-04-23 16:01:47 +02001066 int res;
1067
Johannes Berga1699b72010-07-30 16:46:07 +02001068 mutex_lock(&sdata->local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +02001069 res = __ieee80211_start_scan(sdata, req);
Johannes Berga1699b72010-07-30 16:46:07 +02001070 mutex_unlock(&sdata->local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +02001071
1072 return res;
1073}
1074
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001075int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
1076 const u8 *ssid, u8 ssid_len,
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001077 struct ieee80211_channel **channels,
1078 unsigned int n_channels,
Simon Wunderlich7ca15a02013-07-08 16:55:56 +02001079 enum nl80211_bss_scan_width scan_width)
Johannes Bergf3b85252009-04-23 16:01:47 +02001080{
Johannes Berg0a51b272008-09-08 17:44:25 +02001081 struct ieee80211_local *local = sdata->local;
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001082 int ret = -EBUSY, i, n_ch = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001083 enum nl80211_band band;
Johannes Berg0a51b272008-09-08 17:44:25 +02001084
Johannes Berga1699b72010-07-30 16:46:07 +02001085 mutex_lock(&local->mtx);
Johannes Berg2a519312009-02-10 21:25:55 +01001086
Johannes Bergf3b85252009-04-23 16:01:47 +02001087 /* busy scanning */
1088 if (local->scan_req)
1089 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01001090
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001091 /* fill internal scan request */
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001092 if (!channels) {
1093 int max_n;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001094
Johannes Berg57fbcce2016-04-12 15:56:15 +02001095 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001096 if (!local->hw.wiphy->bands[band])
1097 continue;
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001098
1099 max_n = local->hw.wiphy->bands[band]->n_channels;
1100 for (i = 0; i < max_n; i++) {
1101 struct ieee80211_channel *tmp_ch =
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001102 &local->hw.wiphy->bands[band]->channels[i];
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001103
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001104 if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR |
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001105 IEEE80211_CHAN_DISABLED))
1106 continue;
1107
1108 local->int_scan_req->channels[n_ch] = tmp_ch;
1109 n_ch++;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001110 }
1111 }
1112
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001113 if (WARN_ON_ONCE(n_ch == 0))
1114 goto unlock;
1115
1116 local->int_scan_req->n_channels = n_ch;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001117 } else {
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001118 for (i = 0; i < n_channels; i++) {
1119 if (channels[i]->flags & (IEEE80211_CHAN_NO_IR |
1120 IEEE80211_CHAN_DISABLED))
1121 continue;
1122
1123 local->int_scan_req->channels[n_ch] = channels[i];
1124 n_ch++;
1125 }
1126
1127 if (WARN_ON_ONCE(n_ch == 0))
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001128 goto unlock;
1129
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001130 local->int_scan_req->n_channels = n_ch;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001131 }
1132
1133 local->int_scan_req->ssids = &local->scan_ssid;
1134 local->int_scan_req->n_ssids = 1;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +02001135 local->int_scan_req->scan_width = scan_width;
Johannes Berg5ba63532009-08-07 17:54:07 +02001136 memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
1137 local->int_scan_req->ssids[0].ssid_len = ssid_len;
Johannes Berg2a519312009-02-10 21:25:55 +01001138
Johannes Berg5ba63532009-08-07 17:54:07 +02001139 ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
Johannes Bergf3b85252009-04-23 16:01:47 +02001140 unlock:
Johannes Berga1699b72010-07-30 16:46:07 +02001141 mutex_unlock(&local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +02001142 return ret;
Johannes Berg0a51b272008-09-08 17:44:25 +02001143}
Johannes Berg5bb644a2009-05-17 11:40:42 +02001144
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001145/*
1146 * Only call this function when a scan can't be queued -- under RTNL.
1147 */
Johannes Berg5bb644a2009-05-17 11:40:42 +02001148void ieee80211_scan_cancel(struct ieee80211_local *local)
1149{
Johannes Berg5bb644a2009-05-17 11:40:42 +02001150 /*
Eliad Pellerb8564392011-06-13 12:47:30 +03001151 * We are canceling software scan, or deferred scan that was not
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001152 * yet really started (see __ieee80211_start_scan ).
1153 *
1154 * Regarding hardware scan:
1155 * - we can not call __ieee80211_scan_completed() as when
1156 * SCAN_HW_SCANNING bit is set this function change
1157 * local->hw_scan_req to operate on 5G band, what race with
1158 * driver which can use local->hw_scan_req
1159 *
1160 * - we can not cancel scan_work since driver can schedule it
1161 * by ieee80211_scan_completed(..., true) to finish scan
1162 *
Eliad Pellerb8564392011-06-13 12:47:30 +03001163 * Hence we only call the cancel_hw_scan() callback, but the low-level
1164 * driver is still responsible for calling ieee80211_scan_completed()
1165 * after the scan was completed/aborted.
Johannes Berg5bb644a2009-05-17 11:40:42 +02001166 */
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001167
Johannes Berga1699b72010-07-30 16:46:07 +02001168 mutex_lock(&local->mtx);
Eliad Pellerb8564392011-06-13 12:47:30 +03001169 if (!local->scan_req)
1170 goto out;
1171
Emmanuel Grumbacha7540552013-09-16 11:12:07 +03001172 /*
1173 * We have a scan running and the driver already reported completion,
1174 * but the worker hasn't run yet or is stuck on the mutex - mark it as
1175 * cancelled.
1176 */
1177 if (test_bit(SCAN_HW_SCANNING, &local->scanning) &&
1178 test_bit(SCAN_COMPLETED, &local->scanning)) {
1179 set_bit(SCAN_HW_CANCELLED, &local->scanning);
1180 goto out;
1181 }
1182
Eliad Pellerb8564392011-06-13 12:47:30 +03001183 if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
Emmanuel Grumbacha7540552013-09-16 11:12:07 +03001184 /*
1185 * Make sure that __ieee80211_scan_completed doesn't trigger a
1186 * scan on another band.
1187 */
1188 set_bit(SCAN_HW_CANCELLED, &local->scanning);
Eliad Pellerb8564392011-06-13 12:47:30 +03001189 if (local->ops->cancel_hw_scan)
Johannes Berge2fd5db2012-07-06 21:39:28 +02001190 drv_cancel_hw_scan(local,
1191 rcu_dereference_protected(local->scan_sdata,
1192 lockdep_is_held(&local->mtx)));
Eliad Pellerb8564392011-06-13 12:47:30 +03001193 goto out;
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001194 }
Eliad Pellerb8564392011-06-13 12:47:30 +03001195
1196 /*
1197 * If the work is currently running, it must be blocked on
1198 * the mutex, but we'll set scan_sdata = NULL and it'll
1199 * simply exit once it acquires the mutex.
1200 */
1201 cancel_delayed_work(&local->scan_work);
1202 /* and clean up */
Avraham Stern7947d3e2016-07-05 15:23:12 +03001203 memset(&local->scan_info, 0, sizeof(local->scan_info));
Eliad Peller8bd2a242013-12-05 11:21:27 +02001204 __ieee80211_scan_completed(&local->hw, true);
Eliad Pellerb8564392011-06-13 12:47:30 +03001205out:
Johannes Bergd07bfd82011-03-07 15:48:41 +01001206 mutex_unlock(&local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02001207}
Luciano Coelho79f460c2011-05-11 17:09:36 +03001208
David Spinadeld43c6b62013-12-08 21:48:57 +02001209int __ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
1210 struct cfg80211_sched_scan_request *req)
Luciano Coelho79f460c2011-05-11 17:09:36 +03001211{
1212 struct ieee80211_local *local = sdata->local;
David Spinadel633e2712014-02-06 16:15:23 +02001213 struct ieee80211_scan_ies sched_scan_ies = {};
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001214 struct cfg80211_chan_def chandef;
David Spinadel633e2712014-02-06 16:15:23 +02001215 int ret, i, iebufsz, num_bands = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001216 u32 rate_masks[NUM_NL80211_BANDS] = {};
David Spinadel633e2712014-02-06 16:15:23 +02001217 u8 bands_used = 0;
1218 u8 *ie;
Johannes Bergb9771d42018-05-28 15:47:41 +02001219 u32 flags = 0;
Johannes Bergc604b9f2012-11-29 12:45:18 +01001220
David Spinadele4dcbb32014-02-11 13:45:41 +02001221 iebufsz = local->scan_ies_len + req->ie_len;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001222
David Spinadeld43c6b62013-12-08 21:48:57 +02001223 lockdep_assert_held(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001224
David Spinadeld43c6b62013-12-08 21:48:57 +02001225 if (!local->ops->sched_scan_start)
1226 return -ENOTSUPP;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001227
Johannes Berg57fbcce2016-04-12 15:56:15 +02001228 for (i = 0; i < NUM_NL80211_BANDS; i++) {
David Spinadel633e2712014-02-06 16:15:23 +02001229 if (local->hw.wiphy->bands[i]) {
1230 bands_used |= BIT(i);
1231 rate_masks[i] = (u32) -1;
1232 num_bands++;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001233 }
Luciano Coelho79f460c2011-05-11 17:09:36 +03001234 }
1235
Johannes Bergb9771d42018-05-28 15:47:41 +02001236 if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
1237 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
1238
Kees Cook6396bb22018-06-12 14:03:40 -07001239 ie = kcalloc(iebufsz, num_bands, GFP_KERNEL);
David Spinadel633e2712014-02-06 16:15:23 +02001240 if (!ie) {
1241 ret = -ENOMEM;
1242 goto out;
1243 }
1244
1245 ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
1246
Kirtika Ruchandanicd5861b2016-11-23 20:45:49 -08001247 ieee80211_build_preq_ies(local, ie, num_bands * iebufsz,
1248 &sched_scan_ies, req->ie,
Johannes Berg00387f32018-05-28 15:47:38 +02001249 req->ie_len, bands_used, rate_masks, &chandef,
Johannes Bergb9771d42018-05-28 15:47:41 +02001250 flags);
David Spinadel633e2712014-02-06 16:15:23 +02001251
Johannes Berg30dd3ed2012-09-04 19:15:01 +02001252 ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies);
David Spinadeld43c6b62013-12-08 21:48:57 +02001253 if (ret == 0) {
Johannes Berg5260a5b2012-07-06 21:55:11 +02001254 rcu_assign_pointer(local->sched_scan_sdata, sdata);
Johannes Berg6ea0a692014-11-19 11:55:49 +01001255 rcu_assign_pointer(local->sched_scan_req, req);
David Spinadeld43c6b62013-12-08 21:48:57 +02001256 }
Luciano Coelho79f460c2011-05-11 17:09:36 +03001257
David Spinadel633e2712014-02-06 16:15:23 +02001258 kfree(ie);
David Spinadeld43c6b62013-12-08 21:48:57 +02001259
David Spinadel633e2712014-02-06 16:15:23 +02001260out:
David Spinadeld43c6b62013-12-08 21:48:57 +02001261 if (ret) {
1262 /* Clean in case of failure after HW restart or upon resume. */
Monam Agarwal0c2bef462014-03-24 00:51:43 +05301263 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
Johannes Berg6ea0a692014-11-19 11:55:49 +01001264 RCU_INIT_POINTER(local->sched_scan_req, NULL);
David Spinadeld43c6b62013-12-08 21:48:57 +02001265 }
1266
1267 return ret;
1268}
1269
1270int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
1271 struct cfg80211_sched_scan_request *req)
1272{
1273 struct ieee80211_local *local = sdata->local;
1274 int ret;
1275
1276 mutex_lock(&local->mtx);
1277
1278 if (rcu_access_pointer(local->sched_scan_sdata)) {
1279 mutex_unlock(&local->mtx);
1280 return -EBUSY;
1281 }
1282
1283 ret = __ieee80211_request_sched_scan_start(sdata, req);
1284
Johannes Berg5260a5b2012-07-06 21:55:11 +02001285 mutex_unlock(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001286 return ret;
1287}
1288
Eliad Peller0d440ea2015-10-25 10:59:33 +02001289int ieee80211_request_sched_scan_stop(struct ieee80211_local *local)
Luciano Coelho79f460c2011-05-11 17:09:36 +03001290{
Eliad Peller0d440ea2015-10-25 10:59:33 +02001291 struct ieee80211_sub_if_data *sched_scan_sdata;
1292 int ret = -ENOENT;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001293
Johannes Berg5260a5b2012-07-06 21:55:11 +02001294 mutex_lock(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001295
1296 if (!local->ops->sched_scan_stop) {
1297 ret = -ENOTSUPP;
1298 goto out;
1299 }
1300
David Spinadeld43c6b62013-12-08 21:48:57 +02001301 /* We don't want to restart sched scan anymore. */
Johannes Berg6ea0a692014-11-19 11:55:49 +01001302 RCU_INIT_POINTER(local->sched_scan_req, NULL);
David Spinadeld43c6b62013-12-08 21:48:57 +02001303
Eliad Peller0d440ea2015-10-25 10:59:33 +02001304 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
1305 lockdep_is_held(&local->mtx));
1306 if (sched_scan_sdata) {
1307 ret = drv_sched_scan_stop(local, sched_scan_sdata);
Alexander Bondar71228a12014-03-16 10:49:54 +02001308 if (!ret)
Andreea-Cristina Bernatad053a92014-08-22 16:14:49 +03001309 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
Alexander Bondar71228a12014-03-16 10:49:54 +02001310 }
Luciano Coelho79f460c2011-05-11 17:09:36 +03001311out:
Johannes Berg5260a5b2012-07-06 21:55:11 +02001312 mutex_unlock(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001313
1314 return ret;
1315}
1316
1317void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
1318{
1319 struct ieee80211_local *local = hw_to_local(hw);
1320
1321 trace_api_sched_scan_results(local);
1322
Arend Van Sprielb34939b2017-04-28 13:40:28 +01001323 cfg80211_sched_scan_results(hw->wiphy, 0);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001324}
1325EXPORT_SYMBOL(ieee80211_sched_scan_results);
1326
Johannes Bergf6837ba82014-04-30 14:19:04 +02001327void ieee80211_sched_scan_end(struct ieee80211_local *local)
Luciano Coelho85a99942011-05-12 16:28:29 +03001328{
Luciano Coelho85a99942011-05-12 16:28:29 +03001329 mutex_lock(&local->mtx);
1330
Johannes Berg5260a5b2012-07-06 21:55:11 +02001331 if (!rcu_access_pointer(local->sched_scan_sdata)) {
Luciano Coelho85a99942011-05-12 16:28:29 +03001332 mutex_unlock(&local->mtx);
1333 return;
1334 }
1335
Monam Agarwal0c2bef462014-03-24 00:51:43 +05301336 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
Luciano Coelho85a99942011-05-12 16:28:29 +03001337
David Spinadeld43c6b62013-12-08 21:48:57 +02001338 /* If sched scan was aborted by the driver. */
Johannes Berg6ea0a692014-11-19 11:55:49 +01001339 RCU_INIT_POINTER(local->sched_scan_req, NULL);
David Spinadeld43c6b62013-12-08 21:48:57 +02001340
Luciano Coelho85a99942011-05-12 16:28:29 +03001341 mutex_unlock(&local->mtx);
1342
Arend Van Sprielb34939b2017-04-28 13:40:28 +01001343 cfg80211_sched_scan_stopped(local->hw.wiphy, 0);
Luciano Coelho85a99942011-05-12 16:28:29 +03001344}
1345
Johannes Bergf6837ba82014-04-30 14:19:04 +02001346void ieee80211_sched_scan_stopped_work(struct work_struct *work)
1347{
1348 struct ieee80211_local *local =
1349 container_of(work, struct ieee80211_local,
1350 sched_scan_stopped_work);
1351
1352 ieee80211_sched_scan_end(local);
1353}
1354
Luciano Coelho79f460c2011-05-11 17:09:36 +03001355void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
1356{
1357 struct ieee80211_local *local = hw_to_local(hw);
1358
1359 trace_api_sched_scan_stopped(local);
1360
Eliad Peller2bc533b2016-01-05 16:28:06 +02001361 /*
1362 * this shouldn't really happen, so for simplicity
1363 * simply ignore it, and let mac80211 reconfigure
1364 * the sched scan later on.
1365 */
1366 if (local->in_reconfig)
1367 return;
1368
Johannes Berg18db5942013-11-06 10:34:36 +01001369 schedule_work(&local->sched_scan_stopped_work);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001370}
1371EXPORT_SYMBOL(ieee80211_sched_scan_stopped);