blob: dcce354e904c68848b9db78bc9b341bc22315639 [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"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020023
24//! Approximate amount of data needed to pass a scan result back to iwlist
25#define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
26 + IW_ESSID_MAX_SIZE \
27 + IW_EV_UINT_LEN \
28 + IW_EV_FREQ_LEN \
29 + IW_EV_QUAL_LEN \
30 + IW_ESSID_MAX_SIZE \
31 + IW_EV_PARAM_LEN \
32 + 40) /* 40 for WPAIE */
33
34//! Memory needed to store a max sized channel List TLV for a firmware scan
35#define CHAN_TLV_MAX_SIZE (sizeof(struct mrvlietypesheader) \
36 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
37 * sizeof(struct chanscanparamset)))
38
39//! Memory needed to store a max number/size SSID TLV for a firmware scan
40#define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvlietypes_ssidparamset))
41
42//! Maximum memory needed for a wlan_scan_cmd_config with all TLVs at max
43#define MAX_SCAN_CFG_ALLOC (sizeof(struct wlan_scan_cmd_config) \
44 + sizeof(struct mrvlietypes_numprobes) \
45 + CHAN_TLV_MAX_SIZE \
46 + SSID_TLV_MAX_SIZE)
47
48//! The maximum number of channels the firmware can scan per command
49#define MRVDRV_MAX_CHANNELS_PER_SCAN 14
50
51/**
52 * @brief Number of channels to scan per firmware scan command issuance.
53 *
54 * Number restricted to prevent hitting the limit on the amount of scan data
55 * returned in a single firmware scan command.
56 */
57#define MRVDRV_CHANNELS_PER_SCAN_CMD 4
58
59//! Scan time specified in the channel TLV for each channel for passive scans
60#define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
61
62//! Scan time specified in the channel TLV for each channel for active scans
63#define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
64
Dan Williams123e0e02007-05-25 23:23:43 -040065static const u8 zeromac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
66static const u8 bcastmac[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Dan Williamseb8f7332007-05-25 16:25:21 -040067
Dan Williamsfcdb53d2007-05-25 16:15:56 -040068static inline void clear_bss_descriptor (struct bss_descriptor * bss)
69{
70 /* Don't blow away ->list, just BSS data */
71 memset(bss, 0, offsetof(struct bss_descriptor, list));
72}
73
74static inline int match_bss_no_security(struct wlan_802_11_security * secinfo,
75 struct bss_descriptor * match_bss)
76{
77 if ( !secinfo->wep_enabled
78 && !secinfo->WPAenabled
79 && !secinfo->WPA2enabled
Dan Williamsab617972007-08-02 10:48:02 -040080 && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
81 && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
Dan Williams0c9ca6902007-08-02 10:43:44 -040082 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -040083 return 1;
84 }
85 return 0;
86}
87
88static inline int match_bss_static_wep(struct wlan_802_11_security * secinfo,
89 struct bss_descriptor * match_bss)
90{
91 if ( secinfo->wep_enabled
92 && !secinfo->WPAenabled
93 && !secinfo->WPA2enabled
Dan Williams0c9ca6902007-08-02 10:43:44 -040094 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -040095 return 1;
96 }
97 return 0;
98}
99
100static inline int match_bss_wpa(struct wlan_802_11_security * secinfo,
101 struct bss_descriptor * match_bss)
102{
103 if ( !secinfo->wep_enabled
104 && secinfo->WPAenabled
Dan Williamsab617972007-08-02 10:48:02 -0400105 && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400106 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
Dan Williams0c9ca6902007-08-02 10:43:44 -0400107 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
108 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400109 ) {
110 return 1;
111 }
112 return 0;
113}
114
115static inline int match_bss_wpa2(struct wlan_802_11_security * secinfo,
116 struct bss_descriptor * match_bss)
117{
118 if ( !secinfo->wep_enabled
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400119 && secinfo->WPA2enabled
Dan Williamsab617972007-08-02 10:48:02 -0400120 && (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400121 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
Dan Williams0c9ca6902007-08-02 10:43:44 -0400122 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
123 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400124 ) {
125 return 1;
126 }
127 return 0;
128}
129
130static inline int match_bss_dynamic_wep(struct wlan_802_11_security * secinfo,
131 struct bss_descriptor * match_bss)
132{
133 if ( !secinfo->wep_enabled
134 && !secinfo->WPAenabled
135 && !secinfo->WPA2enabled
Dan Williamsab617972007-08-02 10:48:02 -0400136 && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
137 && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
Dan Williams0c9ca6902007-08-02 10:43:44 -0400138 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400139 return 1;
140 }
141 return 0;
142}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200143
144/**
145 * @brief Check if a scanned network compatible with the driver settings
146 *
147 * WEP WPA WPA2 ad-hoc encrypt Network
148 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
149 * 0 0 0 0 NONE 0 0 0 yes No security
150 * 1 0 0 0 NONE 1 0 0 yes Static WEP
151 * 0 1 0 0 x 1x 1 x yes WPA
152 * 0 0 1 0 x 1x x 1 yes WPA2
153 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
154 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
155 *
156 *
157 * @param adapter A pointer to wlan_adapter
158 * @param index Index in scantable to check against current driver settings
159 * @param mode Network mode: Infrastructure or IBSS
160 *
161 * @return Index in scantable, or error code if negative
162 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400163static int is_network_compatible(wlan_adapter * adapter,
164 struct bss_descriptor * bss, u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200165{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400166 int matched = 0;
167
Holger Schurig9012b282007-05-25 11:27:16 -0400168 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200169
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400170 if (bss->mode != mode)
171 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200172
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400173 if ((matched = match_bss_no_security(&adapter->secinfo, bss))) {
174 goto done;
175 } else if ((matched = match_bss_static_wep(&adapter->secinfo, bss))) {
176 goto done;
177 } else if ((matched = match_bss_wpa(&adapter->secinfo, bss))) {
178 lbs_deb_scan(
179 "is_network_compatible() WPA: wpa_ie=%#x "
180 "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
181 "privacy=%#x\n", bss->wpa_ie[0], bss->rsn_ie[0],
Dan Williams889c05b2007-05-10 22:57:23 -0400182 adapter->secinfo.wep_enabled ? "e" : "d",
183 adapter->secinfo.WPAenabled ? "e" : "d",
184 adapter->secinfo.WPA2enabled ? "e" : "d",
Dan Williams0c9ca6902007-08-02 10:43:44 -0400185 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400186 goto done;
187 } else if ((matched = match_bss_wpa2(&adapter->secinfo, bss))) {
188 lbs_deb_scan(
189 "is_network_compatible() WPA2: wpa_ie=%#x "
190 "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
191 "privacy=%#x\n", bss->wpa_ie[0], bss->rsn_ie[0],
192 adapter->secinfo.wep_enabled ? "e" : "d",
193 adapter->secinfo.WPAenabled ? "e" : "d",
194 adapter->secinfo.WPA2enabled ? "e" : "d",
Dan Williams0c9ca6902007-08-02 10:43:44 -0400195 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400196 goto done;
197 } else if ((matched = match_bss_dynamic_wep(&adapter->secinfo, bss))) {
198 lbs_deb_scan(
199 "is_network_compatible() dynamic WEP: "
200 "wpa_ie=%#x wpa2_ie=%#x privacy=%#x\n",
Dan Williams0c9ca6902007-08-02 10:43:44 -0400201 bss->wpa_ie[0], bss->rsn_ie[0],
202 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Holger Schurig9012b282007-05-25 11:27:16 -0400203 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200204 }
205
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400206 /* bss security settings don't match those configured on card */
207 lbs_deb_scan(
208 "is_network_compatible() FAILED: wpa_ie=%#x "
209 "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s privacy=%#x\n",
210 bss->wpa_ie[0], bss->rsn_ie[0],
211 adapter->secinfo.wep_enabled ? "e" : "d",
212 adapter->secinfo.WPAenabled ? "e" : "d",
213 adapter->secinfo.WPA2enabled ? "e" : "d",
Dan Williams0c9ca6902007-08-02 10:43:44 -0400214 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Holger Schurig9012b282007-05-25 11:27:16 -0400215
216done:
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400217 lbs_deb_leave(LBS_DEB_SCAN);
218 return matched;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200219}
220
221/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200222 * @brief Create a channel list for the driver to scan based on region info
223 *
224 * Use the driver region/band information to construct a comprehensive list
225 * of channels to scan. This routine is used for any scan that is not
226 * provided a specific channel list to scan.
227 *
228 * @param priv A pointer to wlan_private structure
229 * @param scanchanlist Output parameter: resulting channel list to scan
230 * @param filteredscan Flag indicating whether or not a BSSID or SSID filter
231 * is being sent in the command to firmware. Used to
232 * increase the number of channels sent in a scan
233 * command and to disable the firmware channel scan
234 * filter.
235 *
236 * @return void
237 */
238static void wlan_scan_create_channel_list(wlan_private * priv,
239 struct chanscanparamset * scanchanlist,
240 u8 filteredscan)
241{
242
243 wlan_adapter *adapter = priv->adapter;
244 struct region_channel *scanregion;
245 struct chan_freq_power *cfp;
246 int rgnidx;
247 int chanidx;
248 int nextchan;
249 u8 scantype;
250
251 chanidx = 0;
252
253 /* Set the default scan type to the user specified type, will later
254 * be changed to passive on a per channel basis if restricted by
255 * regulatory requirements (11d or 11h)
256 */
Holger Schurig4f2fdaa2007-08-02 13:12:27 -0400257 scantype = CMD_SCAN_TYPE_ACTIVE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200258
259 for (rgnidx = 0; rgnidx < ARRAY_SIZE(adapter->region_channel); rgnidx++) {
260 if (priv->adapter->enable11d &&
Dan Williams0aef64d2007-08-02 11:31:18 -0400261 adapter->connect_status != LIBERTAS_CONNECTED) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200262 /* Scan all the supported chan for the first scan */
263 if (!adapter->universal_channel[rgnidx].valid)
264 continue;
265 scanregion = &adapter->universal_channel[rgnidx];
266
267 /* clear the parsed_region_chan for the first scan */
268 memset(&adapter->parsed_region_chan, 0x00,
269 sizeof(adapter->parsed_region_chan));
270 } else {
271 if (!adapter->region_channel[rgnidx].valid)
272 continue;
273 scanregion = &adapter->region_channel[rgnidx];
274 }
275
276 for (nextchan = 0;
277 nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
278
279 cfp = scanregion->CFP + nextchan;
280
281 if (priv->adapter->enable11d) {
282 scantype =
283 libertas_get_scan_type_11d(cfp->channel,
284 &adapter->
285 parsed_region_chan);
286 }
287
288 switch (scanregion->band) {
289 case BAND_B:
290 case BAND_G:
291 default:
292 scanchanlist[chanidx].radiotype =
Dan Williams0aef64d2007-08-02 11:31:18 -0400293 CMD_SCAN_RADIO_TYPE_BG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200294 break;
295 }
296
Dan Williams0aef64d2007-08-02 11:31:18 -0400297 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200298 scanchanlist[chanidx].maxscantime =
David Woodhouse981f1872007-05-25 23:36:54 -0400299 cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200300 scanchanlist[chanidx].chanscanmode.passivescan =
301 1;
302 } else {
303 scanchanlist[chanidx].maxscantime =
David Woodhouse981f1872007-05-25 23:36:54 -0400304 cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200305 scanchanlist[chanidx].chanscanmode.passivescan =
306 0;
307 }
308
309 scanchanlist[chanidx].channumber = cfp->channel;
310
311 if (filteredscan) {
312 scanchanlist[chanidx].chanscanmode.
313 disablechanfilt = 1;
314 }
315 }
316 }
317}
318
Dan Williams2afc0c52007-08-02 13:19:04 -0400319
320/* Delayed partial scan worker */
321void libertas_scan_worker(struct work_struct *work)
322{
323 wlan_private *priv = container_of(work, wlan_private, scan_work.work);
324
325 wlan_scan_networks(priv, NULL, 0);
326}
327
328
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200329/**
330 * @brief Construct a wlan_scan_cmd_config structure to use in issue scan cmds
331 *
332 * Application layer or other functions can invoke wlan_scan_networks
333 * with a scan configuration supplied in a wlan_ioctl_user_scan_cfg struct.
334 * This structure is used as the basis of one or many wlan_scan_cmd_config
335 * commands that are sent to the command processing module and sent to
336 * firmware.
337 *
338 * Create a wlan_scan_cmd_config based on the following user supplied
339 * parameters (if present):
340 * - SSID filter
341 * - BSSID filter
342 * - Number of Probes to be sent
343 * - channel list
344 *
345 * If the SSID or BSSID filter is not present, disable/clear the filter.
346 * If the number of probes is not set, use the adapter default setting
347 * Qualify the channel
348 *
349 * @param priv A pointer to wlan_private structure
350 * @param puserscanin NULL or pointer to scan configuration parameters
351 * @param ppchantlvout Output parameter: Pointer to the start of the
352 * channel TLV portion of the output scan config
353 * @param pscanchanlist Output parameter: Pointer to the resulting channel
354 * list to scan
355 * @param pmaxchanperscan Output parameter: Number of channels to scan for
356 * each issuance of the firmware scan command
357 * @param pfilteredscan Output parameter: Flag indicating whether or not
358 * a BSSID or SSID filter is being sent in the
359 * command to firmware. Used to increase the number
360 * of channels sent in a scan command and to
361 * disable the firmware channel scan filter.
362 * @param pscancurrentonly Output parameter: Flag indicating whether or not
363 * we are only scanning our current active channel
364 *
365 * @return resulting scan configuration
366 */
367static struct wlan_scan_cmd_config *
368wlan_scan_setup_scan_config(wlan_private * priv,
369 const struct wlan_ioctl_user_scan_cfg * puserscanin,
370 struct mrvlietypes_chanlistparamset ** ppchantlvout,
371 struct chanscanparamset * pscanchanlist,
372 int *pmaxchanperscan,
373 u8 * pfilteredscan,
374 u8 * pscancurrentonly)
375{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200376 struct mrvlietypes_numprobes *pnumprobestlv;
377 struct mrvlietypes_ssidparamset *pssidtlv;
378 struct wlan_scan_cmd_config * pscancfgout = NULL;
379 u8 *ptlvpos;
380 u16 numprobes;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200381 int chanidx;
382 int scantype;
383 int scandur;
384 int channel;
385 int radiotype;
386
387 pscancfgout = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
388 if (pscancfgout == NULL)
389 goto out;
390
391 /* The tlvbufferlen is calculated for each scan command. The TLVs added
392 * in this routine will be preserved since the routine that sends
393 * the command will append channelTLVs at *ppchantlvout. The difference
394 * between the *ppchantlvout and the tlvbuffer start will be used
395 * to calculate the size of anything we add in this routine.
396 */
397 pscancfgout->tlvbufferlen = 0;
398
399 /* Running tlv pointer. Assigned to ppchantlvout at end of function
400 * so later routines know where channels can be added to the command buf
401 */
402 ptlvpos = pscancfgout->tlvbuffer;
403
404 /*
405 * Set the initial scan paramters for progressive scanning. If a specific
406 * BSSID or SSID is used, the number of channels in the scan command
407 * will be increased to the absolute maximum
408 */
409 *pmaxchanperscan = MRVDRV_CHANNELS_PER_SCAN_CMD;
410
411 /* Initialize the scan as un-filtered by firmware, set to TRUE below if
412 * a SSID or BSSID filter is sent in the command
413 */
414 *pfilteredscan = 0;
415
416 /* Initialize the scan as not being only on the current channel. If
417 * the channel list is customized, only contains one channel, and
418 * is the active channel, this is set true and data flow is not halted.
419 */
420 *pscancurrentonly = 0;
421
422 if (puserscanin) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200423 /* Set the bss type scan filter, use adapter setting if unset */
424 pscancfgout->bsstype =
Holger Schurigd65ead82007-08-02 13:12:12 -0400425 puserscanin->bsstype ? puserscanin->bsstype : CMD_BSS_TYPE_ANY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200426
427 /* Set the number of probes to send, use adapter setting if unset */
Holger Schurige2aa3342007-08-02 13:05:53 -0400428 numprobes = puserscanin->numprobes ? puserscanin->numprobes : 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200429
430 /*
431 * Set the BSSID filter to the incoming configuration,
432 * if non-zero. If not set, it will remain disabled (all zeros).
433 */
Dan Williamseb8f7332007-05-25 16:25:21 -0400434 memcpy(pscancfgout->bssid, puserscanin->bssid,
435 sizeof(pscancfgout->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200436
Dan Williamseb8f7332007-05-25 16:25:21 -0400437 if (puserscanin->ssid_len) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200438 pssidtlv =
439 (struct mrvlietypes_ssidparamset *) pscancfgout->
440 tlvbuffer;
441 pssidtlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
Dan Williamseb8f7332007-05-25 16:25:21 -0400442 pssidtlv->header.len = cpu_to_le16(puserscanin->ssid_len);
443 memcpy(pssidtlv->ssid, puserscanin->ssid,
444 puserscanin->ssid_len);
445 ptlvpos += sizeof(pssidtlv->header) + puserscanin->ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200446 }
447
448 /*
449 * The default number of channels sent in the command is low to
450 * ensure the response buffer from the firmware does not truncate
451 * scan results. That is not an issue with an SSID or BSSID
452 * filter applied to the scan results in the firmware.
453 */
Dan Williamseb8f7332007-05-25 16:25:21 -0400454 if ( puserscanin->ssid_len
455 || (compare_ether_addr(pscancfgout->bssid, &zeromac[0]) != 0)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200456 *pmaxchanperscan = MRVDRV_MAX_CHANNELS_PER_SCAN;
457 *pfilteredscan = 1;
458 }
459 } else {
Holger Schurigd65ead82007-08-02 13:12:12 -0400460 pscancfgout->bsstype = CMD_BSS_TYPE_ANY;
Holger Schurige2aa3342007-08-02 13:05:53 -0400461 numprobes = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200462 }
463
464 /* If the input config or adapter has the number of Probes set, add tlv */
465 if (numprobes) {
466 pnumprobestlv = (struct mrvlietypes_numprobes *) ptlvpos;
David Woodhouse981f1872007-05-25 23:36:54 -0400467 pnumprobestlv->header.type = cpu_to_le16(TLV_TYPE_NUMPROBES);
468 pnumprobestlv->header.len = cpu_to_le16(2);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200469 pnumprobestlv->numprobes = cpu_to_le16(numprobes);
470
David Woodhouse981f1872007-05-25 23:36:54 -0400471 ptlvpos += sizeof(*pnumprobestlv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200472 }
473
474 /*
475 * Set the output for the channel TLV to the address in the tlv buffer
476 * past any TLVs that were added in this fuction (SSID, numprobes).
477 * channel TLVs will be added past this for each scan command, preserving
478 * the TLVs that were previously added.
479 */
480 *ppchantlvout = (struct mrvlietypes_chanlistparamset *) ptlvpos;
481
Dan Williams2afc0c52007-08-02 13:19:04 -0400482 if (!puserscanin || !puserscanin->chanlist[0].channumber) {
483 /* Create a default channel scan list */
Holger Schurig9012b282007-05-25 11:27:16 -0400484 lbs_deb_scan("Scan: Creating full region channel list\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200485 wlan_scan_create_channel_list(priv, pscanchanlist,
486 *pfilteredscan);
Dan Williams2afc0c52007-08-02 13:19:04 -0400487 goto out;
488 }
489
490 lbs_deb_scan("Scan: Using supplied channel list\n");
491 for (chanidx = 0;
492 chanidx < WLAN_IOCTL_USER_SCAN_CHAN_MAX
493 && puserscanin->chanlist[chanidx].channumber; chanidx++) {
494
495 channel = puserscanin->chanlist[chanidx].channumber;
496 (pscanchanlist + chanidx)->channumber = channel;
497
498 radiotype = puserscanin->chanlist[chanidx].radiotype;
499 (pscanchanlist + chanidx)->radiotype = radiotype;
500
501 scantype = puserscanin->chanlist[chanidx].scantype;
502
503 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
504 (pscanchanlist +
505 chanidx)->chanscanmode.passivescan = 1;
506 } else {
507 (pscanchanlist +
508 chanidx)->chanscanmode.passivescan = 0;
509 }
510
511 if (puserscanin->chanlist[chanidx].scantime) {
512 scandur = puserscanin->chanlist[chanidx].scantime;
513 } else {
514 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
515 scandur = MRVDRV_PASSIVE_SCAN_CHAN_TIME;
516 } else {
517 scandur = MRVDRV_ACTIVE_SCAN_CHAN_TIME;
518 }
519 }
520
521 (pscanchanlist + chanidx)->minscantime =
522 cpu_to_le16(scandur);
523 (pscanchanlist + chanidx)->maxscantime =
524 cpu_to_le16(scandur);
525 }
526
527 /* Check if we are only scanning the current channel */
528 if ((chanidx == 1) &&
529 (puserscanin->chanlist[0].channumber ==
530 priv->adapter->curbssparams.channel)) {
531 *pscancurrentonly = 1;
532 lbs_deb_scan("Scan: Scanning current channel only");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533 }
534
535out:
536 return pscancfgout;
537}
538
539/**
540 * @brief Construct and send multiple scan config commands to the firmware
541 *
542 * Previous routines have created a wlan_scan_cmd_config with any requested
543 * TLVs. This function splits the channel TLV into maxchanperscan lists
544 * and sends the portion of the channel TLV along with the other TLVs
545 * to the wlan_cmd routines for execution in the firmware.
546 *
547 * @param priv A pointer to wlan_private structure
548 * @param maxchanperscan Maximum number channels to be included in each
549 * scan command sent to firmware
550 * @param filteredscan Flag indicating whether or not a BSSID or SSID
551 * filter is being used for the firmware command
552 * scan command sent to firmware
553 * @param pscancfgout Scan configuration used for this scan.
554 * @param pchantlvout Pointer in the pscancfgout where the channel TLV
555 * should start. This is past any other TLVs that
556 * must be sent down in each firmware command.
557 * @param pscanchanlist List of channels to scan in maxchanperscan segments
558 *
559 * @return 0 or error return otherwise
560 */
561static int wlan_scan_channel_list(wlan_private * priv,
562 int maxchanperscan,
563 u8 filteredscan,
564 struct wlan_scan_cmd_config * pscancfgout,
565 struct mrvlietypes_chanlistparamset * pchantlvout,
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400566 struct chanscanparamset * pscanchanlist,
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400567 const struct wlan_ioctl_user_scan_cfg * puserscanin,
568 int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200569{
570 struct chanscanparamset *ptmpchan;
571 struct chanscanparamset *pstartchan;
572 u8 scanband;
573 int doneearly;
574 int tlvidx;
575 int ret = 0;
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400576 int scanned = 0;
577 union iwreq_data wrqu;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200578
Holger Schurig9012b282007-05-25 11:27:16 -0400579 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200580
Holger Schurig314a8862007-10-08 12:20:04 +0200581 if (!pscancfgout || !pchantlvout || !pscanchanlist) {
Holger Schurig9012b282007-05-25 11:27:16 -0400582 lbs_deb_scan("Scan: Null detect: %p, %p, %p\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200583 pscancfgout, pchantlvout, pscanchanlist);
584 return -1;
585 }
586
587 pchantlvout->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
588
589 /* Set the temp channel struct pointer to the start of the desired list */
590 ptmpchan = pscanchanlist;
591
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400592 if (priv->adapter->last_scanned_channel && !puserscanin)
593 ptmpchan += priv->adapter->last_scanned_channel;
594
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200595 /* Loop through the desired channel list, sending a new firmware scan
596 * commands for each maxchanperscan channels (or for 1,6,11 individually
597 * if configured accordingly)
598 */
599 while (ptmpchan->channumber) {
600
601 tlvidx = 0;
602 pchantlvout->header.len = 0;
603 scanband = ptmpchan->radiotype;
604 pstartchan = ptmpchan;
605 doneearly = 0;
606
607 /* Construct the channel TLV for the scan command. Continue to
608 * insert channel TLVs until:
609 * - the tlvidx hits the maximum configured per scan command
610 * - the next channel to insert is 0 (end of desired channel list)
611 * - doneearly is set (controlling individual scanning of 1,6,11)
612 */
613 while (tlvidx < maxchanperscan && ptmpchan->channumber
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400614 && !doneearly && scanned < 2) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200615
Dan Williams2afc0c52007-08-02 13:19:04 -0400616 lbs_deb_scan("Scan: Chan(%3d), Radio(%d), mode(%d,%d), "
617 "Dur(%d)\n",
618 ptmpchan->channumber, ptmpchan->radiotype,
619 ptmpchan->chanscanmode.passivescan,
620 ptmpchan->chanscanmode.disablechanfilt,
621 ptmpchan->maxscantime);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200622
623 /* Copy the current channel TLV to the command being prepared */
624 memcpy(pchantlvout->chanscanparam + tlvidx,
625 ptmpchan, sizeof(pchantlvout->chanscanparam));
626
627 /* Increment the TLV header length by the size appended */
David Woodhouse981f1872007-05-25 23:36:54 -0400628 /* Ew, it would be _so_ nice if we could just declare the
629 variable little-endian and let GCC handle it for us */
630 pchantlvout->header.len =
631 cpu_to_le16(le16_to_cpu(pchantlvout->header.len) +
632 sizeof(pchantlvout->chanscanparam));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200633
634 /*
635 * The tlv buffer length is set to the number of bytes of the
636 * between the channel tlv pointer and the start of the
637 * tlv buffer. This compensates for any TLVs that were appended
638 * before the channel list.
639 */
640 pscancfgout->tlvbufferlen = ((u8 *) pchantlvout
641 - pscancfgout->tlvbuffer);
642
643 /* Add the size of the channel tlv header and the data length */
644 pscancfgout->tlvbufferlen +=
645 (sizeof(pchantlvout->header)
David Woodhouse981f1872007-05-25 23:36:54 -0400646 + le16_to_cpu(pchantlvout->header.len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200647
648 /* Increment the index to the channel tlv we are constructing */
649 tlvidx++;
650
651 doneearly = 0;
652
653 /* Stop the loop if the *current* channel is in the 1,6,11 set
654 * and we are not filtering on a BSSID or SSID.
655 */
656 if (!filteredscan && (ptmpchan->channumber == 1
657 || ptmpchan->channumber == 6
658 || ptmpchan->channumber == 11)) {
659 doneearly = 1;
660 }
661
662 /* Increment the tmp pointer to the next channel to be scanned */
663 ptmpchan++;
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400664 scanned++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200665
666 /* Stop the loop if the *next* channel is in the 1,6,11 set.
667 * This will cause it to be the only channel scanned on the next
668 * interation
669 */
670 if (!filteredscan && (ptmpchan->channumber == 1
671 || ptmpchan->channumber == 6
672 || ptmpchan->channumber == 11)) {
673 doneearly = 1;
674 }
675 }
676
677 /* Send the scan command to the firmware with the specified cfg */
Dan Williams0aef64d2007-08-02 11:31:18 -0400678 ret = libertas_prepare_and_send_command(priv, CMD_802_11_SCAN, 0,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200679 0, 0, pscancfgout);
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400680 if (scanned >= 2 && !full_scan) {
Holger Schurig9012b282007-05-25 11:27:16 -0400681 ret = 0;
682 goto done;
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400683 }
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400684 scanned = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200685 }
686
Dan Williamsd9ad2f52007-05-25 22:38:41 -0400687done:
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400688 priv->adapter->last_scanned_channel = ptmpchan->channumber;
689
Dan Williams2afc0c52007-08-02 13:19:04 -0400690 if (priv->adapter->last_scanned_channel) {
691 /* Schedule the next part of the partial scan */
692 if (!full_scan && !priv->adapter->surpriseremoved) {
693 cancel_delayed_work(&priv->scan_work);
694 queue_delayed_work(priv->work_thread, &priv->scan_work,
695 msecs_to_jiffies(300));
696 }
697 } else {
698 /* All done, tell userspace the scan table has been updated */
699 memset(&wrqu, 0, sizeof(union iwreq_data));
700 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
701 }
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400702
Holger Schurig9012b282007-05-25 11:27:16 -0400703 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200704 return ret;
705}
706
Dan Williamseb8f7332007-05-25 16:25:21 -0400707static void
708clear_selected_scan_list_entries(wlan_adapter * adapter,
709 const struct wlan_ioctl_user_scan_cfg * scan_cfg)
710{
711 struct bss_descriptor * bss;
712 struct bss_descriptor * safe;
713 u32 clear_ssid_flag = 0, clear_bssid_flag = 0;
714
715 if (!scan_cfg)
716 return;
717
718 if (scan_cfg->clear_ssid && scan_cfg->ssid_len)
719 clear_ssid_flag = 1;
720
721 if (scan_cfg->clear_bssid
722 && (compare_ether_addr(scan_cfg->bssid, &zeromac[0]) != 0)
723 && (compare_ether_addr(scan_cfg->bssid, &bcastmac[0]) != 0)) {
724 clear_bssid_flag = 1;
725 }
726
727 if (!clear_ssid_flag && !clear_bssid_flag)
728 return;
729
730 mutex_lock(&adapter->lock);
731 list_for_each_entry_safe (bss, safe, &adapter->network_list, list) {
732 u32 clear = 0;
733
734 /* Check for an SSID match */
735 if ( clear_ssid_flag
Dan Williamsd8efea22007-05-28 23:54:55 -0400736 && (bss->ssid_len == scan_cfg->ssid_len)
737 && !memcmp(bss->ssid, scan_cfg->ssid, bss->ssid_len))
Dan Williamseb8f7332007-05-25 16:25:21 -0400738 clear = 1;
739
740 /* Check for a BSSID match */
741 if ( clear_bssid_flag
742 && !compare_ether_addr(bss->bssid, scan_cfg->bssid))
743 clear = 1;
744
745 if (clear) {
746 list_move_tail (&bss->list, &adapter->network_free_list);
747 clear_bss_descriptor(bss);
748 }
749 }
750 mutex_unlock(&adapter->lock);
751}
752
753
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200754/**
755 * @brief Internal function used to start a scan based on an input config
756 *
757 * Use the input user scan configuration information when provided in
758 * order to send the appropriate scan commands to firmware to populate or
759 * update the internal driver scan table
760 *
761 * @param priv A pointer to wlan_private structure
762 * @param puserscanin Pointer to the input configuration for the requested
763 * scan.
764 *
765 * @return 0 or < 0 if error
766 */
767int wlan_scan_networks(wlan_private * priv,
Dan Williams2afc0c52007-08-02 13:19:04 -0400768 const struct wlan_ioctl_user_scan_cfg * puserscanin,
769 int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200770{
Dan Williamseb8f7332007-05-25 16:25:21 -0400771 wlan_adapter * adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200772 struct mrvlietypes_chanlistparamset *pchantlvout;
773 struct chanscanparamset * scan_chan_list = NULL;
774 struct wlan_scan_cmd_config * scan_cfg = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200775 u8 filteredscan;
776 u8 scancurrentchanonly;
777 int maxchanperscan;
778 int ret;
Dan Williamsf8f55102007-05-30 10:12:55 -0400779#ifdef CONFIG_LIBERTAS_DEBUG
780 struct bss_descriptor * iter_bss;
781 int i = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700782 DECLARE_MAC_BUF(mac);
Dan Williamsf8f55102007-05-30 10:12:55 -0400783#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200784
Dan Williams2afc0c52007-08-02 13:19:04 -0400785 lbs_deb_enter(LBS_DEB_SCAN);
786
787 /* Cancel any partial outstanding partial scans if this scan
788 * is a full scan.
789 */
790 if (full_scan && delayed_work_pending(&priv->scan_work))
791 cancel_delayed_work(&priv->scan_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200792
793 scan_chan_list = kzalloc(sizeof(struct chanscanparamset) *
794 WLAN_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
795 if (scan_chan_list == NULL) {
796 ret = -ENOMEM;
797 goto out;
798 }
799
800 scan_cfg = wlan_scan_setup_scan_config(priv,
801 puserscanin,
802 &pchantlvout,
803 scan_chan_list,
804 &maxchanperscan,
805 &filteredscan,
806 &scancurrentchanonly);
807 if (scan_cfg == NULL) {
808 ret = -ENOMEM;
809 goto out;
810 }
811
Dan Williamseb8f7332007-05-25 16:25:21 -0400812 clear_selected_scan_list_entries(adapter, puserscanin);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200813
814 /* Keep the data path active if we are only scanning our current channel */
815 if (!scancurrentchanonly) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400816 netif_stop_queue(priv->dev);
817 netif_carrier_off(priv->dev);
Holger Schurig3cf84092007-08-02 11:50:12 -0400818 if (priv->mesh_dev) {
819 netif_stop_queue(priv->mesh_dev);
820 netif_carrier_off(priv->mesh_dev);
821 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200822 }
823
824 ret = wlan_scan_channel_list(priv,
825 maxchanperscan,
826 filteredscan,
827 scan_cfg,
828 pchantlvout,
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400829 scan_chan_list,
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400830 puserscanin,
831 full_scan);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200832
Dan Williamsf8f55102007-05-30 10:12:55 -0400833#ifdef CONFIG_LIBERTAS_DEBUG
834 /* Dump the scan table */
835 mutex_lock(&adapter->lock);
836 list_for_each_entry (iter_bss, &adapter->network_list, list) {
Joe Perches0795af52007-10-03 17:59:30 -0700837 lbs_deb_scan("Scan:(%02d) %s, RSSI[%03d], SSID[%s]\n",
838 i++, print_mac(mac, iter_bss->bssid), (s32) iter_bss->rssi,
Dan Williamsf8f55102007-05-30 10:12:55 -0400839 escape_essid(iter_bss->ssid, iter_bss->ssid_len));
840 }
841 mutex_unlock(&adapter->lock);
842#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200843
Dan Williams0aef64d2007-08-02 11:31:18 -0400844 if (priv->adapter->connect_status == LIBERTAS_CONNECTED) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400845 netif_carrier_on(priv->dev);
846 netif_wake_queue(priv->dev);
Holger Schurig3cf84092007-08-02 11:50:12 -0400847 if (priv->mesh_dev) {
848 netif_carrier_on(priv->mesh_dev);
849 netif_wake_queue(priv->mesh_dev);
850 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200851 }
852
853out:
854 if (scan_cfg)
855 kfree(scan_cfg);
856
857 if (scan_chan_list)
858 kfree(scan_chan_list);
859
Holger Schurig9012b282007-05-25 11:27:16 -0400860 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200861 return ret;
862}
863
864/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200865 * @brief Interpret a BSS scan response returned from the firmware
866 *
867 * Parse the various fixed fields and IEs passed back for a a BSS probe
868 * response or beacon from the scan command. Record information as needed
869 * in the scan table struct bss_descriptor for that entry.
870 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400871 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200872 *
873 * @return 0 or -1
874 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400875static int libertas_process_bss(struct bss_descriptor * bss,
876 u8 ** pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200877{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200878 struct ieeetypes_fhparamset *pFH;
879 struct ieeetypes_dsparamset *pDS;
880 struct ieeetypes_cfparamset *pCF;
881 struct ieeetypes_ibssparamset *pibss;
Joe Perches0795af52007-10-03 17:59:30 -0700882 DECLARE_MAC_BUF(mac);
Dan Williams8c512762007-08-02 11:40:45 -0400883 struct ieeetypes_countryinfoset *pcountryinfo;
884 u8 *pos, *end, *p;
885 u8 n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
886 u16 beaconsize = 0;
Holger Schurig9012b282007-05-25 11:27:16 -0400887 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200888
Holger Schurig9012b282007-05-25 11:27:16 -0400889 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200890
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200891 if (*bytesleft >= sizeof(beaconsize)) {
892 /* Extract & convert beacon size from the command buffer */
Vladimir Davydovac630c22007-09-06 21:45:36 -0400893 beaconsize = le16_to_cpu(get_unaligned((u16 *)*pbeaconinfo));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200894 *bytesleft -= sizeof(beaconsize);
895 *pbeaconinfo += sizeof(beaconsize);
896 }
897
898 if (beaconsize == 0 || beaconsize > *bytesleft) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200899 *pbeaconinfo += *bytesleft;
900 *bytesleft = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200901 return -1;
902 }
903
904 /* Initialize the current working beacon pointer for this BSS iteration */
Dan Williamsab617972007-08-02 10:48:02 -0400905 pos = *pbeaconinfo;
906 end = pos + beaconsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200907
908 /* Advance the return beacon pointer past the current beacon */
909 *pbeaconinfo += beaconsize;
910 *bytesleft -= beaconsize;
911
Dan Williamsab617972007-08-02 10:48:02 -0400912 memcpy(bss->bssid, pos, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -0700913 lbs_deb_scan("process_bss: AP BSSID %s\n", print_mac(mac, bss->bssid));
Dan Williamsab617972007-08-02 10:48:02 -0400914 pos += ETH_ALEN;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200915
Dan Williamsab617972007-08-02 10:48:02 -0400916 if ((end - pos) < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400917 lbs_deb_scan("process_bss: Not enough bytes left\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200918 return -1;
919 }
920
921 /*
922 * next 4 fields are RSSI, time stamp, beacon interval,
923 * and capability information
924 */
925
926 /* RSSI is 1 byte long */
Dan Williamsab617972007-08-02 10:48:02 -0400927 bss->rssi = *pos;
928 lbs_deb_scan("process_bss: RSSI=%02X\n", *pos);
929 pos++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200930
931 /* time stamp is 8 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400932 pos += 8;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200933
934 /* beacon interval is 2 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400935 bss->beaconperiod = le16_to_cpup((void *) pos);
936 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200937
938 /* capability information is 2 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400939 bss->capability = le16_to_cpup((void *) pos);
Dan Williams0c9ca6902007-08-02 10:43:44 -0400940 lbs_deb_scan("process_bss: capabilities = 0x%4X\n", bss->capability);
Dan Williamsab617972007-08-02 10:48:02 -0400941 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200942
Dan Williams0c9ca6902007-08-02 10:43:44 -0400943 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
944 lbs_deb_scan("process_bss: AP WEP enabled\n");
945 if (bss->capability & WLAN_CAPABILITY_IBSS)
946 bss->mode = IW_MODE_ADHOC;
947 else
948 bss->mode = IW_MODE_INFRA;
949
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200950 /* rest of the current buffer are IE's */
Dan Williamsab617972007-08-02 10:48:02 -0400951 lbs_deb_scan("process_bss: IE length for this AP = %zd\n", end - pos);
Holger Schurigece56192007-08-02 11:53:06 -0400952 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200953
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200954 /* process variable IE */
Dan Williamsab617972007-08-02 10:48:02 -0400955 while (pos <= end - 2) {
956 struct ieee80211_info_element * elem =
957 (struct ieee80211_info_element *) pos;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200958
Dan Williamsab617972007-08-02 10:48:02 -0400959 if (pos + elem->len > end) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400960 lbs_deb_scan("process_bss: error in processing IE, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200961 "bytes left < IE length\n");
Dan Williamsab617972007-08-02 10:48:02 -0400962 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200963 }
964
Dan Williamsab617972007-08-02 10:48:02 -0400965 switch (elem->id) {
966 case MFIE_TYPE_SSID:
967 bss->ssid_len = elem->len;
968 memcpy(bss->ssid, elem->data, elem->len);
Dan Williamsd8efea22007-05-28 23:54:55 -0400969 lbs_deb_scan("ssid '%s', ssid length %u\n",
970 escape_essid(bss->ssid, bss->ssid_len),
971 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200972 break;
973
Dan Williamsab617972007-08-02 10:48:02 -0400974 case MFIE_TYPE_RATES:
Dan Williams8c512762007-08-02 11:40:45 -0400975 n_basic_rates = min_t(u8, MAX_RATES, elem->len);
976 memcpy(bss->rates, elem->data, n_basic_rates);
977 got_basic_rates = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200978 break;
979
Dan Williamsab617972007-08-02 10:48:02 -0400980 case MFIE_TYPE_FH_SET:
981 pFH = (struct ieeetypes_fhparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400982 memmove(&bss->phyparamset.fhparamset, pFH,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200983 sizeof(struct ieeetypes_fhparamset));
David Woodhouse981f1872007-05-25 23:36:54 -0400984#if 0 /* I think we can store these LE */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400985 bss->phyparamset.fhparamset.dwelltime
986 = le16_to_cpu(bss->phyparamset.fhparamset.dwelltime);
David Woodhouse981f1872007-05-25 23:36:54 -0400987#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200988 break;
989
Dan Williamsab617972007-08-02 10:48:02 -0400990 case MFIE_TYPE_DS_SET:
991 pDS = (struct ieeetypes_dsparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400992 bss->channel = pDS->currentchan;
993 memcpy(&bss->phyparamset.dsparamset, pDS,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200994 sizeof(struct ieeetypes_dsparamset));
995 break;
996
Dan Williamsab617972007-08-02 10:48:02 -0400997 case MFIE_TYPE_CF_SET:
998 pCF = (struct ieeetypes_cfparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400999 memcpy(&bss->ssparamset.cfparamset, pCF,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001000 sizeof(struct ieeetypes_cfparamset));
1001 break;
1002
Dan Williamsab617972007-08-02 10:48:02 -04001003 case MFIE_TYPE_IBSS_SET:
1004 pibss = (struct ieeetypes_ibssparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001005 bss->atimwindow = le32_to_cpu(pibss->atimwindow);
1006 memmove(&bss->ssparamset.ibssparamset, pibss,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001007 sizeof(struct ieeetypes_ibssparamset));
David Woodhouse981f1872007-05-25 23:36:54 -04001008#if 0
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001009 bss->ssparamset.ibssparamset.atimwindow
1010 = le16_to_cpu(bss->ssparamset.ibssparamset.atimwindow);
David Woodhouse981f1872007-05-25 23:36:54 -04001011#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001012 break;
1013
Dan Williamsab617972007-08-02 10:48:02 -04001014 case MFIE_TYPE_COUNTRY:
1015 pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001016 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001017 || pcountryinfo->len > 254) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001018 lbs_deb_scan("process_bss: 11D- Err "
Dan Williams4269e2a2007-05-10 23:10:18 -04001019 "CountryInfo len =%d min=%zd max=254\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001020 pcountryinfo->len,
1021 sizeof(pcountryinfo->countrycode));
Holger Schurig9012b282007-05-25 11:27:16 -04001022 ret = -1;
1023 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001024 }
1025
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001026 memcpy(&bss->countryinfo,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001027 pcountryinfo, pcountryinfo->len + 2);
Holger Schurigece56192007-08-02 11:53:06 -04001028 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001029 (u8 *) pcountryinfo,
1030 (u32) (pcountryinfo->len + 2));
1031 break;
1032
Dan Williamsab617972007-08-02 10:48:02 -04001033 case MFIE_TYPE_RATES_EX:
1034 /* only process extended supported rate if data rate is
1035 * already found. Data rate IE should come before
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001036 * extended supported rate IE
1037 */
Dan Williams8c512762007-08-02 11:40:45 -04001038 if (!got_basic_rates)
Dan Williamsab617972007-08-02 10:48:02 -04001039 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001040
Dan Williams8c512762007-08-02 11:40:45 -04001041 n_ex_rates = elem->len;
1042 if (n_basic_rates + n_ex_rates > MAX_RATES)
1043 n_ex_rates = MAX_RATES - n_basic_rates;
Dan Williamsab617972007-08-02 10:48:02 -04001044
Dan Williams8c512762007-08-02 11:40:45 -04001045 p = bss->rates + n_basic_rates;
1046 memcpy(p, elem->data, n_ex_rates);
Dan Williamsab617972007-08-02 10:48:02 -04001047 break;
1048
1049 case MFIE_TYPE_GENERIC:
1050 if (elem->len >= 4 &&
1051 elem->data[0] == 0x00 &&
1052 elem->data[1] == 0x50 &&
1053 elem->data[2] == 0xf2 &&
1054 elem->data[3] == 0x01) {
1055 bss->wpa_ie_len = min(elem->len + 2,
1056 MAX_WPA_IE_LEN);
1057 memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
Holger Schurigece56192007-08-02 11:53:06 -04001058 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: WPA IE", bss->wpa_ie,
Dan Williamsab617972007-08-02 10:48:02 -04001059 elem->len);
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001060 } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
1061 elem->data[0] == 0x00 &&
1062 elem->data[1] == 0x50 &&
1063 elem->data[2] == 0x43 &&
1064 elem->data[3] == 0x04) {
1065 bss->mesh = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001066 }
1067 break;
1068
Dan Williamsab617972007-08-02 10:48:02 -04001069 case MFIE_TYPE_RSN:
1070 bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
1071 memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
Holger Schurigece56192007-08-02 11:53:06 -04001072 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE", bss->rsn_ie, elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001073 break;
1074
Dan Williamsab617972007-08-02 10:48:02 -04001075 default:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001076 break;
1077 }
1078
Dan Williamsab617972007-08-02 10:48:02 -04001079 pos += elem->len + 2;
1080 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001081
1082 /* Timestamp */
1083 bss->last_scanned = jiffies;
Dan Williams8c512762007-08-02 11:40:45 -04001084 libertas_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001085
Holger Schurig9012b282007-05-25 11:27:16 -04001086 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001087
Holger Schurig9012b282007-05-25 11:27:16 -04001088done:
1089 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1090 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001091}
1092
1093/**
1094 * @brief Compare two SSIDs
1095 *
1096 * @param ssid1 A pointer to ssid to compare
1097 * @param ssid2 A pointer to ssid to compare
1098 *
1099 * @return 0--ssid is same, otherwise is different
1100 */
Dan Williams717c9332007-05-29 00:03:31 -04001101int libertas_ssid_cmp(u8 *ssid1, u8 ssid1_len, u8 *ssid2, u8 ssid2_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001102{
Dan Williamsd8efea22007-05-28 23:54:55 -04001103 if (ssid1_len != ssid2_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001104 return -1;
1105
Dan Williamsd8efea22007-05-28 23:54:55 -04001106 return memcmp(ssid1, ssid2, ssid1_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001107}
1108
1109/**
1110 * @brief This function finds a specific compatible BSSID in the scan list
1111 *
1112 * @param adapter A pointer to wlan_adapter
1113 * @param bssid BSSID to find in the scan list
1114 * @param mode Network mode: Infrastructure or IBSS
1115 *
1116 * @return index in BSSID list, or error return code (< 0)
1117 */
Dan Williams717c9332007-05-29 00:03:31 -04001118struct bss_descriptor * libertas_find_bssid_in_list(wlan_adapter * adapter,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001119 u8 * bssid, u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001120{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001121 struct bss_descriptor * iter_bss;
1122 struct bss_descriptor * found_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001123
1124 if (!bssid)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001125 return NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001126
Holger Schurigece56192007-08-02 11:53:06 -04001127 lbs_deb_hex(LBS_DEB_SCAN, "looking for",
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001128 bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001129
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001130 /* Look through the scan table for a compatible match. The loop will
1131 * continue past a matched bssid that is not compatible in case there
1132 * is an AP with multiple SSIDs assigned to the same BSSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001133 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001134 mutex_lock(&adapter->lock);
1135 list_for_each_entry (iter_bss, &adapter->network_list, list) {
Dan Williams3cf209312007-05-25 17:28:30 -04001136 if (compare_ether_addr(iter_bss->bssid, bssid))
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001137 continue; /* bssid doesn't match */
1138 switch (mode) {
1139 case IW_MODE_INFRA:
1140 case IW_MODE_ADHOC:
1141 if (!is_network_compatible(adapter, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001142 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001143 found_bss = iter_bss;
1144 break;
1145 default:
1146 found_bss = iter_bss;
1147 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001148 }
1149 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001150 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001151
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001152 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001153}
1154
1155/**
1156 * @brief This function finds ssid in ssid list.
1157 *
1158 * @param adapter A pointer to wlan_adapter
1159 * @param ssid SSID to find in the list
1160 * @param bssid BSSID to qualify the SSID selection (if provided)
1161 * @param mode Network mode: Infrastructure or IBSS
1162 *
1163 * @return index in BSSID list
1164 */
Dan Williams717c9332007-05-29 00:03:31 -04001165struct bss_descriptor * libertas_find_ssid_in_list(wlan_adapter * adapter,
Dan Williamsd8efea22007-05-28 23:54:55 -04001166 u8 *ssid, u8 ssid_len, u8 * bssid, u8 mode,
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001167 int channel)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001168{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001169 u8 bestrssi = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001170 struct bss_descriptor * iter_bss = NULL;
1171 struct bss_descriptor * found_bss = NULL;
1172 struct bss_descriptor * tmp_oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001173
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001174 mutex_lock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001175
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001176 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1177 if ( !tmp_oldest
1178 || (iter_bss->last_scanned < tmp_oldest->last_scanned))
1179 tmp_oldest = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001180
Dan Williams717c9332007-05-29 00:03:31 -04001181 if (libertas_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001182 ssid, ssid_len) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001183 continue; /* ssid doesn't match */
Dan Williams3cf209312007-05-25 17:28:30 -04001184 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001185 continue; /* bssid doesn't match */
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001186 if ((channel > 0) && (iter_bss->channel != channel))
1187 continue; /* channel doesn't match */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001188
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001189 switch (mode) {
1190 case IW_MODE_INFRA:
1191 case IW_MODE_ADHOC:
1192 if (!is_network_compatible(adapter, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001193 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001194
1195 if (bssid) {
1196 /* Found requested BSSID */
1197 found_bss = iter_bss;
1198 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001199 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001200
1201 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1202 bestrssi = SCAN_RSSI(iter_bss->rssi);
1203 found_bss = iter_bss;
1204 }
1205 break;
1206 case IW_MODE_AUTO:
1207 default:
1208 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1209 bestrssi = SCAN_RSSI(iter_bss->rssi);
1210 found_bss = iter_bss;
1211 }
1212 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001213 }
1214 }
1215
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001216out:
1217 mutex_unlock(&adapter->lock);
1218 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001219}
1220
1221/**
1222 * @brief This function finds the best SSID in the Scan List
1223 *
1224 * Search the scan table for the best SSID that also matches the current
1225 * adapter network preference (infrastructure or adhoc)
1226 *
1227 * @param adapter A pointer to wlan_adapter
1228 *
1229 * @return index in BSSID list
1230 */
Holger Schurigac558ca2007-08-02 11:49:06 -04001231static struct bss_descriptor * libertas_find_best_ssid_in_list(wlan_adapter * adapter,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001232 u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001233{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001234 u8 bestrssi = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001235 struct bss_descriptor * iter_bss;
1236 struct bss_descriptor * best_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001237
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001238 mutex_lock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001239
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001240 list_for_each_entry (iter_bss, &adapter->network_list, list) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001241 switch (mode) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001242 case IW_MODE_INFRA:
1243 case IW_MODE_ADHOC:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001244 if (!is_network_compatible(adapter, iter_bss, mode))
1245 break;
1246 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1247 break;
1248 bestrssi = SCAN_RSSI(iter_bss->rssi);
1249 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001250 break;
Dan Williams0dc5a292007-05-10 22:58:02 -04001251 case IW_MODE_AUTO:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001252 default:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001253 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1254 break;
1255 bestrssi = SCAN_RSSI(iter_bss->rssi);
1256 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001257 break;
1258 }
1259 }
1260
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001261 mutex_unlock(&adapter->lock);
1262 return best_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001263}
1264
1265/**
1266 * @brief Find the AP with specific ssid in the scan list
1267 *
1268 * @param priv A pointer to wlan_private structure
1269 * @param pSSID A pointer to AP's ssid
1270 *
1271 * @return 0--success, otherwise--fail
1272 */
Dan Williams717c9332007-05-29 00:03:31 -04001273int libertas_find_best_network_ssid(wlan_private * priv,
Dan Williamsd8efea22007-05-28 23:54:55 -04001274 u8 *out_ssid, u8 *out_ssid_len, u8 preferred_mode, u8 *out_mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001275{
1276 wlan_adapter *adapter = priv->adapter;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001277 int ret = -1;
1278 struct bss_descriptor * found;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001279
Holger Schurig9012b282007-05-25 11:27:16 -04001280 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001281
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001282 wlan_scan_networks(priv, NULL, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001283 if (adapter->surpriseremoved)
1284 return -1;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001285
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001286 wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1287
Dan Williams717c9332007-05-29 00:03:31 -04001288 found = libertas_find_best_ssid_in_list(adapter, preferred_mode);
Dan Williamsd8efea22007-05-28 23:54:55 -04001289 if (found && (found->ssid_len > 0)) {
1290 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1291 *out_ssid_len = found->ssid_len;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001292 *out_mode = found->mode;
1293 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001294 }
1295
Holger Schurig9012b282007-05-25 11:27:16 -04001296 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001297 return ret;
1298}
1299
1300/**
1301 * @brief Scan Network
1302 *
1303 * @param dev A pointer to net_device structure
1304 * @param info A pointer to iw_request_info structure
1305 * @param vwrq A pointer to iw_param structure
1306 * @param extra A pointer to extra data buf
1307 *
1308 * @return 0 --success, otherwise fail
1309 */
1310int libertas_set_scan(struct net_device *dev, struct iw_request_info *info,
1311 struct iw_param *vwrq, char *extra)
1312{
1313 wlan_private *priv = dev->priv;
1314 wlan_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001315
Holger Schurig9012b282007-05-25 11:27:16 -04001316 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001317
Dan Williams2afc0c52007-08-02 13:19:04 -04001318 if (!delayed_work_pending(&priv->scan_work)) {
1319 queue_delayed_work(priv->work_thread, &priv->scan_work,
1320 msecs_to_jiffies(50));
1321 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001322
1323 if (adapter->surpriseremoved)
1324 return -1;
1325
Holger Schurig9012b282007-05-25 11:27:16 -04001326 lbs_deb_leave(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001327 return 0;
1328}
1329
1330/**
1331 * @brief Send a scan command for all available channels filtered on a spec
1332 *
1333 * @param priv A pointer to wlan_private structure
1334 * @param prequestedssid A pointer to AP's ssid
1335 * @param keeppreviousscan Flag used to save/clear scan table before scan
1336 *
1337 * @return 0-success, otherwise fail
1338 */
Dan Williams717c9332007-05-29 00:03:31 -04001339int libertas_send_specific_ssid_scan(wlan_private * priv,
Dan Williamsd8efea22007-05-28 23:54:55 -04001340 u8 *ssid, u8 ssid_len, u8 clear_ssid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001341{
1342 wlan_adapter *adapter = priv->adapter;
1343 struct wlan_ioctl_user_scan_cfg scancfg;
Dan Williamseb8f7332007-05-25 16:25:21 -04001344 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001345
Holger Schurig9012b282007-05-25 11:27:16 -04001346 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001347
Dan Williamsd8efea22007-05-28 23:54:55 -04001348 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -04001349 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001350
1351 memset(&scancfg, 0x00, sizeof(scancfg));
Dan Williamsd8efea22007-05-28 23:54:55 -04001352 memcpy(scancfg.ssid, ssid, ssid_len);
1353 scancfg.ssid_len = ssid_len;
Dan Williamseb8f7332007-05-25 16:25:21 -04001354 scancfg.clear_ssid = clear_ssid;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001355
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001356 wlan_scan_networks(priv, &scancfg, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001357 if (adapter->surpriseremoved)
1358 return -1;
1359 wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1360
Dan Williamseb8f7332007-05-25 16:25:21 -04001361out:
Holger Schurig9012b282007-05-25 11:27:16 -04001362 lbs_deb_leave(LBS_DEB_ASSOC);
Dan Williamseb8f7332007-05-25 16:25:21 -04001363 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001364}
1365
Dan Williams00af0152007-08-02 13:14:56 -04001366#define MAX_CUSTOM_LEN 64
1367
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001368static inline char *libertas_translate_scan(wlan_private *priv,
1369 char *start, char *stop,
1370 struct bss_descriptor *bss)
1371{
1372 wlan_adapter *adapter = priv->adapter;
1373 struct chan_freq_power *cfp;
1374 char *current_val; /* For rates */
1375 struct iw_event iwe; /* Temporary buffer */
1376 int j;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001377#define PERFECT_RSSI ((u8)50)
1378#define WORST_RSSI ((u8)0)
1379#define RSSI_DIFF ((u8)(PERFECT_RSSI - WORST_RSSI))
1380 u8 rssi;
1381
1382 cfp = libertas_find_cfp_by_band_and_channel(adapter, 0, bss->channel);
1383 if (!cfp) {
1384 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
1385 return NULL;
1386 }
1387
1388 /* First entry *MUST* be the AP BSSID */
1389 iwe.cmd = SIOCGIWAP;
1390 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1391 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
1392 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
1393
1394 /* SSID */
1395 iwe.cmd = SIOCGIWESSID;
1396 iwe.u.data.flags = 1;
Dan Williamsd8efea22007-05-28 23:54:55 -04001397 iwe.u.data.length = min((u32) bss->ssid_len, (u32) IW_ESSID_MAX_SIZE);
1398 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001399
1400 /* Mode */
1401 iwe.cmd = SIOCGIWMODE;
1402 iwe.u.mode = bss->mode;
1403 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
1404
1405 /* Frequency */
1406 iwe.cmd = SIOCGIWFREQ;
1407 iwe.u.freq.m = (long)cfp->freq * 100000;
1408 iwe.u.freq.e = 1;
1409 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
1410
1411 /* Add quality statistics */
1412 iwe.cmd = IWEVQUAL;
1413 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
1414 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
1415
1416 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
1417 iwe.u.qual.qual =
1418 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
1419 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
1420 (RSSI_DIFF * RSSI_DIFF);
1421 if (iwe.u.qual.qual > 100)
1422 iwe.u.qual.qual = 100;
1423
1424 if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
1425 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
1426 } else {
1427 iwe.u.qual.noise =
1428 CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
1429 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001430
Dan Williams80e78ef2007-05-25 22:18:47 -04001431 /* Locally created ad-hoc BSSs won't have beacons if this is the
1432 * only station in the adhoc network; so get signal strength
1433 * from receive statistics.
1434 */
1435 if ((adapter->mode == IW_MODE_ADHOC)
1436 && adapter->adhoccreate
Dan Williams717c9332007-05-29 00:03:31 -04001437 && !libertas_ssid_cmp(adapter->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001438 adapter->curbssparams.ssid_len,
1439 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -04001440 int snr, nf;
1441 snr = adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1442 nf = adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1443 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001444 }
1445 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
1446
1447 /* Add encryption capability */
1448 iwe.cmd = SIOCGIWENCODE;
Dan Williams0c9ca6902007-08-02 10:43:44 -04001449 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001450 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1451 } else {
1452 iwe.u.data.flags = IW_ENCODE_DISABLED;
1453 }
1454 iwe.u.data.length = 0;
Dan Williamsd8efea22007-05-28 23:54:55 -04001455 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001456
1457 current_val = start + IW_EV_LCP_LEN;
1458
1459 iwe.cmd = SIOCGIWRATE;
1460 iwe.u.bitrate.fixed = 0;
1461 iwe.u.bitrate.disabled = 0;
1462 iwe.u.bitrate.value = 0;
1463
Dan Williams8c512762007-08-02 11:40:45 -04001464 for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
1465 /* Bit rate given in 500 kb/s units */
1466 iwe.u.bitrate.value = bss->rates[j] * 500000;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001467 current_val = iwe_stream_add_value(start, current_val,
1468 stop, &iwe, IW_EV_PARAM_LEN);
1469 }
1470 if ((bss->mode == IW_MODE_ADHOC)
Dan Williams717c9332007-05-29 00:03:31 -04001471 && !libertas_ssid_cmp(adapter->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001472 adapter->curbssparams.ssid_len,
1473 bss->ssid, bss->ssid_len)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001474 && adapter->adhoccreate) {
1475 iwe.u.bitrate.value = 22 * 500000;
1476 current_val = iwe_stream_add_value(start, current_val,
1477 stop, &iwe, IW_EV_PARAM_LEN);
1478 }
1479 /* Check if we added any event */
1480 if((current_val - start) > IW_EV_LCP_LEN)
1481 start = current_val;
1482
1483 memset(&iwe, 0, sizeof(iwe));
1484 if (bss->wpa_ie_len) {
1485 char buf[MAX_WPA_IE_LEN];
1486 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
1487 iwe.cmd = IWEVGENIE;
1488 iwe.u.data.length = bss->wpa_ie_len;
1489 start = iwe_stream_add_point(start, stop, &iwe, buf);
1490 }
1491
1492 memset(&iwe, 0, sizeof(iwe));
1493 if (bss->rsn_ie_len) {
1494 char buf[MAX_WPA_IE_LEN];
1495 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
1496 iwe.cmd = IWEVGENIE;
1497 iwe.u.data.length = bss->rsn_ie_len;
1498 start = iwe_stream_add_point(start, stop, &iwe, buf);
1499 }
1500
Dan Williams00af0152007-08-02 13:14:56 -04001501 if (bss->mesh) {
1502 char custom[MAX_CUSTOM_LEN];
1503 char *p = custom;
1504
1505 iwe.cmd = IWEVCUSTOM;
1506 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
1507 "mesh-type: olpc");
1508 iwe.u.data.length = p - custom;
1509 if (iwe.u.data.length)
1510 start = iwe_stream_add_point(start, stop, &iwe, custom);
1511 }
1512
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001513 return start;
1514}
1515
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001516/**
1517 * @brief Retrieve the scan table entries via wireless tools IOCTL call
1518 *
1519 * @param dev A pointer to net_device structure
1520 * @param info A pointer to iw_request_info structure
1521 * @param dwrq A pointer to iw_point structure
1522 * @param extra A pointer to extra data buf
1523 *
1524 * @return 0 --success, otherwise fail
1525 */
1526int libertas_get_scan(struct net_device *dev, struct iw_request_info *info,
1527 struct iw_point *dwrq, char *extra)
1528{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001529#define SCAN_ITEM_SIZE 128
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001530 wlan_private *priv = dev->priv;
1531 wlan_adapter *adapter = priv->adapter;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001532 int err = 0;
1533 char *ev = extra;
1534 char *stop = ev + dwrq->length;
1535 struct bss_descriptor * iter_bss;
1536 struct bss_descriptor * safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001537
Holger Schurig9012b282007-05-25 11:27:16 -04001538 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001539
Dan Williams80e78ef2007-05-25 22:18:47 -04001540 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001541 if ((adapter->mode == IW_MODE_ADHOC) && adapter->adhoccreate) {
Dan Williams0aef64d2007-08-02 11:31:18 -04001542 libertas_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
1543 CMD_OPTION_WAITFORRSP, 0, NULL);
Dan Williams80e78ef2007-05-25 22:18:47 -04001544 }
1545
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001546 mutex_lock(&adapter->lock);
1547 list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1548 char * next_ev;
1549 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001550
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001551 if (stop - ev < SCAN_ITEM_SIZE) {
1552 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001553 break;
1554 }
1555
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001556 /* For mesh device, list only mesh networks */
1557 if (dev == priv->mesh_dev && !iter_bss->mesh)
1558 continue;
1559
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001560 /* Prune old an old scan result */
1561 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1562 if (time_after(jiffies, stale_time)) {
1563 list_move_tail (&iter_bss->list,
1564 &adapter->network_free_list);
1565 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001566 continue;
1567 }
1568
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001569 /* Translate to WE format this entry */
1570 next_ev = libertas_translate_scan(priv, ev, stop, iter_bss);
1571 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001572 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001573 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001574 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001575 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001576
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001577 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001578 dwrq->flags = 0;
1579
Holger Schurig9012b282007-05-25 11:27:16 -04001580 lbs_deb_leave(LBS_DEB_ASSOC);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001581 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001582}
1583
1584/**
1585 * @brief Prepare a scan command to be sent to the firmware
1586 *
1587 * Use the wlan_scan_cmd_config sent to the command processing module in
1588 * the libertas_prepare_and_send_command to configure a cmd_ds_802_11_scan command
1589 * struct to send to firmware.
1590 *
1591 * The fixed fields specifying the BSS type and BSSID filters as well as a
1592 * variable number/length of TLVs are sent in the command to firmware.
1593 *
1594 * @param priv A pointer to wlan_private structure
1595 * @param cmd A pointer to cmd_ds_command structure to be sent to
1596 * firmware with the cmd_DS_801_11_SCAN structure
1597 * @param pdata_buf Void pointer cast of a wlan_scan_cmd_config struct used
1598 * to set the fields/TLVs for the command sent to firmware
1599 *
1600 * @return 0 or -1
1601 *
1602 * @sa wlan_scan_create_channel_list
1603 */
1604int libertas_cmd_80211_scan(wlan_private * priv,
1605 struct cmd_ds_command *cmd, void *pdata_buf)
1606{
1607 struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
1608 struct wlan_scan_cmd_config *pscancfg;
1609
Holger Schurig9012b282007-05-25 11:27:16 -04001610 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001611
1612 pscancfg = pdata_buf;
1613
1614 /* Set fixed field variables in scan command */
1615 pscan->bsstype = pscancfg->bsstype;
Dan Williams492b6da2007-08-02 11:16:07 -04001616 memcpy(pscan->bssid, pscancfg->bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001617 memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
1618
Dan Williams0aef64d2007-08-02 11:31:18 -04001619 cmd->command = cpu_to_le16(CMD_802_11_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001620
1621 /* size is equal to the sizeof(fixed portions) + the TLV len + header */
Dan Williams492b6da2007-08-02 11:16:07 -04001622 cmd->size = cpu_to_le16(sizeof(pscan->bsstype) + ETH_ALEN
1623 + pscancfg->tlvbufferlen + S_DS_GEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001624
Holger Schurig9012b282007-05-25 11:27:16 -04001625 lbs_deb_scan("SCAN_CMD: command=%x, size=%x, seqnum=%x\n",
David Woodhouse981f1872007-05-25 23:36:54 -04001626 le16_to_cpu(cmd->command), le16_to_cpu(cmd->size),
1627 le16_to_cpu(cmd->seqnum));
Holger Schurig9012b282007-05-25 11:27:16 -04001628
1629 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001630 return 0;
1631}
1632
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001633static inline int is_same_network(struct bss_descriptor *src,
1634 struct bss_descriptor *dst)
1635{
1636 /* A network is only a duplicate if the channel, BSSID, and ESSID
1637 * all match. We treat all <hidden> with the same BSSID and channel
1638 * as one network */
Dan Williamsd8efea22007-05-28 23:54:55 -04001639 return ((src->ssid_len == dst->ssid_len) &&
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001640 (src->channel == dst->channel) &&
1641 !compare_ether_addr(src->bssid, dst->bssid) &&
Dan Williamsd8efea22007-05-28 23:54:55 -04001642 !memcmp(src->ssid, dst->ssid, src->ssid_len));
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001643}
1644
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001645/**
1646 * @brief This function handles the command response of scan
1647 *
1648 * The response buffer for the scan command has the following
1649 * memory layout:
1650 *
1651 * .-----------------------------------------------------------.
1652 * | header (4 * sizeof(u16)): Standard command response hdr |
1653 * .-----------------------------------------------------------.
1654 * | bufsize (u16) : sizeof the BSS Description data |
1655 * .-----------------------------------------------------------.
1656 * | NumOfSet (u8) : Number of BSS Descs returned |
1657 * .-----------------------------------------------------------.
1658 * | BSSDescription data (variable, size given in bufsize) |
1659 * .-----------------------------------------------------------.
1660 * | TLV data (variable, size calculated using header->size, |
1661 * | bufsize and sizeof the fixed fields above) |
1662 * .-----------------------------------------------------------.
1663 *
1664 * @param priv A pointer to wlan_private structure
1665 * @param resp A pointer to cmd_ds_command
1666 *
1667 * @return 0 or -1
1668 */
1669int libertas_ret_80211_scan(wlan_private * priv, struct cmd_ds_command *resp)
1670{
1671 wlan_adapter *adapter = priv->adapter;
1672 struct cmd_ds_802_11_scan_rsp *pscan;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001673 struct bss_descriptor * iter_bss;
1674 struct bss_descriptor * safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001675 u8 *pbssinfo;
1676 u16 scanrespsize;
1677 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001678 int idx;
1679 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001680 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001681
Holger Schurig9012b282007-05-25 11:27:16 -04001682 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001683
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001684 /* Prune old entries from scan table */
1685 list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1686 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1687 if (time_before(jiffies, stale_time))
1688 continue;
1689 list_move_tail (&iter_bss->list, &adapter->network_free_list);
1690 clear_bss_descriptor(iter_bss);
1691 }
1692
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001693 pscan = &resp->params.scanresp;
1694
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001695 if (pscan->nr_sets > MAX_NETWORK_COUNT) {
1696 lbs_deb_scan(
1697 "SCAN_RESP: too many scan results (%d, max %d)!!\n",
1698 pscan->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001699 ret = -1;
1700 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001701 }
1702
Vladimir Davydovac630c22007-09-06 21:45:36 -04001703 bytesleft = le16_to_cpu(get_unaligned((u16*)&pscan->bssdescriptsize));
Holger Schurig9012b282007-05-25 11:27:16 -04001704 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001705
Vladimir Davydovac630c22007-09-06 21:45:36 -04001706 scanrespsize = le16_to_cpu(get_unaligned((u16*)&resp->size));
Holger Schurig9012b282007-05-25 11:27:16 -04001707 lbs_deb_scan("SCAN_RESP: returned %d AP before parsing\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001708 pscan->nr_sets);
1709
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001710 pbssinfo = pscan->bssdesc_and_tlvbuffer;
1711
1712 /* The size of the TLV buffer is equal to the entire command response
1713 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1714 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1715 * response header (S_DS_GEN)
1716 */
1717 tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
1718 + sizeof(pscan->nr_sets)
1719 + S_DS_GEN);
1720
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001721 /*
1722 * Process each scan response returned (pscan->nr_sets). Save
1723 * the information in the newbssentry and then insert into the
1724 * driver scan table either as an update to an existing entry
1725 * or as an addition at the end of the table
1726 */
1727 for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001728 struct bss_descriptor new;
1729 struct bss_descriptor * found = NULL;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001730 struct bss_descriptor * oldest = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07001731 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001732
1733 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001734 memset(&new, 0, sizeof (struct bss_descriptor));
1735 if (libertas_process_bss(&new, &pbssinfo, &bytesleft) != 0) {
1736 /* error parsing the scan response, skipped */
1737 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1738 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001739 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001740
1741 /* Try to find this bss in the scan table */
1742 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1743 if (is_same_network(iter_bss, &new)) {
1744 found = iter_bss;
1745 break;
1746 }
1747
1748 if ((oldest == NULL) ||
1749 (iter_bss->last_scanned < oldest->last_scanned))
1750 oldest = iter_bss;
1751 }
1752
1753 if (found) {
1754 /* found, clear it */
1755 clear_bss_descriptor(found);
1756 } else if (!list_empty(&adapter->network_free_list)) {
1757 /* Pull one from the free list */
1758 found = list_entry(adapter->network_free_list.next,
1759 struct bss_descriptor, list);
1760 list_move_tail(&found->list, &adapter->network_list);
1761 } else if (oldest) {
1762 /* If there are no more slots, expire the oldest */
1763 found = oldest;
1764 clear_bss_descriptor(found);
1765 list_move_tail(&found->list, &adapter->network_list);
1766 } else {
1767 continue;
1768 }
1769
Joe Perches0795af52007-10-03 17:59:30 -07001770 lbs_deb_scan("SCAN_RESP: BSSID = %s\n",
1771 print_mac(mac, new.bssid));
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001772
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001773 /* Copy the locally created newbssentry to the scan table */
1774 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001775 }
1776
Holger Schurig9012b282007-05-25 11:27:16 -04001777 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001778
Holger Schurig9012b282007-05-25 11:27:16 -04001779done:
1780 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1781 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001782}