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 12 13 14 15 16

#include "assoc.h"
#include "join.h"
#include "decl.h"
#include "hostcmd.h"
#include "host.h"


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 };

17

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

25
	lbs_deb_enter(LBS_DEB_ASSOC);
26

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

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

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

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

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

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


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

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

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

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

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


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

115 116
	lbs_deb_enter(LBS_DEB_ASSOC);

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

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

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


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

141
	lbs_deb_enter(LBS_DEB_ASSOC);
142

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

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

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

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


165
static int update_channel(struct lbs_private *priv)
166
{
167
	int ret;
168
	/* the channel in f/w could be out of sync, get the current channel */
169 170
	lbs_deb_enter(LBS_DEB_ASSOC);
	ret = lbs_prepare_and_send_command(priv, CMD_802_11_RF_CHANNEL,
171 172
				    CMD_OPT_802_11_RF_CHANNEL_GET,
				    CMD_OPTION_WAITFORRSP, 0, NULL);
173 174
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
	return ret;
175 176
}

177
void lbs_sync_channel(struct work_struct *work)
178
{
179 180
	struct lbs_private *priv = container_of(work, struct lbs_private,
		sync_channel);
181

182
	lbs_deb_enter(LBS_DEB_ASSOC);
183 184
	if (update_channel(priv) != 0)
		lbs_pr_info("Channel synchronization failed.");
185
	lbs_deb_leave(LBS_DEB_ASSOC);
186 187
}

188
static int assoc_helper_channel(struct lbs_private *priv,
189 190 191 192 193 194 195 196 197 198 199
                                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.");
	}

200
	if (assoc_req->channel == priv->curbssparams.channel)
201 202 203
		goto done;

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

206
	ret = lbs_prepare_and_send_command(priv, CMD_802_11_RF_CHANNEL,
207 208
				CMD_OPT_802_11_RF_CHANNEL_SET,
				CMD_OPTION_WAITFORRSP, 0, &assoc_req->channel);
209 210 211 212 213 214 215 216 217
	if (ret < 0) {
		lbs_deb_assoc("ASSOC: channel: error setting channel.");
	}

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

218
	if (assoc_req->channel != priv->curbssparams.channel) {
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
		lbs_deb_assoc("ASSOC: channel: failed to update channel to %d",
		              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;
}


242
static int assoc_helper_wep_keys(struct lbs_private *priv,
243 244 245 246 247
                                 struct assoc_request * assoc_req)
{
	int i;
	int ret = 0;

248
	lbs_deb_enter(LBS_DEB_ASSOC);
249 250 251 252 253 254

	/* 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) {
255
		ret = lbs_prepare_and_send_command(priv,
256 257 258
					    CMD_802_11_SET_WEP,
					    CMD_ACT_ADD,
					    CMD_OPTION_WAITFORRSP,
259 260
					    0, assoc_req);
	} else {
261
		ret = lbs_prepare_and_send_command(priv,
262 263 264
					    CMD_802_11_SET_WEP,
					    CMD_ACT_REMOVE,
					    CMD_OPTION_WAITFORRSP,
265 266 267 268 269 270 271
					    0, NULL);
	}

	if (ret)
		goto out;

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

280
	mutex_lock(&priv->lock);
281

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

289
	mutex_unlock(&priv->lock);
290 291

out:
292
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
293 294 295
	return ret;
}

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

303
	lbs_deb_enter(LBS_DEB_ASSOC);
304

305
	memcpy(&priv->secinfo, &assoc_req->secinfo,
306
		sizeof(struct lbs_802_11_security));
307

308
	ret = lbs_set_mac_packet_filter(priv);
309 310
	if (ret)
		goto out;
311

312 313 314 315 316 317
	/* 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 */
318
	ret = lbs_prepare_and_send_command(priv,
319 320 321
				    CMD_802_11_ENABLE_RSN,
				    CMD_ACT_GET,
				    CMD_OPTION_WAITFORRSP,
322 323 324 325 326 327 328 329 330 331 332 333 334
				    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;
335
	ret = lbs_prepare_and_send_command(priv,
336 337 338
				    CMD_802_11_ENABLE_RSN,
				    CMD_ACT_SET,
				    CMD_OPTION_WAITFORRSP,
339
				    0, &rsn);
340 341

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


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

353
	lbs_deb_enter(LBS_DEB_ASSOC);
354

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

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

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

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


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

395
	lbs_deb_enter(LBS_DEB_ASSOC);
396 397

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

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


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

	lbs_deb_enter(LBS_DEB_ASSOC);

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

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

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

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

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

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

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


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

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

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

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

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

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


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

500
	lbs_deb_enter(LBS_DEB_ASSOC);
501

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

508 509
	if (!assoc_req)
		goto done;
510

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

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

	if (find_any_ssid) {
543
		u8 new_mode;
544

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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