Commit | Line | Data |
---|---|---|
876c9d3a MT |
1 | /** |
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 | */ | |
7e272fcf | 7 | #include <linux/types.h> |
fcdb53db | 8 | #include <linux/etherdevice.h> |
2c706002 | 9 | #include <linux/if_arp.h> |
ac630c2b | 10 | #include <asm/unaligned.h> |
7e272fcf JL |
11 | #include <net/lib80211.h> |
12 | ||
876c9d3a MT |
13 | #include "host.h" |
14 | #include "decl.h" | |
15 | #include "dev.h" | |
16 | #include "scan.h" | |
fa62f99c | 17 | #include "cmd.h" |
876c9d3a MT |
18 | |
19 | //! Approximate amount of data needed to pass a scan result back to iwlist | |
20 | #define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \ | |
21 | + IW_ESSID_MAX_SIZE \ | |
22 | + IW_EV_UINT_LEN \ | |
23 | + IW_EV_FREQ_LEN \ | |
24 | + IW_EV_QUAL_LEN \ | |
25 | + IW_ESSID_MAX_SIZE \ | |
26 | + IW_EV_PARAM_LEN \ | |
27 | + 40) /* 40 for WPAIE */ | |
28 | ||
29 | //! Memory needed to store a max sized channel List TLV for a firmware scan | |
75b6a61a | 30 | #define CHAN_TLV_MAX_SIZE (sizeof(struct mrvl_ie_header) \ |
876c9d3a MT |
31 | + (MRVDRV_MAX_CHANNELS_PER_SCAN \ |
32 | * sizeof(struct chanscanparamset))) | |
33 | ||
34 | //! Memory needed to store a max number/size SSID TLV for a firmware scan | |
75b6a61a | 35 | #define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvl_ie_ssid_param_set)) |
876c9d3a | 36 | |
fa62f99c DW |
37 | //! Maximum memory needed for a cmd_ds_802_11_scan with all TLVs at max |
38 | #define MAX_SCAN_CFG_ALLOC (sizeof(struct cmd_ds_802_11_scan) \ | |
39 | + CHAN_TLV_MAX_SIZE + SSID_TLV_MAX_SIZE) | |
876c9d3a MT |
40 | |
41 | //! The maximum number of channels the firmware can scan per command | |
42 | #define MRVDRV_MAX_CHANNELS_PER_SCAN 14 | |
43 | ||
44 | /** | |
45 | * @brief Number of channels to scan per firmware scan command issuance. | |
46 | * | |
47 | * Number restricted to prevent hitting the limit on the amount of scan data | |
48 | * returned in a single firmware scan command. | |
49 | */ | |
50 | #define MRVDRV_CHANNELS_PER_SCAN_CMD 4 | |
51 | ||
52 | //! Scan time specified in the channel TLV for each channel for passive scans | |
53 | #define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100 | |
54 | ||
55 | //! Scan time specified in the channel TLV for each channel for active scans | |
56 | #define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100 | |
57 | ||
2c706002 JB |
58 | #define DEFAULT_MAX_SCAN_AGE (15 * HZ) |
59 | ||
fa62f99c DW |
60 | static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy, |
61 | struct cmd_header *resp); | |
e56188ac HS |
62 | |
63 | /*********************************************************************/ | |
64 | /* */ | |
65 | /* Misc helper functions */ | |
66 | /* */ | |
67 | /*********************************************************************/ | |
68 | ||
23ff5036 HS |
69 | /** |
70 | * @brief Unsets the MSB on basic rates | |
71 | * | |
72 | * Scan through an array and unset the MSB for basic data rates. | |
73 | * | |
74 | * @param rates buffer of data rates | |
75 | * @param len size of buffer | |
76 | */ | |
77 | static void lbs_unset_basic_rate_flags(u8 *rates, size_t len) | |
78 | { | |
79 | int i; | |
80 | ||
81 | for (i = 0; i < len; i++) | |
82 | rates[i] &= 0x7f; | |
83 | } | |
84 | ||
85 | ||
f137e054 | 86 | static inline void clear_bss_descriptor(struct bss_descriptor *bss) |
fcdb53db DW |
87 | { |
88 | /* Don't blow away ->list, just BSS data */ | |
89 | memset(bss, 0, offsetof(struct bss_descriptor, list)); | |
90 | } | |
91 | ||
ffd074fc HS |
92 | /** |
93 | * @brief Compare two SSIDs | |
94 | * | |
95 | * @param ssid1 A pointer to ssid to compare | |
96 | * @param ssid2 A pointer to ssid to compare | |
97 | * | |
98 | * @return 0: ssid is same, otherwise is different | |
99 | */ | |
f137e054 DW |
100 | int lbs_ssid_cmp(uint8_t *ssid1, uint8_t ssid1_len, uint8_t *ssid2, |
101 | uint8_t ssid2_len) | |
ffd074fc HS |
102 | { |
103 | if (ssid1_len != ssid2_len) | |
104 | return -1; | |
105 | ||
106 | return memcmp(ssid1, ssid2, ssid1_len); | |
107 | } | |
108 | ||
ffd074fc HS |
109 | static inline int is_same_network(struct bss_descriptor *src, |
110 | struct bss_descriptor *dst) | |
111 | { | |
112 | /* A network is only a duplicate if the channel, BSSID, and ESSID | |
113 | * all match. We treat all <hidden> with the same BSSID and channel | |
114 | * as one network */ | |
115 | return ((src->ssid_len == dst->ssid_len) && | |
116 | (src->channel == dst->channel) && | |
117 | !compare_ether_addr(src->bssid, dst->bssid) && | |
118 | !memcmp(src->ssid, dst->ssid, src->ssid_len)); | |
119 | } | |
120 | ||
876c9d3a | 121 | |
e56188ac HS |
122 | |
123 | ||
e56188ac HS |
124 | /*********************************************************************/ |
125 | /* */ | |
126 | /* Main scanning support */ | |
127 | /* */ | |
128 | /*********************************************************************/ | |
129 | ||
876c9d3a MT |
130 | /** |
131 | * @brief Create a channel list for the driver to scan based on region info | |
132 | * | |
10078321 | 133 | * Only used from lbs_scan_setup_scan_config() |
e56188ac | 134 | * |
876c9d3a MT |
135 | * Use the driver region/band information to construct a comprehensive list |
136 | * of channels to scan. This routine is used for any scan that is not | |
137 | * provided a specific channel list to scan. | |
138 | * | |
69f9032d | 139 | * @param priv A pointer to struct lbs_private structure |
876c9d3a | 140 | * @param scanchanlist Output parameter: resulting channel list to scan |
876c9d3a MT |
141 | * |
142 | * @return void | |
143 | */ | |
ffd074fc | 144 | static int lbs_scan_create_channel_list(struct lbs_private *priv, |
52933d81 | 145 | struct chanscanparamset *scanchanlist) |
876c9d3a | 146 | { |
876c9d3a MT |
147 | struct region_channel *scanregion; |
148 | struct chan_freq_power *cfp; | |
149 | int rgnidx; | |
150 | int chanidx; | |
151 | int nextchan; | |
f137e054 | 152 | uint8_t scantype; |
876c9d3a MT |
153 | |
154 | chanidx = 0; | |
155 | ||
156 | /* Set the default scan type to the user specified type, will later | |
157 | * be changed to passive on a per channel basis if restricted by | |
158 | * regulatory requirements (11d or 11h) | |
159 | */ | |
4f2fdaaf | 160 | scantype = CMD_SCAN_TYPE_ACTIVE; |
876c9d3a | 161 | |
aa21c004 | 162 | for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) { |
f137e054 DW |
163 | if (priv->enable11d && (priv->connect_status != LBS_CONNECTED) |
164 | && (priv->mesh_connect_status != LBS_CONNECTED)) { | |
876c9d3a | 165 | /* Scan all the supported chan for the first scan */ |
aa21c004 | 166 | if (!priv->universal_channel[rgnidx].valid) |
876c9d3a | 167 | continue; |
aa21c004 | 168 | scanregion = &priv->universal_channel[rgnidx]; |
876c9d3a MT |
169 | |
170 | /* clear the parsed_region_chan for the first scan */ | |
aa21c004 DW |
171 | memset(&priv->parsed_region_chan, 0x00, |
172 | sizeof(priv->parsed_region_chan)); | |
876c9d3a | 173 | } else { |
aa21c004 | 174 | if (!priv->region_channel[rgnidx].valid) |
876c9d3a | 175 | continue; |
aa21c004 | 176 | scanregion = &priv->region_channel[rgnidx]; |
876c9d3a MT |
177 | } |
178 | ||
f137e054 DW |
179 | for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) { |
180 | struct chanscanparamset *chan = &scanchanlist[chanidx]; | |
876c9d3a MT |
181 | |
182 | cfp = scanregion->CFP + nextchan; | |
183 | ||
f137e054 DW |
184 | if (priv->enable11d) |
185 | scantype = lbs_get_scan_type_11d(cfp->channel, | |
186 | &priv->parsed_region_chan); | |
876c9d3a | 187 | |
f137e054 DW |
188 | if (scanregion->band == BAND_B || scanregion->band == BAND_G) |
189 | chan->radiotype = CMD_SCAN_RADIO_TYPE_BG; | |
876c9d3a | 190 | |
0aef64d7 | 191 | if (scantype == CMD_SCAN_TYPE_PASSIVE) { |
f137e054 DW |
192 | chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME); |
193 | chan->chanscanmode.passivescan = 1; | |
876c9d3a | 194 | } else { |
f137e054 DW |
195 | chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME); |
196 | chan->chanscanmode.passivescan = 0; | |
876c9d3a MT |
197 | } |
198 | ||
f137e054 | 199 | chan->channumber = cfp->channel; |
876c9d3a MT |
200 | } |
201 | } | |
ffd074fc | 202 | return chanidx; |
876c9d3a MT |
203 | } |
204 | ||
ffd074fc HS |
205 | /* |
206 | * Add SSID TLV of the form: | |
207 | * | |
208 | * TLV-ID SSID 00 00 | |
209 | * length 06 00 | |
210 | * ssid 4d 4e 54 45 53 54 | |
211 | */ | |
52933d81 | 212 | static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv) |
2afc0c5d | 213 | { |
75b6a61a | 214 | struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv; |
f137e054 | 215 | |
ffd074fc | 216 | ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID); |
52933d81 HS |
217 | ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len); |
218 | memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len); | |
219 | return sizeof(ssid_tlv->header) + priv->scan_ssid_len; | |
2afc0c5d DW |
220 | } |
221 | ||
ffd074fc HS |
222 | /* |
223 | * Add CHANLIST TLV of the form | |
876c9d3a | 224 | * |
ffd074fc HS |
225 | * TLV-ID CHANLIST 01 01 |
226 | * length 5b 00 | |
227 | * channel 1 00 01 00 00 00 64 00 | |
228 | * radio type 00 | |
229 | * channel 01 | |
230 | * scan type 00 | |
231 | * min scan time 00 00 | |
232 | * max scan time 64 00 | |
233 | * channel 2 00 02 00 00 00 64 00 | |
234 | * channel 3 00 03 00 00 00 64 00 | |
235 | * channel 4 00 04 00 00 00 64 00 | |
236 | * channel 5 00 05 00 00 00 64 00 | |
237 | * channel 6 00 06 00 00 00 64 00 | |
238 | * channel 7 00 07 00 00 00 64 00 | |
239 | * channel 8 00 08 00 00 00 64 00 | |
240 | * channel 9 00 09 00 00 00 64 00 | |
241 | * channel 10 00 0a 00 00 00 64 00 | |
242 | * channel 11 00 0b 00 00 00 64 00 | |
243 | * channel 12 00 0c 00 00 00 64 00 | |
244 | * channel 13 00 0d 00 00 00 64 00 | |
876c9d3a | 245 | * |
876c9d3a | 246 | */ |
f137e054 DW |
247 | static int lbs_scan_add_chanlist_tlv(uint8_t *tlv, |
248 | struct chanscanparamset *chan_list, | |
249 | int chan_count) | |
876c9d3a | 250 | { |
f137e054 | 251 | size_t size = sizeof(struct chanscanparamset) *chan_count; |
75b6a61a | 252 | struct mrvl_ie_chanlist_param_set *chan_tlv = (void *)tlv; |
ffd074fc HS |
253 | |
254 | chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST); | |
255 | memcpy(chan_tlv->chanscanparam, chan_list, size); | |
256 | chan_tlv->header.len = cpu_to_le16(size); | |
257 | return sizeof(chan_tlv->header) + size; | |
876c9d3a MT |
258 | } |
259 | ||
ffd074fc HS |
260 | /* |
261 | * Add RATES TLV of the form | |
876c9d3a | 262 | * |
ffd074fc HS |
263 | * TLV-ID RATES 01 00 |
264 | * length 0e 00 | |
265 | * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c | |
876c9d3a | 266 | * |
ffd074fc HS |
267 | * The rates are in lbs_bg_rates[], but for the 802.11b |
268 | * rates the high bit isn't set. | |
876c9d3a | 269 | */ |
f137e054 | 270 | static int lbs_scan_add_rates_tlv(uint8_t *tlv) |
876c9d3a | 271 | { |
ffd074fc | 272 | int i; |
75b6a61a | 273 | struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv; |
ffd074fc HS |
274 | |
275 | rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES); | |
276 | tlv += sizeof(rate_tlv->header); | |
277 | for (i = 0; i < MAX_RATES; i++) { | |
278 | *tlv = lbs_bg_rates[i]; | |
279 | if (*tlv == 0) | |
280 | break; | |
281 | /* This code makes sure that the 802.11b rates (1 MBit/s, 2 | |
282 | MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set. | |
283 | Note that the values are MBit/s * 2, to mark them as | |
284 | basic rates so that the firmware likes it better */ | |
285 | if (*tlv == 0x02 || *tlv == 0x04 || | |
286 | *tlv == 0x0b || *tlv == 0x16) | |
287 | *tlv |= 0x80; | |
288 | tlv++; | |
2afc0c5d | 289 | } |
ffd074fc HS |
290 | rate_tlv->header.len = cpu_to_le16(i); |
291 | return sizeof(rate_tlv->header) + i; | |
876c9d3a MT |
292 | } |
293 | ||
e56188ac | 294 | /* |
ffd074fc HS |
295 | * Generate the CMD_802_11_SCAN command with the proper tlv |
296 | * for a bunch of channels. | |
297 | */ | |
fa62f99c | 298 | static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype, |
52933d81 | 299 | struct chanscanparamset *chan_list, int chan_count) |
eb8f7330 | 300 | { |
ffd074fc | 301 | int ret = -ENOMEM; |
fa62f99c DW |
302 | struct cmd_ds_802_11_scan *scan_cmd; |
303 | uint8_t *tlv; /* pointer into our current, growing TLV storage area */ | |
eb8f7330 | 304 | |
fa62f99c | 305 | lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d", |
c0d43990 HS |
306 | bsstype, chan_list ? chan_list[0].channumber : -1, |
307 | chan_count); | |
e56188ac | 308 | |
ffd074fc HS |
309 | /* create the fixed part for scan command */ |
310 | scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL); | |
311 | if (scan_cmd == NULL) | |
e56188ac | 312 | goto out; |
fa62f99c | 313 | |
ffd074fc | 314 | tlv = scan_cmd->tlvbuffer; |
52933d81 HS |
315 | /* TODO: do we need to scan for a specific BSSID? |
316 | memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */ | |
ffd074fc HS |
317 | scan_cmd->bsstype = bsstype; |
318 | ||
319 | /* add TLVs */ | |
52933d81 HS |
320 | if (priv->scan_ssid_len) |
321 | tlv += lbs_scan_add_ssid_tlv(priv, tlv); | |
ffd074fc HS |
322 | if (chan_list && chan_count) |
323 | tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count); | |
324 | tlv += lbs_scan_add_rates_tlv(tlv); | |
325 | ||
326 | /* This is the final data we are about to send */ | |
fa62f99c DW |
327 | scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd); |
328 | lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd, | |
329 | sizeof(*scan_cmd)); | |
ffd074fc | 330 | lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer, |
fa62f99c DW |
331 | tlv - scan_cmd->tlvbuffer); |
332 | ||
333 | ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr, | |
334 | le16_to_cpu(scan_cmd->hdr.size), | |
335 | lbs_ret_80211_scan, 0); | |
ffd074fc | 336 | |
e56188ac | 337 | out: |
ffd074fc HS |
338 | kfree(scan_cmd); |
339 | lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret); | |
340 | return ret; | |
eb8f7330 DW |
341 | } |
342 | ||
876c9d3a MT |
343 | /** |
344 | * @brief Internal function used to start a scan based on an input config | |
345 | * | |
346 | * Use the input user scan configuration information when provided in | |
347 | * order to send the appropriate scan commands to firmware to populate or | |
348 | * update the internal driver scan table | |
349 | * | |
69f9032d | 350 | * @param priv A pointer to struct lbs_private structure |
52933d81 | 351 | * @param full_scan Do a full-scan (blocking) |
876c9d3a MT |
352 | * |
353 | * @return 0 or < 0 if error | |
354 | */ | |
245bf20f | 355 | int lbs_scan_networks(struct lbs_private *priv, int full_scan) |
876c9d3a | 356 | { |
ffd074fc HS |
357 | int ret = -ENOMEM; |
358 | struct chanscanparamset *chan_list; | |
359 | struct chanscanparamset *curr_chans; | |
360 | int chan_count; | |
f137e054 | 361 | uint8_t bsstype = CMD_BSS_TYPE_ANY; |
ffd074fc | 362 | int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD; |
ffd074fc | 363 | union iwreq_data wrqu; |
f8f55108 | 364 | #ifdef CONFIG_LIBERTAS_DEBUG |
ffd074fc | 365 | struct bss_descriptor *iter; |
f8f55108 | 366 | int i = 0; |
9387b7ca | 367 | DECLARE_SSID_BUF(ssid); |
f8f55108 | 368 | #endif |
876c9d3a | 369 | |
f137e054 | 370 | lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan); |
2afc0c5d DW |
371 | |
372 | /* Cancel any partial outstanding partial scans if this scan | |
373 | * is a full scan. | |
374 | */ | |
375 | if (full_scan && delayed_work_pending(&priv->scan_work)) | |
376 | cancel_delayed_work(&priv->scan_work); | |
876c9d3a | 377 | |
52933d81 HS |
378 | /* User-specified bsstype or channel list |
379 | TODO: this can be implemented if some user-space application | |
380 | need the feature. Formerly, it was accessible from debugfs, | |
381 | but then nowhere used. | |
ffd074fc HS |
382 | if (user_cfg) { |
383 | if (user_cfg->bsstype) | |
52933d81 HS |
384 | bsstype = user_cfg->bsstype; |
385 | } */ | |
386 | ||
387 | lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype); | |
876c9d3a | 388 | |
ffd074fc HS |
389 | /* Create list of channels to scan */ |
390 | chan_list = kzalloc(sizeof(struct chanscanparamset) * | |
f137e054 | 391 | LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL); |
ffd074fc HS |
392 | if (!chan_list) { |
393 | lbs_pr_alert("SCAN: chan_list empty\n"); | |
876c9d3a MT |
394 | goto out; |
395 | } | |
396 | ||
ffd074fc | 397 | /* We want to scan all channels */ |
52933d81 | 398 | chan_count = lbs_scan_create_channel_list(priv, chan_list); |
876c9d3a | 399 | |
ffd074fc HS |
400 | netif_stop_queue(priv->dev); |
401 | netif_carrier_off(priv->dev); | |
402 | if (priv->mesh_dev) { | |
a27b9f96 DW |
403 | netif_stop_queue(priv->mesh_dev); |
404 | netif_carrier_off(priv->mesh_dev); | |
876c9d3a MT |
405 | } |
406 | ||
ffd074fc | 407 | /* Prepare to continue an interrupted scan */ |
8816edce HS |
408 | lbs_deb_scan("chan_count %d, scan_channel %d\n", |
409 | chan_count, priv->scan_channel); | |
ffd074fc HS |
410 | curr_chans = chan_list; |
411 | /* advance channel list by already-scanned-channels */ | |
8816edce HS |
412 | if (priv->scan_channel > 0) { |
413 | curr_chans += priv->scan_channel; | |
414 | chan_count -= priv->scan_channel; | |
ffd074fc HS |
415 | } |
416 | ||
417 | /* Send scan command(s) | |
418 | * numchannels contains the number of channels we should maximally scan | |
419 | * chan_count is the total number of channels to scan | |
420 | */ | |
421 | ||
422 | while (chan_count) { | |
423 | int to_scan = min(numchannels, chan_count); | |
424 | lbs_deb_scan("scanning %d of %d channels\n", | |
f137e054 | 425 | to_scan, chan_count); |
ffd074fc | 426 | ret = lbs_do_scan(priv, bsstype, curr_chans, |
52933d81 | 427 | to_scan); |
ffd074fc HS |
428 | if (ret) { |
429 | lbs_pr_err("SCAN_CMD failed\n"); | |
430 | goto out2; | |
431 | } | |
432 | curr_chans += to_scan; | |
433 | chan_count -= to_scan; | |
434 | ||
435 | /* somehow schedule the next part of the scan */ | |
f137e054 | 436 | if (chan_count && !full_scan && |
aa21c004 | 437 | !priv->surpriseremoved) { |
ffd074fc | 438 | /* -1 marks just that we're currently scanning */ |
8816edce HS |
439 | if (priv->scan_channel < 0) |
440 | priv->scan_channel = to_scan; | |
ffd074fc | 441 | else |
8816edce | 442 | priv->scan_channel += to_scan; |
ffd074fc HS |
443 | cancel_delayed_work(&priv->scan_work); |
444 | queue_delayed_work(priv->work_thread, &priv->scan_work, | |
f137e054 | 445 | msecs_to_jiffies(300)); |
ffd074fc HS |
446 | /* skip over GIWSCAN event */ |
447 | goto out; | |
448 | } | |
449 | ||
450 | } | |
451 | memset(&wrqu, 0, sizeof(union iwreq_data)); | |
452 | wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL); | |
876c9d3a | 453 | |
f8f55108 DW |
454 | #ifdef CONFIG_LIBERTAS_DEBUG |
455 | /* Dump the scan table */ | |
aa21c004 | 456 | mutex_lock(&priv->lock); |
ffd074fc | 457 | lbs_deb_scan("scan table:\n"); |
aa21c004 | 458 | list_for_each_entry(iter, &priv->network_list, list) |
e174961c JB |
459 | lbs_deb_scan("%02d: BSSID %pM, RSSI %d, SSID '%s'\n", |
460 | i++, iter->bssid, iter->rssi, | |
9387b7ca | 461 | print_ssid(ssid, iter->ssid, iter->ssid_len)); |
aa21c004 | 462 | mutex_unlock(&priv->lock); |
f8f55108 | 463 | #endif |
876c9d3a | 464 | |
ffd074fc | 465 | out2: |
8816edce | 466 | priv->scan_channel = 0; |
ffd074fc HS |
467 | |
468 | out: | |
aa21c004 | 469 | if (priv->connect_status == LBS_CONNECTED) { |
634b8f49 | 470 | netif_carrier_on(priv->dev); |
a27b9f96 DW |
471 | if (!priv->tx_pending_len) |
472 | netif_wake_queue(priv->dev); | |
01d77d8d | 473 | } |
aa21c004 | 474 | if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) { |
01d77d8d | 475 | netif_carrier_on(priv->mesh_dev); |
a27b9f96 DW |
476 | if (!priv->tx_pending_len) |
477 | netif_wake_queue(priv->mesh_dev); | |
876c9d3a | 478 | } |
ffd074fc | 479 | kfree(chan_list); |
876c9d3a | 480 | |
9012b28a | 481 | lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret); |
876c9d3a MT |
482 | return ret; |
483 | } | |
484 | ||
52933d81 HS |
485 | void lbs_scan_worker(struct work_struct *work) |
486 | { | |
487 | struct lbs_private *priv = | |
488 | container_of(work, struct lbs_private, scan_work.work); | |
489 | ||
490 | lbs_deb_enter(LBS_DEB_SCAN); | |
491 | lbs_scan_networks(priv, 0); | |
492 | lbs_deb_leave(LBS_DEB_SCAN); | |
493 | } | |
494 | ||
495 | ||
ffd074fc HS |
496 | /*********************************************************************/ |
497 | /* */ | |
498 | /* Result interpretation */ | |
499 | /* */ | |
500 | /*********************************************************************/ | |
501 | ||
876c9d3a MT |
502 | /** |
503 | * @brief Interpret a BSS scan response returned from the firmware | |
504 | * | |
505 | * Parse the various fixed fields and IEs passed back for a a BSS probe | |
ffd074fc HS |
506 | * response or beacon from the scan command. Record information as needed |
507 | * in the scan table struct bss_descriptor for that entry. | |
876c9d3a | 508 | * |
fcdb53db | 509 | * @param bss Output parameter: Pointer to the BSS Entry |
876c9d3a MT |
510 | * |
511 | * @return 0 or -1 | |
512 | */ | |
10078321 | 513 | static int lbs_process_bss(struct bss_descriptor *bss, |
f137e054 | 514 | uint8_t **pbeaconinfo, int *bytesleft) |
876c9d3a | 515 | { |
5fd164e9 DW |
516 | struct ieee_ie_fh_param_set *fh; |
517 | struct ieee_ie_ds_param_set *ds; | |
518 | struct ieee_ie_cf_param_set *cf; | |
519 | struct ieee_ie_ibss_param_set *ibss; | |
9387b7ca | 520 | DECLARE_SSID_BUF(ssid); |
75b6a61a | 521 | struct ieee_ie_country_info_set *pcountryinfo; |
f137e054 DW |
522 | uint8_t *pos, *end, *p; |
523 | uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0; | |
524 | uint16_t beaconsize = 0; | |
8c512765 | 525 | int ret; |
876c9d3a | 526 | |
e56188ac | 527 | lbs_deb_enter(LBS_DEB_SCAN); |
876c9d3a | 528 | |
876c9d3a MT |
529 | if (*bytesleft >= sizeof(beaconsize)) { |
530 | /* Extract & convert beacon size from the command buffer */ | |
533dd1b0 | 531 | beaconsize = get_unaligned_le16(*pbeaconinfo); |
876c9d3a MT |
532 | *bytesleft -= sizeof(beaconsize); |
533 | *pbeaconinfo += sizeof(beaconsize); | |
534 | } | |
535 | ||
536 | if (beaconsize == 0 || beaconsize > *bytesleft) { | |
876c9d3a MT |
537 | *pbeaconinfo += *bytesleft; |
538 | *bytesleft = 0; | |
e56188ac HS |
539 | ret = -1; |
540 | goto done; | |
876c9d3a MT |
541 | } |
542 | ||
543 | /* Initialize the current working beacon pointer for this BSS iteration */ | |
ab617971 DW |
544 | pos = *pbeaconinfo; |
545 | end = pos + beaconsize; | |
876c9d3a MT |
546 | |
547 | /* Advance the return beacon pointer past the current beacon */ | |
548 | *pbeaconinfo += beaconsize; | |
549 | *bytesleft -= beaconsize; | |
550 | ||
ab617971 | 551 | memcpy(bss->bssid, pos, ETH_ALEN); |
e174961c | 552 | lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid); |
ab617971 | 553 | pos += ETH_ALEN; |
876c9d3a | 554 | |
ab617971 | 555 | if ((end - pos) < 12) { |
fcdb53db | 556 | lbs_deb_scan("process_bss: Not enough bytes left\n"); |
e56188ac HS |
557 | ret = -1; |
558 | goto done; | |
876c9d3a MT |
559 | } |
560 | ||
561 | /* | |
562 | * next 4 fields are RSSI, time stamp, beacon interval, | |
563 | * and capability information | |
564 | */ | |
565 | ||
566 | /* RSSI is 1 byte long */ | |
ab617971 | 567 | bss->rssi = *pos; |
ffd074fc | 568 | lbs_deb_scan("process_bss: RSSI %d\n", *pos); |
ab617971 | 569 | pos++; |
876c9d3a MT |
570 | |
571 | /* time stamp is 8 bytes long */ | |
ab617971 | 572 | pos += 8; |
876c9d3a MT |
573 | |
574 | /* beacon interval is 2 bytes long */ | |
814feefa | 575 | bss->beaconperiod = get_unaligned_le16(pos); |
ab617971 | 576 | pos += 2; |
876c9d3a MT |
577 | |
578 | /* capability information is 2 bytes long */ | |
814feefa | 579 | bss->capability = get_unaligned_le16(pos); |
ffd074fc | 580 | lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability); |
ab617971 | 581 | pos += 2; |
876c9d3a | 582 | |
0c9ca690 | 583 | if (bss->capability & WLAN_CAPABILITY_PRIVACY) |
ffd074fc | 584 | lbs_deb_scan("process_bss: WEP enabled\n"); |
0c9ca690 DW |
585 | if (bss->capability & WLAN_CAPABILITY_IBSS) |
586 | bss->mode = IW_MODE_ADHOC; | |
587 | else | |
588 | bss->mode = IW_MODE_INFRA; | |
589 | ||
876c9d3a | 590 | /* rest of the current buffer are IE's */ |
ffd074fc | 591 | lbs_deb_scan("process_bss: IE len %zd\n", end - pos); |
ece56191 | 592 | lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos); |
876c9d3a | 593 | |
876c9d3a | 594 | /* process variable IE */ |
ab617971 | 595 | while (pos <= end - 2) { |
2c706002 | 596 | if (pos + pos[1] > end) { |
fcdb53db | 597 | lbs_deb_scan("process_bss: error in processing IE, " |
f137e054 | 598 | "bytes left < IE length\n"); |
ab617971 | 599 | break; |
876c9d3a MT |
600 | } |
601 | ||
2c706002 JB |
602 | switch (pos[0]) { |
603 | case WLAN_EID_SSID: | |
604 | bss->ssid_len = min_t(int, IEEE80211_MAX_SSID_LEN, pos[1]); | |
605 | memcpy(bss->ssid, pos + 2, bss->ssid_len); | |
ffd074fc | 606 | lbs_deb_scan("got SSID IE: '%s', len %u\n", |
9387b7ca | 607 | print_ssid(ssid, bss->ssid, bss->ssid_len), |
d8efea25 | 608 | bss->ssid_len); |
876c9d3a MT |
609 | break; |
610 | ||
2c706002 JB |
611 | case WLAN_EID_SUPP_RATES: |
612 | n_basic_rates = min_t(uint8_t, MAX_RATES, pos[1]); | |
613 | memcpy(bss->rates, pos + 2, n_basic_rates); | |
8c512765 | 614 | got_basic_rates = 1; |
ffd074fc | 615 | lbs_deb_scan("got RATES IE\n"); |
876c9d3a MT |
616 | break; |
617 | ||
2c706002 | 618 | case WLAN_EID_FH_PARAMS: |
5fd164e9 DW |
619 | fh = (struct ieee_ie_fh_param_set *) pos; |
620 | memcpy(&bss->phy.fh, fh, sizeof(*fh)); | |
ffd074fc | 621 | lbs_deb_scan("got FH IE\n"); |
876c9d3a MT |
622 | break; |
623 | ||
2c706002 | 624 | case WLAN_EID_DS_PARAMS: |
5fd164e9 DW |
625 | ds = (struct ieee_ie_ds_param_set *) pos; |
626 | bss->channel = ds->channel; | |
627 | memcpy(&bss->phy.ds, ds, sizeof(*ds)); | |
ffd074fc | 628 | lbs_deb_scan("got DS IE, channel %d\n", bss->channel); |
876c9d3a MT |
629 | break; |
630 | ||
2c706002 | 631 | case WLAN_EID_CF_PARAMS: |
5fd164e9 DW |
632 | cf = (struct ieee_ie_cf_param_set *) pos; |
633 | memcpy(&bss->ss.cf, cf, sizeof(*cf)); | |
ffd074fc | 634 | lbs_deb_scan("got CF IE\n"); |
876c9d3a MT |
635 | break; |
636 | ||
2c706002 | 637 | case WLAN_EID_IBSS_PARAMS: |
5fd164e9 DW |
638 | ibss = (struct ieee_ie_ibss_param_set *) pos; |
639 | bss->atimwindow = ibss->atimwindow; | |
640 | memcpy(&bss->ss.ibss, ibss, sizeof(*ibss)); | |
ffd074fc | 641 | lbs_deb_scan("got IBSS IE\n"); |
876c9d3a MT |
642 | break; |
643 | ||
2c706002 | 644 | case WLAN_EID_COUNTRY: |
75b6a61a | 645 | pcountryinfo = (struct ieee_ie_country_info_set *) pos; |
ffd074fc | 646 | lbs_deb_scan("got COUNTRY IE\n"); |
75b6a61a DW |
647 | if (pcountryinfo->header.len < sizeof(pcountryinfo->countrycode) |
648 | || pcountryinfo->header.len > 254) { | |
649 | lbs_deb_scan("%s: 11D- Err CountryInfo len %d, min %zd, max 254\n", | |
650 | __func__, | |
651 | pcountryinfo->header.len, | |
652 | sizeof(pcountryinfo->countrycode)); | |
9012b28a HS |
653 | ret = -1; |
654 | goto done; | |
876c9d3a MT |
655 | } |
656 | ||
75b6a61a DW |
657 | memcpy(&bss->countryinfo, pcountryinfo, |
658 | pcountryinfo->header.len + 2); | |
ece56191 | 659 | lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo", |
f137e054 | 660 | (uint8_t *) pcountryinfo, |
75b6a61a | 661 | (int) (pcountryinfo->header.len + 2)); |
876c9d3a MT |
662 | break; |
663 | ||
2c706002 | 664 | case WLAN_EID_EXT_SUPP_RATES: |
ab617971 DW |
665 | /* only process extended supported rate if data rate is |
666 | * already found. Data rate IE should come before | |
876c9d3a MT |
667 | * extended supported rate IE |
668 | */ | |
ffd074fc HS |
669 | lbs_deb_scan("got RATESEX IE\n"); |
670 | if (!got_basic_rates) { | |
671 | lbs_deb_scan("... but ignoring it\n"); | |
ab617971 | 672 | break; |
ffd074fc | 673 | } |
876c9d3a | 674 | |
2c706002 | 675 | n_ex_rates = pos[1]; |
8c512765 DW |
676 | if (n_basic_rates + n_ex_rates > MAX_RATES) |
677 | n_ex_rates = MAX_RATES - n_basic_rates; | |
876c9d3a | 678 | |
8c512765 | 679 | p = bss->rates + n_basic_rates; |
2c706002 | 680 | memcpy(p, pos + 2, n_ex_rates); |
876c9d3a | 681 | break; |
ab617971 | 682 | |
2c706002 JB |
683 | case WLAN_EID_GENERIC: |
684 | if (pos[1] >= 4 && | |
685 | pos[2] == 0x00 && pos[3] == 0x50 && | |
686 | pos[4] == 0xf2 && pos[5] == 0x01) { | |
687 | bss->wpa_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN); | |
688 | memcpy(bss->wpa_ie, pos, bss->wpa_ie_len); | |
ffd074fc | 689 | lbs_deb_scan("got WPA IE\n"); |
2c706002 JB |
690 | lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie, |
691 | bss->wpa_ie_len); | |
692 | } else if (pos[1] >= MARVELL_MESH_IE_LENGTH && | |
693 | pos[2] == 0x00 && pos[3] == 0x50 && | |
baf62eec | 694 | pos[4] == 0x43 && pos[5] == 0x04) { |
ffd074fc | 695 | lbs_deb_scan("got mesh IE\n"); |
1e838bf3 | 696 | bss->mesh = 1; |
ffd074fc | 697 | } else { |
f137e054 | 698 | lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n", |
2c706002 JB |
699 | pos[2], pos[3], |
700 | pos[4], pos[5], | |
701 | pos[1]); | |
ab617971 | 702 | } |
876c9d3a | 703 | break; |
ab617971 | 704 | |
2c706002 | 705 | case WLAN_EID_RSN: |
ffd074fc | 706 | lbs_deb_scan("got RSN IE\n"); |
2c706002 JB |
707 | bss->rsn_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN); |
708 | memcpy(bss->rsn_ie, pos, bss->rsn_ie_len); | |
ffd074fc | 709 | lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE", |
2c706002 | 710 | bss->rsn_ie, bss->rsn_ie_len); |
876c9d3a MT |
711 | break; |
712 | ||
ab617971 | 713 | default: |
ffd074fc | 714 | lbs_deb_scan("got IE 0x%04x, len %d\n", |
2c706002 | 715 | pos[0], pos[1]); |
876c9d3a MT |
716 | break; |
717 | } | |
718 | ||
2c706002 | 719 | pos += pos[1] + 2; |
ab617971 | 720 | } |
fcdb53db DW |
721 | |
722 | /* Timestamp */ | |
723 | bss->last_scanned = jiffies; | |
10078321 | 724 | lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates)); |
fcdb53db | 725 | |
9012b28a | 726 | ret = 0; |
876c9d3a | 727 | |
9012b28a HS |
728 | done: |
729 | lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret); | |
730 | return ret; | |
876c9d3a MT |
731 | } |
732 | ||
876c9d3a MT |
733 | /** |
734 | * @brief Send a scan command for all available channels filtered on a spec | |
735 | * | |
e56188ac HS |
736 | * Used in association code and from debugfs |
737 | * | |
69f9032d | 738 | * @param priv A pointer to struct lbs_private structure |
e56188ac HS |
739 | * @param ssid A pointer to the SSID to scan for |
740 | * @param ssid_len Length of the SSID | |
876c9d3a MT |
741 | * |
742 | * @return 0-success, otherwise fail | |
743 | */ | |
f137e054 | 744 | int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid, |
52933d81 | 745 | uint8_t ssid_len) |
876c9d3a | 746 | { |
9387b7ca | 747 | DECLARE_SSID_BUF(ssid_buf); |
eb8f7330 | 748 | int ret = 0; |
876c9d3a | 749 | |
52933d81 | 750 | lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n", |
9387b7ca | 751 | print_ssid(ssid_buf, ssid, ssid_len)); |
876c9d3a | 752 | |
d8efea25 | 753 | if (!ssid_len) |
eb8f7330 | 754 | goto out; |
876c9d3a | 755 | |
52933d81 HS |
756 | memcpy(priv->scan_ssid, ssid, ssid_len); |
757 | priv->scan_ssid_len = ssid_len; | |
876c9d3a | 758 | |
52933d81 | 759 | lbs_scan_networks(priv, 1); |
aa21c004 | 760 | if (priv->surpriseremoved) { |
e56188ac HS |
761 | ret = -1; |
762 | goto out; | |
763 | } | |
876c9d3a | 764 | |
eb8f7330 | 765 | out: |
e56188ac | 766 | lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret); |
eb8f7330 | 767 | return ret; |
876c9d3a MT |
768 | } |
769 | ||
e56188ac HS |
770 | |
771 | ||
772 | ||
773 | /*********************************************************************/ | |
774 | /* */ | |
775 | /* Support for Wireless Extensions */ | |
776 | /* */ | |
777 | /*********************************************************************/ | |
778 | ||
ffd074fc | 779 | |
00af0157 DW |
780 | #define MAX_CUSTOM_LEN 64 |
781 | ||
69f9032d | 782 | static inline char *lbs_translate_scan(struct lbs_private *priv, |
ccc58057 DM |
783 | struct iw_request_info *info, |
784 | char *start, char *stop, | |
785 | struct bss_descriptor *bss) | |
876c9d3a | 786 | { |
876c9d3a | 787 | struct chan_freq_power *cfp; |
876c9d3a MT |
788 | char *current_val; /* For rates */ |
789 | struct iw_event iwe; /* Temporary buffer */ | |
876c9d3a | 790 | int j; |
f137e054 DW |
791 | #define PERFECT_RSSI ((uint8_t)50) |
792 | #define WORST_RSSI ((uint8_t)0) | |
793 | #define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI)) | |
794 | uint8_t rssi; | |
876c9d3a | 795 | |
e56188ac HS |
796 | lbs_deb_enter(LBS_DEB_SCAN); |
797 | ||
aa21c004 | 798 | cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel); |
fcdb53db DW |
799 | if (!cfp) { |
800 | lbs_deb_scan("Invalid channel number %d\n", bss->channel); | |
e56188ac HS |
801 | start = NULL; |
802 | goto out; | |
2be92196 | 803 | } |
876c9d3a | 804 | |
ffd074fc | 805 | /* First entry *MUST* be the BSSID */ |
fcdb53db DW |
806 | iwe.cmd = SIOCGIWAP; |
807 | iwe.u.ap_addr.sa_family = ARPHRD_ETHER; | |
808 | memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN); | |
ccc58057 | 809 | start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN); |
fcdb53db DW |
810 | |
811 | /* SSID */ | |
812 | iwe.cmd = SIOCGIWESSID; | |
813 | iwe.u.data.flags = 1; | |
f137e054 | 814 | iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IW_ESSID_MAX_SIZE); |
ccc58057 | 815 | start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid); |
fcdb53db DW |
816 | |
817 | /* Mode */ | |
818 | iwe.cmd = SIOCGIWMODE; | |
819 | iwe.u.mode = bss->mode; | |
ccc58057 | 820 | start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN); |
fcdb53db DW |
821 | |
822 | /* Frequency */ | |
823 | iwe.cmd = SIOCGIWFREQ; | |
824 | iwe.u.freq.m = (long)cfp->freq * 100000; | |
825 | iwe.u.freq.e = 1; | |
ccc58057 | 826 | start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN); |
fcdb53db DW |
827 | |
828 | /* Add quality statistics */ | |
829 | iwe.cmd = IWEVQUAL; | |
830 | iwe.u.qual.updated = IW_QUAL_ALL_UPDATED; | |
831 | iwe.u.qual.level = SCAN_RSSI(bss->rssi); | |
832 | ||
833 | rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE; | |
834 | iwe.u.qual.qual = | |
f137e054 DW |
835 | (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) * |
836 | (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) / | |
837 | (RSSI_DIFF * RSSI_DIFF); | |
fcdb53db DW |
838 | if (iwe.u.qual.qual > 100) |
839 | iwe.u.qual.qual = 100; | |
840 | ||
aa21c004 | 841 | if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) { |
fcdb53db DW |
842 | iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE; |
843 | } else { | |
f137e054 | 844 | iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]); |
fcdb53db | 845 | } |
80e78ef7 DW |
846 | |
847 | /* Locally created ad-hoc BSSs won't have beacons if this is the | |
848 | * only station in the adhoc network; so get signal strength | |
849 | * from receive statistics. | |
850 | */ | |
f137e054 | 851 | if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate |
aa21c004 | 852 | && !lbs_ssid_cmp(priv->curbssparams.ssid, |
f137e054 DW |
853 | priv->curbssparams.ssid_len, |
854 | bss->ssid, bss->ssid_len)) { | |
80e78ef7 | 855 | int snr, nf; |
aa21c004 DW |
856 | snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE; |
857 | nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE; | |
80e78ef7 | 858 | iwe.u.qual.level = CAL_RSSI(snr, nf); |
fcdb53db | 859 | } |
ccc58057 | 860 | start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN); |
876c9d3a | 861 | |
fcdb53db DW |
862 | /* Add encryption capability */ |
863 | iwe.cmd = SIOCGIWENCODE; | |
0c9ca690 | 864 | if (bss->capability & WLAN_CAPABILITY_PRIVACY) { |
fcdb53db DW |
865 | iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY; |
866 | } else { | |
867 | iwe.u.data.flags = IW_ENCODE_DISABLED; | |
868 | } | |
869 | iwe.u.data.length = 0; | |
ccc58057 | 870 | start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid); |
876c9d3a | 871 | |
ccc58057 | 872 | current_val = start + iwe_stream_lcp_len(info); |
876c9d3a | 873 | |
fcdb53db DW |
874 | iwe.cmd = SIOCGIWRATE; |
875 | iwe.u.bitrate.fixed = 0; | |
876 | iwe.u.bitrate.disabled = 0; | |
877 | iwe.u.bitrate.value = 0; | |
876c9d3a | 878 | |
8c512765 DW |
879 | for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) { |
880 | /* Bit rate given in 500 kb/s units */ | |
881 | iwe.u.bitrate.value = bss->rates[j] * 500000; | |
ccc58057 DM |
882 | current_val = iwe_stream_add_value(info, start, current_val, |
883 | stop, &iwe, IW_EV_PARAM_LEN); | |
fcdb53db | 884 | } |
f137e054 | 885 | if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate |
aa21c004 | 886 | && !lbs_ssid_cmp(priv->curbssparams.ssid, |
f137e054 DW |
887 | priv->curbssparams.ssid_len, |
888 | bss->ssid, bss->ssid_len)) { | |
fcdb53db | 889 | iwe.u.bitrate.value = 22 * 500000; |
ccc58057 | 890 | current_val = iwe_stream_add_value(info, start, current_val, |
f137e054 | 891 | stop, &iwe, IW_EV_PARAM_LEN); |
fcdb53db DW |
892 | } |
893 | /* Check if we added any event */ | |
ccc58057 | 894 | if ((current_val - start) > iwe_stream_lcp_len(info)) |
fcdb53db DW |
895 | start = current_val; |
896 | ||
897 | memset(&iwe, 0, sizeof(iwe)); | |
898 | if (bss->wpa_ie_len) { | |
899 | char buf[MAX_WPA_IE_LEN]; | |
900 | memcpy(buf, bss->wpa_ie, bss->wpa_ie_len); | |
901 | iwe.cmd = IWEVGENIE; | |
902 | iwe.u.data.length = bss->wpa_ie_len; | |
ccc58057 | 903 | start = iwe_stream_add_point(info, start, stop, &iwe, buf); |
fcdb53db | 904 | } |
876c9d3a | 905 | |
fcdb53db DW |
906 | memset(&iwe, 0, sizeof(iwe)); |
907 | if (bss->rsn_ie_len) { | |
908 | char buf[MAX_WPA_IE_LEN]; | |
909 | memcpy(buf, bss->rsn_ie, bss->rsn_ie_len); | |
910 | iwe.cmd = IWEVGENIE; | |
911 | iwe.u.data.length = bss->rsn_ie_len; | |
ccc58057 | 912 | start = iwe_stream_add_point(info, start, stop, &iwe, buf); |
fcdb53db | 913 | } |
876c9d3a | 914 | |
00af0157 DW |
915 | if (bss->mesh) { |
916 | char custom[MAX_CUSTOM_LEN]; | |
917 | char *p = custom; | |
918 | ||
919 | iwe.cmd = IWEVCUSTOM; | |
f137e054 | 920 | p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc"); |
00af0157 DW |
921 | iwe.u.data.length = p - custom; |
922 | if (iwe.u.data.length) | |
ccc58057 DM |
923 | start = iwe_stream_add_point(info, start, stop, |
924 | &iwe, custom); | |
00af0157 DW |
925 | } |
926 | ||
e56188ac HS |
927 | out: |
928 | lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start); | |
fcdb53db DW |
929 | return start; |
930 | } | |
876c9d3a | 931 | |
ffd074fc HS |
932 | |
933 | /** | |
934 | * @brief Handle Scan Network ioctl | |
935 | * | |
936 | * @param dev A pointer to net_device structure | |
937 | * @param info A pointer to iw_request_info structure | |
938 | * @param vwrq A pointer to iw_param structure | |
939 | * @param extra A pointer to extra data buf | |
940 | * | |
941 | * @return 0 --success, otherwise fail | |
942 | */ | |
943 | int lbs_set_scan(struct net_device *dev, struct iw_request_info *info, | |
52933d81 | 944 | union iwreq_data *wrqu, char *extra) |
ffd074fc | 945 | { |
9387b7ca | 946 | DECLARE_SSID_BUF(ssid); |
ab65f649 | 947 | struct lbs_private *priv = dev->ml_priv; |
52933d81 | 948 | int ret = 0; |
ffd074fc | 949 | |
52933d81 | 950 | lbs_deb_enter(LBS_DEB_WEXT); |
ffd074fc | 951 | |
d5db2dfa DW |
952 | if (!priv->radio_on) { |
953 | ret = -EINVAL; | |
954 | goto out; | |
955 | } | |
956 | ||
52933d81 HS |
957 | if (!netif_running(dev)) { |
958 | ret = -ENETDOWN; | |
959 | goto out; | |
960 | } | |
ffd074fc HS |
961 | |
962 | /* mac80211 does this: | |
963 | struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); | |
52933d81 HS |
964 | if (sdata->type != IEEE80211_IF_TYPE_xxx) { |
965 | ret = -EOPNOTSUPP; | |
966 | goto out; | |
967 | } | |
968 | */ | |
ffd074fc HS |
969 | |
970 | if (wrqu->data.length == sizeof(struct iw_scan_req) && | |
971 | wrqu->data.flags & IW_SCAN_THIS_ESSID) { | |
52933d81 HS |
972 | struct iw_scan_req *req = (struct iw_scan_req *)extra; |
973 | priv->scan_ssid_len = req->essid_len; | |
974 | memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len); | |
975 | lbs_deb_wext("set_scan, essid '%s'\n", | |
9387b7ca | 976 | print_ssid(ssid, priv->scan_ssid, priv->scan_ssid_len)); |
52933d81 HS |
977 | } else { |
978 | priv->scan_ssid_len = 0; | |
ffd074fc | 979 | } |
ffd074fc HS |
980 | |
981 | if (!delayed_work_pending(&priv->scan_work)) | |
982 | queue_delayed_work(priv->work_thread, &priv->scan_work, | |
f137e054 | 983 | msecs_to_jiffies(50)); |
ffd074fc | 984 | /* set marker that currently a scan is taking place */ |
8816edce | 985 | priv->scan_channel = -1; |
ffd074fc | 986 | |
aa21c004 | 987 | if (priv->surpriseremoved) |
52933d81 | 988 | ret = -EIO; |
ffd074fc | 989 | |
52933d81 HS |
990 | out: |
991 | lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret); | |
992 | return ret; | |
ffd074fc HS |
993 | } |
994 | ||
995 | ||
fcdb53db | 996 | /** |
e56188ac | 997 | * @brief Handle Retrieve scan table ioctl |
fcdb53db DW |
998 | * |
999 | * @param dev A pointer to net_device structure | |
1000 | * @param info A pointer to iw_request_info structure | |
1001 | * @param dwrq A pointer to iw_point structure | |
1002 | * @param extra A pointer to extra data buf | |
1003 | * | |
1004 | * @return 0 --success, otherwise fail | |
1005 | */ | |
10078321 | 1006 | int lbs_get_scan(struct net_device *dev, struct iw_request_info *info, |
f137e054 | 1007 | struct iw_point *dwrq, char *extra) |
fcdb53db DW |
1008 | { |
1009 | #define SCAN_ITEM_SIZE 128 | |
ab65f649 | 1010 | struct lbs_private *priv = dev->ml_priv; |
fcdb53db DW |
1011 | int err = 0; |
1012 | char *ev = extra; | |
1013 | char *stop = ev + dwrq->length; | |
f137e054 DW |
1014 | struct bss_descriptor *iter_bss; |
1015 | struct bss_descriptor *safe; | |
876c9d3a | 1016 | |
52933d81 | 1017 | lbs_deb_enter(LBS_DEB_WEXT); |
876c9d3a | 1018 | |
ffd074fc | 1019 | /* iwlist should wait until the current scan is finished */ |
8816edce | 1020 | if (priv->scan_channel) |
ffd074fc HS |
1021 | return -EAGAIN; |
1022 | ||
80e78ef7 | 1023 | /* Update RSSI if current BSS is a locally created ad-hoc BSS */ |
f137e054 | 1024 | if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate) |
10078321 | 1025 | lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0, |
f137e054 | 1026 | CMD_OPTION_WAITFORRSP, 0, NULL); |
80e78ef7 | 1027 | |
aa21c004 DW |
1028 | mutex_lock(&priv->lock); |
1029 | list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) { | |
f137e054 | 1030 | char *next_ev; |
fcdb53db | 1031 | unsigned long stale_time; |
876c9d3a | 1032 | |
fcdb53db DW |
1033 | if (stop - ev < SCAN_ITEM_SIZE) { |
1034 | err = -E2BIG; | |
1035 | break; | |
876c9d3a | 1036 | } |
876c9d3a | 1037 | |
1e838bf3 LCC |
1038 | /* For mesh device, list only mesh networks */ |
1039 | if (dev == priv->mesh_dev && !iter_bss->mesh) | |
1040 | continue; | |
1041 | ||
fcdb53db DW |
1042 | /* Prune old an old scan result */ |
1043 | stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE; | |
1044 | if (time_after(jiffies, stale_time)) { | |
f137e054 | 1045 | list_move_tail(&iter_bss->list, &priv->network_free_list); |
fcdb53db DW |
1046 | clear_bss_descriptor(iter_bss); |
1047 | continue; | |
876c9d3a MT |
1048 | } |
1049 | ||
fcdb53db | 1050 | /* Translate to WE format this entry */ |
ccc58057 | 1051 | next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss); |
fcdb53db DW |
1052 | if (next_ev == NULL) |
1053 | continue; | |
1054 | ev = next_ev; | |
876c9d3a | 1055 | } |
aa21c004 | 1056 | mutex_unlock(&priv->lock); |
876c9d3a | 1057 | |
fcdb53db | 1058 | dwrq->length = (ev - extra); |
876c9d3a MT |
1059 | dwrq->flags = 0; |
1060 | ||
52933d81 | 1061 | lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err); |
fcdb53db | 1062 | return err; |
876c9d3a MT |
1063 | } |
1064 | ||
e56188ac HS |
1065 | |
1066 | ||
1067 | ||
1068 | /*********************************************************************/ | |
1069 | /* */ | |
1070 | /* Command execution */ | |
1071 | /* */ | |
1072 | /*********************************************************************/ | |
1073 | ||
1074 | ||
876c9d3a MT |
1075 | /** |
1076 | * @brief This function handles the command response of scan | |
1077 | * | |
e56188ac HS |
1078 | * Called from handle_cmd_response() in cmdrespc. |
1079 | * | |
876c9d3a MT |
1080 | * The response buffer for the scan command has the following |
1081 | * memory layout: | |
1082 | * | |
1083 | * .-----------------------------------------------------------. | |
1084 | * | header (4 * sizeof(u16)): Standard command response hdr | | |
1085 | * .-----------------------------------------------------------. | |
1086 | * | bufsize (u16) : sizeof the BSS Description data | | |
1087 | * .-----------------------------------------------------------. | |
1088 | * | NumOfSet (u8) : Number of BSS Descs returned | | |
1089 | * .-----------------------------------------------------------. | |
1090 | * | BSSDescription data (variable, size given in bufsize) | | |
1091 | * .-----------------------------------------------------------. | |
1092 | * | TLV data (variable, size calculated using header->size, | | |
1093 | * | bufsize and sizeof the fixed fields above) | | |
1094 | * .-----------------------------------------------------------. | |
1095 | * | |
69f9032d | 1096 | * @param priv A pointer to struct lbs_private structure |
876c9d3a MT |
1097 | * @param resp A pointer to cmd_ds_command |
1098 | * | |
1099 | * @return 0 or -1 | |
1100 | */ | |
fa62f99c DW |
1101 | static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy, |
1102 | struct cmd_header *resp) | |
876c9d3a | 1103 | { |
fa62f99c | 1104 | struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp; |
f137e054 DW |
1105 | struct bss_descriptor *iter_bss; |
1106 | struct bss_descriptor *safe; | |
fa62f99c DW |
1107 | uint8_t *bssinfo; |
1108 | uint16_t scanrespsize; | |
876c9d3a | 1109 | int bytesleft; |
876c9d3a MT |
1110 | int idx; |
1111 | int tlvbufsize; | |
9012b28a | 1112 | int ret; |
876c9d3a | 1113 | |
e56188ac | 1114 | lbs_deb_enter(LBS_DEB_SCAN); |
876c9d3a | 1115 | |
fcdb53db | 1116 | /* Prune old entries from scan table */ |
aa21c004 | 1117 | list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) { |
fcdb53db DW |
1118 | unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE; |
1119 | if (time_before(jiffies, stale_time)) | |
1120 | continue; | |
aa21c004 | 1121 | list_move_tail (&iter_bss->list, &priv->network_free_list); |
fcdb53db DW |
1122 | clear_bss_descriptor(iter_bss); |
1123 | } | |
1124 | ||
fa62f99c DW |
1125 | if (scanresp->nr_sets > MAX_NETWORK_COUNT) { |
1126 | lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n", | |
1127 | scanresp->nr_sets, MAX_NETWORK_COUNT); | |
9012b28a HS |
1128 | ret = -1; |
1129 | goto done; | |
876c9d3a MT |
1130 | } |
1131 | ||
2c5b9e51 | 1132 | bytesleft = get_unaligned_le16(&scanresp->bssdescriptsize); |
9012b28a | 1133 | lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft); |
876c9d3a | 1134 | |
e7240aca | 1135 | scanrespsize = le16_to_cpu(resp->size); |
fa62f99c | 1136 | lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets); |
876c9d3a | 1137 | |
fa62f99c | 1138 | bssinfo = scanresp->bssdesc_and_tlvbuffer; |
876c9d3a MT |
1139 | |
1140 | /* The size of the TLV buffer is equal to the entire command response | |
1141 | * size (scanrespsize) minus the fixed fields (sizeof()'s), the | |
1142 | * BSS Descriptions (bssdescriptsize as bytesLef) and the command | |
1143 | * response header (S_DS_GEN) | |
1144 | */ | |
fa62f99c DW |
1145 | tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize) |
1146 | + sizeof(scanresp->nr_sets) | |
876c9d3a MT |
1147 | + S_DS_GEN); |
1148 | ||
876c9d3a | 1149 | /* |
fa62f99c | 1150 | * Process each scan response returned (scanresp->nr_sets). Save |
876c9d3a MT |
1151 | * the information in the newbssentry and then insert into the |
1152 | * driver scan table either as an update to an existing entry | |
1153 | * or as an addition at the end of the table | |
1154 | */ | |
fa62f99c | 1155 | for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) { |
fcdb53db | 1156 | struct bss_descriptor new; |
fa62f99c DW |
1157 | struct bss_descriptor *found = NULL; |
1158 | struct bss_descriptor *oldest = NULL; | |
876c9d3a MT |
1159 | |
1160 | /* Process the data fields and IEs returned for this BSS */ | |
fcdb53db | 1161 | memset(&new, 0, sizeof (struct bss_descriptor)); |
fa62f99c | 1162 | if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) { |
fcdb53db DW |
1163 | /* error parsing the scan response, skipped */ |
1164 | lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n"); | |
1165 | continue; | |
1166 | } | |
876c9d3a | 1167 | |
fcdb53db | 1168 | /* Try to find this bss in the scan table */ |
aa21c004 | 1169 | list_for_each_entry (iter_bss, &priv->network_list, list) { |
fcdb53db DW |
1170 | if (is_same_network(iter_bss, &new)) { |
1171 | found = iter_bss; | |
1172 | break; | |
876c9d3a MT |
1173 | } |
1174 | ||
fcdb53db DW |
1175 | if ((oldest == NULL) || |
1176 | (iter_bss->last_scanned < oldest->last_scanned)) | |
1177 | oldest = iter_bss; | |
1178 | } | |
876c9d3a | 1179 | |
fcdb53db DW |
1180 | if (found) { |
1181 | /* found, clear it */ | |
1182 | clear_bss_descriptor(found); | |
aa21c004 | 1183 | } else if (!list_empty(&priv->network_free_list)) { |
fcdb53db | 1184 | /* Pull one from the free list */ |
aa21c004 | 1185 | found = list_entry(priv->network_free_list.next, |
fcdb53db | 1186 | struct bss_descriptor, list); |
aa21c004 | 1187 | list_move_tail(&found->list, &priv->network_list); |
fcdb53db DW |
1188 | } else if (oldest) { |
1189 | /* If there are no more slots, expire the oldest */ | |
1190 | found = oldest; | |
1191 | clear_bss_descriptor(found); | |
aa21c004 | 1192 | list_move_tail(&found->list, &priv->network_list); |
876c9d3a | 1193 | } else { |
fcdb53db DW |
1194 | continue; |
1195 | } | |
876c9d3a | 1196 | |
e174961c | 1197 | lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid); |
fcdb53db | 1198 | |
fcdb53db DW |
1199 | /* Copy the locally created newbssentry to the scan table */ |
1200 | memcpy(found, &new, offsetof(struct bss_descriptor, list)); | |
1201 | } | |
876c9d3a | 1202 | |
9012b28a | 1203 | ret = 0; |
876c9d3a | 1204 | |
9012b28a HS |
1205 | done: |
1206 | lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret); | |
1207 | return ret; | |
876c9d3a | 1208 | } |