blob: 38b056066e19eb7a82c6f4b9a9caed97b4d6470b [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>
Dan Williamsfcdb53d2007-05-25 16:15:56 -04008#include <linux/etherdevice.h>
Johannes Berg2c7060022008-10-30 22:09:54 +01009#include <linux/if_arp.h>
Vladimir Davydovac630c22007-09-06 21:45:36 -040010#include <asm/unaligned.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040011#include <net/lib80211.h>
12
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020013#include "host.h"
14#include "decl.h"
15#include "dev.h"
16#include "scan.h"
David Woodhousefa62f992008-03-03 12:18:03 +010017#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020018
19//! Approximate amount of data needed to pass a scan result back to iwlist
20#define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
21 + IW_ESSID_MAX_SIZE \
22 + IW_EV_UINT_LEN \
23 + IW_EV_FREQ_LEN \
24 + IW_EV_QUAL_LEN \
25 + IW_ESSID_MAX_SIZE \
26 + IW_EV_PARAM_LEN \
27 + 40) /* 40 for WPAIE */
28
29//! Memory needed to store a max sized channel List TLV for a firmware scan
30#define CHAN_TLV_MAX_SIZE (sizeof(struct mrvlietypesheader) \
31 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
32 * sizeof(struct chanscanparamset)))
33
34//! Memory needed to store a max number/size SSID TLV for a firmware scan
35#define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvlietypes_ssidparamset))
36
David Woodhousefa62f992008-03-03 12:18:03 +010037//! Maximum memory needed for a cmd_ds_802_11_scan with all TLVs at max
38#define MAX_SCAN_CFG_ALLOC (sizeof(struct cmd_ds_802_11_scan) \
39 + CHAN_TLV_MAX_SIZE + SSID_TLV_MAX_SIZE)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020040
41//! The maximum number of channels the firmware can scan per command
42#define MRVDRV_MAX_CHANNELS_PER_SCAN 14
43
44/**
45 * @brief Number of channels to scan per firmware scan command issuance.
46 *
47 * Number restricted to prevent hitting the limit on the amount of scan data
48 * returned in a single firmware scan command.
49 */
50#define MRVDRV_CHANNELS_PER_SCAN_CMD 4
51
52//! Scan time specified in the channel TLV for each channel for passive scans
53#define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
54
55//! Scan time specified in the channel TLV for each channel for active scans
56#define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
57
Johannes Berg2c7060022008-10-30 22:09:54 +010058#define DEFAULT_MAX_SCAN_AGE (15 * HZ)
59
David Woodhousefa62f992008-03-03 12:18:03 +010060static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
61 struct cmd_header *resp);
Holger Schurige56188a2007-10-09 14:15:19 +020062
63/*********************************************************************/
64/* */
65/* Misc helper functions */
66/* */
67/*********************************************************************/
68
Holger Schurig23ff5032008-01-28 17:27:03 +010069/**
70 * @brief Unsets the MSB on basic rates
71 *
72 * Scan through an array and unset the MSB for basic data rates.
73 *
74 * @param rates buffer of data rates
75 * @param len size of buffer
76 */
77static void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
78{
79 int i;
80
81 for (i = 0; i < len; i++)
82 rates[i] &= 0x7f;
83}
84
85
David Woodhousef137e052008-03-03 12:18:59 +010086static inline void clear_bss_descriptor(struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -040087{
88 /* Don't blow away ->list, just BSS data */
89 memset(bss, 0, offsetof(struct bss_descriptor, list));
90}
91
Holger Schurigffd074f2007-12-07 16:52:10 +010092/**
93 * @brief Compare two SSIDs
94 *
95 * @param ssid1 A pointer to ssid to compare
96 * @param ssid2 A pointer to ssid to compare
97 *
98 * @return 0: ssid is same, otherwise is different
99 */
David Woodhousef137e052008-03-03 12:18:59 +0100100int lbs_ssid_cmp(uint8_t *ssid1, uint8_t ssid1_len, uint8_t *ssid2,
101 uint8_t ssid2_len)
Holger Schurigffd074f2007-12-07 16:52:10 +0100102{
103 if (ssid1_len != ssid2_len)
104 return -1;
105
106 return memcmp(ssid1, ssid2, ssid1_len);
107}
108
Holger Schurigffd074f2007-12-07 16:52:10 +0100109static inline int is_same_network(struct bss_descriptor *src,
110 struct bss_descriptor *dst)
111{
112 /* A network is only a duplicate if the channel, BSSID, and ESSID
113 * all match. We treat all <hidden> with the same BSSID and channel
114 * as one network */
115 return ((src->ssid_len == dst->ssid_len) &&
116 (src->channel == dst->channel) &&
117 !compare_ether_addr(src->bssid, dst->bssid) &&
118 !memcmp(src->ssid, dst->ssid, src->ssid_len));
119}
120
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200121
Holger Schurige56188a2007-10-09 14:15:19 +0200122
123
Holger Schurige56188a2007-10-09 14:15:19 +0200124/*********************************************************************/
125/* */
126/* Main scanning support */
127/* */
128/*********************************************************************/
129
Holger Schurige56188a2007-10-09 14:15:19 +0200130/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200131 * @brief Create a channel list for the driver to scan based on region info
132 *
Holger Schurig10078322007-11-15 18:05:47 -0500133 * Only used from lbs_scan_setup_scan_config()
Holger Schurige56188a2007-10-09 14:15:19 +0200134 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200135 * Use the driver region/band information to construct a comprehensive list
136 * of channels to scan. This routine is used for any scan that is not
137 * provided a specific channel list to scan.
138 *
Holger Schurig69f90322007-11-23 15:43:44 +0100139 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200140 * @param scanchanlist Output parameter: resulting channel list to scan
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200141 *
142 * @return void
143 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100144static int lbs_scan_create_channel_list(struct lbs_private *priv,
Holger Schurig52933d82008-03-05 07:05:32 +0100145 struct chanscanparamset *scanchanlist)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200146{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200147 struct region_channel *scanregion;
148 struct chan_freq_power *cfp;
149 int rgnidx;
150 int chanidx;
151 int nextchan;
David Woodhousef137e052008-03-03 12:18:59 +0100152 uint8_t scantype;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200153
154 chanidx = 0;
155
156 /* Set the default scan type to the user specified type, will later
157 * be changed to passive on a per channel basis if restricted by
158 * regulatory requirements (11d or 11h)
159 */
Holger Schurig4f2fdaa2007-08-02 13:12:27 -0400160 scantype = CMD_SCAN_TYPE_ACTIVE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200161
David Woodhouseaa21c002007-12-08 20:04:36 +0000162 for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
David Woodhousef137e052008-03-03 12:18:59 +0100163 if (priv->enable11d && (priv->connect_status != LBS_CONNECTED)
164 && (priv->mesh_connect_status != LBS_CONNECTED)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200165 /* Scan all the supported chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000166 if (!priv->universal_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200167 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000168 scanregion = &priv->universal_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200169
170 /* clear the parsed_region_chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000171 memset(&priv->parsed_region_chan, 0x00,
172 sizeof(priv->parsed_region_chan));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200173 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +0000174 if (!priv->region_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200175 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000176 scanregion = &priv->region_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200177 }
178
David Woodhousef137e052008-03-03 12:18:59 +0100179 for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
180 struct chanscanparamset *chan = &scanchanlist[chanidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200181
182 cfp = scanregion->CFP + nextchan;
183
David Woodhousef137e052008-03-03 12:18:59 +0100184 if (priv->enable11d)
185 scantype = lbs_get_scan_type_11d(cfp->channel,
186 &priv->parsed_region_chan);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200187
David Woodhousef137e052008-03-03 12:18:59 +0100188 if (scanregion->band == BAND_B || scanregion->band == BAND_G)
189 chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200190
Dan Williams0aef64d2007-08-02 11:31:18 -0400191 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
David Woodhousef137e052008-03-03 12:18:59 +0100192 chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
193 chan->chanscanmode.passivescan = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200194 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100195 chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
196 chan->chanscanmode.passivescan = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200197 }
198
David Woodhousef137e052008-03-03 12:18:59 +0100199 chan->channumber = cfp->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200200 }
201 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100202 return chanidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200203}
204
Holger Schurige56188a2007-10-09 14:15:19 +0200205/*
Holger Schurigffd074f2007-12-07 16:52:10 +0100206 * Add SSID TLV of the form:
207 *
208 * TLV-ID SSID 00 00
209 * length 06 00
210 * ssid 4d 4e 54 45 53 54
211 */
Holger Schurig52933d82008-03-05 07:05:32 +0100212static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv)
Dan Williamseb8f7332007-05-25 16:25:21 -0400213{
David Woodhousef137e052008-03-03 12:18:59 +0100214 struct mrvlietypes_ssidparamset *ssid_tlv = (void *)tlv;
215
Holger Schurigffd074f2007-12-07 16:52:10 +0100216 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
Holger Schurig52933d82008-03-05 07:05:32 +0100217 ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len);
218 memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len);
219 return sizeof(ssid_tlv->header) + priv->scan_ssid_len;
Holger Schurigffd074f2007-12-07 16:52:10 +0100220}
Dan Williamseb8f7332007-05-25 16:25:21 -0400221
Holger Schurigffd074f2007-12-07 16:52:10 +0100222/*
223 * Add CHANLIST TLV of the form
224 *
225 * TLV-ID CHANLIST 01 01
226 * length 5b 00
227 * channel 1 00 01 00 00 00 64 00
228 * radio type 00
229 * channel 01
230 * scan type 00
231 * min scan time 00 00
232 * max scan time 64 00
233 * channel 2 00 02 00 00 00 64 00
234 * channel 3 00 03 00 00 00 64 00
235 * channel 4 00 04 00 00 00 64 00
236 * channel 5 00 05 00 00 00 64 00
237 * channel 6 00 06 00 00 00 64 00
238 * channel 7 00 07 00 00 00 64 00
239 * channel 8 00 08 00 00 00 64 00
240 * channel 9 00 09 00 00 00 64 00
241 * channel 10 00 0a 00 00 00 64 00
242 * channel 11 00 0b 00 00 00 64 00
243 * channel 12 00 0c 00 00 00 64 00
244 * channel 13 00 0d 00 00 00 64 00
245 *
246 */
David Woodhousef137e052008-03-03 12:18:59 +0100247static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
248 struct chanscanparamset *chan_list,
249 int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100250{
David Woodhousef137e052008-03-03 12:18:59 +0100251 size_t size = sizeof(struct chanscanparamset) *chan_count;
252 struct mrvlietypes_chanlistparamset *chan_tlv = (void *)tlv;
Dan Williamseb8f7332007-05-25 16:25:21 -0400253
Holger Schurigffd074f2007-12-07 16:52:10 +0100254 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
255 memcpy(chan_tlv->chanscanparam, chan_list, size);
256 chan_tlv->header.len = cpu_to_le16(size);
257 return sizeof(chan_tlv->header) + size;
258}
Dan Williamseb8f7332007-05-25 16:25:21 -0400259
Holger Schurigffd074f2007-12-07 16:52:10 +0100260/*
261 * Add RATES TLV of the form
262 *
263 * TLV-ID RATES 01 00
264 * length 0e 00
265 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
266 *
267 * The rates are in lbs_bg_rates[], but for the 802.11b
268 * rates the high bit isn't set.
269 */
David Woodhousef137e052008-03-03 12:18:59 +0100270static int lbs_scan_add_rates_tlv(uint8_t *tlv)
Holger Schurigffd074f2007-12-07 16:52:10 +0100271{
272 int i;
David Woodhousef137e052008-03-03 12:18:59 +0100273 struct mrvlietypes_ratesparamset *rate_tlv = (void *)tlv;
Holger Schurigffd074f2007-12-07 16:52:10 +0100274
275 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
276 tlv += sizeof(rate_tlv->header);
277 for (i = 0; i < MAX_RATES; i++) {
278 *tlv = lbs_bg_rates[i];
279 if (*tlv == 0)
280 break;
281 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
282 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
283 Note that the values are MBit/s * 2, to mark them as
284 basic rates so that the firmware likes it better */
285 if (*tlv == 0x02 || *tlv == 0x04 ||
286 *tlv == 0x0b || *tlv == 0x16)
287 *tlv |= 0x80;
288 tlv++;
Dan Williamseb8f7332007-05-25 16:25:21 -0400289 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100290 rate_tlv->header.len = cpu_to_le16(i);
291 return sizeof(rate_tlv->header) + i;
292}
Dan Williamseb8f7332007-05-25 16:25:21 -0400293
Holger Schurigffd074f2007-12-07 16:52:10 +0100294/*
295 * Generate the CMD_802_11_SCAN command with the proper tlv
296 * for a bunch of channels.
297 */
David Woodhousefa62f992008-03-03 12:18:03 +0100298static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
Holger Schurig52933d82008-03-05 07:05:32 +0100299 struct chanscanparamset *chan_list, int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100300{
301 int ret = -ENOMEM;
David Woodhousefa62f992008-03-03 12:18:03 +0100302 struct cmd_ds_802_11_scan *scan_cmd;
303 uint8_t *tlv; /* pointer into our current, growing TLV storage area */
Holger Schurigffd074f2007-12-07 16:52:10 +0100304
David Woodhousefa62f992008-03-03 12:18:03 +0100305 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
Holger Schurigc0d43992008-04-29 10:07:56 +0200306 bsstype, chan_list ? chan_list[0].channumber : -1,
307 chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100308
309 /* create the fixed part for scan command */
310 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
311 if (scan_cmd == NULL)
Holger Schurige56188a2007-10-09 14:15:19 +0200312 goto out;
David Woodhousefa62f992008-03-03 12:18:03 +0100313
Holger Schurigffd074f2007-12-07 16:52:10 +0100314 tlv = scan_cmd->tlvbuffer;
Holger Schurig52933d82008-03-05 07:05:32 +0100315 /* TODO: do we need to scan for a specific BSSID?
316 memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
Holger Schurigffd074f2007-12-07 16:52:10 +0100317 scan_cmd->bsstype = bsstype;
Dan Williamseb8f7332007-05-25 16:25:21 -0400318
Holger Schurigffd074f2007-12-07 16:52:10 +0100319 /* add TLVs */
Holger Schurig52933d82008-03-05 07:05:32 +0100320 if (priv->scan_ssid_len)
321 tlv += lbs_scan_add_ssid_tlv(priv, tlv);
Holger Schurigffd074f2007-12-07 16:52:10 +0100322 if (chan_list && chan_count)
323 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
324 tlv += lbs_scan_add_rates_tlv(tlv);
Dan Williamseb8f7332007-05-25 16:25:21 -0400325
Holger Schurigffd074f2007-12-07 16:52:10 +0100326 /* This is the final data we are about to send */
David Woodhousefa62f992008-03-03 12:18:03 +0100327 scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
328 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
329 sizeof(*scan_cmd));
Holger Schurigffd074f2007-12-07 16:52:10 +0100330 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
David Woodhousefa62f992008-03-03 12:18:03 +0100331 tlv - scan_cmd->tlvbuffer);
Dan Williamseb8f7332007-05-25 16:25:21 -0400332
David Woodhousefa62f992008-03-03 12:18:03 +0100333 ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
334 le16_to_cpu(scan_cmd->hdr.size),
335 lbs_ret_80211_scan, 0);
336
Holger Schurige56188a2007-10-09 14:15:19 +0200337out:
Holger Schurigffd074f2007-12-07 16:52:10 +0100338 kfree(scan_cmd);
339 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
340 return ret;
Dan Williamseb8f7332007-05-25 16:25:21 -0400341}
342
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200343/**
344 * @brief Internal function used to start a scan based on an input config
345 *
346 * Use the input user scan configuration information when provided in
347 * order to send the appropriate scan commands to firmware to populate or
348 * update the internal driver scan table
349 *
Holger Schurig69f90322007-11-23 15:43:44 +0100350 * @param priv A pointer to struct lbs_private structure
Holger Schurig52933d82008-03-05 07:05:32 +0100351 * @param full_scan Do a full-scan (blocking)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200352 *
353 * @return 0 or < 0 if error
354 */
Holger Schurig245bf202008-04-02 16:27:42 +0200355int lbs_scan_networks(struct lbs_private *priv, int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200356{
Holger Schurigffd074f2007-12-07 16:52:10 +0100357 int ret = -ENOMEM;
358 struct chanscanparamset *chan_list;
359 struct chanscanparamset *curr_chans;
360 int chan_count;
David Woodhousef137e052008-03-03 12:18:59 +0100361 uint8_t bsstype = CMD_BSS_TYPE_ANY;
Holger Schurigffd074f2007-12-07 16:52:10 +0100362 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
Holger Schurigffd074f2007-12-07 16:52:10 +0100363 union iwreq_data wrqu;
Dan Williamsf8f55102007-05-30 10:12:55 -0400364#ifdef CONFIG_LIBERTAS_DEBUG
Holger Schurigffd074f2007-12-07 16:52:10 +0100365 struct bss_descriptor *iter;
Dan Williamsf8f55102007-05-30 10:12:55 -0400366 int i = 0;
John W. Linville9387b7c2008-09-30 20:59:05 -0400367 DECLARE_SSID_BUF(ssid);
Dan Williamsf8f55102007-05-30 10:12:55 -0400368#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200369
David Woodhousef137e052008-03-03 12:18:59 +0100370 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
Dan Williams2afc0c52007-08-02 13:19:04 -0400371
372 /* Cancel any partial outstanding partial scans if this scan
373 * is a full scan.
374 */
375 if (full_scan && delayed_work_pending(&priv->scan_work))
376 cancel_delayed_work(&priv->scan_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200377
Holger Schurig52933d82008-03-05 07:05:32 +0100378 /* User-specified bsstype or channel list
379 TODO: this can be implemented if some user-space application
380 need the feature. Formerly, it was accessible from debugfs,
381 but then nowhere used.
Holger Schurigffd074f2007-12-07 16:52:10 +0100382 if (user_cfg) {
383 if (user_cfg->bsstype)
Holger Schurig52933d82008-03-05 07:05:32 +0100384 bsstype = user_cfg->bsstype;
385 } */
386
387 lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200388
Holger Schurigffd074f2007-12-07 16:52:10 +0100389 /* Create list of channels to scan */
390 chan_list = kzalloc(sizeof(struct chanscanparamset) *
David Woodhousef137e052008-03-03 12:18:59 +0100391 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
Holger Schurigffd074f2007-12-07 16:52:10 +0100392 if (!chan_list) {
393 lbs_pr_alert("SCAN: chan_list empty\n");
394 goto out;
395 }
396
397 /* We want to scan all channels */
Holger Schurig52933d82008-03-05 07:05:32 +0100398 chan_count = lbs_scan_create_channel_list(priv, chan_list);
Holger Schurigffd074f2007-12-07 16:52:10 +0100399
400 netif_stop_queue(priv->dev);
401 netif_carrier_off(priv->dev);
402 if (priv->mesh_dev) {
David Woodhousea27b9f92007-12-12 00:41:51 -0500403 netif_stop_queue(priv->mesh_dev);
404 netif_carrier_off(priv->mesh_dev);
Holger Schurigffd074f2007-12-07 16:52:10 +0100405 }
406
407 /* Prepare to continue an interrupted scan */
Holger Schurig8816edc2008-01-28 17:28:05 +0100408 lbs_deb_scan("chan_count %d, scan_channel %d\n",
409 chan_count, priv->scan_channel);
Holger Schurigffd074f2007-12-07 16:52:10 +0100410 curr_chans = chan_list;
411 /* advance channel list by already-scanned-channels */
Holger Schurig8816edc2008-01-28 17:28:05 +0100412 if (priv->scan_channel > 0) {
413 curr_chans += priv->scan_channel;
414 chan_count -= priv->scan_channel;
Holger Schurigffd074f2007-12-07 16:52:10 +0100415 }
416
417 /* Send scan command(s)
418 * numchannels contains the number of channels we should maximally scan
419 * chan_count is the total number of channels to scan
420 */
421
422 while (chan_count) {
423 int to_scan = min(numchannels, chan_count);
424 lbs_deb_scan("scanning %d of %d channels\n",
David Woodhousef137e052008-03-03 12:18:59 +0100425 to_scan, chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100426 ret = lbs_do_scan(priv, bsstype, curr_chans,
Holger Schurig52933d82008-03-05 07:05:32 +0100427 to_scan);
Holger Schurigffd074f2007-12-07 16:52:10 +0100428 if (ret) {
429 lbs_pr_err("SCAN_CMD failed\n");
430 goto out2;
431 }
432 curr_chans += to_scan;
433 chan_count -= to_scan;
434
435 /* somehow schedule the next part of the scan */
David Woodhousef137e052008-03-03 12:18:59 +0100436 if (chan_count && !full_scan &&
David Woodhouseaa21c002007-12-08 20:04:36 +0000437 !priv->surpriseremoved) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100438 /* -1 marks just that we're currently scanning */
Holger Schurig8816edc2008-01-28 17:28:05 +0100439 if (priv->scan_channel < 0)
440 priv->scan_channel = to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100441 else
Holger Schurig8816edc2008-01-28 17:28:05 +0100442 priv->scan_channel += to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100443 cancel_delayed_work(&priv->scan_work);
444 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100445 msecs_to_jiffies(300));
Holger Schurigffd074f2007-12-07 16:52:10 +0100446 /* skip over GIWSCAN event */
447 goto out;
448 }
449
450 }
451 memset(&wrqu, 0, sizeof(union iwreq_data));
452 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200453
Dan Williamsf8f55102007-05-30 10:12:55 -0400454#ifdef CONFIG_LIBERTAS_DEBUG
455 /* Dump the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000456 mutex_lock(&priv->lock);
Holger Schurigffd074f2007-12-07 16:52:10 +0100457 lbs_deb_scan("scan table:\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000458 list_for_each_entry(iter, &priv->network_list, list)
Johannes Berge1749612008-10-27 15:59:26 -0700459 lbs_deb_scan("%02d: BSSID %pM, RSSI %d, SSID '%s'\n",
460 i++, iter->bssid, iter->rssi,
John W. Linville9387b7c2008-09-30 20:59:05 -0400461 print_ssid(ssid, iter->ssid, iter->ssid_len));
David Woodhouseaa21c002007-12-08 20:04:36 +0000462 mutex_unlock(&priv->lock);
Dan Williamsf8f55102007-05-30 10:12:55 -0400463#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200464
Holger Schurigffd074f2007-12-07 16:52:10 +0100465out2:
Holger Schurig8816edc2008-01-28 17:28:05 +0100466 priv->scan_channel = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100467
468out:
David Woodhouseaa21c002007-12-08 20:04:36 +0000469 if (priv->connect_status == LBS_CONNECTED) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400470 netif_carrier_on(priv->dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500471 if (!priv->tx_pending_len)
472 netif_wake_queue(priv->dev);
Brajesh Dave01d77d82007-11-20 17:44:14 -0500473 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000474 if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
Brajesh Dave01d77d82007-11-20 17:44:14 -0500475 netif_carrier_on(priv->mesh_dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500476 if (!priv->tx_pending_len)
477 netif_wake_queue(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200478 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100479 kfree(chan_list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200480
Holger Schurig9012b282007-05-25 11:27:16 -0400481 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200482 return ret;
483}
484
Holger Schurig52933d82008-03-05 07:05:32 +0100485void lbs_scan_worker(struct work_struct *work)
486{
487 struct lbs_private *priv =
488 container_of(work, struct lbs_private, scan_work.work);
489
490 lbs_deb_enter(LBS_DEB_SCAN);
491 lbs_scan_networks(priv, 0);
492 lbs_deb_leave(LBS_DEB_SCAN);
493}
494
495
Holger Schurigffd074f2007-12-07 16:52:10 +0100496/*********************************************************************/
497/* */
498/* Result interpretation */
499/* */
500/*********************************************************************/
501
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200502/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200503 * @brief Interpret a BSS scan response returned from the firmware
504 *
505 * Parse the various fixed fields and IEs passed back for a a BSS probe
Holger Schurigffd074f2007-12-07 16:52:10 +0100506 * response or beacon from the scan command. Record information as needed
507 * in the scan table struct bss_descriptor for that entry.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200508 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400509 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200510 *
511 * @return 0 or -1
512 */
Holger Schurig10078322007-11-15 18:05:47 -0500513static int lbs_process_bss(struct bss_descriptor *bss,
David Woodhousef137e052008-03-03 12:18:59 +0100514 uint8_t **pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200515{
Dan Williams5fd164e2009-05-22 20:01:21 -0400516 struct ieee_ie_fh_param_set *fh;
517 struct ieee_ie_ds_param_set *ds;
518 struct ieee_ie_cf_param_set *cf;
519 struct ieee_ie_ibss_param_set *ibss;
John W. Linville9387b7c2008-09-30 20:59:05 -0400520 DECLARE_SSID_BUF(ssid);
Dan Williams8c512762007-08-02 11:40:45 -0400521 struct ieeetypes_countryinfoset *pcountryinfo;
David Woodhousef137e052008-03-03 12:18:59 +0100522 uint8_t *pos, *end, *p;
523 uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
524 uint16_t beaconsize = 0;
Holger Schurig9012b282007-05-25 11:27:16 -0400525 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200526
Holger Schurige56188a2007-10-09 14:15:19 +0200527 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200528
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200529 if (*bytesleft >= sizeof(beaconsize)) {
530 /* Extract & convert beacon size from the command buffer */
Harvey Harrison533dd1b2008-04-29 01:03:36 -0700531 beaconsize = get_unaligned_le16(*pbeaconinfo);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200532 *bytesleft -= sizeof(beaconsize);
533 *pbeaconinfo += sizeof(beaconsize);
534 }
535
536 if (beaconsize == 0 || beaconsize > *bytesleft) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200537 *pbeaconinfo += *bytesleft;
538 *bytesleft = 0;
Holger Schurige56188a2007-10-09 14:15:19 +0200539 ret = -1;
540 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200541 }
542
543 /* Initialize the current working beacon pointer for this BSS iteration */
Dan Williamsab617972007-08-02 10:48:02 -0400544 pos = *pbeaconinfo;
545 end = pos + beaconsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200546
547 /* Advance the return beacon pointer past the current beacon */
548 *pbeaconinfo += beaconsize;
549 *bytesleft -= beaconsize;
550
Dan Williamsab617972007-08-02 10:48:02 -0400551 memcpy(bss->bssid, pos, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -0700552 lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid);
Dan Williamsab617972007-08-02 10:48:02 -0400553 pos += ETH_ALEN;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200554
Dan Williamsab617972007-08-02 10:48:02 -0400555 if ((end - pos) < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400556 lbs_deb_scan("process_bss: Not enough bytes left\n");
Holger Schurige56188a2007-10-09 14:15:19 +0200557 ret = -1;
558 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200559 }
560
561 /*
562 * next 4 fields are RSSI, time stamp, beacon interval,
563 * and capability information
564 */
565
566 /* RSSI is 1 byte long */
Dan Williamsab617972007-08-02 10:48:02 -0400567 bss->rssi = *pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100568 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
Dan Williamsab617972007-08-02 10:48:02 -0400569 pos++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200570
571 /* time stamp is 8 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400572 pos += 8;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200573
574 /* beacon interval is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300575 bss->beaconperiod = get_unaligned_le16(pos);
Dan Williamsab617972007-08-02 10:48:02 -0400576 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200577
578 /* capability information is 2 bytes long */
Ihar Hrachyshka814feef2008-07-09 09:29:58 +0300579 bss->capability = get_unaligned_le16(pos);
Holger Schurigffd074f2007-12-07 16:52:10 +0100580 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
Dan Williamsab617972007-08-02 10:48:02 -0400581 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200582
Dan Williams0c9ca692007-08-02 10:43:44 -0400583 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
Holger Schurigffd074f2007-12-07 16:52:10 +0100584 lbs_deb_scan("process_bss: WEP enabled\n");
Dan Williams0c9ca692007-08-02 10:43:44 -0400585 if (bss->capability & WLAN_CAPABILITY_IBSS)
586 bss->mode = IW_MODE_ADHOC;
587 else
588 bss->mode = IW_MODE_INFRA;
589
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200590 /* rest of the current buffer are IE's */
Holger Schurigffd074f2007-12-07 16:52:10 +0100591 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
Holger Schurigece56192007-08-02 11:53:06 -0400592 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200593
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200594 /* process variable IE */
Dan Williamsab617972007-08-02 10:48:02 -0400595 while (pos <= end - 2) {
Johannes Berg2c7060022008-10-30 22:09:54 +0100596 if (pos + pos[1] > end) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400597 lbs_deb_scan("process_bss: error in processing IE, "
David Woodhousef137e052008-03-03 12:18:59 +0100598 "bytes left < IE length\n");
Dan Williamsab617972007-08-02 10:48:02 -0400599 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200600 }
601
Johannes Berg2c7060022008-10-30 22:09:54 +0100602 switch (pos[0]) {
603 case WLAN_EID_SSID:
604 bss->ssid_len = min_t(int, IEEE80211_MAX_SSID_LEN, pos[1]);
605 memcpy(bss->ssid, pos + 2, bss->ssid_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100606 lbs_deb_scan("got SSID IE: '%s', len %u\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400607 print_ssid(ssid, bss->ssid, bss->ssid_len),
Dan Williamsd8efea22007-05-28 23:54:55 -0400608 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609 break;
610
Johannes Berg2c7060022008-10-30 22:09:54 +0100611 case WLAN_EID_SUPP_RATES:
612 n_basic_rates = min_t(uint8_t, MAX_RATES, pos[1]);
613 memcpy(bss->rates, pos + 2, n_basic_rates);
Dan Williams8c512762007-08-02 11:40:45 -0400614 got_basic_rates = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100615 lbs_deb_scan("got RATES IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200616 break;
617
Johannes Berg2c7060022008-10-30 22:09:54 +0100618 case WLAN_EID_FH_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400619 fh = (struct ieee_ie_fh_param_set *) pos;
620 memcpy(&bss->phy.fh, fh, sizeof(*fh));
Holger Schurigffd074f2007-12-07 16:52:10 +0100621 lbs_deb_scan("got FH IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200622 break;
623
Johannes Berg2c7060022008-10-30 22:09:54 +0100624 case WLAN_EID_DS_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400625 ds = (struct ieee_ie_ds_param_set *) pos;
626 bss->channel = ds->channel;
627 memcpy(&bss->phy.ds, ds, sizeof(*ds));
Holger Schurigffd074f2007-12-07 16:52:10 +0100628 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200629 break;
630
Johannes Berg2c7060022008-10-30 22:09:54 +0100631 case WLAN_EID_CF_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400632 cf = (struct ieee_ie_cf_param_set *) pos;
633 memcpy(&bss->ss.cf, cf, sizeof(*cf));
Holger Schurigffd074f2007-12-07 16:52:10 +0100634 lbs_deb_scan("got CF IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200635 break;
636
Johannes Berg2c7060022008-10-30 22:09:54 +0100637 case WLAN_EID_IBSS_PARAMS:
Dan Williams5fd164e2009-05-22 20:01:21 -0400638 ibss = (struct ieee_ie_ibss_param_set *) pos;
639 bss->atimwindow = ibss->atimwindow;
640 memcpy(&bss->ss.ibss, ibss, sizeof(*ibss));
Holger Schurigffd074f2007-12-07 16:52:10 +0100641 lbs_deb_scan("got IBSS IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200642 break;
643
Johannes Berg2c7060022008-10-30 22:09:54 +0100644 case WLAN_EID_COUNTRY:
Dan Williamsab617972007-08-02 10:48:02 -0400645 pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100646 lbs_deb_scan("got COUNTRY IE\n");
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400647 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200648 || pcountryinfo->len > 254) {
David Woodhousef137e052008-03-03 12:18:59 +0100649 lbs_deb_scan("process_bss: 11D- Err CountryInfo len %d, min %zd, max 254\n",
650 pcountryinfo->len, sizeof(pcountryinfo->countrycode));
Holger Schurig9012b282007-05-25 11:27:16 -0400651 ret = -1;
652 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200653 }
654
David Woodhousef137e052008-03-03 12:18:59 +0100655 memcpy(&bss->countryinfo, pcountryinfo, pcountryinfo->len + 2);
Holger Schurigece56192007-08-02 11:53:06 -0400656 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
David Woodhousef137e052008-03-03 12:18:59 +0100657 (uint8_t *) pcountryinfo,
658 (int) (pcountryinfo->len + 2));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200659 break;
660
Johannes Berg2c7060022008-10-30 22:09:54 +0100661 case WLAN_EID_EXT_SUPP_RATES:
Dan Williamsab617972007-08-02 10:48:02 -0400662 /* only process extended supported rate if data rate is
663 * already found. Data rate IE should come before
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200664 * extended supported rate IE
665 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100666 lbs_deb_scan("got RATESEX IE\n");
667 if (!got_basic_rates) {
668 lbs_deb_scan("... but ignoring it\n");
Dan Williamsab617972007-08-02 10:48:02 -0400669 break;
Holger Schurigffd074f2007-12-07 16:52:10 +0100670 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200671
Johannes Berg2c7060022008-10-30 22:09:54 +0100672 n_ex_rates = pos[1];
Dan Williams8c512762007-08-02 11:40:45 -0400673 if (n_basic_rates + n_ex_rates > MAX_RATES)
674 n_ex_rates = MAX_RATES - n_basic_rates;
Dan Williamsab617972007-08-02 10:48:02 -0400675
Dan Williams8c512762007-08-02 11:40:45 -0400676 p = bss->rates + n_basic_rates;
Johannes Berg2c7060022008-10-30 22:09:54 +0100677 memcpy(p, pos + 2, n_ex_rates);
Dan Williamsab617972007-08-02 10:48:02 -0400678 break;
679
Johannes Berg2c7060022008-10-30 22:09:54 +0100680 case WLAN_EID_GENERIC:
681 if (pos[1] >= 4 &&
682 pos[2] == 0x00 && pos[3] == 0x50 &&
683 pos[4] == 0xf2 && pos[5] == 0x01) {
684 bss->wpa_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
685 memcpy(bss->wpa_ie, pos, bss->wpa_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100686 lbs_deb_scan("got WPA IE\n");
Johannes Berg2c7060022008-10-30 22:09:54 +0100687 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie,
688 bss->wpa_ie_len);
689 } else if (pos[1] >= MARVELL_MESH_IE_LENGTH &&
690 pos[2] == 0x00 && pos[3] == 0x50 &&
Roel Kluinbaf62ee2009-02-03 17:15:57 +0100691 pos[4] == 0x43 && pos[5] == 0x04) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100692 lbs_deb_scan("got mesh IE\n");
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400693 bss->mesh = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100694 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100695 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
Johannes Berg2c7060022008-10-30 22:09:54 +0100696 pos[2], pos[3],
697 pos[4], pos[5],
698 pos[1]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200699 }
700 break;
701
Johannes Berg2c7060022008-10-30 22:09:54 +0100702 case WLAN_EID_RSN:
Holger Schurigffd074f2007-12-07 16:52:10 +0100703 lbs_deb_scan("got RSN IE\n");
Johannes Berg2c7060022008-10-30 22:09:54 +0100704 bss->rsn_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
705 memcpy(bss->rsn_ie, pos, bss->rsn_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100706 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
Johannes Berg2c7060022008-10-30 22:09:54 +0100707 bss->rsn_ie, bss->rsn_ie_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200708 break;
709
Dan Williamsab617972007-08-02 10:48:02 -0400710 default:
Holger Schurigffd074f2007-12-07 16:52:10 +0100711 lbs_deb_scan("got IE 0x%04x, len %d\n",
Johannes Berg2c7060022008-10-30 22:09:54 +0100712 pos[0], pos[1]);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200713 break;
714 }
715
Johannes Berg2c7060022008-10-30 22:09:54 +0100716 pos += pos[1] + 2;
Dan Williamsab617972007-08-02 10:48:02 -0400717 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400718
719 /* Timestamp */
720 bss->last_scanned = jiffies;
Holger Schurig10078322007-11-15 18:05:47 -0500721 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400722
Holger Schurig9012b282007-05-25 11:27:16 -0400723 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200724
Holger Schurig9012b282007-05-25 11:27:16 -0400725done:
726 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
727 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200728}
729
730/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200731 * @brief Send a scan command for all available channels filtered on a spec
732 *
Holger Schurige56188a2007-10-09 14:15:19 +0200733 * Used in association code and from debugfs
734 *
Holger Schurig69f90322007-11-23 15:43:44 +0100735 * @param priv A pointer to struct lbs_private structure
Holger Schurige56188a2007-10-09 14:15:19 +0200736 * @param ssid A pointer to the SSID to scan for
737 * @param ssid_len Length of the SSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200738 *
739 * @return 0-success, otherwise fail
740 */
David Woodhousef137e052008-03-03 12:18:59 +0100741int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
Holger Schurig52933d82008-03-05 07:05:32 +0100742 uint8_t ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200743{
John W. Linville9387b7c2008-09-30 20:59:05 -0400744 DECLARE_SSID_BUF(ssid_buf);
Dan Williamseb8f7332007-05-25 16:25:21 -0400745 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200746
Holger Schurig52933d82008-03-05 07:05:32 +0100747 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400748 print_ssid(ssid_buf, ssid, ssid_len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200749
Dan Williamsd8efea22007-05-28 23:54:55 -0400750 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -0400751 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200752
Holger Schurig52933d82008-03-05 07:05:32 +0100753 memcpy(priv->scan_ssid, ssid, ssid_len);
754 priv->scan_ssid_len = ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200755
Holger Schurig52933d82008-03-05 07:05:32 +0100756 lbs_scan_networks(priv, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +0000757 if (priv->surpriseremoved) {
Holger Schurige56188a2007-10-09 14:15:19 +0200758 ret = -1;
759 goto out;
760 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200761
Dan Williamseb8f7332007-05-25 16:25:21 -0400762out:
Holger Schurige56188a2007-10-09 14:15:19 +0200763 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Dan Williamseb8f7332007-05-25 16:25:21 -0400764 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200765}
766
Holger Schurige56188a2007-10-09 14:15:19 +0200767
768
769
770/*********************************************************************/
771/* */
772/* Support for Wireless Extensions */
773/* */
774/*********************************************************************/
775
Holger Schurigffd074f2007-12-07 16:52:10 +0100776
Dan Williams00af0152007-08-02 13:14:56 -0400777#define MAX_CUSTOM_LEN 64
778
Holger Schurig69f90322007-11-23 15:43:44 +0100779static inline char *lbs_translate_scan(struct lbs_private *priv,
David S. Millerccc58052008-06-16 18:50:49 -0700780 struct iw_request_info *info,
781 char *start, char *stop,
782 struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400783{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400784 struct chan_freq_power *cfp;
785 char *current_val; /* For rates */
786 struct iw_event iwe; /* Temporary buffer */
787 int j;
David Woodhousef137e052008-03-03 12:18:59 +0100788#define PERFECT_RSSI ((uint8_t)50)
789#define WORST_RSSI ((uint8_t)0)
790#define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
791 uint8_t rssi;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400792
Holger Schurige56188a2007-10-09 14:15:19 +0200793 lbs_deb_enter(LBS_DEB_SCAN);
794
David Woodhouseaa21c002007-12-08 20:04:36 +0000795 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400796 if (!cfp) {
797 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
Holger Schurige56188a2007-10-09 14:15:19 +0200798 start = NULL;
799 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400800 }
801
Holger Schurigffd074f2007-12-07 16:52:10 +0100802 /* First entry *MUST* be the BSSID */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400803 iwe.cmd = SIOCGIWAP;
804 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
805 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
David S. Millerccc58052008-06-16 18:50:49 -0700806 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400807
808 /* SSID */
809 iwe.cmd = SIOCGIWESSID;
810 iwe.u.data.flags = 1;
David Woodhousef137e052008-03-03 12:18:59 +0100811 iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IW_ESSID_MAX_SIZE);
David S. Millerccc58052008-06-16 18:50:49 -0700812 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400813
814 /* Mode */
815 iwe.cmd = SIOCGIWMODE;
816 iwe.u.mode = bss->mode;
David S. Millerccc58052008-06-16 18:50:49 -0700817 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400818
819 /* Frequency */
820 iwe.cmd = SIOCGIWFREQ;
821 iwe.u.freq.m = (long)cfp->freq * 100000;
822 iwe.u.freq.e = 1;
David S. Millerccc58052008-06-16 18:50:49 -0700823 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400824
825 /* Add quality statistics */
826 iwe.cmd = IWEVQUAL;
827 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
828 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
829
830 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
831 iwe.u.qual.qual =
David Woodhousef137e052008-03-03 12:18:59 +0100832 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
833 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
834 (RSSI_DIFF * RSSI_DIFF);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400835 if (iwe.u.qual.qual > 100)
836 iwe.u.qual.qual = 100;
837
David Woodhouseaa21c002007-12-08 20:04:36 +0000838 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400839 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
840 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100841 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400842 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400843
Dan Williams80e78ef2007-05-25 22:18:47 -0400844 /* Locally created ad-hoc BSSs won't have beacons if this is the
845 * only station in the adhoc network; so get signal strength
846 * from receive statistics.
847 */
David Woodhousef137e052008-03-03 12:18:59 +0100848 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +0000849 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +0100850 priv->curbssparams.ssid_len,
851 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -0400852 int snr, nf;
David Woodhouseaa21c002007-12-08 20:04:36 +0000853 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
854 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
Dan Williams80e78ef2007-05-25 22:18:47 -0400855 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400856 }
David S. Millerccc58052008-06-16 18:50:49 -0700857 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400858
859 /* Add encryption capability */
860 iwe.cmd = SIOCGIWENCODE;
Dan Williams0c9ca692007-08-02 10:43:44 -0400861 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400862 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
863 } else {
864 iwe.u.data.flags = IW_ENCODE_DISABLED;
865 }
866 iwe.u.data.length = 0;
David S. Millerccc58052008-06-16 18:50:49 -0700867 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400868
David S. Millerccc58052008-06-16 18:50:49 -0700869 current_val = start + iwe_stream_lcp_len(info);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400870
871 iwe.cmd = SIOCGIWRATE;
872 iwe.u.bitrate.fixed = 0;
873 iwe.u.bitrate.disabled = 0;
874 iwe.u.bitrate.value = 0;
875
Dan Williams8c512762007-08-02 11:40:45 -0400876 for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
877 /* Bit rate given in 500 kb/s units */
878 iwe.u.bitrate.value = bss->rates[j] * 500000;
David S. Millerccc58052008-06-16 18:50:49 -0700879 current_val = iwe_stream_add_value(info, start, current_val,
880 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400881 }
David Woodhousef137e052008-03-03 12:18:59 +0100882 if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +0000883 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +0100884 priv->curbssparams.ssid_len,
885 bss->ssid, bss->ssid_len)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400886 iwe.u.bitrate.value = 22 * 500000;
David S. Millerccc58052008-06-16 18:50:49 -0700887 current_val = iwe_stream_add_value(info, start, current_val,
David Woodhousef137e052008-03-03 12:18:59 +0100888 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400889 }
890 /* Check if we added any event */
David S. Millerccc58052008-06-16 18:50:49 -0700891 if ((current_val - start) > iwe_stream_lcp_len(info))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400892 start = current_val;
893
894 memset(&iwe, 0, sizeof(iwe));
895 if (bss->wpa_ie_len) {
896 char buf[MAX_WPA_IE_LEN];
897 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
898 iwe.cmd = IWEVGENIE;
899 iwe.u.data.length = bss->wpa_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -0700900 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400901 }
902
903 memset(&iwe, 0, sizeof(iwe));
904 if (bss->rsn_ie_len) {
905 char buf[MAX_WPA_IE_LEN];
906 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
907 iwe.cmd = IWEVGENIE;
908 iwe.u.data.length = bss->rsn_ie_len;
David S. Millerccc58052008-06-16 18:50:49 -0700909 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400910 }
911
Dan Williams00af0152007-08-02 13:14:56 -0400912 if (bss->mesh) {
913 char custom[MAX_CUSTOM_LEN];
914 char *p = custom;
915
916 iwe.cmd = IWEVCUSTOM;
David Woodhousef137e052008-03-03 12:18:59 +0100917 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
Dan Williams00af0152007-08-02 13:14:56 -0400918 iwe.u.data.length = p - custom;
919 if (iwe.u.data.length)
David S. Millerccc58052008-06-16 18:50:49 -0700920 start = iwe_stream_add_point(info, start, stop,
921 &iwe, custom);
Dan Williams00af0152007-08-02 13:14:56 -0400922 }
923
Holger Schurige56188a2007-10-09 14:15:19 +0200924out:
925 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400926 return start;
927}
928
Holger Schurigffd074f2007-12-07 16:52:10 +0100929
930/**
931 * @brief Handle Scan Network ioctl
932 *
933 * @param dev A pointer to net_device structure
934 * @param info A pointer to iw_request_info structure
935 * @param vwrq A pointer to iw_param structure
936 * @param extra A pointer to extra data buf
937 *
938 * @return 0 --success, otherwise fail
939 */
940int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
Holger Schurig52933d82008-03-05 07:05:32 +0100941 union iwreq_data *wrqu, char *extra)
Holger Schurigffd074f2007-12-07 16:52:10 +0100942{
John W. Linville9387b7c2008-09-30 20:59:05 -0400943 DECLARE_SSID_BUF(ssid);
Kiran Divekarab65f642009-02-19 19:32:39 -0500944 struct lbs_private *priv = dev->ml_priv;
Holger Schurig52933d82008-03-05 07:05:32 +0100945 int ret = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100946
Holger Schurig52933d82008-03-05 07:05:32 +0100947 lbs_deb_enter(LBS_DEB_WEXT);
Holger Schurigffd074f2007-12-07 16:52:10 +0100948
Dan Williamsd5db2df2008-08-21 17:51:07 -0400949 if (!priv->radio_on) {
950 ret = -EINVAL;
951 goto out;
952 }
953
Holger Schurig52933d82008-03-05 07:05:32 +0100954 if (!netif_running(dev)) {
955 ret = -ENETDOWN;
956 goto out;
957 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100958
959 /* mac80211 does this:
960 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Holger Schurig52933d82008-03-05 07:05:32 +0100961 if (sdata->type != IEEE80211_IF_TYPE_xxx) {
962 ret = -EOPNOTSUPP;
963 goto out;
964 }
965 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100966
967 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
968 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Holger Schurig52933d82008-03-05 07:05:32 +0100969 struct iw_scan_req *req = (struct iw_scan_req *)extra;
970 priv->scan_ssid_len = req->essid_len;
971 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
972 lbs_deb_wext("set_scan, essid '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -0400973 print_ssid(ssid, priv->scan_ssid, priv->scan_ssid_len));
Holger Schurig52933d82008-03-05 07:05:32 +0100974 } else {
975 priv->scan_ssid_len = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100976 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100977
978 if (!delayed_work_pending(&priv->scan_work))
979 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100980 msecs_to_jiffies(50));
Holger Schurigffd074f2007-12-07 16:52:10 +0100981 /* set marker that currently a scan is taking place */
Holger Schurig8816edc2008-01-28 17:28:05 +0100982 priv->scan_channel = -1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100983
David Woodhouseaa21c002007-12-08 20:04:36 +0000984 if (priv->surpriseremoved)
Holger Schurig52933d82008-03-05 07:05:32 +0100985 ret = -EIO;
Holger Schurigffd074f2007-12-07 16:52:10 +0100986
Holger Schurig52933d82008-03-05 07:05:32 +0100987out:
988 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
989 return ret;
Holger Schurigffd074f2007-12-07 16:52:10 +0100990}
991
992
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200993/**
Holger Schurige56188a2007-10-09 14:15:19 +0200994 * @brief Handle Retrieve scan table ioctl
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200995 *
996 * @param dev A pointer to net_device structure
997 * @param info A pointer to iw_request_info structure
998 * @param dwrq A pointer to iw_point structure
999 * @param extra A pointer to extra data buf
1000 *
1001 * @return 0 --success, otherwise fail
1002 */
Holger Schurig10078322007-11-15 18:05:47 -05001003int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
David Woodhousef137e052008-03-03 12:18:59 +01001004 struct iw_point *dwrq, char *extra)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001005{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001006#define SCAN_ITEM_SIZE 128
Kiran Divekarab65f642009-02-19 19:32:39 -05001007 struct lbs_private *priv = dev->ml_priv;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001008 int err = 0;
1009 char *ev = extra;
1010 char *stop = ev + dwrq->length;
David Woodhousef137e052008-03-03 12:18:59 +01001011 struct bss_descriptor *iter_bss;
1012 struct bss_descriptor *safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001013
Holger Schurig52933d82008-03-05 07:05:32 +01001014 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001015
Holger Schurigffd074f2007-12-07 16:52:10 +01001016 /* iwlist should wait until the current scan is finished */
Holger Schurig8816edc2008-01-28 17:28:05 +01001017 if (priv->scan_channel)
Holger Schurigffd074f2007-12-07 16:52:10 +01001018 return -EAGAIN;
1019
Dan Williams80e78ef2007-05-25 22:18:47 -04001020 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
David Woodhousef137e052008-03-03 12:18:59 +01001021 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate)
Holger Schurig10078322007-11-15 18:05:47 -05001022 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
David Woodhousef137e052008-03-03 12:18:59 +01001023 CMD_OPTION_WAITFORRSP, 0, NULL);
Dan Williams80e78ef2007-05-25 22:18:47 -04001024
David Woodhouseaa21c002007-12-08 20:04:36 +00001025 mutex_lock(&priv->lock);
1026 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
David Woodhousef137e052008-03-03 12:18:59 +01001027 char *next_ev;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001028 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001029
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001030 if (stop - ev < SCAN_ITEM_SIZE) {
1031 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001032 break;
1033 }
1034
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001035 /* For mesh device, list only mesh networks */
1036 if (dev == priv->mesh_dev && !iter_bss->mesh)
1037 continue;
1038
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001039 /* Prune old an old scan result */
1040 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1041 if (time_after(jiffies, stale_time)) {
David Woodhousef137e052008-03-03 12:18:59 +01001042 list_move_tail(&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001043 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001044 continue;
1045 }
1046
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001047 /* Translate to WE format this entry */
David S. Millerccc58052008-06-16 18:50:49 -07001048 next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001049 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001050 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001051 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001052 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001053 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001054
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001055 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001056 dwrq->flags = 0;
1057
Holger Schurig52933d82008-03-05 07:05:32 +01001058 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001059 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001060}
1061
Holger Schurige56188a2007-10-09 14:15:19 +02001062
1063
1064
1065/*********************************************************************/
1066/* */
1067/* Command execution */
1068/* */
1069/*********************************************************************/
1070
1071
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001072/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001073 * @brief This function handles the command response of scan
1074 *
Holger Schurige56188a2007-10-09 14:15:19 +02001075 * Called from handle_cmd_response() in cmdrespc.
1076 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001077 * The response buffer for the scan command has the following
1078 * memory layout:
1079 *
1080 * .-----------------------------------------------------------.
1081 * | header (4 * sizeof(u16)): Standard command response hdr |
1082 * .-----------------------------------------------------------.
1083 * | bufsize (u16) : sizeof the BSS Description data |
1084 * .-----------------------------------------------------------.
1085 * | NumOfSet (u8) : Number of BSS Descs returned |
1086 * .-----------------------------------------------------------.
1087 * | BSSDescription data (variable, size given in bufsize) |
1088 * .-----------------------------------------------------------.
1089 * | TLV data (variable, size calculated using header->size, |
1090 * | bufsize and sizeof the fixed fields above) |
1091 * .-----------------------------------------------------------.
1092 *
Holger Schurig69f90322007-11-23 15:43:44 +01001093 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001094 * @param resp A pointer to cmd_ds_command
1095 *
1096 * @return 0 or -1
1097 */
David Woodhousefa62f992008-03-03 12:18:03 +01001098static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1099 struct cmd_header *resp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001100{
David Woodhousefa62f992008-03-03 12:18:03 +01001101 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
David Woodhousef137e052008-03-03 12:18:59 +01001102 struct bss_descriptor *iter_bss;
1103 struct bss_descriptor *safe;
David Woodhousefa62f992008-03-03 12:18:03 +01001104 uint8_t *bssinfo;
1105 uint16_t scanrespsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001106 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001107 int idx;
1108 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001109 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001110
Holger Schurige56188a2007-10-09 14:15:19 +02001111 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001112
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001113 /* Prune old entries from scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001114 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001115 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1116 if (time_before(jiffies, stale_time))
1117 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +00001118 list_move_tail (&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001119 clear_bss_descriptor(iter_bss);
1120 }
1121
David Woodhousefa62f992008-03-03 12:18:03 +01001122 if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1123 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1124 scanresp->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001125 ret = -1;
1126 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001127 }
1128
David Woodhousefa62f992008-03-03 12:18:03 +01001129 bytesleft = le16_to_cpu(scanresp->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001130 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001131
David Woodhousee7240ac2007-12-11 17:54:36 -05001132 scanrespsize = le16_to_cpu(resp->size);
David Woodhousefa62f992008-03-03 12:18:03 +01001133 lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001134
David Woodhousefa62f992008-03-03 12:18:03 +01001135 bssinfo = scanresp->bssdesc_and_tlvbuffer;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001136
1137 /* The size of the TLV buffer is equal to the entire command response
1138 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1139 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1140 * response header (S_DS_GEN)
1141 */
David Woodhousefa62f992008-03-03 12:18:03 +01001142 tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1143 + sizeof(scanresp->nr_sets)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001144 + S_DS_GEN);
1145
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001146 /*
David Woodhousefa62f992008-03-03 12:18:03 +01001147 * Process each scan response returned (scanresp->nr_sets). Save
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001148 * the information in the newbssentry and then insert into the
1149 * driver scan table either as an update to an existing entry
1150 * or as an addition at the end of the table
1151 */
David Woodhousefa62f992008-03-03 12:18:03 +01001152 for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001153 struct bss_descriptor new;
David Woodhousefa62f992008-03-03 12:18:03 +01001154 struct bss_descriptor *found = NULL;
1155 struct bss_descriptor *oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001156
1157 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001158 memset(&new, 0, sizeof (struct bss_descriptor));
David Woodhousefa62f992008-03-03 12:18:03 +01001159 if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001160 /* error parsing the scan response, skipped */
1161 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1162 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001163 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001164
1165 /* Try to find this bss in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001166 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001167 if (is_same_network(iter_bss, &new)) {
1168 found = iter_bss;
1169 break;
1170 }
1171
1172 if ((oldest == NULL) ||
1173 (iter_bss->last_scanned < oldest->last_scanned))
1174 oldest = iter_bss;
1175 }
1176
1177 if (found) {
1178 /* found, clear it */
1179 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001180 } else if (!list_empty(&priv->network_free_list)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001181 /* Pull one from the free list */
David Woodhouseaa21c002007-12-08 20:04:36 +00001182 found = list_entry(priv->network_free_list.next,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001183 struct bss_descriptor, list);
David Woodhouseaa21c002007-12-08 20:04:36 +00001184 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001185 } else if (oldest) {
1186 /* If there are no more slots, expire the oldest */
1187 found = oldest;
1188 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001189 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001190 } else {
1191 continue;
1192 }
1193
Johannes Berge1749612008-10-27 15:59:26 -07001194 lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001195
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001196 /* Copy the locally created newbssentry to the scan table */
1197 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001198 }
1199
Holger Schurig9012b282007-05-25 11:27:16 -04001200 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001201
Holger Schurig9012b282007-05-25 11:27:16 -04001202done:
1203 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1204 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001205}