blob: 1c7003d7de43af39066a54e029063b09ede2b335 [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"
Holger Schurig697900a2008-04-02 16:27:10 +020022#include "assoc.h"
David Woodhousefa62f992008-03-03 12:18:03 +010023#include "cmd.h"
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020024
25//! Approximate amount of data needed to pass a scan result back to iwlist
26#define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
27 + IW_ESSID_MAX_SIZE \
28 + IW_EV_UINT_LEN \
29 + IW_EV_FREQ_LEN \
30 + IW_EV_QUAL_LEN \
31 + IW_ESSID_MAX_SIZE \
32 + IW_EV_PARAM_LEN \
33 + 40) /* 40 for WPAIE */
34
35//! Memory needed to store a max sized channel List TLV for a firmware scan
36#define CHAN_TLV_MAX_SIZE (sizeof(struct mrvlietypesheader) \
37 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
38 * sizeof(struct chanscanparamset)))
39
40//! Memory needed to store a max number/size SSID TLV for a firmware scan
41#define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvlietypes_ssidparamset))
42
David Woodhousefa62f992008-03-03 12:18:03 +010043//! Maximum memory needed for a cmd_ds_802_11_scan with all TLVs at max
44#define MAX_SCAN_CFG_ALLOC (sizeof(struct cmd_ds_802_11_scan) \
45 + CHAN_TLV_MAX_SIZE + SSID_TLV_MAX_SIZE)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -020046
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
David Woodhousefa62f992008-03-03 12:18:03 +010064static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
65 struct cmd_header *resp);
Holger Schurige56188a2007-10-09 14:15:19 +020066
67/*********************************************************************/
68/* */
69/* Misc helper functions */
70/* */
71/*********************************************************************/
72
Holger Schurig23ff5032008-01-28 17:27:03 +010073/**
74 * @brief Unsets the MSB on basic rates
75 *
76 * Scan through an array and unset the MSB for basic data rates.
77 *
78 * @param rates buffer of data rates
79 * @param len size of buffer
80 */
81static void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
82{
83 int i;
84
85 for (i = 0; i < len; i++)
86 rates[i] &= 0x7f;
87}
88
89
David Woodhousef137e052008-03-03 12:18:59 +010090static inline void clear_bss_descriptor(struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -040091{
92 /* Don't blow away ->list, just BSS data */
93 memset(bss, 0, offsetof(struct bss_descriptor, list));
94}
95
Holger Schurigffd074f2007-12-07 16:52:10 +010096/**
97 * @brief Compare two SSIDs
98 *
99 * @param ssid1 A pointer to ssid to compare
100 * @param ssid2 A pointer to ssid to compare
101 *
102 * @return 0: ssid is same, otherwise is different
103 */
David Woodhousef137e052008-03-03 12:18:59 +0100104int lbs_ssid_cmp(uint8_t *ssid1, uint8_t ssid1_len, uint8_t *ssid2,
105 uint8_t ssid2_len)
Holger Schurigffd074f2007-12-07 16:52:10 +0100106{
107 if (ssid1_len != ssid2_len)
108 return -1;
109
110 return memcmp(ssid1, ssid2, ssid1_len);
111}
112
Holger Schurig10078322007-11-15 18:05:47 -0500113static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
David Woodhousef137e052008-03-03 12:18:59 +0100114 struct bss_descriptor *match_bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400115{
David Woodhousef137e052008-03-03 12:18:59 +0100116 if (!secinfo->wep_enabled && !secinfo->WPAenabled
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400117 && !secinfo->WPA2enabled
Dan Williamsab617972007-08-02 10:48:02 -0400118 && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
119 && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
David Woodhousef137e052008-03-03 12:18:59 +0100120 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400121 return 1;
David Woodhousef137e052008-03-03 12:18:59 +0100122 else
123 return 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400124}
125
Holger Schurig10078322007-11-15 18:05:47 -0500126static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
David Woodhousef137e052008-03-03 12:18:59 +0100127 struct bss_descriptor *match_bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400128{
David Woodhousef137e052008-03-03 12:18:59 +0100129 if (secinfo->wep_enabled && !secinfo->WPAenabled
130 && !secinfo->WPA2enabled
131 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400132 return 1;
David Woodhousef137e052008-03-03 12:18:59 +0100133 else
134 return 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400135}
136
Holger Schurig10078322007-11-15 18:05:47 -0500137static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
David Woodhousef137e052008-03-03 12:18:59 +0100138 struct bss_descriptor *match_bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400139{
David Woodhousef137e052008-03-03 12:18:59 +0100140 if (!secinfo->wep_enabled && secinfo->WPAenabled
141 && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
142 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
143 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
144 )
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400145 return 1;
David Woodhousef137e052008-03-03 12:18:59 +0100146 else
147 return 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400148}
149
Holger Schurig10078322007-11-15 18:05:47 -0500150static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
David Woodhousef137e052008-03-03 12:18:59 +0100151 struct bss_descriptor *match_bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400152{
David Woodhousef137e052008-03-03 12:18:59 +0100153 if (!secinfo->wep_enabled && secinfo->WPA2enabled
154 && (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
155 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
156 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
157 )
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400158 return 1;
David Woodhousef137e052008-03-03 12:18:59 +0100159 else
160 return 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400161}
162
Holger Schurig10078322007-11-15 18:05:47 -0500163static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
David Woodhousef137e052008-03-03 12:18:59 +0100164 struct bss_descriptor *match_bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400165{
David Woodhousef137e052008-03-03 12:18:59 +0100166 if (!secinfo->wep_enabled && !secinfo->WPAenabled
167 && !secinfo->WPA2enabled
168 && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
169 && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
170 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400171 return 1;
David Woodhousef137e052008-03-03 12:18:59 +0100172 else
173 return 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400174}
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200175
Holger Schurigffd074f2007-12-07 16:52:10 +0100176static inline int is_same_network(struct bss_descriptor *src,
177 struct bss_descriptor *dst)
178{
179 /* A network is only a duplicate if the channel, BSSID, and ESSID
180 * all match. We treat all <hidden> with the same BSSID and channel
181 * as one network */
182 return ((src->ssid_len == dst->ssid_len) &&
183 (src->channel == dst->channel) &&
184 !compare_ether_addr(src->bssid, dst->bssid) &&
185 !memcmp(src->ssid, dst->ssid, src->ssid_len));
186}
187
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200188/**
189 * @brief Check if a scanned network compatible with the driver settings
190 *
191 * WEP WPA WPA2 ad-hoc encrypt Network
192 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
193 * 0 0 0 0 NONE 0 0 0 yes No security
194 * 1 0 0 0 NONE 1 0 0 yes Static WEP
195 * 0 1 0 0 x 1x 1 x yes WPA
196 * 0 0 1 0 x 1x x 1 yes WPA2
197 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
198 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
199 *
200 *
David Woodhouseaa21c002007-12-08 20:04:36 +0000201 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200202 * @param index Index in scantable to check against current driver settings
203 * @param mode Network mode: Infrastructure or IBSS
204 *
205 * @return Index in scantable, or error code if negative
206 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000207static int is_network_compatible(struct lbs_private *priv,
David Woodhousef137e052008-03-03 12:18:59 +0100208 struct bss_descriptor *bss, uint8_t mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200209{
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400210 int matched = 0;
211
Holger Schurige56188a2007-10-09 14:15:19 +0200212 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200213
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400214 if (bss->mode != mode)
215 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200216
David Woodhouseaa21c002007-12-08 20:04:36 +0000217 if ((matched = match_bss_no_security(&priv->secinfo, bss))) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400218 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000219 } else if ((matched = match_bss_static_wep(&priv->secinfo, bss))) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400220 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000221 } else if ((matched = match_bss_wpa(&priv->secinfo, bss))) {
David Woodhousef137e052008-03-03 12:18:59 +0100222 lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
223 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
224 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
225 priv->secinfo.wep_enabled ? "e" : "d",
226 priv->secinfo.WPAenabled ? "e" : "d",
227 priv->secinfo.WPA2enabled ? "e" : "d",
228 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400229 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000230 } else if ((matched = match_bss_wpa2(&priv->secinfo, bss))) {
David Woodhousef137e052008-03-03 12:18:59 +0100231 lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
232 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
233 "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
234 priv->secinfo.wep_enabled ? "e" : "d",
235 priv->secinfo.WPAenabled ? "e" : "d",
236 priv->secinfo.WPA2enabled ? "e" : "d",
237 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400238 goto done;
David Woodhouseaa21c002007-12-08 20:04:36 +0000239 } else if ((matched = match_bss_dynamic_wep(&priv->secinfo, bss))) {
David Woodhousef137e052008-03-03 12:18:59 +0100240 lbs_deb_scan("is_network_compatible() dynamic WEP: "
241 "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
242 bss->wpa_ie[0], bss->rsn_ie[0],
243 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Holger Schurig9012b282007-05-25 11:27:16 -0400244 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200245 }
246
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400247 /* bss security settings don't match those configured on card */
David Woodhousef137e052008-03-03 12:18:59 +0100248 lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
249 "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
250 bss->wpa_ie[0], bss->rsn_ie[0],
251 priv->secinfo.wep_enabled ? "e" : "d",
252 priv->secinfo.WPAenabled ? "e" : "d",
253 priv->secinfo.WPA2enabled ? "e" : "d",
254 (bss->capability & WLAN_CAPABILITY_PRIVACY));
Holger Schurig9012b282007-05-25 11:27:16 -0400255
256done:
Holger Schurige56188a2007-10-09 14:15:19 +0200257 lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400258 return matched;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200259}
260
Holger Schurige56188a2007-10-09 14:15:19 +0200261
262
Holger Schurige56188a2007-10-09 14:15:19 +0200263/*********************************************************************/
264/* */
265/* Main scanning support */
266/* */
267/*********************************************************************/
268
Holger Schurige56188a2007-10-09 14:15:19 +0200269/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200270 * @brief Create a channel list for the driver to scan based on region info
271 *
Holger Schurig10078322007-11-15 18:05:47 -0500272 * Only used from lbs_scan_setup_scan_config()
Holger Schurige56188a2007-10-09 14:15:19 +0200273 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200274 * Use the driver region/band information to construct a comprehensive list
275 * of channels to scan. This routine is used for any scan that is not
276 * provided a specific channel list to scan.
277 *
Holger Schurig69f90322007-11-23 15:43:44 +0100278 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200279 * @param scanchanlist Output parameter: resulting channel list to scan
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200280 *
281 * @return void
282 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100283static int lbs_scan_create_channel_list(struct lbs_private *priv,
Holger Schurig52933d82008-03-05 07:05:32 +0100284 struct chanscanparamset *scanchanlist)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200285{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200286 struct region_channel *scanregion;
287 struct chan_freq_power *cfp;
288 int rgnidx;
289 int chanidx;
290 int nextchan;
David Woodhousef137e052008-03-03 12:18:59 +0100291 uint8_t scantype;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200292
293 chanidx = 0;
294
295 /* Set the default scan type to the user specified type, will later
296 * be changed to passive on a per channel basis if restricted by
297 * regulatory requirements (11d or 11h)
298 */
Holger Schurig4f2fdaa2007-08-02 13:12:27 -0400299 scantype = CMD_SCAN_TYPE_ACTIVE;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200300
David Woodhouseaa21c002007-12-08 20:04:36 +0000301 for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
David Woodhousef137e052008-03-03 12:18:59 +0100302 if (priv->enable11d && (priv->connect_status != LBS_CONNECTED)
303 && (priv->mesh_connect_status != LBS_CONNECTED)) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200304 /* Scan all the supported chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000305 if (!priv->universal_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200306 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000307 scanregion = &priv->universal_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200308
309 /* clear the parsed_region_chan for the first scan */
David Woodhouseaa21c002007-12-08 20:04:36 +0000310 memset(&priv->parsed_region_chan, 0x00,
311 sizeof(priv->parsed_region_chan));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200312 } else {
David Woodhouseaa21c002007-12-08 20:04:36 +0000313 if (!priv->region_channel[rgnidx].valid)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200314 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +0000315 scanregion = &priv->region_channel[rgnidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200316 }
317
David Woodhousef137e052008-03-03 12:18:59 +0100318 for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
319 struct chanscanparamset *chan = &scanchanlist[chanidx];
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200320
321 cfp = scanregion->CFP + nextchan;
322
David Woodhousef137e052008-03-03 12:18:59 +0100323 if (priv->enable11d)
324 scantype = lbs_get_scan_type_11d(cfp->channel,
325 &priv->parsed_region_chan);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200326
David Woodhousef137e052008-03-03 12:18:59 +0100327 if (scanregion->band == BAND_B || scanregion->band == BAND_G)
328 chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200329
Dan Williams0aef64d2007-08-02 11:31:18 -0400330 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
David Woodhousef137e052008-03-03 12:18:59 +0100331 chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
332 chan->chanscanmode.passivescan = 1;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200333 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100334 chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
335 chan->chanscanmode.passivescan = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200336 }
337
David Woodhousef137e052008-03-03 12:18:59 +0100338 chan->channumber = cfp->channel;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200339 }
340 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100341 return chanidx;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200342}
343
Dan Williams2afc0c52007-08-02 13:19:04 -0400344
Holger Schurige56188a2007-10-09 14:15:19 +0200345/*
Holger Schurigffd074f2007-12-07 16:52:10 +0100346 * Add SSID TLV of the form:
347 *
348 * TLV-ID SSID 00 00
349 * length 06 00
350 * ssid 4d 4e 54 45 53 54
351 */
Holger Schurig52933d82008-03-05 07:05:32 +0100352static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv)
Dan Williamseb8f7332007-05-25 16:25:21 -0400353{
David Woodhousef137e052008-03-03 12:18:59 +0100354 struct mrvlietypes_ssidparamset *ssid_tlv = (void *)tlv;
355
Holger Schurigffd074f2007-12-07 16:52:10 +0100356 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
Holger Schurig52933d82008-03-05 07:05:32 +0100357 ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len);
358 memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len);
359 return sizeof(ssid_tlv->header) + priv->scan_ssid_len;
Holger Schurigffd074f2007-12-07 16:52:10 +0100360}
Dan Williamseb8f7332007-05-25 16:25:21 -0400361
Holger Schurige56188a2007-10-09 14:15:19 +0200362
Holger Schurigffd074f2007-12-07 16:52:10 +0100363/*
364 * Add CHANLIST TLV of the form
365 *
366 * TLV-ID CHANLIST 01 01
367 * length 5b 00
368 * channel 1 00 01 00 00 00 64 00
369 * radio type 00
370 * channel 01
371 * scan type 00
372 * min scan time 00 00
373 * max scan time 64 00
374 * channel 2 00 02 00 00 00 64 00
375 * channel 3 00 03 00 00 00 64 00
376 * channel 4 00 04 00 00 00 64 00
377 * channel 5 00 05 00 00 00 64 00
378 * channel 6 00 06 00 00 00 64 00
379 * channel 7 00 07 00 00 00 64 00
380 * channel 8 00 08 00 00 00 64 00
381 * channel 9 00 09 00 00 00 64 00
382 * channel 10 00 0a 00 00 00 64 00
383 * channel 11 00 0b 00 00 00 64 00
384 * channel 12 00 0c 00 00 00 64 00
385 * channel 13 00 0d 00 00 00 64 00
386 *
387 */
David Woodhousef137e052008-03-03 12:18:59 +0100388static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
389 struct chanscanparamset *chan_list,
390 int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100391{
David Woodhousef137e052008-03-03 12:18:59 +0100392 size_t size = sizeof(struct chanscanparamset) *chan_count;
393 struct mrvlietypes_chanlistparamset *chan_tlv = (void *)tlv;
Dan Williamseb8f7332007-05-25 16:25:21 -0400394
Holger Schurigffd074f2007-12-07 16:52:10 +0100395 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
396 memcpy(chan_tlv->chanscanparam, chan_list, size);
397 chan_tlv->header.len = cpu_to_le16(size);
398 return sizeof(chan_tlv->header) + size;
399}
Dan Williamseb8f7332007-05-25 16:25:21 -0400400
Holger Schurigffd074f2007-12-07 16:52:10 +0100401
402/*
403 * Add RATES TLV of the form
404 *
405 * TLV-ID RATES 01 00
406 * length 0e 00
407 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
408 *
409 * The rates are in lbs_bg_rates[], but for the 802.11b
410 * rates the high bit isn't set.
411 */
David Woodhousef137e052008-03-03 12:18:59 +0100412static int lbs_scan_add_rates_tlv(uint8_t *tlv)
Holger Schurigffd074f2007-12-07 16:52:10 +0100413{
414 int i;
David Woodhousef137e052008-03-03 12:18:59 +0100415 struct mrvlietypes_ratesparamset *rate_tlv = (void *)tlv;
Holger Schurigffd074f2007-12-07 16:52:10 +0100416
417 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
418 tlv += sizeof(rate_tlv->header);
419 for (i = 0; i < MAX_RATES; i++) {
420 *tlv = lbs_bg_rates[i];
421 if (*tlv == 0)
422 break;
423 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
424 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
425 Note that the values are MBit/s * 2, to mark them as
426 basic rates so that the firmware likes it better */
427 if (*tlv == 0x02 || *tlv == 0x04 ||
428 *tlv == 0x0b || *tlv == 0x16)
429 *tlv |= 0x80;
430 tlv++;
Dan Williamseb8f7332007-05-25 16:25:21 -0400431 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100432 rate_tlv->header.len = cpu_to_le16(i);
433 return sizeof(rate_tlv->header) + i;
434}
Dan Williamseb8f7332007-05-25 16:25:21 -0400435
Holger Schurigffd074f2007-12-07 16:52:10 +0100436
437/*
438 * Generate the CMD_802_11_SCAN command with the proper tlv
439 * for a bunch of channels.
440 */
David Woodhousefa62f992008-03-03 12:18:03 +0100441static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
Holger Schurig52933d82008-03-05 07:05:32 +0100442 struct chanscanparamset *chan_list, int chan_count)
Holger Schurigffd074f2007-12-07 16:52:10 +0100443{
444 int ret = -ENOMEM;
David Woodhousefa62f992008-03-03 12:18:03 +0100445 struct cmd_ds_802_11_scan *scan_cmd;
446 uint8_t *tlv; /* pointer into our current, growing TLV storage area */
Holger Schurigffd074f2007-12-07 16:52:10 +0100447
David Woodhousefa62f992008-03-03 12:18:03 +0100448 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
449 bsstype, chan_list[0].channumber, chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100450
451 /* create the fixed part for scan command */
452 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
453 if (scan_cmd == NULL)
Holger Schurige56188a2007-10-09 14:15:19 +0200454 goto out;
David Woodhousefa62f992008-03-03 12:18:03 +0100455
Holger Schurigffd074f2007-12-07 16:52:10 +0100456 tlv = scan_cmd->tlvbuffer;
Holger Schurig52933d82008-03-05 07:05:32 +0100457 /* TODO: do we need to scan for a specific BSSID?
458 memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
Holger Schurigffd074f2007-12-07 16:52:10 +0100459 scan_cmd->bsstype = bsstype;
Dan Williamseb8f7332007-05-25 16:25:21 -0400460
Holger Schurigffd074f2007-12-07 16:52:10 +0100461 /* add TLVs */
Holger Schurig52933d82008-03-05 07:05:32 +0100462 if (priv->scan_ssid_len)
463 tlv += lbs_scan_add_ssid_tlv(priv, tlv);
Holger Schurigffd074f2007-12-07 16:52:10 +0100464 if (chan_list && chan_count)
465 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
466 tlv += lbs_scan_add_rates_tlv(tlv);
Dan Williamseb8f7332007-05-25 16:25:21 -0400467
Holger Schurigffd074f2007-12-07 16:52:10 +0100468 /* This is the final data we are about to send */
David Woodhousefa62f992008-03-03 12:18:03 +0100469 scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
470 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
471 sizeof(*scan_cmd));
Holger Schurigffd074f2007-12-07 16:52:10 +0100472 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
David Woodhousefa62f992008-03-03 12:18:03 +0100473 tlv - scan_cmd->tlvbuffer);
Dan Williamseb8f7332007-05-25 16:25:21 -0400474
David Woodhousefa62f992008-03-03 12:18:03 +0100475 ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
476 le16_to_cpu(scan_cmd->hdr.size),
477 lbs_ret_80211_scan, 0);
478
Holger Schurige56188a2007-10-09 14:15:19 +0200479out:
Holger Schurigffd074f2007-12-07 16:52:10 +0100480 kfree(scan_cmd);
481 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
482 return ret;
Dan Williamseb8f7332007-05-25 16:25:21 -0400483}
484
485
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200486/**
487 * @brief Internal function used to start a scan based on an input config
488 *
Holger Schurige56188a2007-10-09 14:15:19 +0200489 * Also used from debugfs
490 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200491 * Use the input user scan configuration information when provided in
492 * order to send the appropriate scan commands to firmware to populate or
493 * update the internal driver scan table
494 *
Holger Schurig69f90322007-11-23 15:43:44 +0100495 * @param priv A pointer to struct lbs_private structure
Holger Schurig52933d82008-03-05 07:05:32 +0100496 * @param full_scan Do a full-scan (blocking)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200497 *
498 * @return 0 or < 0 if error
499 */
Holger Schurig52933d82008-03-05 07:05:32 +0100500static int lbs_scan_networks(struct lbs_private *priv, int full_scan)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200501{
Holger Schurigffd074f2007-12-07 16:52:10 +0100502 int ret = -ENOMEM;
503 struct chanscanparamset *chan_list;
504 struct chanscanparamset *curr_chans;
505 int chan_count;
David Woodhousef137e052008-03-03 12:18:59 +0100506 uint8_t bsstype = CMD_BSS_TYPE_ANY;
Holger Schurigffd074f2007-12-07 16:52:10 +0100507 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
Holger Schurigffd074f2007-12-07 16:52:10 +0100508 union iwreq_data wrqu;
Dan Williamsf8f55102007-05-30 10:12:55 -0400509#ifdef CONFIG_LIBERTAS_DEBUG
Holger Schurigffd074f2007-12-07 16:52:10 +0100510 struct bss_descriptor *iter;
Dan Williamsf8f55102007-05-30 10:12:55 -0400511 int i = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700512 DECLARE_MAC_BUF(mac);
Dan Williamsf8f55102007-05-30 10:12:55 -0400513#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200514
David Woodhousef137e052008-03-03 12:18:59 +0100515 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
Dan Williams2afc0c52007-08-02 13:19:04 -0400516
517 /* Cancel any partial outstanding partial scans if this scan
518 * is a full scan.
519 */
520 if (full_scan && delayed_work_pending(&priv->scan_work))
521 cancel_delayed_work(&priv->scan_work);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200522
Holger Schurig52933d82008-03-05 07:05:32 +0100523 /* User-specified bsstype or channel list
524 TODO: this can be implemented if some user-space application
525 need the feature. Formerly, it was accessible from debugfs,
526 but then nowhere used.
Holger Schurigffd074f2007-12-07 16:52:10 +0100527 if (user_cfg) {
528 if (user_cfg->bsstype)
Holger Schurig52933d82008-03-05 07:05:32 +0100529 bsstype = user_cfg->bsstype;
530 } */
531
532 lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200533
Holger Schurigffd074f2007-12-07 16:52:10 +0100534 /* Create list of channels to scan */
535 chan_list = kzalloc(sizeof(struct chanscanparamset) *
David Woodhousef137e052008-03-03 12:18:59 +0100536 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
Holger Schurigffd074f2007-12-07 16:52:10 +0100537 if (!chan_list) {
538 lbs_pr_alert("SCAN: chan_list empty\n");
539 goto out;
540 }
541
542 /* We want to scan all channels */
Holger Schurig52933d82008-03-05 07:05:32 +0100543 chan_count = lbs_scan_create_channel_list(priv, chan_list);
Holger Schurigffd074f2007-12-07 16:52:10 +0100544
545 netif_stop_queue(priv->dev);
546 netif_carrier_off(priv->dev);
547 if (priv->mesh_dev) {
David Woodhousea27b9f92007-12-12 00:41:51 -0500548 netif_stop_queue(priv->mesh_dev);
549 netif_carrier_off(priv->mesh_dev);
Holger Schurigffd074f2007-12-07 16:52:10 +0100550 }
551
552 /* Prepare to continue an interrupted scan */
Holger Schurig8816edc2008-01-28 17:28:05 +0100553 lbs_deb_scan("chan_count %d, scan_channel %d\n",
554 chan_count, priv->scan_channel);
Holger Schurigffd074f2007-12-07 16:52:10 +0100555 curr_chans = chan_list;
556 /* advance channel list by already-scanned-channels */
Holger Schurig8816edc2008-01-28 17:28:05 +0100557 if (priv->scan_channel > 0) {
558 curr_chans += priv->scan_channel;
559 chan_count -= priv->scan_channel;
Holger Schurigffd074f2007-12-07 16:52:10 +0100560 }
561
562 /* Send scan command(s)
563 * numchannels contains the number of channels we should maximally scan
564 * chan_count is the total number of channels to scan
565 */
566
567 while (chan_count) {
568 int to_scan = min(numchannels, chan_count);
569 lbs_deb_scan("scanning %d of %d channels\n",
David Woodhousef137e052008-03-03 12:18:59 +0100570 to_scan, chan_count);
Holger Schurigffd074f2007-12-07 16:52:10 +0100571 ret = lbs_do_scan(priv, bsstype, curr_chans,
Holger Schurig52933d82008-03-05 07:05:32 +0100572 to_scan);
Holger Schurigffd074f2007-12-07 16:52:10 +0100573 if (ret) {
574 lbs_pr_err("SCAN_CMD failed\n");
575 goto out2;
576 }
577 curr_chans += to_scan;
578 chan_count -= to_scan;
579
580 /* somehow schedule the next part of the scan */
David Woodhousef137e052008-03-03 12:18:59 +0100581 if (chan_count && !full_scan &&
David Woodhouseaa21c002007-12-08 20:04:36 +0000582 !priv->surpriseremoved) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100583 /* -1 marks just that we're currently scanning */
Holger Schurig8816edc2008-01-28 17:28:05 +0100584 if (priv->scan_channel < 0)
585 priv->scan_channel = to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100586 else
Holger Schurig8816edc2008-01-28 17:28:05 +0100587 priv->scan_channel += to_scan;
Holger Schurigffd074f2007-12-07 16:52:10 +0100588 cancel_delayed_work(&priv->scan_work);
589 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +0100590 msecs_to_jiffies(300));
Holger Schurigffd074f2007-12-07 16:52:10 +0100591 /* skip over GIWSCAN event */
592 goto out;
593 }
594
595 }
596 memset(&wrqu, 0, sizeof(union iwreq_data));
597 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200598
Dan Williamsf8f55102007-05-30 10:12:55 -0400599#ifdef CONFIG_LIBERTAS_DEBUG
600 /* Dump the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +0000601 mutex_lock(&priv->lock);
Holger Schurigffd074f2007-12-07 16:52:10 +0100602 lbs_deb_scan("scan table:\n");
David Woodhouseaa21c002007-12-08 20:04:36 +0000603 list_for_each_entry(iter, &priv->network_list, list)
Holger Schurigffd074f2007-12-07 16:52:10 +0100604 lbs_deb_scan("%02d: BSSID %s, RSSI %d, SSID '%s'\n",
Holger Schurigff829ae2008-03-19 17:08:32 +0100605 i++, print_mac(mac, iter->bssid), iter->rssi,
David Woodhousef137e052008-03-03 12:18:59 +0100606 escape_essid(iter->ssid, iter->ssid_len));
David Woodhouseaa21c002007-12-08 20:04:36 +0000607 mutex_unlock(&priv->lock);
Dan Williamsf8f55102007-05-30 10:12:55 -0400608#endif
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200609
Holger Schurigffd074f2007-12-07 16:52:10 +0100610out2:
Holger Schurig8816edc2008-01-28 17:28:05 +0100611 priv->scan_channel = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +0100612
613out:
David Woodhouseaa21c002007-12-08 20:04:36 +0000614 if (priv->connect_status == LBS_CONNECTED) {
Holger Schurig634b8f42007-05-25 13:05:16 -0400615 netif_carrier_on(priv->dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500616 if (!priv->tx_pending_len)
617 netif_wake_queue(priv->dev);
Brajesh Dave01d77d82007-11-20 17:44:14 -0500618 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000619 if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
Brajesh Dave01d77d82007-11-20 17:44:14 -0500620 netif_carrier_on(priv->mesh_dev);
David Woodhousea27b9f92007-12-12 00:41:51 -0500621 if (!priv->tx_pending_len)
622 netif_wake_queue(priv->mesh_dev);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200623 }
Holger Schurigffd074f2007-12-07 16:52:10 +0100624 kfree(chan_list);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200625
Holger Schurig9012b282007-05-25 11:27:16 -0400626 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200627 return ret;
628}
629
Holger Schurigffd074f2007-12-07 16:52:10 +0100630
631
632
Holger Schurig52933d82008-03-05 07:05:32 +0100633void lbs_scan_worker(struct work_struct *work)
634{
635 struct lbs_private *priv =
636 container_of(work, struct lbs_private, scan_work.work);
637
638 lbs_deb_enter(LBS_DEB_SCAN);
639 lbs_scan_networks(priv, 0);
640 lbs_deb_leave(LBS_DEB_SCAN);
641}
642
643
Holger Schurigffd074f2007-12-07 16:52:10 +0100644/*********************************************************************/
645/* */
646/* Result interpretation */
647/* */
648/*********************************************************************/
649
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200650/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200651 * @brief Interpret a BSS scan response returned from the firmware
652 *
653 * Parse the various fixed fields and IEs passed back for a a BSS probe
Holger Schurigffd074f2007-12-07 16:52:10 +0100654 * response or beacon from the scan command. Record information as needed
655 * in the scan table struct bss_descriptor for that entry.
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200656 *
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400657 * @param bss Output parameter: Pointer to the BSS Entry
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200658 *
659 * @return 0 or -1
660 */
Holger Schurig10078322007-11-15 18:05:47 -0500661static int lbs_process_bss(struct bss_descriptor *bss,
David Woodhousef137e052008-03-03 12:18:59 +0100662 uint8_t **pbeaconinfo, int *bytesleft)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200663{
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200664 struct ieeetypes_fhparamset *pFH;
665 struct ieeetypes_dsparamset *pDS;
666 struct ieeetypes_cfparamset *pCF;
667 struct ieeetypes_ibssparamset *pibss;
Joe Perches0795af52007-10-03 17:59:30 -0700668 DECLARE_MAC_BUF(mac);
Dan Williams8c512762007-08-02 11:40:45 -0400669 struct ieeetypes_countryinfoset *pcountryinfo;
David Woodhousef137e052008-03-03 12:18:59 +0100670 uint8_t *pos, *end, *p;
671 uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
672 uint16_t beaconsize = 0;
Holger Schurig9012b282007-05-25 11:27:16 -0400673 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200674
Holger Schurige56188a2007-10-09 14:15:19 +0200675 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200676
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200677 if (*bytesleft >= sizeof(beaconsize)) {
678 /* Extract & convert beacon size from the command buffer */
David Woodhousee7240ac2007-12-11 17:54:36 -0500679 beaconsize = le16_to_cpu(get_unaligned((__le16 *)*pbeaconinfo));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200680 *bytesleft -= sizeof(beaconsize);
681 *pbeaconinfo += sizeof(beaconsize);
682 }
683
684 if (beaconsize == 0 || beaconsize > *bytesleft) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200685 *pbeaconinfo += *bytesleft;
686 *bytesleft = 0;
Holger Schurige56188a2007-10-09 14:15:19 +0200687 ret = -1;
688 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200689 }
690
691 /* Initialize the current working beacon pointer for this BSS iteration */
Dan Williamsab617972007-08-02 10:48:02 -0400692 pos = *pbeaconinfo;
693 end = pos + beaconsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200694
695 /* Advance the return beacon pointer past the current beacon */
696 *pbeaconinfo += beaconsize;
697 *bytesleft -= beaconsize;
698
Dan Williamsab617972007-08-02 10:48:02 -0400699 memcpy(bss->bssid, pos, ETH_ALEN);
Holger Schurigffd074f2007-12-07 16:52:10 +0100700 lbs_deb_scan("process_bss: BSSID %s\n", print_mac(mac, bss->bssid));
Dan Williamsab617972007-08-02 10:48:02 -0400701 pos += ETH_ALEN;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200702
Dan Williamsab617972007-08-02 10:48:02 -0400703 if ((end - pos) < 12) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400704 lbs_deb_scan("process_bss: Not enough bytes left\n");
Holger Schurige56188a2007-10-09 14:15:19 +0200705 ret = -1;
706 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200707 }
708
709 /*
710 * next 4 fields are RSSI, time stamp, beacon interval,
711 * and capability information
712 */
713
714 /* RSSI is 1 byte long */
Dan Williamsab617972007-08-02 10:48:02 -0400715 bss->rssi = *pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100716 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
Dan Williamsab617972007-08-02 10:48:02 -0400717 pos++;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200718
719 /* time stamp is 8 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400720 pos += 8;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200721
722 /* beacon interval is 2 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400723 bss->beaconperiod = le16_to_cpup((void *) pos);
724 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200725
726 /* capability information is 2 bytes long */
Dan Williamsab617972007-08-02 10:48:02 -0400727 bss->capability = le16_to_cpup((void *) pos);
Holger Schurigffd074f2007-12-07 16:52:10 +0100728 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
Dan Williamsab617972007-08-02 10:48:02 -0400729 pos += 2;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200730
Dan Williams0c9ca6902007-08-02 10:43:44 -0400731 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
Holger Schurigffd074f2007-12-07 16:52:10 +0100732 lbs_deb_scan("process_bss: WEP enabled\n");
Dan Williams0c9ca6902007-08-02 10:43:44 -0400733 if (bss->capability & WLAN_CAPABILITY_IBSS)
734 bss->mode = IW_MODE_ADHOC;
735 else
736 bss->mode = IW_MODE_INFRA;
737
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200738 /* rest of the current buffer are IE's */
Holger Schurigffd074f2007-12-07 16:52:10 +0100739 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
Holger Schurigece56192007-08-02 11:53:06 -0400740 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200741
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200742 /* process variable IE */
Dan Williamsab617972007-08-02 10:48:02 -0400743 while (pos <= end - 2) {
David Woodhousef137e052008-03-03 12:18:59 +0100744 struct ieee80211_info_element * elem = (void *)pos;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200745
Dan Williamsab617972007-08-02 10:48:02 -0400746 if (pos + elem->len > end) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400747 lbs_deb_scan("process_bss: error in processing IE, "
David Woodhousef137e052008-03-03 12:18:59 +0100748 "bytes left < IE length\n");
Dan Williamsab617972007-08-02 10:48:02 -0400749 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200750 }
751
Dan Williamsab617972007-08-02 10:48:02 -0400752 switch (elem->id) {
753 case MFIE_TYPE_SSID:
754 bss->ssid_len = elem->len;
755 memcpy(bss->ssid, elem->data, elem->len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100756 lbs_deb_scan("got SSID IE: '%s', len %u\n",
Dan Williamsd8efea22007-05-28 23:54:55 -0400757 escape_essid(bss->ssid, bss->ssid_len),
758 bss->ssid_len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200759 break;
760
Dan Williamsab617972007-08-02 10:48:02 -0400761 case MFIE_TYPE_RATES:
David Woodhousef137e052008-03-03 12:18:59 +0100762 n_basic_rates = min_t(uint8_t, MAX_RATES, elem->len);
Dan Williams8c512762007-08-02 11:40:45 -0400763 memcpy(bss->rates, elem->data, n_basic_rates);
764 got_basic_rates = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100765 lbs_deb_scan("got RATES IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200766 break;
767
Dan Williamsab617972007-08-02 10:48:02 -0400768 case MFIE_TYPE_FH_SET:
769 pFH = (struct ieeetypes_fhparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400770 memmove(&bss->phyparamset.fhparamset, pFH,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200771 sizeof(struct ieeetypes_fhparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100772 lbs_deb_scan("got FH IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200773 break;
774
Dan Williamsab617972007-08-02 10:48:02 -0400775 case MFIE_TYPE_DS_SET:
776 pDS = (struct ieeetypes_dsparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400777 bss->channel = pDS->currentchan;
778 memcpy(&bss->phyparamset.dsparamset, pDS,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200779 sizeof(struct ieeetypes_dsparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100780 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200781 break;
782
Dan Williamsab617972007-08-02 10:48:02 -0400783 case MFIE_TYPE_CF_SET:
784 pCF = (struct ieeetypes_cfparamset *) pos;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400785 memcpy(&bss->ssparamset.cfparamset, pCF,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200786 sizeof(struct ieeetypes_cfparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100787 lbs_deb_scan("got CF IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200788 break;
789
Dan Williamsab617972007-08-02 10:48:02 -0400790 case MFIE_TYPE_IBSS_SET:
791 pibss = (struct ieeetypes_ibssparamset *) pos;
David Woodhousee7240ac2007-12-11 17:54:36 -0500792 bss->atimwindow = le16_to_cpu(pibss->atimwindow);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400793 memmove(&bss->ssparamset.ibssparamset, pibss,
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200794 sizeof(struct ieeetypes_ibssparamset));
Holger Schurigffd074f2007-12-07 16:52:10 +0100795 lbs_deb_scan("got IBSS IE\n");
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200796 break;
797
Dan Williamsab617972007-08-02 10:48:02 -0400798 case MFIE_TYPE_COUNTRY:
799 pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
Holger Schurigffd074f2007-12-07 16:52:10 +0100800 lbs_deb_scan("got COUNTRY IE\n");
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400801 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200802 || pcountryinfo->len > 254) {
David Woodhousef137e052008-03-03 12:18:59 +0100803 lbs_deb_scan("process_bss: 11D- Err CountryInfo len %d, min %zd, max 254\n",
804 pcountryinfo->len, sizeof(pcountryinfo->countrycode));
Holger Schurig9012b282007-05-25 11:27:16 -0400805 ret = -1;
806 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200807 }
808
David Woodhousef137e052008-03-03 12:18:59 +0100809 memcpy(&bss->countryinfo, pcountryinfo, pcountryinfo->len + 2);
Holger Schurigece56192007-08-02 11:53:06 -0400810 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
David Woodhousef137e052008-03-03 12:18:59 +0100811 (uint8_t *) pcountryinfo,
812 (int) (pcountryinfo->len + 2));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200813 break;
814
Dan Williamsab617972007-08-02 10:48:02 -0400815 case MFIE_TYPE_RATES_EX:
816 /* only process extended supported rate if data rate is
817 * already found. Data rate IE should come before
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200818 * extended supported rate IE
819 */
Holger Schurigffd074f2007-12-07 16:52:10 +0100820 lbs_deb_scan("got RATESEX IE\n");
821 if (!got_basic_rates) {
822 lbs_deb_scan("... but ignoring it\n");
Dan Williamsab617972007-08-02 10:48:02 -0400823 break;
Holger Schurigffd074f2007-12-07 16:52:10 +0100824 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200825
Dan Williams8c512762007-08-02 11:40:45 -0400826 n_ex_rates = elem->len;
827 if (n_basic_rates + n_ex_rates > MAX_RATES)
828 n_ex_rates = MAX_RATES - n_basic_rates;
Dan Williamsab617972007-08-02 10:48:02 -0400829
Dan Williams8c512762007-08-02 11:40:45 -0400830 p = bss->rates + n_basic_rates;
831 memcpy(p, elem->data, n_ex_rates);
Dan Williamsab617972007-08-02 10:48:02 -0400832 break;
833
834 case MFIE_TYPE_GENERIC:
835 if (elem->len >= 4 &&
David Woodhousef137e052008-03-03 12:18:59 +0100836 elem->data[0] == 0x00 && elem->data[1] == 0x50 &&
837 elem->data[2] == 0xf2 && elem->data[3] == 0x01) {
838 bss->wpa_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
Dan Williamsab617972007-08-02 10:48:02 -0400839 memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100840 lbs_deb_scan("got WPA IE\n");
David Woodhousef137e052008-03-03 12:18:59 +0100841 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie, elem->len);
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400842 } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
David Woodhousef137e052008-03-03 12:18:59 +0100843 elem->data[0] == 0x00 && elem->data[1] == 0x50 &&
844 elem->data[2] == 0x43 && elem->data[3] == 0x04) {
Holger Schurigffd074f2007-12-07 16:52:10 +0100845 lbs_deb_scan("got mesh IE\n");
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -0400846 bss->mesh = 1;
Holger Schurigffd074f2007-12-07 16:52:10 +0100847 } else {
David Woodhousef137e052008-03-03 12:18:59 +0100848 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
Holger Schurigffd074f2007-12-07 16:52:10 +0100849 elem->data[0], elem->data[1],
850 elem->data[2], elem->data[3],
851 elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200852 }
853 break;
854
Dan Williamsab617972007-08-02 10:48:02 -0400855 case MFIE_TYPE_RSN:
Holger Schurigffd074f2007-12-07 16:52:10 +0100856 lbs_deb_scan("got RSN IE\n");
Dan Williamsab617972007-08-02 10:48:02 -0400857 bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
858 memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
Holger Schurigffd074f2007-12-07 16:52:10 +0100859 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
David Woodhousef137e052008-03-03 12:18:59 +0100860 bss->rsn_ie, elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200861 break;
862
Dan Williamsab617972007-08-02 10:48:02 -0400863 default:
Holger Schurigffd074f2007-12-07 16:52:10 +0100864 lbs_deb_scan("got IE 0x%04x, len %d\n",
David Woodhousef137e052008-03-03 12:18:59 +0100865 elem->id, elem->len);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200866 break;
867 }
868
Dan Williamsab617972007-08-02 10:48:02 -0400869 pos += elem->len + 2;
870 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400871
872 /* Timestamp */
873 bss->last_scanned = jiffies;
Holger Schurig10078322007-11-15 18:05:47 -0500874 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400875
Holger Schurig9012b282007-05-25 11:27:16 -0400876 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200877
Holger Schurig9012b282007-05-25 11:27:16 -0400878done:
879 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
880 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200881}
882
883/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200884 * @brief This function finds a specific compatible BSSID in the scan list
885 *
Holger Schurige56188a2007-10-09 14:15:19 +0200886 * Used in association code
887 *
David Woodhouseaa21c002007-12-08 20:04:36 +0000888 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200889 * @param bssid BSSID to find in the scan list
890 * @param mode Network mode: Infrastructure or IBSS
891 *
892 * @return index in BSSID list, or error return code (< 0)
893 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000894struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
David Woodhousef137e052008-03-03 12:18:59 +0100895 uint8_t *bssid, uint8_t mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200896{
David Woodhousef137e052008-03-03 12:18:59 +0100897 struct bss_descriptor *iter_bss;
898 struct bss_descriptor *found_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200899
Holger Schurige56188a2007-10-09 14:15:19 +0200900 lbs_deb_enter(LBS_DEB_SCAN);
901
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200902 if (!bssid)
Holger Schurige56188a2007-10-09 14:15:19 +0200903 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200904
David Woodhousef137e052008-03-03 12:18:59 +0100905 lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200906
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400907 /* Look through the scan table for a compatible match. The loop will
908 * continue past a matched bssid that is not compatible in case there
909 * is an AP with multiple SSIDs assigned to the same BSSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200910 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000911 mutex_lock(&priv->lock);
912 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williams3cf209312007-05-25 17:28:30 -0400913 if (compare_ether_addr(iter_bss->bssid, bssid))
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400914 continue; /* bssid doesn't match */
915 switch (mode) {
916 case IW_MODE_INFRA:
917 case IW_MODE_ADHOC:
David Woodhouseaa21c002007-12-08 20:04:36 +0000918 if (!is_network_compatible(priv, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200919 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400920 found_bss = iter_bss;
921 break;
922 default:
923 found_bss = iter_bss;
924 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200925 }
926 }
David Woodhouseaa21c002007-12-08 20:04:36 +0000927 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200928
Holger Schurige56188a2007-10-09 14:15:19 +0200929out:
930 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400931 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200932}
933
934/**
935 * @brief This function finds ssid in ssid list.
936 *
Holger Schurige56188a2007-10-09 14:15:19 +0200937 * Used in association code
938 *
David Woodhouseaa21c002007-12-08 20:04:36 +0000939 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200940 * @param ssid SSID to find in the list
941 * @param bssid BSSID to qualify the SSID selection (if provided)
942 * @param mode Network mode: Infrastructure or IBSS
943 *
944 * @return index in BSSID list
945 */
David Woodhouseaa21c002007-12-08 20:04:36 +0000946struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
David Woodhousef137e052008-03-03 12:18:59 +0100947 uint8_t *ssid, uint8_t ssid_len,
948 uint8_t *bssid, uint8_t mode,
949 int channel)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200950{
Holger Schurigff829ae2008-03-19 17:08:32 +0100951 u32 bestrssi = 0;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400952 struct bss_descriptor * iter_bss = NULL;
953 struct bss_descriptor * found_bss = NULL;
954 struct bss_descriptor * tmp_oldest = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200955
Holger Schurige56188a2007-10-09 14:15:19 +0200956 lbs_deb_enter(LBS_DEB_SCAN);
957
David Woodhouseaa21c002007-12-08 20:04:36 +0000958 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200959
David Woodhouseaa21c002007-12-08 20:04:36 +0000960 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400961 if ( !tmp_oldest
962 || (iter_bss->last_scanned < tmp_oldest->last_scanned))
963 tmp_oldest = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200964
Holger Schurig10078322007-11-15 18:05:47 -0500965 if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
David Woodhousef137e052008-03-03 12:18:59 +0100966 ssid, ssid_len) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400967 continue; /* ssid doesn't match */
Dan Williams3cf209312007-05-25 17:28:30 -0400968 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400969 continue; /* bssid doesn't match */
Dan Williamsaeea0ab2007-05-25 22:30:48 -0400970 if ((channel > 0) && (iter_bss->channel != channel))
971 continue; /* channel doesn't match */
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200972
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400973 switch (mode) {
974 case IW_MODE_INFRA:
975 case IW_MODE_ADHOC:
David Woodhouseaa21c002007-12-08 20:04:36 +0000976 if (!is_network_compatible(priv, iter_bss, mode))
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200977 break;
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400978
979 if (bssid) {
980 /* Found requested BSSID */
981 found_bss = iter_bss;
982 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200983 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -0400984
985 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
986 bestrssi = SCAN_RSSI(iter_bss->rssi);
987 found_bss = iter_bss;
988 }
989 break;
990 case IW_MODE_AUTO:
991 default:
992 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
993 bestrssi = SCAN_RSSI(iter_bss->rssi);
994 found_bss = iter_bss;
995 }
996 break;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -0200997 }
998 }
999
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001000out:
David Woodhouseaa21c002007-12-08 20:04:36 +00001001 mutex_unlock(&priv->lock);
Holger Schurige56188a2007-10-09 14:15:19 +02001002 lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001003 return found_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001004}
1005
1006/**
1007 * @brief This function finds the best SSID in the Scan List
1008 *
1009 * Search the scan table for the best SSID that also matches the current
1010 * adapter network preference (infrastructure or adhoc)
1011 *
David Woodhouseaa21c002007-12-08 20:04:36 +00001012 * @param priv A pointer to struct lbs_private
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001013 *
1014 * @return index in BSSID list
1015 */
David Woodhousef137e052008-03-03 12:18:59 +01001016static struct bss_descriptor *lbs_find_best_ssid_in_list(struct lbs_private *priv,
1017 uint8_t mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001018{
David Woodhousef137e052008-03-03 12:18:59 +01001019 uint8_t bestrssi = 0;
1020 struct bss_descriptor *iter_bss;
1021 struct bss_descriptor *best_bss = NULL;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001022
Holger Schurige56188a2007-10-09 14:15:19 +02001023 lbs_deb_enter(LBS_DEB_SCAN);
1024
David Woodhouseaa21c002007-12-08 20:04:36 +00001025 mutex_lock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001026
David Woodhouseaa21c002007-12-08 20:04:36 +00001027 list_for_each_entry (iter_bss, &priv->network_list, list) {
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001028 switch (mode) {
Dan Williams0dc5a292007-05-10 22:58:02 -04001029 case IW_MODE_INFRA:
1030 case IW_MODE_ADHOC:
David Woodhouseaa21c002007-12-08 20:04:36 +00001031 if (!is_network_compatible(priv, iter_bss, mode))
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001032 break;
1033 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1034 break;
1035 bestrssi = SCAN_RSSI(iter_bss->rssi);
1036 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001037 break;
Dan Williams0dc5a292007-05-10 22:58:02 -04001038 case IW_MODE_AUTO:
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001039 default:
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001040 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1041 break;
1042 bestrssi = SCAN_RSSI(iter_bss->rssi);
1043 best_bss = iter_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001044 break;
1045 }
1046 }
1047
David Woodhouseaa21c002007-12-08 20:04:36 +00001048 mutex_unlock(&priv->lock);
Holger Schurige56188a2007-10-09 14:15:19 +02001049 lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001050 return best_bss;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001051}
1052
1053/**
Holger Schurig52933d82008-03-05 07:05:32 +01001054 * @brief Find the best AP
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001055 *
Holger Schurige56188a2007-10-09 14:15:19 +02001056 * Used from association worker.
1057 *
Holger Schurig69f90322007-11-23 15:43:44 +01001058 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001059 * @param pSSID A pointer to AP's ssid
1060 *
1061 * @return 0--success, otherwise--fail
1062 */
David Woodhousef137e052008-03-03 12:18:59 +01001063int lbs_find_best_network_ssid(struct lbs_private *priv, uint8_t *out_ssid,
1064 uint8_t *out_ssid_len, uint8_t preferred_mode,
1065 uint8_t *out_mode)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001066{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001067 int ret = -1;
David Woodhousef137e052008-03-03 12:18:59 +01001068 struct bss_descriptor *found;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001069
Holger Schurige56188a2007-10-09 14:15:19 +02001070 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001071
Holger Schurig52933d82008-03-05 07:05:32 +01001072 priv->scan_ssid_len = 0;
1073 lbs_scan_networks(priv, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +00001074 if (priv->surpriseremoved)
Holger Schurige56188a2007-10-09 14:15:19 +02001075 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001076
David Woodhouseaa21c002007-12-08 20:04:36 +00001077 found = lbs_find_best_ssid_in_list(priv, preferred_mode);
Dan Williamsd8efea22007-05-28 23:54:55 -04001078 if (found && (found->ssid_len > 0)) {
1079 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1080 *out_ssid_len = found->ssid_len;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001081 *out_mode = found->mode;
1082 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001083 }
1084
Holger Schurige56188a2007-10-09 14:15:19 +02001085out:
Holger Schurig9012b282007-05-25 11:27:16 -04001086 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001087 return ret;
1088}
1089
Holger Schurige56188a2007-10-09 14:15:19 +02001090
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001091/**
1092 * @brief Send a scan command for all available channels filtered on a spec
1093 *
Holger Schurige56188a2007-10-09 14:15:19 +02001094 * Used in association code and from debugfs
1095 *
Holger Schurig69f90322007-11-23 15:43:44 +01001096 * @param priv A pointer to struct lbs_private structure
Holger Schurige56188a2007-10-09 14:15:19 +02001097 * @param ssid A pointer to the SSID to scan for
1098 * @param ssid_len Length of the SSID
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001099 *
1100 * @return 0-success, otherwise fail
1101 */
David Woodhousef137e052008-03-03 12:18:59 +01001102int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
Holger Schurig52933d82008-03-05 07:05:32 +01001103 uint8_t ssid_len)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001104{
Dan Williamseb8f7332007-05-25 16:25:21 -04001105 int ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001106
Holger Schurig52933d82008-03-05 07:05:32 +01001107 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
1108 escape_essid(ssid, ssid_len));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001109
Dan Williamsd8efea22007-05-28 23:54:55 -04001110 if (!ssid_len)
Dan Williamseb8f7332007-05-25 16:25:21 -04001111 goto out;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001112
Holger Schurig52933d82008-03-05 07:05:32 +01001113 memcpy(priv->scan_ssid, ssid, ssid_len);
1114 priv->scan_ssid_len = ssid_len;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001115
Holger Schurig52933d82008-03-05 07:05:32 +01001116 lbs_scan_networks(priv, 1);
David Woodhouseaa21c002007-12-08 20:04:36 +00001117 if (priv->surpriseremoved) {
Holger Schurige56188a2007-10-09 14:15:19 +02001118 ret = -1;
1119 goto out;
1120 }
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001121
Dan Williamseb8f7332007-05-25 16:25:21 -04001122out:
Holger Schurige56188a2007-10-09 14:15:19 +02001123 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
Dan Williamseb8f7332007-05-25 16:25:21 -04001124 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001125}
1126
Holger Schurige56188a2007-10-09 14:15:19 +02001127
1128
1129
1130/*********************************************************************/
1131/* */
1132/* Support for Wireless Extensions */
1133/* */
1134/*********************************************************************/
1135
Holger Schurigffd074f2007-12-07 16:52:10 +01001136
Dan Williams00af0152007-08-02 13:14:56 -04001137#define MAX_CUSTOM_LEN 64
1138
Holger Schurig69f90322007-11-23 15:43:44 +01001139static inline char *lbs_translate_scan(struct lbs_private *priv,
David Woodhousef137e052008-03-03 12:18:59 +01001140 char *start, char *stop,
1141 struct bss_descriptor *bss)
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001142{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001143 struct chan_freq_power *cfp;
1144 char *current_val; /* For rates */
1145 struct iw_event iwe; /* Temporary buffer */
1146 int j;
David Woodhousef137e052008-03-03 12:18:59 +01001147#define PERFECT_RSSI ((uint8_t)50)
1148#define WORST_RSSI ((uint8_t)0)
1149#define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
1150 uint8_t rssi;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001151
Holger Schurige56188a2007-10-09 14:15:19 +02001152 lbs_deb_enter(LBS_DEB_SCAN);
1153
David Woodhouseaa21c002007-12-08 20:04:36 +00001154 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001155 if (!cfp) {
1156 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
Holger Schurige56188a2007-10-09 14:15:19 +02001157 start = NULL;
1158 goto out;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001159 }
1160
Holger Schurigffd074f2007-12-07 16:52:10 +01001161 /* First entry *MUST* be the BSSID */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001162 iwe.cmd = SIOCGIWAP;
1163 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1164 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
1165 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
1166
1167 /* SSID */
1168 iwe.cmd = SIOCGIWESSID;
1169 iwe.u.data.flags = 1;
David Woodhousef137e052008-03-03 12:18:59 +01001170 iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IW_ESSID_MAX_SIZE);
Dan Williamsd8efea22007-05-28 23:54:55 -04001171 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001172
1173 /* Mode */
1174 iwe.cmd = SIOCGIWMODE;
1175 iwe.u.mode = bss->mode;
1176 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
1177
1178 /* Frequency */
1179 iwe.cmd = SIOCGIWFREQ;
1180 iwe.u.freq.m = (long)cfp->freq * 100000;
1181 iwe.u.freq.e = 1;
1182 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
1183
1184 /* Add quality statistics */
1185 iwe.cmd = IWEVQUAL;
1186 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
1187 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
1188
1189 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
1190 iwe.u.qual.qual =
David Woodhousef137e052008-03-03 12:18:59 +01001191 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
1192 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
1193 (RSSI_DIFF * RSSI_DIFF);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001194 if (iwe.u.qual.qual > 100)
1195 iwe.u.qual.qual = 100;
1196
David Woodhouseaa21c002007-12-08 20:04:36 +00001197 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001198 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
1199 } else {
David Woodhousef137e052008-03-03 12:18:59 +01001200 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001201 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001202
Dan Williams80e78ef2007-05-25 22:18:47 -04001203 /* Locally created ad-hoc BSSs won't have beacons if this is the
1204 * only station in the adhoc network; so get signal strength
1205 * from receive statistics.
1206 */
David Woodhousef137e052008-03-03 12:18:59 +01001207 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +00001208 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +01001209 priv->curbssparams.ssid_len,
1210 bss->ssid, bss->ssid_len)) {
Dan Williams80e78ef2007-05-25 22:18:47 -04001211 int snr, nf;
David Woodhouseaa21c002007-12-08 20:04:36 +00001212 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1213 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
Dan Williams80e78ef2007-05-25 22:18:47 -04001214 iwe.u.qual.level = CAL_RSSI(snr, nf);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001215 }
1216 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
1217
1218 /* Add encryption capability */
1219 iwe.cmd = SIOCGIWENCODE;
Dan Williams0c9ca6902007-08-02 10:43:44 -04001220 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001221 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1222 } else {
1223 iwe.u.data.flags = IW_ENCODE_DISABLED;
1224 }
1225 iwe.u.data.length = 0;
Dan Williamsd8efea22007-05-28 23:54:55 -04001226 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001227
1228 current_val = start + IW_EV_LCP_LEN;
1229
1230 iwe.cmd = SIOCGIWRATE;
1231 iwe.u.bitrate.fixed = 0;
1232 iwe.u.bitrate.disabled = 0;
1233 iwe.u.bitrate.value = 0;
1234
Dan Williams8c512762007-08-02 11:40:45 -04001235 for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
1236 /* Bit rate given in 500 kb/s units */
1237 iwe.u.bitrate.value = bss->rates[j] * 500000;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001238 current_val = iwe_stream_add_value(start, current_val,
1239 stop, &iwe, IW_EV_PARAM_LEN);
1240 }
David Woodhousef137e052008-03-03 12:18:59 +01001241 if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
David Woodhouseaa21c002007-12-08 20:04:36 +00001242 && !lbs_ssid_cmp(priv->curbssparams.ssid,
David Woodhousef137e052008-03-03 12:18:59 +01001243 priv->curbssparams.ssid_len,
1244 bss->ssid, bss->ssid_len)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001245 iwe.u.bitrate.value = 22 * 500000;
1246 current_val = iwe_stream_add_value(start, current_val,
David Woodhousef137e052008-03-03 12:18:59 +01001247 stop, &iwe, IW_EV_PARAM_LEN);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001248 }
1249 /* Check if we added any event */
1250 if((current_val - start) > IW_EV_LCP_LEN)
1251 start = current_val;
1252
1253 memset(&iwe, 0, sizeof(iwe));
1254 if (bss->wpa_ie_len) {
1255 char buf[MAX_WPA_IE_LEN];
1256 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
1257 iwe.cmd = IWEVGENIE;
1258 iwe.u.data.length = bss->wpa_ie_len;
1259 start = iwe_stream_add_point(start, stop, &iwe, buf);
1260 }
1261
1262 memset(&iwe, 0, sizeof(iwe));
1263 if (bss->rsn_ie_len) {
1264 char buf[MAX_WPA_IE_LEN];
1265 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
1266 iwe.cmd = IWEVGENIE;
1267 iwe.u.data.length = bss->rsn_ie_len;
1268 start = iwe_stream_add_point(start, stop, &iwe, buf);
1269 }
1270
Dan Williams00af0152007-08-02 13:14:56 -04001271 if (bss->mesh) {
1272 char custom[MAX_CUSTOM_LEN];
1273 char *p = custom;
1274
1275 iwe.cmd = IWEVCUSTOM;
David Woodhousef137e052008-03-03 12:18:59 +01001276 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
Dan Williams00af0152007-08-02 13:14:56 -04001277 iwe.u.data.length = p - custom;
1278 if (iwe.u.data.length)
1279 start = iwe_stream_add_point(start, stop, &iwe, custom);
1280 }
1281
Holger Schurige56188a2007-10-09 14:15:19 +02001282out:
1283 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001284 return start;
1285}
1286
Holger Schurigffd074f2007-12-07 16:52:10 +01001287
1288/**
1289 * @brief Handle Scan Network ioctl
1290 *
1291 * @param dev A pointer to net_device structure
1292 * @param info A pointer to iw_request_info structure
1293 * @param vwrq A pointer to iw_param structure
1294 * @param extra A pointer to extra data buf
1295 *
1296 * @return 0 --success, otherwise fail
1297 */
1298int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
Holger Schurig52933d82008-03-05 07:05:32 +01001299 union iwreq_data *wrqu, char *extra)
Holger Schurigffd074f2007-12-07 16:52:10 +01001300{
1301 struct lbs_private *priv = dev->priv;
Holger Schurig52933d82008-03-05 07:05:32 +01001302 int ret = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +01001303
Holger Schurig52933d82008-03-05 07:05:32 +01001304 lbs_deb_enter(LBS_DEB_WEXT);
Holger Schurigffd074f2007-12-07 16:52:10 +01001305
Holger Schurig52933d82008-03-05 07:05:32 +01001306 if (!netif_running(dev)) {
1307 ret = -ENETDOWN;
1308 goto out;
1309 }
Holger Schurigffd074f2007-12-07 16:52:10 +01001310
1311 /* mac80211 does this:
1312 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Holger Schurig52933d82008-03-05 07:05:32 +01001313 if (sdata->type != IEEE80211_IF_TYPE_xxx) {
1314 ret = -EOPNOTSUPP;
1315 goto out;
1316 }
1317 */
Holger Schurigffd074f2007-12-07 16:52:10 +01001318
1319 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
1320 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
Holger Schurig52933d82008-03-05 07:05:32 +01001321 struct iw_scan_req *req = (struct iw_scan_req *)extra;
1322 priv->scan_ssid_len = req->essid_len;
1323 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
1324 lbs_deb_wext("set_scan, essid '%s'\n",
1325 escape_essid(priv->scan_ssid, priv->scan_ssid_len));
1326 } else {
1327 priv->scan_ssid_len = 0;
Holger Schurigffd074f2007-12-07 16:52:10 +01001328 }
Holger Schurigffd074f2007-12-07 16:52:10 +01001329
1330 if (!delayed_work_pending(&priv->scan_work))
1331 queue_delayed_work(priv->work_thread, &priv->scan_work,
David Woodhousef137e052008-03-03 12:18:59 +01001332 msecs_to_jiffies(50));
Holger Schurigffd074f2007-12-07 16:52:10 +01001333 /* set marker that currently a scan is taking place */
Holger Schurig8816edc2008-01-28 17:28:05 +01001334 priv->scan_channel = -1;
Holger Schurigffd074f2007-12-07 16:52:10 +01001335
David Woodhouseaa21c002007-12-08 20:04:36 +00001336 if (priv->surpriseremoved)
Holger Schurig52933d82008-03-05 07:05:32 +01001337 ret = -EIO;
Holger Schurigffd074f2007-12-07 16:52:10 +01001338
Holger Schurig52933d82008-03-05 07:05:32 +01001339out:
1340 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
1341 return ret;
Holger Schurigffd074f2007-12-07 16:52:10 +01001342}
1343
1344
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001345/**
Holger Schurige56188a2007-10-09 14:15:19 +02001346 * @brief Handle Retrieve scan table ioctl
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001347 *
1348 * @param dev A pointer to net_device structure
1349 * @param info A pointer to iw_request_info structure
1350 * @param dwrq A pointer to iw_point structure
1351 * @param extra A pointer to extra data buf
1352 *
1353 * @return 0 --success, otherwise fail
1354 */
Holger Schurig10078322007-11-15 18:05:47 -05001355int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
David Woodhousef137e052008-03-03 12:18:59 +01001356 struct iw_point *dwrq, char *extra)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001357{
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001358#define SCAN_ITEM_SIZE 128
Holger Schurig69f90322007-11-23 15:43:44 +01001359 struct lbs_private *priv = dev->priv;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001360 int err = 0;
1361 char *ev = extra;
1362 char *stop = ev + dwrq->length;
David Woodhousef137e052008-03-03 12:18:59 +01001363 struct bss_descriptor *iter_bss;
1364 struct bss_descriptor *safe;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001365
Holger Schurig52933d82008-03-05 07:05:32 +01001366 lbs_deb_enter(LBS_DEB_WEXT);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001367
Holger Schurigffd074f2007-12-07 16:52:10 +01001368 /* iwlist should wait until the current scan is finished */
Holger Schurig8816edc2008-01-28 17:28:05 +01001369 if (priv->scan_channel)
Holger Schurigffd074f2007-12-07 16:52:10 +01001370 return -EAGAIN;
1371
Dan Williams80e78ef2007-05-25 22:18:47 -04001372 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
David Woodhousef137e052008-03-03 12:18:59 +01001373 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate)
Holger Schurig10078322007-11-15 18:05:47 -05001374 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
David Woodhousef137e052008-03-03 12:18:59 +01001375 CMD_OPTION_WAITFORRSP, 0, NULL);
Dan Williams80e78ef2007-05-25 22:18:47 -04001376
David Woodhouseaa21c002007-12-08 20:04:36 +00001377 mutex_lock(&priv->lock);
1378 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
David Woodhousef137e052008-03-03 12:18:59 +01001379 char *next_ev;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001380 unsigned long stale_time;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001381
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001382 if (stop - ev < SCAN_ITEM_SIZE) {
1383 err = -E2BIG;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001384 break;
1385 }
1386
Luis Carlos Cobo1e838bf2007-08-02 10:51:27 -04001387 /* For mesh device, list only mesh networks */
1388 if (dev == priv->mesh_dev && !iter_bss->mesh)
1389 continue;
1390
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001391 /* Prune old an old scan result */
1392 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1393 if (time_after(jiffies, stale_time)) {
David Woodhousef137e052008-03-03 12:18:59 +01001394 list_move_tail(&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001395 clear_bss_descriptor(iter_bss);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001396 continue;
1397 }
1398
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001399 /* Translate to WE format this entry */
Holger Schurig10078322007-11-15 18:05:47 -05001400 next_ev = lbs_translate_scan(priv, ev, stop, iter_bss);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001401 if (next_ev == NULL)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001402 continue;
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001403 ev = next_ev;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001404 }
David Woodhouseaa21c002007-12-08 20:04:36 +00001405 mutex_unlock(&priv->lock);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001406
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001407 dwrq->length = (ev - extra);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001408 dwrq->flags = 0;
1409
Holger Schurig52933d82008-03-05 07:05:32 +01001410 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001411 return err;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001412}
1413
Holger Schurige56188a2007-10-09 14:15:19 +02001414
1415
1416
1417/*********************************************************************/
1418/* */
1419/* Command execution */
1420/* */
1421/*********************************************************************/
1422
1423
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001424/**
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001425 * @brief This function handles the command response of scan
1426 *
Holger Schurige56188a2007-10-09 14:15:19 +02001427 * Called from handle_cmd_response() in cmdrespc.
1428 *
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001429 * The response buffer for the scan command has the following
1430 * memory layout:
1431 *
1432 * .-----------------------------------------------------------.
1433 * | header (4 * sizeof(u16)): Standard command response hdr |
1434 * .-----------------------------------------------------------.
1435 * | bufsize (u16) : sizeof the BSS Description data |
1436 * .-----------------------------------------------------------.
1437 * | NumOfSet (u8) : Number of BSS Descs returned |
1438 * .-----------------------------------------------------------.
1439 * | BSSDescription data (variable, size given in bufsize) |
1440 * .-----------------------------------------------------------.
1441 * | TLV data (variable, size calculated using header->size, |
1442 * | bufsize and sizeof the fixed fields above) |
1443 * .-----------------------------------------------------------.
1444 *
Holger Schurig69f90322007-11-23 15:43:44 +01001445 * @param priv A pointer to struct lbs_private structure
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001446 * @param resp A pointer to cmd_ds_command
1447 *
1448 * @return 0 or -1
1449 */
David Woodhousefa62f992008-03-03 12:18:03 +01001450static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1451 struct cmd_header *resp)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001452{
David Woodhousefa62f992008-03-03 12:18:03 +01001453 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
David Woodhousef137e052008-03-03 12:18:59 +01001454 struct bss_descriptor *iter_bss;
1455 struct bss_descriptor *safe;
David Woodhousefa62f992008-03-03 12:18:03 +01001456 uint8_t *bssinfo;
1457 uint16_t scanrespsize;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001458 int bytesleft;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001459 int idx;
1460 int tlvbufsize;
Holger Schurig9012b282007-05-25 11:27:16 -04001461 int ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001462
Holger Schurige56188a2007-10-09 14:15:19 +02001463 lbs_deb_enter(LBS_DEB_SCAN);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001464
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001465 /* Prune old entries from scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001466 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001467 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1468 if (time_before(jiffies, stale_time))
1469 continue;
David Woodhouseaa21c002007-12-08 20:04:36 +00001470 list_move_tail (&iter_bss->list, &priv->network_free_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001471 clear_bss_descriptor(iter_bss);
1472 }
1473
David Woodhousefa62f992008-03-03 12:18:03 +01001474 if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1475 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1476 scanresp->nr_sets, MAX_NETWORK_COUNT);
Holger Schurig9012b282007-05-25 11:27:16 -04001477 ret = -1;
1478 goto done;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001479 }
1480
David Woodhousefa62f992008-03-03 12:18:03 +01001481 bytesleft = le16_to_cpu(scanresp->bssdescriptsize);
Holger Schurig9012b282007-05-25 11:27:16 -04001482 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001483
David Woodhousee7240ac2007-12-11 17:54:36 -05001484 scanrespsize = le16_to_cpu(resp->size);
David Woodhousefa62f992008-03-03 12:18:03 +01001485 lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001486
David Woodhousefa62f992008-03-03 12:18:03 +01001487 bssinfo = scanresp->bssdesc_and_tlvbuffer;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001488
1489 /* The size of the TLV buffer is equal to the entire command response
1490 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1491 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1492 * response header (S_DS_GEN)
1493 */
David Woodhousefa62f992008-03-03 12:18:03 +01001494 tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1495 + sizeof(scanresp->nr_sets)
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001496 + S_DS_GEN);
1497
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001498 /*
David Woodhousefa62f992008-03-03 12:18:03 +01001499 * Process each scan response returned (scanresp->nr_sets). Save
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001500 * the information in the newbssentry and then insert into the
1501 * driver scan table either as an update to an existing entry
1502 * or as an addition at the end of the table
1503 */
David Woodhousefa62f992008-03-03 12:18:03 +01001504 for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001505 struct bss_descriptor new;
David Woodhousefa62f992008-03-03 12:18:03 +01001506 struct bss_descriptor *found = NULL;
1507 struct bss_descriptor *oldest = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07001508 DECLARE_MAC_BUF(mac);
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001509
1510 /* Process the data fields and IEs returned for this BSS */
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001511 memset(&new, 0, sizeof (struct bss_descriptor));
David Woodhousefa62f992008-03-03 12:18:03 +01001512 if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001513 /* error parsing the scan response, skipped */
1514 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1515 continue;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001516 }
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001517
1518 /* Try to find this bss in the scan table */
David Woodhouseaa21c002007-12-08 20:04:36 +00001519 list_for_each_entry (iter_bss, &priv->network_list, list) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001520 if (is_same_network(iter_bss, &new)) {
1521 found = iter_bss;
1522 break;
1523 }
1524
1525 if ((oldest == NULL) ||
1526 (iter_bss->last_scanned < oldest->last_scanned))
1527 oldest = iter_bss;
1528 }
1529
1530 if (found) {
1531 /* found, clear it */
1532 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001533 } else if (!list_empty(&priv->network_free_list)) {
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001534 /* Pull one from the free list */
David Woodhouseaa21c002007-12-08 20:04:36 +00001535 found = list_entry(priv->network_free_list.next,
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001536 struct bss_descriptor, list);
David Woodhouseaa21c002007-12-08 20:04:36 +00001537 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001538 } else if (oldest) {
1539 /* If there are no more slots, expire the oldest */
1540 found = oldest;
1541 clear_bss_descriptor(found);
David Woodhouseaa21c002007-12-08 20:04:36 +00001542 list_move_tail(&found->list, &priv->network_list);
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001543 } else {
1544 continue;
1545 }
1546
David Woodhousefa62f992008-03-03 12:18:03 +01001547 lbs_deb_scan("SCAN_RESP: BSSID %s\n", print_mac(mac, new.bssid));
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001548
Dan Williamsfcdb53d2007-05-25 16:15:56 -04001549 /* Copy the locally created newbssentry to the scan table */
1550 memcpy(found, &new, offsetof(struct bss_descriptor, list));
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001551 }
1552
Holger Schurig9012b282007-05-25 11:27:16 -04001553 ret = 0;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001554
Holger Schurig9012b282007-05-25 11:27:16 -04001555done:
1556 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1557 return ret;
Marcelo Tosatti876c9d32007-02-10 12:25:27 -02001558}