blob: 220361e69cd3adc7a8a2f680457b49cec2711aaa [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 */
John W. Linville7e272fc2008-09-24 18:13:14 -04007#include <linux/types.h>
Roel Kluin430453f2009-07-28 09:59:47 +02008#include <linux/kernel.h>
Dan Williamsfcdb53d2007-05-25 16:15:56 -04009#include <linux/etherdevice.h>
Johannes Berg2c7060022008-10-30 22:09:54 +010010#include <linux/if_arp.h>
Vladimir Davydovac630c22007-09-06 21:45:36 -040011#include <asm/unaligned.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040012#include <net/lib80211.h>
13
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020014#include "host.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020015#include "dev.h"
16#include "scan.h"
Holger Schurig2d465022009-10-22 15:30:48 +020017#include "assoc.h"
Holger Schurige93156e2009-10-22 15:30:58 +020018#include "wext.h"
David Woodhousefa62f992008-03-03 12:18:03 +010019#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020020
21//! Approximate amount of data needed to pass a scan result back to iwlist
22#define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
Holger Schurig243e84e2009-10-22 15:30:47 +020023 + IEEE80211_MAX_SSID_LEN \
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020024 + IW_EV_UINT_LEN \
25 + IW_EV_FREQ_LEN \
26 + IW_EV_QUAL_LEN \
Holger Schurig243e84e2009-10-22 15:30:47 +020027 + IEEE80211_MAX_SSID_LEN \
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020028 + IW_EV_PARAM_LEN \
29 + 40) /* 40 for WPAIE */
30
31//! Memory needed to store a max sized channel List TLV for a firmware scan
Dan Williams75b6a612009-05-22 20:03:09 -040032#define CHAN_TLV_MAX_SIZE (sizeof(struct mrvl_ie_header) \
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020033 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
34 * sizeof(struct chanscanparamset)))
35
36//! Memory needed to store a max number/size SSID TLV for a firmware scan
Dan Williams75b6a612009-05-22 20:03:09 -040037#define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvl_ie_ssid_param_set))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020038
David Woodhousefa62f992008-03-03 12:18:03 +010039//! Maximum memory needed for a cmd_ds_802_11_scan with all TLVs at max
40#define MAX_SCAN_CFG_ALLOC (sizeof(struct cmd_ds_802_11_scan) \
41 + CHAN_TLV_MAX_SIZE + SSID_TLV_MAX_SIZE)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020042
43//! The maximum number of channels the firmware can scan per command
44#define MRVDRV_MAX_CHANNELS_PER_SCAN 14
45
46/**
47 * @brief Number of channels to scan per firmware scan command issuance.
48 *
49 * Number restricted to prevent hitting the limit on the amount of scan data
50 * returned in a single firmware scan command.
51 */
52#define MRVDRV_CHANNELS_PER_SCAN_CMD 4
53
54//! Scan time specified in the channel TLV for each channel for passive scans
55#define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
56
57//! Scan time specified in the channel TLV for each channel for active scans
58#define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
59
Johannes Berg2c7060022008-10-30 22:09:54 +010060#define DEFAULT_MAX_SCAN_AGE (15 * HZ)
61
David Woodhousefa62f992008-03-03 12:18:03 +010062static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
63 struct cmd_header *resp);
Holger Schurige56188a2007-10-09 14:15:19 +020064
65/*********************************************************************/
66/* */
67/* Misc helper functions */
68/* */
69/*********************************************************************/
70
Holger Schurig23ff5032008-01-28 17:27:03 +010071/**
72 * @brief Unsets the MSB on basic rates
73 *
74 * Scan through an array and unset the MSB for basic data rates.
75 *
76 * @param rates buffer of data rates
77 * @param len size of buffer
78 */
79static void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
80{
81 int i;
82
83 for (i = 0; i < len; i++)
84 rates[i] &= 0x7f;
85}
86
87
David Woodhousef137e052008-03-03 12:18:59 +010088static inline void clear_bss_descriptor(struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -040089{
90 /* Don't blow away ->list, just BSS data */
91 memset(bss, 0, offsetof(struct bss_descriptor, list));
92}
93
Holger Schurigffd074f2007-12-07 16:52:10 +010094/**
95 * @brief Compare two SSIDs
96 *
97 * @param ssid1 A pointer to ssid to compare
98 * @param ssid2 A pointer to ssid to compare
99 *
100 * @return 0: ssid is same, otherwise is different
101 */
David Woodhousef137e052008-03-03 12:18:59 +0100102int lbs_ssid_cmp(uint8_t *ssid1, uint8_t ssid1_len, uint8_t *ssid2,
103 uint8_t ssid2_len)
Holger Schurigffd074f2007-12-07 16:52:10 +0100104{
105 if (ssid1_len != ssid2_len)
106 return -1;
107
108 return memcmp(ssid1, ssid2, ssid1_len);
109}
110
Holger Schurigffd074f2007-12-07 16:52:10 +0100111static inline int is_same_network(struct bss_descriptor *src,
112 struct bss_descriptor *dst)
113{
114 /* A network is only a duplicate if the channel, BSSID, and ESSID
115 * all match. We treat all <hidden> with the same BSSID and channel
116 * as one network */
117 return ((src->ssid_len == dst->ssid_len) &&
118 (src->channel == dst->channel) &&
119 !compare_ether_addr(src->bssid, dst->bssid) &&
120 !memcmp(src->ssid, dst->ssid, src->ssid_len));
121}
122
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200123
Holger Schurige56188a2007-10-09 14:15:19 +0200124
Holger Schurig2d465022009-10-22 15:30:48 +0200125/*********************************************************************/
126/* */
127/* Region channel support */
128/* */
129/*********************************************************************/
130
131#define LBS_TX_PWR_DEFAULT 20 /*100mW */
132#define LBS_TX_PWR_US_DEFAULT 20 /*100mW */
133#define LBS_TX_PWR_JP_DEFAULT 16 /*50mW */
134#define LBS_TX_PWR_FR_DEFAULT 20 /*100mW */
135#define LBS_TX_PWR_EMEA_DEFAULT 20 /*100mW */
136
137/* Format { channel, frequency (MHz), maxtxpower } */
138/* band: 'B/G', region: USA FCC/Canada IC */
139static struct chan_freq_power channel_freq_power_US_BG[] = {
140 {1, 2412, LBS_TX_PWR_US_DEFAULT},
141 {2, 2417, LBS_TX_PWR_US_DEFAULT},
142 {3, 2422, LBS_TX_PWR_US_DEFAULT},
143 {4, 2427, LBS_TX_PWR_US_DEFAULT},
144 {5, 2432, LBS_TX_PWR_US_DEFAULT},
145 {6, 2437, LBS_TX_PWR_US_DEFAULT},
146 {7, 2442, LBS_TX_PWR_US_DEFAULT},
147 {8, 2447, LBS_TX_PWR_US_DEFAULT},
148 {9, 2452, LBS_TX_PWR_US_DEFAULT},
149 {10, 2457, LBS_TX_PWR_US_DEFAULT},
150 {11, 2462, LBS_TX_PWR_US_DEFAULT}
151};
152
153/* band: 'B/G', region: Europe ETSI */
154static struct chan_freq_power channel_freq_power_EU_BG[] = {
155 {1, 2412, LBS_TX_PWR_EMEA_DEFAULT},
156 {2, 2417, LBS_TX_PWR_EMEA_DEFAULT},
157 {3, 2422, LBS_TX_PWR_EMEA_DEFAULT},
158 {4, 2427, LBS_TX_PWR_EMEA_DEFAULT},
159 {5, 2432, LBS_TX_PWR_EMEA_DEFAULT},
160 {6, 2437, LBS_TX_PWR_EMEA_DEFAULT},
161 {7, 2442, LBS_TX_PWR_EMEA_DEFAULT},
162 {8, 2447, LBS_TX_PWR_EMEA_DEFAULT},
163 {9, 2452, LBS_TX_PWR_EMEA_DEFAULT},
164 {10, 2457, LBS_TX_PWR_EMEA_DEFAULT},
165 {11, 2462, LBS_TX_PWR_EMEA_DEFAULT},
166 {12, 2467, LBS_TX_PWR_EMEA_DEFAULT},
167 {13, 2472, LBS_TX_PWR_EMEA_DEFAULT}
168};
169
170/* band: 'B/G', region: Spain */
171static struct chan_freq_power channel_freq_power_SPN_BG[] = {
172 {10, 2457, LBS_TX_PWR_DEFAULT},
173 {11, 2462, LBS_TX_PWR_DEFAULT}
174};
175
176/* band: 'B/G', region: France */
177static struct chan_freq_power channel_freq_power_FR_BG[] = {
178 {10, 2457, LBS_TX_PWR_FR_DEFAULT},
179 {11, 2462, LBS_TX_PWR_FR_DEFAULT},
180 {12, 2467, LBS_TX_PWR_FR_DEFAULT},
181 {13, 2472, LBS_TX_PWR_FR_DEFAULT}
182};
183
184/* band: 'B/G', region: Japan */
185static struct chan_freq_power channel_freq_power_JPN_BG[] = {
186 {1, 2412, LBS_TX_PWR_JP_DEFAULT},
187 {2, 2417, LBS_TX_PWR_JP_DEFAULT},
188 {3, 2422, LBS_TX_PWR_JP_DEFAULT},
189 {4, 2427, LBS_TX_PWR_JP_DEFAULT},
190 {5, 2432, LBS_TX_PWR_JP_DEFAULT},
191 {6, 2437, LBS_TX_PWR_JP_DEFAULT},
192 {7, 2442, LBS_TX_PWR_JP_DEFAULT},
193 {8, 2447, LBS_TX_PWR_JP_DEFAULT},
194 {9, 2452, LBS_TX_PWR_JP_DEFAULT},
195 {10, 2457, LBS_TX_PWR_JP_DEFAULT},
196 {11, 2462, LBS_TX_PWR_JP_DEFAULT},
197 {12, 2467, LBS_TX_PWR_JP_DEFAULT},
198 {13, 2472, LBS_TX_PWR_JP_DEFAULT},
199 {14, 2484, LBS_TX_PWR_JP_DEFAULT}
200};
201
202/**
203 * the structure for channel, frequency and power
204 */
205struct region_cfp_table {
206 u8 region;
207 struct chan_freq_power *cfp_BG;
208 int cfp_no_BG;
209};
210
211/**
212 * the structure for the mapping between region and CFP
213 */
214static struct region_cfp_table region_cfp_table[] = {
215 {0x10, /*US FCC */
216 channel_freq_power_US_BG,
217 ARRAY_SIZE(channel_freq_power_US_BG),
218 }
219 ,
220 {0x20, /*CANADA IC */
221 channel_freq_power_US_BG,
222 ARRAY_SIZE(channel_freq_power_US_BG),
223 }
224 ,
225 {0x30, /*EU*/ channel_freq_power_EU_BG,
226 ARRAY_SIZE(channel_freq_power_EU_BG),
227 }
228 ,
229 {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
230 ARRAY_SIZE(channel_freq_power_SPN_BG),
231 }
232 ,
233 {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
234 ARRAY_SIZE(channel_freq_power_FR_BG),
235 }
236 ,
237 {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
238 ARRAY_SIZE(channel_freq_power_JPN_BG),
239 }
240 ,
241/*Add new region here */
242};
243
244/**
245 * @brief This function finds the CFP in
246 * region_cfp_table based on region and band parameter.
247 *
248 * @param region The region code
249 * @param band The band
250 * @param cfp_no A pointer to CFP number
251 * @return A pointer to CFP
252 */
253static struct chan_freq_power *lbs_get_region_cfp_table(u8 region, int *cfp_no)
254{
255 int i, end;
256
257 lbs_deb_enter(LBS_DEB_MAIN);
258
259 end = ARRAY_SIZE(region_cfp_table);
260
261 for (i = 0; i < end ; i++) {
262 lbs_deb_main("region_cfp_table[i].region=%d\n",
263 region_cfp_table[i].region);
264 if (region_cfp_table[i].region == region) {
265 *cfp_no = region_cfp_table[i].cfp_no_BG;
266 lbs_deb_leave(LBS_DEB_MAIN);
267 return region_cfp_table[i].cfp_BG;
268 }
269 }
270
271 lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
272 return NULL;
273}
274
275int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
276{
277 int ret = 0;
278 int i = 0;
279
280 struct chan_freq_power *cfp;
281 int cfp_no;
282
283 lbs_deb_enter(LBS_DEB_MAIN);
284
285 memset(priv->region_channel, 0, sizeof(priv->region_channel));
286
287 cfp = lbs_get_region_cfp_table(region, &cfp_no);
288 if (cfp != NULL) {
289 priv->region_channel[i].nrcfp = cfp_no;
290 priv->region_channel[i].CFP = cfp;
291 } else {
292 lbs_deb_main("wrong region code %#x in band B/G\n",
293 region);
294 ret = -1;
295 goto out;
296 }
297 priv->region_channel[i].valid = 1;
298 priv->region_channel[i].region = region;
299 priv->region_channel[i].band = band;
300 i++;
301out:
302 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
303 return ret;
304}
305
306
307
Holger Schurige56188a2007-10-09 14:15:19 +0200308
Holger Schurige56188a2007-10-09 14:15:19 +0200309/*********************************************************************/
310/* */
311/* Main scanning support */
312/* */
313/*********************************************************************/
314
Holger Schurige56188a2007-10-09 14:15:19 +0200315/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200316 * @brief Create a channel list for the driver to scan based on region info
317 *
Holger Schurig10078322007-11-15 18:05:47 -0500318 * Only used from lbs_scan_setup_scan_config()
Holger Schurige56188a2007-10-09 14:15:19 +0200319 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200320 * Use the driver region/band information to construct a comprehensive list
321 * of channels to scan. This routine is used for any scan that is not
322 * provided a specific channel list to scan.
323 *
Holger Schurig69f90322007-11-23 15:43:44 +0100324 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200325 * @param scanchanlist Output parameter: resulting channel list to scan
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200326 *
327 * @return void
328 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100329static int lbs_scan_create_channel_list(struct lbs_private *priv,
Holger Schurig52933d82008-03-05 07:05:32 +0100330 struct chanscanparamset *scanchanlist)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200331{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200332 struct region_channel *scanregion;
333 struct chan_freq_power *cfp;
334 int rgnidx;
335 int chanidx;
336 int nextchan;
David Woodhousef137e052008-03-03 12:18:59 +0100337 uint8_t scantype;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200338
339 chanidx = 0;
340
341 /* Set the default scan type to the user specified type, will later
342 * be changed to passive on a per channel basis if restricted by
343 * regulatory requirements (11d or 11h)
344 */
Holger Schurig4f2fdaa2007-08-02 13:12:27 -0400345 scantype = CMD_SCAN_TYPE_ACTIVE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200346
David Woodhouseaa21c002007-12-08 20:04:36 +0000347 for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
Holger Schurigd37b4fd2009-10-22 15:30:45 +0200348 if (!priv->region_channel[rgnidx].valid)
349 continue;
350 scanregion = &priv->region_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200351
David Woodhousef137e052008-03-03 12:18:59 +0100352 for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
353 struct chanscanparamset *chan = &scanchanlist[chanidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200354
355 cfp = scanregion->CFP + nextchan;
356
David Woodhousef137e052008-03-03 12:18:59 +0100357 if (scanregion->band == BAND_B || scanregion->band == BAND_G)
358 chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200359
Dan Williams0aef64d2007-08-02 11:31:18 -0400360 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
David Woodhousef137e052008-03-03 12:18:59 +0100361 chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
362 chan->chanscanmode.passivescan = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200363 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100364 chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
365 chan->chanscanmode.passivescan = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200366 }
367
David Woodhousef137e052008-03-03 12:18:59 +0100368 chan->channumber = cfp->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200369 }
370 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100371 return chanidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200372}
373
Holger Schurige56188a2007-10-09 14:15:19 +0200374/*
Holger Schurigffd074f2007-12-07 16:52:10 +0100375 * Add SSID TLV of the form:
376 *
377 * TLV-ID SSID 00 00
378 * length 06 00
379 * ssid 4d 4e 54 45 53 54
380 */
Holger Schurig52933d82008-03-05 07:05:32 +0100381static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv)
Dan Williamseb8f7332007-05-25 16:25:21 -0400382{
Dan Williams75b6a612009-05-22 20:03:09 -0400383 struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
David Woodhousef137e052008-03-03 12:18:59 +0100384
Holger Schurigffd074f2007-12-07 16:52:10 +0100385 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
Holger Schurig52933d82008-03-05 07:05:32 +0100386 ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len);
387 memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len);
388 return sizeof(ssid_tlv->header) + priv->scan_ssid_len;
Holger Schurigffd074f2007-12-07 16:52:10 +0100389}
Dan Williamseb8f7332007-05-25 16:25:21 -0400390
Holger Schurigffd074f2007-12-07 16:52:10 +0100391/*
392 * Add CHANLIST TLV of the form
393 *
394 * TLV-ID CHANLIST 01 01
395 * length 5b 00
396 * channel 1 00 01 00 00 00 64 00
397 * radio type 00
398 * channel 01
399 * scan type 00
400 * min scan time 00 00
401 * max scan time 64 00
402 * channel 2 00 02 00 00 00 64 00
403 * channel 3 00 03 00 00 00 64 00
404 * channel 4 00 04 00 00 00 64 00
405 * channel 5 00 05 00 00 00 64 00
406 * channel 6 00 06 00 00 00 64 00
407 * channel 7 00 07 00 00 00 64 00
408 * channel 8 00 08 00 00 00 64 00
409 * channel 9 00 09 00 00 00 64 00
410 * channel 10 00 0a 00 00 00 64 00
411 * channel 11 00 0b 00 00 00 64 00
412 * channel 12 00 0c 00 00 00 64 00
413 * channel 13 00 0d 00 00 00 64 00
414 *
415 */
David Woodhousef137e052008-03-03 12:18:59 +0100416static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
417 struct chanscanparamset *chan_list,
418 int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100419{
David Woodhousef137e052008-03-03 12:18:59 +0100420 size_t size = sizeof(struct chanscanparamset) *chan_count;
Dan Williams75b6a612009-05-22 20:03:09 -0400421 struct mrvl_ie_chanlist_param_set *chan_tlv = (void *)tlv;
Dan Williamseb8f7332007-05-25 16:25:21 -0400422
Holger Schurigffd074f2007-12-07 16:52:10 +0100423 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
424 memcpy(chan_tlv->chanscanparam, chan_list, size);
425 chan_tlv->header.len = cpu_to_le16(size);
426 return sizeof(chan_tlv->header) + size;
427}
Dan Williamseb8f7332007-05-25 16:25:21 -0400428
Holger Schurigffd074f2007-12-07 16:52:10 +0100429/*
430 * Add RATES TLV of the form
431 *
432 * TLV-ID RATES 01 00
433 * length 0e 00
434 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
435 *
436 * The rates are in lbs_bg_rates[], but for the 802.11b
437 * rates the high bit isn't set.
438 */
David Woodhousef137e052008-03-03 12:18:59 +0100439static int lbs_scan_add_rates_tlv(uint8_t *tlv)
Holger Schurigffd074f2007-12-07 16:52:10 +0100440{
441 int i;
Dan Williams75b6a612009-05-22 20:03:09 -0400442 struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
Holger Schurigffd074f2007-12-07 16:52:10 +0100443
444 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
445 tlv += sizeof(rate_tlv->header);
446 for (i = 0; i < MAX_RATES; i++) {
447 *tlv = lbs_bg_rates[i];
448 if (*tlv == 0)
449 break;
450 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
451 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
452 Note that the values are MBit/s * 2, to mark them as
453 basic rates so that the firmware likes it better */
454 if (*tlv == 0x02 || *tlv == 0x04 ||
455 *tlv == 0x0b || *tlv == 0x16)
456 *tlv |= 0x80;
457 tlv++;
Dan Williamseb8f7332007-05-25 16:25:21 -0400458 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100459 rate_tlv->header.len = cpu_to_le16(i);
460 return sizeof(rate_tlv->header) + i;
461}
Dan Williamseb8f7332007-05-25 16:25:21 -0400462
Holger Schurigffd074f2007-12-07 16:52:10 +0100463/*
464 * Generate the CMD_802_11_SCAN command with the proper tlv
465 * for a bunch of channels.
466 */
David Woodhousefa62f992008-03-03 12:18:03 +0100467static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
Holger Schurig52933d82008-03-05 07:05:32 +0100468 struct chanscanparamset *chan_list, int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100469{
470 int ret = -ENOMEM;
David Woodhousefa62f992008-03-03 12:18:03 +0100471 struct cmd_ds_802_11_scan *scan_cmd;
472 uint8_t *tlv; /* pointer into our current, growing TLV storage area */
Holger Schurigffd074f2007-12-07 16:52:10 +0100473
David Woodhousefa62f992008-03-03 12:18:03 +0100474 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
Holger Schurigc0d43992008-04-29 10:07:56 +0200475 bsstype, chan_list ? chan_list[0].channumber : -1,
476 chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100477
478 /* create the fixed part for scan command */
479 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
480 if (scan_cmd == NULL)
Holger Schurige56188a2007-10-09 14:15:19 +0200481 goto out;
David Woodhousefa62f992008-03-03 12:18:03 +0100482
Holger Schurigffd074f2007-12-07 16:52:10 +0100483 tlv = scan_cmd->tlvbuffer;
Holger Schurig52933d82008-03-05 07:05:32 +0100484 /* TODO: do we need to scan for a specific BSSID?
485 memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
Holger Schurigffd074f2007-12-07 16:52:10 +0100486 scan_cmd->bsstype = bsstype;
Dan Williamseb8f7332007-05-25 16:25:21 -0400487
Holger Schurigffd074f2007-12-07 16:52:10 +0100488 /* add TLVs */
Holger Schurig52933d82008-03-05 07:05:32 +0100489 if (priv->scan_ssid_len)
490 tlv += lbs_scan_add_ssid_tlv(priv, tlv);
Holger Schurigffd074f2007-12-07 16:52:10 +0100491 if (chan_list && chan_count)
492 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
493 tlv += lbs_scan_add_rates_tlv(tlv);
Dan Williamseb8f7332007-05-25 16:25:21 -0400494
Holger Schurigffd074f2007-12-07 16:52:10 +0100495 /* This is the final data we are about to send */
David Woodhousefa62f992008-03-03 12:18:03 +0100496 scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
497 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
498 sizeof(*scan_cmd));
Holger Schurigffd074f2007-12-07 16:52:10 +0100499 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
David Woodhousefa62f992008-03-03 12:18:03 +0100500 tlv - scan_cmd->tlvbuffer);
Dan Williamseb8f7332007-05-25 16:25:21 -0400501
David Woodhousefa62f992008-03-03 12:18:03 +0100502 ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
503 le16_to_cpu(scan_cmd->hdr.size),
504 lbs_ret_80211_scan, 0);
505
Holger Schurige56188a2007-10-09 14:15:19 +0200506out:
Holger Schurigffd074f2007-12-07 16:52:10 +0100507 kfree(scan_cmd);
508 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
509 return ret;
Dan Williamseb8f7332007-05-25 16:25:21 -0400510}
511
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200512/**
513 * @brief Internal function used to start a scan based on an input config
514 *
515 * Use the input user scan configuration information when provided in
516 * order to send the appropriate scan commands to firmware to populate or
517 * update the internal driver scan table
518 *
Holger Schurig69f90322007-11-23 15:43:44 +0100519 * @param priv A pointer to struct lbs_private structure
Holger Schurig52933d82008-03-05 07:05:32 +0100520 * @param full_scan Do a full-scan (blocking)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200521 *
522 * @return 0 or < 0 if error
523 */
Holger Schurig245bf202008-04-02 16:27:42 +0200524int lbs_scan_networks(struct lbs_private *priv, int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200525{
Holger Schurigffd074f2007-12-07 16:52:10 +0100526 int ret = -ENOMEM;
527 struct chanscanparamset *chan_list;
528 struct chanscanparamset *curr_chans;
529 int chan_count;
David Woodhousef137e052008-03-03 12:18:59 +0100530 uint8_t bsstype = CMD_BSS_TYPE_ANY;
Holger Schurigffd074f2007-12-07 16:52:10 +0100531 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
Holger Schurigffd074f2007-12-07 16:52:10 +0100532 union iwreq_data wrqu;
Dan Williamsf8f55102007-05-30 10:12:55 -0400533#ifdef CONFIG_LIBERTAS_DEBUG
Holger Schurigffd074f2007-12-07 16:52:10 +0100534 struct bss_descriptor *iter;
Dan Williamsf8f55102007-05-30 10:12:55 -0400535 int i = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -0400536 DECLARE_SSID_BUF(ssid);
Dan Williamsf8f55102007-05-30 10:12:55 -0400537#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200538
David Woodhousef137e052008-03-03 12:18:59 +0100539 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
Dan Williams2afc0c52007-08-02 13:19:04 -0400540
541 /* Cancel any partial outstanding partial scans if this scan
542 * is a full scan.
543 */
544 if (full_scan && delayed_work_pending(&priv->scan_work))
545 cancel_delayed_work(&priv->scan_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200546
Holger Schurig52933d82008-03-05 07:05:32 +0100547 /* User-specified bsstype or channel list
548 TODO: this can be implemented if some user-space application
549 need the feature. Formerly, it was accessible from debugfs,
550 but then nowhere used.
Holger Schurigffd074f2007-12-07 16:52:10 +0100551 if (user_cfg) {
552 if (user_cfg->bsstype)
Holger Schurig52933d82008-03-05 07:05:32 +0100553 bsstype = user_cfg->bsstype;
554 } */
555
556 lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200557
Holger Schurigffd074f2007-12-07 16:52:10 +0100558 /* Create list of channels to scan */
559 chan_list = kzalloc(sizeof(struct chanscanparamset) *
David Woodhousef137e052008-03-03 12:18:59 +0100560 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
Holger Schurigffd074f2007-12-07 16:52:10 +0100561 if (!chan_list) {
562 lbs_pr_alert("SCAN: chan_list empty\n");
563 goto out;
564 }
565
566 /* We want to scan all channels */
Holger Schurig52933d82008-03-05 07:05:32 +0100567 chan_count = lbs_scan_create_channel_list(priv, chan_list);
Holger Schurigffd074f2007-12-07 16:52:10 +0100568
569 netif_stop_queue(priv->dev);
Samuel Ortiz659c8e52009-12-18 11:36:49 +0100570 if (priv->mesh_dev)
David Woodhousea27b9f92007-12-12 00:41:51 -0500571 netif_stop_queue(priv->mesh_dev);
Holger Schurigffd074f2007-12-07 16:52:10 +0100572
573 /* Prepare to continue an interrupted scan */
Holger Schurig8816edc2008-01-28 17:28:05 +0100574 lbs_deb_scan("chan_count %d, scan_channel %d\n",
575 chan_count, priv->scan_channel);
Holger Schurigffd074f2007-12-07 16:52:10 +0100576 curr_chans = chan_list;
577 /* advance channel list by already-scanned-channels */
Holger Schurig8816edc2008-01-28 17:28:05 +0100578 if (priv->scan_channel > 0) {
579 curr_chans += priv->scan_channel;
580 chan_count -= priv->scan_channel;
Holger Schurigffd074f2007-12-07 16:52:10 +0100581 }
582
583 /* Send scan command(s)
584 * numchannels contains the number of channels we should maximally scan
585 * chan_count is the total number of channels to scan
586 */
587
588 while (chan_count) {
589 int to_scan = min(numchannels, chan_count);
590 lbs_deb_scan("scanning %d of %d channels\n",
David Woodhousef137e052008-03-03 12:18:59 +0100591 to_scan, chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100592 ret = lbs_do_scan(priv, bsstype, curr_chans,
Holger Schurig52933d82008-03-05 07:05:32 +0100593 to_scan);
Holger Schurigffd074f2007-12-07 16:52:10 +0100594 if (ret) {
595 lbs_pr_err("SCAN_CMD failed\n");
596 goto out2;
597 }
598 curr_chans += to_scan;
599 chan_count -= to_scan;
600
601 /* somehow schedule the next part of the scan */
David Woodhousef137e052008-03-03 12:18:59 +0100602 if (chan_count && !full_scan &&
David Woodhouseaa21c002007-12-08 20:04:36 +0000603 !priv->surpriseremoved) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100604 /* -1 marks just that we're currently scanning */
Holger Schurig8816edc2008-01-28 17:28:05 +0100605 if (priv->scan_channel < 0)
606 priv->scan_channel = to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100607 else
Holger Schurig8816edc2008-01-28 17:28:05 +0100608 priv->scan_channel += to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100609 cancel_delayed_work(&priv->scan_work);
610 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100611 msecs_to_jiffies(300));
Holger Schurigffd074f2007-12-07 16:52:10 +0100612 /* skip over GIWSCAN event */
613 goto out;
614 }
615
616 }
617 memset(&wrqu, 0, sizeof(union iwreq_data));
618 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200619
Dan Williamsf8f55102007-05-30 10:12:55 -0400620#ifdef CONFIG_LIBERTAS_DEBUG
621 /* Dump the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000622 mutex_lock(&priv->lock);
Holger Schurigffd074f2007-12-07 16:52:10 +0100623 lbs_deb_scan("scan table:\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000624 list_for_each_entry(iter, &priv->network_list, list)
Johannes Berge1749612008-10-27 15:59:26 -0700625 lbs_deb_scan("%02d: BSSID %pM, RSSI %d, SSID '%s'\n",
626 i++, iter->bssid, iter->rssi,
John W. Linville9387b7c2008-09-30 20:59:05 -0400627 print_ssid(ssid, iter->ssid, iter->ssid_len));
David Woodhouseaa21c002007-12-08 20:04:36 +0000628 mutex_unlock(&priv->lock);
Dan Williamsf8f55102007-05-30 10:12:55 -0400629#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200630
Holger Schurigffd074f2007-12-07 16:52:10 +0100631out2:
Holger Schurig8816edc2008-01-28 17:28:05 +0100632 priv->scan_channel = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100633
634out:
Samuel Ortiz659c8e52009-12-18 11:36:49 +0100635 if (priv->connect_status == LBS_CONNECTED && !priv->tx_pending_len)
636 netif_wake_queue(priv->dev);
637
John W. Linville891dc5e2009-12-30 15:25:08 -0500638 if (priv->mesh_dev && lbs_mesh_connected(priv) &&
Samuel Ortiz659c8e52009-12-18 11:36:49 +0100639 !priv->tx_pending_len)
640 netif_wake_queue(priv->mesh_dev);
641
Holger Schurigffd074f2007-12-07 16:52:10 +0100642 kfree(chan_list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200643
Holger Schurig9012b282007-05-25 11:27:16 -0400644 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200645 return ret;
646}
647
Holger Schurig52933d82008-03-05 07:05:32 +0100648void lbs_scan_worker(struct work_struct *work)
649{
650 struct lbs_private *priv =
651 container_of(work, struct lbs_private, scan_work.work);
652
653 lbs_deb_enter(LBS_DEB_SCAN);
654 lbs_scan_networks(priv, 0);
655 lbs_deb_leave(LBS_DEB_SCAN);
656}
657
658
Holger Schurigffd074f2007-12-07 16:52:10 +0100659/*********************************************************************/
660/* */
661/* Result interpretation */
662/* */
663/*********************************************************************/
664
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200665/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200666 * @brief Interpret a BSS scan response returned from the firmware
667 *
668 * Parse the various fixed fields and IEs passed back for a a BSS probe
Holger Schurigffd074f2007-12-07 16:52:10 +0100669 * response or beacon from the scan command. Record information as needed
670 * in the scan table struct bss_descriptor for that entry.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200671 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400672 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200673 *
674 * @return 0 or -1
675 */
Holger Schurig10078322007-11-15 18:05:47 -0500676static int lbs_process_bss(struct bss_descriptor *bss,
David Woodhousef137e052008-03-03 12:18:59 +0100677 uint8_t **pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200678{
Dan Williams5fd164e2009-05-22 20:01:21 -0400679 struct ieee_ie_fh_param_set *fh;
680 struct ieee_ie_ds_param_set *ds;
681 struct ieee_ie_cf_param_set *cf;
682 struct ieee_ie_ibss_param_set *ibss;
John W. Linville9387b7c2008-09-30 20:59:05 -0400683 DECLARE_SSID_BUF(ssid);
David Woodhousef137e052008-03-03 12:18:59 +0100684 uint8_t *pos, *end, *p;
685 uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
686 uint16_t beaconsize = 0;
Holger Schurig9012b282007-05-25 11:27:16 -0400687 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200688
Holger Schurige56188a2007-10-09 14:15:19 +0200689 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200690
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200691 if (*bytesleft >= sizeof(beaconsize)) {
692 /* Extract & convert beacon size from the command buffer */
Harvey Harrison533dd1b2008-04-29 01:03:36 -0700693 beaconsize = get_unaligned_le16(*pbeaconinfo);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200694 *bytesleft -= sizeof(beaconsize);
695 *pbeaconinfo += sizeof(beaconsize);
696 }
697
698 if (beaconsize == 0 || beaconsize > *bytesleft) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200699 *pbeaconinfo += *bytesleft;
700 *bytesleft = 0;
Holger Schurige56188a2007-10-09 14:15:19 +0200701 ret = -1;
702 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200703 }
704
705 /* Initialize the current working beacon pointer for this BSS iteration */
Dan Williamsab617972007-08-02 10:48:02 -0400706 pos = *pbeaconinfo;
707 end = pos + beaconsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200708
709 /* Advance the return beacon pointer past the current beacon */
710 *pbeaconinfo += beaconsize;
711 *bytesleft -= beaconsize;
712
Dan Williamsab617972007-08-02 10:48:02 -0400713 memcpy(bss->bssid, pos, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -0700714 lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid);
Dan Williamsab617972007-08-02 10:48:02 -0400715 pos += ETH_ALEN;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200716
Dan Williamsab617972007-08-02 10:48:02 -0400717 if ((end - pos) < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400718 lbs_deb_scan("process_bss: Not enough bytes left\n");
Holger Schurige56188a2007-10-09 14:15:19 +0200719 ret = -1;
720 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200721 }
722
723 /*
724 * next 4 fields are RSSI, time stamp, beacon interval,
725 * and capability information
726 */
727
728 /* RSSI is 1 byte long */
Dan Williamsab617972007-08-02 10:48:02 -0400729 bss->rssi = *pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100730 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
Dan Williamsab617972007-08-02 10:48:02 -0400731 pos++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200732
733 /* time stamp is 8 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400734 pos += 8;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200735
736 /* beacon interval is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300737 bss->beaconperiod = get_unaligned_le16(pos);
Dan Williamsab617972007-08-02 10:48:02 -0400738 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200739
740 /* capability information is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300741 bss->capability = get_unaligned_le16(pos);
Holger Schurigffd074f2007-12-07 16:52:10 +0100742 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
Dan Williamsab617972007-08-02 10:48:02 -0400743 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200744
Dan Williams0c9ca6902007-08-02 10:43:44 -0400745 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
Holger Schurigffd074f2007-12-07 16:52:10 +0100746 lbs_deb_scan("process_bss: WEP enabled\n");
Dan Williams0c9ca6902007-08-02 10:43:44 -0400747 if (bss->capability & WLAN_CAPABILITY_IBSS)
748 bss->mode = IW_MODE_ADHOC;
749 else
750 bss->mode = IW_MODE_INFRA;
751
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200752 /* rest of the current buffer are IE's */
Holger Schurigffd074f2007-12-07 16:52:10 +0100753 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
Holger Schurigece56192007-08-02 11:53:06 -0400754 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200755
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200756 /* process variable IE */
Dan Williamsab617972007-08-02 10:48:02 -0400757 while (pos <= end - 2) {
Johannes Berg2c7060022008-10-30 22:09:54 +0100758 if (pos + pos[1] > end) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400759 lbs_deb_scan("process_bss: error in processing IE, "
David Woodhousef137e052008-03-03 12:18:59 +0100760 "bytes left < IE length\n");
Dan Williamsab617972007-08-02 10:48:02 -0400761 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200762 }
763
Johannes Berg2c7060022008-10-30 22:09:54 +0100764 switch (pos[0]) {
765 case WLAN_EID_SSID:
766 bss->ssid_len = min_t(int, IEEE80211_MAX_SSID_LEN, pos[1]);
767 memcpy(bss->ssid, pos + 2, bss->ssid_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100768 lbs_deb_scan("got SSID IE: '%s', len %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400769 print_ssid(ssid, bss->ssid, bss->ssid_len),
Dan Williamsd8efea22007-05-28 23:54:55 -0400770 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200771 break;
772
Johannes Berg2c7060022008-10-30 22:09:54 +0100773 case WLAN_EID_SUPP_RATES:
774 n_basic_rates = min_t(uint8_t, MAX_RATES, pos[1]);
775 memcpy(bss->rates, pos + 2, n_basic_rates);
Dan Williams8c512762007-08-02 11:40:45 -0400776 got_basic_rates = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100777 lbs_deb_scan("got RATES IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200778 break;
779
Johannes Berg2c7060022008-10-30 22:09:54 +0100780 case WLAN_EID_FH_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400781 fh = (struct ieee_ie_fh_param_set *) pos;
782 memcpy(&bss->phy.fh, fh, sizeof(*fh));
Holger Schurigffd074f2007-12-07 16:52:10 +0100783 lbs_deb_scan("got FH IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200784 break;
785
Johannes Berg2c7060022008-10-30 22:09:54 +0100786 case WLAN_EID_DS_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400787 ds = (struct ieee_ie_ds_param_set *) pos;
788 bss->channel = ds->channel;
789 memcpy(&bss->phy.ds, ds, sizeof(*ds));
Holger Schurigffd074f2007-12-07 16:52:10 +0100790 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200791 break;
792
Johannes Berg2c7060022008-10-30 22:09:54 +0100793 case WLAN_EID_CF_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400794 cf = (struct ieee_ie_cf_param_set *) pos;
795 memcpy(&bss->ss.cf, cf, sizeof(*cf));
Holger Schurigffd074f2007-12-07 16:52:10 +0100796 lbs_deb_scan("got CF IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200797 break;
798
Johannes Berg2c7060022008-10-30 22:09:54 +0100799 case WLAN_EID_IBSS_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400800 ibss = (struct ieee_ie_ibss_param_set *) pos;
801 bss->atimwindow = ibss->atimwindow;
802 memcpy(&bss->ss.ibss, ibss, sizeof(*ibss));
Holger Schurigffd074f2007-12-07 16:52:10 +0100803 lbs_deb_scan("got IBSS IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200804 break;
805
Johannes Berg2c7060022008-10-30 22:09:54 +0100806 case WLAN_EID_EXT_SUPP_RATES:
Dan Williamsab617972007-08-02 10:48:02 -0400807 /* only process extended supported rate if data rate is
808 * already found. Data rate IE should come before
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200809 * extended supported rate IE
810 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100811 lbs_deb_scan("got RATESEX IE\n");
812 if (!got_basic_rates) {
813 lbs_deb_scan("... but ignoring it\n");
Dan Williamsab617972007-08-02 10:48:02 -0400814 break;
Holger Schurigffd074f2007-12-07 16:52:10 +0100815 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200816
Johannes Berg2c7060022008-10-30 22:09:54 +0100817 n_ex_rates = pos[1];
Dan Williams8c512762007-08-02 11:40:45 -0400818 if (n_basic_rates + n_ex_rates > MAX_RATES)
819 n_ex_rates = MAX_RATES - n_basic_rates;
Dan Williamsab617972007-08-02 10:48:02 -0400820
Dan Williams8c512762007-08-02 11:40:45 -0400821 p = bss->rates + n_basic_rates;
Johannes Berg2c7060022008-10-30 22:09:54 +0100822 memcpy(p, pos + 2, n_ex_rates);
Dan Williamsab617972007-08-02 10:48:02 -0400823 break;
824
Johannes Berg2c7060022008-10-30 22:09:54 +0100825 case WLAN_EID_GENERIC:
826 if (pos[1] >= 4 &&
827 pos[2] == 0x00 && pos[3] == 0x50 &&
828 pos[4] == 0xf2 && pos[5] == 0x01) {
829 bss->wpa_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
830 memcpy(bss->wpa_ie, pos, bss->wpa_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100831 lbs_deb_scan("got WPA IE\n");
Johannes Berg2c7060022008-10-30 22:09:54 +0100832 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie,
833 bss->wpa_ie_len);
834 } else if (pos[1] >= MARVELL_MESH_IE_LENGTH &&
835 pos[2] == 0x00 && pos[3] == 0x50 &&
Roel Kluinbaf62ee2009-02-03 17:15:57 +0100836 pos[4] == 0x43 && pos[5] == 0x04) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100837 lbs_deb_scan("got mesh IE\n");
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400838 bss->mesh = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100839 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100840 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
Johannes Berg2c7060022008-10-30 22:09:54 +0100841 pos[2], pos[3],
842 pos[4], pos[5],
843 pos[1]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200844 }
845 break;
846
Johannes Berg2c7060022008-10-30 22:09:54 +0100847 case WLAN_EID_RSN:
Holger Schurigffd074f2007-12-07 16:52:10 +0100848 lbs_deb_scan("got RSN IE\n");
Johannes Berg2c7060022008-10-30 22:09:54 +0100849 bss->rsn_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
850 memcpy(bss->rsn_ie, pos, bss->rsn_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100851 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
Johannes Berg2c7060022008-10-30 22:09:54 +0100852 bss->rsn_ie, bss->rsn_ie_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200853 break;
854
Dan Williamsab617972007-08-02 10:48:02 -0400855 default:
Holger Schurigffd074f2007-12-07 16:52:10 +0100856 lbs_deb_scan("got IE 0x%04x, len %d\n",
Johannes Berg2c7060022008-10-30 22:09:54 +0100857 pos[0], pos[1]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200858 break;
859 }
860
Johannes Berg2c7060022008-10-30 22:09:54 +0100861 pos += pos[1] + 2;
Dan Williamsab617972007-08-02 10:48:02 -0400862 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400863
864 /* Timestamp */
865 bss->last_scanned = jiffies;
Holger Schurig10078322007-11-15 18:05:47 -0500866 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400867
Holger Schurig9012b282007-05-25 11:27:16 -0400868 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200869
Holger Schurig9012b282007-05-25 11:27:16 -0400870done:
871 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
872 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200873}
874
875/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200876 * @brief Send a scan command for all available channels filtered on a spec
877 *
Holger Schurige56188a2007-10-09 14:15:19 +0200878 * Used in association code and from debugfs
879 *
Holger Schurig69f90322007-11-23 15:43:44 +0100880 * @param priv A pointer to struct lbs_private structure
Holger Schurige56188a2007-10-09 14:15:19 +0200881 * @param ssid A pointer to the SSID to scan for
882 * @param ssid_len Length of the SSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200883 *
884 * @return 0-success, otherwise fail
885 */
David Woodhousef137e052008-03-03 12:18:59 +0100886int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100887 uint8_t ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200888{
John W. Linville9387b7c2008-09-30 20:59:05 -0400889 DECLARE_SSID_BUF(ssid_buf);
Dan Williamseb8f7332007-05-25 16:25:21 -0400890 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200891
Holger Schurig52933d82008-03-05 07:05:32 +0100892 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400893 print_ssid(ssid_buf, ssid, ssid_len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200894
Dan Williamsd8efea22007-05-28 23:54:55 -0400895 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -0400896 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200897
Holger Schurig52933d82008-03-05 07:05:32 +0100898 memcpy(priv->scan_ssid, ssid, ssid_len);
899 priv->scan_ssid_len = ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200900
Holger Schurig52933d82008-03-05 07:05:32 +0100901 lbs_scan_networks(priv, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +0000902 if (priv->surpriseremoved) {
Holger Schurige56188a2007-10-09 14:15:19 +0200903 ret = -1;
904 goto out;
905 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200906
Dan Williamseb8f7332007-05-25 16:25:21 -0400907out:
Holger Schurige56188a2007-10-09 14:15:19 +0200908 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Dan Williamseb8f7332007-05-25 16:25:21 -0400909 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200910}
911
Holger Schurige56188a2007-10-09 14:15:19 +0200912
913
914
915/*********************************************************************/
916/* */
917/* Support for Wireless Extensions */
918/* */
919/*********************************************************************/
920
Holger Schurigffd074f2007-12-07 16:52:10 +0100921
Dan Williams00af0152007-08-02 13:14:56 -0400922#define MAX_CUSTOM_LEN 64
923
Holger Schurig69f90322007-11-23 15:43:44 +0100924static inline char *lbs_translate_scan(struct lbs_private *priv,
David S. Millerccc58052008-06-16 18:50:49 -0700925 struct iw_request_info *info,
926 char *start, char *stop,
927 struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400928{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400929 struct chan_freq_power *cfp;
930 char *current_val; /* For rates */
931 struct iw_event iwe; /* Temporary buffer */
932 int j;
David Woodhousef137e052008-03-03 12:18:59 +0100933#define PERFECT_RSSI ((uint8_t)50)
934#define WORST_RSSI ((uint8_t)0)
935#define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
936 uint8_t rssi;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400937
Holger Schurige56188a2007-10-09 14:15:19 +0200938 lbs_deb_enter(LBS_DEB_SCAN);
939
David Woodhouseaa21c002007-12-08 20:04:36 +0000940 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400941 if (!cfp) {
942 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
Holger Schurige56188a2007-10-09 14:15:19 +0200943 start = NULL;
944 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400945 }
946
Holger Schurigffd074f2007-12-07 16:52:10 +0100947 /* First entry *MUST* be the BSSID */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400948 iwe.cmd = SIOCGIWAP;
949 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
950 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -0700951 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400952
953 /* SSID */
954 iwe.cmd = SIOCGIWESSID;
955 iwe.u.data.flags = 1;
Holger Schurig243e84e2009-10-22 15:30:47 +0200956 iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IEEE80211_MAX_SSID_LEN);
David S. Millerccc58052008-06-16 18:50:49 -0700957 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400958
959 /* Mode */
960 iwe.cmd = SIOCGIWMODE;
961 iwe.u.mode = bss->mode;
David S. Millerccc58052008-06-16 18:50:49 -0700962 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400963
964 /* Frequency */
965 iwe.cmd = SIOCGIWFREQ;
966 iwe.u.freq.m = (long)cfp->freq * 100000;
967 iwe.u.freq.e = 1;
David S. Millerccc58052008-06-16 18:50:49 -0700968 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400969
970 /* Add quality statistics */
971 iwe.cmd = IWEVQUAL;
972 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
973 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
974
975 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
976 iwe.u.qual.qual =
David Woodhousef137e052008-03-03 12:18:59 +0100977 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
978 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
979 (RSSI_DIFF * RSSI_DIFF);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400980 if (iwe.u.qual.qual > 100)
981 iwe.u.qual.qual = 100;
982
David Woodhouseaa21c002007-12-08 20:04:36 +0000983 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400984 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
985 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100986 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400987 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400988
Dan Williams80e78ef2007-05-25 22:18:47 -0400989 /* Locally created ad-hoc BSSs won't have beacons if this is the
990 * only station in the adhoc network; so get signal strength
991 * from receive statistics.
992 */
David Woodhousef137e052008-03-03 12:18:59 +0100993 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +0000994 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +0100995 priv->curbssparams.ssid_len,
996 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -0400997 int snr, nf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000998 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
999 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
Dan Williams80e78ef2007-05-25 22:18:47 -04001000 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001001 }
David S. Millerccc58052008-06-16 18:50:49 -07001002 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001003
1004 /* Add encryption capability */
1005 iwe.cmd = SIOCGIWENCODE;
Dan Williams0c9ca6902007-08-02 10:43:44 -04001006 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001007 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1008 } else {
1009 iwe.u.data.flags = IW_ENCODE_DISABLED;
1010 }
1011 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -07001012 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001013
David S. Millerccc58052008-06-16 18:50:49 -07001014 current_val = start + iwe_stream_lcp_len(info);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001015
1016 iwe.cmd = SIOCGIWRATE;
1017 iwe.u.bitrate.fixed = 0;
1018 iwe.u.bitrate.disabled = 0;
1019 iwe.u.bitrate.value = 0;
1020
Roel Kluin430453f2009-07-28 09:59:47 +02001021 for (j = 0; j < ARRAY_SIZE(bss->rates) && bss->rates[j]; j++) {
Dan Williams8c512762007-08-02 11:40:45 -04001022 /* Bit rate given in 500 kb/s units */
1023 iwe.u.bitrate.value = bss->rates[j] * 500000;
David S. Millerccc58052008-06-16 18:50:49 -07001024 current_val = iwe_stream_add_value(info, start, current_val,
1025 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001026 }
David Woodhousef137e052008-03-03 12:18:59 +01001027 if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +00001028 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +01001029 priv->curbssparams.ssid_len,
1030 bss->ssid, bss->ssid_len)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001031 iwe.u.bitrate.value = 22 * 500000;
David S. Millerccc58052008-06-16 18:50:49 -07001032 current_val = iwe_stream_add_value(info, start, current_val,
David Woodhousef137e052008-03-03 12:18:59 +01001033 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001034 }
1035 /* Check if we added any event */
David S. Millerccc58052008-06-16 18:50:49 -07001036 if ((current_val - start) > iwe_stream_lcp_len(info))
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001037 start = current_val;
1038
1039 memset(&iwe, 0, sizeof(iwe));
1040 if (bss->wpa_ie_len) {
1041 char buf[MAX_WPA_IE_LEN];
1042 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
1043 iwe.cmd = IWEVGENIE;
1044 iwe.u.data.length = bss->wpa_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07001045 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001046 }
1047
1048 memset(&iwe, 0, sizeof(iwe));
1049 if (bss->rsn_ie_len) {
1050 char buf[MAX_WPA_IE_LEN];
1051 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
1052 iwe.cmd = IWEVGENIE;
1053 iwe.u.data.length = bss->rsn_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -07001054 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001055 }
1056
Dan Williams00af0152007-08-02 13:14:56 -04001057 if (bss->mesh) {
1058 char custom[MAX_CUSTOM_LEN];
1059 char *p = custom;
1060
1061 iwe.cmd = IWEVCUSTOM;
David Woodhousef137e052008-03-03 12:18:59 +01001062 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
Dan Williams00af0152007-08-02 13:14:56 -04001063 iwe.u.data.length = p - custom;
1064 if (iwe.u.data.length)
David S. Millerccc58052008-06-16 18:50:49 -07001065 start = iwe_stream_add_point(info, start, stop,
1066 &iwe, custom);
Dan Williams00af0152007-08-02 13:14:56 -04001067 }
1068
Holger Schurige56188a2007-10-09 14:15:19 +02001069out:
1070 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001071 return start;
1072}
1073
Holger Schurigffd074f2007-12-07 16:52:10 +01001074
1075/**
1076 * @brief Handle Scan Network ioctl
1077 *
1078 * @param dev A pointer to net_device structure
1079 * @param info A pointer to iw_request_info structure
1080 * @param vwrq A pointer to iw_param structure
1081 * @param extra A pointer to extra data buf
1082 *
1083 * @return 0 --success, otherwise fail
1084 */
1085int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
Holger Schurig52933d82008-03-05 07:05:32 +01001086 union iwreq_data *wrqu, char *extra)
Holger Schurigffd074f2007-12-07 16:52:10 +01001087{
John W. Linville9387b7c2008-09-30 20:59:05 -04001088 DECLARE_SSID_BUF(ssid);
Kiran Divekarab65f642009-02-19 19:32:39 -05001089 struct lbs_private *priv = dev->ml_priv;
Holger Schurig52933d82008-03-05 07:05:32 +01001090 int ret = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +01001091
Holger Schurig52933d82008-03-05 07:05:32 +01001092 lbs_deb_enter(LBS_DEB_WEXT);
Holger Schurigffd074f2007-12-07 16:52:10 +01001093
Dan Williamsd5db2df2008-08-21 17:51:07 -04001094 if (!priv->radio_on) {
1095 ret = -EINVAL;
1096 goto out;
1097 }
1098
Holger Schurig52933d82008-03-05 07:05:32 +01001099 if (!netif_running(dev)) {
1100 ret = -ENETDOWN;
1101 goto out;
1102 }
Holger Schurigffd074f2007-12-07 16:52:10 +01001103
1104 /* mac80211 does this:
1105 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Holger Schurig52933d82008-03-05 07:05:32 +01001106 if (sdata->type != IEEE80211_IF_TYPE_xxx) {
1107 ret = -EOPNOTSUPP;
1108 goto out;
1109 }
1110 */
Holger Schurigffd074f2007-12-07 16:52:10 +01001111
1112 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
1113 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Holger Schurig52933d82008-03-05 07:05:32 +01001114 struct iw_scan_req *req = (struct iw_scan_req *)extra;
1115 priv->scan_ssid_len = req->essid_len;
1116 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
1117 lbs_deb_wext("set_scan, essid '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04001118 print_ssid(ssid, priv->scan_ssid, priv->scan_ssid_len));
Holger Schurig52933d82008-03-05 07:05:32 +01001119 } else {
1120 priv->scan_ssid_len = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +01001121 }
Holger Schurigffd074f2007-12-07 16:52:10 +01001122
1123 if (!delayed_work_pending(&priv->scan_work))
1124 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +01001125 msecs_to_jiffies(50));
Holger Schurigffd074f2007-12-07 16:52:10 +01001126 /* set marker that currently a scan is taking place */
Holger Schurig8816edc2008-01-28 17:28:05 +01001127 priv->scan_channel = -1;
Holger Schurigffd074f2007-12-07 16:52:10 +01001128
David Woodhouseaa21c002007-12-08 20:04:36 +00001129 if (priv->surpriseremoved)
Holger Schurig52933d82008-03-05 07:05:32 +01001130 ret = -EIO;
Holger Schurigffd074f2007-12-07 16:52:10 +01001131
Holger Schurig52933d82008-03-05 07:05:32 +01001132out:
1133 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1134 return ret;
Holger Schurigffd074f2007-12-07 16:52:10 +01001135}
1136
1137
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001138/**
Holger Schurige56188a2007-10-09 14:15:19 +02001139 * @brief Handle Retrieve scan table ioctl
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001140 *
1141 * @param dev A pointer to net_device structure
1142 * @param info A pointer to iw_request_info structure
1143 * @param dwrq A pointer to iw_point structure
1144 * @param extra A pointer to extra data buf
1145 *
1146 * @return 0 --success, otherwise fail
1147 */
Holger Schurig10078322007-11-15 18:05:47 -05001148int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
David Woodhousef137e052008-03-03 12:18:59 +01001149 struct iw_point *dwrq, char *extra)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001150{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001151#define SCAN_ITEM_SIZE 128
Kiran Divekarab65f642009-02-19 19:32:39 -05001152 struct lbs_private *priv = dev->ml_priv;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001153 int err = 0;
1154 char *ev = extra;
1155 char *stop = ev + dwrq->length;
David Woodhousef137e052008-03-03 12:18:59 +01001156 struct bss_descriptor *iter_bss;
1157 struct bss_descriptor *safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001158
Holger Schurig52933d82008-03-05 07:05:32 +01001159 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001160
Holger Schurigffd074f2007-12-07 16:52:10 +01001161 /* iwlist should wait until the current scan is finished */
Holger Schurig8816edc2008-01-28 17:28:05 +01001162 if (priv->scan_channel)
Holger Schurigffd074f2007-12-07 16:52:10 +01001163 return -EAGAIN;
1164
Dan Williams80e78ef2007-05-25 22:18:47 -04001165 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
Amitkumar Karwarc0bbd572009-10-08 19:38:45 -07001166 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate) {
1167 err = lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
1168 CMD_OPTION_WAITFORRSP, 0, NULL);
1169 if (err)
1170 goto out;
1171 }
Dan Williams80e78ef2007-05-25 22:18:47 -04001172
David Woodhouseaa21c002007-12-08 20:04:36 +00001173 mutex_lock(&priv->lock);
1174 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
David Woodhousef137e052008-03-03 12:18:59 +01001175 char *next_ev;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001176 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001177
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001178 if (stop - ev < SCAN_ITEM_SIZE) {
1179 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001180 break;
1181 }
1182
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001183 /* For mesh device, list only mesh networks */
1184 if (dev == priv->mesh_dev && !iter_bss->mesh)
1185 continue;
1186
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001187 /* Prune old an old scan result */
1188 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1189 if (time_after(jiffies, stale_time)) {
David Woodhousef137e052008-03-03 12:18:59 +01001190 list_move_tail(&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001191 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001192 continue;
1193 }
1194
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001195 /* Translate to WE format this entry */
David S. Millerccc58052008-06-16 18:50:49 -07001196 next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001197 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001198 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001199 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001200 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001201 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001202
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001203 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001204 dwrq->flags = 0;
Amitkumar Karwarc0bbd572009-10-08 19:38:45 -07001205out:
Holger Schurig52933d82008-03-05 07:05:32 +01001206 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001207 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001208}
1209
Holger Schurige56188a2007-10-09 14:15:19 +02001210
1211
1212
1213/*********************************************************************/
1214/* */
1215/* Command execution */
1216/* */
1217/*********************************************************************/
1218
1219
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001220/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001221 * @brief This function handles the command response of scan
1222 *
Holger Schurige56188a2007-10-09 14:15:19 +02001223 * Called from handle_cmd_response() in cmdrespc.
1224 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001225 * The response buffer for the scan command has the following
1226 * memory layout:
1227 *
1228 * .-----------------------------------------------------------.
1229 * | header (4 * sizeof(u16)): Standard command response hdr |
1230 * .-----------------------------------------------------------.
1231 * | bufsize (u16) : sizeof the BSS Description data |
1232 * .-----------------------------------------------------------.
1233 * | NumOfSet (u8) : Number of BSS Descs returned |
1234 * .-----------------------------------------------------------.
1235 * | BSSDescription data (variable, size given in bufsize) |
1236 * .-----------------------------------------------------------.
1237 * | TLV data (variable, size calculated using header->size, |
1238 * | bufsize and sizeof the fixed fields above) |
1239 * .-----------------------------------------------------------.
1240 *
Holger Schurig69f90322007-11-23 15:43:44 +01001241 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001242 * @param resp A pointer to cmd_ds_command
1243 *
1244 * @return 0 or -1
1245 */
David Woodhousefa62f992008-03-03 12:18:03 +01001246static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1247 struct cmd_header *resp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001248{
David Woodhousefa62f992008-03-03 12:18:03 +01001249 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
David Woodhousef137e052008-03-03 12:18:59 +01001250 struct bss_descriptor *iter_bss;
1251 struct bss_descriptor *safe;
David Woodhousefa62f992008-03-03 12:18:03 +01001252 uint8_t *bssinfo;
1253 uint16_t scanrespsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001254 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001255 int idx;
1256 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001257 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001258
Holger Schurige56188a2007-10-09 14:15:19 +02001259 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001260
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001261 /* Prune old entries from scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001262 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001263 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1264 if (time_before(jiffies, stale_time))
1265 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +00001266 list_move_tail (&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001267 clear_bss_descriptor(iter_bss);
1268 }
1269
David Woodhousefa62f992008-03-03 12:18:03 +01001270 if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1271 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1272 scanresp->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001273 ret = -1;
1274 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001275 }
1276
Cliff Cai2c5b9e512009-05-27 14:03:09 -04001277 bytesleft = get_unaligned_le16(&scanresp->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001278 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001279
David Woodhousee7240ac2007-12-11 17:54:36 -05001280 scanrespsize = le16_to_cpu(resp->size);
David Woodhousefa62f992008-03-03 12:18:03 +01001281 lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001282
David Woodhousefa62f992008-03-03 12:18:03 +01001283 bssinfo = scanresp->bssdesc_and_tlvbuffer;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001284
1285 /* The size of the TLV buffer is equal to the entire command response
1286 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1287 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
Holger Schurig8ec97cc2009-10-22 15:30:55 +02001288 * response header (sizeof(struct cmd_header))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001289 */
David Woodhousefa62f992008-03-03 12:18:03 +01001290 tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1291 + sizeof(scanresp->nr_sets)
Holger Schurig8ec97cc2009-10-22 15:30:55 +02001292 + sizeof(struct cmd_header));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001293
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001294 /*
David Woodhousefa62f992008-03-03 12:18:03 +01001295 * Process each scan response returned (scanresp->nr_sets). Save
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001296 * the information in the newbssentry and then insert into the
1297 * driver scan table either as an update to an existing entry
1298 * or as an addition at the end of the table
1299 */
David Woodhousefa62f992008-03-03 12:18:03 +01001300 for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001301 struct bss_descriptor new;
David Woodhousefa62f992008-03-03 12:18:03 +01001302 struct bss_descriptor *found = NULL;
1303 struct bss_descriptor *oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001304
1305 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001306 memset(&new, 0, sizeof (struct bss_descriptor));
David Woodhousefa62f992008-03-03 12:18:03 +01001307 if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001308 /* error parsing the scan response, skipped */
1309 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1310 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001311 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001312
1313 /* Try to find this bss in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001314 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001315 if (is_same_network(iter_bss, &new)) {
1316 found = iter_bss;
1317 break;
1318 }
1319
1320 if ((oldest == NULL) ||
1321 (iter_bss->last_scanned < oldest->last_scanned))
1322 oldest = iter_bss;
1323 }
1324
1325 if (found) {
1326 /* found, clear it */
1327 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001328 } else if (!list_empty(&priv->network_free_list)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001329 /* Pull one from the free list */
David Woodhouseaa21c002007-12-08 20:04:36 +00001330 found = list_entry(priv->network_free_list.next,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001331 struct bss_descriptor, list);
David Woodhouseaa21c002007-12-08 20:04:36 +00001332 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001333 } else if (oldest) {
1334 /* If there are no more slots, expire the oldest */
1335 found = oldest;
1336 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001337 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001338 } else {
1339 continue;
1340 }
1341
Johannes Berge1749612008-10-27 15:59:26 -07001342 lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001343
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001344 /* Copy the locally created newbssentry to the scan table */
1345 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001346 }
1347
Holger Schurig9012b282007-05-25 11:27:16 -04001348 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001349
Holger Schurig9012b282007-05-25 11:27:16 -04001350done:
1351 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1352 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001353}