scan.c 60.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/**
  * Functions implementing wlan scan IOCTL and firmware command APIs
  *
  * IOCTL handlers as well as command preperation and response routines
  *  for sending scan commands to the firmware.
  */
#include <linux/ctype.h>
#include <linux/if.h>
#include <linux/netdevice.h>
#include <linux/wireless.h>

#include <net/ieee80211.h>
#include <net/iw_handler.h>

#include "host.h"
#include "decl.h"
#include "dev.h"
#include "scan.h"

//! Approximate amount of data needed to pass a scan result back to iwlist
#define MAX_SCAN_CELL_SIZE  (IW_EV_ADDR_LEN             \
                             + IW_ESSID_MAX_SIZE        \
                             + IW_EV_UINT_LEN           \
                             + IW_EV_FREQ_LEN           \
                             + IW_EV_QUAL_LEN           \
                             + IW_ESSID_MAX_SIZE        \
                             + IW_EV_PARAM_LEN          \
                             + 40)	/* 40 for WPAIE */

//! Memory needed to store a max sized channel List TLV for a firmware scan
#define CHAN_TLV_MAX_SIZE  (sizeof(struct mrvlietypesheader)    \
                            + (MRVDRV_MAX_CHANNELS_PER_SCAN     \
                               * sizeof(struct chanscanparamset)))

//! Memory needed to store a max number/size SSID TLV for a firmware scan
#define SSID_TLV_MAX_SIZE  (1 * sizeof(struct mrvlietypes_ssidparamset))

//! Maximum memory needed for a wlan_scan_cmd_config with all TLVs at max
#define MAX_SCAN_CFG_ALLOC (sizeof(struct wlan_scan_cmd_config)  \
                            + sizeof(struct mrvlietypes_numprobes)   \
                            + CHAN_TLV_MAX_SIZE                 \
                            + SSID_TLV_MAX_SIZE)

//! The maximum number of channels the firmware can scan per command
#define MRVDRV_MAX_CHANNELS_PER_SCAN   14

/**
 * @brief Number of channels to scan per firmware scan command issuance.
 *
 *  Number restricted to prevent hitting the limit on the amount of scan data
 *  returned in a single firmware scan command.
 */
#define MRVDRV_CHANNELS_PER_SCAN_CMD   4

//! Scan time specified in the channel TLV for each channel for passive scans
#define MRVDRV_PASSIVE_SCAN_CHAN_TIME  100

//! Scan time specified in the channel TLV for each channel for active scans
#define MRVDRV_ACTIVE_SCAN_CHAN_TIME   100

//! Macro to enable/disable SSID checking before storing a scan table
#ifdef DISCARD_BAD_SSID
#define CHECK_SSID_IS_VALID(x) ssid_valid(&bssidEntry.ssid)
#else
#define CHECK_SSID_IS_VALID(x) 1
#endif

/**
 *  @brief Check if a scanned network compatible with the driver settings
 *
 *   WEP     WPA     WPA2    ad-hoc  encrypt                      Network
 * enabled enabled  enabled   AES     mode   privacy  WPA  WPA2  Compatible
 *    0       0        0       0      NONE      0      0    0   yes No security
 *    1       0        0       0      NONE      1      0    0   yes Static WEP
 *    0       1        0       0       x        1x     1    x   yes WPA
 *    0       0        1       0       x        1x     x    1   yes WPA2
 *    0       0        0       1      NONE      1      0    0   yes Ad-hoc AES
 *    0       0        0       0     !=NONE     1      0    0   yes Dynamic WEP
 *
 *
 *  @param adapter A pointer to wlan_adapter
 *  @param index   Index in scantable to check against current driver settings
 *  @param mode    Network mode: Infrastructure or IBSS
 *
 *  @return        Index in scantable, or error code if negative
 */
87
static int is_network_compatible(wlan_adapter * adapter, int index, u8 mode)
88
{
89
	lbs_deb_enter(LBS_DEB_ASSOC);
90

91
	if (adapter->scantable[index].mode == mode) {
92
		if (   !adapter->secinfo.wep_enabled
93 94
		    && !adapter->secinfo.WPAenabled
		    && !adapter->secinfo.WPA2enabled
95 96
		    && adapter->scantable[index].wpa_ie[0] != WPA_IE
		    && adapter->scantable[index].rsn_ie[0] != WPA2_IE
97 98
		    && !adapter->scantable[index].privacy) {
			/* no security */
99
			goto done;
100
		} else if (   adapter->secinfo.wep_enabled
101 102 103 104
			   && !adapter->secinfo.WPAenabled
			   && !adapter->secinfo.WPA2enabled
			   && adapter->scantable[index].privacy) {
			/* static WEP enabled */
105
			goto done;
106
		} else if (   !adapter->secinfo.wep_enabled
107 108
			   && adapter->secinfo.WPAenabled
			   && !adapter->secinfo.WPA2enabled
109
			   && (adapter->scantable[index].wpa_ie[0] == WPA_IE)
110 111 112 113
			   /* privacy bit may NOT be set in some APs like LinkSys WRT54G
			      && adapter->scantable[index].privacy */
		    ) {
			/* WPA enabled */
114
            lbs_deb_scan(
115
			       "is_network_compatible() WPA: index=%d wpa_ie=%#x "
116
			       "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
117
			       "privacy=%#x\n", index,
118 119
			       adapter->scantable[index].wpa_ie[0],
			       adapter->scantable[index].rsn_ie[0],
120 121 122
			       adapter->secinfo.wep_enabled ? "e" : "d",
			       adapter->secinfo.WPAenabled ? "e" : "d",
			       adapter->secinfo.WPA2enabled ? "e" : "d",
123
			       adapter->scantable[index].privacy);
124
			goto done;
125
		} else if (   !adapter->secinfo.wep_enabled
126 127
			   && !adapter->secinfo.WPAenabled
			   && adapter->secinfo.WPA2enabled
128
			   && (adapter->scantable[index].rsn_ie[0] == WPA2_IE)
129 130 131 132
			   /* privacy bit may NOT be set in some APs like LinkSys WRT54G
			      && adapter->scantable[index].privacy */
		    ) {
			/* WPA2 enabled */
133
            lbs_deb_scan(
134
			       "is_network_compatible() WPA2: index=%d wpa_ie=%#x "
135
			       "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
136
			       "privacy=%#x\n", index,
137 138
			       adapter->scantable[index].wpa_ie[0],
			       adapter->scantable[index].rsn_ie[0],
139 140 141
			       adapter->secinfo.wep_enabled ? "e" : "d",
			       adapter->secinfo.WPAenabled ? "e" : "d",
			       adapter->secinfo.WPA2enabled ? "e" : "d",
142
			       adapter->scantable[index].privacy);
143
			goto done;
144
		} else if (   !adapter->secinfo.wep_enabled
145 146
			   && !adapter->secinfo.WPAenabled
			   && !adapter->secinfo.WPA2enabled
147 148
			   && (adapter->scantable[index].wpa_ie[0] != WPA_IE)
			   && (adapter->scantable[index].rsn_ie[0] != WPA2_IE)
149 150
			   && adapter->scantable[index].privacy) {
			/* dynamic WEP enabled */
151
            lbs_deb_scan(
152
			       "is_network_compatible() dynamic WEP: index=%d "
153
			       "wpa_ie=%#x wpa2_ie=%#x privacy=%#x\n",
154
			       index,
155 156
			       adapter->scantable[index].wpa_ie[0],
			       adapter->scantable[index].rsn_ie[0],
157
			       adapter->scantable[index].privacy);
158
			goto done;
159 160 161
		}

		/* security doesn't match */
162
        lbs_deb_scan(
163
		       "is_network_compatible() FAILED: index=%d wpa_ie=%#x "
164
		       "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s privacy=%#x\n",
165
		       index,
166 167
		       adapter->scantable[index].wpa_ie[0],
		       adapter->scantable[index].rsn_ie[0],
168 169 170
		       adapter->secinfo.wep_enabled ? "e" : "d",
		       adapter->secinfo.WPAenabled ? "e" : "d",
		       adapter->secinfo.WPA2enabled ? "e" : "d",
171
		       adapter->scantable[index].privacy);
172 173
		index = -ECONNREFUSED;
		goto done;
174 175 176
	}

	/* mode doesn't match */
177 178 179 180 181
	index = -ENETUNREACH;

done:
	lbs_deb_leave_args(LBS_DEB_SCAN, "index %d", index);
	return index;
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
}

/**
 *  @brief This function validates a SSID as being able to be printed
 *
 *  @param pssid   SSID structure to validate
 *
 *  @return        TRUE or FALSE
 */
static u8 ssid_valid(struct WLAN_802_11_SSID *pssid)
{
	int ssididx;

	for (ssididx = 0; ssididx < pssid->ssidlength; ssididx++) {
		if (!isprint(pssid->ssid[ssididx])) {
			return 0;
		}
	}

	return 1;
}

/**
 *  @brief Post process the scan table after a new scan command has completed
 *
 *  Inspect each entry of the scan table and try to find an entry that
 *    matches our current associated/joined network from the scan.  If
 *    one is found, update the stored copy of the bssdescriptor for our
 *    current network.
 *
 *  Debug dump the current scan table contents if compiled accordingly.
 *
 *  @param priv   A pointer to wlan_private structure
 *
 *  @return       void
 */
static void wlan_scan_process_results(wlan_private * priv)
{
	wlan_adapter *adapter = priv->adapter;
	int foundcurrent;
	int i;

	foundcurrent = 0;

	if (adapter->connect_status == libertas_connected) {
		/* try to find the current BSSID in the new scan list */
		for (i = 0; i < adapter->numinscantable; i++) {
			if (!libertas_SSID_cmp(&adapter->scantable[i].ssid,
				     &adapter->curbssparams.ssid) &&
			    !memcmp(adapter->curbssparams.bssid,
				    adapter->scantable[i].macaddress,
				    ETH_ALEN)) {
				foundcurrent = 1;
			}
		}

		if (foundcurrent) {
			/* Make a copy of current BSSID descriptor */
			memcpy(&adapter->curbssparams.bssdescriptor,
			       &adapter->scantable[i],
			       sizeof(adapter->curbssparams.bssdescriptor));
		}
	}

	for (i = 0; i < adapter->numinscantable; i++) {
247
		lbs_deb_scan("Scan:(%02d) %02x:%02x:%02x:%02x:%02x:%02x, "
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
		       "RSSI[%03d], SSID[%s]\n",
		       i,
		       adapter->scantable[i].macaddress[0],
		       adapter->scantable[i].macaddress[1],
		       adapter->scantable[i].macaddress[2],
		       adapter->scantable[i].macaddress[3],
		       adapter->scantable[i].macaddress[4],
		       adapter->scantable[i].macaddress[5],
		       (s32) adapter->scantable[i].rssi,
		       adapter->scantable[i].ssid.ssid);
	}
}

/**
 *  @brief Create a channel list for the driver to scan based on region info
 *
 *  Use the driver region/band information to construct a comprehensive list
 *    of channels to scan.  This routine is used for any scan that is not
 *    provided a specific channel list to scan.
 *
 *  @param priv          A pointer to wlan_private structure
 *  @param scanchanlist  Output parameter: resulting channel list to scan
 *  @param filteredscan  Flag indicating whether or not a BSSID or SSID filter
 *                       is being sent in the command to firmware.  Used to
 *                       increase the number of channels sent in a scan
 *                       command and to disable the firmware channel scan
 *                       filter.
 *
 *  @return              void
 */
static void wlan_scan_create_channel_list(wlan_private * priv,
					  struct chanscanparamset * scanchanlist,
					  u8 filteredscan)
{

	wlan_adapter *adapter = priv->adapter;
	struct region_channel *scanregion;
	struct chan_freq_power *cfp;
	int rgnidx;
	int chanidx;
	int nextchan;
	u8 scantype;

	chanidx = 0;

	/* Set the default scan type to the user specified type, will later
	 *   be changed to passive on a per channel basis if restricted by
	 *   regulatory requirements (11d or 11h)
	 */
	scantype = adapter->scantype;

	for (rgnidx = 0; rgnidx < ARRAY_SIZE(adapter->region_channel); rgnidx++) {
		if (priv->adapter->enable11d &&
		    adapter->connect_status != libertas_connected) {
			/* Scan all the supported chan for the first scan */
			if (!adapter->universal_channel[rgnidx].valid)
				continue;
			scanregion = &adapter->universal_channel[rgnidx];

			/* clear the parsed_region_chan for the first scan */
			memset(&adapter->parsed_region_chan, 0x00,
			       sizeof(adapter->parsed_region_chan));
		} else {
			if (!adapter->region_channel[rgnidx].valid)
				continue;
			scanregion = &adapter->region_channel[rgnidx];
		}

		for (nextchan = 0;
		     nextchan < scanregion->nrcfp; nextchan++, chanidx++) {

			cfp = scanregion->CFP + nextchan;

			if (priv->adapter->enable11d) {
				scantype =
				    libertas_get_scan_type_11d(cfp->channel,
							   &adapter->
							   parsed_region_chan);
			}

			switch (scanregion->band) {
			case BAND_B:
			case BAND_G:
			default:
				scanchanlist[chanidx].radiotype =
				    cmd_scan_radio_type_bg;
				break;
			}

			if (scantype == cmd_scan_type_passive) {
				scanchanlist[chanidx].maxscantime =
				    cpu_to_le16
				    (MRVDRV_PASSIVE_SCAN_CHAN_TIME);
				scanchanlist[chanidx].chanscanmode.passivescan =
				    1;
			} else {
				scanchanlist[chanidx].maxscantime =
				    cpu_to_le16
				    (MRVDRV_ACTIVE_SCAN_CHAN_TIME);
				scanchanlist[chanidx].chanscanmode.passivescan =
				    0;
			}

			scanchanlist[chanidx].channumber = cfp->channel;

			if (filteredscan) {
				scanchanlist[chanidx].chanscanmode.
				    disablechanfilt = 1;
			}
		}
	}
}

/**
 *  @brief Construct a wlan_scan_cmd_config structure to use in issue scan cmds
 *
 *  Application layer or other functions can invoke wlan_scan_networks
 *    with a scan configuration supplied in a wlan_ioctl_user_scan_cfg struct.
 *    This structure is used as the basis of one or many wlan_scan_cmd_config
 *    commands that are sent to the command processing module and sent to
 *    firmware.
 *
 *  Create a wlan_scan_cmd_config based on the following user supplied
 *    parameters (if present):
 *             - SSID filter
 *             - BSSID filter
 *             - Number of Probes to be sent
 *             - channel list
 *
 *  If the SSID or BSSID filter is not present, disable/clear the filter.
 *  If the number of probes is not set, use the adapter default setting
 *  Qualify the channel
 *
 *  @param priv             A pointer to wlan_private structure
 *  @param puserscanin      NULL or pointer to scan configuration parameters
 *  @param ppchantlvout     Output parameter: Pointer to the start of the
 *                          channel TLV portion of the output scan config
 *  @param pscanchanlist    Output parameter: Pointer to the resulting channel
 *                          list to scan
 *  @param pmaxchanperscan  Output parameter: Number of channels to scan for
 *                          each issuance of the firmware scan command
 *  @param pfilteredscan    Output parameter: Flag indicating whether or not
 *                          a BSSID or SSID filter is being sent in the
 *                          command to firmware.  Used to increase the number
 *                          of channels sent in a scan command and to
 *                          disable the firmware channel scan filter.
 *  @param pscancurrentonly Output parameter: Flag indicating whether or not
 *                          we are only scanning our current active channel
 *
 *  @return                 resulting scan configuration
 */
static struct wlan_scan_cmd_config *
wlan_scan_setup_scan_config(wlan_private * priv,
			    const struct wlan_ioctl_user_scan_cfg * puserscanin,
			    struct mrvlietypes_chanlistparamset ** ppchantlvout,
			    struct chanscanparamset * pscanchanlist,
			    int *pmaxchanperscan,
			    u8 * pfilteredscan,
			    u8 * pscancurrentonly)
{
	wlan_adapter *adapter = priv->adapter;
	const u8 zeromac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
	struct mrvlietypes_numprobes *pnumprobestlv;
	struct mrvlietypes_ssidparamset *pssidtlv;
	struct wlan_scan_cmd_config * pscancfgout = NULL;
	u8 *ptlvpos;
	u16 numprobes;
	u16 ssidlen;
	int chanidx;
	int scantype;
	int scandur;
	int channel;
	int radiotype;

	pscancfgout = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
	if (pscancfgout == NULL)
		goto out;

	/* The tlvbufferlen is calculated for each scan command.  The TLVs added
	 *   in this routine will be preserved since the routine that sends
	 *   the command will append channelTLVs at *ppchantlvout.  The difference
	 *   between the *ppchantlvout and the tlvbuffer start will be used
	 *   to calculate the size of anything we add in this routine.
	 */
	pscancfgout->tlvbufferlen = 0;

	/* Running tlv pointer.  Assigned to ppchantlvout at end of function
	 *  so later routines know where channels can be added to the command buf
	 */
	ptlvpos = pscancfgout->tlvbuffer;

	/*
	 * Set the initial scan paramters for progressive scanning.  If a specific
	 *   BSSID or SSID is used, the number of channels in the scan command
	 *   will be increased to the absolute maximum
	 */
	*pmaxchanperscan = MRVDRV_CHANNELS_PER_SCAN_CMD;

	/* Initialize the scan as un-filtered by firmware, set to TRUE below if
	 *   a SSID or BSSID filter is sent in the command
	 */
	*pfilteredscan = 0;

	/* Initialize the scan as not being only on the current channel.  If
	 *   the channel list is customized, only contains one channel, and
	 *   is the active channel, this is set true and data flow is not halted.
	 */
	*pscancurrentonly = 0;

	if (puserscanin) {

		/* Set the bss type scan filter, use adapter setting if unset */
		pscancfgout->bsstype =
		    (puserscanin->bsstype ? puserscanin->bsstype : adapter->
		     scanmode);

		/* Set the number of probes to send, use adapter setting if unset */
		numprobes = (puserscanin->numprobes ? puserscanin->numprobes :
			     adapter->scanprobes);

		/*
		 * Set the BSSID filter to the incoming configuration,
		 *   if non-zero.  If not set, it will remain disabled (all zeros).
		 */
		memcpy(pscancfgout->specificBSSID,
		       puserscanin->specificBSSID,
		       sizeof(pscancfgout->specificBSSID));

		ssidlen = strlen(puserscanin->specificSSID);

		if (ssidlen) {
			pssidtlv =
			    (struct mrvlietypes_ssidparamset *) pscancfgout->
			    tlvbuffer;
			pssidtlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
			pssidtlv->header.len = cpu_to_le16(ssidlen);
			memcpy(pssidtlv->ssid, puserscanin->specificSSID,
			       ssidlen);
			ptlvpos += sizeof(pssidtlv->header) + ssidlen;
		}

		/*
		 *  The default number of channels sent in the command is low to
		 *    ensure the response buffer from the firmware does not truncate
		 *    scan results.  That is not an issue with an SSID or BSSID
		 *    filter applied to the scan results in the firmware.
		 */
		if (ssidlen || (memcmp(pscancfgout->specificBSSID,
				       &zeromac, sizeof(zeromac)) != 0)) {
			*pmaxchanperscan = MRVDRV_MAX_CHANNELS_PER_SCAN;
			*pfilteredscan = 1;
		}
	} else {
		pscancfgout->bsstype = adapter->scanmode;
		numprobes = adapter->scanprobes;
	}

	/* If the input config or adapter has the number of Probes set, add tlv */
	if (numprobes) {
		pnumprobestlv = (struct mrvlietypes_numprobes *) ptlvpos;
		pnumprobestlv->header.type =
		    cpu_to_le16(TLV_TYPE_NUMPROBES);
		pnumprobestlv->header.len = sizeof(pnumprobestlv->numprobes);
		pnumprobestlv->numprobes = cpu_to_le16(numprobes);

		ptlvpos +=
		    sizeof(pnumprobestlv->header) + pnumprobestlv->header.len;

		pnumprobestlv->header.len =
		    cpu_to_le16(pnumprobestlv->header.len);
	}

	/*
	 * Set the output for the channel TLV to the address in the tlv buffer
	 *   past any TLVs that were added in this fuction (SSID, numprobes).
	 *   channel TLVs will be added past this for each scan command, preserving
	 *   the TLVs that were previously added.
	 */
	*ppchantlvout = (struct mrvlietypes_chanlistparamset *) ptlvpos;

	if (puserscanin && puserscanin->chanlist[0].channumber) {

530
		lbs_deb_scan("Scan: Using supplied channel list\n");
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573

		for (chanidx = 0;
		     chanidx < WLAN_IOCTL_USER_SCAN_CHAN_MAX
		     && puserscanin->chanlist[chanidx].channumber; chanidx++) {

			channel = puserscanin->chanlist[chanidx].channumber;
			(pscanchanlist + chanidx)->channumber = channel;

			radiotype = puserscanin->chanlist[chanidx].radiotype;
			(pscanchanlist + chanidx)->radiotype = radiotype;

			scantype = puserscanin->chanlist[chanidx].scantype;

			if (scantype == cmd_scan_type_passive) {
				(pscanchanlist +
				 chanidx)->chanscanmode.passivescan = 1;
			} else {
				(pscanchanlist +
				 chanidx)->chanscanmode.passivescan = 0;
			}

			if (puserscanin->chanlist[chanidx].scantime) {
				scandur =
				    puserscanin->chanlist[chanidx].scantime;
			} else {
				if (scantype == cmd_scan_type_passive) {
					scandur = MRVDRV_PASSIVE_SCAN_CHAN_TIME;
				} else {
					scandur = MRVDRV_ACTIVE_SCAN_CHAN_TIME;
				}
			}

			(pscanchanlist + chanidx)->minscantime =
			    cpu_to_le16(scandur);
			(pscanchanlist + chanidx)->maxscantime =
			    cpu_to_le16(scandur);
		}

		/* Check if we are only scanning the current channel */
		if ((chanidx == 1) && (puserscanin->chanlist[0].channumber
				       ==
				       priv->adapter->curbssparams.channel)) {
			*pscancurrentonly = 1;
574
			lbs_deb_scan("Scan: Scanning current channel only");
575 576 577
		}

	} else {
578
		lbs_deb_scan("Scan: Creating full region channel list\n");
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
		wlan_scan_create_channel_list(priv, pscanchanlist,
					      *pfilteredscan);
	}

out:
	return pscancfgout;
}

/**
 *  @brief Construct and send multiple scan config commands to the firmware
 *
 *  Previous routines have created a wlan_scan_cmd_config with any requested
 *   TLVs.  This function splits the channel TLV into maxchanperscan lists
 *   and sends the portion of the channel TLV along with the other TLVs
 *   to the wlan_cmd routines for execution in the firmware.
 *
 *  @param priv            A pointer to wlan_private structure
 *  @param maxchanperscan  Maximum number channels to be included in each
 *                         scan command sent to firmware
 *  @param filteredscan    Flag indicating whether or not a BSSID or SSID
 *                         filter is being used for the firmware command
 *                         scan command sent to firmware
 *  @param pscancfgout     Scan configuration used for this scan.
 *  @param pchantlvout     Pointer in the pscancfgout where the channel TLV
 *                         should start.  This is past any other TLVs that
 *                         must be sent down in each firmware command.
 *  @param pscanchanlist   List of channels to scan in maxchanperscan segments
 *
 *  @return                0 or error return otherwise
 */
static int wlan_scan_channel_list(wlan_private * priv,
				  int maxchanperscan,
				  u8 filteredscan,
				  struct wlan_scan_cmd_config * pscancfgout,
				  struct mrvlietypes_chanlistparamset * pchantlvout,
614
				  struct chanscanparamset * pscanchanlist,
615 616
				  const struct wlan_ioctl_user_scan_cfg * puserscanin,
				  int full_scan)
617 618 619 620 621 622 623
{
	struct chanscanparamset *ptmpchan;
	struct chanscanparamset *pstartchan;
	u8 scanband;
	int doneearly;
	int tlvidx;
	int ret = 0;
624 625
	int scanned = 0;
	union iwreq_data wrqu;
626

627
	lbs_deb_enter(LBS_DEB_ASSOC);
628 629

	if (pscancfgout == 0 || pchantlvout == 0 || pscanchanlist == 0) {
630
		lbs_deb_scan("Scan: Null detect: %p, %p, %p\n",
631 632 633 634 635 636 637 638 639
		       pscancfgout, pchantlvout, pscanchanlist);
		return -1;
	}

	pchantlvout->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);

	/* Set the temp channel struct pointer to the start of the desired list */
	ptmpchan = pscanchanlist;

640 641 642
	if (priv->adapter->last_scanned_channel && !puserscanin)
		ptmpchan += priv->adapter->last_scanned_channel;

643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
	/* Loop through the desired channel list, sending a new firmware scan
	 *   commands for each maxchanperscan channels (or for 1,6,11 individually
	 *   if configured accordingly)
	 */
	while (ptmpchan->channumber) {

		tlvidx = 0;
		pchantlvout->header.len = 0;
		scanband = ptmpchan->radiotype;
		pstartchan = ptmpchan;
		doneearly = 0;

		/* Construct the channel TLV for the scan command.  Continue to
		 *  insert channel TLVs until:
		 *    - the tlvidx hits the maximum configured per scan command
		 *    - the next channel to insert is 0 (end of desired channel list)
		 *    - doneearly is set (controlling individual scanning of 1,6,11)
		 */
		while (tlvidx < maxchanperscan && ptmpchan->channumber
662
		       && !doneearly && scanned < 2) {
663

664
            lbs_deb_scan(
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
                    "Scan: Chan(%3d), Radio(%d), mode(%d,%d), Dur(%d)\n",
                ptmpchan->channumber, ptmpchan->radiotype,
                ptmpchan->chanscanmode.passivescan,
                ptmpchan->chanscanmode.disablechanfilt,
                ptmpchan->maxscantime);

			/* Copy the current channel TLV to the command being prepared */
			memcpy(pchantlvout->chanscanparam + tlvidx,
			       ptmpchan, sizeof(pchantlvout->chanscanparam));

			/* Increment the TLV header length by the size appended */
			pchantlvout->header.len +=
			    sizeof(pchantlvout->chanscanparam);

			/*
			 *  The tlv buffer length is set to the number of bytes of the
			 *    between the channel tlv pointer and the start of the
			 *    tlv buffer.  This compensates for any TLVs that were appended
			 *    before the channel list.
			 */
			pscancfgout->tlvbufferlen = ((u8 *) pchantlvout
						     - pscancfgout->tlvbuffer);

			/*  Add the size of the channel tlv header and the data length */
			pscancfgout->tlvbufferlen +=
			    (sizeof(pchantlvout->header)
			     + pchantlvout->header.len);

			/* Increment the index to the channel tlv we are constructing */
			tlvidx++;

			doneearly = 0;

			/* Stop the loop if the *current* channel is in the 1,6,11 set
			 *   and we are not filtering on a BSSID or SSID.
			 */
			if (!filteredscan && (ptmpchan->channumber == 1
					      || ptmpchan->channumber == 6
					      || ptmpchan->channumber == 11)) {
				doneearly = 1;
			}

			/* Increment the tmp pointer to the next channel to be scanned */
			ptmpchan++;
709
			scanned++;
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724

			/* Stop the loop if the *next* channel is in the 1,6,11 set.
			 *  This will cause it to be the only channel scanned on the next
			 *  interation
			 */
			if (!filteredscan && (ptmpchan->channumber == 1
					      || ptmpchan->channumber == 6
					      || ptmpchan->channumber == 11)) {
				doneearly = 1;
			}
		}

		/* Send the scan command to the firmware with the specified cfg */
		ret = libertas_prepare_and_send_command(priv, cmd_802_11_scan, 0,
					    0, 0, pscancfgout);
725
		if (scanned >= 2 && !full_scan) {
726
			priv->adapter->last_scanned_channel = ptmpchan->channumber;
727 728
			ret = 0;
			goto done;
729
		}
730
		scanned = 0;
731 732
	}

733 734 735 736 737
	priv->adapter->last_scanned_channel = ptmpchan->channumber;

	memset(&wrqu, 0, sizeof(union iwreq_data));
	wireless_send_event(priv->wlan_dev.netdev, SIOCGIWSCAN, &wrqu, NULL);

738 739
done:
	lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
	return ret;
}

/**
 *  @brief Internal function used to start a scan based on an input config
 *
 *  Use the input user scan configuration information when provided in
 *    order to send the appropriate scan commands to firmware to populate or
 *    update the internal driver scan table
 *
 *  @param priv          A pointer to wlan_private structure
 *  @param puserscanin   Pointer to the input configuration for the requested
 *                       scan.
 *
 *  @return              0 or < 0 if error
 */
int wlan_scan_networks(wlan_private * priv,
757 758
			      const struct wlan_ioctl_user_scan_cfg * puserscanin,
			      int full_scan)
759 760 761 762 763 764 765 766 767 768 769
{
	wlan_adapter *adapter = priv->adapter;
	struct mrvlietypes_chanlistparamset *pchantlvout;
	struct chanscanparamset * scan_chan_list = NULL;
	struct wlan_scan_cmd_config * scan_cfg = NULL;
	u8 keeppreviousscan;
	u8 filteredscan;
	u8 scancurrentchanonly;
	int maxchanperscan;
	int ret;

770
	lbs_deb_enter(LBS_DEB_ASSOC);
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796

	scan_chan_list = kzalloc(sizeof(struct chanscanparamset) *
				WLAN_IOCTL_USER_SCAN_CHAN_MAX, GFP_KERNEL);
	if (scan_chan_list == NULL) {
		ret = -ENOMEM;
		goto out;
	}

	scan_cfg = wlan_scan_setup_scan_config(priv,
					       puserscanin,
					       &pchantlvout,
					       scan_chan_list,
					       &maxchanperscan,
					       &filteredscan,
					       &scancurrentchanonly);
	if (scan_cfg == NULL) {
		ret = -ENOMEM;
		goto out;
	}

	keeppreviousscan = 0;

	if (puserscanin) {
		keeppreviousscan = puserscanin->keeppreviousscan;
	}

797 798 799
	if (adapter->last_scanned_channel)
		keeppreviousscan = 1;

800 801 802 803 804 805 806 807 808 809
	if (!keeppreviousscan) {
		memset(adapter->scantable, 0x00,
		       sizeof(struct bss_descriptor) * MRVDRV_MAX_BSSID_LIST);
		adapter->numinscantable = 0;
	}

	/* Keep the data path active if we are only scanning our current channel */
	if (!scancurrentchanonly) {
		netif_stop_queue(priv->wlan_dev.netdev);
		netif_carrier_off(priv->wlan_dev.netdev);
810 811
		netif_stop_queue(priv->mesh_dev);
		netif_carrier_off(priv->mesh_dev);
812 813 814 815 816 817 818
	}

	ret = wlan_scan_channel_list(priv,
				     maxchanperscan,
				     filteredscan,
				     scan_cfg,
				     pchantlvout,
819
				     scan_chan_list,
820 821
				     puserscanin,
				     full_scan);
822 823 824 825 826 827 828 829

	/*  Process the resulting scan table:
	 *    - Remove any bad ssids
	 *    - Update our current BSS information from scan data
	 */
	wlan_scan_process_results(priv);

	if (priv->adapter->connect_status == libertas_connected) {
830 831
		netif_carrier_on(priv->mesh_dev);
		netif_wake_queue(priv->mesh_dev);
832 833 834 835 836 837 838 839 840
	}

out:
	if (scan_cfg)
		kfree(scan_cfg);

	if (scan_chan_list)
		kfree(scan_chan_list);

841
	lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
	return ret;
}

/**
 *  @brief Inspect the scan response buffer for pointers to expected TLVs
 *
 *  TLVs can be included at the end of the scan response BSS information.
 *    Parse the data in the buffer for pointers to TLVs that can potentially
 *    be passed back in the response
 *
 *  @param ptlv        Pointer to the start of the TLV buffer to parse
 *  @param tlvbufsize  size of the TLV buffer
 *  @param ptsftlv     Output parameter: Pointer to the TSF TLV if found
 *
 *  @return            void
 */
static
void wlan_ret_802_11_scan_get_tlv_ptrs(struct mrvlietypes_data * ptlv,
				       int tlvbufsize,
				       struct mrvlietypes_tsftimestamp ** ptsftlv)
{
	struct mrvlietypes_data *pcurrenttlv;
	int tlvbufleft;
	u16 tlvtype;
	u16 tlvlen;

	pcurrenttlv = ptlv;
	tlvbufleft = tlvbufsize;
	*ptsftlv = NULL;

872
	lbs_deb_scan("SCAN_RESP: tlvbufsize = %d\n", tlvbufsize);
873 874 875 876 877 878 879 880 881 882 883 884
	lbs_dbg_hex("SCAN_RESP: TLV Buf", (u8 *) ptlv, tlvbufsize);

	while (tlvbufleft >= sizeof(struct mrvlietypesheader)) {
		tlvtype = le16_to_cpu(pcurrenttlv->header.type);
		tlvlen = le16_to_cpu(pcurrenttlv->header.len);

		switch (tlvtype) {
		case TLV_TYPE_TSFTIMESTAMP:
			*ptsftlv = (struct mrvlietypes_tsftimestamp *) pcurrenttlv;
			break;

		default:
885
			lbs_deb_scan("SCAN_RESP: Unhandled TLV = %d\n",
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
			       tlvtype);
			/* Give up, this seems corrupted */
			return;
		}		/* switch */

		tlvbufleft -= (sizeof(ptlv->header) + tlvlen);
		pcurrenttlv =
		    (struct mrvlietypes_data *) (pcurrenttlv->Data + tlvlen);
	}			/* while */
}

/**
 *  @brief Interpret a BSS scan response returned from the firmware
 *
 *  Parse the various fixed fields and IEs passed back for a a BSS probe
 *   response or beacon from the scan command.  Record information as needed
 *   in the scan table struct bss_descriptor for that entry.
 *
 *  @param pBSSIDEntry  Output parameter: Pointer to the BSS Entry
 *
 *  @return             0 or -1
 */
static int InterpretBSSDescriptionWithIE(struct bss_descriptor * pBSSEntry,
					 u8 ** pbeaconinfo, int *bytesleft)
{
	enum ieeetypes_elementid elemID;
	struct ieeetypes_fhparamset *pFH;
	struct ieeetypes_dsparamset *pDS;
	struct ieeetypes_cfparamset *pCF;
	struct ieeetypes_ibssparamset *pibss;
	struct ieeetypes_capinfo *pcap;
	struct WLAN_802_11_FIXED_IEs fixedie;
	u8 *pcurrentptr;
	u8 *pRate;
	u8 elemlen;
	u8 bytestocopy;
	u8 ratesize;
	u16 beaconsize;
	u8 founddatarateie;
	int bytesleftforcurrentbeacon;
926
	int ret;
927 928 929 930 931 932

	struct IE_WPA *pIe;
	const u8 oui01[4] = { 0x00, 0x50, 0xf2, 0x01 };

	struct ieeetypes_countryinfoset *pcountryinfo;

933
	lbs_deb_enter(LBS_DEB_ASSOC);
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964

	founddatarateie = 0;
	ratesize = 0;
	beaconsize = 0;

	if (*bytesleft >= sizeof(beaconsize)) {
		/* Extract & convert beacon size from the command buffer */
		memcpy(&beaconsize, *pbeaconinfo, sizeof(beaconsize));
		beaconsize = le16_to_cpu(beaconsize);
		*bytesleft -= sizeof(beaconsize);
		*pbeaconinfo += sizeof(beaconsize);
	}

	if (beaconsize == 0 || beaconsize > *bytesleft) {

		*pbeaconinfo += *bytesleft;
		*bytesleft = 0;

		return -1;
	}

	/* Initialize the current working beacon pointer for this BSS iteration */
	pcurrentptr = *pbeaconinfo;

	/* Advance the return beacon pointer past the current beacon */
	*pbeaconinfo += beaconsize;
	*bytesleft -= beaconsize;

	bytesleftforcurrentbeacon = beaconsize;

	memcpy(pBSSEntry->macaddress, pcurrentptr, ETH_ALEN);
965
	lbs_deb_scan("InterpretIE: AP MAC Addr-%x:%x:%x:%x:%x:%x\n",
966 967 968 969 970 971 972 973
	       pBSSEntry->macaddress[0], pBSSEntry->macaddress[1],
	       pBSSEntry->macaddress[2], pBSSEntry->macaddress[3],
	       pBSSEntry->macaddress[4], pBSSEntry->macaddress[5]);

	pcurrentptr += ETH_ALEN;
	bytesleftforcurrentbeacon -= ETH_ALEN;

	if (bytesleftforcurrentbeacon < 12) {
974
		lbs_deb_scan("InterpretIE: Not enough bytes left\n");
975 976 977 978 979 980 981 982 983 984
		return -1;
	}

	/*
	 * next 4 fields are RSSI, time stamp, beacon interval,
	 *   and capability information
	 */

	/* RSSI is 1 byte long */
	pBSSEntry->rssi = le32_to_cpu((long)(*pcurrentptr));
985
	lbs_deb_scan("InterpretIE: RSSI=%02X\n", *pcurrentptr);
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
	pcurrentptr += 1;
	bytesleftforcurrentbeacon -= 1;

	/* time stamp is 8 bytes long */
	memcpy(fixedie.timestamp, pcurrentptr, 8);
	memcpy(pBSSEntry->timestamp, pcurrentptr, 8);
	pcurrentptr += 8;
	bytesleftforcurrentbeacon -= 8;

	/* beacon interval is 2 bytes long */
	memcpy(&fixedie.beaconinterval, pcurrentptr, 2);
	pBSSEntry->beaconperiod = le16_to_cpu(fixedie.beaconinterval);
	pcurrentptr += 2;
	bytesleftforcurrentbeacon -= 2;

	/* capability information is 2 bytes long */
	memcpy(&fixedie.capabilities, pcurrentptr, 2);
1003
	lbs_deb_scan("InterpretIE: fixedie.capabilities=0x%X\n",
1004 1005 1006 1007 1008 1009 1010 1011
	       fixedie.capabilities);
	fixedie.capabilities = le16_to_cpu(fixedie.capabilities);
	pcap = (struct ieeetypes_capinfo *) & fixedie.capabilities;
	memcpy(&pBSSEntry->cap, pcap, sizeof(struct ieeetypes_capinfo));
	pcurrentptr += 2;
	bytesleftforcurrentbeacon -= 2;

	/* rest of the current buffer are IE's */
1012
	lbs_deb_scan("InterpretIE: IElength for this AP = %d\n",
1013 1014 1015 1016 1017 1018
	       bytesleftforcurrentbeacon);

	lbs_dbg_hex("InterpretIE: IE info", (u8 *) pcurrentptr,
		bytesleftforcurrentbeacon);

	if (pcap->privacy) {
1019
		lbs_deb_scan("InterpretIE: AP WEP enabled\n");
1020 1021 1022 1023 1024 1025
		pBSSEntry->privacy = wlan802_11privfilter8021xWEP;
	} else {
		pBSSEntry->privacy = wlan802_11privfilteracceptall;
	}

	if (pcap->ibss == 1) {
1026
		pBSSEntry->mode = IW_MODE_ADHOC;
1027
	} else {
1028
		pBSSEntry->mode = IW_MODE_INFRA;
1029 1030 1031 1032 1033 1034 1035 1036
	}

	/* process variable IE */
	while (bytesleftforcurrentbeacon >= 2) {
		elemID = (enum ieeetypes_elementid) (*((u8 *) pcurrentptr));
		elemlen = *((u8 *) pcurrentptr + 1);

		if (bytesleftforcurrentbeacon < elemlen) {
1037
			lbs_deb_scan("InterpretIE: error in processing IE, "
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
			       "bytes left < IE length\n");
			bytesleftforcurrentbeacon = 0;
			continue;
		}

		switch (elemID) {

		case SSID:
			pBSSEntry->ssid.ssidlength = elemlen;
			memcpy(pBSSEntry->ssid.ssid, (pcurrentptr + 2),
			       elemlen);
1049
			lbs_deb_scan("ssid '%s'\n", pBSSEntry->ssid.ssid);
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
			break;

		case SUPPORTED_RATES:
			memcpy(pBSSEntry->datarates, (pcurrentptr + 2),
			       elemlen);
			memmove(pBSSEntry->libertas_supported_rates, (pcurrentptr + 2),
				elemlen);
			ratesize = elemlen;
			founddatarateie = 1;
			break;

		case EXTRA_IE:
1062
			lbs_deb_scan("InterpretIE: EXTRA_IE Found!\n");
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
			pBSSEntry->extra_ie = 1;
			break;

		case FH_PARAM_SET:
			pFH = (struct ieeetypes_fhparamset *) pcurrentptr;
			memmove(&pBSSEntry->phyparamset.fhparamset, pFH,
				sizeof(struct ieeetypes_fhparamset));
			pBSSEntry->phyparamset.fhparamset.dwelltime
			    =
			    le16_to_cpu(pBSSEntry->phyparamset.fhparamset.
					     dwelltime);
			break;

		case DS_PARAM_SET:
			pDS = (struct ieeetypes_dsparamset *) pcurrentptr;

			pBSSEntry->channel = pDS->currentchan;

			memcpy(&pBSSEntry->phyparamset.dsparamset, pDS,
			       sizeof(struct ieeetypes_dsparamset));
			break;

		case CF_PARAM_SET:
			pCF = (struct ieeetypes_cfparamset *) pcurrentptr;

			memcpy(&pBSSEntry->ssparamset.cfparamset, pCF,
			       sizeof(struct ieeetypes_cfparamset));
			break;

		case IBSS_PARAM_SET:
			pibss = (struct ieeetypes_ibssparamset *) pcurrentptr;
			pBSSEntry->atimwindow =
			    le32_to_cpu(pibss->atimwindow);

			memmove(&pBSSEntry->ssparamset.ibssparamset, pibss,
				sizeof(struct ieeetypes_ibssparamset));

			pBSSEntry->ssparamset.ibssparamset.atimwindow
			    =
			    le16_to_cpu(pBSSEntry->ssparamset.ibssparamset.
					     atimwindow);
			break;

			/* Handle Country Info IE */
		case COUNTRY_INFO:
			pcountryinfo =
			    (struct ieeetypes_countryinfoset *) pcurrentptr;

			if (pcountryinfo->len <
			    sizeof(pcountryinfo->countrycode)
			    || pcountryinfo->len > 254) {
1114
				lbs_deb_scan("InterpretIE: 11D- Err "
D
Dan Williams 已提交
1115
				       "CountryInfo len =%d min=%zd max=254\n",
1116 1117
				       pcountryinfo->len,
				       sizeof(pcountryinfo->countrycode));
1118 1119
				ret = -1;
				goto done;
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
			}

			memcpy(&pBSSEntry->countryinfo,
			       pcountryinfo, pcountryinfo->len + 2);
			lbs_dbg_hex("InterpretIE: 11D- CountryInfo:",
				(u8 *) pcountryinfo,
				(u32) (pcountryinfo->len + 2));
			break;

		case EXTENDED_SUPPORTED_RATES:
			/*
			 * only process extended supported rate
			 * if data rate is already found.
			 * data rate IE should come before
			 * extended supported rate IE
			 */
			if (founddatarateie) {
				if ((elemlen + ratesize) > WLAN_SUPPORTED_RATES) {
					bytestocopy =
					    (WLAN_SUPPORTED_RATES - ratesize);
				} else {
					bytestocopy = elemlen;
				}

				pRate = (u8 *) pBSSEntry->datarates;
				pRate += ratesize;
				memmove(pRate, (pcurrentptr + 2), bytestocopy);

				pRate = (u8 *) pBSSEntry->libertas_supported_rates;

				pRate += ratesize;
				memmove(pRate, (pcurrentptr + 2), bytestocopy);
			}
			break;

		case VENDOR_SPECIFIC_221:
#define IE_ID_LEN_FIELDS_BYTES 2
			pIe = (struct IE_WPA *)pcurrentptr;

1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
			if (memcmp(pIe->oui, oui01, sizeof(oui01)))
				break;

			pBSSEntry->wpa_ie_len = min_t(size_t,
				elemlen + IE_ID_LEN_FIELDS_BYTES,
				sizeof(pBSSEntry->wpa_ie));
			memcpy(pBSSEntry->wpa_ie, pcurrentptr,
				pBSSEntry->wpa_ie_len);
			lbs_dbg_hex("InterpretIE: Resp WPA_IE",
				pBSSEntry->wpa_ie, elemlen);
1169 1170 1171 1172
			break;
		case WPA2_IE:
			pIe = (struct IE_WPA *)pcurrentptr;

1173 1174 1175 1176 1177
			pBSSEntry->rsn_ie_len = min_t(size_t,
				elemlen + IE_ID_LEN_FIELDS_BYTES,
				sizeof(pBSSEntry->rsn_ie));
			memcpy(pBSSEntry->rsn_ie, pcurrentptr,
				pBSSEntry->rsn_ie_len);
1178
			lbs_dbg_hex("InterpretIE: Resp WPA2_IE",
1179
				pBSSEntry->rsn_ie, elemlen);
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
			break;
		case TIM:
			break;

		case CHALLENGE_TEXT:
			break;
		}

		pcurrentptr += elemlen + 2;

		/* need to account for IE ID and IE len */
		bytesleftforcurrentbeacon -= (elemlen + 2);

	}			/* while (bytesleftforcurrentbeacon > 2) */
1194
	ret = 0;
1195

1196 1197 1198
done:
	lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
	return ret;
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
}

/**
 *  @brief Compare two SSIDs
 *
 *  @param ssid1    A pointer to ssid to compare
 *  @param ssid2    A pointer to ssid to compare
 *
 *  @return         0--ssid is same, otherwise is different
 */
int libertas_SSID_cmp(struct WLAN_802_11_SSID *ssid1, struct WLAN_802_11_SSID *ssid2)
{
	if (!ssid1 || !ssid2)
		return -1;

	if (ssid1->ssidlength != ssid2->ssidlength)
		return -1;

	return memcmp(ssid1->ssid, ssid2->ssid, ssid1->ssidlength);
}

/**
 *  @brief This function finds a specific compatible BSSID in the scan list
 *
 *  @param adapter  A pointer to wlan_adapter
 *  @param bssid    BSSID to find in the scan list
 *  @param mode     Network mode: Infrastructure or IBSS
 *
 *  @return         index in BSSID list, or error return code (< 0)
 */
1229
int libertas_find_BSSID_in_list(wlan_adapter * adapter, u8 * bssid, u8 mode)
1230 1231 1232 1233 1234 1235 1236
{
	int ret = -ENETUNREACH;
	int i;

	if (!bssid)
		return -EFAULT;

1237
	lbs_deb_scan("FindBSSID: Num of BSSIDs = %d\n",
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	       adapter->numinscantable);

	/* Look through the scan table for a compatible match. The ret return
	 *   variable will be equal to the index in the scan table (greater
	 *   than zero) if the network is compatible.  The loop will continue
	 *   past a matched bssid that is not compatible in case there is an
	 *   AP with multiple SSIDs assigned to the same BSSID
	 */
	for (i = 0; ret < 0 && i < adapter->numinscantable; i++) {
		if (!memcmp(adapter->scantable[i].macaddress, bssid, ETH_ALEN)) {
			switch (mode) {
1249 1250
			case IW_MODE_INFRA:
			case IW_MODE_ADHOC:
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
				ret = is_network_compatible(adapter, i, mode);
				break;
			default:
				ret = i;
				break;
			}
		}
	}

	return ret;
}

/**
 *  @brief This function finds ssid in ssid list.
 *
 *  @param adapter  A pointer to wlan_adapter
 *  @param ssid     SSID to find in the list
 *  @param bssid    BSSID to qualify the SSID selection (if provided)
 *  @param mode     Network mode: Infrastructure or IBSS
 *
 *  @return         index in BSSID list
 */
int libertas_find_SSID_in_list(wlan_adapter * adapter,
1274
		   struct WLAN_802_11_SSID *ssid, u8 * bssid, u8 mode)
1275 1276 1277 1278 1279 1280
{
	int net = -ENETUNREACH;
	u8 bestrssi = 0;
	int i;
	int j;

1281
	lbs_deb_scan("Num of Entries in Table = %d\n", adapter->numinscantable);
1282 1283 1284 1285 1286 1287 1288

	for (i = 0; i < adapter->numinscantable; i++) {
		if (!libertas_SSID_cmp(&adapter->scantable[i].ssid, ssid) &&
		    (!bssid ||
		     !memcmp(adapter->scantable[i].
			     macaddress, bssid, ETH_ALEN))) {
			switch (mode) {
1289 1290
			case IW_MODE_INFRA:
			case IW_MODE_ADHOC:
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
				j = is_network_compatible(adapter, i, mode);

				if (j >= 0) {
					if (bssid) {
						return i;
					}

					if (SCAN_RSSI
					    (adapter->scantable[i].rssi)
					    > bestrssi) {
						bestrssi =
						    SCAN_RSSI(adapter->
							      scantable[i].
							      rssi);
						net = i;
					}
				} else {
					if (net == -ENETUNREACH) {
						net = j;
					}
				}
				break;
1313
			case IW_MODE_AUTO:
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
			default:
				if (SCAN_RSSI(adapter->scantable[i].rssi)
				    > bestrssi) {
					bestrssi =
					    SCAN_RSSI(adapter->scantable[i].
						      rssi);
					net = i;
				}
				break;
			}
		}
	}

	return net;
}

/**
 *  @brief This function finds the best SSID in the Scan List
 *
 *  Search the scan table for the best SSID that also matches the current
 *   adapter network preference (infrastructure or adhoc)
 *
 *  @param adapter  A pointer to wlan_adapter
 *
 *  @return         index in BSSID list
 */
1340
int libertas_find_best_SSID_in_list(wlan_adapter * adapter, u8 mode)
1341 1342 1343 1344 1345
{
	int bestnet = -ENETUNREACH;
	u8 bestrssi = 0;
	int i;

1346
	lbs_deb_enter(LBS_DEB_ASSOC);
1347

1348
	lbs_deb_scan("Num of BSSIDs = %d\n", adapter->numinscantable);
1349 1350 1351

	for (i = 0; i < adapter->numinscantable; i++) {
		switch (mode) {
1352 1353
		case IW_MODE_INFRA:
		case IW_MODE_ADHOC:
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363
			if (is_network_compatible(adapter, i, mode) >= 0) {
				if (SCAN_RSSI(adapter->scantable[i].rssi) >
				    bestrssi) {
					bestrssi =
					    SCAN_RSSI(adapter->scantable[i].
						      rssi);
					bestnet = i;
				}
			}
			break;
1364
		case IW_MODE_AUTO:
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
		default:
			if (SCAN_RSSI(adapter->scantable[i].rssi) > bestrssi) {
				bestrssi =
				    SCAN_RSSI(adapter->scantable[i].rssi);
				bestnet = i;
			}
			break;
		}
	}

1375
	lbs_deb_leave_args(LBS_DEB_SCAN, "bestnet %d", bestnet);
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
	return bestnet;
}

/**
 *  @brief Find the AP with specific ssid in the scan list
 *
 *  @param priv         A pointer to wlan_private structure
 *  @param pSSID        A pointer to AP's ssid
 *
 *  @return             0--success, otherwise--fail
 */
int libertas_find_best_network_SSID(wlan_private * priv,
                                    struct WLAN_802_11_SSID *pSSID,
1389
                                    u8 preferred_mode, u8 *out_mode)
1390 1391 1392 1393 1394 1395
{
	wlan_adapter *adapter = priv->adapter;
	int ret = 0;
	struct bss_descriptor *preqbssid;
	int i;

1396
	lbs_deb_enter(LBS_DEB_ASSOC);
1397 1398 1399

	memset(pSSID, 0, sizeof(struct WLAN_802_11_SSID));

1400
	wlan_scan_networks(priv, NULL, 1);
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
	if (adapter->surpriseremoved)
		return -1;
	wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);

	i = libertas_find_best_SSID_in_list(adapter, preferred_mode);
	if (i < 0) {
		ret = -1;
		goto out;
	}

	preqbssid = &adapter->scantable[i];
	memcpy(pSSID, &preqbssid->ssid,
	       sizeof(struct WLAN_802_11_SSID));
1414
	*out_mode = preqbssid->mode;
1415 1416 1417 1418 1419 1420

	if (!pSSID->ssidlength) {
		ret = -1;
	}

out:
1421
	lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
	return ret;
}

/**
 *  @brief Scan Network
 *
 *  @param dev          A pointer to net_device structure
 *  @param info         A pointer to iw_request_info structure
 *  @param vwrq         A pointer to iw_param structure
 *  @param extra        A pointer to extra data buf
 *
 *  @return             0 --success, otherwise fail
 */
int libertas_set_scan(struct net_device *dev, struct iw_request_info *info,
		  struct iw_param *vwrq, char *extra)
{
	wlan_private *priv = dev->priv;
	wlan_adapter *adapter = priv->adapter;

1441
	lbs_deb_enter(LBS_DEB_SCAN);
1442

1443
	wlan_scan_networks(priv, NULL, 0);
1444 1445 1446 1447

	if (adapter->surpriseremoved)
		return -1;

1448
	lbs_deb_leave(LBS_DEB_SCAN);
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
	return 0;
}

/**
 *  @brief Send a scan command for all available channels filtered on a spec
 *
 *  @param priv             A pointer to wlan_private structure
 *  @param prequestedssid   A pointer to AP's ssid
 *  @param keeppreviousscan Flag used to save/clear scan table before scan
 *
 *  @return                0-success, otherwise fail
 */
int libertas_send_specific_SSID_scan(wlan_private * priv,
			 struct WLAN_802_11_SSID *prequestedssid,
			 u8 keeppreviousscan)
{
	wlan_adapter *adapter = priv->adapter;
	struct wlan_ioctl_user_scan_cfg scancfg;

1468
	lbs_deb_enter(LBS_DEB_ASSOC);
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479

	if (prequestedssid == NULL) {
		return -1;
	}

	memset(&scancfg, 0x00, sizeof(scancfg));

	memcpy(scancfg.specificSSID, prequestedssid->ssid,
	       prequestedssid->ssidlength);
	scancfg.keeppreviousscan = keeppreviousscan;

1480
	wlan_scan_networks(priv, &scancfg, 1);
1481 1482 1483 1484
	if (adapter->surpriseremoved)
		return -1;
	wait_event_interruptible(adapter->cmd_pending, !adapter->nr_cmd_pending);

1485
	lbs_deb_leave(LBS_DEB_ASSOC);
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
	return 0;
}

/**
 *  @brief scan an AP with specific BSSID
 *
 *  @param priv             A pointer to wlan_private structure
 *  @param bssid            A pointer to AP's bssid
 *  @param keeppreviousscan Flag used to save/clear scan table before scan
 *
 *  @return          0-success, otherwise fail
 */
int libertas_send_specific_BSSID_scan(wlan_private * priv, u8 * bssid, u8 keeppreviousscan)
{
	struct wlan_ioctl_user_scan_cfg scancfg;

1502
	lbs_deb_enter(LBS_DEB_ASSOC);
1503 1504 1505 1506 1507 1508 1509 1510 1511

	if (bssid == NULL) {
		return -1;
	}

	memset(&scancfg, 0x00, sizeof(scancfg));
	memcpy(scancfg.specificBSSID, bssid, sizeof(scancfg.specificBSSID));
	scancfg.keeppreviousscan = keeppreviousscan;

1512
	wlan_scan_networks(priv, &scancfg, 1);
1513 1514 1515 1516 1517
	if (priv->adapter->surpriseremoved)
		return -1;
	wait_event_interruptible(priv->adapter->cmd_pending,
		!priv->adapter->nr_cmd_pending);

1518
	lbs_deb_leave(LBS_DEB_ASSOC);
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
	return 0;
}

/**
 *  @brief  Retrieve the scan table entries via wireless tools IOCTL call
 *
 *  @param dev          A pointer to net_device structure
 *  @param info         A pointer to iw_request_info structure
 *  @param dwrq         A pointer to iw_point structure
 *  @param extra        A pointer to extra data buf
 *
 *  @return             0 --success, otherwise fail
 */
int libertas_get_scan(struct net_device *dev, struct iw_request_info *info,
		  struct iw_point *dwrq, char *extra)
{
	wlan_private *priv = dev->priv;
	wlan_adapter *adapter = priv->adapter;
	int ret = 0;
	char *current_ev = extra;
	char *end_buf = extra + IW_SCAN_MAX_DATA;
	struct chan_freq_power *cfp;
	struct bss_descriptor *pscantable;
	char *current_val;	/* For rates */
	struct iw_event iwe;	/* Temporary buffer */
	int i;
	int j;
	int rate;
#define PERFECT_RSSI ((u8)50)
#define WORST_RSSI   ((u8)0)
#define RSSI_DIFF    ((u8)(PERFECT_RSSI - WORST_RSSI))
	u8 rssi;

	u8 buf[16 + 256 * 2];
	u8 *ptr;

1555
	lbs_deb_enter(LBS_DEB_ASSOC);
1556 1557 1558 1559 1560

	/*
	 * if there's either commands in the queue or one being
	 * processed return -EAGAIN for iwlist to retry later.
	 */
1561 1562 1563 1564 1565
	if (adapter->nr_cmd_pending)
		return -EAGAIN;

	if (adapter->last_scanned_channel) {
		wlan_scan_networks(priv, NULL, 0);
1566
		return -EAGAIN;
1567
	}
1568 1569

	if (adapter->connect_status == libertas_connected)
1570
		lbs_deb_scan("current ssid '%s'\n",
1571 1572
		       adapter->curbssparams.ssid.ssid);

1573
	lbs_deb_scan("Scan: Get: numinscantable = %d\n",
1574 1575 1576 1577 1578 1579 1580 1581 1582
	       adapter->numinscantable);

	/* The old API using SIOCGIWAPLIST had a hard limit of IW_MAX_AP.
	 * The new API using SIOCGIWSCAN is only limited by buffer size
	 * WE-14 -> WE-16 the buffer is limited to IW_SCAN_MAX_DATA bytes
	 * which is 4096.
	 */
	for (i = 0; i < adapter->numinscantable; i++) {
		if ((current_ev + MAX_SCAN_CELL_SIZE) >= end_buf) {
1583
			lbs_deb_scan("i=%d break out: current_ev=%p end_buf=%p "
D
Dan Williams 已提交
1584
			       "MAX_SCAN_CELL_SIZE=%zd\n",
1585 1586 1587 1588 1589 1590
			       i, current_ev, end_buf, MAX_SCAN_CELL_SIZE);
			break;
		}

		pscantable = &adapter->scantable[i];

1591
		lbs_deb_scan("i %d, ssid '%s'\n", i, pscantable->ssid.ssid);
1592 1593 1594 1595 1596

		cfp =
		    libertas_find_cfp_by_band_and_channel(adapter, 0,
						 pscantable->channel);
		if (!cfp) {
1597
			lbs_deb_scan("Invalid channel number %d\n",
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631
			       pscantable->channel);
			continue;
		}

		if (!ssid_valid(&adapter->scantable[i].ssid)) {
			continue;
		}

		/* First entry *MUST* be the AP MAC address */
		iwe.cmd = SIOCGIWAP;
		iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
		memcpy(iwe.u.ap_addr.sa_data,
		       &adapter->scantable[i].macaddress, ETH_ALEN);

		iwe.len = IW_EV_ADDR_LEN;
		current_ev =
		    iwe_stream_add_event(current_ev, end_buf, &iwe, iwe.len);

		//Add the ESSID
		iwe.u.data.length = adapter->scantable[i].ssid.ssidlength;

		if (iwe.u.data.length > 32) {
			iwe.u.data.length = 32;
		}

		iwe.cmd = SIOCGIWESSID;
		iwe.u.data.flags = 1;
		iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
		current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
						  adapter->scantable[i].ssid.
						  ssid);

		//Add mode
		iwe.cmd = SIOCGIWMODE;
1632
		iwe.u.mode = adapter->scantable[i].mode;
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
		iwe.len = IW_EV_UINT_LEN;
		current_ev =
		    iwe_stream_add_event(current_ev, end_buf, &iwe, iwe.len);

		//frequency
		iwe.cmd = SIOCGIWFREQ;
		iwe.u.freq.m = (long)cfp->freq * 100000;
		iwe.u.freq.e = 1;
		iwe.len = IW_EV_FREQ_LEN;
		current_ev =
		    iwe_stream_add_event(current_ev, end_buf, &iwe, iwe.len);

		/* Add quality statistics */
		iwe.cmd = IWEVQUAL;
		iwe.u.qual.updated = IW_QUAL_ALL_UPDATED;
		iwe.u.qual.level = SCAN_RSSI(adapter->scantable[i].rssi);

		rssi = iwe.u.qual.level - MRVDRV_NF_DEFAULT_SCAN_VALUE;
		iwe.u.qual.qual =
		    (100 * RSSI_DIFF * RSSI_DIFF - (PERFECT_RSSI - rssi) *
		     (15 * (RSSI_DIFF) + 62 * (PERFECT_RSSI - rssi))) /
		    (RSSI_DIFF * RSSI_DIFF);
		if (iwe.u.qual.qual > 100)
			iwe.u.qual.qual = 100;
		else if (iwe.u.qual.qual < 1)
			iwe.u.qual.qual = 0;

		if (adapter->NF[TYPE_BEACON][TYPE_NOAVG] == 0) {
			iwe.u.qual.noise = MRVDRV_NF_DEFAULT_SCAN_VALUE;
		} else {
			iwe.u.qual.noise =
			    CAL_NF(adapter->NF[TYPE_BEACON][TYPE_NOAVG]);
		}
1666
		if ((adapter->mode == IW_MODE_ADHOC) &&
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
		    !libertas_SSID_cmp(&adapter->curbssparams.ssid,
			     &adapter->scantable[i].ssid)
		    && adapter->adhoccreate) {
			ret = libertas_prepare_and_send_command(priv,
						    cmd_802_11_rssi,
						    0,
						    cmd_option_waitforrsp,
						    0, NULL);

			if (!ret) {
				iwe.u.qual.level =
				    CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_AVG] /
					     AVG_SCALE,
					     adapter->NF[TYPE_RXPD][TYPE_AVG] /
					     AVG_SCALE);
			}
		}
		iwe.len = IW_EV_QUAL_LEN;
		current_ev =
		    iwe_stream_add_event(current_ev, end_buf, &iwe, iwe.len);

		/* Add encryption capability */
		iwe.cmd = SIOCGIWENCODE;
		if (adapter->scantable[i].privacy) {
			iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
		} else {
			iwe.u.data.flags = IW_ENCODE_DISABLED;
		}
		iwe.u.data.length = 0;
		iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
		current_ev = iwe_stream_add_point(current_ev, end_buf, &iwe,
						  adapter->scantable->ssid.
						  ssid);

		current_val = current_ev + IW_EV_LCP_LEN;

		iwe.cmd = SIOCGIWRATE;

		iwe.u.bitrate.fixed = 0;
		iwe.u.bitrate.disabled = 0;
		iwe.u.bitrate.value = 0;

		/* Bit rate given in 500 kb/s units (+ 0x80) */
		for (j = 0; j < sizeof(adapter->scantable[i].libertas_supported_rates);
		     j++) {
			if (adapter->scantable[i].libertas_supported_rates[j] == 0) {
				break;
			}
			rate =
			    (adapter->scantable[i].libertas_supported_rates[j] & 0x7F) *
			    500000;
			if (rate > iwe.u.bitrate.value) {
				iwe.u.bitrate.value = rate;
			}

			iwe.u.bitrate.value =
			    (adapter->scantable[i].libertas_supported_rates[j]
			     & 0x7f) * 500000;
			iwe.len = IW_EV_PARAM_LEN;
			current_ev =
			    iwe_stream_add_value(current_ev, current_val,
						 end_buf, &iwe, iwe.len);

		}
1731
		if ((adapter->scantable[i].mode == IW_MODE_ADHOC)
1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
		    && !libertas_SSID_cmp(&adapter->curbssparams.ssid,
				&adapter->scantable[i].ssid)
		    && adapter->adhoccreate) {
			iwe.u.bitrate.value = 22 * 500000;
		}
		iwe.len = IW_EV_PARAM_LEN;
		current_ev =
		    iwe_stream_add_value(current_ev, current_val, end_buf, &iwe,
					 iwe.len);

		/* Add new value to event */
		current_val = current_ev + IW_EV_LCP_LEN;

1745
		if (adapter->scantable[i].rsn_ie[0] == WPA2_IE) {
1746 1747
			memset(&iwe, 0, sizeof(iwe));
			memset(buf, 0, sizeof(buf));
1748 1749
			memcpy(buf, adapter->scantable[i].rsn_ie,
					adapter->scantable[i].rsn_ie_len);
1750
			iwe.cmd = IWEVGENIE;
1751
			iwe.u.data.length = adapter->scantable[i].rsn_ie_len;
1752 1753 1754 1755
			iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
			current_ev = iwe_stream_add_point(current_ev, end_buf,
					&iwe, buf);
		}
1756
		if (adapter->scantable[i].wpa_ie[0] == WPA_IE) {
1757 1758
			memset(&iwe, 0, sizeof(iwe));
			memset(buf, 0, sizeof(buf));
1759 1760
			memcpy(buf, adapter->scantable[i].wpa_ie,
					adapter->scantable[i].wpa_ie_len);
1761
			iwe.cmd = IWEVGENIE;
1762
			iwe.u.data.length = adapter->scantable[i].wpa_ie_len;
1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
			iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
			current_ev = iwe_stream_add_point(current_ev, end_buf,
					&iwe, buf);
		}


		if (adapter->scantable[i].extra_ie != 0) {
			memset(&iwe, 0, sizeof(iwe));
			memset(buf, 0, sizeof(buf));
			ptr = buf;
			ptr += sprintf(ptr, "extra_ie");
			iwe.u.data.length = strlen(buf);

1776
			lbs_deb_scan("iwe.u.data.length %d\n",
1777
			       iwe.u.data.length);
1778
			lbs_deb_scan("BUF: %s \n", buf);
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798

			iwe.cmd = IWEVCUSTOM;
			iwe.len = IW_EV_POINT_LEN + iwe.u.data.length;
			current_ev =
			    iwe_stream_add_point(current_ev, end_buf, &iwe,
						 buf);
		}

		current_val = current_ev + IW_EV_LCP_LEN;

		/*
		 * Check if we added any event
		 */
		if ((current_val - current_ev) > IW_EV_LCP_LEN)
			current_ev = current_val;
	}

	dwrq->length = (current_ev - extra);
	dwrq->flags = 0;

1799
	lbs_deb_leave(LBS_DEB_ASSOC);
1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828
	return 0;
}

/**
 *  @brief Prepare a scan command to be sent to the firmware
 *
 *  Use the wlan_scan_cmd_config sent to the command processing module in
 *   the libertas_prepare_and_send_command to configure a cmd_ds_802_11_scan command
 *   struct to send to firmware.
 *
 *  The fixed fields specifying the BSS type and BSSID filters as well as a
 *   variable number/length of TLVs are sent in the command to firmware.
 *
 *  @param priv       A pointer to wlan_private structure
 *  @param cmd        A pointer to cmd_ds_command structure to be sent to
 *                    firmware with the cmd_DS_801_11_SCAN structure
 *  @param pdata_buf  Void pointer cast of a wlan_scan_cmd_config struct used
 *                    to set the fields/TLVs for the command sent to firmware
 *
 *  @return           0 or -1
 *
 *  @sa wlan_scan_create_channel_list
 */
int libertas_cmd_80211_scan(wlan_private * priv,
			 struct cmd_ds_command *cmd, void *pdata_buf)
{
	struct cmd_ds_802_11_scan *pscan = &cmd->params.scan;
	struct wlan_scan_cmd_config *pscancfg;

1829
	lbs_deb_enter(LBS_DEB_ASSOC);
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844

	pscancfg = pdata_buf;

	/* Set fixed field variables in scan command */
	pscan->bsstype = pscancfg->bsstype;
	memcpy(pscan->BSSID, pscancfg->specificBSSID, sizeof(pscan->BSSID));
	memcpy(pscan->tlvbuffer, pscancfg->tlvbuffer, pscancfg->tlvbufferlen);

	cmd->command = cpu_to_le16(cmd_802_11_scan);

	/* size is equal to the sizeof(fixed portions) + the TLV len + header */
	cmd->size = cpu_to_le16(sizeof(pscan->bsstype)
				     + sizeof(pscan->BSSID)
				     + pscancfg->tlvbufferlen + S_DS_GEN);

1845
	lbs_deb_scan("SCAN_CMD: command=%x, size=%x, seqnum=%x\n",
1846
	       cmd->command, cmd->size, cmd->seqnum);
1847 1848

	lbs_deb_leave(LBS_DEB_ASSOC);
1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
	return 0;
}

/**
 *  @brief This function handles the command response of scan
 *
 *   The response buffer for the scan command has the following
 *      memory layout:
 *
 *     .-----------------------------------------------------------.
 *     |  header (4 * sizeof(u16)):  Standard command response hdr |
 *     .-----------------------------------------------------------.
 *     |  bufsize (u16) : sizeof the BSS Description data          |
 *     .-----------------------------------------------------------.
 *     |  NumOfSet (u8) : Number of BSS Descs returned             |
 *     .-----------------------------------------------------------.
 *     |  BSSDescription data (variable, size given in bufsize)    |
 *     .-----------------------------------------------------------.
 *     |  TLV data (variable, size calculated using header->size,  |
 *     |            bufsize and sizeof the fixed fields above)     |
 *     .-----------------------------------------------------------.
 *
 *  @param priv    A pointer to wlan_private structure
 *  @param resp    A pointer to cmd_ds_command
 *
 *  @return        0 or -1
 */
int libertas_ret_80211_scan(wlan_private * priv, struct cmd_ds_command *resp)
{
	wlan_adapter *adapter = priv->adapter;
	struct cmd_ds_802_11_scan_rsp *pscan;
	struct bss_descriptor newbssentry;
	struct mrvlietypes_data *ptlv;
	struct mrvlietypes_tsftimestamp *ptsftlv;
	u8 *pbssinfo;
	u16 scanrespsize;
	int bytesleft;
	int numintable;
	int bssIdx;
	int idx;
	int tlvbufsize;
	u64 tsfval;
1891
	int ret;
1892

1893
	lbs_deb_enter(LBS_DEB_ASSOC);
1894 1895 1896 1897

	pscan = &resp->params.scanresp;

	if (pscan->nr_sets > MRVDRV_MAX_BSSID_LIST) {
1898
        lbs_deb_scan(
1899 1900
		       "SCAN_RESP: Invalid number of AP returned (%d)!!\n",
		       pscan->nr_sets);
1901 1902
		ret = -1;
		goto done;
1903 1904 1905
	}

	bytesleft = le16_to_cpu(pscan->bssdescriptsize);
1906
	lbs_deb_scan("SCAN_RESP: bssdescriptsize %d\n", bytesleft);
1907 1908

	scanrespsize = le16_to_cpu(resp->size);
1909
	lbs_deb_scan("SCAN_RESP: returned %d AP before parsing\n",
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
	       pscan->nr_sets);

	numintable = adapter->numinscantable;
	pbssinfo = pscan->bssdesc_and_tlvbuffer;

	/* The size of the TLV buffer is equal to the entire command response
	 *   size (scanrespsize) minus the fixed fields (sizeof()'s), the
	 *   BSS Descriptions (bssdescriptsize as bytesLef) and the command
	 *   response header (S_DS_GEN)
	 */
	tlvbufsize = scanrespsize - (bytesleft + sizeof(pscan->bssdescriptsize)
				     + sizeof(pscan->nr_sets)
				     + S_DS_GEN);

	ptlv = (struct mrvlietypes_data *) (pscan->bssdesc_and_tlvbuffer + bytesleft);

	/* Search the TLV buffer space in the scan response for any valid TLVs */
	wlan_ret_802_11_scan_get_tlv_ptrs(ptlv, tlvbufsize, &ptsftlv);

	/*
	 *  Process each scan response returned (pscan->nr_sets).  Save
	 *    the information in the newbssentry and then insert into the
	 *    driver scan table either as an update to an existing entry
	 *    or as an addition at the end of the table
	 */
	for (idx = 0; idx < pscan->nr_sets && bytesleft; idx++) {
		/* Zero out the newbssentry we are about to store info in */
		memset(&newbssentry, 0x00, sizeof(newbssentry));

		/* Process the data fields and IEs returned for this BSS */
		if ((InterpretBSSDescriptionWithIE(&newbssentry,
						   &pbssinfo,
						   &bytesleft) ==
		     0)
		    && CHECK_SSID_IS_VALID(&newbssentry.ssid)) {

1946
            lbs_deb_scan(
1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978
			       "SCAN_RESP: BSSID = %02x:%02x:%02x:%02x:%02x:%02x\n",
			       newbssentry.macaddress[0],
			       newbssentry.macaddress[1],
			       newbssentry.macaddress[2],
			       newbssentry.macaddress[3],
			       newbssentry.macaddress[4],
			       newbssentry.macaddress[5]);

			/*
			 * Search the scan table for the same bssid
			 */
			for (bssIdx = 0; bssIdx < numintable; bssIdx++) {
				if (memcmp(newbssentry.macaddress,
					   adapter->scantable[bssIdx].
					   macaddress,
					   sizeof(newbssentry.macaddress)) ==
				    0) {
					/*
					 * If the SSID matches as well, it is a duplicate of
					 *   this entry.  Keep the bssIdx set to this
					 *   entry so we replace the old contents in the table
					 */
					if ((newbssentry.ssid.ssidlength ==
					     adapter->scantable[bssIdx].ssid.
					     ssidlength)
					    &&
					    (memcmp
					     (newbssentry.ssid.ssid,
					      adapter->scantable[bssIdx].ssid.
					      ssid,
					      newbssentry.ssid.ssidlength) ==
					     0)) {
1979
                        lbs_deb_scan(
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022
						       "SCAN_RESP: Duplicate of index: %d\n",
						       bssIdx);
						break;
					}
				}
			}
			/*
			 * If the bssIdx is equal to the number of entries in the table,
			 *   the new entry was not a duplicate; append it to the scan
			 *   table
			 */
			if (bssIdx == numintable) {
				/* Range check the bssIdx, keep it limited to the last entry */
				if (bssIdx == MRVDRV_MAX_BSSID_LIST) {
					bssIdx--;
				} else {
					numintable++;
				}
			}

			/*
			 * If the TSF TLV was appended to the scan results, save the
			 *   this entries TSF value in the networktsf field.  The
			 *   networktsf is the firmware's TSF value at the time the
			 *   beacon or probe response was received.
			 */
			if (ptsftlv) {
				memcpy(&tsfval, &ptsftlv->tsftable[idx],
				       sizeof(tsfval));
				tsfval = le64_to_cpu(tsfval);

				memcpy(&newbssentry.networktsf,
				       &tsfval, sizeof(newbssentry.networktsf));
			}

			/* Copy the locally created newbssentry to the scan table */
			memcpy(&adapter->scantable[bssIdx],
			       &newbssentry,
			       sizeof(adapter->scantable[bssIdx]));

		} else {

			/* error parsing/interpreting the scan response, skipped */
2023
			lbs_deb_scan("SCAN_RESP: "
2024 2025 2026 2027
			       "InterpretBSSDescriptionWithIE returned ERROR\n");
		}
	}

2028
	lbs_deb_scan("SCAN_RESP: Scanned %2d APs, %d valid, %d total\n",
2029 2030 2031 2032 2033
	       pscan->nr_sets, numintable - adapter->numinscantable,
	       numintable);

	/* Update the total number of BSSIDs in the scan table */
	adapter->numinscantable = numintable;
2034
	ret = 0;
2035

2036 2037 2038
done:
	lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
	return ret;
2039
}