Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * SME code for cfg80211
   4 * both driver SME event handling and the SME implementation
   5 * (for nl80211's connect() and wext)
   6 *
   7 * Copyright 2009	Johannes Berg <johannes@sipsolutions.net>
   8 * Copyright (C) 2009, 2020, 2022-2024 Intel Corporation. All rights reserved.
   9 * Copyright 2017	Intel Deutschland GmbH
  10 */
  11
  12#include <linux/etherdevice.h>
  13#include <linux/if_arp.h>
  14#include <linux/slab.h>
  15#include <linux/workqueue.h>
  16#include <linux/wireless.h>
  17#include <linux/export.h>
  18#include <net/iw_handler.h>
  19#include <net/cfg80211.h>
  20#include <net/rtnetlink.h>
  21#include "nl80211.h"
  22#include "reg.h"
  23#include "rdev-ops.h"
  24
  25/*
  26 * Software SME in cfg80211, using auth/assoc/deauth calls to the
  27 * driver. This is for implementing nl80211's connect/disconnect
  28 * and wireless extensions (if configured.)
  29 */
  30
  31struct cfg80211_conn {
  32	struct cfg80211_connect_params params;
  33	/* these are sub-states of the _CONNECTING sme_state */
  34	enum {
 
  35		CFG80211_CONN_SCANNING,
  36		CFG80211_CONN_SCAN_AGAIN,
  37		CFG80211_CONN_AUTHENTICATE_NEXT,
  38		CFG80211_CONN_AUTHENTICATING,
  39		CFG80211_CONN_AUTH_FAILED_TIMEOUT,
  40		CFG80211_CONN_ASSOCIATE_NEXT,
  41		CFG80211_CONN_ASSOCIATING,
  42		CFG80211_CONN_ASSOC_FAILED,
  43		CFG80211_CONN_ASSOC_FAILED_TIMEOUT,
  44		CFG80211_CONN_DEAUTH,
  45		CFG80211_CONN_ABANDON,
  46		CFG80211_CONN_CONNECTED,
  47	} state;
  48	u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
  49	const u8 *ie;
  50	size_t ie_len;
  51	bool auto_auth, prev_bssid_valid;
  52};
  53
  54static void cfg80211_sme_free(struct wireless_dev *wdev)
  55{
  56	if (!wdev->conn)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  57		return;
  58
  59	kfree(wdev->conn->ie);
  60	kfree(wdev->conn);
  61	wdev->conn = NULL;
  62}
  63
 
 
  64static int cfg80211_conn_scan(struct wireless_dev *wdev)
  65{
  66	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  67	struct cfg80211_scan_request *request;
  68	int n_channels, err;
  69
  70	lockdep_assert_wiphy(wdev->wiphy);
 
 
  71
  72	if (rdev->scan_req || rdev->scan_msg)
  73		return -EBUSY;
  74
  75	if (wdev->conn->params.channel)
  76		n_channels = 1;
  77	else
  78		n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
 
  79
 
 
 
 
 
 
  80	request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
  81			  sizeof(request->channels[0]) * n_channels,
  82			  GFP_KERNEL);
  83	if (!request)
  84		return -ENOMEM;
  85
  86	request->n_channels = n_channels;
  87	if (wdev->conn->params.channel) {
  88		enum nl80211_band band = wdev->conn->params.channel->band;
  89		struct ieee80211_supported_band *sband =
  90			wdev->wiphy->bands[band];
  91
  92		if (!sband) {
  93			kfree(request);
  94			return -EINVAL;
  95		}
  96		request->channels[0] = wdev->conn->params.channel;
  97		request->rates[band] = (1 << sband->n_bitrates) - 1;
  98	} else {
  99		int i = 0, j;
 100		enum nl80211_band band;
 101		struct ieee80211_supported_band *bands;
 102		struct ieee80211_channel *channel;
 103
 104		for (band = 0; band < NUM_NL80211_BANDS; band++) {
 105			bands = wdev->wiphy->bands[band];
 106			if (!bands)
 107				continue;
 108			for (j = 0; j < bands->n_channels; j++) {
 109				channel = &bands->channels[j];
 110				if (channel->flags & IEEE80211_CHAN_DISABLED)
 111					continue;
 112				request->channels[i++] = channel;
 113			}
 114			request->rates[band] = (1 << bands->n_bitrates) - 1;
 115		}
 116		n_channels = i;
 117	}
 118	request->n_channels = n_channels;
 119	request->ssids = (void *)request +
 120		struct_size(request, channels, n_channels);
 121	request->n_ssids = 1;
 122
 123	memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
 124		wdev->conn->params.ssid_len);
 125	request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
 126
 127	eth_broadcast_addr(request->bssid);
 128
 129	request->wdev = wdev;
 130	request->wiphy = &rdev->wiphy;
 131	request->scan_start = jiffies;
 132
 133	rdev->scan_req = request;
 134
 135	err = cfg80211_scan(rdev);
 136	if (!err) {
 137		wdev->conn->state = CFG80211_CONN_SCANNING;
 138		nl80211_send_scan_start(rdev, wdev);
 139		dev_hold(wdev->netdev);
 140	} else {
 141		rdev->scan_req = NULL;
 142		kfree(request);
 143	}
 144	return err;
 145}
 146
 147static int cfg80211_conn_do_work(struct wireless_dev *wdev,
 148				 enum nl80211_timeout_reason *treason)
 149{
 150	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
 151	struct cfg80211_connect_params *params;
 152	struct cfg80211_auth_request auth_req = {};
 153	struct cfg80211_assoc_request req = {};
 154	int err;
 155
 156	lockdep_assert_wiphy(wdev->wiphy);
 157
 158	if (!wdev->conn)
 159		return 0;
 160
 161	params = &wdev->conn->params;
 162
 163	switch (wdev->conn->state) {
 164	case CFG80211_CONN_SCANNING:
 165		/* didn't find it during scan ... */
 166		return -ENOENT;
 167	case CFG80211_CONN_SCAN_AGAIN:
 168		return cfg80211_conn_scan(wdev);
 169	case CFG80211_CONN_AUTHENTICATE_NEXT:
 170		if (WARN_ON(!rdev->ops->auth))
 171			return -EOPNOTSUPP;
 172		wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
 173		auth_req.key = params->key;
 174		auth_req.key_len = params->key_len;
 175		auth_req.key_idx = params->key_idx;
 176		auth_req.auth_type = params->auth_type;
 177		auth_req.bss = cfg80211_get_bss(&rdev->wiphy, params->channel,
 178						params->bssid,
 179						params->ssid, params->ssid_len,
 180						IEEE80211_BSS_TYPE_ESS,
 181						IEEE80211_PRIVACY_ANY);
 182		auth_req.link_id = -1;
 183		err = cfg80211_mlme_auth(rdev, wdev->netdev, &auth_req);
 184		cfg80211_put_bss(&rdev->wiphy, auth_req.bss);
 185		return err;
 186	case CFG80211_CONN_AUTH_FAILED_TIMEOUT:
 187		*treason = NL80211_TIMEOUT_AUTH;
 188		return -ENOTCONN;
 189	case CFG80211_CONN_ASSOCIATE_NEXT:
 190		if (WARN_ON(!rdev->ops->assoc))
 191			return -EOPNOTSUPP;
 192		wdev->conn->state = CFG80211_CONN_ASSOCIATING;
 193		if (wdev->conn->prev_bssid_valid)
 194			req.prev_bssid = wdev->conn->prev_bssid;
 195		req.ie = params->ie;
 196		req.ie_len = params->ie_len;
 197		req.use_mfp = params->mfp != NL80211_MFP_NO;
 198		req.crypto = params->crypto;
 199		req.flags = params->flags;
 200		req.ht_capa = params->ht_capa;
 201		req.ht_capa_mask = params->ht_capa_mask;
 202		req.vht_capa = params->vht_capa;
 203		req.vht_capa_mask = params->vht_capa_mask;
 204		req.link_id = -1;
 205
 206		req.bss = cfg80211_get_bss(&rdev->wiphy, params->channel,
 207					   params->bssid,
 208					   params->ssid, params->ssid_len,
 209					   IEEE80211_BSS_TYPE_ESS,
 210					   IEEE80211_PRIVACY_ANY);
 211		if (!req.bss) {
 212			err = -ENOENT;
 213		} else {
 214			err = cfg80211_mlme_assoc(rdev, wdev->netdev,
 215						  &req, NULL);
 216			cfg80211_put_bss(&rdev->wiphy, req.bss);
 217		}
 218
 219		if (err)
 220			cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
 221					     NULL, 0,
 222					     WLAN_REASON_DEAUTH_LEAVING,
 223					     false);
 224		return err;
 225	case CFG80211_CONN_ASSOC_FAILED_TIMEOUT:
 226		*treason = NL80211_TIMEOUT_ASSOC;
 227		fallthrough;
 228	case CFG80211_CONN_ASSOC_FAILED:
 229		cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
 230				     NULL, 0,
 231				     WLAN_REASON_DEAUTH_LEAVING, false);
 232		return -ENOTCONN;
 233	case CFG80211_CONN_DEAUTH:
 234		cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
 235				     NULL, 0,
 236				     WLAN_REASON_DEAUTH_LEAVING, false);
 237		fallthrough;
 238	case CFG80211_CONN_ABANDON:
 239		/* free directly, disconnected event already sent */
 240		cfg80211_sme_free(wdev);
 241		return 0;
 242	default:
 243		return 0;
 244	}
 245}
 246
 247void cfg80211_conn_work(struct work_struct *work)
 248{
 249	struct cfg80211_registered_device *rdev =
 250		container_of(work, struct cfg80211_registered_device, conn_work);
 251	struct wireless_dev *wdev;
 252	u8 bssid_buf[ETH_ALEN], *bssid = NULL;
 253	enum nl80211_timeout_reason treason;
 254
 255	wiphy_lock(&rdev->wiphy);
 256
 257	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
 258		if (!wdev->netdev)
 259			continue;
 260
 261		if (!netif_running(wdev->netdev))
 
 
 
 262			continue;
 263
 264		if (!wdev->conn ||
 265		    wdev->conn->state == CFG80211_CONN_CONNECTED)
 266			continue;
 267
 268		if (wdev->conn->params.bssid) {
 269			memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
 270			bssid = bssid_buf;
 271		}
 272		treason = NL80211_TIMEOUT_UNSPECIFIED;
 273		if (cfg80211_conn_do_work(wdev, &treason)) {
 274			struct cfg80211_connect_resp_params cr;
 275
 276			memset(&cr, 0, sizeof(cr));
 277			cr.status = -1;
 278			cr.links[0].bssid = bssid;
 279			cr.timeout_reason = treason;
 280			__cfg80211_connect_result(wdev->netdev, &cr, false);
 281		}
 282	}
 283
 284	wiphy_unlock(&rdev->wiphy);
 285}
 286
 287static void cfg80211_step_auth_next(struct cfg80211_conn *conn,
 288				    struct cfg80211_bss *bss)
 289{
 290	memcpy(conn->bssid, bss->bssid, ETH_ALEN);
 291	conn->params.bssid = conn->bssid;
 292	conn->params.channel = bss->channel;
 293	conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
 294}
 295
 296/* Returned bss is reference counted and must be cleaned up appropriately. */
 297static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
 298{
 299	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
 300	struct cfg80211_bss *bss;
 
 
 
 301
 302	lockdep_assert_wiphy(wdev->wiphy);
 
 303
 304	bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
 305			       wdev->conn->params.bssid,
 306			       wdev->conn->params.ssid,
 307			       wdev->conn->params.ssid_len,
 308			       wdev->conn_bss_type,
 309			       IEEE80211_PRIVACY(wdev->conn->params.privacy));
 310	if (!bss)
 311		return NULL;
 312
 313	cfg80211_step_auth_next(wdev->conn, bss);
 
 
 
 314	schedule_work(&rdev->conn_work);
 315
 316	return bss;
 317}
 318
 319void cfg80211_sme_scan_done(struct net_device *dev)
 320{
 321	struct wireless_dev *wdev = dev->ieee80211_ptr;
 322	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
 323	struct cfg80211_bss *bss;
 324
 325	lockdep_assert_wiphy(wdev->wiphy);
 
 
 
 326
 327	if (!wdev->conn)
 328		return;
 329
 330	if (wdev->conn->state != CFG80211_CONN_SCANNING &&
 331	    wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
 332		return;
 333
 334	bss = cfg80211_get_conn_bss(wdev);
 335	if (bss)
 336		cfg80211_put_bss(&rdev->wiphy, bss);
 337	else
 338		schedule_work(&rdev->conn_work);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 339}
 340
 341void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
 
 342{
 
 343	struct wiphy *wiphy = wdev->wiphy;
 344	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
 345	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
 346	u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
 347
 348	lockdep_assert_wiphy(wdev->wiphy);
 
 
 
 
 349
 350	if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
 351		return;
 352
 353	if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
 354	    wdev->conn->auto_auth &&
 355	    wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
 356		/* select automatically between only open, shared, leap */
 357		switch (wdev->conn->params.auth_type) {
 358		case NL80211_AUTHTYPE_OPEN_SYSTEM:
 359			if (wdev->connect_keys)
 360				wdev->conn->params.auth_type =
 361					NL80211_AUTHTYPE_SHARED_KEY;
 362			else
 363				wdev->conn->params.auth_type =
 364					NL80211_AUTHTYPE_NETWORK_EAP;
 365			break;
 366		case NL80211_AUTHTYPE_SHARED_KEY:
 367			wdev->conn->params.auth_type =
 368				NL80211_AUTHTYPE_NETWORK_EAP;
 369			break;
 370		default:
 371			/* huh? */
 372			wdev->conn->params.auth_type =
 373				NL80211_AUTHTYPE_OPEN_SYSTEM;
 374			break;
 375		}
 376		wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
 377		schedule_work(&rdev->conn_work);
 378	} else if (status_code != WLAN_STATUS_SUCCESS) {
 379		struct cfg80211_connect_resp_params cr;
 380
 381		memset(&cr, 0, sizeof(cr));
 382		cr.status = status_code;
 383		cr.links[0].bssid = mgmt->bssid;
 384		cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED;
 385		__cfg80211_connect_result(wdev->netdev, &cr, false);
 386	} else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
 387		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
 388		schedule_work(&rdev->conn_work);
 389	}
 390}
 391
 392bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
 393{
 394	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
 
 395
 396	if (!wdev->conn)
 397		return false;
 398
 399	if (status == WLAN_STATUS_SUCCESS) {
 400		wdev->conn->state = CFG80211_CONN_CONNECTED;
 401		return false;
 402	}
 403
 404	if (wdev->conn->prev_bssid_valid) {
 405		/*
 406		 * Some stupid APs don't accept reassoc, so we
 407		 * need to fall back to trying regular assoc;
 408		 * return true so no event is sent to userspace.
 409		 */
 410		wdev->conn->prev_bssid_valid = false;
 411		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
 412		schedule_work(&rdev->conn_work);
 413		return true;
 414	}
 415
 416	wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
 417	schedule_work(&rdev->conn_work);
 418	return false;
 419}
 420
 421void cfg80211_sme_deauth(struct wireless_dev *wdev)
 422{
 423	cfg80211_sme_free(wdev);
 424}
 425
 426void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
 427{
 428	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
 429
 430	if (!wdev->conn)
 431		return;
 432
 433	wdev->conn->state = CFG80211_CONN_AUTH_FAILED_TIMEOUT;
 434	schedule_work(&rdev->conn_work);
 435}
 436
 437void cfg80211_sme_disassoc(struct wireless_dev *wdev)
 438{
 439	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
 440
 441	if (!wdev->conn)
 442		return;
 443
 444	wdev->conn->state = CFG80211_CONN_DEAUTH;
 445	schedule_work(&rdev->conn_work);
 446}
 447
 448void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
 449{
 450	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
 451
 452	if (!wdev->conn)
 453		return;
 454
 455	wdev->conn->state = CFG80211_CONN_ASSOC_FAILED_TIMEOUT;
 456	schedule_work(&rdev->conn_work);
 457}
 458
 459void cfg80211_sme_abandon_assoc(struct wireless_dev *wdev)
 460{
 461	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
 462
 463	if (!wdev->conn)
 464		return;
 465
 466	wdev->conn->state = CFG80211_CONN_ABANDON;
 467	schedule_work(&rdev->conn_work);
 468}
 469
 470static void cfg80211_wdev_release_bsses(struct wireless_dev *wdev)
 471{
 472	unsigned int link;
 473
 474	for_each_valid_link(wdev, link) {
 475		if (!wdev->links[link].client.current_bss)
 476			continue;
 477		cfg80211_unhold_bss(wdev->links[link].client.current_bss);
 478		cfg80211_put_bss(wdev->wiphy,
 479				 &wdev->links[link].client.current_bss->pub);
 480		wdev->links[link].client.current_bss = NULL;
 481	}
 482}
 483
 484void cfg80211_wdev_release_link_bsses(struct wireless_dev *wdev, u16 link_mask)
 485{
 486	unsigned int link;
 487
 488	for_each_valid_link(wdev, link) {
 489		if (!wdev->links[link].client.current_bss ||
 490		    !(link_mask & BIT(link)))
 491			continue;
 492		cfg80211_unhold_bss(wdev->links[link].client.current_bss);
 493		cfg80211_put_bss(wdev->wiphy,
 494				 &wdev->links[link].client.current_bss->pub);
 495		wdev->links[link].client.current_bss = NULL;
 496	}
 497}
 498
 499static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
 500				     const u8 *ies, size_t ies_len,
 501				     const u8 **out_ies, size_t *out_ies_len)
 502{
 503	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
 504	u8 *buf;
 505	size_t offs;
 506
 507	if (!rdev->wiphy.extended_capabilities_len ||
 508	    (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
 509		*out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
 510		if (!*out_ies)
 511			return -ENOMEM;
 512		*out_ies_len = ies_len;
 513		return 0;
 514	}
 515
 516	buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
 517		      GFP_KERNEL);
 518	if (!buf)
 519		return -ENOMEM;
 520
 521	if (ies_len) {
 522		static const u8 before_extcapa[] = {
 523			/* not listing IEs expected to be created by driver */
 524			WLAN_EID_RSN,
 525			WLAN_EID_QOS_CAPA,
 526			WLAN_EID_RRM_ENABLED_CAPABILITIES,
 527			WLAN_EID_MOBILITY_DOMAIN,
 528			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
 529			WLAN_EID_BSS_COEX_2040,
 530		};
 531
 532		offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
 533					  ARRAY_SIZE(before_extcapa), 0);
 534		memcpy(buf, ies, offs);
 535		/* leave a whole for extended capabilities IE */
 536		memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
 537		       ies + offs, ies_len - offs);
 538	} else {
 539		offs = 0;
 540	}
 541
 542	/* place extended capabilities IE (with only driver capabilities) */
 543	buf[offs] = WLAN_EID_EXT_CAPABILITY;
 544	buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
 545	memcpy(buf + offs + 2,
 546	       rdev->wiphy.extended_capabilities,
 547	       rdev->wiphy.extended_capabilities_len);
 548
 549	*out_ies = buf;
 550	*out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
 551
 552	return 0;
 553}
 554
 555static int cfg80211_sme_connect(struct wireless_dev *wdev,
 556				struct cfg80211_connect_params *connect,
 557				const u8 *prev_bssid)
 558{
 559	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
 560	struct cfg80211_bss *bss;
 561	int err;
 562
 563	if (!rdev->ops->auth || !rdev->ops->assoc)
 564		return -EOPNOTSUPP;
 565
 566	cfg80211_wdev_release_bsses(wdev);
 567
 568	if (wdev->connected) {
 569		cfg80211_sme_free(wdev);
 570		wdev->connected = false;
 571	}
 572
 573	if (wdev->conn)
 574		return -EINPROGRESS;
 575
 576	wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
 577	if (!wdev->conn)
 578		return -ENOMEM;
 579
 580	/*
 581	 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
 
 582	 */
 583	memcpy(&wdev->conn->params, connect, sizeof(*connect));
 584	if (connect->bssid) {
 585		wdev->conn->params.bssid = wdev->conn->bssid;
 586		memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
 587	}
 588
 589	if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
 590				      &wdev->conn->ie,
 591				      &wdev->conn->params.ie_len)) {
 592		kfree(wdev->conn);
 593		wdev->conn = NULL;
 594		return -ENOMEM;
 595	}
 596	wdev->conn->params.ie = wdev->conn->ie;
 597
 598	if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
 599		wdev->conn->auto_auth = true;
 600		/* start with open system ... should mostly work */
 601		wdev->conn->params.auth_type =
 602			NL80211_AUTHTYPE_OPEN_SYSTEM;
 603	} else {
 604		wdev->conn->auto_auth = false;
 605	}
 606
 607	wdev->conn->params.ssid = wdev->u.client.ssid;
 608	wdev->conn->params.ssid_len = wdev->u.client.ssid_len;
 609
 610	/* see if we have the bss already */
 611	bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
 612			       wdev->conn->params.bssid,
 613			       wdev->conn->params.ssid,
 614			       wdev->conn->params.ssid_len,
 615			       wdev->conn_bss_type,
 616			       IEEE80211_PRIVACY(wdev->conn->params.privacy));
 617
 618	if (prev_bssid) {
 619		memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
 620		wdev->conn->prev_bssid_valid = true;
 621	}
 622
 623	/* we're good if we have a matching bss struct */
 624	if (bss) {
 625		enum nl80211_timeout_reason treason;
 626
 627		cfg80211_step_auth_next(wdev->conn, bss);
 628		err = cfg80211_conn_do_work(wdev, &treason);
 629		cfg80211_put_bss(wdev->wiphy, bss);
 630	} else {
 631		/* otherwise we'll need to scan for the AP first */
 632		err = cfg80211_conn_scan(wdev);
 633
 634		/*
 635		 * If we can't scan right now, then we need to scan again
 636		 * after the current scan finished, since the parameters
 637		 * changed (unless we find a good AP anyway).
 638		 */
 639		if (err == -EBUSY) {
 640			err = 0;
 641			wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
 642		}
 643	}
 644
 645	if (err)
 646		cfg80211_sme_free(wdev);
 647
 648	return err;
 649}
 650
 651static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
 652{
 653	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
 654	int err;
 655
 656	if (!wdev->conn)
 657		return 0;
 658
 659	if (!rdev->ops->deauth)
 660		return -EOPNOTSUPP;
 661
 662	if (wdev->conn->state == CFG80211_CONN_SCANNING ||
 663	    wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
 664		err = 0;
 665		goto out;
 666	}
 667
 668	/* wdev->conn->params.bssid must be set if > SCANNING */
 669	err = cfg80211_mlme_deauth(rdev, wdev->netdev,
 670				   wdev->conn->params.bssid,
 671				   NULL, 0, reason, false);
 672 out:
 673	cfg80211_sme_free(wdev);
 674	return err;
 675}
 676
 677/*
 678 * code shared for in-device and software SME
 679 */
 680
 681static bool cfg80211_is_all_idle(void)
 682{
 683	struct cfg80211_registered_device *rdev;
 684	struct wireless_dev *wdev;
 685	bool is_all_idle = true;
 686
 687	/*
 688	 * All devices must be idle as otherwise if you are actively
 689	 * scanning some new beacon hints could be learned and would
 690	 * count as new regulatory hints.
 691	 * Also if there is any other active beaconing interface we
 692	 * need not issue a disconnect hint and reset any info such
 693	 * as chan dfs state, etc.
 694	 */
 695	for_each_rdev(rdev) {
 696		wiphy_lock(&rdev->wiphy);
 697		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
 698			if (wdev->conn || wdev->connected ||
 699			    cfg80211_beaconing_iface_active(wdev))
 700				is_all_idle = false;
 701		}
 702		wiphy_unlock(&rdev->wiphy);
 703	}
 704
 705	return is_all_idle;
 706}
 707
 708static void disconnect_work(struct work_struct *work)
 709{
 710	rtnl_lock();
 711	if (cfg80211_is_all_idle())
 712		regulatory_hint_disconnect();
 713	rtnl_unlock();
 714}
 715
 716DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
 717
 718static void
 719cfg80211_connect_result_release_bsses(struct wireless_dev *wdev,
 720				      struct cfg80211_connect_resp_params *cr)
 721{
 722	unsigned int link;
 
 723
 724	for_each_valid_link(cr, link) {
 725		if (!cr->links[link].bss)
 726			continue;
 727		cfg80211_unhold_bss(bss_from_pub(cr->links[link].bss));
 728		cfg80211_put_bss(wdev->wiphy, cr->links[link].bss);
 729	}
 730}
 731
 732/*
 733 * API calls for drivers implementing connect/disconnect and
 734 * SME event handling
 735 */
 736
 737/* This method must consume bss one way or another */
 738void __cfg80211_connect_result(struct net_device *dev,
 739			       struct cfg80211_connect_resp_params *cr,
 740			       bool wextev)
 741{
 742	struct wireless_dev *wdev = dev->ieee80211_ptr;
 743	const struct element *country_elem = NULL;
 744	const struct element *ssid;
 745	const u8 *country_data;
 746	u8 country_datalen;
 747#ifdef CONFIG_CFG80211_WEXT
 748	union iwreq_data wrqu;
 749#endif
 750	unsigned int link;
 751	const u8 *connected_addr;
 752	bool bss_not_found = false;
 753
 754	lockdep_assert_wiphy(wdev->wiphy);
 755
 756	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
 757		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
 758		goto out;
 759
 760	if (cr->valid_links) {
 761		if (WARN_ON(!cr->ap_mld_addr))
 762			goto out;
 763
 764		for_each_valid_link(cr, link) {
 765			if (WARN_ON(!cr->links[link].addr))
 766				goto out;
 767		}
 768
 769		if (WARN_ON(wdev->connect_keys))
 770			goto out;
 771	}
 772
 773	wdev->unprot_beacon_reported = 0;
 774	nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev, cr,
 775				    GFP_KERNEL);
 776	connected_addr = cr->valid_links ? cr->ap_mld_addr : cr->links[0].bssid;
 777
 778#ifdef CONFIG_CFG80211_WEXT
 779	if (wextev && !cr->valid_links) {
 780		if (cr->req_ie && cr->status == WLAN_STATUS_SUCCESS) {
 781			memset(&wrqu, 0, sizeof(wrqu));
 782			wrqu.data.length = cr->req_ie_len;
 783			wireless_send_event(dev, IWEVASSOCREQIE, &wrqu,
 784					    cr->req_ie);
 785		}
 786
 787		if (cr->resp_ie && cr->status == WLAN_STATUS_SUCCESS) {
 788			memset(&wrqu, 0, sizeof(wrqu));
 789			wrqu.data.length = cr->resp_ie_len;
 790			wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu,
 791					    cr->resp_ie);
 792		}
 793
 794		memset(&wrqu, 0, sizeof(wrqu));
 795		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
 796		if (connected_addr && cr->status == WLAN_STATUS_SUCCESS) {
 797			memcpy(wrqu.ap_addr.sa_data, connected_addr, ETH_ALEN);
 798			memcpy(wdev->wext.prev_bssid, connected_addr, ETH_ALEN);
 799			wdev->wext.prev_bssid_valid = true;
 800		}
 801		wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
 802	}
 803#endif
 804
 805	if (cr->status == WLAN_STATUS_SUCCESS) {
 806		if (!wiphy_to_rdev(wdev->wiphy)->ops->connect) {
 807			for_each_valid_link(cr, link) {
 808				if (WARN_ON_ONCE(!cr->links[link].bss))
 809					break;
 810			}
 811		}
 812
 813		for_each_valid_link(cr, link) {
 814			/* don't do extra lookups for failures */
 815			if (cr->links[link].status != WLAN_STATUS_SUCCESS)
 816				continue;
 817
 818			if (cr->links[link].bss)
 819				continue;
 820
 821			cr->links[link].bss =
 822				cfg80211_get_bss(wdev->wiphy, NULL,
 823						 cr->links[link].bssid,
 824						 wdev->u.client.ssid,
 825						 wdev->u.client.ssid_len,
 826						 wdev->conn_bss_type,
 827						 IEEE80211_PRIVACY_ANY);
 828			if (!cr->links[link].bss) {
 829				bss_not_found = true;
 830				break;
 831			}
 832			cfg80211_hold_bss(bss_from_pub(cr->links[link].bss));
 833		}
 834	}
 835
 836	cfg80211_wdev_release_bsses(wdev);
 
 837
 838	if (cr->status != WLAN_STATUS_SUCCESS) {
 839		kfree_sensitive(wdev->connect_keys);
 
 
 
 
 
 840		wdev->connect_keys = NULL;
 841		wdev->u.client.ssid_len = 0;
 842		wdev->conn_owner_nlportid = 0;
 843		cfg80211_connect_result_release_bsses(wdev, cr);
 844		cfg80211_sme_free(wdev);
 845		return;
 846	}
 847
 848	if (WARN_ON(bss_not_found)) {
 849		cfg80211_connect_result_release_bsses(wdev, cr);
 850		return;
 851	}
 852
 853	memset(wdev->links, 0, sizeof(wdev->links));
 854	for_each_valid_link(cr, link) {
 855		if (cr->links[link].status == WLAN_STATUS_SUCCESS)
 856			continue;
 857		cr->valid_links &= ~BIT(link);
 858		/* don't require bss pointer for failed links */
 859		if (!cr->links[link].bss)
 860			continue;
 861		cfg80211_unhold_bss(bss_from_pub(cr->links[link].bss));
 862		cfg80211_put_bss(wdev->wiphy, cr->links[link].bss);
 863	}
 864	wdev->valid_links = cr->valid_links;
 865	for_each_valid_link(cr, link)
 866		wdev->links[link].client.current_bss =
 867			bss_from_pub(cr->links[link].bss);
 868	wdev->connected = true;
 869	ether_addr_copy(wdev->u.client.connected_addr, connected_addr);
 870	if (cr->valid_links) {
 871		for_each_valid_link(cr, link)
 872			memcpy(wdev->links[link].addr, cr->links[link].addr,
 873			       ETH_ALEN);
 874	}
 875
 876	cfg80211_upload_connect_keys(wdev);
 877
 878	rcu_read_lock();
 879	for_each_valid_link(cr, link) {
 880		country_elem =
 881			ieee80211_bss_get_elem(cr->links[link].bss,
 882					       WLAN_EID_COUNTRY);
 883		if (country_elem)
 884			break;
 885	}
 886	if (!country_elem) {
 887		rcu_read_unlock();
 888		return;
 889	}
 890
 891	country_datalen = country_elem->datalen;
 892	country_data = kmemdup(country_elem->data, country_datalen, GFP_ATOMIC);
 893	rcu_read_unlock();
 894
 895	if (!country_data)
 896		return;
 897
 898	regulatory_hint_country_ie(wdev->wiphy,
 899				   cr->links[link].bss->channel->band,
 900				   country_data, country_datalen);
 901	kfree(country_data);
 902
 903	if (!wdev->u.client.ssid_len) {
 904		rcu_read_lock();
 905		for_each_valid_link(cr, link) {
 906			ssid = ieee80211_bss_get_elem(cr->links[link].bss,
 907						      WLAN_EID_SSID);
 908
 909			if (!ssid || !ssid->datalen)
 910				continue;
 911
 912			memcpy(wdev->u.client.ssid, ssid->data, ssid->datalen);
 913			wdev->u.client.ssid_len = ssid->datalen;
 914			break;
 915		}
 916		rcu_read_unlock();
 917	}
 918
 919	return;
 920out:
 921	for_each_valid_link(cr, link)
 922		cfg80211_put_bss(wdev->wiphy, cr->links[link].bss);
 923}
 924
 925static void cfg80211_update_link_bss(struct wireless_dev *wdev,
 926				     struct cfg80211_bss **bss)
 927{
 928	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
 929	struct cfg80211_internal_bss *ibss;
 930
 931	if (!*bss)
 932		return;
 933
 934	ibss = bss_from_pub(*bss);
 935	if (list_empty(&ibss->list)) {
 936		struct cfg80211_bss *found = NULL, *tmp = *bss;
 937
 938		found = cfg80211_get_bss(wdev->wiphy, NULL,
 939					 (*bss)->bssid,
 940					 wdev->u.client.ssid,
 941					 wdev->u.client.ssid_len,
 942					 wdev->conn_bss_type,
 943					 IEEE80211_PRIVACY_ANY);
 944		if (found) {
 945			/* The same BSS is already updated so use it
 946			 * instead, as it has latest info.
 947			 */
 948			*bss = found;
 949		} else {
 950			/* Update with BSS provided by driver, it will
 951			 * be freshly added and ref cnted, we can free
 952			 * the old one.
 953			 *
 954			 * signal_valid can be false, as we are not
 955			 * expecting the BSS to be found.
 956			 *
 957			 * keep the old timestamp to avoid confusion
 958			 */
 959			cfg80211_bss_update(rdev, ibss, false,
 960					    ibss->ts);
 961		}
 962
 963		cfg80211_put_bss(wdev->wiphy, tmp);
 964	}
 965}
 966
 967/* Consumes bss object(s) one way or another */
 968void cfg80211_connect_done(struct net_device *dev,
 969			   struct cfg80211_connect_resp_params *params,
 970			   gfp_t gfp)
 971{
 972	struct wireless_dev *wdev = dev->ieee80211_ptr;
 973	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
 974	struct cfg80211_event *ev;
 975	unsigned long flags;
 976	u8 *next;
 977	size_t link_info_size = 0;
 978	unsigned int link;
 979
 980	for_each_valid_link(params, link) {
 981		cfg80211_update_link_bss(wdev, &params->links[link].bss);
 982		link_info_size += params->links[link].bssid ? ETH_ALEN : 0;
 983		link_info_size += params->links[link].addr ? ETH_ALEN : 0;
 984	}
 985
 986	ev = kzalloc(sizeof(*ev) + (params->ap_mld_addr ? ETH_ALEN : 0) +
 987		     params->req_ie_len + params->resp_ie_len +
 988		     params->fils.kek_len + params->fils.pmk_len +
 989		     (params->fils.pmkid ? WLAN_PMKID_LEN : 0) + link_info_size,
 990		     gfp);
 991
 992	if (!ev) {
 993		for_each_valid_link(params, link)
 994			cfg80211_put_bss(wdev->wiphy,
 995					 params->links[link].bss);
 996		return;
 997	}
 998
 999	ev->type = EVENT_CONNECT_RESULT;
1000	next = ((u8 *)ev) + sizeof(*ev);
1001	if (params->ap_mld_addr) {
1002		ev->cr.ap_mld_addr = next;
1003		memcpy((void *)ev->cr.ap_mld_addr, params->ap_mld_addr,
1004		       ETH_ALEN);
1005		next += ETH_ALEN;
1006	}
1007	if (params->req_ie_len) {
1008		ev->cr.req_ie = next;
1009		ev->cr.req_ie_len = params->req_ie_len;
1010		memcpy((void *)ev->cr.req_ie, params->req_ie,
1011		       params->req_ie_len);
1012		next += params->req_ie_len;
1013	}
1014	if (params->resp_ie_len) {
1015		ev->cr.resp_ie = next;
1016		ev->cr.resp_ie_len = params->resp_ie_len;
1017		memcpy((void *)ev->cr.resp_ie, params->resp_ie,
1018		       params->resp_ie_len);
1019		next += params->resp_ie_len;
1020	}
1021	if (params->fils.kek_len) {
1022		ev->cr.fils.kek = next;
1023		ev->cr.fils.kek_len = params->fils.kek_len;
1024		memcpy((void *)ev->cr.fils.kek, params->fils.kek,
1025		       params->fils.kek_len);
1026		next += params->fils.kek_len;
1027	}
1028	if (params->fils.pmk_len) {
1029		ev->cr.fils.pmk = next;
1030		ev->cr.fils.pmk_len = params->fils.pmk_len;
1031		memcpy((void *)ev->cr.fils.pmk, params->fils.pmk,
1032		       params->fils.pmk_len);
1033		next += params->fils.pmk_len;
1034	}
1035	if (params->fils.pmkid) {
1036		ev->cr.fils.pmkid = next;
1037		memcpy((void *)ev->cr.fils.pmkid, params->fils.pmkid,
1038		       WLAN_PMKID_LEN);
1039		next += WLAN_PMKID_LEN;
1040	}
1041	ev->cr.fils.update_erp_next_seq_num = params->fils.update_erp_next_seq_num;
1042	if (params->fils.update_erp_next_seq_num)
1043		ev->cr.fils.erp_next_seq_num = params->fils.erp_next_seq_num;
1044	ev->cr.valid_links = params->valid_links;
1045	for_each_valid_link(params, link) {
1046		if (params->links[link].bss)
1047			cfg80211_hold_bss(
1048				bss_from_pub(params->links[link].bss));
1049		ev->cr.links[link].bss = params->links[link].bss;
1050		ev->cr.links[link].status = params->links[link].status;
1051
1052		if (params->links[link].addr) {
1053			ev->cr.links[link].addr = next;
1054			memcpy((void *)ev->cr.links[link].addr,
1055			       params->links[link].addr,
1056			       ETH_ALEN);
1057			next += ETH_ALEN;
1058		}
1059		if (params->links[link].bssid) {
1060			ev->cr.links[link].bssid = next;
1061			memcpy((void *)ev->cr.links[link].bssid,
1062			       params->links[link].bssid,
1063			       ETH_ALEN);
1064			next += ETH_ALEN;
1065		}
1066	}
1067	ev->cr.status = params->status;
1068	ev->cr.timeout_reason = params->timeout_reason;
1069
1070	spin_lock_irqsave(&wdev->event_lock, flags);
1071	list_add_tail(&ev->list, &wdev->event_list);
1072	spin_unlock_irqrestore(&wdev->event_lock, flags);
1073	queue_work(cfg80211_wq, &rdev->event_work);
1074}
1075EXPORT_SYMBOL(cfg80211_connect_done);
1076
1077/* Consumes bss object one way or another */
1078void __cfg80211_roamed(struct wireless_dev *wdev,
1079		       struct cfg80211_roam_info *info)
 
 
 
1080{
 
1081#ifdef CONFIG_CFG80211_WEXT
1082	union iwreq_data wrqu;
1083#endif
1084	unsigned int link;
1085	const u8 *connected_addr;
1086
1087	lockdep_assert_wiphy(wdev->wiphy);
1088
1089	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1090		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
1091		goto out;
1092
1093	if (WARN_ON(!wdev->connected))
1094		goto out;
1095
1096	if (info->valid_links) {
1097		if (WARN_ON(!info->ap_mld_addr))
1098			goto out;
1099
1100		for_each_valid_link(info, link) {
1101			if (WARN_ON(!info->links[link].addr))
1102				goto out;
1103		}
1104	}
1105
1106	cfg80211_wdev_release_bsses(wdev);
 
 
1107
1108	for_each_valid_link(info, link) {
1109		if (WARN_ON(!info->links[link].bss))
1110			goto out;
1111	}
1112
1113	memset(wdev->links, 0, sizeof(wdev->links));
1114	wdev->valid_links = info->valid_links;
1115	for_each_valid_link(info, link) {
1116		cfg80211_hold_bss(bss_from_pub(info->links[link].bss));
1117		wdev->links[link].client.current_bss =
1118			bss_from_pub(info->links[link].bss);
1119	}
1120
1121	connected_addr = info->valid_links ?
1122			 info->ap_mld_addr :
1123			 info->links[0].bss->bssid;
1124	ether_addr_copy(wdev->u.client.connected_addr, connected_addr);
1125	if (info->valid_links) {
1126		for_each_valid_link(info, link)
1127			memcpy(wdev->links[link].addr, info->links[link].addr,
1128			       ETH_ALEN);
1129	}
1130	wdev->unprot_beacon_reported = 0;
1131	nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
1132			    wdev->netdev, info, GFP_KERNEL);
1133
1134#ifdef CONFIG_CFG80211_WEXT
1135	if (!info->valid_links) {
1136		if (info->req_ie) {
1137			memset(&wrqu, 0, sizeof(wrqu));
1138			wrqu.data.length = info->req_ie_len;
1139			wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
1140					    &wrqu, info->req_ie);
1141		}
1142
1143		if (info->resp_ie) {
1144			memset(&wrqu, 0, sizeof(wrqu));
1145			wrqu.data.length = info->resp_ie_len;
1146			wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
1147					    &wrqu, info->resp_ie);
1148		}
1149
 
 
1150		memset(&wrqu, 0, sizeof(wrqu));
1151		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1152		memcpy(wrqu.ap_addr.sa_data, connected_addr, ETH_ALEN);
1153		memcpy(wdev->wext.prev_bssid, connected_addr, ETH_ALEN);
1154		wdev->wext.prev_bssid_valid = true;
1155		wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
1156	}
1157#endif
1158
1159	return;
1160out:
1161	for_each_valid_link(info, link)
1162		cfg80211_put_bss(wdev->wiphy, info->links[link].bss);
1163}
1164
1165/* Consumes info->links.bss object(s) one way or another */
1166void cfg80211_roamed(struct net_device *dev, struct cfg80211_roam_info *info,
1167		     gfp_t gfp)
1168{
1169	struct wireless_dev *wdev = dev->ieee80211_ptr;
1170	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1171	struct cfg80211_event *ev;
1172	unsigned long flags;
1173	u8 *next;
1174	unsigned int link;
1175	size_t link_info_size = 0;
1176	bool bss_not_found = false;
1177
1178	for_each_valid_link(info, link) {
1179		link_info_size += info->links[link].addr ? ETH_ALEN : 0;
1180		link_info_size += info->links[link].bssid ? ETH_ALEN : 0;
1181
1182		if (info->links[link].bss)
1183			continue;
1184
1185		info->links[link].bss =
1186			cfg80211_get_bss(wdev->wiphy,
1187					 info->links[link].channel,
1188					 info->links[link].bssid,
1189					 wdev->u.client.ssid,
1190					 wdev->u.client.ssid_len,
1191					 wdev->conn_bss_type,
1192					 IEEE80211_PRIVACY_ANY);
1193
1194		if (!info->links[link].bss) {
1195			bss_not_found = true;
1196			break;
1197		}
1198	}
1199
1200	if (WARN_ON(bss_not_found))
1201		goto out;
1202
1203	ev = kzalloc(sizeof(*ev) + info->req_ie_len + info->resp_ie_len +
1204		     info->fils.kek_len + info->fils.pmk_len +
1205		     (info->fils.pmkid ? WLAN_PMKID_LEN : 0) +
1206		     (info->ap_mld_addr ? ETH_ALEN : 0) + link_info_size, gfp);
1207	if (!ev)
1208		goto out;
1209
1210	ev->type = EVENT_ROAMED;
1211	next = ((u8 *)ev) + sizeof(*ev);
1212	if (info->req_ie_len) {
1213		ev->rm.req_ie = next;
1214		ev->rm.req_ie_len = info->req_ie_len;
1215		memcpy((void *)ev->rm.req_ie, info->req_ie, info->req_ie_len);
1216		next += info->req_ie_len;
1217	}
1218	if (info->resp_ie_len) {
1219		ev->rm.resp_ie = next;
1220		ev->rm.resp_ie_len = info->resp_ie_len;
1221		memcpy((void *)ev->rm.resp_ie, info->resp_ie,
1222		       info->resp_ie_len);
1223		next += info->resp_ie_len;
1224	}
1225	if (info->fils.kek_len) {
1226		ev->rm.fils.kek = next;
1227		ev->rm.fils.kek_len = info->fils.kek_len;
1228		memcpy((void *)ev->rm.fils.kek, info->fils.kek,
1229		       info->fils.kek_len);
1230		next += info->fils.kek_len;
1231	}
1232	if (info->fils.pmk_len) {
1233		ev->rm.fils.pmk = next;
1234		ev->rm.fils.pmk_len = info->fils.pmk_len;
1235		memcpy((void *)ev->rm.fils.pmk, info->fils.pmk,
1236		       info->fils.pmk_len);
1237		next += info->fils.pmk_len;
1238	}
1239	if (info->fils.pmkid) {
1240		ev->rm.fils.pmkid = next;
1241		memcpy((void *)ev->rm.fils.pmkid, info->fils.pmkid,
1242		       WLAN_PMKID_LEN);
1243		next += WLAN_PMKID_LEN;
1244	}
1245	ev->rm.fils.update_erp_next_seq_num = info->fils.update_erp_next_seq_num;
1246	if (info->fils.update_erp_next_seq_num)
1247		ev->rm.fils.erp_next_seq_num = info->fils.erp_next_seq_num;
1248	if (info->ap_mld_addr) {
1249		ev->rm.ap_mld_addr = next;
1250		memcpy((void *)ev->rm.ap_mld_addr, info->ap_mld_addr,
1251		       ETH_ALEN);
1252		next += ETH_ALEN;
1253	}
1254	ev->rm.valid_links = info->valid_links;
1255	for_each_valid_link(info, link) {
1256		ev->rm.links[link].bss = info->links[link].bss;
1257
1258		if (info->links[link].addr) {
1259			ev->rm.links[link].addr = next;
1260			memcpy((void *)ev->rm.links[link].addr,
1261			       info->links[link].addr,
1262			       ETH_ALEN);
1263			next += ETH_ALEN;
1264		}
1265
1266		if (info->links[link].bssid) {
1267			ev->rm.links[link].bssid = next;
1268			memcpy((void *)ev->rm.links[link].bssid,
1269			       info->links[link].bssid,
1270			       ETH_ALEN);
1271			next += ETH_ALEN;
1272		}
1273	}
1274
1275	spin_lock_irqsave(&wdev->event_lock, flags);
1276	list_add_tail(&ev->list, &wdev->event_list);
1277	spin_unlock_irqrestore(&wdev->event_lock, flags);
1278	queue_work(cfg80211_wq, &rdev->event_work);
1279
1280	return;
1281out:
1282	for_each_valid_link(info, link)
1283		cfg80211_put_bss(wdev->wiphy, info->links[link].bss);
1284
1285}
1286EXPORT_SYMBOL(cfg80211_roamed);
1287
1288void __cfg80211_port_authorized(struct wireless_dev *wdev, const u8 *peer_addr,
1289					const u8 *td_bitmap, u8 td_bitmap_len)
1290{
1291	lockdep_assert_wiphy(wdev->wiphy);
1292
1293	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1294		wdev->iftype != NL80211_IFTYPE_P2P_CLIENT &&
1295		wdev->iftype != NL80211_IFTYPE_AP &&
1296		wdev->iftype != NL80211_IFTYPE_P2P_GO))
1297		return;
1298
1299	if (wdev->iftype == NL80211_IFTYPE_STATION ||
1300		wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) {
1301		if (WARN_ON(!wdev->connected) ||
1302			WARN_ON(!ether_addr_equal(wdev->u.client.connected_addr, peer_addr)))
1303			return;
1304	}
1305
1306	nl80211_send_port_authorized(wiphy_to_rdev(wdev->wiphy), wdev->netdev,
1307				     peer_addr, td_bitmap, td_bitmap_len);
 
 
 
 
 
1308}
1309
1310void cfg80211_port_authorized(struct net_device *dev, const u8 *peer_addr,
1311			      const u8 *td_bitmap, u8 td_bitmap_len, gfp_t gfp)
 
 
 
1312{
1313	struct wireless_dev *wdev = dev->ieee80211_ptr;
1314	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1315	struct cfg80211_event *ev;
1316	unsigned long flags;
1317
1318	if (WARN_ON(!peer_addr))
1319		return;
1320
1321	ev = kzalloc(sizeof(*ev) + td_bitmap_len, gfp);
1322	if (!ev)
1323		return;
1324
1325	ev->type = EVENT_PORT_AUTHORIZED;
1326	memcpy(ev->pa.peer_addr, peer_addr, ETH_ALEN);
1327	ev->pa.td_bitmap = ((u8 *)ev) + sizeof(*ev);
1328	ev->pa.td_bitmap_len = td_bitmap_len;
1329	memcpy((void *)ev->pa.td_bitmap, td_bitmap, td_bitmap_len);
 
 
 
 
1330
1331	/*
1332	 * Use the wdev event list so that if there are pending
1333	 * connected/roamed events, they will be reported first.
1334	 */
1335	spin_lock_irqsave(&wdev->event_lock, flags);
1336	list_add_tail(&ev->list, &wdev->event_list);
1337	spin_unlock_irqrestore(&wdev->event_lock, flags);
1338	queue_work(cfg80211_wq, &rdev->event_work);
1339}
1340EXPORT_SYMBOL(cfg80211_port_authorized);
1341
1342void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
1343			     size_t ie_len, u16 reason, bool from_ap)
1344{
1345	struct wireless_dev *wdev = dev->ieee80211_ptr;
1346	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1347	int i;
1348#ifdef CONFIG_CFG80211_WEXT
1349	union iwreq_data wrqu;
1350#endif
1351
1352	lockdep_assert_wiphy(wdev->wiphy);
1353
1354	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1355		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
1356		return;
1357
1358	cfg80211_wdev_release_bsses(wdev);
1359	wdev->valid_links = 0;
1360	wdev->connected = false;
1361	wdev->u.client.ssid_len = 0;
1362	wdev->conn_owner_nlportid = 0;
1363	kfree_sensitive(wdev->connect_keys);
1364	wdev->connect_keys = NULL;
1365
1366	nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1367
1368	/* stop critical protocol if supported */
1369	if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
1370		rdev->crit_proto_nlportid = 0;
1371		rdev_crit_proto_stop(rdev, wdev);
 
 
 
 
 
 
 
 
 
 
 
 
 
1372	}
1373
 
 
1374	/*
1375	 * Delete all the keys ... pairwise keys can't really
1376	 * exist any more anyway, but default keys might.
1377	 */
1378	if (rdev->ops->del_key) {
1379		int max_key_idx = 5;
1380
1381		if (wiphy_ext_feature_isset(
1382			    wdev->wiphy,
1383			    NL80211_EXT_FEATURE_BEACON_PROTECTION) ||
1384		    wiphy_ext_feature_isset(
1385			    wdev->wiphy,
1386			    NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT))
1387			max_key_idx = 7;
1388		for (i = 0; i <= max_key_idx; i++)
1389			rdev_del_key(rdev, dev, -1, i, false, NULL);
1390	}
1391
1392	rdev_set_qos_map(rdev, dev, NULL);
1393
1394#ifdef CONFIG_CFG80211_WEXT
1395	memset(&wrqu, 0, sizeof(wrqu));
1396	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1397	wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
1398	wdev->wext.connect.ssid_len = 0;
1399#endif
1400
1401	schedule_work(&cfg80211_disconnect_work);
1402
1403	cfg80211_schedule_channels_check(wdev);
1404}
1405
1406void cfg80211_disconnected(struct net_device *dev, u16 reason,
1407			   const u8 *ie, size_t ie_len,
1408			   bool locally_generated, gfp_t gfp)
1409{
1410	struct wireless_dev *wdev = dev->ieee80211_ptr;
1411	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1412	struct cfg80211_event *ev;
1413	unsigned long flags;
1414
 
 
1415	ev = kzalloc(sizeof(*ev) + ie_len, gfp);
1416	if (!ev)
1417		return;
1418
1419	ev->type = EVENT_DISCONNECTED;
1420	ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
1421	ev->dc.ie_len = ie_len;
1422	memcpy((void *)ev->dc.ie, ie, ie_len);
1423	ev->dc.reason = reason;
1424	ev->dc.locally_generated = locally_generated;
1425
1426	spin_lock_irqsave(&wdev->event_lock, flags);
1427	list_add_tail(&ev->list, &wdev->event_list);
1428	spin_unlock_irqrestore(&wdev->event_lock, flags);
1429	queue_work(cfg80211_wq, &rdev->event_work);
1430}
1431EXPORT_SYMBOL(cfg80211_disconnected);
1432
1433/*
1434 * API calls for nl80211/wext compatibility code
1435 */
1436int cfg80211_connect(struct cfg80211_registered_device *rdev,
1437		     struct net_device *dev,
1438		     struct cfg80211_connect_params *connect,
1439		     struct cfg80211_cached_keys *connkeys,
1440		     const u8 *prev_bssid)
1441{
1442	struct wireless_dev *wdev = dev->ieee80211_ptr;
 
1443	int err;
1444
1445	lockdep_assert_wiphy(wdev->wiphy);
1446
1447	/*
1448	 * If we have an ssid_len, we're trying to connect or are
1449	 * already connected, so reject a new SSID unless it's the
1450	 * same (which is the case for re-association.)
1451	 */
1452	if (wdev->u.client.ssid_len &&
1453	    (wdev->u.client.ssid_len != connect->ssid_len ||
1454	     memcmp(wdev->u.client.ssid, connect->ssid, wdev->u.client.ssid_len)))
1455		return -EALREADY;
1456
1457	/*
1458	 * If connected, reject (re-)association unless prev_bssid
1459	 * matches the current BSSID.
1460	 */
1461	if (wdev->connected) {
1462		if (!prev_bssid)
1463			return -EALREADY;
1464		if (!ether_addr_equal(prev_bssid,
1465				      wdev->u.client.connected_addr))
1466			return -ENOTCONN;
1467	}
1468
1469	/*
1470	 * Reject if we're in the process of connecting with WEP,
1471	 * this case isn't very interesting and trying to handle
1472	 * it would make the code much more complex.
1473	 */
1474	if (wdev->connect_keys)
1475		return -EINPROGRESS;
1476
1477	cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
1478				  rdev->wiphy.ht_capa_mod_mask);
1479	cfg80211_oper_and_vht_capa(&connect->vht_capa_mask,
1480				   rdev->wiphy.vht_capa_mod_mask);
1481
1482	if (connkeys && connkeys->def >= 0) {
1483		int idx;
1484		u32 cipher;
1485
1486		idx = connkeys->def;
1487		cipher = connkeys->params[idx].cipher;
1488		/* If given a WEP key we may need it for shared key auth */
1489		if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1490		    cipher == WLAN_CIPHER_SUITE_WEP104) {
1491			connect->key_idx = idx;
1492			connect->key = connkeys->params[idx].key;
1493			connect->key_len = connkeys->params[idx].key_len;
1494
1495			/*
1496			 * If ciphers are not set (e.g. when going through
1497			 * iwconfig), we have to set them appropriately here.
1498			 */
1499			if (connect->crypto.cipher_group == 0)
1500				connect->crypto.cipher_group = cipher;
1501
1502			if (connect->crypto.n_ciphers_pairwise == 0) {
1503				connect->crypto.n_ciphers_pairwise = 1;
1504				connect->crypto.ciphers_pairwise[0] = cipher;
1505			}
1506		}
1507	} else {
1508		if (WARN_ON(connkeys))
1509			return -EINVAL;
1510
1511		/* connect can point to wdev->wext.connect which
1512		 * can hold key data from a previous connection
1513		 */
1514		connect->key = NULL;
1515		connect->key_len = 0;
1516		connect->key_idx = 0;
1517	}
1518
1519	wdev->connect_keys = connkeys;
1520	memcpy(wdev->u.client.ssid, connect->ssid, connect->ssid_len);
1521	wdev->u.client.ssid_len = connect->ssid_len;
1522
1523	wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
1524					      IEEE80211_BSS_TYPE_ESS;
1525
1526	if (!rdev->ops->connect)
1527		err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1528	else
1529		err = rdev_connect(rdev, dev, connect);
1530
1531	if (err) {
1532		wdev->connect_keys = NULL;
1533		/*
1534		 * This could be reassoc getting refused, don't clear
1535		 * ssid_len in that case.
1536		 */
1537		if (!wdev->connected)
1538			wdev->u.client.ssid_len = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1539		return err;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1540	}
 
1541
1542	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
1543}
1544
1545int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1546			struct net_device *dev, u16 reason, bool wextev)
1547{
1548	struct wireless_dev *wdev = dev->ieee80211_ptr;
1549	int err = 0;
1550
1551	lockdep_assert_wiphy(wdev->wiphy);
1552
1553	kfree_sensitive(wdev->connect_keys);
 
 
 
1554	wdev->connect_keys = NULL;
1555
1556	wdev->conn_owner_nlportid = 0;
 
 
1557
1558	if (wdev->conn)
1559		err = cfg80211_sme_disconnect(wdev, reason);
1560	else if (!rdev->ops->disconnect)
1561		cfg80211_mlme_down(rdev, dev);
1562	else if (wdev->u.client.ssid_len)
1563		err = rdev_disconnect(rdev, dev, reason);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1564
1565	/*
1566	 * Clear ssid_len unless we actually were fully connected,
1567	 * in which case cfg80211_disconnected() will take care of
1568	 * this later.
1569	 */
1570	if (!wdev->connected)
1571		wdev->u.client.ssid_len = 0;
 
 
 
 
 
 
 
 
 
 
 
 
1572
1573	return err;
1574}
1575
1576/*
1577 * Used to clean up after the connection / connection attempt owner socket
1578 * disconnects
1579 */
1580void cfg80211_autodisconnect_wk(struct work_struct *work)
1581{
1582	struct wireless_dev *wdev =
1583		container_of(work, struct wireless_dev, disconnect_wk);
1584	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1585
1586	wiphy_lock(wdev->wiphy);
1587
1588	if (wdev->conn_owner_nlportid) {
1589		switch (wdev->iftype) {
1590		case NL80211_IFTYPE_ADHOC:
1591			cfg80211_leave_ibss(rdev, wdev->netdev, false);
1592			break;
1593		case NL80211_IFTYPE_AP:
1594		case NL80211_IFTYPE_P2P_GO:
1595			cfg80211_stop_ap(rdev, wdev->netdev, -1, false);
1596			break;
1597		case NL80211_IFTYPE_MESH_POINT:
1598			cfg80211_leave_mesh(rdev, wdev->netdev);
1599			break;
1600		case NL80211_IFTYPE_STATION:
1601		case NL80211_IFTYPE_P2P_CLIENT:
1602			/*
1603			 * Use disconnect_bssid if still connecting and
1604			 * ops->disconnect not implemented.  Otherwise we can
1605			 * use cfg80211_disconnect.
1606			 */
1607			if (rdev->ops->disconnect || wdev->connected)
1608				cfg80211_disconnect(rdev, wdev->netdev,
1609						    WLAN_REASON_DEAUTH_LEAVING,
1610						    true);
1611			else
1612				cfg80211_mlme_deauth(rdev, wdev->netdev,
1613						     wdev->disconnect_bssid,
1614						     NULL, 0,
1615						     WLAN_REASON_DEAUTH_LEAVING,
1616						     false);
1617			break;
1618		default:
1619			break;
1620		}
1621	}
1622
1623	wiphy_unlock(wdev->wiphy);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1624}
v3.1
 
   1/*
   2 * SME code for cfg80211's connect emulation.
 
 
   3 *
   4 * Copyright 2009	Johannes Berg <johannes@sipsolutions.net>
   5 * Copyright (C) 2009   Intel Corporation. All rights reserved.
 
   6 */
   7
   8#include <linux/etherdevice.h>
   9#include <linux/if_arp.h>
  10#include <linux/slab.h>
  11#include <linux/workqueue.h>
  12#include <linux/wireless.h>
 
  13#include <net/iw_handler.h>
  14#include <net/cfg80211.h>
  15#include <net/rtnetlink.h>
  16#include "nl80211.h"
  17#include "reg.h"
 
 
 
 
 
 
 
  18
  19struct cfg80211_conn {
  20	struct cfg80211_connect_params params;
  21	/* these are sub-states of the _CONNECTING sme_state */
  22	enum {
  23		CFG80211_CONN_IDLE,
  24		CFG80211_CONN_SCANNING,
  25		CFG80211_CONN_SCAN_AGAIN,
  26		CFG80211_CONN_AUTHENTICATE_NEXT,
  27		CFG80211_CONN_AUTHENTICATING,
 
  28		CFG80211_CONN_ASSOCIATE_NEXT,
  29		CFG80211_CONN_ASSOCIATING,
  30		CFG80211_CONN_DEAUTH_ASSOC_FAIL,
 
 
 
 
  31	} state;
  32	u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
  33	u8 *ie;
  34	size_t ie_len;
  35	bool auto_auth, prev_bssid_valid;
  36};
  37
  38static bool cfg80211_is_all_idle(void)
  39{
  40	struct cfg80211_registered_device *rdev;
  41	struct wireless_dev *wdev;
  42	bool is_all_idle = true;
  43
  44	mutex_lock(&cfg80211_mutex);
  45
  46	/*
  47	 * All devices must be idle as otherwise if you are actively
  48	 * scanning some new beacon hints could be learned and would
  49	 * count as new regulatory hints.
  50	 */
  51	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  52		cfg80211_lock_rdev(rdev);
  53		list_for_each_entry(wdev, &rdev->netdev_list, list) {
  54			wdev_lock(wdev);
  55			if (wdev->sme_state != CFG80211_SME_IDLE)
  56				is_all_idle = false;
  57			wdev_unlock(wdev);
  58		}
  59		cfg80211_unlock_rdev(rdev);
  60	}
  61
  62	mutex_unlock(&cfg80211_mutex);
  63
  64	return is_all_idle;
  65}
  66
  67static void disconnect_work(struct work_struct *work)
  68{
  69	if (!cfg80211_is_all_idle())
  70		return;
  71
  72	regulatory_hint_disconnect();
 
 
  73}
  74
  75static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
  76
  77static int cfg80211_conn_scan(struct wireless_dev *wdev)
  78{
  79	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  80	struct cfg80211_scan_request *request;
  81	int n_channels, err;
  82
  83	ASSERT_RTNL();
  84	ASSERT_RDEV_LOCK(rdev);
  85	ASSERT_WDEV_LOCK(wdev);
  86
  87	if (rdev->scan_req)
  88		return -EBUSY;
  89
  90	if (wdev->conn->params.channel) {
  91		n_channels = 1;
  92	} else {
  93		enum ieee80211_band band;
  94		n_channels = 0;
  95
  96		for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  97			if (!wdev->wiphy->bands[band])
  98				continue;
  99			n_channels += wdev->wiphy->bands[band]->n_channels;
 100		}
 101	}
 102	request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
 103			  sizeof(request->channels[0]) * n_channels,
 104			  GFP_KERNEL);
 105	if (!request)
 106		return -ENOMEM;
 107
 108	if (wdev->conn->params.channel)
 
 
 
 
 
 
 
 
 
 109		request->channels[0] = wdev->conn->params.channel;
 110	else {
 
 111		int i = 0, j;
 112		enum ieee80211_band band;
 113
 114		for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
 115			if (!wdev->wiphy->bands[band])
 
 
 
 116				continue;
 117			for (j = 0; j < wdev->wiphy->bands[band]->n_channels;
 118			     i++, j++)
 119				request->channels[i] =
 120					&wdev->wiphy->bands[band]->channels[j];
 121			request->rates[band] =
 122				(1 << wdev->wiphy->bands[band]->n_bitrates) - 1;
 
 123		}
 
 124	}
 125	request->n_channels = n_channels;
 126	request->ssids = (void *)&request->channels[n_channels];
 
 127	request->n_ssids = 1;
 128
 129	memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
 130		wdev->conn->params.ssid_len);
 131	request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
 132
 133	request->dev = wdev->netdev;
 
 
 134	request->wiphy = &rdev->wiphy;
 
 135
 136	rdev->scan_req = request;
 137
 138	err = rdev->ops->scan(wdev->wiphy, wdev->netdev, request);
 139	if (!err) {
 140		wdev->conn->state = CFG80211_CONN_SCANNING;
 141		nl80211_send_scan_start(rdev, wdev->netdev);
 142		dev_hold(wdev->netdev);
 143	} else {
 144		rdev->scan_req = NULL;
 145		kfree(request);
 146	}
 147	return err;
 148}
 149
 150static int cfg80211_conn_do_work(struct wireless_dev *wdev)
 
 151{
 152	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
 153	struct cfg80211_connect_params *params;
 154	const u8 *prev_bssid = NULL;
 
 155	int err;
 156
 157	ASSERT_WDEV_LOCK(wdev);
 158
 159	if (!wdev->conn)
 160		return 0;
 161
 162	params = &wdev->conn->params;
 163
 164	switch (wdev->conn->state) {
 
 
 
 165	case CFG80211_CONN_SCAN_AGAIN:
 166		return cfg80211_conn_scan(wdev);
 167	case CFG80211_CONN_AUTHENTICATE_NEXT:
 168		BUG_ON(!rdev->ops->auth);
 
 169		wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
 170		return __cfg80211_mlme_auth(rdev, wdev->netdev,
 171					    params->channel, params->auth_type,
 172					    params->bssid,
 173					    params->ssid, params->ssid_len,
 174					    NULL, 0,
 175					    params->key, params->key_len,
 176					    params->key_idx, false);
 
 
 
 
 
 
 
 
 
 177	case CFG80211_CONN_ASSOCIATE_NEXT:
 178		BUG_ON(!rdev->ops->assoc);
 
 179		wdev->conn->state = CFG80211_CONN_ASSOCIATING;
 180		if (wdev->conn->prev_bssid_valid)
 181			prev_bssid = wdev->conn->prev_bssid;
 182		err = __cfg80211_mlme_assoc(rdev, wdev->netdev,
 183					    params->channel, params->bssid,
 184					    prev_bssid,
 185					    params->ssid, params->ssid_len,
 186					    params->ie, params->ie_len,
 187					    false, &params->crypto);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 188		if (err)
 189			__cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
 190					       NULL, 0,
 191					       WLAN_REASON_DEAUTH_LEAVING,
 192					       false);
 193		return err;
 194	case CFG80211_CONN_DEAUTH_ASSOC_FAIL:
 195		__cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
 196				       NULL, 0,
 197				       WLAN_REASON_DEAUTH_LEAVING, false);
 198		/* return an error so that we call __cfg80211_connect_result() */
 199		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 200	default:
 201		return 0;
 202	}
 203}
 204
 205void cfg80211_conn_work(struct work_struct *work)
 206{
 207	struct cfg80211_registered_device *rdev =
 208		container_of(work, struct cfg80211_registered_device, conn_work);
 209	struct wireless_dev *wdev;
 210	u8 bssid_buf[ETH_ALEN], *bssid = NULL;
 
 
 
 211
 212	rtnl_lock();
 213	cfg80211_lock_rdev(rdev);
 214	mutex_lock(&rdev->devlist_mtx);
 215
 216	list_for_each_entry(wdev, &rdev->netdev_list, list) {
 217		wdev_lock(wdev);
 218		if (!netif_running(wdev->netdev)) {
 219			wdev_unlock(wdev);
 220			continue;
 221		}
 222		if (wdev->sme_state != CFG80211_SME_CONNECTING) {
 223			wdev_unlock(wdev);
 224			continue;
 225		}
 226		if (wdev->conn->params.bssid) {
 227			memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
 228			bssid = bssid_buf;
 229		}
 230		if (cfg80211_conn_do_work(wdev))
 231			__cfg80211_connect_result(
 232					wdev->netdev, bssid,
 233					NULL, 0, NULL, 0,
 234					WLAN_STATUS_UNSPECIFIED_FAILURE,
 235					false, NULL);
 236		wdev_unlock(wdev);
 
 
 
 237	}
 238
 239	mutex_unlock(&rdev->devlist_mtx);
 240	cfg80211_unlock_rdev(rdev);
 241	rtnl_unlock();
 
 
 
 
 
 
 
 242}
 243
 
 244static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
 245{
 246	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
 247	struct cfg80211_bss *bss;
 248	u16 capa = WLAN_CAPABILITY_ESS;
 249
 250	ASSERT_WDEV_LOCK(wdev);
 251
 252	if (wdev->conn->params.privacy)
 253		capa |= WLAN_CAPABILITY_PRIVACY;
 254
 255	bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
 256			       wdev->conn->params.bssid,
 257			       wdev->conn->params.ssid,
 258			       wdev->conn->params.ssid_len,
 259			       WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY,
 260			       capa);
 261	if (!bss)
 262		return NULL;
 263
 264	memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
 265	wdev->conn->params.bssid = wdev->conn->bssid;
 266	wdev->conn->params.channel = bss->channel;
 267	wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
 268	schedule_work(&rdev->conn_work);
 269
 270	return bss;
 271}
 272
 273static void __cfg80211_sme_scan_done(struct net_device *dev)
 274{
 275	struct wireless_dev *wdev = dev->ieee80211_ptr;
 276	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
 277	struct cfg80211_bss *bss;
 278
 279	ASSERT_WDEV_LOCK(wdev);
 280
 281	if (wdev->sme_state != CFG80211_SME_CONNECTING)
 282		return;
 283
 284	if (!wdev->conn)
 285		return;
 286
 287	if (wdev->conn->state != CFG80211_CONN_SCANNING &&
 288	    wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
 289		return;
 290
 291	bss = cfg80211_get_conn_bss(wdev);
 292	if (bss) {
 293		cfg80211_put_bss(bss);
 294	} else {
 295		/* not found */
 296		if (wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)
 297			schedule_work(&rdev->conn_work);
 298		else
 299			__cfg80211_connect_result(
 300					wdev->netdev,
 301					wdev->conn->params.bssid,
 302					NULL, 0, NULL, 0,
 303					WLAN_STATUS_UNSPECIFIED_FAILURE,
 304					false, NULL);
 305	}
 306}
 307
 308void cfg80211_sme_scan_done(struct net_device *dev)
 309{
 310	struct wireless_dev *wdev = dev->ieee80211_ptr;
 311
 312	mutex_lock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
 313	wdev_lock(wdev);
 314	__cfg80211_sme_scan_done(dev);
 315	wdev_unlock(wdev);
 316	mutex_unlock(&wiphy_to_dev(wdev->wiphy)->devlist_mtx);
 317}
 318
 319void cfg80211_sme_rx_auth(struct net_device *dev,
 320			  const u8 *buf, size_t len)
 321{
 322	struct wireless_dev *wdev = dev->ieee80211_ptr;
 323	struct wiphy *wiphy = wdev->wiphy;
 324	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
 325	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
 326	u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
 327
 328	ASSERT_WDEV_LOCK(wdev);
 329
 330	/* should only RX auth frames when connecting */
 331	if (wdev->sme_state != CFG80211_SME_CONNECTING)
 332		return;
 333
 334	if (WARN_ON(!wdev->conn))
 335		return;
 336
 337	if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
 338	    wdev->conn->auto_auth &&
 339	    wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
 340		/* select automatically between only open, shared, leap */
 341		switch (wdev->conn->params.auth_type) {
 342		case NL80211_AUTHTYPE_OPEN_SYSTEM:
 343			if (wdev->connect_keys)
 344				wdev->conn->params.auth_type =
 345					NL80211_AUTHTYPE_SHARED_KEY;
 346			else
 347				wdev->conn->params.auth_type =
 348					NL80211_AUTHTYPE_NETWORK_EAP;
 349			break;
 350		case NL80211_AUTHTYPE_SHARED_KEY:
 351			wdev->conn->params.auth_type =
 352				NL80211_AUTHTYPE_NETWORK_EAP;
 353			break;
 354		default:
 355			/* huh? */
 356			wdev->conn->params.auth_type =
 357				NL80211_AUTHTYPE_OPEN_SYSTEM;
 358			break;
 359		}
 360		wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
 361		schedule_work(&rdev->conn_work);
 362	} else if (status_code != WLAN_STATUS_SUCCESS) {
 363		__cfg80211_connect_result(dev, mgmt->bssid, NULL, 0, NULL, 0,
 364					  status_code, false, NULL);
 365	} else if (wdev->sme_state == CFG80211_SME_CONNECTING &&
 366		 wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
 
 
 
 
 367		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
 368		schedule_work(&rdev->conn_work);
 369	}
 370}
 371
 372bool cfg80211_sme_failed_reassoc(struct wireless_dev *wdev)
 373{
 374	struct wiphy *wiphy = wdev->wiphy;
 375	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
 376
 377	if (WARN_ON(!wdev->conn))
 378		return false;
 379
 380	if (!wdev->conn->prev_bssid_valid)
 
 381		return false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 382
 383	/*
 384	 * Some stupid APs don't accept reassoc, so we
 385	 * need to fall back to trying regular assoc.
 386	 */
 387	wdev->conn->prev_bssid_valid = false;
 388	wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
 389	schedule_work(&rdev->conn_work);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 390
 391	return true;
 
 
 
 
 
 392}
 393
 394void cfg80211_sme_failed_assoc(struct wireless_dev *wdev)
 
 
 
 
 395{
 396	struct wiphy *wiphy = wdev->wiphy;
 397	struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
 398
 399	wdev->conn->state = CFG80211_CONN_DEAUTH_ASSOC_FAIL;
 400	schedule_work(&rdev->conn_work);
 
 
 
 
 401}
 402
 403void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
 404			       const u8 *req_ie, size_t req_ie_len,
 405			       const u8 *resp_ie, size_t resp_ie_len,
 406			       u16 status, bool wextev,
 407			       struct cfg80211_bss *bss)
 
 
 
 
 408{
 409	struct wireless_dev *wdev = dev->ieee80211_ptr;
 410	u8 *country_ie;
 
 
 
 411#ifdef CONFIG_CFG80211_WEXT
 412	union iwreq_data wrqu;
 413#endif
 
 
 
 414
 415	ASSERT_WDEV_LOCK(wdev);
 416
 417	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
 418		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
 419		return;
 
 
 
 
 
 
 
 
 
 420
 421	if (wdev->sme_state != CFG80211_SME_CONNECTING)
 422		return;
 
 423
 424	nl80211_send_connect_result(wiphy_to_dev(wdev->wiphy), dev,
 425				    bssid, req_ie, req_ie_len,
 426				    resp_ie, resp_ie_len,
 427				    status, GFP_KERNEL);
 428
 429#ifdef CONFIG_CFG80211_WEXT
 430	if (wextev) {
 431		if (req_ie && status == WLAN_STATUS_SUCCESS) {
 432			memset(&wrqu, 0, sizeof(wrqu));
 433			wrqu.data.length = req_ie_len;
 434			wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
 
 435		}
 436
 437		if (resp_ie && status == WLAN_STATUS_SUCCESS) {
 438			memset(&wrqu, 0, sizeof(wrqu));
 439			wrqu.data.length = resp_ie_len;
 440			wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
 
 441		}
 442
 443		memset(&wrqu, 0, sizeof(wrqu));
 444		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
 445		if (bssid && status == WLAN_STATUS_SUCCESS) {
 446			memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
 447			memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
 448			wdev->wext.prev_bssid_valid = true;
 449		}
 450		wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
 451	}
 452#endif
 453
 454	if (wdev->current_bss) {
 455		cfg80211_unhold_bss(wdev->current_bss);
 456		cfg80211_put_bss(&wdev->current_bss->pub);
 457		wdev->current_bss = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 458	}
 459
 460	if (wdev->conn)
 461		wdev->conn->state = CFG80211_CONN_IDLE;
 462
 463	if (status != WLAN_STATUS_SUCCESS) {
 464		wdev->sme_state = CFG80211_SME_IDLE;
 465		if (wdev->conn)
 466			kfree(wdev->conn->ie);
 467		kfree(wdev->conn);
 468		wdev->conn = NULL;
 469		kfree(wdev->connect_keys);
 470		wdev->connect_keys = NULL;
 471		wdev->ssid_len = 0;
 
 
 
 
 
 
 
 
 472		return;
 473	}
 474
 475	if (!bss)
 476		bss = cfg80211_get_bss(wdev->wiphy,
 477				       wdev->conn ? wdev->conn->params.channel :
 478				       NULL,
 479				       bssid,
 480				       wdev->ssid, wdev->ssid_len,
 481				       WLAN_CAPABILITY_ESS,
 482				       WLAN_CAPABILITY_ESS);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 483
 484	if (WARN_ON(!bss))
 485		return;
 486
 487	cfg80211_hold_bss(bss_from_pub(bss));
 488	wdev->current_bss = bss_from_pub(bss);
 
 
 
 
 
 
 
 
 
 
 
 489
 490	wdev->sme_state = CFG80211_SME_CONNECTED;
 491	cfg80211_upload_connect_keys(wdev);
 
 
 
 
 
 
 
 
 
 
 492
 493	country_ie = (u8 *) ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
 
 
 
 
 494
 495	if (!country_ie)
 496		return;
 497
 498	/*
 499	 * ieee80211_bss_get_ie() ensures we can access:
 500	 * - country_ie + 2, the start of the country ie data, and
 501	 * - and country_ie[1] which is the IE length
 502	 */
 503	regulatory_hint_11d(wdev->wiphy,
 504			    bss->channel->band,
 505			    country_ie + 2,
 506			    country_ie[1]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 507}
 508
 509void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
 510			     const u8 *req_ie, size_t req_ie_len,
 511			     const u8 *resp_ie, size_t resp_ie_len,
 512			     u16 status, gfp_t gfp)
 513{
 514	struct wireless_dev *wdev = dev->ieee80211_ptr;
 515	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
 516	struct cfg80211_event *ev;
 517	unsigned long flags;
 518
 519	CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTING);
 520
 521	ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
 522	if (!ev)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 523		return;
 
 524
 525	ev->type = EVENT_CONNECT_RESULT;
 526	if (bssid)
 527		memcpy(ev->cr.bssid, bssid, ETH_ALEN);
 528	if (req_ie_len) {
 529		ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
 530		ev->cr.req_ie_len = req_ie_len;
 531		memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
 532	}
 533	if (resp_ie_len) {
 534		ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
 535		ev->cr.resp_ie_len = resp_ie_len;
 536		memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 537	}
 538	ev->cr.status = status;
 
 539
 540	spin_lock_irqsave(&wdev->event_lock, flags);
 541	list_add_tail(&ev->list, &wdev->event_list);
 542	spin_unlock_irqrestore(&wdev->event_lock, flags);
 543	queue_work(cfg80211_wq, &rdev->event_work);
 544}
 545EXPORT_SYMBOL(cfg80211_connect_result);
 546
 
 547void __cfg80211_roamed(struct wireless_dev *wdev,
 548		       struct ieee80211_channel *channel,
 549		       const u8 *bssid,
 550		       const u8 *req_ie, size_t req_ie_len,
 551		       const u8 *resp_ie, size_t resp_ie_len)
 552{
 553	struct cfg80211_bss *bss;
 554#ifdef CONFIG_CFG80211_WEXT
 555	union iwreq_data wrqu;
 556#endif
 
 
 557
 558	ASSERT_WDEV_LOCK(wdev);
 559
 560	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
 561		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
 562		return;
 
 
 
 563
 564	if (wdev->sme_state != CFG80211_SME_CONNECTED)
 565		return;
 
 566
 567	/* internal error -- how did we get to CONNECTED w/o BSS? */
 568	if (WARN_ON(!wdev->current_bss)) {
 569		return;
 
 570	}
 571
 572	cfg80211_unhold_bss(wdev->current_bss);
 573	cfg80211_put_bss(&wdev->current_bss->pub);
 574	wdev->current_bss = NULL;
 575
 576	bss = cfg80211_get_bss(wdev->wiphy, channel, bssid,
 577			       wdev->ssid, wdev->ssid_len,
 578			       WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS);
 
 579
 580	if (WARN_ON(!bss))
 581		return;
 
 
 
 
 
 582
 583	cfg80211_hold_bss(bss_from_pub(bss));
 584	wdev->current_bss = bss_from_pub(bss);
 
 
 
 
 
 
 
 
 
 
 585
 586	nl80211_send_roamed(wiphy_to_dev(wdev->wiphy), wdev->netdev, bssid,
 587			    req_ie, req_ie_len, resp_ie, resp_ie_len,
 588			    GFP_KERNEL);
 
 
 
 
 
 
 
 
 
 
 
 
 589
 590#ifdef CONFIG_CFG80211_WEXT
 591	if (req_ie) {
 592		memset(&wrqu, 0, sizeof(wrqu));
 593		wrqu.data.length = req_ie_len;
 594		wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
 595				    &wrqu, req_ie);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 596	}
 597
 598	if (resp_ie) {
 599		memset(&wrqu, 0, sizeof(wrqu));
 600		wrqu.data.length = resp_ie_len;
 601		wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
 602				    &wrqu, resp_ie);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 603	}
 604
 605	memset(&wrqu, 0, sizeof(wrqu));
 606	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
 607	memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
 608	memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
 609	wdev->wext.prev_bssid_valid = true;
 610	wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
 611#endif
 612}
 613
 614void cfg80211_roamed(struct net_device *dev,
 615		     struct ieee80211_channel *channel,
 616		     const u8 *bssid,
 617		     const u8 *req_ie, size_t req_ie_len,
 618		     const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
 619{
 620	struct wireless_dev *wdev = dev->ieee80211_ptr;
 621	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
 622	struct cfg80211_event *ev;
 623	unsigned long flags;
 624
 625	CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
 
 626
 627	ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
 628	if (!ev)
 629		return;
 630
 631	ev->type = EVENT_ROAMED;
 632	ev->rm.channel = channel;
 633	memcpy(ev->rm.bssid, bssid, ETH_ALEN);
 634	ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
 635	ev->rm.req_ie_len = req_ie_len;
 636	memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
 637	ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
 638	ev->rm.resp_ie_len = resp_ie_len;
 639	memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
 640
 
 
 
 
 641	spin_lock_irqsave(&wdev->event_lock, flags);
 642	list_add_tail(&ev->list, &wdev->event_list);
 643	spin_unlock_irqrestore(&wdev->event_lock, flags);
 644	queue_work(cfg80211_wq, &rdev->event_work);
 645}
 646EXPORT_SYMBOL(cfg80211_roamed);
 647
 648void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
 649			     size_t ie_len, u16 reason, bool from_ap)
 650{
 651	struct wireless_dev *wdev = dev->ieee80211_ptr;
 652	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
 653	int i;
 654#ifdef CONFIG_CFG80211_WEXT
 655	union iwreq_data wrqu;
 656#endif
 657
 658	ASSERT_WDEV_LOCK(wdev);
 659
 660	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
 661		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
 662		return;
 663
 664	if (wdev->sme_state != CFG80211_SME_CONNECTED)
 665		return;
 
 
 
 
 
 666
 667	if (wdev->current_bss) {
 668		cfg80211_unhold_bss(wdev->current_bss);
 669		cfg80211_put_bss(&wdev->current_bss->pub);
 670	}
 671
 672	wdev->current_bss = NULL;
 673	wdev->sme_state = CFG80211_SME_IDLE;
 674	wdev->ssid_len = 0;
 675
 676	if (wdev->conn) {
 677		const u8 *bssid;
 678		int ret;
 679
 680		kfree(wdev->conn->ie);
 681		wdev->conn->ie = NULL;
 682		kfree(wdev->conn);
 683		wdev->conn = NULL;
 684
 685		/*
 686		 * If this disconnect was due to a disassoc, we
 687		 * we might still have an auth BSS around. For
 688		 * the userspace SME that's currently expected,
 689		 * but for the kernel SME (nl80211 CONNECT or
 690		 * wireless extensions) we want to clear up all
 691		 * state.
 692		 */
 693		for (i = 0; i < MAX_AUTH_BSSES; i++) {
 694			if (!wdev->auth_bsses[i])
 695				continue;
 696			bssid = wdev->auth_bsses[i]->pub.bssid;
 697			ret = __cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
 698						WLAN_REASON_DEAUTH_LEAVING,
 699						false);
 700			WARN(ret, "deauth failed: %d\n", ret);
 701		}
 702	}
 703
 704	nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
 705
 706	/*
 707	 * Delete all the keys ... pairwise keys can't really
 708	 * exist any more anyway, but default keys might.
 709	 */
 710	if (rdev->ops->del_key)
 711		for (i = 0; i < 6; i++)
 712			rdev->ops->del_key(wdev->wiphy, dev, i, false, NULL);
 
 
 
 
 
 
 
 
 
 
 
 
 713
 714#ifdef CONFIG_CFG80211_WEXT
 715	memset(&wrqu, 0, sizeof(wrqu));
 716	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
 717	wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
 718	wdev->wext.connect.ssid_len = 0;
 719#endif
 720
 721	schedule_work(&cfg80211_disconnect_work);
 
 
 722}
 723
 724void cfg80211_disconnected(struct net_device *dev, u16 reason,
 725			   u8 *ie, size_t ie_len, gfp_t gfp)
 
 726{
 727	struct wireless_dev *wdev = dev->ieee80211_ptr;
 728	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
 729	struct cfg80211_event *ev;
 730	unsigned long flags;
 731
 732	CFG80211_DEV_WARN_ON(wdev->sme_state != CFG80211_SME_CONNECTED);
 733
 734	ev = kzalloc(sizeof(*ev) + ie_len, gfp);
 735	if (!ev)
 736		return;
 737
 738	ev->type = EVENT_DISCONNECTED;
 739	ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
 740	ev->dc.ie_len = ie_len;
 741	memcpy((void *)ev->dc.ie, ie, ie_len);
 742	ev->dc.reason = reason;
 
 743
 744	spin_lock_irqsave(&wdev->event_lock, flags);
 745	list_add_tail(&ev->list, &wdev->event_list);
 746	spin_unlock_irqrestore(&wdev->event_lock, flags);
 747	queue_work(cfg80211_wq, &rdev->event_work);
 748}
 749EXPORT_SYMBOL(cfg80211_disconnected);
 750
 751int __cfg80211_connect(struct cfg80211_registered_device *rdev,
 752		       struct net_device *dev,
 753		       struct cfg80211_connect_params *connect,
 754		       struct cfg80211_cached_keys *connkeys,
 755		       const u8 *prev_bssid)
 
 
 
 756{
 757	struct wireless_dev *wdev = dev->ieee80211_ptr;
 758	struct cfg80211_bss *bss = NULL;
 759	int err;
 760
 761	ASSERT_WDEV_LOCK(wdev);
 762
 763	if (wdev->sme_state != CFG80211_SME_IDLE)
 
 
 
 
 
 
 
 764		return -EALREADY;
 765
 766	if (WARN_ON(wdev->connect_keys)) {
 767		kfree(wdev->connect_keys);
 768		wdev->connect_keys = NULL;
 
 
 
 
 
 
 
 769	}
 770
 
 
 
 
 
 
 
 
 
 
 
 
 
 771	if (connkeys && connkeys->def >= 0) {
 772		int idx;
 773		u32 cipher;
 774
 775		idx = connkeys->def;
 776		cipher = connkeys->params[idx].cipher;
 777		/* If given a WEP key we may need it for shared key auth */
 778		if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
 779		    cipher == WLAN_CIPHER_SUITE_WEP104) {
 780			connect->key_idx = idx;
 781			connect->key = connkeys->params[idx].key;
 782			connect->key_len = connkeys->params[idx].key_len;
 783
 784			/*
 785			 * If ciphers are not set (e.g. when going through
 786			 * iwconfig), we have to set them appropriately here.
 787			 */
 788			if (connect->crypto.cipher_group == 0)
 789				connect->crypto.cipher_group = cipher;
 790
 791			if (connect->crypto.n_ciphers_pairwise == 0) {
 792				connect->crypto.n_ciphers_pairwise = 1;
 793				connect->crypto.ciphers_pairwise[0] = cipher;
 794			}
 795		}
 
 
 
 
 
 
 
 
 
 
 796	}
 797
 798	if (!rdev->ops->connect) {
 799		if (!rdev->ops->auth || !rdev->ops->assoc)
 800			return -EOPNOTSUPP;
 801
 802		if (WARN_ON(wdev->conn))
 803			return -EINPROGRESS;
 804
 805		wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
 806		if (!wdev->conn)
 807			return -ENOMEM;
 
 808
 
 
 809		/*
 810		 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
 
 811		 */
 812		memcpy(&wdev->conn->params, connect, sizeof(*connect));
 813		if (connect->bssid) {
 814			wdev->conn->params.bssid = wdev->conn->bssid;
 815			memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
 816		}
 817
 818		if (connect->ie) {
 819			wdev->conn->ie = kmemdup(connect->ie, connect->ie_len,
 820						GFP_KERNEL);
 821			wdev->conn->params.ie = wdev->conn->ie;
 822			if (!wdev->conn->ie) {
 823				kfree(wdev->conn);
 824				wdev->conn = NULL;
 825				return -ENOMEM;
 826			}
 827		}
 828
 829		if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
 830			wdev->conn->auto_auth = true;
 831			/* start with open system ... should mostly work */
 832			wdev->conn->params.auth_type =
 833				NL80211_AUTHTYPE_OPEN_SYSTEM;
 834		} else {
 835			wdev->conn->auto_auth = false;
 836		}
 837
 838		memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
 839		wdev->ssid_len = connect->ssid_len;
 840		wdev->conn->params.ssid = wdev->ssid;
 841		wdev->conn->params.ssid_len = connect->ssid_len;
 842
 843		/* see if we have the bss already */
 844		bss = cfg80211_get_conn_bss(wdev);
 845
 846		wdev->sme_state = CFG80211_SME_CONNECTING;
 847		wdev->connect_keys = connkeys;
 848
 849		if (prev_bssid) {
 850			memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
 851			wdev->conn->prev_bssid_valid = true;
 852		}
 853
 854		/* we're good if we have a matching bss struct */
 855		if (bss) {
 856			wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
 857			err = cfg80211_conn_do_work(wdev);
 858			cfg80211_put_bss(bss);
 859		} else {
 860			/* otherwise we'll need to scan for the AP first */
 861			err = cfg80211_conn_scan(wdev);
 862			/*
 863			 * If we can't scan right now, then we need to scan again
 864			 * after the current scan finished, since the parameters
 865			 * changed (unless we find a good AP anyway).
 866			 */
 867			if (err == -EBUSY) {
 868				err = 0;
 869				wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
 870			}
 871		}
 872		if (err) {
 873			kfree(wdev->conn->ie);
 874			kfree(wdev->conn);
 875			wdev->conn = NULL;
 876			wdev->sme_state = CFG80211_SME_IDLE;
 877			wdev->connect_keys = NULL;
 878			wdev->ssid_len = 0;
 879		}
 880
 881		return err;
 882	} else {
 883		wdev->sme_state = CFG80211_SME_CONNECTING;
 884		wdev->connect_keys = connkeys;
 885		err = rdev->ops->connect(&rdev->wiphy, dev, connect);
 886		if (err) {
 887			wdev->connect_keys = NULL;
 888			wdev->sme_state = CFG80211_SME_IDLE;
 889			return err;
 890		}
 891
 892		memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
 893		wdev->ssid_len = connect->ssid_len;
 894
 895		return 0;
 896	}
 897}
 898
 899int cfg80211_connect(struct cfg80211_registered_device *rdev,
 900		     struct net_device *dev,
 901		     struct cfg80211_connect_params *connect,
 902		     struct cfg80211_cached_keys *connkeys)
 903{
 904	int err;
 905
 906	mutex_lock(&rdev->devlist_mtx);
 907	wdev_lock(dev->ieee80211_ptr);
 908	err = __cfg80211_connect(rdev, dev, connect, connkeys, NULL);
 909	wdev_unlock(dev->ieee80211_ptr);
 910	mutex_unlock(&rdev->devlist_mtx);
 911
 912	return err;
 913}
 914
 915int __cfg80211_disconnect(struct cfg80211_registered_device *rdev,
 916			  struct net_device *dev, u16 reason, bool wextev)
 917{
 918	struct wireless_dev *wdev = dev->ieee80211_ptr;
 919	int err;
 920
 921	ASSERT_WDEV_LOCK(wdev);
 922
 923	if (wdev->sme_state == CFG80211_SME_IDLE)
 924		return -EINVAL;
 925
 926	kfree(wdev->connect_keys);
 927	wdev->connect_keys = NULL;
 928
 929	if (!rdev->ops->disconnect) {
 930		if (!rdev->ops->deauth)
 931			return -EOPNOTSUPP;
 932
 933		/* was it connected by userspace SME? */
 934		if (!wdev->conn) {
 935			cfg80211_mlme_down(rdev, dev);
 936			return 0;
 937		}
 938
 939		if (wdev->sme_state == CFG80211_SME_CONNECTING &&
 940		    (wdev->conn->state == CFG80211_CONN_SCANNING ||
 941		     wdev->conn->state == CFG80211_CONN_SCAN_AGAIN)) {
 942			wdev->sme_state = CFG80211_SME_IDLE;
 943			kfree(wdev->conn->ie);
 944			kfree(wdev->conn);
 945			wdev->conn = NULL;
 946			wdev->ssid_len = 0;
 947			return 0;
 948		}
 949
 950		/* wdev->conn->params.bssid must be set if > SCANNING */
 951		err = __cfg80211_mlme_deauth(rdev, dev,
 952					     wdev->conn->params.bssid,
 953					     NULL, 0, reason, false);
 954		if (err)
 955			return err;
 956	} else {
 957		err = rdev->ops->disconnect(&rdev->wiphy, dev, reason);
 958		if (err)
 959			return err;
 960	}
 961
 962	if (wdev->sme_state == CFG80211_SME_CONNECTED)
 963		__cfg80211_disconnected(dev, NULL, 0, 0, false);
 964	else if (wdev->sme_state == CFG80211_SME_CONNECTING)
 965		__cfg80211_connect_result(dev, NULL, NULL, 0, NULL, 0,
 966					  WLAN_STATUS_UNSPECIFIED_FAILURE,
 967					  wextev, NULL);
 968
 969	return 0;
 970}
 971
 972int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
 973			struct net_device *dev,
 974			u16 reason, bool wextev)
 975{
 976	int err;
 977
 978	wdev_lock(dev->ieee80211_ptr);
 979	err = __cfg80211_disconnect(rdev, dev, reason, wextev);
 980	wdev_unlock(dev->ieee80211_ptr);
 981
 982	return err;
 983}
 984
 985void cfg80211_sme_disassoc(struct net_device *dev, int idx)
 
 
 
 
 986{
 987	struct wireless_dev *wdev = dev->ieee80211_ptr;
 988	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
 989	u8 bssid[ETH_ALEN];
 990
 991	ASSERT_WDEV_LOCK(wdev);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 992
 993	if (!wdev->conn)
 994		return;
 995
 996	if (wdev->conn->state == CFG80211_CONN_IDLE)
 997		return;
 998
 999	/*
1000	 * Ok, so the association was made by this SME -- we don't
1001	 * want it any more so deauthenticate too.
1002	 */
1003
1004	if (!wdev->auth_bsses[idx])
1005		return;
1006
1007	memcpy(bssid, wdev->auth_bsses[idx]->pub.bssid, ETH_ALEN);
1008	if (__cfg80211_mlme_deauth(rdev, dev, bssid,
1009				   NULL, 0, WLAN_REASON_DEAUTH_LEAVING,
1010				   false)) {
1011		/* whatever -- assume gone anyway */
1012		cfg80211_unhold_bss(wdev->auth_bsses[idx]);
1013		cfg80211_put_bss(&wdev->auth_bsses[idx]->pub);
1014		wdev->auth_bsses[idx] = NULL;
1015	}
1016}