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.
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>
20 //! Approximate amount of data needed to pass a scan result back to iwlist
21 #define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_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
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 /*********************************************************************/
66 /* Misc helper functions */
68 /*********************************************************************/
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)
82 for (i = 0; i < len; i++)
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));
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,
104 if (ssid1_len != ssid2_len)
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
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 /*********************************************************************/
127 /* Main scanning support */
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
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;
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->region_channel[rgnidx].valid)
166 scanregion = &priv->region_channel[rgnidx];
168 for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
169 struct chanscanparamset *chan = &scanchanlist[chanidx];
171 cfp = scanregion->CFP + nextchan;
173 if (scanregion->band == BAND_B || scanregion->band == BAND_G)
174 chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
176 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
177 chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
178 chan->chanscanmode.passivescan = 1;
180 chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
181 chan->chanscanmode.passivescan = 0;
184 chan->channumber = cfp->channel;
191 * Add SSID TLV of the form:
195 * ssid 4d 4e 54 45 53 54
197 static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv)
199 struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
201 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
202 ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len);
203 memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len);
204 return sizeof(ssid_tlv->header) + priv->scan_ssid_len;
208 * Add CHANLIST TLV of the form
210 * TLV-ID CHANLIST 01 01
212 * channel 1 00 01 00 00 00 64 00
216 * min scan time 00 00
217 * max scan time 64 00
218 * channel 2 00 02 00 00 00 64 00
219 * channel 3 00 03 00 00 00 64 00
220 * channel 4 00 04 00 00 00 64 00
221 * channel 5 00 05 00 00 00 64 00
222 * channel 6 00 06 00 00 00 64 00
223 * channel 7 00 07 00 00 00 64 00
224 * channel 8 00 08 00 00 00 64 00
225 * channel 9 00 09 00 00 00 64 00
226 * channel 10 00 0a 00 00 00 64 00
227 * channel 11 00 0b 00 00 00 64 00
228 * channel 12 00 0c 00 00 00 64 00
229 * channel 13 00 0d 00 00 00 64 00
232 static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
233 struct chanscanparamset *chan_list,
236 size_t size = sizeof(struct chanscanparamset) *chan_count;
237 struct mrvl_ie_chanlist_param_set *chan_tlv = (void *)tlv;
239 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
240 memcpy(chan_tlv->chanscanparam, chan_list, size);
241 chan_tlv->header.len = cpu_to_le16(size);
242 return sizeof(chan_tlv->header) + size;
246 * Add RATES TLV of the form
250 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
252 * The rates are in lbs_bg_rates[], but for the 802.11b
253 * rates the high bit isn't set.
255 static int lbs_scan_add_rates_tlv(uint8_t *tlv)
258 struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
260 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
261 tlv += sizeof(rate_tlv->header);
262 for (i = 0; i < MAX_RATES; i++) {
263 *tlv = lbs_bg_rates[i];
266 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
267 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
268 Note that the values are MBit/s * 2, to mark them as
269 basic rates so that the firmware likes it better */
270 if (*tlv == 0x02 || *tlv == 0x04 ||
271 *tlv == 0x0b || *tlv == 0x16)
275 rate_tlv->header.len = cpu_to_le16(i);
276 return sizeof(rate_tlv->header) + i;
280 * Generate the CMD_802_11_SCAN command with the proper tlv
281 * for a bunch of channels.
283 static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
284 struct chanscanparamset *chan_list, int chan_count)
287 struct cmd_ds_802_11_scan *scan_cmd;
288 uint8_t *tlv; /* pointer into our current, growing TLV storage area */
290 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
291 bsstype, chan_list ? chan_list[0].channumber : -1,
294 /* create the fixed part for scan command */
295 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
296 if (scan_cmd == NULL)
299 tlv = scan_cmd->tlvbuffer;
300 /* TODO: do we need to scan for a specific BSSID?
301 memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
302 scan_cmd->bsstype = bsstype;
305 if (priv->scan_ssid_len)
306 tlv += lbs_scan_add_ssid_tlv(priv, tlv);
307 if (chan_list && chan_count)
308 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
309 tlv += lbs_scan_add_rates_tlv(tlv);
311 /* This is the final data we are about to send */
312 scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
313 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
315 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
316 tlv - scan_cmd->tlvbuffer);
318 ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
319 le16_to_cpu(scan_cmd->hdr.size),
320 lbs_ret_80211_scan, 0);
324 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
329 * @brief Internal function used to start a scan based on an input config
331 * Use the input user scan configuration information when provided in
332 * order to send the appropriate scan commands to firmware to populate or
333 * update the internal driver scan table
335 * @param priv A pointer to struct lbs_private structure
336 * @param full_scan Do a full-scan (blocking)
338 * @return 0 or < 0 if error
340 int lbs_scan_networks(struct lbs_private *priv, int full_scan)
343 struct chanscanparamset *chan_list;
344 struct chanscanparamset *curr_chans;
346 uint8_t bsstype = CMD_BSS_TYPE_ANY;
347 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
348 union iwreq_data wrqu;
349 #ifdef CONFIG_LIBERTAS_DEBUG
350 struct bss_descriptor *iter;
352 DECLARE_SSID_BUF(ssid);
355 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
357 /* Cancel any partial outstanding partial scans if this scan
360 if (full_scan && delayed_work_pending(&priv->scan_work))
361 cancel_delayed_work(&priv->scan_work);
363 /* User-specified bsstype or channel list
364 TODO: this can be implemented if some user-space application
365 need the feature. Formerly, it was accessible from debugfs,
366 but then nowhere used.
368 if (user_cfg->bsstype)
369 bsstype = user_cfg->bsstype;
372 lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
374 /* Create list of channels to scan */
375 chan_list = kzalloc(sizeof(struct chanscanparamset) *
376 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
378 lbs_pr_alert("SCAN: chan_list empty\n");
382 /* We want to scan all channels */
383 chan_count = lbs_scan_create_channel_list(priv, chan_list);
385 netif_stop_queue(priv->dev);
386 netif_carrier_off(priv->dev);
387 if (priv->mesh_dev) {
388 netif_stop_queue(priv->mesh_dev);
389 netif_carrier_off(priv->mesh_dev);
392 /* Prepare to continue an interrupted scan */
393 lbs_deb_scan("chan_count %d, scan_channel %d\n",
394 chan_count, priv->scan_channel);
395 curr_chans = chan_list;
396 /* advance channel list by already-scanned-channels */
397 if (priv->scan_channel > 0) {
398 curr_chans += priv->scan_channel;
399 chan_count -= priv->scan_channel;
402 /* Send scan command(s)
403 * numchannels contains the number of channels we should maximally scan
404 * chan_count is the total number of channels to scan
408 int to_scan = min(numchannels, chan_count);
409 lbs_deb_scan("scanning %d of %d channels\n",
410 to_scan, chan_count);
411 ret = lbs_do_scan(priv, bsstype, curr_chans,
414 lbs_pr_err("SCAN_CMD failed\n");
417 curr_chans += to_scan;
418 chan_count -= to_scan;
420 /* somehow schedule the next part of the scan */
421 if (chan_count && !full_scan &&
422 !priv->surpriseremoved) {
423 /* -1 marks just that we're currently scanning */
424 if (priv->scan_channel < 0)
425 priv->scan_channel = to_scan;
427 priv->scan_channel += to_scan;
428 cancel_delayed_work(&priv->scan_work);
429 queue_delayed_work(priv->work_thread, &priv->scan_work,
430 msecs_to_jiffies(300));
431 /* skip over GIWSCAN event */
436 memset(&wrqu, 0, sizeof(union iwreq_data));
437 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
439 #ifdef CONFIG_LIBERTAS_DEBUG
440 /* Dump the scan table */
441 mutex_lock(&priv->lock);
442 lbs_deb_scan("scan table:\n");
443 list_for_each_entry(iter, &priv->network_list, list)
444 lbs_deb_scan("%02d: BSSID %pM, RSSI %d, SSID '%s'\n",
445 i++, iter->bssid, iter->rssi,
446 print_ssid(ssid, iter->ssid, iter->ssid_len));
447 mutex_unlock(&priv->lock);
451 priv->scan_channel = 0;
454 if (priv->connect_status == LBS_CONNECTED) {
455 netif_carrier_on(priv->dev);
456 if (!priv->tx_pending_len)
457 netif_wake_queue(priv->dev);
459 if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
460 netif_carrier_on(priv->mesh_dev);
461 if (!priv->tx_pending_len)
462 netif_wake_queue(priv->mesh_dev);
466 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
470 void lbs_scan_worker(struct work_struct *work)
472 struct lbs_private *priv =
473 container_of(work, struct lbs_private, scan_work.work);
475 lbs_deb_enter(LBS_DEB_SCAN);
476 lbs_scan_networks(priv, 0);
477 lbs_deb_leave(LBS_DEB_SCAN);
481 /*********************************************************************/
483 /* Result interpretation */
485 /*********************************************************************/
488 * @brief Interpret a BSS scan response returned from the firmware
490 * Parse the various fixed fields and IEs passed back for a a BSS probe
491 * response or beacon from the scan command. Record information as needed
492 * in the scan table struct bss_descriptor for that entry.
494 * @param bss Output parameter: Pointer to the BSS Entry
498 static int lbs_process_bss(struct bss_descriptor *bss,
499 uint8_t **pbeaconinfo, int *bytesleft)
501 struct ieee_ie_fh_param_set *fh;
502 struct ieee_ie_ds_param_set *ds;
503 struct ieee_ie_cf_param_set *cf;
504 struct ieee_ie_ibss_param_set *ibss;
505 DECLARE_SSID_BUF(ssid);
506 struct ieee_ie_country_info_set *pcountryinfo;
507 uint8_t *pos, *end, *p;
508 uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
509 uint16_t beaconsize = 0;
512 lbs_deb_enter(LBS_DEB_SCAN);
514 if (*bytesleft >= sizeof(beaconsize)) {
515 /* Extract & convert beacon size from the command buffer */
516 beaconsize = get_unaligned_le16(*pbeaconinfo);
517 *bytesleft -= sizeof(beaconsize);
518 *pbeaconinfo += sizeof(beaconsize);
521 if (beaconsize == 0 || beaconsize > *bytesleft) {
522 *pbeaconinfo += *bytesleft;
528 /* Initialize the current working beacon pointer for this BSS iteration */
530 end = pos + beaconsize;
532 /* Advance the return beacon pointer past the current beacon */
533 *pbeaconinfo += beaconsize;
534 *bytesleft -= beaconsize;
536 memcpy(bss->bssid, pos, ETH_ALEN);
537 lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid);
540 if ((end - pos) < 12) {
541 lbs_deb_scan("process_bss: Not enough bytes left\n");
547 * next 4 fields are RSSI, time stamp, beacon interval,
548 * and capability information
551 /* RSSI is 1 byte long */
553 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
556 /* time stamp is 8 bytes long */
559 /* beacon interval is 2 bytes long */
560 bss->beaconperiod = get_unaligned_le16(pos);
563 /* capability information is 2 bytes long */
564 bss->capability = get_unaligned_le16(pos);
565 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
568 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
569 lbs_deb_scan("process_bss: WEP enabled\n");
570 if (bss->capability & WLAN_CAPABILITY_IBSS)
571 bss->mode = IW_MODE_ADHOC;
573 bss->mode = IW_MODE_INFRA;
575 /* rest of the current buffer are IE's */
576 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
577 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
579 /* process variable IE */
580 while (pos <= end - 2) {
581 if (pos + pos[1] > end) {
582 lbs_deb_scan("process_bss: error in processing IE, "
583 "bytes left < IE length\n");
589 bss->ssid_len = min_t(int, IEEE80211_MAX_SSID_LEN, pos[1]);
590 memcpy(bss->ssid, pos + 2, bss->ssid_len);
591 lbs_deb_scan("got SSID IE: '%s', len %u\n",
592 print_ssid(ssid, bss->ssid, bss->ssid_len),
596 case WLAN_EID_SUPP_RATES:
597 n_basic_rates = min_t(uint8_t, MAX_RATES, pos[1]);
598 memcpy(bss->rates, pos + 2, n_basic_rates);
600 lbs_deb_scan("got RATES IE\n");
603 case WLAN_EID_FH_PARAMS:
604 fh = (struct ieee_ie_fh_param_set *) pos;
605 memcpy(&bss->phy.fh, fh, sizeof(*fh));
606 lbs_deb_scan("got FH IE\n");
609 case WLAN_EID_DS_PARAMS:
610 ds = (struct ieee_ie_ds_param_set *) pos;
611 bss->channel = ds->channel;
612 memcpy(&bss->phy.ds, ds, sizeof(*ds));
613 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
616 case WLAN_EID_CF_PARAMS:
617 cf = (struct ieee_ie_cf_param_set *) pos;
618 memcpy(&bss->ss.cf, cf, sizeof(*cf));
619 lbs_deb_scan("got CF IE\n");
622 case WLAN_EID_IBSS_PARAMS:
623 ibss = (struct ieee_ie_ibss_param_set *) pos;
624 bss->atimwindow = ibss->atimwindow;
625 memcpy(&bss->ss.ibss, ibss, sizeof(*ibss));
626 lbs_deb_scan("got IBSS IE\n");
629 case WLAN_EID_COUNTRY:
630 pcountryinfo = (struct ieee_ie_country_info_set *) pos;
631 lbs_deb_scan("got COUNTRY IE\n");
632 if (pcountryinfo->header.len < sizeof(pcountryinfo->countrycode)
633 || pcountryinfo->header.len > 254) {
634 lbs_deb_scan("%s: 11D- Err CountryInfo len %d, min %zd, max 254\n",
636 pcountryinfo->header.len,
637 sizeof(pcountryinfo->countrycode));
642 memcpy(&bss->countryinfo, pcountryinfo,
643 pcountryinfo->header.len + 2);
644 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
645 (uint8_t *) pcountryinfo,
646 (int) (pcountryinfo->header.len + 2));
649 case WLAN_EID_EXT_SUPP_RATES:
650 /* only process extended supported rate if data rate is
651 * already found. Data rate IE should come before
652 * extended supported rate IE
654 lbs_deb_scan("got RATESEX IE\n");
655 if (!got_basic_rates) {
656 lbs_deb_scan("... but ignoring it\n");
661 if (n_basic_rates + n_ex_rates > MAX_RATES)
662 n_ex_rates = MAX_RATES - n_basic_rates;
664 p = bss->rates + n_basic_rates;
665 memcpy(p, pos + 2, n_ex_rates);
668 case WLAN_EID_GENERIC:
670 pos[2] == 0x00 && pos[3] == 0x50 &&
671 pos[4] == 0xf2 && pos[5] == 0x01) {
672 bss->wpa_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
673 memcpy(bss->wpa_ie, pos, bss->wpa_ie_len);
674 lbs_deb_scan("got WPA IE\n");
675 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie,
677 } else if (pos[1] >= MARVELL_MESH_IE_LENGTH &&
678 pos[2] == 0x00 && pos[3] == 0x50 &&
679 pos[4] == 0x43 && pos[5] == 0x04) {
680 lbs_deb_scan("got mesh IE\n");
683 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
691 lbs_deb_scan("got RSN IE\n");
692 bss->rsn_ie_len = min(pos[1] + 2, MAX_WPA_IE_LEN);
693 memcpy(bss->rsn_ie, pos, bss->rsn_ie_len);
694 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
695 bss->rsn_ie, bss->rsn_ie_len);
699 lbs_deb_scan("got IE 0x%04x, len %d\n",
708 bss->last_scanned = jiffies;
709 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
714 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
719 * @brief Send a scan command for all available channels filtered on a spec
721 * Used in association code and from debugfs
723 * @param priv A pointer to struct lbs_private structure
724 * @param ssid A pointer to the SSID to scan for
725 * @param ssid_len Length of the SSID
727 * @return 0-success, otherwise fail
729 int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
732 DECLARE_SSID_BUF(ssid_buf);
735 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
736 print_ssid(ssid_buf, ssid, ssid_len));
741 memcpy(priv->scan_ssid, ssid, ssid_len);
742 priv->scan_ssid_len = ssid_len;
744 lbs_scan_networks(priv, 1);
745 if (priv->surpriseremoved) {
751 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
758 /*********************************************************************/
760 /* Support for Wireless Extensions */
762 /*********************************************************************/
765 #define MAX_CUSTOM_LEN 64
767 static inline char *lbs_translate_scan(struct lbs_private *priv,
768 struct iw_request_info *info,
769 char *start, char *stop,
770 struct bss_descriptor *bss)
772 struct chan_freq_power *cfp;
773 char *current_val; /* For rates */
774 struct iw_event iwe; /* Temporary buffer */
776 #define PERFECT_RSSI ((uint8_t)50)
777 #define WORST_RSSI ((uint8_t)0)
778 #define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
781 lbs_deb_enter(LBS_DEB_SCAN);
783 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
785 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
790 /* First entry *MUST* be the BSSID */
792 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
793 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
794 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
797 iwe.cmd = SIOCGIWESSID;
798 iwe.u.data.flags = 1;
799 iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IW_ESSID_MAX_SIZE);
800 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
803 iwe.cmd = SIOCGIWMODE;
804 iwe.u.mode = bss->mode;
805 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
808 iwe.cmd = SIOCGIWFREQ;
809 iwe.u.freq.m = (long)cfp->freq * 100000;
811 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
813 /* Add quality statistics */
815 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
816 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
818 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
820 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
821 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
822 (RSSI_DIFF * RSSI_DIFF);
823 if (iwe.u.qual.qual > 100)
824 iwe.u.qual.qual = 100;
826 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
827 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
829 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
832 /* Locally created ad-hoc BSSs won't have beacons if this is the
833 * only station in the adhoc network; so get signal strength
834 * from receive statistics.
836 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
837 && !lbs_ssid_cmp(priv->curbssparams.ssid,
838 priv->curbssparams.ssid_len,
839 bss->ssid, bss->ssid_len)) {
841 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
842 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
843 iwe.u.qual.level = CAL_RSSI(snr, nf);
845 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
847 /* Add encryption capability */
848 iwe.cmd = SIOCGIWENCODE;
849 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
850 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
852 iwe.u.data.flags = IW_ENCODE_DISABLED;
854 iwe.u.data.length = 0;
855 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
857 current_val = start + iwe_stream_lcp_len(info);
859 iwe.cmd = SIOCGIWRATE;
860 iwe.u.bitrate.fixed = 0;
861 iwe.u.bitrate.disabled = 0;
862 iwe.u.bitrate.value = 0;
864 for (j = 0; j < ARRAY_SIZE(bss->rates) && bss->rates[j]; j++) {
865 /* Bit rate given in 500 kb/s units */
866 iwe.u.bitrate.value = bss->rates[j] * 500000;
867 current_val = iwe_stream_add_value(info, start, current_val,
868 stop, &iwe, IW_EV_PARAM_LEN);
870 if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
871 && !lbs_ssid_cmp(priv->curbssparams.ssid,
872 priv->curbssparams.ssid_len,
873 bss->ssid, bss->ssid_len)) {
874 iwe.u.bitrate.value = 22 * 500000;
875 current_val = iwe_stream_add_value(info, start, current_val,
876 stop, &iwe, IW_EV_PARAM_LEN);
878 /* Check if we added any event */
879 if ((current_val - start) > iwe_stream_lcp_len(info))
882 memset(&iwe, 0, sizeof(iwe));
883 if (bss->wpa_ie_len) {
884 char buf[MAX_WPA_IE_LEN];
885 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
887 iwe.u.data.length = bss->wpa_ie_len;
888 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
891 memset(&iwe, 0, sizeof(iwe));
892 if (bss->rsn_ie_len) {
893 char buf[MAX_WPA_IE_LEN];
894 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
896 iwe.u.data.length = bss->rsn_ie_len;
897 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
901 char custom[MAX_CUSTOM_LEN];
904 iwe.cmd = IWEVCUSTOM;
905 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
906 iwe.u.data.length = p - custom;
907 if (iwe.u.data.length)
908 start = iwe_stream_add_point(info, start, stop,
913 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
919 * @brief Handle Scan Network ioctl
921 * @param dev A pointer to net_device structure
922 * @param info A pointer to iw_request_info structure
923 * @param vwrq A pointer to iw_param structure
924 * @param extra A pointer to extra data buf
926 * @return 0 --success, otherwise fail
928 int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
929 union iwreq_data *wrqu, char *extra)
931 DECLARE_SSID_BUF(ssid);
932 struct lbs_private *priv = dev->ml_priv;
935 lbs_deb_enter(LBS_DEB_WEXT);
937 if (!priv->radio_on) {
942 if (!netif_running(dev)) {
947 /* mac80211 does this:
948 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
949 if (sdata->type != IEEE80211_IF_TYPE_xxx) {
955 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
956 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
957 struct iw_scan_req *req = (struct iw_scan_req *)extra;
958 priv->scan_ssid_len = req->essid_len;
959 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
960 lbs_deb_wext("set_scan, essid '%s'\n",
961 print_ssid(ssid, priv->scan_ssid, priv->scan_ssid_len));
963 priv->scan_ssid_len = 0;
966 if (!delayed_work_pending(&priv->scan_work))
967 queue_delayed_work(priv->work_thread, &priv->scan_work,
968 msecs_to_jiffies(50));
969 /* set marker that currently a scan is taking place */
970 priv->scan_channel = -1;
972 if (priv->surpriseremoved)
976 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
982 * @brief Handle Retrieve scan table ioctl
984 * @param dev A pointer to net_device structure
985 * @param info A pointer to iw_request_info structure
986 * @param dwrq A pointer to iw_point structure
987 * @param extra A pointer to extra data buf
989 * @return 0 --success, otherwise fail
991 int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
992 struct iw_point *dwrq, char *extra)
994 #define SCAN_ITEM_SIZE 128
995 struct lbs_private *priv = dev->ml_priv;
998 char *stop = ev + dwrq->length;
999 struct bss_descriptor *iter_bss;
1000 struct bss_descriptor *safe;
1002 lbs_deb_enter(LBS_DEB_WEXT);
1004 /* iwlist should wait until the current scan is finished */
1005 if (priv->scan_channel)
1008 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
1009 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate) {
1010 err = lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
1011 CMD_OPTION_WAITFORRSP, 0, NULL);
1016 mutex_lock(&priv->lock);
1017 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
1019 unsigned long stale_time;
1021 if (stop - ev < SCAN_ITEM_SIZE) {
1026 /* For mesh device, list only mesh networks */
1027 if (dev == priv->mesh_dev && !iter_bss->mesh)
1030 /* Prune old an old scan result */
1031 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1032 if (time_after(jiffies, stale_time)) {
1033 list_move_tail(&iter_bss->list, &priv->network_free_list);
1034 clear_bss_descriptor(iter_bss);
1038 /* Translate to WE format this entry */
1039 next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss);
1040 if (next_ev == NULL)
1044 mutex_unlock(&priv->lock);
1046 dwrq->length = (ev - extra);
1049 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
1056 /*********************************************************************/
1058 /* Command execution */
1060 /*********************************************************************/
1064 * @brief This function handles the command response of scan
1066 * Called from handle_cmd_response() in cmdrespc.
1068 * The response buffer for the scan command has the following
1071 * .-----------------------------------------------------------.
1072 * | header (4 * sizeof(u16)): Standard command response hdr |
1073 * .-----------------------------------------------------------.
1074 * | bufsize (u16) : sizeof the BSS Description data |
1075 * .-----------------------------------------------------------.
1076 * | NumOfSet (u8) : Number of BSS Descs returned |
1077 * .-----------------------------------------------------------.
1078 * | BSSDescription data (variable, size given in bufsize) |
1079 * .-----------------------------------------------------------.
1080 * | TLV data (variable, size calculated using header->size, |
1081 * | bufsize and sizeof the fixed fields above) |
1082 * .-----------------------------------------------------------.
1084 * @param priv A pointer to struct lbs_private structure
1085 * @param resp A pointer to cmd_ds_command
1089 static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1090 struct cmd_header *resp)
1092 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
1093 struct bss_descriptor *iter_bss;
1094 struct bss_descriptor *safe;
1096 uint16_t scanrespsize;
1102 lbs_deb_enter(LBS_DEB_SCAN);
1104 /* Prune old entries from scan table */
1105 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
1106 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1107 if (time_before(jiffies, stale_time))
1109 list_move_tail (&iter_bss->list, &priv->network_free_list);
1110 clear_bss_descriptor(iter_bss);
1113 if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1114 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1115 scanresp->nr_sets, MAX_NETWORK_COUNT);
1120 bytesleft = get_unaligned_le16(&scanresp->bssdescriptsize);
1121 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
1123 scanrespsize = le16_to_cpu(resp->size);
1124 lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
1126 bssinfo = scanresp->bssdesc_and_tlvbuffer;
1128 /* The size of the TLV buffer is equal to the entire command response
1129 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1130 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1131 * response header (S_DS_GEN)
1133 tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1134 + sizeof(scanresp->nr_sets)
1138 * Process each scan response returned (scanresp->nr_sets). Save
1139 * the information in the newbssentry and then insert into the
1140 * driver scan table either as an update to an existing entry
1141 * or as an addition at the end of the table
1143 for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
1144 struct bss_descriptor new;
1145 struct bss_descriptor *found = NULL;
1146 struct bss_descriptor *oldest = NULL;
1148 /* Process the data fields and IEs returned for this BSS */
1149 memset(&new, 0, sizeof (struct bss_descriptor));
1150 if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
1151 /* error parsing the scan response, skipped */
1152 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1156 /* Try to find this bss in the scan table */
1157 list_for_each_entry (iter_bss, &priv->network_list, list) {
1158 if (is_same_network(iter_bss, &new)) {
1163 if ((oldest == NULL) ||
1164 (iter_bss->last_scanned < oldest->last_scanned))
1169 /* found, clear it */
1170 clear_bss_descriptor(found);
1171 } else if (!list_empty(&priv->network_free_list)) {
1172 /* Pull one from the free list */
1173 found = list_entry(priv->network_free_list.next,
1174 struct bss_descriptor, list);
1175 list_move_tail(&found->list, &priv->network_list);
1176 } else if (oldest) {
1177 /* If there are no more slots, expire the oldest */
1179 clear_bss_descriptor(found);
1180 list_move_tail(&found->list, &priv->network_list);
1185 lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid);
1187 /* Copy the locally created newbssentry to the scan table */
1188 memcpy(found, &new, offsetof(struct bss_descriptor, list));
1194 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);