xref: /openbmc/linux/net/wireless/sme.c (revision 818965d3)
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) {
842a84ee86SKarl Beldan 		enum ieee80211_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;
966829c878SJohannes Berg 		enum ieee80211_band band;
97e3081501SRajkumar Manoharan 		struct ieee80211_supported_band *bands;
98e3081501SRajkumar Manoharan 		struct ieee80211_channel *channel;
996829c878SJohannes Berg 
1006829c878SJohannes Berg 		for (band = 0; band < IEEE80211_NUM_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 
122*818965d3SJouni Malinen 	eth_broadcast_addr(request->bssid);
123*818965d3SJouni 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 
495ceca7b71SJohannes Berg 	if (wdev->current_bss)
496ceca7b71SJohannes Berg 		return -EALREADY;
497ceca7b71SJohannes Berg 
498ceca7b71SJohannes Berg 	if (WARN_ON(wdev->conn))
499ceca7b71SJohannes Berg 		return -EINPROGRESS;
500ceca7b71SJohannes Berg 
501ceca7b71SJohannes Berg 	wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
502ceca7b71SJohannes Berg 	if (!wdev->conn)
503ceca7b71SJohannes Berg 		return -ENOMEM;
504ceca7b71SJohannes Berg 
505ceca7b71SJohannes Berg 	/*
506ceca7b71SJohannes Berg 	 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
507ceca7b71SJohannes Berg 	 */
508ceca7b71SJohannes Berg 	memcpy(&wdev->conn->params, connect, sizeof(*connect));
509ceca7b71SJohannes Berg 	if (connect->bssid) {
510ceca7b71SJohannes Berg 		wdev->conn->params.bssid = wdev->conn->bssid;
511ceca7b71SJohannes Berg 		memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
512ceca7b71SJohannes Berg 	}
513ceca7b71SJohannes Berg 
51446b9d180SJohannes Berg 	if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
51546b9d180SJohannes Berg 				      &wdev->conn->ie,
51646b9d180SJohannes Berg 				      &wdev->conn->params.ie_len)) {
517ceca7b71SJohannes Berg 		kfree(wdev->conn);
518ceca7b71SJohannes Berg 		wdev->conn = NULL;
519ceca7b71SJohannes Berg 		return -ENOMEM;
520ceca7b71SJohannes Berg 	}
52146b9d180SJohannes Berg 	wdev->conn->params.ie = wdev->conn->ie;
522ceca7b71SJohannes Berg 
523ceca7b71SJohannes Berg 	if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
524ceca7b71SJohannes Berg 		wdev->conn->auto_auth = true;
525ceca7b71SJohannes Berg 		/* start with open system ... should mostly work */
526ceca7b71SJohannes Berg 		wdev->conn->params.auth_type =
527ceca7b71SJohannes Berg 			NL80211_AUTHTYPE_OPEN_SYSTEM;
528ceca7b71SJohannes Berg 	} else {
529ceca7b71SJohannes Berg 		wdev->conn->auto_auth = false;
530ceca7b71SJohannes Berg 	}
531ceca7b71SJohannes Berg 
532ceca7b71SJohannes Berg 	wdev->conn->params.ssid = wdev->ssid;
533babd3a27SZhao, Gang 	wdev->conn->params.ssid_len = wdev->ssid_len;
534ceca7b71SJohannes Berg 
535ceca7b71SJohannes Berg 	/* see if we have the bss already */
536ceca7b71SJohannes Berg 	bss = cfg80211_get_conn_bss(wdev);
537ceca7b71SJohannes Berg 
538ceca7b71SJohannes Berg 	if (prev_bssid) {
539ceca7b71SJohannes Berg 		memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
540ceca7b71SJohannes Berg 		wdev->conn->prev_bssid_valid = true;
541ceca7b71SJohannes Berg 	}
542ceca7b71SJohannes Berg 
543ceca7b71SJohannes Berg 	/* we're good if we have a matching bss struct */
544ceca7b71SJohannes Berg 	if (bss) {
545ceca7b71SJohannes Berg 		err = cfg80211_conn_do_work(wdev);
546ceca7b71SJohannes Berg 		cfg80211_put_bss(wdev->wiphy, bss);
547ceca7b71SJohannes Berg 	} else {
548ceca7b71SJohannes Berg 		/* otherwise we'll need to scan for the AP first */
549ceca7b71SJohannes Berg 		err = cfg80211_conn_scan(wdev);
550ceca7b71SJohannes Berg 
551ceca7b71SJohannes Berg 		/*
552ceca7b71SJohannes Berg 		 * If we can't scan right now, then we need to scan again
553ceca7b71SJohannes Berg 		 * after the current scan finished, since the parameters
554ceca7b71SJohannes Berg 		 * changed (unless we find a good AP anyway).
555ceca7b71SJohannes Berg 		 */
556ceca7b71SJohannes Berg 		if (err == -EBUSY) {
557ceca7b71SJohannes Berg 			err = 0;
558ceca7b71SJohannes Berg 			wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
559ceca7b71SJohannes Berg 		}
560ceca7b71SJohannes Berg 	}
561ceca7b71SJohannes Berg 
562ceca7b71SJohannes Berg 	if (err)
563ceca7b71SJohannes Berg 		cfg80211_sme_free(wdev);
564ceca7b71SJohannes Berg 
565ceca7b71SJohannes Berg 	return err;
566ceca7b71SJohannes Berg }
567ceca7b71SJohannes Berg 
568ceca7b71SJohannes Berg static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
569ceca7b71SJohannes Berg {
570f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
571ceca7b71SJohannes Berg 	int err;
572ceca7b71SJohannes Berg 
573ceca7b71SJohannes Berg 	if (!wdev->conn)
574ceca7b71SJohannes Berg 		return 0;
575ceca7b71SJohannes Berg 
576ceca7b71SJohannes Berg 	if (!rdev->ops->deauth)
577ceca7b71SJohannes Berg 		return -EOPNOTSUPP;
578ceca7b71SJohannes Berg 
579ceca7b71SJohannes Berg 	if (wdev->conn->state == CFG80211_CONN_SCANNING ||
580ceca7b71SJohannes Berg 	    wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
581ceca7b71SJohannes Berg 		err = 0;
582ceca7b71SJohannes Berg 		goto out;
583ceca7b71SJohannes Berg 	}
584ceca7b71SJohannes Berg 
585ceca7b71SJohannes Berg 	/* wdev->conn->params.bssid must be set if > SCANNING */
586ceca7b71SJohannes Berg 	err = cfg80211_mlme_deauth(rdev, wdev->netdev,
587ceca7b71SJohannes Berg 				   wdev->conn->params.bssid,
588ceca7b71SJohannes Berg 				   NULL, 0, reason, false);
589ceca7b71SJohannes Berg  out:
590ceca7b71SJohannes Berg 	cfg80211_sme_free(wdev);
591ceca7b71SJohannes Berg 	return err;
592ceca7b71SJohannes Berg }
593ceca7b71SJohannes Berg 
594ceca7b71SJohannes Berg /*
595ceca7b71SJohannes Berg  * code shared for in-device and software SME
596ceca7b71SJohannes Berg  */
597ceca7b71SJohannes Berg 
598ceca7b71SJohannes Berg static bool cfg80211_is_all_idle(void)
599ceca7b71SJohannes Berg {
600ceca7b71SJohannes Berg 	struct cfg80211_registered_device *rdev;
601ceca7b71SJohannes Berg 	struct wireless_dev *wdev;
602ceca7b71SJohannes Berg 	bool is_all_idle = true;
603ceca7b71SJohannes Berg 
604ceca7b71SJohannes Berg 	/*
605ceca7b71SJohannes Berg 	 * All devices must be idle as otherwise if you are actively
606ceca7b71SJohannes Berg 	 * scanning some new beacon hints could be learned and would
607ceca7b71SJohannes Berg 	 * count as new regulatory hints.
608ceca7b71SJohannes Berg 	 */
609ceca7b71SJohannes Berg 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
610ceca7b71SJohannes Berg 		list_for_each_entry(wdev, &rdev->wdev_list, list) {
611ceca7b71SJohannes Berg 			wdev_lock(wdev);
612ceca7b71SJohannes Berg 			if (wdev->conn || wdev->current_bss)
613ceca7b71SJohannes Berg 				is_all_idle = false;
614ceca7b71SJohannes Berg 			wdev_unlock(wdev);
615ceca7b71SJohannes Berg 		}
616ceca7b71SJohannes Berg 	}
617ceca7b71SJohannes Berg 
618ceca7b71SJohannes Berg 	return is_all_idle;
619ceca7b71SJohannes Berg }
620ceca7b71SJohannes Berg 
621ceca7b71SJohannes Berg static void disconnect_work(struct work_struct *work)
622ceca7b71SJohannes Berg {
623ceca7b71SJohannes Berg 	rtnl_lock();
624ceca7b71SJohannes Berg 	if (cfg80211_is_all_idle())
625ceca7b71SJohannes Berg 		regulatory_hint_disconnect();
626ceca7b71SJohannes Berg 	rtnl_unlock();
627ceca7b71SJohannes Berg }
628ceca7b71SJohannes Berg 
629ceca7b71SJohannes Berg static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
630ceca7b71SJohannes Berg 
631ceca7b71SJohannes Berg 
632ceca7b71SJohannes Berg /*
633ceca7b71SJohannes Berg  * API calls for drivers implementing connect/disconnect and
634ceca7b71SJohannes Berg  * SME event handling
635ceca7b71SJohannes Berg  */
636ceca7b71SJohannes Berg 
6376f390908SBen Greear /* This method must consume bss one way or another */
638667503ddSJohannes Berg void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
639b23aa676SSamuel Ortiz 			       const u8 *req_ie, size_t req_ie_len,
640b23aa676SSamuel Ortiz 			       const u8 *resp_ie, size_t resp_ie_len,
641df7fc0f9SJohannes Berg 			       u16 status, bool wextev,
642df7fc0f9SJohannes Berg 			       struct cfg80211_bss *bss)
643b23aa676SSamuel Ortiz {
644b23aa676SSamuel Ortiz 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6459caf0364SJohannes Berg 	const u8 *country_ie;
6463d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
647b23aa676SSamuel Ortiz 	union iwreq_data wrqu;
648b23aa676SSamuel Ortiz #endif
649b23aa676SSamuel Ortiz 
650667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
651667503ddSJohannes Berg 
652074ac8dfSJohannes Berg 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
6536f390908SBen Greear 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
6546f390908SBen Greear 		cfg80211_put_bss(wdev->wiphy, bss);
655b23aa676SSamuel Ortiz 		return;
6566f390908SBen Greear 	}
657b23aa676SSamuel Ortiz 
658f26cbf40SZhao, Gang 	nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev,
659e45cd82aSJohannes Berg 				    bssid, req_ie, req_ie_len,
660e45cd82aSJohannes Berg 				    resp_ie, resp_ie_len,
661667503ddSJohannes Berg 				    status, GFP_KERNEL);
662e45cd82aSJohannes Berg 
6633d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
664e45cd82aSJohannes Berg 	if (wextev) {
665e45cd82aSJohannes Berg 		if (req_ie && status == WLAN_STATUS_SUCCESS) {
666e45cd82aSJohannes Berg 			memset(&wrqu, 0, sizeof(wrqu));
667e45cd82aSJohannes Berg 			wrqu.data.length = req_ie_len;
6683409ff77SZhu Yi 			wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
669e45cd82aSJohannes Berg 		}
670e45cd82aSJohannes Berg 
671e45cd82aSJohannes Berg 		if (resp_ie && status == WLAN_STATUS_SUCCESS) {
672e45cd82aSJohannes Berg 			memset(&wrqu, 0, sizeof(wrqu));
673e45cd82aSJohannes Berg 			wrqu.data.length = resp_ie_len;
674e45cd82aSJohannes Berg 			wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
675e45cd82aSJohannes Berg 		}
676e45cd82aSJohannes Berg 
677e45cd82aSJohannes Berg 		memset(&wrqu, 0, sizeof(wrqu));
678e45cd82aSJohannes Berg 		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
679f401a6f7SJohannes Berg 		if (bssid && status == WLAN_STATUS_SUCCESS) {
680e45cd82aSJohannes Berg 			memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
681f401a6f7SJohannes Berg 			memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
682f401a6f7SJohannes Berg 			wdev->wext.prev_bssid_valid = true;
683f401a6f7SJohannes Berg 		}
684e45cd82aSJohannes Berg 		wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
685e45cd82aSJohannes Berg 	}
686e45cd82aSJohannes Berg #endif
687e45cd82aSJohannes Berg 
6884c4d684aSUjjal Roy 	if (!bss && (status == WLAN_STATUS_SUCCESS)) {
689f26cbf40SZhao, Gang 		WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
6904c4d684aSUjjal Roy 		bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
6914c4d684aSUjjal Roy 				       wdev->ssid, wdev->ssid_len,
69234d50519SLior David 				       wdev->conn_bss_type,
6936eb18137SDedy Lansky 				       IEEE80211_PRIVACY_ANY);
6944c4d684aSUjjal Roy 		if (bss)
6954c4d684aSUjjal Roy 			cfg80211_hold_bss(bss_from_pub(bss));
6964c4d684aSUjjal Roy 	}
6974c4d684aSUjjal Roy 
698df7fc0f9SJohannes Berg 	if (wdev->current_bss) {
699df7fc0f9SJohannes Berg 		cfg80211_unhold_bss(wdev->current_bss);
7005b112d3dSJohannes Berg 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
701df7fc0f9SJohannes Berg 		wdev->current_bss = NULL;
702df7fc0f9SJohannes Berg 	}
703df7fc0f9SJohannes Berg 
704fffd0934SJohannes Berg 	if (status != WLAN_STATUS_SUCCESS) {
705b47f610bSJohannes Berg 		kzfree(wdev->connect_keys);
706fffd0934SJohannes Berg 		wdev->connect_keys = NULL;
7078dadadb7SJohannes Berg 		wdev->ssid_len = 0;
708f1940c57SJohannes Berg 		if (bss) {
709f1940c57SJohannes Berg 			cfg80211_unhold_bss(bss_from_pub(bss));
7105b112d3dSJohannes Berg 			cfg80211_put_bss(wdev->wiphy, bss);
711f1940c57SJohannes Berg 		}
712c1fbb258SEliad Peller 		cfg80211_sme_free(wdev);
713fffd0934SJohannes Berg 		return;
714fffd0934SJohannes Berg 	}
715fffd0934SJohannes Berg 
716b23aa676SSamuel Ortiz 	if (WARN_ON(!bss))
717b23aa676SSamuel Ortiz 		return;
718f1940c57SJohannes Berg 
71919957bb3SJohannes Berg 	wdev->current_bss = bss_from_pub(bss);
720b23aa676SSamuel Ortiz 
721fffd0934SJohannes Berg 	cfg80211_upload_connect_keys(wdev);
7228b19e6caSLuis R. Rodriguez 
7239caf0364SJohannes Berg 	rcu_read_lock();
7249caf0364SJohannes Berg 	country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
7259caf0364SJohannes Berg 	if (!country_ie) {
7269caf0364SJohannes Berg 		rcu_read_unlock();
7279caf0364SJohannes Berg 		return;
7289caf0364SJohannes Berg 	}
7299caf0364SJohannes Berg 
7309caf0364SJohannes Berg 	country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
7319caf0364SJohannes Berg 	rcu_read_unlock();
7328b19e6caSLuis R. Rodriguez 
7338b19e6caSLuis R. Rodriguez 	if (!country_ie)
7348b19e6caSLuis R. Rodriguez 		return;
7358b19e6caSLuis R. Rodriguez 
7368b19e6caSLuis R. Rodriguez 	/*
7378b19e6caSLuis R. Rodriguez 	 * ieee80211_bss_get_ie() ensures we can access:
7388b19e6caSLuis R. Rodriguez 	 * - country_ie + 2, the start of the country ie data, and
7398b19e6caSLuis R. Rodriguez 	 * - and country_ie[1] which is the IE length
7408b19e6caSLuis R. Rodriguez 	 */
741789fd033SLuis R. Rodriguez 	regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
7421a919318SJohannes Berg 				   country_ie + 2, country_ie[1]);
7439caf0364SJohannes Berg 	kfree(country_ie);
744b23aa676SSamuel Ortiz }
745f2129354SJohannes Berg 
746f2129354SJohannes Berg void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
747f2129354SJohannes Berg 			     const u8 *req_ie, size_t req_ie_len,
748f2129354SJohannes Berg 			     const u8 *resp_ie, size_t resp_ie_len,
749f2129354SJohannes Berg 			     u16 status, gfp_t gfp)
750f2129354SJohannes Berg {
751667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
752f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
753667503ddSJohannes Berg 	struct cfg80211_event *ev;
754667503ddSJohannes Berg 	unsigned long flags;
755667503ddSJohannes Berg 
756667503ddSJohannes Berg 	ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
757667503ddSJohannes Berg 	if (!ev)
758667503ddSJohannes Berg 		return;
759667503ddSJohannes Berg 
760667503ddSJohannes Berg 	ev->type = EVENT_CONNECT_RESULT;
76116a832e7SZhu Yi 	if (bssid)
762667503ddSJohannes Berg 		memcpy(ev->cr.bssid, bssid, ETH_ALEN);
7637834704bSNishant Sarmukadam 	if (req_ie_len) {
764667503ddSJohannes Berg 		ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
765667503ddSJohannes Berg 		ev->cr.req_ie_len = req_ie_len;
766667503ddSJohannes Berg 		memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
7677834704bSNishant Sarmukadam 	}
7687834704bSNishant Sarmukadam 	if (resp_ie_len) {
769667503ddSJohannes Berg 		ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
770667503ddSJohannes Berg 		ev->cr.resp_ie_len = resp_ie_len;
771667503ddSJohannes Berg 		memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
7727834704bSNishant Sarmukadam 	}
773667503ddSJohannes Berg 	ev->cr.status = status;
774667503ddSJohannes Berg 
775667503ddSJohannes Berg 	spin_lock_irqsave(&wdev->event_lock, flags);
776667503ddSJohannes Berg 	list_add_tail(&ev->list, &wdev->event_list);
777667503ddSJohannes Berg 	spin_unlock_irqrestore(&wdev->event_lock, flags);
778e60d7443SAlban Browaeys 	queue_work(cfg80211_wq, &rdev->event_work);
779f2129354SJohannes Berg }
780b23aa676SSamuel Ortiz EXPORT_SYMBOL(cfg80211_connect_result);
781b23aa676SSamuel Ortiz 
7820e3a39b5SBen Greear /* Consumes bss object one way or another */
783ed9d0102SJouni Malinen void __cfg80211_roamed(struct wireless_dev *wdev,
784adbde344SVasanthakumar Thiagarajan 		       struct cfg80211_bss *bss,
785b23aa676SSamuel Ortiz 		       const u8 *req_ie, size_t req_ie_len,
786667503ddSJohannes Berg 		       const u8 *resp_ie, size_t resp_ie_len)
787b23aa676SSamuel Ortiz {
7883d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
789b23aa676SSamuel Ortiz 	union iwreq_data wrqu;
790b23aa676SSamuel Ortiz #endif
791667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
792667503ddSJohannes Berg 
793074ac8dfSJohannes Berg 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
794074ac8dfSJohannes Berg 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
795adbde344SVasanthakumar Thiagarajan 		goto out;
796b23aa676SSamuel Ortiz 
797ceca7b71SJohannes Berg 	if (WARN_ON(!wdev->current_bss))
798adbde344SVasanthakumar Thiagarajan 		goto out;
799b23aa676SSamuel Ortiz 
800b23aa676SSamuel Ortiz 	cfg80211_unhold_bss(wdev->current_bss);
8015b112d3dSJohannes Berg 	cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
802b23aa676SSamuel Ortiz 	wdev->current_bss = NULL;
803b23aa676SSamuel Ortiz 
80419957bb3SJohannes Berg 	cfg80211_hold_bss(bss_from_pub(bss));
80519957bb3SJohannes Berg 	wdev->current_bss = bss_from_pub(bss);
806b23aa676SSamuel Ortiz 
807f26cbf40SZhao, Gang 	nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
808f26cbf40SZhao, Gang 			    wdev->netdev, bss->bssid,
809667503ddSJohannes Berg 			    req_ie, req_ie_len, resp_ie, resp_ie_len,
810667503ddSJohannes Berg 			    GFP_KERNEL);
811b23aa676SSamuel Ortiz 
8123d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
813b23aa676SSamuel Ortiz 	if (req_ie) {
814b23aa676SSamuel Ortiz 		memset(&wrqu, 0, sizeof(wrqu));
815b23aa676SSamuel Ortiz 		wrqu.data.length = req_ie_len;
8163409ff77SZhu Yi 		wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
817667503ddSJohannes Berg 				    &wrqu, req_ie);
818b23aa676SSamuel Ortiz 	}
819b23aa676SSamuel Ortiz 
820b23aa676SSamuel Ortiz 	if (resp_ie) {
821b23aa676SSamuel Ortiz 		memset(&wrqu, 0, sizeof(wrqu));
822b23aa676SSamuel Ortiz 		wrqu.data.length = resp_ie_len;
823667503ddSJohannes Berg 		wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
824667503ddSJohannes Berg 				    &wrqu, resp_ie);
825b23aa676SSamuel Ortiz 	}
826b23aa676SSamuel Ortiz 
827b23aa676SSamuel Ortiz 	memset(&wrqu, 0, sizeof(wrqu));
828b23aa676SSamuel Ortiz 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
829adbde344SVasanthakumar Thiagarajan 	memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
830adbde344SVasanthakumar Thiagarajan 	memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
831f401a6f7SJohannes Berg 	wdev->wext.prev_bssid_valid = true;
832667503ddSJohannes Berg 	wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
833b23aa676SSamuel Ortiz #endif
834adbde344SVasanthakumar Thiagarajan 
835adbde344SVasanthakumar Thiagarajan 	return;
836adbde344SVasanthakumar Thiagarajan out:
8375b112d3dSJohannes Berg 	cfg80211_put_bss(wdev->wiphy, bss);
838b23aa676SSamuel Ortiz }
839667503ddSJohannes Berg 
840ed9d0102SJouni Malinen void cfg80211_roamed(struct net_device *dev,
841ed9d0102SJouni Malinen 		     struct ieee80211_channel *channel,
842ed9d0102SJouni Malinen 		     const u8 *bssid,
843667503ddSJohannes Berg 		     const u8 *req_ie, size_t req_ie_len,
844667503ddSJohannes Berg 		     const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
845667503ddSJohannes Berg {
846667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
847adbde344SVasanthakumar Thiagarajan 	struct cfg80211_bss *bss;
848adbde344SVasanthakumar Thiagarajan 
849adbde344SVasanthakumar Thiagarajan 	bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
8506eb18137SDedy Lansky 			       wdev->ssid_len,
85134d50519SLior David 			       wdev->conn_bss_type, IEEE80211_PRIVACY_ANY);
852adbde344SVasanthakumar Thiagarajan 	if (WARN_ON(!bss))
853adbde344SVasanthakumar Thiagarajan 		return;
854adbde344SVasanthakumar Thiagarajan 
855adbde344SVasanthakumar Thiagarajan 	cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
856adbde344SVasanthakumar Thiagarajan 			    resp_ie_len, gfp);
857adbde344SVasanthakumar Thiagarajan }
858adbde344SVasanthakumar Thiagarajan EXPORT_SYMBOL(cfg80211_roamed);
859adbde344SVasanthakumar Thiagarajan 
8600e3a39b5SBen Greear /* Consumes bss object one way or another */
861adbde344SVasanthakumar Thiagarajan void cfg80211_roamed_bss(struct net_device *dev,
862adbde344SVasanthakumar Thiagarajan 			 struct cfg80211_bss *bss, const u8 *req_ie,
863adbde344SVasanthakumar Thiagarajan 			 size_t req_ie_len, const u8 *resp_ie,
864adbde344SVasanthakumar Thiagarajan 			 size_t resp_ie_len, gfp_t gfp)
865adbde344SVasanthakumar Thiagarajan {
866adbde344SVasanthakumar Thiagarajan 	struct wireless_dev *wdev = dev->ieee80211_ptr;
867f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
868667503ddSJohannes Berg 	struct cfg80211_event *ev;
869667503ddSJohannes Berg 	unsigned long flags;
870667503ddSJohannes Berg 
871adbde344SVasanthakumar Thiagarajan 	if (WARN_ON(!bss))
872667503ddSJohannes Berg 		return;
873667503ddSJohannes Berg 
874adbde344SVasanthakumar Thiagarajan 	ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
875adbde344SVasanthakumar Thiagarajan 	if (!ev) {
8765b112d3dSJohannes Berg 		cfg80211_put_bss(wdev->wiphy, bss);
877adbde344SVasanthakumar Thiagarajan 		return;
878adbde344SVasanthakumar Thiagarajan 	}
879adbde344SVasanthakumar Thiagarajan 
880667503ddSJohannes Berg 	ev->type = EVENT_ROAMED;
881667503ddSJohannes Berg 	ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
882667503ddSJohannes Berg 	ev->rm.req_ie_len = req_ie_len;
883667503ddSJohannes Berg 	memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
884667503ddSJohannes Berg 	ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
885667503ddSJohannes Berg 	ev->rm.resp_ie_len = resp_ie_len;
886667503ddSJohannes Berg 	memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
887adbde344SVasanthakumar Thiagarajan 	ev->rm.bss = bss;
888667503ddSJohannes Berg 
889667503ddSJohannes Berg 	spin_lock_irqsave(&wdev->event_lock, flags);
890667503ddSJohannes Berg 	list_add_tail(&ev->list, &wdev->event_list);
891667503ddSJohannes Berg 	spin_unlock_irqrestore(&wdev->event_lock, flags);
892e60d7443SAlban Browaeys 	queue_work(cfg80211_wq, &rdev->event_work);
893667503ddSJohannes Berg }
894adbde344SVasanthakumar Thiagarajan EXPORT_SYMBOL(cfg80211_roamed_bss);
895b23aa676SSamuel Ortiz 
896667503ddSJohannes Berg void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
8976829c878SJohannes Berg 			     size_t ie_len, u16 reason, bool from_ap)
898b23aa676SSamuel Ortiz {
899b23aa676SSamuel Ortiz 	struct wireless_dev *wdev = dev->ieee80211_ptr;
900f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
901fffd0934SJohannes Berg 	int i;
9023d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
903b23aa676SSamuel Ortiz 	union iwreq_data wrqu;
904b23aa676SSamuel Ortiz #endif
905b23aa676SSamuel Ortiz 
906667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
907667503ddSJohannes Berg 
908074ac8dfSJohannes Berg 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
909074ac8dfSJohannes Berg 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
910b23aa676SSamuel Ortiz 		return;
911b23aa676SSamuel Ortiz 
912b23aa676SSamuel Ortiz 	if (wdev->current_bss) {
913b23aa676SSamuel Ortiz 		cfg80211_unhold_bss(wdev->current_bss);
9145b112d3dSJohannes Berg 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
915b23aa676SSamuel Ortiz 	}
916b23aa676SSamuel Ortiz 
917b23aa676SSamuel Ortiz 	wdev->current_bss = NULL;
9188dadadb7SJohannes Berg 	wdev->ssid_len = 0;
919b23aa676SSamuel Ortiz 
920fffd0934SJohannes Berg 	nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
921fffd0934SJohannes Berg 
922b8607152SArend van Spriel 	/* stop critical protocol if supported */
923b8607152SArend van Spriel 	if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
924b8607152SArend van Spriel 		rdev->crit_proto_nlportid = 0;
925b8607152SArend van Spriel 		rdev_crit_proto_stop(rdev, wdev);
926b8607152SArend van Spriel 	}
927b8607152SArend van Spriel 
928fffd0934SJohannes Berg 	/*
929fffd0934SJohannes Berg 	 * Delete all the keys ... pairwise keys can't really
930fffd0934SJohannes Berg 	 * exist any more anyway, but default keys might.
931fffd0934SJohannes Berg 	 */
932fffd0934SJohannes Berg 	if (rdev->ops->del_key)
933fffd0934SJohannes Berg 		for (i = 0; i < 6; i++)
934e35e4d28SHila Gonen 			rdev_del_key(rdev, dev, i, false, NULL);
935b23aa676SSamuel Ortiz 
936fa9ffc74SKyeyoon Park 	rdev_set_qos_map(rdev, dev, NULL);
937fa9ffc74SKyeyoon Park 
9383d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
939b23aa676SSamuel Ortiz 	memset(&wrqu, 0, sizeof(wrqu));
940b23aa676SSamuel Ortiz 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
941b23aa676SSamuel Ortiz 	wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
9425f612033SAbhijeet Kolekar 	wdev->wext.connect.ssid_len = 0;
943b23aa676SSamuel Ortiz #endif
94409d989d1SLuis R. Rodriguez 
94509d989d1SLuis R. Rodriguez 	schedule_work(&cfg80211_disconnect_work);
946b23aa676SSamuel Ortiz }
947b23aa676SSamuel Ortiz 
948b23aa676SSamuel Ortiz void cfg80211_disconnected(struct net_device *dev, u16 reason,
94980279fb7SJohannes Berg 			   const u8 *ie, size_t ie_len,
95080279fb7SJohannes Berg 			   bool locally_generated, gfp_t gfp)
951b23aa676SSamuel Ortiz {
952667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
953f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
954667503ddSJohannes Berg 	struct cfg80211_event *ev;
955667503ddSJohannes Berg 	unsigned long flags;
956667503ddSJohannes Berg 
957667503ddSJohannes Berg 	ev = kzalloc(sizeof(*ev) + ie_len, gfp);
958667503ddSJohannes Berg 	if (!ev)
959667503ddSJohannes Berg 		return;
960667503ddSJohannes Berg 
961667503ddSJohannes Berg 	ev->type = EVENT_DISCONNECTED;
962667503ddSJohannes Berg 	ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
963667503ddSJohannes Berg 	ev->dc.ie_len = ie_len;
964667503ddSJohannes Berg 	memcpy((void *)ev->dc.ie, ie, ie_len);
965667503ddSJohannes Berg 	ev->dc.reason = reason;
96680279fb7SJohannes Berg 	ev->dc.locally_generated = locally_generated;
967667503ddSJohannes Berg 
968667503ddSJohannes Berg 	spin_lock_irqsave(&wdev->event_lock, flags);
969667503ddSJohannes Berg 	list_add_tail(&ev->list, &wdev->event_list);
970667503ddSJohannes Berg 	spin_unlock_irqrestore(&wdev->event_lock, flags);
971e60d7443SAlban Browaeys 	queue_work(cfg80211_wq, &rdev->event_work);
972b23aa676SSamuel Ortiz }
973b23aa676SSamuel Ortiz EXPORT_SYMBOL(cfg80211_disconnected);
974b23aa676SSamuel Ortiz 
975ceca7b71SJohannes Berg /*
976ceca7b71SJohannes Berg  * API calls for nl80211/wext compatibility code
977ceca7b71SJohannes Berg  */
97883739b03SJohannes Berg int cfg80211_connect(struct cfg80211_registered_device *rdev,
979b23aa676SSamuel Ortiz 		     struct net_device *dev,
980fffd0934SJohannes Berg 		     struct cfg80211_connect_params *connect,
981f401a6f7SJohannes Berg 		     struct cfg80211_cached_keys *connkeys,
982f401a6f7SJohannes Berg 		     const u8 *prev_bssid)
983b23aa676SSamuel Ortiz {
984b23aa676SSamuel Ortiz 	struct wireless_dev *wdev = dev->ieee80211_ptr;
985667503ddSJohannes Berg 	int err;
986667503ddSJohannes Berg 
987667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
988b23aa676SSamuel Ortiz 
989fffd0934SJohannes Berg 	if (WARN_ON(wdev->connect_keys)) {
990b47f610bSJohannes Berg 		kzfree(wdev->connect_keys);
991fffd0934SJohannes Berg 		wdev->connect_keys = NULL;
992fffd0934SJohannes Berg 	}
993fffd0934SJohannes Berg 
9947e7c8926SBen Greear 	cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
9957e7c8926SBen Greear 				  rdev->wiphy.ht_capa_mod_mask);
9967e7c8926SBen Greear 
997fffd0934SJohannes Berg 	if (connkeys && connkeys->def >= 0) {
998fffd0934SJohannes Berg 		int idx;
999bcba8eaeSSamuel Ortiz 		u32 cipher;
1000fffd0934SJohannes Berg 
1001fffd0934SJohannes Berg 		idx = connkeys->def;
1002bcba8eaeSSamuel Ortiz 		cipher = connkeys->params[idx].cipher;
1003fffd0934SJohannes Berg 		/* If given a WEP key we may need it for shared key auth */
1004bcba8eaeSSamuel Ortiz 		if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1005bcba8eaeSSamuel Ortiz 		    cipher == WLAN_CIPHER_SUITE_WEP104) {
1006fffd0934SJohannes Berg 			connect->key_idx = idx;
1007fffd0934SJohannes Berg 			connect->key = connkeys->params[idx].key;
1008fffd0934SJohannes Berg 			connect->key_len = connkeys->params[idx].key_len;
1009bcba8eaeSSamuel Ortiz 
1010bcba8eaeSSamuel Ortiz 			/*
1011bcba8eaeSSamuel Ortiz 			 * If ciphers are not set (e.g. when going through
1012bcba8eaeSSamuel Ortiz 			 * iwconfig), we have to set them appropriately here.
1013bcba8eaeSSamuel Ortiz 			 */
1014bcba8eaeSSamuel Ortiz 			if (connect->crypto.cipher_group == 0)
1015bcba8eaeSSamuel Ortiz 				connect->crypto.cipher_group = cipher;
1016bcba8eaeSSamuel Ortiz 
1017bcba8eaeSSamuel Ortiz 			if (connect->crypto.n_ciphers_pairwise == 0) {
1018bcba8eaeSSamuel Ortiz 				connect->crypto.n_ciphers_pairwise = 1;
1019bcba8eaeSSamuel Ortiz 				connect->crypto.ciphers_pairwise[0] = cipher;
1020bcba8eaeSSamuel Ortiz 			}
1021fffd0934SJohannes Berg 		}
1022fffd0934SJohannes Berg 	}
1023fffd0934SJohannes Berg 
1024ceca7b71SJohannes Berg 	wdev->connect_keys = connkeys;
10256829c878SJohannes Berg 	memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
10266829c878SJohannes Berg 	wdev->ssid_len = connect->ssid_len;
10276829c878SJohannes Berg 
102834d50519SLior David 	wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
102934d50519SLior David 					      IEEE80211_BSS_TYPE_ESS;
103034d50519SLior David 
1031ceca7b71SJohannes Berg 	if (!rdev->ops->connect)
1032ceca7b71SJohannes Berg 		err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1033ceca7b71SJohannes Berg 	else
1034ceca7b71SJohannes Berg 		err = rdev_connect(rdev, dev, connect);
10356829c878SJohannes Berg 
103619957bb3SJohannes Berg 	if (err) {
1037fffd0934SJohannes Berg 		wdev->connect_keys = NULL;
10388dadadb7SJohannes Berg 		wdev->ssid_len = 0;
1039b23aa676SSamuel Ortiz 		return err;
1040b23aa676SSamuel Ortiz 	}
1041b23aa676SSamuel Ortiz 
1042b23aa676SSamuel Ortiz 	return 0;
1043b23aa676SSamuel Ortiz }
1044b23aa676SSamuel Ortiz 
104583739b03SJohannes Berg int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1046f2129354SJohannes Berg 			struct net_device *dev, u16 reason, bool wextev)
1047b23aa676SSamuel Ortiz {
10486829c878SJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1049dee8a973SJohannes Berg 	int err = 0;
1050b23aa676SSamuel Ortiz 
1051667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
1052667503ddSJohannes Berg 
1053b47f610bSJohannes Berg 	kzfree(wdev->connect_keys);
1054fffd0934SJohannes Berg 	wdev->connect_keys = NULL;
1055fffd0934SJohannes Berg 
1056dee8a973SJohannes Berg 	if (wdev->conn)
1057ceca7b71SJohannes Berg 		err = cfg80211_sme_disconnect(wdev, reason);
1058dee8a973SJohannes Berg 	else if (!rdev->ops->disconnect)
105919957bb3SJohannes Berg 		cfg80211_mlme_down(rdev, dev);
1060dee8a973SJohannes Berg 	else if (wdev->current_bss)
1061e35e4d28SHila Gonen 		err = rdev_disconnect(rdev, dev, reason);
1062ceca7b71SJohannes Berg 
1063b23aa676SSamuel Ortiz 	return err;
1064b23aa676SSamuel Ortiz }
1065