blob: 7e4b0ab108dca7d85a8d1b77d9086f3939449fce [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
16#include "host.h"
17#include "decl.h"
18#include "dev.h"
19#include "scan.h"
Dan Williams8c512762007-08-02 11:40:45 -040020#include "join.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020021
22//! Approximate amount of data needed to pass a scan result back to iwlist
23#define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
24 + IW_ESSID_MAX_SIZE \
25 + IW_EV_UINT_LEN \
26 + IW_EV_FREQ_LEN \
27 + IW_EV_QUAL_LEN \
28 + IW_ESSID_MAX_SIZE \
29 + IW_EV_PARAM_LEN \
30 + 40) /* 40 for WPAIE */
31
32//! Memory needed to store a max sized channel List TLV for a firmware scan
33#define CHAN_TLV_MAX_SIZE (sizeof(struct mrvlietypesheader) \
34 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
35 * sizeof(struct chanscanparamset)))
36
37//! Memory needed to store a max number/size SSID TLV for a firmware scan
38#define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvlietypes_ssidparamset))
39
40//! Maximum memory needed for a wlan_scan_cmd_config with all TLVs at max
41#define MAX_SCAN_CFG_ALLOC (sizeof(struct wlan_scan_cmd_config) \
42 + sizeof(struct mrvlietypes_numprobes) \
43 + CHAN_TLV_MAX_SIZE \
44 + SSID_TLV_MAX_SIZE)
45
46//! The maximum number of channels the firmware can scan per command
47#define MRVDRV_MAX_CHANNELS_PER_SCAN 14
48
49/**
50 * @brief Number of channels to scan per firmware scan command issuance.
51 *
52 * Number restricted to prevent hitting the limit on the amount of scan data
53 * returned in a single firmware scan command.
54 */
55#define MRVDRV_CHANNELS_PER_SCAN_CMD 4
56
57//! Scan time specified in the channel TLV for each channel for passive scans
58#define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
59
60//! Scan time specified in the channel TLV for each channel for active scans
61#define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
62
Dan Williams123e0e02007-05-25 23:23:43 -040063static const u8 zeromac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
64static const u8 bcastmac[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Dan Williamseb8f7332007-05-25 16:25:21 -040065
Dan Williamsfcdb53d2007-05-25 16:15:56 -040066static inline void clear_bss_descriptor (struct bss_descriptor * bss)
67{
68 /* Don't blow away ->list, just BSS data */
69 memset(bss, 0, offsetof(struct bss_descriptor, list));
70}
71
72static inline int match_bss_no_security(struct wlan_802_11_security * secinfo,
73 struct bss_descriptor * match_bss)
74{
75 if ( !secinfo->wep_enabled
76 && !secinfo->WPAenabled
77 && !secinfo->WPA2enabled
Dan Williamsab617972007-08-02 10:48:02 -040078 && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
79 && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
Dan Williams0c9ca6902007-08-02 10:43:44 -040080 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -040081 return 1;
82 }
83 return 0;
84}
85
86static inline int match_bss_static_wep(struct wlan_802_11_security * secinfo,
87 struct bss_descriptor * match_bss)
88{
89 if ( secinfo->wep_enabled
90 && !secinfo->WPAenabled
91 && !secinfo->WPA2enabled
Dan Williams0c9ca6902007-08-02 10:43:44 -040092 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -040093 return 1;
94 }
95 return 0;
96}
97
98static inline int match_bss_wpa(struct wlan_802_11_security * secinfo,
99 struct bss_descriptor * match_bss)
100{
101 if ( !secinfo->wep_enabled
102 && secinfo->WPAenabled
Dan Williamsab617972007-08-02 10:48:02 -0400103 && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400104 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
Dan Williams0c9ca6902007-08-02 10:43:44 -0400105 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
106 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400107 ) {
108 return 1;
109 }
110 return 0;
111}
112
113static inline int match_bss_wpa2(struct wlan_802_11_security * secinfo,
114 struct bss_descriptor * match_bss)
115{
116 if ( !secinfo->wep_enabled
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400117 && secinfo->WPA2enabled
Dan Williamsab617972007-08-02 10:48:02 -0400118 && (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400119 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
Dan Williams0c9ca6902007-08-02 10:43:44 -0400120 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
121 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400122 ) {
123 return 1;
124 }
125 return 0;
126}
127
128static inline int match_bss_dynamic_wep(struct wlan_802_11_security * secinfo,
129 struct bss_descriptor * match_bss)
130{
131 if ( !secinfo->wep_enabled
132 && !secinfo->WPAenabled
133 && !secinfo->WPA2enabled
Dan Williamsab617972007-08-02 10:48:02 -0400134 && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
135 && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
Dan Williams0c9ca6902007-08-02 10:43:44 -0400136 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400137 return 1;
138 }
139 return 0;
140}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200141
142/**
143 * @brief Check if a scanned network compatible with the driver settings
144 *
145 * WEP WPA WPA2 ad-hoc encrypt Network
146 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
147 * 0 0 0 0 NONE 0 0 0 yes No security
148 * 1 0 0 0 NONE 1 0 0 yes Static WEP
149 * 0 1 0 0 x 1x 1 x yes WPA
150 * 0 0 1 0 x 1x x 1 yes WPA2
151 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
152 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
153 *
154 *
155 * @param adapter A pointer to wlan_adapter
156 * @param index Index in scantable to check against current driver settings
157 * @param mode Network mode: Infrastructure or IBSS
158 *
159 * @return Index in scantable, or error code if negative
160 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400161static int is_network_compatible(wlan_adapter * adapter,
162 struct bss_descriptor * bss, u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200163{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400164 int matched = 0;
165
Holger Schurig9012b282007-05-25 11:27:16 -0400166 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200167
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400168 if (bss->mode != mode)
169 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200170
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400171 if ((matched = match_bss_no_security(&adapter->secinfo, bss))) {
172 goto done;
173 } else if ((matched = match_bss_static_wep(&adapter->secinfo, bss))) {
174 goto done;
175 } else if ((matched = match_bss_wpa(&adapter->secinfo, bss))) {
176 lbs_deb_scan(
177 "is_network_compatible() WPA: wpa_ie=%#x "
178 "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
179 "privacy=%#x\n", bss->wpa_ie[0], bss->rsn_ie[0],
Dan Williams889c05b2007-05-10 22:57:23 -0400180 adapter->secinfo.wep_enabled ? "e" : "d",
181 adapter->secinfo.WPAenabled ? "e" : "d",
182 adapter->secinfo.WPA2enabled ? "e" : "d",
Dan Williams0c9ca6902007-08-02 10:43:44 -0400183 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400184 goto done;
185 } else if ((matched = match_bss_wpa2(&adapter->secinfo, bss))) {
186 lbs_deb_scan(
187 "is_network_compatible() WPA2: wpa_ie=%#x "
188 "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
189 "privacy=%#x\n", bss->wpa_ie[0], bss->rsn_ie[0],
190 adapter->secinfo.wep_enabled ? "e" : "d",
191 adapter->secinfo.WPAenabled ? "e" : "d",
192 adapter->secinfo.WPA2enabled ? "e" : "d",
Dan Williams0c9ca6902007-08-02 10:43:44 -0400193 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400194 goto done;
195 } else if ((matched = match_bss_dynamic_wep(&adapter->secinfo, bss))) {
196 lbs_deb_scan(
197 "is_network_compatible() dynamic WEP: "
198 "wpa_ie=%#x wpa2_ie=%#x privacy=%#x\n",
Dan Williams0c9ca6902007-08-02 10:43:44 -0400199 bss->wpa_ie[0], bss->rsn_ie[0],
200 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Holger Schurig9012b282007-05-25 11:27:16 -0400201 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200202 }
203
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400204 /* bss security settings don't match those configured on card */
205 lbs_deb_scan(
206 "is_network_compatible() FAILED: wpa_ie=%#x "
207 "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s privacy=%#x\n",
208 bss->wpa_ie[0], bss->rsn_ie[0],
209 adapter->secinfo.wep_enabled ? "e" : "d",
210 adapter->secinfo.WPAenabled ? "e" : "d",
211 adapter->secinfo.WPA2enabled ? "e" : "d",
Dan Williams0c9ca6902007-08-02 10:43:44 -0400212 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Holger Schurig9012b282007-05-25 11:27:16 -0400213
214done:
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400215 lbs_deb_leave(LBS_DEB_SCAN);
216 return matched;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200217}
218
219/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200220 * @brief Create a channel list for the driver to scan based on region info
221 *
222 * Use the driver region/band information to construct a comprehensive list
223 * of channels to scan. This routine is used for any scan that is not
224 * provided a specific channel list to scan.
225 *
226 * @param priv A pointer to wlan_private structure
227 * @param scanchanlist Output parameter: resulting channel list to scan
228 * @param filteredscan Flag indicating whether or not a BSSID or SSID filter
229 * is being sent in the command to firmware. Used to
230 * increase the number of channels sent in a scan
231 * command and to disable the firmware channel scan
232 * filter.
233 *
234 * @return void
235 */
236static void wlan_scan_create_channel_list(wlan_private * priv,
237 struct chanscanparamset * scanchanlist,
238 u8 filteredscan)
239{
240
241 wlan_adapter *adapter = priv->adapter;
242 struct region_channel *scanregion;
243 struct chan_freq_power *cfp;
244 int rgnidx;
245 int chanidx;
246 int nextchan;
247 u8 scantype;
248
249 chanidx = 0;
250
251 /* Set the default scan type to the user specified type, will later
252 * be changed to passive on a per channel basis if restricted by
253 * regulatory requirements (11d or 11h)
254 */
255 scantype = adapter->scantype;
256
257 for (rgnidx = 0; rgnidx < ARRAY_SIZE(adapter->region_channel); rgnidx++) {
258 if (priv->adapter->enable11d &&
Dan Williams0aef64d2007-08-02 11:31:18 -0400259 adapter->connect_status != LIBERTAS_CONNECTED) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200260 /* Scan all the supported chan for the first scan */
261 if (!adapter->universal_channel[rgnidx].valid)
262 continue;
263 scanregion = &adapter->universal_channel[rgnidx];
264
265 /* clear the parsed_region_chan for the first scan */
266 memset(&adapter->parsed_region_chan, 0x00,
267 sizeof(adapter->parsed_region_chan));
268 } else {
269 if (!adapter->region_channel[rgnidx].valid)
270 continue;
271 scanregion = &adapter->region_channel[rgnidx];
272 }
273
274 for (nextchan = 0;
275 nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
276
277 cfp = scanregion->CFP + nextchan;
278
279 if (priv->adapter->enable11d) {
280 scantype =
281 libertas_get_scan_type_11d(cfp->channel,
282 &adapter->
283 parsed_region_chan);
284 }
285
286 switch (scanregion->band) {
287 case BAND_B:
288 case BAND_G:
289 default:
290 scanchanlist[chanidx].radiotype =
Dan Williams0aef64d2007-08-02 11:31:18 -0400291 CMD_SCAN_RADIO_TYPE_BG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200292 break;
293 }
294
Dan Williams0aef64d2007-08-02 11:31:18 -0400295 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200296 scanchanlist[chanidx].maxscantime =
David Woodhouse981f1872007-05-25 23:36:54 -0400297 cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200298 scanchanlist[chanidx].chanscanmode.passivescan =
299 1;
300 } else {
301 scanchanlist[chanidx].maxscantime =
David Woodhouse981f1872007-05-25 23:36:54 -0400302 cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200303 scanchanlist[chanidx].chanscanmode.passivescan =
304 0;
305 }
306
307 scanchanlist[chanidx].channumber = cfp->channel;
308
309 if (filteredscan) {
310 scanchanlist[chanidx].chanscanmode.
311 disablechanfilt = 1;
312 }
313 }
314 }
315}
316
317/**
318 * @brief Construct a wlan_scan_cmd_config structure to use in issue scan cmds
319 *
320 * Application layer or other functions can invoke wlan_scan_networks
321 * with a scan configuration supplied in a wlan_ioctl_user_scan_cfg struct.
322 * This structure is used as the basis of one or many wlan_scan_cmd_config
323 * commands that are sent to the command processing module and sent to
324 * firmware.
325 *
326 * Create a wlan_scan_cmd_config based on the following user supplied
327 * parameters (if present):
328 * - SSID filter
329 * - BSSID filter
330 * - Number of Probes to be sent
331 * - channel list
332 *
333 * If the SSID or BSSID filter is not present, disable/clear the filter.
334 * If the number of probes is not set, use the adapter default setting
335 * Qualify the channel
336 *
337 * @param priv A pointer to wlan_private structure
338 * @param puserscanin NULL or pointer to scan configuration parameters
339 * @param ppchantlvout Output parameter: Pointer to the start of the
340 * channel TLV portion of the output scan config
341 * @param pscanchanlist Output parameter: Pointer to the resulting channel
342 * list to scan
343 * @param pmaxchanperscan Output parameter: Number of channels to scan for
344 * each issuance of the firmware scan command
345 * @param pfilteredscan Output parameter: Flag indicating whether or not
346 * a BSSID or SSID filter is being sent in the
347 * command to firmware. Used to increase the number
348 * of channels sent in a scan command and to
349 * disable the firmware channel scan filter.
350 * @param pscancurrentonly Output parameter: Flag indicating whether or not
351 * we are only scanning our current active channel
352 *
353 * @return resulting scan configuration
354 */
355static struct wlan_scan_cmd_config *
356wlan_scan_setup_scan_config(wlan_private * priv,
357 const struct wlan_ioctl_user_scan_cfg * puserscanin,
358 struct mrvlietypes_chanlistparamset ** ppchantlvout,
359 struct chanscanparamset * pscanchanlist,
360 int *pmaxchanperscan,
361 u8 * pfilteredscan,
362 u8 * pscancurrentonly)
363{
364 wlan_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200365 struct mrvlietypes_numprobes *pnumprobestlv;
366 struct mrvlietypes_ssidparamset *pssidtlv;
367 struct wlan_scan_cmd_config * pscancfgout = NULL;
368 u8 *ptlvpos;
369 u16 numprobes;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200370 int chanidx;
371 int scantype;
372 int scandur;
373 int channel;
374 int radiotype;
375
376 pscancfgout = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
377 if (pscancfgout == NULL)
378 goto out;
379
380 /* The tlvbufferlen is calculated for each scan command. The TLVs added
381 * in this routine will be preserved since the routine that sends
382 * the command will append channelTLVs at *ppchantlvout. The difference
383 * between the *ppchantlvout and the tlvbuffer start will be used
384 * to calculate the size of anything we add in this routine.
385 */
386 pscancfgout->tlvbufferlen = 0;
387
388 /* Running tlv pointer. Assigned to ppchantlvout at end of function
389 * so later routines know where channels can be added to the command buf
390 */
391 ptlvpos = pscancfgout->tlvbuffer;
392
393 /*
394 * Set the initial scan paramters for progressive scanning. If a specific
395 * BSSID or SSID is used, the number of channels in the scan command
396 * will be increased to the absolute maximum
397 */
398 *pmaxchanperscan = MRVDRV_CHANNELS_PER_SCAN_CMD;
399
400 /* Initialize the scan as un-filtered by firmware, set to TRUE below if
401 * a SSID or BSSID filter is sent in the command
402 */
403 *pfilteredscan = 0;
404
405 /* Initialize the scan as not being only on the current channel. If
406 * the channel list is customized, only contains one channel, and
407 * is the active channel, this is set true and data flow is not halted.
408 */
409 *pscancurrentonly = 0;
410
411 if (puserscanin) {
412
413 /* Set the bss type scan filter, use adapter setting if unset */
414 pscancfgout->bsstype =
415 (puserscanin->bsstype ? puserscanin->bsstype : adapter->
416 scanmode);
417
418 /* Set the number of probes to send, use adapter setting if unset */
Holger Schurige2aa3342007-08-02 13:05:53 -0400419 numprobes = puserscanin->numprobes ? puserscanin->numprobes : 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200420
421 /*
422 * Set the BSSID filter to the incoming configuration,
423 * if non-zero. If not set, it will remain disabled (all zeros).
424 */
Dan Williamseb8f7332007-05-25 16:25:21 -0400425 memcpy(pscancfgout->bssid, puserscanin->bssid,
426 sizeof(pscancfgout->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200427
Dan Williamseb8f7332007-05-25 16:25:21 -0400428 if (puserscanin->ssid_len) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200429 pssidtlv =
430 (struct mrvlietypes_ssidparamset *) pscancfgout->
431 tlvbuffer;
432 pssidtlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
Dan Williamseb8f7332007-05-25 16:25:21 -0400433 pssidtlv->header.len = cpu_to_le16(puserscanin->ssid_len);
434 memcpy(pssidtlv->ssid, puserscanin->ssid,
435 puserscanin->ssid_len);
436 ptlvpos += sizeof(pssidtlv->header) + puserscanin->ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200437 }
438
439 /*
440 * The default number of channels sent in the command is low to
441 * ensure the response buffer from the firmware does not truncate
442 * scan results. That is not an issue with an SSID or BSSID
443 * filter applied to the scan results in the firmware.
444 */
Dan Williamseb8f7332007-05-25 16:25:21 -0400445 if ( puserscanin->ssid_len
446 || (compare_ether_addr(pscancfgout->bssid, &zeromac[0]) != 0)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200447 *pmaxchanperscan = MRVDRV_MAX_CHANNELS_PER_SCAN;
448 *pfilteredscan = 1;
449 }
450 } else {
451 pscancfgout->bsstype = adapter->scanmode;
Holger Schurige2aa3342007-08-02 13:05:53 -0400452 numprobes = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200453 }
454
455 /* If the input config or adapter has the number of Probes set, add tlv */
456 if (numprobes) {
457 pnumprobestlv = (struct mrvlietypes_numprobes *) ptlvpos;
David Woodhouse981f1872007-05-25 23:36:54 -0400458 pnumprobestlv->header.type = cpu_to_le16(TLV_TYPE_NUMPROBES);
459 pnumprobestlv->header.len = cpu_to_le16(2);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200460 pnumprobestlv->numprobes = cpu_to_le16(numprobes);
461
David Woodhouse981f1872007-05-25 23:36:54 -0400462 ptlvpos += sizeof(*pnumprobestlv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200463 }
464
465 /*
466 * Set the output for the channel TLV to the address in the tlv buffer
467 * past any TLVs that were added in this fuction (SSID, numprobes).
468 * channel TLVs will be added past this for each scan command, preserving
469 * the TLVs that were previously added.
470 */
471 *ppchantlvout = (struct mrvlietypes_chanlistparamset *) ptlvpos;
472
473 if (puserscanin && puserscanin->chanlist[0].channumber) {
474
Holger Schurig9012b282007-05-25 11:27:16 -0400475 lbs_deb_scan("Scan: Using supplied channel list\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200476
477 for (chanidx = 0;
478 chanidx < WLAN_IOCTL_USER_SCAN_CHAN_MAX
479 && puserscanin->chanlist[chanidx].channumber; chanidx++) {
480
481 channel = puserscanin->chanlist[chanidx].channumber;
482 (pscanchanlist + chanidx)->channumber = channel;
483
484 radiotype = puserscanin->chanlist[chanidx].radiotype;
485 (pscanchanlist + chanidx)->radiotype = radiotype;
486
487 scantype = puserscanin->chanlist[chanidx].scantype;
488
Dan Williams0aef64d2007-08-02 11:31:18 -0400489 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200490 (pscanchanlist +
491 chanidx)->chanscanmode.passivescan = 1;
492 } else {
493 (pscanchanlist +
494 chanidx)->chanscanmode.passivescan = 0;
495 }
496
497 if (puserscanin->chanlist[chanidx].scantime) {
498 scandur =
499 puserscanin->chanlist[chanidx].scantime;
500 } else {
Dan Williams0aef64d2007-08-02 11:31:18 -0400501 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200502 scandur = MRVDRV_PASSIVE_SCAN_CHAN_TIME;
503 } else {
504 scandur = MRVDRV_ACTIVE_SCAN_CHAN_TIME;
505 }
506 }
507
508 (pscanchanlist + chanidx)->minscantime =
509 cpu_to_le16(scandur);
510 (pscanchanlist + chanidx)->maxscantime =
511 cpu_to_le16(scandur);
512 }
513
514 /* Check if we are only scanning the current channel */
515 if ((chanidx == 1) && (puserscanin->chanlist[0].channumber
516 ==
517 priv->adapter->curbssparams.channel)) {
518 *pscancurrentonly = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400519 lbs_deb_scan("Scan: Scanning current channel only");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200520 }
521
522 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400523 lbs_deb_scan("Scan: Creating full region channel list\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200524 wlan_scan_create_channel_list(priv, pscanchanlist,
525 *pfilteredscan);
526 }
527
528out:
529 return pscancfgout;
530}
531
532/**
533 * @brief Construct and send multiple scan config commands to the firmware
534 *
535 * Previous routines have created a wlan_scan_cmd_config with any requested
536 * TLVs. This function splits the channel TLV into maxchanperscan lists
537 * and sends the portion of the channel TLV along with the other TLVs
538 * to the wlan_cmd routines for execution in the firmware.
539 *
540 * @param priv A pointer to wlan_private structure
541 * @param maxchanperscan Maximum number channels to be included in each
542 * scan command sent to firmware
543 * @param filteredscan Flag indicating whether or not a BSSID or SSID
544 * filter is being used for the firmware command
545 * scan command sent to firmware
546 * @param pscancfgout Scan configuration used for this scan.
547 * @param pchantlvout Pointer in the pscancfgout where the channel TLV
548 * should start. This is past any other TLVs that
549 * must be sent down in each firmware command.
550 * @param pscanchanlist List of channels to scan in maxchanperscan segments
551 *
552 * @return 0 or error return otherwise
553 */
554static int wlan_scan_channel_list(wlan_private * priv,
555 int maxchanperscan,
556 u8 filteredscan,
557 struct wlan_scan_cmd_config * pscancfgout,
558 struct mrvlietypes_chanlistparamset * pchantlvout,
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400559 struct chanscanparamset * pscanchanlist,
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400560 const struct wlan_ioctl_user_scan_cfg * puserscanin,
561 int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200562{
563 struct chanscanparamset *ptmpchan;
564 struct chanscanparamset *pstartchan;
565 u8 scanband;
566 int doneearly;
567 int tlvidx;
568 int ret = 0;
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400569 int scanned = 0;
570 union iwreq_data wrqu;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200571
Holger Schurig9012b282007-05-25 11:27:16 -0400572 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200573
574 if (pscancfgout == 0 || pchantlvout == 0 || pscanchanlist == 0) {
Holger Schurig9012b282007-05-25 11:27:16 -0400575 lbs_deb_scan("Scan: Null detect: %p, %p, %p\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200576 pscancfgout, pchantlvout, pscanchanlist);
577 return -1;
578 }
579
580 pchantlvout->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
581
582 /* Set the temp channel struct pointer to the start of the desired list */
583 ptmpchan = pscanchanlist;
584
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400585 if (priv->adapter->last_scanned_channel && !puserscanin)
586 ptmpchan += priv->adapter->last_scanned_channel;
587
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200588 /* Loop through the desired channel list, sending a new firmware scan
589 * commands for each maxchanperscan channels (or for 1,6,11 individually
590 * if configured accordingly)
591 */
592 while (ptmpchan->channumber) {
593
594 tlvidx = 0;
595 pchantlvout->header.len = 0;
596 scanband = ptmpchan->radiotype;
597 pstartchan = ptmpchan;
598 doneearly = 0;
599
600 /* Construct the channel TLV for the scan command. Continue to
601 * insert channel TLVs until:
602 * - the tlvidx hits the maximum configured per scan command
603 * - the next channel to insert is 0 (end of desired channel list)
604 * - doneearly is set (controlling individual scanning of 1,6,11)
605 */
606 while (tlvidx < maxchanperscan && ptmpchan->channumber
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400607 && !doneearly && scanned < 2) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200608
Holger Schurig9012b282007-05-25 11:27:16 -0400609 lbs_deb_scan(
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200610 "Scan: Chan(%3d), Radio(%d), mode(%d,%d), Dur(%d)\n",
611 ptmpchan->channumber, ptmpchan->radiotype,
612 ptmpchan->chanscanmode.passivescan,
613 ptmpchan->chanscanmode.disablechanfilt,
614 ptmpchan->maxscantime);
615
616 /* Copy the current channel TLV to the command being prepared */
617 memcpy(pchantlvout->chanscanparam + tlvidx,
618 ptmpchan, sizeof(pchantlvout->chanscanparam));
619
620 /* Increment the TLV header length by the size appended */
David Woodhouse981f1872007-05-25 23:36:54 -0400621 /* Ew, it would be _so_ nice if we could just declare the
622 variable little-endian and let GCC handle it for us */
623 pchantlvout->header.len =
624 cpu_to_le16(le16_to_cpu(pchantlvout->header.len) +
625 sizeof(pchantlvout->chanscanparam));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200626
627 /*
628 * The tlv buffer length is set to the number of bytes of the
629 * between the channel tlv pointer and the start of the
630 * tlv buffer. This compensates for any TLVs that were appended
631 * before the channel list.
632 */
633 pscancfgout->tlvbufferlen = ((u8 *) pchantlvout
634 - pscancfgout->tlvbuffer);
635
636 /* Add the size of the channel tlv header and the data length */
637 pscancfgout->tlvbufferlen +=
638 (sizeof(pchantlvout->header)
David Woodhouse981f1872007-05-25 23:36:54 -0400639 + le16_to_cpu(pchantlvout->header.len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200640
641 /* Increment the index to the channel tlv we are constructing */
642 tlvidx++;
643
644 doneearly = 0;
645
646 /* Stop the loop if the *current* channel is in the 1,6,11 set
647 * and we are not filtering on a BSSID or SSID.
648 */
649 if (!filteredscan && (ptmpchan->channumber == 1
650 || ptmpchan->channumber == 6
651 || ptmpchan->channumber == 11)) {
652 doneearly = 1;
653 }
654
655 /* Increment the tmp pointer to the next channel to be scanned */
656 ptmpchan++;
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400657 scanned++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200658
659 /* Stop the loop if the *next* channel is in the 1,6,11 set.
660 * This will cause it to be the only channel scanned on the next
661 * interation
662 */
663 if (!filteredscan && (ptmpchan->channumber == 1
664 || ptmpchan->channumber == 6
665 || ptmpchan->channumber == 11)) {
666 doneearly = 1;
667 }
668 }
669
670 /* Send the scan command to the firmware with the specified cfg */
Dan Williams0aef64d2007-08-02 11:31:18 -0400671 ret = libertas_prepare_and_send_command(priv, CMD_802_11_SCAN, 0,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200672 0, 0, pscancfgout);
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400673 if (scanned >= 2 && !full_scan) {
Holger Schurig9012b282007-05-25 11:27:16 -0400674 ret = 0;
675 goto done;
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400676 }
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400677 scanned = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200678 }
679
Dan Williamsd9ad2f52007-05-25 22:38:41 -0400680done:
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400681 priv->adapter->last_scanned_channel = ptmpchan->channumber;
682
Dan Williamsd9ad2f52007-05-25 22:38:41 -0400683 /* Tell userspace the scan table has been updated */
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400684 memset(&wrqu, 0, sizeof(union iwreq_data));
Holger Schurig634b8f42007-05-25 13:05:16 -0400685 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400686
Holger Schurig9012b282007-05-25 11:27:16 -0400687 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200688 return ret;
689}
690
Dan Williamseb8f7332007-05-25 16:25:21 -0400691static void
692clear_selected_scan_list_entries(wlan_adapter * adapter,
693 const struct wlan_ioctl_user_scan_cfg * scan_cfg)
694{
695 struct bss_descriptor * bss;
696 struct bss_descriptor * safe;
697 u32 clear_ssid_flag = 0, clear_bssid_flag = 0;
698
699 if (!scan_cfg)
700 return;
701
702 if (scan_cfg->clear_ssid && scan_cfg->ssid_len)
703 clear_ssid_flag = 1;
704
705 if (scan_cfg->clear_bssid
706 && (compare_ether_addr(scan_cfg->bssid, &zeromac[0]) != 0)
707 && (compare_ether_addr(scan_cfg->bssid, &bcastmac[0]) != 0)) {
708 clear_bssid_flag = 1;
709 }
710
711 if (!clear_ssid_flag && !clear_bssid_flag)
712 return;
713
714 mutex_lock(&adapter->lock);
715 list_for_each_entry_safe (bss, safe, &adapter->network_list, list) {
716 u32 clear = 0;
717
718 /* Check for an SSID match */
719 if ( clear_ssid_flag
Dan Williamsd8efea22007-05-28 23:54:55 -0400720 && (bss->ssid_len == scan_cfg->ssid_len)
721 && !memcmp(bss->ssid, scan_cfg->ssid, bss->ssid_len))
Dan Williamseb8f7332007-05-25 16:25:21 -0400722 clear = 1;
723
724 /* Check for a BSSID match */
725 if ( clear_bssid_flag
726 && !compare_ether_addr(bss->bssid, scan_cfg->bssid))
727 clear = 1;
728
729 if (clear) {
730 list_move_tail (&bss->list, &adapter->network_free_list);
731 clear_bss_descriptor(bss);
732 }
733 }
734 mutex_unlock(&adapter->lock);
735}
736
737
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200738/**
739 * @brief Internal function used to start a scan based on an input config
740 *
741 * Use the input user scan configuration information when provided in
742 * order to send the appropriate scan commands to firmware to populate or
743 * update the internal driver scan table
744 *
745 * @param priv A pointer to wlan_private structure
746 * @param puserscanin Pointer to the input configuration for the requested
747 * scan.
748 *
749 * @return 0 or < 0 if error
750 */
751int wlan_scan_networks(wlan_private * priv,
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400752 const struct wlan_ioctl_user_scan_cfg * puserscanin,
753 int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200754{
Dan Williamseb8f7332007-05-25 16:25:21 -0400755 wlan_adapter * adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200756 struct mrvlietypes_chanlistparamset *pchantlvout;
757 struct chanscanparamset * scan_chan_list = NULL;
758 struct wlan_scan_cmd_config * scan_cfg = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200759 u8 filteredscan;
760 u8 scancurrentchanonly;
761 int maxchanperscan;
762 int ret;
Dan Williamsf8f55102007-05-30 10:12:55 -0400763#ifdef CONFIG_LIBERTAS_DEBUG
764 struct bss_descriptor * iter_bss;
765 int i = 0;
766#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200767
Holger Schurig9012b282007-05-25 11:27:16 -0400768 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200769
770 scan_chan_list = kzalloc(sizeof(struct chanscanparamset) *
771 WLAN_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
772 if (scan_chan_list == NULL) {
773 ret = -ENOMEM;
774 goto out;
775 }
776
777 scan_cfg = wlan_scan_setup_scan_config(priv,
778 puserscanin,
779 &pchantlvout,
780 scan_chan_list,
781 &maxchanperscan,
782 &filteredscan,
783 &scancurrentchanonly);
784 if (scan_cfg == NULL) {
785 ret = -ENOMEM;
786 goto out;
787 }
788
Dan Williamseb8f7332007-05-25 16:25:21 -0400789 clear_selected_scan_list_entries(adapter, puserscanin);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200790
791 /* Keep the data path active if we are only scanning our current channel */
792 if (!scancurrentchanonly) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400793 netif_stop_queue(priv->dev);
794 netif_carrier_off(priv->dev);
Holger Schurig3cf84092007-08-02 11:50:12 -0400795 if (priv->mesh_dev) {
796 netif_stop_queue(priv->mesh_dev);
797 netif_carrier_off(priv->mesh_dev);
798 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200799 }
800
801 ret = wlan_scan_channel_list(priv,
802 maxchanperscan,
803 filteredscan,
804 scan_cfg,
805 pchantlvout,
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400806 scan_chan_list,
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400807 puserscanin,
808 full_scan);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200809
Dan Williamsf8f55102007-05-30 10:12:55 -0400810#ifdef CONFIG_LIBERTAS_DEBUG
811 /* Dump the scan table */
812 mutex_lock(&adapter->lock);
813 list_for_each_entry (iter_bss, &adapter->network_list, list) {
814 lbs_deb_scan("Scan:(%02d) " MAC_FMT ", RSSI[%03d], SSID[%s]\n",
815 i++, MAC_ARG(iter_bss->bssid), (s32) iter_bss->rssi,
816 escape_essid(iter_bss->ssid, iter_bss->ssid_len));
817 }
818 mutex_unlock(&adapter->lock);
819#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200820
Dan Williams0aef64d2007-08-02 11:31:18 -0400821 if (priv->adapter->connect_status == LIBERTAS_CONNECTED) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400822 netif_carrier_on(priv->dev);
823 netif_wake_queue(priv->dev);
Holger Schurig3cf84092007-08-02 11:50:12 -0400824 if (priv->mesh_dev) {
825 netif_carrier_on(priv->mesh_dev);
826 netif_wake_queue(priv->mesh_dev);
827 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200828 }
829
830out:
831 if (scan_cfg)
832 kfree(scan_cfg);
833
834 if (scan_chan_list)
835 kfree(scan_chan_list);
836
Holger Schurig9012b282007-05-25 11:27:16 -0400837 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200838 return ret;
839}
840
841/**
842 * @brief Inspect the scan response buffer for pointers to expected TLVs
843 *
844 * TLVs can be included at the end of the scan response BSS information.
845 * Parse the data in the buffer for pointers to TLVs that can potentially
846 * be passed back in the response
847 *
848 * @param ptlv Pointer to the start of the TLV buffer to parse
849 * @param tlvbufsize size of the TLV buffer
850 * @param ptsftlv Output parameter: Pointer to the TSF TLV if found
851 *
852 * @return void
853 */
854static
855void wlan_ret_802_11_scan_get_tlv_ptrs(struct mrvlietypes_data * ptlv,
856 int tlvbufsize,
857 struct mrvlietypes_tsftimestamp ** ptsftlv)
858{
859 struct mrvlietypes_data *pcurrenttlv;
860 int tlvbufleft;
861 u16 tlvtype;
862 u16 tlvlen;
863
864 pcurrenttlv = ptlv;
865 tlvbufleft = tlvbufsize;
866 *ptsftlv = NULL;
867
Holger Schurig9012b282007-05-25 11:27:16 -0400868 lbs_deb_scan("SCAN_RESP: tlvbufsize = %d\n", tlvbufsize);
Holger Schurigece56192007-08-02 11:53:06 -0400869 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_RESP: TLV Buf", (u8 *) ptlv, tlvbufsize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200870
871 while (tlvbufleft >= sizeof(struct mrvlietypesheader)) {
872 tlvtype = le16_to_cpu(pcurrenttlv->header.type);
873 tlvlen = le16_to_cpu(pcurrenttlv->header.len);
874
875 switch (tlvtype) {
876 case TLV_TYPE_TSFTIMESTAMP:
877 *ptsftlv = (struct mrvlietypes_tsftimestamp *) pcurrenttlv;
878 break;
879
880 default:
Holger Schurig9012b282007-05-25 11:27:16 -0400881 lbs_deb_scan("SCAN_RESP: Unhandled TLV = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200882 tlvtype);
883 /* Give up, this seems corrupted */
884 return;
885 } /* switch */
886
887 tlvbufleft -= (sizeof(ptlv->header) + tlvlen);
888 pcurrenttlv =
889 (struct mrvlietypes_data *) (pcurrenttlv->Data + tlvlen);
890 } /* while */
891}
892
893/**
894 * @brief Interpret a BSS scan response returned from the firmware
895 *
896 * Parse the various fixed fields and IEs passed back for a a BSS probe
897 * response or beacon from the scan command. Record information as needed
898 * in the scan table struct bss_descriptor for that entry.
899 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400900 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200901 *
902 * @return 0 or -1
903 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400904static int libertas_process_bss(struct bss_descriptor * bss,
905 u8 ** pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200906{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200907 struct ieeetypes_fhparamset *pFH;
908 struct ieeetypes_dsparamset *pDS;
909 struct ieeetypes_cfparamset *pCF;
910 struct ieeetypes_ibssparamset *pibss;
Dan Williams8c512762007-08-02 11:40:45 -0400911 struct ieeetypes_countryinfoset *pcountryinfo;
912 u8 *pos, *end, *p;
913 u8 n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
914 u16 beaconsize = 0;
Holger Schurig9012b282007-05-25 11:27:16 -0400915 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200916
Holger Schurig9012b282007-05-25 11:27:16 -0400917 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200918
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200919 if (*bytesleft >= sizeof(beaconsize)) {
920 /* Extract & convert beacon size from the command buffer */
David Woodhouse981f1872007-05-25 23:36:54 -0400921 beaconsize = le16_to_cpup((void *)*pbeaconinfo);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200922 *bytesleft -= sizeof(beaconsize);
923 *pbeaconinfo += sizeof(beaconsize);
924 }
925
926 if (beaconsize == 0 || beaconsize > *bytesleft) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200927 *pbeaconinfo += *bytesleft;
928 *bytesleft = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200929 return -1;
930 }
931
932 /* Initialize the current working beacon pointer for this BSS iteration */
Dan Williamsab617972007-08-02 10:48:02 -0400933 pos = *pbeaconinfo;
934 end = pos + beaconsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200935
936 /* Advance the return beacon pointer past the current beacon */
937 *pbeaconinfo += beaconsize;
938 *bytesleft -= beaconsize;
939
Dan Williamsab617972007-08-02 10:48:02 -0400940 memcpy(bss->bssid, pos, ETH_ALEN);
Dan Williams02eb2292007-05-25 17:27:31 -0400941 lbs_deb_scan("process_bss: AP BSSID " MAC_FMT "\n", MAC_ARG(bss->bssid));
Dan Williamsab617972007-08-02 10:48:02 -0400942 pos += ETH_ALEN;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200943
Dan Williamsab617972007-08-02 10:48:02 -0400944 if ((end - pos) < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400945 lbs_deb_scan("process_bss: Not enough bytes left\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200946 return -1;
947 }
948
949 /*
950 * next 4 fields are RSSI, time stamp, beacon interval,
951 * and capability information
952 */
953
954 /* RSSI is 1 byte long */
Dan Williamsab617972007-08-02 10:48:02 -0400955 bss->rssi = *pos;
956 lbs_deb_scan("process_bss: RSSI=%02X\n", *pos);
957 pos++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200958
959 /* time stamp is 8 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400960 bss->timestamp = le64_to_cpup((void *) pos);
961 pos += 8;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200962
963 /* beacon interval is 2 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400964 bss->beaconperiod = le16_to_cpup((void *) pos);
965 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200966
967 /* capability information is 2 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400968 bss->capability = le16_to_cpup((void *) pos);
Dan Williams0c9ca6902007-08-02 10:43:44 -0400969 lbs_deb_scan("process_bss: capabilities = 0x%4X\n", bss->capability);
Dan Williamsab617972007-08-02 10:48:02 -0400970 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200971
Dan Williams0c9ca6902007-08-02 10:43:44 -0400972 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
973 lbs_deb_scan("process_bss: AP WEP enabled\n");
974 if (bss->capability & WLAN_CAPABILITY_IBSS)
975 bss->mode = IW_MODE_ADHOC;
976 else
977 bss->mode = IW_MODE_INFRA;
978
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200979 /* rest of the current buffer are IE's */
Dan Williamsab617972007-08-02 10:48:02 -0400980 lbs_deb_scan("process_bss: IE length for this AP = %zd\n", end - pos);
Holger Schurigece56192007-08-02 11:53:06 -0400981 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200982
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200983 /* process variable IE */
Dan Williamsab617972007-08-02 10:48:02 -0400984 while (pos <= end - 2) {
985 struct ieee80211_info_element * elem =
986 (struct ieee80211_info_element *) pos;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200987
Dan Williamsab617972007-08-02 10:48:02 -0400988 if (pos + elem->len > end) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400989 lbs_deb_scan("process_bss: error in processing IE, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200990 "bytes left < IE length\n");
Dan Williamsab617972007-08-02 10:48:02 -0400991 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200992 }
993
Dan Williamsab617972007-08-02 10:48:02 -0400994 switch (elem->id) {
995 case MFIE_TYPE_SSID:
996 bss->ssid_len = elem->len;
997 memcpy(bss->ssid, elem->data, elem->len);
Dan Williamsd8efea22007-05-28 23:54:55 -0400998 lbs_deb_scan("ssid '%s', ssid length %u\n",
999 escape_essid(bss->ssid, bss->ssid_len),
1000 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001001 break;
1002
Dan Williamsab617972007-08-02 10:48:02 -04001003 case MFIE_TYPE_RATES:
Dan Williams8c512762007-08-02 11:40:45 -04001004 n_basic_rates = min_t(u8, MAX_RATES, elem->len);
1005 memcpy(bss->rates, elem->data, n_basic_rates);
1006 got_basic_rates = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001007 break;
1008
Dan Williamsab617972007-08-02 10:48:02 -04001009 case MFIE_TYPE_FH_SET:
1010 pFH = (struct ieeetypes_fhparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001011 memmove(&bss->phyparamset.fhparamset, pFH,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001012 sizeof(struct ieeetypes_fhparamset));
David Woodhouse981f1872007-05-25 23:36:54 -04001013#if 0 /* I think we can store these LE */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001014 bss->phyparamset.fhparamset.dwelltime
1015 = le16_to_cpu(bss->phyparamset.fhparamset.dwelltime);
David Woodhouse981f1872007-05-25 23:36:54 -04001016#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001017 break;
1018
Dan Williamsab617972007-08-02 10:48:02 -04001019 case MFIE_TYPE_DS_SET:
1020 pDS = (struct ieeetypes_dsparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001021 bss->channel = pDS->currentchan;
1022 memcpy(&bss->phyparamset.dsparamset, pDS,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001023 sizeof(struct ieeetypes_dsparamset));
1024 break;
1025
Dan Williamsab617972007-08-02 10:48:02 -04001026 case MFIE_TYPE_CF_SET:
1027 pCF = (struct ieeetypes_cfparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001028 memcpy(&bss->ssparamset.cfparamset, pCF,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001029 sizeof(struct ieeetypes_cfparamset));
1030 break;
1031
Dan Williamsab617972007-08-02 10:48:02 -04001032 case MFIE_TYPE_IBSS_SET:
1033 pibss = (struct ieeetypes_ibssparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001034 bss->atimwindow = le32_to_cpu(pibss->atimwindow);
1035 memmove(&bss->ssparamset.ibssparamset, pibss,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001036 sizeof(struct ieeetypes_ibssparamset));
David Woodhouse981f1872007-05-25 23:36:54 -04001037#if 0
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001038 bss->ssparamset.ibssparamset.atimwindow
1039 = le16_to_cpu(bss->ssparamset.ibssparamset.atimwindow);
David Woodhouse981f1872007-05-25 23:36:54 -04001040#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001041 break;
1042
Dan Williamsab617972007-08-02 10:48:02 -04001043 case MFIE_TYPE_COUNTRY:
1044 pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001045 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001046 || pcountryinfo->len > 254) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001047 lbs_deb_scan("process_bss: 11D- Err "
Dan Williams4269e2a2007-05-10 23:10:18 -04001048 "CountryInfo len =%d min=%zd max=254\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001049 pcountryinfo->len,
1050 sizeof(pcountryinfo->countrycode));
Holger Schurig9012b282007-05-25 11:27:16 -04001051 ret = -1;
1052 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001053 }
1054
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001055 memcpy(&bss->countryinfo,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001056 pcountryinfo, pcountryinfo->len + 2);
Holger Schurigece56192007-08-02 11:53:06 -04001057 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001058 (u8 *) pcountryinfo,
1059 (u32) (pcountryinfo->len + 2));
1060 break;
1061
Dan Williamsab617972007-08-02 10:48:02 -04001062 case MFIE_TYPE_RATES_EX:
1063 /* only process extended supported rate if data rate is
1064 * already found. Data rate IE should come before
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001065 * extended supported rate IE
1066 */
Dan Williams8c512762007-08-02 11:40:45 -04001067 if (!got_basic_rates)
Dan Williamsab617972007-08-02 10:48:02 -04001068 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001069
Dan Williams8c512762007-08-02 11:40:45 -04001070 n_ex_rates = elem->len;
1071 if (n_basic_rates + n_ex_rates > MAX_RATES)
1072 n_ex_rates = MAX_RATES - n_basic_rates;
Dan Williamsab617972007-08-02 10:48:02 -04001073
Dan Williams8c512762007-08-02 11:40:45 -04001074 p = bss->rates + n_basic_rates;
1075 memcpy(p, elem->data, n_ex_rates);
Dan Williamsab617972007-08-02 10:48:02 -04001076 break;
1077
1078 case MFIE_TYPE_GENERIC:
1079 if (elem->len >= 4 &&
1080 elem->data[0] == 0x00 &&
1081 elem->data[1] == 0x50 &&
1082 elem->data[2] == 0xf2 &&
1083 elem->data[3] == 0x01) {
1084 bss->wpa_ie_len = min(elem->len + 2,
1085 MAX_WPA_IE_LEN);
1086 memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
Holger Schurigece56192007-08-02 11:53:06 -04001087 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: WPA IE", bss->wpa_ie,
Dan Williamsab617972007-08-02 10:48:02 -04001088 elem->len);
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001089 } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
1090 elem->data[0] == 0x00 &&
1091 elem->data[1] == 0x50 &&
1092 elem->data[2] == 0x43 &&
1093 elem->data[3] == 0x04) {
1094 bss->mesh = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001095 }
1096 break;
1097
Dan Williamsab617972007-08-02 10:48:02 -04001098 case MFIE_TYPE_RSN:
1099 bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
1100 memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
Holger Schurigece56192007-08-02 11:53:06 -04001101 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE", bss->rsn_ie, elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001102 break;
1103
Dan Williamsab617972007-08-02 10:48:02 -04001104 default:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001105 break;
1106 }
1107
Dan Williamsab617972007-08-02 10:48:02 -04001108 pos += elem->len + 2;
1109 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001110
1111 /* Timestamp */
1112 bss->last_scanned = jiffies;
Dan Williams8c512762007-08-02 11:40:45 -04001113 libertas_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001114
Holger Schurig9012b282007-05-25 11:27:16 -04001115 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001116
Holger Schurig9012b282007-05-25 11:27:16 -04001117done:
1118 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1119 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001120}
1121
1122/**
1123 * @brief Compare two SSIDs
1124 *
1125 * @param ssid1 A pointer to ssid to compare
1126 * @param ssid2 A pointer to ssid to compare
1127 *
1128 * @return 0--ssid is same, otherwise is different
1129 */
Dan Williams717c9332007-05-29 00:03:31 -04001130int libertas_ssid_cmp(u8 *ssid1, u8 ssid1_len, u8 *ssid2, u8 ssid2_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001131{
Dan Williamsd8efea22007-05-28 23:54:55 -04001132 if (ssid1_len != ssid2_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001133 return -1;
1134
Dan Williamsd8efea22007-05-28 23:54:55 -04001135 return memcmp(ssid1, ssid2, ssid1_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001136}
1137
1138/**
1139 * @brief This function finds a specific compatible BSSID in the scan list
1140 *
1141 * @param adapter A pointer to wlan_adapter
1142 * @param bssid BSSID to find in the scan list
1143 * @param mode Network mode: Infrastructure or IBSS
1144 *
1145 * @return index in BSSID list, or error return code (< 0)
1146 */
Dan Williams717c9332007-05-29 00:03:31 -04001147struct bss_descriptor * libertas_find_bssid_in_list(wlan_adapter * adapter,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001148 u8 * bssid, u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001149{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001150 struct bss_descriptor * iter_bss;
1151 struct bss_descriptor * found_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001152
1153 if (!bssid)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001154 return NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001155
Holger Schurigece56192007-08-02 11:53:06 -04001156 lbs_deb_hex(LBS_DEB_SCAN, "looking for",
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001157 bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001158
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001159 /* Look through the scan table for a compatible match. The loop will
1160 * continue past a matched bssid that is not compatible in case there
1161 * is an AP with multiple SSIDs assigned to the same BSSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001162 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001163 mutex_lock(&adapter->lock);
1164 list_for_each_entry (iter_bss, &adapter->network_list, list) {
Dan Williams3cf209312007-05-25 17:28:30 -04001165 if (compare_ether_addr(iter_bss->bssid, bssid))
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001166 continue; /* bssid doesn't match */
1167 switch (mode) {
1168 case IW_MODE_INFRA:
1169 case IW_MODE_ADHOC:
1170 if (!is_network_compatible(adapter, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001171 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001172 found_bss = iter_bss;
1173 break;
1174 default:
1175 found_bss = iter_bss;
1176 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001177 }
1178 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001179 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001180
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001181 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001182}
1183
1184/**
1185 * @brief This function finds ssid in ssid list.
1186 *
1187 * @param adapter A pointer to wlan_adapter
1188 * @param ssid SSID to find in the list
1189 * @param bssid BSSID to qualify the SSID selection (if provided)
1190 * @param mode Network mode: Infrastructure or IBSS
1191 *
1192 * @return index in BSSID list
1193 */
Dan Williams717c9332007-05-29 00:03:31 -04001194struct bss_descriptor * libertas_find_ssid_in_list(wlan_adapter * adapter,
Dan Williamsd8efea22007-05-28 23:54:55 -04001195 u8 *ssid, u8 ssid_len, u8 * bssid, u8 mode,
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001196 int channel)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001197{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001198 u8 bestrssi = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001199 struct bss_descriptor * iter_bss = NULL;
1200 struct bss_descriptor * found_bss = NULL;
1201 struct bss_descriptor * tmp_oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001202
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001203 mutex_lock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001204
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001205 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1206 if ( !tmp_oldest
1207 || (iter_bss->last_scanned < tmp_oldest->last_scanned))
1208 tmp_oldest = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001209
Dan Williams717c9332007-05-29 00:03:31 -04001210 if (libertas_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001211 ssid, ssid_len) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001212 continue; /* ssid doesn't match */
Dan Williams3cf209312007-05-25 17:28:30 -04001213 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001214 continue; /* bssid doesn't match */
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001215 if ((channel > 0) && (iter_bss->channel != channel))
1216 continue; /* channel doesn't match */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001217
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001218 switch (mode) {
1219 case IW_MODE_INFRA:
1220 case IW_MODE_ADHOC:
1221 if (!is_network_compatible(adapter, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001222 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001223
1224 if (bssid) {
1225 /* Found requested BSSID */
1226 found_bss = iter_bss;
1227 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001228 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001229
1230 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1231 bestrssi = SCAN_RSSI(iter_bss->rssi);
1232 found_bss = iter_bss;
1233 }
1234 break;
1235 case IW_MODE_AUTO:
1236 default:
1237 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1238 bestrssi = SCAN_RSSI(iter_bss->rssi);
1239 found_bss = iter_bss;
1240 }
1241 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001242 }
1243 }
1244
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001245out:
1246 mutex_unlock(&adapter->lock);
1247 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001248}
1249
1250/**
1251 * @brief This function finds the best SSID in the Scan List
1252 *
1253 * Search the scan table for the best SSID that also matches the current
1254 * adapter network preference (infrastructure or adhoc)
1255 *
1256 * @param adapter A pointer to wlan_adapter
1257 *
1258 * @return index in BSSID list
1259 */
Holger Schurigac558ca2007-08-02 11:49:06 -04001260static struct bss_descriptor * libertas_find_best_ssid_in_list(wlan_adapter * adapter,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001261 u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001262{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001263 u8 bestrssi = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001264 struct bss_descriptor * iter_bss;
1265 struct bss_descriptor * best_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001266
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001267 mutex_lock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001268
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001269 list_for_each_entry (iter_bss, &adapter->network_list, list) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001270 switch (mode) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001271 case IW_MODE_INFRA:
1272 case IW_MODE_ADHOC:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001273 if (!is_network_compatible(adapter, iter_bss, mode))
1274 break;
1275 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1276 break;
1277 bestrssi = SCAN_RSSI(iter_bss->rssi);
1278 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001279 break;
Dan Williams0dc5a292007-05-10 22:58:02 -04001280 case IW_MODE_AUTO:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001281 default:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001282 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1283 break;
1284 bestrssi = SCAN_RSSI(iter_bss->rssi);
1285 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001286 break;
1287 }
1288 }
1289
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001290 mutex_unlock(&adapter->lock);
1291 return best_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001292}
1293
1294/**
1295 * @brief Find the AP with specific ssid in the scan list
1296 *
1297 * @param priv A pointer to wlan_private structure
1298 * @param pSSID A pointer to AP's ssid
1299 *
1300 * @return 0--success, otherwise--fail
1301 */
Dan Williams717c9332007-05-29 00:03:31 -04001302int libertas_find_best_network_ssid(wlan_private * priv,
Dan Williamsd8efea22007-05-28 23:54:55 -04001303 u8 *out_ssid, u8 *out_ssid_len, u8 preferred_mode, u8 *out_mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001304{
1305 wlan_adapter *adapter = priv->adapter;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001306 int ret = -1;
1307 struct bss_descriptor * found;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001308
Holger Schurig9012b282007-05-25 11:27:16 -04001309 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001310
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001311 wlan_scan_networks(priv, NULL, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001312 if (adapter->surpriseremoved)
1313 return -1;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001314
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001315 wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1316
Dan Williams717c9332007-05-29 00:03:31 -04001317 found = libertas_find_best_ssid_in_list(adapter, preferred_mode);
Dan Williamsd8efea22007-05-28 23:54:55 -04001318 if (found && (found->ssid_len > 0)) {
1319 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1320 *out_ssid_len = found->ssid_len;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001321 *out_mode = found->mode;
1322 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001323 }
1324
Holger Schurig9012b282007-05-25 11:27:16 -04001325 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001326 return ret;
1327}
1328
1329/**
1330 * @brief Scan Network
1331 *
1332 * @param dev A pointer to net_device structure
1333 * @param info A pointer to iw_request_info structure
1334 * @param vwrq A pointer to iw_param structure
1335 * @param extra A pointer to extra data buf
1336 *
1337 * @return 0 --success, otherwise fail
1338 */
1339int libertas_set_scan(struct net_device *dev, struct iw_request_info *info,
1340 struct iw_param *vwrq, char *extra)
1341{
1342 wlan_private *priv = dev->priv;
1343 wlan_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001344
Holger Schurig9012b282007-05-25 11:27:16 -04001345 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001346
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001347 wlan_scan_networks(priv, NULL, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001348
1349 if (adapter->surpriseremoved)
1350 return -1;
1351
Holger Schurig9012b282007-05-25 11:27:16 -04001352 lbs_deb_leave(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001353 return 0;
1354}
1355
1356/**
1357 * @brief Send a scan command for all available channels filtered on a spec
1358 *
1359 * @param priv A pointer to wlan_private structure
1360 * @param prequestedssid A pointer to AP's ssid
1361 * @param keeppreviousscan Flag used to save/clear scan table before scan
1362 *
1363 * @return 0-success, otherwise fail
1364 */
Dan Williams717c9332007-05-29 00:03:31 -04001365int libertas_send_specific_ssid_scan(wlan_private * priv,
Dan Williamsd8efea22007-05-28 23:54:55 -04001366 u8 *ssid, u8 ssid_len, u8 clear_ssid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001367{
1368 wlan_adapter *adapter = priv->adapter;
1369 struct wlan_ioctl_user_scan_cfg scancfg;
Dan Williamseb8f7332007-05-25 16:25:21 -04001370 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001371
Holger Schurig9012b282007-05-25 11:27:16 -04001372 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001373
Dan Williamsd8efea22007-05-28 23:54:55 -04001374 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -04001375 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001376
1377 memset(&scancfg, 0x00, sizeof(scancfg));
Dan Williamsd8efea22007-05-28 23:54:55 -04001378 memcpy(scancfg.ssid, ssid, ssid_len);
1379 scancfg.ssid_len = ssid_len;
Dan Williamseb8f7332007-05-25 16:25:21 -04001380 scancfg.clear_ssid = clear_ssid;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001381
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001382 wlan_scan_networks(priv, &scancfg, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001383 if (adapter->surpriseremoved)
1384 return -1;
1385 wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1386
Dan Williamseb8f7332007-05-25 16:25:21 -04001387out:
Holger Schurig9012b282007-05-25 11:27:16 -04001388 lbs_deb_leave(LBS_DEB_ASSOC);
Dan Williamseb8f7332007-05-25 16:25:21 -04001389 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001390}
1391
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001392static inline char *libertas_translate_scan(wlan_private *priv,
1393 char *start, char *stop,
1394 struct bss_descriptor *bss)
1395{
1396 wlan_adapter *adapter = priv->adapter;
1397 struct chan_freq_power *cfp;
1398 char *current_val; /* For rates */
1399 struct iw_event iwe; /* Temporary buffer */
1400 int j;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001401#define PERFECT_RSSI ((u8)50)
1402#define WORST_RSSI ((u8)0)
1403#define RSSI_DIFF ((u8)(PERFECT_RSSI - WORST_RSSI))
1404 u8 rssi;
1405
1406 cfp = libertas_find_cfp_by_band_and_channel(adapter, 0, bss->channel);
1407 if (!cfp) {
1408 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
1409 return NULL;
1410 }
1411
1412 /* First entry *MUST* be the AP BSSID */
1413 iwe.cmd = SIOCGIWAP;
1414 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1415 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
1416 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
1417
1418 /* SSID */
1419 iwe.cmd = SIOCGIWESSID;
1420 iwe.u.data.flags = 1;
Dan Williamsd8efea22007-05-28 23:54:55 -04001421 iwe.u.data.length = min((u32) bss->ssid_len, (u32) IW_ESSID_MAX_SIZE);
1422 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001423
1424 /* Mode */
1425 iwe.cmd = SIOCGIWMODE;
1426 iwe.u.mode = bss->mode;
1427 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
1428
1429 /* Frequency */
1430 iwe.cmd = SIOCGIWFREQ;
1431 iwe.u.freq.m = (long)cfp->freq * 100000;
1432 iwe.u.freq.e = 1;
1433 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
1434
1435 /* Add quality statistics */
1436 iwe.cmd = IWEVQUAL;
1437 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
1438 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
1439
1440 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
1441 iwe.u.qual.qual =
1442 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
1443 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
1444 (RSSI_DIFF * RSSI_DIFF);
1445 if (iwe.u.qual.qual > 100)
1446 iwe.u.qual.qual = 100;
1447
1448 if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
1449 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
1450 } else {
1451 iwe.u.qual.noise =
1452 CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
1453 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001454
Dan Williams80e78ef2007-05-25 22:18:47 -04001455 /* Locally created ad-hoc BSSs won't have beacons if this is the
1456 * only station in the adhoc network; so get signal strength
1457 * from receive statistics.
1458 */
1459 if ((adapter->mode == IW_MODE_ADHOC)
1460 && adapter->adhoccreate
Dan Williams717c9332007-05-29 00:03:31 -04001461 && !libertas_ssid_cmp(adapter->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001462 adapter->curbssparams.ssid_len,
1463 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -04001464 int snr, nf;
1465 snr = adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1466 nf = adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1467 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001468 }
1469 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
1470
1471 /* Add encryption capability */
1472 iwe.cmd = SIOCGIWENCODE;
Dan Williams0c9ca6902007-08-02 10:43:44 -04001473 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001474 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1475 } else {
1476 iwe.u.data.flags = IW_ENCODE_DISABLED;
1477 }
1478 iwe.u.data.length = 0;
Dan Williamsd8efea22007-05-28 23:54:55 -04001479 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001480
1481 current_val = start + IW_EV_LCP_LEN;
1482
1483 iwe.cmd = SIOCGIWRATE;
1484 iwe.u.bitrate.fixed = 0;
1485 iwe.u.bitrate.disabled = 0;
1486 iwe.u.bitrate.value = 0;
1487
Dan Williams8c512762007-08-02 11:40:45 -04001488 for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
1489 /* Bit rate given in 500 kb/s units */
1490 iwe.u.bitrate.value = bss->rates[j] * 500000;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001491 current_val = iwe_stream_add_value(start, current_val,
1492 stop, &iwe, IW_EV_PARAM_LEN);
1493 }
1494 if ((bss->mode == IW_MODE_ADHOC)
Dan Williams717c9332007-05-29 00:03:31 -04001495 && !libertas_ssid_cmp(adapter->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001496 adapter->curbssparams.ssid_len,
1497 bss->ssid, bss->ssid_len)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001498 && adapter->adhoccreate) {
1499 iwe.u.bitrate.value = 22 * 500000;
1500 current_val = iwe_stream_add_value(start, current_val,
1501 stop, &iwe, IW_EV_PARAM_LEN);
1502 }
1503 /* Check if we added any event */
1504 if((current_val - start) > IW_EV_LCP_LEN)
1505 start = current_val;
1506
1507 memset(&iwe, 0, sizeof(iwe));
1508 if (bss->wpa_ie_len) {
1509 char buf[MAX_WPA_IE_LEN];
1510 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
1511 iwe.cmd = IWEVGENIE;
1512 iwe.u.data.length = bss->wpa_ie_len;
1513 start = iwe_stream_add_point(start, stop, &iwe, buf);
1514 }
1515
1516 memset(&iwe, 0, sizeof(iwe));
1517 if (bss->rsn_ie_len) {
1518 char buf[MAX_WPA_IE_LEN];
1519 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
1520 iwe.cmd = IWEVGENIE;
1521 iwe.u.data.length = bss->rsn_ie_len;
1522 start = iwe_stream_add_point(start, stop, &iwe, buf);
1523 }
1524
1525 return start;
1526}
1527
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001528/**
1529 * @brief Retrieve the scan table entries via wireless tools IOCTL call
1530 *
1531 * @param dev A pointer to net_device structure
1532 * @param info A pointer to iw_request_info structure
1533 * @param dwrq A pointer to iw_point structure
1534 * @param extra A pointer to extra data buf
1535 *
1536 * @return 0 --success, otherwise fail
1537 */
1538int libertas_get_scan(struct net_device *dev, struct iw_request_info *info,
1539 struct iw_point *dwrq, char *extra)
1540{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001541#define SCAN_ITEM_SIZE 128
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001542 wlan_private *priv = dev->priv;
1543 wlan_adapter *adapter = priv->adapter;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001544 int err = 0;
1545 char *ev = extra;
1546 char *stop = ev + dwrq->length;
1547 struct bss_descriptor * iter_bss;
1548 struct bss_descriptor * safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001549
Holger Schurig9012b282007-05-25 11:27:16 -04001550 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001551
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001552 /* If we've got an uncompleted scan, schedule the next part */
1553 if (!adapter->nr_cmd_pending && adapter->last_scanned_channel)
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001554 wlan_scan_networks(priv, NULL, 0);
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001555
Dan Williams80e78ef2007-05-25 22:18:47 -04001556 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001557 if ((adapter->mode == IW_MODE_ADHOC) && adapter->adhoccreate) {
Dan Williams0aef64d2007-08-02 11:31:18 -04001558 libertas_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
1559 CMD_OPTION_WAITFORRSP, 0, NULL);
Dan Williams80e78ef2007-05-25 22:18:47 -04001560 }
1561
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001562 mutex_lock(&adapter->lock);
1563 list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1564 char * next_ev;
1565 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001566
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001567 if (stop - ev < SCAN_ITEM_SIZE) {
1568 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001569 break;
1570 }
1571
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001572 /* For mesh device, list only mesh networks */
1573 if (dev == priv->mesh_dev && !iter_bss->mesh)
1574 continue;
1575
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001576 /* Prune old an old scan result */
1577 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1578 if (time_after(jiffies, stale_time)) {
1579 list_move_tail (&iter_bss->list,
1580 &adapter->network_free_list);
1581 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001582 continue;
1583 }
1584
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001585 /* Translate to WE format this entry */
1586 next_ev = libertas_translate_scan(priv, ev, stop, iter_bss);
1587 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001588 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001589 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001590 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001591 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001592
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001593 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001594 dwrq->flags = 0;
1595
Holger Schurig9012b282007-05-25 11:27:16 -04001596 lbs_deb_leave(LBS_DEB_ASSOC);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001597 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001598}
1599
1600/**
1601 * @brief Prepare a scan command to be sent to the firmware
1602 *
1603 * Use the wlan_scan_cmd_config sent to the command processing module in
1604 * the libertas_prepare_and_send_command to configure a cmd_ds_802_11_scan command
1605 * struct to send to firmware.
1606 *
1607 * The fixed fields specifying the BSS type and BSSID filters as well as a
1608 * variable number/length of TLVs are sent in the command to firmware.
1609 *
1610 * @param priv A pointer to wlan_private structure
1611 * @param cmd A pointer to cmd_ds_command structure to be sent to
1612 * firmware with the cmd_DS_801_11_SCAN structure
1613 * @param pdata_buf Void pointer cast of a wlan_scan_cmd_config struct used
1614 * to set the fields/TLVs for the command sent to firmware
1615 *
1616 * @return 0 or -1
1617 *
1618 * @sa wlan_scan_create_channel_list
1619 */
1620int libertas_cmd_80211_scan(wlan_private * priv,
1621 struct cmd_ds_command *cmd, void *pdata_buf)
1622{
1623 struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
1624 struct wlan_scan_cmd_config *pscancfg;
1625
Holger Schurig9012b282007-05-25 11:27:16 -04001626 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001627
1628 pscancfg = pdata_buf;
1629
1630 /* Set fixed field variables in scan command */
1631 pscan->bsstype = pscancfg->bsstype;
Dan Williams492b6da2007-08-02 11:16:07 -04001632 memcpy(pscan->bssid, pscancfg->bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001633 memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
1634
Dan Williams0aef64d2007-08-02 11:31:18 -04001635 cmd->command = cpu_to_le16(CMD_802_11_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001636
1637 /* size is equal to the sizeof(fixed portions) + the TLV len + header */
Dan Williams492b6da2007-08-02 11:16:07 -04001638 cmd->size = cpu_to_le16(sizeof(pscan->bsstype) + ETH_ALEN
1639 + pscancfg->tlvbufferlen + S_DS_GEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001640
Holger Schurig9012b282007-05-25 11:27:16 -04001641 lbs_deb_scan("SCAN_CMD: command=%x, size=%x, seqnum=%x\n",
David Woodhouse981f1872007-05-25 23:36:54 -04001642 le16_to_cpu(cmd->command), le16_to_cpu(cmd->size),
1643 le16_to_cpu(cmd->seqnum));
Holger Schurig9012b282007-05-25 11:27:16 -04001644
1645 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001646 return 0;
1647}
1648
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001649static inline int is_same_network(struct bss_descriptor *src,
1650 struct bss_descriptor *dst)
1651{
1652 /* A network is only a duplicate if the channel, BSSID, and ESSID
1653 * all match. We treat all <hidden> with the same BSSID and channel
1654 * as one network */
Dan Williamsd8efea22007-05-28 23:54:55 -04001655 return ((src->ssid_len == dst->ssid_len) &&
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001656 (src->channel == dst->channel) &&
1657 !compare_ether_addr(src->bssid, dst->bssid) &&
Dan Williamsd8efea22007-05-28 23:54:55 -04001658 !memcmp(src->ssid, dst->ssid, src->ssid_len));
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001659}
1660
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001661/**
1662 * @brief This function handles the command response of scan
1663 *
1664 * The response buffer for the scan command has the following
1665 * memory layout:
1666 *
1667 * .-----------------------------------------------------------.
1668 * | header (4 * sizeof(u16)): Standard command response hdr |
1669 * .-----------------------------------------------------------.
1670 * | bufsize (u16) : sizeof the BSS Description data |
1671 * .-----------------------------------------------------------.
1672 * | NumOfSet (u8) : Number of BSS Descs returned |
1673 * .-----------------------------------------------------------.
1674 * | BSSDescription data (variable, size given in bufsize) |
1675 * .-----------------------------------------------------------.
1676 * | TLV data (variable, size calculated using header->size, |
1677 * | bufsize and sizeof the fixed fields above) |
1678 * .-----------------------------------------------------------.
1679 *
1680 * @param priv A pointer to wlan_private structure
1681 * @param resp A pointer to cmd_ds_command
1682 *
1683 * @return 0 or -1
1684 */
1685int libertas_ret_80211_scan(wlan_private * priv, struct cmd_ds_command *resp)
1686{
1687 wlan_adapter *adapter = priv->adapter;
1688 struct cmd_ds_802_11_scan_rsp *pscan;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001689 struct mrvlietypes_data *ptlv;
1690 struct mrvlietypes_tsftimestamp *ptsftlv;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001691 struct bss_descriptor * iter_bss;
1692 struct bss_descriptor * safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001693 u8 *pbssinfo;
1694 u16 scanrespsize;
1695 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001696 int idx;
1697 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001698 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001699
Holger Schurig9012b282007-05-25 11:27:16 -04001700 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001701
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001702 /* Prune old entries from scan table */
1703 list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1704 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1705 if (time_before(jiffies, stale_time))
1706 continue;
1707 list_move_tail (&iter_bss->list, &adapter->network_free_list);
1708 clear_bss_descriptor(iter_bss);
1709 }
1710
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001711 pscan = &resp->params.scanresp;
1712
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001713 if (pscan->nr_sets > MAX_NETWORK_COUNT) {
1714 lbs_deb_scan(
1715 "SCAN_RESP: too many scan results (%d, max %d)!!\n",
1716 pscan->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001717 ret = -1;
1718 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001719 }
1720
1721 bytesleft = le16_to_cpu(pscan->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001722 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001723
1724 scanrespsize = le16_to_cpu(resp->size);
Holger Schurig9012b282007-05-25 11:27:16 -04001725 lbs_deb_scan("SCAN_RESP: returned %d AP before parsing\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001726 pscan->nr_sets);
1727
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001728 pbssinfo = pscan->bssdesc_and_tlvbuffer;
1729
1730 /* The size of the TLV buffer is equal to the entire command response
1731 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1732 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1733 * response header (S_DS_GEN)
1734 */
1735 tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
1736 + sizeof(pscan->nr_sets)
1737 + S_DS_GEN);
1738
1739 ptlv = (struct mrvlietypes_data *) (pscan->bssdesc_and_tlvbuffer + bytesleft);
1740
1741 /* Search the TLV buffer space in the scan response for any valid TLVs */
1742 wlan_ret_802_11_scan_get_tlv_ptrs(ptlv, tlvbufsize, &ptsftlv);
1743
1744 /*
1745 * Process each scan response returned (pscan->nr_sets). Save
1746 * the information in the newbssentry and then insert into the
1747 * driver scan table either as an update to an existing entry
1748 * or as an addition at the end of the table
1749 */
1750 for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001751 struct bss_descriptor new;
1752 struct bss_descriptor * found = NULL;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001753 struct bss_descriptor * oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001754
1755 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001756 memset(&new, 0, sizeof (struct bss_descriptor));
1757 if (libertas_process_bss(&new, &pbssinfo, &bytesleft) != 0) {
1758 /* error parsing the scan response, skipped */
1759 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1760 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001761 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001762
1763 /* Try to find this bss in the scan table */
1764 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1765 if (is_same_network(iter_bss, &new)) {
1766 found = iter_bss;
1767 break;
1768 }
1769
1770 if ((oldest == NULL) ||
1771 (iter_bss->last_scanned < oldest->last_scanned))
1772 oldest = iter_bss;
1773 }
1774
1775 if (found) {
1776 /* found, clear it */
1777 clear_bss_descriptor(found);
1778 } else if (!list_empty(&adapter->network_free_list)) {
1779 /* Pull one from the free list */
1780 found = list_entry(adapter->network_free_list.next,
1781 struct bss_descriptor, list);
1782 list_move_tail(&found->list, &adapter->network_list);
1783 } else if (oldest) {
1784 /* If there are no more slots, expire the oldest */
1785 found = oldest;
1786 clear_bss_descriptor(found);
1787 list_move_tail(&found->list, &adapter->network_list);
1788 } else {
1789 continue;
1790 }
1791
1792 lbs_deb_scan("SCAN_RESP: BSSID = " MAC_FMT "\n",
1793 new.bssid[0], new.bssid[1], new.bssid[2],
1794 new.bssid[3], new.bssid[4], new.bssid[5]);
1795
1796 /*
1797 * If the TSF TLV was appended to the scan results, save the
1798 * this entries TSF value in the networktsf field. The
1799 * networktsf is the firmware's TSF value at the time the
1800 * beacon or probe response was received.
1801 */
1802 if (ptsftlv) {
David Woodhouse981f1872007-05-25 23:36:54 -04001803 new.networktsf = le64_to_cpup(&ptsftlv->tsftable[idx]);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001804 }
1805
1806 /* Copy the locally created newbssentry to the scan table */
1807 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001808 }
1809
Holger Schurig9012b282007-05-25 11:27:16 -04001810 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001811
Holger Schurig9012b282007-05-25 11:27:16 -04001812done:
1813 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1814 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001815}