blob: b58f75da9c844ea0b2edbc49dd475d49319656fc [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
Johannes Bergc2b13452008-09-11 00:01:55 +020061struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +020062ieee80211_bss_info_update(struct ieee80211_local *local,
63 struct ieee80211_rx_status *rx_status,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +020064 struct ieee80211_mgmt *mgmt, size_t len,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +020065 struct ieee80211_channel *channel)
Johannes Berg5484e232008-09-08 17:44:27 +020066{
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +020067 bool beacon = ieee80211_is_beacon(mgmt->frame_control);
Johannes Berg0c1ad2c2009-12-23 13:15:39 +010068 struct cfg80211_bss *cbss;
Johannes Bergc2b13452008-09-11 00:01:55 +020069 struct ieee80211_bss *bss;
Stanislaw Gruszkaf0b058b2010-04-28 15:17:03 +020070 int clen, srlen;
Johannes Berg162dd6a2016-02-23 23:05:06 +020071 struct cfg80211_inform_bss bss_meta = {
72 .boottime_ns = rx_status->boottime_ns,
73 };
Sara Sharon74d803b2015-06-03 10:44:17 +030074 bool signal_valid;
Avraham Stern7947d3e2016-07-05 15:23:12 +030075 struct ieee80211_sub_if_data *scan_sdata;
Sara Sharon4abb52a2019-01-16 12:14:41 +020076 struct ieee802_11_elems elems;
77 size_t baselen;
78 u8 *elements;
Johannes Berg2a519312009-02-10 21:25:55 +010079
Tosoni1ad22fb2018-03-14 16:58:34 +000080 if (rx_status->flag & RX_FLAG_NO_SIGNAL_VAL)
81 bss_meta.signal = 0; /* invalid signal indication */
82 else if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
Johannes Berg61f6bba2015-10-13 11:36:21 +020083 bss_meta.signal = rx_status->signal * 100;
Johannes Berg30686bf2015-06-02 21:39:54 +020084 else if (ieee80211_hw_check(&local->hw, SIGNAL_UNSPEC))
Johannes Berg61f6bba2015-10-13 11:36:21 +020085 bss_meta.signal = (rx_status->signal * 100) / local->hw.max_signal;
Johannes Berg2a519312009-02-10 21:25:55 +010086
Johannes Berg61f6bba2015-10-13 11:36:21 +020087 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_20;
Johannes Bergda6a4352017-04-26 12:14:59 +020088 if (rx_status->bw == RATE_INFO_BW_5)
Johannes Berg61f6bba2015-10-13 11:36:21 +020089 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_5;
Johannes Bergda6a4352017-04-26 12:14:59 +020090 else if (rx_status->bw == RATE_INFO_BW_10)
Johannes Berg61f6bba2015-10-13 11:36:21 +020091 bss_meta.scan_width = NL80211_BSS_CHAN_WIDTH_10;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +020092
Johannes Berg61f6bba2015-10-13 11:36:21 +020093 bss_meta.chan = channel;
Avraham Stern7947d3e2016-07-05 15:23:12 +030094
95 rcu_read_lock();
96 scan_sdata = rcu_dereference(local->scan_sdata);
97 if (scan_sdata && scan_sdata->vif.type == NL80211_IFTYPE_STATION &&
98 scan_sdata->vif.bss_conf.assoc &&
99 ieee80211_have_rx_timestamp(rx_status)) {
100 bss_meta.parent_tsf =
101 ieee80211_calculate_rx_timestamp(local, rx_status,
102 len + FCS_LEN, 24);
103 ether_addr_copy(bss_meta.parent_bssid,
104 scan_sdata->vif.bss_conf.bssid);
105 }
106 rcu_read_unlock();
107
Johannes Berg61f6bba2015-10-13 11:36:21 +0200108 cbss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta,
109 mgmt, len, GFP_ATOMIC);
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100110 if (!cbss)
Johannes Berg00d3f142009-02-10 21:26:00 +0100111 return NULL;
Sara Sharon4abb52a2019-01-16 12:14:41 +0200112
113 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
114 elements = mgmt->u.probe_resp.variable;
115 baselen = offsetof(struct ieee80211_mgmt,
116 u.probe_resp.variable);
117 } else {
118 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
119 elements = mgmt->u.beacon.variable;
120 }
121
122 if (baselen > len)
123 return NULL;
124
125 ieee802_11_parse_elems(elements, len - baselen, false, &elems,
126 mgmt->bssid, cbss->bssid);
127
Sara Sharon74d803b2015-06-03 10:44:17 +0300128 /* In case the signal is invalid update the status */
129 signal_valid = abs(channel->center_freq - cbss->channel->center_freq)
130 <= local->hw.wiphy->max_adj_channel_rssi_comp;
131 if (!signal_valid)
132 rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
Johannes Berg00d3f142009-02-10 21:26:00 +0100133
Johannes Berg0c1ad2c2009-12-23 13:15:39 +0100134 bss = (void *)cbss->priv;
Johannes Berg5484e232008-09-08 17:44:27 +0200135
Johannes Bergef429da2013-02-05 17:48:40 +0100136 if (beacon)
137 bss->device_ts_beacon = rx_status->device_timestamp;
138 else
139 bss->device_ts_presp = rx_status->device_timestamp;
Johannes Berg8c358bc2012-05-22 22:13:05 +0200140
Sara Sharon4abb52a2019-01-16 12:14:41 +0200141 if (elems.parse_error) {
Paul Stewartfcff4f12012-02-23 17:59:53 -0800142 if (beacon)
143 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON;
144 else
145 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP;
146 } else {
147 if (beacon)
148 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON;
149 else
150 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP;
Johannes Berg5484e232008-09-08 17:44:27 +0200151 }
152
Paul Stewartfcff4f12012-02-23 17:59:53 -0800153 /* save the ERP value so that it is available at association time */
Sara Sharon4abb52a2019-01-16 12:14:41 +0200154 if (elems.erp_info && (!elems.parse_error ||
155 !(bss->valid_data & IEEE80211_BSS_VALID_ERP))) {
156 bss->erp_value = elems.erp_info[0];
Paul Stewartfcff4f12012-02-23 17:59:53 -0800157 bss->has_erp_value = true;
Sara Sharon4abb52a2019-01-16 12:14:41 +0200158 if (!elems.parse_error)
Paul Stewartfcff4f12012-02-23 17:59:53 -0800159 bss->valid_data |= IEEE80211_BSS_VALID_ERP;
160 }
161
Stanislaw Gruszkaf0b058b2010-04-28 15:17:03 +0200162 /* replace old supported rates if we get new values */
Sara Sharon4abb52a2019-01-16 12:14:41 +0200163 if (!elems.parse_error ||
Paul Stewartfcff4f12012-02-23 17:59:53 -0800164 !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) {
165 srlen = 0;
Sara Sharon4abb52a2019-01-16 12:14:41 +0200166 if (elems.supp_rates) {
Paul Stewartfcff4f12012-02-23 17:59:53 -0800167 clen = IEEE80211_MAX_SUPP_RATES;
Sara Sharon4abb52a2019-01-16 12:14:41 +0200168 if (clen > elems.supp_rates_len)
169 clen = elems.supp_rates_len;
170 memcpy(bss->supp_rates, elems.supp_rates, clen);
Paul Stewartfcff4f12012-02-23 17:59:53 -0800171 srlen += clen;
172 }
Sara Sharon4abb52a2019-01-16 12:14:41 +0200173 if (elems.ext_supp_rates) {
Paul Stewartfcff4f12012-02-23 17:59:53 -0800174 clen = IEEE80211_MAX_SUPP_RATES - srlen;
Sara Sharon4abb52a2019-01-16 12:14:41 +0200175 if (clen > elems.ext_supp_rates_len)
176 clen = elems.ext_supp_rates_len;
177 memcpy(bss->supp_rates + srlen, elems.ext_supp_rates,
Paul Stewartfcff4f12012-02-23 17:59:53 -0800178 clen);
179 srlen += clen;
180 }
181 if (srlen) {
182 bss->supp_rates_len = srlen;
Sara Sharon4abb52a2019-01-16 12:14:41 +0200183 if (!elems.parse_error)
Paul Stewartfcff4f12012-02-23 17:59:53 -0800184 bss->valid_data |= IEEE80211_BSS_VALID_RATES;
185 }
Johannes Berg5484e232008-09-08 17:44:27 +0200186 }
Johannes Berg5484e232008-09-08 17:44:27 +0200187
Sara Sharon4abb52a2019-01-16 12:14:41 +0200188 if (!elems.parse_error ||
Paul Stewartfcff4f12012-02-23 17:59:53 -0800189 !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) {
Sara Sharon4abb52a2019-01-16 12:14:41 +0200190 bss->wmm_used = elems.wmm_param || elems.wmm_info;
191 bss->uapsd_supported = is_uapsd_supported(&elems);
192 if (!elems.parse_error)
Paul Stewartfcff4f12012-02-23 17:59:53 -0800193 bss->valid_data |= IEEE80211_BSS_VALID_WMM;
194 }
Johannes Berg5484e232008-09-08 17:44:27 +0200195
Alexander Bondar817cee72013-05-19 14:23:57 +0300196 if (beacon) {
197 struct ieee80211_supported_band *sband =
198 local->hw.wiphy->bands[rx_status->band];
Johannes Bergda6a4352017-04-26 12:14:59 +0200199 if (!(rx_status->encoding == RX_ENC_HT) &&
200 !(rx_status->encoding == RX_ENC_VHT))
Alexander Bondar817cee72013-05-19 14:23:57 +0300201 bss->beacon_rate =
202 &sband->bitrates[rx_status->rate_idx];
203 }
204
Johannes Berg5484e232008-09-08 17:44:27 +0200205 return bss;
206}
Johannes Berg0a51b272008-09-08 17:44:25 +0200207
Roee Zamir40b0bd22017-08-06 11:38:23 +0300208static bool ieee80211_scan_accept_presp(struct ieee80211_sub_if_data *sdata,
209 u32 scan_flags, const u8 *da)
210{
211 if (!sdata)
212 return false;
213 /* accept broadcast for OCE */
214 if (scan_flags & NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP &&
215 is_broadcast_ether_addr(da))
216 return true;
217 if (scan_flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
218 return true;
219 return ether_addr_equal(da, sdata->vif.addr);
220}
221
Johannes Bergd48b2962012-07-06 22:19:27 +0200222void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb)
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200223{
Johannes Bergf1d58c22009-06-17 13:13:00 +0200224 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
Johannes Bergd48b2962012-07-06 22:19:27 +0200225 struct ieee80211_sub_if_data *sdata1, *sdata2;
226 struct ieee80211_mgmt *mgmt = (void *)skb->data;
Johannes Bergc2b13452008-09-11 00:01:55 +0200227 struct ieee80211_bss *bss;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200228 struct ieee80211_channel *channel;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200229
Johannes Bergd48b2962012-07-06 22:19:27 +0200230 if (skb->len < 24 ||
231 (!ieee80211_is_probe_resp(mgmt->frame_control) &&
232 !ieee80211_is_beacon(mgmt->frame_control)))
233 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200234
Johannes Bergd48b2962012-07-06 22:19:27 +0200235 sdata1 = rcu_dereference(local->scan_sdata);
236 sdata2 = rcu_dereference(local->sched_scan_sdata);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200237
Johannes Bergd48b2962012-07-06 22:19:27 +0200238 if (likely(!sdata1 && !sdata2))
239 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200240
Johannes Bergd48b2962012-07-06 22:19:27 +0200241 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
Johannes Berga344d672014-06-12 22:24:31 +0200242 struct cfg80211_scan_request *scan_req;
243 struct cfg80211_sched_scan_request *sched_scan_req;
Roee Zamir40b0bd22017-08-06 11:38:23 +0300244 u32 scan_req_flags = 0, sched_scan_req_flags = 0;
Johannes Berga344d672014-06-12 22:24:31 +0200245
246 scan_req = rcu_dereference(local->scan_req);
247 sched_scan_req = rcu_dereference(local->sched_scan_req);
248
Roee Zamir40b0bd22017-08-06 11:38:23 +0300249 if (scan_req)
250 scan_req_flags = scan_req->flags;
251
252 if (sched_scan_req)
253 sched_scan_req_flags = sched_scan_req->flags;
254
255 /* ignore ProbeResp to foreign address or non-bcast (OCE)
256 * unless scanning with randomised address
Johannes Berga344d672014-06-12 22:24:31 +0200257 */
Roee Zamir40b0bd22017-08-06 11:38:23 +0300258 if (!ieee80211_scan_accept_presp(sdata1, scan_req_flags,
259 mgmt->da) &&
260 !ieee80211_scan_accept_presp(sdata2, sched_scan_req_flags,
261 mgmt->da))
Johannes Bergd48b2962012-07-06 22:19:27 +0200262 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200263 }
264
Johannes Berg0172bb72012-11-23 14:23:30 +0100265 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200266
267 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
Johannes Bergd48b2962012-07-06 22:19:27 +0200268 return;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200269
Johannes Bergd48b2962012-07-06 22:19:27 +0200270 bss = ieee80211_bss_info_update(local, rx_status,
Sara Sharon4abb52a2019-01-16 12:14:41 +0200271 mgmt, skb->len,
Emmanuel Grumbachd45c4172012-12-10 16:19:13 +0200272 channel);
Jouni Malinend048e502008-10-11 03:29:55 +0300273 if (bss)
Johannes Bergd48b2962012-07-06 22:19:27 +0200274 ieee80211_rx_bss_put(local, bss);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200275}
276
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200277static void
278ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef,
279 enum nl80211_bss_scan_width scan_width)
280{
281 memset(chandef, 0, sizeof(*chandef));
282 switch (scan_width) {
283 case NL80211_BSS_CHAN_WIDTH_5:
284 chandef->width = NL80211_CHAN_WIDTH_5;
285 break;
286 case NL80211_BSS_CHAN_WIDTH_10:
287 chandef->width = NL80211_CHAN_WIDTH_10;
288 break;
289 default:
290 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
291 break;
292 }
293}
294
Johannes Berg4d36ec52009-10-27 20:59:55 +0100295/* return false if no more work */
296static bool ieee80211_prep_hw_scan(struct ieee80211_local *local)
297{
Johannes Berg6ea0a692014-11-19 11:55:49 +0100298 struct cfg80211_scan_request *req;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200299 struct cfg80211_chan_def chandef;
David Spinadelc56ef672014-02-05 15:21:13 +0200300 u8 bands_used = 0;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100301 int i, ielen, n_chans;
Johannes Bergb9771d42018-05-28 15:47:41 +0200302 u32 flags = 0;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100303
Johannes Berg6ea0a692014-11-19 11:55:49 +0100304 req = rcu_dereference_protected(local->scan_req,
305 lockdep_is_held(&local->mtx));
306
Emmanuel Grumbacha7540552013-09-16 11:12:07 +0300307 if (test_bit(SCAN_HW_CANCELLED, &local->scanning))
308 return false;
309
Johannes Berg30686bf2015-06-02 21:39:54 +0200310 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {
Johannes Berg4d36ec52009-10-27 20:59:55 +0100311 for (i = 0; i < req->n_channels; i++) {
David Spinadelc56ef672014-02-05 15:21:13 +0200312 local->hw_scan_req->req.channels[i] = req->channels[i];
313 bands_used |= BIT(req->channels[i]->band);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100314 }
315
David Spinadelc56ef672014-02-05 15:21:13 +0200316 n_chans = req->n_channels;
317 } else {
318 do {
Johannes Berg57fbcce2016-04-12 15:56:15 +0200319 if (local->hw_scan_band == NUM_NL80211_BANDS)
David Spinadelc56ef672014-02-05 15:21:13 +0200320 return false;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100321
David Spinadelc56ef672014-02-05 15:21:13 +0200322 n_chans = 0;
323
324 for (i = 0; i < req->n_channels; i++) {
325 if (req->channels[i]->band !=
326 local->hw_scan_band)
327 continue;
328 local->hw_scan_req->req.channels[n_chans] =
329 req->channels[i];
330 n_chans++;
331 bands_used |= BIT(req->channels[i]->band);
332 }
333
334 local->hw_scan_band++;
335 } while (!n_chans);
336 }
337
338 local->hw_scan_req->req.n_channels = n_chans;
Simon Wunderlich2103dec2013-07-08 16:55:53 +0200339 ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100340
Johannes Bergb9771d42018-05-28 15:47:41 +0200341 if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
342 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
343
David Spinadelc56ef672014-02-05 15:21:13 +0200344 ielen = ieee80211_build_preq_ies(local,
345 (u8 *)local->hw_scan_req->req.ie,
Johannes Bergc604b9f2012-11-29 12:45:18 +0100346 local->hw_scan_ies_bufsize,
David Spinadelc56ef672014-02-05 15:21:13 +0200347 &local->hw_scan_req->ies,
348 req->ie, req->ie_len,
Johannes Bergb9771d42018-05-28 15:47:41 +0200349 bands_used, req->rates, &chandef,
350 flags);
David Spinadelc56ef672014-02-05 15:21:13 +0200351 local->hw_scan_req->req.ie_len = ielen;
352 local->hw_scan_req->req.no_cck = req->no_cck;
Johannes Berga344d672014-06-12 22:24:31 +0200353 ether_addr_copy(local->hw_scan_req->req.mac_addr, req->mac_addr);
354 ether_addr_copy(local->hw_scan_req->req.mac_addr_mask,
355 req->mac_addr_mask);
Jouni Malinene345f442016-02-26 22:12:48 +0200356 ether_addr_copy(local->hw_scan_req->req.bssid, req->bssid);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100357
358 return true;
359}
360
Eliad Peller8bd2a242013-12-05 11:21:27 +0200361static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
Johannes Berg0a51b272008-09-08 17:44:25 +0200362{
363 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berge9da68d2018-10-18 10:35:47 +0200364 bool hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
Eliad Pellera2b70e82013-12-05 11:21:28 +0200365 bool was_scanning = local->scanning;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100366 struct cfg80211_scan_request *scan_req;
Johannes Berga344d672014-06-12 22:24:31 +0200367 struct ieee80211_sub_if_data *scan_sdata;
Sachin Kulkarni4fa11ec2016-01-12 14:30:19 +0530368 struct ieee80211_sub_if_data *sdata;
Johannes Berg0a51b272008-09-08 17:44:25 +0200369
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200370 lockdep_assert_held(&local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +0200371
Johannes Berg6d3560d2009-10-31 07:44:08 +0100372 /*
373 * It's ok to abort a not-yet-running scan (that
374 * we have one at all will be verified by checking
375 * local->scan_req next), but not to complete it
376 * successfully.
377 */
378 if (WARN_ON(!local->scanning && !aborted))
379 aborted = true;
Johannes Bergde95a542009-04-01 11:58:36 +0200380
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200381 if (WARN_ON(!local->scan_req))
Johannes Bergd07bfd82011-03-07 15:48:41 +0100382 return;
Johannes Bergf3b85252009-04-23 16:01:47 +0200383
David Spinadelc56ef672014-02-05 15:21:13 +0200384 if (hw_scan && !aborted &&
Johannes Berg30686bf2015-06-02 21:39:54 +0200385 !ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS) &&
David Spinadelc56ef672014-02-05 15:21:13 +0200386 ieee80211_prep_hw_scan(local)) {
Johannes Berge2fd5db2012-07-06 21:39:28 +0200387 int rc;
388
389 rc = drv_hw_scan(local,
390 rcu_dereference_protected(local->scan_sdata,
391 lockdep_is_held(&local->mtx)),
392 local->hw_scan_req);
393
Stanislaw Gruszka6eb11a92010-10-06 11:22:11 +0200394 if (rc == 0)
Johannes Bergd07bfd82011-03-07 15:48:41 +0100395 return;
Avraham Stern7947d3e2016-07-05 15:23:12 +0300396
Johannes Berg7d10f6b2016-07-05 15:23:13 +0300397 /* HW scan failed and is going to be reported as aborted,
398 * so clear old scan info.
Avraham Stern7947d3e2016-07-05 15:23:12 +0300399 */
400 memset(&local->scan_info, 0, sizeof(local->scan_info));
Johannes Berg7d10f6b2016-07-05 15:23:13 +0300401 aborted = true;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100402 }
403
404 kfree(local->hw_scan_req);
405 local->hw_scan_req = NULL;
Johannes Bergf3b85252009-04-23 16:01:47 +0200406
Johannes Berg6ea0a692014-11-19 11:55:49 +0100407 scan_req = rcu_dereference_protected(local->scan_req,
408 lockdep_is_held(&local->mtx));
409
Avraham Stern1d762502016-07-05 17:10:13 +0300410 if (scan_req != local->int_scan_req) {
Avraham Stern7947d3e2016-07-05 15:23:12 +0300411 local->scan_info.aborted = aborted;
412 cfg80211_scan_done(scan_req, &local->scan_info);
Avraham Stern1d762502016-07-05 17:10:13 +0300413 }
Johannes Berg6ea0a692014-11-19 11:55:49 +0100414 RCU_INIT_POINTER(local->scan_req, NULL);
Johannes Berga344d672014-06-12 22:24:31 +0200415
416 scan_sdata = rcu_dereference_protected(local->scan_sdata,
417 lockdep_is_held(&local->mtx));
Monam Agarwal0c2bef462014-03-24 00:51:43 +0530418 RCU_INIT_POINTER(local->scan_sdata, NULL);
Johannes Berg2a519312009-02-10 21:25:55 +0100419
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200420 local->scanning = 0;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200421 local->scan_chandef.chan = NULL;
Johannes Bergf3b85252009-04-23 16:01:47 +0200422
Johannes Berg07ef03e2011-11-08 16:21:21 +0100423 /* Set power back to normal operating levels. */
424 ieee80211_hw_config(local, 0);
Ben Greearb23b0252011-02-04 11:54:17 -0800425
Eliad Peller8bd2a242013-12-05 11:21:27 +0200426 if (!hw_scan) {
Ben Greearb23b0252011-02-04 11:54:17 -0800427 ieee80211_configure_filter(local);
Johannes Berga344d672014-06-12 22:24:31 +0200428 drv_sw_scan_complete(local, scan_sdata);
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +0100429 ieee80211_offchannel_return(local);
Ben Greearb23b0252011-02-04 11:54:17 -0800430 }
431
Johannes Berg5cff20e2009-04-29 12:26:17 +0200432 ieee80211_recalc_idle(local);
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200433
Johannes Berg0a51b272008-09-08 17:44:25 +0200434 ieee80211_mlme_notify_scan_completed(local);
Johannes Berg46900292009-02-15 12:44:28 +0100435 ieee80211_ibss_notify_scan_completed(local);
Sachin Kulkarni4fa11ec2016-01-12 14:30:19 +0530436
437 /* Requeue all the work that might have been ignored while
438 * the scan was in progress; if there was none this will
439 * just be a no-op for the particular interface.
440 */
441 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
442 if (ieee80211_sdata_running(sdata))
443 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
444 }
445
Eliad Pellera2b70e82013-12-05 11:21:28 +0200446 if (was_scanning)
447 ieee80211_start_next_roc(local);
Johannes Berg0a51b272008-09-08 17:44:25 +0200448}
Johannes Berg8789d452010-08-26 13:30:26 +0200449
Avraham Stern7947d3e2016-07-05 15:23:12 +0300450void ieee80211_scan_completed(struct ieee80211_hw *hw,
451 struct cfg80211_scan_info *info)
Johannes Berg8789d452010-08-26 13:30:26 +0200452{
453 struct ieee80211_local *local = hw_to_local(hw);
454
Johannes Berg58bd7f12016-09-14 09:37:54 +0200455 trace_api_scan_completed(local, info->aborted);
Johannes Berg8789d452010-08-26 13:30:26 +0200456
457 set_bit(SCAN_COMPLETED, &local->scanning);
Avraham Stern7947d3e2016-07-05 15:23:12 +0300458 if (info->aborted)
Johannes Berg8789d452010-08-26 13:30:26 +0200459 set_bit(SCAN_ABORTED, &local->scanning);
Avraham Stern7947d3e2016-07-05 15:23:12 +0300460
461 memcpy(&local->scan_info, info, sizeof(*info));
462
Johannes Berg8789d452010-08-26 13:30:26 +0200463 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
464}
Johannes Berg0a51b272008-09-08 17:44:25 +0200465EXPORT_SYMBOL(ieee80211_scan_completed);
466
Johannes Berga344d672014-06-12 22:24:31 +0200467static int ieee80211_start_sw_scan(struct ieee80211_local *local,
468 struct ieee80211_sub_if_data *sdata)
Johannes Bergf3b85252009-04-23 16:01:47 +0200469{
Johannes Bergfe57d9f2012-07-26 14:55:08 +0200470 /* Software scan is not supported in multi-channel cases */
471 if (local->use_chanctx)
472 return -EOPNOTSUPP;
473
Johannes Bergf3b85252009-04-23 16:01:47 +0200474 /*
475 * Hardware/driver doesn't support hw_scan, so use software
476 * scanning instead. First send a nullfunc frame with power save
477 * bit on so that AP will buffer the frames for us while we are not
478 * listening, then send probe requests to each channel and wait for
479 * the responses. After all channels are scanned, tune back to the
480 * original channel and send a nullfunc frame with power save bit
481 * off to trigger the AP to send us all the buffered frames.
482 *
483 * Note that while local->sw_scanning is true everything else but
484 * nullfunc frames and probe requests will be dropped in
485 * ieee80211_tx_h_check_assoc().
486 */
Johannes Berga344d672014-06-12 22:24:31 +0200487 drv_sw_scan_start(local, sdata, local->scan_addr);
Johannes Bergf3b85252009-04-23 16:01:47 +0200488
Rajkumar Manoharande312db2012-03-27 11:01:06 +0530489 local->leave_oper_channel_time = jiffies;
Helmut Schaa977923b2009-07-23 12:14:20 +0200490 local->next_scan_state = SCAN_DECISION;
Johannes Bergf3b85252009-04-23 16:01:47 +0200491 local->scan_channel_idx = 0;
492
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +0100493 ieee80211_offchannel_stop_vifs(local);
Johannes Berga80f7c02009-12-23 13:15:32 +0100494
Seth Forshee9c35d7d2013-02-11 11:21:08 -0600495 /* ensure nullfunc is transmitted before leaving operating channel */
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +0200496 ieee80211_flush_queues(local, NULL, false);
Seth Forshee9c35d7d2013-02-11 11:21:08 -0600497
Johannes Berg3ac64be2009-08-17 16:16:53 +0200498 ieee80211_configure_filter(local);
Johannes Bergf3b85252009-04-23 16:01:47 +0200499
Ben Greear59bdf3b2011-02-07 13:44:38 -0800500 /* We need to set power level at maximum rate for scanning. */
501 ieee80211_hw_config(local, 0);
502
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400503 ieee80211_queue_delayed_work(&local->hw,
Johannes Berg07ef03e2011-11-08 16:21:21 +0100504 &local->scan_work, 0);
Johannes Bergf3b85252009-04-23 16:01:47 +0200505
506 return 0;
507}
508
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200509static bool ieee80211_can_scan(struct ieee80211_local *local,
510 struct ieee80211_sub_if_data *sdata)
511{
Eliad Peller5cbc95a2015-01-07 17:50:09 +0200512 if (ieee80211_is_radar_required(local))
Simon Wunderlich164eb022013-02-08 18:16:20 +0100513 return false;
514
Johannes Berg2eb278e2012-06-05 14:28:42 +0200515 if (!list_empty(&local->roc_list))
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200516 return false;
517
518 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
Stanislaw Gruszka392b9ff2013-08-27 11:36:35 +0200519 sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL)
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200520 return false;
521
522 return true;
523}
524
525void ieee80211_run_deferred_scan(struct ieee80211_local *local)
526{
527 lockdep_assert_held(&local->mtx);
528
529 if (!local->scan_req || local->scanning)
530 return;
531
Johannes Berge2fd5db2012-07-06 21:39:28 +0200532 if (!ieee80211_can_scan(local,
533 rcu_dereference_protected(
534 local->scan_sdata,
535 lockdep_is_held(&local->mtx))))
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200536 return;
537
538 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
539 round_jiffies_relative(0));
540}
Johannes Bergf3b85252009-04-23 16:01:47 +0200541
Johannes Berg45ad6832018-05-28 15:47:39 +0200542static void ieee80211_send_scan_probe_req(struct ieee80211_sub_if_data *sdata,
543 const u8 *src, const u8 *dst,
544 const u8 *ssid, size_t ssid_len,
545 const u8 *ie, size_t ie_len,
546 u32 ratemask, u32 flags, u32 tx_flags,
547 struct ieee80211_channel *channel)
548{
549 struct sk_buff *skb;
Johannes Bergb9771d42018-05-28 15:47:41 +0200550 u32 txdata_flags = 0;
Johannes Berg45ad6832018-05-28 15:47:39 +0200551
552 skb = ieee80211_build_probe_req(sdata, src, dst, ratemask, channel,
553 ssid, ssid_len,
554 ie, ie_len, flags);
Johannes Bergb9771d42018-05-28 15:47:41 +0200555
Johannes Berg45ad6832018-05-28 15:47:39 +0200556 if (skb) {
Johannes Bergb9771d42018-05-28 15:47:41 +0200557 if (flags & IEEE80211_PROBE_FLAG_RANDOM_SN) {
558 struct ieee80211_hdr *hdr = (void *)skb->data;
559 u16 sn = get_random_u32();
560
561 txdata_flags |= IEEE80211_TX_NO_SEQNO;
562 hdr->seq_ctrl =
563 cpu_to_le16(IEEE80211_SN_TO_SEQ(sn));
564 }
Johannes Berg45ad6832018-05-28 15:47:39 +0200565 IEEE80211_SKB_CB(skb)->flags |= tx_flags;
Johannes Bergb9771d42018-05-28 15:47:41 +0200566 ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band,
567 txdata_flags);
Johannes Berg45ad6832018-05-28 15:47:39 +0200568 }
569}
570
Ben Greear8a690672012-04-17 10:54:16 -0700571static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
572 unsigned long *next_delay)
573{
574 int i;
Johannes Berge2fd5db2012-07-06 21:39:28 +0200575 struct ieee80211_sub_if_data *sdata;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100576 struct cfg80211_scan_request *scan_req;
Johannes Berg57fbcce2016-04-12 15:56:15 +0200577 enum nl80211_band band = local->hw.conf.chandef.chan->band;
Johannes Bergb9771d42018-05-28 15:47:41 +0200578 u32 flags = 0, tx_flags;
Seth Forshee6c17b772013-02-11 11:21:07 -0600579
Johannes Berg6ea0a692014-11-19 11:55:49 +0100580 scan_req = rcu_dereference_protected(local->scan_req,
581 lockdep_is_held(&local->mtx));
582
Seth Forshee6c17b772013-02-11 11:21:07 -0600583 tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100584 if (scan_req->no_cck)
Seth Forshee6c17b772013-02-11 11:21:07 -0600585 tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
Johannes Bergb9771d42018-05-28 15:47:41 +0200586 if (scan_req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
587 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
588 if (scan_req->flags & NL80211_SCAN_FLAG_RANDOM_SN)
589 flags |= IEEE80211_PROBE_FLAG_RANDOM_SN;
Ben Greear8a690672012-04-17 10:54:16 -0700590
Johannes Berge2fd5db2012-07-06 21:39:28 +0200591 sdata = rcu_dereference_protected(local->scan_sdata,
Peter Senna Tschudin316b6b52012-09-06 18:09:16 +0200592 lockdep_is_held(&local->mtx));
Johannes Berge2fd5db2012-07-06 21:39:28 +0200593
Johannes Berg6ea0a692014-11-19 11:55:49 +0100594 for (i = 0; i < scan_req->n_ssids; i++)
Johannes Berg45ad6832018-05-28 15:47:39 +0200595 ieee80211_send_scan_probe_req(
Jouni Malinene345f442016-02-26 22:12:48 +0200596 sdata, local->scan_addr, scan_req->bssid,
Johannes Berg6ea0a692014-11-19 11:55:49 +0100597 scan_req->ssids[i].ssid, scan_req->ssids[i].ssid_len,
598 scan_req->ie, scan_req->ie_len,
Johannes Bergb9771d42018-05-28 15:47:41 +0200599 scan_req->rates[band], flags,
Johannes Berg45ad6832018-05-28 15:47:39 +0200600 tx_flags, local->hw.conf.chandef.chan);
Ben Greear8a690672012-04-17 10:54:16 -0700601
602 /*
603 * After sending probe requests, wait for probe responses
604 * on the channel.
605 */
606 *next_delay = IEEE80211_CHANNEL_TIME;
607 local->next_scan_state = SCAN_DECISION;
608}
609
Johannes Bergf3b85252009-04-23 16:01:47 +0200610static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
611 struct cfg80211_scan_request *req)
612{
613 struct ieee80211_local *local = sdata->local;
Johannes Berge9da68d2018-10-18 10:35:47 +0200614 bool hw_scan = local->ops->hw_scan;
Johannes Bergf3b85252009-04-23 16:01:47 +0200615 int rc;
616
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200617 lockdep_assert_held(&local->mtx);
618
Eliad Peller2726f232015-01-07 17:50:11 +0200619 if (local->scan_req || ieee80211_is_radar_required(local))
Johannes Bergf3b85252009-04-23 16:01:47 +0200620 return -EBUSY;
621
Stanislaw Gruszka133d40f2012-03-28 16:01:19 +0200622 if (!ieee80211_can_scan(local, sdata)) {
John W. Linville6e7e6212010-02-08 16:38:38 -0500623 /* wait for the work to finish/time out */
Johannes Berg6ea0a692014-11-19 11:55:49 +0100624 rcu_assign_pointer(local->scan_req, req);
Johannes Berge2fd5db2012-07-06 21:39:28 +0200625 rcu_assign_pointer(local->scan_sdata, sdata);
Johannes Bergc0ce77b2010-02-03 10:22:31 +0100626 return 0;
627 }
628
Johannes Berge9da68d2018-10-18 10:35:47 +0200629 again:
630 if (hw_scan) {
Johannes Bergf3b85252009-04-23 16:01:47 +0200631 u8 *ies;
Johannes Bergf3b85252009-04-23 16:01:47 +0200632
David Spinadele4dcbb32014-02-11 13:45:41 +0200633 local->hw_scan_ies_bufsize = local->scan_ies_len + req->ie_len;
David Spinadelc56ef672014-02-05 15:21:13 +0200634
Johannes Berg30686bf2015-06-02 21:39:54 +0200635 if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {
David Spinadelc56ef672014-02-05 15:21:13 +0200636 int i, n_bands = 0;
637 u8 bands_counted = 0;
638
639 for (i = 0; i < req->n_channels; i++) {
640 if (bands_counted & BIT(req->channels[i]->band))
641 continue;
642 bands_counted |= BIT(req->channels[i]->band);
643 n_bands++;
644 }
645
646 local->hw_scan_ies_bufsize *= n_bands;
647 }
648
Johannes Berg4d36ec52009-10-27 20:59:55 +0100649 local->hw_scan_req = kmalloc(
650 sizeof(*local->hw_scan_req) +
651 req->n_channels * sizeof(req->channels[0]) +
Johannes Bergc604b9f2012-11-29 12:45:18 +0100652 local->hw_scan_ies_bufsize, GFP_KERNEL);
Johannes Berg4d36ec52009-10-27 20:59:55 +0100653 if (!local->hw_scan_req)
Johannes Bergf3b85252009-04-23 16:01:47 +0200654 return -ENOMEM;
655
David Spinadelc56ef672014-02-05 15:21:13 +0200656 local->hw_scan_req->req.ssids = req->ssids;
657 local->hw_scan_req->req.n_ssids = req->n_ssids;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100658 ies = (u8 *)local->hw_scan_req +
659 sizeof(*local->hw_scan_req) +
660 req->n_channels * sizeof(req->channels[0]);
David Spinadelc56ef672014-02-05 15:21:13 +0200661 local->hw_scan_req->req.ie = ies;
662 local->hw_scan_req->req.flags = req->flags;
Jouni Malinene345f442016-02-26 22:12:48 +0200663 eth_broadcast_addr(local->hw_scan_req->req.bssid);
Avraham Stern7947d3e2016-07-05 15:23:12 +0300664 local->hw_scan_req->req.duration = req->duration;
665 local->hw_scan_req->req.duration_mandatory =
666 req->duration_mandatory;
Johannes Berg4d36ec52009-10-27 20:59:55 +0100667
668 local->hw_scan_band = 0;
John W. Linville6e7e6212010-02-08 16:38:38 -0500669
670 /*
671 * After allocating local->hw_scan_req, we must
672 * go through until ieee80211_prep_hw_scan(), so
673 * anything that might be changed here and leave
674 * this function early must not go after this
675 * allocation.
676 */
Johannes Bergf3b85252009-04-23 16:01:47 +0200677 }
678
Johannes Berg6ea0a692014-11-19 11:55:49 +0100679 rcu_assign_pointer(local->scan_req, req);
Johannes Berge2fd5db2012-07-06 21:39:28 +0200680 rcu_assign_pointer(local->scan_sdata, sdata);
Johannes Bergf3b85252009-04-23 16:01:47 +0200681
Johannes Berga344d672014-06-12 22:24:31 +0200682 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
683 get_random_mask_addr(local->scan_addr,
684 req->mac_addr,
685 req->mac_addr_mask);
686 else
687 memcpy(local->scan_addr, sdata->vif.addr, ETH_ALEN);
688
Johannes Berge9da68d2018-10-18 10:35:47 +0200689 if (hw_scan) {
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200690 __set_bit(SCAN_HW_SCANNING, &local->scanning);
Ben Greear8a690672012-04-17 10:54:16 -0700691 } else if ((req->n_channels == 1) &&
Karl Beldan675a0b02013-03-25 16:26:57 +0100692 (req->channels[0] == local->_oper_chandef.chan)) {
Johannes Berg9b864872012-07-26 14:38:32 +0200693 /*
694 * If we are scanning only on the operating channel
695 * then we do not need to stop normal activities
Ben Greear8a690672012-04-17 10:54:16 -0700696 */
697 unsigned long next_delay;
698
699 __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
700
701 ieee80211_recalc_idle(local);
702
703 /* Notify driver scan is starting, keep order of operations
704 * same as normal software scan, in case that matters. */
Johannes Berga344d672014-06-12 22:24:31 +0200705 drv_sw_scan_start(local, sdata, local->scan_addr);
Ben Greear8a690672012-04-17 10:54:16 -0700706
707 ieee80211_configure_filter(local); /* accept probe-responses */
708
709 /* We need to ensure power level is at max for scanning. */
710 ieee80211_hw_config(local, 0);
711
Antonio Quartulli4e39cca2015-11-21 18:13:40 +0800712 if ((req->channels[0]->flags & (IEEE80211_CHAN_NO_IR |
713 IEEE80211_CHAN_RADAR)) ||
Johannes Berg6ea0a692014-11-19 11:55:49 +0100714 !req->n_ssids) {
Ben Greear8a690672012-04-17 10:54:16 -0700715 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
716 } else {
717 ieee80211_scan_state_send_probe(local, &next_delay);
718 next_delay = IEEE80211_CHANNEL_TIME;
719 }
720
721 /* Now, just wait a bit and we are all done! */
722 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
723 next_delay);
724 return 0;
725 } else {
726 /* Do normal software scan */
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200727 __set_bit(SCAN_SW_SCANNING, &local->scanning);
Ben Greear8a690672012-04-17 10:54:16 -0700728 }
John W. Linville6e7e6212010-02-08 16:38:38 -0500729
Johannes Berg5cff20e2009-04-29 12:26:17 +0200730 ieee80211_recalc_idle(local);
Johannes Bergf3b85252009-04-23 16:01:47 +0200731
Johannes Berge9da68d2018-10-18 10:35:47 +0200732 if (hw_scan) {
Johannes Berg4d36ec52009-10-27 20:59:55 +0100733 WARN_ON(!ieee80211_prep_hw_scan(local));
Johannes Berga060bbf2010-04-27 11:59:34 +0200734 rc = drv_hw_scan(local, sdata, local->hw_scan_req);
Johannes Berga344d672014-06-12 22:24:31 +0200735 } else {
736 rc = ieee80211_start_sw_scan(local, sdata);
737 }
Johannes Bergf3b85252009-04-23 16:01:47 +0200738
Johannes Bergf3b85252009-04-23 16:01:47 +0200739 if (rc) {
Johannes Berg4d36ec52009-10-27 20:59:55 +0100740 kfree(local->hw_scan_req);
741 local->hw_scan_req = NULL;
Helmut Schaafbe9c4292009-07-23 12:14:04 +0200742 local->scanning = 0;
Johannes Bergf3b85252009-04-23 16:01:47 +0200743
Johannes Berg5cff20e2009-04-29 12:26:17 +0200744 ieee80211_recalc_idle(local);
745
Johannes Bergf3b85252009-04-23 16:01:47 +0200746 local->scan_req = NULL;
Monam Agarwal0c2bef462014-03-24 00:51:43 +0530747 RCU_INIT_POINTER(local->scan_sdata, NULL);
Johannes Bergf3b85252009-04-23 16:01:47 +0200748 }
749
Johannes Berge9da68d2018-10-18 10:35:47 +0200750 if (hw_scan && rc == 1) {
751 /*
752 * we can't fall back to software for P2P-GO
753 * as it must update NoA etc.
754 */
755 if (ieee80211_vif_type_p2p(&sdata->vif) ==
756 NL80211_IFTYPE_P2P_GO)
757 return -EOPNOTSUPP;
758 hw_scan = false;
759 goto again;
760 }
761
Johannes Bergf3b85252009-04-23 16:01:47 +0200762 return rc;
763}
764
Helmut Schaadf13cce2010-02-24 14:19:21 +0100765static unsigned long
766ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
767{
768 /*
769 * TODO: channel switching also consumes quite some time,
770 * add that delay as well to get a better estimation
771 */
Antonio Quartulli4e39cca2015-11-21 18:13:40 +0800772 if (chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR))
Helmut Schaadf13cce2010-02-24 14:19:21 +0100773 return IEEE80211_PASSIVE_CHANNEL_TIME;
774 return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
775}
776
Stanislaw Gruszkae229f842010-10-06 11:22:09 +0200777static void ieee80211_scan_state_decision(struct ieee80211_local *local,
778 unsigned long *next_delay)
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200779{
Helmut Schaa142b9f52009-07-23 13:18:01 +0200780 bool associated = false;
Helmut Schaadf13cce2010-02-24 14:19:21 +0100781 bool tx_empty = true;
782 bool bad_latency;
Helmut Schaa142b9f52009-07-23 13:18:01 +0200783 struct ieee80211_sub_if_data *sdata;
Helmut Schaadf13cce2010-02-24 14:19:21 +0100784 struct ieee80211_channel *next_chan;
Sam Lefflercd2bb512012-10-11 21:03:35 -0700785 enum mac80211_scan_state next_scan_state;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100786 struct cfg80211_scan_request *scan_req;
Helmut Schaa142b9f52009-07-23 13:18:01 +0200787
Helmut Schaadf13cce2010-02-24 14:19:21 +0100788 /*
789 * check if at least one STA interface is associated,
790 * check if at least one STA interface has pending tx frames
791 * and grab the lowest used beacon interval
792 */
Helmut Schaa142b9f52009-07-23 13:18:01 +0200793 mutex_lock(&local->iflist_mtx);
794 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Berg9607e6b662009-12-23 13:15:31 +0100795 if (!ieee80211_sdata_running(sdata))
Helmut Schaa142b9f52009-07-23 13:18:01 +0200796 continue;
797
798 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
799 if (sdata->u.mgd.associated) {
800 associated = true;
Helmut Schaadf13cce2010-02-24 14:19:21 +0100801
Helmut Schaadf13cce2010-02-24 14:19:21 +0100802 if (!qdisc_all_tx_empty(sdata->dev)) {
803 tx_empty = false;
804 break;
805 }
Helmut Schaa142b9f52009-07-23 13:18:01 +0200806 }
807 }
808 }
809 mutex_unlock(&local->iflist_mtx);
810
Johannes Berg6ea0a692014-11-19 11:55:49 +0100811 scan_req = rcu_dereference_protected(local->scan_req,
812 lockdep_is_held(&local->mtx));
813
814 next_chan = scan_req->channels[local->scan_channel_idx];
Ben Greearb23b0252011-02-04 11:54:17 -0800815
Johannes Berg07ef03e2011-11-08 16:21:21 +0100816 /*
817 * we're currently scanning a different channel, let's
818 * see if we can scan another channel without interfering
819 * with the current traffic situation.
820 *
Stanislaw Gruszka3f892b62013-01-23 12:32:45 +0100821 * Keep good latency, do not stay off-channel more than 125 ms.
Johannes Berg07ef03e2011-11-08 16:21:21 +0100822 */
Helmut Schaadf13cce2010-02-24 14:19:21 +0100823
Johannes Berg07ef03e2011-11-08 16:21:21 +0100824 bad_latency = time_after(jiffies +
Stanislaw Gruszka3f892b62013-01-23 12:32:45 +0100825 ieee80211_scan_get_channel_time(next_chan),
826 local->leave_oper_channel_time + HZ / 8);
Helmut Schaadf13cce2010-02-24 14:19:21 +0100827
Sam Lefflercd2bb512012-10-11 21:03:35 -0700828 if (associated && !tx_empty) {
Johannes Berg6ea0a692014-11-19 11:55:49 +0100829 if (scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
Sam Lefflercd2bb512012-10-11 21:03:35 -0700830 next_scan_state = SCAN_ABORT;
831 else
832 next_scan_state = SCAN_SUSPEND;
Stanislaw Gruszka3f892b62013-01-23 12:32:45 +0100833 } else if (associated && bad_latency) {
Sam Lefflercd2bb512012-10-11 21:03:35 -0700834 next_scan_state = SCAN_SUSPEND;
835 } else {
836 next_scan_state = SCAN_SET_CHANNEL;
837 }
838
839 local->next_scan_state = next_scan_state;
Helmut Schaa142b9f52009-07-23 13:18:01 +0200840
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200841 *next_delay = 0;
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200842}
843
844static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
845 unsigned long *next_delay)
846{
847 int skip;
848 struct ieee80211_channel *chan;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200849 enum nl80211_bss_scan_width oper_scan_width;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100850 struct cfg80211_scan_request *scan_req;
851
852 scan_req = rcu_dereference_protected(local->scan_req,
853 lockdep_is_held(&local->mtx));
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200854
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200855 skip = 0;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100856 chan = scan_req->channels[local->scan_channel_idx];
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200857
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200858 local->scan_chandef.chan = chan;
859 local->scan_chandef.center_freq1 = chan->center_freq;
860 local->scan_chandef.center_freq2 = 0;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100861 switch (scan_req->scan_width) {
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200862 case NL80211_BSS_CHAN_WIDTH_5:
863 local->scan_chandef.width = NL80211_CHAN_WIDTH_5;
864 break;
865 case NL80211_BSS_CHAN_WIDTH_10:
866 local->scan_chandef.width = NL80211_CHAN_WIDTH_10;
867 break;
868 case NL80211_BSS_CHAN_WIDTH_20:
869 /* If scanning on oper channel, use whatever channel-type
870 * is currently in use.
871 */
872 oper_scan_width = cfg80211_chandef_to_scan_width(
873 &local->_oper_chandef);
874 if (chan == local->_oper_chandef.chan &&
Johannes Berg6ea0a692014-11-19 11:55:49 +0100875 oper_scan_width == scan_req->scan_width)
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200876 local->scan_chandef = local->_oper_chandef;
877 else
878 local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
879 break;
880 }
Ben Greearb23b0252011-02-04 11:54:17 -0800881
Johannes Berg07ef03e2011-11-08 16:21:21 +0100882 if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
883 skip = 1;
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200884
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200885 /* advance state machine to next channel/band */
886 local->scan_channel_idx++;
887
Helmut Schaa0ee9c132009-07-25 17:25:51 +0200888 if (skip) {
889 /* if we skip this channel return to the decision state */
890 local->next_scan_state = SCAN_DECISION;
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200891 return;
Helmut Schaa0ee9c132009-07-25 17:25:51 +0200892 }
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200893
894 /*
895 * Probe delay is used to update the NAV, cf. 11.1.3.2.2
896 * (which unfortunately doesn't say _why_ step a) is done,
897 * but it waits for the probe delay or until a frame is
898 * received - and the received frame would update the NAV).
899 * For now, we do not support waiting until a frame is
900 * received.
901 *
902 * In any case, it is not necessary for a passive scan.
903 */
Antonio Quartulli4e39cca2015-11-21 18:13:40 +0800904 if ((chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) ||
905 !scan_req->n_ssids) {
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200906 *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
Helmut Schaa977923b2009-07-23 12:14:20 +0200907 local->next_scan_state = SCAN_DECISION;
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200908 return;
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200909 }
910
Helmut Schaa2fb3f022009-07-23 12:13:56 +0200911 /* active scan, send probes */
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200912 *next_delay = IEEE80211_PROBE_DELAY;
Helmut Schaa977923b2009-07-23 12:14:20 +0200913 local->next_scan_state = SCAN_SEND_PROBE;
Helmut Schaa7d3be3c2009-07-23 12:13:41 +0200914}
915
Johannes Berg07ef03e2011-11-08 16:21:21 +0100916static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
917 unsigned long *next_delay)
918{
919 /* switch back to the operating channel */
Simon Wunderlich7ca15a02013-07-08 16:55:56 +0200920 local->scan_chandef.chan = NULL;
Johannes Berg07ef03e2011-11-08 16:21:21 +0100921 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
922
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +0100923 /* disable PS */
924 ieee80211_offchannel_return(local);
Johannes Berg07ef03e2011-11-08 16:21:21 +0100925
926 *next_delay = HZ / 5;
927 /* afterwards, resume scan & go to next channel */
928 local->next_scan_state = SCAN_RESUME;
929}
930
931static void ieee80211_scan_state_resume(struct ieee80211_local *local,
932 unsigned long *next_delay)
933{
Stanislaw Gruszkaaacde9e2012-12-20 14:41:18 +0100934 ieee80211_offchannel_stop_vifs(local);
Johannes Berg07ef03e2011-11-08 16:21:21 +0100935
936 if (local->ops->flush) {
Emmanuel Grumbach3b24f4c2015-01-07 15:42:39 +0200937 ieee80211_flush_queues(local, NULL, false);
Johannes Berg07ef03e2011-11-08 16:21:21 +0100938 *next_delay = 0;
939 } else
940 *next_delay = HZ / 10;
941
942 /* remember when we left the operating channel */
943 local->leave_oper_channel_time = jiffies;
944
945 /* advance to the next channel to be scanned */
Mohammed Shafi Shajakhande2ee842011-12-24 18:43:28 +0530946 local->next_scan_state = SCAN_SET_CHANNEL;
Johannes Berg07ef03e2011-11-08 16:21:21 +0100947}
948
Johannes Bergc2b13452008-09-11 00:01:55 +0200949void ieee80211_scan_work(struct work_struct *work)
Johannes Berg0a51b272008-09-08 17:44:25 +0200950{
951 struct ieee80211_local *local =
952 container_of(work, struct ieee80211_local, scan_work.work);
Johannes Bergd07bfd82011-03-07 15:48:41 +0100953 struct ieee80211_sub_if_data *sdata;
Johannes Berg6ea0a692014-11-19 11:55:49 +0100954 struct cfg80211_scan_request *scan_req;
Johannes Berg0a51b272008-09-08 17:44:25 +0200955 unsigned long next_delay = 0;
Eliad Peller8bd2a242013-12-05 11:21:27 +0200956 bool aborted;
Johannes Berg8789d452010-08-26 13:30:26 +0200957
Johannes Berga1699b72010-07-30 16:46:07 +0200958 mutex_lock(&local->mtx);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +0200959
Luciano Coelho332ff7f2015-01-22 23:34:10 +0200960 if (!ieee80211_can_run_worker(local)) {
961 aborted = true;
962 goto out_complete;
963 }
964
Johannes Berge2fd5db2012-07-06 21:39:28 +0200965 sdata = rcu_dereference_protected(local->scan_sdata,
966 lockdep_is_held(&local->mtx));
Johannes Berg6ea0a692014-11-19 11:55:49 +0100967 scan_req = rcu_dereference_protected(local->scan_req,
968 lockdep_is_held(&local->mtx));
Johannes Bergd07bfd82011-03-07 15:48:41 +0100969
Ben Greear8a690672012-04-17 10:54:16 -0700970 /* When scanning on-channel, the first-callback means completed. */
971 if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) {
972 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
973 goto out_complete;
974 }
975
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +0200976 if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
977 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
978 goto out_complete;
Johannes Bergf3b85252009-04-23 16:01:47 +0200979 }
980
Johannes Berg6ea0a692014-11-19 11:55:49 +0100981 if (!sdata || !scan_req)
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +0200982 goto out;
983
Eliad Pellerff5db432014-11-12 10:08:29 +0200984 if (!local->scanning) {
Johannes Bergf3b85252009-04-23 16:01:47 +0200985 int rc;
986
Johannes Berg6ea0a692014-11-19 11:55:49 +0100987 RCU_INIT_POINTER(local->scan_req, NULL);
Monam Agarwal0c2bef462014-03-24 00:51:43 +0530988 RCU_INIT_POINTER(local->scan_sdata, NULL);
Johannes Bergf3b85252009-04-23 16:01:47 +0200989
Johannes Berg6ea0a692014-11-19 11:55:49 +0100990 rc = __ieee80211_start_scan(sdata, scan_req);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +0200991 if (rc) {
Stanislaw Gruszka3aed49e2010-10-06 11:22:12 +0200992 /* need to complete scan in cfg80211 */
Johannes Berg6ea0a692014-11-19 11:55:49 +0100993 rcu_assign_pointer(local->scan_req, scan_req);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +0200994 aborted = true;
995 goto out_complete;
996 } else
997 goto out;
Johannes Bergf3b85252009-04-23 16:01:47 +0200998 }
999
Johannes Berg5bc75722008-09-11 00:01:51 +02001000 /*
Helmut Schaaf502d092009-07-23 12:13:48 +02001001 * as long as no delay is required advance immediately
1002 * without scheduling a new work
1003 */
1004 do {
Rajkumar Manoharanc29acf22011-05-14 09:43:28 +05301005 if (!ieee80211_sdata_running(sdata)) {
1006 aborted = true;
1007 goto out_complete;
1008 }
1009
Helmut Schaa977923b2009-07-23 12:14:20 +02001010 switch (local->next_scan_state) {
Helmut Schaa2fb3f022009-07-23 12:13:56 +02001011 case SCAN_DECISION:
Stanislaw Gruszkae229f842010-10-06 11:22:09 +02001012 /* if no more bands/channels left, complete scan */
Johannes Berg6ea0a692014-11-19 11:55:49 +01001013 if (local->scan_channel_idx >= scan_req->n_channels) {
Stanislaw Gruszkae229f842010-10-06 11:22:09 +02001014 aborted = false;
1015 goto out_complete;
1016 }
1017 ieee80211_scan_state_decision(local, &next_delay);
Helmut Schaaf502d092009-07-23 12:13:48 +02001018 break;
Helmut Schaa2fb3f022009-07-23 12:13:56 +02001019 case SCAN_SET_CHANNEL:
1020 ieee80211_scan_state_set_channel(local, &next_delay);
1021 break;
Helmut Schaaf502d092009-07-23 12:13:48 +02001022 case SCAN_SEND_PROBE:
1023 ieee80211_scan_state_send_probe(local, &next_delay);
1024 break;
Johannes Berg07ef03e2011-11-08 16:21:21 +01001025 case SCAN_SUSPEND:
1026 ieee80211_scan_state_suspend(local, &next_delay);
Helmut Schaa142b9f52009-07-23 13:18:01 +02001027 break;
Johannes Berg07ef03e2011-11-08 16:21:21 +01001028 case SCAN_RESUME:
1029 ieee80211_scan_state_resume(local, &next_delay);
Helmut Schaa142b9f52009-07-23 13:18:01 +02001030 break;
Sam Lefflercd2bb512012-10-11 21:03:35 -07001031 case SCAN_ABORT:
1032 aborted = true;
1033 goto out_complete;
Helmut Schaaf502d092009-07-23 12:13:48 +02001034 }
1035 } while (next_delay == 0);
Johannes Berg0a51b272008-09-08 17:44:25 +02001036
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -04001037 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
Johannes Bergd07bfd82011-03-07 15:48:41 +01001038 goto out;
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001039
1040out_complete:
Eliad Peller8bd2a242013-12-05 11:21:27 +02001041 __ieee80211_scan_completed(&local->hw, aborted);
Stanislaw Gruszka259b62e2010-10-06 11:22:08 +02001042out:
1043 mutex_unlock(&local->mtx);
Johannes Berg0a51b272008-09-08 17:44:25 +02001044}
1045
Johannes Bergc2b13452008-09-11 00:01:55 +02001046int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
Johannes Berg2a519312009-02-10 21:25:55 +01001047 struct cfg80211_scan_request *req)
Johannes Berg0a51b272008-09-08 17:44:25 +02001048{
Johannes Bergf3b85252009-04-23 16:01:47 +02001049 int res;
1050
Johannes Berga1699b72010-07-30 16:46:07 +02001051 mutex_lock(&sdata->local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +02001052 res = __ieee80211_start_scan(sdata, req);
Johannes Berga1699b72010-07-30 16:46:07 +02001053 mutex_unlock(&sdata->local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +02001054
1055 return res;
1056}
1057
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001058int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
1059 const u8 *ssid, u8 ssid_len,
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001060 struct ieee80211_channel **channels,
1061 unsigned int n_channels,
Simon Wunderlich7ca15a02013-07-08 16:55:56 +02001062 enum nl80211_bss_scan_width scan_width)
Johannes Bergf3b85252009-04-23 16:01:47 +02001063{
Johannes Berg0a51b272008-09-08 17:44:25 +02001064 struct ieee80211_local *local = sdata->local;
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001065 int ret = -EBUSY, i, n_ch = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001066 enum nl80211_band band;
Johannes Berg0a51b272008-09-08 17:44:25 +02001067
Johannes Berga1699b72010-07-30 16:46:07 +02001068 mutex_lock(&local->mtx);
Johannes Berg2a519312009-02-10 21:25:55 +01001069
Johannes Bergf3b85252009-04-23 16:01:47 +02001070 /* busy scanning */
1071 if (local->scan_req)
1072 goto unlock;
Johannes Berg2a519312009-02-10 21:25:55 +01001073
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001074 /* fill internal scan request */
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001075 if (!channels) {
1076 int max_n;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001077
Johannes Berg57fbcce2016-04-12 15:56:15 +02001078 for (band = 0; band < NUM_NL80211_BANDS; band++) {
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001079 if (!local->hw.wiphy->bands[band])
1080 continue;
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001081
1082 max_n = local->hw.wiphy->bands[band]->n_channels;
1083 for (i = 0; i < max_n; i++) {
1084 struct ieee80211_channel *tmp_ch =
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001085 &local->hw.wiphy->bands[band]->channels[i];
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001086
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +02001087 if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR |
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001088 IEEE80211_CHAN_DISABLED))
1089 continue;
1090
1091 local->int_scan_req->channels[n_ch] = tmp_ch;
1092 n_ch++;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001093 }
1094 }
1095
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001096 if (WARN_ON_ONCE(n_ch == 0))
1097 goto unlock;
1098
1099 local->int_scan_req->n_channels = n_ch;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001100 } else {
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001101 for (i = 0; i < n_channels; i++) {
1102 if (channels[i]->flags & (IEEE80211_CHAN_NO_IR |
1103 IEEE80211_CHAN_DISABLED))
1104 continue;
1105
1106 local->int_scan_req->channels[n_ch] = channels[i];
1107 n_ch++;
1108 }
1109
1110 if (WARN_ON_ONCE(n_ch == 0))
Stanislaw Gruszka34bcf712012-12-11 10:48:23 +01001111 goto unlock;
1112
Janusz.Dziedzic@tieto.com76bed0f2015-03-20 06:37:00 +01001113 local->int_scan_req->n_channels = n_ch;
Johannes Bergbe4a4b62010-05-03 08:49:48 +02001114 }
1115
1116 local->int_scan_req->ssids = &local->scan_ssid;
1117 local->int_scan_req->n_ssids = 1;
Simon Wunderlich7ca15a02013-07-08 16:55:56 +02001118 local->int_scan_req->scan_width = scan_width;
Johannes Berg5ba63532009-08-07 17:54:07 +02001119 memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
1120 local->int_scan_req->ssids[0].ssid_len = ssid_len;
Johannes Berg2a519312009-02-10 21:25:55 +01001121
Johannes Berg5ba63532009-08-07 17:54:07 +02001122 ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
Johannes Bergf3b85252009-04-23 16:01:47 +02001123 unlock:
Johannes Berga1699b72010-07-30 16:46:07 +02001124 mutex_unlock(&local->mtx);
Johannes Bergf3b85252009-04-23 16:01:47 +02001125 return ret;
Johannes Berg0a51b272008-09-08 17:44:25 +02001126}
Johannes Berg5bb644a2009-05-17 11:40:42 +02001127
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001128/*
1129 * Only call this function when a scan can't be queued -- under RTNL.
1130 */
Johannes Berg5bb644a2009-05-17 11:40:42 +02001131void ieee80211_scan_cancel(struct ieee80211_local *local)
1132{
Johannes Berg5bb644a2009-05-17 11:40:42 +02001133 /*
Eliad Pellerb8564392011-06-13 12:47:30 +03001134 * We are canceling software scan, or deferred scan that was not
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001135 * yet really started (see __ieee80211_start_scan ).
1136 *
1137 * Regarding hardware scan:
1138 * - we can not call __ieee80211_scan_completed() as when
1139 * SCAN_HW_SCANNING bit is set this function change
1140 * local->hw_scan_req to operate on 5G band, what race with
1141 * driver which can use local->hw_scan_req
1142 *
1143 * - we can not cancel scan_work since driver can schedule it
1144 * by ieee80211_scan_completed(..., true) to finish scan
1145 *
Eliad Pellerb8564392011-06-13 12:47:30 +03001146 * Hence we only call the cancel_hw_scan() callback, but the low-level
1147 * driver is still responsible for calling ieee80211_scan_completed()
1148 * after the scan was completed/aborted.
Johannes Berg5bb644a2009-05-17 11:40:42 +02001149 */
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001150
Johannes Berga1699b72010-07-30 16:46:07 +02001151 mutex_lock(&local->mtx);
Eliad Pellerb8564392011-06-13 12:47:30 +03001152 if (!local->scan_req)
1153 goto out;
1154
Emmanuel Grumbacha7540552013-09-16 11:12:07 +03001155 /*
1156 * We have a scan running and the driver already reported completion,
1157 * but the worker hasn't run yet or is stuck on the mutex - mark it as
1158 * cancelled.
1159 */
1160 if (test_bit(SCAN_HW_SCANNING, &local->scanning) &&
1161 test_bit(SCAN_COMPLETED, &local->scanning)) {
1162 set_bit(SCAN_HW_CANCELLED, &local->scanning);
1163 goto out;
1164 }
1165
Eliad Pellerb8564392011-06-13 12:47:30 +03001166 if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
Emmanuel Grumbacha7540552013-09-16 11:12:07 +03001167 /*
1168 * Make sure that __ieee80211_scan_completed doesn't trigger a
1169 * scan on another band.
1170 */
1171 set_bit(SCAN_HW_CANCELLED, &local->scanning);
Eliad Pellerb8564392011-06-13 12:47:30 +03001172 if (local->ops->cancel_hw_scan)
Johannes Berge2fd5db2012-07-06 21:39:28 +02001173 drv_cancel_hw_scan(local,
1174 rcu_dereference_protected(local->scan_sdata,
1175 lockdep_is_held(&local->mtx)));
Eliad Pellerb8564392011-06-13 12:47:30 +03001176 goto out;
Stanislaw Gruszka4136c422010-10-06 11:22:10 +02001177 }
Eliad Pellerb8564392011-06-13 12:47:30 +03001178
1179 /*
1180 * If the work is currently running, it must be blocked on
1181 * the mutex, but we'll set scan_sdata = NULL and it'll
1182 * simply exit once it acquires the mutex.
1183 */
1184 cancel_delayed_work(&local->scan_work);
1185 /* and clean up */
Avraham Stern7947d3e2016-07-05 15:23:12 +03001186 memset(&local->scan_info, 0, sizeof(local->scan_info));
Eliad Peller8bd2a242013-12-05 11:21:27 +02001187 __ieee80211_scan_completed(&local->hw, true);
Eliad Pellerb8564392011-06-13 12:47:30 +03001188out:
Johannes Bergd07bfd82011-03-07 15:48:41 +01001189 mutex_unlock(&local->mtx);
Johannes Berg5bb644a2009-05-17 11:40:42 +02001190}
Luciano Coelho79f460c2011-05-11 17:09:36 +03001191
David Spinadeld43c6b62013-12-08 21:48:57 +02001192int __ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
1193 struct cfg80211_sched_scan_request *req)
Luciano Coelho79f460c2011-05-11 17:09:36 +03001194{
1195 struct ieee80211_local *local = sdata->local;
David Spinadel633e2712014-02-06 16:15:23 +02001196 struct ieee80211_scan_ies sched_scan_ies = {};
Simon Wunderlich2103dec2013-07-08 16:55:53 +02001197 struct cfg80211_chan_def chandef;
David Spinadel633e2712014-02-06 16:15:23 +02001198 int ret, i, iebufsz, num_bands = 0;
Johannes Berg57fbcce2016-04-12 15:56:15 +02001199 u32 rate_masks[NUM_NL80211_BANDS] = {};
David Spinadel633e2712014-02-06 16:15:23 +02001200 u8 bands_used = 0;
1201 u8 *ie;
Johannes Bergb9771d42018-05-28 15:47:41 +02001202 u32 flags = 0;
Johannes Bergc604b9f2012-11-29 12:45:18 +01001203
David Spinadele4dcbb32014-02-11 13:45:41 +02001204 iebufsz = local->scan_ies_len + req->ie_len;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001205
David Spinadeld43c6b62013-12-08 21:48:57 +02001206 lockdep_assert_held(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001207
David Spinadeld43c6b62013-12-08 21:48:57 +02001208 if (!local->ops->sched_scan_start)
1209 return -ENOTSUPP;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001210
Johannes Berg57fbcce2016-04-12 15:56:15 +02001211 for (i = 0; i < NUM_NL80211_BANDS; i++) {
David Spinadel633e2712014-02-06 16:15:23 +02001212 if (local->hw.wiphy->bands[i]) {
1213 bands_used |= BIT(i);
1214 rate_masks[i] = (u32) -1;
1215 num_bands++;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001216 }
Luciano Coelho79f460c2011-05-11 17:09:36 +03001217 }
1218
Johannes Bergb9771d42018-05-28 15:47:41 +02001219 if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
1220 flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
1221
Kees Cook6396bb22018-06-12 14:03:40 -07001222 ie = kcalloc(iebufsz, num_bands, GFP_KERNEL);
David Spinadel633e2712014-02-06 16:15:23 +02001223 if (!ie) {
1224 ret = -ENOMEM;
1225 goto out;
1226 }
1227
1228 ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
1229
Kirtika Ruchandanicd5861b2016-11-23 20:45:49 -08001230 ieee80211_build_preq_ies(local, ie, num_bands * iebufsz,
1231 &sched_scan_ies, req->ie,
Johannes Berg00387f32018-05-28 15:47:38 +02001232 req->ie_len, bands_used, rate_masks, &chandef,
Johannes Bergb9771d42018-05-28 15:47:41 +02001233 flags);
David Spinadel633e2712014-02-06 16:15:23 +02001234
Johannes Berg30dd3ed2012-09-04 19:15:01 +02001235 ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies);
David Spinadeld43c6b62013-12-08 21:48:57 +02001236 if (ret == 0) {
Johannes Berg5260a5b2012-07-06 21:55:11 +02001237 rcu_assign_pointer(local->sched_scan_sdata, sdata);
Johannes Berg6ea0a692014-11-19 11:55:49 +01001238 rcu_assign_pointer(local->sched_scan_req, req);
David Spinadeld43c6b62013-12-08 21:48:57 +02001239 }
Luciano Coelho79f460c2011-05-11 17:09:36 +03001240
David Spinadel633e2712014-02-06 16:15:23 +02001241 kfree(ie);
David Spinadeld43c6b62013-12-08 21:48:57 +02001242
David Spinadel633e2712014-02-06 16:15:23 +02001243out:
David Spinadeld43c6b62013-12-08 21:48:57 +02001244 if (ret) {
1245 /* Clean in case of failure after HW restart or upon resume. */
Monam Agarwal0c2bef462014-03-24 00:51:43 +05301246 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
Johannes Berg6ea0a692014-11-19 11:55:49 +01001247 RCU_INIT_POINTER(local->sched_scan_req, NULL);
David Spinadeld43c6b62013-12-08 21:48:57 +02001248 }
1249
1250 return ret;
1251}
1252
1253int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
1254 struct cfg80211_sched_scan_request *req)
1255{
1256 struct ieee80211_local *local = sdata->local;
1257 int ret;
1258
1259 mutex_lock(&local->mtx);
1260
1261 if (rcu_access_pointer(local->sched_scan_sdata)) {
1262 mutex_unlock(&local->mtx);
1263 return -EBUSY;
1264 }
1265
1266 ret = __ieee80211_request_sched_scan_start(sdata, req);
1267
Johannes Berg5260a5b2012-07-06 21:55:11 +02001268 mutex_unlock(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001269 return ret;
1270}
1271
Eliad Peller0d440ea2015-10-25 10:59:33 +02001272int ieee80211_request_sched_scan_stop(struct ieee80211_local *local)
Luciano Coelho79f460c2011-05-11 17:09:36 +03001273{
Eliad Peller0d440ea2015-10-25 10:59:33 +02001274 struct ieee80211_sub_if_data *sched_scan_sdata;
1275 int ret = -ENOENT;
Luciano Coelho79f460c2011-05-11 17:09:36 +03001276
Johannes Berg5260a5b2012-07-06 21:55:11 +02001277 mutex_lock(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001278
1279 if (!local->ops->sched_scan_stop) {
1280 ret = -ENOTSUPP;
1281 goto out;
1282 }
1283
David Spinadeld43c6b62013-12-08 21:48:57 +02001284 /* We don't want to restart sched scan anymore. */
Johannes Berg6ea0a692014-11-19 11:55:49 +01001285 RCU_INIT_POINTER(local->sched_scan_req, NULL);
David Spinadeld43c6b62013-12-08 21:48:57 +02001286
Eliad Peller0d440ea2015-10-25 10:59:33 +02001287 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
1288 lockdep_is_held(&local->mtx));
1289 if (sched_scan_sdata) {
1290 ret = drv_sched_scan_stop(local, sched_scan_sdata);
Alexander Bondar71228a12014-03-16 10:49:54 +02001291 if (!ret)
Andreea-Cristina Bernatad053a92014-08-22 16:14:49 +03001292 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
Alexander Bondar71228a12014-03-16 10:49:54 +02001293 }
Luciano Coelho79f460c2011-05-11 17:09:36 +03001294out:
Johannes Berg5260a5b2012-07-06 21:55:11 +02001295 mutex_unlock(&local->mtx);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001296
1297 return ret;
1298}
1299
1300void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
1301{
1302 struct ieee80211_local *local = hw_to_local(hw);
1303
1304 trace_api_sched_scan_results(local);
1305
Arend Van Sprielb34939b2017-04-28 13:40:28 +01001306 cfg80211_sched_scan_results(hw->wiphy, 0);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001307}
1308EXPORT_SYMBOL(ieee80211_sched_scan_results);
1309
Johannes Bergf6837ba82014-04-30 14:19:04 +02001310void ieee80211_sched_scan_end(struct ieee80211_local *local)
Luciano Coelho85a99942011-05-12 16:28:29 +03001311{
Luciano Coelho85a99942011-05-12 16:28:29 +03001312 mutex_lock(&local->mtx);
1313
Johannes Berg5260a5b2012-07-06 21:55:11 +02001314 if (!rcu_access_pointer(local->sched_scan_sdata)) {
Luciano Coelho85a99942011-05-12 16:28:29 +03001315 mutex_unlock(&local->mtx);
1316 return;
1317 }
1318
Monam Agarwal0c2bef462014-03-24 00:51:43 +05301319 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
Luciano Coelho85a99942011-05-12 16:28:29 +03001320
David Spinadeld43c6b62013-12-08 21:48:57 +02001321 /* If sched scan was aborted by the driver. */
Johannes Berg6ea0a692014-11-19 11:55:49 +01001322 RCU_INIT_POINTER(local->sched_scan_req, NULL);
David Spinadeld43c6b62013-12-08 21:48:57 +02001323
Luciano Coelho85a99942011-05-12 16:28:29 +03001324 mutex_unlock(&local->mtx);
1325
Arend Van Sprielb34939b2017-04-28 13:40:28 +01001326 cfg80211_sched_scan_stopped(local->hw.wiphy, 0);
Luciano Coelho85a99942011-05-12 16:28:29 +03001327}
1328
Johannes Bergf6837ba82014-04-30 14:19:04 +02001329void ieee80211_sched_scan_stopped_work(struct work_struct *work)
1330{
1331 struct ieee80211_local *local =
1332 container_of(work, struct ieee80211_local,
1333 sched_scan_stopped_work);
1334
1335 ieee80211_sched_scan_end(local);
1336}
1337
Luciano Coelho79f460c2011-05-11 17:09:36 +03001338void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
1339{
1340 struct ieee80211_local *local = hw_to_local(hw);
1341
1342 trace_api_sched_scan_stopped(local);
1343
Eliad Peller2bc533b2016-01-05 16:28:06 +02001344 /*
1345 * this shouldn't really happen, so for simplicity
1346 * simply ignore it, and let mac80211 reconfigure
1347 * the sched scan later on.
1348 */
1349 if (local->in_reconfig)
1350 return;
1351
Johannes Berg18db5942013-11-06 10:34:36 +01001352 schedule_work(&local->sched_scan_stopped_work);
Luciano Coelho79f460c2011-05-11 17:09:36 +03001353}
1354EXPORT_SYMBOL(ieee80211_sched_scan_stopped);