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/etherdevice.h>
9 #include <asm/unaligned.h>
11 #include <net/lib80211.h>
19 //! Approximate amount of data needed to pass a scan result back to iwlist
20 #define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
27 + 40) /* 40 for WPAIE */
29 //! Memory needed to store a max sized channel List TLV for a firmware scan
30 #define CHAN_TLV_MAX_SIZE (sizeof(struct mrvlietypesheader) \
31 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
32 * sizeof(struct chanscanparamset)))
34 //! Memory needed to store a max number/size SSID TLV for a firmware scan
35 #define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvlietypes_ssidparamset))
37 //! Maximum memory needed for a cmd_ds_802_11_scan with all TLVs at max
38 #define MAX_SCAN_CFG_ALLOC (sizeof(struct cmd_ds_802_11_scan) \
39 + CHAN_TLV_MAX_SIZE + SSID_TLV_MAX_SIZE)
41 //! The maximum number of channels the firmware can scan per command
42 #define MRVDRV_MAX_CHANNELS_PER_SCAN 14
45 * @brief Number of channels to scan per firmware scan command issuance.
47 * Number restricted to prevent hitting the limit on the amount of scan data
48 * returned in a single firmware scan command.
50 #define MRVDRV_CHANNELS_PER_SCAN_CMD 4
52 //! Scan time specified in the channel TLV for each channel for passive scans
53 #define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
55 //! Scan time specified in the channel TLV for each channel for active scans
56 #define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
58 static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
59 struct cmd_header *resp);
61 /*********************************************************************/
63 /* Misc helper functions */
65 /*********************************************************************/
68 * @brief Unsets the MSB on basic rates
70 * Scan through an array and unset the MSB for basic data rates.
72 * @param rates buffer of data rates
73 * @param len size of buffer
75 static void lbs_unset_basic_rate_flags(u8 *rates, size_t len)
79 for (i = 0; i < len; i++)
84 static inline void clear_bss_descriptor(struct bss_descriptor *bss)
86 /* Don't blow away ->list, just BSS data */
87 memset(bss, 0, offsetof(struct bss_descriptor, list));
91 * @brief Compare two SSIDs
93 * @param ssid1 A pointer to ssid to compare
94 * @param ssid2 A pointer to ssid to compare
96 * @return 0: ssid is same, otherwise is different
98 int lbs_ssid_cmp(uint8_t *ssid1, uint8_t ssid1_len, uint8_t *ssid2,
101 if (ssid1_len != ssid2_len)
104 return memcmp(ssid1, ssid2, ssid1_len);
107 static inline int is_same_network(struct bss_descriptor *src,
108 struct bss_descriptor *dst)
110 /* A network is only a duplicate if the channel, BSSID, and ESSID
111 * all match. We treat all <hidden> with the same BSSID and channel
113 return ((src->ssid_len == dst->ssid_len) &&
114 (src->channel == dst->channel) &&
115 !compare_ether_addr(src->bssid, dst->bssid) &&
116 !memcmp(src->ssid, dst->ssid, src->ssid_len));
122 /*********************************************************************/
124 /* Main scanning support */
126 /*********************************************************************/
129 * @brief Create a channel list for the driver to scan based on region info
131 * Only used from lbs_scan_setup_scan_config()
133 * Use the driver region/band information to construct a comprehensive list
134 * of channels to scan. This routine is used for any scan that is not
135 * provided a specific channel list to scan.
137 * @param priv A pointer to struct lbs_private structure
138 * @param scanchanlist Output parameter: resulting channel list to scan
142 static int lbs_scan_create_channel_list(struct lbs_private *priv,
143 struct chanscanparamset *scanchanlist)
145 struct region_channel *scanregion;
146 struct chan_freq_power *cfp;
154 /* Set the default scan type to the user specified type, will later
155 * be changed to passive on a per channel basis if restricted by
156 * regulatory requirements (11d or 11h)
158 scantype = CMD_SCAN_TYPE_ACTIVE;
160 for (rgnidx = 0; rgnidx < ARRAY_SIZE(priv->region_channel); rgnidx++) {
161 if (priv->enable11d && (priv->connect_status != LBS_CONNECTED)
162 && (priv->mesh_connect_status != LBS_CONNECTED)) {
163 /* Scan all the supported chan for the first scan */
164 if (!priv->universal_channel[rgnidx].valid)
166 scanregion = &priv->universal_channel[rgnidx];
168 /* clear the parsed_region_chan for the first scan */
169 memset(&priv->parsed_region_chan, 0x00,
170 sizeof(priv->parsed_region_chan));
172 if (!priv->region_channel[rgnidx].valid)
174 scanregion = &priv->region_channel[rgnidx];
177 for (nextchan = 0; nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
178 struct chanscanparamset *chan = &scanchanlist[chanidx];
180 cfp = scanregion->CFP + nextchan;
183 scantype = lbs_get_scan_type_11d(cfp->channel,
184 &priv->parsed_region_chan);
186 if (scanregion->band == BAND_B || scanregion->band == BAND_G)
187 chan->radiotype = CMD_SCAN_RADIO_TYPE_BG;
189 if (scantype == CMD_SCAN_TYPE_PASSIVE) {
190 chan->maxscantime = cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
191 chan->chanscanmode.passivescan = 1;
193 chan->maxscantime = cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
194 chan->chanscanmode.passivescan = 0;
197 chan->channumber = cfp->channel;
204 * Add SSID TLV of the form:
208 * ssid 4d 4e 54 45 53 54
210 static int lbs_scan_add_ssid_tlv(struct lbs_private *priv, u8 *tlv)
212 struct mrvlietypes_ssidparamset *ssid_tlv = (void *)tlv;
214 ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
215 ssid_tlv->header.len = cpu_to_le16(priv->scan_ssid_len);
216 memcpy(ssid_tlv->ssid, priv->scan_ssid, priv->scan_ssid_len);
217 return sizeof(ssid_tlv->header) + priv->scan_ssid_len;
221 * Add CHANLIST TLV of the form
223 * TLV-ID CHANLIST 01 01
225 * channel 1 00 01 00 00 00 64 00
229 * min scan time 00 00
230 * max scan time 64 00
231 * channel 2 00 02 00 00 00 64 00
232 * channel 3 00 03 00 00 00 64 00
233 * channel 4 00 04 00 00 00 64 00
234 * channel 5 00 05 00 00 00 64 00
235 * channel 6 00 06 00 00 00 64 00
236 * channel 7 00 07 00 00 00 64 00
237 * channel 8 00 08 00 00 00 64 00
238 * channel 9 00 09 00 00 00 64 00
239 * channel 10 00 0a 00 00 00 64 00
240 * channel 11 00 0b 00 00 00 64 00
241 * channel 12 00 0c 00 00 00 64 00
242 * channel 13 00 0d 00 00 00 64 00
245 static int lbs_scan_add_chanlist_tlv(uint8_t *tlv,
246 struct chanscanparamset *chan_list,
249 size_t size = sizeof(struct chanscanparamset) *chan_count;
250 struct mrvlietypes_chanlistparamset *chan_tlv = (void *)tlv;
252 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
253 memcpy(chan_tlv->chanscanparam, chan_list, size);
254 chan_tlv->header.len = cpu_to_le16(size);
255 return sizeof(chan_tlv->header) + size;
259 * Add RATES TLV of the form
263 * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
265 * The rates are in lbs_bg_rates[], but for the 802.11b
266 * rates the high bit isn't set.
268 static int lbs_scan_add_rates_tlv(uint8_t *tlv)
271 struct mrvlietypes_ratesparamset *rate_tlv = (void *)tlv;
273 rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
274 tlv += sizeof(rate_tlv->header);
275 for (i = 0; i < MAX_RATES; i++) {
276 *tlv = lbs_bg_rates[i];
279 /* This code makes sure that the 802.11b rates (1 MBit/s, 2
280 MBit/s, 5.5 MBit/s and 11 MBit/s get's the high bit set.
281 Note that the values are MBit/s * 2, to mark them as
282 basic rates so that the firmware likes it better */
283 if (*tlv == 0x02 || *tlv == 0x04 ||
284 *tlv == 0x0b || *tlv == 0x16)
288 rate_tlv->header.len = cpu_to_le16(i);
289 return sizeof(rate_tlv->header) + i;
293 * Generate the CMD_802_11_SCAN command with the proper tlv
294 * for a bunch of channels.
296 static int lbs_do_scan(struct lbs_private *priv, uint8_t bsstype,
297 struct chanscanparamset *chan_list, int chan_count)
300 struct cmd_ds_802_11_scan *scan_cmd;
301 uint8_t *tlv; /* pointer into our current, growing TLV storage area */
303 lbs_deb_enter_args(LBS_DEB_SCAN, "bsstype %d, chanlist[].chan %d, chan_count %d",
304 bsstype, chan_list ? chan_list[0].channumber : -1,
307 /* create the fixed part for scan command */
308 scan_cmd = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
309 if (scan_cmd == NULL)
312 tlv = scan_cmd->tlvbuffer;
313 /* TODO: do we need to scan for a specific BSSID?
314 memcpy(scan_cmd->bssid, priv->scan_bssid, ETH_ALEN); */
315 scan_cmd->bsstype = bsstype;
318 if (priv->scan_ssid_len)
319 tlv += lbs_scan_add_ssid_tlv(priv, tlv);
320 if (chan_list && chan_count)
321 tlv += lbs_scan_add_chanlist_tlv(tlv, chan_list, chan_count);
322 tlv += lbs_scan_add_rates_tlv(tlv);
324 /* This is the final data we are about to send */
325 scan_cmd->hdr.size = cpu_to_le16(tlv - (uint8_t *)scan_cmd);
326 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
328 lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
329 tlv - scan_cmd->tlvbuffer);
331 ret = __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
332 le16_to_cpu(scan_cmd->hdr.size),
333 lbs_ret_80211_scan, 0);
337 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
342 * @brief Internal function used to start a scan based on an input config
344 * Use the input user scan configuration information when provided in
345 * order to send the appropriate scan commands to firmware to populate or
346 * update the internal driver scan table
348 * @param priv A pointer to struct lbs_private structure
349 * @param full_scan Do a full-scan (blocking)
351 * @return 0 or < 0 if error
353 int lbs_scan_networks(struct lbs_private *priv, int full_scan)
356 struct chanscanparamset *chan_list;
357 struct chanscanparamset *curr_chans;
359 uint8_t bsstype = CMD_BSS_TYPE_ANY;
360 int numchannels = MRVDRV_CHANNELS_PER_SCAN_CMD;
361 union iwreq_data wrqu;
362 #ifdef CONFIG_LIBERTAS_DEBUG
363 struct bss_descriptor *iter;
367 lbs_deb_enter_args(LBS_DEB_SCAN, "full_scan %d", full_scan);
369 /* Cancel any partial outstanding partial scans if this scan
372 if (full_scan && delayed_work_pending(&priv->scan_work))
373 cancel_delayed_work(&priv->scan_work);
375 /* User-specified bsstype or channel list
376 TODO: this can be implemented if some user-space application
377 need the feature. Formerly, it was accessible from debugfs,
378 but then nowhere used.
380 if (user_cfg->bsstype)
381 bsstype = user_cfg->bsstype;
384 lbs_deb_scan("numchannels %d, bsstype %d\n", numchannels, bsstype);
386 /* Create list of channels to scan */
387 chan_list = kzalloc(sizeof(struct chanscanparamset) *
388 LBS_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
390 lbs_pr_alert("SCAN: chan_list empty\n");
394 /* We want to scan all channels */
395 chan_count = lbs_scan_create_channel_list(priv, chan_list);
397 netif_stop_queue(priv->dev);
398 netif_carrier_off(priv->dev);
399 if (priv->mesh_dev) {
400 netif_stop_queue(priv->mesh_dev);
401 netif_carrier_off(priv->mesh_dev);
404 /* Prepare to continue an interrupted scan */
405 lbs_deb_scan("chan_count %d, scan_channel %d\n",
406 chan_count, priv->scan_channel);
407 curr_chans = chan_list;
408 /* advance channel list by already-scanned-channels */
409 if (priv->scan_channel > 0) {
410 curr_chans += priv->scan_channel;
411 chan_count -= priv->scan_channel;
414 /* Send scan command(s)
415 * numchannels contains the number of channels we should maximally scan
416 * chan_count is the total number of channels to scan
420 int to_scan = min(numchannels, chan_count);
421 lbs_deb_scan("scanning %d of %d channels\n",
422 to_scan, chan_count);
423 ret = lbs_do_scan(priv, bsstype, curr_chans,
426 lbs_pr_err("SCAN_CMD failed\n");
429 curr_chans += to_scan;
430 chan_count -= to_scan;
432 /* somehow schedule the next part of the scan */
433 if (chan_count && !full_scan &&
434 !priv->surpriseremoved) {
435 /* -1 marks just that we're currently scanning */
436 if (priv->scan_channel < 0)
437 priv->scan_channel = to_scan;
439 priv->scan_channel += to_scan;
440 cancel_delayed_work(&priv->scan_work);
441 queue_delayed_work(priv->work_thread, &priv->scan_work,
442 msecs_to_jiffies(300));
443 /* skip over GIWSCAN event */
448 memset(&wrqu, 0, sizeof(union iwreq_data));
449 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
451 #ifdef CONFIG_LIBERTAS_DEBUG
452 /* Dump the scan table */
453 mutex_lock(&priv->lock);
454 lbs_deb_scan("scan table:\n");
455 list_for_each_entry(iter, &priv->network_list, list)
456 lbs_deb_scan("%02d: BSSID %pM, RSSI %d, SSID '%s'\n",
457 i++, iter->bssid, iter->rssi,
458 escape_ssid(iter->ssid, iter->ssid_len));
459 mutex_unlock(&priv->lock);
463 priv->scan_channel = 0;
466 if (priv->connect_status == LBS_CONNECTED) {
467 netif_carrier_on(priv->dev);
468 if (!priv->tx_pending_len)
469 netif_wake_queue(priv->dev);
471 if (priv->mesh_dev && (priv->mesh_connect_status == LBS_CONNECTED)) {
472 netif_carrier_on(priv->mesh_dev);
473 if (!priv->tx_pending_len)
474 netif_wake_queue(priv->mesh_dev);
478 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
482 void lbs_scan_worker(struct work_struct *work)
484 struct lbs_private *priv =
485 container_of(work, struct lbs_private, scan_work.work);
487 lbs_deb_enter(LBS_DEB_SCAN);
488 lbs_scan_networks(priv, 0);
489 lbs_deb_leave(LBS_DEB_SCAN);
493 /*********************************************************************/
495 /* Result interpretation */
497 /*********************************************************************/
500 * @brief Interpret a BSS scan response returned from the firmware
502 * Parse the various fixed fields and IEs passed back for a a BSS probe
503 * response or beacon from the scan command. Record information as needed
504 * in the scan table struct bss_descriptor for that entry.
506 * @param bss Output parameter: Pointer to the BSS Entry
510 static int lbs_process_bss(struct bss_descriptor *bss,
511 uint8_t **pbeaconinfo, int *bytesleft)
513 struct ieeetypes_fhparamset *pFH;
514 struct ieeetypes_dsparamset *pDS;
515 struct ieeetypes_cfparamset *pCF;
516 struct ieeetypes_ibssparamset *pibss;
517 struct ieeetypes_countryinfoset *pcountryinfo;
518 uint8_t *pos, *end, *p;
519 uint8_t n_ex_rates = 0, got_basic_rates = 0, n_basic_rates = 0;
520 uint16_t beaconsize = 0;
523 lbs_deb_enter(LBS_DEB_SCAN);
525 if (*bytesleft >= sizeof(beaconsize)) {
526 /* Extract & convert beacon size from the command buffer */
527 beaconsize = get_unaligned_le16(*pbeaconinfo);
528 *bytesleft -= sizeof(beaconsize);
529 *pbeaconinfo += sizeof(beaconsize);
532 if (beaconsize == 0 || beaconsize > *bytesleft) {
533 *pbeaconinfo += *bytesleft;
539 /* Initialize the current working beacon pointer for this BSS iteration */
541 end = pos + beaconsize;
543 /* Advance the return beacon pointer past the current beacon */
544 *pbeaconinfo += beaconsize;
545 *bytesleft -= beaconsize;
547 memcpy(bss->bssid, pos, ETH_ALEN);
548 lbs_deb_scan("process_bss: BSSID %pM\n", bss->bssid);
551 if ((end - pos) < 12) {
552 lbs_deb_scan("process_bss: Not enough bytes left\n");
558 * next 4 fields are RSSI, time stamp, beacon interval,
559 * and capability information
562 /* RSSI is 1 byte long */
564 lbs_deb_scan("process_bss: RSSI %d\n", *pos);
567 /* time stamp is 8 bytes long */
570 /* beacon interval is 2 bytes long */
571 bss->beaconperiod = get_unaligned_le16(pos);
574 /* capability information is 2 bytes long */
575 bss->capability = get_unaligned_le16(pos);
576 lbs_deb_scan("process_bss: capabilities 0x%04x\n", bss->capability);
579 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
580 lbs_deb_scan("process_bss: WEP enabled\n");
581 if (bss->capability & WLAN_CAPABILITY_IBSS)
582 bss->mode = IW_MODE_ADHOC;
584 bss->mode = IW_MODE_INFRA;
586 /* rest of the current buffer are IE's */
587 lbs_deb_scan("process_bss: IE len %zd\n", end - pos);
588 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: IE info", pos, end - pos);
590 /* process variable IE */
591 while (pos <= end - 2) {
592 struct ieee80211_info_element * elem = (void *)pos;
594 if (pos + elem->len > end) {
595 lbs_deb_scan("process_bss: error in processing IE, "
596 "bytes left < IE length\n");
602 bss->ssid_len = min_t(int, 32, elem->len);
603 memcpy(bss->ssid, elem->data, bss->ssid_len);
604 lbs_deb_scan("got SSID IE: '%s', len %u\n",
605 escape_ssid(bss->ssid, bss->ssid_len),
609 case MFIE_TYPE_RATES:
610 n_basic_rates = min_t(uint8_t, MAX_RATES, elem->len);
611 memcpy(bss->rates, elem->data, n_basic_rates);
613 lbs_deb_scan("got RATES IE\n");
616 case MFIE_TYPE_FH_SET:
617 pFH = (struct ieeetypes_fhparamset *) pos;
618 memmove(&bss->phyparamset.fhparamset, pFH,
619 sizeof(struct ieeetypes_fhparamset));
620 lbs_deb_scan("got FH IE\n");
623 case MFIE_TYPE_DS_SET:
624 pDS = (struct ieeetypes_dsparamset *) pos;
625 bss->channel = pDS->currentchan;
626 memcpy(&bss->phyparamset.dsparamset, pDS,
627 sizeof(struct ieeetypes_dsparamset));
628 lbs_deb_scan("got DS IE, channel %d\n", bss->channel);
631 case MFIE_TYPE_CF_SET:
632 pCF = (struct ieeetypes_cfparamset *) pos;
633 memcpy(&bss->ssparamset.cfparamset, pCF,
634 sizeof(struct ieeetypes_cfparamset));
635 lbs_deb_scan("got CF IE\n");
638 case MFIE_TYPE_IBSS_SET:
639 pibss = (struct ieeetypes_ibssparamset *) pos;
640 bss->atimwindow = le16_to_cpu(pibss->atimwindow);
641 memmove(&bss->ssparamset.ibssparamset, pibss,
642 sizeof(struct ieeetypes_ibssparamset));
643 lbs_deb_scan("got IBSS IE\n");
646 case MFIE_TYPE_COUNTRY:
647 pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
648 lbs_deb_scan("got COUNTRY IE\n");
649 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
650 || pcountryinfo->len > 254) {
651 lbs_deb_scan("process_bss: 11D- Err CountryInfo len %d, min %zd, max 254\n",
652 pcountryinfo->len, sizeof(pcountryinfo->countrycode));
657 memcpy(&bss->countryinfo, pcountryinfo, pcountryinfo->len + 2);
658 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: 11d countryinfo",
659 (uint8_t *) pcountryinfo,
660 (int) (pcountryinfo->len + 2));
663 case MFIE_TYPE_RATES_EX:
664 /* only process extended supported rate if data rate is
665 * already found. Data rate IE should come before
666 * extended supported rate IE
668 lbs_deb_scan("got RATESEX IE\n");
669 if (!got_basic_rates) {
670 lbs_deb_scan("... but ignoring it\n");
674 n_ex_rates = elem->len;
675 if (n_basic_rates + n_ex_rates > MAX_RATES)
676 n_ex_rates = MAX_RATES - n_basic_rates;
678 p = bss->rates + n_basic_rates;
679 memcpy(p, elem->data, n_ex_rates);
682 case MFIE_TYPE_GENERIC:
683 if (elem->len >= 4 &&
684 elem->data[0] == 0x00 && elem->data[1] == 0x50 &&
685 elem->data[2] == 0xf2 && elem->data[3] == 0x01) {
686 bss->wpa_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
687 memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
688 lbs_deb_scan("got WPA IE\n");
689 lbs_deb_hex(LBS_DEB_SCAN, "WPA IE", bss->wpa_ie, elem->len);
690 } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
691 elem->data[0] == 0x00 && elem->data[1] == 0x50 &&
692 elem->data[2] == 0x43 && elem->data[3] == 0x04) {
693 lbs_deb_scan("got mesh IE\n");
696 lbs_deb_scan("got generic IE: %02x:%02x:%02x:%02x, len %d\n",
697 elem->data[0], elem->data[1],
698 elem->data[2], elem->data[3],
704 lbs_deb_scan("got RSN IE\n");
705 bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
706 memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
707 lbs_deb_hex(LBS_DEB_SCAN, "process_bss: RSN_IE",
708 bss->rsn_ie, elem->len);
712 lbs_deb_scan("got IE 0x%04x, len %d\n",
713 elem->id, elem->len);
717 pos += elem->len + 2;
721 bss->last_scanned = jiffies;
722 lbs_unset_basic_rate_flags(bss->rates, sizeof(bss->rates));
727 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
732 * @brief Send a scan command for all available channels filtered on a spec
734 * Used in association code and from debugfs
736 * @param priv A pointer to struct lbs_private structure
737 * @param ssid A pointer to the SSID to scan for
738 * @param ssid_len Length of the SSID
740 * @return 0-success, otherwise fail
742 int lbs_send_specific_ssid_scan(struct lbs_private *priv, uint8_t *ssid,
747 lbs_deb_enter_args(LBS_DEB_SCAN, "SSID '%s'\n",
748 escape_ssid(ssid, ssid_len));
753 memcpy(priv->scan_ssid, ssid, ssid_len);
754 priv->scan_ssid_len = ssid_len;
756 lbs_scan_networks(priv, 1);
757 if (priv->surpriseremoved) {
763 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
770 /*********************************************************************/
772 /* Support for Wireless Extensions */
774 /*********************************************************************/
777 #define MAX_CUSTOM_LEN 64
779 static inline char *lbs_translate_scan(struct lbs_private *priv,
780 struct iw_request_info *info,
781 char *start, char *stop,
782 struct bss_descriptor *bss)
784 struct chan_freq_power *cfp;
785 char *current_val; /* For rates */
786 struct iw_event iwe; /* Temporary buffer */
788 #define PERFECT_RSSI ((uint8_t)50)
789 #define WORST_RSSI ((uint8_t)0)
790 #define RSSI_DIFF ((uint8_t)(PERFECT_RSSI - WORST_RSSI))
793 lbs_deb_enter(LBS_DEB_SCAN);
795 cfp = lbs_find_cfp_by_band_and_channel(priv, 0, bss->channel);
797 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
802 /* First entry *MUST* be the BSSID */
804 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
805 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
806 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_ADDR_LEN);
809 iwe.cmd = SIOCGIWESSID;
810 iwe.u.data.flags = 1;
811 iwe.u.data.length = min((uint32_t) bss->ssid_len, (uint32_t) IW_ESSID_MAX_SIZE);
812 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
815 iwe.cmd = SIOCGIWMODE;
816 iwe.u.mode = bss->mode;
817 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_UINT_LEN);
820 iwe.cmd = SIOCGIWFREQ;
821 iwe.u.freq.m = (long)cfp->freq * 100000;
823 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_FREQ_LEN);
825 /* Add quality statistics */
827 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
828 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
830 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
832 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
833 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
834 (RSSI_DIFF * RSSI_DIFF);
835 if (iwe.u.qual.qual > 100)
836 iwe.u.qual.qual = 100;
838 if (priv->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
839 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
841 iwe.u.qual.noise = CAL_NF(priv->NF[TYPE_BEACON][TYPE_NOAVG]);
844 /* Locally created ad-hoc BSSs won't have beacons if this is the
845 * only station in the adhoc network; so get signal strength
846 * from receive statistics.
848 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate
849 && !lbs_ssid_cmp(priv->curbssparams.ssid,
850 priv->curbssparams.ssid_len,
851 bss->ssid, bss->ssid_len)) {
853 snr = priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
854 nf = priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
855 iwe.u.qual.level = CAL_RSSI(snr, nf);
857 start = iwe_stream_add_event(info, start, stop, &iwe, IW_EV_QUAL_LEN);
859 /* Add encryption capability */
860 iwe.cmd = SIOCGIWENCODE;
861 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
862 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
864 iwe.u.data.flags = IW_ENCODE_DISABLED;
866 iwe.u.data.length = 0;
867 start = iwe_stream_add_point(info, start, stop, &iwe, bss->ssid);
869 current_val = start + iwe_stream_lcp_len(info);
871 iwe.cmd = SIOCGIWRATE;
872 iwe.u.bitrate.fixed = 0;
873 iwe.u.bitrate.disabled = 0;
874 iwe.u.bitrate.value = 0;
876 for (j = 0; bss->rates[j] && (j < sizeof(bss->rates)); j++) {
877 /* Bit rate given in 500 kb/s units */
878 iwe.u.bitrate.value = bss->rates[j] * 500000;
879 current_val = iwe_stream_add_value(info, start, current_val,
880 stop, &iwe, IW_EV_PARAM_LEN);
882 if ((bss->mode == IW_MODE_ADHOC) && priv->adhoccreate
883 && !lbs_ssid_cmp(priv->curbssparams.ssid,
884 priv->curbssparams.ssid_len,
885 bss->ssid, bss->ssid_len)) {
886 iwe.u.bitrate.value = 22 * 500000;
887 current_val = iwe_stream_add_value(info, start, current_val,
888 stop, &iwe, IW_EV_PARAM_LEN);
890 /* Check if we added any event */
891 if ((current_val - start) > iwe_stream_lcp_len(info))
894 memset(&iwe, 0, sizeof(iwe));
895 if (bss->wpa_ie_len) {
896 char buf[MAX_WPA_IE_LEN];
897 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
899 iwe.u.data.length = bss->wpa_ie_len;
900 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
903 memset(&iwe, 0, sizeof(iwe));
904 if (bss->rsn_ie_len) {
905 char buf[MAX_WPA_IE_LEN];
906 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
908 iwe.u.data.length = bss->rsn_ie_len;
909 start = iwe_stream_add_point(info, start, stop, &iwe, buf);
913 char custom[MAX_CUSTOM_LEN];
916 iwe.cmd = IWEVCUSTOM;
917 p += snprintf(p, MAX_CUSTOM_LEN, "mesh-type: olpc");
918 iwe.u.data.length = p - custom;
919 if (iwe.u.data.length)
920 start = iwe_stream_add_point(info, start, stop,
925 lbs_deb_leave_args(LBS_DEB_SCAN, "start %p", start);
931 * @brief Handle Scan Network ioctl
933 * @param dev A pointer to net_device structure
934 * @param info A pointer to iw_request_info structure
935 * @param vwrq A pointer to iw_param structure
936 * @param extra A pointer to extra data buf
938 * @return 0 --success, otherwise fail
940 int lbs_set_scan(struct net_device *dev, struct iw_request_info *info,
941 union iwreq_data *wrqu, char *extra)
943 struct lbs_private *priv = dev->priv;
946 lbs_deb_enter(LBS_DEB_WEXT);
948 if (!priv->radio_on) {
953 if (!netif_running(dev)) {
958 /* mac80211 does this:
959 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
960 if (sdata->type != IEEE80211_IF_TYPE_xxx) {
966 if (wrqu->data.length == sizeof(struct iw_scan_req) &&
967 wrqu->data.flags & IW_SCAN_THIS_ESSID) {
968 struct iw_scan_req *req = (struct iw_scan_req *)extra;
969 priv->scan_ssid_len = req->essid_len;
970 memcpy(priv->scan_ssid, req->essid, priv->scan_ssid_len);
971 lbs_deb_wext("set_scan, essid '%s'\n",
972 escape_ssid(priv->scan_ssid, priv->scan_ssid_len));
974 priv->scan_ssid_len = 0;
977 if (!delayed_work_pending(&priv->scan_work))
978 queue_delayed_work(priv->work_thread, &priv->scan_work,
979 msecs_to_jiffies(50));
980 /* set marker that currently a scan is taking place */
981 priv->scan_channel = -1;
983 if (priv->surpriseremoved)
987 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", ret);
993 * @brief Handle Retrieve scan table ioctl
995 * @param dev A pointer to net_device structure
996 * @param info A pointer to iw_request_info structure
997 * @param dwrq A pointer to iw_point structure
998 * @param extra A pointer to extra data buf
1000 * @return 0 --success, otherwise fail
1002 int lbs_get_scan(struct net_device *dev, struct iw_request_info *info,
1003 struct iw_point *dwrq, char *extra)
1005 #define SCAN_ITEM_SIZE 128
1006 struct lbs_private *priv = dev->priv;
1009 char *stop = ev + dwrq->length;
1010 struct bss_descriptor *iter_bss;
1011 struct bss_descriptor *safe;
1013 lbs_deb_enter(LBS_DEB_WEXT);
1015 /* iwlist should wait until the current scan is finished */
1016 if (priv->scan_channel)
1019 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
1020 if ((priv->mode == IW_MODE_ADHOC) && priv->adhoccreate)
1021 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
1022 CMD_OPTION_WAITFORRSP, 0, NULL);
1024 mutex_lock(&priv->lock);
1025 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
1027 unsigned long stale_time;
1029 if (stop - ev < SCAN_ITEM_SIZE) {
1034 /* For mesh device, list only mesh networks */
1035 if (dev == priv->mesh_dev && !iter_bss->mesh)
1038 /* Prune old an old scan result */
1039 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1040 if (time_after(jiffies, stale_time)) {
1041 list_move_tail(&iter_bss->list, &priv->network_free_list);
1042 clear_bss_descriptor(iter_bss);
1046 /* Translate to WE format this entry */
1047 next_ev = lbs_translate_scan(priv, info, ev, stop, iter_bss);
1048 if (next_ev == NULL)
1052 mutex_unlock(&priv->lock);
1054 dwrq->length = (ev - extra);
1057 lbs_deb_leave_args(LBS_DEB_WEXT, "ret %d", err);
1064 /*********************************************************************/
1066 /* Command execution */
1068 /*********************************************************************/
1072 * @brief This function handles the command response of scan
1074 * Called from handle_cmd_response() in cmdrespc.
1076 * The response buffer for the scan command has the following
1079 * .-----------------------------------------------------------.
1080 * | header (4 * sizeof(u16)): Standard command response hdr |
1081 * .-----------------------------------------------------------.
1082 * | bufsize (u16) : sizeof the BSS Description data |
1083 * .-----------------------------------------------------------.
1084 * | NumOfSet (u8) : Number of BSS Descs returned |
1085 * .-----------------------------------------------------------.
1086 * | BSSDescription data (variable, size given in bufsize) |
1087 * .-----------------------------------------------------------.
1088 * | TLV data (variable, size calculated using header->size, |
1089 * | bufsize and sizeof the fixed fields above) |
1090 * .-----------------------------------------------------------.
1092 * @param priv A pointer to struct lbs_private structure
1093 * @param resp A pointer to cmd_ds_command
1097 static int lbs_ret_80211_scan(struct lbs_private *priv, unsigned long dummy,
1098 struct cmd_header *resp)
1100 struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
1101 struct bss_descriptor *iter_bss;
1102 struct bss_descriptor *safe;
1104 uint16_t scanrespsize;
1110 lbs_deb_enter(LBS_DEB_SCAN);
1112 /* Prune old entries from scan table */
1113 list_for_each_entry_safe (iter_bss, safe, &priv->network_list, list) {
1114 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1115 if (time_before(jiffies, stale_time))
1117 list_move_tail (&iter_bss->list, &priv->network_free_list);
1118 clear_bss_descriptor(iter_bss);
1121 if (scanresp->nr_sets > MAX_NETWORK_COUNT) {
1122 lbs_deb_scan("SCAN_RESP: too many scan results (%d, max %d)\n",
1123 scanresp->nr_sets, MAX_NETWORK_COUNT);
1128 bytesleft = le16_to_cpu(scanresp->bssdescriptsize);
1129 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
1131 scanrespsize = le16_to_cpu(resp->size);
1132 lbs_deb_scan("SCAN_RESP: scan results %d\n", scanresp->nr_sets);
1134 bssinfo = scanresp->bssdesc_and_tlvbuffer;
1136 /* The size of the TLV buffer is equal to the entire command response
1137 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1138 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1139 * response header (S_DS_GEN)
1141 tlvbufsize = scanrespsize - (bytesleft + sizeof(scanresp->bssdescriptsize)
1142 + sizeof(scanresp->nr_sets)
1146 * Process each scan response returned (scanresp->nr_sets). Save
1147 * the information in the newbssentry and then insert into the
1148 * driver scan table either as an update to an existing entry
1149 * or as an addition at the end of the table
1151 for (idx = 0; idx < scanresp->nr_sets && bytesleft; idx++) {
1152 struct bss_descriptor new;
1153 struct bss_descriptor *found = NULL;
1154 struct bss_descriptor *oldest = NULL;
1156 /* Process the data fields and IEs returned for this BSS */
1157 memset(&new, 0, sizeof (struct bss_descriptor));
1158 if (lbs_process_bss(&new, &bssinfo, &bytesleft) != 0) {
1159 /* error parsing the scan response, skipped */
1160 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1164 /* Try to find this bss in the scan table */
1165 list_for_each_entry (iter_bss, &priv->network_list, list) {
1166 if (is_same_network(iter_bss, &new)) {
1171 if ((oldest == NULL) ||
1172 (iter_bss->last_scanned < oldest->last_scanned))
1177 /* found, clear it */
1178 clear_bss_descriptor(found);
1179 } else if (!list_empty(&priv->network_free_list)) {
1180 /* Pull one from the free list */
1181 found = list_entry(priv->network_free_list.next,
1182 struct bss_descriptor, list);
1183 list_move_tail(&found->list, &priv->network_list);
1184 } else if (oldest) {
1185 /* If there are no more slots, expire the oldest */
1187 clear_bss_descriptor(found);
1188 list_move_tail(&found->list, &priv->network_list);
1193 lbs_deb_scan("SCAN_RESP: BSSID %pM\n", new.bssid);
1195 /* Copy the locally created newbssentry to the scan table */
1196 memcpy(found, &new, offsetof(struct bss_descriptor, list));
1202 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);