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/ctype.h>
9 #include <linux/netdevice.h>
10 #include <linux/wireless.h>
11 #include <linux/etherdevice.h>
13 #include <net/ieee80211.h>
14 #include <net/iw_handler.h>
21 //! Approximate amount of data needed to pass a scan result back to iwlist
22 #define MAX_SCAN_CELL_SIZE (IW_EV_ADDR_LEN \
29 + 40) /* 40 for WPAIE */
31 //! Memory needed to store a max sized channel List TLV for a firmware scan
32 #define CHAN_TLV_MAX_SIZE (sizeof(struct mrvlietypesheader) \
33 + (MRVDRV_MAX_CHANNELS_PER_SCAN \
34 * sizeof(struct chanscanparamset)))
36 //! Memory needed to store a max number/size SSID TLV for a firmware scan
37 #define SSID_TLV_MAX_SIZE (1 * sizeof(struct mrvlietypes_ssidparamset))
39 //! Maximum memory needed for a wlan_scan_cmd_config with all TLVs at max
40 #define MAX_SCAN_CFG_ALLOC (sizeof(struct wlan_scan_cmd_config) \
41 + sizeof(struct mrvlietypes_numprobes) \
45 //! The maximum number of channels the firmware can scan per command
46 #define MRVDRV_MAX_CHANNELS_PER_SCAN 14
49 * @brief Number of channels to scan per firmware scan command issuance.
51 * Number restricted to prevent hitting the limit on the amount of scan data
52 * returned in a single firmware scan command.
54 #define MRVDRV_CHANNELS_PER_SCAN_CMD 4
56 //! Scan time specified in the channel TLV for each channel for passive scans
57 #define MRVDRV_PASSIVE_SCAN_CHAN_TIME 100
59 //! Scan time specified in the channel TLV for each channel for active scans
60 #define MRVDRV_ACTIVE_SCAN_CHAN_TIME 100
62 static const u8 zeromac[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
63 static const u8 bcastmac[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
65 static inline void clear_bss_descriptor (struct bss_descriptor * bss)
67 /* Don't blow away ->list, just BSS data */
68 memset(bss, 0, offsetof(struct bss_descriptor, list));
71 static inline int match_bss_no_security(struct wlan_802_11_security * secinfo,
72 struct bss_descriptor * match_bss)
74 if ( !secinfo->wep_enabled
75 && !secinfo->WPAenabled
76 && !secinfo->WPA2enabled
77 && match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC
78 && match_bss->rsn_ie[0] != MFIE_TYPE_RSN
79 && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
85 static inline int match_bss_static_wep(struct wlan_802_11_security * secinfo,
86 struct bss_descriptor * match_bss)
88 if ( secinfo->wep_enabled
89 && !secinfo->WPAenabled
90 && !secinfo->WPA2enabled
91 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
97 static inline int match_bss_wpa(struct wlan_802_11_security * secinfo,
98 struct bss_descriptor * match_bss)
100 if ( !secinfo->wep_enabled
101 && secinfo->WPAenabled
102 && (match_bss->wpa_ie[0] == MFIE_TYPE_GENERIC)
103 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
104 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
112 static inline int match_bss_wpa2(struct wlan_802_11_security * secinfo,
113 struct bss_descriptor * match_bss)
115 if ( !secinfo->wep_enabled
116 && secinfo->WPA2enabled
117 && (match_bss->rsn_ie[0] == MFIE_TYPE_RSN)
118 /* privacy bit may NOT be set in some APs like LinkSys WRT54G
119 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
127 static inline int match_bss_dynamic_wep(struct wlan_802_11_security * secinfo,
128 struct bss_descriptor * match_bss)
130 if ( !secinfo->wep_enabled
131 && !secinfo->WPAenabled
132 && !secinfo->WPA2enabled
133 && (match_bss->wpa_ie[0] != MFIE_TYPE_GENERIC)
134 && (match_bss->rsn_ie[0] != MFIE_TYPE_RSN)
135 && (match_bss->capability & WLAN_CAPABILITY_PRIVACY)) {
142 * @brief Check if a scanned network compatible with the driver settings
144 * WEP WPA WPA2 ad-hoc encrypt Network
145 * enabled enabled enabled AES mode privacy WPA WPA2 Compatible
146 * 0 0 0 0 NONE 0 0 0 yes No security
147 * 1 0 0 0 NONE 1 0 0 yes Static WEP
148 * 0 1 0 0 x 1x 1 x yes WPA
149 * 0 0 1 0 x 1x x 1 yes WPA2
150 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
151 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
154 * @param adapter A pointer to wlan_adapter
155 * @param index Index in scantable to check against current driver settings
156 * @param mode Network mode: Infrastructure or IBSS
158 * @return Index in scantable, or error code if negative
160 static int is_network_compatible(wlan_adapter * adapter,
161 struct bss_descriptor * bss, u8 mode)
165 lbs_deb_enter(LBS_DEB_ASSOC);
167 if (bss->mode != mode)
170 if ((matched = match_bss_no_security(&adapter->secinfo, bss))) {
172 } else if ((matched = match_bss_static_wep(&adapter->secinfo, bss))) {
174 } else if ((matched = match_bss_wpa(&adapter->secinfo, bss))) {
176 "is_network_compatible() WPA: wpa_ie=%#x "
177 "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
178 "privacy=%#x\n", bss->wpa_ie[0], bss->rsn_ie[0],
179 adapter->secinfo.wep_enabled ? "e" : "d",
180 adapter->secinfo.WPAenabled ? "e" : "d",
181 adapter->secinfo.WPA2enabled ? "e" : "d",
182 (bss->capability & WLAN_CAPABILITY_PRIVACY));
184 } else if ((matched = match_bss_wpa2(&adapter->secinfo, bss))) {
186 "is_network_compatible() WPA2: wpa_ie=%#x "
187 "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
188 "privacy=%#x\n", bss->wpa_ie[0], bss->rsn_ie[0],
189 adapter->secinfo.wep_enabled ? "e" : "d",
190 adapter->secinfo.WPAenabled ? "e" : "d",
191 adapter->secinfo.WPA2enabled ? "e" : "d",
192 (bss->capability & WLAN_CAPABILITY_PRIVACY));
194 } else if ((matched = match_bss_dynamic_wep(&adapter->secinfo, bss))) {
196 "is_network_compatible() dynamic WEP: "
197 "wpa_ie=%#x wpa2_ie=%#x privacy=%#x\n",
198 bss->wpa_ie[0], bss->rsn_ie[0],
199 (bss->capability & WLAN_CAPABILITY_PRIVACY));
203 /* bss security settings don't match those configured on card */
205 "is_network_compatible() FAILED: wpa_ie=%#x "
206 "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s privacy=%#x\n",
207 bss->wpa_ie[0], bss->rsn_ie[0],
208 adapter->secinfo.wep_enabled ? "e" : "d",
209 adapter->secinfo.WPAenabled ? "e" : "d",
210 adapter->secinfo.WPA2enabled ? "e" : "d",
211 (bss->capability & WLAN_CAPABILITY_PRIVACY));
214 lbs_deb_leave(LBS_DEB_SCAN);
219 * @brief Create a channel list for the driver to scan based on region info
221 * Use the driver region/band information to construct a comprehensive list
222 * of channels to scan. This routine is used for any scan that is not
223 * provided a specific channel list to scan.
225 * @param priv A pointer to wlan_private structure
226 * @param scanchanlist Output parameter: resulting channel list to scan
227 * @param filteredscan Flag indicating whether or not a BSSID or SSID filter
228 * is being sent in the command to firmware. Used to
229 * increase the number of channels sent in a scan
230 * command and to disable the firmware channel scan
235 static void wlan_scan_create_channel_list(wlan_private * priv,
236 struct chanscanparamset * scanchanlist,
240 wlan_adapter *adapter = priv->adapter;
241 struct region_channel *scanregion;
242 struct chan_freq_power *cfp;
250 /* Set the default scan type to the user specified type, will later
251 * be changed to passive on a per channel basis if restricted by
252 * regulatory requirements (11d or 11h)
254 scantype = adapter->scantype;
256 for (rgnidx = 0; rgnidx < ARRAY_SIZE(adapter->region_channel); rgnidx++) {
257 if (priv->adapter->enable11d &&
258 adapter->connect_status != libertas_connected) {
259 /* Scan all the supported chan for the first scan */
260 if (!adapter->universal_channel[rgnidx].valid)
262 scanregion = &adapter->universal_channel[rgnidx];
264 /* clear the parsed_region_chan for the first scan */
265 memset(&adapter->parsed_region_chan, 0x00,
266 sizeof(adapter->parsed_region_chan));
268 if (!adapter->region_channel[rgnidx].valid)
270 scanregion = &adapter->region_channel[rgnidx];
274 nextchan < scanregion->nrcfp; nextchan++, chanidx++) {
276 cfp = scanregion->CFP + nextchan;
278 if (priv->adapter->enable11d) {
280 libertas_get_scan_type_11d(cfp->channel,
285 switch (scanregion->band) {
289 scanchanlist[chanidx].radiotype =
290 cmd_scan_radio_type_bg;
294 if (scantype == cmd_scan_type_passive) {
295 scanchanlist[chanidx].maxscantime =
296 cpu_to_le16(MRVDRV_PASSIVE_SCAN_CHAN_TIME);
297 scanchanlist[chanidx].chanscanmode.passivescan =
300 scanchanlist[chanidx].maxscantime =
301 cpu_to_le16(MRVDRV_ACTIVE_SCAN_CHAN_TIME);
302 scanchanlist[chanidx].chanscanmode.passivescan =
306 scanchanlist[chanidx].channumber = cfp->channel;
309 scanchanlist[chanidx].chanscanmode.
317 * @brief Construct a wlan_scan_cmd_config structure to use in issue scan cmds
319 * Application layer or other functions can invoke wlan_scan_networks
320 * with a scan configuration supplied in a wlan_ioctl_user_scan_cfg struct.
321 * This structure is used as the basis of one or many wlan_scan_cmd_config
322 * commands that are sent to the command processing module and sent to
325 * Create a wlan_scan_cmd_config based on the following user supplied
326 * parameters (if present):
329 * - Number of Probes to be sent
332 * If the SSID or BSSID filter is not present, disable/clear the filter.
333 * If the number of probes is not set, use the adapter default setting
334 * Qualify the channel
336 * @param priv A pointer to wlan_private structure
337 * @param puserscanin NULL or pointer to scan configuration parameters
338 * @param ppchantlvout Output parameter: Pointer to the start of the
339 * channel TLV portion of the output scan config
340 * @param pscanchanlist Output parameter: Pointer to the resulting channel
342 * @param pmaxchanperscan Output parameter: Number of channels to scan for
343 * each issuance of the firmware scan command
344 * @param pfilteredscan Output parameter: Flag indicating whether or not
345 * a BSSID or SSID filter is being sent in the
346 * command to firmware. Used to increase the number
347 * of channels sent in a scan command and to
348 * disable the firmware channel scan filter.
349 * @param pscancurrentonly Output parameter: Flag indicating whether or not
350 * we are only scanning our current active channel
352 * @return resulting scan configuration
354 static struct wlan_scan_cmd_config *
355 wlan_scan_setup_scan_config(wlan_private * priv,
356 const struct wlan_ioctl_user_scan_cfg * puserscanin,
357 struct mrvlietypes_chanlistparamset ** ppchantlvout,
358 struct chanscanparamset * pscanchanlist,
359 int *pmaxchanperscan,
361 u8 * pscancurrentonly)
363 wlan_adapter *adapter = priv->adapter;
364 struct mrvlietypes_numprobes *pnumprobestlv;
365 struct mrvlietypes_ssidparamset *pssidtlv;
366 struct wlan_scan_cmd_config * pscancfgout = NULL;
375 pscancfgout = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
376 if (pscancfgout == NULL)
379 /* The tlvbufferlen is calculated for each scan command. The TLVs added
380 * in this routine will be preserved since the routine that sends
381 * the command will append channelTLVs at *ppchantlvout. The difference
382 * between the *ppchantlvout and the tlvbuffer start will be used
383 * to calculate the size of anything we add in this routine.
385 pscancfgout->tlvbufferlen = 0;
387 /* Running tlv pointer. Assigned to ppchantlvout at end of function
388 * so later routines know where channels can be added to the command buf
390 ptlvpos = pscancfgout->tlvbuffer;
393 * Set the initial scan paramters for progressive scanning. If a specific
394 * BSSID or SSID is used, the number of channels in the scan command
395 * will be increased to the absolute maximum
397 *pmaxchanperscan = MRVDRV_CHANNELS_PER_SCAN_CMD;
399 /* Initialize the scan as un-filtered by firmware, set to TRUE below if
400 * a SSID or BSSID filter is sent in the command
404 /* Initialize the scan as not being only on the current channel. If
405 * the channel list is customized, only contains one channel, and
406 * is the active channel, this is set true and data flow is not halted.
408 *pscancurrentonly = 0;
412 /* Set the bss type scan filter, use adapter setting if unset */
413 pscancfgout->bsstype =
414 (puserscanin->bsstype ? puserscanin->bsstype : adapter->
417 /* Set the number of probes to send, use adapter setting if unset */
418 numprobes = (puserscanin->numprobes ? puserscanin->numprobes :
419 adapter->scanprobes);
422 * Set the BSSID filter to the incoming configuration,
423 * if non-zero. If not set, it will remain disabled (all zeros).
425 memcpy(pscancfgout->bssid, puserscanin->bssid,
426 sizeof(pscancfgout->bssid));
428 if (puserscanin->ssid_len) {
430 (struct mrvlietypes_ssidparamset *) pscancfgout->
432 pssidtlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
433 pssidtlv->header.len = cpu_to_le16(puserscanin->ssid_len);
434 memcpy(pssidtlv->ssid, puserscanin->ssid,
435 puserscanin->ssid_len);
436 ptlvpos += sizeof(pssidtlv->header) + puserscanin->ssid_len;
440 * The default number of channels sent in the command is low to
441 * ensure the response buffer from the firmware does not truncate
442 * scan results. That is not an issue with an SSID or BSSID
443 * filter applied to the scan results in the firmware.
445 if ( puserscanin->ssid_len
446 || (compare_ether_addr(pscancfgout->bssid, &zeromac[0]) != 0)) {
447 *pmaxchanperscan = MRVDRV_MAX_CHANNELS_PER_SCAN;
451 pscancfgout->bsstype = adapter->scanmode;
452 numprobes = adapter->scanprobes;
455 /* If the input config or adapter has the number of Probes set, add tlv */
457 pnumprobestlv = (struct mrvlietypes_numprobes *) ptlvpos;
458 pnumprobestlv->header.type = cpu_to_le16(TLV_TYPE_NUMPROBES);
459 pnumprobestlv->header.len = cpu_to_le16(2);
460 pnumprobestlv->numprobes = cpu_to_le16(numprobes);
462 ptlvpos += sizeof(*pnumprobestlv);
466 * Set the output for the channel TLV to the address in the tlv buffer
467 * past any TLVs that were added in this fuction (SSID, numprobes).
468 * channel TLVs will be added past this for each scan command, preserving
469 * the TLVs that were previously added.
471 *ppchantlvout = (struct mrvlietypes_chanlistparamset *) ptlvpos;
473 if (puserscanin && puserscanin->chanlist[0].channumber) {
475 lbs_deb_scan("Scan: Using supplied channel list\n");
478 chanidx < WLAN_IOCTL_USER_SCAN_CHAN_MAX
479 && puserscanin->chanlist[chanidx].channumber; chanidx++) {
481 channel = puserscanin->chanlist[chanidx].channumber;
482 (pscanchanlist + chanidx)->channumber = channel;
484 radiotype = puserscanin->chanlist[chanidx].radiotype;
485 (pscanchanlist + chanidx)->radiotype = radiotype;
487 scantype = puserscanin->chanlist[chanidx].scantype;
489 if (scantype == cmd_scan_type_passive) {
491 chanidx)->chanscanmode.passivescan = 1;
494 chanidx)->chanscanmode.passivescan = 0;
497 if (puserscanin->chanlist[chanidx].scantime) {
499 puserscanin->chanlist[chanidx].scantime;
501 if (scantype == cmd_scan_type_passive) {
502 scandur = MRVDRV_PASSIVE_SCAN_CHAN_TIME;
504 scandur = MRVDRV_ACTIVE_SCAN_CHAN_TIME;
508 (pscanchanlist + chanidx)->minscantime =
509 cpu_to_le16(scandur);
510 (pscanchanlist + chanidx)->maxscantime =
511 cpu_to_le16(scandur);
514 /* Check if we are only scanning the current channel */
515 if ((chanidx == 1) && (puserscanin->chanlist[0].channumber
517 priv->adapter->curbssparams.channel)) {
518 *pscancurrentonly = 1;
519 lbs_deb_scan("Scan: Scanning current channel only");
523 lbs_deb_scan("Scan: Creating full region channel list\n");
524 wlan_scan_create_channel_list(priv, pscanchanlist,
533 * @brief Construct and send multiple scan config commands to the firmware
535 * Previous routines have created a wlan_scan_cmd_config with any requested
536 * TLVs. This function splits the channel TLV into maxchanperscan lists
537 * and sends the portion of the channel TLV along with the other TLVs
538 * to the wlan_cmd routines for execution in the firmware.
540 * @param priv A pointer to wlan_private structure
541 * @param maxchanperscan Maximum number channels to be included in each
542 * scan command sent to firmware
543 * @param filteredscan Flag indicating whether or not a BSSID or SSID
544 * filter is being used for the firmware command
545 * scan command sent to firmware
546 * @param pscancfgout Scan configuration used for this scan.
547 * @param pchantlvout Pointer in the pscancfgout where the channel TLV
548 * should start. This is past any other TLVs that
549 * must be sent down in each firmware command.
550 * @param pscanchanlist List of channels to scan in maxchanperscan segments
552 * @return 0 or error return otherwise
554 static int wlan_scan_channel_list(wlan_private * priv,
557 struct wlan_scan_cmd_config * pscancfgout,
558 struct mrvlietypes_chanlistparamset * pchantlvout,
559 struct chanscanparamset * pscanchanlist,
560 const struct wlan_ioctl_user_scan_cfg * puserscanin,
563 struct chanscanparamset *ptmpchan;
564 struct chanscanparamset *pstartchan;
570 union iwreq_data wrqu;
572 lbs_deb_enter(LBS_DEB_ASSOC);
574 if (pscancfgout == 0 || pchantlvout == 0 || pscanchanlist == 0) {
575 lbs_deb_scan("Scan: Null detect: %p, %p, %p\n",
576 pscancfgout, pchantlvout, pscanchanlist);
580 pchantlvout->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
582 /* Set the temp channel struct pointer to the start of the desired list */
583 ptmpchan = pscanchanlist;
585 if (priv->adapter->last_scanned_channel && !puserscanin)
586 ptmpchan += priv->adapter->last_scanned_channel;
588 /* Loop through the desired channel list, sending a new firmware scan
589 * commands for each maxchanperscan channels (or for 1,6,11 individually
590 * if configured accordingly)
592 while (ptmpchan->channumber) {
595 pchantlvout->header.len = 0;
596 scanband = ptmpchan->radiotype;
597 pstartchan = ptmpchan;
600 /* Construct the channel TLV for the scan command. Continue to
601 * insert channel TLVs until:
602 * - the tlvidx hits the maximum configured per scan command
603 * - the next channel to insert is 0 (end of desired channel list)
604 * - doneearly is set (controlling individual scanning of 1,6,11)
606 while (tlvidx < maxchanperscan && ptmpchan->channumber
607 && !doneearly && scanned < 2) {
610 "Scan: Chan(%3d), Radio(%d), mode(%d,%d), Dur(%d)\n",
611 ptmpchan->channumber, ptmpchan->radiotype,
612 ptmpchan->chanscanmode.passivescan,
613 ptmpchan->chanscanmode.disablechanfilt,
614 ptmpchan->maxscantime);
616 /* Copy the current channel TLV to the command being prepared */
617 memcpy(pchantlvout->chanscanparam + tlvidx,
618 ptmpchan, sizeof(pchantlvout->chanscanparam));
620 /* Increment the TLV header length by the size appended */
621 /* Ew, it would be _so_ nice if we could just declare the
622 variable little-endian and let GCC handle it for us */
623 pchantlvout->header.len =
624 cpu_to_le16(le16_to_cpu(pchantlvout->header.len) +
625 sizeof(pchantlvout->chanscanparam));
628 * The tlv buffer length is set to the number of bytes of the
629 * between the channel tlv pointer and the start of the
630 * tlv buffer. This compensates for any TLVs that were appended
631 * before the channel list.
633 pscancfgout->tlvbufferlen = ((u8 *) pchantlvout
634 - pscancfgout->tlvbuffer);
636 /* Add the size of the channel tlv header and the data length */
637 pscancfgout->tlvbufferlen +=
638 (sizeof(pchantlvout->header)
639 + le16_to_cpu(pchantlvout->header.len));
641 /* Increment the index to the channel tlv we are constructing */
646 /* Stop the loop if the *current* channel is in the 1,6,11 set
647 * and we are not filtering on a BSSID or SSID.
649 if (!filteredscan && (ptmpchan->channumber == 1
650 || ptmpchan->channumber == 6
651 || ptmpchan->channumber == 11)) {
655 /* Increment the tmp pointer to the next channel to be scanned */
659 /* Stop the loop if the *next* channel is in the 1,6,11 set.
660 * This will cause it to be the only channel scanned on the next
663 if (!filteredscan && (ptmpchan->channumber == 1
664 || ptmpchan->channumber == 6
665 || ptmpchan->channumber == 11)) {
670 /* Send the scan command to the firmware with the specified cfg */
671 ret = libertas_prepare_and_send_command(priv, cmd_802_11_scan, 0,
673 if (scanned >= 2 && !full_scan) {
681 priv->adapter->last_scanned_channel = ptmpchan->channumber;
683 /* Tell userspace the scan table has been updated */
684 memset(&wrqu, 0, sizeof(union iwreq_data));
685 wireless_send_event(priv->dev, SIOCGIWSCAN, &wrqu, NULL);
687 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
692 clear_selected_scan_list_entries(wlan_adapter * adapter,
693 const struct wlan_ioctl_user_scan_cfg * scan_cfg)
695 struct bss_descriptor * bss;
696 struct bss_descriptor * safe;
697 u32 clear_ssid_flag = 0, clear_bssid_flag = 0;
702 if (scan_cfg->clear_ssid && scan_cfg->ssid_len)
705 if (scan_cfg->clear_bssid
706 && (compare_ether_addr(scan_cfg->bssid, &zeromac[0]) != 0)
707 && (compare_ether_addr(scan_cfg->bssid, &bcastmac[0]) != 0)) {
708 clear_bssid_flag = 1;
711 if (!clear_ssid_flag && !clear_bssid_flag)
714 mutex_lock(&adapter->lock);
715 list_for_each_entry_safe (bss, safe, &adapter->network_list, list) {
718 /* Check for an SSID match */
720 && (bss->ssid_len == scan_cfg->ssid_len)
721 && !memcmp(bss->ssid, scan_cfg->ssid, bss->ssid_len))
724 /* Check for a BSSID match */
725 if ( clear_bssid_flag
726 && !compare_ether_addr(bss->bssid, scan_cfg->bssid))
730 list_move_tail (&bss->list, &adapter->network_free_list);
731 clear_bss_descriptor(bss);
734 mutex_unlock(&adapter->lock);
739 * @brief Internal function used to start a scan based on an input config
741 * Use the input user scan configuration information when provided in
742 * order to send the appropriate scan commands to firmware to populate or
743 * update the internal driver scan table
745 * @param priv A pointer to wlan_private structure
746 * @param puserscanin Pointer to the input configuration for the requested
749 * @return 0 or < 0 if error
751 int wlan_scan_networks(wlan_private * priv,
752 const struct wlan_ioctl_user_scan_cfg * puserscanin,
755 wlan_adapter * adapter = priv->adapter;
756 struct mrvlietypes_chanlistparamset *pchantlvout;
757 struct chanscanparamset * scan_chan_list = NULL;
758 struct wlan_scan_cmd_config * scan_cfg = NULL;
760 u8 scancurrentchanonly;
763 #ifdef CONFIG_LIBERTAS_DEBUG
764 struct bss_descriptor * iter_bss;
768 lbs_deb_enter(LBS_DEB_ASSOC);
770 scan_chan_list = kzalloc(sizeof(struct chanscanparamset) *
771 WLAN_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
772 if (scan_chan_list == NULL) {
777 scan_cfg = wlan_scan_setup_scan_config(priv,
783 &scancurrentchanonly);
784 if (scan_cfg == NULL) {
789 clear_selected_scan_list_entries(adapter, puserscanin);
791 /* Keep the data path active if we are only scanning our current channel */
792 if (!scancurrentchanonly) {
793 netif_stop_queue(priv->dev);
794 netif_carrier_off(priv->dev);
795 netif_stop_queue(priv->mesh_dev);
796 netif_carrier_off(priv->mesh_dev);
799 ret = wlan_scan_channel_list(priv,
808 #ifdef CONFIG_LIBERTAS_DEBUG
809 /* Dump the scan table */
810 mutex_lock(&adapter->lock);
811 list_for_each_entry (iter_bss, &adapter->network_list, list) {
812 lbs_deb_scan("Scan:(%02d) " MAC_FMT ", RSSI[%03d], SSID[%s]\n",
813 i++, MAC_ARG(iter_bss->bssid), (s32) iter_bss->rssi,
814 escape_essid(iter_bss->ssid, iter_bss->ssid_len));
816 mutex_unlock(&adapter->lock);
819 if (priv->adapter->connect_status == libertas_connected) {
820 netif_carrier_on(priv->dev);
821 netif_wake_queue(priv->dev);
822 netif_carrier_on(priv->mesh_dev);
823 netif_wake_queue(priv->mesh_dev);
831 kfree(scan_chan_list);
833 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
838 * @brief Inspect the scan response buffer for pointers to expected TLVs
840 * TLVs can be included at the end of the scan response BSS information.
841 * Parse the data in the buffer for pointers to TLVs that can potentially
842 * be passed back in the response
844 * @param ptlv Pointer to the start of the TLV buffer to parse
845 * @param tlvbufsize size of the TLV buffer
846 * @param ptsftlv Output parameter: Pointer to the TSF TLV if found
851 void wlan_ret_802_11_scan_get_tlv_ptrs(struct mrvlietypes_data * ptlv,
853 struct mrvlietypes_tsftimestamp ** ptsftlv)
855 struct mrvlietypes_data *pcurrenttlv;
861 tlvbufleft = tlvbufsize;
864 lbs_deb_scan("SCAN_RESP: tlvbufsize = %d\n", tlvbufsize);
865 lbs_dbg_hex("SCAN_RESP: TLV Buf", (u8 *) ptlv, tlvbufsize);
867 while (tlvbufleft >= sizeof(struct mrvlietypesheader)) {
868 tlvtype = le16_to_cpu(pcurrenttlv->header.type);
869 tlvlen = le16_to_cpu(pcurrenttlv->header.len);
872 case TLV_TYPE_TSFTIMESTAMP:
873 *ptsftlv = (struct mrvlietypes_tsftimestamp *) pcurrenttlv;
877 lbs_deb_scan("SCAN_RESP: Unhandled TLV = %d\n",
879 /* Give up, this seems corrupted */
883 tlvbufleft -= (sizeof(ptlv->header) + tlvlen);
885 (struct mrvlietypes_data *) (pcurrenttlv->Data + tlvlen);
890 * @brief Interpret a BSS scan response returned from the firmware
892 * Parse the various fixed fields and IEs passed back for a a BSS probe
893 * response or beacon from the scan command. Record information as needed
894 * in the scan table struct bss_descriptor for that entry.
896 * @param bss Output parameter: Pointer to the BSS Entry
900 static int libertas_process_bss(struct bss_descriptor * bss,
901 u8 ** pbeaconinfo, int *bytesleft)
903 struct ieeetypes_fhparamset *pFH;
904 struct ieeetypes_dsparamset *pDS;
905 struct ieeetypes_cfparamset *pCF;
906 struct ieeetypes_ibssparamset *pibss;
915 struct ieeetypes_countryinfoset *pcountryinfo;
917 lbs_deb_enter(LBS_DEB_ASSOC);
923 if (*bytesleft >= sizeof(beaconsize)) {
924 /* Extract & convert beacon size from the command buffer */
925 beaconsize = le16_to_cpup((void *)*pbeaconinfo);
926 *bytesleft -= sizeof(beaconsize);
927 *pbeaconinfo += sizeof(beaconsize);
930 if (beaconsize == 0 || beaconsize > *bytesleft) {
931 *pbeaconinfo += *bytesleft;
936 /* Initialize the current working beacon pointer for this BSS iteration */
938 end = pos + beaconsize;
940 /* Advance the return beacon pointer past the current beacon */
941 *pbeaconinfo += beaconsize;
942 *bytesleft -= beaconsize;
944 memcpy(bss->bssid, pos, ETH_ALEN);
945 lbs_deb_scan("process_bss: AP BSSID " MAC_FMT "\n", MAC_ARG(bss->bssid));
948 if ((end - pos) < 12) {
949 lbs_deb_scan("process_bss: Not enough bytes left\n");
954 * next 4 fields are RSSI, time stamp, beacon interval,
955 * and capability information
958 /* RSSI is 1 byte long */
960 lbs_deb_scan("process_bss: RSSI=%02X\n", *pos);
963 /* time stamp is 8 bytes long */
964 bss->timestamp = le64_to_cpup((void *) pos);
967 /* beacon interval is 2 bytes long */
968 bss->beaconperiod = le16_to_cpup((void *) pos);
971 /* capability information is 2 bytes long */
972 bss->capability = le16_to_cpup((void *) pos);
973 lbs_deb_scan("process_bss: capabilities = 0x%4X\n", bss->capability);
976 if (bss->capability & WLAN_CAPABILITY_PRIVACY)
977 lbs_deb_scan("process_bss: AP WEP enabled\n");
978 if (bss->capability & WLAN_CAPABILITY_IBSS)
979 bss->mode = IW_MODE_ADHOC;
981 bss->mode = IW_MODE_INFRA;
983 /* rest of the current buffer are IE's */
984 lbs_deb_scan("process_bss: IE length for this AP = %zd\n", end - pos);
985 lbs_dbg_hex("process_bss: IE info", pos, end - pos);
987 /* process variable IE */
988 while (pos <= end - 2) {
989 struct ieee80211_info_element * elem =
990 (struct ieee80211_info_element *) pos;
992 if (pos + elem->len > end) {
993 lbs_deb_scan("process_bss: error in processing IE, "
994 "bytes left < IE length\n");
1000 bss->ssid_len = elem->len;
1001 memcpy(bss->ssid, elem->data, elem->len);
1002 lbs_deb_scan("ssid '%s', ssid length %u\n",
1003 escape_essid(bss->ssid, bss->ssid_len),
1007 case MFIE_TYPE_RATES:
1008 memcpy(bss->datarates, elem->data, elem->len);
1009 memmove(bss->libertas_supported_rates, elem->data,
1011 ratesize = elem->len;
1012 founddatarateie = 1;
1015 case MFIE_TYPE_FH_SET:
1016 pFH = (struct ieeetypes_fhparamset *) pos;
1017 memmove(&bss->phyparamset.fhparamset, pFH,
1018 sizeof(struct ieeetypes_fhparamset));
1019 #if 0 /* I think we can store these LE */
1020 bss->phyparamset.fhparamset.dwelltime
1021 = le16_to_cpu(bss->phyparamset.fhparamset.dwelltime);
1025 case MFIE_TYPE_DS_SET:
1026 pDS = (struct ieeetypes_dsparamset *) pos;
1027 bss->channel = pDS->currentchan;
1028 memcpy(&bss->phyparamset.dsparamset, pDS,
1029 sizeof(struct ieeetypes_dsparamset));
1032 case MFIE_TYPE_CF_SET:
1033 pCF = (struct ieeetypes_cfparamset *) pos;
1034 memcpy(&bss->ssparamset.cfparamset, pCF,
1035 sizeof(struct ieeetypes_cfparamset));
1038 case MFIE_TYPE_IBSS_SET:
1039 pibss = (struct ieeetypes_ibssparamset *) pos;
1040 bss->atimwindow = le32_to_cpu(pibss->atimwindow);
1041 memmove(&bss->ssparamset.ibssparamset, pibss,
1042 sizeof(struct ieeetypes_ibssparamset));
1044 bss->ssparamset.ibssparamset.atimwindow
1045 = le16_to_cpu(bss->ssparamset.ibssparamset.atimwindow);
1049 case MFIE_TYPE_COUNTRY:
1050 pcountryinfo = (struct ieeetypes_countryinfoset *) pos;
1051 if (pcountryinfo->len < sizeof(pcountryinfo->countrycode)
1052 || pcountryinfo->len > 254) {
1053 lbs_deb_scan("process_bss: 11D- Err "
1054 "CountryInfo len =%d min=%zd max=254\n",
1056 sizeof(pcountryinfo->countrycode));
1061 memcpy(&bss->countryinfo,
1062 pcountryinfo, pcountryinfo->len + 2);
1063 lbs_dbg_hex("process_bss: 11D- CountryInfo:",
1064 (u8 *) pcountryinfo,
1065 (u32) (pcountryinfo->len + 2));
1068 case MFIE_TYPE_RATES_EX:
1069 /* only process extended supported rate if data rate is
1070 * already found. Data rate IE should come before
1071 * extended supported rate IE
1073 if (!founddatarateie)
1076 if ((elem->len + ratesize) > WLAN_SUPPORTED_RATES) {
1078 (WLAN_SUPPORTED_RATES - ratesize);
1080 bytestocopy = elem->len;
1083 pRate = (u8 *) bss->datarates;
1085 memmove(pRate, elem->data, bytestocopy);
1086 pRate = (u8 *) bss->libertas_supported_rates;
1088 memmove(pRate, elem->data, bytestocopy);
1091 case MFIE_TYPE_GENERIC:
1092 if (elem->len >= 4 &&
1093 elem->data[0] == 0x00 &&
1094 elem->data[1] == 0x50 &&
1095 elem->data[2] == 0xf2 &&
1096 elem->data[3] == 0x01) {
1097 bss->wpa_ie_len = min(elem->len + 2,
1099 memcpy(bss->wpa_ie, elem, bss->wpa_ie_len);
1100 lbs_dbg_hex("process_bss: WPA IE", bss->wpa_ie,
1102 } else if (elem->len >= MARVELL_MESH_IE_LENGTH &&
1103 elem->data[0] == 0x00 &&
1104 elem->data[1] == 0x50 &&
1105 elem->data[2] == 0x43 &&
1106 elem->data[3] == 0x04) {
1112 bss->rsn_ie_len = min(elem->len + 2, MAX_WPA_IE_LEN);
1113 memcpy(bss->rsn_ie, elem, bss->rsn_ie_len);
1114 lbs_dbg_hex("process_bss: RSN_IE", bss->rsn_ie, elem->len);
1121 pos += elem->len + 2;
1125 bss->last_scanned = jiffies;
1130 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1135 * @brief Compare two SSIDs
1137 * @param ssid1 A pointer to ssid to compare
1138 * @param ssid2 A pointer to ssid to compare
1140 * @return 0--ssid is same, otherwise is different
1142 int libertas_ssid_cmp(u8 *ssid1, u8 ssid1_len, u8 *ssid2, u8 ssid2_len)
1144 if (ssid1_len != ssid2_len)
1147 return memcmp(ssid1, ssid2, ssid1_len);
1151 * @brief This function finds a specific compatible BSSID in the scan list
1153 * @param adapter A pointer to wlan_adapter
1154 * @param bssid BSSID to find in the scan list
1155 * @param mode Network mode: Infrastructure or IBSS
1157 * @return index in BSSID list, or error return code (< 0)
1159 struct bss_descriptor * libertas_find_bssid_in_list(wlan_adapter * adapter,
1160 u8 * bssid, u8 mode)
1162 struct bss_descriptor * iter_bss;
1163 struct bss_descriptor * found_bss = NULL;
1168 lbs_dbg_hex("libertas_find_BSSID_in_list: looking for ",
1171 /* Look through the scan table for a compatible match. The loop will
1172 * continue past a matched bssid that is not compatible in case there
1173 * is an AP with multiple SSIDs assigned to the same BSSID
1175 mutex_lock(&adapter->lock);
1176 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1177 if (compare_ether_addr(iter_bss->bssid, bssid))
1178 continue; /* bssid doesn't match */
1182 if (!is_network_compatible(adapter, iter_bss, mode))
1184 found_bss = iter_bss;
1187 found_bss = iter_bss;
1191 mutex_unlock(&adapter->lock);
1197 * @brief This function finds ssid in ssid list.
1199 * @param adapter A pointer to wlan_adapter
1200 * @param ssid SSID to find in the list
1201 * @param bssid BSSID to qualify the SSID selection (if provided)
1202 * @param mode Network mode: Infrastructure or IBSS
1204 * @return index in BSSID list
1206 struct bss_descriptor * libertas_find_ssid_in_list(wlan_adapter * adapter,
1207 u8 *ssid, u8 ssid_len, u8 * bssid, u8 mode,
1211 struct bss_descriptor * iter_bss = NULL;
1212 struct bss_descriptor * found_bss = NULL;
1213 struct bss_descriptor * tmp_oldest = NULL;
1215 mutex_lock(&adapter->lock);
1217 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1219 || (iter_bss->last_scanned < tmp_oldest->last_scanned))
1220 tmp_oldest = iter_bss;
1222 if (libertas_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
1223 ssid, ssid_len) != 0)
1224 continue; /* ssid doesn't match */
1225 if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
1226 continue; /* bssid doesn't match */
1227 if ((channel > 0) && (iter_bss->channel != channel))
1228 continue; /* channel doesn't match */
1233 if (!is_network_compatible(adapter, iter_bss, mode))
1237 /* Found requested BSSID */
1238 found_bss = iter_bss;
1242 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1243 bestrssi = SCAN_RSSI(iter_bss->rssi);
1244 found_bss = iter_bss;
1249 if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
1250 bestrssi = SCAN_RSSI(iter_bss->rssi);
1251 found_bss = iter_bss;
1258 mutex_unlock(&adapter->lock);
1263 * @brief This function finds the best SSID in the Scan List
1265 * Search the scan table for the best SSID that also matches the current
1266 * adapter network preference (infrastructure or adhoc)
1268 * @param adapter A pointer to wlan_adapter
1270 * @return index in BSSID list
1272 struct bss_descriptor * libertas_find_best_ssid_in_list(wlan_adapter * adapter,
1276 struct bss_descriptor * iter_bss;
1277 struct bss_descriptor * best_bss = NULL;
1279 mutex_lock(&adapter->lock);
1281 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1285 if (!is_network_compatible(adapter, iter_bss, mode))
1287 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1289 bestrssi = SCAN_RSSI(iter_bss->rssi);
1290 best_bss = iter_bss;
1294 if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
1296 bestrssi = SCAN_RSSI(iter_bss->rssi);
1297 best_bss = iter_bss;
1302 mutex_unlock(&adapter->lock);
1307 * @brief Find the AP with specific ssid in the scan list
1309 * @param priv A pointer to wlan_private structure
1310 * @param pSSID A pointer to AP's ssid
1312 * @return 0--success, otherwise--fail
1314 int libertas_find_best_network_ssid(wlan_private * priv,
1315 u8 *out_ssid, u8 *out_ssid_len, u8 preferred_mode, u8 *out_mode)
1317 wlan_adapter *adapter = priv->adapter;
1319 struct bss_descriptor * found;
1321 lbs_deb_enter(LBS_DEB_ASSOC);
1323 wlan_scan_networks(priv, NULL, 1);
1324 if (adapter->surpriseremoved)
1327 wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1329 found = libertas_find_best_ssid_in_list(adapter, preferred_mode);
1330 if (found && (found->ssid_len > 0)) {
1331 memcpy(out_ssid, &found->ssid, IW_ESSID_MAX_SIZE);
1332 *out_ssid_len = found->ssid_len;
1333 *out_mode = found->mode;
1337 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1342 * @brief Scan Network
1344 * @param dev A pointer to net_device structure
1345 * @param info A pointer to iw_request_info structure
1346 * @param vwrq A pointer to iw_param structure
1347 * @param extra A pointer to extra data buf
1349 * @return 0 --success, otherwise fail
1351 int libertas_set_scan(struct net_device *dev, struct iw_request_info *info,
1352 struct iw_param *vwrq, char *extra)
1354 wlan_private *priv = dev->priv;
1355 wlan_adapter *adapter = priv->adapter;
1357 lbs_deb_enter(LBS_DEB_SCAN);
1359 wlan_scan_networks(priv, NULL, 0);
1361 if (adapter->surpriseremoved)
1364 lbs_deb_leave(LBS_DEB_SCAN);
1369 * @brief Send a scan command for all available channels filtered on a spec
1371 * @param priv A pointer to wlan_private structure
1372 * @param prequestedssid A pointer to AP's ssid
1373 * @param keeppreviousscan Flag used to save/clear scan table before scan
1375 * @return 0-success, otherwise fail
1377 int libertas_send_specific_ssid_scan(wlan_private * priv,
1378 u8 *ssid, u8 ssid_len, u8 clear_ssid)
1380 wlan_adapter *adapter = priv->adapter;
1381 struct wlan_ioctl_user_scan_cfg scancfg;
1384 lbs_deb_enter(LBS_DEB_ASSOC);
1389 memset(&scancfg, 0x00, sizeof(scancfg));
1390 memcpy(scancfg.ssid, ssid, ssid_len);
1391 scancfg.ssid_len = ssid_len;
1392 scancfg.clear_ssid = clear_ssid;
1394 wlan_scan_networks(priv, &scancfg, 1);
1395 if (adapter->surpriseremoved)
1397 wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);
1400 lbs_deb_leave(LBS_DEB_ASSOC);
1405 * @brief scan an AP with specific BSSID
1407 * @param priv A pointer to wlan_private structure
1408 * @param bssid A pointer to AP's bssid
1409 * @param keeppreviousscan Flag used to save/clear scan table before scan
1411 * @return 0-success, otherwise fail
1413 int libertas_send_specific_bssid_scan(wlan_private * priv, u8 * bssid, u8 clear_bssid)
1415 struct wlan_ioctl_user_scan_cfg scancfg;
1417 lbs_deb_enter(LBS_DEB_ASSOC);
1422 memset(&scancfg, 0x00, sizeof(scancfg));
1423 memcpy(scancfg.bssid, bssid, ETH_ALEN);
1424 scancfg.clear_bssid = clear_bssid;
1426 wlan_scan_networks(priv, &scancfg, 1);
1427 if (priv->adapter->surpriseremoved)
1429 wait_event_interruptible(priv->adapter->cmd_pending,
1430 !priv->adapter->nr_cmd_pending);
1433 lbs_deb_leave(LBS_DEB_ASSOC);
1437 static inline char *libertas_translate_scan(wlan_private *priv,
1438 char *start, char *stop,
1439 struct bss_descriptor *bss)
1441 wlan_adapter *adapter = priv->adapter;
1442 struct chan_freq_power *cfp;
1443 char *current_val; /* For rates */
1444 struct iw_event iwe; /* Temporary buffer */
1446 #define PERFECT_RSSI ((u8)50)
1447 #define WORST_RSSI ((u8)0)
1448 #define RSSI_DIFF ((u8)(PERFECT_RSSI - WORST_RSSI))
1451 cfp = libertas_find_cfp_by_band_and_channel(adapter, 0, bss->channel);
1453 lbs_deb_scan("Invalid channel number %d\n", bss->channel);
1457 /* First entry *MUST* be the AP BSSID */
1458 iwe.cmd = SIOCGIWAP;
1459 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1460 memcpy(iwe.u.ap_addr.sa_data, &bss->bssid, ETH_ALEN);
1461 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_ADDR_LEN);
1464 iwe.cmd = SIOCGIWESSID;
1465 iwe.u.data.flags = 1;
1466 iwe.u.data.length = min((u32) bss->ssid_len, (u32) IW_ESSID_MAX_SIZE);
1467 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
1470 iwe.cmd = SIOCGIWMODE;
1471 iwe.u.mode = bss->mode;
1472 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_UINT_LEN);
1475 iwe.cmd = SIOCGIWFREQ;
1476 iwe.u.freq.m = (long)cfp->freq * 100000;
1478 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_FREQ_LEN);
1480 /* Add quality statistics */
1482 iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
1483 iwe.u.qual.level = SCAN_RSSI(bss->rssi);
1485 rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
1487 (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
1488 (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
1489 (RSSI_DIFF * RSSI_DIFF);
1490 if (iwe.u.qual.qual > 100)
1491 iwe.u.qual.qual = 100;
1493 if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
1494 iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
1497 CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
1500 /* Locally created ad-hoc BSSs won't have beacons if this is the
1501 * only station in the adhoc network; so get signal strength
1502 * from receive statistics.
1504 if ((adapter->mode == IW_MODE_ADHOC)
1505 && adapter->adhoccreate
1506 && !libertas_ssid_cmp(adapter->curbssparams.ssid,
1507 adapter->curbssparams.ssid_len,
1508 bss->ssid, bss->ssid_len)) {
1510 snr = adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1511 nf = adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE;
1512 iwe.u.qual.level = CAL_RSSI(snr, nf);
1514 start = iwe_stream_add_event(start, stop, &iwe, IW_EV_QUAL_LEN);
1516 /* Add encryption capability */
1517 iwe.cmd = SIOCGIWENCODE;
1518 if (bss->capability & WLAN_CAPABILITY_PRIVACY) {
1519 iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
1521 iwe.u.data.flags = IW_ENCODE_DISABLED;
1523 iwe.u.data.length = 0;
1524 start = iwe_stream_add_point(start, stop, &iwe, bss->ssid);
1526 current_val = start + IW_EV_LCP_LEN;
1528 iwe.cmd = SIOCGIWRATE;
1529 iwe.u.bitrate.fixed = 0;
1530 iwe.u.bitrate.disabled = 0;
1531 iwe.u.bitrate.value = 0;
1533 for (j = 0; j < sizeof(bss->libertas_supported_rates); j++) {
1534 u8 rate = bss->libertas_supported_rates[j];
1536 break; /* no more rates */
1537 /* Bit rate given in 500 kb/s units (+ 0x80) */
1538 iwe.u.bitrate.value = (rate & 0x7f) * 500000;
1539 current_val = iwe_stream_add_value(start, current_val,
1540 stop, &iwe, IW_EV_PARAM_LEN);
1542 if ((bss->mode == IW_MODE_ADHOC)
1543 && !libertas_ssid_cmp(adapter->curbssparams.ssid,
1544 adapter->curbssparams.ssid_len,
1545 bss->ssid, bss->ssid_len)
1546 && adapter->adhoccreate) {
1547 iwe.u.bitrate.value = 22 * 500000;
1548 current_val = iwe_stream_add_value(start, current_val,
1549 stop, &iwe, IW_EV_PARAM_LEN);
1551 /* Check if we added any event */
1552 if((current_val - start) > IW_EV_LCP_LEN)
1553 start = current_val;
1555 memset(&iwe, 0, sizeof(iwe));
1556 if (bss->wpa_ie_len) {
1557 char buf[MAX_WPA_IE_LEN];
1558 memcpy(buf, bss->wpa_ie, bss->wpa_ie_len);
1559 iwe.cmd = IWEVGENIE;
1560 iwe.u.data.length = bss->wpa_ie_len;
1561 start = iwe_stream_add_point(start, stop, &iwe, buf);
1564 memset(&iwe, 0, sizeof(iwe));
1565 if (bss->rsn_ie_len) {
1566 char buf[MAX_WPA_IE_LEN];
1567 memcpy(buf, bss->rsn_ie, bss->rsn_ie_len);
1568 iwe.cmd = IWEVGENIE;
1569 iwe.u.data.length = bss->rsn_ie_len;
1570 start = iwe_stream_add_point(start, stop, &iwe, buf);
1577 * @brief Retrieve the scan table entries via wireless tools IOCTL call
1579 * @param dev A pointer to net_device structure
1580 * @param info A pointer to iw_request_info structure
1581 * @param dwrq A pointer to iw_point structure
1582 * @param extra A pointer to extra data buf
1584 * @return 0 --success, otherwise fail
1586 int libertas_get_scan(struct net_device *dev, struct iw_request_info *info,
1587 struct iw_point *dwrq, char *extra)
1589 #define SCAN_ITEM_SIZE 128
1590 wlan_private *priv = dev->priv;
1591 wlan_adapter *adapter = priv->adapter;
1594 char *stop = ev + dwrq->length;
1595 struct bss_descriptor * iter_bss;
1596 struct bss_descriptor * safe;
1598 lbs_deb_enter(LBS_DEB_ASSOC);
1600 /* If we've got an uncompleted scan, schedule the next part */
1601 if (!adapter->nr_cmd_pending && adapter->last_scanned_channel)
1602 wlan_scan_networks(priv, NULL, 0);
1604 /* Update RSSI if current BSS is a locally created ad-hoc BSS */
1605 if ((adapter->mode == IW_MODE_ADHOC) && adapter->adhoccreate) {
1606 libertas_prepare_and_send_command(priv, cmd_802_11_rssi, 0,
1607 cmd_option_waitforrsp, 0, NULL);
1610 mutex_lock(&adapter->lock);
1611 list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1613 unsigned long stale_time;
1615 if (stop - ev < SCAN_ITEM_SIZE) {
1620 /* For mesh device, list only mesh networks */
1621 if (dev == priv->mesh_dev && !iter_bss->mesh)
1624 /* Prune old an old scan result */
1625 stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1626 if (time_after(jiffies, stale_time)) {
1627 list_move_tail (&iter_bss->list,
1628 &adapter->network_free_list);
1629 clear_bss_descriptor(iter_bss);
1633 /* Translate to WE format this entry */
1634 next_ev = libertas_translate_scan(priv, ev, stop, iter_bss);
1635 if (next_ev == NULL)
1639 mutex_unlock(&adapter->lock);
1641 dwrq->length = (ev - extra);
1644 lbs_deb_leave(LBS_DEB_ASSOC);
1649 * @brief Prepare a scan command to be sent to the firmware
1651 * Use the wlan_scan_cmd_config sent to the command processing module in
1652 * the libertas_prepare_and_send_command to configure a cmd_ds_802_11_scan command
1653 * struct to send to firmware.
1655 * The fixed fields specifying the BSS type and BSSID filters as well as a
1656 * variable number/length of TLVs are sent in the command to firmware.
1658 * @param priv A pointer to wlan_private structure
1659 * @param cmd A pointer to cmd_ds_command structure to be sent to
1660 * firmware with the cmd_DS_801_11_SCAN structure
1661 * @param pdata_buf Void pointer cast of a wlan_scan_cmd_config struct used
1662 * to set the fields/TLVs for the command sent to firmware
1666 * @sa wlan_scan_create_channel_list
1668 int libertas_cmd_80211_scan(wlan_private * priv,
1669 struct cmd_ds_command *cmd, void *pdata_buf)
1671 struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
1672 struct wlan_scan_cmd_config *pscancfg;
1674 lbs_deb_enter(LBS_DEB_ASSOC);
1676 pscancfg = pdata_buf;
1678 /* Set fixed field variables in scan command */
1679 pscan->bsstype = pscancfg->bsstype;
1680 memcpy(pscan->BSSID, pscancfg->bssid, sizeof(pscan->BSSID));
1681 memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);
1683 cmd->command = cpu_to_le16(cmd_802_11_scan);
1685 /* size is equal to the sizeof(fixed portions) + the TLV len + header */
1686 cmd->size = cpu_to_le16(sizeof(pscan->bsstype)
1687 + sizeof(pscan->BSSID)
1688 + pscancfg->tlvbufferlen + S_DS_GEN);
1690 lbs_deb_scan("SCAN_CMD: command=%x, size=%x, seqnum=%x\n",
1691 le16_to_cpu(cmd->command), le16_to_cpu(cmd->size),
1692 le16_to_cpu(cmd->seqnum));
1694 lbs_deb_leave(LBS_DEB_ASSOC);
1698 static inline int is_same_network(struct bss_descriptor *src,
1699 struct bss_descriptor *dst)
1701 /* A network is only a duplicate if the channel, BSSID, and ESSID
1702 * all match. We treat all <hidden> with the same BSSID and channel
1704 return ((src->ssid_len == dst->ssid_len) &&
1705 (src->channel == dst->channel) &&
1706 !compare_ether_addr(src->bssid, dst->bssid) &&
1707 !memcmp(src->ssid, dst->ssid, src->ssid_len));
1711 * @brief This function handles the command response of scan
1713 * The response buffer for the scan command has the following
1716 * .-----------------------------------------------------------.
1717 * | header (4 * sizeof(u16)): Standard command response hdr |
1718 * .-----------------------------------------------------------.
1719 * | bufsize (u16) : sizeof the BSS Description data |
1720 * .-----------------------------------------------------------.
1721 * | NumOfSet (u8) : Number of BSS Descs returned |
1722 * .-----------------------------------------------------------.
1723 * | BSSDescription data (variable, size given in bufsize) |
1724 * .-----------------------------------------------------------.
1725 * | TLV data (variable, size calculated using header->size, |
1726 * | bufsize and sizeof the fixed fields above) |
1727 * .-----------------------------------------------------------.
1729 * @param priv A pointer to wlan_private structure
1730 * @param resp A pointer to cmd_ds_command
1734 int libertas_ret_80211_scan(wlan_private * priv, struct cmd_ds_command *resp)
1736 wlan_adapter *adapter = priv->adapter;
1737 struct cmd_ds_802_11_scan_rsp *pscan;
1738 struct mrvlietypes_data *ptlv;
1739 struct mrvlietypes_tsftimestamp *ptsftlv;
1740 struct bss_descriptor * iter_bss;
1741 struct bss_descriptor * safe;
1749 lbs_deb_enter(LBS_DEB_ASSOC);
1751 /* Prune old entries from scan table */
1752 list_for_each_entry_safe (iter_bss, safe, &adapter->network_list, list) {
1753 unsigned long stale_time = iter_bss->last_scanned + DEFAULT_MAX_SCAN_AGE;
1754 if (time_before(jiffies, stale_time))
1756 list_move_tail (&iter_bss->list, &adapter->network_free_list);
1757 clear_bss_descriptor(iter_bss);
1760 pscan = &resp->params.scanresp;
1762 if (pscan->nr_sets > MAX_NETWORK_COUNT) {
1764 "SCAN_RESP: too many scan results (%d, max %d)!!\n",
1765 pscan->nr_sets, MAX_NETWORK_COUNT);
1770 bytesleft = le16_to_cpu(pscan->bssdescriptsize);
1771 lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
1773 scanrespsize = le16_to_cpu(resp->size);
1774 lbs_deb_scan("SCAN_RESP: returned %d AP before parsing\n",
1777 pbssinfo = pscan->bssdesc_and_tlvbuffer;
1779 /* The size of the TLV buffer is equal to the entire command response
1780 * size (scanrespsize) minus the fixed fields (sizeof()'s), the
1781 * BSS Descriptions (bssdescriptsize as bytesLef) and the command
1782 * response header (S_DS_GEN)
1784 tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
1785 + sizeof(pscan->nr_sets)
1788 ptlv = (struct mrvlietypes_data *) (pscan->bssdesc_and_tlvbuffer + bytesleft);
1790 /* Search the TLV buffer space in the scan response for any valid TLVs */
1791 wlan_ret_802_11_scan_get_tlv_ptrs(ptlv, tlvbufsize, &ptsftlv);
1794 * Process each scan response returned (pscan->nr_sets). Save
1795 * the information in the newbssentry and then insert into the
1796 * driver scan table either as an update to an existing entry
1797 * or as an addition at the end of the table
1799 for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
1800 struct bss_descriptor new;
1801 struct bss_descriptor * found = NULL;
1802 struct bss_descriptor * oldest = NULL;
1804 /* Process the data fields and IEs returned for this BSS */
1805 memset(&new, 0, sizeof (struct bss_descriptor));
1806 if (libertas_process_bss(&new, &pbssinfo, &bytesleft) != 0) {
1807 /* error parsing the scan response, skipped */
1808 lbs_deb_scan("SCAN_RESP: process_bss returned ERROR\n");
1812 /* Try to find this bss in the scan table */
1813 list_for_each_entry (iter_bss, &adapter->network_list, list) {
1814 if (is_same_network(iter_bss, &new)) {
1819 if ((oldest == NULL) ||
1820 (iter_bss->last_scanned < oldest->last_scanned))
1825 /* found, clear it */
1826 clear_bss_descriptor(found);
1827 } else if (!list_empty(&adapter->network_free_list)) {
1828 /* Pull one from the free list */
1829 found = list_entry(adapter->network_free_list.next,
1830 struct bss_descriptor, list);
1831 list_move_tail(&found->list, &adapter->network_list);
1832 } else if (oldest) {
1833 /* If there are no more slots, expire the oldest */
1835 clear_bss_descriptor(found);
1836 list_move_tail(&found->list, &adapter->network_list);
1841 lbs_deb_scan("SCAN_RESP: BSSID = " MAC_FMT "\n",
1842 new.bssid[0], new.bssid[1], new.bssid[2],
1843 new.bssid[3], new.bssid[4], new.bssid[5]);
1846 * If the TSF TLV was appended to the scan results, save the
1847 * this entries TSF value in the networktsf field. The
1848 * networktsf is the firmware's TSF value at the time the
1849 * beacon or probe response was received.
1852 new.networktsf = le64_to_cpup(&ptsftlv->tsftable[idx]);
1855 /* Copy the locally created newbssentry to the scan table */
1856 memcpy(found, &new, offsetof(struct bss_descriptor, list));
1862 lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);