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