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