blob: 6c95af3023ccf366897726c5fa460a3d5933d72f [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++) {
David Woodhousef137e052008-03-03 12:18:59 +0100164 if (priv->enable11d && (priv->connect_status != LBS_CONNECTED)
165 && (priv->mesh_connect_status != LBS_CONNECTED)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200166 /* Scan all the supported chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000167 if (!priv->universal_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200168 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000169 scanregion = &priv->universal_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200170
171 /* clear the parsed_region_chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000172 memset(&priv->parsed_region_chan, 0x00,
173 sizeof(priv->parsed_region_chan));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200174 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +0000175 if (!priv->region_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200176 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000177 scanregion = &priv->region_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200178 }
179
David Woodhousef137e052008-03-03 12:18:59 +0100180 for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
181 struct chanscanparamset *chan = &scanchanlist[chanidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200182
183 cfp = scanregion->CFP + nextchan;
184
David Woodhousef137e052008-03-03 12:18:59 +0100185 if (priv->enable11d)
186 scantype = lbs_get_scan_type_11d(cfp->channel,
187 &priv->parsed_region_chan);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200188
David Woodhousef137e052008-03-03 12:18:59 +0100189 if (scanregion->band == BAND_B || scanregion->band == BAND_G)
190 chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200191
Dan Williams0aef64d2007-08-02 11:31:18 -0400192 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
David Woodhousef137e052008-03-03 12:18:59 +0100193 chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
194 chan->chanscanmode.passivescan = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200195 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100196 chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
197 chan->chanscanmode.passivescan = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200198 }
199
David Woodhousef137e052008-03-03 12:18:59 +0100200 chan->channumber = cfp->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200201 }
202 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100203 return chanidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200204}
205
Holger Schurige56188a2007-10-09 14:15:19 +0200206/*
Holger Schurigffd074f2007-12-07 16:52:10 +0100207 * Add SSID TLV of the form:
208 *
209 * TLV-ID SSID 00 00
210 * length 06 00
211 * ssid 4d 4e 54 45 53 54
212 */
Holger Schurig52933d82008-03-05 07:05:32 +0100213static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv)
Dan Williamseb8f7332007-05-25 16:25:21 -0400214{
Dan Williams75b6a612009-05-22 20:03:09 -0400215 struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
David Woodhousef137e052008-03-03 12:18:59 +0100216
Holger Schurigffd074f2007-12-07 16:52:10 +0100217 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
Holger Schurig52933d82008-03-05 07:05:32 +0100218 ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len);
219 memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len);
220 return sizeof(ssid_tlv->header) + priv->scan_ssid_len;
Holger Schurigffd074f2007-12-07 16:52:10 +0100221}
Dan Williamseb8f7332007-05-25 16:25:21 -0400222
Holger Schurigffd074f2007-12-07 16:52:10 +0100223/*
224 * Add CHANLIST TLV of the form
225 *
226 * TLV-ID CHANLIST 01 01
227 * length 5b 00
228 * channel 1 00 01 00 00 00 64 00
229 * radio type 00
230 * channel 01
231 * scan type 00
232 * min scan time 00 00
233 * max scan time 64 00
234 * channel 2 00 02 00 00 00 64 00
235 * channel 3 00 03 00 00 00 64 00
236 * channel 4 00 04 00 00 00 64 00
237 * channel 5 00 05 00 00 00 64 00
238 * channel 6 00 06 00 00 00 64 00
239 * channel 7 00 07 00 00 00 64 00
240 * channel 8 00 08 00 00 00 64 00
241 * channel 9 00 09 00 00 00 64 00
242 * channel 10 00 0a 00 00 00 64 00
243 * channel 11 00 0b 00 00 00 64 00
244 * channel 12 00 0c 00 00 00 64 00
245 * channel 13 00 0d 00 00 00 64 00
246 *
247 */
David Woodhousef137e052008-03-03 12:18:59 +0100248static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
249 struct chanscanparamset *chan_list,
250 int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100251{
David Woodhousef137e052008-03-03 12:18:59 +0100252 size_t size = sizeof(struct chanscanparamset) *chan_count;
Dan Williams75b6a612009-05-22 20:03:09 -0400253 struct mrvl_ie_chanlist_param_set *chan_tlv = (void *)tlv;
Dan Williamseb8f7332007-05-25 16:25:21 -0400254
Holger Schurigffd074f2007-12-07 16:52:10 +0100255 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
256 memcpy(chan_tlv->chanscanparam, chan_list, size);
257 chan_tlv->header.len = cpu_to_le16(size);
258 return sizeof(chan_tlv->header) + size;
259}
Dan Williamseb8f7332007-05-25 16:25:21 -0400260
Holger Schurigffd074f2007-12-07 16:52:10 +0100261/*
262 * Add RATES TLV of the form
263 *
264 * TLV-ID RATES 01 00
265 * length 0e 00
266 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
267 *
268 * The rates are in lbs_bg_rates[], but for the 802.11b
269 * rates the high bit isn't set.
270 */
David Woodhousef137e052008-03-03 12:18:59 +0100271static int lbs_scan_add_rates_tlv(uint8_t *tlv)
Holger Schurigffd074f2007-12-07 16:52:10 +0100272{
273 int i;
Dan Williams75b6a612009-05-22 20:03:09 -0400274 struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
Holger Schurigffd074f2007-12-07 16:52:10 +0100275
276 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
277 tlv += sizeof(rate_tlv->header);
278 for (i = 0; i < MAX_RATES; i++) {
279 *tlv = lbs_bg_rates[i];
280 if (*tlv == 0)
281 break;
282 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
283 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
284 Note that the values are MBit/s * 2, to mark them as
285 basic rates so that the firmware likes it better */
286 if (*tlv == 0x02 || *tlv == 0x04 ||
287 *tlv == 0x0b || *tlv == 0x16)
288 *tlv |= 0x80;
289 tlv++;
Dan Williamseb8f7332007-05-25 16:25:21 -0400290 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100291 rate_tlv->header.len = cpu_to_le16(i);
292 return sizeof(rate_tlv->header) + i;
293}
Dan Williamseb8f7332007-05-25 16:25:21 -0400294
Holger Schurigffd074f2007-12-07 16:52:10 +0100295/*
296 * Generate the CMD_802_11_SCAN command with the proper tlv
297 * for a bunch of channels.
298 */
David Woodhousefa62f992008-03-03 12:18:03 +0100299static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
Holger Schurig52933d82008-03-05 07:05:32 +0100300 struct chanscanparamset *chan_list, int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100301{
302 int ret = -ENOMEM;
David Woodhousefa62f992008-03-03 12:18:03 +0100303 struct cmd_ds_802_11_scan *scan_cmd;
304 uint8_t *tlv; /* pointer into our current, growing TLV storage area */
Holger Schurigffd074f2007-12-07 16:52:10 +0100305
David Woodhousefa62f992008-03-03 12:18:03 +0100306 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
Holger Schurigc0d43992008-04-29 10:07:56 +0200307 bsstype, chan_list ? chan_list[0].channumber : -1,
308 chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100309
310 /* create the fixed part for scan command */
311 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
312 if (scan_cmd == NULL)
Holger Schurige56188a2007-10-09 14:15:19 +0200313 goto out;
David Woodhousefa62f992008-03-03 12:18:03 +0100314
Holger Schurigffd074f2007-12-07 16:52:10 +0100315 tlv = scan_cmd->tlvbuffer;
Holger Schurig52933d82008-03-05 07:05:32 +0100316 /* TODO: do we need to scan for a specific BSSID?
317 memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
Holger Schurigffd074f2007-12-07 16:52:10 +0100318 scan_cmd->bsstype = bsstype;
Dan Williamseb8f7332007-05-25 16:25:21 -0400319
Holger Schurigffd074f2007-12-07 16:52:10 +0100320 /* add TLVs */
Holger Schurig52933d82008-03-05 07:05:32 +0100321 if (priv->scan_ssid_len)
322 tlv += lbs_scan_add_ssid_tlv(priv, tlv);
Holger Schurigffd074f2007-12-07 16:52:10 +0100323 if (chan_list && chan_count)
324 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
325 tlv += lbs_scan_add_rates_tlv(tlv);
Dan Williamseb8f7332007-05-25 16:25:21 -0400326
Holger Schurigffd074f2007-12-07 16:52:10 +0100327 /* This is the final data we are about to send */
David Woodhousefa62f992008-03-03 12:18:03 +0100328 scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
329 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
330 sizeof(*scan_cmd));
Holger Schurigffd074f2007-12-07 16:52:10 +0100331 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
David Woodhousefa62f992008-03-03 12:18:03 +0100332 tlv - scan_cmd->tlvbuffer);
Dan Williamseb8f7332007-05-25 16:25:21 -0400333
David Woodhousefa62f992008-03-03 12:18:03 +0100334 ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
335 le16_to_cpu(scan_cmd->hdr.size),
336 lbs_ret_80211_scan, 0);
337
Holger Schurige56188a2007-10-09 14:15:19 +0200338out:
Holger Schurigffd074f2007-12-07 16:52:10 +0100339 kfree(scan_cmd);
340 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
341 return ret;
Dan Williamseb8f7332007-05-25 16:25:21 -0400342}
343
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200344/**
345 * @brief Internal function used to start a scan based on an input config
346 *
347 * Use the input user scan configuration information when provided in
348 * order to send the appropriate scan commands to firmware to populate or
349 * update the internal driver scan table
350 *
Holger Schurig69f90322007-11-23 15:43:44 +0100351 * @param priv A pointer to struct lbs_private structure
Holger Schurig52933d82008-03-05 07:05:32 +0100352 * @param full_scan Do a full-scan (blocking)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200353 *
354 * @return 0 or < 0 if error
355 */
Holger Schurig245bf202008-04-02 16:27:42 +0200356int lbs_scan_networks(struct lbs_private *priv, int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200357{
Holger Schurigffd074f2007-12-07 16:52:10 +0100358 int ret = -ENOMEM;
359 struct chanscanparamset *chan_list;
360 struct chanscanparamset *curr_chans;
361 int chan_count;
David Woodhousef137e052008-03-03 12:18:59 +0100362 uint8_t bsstype = CMD_BSS_TYPE_ANY;
Holger Schurigffd074f2007-12-07 16:52:10 +0100363 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
Holger Schurigffd074f2007-12-07 16:52:10 +0100364 union iwreq_data wrqu;
Dan Williamsf8f55102007-05-30 10:12:55 -0400365#ifdef CONFIG_LIBERTAS_DEBUG
Holger Schurigffd074f2007-12-07 16:52:10 +0100366 struct bss_descriptor *iter;
Dan Williamsf8f55102007-05-30 10:12:55 -0400367 int i = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -0400368 DECLARE_SSID_BUF(ssid);
Dan Williamsf8f55102007-05-30 10:12:55 -0400369#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200370
David Woodhousef137e052008-03-03 12:18:59 +0100371 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
Dan Williams2afc0c52007-08-02 13:19:04 -0400372
373 /* Cancel any partial outstanding partial scans if this scan
374 * is a full scan.
375 */
376 if (full_scan && delayed_work_pending(&priv->scan_work))
377 cancel_delayed_work(&priv->scan_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200378
Holger Schurig52933d82008-03-05 07:05:32 +0100379 /* User-specified bsstype or channel list
380 TODO: this can be implemented if some user-space application
381 need the feature. Formerly, it was accessible from debugfs,
382 but then nowhere used.
Holger Schurigffd074f2007-12-07 16:52:10 +0100383 if (user_cfg) {
384 if (user_cfg->bsstype)
Holger Schurig52933d82008-03-05 07:05:32 +0100385 bsstype = user_cfg->bsstype;
386 } */
387
388 lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200389
Holger Schurigffd074f2007-12-07 16:52:10 +0100390 /* Create list of channels to scan */
391 chan_list = kzalloc(sizeof(struct chanscanparamset) *
David Woodhousef137e052008-03-03 12:18:59 +0100392 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
Holger Schurigffd074f2007-12-07 16:52:10 +0100393 if (!chan_list) {
394 lbs_pr_alert("SCAN: chan_list empty\n");
395 goto out;
396 }
397
398 /* We want to scan all channels */
Holger Schurig52933d82008-03-05 07:05:32 +0100399 chan_count = lbs_scan_create_channel_list(priv, chan_list);
Holger Schurigffd074f2007-12-07 16:52:10 +0100400
401 netif_stop_queue(priv->dev);
402 netif_carrier_off(priv->dev);
403 if (priv->mesh_dev) {
David Woodhousea27b9f92007-12-12 00:41:51 -0500404 netif_stop_queue(priv->mesh_dev);
405 netif_carrier_off(priv->mesh_dev);
Holger Schurigffd074f2007-12-07 16:52:10 +0100406 }
407
408 /* Prepare to continue an interrupted scan */
Holger Schurig8816edc2008-01-28 17:28:05 +0100409 lbs_deb_scan("chan_count %d, scan_channel %d\n",
410 chan_count, priv->scan_channel);
Holger Schurigffd074f2007-12-07 16:52:10 +0100411 curr_chans = chan_list;
412 /* advance channel list by already-scanned-channels */
Holger Schurig8816edc2008-01-28 17:28:05 +0100413 if (priv->scan_channel > 0) {
414 curr_chans += priv->scan_channel;
415 chan_count -= priv->scan_channel;
Holger Schurigffd074f2007-12-07 16:52:10 +0100416 }
417
418 /* Send scan command(s)
419 * numchannels contains the number of channels we should maximally scan
420 * chan_count is the total number of channels to scan
421 */
422
423 while (chan_count) {
424 int to_scan = min(numchannels, chan_count);
425 lbs_deb_scan("scanning %d of %d channels\n",
David Woodhousef137e052008-03-03 12:18:59 +0100426 to_scan, chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100427 ret = lbs_do_scan(priv, bsstype, curr_chans,
Holger Schurig52933d82008-03-05 07:05:32 +0100428 to_scan);
Holger Schurigffd074f2007-12-07 16:52:10 +0100429 if (ret) {
430 lbs_pr_err("SCAN_CMD failed\n");
431 goto out2;
432 }
433 curr_chans += to_scan;
434 chan_count -= to_scan;
435
436 /* somehow schedule the next part of the scan */
David Woodhousef137e052008-03-03 12:18:59 +0100437 if (chan_count && !full_scan &&
David Woodhouseaa21c002007-12-08 20:04:36 +0000438 !priv->surpriseremoved) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100439 /* -1 marks just that we're currently scanning */
Holger Schurig8816edc2008-01-28 17:28:05 +0100440 if (priv->scan_channel < 0)
441 priv->scan_channel = to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100442 else
Holger Schurig8816edc2008-01-28 17:28:05 +0100443 priv->scan_channel += to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100444 cancel_delayed_work(&priv->scan_work);
445 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100446 msecs_to_jiffies(300));
Holger Schurigffd074f2007-12-07 16:52:10 +0100447 /* skip over GIWSCAN event */
448 goto out;
449 }
450
451 }
452 memset(&wrqu, 0, sizeof(union iwreq_data));
453 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200454
Dan Williamsf8f55102007-05-30 10:12:55 -0400455#ifdef CONFIG_LIBERTAS_DEBUG
456 /* Dump the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000457 mutex_lock(&priv->lock);
Holger Schurigffd074f2007-12-07 16:52:10 +0100458 lbs_deb_scan("scan table:\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000459 list_for_each_entry(iter, &priv->network_list, list)
Johannes Berge1749612008-10-27 15:59:26 -0700460 lbs_deb_scan("%02d: BSSID %pM, RSSI %d, SSID '%s'\n",
461 i++, iter->bssid, iter->rssi,
John W. Linville9387b7c2008-09-30 20:59:05 -0400462 print_ssid(ssid, iter->ssid, iter->ssid_len));
David Woodhouseaa21c002007-12-08 20:04:36 +0000463 mutex_unlock(&priv->lock);
Dan Williamsf8f55102007-05-30 10:12:55 -0400464#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200465
Holger Schurigffd074f2007-12-07 16:52:10 +0100466out2:
Holger Schurig8816edc2008-01-28 17:28:05 +0100467 priv->scan_channel = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100468
469out:
David Woodhouseaa21c002007-12-08 20:04:36 +0000470 if (priv->connect_status == LBS_CONNECTED) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400471 netif_carrier_on(priv->dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500472 if (!priv->tx_pending_len)
473 netif_wake_queue(priv->dev);
Brajesh Dave01d77d82007-11-20 17:44:14 -0500474 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000475 if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
Brajesh Dave01d77d82007-11-20 17:44:14 -0500476 netif_carrier_on(priv->mesh_dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500477 if (!priv->tx_pending_len)
478 netif_wake_queue(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200479 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100480 kfree(chan_list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200481
Holger Schurig9012b282007-05-25 11:27:16 -0400482 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200483 return ret;
484}
485
Holger Schurig52933d82008-03-05 07:05:32 +0100486void lbs_scan_worker(struct work_struct *work)
487{
488 struct lbs_private *priv =
489 container_of(work, struct lbs_private, scan_work.work);
490
491 lbs_deb_enter(LBS_DEB_SCAN);
492 lbs_scan_networks(priv, 0);
493 lbs_deb_leave(LBS_DEB_SCAN);
494}
495
496
Holger Schurigffd074f2007-12-07 16:52:10 +0100497/*********************************************************************/
498/* */
499/* Result interpretation */
500/* */
501/*********************************************************************/
502
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200503/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200504 * @brief Interpret a BSS scan response returned from the firmware
505 *
506 * Parse the various fixed fields and IEs passed back for a a BSS probe
Holger Schurigffd074f2007-12-07 16:52:10 +0100507 * response or beacon from the scan command. Record information as needed
508 * in the scan table struct bss_descriptor for that entry.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200509 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400510 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200511 *
512 * @return 0 or -1
513 */
Holger Schurig10078322007-11-15 18:05:47 -0500514static int lbs_process_bss(struct bss_descriptor *bss,
David Woodhousef137e052008-03-03 12:18:59 +0100515 uint8_t **pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200516{
Dan Williams5fd164e2009-05-22 20:01:21 -0400517 struct ieee_ie_fh_param_set *fh;
518 struct ieee_ie_ds_param_set *ds;
519 struct ieee_ie_cf_param_set *cf;
520 struct ieee_ie_ibss_param_set *ibss;
John W. Linville9387b7c2008-09-30 20:59:05 -0400521 DECLARE_SSID_BUF(ssid);
Dan Williams75b6a612009-05-22 20:03:09 -0400522 struct ieee_ie_country_info_set *pcountryinfo;
David Woodhousef137e052008-03-03 12:18:59 +0100523 uint8_t *pos, *end, *p;
524 uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
525 uint16_t beaconsize = 0;
Holger Schurig9012b282007-05-25 11:27:16 -0400526 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200527
Holger Schurige56188a2007-10-09 14:15:19 +0200528 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200529
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200530 if (*bytesleft >= sizeof(beaconsize)) {
531 /* Extract & convert beacon size from the command buffer */
Harvey Harrison533dd1b2008-04-29 01:03:36 -0700532 beaconsize = get_unaligned_le16(*pbeaconinfo);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533 *bytesleft -= sizeof(beaconsize);
534 *pbeaconinfo += sizeof(beaconsize);
535 }
536
537 if (beaconsize == 0 || beaconsize > *bytesleft) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200538 *pbeaconinfo += *bytesleft;
539 *bytesleft = 0;
Holger Schurige56188a2007-10-09 14:15:19 +0200540 ret = -1;
541 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200542 }
543
544 /* Initialize the current working beacon pointer for this BSS iteration */
Dan Williamsab617972007-08-02 10:48:02 -0400545 pos = *pbeaconinfo;
546 end = pos + beaconsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200547
548 /* Advance the return beacon pointer past the current beacon */
549 *pbeaconinfo += beaconsize;
550 *bytesleft -= beaconsize;
551
Dan Williamsab617972007-08-02 10:48:02 -0400552 memcpy(bss->bssid, pos, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -0700553 lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid);
Dan Williamsab617972007-08-02 10:48:02 -0400554 pos += ETH_ALEN;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200555
Dan Williamsab617972007-08-02 10:48:02 -0400556 if ((end - pos) < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400557 lbs_deb_scan("process_bss: Not enough bytes left\n");
Holger Schurige56188a2007-10-09 14:15:19 +0200558 ret = -1;
559 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200560 }
561
562 /*
563 * next 4 fields are RSSI, time stamp, beacon interval,
564 * and capability information
565 */
566
567 /* RSSI is 1 byte long */
Dan Williamsab617972007-08-02 10:48:02 -0400568 bss->rssi = *pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100569 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
Dan Williamsab617972007-08-02 10:48:02 -0400570 pos++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200571
572 /* time stamp is 8 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400573 pos += 8;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200574
575 /* beacon interval is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300576 bss->beaconperiod = get_unaligned_le16(pos);
Dan Williamsab617972007-08-02 10:48:02 -0400577 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200578
579 /* capability information is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300580 bss->capability = get_unaligned_le16(pos);
Holger Schurigffd074f2007-12-07 16:52:10 +0100581 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
Dan Williamsab617972007-08-02 10:48:02 -0400582 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200583
Dan Williams0c9ca692007-08-02 10:43:44 -0400584 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
Holger Schurigffd074f2007-12-07 16:52:10 +0100585 lbs_deb_scan("process_bss: WEP enabled\n");
Dan Williams0c9ca692007-08-02 10:43:44 -0400586 if (bss->capability & WLAN_CAPABILITY_IBSS)
587 bss->mode = IW_MODE_ADHOC;
588 else
589 bss->mode = IW_MODE_INFRA;
590
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200591 /* rest of the current buffer are IE's */
Holger Schurigffd074f2007-12-07 16:52:10 +0100592 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
Holger Schurigece56192007-08-02 11:53:06 -0400593 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200594
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200595 /* process variable IE */
Dan Williamsab617972007-08-02 10:48:02 -0400596 while (pos <= end - 2) {
Johannes Berg2c7060022008-10-30 22:09:54 +0100597 if (pos + pos[1] > end) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400598 lbs_deb_scan("process_bss: error in processing IE, "
David Woodhousef137e052008-03-03 12:18:59 +0100599 "bytes left < IE length\n");
Dan Williamsab617972007-08-02 10:48:02 -0400600 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200601 }
602
Johannes Berg2c7060022008-10-30 22:09:54 +0100603 switch (pos[0]) {
604 case WLAN_EID_SSID:
605 bss->ssid_len = min_t(int, IEEE80211_MAX_SSID_LEN, pos[1]);
606 memcpy(bss->ssid, pos + 2, bss->ssid_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100607 lbs_deb_scan("got SSID IE: '%s', len %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400608 print_ssid(ssid, bss->ssid, bss->ssid_len),
Dan Williamsd8efea22007-05-28 23:54:55 -0400609 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200610 break;
611
Johannes Berg2c7060022008-10-30 22:09:54 +0100612 case WLAN_EID_SUPP_RATES:
613 n_basic_rates = min_t(uint8_t, MAX_RATES, pos[1]);
614 memcpy(bss->rates, pos + 2, n_basic_rates);
Dan Williams8c512762007-08-02 11:40:45 -0400615 got_basic_rates = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100616 lbs_deb_scan("got RATES IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200617 break;
618
Johannes Berg2c7060022008-10-30 22:09:54 +0100619 case WLAN_EID_FH_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400620 fh = (struct ieee_ie_fh_param_set *) pos;
621 memcpy(&bss->phy.fh, fh, sizeof(*fh));
Holger Schurigffd074f2007-12-07 16:52:10 +0100622 lbs_deb_scan("got FH IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200623 break;
624
Johannes Berg2c7060022008-10-30 22:09:54 +0100625 case WLAN_EID_DS_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400626 ds = (struct ieee_ie_ds_param_set *) pos;
627 bss->channel = ds->channel;
628 memcpy(&bss->phy.ds, ds, sizeof(*ds));
Holger Schurigffd074f2007-12-07 16:52:10 +0100629 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200630 break;
631
Johannes Berg2c7060022008-10-30 22:09:54 +0100632 case WLAN_EID_CF_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400633 cf = (struct ieee_ie_cf_param_set *) pos;
634 memcpy(&bss->ss.cf, cf, sizeof(*cf));
Holger Schurigffd074f2007-12-07 16:52:10 +0100635 lbs_deb_scan("got CF IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200636 break;
637
Johannes Berg2c7060022008-10-30 22:09:54 +0100638 case WLAN_EID_IBSS_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400639 ibss = (struct ieee_ie_ibss_param_set *) pos;
640 bss->atimwindow = ibss->atimwindow;
641 memcpy(&bss->ss.ibss, ibss, sizeof(*ibss));
Holger Schurigffd074f2007-12-07 16:52:10 +0100642 lbs_deb_scan("got IBSS IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200643 break;
644
Johannes Berg2c7060022008-10-30 22:09:54 +0100645 case WLAN_EID_COUNTRY:
Dan Williams75b6a612009-05-22 20:03:09 -0400646 pcountryinfo = (struct ieee_ie_country_info_set *) pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100647 lbs_deb_scan("got COUNTRY IE\n");
Dan Williams75b6a612009-05-22 20:03:09 -0400648 if (pcountryinfo->header.len < sizeof(pcountryinfo->countrycode)
649 || pcountryinfo->header.len > 254) {
650 lbs_deb_scan("%s: 11D- Err CountryInfo len %d, min %zd, max 254\n",
651 __func__,
652 pcountryinfo->header.len,
653 sizeof(pcountryinfo->countrycode));
Holger Schurig9012b282007-05-25 11:27:16 -0400654 ret = -1;
655 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200656 }
657
Dan Williams75b6a612009-05-22 20:03:09 -0400658 memcpy(&bss->countryinfo, pcountryinfo,
659 pcountryinfo->header.len + 2);
Holger Schurigece56192007-08-02 11:53:06 -0400660 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
David Woodhousef137e052008-03-03 12:18:59 +0100661 (uint8_t *) pcountryinfo,
Dan Williams75b6a612009-05-22 20:03:09 -0400662 (int) (pcountryinfo->header.len + 2));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200663 break;
664
Johannes Berg2c7060022008-10-30 22:09:54 +0100665 case WLAN_EID_EXT_SUPP_RATES:
Dan Williamsab617972007-08-02 10:48:02 -0400666 /* only process extended supported rate if data rate is
667 * already found. Data rate IE should come before
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200668 * extended supported rate IE
669 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100670 lbs_deb_scan("got RATESEX IE\n");
671 if (!got_basic_rates) {
672 lbs_deb_scan("... but ignoring it\n");
Dan Williamsab617972007-08-02 10:48:02 -0400673 break;
Holger Schurigffd074f2007-12-07 16:52:10 +0100674 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200675
Johannes Berg2c7060022008-10-30 22:09:54 +0100676 n_ex_rates = pos[1];
Dan Williams8c512762007-08-02 11:40:45 -0400677 if (n_basic_rates + n_ex_rates > MAX_RATES)
678 n_ex_rates = MAX_RATES - n_basic_rates;
Dan Williamsab617972007-08-02 10:48:02 -0400679
Dan Williams8c512762007-08-02 11:40:45 -0400680 p = bss->rates + n_basic_rates;
Johannes Berg2c7060022008-10-30 22:09:54 +0100681 memcpy(p, pos + 2, n_ex_rates);
Dan Williamsab617972007-08-02 10:48:02 -0400682 break;
683
Johannes Berg2c7060022008-10-30 22:09:54 +0100684 case WLAN_EID_GENERIC:
685 if (pos[1] >= 4 &&
686 pos[2] == 0x00 && pos[3] == 0x50 &&
687 pos[4] == 0xf2 && pos[5] == 0x01) {
688 bss->wpa_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
689 memcpy(bss->wpa_ie, pos, bss->wpa_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100690 lbs_deb_scan("got WPA IE\n");
Johannes Berg2c7060022008-10-30 22:09:54 +0100691 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie,
692 bss->wpa_ie_len);
693 } else if (pos[1] >= MARVELL_MESH_IE_LENGTH &&
694 pos[2] == 0x00 && pos[3] == 0x50 &&
Roel Kluinbaf62ee2009-02-03 17:15:57 +0100695 pos[4] == 0x43 && pos[5] == 0x04) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100696 lbs_deb_scan("got mesh IE\n");
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400697 bss->mesh = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100698 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100699 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
Johannes Berg2c7060022008-10-30 22:09:54 +0100700 pos[2], pos[3],
701 pos[4], pos[5],
702 pos[1]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200703 }
704 break;
705
Johannes Berg2c7060022008-10-30 22:09:54 +0100706 case WLAN_EID_RSN:
Holger Schurigffd074f2007-12-07 16:52:10 +0100707 lbs_deb_scan("got RSN IE\n");
Johannes Berg2c7060022008-10-30 22:09:54 +0100708 bss->rsn_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
709 memcpy(bss->rsn_ie, pos, bss->rsn_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100710 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
Johannes Berg2c7060022008-10-30 22:09:54 +0100711 bss->rsn_ie, bss->rsn_ie_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200712 break;
713
Dan Williamsab617972007-08-02 10:48:02 -0400714 default:
Holger Schurigffd074f2007-12-07 16:52:10 +0100715 lbs_deb_scan("got IE 0x%04x, len %d\n",
Johannes Berg2c7060022008-10-30 22:09:54 +0100716 pos[0], pos[1]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200717 break;
718 }
719
Johannes Berg2c7060022008-10-30 22:09:54 +0100720 pos += pos[1] + 2;
Dan Williamsab617972007-08-02 10:48:02 -0400721 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400722
723 /* Timestamp */
724 bss->last_scanned = jiffies;
Holger Schurig10078322007-11-15 18:05:47 -0500725 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400726
Holger Schurig9012b282007-05-25 11:27:16 -0400727 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200728
Holger Schurig9012b282007-05-25 11:27:16 -0400729done:
730 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
731 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200732}
733
734/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200735 * @brief Send a scan command for all available channels filtered on a spec
736 *
Holger Schurige56188a2007-10-09 14:15:19 +0200737 * Used in association code and from debugfs
738 *
Holger Schurig69f90322007-11-23 15:43:44 +0100739 * @param priv A pointer to struct lbs_private structure
Holger Schurige56188a2007-10-09 14:15:19 +0200740 * @param ssid A pointer to the SSID to scan for
741 * @param ssid_len Length of the SSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200742 *
743 * @return 0-success, otherwise fail
744 */
David Woodhousef137e052008-03-03 12:18:59 +0100745int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100746 uint8_t ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200747{
John W. Linville9387b7c2008-09-30 20:59:05 -0400748 DECLARE_SSID_BUF(ssid_buf);
Dan Williamseb8f7332007-05-25 16:25:21 -0400749 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200750
Holger Schurig52933d82008-03-05 07:05:32 +0100751 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400752 print_ssid(ssid_buf, ssid, ssid_len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200753
Dan Williamsd8efea22007-05-28 23:54:55 -0400754 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -0400755 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200756
Holger Schurig52933d82008-03-05 07:05:32 +0100757 memcpy(priv->scan_ssid, ssid, ssid_len);
758 priv->scan_ssid_len = ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200759
Holger Schurig52933d82008-03-05 07:05:32 +0100760 lbs_scan_networks(priv, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +0000761 if (priv->surpriseremoved) {
Holger Schurige56188a2007-10-09 14:15:19 +0200762 ret = -1;
763 goto out;
764 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200765
Dan Williamseb8f7332007-05-25 16:25:21 -0400766out:
Holger Schurige56188a2007-10-09 14:15:19 +0200767 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Dan Williamseb8f7332007-05-25 16:25:21 -0400768 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200769}
770
Holger Schurige56188a2007-10-09 14:15:19 +0200771
772
773
774/*********************************************************************/
775/* */
776/* Support for Wireless Extensions */
777/* */
778/*********************************************************************/
779
Holger Schurigffd074f2007-12-07 16:52:10 +0100780
Dan Williams00af0152007-08-02 13:14:56 -0400781#define MAX_CUSTOM_LEN 64
782
Holger Schurig69f90322007-11-23 15:43:44 +0100783static inline char *lbs_translate_scan(struct lbs_private *priv,
David S. Millerccc58052008-06-16 18:50:49 -0700784 struct iw_request_info *info,
785 char *start, char *stop,
786 struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400787{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400788 struct chan_freq_power *cfp;
789 char *current_val; /* For rates */
790 struct iw_event iwe; /* Temporary buffer */
791 int j;
David Woodhousef137e052008-03-03 12:18:59 +0100792#define PERFECT_RSSI ((uint8_t)50)
793#define WORST_RSSI ((uint8_t)0)
794#define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
795 uint8_t rssi;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400796
Holger Schurige56188a2007-10-09 14:15:19 +0200797 lbs_deb_enter(LBS_DEB_SCAN);
798
David Woodhouseaa21c002007-12-08 20:04:36 +0000799 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400800 if (!cfp) {
801 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
Holger Schurige56188a2007-10-09 14:15:19 +0200802 start = NULL;
803 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400804 }
805
Holger Schurigffd074f2007-12-07 16:52:10 +0100806 /* First entry *MUST* be the BSSID */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400807 iwe.cmd = SIOCGIWAP;
808 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
809 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -0700810 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400811
812 /* SSID */
813 iwe.cmd = SIOCGIWESSID;
814 iwe.u.data.flags = 1;
David Woodhousef137e052008-03-03 12:18:59 +0100815 iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IW_ESSID_MAX_SIZE);
David S. Millerccc58052008-06-16 18:50:49 -0700816 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400817
818 /* Mode */
819 iwe.cmd = SIOCGIWMODE;
820 iwe.u.mode = bss->mode;
David S. Millerccc58052008-06-16 18:50:49 -0700821 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400822
823 /* Frequency */
824 iwe.cmd = SIOCGIWFREQ;
825 iwe.u.freq.m = (long)cfp->freq * 100000;
826 iwe.u.freq.e = 1;
David S. Millerccc58052008-06-16 18:50:49 -0700827 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400828
829 /* Add quality statistics */
830 iwe.cmd = IWEVQUAL;
831 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
832 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
833
834 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
835 iwe.u.qual.qual =
David Woodhousef137e052008-03-03 12:18:59 +0100836 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
837 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
838 (RSSI_DIFF * RSSI_DIFF);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400839 if (iwe.u.qual.qual > 100)
840 iwe.u.qual.qual = 100;
841
David Woodhouseaa21c002007-12-08 20:04:36 +0000842 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400843 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
844 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100845 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400846 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400847
Dan Williams80e78ef2007-05-25 22:18:47 -0400848 /* Locally created ad-hoc BSSs won't have beacons if this is the
849 * only station in the adhoc network; so get signal strength
850 * from receive statistics.
851 */
David Woodhousef137e052008-03-03 12:18:59 +0100852 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +0000853 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +0100854 priv->curbssparams.ssid_len,
855 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -0400856 int snr, nf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000857 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
858 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
Dan Williams80e78ef2007-05-25 22:18:47 -0400859 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400860 }
David S. Millerccc58052008-06-16 18:50:49 -0700861 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400862
863 /* Add encryption capability */
864 iwe.cmd = SIOCGIWENCODE;
Dan Williams0c9ca692007-08-02 10:43:44 -0400865 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400866 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
867 } else {
868 iwe.u.data.flags = IW_ENCODE_DISABLED;
869 }
870 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -0700871 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400872
David S. Millerccc58052008-06-16 18:50:49 -0700873 current_val = start + iwe_stream_lcp_len(info);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400874
875 iwe.cmd = SIOCGIWRATE;
876 iwe.u.bitrate.fixed = 0;
877 iwe.u.bitrate.disabled = 0;
878 iwe.u.bitrate.value = 0;
879
Roel Kluin430453f2009-07-28 09:59:47 +0200880 for (j = 0; j < ARRAY_SIZE(bss->rates) && bss->rates[j]; j++) {
Dan Williams8c512762007-08-02 11:40:45 -0400881 /* Bit rate given in 500 kb/s units */
882 iwe.u.bitrate.value = bss->rates[j] * 500000;
David S. Millerccc58052008-06-16 18:50:49 -0700883 current_val = iwe_stream_add_value(info, start, current_val,
884 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400885 }
David Woodhousef137e052008-03-03 12:18:59 +0100886 if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +0000887 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +0100888 priv->curbssparams.ssid_len,
889 bss->ssid, bss->ssid_len)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400890 iwe.u.bitrate.value = 22 * 500000;
David S. Millerccc58052008-06-16 18:50:49 -0700891 current_val = iwe_stream_add_value(info, start, current_val,
David Woodhousef137e052008-03-03 12:18:59 +0100892 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400893 }
894 /* Check if we added any event */
David S. Millerccc58052008-06-16 18:50:49 -0700895 if ((current_val - start) > iwe_stream_lcp_len(info))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400896 start = current_val;
897
898 memset(&iwe, 0, sizeof(iwe));
899 if (bss->wpa_ie_len) {
900 char buf[MAX_WPA_IE_LEN];
901 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
902 iwe.cmd = IWEVGENIE;
903 iwe.u.data.length = bss->wpa_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -0700904 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400905 }
906
907 memset(&iwe, 0, sizeof(iwe));
908 if (bss->rsn_ie_len) {
909 char buf[MAX_WPA_IE_LEN];
910 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
911 iwe.cmd = IWEVGENIE;
912 iwe.u.data.length = bss->rsn_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -0700913 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400914 }
915
Dan Williams00af0152007-08-02 13:14:56 -0400916 if (bss->mesh) {
917 char custom[MAX_CUSTOM_LEN];
918 char *p = custom;
919
920 iwe.cmd = IWEVCUSTOM;
David Woodhousef137e052008-03-03 12:18:59 +0100921 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
Dan Williams00af0152007-08-02 13:14:56 -0400922 iwe.u.data.length = p - custom;
923 if (iwe.u.data.length)
David S. Millerccc58052008-06-16 18:50:49 -0700924 start = iwe_stream_add_point(info, start, stop,
925 &iwe, custom);
Dan Williams00af0152007-08-02 13:14:56 -0400926 }
927
Holger Schurige56188a2007-10-09 14:15:19 +0200928out:
929 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400930 return start;
931}
932
Holger Schurigffd074f2007-12-07 16:52:10 +0100933
934/**
935 * @brief Handle Scan Network ioctl
936 *
937 * @param dev A pointer to net_device structure
938 * @param info A pointer to iw_request_info structure
939 * @param vwrq A pointer to iw_param structure
940 * @param extra A pointer to extra data buf
941 *
942 * @return 0 --success, otherwise fail
943 */
944int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
Holger Schurig52933d82008-03-05 07:05:32 +0100945 union iwreq_data *wrqu, char *extra)
Holger Schurigffd074f2007-12-07 16:52:10 +0100946{
John W. Linville9387b7c2008-09-30 20:59:05 -0400947 DECLARE_SSID_BUF(ssid);
Kiran Divekarab65f642009-02-19 19:32:39 -0500948 struct lbs_private *priv = dev->ml_priv;
Holger Schurig52933d82008-03-05 07:05:32 +0100949 int ret = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100950
Holger Schurig52933d82008-03-05 07:05:32 +0100951 lbs_deb_enter(LBS_DEB_WEXT);
Holger Schurigffd074f2007-12-07 16:52:10 +0100952
Dan Williamsd5db2df2008-08-21 17:51:07 -0400953 if (!priv->radio_on) {
954 ret = -EINVAL;
955 goto out;
956 }
957
Holger Schurig52933d82008-03-05 07:05:32 +0100958 if (!netif_running(dev)) {
959 ret = -ENETDOWN;
960 goto out;
961 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100962
963 /* mac80211 does this:
964 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Holger Schurig52933d82008-03-05 07:05:32 +0100965 if (sdata->type != IEEE80211_IF_TYPE_xxx) {
966 ret = -EOPNOTSUPP;
967 goto out;
968 }
969 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100970
971 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
972 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Holger Schurig52933d82008-03-05 07:05:32 +0100973 struct iw_scan_req *req = (struct iw_scan_req *)extra;
974 priv->scan_ssid_len = req->essid_len;
975 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
976 lbs_deb_wext("set_scan, essid '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400977 print_ssid(ssid, priv->scan_ssid, priv->scan_ssid_len));
Holger Schurig52933d82008-03-05 07:05:32 +0100978 } else {
979 priv->scan_ssid_len = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100980 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100981
982 if (!delayed_work_pending(&priv->scan_work))
983 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100984 msecs_to_jiffies(50));
Holger Schurigffd074f2007-12-07 16:52:10 +0100985 /* set marker that currently a scan is taking place */
Holger Schurig8816edc2008-01-28 17:28:05 +0100986 priv->scan_channel = -1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100987
David Woodhouseaa21c002007-12-08 20:04:36 +0000988 if (priv->surpriseremoved)
Holger Schurig52933d82008-03-05 07:05:32 +0100989 ret = -EIO;
Holger Schurigffd074f2007-12-07 16:52:10 +0100990
Holger Schurig52933d82008-03-05 07:05:32 +0100991out:
992 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
993 return ret;
Holger Schurigffd074f2007-12-07 16:52:10 +0100994}
995
996
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200997/**
Holger Schurige56188a2007-10-09 14:15:19 +0200998 * @brief Handle Retrieve scan table ioctl
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200999 *
1000 * @param dev A pointer to net_device structure
1001 * @param info A pointer to iw_request_info structure
1002 * @param dwrq A pointer to iw_point structure
1003 * @param extra A pointer to extra data buf
1004 *
1005 * @return 0 --success, otherwise fail
1006 */
Holger Schurig10078322007-11-15 18:05:47 -05001007int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
David Woodhousef137e052008-03-03 12:18:59 +01001008 struct iw_point *dwrq, char *extra)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001009{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001010#define SCAN_ITEM_SIZE 128
Kiran Divekarab65f642009-02-19 19:32:39 -05001011 struct lbs_private *priv = dev->ml_priv;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001012 int err = 0;
1013 char *ev = extra;
1014 char *stop = ev + dwrq->length;
David Woodhousef137e052008-03-03 12:18:59 +01001015 struct bss_descriptor *iter_bss;
1016 struct bss_descriptor *safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001017
Holger Schurig52933d82008-03-05 07:05:32 +01001018 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001019
Holger Schurigffd074f2007-12-07 16:52:10 +01001020 /* iwlist should wait until the current scan is finished */
Holger Schurig8816edc2008-01-28 17:28:05 +01001021 if (priv->scan_channel)
Holger Schurigffd074f2007-12-07 16:52:10 +01001022 return -EAGAIN;
1023
Dan Williams80e78ef2007-05-25 22:18:47 -04001024 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
David Woodhousef137e052008-03-03 12:18:59 +01001025 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate)
Holger Schurig10078322007-11-15 18:05:47 -05001026 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
David Woodhousef137e052008-03-03 12:18:59 +01001027 CMD_OPTION_WAITFORRSP, 0, NULL);
Dan Williams80e78ef2007-05-25 22:18:47 -04001028
David Woodhouseaa21c002007-12-08 20:04:36 +00001029 mutex_lock(&priv->lock);
1030 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
David Woodhousef137e052008-03-03 12:18:59 +01001031 char *next_ev;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001032 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001033
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001034 if (stop - ev < SCAN_ITEM_SIZE) {
1035 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001036 break;
1037 }
1038
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001039 /* For mesh device, list only mesh networks */
1040 if (dev == priv->mesh_dev && !iter_bss->mesh)
1041 continue;
1042
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001043 /* Prune old an old scan result */
1044 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1045 if (time_after(jiffies, stale_time)) {
David Woodhousef137e052008-03-03 12:18:59 +01001046 list_move_tail(&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001047 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001048 continue;
1049 }
1050
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001051 /* Translate to WE format this entry */
David S. Millerccc58052008-06-16 18:50:49 -07001052 next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001053 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001054 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001055 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001056 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001057 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001058
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001059 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001060 dwrq->flags = 0;
1061
Holger Schurig52933d82008-03-05 07:05:32 +01001062 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001063 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001064}
1065
Holger Schurige56188a2007-10-09 14:15:19 +02001066
1067
1068
1069/*********************************************************************/
1070/* */
1071/* Command execution */
1072/* */
1073/*********************************************************************/
1074
1075
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001076/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001077 * @brief This function handles the command response of scan
1078 *
Holger Schurige56188a2007-10-09 14:15:19 +02001079 * Called from handle_cmd_response() in cmdrespc.
1080 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001081 * The response buffer for the scan command has the following
1082 * memory layout:
1083 *
1084 * .-----------------------------------------------------------.
1085 * | header (4 * sizeof(u16)): Standard command response hdr |
1086 * .-----------------------------------------------------------.
1087 * | bufsize (u16) : sizeof the BSS Description data |
1088 * .-----------------------------------------------------------.
1089 * | NumOfSet (u8) : Number of BSS Descs returned |
1090 * .-----------------------------------------------------------.
1091 * | BSSDescription data (variable, size given in bufsize) |
1092 * .-----------------------------------------------------------.
1093 * | TLV data (variable, size calculated using header->size, |
1094 * | bufsize and sizeof the fixed fields above) |
1095 * .-----------------------------------------------------------.
1096 *
Holger Schurig69f90322007-11-23 15:43:44 +01001097 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001098 * @param resp A pointer to cmd_ds_command
1099 *
1100 * @return 0 or -1
1101 */
David Woodhousefa62f992008-03-03 12:18:03 +01001102static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1103 struct cmd_header *resp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001104{
David Woodhousefa62f992008-03-03 12:18:03 +01001105 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
David Woodhousef137e052008-03-03 12:18:59 +01001106 struct bss_descriptor *iter_bss;
1107 struct bss_descriptor *safe;
David Woodhousefa62f992008-03-03 12:18:03 +01001108 uint8_t *bssinfo;
1109 uint16_t scanrespsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001110 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001111 int idx;
1112 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001113 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001114
Holger Schurige56188a2007-10-09 14:15:19 +02001115 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001116
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001117 /* Prune old entries from scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001118 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001119 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1120 if (time_before(jiffies, stale_time))
1121 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +00001122 list_move_tail (&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001123 clear_bss_descriptor(iter_bss);
1124 }
1125
David Woodhousefa62f992008-03-03 12:18:03 +01001126 if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1127 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1128 scanresp->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001129 ret = -1;
1130 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001131 }
1132
Cliff Cai2c5b9e512009-05-27 14:03:09 -04001133 bytesleft = get_unaligned_le16(&scanresp->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001134 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001135
David Woodhousee7240ac2007-12-11 17:54:36 -05001136 scanrespsize = le16_to_cpu(resp->size);
David Woodhousefa62f992008-03-03 12:18:03 +01001137 lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001138
David Woodhousefa62f992008-03-03 12:18:03 +01001139 bssinfo = scanresp->bssdesc_and_tlvbuffer;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001140
1141 /* The size of the TLV buffer is equal to the entire command response
1142 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1143 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1144 * response header (S_DS_GEN)
1145 */
David Woodhousefa62f992008-03-03 12:18:03 +01001146 tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1147 + sizeof(scanresp->nr_sets)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001148 + S_DS_GEN);
1149
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001150 /*
David Woodhousefa62f992008-03-03 12:18:03 +01001151 * Process each scan response returned (scanresp->nr_sets). Save
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001152 * the information in the newbssentry and then insert into the
1153 * driver scan table either as an update to an existing entry
1154 * or as an addition at the end of the table
1155 */
David Woodhousefa62f992008-03-03 12:18:03 +01001156 for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001157 struct bss_descriptor new;
David Woodhousefa62f992008-03-03 12:18:03 +01001158 struct bss_descriptor *found = NULL;
1159 struct bss_descriptor *oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001160
1161 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001162 memset(&new, 0, sizeof (struct bss_descriptor));
David Woodhousefa62f992008-03-03 12:18:03 +01001163 if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001164 /* error parsing the scan response, skipped */
1165 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1166 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001167 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001168
1169 /* Try to find this bss in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001170 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001171 if (is_same_network(iter_bss, &new)) {
1172 found = iter_bss;
1173 break;
1174 }
1175
1176 if ((oldest == NULL) ||
1177 (iter_bss->last_scanned < oldest->last_scanned))
1178 oldest = iter_bss;
1179 }
1180
1181 if (found) {
1182 /* found, clear it */
1183 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001184 } else if (!list_empty(&priv->network_free_list)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001185 /* Pull one from the free list */
David Woodhouseaa21c002007-12-08 20:04:36 +00001186 found = list_entry(priv->network_free_list.next,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001187 struct bss_descriptor, list);
David Woodhouseaa21c002007-12-08 20:04:36 +00001188 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001189 } else if (oldest) {
1190 /* If there are no more slots, expire the oldest */
1191 found = oldest;
1192 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001193 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001194 } else {
1195 continue;
1196 }
1197
Johannes Berge1749612008-10-27 15:59:26 -07001198 lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001199
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001200 /* Copy the locally created newbssentry to the scan table */
1201 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001202 }
1203
Holger Schurig9012b282007-05-25 11:27:16 -04001204 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001205
Holger Schurig9012b282007-05-25 11:27:16 -04001206done:
1207 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1208 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001209}