blob: 99f11a56d84e211ebe15f0e14f62cb5f92832bd0 [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 */
7#include <linux/ctype.h>
8#include <linux/if.h>
9#include <linux/netdevice.h>
10#include <linux/wireless.h>
Dan Williamsfcdb53d2007-05-25 16:15:56 -040011#include <linux/etherdevice.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020012
13#include <net/ieee80211.h>
14#include <net/iw_handler.h>
15
Vladimir Davydovac630c22007-09-06 21:45:36 -040016#include <asm/unaligned.h>
17
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020018#include "host.h"
19#include "decl.h"
20#include "dev.h"
21#include "scan.h"
Dan Williams8c512762007-08-02 11:40:45 -040022#include "join.h"
David Woodhousefa62f992008-03-03 12:18:03 +010023#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020024
25//! Approximate amount of data needed to pass a scan result back to iwlist
26#define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
27 + IW_ESSID_MAX_SIZE \
28 + IW_EV_UINT_LEN \
29 + IW_EV_FREQ_LEN \
30 + IW_EV_QUAL_LEN \
31 + IW_ESSID_MAX_SIZE \
32 + IW_EV_PARAM_LEN \
33 + 40) /* 40 for WPAIE */
34
35//! Memory needed to store a max sized channel List TLV for a firmware scan
36#define CHAN_TLV_MAX_SIZE (sizeof(struct mrvlietypesheader) \
37 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
38 * sizeof(struct chanscanparamset)))
39
40//! Memory needed to store a max number/size SSID TLV for a firmware scan
41#define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvlietypes_ssidparamset))
42
David Woodhousefa62f992008-03-03 12:18:03 +010043//! Maximum memory needed for a cmd_ds_802_11_scan with all TLVs at max
44#define MAX_SCAN_CFG_ALLOC (sizeof(struct cmd_ds_802_11_scan) \
45 + CHAN_TLV_MAX_SIZE + SSID_TLV_MAX_SIZE)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020046
47//! The maximum number of channels the firmware can scan per command
48#define MRVDRV_MAX_CHANNELS_PER_SCAN 14
49
50/**
51 * @brief Number of channels to scan per firmware scan command issuance.
52 *
53 * Number restricted to prevent hitting the limit on the amount of scan data
54 * returned in a single firmware scan command.
55 */
56#define MRVDRV_CHANNELS_PER_SCAN_CMD 4
57
58//! Scan time specified in the channel TLV for each channel for passive scans
59#define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
60
61//! Scan time specified in the channel TLV for each channel for active scans
62#define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
63
David Woodhousefa62f992008-03-03 12:18:03 +010064static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
65 struct cmd_header *resp);
Holger Schurige56188a2007-10-09 14:15:19 +020066
67/*********************************************************************/
68/* */
69/* Misc helper functions */
70/* */
71/*********************************************************************/
72
Holger Schurig23ff5032008-01-28 17:27:03 +010073/**
74 * @brief Unsets the MSB on basic rates
75 *
76 * Scan through an array and unset the MSB for basic data rates.
77 *
78 * @param rates buffer of data rates
79 * @param len size of buffer
80 */
81static void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
82{
83 int i;
84
85 for (i = 0; i < len; i++)
86 rates[i] &= 0x7f;
87}
88
89
David Woodhousef137e052008-03-03 12:18:59 +010090static inline void clear_bss_descriptor(struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -040091{
92 /* Don't blow away ->list, just BSS data */
93 memset(bss, 0, offsetof(struct bss_descriptor, list));
94}
95
Holger Schurigffd074f2007-12-07 16:52:10 +010096/**
97 * @brief Compare two SSIDs
98 *
99 * @param ssid1 A pointer to ssid to compare
100 * @param ssid2 A pointer to ssid to compare
101 *
102 * @return 0: ssid is same, otherwise is different
103 */
David Woodhousef137e052008-03-03 12:18:59 +0100104int lbs_ssid_cmp(uint8_t *ssid1, uint8_t ssid1_len, uint8_t *ssid2,
105 uint8_t ssid2_len)
Holger Schurigffd074f2007-12-07 16:52:10 +0100106{
107 if (ssid1_len != ssid2_len)
108 return -1;
109
110 return memcmp(ssid1, ssid2, ssid1_len);
111}
112
Holger Schurig10078322007-11-15 18:05:47 -0500113static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
David Woodhousef137e052008-03-03 12:18:59 +0100114 struct bss_descriptor *match_bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400115{
David Woodhousef137e052008-03-03 12:18:59 +0100116 if (!secinfo->wep_enabled && !secinfo->WPAenabled
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400117 && !secinfo->WPA2enabled
Dan Williamsab617972007-08-02 10:48:02 -0400118 && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
119 && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
David Woodhousef137e052008-03-03 12:18:59 +0100120 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400121 return 1;
David Woodhousef137e052008-03-03 12:18:59 +0100122 else
123 return 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400124}
125
Holger Schurig10078322007-11-15 18:05:47 -0500126static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
David Woodhousef137e052008-03-03 12:18:59 +0100127 struct bss_descriptor *match_bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400128{
David Woodhousef137e052008-03-03 12:18:59 +0100129 if (secinfo->wep_enabled && !secinfo->WPAenabled
130 && !secinfo->WPA2enabled
131 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400132 return 1;
David Woodhousef137e052008-03-03 12:18:59 +0100133 else
134 return 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400135}
136
Holger Schurig10078322007-11-15 18:05:47 -0500137static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
David Woodhousef137e052008-03-03 12:18:59 +0100138 struct bss_descriptor *match_bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400139{
David Woodhousef137e052008-03-03 12:18:59 +0100140 if (!secinfo->wep_enabled && secinfo->WPAenabled
141 && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
142 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
143 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
144 )
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400145 return 1;
David Woodhousef137e052008-03-03 12:18:59 +0100146 else
147 return 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400148}
149
Holger Schurig10078322007-11-15 18:05:47 -0500150static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
David Woodhousef137e052008-03-03 12:18:59 +0100151 struct bss_descriptor *match_bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400152{
David Woodhousef137e052008-03-03 12:18:59 +0100153 if (!secinfo->wep_enabled && secinfo->WPA2enabled
154 && (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
155 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
156 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
157 )
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400158 return 1;
David Woodhousef137e052008-03-03 12:18:59 +0100159 else
160 return 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400161}
162
Holger Schurig10078322007-11-15 18:05:47 -0500163static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
David Woodhousef137e052008-03-03 12:18:59 +0100164 struct bss_descriptor *match_bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400165{
David Woodhousef137e052008-03-03 12:18:59 +0100166 if (!secinfo->wep_enabled && !secinfo->WPAenabled
167 && !secinfo->WPA2enabled
168 && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
169 && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
170 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400171 return 1;
David Woodhousef137e052008-03-03 12:18:59 +0100172 else
173 return 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400174}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200175
Holger Schurigffd074f2007-12-07 16:52:10 +0100176static inline int is_same_network(struct bss_descriptor *src,
177 struct bss_descriptor *dst)
178{
179 /* A network is only a duplicate if the channel, BSSID, and ESSID
180 * all match. We treat all <hidden> with the same BSSID and channel
181 * as one network */
182 return ((src->ssid_len == dst->ssid_len) &&
183 (src->channel == dst->channel) &&
184 !compare_ether_addr(src->bssid, dst->bssid) &&
185 !memcmp(src->ssid, dst->ssid, src->ssid_len));
186}
187
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200188/**
189 * @brief Check if a scanned network compatible with the driver settings
190 *
191 * WEP WPA WPA2 ad-hoc encrypt Network
192 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
193 * 0 0 0 0 NONE 0 0 0 yes No security
194 * 1 0 0 0 NONE 1 0 0 yes Static WEP
195 * 0 1 0 0 x 1x 1 x yes WPA
196 * 0 0 1 0 x 1x x 1 yes WPA2
197 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
198 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
199 *
200 *
David Woodhouseaa21c002007-12-08 20:04:36 +0000201 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200202 * @param index Index in scantable to check against current driver settings
203 * @param mode Network mode: Infrastructure or IBSS
204 *
205 * @return Index in scantable, or error code if negative
206 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000207static int is_network_compatible(struct lbs_private *priv,
David Woodhousef137e052008-03-03 12:18:59 +0100208 struct bss_descriptor *bss, uint8_t mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200209{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400210 int matched = 0;
211
Holger Schurige56188a2007-10-09 14:15:19 +0200212 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200213
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400214 if (bss->mode != mode)
215 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200216
David Woodhouseaa21c002007-12-08 20:04:36 +0000217 if ((matched = match_bss_no_security(&priv->secinfo, bss))) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400218 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000219 } else if ((matched = match_bss_static_wep(&priv->secinfo, bss))) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400220 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000221 } else if ((matched = match_bss_wpa(&priv->secinfo, bss))) {
David Woodhousef137e052008-03-03 12:18:59 +0100222 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
223 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
224 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
225 priv->secinfo.wep_enabled ? "e" : "d",
226 priv->secinfo.WPAenabled ? "e" : "d",
227 priv->secinfo.WPA2enabled ? "e" : "d",
228 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400229 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000230 } else if ((matched = match_bss_wpa2(&priv->secinfo, bss))) {
David Woodhousef137e052008-03-03 12:18:59 +0100231 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
232 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
233 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
234 priv->secinfo.wep_enabled ? "e" : "d",
235 priv->secinfo.WPAenabled ? "e" : "d",
236 priv->secinfo.WPA2enabled ? "e" : "d",
237 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400238 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000239 } else if ((matched = match_bss_dynamic_wep(&priv->secinfo, bss))) {
David Woodhousef137e052008-03-03 12:18:59 +0100240 lbs_deb_scan("is_network_compatible() dynamic WEP: "
241 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
242 bss->wpa_ie[0], bss->rsn_ie[0],
243 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Holger Schurig9012b282007-05-25 11:27:16 -0400244 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200245 }
246
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400247 /* bss security settings don't match those configured on card */
David Woodhousef137e052008-03-03 12:18:59 +0100248 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
249 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
250 bss->wpa_ie[0], bss->rsn_ie[0],
251 priv->secinfo.wep_enabled ? "e" : "d",
252 priv->secinfo.WPAenabled ? "e" : "d",
253 priv->secinfo.WPA2enabled ? "e" : "d",
254 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Holger Schurig9012b282007-05-25 11:27:16 -0400255
256done:
Holger Schurige56188a2007-10-09 14:15:19 +0200257 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400258 return matched;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200259}
260
Holger Schurige56188a2007-10-09 14:15:19 +0200261
262
263
264/*********************************************************************/
265/* */
266/* Main scanning support */
267/* */
268/*********************************************************************/
269
Holger Schurigffd074f2007-12-07 16:52:10 +0100270void lbs_scan_worker(struct work_struct *work)
271{
272 struct lbs_private *priv =
273 container_of(work, struct lbs_private, scan_work.work);
274
275 lbs_deb_enter(LBS_DEB_SCAN);
276 lbs_scan_networks(priv, NULL, 0);
277 lbs_deb_leave(LBS_DEB_SCAN);
278}
279
Holger Schurige56188a2007-10-09 14:15:19 +0200280
281/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200282 * @brief Create a channel list for the driver to scan based on region info
283 *
Holger Schurig10078322007-11-15 18:05:47 -0500284 * Only used from lbs_scan_setup_scan_config()
Holger Schurige56188a2007-10-09 14:15:19 +0200285 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200286 * Use the driver region/band information to construct a comprehensive list
287 * of channels to scan. This routine is used for any scan that is not
288 * provided a specific channel list to scan.
289 *
Holger Schurig69f90322007-11-23 15:43:44 +0100290 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200291 * @param scanchanlist Output parameter: resulting channel list to scan
292 * @param filteredscan Flag indicating whether or not a BSSID or SSID filter
293 * is being sent in the command to firmware. Used to
294 * increase the number of channels sent in a scan
295 * command and to disable the firmware channel scan
296 * filter.
297 *
298 * @return void
299 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100300static int lbs_scan_create_channel_list(struct lbs_private *priv,
David Woodhousef137e052008-03-03 12:18:59 +0100301 struct chanscanparamset *scanchanlist,
302 uint8_t filteredscan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200303{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200304 struct region_channel *scanregion;
305 struct chan_freq_power *cfp;
306 int rgnidx;
307 int chanidx;
308 int nextchan;
David Woodhousef137e052008-03-03 12:18:59 +0100309 uint8_t scantype;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200310
311 chanidx = 0;
312
313 /* Set the default scan type to the user specified type, will later
314 * be changed to passive on a per channel basis if restricted by
315 * regulatory requirements (11d or 11h)
316 */
Holger Schurig4f2fdaa2007-08-02 13:12:27 -0400317 scantype = CMD_SCAN_TYPE_ACTIVE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200318
David Woodhouseaa21c002007-12-08 20:04:36 +0000319 for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
David Woodhousef137e052008-03-03 12:18:59 +0100320 if (priv->enable11d && (priv->connect_status != LBS_CONNECTED)
321 && (priv->mesh_connect_status != LBS_CONNECTED)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200322 /* Scan all the supported chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000323 if (!priv->universal_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200324 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000325 scanregion = &priv->universal_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200326
327 /* clear the parsed_region_chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000328 memset(&priv->parsed_region_chan, 0x00,
329 sizeof(priv->parsed_region_chan));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200330 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +0000331 if (!priv->region_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200332 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000333 scanregion = &priv->region_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200334 }
335
David Woodhousef137e052008-03-03 12:18:59 +0100336 for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
337 struct chanscanparamset *chan = &scanchanlist[chanidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200338
339 cfp = scanregion->CFP + nextchan;
340
David Woodhousef137e052008-03-03 12:18:59 +0100341 if (priv->enable11d)
342 scantype = lbs_get_scan_type_11d(cfp->channel,
343 &priv->parsed_region_chan);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200344
David Woodhousef137e052008-03-03 12:18:59 +0100345 if (scanregion->band == BAND_B || scanregion->band == BAND_G)
346 chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200347
Dan Williams0aef64d2007-08-02 11:31:18 -0400348 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
David Woodhousef137e052008-03-03 12:18:59 +0100349 chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
350 chan->chanscanmode.passivescan = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200351 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100352 chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
353 chan->chanscanmode.passivescan = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200354 }
355
David Woodhousef137e052008-03-03 12:18:59 +0100356 chan->channumber = cfp->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200357
David Woodhousef137e052008-03-03 12:18:59 +0100358 if (filteredscan)
359 chan->chanscanmode.disablechanfilt = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200360 }
361 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100362 return chanidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200363}
364
Dan Williams2afc0c52007-08-02 13:19:04 -0400365
Holger Schurige56188a2007-10-09 14:15:19 +0200366/*
Holger Schurigffd074f2007-12-07 16:52:10 +0100367 * Add SSID TLV of the form:
368 *
369 * TLV-ID SSID 00 00
370 * length 06 00
371 * ssid 4d 4e 54 45 53 54
372 */
David Woodhousef137e052008-03-03 12:18:59 +0100373static int lbs_scan_add_ssid_tlv(uint8_t *tlv,
374 const struct lbs_ioctl_user_scan_cfg *user_cfg)
Dan Williamseb8f7332007-05-25 16:25:21 -0400375{
David Woodhousef137e052008-03-03 12:18:59 +0100376 struct mrvlietypes_ssidparamset *ssid_tlv = (void *)tlv;
377
Holger Schurigffd074f2007-12-07 16:52:10 +0100378 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
379 ssid_tlv->header.len = cpu_to_le16(user_cfg->ssid_len);
380 memcpy(ssid_tlv->ssid, user_cfg->ssid, user_cfg->ssid_len);
381 return sizeof(ssid_tlv->header) + user_cfg->ssid_len;
382}
Dan Williamseb8f7332007-05-25 16:25:21 -0400383
Holger Schurige56188a2007-10-09 14:15:19 +0200384
Holger Schurigffd074f2007-12-07 16:52:10 +0100385/*
386 * Add CHANLIST TLV of the form
387 *
388 * TLV-ID CHANLIST 01 01
389 * length 5b 00
390 * channel 1 00 01 00 00 00 64 00
391 * radio type 00
392 * channel 01
393 * scan type 00
394 * min scan time 00 00
395 * max scan time 64 00
396 * channel 2 00 02 00 00 00 64 00
397 * channel 3 00 03 00 00 00 64 00
398 * channel 4 00 04 00 00 00 64 00
399 * channel 5 00 05 00 00 00 64 00
400 * channel 6 00 06 00 00 00 64 00
401 * channel 7 00 07 00 00 00 64 00
402 * channel 8 00 08 00 00 00 64 00
403 * channel 9 00 09 00 00 00 64 00
404 * channel 10 00 0a 00 00 00 64 00
405 * channel 11 00 0b 00 00 00 64 00
406 * channel 12 00 0c 00 00 00 64 00
407 * channel 13 00 0d 00 00 00 64 00
408 *
409 */
David Woodhousef137e052008-03-03 12:18:59 +0100410static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
411 struct chanscanparamset *chan_list,
412 int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100413{
David Woodhousef137e052008-03-03 12:18:59 +0100414 size_t size = sizeof(struct chanscanparamset) *chan_count;
415 struct mrvlietypes_chanlistparamset *chan_tlv = (void *)tlv;
Dan Williamseb8f7332007-05-25 16:25:21 -0400416
Holger Schurigffd074f2007-12-07 16:52:10 +0100417 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
418 memcpy(chan_tlv->chanscanparam, chan_list, size);
419 chan_tlv->header.len = cpu_to_le16(size);
420 return sizeof(chan_tlv->header) + size;
421}
Dan Williamseb8f7332007-05-25 16:25:21 -0400422
Holger Schurigffd074f2007-12-07 16:52:10 +0100423
424/*
425 * Add RATES TLV of the form
426 *
427 * TLV-ID RATES 01 00
428 * length 0e 00
429 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
430 *
431 * The rates are in lbs_bg_rates[], but for the 802.11b
432 * rates the high bit isn't set.
433 */
David Woodhousef137e052008-03-03 12:18:59 +0100434static int lbs_scan_add_rates_tlv(uint8_t *tlv)
Holger Schurigffd074f2007-12-07 16:52:10 +0100435{
436 int i;
David Woodhousef137e052008-03-03 12:18:59 +0100437 struct mrvlietypes_ratesparamset *rate_tlv = (void *)tlv;
Holger Schurigffd074f2007-12-07 16:52:10 +0100438
439 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
440 tlv += sizeof(rate_tlv->header);
441 for (i = 0; i < MAX_RATES; i++) {
442 *tlv = lbs_bg_rates[i];
443 if (*tlv == 0)
444 break;
445 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
446 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
447 Note that the values are MBit/s * 2, to mark them as
448 basic rates so that the firmware likes it better */
449 if (*tlv == 0x02 || *tlv == 0x04 ||
450 *tlv == 0x0b || *tlv == 0x16)
451 *tlv |= 0x80;
452 tlv++;
Dan Williamseb8f7332007-05-25 16:25:21 -0400453 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100454 rate_tlv->header.len = cpu_to_le16(i);
455 return sizeof(rate_tlv->header) + i;
456}
Dan Williamseb8f7332007-05-25 16:25:21 -0400457
Holger Schurigffd074f2007-12-07 16:52:10 +0100458
459/*
460 * Generate the CMD_802_11_SCAN command with the proper tlv
461 * for a bunch of channels.
462 */
David Woodhousefa62f992008-03-03 12:18:03 +0100463static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
464 struct chanscanparamset *chan_list, int chan_count,
465 const struct lbs_ioctl_user_scan_cfg *user_cfg)
Holger Schurigffd074f2007-12-07 16:52:10 +0100466{
467 int ret = -ENOMEM;
David Woodhousefa62f992008-03-03 12:18:03 +0100468 struct cmd_ds_802_11_scan *scan_cmd;
469 uint8_t *tlv; /* pointer into our current, growing TLV storage area */
Holger Schurigffd074f2007-12-07 16:52:10 +0100470
David Woodhousefa62f992008-03-03 12:18:03 +0100471 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
472 bsstype, chan_list[0].channumber, chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100473
474 /* create the fixed part for scan command */
475 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
476 if (scan_cmd == NULL)
Holger Schurige56188a2007-10-09 14:15:19 +0200477 goto out;
David Woodhousefa62f992008-03-03 12:18:03 +0100478
Holger Schurigffd074f2007-12-07 16:52:10 +0100479 tlv = scan_cmd->tlvbuffer;
480 if (user_cfg)
481 memcpy(scan_cmd->bssid, user_cfg->bssid, ETH_ALEN);
482 scan_cmd->bsstype = bsstype;
Dan Williamseb8f7332007-05-25 16:25:21 -0400483
Holger Schurigffd074f2007-12-07 16:52:10 +0100484 /* add TLVs */
485 if (user_cfg && user_cfg->ssid_len)
486 tlv += lbs_scan_add_ssid_tlv(tlv, user_cfg);
487 if (chan_list && chan_count)
488 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
489 tlv += lbs_scan_add_rates_tlv(tlv);
Dan Williamseb8f7332007-05-25 16:25:21 -0400490
Holger Schurigffd074f2007-12-07 16:52:10 +0100491 /* This is the final data we are about to send */
David Woodhousefa62f992008-03-03 12:18:03 +0100492 scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
493 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
494 sizeof(*scan_cmd));
Holger Schurigffd074f2007-12-07 16:52:10 +0100495 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
David Woodhousefa62f992008-03-03 12:18:03 +0100496 tlv - scan_cmd->tlvbuffer);
Dan Williamseb8f7332007-05-25 16:25:21 -0400497
David Woodhousefa62f992008-03-03 12:18:03 +0100498 ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
499 le16_to_cpu(scan_cmd->hdr.size),
500 lbs_ret_80211_scan, 0);
501
Holger Schurige56188a2007-10-09 14:15:19 +0200502out:
Holger Schurigffd074f2007-12-07 16:52:10 +0100503 kfree(scan_cmd);
504 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
505 return ret;
Dan Williamseb8f7332007-05-25 16:25:21 -0400506}
507
508
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200509/**
510 * @brief Internal function used to start a scan based on an input config
511 *
Holger Schurige56188a2007-10-09 14:15:19 +0200512 * Also used from debugfs
513 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200514 * Use the input user scan configuration information when provided in
515 * order to send the appropriate scan commands to firmware to populate or
516 * update the internal driver scan table
517 *
Holger Schurig69f90322007-11-23 15:43:44 +0100518 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200519 * @param puserscanin Pointer to the input configuration for the requested
520 * scan.
521 *
522 * @return 0 or < 0 if error
523 */
Holger Schurig69f90322007-11-23 15:43:44 +0100524int lbs_scan_networks(struct lbs_private *priv,
David Woodhousef137e052008-03-03 12:18:59 +0100525 const struct lbs_ioctl_user_scan_cfg *user_cfg,
526 int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200527{
Holger Schurigffd074f2007-12-07 16:52:10 +0100528 int ret = -ENOMEM;
529 struct chanscanparamset *chan_list;
530 struct chanscanparamset *curr_chans;
531 int chan_count;
David Woodhousef137e052008-03-03 12:18:59 +0100532 uint8_t bsstype = CMD_BSS_TYPE_ANY;
Holger Schurigffd074f2007-12-07 16:52:10 +0100533 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
534 int filteredscan = 0;
535 union iwreq_data wrqu;
Dan Williamsf8f55102007-05-30 10:12:55 -0400536#ifdef CONFIG_LIBERTAS_DEBUG
Holger Schurigffd074f2007-12-07 16:52:10 +0100537 struct bss_descriptor *iter;
Dan Williamsf8f55102007-05-30 10:12:55 -0400538 int i = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700539 DECLARE_MAC_BUF(mac);
Dan Williamsf8f55102007-05-30 10:12:55 -0400540#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200541
David Woodhousef137e052008-03-03 12:18:59 +0100542 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
Dan Williams2afc0c52007-08-02 13:19:04 -0400543
544 /* Cancel any partial outstanding partial scans if this scan
545 * is a full scan.
546 */
547 if (full_scan && delayed_work_pending(&priv->scan_work))
548 cancel_delayed_work(&priv->scan_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200549
Holger Schurigffd074f2007-12-07 16:52:10 +0100550 /* Determine same scan parameters */
551 if (user_cfg) {
552 if (user_cfg->bsstype)
553 bsstype = user_cfg->bsstype;
David Woodhousef137e052008-03-03 12:18:59 +0100554 if (!is_zero_ether_addr(user_cfg->bssid)) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100555 numchannels = MRVDRV_MAX_CHANNELS_PER_SCAN;
556 filteredscan = 1;
Holger Schurig3cf84092007-08-02 11:50:12 -0400557 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200558 }
David Woodhousef137e052008-03-03 12:18:59 +0100559 lbs_deb_scan("numchannels %d, bsstype %d, filteredscan %d\n",
560 numchannels, bsstype, filteredscan);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200561
Holger Schurigffd074f2007-12-07 16:52:10 +0100562 /* Create list of channels to scan */
563 chan_list = kzalloc(sizeof(struct chanscanparamset) *
David Woodhousef137e052008-03-03 12:18:59 +0100564 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
Holger Schurigffd074f2007-12-07 16:52:10 +0100565 if (!chan_list) {
566 lbs_pr_alert("SCAN: chan_list empty\n");
567 goto out;
568 }
569
570 /* We want to scan all channels */
David Woodhousef137e052008-03-03 12:18:59 +0100571 chan_count = lbs_scan_create_channel_list(priv, chan_list, filteredscan);
Holger Schurigffd074f2007-12-07 16:52:10 +0100572
573 netif_stop_queue(priv->dev);
574 netif_carrier_off(priv->dev);
575 if (priv->mesh_dev) {
David Woodhousea27b9f92007-12-12 00:41:51 -0500576 netif_stop_queue(priv->mesh_dev);
577 netif_carrier_off(priv->mesh_dev);
Holger Schurigffd074f2007-12-07 16:52:10 +0100578 }
579
580 /* Prepare to continue an interrupted scan */
Holger Schurig8816edc2008-01-28 17:28:05 +0100581 lbs_deb_scan("chan_count %d, scan_channel %d\n",
582 chan_count, priv->scan_channel);
Holger Schurigffd074f2007-12-07 16:52:10 +0100583 curr_chans = chan_list;
584 /* advance channel list by already-scanned-channels */
Holger Schurig8816edc2008-01-28 17:28:05 +0100585 if (priv->scan_channel > 0) {
586 curr_chans += priv->scan_channel;
587 chan_count -= priv->scan_channel;
Holger Schurigffd074f2007-12-07 16:52:10 +0100588 }
589
590 /* Send scan command(s)
591 * numchannels contains the number of channels we should maximally scan
592 * chan_count is the total number of channels to scan
593 */
594
595 while (chan_count) {
596 int to_scan = min(numchannels, chan_count);
597 lbs_deb_scan("scanning %d of %d channels\n",
David Woodhousef137e052008-03-03 12:18:59 +0100598 to_scan, chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100599 ret = lbs_do_scan(priv, bsstype, curr_chans,
David Woodhousef137e052008-03-03 12:18:59 +0100600 to_scan, user_cfg);
Holger Schurigffd074f2007-12-07 16:52:10 +0100601 if (ret) {
602 lbs_pr_err("SCAN_CMD failed\n");
603 goto out2;
604 }
605 curr_chans += to_scan;
606 chan_count -= to_scan;
607
608 /* somehow schedule the next part of the scan */
David Woodhousef137e052008-03-03 12:18:59 +0100609 if (chan_count && !full_scan &&
David Woodhouseaa21c002007-12-08 20:04:36 +0000610 !priv->surpriseremoved) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100611 /* -1 marks just that we're currently scanning */
Holger Schurig8816edc2008-01-28 17:28:05 +0100612 if (priv->scan_channel < 0)
613 priv->scan_channel = to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100614 else
Holger Schurig8816edc2008-01-28 17:28:05 +0100615 priv->scan_channel += to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100616 cancel_delayed_work(&priv->scan_work);
617 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100618 msecs_to_jiffies(300));
Holger Schurigffd074f2007-12-07 16:52:10 +0100619 /* skip over GIWSCAN event */
620 goto out;
621 }
622
623 }
624 memset(&wrqu, 0, sizeof(union iwreq_data));
625 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200626
Dan Williamsf8f55102007-05-30 10:12:55 -0400627#ifdef CONFIG_LIBERTAS_DEBUG
628 /* Dump the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000629 mutex_lock(&priv->lock);
Holger Schurigffd074f2007-12-07 16:52:10 +0100630 lbs_deb_scan("scan table:\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000631 list_for_each_entry(iter, &priv->network_list, list)
Holger Schurigffd074f2007-12-07 16:52:10 +0100632 lbs_deb_scan("%02d: BSSID %s, RSSI %d, SSID '%s'\n",
David Woodhousef137e052008-03-03 12:18:59 +0100633 i++, print_mac(mac, iter->bssid), (int)iter->rssi,
634 escape_essid(iter->ssid, iter->ssid_len));
David Woodhouseaa21c002007-12-08 20:04:36 +0000635 mutex_unlock(&priv->lock);
Dan Williamsf8f55102007-05-30 10:12:55 -0400636#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200637
Holger Schurigffd074f2007-12-07 16:52:10 +0100638out2:
Holger Schurig8816edc2008-01-28 17:28:05 +0100639 priv->scan_channel = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100640
641out:
David Woodhouseaa21c002007-12-08 20:04:36 +0000642 if (priv->connect_status == LBS_CONNECTED) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400643 netif_carrier_on(priv->dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500644 if (!priv->tx_pending_len)
645 netif_wake_queue(priv->dev);
Brajesh Dave01d77d82007-11-20 17:44:14 -0500646 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000647 if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
Brajesh Dave01d77d82007-11-20 17:44:14 -0500648 netif_carrier_on(priv->mesh_dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500649 if (!priv->tx_pending_len)
650 netif_wake_queue(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200651 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100652 kfree(chan_list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200653
Holger Schurig9012b282007-05-25 11:27:16 -0400654 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200655 return ret;
656}
657
Holger Schurigffd074f2007-12-07 16:52:10 +0100658
659
660
661/*********************************************************************/
662/* */
663/* Result interpretation */
664/* */
665/*********************************************************************/
666
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200667/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200668 * @brief Interpret a BSS scan response returned from the firmware
669 *
670 * Parse the various fixed fields and IEs passed back for a a BSS probe
Holger Schurigffd074f2007-12-07 16:52:10 +0100671 * response or beacon from the scan command. Record information as needed
672 * in the scan table struct bss_descriptor for that entry.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200673 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400674 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200675 *
676 * @return 0 or -1
677 */
Holger Schurig10078322007-11-15 18:05:47 -0500678static int lbs_process_bss(struct bss_descriptor *bss,
David Woodhousef137e052008-03-03 12:18:59 +0100679 uint8_t **pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200680{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200681 struct ieeetypes_fhparamset *pFH;
682 struct ieeetypes_dsparamset *pDS;
683 struct ieeetypes_cfparamset *pCF;
684 struct ieeetypes_ibssparamset *pibss;
Joe Perches0795af52007-10-03 17:59:30 -0700685 DECLARE_MAC_BUF(mac);
Dan Williams8c512762007-08-02 11:40:45 -0400686 struct ieeetypes_countryinfoset *pcountryinfo;
David Woodhousef137e052008-03-03 12:18:59 +0100687 uint8_t *pos, *end, *p;
688 uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
689 uint16_t beaconsize = 0;
Holger Schurig9012b282007-05-25 11:27:16 -0400690 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200691
Holger Schurige56188a2007-10-09 14:15:19 +0200692 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200693
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200694 if (*bytesleft >= sizeof(beaconsize)) {
695 /* Extract & convert beacon size from the command buffer */
David Woodhousee7240ac2007-12-11 17:54:36 -0500696 beaconsize = le16_to_cpu(get_unaligned((__le16 *)*pbeaconinfo));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200697 *bytesleft -= sizeof(beaconsize);
698 *pbeaconinfo += sizeof(beaconsize);
699 }
700
701 if (beaconsize == 0 || beaconsize > *bytesleft) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200702 *pbeaconinfo += *bytesleft;
703 *bytesleft = 0;
Holger Schurige56188a2007-10-09 14:15:19 +0200704 ret = -1;
705 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200706 }
707
708 /* Initialize the current working beacon pointer for this BSS iteration */
Dan Williamsab617972007-08-02 10:48:02 -0400709 pos = *pbeaconinfo;
710 end = pos + beaconsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200711
712 /* Advance the return beacon pointer past the current beacon */
713 *pbeaconinfo += beaconsize;
714 *bytesleft -= beaconsize;
715
Dan Williamsab617972007-08-02 10:48:02 -0400716 memcpy(bss->bssid, pos, ETH_ALEN);
Holger Schurigffd074f2007-12-07 16:52:10 +0100717 lbs_deb_scan("process_bss: BSSID %s\n", print_mac(mac, bss->bssid));
Dan Williamsab617972007-08-02 10:48:02 -0400718 pos += ETH_ALEN;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200719
Dan Williamsab617972007-08-02 10:48:02 -0400720 if ((end - pos) < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400721 lbs_deb_scan("process_bss: Not enough bytes left\n");
Holger Schurige56188a2007-10-09 14:15:19 +0200722 ret = -1;
723 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200724 }
725
726 /*
727 * next 4 fields are RSSI, time stamp, beacon interval,
728 * and capability information
729 */
730
731 /* RSSI is 1 byte long */
Dan Williamsab617972007-08-02 10:48:02 -0400732 bss->rssi = *pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100733 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
Dan Williamsab617972007-08-02 10:48:02 -0400734 pos++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200735
736 /* time stamp is 8 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400737 pos += 8;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200738
739 /* beacon interval is 2 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400740 bss->beaconperiod = le16_to_cpup((void *) pos);
741 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200742
743 /* capability information is 2 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400744 bss->capability = le16_to_cpup((void *) pos);
Holger Schurigffd074f2007-12-07 16:52:10 +0100745 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
Dan Williamsab617972007-08-02 10:48:02 -0400746 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200747
Dan Williams0c9ca692007-08-02 10:43:44 -0400748 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
Holger Schurigffd074f2007-12-07 16:52:10 +0100749 lbs_deb_scan("process_bss: WEP enabled\n");
Dan Williams0c9ca692007-08-02 10:43:44 -0400750 if (bss->capability & WLAN_CAPABILITY_IBSS)
751 bss->mode = IW_MODE_ADHOC;
752 else
753 bss->mode = IW_MODE_INFRA;
754
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200755 /* rest of the current buffer are IE's */
Holger Schurigffd074f2007-12-07 16:52:10 +0100756 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
Holger Schurigece56192007-08-02 11:53:06 -0400757 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200758
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200759 /* process variable IE */
Dan Williamsab617972007-08-02 10:48:02 -0400760 while (pos <= end - 2) {
David Woodhousef137e052008-03-03 12:18:59 +0100761 struct ieee80211_info_element * elem = (void *)pos;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200762
Dan Williamsab617972007-08-02 10:48:02 -0400763 if (pos + elem->len > end) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400764 lbs_deb_scan("process_bss: error in processing IE, "
David Woodhousef137e052008-03-03 12:18:59 +0100765 "bytes left < IE length\n");
Dan Williamsab617972007-08-02 10:48:02 -0400766 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200767 }
768
Dan Williamsab617972007-08-02 10:48:02 -0400769 switch (elem->id) {
770 case MFIE_TYPE_SSID:
771 bss->ssid_len = elem->len;
772 memcpy(bss->ssid, elem->data, elem->len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100773 lbs_deb_scan("got SSID IE: '%s', len %u\n",
Dan Williamsd8efea22007-05-28 23:54:55 -0400774 escape_essid(bss->ssid, bss->ssid_len),
775 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200776 break;
777
Dan Williamsab617972007-08-02 10:48:02 -0400778 case MFIE_TYPE_RATES:
David Woodhousef137e052008-03-03 12:18:59 +0100779 n_basic_rates = min_t(uint8_t, MAX_RATES, elem->len);
Dan Williams8c512762007-08-02 11:40:45 -0400780 memcpy(bss->rates, elem->data, n_basic_rates);
781 got_basic_rates = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100782 lbs_deb_scan("got RATES IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200783 break;
784
Dan Williamsab617972007-08-02 10:48:02 -0400785 case MFIE_TYPE_FH_SET:
786 pFH = (struct ieeetypes_fhparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400787 memmove(&bss->phyparamset.fhparamset, pFH,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200788 sizeof(struct ieeetypes_fhparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100789 lbs_deb_scan("got FH IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200790 break;
791
Dan Williamsab617972007-08-02 10:48:02 -0400792 case MFIE_TYPE_DS_SET:
793 pDS = (struct ieeetypes_dsparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400794 bss->channel = pDS->currentchan;
795 memcpy(&bss->phyparamset.dsparamset, pDS,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200796 sizeof(struct ieeetypes_dsparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100797 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200798 break;
799
Dan Williamsab617972007-08-02 10:48:02 -0400800 case MFIE_TYPE_CF_SET:
801 pCF = (struct ieeetypes_cfparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400802 memcpy(&bss->ssparamset.cfparamset, pCF,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200803 sizeof(struct ieeetypes_cfparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100804 lbs_deb_scan("got CF IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200805 break;
806
Dan Williamsab617972007-08-02 10:48:02 -0400807 case MFIE_TYPE_IBSS_SET:
808 pibss = (struct ieeetypes_ibssparamset *) pos;
David Woodhousee7240ac2007-12-11 17:54:36 -0500809 bss->atimwindow = le16_to_cpu(pibss->atimwindow);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400810 memmove(&bss->ssparamset.ibssparamset, pibss,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200811 sizeof(struct ieeetypes_ibssparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100812 lbs_deb_scan("got IBSS IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200813 break;
814
Dan Williamsab617972007-08-02 10:48:02 -0400815 case MFIE_TYPE_COUNTRY:
816 pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100817 lbs_deb_scan("got COUNTRY IE\n");
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400818 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200819 || pcountryinfo->len > 254) {
David Woodhousef137e052008-03-03 12:18:59 +0100820 lbs_deb_scan("process_bss: 11D- Err CountryInfo len %d, min %zd, max 254\n",
821 pcountryinfo->len, sizeof(pcountryinfo->countrycode));
Holger Schurig9012b282007-05-25 11:27:16 -0400822 ret = -1;
823 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200824 }
825
David Woodhousef137e052008-03-03 12:18:59 +0100826 memcpy(&bss->countryinfo, pcountryinfo, pcountryinfo->len + 2);
Holger Schurigece56192007-08-02 11:53:06 -0400827 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
David Woodhousef137e052008-03-03 12:18:59 +0100828 (uint8_t *) pcountryinfo,
829 (int) (pcountryinfo->len + 2));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200830 break;
831
Dan Williamsab617972007-08-02 10:48:02 -0400832 case MFIE_TYPE_RATES_EX:
833 /* only process extended supported rate if data rate is
834 * already found. Data rate IE should come before
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200835 * extended supported rate IE
836 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100837 lbs_deb_scan("got RATESEX IE\n");
838 if (!got_basic_rates) {
839 lbs_deb_scan("... but ignoring it\n");
Dan Williamsab617972007-08-02 10:48:02 -0400840 break;
Holger Schurigffd074f2007-12-07 16:52:10 +0100841 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200842
Dan Williams8c512762007-08-02 11:40:45 -0400843 n_ex_rates = elem->len;
844 if (n_basic_rates + n_ex_rates > MAX_RATES)
845 n_ex_rates = MAX_RATES - n_basic_rates;
Dan Williamsab617972007-08-02 10:48:02 -0400846
Dan Williams8c512762007-08-02 11:40:45 -0400847 p = bss->rates + n_basic_rates;
848 memcpy(p, elem->data, n_ex_rates);
Dan Williamsab617972007-08-02 10:48:02 -0400849 break;
850
851 case MFIE_TYPE_GENERIC:
852 if (elem->len >= 4 &&
David Woodhousef137e052008-03-03 12:18:59 +0100853 elem->data[0] == 0x00 && elem->data[1] == 0x50 &&
854 elem->data[2] == 0xf2 && elem->data[3] == 0x01) {
855 bss->wpa_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
Dan Williamsab617972007-08-02 10:48:02 -0400856 memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100857 lbs_deb_scan("got WPA IE\n");
David Woodhousef137e052008-03-03 12:18:59 +0100858 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie, elem->len);
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400859 } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
David Woodhousef137e052008-03-03 12:18:59 +0100860 elem->data[0] == 0x00 && elem->data[1] == 0x50 &&
861 elem->data[2] == 0x43 && elem->data[3] == 0x04) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100862 lbs_deb_scan("got mesh IE\n");
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400863 bss->mesh = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100864 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100865 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
Holger Schurigffd074f2007-12-07 16:52:10 +0100866 elem->data[0], elem->data[1],
867 elem->data[2], elem->data[3],
868 elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200869 }
870 break;
871
Dan Williamsab617972007-08-02 10:48:02 -0400872 case MFIE_TYPE_RSN:
Holger Schurigffd074f2007-12-07 16:52:10 +0100873 lbs_deb_scan("got RSN IE\n");
Dan Williamsab617972007-08-02 10:48:02 -0400874 bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
875 memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100876 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
David Woodhousef137e052008-03-03 12:18:59 +0100877 bss->rsn_ie, elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200878 break;
879
Dan Williamsab617972007-08-02 10:48:02 -0400880 default:
Holger Schurigffd074f2007-12-07 16:52:10 +0100881 lbs_deb_scan("got IE 0x%04x, len %d\n",
David Woodhousef137e052008-03-03 12:18:59 +0100882 elem->id, elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200883 break;
884 }
885
Dan Williamsab617972007-08-02 10:48:02 -0400886 pos += elem->len + 2;
887 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400888
889 /* Timestamp */
890 bss->last_scanned = jiffies;
Holger Schurig10078322007-11-15 18:05:47 -0500891 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400892
Holger Schurig9012b282007-05-25 11:27:16 -0400893 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200894
Holger Schurig9012b282007-05-25 11:27:16 -0400895done:
896 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
897 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200898}
899
900/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200901 * @brief This function finds a specific compatible BSSID in the scan list
902 *
Holger Schurige56188a2007-10-09 14:15:19 +0200903 * Used in association code
904 *
David Woodhouseaa21c002007-12-08 20:04:36 +0000905 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200906 * @param bssid BSSID to find in the scan list
907 * @param mode Network mode: Infrastructure or IBSS
908 *
909 * @return index in BSSID list, or error return code (< 0)
910 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000911struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
David Woodhousef137e052008-03-03 12:18:59 +0100912 uint8_t *bssid, uint8_t mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200913{
David Woodhousef137e052008-03-03 12:18:59 +0100914 struct bss_descriptor *iter_bss;
915 struct bss_descriptor *found_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200916
Holger Schurige56188a2007-10-09 14:15:19 +0200917 lbs_deb_enter(LBS_DEB_SCAN);
918
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200919 if (!bssid)
Holger Schurige56188a2007-10-09 14:15:19 +0200920 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200921
David Woodhousef137e052008-03-03 12:18:59 +0100922 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200923
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400924 /* Look through the scan table for a compatible match. The loop will
925 * continue past a matched bssid that is not compatible in case there
926 * is an AP with multiple SSIDs assigned to the same BSSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200927 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000928 mutex_lock(&priv->lock);
929 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williams3cf20932007-05-25 17:28:30 -0400930 if (compare_ether_addr(iter_bss->bssid, bssid))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400931 continue; /* bssid doesn't match */
932 switch (mode) {
933 case IW_MODE_INFRA:
934 case IW_MODE_ADHOC:
David Woodhouseaa21c002007-12-08 20:04:36 +0000935 if (!is_network_compatible(priv, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200936 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400937 found_bss = iter_bss;
938 break;
939 default:
940 found_bss = iter_bss;
941 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200942 }
943 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000944 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200945
Holger Schurige56188a2007-10-09 14:15:19 +0200946out:
947 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400948 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200949}
950
951/**
952 * @brief This function finds ssid in ssid list.
953 *
Holger Schurige56188a2007-10-09 14:15:19 +0200954 * Used in association code
955 *
David Woodhouseaa21c002007-12-08 20:04:36 +0000956 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200957 * @param ssid SSID to find in the list
958 * @param bssid BSSID to qualify the SSID selection (if provided)
959 * @param mode Network mode: Infrastructure or IBSS
960 *
961 * @return index in BSSID list
962 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000963struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
David Woodhousef137e052008-03-03 12:18:59 +0100964 uint8_t *ssid, uint8_t ssid_len,
965 uint8_t *bssid, uint8_t mode,
966 int channel)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200967{
David Woodhousef137e052008-03-03 12:18:59 +0100968 uint8_t bestrssi = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400969 struct bss_descriptor * iter_bss = NULL;
970 struct bss_descriptor * found_bss = NULL;
971 struct bss_descriptor * tmp_oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200972
Holger Schurige56188a2007-10-09 14:15:19 +0200973 lbs_deb_enter(LBS_DEB_SCAN);
974
David Woodhouseaa21c002007-12-08 20:04:36 +0000975 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200976
David Woodhouseaa21c002007-12-08 20:04:36 +0000977 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400978 if ( !tmp_oldest
979 || (iter_bss->last_scanned < tmp_oldest->last_scanned))
980 tmp_oldest = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200981
Holger Schurig10078322007-11-15 18:05:47 -0500982 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
David Woodhousef137e052008-03-03 12:18:59 +0100983 ssid, ssid_len) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400984 continue; /* ssid doesn't match */
Dan Williams3cf20932007-05-25 17:28:30 -0400985 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400986 continue; /* bssid doesn't match */
Dan Williamsaeea0ab2007-05-25 22:30:48 -0400987 if ((channel > 0) && (iter_bss->channel != channel))
988 continue; /* channel doesn't match */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200989
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400990 switch (mode) {
991 case IW_MODE_INFRA:
992 case IW_MODE_ADHOC:
David Woodhouseaa21c002007-12-08 20:04:36 +0000993 if (!is_network_compatible(priv, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200994 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400995
996 if (bssid) {
997 /* Found requested BSSID */
998 found_bss = iter_bss;
999 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001000 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001001
1002 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1003 bestrssi = SCAN_RSSI(iter_bss->rssi);
1004 found_bss = iter_bss;
1005 }
1006 break;
1007 case IW_MODE_AUTO:
1008 default:
1009 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1010 bestrssi = SCAN_RSSI(iter_bss->rssi);
1011 found_bss = iter_bss;
1012 }
1013 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001014 }
1015 }
1016
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001017out:
David Woodhouseaa21c002007-12-08 20:04:36 +00001018 mutex_unlock(&priv->lock);
Holger Schurige56188a2007-10-09 14:15:19 +02001019 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001020 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001021}
1022
1023/**
1024 * @brief This function finds the best SSID in the Scan List
1025 *
1026 * Search the scan table for the best SSID that also matches the current
1027 * adapter network preference (infrastructure or adhoc)
1028 *
David Woodhouseaa21c002007-12-08 20:04:36 +00001029 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001030 *
1031 * @return index in BSSID list
1032 */
David Woodhousef137e052008-03-03 12:18:59 +01001033static struct bss_descriptor *lbs_find_best_ssid_in_list(struct lbs_private *priv,
1034 uint8_t mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001035{
David Woodhousef137e052008-03-03 12:18:59 +01001036 uint8_t bestrssi = 0;
1037 struct bss_descriptor *iter_bss;
1038 struct bss_descriptor *best_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001039
Holger Schurige56188a2007-10-09 14:15:19 +02001040 lbs_deb_enter(LBS_DEB_SCAN);
1041
David Woodhouseaa21c002007-12-08 20:04:36 +00001042 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001043
David Woodhouseaa21c002007-12-08 20:04:36 +00001044 list_for_each_entry (iter_bss, &priv->network_list, list) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001045 switch (mode) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001046 case IW_MODE_INFRA:
1047 case IW_MODE_ADHOC:
David Woodhouseaa21c002007-12-08 20:04:36 +00001048 if (!is_network_compatible(priv, iter_bss, mode))
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001049 break;
1050 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1051 break;
1052 bestrssi = SCAN_RSSI(iter_bss->rssi);
1053 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001054 break;
Dan Williams0dc5a292007-05-10 22:58:02 -04001055 case IW_MODE_AUTO:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001056 default:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001057 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1058 break;
1059 bestrssi = SCAN_RSSI(iter_bss->rssi);
1060 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001061 break;
1062 }
1063 }
1064
David Woodhouseaa21c002007-12-08 20:04:36 +00001065 mutex_unlock(&priv->lock);
Holger Schurige56188a2007-10-09 14:15:19 +02001066 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001067 return best_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001068}
1069
1070/**
1071 * @brief Find the AP with specific ssid in the scan list
1072 *
Holger Schurige56188a2007-10-09 14:15:19 +02001073 * Used from association worker.
1074 *
Holger Schurig69f90322007-11-23 15:43:44 +01001075 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001076 * @param pSSID A pointer to AP's ssid
1077 *
1078 * @return 0--success, otherwise--fail
1079 */
David Woodhousef137e052008-03-03 12:18:59 +01001080int lbs_find_best_network_ssid(struct lbs_private *priv, uint8_t *out_ssid,
1081 uint8_t *out_ssid_len, uint8_t preferred_mode,
1082 uint8_t *out_mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001083{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001084 int ret = -1;
David Woodhousef137e052008-03-03 12:18:59 +01001085 struct bss_descriptor *found;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001086
Holger Schurige56188a2007-10-09 14:15:19 +02001087 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001088
Holger Schurig10078322007-11-15 18:05:47 -05001089 lbs_scan_networks(priv, NULL, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +00001090 if (priv->surpriseremoved)
Holger Schurige56188a2007-10-09 14:15:19 +02001091 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001092
David Woodhouseaa21c002007-12-08 20:04:36 +00001093 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
Dan Williamsd8efea22007-05-28 23:54:55 -04001094 if (found && (found->ssid_len > 0)) {
1095 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1096 *out_ssid_len = found->ssid_len;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001097 *out_mode = found->mode;
1098 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001099 }
1100
Holger Schurige56188a2007-10-09 14:15:19 +02001101out:
Holger Schurig9012b282007-05-25 11:27:16 -04001102 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001103 return ret;
1104}
1105
Holger Schurige56188a2007-10-09 14:15:19 +02001106
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001107/**
1108 * @brief Send a scan command for all available channels filtered on a spec
1109 *
Holger Schurige56188a2007-10-09 14:15:19 +02001110 * Used in association code and from debugfs
1111 *
Holger Schurig69f90322007-11-23 15:43:44 +01001112 * @param priv A pointer to struct lbs_private structure
Holger Schurige56188a2007-10-09 14:15:19 +02001113 * @param ssid A pointer to the SSID to scan for
1114 * @param ssid_len Length of the SSID
1115 * @param clear_ssid Should existing scan results with this SSID
1116 * be cleared?
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001117 *
1118 * @return 0-success, otherwise fail
1119 */
David Woodhousef137e052008-03-03 12:18:59 +01001120int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
1121 uint8_t ssid_len, uint8_t clear_ssid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001122{
Holger Schurig10078322007-11-15 18:05:47 -05001123 struct lbs_ioctl_user_scan_cfg scancfg;
Dan Williamseb8f7332007-05-25 16:25:21 -04001124 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001125
Holger Schurige56188a2007-10-09 14:15:19 +02001126 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s', clear %d",
David Woodhousef137e052008-03-03 12:18:59 +01001127 escape_essid(ssid, ssid_len), clear_ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001128
Dan Williamsd8efea22007-05-28 23:54:55 -04001129 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -04001130 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001131
1132 memset(&scancfg, 0x00, sizeof(scancfg));
Dan Williamsd8efea22007-05-28 23:54:55 -04001133 memcpy(scancfg.ssid, ssid, ssid_len);
1134 scancfg.ssid_len = ssid_len;
Dan Williamseb8f7332007-05-25 16:25:21 -04001135 scancfg.clear_ssid = clear_ssid;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001136
Holger Schurig10078322007-11-15 18:05:47 -05001137 lbs_scan_networks(priv, &scancfg, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +00001138 if (priv->surpriseremoved) {
Holger Schurige56188a2007-10-09 14:15:19 +02001139 ret = -1;
1140 goto out;
1141 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001142
Dan Williamseb8f7332007-05-25 16:25:21 -04001143out:
Holger Schurige56188a2007-10-09 14:15:19 +02001144 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Dan Williamseb8f7332007-05-25 16:25:21 -04001145 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001146}
1147
Holger Schurige56188a2007-10-09 14:15:19 +02001148
1149
1150
1151/*********************************************************************/
1152/* */
1153/* Support for Wireless Extensions */
1154/* */
1155/*********************************************************************/
1156
Holger Schurigffd074f2007-12-07 16:52:10 +01001157
Dan Williams00af0152007-08-02 13:14:56 -04001158#define MAX_CUSTOM_LEN 64
1159
Holger Schurig69f90322007-11-23 15:43:44 +01001160static inline char *lbs_translate_scan(struct lbs_private *priv,
David Woodhousef137e052008-03-03 12:18:59 +01001161 char *start, char *stop,
1162 struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001163{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001164 struct chan_freq_power *cfp;
1165 char *current_val; /* For rates */
1166 struct iw_event iwe; /* Temporary buffer */
1167 int j;
David Woodhousef137e052008-03-03 12:18:59 +01001168#define PERFECT_RSSI ((uint8_t)50)
1169#define WORST_RSSI ((uint8_t)0)
1170#define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
1171 uint8_t rssi;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001172
Holger Schurige56188a2007-10-09 14:15:19 +02001173 lbs_deb_enter(LBS_DEB_SCAN);
1174
David Woodhouseaa21c002007-12-08 20:04:36 +00001175 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001176 if (!cfp) {
1177 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
Holger Schurige56188a2007-10-09 14:15:19 +02001178 start = NULL;
1179 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001180 }
1181
Holger Schurigffd074f2007-12-07 16:52:10 +01001182 /* First entry *MUST* be the BSSID */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001183 iwe.cmd = SIOCGIWAP;
1184 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1185 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
1186 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
1187
1188 /* SSID */
1189 iwe.cmd = SIOCGIWESSID;
1190 iwe.u.data.flags = 1;
David Woodhousef137e052008-03-03 12:18:59 +01001191 iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IW_ESSID_MAX_SIZE);
Dan Williamsd8efea22007-05-28 23:54:55 -04001192 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001193
1194 /* Mode */
1195 iwe.cmd = SIOCGIWMODE;
1196 iwe.u.mode = bss->mode;
1197 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
1198
1199 /* Frequency */
1200 iwe.cmd = SIOCGIWFREQ;
1201 iwe.u.freq.m = (long)cfp->freq * 100000;
1202 iwe.u.freq.e = 1;
1203 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
1204
1205 /* Add quality statistics */
1206 iwe.cmd = IWEVQUAL;
1207 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
1208 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
1209
1210 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
1211 iwe.u.qual.qual =
David Woodhousef137e052008-03-03 12:18:59 +01001212 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
1213 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
1214 (RSSI_DIFF * RSSI_DIFF);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001215 if (iwe.u.qual.qual > 100)
1216 iwe.u.qual.qual = 100;
1217
David Woodhouseaa21c002007-12-08 20:04:36 +00001218 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001219 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
1220 } else {
David Woodhousef137e052008-03-03 12:18:59 +01001221 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001222 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001223
Dan Williams80e78ef2007-05-25 22:18:47 -04001224 /* Locally created ad-hoc BSSs won't have beacons if this is the
1225 * only station in the adhoc network; so get signal strength
1226 * from receive statistics.
1227 */
David Woodhousef137e052008-03-03 12:18:59 +01001228 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +00001229 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +01001230 priv->curbssparams.ssid_len,
1231 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -04001232 int snr, nf;
David Woodhouseaa21c002007-12-08 20:04:36 +00001233 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1234 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
Dan Williams80e78ef2007-05-25 22:18:47 -04001235 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001236 }
1237 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
1238
1239 /* Add encryption capability */
1240 iwe.cmd = SIOCGIWENCODE;
Dan Williams0c9ca692007-08-02 10:43:44 -04001241 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001242 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1243 } else {
1244 iwe.u.data.flags = IW_ENCODE_DISABLED;
1245 }
1246 iwe.u.data.length = 0;
Dan Williamsd8efea22007-05-28 23:54:55 -04001247 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001248
1249 current_val = start + IW_EV_LCP_LEN;
1250
1251 iwe.cmd = SIOCGIWRATE;
1252 iwe.u.bitrate.fixed = 0;
1253 iwe.u.bitrate.disabled = 0;
1254 iwe.u.bitrate.value = 0;
1255
Dan Williams8c512762007-08-02 11:40:45 -04001256 for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
1257 /* Bit rate given in 500 kb/s units */
1258 iwe.u.bitrate.value = bss->rates[j] * 500000;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001259 current_val = iwe_stream_add_value(start, current_val,
1260 stop, &iwe, IW_EV_PARAM_LEN);
1261 }
David Woodhousef137e052008-03-03 12:18:59 +01001262 if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +00001263 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +01001264 priv->curbssparams.ssid_len,
1265 bss->ssid, bss->ssid_len)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001266 iwe.u.bitrate.value = 22 * 500000;
1267 current_val = iwe_stream_add_value(start, current_val,
David Woodhousef137e052008-03-03 12:18:59 +01001268 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001269 }
1270 /* Check if we added any event */
1271 if((current_val - start) > IW_EV_LCP_LEN)
1272 start = current_val;
1273
1274 memset(&iwe, 0, sizeof(iwe));
1275 if (bss->wpa_ie_len) {
1276 char buf[MAX_WPA_IE_LEN];
1277 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
1278 iwe.cmd = IWEVGENIE;
1279 iwe.u.data.length = bss->wpa_ie_len;
1280 start = iwe_stream_add_point(start, stop, &iwe, buf);
1281 }
1282
1283 memset(&iwe, 0, sizeof(iwe));
1284 if (bss->rsn_ie_len) {
1285 char buf[MAX_WPA_IE_LEN];
1286 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
1287 iwe.cmd = IWEVGENIE;
1288 iwe.u.data.length = bss->rsn_ie_len;
1289 start = iwe_stream_add_point(start, stop, &iwe, buf);
1290 }
1291
Dan Williams00af0152007-08-02 13:14:56 -04001292 if (bss->mesh) {
1293 char custom[MAX_CUSTOM_LEN];
1294 char *p = custom;
1295
1296 iwe.cmd = IWEVCUSTOM;
David Woodhousef137e052008-03-03 12:18:59 +01001297 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
Dan Williams00af0152007-08-02 13:14:56 -04001298 iwe.u.data.length = p - custom;
1299 if (iwe.u.data.length)
1300 start = iwe_stream_add_point(start, stop, &iwe, custom);
1301 }
1302
Holger Schurige56188a2007-10-09 14:15:19 +02001303out:
1304 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001305 return start;
1306}
1307
Holger Schurigffd074f2007-12-07 16:52:10 +01001308
1309/**
1310 * @brief Handle Scan Network ioctl
1311 *
1312 * @param dev A pointer to net_device structure
1313 * @param info A pointer to iw_request_info structure
1314 * @param vwrq A pointer to iw_param structure
1315 * @param extra A pointer to extra data buf
1316 *
1317 * @return 0 --success, otherwise fail
1318 */
1319int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
David Woodhousef137e052008-03-03 12:18:59 +01001320 struct iw_param *wrqu, char *extra)
Holger Schurigffd074f2007-12-07 16:52:10 +01001321{
1322 struct lbs_private *priv = dev->priv;
Holger Schurigffd074f2007-12-07 16:52:10 +01001323
1324 lbs_deb_enter(LBS_DEB_SCAN);
1325
1326 if (!netif_running(dev))
1327 return -ENETDOWN;
1328
1329 /* mac80211 does this:
1330 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1331 if (sdata->type != IEEE80211_IF_TYPE_xxx)
1332 return -EOPNOTSUPP;
1333
1334 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
1335 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1336 req = (struct iw_scan_req *)extra;
1337 ssid = req->essid;
1338 ssid_len = req->essid_len;
1339 }
1340 */
1341
1342 if (!delayed_work_pending(&priv->scan_work))
1343 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +01001344 msecs_to_jiffies(50));
Holger Schurigffd074f2007-12-07 16:52:10 +01001345 /* set marker that currently a scan is taking place */
Holger Schurig8816edc2008-01-28 17:28:05 +01001346 priv->scan_channel = -1;
Holger Schurigffd074f2007-12-07 16:52:10 +01001347
David Woodhouseaa21c002007-12-08 20:04:36 +00001348 if (priv->surpriseremoved)
Holger Schurigffd074f2007-12-07 16:52:10 +01001349 return -EIO;
1350
1351 lbs_deb_leave(LBS_DEB_SCAN);
1352 return 0;
1353}
1354
1355
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001356/**
Holger Schurige56188a2007-10-09 14:15:19 +02001357 * @brief Handle Retrieve scan table ioctl
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001358 *
1359 * @param dev A pointer to net_device structure
1360 * @param info A pointer to iw_request_info structure
1361 * @param dwrq A pointer to iw_point structure
1362 * @param extra A pointer to extra data buf
1363 *
1364 * @return 0 --success, otherwise fail
1365 */
Holger Schurig10078322007-11-15 18:05:47 -05001366int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
David Woodhousef137e052008-03-03 12:18:59 +01001367 struct iw_point *dwrq, char *extra)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001368{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001369#define SCAN_ITEM_SIZE 128
Holger Schurig69f90322007-11-23 15:43:44 +01001370 struct lbs_private *priv = dev->priv;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001371 int err = 0;
1372 char *ev = extra;
1373 char *stop = ev + dwrq->length;
David Woodhousef137e052008-03-03 12:18:59 +01001374 struct bss_descriptor *iter_bss;
1375 struct bss_descriptor *safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001376
Holger Schurige56188a2007-10-09 14:15:19 +02001377 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001378
Holger Schurigffd074f2007-12-07 16:52:10 +01001379 /* iwlist should wait until the current scan is finished */
Holger Schurig8816edc2008-01-28 17:28:05 +01001380 if (priv->scan_channel)
Holger Schurigffd074f2007-12-07 16:52:10 +01001381 return -EAGAIN;
1382
Dan Williams80e78ef2007-05-25 22:18:47 -04001383 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
David Woodhousef137e052008-03-03 12:18:59 +01001384 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate)
Holger Schurig10078322007-11-15 18:05:47 -05001385 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
David Woodhousef137e052008-03-03 12:18:59 +01001386 CMD_OPTION_WAITFORRSP, 0, NULL);
Dan Williams80e78ef2007-05-25 22:18:47 -04001387
David Woodhouseaa21c002007-12-08 20:04:36 +00001388 mutex_lock(&priv->lock);
1389 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
David Woodhousef137e052008-03-03 12:18:59 +01001390 char *next_ev;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001391 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001392
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001393 if (stop - ev < SCAN_ITEM_SIZE) {
1394 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001395 break;
1396 }
1397
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001398 /* For mesh device, list only mesh networks */
1399 if (dev == priv->mesh_dev && !iter_bss->mesh)
1400 continue;
1401
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001402 /* Prune old an old scan result */
1403 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1404 if (time_after(jiffies, stale_time)) {
David Woodhousef137e052008-03-03 12:18:59 +01001405 list_move_tail(&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001406 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001407 continue;
1408 }
1409
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001410 /* Translate to WE format this entry */
Holger Schurig10078322007-11-15 18:05:47 -05001411 next_ev = lbs_translate_scan(priv, ev, stop, iter_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001412 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001413 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001414 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001415 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001416 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001417
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001418 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001419 dwrq->flags = 0;
1420
Holger Schurige56188a2007-10-09 14:15:19 +02001421 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", err);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001422 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001423}
1424
Holger Schurige56188a2007-10-09 14:15:19 +02001425
1426
1427
1428/*********************************************************************/
1429/* */
1430/* Command execution */
1431/* */
1432/*********************************************************************/
1433
1434
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001435/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001436 * @brief This function handles the command response of scan
1437 *
Holger Schurige56188a2007-10-09 14:15:19 +02001438 * Called from handle_cmd_response() in cmdrespc.
1439 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001440 * The response buffer for the scan command has the following
1441 * memory layout:
1442 *
1443 * .-----------------------------------------------------------.
1444 * | header (4 * sizeof(u16)): Standard command response hdr |
1445 * .-----------------------------------------------------------.
1446 * | bufsize (u16) : sizeof the BSS Description data |
1447 * .-----------------------------------------------------------.
1448 * | NumOfSet (u8) : Number of BSS Descs returned |
1449 * .-----------------------------------------------------------.
1450 * | BSSDescription data (variable, size given in bufsize) |
1451 * .-----------------------------------------------------------.
1452 * | TLV data (variable, size calculated using header->size, |
1453 * | bufsize and sizeof the fixed fields above) |
1454 * .-----------------------------------------------------------.
1455 *
Holger Schurig69f90322007-11-23 15:43:44 +01001456 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001457 * @param resp A pointer to cmd_ds_command
1458 *
1459 * @return 0 or -1
1460 */
David Woodhousefa62f992008-03-03 12:18:03 +01001461static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1462 struct cmd_header *resp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001463{
David Woodhousefa62f992008-03-03 12:18:03 +01001464 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
David Woodhousef137e052008-03-03 12:18:59 +01001465 struct bss_descriptor *iter_bss;
1466 struct bss_descriptor *safe;
David Woodhousefa62f992008-03-03 12:18:03 +01001467 uint8_t *bssinfo;
1468 uint16_t scanrespsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001469 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001470 int idx;
1471 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001472 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001473
Holger Schurige56188a2007-10-09 14:15:19 +02001474 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001475
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001476 /* Prune old entries from scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001477 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001478 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1479 if (time_before(jiffies, stale_time))
1480 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +00001481 list_move_tail (&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001482 clear_bss_descriptor(iter_bss);
1483 }
1484
David Woodhousefa62f992008-03-03 12:18:03 +01001485 if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1486 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1487 scanresp->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001488 ret = -1;
1489 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001490 }
1491
David Woodhousefa62f992008-03-03 12:18:03 +01001492 bytesleft = le16_to_cpu(scanresp->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001493 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001494
David Woodhousee7240ac2007-12-11 17:54:36 -05001495 scanrespsize = le16_to_cpu(resp->size);
David Woodhousefa62f992008-03-03 12:18:03 +01001496 lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001497
David Woodhousefa62f992008-03-03 12:18:03 +01001498 bssinfo = scanresp->bssdesc_and_tlvbuffer;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001499
1500 /* The size of the TLV buffer is equal to the entire command response
1501 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1502 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1503 * response header (S_DS_GEN)
1504 */
David Woodhousefa62f992008-03-03 12:18:03 +01001505 tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1506 + sizeof(scanresp->nr_sets)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001507 + S_DS_GEN);
1508
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001509 /*
David Woodhousefa62f992008-03-03 12:18:03 +01001510 * Process each scan response returned (scanresp->nr_sets). Save
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001511 * the information in the newbssentry and then insert into the
1512 * driver scan table either as an update to an existing entry
1513 * or as an addition at the end of the table
1514 */
David Woodhousefa62f992008-03-03 12:18:03 +01001515 for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001516 struct bss_descriptor new;
David Woodhousefa62f992008-03-03 12:18:03 +01001517 struct bss_descriptor *found = NULL;
1518 struct bss_descriptor *oldest = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07001519 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001520
1521 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001522 memset(&new, 0, sizeof (struct bss_descriptor));
David Woodhousefa62f992008-03-03 12:18:03 +01001523 if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001524 /* error parsing the scan response, skipped */
1525 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1526 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001527 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001528
1529 /* Try to find this bss in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001530 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001531 if (is_same_network(iter_bss, &new)) {
1532 found = iter_bss;
1533 break;
1534 }
1535
1536 if ((oldest == NULL) ||
1537 (iter_bss->last_scanned < oldest->last_scanned))
1538 oldest = iter_bss;
1539 }
1540
1541 if (found) {
1542 /* found, clear it */
1543 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001544 } else if (!list_empty(&priv->network_free_list)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001545 /* Pull one from the free list */
David Woodhouseaa21c002007-12-08 20:04:36 +00001546 found = list_entry(priv->network_free_list.next,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001547 struct bss_descriptor, list);
David Woodhouseaa21c002007-12-08 20:04:36 +00001548 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001549 } else if (oldest) {
1550 /* If there are no more slots, expire the oldest */
1551 found = oldest;
1552 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001553 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001554 } else {
1555 continue;
1556 }
1557
David Woodhousefa62f992008-03-03 12:18:03 +01001558 lbs_deb_scan("SCAN_RESP: BSSID %s\n", print_mac(mac, new.bssid));
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001559
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001560 /* Copy the locally created newbssentry to the scan table */
1561 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001562 }
1563
Holger Schurig9012b282007-05-25 11:27:16 -04001564 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001565
Holger Schurig9012b282007-05-25 11:27:16 -04001566done:
1567 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1568 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001569}