blob: 20df861c6c4ca4c8e73169ffb39d6acfebf303da [file] [log] [blame]
Johannes Berg0a51b272008-09-08 17:44:25 +02001/*
Johannes Berg5484e232008-09-08 17:44:27 +02002 * Scanning implementation
3 *
Johannes Berg0a51b272008-09-08 17:44:25 +02004 * 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
Johannes Berg00d3f142009-02-10 21:26:00 +010015/* TODO: figure out how to avoid that the "current BSS" expires */
Johannes Berg5484e232008-09-08 17:44:27 +020016
Johannes Berg0a51b272008-09-08 17:44:25 +020017#include <linux/wireless.h>
18#include <linux/if_arp.h>
Johannes Berg078e1e62009-01-22 18:07:31 +010019#include <linux/rtnetlink.h>
Johannes Berg0a51b272008-09-08 17:44:25 +020020#include <net/mac80211.h>
21#include <net/iw_handler.h>
22
23#include "ieee80211_i.h"
Johannes Berg5484e232008-09-08 17:44:27 +020024#include "mesh.h"
Johannes Berg0a51b272008-09-08 17:44:25 +020025
26#define IEEE80211_PROBE_DELAY (HZ / 33)
27#define IEEE80211_CHANNEL_TIME (HZ / 33)
28#define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
29
Johannes Bergc2b13452008-09-11 00:01:55 +020030struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +020031ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
32 u8 *ssid, u8 ssid_len)
33{
Johannes Berg00d3f142009-02-10 21:26:00 +010034 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);
Johannes Berg5484e232008-09-08 17:44:27 +020039}
40
Johannes Berg00d3f142009-02-10 21:26:00 +010041static void ieee80211_rx_bss_free(struct cfg80211_bss *cbss)
Johannes Berg5484e232008-09-08 17:44:27 +020042{
Johannes Berg00d3f142009-02-10 21:26:00 +010043 struct ieee80211_bss *bss = (void *)cbss;
Johannes Berg5484e232008-09-08 17:44:27 +020044
Johannes Berg5484e232008-09-08 17:44:27 +020045 kfree(bss_mesh_id(bss));
46 kfree(bss_mesh_cfg(bss));
Johannes Berg5484e232008-09-08 17:44:27 +020047}
48
49void ieee80211_rx_bss_put(struct ieee80211_local *local,
Johannes Bergc2b13452008-09-11 00:01:55 +020050 struct ieee80211_bss *bss)
Johannes Berg5484e232008-09-08 17:44:27 +020051{
Johannes Berg00d3f142009-02-10 21:26:00 +010052 cfg80211_put_bss((struct cfg80211_bss *)bss);
Johannes Berg5484e232008-09-08 17:44:27 +020053}
54
Johannes Bergc2b13452008-09-11 00:01:55 +020055struct ieee80211_bss *
Johannes Berg5484e232008-09-08 17:44:27 +020056ieee80211_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,
Johannes Berg2a519312009-02-10 21:25:55 +010061 struct ieee80211_channel *channel,
62 bool beacon)
Johannes Berg5484e232008-09-08 17:44:27 +020063{
Johannes Bergc2b13452008-09-11 00:01:55 +020064 struct ieee80211_bss *bss;
Johannes Berg00d3f142009-02-10 21:26:00 +010065 int clen;
Johannes Berg2a519312009-02-10 21:25:55 +010066 s32 signal = 0;
67
Johannes Berg77965c92009-02-18 18:45:06 +010068 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
Johannes Berg2a519312009-02-10 21:25:55 +010069 signal = rx_status->signal * 100;
Johannes Berg77965c92009-02-18 18:45:06 +010070 else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
Johannes Berg2a519312009-02-10 21:25:55 +010071 signal = (rx_status->signal * 100) / local->hw.max_signal;
Johannes Berg2a519312009-02-10 21:25:55 +010072
Johannes Berg00d3f142009-02-10 21:26:00 +010073 bss = (void *)cfg80211_inform_bss_frame(local->hw.wiphy, channel,
Johannes Berg77965c92009-02-18 18:45:06 +010074 mgmt, len, signal, GFP_ATOMIC);
Johannes Berg5484e232008-09-08 17:44:27 +020075
Johannes Berg00d3f142009-02-10 21:26:00 +010076 if (!bss)
77 return NULL;
78
79 bss->cbss.free_priv = ieee80211_rx_bss_free;
Johannes Berg5484e232008-09-08 17:44:27 +020080
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
Johannes Berg5484e232008-09-08 17:44:27 +020087 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 APs */
94 if (!elems->tim || bss->dtim_period == 0)
95 bss->dtim_period = 1;
96
97 bss->supp_rates_len = 0;
98 if (elems->supp_rates) {
99 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
100 if (clen > elems->supp_rates_len)
101 clen = elems->supp_rates_len;
102 memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
103 clen);
104 bss->supp_rates_len += clen;
105 }
106 if (elems->ext_supp_rates) {
107 clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
108 if (clen > elems->ext_supp_rates_len)
109 clen = elems->ext_supp_rates_len;
110 memcpy(&bss->supp_rates[bss->supp_rates_len],
111 elems->ext_supp_rates, clen);
112 bss->supp_rates_len += clen;
113 }
114
Johannes Berg5484e232008-09-08 17:44:27 +0200115 bss->wmm_used = elems->wmm_param || elems->wmm_info;
116
117 if (!beacon)
118 bss->last_probe_resp = jiffies;
119
Johannes Berg5484e232008-09-08 17:44:27 +0200120 return bss;
121}
Johannes Berg0a51b272008-09-08 17:44:25 +0200122
Vasanthakumar Thiagarajan7a947082009-02-04 18:28:48 +0530123void ieee80211_rx_bss_remove(struct ieee80211_sub_if_data *sdata, u8 *bssid,
124 int freq, u8 *ssid, u8 ssid_len)
125{
126 struct ieee80211_bss *bss;
127 struct ieee80211_local *local = sdata->local;
128
129 bss = ieee80211_rx_bss_get(local, bssid, freq, ssid, ssid_len);
130 if (bss) {
Johannes Berg00d3f142009-02-10 21:26:00 +0100131 cfg80211_unlink_bss(local->hw.wiphy, (void *)bss);
Vasanthakumar Thiagarajan7a947082009-02-04 18:28:48 +0530132 ieee80211_rx_bss_put(local, bss);
133 }
134}
135
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200136ieee80211_rx_result
Johannes Bergc2b13452008-09-11 00:01:55 +0200137ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
138 struct ieee80211_rx_status *rx_status)
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200139{
140 struct ieee80211_mgmt *mgmt;
Johannes Bergc2b13452008-09-11 00:01:55 +0200141 struct ieee80211_bss *bss;
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200142 u8 *elements;
143 struct ieee80211_channel *channel;
144 size_t baselen;
145 int freq;
146 __le16 fc;
147 bool presp, beacon = false;
148 struct ieee802_11_elems elems;
149
150 if (skb->len < 2)
151 return RX_DROP_UNUSABLE;
152
153 mgmt = (struct ieee80211_mgmt *) skb->data;
154 fc = mgmt->frame_control;
155
156 if (ieee80211_is_ctl(fc))
157 return RX_CONTINUE;
158
159 if (skb->len < 24)
160 return RX_DROP_MONITOR;
161
162 presp = ieee80211_is_probe_resp(fc);
163 if (presp) {
164 /* ignore ProbeResp to foreign address */
165 if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
166 return RX_DROP_MONITOR;
167
168 presp = true;
169 elements = mgmt->u.probe_resp.variable;
170 baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
171 } else {
172 beacon = ieee80211_is_beacon(fc);
173 baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
174 elements = mgmt->u.beacon.variable;
175 }
176
177 if (!presp && !beacon)
178 return RX_CONTINUE;
179
180 if (baselen > skb->len)
181 return RX_DROP_MONITOR;
182
183 ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
184
185 if (elems.ds_params && elems.ds_params_len == 1)
186 freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
187 else
188 freq = rx_status->freq;
189
190 channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
191
192 if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
193 return RX_DROP_MONITOR;
194
195 bss = ieee80211_bss_info_update(sdata->local, rx_status,
196 mgmt, skb->len, &elems,
Johannes Berg2a519312009-02-10 21:25:55 +0100197 channel, beacon);
Jouni Malinend048e502008-10-11 03:29:55 +0300198 if (bss)
199 ieee80211_rx_bss_put(sdata->local, bss);
Johannes Berg98c8fcc2008-09-08 17:44:26 +0200200
201 dev_kfree_skb(skb);
202 return RX_QUEUED;
203}
204
Helmut Schaaaf88b902009-03-09 15:47:08 +0100205void ieee80211_scan_failed(struct ieee80211_local *local)
206{
207 if (WARN_ON(!local->scan_req))
208 return;
209
210 /* notify cfg80211 about the failed scan */
211 if (local->scan_req != &local->int_scan_req)
212 cfg80211_scan_done(local->scan_req, true);
213
214 local->scan_req = NULL;
215}
216
Kalle Valo9050bdd2009-03-22 21:57:21 +0200217/*
218 * inform AP that we will go to sleep so that it will buffer the frames
219 * while we scan
220 */
221static void ieee80211_scan_ps_enable(struct ieee80211_sub_if_data *sdata)
222{
223 struct ieee80211_local *local = sdata->local;
224 bool ps = false;
225
226 /* FIXME: what to do when local->pspolling is true? */
227
228 del_timer_sync(&local->dynamic_ps_timer);
229 cancel_work_sync(&local->dynamic_ps_enable_work);
230
231 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
232 ps = true;
233 local->hw.conf.flags &= ~IEEE80211_CONF_PS;
234 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
235 }
236
237 if (!ps || !(local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK))
238 /*
239 * If power save was enabled, no need to send a nullfunc
240 * frame because AP knows that we are sleeping. But if the
241 * hardware is creating the nullfunc frame for power save
242 * status (ie. IEEE80211_HW_PS_NULLFUNC_STACK is not
243 * enabled) and power save was enabled, the firmware just
244 * sent a null frame with power save disabled. So we need
245 * to send a new nullfunc frame to inform the AP that we
246 * are again sleeping.
247 */
248 ieee80211_send_nullfunc(local, sdata, 1);
249}
250
251/* inform AP that we are awake again, unless power save is enabled */
252static void ieee80211_scan_ps_disable(struct ieee80211_sub_if_data *sdata)
253{
254 struct ieee80211_local *local = sdata->local;
255
Johannes Berg965beda2009-04-16 13:17:24 +0200256 if (!local->ps_sdata)
Kalle Valo9050bdd2009-03-22 21:57:21 +0200257 ieee80211_send_nullfunc(local, sdata, 0);
258 else {
259 /*
260 * In !IEEE80211_HW_PS_NULLFUNC_STACK case the hardware
261 * will send a nullfunc frame with the powersave bit set
262 * even though the AP already knows that we are sleeping.
263 * This could be avoided by sending a null frame with power
264 * save bit disabled before enabling the power save, but
265 * this doesn't gain anything.
266 *
267 * When IEEE80211_HW_PS_NULLFUNC_STACK is enabled, no need
268 * to send a nullfunc frame because AP already knows that
269 * we are sleeping, let's just enable power save mode in
270 * hardware.
271 */
272 local->hw.conf.flags |= IEEE80211_CONF_PS;
273 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
274 }
275}
276
Johannes Berg2a519312009-02-10 21:25:55 +0100277void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
Johannes Berg0a51b272008-09-08 17:44:25 +0200278{
279 struct ieee80211_local *local = hw_to_local(hw);
280 struct ieee80211_sub_if_data *sdata;
Johannes Berg0a51b272008-09-08 17:44:25 +0200281
Johannes Bergc2b13452008-09-11 00:01:55 +0200282 if (WARN_ON(!local->hw_scanning && !local->sw_scanning))
Johannes Berg5bc75722008-09-11 00:01:51 +0200283 return;
284
Johannes Berg2a519312009-02-10 21:25:55 +0100285 if (WARN_ON(!local->scan_req))
286 return;
Johannes Berg5bc75722008-09-11 00:01:51 +0200287
Johannes Bergde95a542009-04-01 11:58:36 +0200288 if (local->hw_scanning) {
289 kfree(local->scan_req->ie);
290 local->scan_req->ie = local->orig_ies;
291 local->scan_req->ie_len = local->orig_ies_len;
292 }
293
Johannes Berg2a519312009-02-10 21:25:55 +0100294 if (local->scan_req != &local->int_scan_req)
295 cfg80211_scan_done(local->scan_req, aborted);
296 local->scan_req = NULL;
297
298 local->last_scan_completed = jiffies;
Johannes Berg0a51b272008-09-08 17:44:25 +0200299
Johannes Bergc2b13452008-09-11 00:01:55 +0200300 if (local->hw_scanning) {
301 local->hw_scanning = false;
Johannes Berge8975582008-10-09 12:18:51 +0200302 /*
303 * Somebody might have requested channel change during scan
304 * that we won't have acted upon, try now. ieee80211_hw_config
305 * will set the flag based on actual changes.
306 */
307 ieee80211_hw_config(local, 0);
Johannes Berg0a51b272008-09-08 17:44:25 +0200308 goto done;
309 }
310
Johannes Bergc2b13452008-09-11 00:01:55 +0200311 local->sw_scanning = false;
Johannes Berge8975582008-10-09 12:18:51 +0200312 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
Johannes Berg0a51b272008-09-08 17:44:25 +0200313
314 netif_tx_lock_bh(local->mdev);
315 netif_addr_lock(local->mdev);
316 local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
317 local->ops->configure_filter(local_to_hw(local),
318 FIF_BCN_PRBRESP_PROMISC,
319 &local->filter_flags,
320 local->mdev->mc_count,
321 local->mdev->mc_list);
322
323 netif_addr_unlock(local->mdev);
324 netif_tx_unlock_bh(local->mdev);
325
Michael Buesch80e775b2009-02-20 15:37:03 +0100326 if (local->ops->sw_scan_complete)
327 local->ops->sw_scan_complete(local_to_hw(local));
328
Johannes Berg078e1e62009-01-22 18:07:31 +0100329 mutex_lock(&local->iflist_mtx);
330 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Bergfb9ddbf2009-01-26 19:11:57 +0100331 if (!netif_running(sdata->dev))
332 continue;
333
Johannes Berg0a51b272008-09-08 17:44:25 +0200334 /* Tell AP we're back */
Johannes Berg05c914f2008-09-11 00:01:58 +0200335 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Berg46900292009-02-15 12:44:28 +0100336 if (sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED) {
Kalle Valo9050bdd2009-03-22 21:57:21 +0200337 ieee80211_scan_ps_disable(sdata);
Johannes Berg0a51b272008-09-08 17:44:25 +0200338 netif_tx_wake_all_queues(sdata->dev);
339 }
340 } else
341 netif_tx_wake_all_queues(sdata->dev);
Johannes Berg078e1e62009-01-22 18:07:31 +0100342
Johannes Berg14b80722009-02-10 21:25:42 +0100343 /* re-enable beaconing */
344 if (sdata->vif.type == NL80211_IFTYPE_AP ||
345 sdata->vif.type == NL80211_IFTYPE_ADHOC ||
346 sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
347 ieee80211_if_config(sdata,
348 IEEE80211_IFCC_BEACON_ENABLED);
Johannes Berg0a51b272008-09-08 17:44:25 +0200349 }
Johannes Berg078e1e62009-01-22 18:07:31 +0100350 mutex_unlock(&local->iflist_mtx);
Johannes Berg0a51b272008-09-08 17:44:25 +0200351
352 done:
353 ieee80211_mlme_notify_scan_completed(local);
Johannes Berg46900292009-02-15 12:44:28 +0100354 ieee80211_ibss_notify_scan_completed(local);
Johannes Berg472dbc42008-09-11 00:01:49 +0200355 ieee80211_mesh_notify_scan_completed(local);
Johannes Berg0a51b272008-09-08 17:44:25 +0200356}
357EXPORT_SYMBOL(ieee80211_scan_completed);
358
Johannes Bergc2b13452008-09-11 00:01:55 +0200359void ieee80211_scan_work(struct work_struct *work)
Johannes Berg0a51b272008-09-08 17:44:25 +0200360{
361 struct ieee80211_local *local =
362 container_of(work, struct ieee80211_local, scan_work.work);
363 struct ieee80211_sub_if_data *sdata = local->scan_sdata;
Johannes Berg0a51b272008-09-08 17:44:25 +0200364 struct ieee80211_channel *chan;
Johannes Berg2a519312009-02-10 21:25:55 +0100365 int skip, i;
Johannes Berg0a51b272008-09-08 17:44:25 +0200366 unsigned long next_delay = 0;
367
Johannes Berg5bc75722008-09-11 00:01:51 +0200368 /*
369 * Avoid re-scheduling when the sdata is going away.
370 */
371 if (!netif_running(sdata->dev))
Johannes Berg0a51b272008-09-08 17:44:25 +0200372 return;
373
374 switch (local->scan_state) {
375 case SCAN_SET_CHANNEL:
Johannes Berg0a51b272008-09-08 17:44:25 +0200376 /* if no more bands/channels left, complete scan */
Johannes Berg2a519312009-02-10 21:25:55 +0100377 if (local->scan_channel_idx >= local->scan_req->n_channels) {
378 ieee80211_scan_completed(local_to_hw(local), false);
Johannes Berg0a51b272008-09-08 17:44:25 +0200379 return;
380 }
381 skip = 0;
Johannes Berg2a519312009-02-10 21:25:55 +0100382 chan = local->scan_req->channels[local->scan_channel_idx];
Johannes Berg0a51b272008-09-08 17:44:25 +0200383
384 if (chan->flags & IEEE80211_CHAN_DISABLED ||
Johannes Berg05c914f2008-09-11 00:01:58 +0200385 (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
Johannes Berg0a51b272008-09-08 17:44:25 +0200386 chan->flags & IEEE80211_CHAN_NO_IBSS))
387 skip = 1;
388
389 if (!skip) {
390 local->scan_channel = chan;
Johannes Berge8975582008-10-09 12:18:51 +0200391 if (ieee80211_hw_config(local,
392 IEEE80211_CONF_CHANGE_CHANNEL))
Johannes Berg0a51b272008-09-08 17:44:25 +0200393 skip = 1;
Johannes Berg0a51b272008-09-08 17:44:25 +0200394 }
395
396 /* advance state machine to next channel/band */
397 local->scan_channel_idx++;
Johannes Berg0a51b272008-09-08 17:44:25 +0200398
399 if (skip)
400 break;
401
402 next_delay = IEEE80211_PROBE_DELAY +
403 usecs_to_jiffies(local->hw.channel_change_time);
404 local->scan_state = SCAN_SEND_PROBE;
405 break;
406 case SCAN_SEND_PROBE:
407 next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
408 local->scan_state = SCAN_SET_CHANNEL;
409
Johannes Berg2a519312009-02-10 21:25:55 +0100410 if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
411 !local->scan_req->n_ssids)
Johannes Berg0a51b272008-09-08 17:44:25 +0200412 break;
Johannes Berg2a519312009-02-10 21:25:55 +0100413 for (i = 0; i < local->scan_req->n_ssids; i++)
414 ieee80211_send_probe_req(
415 sdata, NULL,
416 local->scan_req->ssids[i].ssid,
Jouni Malinen70692ad2009-02-16 19:39:13 +0200417 local->scan_req->ssids[i].ssid_len,
418 local->scan_req->ie, local->scan_req->ie_len);
Johannes Berg0a51b272008-09-08 17:44:25 +0200419 next_delay = IEEE80211_CHANNEL_TIME;
420 break;
421 }
422
Johannes Berg5bc75722008-09-11 00:01:51 +0200423 queue_delayed_work(local->hw.workqueue, &local->scan_work,
424 next_delay);
Johannes Berg0a51b272008-09-08 17:44:25 +0200425}
426
427
Johannes Bergc2b13452008-09-11 00:01:55 +0200428int ieee80211_start_scan(struct ieee80211_sub_if_data *scan_sdata,
Johannes Berg2a519312009-02-10 21:25:55 +0100429 struct cfg80211_scan_request *req)
Johannes Berg0a51b272008-09-08 17:44:25 +0200430{
431 struct ieee80211_local *local = scan_sdata->local;
432 struct ieee80211_sub_if_data *sdata;
433
Johannes Berg2a519312009-02-10 21:25:55 +0100434 if (!req)
Johannes Berg0a51b272008-09-08 17:44:25 +0200435 return -EINVAL;
436
Johannes Berg2a519312009-02-10 21:25:55 +0100437 if (local->scan_req && local->scan_req != req)
438 return -EBUSY;
439
440 local->scan_req = req;
441
Johannes Berg0a51b272008-09-08 17:44:25 +0200442 /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
443 * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
444 * BSSID: MACAddress
445 * SSID
446 * ScanType: ACTIVE, PASSIVE
447 * ProbeDelay: delay (in microseconds) to be used prior to transmitting
448 * a Probe frame during active scanning
449 * ChannelList
450 * MinChannelTime (>= ProbeDelay), in TU
451 * MaxChannelTime: (>= MinChannelTime), in TU
452 */
453
454 /* MLME-SCAN.confirm
455 * BSSDescriptionSet
456 * ResultCode: SUCCESS, INVALID_PARAMETERS
457 */
458
Johannes Bergc2b13452008-09-11 00:01:55 +0200459 if (local->sw_scanning || local->hw_scanning) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200460 if (local->scan_sdata == scan_sdata)
461 return 0;
462 return -EBUSY;
463 }
464
465 if (local->ops->hw_scan) {
Johannes Bergde95a542009-04-01 11:58:36 +0200466 u8 *ies;
467 int rc, ielen;
468
469 ies = kmalloc(2 + IEEE80211_MAX_SSID_LEN +
470 local->scan_ies_len + req->ie_len, GFP_KERNEL);
471 if (!ies)
472 return -ENOMEM;
473
474 ielen = ieee80211_build_preq_ies(local, ies,
475 req->ie, req->ie_len);
476 local->orig_ies = req->ie;
477 local->orig_ies_len = req->ie_len;
478 req->ie = ies;
479 req->ie_len = ielen;
Johannes Berg5bc75722008-09-11 00:01:51 +0200480
Johannes Bergc2b13452008-09-11 00:01:55 +0200481 local->hw_scanning = true;
Johannes Berg2a519312009-02-10 21:25:55 +0100482 rc = local->ops->hw_scan(local_to_hw(local), req);
Johannes Berg5bc75722008-09-11 00:01:51 +0200483 if (rc) {
Johannes Bergc2b13452008-09-11 00:01:55 +0200484 local->hw_scanning = false;
Johannes Bergde95a542009-04-01 11:58:36 +0200485 kfree(ies);
486 req->ie_len = local->orig_ies_len;
487 req->ie = local->orig_ies;
Johannes Berg5bc75722008-09-11 00:01:51 +0200488 return rc;
Johannes Berg0a51b272008-09-08 17:44:25 +0200489 }
Johannes Berg5bc75722008-09-11 00:01:51 +0200490 local->scan_sdata = scan_sdata;
491 return 0;
Johannes Berg0a51b272008-09-08 17:44:25 +0200492 }
493
Kalle Valoa9a6fff2009-03-18 14:06:44 +0200494 /*
495 * Hardware/driver doesn't support hw_scan, so use software
496 * scanning instead. First send a nullfunc frame with power save
497 * bit on so that AP will buffer the frames for us while we are not
498 * listening, then send probe requests to each channel and wait for
499 * the responses. After all channels are scanned, tune back to the
500 * original channel and send a nullfunc frame with power save bit
501 * off to trigger the AP to send us all the buffered frames.
502 *
503 * Note that while local->sw_scanning is true everything else but
504 * nullfunc frames and probe requests will be dropped in
505 * ieee80211_tx_h_check_assoc().
506 */
Johannes Bergc2b13452008-09-11 00:01:55 +0200507 local->sw_scanning = true;
Michael Buesch80e775b2009-02-20 15:37:03 +0100508 if (local->ops->sw_scan_start)
509 local->ops->sw_scan_start(local_to_hw(local));
Johannes Berg0a51b272008-09-08 17:44:25 +0200510
Johannes Berg078e1e62009-01-22 18:07:31 +0100511 mutex_lock(&local->iflist_mtx);
512 list_for_each_entry(sdata, &local->interfaces, list) {
Johannes Bergfb9ddbf2009-01-26 19:11:57 +0100513 if (!netif_running(sdata->dev))
514 continue;
515
Johannes Berg14b80722009-02-10 21:25:42 +0100516 /* disable beaconing */
517 if (sdata->vif.type == NL80211_IFTYPE_AP ||
518 sdata->vif.type == NL80211_IFTYPE_ADHOC ||
519 sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
520 ieee80211_if_config(sdata,
521 IEEE80211_IFCC_BEACON_ENABLED);
Johannes Berg078e1e62009-01-22 18:07:31 +0100522
Johannes Berg05c914f2008-09-11 00:01:58 +0200523 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
Johannes Berg46900292009-02-15 12:44:28 +0100524 if (sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200525 netif_tx_stop_all_queues(sdata->dev);
Kalle Valo9050bdd2009-03-22 21:57:21 +0200526 ieee80211_scan_ps_enable(sdata);
Johannes Berg0a51b272008-09-08 17:44:25 +0200527 }
528 } else
529 netif_tx_stop_all_queues(sdata->dev);
530 }
Johannes Berg078e1e62009-01-22 18:07:31 +0100531 mutex_unlock(&local->iflist_mtx);
Johannes Berg0a51b272008-09-08 17:44:25 +0200532
Johannes Berg0a51b272008-09-08 17:44:25 +0200533 local->scan_state = SCAN_SET_CHANNEL;
534 local->scan_channel_idx = 0;
Johannes Berg0a51b272008-09-08 17:44:25 +0200535 local->scan_sdata = scan_sdata;
Johannes Berg2a519312009-02-10 21:25:55 +0100536 local->scan_req = req;
Johannes Berg0a51b272008-09-08 17:44:25 +0200537
538 netif_addr_lock_bh(local->mdev);
539 local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
540 local->ops->configure_filter(local_to_hw(local),
541 FIF_BCN_PRBRESP_PROMISC,
542 &local->filter_flags,
543 local->mdev->mc_count,
544 local->mdev->mc_list);
545 netif_addr_unlock_bh(local->mdev);
546
547 /* TODO: start scan as soon as all nullfunc frames are ACKed */
548 queue_delayed_work(local->hw.workqueue, &local->scan_work,
549 IEEE80211_CHANNEL_TIME);
550
551 return 0;
552}
553
554
Johannes Bergc2b13452008-09-11 00:01:55 +0200555int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
Johannes Berg2a519312009-02-10 21:25:55 +0100556 struct cfg80211_scan_request *req)
Johannes Berg0a51b272008-09-08 17:44:25 +0200557{
Johannes Berg0a51b272008-09-08 17:44:25 +0200558 struct ieee80211_local *local = sdata->local;
Johannes Berg46900292009-02-15 12:44:28 +0100559 struct ieee80211_if_managed *ifmgd;
Johannes Berg0a51b272008-09-08 17:44:25 +0200560
Johannes Berg2a519312009-02-10 21:25:55 +0100561 if (!req)
562 return -EINVAL;
563
564 if (local->scan_req && local->scan_req != req)
565 return -EBUSY;
566
567 local->scan_req = req;
568
Johannes Berg05c914f2008-09-11 00:01:58 +0200569 if (sdata->vif.type != NL80211_IFTYPE_STATION)
Johannes Berg2a519312009-02-10 21:25:55 +0100570 return ieee80211_start_scan(sdata, req);
Johannes Berg0a51b272008-09-08 17:44:25 +0200571
Johannes Berg9116dd02008-09-08 17:47:23 +0200572 /*
573 * STA has a state machine that might need to defer scanning
574 * while it's trying to associate/authenticate, therefore we
575 * queue it up to the state machine in that case.
576 */
577
Johannes Bergc2b13452008-09-11 00:01:55 +0200578 if (local->sw_scanning || local->hw_scanning) {
Johannes Berg0a51b272008-09-08 17:44:25 +0200579 if (local->scan_sdata == sdata)
580 return 0;
581 return -EBUSY;
582 }
583
Johannes Berg46900292009-02-15 12:44:28 +0100584 ifmgd = &sdata->u.mgd;
585 set_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request);
586 queue_work(local->hw.workqueue, &ifmgd->work);
Johannes Berg9116dd02008-09-08 17:47:23 +0200587
Johannes Berg0a51b272008-09-08 17:44:25 +0200588 return 0;
589}