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/etherdevice.h>
8 #include <asm/unaligned.h>
16 //! Approximate amount of data needed to pass a scan result back to iwlist
17 #define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
24 + 40) /* 40 for WPAIE */
26 //! Memory needed to store a max sized channel List TLV for a firmware scan
27 #define CHAN_TLV_MAX_SIZE (sizeof(struct mrvlietypesheader) \
28 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
29 * sizeof(struct chanscanparamset)))
31 //! Memory needed to store a max number/size SSID TLV for a firmware scan
32 #define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvlietypes_ssidparamset))
34 //! Maximum memory needed for a cmd_ds_802_11_scan with all TLVs at max
35 #define MAX_SCAN_CFG_ALLOC (sizeof(struct cmd_ds_802_11_scan) \
36 + CHAN_TLV_MAX_SIZE + SSID_TLV_MAX_SIZE)
38 //! The maximum number of channels the firmware can scan per command
39 #define MRVDRV_MAX_CHANNELS_PER_SCAN 14
42 * @brief Number of channels to scan per firmware scan command issuance.
44 * Number restricted to prevent hitting the limit on the amount of scan data
45 * returned in a single firmware scan command.
47 #define MRVDRV_CHANNELS_PER_SCAN_CMD 4
49 //! Scan time specified in the channel TLV for each channel for passive scans
50 #define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
52 //! Scan time specified in the channel TLV for each channel for active scans
53 #define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
55 static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
56 struct cmd_header *resp);
58 /*********************************************************************/
60 /* Misc helper functions */
62 /*********************************************************************/
65 * @brief Unsets the MSB on basic rates
67 * Scan through an array and unset the MSB for basic data rates.
69 * @param rates buffer of data rates
70 * @param len size of buffer
72 static void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
76 for (i = 0; i < len; i++)
81 static inline void clear_bss_descriptor(struct bss_descriptor *bss)
83 /* Don't blow away ->list, just BSS data */
84 memset(bss, 0, offsetof(struct bss_descriptor, list));
88 * @brief Compare two SSIDs
90 * @param ssid1 A pointer to ssid to compare
91 * @param ssid2 A pointer to ssid to compare
93 * @return 0: ssid is same, otherwise is different
95 int lbs_ssid_cmp(uint8_t *ssid1, uint8_t ssid1_len, uint8_t *ssid2,
98 if (ssid1_len != ssid2_len)
101 return memcmp(ssid1, ssid2, ssid1_len);
104 static inline int is_same_network(struct bss_descriptor *src,
105 struct bss_descriptor *dst)
107 /* A network is only a duplicate if the channel, BSSID, and ESSID
108 * all match. We treat all <hidden> with the same BSSID and channel
110 return ((src->ssid_len == dst->ssid_len) &&
111 (src->channel == dst->channel) &&
112 !compare_ether_addr(src->bssid, dst->bssid) &&
113 !memcmp(src->ssid, dst->ssid, src->ssid_len));
119 /*********************************************************************/
121 /* Main scanning support */
123 /*********************************************************************/
126 * @brief Create a channel list for the driver to scan based on region info
128 * Only used from lbs_scan_setup_scan_config()
130 * Use the driver region/band information to construct a comprehensive list
131 * of channels to scan. This routine is used for any scan that is not
132 * provided a specific channel list to scan.
134 * @param priv A pointer to struct lbs_private structure
135 * @param scanchanlist Output parameter: resulting channel list to scan
139 static int lbs_scan_create_channel_list(struct lbs_private *priv,
140 struct chanscanparamset *scanchanlist)
142 struct region_channel *scanregion;
143 struct chan_freq_power *cfp;
151 /* Set the default scan type to the user specified type, will later
152 * be changed to passive on a per channel basis if restricted by
153 * regulatory requirements (11d or 11h)
155 scantype = CMD_SCAN_TYPE_ACTIVE;
157 for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
158 if (priv->enable11d && (priv->connect_status != LBS_CONNECTED)
159 && (priv->mesh_connect_status != LBS_CONNECTED)) {
160 /* Scan all the supported chan for the first scan */
161 if (!priv->universal_channel[rgnidx].valid)
163 scanregion = &priv->universal_channel[rgnidx];
165 /* clear the parsed_region_chan for the first scan */
166 memset(&priv->parsed_region_chan, 0x00,
167 sizeof(priv->parsed_region_chan));
169 if (!priv->region_channel[rgnidx].valid)
171 scanregion = &priv->region_channel[rgnidx];
174 for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
175 struct chanscanparamset *chan = &scanchanlist[chanidx];
177 cfp = scanregion->CFP + nextchan;
180 scantype = lbs_get_scan_type_11d(cfp->channel,
181 &priv->parsed_region_chan);
183 if (scanregion->band == BAND_B || scanregion->band == BAND_G)
184 chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
186 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
187 chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
188 chan->chanscanmode.passivescan = 1;
190 chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
191 chan->chanscanmode.passivescan = 0;
194 chan->channumber = cfp->channel;
201 * Add SSID TLV of the form:
205 * ssid 4d 4e 54 45 53 54
207 static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv)
209 struct mrvlietypes_ssidparamset *ssid_tlv = (void *)tlv;
211 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
212 ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len);
213 memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len);
214 return sizeof(ssid_tlv->header) + priv->scan_ssid_len;
218 * Add CHANLIST TLV of the form
220 * TLV-ID CHANLIST 01 01
222 * channel 1 00 01 00 00 00 64 00
226 * min scan time 00 00
227 * max scan time 64 00
228 * channel 2 00 02 00 00 00 64 00
229 * channel 3 00 03 00 00 00 64 00
230 * channel 4 00 04 00 00 00 64 00
231 * channel 5 00 05 00 00 00 64 00
232 * channel 6 00 06 00 00 00 64 00
233 * channel 7 00 07 00 00 00 64 00
234 * channel 8 00 08 00 00 00 64 00
235 * channel 9 00 09 00 00 00 64 00
236 * channel 10 00 0a 00 00 00 64 00
237 * channel 11 00 0b 00 00 00 64 00
238 * channel 12 00 0c 00 00 00 64 00
239 * channel 13 00 0d 00 00 00 64 00
242 static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
243 struct chanscanparamset *chan_list,
246 size_t size = sizeof(struct chanscanparamset) *chan_count;
247 struct mrvlietypes_chanlistparamset *chan_tlv = (void *)tlv;
249 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
250 memcpy(chan_tlv->chanscanparam, chan_list, size);
251 chan_tlv->header.len = cpu_to_le16(size);
252 return sizeof(chan_tlv->header) + size;
256 * Add RATES TLV of the form
260 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
262 * The rates are in lbs_bg_rates[], but for the 802.11b
263 * rates the high bit isn't set.
265 static int lbs_scan_add_rates_tlv(uint8_t *tlv)
268 struct mrvlietypes_ratesparamset *rate_tlv = (void *)tlv;
270 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
271 tlv += sizeof(rate_tlv->header);
272 for (i = 0; i < MAX_RATES; i++) {
273 *tlv = lbs_bg_rates[i];
276 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
277 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
278 Note that the values are MBit/s * 2, to mark them as
279 basic rates so that the firmware likes it better */
280 if (*tlv == 0x02 || *tlv == 0x04 ||
281 *tlv == 0x0b || *tlv == 0x16)
285 rate_tlv->header.len = cpu_to_le16(i);
286 return sizeof(rate_tlv->header) + i;
290 * Generate the CMD_802_11_SCAN command with the proper tlv
291 * for a bunch of channels.
293 static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
294 struct chanscanparamset *chan_list, int chan_count)
297 struct cmd_ds_802_11_scan *scan_cmd;
298 uint8_t *tlv; /* pointer into our current, growing TLV storage area */
300 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
301 bsstype, chan_list[0].channumber, chan_count);
303 /* create the fixed part for scan command */
304 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
305 if (scan_cmd == NULL)
308 tlv = scan_cmd->tlvbuffer;
309 /* TODO: do we need to scan for a specific BSSID?
310 memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
311 scan_cmd->bsstype = bsstype;
314 if (priv->scan_ssid_len)
315 tlv += lbs_scan_add_ssid_tlv(priv, tlv);
316 if (chan_list && chan_count)
317 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
318 tlv += lbs_scan_add_rates_tlv(tlv);
320 /* This is the final data we are about to send */
321 scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
322 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
324 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
325 tlv - scan_cmd->tlvbuffer);
327 ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
328 le16_to_cpu(scan_cmd->hdr.size),
329 lbs_ret_80211_scan, 0);
333 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
338 * @brief Internal function used to start a scan based on an input config
340 * Use the input user scan configuration information when provided in
341 * order to send the appropriate scan commands to firmware to populate or
342 * update the internal driver scan table
344 * @param priv A pointer to struct lbs_private structure
345 * @param full_scan Do a full-scan (blocking)
347 * @return 0 or < 0 if error
349 int lbs_scan_networks(struct lbs_private *priv, int full_scan)
352 struct chanscanparamset *chan_list;
353 struct chanscanparamset *curr_chans;
355 uint8_t bsstype = CMD_BSS_TYPE_ANY;
356 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
357 union iwreq_data wrqu;
358 #ifdef CONFIG_LIBERTAS_DEBUG
359 struct bss_descriptor *iter;
361 DECLARE_MAC_BUF(mac);
364 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
366 /* Cancel any partial outstanding partial scans if this scan
369 if (full_scan && delayed_work_pending(&priv->scan_work))
370 cancel_delayed_work(&priv->scan_work);
372 /* User-specified bsstype or channel list
373 TODO: this can be implemented if some user-space application
374 need the feature. Formerly, it was accessible from debugfs,
375 but then nowhere used.
377 if (user_cfg->bsstype)
378 bsstype = user_cfg->bsstype;
381 lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
383 /* Create list of channels to scan */
384 chan_list = kzalloc(sizeof(struct chanscanparamset) *
385 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
387 lbs_pr_alert("SCAN: chan_list empty\n");
391 /* We want to scan all channels */
392 chan_count = lbs_scan_create_channel_list(priv, chan_list);
394 netif_stop_queue(priv->dev);
395 netif_carrier_off(priv->dev);
396 if (priv->mesh_dev) {
397 netif_stop_queue(priv->mesh_dev);
398 netif_carrier_off(priv->mesh_dev);
401 /* Prepare to continue an interrupted scan */
402 lbs_deb_scan("chan_count %d, scan_channel %d\n",
403 chan_count, priv->scan_channel);
404 curr_chans = chan_list;
405 /* advance channel list by already-scanned-channels */
406 if (priv->scan_channel > 0) {
407 curr_chans += priv->scan_channel;
408 chan_count -= priv->scan_channel;
411 /* Send scan command(s)
412 * numchannels contains the number of channels we should maximally scan
413 * chan_count is the total number of channels to scan
417 int to_scan = min(numchannels, chan_count);
418 lbs_deb_scan("scanning %d of %d channels\n",
419 to_scan, chan_count);
420 ret = lbs_do_scan(priv, bsstype, curr_chans,
423 lbs_pr_err("SCAN_CMD failed\n");
426 curr_chans += to_scan;
427 chan_count -= to_scan;
429 /* somehow schedule the next part of the scan */
430 if (chan_count && !full_scan &&
431 !priv->surpriseremoved) {
432 /* -1 marks just that we're currently scanning */
433 if (priv->scan_channel < 0)
434 priv->scan_channel = to_scan;
436 priv->scan_channel += to_scan;
437 cancel_delayed_work(&priv->scan_work);
438 queue_delayed_work(priv->work_thread, &priv->scan_work,
439 msecs_to_jiffies(300));
440 /* skip over GIWSCAN event */
445 memset(&wrqu, 0, sizeof(union iwreq_data));
446 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
448 #ifdef CONFIG_LIBERTAS_DEBUG
449 /* Dump the scan table */
450 mutex_lock(&priv->lock);
451 lbs_deb_scan("scan table:\n");
452 list_for_each_entry(iter, &priv->network_list, list)
453 lbs_deb_scan("%02d: BSSID %s, RSSI %d, SSID '%s'\n",
454 i++, print_mac(mac, iter->bssid), iter->rssi,
455 escape_essid(iter->ssid, iter->ssid_len));
456 mutex_unlock(&priv->lock);
460 priv->scan_channel = 0;
463 if (priv->connect_status == LBS_CONNECTED) {
464 netif_carrier_on(priv->dev);
465 if (!priv->tx_pending_len)
466 netif_wake_queue(priv->dev);
468 if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
469 netif_carrier_on(priv->mesh_dev);
470 if (!priv->tx_pending_len)
471 netif_wake_queue(priv->mesh_dev);
475 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
479 void lbs_scan_worker(struct work_struct *work)
481 struct lbs_private *priv =
482 container_of(work, struct lbs_private, scan_work.work);
484 lbs_deb_enter(LBS_DEB_SCAN);
485 lbs_scan_networks(priv, 0);
486 lbs_deb_leave(LBS_DEB_SCAN);
490 /*********************************************************************/
492 /* Result interpretation */
494 /*********************************************************************/
497 * @brief Interpret a BSS scan response returned from the firmware
499 * Parse the various fixed fields and IEs passed back for a a BSS probe
500 * response or beacon from the scan command. Record information as needed
501 * in the scan table struct bss_descriptor for that entry.
503 * @param bss Output parameter: Pointer to the BSS Entry
507 static int lbs_process_bss(struct bss_descriptor *bss,
508 uint8_t **pbeaconinfo, int *bytesleft)
510 struct ieeetypes_fhparamset *pFH;
511 struct ieeetypes_dsparamset *pDS;
512 struct ieeetypes_cfparamset *pCF;
513 struct ieeetypes_ibssparamset *pibss;
514 DECLARE_MAC_BUF(mac);
515 struct ieeetypes_countryinfoset *pcountryinfo;
516 uint8_t *pos, *end, *p;
517 uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
518 uint16_t beaconsize = 0;
521 lbs_deb_enter(LBS_DEB_SCAN);
523 if (*bytesleft >= sizeof(beaconsize)) {
524 /* Extract & convert beacon size from the command buffer */
525 beaconsize = le16_to_cpu(get_unaligned((__le16 *)*pbeaconinfo));
526 *bytesleft -= sizeof(beaconsize);
527 *pbeaconinfo += sizeof(beaconsize);
530 if (beaconsize == 0 || beaconsize > *bytesleft) {
531 *pbeaconinfo += *bytesleft;
537 /* Initialize the current working beacon pointer for this BSS iteration */
539 end = pos + beaconsize;
541 /* Advance the return beacon pointer past the current beacon */
542 *pbeaconinfo += beaconsize;
543 *bytesleft -= beaconsize;
545 memcpy(bss->bssid, pos, ETH_ALEN);
546 lbs_deb_scan("process_bss: BSSID %s\n", print_mac(mac, bss->bssid));
549 if ((end - pos) < 12) {
550 lbs_deb_scan("process_bss: Not enough bytes left\n");
556 * next 4 fields are RSSI, time stamp, beacon interval,
557 * and capability information
560 /* RSSI is 1 byte long */
562 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
565 /* time stamp is 8 bytes long */
568 /* beacon interval is 2 bytes long */
569 bss->beaconperiod = le16_to_cpup((void *) pos);
572 /* capability information is 2 bytes long */
573 bss->capability = le16_to_cpup((void *) pos);
574 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
577 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
578 lbs_deb_scan("process_bss: WEP enabled\n");
579 if (bss->capability & WLAN_CAPABILITY_IBSS)
580 bss->mode = IW_MODE_ADHOC;
582 bss->mode = IW_MODE_INFRA;
584 /* rest of the current buffer are IE's */
585 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
586 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
588 /* process variable IE */
589 while (pos <= end - 2) {
590 struct ieee80211_info_element * elem = (void *)pos;
592 if (pos + elem->len > end) {
593 lbs_deb_scan("process_bss: error in processing IE, "
594 "bytes left < IE length\n");
600 bss->ssid_len = elem->len;
601 memcpy(bss->ssid, elem->data, elem->len);
602 lbs_deb_scan("got SSID IE: '%s', len %u\n",
603 escape_essid(bss->ssid, bss->ssid_len),
607 case MFIE_TYPE_RATES:
608 n_basic_rates = min_t(uint8_t, MAX_RATES, elem->len);
609 memcpy(bss->rates, elem->data, n_basic_rates);
611 lbs_deb_scan("got RATES IE\n");
614 case MFIE_TYPE_FH_SET:
615 pFH = (struct ieeetypes_fhparamset *) pos;
616 memmove(&bss->phyparamset.fhparamset, pFH,
617 sizeof(struct ieeetypes_fhparamset));
618 lbs_deb_scan("got FH IE\n");
621 case MFIE_TYPE_DS_SET:
622 pDS = (struct ieeetypes_dsparamset *) pos;
623 bss->channel = pDS->currentchan;
624 memcpy(&bss->phyparamset.dsparamset, pDS,
625 sizeof(struct ieeetypes_dsparamset));
626 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
629 case MFIE_TYPE_CF_SET:
630 pCF = (struct ieeetypes_cfparamset *) pos;
631 memcpy(&bss->ssparamset.cfparamset, pCF,
632 sizeof(struct ieeetypes_cfparamset));
633 lbs_deb_scan("got CF IE\n");
636 case MFIE_TYPE_IBSS_SET:
637 pibss = (struct ieeetypes_ibssparamset *) pos;
638 bss->atimwindow = le16_to_cpu(pibss->atimwindow);
639 memmove(&bss->ssparamset.ibssparamset, pibss,
640 sizeof(struct ieeetypes_ibssparamset));
641 lbs_deb_scan("got IBSS IE\n");
644 case MFIE_TYPE_COUNTRY:
645 pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
646 lbs_deb_scan("got COUNTRY IE\n");
647 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
648 || pcountryinfo->len > 254) {
649 lbs_deb_scan("process_bss: 11D- Err CountryInfo len %d, min %zd, max 254\n",
650 pcountryinfo->len, sizeof(pcountryinfo->countrycode));
655 memcpy(&bss->countryinfo, pcountryinfo, pcountryinfo->len + 2);
656 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
657 (uint8_t *) pcountryinfo,
658 (int) (pcountryinfo->len + 2));
661 case MFIE_TYPE_RATES_EX:
662 /* only process extended supported rate if data rate is
663 * already found. Data rate IE should come before
664 * extended supported rate IE
666 lbs_deb_scan("got RATESEX IE\n");
667 if (!got_basic_rates) {
668 lbs_deb_scan("... but ignoring it\n");
672 n_ex_rates = elem->len;
673 if (n_basic_rates + n_ex_rates > MAX_RATES)
674 n_ex_rates = MAX_RATES - n_basic_rates;
676 p = bss->rates + n_basic_rates;
677 memcpy(p, elem->data, n_ex_rates);
680 case MFIE_TYPE_GENERIC:
681 if (elem->len >= 4 &&
682 elem->data[0] == 0x00 && elem->data[1] == 0x50 &&
683 elem->data[2] == 0xf2 && elem->data[3] == 0x01) {
684 bss->wpa_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
685 memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
686 lbs_deb_scan("got WPA IE\n");
687 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie, elem->len);
688 } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
689 elem->data[0] == 0x00 && elem->data[1] == 0x50 &&
690 elem->data[2] == 0x43 && elem->data[3] == 0x04) {
691 lbs_deb_scan("got mesh IE\n");
694 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
695 elem->data[0], elem->data[1],
696 elem->data[2], elem->data[3],
702 lbs_deb_scan("got RSN IE\n");
703 bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
704 memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
705 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
706 bss->rsn_ie, elem->len);
710 lbs_deb_scan("got IE 0x%04x, len %d\n",
711 elem->id, elem->len);
715 pos += elem->len + 2;
719 bss->last_scanned = jiffies;
720 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
725 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
730 * @brief Send a scan command for all available channels filtered on a spec
732 * Used in association code and from debugfs
734 * @param priv A pointer to struct lbs_private structure
735 * @param ssid A pointer to the SSID to scan for
736 * @param ssid_len Length of the SSID
738 * @return 0-success, otherwise fail
740 int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
745 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
746 escape_essid(ssid, ssid_len));
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) {
761 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
768 /*********************************************************************/
770 /* Support for Wireless Extensions */
772 /*********************************************************************/
775 #define MAX_CUSTOM_LEN 64
777 static inline char *lbs_translate_scan(struct lbs_private *priv,
778 char *start, char *stop,
779 struct bss_descriptor *bss)
781 struct chan_freq_power *cfp;
782 char *current_val; /* For rates */
783 struct iw_event iwe; /* Temporary buffer */
785 #define PERFECT_RSSI ((uint8_t)50)
786 #define WORST_RSSI ((uint8_t)0)
787 #define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
790 lbs_deb_enter(LBS_DEB_SCAN);
792 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
794 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
799 /* First entry *MUST* be the BSSID */
801 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
802 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
803 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
806 iwe.cmd = SIOCGIWESSID;
807 iwe.u.data.flags = 1;
808 iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IW_ESSID_MAX_SIZE);
809 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
812 iwe.cmd = SIOCGIWMODE;
813 iwe.u.mode = bss->mode;
814 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
817 iwe.cmd = SIOCGIWFREQ;
818 iwe.u.freq.m = (long)cfp->freq * 100000;
820 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
822 /* Add quality statistics */
824 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
825 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
827 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
829 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
830 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
831 (RSSI_DIFF * RSSI_DIFF);
832 if (iwe.u.qual.qual > 100)
833 iwe.u.qual.qual = 100;
835 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
836 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
838 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
841 /* Locally created ad-hoc BSSs won't have beacons if this is the
842 * only station in the adhoc network; so get signal strength
843 * from receive statistics.
845 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
846 && !lbs_ssid_cmp(priv->curbssparams.ssid,
847 priv->curbssparams.ssid_len,
848 bss->ssid, bss->ssid_len)) {
850 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
851 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
852 iwe.u.qual.level = CAL_RSSI(snr, nf);
854 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
856 /* Add encryption capability */
857 iwe.cmd = SIOCGIWENCODE;
858 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
859 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
861 iwe.u.data.flags = IW_ENCODE_DISABLED;
863 iwe.u.data.length = 0;
864 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
866 current_val = start + IW_EV_LCP_LEN;
868 iwe.cmd = SIOCGIWRATE;
869 iwe.u.bitrate.fixed = 0;
870 iwe.u.bitrate.disabled = 0;
871 iwe.u.bitrate.value = 0;
873 for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
874 /* Bit rate given in 500 kb/s units */
875 iwe.u.bitrate.value = bss->rates[j] * 500000;
876 current_val = iwe_stream_add_value(start, current_val,
877 stop, &iwe, IW_EV_PARAM_LEN);
879 if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
880 && !lbs_ssid_cmp(priv->curbssparams.ssid,
881 priv->curbssparams.ssid_len,
882 bss->ssid, bss->ssid_len)) {
883 iwe.u.bitrate.value = 22 * 500000;
884 current_val = iwe_stream_add_value(start, current_val,
885 stop, &iwe, IW_EV_PARAM_LEN);
887 /* Check if we added any event */
888 if((current_val - start) > IW_EV_LCP_LEN)
891 memset(&iwe, 0, sizeof(iwe));
892 if (bss->wpa_ie_len) {
893 char buf[MAX_WPA_IE_LEN];
894 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
896 iwe.u.data.length = bss->wpa_ie_len;
897 start = iwe_stream_add_point(start, stop, &iwe, buf);
900 memset(&iwe, 0, sizeof(iwe));
901 if (bss->rsn_ie_len) {
902 char buf[MAX_WPA_IE_LEN];
903 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
905 iwe.u.data.length = bss->rsn_ie_len;
906 start = iwe_stream_add_point(start, stop, &iwe, buf);
910 char custom[MAX_CUSTOM_LEN];
913 iwe.cmd = IWEVCUSTOM;
914 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
915 iwe.u.data.length = p - custom;
916 if (iwe.u.data.length)
917 start = iwe_stream_add_point(start, stop, &iwe, custom);
921 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
927 * @brief Handle Scan Network ioctl
929 * @param dev A pointer to net_device structure
930 * @param info A pointer to iw_request_info structure
931 * @param vwrq A pointer to iw_param structure
932 * @param extra A pointer to extra data buf
934 * @return 0 --success, otherwise fail
936 int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
937 union iwreq_data *wrqu, char *extra)
939 struct lbs_private *priv = dev->priv;
942 lbs_deb_enter(LBS_DEB_WEXT);
944 if (!netif_running(dev)) {
949 /* mac80211 does this:
950 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
951 if (sdata->type != IEEE80211_IF_TYPE_xxx) {
957 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
958 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
959 struct iw_scan_req *req = (struct iw_scan_req *)extra;
960 priv->scan_ssid_len = req->essid_len;
961 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
962 lbs_deb_wext("set_scan, essid '%s'\n",
963 escape_essid(priv->scan_ssid, priv->scan_ssid_len));
965 priv->scan_ssid_len = 0;
968 if (!delayed_work_pending(&priv->scan_work))
969 queue_delayed_work(priv->work_thread, &priv->scan_work,
970 msecs_to_jiffies(50));
971 /* set marker that currently a scan is taking place */
972 priv->scan_channel = -1;
974 if (priv->surpriseremoved)
978 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
984 * @brief Handle Retrieve scan table ioctl
986 * @param dev A pointer to net_device structure
987 * @param info A pointer to iw_request_info structure
988 * @param dwrq A pointer to iw_point structure
989 * @param extra A pointer to extra data buf
991 * @return 0 --success, otherwise fail
993 int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
994 struct iw_point *dwrq, char *extra)
996 #define SCAN_ITEM_SIZE 128
997 struct lbs_private *priv = dev->priv;
1000 char *stop = ev + dwrq->length;
1001 struct bss_descriptor *iter_bss;
1002 struct bss_descriptor *safe;
1004 lbs_deb_enter(LBS_DEB_WEXT);
1006 /* iwlist should wait until the current scan is finished */
1007 if (priv->scan_channel)
1010 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
1011 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate)
1012 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
1013 CMD_OPTION_WAITFORRSP, 0, NULL);
1015 mutex_lock(&priv->lock);
1016 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
1018 unsigned long stale_time;
1020 if (stop - ev < SCAN_ITEM_SIZE) {
1025 /* For mesh device, list only mesh networks */
1026 if (dev == priv->mesh_dev && !iter_bss->mesh)
1029 /* Prune old an old scan result */
1030 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1031 if (time_after(jiffies, stale_time)) {
1032 list_move_tail(&iter_bss->list, &priv->network_free_list);
1033 clear_bss_descriptor(iter_bss);
1037 /* Translate to WE format this entry */
1038 next_ev = lbs_translate_scan(priv, ev, stop, iter_bss);
1039 if (next_ev == NULL)
1043 mutex_unlock(&priv->lock);
1045 dwrq->length = (ev - extra);
1048 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
1055 /*********************************************************************/
1057 /* Command execution */
1059 /*********************************************************************/
1063 * @brief This function handles the command response of scan
1065 * Called from handle_cmd_response() in cmdrespc.
1067 * The response buffer for the scan command has the following
1070 * .-----------------------------------------------------------.
1071 * | header (4 * sizeof(u16)): Standard command response hdr |
1072 * .-----------------------------------------------------------.
1073 * | bufsize (u16) : sizeof the BSS Description data |
1074 * .-----------------------------------------------------------.
1075 * | NumOfSet (u8) : Number of BSS Descs returned |
1076 * .-----------------------------------------------------------.
1077 * | BSSDescription data (variable, size given in bufsize) |
1078 * .-----------------------------------------------------------.
1079 * | TLV data (variable, size calculated using header->size, |
1080 * | bufsize and sizeof the fixed fields above) |
1081 * .-----------------------------------------------------------.
1083 * @param priv A pointer to struct lbs_private structure
1084 * @param resp A pointer to cmd_ds_command
1088 static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1089 struct cmd_header *resp)
1091 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
1092 struct bss_descriptor *iter_bss;
1093 struct bss_descriptor *safe;
1095 uint16_t scanrespsize;
1101 lbs_deb_enter(LBS_DEB_SCAN);
1103 /* Prune old entries from scan table */
1104 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
1105 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1106 if (time_before(jiffies, stale_time))
1108 list_move_tail (&iter_bss->list, &priv->network_free_list);
1109 clear_bss_descriptor(iter_bss);
1112 if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1113 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1114 scanresp->nr_sets, MAX_NETWORK_COUNT);
1119 bytesleft = le16_to_cpu(scanresp->bssdescriptsize);
1120 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
1122 scanrespsize = le16_to_cpu(resp->size);
1123 lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
1125 bssinfo = scanresp->bssdesc_and_tlvbuffer;
1127 /* The size of the TLV buffer is equal to the entire command response
1128 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1129 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1130 * response header (S_DS_GEN)
1132 tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1133 + sizeof(scanresp->nr_sets)
1137 * Process each scan response returned (scanresp->nr_sets). Save
1138 * the information in the newbssentry and then insert into the
1139 * driver scan table either as an update to an existing entry
1140 * or as an addition at the end of the table
1142 for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
1143 struct bss_descriptor new;
1144 struct bss_descriptor *found = NULL;
1145 struct bss_descriptor *oldest = NULL;
1146 DECLARE_MAC_BUF(mac);
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 %s\n", print_mac(mac, 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);