blob: 7d82f13bdf1d2f90b79834fa34d8bd3e0bb603f6 [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/**
2 * Functions implementing wlan scan IOCTL and firmware command APIs
3 *
4 * IOCTL handlers as well as command preperation and response routines
5 * for sending scan commands to the firmware.
6 */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
John W. Linville7e272fc2008-09-24 18:13:14 -04008#include <linux/types.h>
Roel Kluin430453f2009-07-28 09:59:47 +02009#include <linux/kernel.h>
Dan Williamsfcdb53d2007-05-25 16:15:56 -040010#include <linux/etherdevice.h>
Johannes Berg2c7060022008-10-30 22:09:54 +010011#include <linux/if_arp.h>
Vladimir Davydovac630c22007-09-06 21:45:36 -040012#include <asm/unaligned.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040013#include <net/lib80211.h>
14
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020015#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020016#include "dev.h"
17#include "scan.h"
Holger Schurig2d465022009-10-22 15:30:48 +020018#include "assoc.h"
Holger Schurige93156e2009-10-22 15:30:58 +020019#include "wext.h"
David Woodhousefa62f992008-03-03 12:18:03 +010020#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020021
22//! Approximate amount of data needed to pass a scan result back to iwlist
23#define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
Holger Schurig243e84e2009-10-22 15:30:47 +020024 + IEEE80211_MAX_SSID_LEN \
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020025 + IW_EV_UINT_LEN \
26 + IW_EV_FREQ_LEN \
27 + IW_EV_QUAL_LEN \
Holger Schurig243e84e2009-10-22 15:30:47 +020028 + IEEE80211_MAX_SSID_LEN \
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020029 + IW_EV_PARAM_LEN \
30 + 40) /* 40 for WPAIE */
31
32//! Memory needed to store a max sized channel List TLV for a firmware scan
Dan Williams75b6a612009-05-22 20:03:09 -040033#define CHAN_TLV_MAX_SIZE (sizeof(struct mrvl_ie_header) \
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020034 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
35 * sizeof(struct chanscanparamset)))
36
37//! Memory needed to store a max number/size SSID TLV for a firmware scan
Dan Williams75b6a612009-05-22 20:03:09 -040038#define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvl_ie_ssid_param_set))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020039
David Woodhousefa62f992008-03-03 12:18:03 +010040//! Maximum memory needed for a cmd_ds_802_11_scan with all TLVs at max
41#define MAX_SCAN_CFG_ALLOC (sizeof(struct cmd_ds_802_11_scan) \
42 + CHAN_TLV_MAX_SIZE + SSID_TLV_MAX_SIZE)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020043
44//! The maximum number of channels the firmware can scan per command
45#define MRVDRV_MAX_CHANNELS_PER_SCAN 14
46
47/**
48 * @brief Number of channels to scan per firmware scan command issuance.
49 *
50 * Number restricted to prevent hitting the limit on the amount of scan data
51 * returned in a single firmware scan command.
52 */
53#define MRVDRV_CHANNELS_PER_SCAN_CMD 4
54
55//! Scan time specified in the channel TLV for each channel for passive scans
56#define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
57
58//! Scan time specified in the channel TLV for each channel for active scans
59#define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
60
Johannes Berg2c7060022008-10-30 22:09:54 +010061#define DEFAULT_MAX_SCAN_AGE (15 * HZ)
62
David Woodhousefa62f992008-03-03 12:18:03 +010063static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
64 struct cmd_header *resp);
Holger Schurige56188a2007-10-09 14:15:19 +020065
66/*********************************************************************/
67/* */
68/* Misc helper functions */
69/* */
70/*********************************************************************/
71
Holger Schurig23ff5032008-01-28 17:27:03 +010072/**
73 * @brief Unsets the MSB on basic rates
74 *
75 * Scan through an array and unset the MSB for basic data rates.
76 *
77 * @param rates buffer of data rates
78 * @param len size of buffer
79 */
80static void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
81{
82 int i;
83
84 for (i = 0; i < len; i++)
85 rates[i] &= 0x7f;
86}
87
88
David Woodhousef137e052008-03-03 12:18:59 +010089static inline void clear_bss_descriptor(struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -040090{
91 /* Don't blow away ->list, just BSS data */
92 memset(bss, 0, offsetof(struct bss_descriptor, list));
93}
94
Holger Schurigffd074f2007-12-07 16:52:10 +010095/**
96 * @brief Compare two SSIDs
97 *
98 * @param ssid1 A pointer to ssid to compare
99 * @param ssid2 A pointer to ssid to compare
100 *
101 * @return 0: ssid is same, otherwise is different
102 */
David Woodhousef137e052008-03-03 12:18:59 +0100103int lbs_ssid_cmp(uint8_t *ssid1, uint8_t ssid1_len, uint8_t *ssid2,
104 uint8_t ssid2_len)
Holger Schurigffd074f2007-12-07 16:52:10 +0100105{
106 if (ssid1_len != ssid2_len)
107 return -1;
108
109 return memcmp(ssid1, ssid2, ssid1_len);
110}
111
Holger Schurigffd074f2007-12-07 16:52:10 +0100112static inline int is_same_network(struct bss_descriptor *src,
113 struct bss_descriptor *dst)
114{
115 /* A network is only a duplicate if the channel, BSSID, and ESSID
116 * all match. We treat all <hidden> with the same BSSID and channel
117 * as one network */
118 return ((src->ssid_len == dst->ssid_len) &&
119 (src->channel == dst->channel) &&
120 !compare_ether_addr(src->bssid, dst->bssid) &&
121 !memcmp(src->ssid, dst->ssid, src->ssid_len));
122}
123
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200124
Holger Schurige56188a2007-10-09 14:15:19 +0200125
Holger Schurig2d465022009-10-22 15:30:48 +0200126/*********************************************************************/
127/* */
128/* Region channel support */
129/* */
130/*********************************************************************/
131
132#define LBS_TX_PWR_DEFAULT 20 /*100mW */
133#define LBS_TX_PWR_US_DEFAULT 20 /*100mW */
134#define LBS_TX_PWR_JP_DEFAULT 16 /*50mW */
135#define LBS_TX_PWR_FR_DEFAULT 20 /*100mW */
136#define LBS_TX_PWR_EMEA_DEFAULT 20 /*100mW */
137
138/* Format { channel, frequency (MHz), maxtxpower } */
139/* band: 'B/G', region: USA FCC/Canada IC */
140static struct chan_freq_power channel_freq_power_US_BG[] = {
141 {1, 2412, LBS_TX_PWR_US_DEFAULT},
142 {2, 2417, LBS_TX_PWR_US_DEFAULT},
143 {3, 2422, LBS_TX_PWR_US_DEFAULT},
144 {4, 2427, LBS_TX_PWR_US_DEFAULT},
145 {5, 2432, LBS_TX_PWR_US_DEFAULT},
146 {6, 2437, LBS_TX_PWR_US_DEFAULT},
147 {7, 2442, LBS_TX_PWR_US_DEFAULT},
148 {8, 2447, LBS_TX_PWR_US_DEFAULT},
149 {9, 2452, LBS_TX_PWR_US_DEFAULT},
150 {10, 2457, LBS_TX_PWR_US_DEFAULT},
151 {11, 2462, LBS_TX_PWR_US_DEFAULT}
152};
153
154/* band: 'B/G', region: Europe ETSI */
155static struct chan_freq_power channel_freq_power_EU_BG[] = {
156 {1, 2412, LBS_TX_PWR_EMEA_DEFAULT},
157 {2, 2417, LBS_TX_PWR_EMEA_DEFAULT},
158 {3, 2422, LBS_TX_PWR_EMEA_DEFAULT},
159 {4, 2427, LBS_TX_PWR_EMEA_DEFAULT},
160 {5, 2432, LBS_TX_PWR_EMEA_DEFAULT},
161 {6, 2437, LBS_TX_PWR_EMEA_DEFAULT},
162 {7, 2442, LBS_TX_PWR_EMEA_DEFAULT},
163 {8, 2447, LBS_TX_PWR_EMEA_DEFAULT},
164 {9, 2452, LBS_TX_PWR_EMEA_DEFAULT},
165 {10, 2457, LBS_TX_PWR_EMEA_DEFAULT},
166 {11, 2462, LBS_TX_PWR_EMEA_DEFAULT},
167 {12, 2467, LBS_TX_PWR_EMEA_DEFAULT},
168 {13, 2472, LBS_TX_PWR_EMEA_DEFAULT}
169};
170
171/* band: 'B/G', region: Spain */
172static struct chan_freq_power channel_freq_power_SPN_BG[] = {
173 {10, 2457, LBS_TX_PWR_DEFAULT},
174 {11, 2462, LBS_TX_PWR_DEFAULT}
175};
176
177/* band: 'B/G', region: France */
178static struct chan_freq_power channel_freq_power_FR_BG[] = {
179 {10, 2457, LBS_TX_PWR_FR_DEFAULT},
180 {11, 2462, LBS_TX_PWR_FR_DEFAULT},
181 {12, 2467, LBS_TX_PWR_FR_DEFAULT},
182 {13, 2472, LBS_TX_PWR_FR_DEFAULT}
183};
184
185/* band: 'B/G', region: Japan */
186static struct chan_freq_power channel_freq_power_JPN_BG[] = {
187 {1, 2412, LBS_TX_PWR_JP_DEFAULT},
188 {2, 2417, LBS_TX_PWR_JP_DEFAULT},
189 {3, 2422, LBS_TX_PWR_JP_DEFAULT},
190 {4, 2427, LBS_TX_PWR_JP_DEFAULT},
191 {5, 2432, LBS_TX_PWR_JP_DEFAULT},
192 {6, 2437, LBS_TX_PWR_JP_DEFAULT},
193 {7, 2442, LBS_TX_PWR_JP_DEFAULT},
194 {8, 2447, LBS_TX_PWR_JP_DEFAULT},
195 {9, 2452, LBS_TX_PWR_JP_DEFAULT},
196 {10, 2457, LBS_TX_PWR_JP_DEFAULT},
197 {11, 2462, LBS_TX_PWR_JP_DEFAULT},
198 {12, 2467, LBS_TX_PWR_JP_DEFAULT},
199 {13, 2472, LBS_TX_PWR_JP_DEFAULT},
200 {14, 2484, LBS_TX_PWR_JP_DEFAULT}
201};
202
203/**
204 * the structure for channel, frequency and power
205 */
206struct region_cfp_table {
207 u8 region;
208 struct chan_freq_power *cfp_BG;
209 int cfp_no_BG;
210};
211
212/**
213 * the structure for the mapping between region and CFP
214 */
215static struct region_cfp_table region_cfp_table[] = {
216 {0x10, /*US FCC */
217 channel_freq_power_US_BG,
218 ARRAY_SIZE(channel_freq_power_US_BG),
219 }
220 ,
221 {0x20, /*CANADA IC */
222 channel_freq_power_US_BG,
223 ARRAY_SIZE(channel_freq_power_US_BG),
224 }
225 ,
226 {0x30, /*EU*/ channel_freq_power_EU_BG,
227 ARRAY_SIZE(channel_freq_power_EU_BG),
228 }
229 ,
230 {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
231 ARRAY_SIZE(channel_freq_power_SPN_BG),
232 }
233 ,
234 {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
235 ARRAY_SIZE(channel_freq_power_FR_BG),
236 }
237 ,
238 {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
239 ARRAY_SIZE(channel_freq_power_JPN_BG),
240 }
241 ,
242/*Add new region here */
243};
244
245/**
246 * @brief This function finds the CFP in
247 * region_cfp_table based on region and band parameter.
248 *
249 * @param region The region code
250 * @param band The band
251 * @param cfp_no A pointer to CFP number
252 * @return A pointer to CFP
253 */
254static struct chan_freq_power *lbs_get_region_cfp_table(u8 region, int *cfp_no)
255{
256 int i, end;
257
258 lbs_deb_enter(LBS_DEB_MAIN);
259
260 end = ARRAY_SIZE(region_cfp_table);
261
262 for (i = 0; i < end ; i++) {
263 lbs_deb_main("region_cfp_table[i].region=%d\n",
264 region_cfp_table[i].region);
265 if (region_cfp_table[i].region == region) {
266 *cfp_no = region_cfp_table[i].cfp_no_BG;
267 lbs_deb_leave(LBS_DEB_MAIN);
268 return region_cfp_table[i].cfp_BG;
269 }
270 }
271
272 lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
273 return NULL;
274}
275
276int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
277{
278 int ret = 0;
279 int i = 0;
280
281 struct chan_freq_power *cfp;
282 int cfp_no;
283
284 lbs_deb_enter(LBS_DEB_MAIN);
285
286 memset(priv->region_channel, 0, sizeof(priv->region_channel));
287
288 cfp = lbs_get_region_cfp_table(region, &cfp_no);
289 if (cfp != NULL) {
290 priv->region_channel[i].nrcfp = cfp_no;
291 priv->region_channel[i].CFP = cfp;
292 } else {
293 lbs_deb_main("wrong region code %#x in band B/G\n",
294 region);
295 ret = -1;
296 goto out;
297 }
298 priv->region_channel[i].valid = 1;
299 priv->region_channel[i].region = region;
300 priv->region_channel[i].band = band;
301 i++;
302out:
303 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
304 return ret;
305}
306
307
308
Holger Schurige56188a2007-10-09 14:15:19 +0200309
Holger Schurige56188a2007-10-09 14:15:19 +0200310/*********************************************************************/
311/* */
312/* Main scanning support */
313/* */
314/*********************************************************************/
315
Holger Schurige56188a2007-10-09 14:15:19 +0200316/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200317 * @brief Create a channel list for the driver to scan based on region info
318 *
Holger Schurig10078322007-11-15 18:05:47 -0500319 * Only used from lbs_scan_setup_scan_config()
Holger Schurige56188a2007-10-09 14:15:19 +0200320 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200321 * Use the driver region/band information to construct a comprehensive list
322 * of channels to scan. This routine is used for any scan that is not
323 * provided a specific channel list to scan.
324 *
Holger Schurig69f90322007-11-23 15:43:44 +0100325 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200326 * @param scanchanlist Output parameter: resulting channel list to scan
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200327 *
328 * @return void
329 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100330static int lbs_scan_create_channel_list(struct lbs_private *priv,
Holger Schurig52933d82008-03-05 07:05:32 +0100331 struct chanscanparamset *scanchanlist)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200332{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200333 struct region_channel *scanregion;
334 struct chan_freq_power *cfp;
335 int rgnidx;
336 int chanidx;
337 int nextchan;
David Woodhousef137e052008-03-03 12:18:59 +0100338 uint8_t scantype;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200339
340 chanidx = 0;
341
342 /* Set the default scan type to the user specified type, will later
343 * be changed to passive on a per channel basis if restricted by
344 * regulatory requirements (11d or 11h)
345 */
Holger Schurig4f2fdaa2007-08-02 13:12:27 -0400346 scantype = CMD_SCAN_TYPE_ACTIVE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200347
David Woodhouseaa21c002007-12-08 20:04:36 +0000348 for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
Holger Schurigd37b4fd2009-10-22 15:30:45 +0200349 if (!priv->region_channel[rgnidx].valid)
350 continue;
351 scanregion = &priv->region_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200352
David Woodhousef137e052008-03-03 12:18:59 +0100353 for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
354 struct chanscanparamset *chan = &scanchanlist[chanidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200355
356 cfp = scanregion->CFP + nextchan;
357
David Woodhousef137e052008-03-03 12:18:59 +0100358 if (scanregion->band == BAND_B || scanregion->band == BAND_G)
359 chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200360
Dan Williams0aef64d2007-08-02 11:31:18 -0400361 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
David Woodhousef137e052008-03-03 12:18:59 +0100362 chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
363 chan->chanscanmode.passivescan = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200364 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100365 chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
366 chan->chanscanmode.passivescan = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200367 }
368
David Woodhousef137e052008-03-03 12:18:59 +0100369 chan->channumber = cfp->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200370 }
371 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100372 return chanidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200373}
374
Holger Schurige56188a2007-10-09 14:15:19 +0200375/*
Holger Schurigffd074f2007-12-07 16:52:10 +0100376 * Add SSID TLV of the form:
377 *
378 * TLV-ID SSID 00 00
379 * length 06 00
380 * ssid 4d 4e 54 45 53 54
381 */
Holger Schurig52933d82008-03-05 07:05:32 +0100382static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv)
Dan Williamseb8f7332007-05-25 16:25:21 -0400383{
Dan Williams75b6a612009-05-22 20:03:09 -0400384 struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
David Woodhousef137e052008-03-03 12:18:59 +0100385
Holger Schurigffd074f2007-12-07 16:52:10 +0100386 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
Holger Schurig52933d82008-03-05 07:05:32 +0100387 ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len);
388 memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len);
389 return sizeof(ssid_tlv->header) + priv->scan_ssid_len;
Holger Schurigffd074f2007-12-07 16:52:10 +0100390}
Dan Williamseb8f7332007-05-25 16:25:21 -0400391
Holger Schurigffd074f2007-12-07 16:52:10 +0100392/*
393 * Add CHANLIST TLV of the form
394 *
395 * TLV-ID CHANLIST 01 01
396 * length 5b 00
397 * channel 1 00 01 00 00 00 64 00
398 * radio type 00
399 * channel 01
400 * scan type 00
401 * min scan time 00 00
402 * max scan time 64 00
403 * channel 2 00 02 00 00 00 64 00
404 * channel 3 00 03 00 00 00 64 00
405 * channel 4 00 04 00 00 00 64 00
406 * channel 5 00 05 00 00 00 64 00
407 * channel 6 00 06 00 00 00 64 00
408 * channel 7 00 07 00 00 00 64 00
409 * channel 8 00 08 00 00 00 64 00
410 * channel 9 00 09 00 00 00 64 00
411 * channel 10 00 0a 00 00 00 64 00
412 * channel 11 00 0b 00 00 00 64 00
413 * channel 12 00 0c 00 00 00 64 00
414 * channel 13 00 0d 00 00 00 64 00
415 *
416 */
David Woodhousef137e052008-03-03 12:18:59 +0100417static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
418 struct chanscanparamset *chan_list,
419 int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100420{
David Woodhousef137e052008-03-03 12:18:59 +0100421 size_t size = sizeof(struct chanscanparamset) *chan_count;
Dan Williams75b6a612009-05-22 20:03:09 -0400422 struct mrvl_ie_chanlist_param_set *chan_tlv = (void *)tlv;
Dan Williamseb8f7332007-05-25 16:25:21 -0400423
Holger Schurigffd074f2007-12-07 16:52:10 +0100424 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
425 memcpy(chan_tlv->chanscanparam, chan_list, size);
426 chan_tlv->header.len = cpu_to_le16(size);
427 return sizeof(chan_tlv->header) + size;
428}
Dan Williamseb8f7332007-05-25 16:25:21 -0400429
Holger Schurigffd074f2007-12-07 16:52:10 +0100430/*
431 * Add RATES TLV of the form
432 *
433 * TLV-ID RATES 01 00
434 * length 0e 00
435 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
436 *
437 * The rates are in lbs_bg_rates[], but for the 802.11b
438 * rates the high bit isn't set.
439 */
David Woodhousef137e052008-03-03 12:18:59 +0100440static int lbs_scan_add_rates_tlv(uint8_t *tlv)
Holger Schurigffd074f2007-12-07 16:52:10 +0100441{
442 int i;
Dan Williams75b6a612009-05-22 20:03:09 -0400443 struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
Holger Schurigffd074f2007-12-07 16:52:10 +0100444
445 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
446 tlv += sizeof(rate_tlv->header);
447 for (i = 0; i < MAX_RATES; i++) {
448 *tlv = lbs_bg_rates[i];
449 if (*tlv == 0)
450 break;
451 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
452 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
453 Note that the values are MBit/s * 2, to mark them as
454 basic rates so that the firmware likes it better */
455 if (*tlv == 0x02 || *tlv == 0x04 ||
456 *tlv == 0x0b || *tlv == 0x16)
457 *tlv |= 0x80;
458 tlv++;
Dan Williamseb8f7332007-05-25 16:25:21 -0400459 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100460 rate_tlv->header.len = cpu_to_le16(i);
461 return sizeof(rate_tlv->header) + i;
462}
Dan Williamseb8f7332007-05-25 16:25:21 -0400463
Holger Schurigffd074f2007-12-07 16:52:10 +0100464/*
465 * Generate the CMD_802_11_SCAN command with the proper tlv
466 * for a bunch of channels.
467 */
David Woodhousefa62f992008-03-03 12:18:03 +0100468static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
Holger Schurig52933d82008-03-05 07:05:32 +0100469 struct chanscanparamset *chan_list, int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100470{
471 int ret = -ENOMEM;
David Woodhousefa62f992008-03-03 12:18:03 +0100472 struct cmd_ds_802_11_scan *scan_cmd;
473 uint8_t *tlv; /* pointer into our current, growing TLV storage area */
Holger Schurigffd074f2007-12-07 16:52:10 +0100474
David Woodhousefa62f992008-03-03 12:18:03 +0100475 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
Holger Schurigc0d43992008-04-29 10:07:56 +0200476 bsstype, chan_list ? chan_list[0].channumber : -1,
477 chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100478
479 /* create the fixed part for scan command */
480 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
481 if (scan_cmd == NULL)
Holger Schurige56188a2007-10-09 14:15:19 +0200482 goto out;
David Woodhousefa62f992008-03-03 12:18:03 +0100483
Holger Schurigffd074f2007-12-07 16:52:10 +0100484 tlv = scan_cmd->tlvbuffer;
Holger Schurig52933d82008-03-05 07:05:32 +0100485 /* TODO: do we need to scan for a specific BSSID?
486 memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
Holger Schurigffd074f2007-12-07 16:52:10 +0100487 scan_cmd->bsstype = bsstype;
Dan Williamseb8f7332007-05-25 16:25:21 -0400488
Holger Schurigffd074f2007-12-07 16:52:10 +0100489 /* add TLVs */
Holger Schurig52933d82008-03-05 07:05:32 +0100490 if (priv->scan_ssid_len)
491 tlv += lbs_scan_add_ssid_tlv(priv, tlv);
Holger Schurigffd074f2007-12-07 16:52:10 +0100492 if (chan_list && chan_count)
493 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
494 tlv += lbs_scan_add_rates_tlv(tlv);
Dan Williamseb8f7332007-05-25 16:25:21 -0400495
Holger Schurigffd074f2007-12-07 16:52:10 +0100496 /* This is the final data we are about to send */
David Woodhousefa62f992008-03-03 12:18:03 +0100497 scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
498 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
499 sizeof(*scan_cmd));
Holger Schurigffd074f2007-12-07 16:52:10 +0100500 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
David Woodhousefa62f992008-03-03 12:18:03 +0100501 tlv - scan_cmd->tlvbuffer);
Dan Williamseb8f7332007-05-25 16:25:21 -0400502
David Woodhousefa62f992008-03-03 12:18:03 +0100503 ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
504 le16_to_cpu(scan_cmd->hdr.size),
505 lbs_ret_80211_scan, 0);
506
Holger Schurige56188a2007-10-09 14:15:19 +0200507out:
Holger Schurigffd074f2007-12-07 16:52:10 +0100508 kfree(scan_cmd);
509 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
510 return ret;
Dan Williamseb8f7332007-05-25 16:25:21 -0400511}
512
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200513/**
514 * @brief Internal function used to start a scan based on an input config
515 *
516 * Use the input user scan configuration information when provided in
517 * order to send the appropriate scan commands to firmware to populate or
518 * update the internal driver scan table
519 *
Holger Schurig69f90322007-11-23 15:43:44 +0100520 * @param priv A pointer to struct lbs_private structure
Holger Schurig52933d82008-03-05 07:05:32 +0100521 * @param full_scan Do a full-scan (blocking)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200522 *
523 * @return 0 or < 0 if error
524 */
Holger Schurig245bf202008-04-02 16:27:42 +0200525int lbs_scan_networks(struct lbs_private *priv, int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200526{
Holger Schurigffd074f2007-12-07 16:52:10 +0100527 int ret = -ENOMEM;
528 struct chanscanparamset *chan_list;
529 struct chanscanparamset *curr_chans;
530 int chan_count;
David Woodhousef137e052008-03-03 12:18:59 +0100531 uint8_t bsstype = CMD_BSS_TYPE_ANY;
Holger Schurigffd074f2007-12-07 16:52:10 +0100532 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
Holger Schurigffd074f2007-12-07 16:52:10 +0100533 union iwreq_data wrqu;
Dan Williamsf8f55102007-05-30 10:12:55 -0400534#ifdef CONFIG_LIBERTAS_DEBUG
Holger Schurigffd074f2007-12-07 16:52:10 +0100535 struct bss_descriptor *iter;
Dan Williamsf8f55102007-05-30 10:12:55 -0400536 int i = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -0400537 DECLARE_SSID_BUF(ssid);
Dan Williamsf8f55102007-05-30 10:12:55 -0400538#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200539
David Woodhousef137e052008-03-03 12:18:59 +0100540 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
Dan Williams2afc0c52007-08-02 13:19:04 -0400541
542 /* Cancel any partial outstanding partial scans if this scan
543 * is a full scan.
544 */
545 if (full_scan && delayed_work_pending(&priv->scan_work))
546 cancel_delayed_work(&priv->scan_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200547
Holger Schurig52933d82008-03-05 07:05:32 +0100548 /* User-specified bsstype or channel list
549 TODO: this can be implemented if some user-space application
550 need the feature. Formerly, it was accessible from debugfs,
551 but then nowhere used.
Holger Schurigffd074f2007-12-07 16:52:10 +0100552 if (user_cfg) {
553 if (user_cfg->bsstype)
Holger Schurig52933d82008-03-05 07:05:32 +0100554 bsstype = user_cfg->bsstype;
555 } */
556
557 lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200558
Holger Schurigffd074f2007-12-07 16:52:10 +0100559 /* Create list of channels to scan */
560 chan_list = kzalloc(sizeof(struct chanscanparamset) *
David Woodhousef137e052008-03-03 12:18:59 +0100561 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
Holger Schurigffd074f2007-12-07 16:52:10 +0100562 if (!chan_list) {
563 lbs_pr_alert("SCAN: chan_list empty\n");
564 goto out;
565 }
566
567 /* We want to scan all channels */
Holger Schurig52933d82008-03-05 07:05:32 +0100568 chan_count = lbs_scan_create_channel_list(priv, chan_list);
Holger Schurigffd074f2007-12-07 16:52:10 +0100569
570 netif_stop_queue(priv->dev);
Samuel Ortiz659c8e52009-12-18 11:36:49 +0100571 if (priv->mesh_dev)
David Woodhousea27b9f92007-12-12 00:41:51 -0500572 netif_stop_queue(priv->mesh_dev);
Holger Schurigffd074f2007-12-07 16:52:10 +0100573
574 /* Prepare to continue an interrupted scan */
Holger Schurig8816edc2008-01-28 17:28:05 +0100575 lbs_deb_scan("chan_count %d, scan_channel %d\n",
576 chan_count, priv->scan_channel);
Holger Schurigffd074f2007-12-07 16:52:10 +0100577 curr_chans = chan_list;
578 /* advance channel list by already-scanned-channels */
Holger Schurig8816edc2008-01-28 17:28:05 +0100579 if (priv->scan_channel > 0) {
580 curr_chans += priv->scan_channel;
581 chan_count -= priv->scan_channel;
Holger Schurigffd074f2007-12-07 16:52:10 +0100582 }
583
584 /* Send scan command(s)
585 * numchannels contains the number of channels we should maximally scan
586 * chan_count is the total number of channels to scan
587 */
588
589 while (chan_count) {
590 int to_scan = min(numchannels, chan_count);
591 lbs_deb_scan("scanning %d of %d channels\n",
David Woodhousef137e052008-03-03 12:18:59 +0100592 to_scan, chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100593 ret = lbs_do_scan(priv, bsstype, curr_chans,
Holger Schurig52933d82008-03-05 07:05:32 +0100594 to_scan);
Holger Schurigffd074f2007-12-07 16:52:10 +0100595 if (ret) {
596 lbs_pr_err("SCAN_CMD failed\n");
597 goto out2;
598 }
599 curr_chans += to_scan;
600 chan_count -= to_scan;
601
602 /* somehow schedule the next part of the scan */
David Woodhousef137e052008-03-03 12:18:59 +0100603 if (chan_count && !full_scan &&
David Woodhouseaa21c002007-12-08 20:04:36 +0000604 !priv->surpriseremoved) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100605 /* -1 marks just that we're currently scanning */
Holger Schurig8816edc2008-01-28 17:28:05 +0100606 if (priv->scan_channel < 0)
607 priv->scan_channel = to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100608 else
Holger Schurig8816edc2008-01-28 17:28:05 +0100609 priv->scan_channel += to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100610 cancel_delayed_work(&priv->scan_work);
611 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100612 msecs_to_jiffies(300));
Holger Schurigffd074f2007-12-07 16:52:10 +0100613 /* skip over GIWSCAN event */
614 goto out;
615 }
616
617 }
618 memset(&wrqu, 0, sizeof(union iwreq_data));
619 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200620
Dan Williamsf8f55102007-05-30 10:12:55 -0400621#ifdef CONFIG_LIBERTAS_DEBUG
622 /* Dump the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000623 mutex_lock(&priv->lock);
Holger Schurigffd074f2007-12-07 16:52:10 +0100624 lbs_deb_scan("scan table:\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000625 list_for_each_entry(iter, &priv->network_list, list)
Johannes Berge1749612008-10-27 15:59:26 -0700626 lbs_deb_scan("%02d: BSSID %pM, RSSI %d, SSID '%s'\n",
627 i++, iter->bssid, iter->rssi,
John W. Linville9387b7c2008-09-30 20:59:05 -0400628 print_ssid(ssid, iter->ssid, iter->ssid_len));
David Woodhouseaa21c002007-12-08 20:04:36 +0000629 mutex_unlock(&priv->lock);
Dan Williamsf8f55102007-05-30 10:12:55 -0400630#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200631
Holger Schurigffd074f2007-12-07 16:52:10 +0100632out2:
Holger Schurig8816edc2008-01-28 17:28:05 +0100633 priv->scan_channel = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100634
635out:
Samuel Ortiz659c8e52009-12-18 11:36:49 +0100636 if (priv->connect_status == LBS_CONNECTED && !priv->tx_pending_len)
637 netif_wake_queue(priv->dev);
638
John W. Linville891dc5e2009-12-30 15:25:08 -0500639 if (priv->mesh_dev && lbs_mesh_connected(priv) &&
Samuel Ortiz659c8e52009-12-18 11:36:49 +0100640 !priv->tx_pending_len)
641 netif_wake_queue(priv->mesh_dev);
642
Holger Schurigffd074f2007-12-07 16:52:10 +0100643 kfree(chan_list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200644
Holger Schurig9012b282007-05-25 11:27:16 -0400645 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200646 return ret;
647}
648
Holger Schurig52933d82008-03-05 07:05:32 +0100649void lbs_scan_worker(struct work_struct *work)
650{
651 struct lbs_private *priv =
652 container_of(work, struct lbs_private, scan_work.work);
653
654 lbs_deb_enter(LBS_DEB_SCAN);
655 lbs_scan_networks(priv, 0);
656 lbs_deb_leave(LBS_DEB_SCAN);
657}
658
659
Holger Schurigffd074f2007-12-07 16:52:10 +0100660/*********************************************************************/
661/* */
662/* Result interpretation */
663/* */
664/*********************************************************************/
665
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200666/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200667 * @brief Interpret a BSS scan response returned from the firmware
668 *
Walter Goldens77c20612010-05-18 04:44:54 -0700669 * Parse the various fixed fields and IEs passed back for a BSS probe
Holger Schurigffd074f2007-12-07 16:52:10 +0100670 * response or beacon from the scan command. Record information as needed
671 * in the scan table struct bss_descriptor for that entry.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200672 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400673 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200674 *
675 * @return 0 or -1
676 */
Holger Schurig10078322007-11-15 18:05:47 -0500677static int lbs_process_bss(struct bss_descriptor *bss,
David Woodhousef137e052008-03-03 12:18:59 +0100678 uint8_t **pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200679{
Dan Williams5fd164e2009-05-22 20:01:21 -0400680 struct ieee_ie_fh_param_set *fh;
681 struct ieee_ie_ds_param_set *ds;
682 struct ieee_ie_cf_param_set *cf;
683 struct ieee_ie_ibss_param_set *ibss;
John W. Linville9387b7c2008-09-30 20:59:05 -0400684 DECLARE_SSID_BUF(ssid);
David Woodhousef137e052008-03-03 12:18:59 +0100685 uint8_t *pos, *end, *p;
686 uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
687 uint16_t beaconsize = 0;
Holger Schurig9012b282007-05-25 11:27:16 -0400688 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200689
Holger Schurige56188a2007-10-09 14:15:19 +0200690 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200691
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200692 if (*bytesleft >= sizeof(beaconsize)) {
693 /* Extract & convert beacon size from the command buffer */
Harvey Harrison533dd1b2008-04-29 01:03:36 -0700694 beaconsize = get_unaligned_le16(*pbeaconinfo);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200695 *bytesleft -= sizeof(beaconsize);
696 *pbeaconinfo += sizeof(beaconsize);
697 }
698
699 if (beaconsize == 0 || beaconsize > *bytesleft) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200700 *pbeaconinfo += *bytesleft;
701 *bytesleft = 0;
Holger Schurige56188a2007-10-09 14:15:19 +0200702 ret = -1;
703 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200704 }
705
706 /* Initialize the current working beacon pointer for this BSS iteration */
Dan Williamsab617972007-08-02 10:48:02 -0400707 pos = *pbeaconinfo;
708 end = pos + beaconsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200709
710 /* Advance the return beacon pointer past the current beacon */
711 *pbeaconinfo += beaconsize;
712 *bytesleft -= beaconsize;
713
Dan Williamsab617972007-08-02 10:48:02 -0400714 memcpy(bss->bssid, pos, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -0700715 lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid);
Dan Williamsab617972007-08-02 10:48:02 -0400716 pos += ETH_ALEN;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200717
Dan Williamsab617972007-08-02 10:48:02 -0400718 if ((end - pos) < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400719 lbs_deb_scan("process_bss: Not enough bytes left\n");
Holger Schurige56188a2007-10-09 14:15:19 +0200720 ret = -1;
721 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200722 }
723
724 /*
725 * next 4 fields are RSSI, time stamp, beacon interval,
726 * and capability information
727 */
728
729 /* RSSI is 1 byte long */
Dan Williamsab617972007-08-02 10:48:02 -0400730 bss->rssi = *pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100731 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
Dan Williamsab617972007-08-02 10:48:02 -0400732 pos++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200733
734 /* time stamp is 8 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400735 pos += 8;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200736
737 /* beacon interval is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300738 bss->beaconperiod = get_unaligned_le16(pos);
Dan Williamsab617972007-08-02 10:48:02 -0400739 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200740
741 /* capability information is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300742 bss->capability = get_unaligned_le16(pos);
Holger Schurigffd074f2007-12-07 16:52:10 +0100743 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
Dan Williamsab617972007-08-02 10:48:02 -0400744 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200745
Dan Williams0c9ca692007-08-02 10:43:44 -0400746 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
Holger Schurigffd074f2007-12-07 16:52:10 +0100747 lbs_deb_scan("process_bss: WEP enabled\n");
Dan Williams0c9ca692007-08-02 10:43:44 -0400748 if (bss->capability & WLAN_CAPABILITY_IBSS)
749 bss->mode = IW_MODE_ADHOC;
750 else
751 bss->mode = IW_MODE_INFRA;
752
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200753 /* rest of the current buffer are IE's */
Holger Schurigffd074f2007-12-07 16:52:10 +0100754 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
Holger Schurigece56192007-08-02 11:53:06 -0400755 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200756
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200757 /* process variable IE */
Dan Williamsab617972007-08-02 10:48:02 -0400758 while (pos <= end - 2) {
Johannes Berg2c7060022008-10-30 22:09:54 +0100759 if (pos + pos[1] > end) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400760 lbs_deb_scan("process_bss: error in processing IE, "
David Woodhousef137e052008-03-03 12:18:59 +0100761 "bytes left < IE length\n");
Dan Williamsab617972007-08-02 10:48:02 -0400762 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200763 }
764
Johannes Berg2c7060022008-10-30 22:09:54 +0100765 switch (pos[0]) {
766 case WLAN_EID_SSID:
767 bss->ssid_len = min_t(int, IEEE80211_MAX_SSID_LEN, pos[1]);
768 memcpy(bss->ssid, pos + 2, bss->ssid_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100769 lbs_deb_scan("got SSID IE: '%s', len %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400770 print_ssid(ssid, bss->ssid, bss->ssid_len),
Dan Williamsd8efea22007-05-28 23:54:55 -0400771 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200772 break;
773
Johannes Berg2c7060022008-10-30 22:09:54 +0100774 case WLAN_EID_SUPP_RATES:
775 n_basic_rates = min_t(uint8_t, MAX_RATES, pos[1]);
776 memcpy(bss->rates, pos + 2, n_basic_rates);
Dan Williams8c512762007-08-02 11:40:45 -0400777 got_basic_rates = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100778 lbs_deb_scan("got RATES IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200779 break;
780
Johannes Berg2c7060022008-10-30 22:09:54 +0100781 case WLAN_EID_FH_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400782 fh = (struct ieee_ie_fh_param_set *) pos;
783 memcpy(&bss->phy.fh, fh, sizeof(*fh));
Holger Schurigffd074f2007-12-07 16:52:10 +0100784 lbs_deb_scan("got FH IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200785 break;
786
Johannes Berg2c7060022008-10-30 22:09:54 +0100787 case WLAN_EID_DS_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400788 ds = (struct ieee_ie_ds_param_set *) pos;
789 bss->channel = ds->channel;
790 memcpy(&bss->phy.ds, ds, sizeof(*ds));
Holger Schurigffd074f2007-12-07 16:52:10 +0100791 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200792 break;
793
Johannes Berg2c7060022008-10-30 22:09:54 +0100794 case WLAN_EID_CF_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400795 cf = (struct ieee_ie_cf_param_set *) pos;
796 memcpy(&bss->ss.cf, cf, sizeof(*cf));
Holger Schurigffd074f2007-12-07 16:52:10 +0100797 lbs_deb_scan("got CF IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200798 break;
799
Johannes Berg2c7060022008-10-30 22:09:54 +0100800 case WLAN_EID_IBSS_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400801 ibss = (struct ieee_ie_ibss_param_set *) pos;
802 bss->atimwindow = ibss->atimwindow;
803 memcpy(&bss->ss.ibss, ibss, sizeof(*ibss));
Holger Schurigffd074f2007-12-07 16:52:10 +0100804 lbs_deb_scan("got IBSS IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200805 break;
806
Johannes Berg2c7060022008-10-30 22:09:54 +0100807 case WLAN_EID_EXT_SUPP_RATES:
Dan Williamsab617972007-08-02 10:48:02 -0400808 /* only process extended supported rate if data rate is
809 * already found. Data rate IE should come before
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200810 * extended supported rate IE
811 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100812 lbs_deb_scan("got RATESEX IE\n");
813 if (!got_basic_rates) {
814 lbs_deb_scan("... but ignoring it\n");
Dan Williamsab617972007-08-02 10:48:02 -0400815 break;
Holger Schurigffd074f2007-12-07 16:52:10 +0100816 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200817
Johannes Berg2c7060022008-10-30 22:09:54 +0100818 n_ex_rates = pos[1];
Dan Williams8c512762007-08-02 11:40:45 -0400819 if (n_basic_rates + n_ex_rates > MAX_RATES)
820 n_ex_rates = MAX_RATES - n_basic_rates;
Dan Williamsab617972007-08-02 10:48:02 -0400821
Dan Williams8c512762007-08-02 11:40:45 -0400822 p = bss->rates + n_basic_rates;
Johannes Berg2c7060022008-10-30 22:09:54 +0100823 memcpy(p, pos + 2, n_ex_rates);
Dan Williamsab617972007-08-02 10:48:02 -0400824 break;
825
Johannes Berg2c7060022008-10-30 22:09:54 +0100826 case WLAN_EID_GENERIC:
827 if (pos[1] >= 4 &&
828 pos[2] == 0x00 && pos[3] == 0x50 &&
829 pos[4] == 0xf2 && pos[5] == 0x01) {
830 bss->wpa_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
831 memcpy(bss->wpa_ie, pos, bss->wpa_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100832 lbs_deb_scan("got WPA IE\n");
Johannes Berg2c7060022008-10-30 22:09:54 +0100833 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie,
834 bss->wpa_ie_len);
835 } else if (pos[1] >= MARVELL_MESH_IE_LENGTH &&
836 pos[2] == 0x00 && pos[3] == 0x50 &&
Roel Kluinbaf62ee2009-02-03 17:15:57 +0100837 pos[4] == 0x43 && pos[5] == 0x04) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100838 lbs_deb_scan("got mesh IE\n");
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400839 bss->mesh = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100840 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100841 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
Johannes Berg2c7060022008-10-30 22:09:54 +0100842 pos[2], pos[3],
843 pos[4], pos[5],
844 pos[1]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200845 }
846 break;
847
Johannes Berg2c7060022008-10-30 22:09:54 +0100848 case WLAN_EID_RSN:
Holger Schurigffd074f2007-12-07 16:52:10 +0100849 lbs_deb_scan("got RSN IE\n");
Johannes Berg2c7060022008-10-30 22:09:54 +0100850 bss->rsn_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
851 memcpy(bss->rsn_ie, pos, bss->rsn_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100852 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
Johannes Berg2c7060022008-10-30 22:09:54 +0100853 bss->rsn_ie, bss->rsn_ie_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200854 break;
855
Dan Williamsab617972007-08-02 10:48:02 -0400856 default:
Holger Schurigffd074f2007-12-07 16:52:10 +0100857 lbs_deb_scan("got IE 0x%04x, len %d\n",
Johannes Berg2c7060022008-10-30 22:09:54 +0100858 pos[0], pos[1]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200859 break;
860 }
861
Johannes Berg2c7060022008-10-30 22:09:54 +0100862 pos += pos[1] + 2;
Dan Williamsab617972007-08-02 10:48:02 -0400863 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400864
865 /* Timestamp */
866 bss->last_scanned = jiffies;
Holger Schurig10078322007-11-15 18:05:47 -0500867 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400868
Holger Schurig9012b282007-05-25 11:27:16 -0400869 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200870
Holger Schurig9012b282007-05-25 11:27:16 -0400871done:
872 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
873 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200874}
875
876/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200877 * @brief Send a scan command for all available channels filtered on a spec
878 *
Holger Schurige56188a2007-10-09 14:15:19 +0200879 * Used in association code and from debugfs
880 *
Holger Schurig69f90322007-11-23 15:43:44 +0100881 * @param priv A pointer to struct lbs_private structure
Holger Schurige56188a2007-10-09 14:15:19 +0200882 * @param ssid A pointer to the SSID to scan for
883 * @param ssid_len Length of the SSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200884 *
885 * @return 0-success, otherwise fail
886 */
David Woodhousef137e052008-03-03 12:18:59 +0100887int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100888 uint8_t ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200889{
John W. Linville9387b7c2008-09-30 20:59:05 -0400890 DECLARE_SSID_BUF(ssid_buf);
Dan Williamseb8f7332007-05-25 16:25:21 -0400891 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200892
Holger Schurig52933d82008-03-05 07:05:32 +0100893 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400894 print_ssid(ssid_buf, ssid, ssid_len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200895
Dan Williamsd8efea22007-05-28 23:54:55 -0400896 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -0400897 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200898
Holger Schurig52933d82008-03-05 07:05:32 +0100899 memcpy(priv->scan_ssid, ssid, ssid_len);
900 priv->scan_ssid_len = ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200901
Holger Schurig52933d82008-03-05 07:05:32 +0100902 lbs_scan_networks(priv, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +0000903 if (priv->surpriseremoved) {
Holger Schurige56188a2007-10-09 14:15:19 +0200904 ret = -1;
905 goto out;
906 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200907
Dan Williamseb8f7332007-05-25 16:25:21 -0400908out:
Holger Schurige56188a2007-10-09 14:15:19 +0200909 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Dan Williamseb8f7332007-05-25 16:25:21 -0400910 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200911}
912
Holger Schurige56188a2007-10-09 14:15:19 +0200913
914
915
916/*********************************************************************/
917/* */
918/* Support for Wireless Extensions */
919/* */
920/*********************************************************************/
921
Holger Schurigffd074f2007-12-07 16:52:10 +0100922
Dan Williams00af0152007-08-02 13:14:56 -0400923#define MAX_CUSTOM_LEN 64
924
Holger Schurig69f90322007-11-23 15:43:44 +0100925static inline char *lbs_translate_scan(struct lbs_private *priv,
David S. Millerccc58052008-06-16 18:50:49 -0700926 struct iw_request_info *info,
927 char *start, char *stop,
928 struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400929{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400930 struct chan_freq_power *cfp;
931 char *current_val; /* For rates */
932 struct iw_event iwe; /* Temporary buffer */
933 int j;
David Woodhousef137e052008-03-03 12:18:59 +0100934#define PERFECT_RSSI ((uint8_t)50)
935#define WORST_RSSI ((uint8_t)0)
936#define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
937 uint8_t rssi;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400938
Holger Schurige56188a2007-10-09 14:15:19 +0200939 lbs_deb_enter(LBS_DEB_SCAN);
940
David Woodhouseaa21c002007-12-08 20:04:36 +0000941 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400942 if (!cfp) {
943 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
Holger Schurige56188a2007-10-09 14:15:19 +0200944 start = NULL;
945 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400946 }
947
Holger Schurigffd074f2007-12-07 16:52:10 +0100948 /* First entry *MUST* be the BSSID */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400949 iwe.cmd = SIOCGIWAP;
950 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
951 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -0700952 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400953
954 /* SSID */
955 iwe.cmd = SIOCGIWESSID;
956 iwe.u.data.flags = 1;
Holger Schurig243e84e2009-10-22 15:30:47 +0200957 iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IEEE80211_MAX_SSID_LEN);
David S. Millerccc58052008-06-16 18:50:49 -0700958 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400959
960 /* Mode */
961 iwe.cmd = SIOCGIWMODE;
962 iwe.u.mode = bss->mode;
David S. Millerccc58052008-06-16 18:50:49 -0700963 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400964
965 /* Frequency */
966 iwe.cmd = SIOCGIWFREQ;
967 iwe.u.freq.m = (long)cfp->freq * 100000;
968 iwe.u.freq.e = 1;
David S. Millerccc58052008-06-16 18:50:49 -0700969 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400970
971 /* Add quality statistics */
972 iwe.cmd = IWEVQUAL;
973 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
974 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
975
976 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
977 iwe.u.qual.qual =
David Woodhousef137e052008-03-03 12:18:59 +0100978 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
979 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
980 (RSSI_DIFF * RSSI_DIFF);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400981 if (iwe.u.qual.qual > 100)
982 iwe.u.qual.qual = 100;
983
David Woodhouseaa21c002007-12-08 20:04:36 +0000984 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400985 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
986 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100987 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400988 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400989
Dan Williams80e78ef2007-05-25 22:18:47 -0400990 /* Locally created ad-hoc BSSs won't have beacons if this is the
991 * only station in the adhoc network; so get signal strength
992 * from receive statistics.
993 */
David Woodhousef137e052008-03-03 12:18:59 +0100994 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +0000995 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +0100996 priv->curbssparams.ssid_len,
997 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -0400998 int snr, nf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000999 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1000 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
Dan Williams80e78ef2007-05-25 22:18:47 -04001001 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001002 }
David S. Millerccc58052008-06-16 18:50:49 -07001003 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001004
1005 /* Add encryption capability */
1006 iwe.cmd = SIOCGIWENCODE;
Dan Williams0c9ca692007-08-02 10:43:44 -04001007 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001008 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1009 } else {
1010 iwe.u.data.flags = IW_ENCODE_DISABLED;
1011 }
1012 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -07001013 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001014
David S. Millerccc58052008-06-16 18:50:49 -07001015 current_val = start + iwe_stream_lcp_len(info);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001016
1017 iwe.cmd = SIOCGIWRATE;
1018 iwe.u.bitrate.fixed = 0;
1019 iwe.u.bitrate.disabled = 0;
1020 iwe.u.bitrate.value = 0;
1021
Roel Kluin430453f2009-07-28 09:59:47 +02001022 for (j = 0; j < ARRAY_SIZE(bss->rates) && bss->rates[j]; j++) {
Dan Williams8c512762007-08-02 11:40:45 -04001023 /* Bit rate given in 500 kb/s units */
1024 iwe.u.bitrate.value = bss->rates[j] * 500000;
David S. Millerccc58052008-06-16 18:50:49 -07001025 current_val = iwe_stream_add_value(info, start, current_val,
1026 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001027 }
David Woodhousef137e052008-03-03 12:18:59 +01001028 if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +00001029 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +01001030 priv->curbssparams.ssid_len,
1031 bss->ssid, bss->ssid_len)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001032 iwe.u.bitrate.value = 22 * 500000;
David S. Millerccc58052008-06-16 18:50:49 -07001033 current_val = iwe_stream_add_value(info, start, current_val,
David Woodhousef137e052008-03-03 12:18:59 +01001034 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001035 }
1036 /* Check if we added any event */
David S. Millerccc58052008-06-16 18:50:49 -07001037 if ((current_val - start) > iwe_stream_lcp_len(info))
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001038 start = current_val;
1039
1040 memset(&iwe, 0, sizeof(iwe));
1041 if (bss->wpa_ie_len) {
1042 char buf[MAX_WPA_IE_LEN];
1043 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
1044 iwe.cmd = IWEVGENIE;
1045 iwe.u.data.length = bss->wpa_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07001046 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001047 }
1048
1049 memset(&iwe, 0, sizeof(iwe));
1050 if (bss->rsn_ie_len) {
1051 char buf[MAX_WPA_IE_LEN];
1052 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
1053 iwe.cmd = IWEVGENIE;
1054 iwe.u.data.length = bss->rsn_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07001055 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001056 }
1057
Dan Williams00af0152007-08-02 13:14:56 -04001058 if (bss->mesh) {
1059 char custom[MAX_CUSTOM_LEN];
1060 char *p = custom;
1061
1062 iwe.cmd = IWEVCUSTOM;
David Woodhousef137e052008-03-03 12:18:59 +01001063 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
Dan Williams00af0152007-08-02 13:14:56 -04001064 iwe.u.data.length = p - custom;
1065 if (iwe.u.data.length)
David S. Millerccc58052008-06-16 18:50:49 -07001066 start = iwe_stream_add_point(info, start, stop,
1067 &iwe, custom);
Dan Williams00af0152007-08-02 13:14:56 -04001068 }
1069
Holger Schurige56188a2007-10-09 14:15:19 +02001070out:
1071 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001072 return start;
1073}
1074
Holger Schurigffd074f2007-12-07 16:52:10 +01001075
1076/**
1077 * @brief Handle Scan Network ioctl
1078 *
1079 * @param dev A pointer to net_device structure
1080 * @param info A pointer to iw_request_info structure
1081 * @param vwrq A pointer to iw_param structure
1082 * @param extra A pointer to extra data buf
1083 *
1084 * @return 0 --success, otherwise fail
1085 */
1086int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
Holger Schurig52933d82008-03-05 07:05:32 +01001087 union iwreq_data *wrqu, char *extra)
Holger Schurigffd074f2007-12-07 16:52:10 +01001088{
John W. Linville9387b7c2008-09-30 20:59:05 -04001089 DECLARE_SSID_BUF(ssid);
Kiran Divekarab65f642009-02-19 19:32:39 -05001090 struct lbs_private *priv = dev->ml_priv;
Holger Schurig52933d82008-03-05 07:05:32 +01001091 int ret = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +01001092
Holger Schurig52933d82008-03-05 07:05:32 +01001093 lbs_deb_enter(LBS_DEB_WEXT);
Holger Schurigffd074f2007-12-07 16:52:10 +01001094
Dan Williamsd5db2df2008-08-21 17:51:07 -04001095 if (!priv->radio_on) {
1096 ret = -EINVAL;
1097 goto out;
1098 }
1099
Holger Schurig52933d82008-03-05 07:05:32 +01001100 if (!netif_running(dev)) {
1101 ret = -ENETDOWN;
1102 goto out;
1103 }
Holger Schurigffd074f2007-12-07 16:52:10 +01001104
1105 /* mac80211 does this:
1106 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Holger Schurig52933d82008-03-05 07:05:32 +01001107 if (sdata->type != IEEE80211_IF_TYPE_xxx) {
1108 ret = -EOPNOTSUPP;
1109 goto out;
1110 }
1111 */
Holger Schurigffd074f2007-12-07 16:52:10 +01001112
1113 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
1114 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Holger Schurig52933d82008-03-05 07:05:32 +01001115 struct iw_scan_req *req = (struct iw_scan_req *)extra;
1116 priv->scan_ssid_len = req->essid_len;
1117 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
1118 lbs_deb_wext("set_scan, essid '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04001119 print_ssid(ssid, priv->scan_ssid, priv->scan_ssid_len));
Holger Schurig52933d82008-03-05 07:05:32 +01001120 } else {
1121 priv->scan_ssid_len = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +01001122 }
Holger Schurigffd074f2007-12-07 16:52:10 +01001123
1124 if (!delayed_work_pending(&priv->scan_work))
1125 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +01001126 msecs_to_jiffies(50));
Holger Schurigffd074f2007-12-07 16:52:10 +01001127 /* set marker that currently a scan is taking place */
Holger Schurig8816edc2008-01-28 17:28:05 +01001128 priv->scan_channel = -1;
Holger Schurigffd074f2007-12-07 16:52:10 +01001129
David Woodhouseaa21c002007-12-08 20:04:36 +00001130 if (priv->surpriseremoved)
Holger Schurig52933d82008-03-05 07:05:32 +01001131 ret = -EIO;
Holger Schurigffd074f2007-12-07 16:52:10 +01001132
Holger Schurig52933d82008-03-05 07:05:32 +01001133out:
1134 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1135 return ret;
Holger Schurigffd074f2007-12-07 16:52:10 +01001136}
1137
1138
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001139/**
Holger Schurige56188a2007-10-09 14:15:19 +02001140 * @brief Handle Retrieve scan table ioctl
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001141 *
1142 * @param dev A pointer to net_device structure
1143 * @param info A pointer to iw_request_info structure
1144 * @param dwrq A pointer to iw_point structure
1145 * @param extra A pointer to extra data buf
1146 *
1147 * @return 0 --success, otherwise fail
1148 */
Holger Schurig10078322007-11-15 18:05:47 -05001149int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
David Woodhousef137e052008-03-03 12:18:59 +01001150 struct iw_point *dwrq, char *extra)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001151{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001152#define SCAN_ITEM_SIZE 128
Kiran Divekarab65f642009-02-19 19:32:39 -05001153 struct lbs_private *priv = dev->ml_priv;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001154 int err = 0;
1155 char *ev = extra;
1156 char *stop = ev + dwrq->length;
David Woodhousef137e052008-03-03 12:18:59 +01001157 struct bss_descriptor *iter_bss;
1158 struct bss_descriptor *safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001159
Holger Schurig52933d82008-03-05 07:05:32 +01001160 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001161
Holger Schurigffd074f2007-12-07 16:52:10 +01001162 /* iwlist should wait until the current scan is finished */
Holger Schurig8816edc2008-01-28 17:28:05 +01001163 if (priv->scan_channel)
Holger Schurigffd074f2007-12-07 16:52:10 +01001164 return -EAGAIN;
1165
Dan Williams80e78ef2007-05-25 22:18:47 -04001166 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
Amitkumar Karwarc0bbd572009-10-08 19:38:45 -07001167 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate) {
1168 err = lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
1169 CMD_OPTION_WAITFORRSP, 0, NULL);
1170 if (err)
1171 goto out;
1172 }
Dan Williams80e78ef2007-05-25 22:18:47 -04001173
David Woodhouseaa21c002007-12-08 20:04:36 +00001174 mutex_lock(&priv->lock);
1175 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
David Woodhousef137e052008-03-03 12:18:59 +01001176 char *next_ev;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001177 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001178
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001179 if (stop - ev < SCAN_ITEM_SIZE) {
1180 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001181 break;
1182 }
1183
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001184 /* For mesh device, list only mesh networks */
1185 if (dev == priv->mesh_dev && !iter_bss->mesh)
1186 continue;
1187
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001188 /* Prune old an old scan result */
1189 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1190 if (time_after(jiffies, stale_time)) {
David Woodhousef137e052008-03-03 12:18:59 +01001191 list_move_tail(&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001192 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001193 continue;
1194 }
1195
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001196 /* Translate to WE format this entry */
David S. Millerccc58052008-06-16 18:50:49 -07001197 next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001198 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001199 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001200 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001201 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001202 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001203
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001204 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001205 dwrq->flags = 0;
Amitkumar Karwarc0bbd572009-10-08 19:38:45 -07001206out:
Holger Schurig52933d82008-03-05 07:05:32 +01001207 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001208 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001209}
1210
Holger Schurige56188a2007-10-09 14:15:19 +02001211
1212
1213
1214/*********************************************************************/
1215/* */
1216/* Command execution */
1217/* */
1218/*********************************************************************/
1219
1220
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001221/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001222 * @brief This function handles the command response of scan
1223 *
Holger Schurige56188a2007-10-09 14:15:19 +02001224 * Called from handle_cmd_response() in cmdrespc.
1225 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001226 * The response buffer for the scan command has the following
1227 * memory layout:
1228 *
1229 * .-----------------------------------------------------------.
1230 * | header (4 * sizeof(u16)): Standard command response hdr |
1231 * .-----------------------------------------------------------.
1232 * | bufsize (u16) : sizeof the BSS Description data |
1233 * .-----------------------------------------------------------.
1234 * | NumOfSet (u8) : Number of BSS Descs returned |
1235 * .-----------------------------------------------------------.
1236 * | BSSDescription data (variable, size given in bufsize) |
1237 * .-----------------------------------------------------------.
1238 * | TLV data (variable, size calculated using header->size, |
1239 * | bufsize and sizeof the fixed fields above) |
1240 * .-----------------------------------------------------------.
1241 *
Holger Schurig69f90322007-11-23 15:43:44 +01001242 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001243 * @param resp A pointer to cmd_ds_command
1244 *
1245 * @return 0 or -1
1246 */
David Woodhousefa62f992008-03-03 12:18:03 +01001247static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1248 struct cmd_header *resp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001249{
David Woodhousefa62f992008-03-03 12:18:03 +01001250 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
David Woodhousef137e052008-03-03 12:18:59 +01001251 struct bss_descriptor *iter_bss;
1252 struct bss_descriptor *safe;
David Woodhousefa62f992008-03-03 12:18:03 +01001253 uint8_t *bssinfo;
1254 uint16_t scanrespsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001255 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001256 int idx;
1257 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001258 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001259
Holger Schurige56188a2007-10-09 14:15:19 +02001260 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001261
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001262 /* Prune old entries from scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001263 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001264 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1265 if (time_before(jiffies, stale_time))
1266 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +00001267 list_move_tail (&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001268 clear_bss_descriptor(iter_bss);
1269 }
1270
David Woodhousefa62f992008-03-03 12:18:03 +01001271 if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1272 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1273 scanresp->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001274 ret = -1;
1275 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001276 }
1277
Cliff Cai2c5b9e512009-05-27 14:03:09 -04001278 bytesleft = get_unaligned_le16(&scanresp->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001279 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001280
David Woodhousee7240ac2007-12-11 17:54:36 -05001281 scanrespsize = le16_to_cpu(resp->size);
David Woodhousefa62f992008-03-03 12:18:03 +01001282 lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001283
David Woodhousefa62f992008-03-03 12:18:03 +01001284 bssinfo = scanresp->bssdesc_and_tlvbuffer;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001285
1286 /* The size of the TLV buffer is equal to the entire command response
1287 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1288 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
Holger Schurig8ec97cc2009-10-22 15:30:55 +02001289 * response header (sizeof(struct cmd_header))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001290 */
David Woodhousefa62f992008-03-03 12:18:03 +01001291 tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1292 + sizeof(scanresp->nr_sets)
Holger Schurig8ec97cc2009-10-22 15:30:55 +02001293 + sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001294
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001295 /*
David Woodhousefa62f992008-03-03 12:18:03 +01001296 * Process each scan response returned (scanresp->nr_sets). Save
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001297 * the information in the newbssentry and then insert into the
1298 * driver scan table either as an update to an existing entry
1299 * or as an addition at the end of the table
1300 */
David Woodhousefa62f992008-03-03 12:18:03 +01001301 for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001302 struct bss_descriptor new;
David Woodhousefa62f992008-03-03 12:18:03 +01001303 struct bss_descriptor *found = NULL;
1304 struct bss_descriptor *oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001305
1306 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001307 memset(&new, 0, sizeof (struct bss_descriptor));
David Woodhousefa62f992008-03-03 12:18:03 +01001308 if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001309 /* error parsing the scan response, skipped */
1310 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1311 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001312 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001313
1314 /* Try to find this bss in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001315 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001316 if (is_same_network(iter_bss, &new)) {
1317 found = iter_bss;
1318 break;
1319 }
1320
1321 if ((oldest == NULL) ||
1322 (iter_bss->last_scanned < oldest->last_scanned))
1323 oldest = iter_bss;
1324 }
1325
1326 if (found) {
1327 /* found, clear it */
1328 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001329 } else if (!list_empty(&priv->network_free_list)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001330 /* Pull one from the free list */
David Woodhouseaa21c002007-12-08 20:04:36 +00001331 found = list_entry(priv->network_free_list.next,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001332 struct bss_descriptor, list);
David Woodhouseaa21c002007-12-08 20:04:36 +00001333 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001334 } else if (oldest) {
1335 /* If there are no more slots, expire the oldest */
1336 found = oldest;
1337 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001338 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001339 } else {
1340 continue;
1341 }
1342
Johannes Berge1749612008-10-27 15:59:26 -07001343 lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001344
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001345 /* Copy the locally created newbssentry to the scan table */
1346 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001347 }
1348
Holger Schurig9012b282007-05-25 11:27:16 -04001349 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001350
Holger Schurig9012b282007-05-25 11:27:16 -04001351done:
1352 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1353 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001354}