mac82011: use frame control to differentiate probe resp/beacon
[deliverable/linux.git] / net / mac80211 / scan.c
CommitLineData
0a51b27e 1/*
5484e237
JB
2 * Scanning implementation
3 *
0a51b27e
JB
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
0a51b27e 15#include <linux/if_arp.h>
888d04df 16#include <linux/etherdevice.h>
078e1e60 17#include <linux/rtnetlink.h>
e8db0be1 18#include <linux/pm_qos.h>
df13cce5 19#include <net/sch_generic.h>
5a0e3ad6 20#include <linux/slab.h>
bc3b2d7f 21#include <linux/export.h>
0a51b27e 22#include <net/mac80211.h>
0a51b27e
JB
23
24#include "ieee80211_i.h"
24487981 25#include "driver-ops.h"
5484e237 26#include "mesh.h"
0a51b27e
JB
27
28#define IEEE80211_PROBE_DELAY (HZ / 33)
29#define IEEE80211_CHANNEL_TIME (HZ / 33)
96f7e739 30#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 8)
0a51b27e 31
00d3f14c 32static void ieee80211_rx_bss_free(struct cfg80211_bss *cbss)
5484e237 33{
0c1ad2ca 34 struct ieee80211_bss *bss = (void *)cbss->priv;
5484e237 35
5484e237
JB
36 kfree(bss_mesh_id(bss));
37 kfree(bss_mesh_cfg(bss));
5484e237
JB
38}
39
40void ieee80211_rx_bss_put(struct ieee80211_local *local,
c2b13452 41 struct ieee80211_bss *bss)
5484e237 42{
0c1ad2ca
JB
43 if (!bss)
44 return;
45 cfg80211_put_bss(container_of((void *)bss, struct cfg80211_bss, priv));
5484e237
JB
46}
47
ab13315a
KV
48static bool is_uapsd_supported(struct ieee802_11_elems *elems)
49{
50 u8 qos_info;
51
52 if (elems->wmm_info && elems->wmm_info_len == 7
53 && elems->wmm_info[5] == 1)
54 qos_info = elems->wmm_info[6];
55 else if (elems->wmm_param && elems->wmm_param_len == 24
56 && elems->wmm_param[5] == 1)
57 qos_info = elems->wmm_param[6];
58 else
59 /* no valid wmm information or parameter element found */
60 return false;
61
62 return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD;
63}
64
c2b13452 65struct ieee80211_bss *
5484e237
JB
66ieee80211_bss_info_update(struct ieee80211_local *local,
67 struct ieee80211_rx_status *rx_status,
d45c4172 68 struct ieee80211_mgmt *mgmt, size_t len,
5484e237 69 struct ieee802_11_elems *elems,
d45c4172 70 struct ieee80211_channel *channel)
5484e237 71{
d45c4172 72 bool beacon = ieee80211_is_beacon(mgmt->frame_control);
0c1ad2ca 73 struct cfg80211_bss *cbss;
c2b13452 74 struct ieee80211_bss *bss;
f0b058b6 75 int clen, srlen;
2a519311
JB
76 s32 signal = 0;
77
77965c97 78 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
2a519311 79 signal = rx_status->signal * 100;
77965c97 80 else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
2a519311 81 signal = (rx_status->signal * 100) / local->hw.max_signal;
2a519311 82
0c1ad2ca
JB
83 cbss = cfg80211_inform_bss_frame(local->hw.wiphy, channel,
84 mgmt, len, signal, GFP_ATOMIC);
0c1ad2ca 85 if (!cbss)
00d3f14c
JB
86 return NULL;
87
0c1ad2ca
JB
88 cbss->free_priv = ieee80211_rx_bss_free;
89 bss = (void *)cbss->priv;
5484e237 90
8c358bcd
JB
91 bss->device_ts = rx_status->device_timestamp;
92
fcff4f10
PS
93 if (elems->parse_error) {
94 if (beacon)
95 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON;
96 else
97 bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP;
98 } else {
99 if (beacon)
100 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON;
101 else
102 bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP;
103 }
104
5484e237 105 /* save the ERP value so that it is available at association time */
fcff4f10
PS
106 if (elems->erp_info && elems->erp_info_len >= 1 &&
107 (!elems->parse_error ||
108 !(bss->valid_data & IEEE80211_BSS_VALID_ERP))) {
5484e237 109 bss->erp_value = elems->erp_info[0];
3db1cd5c 110 bss->has_erp_value = true;
fcff4f10
PS
111 if (!elems->parse_error)
112 bss->valid_data |= IEEE80211_BSS_VALID_ERP;
5484e237
JB
113 }
114
f0b058b6 115 /* replace old supported rates if we get new values */
fcff4f10
PS
116 if (!elems->parse_error ||
117 !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) {
118 srlen = 0;
119 if (elems->supp_rates) {
120 clen = IEEE80211_MAX_SUPP_RATES;
121 if (clen > elems->supp_rates_len)
122 clen = elems->supp_rates_len;
123 memcpy(bss->supp_rates, elems->supp_rates, clen);
124 srlen += clen;
125 }
126 if (elems->ext_supp_rates) {
127 clen = IEEE80211_MAX_SUPP_RATES - srlen;
128 if (clen > elems->ext_supp_rates_len)
129 clen = elems->ext_supp_rates_len;
130 memcpy(bss->supp_rates + srlen, elems->ext_supp_rates,
131 clen);
132 srlen += clen;
133 }
134 if (srlen) {
135 bss->supp_rates_len = srlen;
136 if (!elems->parse_error)
137 bss->valid_data |= IEEE80211_BSS_VALID_RATES;
138 }
5484e237 139 }
5484e237 140
fcff4f10
PS
141 if (!elems->parse_error ||
142 !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) {
143 bss->wmm_used = elems->wmm_param || elems->wmm_info;
144 bss->uapsd_supported = is_uapsd_supported(elems);
145 if (!elems->parse_error)
146 bss->valid_data |= IEEE80211_BSS_VALID_WMM;
147 }
5484e237
JB
148
149 if (!beacon)
150 bss->last_probe_resp = jiffies;
151
5484e237
JB
152 return bss;
153}
0a51b27e 154
d48b2968 155void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb)
98c8fccf 156{
f1d58c25 157 struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
d48b2968
JB
158 struct ieee80211_sub_if_data *sdata1, *sdata2;
159 struct ieee80211_mgmt *mgmt = (void *)skb->data;
c2b13452 160 struct ieee80211_bss *bss;
98c8fccf
JB
161 u8 *elements;
162 struct ieee80211_channel *channel;
163 size_t baselen;
d48b2968 164 bool beacon;
98c8fccf
JB
165 struct ieee802_11_elems elems;
166
d48b2968
JB
167 if (skb->len < 24 ||
168 (!ieee80211_is_probe_resp(mgmt->frame_control) &&
169 !ieee80211_is_beacon(mgmt->frame_control)))
170 return;
98c8fccf 171
d48b2968
JB
172 sdata1 = rcu_dereference(local->scan_sdata);
173 sdata2 = rcu_dereference(local->sched_scan_sdata);
98c8fccf 174
d48b2968
JB
175 if (likely(!sdata1 && !sdata2))
176 return;
98c8fccf 177
d48b2968 178 if (ieee80211_is_probe_resp(mgmt->frame_control)) {
98c8fccf 179 /* ignore ProbeResp to foreign address */
d48b2968
JB
180 if ((!sdata1 || !ether_addr_equal(mgmt->da, sdata1->vif.addr)) &&
181 (!sdata2 || !ether_addr_equal(mgmt->da, sdata2->vif.addr)))
182 return;
98c8fccf 183
98c8fccf
JB
184 elements = mgmt->u.probe_resp.variable;
185 baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
d48b2968 186 beacon = false;
98c8fccf 187 } else {
98c8fccf
JB
188 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
189 elements = mgmt->u.beacon.variable;
d48b2968 190 beacon = true;
98c8fccf
JB
191 }
192
98c8fccf 193 if (baselen > skb->len)
d48b2968 194 return;
98c8fccf
JB
195
196 ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
197
0172bb75 198 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
98c8fccf
JB
199
200 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
d48b2968 201 return;
98c8fccf 202
d48b2968 203 bss = ieee80211_bss_info_update(local, rx_status,
98c8fccf 204 mgmt, skb->len, &elems,
d45c4172 205 channel);
d048e503 206 if (bss)
d48b2968 207 ieee80211_rx_bss_put(local, bss);
98c8fccf
JB
208}
209
4d36ec58
JB
210/* return false if no more work */
211static bool ieee80211_prep_hw_scan(struct ieee80211_local *local)
212{
213 struct cfg80211_scan_request *req = local->scan_req;
214 enum ieee80211_band band;
215 int i, ielen, n_chans;
216
217 do {
218 if (local->hw_scan_band == IEEE80211_NUM_BANDS)
219 return false;
220
221 band = local->hw_scan_band;
222 n_chans = 0;
223 for (i = 0; i < req->n_channels; i++) {
224 if (req->channels[i]->band == band) {
225 local->hw_scan_req->channels[n_chans] =
226 req->channels[i];
227 n_chans++;
228 }
229 }
230
231 local->hw_scan_band++;
232 } while (!n_chans);
233
234 local->hw_scan_req->n_channels = n_chans;
235
236 ielen = ieee80211_build_preq_ies(local, (u8 *)local->hw_scan_req->ie,
c604b9f2 237 local->hw_scan_ies_bufsize,
8ee31080 238 req->ie, req->ie_len, band,
85a237fe 239 req->rates[band], 0);
4d36ec58 240 local->hw_scan_req->ie_len = ielen;
dcd83976 241 local->hw_scan_req->no_cck = req->no_cck;
4d36ec58
JB
242
243 return true;
244}
245
d07bfd8b 246static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted,
e229f844 247 bool was_hw_scan)
0a51b27e
JB
248{
249 struct ieee80211_local *local = hw_to_local(hw);
0a51b27e 250
e229f844 251 lockdep_assert_held(&local->mtx);
5bc75728 252
6d3560d4
JB
253 /*
254 * It's ok to abort a not-yet-running scan (that
255 * we have one at all will be verified by checking
256 * local->scan_req next), but not to complete it
257 * successfully.
258 */
259 if (WARN_ON(!local->scanning && !aborted))
260 aborted = true;
5bc75728 261
e229f844 262 if (WARN_ON(!local->scan_req))
d07bfd8b 263 return;
de95a54b 264
4d36ec58 265 if (was_hw_scan && !aborted && ieee80211_prep_hw_scan(local)) {
e2fd5dbc
JB
266 int rc;
267
268 rc = drv_hw_scan(local,
269 rcu_dereference_protected(local->scan_sdata,
270 lockdep_is_held(&local->mtx)),
271 local->hw_scan_req);
272
6eb11a9a 273 if (rc == 0)
d07bfd8b 274 return;
4d36ec58
JB
275 }
276
277 kfree(local->hw_scan_req);
278 local->hw_scan_req = NULL;
f3b85252 279
5ba63533 280 if (local->scan_req != local->int_scan_req)
2a519311
JB
281 cfg80211_scan_done(local->scan_req, aborted);
282 local->scan_req = NULL;
3aa569c3 283 rcu_assign_pointer(local->scan_sdata, NULL);
2a519311 284
fbe9c429 285 local->scanning = 0;
58905ca5 286 local->scan_channel = NULL;
f3b85252 287
07ef03ee
JB
288 /* Set power back to normal operating levels. */
289 ieee80211_hw_config(local, 0);
a0daa0e7 290
e229f844
SG
291 if (!was_hw_scan) {
292 ieee80211_configure_filter(local);
293 drv_sw_scan_complete(local);
e76aadc5 294 ieee80211_offchannel_return(local, true);
e229f844 295 }
0a51b27e 296
5cff20e6 297 ieee80211_recalc_idle(local);
e229f844 298
0a51b27e 299 ieee80211_mlme_notify_scan_completed(local);
46900298 300 ieee80211_ibss_notify_scan_completed(local);
472dbc45 301 ieee80211_mesh_notify_scan_completed(local);
2eb278e0 302 ieee80211_start_next_roc(local);
0a51b27e 303}
8789d459
JB
304
305void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
306{
307 struct ieee80211_local *local = hw_to_local(hw);
308
309 trace_api_scan_completed(local, aborted);
310
311 set_bit(SCAN_COMPLETED, &local->scanning);
312 if (aborted)
313 set_bit(SCAN_ABORTED, &local->scanning);
314 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
315}
0a51b27e
JB
316EXPORT_SYMBOL(ieee80211_scan_completed);
317
f3b85252
JB
318static int ieee80211_start_sw_scan(struct ieee80211_local *local)
319{
fe57d9f5
JB
320 /* Software scan is not supported in multi-channel cases */
321 if (local->use_chanctx)
322 return -EOPNOTSUPP;
323
f3b85252
JB
324 /*
325 * Hardware/driver doesn't support hw_scan, so use software
326 * scanning instead. First send a nullfunc frame with power save
327 * bit on so that AP will buffer the frames for us while we are not
328 * listening, then send probe requests to each channel and wait for
329 * the responses. After all channels are scanned, tune back to the
330 * original channel and send a nullfunc frame with power save bit
331 * off to trigger the AP to send us all the buffered frames.
332 *
333 * Note that while local->sw_scanning is true everything else but
334 * nullfunc frames and probe requests will be dropped in
335 * ieee80211_tx_h_check_assoc().
336 */
24487981 337 drv_sw_scan_start(local);
f3b85252 338
de312db3 339 local->leave_oper_channel_time = jiffies;
977923b0 340 local->next_scan_state = SCAN_DECISION;
f3b85252
JB
341 local->scan_channel_idx = 0;
342
07ef03ee 343 ieee80211_offchannel_stop_vifs(local, true);
a80f7c0b 344
3ac64bee 345 ieee80211_configure_filter(local);
f3b85252 346
59bdf3b0
BG
347 /* We need to set power level at maximum rate for scanning. */
348 ieee80211_hw_config(local, 0);
349
42935eca 350 ieee80211_queue_delayed_work(&local->hw,
07ef03ee 351 &local->scan_work, 0);
f3b85252
JB
352
353 return 0;
354}
355
133d40f9
SG
356static bool ieee80211_can_scan(struct ieee80211_local *local,
357 struct ieee80211_sub_if_data *sdata)
358{
2eb278e0 359 if (!list_empty(&local->roc_list))
133d40f9
SG
360 return false;
361
362 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
363 sdata->u.mgd.flags & (IEEE80211_STA_BEACON_POLL |
364 IEEE80211_STA_CONNECTION_POLL))
365 return false;
366
367 return true;
368}
369
370void ieee80211_run_deferred_scan(struct ieee80211_local *local)
371{
372 lockdep_assert_held(&local->mtx);
373
374 if (!local->scan_req || local->scanning)
375 return;
376
e2fd5dbc
JB
377 if (!ieee80211_can_scan(local,
378 rcu_dereference_protected(
379 local->scan_sdata,
380 lockdep_is_held(&local->mtx))))
133d40f9
SG
381 return;
382
383 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
384 round_jiffies_relative(0));
385}
f3b85252 386
8a690674
BG
387static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
388 unsigned long *next_delay)
389{
390 int i;
e2fd5dbc 391 struct ieee80211_sub_if_data *sdata;
8a690674
BG
392 enum ieee80211_band band = local->hw.conf.channel->band;
393
e2fd5dbc 394 sdata = rcu_dereference_protected(local->scan_sdata,
316b6b5d 395 lockdep_is_held(&local->mtx));
e2fd5dbc 396
8a690674
BG
397 for (i = 0; i < local->scan_req->n_ssids; i++)
398 ieee80211_send_probe_req(
399 sdata, NULL,
400 local->scan_req->ssids[i].ssid,
401 local->scan_req->ssids[i].ssid_len,
402 local->scan_req->ie, local->scan_req->ie_len,
403 local->scan_req->rates[band], false,
fe94fe05 404 local->scan_req->no_cck,
55de908a 405 local->hw.conf.channel, true);
8a690674
BG
406
407 /*
408 * After sending probe requests, wait for probe responses
409 * on the channel.
410 */
411 *next_delay = IEEE80211_CHANNEL_TIME;
412 local->next_scan_state = SCAN_DECISION;
413}
414
f3b85252
JB
415static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
416 struct cfg80211_scan_request *req)
417{
418 struct ieee80211_local *local = sdata->local;
f3b85252
JB
419 int rc;
420
e229f844
SG
421 lockdep_assert_held(&local->mtx);
422
f3b85252
JB
423 if (local->scan_req)
424 return -EBUSY;
425
133d40f9 426 if (!ieee80211_can_scan(local, sdata)) {
6e7e6213 427 /* wait for the work to finish/time out */
c0ce77b8 428 local->scan_req = req;
e2fd5dbc 429 rcu_assign_pointer(local->scan_sdata, sdata);
c0ce77b8
JB
430 return 0;
431 }
432
f3b85252
JB
433 if (local->ops->hw_scan) {
434 u8 *ies;
f3b85252 435
c604b9f2
JB
436 local->hw_scan_ies_bufsize = 2 + IEEE80211_MAX_SSID_LEN +
437 local->scan_ies_len +
438 req->ie_len;
4d36ec58
JB
439 local->hw_scan_req = kmalloc(
440 sizeof(*local->hw_scan_req) +
441 req->n_channels * sizeof(req->channels[0]) +
c604b9f2 442 local->hw_scan_ies_bufsize, GFP_KERNEL);
4d36ec58 443 if (!local->hw_scan_req)
f3b85252
JB
444 return -ENOMEM;
445
4d36ec58
JB
446 local->hw_scan_req->ssids = req->ssids;
447 local->hw_scan_req->n_ssids = req->n_ssids;
448 ies = (u8 *)local->hw_scan_req +
449 sizeof(*local->hw_scan_req) +
450 req->n_channels * sizeof(req->channels[0]);
451 local->hw_scan_req->ie = ies;
cd2bb512 452 local->hw_scan_req->flags = req->flags;
4d36ec58
JB
453
454 local->hw_scan_band = 0;
6e7e6213
JL
455
456 /*
457 * After allocating local->hw_scan_req, we must
458 * go through until ieee80211_prep_hw_scan(), so
459 * anything that might be changed here and leave
460 * this function early must not go after this
461 * allocation.
462 */
f3b85252
JB
463 }
464
465 local->scan_req = req;
e2fd5dbc 466 rcu_assign_pointer(local->scan_sdata, sdata);
f3b85252 467
8a690674 468 if (local->ops->hw_scan) {
fbe9c429 469 __set_bit(SCAN_HW_SCANNING, &local->scanning);
8a690674 470 } else if ((req->n_channels == 1) &&
55de908a 471 (req->channels[0] == local->_oper_channel)) {
9b864870
JB
472 /*
473 * If we are scanning only on the operating channel
474 * then we do not need to stop normal activities
8a690674
BG
475 */
476 unsigned long next_delay;
477
478 __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
479
480 ieee80211_recalc_idle(local);
481
482 /* Notify driver scan is starting, keep order of operations
483 * same as normal software scan, in case that matters. */
484 drv_sw_scan_start(local);
485
486 ieee80211_configure_filter(local); /* accept probe-responses */
487
488 /* We need to ensure power level is at max for scanning. */
489 ieee80211_hw_config(local, 0);
490
491 if ((req->channels[0]->flags &
492 IEEE80211_CHAN_PASSIVE_SCAN) ||
493 !local->scan_req->n_ssids) {
494 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
495 } else {
496 ieee80211_scan_state_send_probe(local, &next_delay);
497 next_delay = IEEE80211_CHANNEL_TIME;
498 }
499
500 /* Now, just wait a bit and we are all done! */
501 ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
502 next_delay);
503 return 0;
504 } else {
505 /* Do normal software scan */
fbe9c429 506 __set_bit(SCAN_SW_SCANNING, &local->scanning);
8a690674 507 }
6e7e6213 508
5cff20e6 509 ieee80211_recalc_idle(local);
f3b85252 510
4d36ec58
JB
511 if (local->ops->hw_scan) {
512 WARN_ON(!ieee80211_prep_hw_scan(local));
a060bbfe 513 rc = drv_hw_scan(local, sdata, local->hw_scan_req);
4d36ec58 514 } else
f3b85252
JB
515 rc = ieee80211_start_sw_scan(local);
516
f3b85252 517 if (rc) {
4d36ec58
JB
518 kfree(local->hw_scan_req);
519 local->hw_scan_req = NULL;
fbe9c429 520 local->scanning = 0;
f3b85252 521
5cff20e6
JB
522 ieee80211_recalc_idle(local);
523
f3b85252 524 local->scan_req = NULL;
e2fd5dbc 525 rcu_assign_pointer(local->scan_sdata, NULL);
f3b85252
JB
526 }
527
528 return rc;
529}
530
df13cce5
HS
531static unsigned long
532ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
533{
534 /*
535 * TODO: channel switching also consumes quite some time,
536 * add that delay as well to get a better estimation
537 */
538 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
539 return IEEE80211_PASSIVE_CHANNEL_TIME;
540 return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
541}
542
e229f844
SG
543static void ieee80211_scan_state_decision(struct ieee80211_local *local,
544 unsigned long *next_delay)
7d3be3cc 545{
142b9f50 546 bool associated = false;
df13cce5
HS
547 bool tx_empty = true;
548 bool bad_latency;
549 bool listen_int_exceeded;
550 unsigned long min_beacon_int = 0;
142b9f50 551 struct ieee80211_sub_if_data *sdata;
df13cce5 552 struct ieee80211_channel *next_chan;
cd2bb512 553 enum mac80211_scan_state next_scan_state;
142b9f50 554
df13cce5
HS
555 /*
556 * check if at least one STA interface is associated,
557 * check if at least one STA interface has pending tx frames
558 * and grab the lowest used beacon interval
559 */
142b9f50
HS
560 mutex_lock(&local->iflist_mtx);
561 list_for_each_entry(sdata, &local->interfaces, list) {
9607e6b6 562 if (!ieee80211_sdata_running(sdata))
142b9f50
HS
563 continue;
564
565 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
566 if (sdata->u.mgd.associated) {
567 associated = true;
df13cce5
HS
568
569 if (sdata->vif.bss_conf.beacon_int <
570 min_beacon_int || min_beacon_int == 0)
571 min_beacon_int =
572 sdata->vif.bss_conf.beacon_int;
573
574 if (!qdisc_all_tx_empty(sdata->dev)) {
575 tx_empty = false;
576 break;
577 }
142b9f50
HS
578 }
579 }
580 }
581 mutex_unlock(&local->iflist_mtx);
582
b23b025f
BG
583 next_chan = local->scan_req->channels[local->scan_channel_idx];
584
a80f7c0b 585 /*
07ef03ee
JB
586 * we're currently scanning a different channel, let's
587 * see if we can scan another channel without interfering
588 * with the current traffic situation.
589 *
590 * Since we don't know if the AP has pending frames for us
591 * we can only check for our tx queues and use the current
592 * pm_qos requirements for rx. Hence, if no tx traffic occurs
593 * at all we will scan as many channels in a row as the pm_qos
594 * latency allows us to. Additionally we also check for the
595 * currently negotiated listen interval to prevent losing
596 * frames unnecessarily.
597 *
598 * Otherwise switch back to the operating channel.
a80f7c0b 599 */
a80f7c0b 600
07ef03ee
JB
601 bad_latency = time_after(jiffies +
602 ieee80211_scan_get_channel_time(next_chan),
603 local->leave_oper_channel_time +
604 usecs_to_jiffies(pm_qos_request(PM_QOS_NETWORK_LATENCY)));
df13cce5 605
07ef03ee
JB
606 listen_int_exceeded = time_after(jiffies +
607 ieee80211_scan_get_channel_time(next_chan),
608 local->leave_oper_channel_time +
609 usecs_to_jiffies(min_beacon_int * 1024) *
610 local->hw.conf.listen_interval);
142b9f50 611
cd2bb512
SL
612 if (associated && !tx_empty) {
613 if (local->scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
614 next_scan_state = SCAN_ABORT;
615 else
616 next_scan_state = SCAN_SUSPEND;
617 } else if (associated && (bad_latency || listen_int_exceeded)) {
618 next_scan_state = SCAN_SUSPEND;
619 } else {
620 next_scan_state = SCAN_SET_CHANNEL;
621 }
622
623 local->next_scan_state = next_scan_state;
142b9f50 624
07ef03ee 625 *next_delay = 0;
142b9f50
HS
626}
627
2fb3f028
HS
628static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
629 unsigned long *next_delay)
630{
631 int skip;
632 struct ieee80211_channel *chan;
2fb3f028 633
7d3be3cc
HS
634 skip = 0;
635 chan = local->scan_req->channels[local->scan_channel_idx];
636
584991dc 637 local->scan_channel = chan;
b23b025f 638
07ef03ee
JB
639 if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
640 skip = 1;
7d3be3cc 641
7d3be3cc
HS
642 /* advance state machine to next channel/band */
643 local->scan_channel_idx++;
644
0ee9c13c
HS
645 if (skip) {
646 /* if we skip this channel return to the decision state */
647 local->next_scan_state = SCAN_DECISION;
2fb3f028 648 return;
0ee9c13c 649 }
7d3be3cc
HS
650
651 /*
652 * Probe delay is used to update the NAV, cf. 11.1.3.2.2
653 * (which unfortunately doesn't say _why_ step a) is done,
654 * but it waits for the probe delay or until a frame is
655 * received - and the received frame would update the NAV).
656 * For now, we do not support waiting until a frame is
657 * received.
658 *
659 * In any case, it is not necessary for a passive scan.
660 */
661 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
662 !local->scan_req->n_ssids) {
663 *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
977923b0 664 local->next_scan_state = SCAN_DECISION;
2fb3f028 665 return;
7d3be3cc
HS
666 }
667
2fb3f028 668 /* active scan, send probes */
7d3be3cc 669 *next_delay = IEEE80211_PROBE_DELAY;
977923b0 670 local->next_scan_state = SCAN_SEND_PROBE;
7d3be3cc
HS
671}
672
07ef03ee
JB
673static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
674 unsigned long *next_delay)
675{
676 /* switch back to the operating channel */
677 local->scan_channel = NULL;
678 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
679
680 /*
681 * Re-enable vifs and beaconing. Leave PS
682 * in off-channel state..will put that back
683 * on-channel at the end of scanning.
684 */
e76aadc5 685 ieee80211_offchannel_return(local, false);
07ef03ee
JB
686
687 *next_delay = HZ / 5;
688 /* afterwards, resume scan & go to next channel */
689 local->next_scan_state = SCAN_RESUME;
690}
691
692static void ieee80211_scan_state_resume(struct ieee80211_local *local,
693 unsigned long *next_delay)
694{
695 /* PS already is in off-channel mode */
696 ieee80211_offchannel_stop_vifs(local, false);
697
698 if (local->ops->flush) {
699 drv_flush(local, false);
700 *next_delay = 0;
701 } else
702 *next_delay = HZ / 10;
703
704 /* remember when we left the operating channel */
705 local->leave_oper_channel_time = jiffies;
706
707 /* advance to the next channel to be scanned */
de2ee84d 708 local->next_scan_state = SCAN_SET_CHANNEL;
07ef03ee
JB
709}
710
c2b13452 711void ieee80211_scan_work(struct work_struct *work)
0a51b27e
JB
712{
713 struct ieee80211_local *local =
714 container_of(work, struct ieee80211_local, scan_work.work);
d07bfd8b 715 struct ieee80211_sub_if_data *sdata;
0a51b27e 716 unsigned long next_delay = 0;
d07bfd8b 717 bool aborted, hw_scan;
0a51b27e 718
259b62e3 719 mutex_lock(&local->mtx);
8789d459 720
e2fd5dbc
JB
721 sdata = rcu_dereference_protected(local->scan_sdata,
722 lockdep_is_held(&local->mtx));
d07bfd8b 723
8a690674
BG
724 /* When scanning on-channel, the first-callback means completed. */
725 if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) {
726 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
727 goto out_complete;
728 }
729
259b62e3 730 if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
8789d459 731 aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
259b62e3 732 goto out_complete;
8789d459
JB
733 }
734
259b62e3
SG
735 if (!sdata || !local->scan_req)
736 goto out;
f3b85252 737
fbe9c429 738 if (local->scan_req && !local->scanning) {
f3b85252
JB
739 struct cfg80211_scan_request *req = local->scan_req;
740 int rc;
741
742 local->scan_req = NULL;
e2fd5dbc 743 rcu_assign_pointer(local->scan_sdata, NULL);
f3b85252
JB
744
745 rc = __ieee80211_start_scan(sdata, req);
259b62e3 746 if (rc) {
3aed49ef
SG
747 /* need to complete scan in cfg80211 */
748 local->scan_req = req;
259b62e3
SG
749 aborted = true;
750 goto out_complete;
751 } else
752 goto out;
f3b85252
JB
753 }
754
5bc75728
JB
755 /*
756 * Avoid re-scheduling when the sdata is going away.
757 */
9607e6b6 758 if (!ieee80211_sdata_running(sdata)) {
259b62e3
SG
759 aborted = true;
760 goto out_complete;
f3b85252 761 }
0a51b27e 762
f502d09b
HS
763 /*
764 * as long as no delay is required advance immediately
765 * without scheduling a new work
766 */
767 do {
c29acf20
RM
768 if (!ieee80211_sdata_running(sdata)) {
769 aborted = true;
770 goto out_complete;
771 }
772
977923b0 773 switch (local->next_scan_state) {
2fb3f028 774 case SCAN_DECISION:
e229f844
SG
775 /* if no more bands/channels left, complete scan */
776 if (local->scan_channel_idx >= local->scan_req->n_channels) {
777 aborted = false;
778 goto out_complete;
779 }
780 ieee80211_scan_state_decision(local, &next_delay);
f502d09b 781 break;
2fb3f028
HS
782 case SCAN_SET_CHANNEL:
783 ieee80211_scan_state_set_channel(local, &next_delay);
784 break;
f502d09b
HS
785 case SCAN_SEND_PROBE:
786 ieee80211_scan_state_send_probe(local, &next_delay);
787 break;
07ef03ee
JB
788 case SCAN_SUSPEND:
789 ieee80211_scan_state_suspend(local, &next_delay);
142b9f50 790 break;
07ef03ee
JB
791 case SCAN_RESUME:
792 ieee80211_scan_state_resume(local, &next_delay);
142b9f50 793 break;
cd2bb512
SL
794 case SCAN_ABORT:
795 aborted = true;
796 goto out_complete;
f502d09b
HS
797 }
798 } while (next_delay == 0);
0a51b27e 799
42935eca 800 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
d07bfd8b 801 goto out;
259b62e3
SG
802
803out_complete:
e229f844 804 hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
d07bfd8b 805 __ieee80211_scan_completed(&local->hw, aborted, hw_scan);
259b62e3
SG
806out:
807 mutex_unlock(&local->mtx);
0a51b27e
JB
808}
809
f3b85252
JB
810int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
811 struct cfg80211_scan_request *req)
0a51b27e 812{
f3b85252 813 int res;
de95a54b 814
a1699b75 815 mutex_lock(&sdata->local->mtx);
f3b85252 816 res = __ieee80211_start_scan(sdata, req);
a1699b75 817 mutex_unlock(&sdata->local->mtx);
0a51b27e 818
f3b85252 819 return res;
0a51b27e
JB
820}
821
34bcf715
SG
822int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
823 const u8 *ssid, u8 ssid_len,
824 struct ieee80211_channel *chan)
0a51b27e 825{
0a51b27e 826 struct ieee80211_local *local = sdata->local;
f3b85252 827 int ret = -EBUSY;
fb63bc41 828 enum ieee80211_band band;
2a519311 829
a1699b75 830 mutex_lock(&local->mtx);
0a51b27e 831
f3b85252
JB
832 /* busy scanning */
833 if (local->scan_req)
834 goto unlock;
9116dd01 835
be4a4b6a
JB
836 /* fill internal scan request */
837 if (!chan) {
34bcf715
SG
838 int i, max_n;
839 int n_ch = 0;
be4a4b6a
JB
840
841 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
842 if (!local->hw.wiphy->bands[band])
843 continue;
34bcf715
SG
844
845 max_n = local->hw.wiphy->bands[band]->n_channels;
846 for (i = 0; i < max_n; i++) {
847 struct ieee80211_channel *tmp_ch =
be4a4b6a 848 &local->hw.wiphy->bands[band]->channels[i];
34bcf715
SG
849
850 if (tmp_ch->flags & (IEEE80211_CHAN_NO_IBSS |
851 IEEE80211_CHAN_DISABLED))
852 continue;
853
854 local->int_scan_req->channels[n_ch] = tmp_ch;
855 n_ch++;
be4a4b6a
JB
856 }
857 }
858
34bcf715
SG
859 if (WARN_ON_ONCE(n_ch == 0))
860 goto unlock;
861
862 local->int_scan_req->n_channels = n_ch;
be4a4b6a 863 } else {
34bcf715
SG
864 if (WARN_ON_ONCE(chan->flags & (IEEE80211_CHAN_NO_IBSS |
865 IEEE80211_CHAN_DISABLED)))
866 goto unlock;
867
be4a4b6a
JB
868 local->int_scan_req->channels[0] = chan;
869 local->int_scan_req->n_channels = 1;
870 }
871
872 local->int_scan_req->ssids = &local->scan_ssid;
873 local->int_scan_req->n_ssids = 1;
5ba63533
JB
874 memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
875 local->int_scan_req->ssids[0].ssid_len = ssid_len;
9116dd01 876
5ba63533 877 ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
f3b85252 878 unlock:
a1699b75 879 mutex_unlock(&local->mtx);
f3b85252 880 return ret;
0a51b27e 881}
5bb644a0 882
4136c422
SG
883/*
884 * Only call this function when a scan can't be queued -- under RTNL.
885 */
5bb644a0
JB
886void ieee80211_scan_cancel(struct ieee80211_local *local)
887{
5bb644a0 888 /*
b856439b 889 * We are canceling software scan, or deferred scan that was not
4136c422
SG
890 * yet really started (see __ieee80211_start_scan ).
891 *
892 * Regarding hardware scan:
893 * - we can not call __ieee80211_scan_completed() as when
894 * SCAN_HW_SCANNING bit is set this function change
895 * local->hw_scan_req to operate on 5G band, what race with
896 * driver which can use local->hw_scan_req
897 *
898 * - we can not cancel scan_work since driver can schedule it
899 * by ieee80211_scan_completed(..., true) to finish scan
900 *
b856439b
EP
901 * Hence we only call the cancel_hw_scan() callback, but the low-level
902 * driver is still responsible for calling ieee80211_scan_completed()
903 * after the scan was completed/aborted.
5bb644a0 904 */
4136c422 905
a1699b75 906 mutex_lock(&local->mtx);
b856439b
EP
907 if (!local->scan_req)
908 goto out;
909
910 if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
911 if (local->ops->cancel_hw_scan)
e2fd5dbc
JB
912 drv_cancel_hw_scan(local,
913 rcu_dereference_protected(local->scan_sdata,
914 lockdep_is_held(&local->mtx)));
b856439b 915 goto out;
4136c422 916 }
b856439b
EP
917
918 /*
919 * If the work is currently running, it must be blocked on
920 * the mutex, but we'll set scan_sdata = NULL and it'll
921 * simply exit once it acquires the mutex.
922 */
923 cancel_delayed_work(&local->scan_work);
924 /* and clean up */
925 __ieee80211_scan_completed(&local->hw, true, false);
926out:
d07bfd8b 927 mutex_unlock(&local->mtx);
5bb644a0 928}
79f460ca
LC
929
930int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
931 struct cfg80211_sched_scan_request *req)
932{
933 struct ieee80211_local *local = sdata->local;
bca1e29f 934 struct ieee80211_sched_scan_ies sched_scan_ies = {};
c604b9f2
JB
935 int ret, i, iebufsz;
936
937 iebufsz = 2 + IEEE80211_MAX_SSID_LEN +
938 local->scan_ies_len + req->ie_len;
79f460ca 939
5260a5b2 940 mutex_lock(&local->mtx);
79f460ca 941
5260a5b2 942 if (rcu_access_pointer(local->sched_scan_sdata)) {
79f460ca
LC
943 ret = -EBUSY;
944 goto out;
945 }
946
947 if (!local->ops->sched_scan_start) {
948 ret = -ENOTSUPP;
949 goto out;
950 }
951
952 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
d811b3d5
AN
953 if (!local->hw.wiphy->bands[i])
954 continue;
955
c604b9f2 956 sched_scan_ies.ie[i] = kzalloc(iebufsz, GFP_KERNEL);
30dd3edf 957 if (!sched_scan_ies.ie[i]) {
79f460ca
LC
958 ret = -ENOMEM;
959 goto out_free;
960 }
961
30dd3edf
JB
962 sched_scan_ies.len[i] =
963 ieee80211_build_preq_ies(local, sched_scan_ies.ie[i],
c604b9f2
JB
964 iebufsz, req->ie, req->ie_len,
965 i, (u32) -1, 0);
79f460ca
LC
966 }
967
30dd3edf
JB
968 ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies);
969 if (ret == 0)
5260a5b2 970 rcu_assign_pointer(local->sched_scan_sdata, sdata);
79f460ca
LC
971
972out_free:
973 while (i > 0)
30dd3edf 974 kfree(sched_scan_ies.ie[--i]);
79f460ca 975out:
5260a5b2 976 mutex_unlock(&local->mtx);
79f460ca
LC
977 return ret;
978}
979
85a9994a 980int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata)
79f460ca
LC
981{
982 struct ieee80211_local *local = sdata->local;
30dd3edf 983 int ret = 0;
79f460ca 984
5260a5b2 985 mutex_lock(&local->mtx);
79f460ca
LC
986
987 if (!local->ops->sched_scan_stop) {
988 ret = -ENOTSUPP;
989 goto out;
990 }
991
30dd3edf 992 if (rcu_access_pointer(local->sched_scan_sdata))
85a9994a 993 drv_sched_scan_stop(local, sdata);
30dd3edf 994
79f460ca 995out:
5260a5b2 996 mutex_unlock(&local->mtx);
79f460ca
LC
997
998 return ret;
999}
1000
1001void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
1002{
1003 struct ieee80211_local *local = hw_to_local(hw);
1004
1005 trace_api_sched_scan_results(local);
1006
1007 cfg80211_sched_scan_results(hw->wiphy);
1008}
1009EXPORT_SYMBOL(ieee80211_sched_scan_results);
1010
85a9994a
LC
1011void ieee80211_sched_scan_stopped_work(struct work_struct *work)
1012{
1013 struct ieee80211_local *local =
1014 container_of(work, struct ieee80211_local,
1015 sched_scan_stopped_work);
85a9994a
LC
1016
1017 mutex_lock(&local->mtx);
1018
5260a5b2 1019 if (!rcu_access_pointer(local->sched_scan_sdata)) {
85a9994a
LC
1020 mutex_unlock(&local->mtx);
1021 return;
1022 }
1023
5260a5b2 1024 rcu_assign_pointer(local->sched_scan_sdata, NULL);
85a9994a
LC
1025
1026 mutex_unlock(&local->mtx);
1027
1028 cfg80211_sched_scan_stopped(local->hw.wiphy);
1029}
1030
79f460ca
LC
1031void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
1032{
1033 struct ieee80211_local *local = hw_to_local(hw);
1034
1035 trace_api_sched_scan_stopped(local);
1036
85a9994a 1037 ieee80211_queue_work(&local->hw, &local->sched_scan_stopped_work);
79f460ca
LC
1038}
1039EXPORT_SYMBOL(ieee80211_sched_scan_stopped);
This page took 0.371543 seconds and 5 git commands to generate.