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

#include <linux/bitops.h>
#include <net/ieee80211.h>
5
#include <linux/etherdevice.h>
6 7 8 9 10 11

#include "assoc.h"
#include "join.h"
#include "decl.h"
#include "hostcmd.h"
#include "host.h"
12
#include "cmd.h"
13 14 15 16 17


static const u8 bssid_any[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
static const u8 bssid_off[ETH_ALEN] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

18

19
static int assoc_helper_essid(struct lbs_private *priv,
20 21 22
                              struct assoc_request * assoc_req)
{
	int ret = 0;
23
	struct bss_descriptor * bss;
24
	int channel = -1;
25

26
	lbs_deb_enter(LBS_DEB_ASSOC);
27

28 29 30 31
	/* FIXME: take channel into account when picking SSIDs if a channel
	 * is set.
	 */

32 33 34
	if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
		channel = assoc_req->channel;

35
	lbs_deb_assoc("SSID '%s' requested\n",
36
	              escape_essid(assoc_req->ssid, assoc_req->ssid_len));
37
	if (assoc_req->mode == IW_MODE_INFRA) {
38
		lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
39
			assoc_req->ssid_len, 0);
40

41
		bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
42
				assoc_req->ssid_len, NULL, IW_MODE_INFRA, channel);
43
		if (bss != NULL) {
44
			memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
45
			ret = lbs_associate(priv, assoc_req);
46
		} else {
47
			lbs_deb_assoc("SSID not found; cannot associate\n");
48
		}
49
	} else if (assoc_req->mode == IW_MODE_ADHOC) {
50 51 52
		/* Scan for the network, do not save previous results.  Stale
		 *   scan data will cause us to join a non-existant adhoc network
		 */
53
		lbs_send_specific_ssid_scan(priv, assoc_req->ssid,
54
			assoc_req->ssid_len, 1);
55 56

		/* Search for the requested SSID in the scan table */
57
		bss = lbs_find_ssid_in_list(priv, assoc_req->ssid,
58
				assoc_req->ssid_len, NULL, IW_MODE_ADHOC, channel);
59
		if (bss != NULL) {
60
			lbs_deb_assoc("SSID found, will join\n");
61
			memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
62
			lbs_join_adhoc_network(priv, assoc_req);
63 64
		} else {
			/* else send START command */
65
			lbs_deb_assoc("SSID not found, creating adhoc network\n");
66
			memcpy(&assoc_req->bss.ssid, &assoc_req->ssid,
67 68
				IW_ESSID_MAX_SIZE);
			assoc_req->bss.ssid_len = assoc_req->ssid_len;
69
			lbs_start_adhoc_network(priv, assoc_req);
70 71 72
		}
	}

73
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
74 75 76 77
	return ret;
}


78
static int assoc_helper_bssid(struct lbs_private *priv,
79 80
                              struct assoc_request * assoc_req)
{
81 82
	int ret = 0;
	struct bss_descriptor * bss;
83
	DECLARE_MAC_BUF(mac);
84

85 86
	lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID %s",
		print_mac(mac, assoc_req->bssid));
87 88

	/* Search for index position in list for requested MAC */
89
	bss = lbs_find_bssid_in_list(priv, assoc_req->bssid,
90
			    assoc_req->mode);
91
	if (bss == NULL) {
92 93
		lbs_deb_assoc("ASSOC: WAP: BSSID %s not found, "
			"cannot associate.\n", print_mac(mac, assoc_req->bssid));
94 95 96
		goto out;
	}

97
	memcpy(&assoc_req->bss, bss, sizeof(struct bss_descriptor));
98
	if (assoc_req->mode == IW_MODE_INFRA) {
99 100
		ret = lbs_associate(priv, assoc_req);
		lbs_deb_assoc("ASSOC: lbs_associate(bssid) returned %d\n", ret);
101
	} else if (assoc_req->mode == IW_MODE_ADHOC) {
102
		lbs_join_adhoc_network(priv, assoc_req);
103 104 105
	}

out:
106
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
107 108 109 110
	return ret;
}


111
static int assoc_helper_associate(struct lbs_private *priv,
112 113 114 115
                                  struct assoc_request * assoc_req)
{
	int ret = 0, done = 0;

116 117
	lbs_deb_enter(LBS_DEB_ASSOC);

118 119 120
	/* If we're given and 'any' BSSID, try associating based on SSID */

	if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
121 122
		if (compare_ether_addr(bssid_any, assoc_req->bssid)
		    && compare_ether_addr(bssid_off, assoc_req->bssid)) {
123 124 125 126 127 128 129 130 131
			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);
	}

132
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
133 134 135 136
	return ret;
}


137
static int assoc_helper_mode(struct lbs_private *priv,
138 139 140 141
                             struct assoc_request * assoc_req)
{
	int ret = 0;

142
	lbs_deb_enter(LBS_DEB_ASSOC);
143

144
	if (assoc_req->mode == priv->mode)
145
		goto done;
146

147
	if (assoc_req->mode == IW_MODE_INFRA) {
148
		if (priv->psstate != PS_STATE_FULL_POWER)
149
			lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
150
		priv->psmode = LBS802_11POWERMODECAM;
151 152
	}

153
	priv->mode = assoc_req->mode;
154
	ret = lbs_prepare_and_send_command(priv,
155 156
				    CMD_802_11_SNMP_MIB,
				    0, CMD_OPTION_WAITFORRSP,
157
				    OID_802_11_INFRASTRUCTURE_MODE,
158
		/* Shoot me now */  (void *) (size_t) assoc_req->mode);
159

160 161
done:
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
162 163 164 165
	return ret;
}


166
static int update_channel(struct lbs_private *priv)
167
{
168
	int ret;
169

170
	/* the channel in f/w could be out of sync, get the current channel */
171
	lbs_deb_enter(LBS_DEB_ASSOC);
172 173 174 175 176

	ret = lbs_get_channel(priv);
	if (ret > 0)
		priv->curbssparams.channel = (u8) ret;

177 178
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
	return ret;
179 180
}

181
void lbs_sync_channel(struct work_struct *work)
182
{
183 184
	struct lbs_private *priv = container_of(work, struct lbs_private,
		sync_channel);
185

186
	lbs_deb_enter(LBS_DEB_ASSOC);
187 188
	if (update_channel(priv) != 0)
		lbs_pr_info("Channel synchronization failed.");
189
	lbs_deb_leave(LBS_DEB_ASSOC);
190 191
}

192
static int assoc_helper_channel(struct lbs_private *priv,
193 194 195 196 197 198 199 200 201 202 203
                                struct assoc_request * assoc_req)
{
	int ret = 0;

	lbs_deb_enter(LBS_DEB_ASSOC);

	ret = update_channel(priv);
	if (ret < 0) {
		lbs_deb_assoc("ASSOC: channel: error getting channel.");
	}

204
	if (assoc_req->channel == priv->curbssparams.channel)
205 206 207
		goto done;

	lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
208
	       priv->curbssparams.channel, assoc_req->channel);
209

210 211
	ret = lbs_set_channel(priv, assoc_req->channel);
	if (ret < 0)
212 213
		lbs_deb_assoc("ASSOC: channel: error setting channel.");

214 215 216
	/* FIXME: shouldn't need to grab the channel _again_ after setting
	 * it since the firmware is supposed to return the new channel, but
	 * whatever... */
217
	ret = update_channel(priv);
218
	if (ret < 0)
219 220
		lbs_deb_assoc("ASSOC: channel: error getting channel.");

221
	if (assoc_req->channel != priv->curbssparams.channel) {
222
		lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
		              assoc_req->channel);
		goto done;
	}

	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 */
	set_bit(ASSOC_FLAG_SSID, &assoc_req->flags);

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


245
static int assoc_helper_wep_keys(struct lbs_private *priv,
246 247 248 249 250
                                 struct assoc_request * assoc_req)
{
	int i;
	int ret = 0;

251
	lbs_deb_enter(LBS_DEB_ASSOC);
252 253 254 255 256 257

	/* Set or remove WEP keys */
	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) {
258
		ret = lbs_prepare_and_send_command(priv,
259 260 261
					    CMD_802_11_SET_WEP,
					    CMD_ACT_ADD,
					    CMD_OPTION_WAITFORRSP,
262 263
					    0, assoc_req);
	} else {
264
		ret = lbs_prepare_and_send_command(priv,
265 266 267
					    CMD_802_11_SET_WEP,
					    CMD_ACT_REMOVE,
					    CMD_OPTION_WAITFORRSP,
268 269 270 271 272 273 274
					    0, NULL);
	}

	if (ret)
		goto out;

	/* enable/disable the MAC's WEP packet filter */
275
	if (assoc_req->secinfo.wep_enabled)
276
		priv->currentpacketfilter |= CMD_ACT_MAC_WEP_ENABLE;
277
	else
278
		priv->currentpacketfilter &= ~CMD_ACT_MAC_WEP_ENABLE;
279
	ret = lbs_set_mac_packet_filter(priv);
280 281 282
	if (ret)
		goto out;

283
	mutex_lock(&priv->lock);
284

285
	/* Copy WEP keys into priv wep key fields */
286
	for (i = 0; i < 4; i++) {
287
		memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
288
			sizeof(struct enc_key));
289
	}
290
	priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
291

292
	mutex_unlock(&priv->lock);
293 294

out:
295
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
296 297 298
	return ret;
}

299
static int assoc_helper_secinfo(struct lbs_private *priv,
300 301 302
                                struct assoc_request * assoc_req)
{
	int ret = 0;
303 304
	u32 do_wpa;
	u32 rsn = 0;
305

306
	lbs_deb_enter(LBS_DEB_ASSOC);
307

308
	memcpy(&priv->secinfo, &assoc_req->secinfo,
309
		sizeof(struct lbs_802_11_security));
310

311
	ret = lbs_set_mac_packet_filter(priv);
312 313
	if (ret)
		goto out;
314

315 316 317 318 319 320
	/* 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 */
321
	ret = lbs_prepare_and_send_command(priv,
322 323 324
				    CMD_802_11_ENABLE_RSN,
				    CMD_ACT_GET,
				    CMD_OPTION_WAITFORRSP,
325 326 327 328 329 330 331 332 333 334 335 336 337
				    0, &rsn);
	if (ret) {
		lbs_deb_assoc("Failed to get RSN status: %d", ret);
		goto out;
	}

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

	/* Set RSN enabled/disabled */
	rsn = do_wpa;
338
	ret = lbs_prepare_and_send_command(priv,
339 340 341
				    CMD_802_11_ENABLE_RSN,
				    CMD_ACT_SET,
				    CMD_OPTION_WAITFORRSP,
342
				    0, &rsn);
343 344

out:
345
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
346 347 348 349
	return ret;
}


350
static int assoc_helper_wpa_keys(struct lbs_private *priv,
351 352 353
                                 struct assoc_request * assoc_req)
{
	int ret = 0;
354
	unsigned int flags = assoc_req->flags;
355

356
	lbs_deb_enter(LBS_DEB_ASSOC);
357

358 359 360 361
	/* 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.
	 */
362

363 364
	if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
		clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
365
		ret = lbs_prepare_and_send_command(priv,
366 367 368 369 370 371 372 373 374 375 376 377 378
					CMD_802_11_KEY_MATERIAL,
					CMD_ACT_SET,
					CMD_OPTION_WAITFORRSP,
					0, assoc_req);
		assoc_req->flags = flags;
	}

	if (ret)
		goto out;

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

379
		ret = lbs_prepare_and_send_command(priv,
380 381 382 383 384 385 386 387
					CMD_802_11_KEY_MATERIAL,
					CMD_ACT_SET,
					CMD_OPTION_WAITFORRSP,
					0, assoc_req);
		assoc_req->flags = flags;
	}

out:
388
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
389 390 391 392
	return ret;
}


393
static int assoc_helper_wpa_ie(struct lbs_private *priv,
394 395 396 397
                               struct assoc_request * assoc_req)
{
	int ret = 0;

398
	lbs_deb_enter(LBS_DEB_ASSOC);
399 400

	if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
401 402
		memcpy(&priv->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
		priv->wpa_ie_len = assoc_req->wpa_ie_len;
403
	} else {
404 405
		memset(&priv->wpa_ie, 0, MAX_WPA_IE_LEN);
		priv->wpa_ie_len = 0;
406 407
	}

408
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
409 410 411 412
	return ret;
}


413
static int should_deauth_infrastructure(struct lbs_private *priv,
414 415
                                        struct assoc_request * assoc_req)
{
416 417 418 419
	int ret = 0;

	lbs_deb_enter(LBS_DEB_ASSOC);

420
	if (priv->connect_status != LBS_CONNECTED)
421 422 423
		return 0;

	if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
424 425 426
		lbs_deb_assoc("Deauthenticating due to new SSID\n");
		ret = 1;
		goto out;
427 428 429
	}

	if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
430
		if (priv->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
431 432 433
			lbs_deb_assoc("Deauthenticating due to new security\n");
			ret = 1;
			goto out;
434 435 436 437
		}
	}

	if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
438 439 440
		lbs_deb_assoc("Deauthenticating due to new BSSID\n");
		ret = 1;
		goto out;
441 442
	}

443
	if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
444 445 446
		lbs_deb_assoc("Deauthenticating due to channel switch\n");
		ret = 1;
		goto out;
447 448
	}

449 450
	/* FIXME: deal with 'auto' mode somehow */
	if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
451 452 453 454 455 456
		if (assoc_req->mode != IW_MODE_INFRA) {
			lbs_deb_assoc("Deauthenticating due to leaving "
				"infra mode\n");
			ret = 1;
			goto out;
		}
457 458
	}

459 460
out:
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
461 462 463 464
	return 0;
}


465
static int should_stop_adhoc(struct lbs_private *priv,
466 467
                             struct assoc_request * assoc_req)
{
468 469
	lbs_deb_enter(LBS_DEB_ASSOC);

470
	if (priv->connect_status != LBS_CONNECTED)
471 472
		return 0;

473 474
	if (lbs_ssid_cmp(priv->curbssparams.ssid,
	                      priv->curbssparams.ssid_len,
475
	                      assoc_req->ssid, assoc_req->ssid_len) != 0)
476 477 478 479
		return 1;

	/* FIXME: deal with 'auto' mode somehow */
	if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
480
		if (assoc_req->mode != IW_MODE_ADHOC)
481 482 483
			return 1;
	}

484
	if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
485
		if (assoc_req->channel != priv->curbssparams.channel)
486 487 488
			return 1;
	}

489
	lbs_deb_leave(LBS_DEB_ASSOC);
490 491 492 493
	return 0;
}


494
void lbs_association_worker(struct work_struct *work)
495
{
496 497
	struct lbs_private *priv = container_of(work, struct lbs_private,
		assoc_work.work);
498 499 500
	struct assoc_request * assoc_req = NULL;
	int ret = 0;
	int find_any_ssid = 0;
501
	DECLARE_MAC_BUF(mac);
502

503
	lbs_deb_enter(LBS_DEB_ASSOC);
504

505 506 507 508 509
	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);
510

511 512
	if (!assoc_req)
		goto done;
513

514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
	lbs_deb_assoc(
		"Association Request:\n"
		"    flags:     0x%08lx\n"
		"    SSID:      '%s'\n"
		"    chann:     %d\n"
		"    band:      %d\n"
		"    mode:      %d\n"
		"    BSSID:     %s\n"
		"    secinfo:  %s%s%s\n"
		"    auth_mode: %d\n",
		assoc_req->flags,
		escape_essid(assoc_req->ssid, assoc_req->ssid_len),
		assoc_req->channel, assoc_req->band, assoc_req->mode,
		print_mac(mac, assoc_req->bssid),
		assoc_req->secinfo.WPAenabled ? " WPA" : "",
		assoc_req->secinfo.WPA2enabled ? " WPA2" : "",
		assoc_req->secinfo.wep_enabled ? " WEP" : "",
		assoc_req->secinfo.auth_mode);
532 533 534

	/* If 'any' SSID was specified, find an SSID to associate with */
	if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
535
	    && !assoc_req->ssid_len)
536 537 538 539
		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)) {
540 541
		if (compare_ether_addr(assoc_req->bssid, bssid_any)
		    && compare_ether_addr(assoc_req->bssid, bssid_off))
542 543 544 545
			find_any_ssid = 0;
	}

	if (find_any_ssid) {
546
		u8 new_mode;
547

548
		ret = lbs_find_best_network_ssid(priv, assoc_req->ssid,
549
				&assoc_req->ssid_len, assoc_req->mode, &new_mode);
550
		if (ret) {
551
			lbs_deb_assoc("Could not find best network\n");
552 553 554 555 556
			ret = -ENETUNREACH;
			goto out;
		}

		/* Ensure we switch to the mode of the AP */
557
		if (assoc_req->mode == IW_MODE_AUTO) {
558 559 560 561 562 563 564 565 566
			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.
	 */
567 568
	if (priv->mode == IW_MODE_INFRA) {
		if (should_deauth_infrastructure(priv, assoc_req)) {
569
			ret = lbs_send_deauthentication(priv);
570
			if (ret) {
571
				lbs_deb_assoc("Deauthentication due to new "
572 573 574 575
					"configuration request failed: %d\n",
					ret);
			}
		}
576 577
	} else if (priv->mode == IW_MODE_ADHOC) {
		if (should_stop_adhoc(priv, assoc_req)) {
578
			ret = lbs_stop_adhoc_network(priv);
579
			if (ret) {
580
				lbs_deb_assoc("Teardown of AdHoc network due to "
581 582 583 584 585 586 587 588 589 590
					"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);
591
		if (ret)
592 593 594
			goto out;
	}

595 596
	if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags)) {
		ret = assoc_helper_channel(priv, assoc_req);
597
		if (ret)
598 599 600
			goto out;
	}

601 602 603
	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);
604
		if (ret)
605 606 607 608 609
			goto out;
	}

	if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
		ret = assoc_helper_secinfo(priv, assoc_req);
610
		if (ret)
611 612 613 614 615
			goto out;
	}

	if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
		ret = assoc_helper_wpa_ie(priv, assoc_req);
616
		if (ret)
617 618 619 620 621 622
			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);
623
		if (ret)
624 625 626 627 628 629 630 631 632 633 634 635
			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) {
636
			lbs_deb_assoc("ASSOC: association unsuccessful: %d\n",
637 638 639 640
				ret);
			success = 0;
		}

641
		if (priv->connect_status != LBS_CONNECTED) {
642 643
			lbs_deb_assoc("ASSOC: association unsuccessful, "
				"not connected\n");
644 645 646 647
			success = 0;
		}

		if (success) {
648
			lbs_deb_assoc("ASSOC: associated to '%s', %s\n",
649 650 651
				escape_essid(priv->curbssparams.ssid,
				             priv->curbssparams.ssid_len),
				print_mac(mac, priv->curbssparams.bssid));
652
			lbs_prepare_and_send_command(priv,
653 654
				CMD_802_11_RSSI,
				0, CMD_OPTION_WAITFORRSP, 0, NULL);
655

656
			lbs_prepare_and_send_command(priv,
657 658
				CMD_802_11_GET_LOG,
				0, CMD_OPTION_WAITFORRSP, 0, NULL);
659 660 661 662 663 664 665
		} else {
			ret = -1;
		}
	}

out:
	if (ret) {
666
		lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
667 668
			ret);
	}
669

670 671 672
	mutex_lock(&priv->lock);
	priv->in_progress_assoc_req = NULL;
	mutex_unlock(&priv->lock);
673
	kfree(assoc_req);
674 675 676

done:
	lbs_deb_leave(LBS_DEB_ASSOC);
677 678 679 680 681 682
}


/*
 * Caller MUST hold any necessary locks
 */
683
struct assoc_request *lbs_get_association_request(struct lbs_private *priv)
684 685 686
{
	struct assoc_request * assoc_req;

687
	lbs_deb_enter(LBS_DEB_ASSOC);
688 689
	if (!priv->pending_assoc_req) {
		priv->pending_assoc_req = kzalloc(sizeof(struct assoc_request),
690
		                                     GFP_KERNEL);
691
		if (!priv->pending_assoc_req) {
692 693 694 695 696 697 698 699 700
			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.
	 */
701
	assoc_req = priv->pending_assoc_req;
702
	if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
703
		memcpy(&assoc_req->ssid, &priv->curbssparams.ssid,
704
		       IW_ESSID_MAX_SIZE);
705
		assoc_req->ssid_len = priv->curbssparams.ssid_len;
706 707 708
	}

	if (!test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
709
		assoc_req->channel = priv->curbssparams.channel;
710

711
	if (!test_bit(ASSOC_FLAG_BAND, &assoc_req->flags))
712
		assoc_req->band = priv->curbssparams.band;
713

714
	if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
715
		assoc_req->mode = priv->mode;
716 717

	if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
718
		memcpy(&assoc_req->bssid, priv->curbssparams.bssid,
719 720 721 722 723 724
			ETH_ALEN);
	}

	if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
		int i;
		for (i = 0; i < 4; i++) {
725
			memcpy(&assoc_req->wep_keys[i], &priv->wep_keys[i],
726
				sizeof(struct enc_key));
727 728 729 730
		}
	}

	if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
731
		assoc_req->wep_tx_keyidx = priv->wep_tx_keyidx;
732 733

	if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
734
		memcpy(&assoc_req->wpa_mcast_key, &priv->wpa_mcast_key,
735
			sizeof(struct enc_key));
736 737 738
	}

	if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
739
		memcpy(&assoc_req->wpa_unicast_key, &priv->wpa_unicast_key,
740
			sizeof(struct enc_key));
741 742 743
	}

	if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
744
		memcpy(&assoc_req->secinfo, &priv->secinfo,
745
			sizeof(struct lbs_802_11_security));
746 747 748
	}

	if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
749
		memcpy(&assoc_req->wpa_ie, &priv->wpa_ie,
750
			MAX_WPA_IE_LEN);
751
		assoc_req->wpa_ie_len = priv->wpa_ie_len;
752 753
	}

754
	lbs_deb_leave(LBS_DEB_ASSOC);
755 756
	return assoc_req;
}