xref: /openbmc/linux/net/wireless/sme.c (revision e7054989)
1b23aa676SSamuel Ortiz /*
2ceca7b71SJohannes Berg  * SME code for cfg80211
3ceca7b71SJohannes Berg  * both driver SME event handling and the SME implementation
4ceca7b71SJohannes Berg  * (for nl80211's connect() and wext)
5b23aa676SSamuel Ortiz  *
6b23aa676SSamuel Ortiz  * Copyright 2009	Johannes Berg <johannes@sipsolutions.net>
7b23aa676SSamuel Ortiz  * Copyright (C) 2009   Intel Corporation. All rights reserved.
8b23aa676SSamuel Ortiz  */
9b23aa676SSamuel Ortiz 
10b23aa676SSamuel Ortiz #include <linux/etherdevice.h>
11b23aa676SSamuel Ortiz #include <linux/if_arp.h>
125a0e3ad6STejun Heo #include <linux/slab.h>
13b23aa676SSamuel Ortiz #include <linux/workqueue.h>
14a9a11622SJohannes Berg #include <linux/wireless.h>
15bc3b2d7fSPaul Gortmaker #include <linux/export.h>
16a9a11622SJohannes Berg #include <net/iw_handler.h>
17b23aa676SSamuel Ortiz #include <net/cfg80211.h>
18b23aa676SSamuel Ortiz #include <net/rtnetlink.h>
19b23aa676SSamuel Ortiz #include "nl80211.h"
208b19e6caSLuis R. Rodriguez #include "reg.h"
21e35e4d28SHila Gonen #include "rdev-ops.h"
22b23aa676SSamuel Ortiz 
23ceca7b71SJohannes Berg /*
24ceca7b71SJohannes Berg  * Software SME in cfg80211, using auth/assoc/deauth calls to the
25ceca7b71SJohannes Berg  * driver. This is is for implementing nl80211's connect/disconnect
26ceca7b71SJohannes Berg  * and wireless extensions (if configured.)
27ceca7b71SJohannes Berg  */
28ceca7b71SJohannes Berg 
296829c878SJohannes Berg struct cfg80211_conn {
306829c878SJohannes Berg 	struct cfg80211_connect_params params;
316829c878SJohannes Berg 	/* these are sub-states of the _CONNECTING sme_state */
326829c878SJohannes Berg 	enum {
336829c878SJohannes Berg 		CFG80211_CONN_SCANNING,
346829c878SJohannes Berg 		CFG80211_CONN_SCAN_AGAIN,
356829c878SJohannes Berg 		CFG80211_CONN_AUTHENTICATE_NEXT,
366829c878SJohannes Berg 		CFG80211_CONN_AUTHENTICATING,
37923a0e7dSJohannes Berg 		CFG80211_CONN_AUTH_FAILED,
386829c878SJohannes Berg 		CFG80211_CONN_ASSOCIATE_NEXT,
396829c878SJohannes Berg 		CFG80211_CONN_ASSOCIATING,
40923a0e7dSJohannes Berg 		CFG80211_CONN_ASSOC_FAILED,
41ceca7b71SJohannes Berg 		CFG80211_CONN_DEAUTH,
42ceca7b71SJohannes Berg 		CFG80211_CONN_CONNECTED,
436829c878SJohannes Berg 	} state;
44f401a6f7SJohannes Berg 	u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
4546b9d180SJohannes Berg 	const u8 *ie;
466829c878SJohannes Berg 	size_t ie_len;
47f401a6f7SJohannes Berg 	bool auto_auth, prev_bssid_valid;
486829c878SJohannes Berg };
496829c878SJohannes Berg 
50ceca7b71SJohannes Berg static void cfg80211_sme_free(struct wireless_dev *wdev)
5109d989d1SLuis R. Rodriguez {
52ceca7b71SJohannes Berg 	if (!wdev->conn)
53ceca7b71SJohannes Berg 		return;
5409d989d1SLuis R. Rodriguez 
55ceca7b71SJohannes Berg 	kfree(wdev->conn->ie);
56ceca7b71SJohannes Berg 	kfree(wdev->conn);
57ceca7b71SJohannes Berg 	wdev->conn = NULL;
5809d989d1SLuis R. Rodriguez }
596829c878SJohannes Berg 
606829c878SJohannes Berg static int cfg80211_conn_scan(struct wireless_dev *wdev)
616829c878SJohannes Berg {
62f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
636829c878SJohannes Berg 	struct cfg80211_scan_request *request;
646829c878SJohannes Berg 	int n_channels, err;
656829c878SJohannes Berg 
666829c878SJohannes Berg 	ASSERT_RTNL();
67667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
686829c878SJohannes Berg 
69f9d15d16SJohannes Berg 	if (rdev->scan_req || rdev->scan_msg)
706829c878SJohannes Berg 		return -EBUSY;
716829c878SJohannes Berg 
72bdfbec2dSIlan Peer 	if (wdev->conn->params.channel)
736829c878SJohannes Berg 		n_channels = 1;
74bdfbec2dSIlan Peer 	else
75bdfbec2dSIlan Peer 		n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
766829c878SJohannes Berg 
776829c878SJohannes Berg 	request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
786829c878SJohannes Berg 			  sizeof(request->channels[0]) * n_channels,
796829c878SJohannes Berg 			  GFP_KERNEL);
806829c878SJohannes Berg 	if (!request)
816829c878SJohannes Berg 		return -ENOMEM;
826829c878SJohannes Berg 
832a84ee86SKarl Beldan 	if (wdev->conn->params.channel) {
8457fbcce3SJohannes Berg 		enum nl80211_band band = wdev->conn->params.channel->band;
852a84ee86SKarl Beldan 		struct ieee80211_supported_band *sband =
862a84ee86SKarl Beldan 			wdev->wiphy->bands[band];
872a84ee86SKarl Beldan 
882a84ee86SKarl Beldan 		if (!sband) {
892a84ee86SKarl Beldan 			kfree(request);
902a84ee86SKarl Beldan 			return -EINVAL;
912a84ee86SKarl Beldan 		}
926829c878SJohannes Berg 		request->channels[0] = wdev->conn->params.channel;
932a84ee86SKarl Beldan 		request->rates[band] = (1 << sband->n_bitrates) - 1;
942a84ee86SKarl Beldan 	} else {
956829c878SJohannes Berg 		int i = 0, j;
9657fbcce3SJohannes Berg 		enum nl80211_band band;
97e3081501SRajkumar Manoharan 		struct ieee80211_supported_band *bands;
98e3081501SRajkumar Manoharan 		struct ieee80211_channel *channel;
996829c878SJohannes Berg 
10057fbcce3SJohannes Berg 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
101e3081501SRajkumar Manoharan 			bands = wdev->wiphy->bands[band];
102e3081501SRajkumar Manoharan 			if (!bands)
1036829c878SJohannes Berg 				continue;
104e3081501SRajkumar Manoharan 			for (j = 0; j < bands->n_channels; j++) {
105e3081501SRajkumar Manoharan 				channel = &bands->channels[j];
106e3081501SRajkumar Manoharan 				if (channel->flags & IEEE80211_CHAN_DISABLED)
107e3081501SRajkumar Manoharan 					continue;
108e3081501SRajkumar Manoharan 				request->channels[i++] = channel;
1096829c878SJohannes Berg 			}
110e3081501SRajkumar Manoharan 			request->rates[band] = (1 << bands->n_bitrates) - 1;
111e3081501SRajkumar Manoharan 		}
112e3081501SRajkumar Manoharan 		n_channels = i;
1136829c878SJohannes Berg 	}
1146829c878SJohannes Berg 	request->n_channels = n_channels;
1155ba63533SJohannes Berg 	request->ssids = (void *)&request->channels[n_channels];
1166829c878SJohannes Berg 	request->n_ssids = 1;
1176829c878SJohannes Berg 
1186829c878SJohannes Berg 	memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
1196829c878SJohannes Berg 		wdev->conn->params.ssid_len);
1206829c878SJohannes Berg 	request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
1216829c878SJohannes Berg 
122818965d3SJouni Malinen 	eth_broadcast_addr(request->bssid);
123818965d3SJouni Malinen 
124fd014284SJohannes Berg 	request->wdev = wdev;
12579c97e97SJohannes Berg 	request->wiphy = &rdev->wiphy;
12615d6030bSSam Leffler 	request->scan_start = jiffies;
1276829c878SJohannes Berg 
12879c97e97SJohannes Berg 	rdev->scan_req = request;
1296829c878SJohannes Berg 
130e35e4d28SHila Gonen 	err = rdev_scan(rdev, request);
1316829c878SJohannes Berg 	if (!err) {
1326829c878SJohannes Berg 		wdev->conn->state = CFG80211_CONN_SCANNING;
133fd014284SJohannes Berg 		nl80211_send_scan_start(rdev, wdev);
134463d0183SJohannes Berg 		dev_hold(wdev->netdev);
1356829c878SJohannes Berg 	} else {
13679c97e97SJohannes Berg 		rdev->scan_req = NULL;
1376829c878SJohannes Berg 		kfree(request);
1386829c878SJohannes Berg 	}
1396829c878SJohannes Berg 	return err;
1406829c878SJohannes Berg }
1416829c878SJohannes Berg 
1426829c878SJohannes Berg static int cfg80211_conn_do_work(struct wireless_dev *wdev)
1436829c878SJohannes Berg {
144f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14519957bb3SJohannes Berg 	struct cfg80211_connect_params *params;
146f62fab73SJohannes Berg 	struct cfg80211_assoc_request req = {};
14719957bb3SJohannes Berg 	int err;
1486829c878SJohannes Berg 
149667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
150667503ddSJohannes Berg 
1516829c878SJohannes Berg 	if (!wdev->conn)
1526829c878SJohannes Berg 		return 0;
1536829c878SJohannes Berg 
15419957bb3SJohannes Berg 	params = &wdev->conn->params;
15519957bb3SJohannes Berg 
1566829c878SJohannes Berg 	switch (wdev->conn->state) {
157ceca7b71SJohannes Berg 	case CFG80211_CONN_SCANNING:
158ceca7b71SJohannes Berg 		/* didn't find it during scan ... */
159ceca7b71SJohannes Berg 		return -ENOENT;
1606829c878SJohannes Berg 	case CFG80211_CONN_SCAN_AGAIN:
1616829c878SJohannes Berg 		return cfg80211_conn_scan(wdev);
1626829c878SJohannes Berg 	case CFG80211_CONN_AUTHENTICATE_NEXT:
1632fd05115SJohannes Berg 		if (WARN_ON(!rdev->ops->auth))
1642fd05115SJohannes Berg 			return -EOPNOTSUPP;
16519957bb3SJohannes Berg 		wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
16691bf9b26SJohannes Berg 		return cfg80211_mlme_auth(rdev, wdev->netdev,
16719957bb3SJohannes Berg 					  params->channel, params->auth_type,
16819957bb3SJohannes Berg 					  params->bssid,
16919957bb3SJohannes Berg 					  params->ssid, params->ssid_len,
170fffd0934SJohannes Berg 					  NULL, 0,
171fffd0934SJohannes Berg 					  params->key, params->key_len,
172e39e5b5eSJouni Malinen 					  params->key_idx, NULL, 0);
173923a0e7dSJohannes Berg 	case CFG80211_CONN_AUTH_FAILED:
174923a0e7dSJohannes Berg 		return -ENOTCONN;
1756829c878SJohannes Berg 	case CFG80211_CONN_ASSOCIATE_NEXT:
1762fd05115SJohannes Berg 		if (WARN_ON(!rdev->ops->assoc))
1772fd05115SJohannes Berg 			return -EOPNOTSUPP;
17819957bb3SJohannes Berg 		wdev->conn->state = CFG80211_CONN_ASSOCIATING;
179f401a6f7SJohannes Berg 		if (wdev->conn->prev_bssid_valid)
180f62fab73SJohannes Berg 			req.prev_bssid = wdev->conn->prev_bssid;
181f62fab73SJohannes Berg 		req.ie = params->ie;
182f62fab73SJohannes Berg 		req.ie_len = params->ie_len;
183f62fab73SJohannes Berg 		req.use_mfp = params->mfp != NL80211_MFP_NO;
184f62fab73SJohannes Berg 		req.crypto = params->crypto;
185f62fab73SJohannes Berg 		req.flags = params->flags;
186f62fab73SJohannes Berg 		req.ht_capa = params->ht_capa;
187f62fab73SJohannes Berg 		req.ht_capa_mask = params->ht_capa_mask;
188f62fab73SJohannes Berg 		req.vht_capa = params->vht_capa;
189f62fab73SJohannes Berg 		req.vht_capa_mask = params->vht_capa_mask;
190f62fab73SJohannes Berg 
19191bf9b26SJohannes Berg 		err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
192f62fab73SJohannes Berg 					  params->bssid, params->ssid,
193f62fab73SJohannes Berg 					  params->ssid_len, &req);
19419957bb3SJohannes Berg 		if (err)
19591bf9b26SJohannes Berg 			cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
196667503ddSJohannes Berg 					     NULL, 0,
197d5cdfacbSJouni Malinen 					     WLAN_REASON_DEAUTH_LEAVING,
198d5cdfacbSJouni Malinen 					     false);
19919957bb3SJohannes Berg 		return err;
200923a0e7dSJohannes Berg 	case CFG80211_CONN_ASSOC_FAILED:
201923a0e7dSJohannes Berg 		cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
202923a0e7dSJohannes Berg 				     NULL, 0,
203923a0e7dSJohannes Berg 				     WLAN_REASON_DEAUTH_LEAVING, false);
204923a0e7dSJohannes Berg 		return -ENOTCONN;
205ceca7b71SJohannes Berg 	case CFG80211_CONN_DEAUTH:
20691bf9b26SJohannes Berg 		cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
2077d930bc3SJohannes Berg 				     NULL, 0,
208d5cdfacbSJouni Malinen 				     WLAN_REASON_DEAUTH_LEAVING, false);
209923a0e7dSJohannes Berg 		/* free directly, disconnected event already sent */
210923a0e7dSJohannes Berg 		cfg80211_sme_free(wdev);
211ceca7b71SJohannes Berg 		return 0;
2126829c878SJohannes Berg 	default:
2136829c878SJohannes Berg 		return 0;
2146829c878SJohannes Berg 	}
2156829c878SJohannes Berg }
2166829c878SJohannes Berg 
2176829c878SJohannes Berg void cfg80211_conn_work(struct work_struct *work)
2186829c878SJohannes Berg {
21979c97e97SJohannes Berg 	struct cfg80211_registered_device *rdev =
2206829c878SJohannes Berg 		container_of(work, struct cfg80211_registered_device, conn_work);
2216829c878SJohannes Berg 	struct wireless_dev *wdev;
2227400f42eSJohannes Berg 	u8 bssid_buf[ETH_ALEN], *bssid = NULL;
2236829c878SJohannes Berg 
2246829c878SJohannes Berg 	rtnl_lock();
2256829c878SJohannes Berg 
22689a54e48SJohannes Berg 	list_for_each_entry(wdev, &rdev->wdev_list, list) {
227c8157976SJohannes Berg 		if (!wdev->netdev)
228c8157976SJohannes Berg 			continue;
229c8157976SJohannes Berg 
230667503ddSJohannes Berg 		wdev_lock(wdev);
231667503ddSJohannes Berg 		if (!netif_running(wdev->netdev)) {
232667503ddSJohannes Berg 			wdev_unlock(wdev);
2336829c878SJohannes Berg 			continue;
234667503ddSJohannes Berg 		}
235ceca7b71SJohannes Berg 		if (!wdev->conn ||
236ceca7b71SJohannes Berg 		    wdev->conn->state == CFG80211_CONN_CONNECTED) {
237667503ddSJohannes Berg 			wdev_unlock(wdev);
2386829c878SJohannes Berg 			continue;
239667503ddSJohannes Berg 		}
2407400f42eSJohannes Berg 		if (wdev->conn->params.bssid) {
2417400f42eSJohannes Berg 			memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
2427400f42eSJohannes Berg 			bssid = bssid_buf;
2437400f42eSJohannes Berg 		}
244ceca7b71SJohannes Berg 		if (cfg80211_conn_do_work(wdev)) {
245667503ddSJohannes Berg 			__cfg80211_connect_result(
2467d930bc3SJohannes Berg 					wdev->netdev, bssid,
2476829c878SJohannes Berg 					NULL, 0, NULL, 0,
2486829c878SJohannes Berg 					WLAN_STATUS_UNSPECIFIED_FAILURE,
249df7fc0f9SJohannes Berg 					false, NULL);
250ceca7b71SJohannes Berg 		}
251667503ddSJohannes Berg 		wdev_unlock(wdev);
2526829c878SJohannes Berg 	}
2536829c878SJohannes Berg 
2546829c878SJohannes Berg 	rtnl_unlock();
2556829c878SJohannes Berg }
2566829c878SJohannes Berg 
2570e3a39b5SBen Greear /* Returned bss is reference counted and must be cleaned up appropriately. */
258bbac31f4SJohannes Berg static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
2596829c878SJohannes Berg {
260f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
2616829c878SJohannes Berg 	struct cfg80211_bss *bss;
2626829c878SJohannes Berg 
263667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
264667503ddSJohannes Berg 
265ed9d0102SJouni Malinen 	bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
266ed9d0102SJouni Malinen 			       wdev->conn->params.bssid,
2676829c878SJohannes Berg 			       wdev->conn->params.ssid,
2686829c878SJohannes Berg 			       wdev->conn->params.ssid_len,
26934d50519SLior David 			       wdev->conn_bss_type,
2706eb18137SDedy Lansky 			       IEEE80211_PRIVACY(wdev->conn->params.privacy));
2716829c878SJohannes Berg 	if (!bss)
272bbac31f4SJohannes Berg 		return NULL;
2736829c878SJohannes Berg 
2746829c878SJohannes Berg 	memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
2756829c878SJohannes Berg 	wdev->conn->params.bssid = wdev->conn->bssid;
2766829c878SJohannes Berg 	wdev->conn->params.channel = bss->channel;
2776829c878SJohannes Berg 	wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
27879c97e97SJohannes Berg 	schedule_work(&rdev->conn_work);
2796829c878SJohannes Berg 
280bbac31f4SJohannes Berg 	return bss;
2816829c878SJohannes Berg }
2826829c878SJohannes Berg 
283667503ddSJohannes Berg static void __cfg80211_sme_scan_done(struct net_device *dev)
2846829c878SJohannes Berg {
2856829c878SJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
286f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
287bbac31f4SJohannes Berg 	struct cfg80211_bss *bss;
2886829c878SJohannes Berg 
289667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
290667503ddSJohannes Berg 
291d4b1a687SZhu Yi 	if (!wdev->conn)
2926829c878SJohannes Berg 		return;
2936829c878SJohannes Berg 
2946829c878SJohannes Berg 	if (wdev->conn->state != CFG80211_CONN_SCANNING &&
2956829c878SJohannes Berg 	    wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
2966829c878SJohannes Berg 		return;
2976829c878SJohannes Berg 
298bbac31f4SJohannes Berg 	bss = cfg80211_get_conn_bss(wdev);
299ceca7b71SJohannes Berg 	if (bss)
3005b112d3dSJohannes Berg 		cfg80211_put_bss(&rdev->wiphy, bss);
3016829c878SJohannes Berg 	else
302ceca7b71SJohannes Berg 		schedule_work(&rdev->conn_work);
3036829c878SJohannes Berg }
3046829c878SJohannes Berg 
305667503ddSJohannes Berg void cfg80211_sme_scan_done(struct net_device *dev)
306667503ddSJohannes Berg {
307667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
308667503ddSJohannes Berg 
309667503ddSJohannes Berg 	wdev_lock(wdev);
310667503ddSJohannes Berg 	__cfg80211_sme_scan_done(dev);
311667503ddSJohannes Berg 	wdev_unlock(wdev);
312667503ddSJohannes Berg }
313667503ddSJohannes Berg 
314ceca7b71SJohannes Berg void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
3156829c878SJohannes Berg {
3166829c878SJohannes Berg 	struct wiphy *wiphy = wdev->wiphy;
317f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
3186829c878SJohannes Berg 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
3196829c878SJohannes Berg 	u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
3206829c878SJohannes Berg 
321667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
322667503ddSJohannes Berg 
323ceca7b71SJohannes Berg 	if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
3246829c878SJohannes Berg 		return;
3256829c878SJohannes Berg 
3266829c878SJohannes Berg 	if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
3276829c878SJohannes Berg 	    wdev->conn->auto_auth &&
3286829c878SJohannes Berg 	    wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
3296829c878SJohannes Berg 		/* select automatically between only open, shared, leap */
3306829c878SJohannes Berg 		switch (wdev->conn->params.auth_type) {
3316829c878SJohannes Berg 		case NL80211_AUTHTYPE_OPEN_SYSTEM:
332fffd0934SJohannes Berg 			if (wdev->connect_keys)
3336829c878SJohannes Berg 				wdev->conn->params.auth_type =
3346829c878SJohannes Berg 					NL80211_AUTHTYPE_SHARED_KEY;
335fffd0934SJohannes Berg 			else
336fffd0934SJohannes Berg 				wdev->conn->params.auth_type =
337fffd0934SJohannes Berg 					NL80211_AUTHTYPE_NETWORK_EAP;
3386829c878SJohannes Berg 			break;
3396829c878SJohannes Berg 		case NL80211_AUTHTYPE_SHARED_KEY:
3406829c878SJohannes Berg 			wdev->conn->params.auth_type =
3416829c878SJohannes Berg 				NL80211_AUTHTYPE_NETWORK_EAP;
3426829c878SJohannes Berg 			break;
3436829c878SJohannes Berg 		default:
3446829c878SJohannes Berg 			/* huh? */
3456829c878SJohannes Berg 			wdev->conn->params.auth_type =
3466829c878SJohannes Berg 				NL80211_AUTHTYPE_OPEN_SYSTEM;
3476829c878SJohannes Berg 			break;
3486829c878SJohannes Berg 		}
3496829c878SJohannes Berg 		wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
3506829c878SJohannes Berg 		schedule_work(&rdev->conn_work);
35119957bb3SJohannes Berg 	} else if (status_code != WLAN_STATUS_SUCCESS) {
352ceca7b71SJohannes Berg 		__cfg80211_connect_result(wdev->netdev, mgmt->bssid,
353ceca7b71SJohannes Berg 					  NULL, 0, NULL, 0,
354df7fc0f9SJohannes Berg 					  status_code, false, NULL);
355ceca7b71SJohannes Berg 	} else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
3566829c878SJohannes Berg 		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
3576829c878SJohannes Berg 		schedule_work(&rdev->conn_work);
3586829c878SJohannes Berg 	}
3596829c878SJohannes Berg }
360b23aa676SSamuel Ortiz 
361ceca7b71SJohannes Berg bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
362f401a6f7SJohannes Berg {
363f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
364f401a6f7SJohannes Berg 
365ceca7b71SJohannes Berg 	if (!wdev->conn)
366f401a6f7SJohannes Berg 		return false;
367f401a6f7SJohannes Berg 
368ceca7b71SJohannes Berg 	if (status == WLAN_STATUS_SUCCESS) {
369ceca7b71SJohannes Berg 		wdev->conn->state = CFG80211_CONN_CONNECTED;
370f401a6f7SJohannes Berg 		return false;
371ceca7b71SJohannes Berg 	}
372f401a6f7SJohannes Berg 
373ceca7b71SJohannes Berg 	if (wdev->conn->prev_bssid_valid) {
374f401a6f7SJohannes Berg 		/*
375f401a6f7SJohannes Berg 		 * Some stupid APs don't accept reassoc, so we
376ceca7b71SJohannes Berg 		 * need to fall back to trying regular assoc;
377ceca7b71SJohannes Berg 		 * return true so no event is sent to userspace.
378f401a6f7SJohannes Berg 		 */
379f401a6f7SJohannes Berg 		wdev->conn->prev_bssid_valid = false;
380f401a6f7SJohannes Berg 		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
381f401a6f7SJohannes Berg 		schedule_work(&rdev->conn_work);
382f401a6f7SJohannes Berg 		return true;
383f401a6f7SJohannes Berg 	}
384f401a6f7SJohannes Berg 
385923a0e7dSJohannes Berg 	wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
386ceca7b71SJohannes Berg 	schedule_work(&rdev->conn_work);
387ceca7b71SJohannes Berg 	return false;
388ceca7b71SJohannes Berg }
3897d930bc3SJohannes Berg 
390ceca7b71SJohannes Berg void cfg80211_sme_deauth(struct wireless_dev *wdev)
391ceca7b71SJohannes Berg {
392ceca7b71SJohannes Berg 	cfg80211_sme_free(wdev);
393ceca7b71SJohannes Berg }
394ceca7b71SJohannes Berg 
395ceca7b71SJohannes Berg void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
396ceca7b71SJohannes Berg {
397f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
398923a0e7dSJohannes Berg 
399923a0e7dSJohannes Berg 	if (!wdev->conn)
400923a0e7dSJohannes Berg 		return;
401923a0e7dSJohannes Berg 
402923a0e7dSJohannes Berg 	wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
403923a0e7dSJohannes Berg 	schedule_work(&rdev->conn_work);
404ceca7b71SJohannes Berg }
405ceca7b71SJohannes Berg 
406ceca7b71SJohannes Berg void cfg80211_sme_disassoc(struct wireless_dev *wdev)
407ceca7b71SJohannes Berg {
408f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
409ceca7b71SJohannes Berg 
410ceca7b71SJohannes Berg 	if (!wdev->conn)
411ceca7b71SJohannes Berg 		return;
412ceca7b71SJohannes Berg 
413ceca7b71SJohannes Berg 	wdev->conn->state = CFG80211_CONN_DEAUTH;
4147d930bc3SJohannes Berg 	schedule_work(&rdev->conn_work);
4157d930bc3SJohannes Berg }
4167d930bc3SJohannes Berg 
417ceca7b71SJohannes Berg void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
418ceca7b71SJohannes Berg {
419f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
420923a0e7dSJohannes Berg 
421923a0e7dSJohannes Berg 	if (!wdev->conn)
422923a0e7dSJohannes Berg 		return;
423923a0e7dSJohannes Berg 
424923a0e7dSJohannes Berg 	wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
425923a0e7dSJohannes Berg 	schedule_work(&rdev->conn_work);
426ceca7b71SJohannes Berg }
427ceca7b71SJohannes Berg 
42846b9d180SJohannes Berg static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
42946b9d180SJohannes Berg 				     const u8 *ies, size_t ies_len,
43046b9d180SJohannes Berg 				     const u8 **out_ies, size_t *out_ies_len)
43146b9d180SJohannes Berg {
43246b9d180SJohannes Berg 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
43346b9d180SJohannes Berg 	u8 *buf;
43446b9d180SJohannes Berg 	size_t offs;
43546b9d180SJohannes Berg 
43646b9d180SJohannes Berg 	if (!rdev->wiphy.extended_capabilities_len ||
43746b9d180SJohannes Berg 	    (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
43846b9d180SJohannes Berg 		*out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
43946b9d180SJohannes Berg 		if (!*out_ies)
44046b9d180SJohannes Berg 			return -ENOMEM;
44146b9d180SJohannes Berg 		*out_ies_len = ies_len;
44246b9d180SJohannes Berg 		return 0;
44346b9d180SJohannes Berg 	}
44446b9d180SJohannes Berg 
44546b9d180SJohannes Berg 	buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
44646b9d180SJohannes Berg 		      GFP_KERNEL);
44746b9d180SJohannes Berg 	if (!buf)
44846b9d180SJohannes Berg 		return -ENOMEM;
44946b9d180SJohannes Berg 
45046b9d180SJohannes Berg 	if (ies_len) {
45146b9d180SJohannes Berg 		static const u8 before_extcapa[] = {
45246b9d180SJohannes Berg 			/* not listing IEs expected to be created by driver */
45346b9d180SJohannes Berg 			WLAN_EID_RSN,
45446b9d180SJohannes Berg 			WLAN_EID_QOS_CAPA,
45546b9d180SJohannes Berg 			WLAN_EID_RRM_ENABLED_CAPABILITIES,
45646b9d180SJohannes Berg 			WLAN_EID_MOBILITY_DOMAIN,
45746b9d180SJohannes Berg 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
45846b9d180SJohannes Berg 			WLAN_EID_BSS_COEX_2040,
45946b9d180SJohannes Berg 		};
46046b9d180SJohannes Berg 
46146b9d180SJohannes Berg 		offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
46246b9d180SJohannes Berg 					  ARRAY_SIZE(before_extcapa), 0);
46346b9d180SJohannes Berg 		memcpy(buf, ies, offs);
46446b9d180SJohannes Berg 		/* leave a whole for extended capabilities IE */
46546b9d180SJohannes Berg 		memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
46646b9d180SJohannes Berg 		       ies + offs, ies_len - offs);
46746b9d180SJohannes Berg 	} else {
46846b9d180SJohannes Berg 		offs = 0;
46946b9d180SJohannes Berg 	}
47046b9d180SJohannes Berg 
47146b9d180SJohannes Berg 	/* place extended capabilities IE (with only driver capabilities) */
47246b9d180SJohannes Berg 	buf[offs] = WLAN_EID_EXT_CAPABILITY;
47346b9d180SJohannes Berg 	buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
47446b9d180SJohannes Berg 	memcpy(buf + offs + 2,
47546b9d180SJohannes Berg 	       rdev->wiphy.extended_capabilities,
47646b9d180SJohannes Berg 	       rdev->wiphy.extended_capabilities_len);
47746b9d180SJohannes Berg 
47846b9d180SJohannes Berg 	*out_ies = buf;
47946b9d180SJohannes Berg 	*out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
48046b9d180SJohannes Berg 
48146b9d180SJohannes Berg 	return 0;
48246b9d180SJohannes Berg }
48346b9d180SJohannes Berg 
484ceca7b71SJohannes Berg static int cfg80211_sme_connect(struct wireless_dev *wdev,
485ceca7b71SJohannes Berg 				struct cfg80211_connect_params *connect,
486ceca7b71SJohannes Berg 				const u8 *prev_bssid)
487ceca7b71SJohannes Berg {
488f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
489ceca7b71SJohannes Berg 	struct cfg80211_bss *bss;
490ceca7b71SJohannes Berg 	int err;
491ceca7b71SJohannes Berg 
492ceca7b71SJohannes Berg 	if (!rdev->ops->auth || !rdev->ops->assoc)
493ceca7b71SJohannes Berg 		return -EOPNOTSUPP;
494ceca7b71SJohannes Berg 
4954ce2bd9cSJouni Malinen 	if (wdev->current_bss) {
4964ce2bd9cSJouni Malinen 		if (!prev_bssid)
497ceca7b71SJohannes Berg 			return -EALREADY;
4984ce2bd9cSJouni Malinen 		if (prev_bssid &&
4994ce2bd9cSJouni Malinen 		    !ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
5004ce2bd9cSJouni Malinen 			return -ENOTCONN;
5014ce2bd9cSJouni Malinen 		cfg80211_unhold_bss(wdev->current_bss);
5024ce2bd9cSJouni Malinen 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
5034ce2bd9cSJouni Malinen 		wdev->current_bss = NULL;
5044ce2bd9cSJouni Malinen 
5054ce2bd9cSJouni Malinen 		cfg80211_sme_free(wdev);
5064ce2bd9cSJouni Malinen 	}
507ceca7b71SJohannes Berg 
508ceca7b71SJohannes Berg 	if (WARN_ON(wdev->conn))
509ceca7b71SJohannes Berg 		return -EINPROGRESS;
510ceca7b71SJohannes Berg 
511ceca7b71SJohannes Berg 	wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
512ceca7b71SJohannes Berg 	if (!wdev->conn)
513ceca7b71SJohannes Berg 		return -ENOMEM;
514ceca7b71SJohannes Berg 
515ceca7b71SJohannes Berg 	/*
516ceca7b71SJohannes Berg 	 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
517ceca7b71SJohannes Berg 	 */
518ceca7b71SJohannes Berg 	memcpy(&wdev->conn->params, connect, sizeof(*connect));
519ceca7b71SJohannes Berg 	if (connect->bssid) {
520ceca7b71SJohannes Berg 		wdev->conn->params.bssid = wdev->conn->bssid;
521ceca7b71SJohannes Berg 		memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
522ceca7b71SJohannes Berg 	}
523ceca7b71SJohannes Berg 
52446b9d180SJohannes Berg 	if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
52546b9d180SJohannes Berg 				      &wdev->conn->ie,
52646b9d180SJohannes Berg 				      &wdev->conn->params.ie_len)) {
527ceca7b71SJohannes Berg 		kfree(wdev->conn);
528ceca7b71SJohannes Berg 		wdev->conn = NULL;
529ceca7b71SJohannes Berg 		return -ENOMEM;
530ceca7b71SJohannes Berg 	}
53146b9d180SJohannes Berg 	wdev->conn->params.ie = wdev->conn->ie;
532ceca7b71SJohannes Berg 
533ceca7b71SJohannes Berg 	if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
534ceca7b71SJohannes Berg 		wdev->conn->auto_auth = true;
535ceca7b71SJohannes Berg 		/* start with open system ... should mostly work */
536ceca7b71SJohannes Berg 		wdev->conn->params.auth_type =
537ceca7b71SJohannes Berg 			NL80211_AUTHTYPE_OPEN_SYSTEM;
538ceca7b71SJohannes Berg 	} else {
539ceca7b71SJohannes Berg 		wdev->conn->auto_auth = false;
540ceca7b71SJohannes Berg 	}
541ceca7b71SJohannes Berg 
542ceca7b71SJohannes Berg 	wdev->conn->params.ssid = wdev->ssid;
543babd3a27SZhao, Gang 	wdev->conn->params.ssid_len = wdev->ssid_len;
544ceca7b71SJohannes Berg 
545ceca7b71SJohannes Berg 	/* see if we have the bss already */
546ceca7b71SJohannes Berg 	bss = cfg80211_get_conn_bss(wdev);
547ceca7b71SJohannes Berg 
548ceca7b71SJohannes Berg 	if (prev_bssid) {
549ceca7b71SJohannes Berg 		memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
550ceca7b71SJohannes Berg 		wdev->conn->prev_bssid_valid = true;
551ceca7b71SJohannes Berg 	}
552ceca7b71SJohannes Berg 
553ceca7b71SJohannes Berg 	/* we're good if we have a matching bss struct */
554ceca7b71SJohannes Berg 	if (bss) {
555ceca7b71SJohannes Berg 		err = cfg80211_conn_do_work(wdev);
556ceca7b71SJohannes Berg 		cfg80211_put_bss(wdev->wiphy, bss);
557ceca7b71SJohannes Berg 	} else {
558ceca7b71SJohannes Berg 		/* otherwise we'll need to scan for the AP first */
559ceca7b71SJohannes Berg 		err = cfg80211_conn_scan(wdev);
560ceca7b71SJohannes Berg 
561ceca7b71SJohannes Berg 		/*
562ceca7b71SJohannes Berg 		 * If we can't scan right now, then we need to scan again
563ceca7b71SJohannes Berg 		 * after the current scan finished, since the parameters
564ceca7b71SJohannes Berg 		 * changed (unless we find a good AP anyway).
565ceca7b71SJohannes Berg 		 */
566ceca7b71SJohannes Berg 		if (err == -EBUSY) {
567ceca7b71SJohannes Berg 			err = 0;
568ceca7b71SJohannes Berg 			wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
569ceca7b71SJohannes Berg 		}
570ceca7b71SJohannes Berg 	}
571ceca7b71SJohannes Berg 
572ceca7b71SJohannes Berg 	if (err)
573ceca7b71SJohannes Berg 		cfg80211_sme_free(wdev);
574ceca7b71SJohannes Berg 
575ceca7b71SJohannes Berg 	return err;
576ceca7b71SJohannes Berg }
577ceca7b71SJohannes Berg 
578ceca7b71SJohannes Berg static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
579ceca7b71SJohannes Berg {
580f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
581ceca7b71SJohannes Berg 	int err;
582ceca7b71SJohannes Berg 
583ceca7b71SJohannes Berg 	if (!wdev->conn)
584ceca7b71SJohannes Berg 		return 0;
585ceca7b71SJohannes Berg 
586ceca7b71SJohannes Berg 	if (!rdev->ops->deauth)
587ceca7b71SJohannes Berg 		return -EOPNOTSUPP;
588ceca7b71SJohannes Berg 
589ceca7b71SJohannes Berg 	if (wdev->conn->state == CFG80211_CONN_SCANNING ||
590ceca7b71SJohannes Berg 	    wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
591ceca7b71SJohannes Berg 		err = 0;
592ceca7b71SJohannes Berg 		goto out;
593ceca7b71SJohannes Berg 	}
594ceca7b71SJohannes Berg 
595ceca7b71SJohannes Berg 	/* wdev->conn->params.bssid must be set if > SCANNING */
596ceca7b71SJohannes Berg 	err = cfg80211_mlme_deauth(rdev, wdev->netdev,
597ceca7b71SJohannes Berg 				   wdev->conn->params.bssid,
598ceca7b71SJohannes Berg 				   NULL, 0, reason, false);
599ceca7b71SJohannes Berg  out:
600ceca7b71SJohannes Berg 	cfg80211_sme_free(wdev);
601ceca7b71SJohannes Berg 	return err;
602ceca7b71SJohannes Berg }
603ceca7b71SJohannes Berg 
604ceca7b71SJohannes Berg /*
605ceca7b71SJohannes Berg  * code shared for in-device and software SME
606ceca7b71SJohannes Berg  */
607ceca7b71SJohannes Berg 
608ceca7b71SJohannes Berg static bool cfg80211_is_all_idle(void)
609ceca7b71SJohannes Berg {
610ceca7b71SJohannes Berg 	struct cfg80211_registered_device *rdev;
611ceca7b71SJohannes Berg 	struct wireless_dev *wdev;
612ceca7b71SJohannes Berg 	bool is_all_idle = true;
613ceca7b71SJohannes Berg 
614ceca7b71SJohannes Berg 	/*
615ceca7b71SJohannes Berg 	 * All devices must be idle as otherwise if you are actively
616ceca7b71SJohannes Berg 	 * scanning some new beacon hints could be learned and would
617ceca7b71SJohannes Berg 	 * count as new regulatory hints.
618ceca7b71SJohannes Berg 	 */
619ceca7b71SJohannes Berg 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
620ceca7b71SJohannes Berg 		list_for_each_entry(wdev, &rdev->wdev_list, list) {
621ceca7b71SJohannes Berg 			wdev_lock(wdev);
622ceca7b71SJohannes Berg 			if (wdev->conn || wdev->current_bss)
623ceca7b71SJohannes Berg 				is_all_idle = false;
624ceca7b71SJohannes Berg 			wdev_unlock(wdev);
625ceca7b71SJohannes Berg 		}
626ceca7b71SJohannes Berg 	}
627ceca7b71SJohannes Berg 
628ceca7b71SJohannes Berg 	return is_all_idle;
629ceca7b71SJohannes Berg }
630ceca7b71SJohannes Berg 
631ceca7b71SJohannes Berg static void disconnect_work(struct work_struct *work)
632ceca7b71SJohannes Berg {
633ceca7b71SJohannes Berg 	rtnl_lock();
634ceca7b71SJohannes Berg 	if (cfg80211_is_all_idle())
635ceca7b71SJohannes Berg 		regulatory_hint_disconnect();
636ceca7b71SJohannes Berg 	rtnl_unlock();
637ceca7b71SJohannes Berg }
638ceca7b71SJohannes Berg 
639ceca7b71SJohannes Berg static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
640ceca7b71SJohannes Berg 
641ceca7b71SJohannes Berg 
642ceca7b71SJohannes Berg /*
643ceca7b71SJohannes Berg  * API calls for drivers implementing connect/disconnect and
644ceca7b71SJohannes Berg  * SME event handling
645ceca7b71SJohannes Berg  */
646ceca7b71SJohannes Berg 
6476f390908SBen Greear /* This method must consume bss one way or another */
648667503ddSJohannes Berg void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
649b23aa676SSamuel Ortiz 			       const u8 *req_ie, size_t req_ie_len,
650b23aa676SSamuel Ortiz 			       const u8 *resp_ie, size_t resp_ie_len,
651df7fc0f9SJohannes Berg 			       u16 status, bool wextev,
652df7fc0f9SJohannes Berg 			       struct cfg80211_bss *bss)
653b23aa676SSamuel Ortiz {
654b23aa676SSamuel Ortiz 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6559caf0364SJohannes Berg 	const u8 *country_ie;
6563d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
657b23aa676SSamuel Ortiz 	union iwreq_data wrqu;
658b23aa676SSamuel Ortiz #endif
659b23aa676SSamuel Ortiz 
660667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
661667503ddSJohannes Berg 
662074ac8dfSJohannes Berg 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
6636f390908SBen Greear 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
6646f390908SBen Greear 		cfg80211_put_bss(wdev->wiphy, bss);
665b23aa676SSamuel Ortiz 		return;
6666f390908SBen Greear 	}
667b23aa676SSamuel Ortiz 
668f26cbf40SZhao, Gang 	nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev,
669e45cd82aSJohannes Berg 				    bssid, req_ie, req_ie_len,
670e45cd82aSJohannes Berg 				    resp_ie, resp_ie_len,
671667503ddSJohannes Berg 				    status, GFP_KERNEL);
672e45cd82aSJohannes Berg 
6733d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
674e45cd82aSJohannes Berg 	if (wextev) {
675e45cd82aSJohannes Berg 		if (req_ie && status == WLAN_STATUS_SUCCESS) {
676e45cd82aSJohannes Berg 			memset(&wrqu, 0, sizeof(wrqu));
677e45cd82aSJohannes Berg 			wrqu.data.length = req_ie_len;
6783409ff77SZhu Yi 			wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
679e45cd82aSJohannes Berg 		}
680e45cd82aSJohannes Berg 
681e45cd82aSJohannes Berg 		if (resp_ie && status == WLAN_STATUS_SUCCESS) {
682e45cd82aSJohannes Berg 			memset(&wrqu, 0, sizeof(wrqu));
683e45cd82aSJohannes Berg 			wrqu.data.length = resp_ie_len;
684e45cd82aSJohannes Berg 			wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
685e45cd82aSJohannes Berg 		}
686e45cd82aSJohannes Berg 
687e45cd82aSJohannes Berg 		memset(&wrqu, 0, sizeof(wrqu));
688e45cd82aSJohannes Berg 		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
689f401a6f7SJohannes Berg 		if (bssid && status == WLAN_STATUS_SUCCESS) {
690e45cd82aSJohannes Berg 			memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
691f401a6f7SJohannes Berg 			memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
692f401a6f7SJohannes Berg 			wdev->wext.prev_bssid_valid = true;
693f401a6f7SJohannes Berg 		}
694e45cd82aSJohannes Berg 		wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
695e45cd82aSJohannes Berg 	}
696e45cd82aSJohannes Berg #endif
697e45cd82aSJohannes Berg 
6984c4d684aSUjjal Roy 	if (!bss && (status == WLAN_STATUS_SUCCESS)) {
699f26cbf40SZhao, Gang 		WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
7004c4d684aSUjjal Roy 		bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
7014c4d684aSUjjal Roy 				       wdev->ssid, wdev->ssid_len,
70234d50519SLior David 				       wdev->conn_bss_type,
7036eb18137SDedy Lansky 				       IEEE80211_PRIVACY_ANY);
7044c4d684aSUjjal Roy 		if (bss)
7054c4d684aSUjjal Roy 			cfg80211_hold_bss(bss_from_pub(bss));
7064c4d684aSUjjal Roy 	}
7074c4d684aSUjjal Roy 
708df7fc0f9SJohannes Berg 	if (wdev->current_bss) {
709df7fc0f9SJohannes Berg 		cfg80211_unhold_bss(wdev->current_bss);
7105b112d3dSJohannes Berg 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
711df7fc0f9SJohannes Berg 		wdev->current_bss = NULL;
712df7fc0f9SJohannes Berg 	}
713df7fc0f9SJohannes Berg 
714fffd0934SJohannes Berg 	if (status != WLAN_STATUS_SUCCESS) {
715b47f610bSJohannes Berg 		kzfree(wdev->connect_keys);
716fffd0934SJohannes Berg 		wdev->connect_keys = NULL;
7178dadadb7SJohannes Berg 		wdev->ssid_len = 0;
718f1940c57SJohannes Berg 		if (bss) {
719f1940c57SJohannes Berg 			cfg80211_unhold_bss(bss_from_pub(bss));
7205b112d3dSJohannes Berg 			cfg80211_put_bss(wdev->wiphy, bss);
721f1940c57SJohannes Berg 		}
722c1fbb258SEliad Peller 		cfg80211_sme_free(wdev);
723fffd0934SJohannes Berg 		return;
724fffd0934SJohannes Berg 	}
725fffd0934SJohannes Berg 
726b23aa676SSamuel Ortiz 	if (WARN_ON(!bss))
727b23aa676SSamuel Ortiz 		return;
728f1940c57SJohannes Berg 
72919957bb3SJohannes Berg 	wdev->current_bss = bss_from_pub(bss);
730b23aa676SSamuel Ortiz 
731fffd0934SJohannes Berg 	cfg80211_upload_connect_keys(wdev);
7328b19e6caSLuis R. Rodriguez 
7339caf0364SJohannes Berg 	rcu_read_lock();
7349caf0364SJohannes Berg 	country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
7359caf0364SJohannes Berg 	if (!country_ie) {
7369caf0364SJohannes Berg 		rcu_read_unlock();
7379caf0364SJohannes Berg 		return;
7389caf0364SJohannes Berg 	}
7399caf0364SJohannes Berg 
7409caf0364SJohannes Berg 	country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
7419caf0364SJohannes Berg 	rcu_read_unlock();
7428b19e6caSLuis R. Rodriguez 
7438b19e6caSLuis R. Rodriguez 	if (!country_ie)
7448b19e6caSLuis R. Rodriguez 		return;
7458b19e6caSLuis R. Rodriguez 
7468b19e6caSLuis R. Rodriguez 	/*
7478b19e6caSLuis R. Rodriguez 	 * ieee80211_bss_get_ie() ensures we can access:
7488b19e6caSLuis R. Rodriguez 	 * - country_ie + 2, the start of the country ie data, and
7498b19e6caSLuis R. Rodriguez 	 * - and country_ie[1] which is the IE length
7508b19e6caSLuis R. Rodriguez 	 */
751789fd033SLuis R. Rodriguez 	regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
7521a919318SJohannes Berg 				   country_ie + 2, country_ie[1]);
7539caf0364SJohannes Berg 	kfree(country_ie);
754b23aa676SSamuel Ortiz }
755f2129354SJohannes Berg 
756*e7054989SKanchanapally, Vidyullatha /* Consumes bss object one way or another */
757*e7054989SKanchanapally, Vidyullatha void cfg80211_connect_bss(struct net_device *dev, const u8 *bssid,
758*e7054989SKanchanapally, Vidyullatha 			  struct cfg80211_bss *bss, const u8 *req_ie,
759*e7054989SKanchanapally, Vidyullatha 			  size_t req_ie_len, const u8 *resp_ie,
760*e7054989SKanchanapally, Vidyullatha 			  size_t resp_ie_len, u16 status, gfp_t gfp)
761f2129354SJohannes Berg {
762667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
763f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
764667503ddSJohannes Berg 	struct cfg80211_event *ev;
765667503ddSJohannes Berg 	unsigned long flags;
766667503ddSJohannes Berg 
767*e7054989SKanchanapally, Vidyullatha 	if (bss) {
768*e7054989SKanchanapally, Vidyullatha 		/* Make sure the bss entry provided by the driver is valid. */
769*e7054989SKanchanapally, Vidyullatha 		struct cfg80211_internal_bss *ibss = bss_from_pub(bss);
770*e7054989SKanchanapally, Vidyullatha 
771*e7054989SKanchanapally, Vidyullatha 		if (WARN_ON(list_empty(&ibss->list))) {
772*e7054989SKanchanapally, Vidyullatha 			cfg80211_put_bss(wdev->wiphy, bss);
773667503ddSJohannes Berg 			return;
774*e7054989SKanchanapally, Vidyullatha 		}
775*e7054989SKanchanapally, Vidyullatha 	}
776*e7054989SKanchanapally, Vidyullatha 
777*e7054989SKanchanapally, Vidyullatha 	ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
778*e7054989SKanchanapally, Vidyullatha 	if (!ev) {
779*e7054989SKanchanapally, Vidyullatha 		cfg80211_put_bss(wdev->wiphy, bss);
780*e7054989SKanchanapally, Vidyullatha 		return;
781*e7054989SKanchanapally, Vidyullatha 	}
782667503ddSJohannes Berg 
783667503ddSJohannes Berg 	ev->type = EVENT_CONNECT_RESULT;
78416a832e7SZhu Yi 	if (bssid)
785667503ddSJohannes Berg 		memcpy(ev->cr.bssid, bssid, ETH_ALEN);
7867834704bSNishant Sarmukadam 	if (req_ie_len) {
787667503ddSJohannes Berg 		ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
788667503ddSJohannes Berg 		ev->cr.req_ie_len = req_ie_len;
789667503ddSJohannes Berg 		memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
7907834704bSNishant Sarmukadam 	}
7917834704bSNishant Sarmukadam 	if (resp_ie_len) {
792667503ddSJohannes Berg 		ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
793667503ddSJohannes Berg 		ev->cr.resp_ie_len = resp_ie_len;
794667503ddSJohannes Berg 		memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
7957834704bSNishant Sarmukadam 	}
796*e7054989SKanchanapally, Vidyullatha 	if (bss)
797*e7054989SKanchanapally, Vidyullatha 		cfg80211_hold_bss(bss_from_pub(bss));
798*e7054989SKanchanapally, Vidyullatha 	ev->cr.bss = bss;
799667503ddSJohannes Berg 	ev->cr.status = status;
800667503ddSJohannes Berg 
801667503ddSJohannes Berg 	spin_lock_irqsave(&wdev->event_lock, flags);
802667503ddSJohannes Berg 	list_add_tail(&ev->list, &wdev->event_list);
803667503ddSJohannes Berg 	spin_unlock_irqrestore(&wdev->event_lock, flags);
804e60d7443SAlban Browaeys 	queue_work(cfg80211_wq, &rdev->event_work);
805f2129354SJohannes Berg }
806*e7054989SKanchanapally, Vidyullatha EXPORT_SYMBOL(cfg80211_connect_bss);
807b23aa676SSamuel Ortiz 
8080e3a39b5SBen Greear /* Consumes bss object one way or another */
809ed9d0102SJouni Malinen void __cfg80211_roamed(struct wireless_dev *wdev,
810adbde344SVasanthakumar Thiagarajan 		       struct cfg80211_bss *bss,
811b23aa676SSamuel Ortiz 		       const u8 *req_ie, size_t req_ie_len,
812667503ddSJohannes Berg 		       const u8 *resp_ie, size_t resp_ie_len)
813b23aa676SSamuel Ortiz {
8143d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
815b23aa676SSamuel Ortiz 	union iwreq_data wrqu;
816b23aa676SSamuel Ortiz #endif
817667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
818667503ddSJohannes Berg 
819074ac8dfSJohannes Berg 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
820074ac8dfSJohannes Berg 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
821adbde344SVasanthakumar Thiagarajan 		goto out;
822b23aa676SSamuel Ortiz 
823ceca7b71SJohannes Berg 	if (WARN_ON(!wdev->current_bss))
824adbde344SVasanthakumar Thiagarajan 		goto out;
825b23aa676SSamuel Ortiz 
826b23aa676SSamuel Ortiz 	cfg80211_unhold_bss(wdev->current_bss);
8275b112d3dSJohannes Berg 	cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
828b23aa676SSamuel Ortiz 	wdev->current_bss = NULL;
829b23aa676SSamuel Ortiz 
83019957bb3SJohannes Berg 	cfg80211_hold_bss(bss_from_pub(bss));
83119957bb3SJohannes Berg 	wdev->current_bss = bss_from_pub(bss);
832b23aa676SSamuel Ortiz 
833f26cbf40SZhao, Gang 	nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
834f26cbf40SZhao, Gang 			    wdev->netdev, bss->bssid,
835667503ddSJohannes Berg 			    req_ie, req_ie_len, resp_ie, resp_ie_len,
836667503ddSJohannes Berg 			    GFP_KERNEL);
837b23aa676SSamuel Ortiz 
8383d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
839b23aa676SSamuel Ortiz 	if (req_ie) {
840b23aa676SSamuel Ortiz 		memset(&wrqu, 0, sizeof(wrqu));
841b23aa676SSamuel Ortiz 		wrqu.data.length = req_ie_len;
8423409ff77SZhu Yi 		wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
843667503ddSJohannes Berg 				    &wrqu, req_ie);
844b23aa676SSamuel Ortiz 	}
845b23aa676SSamuel Ortiz 
846b23aa676SSamuel Ortiz 	if (resp_ie) {
847b23aa676SSamuel Ortiz 		memset(&wrqu, 0, sizeof(wrqu));
848b23aa676SSamuel Ortiz 		wrqu.data.length = resp_ie_len;
849667503ddSJohannes Berg 		wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
850667503ddSJohannes Berg 				    &wrqu, resp_ie);
851b23aa676SSamuel Ortiz 	}
852b23aa676SSamuel Ortiz 
853b23aa676SSamuel Ortiz 	memset(&wrqu, 0, sizeof(wrqu));
854b23aa676SSamuel Ortiz 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
855adbde344SVasanthakumar Thiagarajan 	memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
856adbde344SVasanthakumar Thiagarajan 	memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
857f401a6f7SJohannes Berg 	wdev->wext.prev_bssid_valid = true;
858667503ddSJohannes Berg 	wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
859b23aa676SSamuel Ortiz #endif
860adbde344SVasanthakumar Thiagarajan 
861adbde344SVasanthakumar Thiagarajan 	return;
862adbde344SVasanthakumar Thiagarajan out:
8635b112d3dSJohannes Berg 	cfg80211_put_bss(wdev->wiphy, bss);
864b23aa676SSamuel Ortiz }
865667503ddSJohannes Berg 
866ed9d0102SJouni Malinen void cfg80211_roamed(struct net_device *dev,
867ed9d0102SJouni Malinen 		     struct ieee80211_channel *channel,
868ed9d0102SJouni Malinen 		     const u8 *bssid,
869667503ddSJohannes Berg 		     const u8 *req_ie, size_t req_ie_len,
870667503ddSJohannes Berg 		     const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
871667503ddSJohannes Berg {
872667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
873adbde344SVasanthakumar Thiagarajan 	struct cfg80211_bss *bss;
874adbde344SVasanthakumar Thiagarajan 
875adbde344SVasanthakumar Thiagarajan 	bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
8766eb18137SDedy Lansky 			       wdev->ssid_len,
87734d50519SLior David 			       wdev->conn_bss_type, IEEE80211_PRIVACY_ANY);
878adbde344SVasanthakumar Thiagarajan 	if (WARN_ON(!bss))
879adbde344SVasanthakumar Thiagarajan 		return;
880adbde344SVasanthakumar Thiagarajan 
881adbde344SVasanthakumar Thiagarajan 	cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
882adbde344SVasanthakumar Thiagarajan 			    resp_ie_len, gfp);
883adbde344SVasanthakumar Thiagarajan }
884adbde344SVasanthakumar Thiagarajan EXPORT_SYMBOL(cfg80211_roamed);
885adbde344SVasanthakumar Thiagarajan 
8860e3a39b5SBen Greear /* Consumes bss object one way or another */
887adbde344SVasanthakumar Thiagarajan void cfg80211_roamed_bss(struct net_device *dev,
888adbde344SVasanthakumar Thiagarajan 			 struct cfg80211_bss *bss, const u8 *req_ie,
889adbde344SVasanthakumar Thiagarajan 			 size_t req_ie_len, const u8 *resp_ie,
890adbde344SVasanthakumar Thiagarajan 			 size_t resp_ie_len, gfp_t gfp)
891adbde344SVasanthakumar Thiagarajan {
892adbde344SVasanthakumar Thiagarajan 	struct wireless_dev *wdev = dev->ieee80211_ptr;
893f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
894667503ddSJohannes Berg 	struct cfg80211_event *ev;
895667503ddSJohannes Berg 	unsigned long flags;
896667503ddSJohannes Berg 
897adbde344SVasanthakumar Thiagarajan 	if (WARN_ON(!bss))
898667503ddSJohannes Berg 		return;
899667503ddSJohannes Berg 
900adbde344SVasanthakumar Thiagarajan 	ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
901adbde344SVasanthakumar Thiagarajan 	if (!ev) {
9025b112d3dSJohannes Berg 		cfg80211_put_bss(wdev->wiphy, bss);
903adbde344SVasanthakumar Thiagarajan 		return;
904adbde344SVasanthakumar Thiagarajan 	}
905adbde344SVasanthakumar Thiagarajan 
906667503ddSJohannes Berg 	ev->type = EVENT_ROAMED;
907667503ddSJohannes Berg 	ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
908667503ddSJohannes Berg 	ev->rm.req_ie_len = req_ie_len;
909667503ddSJohannes Berg 	memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
910667503ddSJohannes Berg 	ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
911667503ddSJohannes Berg 	ev->rm.resp_ie_len = resp_ie_len;
912667503ddSJohannes Berg 	memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
913adbde344SVasanthakumar Thiagarajan 	ev->rm.bss = bss;
914667503ddSJohannes Berg 
915667503ddSJohannes Berg 	spin_lock_irqsave(&wdev->event_lock, flags);
916667503ddSJohannes Berg 	list_add_tail(&ev->list, &wdev->event_list);
917667503ddSJohannes Berg 	spin_unlock_irqrestore(&wdev->event_lock, flags);
918e60d7443SAlban Browaeys 	queue_work(cfg80211_wq, &rdev->event_work);
919667503ddSJohannes Berg }
920adbde344SVasanthakumar Thiagarajan EXPORT_SYMBOL(cfg80211_roamed_bss);
921b23aa676SSamuel Ortiz 
922667503ddSJohannes Berg void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
9236829c878SJohannes Berg 			     size_t ie_len, u16 reason, bool from_ap)
924b23aa676SSamuel Ortiz {
925b23aa676SSamuel Ortiz 	struct wireless_dev *wdev = dev->ieee80211_ptr;
926f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
927fffd0934SJohannes Berg 	int i;
9283d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
929b23aa676SSamuel Ortiz 	union iwreq_data wrqu;
930b23aa676SSamuel Ortiz #endif
931b23aa676SSamuel Ortiz 
932667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
933667503ddSJohannes Berg 
934074ac8dfSJohannes Berg 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
935074ac8dfSJohannes Berg 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
936b23aa676SSamuel Ortiz 		return;
937b23aa676SSamuel Ortiz 
938b23aa676SSamuel Ortiz 	if (wdev->current_bss) {
939b23aa676SSamuel Ortiz 		cfg80211_unhold_bss(wdev->current_bss);
9405b112d3dSJohannes Berg 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
941b23aa676SSamuel Ortiz 	}
942b23aa676SSamuel Ortiz 
943b23aa676SSamuel Ortiz 	wdev->current_bss = NULL;
9448dadadb7SJohannes Berg 	wdev->ssid_len = 0;
945b23aa676SSamuel Ortiz 
946fffd0934SJohannes Berg 	nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
947fffd0934SJohannes Berg 
948b8607152SArend van Spriel 	/* stop critical protocol if supported */
949b8607152SArend van Spriel 	if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
950b8607152SArend van Spriel 		rdev->crit_proto_nlportid = 0;
951b8607152SArend van Spriel 		rdev_crit_proto_stop(rdev, wdev);
952b8607152SArend van Spriel 	}
953b8607152SArend van Spriel 
954fffd0934SJohannes Berg 	/*
955fffd0934SJohannes Berg 	 * Delete all the keys ... pairwise keys can't really
956fffd0934SJohannes Berg 	 * exist any more anyway, but default keys might.
957fffd0934SJohannes Berg 	 */
958fffd0934SJohannes Berg 	if (rdev->ops->del_key)
959fffd0934SJohannes Berg 		for (i = 0; i < 6; i++)
960e35e4d28SHila Gonen 			rdev_del_key(rdev, dev, i, false, NULL);
961b23aa676SSamuel Ortiz 
962fa9ffc74SKyeyoon Park 	rdev_set_qos_map(rdev, dev, NULL);
963fa9ffc74SKyeyoon Park 
9643d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
965b23aa676SSamuel Ortiz 	memset(&wrqu, 0, sizeof(wrqu));
966b23aa676SSamuel Ortiz 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
967b23aa676SSamuel Ortiz 	wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
9685f612033SAbhijeet Kolekar 	wdev->wext.connect.ssid_len = 0;
969b23aa676SSamuel Ortiz #endif
97009d989d1SLuis R. Rodriguez 
97109d989d1SLuis R. Rodriguez 	schedule_work(&cfg80211_disconnect_work);
972b23aa676SSamuel Ortiz }
973b23aa676SSamuel Ortiz 
974b23aa676SSamuel Ortiz void cfg80211_disconnected(struct net_device *dev, u16 reason,
97580279fb7SJohannes Berg 			   const u8 *ie, size_t ie_len,
97680279fb7SJohannes Berg 			   bool locally_generated, gfp_t gfp)
977b23aa676SSamuel Ortiz {
978667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
979f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
980667503ddSJohannes Berg 	struct cfg80211_event *ev;
981667503ddSJohannes Berg 	unsigned long flags;
982667503ddSJohannes Berg 
983667503ddSJohannes Berg 	ev = kzalloc(sizeof(*ev) + ie_len, gfp);
984667503ddSJohannes Berg 	if (!ev)
985667503ddSJohannes Berg 		return;
986667503ddSJohannes Berg 
987667503ddSJohannes Berg 	ev->type = EVENT_DISCONNECTED;
988667503ddSJohannes Berg 	ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
989667503ddSJohannes Berg 	ev->dc.ie_len = ie_len;
990667503ddSJohannes Berg 	memcpy((void *)ev->dc.ie, ie, ie_len);
991667503ddSJohannes Berg 	ev->dc.reason = reason;
99280279fb7SJohannes Berg 	ev->dc.locally_generated = locally_generated;
993667503ddSJohannes Berg 
994667503ddSJohannes Berg 	spin_lock_irqsave(&wdev->event_lock, flags);
995667503ddSJohannes Berg 	list_add_tail(&ev->list, &wdev->event_list);
996667503ddSJohannes Berg 	spin_unlock_irqrestore(&wdev->event_lock, flags);
997e60d7443SAlban Browaeys 	queue_work(cfg80211_wq, &rdev->event_work);
998b23aa676SSamuel Ortiz }
999b23aa676SSamuel Ortiz EXPORT_SYMBOL(cfg80211_disconnected);
1000b23aa676SSamuel Ortiz 
1001ceca7b71SJohannes Berg /*
1002ceca7b71SJohannes Berg  * API calls for nl80211/wext compatibility code
1003ceca7b71SJohannes Berg  */
100483739b03SJohannes Berg int cfg80211_connect(struct cfg80211_registered_device *rdev,
1005b23aa676SSamuel Ortiz 		     struct net_device *dev,
1006fffd0934SJohannes Berg 		     struct cfg80211_connect_params *connect,
1007f401a6f7SJohannes Berg 		     struct cfg80211_cached_keys *connkeys,
1008f401a6f7SJohannes Berg 		     const u8 *prev_bssid)
1009b23aa676SSamuel Ortiz {
1010b23aa676SSamuel Ortiz 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1011667503ddSJohannes Berg 	int err;
1012667503ddSJohannes Berg 
1013667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
1014b23aa676SSamuel Ortiz 
1015fffd0934SJohannes Berg 	if (WARN_ON(wdev->connect_keys)) {
1016b47f610bSJohannes Berg 		kzfree(wdev->connect_keys);
1017fffd0934SJohannes Berg 		wdev->connect_keys = NULL;
1018fffd0934SJohannes Berg 	}
1019fffd0934SJohannes Berg 
10207e7c8926SBen Greear 	cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
10217e7c8926SBen Greear 				  rdev->wiphy.ht_capa_mod_mask);
10227e7c8926SBen Greear 
1023fffd0934SJohannes Berg 	if (connkeys && connkeys->def >= 0) {
1024fffd0934SJohannes Berg 		int idx;
1025bcba8eaeSSamuel Ortiz 		u32 cipher;
1026fffd0934SJohannes Berg 
1027fffd0934SJohannes Berg 		idx = connkeys->def;
1028bcba8eaeSSamuel Ortiz 		cipher = connkeys->params[idx].cipher;
1029fffd0934SJohannes Berg 		/* If given a WEP key we may need it for shared key auth */
1030bcba8eaeSSamuel Ortiz 		if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1031bcba8eaeSSamuel Ortiz 		    cipher == WLAN_CIPHER_SUITE_WEP104) {
1032fffd0934SJohannes Berg 			connect->key_idx = idx;
1033fffd0934SJohannes Berg 			connect->key = connkeys->params[idx].key;
1034fffd0934SJohannes Berg 			connect->key_len = connkeys->params[idx].key_len;
1035bcba8eaeSSamuel Ortiz 
1036bcba8eaeSSamuel Ortiz 			/*
1037bcba8eaeSSamuel Ortiz 			 * If ciphers are not set (e.g. when going through
1038bcba8eaeSSamuel Ortiz 			 * iwconfig), we have to set them appropriately here.
1039bcba8eaeSSamuel Ortiz 			 */
1040bcba8eaeSSamuel Ortiz 			if (connect->crypto.cipher_group == 0)
1041bcba8eaeSSamuel Ortiz 				connect->crypto.cipher_group = cipher;
1042bcba8eaeSSamuel Ortiz 
1043bcba8eaeSSamuel Ortiz 			if (connect->crypto.n_ciphers_pairwise == 0) {
1044bcba8eaeSSamuel Ortiz 				connect->crypto.n_ciphers_pairwise = 1;
1045bcba8eaeSSamuel Ortiz 				connect->crypto.ciphers_pairwise[0] = cipher;
1046bcba8eaeSSamuel Ortiz 			}
1047fffd0934SJohannes Berg 		}
1048fffd0934SJohannes Berg 	}
1049fffd0934SJohannes Berg 
1050ceca7b71SJohannes Berg 	wdev->connect_keys = connkeys;
10516829c878SJohannes Berg 	memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
10526829c878SJohannes Berg 	wdev->ssid_len = connect->ssid_len;
10536829c878SJohannes Berg 
105434d50519SLior David 	wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
105534d50519SLior David 					      IEEE80211_BSS_TYPE_ESS;
105634d50519SLior David 
1057ceca7b71SJohannes Berg 	if (!rdev->ops->connect)
1058ceca7b71SJohannes Berg 		err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1059ceca7b71SJohannes Berg 	else
1060ceca7b71SJohannes Berg 		err = rdev_connect(rdev, dev, connect);
10616829c878SJohannes Berg 
106219957bb3SJohannes Berg 	if (err) {
1063fffd0934SJohannes Berg 		wdev->connect_keys = NULL;
10648dadadb7SJohannes Berg 		wdev->ssid_len = 0;
1065b23aa676SSamuel Ortiz 		return err;
1066b23aa676SSamuel Ortiz 	}
1067b23aa676SSamuel Ortiz 
1068b23aa676SSamuel Ortiz 	return 0;
1069b23aa676SSamuel Ortiz }
1070b23aa676SSamuel Ortiz 
107183739b03SJohannes Berg int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1072f2129354SJohannes Berg 			struct net_device *dev, u16 reason, bool wextev)
1073b23aa676SSamuel Ortiz {
10746829c878SJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1075dee8a973SJohannes Berg 	int err = 0;
1076b23aa676SSamuel Ortiz 
1077667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
1078667503ddSJohannes Berg 
1079b47f610bSJohannes Berg 	kzfree(wdev->connect_keys);
1080fffd0934SJohannes Berg 	wdev->connect_keys = NULL;
1081fffd0934SJohannes Berg 
1082dee8a973SJohannes Berg 	if (wdev->conn)
1083ceca7b71SJohannes Berg 		err = cfg80211_sme_disconnect(wdev, reason);
1084dee8a973SJohannes Berg 	else if (!rdev->ops->disconnect)
108519957bb3SJohannes Berg 		cfg80211_mlme_down(rdev, dev);
1086dee8a973SJohannes Berg 	else if (wdev->current_bss)
1087e35e4d28SHila Gonen 		err = rdev_disconnect(rdev, dev, reason);
1088ceca7b71SJohannes Berg 
1089b23aa676SSamuel Ortiz 	return err;
1090b23aa676SSamuel Ortiz }
1091