net: clear heap allocations for privileged ethtool actions
[firefly-linux-kernel-4.4.55.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  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 /* TODO: figure out how to avoid that the "current BSS" expires */
16
17 #include <linux/wireless.h>
18 #include <linux/if_arp.h>
19 #include <linux/rtnetlink.h>
20 #include <net/mac80211.h>
21
22 #include "ieee80211_i.h"
23 #include "driver-ops.h"
24 #include "mesh.h"
25
26 #define IEEE80211_PROBE_DELAY (HZ / 33)
27 #define IEEE80211_CHANNEL_TIME (HZ / 33)
28 #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 8)
29
30 struct ieee80211_bss *
31 ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
32                      u8 *ssid, u8 ssid_len)
33 {
34         return (void *)cfg80211_get_bss(local->hw.wiphy,
35                                         ieee80211_get_channel(local->hw.wiphy,
36                                                               freq),
37                                         bssid, ssid, ssid_len,
38                                         0, 0);
39 }
40
41 static void ieee80211_rx_bss_free(struct cfg80211_bss *cbss)
42 {
43         struct ieee80211_bss *bss = (void *)cbss;
44
45         kfree(bss_mesh_id(bss));
46         kfree(bss_mesh_cfg(bss));
47 }
48
49 void ieee80211_rx_bss_put(struct ieee80211_local *local,
50                           struct ieee80211_bss *bss)
51 {
52         cfg80211_put_bss((struct cfg80211_bss *)bss);
53 }
54
55 struct ieee80211_bss *
56 ieee80211_bss_info_update(struct ieee80211_local *local,
57                           struct ieee80211_rx_status *rx_status,
58                           struct ieee80211_mgmt *mgmt,
59                           size_t len,
60                           struct ieee802_11_elems *elems,
61                           struct ieee80211_channel *channel,
62                           bool beacon)
63 {
64         struct ieee80211_bss *bss;
65         int clen, srlen;
66         s32 signal = 0;
67
68         if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
69                 signal = rx_status->signal * 100;
70         else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
71                 signal = (rx_status->signal * 100) / local->hw.max_signal;
72
73         bss = (void *)cfg80211_inform_bss_frame(local->hw.wiphy, channel,
74                                                 mgmt, len, signal, GFP_ATOMIC);
75
76         if (!bss)
77                 return NULL;
78
79         bss->cbss.free_priv = ieee80211_rx_bss_free;
80
81         /* save the ERP value so that it is available at association time */
82         if (elems->erp_info && elems->erp_info_len >= 1) {
83                 bss->erp_value = elems->erp_info[0];
84                 bss->has_erp_value = 1;
85         }
86
87         if (elems->tim) {
88                 struct ieee80211_tim_ie *tim_ie =
89                         (struct ieee80211_tim_ie *)elems->tim;
90                 bss->dtim_period = tim_ie->dtim_period;
91         }
92
93         /* set default value for buggy AP/no TIM element */
94         if (bss->dtim_period == 0)
95                 bss->dtim_period = 1;
96
97         /* replace old supported rates if we get new values */
98         srlen = 0;
99         if (elems->supp_rates) {
100                 clen = IEEE80211_MAX_SUPP_RATES;
101                 if (clen > elems->supp_rates_len)
102                         clen = elems->supp_rates_len;
103                 memcpy(bss->supp_rates, elems->supp_rates, clen);
104                 srlen += clen;
105         }
106         if (elems->ext_supp_rates) {
107                 clen = IEEE80211_MAX_SUPP_RATES - srlen;
108                 if (clen > elems->ext_supp_rates_len)
109                         clen = elems->ext_supp_rates_len;
110                 memcpy(bss->supp_rates + srlen, elems->ext_supp_rates, clen);
111                 srlen += clen;
112         }
113         if (srlen)
114                 bss->supp_rates_len = srlen;
115
116         bss->wmm_used = elems->wmm_param || elems->wmm_info;
117
118         if (!beacon)
119                 bss->last_probe_resp = jiffies;
120
121         return bss;
122 }
123
124 ieee80211_rx_result
125 ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
126 {
127         struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
128         struct ieee80211_mgmt *mgmt;
129         struct ieee80211_bss *bss;
130         u8 *elements;
131         struct ieee80211_channel *channel;
132         size_t baselen;
133         int freq;
134         __le16 fc;
135         bool presp, beacon = false;
136         struct ieee802_11_elems elems;
137
138         if (skb->len < 2)
139                 return RX_DROP_UNUSABLE;
140
141         mgmt = (struct ieee80211_mgmt *) skb->data;
142         fc = mgmt->frame_control;
143
144         if (ieee80211_is_ctl(fc))
145                 return RX_CONTINUE;
146
147         if (skb->len < 24)
148                 return RX_DROP_MONITOR;
149
150         presp = ieee80211_is_probe_resp(fc);
151         if (presp) {
152                 /* ignore ProbeResp to foreign address */
153                 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
154                         return RX_DROP_MONITOR;
155
156                 presp = true;
157                 elements = mgmt->u.probe_resp.variable;
158                 baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
159         } else {
160                 beacon = ieee80211_is_beacon(fc);
161                 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
162                 elements = mgmt->u.beacon.variable;
163         }
164
165         if (!presp && !beacon)
166                 return RX_CONTINUE;
167
168         if (baselen > skb->len)
169                 return RX_DROP_MONITOR;
170
171         ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
172
173         if (elems.ds_params && elems.ds_params_len == 1)
174                 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
175         else
176                 freq = rx_status->freq;
177
178         channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
179
180         if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
181                 return RX_DROP_MONITOR;
182
183         bss = ieee80211_bss_info_update(sdata->local, rx_status,
184                                         mgmt, skb->len, &elems,
185                                         channel, beacon);
186         if (bss)
187                 ieee80211_rx_bss_put(sdata->local, bss);
188
189         dev_kfree_skb(skb);
190         return RX_QUEUED;
191 }
192
193 /*
194  * inform AP that we will go to sleep so that it will buffer the frames
195  * while we scan
196  */
197 static void ieee80211_scan_ps_enable(struct ieee80211_sub_if_data *sdata)
198 {
199         struct ieee80211_local *local = sdata->local;
200
201         local->scan_ps_enabled = false;
202
203         /* FIXME: what to do when local->pspolling is true? */
204
205         del_timer_sync(&local->dynamic_ps_timer);
206         cancel_work_sync(&local->dynamic_ps_enable_work);
207
208         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
209                 local->scan_ps_enabled = true;
210                 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
211                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
212         }
213
214         if (!(local->scan_ps_enabled) ||
215             !(local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK))
216                 /*
217                  * If power save was enabled, no need to send a nullfunc
218                  * frame because AP knows that we are sleeping. But if the
219                  * hardware is creating the nullfunc frame for power save
220                  * status (ie. IEEE80211_HW_PS_NULLFUNC_STACK is not
221                  * enabled) and power save was enabled, the firmware just
222                  * sent a null frame with power save disabled. So we need
223                  * to send a new nullfunc frame to inform the AP that we
224                  * are again sleeping.
225                  */
226                 ieee80211_send_nullfunc(local, sdata, 1);
227 }
228
229 /* inform AP that we are awake again, unless power save is enabled */
230 static void ieee80211_scan_ps_disable(struct ieee80211_sub_if_data *sdata)
231 {
232         struct ieee80211_local *local = sdata->local;
233
234         if (!local->ps_sdata)
235                 ieee80211_send_nullfunc(local, sdata, 0);
236         else if (local->scan_ps_enabled) {
237                 /*
238                  * In !IEEE80211_HW_PS_NULLFUNC_STACK case the hardware
239                  * will send a nullfunc frame with the powersave bit set
240                  * even though the AP already knows that we are sleeping.
241                  * This could be avoided by sending a null frame with power
242                  * save bit disabled before enabling the power save, but
243                  * this doesn't gain anything.
244                  *
245                  * When IEEE80211_HW_PS_NULLFUNC_STACK is enabled, no need
246                  * to send a nullfunc frame because AP already knows that
247                  * we are sleeping, let's just enable power save mode in
248                  * hardware.
249                  */
250                 local->hw.conf.flags |= IEEE80211_CONF_PS;
251                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
252         } else if (local->hw.conf.dynamic_ps_timeout > 0) {
253                 /*
254                  * If IEEE80211_CONF_PS was not set and the dynamic_ps_timer
255                  * had been running before leaving the operating channel,
256                  * restart the timer now and send a nullfunc frame to inform
257                  * the AP that we are awake.
258                  */
259                 ieee80211_send_nullfunc(local, sdata, 0);
260                 mod_timer(&local->dynamic_ps_timer, jiffies +
261                           msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
262         }
263 }
264
265 static void ieee80211_restore_scan_ies(struct ieee80211_local *local)
266 {
267         kfree(local->scan_req->ie);
268         local->scan_req->ie = local->orig_ies;
269         local->scan_req->ie_len = local->orig_ies_len;
270 }
271
272 void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
273 {
274         struct ieee80211_local *local = hw_to_local(hw);
275         struct ieee80211_sub_if_data *sdata;
276         bool was_hw_scan;
277
278         mutex_lock(&local->scan_mtx);
279
280         /*
281          * It's ok to abort a not-yet-running scan (that
282          * we have one at all will be verified by checking
283          * local->scan_req next), but not to complete it
284          * successfully.
285          */
286         if (WARN_ON(!local->scanning && !aborted))
287                 aborted = true;
288
289         if (WARN_ON(!local->scan_req)) {
290                 mutex_unlock(&local->scan_mtx);
291                 return;
292         }
293
294         if (test_bit(SCAN_HW_SCANNING, &local->scanning))
295                 ieee80211_restore_scan_ies(local);
296
297         if (local->scan_req != local->int_scan_req)
298                 cfg80211_scan_done(local->scan_req, aborted);
299         local->scan_req = NULL;
300         local->scan_sdata = NULL;
301
302         was_hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
303         local->scanning = 0;
304         local->scan_channel = NULL;
305
306         /* we only have to protect scan_req and hw/sw scan */
307         mutex_unlock(&local->scan_mtx);
308
309         ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
310         if (was_hw_scan)
311                 goto done;
312
313         ieee80211_configure_filter(local);
314
315         drv_sw_scan_complete(local);
316
317         mutex_lock(&local->iflist_mtx);
318         list_for_each_entry(sdata, &local->interfaces, list) {
319                 if (!netif_running(sdata->dev))
320                         continue;
321
322                 /* Tell AP we're back */
323                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
324                         if (sdata->u.mgd.associated) {
325                                 ieee80211_scan_ps_disable(sdata);
326                                 netif_tx_wake_all_queues(sdata->dev);
327                         }
328                 } else
329                         netif_tx_wake_all_queues(sdata->dev);
330
331                 /* re-enable beaconing */
332                 if (sdata->vif.type == NL80211_IFTYPE_AP ||
333                     sdata->vif.type == NL80211_IFTYPE_ADHOC ||
334                     sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
335                         ieee80211_bss_info_change_notify(
336                                 sdata, BSS_CHANGED_BEACON_ENABLED);
337         }
338         mutex_unlock(&local->iflist_mtx);
339
340  done:
341         ieee80211_recalc_idle(local);
342         ieee80211_mlme_notify_scan_completed(local);
343         ieee80211_ibss_notify_scan_completed(local);
344         ieee80211_mesh_notify_scan_completed(local);
345 }
346 EXPORT_SYMBOL(ieee80211_scan_completed);
347
348 static int ieee80211_start_sw_scan(struct ieee80211_local *local)
349 {
350         struct ieee80211_sub_if_data *sdata;
351
352         /*
353          * Hardware/driver doesn't support hw_scan, so use software
354          * scanning instead. First send a nullfunc frame with power save
355          * bit on so that AP will buffer the frames for us while we are not
356          * listening, then send probe requests to each channel and wait for
357          * the responses. After all channels are scanned, tune back to the
358          * original channel and send a nullfunc frame with power save bit
359          * off to trigger the AP to send us all the buffered frames.
360          *
361          * Note that while local->sw_scanning is true everything else but
362          * nullfunc frames and probe requests will be dropped in
363          * ieee80211_tx_h_check_assoc().
364          */
365         drv_sw_scan_start(local);
366
367         mutex_lock(&local->iflist_mtx);
368         list_for_each_entry(sdata, &local->interfaces, list) {
369                 if (!netif_running(sdata->dev))
370                         continue;
371
372                 /* disable beaconing */
373                 if (sdata->vif.type == NL80211_IFTYPE_AP ||
374                     sdata->vif.type == NL80211_IFTYPE_ADHOC ||
375                     sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
376                         ieee80211_bss_info_change_notify(
377                                 sdata, BSS_CHANGED_BEACON_ENABLED);
378
379                 /*
380                  * only handle non-STA interfaces here, STA interfaces
381                  * are handled in the scan state machine
382                  */
383                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
384                         netif_tx_stop_all_queues(sdata->dev);
385         }
386         mutex_unlock(&local->iflist_mtx);
387
388         local->next_scan_state = SCAN_DECISION;
389         local->scan_channel_idx = 0;
390
391         ieee80211_configure_filter(local);
392
393         /* TODO: start scan as soon as all nullfunc frames are ACKed */
394         ieee80211_queue_delayed_work(&local->hw,
395                                      &local->scan_work,
396                                      IEEE80211_CHANNEL_TIME);
397
398         return 0;
399 }
400
401
402 static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
403                                   struct cfg80211_scan_request *req)
404 {
405         struct ieee80211_local *local = sdata->local;
406         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
407         int rc;
408
409         if (local->scan_req)
410                 return -EBUSY;
411
412         if (req != local->int_scan_req &&
413             sdata->vif.type == NL80211_IFTYPE_STATION &&
414             !list_empty(&ifmgd->work_list)) {
415                 /* actually wait for the work it's doing to finish/time out */
416                 set_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request);
417                 local->scan_req = req;
418                 local->scan_sdata = sdata;
419                 return 0;
420         }
421
422         if (local->ops->hw_scan) {
423                 u8 *ies;
424                 int ielen;
425
426                 ies = kmalloc(2 + IEEE80211_MAX_SSID_LEN +
427                               local->scan_ies_len + req->ie_len, GFP_KERNEL);
428                 if (!ies)
429                         return -ENOMEM;
430
431                 ielen = ieee80211_build_preq_ies(local, ies,
432                                                  req->ie, req->ie_len);
433                 local->orig_ies = req->ie;
434                 local->orig_ies_len = req->ie_len;
435                 req->ie = ies;
436                 req->ie_len = ielen;
437         }
438
439         local->scan_req = req;
440         local->scan_sdata = sdata;
441
442         if (local->ops->hw_scan)
443                 __set_bit(SCAN_HW_SCANNING, &local->scanning);
444         else
445                 __set_bit(SCAN_SW_SCANNING, &local->scanning);
446         /*
447          * Kicking off the scan need not be protected,
448          * only the scan variable stuff, since now
449          * local->scan_req is assigned and other callers
450          * will abort their scan attempts.
451          *
452          * This avoids getting a scan_mtx -> iflist_mtx
453          * dependency, so that the scan completed calls
454          * have more locking freedom.
455          */
456
457         ieee80211_recalc_idle(local);
458         mutex_unlock(&local->scan_mtx);
459
460         if (local->ops->hw_scan)
461                 rc = drv_hw_scan(local, local->scan_req);
462         else
463                 rc = ieee80211_start_sw_scan(local);
464
465         mutex_lock(&local->scan_mtx);
466
467         if (rc) {
468                 if (local->ops->hw_scan)
469                         ieee80211_restore_scan_ies(local);
470                 local->scanning = 0;
471
472                 ieee80211_recalc_idle(local);
473
474                 local->scan_req = NULL;
475                 local->scan_sdata = NULL;
476         }
477
478         return rc;
479 }
480
481 static int ieee80211_scan_state_decision(struct ieee80211_local *local,
482                                          unsigned long *next_delay)
483 {
484         bool associated = false;
485         struct ieee80211_sub_if_data *sdata;
486
487         /* if no more bands/channels left, complete scan and advance to the idle state */
488         if (local->scan_channel_idx >= local->scan_req->n_channels) {
489                 ieee80211_scan_completed(&local->hw, false);
490                 return 1;
491         }
492
493         /* check if at least one STA interface is associated */
494         mutex_lock(&local->iflist_mtx);
495         list_for_each_entry(sdata, &local->interfaces, list) {
496                 if (!netif_running(sdata->dev))
497                         continue;
498
499                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
500                         if (sdata->u.mgd.associated) {
501                                 associated = true;
502                                 break;
503                         }
504                 }
505         }
506         mutex_unlock(&local->iflist_mtx);
507
508         if (local->scan_channel) {
509                 /*
510                  * we're currently scanning a different channel, let's
511                  * switch back to the operating channel now if at least
512                  * one interface is associated. Otherwise just scan the
513                  * next channel
514                  */
515                 if (associated)
516                         local->next_scan_state = SCAN_ENTER_OPER_CHANNEL;
517                 else
518                         local->next_scan_state = SCAN_SET_CHANNEL;
519         } else {
520                 /*
521                  * we're on the operating channel currently, let's
522                  * leave that channel now to scan another one
523                  */
524                 local->next_scan_state = SCAN_LEAVE_OPER_CHANNEL;
525         }
526
527         *next_delay = 0;
528         return 0;
529 }
530
531 static void ieee80211_scan_state_leave_oper_channel(struct ieee80211_local *local,
532                                                     unsigned long *next_delay)
533 {
534         struct ieee80211_sub_if_data *sdata;
535
536         /*
537          * notify the AP about us leaving the channel and stop all STA interfaces
538          */
539         mutex_lock(&local->iflist_mtx);
540         list_for_each_entry(sdata, &local->interfaces, list) {
541                 if (!netif_running(sdata->dev))
542                         continue;
543
544                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
545                         netif_tx_stop_all_queues(sdata->dev);
546                         if (sdata->u.mgd.associated)
547                                 ieee80211_scan_ps_enable(sdata);
548                 }
549         }
550         mutex_unlock(&local->iflist_mtx);
551
552         __set_bit(SCAN_OFF_CHANNEL, &local->scanning);
553
554         /* advance to the next channel to be scanned */
555         *next_delay = HZ / 10;
556         local->next_scan_state = SCAN_SET_CHANNEL;
557 }
558
559 static void ieee80211_scan_state_enter_oper_channel(struct ieee80211_local *local,
560                                                     unsigned long *next_delay)
561 {
562         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
563
564         /* switch back to the operating channel */
565         local->scan_channel = NULL;
566         ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
567
568         /*
569          * notify the AP about us being back and restart all STA interfaces
570          */
571         mutex_lock(&local->iflist_mtx);
572         list_for_each_entry(sdata, &local->interfaces, list) {
573                 if (!netif_running(sdata->dev))
574                         continue;
575
576                 /* Tell AP we're back */
577                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
578                         if (sdata->u.mgd.associated)
579                                 ieee80211_scan_ps_disable(sdata);
580                         netif_tx_wake_all_queues(sdata->dev);
581                 }
582         }
583         mutex_unlock(&local->iflist_mtx);
584
585         __clear_bit(SCAN_OFF_CHANNEL, &local->scanning);
586
587         *next_delay = HZ / 5;
588         local->next_scan_state = SCAN_DECISION;
589 }
590
591 static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
592                                              unsigned long *next_delay)
593 {
594         int skip;
595         struct ieee80211_channel *chan;
596         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
597
598         skip = 0;
599         chan = local->scan_req->channels[local->scan_channel_idx];
600
601         if (chan->flags & IEEE80211_CHAN_DISABLED ||
602             (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
603              chan->flags & IEEE80211_CHAN_NO_IBSS))
604                 skip = 1;
605
606         if (!skip) {
607                 local->scan_channel = chan;
608                 if (ieee80211_hw_config(local,
609                                         IEEE80211_CONF_CHANGE_CHANNEL))
610                         skip = 1;
611         }
612
613         /* advance state machine to next channel/band */
614         local->scan_channel_idx++;
615
616         if (skip) {
617                 /* if we skip this channel return to the decision state */
618                 local->next_scan_state = SCAN_DECISION;
619                 return;
620         }
621
622         /*
623          * Probe delay is used to update the NAV, cf. 11.1.3.2.2
624          * (which unfortunately doesn't say _why_ step a) is done,
625          * but it waits for the probe delay or until a frame is
626          * received - and the received frame would update the NAV).
627          * For now, we do not support waiting until a frame is
628          * received.
629          *
630          * In any case, it is not necessary for a passive scan.
631          */
632         if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
633             !local->scan_req->n_ssids) {
634                 *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
635                 local->next_scan_state = SCAN_DECISION;
636                 return;
637         }
638
639         /* active scan, send probes */
640         *next_delay = IEEE80211_PROBE_DELAY;
641         local->next_scan_state = SCAN_SEND_PROBE;
642 }
643
644 static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
645                                             unsigned long *next_delay)
646 {
647         int i;
648         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
649
650         for (i = 0; i < local->scan_req->n_ssids; i++)
651                 ieee80211_send_probe_req(
652                         sdata, NULL,
653                         local->scan_req->ssids[i].ssid,
654                         local->scan_req->ssids[i].ssid_len,
655                         local->scan_req->ie, local->scan_req->ie_len);
656
657         /*
658          * After sending probe requests, wait for probe responses
659          * on the channel.
660          */
661         *next_delay = IEEE80211_CHANNEL_TIME;
662         local->next_scan_state = SCAN_DECISION;
663 }
664
665 void ieee80211_scan_work(struct work_struct *work)
666 {
667         struct ieee80211_local *local =
668                 container_of(work, struct ieee80211_local, scan_work.work);
669         struct ieee80211_sub_if_data *sdata = local->scan_sdata;
670         unsigned long next_delay = 0;
671
672         mutex_lock(&local->scan_mtx);
673         if (!sdata || !local->scan_req) {
674                 mutex_unlock(&local->scan_mtx);
675                 return;
676         }
677
678         if (local->scan_req && !local->scanning) {
679                 struct cfg80211_scan_request *req = local->scan_req;
680                 int rc;
681
682                 local->scan_req = NULL;
683                 local->scan_sdata = NULL;
684
685                 rc = __ieee80211_start_scan(sdata, req);
686                 mutex_unlock(&local->scan_mtx);
687
688                 if (rc)
689                         ieee80211_scan_completed(&local->hw, true);
690                 return;
691         }
692
693         mutex_unlock(&local->scan_mtx);
694
695         /*
696          * Avoid re-scheduling when the sdata is going away.
697          */
698         if (!netif_running(sdata->dev)) {
699                 ieee80211_scan_completed(&local->hw, true);
700                 return;
701         }
702
703         /*
704          * as long as no delay is required advance immediately
705          * without scheduling a new work
706          */
707         do {
708                 switch (local->next_scan_state) {
709                 case SCAN_DECISION:
710                         if (ieee80211_scan_state_decision(local, &next_delay))
711                                 return;
712                         break;
713                 case SCAN_SET_CHANNEL:
714                         ieee80211_scan_state_set_channel(local, &next_delay);
715                         break;
716                 case SCAN_SEND_PROBE:
717                         ieee80211_scan_state_send_probe(local, &next_delay);
718                         break;
719                 case SCAN_LEAVE_OPER_CHANNEL:
720                         ieee80211_scan_state_leave_oper_channel(local, &next_delay);
721                         break;
722                 case SCAN_ENTER_OPER_CHANNEL:
723                         ieee80211_scan_state_enter_oper_channel(local, &next_delay);
724                         break;
725                 }
726         } while (next_delay == 0);
727
728         ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
729 }
730
731 int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
732                            struct cfg80211_scan_request *req)
733 {
734         int res;
735
736         mutex_lock(&sdata->local->scan_mtx);
737         res = __ieee80211_start_scan(sdata, req);
738         mutex_unlock(&sdata->local->scan_mtx);
739
740         return res;
741 }
742
743 int ieee80211_request_internal_scan(struct ieee80211_sub_if_data *sdata,
744                                     const u8 *ssid, u8 ssid_len)
745 {
746         struct ieee80211_local *local = sdata->local;
747         int ret = -EBUSY;
748
749         mutex_lock(&local->scan_mtx);
750
751         /* busy scanning */
752         if (local->scan_req)
753                 goto unlock;
754
755         memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
756         local->int_scan_req->ssids[0].ssid_len = ssid_len;
757
758         ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
759  unlock:
760         mutex_unlock(&local->scan_mtx);
761         return ret;
762 }
763
764 void ieee80211_scan_cancel(struct ieee80211_local *local)
765 {
766         bool abortscan;
767
768         cancel_delayed_work_sync(&local->scan_work);
769
770         /*
771          * Only call this function when a scan can't be
772          * queued -- mostly at suspend under RTNL.
773          */
774         mutex_lock(&local->scan_mtx);
775         abortscan = test_bit(SCAN_SW_SCANNING, &local->scanning) ||
776                     (!local->scanning && local->scan_req);
777         mutex_unlock(&local->scan_mtx);
778
779         if (abortscan)
780                 ieee80211_scan_completed(&local->hw, true);
781 }