blob: e2e9ebcd8340372e280de4d0d92632d1197dec4b [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 Williams0c9ca692007-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 Williams0c9ca692007-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 Williams0c9ca692007-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 Williams0c9ca692007-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 Williams0c9ca692007-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 Williams0c9ca692007-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 Williams0c9ca692007-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 Williams0c9ca692007-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 Williams0c9ca692007-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 */
Holger Schurig4f2fdaa2007-08-02 13:12:27 -0400255 scantype = CMD_SCAN_TYPE_ACTIVE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200256
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
Dan Williams2afc0c52007-08-02 13:19:04 -0400317
318/* Delayed partial scan worker */
319void libertas_scan_worker(struct work_struct *work)
320{
321 wlan_private *priv = container_of(work, wlan_private, scan_work.work);
322
323 wlan_scan_networks(priv, NULL, 0);
324}
325
326
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200327/**
328 * @brief Construct a wlan_scan_cmd_config structure to use in issue scan cmds
329 *
330 * Application layer or other functions can invoke wlan_scan_networks
331 * with a scan configuration supplied in a wlan_ioctl_user_scan_cfg struct.
332 * This structure is used as the basis of one or many wlan_scan_cmd_config
333 * commands that are sent to the command processing module and sent to
334 * firmware.
335 *
336 * Create a wlan_scan_cmd_config based on the following user supplied
337 * parameters (if present):
338 * - SSID filter
339 * - BSSID filter
340 * - Number of Probes to be sent
341 * - channel list
342 *
343 * If the SSID or BSSID filter is not present, disable/clear the filter.
344 * If the number of probes is not set, use the adapter default setting
345 * Qualify the channel
346 *
347 * @param priv A pointer to wlan_private structure
348 * @param puserscanin NULL or pointer to scan configuration parameters
349 * @param ppchantlvout Output parameter: Pointer to the start of the
350 * channel TLV portion of the output scan config
351 * @param pscanchanlist Output parameter: Pointer to the resulting channel
352 * list to scan
353 * @param pmaxchanperscan Output parameter: Number of channels to scan for
354 * each issuance of the firmware scan command
355 * @param pfilteredscan Output parameter: Flag indicating whether or not
356 * a BSSID or SSID filter is being sent in the
357 * command to firmware. Used to increase the number
358 * of channels sent in a scan command and to
359 * disable the firmware channel scan filter.
360 * @param pscancurrentonly Output parameter: Flag indicating whether or not
361 * we are only scanning our current active channel
362 *
363 * @return resulting scan configuration
364 */
365static struct wlan_scan_cmd_config *
366wlan_scan_setup_scan_config(wlan_private * priv,
367 const struct wlan_ioctl_user_scan_cfg * puserscanin,
368 struct mrvlietypes_chanlistparamset ** ppchantlvout,
369 struct chanscanparamset * pscanchanlist,
370 int *pmaxchanperscan,
371 u8 * pfilteredscan,
372 u8 * pscancurrentonly)
373{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200374 struct mrvlietypes_numprobes *pnumprobestlv;
375 struct mrvlietypes_ssidparamset *pssidtlv;
376 struct wlan_scan_cmd_config * pscancfgout = NULL;
377 u8 *ptlvpos;
378 u16 numprobes;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200379 int chanidx;
380 int scantype;
381 int scandur;
382 int channel;
383 int radiotype;
384
385 pscancfgout = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
386 if (pscancfgout == NULL)
387 goto out;
388
389 /* The tlvbufferlen is calculated for each scan command. The TLVs added
390 * in this routine will be preserved since the routine that sends
391 * the command will append channelTLVs at *ppchantlvout. The difference
392 * between the *ppchantlvout and the tlvbuffer start will be used
393 * to calculate the size of anything we add in this routine.
394 */
395 pscancfgout->tlvbufferlen = 0;
396
397 /* Running tlv pointer. Assigned to ppchantlvout at end of function
398 * so later routines know where channels can be added to the command buf
399 */
400 ptlvpos = pscancfgout->tlvbuffer;
401
402 /*
403 * Set the initial scan paramters for progressive scanning. If a specific
404 * BSSID or SSID is used, the number of channels in the scan command
405 * will be increased to the absolute maximum
406 */
407 *pmaxchanperscan = MRVDRV_CHANNELS_PER_SCAN_CMD;
408
409 /* Initialize the scan as un-filtered by firmware, set to TRUE below if
410 * a SSID or BSSID filter is sent in the command
411 */
412 *pfilteredscan = 0;
413
414 /* Initialize the scan as not being only on the current channel. If
415 * the channel list is customized, only contains one channel, and
416 * is the active channel, this is set true and data flow is not halted.
417 */
418 *pscancurrentonly = 0;
419
420 if (puserscanin) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200421 /* Set the bss type scan filter, use adapter setting if unset */
422 pscancfgout->bsstype =
Holger Schurigd65ead82007-08-02 13:12:12 -0400423 puserscanin->bsstype ? puserscanin->bsstype : CMD_BSS_TYPE_ANY;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200424
425 /* Set the number of probes to send, use adapter setting if unset */
Holger Schurige2aa3342007-08-02 13:05:53 -0400426 numprobes = puserscanin->numprobes ? puserscanin->numprobes : 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200427
428 /*
429 * Set the BSSID filter to the incoming configuration,
430 * if non-zero. If not set, it will remain disabled (all zeros).
431 */
Dan Williamseb8f7332007-05-25 16:25:21 -0400432 memcpy(pscancfgout->bssid, puserscanin->bssid,
433 sizeof(pscancfgout->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200434
Dan Williamseb8f7332007-05-25 16:25:21 -0400435 if (puserscanin->ssid_len) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200436 pssidtlv =
437 (struct mrvlietypes_ssidparamset *) pscancfgout->
438 tlvbuffer;
439 pssidtlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
Dan Williamseb8f7332007-05-25 16:25:21 -0400440 pssidtlv->header.len = cpu_to_le16(puserscanin->ssid_len);
441 memcpy(pssidtlv->ssid, puserscanin->ssid,
442 puserscanin->ssid_len);
443 ptlvpos += sizeof(pssidtlv->header) + puserscanin->ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200444 }
445
446 /*
447 * The default number of channels sent in the command is low to
448 * ensure the response buffer from the firmware does not truncate
449 * scan results. That is not an issue with an SSID or BSSID
450 * filter applied to the scan results in the firmware.
451 */
Dan Williamseb8f7332007-05-25 16:25:21 -0400452 if ( puserscanin->ssid_len
453 || (compare_ether_addr(pscancfgout->bssid, &zeromac[0]) != 0)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200454 *pmaxchanperscan = MRVDRV_MAX_CHANNELS_PER_SCAN;
455 *pfilteredscan = 1;
456 }
457 } else {
Holger Schurigd65ead82007-08-02 13:12:12 -0400458 pscancfgout->bsstype = CMD_BSS_TYPE_ANY;
Holger Schurige2aa3342007-08-02 13:05:53 -0400459 numprobes = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200460 }
461
462 /* If the input config or adapter has the number of Probes set, add tlv */
463 if (numprobes) {
464 pnumprobestlv = (struct mrvlietypes_numprobes *) ptlvpos;
David Woodhouse981f1872007-05-25 23:36:54 -0400465 pnumprobestlv->header.type = cpu_to_le16(TLV_TYPE_NUMPROBES);
466 pnumprobestlv->header.len = cpu_to_le16(2);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200467 pnumprobestlv->numprobes = cpu_to_le16(numprobes);
468
David Woodhouse981f1872007-05-25 23:36:54 -0400469 ptlvpos += sizeof(*pnumprobestlv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200470 }
471
472 /*
473 * Set the output for the channel TLV to the address in the tlv buffer
474 * past any TLVs that were added in this fuction (SSID, numprobes).
475 * channel TLVs will be added past this for each scan command, preserving
476 * the TLVs that were previously added.
477 */
478 *ppchantlvout = (struct mrvlietypes_chanlistparamset *) ptlvpos;
479
Dan Williams2afc0c52007-08-02 13:19:04 -0400480 if (!puserscanin || !puserscanin->chanlist[0].channumber) {
481 /* Create a default channel scan list */
Holger Schurig9012b282007-05-25 11:27:16 -0400482 lbs_deb_scan("Scan: Creating full region channel list\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200483 wlan_scan_create_channel_list(priv, pscanchanlist,
484 *pfilteredscan);
Dan Williams2afc0c52007-08-02 13:19:04 -0400485 goto out;
486 }
487
488 lbs_deb_scan("Scan: Using supplied channel list\n");
489 for (chanidx = 0;
490 chanidx < WLAN_IOCTL_USER_SCAN_CHAN_MAX
491 && puserscanin->chanlist[chanidx].channumber; chanidx++) {
492
493 channel = puserscanin->chanlist[chanidx].channumber;
494 (pscanchanlist + chanidx)->channumber = channel;
495
496 radiotype = puserscanin->chanlist[chanidx].radiotype;
497 (pscanchanlist + chanidx)->radiotype = radiotype;
498
499 scantype = puserscanin->chanlist[chanidx].scantype;
500
501 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
502 (pscanchanlist +
503 chanidx)->chanscanmode.passivescan = 1;
504 } else {
505 (pscanchanlist +
506 chanidx)->chanscanmode.passivescan = 0;
507 }
508
509 if (puserscanin->chanlist[chanidx].scantime) {
510 scandur = puserscanin->chanlist[chanidx].scantime;
511 } else {
512 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
513 scandur = MRVDRV_PASSIVE_SCAN_CHAN_TIME;
514 } else {
515 scandur = MRVDRV_ACTIVE_SCAN_CHAN_TIME;
516 }
517 }
518
519 (pscanchanlist + chanidx)->minscantime =
520 cpu_to_le16(scandur);
521 (pscanchanlist + chanidx)->maxscantime =
522 cpu_to_le16(scandur);
523 }
524
525 /* Check if we are only scanning the current channel */
526 if ((chanidx == 1) &&
527 (puserscanin->chanlist[0].channumber ==
528 priv->adapter->curbssparams.channel)) {
529 *pscancurrentonly = 1;
530 lbs_deb_scan("Scan: Scanning current channel only");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200531 }
532
533out:
534 return pscancfgout;
535}
536
537/**
538 * @brief Construct and send multiple scan config commands to the firmware
539 *
540 * Previous routines have created a wlan_scan_cmd_config with any requested
541 * TLVs. This function splits the channel TLV into maxchanperscan lists
542 * and sends the portion of the channel TLV along with the other TLVs
543 * to the wlan_cmd routines for execution in the firmware.
544 *
545 * @param priv A pointer to wlan_private structure
546 * @param maxchanperscan Maximum number channels to be included in each
547 * scan command sent to firmware
548 * @param filteredscan Flag indicating whether or not a BSSID or SSID
549 * filter is being used for the firmware command
550 * scan command sent to firmware
551 * @param pscancfgout Scan configuration used for this scan.
552 * @param pchantlvout Pointer in the pscancfgout where the channel TLV
553 * should start. This is past any other TLVs that
554 * must be sent down in each firmware command.
555 * @param pscanchanlist List of channels to scan in maxchanperscan segments
556 *
557 * @return 0 or error return otherwise
558 */
559static int wlan_scan_channel_list(wlan_private * priv,
560 int maxchanperscan,
561 u8 filteredscan,
562 struct wlan_scan_cmd_config * pscancfgout,
563 struct mrvlietypes_chanlistparamset * pchantlvout,
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400564 struct chanscanparamset * pscanchanlist,
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400565 const struct wlan_ioctl_user_scan_cfg * puserscanin,
566 int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200567{
568 struct chanscanparamset *ptmpchan;
569 struct chanscanparamset *pstartchan;
570 u8 scanband;
571 int doneearly;
572 int tlvidx;
573 int ret = 0;
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400574 int scanned = 0;
575 union iwreq_data wrqu;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200576
Holger Schurig9012b282007-05-25 11:27:16 -0400577 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200578
579 if (pscancfgout == 0 || pchantlvout == 0 || pscanchanlist == 0) {
Holger Schurig9012b282007-05-25 11:27:16 -0400580 lbs_deb_scan("Scan: Null detect: %p, %p, %p\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200581 pscancfgout, pchantlvout, pscanchanlist);
582 return -1;
583 }
584
585 pchantlvout->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
586
587 /* Set the temp channel struct pointer to the start of the desired list */
588 ptmpchan = pscanchanlist;
589
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400590 if (priv->adapter->last_scanned_channel && !puserscanin)
591 ptmpchan += priv->adapter->last_scanned_channel;
592
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200593 /* Loop through the desired channel list, sending a new firmware scan
594 * commands for each maxchanperscan channels (or for 1,6,11 individually
595 * if configured accordingly)
596 */
597 while (ptmpchan->channumber) {
598
599 tlvidx = 0;
600 pchantlvout->header.len = 0;
601 scanband = ptmpchan->radiotype;
602 pstartchan = ptmpchan;
603 doneearly = 0;
604
605 /* Construct the channel TLV for the scan command. Continue to
606 * insert channel TLVs until:
607 * - the tlvidx hits the maximum configured per scan command
608 * - the next channel to insert is 0 (end of desired channel list)
609 * - doneearly is set (controlling individual scanning of 1,6,11)
610 */
611 while (tlvidx < maxchanperscan && ptmpchan->channumber
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400612 && !doneearly && scanned < 2) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200613
Dan Williams2afc0c52007-08-02 13:19:04 -0400614 lbs_deb_scan("Scan: Chan(%3d), Radio(%d), mode(%d,%d), "
615 "Dur(%d)\n",
616 ptmpchan->channumber, ptmpchan->radiotype,
617 ptmpchan->chanscanmode.passivescan,
618 ptmpchan->chanscanmode.disablechanfilt,
619 ptmpchan->maxscantime);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200620
621 /* Copy the current channel TLV to the command being prepared */
622 memcpy(pchantlvout->chanscanparam + tlvidx,
623 ptmpchan, sizeof(pchantlvout->chanscanparam));
624
625 /* Increment the TLV header length by the size appended */
David Woodhouse981f1872007-05-25 23:36:54 -0400626 /* Ew, it would be _so_ nice if we could just declare the
627 variable little-endian and let GCC handle it for us */
628 pchantlvout->header.len =
629 cpu_to_le16(le16_to_cpu(pchantlvout->header.len) +
630 sizeof(pchantlvout->chanscanparam));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200631
632 /*
633 * The tlv buffer length is set to the number of bytes of the
634 * between the channel tlv pointer and the start of the
635 * tlv buffer. This compensates for any TLVs that were appended
636 * before the channel list.
637 */
638 pscancfgout->tlvbufferlen = ((u8 *) pchantlvout
639 - pscancfgout->tlvbuffer);
640
641 /* Add the size of the channel tlv header and the data length */
642 pscancfgout->tlvbufferlen +=
643 (sizeof(pchantlvout->header)
David Woodhouse981f1872007-05-25 23:36:54 -0400644 + le16_to_cpu(pchantlvout->header.len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200645
646 /* Increment the index to the channel tlv we are constructing */
647 tlvidx++;
648
649 doneearly = 0;
650
651 /* Stop the loop if the *current* channel is in the 1,6,11 set
652 * and we are not filtering on a BSSID or SSID.
653 */
654 if (!filteredscan && (ptmpchan->channumber == 1
655 || ptmpchan->channumber == 6
656 || ptmpchan->channumber == 11)) {
657 doneearly = 1;
658 }
659
660 /* Increment the tmp pointer to the next channel to be scanned */
661 ptmpchan++;
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400662 scanned++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200663
664 /* Stop the loop if the *next* channel is in the 1,6,11 set.
665 * This will cause it to be the only channel scanned on the next
666 * interation
667 */
668 if (!filteredscan && (ptmpchan->channumber == 1
669 || ptmpchan->channumber == 6
670 || ptmpchan->channumber == 11)) {
671 doneearly = 1;
672 }
673 }
674
675 /* Send the scan command to the firmware with the specified cfg */
Dan Williams0aef64d2007-08-02 11:31:18 -0400676 ret = libertas_prepare_and_send_command(priv, CMD_802_11_SCAN, 0,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200677 0, 0, pscancfgout);
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400678 if (scanned >= 2 && !full_scan) {
Holger Schurig9012b282007-05-25 11:27:16 -0400679 ret = 0;
680 goto done;
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400681 }
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400682 scanned = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200683 }
684
Dan Williamsd9ad2f52007-05-25 22:38:41 -0400685done:
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400686 priv->adapter->last_scanned_channel = ptmpchan->channumber;
687
Dan Williams2afc0c52007-08-02 13:19:04 -0400688 if (priv->adapter->last_scanned_channel) {
689 /* Schedule the next part of the partial scan */
690 if (!full_scan && !priv->adapter->surpriseremoved) {
691 cancel_delayed_work(&priv->scan_work);
692 queue_delayed_work(priv->work_thread, &priv->scan_work,
693 msecs_to_jiffies(300));
694 }
695 } else {
696 /* All done, tell userspace the scan table has been updated */
697 memset(&wrqu, 0, sizeof(union iwreq_data));
698 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
699 }
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400700
Holger Schurig9012b282007-05-25 11:27:16 -0400701 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200702 return ret;
703}
704
Dan Williamseb8f7332007-05-25 16:25:21 -0400705static void
706clear_selected_scan_list_entries(wlan_adapter * adapter,
707 const struct wlan_ioctl_user_scan_cfg * scan_cfg)
708{
709 struct bss_descriptor * bss;
710 struct bss_descriptor * safe;
711 u32 clear_ssid_flag = 0, clear_bssid_flag = 0;
712
713 if (!scan_cfg)
714 return;
715
716 if (scan_cfg->clear_ssid && scan_cfg->ssid_len)
717 clear_ssid_flag = 1;
718
719 if (scan_cfg->clear_bssid
720 && (compare_ether_addr(scan_cfg->bssid, &zeromac[0]) != 0)
721 && (compare_ether_addr(scan_cfg->bssid, &bcastmac[0]) != 0)) {
722 clear_bssid_flag = 1;
723 }
724
725 if (!clear_ssid_flag && !clear_bssid_flag)
726 return;
727
728 mutex_lock(&adapter->lock);
729 list_for_each_entry_safe (bss, safe, &adapter->network_list, list) {
730 u32 clear = 0;
731
732 /* Check for an SSID match */
733 if ( clear_ssid_flag
Dan Williamsd8efea22007-05-28 23:54:55 -0400734 && (bss->ssid_len == scan_cfg->ssid_len)
735 && !memcmp(bss->ssid, scan_cfg->ssid, bss->ssid_len))
Dan Williamseb8f7332007-05-25 16:25:21 -0400736 clear = 1;
737
738 /* Check for a BSSID match */
739 if ( clear_bssid_flag
740 && !compare_ether_addr(bss->bssid, scan_cfg->bssid))
741 clear = 1;
742
743 if (clear) {
744 list_move_tail (&bss->list, &adapter->network_free_list);
745 clear_bss_descriptor(bss);
746 }
747 }
748 mutex_unlock(&adapter->lock);
749}
750
751
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200752/**
753 * @brief Internal function used to start a scan based on an input config
754 *
755 * Use the input user scan configuration information when provided in
756 * order to send the appropriate scan commands to firmware to populate or
757 * update the internal driver scan table
758 *
759 * @param priv A pointer to wlan_private structure
760 * @param puserscanin Pointer to the input configuration for the requested
761 * scan.
762 *
763 * @return 0 or < 0 if error
764 */
765int wlan_scan_networks(wlan_private * priv,
Dan Williams2afc0c52007-08-02 13:19:04 -0400766 const struct wlan_ioctl_user_scan_cfg * puserscanin,
767 int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200768{
Dan Williamseb8f7332007-05-25 16:25:21 -0400769 wlan_adapter * adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200770 struct mrvlietypes_chanlistparamset *pchantlvout;
771 struct chanscanparamset * scan_chan_list = NULL;
772 struct wlan_scan_cmd_config * scan_cfg = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200773 u8 filteredscan;
774 u8 scancurrentchanonly;
775 int maxchanperscan;
776 int ret;
Dan Williamsf8f55102007-05-30 10:12:55 -0400777#ifdef CONFIG_LIBERTAS_DEBUG
778 struct bss_descriptor * iter_bss;
779 int i = 0;
780#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200781
Dan Williams2afc0c52007-08-02 13:19:04 -0400782 lbs_deb_enter(LBS_DEB_SCAN);
783
784 /* Cancel any partial outstanding partial scans if this scan
785 * is a full scan.
786 */
787 if (full_scan && delayed_work_pending(&priv->scan_work))
788 cancel_delayed_work(&priv->scan_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200789
790 scan_chan_list = kzalloc(sizeof(struct chanscanparamset) *
791 WLAN_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
792 if (scan_chan_list == NULL) {
793 ret = -ENOMEM;
794 goto out;
795 }
796
797 scan_cfg = wlan_scan_setup_scan_config(priv,
798 puserscanin,
799 &pchantlvout,
800 scan_chan_list,
801 &maxchanperscan,
802 &filteredscan,
803 &scancurrentchanonly);
804 if (scan_cfg == NULL) {
805 ret = -ENOMEM;
806 goto out;
807 }
808
Dan Williamseb8f7332007-05-25 16:25:21 -0400809 clear_selected_scan_list_entries(adapter, puserscanin);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200810
811 /* Keep the data path active if we are only scanning our current channel */
812 if (!scancurrentchanonly) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400813 netif_stop_queue(priv->dev);
814 netif_carrier_off(priv->dev);
Holger Schurig3cf84092007-08-02 11:50:12 -0400815 if (priv->mesh_dev) {
816 netif_stop_queue(priv->mesh_dev);
817 netif_carrier_off(priv->mesh_dev);
818 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200819 }
820
821 ret = wlan_scan_channel_list(priv,
822 maxchanperscan,
823 filteredscan,
824 scan_cfg,
825 pchantlvout,
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400826 scan_chan_list,
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400827 puserscanin,
828 full_scan);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200829
Dan Williamsf8f55102007-05-30 10:12:55 -0400830#ifdef CONFIG_LIBERTAS_DEBUG
831 /* Dump the scan table */
832 mutex_lock(&adapter->lock);
833 list_for_each_entry (iter_bss, &adapter->network_list, list) {
834 lbs_deb_scan("Scan:(%02d) " MAC_FMT ", RSSI[%03d], SSID[%s]\n",
835 i++, MAC_ARG(iter_bss->bssid), (s32) iter_bss->rssi,
836 escape_essid(iter_bss->ssid, iter_bss->ssid_len));
837 }
838 mutex_unlock(&adapter->lock);
839#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200840
Dan Williams0aef64d2007-08-02 11:31:18 -0400841 if (priv->adapter->connect_status == LIBERTAS_CONNECTED) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400842 netif_carrier_on(priv->dev);
843 netif_wake_queue(priv->dev);
Holger Schurig3cf84092007-08-02 11:50:12 -0400844 if (priv->mesh_dev) {
845 netif_carrier_on(priv->mesh_dev);
846 netif_wake_queue(priv->mesh_dev);
847 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200848 }
849
850out:
851 if (scan_cfg)
852 kfree(scan_cfg);
853
854 if (scan_chan_list)
855 kfree(scan_chan_list);
856
Holger Schurig9012b282007-05-25 11:27:16 -0400857 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200858 return ret;
859}
860
861/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200862 * @brief Interpret a BSS scan response returned from the firmware
863 *
864 * Parse the various fixed fields and IEs passed back for a a BSS probe
865 * response or beacon from the scan command. Record information as needed
866 * in the scan table struct bss_descriptor for that entry.
867 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400868 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200869 *
870 * @return 0 or -1
871 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400872static int libertas_process_bss(struct bss_descriptor * bss,
873 u8 ** pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200874{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200875 struct ieeetypes_fhparamset *pFH;
876 struct ieeetypes_dsparamset *pDS;
877 struct ieeetypes_cfparamset *pCF;
878 struct ieeetypes_ibssparamset *pibss;
Dan Williams8c512762007-08-02 11:40:45 -0400879 struct ieeetypes_countryinfoset *pcountryinfo;
880 u8 *pos, *end, *p;
881 u8 n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
882 u16 beaconsize = 0;
Holger Schurig9012b282007-05-25 11:27:16 -0400883 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200884
Holger Schurig9012b282007-05-25 11:27:16 -0400885 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200886
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200887 if (*bytesleft >= sizeof(beaconsize)) {
888 /* Extract & convert beacon size from the command buffer */
David Woodhouse981f1872007-05-25 23:36:54 -0400889 beaconsize = le16_to_cpup((void *)*pbeaconinfo);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200890 *bytesleft -= sizeof(beaconsize);
891 *pbeaconinfo += sizeof(beaconsize);
892 }
893
894 if (beaconsize == 0 || beaconsize > *bytesleft) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200895 *pbeaconinfo += *bytesleft;
896 *bytesleft = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200897 return -1;
898 }
899
900 /* Initialize the current working beacon pointer for this BSS iteration */
Dan Williamsab617972007-08-02 10:48:02 -0400901 pos = *pbeaconinfo;
902 end = pos + beaconsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200903
904 /* Advance the return beacon pointer past the current beacon */
905 *pbeaconinfo += beaconsize;
906 *bytesleft -= beaconsize;
907
Dan Williamsab617972007-08-02 10:48:02 -0400908 memcpy(bss->bssid, pos, ETH_ALEN);
Dan Williams02eb2292007-05-25 17:27:31 -0400909 lbs_deb_scan("process_bss: AP BSSID " MAC_FMT "\n", MAC_ARG(bss->bssid));
Dan Williamsab617972007-08-02 10:48:02 -0400910 pos += ETH_ALEN;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200911
Dan Williamsab617972007-08-02 10:48:02 -0400912 if ((end - pos) < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400913 lbs_deb_scan("process_bss: Not enough bytes left\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200914 return -1;
915 }
916
917 /*
918 * next 4 fields are RSSI, time stamp, beacon interval,
919 * and capability information
920 */
921
922 /* RSSI is 1 byte long */
Dan Williamsab617972007-08-02 10:48:02 -0400923 bss->rssi = *pos;
924 lbs_deb_scan("process_bss: RSSI=%02X\n", *pos);
925 pos++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200926
927 /* time stamp is 8 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400928 pos += 8;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200929
930 /* beacon interval is 2 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400931 bss->beaconperiod = le16_to_cpup((void *) pos);
932 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200933
934 /* capability information is 2 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400935 bss->capability = le16_to_cpup((void *) pos);
Dan Williams0c9ca692007-08-02 10:43:44 -0400936 lbs_deb_scan("process_bss: capabilities = 0x%4X\n", bss->capability);
Dan Williamsab617972007-08-02 10:48:02 -0400937 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200938
Dan Williams0c9ca692007-08-02 10:43:44 -0400939 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
940 lbs_deb_scan("process_bss: AP WEP enabled\n");
941 if (bss->capability & WLAN_CAPABILITY_IBSS)
942 bss->mode = IW_MODE_ADHOC;
943 else
944 bss->mode = IW_MODE_INFRA;
945
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200946 /* rest of the current buffer are IE's */
Dan Williamsab617972007-08-02 10:48:02 -0400947 lbs_deb_scan("process_bss: IE length for this AP = %zd\n", end - pos);
Holger Schurigece56192007-08-02 11:53:06 -0400948 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200949
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200950 /* process variable IE */
Dan Williamsab617972007-08-02 10:48:02 -0400951 while (pos <= end - 2) {
952 struct ieee80211_info_element * elem =
953 (struct ieee80211_info_element *) pos;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200954
Dan Williamsab617972007-08-02 10:48:02 -0400955 if (pos + elem->len > end) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400956 lbs_deb_scan("process_bss: error in processing IE, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200957 "bytes left < IE length\n");
Dan Williamsab617972007-08-02 10:48:02 -0400958 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200959 }
960
Dan Williamsab617972007-08-02 10:48:02 -0400961 switch (elem->id) {
962 case MFIE_TYPE_SSID:
963 bss->ssid_len = elem->len;
964 memcpy(bss->ssid, elem->data, elem->len);
Dan Williamsd8efea22007-05-28 23:54:55 -0400965 lbs_deb_scan("ssid '%s', ssid length %u\n",
966 escape_essid(bss->ssid, bss->ssid_len),
967 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200968 break;
969
Dan Williamsab617972007-08-02 10:48:02 -0400970 case MFIE_TYPE_RATES:
Dan Williams8c512762007-08-02 11:40:45 -0400971 n_basic_rates = min_t(u8, MAX_RATES, elem->len);
972 memcpy(bss->rates, elem->data, n_basic_rates);
973 got_basic_rates = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200974 break;
975
Dan Williamsab617972007-08-02 10:48:02 -0400976 case MFIE_TYPE_FH_SET:
977 pFH = (struct ieeetypes_fhparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400978 memmove(&bss->phyparamset.fhparamset, pFH,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200979 sizeof(struct ieeetypes_fhparamset));
David Woodhouse981f1872007-05-25 23:36:54 -0400980#if 0 /* I think we can store these LE */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400981 bss->phyparamset.fhparamset.dwelltime
982 = le16_to_cpu(bss->phyparamset.fhparamset.dwelltime);
David Woodhouse981f1872007-05-25 23:36:54 -0400983#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200984 break;
985
Dan Williamsab617972007-08-02 10:48:02 -0400986 case MFIE_TYPE_DS_SET:
987 pDS = (struct ieeetypes_dsparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400988 bss->channel = pDS->currentchan;
989 memcpy(&bss->phyparamset.dsparamset, pDS,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200990 sizeof(struct ieeetypes_dsparamset));
991 break;
992
Dan Williamsab617972007-08-02 10:48:02 -0400993 case MFIE_TYPE_CF_SET:
994 pCF = (struct ieeetypes_cfparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400995 memcpy(&bss->ssparamset.cfparamset, pCF,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200996 sizeof(struct ieeetypes_cfparamset));
997 break;
998
Dan Williamsab617972007-08-02 10:48:02 -0400999 case MFIE_TYPE_IBSS_SET:
1000 pibss = (struct ieeetypes_ibssparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001001 bss->atimwindow = le32_to_cpu(pibss->atimwindow);
1002 memmove(&bss->ssparamset.ibssparamset, pibss,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001003 sizeof(struct ieeetypes_ibssparamset));
David Woodhouse981f1872007-05-25 23:36:54 -04001004#if 0
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001005 bss->ssparamset.ibssparamset.atimwindow
1006 = le16_to_cpu(bss->ssparamset.ibssparamset.atimwindow);
David Woodhouse981f1872007-05-25 23:36:54 -04001007#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001008 break;
1009
Dan Williamsab617972007-08-02 10:48:02 -04001010 case MFIE_TYPE_COUNTRY:
1011 pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001012 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001013 || pcountryinfo->len > 254) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001014 lbs_deb_scan("process_bss: 11D- Err "
Dan Williams4269e2a2007-05-10 23:10:18 -04001015 "CountryInfo len =%d min=%zd max=254\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001016 pcountryinfo->len,
1017 sizeof(pcountryinfo->countrycode));
Holger Schurig9012b282007-05-25 11:27:16 -04001018 ret = -1;
1019 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001020 }
1021
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001022 memcpy(&bss->countryinfo,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001023 pcountryinfo, pcountryinfo->len + 2);
Holger Schurigece56192007-08-02 11:53:06 -04001024 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001025 (u8 *) pcountryinfo,
1026 (u32) (pcountryinfo->len + 2));
1027 break;
1028
Dan Williamsab617972007-08-02 10:48:02 -04001029 case MFIE_TYPE_RATES_EX:
1030 /* only process extended supported rate if data rate is
1031 * already found. Data rate IE should come before
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001032 * extended supported rate IE
1033 */
Dan Williams8c512762007-08-02 11:40:45 -04001034 if (!got_basic_rates)
Dan Williamsab617972007-08-02 10:48:02 -04001035 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001036
Dan Williams8c512762007-08-02 11:40:45 -04001037 n_ex_rates = elem->len;
1038 if (n_basic_rates + n_ex_rates > MAX_RATES)
1039 n_ex_rates = MAX_RATES - n_basic_rates;
Dan Williamsab617972007-08-02 10:48:02 -04001040
Dan Williams8c512762007-08-02 11:40:45 -04001041 p = bss->rates + n_basic_rates;
1042 memcpy(p, elem->data, n_ex_rates);
Dan Williamsab617972007-08-02 10:48:02 -04001043 break;
1044
1045 case MFIE_TYPE_GENERIC:
1046 if (elem->len >= 4 &&
1047 elem->data[0] == 0x00 &&
1048 elem->data[1] == 0x50 &&
1049 elem->data[2] == 0xf2 &&
1050 elem->data[3] == 0x01) {
1051 bss->wpa_ie_len = min(elem->len + 2,
1052 MAX_WPA_IE_LEN);
1053 memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
Holger Schurigece56192007-08-02 11:53:06 -04001054 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: WPA IE", bss->wpa_ie,
Dan Williamsab617972007-08-02 10:48:02 -04001055 elem->len);
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001056 } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
1057 elem->data[0] == 0x00 &&
1058 elem->data[1] == 0x50 &&
1059 elem->data[2] == 0x43 &&
1060 elem->data[3] == 0x04) {
1061 bss->mesh = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001062 }
1063 break;
1064
Dan Williamsab617972007-08-02 10:48:02 -04001065 case MFIE_TYPE_RSN:
1066 bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
1067 memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
Holger Schurigece56192007-08-02 11:53:06 -04001068 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE", bss->rsn_ie, elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001069 break;
1070
Dan Williamsab617972007-08-02 10:48:02 -04001071 default:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001072 break;
1073 }
1074
Dan Williamsab617972007-08-02 10:48:02 -04001075 pos += elem->len + 2;
1076 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001077
1078 /* Timestamp */
1079 bss->last_scanned = jiffies;
Dan Williams8c512762007-08-02 11:40:45 -04001080 libertas_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001081
Holger Schurig9012b282007-05-25 11:27:16 -04001082 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001083
Holger Schurig9012b282007-05-25 11:27:16 -04001084done:
1085 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1086 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001087}
1088
1089/**
1090 * @brief Compare two SSIDs
1091 *
1092 * @param ssid1 A pointer to ssid to compare
1093 * @param ssid2 A pointer to ssid to compare
1094 *
1095 * @return 0--ssid is same, otherwise is different
1096 */
Dan Williams717c9332007-05-29 00:03:31 -04001097int libertas_ssid_cmp(u8 *ssid1, u8 ssid1_len, u8 *ssid2, u8 ssid2_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001098{
Dan Williamsd8efea22007-05-28 23:54:55 -04001099 if (ssid1_len != ssid2_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001100 return -1;
1101
Dan Williamsd8efea22007-05-28 23:54:55 -04001102 return memcmp(ssid1, ssid2, ssid1_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001103}
1104
1105/**
1106 * @brief This function finds a specific compatible BSSID in the scan list
1107 *
1108 * @param adapter A pointer to wlan_adapter
1109 * @param bssid BSSID to find in the scan list
1110 * @param mode Network mode: Infrastructure or IBSS
1111 *
1112 * @return index in BSSID list, or error return code (< 0)
1113 */
Dan Williams717c9332007-05-29 00:03:31 -04001114struct bss_descriptor * libertas_find_bssid_in_list(wlan_adapter * adapter,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001115 u8 * bssid, u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001116{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001117 struct bss_descriptor * iter_bss;
1118 struct bss_descriptor * found_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001119
1120 if (!bssid)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001121 return NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001122
Holger Schurigece56192007-08-02 11:53:06 -04001123 lbs_deb_hex(LBS_DEB_SCAN, "looking for",
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001124 bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001125
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001126 /* Look through the scan table for a compatible match. The loop will
1127 * continue past a matched bssid that is not compatible in case there
1128 * is an AP with multiple SSIDs assigned to the same BSSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001129 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001130 mutex_lock(&adapter->lock);
1131 list_for_each_entry (iter_bss, &adapter->network_list, list) {
Dan Williams3cf20932007-05-25 17:28:30 -04001132 if (compare_ether_addr(iter_bss->bssid, bssid))
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001133 continue; /* bssid doesn't match */
1134 switch (mode) {
1135 case IW_MODE_INFRA:
1136 case IW_MODE_ADHOC:
1137 if (!is_network_compatible(adapter, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001138 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001139 found_bss = iter_bss;
1140 break;
1141 default:
1142 found_bss = iter_bss;
1143 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001144 }
1145 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001146 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001147
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001148 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001149}
1150
1151/**
1152 * @brief This function finds ssid in ssid list.
1153 *
1154 * @param adapter A pointer to wlan_adapter
1155 * @param ssid SSID to find in the list
1156 * @param bssid BSSID to qualify the SSID selection (if provided)
1157 * @param mode Network mode: Infrastructure or IBSS
1158 *
1159 * @return index in BSSID list
1160 */
Dan Williams717c9332007-05-29 00:03:31 -04001161struct bss_descriptor * libertas_find_ssid_in_list(wlan_adapter * adapter,
Dan Williamsd8efea22007-05-28 23:54:55 -04001162 u8 *ssid, u8 ssid_len, u8 * bssid, u8 mode,
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001163 int channel)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001164{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001165 u8 bestrssi = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001166 struct bss_descriptor * iter_bss = NULL;
1167 struct bss_descriptor * found_bss = NULL;
1168 struct bss_descriptor * tmp_oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001169
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001170 mutex_lock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001171
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001172 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1173 if ( !tmp_oldest
1174 || (iter_bss->last_scanned < tmp_oldest->last_scanned))
1175 tmp_oldest = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001176
Dan Williams717c9332007-05-29 00:03:31 -04001177 if (libertas_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001178 ssid, ssid_len) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001179 continue; /* ssid doesn't match */
Dan Williams3cf20932007-05-25 17:28:30 -04001180 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001181 continue; /* bssid doesn't match */
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001182 if ((channel > 0) && (iter_bss->channel != channel))
1183 continue; /* channel doesn't match */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001184
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001185 switch (mode) {
1186 case IW_MODE_INFRA:
1187 case IW_MODE_ADHOC:
1188 if (!is_network_compatible(adapter, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001189 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001190
1191 if (bssid) {
1192 /* Found requested BSSID */
1193 found_bss = iter_bss;
1194 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001195 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001196
1197 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1198 bestrssi = SCAN_RSSI(iter_bss->rssi);
1199 found_bss = iter_bss;
1200 }
1201 break;
1202 case IW_MODE_AUTO:
1203 default:
1204 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1205 bestrssi = SCAN_RSSI(iter_bss->rssi);
1206 found_bss = iter_bss;
1207 }
1208 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001209 }
1210 }
1211
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001212out:
1213 mutex_unlock(&adapter->lock);
1214 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001215}
1216
1217/**
1218 * @brief This function finds the best SSID in the Scan List
1219 *
1220 * Search the scan table for the best SSID that also matches the current
1221 * adapter network preference (infrastructure or adhoc)
1222 *
1223 * @param adapter A pointer to wlan_adapter
1224 *
1225 * @return index in BSSID list
1226 */
Holger Schurigac558ca2007-08-02 11:49:06 -04001227static struct bss_descriptor * libertas_find_best_ssid_in_list(wlan_adapter * adapter,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001228 u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001229{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001230 u8 bestrssi = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001231 struct bss_descriptor * iter_bss;
1232 struct bss_descriptor * best_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001233
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001234 mutex_lock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001235
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001236 list_for_each_entry (iter_bss, &adapter->network_list, list) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001237 switch (mode) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001238 case IW_MODE_INFRA:
1239 case IW_MODE_ADHOC:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001240 if (!is_network_compatible(adapter, iter_bss, mode))
1241 break;
1242 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1243 break;
1244 bestrssi = SCAN_RSSI(iter_bss->rssi);
1245 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001246 break;
Dan Williams0dc5a292007-05-10 22:58:02 -04001247 case IW_MODE_AUTO:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001248 default:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001249 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1250 break;
1251 bestrssi = SCAN_RSSI(iter_bss->rssi);
1252 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001253 break;
1254 }
1255 }
1256
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001257 mutex_unlock(&adapter->lock);
1258 return best_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001259}
1260
1261/**
1262 * @brief Find the AP with specific ssid in the scan list
1263 *
1264 * @param priv A pointer to wlan_private structure
1265 * @param pSSID A pointer to AP's ssid
1266 *
1267 * @return 0--success, otherwise--fail
1268 */
Dan Williams717c9332007-05-29 00:03:31 -04001269int libertas_find_best_network_ssid(wlan_private * priv,
Dan Williamsd8efea22007-05-28 23:54:55 -04001270 u8 *out_ssid, u8 *out_ssid_len, u8 preferred_mode, u8 *out_mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001271{
1272 wlan_adapter *adapter = priv->adapter;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001273 int ret = -1;
1274 struct bss_descriptor * found;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001275
Holger Schurig9012b282007-05-25 11:27:16 -04001276 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001277
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001278 wlan_scan_networks(priv, NULL, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001279 if (adapter->surpriseremoved)
1280 return -1;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001281
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001282 wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1283
Dan Williams717c9332007-05-29 00:03:31 -04001284 found = libertas_find_best_ssid_in_list(adapter, preferred_mode);
Dan Williamsd8efea22007-05-28 23:54:55 -04001285 if (found && (found->ssid_len > 0)) {
1286 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1287 *out_ssid_len = found->ssid_len;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001288 *out_mode = found->mode;
1289 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001290 }
1291
Holger Schurig9012b282007-05-25 11:27:16 -04001292 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001293 return ret;
1294}
1295
1296/**
1297 * @brief Scan Network
1298 *
1299 * @param dev A pointer to net_device structure
1300 * @param info A pointer to iw_request_info structure
1301 * @param vwrq A pointer to iw_param structure
1302 * @param extra A pointer to extra data buf
1303 *
1304 * @return 0 --success, otherwise fail
1305 */
1306int libertas_set_scan(struct net_device *dev, struct iw_request_info *info,
1307 struct iw_param *vwrq, char *extra)
1308{
1309 wlan_private *priv = dev->priv;
1310 wlan_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001311
Holger Schurig9012b282007-05-25 11:27:16 -04001312 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001313
Dan Williams2afc0c52007-08-02 13:19:04 -04001314 if (!delayed_work_pending(&priv->scan_work)) {
1315 queue_delayed_work(priv->work_thread, &priv->scan_work,
1316 msecs_to_jiffies(50));
1317 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001318
1319 if (adapter->surpriseremoved)
1320 return -1;
1321
Holger Schurig9012b282007-05-25 11:27:16 -04001322 lbs_deb_leave(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001323 return 0;
1324}
1325
1326/**
1327 * @brief Send a scan command for all available channels filtered on a spec
1328 *
1329 * @param priv A pointer to wlan_private structure
1330 * @param prequestedssid A pointer to AP's ssid
1331 * @param keeppreviousscan Flag used to save/clear scan table before scan
1332 *
1333 * @return 0-success, otherwise fail
1334 */
Dan Williams717c9332007-05-29 00:03:31 -04001335int libertas_send_specific_ssid_scan(wlan_private * priv,
Dan Williamsd8efea22007-05-28 23:54:55 -04001336 u8 *ssid, u8 ssid_len, u8 clear_ssid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001337{
1338 wlan_adapter *adapter = priv->adapter;
1339 struct wlan_ioctl_user_scan_cfg scancfg;
Dan Williamseb8f7332007-05-25 16:25:21 -04001340 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001341
Holger Schurig9012b282007-05-25 11:27:16 -04001342 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001343
Dan Williamsd8efea22007-05-28 23:54:55 -04001344 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -04001345 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001346
1347 memset(&scancfg, 0x00, sizeof(scancfg));
Dan Williamsd8efea22007-05-28 23:54:55 -04001348 memcpy(scancfg.ssid, ssid, ssid_len);
1349 scancfg.ssid_len = ssid_len;
Dan Williamseb8f7332007-05-25 16:25:21 -04001350 scancfg.clear_ssid = clear_ssid;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001351
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001352 wlan_scan_networks(priv, &scancfg, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001353 if (adapter->surpriseremoved)
1354 return -1;
1355 wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1356
Dan Williamseb8f7332007-05-25 16:25:21 -04001357out:
Holger Schurig9012b282007-05-25 11:27:16 -04001358 lbs_deb_leave(LBS_DEB_ASSOC);
Dan Williamseb8f7332007-05-25 16:25:21 -04001359 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001360}
1361
Dan Williams00af0152007-08-02 13:14:56 -04001362#define MAX_CUSTOM_LEN 64
1363
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001364static inline char *libertas_translate_scan(wlan_private *priv,
1365 char *start, char *stop,
1366 struct bss_descriptor *bss)
1367{
1368 wlan_adapter *adapter = priv->adapter;
1369 struct chan_freq_power *cfp;
1370 char *current_val; /* For rates */
1371 struct iw_event iwe; /* Temporary buffer */
1372 int j;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001373#define PERFECT_RSSI ((u8)50)
1374#define WORST_RSSI ((u8)0)
1375#define RSSI_DIFF ((u8)(PERFECT_RSSI - WORST_RSSI))
1376 u8 rssi;
1377
1378 cfp = libertas_find_cfp_by_band_and_channel(adapter, 0, bss->channel);
1379 if (!cfp) {
1380 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
1381 return NULL;
1382 }
1383
1384 /* First entry *MUST* be the AP BSSID */
1385 iwe.cmd = SIOCGIWAP;
1386 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1387 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
1388 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
1389
1390 /* SSID */
1391 iwe.cmd = SIOCGIWESSID;
1392 iwe.u.data.flags = 1;
Dan Williamsd8efea22007-05-28 23:54:55 -04001393 iwe.u.data.length = min((u32) bss->ssid_len, (u32) IW_ESSID_MAX_SIZE);
1394 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001395
1396 /* Mode */
1397 iwe.cmd = SIOCGIWMODE;
1398 iwe.u.mode = bss->mode;
1399 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
1400
1401 /* Frequency */
1402 iwe.cmd = SIOCGIWFREQ;
1403 iwe.u.freq.m = (long)cfp->freq * 100000;
1404 iwe.u.freq.e = 1;
1405 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
1406
1407 /* Add quality statistics */
1408 iwe.cmd = IWEVQUAL;
1409 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
1410 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
1411
1412 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
1413 iwe.u.qual.qual =
1414 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
1415 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
1416 (RSSI_DIFF * RSSI_DIFF);
1417 if (iwe.u.qual.qual > 100)
1418 iwe.u.qual.qual = 100;
1419
1420 if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
1421 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
1422 } else {
1423 iwe.u.qual.noise =
1424 CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
1425 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001426
Dan Williams80e78ef2007-05-25 22:18:47 -04001427 /* Locally created ad-hoc BSSs won't have beacons if this is the
1428 * only station in the adhoc network; so get signal strength
1429 * from receive statistics.
1430 */
1431 if ((adapter->mode == IW_MODE_ADHOC)
1432 && adapter->adhoccreate
Dan Williams717c9332007-05-29 00:03:31 -04001433 && !libertas_ssid_cmp(adapter->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001434 adapter->curbssparams.ssid_len,
1435 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -04001436 int snr, nf;
1437 snr = adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1438 nf = adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1439 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001440 }
1441 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
1442
1443 /* Add encryption capability */
1444 iwe.cmd = SIOCGIWENCODE;
Dan Williams0c9ca692007-08-02 10:43:44 -04001445 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001446 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1447 } else {
1448 iwe.u.data.flags = IW_ENCODE_DISABLED;
1449 }
1450 iwe.u.data.length = 0;
Dan Williamsd8efea22007-05-28 23:54:55 -04001451 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001452
1453 current_val = start + IW_EV_LCP_LEN;
1454
1455 iwe.cmd = SIOCGIWRATE;
1456 iwe.u.bitrate.fixed = 0;
1457 iwe.u.bitrate.disabled = 0;
1458 iwe.u.bitrate.value = 0;
1459
Dan Williams8c512762007-08-02 11:40:45 -04001460 for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
1461 /* Bit rate given in 500 kb/s units */
1462 iwe.u.bitrate.value = bss->rates[j] * 500000;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001463 current_val = iwe_stream_add_value(start, current_val,
1464 stop, &iwe, IW_EV_PARAM_LEN);
1465 }
1466 if ((bss->mode == IW_MODE_ADHOC)
Dan Williams717c9332007-05-29 00:03:31 -04001467 && !libertas_ssid_cmp(adapter->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001468 adapter->curbssparams.ssid_len,
1469 bss->ssid, bss->ssid_len)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001470 && adapter->adhoccreate) {
1471 iwe.u.bitrate.value = 22 * 500000;
1472 current_val = iwe_stream_add_value(start, current_val,
1473 stop, &iwe, IW_EV_PARAM_LEN);
1474 }
1475 /* Check if we added any event */
1476 if((current_val - start) > IW_EV_LCP_LEN)
1477 start = current_val;
1478
1479 memset(&iwe, 0, sizeof(iwe));
1480 if (bss->wpa_ie_len) {
1481 char buf[MAX_WPA_IE_LEN];
1482 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
1483 iwe.cmd = IWEVGENIE;
1484 iwe.u.data.length = bss->wpa_ie_len;
1485 start = iwe_stream_add_point(start, stop, &iwe, buf);
1486 }
1487
1488 memset(&iwe, 0, sizeof(iwe));
1489 if (bss->rsn_ie_len) {
1490 char buf[MAX_WPA_IE_LEN];
1491 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
1492 iwe.cmd = IWEVGENIE;
1493 iwe.u.data.length = bss->rsn_ie_len;
1494 start = iwe_stream_add_point(start, stop, &iwe, buf);
1495 }
1496
Dan Williams00af0152007-08-02 13:14:56 -04001497 if (bss->mesh) {
1498 char custom[MAX_CUSTOM_LEN];
1499 char *p = custom;
1500
1501 iwe.cmd = IWEVCUSTOM;
1502 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
1503 "mesh-type: olpc");
1504 iwe.u.data.length = p - custom;
1505 if (iwe.u.data.length)
1506 start = iwe_stream_add_point(start, stop, &iwe, custom);
1507 }
1508
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001509 return start;
1510}
1511
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001512/**
1513 * @brief Retrieve the scan table entries via wireless tools IOCTL call
1514 *
1515 * @param dev A pointer to net_device structure
1516 * @param info A pointer to iw_request_info structure
1517 * @param dwrq A pointer to iw_point structure
1518 * @param extra A pointer to extra data buf
1519 *
1520 * @return 0 --success, otherwise fail
1521 */
1522int libertas_get_scan(struct net_device *dev, struct iw_request_info *info,
1523 struct iw_point *dwrq, char *extra)
1524{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001525#define SCAN_ITEM_SIZE 128
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001526 wlan_private *priv = dev->priv;
1527 wlan_adapter *adapter = priv->adapter;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001528 int err = 0;
1529 char *ev = extra;
1530 char *stop = ev + dwrq->length;
1531 struct bss_descriptor * iter_bss;
1532 struct bss_descriptor * safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001533
Holger Schurig9012b282007-05-25 11:27:16 -04001534 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001535
Dan Williams80e78ef2007-05-25 22:18:47 -04001536 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001537 if ((adapter->mode == IW_MODE_ADHOC) && adapter->adhoccreate) {
Dan Williams0aef64d2007-08-02 11:31:18 -04001538 libertas_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
1539 CMD_OPTION_WAITFORRSP, 0, NULL);
Dan Williams80e78ef2007-05-25 22:18:47 -04001540 }
1541
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001542 mutex_lock(&adapter->lock);
1543 list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1544 char * next_ev;
1545 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001546
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001547 if (stop - ev < SCAN_ITEM_SIZE) {
1548 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001549 break;
1550 }
1551
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001552 /* For mesh device, list only mesh networks */
1553 if (dev == priv->mesh_dev && !iter_bss->mesh)
1554 continue;
1555
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001556 /* Prune old an old scan result */
1557 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1558 if (time_after(jiffies, stale_time)) {
1559 list_move_tail (&iter_bss->list,
1560 &adapter->network_free_list);
1561 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001562 continue;
1563 }
1564
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001565 /* Translate to WE format this entry */
1566 next_ev = libertas_translate_scan(priv, ev, stop, iter_bss);
1567 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001568 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001569 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001570 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001571 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001572
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001573 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001574 dwrq->flags = 0;
1575
Holger Schurig9012b282007-05-25 11:27:16 -04001576 lbs_deb_leave(LBS_DEB_ASSOC);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001577 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001578}
1579
1580/**
1581 * @brief Prepare a scan command to be sent to the firmware
1582 *
1583 * Use the wlan_scan_cmd_config sent to the command processing module in
1584 * the libertas_prepare_and_send_command to configure a cmd_ds_802_11_scan command
1585 * struct to send to firmware.
1586 *
1587 * The fixed fields specifying the BSS type and BSSID filters as well as a
1588 * variable number/length of TLVs are sent in the command to firmware.
1589 *
1590 * @param priv A pointer to wlan_private structure
1591 * @param cmd A pointer to cmd_ds_command structure to be sent to
1592 * firmware with the cmd_DS_801_11_SCAN structure
1593 * @param pdata_buf Void pointer cast of a wlan_scan_cmd_config struct used
1594 * to set the fields/TLVs for the command sent to firmware
1595 *
1596 * @return 0 or -1
1597 *
1598 * @sa wlan_scan_create_channel_list
1599 */
1600int libertas_cmd_80211_scan(wlan_private * priv,
1601 struct cmd_ds_command *cmd, void *pdata_buf)
1602{
1603 struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
1604 struct wlan_scan_cmd_config *pscancfg;
1605
Holger Schurig9012b282007-05-25 11:27:16 -04001606 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001607
1608 pscancfg = pdata_buf;
1609
1610 /* Set fixed field variables in scan command */
1611 pscan->bsstype = pscancfg->bsstype;
Dan Williams492b6da2007-08-02 11:16:07 -04001612 memcpy(pscan->bssid, pscancfg->bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001613 memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
1614
Dan Williams0aef64d2007-08-02 11:31:18 -04001615 cmd->command = cpu_to_le16(CMD_802_11_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001616
1617 /* size is equal to the sizeof(fixed portions) + the TLV len + header */
Dan Williams492b6da2007-08-02 11:16:07 -04001618 cmd->size = cpu_to_le16(sizeof(pscan->bsstype) + ETH_ALEN
1619 + pscancfg->tlvbufferlen + S_DS_GEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001620
Holger Schurig9012b282007-05-25 11:27:16 -04001621 lbs_deb_scan("SCAN_CMD: command=%x, size=%x, seqnum=%x\n",
David Woodhouse981f1872007-05-25 23:36:54 -04001622 le16_to_cpu(cmd->command), le16_to_cpu(cmd->size),
1623 le16_to_cpu(cmd->seqnum));
Holger Schurig9012b282007-05-25 11:27:16 -04001624
1625 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001626 return 0;
1627}
1628
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001629static inline int is_same_network(struct bss_descriptor *src,
1630 struct bss_descriptor *dst)
1631{
1632 /* A network is only a duplicate if the channel, BSSID, and ESSID
1633 * all match. We treat all <hidden> with the same BSSID and channel
1634 * as one network */
Dan Williamsd8efea22007-05-28 23:54:55 -04001635 return ((src->ssid_len == dst->ssid_len) &&
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001636 (src->channel == dst->channel) &&
1637 !compare_ether_addr(src->bssid, dst->bssid) &&
Dan Williamsd8efea22007-05-28 23:54:55 -04001638 !memcmp(src->ssid, dst->ssid, src->ssid_len));
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001639}
1640
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001641/**
1642 * @brief This function handles the command response of scan
1643 *
1644 * The response buffer for the scan command has the following
1645 * memory layout:
1646 *
1647 * .-----------------------------------------------------------.
1648 * | header (4 * sizeof(u16)): Standard command response hdr |
1649 * .-----------------------------------------------------------.
1650 * | bufsize (u16) : sizeof the BSS Description data |
1651 * .-----------------------------------------------------------.
1652 * | NumOfSet (u8) : Number of BSS Descs returned |
1653 * .-----------------------------------------------------------.
1654 * | BSSDescription data (variable, size given in bufsize) |
1655 * .-----------------------------------------------------------.
1656 * | TLV data (variable, size calculated using header->size, |
1657 * | bufsize and sizeof the fixed fields above) |
1658 * .-----------------------------------------------------------.
1659 *
1660 * @param priv A pointer to wlan_private structure
1661 * @param resp A pointer to cmd_ds_command
1662 *
1663 * @return 0 or -1
1664 */
1665int libertas_ret_80211_scan(wlan_private * priv, struct cmd_ds_command *resp)
1666{
1667 wlan_adapter *adapter = priv->adapter;
1668 struct cmd_ds_802_11_scan_rsp *pscan;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001669 struct bss_descriptor * iter_bss;
1670 struct bss_descriptor * safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001671 u8 *pbssinfo;
1672 u16 scanrespsize;
1673 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001674 int idx;
1675 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001676 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001677
Holger Schurig9012b282007-05-25 11:27:16 -04001678 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001679
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001680 /* Prune old entries from scan table */
1681 list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1682 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1683 if (time_before(jiffies, stale_time))
1684 continue;
1685 list_move_tail (&iter_bss->list, &adapter->network_free_list);
1686 clear_bss_descriptor(iter_bss);
1687 }
1688
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001689 pscan = &resp->params.scanresp;
1690
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001691 if (pscan->nr_sets > MAX_NETWORK_COUNT) {
1692 lbs_deb_scan(
1693 "SCAN_RESP: too many scan results (%d, max %d)!!\n",
1694 pscan->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001695 ret = -1;
1696 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001697 }
1698
1699 bytesleft = le16_to_cpu(pscan->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001700 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001701
1702 scanrespsize = le16_to_cpu(resp->size);
Holger Schurig9012b282007-05-25 11:27:16 -04001703 lbs_deb_scan("SCAN_RESP: returned %d AP before parsing\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001704 pscan->nr_sets);
1705
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001706 pbssinfo = pscan->bssdesc_and_tlvbuffer;
1707
1708 /* The size of the TLV buffer is equal to the entire command response
1709 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1710 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1711 * response header (S_DS_GEN)
1712 */
1713 tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
1714 + sizeof(pscan->nr_sets)
1715 + S_DS_GEN);
1716
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001717 /*
1718 * Process each scan response returned (pscan->nr_sets). Save
1719 * the information in the newbssentry and then insert into the
1720 * driver scan table either as an update to an existing entry
1721 * or as an addition at the end of the table
1722 */
1723 for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001724 struct bss_descriptor new;
1725 struct bss_descriptor * found = NULL;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001726 struct bss_descriptor * oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001727
1728 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001729 memset(&new, 0, sizeof (struct bss_descriptor));
1730 if (libertas_process_bss(&new, &pbssinfo, &bytesleft) != 0) {
1731 /* error parsing the scan response, skipped */
1732 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1733 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001734 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001735
1736 /* Try to find this bss in the scan table */
1737 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1738 if (is_same_network(iter_bss, &new)) {
1739 found = iter_bss;
1740 break;
1741 }
1742
1743 if ((oldest == NULL) ||
1744 (iter_bss->last_scanned < oldest->last_scanned))
1745 oldest = iter_bss;
1746 }
1747
1748 if (found) {
1749 /* found, clear it */
1750 clear_bss_descriptor(found);
1751 } else if (!list_empty(&adapter->network_free_list)) {
1752 /* Pull one from the free list */
1753 found = list_entry(adapter->network_free_list.next,
1754 struct bss_descriptor, list);
1755 list_move_tail(&found->list, &adapter->network_list);
1756 } else if (oldest) {
1757 /* If there are no more slots, expire the oldest */
1758 found = oldest;
1759 clear_bss_descriptor(found);
1760 list_move_tail(&found->list, &adapter->network_list);
1761 } else {
1762 continue;
1763 }
1764
1765 lbs_deb_scan("SCAN_RESP: BSSID = " MAC_FMT "\n",
1766 new.bssid[0], new.bssid[1], new.bssid[2],
1767 new.bssid[3], new.bssid[4], new.bssid[5]);
1768
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001769 /* Copy the locally created newbssentry to the scan table */
1770 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001771 }
1772
Holger Schurig9012b282007-05-25 11:27:16 -04001773 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001774
Holger Schurig9012b282007-05-25 11:27:16 -04001775done:
1776 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1777 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001778}