blob: 9a61188b62e92cf2fe9984c5ca58f4ac8cbd8570 [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
Dan Williamsfcdb53d2007-05-25 16:15:56 -040076static inline void clear_bss_descriptor (struct bss_descriptor * bss)
77{
78 /* Don't blow away ->list, just BSS data */
79 memset(bss, 0, offsetof(struct bss_descriptor, list));
80}
81
Holger Schurigffd074f2007-12-07 16:52:10 +010082/**
83 * @brief Compare two SSIDs
84 *
85 * @param ssid1 A pointer to ssid to compare
86 * @param ssid2 A pointer to ssid to compare
87 *
88 * @return 0: ssid is same, otherwise is different
89 */
90int lbs_ssid_cmp(u8 *ssid1, u8 ssid1_len, u8 *ssid2, u8 ssid2_len)
91{
92 if (ssid1_len != ssid2_len)
93 return -1;
94
95 return memcmp(ssid1, ssid2, ssid1_len);
96}
97
Holger Schurig10078322007-11-15 18:05:47 -050098static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
Dan Williamsfcdb53d2007-05-25 16:15:56 -040099 struct bss_descriptor * match_bss)
100{
101 if ( !secinfo->wep_enabled
102 && !secinfo->WPAenabled
103 && !secinfo->WPA2enabled
Dan Williamsab617972007-08-02 10:48:02 -0400104 && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
105 && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
Dan Williams0c9ca692007-08-02 10:43:44 -0400106 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400107 return 1;
108 }
109 return 0;
110}
111
Holger Schurig10078322007-11-15 18:05:47 -0500112static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400113 struct bss_descriptor * match_bss)
114{
115 if ( secinfo->wep_enabled
116 && !secinfo->WPAenabled
117 && !secinfo->WPA2enabled
Dan Williams0c9ca692007-08-02 10:43:44 -0400118 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400119 return 1;
120 }
121 return 0;
122}
123
Holger Schurig10078322007-11-15 18:05:47 -0500124static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400125 struct bss_descriptor * match_bss)
126{
127 if ( !secinfo->wep_enabled
128 && secinfo->WPAenabled
Dan Williamsab617972007-08-02 10:48:02 -0400129 && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400130 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
Dan Williams0c9ca692007-08-02 10:43:44 -0400131 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
132 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400133 ) {
134 return 1;
135 }
136 return 0;
137}
138
Holger Schurig10078322007-11-15 18:05:47 -0500139static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400140 struct bss_descriptor * match_bss)
141{
142 if ( !secinfo->wep_enabled
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400143 && secinfo->WPA2enabled
Dan Williamsab617972007-08-02 10:48:02 -0400144 && (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400145 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
Dan Williams0c9ca692007-08-02 10:43:44 -0400146 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
147 */
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400148 ) {
149 return 1;
150 }
151 return 0;
152}
153
Holger Schurig10078322007-11-15 18:05:47 -0500154static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400155 struct bss_descriptor * match_bss)
156{
157 if ( !secinfo->wep_enabled
158 && !secinfo->WPAenabled
159 && !secinfo->WPA2enabled
Dan Williamsab617972007-08-02 10:48:02 -0400160 && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
161 && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
Dan Williams0c9ca692007-08-02 10:43:44 -0400162 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400163 return 1;
164 }
165 return 0;
166}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200167
Holger Schurigffd074f2007-12-07 16:52:10 +0100168static inline int is_same_network(struct bss_descriptor *src,
169 struct bss_descriptor *dst)
170{
171 /* A network is only a duplicate if the channel, BSSID, and ESSID
172 * all match. We treat all <hidden> with the same BSSID and channel
173 * as one network */
174 return ((src->ssid_len == dst->ssid_len) &&
175 (src->channel == dst->channel) &&
176 !compare_ether_addr(src->bssid, dst->bssid) &&
177 !memcmp(src->ssid, dst->ssid, src->ssid_len));
178}
179
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200180/**
181 * @brief Check if a scanned network compatible with the driver settings
182 *
183 * WEP WPA WPA2 ad-hoc encrypt Network
184 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
185 * 0 0 0 0 NONE 0 0 0 yes No security
186 * 1 0 0 0 NONE 1 0 0 yes Static WEP
187 * 0 1 0 0 x 1x 1 x yes WPA
188 * 0 0 1 0 x 1x x 1 yes WPA2
189 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
190 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
191 *
192 *
David Woodhouseaa21c002007-12-08 20:04:36 +0000193 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200194 * @param index Index in scantable to check against current driver settings
195 * @param mode Network mode: Infrastructure or IBSS
196 *
197 * @return Index in scantable, or error code if negative
198 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000199static int is_network_compatible(struct lbs_private *priv,
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400200 struct bss_descriptor * bss, u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200201{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400202 int matched = 0;
203
Holger Schurige56188a2007-10-09 14:15:19 +0200204 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200205
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400206 if (bss->mode != mode)
207 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200208
David Woodhouseaa21c002007-12-08 20:04:36 +0000209 if ((matched = match_bss_no_security(&priv->secinfo, bss))) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400210 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000211 } else if ((matched = match_bss_static_wep(&priv->secinfo, bss))) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400212 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000213 } else if ((matched = match_bss_wpa(&priv->secinfo, bss))) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400214 lbs_deb_scan(
Holger Schurigffd074f2007-12-07 16:52:10 +0100215 "is_network_compatible() WPA: wpa_ie 0x%x "
216 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
217 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
David Woodhouseaa21c002007-12-08 20:04:36 +0000218 priv->secinfo.wep_enabled ? "e" : "d",
219 priv->secinfo.WPAenabled ? "e" : "d",
220 priv->secinfo.WPA2enabled ? "e" : "d",
Dan Williams0c9ca692007-08-02 10:43:44 -0400221 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400222 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000223 } else if ((matched = match_bss_wpa2(&priv->secinfo, bss))) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400224 lbs_deb_scan(
Holger Schurigffd074f2007-12-07 16:52:10 +0100225 "is_network_compatible() WPA2: wpa_ie 0x%x "
226 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
227 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
David Woodhouseaa21c002007-12-08 20:04:36 +0000228 priv->secinfo.wep_enabled ? "e" : "d",
229 priv->secinfo.WPAenabled ? "e" : "d",
230 priv->secinfo.WPA2enabled ? "e" : "d",
Dan Williams0c9ca692007-08-02 10:43:44 -0400231 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400232 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000233 } else if ((matched = match_bss_dynamic_wep(&priv->secinfo, bss))) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400234 lbs_deb_scan(
235 "is_network_compatible() dynamic WEP: "
Holger Schurigffd074f2007-12-07 16:52:10 +0100236 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
Dan Williams0c9ca692007-08-02 10:43:44 -0400237 bss->wpa_ie[0], bss->rsn_ie[0],
238 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Holger Schurig9012b282007-05-25 11:27:16 -0400239 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200240 }
241
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400242 /* bss security settings don't match those configured on card */
243 lbs_deb_scan(
Holger Schurigffd074f2007-12-07 16:52:10 +0100244 "is_network_compatible() FAILED: wpa_ie 0x%x "
245 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400246 bss->wpa_ie[0], bss->rsn_ie[0],
David Woodhouseaa21c002007-12-08 20:04:36 +0000247 priv->secinfo.wep_enabled ? "e" : "d",
248 priv->secinfo.WPAenabled ? "e" : "d",
249 priv->secinfo.WPA2enabled ? "e" : "d",
Dan Williams0c9ca692007-08-02 10:43:44 -0400250 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Holger Schurig9012b282007-05-25 11:27:16 -0400251
252done:
Holger Schurige56188a2007-10-09 14:15:19 +0200253 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400254 return matched;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200255}
256
Holger Schurige56188a2007-10-09 14:15:19 +0200257
258
259
260/*********************************************************************/
261/* */
262/* Main scanning support */
263/* */
264/*********************************************************************/
265
Holger Schurigffd074f2007-12-07 16:52:10 +0100266void lbs_scan_worker(struct work_struct *work)
267{
268 struct lbs_private *priv =
269 container_of(work, struct lbs_private, scan_work.work);
270
271 lbs_deb_enter(LBS_DEB_SCAN);
272 lbs_scan_networks(priv, NULL, 0);
273 lbs_deb_leave(LBS_DEB_SCAN);
274}
275
Holger Schurige56188a2007-10-09 14:15:19 +0200276
277/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200278 * @brief Create a channel list for the driver to scan based on region info
279 *
Holger Schurig10078322007-11-15 18:05:47 -0500280 * Only used from lbs_scan_setup_scan_config()
Holger Schurige56188a2007-10-09 14:15:19 +0200281 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200282 * Use the driver region/band information to construct a comprehensive list
283 * of channels to scan. This routine is used for any scan that is not
284 * provided a specific channel list to scan.
285 *
Holger Schurig69f90322007-11-23 15:43:44 +0100286 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200287 * @param scanchanlist Output parameter: resulting channel list to scan
288 * @param filteredscan Flag indicating whether or not a BSSID or SSID filter
289 * is being sent in the command to firmware. Used to
290 * increase the number of channels sent in a scan
291 * command and to disable the firmware channel scan
292 * filter.
293 *
294 * @return void
295 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100296static int lbs_scan_create_channel_list(struct lbs_private *priv,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200297 struct chanscanparamset * scanchanlist,
298 u8 filteredscan)
299{
300
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200301 struct region_channel *scanregion;
302 struct chan_freq_power *cfp;
303 int rgnidx;
304 int chanidx;
305 int nextchan;
306 u8 scantype;
307
308 chanidx = 0;
309
310 /* Set the default scan type to the user specified type, will later
311 * be changed to passive on a per channel basis if restricted by
312 * regulatory requirements (11d or 11h)
313 */
Holger Schurig4f2fdaa2007-08-02 13:12:27 -0400314 scantype = CMD_SCAN_TYPE_ACTIVE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200315
David Woodhouseaa21c002007-12-08 20:04:36 +0000316 for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
317 if (priv->enable11d &&
318 (priv->connect_status != LBS_CONNECTED) &&
319 (priv->mesh_connect_status != LBS_CONNECTED)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200320 /* Scan all the supported chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000321 if (!priv->universal_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200322 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000323 scanregion = &priv->universal_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200324
325 /* clear the parsed_region_chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000326 memset(&priv->parsed_region_chan, 0x00,
327 sizeof(priv->parsed_region_chan));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200328 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +0000329 if (!priv->region_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200330 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000331 scanregion = &priv->region_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200332 }
333
334 for (nextchan = 0;
335 nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
336
337 cfp = scanregion->CFP + nextchan;
338
David Woodhouseaa21c002007-12-08 20:04:36 +0000339 if (priv->enable11d) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200340 scantype =
Holger Schurig10078322007-11-15 18:05:47 -0500341 lbs_get_scan_type_11d(cfp->channel,
David Woodhouseaa21c002007-12-08 20:04:36 +0000342 &priv->
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200343 parsed_region_chan);
344 }
345
346 switch (scanregion->band) {
347 case BAND_B:
348 case BAND_G:
349 default:
350 scanchanlist[chanidx].radiotype =
Dan Williams0aef64d2007-08-02 11:31:18 -0400351 CMD_SCAN_RADIO_TYPE_BG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200352 break;
353 }
354
Dan Williams0aef64d2007-08-02 11:31:18 -0400355 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200356 scanchanlist[chanidx].maxscantime =
David Woodhouse981f1872007-05-25 23:36:54 -0400357 cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200358 scanchanlist[chanidx].chanscanmode.passivescan =
359 1;
360 } else {
361 scanchanlist[chanidx].maxscantime =
David Woodhouse981f1872007-05-25 23:36:54 -0400362 cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200363 scanchanlist[chanidx].chanscanmode.passivescan =
364 0;
365 }
366
367 scanchanlist[chanidx].channumber = cfp->channel;
368
369 if (filteredscan) {
370 scanchanlist[chanidx].chanscanmode.
371 disablechanfilt = 1;
372 }
373 }
374 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100375 return chanidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200376}
377
Dan Williams2afc0c52007-08-02 13:19:04 -0400378
Holger Schurige56188a2007-10-09 14:15:19 +0200379/*
Holger Schurigffd074f2007-12-07 16:52:10 +0100380 * Add SSID TLV of the form:
381 *
382 * TLV-ID SSID 00 00
383 * length 06 00
384 * ssid 4d 4e 54 45 53 54
385 */
386static int lbs_scan_add_ssid_tlv(u8 *tlv,
387 const struct lbs_ioctl_user_scan_cfg *user_cfg)
Dan Williamseb8f7332007-05-25 16:25:21 -0400388{
Holger Schurigffd074f2007-12-07 16:52:10 +0100389 struct mrvlietypes_ssidparamset *ssid_tlv =
390 (struct mrvlietypes_ssidparamset *)tlv;
391 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
392 ssid_tlv->header.len = cpu_to_le16(user_cfg->ssid_len);
393 memcpy(ssid_tlv->ssid, user_cfg->ssid, user_cfg->ssid_len);
394 return sizeof(ssid_tlv->header) + user_cfg->ssid_len;
395}
Dan Williamseb8f7332007-05-25 16:25:21 -0400396
Holger Schurige56188a2007-10-09 14:15:19 +0200397
Holger Schurigffd074f2007-12-07 16:52:10 +0100398/*
399 * Add CHANLIST TLV of the form
400 *
401 * TLV-ID CHANLIST 01 01
402 * length 5b 00
403 * channel 1 00 01 00 00 00 64 00
404 * radio type 00
405 * channel 01
406 * scan type 00
407 * min scan time 00 00
408 * max scan time 64 00
409 * channel 2 00 02 00 00 00 64 00
410 * channel 3 00 03 00 00 00 64 00
411 * channel 4 00 04 00 00 00 64 00
412 * channel 5 00 05 00 00 00 64 00
413 * channel 6 00 06 00 00 00 64 00
414 * channel 7 00 07 00 00 00 64 00
415 * channel 8 00 08 00 00 00 64 00
416 * channel 9 00 09 00 00 00 64 00
417 * channel 10 00 0a 00 00 00 64 00
418 * channel 11 00 0b 00 00 00 64 00
419 * channel 12 00 0c 00 00 00 64 00
420 * channel 13 00 0d 00 00 00 64 00
421 *
422 */
423static int lbs_scan_add_chanlist_tlv(u8 *tlv,
424 struct chanscanparamset *chan_list,
425 int chan_count)
426{
427 size_t size = sizeof(struct chanscanparamset) * chan_count;
428 struct mrvlietypes_chanlistparamset *chan_tlv =
429 (struct mrvlietypes_chanlistparamset *) tlv;
Dan Williamseb8f7332007-05-25 16:25:21 -0400430
Holger Schurigffd074f2007-12-07 16:52:10 +0100431 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
432 memcpy(chan_tlv->chanscanparam, chan_list, size);
433 chan_tlv->header.len = cpu_to_le16(size);
434 return sizeof(chan_tlv->header) + size;
435}
Dan Williamseb8f7332007-05-25 16:25:21 -0400436
Holger Schurigffd074f2007-12-07 16:52:10 +0100437
438/*
439 * Add RATES TLV of the form
440 *
441 * TLV-ID RATES 01 00
442 * length 0e 00
443 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
444 *
445 * The rates are in lbs_bg_rates[], but for the 802.11b
446 * rates the high bit isn't set.
447 */
448static int lbs_scan_add_rates_tlv(u8 *tlv)
449{
450 int i;
451 struct mrvlietypes_ratesparamset *rate_tlv =
452 (struct mrvlietypes_ratesparamset *) tlv;
453
454 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
455 tlv += sizeof(rate_tlv->header);
456 for (i = 0; i < MAX_RATES; i++) {
457 *tlv = lbs_bg_rates[i];
458 if (*tlv == 0)
459 break;
460 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
461 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
462 Note that the values are MBit/s * 2, to mark them as
463 basic rates so that the firmware likes it better */
464 if (*tlv == 0x02 || *tlv == 0x04 ||
465 *tlv == 0x0b || *tlv == 0x16)
466 *tlv |= 0x80;
467 tlv++;
Dan Williamseb8f7332007-05-25 16:25:21 -0400468 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100469 rate_tlv->header.len = cpu_to_le16(i);
470 return sizeof(rate_tlv->header) + i;
471}
Dan Williamseb8f7332007-05-25 16:25:21 -0400472
Holger Schurigffd074f2007-12-07 16:52:10 +0100473
474/*
475 * Generate the CMD_802_11_SCAN command with the proper tlv
476 * for a bunch of channels.
477 */
478static int lbs_do_scan(struct lbs_private *priv,
479 u8 bsstype,
480 struct chanscanparamset *chan_list,
481 int chan_count,
482 const struct lbs_ioctl_user_scan_cfg *user_cfg)
483{
484 int ret = -ENOMEM;
485 struct lbs_scan_cmd_config *scan_cmd;
486 u8 *tlv; /* pointer into our current, growing TLV storage area */
487
488 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, "
489 "chan_count %d",
490 bsstype, chan_list[0].channumber, chan_count);
491
492 /* create the fixed part for scan command */
493 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
494 if (scan_cmd == NULL)
Holger Schurige56188a2007-10-09 14:15:19 +0200495 goto out;
Holger Schurigffd074f2007-12-07 16:52:10 +0100496 tlv = scan_cmd->tlvbuffer;
497 if (user_cfg)
498 memcpy(scan_cmd->bssid, user_cfg->bssid, ETH_ALEN);
499 scan_cmd->bsstype = bsstype;
Dan Williamseb8f7332007-05-25 16:25:21 -0400500
Holger Schurigffd074f2007-12-07 16:52:10 +0100501 /* add TLVs */
502 if (user_cfg && user_cfg->ssid_len)
503 tlv += lbs_scan_add_ssid_tlv(tlv, user_cfg);
504 if (chan_list && chan_count)
505 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
506 tlv += lbs_scan_add_rates_tlv(tlv);
Dan Williamseb8f7332007-05-25 16:25:21 -0400507
Holger Schurigffd074f2007-12-07 16:52:10 +0100508 /* This is the final data we are about to send */
509 scan_cmd->tlvbufferlen = tlv - scan_cmd->tlvbuffer;
510 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd, 1+6);
511 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
512 scan_cmd->tlvbufferlen);
Dan Williamseb8f7332007-05-25 16:25:21 -0400513
Holger Schurigffd074f2007-12-07 16:52:10 +0100514 ret = lbs_prepare_and_send_command(priv, CMD_802_11_SCAN, 0,
515 CMD_OPTION_WAITFORRSP, 0, scan_cmd);
Holger Schurige56188a2007-10-09 14:15:19 +0200516out:
Holger Schurigffd074f2007-12-07 16:52:10 +0100517 kfree(scan_cmd);
518 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
519 return ret;
Dan Williamseb8f7332007-05-25 16:25:21 -0400520}
521
522
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200523/**
524 * @brief Internal function used to start a scan based on an input config
525 *
Holger Schurige56188a2007-10-09 14:15:19 +0200526 * Also used from debugfs
527 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200528 * Use the input user scan configuration information when provided in
529 * order to send the appropriate scan commands to firmware to populate or
530 * update the internal driver scan table
531 *
Holger Schurig69f90322007-11-23 15:43:44 +0100532 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533 * @param puserscanin Pointer to the input configuration for the requested
534 * scan.
535 *
536 * @return 0 or < 0 if error
537 */
Holger Schurig69f90322007-11-23 15:43:44 +0100538int lbs_scan_networks(struct lbs_private *priv,
Holger Schurigffd074f2007-12-07 16:52:10 +0100539 const struct lbs_ioctl_user_scan_cfg *user_cfg,
Dan Williams2afc0c52007-08-02 13:19:04 -0400540 int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200541{
Holger Schurigffd074f2007-12-07 16:52:10 +0100542 int ret = -ENOMEM;
543 struct chanscanparamset *chan_list;
544 struct chanscanparamset *curr_chans;
545 int chan_count;
546 u8 bsstype = CMD_BSS_TYPE_ANY;
547 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
548 int filteredscan = 0;
549 union iwreq_data wrqu;
Dan Williamsf8f55102007-05-30 10:12:55 -0400550#ifdef CONFIG_LIBERTAS_DEBUG
Holger Schurigffd074f2007-12-07 16:52:10 +0100551 struct bss_descriptor *iter;
Dan Williamsf8f55102007-05-30 10:12:55 -0400552 int i = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700553 DECLARE_MAC_BUF(mac);
Dan Williamsf8f55102007-05-30 10:12:55 -0400554#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200555
Holger Schurigffd074f2007-12-07 16:52:10 +0100556 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d",
557 full_scan);
Dan Williams2afc0c52007-08-02 13:19:04 -0400558
559 /* Cancel any partial outstanding partial scans if this scan
560 * is a full scan.
561 */
562 if (full_scan && delayed_work_pending(&priv->scan_work))
563 cancel_delayed_work(&priv->scan_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200564
Holger Schurigffd074f2007-12-07 16:52:10 +0100565 /* Determine same scan parameters */
566 if (user_cfg) {
567 if (user_cfg->bsstype)
568 bsstype = user_cfg->bsstype;
569 if (compare_ether_addr(user_cfg->bssid, &zeromac[0]) != 0) {
570 numchannels = MRVDRV_MAX_CHANNELS_PER_SCAN;
571 filteredscan = 1;
Holger Schurig3cf84092007-08-02 11:50:12 -0400572 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200573 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100574 lbs_deb_scan("numchannels %d, bsstype %d, "
575 "filteredscan %d\n",
576 numchannels, bsstype, filteredscan);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200577
Holger Schurigffd074f2007-12-07 16:52:10 +0100578 /* Create list of channels to scan */
579 chan_list = kzalloc(sizeof(struct chanscanparamset) *
580 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
581 if (!chan_list) {
582 lbs_pr_alert("SCAN: chan_list empty\n");
583 goto out;
584 }
585
586 /* We want to scan all channels */
587 chan_count = lbs_scan_create_channel_list(priv, chan_list,
588 filteredscan);
589
590 netif_stop_queue(priv->dev);
591 netif_carrier_off(priv->dev);
592 if (priv->mesh_dev) {
David Woodhousea27b9f92007-12-12 00:41:51 -0500593 netif_stop_queue(priv->mesh_dev);
594 netif_carrier_off(priv->mesh_dev);
Holger Schurigffd074f2007-12-07 16:52:10 +0100595 }
596
597 /* Prepare to continue an interrupted scan */
598 lbs_deb_scan("chan_count %d, last_scanned_channel %d\n",
David Woodhousea27b9f92007-12-12 00:41:51 -0500599 chan_count, priv->last_scanned_channel);
Holger Schurigffd074f2007-12-07 16:52:10 +0100600 curr_chans = chan_list;
601 /* advance channel list by already-scanned-channels */
David Woodhouseaa21c002007-12-08 20:04:36 +0000602 if (priv->last_scanned_channel > 0) {
603 curr_chans += priv->last_scanned_channel;
604 chan_count -= priv->last_scanned_channel;
Holger Schurigffd074f2007-12-07 16:52:10 +0100605 }
606
607 /* Send scan command(s)
608 * numchannels contains the number of channels we should maximally scan
609 * chan_count is the total number of channels to scan
610 */
611
612 while (chan_count) {
613 int to_scan = min(numchannels, chan_count);
614 lbs_deb_scan("scanning %d of %d channels\n",
615 to_scan, chan_count);
616 ret = lbs_do_scan(priv, bsstype, curr_chans,
617 to_scan, user_cfg);
618 if (ret) {
619 lbs_pr_err("SCAN_CMD failed\n");
620 goto out2;
621 }
622 curr_chans += to_scan;
623 chan_count -= to_scan;
624
625 /* somehow schedule the next part of the scan */
626 if (chan_count &&
627 !full_scan &&
David Woodhouseaa21c002007-12-08 20:04:36 +0000628 !priv->surpriseremoved) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100629 /* -1 marks just that we're currently scanning */
David Woodhouseaa21c002007-12-08 20:04:36 +0000630 if (priv->last_scanned_channel < 0)
631 priv->last_scanned_channel = to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100632 else
David Woodhouseaa21c002007-12-08 20:04:36 +0000633 priv->last_scanned_channel += to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100634 cancel_delayed_work(&priv->scan_work);
635 queue_delayed_work(priv->work_thread, &priv->scan_work,
636 msecs_to_jiffies(300));
637 /* skip over GIWSCAN event */
638 goto out;
639 }
640
641 }
642 memset(&wrqu, 0, sizeof(union iwreq_data));
643 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200644
Dan Williamsf8f55102007-05-30 10:12:55 -0400645#ifdef CONFIG_LIBERTAS_DEBUG
646 /* Dump the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000647 mutex_lock(&priv->lock);
Holger Schurigffd074f2007-12-07 16:52:10 +0100648 lbs_deb_scan("scan table:\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000649 list_for_each_entry(iter, &priv->network_list, list)
Holger Schurigffd074f2007-12-07 16:52:10 +0100650 lbs_deb_scan("%02d: BSSID %s, RSSI %d, SSID '%s'\n",
651 i++, print_mac(mac, iter->bssid), (s32) iter->rssi,
652 escape_essid(iter->ssid, iter->ssid_len));
David Woodhouseaa21c002007-12-08 20:04:36 +0000653 mutex_unlock(&priv->lock);
Dan Williamsf8f55102007-05-30 10:12:55 -0400654#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200655
Holger Schurigffd074f2007-12-07 16:52:10 +0100656out2:
David Woodhouseaa21c002007-12-08 20:04:36 +0000657 priv->last_scanned_channel = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100658
659out:
David Woodhouseaa21c002007-12-08 20:04:36 +0000660 if (priv->connect_status == LBS_CONNECTED) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400661 netif_carrier_on(priv->dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500662 if (!priv->tx_pending_len)
663 netif_wake_queue(priv->dev);
Brajesh Dave01d77d82007-11-20 17:44:14 -0500664 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000665 if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
Brajesh Dave01d77d82007-11-20 17:44:14 -0500666 netif_carrier_on(priv->mesh_dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500667 if (!priv->tx_pending_len)
668 netif_wake_queue(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200669 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100670 kfree(chan_list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200671
Holger Schurig9012b282007-05-25 11:27:16 -0400672 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200673 return ret;
674}
675
Holger Schurigffd074f2007-12-07 16:52:10 +0100676
677
678
679/*********************************************************************/
680/* */
681/* Result interpretation */
682/* */
683/*********************************************************************/
684
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200685/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200686 * @brief Interpret a BSS scan response returned from the firmware
687 *
688 * Parse the various fixed fields and IEs passed back for a a BSS probe
Holger Schurigffd074f2007-12-07 16:52:10 +0100689 * response or beacon from the scan command. Record information as needed
690 * in the scan table struct bss_descriptor for that entry.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200691 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400692 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200693 *
694 * @return 0 or -1
695 */
Holger Schurig10078322007-11-15 18:05:47 -0500696static int lbs_process_bss(struct bss_descriptor *bss,
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400697 u8 ** pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200698{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200699 struct ieeetypes_fhparamset *pFH;
700 struct ieeetypes_dsparamset *pDS;
701 struct ieeetypes_cfparamset *pCF;
702 struct ieeetypes_ibssparamset *pibss;
Joe Perches0795af52007-10-03 17:59:30 -0700703 DECLARE_MAC_BUF(mac);
Dan Williams8c512762007-08-02 11:40:45 -0400704 struct ieeetypes_countryinfoset *pcountryinfo;
705 u8 *pos, *end, *p;
706 u8 n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
707 u16 beaconsize = 0;
Holger Schurig9012b282007-05-25 11:27:16 -0400708 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200709
Holger Schurige56188a2007-10-09 14:15:19 +0200710 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200711
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200712 if (*bytesleft >= sizeof(beaconsize)) {
713 /* Extract & convert beacon size from the command buffer */
David Woodhousee7240ac2007-12-11 17:54:36 -0500714 beaconsize = le16_to_cpu(get_unaligned((__le16 *)*pbeaconinfo));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200715 *bytesleft -= sizeof(beaconsize);
716 *pbeaconinfo += sizeof(beaconsize);
717 }
718
719 if (beaconsize == 0 || beaconsize > *bytesleft) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200720 *pbeaconinfo += *bytesleft;
721 *bytesleft = 0;
Holger Schurige56188a2007-10-09 14:15:19 +0200722 ret = -1;
723 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200724 }
725
726 /* Initialize the current working beacon pointer for this BSS iteration */
Dan Williamsab617972007-08-02 10:48:02 -0400727 pos = *pbeaconinfo;
728 end = pos + beaconsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200729
730 /* Advance the return beacon pointer past the current beacon */
731 *pbeaconinfo += beaconsize;
732 *bytesleft -= beaconsize;
733
Dan Williamsab617972007-08-02 10:48:02 -0400734 memcpy(bss->bssid, pos, ETH_ALEN);
Holger Schurigffd074f2007-12-07 16:52:10 +0100735 lbs_deb_scan("process_bss: BSSID %s\n", print_mac(mac, bss->bssid));
Dan Williamsab617972007-08-02 10:48:02 -0400736 pos += ETH_ALEN;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200737
Dan Williamsab617972007-08-02 10:48:02 -0400738 if ((end - pos) < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400739 lbs_deb_scan("process_bss: Not enough bytes left\n");
Holger Schurige56188a2007-10-09 14:15:19 +0200740 ret = -1;
741 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200742 }
743
744 /*
745 * next 4 fields are RSSI, time stamp, beacon interval,
746 * and capability information
747 */
748
749 /* RSSI is 1 byte long */
Dan Williamsab617972007-08-02 10:48:02 -0400750 bss->rssi = *pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100751 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
Dan Williamsab617972007-08-02 10:48:02 -0400752 pos++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200753
754 /* time stamp is 8 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400755 pos += 8;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200756
757 /* beacon interval is 2 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400758 bss->beaconperiod = le16_to_cpup((void *) pos);
759 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200760
761 /* capability information is 2 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400762 bss->capability = le16_to_cpup((void *) pos);
Holger Schurigffd074f2007-12-07 16:52:10 +0100763 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
Dan Williamsab617972007-08-02 10:48:02 -0400764 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200765
Dan Williams0c9ca692007-08-02 10:43:44 -0400766 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
Holger Schurigffd074f2007-12-07 16:52:10 +0100767 lbs_deb_scan("process_bss: WEP enabled\n");
Dan Williams0c9ca692007-08-02 10:43:44 -0400768 if (bss->capability & WLAN_CAPABILITY_IBSS)
769 bss->mode = IW_MODE_ADHOC;
770 else
771 bss->mode = IW_MODE_INFRA;
772
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200773 /* rest of the current buffer are IE's */
Holger Schurigffd074f2007-12-07 16:52:10 +0100774 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
Holger Schurigece56192007-08-02 11:53:06 -0400775 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200776
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200777 /* process variable IE */
Dan Williamsab617972007-08-02 10:48:02 -0400778 while (pos <= end - 2) {
779 struct ieee80211_info_element * elem =
780 (struct ieee80211_info_element *) pos;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200781
Dan Williamsab617972007-08-02 10:48:02 -0400782 if (pos + elem->len > end) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400783 lbs_deb_scan("process_bss: error in processing IE, "
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200784 "bytes left < IE length\n");
Dan Williamsab617972007-08-02 10:48:02 -0400785 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200786 }
787
Dan Williamsab617972007-08-02 10:48:02 -0400788 switch (elem->id) {
789 case MFIE_TYPE_SSID:
790 bss->ssid_len = elem->len;
791 memcpy(bss->ssid, elem->data, elem->len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100792 lbs_deb_scan("got SSID IE: '%s', len %u\n",
Dan Williamsd8efea22007-05-28 23:54:55 -0400793 escape_essid(bss->ssid, bss->ssid_len),
794 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200795 break;
796
Dan Williamsab617972007-08-02 10:48:02 -0400797 case MFIE_TYPE_RATES:
Dan Williams8c512762007-08-02 11:40:45 -0400798 n_basic_rates = min_t(u8, MAX_RATES, elem->len);
799 memcpy(bss->rates, elem->data, n_basic_rates);
800 got_basic_rates = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100801 lbs_deb_scan("got RATES IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200802 break;
803
Dan Williamsab617972007-08-02 10:48:02 -0400804 case MFIE_TYPE_FH_SET:
805 pFH = (struct ieeetypes_fhparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400806 memmove(&bss->phyparamset.fhparamset, pFH,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200807 sizeof(struct ieeetypes_fhparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100808 lbs_deb_scan("got FH IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200809 break;
810
Dan Williamsab617972007-08-02 10:48:02 -0400811 case MFIE_TYPE_DS_SET:
812 pDS = (struct ieeetypes_dsparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400813 bss->channel = pDS->currentchan;
814 memcpy(&bss->phyparamset.dsparamset, pDS,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200815 sizeof(struct ieeetypes_dsparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100816 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200817 break;
818
Dan Williamsab617972007-08-02 10:48:02 -0400819 case MFIE_TYPE_CF_SET:
820 pCF = (struct ieeetypes_cfparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400821 memcpy(&bss->ssparamset.cfparamset, pCF,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200822 sizeof(struct ieeetypes_cfparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100823 lbs_deb_scan("got CF IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200824 break;
825
Dan Williamsab617972007-08-02 10:48:02 -0400826 case MFIE_TYPE_IBSS_SET:
827 pibss = (struct ieeetypes_ibssparamset *) pos;
David Woodhousee7240ac2007-12-11 17:54:36 -0500828 bss->atimwindow = le16_to_cpu(pibss->atimwindow);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400829 memmove(&bss->ssparamset.ibssparamset, pibss,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200830 sizeof(struct ieeetypes_ibssparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100831 lbs_deb_scan("got IBSS IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200832 break;
833
Dan Williamsab617972007-08-02 10:48:02 -0400834 case MFIE_TYPE_COUNTRY:
835 pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100836 lbs_deb_scan("got COUNTRY IE\n");
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400837 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200838 || pcountryinfo->len > 254) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400839 lbs_deb_scan("process_bss: 11D- Err "
Holger Schurigffd074f2007-12-07 16:52:10 +0100840 "CountryInfo len %d, min %zd, max 254\n",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200841 pcountryinfo->len,
842 sizeof(pcountryinfo->countrycode));
Holger Schurig9012b282007-05-25 11:27:16 -0400843 ret = -1;
844 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200845 }
846
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400847 memcpy(&bss->countryinfo,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200848 pcountryinfo, pcountryinfo->len + 2);
Holger Schurigece56192007-08-02 11:53:06 -0400849 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200850 (u8 *) pcountryinfo,
851 (u32) (pcountryinfo->len + 2));
852 break;
853
Dan Williamsab617972007-08-02 10:48:02 -0400854 case MFIE_TYPE_RATES_EX:
855 /* only process extended supported rate if data rate is
856 * already found. Data rate IE should come before
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200857 * extended supported rate IE
858 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100859 lbs_deb_scan("got RATESEX IE\n");
860 if (!got_basic_rates) {
861 lbs_deb_scan("... but ignoring it\n");
Dan Williamsab617972007-08-02 10:48:02 -0400862 break;
Holger Schurigffd074f2007-12-07 16:52:10 +0100863 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200864
Dan Williams8c512762007-08-02 11:40:45 -0400865 n_ex_rates = elem->len;
866 if (n_basic_rates + n_ex_rates > MAX_RATES)
867 n_ex_rates = MAX_RATES - n_basic_rates;
Dan Williamsab617972007-08-02 10:48:02 -0400868
Dan Williams8c512762007-08-02 11:40:45 -0400869 p = bss->rates + n_basic_rates;
870 memcpy(p, elem->data, n_ex_rates);
Dan Williamsab617972007-08-02 10:48:02 -0400871 break;
872
873 case MFIE_TYPE_GENERIC:
874 if (elem->len >= 4 &&
875 elem->data[0] == 0x00 &&
876 elem->data[1] == 0x50 &&
877 elem->data[2] == 0xf2 &&
878 elem->data[3] == 0x01) {
879 bss->wpa_ie_len = min(elem->len + 2,
880 MAX_WPA_IE_LEN);
881 memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100882 lbs_deb_scan("got WPA IE\n");
883 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie,
Dan Williamsab617972007-08-02 10:48:02 -0400884 elem->len);
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400885 } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
886 elem->data[0] == 0x00 &&
887 elem->data[1] == 0x50 &&
888 elem->data[2] == 0x43 &&
889 elem->data[3] == 0x04) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100890 lbs_deb_scan("got mesh IE\n");
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400891 bss->mesh = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100892 } else {
893 lbs_deb_scan("got generiec IE: "
894 "%02x:%02x:%02x:%02x, len %d\n",
895 elem->data[0], elem->data[1],
896 elem->data[2], elem->data[3],
897 elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200898 }
899 break;
900
Dan Williamsab617972007-08-02 10:48:02 -0400901 case MFIE_TYPE_RSN:
Holger Schurigffd074f2007-12-07 16:52:10 +0100902 lbs_deb_scan("got RSN IE\n");
Dan Williamsab617972007-08-02 10:48:02 -0400903 bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
904 memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100905 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
906 bss->rsn_ie, elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200907 break;
908
Dan Williamsab617972007-08-02 10:48:02 -0400909 default:
Holger Schurigffd074f2007-12-07 16:52:10 +0100910 lbs_deb_scan("got IE 0x%04x, len %d\n",
911 elem->id, elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200912 break;
913 }
914
Dan Williamsab617972007-08-02 10:48:02 -0400915 pos += elem->len + 2;
916 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400917
918 /* Timestamp */
919 bss->last_scanned = jiffies;
Holger Schurig10078322007-11-15 18:05:47 -0500920 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400921
Holger Schurig9012b282007-05-25 11:27:16 -0400922 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200923
Holger Schurig9012b282007-05-25 11:27:16 -0400924done:
925 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
926 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200927}
928
929/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200930 * @brief This function finds a specific compatible BSSID in the scan list
931 *
Holger Schurige56188a2007-10-09 14:15:19 +0200932 * Used in association code
933 *
David Woodhouseaa21c002007-12-08 20:04:36 +0000934 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200935 * @param bssid BSSID to find in the scan list
936 * @param mode Network mode: Infrastructure or IBSS
937 *
938 * @return index in BSSID list, or error return code (< 0)
939 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000940struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400941 u8 * bssid, u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200942{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400943 struct bss_descriptor * iter_bss;
944 struct bss_descriptor * found_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200945
Holger Schurige56188a2007-10-09 14:15:19 +0200946 lbs_deb_enter(LBS_DEB_SCAN);
947
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200948 if (!bssid)
Holger Schurige56188a2007-10-09 14:15:19 +0200949 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200950
Holger Schurigece56192007-08-02 11:53:06 -0400951 lbs_deb_hex(LBS_DEB_SCAN, "looking for",
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400952 bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200953
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400954 /* Look through the scan table for a compatible match. The loop will
955 * continue past a matched bssid that is not compatible in case there
956 * is an AP with multiple SSIDs assigned to the same BSSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200957 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000958 mutex_lock(&priv->lock);
959 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williams3cf20932007-05-25 17:28:30 -0400960 if (compare_ether_addr(iter_bss->bssid, bssid))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400961 continue; /* bssid doesn't match */
962 switch (mode) {
963 case IW_MODE_INFRA:
964 case IW_MODE_ADHOC:
David Woodhouseaa21c002007-12-08 20:04:36 +0000965 if (!is_network_compatible(priv, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200966 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400967 found_bss = iter_bss;
968 break;
969 default:
970 found_bss = iter_bss;
971 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200972 }
973 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000974 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200975
Holger Schurige56188a2007-10-09 14:15:19 +0200976out:
977 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400978 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200979}
980
981/**
982 * @brief This function finds ssid in ssid list.
983 *
Holger Schurige56188a2007-10-09 14:15:19 +0200984 * Used in association code
985 *
David Woodhouseaa21c002007-12-08 20:04:36 +0000986 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200987 * @param ssid SSID to find in the list
988 * @param bssid BSSID to qualify the SSID selection (if provided)
989 * @param mode Network mode: Infrastructure or IBSS
990 *
991 * @return index in BSSID list
992 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000993struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
Dan Williamsd8efea22007-05-28 23:54:55 -0400994 u8 *ssid, u8 ssid_len, u8 * bssid, u8 mode,
Dan Williamsaeea0ab2007-05-25 22:30:48 -0400995 int channel)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200996{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200997 u8 bestrssi = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400998 struct bss_descriptor * iter_bss = NULL;
999 struct bss_descriptor * found_bss = NULL;
1000 struct bss_descriptor * tmp_oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001001
Holger Schurige56188a2007-10-09 14:15:19 +02001002 lbs_deb_enter(LBS_DEB_SCAN);
1003
David Woodhouseaa21c002007-12-08 20:04:36 +00001004 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001005
David Woodhouseaa21c002007-12-08 20:04:36 +00001006 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001007 if ( !tmp_oldest
1008 || (iter_bss->last_scanned < tmp_oldest->last_scanned))
1009 tmp_oldest = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001010
Holger Schurig10078322007-11-15 18:05:47 -05001011 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001012 ssid, ssid_len) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001013 continue; /* ssid doesn't match */
Dan Williams3cf20932007-05-25 17:28:30 -04001014 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001015 continue; /* bssid doesn't match */
Dan Williamsaeea0ab2007-05-25 22:30:48 -04001016 if ((channel > 0) && (iter_bss->channel != channel))
1017 continue; /* channel doesn't match */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001018
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001019 switch (mode) {
1020 case IW_MODE_INFRA:
1021 case IW_MODE_ADHOC:
David Woodhouseaa21c002007-12-08 20:04:36 +00001022 if (!is_network_compatible(priv, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001023 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001024
1025 if (bssid) {
1026 /* Found requested BSSID */
1027 found_bss = iter_bss;
1028 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001029 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001030
1031 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1032 bestrssi = SCAN_RSSI(iter_bss->rssi);
1033 found_bss = iter_bss;
1034 }
1035 break;
1036 case IW_MODE_AUTO:
1037 default:
1038 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1039 bestrssi = SCAN_RSSI(iter_bss->rssi);
1040 found_bss = iter_bss;
1041 }
1042 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001043 }
1044 }
1045
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001046out:
David Woodhouseaa21c002007-12-08 20:04:36 +00001047 mutex_unlock(&priv->lock);
Holger Schurige56188a2007-10-09 14:15:19 +02001048 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001049 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001050}
1051
1052/**
1053 * @brief This function finds the best SSID in the Scan List
1054 *
1055 * Search the scan table for the best SSID that also matches the current
1056 * adapter network preference (infrastructure or adhoc)
1057 *
David Woodhouseaa21c002007-12-08 20:04:36 +00001058 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001059 *
1060 * @return index in BSSID list
1061 */
Holger Schurig69f90322007-11-23 15:43:44 +01001062static struct bss_descriptor *lbs_find_best_ssid_in_list(
David Woodhouseaa21c002007-12-08 20:04:36 +00001063 struct lbs_private *priv,
Holger Schurig69f90322007-11-23 15:43:44 +01001064 u8 mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001065{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001066 u8 bestrssi = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001067 struct bss_descriptor * iter_bss;
1068 struct bss_descriptor * best_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001069
Holger Schurige56188a2007-10-09 14:15:19 +02001070 lbs_deb_enter(LBS_DEB_SCAN);
1071
David Woodhouseaa21c002007-12-08 20:04:36 +00001072 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001073
David Woodhouseaa21c002007-12-08 20:04:36 +00001074 list_for_each_entry (iter_bss, &priv->network_list, list) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001075 switch (mode) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001076 case IW_MODE_INFRA:
1077 case IW_MODE_ADHOC:
David Woodhouseaa21c002007-12-08 20:04:36 +00001078 if (!is_network_compatible(priv, iter_bss, mode))
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001079 break;
1080 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1081 break;
1082 bestrssi = SCAN_RSSI(iter_bss->rssi);
1083 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001084 break;
Dan Williams0dc5a292007-05-10 22:58:02 -04001085 case IW_MODE_AUTO:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001086 default:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001087 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1088 break;
1089 bestrssi = SCAN_RSSI(iter_bss->rssi);
1090 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001091 break;
1092 }
1093 }
1094
David Woodhouseaa21c002007-12-08 20:04:36 +00001095 mutex_unlock(&priv->lock);
Holger Schurige56188a2007-10-09 14:15:19 +02001096 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001097 return best_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001098}
1099
1100/**
1101 * @brief Find the AP with specific ssid in the scan list
1102 *
Holger Schurige56188a2007-10-09 14:15:19 +02001103 * Used from association worker.
1104 *
Holger Schurig69f90322007-11-23 15:43:44 +01001105 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001106 * @param pSSID A pointer to AP's ssid
1107 *
1108 * @return 0--success, otherwise--fail
1109 */
Holger Schurig69f90322007-11-23 15:43:44 +01001110int lbs_find_best_network_ssid(struct lbs_private *priv,
Dan Williamsd8efea22007-05-28 23:54:55 -04001111 u8 *out_ssid, u8 *out_ssid_len, u8 preferred_mode, u8 *out_mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001112{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001113 int ret = -1;
1114 struct bss_descriptor * found;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001115
Holger Schurige56188a2007-10-09 14:15:19 +02001116 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001117
Holger Schurig10078322007-11-15 18:05:47 -05001118 lbs_scan_networks(priv, NULL, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +00001119 if (priv->surpriseremoved)
Holger Schurige56188a2007-10-09 14:15:19 +02001120 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001121
David Woodhouseaa21c002007-12-08 20:04:36 +00001122 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
Dan Williamsd8efea22007-05-28 23:54:55 -04001123 if (found && (found->ssid_len > 0)) {
1124 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1125 *out_ssid_len = found->ssid_len;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001126 *out_mode = found->mode;
1127 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001128 }
1129
Holger Schurige56188a2007-10-09 14:15:19 +02001130out:
Holger Schurig9012b282007-05-25 11:27:16 -04001131 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001132 return ret;
1133}
1134
Holger Schurige56188a2007-10-09 14:15:19 +02001135
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001136/**
1137 * @brief Send a scan command for all available channels filtered on a spec
1138 *
Holger Schurige56188a2007-10-09 14:15:19 +02001139 * Used in association code and from debugfs
1140 *
Holger Schurig69f90322007-11-23 15:43:44 +01001141 * @param priv A pointer to struct lbs_private structure
Holger Schurige56188a2007-10-09 14:15:19 +02001142 * @param ssid A pointer to the SSID to scan for
1143 * @param ssid_len Length of the SSID
1144 * @param clear_ssid Should existing scan results with this SSID
1145 * be cleared?
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001146 *
1147 * @return 0-success, otherwise fail
1148 */
Holger Schurig69f90322007-11-23 15:43:44 +01001149int lbs_send_specific_ssid_scan(struct lbs_private *priv,
Dan Williamsd8efea22007-05-28 23:54:55 -04001150 u8 *ssid, u8 ssid_len, u8 clear_ssid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001151{
Holger Schurig10078322007-11-15 18:05:47 -05001152 struct lbs_ioctl_user_scan_cfg scancfg;
Dan Williamseb8f7332007-05-25 16:25:21 -04001153 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001154
Holger Schurige56188a2007-10-09 14:15:19 +02001155 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s', clear %d",
1156 escape_essid(ssid, ssid_len), clear_ssid);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001157
Dan Williamsd8efea22007-05-28 23:54:55 -04001158 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -04001159 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001160
1161 memset(&scancfg, 0x00, sizeof(scancfg));
Dan Williamsd8efea22007-05-28 23:54:55 -04001162 memcpy(scancfg.ssid, ssid, ssid_len);
1163 scancfg.ssid_len = ssid_len;
Dan Williamseb8f7332007-05-25 16:25:21 -04001164 scancfg.clear_ssid = clear_ssid;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001165
Holger Schurig10078322007-11-15 18:05:47 -05001166 lbs_scan_networks(priv, &scancfg, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +00001167 if (priv->surpriseremoved) {
Holger Schurige56188a2007-10-09 14:15:19 +02001168 ret = -1;
1169 goto out;
1170 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001171
Dan Williamseb8f7332007-05-25 16:25:21 -04001172out:
Holger Schurige56188a2007-10-09 14:15:19 +02001173 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Dan Williamseb8f7332007-05-25 16:25:21 -04001174 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001175}
1176
Holger Schurige56188a2007-10-09 14:15:19 +02001177
1178
1179
1180/*********************************************************************/
1181/* */
1182/* Support for Wireless Extensions */
1183/* */
1184/*********************************************************************/
1185
Holger Schurigffd074f2007-12-07 16:52:10 +01001186
Dan Williams00af0152007-08-02 13:14:56 -04001187#define MAX_CUSTOM_LEN 64
1188
Holger Schurig69f90322007-11-23 15:43:44 +01001189static inline char *lbs_translate_scan(struct lbs_private *priv,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001190 char *start, char *stop,
1191 struct bss_descriptor *bss)
1192{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001193 struct chan_freq_power *cfp;
1194 char *current_val; /* For rates */
1195 struct iw_event iwe; /* Temporary buffer */
1196 int j;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001197#define PERFECT_RSSI ((u8)50)
1198#define WORST_RSSI ((u8)0)
1199#define RSSI_DIFF ((u8)(PERFECT_RSSI - WORST_RSSI))
1200 u8 rssi;
1201
Holger Schurige56188a2007-10-09 14:15:19 +02001202 lbs_deb_enter(LBS_DEB_SCAN);
1203
David Woodhouseaa21c002007-12-08 20:04:36 +00001204 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001205 if (!cfp) {
1206 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
Holger Schurige56188a2007-10-09 14:15:19 +02001207 start = NULL;
1208 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001209 }
1210
Holger Schurigffd074f2007-12-07 16:52:10 +01001211 /* First entry *MUST* be the BSSID */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001212 iwe.cmd = SIOCGIWAP;
1213 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1214 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
1215 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
1216
1217 /* SSID */
1218 iwe.cmd = SIOCGIWESSID;
1219 iwe.u.data.flags = 1;
Dan Williamsd8efea22007-05-28 23:54:55 -04001220 iwe.u.data.length = min((u32) bss->ssid_len, (u32) IW_ESSID_MAX_SIZE);
1221 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001222
1223 /* Mode */
1224 iwe.cmd = SIOCGIWMODE;
1225 iwe.u.mode = bss->mode;
1226 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
1227
1228 /* Frequency */
1229 iwe.cmd = SIOCGIWFREQ;
1230 iwe.u.freq.m = (long)cfp->freq * 100000;
1231 iwe.u.freq.e = 1;
1232 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
1233
1234 /* Add quality statistics */
1235 iwe.cmd = IWEVQUAL;
1236 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
1237 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
1238
1239 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
1240 iwe.u.qual.qual =
1241 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
1242 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
1243 (RSSI_DIFF * RSSI_DIFF);
1244 if (iwe.u.qual.qual > 100)
1245 iwe.u.qual.qual = 100;
1246
David Woodhouseaa21c002007-12-08 20:04:36 +00001247 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001248 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
1249 } else {
1250 iwe.u.qual.noise =
David Woodhouseaa21c002007-12-08 20:04:36 +00001251 CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001252 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001253
Dan Williams80e78ef2007-05-25 22:18:47 -04001254 /* Locally created ad-hoc BSSs won't have beacons if this is the
1255 * only station in the adhoc network; so get signal strength
1256 * from receive statistics.
1257 */
David Woodhouseaa21c002007-12-08 20:04:36 +00001258 if ((priv->mode == IW_MODE_ADHOC)
1259 && priv->adhoccreate
1260 && !lbs_ssid_cmp(priv->curbssparams.ssid,
1261 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001262 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -04001263 int snr, nf;
David Woodhouseaa21c002007-12-08 20:04:36 +00001264 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1265 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
Dan Williams80e78ef2007-05-25 22:18:47 -04001266 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001267 }
1268 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
1269
1270 /* Add encryption capability */
1271 iwe.cmd = SIOCGIWENCODE;
Dan Williams0c9ca692007-08-02 10:43:44 -04001272 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001273 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1274 } else {
1275 iwe.u.data.flags = IW_ENCODE_DISABLED;
1276 }
1277 iwe.u.data.length = 0;
Dan Williamsd8efea22007-05-28 23:54:55 -04001278 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001279
1280 current_val = start + IW_EV_LCP_LEN;
1281
1282 iwe.cmd = SIOCGIWRATE;
1283 iwe.u.bitrate.fixed = 0;
1284 iwe.u.bitrate.disabled = 0;
1285 iwe.u.bitrate.value = 0;
1286
Dan Williams8c512762007-08-02 11:40:45 -04001287 for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
1288 /* Bit rate given in 500 kb/s units */
1289 iwe.u.bitrate.value = bss->rates[j] * 500000;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001290 current_val = iwe_stream_add_value(start, current_val,
1291 stop, &iwe, IW_EV_PARAM_LEN);
1292 }
1293 if ((bss->mode == IW_MODE_ADHOC)
David Woodhouseaa21c002007-12-08 20:04:36 +00001294 && !lbs_ssid_cmp(priv->curbssparams.ssid,
1295 priv->curbssparams.ssid_len,
Dan Williamsd8efea22007-05-28 23:54:55 -04001296 bss->ssid, bss->ssid_len)
David Woodhouseaa21c002007-12-08 20:04:36 +00001297 && priv->adhoccreate) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001298 iwe.u.bitrate.value = 22 * 500000;
1299 current_val = iwe_stream_add_value(start, current_val,
1300 stop, &iwe, IW_EV_PARAM_LEN);
1301 }
1302 /* Check if we added any event */
1303 if((current_val - start) > IW_EV_LCP_LEN)
1304 start = current_val;
1305
1306 memset(&iwe, 0, sizeof(iwe));
1307 if (bss->wpa_ie_len) {
1308 char buf[MAX_WPA_IE_LEN];
1309 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
1310 iwe.cmd = IWEVGENIE;
1311 iwe.u.data.length = bss->wpa_ie_len;
1312 start = iwe_stream_add_point(start, stop, &iwe, buf);
1313 }
1314
1315 memset(&iwe, 0, sizeof(iwe));
1316 if (bss->rsn_ie_len) {
1317 char buf[MAX_WPA_IE_LEN];
1318 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
1319 iwe.cmd = IWEVGENIE;
1320 iwe.u.data.length = bss->rsn_ie_len;
1321 start = iwe_stream_add_point(start, stop, &iwe, buf);
1322 }
1323
Dan Williams00af0152007-08-02 13:14:56 -04001324 if (bss->mesh) {
1325 char custom[MAX_CUSTOM_LEN];
1326 char *p = custom;
1327
1328 iwe.cmd = IWEVCUSTOM;
1329 p += snprintf(p, MAX_CUSTOM_LEN - (p - custom),
1330 "mesh-type: olpc");
1331 iwe.u.data.length = p - custom;
1332 if (iwe.u.data.length)
1333 start = iwe_stream_add_point(start, stop, &iwe, custom);
1334 }
1335
Holger Schurige56188a2007-10-09 14:15:19 +02001336out:
1337 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001338 return start;
1339}
1340
Holger Schurigffd074f2007-12-07 16:52:10 +01001341
1342/**
1343 * @brief Handle Scan Network ioctl
1344 *
1345 * @param dev A pointer to net_device structure
1346 * @param info A pointer to iw_request_info structure
1347 * @param vwrq A pointer to iw_param structure
1348 * @param extra A pointer to extra data buf
1349 *
1350 * @return 0 --success, otherwise fail
1351 */
1352int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
1353 struct iw_param *wrqu, char *extra)
1354{
1355 struct lbs_private *priv = dev->priv;
Holger Schurigffd074f2007-12-07 16:52:10 +01001356
1357 lbs_deb_enter(LBS_DEB_SCAN);
1358
1359 if (!netif_running(dev))
1360 return -ENETDOWN;
1361
1362 /* mac80211 does this:
1363 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1364 if (sdata->type != IEEE80211_IF_TYPE_xxx)
1365 return -EOPNOTSUPP;
1366
1367 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
1368 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
1369 req = (struct iw_scan_req *)extra;
1370 ssid = req->essid;
1371 ssid_len = req->essid_len;
1372 }
1373 */
1374
1375 if (!delayed_work_pending(&priv->scan_work))
1376 queue_delayed_work(priv->work_thread, &priv->scan_work,
1377 msecs_to_jiffies(50));
1378 /* set marker that currently a scan is taking place */
David Woodhouseaa21c002007-12-08 20:04:36 +00001379 priv->last_scanned_channel = -1;
Holger Schurigffd074f2007-12-07 16:52:10 +01001380
David Woodhouseaa21c002007-12-08 20:04:36 +00001381 if (priv->surpriseremoved)
Holger Schurigffd074f2007-12-07 16:52:10 +01001382 return -EIO;
1383
1384 lbs_deb_leave(LBS_DEB_SCAN);
1385 return 0;
1386}
1387
1388
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001389/**
Holger Schurige56188a2007-10-09 14:15:19 +02001390 * @brief Handle Retrieve scan table ioctl
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001391 *
1392 * @param dev A pointer to net_device structure
1393 * @param info A pointer to iw_request_info structure
1394 * @param dwrq A pointer to iw_point structure
1395 * @param extra A pointer to extra data buf
1396 *
1397 * @return 0 --success, otherwise fail
1398 */
Holger Schurig10078322007-11-15 18:05:47 -05001399int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001400 struct iw_point *dwrq, char *extra)
1401{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001402#define SCAN_ITEM_SIZE 128
Holger Schurig69f90322007-11-23 15:43:44 +01001403 struct lbs_private *priv = dev->priv;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001404 int err = 0;
1405 char *ev = extra;
1406 char *stop = ev + dwrq->length;
1407 struct bss_descriptor * iter_bss;
1408 struct bss_descriptor * safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001409
Holger Schurige56188a2007-10-09 14:15:19 +02001410 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001411
Holger Schurigffd074f2007-12-07 16:52:10 +01001412 /* iwlist should wait until the current scan is finished */
David Woodhouseaa21c002007-12-08 20:04:36 +00001413 if (priv->last_scanned_channel)
Holger Schurigffd074f2007-12-07 16:52:10 +01001414 return -EAGAIN;
1415
Dan Williams80e78ef2007-05-25 22:18:47 -04001416 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
David Woodhouseaa21c002007-12-08 20:04:36 +00001417 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate) {
Holger Schurig10078322007-11-15 18:05:47 -05001418 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
Dan Williams0aef64d2007-08-02 11:31:18 -04001419 CMD_OPTION_WAITFORRSP, 0, NULL);
Dan Williams80e78ef2007-05-25 22:18:47 -04001420 }
1421
David Woodhouseaa21c002007-12-08 20:04:36 +00001422 mutex_lock(&priv->lock);
1423 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001424 char * next_ev;
1425 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001426
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001427 if (stop - ev < SCAN_ITEM_SIZE) {
1428 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001429 break;
1430 }
1431
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001432 /* For mesh device, list only mesh networks */
1433 if (dev == priv->mesh_dev && !iter_bss->mesh)
1434 continue;
1435
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001436 /* Prune old an old scan result */
1437 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1438 if (time_after(jiffies, stale_time)) {
1439 list_move_tail (&iter_bss->list,
David Woodhouseaa21c002007-12-08 20:04:36 +00001440 &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001441 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001442 continue;
1443 }
1444
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001445 /* Translate to WE format this entry */
Holger Schurig10078322007-11-15 18:05:47 -05001446 next_ev = lbs_translate_scan(priv, ev, stop, iter_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001447 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001448 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001449 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001450 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001451 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001452
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001453 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001454 dwrq->flags = 0;
1455
Holger Schurige56188a2007-10-09 14:15:19 +02001456 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", err);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001457 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001458}
1459
Holger Schurige56188a2007-10-09 14:15:19 +02001460
1461
1462
1463/*********************************************************************/
1464/* */
1465/* Command execution */
1466/* */
1467/*********************************************************************/
1468
1469
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001470/**
1471 * @brief Prepare a scan command to be sent to the firmware
1472 *
Holger Schurigffd074f2007-12-07 16:52:10 +01001473 * Called via lbs_prepare_and_send_command(priv, CMD_802_11_SCAN, ...)
1474 * from cmd.c
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001475 *
Holger Schurige56188a2007-10-09 14:15:19 +02001476 * Sends a fixed lenght data part (specifying the BSS type and BSSID filters)
1477 * as well as a variable number/length of TLVs to the firmware.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001478 *
Holger Schurig69f90322007-11-23 15:43:44 +01001479 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001480 * @param cmd A pointer to cmd_ds_command structure to be sent to
1481 * firmware with the cmd_DS_801_11_SCAN structure
Holger Schurig10078322007-11-15 18:05:47 -05001482 * @param pdata_buf Void pointer cast of a lbs_scan_cmd_config struct used
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001483 * to set the fields/TLVs for the command sent to firmware
1484 *
1485 * @return 0 or -1
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001486 */
Holger Schurig69f90322007-11-23 15:43:44 +01001487int lbs_cmd_80211_scan(struct lbs_private *priv,
Holger Schurigffd074f2007-12-07 16:52:10 +01001488 struct cmd_ds_command *cmd, void *pdata_buf)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001489{
1490 struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
Holger Schurig10078322007-11-15 18:05:47 -05001491 struct lbs_scan_cmd_config *pscancfg = pdata_buf;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001492
Holger Schurige56188a2007-10-09 14:15:19 +02001493 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001494
1495 /* Set fixed field variables in scan command */
1496 pscan->bsstype = pscancfg->bsstype;
Dan Williams492b6da2007-08-02 11:16:07 -04001497 memcpy(pscan->bssid, pscancfg->bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001498 memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
1499
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001500 /* size is equal to the sizeof(fixed portions) + the TLV len + header */
Dan Williams492b6da2007-08-02 11:16:07 -04001501 cmd->size = cpu_to_le16(sizeof(pscan->bsstype) + ETH_ALEN
1502 + pscancfg->tlvbufferlen + S_DS_GEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001503
Holger Schurige56188a2007-10-09 14:15:19 +02001504 lbs_deb_leave(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001505 return 0;
1506}
1507
1508/**
1509 * @brief This function handles the command response of scan
1510 *
Holger Schurige56188a2007-10-09 14:15:19 +02001511 * Called from handle_cmd_response() in cmdrespc.
1512 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001513 * The response buffer for the scan command has the following
1514 * memory layout:
1515 *
1516 * .-----------------------------------------------------------.
1517 * | header (4 * sizeof(u16)): Standard command response hdr |
1518 * .-----------------------------------------------------------.
1519 * | bufsize (u16) : sizeof the BSS Description data |
1520 * .-----------------------------------------------------------.
1521 * | NumOfSet (u8) : Number of BSS Descs returned |
1522 * .-----------------------------------------------------------.
1523 * | BSSDescription data (variable, size given in bufsize) |
1524 * .-----------------------------------------------------------.
1525 * | TLV data (variable, size calculated using header->size, |
1526 * | bufsize and sizeof the fixed fields above) |
1527 * .-----------------------------------------------------------.
1528 *
Holger Schurig69f90322007-11-23 15:43:44 +01001529 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001530 * @param resp A pointer to cmd_ds_command
1531 *
1532 * @return 0 or -1
1533 */
Holger Schurig69f90322007-11-23 15:43:44 +01001534int lbs_ret_80211_scan(struct lbs_private *priv, struct cmd_ds_command *resp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001535{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001536 struct cmd_ds_802_11_scan_rsp *pscan;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001537 struct bss_descriptor * iter_bss;
1538 struct bss_descriptor * safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001539 u8 *pbssinfo;
1540 u16 scanrespsize;
1541 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001542 int idx;
1543 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001544 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001545
Holger Schurige56188a2007-10-09 14:15:19 +02001546 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001547
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001548 /* Prune old entries from scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001549 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001550 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1551 if (time_before(jiffies, stale_time))
1552 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +00001553 list_move_tail (&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001554 clear_bss_descriptor(iter_bss);
1555 }
1556
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001557 pscan = &resp->params.scanresp;
1558
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001559 if (pscan->nr_sets > MAX_NETWORK_COUNT) {
1560 lbs_deb_scan(
1561 "SCAN_RESP: too many scan results (%d, max %d)!!\n",
1562 pscan->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001563 ret = -1;
1564 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001565 }
1566
David Woodhousee7240ac2007-12-11 17:54:36 -05001567 bytesleft = le16_to_cpu(pscan->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001568 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001569
David Woodhousee7240ac2007-12-11 17:54:36 -05001570 scanrespsize = le16_to_cpu(resp->size);
1571 lbs_deb_scan("SCAN_RESP: scan results %d\n", pscan->nr_sets);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001572
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001573 pbssinfo = pscan->bssdesc_and_tlvbuffer;
1574
1575 /* The size of the TLV buffer is equal to the entire command response
1576 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1577 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1578 * response header (S_DS_GEN)
1579 */
1580 tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
1581 + sizeof(pscan->nr_sets)
1582 + S_DS_GEN);
1583
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001584 /*
1585 * Process each scan response returned (pscan->nr_sets). Save
1586 * the information in the newbssentry and then insert into the
1587 * driver scan table either as an update to an existing entry
1588 * or as an addition at the end of the table
1589 */
1590 for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001591 struct bss_descriptor new;
1592 struct bss_descriptor * found = NULL;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001593 struct bss_descriptor * oldest = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07001594 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001595
1596 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001597 memset(&new, 0, sizeof (struct bss_descriptor));
Holger Schurig10078322007-11-15 18:05:47 -05001598 if (lbs_process_bss(&new, &pbssinfo, &bytesleft) != 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001599 /* error parsing the scan response, skipped */
1600 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1601 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001602 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001603
1604 /* Try to find this bss in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001605 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001606 if (is_same_network(iter_bss, &new)) {
1607 found = iter_bss;
1608 break;
1609 }
1610
1611 if ((oldest == NULL) ||
1612 (iter_bss->last_scanned < oldest->last_scanned))
1613 oldest = iter_bss;
1614 }
1615
1616 if (found) {
1617 /* found, clear it */
1618 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001619 } else if (!list_empty(&priv->network_free_list)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001620 /* Pull one from the free list */
David Woodhouseaa21c002007-12-08 20:04:36 +00001621 found = list_entry(priv->network_free_list.next,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001622 struct bss_descriptor, list);
David Woodhouseaa21c002007-12-08 20:04:36 +00001623 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001624 } else if (oldest) {
1625 /* If there are no more slots, expire the oldest */
1626 found = oldest;
1627 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001628 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001629 } else {
1630 continue;
1631 }
1632
Holger Schurigffd074f2007-12-07 16:52:10 +01001633 lbs_deb_scan("SCAN_RESP: BSSID %s\n",
Joe Perches0795af52007-10-03 17:59:30 -07001634 print_mac(mac, new.bssid));
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001635
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001636 /* Copy the locally created newbssentry to the scan table */
1637 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001638 }
1639
Holger Schurig9012b282007-05-25 11:27:16 -04001640 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001641
Holger Schurig9012b282007-05-25 11:27:16 -04001642done:
1643 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1644 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001645}