mlme.c 27.3 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * cfg80211 MLME SAP interface
 *
 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
 */

#include <linux/kernel.h>
#include <linux/module.h>
9
#include <linux/etherdevice.h>
10 11
#include <linux/netdevice.h>
#include <linux/nl80211.h>
12
#include <linux/slab.h>
13
#include <linux/wireless.h>
14
#include <net/cfg80211.h>
15
#include <net/iw_handler.h>
16 17
#include "core.h"
#include "nl80211.h"
18 19
#include "rdev-ops.h"

20

21
void cfg80211_send_rx_auth(struct net_device *dev, const u8 *buf, size_t len)
22
{
J
Johannes Berg 已提交
23 24
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct wiphy *wiphy = wdev->wiphy;
25
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
J
Johannes Berg 已提交
26

J
Johannes Berg 已提交
27
	wdev_lock(wdev);
28

29 30
	nl80211_send_rx_auth(rdev, dev, buf, len, GFP_KERNEL);
	cfg80211_sme_rx_auth(dev, buf, len);
J
Johannes Berg 已提交
31 32

	wdev_unlock(wdev);
33 34 35
}
EXPORT_SYMBOL(cfg80211_send_rx_auth);

36 37
void cfg80211_send_rx_assoc(struct net_device *dev, struct cfg80211_bss *bss,
			    const u8 *buf, size_t len)
38
{
39 40 41
	u16 status_code;
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct wiphy *wiphy = wdev->wiphy;
42
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
43 44
	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
	u8 *ie = mgmt->u.assoc_resp.variable;
45
	int ieoffs = offsetof(struct ieee80211_mgmt, u.assoc_resp.variable);
46

J
Johannes Berg 已提交
47
	wdev_lock(wdev);
48

49 50
	status_code = le16_to_cpu(mgmt->u.assoc_resp.status_code);

51 52 53 54 55 56 57
	/*
	 * This is a bit of a hack, we don't notify userspace of
	 * a (re-)association reply if we tried to send a reassoc
	 * and got a reject -- we only try again with an assoc
	 * frame instead of reassoc.
	 */
	if (status_code != WLAN_STATUS_SUCCESS && wdev->conn &&
58 59
	    cfg80211_sme_failed_reassoc(wdev)) {
		cfg80211_put_bss(bss);
60
		goto out;
61
	}
62

63
	nl80211_send_rx_assoc(rdev, dev, buf, len, GFP_KERNEL);
64

65
	if (status_code != WLAN_STATUS_SUCCESS && wdev->conn) {
66 67 68 69 70
		cfg80211_sme_failed_assoc(wdev);
		/*
		 * do not call connect_result() now because the
		 * sme will schedule work that does it later.
		 */
71
		cfg80211_put_bss(bss);
72
		goto out;
73 74
	}

75 76 77 78 79 80 81 82 83
	if (!wdev->conn && wdev->sme_state == CFG80211_SME_IDLE) {
		/*
		 * This is for the userspace SME, the CONNECTING
		 * state will be changed to CONNECTED by
		 * __cfg80211_connect_result() below.
		 */
		wdev->sme_state = CFG80211_SME_CONNECTING;
	}

84
	/* this consumes the bss reference */
85 86
	__cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, ie, len - ieoffs,
				  status_code,
87
				  status_code == WLAN_STATUS_SUCCESS, bss);
88
 out:
J
Johannes Berg 已提交
89
	wdev_unlock(wdev);
90 91 92
}
EXPORT_SYMBOL(cfg80211_send_rx_assoc);

93
void __cfg80211_send_deauth(struct net_device *dev,
J
Johannes Berg 已提交
94
				   const u8 *buf, size_t len)
95
{
96 97
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct wiphy *wiphy = wdev->wiphy;
98
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
99
	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
J
Johannes Berg 已提交
100
	const u8 *bssid = mgmt->bssid;
101
	bool was_current = false;
102

J
Johannes Berg 已提交
103
	ASSERT_WDEV_LOCK(wdev);
104

J
Johannes Berg 已提交
105
	if (wdev->current_bss &&
106
	    ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
J
Johannes Berg 已提交
107 108 109
		cfg80211_unhold_bss(wdev->current_bss);
		cfg80211_put_bss(&wdev->current_bss->pub);
		wdev->current_bss = NULL;
110
		was_current = true;
J
Johannes Berg 已提交
111 112
	}

113 114
	nl80211_send_deauth(rdev, dev, buf, len, GFP_KERNEL);

115
	if (wdev->sme_state == CFG80211_SME_CONNECTED && was_current) {
116 117 118 119 120
		u16 reason_code;
		bool from_ap;

		reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);

121
		from_ap = !ether_addr_equal(mgmt->sa, dev->dev_addr);
J
Johannes Berg 已提交
122
		__cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
123
	} else if (wdev->sme_state == CFG80211_SME_CONNECTING) {
J
Johannes Berg 已提交
124 125
		__cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
					  WLAN_STATUS_UNSPECIFIED_FAILURE,
126
					  false, NULL);
J
Johannes Berg 已提交
127 128
	}
}
129
EXPORT_SYMBOL(__cfg80211_send_deauth);
J
Johannes Berg 已提交
130

131
void cfg80211_send_deauth(struct net_device *dev, const u8 *buf, size_t len)
J
Johannes Berg 已提交
132 133 134
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

135 136 137
	wdev_lock(wdev);
	__cfg80211_send_deauth(dev, buf, len);
	wdev_unlock(wdev);
138
}
139
EXPORT_SYMBOL(cfg80211_send_deauth);
140

141
void __cfg80211_send_disassoc(struct net_device *dev,
J
Johannes Berg 已提交
142
				     const u8 *buf, size_t len)
143
{
144 145
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct wiphy *wiphy = wdev->wiphy;
146
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
147
	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
J
Johannes Berg 已提交
148 149 150
	const u8 *bssid = mgmt->bssid;
	u16 reason_code;
	bool from_ap;
151

152
	ASSERT_WDEV_LOCK(wdev);
153 154

	nl80211_send_disassoc(rdev, dev, buf, len, GFP_KERNEL);
155

156 157
	if (wdev->sme_state != CFG80211_SME_CONNECTED)
		return;
158

J
Johannes Berg 已提交
159
	if (wdev->current_bss &&
160
	    ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
161 162 163 164
		cfg80211_sme_disassoc(dev, wdev->current_bss);
		cfg80211_unhold_bss(wdev->current_bss);
		cfg80211_put_bss(&wdev->current_bss->pub);
		wdev->current_bss = NULL;
J
Johannes Berg 已提交
165 166
	} else
		WARN_ON(1);
167 168


J
Johannes Berg 已提交
169 170
	reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);

171
	from_ap = !ether_addr_equal(mgmt->sa, dev->dev_addr);
J
Johannes Berg 已提交
172 173
	__cfg80211_disconnected(dev, NULL, 0, reason_code, from_ap);
}
174
EXPORT_SYMBOL(__cfg80211_send_disassoc);
J
Johannes Berg 已提交
175

176
void cfg80211_send_disassoc(struct net_device *dev, const u8 *buf, size_t len)
J
Johannes Berg 已提交
177 178 179
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

180 181 182
	wdev_lock(wdev);
	__cfg80211_send_disassoc(dev, buf, len);
	wdev_unlock(wdev);
183
}
184
EXPORT_SYMBOL(cfg80211_send_disassoc);
185

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
void cfg80211_send_unprot_deauth(struct net_device *dev, const u8 *buf,
				 size_t len)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

	nl80211_send_unprot_deauth(rdev, dev, buf, len, GFP_ATOMIC);
}
EXPORT_SYMBOL(cfg80211_send_unprot_deauth);

void cfg80211_send_unprot_disassoc(struct net_device *dev, const u8 *buf,
				   size_t len)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

	nl80211_send_unprot_disassoc(rdev, dev, buf, len, GFP_ATOMIC);
}
EXPORT_SYMBOL(cfg80211_send_unprot_disassoc);

208 209 210 211 212 213 214 215 216 217 218 219 220 221
void cfg80211_send_auth_timeout(struct net_device *dev, const u8 *addr)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

	wdev_lock(wdev);

	nl80211_send_auth_timeout(rdev, dev, addr, GFP_KERNEL);
	if (wdev->sme_state == CFG80211_SME_CONNECTING)
		__cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
					  WLAN_STATUS_UNSPECIFIED_FAILURE,
					  false, NULL);

J
Johannes Berg 已提交
222
	wdev_unlock(wdev);
223 224 225
}
EXPORT_SYMBOL(cfg80211_send_auth_timeout);

226
void cfg80211_send_assoc_timeout(struct net_device *dev, const u8 *addr)
227
{
228 229
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct wiphy *wiphy = wdev->wiphy;
230
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
J
Johannes Berg 已提交
231

J
Johannes Berg 已提交
232
	wdev_lock(wdev);
233 234

	nl80211_send_assoc_timeout(rdev, dev, addr, GFP_KERNEL);
235
	if (wdev->sme_state == CFG80211_SME_CONNECTING)
J
Johannes Berg 已提交
236 237
		__cfg80211_connect_result(dev, addr, NULL, 0, NULL, 0,
					  WLAN_STATUS_UNSPECIFIED_FAILURE,
238
					  false, NULL);
J
Johannes Berg 已提交
239

J
Johannes Berg 已提交
240
	wdev_unlock(wdev);
241 242 243
}
EXPORT_SYMBOL(cfg80211_send_assoc_timeout);

244 245
void cfg80211_michael_mic_failure(struct net_device *dev, const u8 *addr,
				  enum nl80211_key_type key_type, int key_id,
246
				  const u8 *tsc, gfp_t gfp)
247 248 249
{
	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
J
Johannes Berg 已提交
250
#ifdef CONFIG_CFG80211_WEXT
251
	union iwreq_data wrqu;
252
	char *buf = kmalloc(128, gfp);
253 254 255 256 257 258 259 260 261 262 263 264 265

	if (buf) {
		sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
			"keyid=%d %scast addr=%pM)", key_id,
			key_type == NL80211_KEYTYPE_GROUP ? "broad" : "uni",
			addr);
		memset(&wrqu, 0, sizeof(wrqu));
		wrqu.data.length = strlen(buf);
		wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
		kfree(buf);
	}
#endif

266
	nl80211_michael_mic_failure(rdev, dev, addr, key_type, key_id, tsc, gfp);
267 268
}
EXPORT_SYMBOL(cfg80211_michael_mic_failure);
J
Johannes Berg 已提交
269 270

/* some MLME handling for userspace SME */
J
Johannes Berg 已提交
271 272 273 274 275 276
int __cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
			 struct net_device *dev,
			 struct ieee80211_channel *chan,
			 enum nl80211_auth_type auth_type,
			 const u8 *bssid,
			 const u8 *ssid, int ssid_len,
J
Johannes Berg 已提交
277
			 const u8 *ie, int ie_len,
278 279
			 const u8 *key, int key_len, int key_idx,
			 const u8 *sae_data, int sae_data_len)
J
Johannes Berg 已提交
280 281 282
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_auth_request req;
283
	int err;
J
Johannes Berg 已提交
284

J
Johannes Berg 已提交
285 286
	ASSERT_WDEV_LOCK(wdev);

J
Johannes Berg 已提交
287 288 289 290
	if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
		if (!key || !key_len || key_idx < 0 || key_idx > 4)
			return -EINVAL;

291
	if (wdev->current_bss &&
292
	    ether_addr_equal(bssid, wdev->current_bss->pub.bssid))
293 294
		return -EALREADY;

J
Johannes Berg 已提交
295 296 297 298
	memset(&req, 0, sizeof(req));

	req.ie = ie;
	req.ie_len = ie_len;
299 300
	req.sae_data = sae_data;
	req.sae_data_len = sae_data_len;
J
Johannes Berg 已提交
301 302 303
	req.auth_type = auth_type;
	req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
				   WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
J
Johannes Berg 已提交
304 305 306
	req.key = key;
	req.key_len = key_len;
	req.key_idx = key_idx;
J
Johannes Berg 已提交
307 308 309
	if (!req.bss)
		return -ENOENT;

310 311 312 313 314
	err = cfg80211_can_use_chan(rdev, wdev, req.bss->channel,
				    CHAN_MODE_SHARED);
	if (err)
		goto out;

315
	err = rdev_auth(rdev, dev, &req);
J
Johannes Berg 已提交
316

317
out:
318
	cfg80211_put_bss(req.bss);
J
Johannes Berg 已提交
319 320 321
	return err;
}

J
Johannes Berg 已提交
322 323 324 325
int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
		       struct net_device *dev, struct ieee80211_channel *chan,
		       enum nl80211_auth_type auth_type, const u8 *bssid,
		       const u8 *ssid, int ssid_len,
J
Johannes Berg 已提交
326
		       const u8 *ie, int ie_len,
327 328
		       const u8 *key, int key_len, int key_idx,
		       const u8 *sae_data, int sae_data_len)
J
Johannes Berg 已提交
329 330 331
{
	int err;

332
	mutex_lock(&rdev->devlist_mtx);
J
Johannes Berg 已提交
333 334
	wdev_lock(dev->ieee80211_ptr);
	err = __cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
J
Johannes Berg 已提交
335
				   ssid, ssid_len, ie, ie_len,
336 337
				   key, key_len, key_idx,
				   sae_data, sae_data_len);
J
Johannes Berg 已提交
338
	wdev_unlock(dev->ieee80211_ptr);
339
	mutex_unlock(&rdev->devlist_mtx);
J
Johannes Berg 已提交
340 341 342 343

	return err;
}

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
/*  Do a logical ht_capa &= ht_capa_mask.  */
void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
			       const struct ieee80211_ht_cap *ht_capa_mask)
{
	int i;
	u8 *p1, *p2;
	if (!ht_capa_mask) {
		memset(ht_capa, 0, sizeof(*ht_capa));
		return;
	}

	p1 = (u8*)(ht_capa);
	p2 = (u8*)(ht_capa_mask);
	for (i = 0; i<sizeof(*ht_capa); i++)
		p1[i] &= p2[i];
}

J
Johannes Berg 已提交
361 362 363 364 365 366
int __cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
			  struct net_device *dev,
			  struct ieee80211_channel *chan,
			  const u8 *bssid, const u8 *prev_bssid,
			  const u8 *ssid, int ssid_len,
			  const u8 *ie, int ie_len, bool use_mfp,
367 368 369
			  struct cfg80211_crypto_settings *crypt,
			  u32 assoc_flags, struct ieee80211_ht_cap *ht_capa,
			  struct ieee80211_ht_cap *ht_capa_mask)
J
Johannes Berg 已提交
370 371 372
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_assoc_request req;
373
	int err;
374
	bool was_connected = false;
J
Johannes Berg 已提交
375

J
Johannes Berg 已提交
376 377
	ASSERT_WDEV_LOCK(wdev);

J
Johannes Berg 已提交
378 379
	memset(&req, 0, sizeof(req));

380
	if (wdev->current_bss && prev_bssid &&
381
	    ether_addr_equal(wdev->current_bss->pub.bssid, prev_bssid)) {
382 383 384 385 386 387 388 389 390
		/*
		 * Trying to reassociate: Allow this to proceed and let the old
		 * association to be dropped when the new one is completed.
		 */
		if (wdev->sme_state == CFG80211_SME_CONNECTED) {
			was_connected = true;
			wdev->sme_state = CFG80211_SME_CONNECTING;
		}
	} else if (wdev->current_bss)
J
Johannes Berg 已提交
391 392 393 394 395 396
		return -EALREADY;

	req.ie = ie;
	req.ie_len = ie_len;
	memcpy(&req.crypto, crypt, sizeof(req.crypto));
	req.use_mfp = use_mfp;
397
	req.prev_bssid = prev_bssid;
398 399 400 401 402 403 404 405 406
	req.flags = assoc_flags;
	if (ht_capa)
		memcpy(&req.ht_capa, ht_capa, sizeof(req.ht_capa));
	if (ht_capa_mask)
		memcpy(&req.ht_capa_mask, ht_capa_mask,
		       sizeof(req.ht_capa_mask));
	cfg80211_oper_and_ht_capa(&req.ht_capa_mask,
				  rdev->wiphy.ht_capa_mod_mask);

J
Johannes Berg 已提交
407 408
	req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
				   WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
409 410 411
	if (!req.bss) {
		if (was_connected)
			wdev->sme_state = CFG80211_SME_CONNECTED;
J
Johannes Berg 已提交
412
		return -ENOENT;
413
	}
J
Johannes Berg 已提交
414

415 416 417 418 419
	err = cfg80211_can_use_chan(rdev, wdev, req.bss->channel,
				    CHAN_MODE_SHARED);
	if (err)
		goto out;

420
	err = rdev_assoc(rdev, dev, &req);
J
Johannes Berg 已提交
421

422
out:
423 424 425 426
	if (err) {
		if (was_connected)
			wdev->sme_state = CFG80211_SME_CONNECTED;
		cfg80211_put_bss(req.bss);
J
Johannes Berg 已提交
427 428 429 430 431
	}

	return err;
}

J
Johannes Berg 已提交
432 433 434 435 436 437
int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
			struct net_device *dev,
			struct ieee80211_channel *chan,
			const u8 *bssid, const u8 *prev_bssid,
			const u8 *ssid, int ssid_len,
			const u8 *ie, int ie_len, bool use_mfp,
438 439 440
			struct cfg80211_crypto_settings *crypt,
			u32 assoc_flags, struct ieee80211_ht_cap *ht_capa,
			struct ieee80211_ht_cap *ht_capa_mask)
J
Johannes Berg 已提交
441 442 443 444
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	int err;

445
	mutex_lock(&rdev->devlist_mtx);
J
Johannes Berg 已提交
446 447
	wdev_lock(wdev);
	err = __cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
448 449
				    ssid, ssid_len, ie, ie_len, use_mfp, crypt,
				    assoc_flags, ht_capa, ht_capa_mask);
J
Johannes Berg 已提交
450
	wdev_unlock(wdev);
451
	mutex_unlock(&rdev->devlist_mtx);
J
Johannes Berg 已提交
452 453 454 455 456 457

	return err;
}

int __cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
			   struct net_device *dev, const u8 *bssid,
458 459
			   const u8 *ie, int ie_len, u16 reason,
			   bool local_state_change)
J
Johannes Berg 已提交
460 461
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
462 463 464 465 466 467
	struct cfg80211_deauth_request req = {
		.bssid = bssid,
		.reason_code = reason,
		.ie = ie,
		.ie_len = ie_len,
	};
J
Johannes Berg 已提交
468

J
Johannes Berg 已提交
469 470
	ASSERT_WDEV_LOCK(wdev);

471 472
	if (local_state_change) {
		if (wdev->current_bss &&
473
		    ether_addr_equal(wdev->current_bss->pub.bssid, bssid)) {
474 475 476
			cfg80211_unhold_bss(wdev->current_bss);
			cfg80211_put_bss(&wdev->current_bss->pub);
			wdev->current_bss = NULL;
J
Johannes Berg 已提交
477 478
		}

479 480
		return 0;
	}
J
Johannes Berg 已提交
481

482
	return rdev_deauth(rdev, dev, &req);
J
Johannes Berg 已提交
483 484
}

J
Johannes Berg 已提交
485 486
int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
			 struct net_device *dev, const u8 *bssid,
487 488
			 const u8 *ie, int ie_len, u16 reason,
			 bool local_state_change)
J
Johannes Berg 已提交
489 490 491 492 493
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	int err;

	wdev_lock(wdev);
494 495
	err = __cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason,
				     local_state_change);
J
Johannes Berg 已提交
496 497 498 499 500 501 502
	wdev_unlock(wdev);

	return err;
}

static int __cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
				    struct net_device *dev, const u8 *bssid,
503 504
				    const u8 *ie, int ie_len, u16 reason,
				    bool local_state_change)
J
Johannes Berg 已提交
505 506 507 508
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_disassoc_request req;

J
Johannes Berg 已提交
509 510
	ASSERT_WDEV_LOCK(wdev);

511 512 513 514 515 516
	if (wdev->sme_state != CFG80211_SME_CONNECTED)
		return -ENOTCONN;

	if (WARN_ON(!wdev->current_bss))
		return -ENOTCONN;

J
Johannes Berg 已提交
517 518
	memset(&req, 0, sizeof(req));
	req.reason_code = reason;
519
	req.local_state_change = local_state_change;
J
Johannes Berg 已提交
520 521
	req.ie = ie;
	req.ie_len = ie_len;
522
	if (ether_addr_equal(wdev->current_bss->pub.bssid, bssid))
J
Johannes Berg 已提交
523 524 525 526
		req.bss = &wdev->current_bss->pub;
	else
		return -ENOTCONN;

527
	return rdev_disassoc(rdev, dev, &req);
J
Johannes Berg 已提交
528 529 530 531
}

int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
			   struct net_device *dev, const u8 *bssid,
532 533
			   const u8 *ie, int ie_len, u16 reason,
			   bool local_state_change)
J
Johannes Berg 已提交
534 535 536 537 538
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	int err;

	wdev_lock(wdev);
539 540
	err = __cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason,
				       local_state_change);
J
Johannes Berg 已提交
541 542 543
	wdev_unlock(wdev);

	return err;
J
Johannes Berg 已提交
544 545 546 547 548 549 550
}

void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
			struct net_device *dev)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_deauth_request req;
551
	u8 bssid[ETH_ALEN];
J
Johannes Berg 已提交
552

J
Johannes Berg 已提交
553 554
	ASSERT_WDEV_LOCK(wdev);

J
Johannes Berg 已提交
555 556 557 558 559 560 561 562
	if (!rdev->ops->deauth)
		return;

	memset(&req, 0, sizeof(req));
	req.reason_code = WLAN_REASON_DEAUTH_LEAVING;
	req.ie = NULL;
	req.ie_len = 0;

563 564
	if (!wdev->current_bss)
		return;
J
Johannes Berg 已提交
565

566 567
	memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
	req.bssid = bssid;
568
	rdev_deauth(rdev, dev, &req);
569 570 571 572 573

	if (wdev->current_bss) {
		cfg80211_unhold_bss(wdev->current_bss);
		cfg80211_put_bss(&wdev->current_bss->pub);
		wdev->current_bss = NULL;
J
Johannes Berg 已提交
574 575
	}
}
576

577
void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
578 579 580 581
			       struct ieee80211_channel *chan,
			       enum nl80211_channel_type channel_type,
			       unsigned int duration, gfp_t gfp)
{
582
	struct wiphy *wiphy = wdev->wiphy;
583 584
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

585
	nl80211_send_remain_on_channel(rdev, wdev, cookie, chan, channel_type,
586 587 588 589
				       duration, gfp);
}
EXPORT_SYMBOL(cfg80211_ready_on_channel);

590
void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
591 592 593 594
					struct ieee80211_channel *chan,
					enum nl80211_channel_type channel_type,
					gfp_t gfp)
{
595
	struct wiphy *wiphy = wdev->wiphy;
596 597
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

598
	nl80211_send_remain_on_channel_cancel(rdev, wdev, cookie, chan,
599 600 601
					      channel_type, gfp);
}
EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
602 603 604 605 606 607 608 609 610 611

void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
		      struct station_info *sinfo, gfp_t gfp)
{
	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

	nl80211_send_sta_event(rdev, dev, mac_addr, sinfo, gfp);
}
EXPORT_SYMBOL(cfg80211_new_sta);
612

613 614 615 616 617 618 619 620 621
void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
{
	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

	nl80211_send_sta_del_event(rdev, dev, mac_addr, gfp);
}
EXPORT_SYMBOL(cfg80211_del_sta);

622 623 624 625 626 627 628 629 630 631 632
void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
			  enum nl80211_connect_failed_reason reason,
			  gfp_t gfp)
{
	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

	nl80211_send_conn_failed_event(rdev, dev, mac_addr, reason, gfp);
}
EXPORT_SYMBOL(cfg80211_conn_failed);

633
struct cfg80211_mgmt_registration {
634 635
	struct list_head list;

636
	u32 nlportid;
637 638 639

	int match_len;

640 641
	__le16 frame_type;

642 643 644
	u8 match[];
};

645
int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_portid,
646 647
				u16 frame_type, const u8 *match_data,
				int match_len)
648
{
649 650
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
651
	struct cfg80211_mgmt_registration *reg, *nreg;
652
	int err = 0;
653 654 655 656 657 658 659 660 661 662 663 664 665 666
	u16 mgmt_type;

	if (!wdev->wiphy->mgmt_stypes)
		return -EOPNOTSUPP;

	if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT)
		return -EINVAL;

	if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE))
		return -EINVAL;

	mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
	if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type)))
		return -EINVAL;
667 668 669 670 671

	nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
	if (!nreg)
		return -ENOMEM;

672
	spin_lock_bh(&wdev->mgmt_registrations_lock);
673

674
	list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
675 676
		int mlen = min(match_len, reg->match_len);

677 678 679
		if (frame_type != le16_to_cpu(reg->frame_type))
			continue;

680 681 682 683 684 685 686 687 688 689 690 691 692
		if (memcmp(reg->match, match_data, mlen) == 0) {
			err = -EALREADY;
			break;
		}
	}

	if (err) {
		kfree(nreg);
		goto out;
	}

	memcpy(nreg->match, match_data, match_len);
	nreg->match_len = match_len;
693
	nreg->nlportid = snd_portid;
694 695
	nreg->frame_type = cpu_to_le16(frame_type);
	list_add(&nreg->list, &wdev->mgmt_registrations);
696

697
	if (rdev->ops->mgmt_frame_register)
698
		rdev_mgmt_frame_register(rdev, wdev, frame_type, true);
699

700
 out:
701
	spin_unlock_bh(&wdev->mgmt_registrations_lock);
702

703 704 705
	return err;
}

706
void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlportid)
707
{
708 709
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
710
	struct cfg80211_mgmt_registration *reg, *tmp;
711

712
	spin_lock_bh(&wdev->mgmt_registrations_lock);
713

714
	list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
715
		if (reg->nlportid != nlportid)
716 717 718 719 720
			continue;

		if (rdev->ops->mgmt_frame_register) {
			u16 frame_type = le16_to_cpu(reg->frame_type);

721 722
			rdev_mgmt_frame_register(rdev, wdev,
						 frame_type, false);
723
		}
724 725 726

		list_del(&reg->list);
		kfree(reg);
727 728
	}

729
	spin_unlock_bh(&wdev->mgmt_registrations_lock);
730

731 732
	if (nlportid == wdev->ap_unexpected_nlportid)
		wdev->ap_unexpected_nlportid = 0;
733 734
}

735
void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
736
{
737
	struct cfg80211_mgmt_registration *reg, *tmp;
738

739
	spin_lock_bh(&wdev->mgmt_registrations_lock);
740

741
	list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
742 743 744 745
		list_del(&reg->list);
		kfree(reg);
	}

746
	spin_unlock_bh(&wdev->mgmt_registrations_lock);
747 748
}

749
int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
750
			  struct wireless_dev *wdev,
751
			  struct ieee80211_channel *chan, bool offchan,
752
			  enum nl80211_channel_type channel_type,
753
			  bool channel_type_valid, unsigned int wait,
754
			  const u8 *buf, size_t len, bool no_cck,
755
			  bool dont_wait_for_ack, u64 *cookie)
756 757
{
	const struct ieee80211_mgmt *mgmt;
758 759 760 761
	u16 stype;

	if (!wdev->wiphy->mgmt_stypes)
		return -EOPNOTSUPP;
762

763
	if (!rdev->ops->mgmt_tx)
764
		return -EOPNOTSUPP;
765

766 767 768 769
	if (len < 24 + 1)
		return -EINVAL;

	mgmt = (const struct ieee80211_mgmt *) buf;
770 771

	if (!ieee80211_is_mgmt(mgmt->frame_control))
772
		return -EINVAL;
773 774 775 776 777 778 779

	stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
	if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
		return -EINVAL;

	if (ieee80211_is_action(mgmt->frame_control) &&
	    mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
780 781
		int err = 0;

782 783
		wdev_lock(wdev);

784 785 786 787 788 789 790 791 792
		switch (wdev->iftype) {
		case NL80211_IFTYPE_ADHOC:
		case NL80211_IFTYPE_STATION:
		case NL80211_IFTYPE_P2P_CLIENT:
			if (!wdev->current_bss) {
				err = -ENOTCONN;
				break;
			}

793 794
			if (!ether_addr_equal(wdev->current_bss->pub.bssid,
					      mgmt->bssid)) {
795 796 797 798 799 800 801 802 803 804
				err = -ENOTCONN;
				break;
			}

			/*
			 * check for IBSS DA must be done by driver as
			 * cfg80211 doesn't track the stations
			 */
			if (wdev->iftype == NL80211_IFTYPE_ADHOC)
				break;
805

806
			/* for station, check that DA is the AP */
807 808
			if (!ether_addr_equal(wdev->current_bss->pub.bssid,
					      mgmt->da)) {
809 810 811 812 813 814 815
				err = -ENOTCONN;
				break;
			}
			break;
		case NL80211_IFTYPE_AP:
		case NL80211_IFTYPE_P2P_GO:
		case NL80211_IFTYPE_AP_VLAN:
816
			if (!ether_addr_equal(mgmt->bssid, wdev_address(wdev)))
817 818
				err = -EINVAL;
			break;
819
		case NL80211_IFTYPE_MESH_POINT:
820
			if (!ether_addr_equal(mgmt->sa, mgmt->bssid)) {
821 822 823 824 825 826 827 828
				err = -EINVAL;
				break;
			}
			/*
			 * check for mesh DA must be done by driver as
			 * cfg80211 doesn't track the stations
			 */
			break;
829 830 831 832 833
		case NL80211_IFTYPE_P2P_DEVICE:
			/*
			 * fall through, P2P device only supports
			 * public action frames
			 */
834 835 836 837
		default:
			err = -EOPNOTSUPP;
			break;
		}
838
		wdev_unlock(wdev);
839 840 841

		if (err)
			return err;
842 843
	}

844
	if (!ether_addr_equal(mgmt->sa, wdev_address(wdev)))
845 846 847
		return -EINVAL;

	/* Transmit the Action frame as requested by user space */
848 849 850 851
	return rdev_mgmt_tx(rdev, wdev, chan, offchan,
			    channel_type, channel_type_valid,
			    wait, buf, len, no_cck, dont_wait_for_ack,
			    cookie);
852 853
}

854
bool cfg80211_rx_mgmt(struct wireless_dev *wdev, int freq, int sig_mbm,
855
		      const u8 *buf, size_t len, gfp_t gfp)
856 857 858
{
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
859 860 861 862 863 864
	struct cfg80211_mgmt_registration *reg;
	const struct ieee80211_txrx_stypes *stypes =
		&wiphy->mgmt_stypes[wdev->iftype];
	struct ieee80211_mgmt *mgmt = (void *)buf;
	const u8 *data;
	int data_len;
865
	bool result = false;
866 867 868
	__le16 ftype = mgmt->frame_control &
		cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
	u16 stype;
869

870
	stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
871

872 873
	if (!(stypes->rx & BIT(stype)))
		return false;
874

875 876 877 878 879 880 881 882
	data = buf + ieee80211_hdrlen(mgmt->frame_control);
	data_len = len - ieee80211_hdrlen(mgmt->frame_control);

	spin_lock_bh(&wdev->mgmt_registrations_lock);

	list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
		if (reg->frame_type != ftype)
			continue;
883

884
		if (reg->match_len > data_len)
885 886
			continue;

887
		if (memcmp(reg->match, data, reg->match_len))
888 889 890 891 892
			continue;

		/* found match! */

		/* Indicate the received Action frame to user space */
893
		if (nl80211_send_mgmt(rdev, wdev, reg->nlportid,
894
				      freq, sig_mbm,
895
				      buf, len, gfp))
896 897 898 899 900 901
			continue;

		result = true;
		break;
	}

902
	spin_unlock_bh(&wdev->mgmt_registrations_lock);
903 904 905

	return result;
}
906
EXPORT_SYMBOL(cfg80211_rx_mgmt);
907

908
void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
909
			     const u8 *buf, size_t len, bool ack, gfp_t gfp)
910 911 912 913 914
{
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

	/* Indicate TX status of the Action frame to user space */
915
	nl80211_send_mgmt_tx_status(rdev, wdev, cookie, buf, len, ack, gfp);
916
}
917
EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
918 919 920 921 922 923 924 925 926 927 928 929 930

void cfg80211_cqm_rssi_notify(struct net_device *dev,
			      enum nl80211_cqm_rssi_threshold_event rssi_event,
			      gfp_t gfp)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

	/* Indicate roaming trigger event to user space */
	nl80211_send_cqm_rssi_notify(rdev, dev, rssi_event, gfp);
}
EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
931 932 933 934 935 936 937 938 939 940 941 942

void cfg80211_cqm_pktloss_notify(struct net_device *dev,
				 const u8 *peer, u32 num_packets, gfp_t gfp)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

	/* Indicate roaming trigger event to user space */
	nl80211_send_cqm_pktloss_notify(rdev, dev, peer, num_packets, gfp);
}
EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
943

944 945 946 947 948 949 950 951 952 953 954 955 956
void cfg80211_cqm_txe_notify(struct net_device *dev,
			     const u8 *peer, u32 num_packets,
			     u32 rate, u32 intvl, gfp_t gfp)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

	nl80211_send_cqm_txe_notify(rdev, dev, peer, num_packets,
				    rate, intvl, gfp);
}
EXPORT_SYMBOL(cfg80211_cqm_txe_notify);

957 958 959 960 961 962 963 964 965 966
void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
			       const u8 *replay_ctr, gfp_t gfp)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

	nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
}
EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
967 968 969 970 971 972 973 974 975 976 977

void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
				     const u8 *bssid, bool preauth, gfp_t gfp)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);

	nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
}
EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
978

979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
void cfg80211_ch_switch_notify(struct net_device *dev, int freq,
			       enum nl80211_channel_type type)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct wiphy *wiphy = wdev->wiphy;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
	struct ieee80211_channel *chan;

	wdev_lock(wdev);

	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
		    wdev->iftype != NL80211_IFTYPE_P2P_GO))
		goto out;

	chan = rdev_freq_to_chan(rdev, freq, type);
	if (WARN_ON(!chan))
		goto out;

997
	wdev->channel = chan;
998 999 1000 1001 1002 1003 1004
	nl80211_ch_switch_notify(rdev, dev, freq, type, GFP_KERNEL);
out:
	wdev_unlock(wdev);
	return;
}
EXPORT_SYMBOL(cfg80211_ch_switch_notify);

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
bool cfg80211_rx_spurious_frame(struct net_device *dev,
				const u8 *addr, gfp_t gfp)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
		    wdev->iftype != NL80211_IFTYPE_P2P_GO))
		return false;

	return nl80211_unexpected_frame(dev, addr, gfp);
}
EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030

bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
					const u8 *addr, gfp_t gfp)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
		    wdev->iftype != NL80211_IFTYPE_P2P_GO &&
		    wdev->iftype != NL80211_IFTYPE_AP_VLAN))
		return false;

	return nl80211_unexpected_4addr_frame(dev, addr, gfp);
}
EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);