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