blob: 20211cbc63f4feb3796749d0808471175c1442f1 [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);
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100147 struct cfg80211_bss *cbss;
Johannes Bergc2b13452008-09-11 00:01:55 +0200148 struct ieee80211_bss *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
Johannes Berg5484e232008-09-08 17:44:27 +0200215 return bss;
216}
Johannes Berg0a51b272008-09-08 17:44:25 +0200217
Roee Zamir40b0bd22017-08-06 11:38:23 +0300218static bool ieee80211_scan_accept_presp(struct ieee80211_sub_if_data *sdata,
219 u32 scan_flags, const u8 *da)
220{
221 if (!sdata)
222 return false;
223 /* accept broadcast for OCE */
224 if (scan_flags & NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP &&
225 is_broadcast_ether_addr(da))
226 return true;
227 if (scan_flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
228 return true;
229 return ether_addr_equal(da, sdata->vif.addr);
230}
231
Johannes Bergd48b2962012-07-06 22:19:27 +0200232void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb)
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200233{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200234 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
Johannes Bergd48b2962012-07-06 22:19:27 +0200235 struct ieee80211_sub_if_data *sdata1, *sdata2;
236 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Johannes Bergc2b13452008-09-11 00:01:55 +0200237 struct ieee80211_bss *bss;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200238 struct ieee80211_channel *channel;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200239
Johannes Bergd48b2962012-07-06 22:19:27 +0200240 if (skb->len < 24 ||
241 (!ieee80211_is_probe_resp(mgmt->frame_control) &&
242 !ieee80211_is_beacon(mgmt->frame_control)))
243 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200244
Johannes Bergd48b2962012-07-06 22:19:27 +0200245 sdata1 = rcu_dereference(local->scan_sdata);
246 sdata2 = rcu_dereference(local->sched_scan_sdata);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200247
Johannes Bergd48b2962012-07-06 22:19:27 +0200248 if (likely(!sdata1 && !sdata2))
249 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200250
Johannes Bergd48b2962012-07-06 22:19:27 +0200251 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
Johannes Berga344d672014-06-12 22:24:31 +0200252 struct cfg80211_scan_request *scan_req;
253 struct cfg80211_sched_scan_request *sched_scan_req;
Roee Zamir40b0bd22017-08-06 11:38:23 +0300254 u32 scan_req_flags = 0, sched_scan_req_flags = 0;
Johannes Berga344d672014-06-12 22:24:31 +0200255
256 scan_req = rcu_dereference(local->scan_req);
257 sched_scan_req = rcu_dereference(local->sched_scan_req);
258
Roee Zamir40b0bd22017-08-06 11:38:23 +0300259 if (scan_req)
260 scan_req_flags = scan_req->flags;
261
262 if (sched_scan_req)
263 sched_scan_req_flags = sched_scan_req->flags;
264
265 /* ignore ProbeResp to foreign address or non-bcast (OCE)
266 * unless scanning with randomised address
Johannes Berga344d672014-06-12 22:24:31 +0200267 */
Roee Zamir40b0bd22017-08-06 11:38:23 +0300268 if (!ieee80211_scan_accept_presp(sdata1, scan_req_flags,
269 mgmt->da) &&
270 !ieee80211_scan_accept_presp(sdata2, sched_scan_req_flags,
271 mgmt->da))
Johannes Bergd48b2962012-07-06 22:19:27 +0200272 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200273 }
274
Johannes Berg0172bb72012-11-23 14:23:30 +0100275 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200276
277 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
Johannes Bergd48b2962012-07-06 22:19:27 +0200278 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200279
Johannes Bergd48b2962012-07-06 22:19:27 +0200280 bss = ieee80211_bss_info_update(local, rx_status,
Sara Sharon4abb52a2019-01-16 12:14:41 +0200281 mgmt, skb->len,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200282 channel);
Jouni Malinend048e502008-10-11 03:29:55 +0300283 if (bss)
Johannes Bergd48b2962012-07-06 22:19:27 +0200284 ieee80211_rx_bss_put(local, bss);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200285}
286
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200287static void
288ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef,
289 enum nl80211_bss_scan_width scan_width)
290{
291 memset(chandef, 0, sizeof(*chandef));
292 switch (scan_width) {
293 case NL80211_BSS_CHAN_WIDTH_5:
294 chandef->width = NL80211_CHAN_WIDTH_5;
295 break;
296 case NL80211_BSS_CHAN_WIDTH_10:
297 chandef->width = NL80211_CHAN_WIDTH_10;
298 break;
299 default:
300 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
301 break;
302 }
303}
304
Johannes Berg4d36ec52009-10-27 20:59:55 +0100305/* return false if no more work */
306static bool ieee80211_prep_hw_scan(struct ieee80211_local *local)
307{
Johannes Berg6ea0a692014-11-19 11:55:49 +0100308 struct cfg80211_scan_request *req;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200309 struct cfg80211_chan_def chandef;
David Spinadelc56ef672014-02-05 15:21:13 +0200310 u8 bands_used = 0;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100311 int i, ielen, n_chans;
Johannes Bergb9771d42018-05-28 15:47:41 +0200312 u32 flags = 0;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100313
Johannes Berg6ea0a692014-11-19 11:55:49 +0100314 req = rcu_dereference_protected(local->scan_req,
315 lockdep_is_held(&local->mtx));
316
Emmanuel Grumbacha7540552013-09-16 11:12:07 +0300317 if (test_bit(SCAN_HW_CANCELLED, &local->scanning))
318 return false;
319
Johannes Berg30686bf2015-06-02 21:39:54 +0200320 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {
Johannes Berg4d36ec52009-10-27 20:59:55 +0100321 for (i = 0; i < req->n_channels; i++) {
David Spinadelc56ef672014-02-05 15:21:13 +0200322 local->hw_scan_req->req.channels[i] = req->channels[i];
323 bands_used |= BIT(req->channels[i]->band);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100324 }
325
David Spinadelc56ef672014-02-05 15:21:13 +0200326 n_chans = req->n_channels;
327 } else {
328 do {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200329 if (local->hw_scan_band == NUM_NL80211_BANDS)
David Spinadelc56ef672014-02-05 15:21:13 +0200330 return false;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100331
David Spinadelc56ef672014-02-05 15:21:13 +0200332 n_chans = 0;
333
334 for (i = 0; i < req->n_channels; i++) {
335 if (req->channels[i]->band !=
336 local->hw_scan_band)
337 continue;
338 local->hw_scan_req->req.channels[n_chans] =
339 req->channels[i];
340 n_chans++;
341 bands_used |= BIT(req->channels[i]->band);
342 }
343
344 local->hw_scan_band++;
345 } while (!n_chans);
346 }
347
348 local->hw_scan_req->req.n_channels = n_chans;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200349 ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100350
Johannes Bergb9771d42018-05-28 15:47:41 +0200351 if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
352 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
353
David Spinadelc56ef672014-02-05 15:21:13 +0200354 ielen = ieee80211_build_preq_ies(local,
355 (u8 *)local->hw_scan_req->req.ie,
Johannes Bergc604b9f2012-11-29 12:45:18 +0100356 local->hw_scan_ies_bufsize,
David Spinadelc56ef672014-02-05 15:21:13 +0200357 &local->hw_scan_req->ies,
358 req->ie, req->ie_len,
Johannes Bergb9771d42018-05-28 15:47:41 +0200359 bands_used, req->rates, &chandef,
360 flags);
David Spinadelc56ef672014-02-05 15:21:13 +0200361 local->hw_scan_req->req.ie_len = ielen;
362 local->hw_scan_req->req.no_cck = req->no_cck;
Johannes Berga344d672014-06-12 22:24:31 +0200363 ether_addr_copy(local->hw_scan_req->req.mac_addr, req->mac_addr);
364 ether_addr_copy(local->hw_scan_req->req.mac_addr_mask,
365 req->mac_addr_mask);
Jouni Malinene345f442016-02-26 22:12:48 +0200366 ether_addr_copy(local->hw_scan_req->req.bssid, req->bssid);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100367
368 return true;
369}
370
Eliad Peller8bd2a242013-12-05 11:21:27 +0200371static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
Johannes Berg0a51b272008-09-08 17:44:25 +0200372{
373 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berge9da68d2018-10-18 10:35:47 +0200374 bool hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
Eliad Pellera2b70e82013-12-05 11:21:28 +0200375 bool was_scanning = local->scanning;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100376 struct cfg80211_scan_request *scan_req;
Johannes Berga344d672014-06-12 22:24:31 +0200377 struct ieee80211_sub_if_data *scan_sdata;
Sachin Kulkarni4fa11ec2016-01-12 14:30:19 +0530378 struct ieee80211_sub_if_data *sdata;
Johannes Berg0a51b272008-09-08 17:44:25 +0200379
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200380 lockdep_assert_held(&local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +0200381
Johannes Berg6d3560d2009-10-31 07:44:08 +0100382 /*
383 * It's ok to abort a not-yet-running scan (that
384 * we have one at all will be verified by checking
385 * local->scan_req next), but not to complete it
386 * successfully.
387 */
388 if (WARN_ON(!local->scanning && !aborted))
389 aborted = true;
Johannes Bergde95a542009-04-01 11:58:36 +0200390
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200391 if (WARN_ON(!local->scan_req))
Johannes Bergd07bfd82011-03-07 15:48:41 +0100392 return;
Johannes Bergf3b85252009-04-23 16:01:47 +0200393
David Spinadelc56ef672014-02-05 15:21:13 +0200394 if (hw_scan && !aborted &&
Johannes Berg30686bf2015-06-02 21:39:54 +0200395 !ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS) &&
David Spinadelc56ef672014-02-05 15:21:13 +0200396 ieee80211_prep_hw_scan(local)) {
Johannes Berge2fd5db2012-07-06 21:39:28 +0200397 int rc;
398
399 rc = drv_hw_scan(local,
400 rcu_dereference_protected(local->scan_sdata,
401 lockdep_is_held(&local->mtx)),
402 local->hw_scan_req);
403
Stanislaw Gruszka6eb11a92010-10-06 11:22:11 +0200404 if (rc == 0)
Johannes Bergd07bfd82011-03-07 15:48:41 +0100405 return;
Avraham Stern7947d3e2016-07-05 15:23:12 +0300406
Johannes Berg7d10f6b2016-07-05 15:23:13 +0300407 /* HW scan failed and is going to be reported as aborted,
408 * so clear old scan info.
Avraham Stern7947d3e2016-07-05 15:23:12 +0300409 */
410 memset(&local->scan_info, 0, sizeof(local->scan_info));
Johannes Berg7d10f6b2016-07-05 15:23:13 +0300411 aborted = true;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100412 }
413
414 kfree(local->hw_scan_req);
415 local->hw_scan_req = NULL;
Johannes Bergf3b85252009-04-23 16:01:47 +0200416
Johannes Berg6ea0a692014-11-19 11:55:49 +0100417 scan_req = rcu_dereference_protected(local->scan_req,
418 lockdep_is_held(&local->mtx));
419
Avraham Stern1d762502016-07-05 17:10:13 +0300420 if (scan_req != local->int_scan_req) {
Avraham Stern7947d3e2016-07-05 15:23:12 +0300421 local->scan_info.aborted = aborted;
422 cfg80211_scan_done(scan_req, &local->scan_info);
Avraham Stern1d762502016-07-05 17:10:13 +0300423 }
Johannes Berg6ea0a692014-11-19 11:55:49 +0100424 RCU_INIT_POINTER(local->scan_req, NULL);
Johannes Berga344d672014-06-12 22:24:31 +0200425
426 scan_sdata = rcu_dereference_protected(local->scan_sdata,
427 lockdep_is_held(&local->mtx));
Monam Agarwal0c2bef462014-03-24 00:51:43 +0530428 RCU_INIT_POINTER(local->scan_sdata, NULL);
Johannes Berg2a519312009-02-10 21:25:55 +0100429
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200430 local->scanning = 0;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200431 local->scan_chandef.chan = NULL;
Johannes Bergf3b85252009-04-23 16:01:47 +0200432
Johannes Berg07ef03e2011-11-08 16:21:21 +0100433 /* Set power back to normal operating levels. */
434 ieee80211_hw_config(local, 0);
Ben Greearb23b0252011-02-04 11:54:17 -0800435
Eliad Peller8bd2a242013-12-05 11:21:27 +0200436 if (!hw_scan) {
Ben Greearb23b0252011-02-04 11:54:17 -0800437 ieee80211_configure_filter(local);
Johannes Berga344d672014-06-12 22:24:31 +0200438 drv_sw_scan_complete(local, scan_sdata);
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +0100439 ieee80211_offchannel_return(local);
Ben Greearb23b0252011-02-04 11:54:17 -0800440 }
441
Johannes Berg5cff20e2009-04-29 12:26:17 +0200442 ieee80211_recalc_idle(local);
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200443
Johannes Berg0a51b272008-09-08 17:44:25 +0200444 ieee80211_mlme_notify_scan_completed(local);
Johannes Berg46900292009-02-15 12:44:28 +0100445 ieee80211_ibss_notify_scan_completed(local);
Sachin Kulkarni4fa11ec2016-01-12 14:30:19 +0530446
447 /* Requeue all the work that might have been ignored while
448 * the scan was in progress; if there was none this will
449 * just be a no-op for the particular interface.
450 */
451 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
452 if (ieee80211_sdata_running(sdata))
453 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
454 }
455
Eliad Pellera2b70e82013-12-05 11:21:28 +0200456 if (was_scanning)
457 ieee80211_start_next_roc(local);
Johannes Berg0a51b272008-09-08 17:44:25 +0200458}
Johannes Berg8789d452010-08-26 13:30:26 +0200459
Avraham Stern7947d3e2016-07-05 15:23:12 +0300460void ieee80211_scan_completed(struct ieee80211_hw *hw,
461 struct cfg80211_scan_info *info)
Johannes Berg8789d452010-08-26 13:30:26 +0200462{
463 struct ieee80211_local *local = hw_to_local(hw);
464
Johannes Berg58bd7f12016-09-14 09:37:54 +0200465 trace_api_scan_completed(local, info->aborted);
Johannes Berg8789d452010-08-26 13:30:26 +0200466
467 set_bit(SCAN_COMPLETED, &local->scanning);
Avraham Stern7947d3e2016-07-05 15:23:12 +0300468 if (info->aborted)
Johannes Berg8789d452010-08-26 13:30:26 +0200469 set_bit(SCAN_ABORTED, &local->scanning);
Avraham Stern7947d3e2016-07-05 15:23:12 +0300470
471 memcpy(&local->scan_info, info, sizeof(*info));
472
Johannes Berg8789d452010-08-26 13:30:26 +0200473 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
474}
Johannes Berg0a51b272008-09-08 17:44:25 +0200475EXPORT_SYMBOL(ieee80211_scan_completed);
476
Johannes Berga344d672014-06-12 22:24:31 +0200477static int ieee80211_start_sw_scan(struct ieee80211_local *local,
478 struct ieee80211_sub_if_data *sdata)
Johannes Bergf3b85252009-04-23 16:01:47 +0200479{
Johannes Bergfe57d9f2012-07-26 14:55:08 +0200480 /* Software scan is not supported in multi-channel cases */
481 if (local->use_chanctx)
482 return -EOPNOTSUPP;
483
Johannes Bergf3b85252009-04-23 16:01:47 +0200484 /*
485 * Hardware/driver doesn't support hw_scan, so use software
486 * scanning instead. First send a nullfunc frame with power save
487 * bit on so that AP will buffer the frames for us while we are not
488 * listening, then send probe requests to each channel and wait for
489 * the responses. After all channels are scanned, tune back to the
490 * original channel and send a nullfunc frame with power save bit
491 * off to trigger the AP to send us all the buffered frames.
492 *
493 * Note that while local->sw_scanning is true everything else but
494 * nullfunc frames and probe requests will be dropped in
495 * ieee80211_tx_h_check_assoc().
496 */
Johannes Berga344d672014-06-12 22:24:31 +0200497 drv_sw_scan_start(local, sdata, local->scan_addr);
Johannes Bergf3b85252009-04-23 16:01:47 +0200498
Rajkumar Manoharande312db2012-03-27 11:01:06 +0530499 local->leave_oper_channel_time = jiffies;
Helmut Schaa977923b2009-07-23 12:14:20 +0200500 local->next_scan_state = SCAN_DECISION;
Johannes Bergf3b85252009-04-23 16:01:47 +0200501 local->scan_channel_idx = 0;
502
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +0100503 ieee80211_offchannel_stop_vifs(local);
Johannes Berga80f7c02009-12-23 13:15:32 +0100504
Seth Forshee9c35d7d2013-02-11 11:21:08 -0600505 /* ensure nullfunc is transmitted before leaving operating channel */
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +0200506 ieee80211_flush_queues(local, NULL, false);
Seth Forshee9c35d7d2013-02-11 11:21:08 -0600507
Johannes Berg3ac64be2009-08-17 16:16:53 +0200508 ieee80211_configure_filter(local);
Johannes Bergf3b85252009-04-23 16:01:47 +0200509
Ben Greear59bdf3b2011-02-07 13:44:38 -0800510 /* We need to set power level at maximum rate for scanning. */
511 ieee80211_hw_config(local, 0);
512
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400513 ieee80211_queue_delayed_work(&local->hw,
Johannes Berg07ef03e2011-11-08 16:21:21 +0100514 &local->scan_work, 0);
Johannes Bergf3b85252009-04-23 16:01:47 +0200515
516 return 0;
517}
518
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200519static bool ieee80211_can_scan(struct ieee80211_local *local,
520 struct ieee80211_sub_if_data *sdata)
521{
Eliad Peller5cbc95a2015-01-07 17:50:09 +0200522 if (ieee80211_is_radar_required(local))
Simon Wunderlich164eb022013-02-08 18:16:20 +0100523 return false;
524
Johannes Berg2eb278e2012-06-05 14:28:42 +0200525 if (!list_empty(&local->roc_list))
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200526 return false;
527
528 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +0200529 sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200530 return false;
531
532 return true;
533}
534
535void ieee80211_run_deferred_scan(struct ieee80211_local *local)
536{
537 lockdep_assert_held(&local->mtx);
538
539 if (!local->scan_req || local->scanning)
540 return;
541
Johannes Berge2fd5db2012-07-06 21:39:28 +0200542 if (!ieee80211_can_scan(local,
543 rcu_dereference_protected(
544 local->scan_sdata,
545 lockdep_is_held(&local->mtx))))
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200546 return;
547
548 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
549 round_jiffies_relative(0));
550}
Johannes Bergf3b85252009-04-23 16:01:47 +0200551
Johannes Berg45ad6832018-05-28 15:47:39 +0200552static void ieee80211_send_scan_probe_req(struct ieee80211_sub_if_data *sdata,
553 const u8 *src, const u8 *dst,
554 const u8 *ssid, size_t ssid_len,
555 const u8 *ie, size_t ie_len,
556 u32 ratemask, u32 flags, u32 tx_flags,
557 struct ieee80211_channel *channel)
558{
559 struct sk_buff *skb;
Johannes Bergb9771d42018-05-28 15:47:41 +0200560 u32 txdata_flags = 0;
Johannes Berg45ad6832018-05-28 15:47:39 +0200561
562 skb = ieee80211_build_probe_req(sdata, src, dst, ratemask, channel,
563 ssid, ssid_len,
564 ie, ie_len, flags);
Johannes Bergb9771d42018-05-28 15:47:41 +0200565
Johannes Berg45ad6832018-05-28 15:47:39 +0200566 if (skb) {
Johannes Bergb9771d42018-05-28 15:47:41 +0200567 if (flags & IEEE80211_PROBE_FLAG_RANDOM_SN) {
568 struct ieee80211_hdr *hdr = (void *)skb->data;
569 u16 sn = get_random_u32();
570
571 txdata_flags |= IEEE80211_TX_NO_SEQNO;
572 hdr->seq_ctrl =
573 cpu_to_le16(IEEE80211_SN_TO_SEQ(sn));
574 }
Johannes Berg45ad6832018-05-28 15:47:39 +0200575 IEEE80211_SKB_CB(skb)->flags |= tx_flags;
Johannes Bergb9771d42018-05-28 15:47:41 +0200576 ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band,
577 txdata_flags);
Johannes Berg45ad6832018-05-28 15:47:39 +0200578 }
579}
580
Ben Greear8a690672012-04-17 10:54:16 -0700581static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
582 unsigned long *next_delay)
583{
584 int i;
Johannes Berge2fd5db2012-07-06 21:39:28 +0200585 struct ieee80211_sub_if_data *sdata;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100586 struct cfg80211_scan_request *scan_req;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200587 enum nl80211_band band = local->hw.conf.chandef.chan->band;
Johannes Bergb9771d42018-05-28 15:47:41 +0200588 u32 flags = 0, tx_flags;
Seth Forshee6c17b772013-02-11 11:21:07 -0600589
Johannes Berg6ea0a692014-11-19 11:55:49 +0100590 scan_req = rcu_dereference_protected(local->scan_req,
591 lockdep_is_held(&local->mtx));
592
Seth Forshee6c17b772013-02-11 11:21:07 -0600593 tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100594 if (scan_req->no_cck)
Seth Forshee6c17b772013-02-11 11:21:07 -0600595 tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
Johannes Bergb9771d42018-05-28 15:47:41 +0200596 if (scan_req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
597 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
598 if (scan_req->flags & NL80211_SCAN_FLAG_RANDOM_SN)
599 flags |= IEEE80211_PROBE_FLAG_RANDOM_SN;
Ben Greear8a690672012-04-17 10:54:16 -0700600
Johannes Berge2fd5db2012-07-06 21:39:28 +0200601 sdata = rcu_dereference_protected(local->scan_sdata,
Peter Senna Tschudin316b6b52012-09-06 18:09:16 +0200602 lockdep_is_held(&local->mtx));
Johannes Berge2fd5db2012-07-06 21:39:28 +0200603
Johannes Berg6ea0a692014-11-19 11:55:49 +0100604 for (i = 0; i < scan_req->n_ssids; i++)
Johannes Berg45ad6832018-05-28 15:47:39 +0200605 ieee80211_send_scan_probe_req(
Jouni Malinene345f442016-02-26 22:12:48 +0200606 sdata, local->scan_addr, scan_req->bssid,
Johannes Berg6ea0a692014-11-19 11:55:49 +0100607 scan_req->ssids[i].ssid, scan_req->ssids[i].ssid_len,
608 scan_req->ie, scan_req->ie_len,
Johannes Bergb9771d42018-05-28 15:47:41 +0200609 scan_req->rates[band], flags,
Johannes Berg45ad6832018-05-28 15:47:39 +0200610 tx_flags, local->hw.conf.chandef.chan);
Ben Greear8a690672012-04-17 10:54:16 -0700611
612 /*
613 * After sending probe requests, wait for probe responses
614 * on the channel.
615 */
616 *next_delay = IEEE80211_CHANNEL_TIME;
617 local->next_scan_state = SCAN_DECISION;
618}
619
Johannes Bergf3b85252009-04-23 16:01:47 +0200620static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
621 struct cfg80211_scan_request *req)
622{
623 struct ieee80211_local *local = sdata->local;
Johannes Berge9da68d2018-10-18 10:35:47 +0200624 bool hw_scan = local->ops->hw_scan;
Johannes Bergf3b85252009-04-23 16:01:47 +0200625 int rc;
626
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200627 lockdep_assert_held(&local->mtx);
628
Eliad Peller2726f232015-01-07 17:50:11 +0200629 if (local->scan_req || ieee80211_is_radar_required(local))
Johannes Bergf3b85252009-04-23 16:01:47 +0200630 return -EBUSY;
631
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200632 if (!ieee80211_can_scan(local, sdata)) {
John W. Linville6e7e6212010-02-08 16:38:38 -0500633 /* wait for the work to finish/time out */
Johannes Berg6ea0a692014-11-19 11:55:49 +0100634 rcu_assign_pointer(local->scan_req, req);
Johannes Berge2fd5db2012-07-06 21:39:28 +0200635 rcu_assign_pointer(local->scan_sdata, sdata);
Johannes Bergc0ce77b2010-02-03 10:22:31 +0100636 return 0;
637 }
638
Johannes Berge9da68d2018-10-18 10:35:47 +0200639 again:
640 if (hw_scan) {
Johannes Bergf3b85252009-04-23 16:01:47 +0200641 u8 *ies;
Johannes Bergf3b85252009-04-23 16:01:47 +0200642
David Spinadele4dcbb32014-02-11 13:45:41 +0200643 local->hw_scan_ies_bufsize = local->scan_ies_len + req->ie_len;
David Spinadelc56ef672014-02-05 15:21:13 +0200644
Johannes Berg30686bf2015-06-02 21:39:54 +0200645 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {
David Spinadelc56ef672014-02-05 15:21:13 +0200646 int i, n_bands = 0;
647 u8 bands_counted = 0;
648
649 for (i = 0; i < req->n_channels; i++) {
650 if (bands_counted & BIT(req->channels[i]->band))
651 continue;
652 bands_counted |= BIT(req->channels[i]->band);
653 n_bands++;
654 }
655
656 local->hw_scan_ies_bufsize *= n_bands;
657 }
658
Johannes Berg4d36ec52009-10-27 20:59:55 +0100659 local->hw_scan_req = kmalloc(
660 sizeof(*local->hw_scan_req) +
661 req->n_channels * sizeof(req->channels[0]) +
Johannes Bergc604b9f2012-11-29 12:45:18 +0100662 local->hw_scan_ies_bufsize, GFP_KERNEL);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100663 if (!local->hw_scan_req)
Johannes Bergf3b85252009-04-23 16:01:47 +0200664 return -ENOMEM;
665
David Spinadelc56ef672014-02-05 15:21:13 +0200666 local->hw_scan_req->req.ssids = req->ssids;
667 local->hw_scan_req->req.n_ssids = req->n_ssids;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100668 ies = (u8 *)local->hw_scan_req +
669 sizeof(*local->hw_scan_req) +
670 req->n_channels * sizeof(req->channels[0]);
David Spinadelc56ef672014-02-05 15:21:13 +0200671 local->hw_scan_req->req.ie = ies;
672 local->hw_scan_req->req.flags = req->flags;
Jouni Malinene345f442016-02-26 22:12:48 +0200673 eth_broadcast_addr(local->hw_scan_req->req.bssid);
Avraham Stern7947d3e2016-07-05 15:23:12 +0300674 local->hw_scan_req->req.duration = req->duration;
675 local->hw_scan_req->req.duration_mandatory =
676 req->duration_mandatory;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100677
678 local->hw_scan_band = 0;
John W. Linville6e7e6212010-02-08 16:38:38 -0500679
680 /*
681 * After allocating local->hw_scan_req, we must
682 * go through until ieee80211_prep_hw_scan(), so
683 * anything that might be changed here and leave
684 * this function early must not go after this
685 * allocation.
686 */
Johannes Bergf3b85252009-04-23 16:01:47 +0200687 }
688
Johannes Berg6ea0a692014-11-19 11:55:49 +0100689 rcu_assign_pointer(local->scan_req, req);
Johannes Berge2fd5db2012-07-06 21:39:28 +0200690 rcu_assign_pointer(local->scan_sdata, sdata);
Johannes Bergf3b85252009-04-23 16:01:47 +0200691
Johannes Berga344d672014-06-12 22:24:31 +0200692 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
693 get_random_mask_addr(local->scan_addr,
694 req->mac_addr,
695 req->mac_addr_mask);
696 else
697 memcpy(local->scan_addr, sdata->vif.addr, ETH_ALEN);
698
Johannes Berge9da68d2018-10-18 10:35:47 +0200699 if (hw_scan) {
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200700 __set_bit(SCAN_HW_SCANNING, &local->scanning);
Ben Greear8a690672012-04-17 10:54:16 -0700701 } else if ((req->n_channels == 1) &&
Karl Beldan675a0b02013-03-25 16:26:57 +0100702 (req->channels[0] == local->_oper_chandef.chan)) {
Johannes Berg9b864872012-07-26 14:38:32 +0200703 /*
704 * If we are scanning only on the operating channel
705 * then we do not need to stop normal activities
Ben Greear8a690672012-04-17 10:54:16 -0700706 */
707 unsigned long next_delay;
708
709 __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
710
711 ieee80211_recalc_idle(local);
712
713 /* Notify driver scan is starting, keep order of operations
714 * same as normal software scan, in case that matters. */
Johannes Berga344d672014-06-12 22:24:31 +0200715 drv_sw_scan_start(local, sdata, local->scan_addr);
Ben Greear8a690672012-04-17 10:54:16 -0700716
717 ieee80211_configure_filter(local); /* accept probe-responses */
718
719 /* We need to ensure power level is at max for scanning. */
720 ieee80211_hw_config(local, 0);
721
Antonio Quartulli4e39cca2015-11-21 18:13:40 +0800722 if ((req->channels[0]->flags & (IEEE80211_CHAN_NO_IR |
723 IEEE80211_CHAN_RADAR)) ||
Johannes Berg6ea0a692014-11-19 11:55:49 +0100724 !req->n_ssids) {
Ben Greear8a690672012-04-17 10:54:16 -0700725 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
726 } else {
727 ieee80211_scan_state_send_probe(local, &next_delay);
728 next_delay = IEEE80211_CHANNEL_TIME;
729 }
730
731 /* Now, just wait a bit and we are all done! */
732 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
733 next_delay);
734 return 0;
735 } else {
736 /* Do normal software scan */
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200737 __set_bit(SCAN_SW_SCANNING, &local->scanning);
Ben Greear8a690672012-04-17 10:54:16 -0700738 }
John W. Linville6e7e6212010-02-08 16:38:38 -0500739
Johannes Berg5cff20e2009-04-29 12:26:17 +0200740 ieee80211_recalc_idle(local);
Johannes Bergf3b85252009-04-23 16:01:47 +0200741
Johannes Berge9da68d2018-10-18 10:35:47 +0200742 if (hw_scan) {
Johannes Berg4d36ec52009-10-27 20:59:55 +0100743 WARN_ON(!ieee80211_prep_hw_scan(local));
Johannes Berga060bbf2010-04-27 11:59:34 +0200744 rc = drv_hw_scan(local, sdata, local->hw_scan_req);
Johannes Berga344d672014-06-12 22:24:31 +0200745 } else {
746 rc = ieee80211_start_sw_scan(local, sdata);
747 }
Johannes Bergf3b85252009-04-23 16:01:47 +0200748
Johannes Bergf3b85252009-04-23 16:01:47 +0200749 if (rc) {
Johannes Berg4d36ec52009-10-27 20:59:55 +0100750 kfree(local->hw_scan_req);
751 local->hw_scan_req = NULL;
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200752 local->scanning = 0;
Johannes Bergf3b85252009-04-23 16:01:47 +0200753
Johannes Berg5cff20e2009-04-29 12:26:17 +0200754 ieee80211_recalc_idle(local);
755
Johannes Bergf3b85252009-04-23 16:01:47 +0200756 local->scan_req = NULL;
Monam Agarwal0c2bef462014-03-24 00:51:43 +0530757 RCU_INIT_POINTER(local->scan_sdata, NULL);
Johannes Bergf3b85252009-04-23 16:01:47 +0200758 }
759
Johannes Berge9da68d2018-10-18 10:35:47 +0200760 if (hw_scan && rc == 1) {
761 /*
762 * we can't fall back to software for P2P-GO
763 * as it must update NoA etc.
764 */
765 if (ieee80211_vif_type_p2p(&sdata->vif) ==
766 NL80211_IFTYPE_P2P_GO)
767 return -EOPNOTSUPP;
768 hw_scan = false;
769 goto again;
770 }
771
Johannes Bergf3b85252009-04-23 16:01:47 +0200772 return rc;
773}
774
Helmut Schaadf13cce2010-02-24 14:19:21 +0100775static unsigned long
776ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
777{
778 /*
779 * TODO: channel switching also consumes quite some time,
780 * add that delay as well to get a better estimation
781 */
Antonio Quartulli4e39cca2015-11-21 18:13:40 +0800782 if (chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR))
Helmut Schaadf13cce2010-02-24 14:19:21 +0100783 return IEEE80211_PASSIVE_CHANNEL_TIME;
784 return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
785}
786
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200787static void ieee80211_scan_state_decision(struct ieee80211_local *local,
788 unsigned long *next_delay)
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200789{
Helmut Schaa142b9f52009-07-23 13:18:01 +0200790 bool associated = false;
Helmut Schaadf13cce2010-02-24 14:19:21 +0100791 bool tx_empty = true;
792 bool bad_latency;
Helmut Schaa142b9f52009-07-23 13:18:01 +0200793 struct ieee80211_sub_if_data *sdata;
Helmut Schaadf13cce2010-02-24 14:19:21 +0100794 struct ieee80211_channel *next_chan;
Sam Lefflercd2bb512012-10-11 21:03:35 -0700795 enum mac80211_scan_state next_scan_state;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100796 struct cfg80211_scan_request *scan_req;
Helmut Schaa142b9f52009-07-23 13:18:01 +0200797
Helmut Schaadf13cce2010-02-24 14:19:21 +0100798 /*
799 * check if at least one STA interface is associated,
800 * check if at least one STA interface has pending tx frames
801 * and grab the lowest used beacon interval
802 */
Helmut Schaa142b9f52009-07-23 13:18:01 +0200803 mutex_lock(&local->iflist_mtx);
804 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +0100805 if (!ieee80211_sdata_running(sdata))
Helmut Schaa142b9f52009-07-23 13:18:01 +0200806 continue;
807
808 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
809 if (sdata->u.mgd.associated) {
810 associated = true;
Helmut Schaadf13cce2010-02-24 14:19:21 +0100811
Helmut Schaadf13cce2010-02-24 14:19:21 +0100812 if (!qdisc_all_tx_empty(sdata->dev)) {
813 tx_empty = false;
814 break;
815 }
Helmut Schaa142b9f52009-07-23 13:18:01 +0200816 }
817 }
818 }
819 mutex_unlock(&local->iflist_mtx);
820
Johannes Berg6ea0a692014-11-19 11:55:49 +0100821 scan_req = rcu_dereference_protected(local->scan_req,
822 lockdep_is_held(&local->mtx));
823
824 next_chan = scan_req->channels[local->scan_channel_idx];
Ben Greearb23b0252011-02-04 11:54:17 -0800825
Johannes Berg07ef03e2011-11-08 16:21:21 +0100826 /*
827 * we're currently scanning a different channel, let's
828 * see if we can scan another channel without interfering
829 * with the current traffic situation.
830 *
Stanislaw Gruszka3f892b62013-01-23 12:32:45 +0100831 * Keep good latency, do not stay off-channel more than 125 ms.
Johannes Berg07ef03e2011-11-08 16:21:21 +0100832 */
Helmut Schaadf13cce2010-02-24 14:19:21 +0100833
Johannes Berg07ef03e2011-11-08 16:21:21 +0100834 bad_latency = time_after(jiffies +
Stanislaw Gruszka3f892b62013-01-23 12:32:45 +0100835 ieee80211_scan_get_channel_time(next_chan),
836 local->leave_oper_channel_time + HZ / 8);
Helmut Schaadf13cce2010-02-24 14:19:21 +0100837
Sam Lefflercd2bb512012-10-11 21:03:35 -0700838 if (associated && !tx_empty) {
Johannes Berg6ea0a692014-11-19 11:55:49 +0100839 if (scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
Sam Lefflercd2bb512012-10-11 21:03:35 -0700840 next_scan_state = SCAN_ABORT;
841 else
842 next_scan_state = SCAN_SUSPEND;
Stanislaw Gruszka3f892b62013-01-23 12:32:45 +0100843 } else if (associated && bad_latency) {
Sam Lefflercd2bb512012-10-11 21:03:35 -0700844 next_scan_state = SCAN_SUSPEND;
845 } else {
846 next_scan_state = SCAN_SET_CHANNEL;
847 }
848
849 local->next_scan_state = next_scan_state;
Helmut Schaa142b9f52009-07-23 13:18:01 +0200850
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200851 *next_delay = 0;
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200852}
853
854static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
855 unsigned long *next_delay)
856{
857 int skip;
858 struct ieee80211_channel *chan;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200859 enum nl80211_bss_scan_width oper_scan_width;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100860 struct cfg80211_scan_request *scan_req;
861
862 scan_req = rcu_dereference_protected(local->scan_req,
863 lockdep_is_held(&local->mtx));
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200864
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200865 skip = 0;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100866 chan = scan_req->channels[local->scan_channel_idx];
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200867
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200868 local->scan_chandef.chan = chan;
869 local->scan_chandef.center_freq1 = chan->center_freq;
870 local->scan_chandef.center_freq2 = 0;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100871 switch (scan_req->scan_width) {
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200872 case NL80211_BSS_CHAN_WIDTH_5:
873 local->scan_chandef.width = NL80211_CHAN_WIDTH_5;
874 break;
875 case NL80211_BSS_CHAN_WIDTH_10:
876 local->scan_chandef.width = NL80211_CHAN_WIDTH_10;
877 break;
878 case NL80211_BSS_CHAN_WIDTH_20:
879 /* If scanning on oper channel, use whatever channel-type
880 * is currently in use.
881 */
882 oper_scan_width = cfg80211_chandef_to_scan_width(
883 &local->_oper_chandef);
884 if (chan == local->_oper_chandef.chan &&
Johannes Berg6ea0a692014-11-19 11:55:49 +0100885 oper_scan_width == scan_req->scan_width)
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200886 local->scan_chandef = local->_oper_chandef;
887 else
888 local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
889 break;
890 }
Ben Greearb23b0252011-02-04 11:54:17 -0800891
Johannes Berg07ef03e2011-11-08 16:21:21 +0100892 if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
893 skip = 1;
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200894
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200895 /* advance state machine to next channel/band */
896 local->scan_channel_idx++;
897
Helmut Schaa0ee9c132009-07-25 17:25:51 +0200898 if (skip) {
899 /* if we skip this channel return to the decision state */
900 local->next_scan_state = SCAN_DECISION;
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200901 return;
Helmut Schaa0ee9c132009-07-25 17:25:51 +0200902 }
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200903
904 /*
905 * Probe delay is used to update the NAV, cf. 11.1.3.2.2
906 * (which unfortunately doesn't say _why_ step a) is done,
907 * but it waits for the probe delay or until a frame is
908 * received - and the received frame would update the NAV).
909 * For now, we do not support waiting until a frame is
910 * received.
911 *
912 * In any case, it is not necessary for a passive scan.
913 */
Antonio Quartulli4e39cca2015-11-21 18:13:40 +0800914 if ((chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) ||
915 !scan_req->n_ssids) {
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200916 *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Helmut Schaa977923b2009-07-23 12:14:20 +0200917 local->next_scan_state = SCAN_DECISION;
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200918 return;
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200919 }
920
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200921 /* active scan, send probes */
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200922 *next_delay = IEEE80211_PROBE_DELAY;
Helmut Schaa977923b2009-07-23 12:14:20 +0200923 local->next_scan_state = SCAN_SEND_PROBE;
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200924}
925
Johannes Berg07ef03e2011-11-08 16:21:21 +0100926static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
927 unsigned long *next_delay)
928{
929 /* switch back to the operating channel */
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200930 local->scan_chandef.chan = NULL;
Johannes Berg07ef03e2011-11-08 16:21:21 +0100931 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
932
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +0100933 /* disable PS */
934 ieee80211_offchannel_return(local);
Johannes Berg07ef03e2011-11-08 16:21:21 +0100935
936 *next_delay = HZ / 5;
937 /* afterwards, resume scan & go to next channel */
938 local->next_scan_state = SCAN_RESUME;
939}
940
941static void ieee80211_scan_state_resume(struct ieee80211_local *local,
942 unsigned long *next_delay)
943{
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +0100944 ieee80211_offchannel_stop_vifs(local);
Johannes Berg07ef03e2011-11-08 16:21:21 +0100945
946 if (local->ops->flush) {
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +0200947 ieee80211_flush_queues(local, NULL, false);
Johannes Berg07ef03e2011-11-08 16:21:21 +0100948 *next_delay = 0;
949 } else
950 *next_delay = HZ / 10;
951
952 /* remember when we left the operating channel */
953 local->leave_oper_channel_time = jiffies;
954
955 /* advance to the next channel to be scanned */
Mohammed Shafi Shajakhande2ee842011-12-24 18:43:28 +0530956 local->next_scan_state = SCAN_SET_CHANNEL;
Johannes Berg07ef03e2011-11-08 16:21:21 +0100957}
958
Johannes Bergc2b13452008-09-11 00:01:55 +0200959void ieee80211_scan_work(struct work_struct *work)
Johannes Berg0a51b272008-09-08 17:44:25 +0200960{
961 struct ieee80211_local *local =
962 container_of(work, struct ieee80211_local, scan_work.work);
Johannes Bergd07bfd82011-03-07 15:48:41 +0100963 struct ieee80211_sub_if_data *sdata;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100964 struct cfg80211_scan_request *scan_req;
Johannes Berg0a51b272008-09-08 17:44:25 +0200965 unsigned long next_delay = 0;
Eliad Peller8bd2a242013-12-05 11:21:27 +0200966 bool aborted;
Johannes Berg8789d452010-08-26 13:30:26 +0200967
Johannes Berga1699b72010-07-30 16:46:07 +0200968 mutex_lock(&local->mtx);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +0200969
Luciano Coelho332ff7f2015-01-22 23:34:10 +0200970 if (!ieee80211_can_run_worker(local)) {
971 aborted = true;
972 goto out_complete;
973 }
974
Johannes Berge2fd5db2012-07-06 21:39:28 +0200975 sdata = rcu_dereference_protected(local->scan_sdata,
976 lockdep_is_held(&local->mtx));
Johannes Berg6ea0a692014-11-19 11:55:49 +0100977 scan_req = rcu_dereference_protected(local->scan_req,
978 lockdep_is_held(&local->mtx));
Johannes Bergd07bfd82011-03-07 15:48:41 +0100979
Ben Greear8a690672012-04-17 10:54:16 -0700980 /* When scanning on-channel, the first-callback means completed. */
981 if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) {
982 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
983 goto out_complete;
984 }
985
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +0200986 if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
987 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
988 goto out_complete;
Johannes Bergf3b85252009-04-23 16:01:47 +0200989 }
990
Johannes Berg6ea0a692014-11-19 11:55:49 +0100991 if (!sdata || !scan_req)
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +0200992 goto out;
993
Eliad Pellerff5db432014-11-12 10:08:29 +0200994 if (!local->scanning) {
Johannes Bergf3b85252009-04-23 16:01:47 +0200995 int rc;
996
Johannes Berg6ea0a692014-11-19 11:55:49 +0100997 RCU_INIT_POINTER(local->scan_req, NULL);
Monam Agarwal0c2bef462014-03-24 00:51:43 +0530998 RCU_INIT_POINTER(local->scan_sdata, NULL);
Johannes Bergf3b85252009-04-23 16:01:47 +0200999
Johannes Berg6ea0a692014-11-19 11:55:49 +01001000 rc = __ieee80211_start_scan(sdata, scan_req);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001001 if (rc) {
Stanislaw Gruszka3aed49e2010-10-06 11:22:12 +02001002 /* need to complete scan in cfg80211 */
Johannes Berg6ea0a692014-11-19 11:55:49 +01001003 rcu_assign_pointer(local->scan_req, scan_req);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001004 aborted = true;
1005 goto out_complete;
1006 } else
1007 goto out;
Johannes Bergf3b85252009-04-23 16:01:47 +02001008 }
1009
Johannes Berg5bc75722008-09-11 00:01:51 +02001010 /*
Helmut Schaaf502d092009-07-23 12:13:48 +02001011 * as long as no delay is required advance immediately
1012 * without scheduling a new work
1013 */
1014 do {
Rajkumar Manoharanc29acf22011-05-14 09:43:28 +05301015 if (!ieee80211_sdata_running(sdata)) {
1016 aborted = true;
1017 goto out_complete;
1018 }
1019
Helmut Schaa977923b2009-07-23 12:14:20 +02001020 switch (local->next_scan_state) {
Helmut Schaa2fb3f022009-07-23 12:13:56 +02001021 case SCAN_DECISION:
Stanislaw Gruszkae229f842010-10-06 11:22:09 +02001022 /* if no more bands/channels left, complete scan */
Johannes Berg6ea0a692014-11-19 11:55:49 +01001023 if (local->scan_channel_idx >= scan_req->n_channels) {
Stanislaw Gruszkae229f842010-10-06 11:22:09 +02001024 aborted = false;
1025 goto out_complete;
1026 }
1027 ieee80211_scan_state_decision(local, &next_delay);
Helmut Schaaf502d092009-07-23 12:13:48 +02001028 break;
Helmut Schaa2fb3f022009-07-23 12:13:56 +02001029 case SCAN_SET_CHANNEL:
1030 ieee80211_scan_state_set_channel(local, &next_delay);
1031 break;
Helmut Schaaf502d092009-07-23 12:13:48 +02001032 case SCAN_SEND_PROBE:
1033 ieee80211_scan_state_send_probe(local, &next_delay);
1034 break;
Johannes Berg07ef03e2011-11-08 16:21:21 +01001035 case SCAN_SUSPEND:
1036 ieee80211_scan_state_suspend(local, &next_delay);
Helmut Schaa142b9f52009-07-23 13:18:01 +02001037 break;
Johannes Berg07ef03e2011-11-08 16:21:21 +01001038 case SCAN_RESUME:
1039 ieee80211_scan_state_resume(local, &next_delay);
Helmut Schaa142b9f52009-07-23 13:18:01 +02001040 break;
Sam Lefflercd2bb512012-10-11 21:03:35 -07001041 case SCAN_ABORT:
1042 aborted = true;
1043 goto out_complete;
Helmut Schaaf502d092009-07-23 12:13:48 +02001044 }
1045 } while (next_delay == 0);
Johannes Berg0a51b272008-09-08 17:44:25 +02001046
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001047 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
Johannes Bergd07bfd82011-03-07 15:48:41 +01001048 goto out;
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001049
1050out_complete:
Eliad Peller8bd2a242013-12-05 11:21:27 +02001051 __ieee80211_scan_completed(&local->hw, aborted);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001052out:
1053 mutex_unlock(&local->mtx);
Johannes Berg0a51b272008-09-08 17:44:25 +02001054}
1055
Johannes Bergc2b13452008-09-11 00:01:55 +02001056int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
Johannes Berg2a519312009-02-10 21:25:55 +01001057 struct cfg80211_scan_request *req)
Johannes Berg0a51b272008-09-08 17:44:25 +02001058{
Johannes Bergf3b85252009-04-23 16:01:47 +02001059 int res;
1060
Johannes Berga1699b72010-07-30 16:46:07 +02001061 mutex_lock(&sdata->local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +02001062 res = __ieee80211_start_scan(sdata, req);
Johannes Berga1699b72010-07-30 16:46:07 +02001063 mutex_unlock(&sdata->local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +02001064
1065 return res;
1066}
1067
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001068int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
1069 const u8 *ssid, u8 ssid_len,
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001070 struct ieee80211_channel **channels,
1071 unsigned int n_channels,
Simon Wunderlich7ca15a02013-07-08 16:55:56 +02001072 enum nl80211_bss_scan_width scan_width)
Johannes Bergf3b85252009-04-23 16:01:47 +02001073{
Johannes Berg0a51b272008-09-08 17:44:25 +02001074 struct ieee80211_local *local = sdata->local;
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001075 int ret = -EBUSY, i, n_ch = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001076 enum nl80211_band band;
Johannes Berg0a51b272008-09-08 17:44:25 +02001077
Johannes Berga1699b72010-07-30 16:46:07 +02001078 mutex_lock(&local->mtx);
Johannes Berg2a519312009-02-10 21:25:55 +01001079
Johannes Bergf3b85252009-04-23 16:01:47 +02001080 /* busy scanning */
1081 if (local->scan_req)
1082 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01001083
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001084 /* fill internal scan request */
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001085 if (!channels) {
1086 int max_n;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001087
Johannes Berg57fbcce2016-04-12 15:56:15 +02001088 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001089 if (!local->hw.wiphy->bands[band])
1090 continue;
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001091
1092 max_n = local->hw.wiphy->bands[band]->n_channels;
1093 for (i = 0; i < max_n; i++) {
1094 struct ieee80211_channel *tmp_ch =
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001095 &local->hw.wiphy->bands[band]->channels[i];
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001096
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001097 if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR |
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001098 IEEE80211_CHAN_DISABLED))
1099 continue;
1100
1101 local->int_scan_req->channels[n_ch] = tmp_ch;
1102 n_ch++;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001103 }
1104 }
1105
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001106 if (WARN_ON_ONCE(n_ch == 0))
1107 goto unlock;
1108
1109 local->int_scan_req->n_channels = n_ch;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001110 } else {
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001111 for (i = 0; i < n_channels; i++) {
1112 if (channels[i]->flags & (IEEE80211_CHAN_NO_IR |
1113 IEEE80211_CHAN_DISABLED))
1114 continue;
1115
1116 local->int_scan_req->channels[n_ch] = channels[i];
1117 n_ch++;
1118 }
1119
1120 if (WARN_ON_ONCE(n_ch == 0))
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001121 goto unlock;
1122
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001123 local->int_scan_req->n_channels = n_ch;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001124 }
1125
1126 local->int_scan_req->ssids = &local->scan_ssid;
1127 local->int_scan_req->n_ssids = 1;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +02001128 local->int_scan_req->scan_width = scan_width;
Johannes Berg5ba63532009-08-07 17:54:07 +02001129 memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
1130 local->int_scan_req->ssids[0].ssid_len = ssid_len;
Johannes Berg2a519312009-02-10 21:25:55 +01001131
Johannes Berg5ba63532009-08-07 17:54:07 +02001132 ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
Johannes Bergf3b85252009-04-23 16:01:47 +02001133 unlock:
Johannes Berga1699b72010-07-30 16:46:07 +02001134 mutex_unlock(&local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +02001135 return ret;
Johannes Berg0a51b272008-09-08 17:44:25 +02001136}
Johannes Berg5bb644a2009-05-17 11:40:42 +02001137
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001138/*
1139 * Only call this function when a scan can't be queued -- under RTNL.
1140 */
Johannes Berg5bb644a2009-05-17 11:40:42 +02001141void ieee80211_scan_cancel(struct ieee80211_local *local)
1142{
Johannes Berg5bb644a2009-05-17 11:40:42 +02001143 /*
Eliad Pellerb8564392011-06-13 12:47:30 +03001144 * We are canceling software scan, or deferred scan that was not
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001145 * yet really started (see __ieee80211_start_scan ).
1146 *
1147 * Regarding hardware scan:
1148 * - we can not call __ieee80211_scan_completed() as when
1149 * SCAN_HW_SCANNING bit is set this function change
1150 * local->hw_scan_req to operate on 5G band, what race with
1151 * driver which can use local->hw_scan_req
1152 *
1153 * - we can not cancel scan_work since driver can schedule it
1154 * by ieee80211_scan_completed(..., true) to finish scan
1155 *
Eliad Pellerb8564392011-06-13 12:47:30 +03001156 * Hence we only call the cancel_hw_scan() callback, but the low-level
1157 * driver is still responsible for calling ieee80211_scan_completed()
1158 * after the scan was completed/aborted.
Johannes Berg5bb644a2009-05-17 11:40:42 +02001159 */
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001160
Johannes Berga1699b72010-07-30 16:46:07 +02001161 mutex_lock(&local->mtx);
Eliad Pellerb8564392011-06-13 12:47:30 +03001162 if (!local->scan_req)
1163 goto out;
1164
Emmanuel Grumbacha7540552013-09-16 11:12:07 +03001165 /*
1166 * We have a scan running and the driver already reported completion,
1167 * but the worker hasn't run yet or is stuck on the mutex - mark it as
1168 * cancelled.
1169 */
1170 if (test_bit(SCAN_HW_SCANNING, &local->scanning) &&
1171 test_bit(SCAN_COMPLETED, &local->scanning)) {
1172 set_bit(SCAN_HW_CANCELLED, &local->scanning);
1173 goto out;
1174 }
1175
Eliad Pellerb8564392011-06-13 12:47:30 +03001176 if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
Emmanuel Grumbacha7540552013-09-16 11:12:07 +03001177 /*
1178 * Make sure that __ieee80211_scan_completed doesn't trigger a
1179 * scan on another band.
1180 */
1181 set_bit(SCAN_HW_CANCELLED, &local->scanning);
Eliad Pellerb8564392011-06-13 12:47:30 +03001182 if (local->ops->cancel_hw_scan)
Johannes Berge2fd5db2012-07-06 21:39:28 +02001183 drv_cancel_hw_scan(local,
1184 rcu_dereference_protected(local->scan_sdata,
1185 lockdep_is_held(&local->mtx)));
Eliad Pellerb8564392011-06-13 12:47:30 +03001186 goto out;
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001187 }
Eliad Pellerb8564392011-06-13 12:47:30 +03001188
1189 /*
1190 * If the work is currently running, it must be blocked on
1191 * the mutex, but we'll set scan_sdata = NULL and it'll
1192 * simply exit once it acquires the mutex.
1193 */
1194 cancel_delayed_work(&local->scan_work);
1195 /* and clean up */
Avraham Stern7947d3e2016-07-05 15:23:12 +03001196 memset(&local->scan_info, 0, sizeof(local->scan_info));
Eliad Peller8bd2a242013-12-05 11:21:27 +02001197 __ieee80211_scan_completed(&local->hw, true);
Eliad Pellerb8564392011-06-13 12:47:30 +03001198out:
Johannes Bergd07bfd82011-03-07 15:48:41 +01001199 mutex_unlock(&local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02001200}
Luciano Coelho79f460c2011-05-11 17:09:36 +03001201
David Spinadeld43c6b62013-12-08 21:48:57 +02001202int __ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
1203 struct cfg80211_sched_scan_request *req)
Luciano Coelho79f460c2011-05-11 17:09:36 +03001204{
1205 struct ieee80211_local *local = sdata->local;
David Spinadel633e2712014-02-06 16:15:23 +02001206 struct ieee80211_scan_ies sched_scan_ies = {};
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001207 struct cfg80211_chan_def chandef;
David Spinadel633e2712014-02-06 16:15:23 +02001208 int ret, i, iebufsz, num_bands = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001209 u32 rate_masks[NUM_NL80211_BANDS] = {};
David Spinadel633e2712014-02-06 16:15:23 +02001210 u8 bands_used = 0;
1211 u8 *ie;
Johannes Bergb9771d42018-05-28 15:47:41 +02001212 u32 flags = 0;
Johannes Bergc604b9f2012-11-29 12:45:18 +01001213
David Spinadele4dcbb32014-02-11 13:45:41 +02001214 iebufsz = local->scan_ies_len + req->ie_len;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001215
David Spinadeld43c6b62013-12-08 21:48:57 +02001216 lockdep_assert_held(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001217
David Spinadeld43c6b62013-12-08 21:48:57 +02001218 if (!local->ops->sched_scan_start)
1219 return -ENOTSUPP;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001220
Johannes Berg57fbcce2016-04-12 15:56:15 +02001221 for (i = 0; i < NUM_NL80211_BANDS; i++) {
David Spinadel633e2712014-02-06 16:15:23 +02001222 if (local->hw.wiphy->bands[i]) {
1223 bands_used |= BIT(i);
1224 rate_masks[i] = (u32) -1;
1225 num_bands++;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001226 }
Luciano Coelho79f460c2011-05-11 17:09:36 +03001227 }
1228
Johannes Bergb9771d42018-05-28 15:47:41 +02001229 if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
1230 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
1231
Kees Cook6396bb22018-06-12 14:03:40 -07001232 ie = kcalloc(iebufsz, num_bands, GFP_KERNEL);
David Spinadel633e2712014-02-06 16:15:23 +02001233 if (!ie) {
1234 ret = -ENOMEM;
1235 goto out;
1236 }
1237
1238 ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
1239
Kirtika Ruchandanicd5861b2016-11-23 20:45:49 -08001240 ieee80211_build_preq_ies(local, ie, num_bands * iebufsz,
1241 &sched_scan_ies, req->ie,
Johannes Berg00387f32018-05-28 15:47:38 +02001242 req->ie_len, bands_used, rate_masks, &chandef,
Johannes Bergb9771d42018-05-28 15:47:41 +02001243 flags);
David Spinadel633e2712014-02-06 16:15:23 +02001244
Johannes Berg30dd3ed2012-09-04 19:15:01 +02001245 ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies);
David Spinadeld43c6b62013-12-08 21:48:57 +02001246 if (ret == 0) {
Johannes Berg5260a5b2012-07-06 21:55:11 +02001247 rcu_assign_pointer(local->sched_scan_sdata, sdata);
Johannes Berg6ea0a692014-11-19 11:55:49 +01001248 rcu_assign_pointer(local->sched_scan_req, req);
David Spinadeld43c6b62013-12-08 21:48:57 +02001249 }
Luciano Coelho79f460c2011-05-11 17:09:36 +03001250
David Spinadel633e2712014-02-06 16:15:23 +02001251 kfree(ie);
David Spinadeld43c6b62013-12-08 21:48:57 +02001252
David Spinadel633e2712014-02-06 16:15:23 +02001253out:
David Spinadeld43c6b62013-12-08 21:48:57 +02001254 if (ret) {
1255 /* Clean in case of failure after HW restart or upon resume. */
Monam Agarwal0c2bef462014-03-24 00:51:43 +05301256 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
Johannes Berg6ea0a692014-11-19 11:55:49 +01001257 RCU_INIT_POINTER(local->sched_scan_req, NULL);
David Spinadeld43c6b62013-12-08 21:48:57 +02001258 }
1259
1260 return ret;
1261}
1262
1263int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
1264 struct cfg80211_sched_scan_request *req)
1265{
1266 struct ieee80211_local *local = sdata->local;
1267 int ret;
1268
1269 mutex_lock(&local->mtx);
1270
1271 if (rcu_access_pointer(local->sched_scan_sdata)) {
1272 mutex_unlock(&local->mtx);
1273 return -EBUSY;
1274 }
1275
1276 ret = __ieee80211_request_sched_scan_start(sdata, req);
1277
Johannes Berg5260a5b2012-07-06 21:55:11 +02001278 mutex_unlock(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001279 return ret;
1280}
1281
Eliad Peller0d440ea2015-10-25 10:59:33 +02001282int ieee80211_request_sched_scan_stop(struct ieee80211_local *local)
Luciano Coelho79f460c2011-05-11 17:09:36 +03001283{
Eliad Peller0d440ea2015-10-25 10:59:33 +02001284 struct ieee80211_sub_if_data *sched_scan_sdata;
1285 int ret = -ENOENT;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001286
Johannes Berg5260a5b2012-07-06 21:55:11 +02001287 mutex_lock(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001288
1289 if (!local->ops->sched_scan_stop) {
1290 ret = -ENOTSUPP;
1291 goto out;
1292 }
1293
David Spinadeld43c6b62013-12-08 21:48:57 +02001294 /* We don't want to restart sched scan anymore. */
Johannes Berg6ea0a692014-11-19 11:55:49 +01001295 RCU_INIT_POINTER(local->sched_scan_req, NULL);
David Spinadeld43c6b62013-12-08 21:48:57 +02001296
Eliad Peller0d440ea2015-10-25 10:59:33 +02001297 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
1298 lockdep_is_held(&local->mtx));
1299 if (sched_scan_sdata) {
1300 ret = drv_sched_scan_stop(local, sched_scan_sdata);
Alexander Bondar71228a12014-03-16 10:49:54 +02001301 if (!ret)
Andreea-Cristina Bernatad053a92014-08-22 16:14:49 +03001302 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
Alexander Bondar71228a12014-03-16 10:49:54 +02001303 }
Luciano Coelho79f460c2011-05-11 17:09:36 +03001304out:
Johannes Berg5260a5b2012-07-06 21:55:11 +02001305 mutex_unlock(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001306
1307 return ret;
1308}
1309
1310void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
1311{
1312 struct ieee80211_local *local = hw_to_local(hw);
1313
1314 trace_api_sched_scan_results(local);
1315
Arend Van Sprielb34939b2017-04-28 13:40:28 +01001316 cfg80211_sched_scan_results(hw->wiphy, 0);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001317}
1318EXPORT_SYMBOL(ieee80211_sched_scan_results);
1319
Johannes Bergf6837ba82014-04-30 14:19:04 +02001320void ieee80211_sched_scan_end(struct ieee80211_local *local)
Luciano Coelho85a99942011-05-12 16:28:29 +03001321{
Luciano Coelho85a99942011-05-12 16:28:29 +03001322 mutex_lock(&local->mtx);
1323
Johannes Berg5260a5b2012-07-06 21:55:11 +02001324 if (!rcu_access_pointer(local->sched_scan_sdata)) {
Luciano Coelho85a99942011-05-12 16:28:29 +03001325 mutex_unlock(&local->mtx);
1326 return;
1327 }
1328
Monam Agarwal0c2bef462014-03-24 00:51:43 +05301329 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
Luciano Coelho85a99942011-05-12 16:28:29 +03001330
David Spinadeld43c6b62013-12-08 21:48:57 +02001331 /* If sched scan was aborted by the driver. */
Johannes Berg6ea0a692014-11-19 11:55:49 +01001332 RCU_INIT_POINTER(local->sched_scan_req, NULL);
David Spinadeld43c6b62013-12-08 21:48:57 +02001333
Luciano Coelho85a99942011-05-12 16:28:29 +03001334 mutex_unlock(&local->mtx);
1335
Arend Van Sprielb34939b2017-04-28 13:40:28 +01001336 cfg80211_sched_scan_stopped(local->hw.wiphy, 0);
Luciano Coelho85a99942011-05-12 16:28:29 +03001337}
1338
Johannes Bergf6837ba82014-04-30 14:19:04 +02001339void ieee80211_sched_scan_stopped_work(struct work_struct *work)
1340{
1341 struct ieee80211_local *local =
1342 container_of(work, struct ieee80211_local,
1343 sched_scan_stopped_work);
1344
1345 ieee80211_sched_scan_end(local);
1346}
1347
Luciano Coelho79f460c2011-05-11 17:09:36 +03001348void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
1349{
1350 struct ieee80211_local *local = hw_to_local(hw);
1351
1352 trace_api_sched_scan_stopped(local);
1353
Eliad Peller2bc533b2016-01-05 16:28:06 +02001354 /*
1355 * this shouldn't really happen, so for simplicity
1356 * simply ignore it, and let mac80211 reconfigure
1357 * the sched scan later on.
1358 */
1359 if (local->in_reconfig)
1360 return;
1361
Johannes Berg18db5942013-11-06 10:34:36 +01001362 schedule_work(&local->sched_scan_stopped_work);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001363}
1364EXPORT_SYMBOL(ieee80211_sched_scan_stopped);