assoc.c 19.3 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 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
static int assoc_helper_essid(struct lbs_private *priv,
22 23 24
                              struct assoc_request * assoc_req)
{
	int ret = 0;
25
	struct bss_descriptor * bss;
26
	int channel = -1;
27

28
	lbs_deb_enter(LBS_DEB_ASSOC);
29

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

34 35 36
	if (test_bit(ASSOC_FLAG_CHANNEL, &assoc_req->flags))
		channel = assoc_req->channel;

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

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

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

75
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
76 77 78 79
	return ret;
}


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

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

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

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

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


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

118 119
	lbs_deb_enter(LBS_DEB_ASSOC);

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

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

134
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
135 136 137 138
	return ret;
}


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

144
	lbs_deb_enter(LBS_DEB_ASSOC);
145

146
	if (assoc_req->mode == priv->mode)
147
		goto done;
148

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

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

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


168
int lbs_update_channel(struct lbs_private *priv)
169
{
170
	int ret;
171

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

	ret = lbs_get_channel(priv);
176 177 178 179
	if (ret > 0) {
		priv->curbssparams.channel = ret;
		ret = 0;
	}
180 181
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
	return ret;
182 183
}

184
void lbs_sync_channel(struct work_struct *work)
185
{
186 187
	struct lbs_private *priv = container_of(work, struct lbs_private,
		sync_channel);
188

189
	lbs_deb_enter(LBS_DEB_ASSOC);
190
	if (lbs_update_channel(priv))
191
		lbs_pr_info("Channel synchronization failed.");
192
	lbs_deb_leave(LBS_DEB_ASSOC);
193 194
}

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

	lbs_deb_enter(LBS_DEB_ASSOC);

202
	ret = lbs_update_channel(priv);
203
	if (ret) {
204
		lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
205
		goto done;
206 207
	}

208
	if (assoc_req->channel == priv->curbssparams.channel)
209 210
		goto done;

211
	if (priv->mesh_dev) {
212 213 214 215
		/* Change mesh channel first; 21.p21 firmware won't let
		   you change channel otherwise (even though it'll return
		   an error to this */
		lbs_mesh_config(priv, 0, assoc_req->channel);
216 217
	}

218
	lbs_deb_assoc("ASSOC: channel: %d -> %d\n",
219
		      priv->curbssparams.channel, assoc_req->channel);
220

221 222
	ret = lbs_set_channel(priv, assoc_req->channel);
	if (ret < 0)
223
		lbs_deb_assoc("ASSOC: channel: error setting channel.\n");
224

225 226 227
	/* FIXME: shouldn't need to grab the channel _again_ after setting
	 * it since the firmware is supposed to return the new channel, but
	 * whatever... */
228
	ret = lbs_update_channel(priv);
229
	if (ret) {
230
		lbs_deb_assoc("ASSOC: channel: error getting channel.\n");
231 232
		goto done;
	}
233

234
	if (assoc_req->channel != priv->curbssparams.channel) {
235
		lbs_deb_assoc("ASSOC: channel: failed to update channel to %d\n",
236
		              assoc_req->channel);
237
		goto restore_mesh;
238 239 240 241 242 243 244 245 246 247 248 249
	}

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

252 253
 restore_mesh:
	if (priv->mesh_dev)
254
		lbs_mesh_config(priv, 1, priv->curbssparams.channel);
255 256

 done:
257 258 259 260 261
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
	return ret;
}


262
static int assoc_helper_wep_keys(struct lbs_private *priv,
263
				 struct assoc_request *assoc_req)
264 265 266 267
{
	int i;
	int ret = 0;

268
	lbs_deb_enter(LBS_DEB_ASSOC);
269 270

	/* Set or remove WEP keys */
271 272 273 274 275
	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);
276 277 278 279 280

	if (ret)
		goto out;

	/* enable/disable the MAC's WEP packet filter */
281
	if (assoc_req->secinfo.wep_enabled)
282
		priv->currentpacketfilter |= CMD_ACT_MAC_WEP_ENABLE;
283
	else
284
		priv->currentpacketfilter &= ~CMD_ACT_MAC_WEP_ENABLE;
285

286
	ret = lbs_set_mac_packet_filter(priv);
287 288 289
	if (ret)
		goto out;

290
	mutex_lock(&priv->lock);
291

292
	/* Copy WEP keys into priv wep key fields */
293
	for (i = 0; i < 4; i++) {
294
		memcpy(&priv->wep_keys[i], &assoc_req->wep_keys[i],
295
		       sizeof(struct enc_key));
296
	}
297
	priv->wep_tx_keyidx = assoc_req->wep_tx_keyidx;
298

299
	mutex_unlock(&priv->lock);
300 301

out:
302
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
303 304 305
	return ret;
}

306
static int assoc_helper_secinfo(struct lbs_private *priv,
307 308 309
                                struct assoc_request * assoc_req)
{
	int ret = 0;
310 311
	uint16_t do_wpa;
	uint16_t rsn = 0;
312

313
	lbs_deb_enter(LBS_DEB_ASSOC);
314

315
	memcpy(&priv->secinfo, &assoc_req->secinfo,
316
		sizeof(struct lbs_802_11_security));
317

318
	ret = lbs_set_mac_packet_filter(priv);
319 320
	if (ret)
		goto out;
321

322 323 324 325 326 327
	/* 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 */
328
	ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_GET, &rsn);
329
	if (ret) {
330
		lbs_deb_assoc("Failed to get RSN status: %d\n", ret);
331 332 333 334
		goto out;
	}

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

	/* Set RSN enabled/disabled */
340
	ret = lbs_cmd_802_11_enable_rsn(priv, CMD_ACT_SET, &do_wpa);
341 342

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


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

354
	lbs_deb_enter(LBS_DEB_ASSOC);
355

356 357 358 359
	/* 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.
	 */
360

361 362
	if (test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
		clear_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags);
363
		ret = lbs_prepare_and_send_command(priv,
364 365 366 367 368 369 370 371 372 373 374 375 376
					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);

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

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


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

396
	lbs_deb_enter(LBS_DEB_ASSOC);
397 398

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

406
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
407 408 409 410
	return ret;
}


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

	lbs_deb_enter(LBS_DEB_ASSOC);

418
	if (priv->connect_status != LBS_CONNECTED)
419 420 421
		return 0;

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

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

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

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

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

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


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

468
	if (priv->connect_status != LBS_CONNECTED)
469 470
		return 0;

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

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

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

487
	lbs_deb_leave(LBS_DEB_ASSOC);
488 489 490 491
	return 0;
}


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

501
	lbs_deb_enter(LBS_DEB_ASSOC);
502

503 504 505 506 507
	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);
508

509 510
	if (!assoc_req)
		goto done;
511

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
	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);
530 531 532

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

	if (find_any_ssid) {
544
		u8 new_mode;
545

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

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

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

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

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

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

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

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

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

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

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

done:
	lbs_deb_leave(LBS_DEB_ASSOC);
675 676 677 678 679 680
}


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

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

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

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

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

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

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

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

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

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

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

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

752
	lbs_deb_leave(LBS_DEB_ASSOC);
753 754
	return assoc_req;
}