blob: 9799d87aaa3b4372c09d7bbceec82ce8f9c9e226 [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;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200235
Dan Williamse76850d2007-05-25 17:09:41 -0400236 if (adapter->connect_status == libertas_connected)
237 return;
238
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400239 mutex_lock(&adapter->lock);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400240 list_for_each_entry (iter_bss, &adapter->network_list, list) {
241 lbs_deb_scan("Scan:(%02d) " MAC_FMT ", RSSI[%03d], SSID[%s]\n",
Dan Williams02eb2292007-05-25 17:27:31 -0400242 i++, MAC_ARG(iter_bss->bssid), (s32) iter_bss->rssi,
243 iter_bss->ssid.ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200244 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400245 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200246}
247
248/**
249 * @brief Create a channel list for the driver to scan based on region info
250 *
251 * Use the driver region/band information to construct a comprehensive list
252 * of channels to scan. This routine is used for any scan that is not
253 * provided a specific channel list to scan.
254 *
255 * @param priv A pointer to wlan_private structure
256 * @param scanchanlist Output parameter: resulting channel list to scan
257 * @param filteredscan Flag indicating whether or not a BSSID or SSID filter
258 * is being sent in the command to firmware. Used to
259 * increase the number of channels sent in a scan
260 * command and to disable the firmware channel scan
261 * filter.
262 *
263 * @return void
264 */
265static void wlan_scan_create_channel_list(wlan_private * priv,
266 struct chanscanparamset * scanchanlist,
267 u8 filteredscan)
268{
269
270 wlan_adapter *adapter = priv->adapter;
271 struct region_channel *scanregion;
272 struct chan_freq_power *cfp;
273 int rgnidx;
274 int chanidx;
275 int nextchan;
276 u8 scantype;
277
278 chanidx = 0;
279
280 /* Set the default scan type to the user specified type, will later
281 * be changed to passive on a per channel basis if restricted by
282 * regulatory requirements (11d or 11h)
283 */
284 scantype = adapter->scantype;
285
286 for (rgnidx = 0; rgnidx < ARRAY_SIZE(adapter->region_channel); rgnidx++) {
287 if (priv->adapter->enable11d &&
288 adapter->connect_status != libertas_connected) {
289 /* Scan all the supported chan for the first scan */
290 if (!adapter->universal_channel[rgnidx].valid)
291 continue;
292 scanregion = &adapter->universal_channel[rgnidx];
293
294 /* clear the parsed_region_chan for the first scan */
295 memset(&adapter->parsed_region_chan, 0x00,
296 sizeof(adapter->parsed_region_chan));
297 } else {
298 if (!adapter->region_channel[rgnidx].valid)
299 continue;
300 scanregion = &adapter->region_channel[rgnidx];
301 }
302
303 for (nextchan = 0;
304 nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
305
306 cfp = scanregion->CFP + nextchan;
307
308 if (priv->adapter->enable11d) {
309 scantype =
310 libertas_get_scan_type_11d(cfp->channel,
311 &adapter->
312 parsed_region_chan);
313 }
314
315 switch (scanregion->band) {
316 case BAND_B:
317 case BAND_G:
318 default:
319 scanchanlist[chanidx].radiotype =
320 cmd_scan_radio_type_bg;
321 break;
322 }
323
324 if (scantype == cmd_scan_type_passive) {
325 scanchanlist[chanidx].maxscantime =
David Woodhouse981f1872007-05-25 23:36:54 -0400326 cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200327 scanchanlist[chanidx].chanscanmode.passivescan =
328 1;
329 } else {
330 scanchanlist[chanidx].maxscantime =
David Woodhouse981f1872007-05-25 23:36:54 -0400331 cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200332 scanchanlist[chanidx].chanscanmode.passivescan =
333 0;
334 }
335
336 scanchanlist[chanidx].channumber = cfp->channel;
337
338 if (filteredscan) {
339 scanchanlist[chanidx].chanscanmode.
340 disablechanfilt = 1;
341 }
342 }
343 }
344}
345
346/**
347 * @brief Construct a wlan_scan_cmd_config structure to use in issue scan cmds
348 *
349 * Application layer or other functions can invoke wlan_scan_networks
350 * with a scan configuration supplied in a wlan_ioctl_user_scan_cfg struct.
351 * This structure is used as the basis of one or many wlan_scan_cmd_config
352 * commands that are sent to the command processing module and sent to
353 * firmware.
354 *
355 * Create a wlan_scan_cmd_config based on the following user supplied
356 * parameters (if present):
357 * - SSID filter
358 * - BSSID filter
359 * - Number of Probes to be sent
360 * - channel list
361 *
362 * If the SSID or BSSID filter is not present, disable/clear the filter.
363 * If the number of probes is not set, use the adapter default setting
364 * Qualify the channel
365 *
366 * @param priv A pointer to wlan_private structure
367 * @param puserscanin NULL or pointer to scan configuration parameters
368 * @param ppchantlvout Output parameter: Pointer to the start of the
369 * channel TLV portion of the output scan config
370 * @param pscanchanlist Output parameter: Pointer to the resulting channel
371 * list to scan
372 * @param pmaxchanperscan Output parameter: Number of channels to scan for
373 * each issuance of the firmware scan command
374 * @param pfilteredscan Output parameter: Flag indicating whether or not
375 * a BSSID or SSID filter is being sent in the
376 * command to firmware. Used to increase the number
377 * of channels sent in a scan command and to
378 * disable the firmware channel scan filter.
379 * @param pscancurrentonly Output parameter: Flag indicating whether or not
380 * we are only scanning our current active channel
381 *
382 * @return resulting scan configuration
383 */
384static struct wlan_scan_cmd_config *
385wlan_scan_setup_scan_config(wlan_private * priv,
386 const struct wlan_ioctl_user_scan_cfg * puserscanin,
387 struct mrvlietypes_chanlistparamset ** ppchantlvout,
388 struct chanscanparamset * pscanchanlist,
389 int *pmaxchanperscan,
390 u8 * pfilteredscan,
391 u8 * pscancurrentonly)
392{
393 wlan_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200394 struct mrvlietypes_numprobes *pnumprobestlv;
395 struct mrvlietypes_ssidparamset *pssidtlv;
396 struct wlan_scan_cmd_config * pscancfgout = NULL;
397 u8 *ptlvpos;
398 u16 numprobes;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200399 int chanidx;
400 int scantype;
401 int scandur;
402 int channel;
403 int radiotype;
404
405 pscancfgout = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
406 if (pscancfgout == NULL)
407 goto out;
408
409 /* The tlvbufferlen is calculated for each scan command. The TLVs added
410 * in this routine will be preserved since the routine that sends
411 * the command will append channelTLVs at *ppchantlvout. The difference
412 * between the *ppchantlvout and the tlvbuffer start will be used
413 * to calculate the size of anything we add in this routine.
414 */
415 pscancfgout->tlvbufferlen = 0;
416
417 /* Running tlv pointer. Assigned to ppchantlvout at end of function
418 * so later routines know where channels can be added to the command buf
419 */
420 ptlvpos = pscancfgout->tlvbuffer;
421
422 /*
423 * Set the initial scan paramters for progressive scanning. If a specific
424 * BSSID or SSID is used, the number of channels in the scan command
425 * will be increased to the absolute maximum
426 */
427 *pmaxchanperscan = MRVDRV_CHANNELS_PER_SCAN_CMD;
428
429 /* Initialize the scan as un-filtered by firmware, set to TRUE below if
430 * a SSID or BSSID filter is sent in the command
431 */
432 *pfilteredscan = 0;
433
434 /* Initialize the scan as not being only on the current channel. If
435 * the channel list is customized, only contains one channel, and
436 * is the active channel, this is set true and data flow is not halted.
437 */
438 *pscancurrentonly = 0;
439
440 if (puserscanin) {
441
442 /* Set the bss type scan filter, use adapter setting if unset */
443 pscancfgout->bsstype =
444 (puserscanin->bsstype ? puserscanin->bsstype : adapter->
445 scanmode);
446
447 /* Set the number of probes to send, use adapter setting if unset */
448 numprobes = (puserscanin->numprobes ? puserscanin->numprobes :
449 adapter->scanprobes);
450
451 /*
452 * Set the BSSID filter to the incoming configuration,
453 * if non-zero. If not set, it will remain disabled (all zeros).
454 */
Dan Williamseb8f7332007-05-25 16:25:21 -0400455 memcpy(pscancfgout->bssid, puserscanin->bssid,
456 sizeof(pscancfgout->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200457
Dan Williamseb8f7332007-05-25 16:25:21 -0400458 if (puserscanin->ssid_len) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200459 pssidtlv =
460 (struct mrvlietypes_ssidparamset *) pscancfgout->
461 tlvbuffer;
462 pssidtlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
Dan Williamseb8f7332007-05-25 16:25:21 -0400463 pssidtlv->header.len = cpu_to_le16(puserscanin->ssid_len);
464 memcpy(pssidtlv->ssid, puserscanin->ssid,
465 puserscanin->ssid_len);
466 ptlvpos += sizeof(pssidtlv->header) + puserscanin->ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200467 }
468
469 /*
470 * The default number of channels sent in the command is low to
471 * ensure the response buffer from the firmware does not truncate
472 * scan results. That is not an issue with an SSID or BSSID
473 * filter applied to the scan results in the firmware.
474 */
Dan Williamseb8f7332007-05-25 16:25:21 -0400475 if ( puserscanin->ssid_len
476 || (compare_ether_addr(pscancfgout->bssid, &zeromac[0]) != 0)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200477 *pmaxchanperscan = MRVDRV_MAX_CHANNELS_PER_SCAN;
478 *pfilteredscan = 1;
479 }
480 } else {
481 pscancfgout->bsstype = adapter->scanmode;
482 numprobes = adapter->scanprobes;
483 }
484
485 /* If the input config or adapter has the number of Probes set, add tlv */
486 if (numprobes) {
487 pnumprobestlv = (struct mrvlietypes_numprobes *) ptlvpos;
David Woodhouse981f1872007-05-25 23:36:54 -0400488 pnumprobestlv->header.type = cpu_to_le16(TLV_TYPE_NUMPROBES);
489 pnumprobestlv->header.len = cpu_to_le16(2);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200490 pnumprobestlv->numprobes = cpu_to_le16(numprobes);
491
David Woodhouse981f1872007-05-25 23:36:54 -0400492 ptlvpos += sizeof(*pnumprobestlv);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200493 }
494
495 /*
496 * Set the output for the channel TLV to the address in the tlv buffer
497 * past any TLVs that were added in this fuction (SSID, numprobes).
498 * channel TLVs will be added past this for each scan command, preserving
499 * the TLVs that were previously added.
500 */
501 *ppchantlvout = (struct mrvlietypes_chanlistparamset *) ptlvpos;
502
503 if (puserscanin && puserscanin->chanlist[0].channumber) {
504
Holger Schurig9012b282007-05-25 11:27:16 -0400505 lbs_deb_scan("Scan: Using supplied channel list\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200506
507 for (chanidx = 0;
508 chanidx < WLAN_IOCTL_USER_SCAN_CHAN_MAX
509 && puserscanin->chanlist[chanidx].channumber; chanidx++) {
510
511 channel = puserscanin->chanlist[chanidx].channumber;
512 (pscanchanlist + chanidx)->channumber = channel;
513
514 radiotype = puserscanin->chanlist[chanidx].radiotype;
515 (pscanchanlist + chanidx)->radiotype = radiotype;
516
517 scantype = puserscanin->chanlist[chanidx].scantype;
518
519 if (scantype == cmd_scan_type_passive) {
520 (pscanchanlist +
521 chanidx)->chanscanmode.passivescan = 1;
522 } else {
523 (pscanchanlist +
524 chanidx)->chanscanmode.passivescan = 0;
525 }
526
527 if (puserscanin->chanlist[chanidx].scantime) {
528 scandur =
529 puserscanin->chanlist[chanidx].scantime;
530 } else {
531 if (scantype == cmd_scan_type_passive) {
532 scandur = MRVDRV_PASSIVE_SCAN_CHAN_TIME;
533 } else {
534 scandur = MRVDRV_ACTIVE_SCAN_CHAN_TIME;
535 }
536 }
537
538 (pscanchanlist + chanidx)->minscantime =
539 cpu_to_le16(scandur);
540 (pscanchanlist + chanidx)->maxscantime =
541 cpu_to_le16(scandur);
542 }
543
544 /* Check if we are only scanning the current channel */
545 if ((chanidx == 1) && (puserscanin->chanlist[0].channumber
546 ==
547 priv->adapter->curbssparams.channel)) {
548 *pscancurrentonly = 1;
Holger Schurig9012b282007-05-25 11:27:16 -0400549 lbs_deb_scan("Scan: Scanning current channel only");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200550 }
551
552 } else {
Holger Schurig9012b282007-05-25 11:27:16 -0400553 lbs_deb_scan("Scan: Creating full region channel list\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200554 wlan_scan_create_channel_list(priv, pscanchanlist,
555 *pfilteredscan);
556 }
557
558out:
559 return pscancfgout;
560}
561
562/**
563 * @brief Construct and send multiple scan config commands to the firmware
564 *
565 * Previous routines have created a wlan_scan_cmd_config with any requested
566 * TLVs. This function splits the channel TLV into maxchanperscan lists
567 * and sends the portion of the channel TLV along with the other TLVs
568 * to the wlan_cmd routines for execution in the firmware.
569 *
570 * @param priv A pointer to wlan_private structure
571 * @param maxchanperscan Maximum number channels to be included in each
572 * scan command sent to firmware
573 * @param filteredscan Flag indicating whether or not a BSSID or SSID
574 * filter is being used for the firmware command
575 * scan command sent to firmware
576 * @param pscancfgout Scan configuration used for this scan.
577 * @param pchantlvout Pointer in the pscancfgout where the channel TLV
578 * should start. This is past any other TLVs that
579 * must be sent down in each firmware command.
580 * @param pscanchanlist List of channels to scan in maxchanperscan segments
581 *
582 * @return 0 or error return otherwise
583 */
584static int wlan_scan_channel_list(wlan_private * priv,
585 int maxchanperscan,
586 u8 filteredscan,
587 struct wlan_scan_cmd_config * pscancfgout,
588 struct mrvlietypes_chanlistparamset * pchantlvout,
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400589 struct chanscanparamset * pscanchanlist,
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400590 const struct wlan_ioctl_user_scan_cfg * puserscanin,
591 int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200592{
593 struct chanscanparamset *ptmpchan;
594 struct chanscanparamset *pstartchan;
595 u8 scanband;
596 int doneearly;
597 int tlvidx;
598 int ret = 0;
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400599 int scanned = 0;
600 union iwreq_data wrqu;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200601
Holger Schurig9012b282007-05-25 11:27:16 -0400602 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200603
604 if (pscancfgout == 0 || pchantlvout == 0 || pscanchanlist == 0) {
Holger Schurig9012b282007-05-25 11:27:16 -0400605 lbs_deb_scan("Scan: Null detect: %p, %p, %p\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200606 pscancfgout, pchantlvout, pscanchanlist);
607 return -1;
608 }
609
610 pchantlvout->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
611
612 /* Set the temp channel struct pointer to the start of the desired list */
613 ptmpchan = pscanchanlist;
614
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400615 if (priv->adapter->last_scanned_channel && !puserscanin)
616 ptmpchan += priv->adapter->last_scanned_channel;
617
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200618 /* Loop through the desired channel list, sending a new firmware scan
619 * commands for each maxchanperscan channels (or for 1,6,11 individually
620 * if configured accordingly)
621 */
622 while (ptmpchan->channumber) {
623
624 tlvidx = 0;
625 pchantlvout->header.len = 0;
626 scanband = ptmpchan->radiotype;
627 pstartchan = ptmpchan;
628 doneearly = 0;
629
630 /* Construct the channel TLV for the scan command. Continue to
631 * insert channel TLVs until:
632 * - the tlvidx hits the maximum configured per scan command
633 * - the next channel to insert is 0 (end of desired channel list)
634 * - doneearly is set (controlling individual scanning of 1,6,11)
635 */
636 while (tlvidx < maxchanperscan && ptmpchan->channumber
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400637 && !doneearly && scanned < 2) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200638
Holger Schurig9012b282007-05-25 11:27:16 -0400639 lbs_deb_scan(
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200640 "Scan: Chan(%3d), Radio(%d), mode(%d,%d), Dur(%d)\n",
641 ptmpchan->channumber, ptmpchan->radiotype,
642 ptmpchan->chanscanmode.passivescan,
643 ptmpchan->chanscanmode.disablechanfilt,
644 ptmpchan->maxscantime);
645
646 /* Copy the current channel TLV to the command being prepared */
647 memcpy(pchantlvout->chanscanparam + tlvidx,
648 ptmpchan, sizeof(pchantlvout->chanscanparam));
649
650 /* Increment the TLV header length by the size appended */
David Woodhouse981f1872007-05-25 23:36:54 -0400651 /* Ew, it would be _so_ nice if we could just declare the
652 variable little-endian and let GCC handle it for us */
653 pchantlvout->header.len =
654 cpu_to_le16(le16_to_cpu(pchantlvout->header.len) +
655 sizeof(pchantlvout->chanscanparam));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200656
657 /*
658 * The tlv buffer length is set to the number of bytes of the
659 * between the channel tlv pointer and the start of the
660 * tlv buffer. This compensates for any TLVs that were appended
661 * before the channel list.
662 */
663 pscancfgout->tlvbufferlen = ((u8 *) pchantlvout
664 - pscancfgout->tlvbuffer);
665
666 /* Add the size of the channel tlv header and the data length */
667 pscancfgout->tlvbufferlen +=
668 (sizeof(pchantlvout->header)
David Woodhouse981f1872007-05-25 23:36:54 -0400669 + le16_to_cpu(pchantlvout->header.len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200670
671 /* Increment the index to the channel tlv we are constructing */
672 tlvidx++;
673
674 doneearly = 0;
675
676 /* Stop the loop if the *current* channel is in the 1,6,11 set
677 * and we are not filtering on a BSSID or SSID.
678 */
679 if (!filteredscan && (ptmpchan->channumber == 1
680 || ptmpchan->channumber == 6
681 || ptmpchan->channumber == 11)) {
682 doneearly = 1;
683 }
684
685 /* Increment the tmp pointer to the next channel to be scanned */
686 ptmpchan++;
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400687 scanned++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200688
689 /* Stop the loop if the *next* channel is in the 1,6,11 set.
690 * This will cause it to be the only channel scanned on the next
691 * interation
692 */
693 if (!filteredscan && (ptmpchan->channumber == 1
694 || ptmpchan->channumber == 6
695 || ptmpchan->channumber == 11)) {
696 doneearly = 1;
697 }
698 }
699
700 /* Send the scan command to the firmware with the specified cfg */
701 ret = libertas_prepare_and_send_command(priv, cmd_802_11_scan, 0,
702 0, 0, pscancfgout);
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400703 if (scanned >= 2 && !full_scan) {
Holger Schurig9012b282007-05-25 11:27:16 -0400704 ret = 0;
705 goto done;
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400706 }
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400707 scanned = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200708 }
709
Dan Williamsd9ad2f52007-05-25 22:38:41 -0400710done:
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400711 priv->adapter->last_scanned_channel = ptmpchan->channumber;
712
Dan Williamsd9ad2f52007-05-25 22:38:41 -0400713 /* Tell userspace the scan table has been updated */
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400714 memset(&wrqu, 0, sizeof(union iwreq_data));
Holger Schurig634b8f42007-05-25 13:05:16 -0400715 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400716
Holger Schurig9012b282007-05-25 11:27:16 -0400717 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200718 return ret;
719}
720
Dan Williamseb8f7332007-05-25 16:25:21 -0400721static void
722clear_selected_scan_list_entries(wlan_adapter * adapter,
723 const struct wlan_ioctl_user_scan_cfg * scan_cfg)
724{
725 struct bss_descriptor * bss;
726 struct bss_descriptor * safe;
727 u32 clear_ssid_flag = 0, clear_bssid_flag = 0;
728
729 if (!scan_cfg)
730 return;
731
732 if (scan_cfg->clear_ssid && scan_cfg->ssid_len)
733 clear_ssid_flag = 1;
734
735 if (scan_cfg->clear_bssid
736 && (compare_ether_addr(scan_cfg->bssid, &zeromac[0]) != 0)
737 && (compare_ether_addr(scan_cfg->bssid, &bcastmac[0]) != 0)) {
738 clear_bssid_flag = 1;
739 }
740
741 if (!clear_ssid_flag && !clear_bssid_flag)
742 return;
743
744 mutex_lock(&adapter->lock);
745 list_for_each_entry_safe (bss, safe, &adapter->network_list, list) {
746 u32 clear = 0;
747
748 /* Check for an SSID match */
749 if ( clear_ssid_flag
750 && (bss->ssid.ssidlength == scan_cfg->ssid_len)
751 && !memcmp(bss->ssid.ssid, scan_cfg->ssid, bss->ssid.ssidlength))
752 clear = 1;
753
754 /* Check for a BSSID match */
755 if ( clear_bssid_flag
756 && !compare_ether_addr(bss->bssid, scan_cfg->bssid))
757 clear = 1;
758
759 if (clear) {
760 list_move_tail (&bss->list, &adapter->network_free_list);
761 clear_bss_descriptor(bss);
762 }
763 }
764 mutex_unlock(&adapter->lock);
765}
766
767
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200768/**
769 * @brief Internal function used to start a scan based on an input config
770 *
771 * Use the input user scan configuration information when provided in
772 * order to send the appropriate scan commands to firmware to populate or
773 * update the internal driver scan table
774 *
775 * @param priv A pointer to wlan_private structure
776 * @param puserscanin Pointer to the input configuration for the requested
777 * scan.
778 *
779 * @return 0 or < 0 if error
780 */
781int wlan_scan_networks(wlan_private * priv,
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400782 const struct wlan_ioctl_user_scan_cfg * puserscanin,
783 int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200784{
Dan Williamseb8f7332007-05-25 16:25:21 -0400785 wlan_adapter * adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200786 struct mrvlietypes_chanlistparamset *pchantlvout;
787 struct chanscanparamset * scan_chan_list = NULL;
788 struct wlan_scan_cmd_config * scan_cfg = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200789 u8 filteredscan;
790 u8 scancurrentchanonly;
791 int maxchanperscan;
792 int ret;
793
Holger Schurig9012b282007-05-25 11:27:16 -0400794 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200795
796 scan_chan_list = kzalloc(sizeof(struct chanscanparamset) *
797 WLAN_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
798 if (scan_chan_list == NULL) {
799 ret = -ENOMEM;
800 goto out;
801 }
802
803 scan_cfg = wlan_scan_setup_scan_config(priv,
804 puserscanin,
805 &pchantlvout,
806 scan_chan_list,
807 &maxchanperscan,
808 &filteredscan,
809 &scancurrentchanonly);
810 if (scan_cfg == NULL) {
811 ret = -ENOMEM;
812 goto out;
813 }
814
Dan Williamseb8f7332007-05-25 16:25:21 -0400815 clear_selected_scan_list_entries(adapter, puserscanin);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200816
817 /* Keep the data path active if we are only scanning our current channel */
818 if (!scancurrentchanonly) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400819 netif_stop_queue(priv->dev);
820 netif_carrier_off(priv->dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400821 netif_stop_queue(priv->mesh_dev);
822 netif_carrier_off(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200823 }
824
825 ret = wlan_scan_channel_list(priv,
826 maxchanperscan,
827 filteredscan,
828 scan_cfg,
829 pchantlvout,
Marcelo Tosatti064827e2007-05-24 23:37:28 -0400830 scan_chan_list,
Marcelo Tosatti2be92192007-05-25 00:33:28 -0400831 puserscanin,
832 full_scan);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200833
834 /* Process the resulting scan table:
835 * - Remove any bad ssids
836 * - Update our current BSS information from scan data
837 */
838 wlan_scan_process_results(priv);
839
840 if (priv->adapter->connect_status == libertas_connected) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400841 netif_carrier_on(priv->dev);
842 netif_wake_queue(priv->dev);
Javier Cardona51d84f52007-05-25 12:06:56 -0400843 netif_carrier_on(priv->mesh_dev);
844 netif_wake_queue(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200845 }
846
847out:
848 if (scan_cfg)
849 kfree(scan_cfg);
850
851 if (scan_chan_list)
852 kfree(scan_chan_list);
853
Holger Schurig9012b282007-05-25 11:27:16 -0400854 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200855 return ret;
856}
857
858/**
859 * @brief Inspect the scan response buffer for pointers to expected TLVs
860 *
861 * TLVs can be included at the end of the scan response BSS information.
862 * Parse the data in the buffer for pointers to TLVs that can potentially
863 * be passed back in the response
864 *
865 * @param ptlv Pointer to the start of the TLV buffer to parse
866 * @param tlvbufsize size of the TLV buffer
867 * @param ptsftlv Output parameter: Pointer to the TSF TLV if found
868 *
869 * @return void
870 */
871static
872void wlan_ret_802_11_scan_get_tlv_ptrs(struct mrvlietypes_data * ptlv,
873 int tlvbufsize,
874 struct mrvlietypes_tsftimestamp ** ptsftlv)
875{
876 struct mrvlietypes_data *pcurrenttlv;
877 int tlvbufleft;
878 u16 tlvtype;
879 u16 tlvlen;
880
881 pcurrenttlv = ptlv;
882 tlvbufleft = tlvbufsize;
883 *ptsftlv = NULL;
884
Holger Schurig9012b282007-05-25 11:27:16 -0400885 lbs_deb_scan("SCAN_RESP: tlvbufsize = %d\n", tlvbufsize);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200886 lbs_dbg_hex("SCAN_RESP: TLV Buf", (u8 *) ptlv, tlvbufsize);
887
888 while (tlvbufleft >= sizeof(struct mrvlietypesheader)) {
889 tlvtype = le16_to_cpu(pcurrenttlv->header.type);
890 tlvlen = le16_to_cpu(pcurrenttlv->header.len);
891
892 switch (tlvtype) {
893 case TLV_TYPE_TSFTIMESTAMP:
894 *ptsftlv = (struct mrvlietypes_tsftimestamp *) pcurrenttlv;
895 break;
896
897 default:
Holger Schurig9012b282007-05-25 11:27:16 -0400898 lbs_deb_scan("SCAN_RESP: Unhandled TLV = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200899 tlvtype);
900 /* Give up, this seems corrupted */
901 return;
902 } /* switch */
903
904 tlvbufleft -= (sizeof(ptlv->header) + tlvlen);
905 pcurrenttlv =
906 (struct mrvlietypes_data *) (pcurrenttlv->Data + tlvlen);
907 } /* while */
908}
909
910/**
911 * @brief Interpret a BSS scan response returned from the firmware
912 *
913 * Parse the various fixed fields and IEs passed back for a a BSS probe
914 * response or beacon from the scan command. Record information as needed
915 * in the scan table struct bss_descriptor for that entry.
916 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400917 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200918 *
919 * @return 0 or -1
920 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400921static int libertas_process_bss(struct bss_descriptor * bss,
922 u8 ** pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200923{
924 enum ieeetypes_elementid elemID;
925 struct ieeetypes_fhparamset *pFH;
926 struct ieeetypes_dsparamset *pDS;
927 struct ieeetypes_cfparamset *pCF;
928 struct ieeetypes_ibssparamset *pibss;
929 struct ieeetypes_capinfo *pcap;
930 struct WLAN_802_11_FIXED_IEs fixedie;
931 u8 *pcurrentptr;
932 u8 *pRate;
933 u8 elemlen;
934 u8 bytestocopy;
935 u8 ratesize;
936 u16 beaconsize;
937 u8 founddatarateie;
938 int bytesleftforcurrentbeacon;
Holger Schurig9012b282007-05-25 11:27:16 -0400939 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200940
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200941 struct IE_WPA *pIe;
942 const u8 oui01[4] = { 0x00, 0x50, 0xf2, 0x01 };
943
944 struct ieeetypes_countryinfoset *pcountryinfo;
945
Holger Schurig9012b282007-05-25 11:27:16 -0400946 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200947
948 founddatarateie = 0;
949 ratesize = 0;
950 beaconsize = 0;
951
952 if (*bytesleft >= sizeof(beaconsize)) {
953 /* Extract & convert beacon size from the command buffer */
David Woodhouse981f1872007-05-25 23:36:54 -0400954 beaconsize = le16_to_cpup((void *)*pbeaconinfo);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200955 *bytesleft -= sizeof(beaconsize);
956 *pbeaconinfo += sizeof(beaconsize);
957 }
958
959 if (beaconsize == 0 || beaconsize > *bytesleft) {
960
961 *pbeaconinfo += *bytesleft;
962 *bytesleft = 0;
963
964 return -1;
965 }
966
967 /* Initialize the current working beacon pointer for this BSS iteration */
968 pcurrentptr = *pbeaconinfo;
969
970 /* Advance the return beacon pointer past the current beacon */
971 *pbeaconinfo += beaconsize;
972 *bytesleft -= beaconsize;
973
974 bytesleftforcurrentbeacon = beaconsize;
975
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400976 memcpy(bss->bssid, pcurrentptr, ETH_ALEN);
Dan Williams02eb2292007-05-25 17:27:31 -0400977 lbs_deb_scan("process_bss: AP BSSID " MAC_FMT "\n", MAC_ARG(bss->bssid));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200978
979 pcurrentptr += ETH_ALEN;
980 bytesleftforcurrentbeacon -= ETH_ALEN;
981
982 if (bytesleftforcurrentbeacon < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400983 lbs_deb_scan("process_bss: Not enough bytes left\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200984 return -1;
985 }
986
987 /*
988 * next 4 fields are RSSI, time stamp, beacon interval,
989 * and capability information
990 */
991
992 /* RSSI is 1 byte long */
David Woodhouse981f1872007-05-25 23:36:54 -0400993 bss->rssi = *pcurrentptr;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400994 lbs_deb_scan("process_bss: RSSI=%02X\n", *pcurrentptr);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200995 pcurrentptr += 1;
996 bytesleftforcurrentbeacon -= 1;
997
998 /* time stamp is 8 bytes long */
David Woodhouse981f1872007-05-25 23:36:54 -0400999 fixedie.timestamp = bss->timestamp = le64_to_cpup((void *)pcurrentptr);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001000 pcurrentptr += 8;
1001 bytesleftforcurrentbeacon -= 8;
1002
1003 /* beacon interval is 2 bytes long */
David Woodhouse981f1872007-05-25 23:36:54 -04001004 fixedie.beaconinterval = bss->beaconperiod = le16_to_cpup((void *)pcurrentptr);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001005 pcurrentptr += 2;
1006 bytesleftforcurrentbeacon -= 2;
1007
1008 /* capability information is 2 bytes long */
David Woodhouse981f1872007-05-25 23:36:54 -04001009 memcpy(&fixedie.capabilities, pcurrentptr, 2);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001010 lbs_deb_scan("process_bss: fixedie.capabilities=0x%X\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001011 fixedie.capabilities);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001012 pcap = (struct ieeetypes_capinfo *) & fixedie.capabilities;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001013 memcpy(&bss->cap, pcap, sizeof(struct ieeetypes_capinfo));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001014 pcurrentptr += 2;
1015 bytesleftforcurrentbeacon -= 2;
1016
1017 /* rest of the current buffer are IE's */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001018 lbs_deb_scan("process_bss: IE length for this AP = %d\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001019 bytesleftforcurrentbeacon);
1020
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001021 lbs_dbg_hex("process_bss: IE info", (u8 *) pcurrentptr,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001022 bytesleftforcurrentbeacon);
1023
1024 if (pcap->privacy) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001025 lbs_deb_scan("process_bss: AP WEP enabled\n");
1026 bss->privacy = wlan802_11privfilter8021xWEP;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001027 } else {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001028 bss->privacy = wlan802_11privfilteracceptall;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001029 }
1030
1031 if (pcap->ibss == 1) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001032 bss->mode = IW_MODE_ADHOC;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001033 } else {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001034 bss->mode = IW_MODE_INFRA;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001035 }
1036
1037 /* process variable IE */
1038 while (bytesleftforcurrentbeacon >= 2) {
1039 elemID = (enum ieeetypes_elementid) (*((u8 *) pcurrentptr));
1040 elemlen = *((u8 *) pcurrentptr + 1);
1041
1042 if (bytesleftforcurrentbeacon < elemlen) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001043 lbs_deb_scan("process_bss: error in processing IE, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001044 "bytes left < IE length\n");
1045 bytesleftforcurrentbeacon = 0;
1046 continue;
1047 }
1048
1049 switch (elemID) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001050 case SSID:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001051 bss->ssid.ssidlength = elemlen;
1052 memcpy(bss->ssid.ssid, (pcurrentptr + 2), elemlen);
1053 lbs_deb_scan("ssid '%s'\n", bss->ssid.ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001054 break;
1055
1056 case SUPPORTED_RATES:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001057 memcpy(bss->datarates, (pcurrentptr + 2), elemlen);
1058 memmove(bss->libertas_supported_rates, (pcurrentptr + 2),
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001059 elemlen);
1060 ratesize = elemlen;
1061 founddatarateie = 1;
1062 break;
1063
1064 case EXTRA_IE:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001065 lbs_deb_scan("process_bss: EXTRA_IE Found!\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001066 break;
1067
1068 case FH_PARAM_SET:
1069 pFH = (struct ieeetypes_fhparamset *) pcurrentptr;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001070 memmove(&bss->phyparamset.fhparamset, pFH,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001071 sizeof(struct ieeetypes_fhparamset));
David Woodhouse981f1872007-05-25 23:36:54 -04001072#if 0 /* I think we can store these LE */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001073 bss->phyparamset.fhparamset.dwelltime
1074 = le16_to_cpu(bss->phyparamset.fhparamset.dwelltime);
David Woodhouse981f1872007-05-25 23:36:54 -04001075#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001076 break;
1077
1078 case DS_PARAM_SET:
1079 pDS = (struct ieeetypes_dsparamset *) pcurrentptr;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001080 bss->channel = pDS->currentchan;
1081 memcpy(&bss->phyparamset.dsparamset, pDS,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001082 sizeof(struct ieeetypes_dsparamset));
1083 break;
1084
1085 case CF_PARAM_SET:
1086 pCF = (struct ieeetypes_cfparamset *) pcurrentptr;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001087 memcpy(&bss->ssparamset.cfparamset, pCF,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001088 sizeof(struct ieeetypes_cfparamset));
1089 break;
1090
1091 case IBSS_PARAM_SET:
1092 pibss = (struct ieeetypes_ibssparamset *) pcurrentptr;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001093 bss->atimwindow = le32_to_cpu(pibss->atimwindow);
1094 memmove(&bss->ssparamset.ibssparamset, pibss,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001095 sizeof(struct ieeetypes_ibssparamset));
David Woodhouse981f1872007-05-25 23:36:54 -04001096#if 0
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001097 bss->ssparamset.ibssparamset.atimwindow
1098 = le16_to_cpu(bss->ssparamset.ibssparamset.atimwindow);
David Woodhouse981f1872007-05-25 23:36:54 -04001099#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001100 break;
1101
1102 /* Handle Country Info IE */
1103 case COUNTRY_INFO:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001104 pcountryinfo = (struct ieeetypes_countryinfoset *) pcurrentptr;
1105 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001106 || pcountryinfo->len > 254) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001107 lbs_deb_scan("process_bss: 11D- Err "
Dan Williams4269e2a2007-05-10 23:10:18 -04001108 "CountryInfo len =%d min=%zd max=254\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001109 pcountryinfo->len,
1110 sizeof(pcountryinfo->countrycode));
Holger Schurig9012b282007-05-25 11:27:16 -04001111 ret = -1;
1112 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001113 }
1114
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001115 memcpy(&bss->countryinfo,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001116 pcountryinfo, pcountryinfo->len + 2);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001117 lbs_dbg_hex("process_bss: 11D- CountryInfo:",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001118 (u8 *) pcountryinfo,
1119 (u32) (pcountryinfo->len + 2));
1120 break;
1121
1122 case EXTENDED_SUPPORTED_RATES:
1123 /*
1124 * only process extended supported rate
1125 * if data rate is already found.
1126 * data rate IE should come before
1127 * extended supported rate IE
1128 */
1129 if (founddatarateie) {
1130 if ((elemlen + ratesize) > WLAN_SUPPORTED_RATES) {
1131 bytestocopy =
1132 (WLAN_SUPPORTED_RATES - ratesize);
1133 } else {
1134 bytestocopy = elemlen;
1135 }
1136
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001137 pRate = (u8 *) bss->datarates;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001138 pRate += ratesize;
1139 memmove(pRate, (pcurrentptr + 2), bytestocopy);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001140 pRate = (u8 *) bss->libertas_supported_rates;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001141 pRate += ratesize;
1142 memmove(pRate, (pcurrentptr + 2), bytestocopy);
1143 }
1144 break;
1145
1146 case VENDOR_SPECIFIC_221:
1147#define IE_ID_LEN_FIELDS_BYTES 2
1148 pIe = (struct IE_WPA *)pcurrentptr;
1149
Dan Williams51b0c9d2007-05-10 22:51:28 -04001150 if (memcmp(pIe->oui, oui01, sizeof(oui01)))
1151 break;
1152
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001153 bss->wpa_ie_len = min(elemlen + IE_ID_LEN_FIELDS_BYTES,
1154 MAX_WPA_IE_LEN);
1155 memcpy(bss->wpa_ie, pcurrentptr, bss->wpa_ie_len);
1156 lbs_dbg_hex("process_bss: WPA IE", bss->wpa_ie, elemlen);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001157 break;
1158 case WPA2_IE:
1159 pIe = (struct IE_WPA *)pcurrentptr;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001160 bss->rsn_ie_len = min(elemlen + IE_ID_LEN_FIELDS_BYTES,
1161 MAX_WPA_IE_LEN);
1162 memcpy(bss->rsn_ie, pcurrentptr, bss->rsn_ie_len);
1163 lbs_dbg_hex("process_bss: RSN_IE", bss->rsn_ie, elemlen);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001164 break;
1165 case TIM:
1166 break;
1167
1168 case CHALLENGE_TEXT:
1169 break;
1170 }
1171
1172 pcurrentptr += elemlen + 2;
1173
1174 /* need to account for IE ID and IE len */
1175 bytesleftforcurrentbeacon -= (elemlen + 2);
1176
1177 } /* while (bytesleftforcurrentbeacon > 2) */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001178
1179 /* Timestamp */
1180 bss->last_scanned = jiffies;
1181
Holger Schurig9012b282007-05-25 11:27:16 -04001182 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001183
Holger Schurig9012b282007-05-25 11:27:16 -04001184done:
1185 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1186 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001187}
1188
1189/**
1190 * @brief Compare two SSIDs
1191 *
1192 * @param ssid1 A pointer to ssid to compare
1193 * @param ssid2 A pointer to ssid to compare
1194 *
1195 * @return 0--ssid is same, otherwise is different
1196 */
1197int libertas_SSID_cmp(struct WLAN_802_11_SSID *ssid1, struct WLAN_802_11_SSID *ssid2)
1198{
1199 if (!ssid1 || !ssid2)
1200 return -1;
1201
1202 if (ssid1->ssidlength != ssid2->ssidlength)
1203 return -1;
1204
1205 return memcmp(ssid1->ssid, ssid2->ssid, ssid1->ssidlength);
1206}
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 Williamsfcdb53d2007-05-25 16:15:56 -04001217struct bss_descriptor * libertas_find_BSSID_in_list(wlan_adapter * adapter,
1218 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 Williamsfcdb53d2007-05-25 16:15:56 -04001264struct bss_descriptor * libertas_find_SSID_in_list(wlan_adapter * adapter,
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001265 struct WLAN_802_11_SSID *ssid, u8 * bssid, u8 mode,
1266 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 Williamsfcdb53d2007-05-25 16:15:56 -04001280 if (libertas_SSID_cmp(&iter_bss->ssid, ssid) != 0)
1281 continue; /* ssid doesn't match */
Dan Williams3cf209312007-05-25 17:28:30 -04001282 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001283 continue; /* bssid doesn't match */
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001284 if ((channel > 0) && (iter_bss->channel != channel))
1285 continue; /* channel doesn't match */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001286
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001287 switch (mode) {
1288 case IW_MODE_INFRA:
1289 case IW_MODE_ADHOC:
1290 if (!is_network_compatible(adapter, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001291 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001292
1293 if (bssid) {
1294 /* Found requested BSSID */
1295 found_bss = iter_bss;
1296 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001297 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001298
1299 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1300 bestrssi = SCAN_RSSI(iter_bss->rssi);
1301 found_bss = iter_bss;
1302 }
1303 break;
1304 case IW_MODE_AUTO:
1305 default:
1306 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1307 bestrssi = SCAN_RSSI(iter_bss->rssi);
1308 found_bss = iter_bss;
1309 }
1310 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001311 }
1312 }
1313
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001314out:
1315 mutex_unlock(&adapter->lock);
1316 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001317}
1318
1319/**
1320 * @brief This function finds the best SSID in the Scan List
1321 *
1322 * Search the scan table for the best SSID that also matches the current
1323 * adapter network preference (infrastructure or adhoc)
1324 *
1325 * @param adapter A pointer to wlan_adapter
1326 *
1327 * @return index in BSSID list
1328 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001329struct bss_descriptor * libertas_find_best_SSID_in_list(wlan_adapter * adapter,
1330 u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001331{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001332 u8 bestrssi = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001333 struct bss_descriptor * iter_bss;
1334 struct bss_descriptor * best_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001335
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001336 mutex_lock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001337
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001338 list_for_each_entry (iter_bss, &adapter->network_list, list) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001339 switch (mode) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001340 case IW_MODE_INFRA:
1341 case IW_MODE_ADHOC:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001342 if (!is_network_compatible(adapter, iter_bss, mode))
1343 break;
1344 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1345 break;
1346 bestrssi = SCAN_RSSI(iter_bss->rssi);
1347 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001348 break;
Dan Williams0dc5a292007-05-10 22:58:02 -04001349 case IW_MODE_AUTO:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001350 default:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001351 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1352 break;
1353 bestrssi = SCAN_RSSI(iter_bss->rssi);
1354 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001355 break;
1356 }
1357 }
1358
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001359 mutex_unlock(&adapter->lock);
1360 return best_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001361}
1362
1363/**
1364 * @brief Find the AP with specific ssid in the scan list
1365 *
1366 * @param priv A pointer to wlan_private structure
1367 * @param pSSID A pointer to AP's ssid
1368 *
1369 * @return 0--success, otherwise--fail
1370 */
1371int libertas_find_best_network_SSID(wlan_private * priv,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001372 struct WLAN_802_11_SSID *ssid,
Dan Williams0dc5a292007-05-10 22:58:02 -04001373 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
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001381 memset(ssid, 0, sizeof(struct WLAN_802_11_SSID));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001382
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001383 wlan_scan_networks(priv, NULL, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001384 if (adapter->surpriseremoved)
1385 return -1;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001386
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001387 wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1388
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001389 found = libertas_find_best_SSID_in_list(adapter, preferred_mode);
1390 if (found && (found->ssid.ssidlength > 0)) {
1391 memcpy(ssid, &found->ssid, sizeof(struct WLAN_802_11_SSID));
1392 *out_mode = found->mode;
1393 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001394 }
1395
Holger Schurig9012b282007-05-25 11:27:16 -04001396 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001397 return ret;
1398}
1399
1400/**
1401 * @brief Scan Network
1402 *
1403 * @param dev A pointer to net_device structure
1404 * @param info A pointer to iw_request_info structure
1405 * @param vwrq A pointer to iw_param structure
1406 * @param extra A pointer to extra data buf
1407 *
1408 * @return 0 --success, otherwise fail
1409 */
1410int libertas_set_scan(struct net_device *dev, struct iw_request_info *info,
1411 struct iw_param *vwrq, char *extra)
1412{
1413 wlan_private *priv = dev->priv;
1414 wlan_adapter *adapter = priv->adapter;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001415
Holger Schurig9012b282007-05-25 11:27:16 -04001416 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001417
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001418 wlan_scan_networks(priv, NULL, 0);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001419
1420 if (adapter->surpriseremoved)
1421 return -1;
1422
Holger Schurig9012b282007-05-25 11:27:16 -04001423 lbs_deb_leave(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001424 return 0;
1425}
1426
1427/**
1428 * @brief Send a scan command for all available channels filtered on a spec
1429 *
1430 * @param priv A pointer to wlan_private structure
1431 * @param prequestedssid A pointer to AP's ssid
1432 * @param keeppreviousscan Flag used to save/clear scan table before scan
1433 *
1434 * @return 0-success, otherwise fail
1435 */
1436int libertas_send_specific_SSID_scan(wlan_private * priv,
1437 struct WLAN_802_11_SSID *prequestedssid,
Dan Williamseb8f7332007-05-25 16:25:21 -04001438 u8 clear_ssid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001439{
1440 wlan_adapter *adapter = priv->adapter;
1441 struct wlan_ioctl_user_scan_cfg scancfg;
Dan Williamseb8f7332007-05-25 16:25:21 -04001442 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001443
Holger Schurig9012b282007-05-25 11:27:16 -04001444 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001445
Dan Williamseb8f7332007-05-25 16:25:21 -04001446 if (prequestedssid == NULL)
1447 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001448
1449 memset(&scancfg, 0x00, sizeof(scancfg));
Dan Williamseb8f7332007-05-25 16:25:21 -04001450 memcpy(scancfg.ssid, prequestedssid->ssid, prequestedssid->ssidlength);
1451 scancfg.ssid_len = prequestedssid->ssidlength;
1452 scancfg.clear_ssid = clear_ssid;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001453
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001454 wlan_scan_networks(priv, &scancfg, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001455 if (adapter->surpriseremoved)
1456 return -1;
1457 wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1458
Dan Williamseb8f7332007-05-25 16:25:21 -04001459out:
Holger Schurig9012b282007-05-25 11:27:16 -04001460 lbs_deb_leave(LBS_DEB_ASSOC);
Dan Williamseb8f7332007-05-25 16:25:21 -04001461 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001462}
1463
1464/**
1465 * @brief scan an AP with specific BSSID
1466 *
1467 * @param priv A pointer to wlan_private structure
1468 * @param bssid A pointer to AP's bssid
1469 * @param keeppreviousscan Flag used to save/clear scan table before scan
1470 *
1471 * @return 0-success, otherwise fail
1472 */
Dan Williamseb8f7332007-05-25 16:25:21 -04001473int libertas_send_specific_BSSID_scan(wlan_private * priv, u8 * bssid, u8 clear_bssid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001474{
1475 struct wlan_ioctl_user_scan_cfg scancfg;
1476
Holger Schurig9012b282007-05-25 11:27:16 -04001477 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001478
Dan Williamseb8f7332007-05-25 16:25:21 -04001479 if (bssid == NULL)
1480 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001481
1482 memset(&scancfg, 0x00, sizeof(scancfg));
Dan Williamseb8f7332007-05-25 16:25:21 -04001483 memcpy(scancfg.bssid, bssid, ETH_ALEN);
1484 scancfg.clear_bssid = clear_bssid;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001485
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001486 wlan_scan_networks(priv, &scancfg, 1);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001487 if (priv->adapter->surpriseremoved)
1488 return -1;
1489 wait_event_interruptible(priv->adapter->cmd_pending,
1490 !priv->adapter->nr_cmd_pending);
1491
Dan Williamseb8f7332007-05-25 16:25:21 -04001492out:
Holger Schurig9012b282007-05-25 11:27:16 -04001493 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001494 return 0;
1495}
1496
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001497static inline char *libertas_translate_scan(wlan_private *priv,
1498 char *start, char *stop,
1499 struct bss_descriptor *bss)
1500{
1501 wlan_adapter *adapter = priv->adapter;
1502 struct chan_freq_power *cfp;
1503 char *current_val; /* For rates */
1504 struct iw_event iwe; /* Temporary buffer */
1505 int j;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001506#define PERFECT_RSSI ((u8)50)
1507#define WORST_RSSI ((u8)0)
1508#define RSSI_DIFF ((u8)(PERFECT_RSSI - WORST_RSSI))
1509 u8 rssi;
1510
1511 cfp = libertas_find_cfp_by_band_and_channel(adapter, 0, bss->channel);
1512 if (!cfp) {
1513 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
1514 return NULL;
1515 }
1516
1517 /* First entry *MUST* be the AP BSSID */
1518 iwe.cmd = SIOCGIWAP;
1519 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1520 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
1521 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
1522
1523 /* SSID */
1524 iwe.cmd = SIOCGIWESSID;
1525 iwe.u.data.flags = 1;
1526 iwe.u.data.length = min(bss->ssid.ssidlength, (u32) IW_ESSID_MAX_SIZE);
1527 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid.ssid);
1528
1529 /* Mode */
1530 iwe.cmd = SIOCGIWMODE;
1531 iwe.u.mode = bss->mode;
1532 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
1533
1534 /* Frequency */
1535 iwe.cmd = SIOCGIWFREQ;
1536 iwe.u.freq.m = (long)cfp->freq * 100000;
1537 iwe.u.freq.e = 1;
1538 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
1539
1540 /* Add quality statistics */
1541 iwe.cmd = IWEVQUAL;
1542 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
1543 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
1544
1545 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
1546 iwe.u.qual.qual =
1547 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
1548 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
1549 (RSSI_DIFF * RSSI_DIFF);
1550 if (iwe.u.qual.qual > 100)
1551 iwe.u.qual.qual = 100;
1552
1553 if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
1554 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
1555 } else {
1556 iwe.u.qual.noise =
1557 CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
1558 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001559
Dan Williams80e78ef2007-05-25 22:18:47 -04001560 /* Locally created ad-hoc BSSs won't have beacons if this is the
1561 * only station in the adhoc network; so get signal strength
1562 * from receive statistics.
1563 */
1564 if ((adapter->mode == IW_MODE_ADHOC)
1565 && adapter->adhoccreate
1566 && !libertas_SSID_cmp(&adapter->curbssparams.ssid, &bss->ssid)) {
1567 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;
1582 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid.ssid);
1583
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)
1601 && !libertas_SSID_cmp(&adapter->curbssparams.ssid, &bss->ssid)
1602 && adapter->adhoccreate) {
1603 iwe.u.bitrate.value = 22 * 500000;
1604 current_val = iwe_stream_add_value(start, current_val,
1605 stop, &iwe, IW_EV_PARAM_LEN);
1606 }
1607 /* Check if we added any event */
1608 if((current_val - start) > IW_EV_LCP_LEN)
1609 start = current_val;
1610
1611 memset(&iwe, 0, sizeof(iwe));
1612 if (bss->wpa_ie_len) {
1613 char buf[MAX_WPA_IE_LEN];
1614 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
1615 iwe.cmd = IWEVGENIE;
1616 iwe.u.data.length = bss->wpa_ie_len;
1617 start = iwe_stream_add_point(start, stop, &iwe, buf);
1618 }
1619
1620 memset(&iwe, 0, sizeof(iwe));
1621 if (bss->rsn_ie_len) {
1622 char buf[MAX_WPA_IE_LEN];
1623 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
1624 iwe.cmd = IWEVGENIE;
1625 iwe.u.data.length = bss->rsn_ie_len;
1626 start = iwe_stream_add_point(start, stop, &iwe, buf);
1627 }
1628
1629 return start;
1630}
1631
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001632/**
1633 * @brief Retrieve the scan table entries via wireless tools IOCTL call
1634 *
1635 * @param dev A pointer to net_device structure
1636 * @param info A pointer to iw_request_info structure
1637 * @param dwrq A pointer to iw_point structure
1638 * @param extra A pointer to extra data buf
1639 *
1640 * @return 0 --success, otherwise fail
1641 */
1642int libertas_get_scan(struct net_device *dev, struct iw_request_info *info,
1643 struct iw_point *dwrq, char *extra)
1644{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001645#define SCAN_ITEM_SIZE 128
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001646 wlan_private *priv = dev->priv;
1647 wlan_adapter *adapter = priv->adapter;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001648 int err = 0;
1649 char *ev = extra;
1650 char *stop = ev + dwrq->length;
1651 struct bss_descriptor * iter_bss;
1652 struct bss_descriptor * safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001653
Holger Schurig9012b282007-05-25 11:27:16 -04001654 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001655
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001656 /* If we've got an uncompleted scan, schedule the next part */
1657 if (!adapter->nr_cmd_pending && adapter->last_scanned_channel)
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001658 wlan_scan_networks(priv, NULL, 0);
Marcelo Tosatti2be92192007-05-25 00:33:28 -04001659
Dan Williams80e78ef2007-05-25 22:18:47 -04001660 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001661 if ((adapter->mode == IW_MODE_ADHOC) && adapter->adhoccreate) {
Dan Williams80e78ef2007-05-25 22:18:47 -04001662 libertas_prepare_and_send_command(priv, cmd_802_11_rssi, 0,
1663 cmd_option_waitforrsp, 0, NULL);
1664 }
1665
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001666 mutex_lock(&adapter->lock);
1667 list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1668 char * next_ev;
1669 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001670
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001671 if (stop - ev < SCAN_ITEM_SIZE) {
1672 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001673 break;
1674 }
1675
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001676 /* Prune old an old scan result */
1677 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1678 if (time_after(jiffies, stale_time)) {
1679 list_move_tail (&iter_bss->list,
1680 &adapter->network_free_list);
1681 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001682 continue;
1683 }
1684
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001685 /* Translate to WE format this entry */
1686 next_ev = libertas_translate_scan(priv, ev, stop, iter_bss);
1687 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001688 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001689 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001690 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001691 mutex_unlock(&adapter->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001692
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001693 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001694 dwrq->flags = 0;
1695
Holger Schurig9012b282007-05-25 11:27:16 -04001696 lbs_deb_leave(LBS_DEB_ASSOC);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001697 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001698}
1699
1700/**
1701 * @brief Prepare a scan command to be sent to the firmware
1702 *
1703 * Use the wlan_scan_cmd_config sent to the command processing module in
1704 * the libertas_prepare_and_send_command to configure a cmd_ds_802_11_scan command
1705 * struct to send to firmware.
1706 *
1707 * The fixed fields specifying the BSS type and BSSID filters as well as a
1708 * variable number/length of TLVs are sent in the command to firmware.
1709 *
1710 * @param priv A pointer to wlan_private structure
1711 * @param cmd A pointer to cmd_ds_command structure to be sent to
1712 * firmware with the cmd_DS_801_11_SCAN structure
1713 * @param pdata_buf Void pointer cast of a wlan_scan_cmd_config struct used
1714 * to set the fields/TLVs for the command sent to firmware
1715 *
1716 * @return 0 or -1
1717 *
1718 * @sa wlan_scan_create_channel_list
1719 */
1720int libertas_cmd_80211_scan(wlan_private * priv,
1721 struct cmd_ds_command *cmd, void *pdata_buf)
1722{
1723 struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
1724 struct wlan_scan_cmd_config *pscancfg;
1725
Holger Schurig9012b282007-05-25 11:27:16 -04001726 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001727
1728 pscancfg = pdata_buf;
1729
1730 /* Set fixed field variables in scan command */
1731 pscan->bsstype = pscancfg->bsstype;
Dan Williamseb8f7332007-05-25 16:25:21 -04001732 memcpy(pscan->BSSID, pscancfg->bssid, sizeof(pscan->BSSID));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001733 memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
1734
1735 cmd->command = cpu_to_le16(cmd_802_11_scan);
1736
1737 /* size is equal to the sizeof(fixed portions) + the TLV len + header */
1738 cmd->size = cpu_to_le16(sizeof(pscan->bsstype)
1739 + sizeof(pscan->BSSID)
1740 + pscancfg->tlvbufferlen + S_DS_GEN);
1741
Holger Schurig9012b282007-05-25 11:27:16 -04001742 lbs_deb_scan("SCAN_CMD: command=%x, size=%x, seqnum=%x\n",
David Woodhouse981f1872007-05-25 23:36:54 -04001743 le16_to_cpu(cmd->command), le16_to_cpu(cmd->size),
1744 le16_to_cpu(cmd->seqnum));
Holger Schurig9012b282007-05-25 11:27:16 -04001745
1746 lbs_deb_leave(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001747 return 0;
1748}
1749
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001750static inline int is_same_network(struct bss_descriptor *src,
1751 struct bss_descriptor *dst)
1752{
1753 /* A network is only a duplicate if the channel, BSSID, and ESSID
1754 * all match. We treat all <hidden> with the same BSSID and channel
1755 * as one network */
1756 return ((src->ssid.ssidlength == dst->ssid.ssidlength) &&
1757 (src->channel == dst->channel) &&
1758 !compare_ether_addr(src->bssid, dst->bssid) &&
1759 !memcmp(src->ssid.ssid, dst->ssid.ssid, src->ssid.ssidlength));
1760}
1761
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001762/**
1763 * @brief This function handles the command response of scan
1764 *
1765 * The response buffer for the scan command has the following
1766 * memory layout:
1767 *
1768 * .-----------------------------------------------------------.
1769 * | header (4 * sizeof(u16)): Standard command response hdr |
1770 * .-----------------------------------------------------------.
1771 * | bufsize (u16) : sizeof the BSS Description data |
1772 * .-----------------------------------------------------------.
1773 * | NumOfSet (u8) : Number of BSS Descs returned |
1774 * .-----------------------------------------------------------.
1775 * | BSSDescription data (variable, size given in bufsize) |
1776 * .-----------------------------------------------------------.
1777 * | TLV data (variable, size calculated using header->size, |
1778 * | bufsize and sizeof the fixed fields above) |
1779 * .-----------------------------------------------------------.
1780 *
1781 * @param priv A pointer to wlan_private structure
1782 * @param resp A pointer to cmd_ds_command
1783 *
1784 * @return 0 or -1
1785 */
1786int libertas_ret_80211_scan(wlan_private * priv, struct cmd_ds_command *resp)
1787{
1788 wlan_adapter *adapter = priv->adapter;
1789 struct cmd_ds_802_11_scan_rsp *pscan;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001790 struct mrvlietypes_data *ptlv;
1791 struct mrvlietypes_tsftimestamp *ptsftlv;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001792 struct bss_descriptor * iter_bss;
1793 struct bss_descriptor * safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001794 u8 *pbssinfo;
1795 u16 scanrespsize;
1796 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001797 int idx;
1798 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001799 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001800
Holger Schurig9012b282007-05-25 11:27:16 -04001801 lbs_deb_enter(LBS_DEB_ASSOC);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001802
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001803 /* Prune old entries from scan table */
1804 list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1805 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1806 if (time_before(jiffies, stale_time))
1807 continue;
1808 list_move_tail (&iter_bss->list, &adapter->network_free_list);
1809 clear_bss_descriptor(iter_bss);
1810 }
1811
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001812 pscan = &resp->params.scanresp;
1813
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001814 if (pscan->nr_sets > MAX_NETWORK_COUNT) {
1815 lbs_deb_scan(
1816 "SCAN_RESP: too many scan results (%d, max %d)!!\n",
1817 pscan->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001818 ret = -1;
1819 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001820 }
1821
1822 bytesleft = le16_to_cpu(pscan->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001823 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001824
1825 scanrespsize = le16_to_cpu(resp->size);
Holger Schurig9012b282007-05-25 11:27:16 -04001826 lbs_deb_scan("SCAN_RESP: returned %d AP before parsing\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001827 pscan->nr_sets);
1828
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001829 pbssinfo = pscan->bssdesc_and_tlvbuffer;
1830
1831 /* The size of the TLV buffer is equal to the entire command response
1832 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1833 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1834 * response header (S_DS_GEN)
1835 */
1836 tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
1837 + sizeof(pscan->nr_sets)
1838 + S_DS_GEN);
1839
1840 ptlv = (struct mrvlietypes_data *) (pscan->bssdesc_and_tlvbuffer + bytesleft);
1841
1842 /* Search the TLV buffer space in the scan response for any valid TLVs */
1843 wlan_ret_802_11_scan_get_tlv_ptrs(ptlv, tlvbufsize, &ptsftlv);
1844
1845 /*
1846 * Process each scan response returned (pscan->nr_sets). Save
1847 * the information in the newbssentry and then insert into the
1848 * driver scan table either as an update to an existing entry
1849 * or as an addition at the end of the table
1850 */
1851 for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001852 struct bss_descriptor new;
1853 struct bss_descriptor * found = NULL;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001854 struct bss_descriptor * oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001855
1856 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001857 memset(&new, 0, sizeof (struct bss_descriptor));
1858 if (libertas_process_bss(&new, &pbssinfo, &bytesleft) != 0) {
1859 /* error parsing the scan response, skipped */
1860 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1861 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001862 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001863
1864 /* Try to find this bss in the scan table */
1865 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1866 if (is_same_network(iter_bss, &new)) {
1867 found = iter_bss;
1868 break;
1869 }
1870
1871 if ((oldest == NULL) ||
1872 (iter_bss->last_scanned < oldest->last_scanned))
1873 oldest = iter_bss;
1874 }
1875
1876 if (found) {
1877 /* found, clear it */
1878 clear_bss_descriptor(found);
1879 } else if (!list_empty(&adapter->network_free_list)) {
1880 /* Pull one from the free list */
1881 found = list_entry(adapter->network_free_list.next,
1882 struct bss_descriptor, list);
1883 list_move_tail(&found->list, &adapter->network_list);
1884 } else if (oldest) {
1885 /* If there are no more slots, expire the oldest */
1886 found = oldest;
1887 clear_bss_descriptor(found);
1888 list_move_tail(&found->list, &adapter->network_list);
1889 } else {
1890 continue;
1891 }
1892
1893 lbs_deb_scan("SCAN_RESP: BSSID = " MAC_FMT "\n",
1894 new.bssid[0], new.bssid[1], new.bssid[2],
1895 new.bssid[3], new.bssid[4], new.bssid[5]);
1896
1897 /*
1898 * If the TSF TLV was appended to the scan results, save the
1899 * this entries TSF value in the networktsf field. The
1900 * networktsf is the firmware's TSF value at the time the
1901 * beacon or probe response was received.
1902 */
1903 if (ptsftlv) {
David Woodhouse981f1872007-05-25 23:36:54 -04001904 new.networktsf = le64_to_cpup(&ptsftlv->tsftable[idx]);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001905 }
1906
1907 /* Copy the locally created newbssentry to the scan table */
1908 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001909 }
1910
Holger Schurig9012b282007-05-25 11:27:16 -04001911 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001912
Holger Schurig9012b282007-05-25 11:27:16 -04001913done:
1914 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1915 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001916}