blob: 37b9684bf29c0306eaa07afabe48810175154101 [file] [log] [blame]
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001/**
2 * Functions implementing wlan scan IOCTL and firmware command APIs
3 *
4 * IOCTL handlers as well as command preperation and response routines
5 * for sending scan commands to the firmware.
6 */
7#include <linux/ctype.h>
8#include <linux/if.h>
9#include <linux/netdevice.h>
10#include <linux/wireless.h>
Dan Williamsfcdb53d2007-05-25 16:15:56 -040011#include <linux/etherdevice.h>
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020012
13#include <net/ieee80211.h>
14#include <net/iw_handler.h>
15
Vladimir Davydovac630c22007-09-06 21:45:36 -040016#include <asm/unaligned.h>
17
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020018#include "host.h"
19#include "decl.h"
20#include "dev.h"
21#include "scan.h"
Dan Williams8c512762007-08-02 11:40:45 -040022#include "join.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020023
24//! Approximate amount of data needed to pass a scan result back to iwlist
25#define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
26 + IW_ESSID_MAX_SIZE \
27 + IW_EV_UINT_LEN \
28 + IW_EV_FREQ_LEN \
29 + IW_EV_QUAL_LEN \
30 + IW_ESSID_MAX_SIZE \
31 + IW_EV_PARAM_LEN \
32 + 40) /* 40 for WPAIE */
33
34//! Memory needed to store a max sized channel List TLV for a firmware scan
35#define CHAN_TLV_MAX_SIZE (sizeof(struct mrvlietypesheader) \
36 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
37 * sizeof(struct chanscanparamset)))
38
39//! Memory needed to store a max number/size SSID TLV for a firmware scan
40#define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvlietypes_ssidparamset))
41
Holger Schurig10078322007-11-15 18:05:47 -050042//! Maximum memory needed for a lbs_scan_cmd_config with all TLVs at max
43#define MAX_SCAN_CFG_ALLOC (sizeof(struct lbs_scan_cmd_config) \
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020044 + CHAN_TLV_MAX_SIZE \
45 + SSID_TLV_MAX_SIZE)
46
47//! The maximum number of channels the firmware can scan per command
48#define MRVDRV_MAX_CHANNELS_PER_SCAN 14
49
50/**
51 * @brief Number of channels to scan per firmware scan command issuance.
52 *
53 * Number restricted to prevent hitting the limit on the amount of scan data
54 * returned in a single firmware scan command.
55 */
56#define MRVDRV_CHANNELS_PER_SCAN_CMD 4
57
58//! Scan time specified in the channel TLV for each channel for passive scans
59#define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
60
61//! Scan time specified in the channel TLV for each channel for active scans
62#define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
63
Dan Williams123e0e02007-05-25 23:23:43 -040064static const u8 zeromac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
65static const u8 bcastmac[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Dan Williamseb8f7332007-05-25 16:25:21 -040066
Holger Schurige56188a2007-10-09 14:15:19 +020067
68
69
70/*********************************************************************/
71/* */
72/* Misc helper functions */
73/* */
74/*********************************************************************/
75
Holger Schurig23ff5032008-01-28 17:27:03 +010076/**
77 * @brief Unsets the MSB on basic rates
78 *
79 * Scan through an array and unset the MSB for basic data rates.
80 *
81 * @param rates buffer of data rates
82 * @param len size of buffer
83 */
84static void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
85{
86 int i;
87
88 for (i = 0; i < len; i++)
89 rates[i] &= 0x7f;
90}
91
92
Dan Williamsfcdb53d2007-05-25 16:15:56 -040093static inline void clear_bss_descriptor (struct bss_descriptor * bss)
94{
95 /* Don't blow away ->list, just BSS data */
96 memset(bss, 0, offsetof(struct bss_descriptor, list));
97}
98
Holger Schurigffd074f2007-12-07 16:52:10 +010099/**
100 * @brief Compare two SSIDs
101 *
102 * @param ssid1 A pointer to ssid to compare
103 * @param ssid2 A pointer to ssid to compare
104 *
105 * @return 0: ssid is same, otherwise is different
106 */
107int lbs_ssid_cmp(u8 *ssid1, u8 ssid1_len, u8 *ssid2, u8 ssid2_len)
108{
109 if (ssid1_len != ssid2_len)
110 return -1;
111
112 return memcmp(ssid1, ssid2, ssid1_len);
113}
114
Holger Schurig10078322007-11-15 18:05:47 -0500115static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400116 struct bss_descriptor * match_bss)
117{
118 if ( !secinfo->wep_enabled
119 && !secinfo->WPAenabled
120 && !secinfo->WPA2enabled
Dan Williamsab617972007-08-02 10:48:02 -0400121 && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
122 && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
Dan Williams0c9ca692007-08-02 10:43:44 -0400123 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400124 return 1;
125 }
126 return 0;
127}
128
Holger Schurig10078322007-11-15 18:05:47 -0500129static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400130 struct bss_descriptor * match_bss)
131{
132 if ( secinfo->wep_enabled
133 && !secinfo->WPAenabled
134 && !secinfo->WPA2enabled
Dan Williams0c9ca692007-08-02 10:43:44 -0400135 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400136 return 1;
137 }
138 return 0;
139}
140
Holger Schurig10078322007-11-15 18:05:47 -0500141static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400142 struct bss_descriptor * match_bss)
143{
144 if ( !secinfo->wep_enabled
145 && secinfo->WPAenabled
Dan Williamsab617972007-08-02 10:48:02 -0400146 && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400147 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
Dan Williams0c9ca692007-08-02 10:43:44 -0400148 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
149 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400150 ) {
151 return 1;
152 }
153 return 0;
154}
155
Holger Schurig10078322007-11-15 18:05:47 -0500156static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400157 struct bss_descriptor * match_bss)
158{
159 if ( !secinfo->wep_enabled
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400160 && secinfo->WPA2enabled
Dan Williamsab617972007-08-02 10:48:02 -0400161 && (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400162 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
Dan Williams0c9ca692007-08-02 10:43:44 -0400163 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
164 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400165 ) {
166 return 1;
167 }
168 return 0;
169}
170
Holger Schurig10078322007-11-15 18:05:47 -0500171static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400172 struct bss_descriptor * match_bss)
173{
174 if ( !secinfo->wep_enabled
175 && !secinfo->WPAenabled
176 && !secinfo->WPA2enabled
Dan Williamsab617972007-08-02 10:48:02 -0400177 && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
178 && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
Dan Williams0c9ca692007-08-02 10:43:44 -0400179 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400180 return 1;
181 }
182 return 0;
183}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200184
Holger Schurigffd074f2007-12-07 16:52:10 +0100185static inline int is_same_network(struct bss_descriptor *src,
186 struct bss_descriptor *dst)
187{
188 /* A network is only a duplicate if the channel, BSSID, and ESSID
189 * all match. We treat all <hidden> with the same BSSID and channel
190 * as one network */
191 return ((src->ssid_len == dst->ssid_len) &&
192 (src->channel == dst->channel) &&
193 !compare_ether_addr(src->bssid, dst->bssid) &&
194 !memcmp(src->ssid, dst->ssid, src->ssid_len));
195}
196
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200197/**
198 * @brief Check if a scanned network compatible with the driver settings
199 *
200 * WEP WPA WPA2 ad-hoc encrypt Network
201 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
202 * 0 0 0 0 NONE 0 0 0 yes No security
203 * 1 0 0 0 NONE 1 0 0 yes Static WEP
204 * 0 1 0 0 x 1x 1 x yes WPA
205 * 0 0 1 0 x 1x x 1 yes WPA2
206 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
207 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
208 *
209 *
David Woodhouseaa21c002007-12-08 20:04:36 +0000210 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200211 * @param index Index in scantable to check against current driver settings
212 * @param mode Network mode: Infrastructure or IBSS
213 *
214 * @return Index in scantable, or error code if negative
215 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000216static int is_network_compatible(struct lbs_private *priv,
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400217 struct bss_descriptor * bss, u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200218{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400219 int matched = 0;
220
Holger Schurige56188a2007-10-09 14:15:19 +0200221 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200222
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400223 if (bss->mode != mode)
224 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200225
David Woodhouseaa21c002007-12-08 20:04:36 +0000226 if ((matched = match_bss_no_security(&priv->secinfo, bss))) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400227 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000228 } else if ((matched = match_bss_static_wep(&priv->secinfo, bss))) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400229 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000230 } else if ((matched = match_bss_wpa(&priv->secinfo, bss))) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400231 lbs_deb_scan(
Holger Schurigffd074f2007-12-07 16:52:10 +0100232 "is_network_compatible() WPA: wpa_ie 0x%x "
233 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
234 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
David Woodhouseaa21c002007-12-08 20:04:36 +0000235 priv->secinfo.wep_enabled ? "e" : "d",
236 priv->secinfo.WPAenabled ? "e" : "d",
237 priv->secinfo.WPA2enabled ? "e" : "d",
Dan Williams0c9ca692007-08-02 10:43:44 -0400238 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400239 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000240 } else if ((matched = match_bss_wpa2(&priv->secinfo, bss))) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400241 lbs_deb_scan(
Holger Schurigffd074f2007-12-07 16:52:10 +0100242 "is_network_compatible() WPA2: wpa_ie 0x%x "
243 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
244 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
David Woodhouseaa21c002007-12-08 20:04:36 +0000245 priv->secinfo.wep_enabled ? "e" : "d",
246 priv->secinfo.WPAenabled ? "e" : "d",
247 priv->secinfo.WPA2enabled ? "e" : "d",
Dan Williams0c9ca692007-08-02 10:43:44 -0400248 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400249 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000250 } else if ((matched = match_bss_dynamic_wep(&priv->secinfo, bss))) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400251 lbs_deb_scan(
252 "is_network_compatible() dynamic WEP: "
Holger Schurigffd074f2007-12-07 16:52:10 +0100253 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
Dan Williams0c9ca692007-08-02 10:43:44 -0400254 bss->wpa_ie[0], bss->rsn_ie[0],
255 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Holger Schurig9012b282007-05-25 11:27:16 -0400256 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200257 }
258
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400259 /* bss security settings don't match those configured on card */
260 lbs_deb_scan(
Holger Schurigffd074f2007-12-07 16:52:10 +0100261 "is_network_compatible() FAILED: wpa_ie 0x%x "
262 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400263 bss->wpa_ie[0], bss->rsn_ie[0],
David Woodhouseaa21c002007-12-08 20:04:36 +0000264 priv->secinfo.wep_enabled ? "e" : "d",
265 priv->secinfo.WPAenabled ? "e" : "d",
266 priv->secinfo.WPA2enabled ? "e" : "d",
Dan Williams0c9ca692007-08-02 10:43:44 -0400267 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Holger Schurig9012b282007-05-25 11:27:16 -0400268
269done:
Holger Schurige56188a2007-10-09 14:15:19 +0200270 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400271 return matched;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200272}
273
Holger Schurige56188a2007-10-09 14:15:19 +0200274
275
276
277/*********************************************************************/
278/* */
279/* Main scanning support */
280/* */
281/*********************************************************************/
282
Holger Schurigffd074f2007-12-07 16:52:10 +0100283void lbs_scan_worker(struct work_struct *work)
284{
285 struct lbs_private *priv =
286 container_of(work, struct lbs_private, scan_work.work);
287
288 lbs_deb_enter(LBS_DEB_SCAN);
289 lbs_scan_networks(priv, NULL, 0);
290 lbs_deb_leave(LBS_DEB_SCAN);
291}
292
Holger Schurige56188a2007-10-09 14:15:19 +0200293
294/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200295 * @brief Create a channel list for the driver to scan based on region info
296 *
Holger Schurig10078322007-11-15 18:05:47 -0500297 * Only used from lbs_scan_setup_scan_config()
Holger Schurige56188a2007-10-09 14:15:19 +0200298 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200299 * Use the driver region/band information to construct a comprehensive list
300 * of channels to scan. This routine is used for any scan that is not
301 * provided a specific channel list to scan.
302 *
Holger Schurig69f90322007-11-23 15:43:44 +0100303 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200304 * @param scanchanlist Output parameter: resulting channel list to scan
305 * @param filteredscan Flag indicating whether or not a BSSID or SSID filter
306 * is being sent in the command to firmware. Used to
307 * increase the number of channels sent in a scan
308 * command and to disable the firmware channel scan
309 * filter.
310 *
311 * @return void
312 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100313static int lbs_scan_create_channel_list(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200314 struct chanscanparamset * scanchanlist,
315 u8 filteredscan)
316{
317
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200318 struct region_channel *scanregion;
319 struct chan_freq_power *cfp;
320 int rgnidx;
321 int chanidx;
322 int nextchan;
323 u8 scantype;
324
325 chanidx = 0;
326
327 /* Set the default scan type to the user specified type, will later
328 * be changed to passive on a per channel basis if restricted by
329 * regulatory requirements (11d or 11h)
330 */
Holger Schurig4f2fdaa2007-08-02 13:12:27 -0400331 scantype = CMD_SCAN_TYPE_ACTIVE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200332
David Woodhouseaa21c002007-12-08 20:04:36 +0000333 for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
334 if (priv->enable11d &&
335 (priv->connect_status != LBS_CONNECTED) &&
336 (priv->mesh_connect_status != LBS_CONNECTED)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200337 /* Scan all the supported chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000338 if (!priv->universal_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200339 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000340 scanregion = &priv->universal_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200341
342 /* clear the parsed_region_chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000343 memset(&priv->parsed_region_chan, 0x00,
344 sizeof(priv->parsed_region_chan));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200345 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +0000346 if (!priv->region_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200347 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000348 scanregion = &priv->region_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200349 }
350
351 for (nextchan = 0;
352 nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
353
354 cfp = scanregion->CFP + nextchan;
355
David Woodhouseaa21c002007-12-08 20:04:36 +0000356 if (priv->enable11d) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200357 scantype =
Holger Schurig10078322007-11-15 18:05:47 -0500358 lbs_get_scan_type_11d(cfp->channel,
David Woodhouseaa21c002007-12-08 20:04:36 +0000359 &priv->
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200360 parsed_region_chan);
361 }
362
363 switch (scanregion->band) {
364 case BAND_B:
365 case BAND_G:
366 default:
367 scanchanlist[chanidx].radiotype =
Dan Williams0aef64d2007-08-02 11:31:18 -0400368 CMD_SCAN_RADIO_TYPE_BG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200369 break;
370 }
371
Dan Williams0aef64d2007-08-02 11:31:18 -0400372 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200373 scanchanlist[chanidx].maxscantime =
David Woodhouse981f1872007-05-25 23:36:54 -0400374 cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200375 scanchanlist[chanidx].chanscanmode.passivescan =
376 1;
377 } else {
378 scanchanlist[chanidx].maxscantime =
David Woodhouse981f1872007-05-25 23:36:54 -0400379 cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200380 scanchanlist[chanidx].chanscanmode.passivescan =
381 0;
382 }
383
384 scanchanlist[chanidx].channumber = cfp->channel;
385
386 if (filteredscan) {
387 scanchanlist[chanidx].chanscanmode.
388 disablechanfilt = 1;
389 }
390 }
391 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100392 return chanidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200393}
394
Dan Williams2afc0c52007-08-02 13:19:04 -0400395
Holger Schurige56188a2007-10-09 14:15:19 +0200396/*
Holger Schurigffd074f2007-12-07 16:52:10 +0100397 * Add SSID TLV of the form:
398 *
399 * TLV-ID SSID 00 00
400 * length 06 00
401 * ssid 4d 4e 54 45 53 54
402 */
403static int lbs_scan_add_ssid_tlv(u8 *tlv,
404 const struct lbs_ioctl_user_scan_cfg *user_cfg)
Dan Williamseb8f7332007-05-25 16:25:21 -0400405{
Holger Schurigffd074f2007-12-07 16:52:10 +0100406 struct mrvlietypes_ssidparamset *ssid_tlv =
407 (struct mrvlietypes_ssidparamset *)tlv;
408 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
409 ssid_tlv->header.len = cpu_to_le16(user_cfg->ssid_len);
410 memcpy(ssid_tlv->ssid, user_cfg->ssid, user_cfg->ssid_len);
411 return sizeof(ssid_tlv->header) + user_cfg->ssid_len;
412}
Dan Williamseb8f7332007-05-25 16:25:21 -0400413
Holger Schurige56188a2007-10-09 14:15:19 +0200414
Holger Schurigffd074f2007-12-07 16:52:10 +0100415/*
416 * Add CHANLIST TLV of the form
417 *
418 * TLV-ID CHANLIST 01 01
419 * length 5b 00
420 * channel 1 00 01 00 00 00 64 00
421 * radio type 00
422 * channel 01
423 * scan type 00
424 * min scan time 00 00
425 * max scan time 64 00
426 * channel 2 00 02 00 00 00 64 00
427 * channel 3 00 03 00 00 00 64 00
428 * channel 4 00 04 00 00 00 64 00
429 * channel 5 00 05 00 00 00 64 00
430 * channel 6 00 06 00 00 00 64 00
431 * channel 7 00 07 00 00 00 64 00
432 * channel 8 00 08 00 00 00 64 00
433 * channel 9 00 09 00 00 00 64 00
434 * channel 10 00 0a 00 00 00 64 00
435 * channel 11 00 0b 00 00 00 64 00
436 * channel 12 00 0c 00 00 00 64 00
437 * channel 13 00 0d 00 00 00 64 00
438 *
439 */
440static int lbs_scan_add_chanlist_tlv(u8 *tlv,
441 struct chanscanparamset *chan_list,
442 int chan_count)
443{
444 size_t size = sizeof(struct chanscanparamset) * chan_count;
445 struct mrvlietypes_chanlistparamset *chan_tlv =
446 (struct mrvlietypes_chanlistparamset *) tlv;
Dan Williamseb8f7332007-05-25 16:25:21 -0400447
Holger Schurigffd074f2007-12-07 16:52:10 +0100448 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
449 memcpy(chan_tlv->chanscanparam, chan_list, size);
450 chan_tlv->header.len = cpu_to_le16(size);
451 return sizeof(chan_tlv->header) + size;
452}
Dan Williamseb8f7332007-05-25 16:25:21 -0400453
Holger Schurigffd074f2007-12-07 16:52:10 +0100454
455/*
456 * Add RATES TLV of the form
457 *
458 * TLV-ID RATES 01 00
459 * length 0e 00
460 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
461 *
462 * The rates are in lbs_bg_rates[], but for the 802.11b
463 * rates the high bit isn't set.
464 */
465static int lbs_scan_add_rates_tlv(u8 *tlv)
466{
467 int i;
468 struct mrvlietypes_ratesparamset *rate_tlv =
469 (struct mrvlietypes_ratesparamset *) tlv;
470
471 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
472 tlv += sizeof(rate_tlv->header);
473 for (i = 0; i < MAX_RATES; i++) {
474 *tlv = lbs_bg_rates[i];
475 if (*tlv == 0)
476 break;
477 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
478 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
479 Note that the values are MBit/s * 2, to mark them as
480 basic rates so that the firmware likes it better */
481 if (*tlv == 0x02 || *tlv == 0x04 ||
482 *tlv == 0x0b || *tlv == 0x16)
483 *tlv |= 0x80;
484 tlv++;
Dan Williamseb8f7332007-05-25 16:25:21 -0400485 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100486 rate_tlv->header.len = cpu_to_le16(i);
487 return sizeof(rate_tlv->header) + i;
488}
Dan Williamseb8f7332007-05-25 16:25:21 -0400489
Holger Schurigffd074f2007-12-07 16:52:10 +0100490
491/*
492 * Generate the CMD_802_11_SCAN command with the proper tlv
493 * for a bunch of channels.
494 */
495static int lbs_do_scan(struct lbs_private *priv,
496 u8 bsstype,
497 struct chanscanparamset *chan_list,
498 int chan_count,
499 const struct lbs_ioctl_user_scan_cfg *user_cfg)
500{
501 int ret = -ENOMEM;
502 struct lbs_scan_cmd_config *scan_cmd;
503 u8 *tlv; /* pointer into our current, growing TLV storage area */
504
505 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, "
506 "chan_count %d",
507 bsstype, chan_list[0].channumber, chan_count);
508
509 /* create the fixed part for scan command */
510 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
511 if (scan_cmd == NULL)
Holger Schurige56188a2007-10-09 14:15:19 +0200512 goto out;
Holger Schurigffd074f2007-12-07 16:52:10 +0100513 tlv = scan_cmd->tlvbuffer;
514 if (user_cfg)
515 memcpy(scan_cmd->bssid, user_cfg->bssid, ETH_ALEN);
516 scan_cmd->bsstype = bsstype;
Dan Williamseb8f7332007-05-25 16:25:21 -0400517
Holger Schurigffd074f2007-12-07 16:52:10 +0100518 /* add TLVs */
519 if (user_cfg && user_cfg->ssid_len)
520 tlv += lbs_scan_add_ssid_tlv(tlv, user_cfg);
521 if (chan_list && chan_count)
522 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
523 tlv += lbs_scan_add_rates_tlv(tlv);
Dan Williamseb8f7332007-05-25 16:25:21 -0400524
Holger Schurigffd074f2007-12-07 16:52:10 +0100525 /* This is the final data we are about to send */
526 scan_cmd->tlvbufferlen = tlv - scan_cmd->tlvbuffer;
527 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd, 1+6);
528 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
529 scan_cmd->tlvbufferlen);
Dan Williamseb8f7332007-05-25 16:25:21 -0400530
Holger Schurigffd074f2007-12-07 16:52:10 +0100531 ret = lbs_prepare_and_send_command(priv, CMD_802_11_SCAN, 0,
532 CMD_OPTION_WAITFORRSP, 0, scan_cmd);
Holger Schurige56188a2007-10-09 14:15:19 +0200533out:
Holger Schurigffd074f2007-12-07 16:52:10 +0100534 kfree(scan_cmd);
535 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
536 return ret;
Dan Williamseb8f7332007-05-25 16:25:21 -0400537}
538
539
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200540/**
541 * @brief Internal function used to start a scan based on an input config
542 *
Holger Schurige56188a2007-10-09 14:15:19 +0200543 * Also used from debugfs
544 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200545 * Use the input user scan configuration information when provided in
546 * order to send the appropriate scan commands to firmware to populate or
547 * update the internal driver scan table
548 *
Holger Schurig69f90322007-11-23 15:43:44 +0100549 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200550 * @param puserscanin Pointer to the input configuration for the requested
551 * scan.
552 *
553 * @return 0 or < 0 if error
554 */
Holger Schurig69f90322007-11-23 15:43:44 +0100555int lbs_scan_networks(struct lbs_private *priv,
Holger Schurigffd074f2007-12-07 16:52:10 +0100556 const struct lbs_ioctl_user_scan_cfg *user_cfg,
Dan Williams2afc0c52007-08-02 13:19:04 -0400557 int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200558{
Holger Schurigffd074f2007-12-07 16:52:10 +0100559 int ret = -ENOMEM;
560 struct chanscanparamset *chan_list;
561 struct chanscanparamset *curr_chans;
562 int chan_count;
563 u8 bsstype = CMD_BSS_TYPE_ANY;
564 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
565 int filteredscan = 0;
566 union iwreq_data wrqu;
Dan Williamsf8f55102007-05-30 10:12:55 -0400567#ifdef CONFIG_LIBERTAS_DEBUG
Holger Schurigffd074f2007-12-07 16:52:10 +0100568 struct bss_descriptor *iter;
Dan Williamsf8f55102007-05-30 10:12:55 -0400569 int i = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700570 DECLARE_MAC_BUF(mac);
Dan Williamsf8f55102007-05-30 10:12:55 -0400571#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200572
Holger Schurigffd074f2007-12-07 16:52:10 +0100573 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d",
574 full_scan);
Dan Williams2afc0c52007-08-02 13:19:04 -0400575
576 /* Cancel any partial outstanding partial scans if this scan
577 * is a full scan.
578 */
579 if (full_scan && delayed_work_pending(&priv->scan_work))
580 cancel_delayed_work(&priv->scan_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200581
Holger Schurigffd074f2007-12-07 16:52:10 +0100582 /* Determine same scan parameters */
583 if (user_cfg) {
584 if (user_cfg->bsstype)
585 bsstype = user_cfg->bsstype;
586 if (compare_ether_addr(user_cfg->bssid, &zeromac[0]) != 0) {
587 numchannels = MRVDRV_MAX_CHANNELS_PER_SCAN;
588 filteredscan = 1;
Holger Schurig3cf84092007-08-02 11:50:12 -0400589 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200590 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100591 lbs_deb_scan("numchannels %d, bsstype %d, "
592 "filteredscan %d\n",
593 numchannels, bsstype, filteredscan);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200594
Holger Schurigffd074f2007-12-07 16:52:10 +0100595 /* Create list of channels to scan */
596 chan_list = kzalloc(sizeof(struct chanscanparamset) *
597 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
598 if (!chan_list) {
599 lbs_pr_alert("SCAN: chan_list empty\n");
600 goto out;
601 }
602
603 /* We want to scan all channels */
604 chan_count = lbs_scan_create_channel_list(priv, chan_list,
605 filteredscan);
606
607 netif_stop_queue(priv->dev);
608 netif_carrier_off(priv->dev);
609 if (priv->mesh_dev) {
David Woodhousea27b9f92007-12-12 00:41:51 -0500610 netif_stop_queue(priv->mesh_dev);
611 netif_carrier_off(priv->mesh_dev);
Holger Schurigffd074f2007-12-07 16:52:10 +0100612 }
613
614 /* Prepare to continue an interrupted scan */
615 lbs_deb_scan("chan_count %d, last_scanned_channel %d\n",
David Woodhousea27b9f92007-12-12 00:41:51 -0500616 chan_count, priv->last_scanned_channel);
Holger Schurigffd074f2007-12-07 16:52:10 +0100617 curr_chans = chan_list;
618 /* advance channel list by already-scanned-channels */
David Woodhouseaa21c002007-12-08 20:04:36 +0000619 if (priv->last_scanned_channel > 0) {
620 curr_chans += priv->last_scanned_channel;
621 chan_count -= priv->last_scanned_channel;
Holger Schurigffd074f2007-12-07 16:52:10 +0100622 }
623
624 /* Send scan command(s)
625 * numchannels contains the number of channels we should maximally scan
626 * chan_count is the total number of channels to scan
627 */
628
629 while (chan_count) {
630 int to_scan = min(numchannels, chan_count);
631 lbs_deb_scan("scanning %d of %d channels\n",
632 to_scan, chan_count);
633 ret = lbs_do_scan(priv, bsstype, curr_chans,
634 to_scan, user_cfg);
635 if (ret) {
636 lbs_pr_err("SCAN_CMD failed\n");
637 goto out2;
638 }
639 curr_chans += to_scan;
640 chan_count -= to_scan;
641
642 /* somehow schedule the next part of the scan */
643 if (chan_count &&
644 !full_scan &&
David Woodhouseaa21c002007-12-08 20:04:36 +0000645 !priv->surpriseremoved) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100646 /* -1 marks just that we're currently scanning */
David Woodhouseaa21c002007-12-08 20:04:36 +0000647 if (priv->last_scanned_channel < 0)
648 priv->last_scanned_channel = to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100649 else
David Woodhouseaa21c002007-12-08 20:04:36 +0000650 priv->last_scanned_channel += to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100651 cancel_delayed_work(&priv->scan_work);
652 queue_delayed_work(priv->work_thread, &priv->scan_work,
653 msecs_to_jiffies(300));
654 /* skip over GIWSCAN event */
655 goto out;
656 }
657
658 }
659 memset(&wrqu, 0, sizeof(union iwreq_data));
660 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200661
Dan Williamsf8f55102007-05-30 10:12:55 -0400662#ifdef CONFIG_LIBERTAS_DEBUG
663 /* Dump the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000664 mutex_lock(&priv->lock);
Holger Schurigffd074f2007-12-07 16:52:10 +0100665 lbs_deb_scan("scan table:\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000666 list_for_each_entry(iter, &priv->network_list, list)
Holger Schurigffd074f2007-12-07 16:52:10 +0100667 lbs_deb_scan("%02d: BSSID %s, RSSI %d, SSID '%s'\n",
668 i++, print_mac(mac, iter->bssid), (s32) iter->rssi,
669 escape_essid(iter->ssid, iter->ssid_len));
David Woodhouseaa21c002007-12-08 20:04:36 +0000670 mutex_unlock(&priv->lock);
Dan Williamsf8f55102007-05-30 10:12:55 -0400671#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200672
Holger Schurigffd074f2007-12-07 16:52:10 +0100673out2:
David Woodhouseaa21c002007-12-08 20:04:36 +0000674 priv->last_scanned_channel = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100675
676out:
David Woodhouseaa21c002007-12-08 20:04:36 +0000677 if (priv->connect_status == LBS_CONNECTED) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400678 netif_carrier_on(priv->dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500679 if (!priv->tx_pending_len)
680 netif_wake_queue(priv->dev);
Brajesh Dave01d77d82007-11-20 17:44:14 -0500681 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000682 if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
Brajesh Dave01d77d82007-11-20 17:44:14 -0500683 netif_carrier_on(priv->mesh_dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500684 if (!priv->tx_pending_len)
685 netif_wake_queue(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200686 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100687 kfree(chan_list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200688
Holger Schurig9012b282007-05-25 11:27:16 -0400689 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200690 return ret;
691}
692
Holger Schurigffd074f2007-12-07 16:52:10 +0100693
694
695
696/*********************************************************************/
697/* */
698/* Result interpretation */
699/* */
700/*********************************************************************/
701
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200702/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200703 * @brief Interpret a BSS scan response returned from the firmware
704 *
705 * Parse the various fixed fields and IEs passed back for a a BSS probe
Holger Schurigffd074f2007-12-07 16:52:10 +0100706 * response or beacon from the scan command. Record information as needed
707 * in the scan table struct bss_descriptor for that entry.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200708 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400709 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200710 *
711 * @return 0 or -1
712 */
Holger Schurig10078322007-11-15 18:05:47 -0500713static int lbs_process_bss(struct bss_descriptor *bss,
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400714 u8 ** pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200715{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200716 struct ieeetypes_fhparamset *pFH;
717 struct ieeetypes_dsparamset *pDS;
718 struct ieeetypes_cfparamset *pCF;
719 struct ieeetypes_ibssparamset *pibss;
Joe Perches0795af52007-10-03 17:59:30 -0700720 DECLARE_MAC_BUF(mac);
Dan Williams8c512762007-08-02 11:40:45 -0400721 struct ieeetypes_countryinfoset *pcountryinfo;
722 u8 *pos, *end, *p;
723 u8 n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
724 u16 beaconsize = 0;
Holger Schurig9012b282007-05-25 11:27:16 -0400725 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200726
Holger Schurige56188a2007-10-09 14:15:19 +0200727 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200728
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200729 if (*bytesleft >= sizeof(beaconsize)) {
730 /* Extract & convert beacon size from the command buffer */
David Woodhousee7240ac2007-12-11 17:54:36 -0500731 beaconsize = le16_to_cpu(get_unaligned((__le16 *)*pbeaconinfo));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200732 *bytesleft -= sizeof(beaconsize);
733 *pbeaconinfo += sizeof(beaconsize);
734 }
735
736 if (beaconsize == 0 || beaconsize > *bytesleft) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200737 *pbeaconinfo += *bytesleft;
738 *bytesleft = 0;
Holger Schurige56188a2007-10-09 14:15:19 +0200739 ret = -1;
740 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200741 }
742
743 /* Initialize the current working beacon pointer for this BSS iteration */
Dan Williamsab617972007-08-02 10:48:02 -0400744 pos = *pbeaconinfo;
745 end = pos + beaconsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200746
747 /* Advance the return beacon pointer past the current beacon */
748 *pbeaconinfo += beaconsize;
749 *bytesleft -= beaconsize;
750
Dan Williamsab617972007-08-02 10:48:02 -0400751 memcpy(bss->bssid, pos, ETH_ALEN);
Holger Schurigffd074f2007-12-07 16:52:10 +0100752 lbs_deb_scan("process_bss: BSSID %s\n", print_mac(mac, bss->bssid));
Dan Williamsab617972007-08-02 10:48:02 -0400753 pos += ETH_ALEN;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200754
Dan Williamsab617972007-08-02 10:48:02 -0400755 if ((end - pos) < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400756 lbs_deb_scan("process_bss: Not enough bytes left\n");
Holger Schurige56188a2007-10-09 14:15:19 +0200757 ret = -1;
758 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200759 }
760
761 /*
762 * next 4 fields are RSSI, time stamp, beacon interval,
763 * and capability information
764 */
765
766 /* RSSI is 1 byte long */
Dan Williamsab617972007-08-02 10:48:02 -0400767 bss->rssi = *pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100768 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
Dan Williamsab617972007-08-02 10:48:02 -0400769 pos++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200770
771 /* time stamp is 8 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400772 pos += 8;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200773
774 /* beacon interval is 2 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400775 bss->beaconperiod = le16_to_cpup((void *) pos);
776 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200777
778 /* capability information is 2 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400779 bss->capability = le16_to_cpup((void *) pos);
Holger Schurigffd074f2007-12-07 16:52:10 +0100780 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
Dan Williamsab617972007-08-02 10:48:02 -0400781 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200782
Dan Williams0c9ca692007-08-02 10:43:44 -0400783 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
Holger Schurigffd074f2007-12-07 16:52:10 +0100784 lbs_deb_scan("process_bss: WEP enabled\n");
Dan Williams0c9ca692007-08-02 10:43:44 -0400785 if (bss->capability & WLAN_CAPABILITY_IBSS)
786 bss->mode = IW_MODE_ADHOC;
787 else
788 bss->mode = IW_MODE_INFRA;
789
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200790 /* rest of the current buffer are IE's */
Holger Schurigffd074f2007-12-07 16:52:10 +0100791 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
Holger Schurigece56192007-08-02 11:53:06 -0400792 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200793
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200794 /* process variable IE */
Dan Williamsab617972007-08-02 10:48:02 -0400795 while (pos <= end - 2) {
796 struct ieee80211_info_element * elem =
797 (struct ieee80211_info_element *) pos;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200798
Dan Williamsab617972007-08-02 10:48:02 -0400799 if (pos + elem->len > end) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400800 lbs_deb_scan("process_bss: error in processing IE, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200801 "bytes left < IE length\n");
Dan Williamsab617972007-08-02 10:48:02 -0400802 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200803 }
804
Dan Williamsab617972007-08-02 10:48:02 -0400805 switch (elem->id) {
806 case MFIE_TYPE_SSID:
807 bss->ssid_len = elem->len;
808 memcpy(bss->ssid, elem->data, elem->len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100809 lbs_deb_scan("got SSID IE: '%s', len %u\n",
Dan Williamsd8efea22007-05-28 23:54:55 -0400810 escape_essid(bss->ssid, bss->ssid_len),
811 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200812 break;
813
Dan Williamsab617972007-08-02 10:48:02 -0400814 case MFIE_TYPE_RATES:
Dan Williams8c512762007-08-02 11:40:45 -0400815 n_basic_rates = min_t(u8, MAX_RATES, elem->len);
816 memcpy(bss->rates, elem->data, n_basic_rates);
817 got_basic_rates = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100818 lbs_deb_scan("got RATES IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200819 break;
820
Dan Williamsab617972007-08-02 10:48:02 -0400821 case MFIE_TYPE_FH_SET:
822 pFH = (struct ieeetypes_fhparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400823 memmove(&bss->phyparamset.fhparamset, pFH,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200824 sizeof(struct ieeetypes_fhparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100825 lbs_deb_scan("got FH IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200826 break;
827
Dan Williamsab617972007-08-02 10:48:02 -0400828 case MFIE_TYPE_DS_SET:
829 pDS = (struct ieeetypes_dsparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400830 bss->channel = pDS->currentchan;
831 memcpy(&bss->phyparamset.dsparamset, pDS,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200832 sizeof(struct ieeetypes_dsparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100833 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200834 break;
835
Dan Williamsab617972007-08-02 10:48:02 -0400836 case MFIE_TYPE_CF_SET:
837 pCF = (struct ieeetypes_cfparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400838 memcpy(&bss->ssparamset.cfparamset, pCF,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200839 sizeof(struct ieeetypes_cfparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100840 lbs_deb_scan("got CF IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200841 break;
842
Dan Williamsab617972007-08-02 10:48:02 -0400843 case MFIE_TYPE_IBSS_SET:
844 pibss = (struct ieeetypes_ibssparamset *) pos;
David Woodhousee7240ac2007-12-11 17:54:36 -0500845 bss->atimwindow = le16_to_cpu(pibss->atimwindow);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400846 memmove(&bss->ssparamset.ibssparamset, pibss,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200847 sizeof(struct ieeetypes_ibssparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100848 lbs_deb_scan("got IBSS IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200849 break;
850
Dan Williamsab617972007-08-02 10:48:02 -0400851 case MFIE_TYPE_COUNTRY:
852 pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100853 lbs_deb_scan("got COUNTRY IE\n");
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400854 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200855 || pcountryinfo->len > 254) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400856 lbs_deb_scan("process_bss: 11D- Err "
Holger Schurigffd074f2007-12-07 16:52:10 +0100857 "CountryInfo len %d, min %zd, max 254\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200858 pcountryinfo->len,
859 sizeof(pcountryinfo->countrycode));
Holger Schurig9012b282007-05-25 11:27:16 -0400860 ret = -1;
861 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200862 }
863
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400864 memcpy(&bss->countryinfo,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200865 pcountryinfo, pcountryinfo->len + 2);
Holger Schurigece56192007-08-02 11:53:06 -0400866 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200867 (u8 *) pcountryinfo,
868 (u32) (pcountryinfo->len + 2));
869 break;
870
Dan Williamsab617972007-08-02 10:48:02 -0400871 case MFIE_TYPE_RATES_EX:
872 /* only process extended supported rate if data rate is
873 * already found. Data rate IE should come before
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200874 * extended supported rate IE
875 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100876 lbs_deb_scan("got RATESEX IE\n");
877 if (!got_basic_rates) {
878 lbs_deb_scan("... but ignoring it\n");
Dan Williamsab617972007-08-02 10:48:02 -0400879 break;
Holger Schurigffd074f2007-12-07 16:52:10 +0100880 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200881
Dan Williams8c512762007-08-02 11:40:45 -0400882 n_ex_rates = elem->len;
883 if (n_basic_rates + n_ex_rates > MAX_RATES)
884 n_ex_rates = MAX_RATES - n_basic_rates;
Dan Williamsab617972007-08-02 10:48:02 -0400885
Dan Williams8c512762007-08-02 11:40:45 -0400886 p = bss->rates + n_basic_rates;
887 memcpy(p, elem->data, n_ex_rates);
Dan Williamsab617972007-08-02 10:48:02 -0400888 break;
889
890 case MFIE_TYPE_GENERIC:
891 if (elem->len >= 4 &&
892 elem->data[0] == 0x00 &&
893 elem->data[1] == 0x50 &&
894 elem->data[2] == 0xf2 &&
895 elem->data[3] == 0x01) {
896 bss->wpa_ie_len = min(elem->len + 2,
897 MAX_WPA_IE_LEN);
898 memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100899 lbs_deb_scan("got WPA IE\n");
900 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie,
Dan Williamsab617972007-08-02 10:48:02 -0400901 elem->len);
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400902 } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
903 elem->data[0] == 0x00 &&
904 elem->data[1] == 0x50 &&
905 elem->data[2] == 0x43 &&
906 elem->data[3] == 0x04) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100907 lbs_deb_scan("got mesh IE\n");
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400908 bss->mesh = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100909 } else {
910 lbs_deb_scan("got generiec IE: "
911 "%02x:%02x:%02x:%02x, len %d\n",
912 elem->data[0], elem->data[1],
913 elem->data[2], elem->data[3],
914 elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200915 }
916 break;
917
Dan Williamsab617972007-08-02 10:48:02 -0400918 case MFIE_TYPE_RSN:
Holger Schurigffd074f2007-12-07 16:52:10 +0100919 lbs_deb_scan("got RSN IE\n");
Dan Williamsab617972007-08-02 10:48:02 -0400920 bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
921 memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100922 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
923 bss->rsn_ie, elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200924 break;
925
Dan Williamsab617972007-08-02 10:48:02 -0400926 default:
Holger Schurigffd074f2007-12-07 16:52:10 +0100927 lbs_deb_scan("got IE 0x%04x, len %d\n",
928 elem->id, elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200929 break;
930 }
931
Dan Williamsab617972007-08-02 10:48:02 -0400932 pos += elem->len + 2;
933 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400934
935 /* Timestamp */
936 bss->last_scanned = jiffies;
Holger Schurig10078322007-11-15 18:05:47 -0500937 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400938
Holger Schurig9012b282007-05-25 11:27:16 -0400939 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200940
Holger Schurig9012b282007-05-25 11:27:16 -0400941done:
942 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
943 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200944}
945
946/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200947 * @brief This function finds a specific compatible BSSID in the scan list
948 *
Holger Schurige56188a2007-10-09 14:15:19 +0200949 * Used in association code
950 *
David Woodhouseaa21c002007-12-08 20:04:36 +0000951 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200952 * @param bssid BSSID to find in the scan list
953 * @param mode Network mode: Infrastructure or IBSS
954 *
955 * @return index in BSSID list, or error return code (< 0)
956 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000957struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400958 u8 * bssid, u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200959{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400960 struct bss_descriptor * iter_bss;
961 struct bss_descriptor * found_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200962
Holger Schurige56188a2007-10-09 14:15:19 +0200963 lbs_deb_enter(LBS_DEB_SCAN);
964
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200965 if (!bssid)
Holger Schurige56188a2007-10-09 14:15:19 +0200966 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200967
Holger Schurigece56192007-08-02 11:53:06 -0400968 lbs_deb_hex(LBS_DEB_SCAN, "looking for",
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400969 bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200970
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400971 /* Look through the scan table for a compatible match. The loop will
972 * continue past a matched bssid that is not compatible in case there
973 * is an AP with multiple SSIDs assigned to the same BSSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200974 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000975 mutex_lock(&priv->lock);
976 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williams3cf20932007-05-25 17:28:30 -0400977 if (compare_ether_addr(iter_bss->bssid, bssid))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400978 continue; /* bssid doesn't match */
979 switch (mode) {
980 case IW_MODE_INFRA:
981 case IW_MODE_ADHOC:
David Woodhouseaa21c002007-12-08 20:04:36 +0000982 if (!is_network_compatible(priv, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200983 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400984 found_bss = iter_bss;
985 break;
986 default:
987 found_bss = iter_bss;
988 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200989 }
990 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000991 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200992
Holger Schurige56188a2007-10-09 14:15:19 +0200993out:
994 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400995 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200996}
997
998/**
999 * @brief This function finds ssid in ssid list.
1000 *
Holger Schurige56188a2007-10-09 14:15:19 +02001001 * Used in association code
1002 *
David Woodhouseaa21c002007-12-08 20:04:36 +00001003 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001004 * @param ssid SSID to find in the list
1005 * @param bssid BSSID to qualify the SSID selection (if provided)
1006 * @param mode Network mode: Infrastructure or IBSS
1007 *
1008 * @return index in BSSID list
1009 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001010struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
Dan Williamsd8efea22007-05-28 23:54:55 -04001011 u8 *ssid, u8 ssid_len, u8 * bssid, u8 mode,
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001012 int channel)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001013{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001014 u8 bestrssi = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001015 struct bss_descriptor * iter_bss = NULL;
1016 struct bss_descriptor * found_bss = NULL;
1017 struct bss_descriptor * tmp_oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001018
Holger Schurige56188a2007-10-09 14:15:19 +02001019 lbs_deb_enter(LBS_DEB_SCAN);
1020
David Woodhouseaa21c002007-12-08 20:04:36 +00001021 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001022
David Woodhouseaa21c002007-12-08 20:04:36 +00001023 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001024 if ( !tmp_oldest
1025 || (iter_bss->last_scanned < tmp_oldest->last_scanned))
1026 tmp_oldest = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001027
Holger Schurig10078322007-11-15 18:05:47 -05001028 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001029 ssid, ssid_len) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001030 continue; /* ssid doesn't match */
Dan Williams3cf20932007-05-25 17:28:30 -04001031 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001032 continue; /* bssid doesn't match */
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001033 if ((channel > 0) && (iter_bss->channel != channel))
1034 continue; /* channel doesn't match */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001035
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001036 switch (mode) {
1037 case IW_MODE_INFRA:
1038 case IW_MODE_ADHOC:
David Woodhouseaa21c002007-12-08 20:04:36 +00001039 if (!is_network_compatible(priv, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001040 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001041
1042 if (bssid) {
1043 /* Found requested BSSID */
1044 found_bss = iter_bss;
1045 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001046 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001047
1048 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1049 bestrssi = SCAN_RSSI(iter_bss->rssi);
1050 found_bss = iter_bss;
1051 }
1052 break;
1053 case IW_MODE_AUTO:
1054 default:
1055 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1056 bestrssi = SCAN_RSSI(iter_bss->rssi);
1057 found_bss = iter_bss;
1058 }
1059 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001060 }
1061 }
1062
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001063out:
David Woodhouseaa21c002007-12-08 20:04:36 +00001064 mutex_unlock(&priv->lock);
Holger Schurige56188a2007-10-09 14:15:19 +02001065 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001066 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001067}
1068
1069/**
1070 * @brief This function finds the best SSID in the Scan List
1071 *
1072 * Search the scan table for the best SSID that also matches the current
1073 * adapter network preference (infrastructure or adhoc)
1074 *
David Woodhouseaa21c002007-12-08 20:04:36 +00001075 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001076 *
1077 * @return index in BSSID list
1078 */
Holger Schurig69f90322007-11-23 15:43:44 +01001079static struct bss_descriptor *lbs_find_best_ssid_in_list(
David Woodhouseaa21c002007-12-08 20:04:36 +00001080 struct lbs_private *priv,
Holger Schurig69f90322007-11-23 15:43:44 +01001081 u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001082{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001083 u8 bestrssi = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001084 struct bss_descriptor * iter_bss;
1085 struct bss_descriptor * best_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001086
Holger Schurige56188a2007-10-09 14:15:19 +02001087 lbs_deb_enter(LBS_DEB_SCAN);
1088
David Woodhouseaa21c002007-12-08 20:04:36 +00001089 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001090
David Woodhouseaa21c002007-12-08 20:04:36 +00001091 list_for_each_entry (iter_bss, &priv->network_list, list) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001092 switch (mode) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001093 case IW_MODE_INFRA:
1094 case IW_MODE_ADHOC:
David Woodhouseaa21c002007-12-08 20:04:36 +00001095 if (!is_network_compatible(priv, iter_bss, mode))
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001096 break;
1097 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1098 break;
1099 bestrssi = SCAN_RSSI(iter_bss->rssi);
1100 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001101 break;
Dan Williams0dc5a292007-05-10 22:58:02 -04001102 case IW_MODE_AUTO:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001103 default:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001104 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1105 break;
1106 bestrssi = SCAN_RSSI(iter_bss->rssi);
1107 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001108 break;
1109 }
1110 }
1111
David Woodhouseaa21c002007-12-08 20:04:36 +00001112 mutex_unlock(&priv->lock);
Holger Schurige56188a2007-10-09 14:15:19 +02001113 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001114 return best_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001115}
1116
1117/**
1118 * @brief Find the AP with specific ssid in the scan list
1119 *
Holger Schurige56188a2007-10-09 14:15:19 +02001120 * Used from association worker.
1121 *
Holger Schurig69f90322007-11-23 15:43:44 +01001122 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001123 * @param pSSID A pointer to AP's ssid
1124 *
1125 * @return 0--success, otherwise--fail
1126 */
Holger Schurig69f90322007-11-23 15:43:44 +01001127int lbs_find_best_network_ssid(struct lbs_private *priv,
Dan Williamsd8efea22007-05-28 23:54:55 -04001128 u8 *out_ssid, u8 *out_ssid_len, u8 preferred_mode, u8 *out_mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001129{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001130 int ret = -1;
1131 struct bss_descriptor * found;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001132
Holger Schurige56188a2007-10-09 14:15:19 +02001133 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001134
Holger Schurig10078322007-11-15 18:05:47 -05001135 lbs_scan_networks(priv, NULL, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +00001136 if (priv->surpriseremoved)
Holger Schurige56188a2007-10-09 14:15:19 +02001137 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001138
David Woodhouseaa21c002007-12-08 20:04:36 +00001139 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
Dan Williamsd8efea22007-05-28 23:54:55 -04001140 if (found && (found->ssid_len > 0)) {
1141 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1142 *out_ssid_len = found->ssid_len;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001143 *out_mode = found->mode;
1144 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001145 }
1146
Holger Schurige56188a2007-10-09 14:15:19 +02001147out:
Holger Schurig9012b282007-05-25 11:27:16 -04001148 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001149 return ret;
1150}
1151
Holger Schurige56188a2007-10-09 14:15:19 +02001152
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001153/**
1154 * @brief Send a scan command for all available channels filtered on a spec
1155 *
Holger Schurige56188a2007-10-09 14:15:19 +02001156 * Used in association code and from debugfs
1157 *
Holger Schurig69f90322007-11-23 15:43:44 +01001158 * @param priv A pointer to struct lbs_private structure
Holger Schurige56188a2007-10-09 14:15:19 +02001159 * @param ssid A pointer to the SSID to scan for
1160 * @param ssid_len Length of the SSID
1161 * @param clear_ssid Should existing scan results with this SSID
1162 * be cleared?
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001163 *
1164 * @return 0-success, otherwise fail
1165 */
Holger Schurig69f90322007-11-23 15:43:44 +01001166int lbs_send_specific_ssid_scan(struct lbs_private *priv,
Dan Williamsd8efea22007-05-28 23:54:55 -04001167 u8 *ssid, u8 ssid_len, u8 clear_ssid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001168{
Holger Schurig10078322007-11-15 18:05:47 -05001169 struct lbs_ioctl_user_scan_cfg scancfg;
Dan Williamseb8f7332007-05-25 16:25:21 -04001170 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001171
Holger Schurige56188a2007-10-09 14:15:19 +02001172 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s', clear %d",
1173 escape_essid(ssid, ssid_len), clear_ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001174
Dan Williamsd8efea22007-05-28 23:54:55 -04001175 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -04001176 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001177
1178 memset(&scancfg, 0x00, sizeof(scancfg));
Dan Williamsd8efea22007-05-28 23:54:55 -04001179 memcpy(scancfg.ssid, ssid, ssid_len);
1180 scancfg.ssid_len = ssid_len;
Dan Williamseb8f7332007-05-25 16:25:21 -04001181 scancfg.clear_ssid = clear_ssid;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001182
Holger Schurig10078322007-11-15 18:05:47 -05001183 lbs_scan_networks(priv, &scancfg, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +00001184 if (priv->surpriseremoved) {
Holger Schurige56188a2007-10-09 14:15:19 +02001185 ret = -1;
1186 goto out;
1187 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001188
Dan Williamseb8f7332007-05-25 16:25:21 -04001189out:
Holger Schurige56188a2007-10-09 14:15:19 +02001190 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Dan Williamseb8f7332007-05-25 16:25:21 -04001191 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001192}
1193
Holger Schurige56188a2007-10-09 14:15:19 +02001194
1195
1196
1197/*********************************************************************/
1198/* */
1199/* Support for Wireless Extensions */
1200/* */
1201/*********************************************************************/
1202
Holger Schurigffd074f2007-12-07 16:52:10 +01001203
Dan Williams00af0152007-08-02 13:14:56 -04001204#define MAX_CUSTOM_LEN 64
1205
Holger Schurig69f90322007-11-23 15:43:44 +01001206static inline char *lbs_translate_scan(struct lbs_private *priv,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001207 char *start, char *stop,
1208 struct bss_descriptor *bss)
1209{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001210 struct chan_freq_power *cfp;
1211 char *current_val; /* For rates */
1212 struct iw_event iwe; /* Temporary buffer */
1213 int j;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001214#define PERFECT_RSSI ((u8)50)
1215#define WORST_RSSI ((u8)0)
1216#define RSSI_DIFF ((u8)(PERFECT_RSSI - WORST_RSSI))
1217 u8 rssi;
1218
Holger Schurige56188a2007-10-09 14:15:19 +02001219 lbs_deb_enter(LBS_DEB_SCAN);
1220
David Woodhouseaa21c002007-12-08 20:04:36 +00001221 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001222 if (!cfp) {
1223 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
Holger Schurige56188a2007-10-09 14:15:19 +02001224 start = NULL;
1225 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001226 }
1227
Holger Schurigffd074f2007-12-07 16:52:10 +01001228 /* First entry *MUST* be the BSSID */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001229 iwe.cmd = SIOCGIWAP;
1230 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1231 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
1232 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
1233
1234 /* SSID */
1235 iwe.cmd = SIOCGIWESSID;
1236 iwe.u.data.flags = 1;
Dan Williamsd8efea22007-05-28 23:54:55 -04001237 iwe.u.data.length = min((u32) bss->ssid_len, (u32) IW_ESSID_MAX_SIZE);
1238 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001239
1240 /* Mode */
1241 iwe.cmd = SIOCGIWMODE;
1242 iwe.u.mode = bss->mode;
1243 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
1244
1245 /* Frequency */
1246 iwe.cmd = SIOCGIWFREQ;
1247 iwe.u.freq.m = (long)cfp->freq * 100000;
1248 iwe.u.freq.e = 1;
1249 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
1250
1251 /* Add quality statistics */
1252 iwe.cmd = IWEVQUAL;
1253 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
1254 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
1255
1256 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
1257 iwe.u.qual.qual =
1258 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
1259 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
1260 (RSSI_DIFF * RSSI_DIFF);
1261 if (iwe.u.qual.qual > 100)
1262 iwe.u.qual.qual = 100;
1263
David Woodhouseaa21c002007-12-08 20:04:36 +00001264 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001265 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
1266 } else {
1267 iwe.u.qual.noise =
David Woodhouseaa21c002007-12-08 20:04:36 +00001268 CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001269 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001270
Dan Williams80e78ef2007-05-25 22:18:47 -04001271 /* Locally created ad-hoc BSSs won't have beacons if this is the
1272 * only station in the adhoc network; so get signal strength
1273 * from receive statistics.
1274 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001275 if ((priv->mode == IW_MODE_ADHOC)
1276 && priv->adhoccreate
1277 && !lbs_ssid_cmp(priv->curbssparams.ssid,
1278 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001279 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -04001280 int snr, nf;
David Woodhouseaa21c002007-12-08 20:04:36 +00001281 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1282 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
Dan Williams80e78ef2007-05-25 22:18:47 -04001283 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001284 }
1285 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
1286
1287 /* Add encryption capability */
1288 iwe.cmd = SIOCGIWENCODE;
Dan Williams0c9ca692007-08-02 10:43:44 -04001289 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001290 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1291 } else {
1292 iwe.u.data.flags = IW_ENCODE_DISABLED;
1293 }
1294 iwe.u.data.length = 0;
Dan Williamsd8efea22007-05-28 23:54:55 -04001295 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001296
1297 current_val = start + IW_EV_LCP_LEN;
1298
1299 iwe.cmd = SIOCGIWRATE;
1300 iwe.u.bitrate.fixed = 0;
1301 iwe.u.bitrate.disabled = 0;
1302 iwe.u.bitrate.value = 0;
1303
Dan Williams8c512762007-08-02 11:40:45 -04001304 for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
1305 /* Bit rate given in 500 kb/s units */
1306 iwe.u.bitrate.value = bss->rates[j] * 500000;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001307 current_val = iwe_stream_add_value(start, current_val,
1308 stop, &iwe, IW_EV_PARAM_LEN);
1309 }
1310 if ((bss->mode == IW_MODE_ADHOC)
David Woodhouseaa21c002007-12-08 20:04:36 +00001311 && !lbs_ssid_cmp(priv->curbssparams.ssid,
1312 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001313 bss->ssid, bss->ssid_len)
David Woodhouseaa21c002007-12-08 20:04:36 +00001314 && priv->adhoccreate) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001315 iwe.u.bitrate.value = 22 * 500000;
1316 current_val = iwe_stream_add_value(start, current_val,
1317 stop, &iwe, IW_EV_PARAM_LEN);
1318 }
1319 /* Check if we added any event */
1320 if((current_val - start) > IW_EV_LCP_LEN)
1321 start = current_val;
1322
1323 memset(&iwe, 0, sizeof(iwe));
1324 if (bss->wpa_ie_len) {
1325 char buf[MAX_WPA_IE_LEN];
1326 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
1327 iwe.cmd = IWEVGENIE;
1328 iwe.u.data.length = bss->wpa_ie_len;
1329 start = iwe_stream_add_point(start, stop, &iwe, buf);
1330 }
1331
1332 memset(&iwe, 0, sizeof(iwe));
1333 if (bss->rsn_ie_len) {
1334 char buf[MAX_WPA_IE_LEN];
1335 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
1336 iwe.cmd = IWEVGENIE;
1337 iwe.u.data.length = bss->rsn_ie_len;
1338 start = iwe_stream_add_point(start, stop, &iwe, buf);
1339 }
1340
Dan Williams00af0152007-08-02 13:14:56 -04001341 if (bss->mesh) {
1342 char custom[MAX_CUSTOM_LEN];
1343 char *p = custom;
1344
1345 iwe.cmd = IWEVCUSTOM;
1346 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
1347 "mesh-type: olpc");
1348 iwe.u.data.length = p - custom;
1349 if (iwe.u.data.length)
1350 start = iwe_stream_add_point(start, stop, &iwe, custom);
1351 }
1352
Holger Schurige56188a2007-10-09 14:15:19 +02001353out:
1354 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001355 return start;
1356}
1357
Holger Schurigffd074f2007-12-07 16:52:10 +01001358
1359/**
1360 * @brief Handle Scan Network ioctl
1361 *
1362 * @param dev A pointer to net_device structure
1363 * @param info A pointer to iw_request_info structure
1364 * @param vwrq A pointer to iw_param structure
1365 * @param extra A pointer to extra data buf
1366 *
1367 * @return 0 --success, otherwise fail
1368 */
1369int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
1370 struct iw_param *wrqu, char *extra)
1371{
1372 struct lbs_private *priv = dev->priv;
Holger Schurigffd074f2007-12-07 16:52:10 +01001373
1374 lbs_deb_enter(LBS_DEB_SCAN);
1375
1376 if (!netif_running(dev))
1377 return -ENETDOWN;
1378
1379 /* mac80211 does this:
1380 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1381 if (sdata->type != IEEE80211_IF_TYPE_xxx)
1382 return -EOPNOTSUPP;
1383
1384 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
1385 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1386 req = (struct iw_scan_req *)extra;
1387 ssid = req->essid;
1388 ssid_len = req->essid_len;
1389 }
1390 */
1391
1392 if (!delayed_work_pending(&priv->scan_work))
1393 queue_delayed_work(priv->work_thread, &priv->scan_work,
1394 msecs_to_jiffies(50));
1395 /* set marker that currently a scan is taking place */
David Woodhouseaa21c002007-12-08 20:04:36 +00001396 priv->last_scanned_channel = -1;
Holger Schurigffd074f2007-12-07 16:52:10 +01001397
David Woodhouseaa21c002007-12-08 20:04:36 +00001398 if (priv->surpriseremoved)
Holger Schurigffd074f2007-12-07 16:52:10 +01001399 return -EIO;
1400
1401 lbs_deb_leave(LBS_DEB_SCAN);
1402 return 0;
1403}
1404
1405
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001406/**
Holger Schurige56188a2007-10-09 14:15:19 +02001407 * @brief Handle Retrieve scan table ioctl
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001408 *
1409 * @param dev A pointer to net_device structure
1410 * @param info A pointer to iw_request_info structure
1411 * @param dwrq A pointer to iw_point structure
1412 * @param extra A pointer to extra data buf
1413 *
1414 * @return 0 --success, otherwise fail
1415 */
Holger Schurig10078322007-11-15 18:05:47 -05001416int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001417 struct iw_point *dwrq, char *extra)
1418{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001419#define SCAN_ITEM_SIZE 128
Holger Schurig69f90322007-11-23 15:43:44 +01001420 struct lbs_private *priv = dev->priv;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001421 int err = 0;
1422 char *ev = extra;
1423 char *stop = ev + dwrq->length;
1424 struct bss_descriptor * iter_bss;
1425 struct bss_descriptor * safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001426
Holger Schurige56188a2007-10-09 14:15:19 +02001427 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001428
Holger Schurigffd074f2007-12-07 16:52:10 +01001429 /* iwlist should wait until the current scan is finished */
David Woodhouseaa21c002007-12-08 20:04:36 +00001430 if (priv->last_scanned_channel)
Holger Schurigffd074f2007-12-07 16:52:10 +01001431 return -EAGAIN;
1432
Dan Williams80e78ef2007-05-25 22:18:47 -04001433 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
David Woodhouseaa21c002007-12-08 20:04:36 +00001434 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate) {
Holger Schurig10078322007-11-15 18:05:47 -05001435 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
Dan Williams0aef64d2007-08-02 11:31:18 -04001436 CMD_OPTION_WAITFORRSP, 0, NULL);
Dan Williams80e78ef2007-05-25 22:18:47 -04001437 }
1438
David Woodhouseaa21c002007-12-08 20:04:36 +00001439 mutex_lock(&priv->lock);
1440 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001441 char * next_ev;
1442 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001443
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001444 if (stop - ev < SCAN_ITEM_SIZE) {
1445 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001446 break;
1447 }
1448
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001449 /* For mesh device, list only mesh networks */
1450 if (dev == priv->mesh_dev && !iter_bss->mesh)
1451 continue;
1452
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001453 /* Prune old an old scan result */
1454 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1455 if (time_after(jiffies, stale_time)) {
1456 list_move_tail (&iter_bss->list,
David Woodhouseaa21c002007-12-08 20:04:36 +00001457 &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001458 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001459 continue;
1460 }
1461
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001462 /* Translate to WE format this entry */
Holger Schurig10078322007-11-15 18:05:47 -05001463 next_ev = lbs_translate_scan(priv, ev, stop, iter_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001464 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001465 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001466 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001467 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001468 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001469
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001470 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001471 dwrq->flags = 0;
1472
Holger Schurige56188a2007-10-09 14:15:19 +02001473 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", err);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001474 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001475}
1476
Holger Schurige56188a2007-10-09 14:15:19 +02001477
1478
1479
1480/*********************************************************************/
1481/* */
1482/* Command execution */
1483/* */
1484/*********************************************************************/
1485
1486
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001487/**
1488 * @brief Prepare a scan command to be sent to the firmware
1489 *
Holger Schurigffd074f2007-12-07 16:52:10 +01001490 * Called via lbs_prepare_and_send_command(priv, CMD_802_11_SCAN, ...)
1491 * from cmd.c
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001492 *
Paulius Zaleckasefad798b2008-02-03 15:42:53 +02001493 * Sends a fixed length data part (specifying the BSS type and BSSID filters)
Holger Schurige56188a2007-10-09 14:15:19 +02001494 * as well as a variable number/length of TLVs to the firmware.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001495 *
Holger Schurig69f90322007-11-23 15:43:44 +01001496 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001497 * @param cmd A pointer to cmd_ds_command structure to be sent to
1498 * firmware with the cmd_DS_801_11_SCAN structure
Holger Schurig10078322007-11-15 18:05:47 -05001499 * @param pdata_buf Void pointer cast of a lbs_scan_cmd_config struct used
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001500 * to set the fields/TLVs for the command sent to firmware
1501 *
1502 * @return 0 or -1
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001503 */
Holger Schurig69f90322007-11-23 15:43:44 +01001504int lbs_cmd_80211_scan(struct lbs_private *priv,
Holger Schurigffd074f2007-12-07 16:52:10 +01001505 struct cmd_ds_command *cmd, void *pdata_buf)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001506{
1507 struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
Holger Schurig10078322007-11-15 18:05:47 -05001508 struct lbs_scan_cmd_config *pscancfg = pdata_buf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001509
Holger Schurige56188a2007-10-09 14:15:19 +02001510 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001511
1512 /* Set fixed field variables in scan command */
1513 pscan->bsstype = pscancfg->bsstype;
Dan Williams492b6da2007-08-02 11:16:07 -04001514 memcpy(pscan->bssid, pscancfg->bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001515 memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
1516
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001517 /* size is equal to the sizeof(fixed portions) + the TLV len + header */
Dan Williams492b6da2007-08-02 11:16:07 -04001518 cmd->size = cpu_to_le16(sizeof(pscan->bsstype) + ETH_ALEN
1519 + pscancfg->tlvbufferlen + S_DS_GEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001520
Holger Schurige56188a2007-10-09 14:15:19 +02001521 lbs_deb_leave(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001522 return 0;
1523}
1524
1525/**
1526 * @brief This function handles the command response of scan
1527 *
Holger Schurige56188a2007-10-09 14:15:19 +02001528 * Called from handle_cmd_response() in cmdrespc.
1529 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001530 * The response buffer for the scan command has the following
1531 * memory layout:
1532 *
1533 * .-----------------------------------------------------------.
1534 * | header (4 * sizeof(u16)): Standard command response hdr |
1535 * .-----------------------------------------------------------.
1536 * | bufsize (u16) : sizeof the BSS Description data |
1537 * .-----------------------------------------------------------.
1538 * | NumOfSet (u8) : Number of BSS Descs returned |
1539 * .-----------------------------------------------------------.
1540 * | BSSDescription data (variable, size given in bufsize) |
1541 * .-----------------------------------------------------------.
1542 * | TLV data (variable, size calculated using header->size, |
1543 * | bufsize and sizeof the fixed fields above) |
1544 * .-----------------------------------------------------------.
1545 *
Holger Schurig69f90322007-11-23 15:43:44 +01001546 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001547 * @param resp A pointer to cmd_ds_command
1548 *
1549 * @return 0 or -1
1550 */
Holger Schurig69f90322007-11-23 15:43:44 +01001551int lbs_ret_80211_scan(struct lbs_private *priv, struct cmd_ds_command *resp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001552{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001553 struct cmd_ds_802_11_scan_rsp *pscan;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001554 struct bss_descriptor * iter_bss;
1555 struct bss_descriptor * safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001556 u8 *pbssinfo;
1557 u16 scanrespsize;
1558 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001559 int idx;
1560 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001561 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001562
Holger Schurige56188a2007-10-09 14:15:19 +02001563 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001564
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001565 /* Prune old entries from scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001566 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001567 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1568 if (time_before(jiffies, stale_time))
1569 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +00001570 list_move_tail (&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001571 clear_bss_descriptor(iter_bss);
1572 }
1573
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001574 pscan = &resp->params.scanresp;
1575
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001576 if (pscan->nr_sets > MAX_NETWORK_COUNT) {
1577 lbs_deb_scan(
1578 "SCAN_RESP: too many scan results (%d, max %d)!!\n",
1579 pscan->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001580 ret = -1;
1581 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001582 }
1583
David Woodhousee7240ac2007-12-11 17:54:36 -05001584 bytesleft = le16_to_cpu(pscan->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001585 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001586
David Woodhousee7240ac2007-12-11 17:54:36 -05001587 scanrespsize = le16_to_cpu(resp->size);
1588 lbs_deb_scan("SCAN_RESP: scan results %d\n", pscan->nr_sets);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001589
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001590 pbssinfo = pscan->bssdesc_and_tlvbuffer;
1591
1592 /* The size of the TLV buffer is equal to the entire command response
1593 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1594 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1595 * response header (S_DS_GEN)
1596 */
1597 tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
1598 + sizeof(pscan->nr_sets)
1599 + S_DS_GEN);
1600
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001601 /*
1602 * Process each scan response returned (pscan->nr_sets). Save
1603 * the information in the newbssentry and then insert into the
1604 * driver scan table either as an update to an existing entry
1605 * or as an addition at the end of the table
1606 */
1607 for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001608 struct bss_descriptor new;
1609 struct bss_descriptor * found = NULL;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001610 struct bss_descriptor * oldest = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07001611 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001612
1613 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001614 memset(&new, 0, sizeof (struct bss_descriptor));
Holger Schurig10078322007-11-15 18:05:47 -05001615 if (lbs_process_bss(&new, &pbssinfo, &bytesleft) != 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001616 /* error parsing the scan response, skipped */
1617 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1618 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001619 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001620
1621 /* Try to find this bss in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001622 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001623 if (is_same_network(iter_bss, &new)) {
1624 found = iter_bss;
1625 break;
1626 }
1627
1628 if ((oldest == NULL) ||
1629 (iter_bss->last_scanned < oldest->last_scanned))
1630 oldest = iter_bss;
1631 }
1632
1633 if (found) {
1634 /* found, clear it */
1635 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001636 } else if (!list_empty(&priv->network_free_list)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001637 /* Pull one from the free list */
David Woodhouseaa21c002007-12-08 20:04:36 +00001638 found = list_entry(priv->network_free_list.next,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001639 struct bss_descriptor, list);
David Woodhouseaa21c002007-12-08 20:04:36 +00001640 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001641 } else if (oldest) {
1642 /* If there are no more slots, expire the oldest */
1643 found = oldest;
1644 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001645 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001646 } else {
1647 continue;
1648 }
1649
Holger Schurigffd074f2007-12-07 16:52:10 +01001650 lbs_deb_scan("SCAN_RESP: BSSID %s\n",
Joe Perches0795af52007-10-03 17:59:30 -07001651 print_mac(mac, new.bssid));
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001652
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001653 /* Copy the locally created newbssentry to the scan table */
1654 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001655 }
1656
Holger Schurig9012b282007-05-25 11:27:16 -04001657 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001658
Holger Schurig9012b282007-05-25 11:27:16 -04001659done:
1660 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1661 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001662}