blob: 606af50fa09b79584ec4df8ce5617fe22d41f5b9 [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"
20
21//! Approximate amount of data needed to pass a scan result back to iwlist
22#define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
23 + IW_ESSID_MAX_SIZE \
24 + IW_EV_UINT_LEN \
25 + IW_EV_FREQ_LEN \
26 + IW_EV_QUAL_LEN \
27 + IW_ESSID_MAX_SIZE \
28 + IW_EV_PARAM_LEN \
29 + 40) /* 40 for WPAIE */
30
31//! Memory needed to store a max sized channel List TLV for a firmware scan
32#define CHAN_TLV_MAX_SIZE (sizeof(struct mrvlietypesheader) \
33 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
34 * sizeof(struct chanscanparamset)))
35
36//! Memory needed to store a max number/size SSID TLV for a firmware scan
37#define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvlietypes_ssidparamset))
38
39//! Maximum memory needed for a wlan_scan_cmd_config with all TLVs at max
40#define MAX_SCAN_CFG_ALLOC (sizeof(struct wlan_scan_cmd_config) \
41 + sizeof(struct mrvlietypes_numprobes) \
42 + CHAN_TLV_MAX_SIZE \
43 + SSID_TLV_MAX_SIZE)
44
45//! The maximum number of channels the firmware can scan per command
46#define MRVDRV_MAX_CHANNELS_PER_SCAN 14
47
48/**
49 * @brief Number of channels to scan per firmware scan command issuance.
50 *
51 * Number restricted to prevent hitting the limit on the amount of scan data
52 * returned in a single firmware scan command.
53 */
54#define MRVDRV_CHANNELS_PER_SCAN_CMD 4
55
56//! Scan time specified in the channel TLV for each channel for passive scans
57#define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
58
59//! Scan time specified in the channel TLV for each channel for active scans
60#define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
61
Dan Williams123e0e02007-05-25 23:23:43 -040062static const u8 zeromac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
63static const u8 bcastmac[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Dan Williamseb8f7332007-05-25 16:25:21 -040064
Dan Williamsfcdb53d2007-05-25 16:15:56 -040065static inline void clear_bss_descriptor (struct bss_descriptor * bss)
66{
67 /* Don't blow away ->list, just BSS data */
68 memset(bss, 0, offsetof(struct bss_descriptor, list));
69}
70
71static inline int match_bss_no_security(struct wlan_802_11_security * secinfo,
72 struct bss_descriptor * match_bss)
73{
74 if ( !secinfo->wep_enabled
75 && !secinfo->WPAenabled
76 && !secinfo->WPA2enabled
77 && match_bss->wpa_ie[0] != WPA_IE
78 && match_bss->rsn_ie[0] != WPA2_IE
79 && !match_bss->privacy) {
80 return 1;
81 }
82 return 0;
83}
84
85static inline int match_bss_static_wep(struct wlan_802_11_security * secinfo,
86 struct bss_descriptor * match_bss)
87{
88 if ( secinfo->wep_enabled
89 && !secinfo->WPAenabled
90 && !secinfo->WPA2enabled
91 && match_bss->privacy) {
92 return 1;
93 }
94 return 0;
95}
96
97static inline int match_bss_wpa(struct wlan_802_11_security * secinfo,
98 struct bss_descriptor * match_bss)
99{
100 if ( !secinfo->wep_enabled
101 && secinfo->WPAenabled
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400102 && (match_bss->wpa_ie[0] == WPA_IE)
103 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
104 && bss->privacy */
105 ) {
106 return 1;
107 }
108 return 0;
109}
110
111static inline int match_bss_wpa2(struct wlan_802_11_security * secinfo,
112 struct bss_descriptor * match_bss)
113{
114 if ( !secinfo->wep_enabled
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400115 && secinfo->WPA2enabled
116 && (match_bss->rsn_ie[0] == WPA2_IE)
117 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
118 && bss->privacy */
119 ) {
120 return 1;
121 }
122 return 0;
123}
124
125static inline int match_bss_dynamic_wep(struct wlan_802_11_security * secinfo,
126 struct bss_descriptor * match_bss)
127{
128 if ( !secinfo->wep_enabled
129 && !secinfo->WPAenabled
130 && !secinfo->WPA2enabled
131 && (match_bss->wpa_ie[0] != WPA_IE)
132 && (match_bss->rsn_ie[0] != WPA2_IE)
133 && match_bss->privacy) {
134 return 1;
135 }
136 return 0;
137}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200138
139/**
140 * @brief Check if a scanned network compatible with the driver settings
141 *
142 * WEP WPA WPA2 ad-hoc encrypt Network
143 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
144 * 0 0 0 0 NONE 0 0 0 yes No security
145 * 1 0 0 0 NONE 1 0 0 yes Static WEP
146 * 0 1 0 0 x 1x 1 x yes WPA
147 * 0 0 1 0 x 1x x 1 yes WPA2
148 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
149 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
150 *
151 *
152 * @param adapter A pointer to wlan_adapter
153 * @param index Index in scantable to check against current driver settings
154 * @param mode Network mode: Infrastructure or IBSS
155 *
156 * @return Index in scantable, or error code if negative
157 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400158static int is_network_compatible(wlan_adapter * adapter,
159 struct bss_descriptor * bss, u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200160{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400161 int matched = 0;
162
Holger Schurig9012b282007-05-25 11:27:16 -0400163 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200164
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400165 if (bss->mode != mode)
166 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200167
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400168 if ((matched = match_bss_no_security(&adapter->secinfo, bss))) {
169 goto done;
170 } else if ((matched = match_bss_static_wep(&adapter->secinfo, bss))) {
171 goto done;
172 } else if ((matched = match_bss_wpa(&adapter->secinfo, bss))) {
173 lbs_deb_scan(
174 "is_network_compatible() WPA: wpa_ie=%#x "
175 "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
176 "privacy=%#x\n", bss->wpa_ie[0], bss->rsn_ie[0],
Dan Williams889c05b2007-05-10 22:57:23 -0400177 adapter->secinfo.wep_enabled ? "e" : "d",
178 adapter->secinfo.WPAenabled ? "e" : "d",
179 adapter->secinfo.WPA2enabled ? "e" : "d",
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400180 bss->privacy);
181 goto done;
182 } else if ((matched = match_bss_wpa2(&adapter->secinfo, bss))) {
183 lbs_deb_scan(
184 "is_network_compatible() WPA2: wpa_ie=%#x "
185 "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
186 "privacy=%#x\n", bss->wpa_ie[0], bss->rsn_ie[0],
187 adapter->secinfo.wep_enabled ? "e" : "d",
188 adapter->secinfo.WPAenabled ? "e" : "d",
189 adapter->secinfo.WPA2enabled ? "e" : "d",
190 bss->privacy);
191 goto done;
192 } else if ((matched = match_bss_dynamic_wep(&adapter->secinfo, bss))) {
193 lbs_deb_scan(
194 "is_network_compatible() dynamic WEP: "
195 "wpa_ie=%#x wpa2_ie=%#x privacy=%#x\n",
196 bss->wpa_ie[0],
197 bss->rsn_ie[0],
198 bss->privacy);
Holger Schurig9012b282007-05-25 11:27:16 -0400199 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200200 }
201
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400202 /* bss security settings don't match those configured on card */
203 lbs_deb_scan(
204 "is_network_compatible() FAILED: wpa_ie=%#x "
205 "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s privacy=%#x\n",
206 bss->wpa_ie[0], bss->rsn_ie[0],
207 adapter->secinfo.wep_enabled ? "e" : "d",
208 adapter->secinfo.WPAenabled ? "e" : "d",
209 adapter->secinfo.WPA2enabled ? "e" : "d",
210 bss->privacy);
Holger Schurig9012b282007-05-25 11:27:16 -0400211
212done:
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400213 lbs_deb_leave(LBS_DEB_SCAN);
214 return matched;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200215}
216
217/**
218 * @brief Post process the scan table after a new scan command has completed
219 *
220 * Inspect each entry of the scan table and try to find an entry that
221 * matches our current associated/joined network from the scan. If
222 * one is found, update the stored copy of the bssdescriptor for our
223 * current network.
224 *
225 * Debug dump the current scan table contents if compiled accordingly.
226 *
227 * @param priv A pointer to wlan_private structure
228 *
229 * @return void
230 */
231static void wlan_scan_process_results(wlan_private * priv)
232{
233 wlan_adapter *adapter = priv->adapter;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400234 struct bss_descriptor * iter_bss;
Dan Williamsd8efea22007-05-28 23:54:55 -0400235 int i = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200236
Dan Williamse76850d2007-05-25 17:09:41 -0400237 if (adapter->connect_status == libertas_connected)
238 return;
239
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400240 mutex_lock(&adapter->lock);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400241 list_for_each_entry (iter_bss, &adapter->network_list, list) {
242 lbs_deb_scan("Scan:(%02d) " MAC_FMT ", RSSI[%03d], SSID[%s]\n",
Dan Williams02eb2292007-05-25 17:27:31 -0400243 i++, MAC_ARG(iter_bss->bssid), (s32) iter_bss->rssi,
Dan Williamsd8efea22007-05-28 23:54:55 -0400244 escape_essid(iter_bss->ssid, iter_bss->ssid_len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200245 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400246 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200247}
248
249/**
250 * @brief Create a channel list for the driver to scan based on region info
251 *
252 * Use the driver region/band information to construct a comprehensive list
253 * of channels to scan. This routine is used for any scan that is not
254 * provided a specific channel list to scan.
255 *
256 * @param priv A pointer to wlan_private structure
257 * @param scanchanlist Output parameter: resulting channel list to scan
258 * @param filteredscan Flag indicating whether or not a BSSID or SSID filter
259 * is being sent in the command to firmware. Used to
260 * increase the number of channels sent in a scan
261 * command and to disable the firmware channel scan
262 * filter.
263 *
264 * @return void
265 */
266static void wlan_scan_create_channel_list(wlan_private * priv,
267 struct chanscanparamset * scanchanlist,
268 u8 filteredscan)
269{
270
271 wlan_adapter *adapter = priv->adapter;
272 struct region_channel *scanregion;
273 struct chan_freq_power *cfp;
274 int rgnidx;
275 int chanidx;
276 int nextchan;
277 u8 scantype;
278
279 chanidx = 0;
280
281 /* Set the default scan type to the user specified type, will later
282 * be changed to passive on a per channel basis if restricted by
283 * regulatory requirements (11d or 11h)
284 */
285 scantype = adapter->scantype;
286
287 for (rgnidx = 0; rgnidx < ARRAY_SIZE(adapter->region_channel); rgnidx++) {
288 if (priv->adapter->enable11d &&
289 adapter->connect_status != libertas_connected) {
290 /* Scan all the supported chan for the first scan */
291 if (!adapter->universal_channel[rgnidx].valid)
292 continue;
293 scanregion = &adapter->universal_channel[rgnidx];
294
295 /* clear the parsed_region_chan for the first scan */
296 memset(&adapter->parsed_region_chan, 0x00,
297 sizeof(adapter->parsed_region_chan));
298 } else {
299 if (!adapter->region_channel[rgnidx].valid)
300 continue;
301 scanregion = &adapter->region_channel[rgnidx];
302 }
303
304 for (nextchan = 0;
305 nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
306
307 cfp = scanregion->CFP + nextchan;
308
309 if (priv->adapter->enable11d) {
310 scantype =
311 libertas_get_scan_type_11d(cfp->channel,
312 &adapter->
313 parsed_region_chan);
314 }
315
316 switch (scanregion->band) {
317 case BAND_B:
318 case BAND_G:
319 default:
320 scanchanlist[chanidx].radiotype =
321 cmd_scan_radio_type_bg;
322 break;
323 }
324
325 if (scantype == cmd_scan_type_passive) {
326 scanchanlist[chanidx].maxscantime =
David Woodhouse981f1872007-05-25 23:36:54 -0400327 cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200328 scanchanlist[chanidx].chanscanmode.passivescan =
329 1;
330 } else {
331 scanchanlist[chanidx].maxscantime =
David Woodhouse981f1872007-05-25 23:36:54 -0400332 cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200333 scanchanlist[chanidx].chanscanmode.passivescan =
334 0;
335 }
336
337 scanchanlist[chanidx].channumber = cfp->channel;
338
339 if (filteredscan) {
340 scanchanlist[chanidx].chanscanmode.
341 disablechanfilt = 1;
342 }
343 }
344 }
345}
346
347/**
348 * @brief Construct a wlan_scan_cmd_config structure to use in issue scan cmds
349 *
350 * Application layer or other functions can invoke wlan_scan_networks
351 * with a scan configuration supplied in a wlan_ioctl_user_scan_cfg struct.
352 * This structure is used as the basis of one or many wlan_scan_cmd_config
353 * commands that are sent to the command processing module and sent to
354 * firmware.
355 *
356 * Create a wlan_scan_cmd_config based on the following user supplied
357 * parameters (if present):
358 * - SSID filter
359 * - BSSID filter
360 * - Number of Probes to be sent
361 * - channel list
362 *
363 * If the SSID or BSSID filter is not present, disable/clear the filter.
364 * If the number of probes is not set, use the adapter default setting
365 * Qualify the channel
366 *
367 * @param priv A pointer to wlan_private structure
368 * @param puserscanin NULL or pointer to scan configuration parameters
369 * @param ppchantlvout Output parameter: Pointer to the start of the
370 * channel TLV portion of the output scan config
371 * @param pscanchanlist Output parameter: Pointer to the resulting channel
372 * list to scan
373 * @param pmaxchanperscan Output parameter: Number of channels to scan for
374 * each issuance of the firmware scan command
375 * @param pfilteredscan Output parameter: Flag indicating whether or not
376 * a BSSID or SSID filter is being sent in the
377 * command to firmware. Used to increase the number
378 * of channels sent in a scan command and to
379 * disable the firmware channel scan filter.
380 * @param pscancurrentonly Output parameter: Flag indicating whether or not
381 * we are only scanning our current active channel
382 *
383 * @return resulting scan configuration
384 */
385static struct wlan_scan_cmd_config *
386wlan_scan_setup_scan_config(wlan_private * priv,
387 const struct wlan_ioctl_user_scan_cfg * puserscanin,
388 struct mrvlietypes_chanlistparamset ** ppchantlvout,
389 struct chanscanparamset * pscanchanlist,
390 int *pmaxchanperscan,
391 u8 * pfilteredscan,
392 u8 * pscancurrentonly)
393{
394 wlan_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200395 struct mrvlietypes_numprobes *pnumprobestlv;
396 struct mrvlietypes_ssidparamset *pssidtlv;
397 struct wlan_scan_cmd_config * pscancfgout = NULL;
398 u8 *ptlvpos;
399 u16 numprobes;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200400 int chanidx;
401 int scantype;
402 int scandur;
403 int channel;
404 int radiotype;
405
406 pscancfgout = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
407 if (pscancfgout == NULL)
408 goto out;
409
410 /* The tlvbufferlen is calculated for each scan command. The TLVs added
411 * in this routine will be preserved since the routine that sends
412 * the command will append channelTLVs at *ppchantlvout. The difference
413 * between the *ppchantlvout and the tlvbuffer start will be used
414 * to calculate the size of anything we add in this routine.
415 */
416 pscancfgout->tlvbufferlen = 0;
417
418 /* Running tlv pointer. Assigned to ppchantlvout at end of function
419 * so later routines know where channels can be added to the command buf
420 */
421 ptlvpos = pscancfgout->tlvbuffer;
422
423 /*
424 * Set the initial scan paramters for progressive scanning. If a specific
425 * BSSID or SSID is used, the number of channels in the scan command
426 * will be increased to the absolute maximum
427 */
428 *pmaxchanperscan = MRVDRV_CHANNELS_PER_SCAN_CMD;
429
430 /* Initialize the scan as un-filtered by firmware, set to TRUE below if
431 * a SSID or BSSID filter is sent in the command
432 */
433 *pfilteredscan = 0;
434
435 /* Initialize the scan as not being only on the current channel. If
436 * the channel list is customized, only contains one channel, and
437 * is the active channel, this is set true and data flow is not halted.
438 */
439 *pscancurrentonly = 0;
440
441 if (puserscanin) {
442
443 /* Set the bss type scan filter, use adapter setting if unset */
444 pscancfgout->bsstype =
445 (puserscanin->bsstype ? puserscanin->bsstype : adapter->
446 scanmode);
447
448 /* Set the number of probes to send, use adapter setting if unset */
449 numprobes = (puserscanin->numprobes ? puserscanin->numprobes :
450 adapter->scanprobes);
451
452 /*
453 * Set the BSSID filter to the incoming configuration,
454 * if non-zero. If not set, it will remain disabled (all zeros).
455 */
Dan Williamseb8f7332007-05-25 16:25:21 -0400456 memcpy(pscancfgout->bssid, puserscanin->bssid,
457 sizeof(pscancfgout->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200458
Dan Williamseb8f7332007-05-25 16:25:21 -0400459 if (puserscanin->ssid_len) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200460 pssidtlv =
461 (struct mrvlietypes_ssidparamset *) pscancfgout->
462 tlvbuffer;
463 pssidtlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
Dan Williamseb8f7332007-05-25 16:25:21 -0400464 pssidtlv->header.len = cpu_to_le16(puserscanin->ssid_len);
465 memcpy(pssidtlv->ssid, puserscanin->ssid,
466 puserscanin->ssid_len);
467 ptlvpos += sizeof(pssidtlv->header) + puserscanin->ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200468 }
469
470 /*
471 * The default number of channels sent in the command is low to
472 * ensure the response buffer from the firmware does not truncate
473 * scan results. That is not an issue with an SSID or BSSID
474 * filter applied to the scan results in the firmware.
475 */
Dan Williamseb8f7332007-05-25 16:25:21 -0400476 if ( puserscanin->ssid_len
477 || (compare_ether_addr(pscancfgout->bssid, &zeromac[0]) != 0)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200478 *pmaxchanperscan = MRVDRV_MAX_CHANNELS_PER_SCAN;
479 *pfilteredscan = 1;
480 }
481 } else {
482 pscancfgout->bsstype = adapter->scanmode;
483 numprobes = adapter->scanprobes;
484 }
485
486 /* If the input config or adapter has the number of Probes set, add tlv */
487 if (numprobes) {
488 pnumprobestlv = (struct mrvlietypes_numprobes *) ptlvpos;
David Woodhouse981f1872007-05-25 23:36:54 -0400489 pnumprobestlv->header.type = cpu_to_le16(TLV_TYPE_NUMPROBES);
490 pnumprobestlv->header.len = cpu_to_le16(2);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200491 pnumprobestlv->numprobes = cpu_to_le16(numprobes);
492
David Woodhouse981f1872007-05-25 23:36:54 -0400493 ptlvpos += sizeof(*pnumprobestlv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200494 }
495
496 /*
497 * Set the output for the channel TLV to the address in the tlv buffer
498 * past any TLVs that were added in this fuction (SSID, numprobes).
499 * channel TLVs will be added past this for each scan command, preserving
500 * the TLVs that were previously added.
501 */
502 *ppchantlvout = (struct mrvlietypes_chanlistparamset *) ptlvpos;
503
504 if (puserscanin && puserscanin->chanlist[0].channumber) {
505
Holger Schurig9012b282007-05-25 11:27:16 -0400506 lbs_deb_scan("Scan: Using supplied channel list\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200507
508 for (chanidx = 0;
509 chanidx < WLAN_IOCTL_USER_SCAN_CHAN_MAX
510 && puserscanin->chanlist[chanidx].channumber; chanidx++) {
511
512 channel = puserscanin->chanlist[chanidx].channumber;
513 (pscanchanlist + chanidx)->channumber = channel;
514
515 radiotype = puserscanin->chanlist[chanidx].radiotype;
516 (pscanchanlist + chanidx)->radiotype = radiotype;
517
518 scantype = puserscanin->chanlist[chanidx].scantype;
519
520 if (scantype == cmd_scan_type_passive) {
521 (pscanchanlist +
522 chanidx)->chanscanmode.passivescan = 1;
523 } else {
524 (pscanchanlist +
525 chanidx)->chanscanmode.passivescan = 0;
526 }
527
528 if (puserscanin->chanlist[chanidx].scantime) {
529 scandur =
530 puserscanin->chanlist[chanidx].scantime;
531 } else {
532 if (scantype == cmd_scan_type_passive) {
533 scandur = MRVDRV_PASSIVE_SCAN_CHAN_TIME;
534 } else {
535 scandur = MRVDRV_ACTIVE_SCAN_CHAN_TIME;
536 }
537 }
538
539 (pscanchanlist + chanidx)->minscantime =
540 cpu_to_le16(scandur);
541 (pscanchanlist + chanidx)->maxscantime =
542 cpu_to_le16(scandur);
543 }
544
545 /* Check if we are only scanning the current channel */
546 if ((chanidx == 1) && (puserscanin->chanlist[0].channumber
547 ==
548 priv->adapter->curbssparams.channel)) {
549 *pscancurrentonly = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400550 lbs_deb_scan("Scan: Scanning current channel only");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200551 }
552
553 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400554 lbs_deb_scan("Scan: Creating full region channel list\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200555 wlan_scan_create_channel_list(priv, pscanchanlist,
556 *pfilteredscan);
557 }
558
559out:
560 return pscancfgout;
561}
562
563/**
564 * @brief Construct and send multiple scan config commands to the firmware
565 *
566 * Previous routines have created a wlan_scan_cmd_config with any requested
567 * TLVs. This function splits the channel TLV into maxchanperscan lists
568 * and sends the portion of the channel TLV along with the other TLVs
569 * to the wlan_cmd routines for execution in the firmware.
570 *
571 * @param priv A pointer to wlan_private structure
572 * @param maxchanperscan Maximum number channels to be included in each
573 * scan command sent to firmware
574 * @param filteredscan Flag indicating whether or not a BSSID or SSID
575 * filter is being used for the firmware command
576 * scan command sent to firmware
577 * @param pscancfgout Scan configuration used for this scan.
578 * @param pchantlvout Pointer in the pscancfgout where the channel TLV
579 * should start. This is past any other TLVs that
580 * must be sent down in each firmware command.
581 * @param pscanchanlist List of channels to scan in maxchanperscan segments
582 *
583 * @return 0 or error return otherwise
584 */
585static int wlan_scan_channel_list(wlan_private * priv,
586 int maxchanperscan,
587 u8 filteredscan,
588 struct wlan_scan_cmd_config * pscancfgout,
589 struct mrvlietypes_chanlistparamset * pchantlvout,
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400590 struct chanscanparamset * pscanchanlist,
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400591 const struct wlan_ioctl_user_scan_cfg * puserscanin,
592 int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200593{
594 struct chanscanparamset *ptmpchan;
595 struct chanscanparamset *pstartchan;
596 u8 scanband;
597 int doneearly;
598 int tlvidx;
599 int ret = 0;
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400600 int scanned = 0;
601 union iwreq_data wrqu;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200602
Holger Schurig9012b282007-05-25 11:27:16 -0400603 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200604
605 if (pscancfgout == 0 || pchantlvout == 0 || pscanchanlist == 0) {
Holger Schurig9012b282007-05-25 11:27:16 -0400606 lbs_deb_scan("Scan: Null detect: %p, %p, %p\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200607 pscancfgout, pchantlvout, pscanchanlist);
608 return -1;
609 }
610
611 pchantlvout->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
612
613 /* Set the temp channel struct pointer to the start of the desired list */
614 ptmpchan = pscanchanlist;
615
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400616 if (priv->adapter->last_scanned_channel && !puserscanin)
617 ptmpchan += priv->adapter->last_scanned_channel;
618
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200619 /* Loop through the desired channel list, sending a new firmware scan
620 * commands for each maxchanperscan channels (or for 1,6,11 individually
621 * if configured accordingly)
622 */
623 while (ptmpchan->channumber) {
624
625 tlvidx = 0;
626 pchantlvout->header.len = 0;
627 scanband = ptmpchan->radiotype;
628 pstartchan = ptmpchan;
629 doneearly = 0;
630
631 /* Construct the channel TLV for the scan command. Continue to
632 * insert channel TLVs until:
633 * - the tlvidx hits the maximum configured per scan command
634 * - the next channel to insert is 0 (end of desired channel list)
635 * - doneearly is set (controlling individual scanning of 1,6,11)
636 */
637 while (tlvidx < maxchanperscan && ptmpchan->channumber
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400638 && !doneearly && scanned < 2) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200639
Holger Schurig9012b282007-05-25 11:27:16 -0400640 lbs_deb_scan(
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200641 "Scan: Chan(%3d), Radio(%d), mode(%d,%d), Dur(%d)\n",
642 ptmpchan->channumber, ptmpchan->radiotype,
643 ptmpchan->chanscanmode.passivescan,
644 ptmpchan->chanscanmode.disablechanfilt,
645 ptmpchan->maxscantime);
646
647 /* Copy the current channel TLV to the command being prepared */
648 memcpy(pchantlvout->chanscanparam + tlvidx,
649 ptmpchan, sizeof(pchantlvout->chanscanparam));
650
651 /* Increment the TLV header length by the size appended */
David Woodhouse981f1872007-05-25 23:36:54 -0400652 /* Ew, it would be _so_ nice if we could just declare the
653 variable little-endian and let GCC handle it for us */
654 pchantlvout->header.len =
655 cpu_to_le16(le16_to_cpu(pchantlvout->header.len) +
656 sizeof(pchantlvout->chanscanparam));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200657
658 /*
659 * The tlv buffer length is set to the number of bytes of the
660 * between the channel tlv pointer and the start of the
661 * tlv buffer. This compensates for any TLVs that were appended
662 * before the channel list.
663 */
664 pscancfgout->tlvbufferlen = ((u8 *) pchantlvout
665 - pscancfgout->tlvbuffer);
666
667 /* Add the size of the channel tlv header and the data length */
668 pscancfgout->tlvbufferlen +=
669 (sizeof(pchantlvout->header)
David Woodhouse981f1872007-05-25 23:36:54 -0400670 + le16_to_cpu(pchantlvout->header.len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200671
672 /* Increment the index to the channel tlv we are constructing */
673 tlvidx++;
674
675 doneearly = 0;
676
677 /* Stop the loop if the *current* channel is in the 1,6,11 set
678 * and we are not filtering on a BSSID or SSID.
679 */
680 if (!filteredscan && (ptmpchan->channumber == 1
681 || ptmpchan->channumber == 6
682 || ptmpchan->channumber == 11)) {
683 doneearly = 1;
684 }
685
686 /* Increment the tmp pointer to the next channel to be scanned */
687 ptmpchan++;
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400688 scanned++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200689
690 /* Stop the loop if the *next* channel is in the 1,6,11 set.
691 * This will cause it to be the only channel scanned on the next
692 * interation
693 */
694 if (!filteredscan && (ptmpchan->channumber == 1
695 || ptmpchan->channumber == 6
696 || ptmpchan->channumber == 11)) {
697 doneearly = 1;
698 }
699 }
700
701 /* Send the scan command to the firmware with the specified cfg */
702 ret = libertas_prepare_and_send_command(priv, cmd_802_11_scan, 0,
703 0, 0, pscancfgout);
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400704 if (scanned >= 2 && !full_scan) {
Holger Schurig9012b282007-05-25 11:27:16 -0400705 ret = 0;
706 goto done;
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400707 }
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400708 scanned = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200709 }
710
Dan Williamsd9ad2f52007-05-25 22:38:41 -0400711done:
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400712 priv->adapter->last_scanned_channel = ptmpchan->channumber;
713
Dan Williamsd9ad2f52007-05-25 22:38:41 -0400714 /* Tell userspace the scan table has been updated */
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400715 memset(&wrqu, 0, sizeof(union iwreq_data));
Holger Schurig634b8f42007-05-25 13:05:16 -0400716 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400717
Holger Schurig9012b282007-05-25 11:27:16 -0400718 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200719 return ret;
720}
721
Dan Williamseb8f7332007-05-25 16:25:21 -0400722static void
723clear_selected_scan_list_entries(wlan_adapter * adapter,
724 const struct wlan_ioctl_user_scan_cfg * scan_cfg)
725{
726 struct bss_descriptor * bss;
727 struct bss_descriptor * safe;
728 u32 clear_ssid_flag = 0, clear_bssid_flag = 0;
729
730 if (!scan_cfg)
731 return;
732
733 if (scan_cfg->clear_ssid && scan_cfg->ssid_len)
734 clear_ssid_flag = 1;
735
736 if (scan_cfg->clear_bssid
737 && (compare_ether_addr(scan_cfg->bssid, &zeromac[0]) != 0)
738 && (compare_ether_addr(scan_cfg->bssid, &bcastmac[0]) != 0)) {
739 clear_bssid_flag = 1;
740 }
741
742 if (!clear_ssid_flag && !clear_bssid_flag)
743 return;
744
745 mutex_lock(&adapter->lock);
746 list_for_each_entry_safe (bss, safe, &adapter->network_list, list) {
747 u32 clear = 0;
748
749 /* Check for an SSID match */
750 if ( clear_ssid_flag
Dan Williamsd8efea22007-05-28 23:54:55 -0400751 && (bss->ssid_len == scan_cfg->ssid_len)
752 && !memcmp(bss->ssid, scan_cfg->ssid, bss->ssid_len))
Dan Williamseb8f7332007-05-25 16:25:21 -0400753 clear = 1;
754
755 /* Check for a BSSID match */
756 if ( clear_bssid_flag
757 && !compare_ether_addr(bss->bssid, scan_cfg->bssid))
758 clear = 1;
759
760 if (clear) {
761 list_move_tail (&bss->list, &adapter->network_free_list);
762 clear_bss_descriptor(bss);
763 }
764 }
765 mutex_unlock(&adapter->lock);
766}
767
768
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200769/**
770 * @brief Internal function used to start a scan based on an input config
771 *
772 * Use the input user scan configuration information when provided in
773 * order to send the appropriate scan commands to firmware to populate or
774 * update the internal driver scan table
775 *
776 * @param priv A pointer to wlan_private structure
777 * @param puserscanin Pointer to the input configuration for the requested
778 * scan.
779 *
780 * @return 0 or < 0 if error
781 */
782int wlan_scan_networks(wlan_private * priv,
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400783 const struct wlan_ioctl_user_scan_cfg * puserscanin,
784 int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200785{
Dan Williamseb8f7332007-05-25 16:25:21 -0400786 wlan_adapter * adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200787 struct mrvlietypes_chanlistparamset *pchantlvout;
788 struct chanscanparamset * scan_chan_list = NULL;
789 struct wlan_scan_cmd_config * scan_cfg = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200790 u8 filteredscan;
791 u8 scancurrentchanonly;
792 int maxchanperscan;
793 int ret;
794
Holger Schurig9012b282007-05-25 11:27:16 -0400795 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200796
797 scan_chan_list = kzalloc(sizeof(struct chanscanparamset) *
798 WLAN_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
799 if (scan_chan_list == NULL) {
800 ret = -ENOMEM;
801 goto out;
802 }
803
804 scan_cfg = wlan_scan_setup_scan_config(priv,
805 puserscanin,
806 &pchantlvout,
807 scan_chan_list,
808 &maxchanperscan,
809 &filteredscan,
810 &scancurrentchanonly);
811 if (scan_cfg == NULL) {
812 ret = -ENOMEM;
813 goto out;
814 }
815
Dan Williamseb8f7332007-05-25 16:25:21 -0400816 clear_selected_scan_list_entries(adapter, puserscanin);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200817
818 /* Keep the data path active if we are only scanning our current channel */
819 if (!scancurrentchanonly) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400820 netif_stop_queue(priv->dev);
821 netif_carrier_off(priv->dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400822 netif_stop_queue(priv->mesh_dev);
823 netif_carrier_off(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200824 }
825
826 ret = wlan_scan_channel_list(priv,
827 maxchanperscan,
828 filteredscan,
829 scan_cfg,
830 pchantlvout,
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400831 scan_chan_list,
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400832 puserscanin,
833 full_scan);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200834
835 /* Process the resulting scan table:
836 * - Remove any bad ssids
837 * - Update our current BSS information from scan data
838 */
839 wlan_scan_process_results(priv);
840
841 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);
Javier Cardona51d84f52007-05-25 12:06:56 -0400844 netif_carrier_on(priv->mesh_dev);
845 netif_wake_queue(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200846 }
847
848out:
849 if (scan_cfg)
850 kfree(scan_cfg);
851
852 if (scan_chan_list)
853 kfree(scan_chan_list);
854
Holger Schurig9012b282007-05-25 11:27:16 -0400855 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200856 return ret;
857}
858
859/**
860 * @brief Inspect the scan response buffer for pointers to expected TLVs
861 *
862 * TLVs can be included at the end of the scan response BSS information.
863 * Parse the data in the buffer for pointers to TLVs that can potentially
864 * be passed back in the response
865 *
866 * @param ptlv Pointer to the start of the TLV buffer to parse
867 * @param tlvbufsize size of the TLV buffer
868 * @param ptsftlv Output parameter: Pointer to the TSF TLV if found
869 *
870 * @return void
871 */
872static
873void wlan_ret_802_11_scan_get_tlv_ptrs(struct mrvlietypes_data * ptlv,
874 int tlvbufsize,
875 struct mrvlietypes_tsftimestamp ** ptsftlv)
876{
877 struct mrvlietypes_data *pcurrenttlv;
878 int tlvbufleft;
879 u16 tlvtype;
880 u16 tlvlen;
881
882 pcurrenttlv = ptlv;
883 tlvbufleft = tlvbufsize;
884 *ptsftlv = NULL;
885
Holger Schurig9012b282007-05-25 11:27:16 -0400886 lbs_deb_scan("SCAN_RESP: tlvbufsize = %d\n", tlvbufsize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200887 lbs_dbg_hex("SCAN_RESP: TLV Buf", (u8 *) ptlv, tlvbufsize);
888
889 while (tlvbufleft >= sizeof(struct mrvlietypesheader)) {
890 tlvtype = le16_to_cpu(pcurrenttlv->header.type);
891 tlvlen = le16_to_cpu(pcurrenttlv->header.len);
892
893 switch (tlvtype) {
894 case TLV_TYPE_TSFTIMESTAMP:
895 *ptsftlv = (struct mrvlietypes_tsftimestamp *) pcurrenttlv;
896 break;
897
898 default:
Holger Schurig9012b282007-05-25 11:27:16 -0400899 lbs_deb_scan("SCAN_RESP: Unhandled TLV = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200900 tlvtype);
901 /* Give up, this seems corrupted */
902 return;
903 } /* switch */
904
905 tlvbufleft -= (sizeof(ptlv->header) + tlvlen);
906 pcurrenttlv =
907 (struct mrvlietypes_data *) (pcurrenttlv->Data + tlvlen);
908 } /* while */
909}
910
911/**
912 * @brief Interpret a BSS scan response returned from the firmware
913 *
914 * Parse the various fixed fields and IEs passed back for a a BSS probe
915 * response or beacon from the scan command. Record information as needed
916 * in the scan table struct bss_descriptor for that entry.
917 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400918 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200919 *
920 * @return 0 or -1
921 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400922static int libertas_process_bss(struct bss_descriptor * bss,
923 u8 ** pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200924{
925 enum ieeetypes_elementid elemID;
926 struct ieeetypes_fhparamset *pFH;
927 struct ieeetypes_dsparamset *pDS;
928 struct ieeetypes_cfparamset *pCF;
929 struct ieeetypes_ibssparamset *pibss;
930 struct ieeetypes_capinfo *pcap;
931 struct WLAN_802_11_FIXED_IEs fixedie;
932 u8 *pcurrentptr;
933 u8 *pRate;
934 u8 elemlen;
935 u8 bytestocopy;
936 u8 ratesize;
937 u16 beaconsize;
938 u8 founddatarateie;
939 int bytesleftforcurrentbeacon;
Holger Schurig9012b282007-05-25 11:27:16 -0400940 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200941
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200942 struct IE_WPA *pIe;
943 const u8 oui01[4] = { 0x00, 0x50, 0xf2, 0x01 };
944
945 struct ieeetypes_countryinfoset *pcountryinfo;
946
Holger Schurig9012b282007-05-25 11:27:16 -0400947 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200948
949 founddatarateie = 0;
950 ratesize = 0;
951 beaconsize = 0;
952
953 if (*bytesleft >= sizeof(beaconsize)) {
954 /* Extract & convert beacon size from the command buffer */
David Woodhouse981f1872007-05-25 23:36:54 -0400955 beaconsize = le16_to_cpup((void *)*pbeaconinfo);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200956 *bytesleft -= sizeof(beaconsize);
957 *pbeaconinfo += sizeof(beaconsize);
958 }
959
960 if (beaconsize == 0 || beaconsize > *bytesleft) {
961
962 *pbeaconinfo += *bytesleft;
963 *bytesleft = 0;
964
965 return -1;
966 }
967
968 /* Initialize the current working beacon pointer for this BSS iteration */
969 pcurrentptr = *pbeaconinfo;
970
971 /* Advance the return beacon pointer past the current beacon */
972 *pbeaconinfo += beaconsize;
973 *bytesleft -= beaconsize;
974
975 bytesleftforcurrentbeacon = beaconsize;
976
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400977 memcpy(bss->bssid, pcurrentptr, ETH_ALEN);
Dan Williams02eb2292007-05-25 17:27:31 -0400978 lbs_deb_scan("process_bss: AP BSSID " MAC_FMT "\n", MAC_ARG(bss->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200979
980 pcurrentptr += ETH_ALEN;
981 bytesleftforcurrentbeacon -= ETH_ALEN;
982
983 if (bytesleftforcurrentbeacon < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400984 lbs_deb_scan("process_bss: Not enough bytes left\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200985 return -1;
986 }
987
988 /*
989 * next 4 fields are RSSI, time stamp, beacon interval,
990 * and capability information
991 */
992
993 /* RSSI is 1 byte long */
David Woodhouse981f1872007-05-25 23:36:54 -0400994 bss->rssi = *pcurrentptr;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400995 lbs_deb_scan("process_bss: RSSI=%02X\n", *pcurrentptr);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200996 pcurrentptr += 1;
997 bytesleftforcurrentbeacon -= 1;
998
999 /* time stamp is 8 bytes long */
David Woodhouse981f1872007-05-25 23:36:54 -04001000 fixedie.timestamp = bss->timestamp = le64_to_cpup((void *)pcurrentptr);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001001 pcurrentptr += 8;
1002 bytesleftforcurrentbeacon -= 8;
1003
1004 /* beacon interval is 2 bytes long */
David Woodhouse981f1872007-05-25 23:36:54 -04001005 fixedie.beaconinterval = bss->beaconperiod = le16_to_cpup((void *)pcurrentptr);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001006 pcurrentptr += 2;
1007 bytesleftforcurrentbeacon -= 2;
1008
1009 /* capability information is 2 bytes long */
David Woodhouse981f1872007-05-25 23:36:54 -04001010 memcpy(&fixedie.capabilities, pcurrentptr, 2);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001011 lbs_deb_scan("process_bss: fixedie.capabilities=0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001012 fixedie.capabilities);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001013 pcap = (struct ieeetypes_capinfo *) & fixedie.capabilities;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001014 memcpy(&bss->cap, pcap, sizeof(struct ieeetypes_capinfo));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001015 pcurrentptr += 2;
1016 bytesleftforcurrentbeacon -= 2;
1017
1018 /* rest of the current buffer are IE's */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001019 lbs_deb_scan("process_bss: IE length for this AP = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001020 bytesleftforcurrentbeacon);
1021
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001022 lbs_dbg_hex("process_bss: IE info", (u8 *) pcurrentptr,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001023 bytesleftforcurrentbeacon);
1024
1025 if (pcap->privacy) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001026 lbs_deb_scan("process_bss: AP WEP enabled\n");
1027 bss->privacy = wlan802_11privfilter8021xWEP;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001028 } else {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001029 bss->privacy = wlan802_11privfilteracceptall;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001030 }
1031
1032 if (pcap->ibss == 1) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001033 bss->mode = IW_MODE_ADHOC;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001034 } else {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001035 bss->mode = IW_MODE_INFRA;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001036 }
1037
1038 /* process variable IE */
1039 while (bytesleftforcurrentbeacon >= 2) {
1040 elemID = (enum ieeetypes_elementid) (*((u8 *) pcurrentptr));
1041 elemlen = *((u8 *) pcurrentptr + 1);
1042
1043 if (bytesleftforcurrentbeacon < elemlen) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001044 lbs_deb_scan("process_bss: error in processing IE, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001045 "bytes left < IE length\n");
1046 bytesleftforcurrentbeacon = 0;
1047 continue;
1048 }
1049
1050 switch (elemID) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001051 case SSID:
Dan Williamsd8efea22007-05-28 23:54:55 -04001052 bss->ssid_len = elemlen;
1053 memcpy(bss->ssid, (pcurrentptr + 2), elemlen);
1054 lbs_deb_scan("ssid '%s', ssid length %u\n",
1055 escape_essid(bss->ssid, bss->ssid_len),
1056 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001057 break;
1058
1059 case SUPPORTED_RATES:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001060 memcpy(bss->datarates, (pcurrentptr + 2), elemlen);
1061 memmove(bss->libertas_supported_rates, (pcurrentptr + 2),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001062 elemlen);
1063 ratesize = elemlen;
1064 founddatarateie = 1;
1065 break;
1066
1067 case EXTRA_IE:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001068 lbs_deb_scan("process_bss: EXTRA_IE Found!\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001069 break;
1070
1071 case FH_PARAM_SET:
1072 pFH = (struct ieeetypes_fhparamset *) pcurrentptr;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001073 memmove(&bss->phyparamset.fhparamset, pFH,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001074 sizeof(struct ieeetypes_fhparamset));
David Woodhouse981f1872007-05-25 23:36:54 -04001075#if 0 /* I think we can store these LE */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001076 bss->phyparamset.fhparamset.dwelltime
1077 = le16_to_cpu(bss->phyparamset.fhparamset.dwelltime);
David Woodhouse981f1872007-05-25 23:36:54 -04001078#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001079 break;
1080
1081 case DS_PARAM_SET:
1082 pDS = (struct ieeetypes_dsparamset *) pcurrentptr;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001083 bss->channel = pDS->currentchan;
1084 memcpy(&bss->phyparamset.dsparamset, pDS,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001085 sizeof(struct ieeetypes_dsparamset));
1086 break;
1087
1088 case CF_PARAM_SET:
1089 pCF = (struct ieeetypes_cfparamset *) pcurrentptr;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001090 memcpy(&bss->ssparamset.cfparamset, pCF,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001091 sizeof(struct ieeetypes_cfparamset));
1092 break;
1093
1094 case IBSS_PARAM_SET:
1095 pibss = (struct ieeetypes_ibssparamset *) pcurrentptr;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001096 bss->atimwindow = le32_to_cpu(pibss->atimwindow);
1097 memmove(&bss->ssparamset.ibssparamset, pibss,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001098 sizeof(struct ieeetypes_ibssparamset));
David Woodhouse981f1872007-05-25 23:36:54 -04001099#if 0
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001100 bss->ssparamset.ibssparamset.atimwindow
1101 = le16_to_cpu(bss->ssparamset.ibssparamset.atimwindow);
David Woodhouse981f1872007-05-25 23:36:54 -04001102#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001103 break;
1104
1105 /* Handle Country Info IE */
1106 case COUNTRY_INFO:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001107 pcountryinfo = (struct ieeetypes_countryinfoset *) pcurrentptr;
1108 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001109 || pcountryinfo->len > 254) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001110 lbs_deb_scan("process_bss: 11D- Err "
Dan Williams4269e2a2007-05-10 23:10:18 -04001111 "CountryInfo len =%d min=%zd max=254\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001112 pcountryinfo->len,
1113 sizeof(pcountryinfo->countrycode));
Holger Schurig9012b282007-05-25 11:27:16 -04001114 ret = -1;
1115 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001116 }
1117
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001118 memcpy(&bss->countryinfo,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001119 pcountryinfo, pcountryinfo->len + 2);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001120 lbs_dbg_hex("process_bss: 11D- CountryInfo:",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001121 (u8 *) pcountryinfo,
1122 (u32) (pcountryinfo->len + 2));
1123 break;
1124
1125 case EXTENDED_SUPPORTED_RATES:
1126 /*
1127 * only process extended supported rate
1128 * if data rate is already found.
1129 * data rate IE should come before
1130 * extended supported rate IE
1131 */
1132 if (founddatarateie) {
1133 if ((elemlen + ratesize) > WLAN_SUPPORTED_RATES) {
1134 bytestocopy =
1135 (WLAN_SUPPORTED_RATES - ratesize);
1136 } else {
1137 bytestocopy = elemlen;
1138 }
1139
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001140 pRate = (u8 *) bss->datarates;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001141 pRate += ratesize;
1142 memmove(pRate, (pcurrentptr + 2), bytestocopy);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001143 pRate = (u8 *) bss->libertas_supported_rates;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001144 pRate += ratesize;
1145 memmove(pRate, (pcurrentptr + 2), bytestocopy);
1146 }
1147 break;
1148
1149 case VENDOR_SPECIFIC_221:
1150#define IE_ID_LEN_FIELDS_BYTES 2
1151 pIe = (struct IE_WPA *)pcurrentptr;
1152
Dan Williams51b0c9d2007-05-10 22:51:28 -04001153 if (memcmp(pIe->oui, oui01, sizeof(oui01)))
1154 break;
1155
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001156 bss->wpa_ie_len = min(elemlen + IE_ID_LEN_FIELDS_BYTES,
1157 MAX_WPA_IE_LEN);
1158 memcpy(bss->wpa_ie, pcurrentptr, bss->wpa_ie_len);
1159 lbs_dbg_hex("process_bss: WPA IE", bss->wpa_ie, elemlen);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001160 break;
1161 case WPA2_IE:
1162 pIe = (struct IE_WPA *)pcurrentptr;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001163 bss->rsn_ie_len = min(elemlen + IE_ID_LEN_FIELDS_BYTES,
1164 MAX_WPA_IE_LEN);
1165 memcpy(bss->rsn_ie, pcurrentptr, bss->rsn_ie_len);
1166 lbs_dbg_hex("process_bss: RSN_IE", bss->rsn_ie, elemlen);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001167 break;
1168 case TIM:
1169 break;
1170
1171 case CHALLENGE_TEXT:
1172 break;
1173 }
1174
1175 pcurrentptr += elemlen + 2;
1176
1177 /* need to account for IE ID and IE len */
1178 bytesleftforcurrentbeacon -= (elemlen + 2);
1179
1180 } /* while (bytesleftforcurrentbeacon > 2) */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001181
1182 /* Timestamp */
1183 bss->last_scanned = jiffies;
1184
Holger Schurig9012b282007-05-25 11:27:16 -04001185 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001186
Holger Schurig9012b282007-05-25 11:27:16 -04001187done:
1188 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1189 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001190}
1191
1192/**
1193 * @brief Compare two SSIDs
1194 *
1195 * @param ssid1 A pointer to ssid to compare
1196 * @param ssid2 A pointer to ssid to compare
1197 *
1198 * @return 0--ssid is same, otherwise is different
1199 */
Dan Williams717c9332007-05-29 00:03:31 -04001200int libertas_ssid_cmp(u8 *ssid1, u8 ssid1_len, u8 *ssid2, u8 ssid2_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001201{
Dan Williamsd8efea22007-05-28 23:54:55 -04001202 if (ssid1_len != ssid2_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001203 return -1;
1204
Dan Williamsd8efea22007-05-28 23:54:55 -04001205 return memcmp(ssid1, ssid2, ssid1_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001206}
1207
1208/**
1209 * @brief This function finds a specific compatible BSSID in the scan list
1210 *
1211 * @param adapter A pointer to wlan_adapter
1212 * @param bssid BSSID to find in the scan list
1213 * @param mode Network mode: Infrastructure or IBSS
1214 *
1215 * @return index in BSSID list, or error return code (< 0)
1216 */
Dan Williams717c9332007-05-29 00:03:31 -04001217struct bss_descriptor * libertas_find_bssid_in_list(wlan_adapter * adapter,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001218 u8 * bssid, u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001219{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001220 struct bss_descriptor * iter_bss;
1221 struct bss_descriptor * found_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001222
1223 if (!bssid)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001224 return NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001225
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001226 lbs_dbg_hex("libertas_find_BSSID_in_list: looking for ",
1227 bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001228
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001229 /* Look through the scan table for a compatible match. The loop will
1230 * continue past a matched bssid that is not compatible in case there
1231 * is an AP with multiple SSIDs assigned to the same BSSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001232 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001233 mutex_lock(&adapter->lock);
1234 list_for_each_entry (iter_bss, &adapter->network_list, list) {
Dan Williams3cf209312007-05-25 17:28:30 -04001235 if (compare_ether_addr(iter_bss->bssid, bssid))
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001236 continue; /* bssid doesn't match */
1237 switch (mode) {
1238 case IW_MODE_INFRA:
1239 case IW_MODE_ADHOC:
1240 if (!is_network_compatible(adapter, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001241 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001242 found_bss = iter_bss;
1243 break;
1244 default:
1245 found_bss = iter_bss;
1246 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001247 }
1248 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001249 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001250
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001251 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001252}
1253
1254/**
1255 * @brief This function finds ssid in ssid list.
1256 *
1257 * @param adapter A pointer to wlan_adapter
1258 * @param ssid SSID to find in the list
1259 * @param bssid BSSID to qualify the SSID selection (if provided)
1260 * @param mode Network mode: Infrastructure or IBSS
1261 *
1262 * @return index in BSSID list
1263 */
Dan Williams717c9332007-05-29 00:03:31 -04001264struct bss_descriptor * libertas_find_ssid_in_list(wlan_adapter * adapter,
Dan Williamsd8efea22007-05-28 23:54:55 -04001265 u8 *ssid, u8 ssid_len, u8 * bssid, u8 mode,
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001266 int channel)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001267{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001268 u8 bestrssi = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001269 struct bss_descriptor * iter_bss = NULL;
1270 struct bss_descriptor * found_bss = NULL;
1271 struct bss_descriptor * tmp_oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001272
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001273 mutex_lock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001274
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001275 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1276 if ( !tmp_oldest
1277 || (iter_bss->last_scanned < tmp_oldest->last_scanned))
1278 tmp_oldest = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001279
Dan Williams717c9332007-05-29 00:03:31 -04001280 if (libertas_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001281 ssid, ssid_len) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001282 continue; /* ssid doesn't match */
Dan Williams3cf209312007-05-25 17:28:30 -04001283 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001284 continue; /* bssid doesn't match */
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001285 if ((channel > 0) && (iter_bss->channel != channel))
1286 continue; /* channel doesn't match */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001287
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001288 switch (mode) {
1289 case IW_MODE_INFRA:
1290 case IW_MODE_ADHOC:
1291 if (!is_network_compatible(adapter, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001292 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001293
1294 if (bssid) {
1295 /* Found requested BSSID */
1296 found_bss = iter_bss;
1297 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001298 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001299
1300 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1301 bestrssi = SCAN_RSSI(iter_bss->rssi);
1302 found_bss = iter_bss;
1303 }
1304 break;
1305 case IW_MODE_AUTO:
1306 default:
1307 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1308 bestrssi = SCAN_RSSI(iter_bss->rssi);
1309 found_bss = iter_bss;
1310 }
1311 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001312 }
1313 }
1314
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001315out:
1316 mutex_unlock(&adapter->lock);
1317 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001318}
1319
1320/**
1321 * @brief This function finds the best SSID in the Scan List
1322 *
1323 * Search the scan table for the best SSID that also matches the current
1324 * adapter network preference (infrastructure or adhoc)
1325 *
1326 * @param adapter A pointer to wlan_adapter
1327 *
1328 * @return index in BSSID list
1329 */
Dan Williams717c9332007-05-29 00:03:31 -04001330struct bss_descriptor * libertas_find_best_ssid_in_list(wlan_adapter * adapter,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001331 u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001332{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001333 u8 bestrssi = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001334 struct bss_descriptor * iter_bss;
1335 struct bss_descriptor * best_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001336
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001337 mutex_lock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001338
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001339 list_for_each_entry (iter_bss, &adapter->network_list, list) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001340 switch (mode) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001341 case IW_MODE_INFRA:
1342 case IW_MODE_ADHOC:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001343 if (!is_network_compatible(adapter, iter_bss, mode))
1344 break;
1345 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1346 break;
1347 bestrssi = SCAN_RSSI(iter_bss->rssi);
1348 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001349 break;
Dan Williams0dc5a292007-05-10 22:58:02 -04001350 case IW_MODE_AUTO:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001351 default:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001352 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1353 break;
1354 bestrssi = SCAN_RSSI(iter_bss->rssi);
1355 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001356 break;
1357 }
1358 }
1359
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001360 mutex_unlock(&adapter->lock);
1361 return best_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001362}
1363
1364/**
1365 * @brief Find the AP with specific ssid in the scan list
1366 *
1367 * @param priv A pointer to wlan_private structure
1368 * @param pSSID A pointer to AP's ssid
1369 *
1370 * @return 0--success, otherwise--fail
1371 */
Dan Williams717c9332007-05-29 00:03:31 -04001372int libertas_find_best_network_ssid(wlan_private * priv,
Dan Williamsd8efea22007-05-28 23:54:55 -04001373 u8 *out_ssid, u8 *out_ssid_len, u8 preferred_mode, u8 *out_mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001374{
1375 wlan_adapter *adapter = priv->adapter;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001376 int ret = -1;
1377 struct bss_descriptor * found;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001378
Holger Schurig9012b282007-05-25 11:27:16 -04001379 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001380
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001381 wlan_scan_networks(priv, NULL, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001382 if (adapter->surpriseremoved)
1383 return -1;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001384
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001385 wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1386
Dan Williams717c9332007-05-29 00:03:31 -04001387 found = libertas_find_best_ssid_in_list(adapter, preferred_mode);
Dan Williamsd8efea22007-05-28 23:54:55 -04001388 if (found && (found->ssid_len > 0)) {
1389 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1390 *out_ssid_len = found->ssid_len;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001391 *out_mode = found->mode;
1392 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001393 }
1394
Holger Schurig9012b282007-05-25 11:27:16 -04001395 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001396 return ret;
1397}
1398
1399/**
1400 * @brief Scan Network
1401 *
1402 * @param dev A pointer to net_device structure
1403 * @param info A pointer to iw_request_info structure
1404 * @param vwrq A pointer to iw_param structure
1405 * @param extra A pointer to extra data buf
1406 *
1407 * @return 0 --success, otherwise fail
1408 */
1409int libertas_set_scan(struct net_device *dev, struct iw_request_info *info,
1410 struct iw_param *vwrq, char *extra)
1411{
1412 wlan_private *priv = dev->priv;
1413 wlan_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001414
Holger Schurig9012b282007-05-25 11:27:16 -04001415 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001416
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001417 wlan_scan_networks(priv, NULL, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001418
1419 if (adapter->surpriseremoved)
1420 return -1;
1421
Holger Schurig9012b282007-05-25 11:27:16 -04001422 lbs_deb_leave(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001423 return 0;
1424}
1425
1426/**
1427 * @brief Send a scan command for all available channels filtered on a spec
1428 *
1429 * @param priv A pointer to wlan_private structure
1430 * @param prequestedssid A pointer to AP's ssid
1431 * @param keeppreviousscan Flag used to save/clear scan table before scan
1432 *
1433 * @return 0-success, otherwise fail
1434 */
Dan Williams717c9332007-05-29 00:03:31 -04001435int libertas_send_specific_ssid_scan(wlan_private * priv,
Dan Williamsd8efea22007-05-28 23:54:55 -04001436 u8 *ssid, u8 ssid_len, u8 clear_ssid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001437{
1438 wlan_adapter *adapter = priv->adapter;
1439 struct wlan_ioctl_user_scan_cfg scancfg;
Dan Williamseb8f7332007-05-25 16:25:21 -04001440 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001441
Holger Schurig9012b282007-05-25 11:27:16 -04001442 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001443
Dan Williamsd8efea22007-05-28 23:54:55 -04001444 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -04001445 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001446
1447 memset(&scancfg, 0x00, sizeof(scancfg));
Dan Williamsd8efea22007-05-28 23:54:55 -04001448 memcpy(scancfg.ssid, ssid, ssid_len);
1449 scancfg.ssid_len = ssid_len;
Dan Williamseb8f7332007-05-25 16:25:21 -04001450 scancfg.clear_ssid = clear_ssid;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001451
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001452 wlan_scan_networks(priv, &scancfg, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001453 if (adapter->surpriseremoved)
1454 return -1;
1455 wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1456
Dan Williamseb8f7332007-05-25 16:25:21 -04001457out:
Holger Schurig9012b282007-05-25 11:27:16 -04001458 lbs_deb_leave(LBS_DEB_ASSOC);
Dan Williamseb8f7332007-05-25 16:25:21 -04001459 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001460}
1461
1462/**
1463 * @brief scan an AP with specific BSSID
1464 *
1465 * @param priv A pointer to wlan_private structure
1466 * @param bssid A pointer to AP's bssid
1467 * @param keeppreviousscan Flag used to save/clear scan table before scan
1468 *
1469 * @return 0-success, otherwise fail
1470 */
Dan Williams717c9332007-05-29 00:03:31 -04001471int libertas_send_specific_bssid_scan(wlan_private * priv, u8 * bssid, u8 clear_bssid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001472{
1473 struct wlan_ioctl_user_scan_cfg scancfg;
1474
Holger Schurig9012b282007-05-25 11:27:16 -04001475 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001476
Dan Williamseb8f7332007-05-25 16:25:21 -04001477 if (bssid == NULL)
1478 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001479
1480 memset(&scancfg, 0x00, sizeof(scancfg));
Dan Williamseb8f7332007-05-25 16:25:21 -04001481 memcpy(scancfg.bssid, bssid, ETH_ALEN);
1482 scancfg.clear_bssid = clear_bssid;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001483
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001484 wlan_scan_networks(priv, &scancfg, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001485 if (priv->adapter->surpriseremoved)
1486 return -1;
1487 wait_event_interruptible(priv->adapter->cmd_pending,
1488 !priv->adapter->nr_cmd_pending);
1489
Dan Williamseb8f7332007-05-25 16:25:21 -04001490out:
Holger Schurig9012b282007-05-25 11:27:16 -04001491 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001492 return 0;
1493}
1494
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001495static inline char *libertas_translate_scan(wlan_private *priv,
1496 char *start, char *stop,
1497 struct bss_descriptor *bss)
1498{
1499 wlan_adapter *adapter = priv->adapter;
1500 struct chan_freq_power *cfp;
1501 char *current_val; /* For rates */
1502 struct iw_event iwe; /* Temporary buffer */
1503 int j;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001504#define PERFECT_RSSI ((u8)50)
1505#define WORST_RSSI ((u8)0)
1506#define RSSI_DIFF ((u8)(PERFECT_RSSI - WORST_RSSI))
1507 u8 rssi;
1508
1509 cfp = libertas_find_cfp_by_band_and_channel(adapter, 0, bss->channel);
1510 if (!cfp) {
1511 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
1512 return NULL;
1513 }
1514
1515 /* First entry *MUST* be the AP BSSID */
1516 iwe.cmd = SIOCGIWAP;
1517 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1518 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
1519 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
1520
1521 /* SSID */
1522 iwe.cmd = SIOCGIWESSID;
1523 iwe.u.data.flags = 1;
Dan Williamsd8efea22007-05-28 23:54:55 -04001524 iwe.u.data.length = min((u32) bss->ssid_len, (u32) IW_ESSID_MAX_SIZE);
1525 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001526
1527 /* Mode */
1528 iwe.cmd = SIOCGIWMODE;
1529 iwe.u.mode = bss->mode;
1530 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
1531
1532 /* Frequency */
1533 iwe.cmd = SIOCGIWFREQ;
1534 iwe.u.freq.m = (long)cfp->freq * 100000;
1535 iwe.u.freq.e = 1;
1536 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
1537
1538 /* Add quality statistics */
1539 iwe.cmd = IWEVQUAL;
1540 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
1541 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
1542
1543 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
1544 iwe.u.qual.qual =
1545 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
1546 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
1547 (RSSI_DIFF * RSSI_DIFF);
1548 if (iwe.u.qual.qual > 100)
1549 iwe.u.qual.qual = 100;
1550
1551 if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
1552 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
1553 } else {
1554 iwe.u.qual.noise =
1555 CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
1556 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001557
Dan Williams80e78ef2007-05-25 22:18:47 -04001558 /* Locally created ad-hoc BSSs won't have beacons if this is the
1559 * only station in the adhoc network; so get signal strength
1560 * from receive statistics.
1561 */
1562 if ((adapter->mode == IW_MODE_ADHOC)
1563 && adapter->adhoccreate
Dan Williams717c9332007-05-29 00:03:31 -04001564 && !libertas_ssid_cmp(adapter->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001565 adapter->curbssparams.ssid_len,
1566 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -04001567 int snr, nf;
1568 snr = adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1569 nf = adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1570 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001571 }
1572 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
1573
1574 /* Add encryption capability */
1575 iwe.cmd = SIOCGIWENCODE;
1576 if (bss->privacy) {
1577 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1578 } else {
1579 iwe.u.data.flags = IW_ENCODE_DISABLED;
1580 }
1581 iwe.u.data.length = 0;
Dan Williamsd8efea22007-05-28 23:54:55 -04001582 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001583
1584 current_val = start + IW_EV_LCP_LEN;
1585
1586 iwe.cmd = SIOCGIWRATE;
1587 iwe.u.bitrate.fixed = 0;
1588 iwe.u.bitrate.disabled = 0;
1589 iwe.u.bitrate.value = 0;
1590
1591 for (j = 0; j < sizeof(bss->libertas_supported_rates); j++) {
1592 u8 rate = bss->libertas_supported_rates[j];
1593 if (rate == 0)
1594 break; /* no more rates */
1595 /* Bit rate given in 500 kb/s units (+ 0x80) */
1596 iwe.u.bitrate.value = (rate & 0x7f) * 500000;
1597 current_val = iwe_stream_add_value(start, current_val,
1598 stop, &iwe, IW_EV_PARAM_LEN);
1599 }
1600 if ((bss->mode == IW_MODE_ADHOC)
Dan Williams717c9332007-05-29 00:03:31 -04001601 && !libertas_ssid_cmp(adapter->curbssparams.ssid,
Dan Williamsd8efea22007-05-28 23:54:55 -04001602 adapter->curbssparams.ssid_len,
1603 bss->ssid, bss->ssid_len)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001604 && adapter->adhoccreate) {
1605 iwe.u.bitrate.value = 22 * 500000;
1606 current_val = iwe_stream_add_value(start, current_val,
1607 stop, &iwe, IW_EV_PARAM_LEN);
1608 }
1609 /* Check if we added any event */
1610 if((current_val - start) > IW_EV_LCP_LEN)
1611 start = current_val;
1612
1613 memset(&iwe, 0, sizeof(iwe));
1614 if (bss->wpa_ie_len) {
1615 char buf[MAX_WPA_IE_LEN];
1616 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
1617 iwe.cmd = IWEVGENIE;
1618 iwe.u.data.length = bss->wpa_ie_len;
1619 start = iwe_stream_add_point(start, stop, &iwe, buf);
1620 }
1621
1622 memset(&iwe, 0, sizeof(iwe));
1623 if (bss->rsn_ie_len) {
1624 char buf[MAX_WPA_IE_LEN];
1625 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
1626 iwe.cmd = IWEVGENIE;
1627 iwe.u.data.length = bss->rsn_ie_len;
1628 start = iwe_stream_add_point(start, stop, &iwe, buf);
1629 }
1630
1631 return start;
1632}
1633
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001634/**
1635 * @brief Retrieve the scan table entries via wireless tools IOCTL call
1636 *
1637 * @param dev A pointer to net_device structure
1638 * @param info A pointer to iw_request_info structure
1639 * @param dwrq A pointer to iw_point structure
1640 * @param extra A pointer to extra data buf
1641 *
1642 * @return 0 --success, otherwise fail
1643 */
1644int libertas_get_scan(struct net_device *dev, struct iw_request_info *info,
1645 struct iw_point *dwrq, char *extra)
1646{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001647#define SCAN_ITEM_SIZE 128
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001648 wlan_private *priv = dev->priv;
1649 wlan_adapter *adapter = priv->adapter;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001650 int err = 0;
1651 char *ev = extra;
1652 char *stop = ev + dwrq->length;
1653 struct bss_descriptor * iter_bss;
1654 struct bss_descriptor * safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001655
Holger Schurig9012b282007-05-25 11:27:16 -04001656 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001657
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001658 /* If we've got an uncompleted scan, schedule the next part */
1659 if (!adapter->nr_cmd_pending && adapter->last_scanned_channel)
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001660 wlan_scan_networks(priv, NULL, 0);
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001661
Dan Williams80e78ef2007-05-25 22:18:47 -04001662 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001663 if ((adapter->mode == IW_MODE_ADHOC) && adapter->adhoccreate) {
Dan Williams80e78ef2007-05-25 22:18:47 -04001664 libertas_prepare_and_send_command(priv, cmd_802_11_rssi, 0,
1665 cmd_option_waitforrsp, 0, NULL);
1666 }
1667
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001668 mutex_lock(&adapter->lock);
1669 list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1670 char * next_ev;
1671 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001672
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001673 if (stop - ev < SCAN_ITEM_SIZE) {
1674 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001675 break;
1676 }
1677
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001678 /* Prune old an old scan result */
1679 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1680 if (time_after(jiffies, stale_time)) {
1681 list_move_tail (&iter_bss->list,
1682 &adapter->network_free_list);
1683 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001684 continue;
1685 }
1686
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001687 /* Translate to WE format this entry */
1688 next_ev = libertas_translate_scan(priv, ev, stop, iter_bss);
1689 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001690 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001691 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001692 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001693 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001694
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001695 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001696 dwrq->flags = 0;
1697
Holger Schurig9012b282007-05-25 11:27:16 -04001698 lbs_deb_leave(LBS_DEB_ASSOC);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001699 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001700}
1701
1702/**
1703 * @brief Prepare a scan command to be sent to the firmware
1704 *
1705 * Use the wlan_scan_cmd_config sent to the command processing module in
1706 * the libertas_prepare_and_send_command to configure a cmd_ds_802_11_scan command
1707 * struct to send to firmware.
1708 *
1709 * The fixed fields specifying the BSS type and BSSID filters as well as a
1710 * variable number/length of TLVs are sent in the command to firmware.
1711 *
1712 * @param priv A pointer to wlan_private structure
1713 * @param cmd A pointer to cmd_ds_command structure to be sent to
1714 * firmware with the cmd_DS_801_11_SCAN structure
1715 * @param pdata_buf Void pointer cast of a wlan_scan_cmd_config struct used
1716 * to set the fields/TLVs for the command sent to firmware
1717 *
1718 * @return 0 or -1
1719 *
1720 * @sa wlan_scan_create_channel_list
1721 */
1722int libertas_cmd_80211_scan(wlan_private * priv,
1723 struct cmd_ds_command *cmd, void *pdata_buf)
1724{
1725 struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
1726 struct wlan_scan_cmd_config *pscancfg;
1727
Holger Schurig9012b282007-05-25 11:27:16 -04001728 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001729
1730 pscancfg = pdata_buf;
1731
1732 /* Set fixed field variables in scan command */
1733 pscan->bsstype = pscancfg->bsstype;
Dan Williamseb8f7332007-05-25 16:25:21 -04001734 memcpy(pscan->BSSID, pscancfg->bssid, sizeof(pscan->BSSID));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001735 memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
1736
1737 cmd->command = cpu_to_le16(cmd_802_11_scan);
1738
1739 /* size is equal to the sizeof(fixed portions) + the TLV len + header */
1740 cmd->size = cpu_to_le16(sizeof(pscan->bsstype)
1741 + sizeof(pscan->BSSID)
1742 + pscancfg->tlvbufferlen + S_DS_GEN);
1743
Holger Schurig9012b282007-05-25 11:27:16 -04001744 lbs_deb_scan("SCAN_CMD: command=%x, size=%x, seqnum=%x\n",
David Woodhouse981f1872007-05-25 23:36:54 -04001745 le16_to_cpu(cmd->command), le16_to_cpu(cmd->size),
1746 le16_to_cpu(cmd->seqnum));
Holger Schurig9012b282007-05-25 11:27:16 -04001747
1748 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001749 return 0;
1750}
1751
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001752static inline int is_same_network(struct bss_descriptor *src,
1753 struct bss_descriptor *dst)
1754{
1755 /* A network is only a duplicate if the channel, BSSID, and ESSID
1756 * all match. We treat all <hidden> with the same BSSID and channel
1757 * as one network */
Dan Williamsd8efea22007-05-28 23:54:55 -04001758 return ((src->ssid_len == dst->ssid_len) &&
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001759 (src->channel == dst->channel) &&
1760 !compare_ether_addr(src->bssid, dst->bssid) &&
Dan Williamsd8efea22007-05-28 23:54:55 -04001761 !memcmp(src->ssid, dst->ssid, src->ssid_len));
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001762}
1763
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001764/**
1765 * @brief This function handles the command response of scan
1766 *
1767 * The response buffer for the scan command has the following
1768 * memory layout:
1769 *
1770 * .-----------------------------------------------------------.
1771 * | header (4 * sizeof(u16)): Standard command response hdr |
1772 * .-----------------------------------------------------------.
1773 * | bufsize (u16) : sizeof the BSS Description data |
1774 * .-----------------------------------------------------------.
1775 * | NumOfSet (u8) : Number of BSS Descs returned |
1776 * .-----------------------------------------------------------.
1777 * | BSSDescription data (variable, size given in bufsize) |
1778 * .-----------------------------------------------------------.
1779 * | TLV data (variable, size calculated using header->size, |
1780 * | bufsize and sizeof the fixed fields above) |
1781 * .-----------------------------------------------------------.
1782 *
1783 * @param priv A pointer to wlan_private structure
1784 * @param resp A pointer to cmd_ds_command
1785 *
1786 * @return 0 or -1
1787 */
1788int libertas_ret_80211_scan(wlan_private * priv, struct cmd_ds_command *resp)
1789{
1790 wlan_adapter *adapter = priv->adapter;
1791 struct cmd_ds_802_11_scan_rsp *pscan;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001792 struct mrvlietypes_data *ptlv;
1793 struct mrvlietypes_tsftimestamp *ptsftlv;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001794 struct bss_descriptor * iter_bss;
1795 struct bss_descriptor * safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001796 u8 *pbssinfo;
1797 u16 scanrespsize;
1798 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001799 int idx;
1800 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001801 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001802
Holger Schurig9012b282007-05-25 11:27:16 -04001803 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001804
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001805 /* Prune old entries from scan table */
1806 list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1807 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1808 if (time_before(jiffies, stale_time))
1809 continue;
1810 list_move_tail (&iter_bss->list, &adapter->network_free_list);
1811 clear_bss_descriptor(iter_bss);
1812 }
1813
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001814 pscan = &resp->params.scanresp;
1815
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001816 if (pscan->nr_sets > MAX_NETWORK_COUNT) {
1817 lbs_deb_scan(
1818 "SCAN_RESP: too many scan results (%d, max %d)!!\n",
1819 pscan->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001820 ret = -1;
1821 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001822 }
1823
1824 bytesleft = le16_to_cpu(pscan->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001825 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001826
1827 scanrespsize = le16_to_cpu(resp->size);
Holger Schurig9012b282007-05-25 11:27:16 -04001828 lbs_deb_scan("SCAN_RESP: returned %d AP before parsing\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001829 pscan->nr_sets);
1830
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001831 pbssinfo = pscan->bssdesc_and_tlvbuffer;
1832
1833 /* The size of the TLV buffer is equal to the entire command response
1834 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1835 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1836 * response header (S_DS_GEN)
1837 */
1838 tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
1839 + sizeof(pscan->nr_sets)
1840 + S_DS_GEN);
1841
1842 ptlv = (struct mrvlietypes_data *) (pscan->bssdesc_and_tlvbuffer + bytesleft);
1843
1844 /* Search the TLV buffer space in the scan response for any valid TLVs */
1845 wlan_ret_802_11_scan_get_tlv_ptrs(ptlv, tlvbufsize, &ptsftlv);
1846
1847 /*
1848 * Process each scan response returned (pscan->nr_sets). Save
1849 * the information in the newbssentry and then insert into the
1850 * driver scan table either as an update to an existing entry
1851 * or as an addition at the end of the table
1852 */
1853 for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001854 struct bss_descriptor new;
1855 struct bss_descriptor * found = NULL;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001856 struct bss_descriptor * oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001857
1858 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001859 memset(&new, 0, sizeof (struct bss_descriptor));
1860 if (libertas_process_bss(&new, &pbssinfo, &bytesleft) != 0) {
1861 /* error parsing the scan response, skipped */
1862 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1863 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001864 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001865
1866 /* Try to find this bss in the scan table */
1867 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1868 if (is_same_network(iter_bss, &new)) {
1869 found = iter_bss;
1870 break;
1871 }
1872
1873 if ((oldest == NULL) ||
1874 (iter_bss->last_scanned < oldest->last_scanned))
1875 oldest = iter_bss;
1876 }
1877
1878 if (found) {
1879 /* found, clear it */
1880 clear_bss_descriptor(found);
1881 } else if (!list_empty(&adapter->network_free_list)) {
1882 /* Pull one from the free list */
1883 found = list_entry(adapter->network_free_list.next,
1884 struct bss_descriptor, list);
1885 list_move_tail(&found->list, &adapter->network_list);
1886 } else if (oldest) {
1887 /* If there are no more slots, expire the oldest */
1888 found = oldest;
1889 clear_bss_descriptor(found);
1890 list_move_tail(&found->list, &adapter->network_list);
1891 } else {
1892 continue;
1893 }
1894
1895 lbs_deb_scan("SCAN_RESP: BSSID = " MAC_FMT "\n",
1896 new.bssid[0], new.bssid[1], new.bssid[2],
1897 new.bssid[3], new.bssid[4], new.bssid[5]);
1898
1899 /*
1900 * If the TSF TLV was appended to the scan results, save the
1901 * this entries TSF value in the networktsf field. The
1902 * networktsf is the firmware's TSF value at the time the
1903 * beacon or probe response was received.
1904 */
1905 if (ptsftlv) {
David Woodhouse981f1872007-05-25 23:36:54 -04001906 new.networktsf = le64_to_cpup(&ptsftlv->tsftable[idx]);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001907 }
1908
1909 /* Copy the locally created newbssentry to the scan table */
1910 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001911 }
1912
Holger Schurig9012b282007-05-25 11:27:16 -04001913 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001914
Holger Schurig9012b282007-05-25 11:27:16 -04001915done:
1916 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1917 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001918}