OSDN Git Service

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