blob: 584ff76bcf8255ae2f6a016ad0f9aea99531ccc8 [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"
15#include "decl.h"
16#include "dev.h"
17#include "scan.h"
David Woodhousefa62f992008-03-03 12:18:03 +010018#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020019
20//! Approximate amount of data needed to pass a scan result back to iwlist
21#define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
22 + IW_ESSID_MAX_SIZE \
23 + IW_EV_UINT_LEN \
24 + IW_EV_FREQ_LEN \
25 + IW_EV_QUAL_LEN \
26 + IW_ESSID_MAX_SIZE \
27 + IW_EV_PARAM_LEN \
28 + 40) /* 40 for WPAIE */
29
30//! Memory needed to store a max sized channel List TLV for a firmware scan
Dan Williams75b6a612009-05-22 20:03:09 -040031#define CHAN_TLV_MAX_SIZE (sizeof(struct mrvl_ie_header) \
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020032 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
33 * sizeof(struct chanscanparamset)))
34
35//! Memory needed to store a max number/size SSID TLV for a firmware scan
Dan Williams75b6a612009-05-22 20:03:09 -040036#define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvl_ie_ssid_param_set))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020037
David Woodhousefa62f992008-03-03 12:18:03 +010038//! Maximum memory needed for a cmd_ds_802_11_scan with all TLVs at max
39#define MAX_SCAN_CFG_ALLOC (sizeof(struct cmd_ds_802_11_scan) \
40 + CHAN_TLV_MAX_SIZE + SSID_TLV_MAX_SIZE)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020041
42//! The maximum number of channels the firmware can scan per command
43#define MRVDRV_MAX_CHANNELS_PER_SCAN 14
44
45/**
46 * @brief Number of channels to scan per firmware scan command issuance.
47 *
48 * Number restricted to prevent hitting the limit on the amount of scan data
49 * returned in a single firmware scan command.
50 */
51#define MRVDRV_CHANNELS_PER_SCAN_CMD 4
52
53//! Scan time specified in the channel TLV for each channel for passive scans
54#define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
55
56//! Scan time specified in the channel TLV for each channel for active scans
57#define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
58
Johannes Berg2c7060022008-10-30 22:09:54 +010059#define DEFAULT_MAX_SCAN_AGE (15 * HZ)
60
David Woodhousefa62f992008-03-03 12:18:03 +010061static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
62 struct cmd_header *resp);
Holger Schurige56188a2007-10-09 14:15:19 +020063
64/*********************************************************************/
65/* */
66/* Misc helper functions */
67/* */
68/*********************************************************************/
69
Holger Schurig23ff5032008-01-28 17:27:03 +010070/**
71 * @brief Unsets the MSB on basic rates
72 *
73 * Scan through an array and unset the MSB for basic data rates.
74 *
75 * @param rates buffer of data rates
76 * @param len size of buffer
77 */
78static void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
79{
80 int i;
81
82 for (i = 0; i < len; i++)
83 rates[i] &= 0x7f;
84}
85
86
David Woodhousef137e052008-03-03 12:18:59 +010087static inline void clear_bss_descriptor(struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -040088{
89 /* Don't blow away ->list, just BSS data */
90 memset(bss, 0, offsetof(struct bss_descriptor, list));
91}
92
Holger Schurigffd074f2007-12-07 16:52:10 +010093/**
94 * @brief Compare two SSIDs
95 *
96 * @param ssid1 A pointer to ssid to compare
97 * @param ssid2 A pointer to ssid to compare
98 *
99 * @return 0: ssid is same, otherwise is different
100 */
David Woodhousef137e052008-03-03 12:18:59 +0100101int lbs_ssid_cmp(uint8_t *ssid1, uint8_t ssid1_len, uint8_t *ssid2,
102 uint8_t ssid2_len)
Holger Schurigffd074f2007-12-07 16:52:10 +0100103{
104 if (ssid1_len != ssid2_len)
105 return -1;
106
107 return memcmp(ssid1, ssid2, ssid1_len);
108}
109
Holger Schurigffd074f2007-12-07 16:52:10 +0100110static inline int is_same_network(struct bss_descriptor *src,
111 struct bss_descriptor *dst)
112{
113 /* A network is only a duplicate if the channel, BSSID, and ESSID
114 * all match. We treat all <hidden> with the same BSSID and channel
115 * as one network */
116 return ((src->ssid_len == dst->ssid_len) &&
117 (src->channel == dst->channel) &&
118 !compare_ether_addr(src->bssid, dst->bssid) &&
119 !memcmp(src->ssid, dst->ssid, src->ssid_len));
120}
121
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200122
Holger Schurige56188a2007-10-09 14:15:19 +0200123
124
Holger Schurige56188a2007-10-09 14:15:19 +0200125/*********************************************************************/
126/* */
127/* Main scanning support */
128/* */
129/*********************************************************************/
130
Holger Schurige56188a2007-10-09 14:15:19 +0200131/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200132 * @brief Create a channel list for the driver to scan based on region info
133 *
Holger Schurig10078322007-11-15 18:05:47 -0500134 * Only used from lbs_scan_setup_scan_config()
Holger Schurige56188a2007-10-09 14:15:19 +0200135 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200136 * Use the driver region/band information to construct a comprehensive list
137 * of channels to scan. This routine is used for any scan that is not
138 * provided a specific channel list to scan.
139 *
Holger Schurig69f90322007-11-23 15:43:44 +0100140 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200141 * @param scanchanlist Output parameter: resulting channel list to scan
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200142 *
143 * @return void
144 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100145static int lbs_scan_create_channel_list(struct lbs_private *priv,
Holger Schurig52933d82008-03-05 07:05:32 +0100146 struct chanscanparamset *scanchanlist)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200147{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200148 struct region_channel *scanregion;
149 struct chan_freq_power *cfp;
150 int rgnidx;
151 int chanidx;
152 int nextchan;
David Woodhousef137e052008-03-03 12:18:59 +0100153 uint8_t scantype;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200154
155 chanidx = 0;
156
157 /* Set the default scan type to the user specified type, will later
158 * be changed to passive on a per channel basis if restricted by
159 * regulatory requirements (11d or 11h)
160 */
Holger Schurig4f2fdaa2007-08-02 13:12:27 -0400161 scantype = CMD_SCAN_TYPE_ACTIVE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200162
David Woodhouseaa21c002007-12-08 20:04:36 +0000163 for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
Holger Schurigd37b4fd2009-10-22 15:30:45 +0200164 if (!priv->region_channel[rgnidx].valid)
165 continue;
166 scanregion = &priv->region_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200167
David Woodhousef137e052008-03-03 12:18:59 +0100168 for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
169 struct chanscanparamset *chan = &scanchanlist[chanidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200170
171 cfp = scanregion->CFP + nextchan;
172
David Woodhousef137e052008-03-03 12:18:59 +0100173 if (scanregion->band == BAND_B || scanregion->band == BAND_G)
174 chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200175
Dan Williams0aef64d2007-08-02 11:31:18 -0400176 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
David Woodhousef137e052008-03-03 12:18:59 +0100177 chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
178 chan->chanscanmode.passivescan = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200179 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100180 chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
181 chan->chanscanmode.passivescan = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200182 }
183
David Woodhousef137e052008-03-03 12:18:59 +0100184 chan->channumber = cfp->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200185 }
186 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100187 return chanidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200188}
189
Holger Schurige56188a2007-10-09 14:15:19 +0200190/*
Holger Schurigffd074f2007-12-07 16:52:10 +0100191 * Add SSID TLV of the form:
192 *
193 * TLV-ID SSID 00 00
194 * length 06 00
195 * ssid 4d 4e 54 45 53 54
196 */
Holger Schurig52933d82008-03-05 07:05:32 +0100197static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv)
Dan Williamseb8f7332007-05-25 16:25:21 -0400198{
Dan Williams75b6a612009-05-22 20:03:09 -0400199 struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
David Woodhousef137e052008-03-03 12:18:59 +0100200
Holger Schurigffd074f2007-12-07 16:52:10 +0100201 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
Holger Schurig52933d82008-03-05 07:05:32 +0100202 ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len);
203 memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len);
204 return sizeof(ssid_tlv->header) + priv->scan_ssid_len;
Holger Schurigffd074f2007-12-07 16:52:10 +0100205}
Dan Williamseb8f7332007-05-25 16:25:21 -0400206
Holger Schurigffd074f2007-12-07 16:52:10 +0100207/*
208 * Add CHANLIST TLV of the form
209 *
210 * TLV-ID CHANLIST 01 01
211 * length 5b 00
212 * channel 1 00 01 00 00 00 64 00
213 * radio type 00
214 * channel 01
215 * scan type 00
216 * min scan time 00 00
217 * max scan time 64 00
218 * channel 2 00 02 00 00 00 64 00
219 * channel 3 00 03 00 00 00 64 00
220 * channel 4 00 04 00 00 00 64 00
221 * channel 5 00 05 00 00 00 64 00
222 * channel 6 00 06 00 00 00 64 00
223 * channel 7 00 07 00 00 00 64 00
224 * channel 8 00 08 00 00 00 64 00
225 * channel 9 00 09 00 00 00 64 00
226 * channel 10 00 0a 00 00 00 64 00
227 * channel 11 00 0b 00 00 00 64 00
228 * channel 12 00 0c 00 00 00 64 00
229 * channel 13 00 0d 00 00 00 64 00
230 *
231 */
David Woodhousef137e052008-03-03 12:18:59 +0100232static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
233 struct chanscanparamset *chan_list,
234 int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100235{
David Woodhousef137e052008-03-03 12:18:59 +0100236 size_t size = sizeof(struct chanscanparamset) *chan_count;
Dan Williams75b6a612009-05-22 20:03:09 -0400237 struct mrvl_ie_chanlist_param_set *chan_tlv = (void *)tlv;
Dan Williamseb8f7332007-05-25 16:25:21 -0400238
Holger Schurigffd074f2007-12-07 16:52:10 +0100239 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
240 memcpy(chan_tlv->chanscanparam, chan_list, size);
241 chan_tlv->header.len = cpu_to_le16(size);
242 return sizeof(chan_tlv->header) + size;
243}
Dan Williamseb8f7332007-05-25 16:25:21 -0400244
Holger Schurigffd074f2007-12-07 16:52:10 +0100245/*
246 * Add RATES TLV of the form
247 *
248 * TLV-ID RATES 01 00
249 * length 0e 00
250 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
251 *
252 * The rates are in lbs_bg_rates[], but for the 802.11b
253 * rates the high bit isn't set.
254 */
David Woodhousef137e052008-03-03 12:18:59 +0100255static int lbs_scan_add_rates_tlv(uint8_t *tlv)
Holger Schurigffd074f2007-12-07 16:52:10 +0100256{
257 int i;
Dan Williams75b6a612009-05-22 20:03:09 -0400258 struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
Holger Schurigffd074f2007-12-07 16:52:10 +0100259
260 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
261 tlv += sizeof(rate_tlv->header);
262 for (i = 0; i < MAX_RATES; i++) {
263 *tlv = lbs_bg_rates[i];
264 if (*tlv == 0)
265 break;
266 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
267 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
268 Note that the values are MBit/s * 2, to mark them as
269 basic rates so that the firmware likes it better */
270 if (*tlv == 0x02 || *tlv == 0x04 ||
271 *tlv == 0x0b || *tlv == 0x16)
272 *tlv |= 0x80;
273 tlv++;
Dan Williamseb8f7332007-05-25 16:25:21 -0400274 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100275 rate_tlv->header.len = cpu_to_le16(i);
276 return sizeof(rate_tlv->header) + i;
277}
Dan Williamseb8f7332007-05-25 16:25:21 -0400278
Holger Schurigffd074f2007-12-07 16:52:10 +0100279/*
280 * Generate the CMD_802_11_SCAN command with the proper tlv
281 * for a bunch of channels.
282 */
David Woodhousefa62f992008-03-03 12:18:03 +0100283static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
Holger Schurig52933d82008-03-05 07:05:32 +0100284 struct chanscanparamset *chan_list, int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100285{
286 int ret = -ENOMEM;
David Woodhousefa62f992008-03-03 12:18:03 +0100287 struct cmd_ds_802_11_scan *scan_cmd;
288 uint8_t *tlv; /* pointer into our current, growing TLV storage area */
Holger Schurigffd074f2007-12-07 16:52:10 +0100289
David Woodhousefa62f992008-03-03 12:18:03 +0100290 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
Holger Schurigc0d43992008-04-29 10:07:56 +0200291 bsstype, chan_list ? chan_list[0].channumber : -1,
292 chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100293
294 /* create the fixed part for scan command */
295 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
296 if (scan_cmd == NULL)
Holger Schurige56188a2007-10-09 14:15:19 +0200297 goto out;
David Woodhousefa62f992008-03-03 12:18:03 +0100298
Holger Schurigffd074f2007-12-07 16:52:10 +0100299 tlv = scan_cmd->tlvbuffer;
Holger Schurig52933d82008-03-05 07:05:32 +0100300 /* TODO: do we need to scan for a specific BSSID?
301 memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
Holger Schurigffd074f2007-12-07 16:52:10 +0100302 scan_cmd->bsstype = bsstype;
Dan Williamseb8f7332007-05-25 16:25:21 -0400303
Holger Schurigffd074f2007-12-07 16:52:10 +0100304 /* add TLVs */
Holger Schurig52933d82008-03-05 07:05:32 +0100305 if (priv->scan_ssid_len)
306 tlv += lbs_scan_add_ssid_tlv(priv, tlv);
Holger Schurigffd074f2007-12-07 16:52:10 +0100307 if (chan_list && chan_count)
308 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
309 tlv += lbs_scan_add_rates_tlv(tlv);
Dan Williamseb8f7332007-05-25 16:25:21 -0400310
Holger Schurigffd074f2007-12-07 16:52:10 +0100311 /* This is the final data we are about to send */
David Woodhousefa62f992008-03-03 12:18:03 +0100312 scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
313 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
314 sizeof(*scan_cmd));
Holger Schurigffd074f2007-12-07 16:52:10 +0100315 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
David Woodhousefa62f992008-03-03 12:18:03 +0100316 tlv - scan_cmd->tlvbuffer);
Dan Williamseb8f7332007-05-25 16:25:21 -0400317
David Woodhousefa62f992008-03-03 12:18:03 +0100318 ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
319 le16_to_cpu(scan_cmd->hdr.size),
320 lbs_ret_80211_scan, 0);
321
Holger Schurige56188a2007-10-09 14:15:19 +0200322out:
Holger Schurigffd074f2007-12-07 16:52:10 +0100323 kfree(scan_cmd);
324 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
325 return ret;
Dan Williamseb8f7332007-05-25 16:25:21 -0400326}
327
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200328/**
329 * @brief Internal function used to start a scan based on an input config
330 *
331 * Use the input user scan configuration information when provided in
332 * order to send the appropriate scan commands to firmware to populate or
333 * update the internal driver scan table
334 *
Holger Schurig69f90322007-11-23 15:43:44 +0100335 * @param priv A pointer to struct lbs_private structure
Holger Schurig52933d82008-03-05 07:05:32 +0100336 * @param full_scan Do a full-scan (blocking)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200337 *
338 * @return 0 or < 0 if error
339 */
Holger Schurig245bf202008-04-02 16:27:42 +0200340int lbs_scan_networks(struct lbs_private *priv, int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200341{
Holger Schurigffd074f2007-12-07 16:52:10 +0100342 int ret = -ENOMEM;
343 struct chanscanparamset *chan_list;
344 struct chanscanparamset *curr_chans;
345 int chan_count;
David Woodhousef137e052008-03-03 12:18:59 +0100346 uint8_t bsstype = CMD_BSS_TYPE_ANY;
Holger Schurigffd074f2007-12-07 16:52:10 +0100347 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
Holger Schurigffd074f2007-12-07 16:52:10 +0100348 union iwreq_data wrqu;
Dan Williamsf8f55102007-05-30 10:12:55 -0400349#ifdef CONFIG_LIBERTAS_DEBUG
Holger Schurigffd074f2007-12-07 16:52:10 +0100350 struct bss_descriptor *iter;
Dan Williamsf8f55102007-05-30 10:12:55 -0400351 int i = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -0400352 DECLARE_SSID_BUF(ssid);
Dan Williamsf8f55102007-05-30 10:12:55 -0400353#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200354
David Woodhousef137e052008-03-03 12:18:59 +0100355 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
Dan Williams2afc0c52007-08-02 13:19:04 -0400356
357 /* Cancel any partial outstanding partial scans if this scan
358 * is a full scan.
359 */
360 if (full_scan && delayed_work_pending(&priv->scan_work))
361 cancel_delayed_work(&priv->scan_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200362
Holger Schurig52933d82008-03-05 07:05:32 +0100363 /* User-specified bsstype or channel list
364 TODO: this can be implemented if some user-space application
365 need the feature. Formerly, it was accessible from debugfs,
366 but then nowhere used.
Holger Schurigffd074f2007-12-07 16:52:10 +0100367 if (user_cfg) {
368 if (user_cfg->bsstype)
Holger Schurig52933d82008-03-05 07:05:32 +0100369 bsstype = user_cfg->bsstype;
370 } */
371
372 lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200373
Holger Schurigffd074f2007-12-07 16:52:10 +0100374 /* Create list of channels to scan */
375 chan_list = kzalloc(sizeof(struct chanscanparamset) *
David Woodhousef137e052008-03-03 12:18:59 +0100376 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
Holger Schurigffd074f2007-12-07 16:52:10 +0100377 if (!chan_list) {
378 lbs_pr_alert("SCAN: chan_list empty\n");
379 goto out;
380 }
381
382 /* We want to scan all channels */
Holger Schurig52933d82008-03-05 07:05:32 +0100383 chan_count = lbs_scan_create_channel_list(priv, chan_list);
Holger Schurigffd074f2007-12-07 16:52:10 +0100384
385 netif_stop_queue(priv->dev);
386 netif_carrier_off(priv->dev);
387 if (priv->mesh_dev) {
David Woodhousea27b9f92007-12-12 00:41:51 -0500388 netif_stop_queue(priv->mesh_dev);
389 netif_carrier_off(priv->mesh_dev);
Holger Schurigffd074f2007-12-07 16:52:10 +0100390 }
391
392 /* Prepare to continue an interrupted scan */
Holger Schurig8816edc2008-01-28 17:28:05 +0100393 lbs_deb_scan("chan_count %d, scan_channel %d\n",
394 chan_count, priv->scan_channel);
Holger Schurigffd074f2007-12-07 16:52:10 +0100395 curr_chans = chan_list;
396 /* advance channel list by already-scanned-channels */
Holger Schurig8816edc2008-01-28 17:28:05 +0100397 if (priv->scan_channel > 0) {
398 curr_chans += priv->scan_channel;
399 chan_count -= priv->scan_channel;
Holger Schurigffd074f2007-12-07 16:52:10 +0100400 }
401
402 /* Send scan command(s)
403 * numchannels contains the number of channels we should maximally scan
404 * chan_count is the total number of channels to scan
405 */
406
407 while (chan_count) {
408 int to_scan = min(numchannels, chan_count);
409 lbs_deb_scan("scanning %d of %d channels\n",
David Woodhousef137e052008-03-03 12:18:59 +0100410 to_scan, chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100411 ret = lbs_do_scan(priv, bsstype, curr_chans,
Holger Schurig52933d82008-03-05 07:05:32 +0100412 to_scan);
Holger Schurigffd074f2007-12-07 16:52:10 +0100413 if (ret) {
414 lbs_pr_err("SCAN_CMD failed\n");
415 goto out2;
416 }
417 curr_chans += to_scan;
418 chan_count -= to_scan;
419
420 /* somehow schedule the next part of the scan */
David Woodhousef137e052008-03-03 12:18:59 +0100421 if (chan_count && !full_scan &&
David Woodhouseaa21c002007-12-08 20:04:36 +0000422 !priv->surpriseremoved) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100423 /* -1 marks just that we're currently scanning */
Holger Schurig8816edc2008-01-28 17:28:05 +0100424 if (priv->scan_channel < 0)
425 priv->scan_channel = to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100426 else
Holger Schurig8816edc2008-01-28 17:28:05 +0100427 priv->scan_channel += to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100428 cancel_delayed_work(&priv->scan_work);
429 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100430 msecs_to_jiffies(300));
Holger Schurigffd074f2007-12-07 16:52:10 +0100431 /* skip over GIWSCAN event */
432 goto out;
433 }
434
435 }
436 memset(&wrqu, 0, sizeof(union iwreq_data));
437 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200438
Dan Williamsf8f55102007-05-30 10:12:55 -0400439#ifdef CONFIG_LIBERTAS_DEBUG
440 /* Dump the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000441 mutex_lock(&priv->lock);
Holger Schurigffd074f2007-12-07 16:52:10 +0100442 lbs_deb_scan("scan table:\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000443 list_for_each_entry(iter, &priv->network_list, list)
Johannes Berge1749612008-10-27 15:59:26 -0700444 lbs_deb_scan("%02d: BSSID %pM, RSSI %d, SSID '%s'\n",
445 i++, iter->bssid, iter->rssi,
John W. Linville9387b7c2008-09-30 20:59:05 -0400446 print_ssid(ssid, iter->ssid, iter->ssid_len));
David Woodhouseaa21c002007-12-08 20:04:36 +0000447 mutex_unlock(&priv->lock);
Dan Williamsf8f55102007-05-30 10:12:55 -0400448#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200449
Holger Schurigffd074f2007-12-07 16:52:10 +0100450out2:
Holger Schurig8816edc2008-01-28 17:28:05 +0100451 priv->scan_channel = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100452
453out:
David Woodhouseaa21c002007-12-08 20:04:36 +0000454 if (priv->connect_status == LBS_CONNECTED) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400455 netif_carrier_on(priv->dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500456 if (!priv->tx_pending_len)
457 netif_wake_queue(priv->dev);
Brajesh Dave01d77d82007-11-20 17:44:14 -0500458 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000459 if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
Brajesh Dave01d77d82007-11-20 17:44:14 -0500460 netif_carrier_on(priv->mesh_dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500461 if (!priv->tx_pending_len)
462 netif_wake_queue(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200463 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100464 kfree(chan_list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200465
Holger Schurig9012b282007-05-25 11:27:16 -0400466 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200467 return ret;
468}
469
Holger Schurig52933d82008-03-05 07:05:32 +0100470void lbs_scan_worker(struct work_struct *work)
471{
472 struct lbs_private *priv =
473 container_of(work, struct lbs_private, scan_work.work);
474
475 lbs_deb_enter(LBS_DEB_SCAN);
476 lbs_scan_networks(priv, 0);
477 lbs_deb_leave(LBS_DEB_SCAN);
478}
479
480
Holger Schurigffd074f2007-12-07 16:52:10 +0100481/*********************************************************************/
482/* */
483/* Result interpretation */
484/* */
485/*********************************************************************/
486
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200487/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200488 * @brief Interpret a BSS scan response returned from the firmware
489 *
490 * Parse the various fixed fields and IEs passed back for a a BSS probe
Holger Schurigffd074f2007-12-07 16:52:10 +0100491 * response or beacon from the scan command. Record information as needed
492 * in the scan table struct bss_descriptor for that entry.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200493 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400494 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200495 *
496 * @return 0 or -1
497 */
Holger Schurig10078322007-11-15 18:05:47 -0500498static int lbs_process_bss(struct bss_descriptor *bss,
David Woodhousef137e052008-03-03 12:18:59 +0100499 uint8_t **pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200500{
Dan Williams5fd164e2009-05-22 20:01:21 -0400501 struct ieee_ie_fh_param_set *fh;
502 struct ieee_ie_ds_param_set *ds;
503 struct ieee_ie_cf_param_set *cf;
504 struct ieee_ie_ibss_param_set *ibss;
John W. Linville9387b7c2008-09-30 20:59:05 -0400505 DECLARE_SSID_BUF(ssid);
Dan Williams75b6a612009-05-22 20:03:09 -0400506 struct ieee_ie_country_info_set *pcountryinfo;
David Woodhousef137e052008-03-03 12:18:59 +0100507 uint8_t *pos, *end, *p;
508 uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
509 uint16_t beaconsize = 0;
Holger Schurig9012b282007-05-25 11:27:16 -0400510 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200511
Holger Schurige56188a2007-10-09 14:15:19 +0200512 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200513
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200514 if (*bytesleft >= sizeof(beaconsize)) {
515 /* Extract & convert beacon size from the command buffer */
Harvey Harrison533dd1b2008-04-29 01:03:36 -0700516 beaconsize = get_unaligned_le16(*pbeaconinfo);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200517 *bytesleft -= sizeof(beaconsize);
518 *pbeaconinfo += sizeof(beaconsize);
519 }
520
521 if (beaconsize == 0 || beaconsize > *bytesleft) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200522 *pbeaconinfo += *bytesleft;
523 *bytesleft = 0;
Holger Schurige56188a2007-10-09 14:15:19 +0200524 ret = -1;
525 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200526 }
527
528 /* Initialize the current working beacon pointer for this BSS iteration */
Dan Williamsab617972007-08-02 10:48:02 -0400529 pos = *pbeaconinfo;
530 end = pos + beaconsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200531
532 /* Advance the return beacon pointer past the current beacon */
533 *pbeaconinfo += beaconsize;
534 *bytesleft -= beaconsize;
535
Dan Williamsab617972007-08-02 10:48:02 -0400536 memcpy(bss->bssid, pos, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -0700537 lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid);
Dan Williamsab617972007-08-02 10:48:02 -0400538 pos += ETH_ALEN;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200539
Dan Williamsab617972007-08-02 10:48:02 -0400540 if ((end - pos) < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400541 lbs_deb_scan("process_bss: Not enough bytes left\n");
Holger Schurige56188a2007-10-09 14:15:19 +0200542 ret = -1;
543 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200544 }
545
546 /*
547 * next 4 fields are RSSI, time stamp, beacon interval,
548 * and capability information
549 */
550
551 /* RSSI is 1 byte long */
Dan Williamsab617972007-08-02 10:48:02 -0400552 bss->rssi = *pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100553 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
Dan Williamsab617972007-08-02 10:48:02 -0400554 pos++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200555
556 /* time stamp is 8 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400557 pos += 8;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200558
559 /* beacon interval is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300560 bss->beaconperiod = get_unaligned_le16(pos);
Dan Williamsab617972007-08-02 10:48:02 -0400561 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200562
563 /* capability information is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300564 bss->capability = get_unaligned_le16(pos);
Holger Schurigffd074f2007-12-07 16:52:10 +0100565 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
Dan Williamsab617972007-08-02 10:48:02 -0400566 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200567
Dan Williams0c9ca692007-08-02 10:43:44 -0400568 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
Holger Schurigffd074f2007-12-07 16:52:10 +0100569 lbs_deb_scan("process_bss: WEP enabled\n");
Dan Williams0c9ca692007-08-02 10:43:44 -0400570 if (bss->capability & WLAN_CAPABILITY_IBSS)
571 bss->mode = IW_MODE_ADHOC;
572 else
573 bss->mode = IW_MODE_INFRA;
574
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200575 /* rest of the current buffer are IE's */
Holger Schurigffd074f2007-12-07 16:52:10 +0100576 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
Holger Schurigece56192007-08-02 11:53:06 -0400577 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200578
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200579 /* process variable IE */
Dan Williamsab617972007-08-02 10:48:02 -0400580 while (pos <= end - 2) {
Johannes Berg2c7060022008-10-30 22:09:54 +0100581 if (pos + pos[1] > end) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400582 lbs_deb_scan("process_bss: error in processing IE, "
David Woodhousef137e052008-03-03 12:18:59 +0100583 "bytes left < IE length\n");
Dan Williamsab617972007-08-02 10:48:02 -0400584 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200585 }
586
Johannes Berg2c7060022008-10-30 22:09:54 +0100587 switch (pos[0]) {
588 case WLAN_EID_SSID:
589 bss->ssid_len = min_t(int, IEEE80211_MAX_SSID_LEN, pos[1]);
590 memcpy(bss->ssid, pos + 2, bss->ssid_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100591 lbs_deb_scan("got SSID IE: '%s', len %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400592 print_ssid(ssid, bss->ssid, bss->ssid_len),
Dan Williamsd8efea22007-05-28 23:54:55 -0400593 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200594 break;
595
Johannes Berg2c7060022008-10-30 22:09:54 +0100596 case WLAN_EID_SUPP_RATES:
597 n_basic_rates = min_t(uint8_t, MAX_RATES, pos[1]);
598 memcpy(bss->rates, pos + 2, n_basic_rates);
Dan Williams8c512762007-08-02 11:40:45 -0400599 got_basic_rates = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100600 lbs_deb_scan("got RATES IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200601 break;
602
Johannes Berg2c7060022008-10-30 22:09:54 +0100603 case WLAN_EID_FH_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400604 fh = (struct ieee_ie_fh_param_set *) pos;
605 memcpy(&bss->phy.fh, fh, sizeof(*fh));
Holger Schurigffd074f2007-12-07 16:52:10 +0100606 lbs_deb_scan("got FH IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200607 break;
608
Johannes Berg2c7060022008-10-30 22:09:54 +0100609 case WLAN_EID_DS_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400610 ds = (struct ieee_ie_ds_param_set *) pos;
611 bss->channel = ds->channel;
612 memcpy(&bss->phy.ds, ds, sizeof(*ds));
Holger Schurigffd074f2007-12-07 16:52:10 +0100613 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200614 break;
615
Johannes Berg2c7060022008-10-30 22:09:54 +0100616 case WLAN_EID_CF_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400617 cf = (struct ieee_ie_cf_param_set *) pos;
618 memcpy(&bss->ss.cf, cf, sizeof(*cf));
Holger Schurigffd074f2007-12-07 16:52:10 +0100619 lbs_deb_scan("got CF IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200620 break;
621
Johannes Berg2c7060022008-10-30 22:09:54 +0100622 case WLAN_EID_IBSS_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400623 ibss = (struct ieee_ie_ibss_param_set *) pos;
624 bss->atimwindow = ibss->atimwindow;
625 memcpy(&bss->ss.ibss, ibss, sizeof(*ibss));
Holger Schurigffd074f2007-12-07 16:52:10 +0100626 lbs_deb_scan("got IBSS IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200627 break;
628
Johannes Berg2c7060022008-10-30 22:09:54 +0100629 case WLAN_EID_COUNTRY:
Dan Williams75b6a612009-05-22 20:03:09 -0400630 pcountryinfo = (struct ieee_ie_country_info_set *) pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100631 lbs_deb_scan("got COUNTRY IE\n");
Dan Williams75b6a612009-05-22 20:03:09 -0400632 if (pcountryinfo->header.len < sizeof(pcountryinfo->countrycode)
633 || pcountryinfo->header.len > 254) {
634 lbs_deb_scan("%s: 11D- Err CountryInfo len %d, min %zd, max 254\n",
635 __func__,
636 pcountryinfo->header.len,
637 sizeof(pcountryinfo->countrycode));
Holger Schurig9012b282007-05-25 11:27:16 -0400638 ret = -1;
639 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200640 }
641
Dan Williams75b6a612009-05-22 20:03:09 -0400642 memcpy(&bss->countryinfo, pcountryinfo,
643 pcountryinfo->header.len + 2);
Holger Schurigece56192007-08-02 11:53:06 -0400644 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
David Woodhousef137e052008-03-03 12:18:59 +0100645 (uint8_t *) pcountryinfo,
Dan Williams75b6a612009-05-22 20:03:09 -0400646 (int) (pcountryinfo->header.len + 2));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200647 break;
648
Johannes Berg2c7060022008-10-30 22:09:54 +0100649 case WLAN_EID_EXT_SUPP_RATES:
Dan Williamsab617972007-08-02 10:48:02 -0400650 /* only process extended supported rate if data rate is
651 * already found. Data rate IE should come before
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200652 * extended supported rate IE
653 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100654 lbs_deb_scan("got RATESEX IE\n");
655 if (!got_basic_rates) {
656 lbs_deb_scan("... but ignoring it\n");
Dan Williamsab617972007-08-02 10:48:02 -0400657 break;
Holger Schurigffd074f2007-12-07 16:52:10 +0100658 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200659
Johannes Berg2c7060022008-10-30 22:09:54 +0100660 n_ex_rates = pos[1];
Dan Williams8c512762007-08-02 11:40:45 -0400661 if (n_basic_rates + n_ex_rates > MAX_RATES)
662 n_ex_rates = MAX_RATES - n_basic_rates;
Dan Williamsab617972007-08-02 10:48:02 -0400663
Dan Williams8c512762007-08-02 11:40:45 -0400664 p = bss->rates + n_basic_rates;
Johannes Berg2c7060022008-10-30 22:09:54 +0100665 memcpy(p, pos + 2, n_ex_rates);
Dan Williamsab617972007-08-02 10:48:02 -0400666 break;
667
Johannes Berg2c7060022008-10-30 22:09:54 +0100668 case WLAN_EID_GENERIC:
669 if (pos[1] >= 4 &&
670 pos[2] == 0x00 && pos[3] == 0x50 &&
671 pos[4] == 0xf2 && pos[5] == 0x01) {
672 bss->wpa_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
673 memcpy(bss->wpa_ie, pos, bss->wpa_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100674 lbs_deb_scan("got WPA IE\n");
Johannes Berg2c7060022008-10-30 22:09:54 +0100675 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie,
676 bss->wpa_ie_len);
677 } else if (pos[1] >= MARVELL_MESH_IE_LENGTH &&
678 pos[2] == 0x00 && pos[3] == 0x50 &&
Roel Kluinbaf62ee2009-02-03 17:15:57 +0100679 pos[4] == 0x43 && pos[5] == 0x04) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100680 lbs_deb_scan("got mesh IE\n");
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400681 bss->mesh = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100682 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100683 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
Johannes Berg2c7060022008-10-30 22:09:54 +0100684 pos[2], pos[3],
685 pos[4], pos[5],
686 pos[1]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200687 }
688 break;
689
Johannes Berg2c7060022008-10-30 22:09:54 +0100690 case WLAN_EID_RSN:
Holger Schurigffd074f2007-12-07 16:52:10 +0100691 lbs_deb_scan("got RSN IE\n");
Johannes Berg2c7060022008-10-30 22:09:54 +0100692 bss->rsn_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
693 memcpy(bss->rsn_ie, pos, bss->rsn_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100694 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
Johannes Berg2c7060022008-10-30 22:09:54 +0100695 bss->rsn_ie, bss->rsn_ie_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200696 break;
697
Dan Williamsab617972007-08-02 10:48:02 -0400698 default:
Holger Schurigffd074f2007-12-07 16:52:10 +0100699 lbs_deb_scan("got IE 0x%04x, len %d\n",
Johannes Berg2c7060022008-10-30 22:09:54 +0100700 pos[0], pos[1]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200701 break;
702 }
703
Johannes Berg2c7060022008-10-30 22:09:54 +0100704 pos += pos[1] + 2;
Dan Williamsab617972007-08-02 10:48:02 -0400705 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400706
707 /* Timestamp */
708 bss->last_scanned = jiffies;
Holger Schurig10078322007-11-15 18:05:47 -0500709 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400710
Holger Schurig9012b282007-05-25 11:27:16 -0400711 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200712
Holger Schurig9012b282007-05-25 11:27:16 -0400713done:
714 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
715 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200716}
717
718/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200719 * @brief Send a scan command for all available channels filtered on a spec
720 *
Holger Schurige56188a2007-10-09 14:15:19 +0200721 * Used in association code and from debugfs
722 *
Holger Schurig69f90322007-11-23 15:43:44 +0100723 * @param priv A pointer to struct lbs_private structure
Holger Schurige56188a2007-10-09 14:15:19 +0200724 * @param ssid A pointer to the SSID to scan for
725 * @param ssid_len Length of the SSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200726 *
727 * @return 0-success, otherwise fail
728 */
David Woodhousef137e052008-03-03 12:18:59 +0100729int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100730 uint8_t ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200731{
John W. Linville9387b7c2008-09-30 20:59:05 -0400732 DECLARE_SSID_BUF(ssid_buf);
Dan Williamseb8f7332007-05-25 16:25:21 -0400733 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200734
Holger Schurig52933d82008-03-05 07:05:32 +0100735 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400736 print_ssid(ssid_buf, ssid, ssid_len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200737
Dan Williamsd8efea22007-05-28 23:54:55 -0400738 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -0400739 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200740
Holger Schurig52933d82008-03-05 07:05:32 +0100741 memcpy(priv->scan_ssid, ssid, ssid_len);
742 priv->scan_ssid_len = ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200743
Holger Schurig52933d82008-03-05 07:05:32 +0100744 lbs_scan_networks(priv, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +0000745 if (priv->surpriseremoved) {
Holger Schurige56188a2007-10-09 14:15:19 +0200746 ret = -1;
747 goto out;
748 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200749
Dan Williamseb8f7332007-05-25 16:25:21 -0400750out:
Holger Schurige56188a2007-10-09 14:15:19 +0200751 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Dan Williamseb8f7332007-05-25 16:25:21 -0400752 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200753}
754
Holger Schurige56188a2007-10-09 14:15:19 +0200755
756
757
758/*********************************************************************/
759/* */
760/* Support for Wireless Extensions */
761/* */
762/*********************************************************************/
763
Holger Schurigffd074f2007-12-07 16:52:10 +0100764
Dan Williams00af0152007-08-02 13:14:56 -0400765#define MAX_CUSTOM_LEN 64
766
Holger Schurig69f90322007-11-23 15:43:44 +0100767static inline char *lbs_translate_scan(struct lbs_private *priv,
David S. Millerccc58052008-06-16 18:50:49 -0700768 struct iw_request_info *info,
769 char *start, char *stop,
770 struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400771{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400772 struct chan_freq_power *cfp;
773 char *current_val; /* For rates */
774 struct iw_event iwe; /* Temporary buffer */
775 int j;
David Woodhousef137e052008-03-03 12:18:59 +0100776#define PERFECT_RSSI ((uint8_t)50)
777#define WORST_RSSI ((uint8_t)0)
778#define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
779 uint8_t rssi;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400780
Holger Schurige56188a2007-10-09 14:15:19 +0200781 lbs_deb_enter(LBS_DEB_SCAN);
782
David Woodhouseaa21c002007-12-08 20:04:36 +0000783 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400784 if (!cfp) {
785 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
Holger Schurige56188a2007-10-09 14:15:19 +0200786 start = NULL;
787 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400788 }
789
Holger Schurigffd074f2007-12-07 16:52:10 +0100790 /* First entry *MUST* be the BSSID */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400791 iwe.cmd = SIOCGIWAP;
792 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
793 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -0700794 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400795
796 /* SSID */
797 iwe.cmd = SIOCGIWESSID;
798 iwe.u.data.flags = 1;
David Woodhousef137e052008-03-03 12:18:59 +0100799 iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IW_ESSID_MAX_SIZE);
David S. Millerccc58052008-06-16 18:50:49 -0700800 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400801
802 /* Mode */
803 iwe.cmd = SIOCGIWMODE;
804 iwe.u.mode = bss->mode;
David S. Millerccc58052008-06-16 18:50:49 -0700805 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400806
807 /* Frequency */
808 iwe.cmd = SIOCGIWFREQ;
809 iwe.u.freq.m = (long)cfp->freq * 100000;
810 iwe.u.freq.e = 1;
David S. Millerccc58052008-06-16 18:50:49 -0700811 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400812
813 /* Add quality statistics */
814 iwe.cmd = IWEVQUAL;
815 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
816 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
817
818 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
819 iwe.u.qual.qual =
David Woodhousef137e052008-03-03 12:18:59 +0100820 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
821 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
822 (RSSI_DIFF * RSSI_DIFF);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400823 if (iwe.u.qual.qual > 100)
824 iwe.u.qual.qual = 100;
825
David Woodhouseaa21c002007-12-08 20:04:36 +0000826 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400827 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
828 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100829 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400830 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400831
Dan Williams80e78ef2007-05-25 22:18:47 -0400832 /* Locally created ad-hoc BSSs won't have beacons if this is the
833 * only station in the adhoc network; so get signal strength
834 * from receive statistics.
835 */
David Woodhousef137e052008-03-03 12:18:59 +0100836 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +0000837 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +0100838 priv->curbssparams.ssid_len,
839 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -0400840 int snr, nf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000841 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
842 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
Dan Williams80e78ef2007-05-25 22:18:47 -0400843 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400844 }
David S. Millerccc58052008-06-16 18:50:49 -0700845 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400846
847 /* Add encryption capability */
848 iwe.cmd = SIOCGIWENCODE;
Dan Williams0c9ca692007-08-02 10:43:44 -0400849 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400850 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
851 } else {
852 iwe.u.data.flags = IW_ENCODE_DISABLED;
853 }
854 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -0700855 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400856
David S. Millerccc58052008-06-16 18:50:49 -0700857 current_val = start + iwe_stream_lcp_len(info);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400858
859 iwe.cmd = SIOCGIWRATE;
860 iwe.u.bitrate.fixed = 0;
861 iwe.u.bitrate.disabled = 0;
862 iwe.u.bitrate.value = 0;
863
Roel Kluin430453f2009-07-28 09:59:47 +0200864 for (j = 0; j < ARRAY_SIZE(bss->rates) && bss->rates[j]; j++) {
Dan Williams8c512762007-08-02 11:40:45 -0400865 /* Bit rate given in 500 kb/s units */
866 iwe.u.bitrate.value = bss->rates[j] * 500000;
David S. Millerccc58052008-06-16 18:50:49 -0700867 current_val = iwe_stream_add_value(info, start, current_val,
868 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400869 }
David Woodhousef137e052008-03-03 12:18:59 +0100870 if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +0000871 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +0100872 priv->curbssparams.ssid_len,
873 bss->ssid, bss->ssid_len)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400874 iwe.u.bitrate.value = 22 * 500000;
David S. Millerccc58052008-06-16 18:50:49 -0700875 current_val = iwe_stream_add_value(info, start, current_val,
David Woodhousef137e052008-03-03 12:18:59 +0100876 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400877 }
878 /* Check if we added any event */
David S. Millerccc58052008-06-16 18:50:49 -0700879 if ((current_val - start) > iwe_stream_lcp_len(info))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400880 start = current_val;
881
882 memset(&iwe, 0, sizeof(iwe));
883 if (bss->wpa_ie_len) {
884 char buf[MAX_WPA_IE_LEN];
885 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
886 iwe.cmd = IWEVGENIE;
887 iwe.u.data.length = bss->wpa_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -0700888 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400889 }
890
891 memset(&iwe, 0, sizeof(iwe));
892 if (bss->rsn_ie_len) {
893 char buf[MAX_WPA_IE_LEN];
894 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
895 iwe.cmd = IWEVGENIE;
896 iwe.u.data.length = bss->rsn_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -0700897 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400898 }
899
Dan Williams00af0152007-08-02 13:14:56 -0400900 if (bss->mesh) {
901 char custom[MAX_CUSTOM_LEN];
902 char *p = custom;
903
904 iwe.cmd = IWEVCUSTOM;
David Woodhousef137e052008-03-03 12:18:59 +0100905 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
Dan Williams00af0152007-08-02 13:14:56 -0400906 iwe.u.data.length = p - custom;
907 if (iwe.u.data.length)
David S. Millerccc58052008-06-16 18:50:49 -0700908 start = iwe_stream_add_point(info, start, stop,
909 &iwe, custom);
Dan Williams00af0152007-08-02 13:14:56 -0400910 }
911
Holger Schurige56188a2007-10-09 14:15:19 +0200912out:
913 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400914 return start;
915}
916
Holger Schurigffd074f2007-12-07 16:52:10 +0100917
918/**
919 * @brief Handle Scan Network ioctl
920 *
921 * @param dev A pointer to net_device structure
922 * @param info A pointer to iw_request_info structure
923 * @param vwrq A pointer to iw_param structure
924 * @param extra A pointer to extra data buf
925 *
926 * @return 0 --success, otherwise fail
927 */
928int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
Holger Schurig52933d82008-03-05 07:05:32 +0100929 union iwreq_data *wrqu, char *extra)
Holger Schurigffd074f2007-12-07 16:52:10 +0100930{
John W. Linville9387b7c2008-09-30 20:59:05 -0400931 DECLARE_SSID_BUF(ssid);
Kiran Divekarab65f642009-02-19 19:32:39 -0500932 struct lbs_private *priv = dev->ml_priv;
Holger Schurig52933d82008-03-05 07:05:32 +0100933 int ret = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100934
Holger Schurig52933d82008-03-05 07:05:32 +0100935 lbs_deb_enter(LBS_DEB_WEXT);
Holger Schurigffd074f2007-12-07 16:52:10 +0100936
Dan Williamsd5db2df2008-08-21 17:51:07 -0400937 if (!priv->radio_on) {
938 ret = -EINVAL;
939 goto out;
940 }
941
Holger Schurig52933d82008-03-05 07:05:32 +0100942 if (!netif_running(dev)) {
943 ret = -ENETDOWN;
944 goto out;
945 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100946
947 /* mac80211 does this:
948 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Holger Schurig52933d82008-03-05 07:05:32 +0100949 if (sdata->type != IEEE80211_IF_TYPE_xxx) {
950 ret = -EOPNOTSUPP;
951 goto out;
952 }
953 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100954
955 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
956 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Holger Schurig52933d82008-03-05 07:05:32 +0100957 struct iw_scan_req *req = (struct iw_scan_req *)extra;
958 priv->scan_ssid_len = req->essid_len;
959 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
960 lbs_deb_wext("set_scan, essid '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400961 print_ssid(ssid, priv->scan_ssid, priv->scan_ssid_len));
Holger Schurig52933d82008-03-05 07:05:32 +0100962 } else {
963 priv->scan_ssid_len = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100964 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100965
966 if (!delayed_work_pending(&priv->scan_work))
967 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100968 msecs_to_jiffies(50));
Holger Schurigffd074f2007-12-07 16:52:10 +0100969 /* set marker that currently a scan is taking place */
Holger Schurig8816edc2008-01-28 17:28:05 +0100970 priv->scan_channel = -1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100971
David Woodhouseaa21c002007-12-08 20:04:36 +0000972 if (priv->surpriseremoved)
Holger Schurig52933d82008-03-05 07:05:32 +0100973 ret = -EIO;
Holger Schurigffd074f2007-12-07 16:52:10 +0100974
Holger Schurig52933d82008-03-05 07:05:32 +0100975out:
976 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
977 return ret;
Holger Schurigffd074f2007-12-07 16:52:10 +0100978}
979
980
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200981/**
Holger Schurige56188a2007-10-09 14:15:19 +0200982 * @brief Handle Retrieve scan table ioctl
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200983 *
984 * @param dev A pointer to net_device structure
985 * @param info A pointer to iw_request_info structure
986 * @param dwrq A pointer to iw_point structure
987 * @param extra A pointer to extra data buf
988 *
989 * @return 0 --success, otherwise fail
990 */
Holger Schurig10078322007-11-15 18:05:47 -0500991int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
David Woodhousef137e052008-03-03 12:18:59 +0100992 struct iw_point *dwrq, char *extra)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200993{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400994#define SCAN_ITEM_SIZE 128
Kiran Divekarab65f642009-02-19 19:32:39 -0500995 struct lbs_private *priv = dev->ml_priv;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400996 int err = 0;
997 char *ev = extra;
998 char *stop = ev + dwrq->length;
David Woodhousef137e052008-03-03 12:18:59 +0100999 struct bss_descriptor *iter_bss;
1000 struct bss_descriptor *safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001001
Holger Schurig52933d82008-03-05 07:05:32 +01001002 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001003
Holger Schurigffd074f2007-12-07 16:52:10 +01001004 /* iwlist should wait until the current scan is finished */
Holger Schurig8816edc2008-01-28 17:28:05 +01001005 if (priv->scan_channel)
Holger Schurigffd074f2007-12-07 16:52:10 +01001006 return -EAGAIN;
1007
Dan Williams80e78ef2007-05-25 22:18:47 -04001008 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
Amitkumar Karwarc0bbd572009-10-08 19:38:45 -07001009 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate) {
1010 err = lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
1011 CMD_OPTION_WAITFORRSP, 0, NULL);
1012 if (err)
1013 goto out;
1014 }
Dan Williams80e78ef2007-05-25 22:18:47 -04001015
David Woodhouseaa21c002007-12-08 20:04:36 +00001016 mutex_lock(&priv->lock);
1017 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
David Woodhousef137e052008-03-03 12:18:59 +01001018 char *next_ev;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001019 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001020
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001021 if (stop - ev < SCAN_ITEM_SIZE) {
1022 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001023 break;
1024 }
1025
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001026 /* For mesh device, list only mesh networks */
1027 if (dev == priv->mesh_dev && !iter_bss->mesh)
1028 continue;
1029
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001030 /* Prune old an old scan result */
1031 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1032 if (time_after(jiffies, stale_time)) {
David Woodhousef137e052008-03-03 12:18:59 +01001033 list_move_tail(&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001034 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001035 continue;
1036 }
1037
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001038 /* Translate to WE format this entry */
David S. Millerccc58052008-06-16 18:50:49 -07001039 next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001040 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001041 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001042 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001043 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001044 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001045
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001046 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001047 dwrq->flags = 0;
Amitkumar Karwarc0bbd572009-10-08 19:38:45 -07001048out:
Holger Schurig52933d82008-03-05 07:05:32 +01001049 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001050 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001051}
1052
Holger Schurige56188a2007-10-09 14:15:19 +02001053
1054
1055
1056/*********************************************************************/
1057/* */
1058/* Command execution */
1059/* */
1060/*********************************************************************/
1061
1062
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001063/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001064 * @brief This function handles the command response of scan
1065 *
Holger Schurige56188a2007-10-09 14:15:19 +02001066 * Called from handle_cmd_response() in cmdrespc.
1067 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001068 * The response buffer for the scan command has the following
1069 * memory layout:
1070 *
1071 * .-----------------------------------------------------------.
1072 * | header (4 * sizeof(u16)): Standard command response hdr |
1073 * .-----------------------------------------------------------.
1074 * | bufsize (u16) : sizeof the BSS Description data |
1075 * .-----------------------------------------------------------.
1076 * | NumOfSet (u8) : Number of BSS Descs returned |
1077 * .-----------------------------------------------------------.
1078 * | BSSDescription data (variable, size given in bufsize) |
1079 * .-----------------------------------------------------------.
1080 * | TLV data (variable, size calculated using header->size, |
1081 * | bufsize and sizeof the fixed fields above) |
1082 * .-----------------------------------------------------------.
1083 *
Holger Schurig69f90322007-11-23 15:43:44 +01001084 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001085 * @param resp A pointer to cmd_ds_command
1086 *
1087 * @return 0 or -1
1088 */
David Woodhousefa62f992008-03-03 12:18:03 +01001089static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1090 struct cmd_header *resp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001091{
David Woodhousefa62f992008-03-03 12:18:03 +01001092 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
David Woodhousef137e052008-03-03 12:18:59 +01001093 struct bss_descriptor *iter_bss;
1094 struct bss_descriptor *safe;
David Woodhousefa62f992008-03-03 12:18:03 +01001095 uint8_t *bssinfo;
1096 uint16_t scanrespsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001097 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001098 int idx;
1099 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001100 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001101
Holger Schurige56188a2007-10-09 14:15:19 +02001102 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001103
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001104 /* Prune old entries from scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001105 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001106 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1107 if (time_before(jiffies, stale_time))
1108 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +00001109 list_move_tail (&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001110 clear_bss_descriptor(iter_bss);
1111 }
1112
David Woodhousefa62f992008-03-03 12:18:03 +01001113 if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1114 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1115 scanresp->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001116 ret = -1;
1117 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001118 }
1119
Cliff Cai2c5b9e512009-05-27 14:03:09 -04001120 bytesleft = get_unaligned_le16(&scanresp->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001121 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001122
David Woodhousee7240ac2007-12-11 17:54:36 -05001123 scanrespsize = le16_to_cpu(resp->size);
David Woodhousefa62f992008-03-03 12:18:03 +01001124 lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001125
David Woodhousefa62f992008-03-03 12:18:03 +01001126 bssinfo = scanresp->bssdesc_and_tlvbuffer;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001127
1128 /* The size of the TLV buffer is equal to the entire command response
1129 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1130 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1131 * response header (S_DS_GEN)
1132 */
David Woodhousefa62f992008-03-03 12:18:03 +01001133 tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1134 + sizeof(scanresp->nr_sets)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001135 + S_DS_GEN);
1136
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001137 /*
David Woodhousefa62f992008-03-03 12:18:03 +01001138 * Process each scan response returned (scanresp->nr_sets). Save
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001139 * the information in the newbssentry and then insert into the
1140 * driver scan table either as an update to an existing entry
1141 * or as an addition at the end of the table
1142 */
David Woodhousefa62f992008-03-03 12:18:03 +01001143 for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001144 struct bss_descriptor new;
David Woodhousefa62f992008-03-03 12:18:03 +01001145 struct bss_descriptor *found = NULL;
1146 struct bss_descriptor *oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001147
1148 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001149 memset(&new, 0, sizeof (struct bss_descriptor));
David Woodhousefa62f992008-03-03 12:18:03 +01001150 if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001151 /* error parsing the scan response, skipped */
1152 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1153 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001154 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001155
1156 /* Try to find this bss in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001157 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001158 if (is_same_network(iter_bss, &new)) {
1159 found = iter_bss;
1160 break;
1161 }
1162
1163 if ((oldest == NULL) ||
1164 (iter_bss->last_scanned < oldest->last_scanned))
1165 oldest = iter_bss;
1166 }
1167
1168 if (found) {
1169 /* found, clear it */
1170 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001171 } else if (!list_empty(&priv->network_free_list)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001172 /* Pull one from the free list */
David Woodhouseaa21c002007-12-08 20:04:36 +00001173 found = list_entry(priv->network_free_list.next,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001174 struct bss_descriptor, list);
David Woodhouseaa21c002007-12-08 20:04:36 +00001175 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001176 } else if (oldest) {
1177 /* If there are no more slots, expire the oldest */
1178 found = oldest;
1179 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001180 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001181 } else {
1182 continue;
1183 }
1184
Johannes Berge1749612008-10-27 15:59:26 -07001185 lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001186
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001187 /* Copy the locally created newbssentry to the scan table */
1188 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001189 }
1190
Holger Schurig9012b282007-05-25 11:27:16 -04001191 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001192
Holger Schurig9012b282007-05-25 11:27:16 -04001193done:
1194 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1195 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001196}