sme.c 26.9 KB
Newer Older
S
Samuel Ortiz 已提交
1 2 3 4 5 6 7 8 9
/*
 * SME code for cfg80211's connect emulation.
 *
 * Copyright 2009	Johannes Berg <johannes@sipsolutions.net>
 * Copyright (C) 2009   Intel Corporation. All rights reserved.
 */

#include <linux/etherdevice.h>
#include <linux/if_arp.h>
10
#include <linux/slab.h>
S
Samuel Ortiz 已提交
11
#include <linux/workqueue.h>
12
#include <linux/wireless.h>
13
#include <linux/export.h>
14
#include <net/iw_handler.h>
S
Samuel Ortiz 已提交
15 16 17
#include <net/cfg80211.h>
#include <net/rtnetlink.h>
#include "nl80211.h"
18
#include "reg.h"
19
#include "rdev-ops.h"
S
Samuel Ortiz 已提交
20

21 22 23 24 25 26 27 28 29 30 31
struct cfg80211_conn {
	struct cfg80211_connect_params params;
	/* these are sub-states of the _CONNECTING sme_state */
	enum {
		CFG80211_CONN_IDLE,
		CFG80211_CONN_SCANNING,
		CFG80211_CONN_SCAN_AGAIN,
		CFG80211_CONN_AUTHENTICATE_NEXT,
		CFG80211_CONN_AUTHENTICATING,
		CFG80211_CONN_ASSOCIATE_NEXT,
		CFG80211_CONN_ASSOCIATING,
32
		CFG80211_CONN_DEAUTH_ASSOC_FAIL,
33
	} state;
34
	u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
35 36
	u8 *ie;
	size_t ie_len;
37
	bool auto_auth, prev_bssid_valid;
38 39
};

40
static bool cfg80211_is_all_idle(void)
41 42 43 44 45 46 47 48 49 50 51 52 53 54
{
	struct cfg80211_registered_device *rdev;
	struct wireless_dev *wdev;
	bool is_all_idle = true;

	mutex_lock(&cfg80211_mutex);

	/*
	 * All devices must be idle as otherwise if you are actively
	 * scanning some new beacon hints could be learned and would
	 * count as new regulatory hints.
	 */
	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
		cfg80211_lock_rdev(rdev);
55
		list_for_each_entry(wdev, &rdev->wdev_list, list) {
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
			wdev_lock(wdev);
			if (wdev->sme_state != CFG80211_SME_IDLE)
				is_all_idle = false;
			wdev_unlock(wdev);
		}
		cfg80211_unlock_rdev(rdev);
	}

	mutex_unlock(&cfg80211_mutex);

	return is_all_idle;
}

static void disconnect_work(struct work_struct *work)
{
	if (!cfg80211_is_all_idle())
		return;

	regulatory_hint_disconnect();
}

static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
78 79 80

static int cfg80211_conn_scan(struct wireless_dev *wdev)
{
81
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
82 83 84 85
	struct cfg80211_scan_request *request;
	int n_channels, err;

	ASSERT_RTNL();
86
	ASSERT_RDEV_LOCK(rdev);
J
Johannes Berg 已提交
87
	ASSERT_WDEV_LOCK(wdev);
88
	lockdep_assert_held(&rdev->sched_scan_mtx);
89

90
	if (rdev->scan_req)
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
		return -EBUSY;

	if (wdev->conn->params.channel) {
		n_channels = 1;
	} else {
		enum ieee80211_band band;
		n_channels = 0;

		for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
			if (!wdev->wiphy->bands[band])
				continue;
			n_channels += wdev->wiphy->bands[band]->n_channels;
		}
	}
	request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
			  sizeof(request->channels[0]) * n_channels,
			  GFP_KERNEL);
	if (!request)
		return -ENOMEM;

	if (wdev->conn->params.channel)
		request->channels[0] = wdev->conn->params.channel;
	else {
		int i = 0, j;
		enum ieee80211_band band;
116 117
		struct ieee80211_supported_band *bands;
		struct ieee80211_channel *channel;
118 119

		for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
120 121
			bands = wdev->wiphy->bands[band];
			if (!bands)
122
				continue;
123 124 125 126 127 128 129
			for (j = 0; j < bands->n_channels; j++) {
				channel = &bands->channels[j];
				if (channel->flags & IEEE80211_CHAN_DISABLED)
					continue;
				request->channels[i++] = channel;
			}
			request->rates[band] = (1 << bands->n_bitrates) - 1;
130
		}
131
		n_channels = i;
132 133
	}
	request->n_channels = n_channels;
134
	request->ssids = (void *)&request->channels[n_channels];
135 136 137 138 139 140
	request->n_ssids = 1;

	memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
		wdev->conn->params.ssid_len);
	request->ssids[0].ssid_len = wdev->conn->params.ssid_len;

J
Johannes Berg 已提交
141
	request->wdev = wdev;
142
	request->wiphy = &rdev->wiphy;
143
	request->scan_start = jiffies;
144

145
	rdev->scan_req = request;
146

147
	err = rdev_scan(rdev, request);
148 149
	if (!err) {
		wdev->conn->state = CFG80211_CONN_SCANNING;
J
Johannes Berg 已提交
150
		nl80211_send_scan_start(rdev, wdev);
151
		dev_hold(wdev->netdev);
152
	} else {
153
		rdev->scan_req = NULL;
154 155 156 157 158 159 160
		kfree(request);
	}
	return err;
}

static int cfg80211_conn_do_work(struct wireless_dev *wdev)
{
161
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
J
Johannes Berg 已提交
162
	struct cfg80211_connect_params *params;
163
	struct cfg80211_assoc_request req = {};
J
Johannes Berg 已提交
164
	int err;
165

J
Johannes Berg 已提交
166 167
	ASSERT_WDEV_LOCK(wdev);

168 169 170
	if (!wdev->conn)
		return 0;

J
Johannes Berg 已提交
171 172
	params = &wdev->conn->params;

173 174 175 176
	switch (wdev->conn->state) {
	case CFG80211_CONN_SCAN_AGAIN:
		return cfg80211_conn_scan(wdev);
	case CFG80211_CONN_AUTHENTICATE_NEXT:
177
		BUG_ON(!rdev->ops->auth);
J
Johannes Berg 已提交
178
		wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
179
		return __cfg80211_mlme_auth(rdev, wdev->netdev,
J
Johannes Berg 已提交
180 181 182
					    params->channel, params->auth_type,
					    params->bssid,
					    params->ssid, params->ssid_len,
J
Johannes Berg 已提交
183 184
					    NULL, 0,
					    params->key, params->key_len,
185
					    params->key_idx, NULL, 0);
186
	case CFG80211_CONN_ASSOCIATE_NEXT:
187
		BUG_ON(!rdev->ops->assoc);
J
Johannes Berg 已提交
188
		wdev->conn->state = CFG80211_CONN_ASSOCIATING;
189
		if (wdev->conn->prev_bssid_valid)
190 191 192 193 194 195 196 197 198 199 200 201 202 203
			req.prev_bssid = wdev->conn->prev_bssid;
		req.ie = params->ie;
		req.ie_len = params->ie_len;
		req.use_mfp = params->mfp != NL80211_MFP_NO;
		req.crypto = params->crypto;
		req.flags = params->flags;
		req.ht_capa = params->ht_capa;
		req.ht_capa_mask = params->ht_capa_mask;
		req.vht_capa = params->vht_capa;
		req.vht_capa_mask = params->vht_capa_mask;

		err = __cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
					    params->bssid, params->ssid,
					    params->ssid_len, &req);
J
Johannes Berg 已提交
204
		if (err)
205
			__cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
J
Johannes Berg 已提交
206
					       NULL, 0,
207 208
					       WLAN_REASON_DEAUTH_LEAVING,
					       false);
J
Johannes Berg 已提交
209
		return err;
210 211 212
	case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
		__cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
				       NULL, 0,
213
				       WLAN_REASON_DEAUTH_LEAVING, false);
214 215
		/* return an error so that we call __cfg80211_connect_result() */
		return -EINVAL;
216 217 218 219 220 221 222
	default:
		return 0;
	}
}

void cfg80211_conn_work(struct work_struct *work)
{
223
	struct cfg80211_registered_device *rdev =
224 225
		container_of(work, struct cfg80211_registered_device, conn_work);
	struct wireless_dev *wdev;
J
Johannes Berg 已提交
226
	u8 bssid_buf[ETH_ALEN], *bssid = NULL;
227 228

	rtnl_lock();
229 230
	cfg80211_lock_rdev(rdev);
	mutex_lock(&rdev->devlist_mtx);
231
	mutex_lock(&rdev->sched_scan_mtx);
232

233
	list_for_each_entry(wdev, &rdev->wdev_list, list) {
234 235 236
		if (!wdev->netdev)
			continue;

J
Johannes Berg 已提交
237 238 239
		wdev_lock(wdev);
		if (!netif_running(wdev->netdev)) {
			wdev_unlock(wdev);
240
			continue;
J
Johannes Berg 已提交
241
		}
242
		if (wdev->sme_state != CFG80211_SME_CONNECTING || !wdev->conn) {
J
Johannes Berg 已提交
243
			wdev_unlock(wdev);
244
			continue;
J
Johannes Berg 已提交
245
		}
J
Johannes Berg 已提交
246 247 248 249
		if (wdev->conn->params.bssid) {
			memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
			bssid = bssid_buf;
		}
250
		if (cfg80211_conn_do_work(wdev))
J
Johannes Berg 已提交
251
			__cfg80211_connect_result(
252
					wdev->netdev, bssid,
J
Johannes Berg 已提交
253 254
					NULL, 0, NULL, 0,
					WLAN_STATUS_UNSPECIFIED_FAILURE,
255
					false, NULL);
J
Johannes Berg 已提交
256
		wdev_unlock(wdev);
257 258
	}

259
	mutex_unlock(&rdev->sched_scan_mtx);
260 261
	mutex_unlock(&rdev->devlist_mtx);
	cfg80211_unlock_rdev(rdev);
262 263 264
	rtnl_unlock();
}

J
Johannes Berg 已提交
265
static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
266
{
267
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
268 269 270
	struct cfg80211_bss *bss;
	u16 capa = WLAN_CAPABILITY_ESS;

J
Johannes Berg 已提交
271 272
	ASSERT_WDEV_LOCK(wdev);

273 274 275
	if (wdev->conn->params.privacy)
		capa |= WLAN_CAPABILITY_PRIVACY;

276 277
	bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
			       wdev->conn->params.bssid,
278 279 280 281 282
			       wdev->conn->params.ssid,
			       wdev->conn->params.ssid_len,
			       WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
			       capa);
	if (!bss)
J
Johannes Berg 已提交
283
		return NULL;
284 285 286 287 288

	memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
	wdev->conn->params.bssid = wdev->conn->bssid;
	wdev->conn->params.channel = bss->channel;
	wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
289
	schedule_work(&rdev->conn_work);
290

J
Johannes Berg 已提交
291
	return bss;
292 293
}

J
Johannes Berg 已提交
294
static void __cfg80211_sme_scan_done(struct net_device *dev)
295 296
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
297
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
J
Johannes Berg 已提交
298
	struct cfg80211_bss *bss;
299

J
Johannes Berg 已提交
300 301
	ASSERT_WDEV_LOCK(wdev);

302 303 304
	if (wdev->sme_state != CFG80211_SME_CONNECTING)
		return;

305
	if (!wdev->conn)
306 307 308 309 310 311
		return;

	if (wdev->conn->state != CFG80211_CONN_SCANNING &&
	    wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
		return;

J
Johannes Berg 已提交
312 313
	bss = cfg80211_get_conn_bss(wdev);
	if (bss) {
314
		cfg80211_put_bss(&rdev->wiphy, bss);
J
Johannes Berg 已提交
315
	} else {
316 317
		/* not found */
		if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
318
			schedule_work(&rdev->conn_work);
319
		else
J
Johannes Berg 已提交
320 321 322 323 324
			__cfg80211_connect_result(
					wdev->netdev,
					wdev->conn->params.bssid,
					NULL, 0, NULL, 0,
					WLAN_STATUS_UNSPECIFIED_FAILURE,
325
					false, NULL);
326 327 328
	}
}

J
Johannes Berg 已提交
329 330 331 332 333 334 335 336 337 338 339
void cfg80211_sme_scan_done(struct net_device *dev)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

	wdev_lock(wdev);
	__cfg80211_sme_scan_done(dev);
	wdev_unlock(wdev);
}

void cfg80211_sme_rx_auth(struct net_device *dev,
			  const u8 *buf, size_t len)
340 341 342 343 344 345 346
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
	u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);

J
Johannes Berg 已提交
347 348
	ASSERT_WDEV_LOCK(wdev);

349 350 351 352 353 354 355 356 357 358 359 360 361
	/* should only RX auth frames when connecting */
	if (wdev->sme_state != CFG80211_SME_CONNECTING)
		return;

	if (WARN_ON(!wdev->conn))
		return;

	if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
	    wdev->conn->auto_auth &&
	    wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
		/* select automatically between only open, shared, leap */
		switch (wdev->conn->params.auth_type) {
		case NL80211_AUTHTYPE_OPEN_SYSTEM:
J
Johannes Berg 已提交
362 363 364 365 366 367
			if (wdev->connect_keys)
				wdev->conn->params.auth_type =
					NL80211_AUTHTYPE_SHARED_KEY;
			else
				wdev->conn->params.auth_type =
					NL80211_AUTHTYPE_NETWORK_EAP;
368 369 370 371 372 373 374 375 376 377 378 379 380
			break;
		case NL80211_AUTHTYPE_SHARED_KEY:
			wdev->conn->params.auth_type =
				NL80211_AUTHTYPE_NETWORK_EAP;
			break;
		default:
			/* huh? */
			wdev->conn->params.auth_type =
				NL80211_AUTHTYPE_OPEN_SYSTEM;
			break;
		}
		wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
		schedule_work(&rdev->conn_work);
J
Johannes Berg 已提交
381
	} else if (status_code != WLAN_STATUS_SUCCESS) {
J
Johannes Berg 已提交
382
		__cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
383
					  status_code, false, NULL);
J
Johannes Berg 已提交
384
	} else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
385 386 387 388 389
		 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
		schedule_work(&rdev->conn_work);
	}
}
S
Samuel Ortiz 已提交
390

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
{
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

	if (WARN_ON(!wdev->conn))
		return false;

	if (!wdev->conn->prev_bssid_valid)
		return false;

	/*
	 * Some stupid APs don't accept reassoc, so we
	 * need to fall back to trying regular assoc.
	 */
	wdev->conn->prev_bssid_valid = false;
	wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
	schedule_work(&rdev->conn_work);

	return true;
}

413 414 415 416 417 418 419 420 421
void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
{
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

	wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
	schedule_work(&rdev->conn_work);
}

J
Johannes Berg 已提交
422 423 424
void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
			       const u8 *req_ie, size_t req_ie_len,
			       const u8 *resp_ie, size_t resp_ie_len,
425 426
			       u16 status, bool wextev,
			       struct cfg80211_bss *bss)
S
Samuel Ortiz 已提交
427 428
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
429
	const u8 *country_ie;
J
Johannes Berg 已提交
430
#ifdef CONFIG_CFG80211_WEXT
S
Samuel Ortiz 已提交
431 432 433
	union iwreq_data wrqu;
#endif

J
Johannes Berg 已提交
434 435
	ASSERT_WDEV_LOCK(wdev);

436 437
	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
S
Samuel Ortiz 已提交
438 439
		return;

440
	if (wdev->sme_state != CFG80211_SME_CONNECTING)
441 442 443
		return;

	nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
444
				    bssid, req_ie, req_ie_len,
445 446
				    resp_ie, resp_ie_len,
				    status, GFP_KERNEL);
447

J
Johannes Berg 已提交
448
#ifdef CONFIG_CFG80211_WEXT
449 450 451 452
	if (wextev) {
		if (req_ie && status == WLAN_STATUS_SUCCESS) {
			memset(&wrqu, 0, sizeof(wrqu));
			wrqu.data.length = req_ie_len;
Z
Zhu Yi 已提交
453
			wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
454 455 456 457 458 459 460 461 462 463
		}

		if (resp_ie && status == WLAN_STATUS_SUCCESS) {
			memset(&wrqu, 0, sizeof(wrqu));
			wrqu.data.length = resp_ie_len;
			wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
		}

		memset(&wrqu, 0, sizeof(wrqu));
		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
464
		if (bssid && status == WLAN_STATUS_SUCCESS) {
465
			memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
466 467 468
			memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
			wdev->wext.prev_bssid_valid = true;
		}
469 470 471 472
		wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
	}
#endif

473 474
	if (wdev->current_bss) {
		cfg80211_unhold_bss(wdev->current_bss);
475
		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
476 477 478
		wdev->current_bss = NULL;
	}

J
Johannes Berg 已提交
479 480 481
	if (wdev->conn)
		wdev->conn->state = CFG80211_CONN_IDLE;

J
Johannes Berg 已提交
482
	if (status != WLAN_STATUS_SUCCESS) {
S
Samuel Ortiz 已提交
483
		wdev->sme_state = CFG80211_SME_IDLE;
484 485
		if (wdev->conn)
			kfree(wdev->conn->ie);
J
Johannes Berg 已提交
486 487
		kfree(wdev->conn);
		wdev->conn = NULL;
J
Johannes Berg 已提交
488 489
		kfree(wdev->connect_keys);
		wdev->connect_keys = NULL;
490
		wdev->ssid_len = 0;
491
		cfg80211_put_bss(wdev->wiphy, bss);
J
Johannes Berg 已提交
492
		return;
S
Samuel Ortiz 已提交
493
	}
J
Johannes Berg 已提交
494

495
	if (!bss)
496 497 498 499
		bss = cfg80211_get_bss(wdev->wiphy,
				       wdev->conn ? wdev->conn->params.channel :
				       NULL,
				       bssid,
500 501 502
				       wdev->ssid, wdev->ssid_len,
				       WLAN_CAPABILITY_ESS,
				       WLAN_CAPABILITY_ESS);
J
Johannes Berg 已提交
503 504 505 506 507 508 509 510 511

	if (WARN_ON(!bss))
		return;

	cfg80211_hold_bss(bss_from_pub(bss));
	wdev->current_bss = bss_from_pub(bss);

	wdev->sme_state = CFG80211_SME_CONNECTED;
	cfg80211_upload_connect_keys(wdev);
512

513 514 515 516 517 518 519 520 521
	rcu_read_lock();
	country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
	if (!country_ie) {
		rcu_read_unlock();
		return;
	}

	country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
	rcu_read_unlock();
522 523 524 525 526 527 528 529 530

	if (!country_ie)
		return;

	/*
	 * ieee80211_bss_get_ie() ensures we can access:
	 * - country_ie + 2, the start of the country ie data, and
	 * - and country_ie[1] which is the IE length
	 */
J
Johannes Berg 已提交
531 532
	regulatory_hint_11d(wdev->wiphy, bss->channel->band,
			    country_ie + 2, country_ie[1]);
533
	kfree(country_ie);
S
Samuel Ortiz 已提交
534
}
535 536 537 538 539 540

void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
			     const u8 *req_ie, size_t req_ie_len,
			     const u8 *resp_ie, size_t resp_ie_len,
			     u16 status, gfp_t gfp)
{
J
Johannes Berg 已提交
541 542 543 544 545
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	struct cfg80211_event *ev;
	unsigned long flags;

546 547
	CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);

J
Johannes Berg 已提交
548 549 550 551 552
	ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
	if (!ev)
		return;

	ev->type = EVENT_CONNECT_RESULT;
553 554
	if (bssid)
		memcpy(ev->cr.bssid, bssid, ETH_ALEN);
555 556 557 558 559 560 561 562 563 564
	if (req_ie_len) {
		ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
		ev->cr.req_ie_len = req_ie_len;
		memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
	}
	if (resp_ie_len) {
		ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
		ev->cr.resp_ie_len = resp_ie_len;
		memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
	}
J
Johannes Berg 已提交
565 566 567 568 569
	ev->cr.status = status;

	spin_lock_irqsave(&wdev->event_lock, flags);
	list_add_tail(&ev->list, &wdev->event_list);
	spin_unlock_irqrestore(&wdev->event_lock, flags);
570
	queue_work(cfg80211_wq, &rdev->event_work);
571
}
S
Samuel Ortiz 已提交
572 573
EXPORT_SYMBOL(cfg80211_connect_result);

574
void __cfg80211_roamed(struct wireless_dev *wdev,
575
		       struct cfg80211_bss *bss,
J
Johannes Berg 已提交
576 577
		       const u8 *req_ie, size_t req_ie_len,
		       const u8 *resp_ie, size_t resp_ie_len)
S
Samuel Ortiz 已提交
578
{
J
Johannes Berg 已提交
579
#ifdef CONFIG_CFG80211_WEXT
S
Samuel Ortiz 已提交
580 581
	union iwreq_data wrqu;
#endif
J
Johannes Berg 已提交
582 583
	ASSERT_WDEV_LOCK(wdev);

584 585
	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
586
		goto out;
S
Samuel Ortiz 已提交
587

588
	if (wdev->sme_state != CFG80211_SME_CONNECTED)
589
		goto out;
S
Samuel Ortiz 已提交
590 591 592

	/* internal error -- how did we get to CONNECTED w/o BSS? */
	if (WARN_ON(!wdev->current_bss)) {
593
		goto out;
S
Samuel Ortiz 已提交
594 595 596
	}

	cfg80211_unhold_bss(wdev->current_bss);
597
	cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
S
Samuel Ortiz 已提交
598 599
	wdev->current_bss = NULL;

J
Johannes Berg 已提交
600 601
	cfg80211_hold_bss(bss_from_pub(bss));
	wdev->current_bss = bss_from_pub(bss);
S
Samuel Ortiz 已提交
602

603
	nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bss->bssid,
J
Johannes Berg 已提交
604 605
			    req_ie, req_ie_len, resp_ie, resp_ie_len,
			    GFP_KERNEL);
S
Samuel Ortiz 已提交
606

J
Johannes Berg 已提交
607
#ifdef CONFIG_CFG80211_WEXT
S
Samuel Ortiz 已提交
608 609 610
	if (req_ie) {
		memset(&wrqu, 0, sizeof(wrqu));
		wrqu.data.length = req_ie_len;
Z
Zhu Yi 已提交
611
		wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
J
Johannes Berg 已提交
612
				    &wrqu, req_ie);
S
Samuel Ortiz 已提交
613 614 615 616 617
	}

	if (resp_ie) {
		memset(&wrqu, 0, sizeof(wrqu));
		wrqu.data.length = resp_ie_len;
J
Johannes Berg 已提交
618 619
		wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
				    &wrqu, resp_ie);
S
Samuel Ortiz 已提交
620 621 622 623
	}

	memset(&wrqu, 0, sizeof(wrqu));
	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
624 625
	memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
	memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
626
	wdev->wext.prev_bssid_valid = true;
J
Johannes Berg 已提交
627
	wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
S
Samuel Ortiz 已提交
628
#endif
629 630 631

	return;
out:
632
	cfg80211_put_bss(wdev->wiphy, bss);
S
Samuel Ortiz 已提交
633
}
J
Johannes Berg 已提交
634

635 636 637
void cfg80211_roamed(struct net_device *dev,
		     struct ieee80211_channel *channel,
		     const u8 *bssid,
J
Johannes Berg 已提交
638 639
		     const u8 *req_ie, size_t req_ie_len,
		     const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_bss *bss;

	CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);

	bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
			       wdev->ssid_len, WLAN_CAPABILITY_ESS,
			       WLAN_CAPABILITY_ESS);
	if (WARN_ON(!bss))
		return;

	cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
			    resp_ie_len, gfp);
}
EXPORT_SYMBOL(cfg80211_roamed);

void cfg80211_roamed_bss(struct net_device *dev,
			 struct cfg80211_bss *bss, const u8 *req_ie,
			 size_t req_ie_len, const u8 *resp_ie,
			 size_t resp_ie_len, gfp_t gfp)
J
Johannes Berg 已提交
661 662 663 664 665 666
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	struct cfg80211_event *ev;
	unsigned long flags;

667 668
	CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);

669 670 671
	if (WARN_ON(!bss))
		return;

J
Johannes Berg 已提交
672
	ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
673
	if (!ev) {
674
		cfg80211_put_bss(wdev->wiphy, bss);
J
Johannes Berg 已提交
675
		return;
676
	}
J
Johannes Berg 已提交
677 678 679 680 681 682 683 684

	ev->type = EVENT_ROAMED;
	ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
	ev->rm.req_ie_len = req_ie_len;
	memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
	ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
	ev->rm.resp_ie_len = resp_ie_len;
	memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
685
	ev->rm.bss = bss;
J
Johannes Berg 已提交
686 687 688 689

	spin_lock_irqsave(&wdev->event_lock, flags);
	list_add_tail(&ev->list, &wdev->event_list);
	spin_unlock_irqrestore(&wdev->event_lock, flags);
690
	queue_work(cfg80211_wq, &rdev->event_work);
J
Johannes Berg 已提交
691
}
692
EXPORT_SYMBOL(cfg80211_roamed_bss);
S
Samuel Ortiz 已提交
693

J
Johannes Berg 已提交
694
void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
695
			     size_t ie_len, u16 reason, bool from_ap)
S
Samuel Ortiz 已提交
696 697
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
J
Johannes Berg 已提交
698 699
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	int i;
J
Johannes Berg 已提交
700
#ifdef CONFIG_CFG80211_WEXT
S
Samuel Ortiz 已提交
701 702 703
	union iwreq_data wrqu;
#endif

J
Johannes Berg 已提交
704 705
	ASSERT_WDEV_LOCK(wdev);

706 707
	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
S
Samuel Ortiz 已提交
708 709
		return;

710
	if (wdev->sme_state != CFG80211_SME_CONNECTED)
S
Samuel Ortiz 已提交
711 712 713 714
		return;

	if (wdev->current_bss) {
		cfg80211_unhold_bss(wdev->current_bss);
715
		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
S
Samuel Ortiz 已提交
716 717 718 719
	}

	wdev->current_bss = NULL;
	wdev->sme_state = CFG80211_SME_IDLE;
720
	wdev->ssid_len = 0;
S
Samuel Ortiz 已提交
721

722 723 724
	if (wdev->conn) {
		kfree(wdev->conn->ie);
		wdev->conn->ie = NULL;
J
Johannes Berg 已提交
725 726
		kfree(wdev->conn);
		wdev->conn = NULL;
727 728
	}

J
Johannes Berg 已提交
729 730 731 732 733 734 735 736
	nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);

	/*
	 * Delete all the keys ... pairwise keys can't really
	 * exist any more anyway, but default keys might.
	 */
	if (rdev->ops->del_key)
		for (i = 0; i < 6; i++)
737
			rdev_del_key(rdev, dev, i, false, NULL);
S
Samuel Ortiz 已提交
738

J
Johannes Berg 已提交
739
#ifdef CONFIG_CFG80211_WEXT
S
Samuel Ortiz 已提交
740 741 742
	memset(&wrqu, 0, sizeof(wrqu));
	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
	wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
743
	wdev->wext.connect.ssid_len = 0;
S
Samuel Ortiz 已提交
744
#endif
745 746

	schedule_work(&cfg80211_disconnect_work);
S
Samuel Ortiz 已提交
747 748 749 750 751
}

void cfg80211_disconnected(struct net_device *dev, u16 reason,
			   u8 *ie, size_t ie_len, gfp_t gfp)
{
J
Johannes Berg 已提交
752 753 754 755 756
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	struct cfg80211_event *ev;
	unsigned long flags;

757 758
	CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);

J
Johannes Berg 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771
	ev = kzalloc(sizeof(*ev) + ie_len, gfp);
	if (!ev)
		return;

	ev->type = EVENT_DISCONNECTED;
	ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
	ev->dc.ie_len = ie_len;
	memcpy((void *)ev->dc.ie, ie, ie_len);
	ev->dc.reason = reason;

	spin_lock_irqsave(&wdev->event_lock, flags);
	list_add_tail(&ev->list, &wdev->event_list);
	spin_unlock_irqrestore(&wdev->event_lock, flags);
772
	queue_work(cfg80211_wq, &rdev->event_work);
S
Samuel Ortiz 已提交
773 774 775
}
EXPORT_SYMBOL(cfg80211_disconnected);

J
Johannes Berg 已提交
776 777
int __cfg80211_connect(struct cfg80211_registered_device *rdev,
		       struct net_device *dev,
J
Johannes Berg 已提交
778
		       struct cfg80211_connect_params *connect,
779 780
		       struct cfg80211_cached_keys *connkeys,
		       const u8 *prev_bssid)
S
Samuel Ortiz 已提交
781 782
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
J
Johannes Berg 已提交
783
	struct cfg80211_bss *bss = NULL;
J
Johannes Berg 已提交
784 785 786
	int err;

	ASSERT_WDEV_LOCK(wdev);
S
Samuel Ortiz 已提交
787 788 789 790

	if (wdev->sme_state != CFG80211_SME_IDLE)
		return -EALREADY;

J
Johannes Berg 已提交
791 792 793 794 795
	if (WARN_ON(wdev->connect_keys)) {
		kfree(wdev->connect_keys);
		wdev->connect_keys = NULL;
	}

796 797 798
	cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
				  rdev->wiphy.ht_capa_mod_mask);

J
Johannes Berg 已提交
799 800
	if (connkeys && connkeys->def >= 0) {
		int idx;
S
Samuel Ortiz 已提交
801
		u32 cipher;
J
Johannes Berg 已提交
802 803

		idx = connkeys->def;
S
Samuel Ortiz 已提交
804
		cipher = connkeys->params[idx].cipher;
J
Johannes Berg 已提交
805
		/* If given a WEP key we may need it for shared key auth */
S
Samuel Ortiz 已提交
806 807
		if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
		    cipher == WLAN_CIPHER_SUITE_WEP104) {
J
Johannes Berg 已提交
808 809 810
			connect->key_idx = idx;
			connect->key = connkeys->params[idx].key;
			connect->key_len = connkeys->params[idx].key_len;
S
Samuel Ortiz 已提交
811 812 813 814 815 816 817 818 819 820 821 822

			/*
			 * If ciphers are not set (e.g. when going through
			 * iwconfig), we have to set them appropriately here.
			 */
			if (connect->crypto.cipher_group == 0)
				connect->crypto.cipher_group = cipher;

			if (connect->crypto.n_ciphers_pairwise == 0) {
				connect->crypto.n_ciphers_pairwise = 1;
				connect->crypto.ciphers_pairwise[0] = cipher;
			}
J
Johannes Berg 已提交
823 824 825
		}
	}

S
Samuel Ortiz 已提交
826
	if (!rdev->ops->connect) {
827 828 829
		if (!rdev->ops->auth || !rdev->ops->assoc)
			return -EOPNOTSUPP;

J
Johannes Berg 已提交
830 831 832 833 834 835
		if (WARN_ON(wdev->conn))
			return -EINPROGRESS;

		wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
		if (!wdev->conn)
			return -ENOMEM;
836 837 838 839 840 841 842 843 844 845 846 847 848 849

		/*
		 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
		 */
		memcpy(&wdev->conn->params, connect, sizeof(*connect));
		if (connect->bssid) {
			wdev->conn->params.bssid = wdev->conn->bssid;
			memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
		}

		if (connect->ie) {
			wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
						GFP_KERNEL);
			wdev->conn->params.ie = wdev->conn->ie;
J
Johannes Berg 已提交
850 851 852
			if (!wdev->conn->ie) {
				kfree(wdev->conn);
				wdev->conn = NULL;
853
				return -ENOMEM;
J
Johannes Berg 已提交
854
			}
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
		}

		if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
			wdev->conn->auto_auth = true;
			/* start with open system ... should mostly work */
			wdev->conn->params.auth_type =
				NL80211_AUTHTYPE_OPEN_SYSTEM;
		} else {
			wdev->conn->auto_auth = false;
		}

		memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
		wdev->ssid_len = connect->ssid_len;
		wdev->conn->params.ssid = wdev->ssid;
		wdev->conn->params.ssid_len = connect->ssid_len;

J
Johannes Berg 已提交
871 872
		/* see if we have the bss already */
		bss = cfg80211_get_conn_bss(wdev);
873 874

		wdev->sme_state = CFG80211_SME_CONNECTING;
J
Johannes Berg 已提交
875
		wdev->connect_keys = connkeys;
876

877 878 879 880 881
		if (prev_bssid) {
			memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
			wdev->conn->prev_bssid_valid = true;
		}

J
Johannes Berg 已提交
882 883
		/* we're good if we have a matching bss struct */
		if (bss) {
884 885
			wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
			err = cfg80211_conn_do_work(wdev);
886
			cfg80211_put_bss(wdev->wiphy, bss);
887 888 889 890 891 892 893 894 895 896 897 898 899
		} else {
			/* otherwise we'll need to scan for the AP first */
			err = cfg80211_conn_scan(wdev);
			/*
			 * If we can't scan right now, then we need to scan again
			 * after the current scan finished, since the parameters
			 * changed (unless we find a good AP anyway).
			 */
			if (err == -EBUSY) {
				err = 0;
				wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
			}
		}
J
Johannes Berg 已提交
900
		if (err) {
901
			kfree(wdev->conn->ie);
J
Johannes Berg 已提交
902 903
			kfree(wdev->conn);
			wdev->conn = NULL;
904
			wdev->sme_state = CFG80211_SME_IDLE;
J
Johannes Berg 已提交
905
			wdev->connect_keys = NULL;
906
			wdev->ssid_len = 0;
J
Johannes Berg 已提交
907
		}
908 909

		return err;
S
Samuel Ortiz 已提交
910 911
	} else {
		wdev->sme_state = CFG80211_SME_CONNECTING;
J
Johannes Berg 已提交
912
		wdev->connect_keys = connkeys;
913
		err = rdev_connect(rdev, dev, connect);
S
Samuel Ortiz 已提交
914
		if (err) {
J
Johannes Berg 已提交
915
			wdev->connect_keys = NULL;
S
Samuel Ortiz 已提交
916 917 918 919
			wdev->sme_state = CFG80211_SME_IDLE;
			return err;
		}

920 921
		memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
		wdev->ssid_len = connect->ssid_len;
S
Samuel Ortiz 已提交
922

923 924
		return 0;
	}
S
Samuel Ortiz 已提交
925 926
}

J
Johannes Berg 已提交
927 928
int cfg80211_connect(struct cfg80211_registered_device *rdev,
		     struct net_device *dev,
J
Johannes Berg 已提交
929 930
		     struct cfg80211_connect_params *connect,
		     struct cfg80211_cached_keys *connkeys)
J
Johannes Berg 已提交
931 932 933
{
	int err;

934
	mutex_lock(&rdev->devlist_mtx);
935 936
	/* might request scan - scan_mtx -> wdev_mtx dependency */
	mutex_lock(&rdev->sched_scan_mtx);
J
Johannes Berg 已提交
937
	wdev_lock(dev->ieee80211_ptr);
938
	err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
J
Johannes Berg 已提交
939
	wdev_unlock(dev->ieee80211_ptr);
940
	mutex_unlock(&rdev->sched_scan_mtx);
941
	mutex_unlock(&rdev->devlist_mtx);
J
Johannes Berg 已提交
942 943 944 945 946 947

	return err;
}

int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
			  struct net_device *dev, u16 reason, bool wextev)
S
Samuel Ortiz 已提交
948
{
949
	struct wireless_dev *wdev = dev->ieee80211_ptr;
S
Samuel Ortiz 已提交
950 951
	int err;

J
Johannes Berg 已提交
952 953
	ASSERT_WDEV_LOCK(wdev);

954 955 956
	if (wdev->sme_state == CFG80211_SME_IDLE)
		return -EINVAL;

J
Johannes Berg 已提交
957 958 959
	kfree(wdev->connect_keys);
	wdev->connect_keys = NULL;

S
Samuel Ortiz 已提交
960
	if (!rdev->ops->disconnect) {
J
Johannes Berg 已提交
961 962
		if (!rdev->ops->deauth)
			return -EOPNOTSUPP;
963

J
Johannes Berg 已提交
964 965 966
		/* was it connected by userspace SME? */
		if (!wdev->conn) {
			cfg80211_mlme_down(rdev, dev);
967
			goto disconnect;
J
Johannes Berg 已提交
968
		}
969 970 971 972 973

		if (wdev->sme_state == CFG80211_SME_CONNECTING &&
		    (wdev->conn->state == CFG80211_CONN_SCANNING ||
		     wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
			wdev->sme_state = CFG80211_SME_IDLE;
974
			kfree(wdev->conn->ie);
J
Johannes Berg 已提交
975 976
			kfree(wdev->conn);
			wdev->conn = NULL;
977
			wdev->ssid_len = 0;
978 979 980 981
			return 0;
		}

		/* wdev->conn->params.bssid must be set if > SCANNING */
J
Johannes Berg 已提交
982 983
		err = __cfg80211_mlme_deauth(rdev, dev,
					     wdev->conn->params.bssid,
984
					     NULL, 0, reason, false);
985 986
		if (err)
			return err;
S
Samuel Ortiz 已提交
987
	} else {
988
		err = rdev_disconnect(rdev, dev, reason);
S
Samuel Ortiz 已提交
989 990 991 992
		if (err)
			return err;
	}

993
 disconnect:
994
	if (wdev->sme_state == CFG80211_SME_CONNECTED)
J
Johannes Berg 已提交
995
		__cfg80211_disconnected(dev, NULL, 0, 0, false);
996
	else if (wdev->sme_state == CFG80211_SME_CONNECTING)
997 998
		__cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
					  WLAN_STATUS_UNSPECIFIED_FAILURE,
999
					  wextev, NULL);
S
Samuel Ortiz 已提交
1000 1001 1002

	return 0;
}
J
Johannes Berg 已提交
1003

J
Johannes Berg 已提交
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
			struct net_device *dev,
			u16 reason, bool wextev)
{
	int err;

	wdev_lock(dev->ieee80211_ptr);
	err = __cfg80211_disconnect(rdev, dev, reason, wextev);
	wdev_unlock(dev->ieee80211_ptr);

	return err;
}

1017 1018
void cfg80211_sme_disassoc(struct net_device *dev,
			   struct cfg80211_internal_bss *bss)
J
Johannes Berg 已提交
1019 1020 1021 1022 1023
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	u8 bssid[ETH_ALEN];

J
Johannes Berg 已提交
1024 1025
	ASSERT_WDEV_LOCK(wdev);

J
Johannes Berg 已提交
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
	if (!wdev->conn)
		return;

	if (wdev->conn->state == CFG80211_CONN_IDLE)
		return;

	/*
	 * Ok, so the association was made by this SME -- we don't
	 * want it any more so deauthenticate too.
	 */

1037
	memcpy(bssid, bss->pub.bssid, ETH_ALEN);
J
Johannes Berg 已提交
1038

1039 1040
	__cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
			       WLAN_REASON_DEAUTH_LEAVING, false);
J
Johannes Berg 已提交
1041
}