assoc.c 15.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Copyright (C) 2006, Red Hat, Inc. */

#include <linux/bitops.h>
#include <net/ieee80211.h>

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

static int assoc_helper_essid(wlan_private *priv,
                              struct assoc_request * assoc_req)
{
	wlan_adapter *adapter = priv->adapter;
	int ret = 0;
21
	struct bss_descriptor * bss;
22

23
	lbs_deb_enter(LBS_DEB_ASSOC);
24

25
	lbs_deb_assoc("New SSID requested: %s\n", assoc_req->ssid.ssid);
26
	if (assoc_req->mode == IW_MODE_INFRA) {
27
		if (adapter->prescan) {
28
			libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 0);
29 30
		}

31
		bss = libertas_find_SSID_in_list(adapter, &assoc_req->ssid,
32
				NULL, IW_MODE_INFRA);
33 34 35
		if (bss != NULL) {
			lbs_deb_assoc("SSID found in scan list, associating\n");
			ret = wlan_associate(priv, bss);
36
			if (ret == 0) {
37
				memcpy(&assoc_req->bssid, bss->bssid, ETH_ALEN);
38 39
			}
		} else {
40
			lbs_deb_assoc("SSID '%s' not found; cannot associate\n",
41 42
				assoc_req->ssid.ssid);
		}
43
	} else if (assoc_req->mode == IW_MODE_ADHOC) {
44 45 46
		/* Scan for the network, do not save previous results.  Stale
		 *   scan data will cause us to join a non-existant adhoc network
		 */
47
		libertas_send_specific_SSID_scan(priv, &assoc_req->ssid, 1);
48 49

		/* Search for the requested SSID in the scan table */
50
		bss = libertas_find_SSID_in_list(adapter, &assoc_req->ssid, NULL,
51
				IW_MODE_ADHOC);
52 53 54
		if (bss != NULL) {
			lbs_deb_assoc("SSID found joining\n");
			libertas_join_adhoc_network(priv, bss);
55 56
		} else {
			/* else send START command */
57
			lbs_deb_assoc("SSID not found in list, so creating adhoc"
58 59 60 61 62 63
				" with SSID '%s'\n", assoc_req->ssid.ssid);
			libertas_start_adhoc_network(priv, &assoc_req->ssid);
		}
		memcpy(&assoc_req->bssid, &adapter->current_addr, ETH_ALEN);
	}

64
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
65 66 67 68 69 70 71 72
	return ret;
}


static int assoc_helper_bssid(wlan_private *priv,
                              struct assoc_request * assoc_req)
{
	wlan_adapter *adapter = priv->adapter;
73 74
	int ret = 0;
	struct bss_descriptor * bss;
75

76
	lbs_deb_enter_args(LBS_DEB_ASSOC, "BSSID" MAC_FMT "\n",
77 78 79
		MAC_ARG(assoc_req->bssid));

	/* Search for index position in list for requested MAC */
80
	bss = libertas_find_BSSID_in_list(adapter, assoc_req->bssid,
81
			    assoc_req->mode);
82
	if (bss == NULL) {
83
		lbs_deb_assoc("ASSOC: WAP: BSSID " MAC_FMT " not found, "
84 85 86 87
			"cannot associate.\n", MAC_ARG(assoc_req->bssid));
		goto out;
	}

88
	if (assoc_req->mode == IW_MODE_INFRA) {
89 90
		ret = wlan_associate(priv, bss);
		lbs_deb_assoc("ASSOC: wlan_associate(bssid) returned %d\n", ret);
91
	} else if (assoc_req->mode == IW_MODE_ADHOC) {
92
		libertas_join_adhoc_network(priv, bss);
93
	}
94
	memcpy(&assoc_req->ssid, &bss->ssid, sizeof(struct WLAN_802_11_SSID));
95 96

out:
97
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	return ret;
}


static int assoc_helper_associate(wlan_private *priv,
                                  struct assoc_request * assoc_req)
{
	int ret = 0, done = 0;

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

	if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
		if (memcmp(bssid_any, assoc_req->bssid, ETH_ALEN)
		    && memcmp(bssid_off, assoc_req->bssid, ETH_ALEN)) {
			ret = assoc_helper_bssid(priv, assoc_req);
			done = 1;
			if (ret) {
115
				lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
116 117 118 119 120 121 122
			}
		}
	}

	if (!done && test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
		ret = assoc_helper_essid(priv, assoc_req);
		if (ret) {
123
			lbs_deb_assoc("ASSOC: bssid: ret = %d\n", ret);
124 125 126 127 128 129 130 131 132 133 134 135 136
		}
	}

	return ret;
}


static int assoc_helper_mode(wlan_private *priv,
                             struct assoc_request * assoc_req)
{
	wlan_adapter *adapter = priv->adapter;
	int ret = 0;

137
	lbs_deb_enter(LBS_DEB_ASSOC);
138

139 140
	if (assoc_req->mode == adapter->mode)
		goto done;
141

142
	if (assoc_req->mode == IW_MODE_INFRA) {
143 144 145 146 147
		if (adapter->psstate != PS_STATE_FULL_POWER)
			libertas_ps_wakeup(priv, cmd_option_waitforrsp);
		adapter->psmode = wlan802_11powermodecam;
	}

148
	adapter->mode = assoc_req->mode;
149 150 151 152
	ret = libertas_prepare_and_send_command(priv,
				    cmd_802_11_snmp_mib,
				    0, cmd_option_waitforrsp,
				    OID_802_11_INFRASTRUCTURE_MODE,
153
				    (void *) (size_t) assoc_req->mode);
154

155 156
done:
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
157 158 159 160 161 162 163 164 165 166 167
	return ret;
}


static int assoc_helper_wep_keys(wlan_private *priv,
                                 struct assoc_request * assoc_req)
{
	wlan_adapter *adapter = priv->adapter;
	int i;
	int ret = 0;

168
	lbs_deb_enter(LBS_DEB_ASSOC);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

	/* 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) {
		ret = libertas_prepare_and_send_command(priv,
					    cmd_802_11_set_wep,
					    cmd_act_add,
					    cmd_option_waitforrsp,
					    0, assoc_req);
	} else {
		ret = libertas_prepare_and_send_command(priv,
					    cmd_802_11_set_wep,
					    cmd_act_remove,
					    cmd_option_waitforrsp,
					    0, NULL);
	}

	if (ret)
		goto out;

	/* enable/disable the MAC's WEP packet filter */
192
	if (assoc_req->secinfo.wep_enabled)
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		adapter->currentpacketfilter |= cmd_act_mac_wep_enable;
	else
		adapter->currentpacketfilter &= ~cmd_act_mac_wep_enable;
	ret = libertas_set_mac_packet_filter(priv);
	if (ret)
		goto out;

	mutex_lock(&adapter->lock);

	/* Copy WEP keys into adapter wep key fields */
	for (i = 0; i < 4; i++) {
		memcpy(&adapter->wep_keys[i], &assoc_req->wep_keys[i],
			sizeof(struct WLAN_802_11_KEY));
	}
	adapter->wep_tx_keyidx = assoc_req->wep_tx_keyidx;

	mutex_unlock(&adapter->lock);

out:
212
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
213 214 215 216 217 218 219 220 221
	return ret;
}

static int assoc_helper_secinfo(wlan_private *priv,
                                struct assoc_request * assoc_req)
{
	wlan_adapter *adapter = priv->adapter;
	int ret = 0;

222
	lbs_deb_enter(LBS_DEB_ASSOC);
223 224 225 226 227 228

	memcpy(&adapter->secinfo, &assoc_req->secinfo,
		sizeof(struct wlan_802_11_security));

	ret = libertas_set_mac_packet_filter(priv);

229
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
230 231 232 233 234 235 236 237 238
	return ret;
}


static int assoc_helper_wpa_keys(wlan_private *priv,
                                 struct assoc_request * assoc_req)
{
	int ret = 0;

239
	lbs_deb_enter(LBS_DEB_ASSOC);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

	/* enable/Disable RSN */
	ret = libertas_prepare_and_send_command(priv,
				    cmd_802_11_enable_rsn,
				    cmd_act_set,
				    cmd_option_waitforrsp,
				    0, assoc_req);
	if (ret)
		goto out;

	ret = libertas_prepare_and_send_command(priv,
				    cmd_802_11_key_material,
				    cmd_act_set,
				    cmd_option_waitforrsp,
				    0, assoc_req);

out:
257
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
258 259 260 261 262 263 264 265 266 267
	return ret;
}


static int assoc_helper_wpa_ie(wlan_private *priv,
                               struct assoc_request * assoc_req)
{
	wlan_adapter *adapter = priv->adapter;
	int ret = 0;

268
	lbs_deb_enter(LBS_DEB_ASSOC);
269 270 271 272 273 274 275 276 277

	if (assoc_req->secinfo.WPAenabled || assoc_req->secinfo.WPA2enabled) {
		memcpy(&adapter->wpa_ie, &assoc_req->wpa_ie, assoc_req->wpa_ie_len);
		adapter->wpa_ie_len = assoc_req->wpa_ie_len;
	} else {
		memset(&adapter->wpa_ie, 0, MAX_WPA_IE_LEN);
		adapter->wpa_ie_len = 0;
	}

278
	lbs_deb_leave_args(LBS_DEB_ASSOC, "ret %d", ret);
279 280 281 282 283 284 285 286 287 288 289
	return ret;
}


static int should_deauth_infrastructure(wlan_adapter *adapter,
                                        struct assoc_request * assoc_req)
{
	if (adapter->connect_status != libertas_connected)
		return 0;

	if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
290
		lbs_deb_assoc("Deauthenticating due to new SSID in "
291 292 293 294 295
			" configuration request.\n");
		return 1;
	}

	if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
296
		if (adapter->secinfo.auth_mode != assoc_req->secinfo.auth_mode) {
297
			lbs_deb_assoc("Deauthenticating due to updated security "
298 299 300 301 302 303
				"info in configuration request.\n");
			return 1;
		}
	}

	if (test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
304
		lbs_deb_assoc("Deauthenticating due to new BSSID in "
305 306 307 308 309 310
			" configuration request.\n");
		return 1;
	}

	/* FIXME: deal with 'auto' mode somehow */
	if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
311
		if (assoc_req->mode != IW_MODE_INFRA)
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
			return 1;
	}

	return 0;
}


static int should_stop_adhoc(wlan_adapter *adapter,
                             struct assoc_request * assoc_req)
{
	if (adapter->connect_status != libertas_connected)
		return 0;

	if (adapter->curbssparams.ssid.ssidlength != assoc_req->ssid.ssidlength)
		return 1;
	if (memcmp(adapter->curbssparams.ssid.ssid, assoc_req->ssid.ssid,
328
			adapter->curbssparams.ssid.ssidlength))
329 330 331 332
		return 1;

	/* FIXME: deal with 'auto' mode somehow */
	if (test_bit(ASSOC_FLAG_MODE, &assoc_req->flags)) {
333
		if (assoc_req->mode != IW_MODE_ADHOC)
334 335 336 337 338 339 340
			return 1;
	}

	return 0;
}


341
void libertas_association_worker(struct work_struct *work)
342 343 344 345 346 347 348
{
	wlan_private *priv = container_of(work, wlan_private, assoc_work.work);
	wlan_adapter *adapter = priv->adapter;
	struct assoc_request * assoc_req = NULL;
	int ret = 0;
	int find_any_ssid = 0;

349
	lbs_deb_enter(LBS_DEB_ASSOC);
350 351 352 353 354 355

	mutex_lock(&adapter->lock);
	assoc_req = adapter->assoc_req;
	adapter->assoc_req = NULL;
	mutex_unlock(&adapter->lock);

356 357
	if (!assoc_req)
		goto done;
358

359
	lbs_deb_assoc("ASSOC: starting new association request: flags = 0x%lX\n",
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
		assoc_req->flags);

	/* If 'any' SSID was specified, find an SSID to associate with */
	if (test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)
	    && !assoc_req->ssid.ssidlength)
		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)) {
		if (memcmp(&assoc_req->bssid, bssid_any, ETH_ALEN)
		    && memcmp(&assoc_req->bssid, bssid_off, ETH_ALEN))
			find_any_ssid = 0;
	}

	if (find_any_ssid) {
375
		u8 new_mode;
376 377 378 379

		ret = libertas_find_best_network_SSID(priv, &assoc_req->ssid,
				assoc_req->mode, &new_mode);
		if (ret) {
380
			lbs_deb_assoc("Could not find best network\n");
381 382 383 384 385
			ret = -ENETUNREACH;
			goto out;
		}

		/* Ensure we switch to the mode of the AP */
386
		if (assoc_req->mode == IW_MODE_AUTO) {
387 388 389 390 391 392 393 394 395
			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.
	 */
396
	if (adapter->mode == IW_MODE_INFRA) {
397 398 399
		if (should_deauth_infrastructure(adapter, assoc_req)) {
			ret = libertas_send_deauthentication(priv);
			if (ret) {
400
				lbs_deb_assoc("Deauthentication due to new "
401 402 403 404
					"configuration request failed: %d\n",
					ret);
			}
		}
405
	} else if (adapter->mode == IW_MODE_ADHOC) {
406 407 408
		if (should_stop_adhoc(adapter, assoc_req)) {
			ret = libertas_stop_adhoc_network(priv);
			if (ret) {
409
				lbs_deb_assoc("Teardown of AdHoc network due to "
410 411 412 413 414 415 416 417 418 419 420
					"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);
		if (ret) {
421
lbs_deb_assoc("ASSOC(:%d) mode: ret = %d\n", __LINE__, ret);
422 423 424 425 426 427 428 429
			goto out;
		}
	}

	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);
		if (ret) {
430
lbs_deb_assoc("ASSOC(:%d) wep_keys: ret = %d\n", __LINE__, ret);
431 432 433 434 435 436 437
			goto out;
		}
	}

	if (test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
		ret = assoc_helper_secinfo(priv, assoc_req);
		if (ret) {
438
lbs_deb_assoc("ASSOC(:%d) secinfo: ret = %d\n", __LINE__, ret);
439 440 441 442 443 444 445
			goto out;
		}
	}

	if (test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
		ret = assoc_helper_wpa_ie(priv, assoc_req);
		if (ret) {
446
lbs_deb_assoc("ASSOC(:%d) wpa_ie: ret = %d\n", __LINE__, ret);
447 448 449 450 451 452 453 454
			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);
		if (ret) {
455
lbs_deb_assoc("ASSOC(:%d) wpa_keys: ret = %d\n", __LINE__, ret);
456 457 458 459 460 461 462 463 464 465 466 467 468
			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) {
469
			lbs_deb_assoc("ASSOC: association attempt unsuccessful: %d\n",
470 471 472 473 474
				ret);
			success = 0;
		}

		if (adapter->connect_status != libertas_connected) {
475
			lbs_deb_assoc("ASSOC: assoication attempt unsuccessful, "
476 477 478 479 480
				"not connected.\n");
			success = 0;
		}

		if (success) {
481
			lbs_deb_assoc("ASSOC: association attempt successful. "
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
				"Associated to '%s' (" MAC_FMT ")\n",
				assoc_req->ssid.ssid, MAC_ARG(assoc_req->bssid));
			libertas_prepare_and_send_command(priv,
				cmd_802_11_rssi,
				0, cmd_option_waitforrsp, 0, NULL);

			libertas_prepare_and_send_command(priv,
				cmd_802_11_get_log,
				0, cmd_option_waitforrsp, 0, NULL);
		} else {

			ret = -1;
		}
	}

out:
	if (ret) {
499
		lbs_deb_assoc("ASSOC: reconfiguration attempt unsuccessful: %d\n",
500 501 502
			ret);
	}
	kfree(assoc_req);
503 504 505

done:
	lbs_deb_leave(LBS_DEB_ASSOC);
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
}


/*
 * Caller MUST hold any necessary locks
 */
struct assoc_request * wlan_get_association_request(wlan_adapter *adapter)
{
	struct assoc_request * assoc_req;

	if (!adapter->assoc_req) {
		adapter->assoc_req = kzalloc(sizeof(struct assoc_request), GFP_KERNEL);
		if (!adapter->assoc_req) {
			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.
	 */
	assoc_req = adapter->assoc_req;
	if (!test_bit(ASSOC_FLAG_SSID, &assoc_req->flags)) {
		memcpy(&assoc_req->ssid, adapter->curbssparams.ssid.ssid,
			adapter->curbssparams.ssid.ssidlength);
	}

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

	if (!test_bit(ASSOC_FLAG_MODE, &assoc_req->flags))
538
		assoc_req->mode = adapter->mode;
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580

	if (!test_bit(ASSOC_FLAG_BSSID, &assoc_req->flags)) {
		memcpy(&assoc_req->bssid, adapter->curbssparams.bssid,
			ETH_ALEN);
	}

	if (!test_bit(ASSOC_FLAG_WEP_KEYS, &assoc_req->flags)) {
		int i;
		for (i = 0; i < 4; i++) {
			memcpy(&assoc_req->wep_keys[i], &adapter->wep_keys[i],
				sizeof(struct WLAN_802_11_KEY));
		}
	}

	if (!test_bit(ASSOC_FLAG_WEP_TX_KEYIDX, &assoc_req->flags))
		assoc_req->wep_tx_keyidx = adapter->wep_tx_keyidx;

	if (!test_bit(ASSOC_FLAG_WPA_MCAST_KEY, &assoc_req->flags)) {
		memcpy(&assoc_req->wpa_mcast_key, &adapter->wpa_mcast_key,
			sizeof(struct WLAN_802_11_KEY));
	}

	if (!test_bit(ASSOC_FLAG_WPA_UCAST_KEY, &assoc_req->flags)) {
		memcpy(&assoc_req->wpa_unicast_key, &adapter->wpa_unicast_key,
			sizeof(struct WLAN_802_11_KEY));
	}

	if (!test_bit(ASSOC_FLAG_SECINFO, &assoc_req->flags)) {
		memcpy(&assoc_req->secinfo, &adapter->secinfo,
			sizeof(struct wlan_802_11_security));
	}

	if (!test_bit(ASSOC_FLAG_WPA_IE, &assoc_req->flags)) {
		memcpy(&assoc_req->wpa_ie, &adapter->wpa_ie,
			MAX_WPA_IE_LEN);
		assoc_req->wpa_ie_len = adapter->wpa_ie_len;
	}

	return assoc_req;
}