assoc.c 51.2 KB
Newer Older
1 2
/* Copyright (C) 2006, Red Hat, Inc. */

3
#include <linux/types.h>
4
#include <linux/etherdevice.h>
J
Johannes Berg 已提交
5 6
#include <linux/ieee80211.h>
#include <linux/if_arp.h>
7
#include <net/lib80211.h>
8 9 10 11

#include "assoc.h"
#include "decl.h"
#include "host.h"
12
#include "scan.h"
13
#include "cmd.h"
14

15 16 17 18
static const u8 bssid_any[ETH_ALEN]  __attribute__ ((aligned (2))) =
	{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
static const u8 bssid_off[ETH_ALEN]  __attribute__ ((aligned (2))) =
	{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
19

20 21 22
/* The firmware needs the following bits masked out of the beacon-derived
 * capability field when associating/joining to a BSS:
 *  9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
23 24 25
 */
#define CAPINFO_MASK	(~(0xda00))

26 27 28 29 30 31 32
/**
 * 802.11b/g supported bitrates (in 500Kb/s units)
 */
u8 lbs_bg_rates[MAX_RATES] =
    { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
0x00, 0x00 };

33

34 35 36 37 38 39 40 41 42 43
/**
 *  @brief This function finds common rates between rates and card rates.
 *
 * It will fill common rates in rates as output if found.
 *
 * NOTE: Setting the MSB of the basic rates need to be taken
 *   care, either before or after calling this function
 *
 *  @param priv     A pointer to struct lbs_private structure
 *  @param rates       the buffer which keeps input and output
44 45
 *  @param rates_size  the size of rates buffer; new size of buffer on return,
 *                     which will be less than or equal to original rates_size
46 47 48 49 50 51 52
 *
 *  @return            0 on success, or -1 on error
 */
static int get_common_rates(struct lbs_private *priv,
	u8 *rates,
	u16 *rates_size)
{
R
Roel Kluin 已提交
53
	int i, j;
54 55 56
	u8 intersection[MAX_RATES];
	u16 intersection_size;
	u16 num_rates = 0;
57

58
	intersection_size = min_t(u16, *rates_size, ARRAY_SIZE(intersection));
59

60 61 62 63 64
	/* Allow each rate from 'rates' that is supported by the hardware */
	for (i = 0; i < ARRAY_SIZE(lbs_bg_rates) && lbs_bg_rates[i]; i++) {
		for (j = 0; j < intersection_size && rates[j]; j++) {
			if (rates[j] == lbs_bg_rates[i])
				intersection[num_rates++] = rates[j];
65 66 67 68
		}
	}

	lbs_deb_hex(LBS_DEB_JOIN, "AP rates    ", rates, *rates_size);
69 70 71
	lbs_deb_hex(LBS_DEB_JOIN, "card rates  ", lbs_bg_rates,
			ARRAY_SIZE(lbs_bg_rates));
	lbs_deb_hex(LBS_DEB_JOIN, "common rates", intersection, num_rates);
72 73 74
	lbs_deb_join("TX data rate 0x%02x\n", priv->cur_rate);

	if (!priv->enablehwauto) {
75 76 77
		for (i = 0; i < num_rates; i++) {
			if (intersection[i] == priv->cur_rate)
				goto done;
78
		}
79 80 81
		lbs_pr_alert("Previously set fixed data rate %#x isn't "
		       "compatible with the network.\n", priv->cur_rate);
		return -1;
82
	}
83 84 85 86 87

done:
	memset(rates, 0, *rates_size);
	*rates_size = num_rates;
	memcpy(rates, intersection, num_rates);
R
Roel Kluin 已提交
88
	return 0;
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
}


/**
 *  @brief Sets the MSB on basic rates as the firmware requires
 *
 * Scan through an array and set the MSB for basic data rates.
 *
 *  @param rates     buffer of data rates
 *  @param len       size of buffer
 */
static void lbs_set_basic_rate_flags(u8 *rates, size_t len)
{
	int i;

	for (i = 0; i < len; i++) {
		if (rates[i] == 0x02 || rates[i] == 0x04 ||
		    rates[i] == 0x0b || rates[i] == 0x16)
			rates[i] |= 0x80;
	}
}

111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
static u8 iw_auth_to_ieee_auth(u8 auth)
{
	if (auth == IW_AUTH_ALG_OPEN_SYSTEM)
		return 0x00;
	else if (auth == IW_AUTH_ALG_SHARED_KEY)
		return 0x01;
	else if (auth == IW_AUTH_ALG_LEAP)
		return 0x80;

	lbs_deb_join("%s: invalid auth alg 0x%X\n", __func__, auth);
	return 0;
}

/**
 *  @brief This function prepares the authenticate command.  AUTHENTICATE only
 *  sets the authentication suite for future associations, as the firmware
 *  handles authentication internally during the ASSOCIATE command.
 *
 *  @param priv      A pointer to struct lbs_private structure
 *  @param bssid     The peer BSSID with which to authenticate
 *  @param auth      The authentication mode to use (from wireless.h)
 *
 *  @return         0 or -1
 */
static int lbs_set_authentication(struct lbs_private *priv, u8 bssid[6], u8 auth)
{
	struct cmd_ds_802_11_authenticate cmd;
	int ret = -1;

	lbs_deb_enter(LBS_DEB_JOIN);

	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
	memcpy(cmd.bssid, bssid, ETH_ALEN);

	cmd.authtype = iw_auth_to_ieee_auth(auth);

J
Johannes Berg 已提交
148
	lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n", bssid, cmd.authtype);
149 150 151 152 153 154 155

	ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);

	lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
	return ret;
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 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

static int lbs_assoc_post(struct lbs_private *priv,
			  struct cmd_ds_802_11_associate_response *resp)
{
	int ret = 0;
	union iwreq_data wrqu;
	struct bss_descriptor *bss;
	u16 status_code;

	lbs_deb_enter(LBS_DEB_ASSOC);

	if (!priv->in_progress_assoc_req) {
		lbs_deb_assoc("ASSOC_RESP: no in-progress assoc request\n");
		ret = -1;
		goto done;
	}
	bss = &priv->in_progress_assoc_req->bss;

	/*
	 * Older FW versions map the IEEE 802.11 Status Code in the association
	 * response to the following values returned in resp->statuscode:
	 *
	 *    IEEE Status Code                Marvell Status Code
	 *    0                       ->      0x0000 ASSOC_RESULT_SUCCESS
	 *    13                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
	 *    14                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
	 *    15                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
	 *    16                      ->      0x0004 ASSOC_RESULT_AUTH_REFUSED
	 *    others                  ->      0x0003 ASSOC_RESULT_REFUSED
	 *
	 * Other response codes:
	 *    0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
	 *    0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
	 *                                    association response from the AP)
	 */

	status_code = le16_to_cpu(resp->statuscode);
	if (priv->fwrelease < 0x09000000) {
		switch (status_code) {
		case 0x00:
			break;
		case 0x01:
			lbs_deb_assoc("ASSOC_RESP: invalid parameters\n");
			break;
		case 0x02:
			lbs_deb_assoc("ASSOC_RESP: internal timer "
				"expired while waiting for the AP\n");
			break;
		case 0x03:
			lbs_deb_assoc("ASSOC_RESP: association "
				"refused by AP\n");
			break;
		case 0x04:
			lbs_deb_assoc("ASSOC_RESP: authentication "
				"refused by AP\n");
			break;
		default:
			lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x "
				" unknown\n", status_code);
			break;
		}
	} else {
		/* v9+ returns the AP's association response */
		lbs_deb_assoc("ASSOC_RESP: failure reason 0x%02x\n", status_code);
	}

	if (status_code) {
		lbs_mac_event_disconnected(priv);
		ret = -1;
		goto done;
	}

	lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_RESP",
		    (void *) (resp + sizeof (resp->hdr)),
		    le16_to_cpu(resp->hdr.size) - sizeof (resp->hdr));

	/* Send a Media Connected event, according to the Spec */
	priv->connect_status = LBS_CONNECTED;

	/* Update current SSID and BSSID */
236
	memcpy(&priv->curbssparams.ssid, &bss->ssid, IEEE80211_MAX_SSID_LEN);
237 238 239 240 241 242 243 244 245 246 247 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
	priv->curbssparams.ssid_len = bss->ssid_len;
	memcpy(priv->curbssparams.bssid, bss->bssid, ETH_ALEN);

	priv->SNR[TYPE_RXPD][TYPE_AVG] = 0;
	priv->NF[TYPE_RXPD][TYPE_AVG] = 0;

	memset(priv->rawSNR, 0x00, sizeof(priv->rawSNR));
	memset(priv->rawNF, 0x00, sizeof(priv->rawNF));
	priv->nextSNRNF = 0;
	priv->numSNRNF = 0;

	netif_carrier_on(priv->dev);
	if (!priv->tx_pending_len)
		netif_wake_queue(priv->dev);

	memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
	wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);

done:
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
	return ret;
}

/**
 *  @brief This function prepares an association-class command.
 *
 *  @param priv      A pointer to struct lbs_private structure
 *  @param assoc_req The association request describing the BSS to associate
 *                   or reassociate with
 *  @param command   The actual command, either CMD_802_11_ASSOCIATE or
 *                   CMD_802_11_REASSOCIATE
 *
 *  @return         0 or -1
 */
static int lbs_associate(struct lbs_private *priv,
			 struct assoc_request *assoc_req,
			 u16 command)
{
	struct cmd_ds_802_11_associate cmd;
	int ret = 0;
	struct bss_descriptor *bss = &assoc_req->bss;
	u8 *pos = &(cmd.iebuf[0]);
	u16 tmpcap, tmplen, tmpauth;
	struct mrvl_ie_ssid_param_set *ssid;
	struct mrvl_ie_ds_param_set *ds;
	struct mrvl_ie_cf_param_set *cf;
	struct mrvl_ie_rates_param_set *rates;
	struct mrvl_ie_rsn_param_set *rsn;
	struct mrvl_ie_auth_type *auth;

	lbs_deb_enter(LBS_DEB_ASSOC);

	BUG_ON((command != CMD_802_11_ASSOCIATE) &&
		(command != CMD_802_11_REASSOCIATE));

	memset(&cmd, 0, sizeof(cmd));
	cmd.hdr.command = cpu_to_le16(command);

	/* Fill in static fields */
	memcpy(cmd.bssid, bss->bssid, ETH_ALEN);
	cmd.listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);

	/* Capability info */
	tmpcap = (bss->capability & CAPINFO_MASK);
	if (bss->mode == IW_MODE_INFRA)
		tmpcap |= WLAN_CAPABILITY_ESS;
	cmd.capability = cpu_to_le16(tmpcap);
	lbs_deb_assoc("ASSOC_CMD: capability 0x%04x\n", tmpcap);

	/* SSID */
	ssid = (struct mrvl_ie_ssid_param_set *) pos;
	ssid->header.type = cpu_to_le16(TLV_TYPE_SSID);
	tmplen = bss->ssid_len;
	ssid->header.len = cpu_to_le16(tmplen);
	memcpy(ssid->ssid, bss->ssid, tmplen);
	pos += sizeof(ssid->header) + tmplen;

	ds = (struct mrvl_ie_ds_param_set *) pos;
	ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
	ds->header.len = cpu_to_le16(1);
	ds->channel = bss->phy.ds.channel;
	pos += sizeof(ds->header) + 1;

	cf = (struct mrvl_ie_cf_param_set *) pos;
	cf->header.type = cpu_to_le16(TLV_TYPE_CF);
	tmplen = sizeof(*cf) - sizeof (cf->header);
	cf->header.len = cpu_to_le16(tmplen);
	/* IE payload should be zeroed, firmware fills it in for us */
	pos += sizeof(*cf);

	rates = (struct mrvl_ie_rates_param_set *) pos;
	rates->header.type = cpu_to_le16(TLV_TYPE_RATES);
330
	tmplen = min_t(u16, ARRAY_SIZE(bss->rates), MAX_RATES);
R
Roel Kluin 已提交
331
	memcpy(&rates->rates, &bss->rates, tmplen);
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
	if (get_common_rates(priv, rates->rates, &tmplen)) {
		ret = -1;
		goto done;
	}
	pos += sizeof(rates->header) + tmplen;
	rates->header.len = cpu_to_le16(tmplen);
	lbs_deb_assoc("ASSOC_CMD: num rates %u\n", tmplen);

	/* Copy the infra. association rates into Current BSS state structure */
	memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
	memcpy(&priv->curbssparams.rates, &rates->rates, tmplen);

	/* Set MSB on basic rates as the firmware requires, but _after_
	 * copying to current bss rates.
	 */
	lbs_set_basic_rate_flags(rates->rates, tmplen);

	/* Firmware v9+ indicate authentication suites as a TLV */
	if (priv->fwrelease >= 0x09000000) {
		auth = (struct mrvl_ie_auth_type *) pos;
		auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
		auth->header.len = cpu_to_le16(2);
		tmpauth = iw_auth_to_ieee_auth(priv->secinfo.auth_mode);
		auth->auth = cpu_to_le16(tmpauth);
		pos += sizeof(auth->header) + 2;

J
Johannes Berg 已提交
358 359
		lbs_deb_join("AUTH_CMD: BSSID %pM, auth 0x%x\n",
			bss->bssid, priv->secinfo.auth_mode);
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
	}

	/* WPA/WPA2 IEs */
	if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
		rsn = (struct mrvl_ie_rsn_param_set *) pos;
		/* WPA_IE or WPA2_IE */
		rsn->header.type = cpu_to_le16((u16) assoc_req->wpa_ie[0]);
		tmplen = (u16) assoc_req->wpa_ie[1];
		rsn->header.len = cpu_to_le16(tmplen);
		memcpy(rsn->rsnie, &assoc_req->wpa_ie[2], tmplen);
		lbs_deb_hex(LBS_DEB_JOIN, "ASSOC_CMD: WPA/RSN IE", (u8 *) rsn,
			sizeof(rsn->header) + tmplen);
		pos += sizeof(rsn->header) + tmplen;
	}

	cmd.hdr.size = cpu_to_le16((sizeof(cmd) - sizeof(cmd.iebuf)) +
				   (u16)(pos - (u8 *) &cmd.iebuf));

	/* update curbssparams */
379
	priv->channel = bss->phy.ds.channel;
380 381 382 383 384 385 386 387 388 389 390 391

	ret = lbs_cmd_with_response(priv, command, &cmd);
	if (ret == 0) {
		ret = lbs_assoc_post(priv,
			(struct cmd_ds_802_11_associate_response *) &cmd);
	}

done:
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
	return ret;
}

392 393 394 395
/**
 *  @brief Associate to a specific BSS discovered in a scan
 *
 *  @param priv      A pointer to struct lbs_private structure
396
 *  @param assoc_req The association request describing the BSS to associate with
397 398 399
 *
 *  @return          0-success, otherwise fail
 */
400
static int lbs_try_associate(struct lbs_private *priv,
401 402 403
	struct assoc_request *assoc_req)
{
	int ret;
404
	u8 preamble = RADIO_PREAMBLE_LONG;
405 406 407

	lbs_deb_enter(LBS_DEB_ASSOC);

408 409 410 411 412 413 414 415 416
	/* FW v9 and higher indicate authentication suites as a TLV in the
	 * association command, not as a separate authentication command.
	 */
	if (priv->fwrelease < 0x09000000) {
		ret = lbs_set_authentication(priv, assoc_req->bss.bssid,
					     priv->secinfo.auth_mode);
		if (ret)
			goto out;
	}
417

418
	/* Use short preamble only when both the BSS and firmware support it */
419 420
	if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
	    (assoc_req->bss.capability & WLAN_CAPABILITY_SHORT_PREAMBLE))
421
		preamble = RADIO_PREAMBLE_SHORT;
422

423 424 425
	ret = lbs_set_radio(priv, preamble, 1);
	if (ret)
		goto out;
426

427
	ret = lbs_associate(priv, assoc_req, CMD_802_11_ASSOCIATE);
428

429
out:
430 431 432 433
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
	return ret;
}

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
static int lbs_adhoc_post(struct lbs_private *priv,
			  struct cmd_ds_802_11_ad_hoc_result *resp)
{
	int ret = 0;
	u16 command = le16_to_cpu(resp->hdr.command);
	u16 result = le16_to_cpu(resp->hdr.result);
	union iwreq_data wrqu;
	struct bss_descriptor *bss;
	DECLARE_SSID_BUF(ssid);

	lbs_deb_enter(LBS_DEB_JOIN);

	if (!priv->in_progress_assoc_req) {
		lbs_deb_join("ADHOC_RESP: no in-progress association "
			"request\n");
		ret = -1;
		goto done;
	}
	bss = &priv->in_progress_assoc_req->bss;

	/*
	 * Join result code 0 --> SUCCESS
	 */
	if (result) {
		lbs_deb_join("ADHOC_RESP: failed (result 0x%X)\n", result);
		if (priv->connect_status == LBS_CONNECTED)
			lbs_mac_event_disconnected(priv);
		ret = -1;
		goto done;
	}

	/* Send a Media Connected event, according to the Spec */
	priv->connect_status = LBS_CONNECTED;

	if (command == CMD_RET(CMD_802_11_AD_HOC_START)) {
		/* Update the created network descriptor with the new BSSID */
		memcpy(bss->bssid, resp->bssid, ETH_ALEN);
	}

	/* Set the BSSID from the joined/started descriptor */
	memcpy(&priv->curbssparams.bssid, bss->bssid, ETH_ALEN);

	/* Set the new SSID to current SSID */
477
	memcpy(&priv->curbssparams.ssid, &bss->ssid, IEEE80211_MAX_SSID_LEN);
478 479 480 481 482 483 484 485 486 487 488 489 490 491
	priv->curbssparams.ssid_len = bss->ssid_len;

	netif_carrier_on(priv->dev);
	if (!priv->tx_pending_len)
		netif_wake_queue(priv->dev);

	memset(&wrqu, 0, sizeof(wrqu));
	memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid, ETH_ALEN);
	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
	wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);

	lbs_deb_join("ADHOC_RESP: Joined/started '%s', BSSID %pM, channel %d\n",
		     print_ssid(ssid, bss->ssid, bss->ssid_len),
		     priv->curbssparams.bssid,
492
		     priv->channel);
493 494 495 496 497 498

done:
	lbs_deb_leave_args(LBS_DEB_JOIN, "ret %d", ret);
	return ret;
}

499 500 501 502
/**
 *  @brief Join an adhoc network found in a previous scan
 *
 *  @param priv         A pointer to struct lbs_private structure
503
 *  @param assoc_req    The association request describing the BSS to join
504
 *
505
 *  @return             0 on success, error on failure
506
 */
507
static int lbs_adhoc_join(struct lbs_private *priv,
508 509
	struct assoc_request *assoc_req)
{
510
	struct cmd_ds_802_11_ad_hoc_join cmd;
511
	struct bss_descriptor *bss = &assoc_req->bss;
512
	u8 preamble = RADIO_PREAMBLE_LONG;
513
	DECLARE_SSID_BUF(ssid);
514 515
	u16 ratesize = 0;
	int ret = 0;
516 517

	lbs_deb_enter(LBS_DEB_ASSOC);
518 519

	lbs_deb_join("current SSID '%s', ssid length %u\n",
520
		print_ssid(ssid, priv->curbssparams.ssid,
521 522 523
		priv->curbssparams.ssid_len),
		priv->curbssparams.ssid_len);
	lbs_deb_join("requested ssid '%s', ssid length %u\n",
524
		print_ssid(ssid, bss->ssid, bss->ssid_len),
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
		bss->ssid_len);

	/* check if the requested SSID is already joined */
	if (priv->curbssparams.ssid_len &&
	    !lbs_ssid_cmp(priv->curbssparams.ssid,
			priv->curbssparams.ssid_len,
			bss->ssid, bss->ssid_len) &&
	    (priv->mode == IW_MODE_ADHOC) &&
	    (priv->connect_status == LBS_CONNECTED)) {
		union iwreq_data wrqu;

		lbs_deb_join("ADHOC_J_CMD: New ad-hoc SSID is the same as "
			"current, not attempting to re-join");

		/* Send the re-association event though, because the association
		 * request really was successful, even if just a null-op.
		 */
		memset(&wrqu, 0, sizeof(wrqu));
		memcpy(wrqu.ap_addr.sa_data, priv->curbssparams.bssid,
		       ETH_ALEN);
		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
		wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
		goto out;
	}

550 551 552
	/* Use short preamble only when both the BSS and firmware support it */
	if ((priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
	    (bss->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)) {
553
		lbs_deb_join("AdhocJoin: Short preamble\n");
554
		preamble = RADIO_PREAMBLE_SHORT;
555 556
	}

557 558 559
	ret = lbs_set_radio(priv, preamble, 1);
	if (ret)
		goto out;
560 561 562 563 564

	lbs_deb_join("AdhocJoin: channel = %d\n", assoc_req->channel);
	lbs_deb_join("AdhocJoin: band = %c\n", assoc_req->band);

	priv->adhoccreate = 0;
565
	priv->channel = bss->channel;
566

567 568 569 570 571 572 573 574 575 576
	/* Build the join command */
	memset(&cmd, 0, sizeof(cmd));
	cmd.hdr.size = cpu_to_le16(sizeof(cmd));

	cmd.bss.type = CMD_BSS_TYPE_IBSS;
	cmd.bss.beaconperiod = cpu_to_le16(bss->beaconperiod);

	memcpy(&cmd.bss.bssid, &bss->bssid, ETH_ALEN);
	memcpy(&cmd.bss.ssid, &bss->ssid, bss->ssid_len);

577
	memcpy(&cmd.bss.ds, &bss->phy.ds, sizeof(struct ieee_ie_ds_param_set));
578

579 580
	memcpy(&cmd.bss.ibss, &bss->ss.ibss,
	       sizeof(struct ieee_ie_ibss_param_set));
581 582 583 584 585 586

	cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
	lbs_deb_join("ADHOC_J_CMD: tmpcap=%4X CAPINFO_MASK=%4X\n",
	       bss->capability, CAPINFO_MASK);

	/* information on BSSID descriptor passed to FW */
J
Johannes Berg 已提交
587 588
	lbs_deb_join("ADHOC_J_CMD: BSSID = %pM, SSID = '%s'\n",
			cmd.bss.bssid, cmd.bss.ssid);
589 590 591 592 593 594 595 596 597 598 599

	/* Only v8 and below support setting these */
	if (priv->fwrelease < 0x09000000) {
		/* failtimeout */
		cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
		/* probedelay */
		cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
	}

	/* Copy Data rates from the rates recorded in scan response */
	memset(cmd.bss.rates, 0, sizeof(cmd.bss.rates));
600
	ratesize = min_t(u16, ARRAY_SIZE(cmd.bss.rates), ARRAY_SIZE (bss->rates));
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
	memcpy(cmd.bss.rates, bss->rates, ratesize);
	if (get_common_rates(priv, cmd.bss.rates, &ratesize)) {
		lbs_deb_join("ADHOC_JOIN: get_common_rates returned error.\n");
		ret = -1;
		goto out;
	}

	/* Copy the ad-hoc creation rates into Current BSS state structure */
	memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
	memcpy(&priv->curbssparams.rates, cmd.bss.rates, ratesize);

	/* Set MSB on basic rates as the firmware requires, but _after_
	 * copying to current bss rates.
	 */
	lbs_set_basic_rate_flags(cmd.bss.rates, ratesize);

617
	cmd.bss.ibss.atimwindow = bss->atimwindow;
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638

	if (assoc_req->secinfo.wep_enabled) {
		u16 tmp = le16_to_cpu(cmd.bss.capability);
		tmp |= WLAN_CAPABILITY_PRIVACY;
		cmd.bss.capability = cpu_to_le16(tmp);
	}

	if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
		__le32 local_ps_mode = cpu_to_le32(LBS802_11POWERMODECAM);

		/* wake up first */
		ret = lbs_prepare_and_send_command(priv, CMD_802_11_PS_MODE,
						   CMD_ACT_SET, 0, 0,
						   &local_ps_mode);
		if (ret) {
			ret = -1;
			goto out;
		}
	}

	ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
639 640 641 642
	if (ret == 0) {
		ret = lbs_adhoc_post(priv,
				     (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
	}
643 644

out:
645
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
646 647 648 649 650 651 652
	return ret;
}

/**
 *  @brief Start an Adhoc Network
 *
 *  @param priv         A pointer to struct lbs_private structure
653
 *  @param assoc_req    The association request describing the BSS to start
654 655
 *
 *  @return             0 on success, error on failure
656
 */
657
static int lbs_adhoc_start(struct lbs_private *priv,
658 659
	struct assoc_request *assoc_req)
{
660
	struct cmd_ds_802_11_ad_hoc_start cmd;
661
	u8 preamble = RADIO_PREAMBLE_LONG;
662 663 664
	size_t ratesize = 0;
	u16 tmpcap = 0;
	int ret = 0;
665
	DECLARE_SSID_BUF(ssid);
666 667

	lbs_deb_enter(LBS_DEB_ASSOC);
668 669

	if (priv->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) {
670
		lbs_deb_join("ADHOC_START: Will use short preamble\n");
671
		preamble = RADIO_PREAMBLE_SHORT;
672 673
	}

674 675 676
	ret = lbs_set_radio(priv, preamble, 1);
	if (ret)
		goto out;
677

678 679 680
	/* Build the start command */
	memset(&cmd, 0, sizeof(cmd));
	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
681

682 683 684
	memcpy(cmd.ssid, assoc_req->ssid, assoc_req->ssid_len);

	lbs_deb_join("ADHOC_START: SSID '%s', ssid length %u\n",
685
		print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
686 687 688 689 690 691 692 693 694 695 696
		assoc_req->ssid_len);

	cmd.bsstype = CMD_BSS_TYPE_IBSS;

	if (priv->beacon_period == 0)
		priv->beacon_period = MRVDRV_BEACON_INTERVAL;
	cmd.beaconperiod = cpu_to_le16(priv->beacon_period);

	WARN_ON(!assoc_req->channel);

	/* set Physical parameter set */
697 698
	cmd.ds.header.id = WLAN_EID_DS_PARAMS;
	cmd.ds.header.len = 1;
699
	cmd.ds.channel = assoc_req->channel;
700 701

	/* set IBSS parameter set */
702 703
	cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
	cmd.ibss.header.len = 2;
704
	cmd.ibss.atimwindow = cpu_to_le16(0);
705 706 707

	/* set capability info */
	tmpcap = WLAN_CAPABILITY_IBSS;
708 709 710 711
	if (assoc_req->secinfo.wep_enabled ||
	    assoc_req->secinfo.WPAenabled ||
	    assoc_req->secinfo.WPA2enabled) {
		lbs_deb_join("ADHOC_START: WEP/WPA enabled, privacy on\n");
712 713
		tmpcap |= WLAN_CAPABILITY_PRIVACY;
	} else
714
		lbs_deb_join("ADHOC_START: WEP disabled, privacy off\n");
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744

	cmd.capability = cpu_to_le16(tmpcap);

	/* Only v8 and below support setting probe delay */
	if (priv->fwrelease < 0x09000000)
		cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);

	ratesize = min(sizeof(cmd.rates), sizeof(lbs_bg_rates));
	memcpy(cmd.rates, lbs_bg_rates, ratesize);

	/* Copy the ad-hoc creating rates into Current BSS state structure */
	memset(&priv->curbssparams.rates, 0, sizeof(priv->curbssparams.rates));
	memcpy(&priv->curbssparams.rates, &cmd.rates, ratesize);

	/* Set MSB on basic rates as the firmware requires, but _after_
	 * copying to current bss rates.
	 */
	lbs_set_basic_rate_flags(cmd.rates, ratesize);

	lbs_deb_join("ADHOC_START: rates=%02x %02x %02x %02x\n",
	       cmd.rates[0], cmd.rates[1], cmd.rates[2], cmd.rates[3]);

	lbs_deb_join("ADHOC_START: Starting Ad-Hoc BSS on channel %d, band %d\n",
		     assoc_req->channel, assoc_req->band);

	priv->adhoccreate = 1;
	priv->mode = IW_MODE_ADHOC;

	ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
	if (ret == 0)
745 746
		ret = lbs_adhoc_post(priv,
				     (struct cmd_ds_802_11_ad_hoc_result *)&cmd);
747

748 749
out:
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
750 751 752
	return ret;
}

753 754 755 756 757 758 759
/**
 *  @brief Stop and Ad-Hoc network and exit Ad-Hoc mode
 *
 *  @param priv         A pointer to struct lbs_private structure
 *  @return             0 on success, or an error
 */
int lbs_adhoc_stop(struct lbs_private *priv)
760
{
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
	struct cmd_ds_802_11_ad_hoc_stop cmd;
	int ret;

	lbs_deb_enter(LBS_DEB_JOIN);

	memset(&cmd, 0, sizeof (cmd));
	cmd.hdr.size = cpu_to_le16 (sizeof (cmd));

	ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);

	/* Clean up everything even if there was an error */
	lbs_mac_event_disconnected(priv);

	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
	return ret;
776
}
777

778 779 780 781 782
static inline int match_bss_no_security(struct lbs_802_11_security *secinfo,
					struct bss_descriptor *match_bss)
{
	if (!secinfo->wep_enabled  && !secinfo->WPAenabled
	    && !secinfo->WPA2enabled
J
Johannes Berg 已提交
783 784
	    && match_bss->wpa_ie[0] != WLAN_EID_GENERIC
	    && match_bss->rsn_ie[0] != WLAN_EID_RSN
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
	    && !(match_bss->capability & WLAN_CAPABILITY_PRIVACY))
		return 1;
	else
		return 0;
}

static inline int match_bss_static_wep(struct lbs_802_11_security *secinfo,
				       struct bss_descriptor *match_bss)
{
	if (secinfo->wep_enabled && !secinfo->WPAenabled
	    && !secinfo->WPA2enabled
	    && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
		return 1;
	else
		return 0;
}

static inline int match_bss_wpa(struct lbs_802_11_security *secinfo,
				struct bss_descriptor *match_bss)
{
	if (!secinfo->wep_enabled && secinfo->WPAenabled
J
Johannes Berg 已提交
806
	    && (match_bss->wpa_ie[0] == WLAN_EID_GENERIC)
807 808 809 810 811 812 813 814 815 816 817 818
	    /* privacy bit may NOT be set in some APs like LinkSys WRT54G
	    && (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
	   )
		return 1;
	else
		return 0;
}

static inline int match_bss_wpa2(struct lbs_802_11_security *secinfo,
				 struct bss_descriptor *match_bss)
{
	if (!secinfo->wep_enabled && secinfo->WPA2enabled &&
J
Johannes Berg 已提交
819
	    (match_bss->rsn_ie[0] == WLAN_EID_RSN)
820 821 822 823 824 825 826 827 828 829 830 831 832
	    /* privacy bit may NOT be set in some APs like LinkSys WRT54G
	    (match_bss->capability & WLAN_CAPABILITY_PRIVACY) */
	   )
		return 1;
	else
		return 0;
}

static inline int match_bss_dynamic_wep(struct lbs_802_11_security *secinfo,
					struct bss_descriptor *match_bss)
{
	if (!secinfo->wep_enabled && !secinfo->WPAenabled
	    && !secinfo->WPA2enabled
J
Johannes Berg 已提交
833 834
	    && (match_bss->wpa_ie[0] != WLAN_EID_GENERIC)
	    && (match_bss->rsn_ie[0] != WLAN_EID_RSN)
835 836 837 838 839 840 841 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 872 873 874 875 876 877 878 879 880 881 882 883 884 885 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 926 927 928 929 930 931 932 933 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 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	    && (match_bss->capability & WLAN_CAPABILITY_PRIVACY))
		return 1;
	else
		return 0;
}

/**
 *  @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 priv A pointer to struct lbs_private
 *  @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
 */
static int is_network_compatible(struct lbs_private *priv,
				 struct bss_descriptor *bss, uint8_t mode)
{
	int matched = 0;

	lbs_deb_enter(LBS_DEB_SCAN);

	if (bss->mode != mode)
		goto done;

	matched = match_bss_no_security(&priv->secinfo, bss);
	if (matched)
		goto done;
	matched = match_bss_static_wep(&priv->secinfo, bss);
	if (matched)
		goto done;
	matched = match_bss_wpa(&priv->secinfo, bss);
	if (matched) {
		lbs_deb_scan("is_network_compatible() WPA: wpa_ie 0x%x "
			     "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
			     "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
			     priv->secinfo.wep_enabled ? "e" : "d",
			     priv->secinfo.WPAenabled ? "e" : "d",
			     priv->secinfo.WPA2enabled ? "e" : "d",
			     (bss->capability & WLAN_CAPABILITY_PRIVACY));
		goto done;
	}
	matched = match_bss_wpa2(&priv->secinfo, bss);
	if (matched) {
		lbs_deb_scan("is_network_compatible() WPA2: wpa_ie 0x%x "
			     "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s "
			     "privacy 0x%x\n", bss->wpa_ie[0], bss->rsn_ie[0],
			     priv->secinfo.wep_enabled ? "e" : "d",
			     priv->secinfo.WPAenabled ? "e" : "d",
			     priv->secinfo.WPA2enabled ? "e" : "d",
			     (bss->capability & WLAN_CAPABILITY_PRIVACY));
		goto done;
	}
	matched = match_bss_dynamic_wep(&priv->secinfo, bss);
	if (matched) {
		lbs_deb_scan("is_network_compatible() dynamic WEP: "
			     "wpa_ie 0x%x wpa2_ie 0x%x privacy 0x%x\n",
			     bss->wpa_ie[0], bss->rsn_ie[0],
			     (bss->capability & WLAN_CAPABILITY_PRIVACY));
		goto done;
	}

	/* bss security settings don't match those configured on card */
	lbs_deb_scan("is_network_compatible() FAILED: wpa_ie 0x%x "
		     "wpa2_ie 0x%x WEP %s WPA %s WPA2 %s privacy 0x%x\n",
		     bss->wpa_ie[0], bss->rsn_ie[0],
		     priv->secinfo.wep_enabled ? "e" : "d",
		     priv->secinfo.WPAenabled ? "e" : "d",
		     priv->secinfo.WPA2enabled ? "e" : "d",
		     (bss->capability & WLAN_CAPABILITY_PRIVACY));

done:
	lbs_deb_leave_args(LBS_DEB_SCAN, "matched: %d", matched);
	return matched;
}

/**
 *  @brief This function finds a specific compatible BSSID in the scan list
 *
 *  Used in association code
 *
 *  @param priv  A pointer to struct lbs_private
 *  @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)
 */
static struct bss_descriptor *lbs_find_bssid_in_list(struct lbs_private *priv,
					      uint8_t *bssid, uint8_t mode)
{
	struct bss_descriptor *iter_bss;
	struct bss_descriptor *found_bss = NULL;

	lbs_deb_enter(LBS_DEB_SCAN);

	if (!bssid)
		goto out;

	lbs_deb_hex(LBS_DEB_SCAN, "looking for", bssid, ETH_ALEN);

	/* Look through the scan table for a compatible match.  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
	 */
	mutex_lock(&priv->lock);
	list_for_each_entry(iter_bss, &priv->network_list, list) {
		if (compare_ether_addr(iter_bss->bssid, bssid))
			continue; /* bssid doesn't match */
		switch (mode) {
		case IW_MODE_INFRA:
		case IW_MODE_ADHOC:
			if (!is_network_compatible(priv, iter_bss, mode))
				break;
			found_bss = iter_bss;
			break;
		default:
			found_bss = iter_bss;
			break;
		}
	}
	mutex_unlock(&priv->lock);

out:
	lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
	return found_bss;
}

/**
 *  @brief This function finds ssid in ssid list.
 *
 *  Used in association code
 *
 *  @param priv  A pointer to struct lbs_private
 *  @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
 */
static struct bss_descriptor *lbs_find_ssid_in_list(struct lbs_private *priv,
					     uint8_t *ssid, uint8_t ssid_len,
					     uint8_t *bssid, uint8_t mode,
					     int channel)
{
	u32 bestrssi = 0;
	struct bss_descriptor *iter_bss = NULL;
	struct bss_descriptor *found_bss = NULL;
	struct bss_descriptor *tmp_oldest = NULL;

	lbs_deb_enter(LBS_DEB_SCAN);

	mutex_lock(&priv->lock);

	list_for_each_entry(iter_bss, &priv->network_list, list) {
		if (!tmp_oldest ||
		    (iter_bss->last_scanned < tmp_oldest->last_scanned))
			tmp_oldest = iter_bss;

		if (lbs_ssid_cmp(iter_bss->ssid, iter_bss->ssid_len,
				 ssid, ssid_len) != 0)
			continue; /* ssid doesn't match */
		if (bssid && compare_ether_addr(iter_bss->bssid, bssid) != 0)
			continue; /* bssid doesn't match */
		if ((channel > 0) && (iter_bss->channel != channel))
			continue; /* channel doesn't match */

		switch (mode) {
		case IW_MODE_INFRA:
		case IW_MODE_ADHOC:
			if (!is_network_compatible(priv, iter_bss, mode))
				break;

			if (bssid) {
				/* Found requested BSSID */
				found_bss = iter_bss;
				goto out;
			}

			if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
				bestrssi = SCAN_RSSI(iter_bss->rssi);
				found_bss = iter_bss;
			}
			break;
		case IW_MODE_AUTO:
		default:
			if (SCAN_RSSI(iter_bss->rssi) > bestrssi) {
				bestrssi = SCAN_RSSI(iter_bss->rssi);
				found_bss = iter_bss;
			}
			break;
		}
	}

out:
	mutex_unlock(&priv->lock);
	lbs_deb_leave_args(LBS_DEB_SCAN, "found_bss %p", found_bss);
	return found_bss;
}

1044
static int assoc_helper_essid(struct lbs_private *priv,
1045 1046 1047
                              struct assoc_request * assoc_req)
{
	int ret = 0;
1048
	struct bss_descriptor * bss;
1049
	int channel = -1;
1050
	DECLARE_SSID_BUF(ssid);
1051

1052
	lbs_deb_enter(LBS_DEB_ASSOC);
1053

1054 1055 1056 1057
	/* FIXME: take channel into account when picking SSIDs if a channel
	 * is set.
	 */

1058 1059 1060
	if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
		channel = assoc_req->channel;

1061
	lbs_deb_assoc("SSID '%s' requested\n",
1062
	              print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len));
1063
	if (assoc_req->mode == IW_MODE_INFRA) {
1064
		lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
1065
			assoc_req->ssid_len);
1066

1067
		bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
1068
				assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
1069
		if (bss != NULL) {
1070
			memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1071
			ret = lbs_try_associate(priv, assoc_req);
1072
		} else {
1073
			lbs_deb_assoc("SSID not found; cannot associate\n");
1074
		}
1075
	} else if (assoc_req->mode == IW_MODE_ADHOC) {
1076 1077 1078
		/* Scan for the network, do not save previous results.  Stale
		 *   scan data will cause us to join a non-existant adhoc network
		 */
1079
		lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
1080
			assoc_req->ssid_len);
1081 1082

		/* Search for the requested SSID in the scan table */
1083
		bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
1084
				assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
1085
		if (bss != NULL) {
1086
			lbs_deb_assoc("SSID found, will join\n");
1087
			memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1088
			lbs_adhoc_join(priv, assoc_req);
1089 1090
		} else {
			/* else send START command */
1091
			lbs_deb_assoc("SSID not found, creating adhoc network\n");
1092
			memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
1093
				IEEE80211_MAX_SSID_LEN);
1094
			assoc_req->bss.ssid_len = assoc_req->ssid_len;
1095
			lbs_adhoc_start(priv, assoc_req);
1096 1097 1098
		}
	}

1099
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1100 1101 1102 1103
	return ret;
}


1104
static int assoc_helper_bssid(struct lbs_private *priv,
1105 1106
                              struct assoc_request * assoc_req)
{
1107 1108
	int ret = 0;
	struct bss_descriptor * bss;
1109

J
Johannes Berg 已提交
1110
	lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %pM", assoc_req->bssid);
1111 1112

	/* Search for index position in list for requested MAC */
1113
	bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
1114
			    assoc_req->mode);
1115
	if (bss == NULL) {
J
Johannes Berg 已提交
1116 1117
		lbs_deb_assoc("ASSOC: WAP: BSSID %pM not found, "
			"cannot associate.\n", assoc_req->bssid);
1118 1119 1120
		goto out;
	}

1121
	memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
1122
	if (assoc_req->mode == IW_MODE_INFRA) {
1123 1124 1125
		ret = lbs_try_associate(priv, assoc_req);
		lbs_deb_assoc("ASSOC: lbs_try_associate(bssid) returned %d\n",
			      ret);
1126
	} else if (assoc_req->mode == IW_MODE_ADHOC) {
1127
		lbs_adhoc_join(priv, assoc_req);
1128 1129 1130
	}

out:
1131
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1132 1133 1134 1135
	return ret;
}


1136
static int assoc_helper_associate(struct lbs_private *priv,
1137 1138 1139 1140
                                  struct assoc_request * assoc_req)
{
	int ret = 0, done = 0;

1141 1142
	lbs_deb_enter(LBS_DEB_ASSOC);

1143 1144 1145
	/* If we're given and 'any' BSSID, try associating based on SSID */

	if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1146 1147
		if (compare_ether_addr(bssid_any, assoc_req->bssid)
		    && compare_ether_addr(bssid_off, assoc_req->bssid)) {
1148 1149 1150 1151 1152 1153 1154 1155 1156
			ret = assoc_helper_bssid(priv, assoc_req);
			done = 1;
		}
	}

	if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
		ret = assoc_helper_essid(priv, assoc_req);
	}

1157
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1158 1159 1160 1161
	return ret;
}


1162
static int assoc_helper_mode(struct lbs_private *priv,
1163 1164 1165 1166
                             struct assoc_request * assoc_req)
{
	int ret = 0;

1167
	lbs_deb_enter(LBS_DEB_ASSOC);
1168

1169
	if (assoc_req->mode == priv->mode)
1170
		goto done;
1171

1172
	if (assoc_req->mode == IW_MODE_INFRA) {
1173
		if (priv->psstate != PS_STATE_FULL_POWER)
1174
			lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1175
		priv->psmode = LBS802_11POWERMODECAM;
1176 1177
	}

1178
	priv->mode = assoc_req->mode;
1179
	ret = lbs_set_snmp_mib(priv, SNMP_MIB_OID_BSS_TYPE, assoc_req->mode);
1180

1181 1182
done:
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1183 1184 1185
	return ret;
}

1186
static int assoc_helper_channel(struct lbs_private *priv,
1187 1188 1189 1190 1191 1192
                                struct assoc_request * assoc_req)
{
	int ret = 0;

	lbs_deb_enter(LBS_DEB_ASSOC);

1193
	ret = lbs_update_channel(priv);
1194
	if (ret) {
1195
		lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
1196
		goto done;
1197 1198
	}

1199
	if (assoc_req->channel == priv->channel)
1200 1201
		goto done;

1202
	if (priv->mesh_dev) {
1203 1204 1205
		/* Change mesh channel first; 21.p21 firmware won't let
		   you change channel otherwise (even though it'll return
		   an error to this */
1206 1207
		lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
				assoc_req->channel);
1208 1209
	}

1210
	lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
1211
		      priv->channel, assoc_req->channel);
1212

1213 1214
	ret = lbs_set_channel(priv, assoc_req->channel);
	if (ret < 0)
1215
		lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
1216

1217 1218 1219
	/* FIXME: shouldn't need to grab the channel _again_ after setting
	 * it since the firmware is supposed to return the new channel, but
	 * whatever... */
1220
	ret = lbs_update_channel(priv);
1221
	if (ret) {
1222
		lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
1223 1224
		goto done;
	}
1225

1226
	if (assoc_req->channel != priv->channel) {
1227
		lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
1228
		              assoc_req->channel);
1229
		goto restore_mesh;
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	}

	if (   assoc_req->secinfo.wep_enabled
	    &&   (assoc_req->wep_keys[0].len
	       || assoc_req->wep_keys[1].len
	       || assoc_req->wep_keys[2].len
	       || assoc_req->wep_keys[3].len)) {
		/* Make sure WEP keys are re-sent to firmware */
		set_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags);
	}

	/* Must restart/rejoin adhoc networks after channel change */
1242
 	set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);
1243

1244 1245
 restore_mesh:
	if (priv->mesh_dev)
1246
		lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1247
				priv->channel);
1248 1249

 done:
1250 1251 1252 1253 1254
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
	return ret;
}


1255
static int assoc_helper_wep_keys(struct lbs_private *priv,
1256
				 struct assoc_request *assoc_req)
1257 1258 1259 1260
{
	int i;
	int ret = 0;

1261
	lbs_deb_enter(LBS_DEB_ASSOC);
1262 1263

	/* Set or remove WEP keys */
1264 1265 1266 1267 1268
	if (assoc_req->wep_keys[0].len || assoc_req->wep_keys[1].len ||
	    assoc_req->wep_keys[2].len || assoc_req->wep_keys[3].len)
		ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_ADD, assoc_req);
	else
		ret = lbs_cmd_802_11_set_wep(priv, CMD_ACT_REMOVE, assoc_req);
1269 1270 1271 1272 1273

	if (ret)
		goto out;

	/* enable/disable the MAC's WEP packet filter */
1274
	if (assoc_req->secinfo.wep_enabled)
1275
		priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
1276
	else
1277
		priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
1278

1279
	lbs_set_mac_control(priv);
1280

1281
	mutex_lock(&priv->lock);
1282

1283
	/* Copy WEP keys into priv wep key fields */
1284
	for (i = 0; i < 4; i++) {
1285
		memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
1286
		       sizeof(struct enc_key));
1287
	}
1288
	priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
1289

1290
	mutex_unlock(&priv->lock);
1291 1292

out:
1293
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1294 1295 1296
	return ret;
}

1297
static int assoc_helper_secinfo(struct lbs_private *priv,
1298 1299 1300
                                struct assoc_request * assoc_req)
{
	int ret = 0;
1301 1302
	uint16_t do_wpa;
	uint16_t rsn = 0;
1303

1304
	lbs_deb_enter(LBS_DEB_ASSOC);
1305

1306
	memcpy(&priv->secinfo, &assoc_req->secinfo,
1307
		sizeof(struct lbs_802_11_security));
1308

1309
	lbs_set_mac_control(priv);
1310

1311 1312 1313 1314 1315 1316
	/* If RSN is already enabled, don't try to enable it again, since
	 * ENABLE_RSN resets internal state machines and will clobber the
	 * 4-way WPA handshake.
	 */

	/* Get RSN enabled/disabled */
1317
	ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
1318
	if (ret) {
1319
		lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
1320 1321 1322 1323
		goto out;
	}

	/* Don't re-enable RSN if it's already enabled */
1324
	do_wpa = assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled;
1325 1326 1327 1328
	if (do_wpa == rsn)
		goto out;

	/* Set RSN enabled/disabled */
1329
	ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
1330 1331

out:
1332
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1333 1334 1335 1336
	return ret;
}


1337
static int assoc_helper_wpa_keys(struct lbs_private *priv,
1338 1339 1340
                                 struct assoc_request * assoc_req)
{
	int ret = 0;
1341
	unsigned int flags = assoc_req->flags;
1342

1343
	lbs_deb_enter(LBS_DEB_ASSOC);
1344

1345 1346 1347 1348
	/* Work around older firmware bug where WPA unicast and multicast
	 * keys must be set independently.  Seen in SDIO parts with firmware
	 * version 5.0.11p0.
	 */
1349

1350 1351
	if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
		clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
1352
		ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1353 1354 1355 1356 1357 1358
		assoc_req->flags = flags;
	}

	if (ret)
		goto out;

1359 1360 1361
	memcpy(&priv->wpa_unicast_key, &assoc_req->wpa_unicast_key,
			sizeof(struct enc_key));

1362 1363 1364
	if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
		clear_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags);

1365
		ret = lbs_cmd_802_11_key_material(priv, CMD_ACT_SET, assoc_req);
1366
		assoc_req->flags = flags;
1367 1368 1369

		memcpy(&priv->wpa_mcast_key, &assoc_req->wpa_mcast_key,
				sizeof(struct enc_key));
1370 1371 1372
	}

out:
1373
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1374 1375 1376 1377
	return ret;
}


1378
static int assoc_helper_wpa_ie(struct lbs_private *priv,
1379 1380 1381 1382
                               struct assoc_request * assoc_req)
{
	int ret = 0;

1383
	lbs_deb_enter(LBS_DEB_ASSOC);
1384 1385

	if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
1386 1387
		memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
		priv->wpa_ie_len = assoc_req->wpa_ie_len;
1388
	} else {
1389 1390
		memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
		priv->wpa_ie_len = 0;
1391 1392
	}

1393
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1394 1395 1396 1397
	return ret;
}


1398
static int should_deauth_infrastructure(struct lbs_private *priv,
1399 1400
                                        struct assoc_request * assoc_req)
{
1401 1402
	int ret = 0;

1403
	if (priv->connect_status != LBS_CONNECTED)
1404 1405
		return 0;

1406
	lbs_deb_enter(LBS_DEB_ASSOC);
1407
	if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1408 1409 1410
		lbs_deb_assoc("Deauthenticating due to new SSID\n");
		ret = 1;
		goto out;
1411 1412 1413
	}

	if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1414
		if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
1415 1416 1417
			lbs_deb_assoc("Deauthenticating due to new security\n");
			ret = 1;
			goto out;
1418 1419 1420 1421
		}
	}

	if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1422 1423 1424
		lbs_deb_assoc("Deauthenticating due to new BSSID\n");
		ret = 1;
		goto out;
1425 1426
	}

1427
	if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1428 1429 1430
		lbs_deb_assoc("Deauthenticating due to channel switch\n");
		ret = 1;
		goto out;
1431 1432
	}

1433 1434
	/* FIXME: deal with 'auto' mode somehow */
	if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1435 1436 1437 1438 1439 1440
		if (assoc_req->mode != IW_MODE_INFRA) {
			lbs_deb_assoc("Deauthenticating due to leaving "
				"infra mode\n");
			ret = 1;
			goto out;
		}
1441 1442
	}

1443 1444
out:
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
1445
	return ret;
1446 1447 1448
}


1449
static int should_stop_adhoc(struct lbs_private *priv,
1450 1451
                             struct assoc_request * assoc_req)
{
1452 1453
	lbs_deb_enter(LBS_DEB_ASSOC);

1454
	if (priv->connect_status != LBS_CONNECTED)
1455 1456
		return 0;

1457 1458
	if (lbs_ssid_cmp(priv->curbssparams.ssid,
	                      priv->curbssparams.ssid_len,
1459
	                      assoc_req->ssid, assoc_req->ssid_len) != 0)
1460 1461 1462 1463
		return 1;

	/* FIXME: deal with 'auto' mode somehow */
	if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
1464
		if (assoc_req->mode != IW_MODE_ADHOC)
1465 1466 1467
			return 1;
	}

1468
	if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
1469
		if (assoc_req->channel != priv->channel)
1470 1471 1472
			return 1;
	}

1473
	lbs_deb_leave(LBS_DEB_ASSOC);
1474 1475 1476 1477
	return 0;
}


1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 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
/**
 *  @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 priv  A pointer to struct lbs_private
 *
 *  @return         index in BSSID list
 */
static struct bss_descriptor *lbs_find_best_ssid_in_list(
	struct lbs_private *priv, uint8_t mode)
{
	uint8_t bestrssi = 0;
	struct bss_descriptor *iter_bss;
	struct bss_descriptor *best_bss = NULL;

	lbs_deb_enter(LBS_DEB_SCAN);

	mutex_lock(&priv->lock);

	list_for_each_entry(iter_bss, &priv->network_list, list) {
		switch (mode) {
		case IW_MODE_INFRA:
		case IW_MODE_ADHOC:
			if (!is_network_compatible(priv, iter_bss, mode))
				break;
			if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
				break;
			bestrssi = SCAN_RSSI(iter_bss->rssi);
			best_bss = iter_bss;
			break;
		case IW_MODE_AUTO:
		default:
			if (SCAN_RSSI(iter_bss->rssi) <= bestrssi)
				break;
			bestrssi = SCAN_RSSI(iter_bss->rssi);
			best_bss = iter_bss;
			break;
		}
	}

	mutex_unlock(&priv->lock);
	lbs_deb_leave_args(LBS_DEB_SCAN, "best_bss %p", best_bss);
	return best_bss;
}

/**
 *  @brief Find the best AP
 *
 *  Used from association worker.
 *
 *  @param priv         A pointer to struct lbs_private structure
 *  @param pSSID        A pointer to AP's ssid
 *
 *  @return             0--success, otherwise--fail
 */
static int lbs_find_best_network_ssid(struct lbs_private *priv,
	uint8_t *out_ssid, uint8_t *out_ssid_len, uint8_t preferred_mode,
	uint8_t *out_mode)
{
	int ret = -1;
	struct bss_descriptor *found;

	lbs_deb_enter(LBS_DEB_SCAN);

	priv->scan_ssid_len = 0;
	lbs_scan_networks(priv, 1);
	if (priv->surpriseremoved)
		goto out;

	found = lbs_find_best_ssid_in_list(priv, preferred_mode);
	if (found && (found->ssid_len > 0)) {
1551
		memcpy(out_ssid, &found->ssid, IEEE80211_MAX_SSID_LEN);
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
		*out_ssid_len = found->ssid_len;
		*out_mode = found->mode;
		ret = 0;
	}

out:
	lbs_deb_leave_args(LBS_DEB_SCAN, "ret %d", ret);
	return ret;
}


1563
void lbs_association_worker(struct work_struct *work)
1564
{
1565 1566
	struct lbs_private *priv = container_of(work, struct lbs_private,
		assoc_work.work);
1567 1568 1569
	struct assoc_request * assoc_req = NULL;
	int ret = 0;
	int find_any_ssid = 0;
1570
	DECLARE_SSID_BUF(ssid);
1571

1572
	lbs_deb_enter(LBS_DEB_ASSOC);
1573

1574 1575 1576 1577 1578
	mutex_lock(&priv->lock);
	assoc_req = priv->pending_assoc_req;
	priv->pending_assoc_req = NULL;
	priv->in_progress_assoc_req = assoc_req;
	mutex_unlock(&priv->lock);
1579

1580 1581
	if (!assoc_req)
		goto done;
1582

1583 1584 1585 1586 1587 1588 1589
	lbs_deb_assoc(
		"Association Request:\n"
		"    flags:     0x%08lx\n"
		"    SSID:      '%s'\n"
		"    chann:     %d\n"
		"    band:      %d\n"
		"    mode:      %d\n"
J
Johannes Berg 已提交
1590
		"    BSSID:     %pM\n"
1591 1592 1593
		"    secinfo:  %s%s%s\n"
		"    auth_mode: %d\n",
		assoc_req->flags,
1594
		print_ssid(ssid, assoc_req->ssid, assoc_req->ssid_len),
1595
		assoc_req->channel, assoc_req->band, assoc_req->mode,
J
Johannes Berg 已提交
1596
		assoc_req->bssid,
1597 1598 1599 1600
		assoc_req->secinfo.WPAenabled ? " WPA" : "",
		assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
		assoc_req->secinfo.wep_enabled ? " WEP" : "",
		assoc_req->secinfo.auth_mode);
1601 1602 1603

	/* If 'any' SSID was specified, find an SSID to associate with */
	if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
1604
	    && !assoc_req->ssid_len)
1605 1606 1607 1608
		find_any_ssid = 1;

	/* But don't use 'any' SSID if there's a valid locked BSSID to use */
	if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1609 1610
		if (compare_ether_addr(assoc_req->bssid, bssid_any)
		    && compare_ether_addr(assoc_req->bssid, bssid_off))
1611 1612 1613 1614
			find_any_ssid = 0;
	}

	if (find_any_ssid) {
1615
		u8 new_mode = assoc_req->mode;
1616

1617
		ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
1618
				&assoc_req->ssid_len, assoc_req->mode, &new_mode);
1619
		if (ret) {
1620
			lbs_deb_assoc("Could not find best network\n");
1621 1622 1623 1624 1625
			ret = -ENETUNREACH;
			goto out;
		}

		/* Ensure we switch to the mode of the AP */
1626
		if (assoc_req->mode == IW_MODE_AUTO) {
1627 1628 1629 1630 1631 1632 1633 1634 1635
			set_bit(ASSOC_FLAG_MODE, &assoc_req->flags);
			assoc_req->mode = new_mode;
		}
	}

	/*
	 * Check if the attributes being changing require deauthentication
	 * from the currently associated infrastructure access point.
	 */
1636 1637
	if (priv->mode == IW_MODE_INFRA) {
		if (should_deauth_infrastructure(priv, assoc_req)) {
1638 1639 1640
			ret = lbs_cmd_80211_deauthenticate(priv,
							   priv->curbssparams.bssid,
							   WLAN_REASON_DEAUTH_LEAVING);
1641
			if (ret) {
1642
				lbs_deb_assoc("Deauthentication due to new "
1643 1644 1645 1646
					"configuration request failed: %d\n",
					ret);
			}
		}
1647 1648
	} else if (priv->mode == IW_MODE_ADHOC) {
		if (should_stop_adhoc(priv, assoc_req)) {
1649
			ret = lbs_adhoc_stop(priv);
1650
			if (ret) {
1651
				lbs_deb_assoc("Teardown of AdHoc network due to "
1652 1653 1654 1655 1656 1657 1658 1659 1660 1661
					"new configuration request failed: %d\n",
					ret);
			}

		}
	}

	/* Send the various configuration bits to the firmware */
	if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
		ret = assoc_helper_mode(priv, assoc_req);
1662
		if (ret)
1663 1664 1665
			goto out;
	}

1666 1667
	if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
		ret = assoc_helper_channel(priv, assoc_req);
1668
		if (ret)
1669 1670 1671
			goto out;
	}

1672 1673 1674
	if (   test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)
	    || test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags)) {
		ret = assoc_helper_wep_keys(priv, assoc_req);
1675
		if (ret)
1676 1677 1678 1679 1680
			goto out;
	}

	if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
		ret = assoc_helper_secinfo(priv, assoc_req);
1681
		if (ret)
1682 1683 1684 1685 1686
			goto out;
	}

	if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
		ret = assoc_helper_wpa_ie(priv, assoc_req);
1687
		if (ret)
1688 1689 1690 1691 1692 1693
			goto out;
	}

	if (test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)
	    || test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
		ret = assoc_helper_wpa_keys(priv, assoc_req);
1694
		if (ret)
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
			goto out;
	}

	/* SSID/BSSID should be the _last_ config option set, because they
	 * trigger the association attempt.
	 */
	if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)
	    || test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
		int success = 1;

		ret = assoc_helper_associate(priv, assoc_req);
		if (ret) {
1707
			lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
1708 1709 1710 1711
				ret);
			success = 0;
		}

1712
		if (priv->connect_status != LBS_CONNECTED) {
1713 1714
			lbs_deb_assoc("ASSOC: association unsuccessful, "
				"not connected\n");
1715 1716 1717 1718
			success = 0;
		}

		if (success) {
J
Johannes Berg 已提交
1719 1720
			lbs_deb_assoc("associated to %pM\n",
				priv->curbssparams.bssid);
1721
			lbs_prepare_and_send_command(priv,
1722 1723
				CMD_802_11_RSSI,
				0, CMD_OPTION_WAITFORRSP, 0, NULL);
1724 1725 1726 1727 1728 1729 1730
		} else {
			ret = -1;
		}
	}

out:
	if (ret) {
1731
		lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
1732 1733
			ret);
	}
1734

1735 1736 1737
	mutex_lock(&priv->lock);
	priv->in_progress_assoc_req = NULL;
	mutex_unlock(&priv->lock);
1738
	kfree(assoc_req);
1739 1740 1741

done:
	lbs_deb_leave(LBS_DEB_ASSOC);
1742 1743 1744 1745 1746 1747
}


/*
 * Caller MUST hold any necessary locks
 */
1748
struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
1749 1750 1751
{
	struct assoc_request * assoc_req;

1752
	lbs_deb_enter(LBS_DEB_ASSOC);
1753 1754
	if (!priv->pending_assoc_req) {
		priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
1755
		                                     GFP_KERNEL);
1756
		if (!priv->pending_assoc_req) {
1757 1758 1759 1760 1761 1762 1763 1764 1765
			lbs_pr_info("Not enough memory to allocate association"
				" request!\n");
			return NULL;
		}
	}

	/* Copy current configuration attributes to the association request,
	 * but don't overwrite any that are already set.
	 */
1766
	assoc_req = priv->pending_assoc_req;
1767
	if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
1768
		memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
1769
		       IEEE80211_MAX_SSID_LEN);
1770
		assoc_req->ssid_len = priv->curbssparams.ssid_len;
1771 1772 1773
	}

	if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
1774
		assoc_req->channel = priv->channel;
1775

1776
	if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
1777
		assoc_req->band = priv->curbssparams.band;
1778

1779
	if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
1780
		assoc_req->mode = priv->mode;
1781 1782

	if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
1783
		memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
1784 1785 1786 1787 1788 1789
			ETH_ALEN);
	}

	if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
		int i;
		for (i = 0; i < 4; i++) {
1790
			memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
1791
				sizeof(struct enc_key));
1792 1793 1794 1795
		}
	}

	if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
1796
		assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
1797 1798

	if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
1799
		memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
1800
			sizeof(struct enc_key));
1801 1802 1803
	}

	if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
1804
		memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
1805
			sizeof(struct enc_key));
1806 1807 1808
	}

	if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
1809
		memcpy(&assoc_req->secinfo, &priv->secinfo,
1810
			sizeof(struct lbs_802_11_security));
1811 1812 1813
	}

	if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
1814
		memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
1815
			MAX_WPA_IE_LEN);
1816
		assoc_req->wpa_ie_len = priv->wpa_ie_len;
1817 1818
	}

1819
	lbs_deb_leave(LBS_DEB_ASSOC);
1820 1821
	return assoc_req;
}
1822 1823


1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
/**
 *  @brief Deauthenticate from a specific BSS
 *
 *  @param priv        A pointer to struct lbs_private structure
 *  @param bssid       The specific BSS to deauthenticate from
 *  @param reason      The 802.11 sec. 7.3.1.7 Reason Code for deauthenticating
 *
 *  @return            0 on success, error on failure
 */
int lbs_cmd_80211_deauthenticate(struct lbs_private *priv, u8 bssid[ETH_ALEN],
				 u16 reason)
1835
{
1836 1837
	struct cmd_ds_802_11_deauthenticate cmd;
	int ret;
1838 1839 1840

	lbs_deb_enter(LBS_DEB_JOIN);

1841 1842 1843 1844
	memset(&cmd, 0, sizeof(cmd));
	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
	memcpy(cmd.macaddr, &bssid[0], ETH_ALEN);
	cmd.reasoncode = cpu_to_le16(reason);
1845

1846
	ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
1847

1848 1849 1850 1851
	/* Clean up everything even if there was an error; can't assume that
	 * we're still authenticated to the AP after trying to deauth.
	 */
	lbs_mac_event_disconnected(priv);
1852 1853

	lbs_deb_leave(LBS_DEB_JOIN);
1854
	return ret;
1855 1856
}