xref: /openbmc/linux/net/wireless/sme.c (revision 80279fb7)
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 
122fd014284SJohannes Berg 	request->wdev = wdev;
12379c97e97SJohannes Berg 	request->wiphy = &rdev->wiphy;
12415d6030bSSam Leffler 	request->scan_start = jiffies;
1256829c878SJohannes Berg 
12679c97e97SJohannes Berg 	rdev->scan_req = request;
1276829c878SJohannes Berg 
128e35e4d28SHila Gonen 	err = rdev_scan(rdev, request);
1296829c878SJohannes Berg 	if (!err) {
1306829c878SJohannes Berg 		wdev->conn->state = CFG80211_CONN_SCANNING;
131fd014284SJohannes Berg 		nl80211_send_scan_start(rdev, wdev);
132463d0183SJohannes Berg 		dev_hold(wdev->netdev);
1336829c878SJohannes Berg 	} else {
13479c97e97SJohannes Berg 		rdev->scan_req = NULL;
1356829c878SJohannes Berg 		kfree(request);
1366829c878SJohannes Berg 	}
1376829c878SJohannes Berg 	return err;
1386829c878SJohannes Berg }
1396829c878SJohannes Berg 
1406829c878SJohannes Berg static int cfg80211_conn_do_work(struct wireless_dev *wdev)
1416829c878SJohannes Berg {
142f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14319957bb3SJohannes Berg 	struct cfg80211_connect_params *params;
144f62fab73SJohannes Berg 	struct cfg80211_assoc_request req = {};
14519957bb3SJohannes Berg 	int err;
1466829c878SJohannes Berg 
147667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
148667503ddSJohannes Berg 
1496829c878SJohannes Berg 	if (!wdev->conn)
1506829c878SJohannes Berg 		return 0;
1516829c878SJohannes Berg 
15219957bb3SJohannes Berg 	params = &wdev->conn->params;
15319957bb3SJohannes Berg 
1546829c878SJohannes Berg 	switch (wdev->conn->state) {
155ceca7b71SJohannes Berg 	case CFG80211_CONN_SCANNING:
156ceca7b71SJohannes Berg 		/* didn't find it during scan ... */
157ceca7b71SJohannes Berg 		return -ENOENT;
1586829c878SJohannes Berg 	case CFG80211_CONN_SCAN_AGAIN:
1596829c878SJohannes Berg 		return cfg80211_conn_scan(wdev);
1606829c878SJohannes Berg 	case CFG80211_CONN_AUTHENTICATE_NEXT:
1612fd05115SJohannes Berg 		if (WARN_ON(!rdev->ops->auth))
1622fd05115SJohannes Berg 			return -EOPNOTSUPP;
16319957bb3SJohannes Berg 		wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
16491bf9b26SJohannes Berg 		return cfg80211_mlme_auth(rdev, wdev->netdev,
16519957bb3SJohannes Berg 					  params->channel, params->auth_type,
16619957bb3SJohannes Berg 					  params->bssid,
16719957bb3SJohannes Berg 					  params->ssid, params->ssid_len,
168fffd0934SJohannes Berg 					  NULL, 0,
169fffd0934SJohannes Berg 					  params->key, params->key_len,
170e39e5b5eSJouni Malinen 					  params->key_idx, NULL, 0);
171923a0e7dSJohannes Berg 	case CFG80211_CONN_AUTH_FAILED:
172923a0e7dSJohannes Berg 		return -ENOTCONN;
1736829c878SJohannes Berg 	case CFG80211_CONN_ASSOCIATE_NEXT:
1742fd05115SJohannes Berg 		if (WARN_ON(!rdev->ops->assoc))
1752fd05115SJohannes Berg 			return -EOPNOTSUPP;
17619957bb3SJohannes Berg 		wdev->conn->state = CFG80211_CONN_ASSOCIATING;
177f401a6f7SJohannes Berg 		if (wdev->conn->prev_bssid_valid)
178f62fab73SJohannes Berg 			req.prev_bssid = wdev->conn->prev_bssid;
179f62fab73SJohannes Berg 		req.ie = params->ie;
180f62fab73SJohannes Berg 		req.ie_len = params->ie_len;
181f62fab73SJohannes Berg 		req.use_mfp = params->mfp != NL80211_MFP_NO;
182f62fab73SJohannes Berg 		req.crypto = params->crypto;
183f62fab73SJohannes Berg 		req.flags = params->flags;
184f62fab73SJohannes Berg 		req.ht_capa = params->ht_capa;
185f62fab73SJohannes Berg 		req.ht_capa_mask = params->ht_capa_mask;
186f62fab73SJohannes Berg 		req.vht_capa = params->vht_capa;
187f62fab73SJohannes Berg 		req.vht_capa_mask = params->vht_capa_mask;
188f62fab73SJohannes Berg 
18991bf9b26SJohannes Berg 		err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
190f62fab73SJohannes Berg 					  params->bssid, params->ssid,
191f62fab73SJohannes Berg 					  params->ssid_len, &req);
19219957bb3SJohannes Berg 		if (err)
19391bf9b26SJohannes Berg 			cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
194667503ddSJohannes Berg 					     NULL, 0,
195d5cdfacbSJouni Malinen 					     WLAN_REASON_DEAUTH_LEAVING,
196d5cdfacbSJouni Malinen 					     false);
19719957bb3SJohannes Berg 		return err;
198923a0e7dSJohannes Berg 	case CFG80211_CONN_ASSOC_FAILED:
199923a0e7dSJohannes Berg 		cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
200923a0e7dSJohannes Berg 				     NULL, 0,
201923a0e7dSJohannes Berg 				     WLAN_REASON_DEAUTH_LEAVING, false);
202923a0e7dSJohannes Berg 		return -ENOTCONN;
203ceca7b71SJohannes Berg 	case CFG80211_CONN_DEAUTH:
20491bf9b26SJohannes Berg 		cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
2057d930bc3SJohannes Berg 				     NULL, 0,
206d5cdfacbSJouni Malinen 				     WLAN_REASON_DEAUTH_LEAVING, false);
207923a0e7dSJohannes Berg 		/* free directly, disconnected event already sent */
208923a0e7dSJohannes Berg 		cfg80211_sme_free(wdev);
209ceca7b71SJohannes Berg 		return 0;
2106829c878SJohannes Berg 	default:
2116829c878SJohannes Berg 		return 0;
2126829c878SJohannes Berg 	}
2136829c878SJohannes Berg }
2146829c878SJohannes Berg 
2156829c878SJohannes Berg void cfg80211_conn_work(struct work_struct *work)
2166829c878SJohannes Berg {
21779c97e97SJohannes Berg 	struct cfg80211_registered_device *rdev =
2186829c878SJohannes Berg 		container_of(work, struct cfg80211_registered_device, conn_work);
2196829c878SJohannes Berg 	struct wireless_dev *wdev;
2207400f42eSJohannes Berg 	u8 bssid_buf[ETH_ALEN], *bssid = NULL;
2216829c878SJohannes Berg 
2226829c878SJohannes Berg 	rtnl_lock();
2236829c878SJohannes Berg 
22489a54e48SJohannes Berg 	list_for_each_entry(wdev, &rdev->wdev_list, list) {
225c8157976SJohannes Berg 		if (!wdev->netdev)
226c8157976SJohannes Berg 			continue;
227c8157976SJohannes Berg 
228667503ddSJohannes Berg 		wdev_lock(wdev);
229667503ddSJohannes Berg 		if (!netif_running(wdev->netdev)) {
230667503ddSJohannes Berg 			wdev_unlock(wdev);
2316829c878SJohannes Berg 			continue;
232667503ddSJohannes Berg 		}
233ceca7b71SJohannes Berg 		if (!wdev->conn ||
234ceca7b71SJohannes Berg 		    wdev->conn->state == CFG80211_CONN_CONNECTED) {
235667503ddSJohannes Berg 			wdev_unlock(wdev);
2366829c878SJohannes Berg 			continue;
237667503ddSJohannes Berg 		}
2387400f42eSJohannes Berg 		if (wdev->conn->params.bssid) {
2397400f42eSJohannes Berg 			memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
2407400f42eSJohannes Berg 			bssid = bssid_buf;
2417400f42eSJohannes Berg 		}
242ceca7b71SJohannes Berg 		if (cfg80211_conn_do_work(wdev)) {
243667503ddSJohannes Berg 			__cfg80211_connect_result(
2447d930bc3SJohannes Berg 					wdev->netdev, bssid,
2456829c878SJohannes Berg 					NULL, 0, NULL, 0,
2466829c878SJohannes Berg 					WLAN_STATUS_UNSPECIFIED_FAILURE,
247df7fc0f9SJohannes Berg 					false, NULL);
248ceca7b71SJohannes Berg 		}
249667503ddSJohannes Berg 		wdev_unlock(wdev);
2506829c878SJohannes Berg 	}
2516829c878SJohannes Berg 
2526829c878SJohannes Berg 	rtnl_unlock();
2536829c878SJohannes Berg }
2546829c878SJohannes Berg 
2550e3a39b5SBen Greear /* Returned bss is reference counted and must be cleaned up appropriately. */
256bbac31f4SJohannes Berg static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
2576829c878SJohannes Berg {
258f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
2596829c878SJohannes Berg 	struct cfg80211_bss *bss;
2606829c878SJohannes Berg 
261667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
262667503ddSJohannes Berg 
263ed9d0102SJouni Malinen 	bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
264ed9d0102SJouni Malinen 			       wdev->conn->params.bssid,
2656829c878SJohannes Berg 			       wdev->conn->params.ssid,
2666829c878SJohannes Berg 			       wdev->conn->params.ssid_len,
2676eb18137SDedy Lansky 			       IEEE80211_BSS_TYPE_ESS,
2686eb18137SDedy Lansky 			       IEEE80211_PRIVACY(wdev->conn->params.privacy));
2696829c878SJohannes Berg 	if (!bss)
270bbac31f4SJohannes Berg 		return NULL;
2716829c878SJohannes Berg 
2726829c878SJohannes Berg 	memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
2736829c878SJohannes Berg 	wdev->conn->params.bssid = wdev->conn->bssid;
2746829c878SJohannes Berg 	wdev->conn->params.channel = bss->channel;
2756829c878SJohannes Berg 	wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
27679c97e97SJohannes Berg 	schedule_work(&rdev->conn_work);
2776829c878SJohannes Berg 
278bbac31f4SJohannes Berg 	return bss;
2796829c878SJohannes Berg }
2806829c878SJohannes Berg 
281667503ddSJohannes Berg static void __cfg80211_sme_scan_done(struct net_device *dev)
2826829c878SJohannes Berg {
2836829c878SJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
284f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
285bbac31f4SJohannes Berg 	struct cfg80211_bss *bss;
2866829c878SJohannes Berg 
287667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
288667503ddSJohannes Berg 
289d4b1a687SZhu Yi 	if (!wdev->conn)
2906829c878SJohannes Berg 		return;
2916829c878SJohannes Berg 
2926829c878SJohannes Berg 	if (wdev->conn->state != CFG80211_CONN_SCANNING &&
2936829c878SJohannes Berg 	    wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
2946829c878SJohannes Berg 		return;
2956829c878SJohannes Berg 
296bbac31f4SJohannes Berg 	bss = cfg80211_get_conn_bss(wdev);
297ceca7b71SJohannes Berg 	if (bss)
2985b112d3dSJohannes Berg 		cfg80211_put_bss(&rdev->wiphy, bss);
2996829c878SJohannes Berg 	else
300ceca7b71SJohannes Berg 		schedule_work(&rdev->conn_work);
3016829c878SJohannes Berg }
3026829c878SJohannes Berg 
303667503ddSJohannes Berg void cfg80211_sme_scan_done(struct net_device *dev)
304667503ddSJohannes Berg {
305667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
306667503ddSJohannes Berg 
307667503ddSJohannes Berg 	wdev_lock(wdev);
308667503ddSJohannes Berg 	__cfg80211_sme_scan_done(dev);
309667503ddSJohannes Berg 	wdev_unlock(wdev);
310667503ddSJohannes Berg }
311667503ddSJohannes Berg 
312ceca7b71SJohannes Berg void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
3136829c878SJohannes Berg {
3146829c878SJohannes Berg 	struct wiphy *wiphy = wdev->wiphy;
315f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
3166829c878SJohannes Berg 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
3176829c878SJohannes Berg 	u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
3186829c878SJohannes Berg 
319667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
320667503ddSJohannes Berg 
321ceca7b71SJohannes Berg 	if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
3226829c878SJohannes Berg 		return;
3236829c878SJohannes Berg 
3246829c878SJohannes Berg 	if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
3256829c878SJohannes Berg 	    wdev->conn->auto_auth &&
3266829c878SJohannes Berg 	    wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
3276829c878SJohannes Berg 		/* select automatically between only open, shared, leap */
3286829c878SJohannes Berg 		switch (wdev->conn->params.auth_type) {
3296829c878SJohannes Berg 		case NL80211_AUTHTYPE_OPEN_SYSTEM:
330fffd0934SJohannes Berg 			if (wdev->connect_keys)
3316829c878SJohannes Berg 				wdev->conn->params.auth_type =
3326829c878SJohannes Berg 					NL80211_AUTHTYPE_SHARED_KEY;
333fffd0934SJohannes Berg 			else
334fffd0934SJohannes Berg 				wdev->conn->params.auth_type =
335fffd0934SJohannes Berg 					NL80211_AUTHTYPE_NETWORK_EAP;
3366829c878SJohannes Berg 			break;
3376829c878SJohannes Berg 		case NL80211_AUTHTYPE_SHARED_KEY:
3386829c878SJohannes Berg 			wdev->conn->params.auth_type =
3396829c878SJohannes Berg 				NL80211_AUTHTYPE_NETWORK_EAP;
3406829c878SJohannes Berg 			break;
3416829c878SJohannes Berg 		default:
3426829c878SJohannes Berg 			/* huh? */
3436829c878SJohannes Berg 			wdev->conn->params.auth_type =
3446829c878SJohannes Berg 				NL80211_AUTHTYPE_OPEN_SYSTEM;
3456829c878SJohannes Berg 			break;
3466829c878SJohannes Berg 		}
3476829c878SJohannes Berg 		wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
3486829c878SJohannes Berg 		schedule_work(&rdev->conn_work);
34919957bb3SJohannes Berg 	} else if (status_code != WLAN_STATUS_SUCCESS) {
350ceca7b71SJohannes Berg 		__cfg80211_connect_result(wdev->netdev, mgmt->bssid,
351ceca7b71SJohannes Berg 					  NULL, 0, NULL, 0,
352df7fc0f9SJohannes Berg 					  status_code, false, NULL);
353ceca7b71SJohannes Berg 	} else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
3546829c878SJohannes Berg 		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
3556829c878SJohannes Berg 		schedule_work(&rdev->conn_work);
3566829c878SJohannes Berg 	}
3576829c878SJohannes Berg }
358b23aa676SSamuel Ortiz 
359ceca7b71SJohannes Berg bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
360f401a6f7SJohannes Berg {
361f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
362f401a6f7SJohannes Berg 
363ceca7b71SJohannes Berg 	if (!wdev->conn)
364f401a6f7SJohannes Berg 		return false;
365f401a6f7SJohannes Berg 
366ceca7b71SJohannes Berg 	if (status == WLAN_STATUS_SUCCESS) {
367ceca7b71SJohannes Berg 		wdev->conn->state = CFG80211_CONN_CONNECTED;
368f401a6f7SJohannes Berg 		return false;
369ceca7b71SJohannes Berg 	}
370f401a6f7SJohannes Berg 
371ceca7b71SJohannes Berg 	if (wdev->conn->prev_bssid_valid) {
372f401a6f7SJohannes Berg 		/*
373f401a6f7SJohannes Berg 		 * Some stupid APs don't accept reassoc, so we
374ceca7b71SJohannes Berg 		 * need to fall back to trying regular assoc;
375ceca7b71SJohannes Berg 		 * return true so no event is sent to userspace.
376f401a6f7SJohannes Berg 		 */
377f401a6f7SJohannes Berg 		wdev->conn->prev_bssid_valid = false;
378f401a6f7SJohannes Berg 		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
379f401a6f7SJohannes Berg 		schedule_work(&rdev->conn_work);
380f401a6f7SJohannes Berg 		return true;
381f401a6f7SJohannes Berg 	}
382f401a6f7SJohannes Berg 
383923a0e7dSJohannes Berg 	wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
384ceca7b71SJohannes Berg 	schedule_work(&rdev->conn_work);
385ceca7b71SJohannes Berg 	return false;
386ceca7b71SJohannes Berg }
3877d930bc3SJohannes Berg 
388ceca7b71SJohannes Berg void cfg80211_sme_deauth(struct wireless_dev *wdev)
389ceca7b71SJohannes Berg {
390ceca7b71SJohannes Berg 	cfg80211_sme_free(wdev);
391ceca7b71SJohannes Berg }
392ceca7b71SJohannes Berg 
393ceca7b71SJohannes Berg void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
394ceca7b71SJohannes Berg {
395f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
396923a0e7dSJohannes Berg 
397923a0e7dSJohannes Berg 	if (!wdev->conn)
398923a0e7dSJohannes Berg 		return;
399923a0e7dSJohannes Berg 
400923a0e7dSJohannes Berg 	wdev->conn->state = CFG80211_CONN_AUTH_FAILED;
401923a0e7dSJohannes Berg 	schedule_work(&rdev->conn_work);
402ceca7b71SJohannes Berg }
403ceca7b71SJohannes Berg 
404ceca7b71SJohannes Berg void cfg80211_sme_disassoc(struct wireless_dev *wdev)
405ceca7b71SJohannes Berg {
406f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
407ceca7b71SJohannes Berg 
408ceca7b71SJohannes Berg 	if (!wdev->conn)
409ceca7b71SJohannes Berg 		return;
410ceca7b71SJohannes Berg 
411ceca7b71SJohannes Berg 	wdev->conn->state = CFG80211_CONN_DEAUTH;
4127d930bc3SJohannes Berg 	schedule_work(&rdev->conn_work);
4137d930bc3SJohannes Berg }
4147d930bc3SJohannes Berg 
415ceca7b71SJohannes Berg void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
416ceca7b71SJohannes Berg {
417f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
418923a0e7dSJohannes Berg 
419923a0e7dSJohannes Berg 	if (!wdev->conn)
420923a0e7dSJohannes Berg 		return;
421923a0e7dSJohannes Berg 
422923a0e7dSJohannes Berg 	wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
423923a0e7dSJohannes Berg 	schedule_work(&rdev->conn_work);
424ceca7b71SJohannes Berg }
425ceca7b71SJohannes Berg 
42646b9d180SJohannes Berg static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
42746b9d180SJohannes Berg 				     const u8 *ies, size_t ies_len,
42846b9d180SJohannes Berg 				     const u8 **out_ies, size_t *out_ies_len)
42946b9d180SJohannes Berg {
43046b9d180SJohannes Berg 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
43146b9d180SJohannes Berg 	u8 *buf;
43246b9d180SJohannes Berg 	size_t offs;
43346b9d180SJohannes Berg 
43446b9d180SJohannes Berg 	if (!rdev->wiphy.extended_capabilities_len ||
43546b9d180SJohannes Berg 	    (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
43646b9d180SJohannes Berg 		*out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
43746b9d180SJohannes Berg 		if (!*out_ies)
43846b9d180SJohannes Berg 			return -ENOMEM;
43946b9d180SJohannes Berg 		*out_ies_len = ies_len;
44046b9d180SJohannes Berg 		return 0;
44146b9d180SJohannes Berg 	}
44246b9d180SJohannes Berg 
44346b9d180SJohannes Berg 	buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
44446b9d180SJohannes Berg 		      GFP_KERNEL);
44546b9d180SJohannes Berg 	if (!buf)
44646b9d180SJohannes Berg 		return -ENOMEM;
44746b9d180SJohannes Berg 
44846b9d180SJohannes Berg 	if (ies_len) {
44946b9d180SJohannes Berg 		static const u8 before_extcapa[] = {
45046b9d180SJohannes Berg 			/* not listing IEs expected to be created by driver */
45146b9d180SJohannes Berg 			WLAN_EID_RSN,
45246b9d180SJohannes Berg 			WLAN_EID_QOS_CAPA,
45346b9d180SJohannes Berg 			WLAN_EID_RRM_ENABLED_CAPABILITIES,
45446b9d180SJohannes Berg 			WLAN_EID_MOBILITY_DOMAIN,
45546b9d180SJohannes Berg 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
45646b9d180SJohannes Berg 			WLAN_EID_BSS_COEX_2040,
45746b9d180SJohannes Berg 		};
45846b9d180SJohannes Berg 
45946b9d180SJohannes Berg 		offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
46046b9d180SJohannes Berg 					  ARRAY_SIZE(before_extcapa), 0);
46146b9d180SJohannes Berg 		memcpy(buf, ies, offs);
46246b9d180SJohannes Berg 		/* leave a whole for extended capabilities IE */
46346b9d180SJohannes Berg 		memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
46446b9d180SJohannes Berg 		       ies + offs, ies_len - offs);
46546b9d180SJohannes Berg 	} else {
46646b9d180SJohannes Berg 		offs = 0;
46746b9d180SJohannes Berg 	}
46846b9d180SJohannes Berg 
46946b9d180SJohannes Berg 	/* place extended capabilities IE (with only driver capabilities) */
47046b9d180SJohannes Berg 	buf[offs] = WLAN_EID_EXT_CAPABILITY;
47146b9d180SJohannes Berg 	buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
47246b9d180SJohannes Berg 	memcpy(buf + offs + 2,
47346b9d180SJohannes Berg 	       rdev->wiphy.extended_capabilities,
47446b9d180SJohannes Berg 	       rdev->wiphy.extended_capabilities_len);
47546b9d180SJohannes Berg 
47646b9d180SJohannes Berg 	*out_ies = buf;
47746b9d180SJohannes Berg 	*out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
47846b9d180SJohannes Berg 
47946b9d180SJohannes Berg 	return 0;
48046b9d180SJohannes Berg }
48146b9d180SJohannes Berg 
482ceca7b71SJohannes Berg static int cfg80211_sme_connect(struct wireless_dev *wdev,
483ceca7b71SJohannes Berg 				struct cfg80211_connect_params *connect,
484ceca7b71SJohannes Berg 				const u8 *prev_bssid)
485ceca7b71SJohannes Berg {
486f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
487ceca7b71SJohannes Berg 	struct cfg80211_bss *bss;
488ceca7b71SJohannes Berg 	int err;
489ceca7b71SJohannes Berg 
490ceca7b71SJohannes Berg 	if (!rdev->ops->auth || !rdev->ops->assoc)
491ceca7b71SJohannes Berg 		return -EOPNOTSUPP;
492ceca7b71SJohannes Berg 
493ceca7b71SJohannes Berg 	if (wdev->current_bss)
494ceca7b71SJohannes Berg 		return -EALREADY;
495ceca7b71SJohannes Berg 
496ceca7b71SJohannes Berg 	if (WARN_ON(wdev->conn))
497ceca7b71SJohannes Berg 		return -EINPROGRESS;
498ceca7b71SJohannes Berg 
499ceca7b71SJohannes Berg 	wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
500ceca7b71SJohannes Berg 	if (!wdev->conn)
501ceca7b71SJohannes Berg 		return -ENOMEM;
502ceca7b71SJohannes Berg 
503ceca7b71SJohannes Berg 	/*
504ceca7b71SJohannes Berg 	 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
505ceca7b71SJohannes Berg 	 */
506ceca7b71SJohannes Berg 	memcpy(&wdev->conn->params, connect, sizeof(*connect));
507ceca7b71SJohannes Berg 	if (connect->bssid) {
508ceca7b71SJohannes Berg 		wdev->conn->params.bssid = wdev->conn->bssid;
509ceca7b71SJohannes Berg 		memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
510ceca7b71SJohannes Berg 	}
511ceca7b71SJohannes Berg 
51246b9d180SJohannes Berg 	if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
51346b9d180SJohannes Berg 				      &wdev->conn->ie,
51446b9d180SJohannes Berg 				      &wdev->conn->params.ie_len)) {
515ceca7b71SJohannes Berg 		kfree(wdev->conn);
516ceca7b71SJohannes Berg 		wdev->conn = NULL;
517ceca7b71SJohannes Berg 		return -ENOMEM;
518ceca7b71SJohannes Berg 	}
51946b9d180SJohannes Berg 	wdev->conn->params.ie = wdev->conn->ie;
520ceca7b71SJohannes Berg 
521ceca7b71SJohannes Berg 	if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
522ceca7b71SJohannes Berg 		wdev->conn->auto_auth = true;
523ceca7b71SJohannes Berg 		/* start with open system ... should mostly work */
524ceca7b71SJohannes Berg 		wdev->conn->params.auth_type =
525ceca7b71SJohannes Berg 			NL80211_AUTHTYPE_OPEN_SYSTEM;
526ceca7b71SJohannes Berg 	} else {
527ceca7b71SJohannes Berg 		wdev->conn->auto_auth = false;
528ceca7b71SJohannes Berg 	}
529ceca7b71SJohannes Berg 
530ceca7b71SJohannes Berg 	wdev->conn->params.ssid = wdev->ssid;
531babd3a27SZhao, Gang 	wdev->conn->params.ssid_len = wdev->ssid_len;
532ceca7b71SJohannes Berg 
533ceca7b71SJohannes Berg 	/* see if we have the bss already */
534ceca7b71SJohannes Berg 	bss = cfg80211_get_conn_bss(wdev);
535ceca7b71SJohannes Berg 
536ceca7b71SJohannes Berg 	if (prev_bssid) {
537ceca7b71SJohannes Berg 		memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
538ceca7b71SJohannes Berg 		wdev->conn->prev_bssid_valid = true;
539ceca7b71SJohannes Berg 	}
540ceca7b71SJohannes Berg 
541ceca7b71SJohannes Berg 	/* we're good if we have a matching bss struct */
542ceca7b71SJohannes Berg 	if (bss) {
543ceca7b71SJohannes Berg 		err = cfg80211_conn_do_work(wdev);
544ceca7b71SJohannes Berg 		cfg80211_put_bss(wdev->wiphy, bss);
545ceca7b71SJohannes Berg 	} else {
546ceca7b71SJohannes Berg 		/* otherwise we'll need to scan for the AP first */
547ceca7b71SJohannes Berg 		err = cfg80211_conn_scan(wdev);
548ceca7b71SJohannes Berg 
549ceca7b71SJohannes Berg 		/*
550ceca7b71SJohannes Berg 		 * If we can't scan right now, then we need to scan again
551ceca7b71SJohannes Berg 		 * after the current scan finished, since the parameters
552ceca7b71SJohannes Berg 		 * changed (unless we find a good AP anyway).
553ceca7b71SJohannes Berg 		 */
554ceca7b71SJohannes Berg 		if (err == -EBUSY) {
555ceca7b71SJohannes Berg 			err = 0;
556ceca7b71SJohannes Berg 			wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
557ceca7b71SJohannes Berg 		}
558ceca7b71SJohannes Berg 	}
559ceca7b71SJohannes Berg 
560ceca7b71SJohannes Berg 	if (err)
561ceca7b71SJohannes Berg 		cfg80211_sme_free(wdev);
562ceca7b71SJohannes Berg 
563ceca7b71SJohannes Berg 	return err;
564ceca7b71SJohannes Berg }
565ceca7b71SJohannes Berg 
566ceca7b71SJohannes Berg static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
567ceca7b71SJohannes Berg {
568f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
569ceca7b71SJohannes Berg 	int err;
570ceca7b71SJohannes Berg 
571ceca7b71SJohannes Berg 	if (!wdev->conn)
572ceca7b71SJohannes Berg 		return 0;
573ceca7b71SJohannes Berg 
574ceca7b71SJohannes Berg 	if (!rdev->ops->deauth)
575ceca7b71SJohannes Berg 		return -EOPNOTSUPP;
576ceca7b71SJohannes Berg 
577ceca7b71SJohannes Berg 	if (wdev->conn->state == CFG80211_CONN_SCANNING ||
578ceca7b71SJohannes Berg 	    wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
579ceca7b71SJohannes Berg 		err = 0;
580ceca7b71SJohannes Berg 		goto out;
581ceca7b71SJohannes Berg 	}
582ceca7b71SJohannes Berg 
583ceca7b71SJohannes Berg 	/* wdev->conn->params.bssid must be set if > SCANNING */
584ceca7b71SJohannes Berg 	err = cfg80211_mlme_deauth(rdev, wdev->netdev,
585ceca7b71SJohannes Berg 				   wdev->conn->params.bssid,
586ceca7b71SJohannes Berg 				   NULL, 0, reason, false);
587ceca7b71SJohannes Berg  out:
588ceca7b71SJohannes Berg 	cfg80211_sme_free(wdev);
589ceca7b71SJohannes Berg 	return err;
590ceca7b71SJohannes Berg }
591ceca7b71SJohannes Berg 
592ceca7b71SJohannes Berg /*
593ceca7b71SJohannes Berg  * code shared for in-device and software SME
594ceca7b71SJohannes Berg  */
595ceca7b71SJohannes Berg 
596ceca7b71SJohannes Berg static bool cfg80211_is_all_idle(void)
597ceca7b71SJohannes Berg {
598ceca7b71SJohannes Berg 	struct cfg80211_registered_device *rdev;
599ceca7b71SJohannes Berg 	struct wireless_dev *wdev;
600ceca7b71SJohannes Berg 	bool is_all_idle = true;
601ceca7b71SJohannes Berg 
602ceca7b71SJohannes Berg 	/*
603ceca7b71SJohannes Berg 	 * All devices must be idle as otherwise if you are actively
604ceca7b71SJohannes Berg 	 * scanning some new beacon hints could be learned and would
605ceca7b71SJohannes Berg 	 * count as new regulatory hints.
606ceca7b71SJohannes Berg 	 */
607ceca7b71SJohannes Berg 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
608ceca7b71SJohannes Berg 		list_for_each_entry(wdev, &rdev->wdev_list, list) {
609ceca7b71SJohannes Berg 			wdev_lock(wdev);
610ceca7b71SJohannes Berg 			if (wdev->conn || wdev->current_bss)
611ceca7b71SJohannes Berg 				is_all_idle = false;
612ceca7b71SJohannes Berg 			wdev_unlock(wdev);
613ceca7b71SJohannes Berg 		}
614ceca7b71SJohannes Berg 	}
615ceca7b71SJohannes Berg 
616ceca7b71SJohannes Berg 	return is_all_idle;
617ceca7b71SJohannes Berg }
618ceca7b71SJohannes Berg 
619ceca7b71SJohannes Berg static void disconnect_work(struct work_struct *work)
620ceca7b71SJohannes Berg {
621ceca7b71SJohannes Berg 	rtnl_lock();
622ceca7b71SJohannes Berg 	if (cfg80211_is_all_idle())
623ceca7b71SJohannes Berg 		regulatory_hint_disconnect();
624ceca7b71SJohannes Berg 	rtnl_unlock();
625ceca7b71SJohannes Berg }
626ceca7b71SJohannes Berg 
627ceca7b71SJohannes Berg static DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
628ceca7b71SJohannes Berg 
629ceca7b71SJohannes Berg 
630ceca7b71SJohannes Berg /*
631ceca7b71SJohannes Berg  * API calls for drivers implementing connect/disconnect and
632ceca7b71SJohannes Berg  * SME event handling
633ceca7b71SJohannes Berg  */
634ceca7b71SJohannes Berg 
6356f390908SBen Greear /* This method must consume bss one way or another */
636667503ddSJohannes Berg void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
637b23aa676SSamuel Ortiz 			       const u8 *req_ie, size_t req_ie_len,
638b23aa676SSamuel Ortiz 			       const u8 *resp_ie, size_t resp_ie_len,
639df7fc0f9SJohannes Berg 			       u16 status, bool wextev,
640df7fc0f9SJohannes Berg 			       struct cfg80211_bss *bss)
641b23aa676SSamuel Ortiz {
642b23aa676SSamuel Ortiz 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6439caf0364SJohannes Berg 	const u8 *country_ie;
6443d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
645b23aa676SSamuel Ortiz 	union iwreq_data wrqu;
646b23aa676SSamuel Ortiz #endif
647b23aa676SSamuel Ortiz 
648667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
649667503ddSJohannes Berg 
650074ac8dfSJohannes Berg 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
6516f390908SBen Greear 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
6526f390908SBen Greear 		cfg80211_put_bss(wdev->wiphy, bss);
653b23aa676SSamuel Ortiz 		return;
6546f390908SBen Greear 	}
655b23aa676SSamuel Ortiz 
656f26cbf40SZhao, Gang 	nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev,
657e45cd82aSJohannes Berg 				    bssid, req_ie, req_ie_len,
658e45cd82aSJohannes Berg 				    resp_ie, resp_ie_len,
659667503ddSJohannes Berg 				    status, GFP_KERNEL);
660e45cd82aSJohannes Berg 
6613d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
662e45cd82aSJohannes Berg 	if (wextev) {
663e45cd82aSJohannes Berg 		if (req_ie && status == WLAN_STATUS_SUCCESS) {
664e45cd82aSJohannes Berg 			memset(&wrqu, 0, sizeof(wrqu));
665e45cd82aSJohannes Berg 			wrqu.data.length = req_ie_len;
6663409ff77SZhu Yi 			wireless_send_event(dev, IWEVASSOCREQIE, &wrqu, req_ie);
667e45cd82aSJohannes Berg 		}
668e45cd82aSJohannes Berg 
669e45cd82aSJohannes Berg 		if (resp_ie && status == WLAN_STATUS_SUCCESS) {
670e45cd82aSJohannes Berg 			memset(&wrqu, 0, sizeof(wrqu));
671e45cd82aSJohannes Berg 			wrqu.data.length = resp_ie_len;
672e45cd82aSJohannes Berg 			wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu, resp_ie);
673e45cd82aSJohannes Berg 		}
674e45cd82aSJohannes Berg 
675e45cd82aSJohannes Berg 		memset(&wrqu, 0, sizeof(wrqu));
676e45cd82aSJohannes Berg 		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
677f401a6f7SJohannes Berg 		if (bssid && status == WLAN_STATUS_SUCCESS) {
678e45cd82aSJohannes Berg 			memcpy(wrqu.ap_addr.sa_data, bssid, ETH_ALEN);
679f401a6f7SJohannes Berg 			memcpy(wdev->wext.prev_bssid, bssid, ETH_ALEN);
680f401a6f7SJohannes Berg 			wdev->wext.prev_bssid_valid = true;
681f401a6f7SJohannes Berg 		}
682e45cd82aSJohannes Berg 		wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
683e45cd82aSJohannes Berg 	}
684e45cd82aSJohannes Berg #endif
685e45cd82aSJohannes Berg 
6864c4d684aSUjjal Roy 	if (!bss && (status == WLAN_STATUS_SUCCESS)) {
687f26cbf40SZhao, Gang 		WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
6884c4d684aSUjjal Roy 		bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
6894c4d684aSUjjal Roy 				       wdev->ssid, wdev->ssid_len,
6906eb18137SDedy Lansky 				       IEEE80211_BSS_TYPE_ESS,
6916eb18137SDedy Lansky 				       IEEE80211_PRIVACY_ANY);
6924c4d684aSUjjal Roy 		if (bss)
6934c4d684aSUjjal Roy 			cfg80211_hold_bss(bss_from_pub(bss));
6944c4d684aSUjjal Roy 	}
6954c4d684aSUjjal Roy 
696df7fc0f9SJohannes Berg 	if (wdev->current_bss) {
697df7fc0f9SJohannes Berg 		cfg80211_unhold_bss(wdev->current_bss);
6985b112d3dSJohannes Berg 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
699df7fc0f9SJohannes Berg 		wdev->current_bss = NULL;
700df7fc0f9SJohannes Berg 	}
701df7fc0f9SJohannes Berg 
702fffd0934SJohannes Berg 	if (status != WLAN_STATUS_SUCCESS) {
703b47f610bSJohannes Berg 		kzfree(wdev->connect_keys);
704fffd0934SJohannes Berg 		wdev->connect_keys = NULL;
7058dadadb7SJohannes Berg 		wdev->ssid_len = 0;
706f1940c57SJohannes Berg 		if (bss) {
707f1940c57SJohannes Berg 			cfg80211_unhold_bss(bss_from_pub(bss));
7085b112d3dSJohannes Berg 			cfg80211_put_bss(wdev->wiphy, bss);
709f1940c57SJohannes Berg 		}
710c1fbb258SEliad Peller 		cfg80211_sme_free(wdev);
711fffd0934SJohannes Berg 		return;
712fffd0934SJohannes Berg 	}
713fffd0934SJohannes Berg 
714b23aa676SSamuel Ortiz 	if (WARN_ON(!bss))
715b23aa676SSamuel Ortiz 		return;
716f1940c57SJohannes Berg 
71719957bb3SJohannes Berg 	wdev->current_bss = bss_from_pub(bss);
718b23aa676SSamuel Ortiz 
719fffd0934SJohannes Berg 	cfg80211_upload_connect_keys(wdev);
7208b19e6caSLuis R. Rodriguez 
7219caf0364SJohannes Berg 	rcu_read_lock();
7229caf0364SJohannes Berg 	country_ie = ieee80211_bss_get_ie(bss, WLAN_EID_COUNTRY);
7239caf0364SJohannes Berg 	if (!country_ie) {
7249caf0364SJohannes Berg 		rcu_read_unlock();
7259caf0364SJohannes Berg 		return;
7269caf0364SJohannes Berg 	}
7279caf0364SJohannes Berg 
7289caf0364SJohannes Berg 	country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
7299caf0364SJohannes Berg 	rcu_read_unlock();
7308b19e6caSLuis R. Rodriguez 
7318b19e6caSLuis R. Rodriguez 	if (!country_ie)
7328b19e6caSLuis R. Rodriguez 		return;
7338b19e6caSLuis R. Rodriguez 
7348b19e6caSLuis R. Rodriguez 	/*
7358b19e6caSLuis R. Rodriguez 	 * ieee80211_bss_get_ie() ensures we can access:
7368b19e6caSLuis R. Rodriguez 	 * - country_ie + 2, the start of the country ie data, and
7378b19e6caSLuis R. Rodriguez 	 * - and country_ie[1] which is the IE length
7388b19e6caSLuis R. Rodriguez 	 */
739789fd033SLuis R. Rodriguez 	regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
7401a919318SJohannes Berg 				   country_ie + 2, country_ie[1]);
7419caf0364SJohannes Berg 	kfree(country_ie);
742b23aa676SSamuel Ortiz }
743f2129354SJohannes Berg 
744f2129354SJohannes Berg void cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
745f2129354SJohannes Berg 			     const u8 *req_ie, size_t req_ie_len,
746f2129354SJohannes Berg 			     const u8 *resp_ie, size_t resp_ie_len,
747f2129354SJohannes Berg 			     u16 status, gfp_t gfp)
748f2129354SJohannes Berg {
749667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
750f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
751667503ddSJohannes Berg 	struct cfg80211_event *ev;
752667503ddSJohannes Berg 	unsigned long flags;
753667503ddSJohannes Berg 
754667503ddSJohannes Berg 	ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
755667503ddSJohannes Berg 	if (!ev)
756667503ddSJohannes Berg 		return;
757667503ddSJohannes Berg 
758667503ddSJohannes Berg 	ev->type = EVENT_CONNECT_RESULT;
75916a832e7SZhu Yi 	if (bssid)
760667503ddSJohannes Berg 		memcpy(ev->cr.bssid, bssid, ETH_ALEN);
7617834704bSNishant Sarmukadam 	if (req_ie_len) {
762667503ddSJohannes Berg 		ev->cr.req_ie = ((u8 *)ev) + sizeof(*ev);
763667503ddSJohannes Berg 		ev->cr.req_ie_len = req_ie_len;
764667503ddSJohannes Berg 		memcpy((void *)ev->cr.req_ie, req_ie, req_ie_len);
7657834704bSNishant Sarmukadam 	}
7667834704bSNishant Sarmukadam 	if (resp_ie_len) {
767667503ddSJohannes Berg 		ev->cr.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
768667503ddSJohannes Berg 		ev->cr.resp_ie_len = resp_ie_len;
769667503ddSJohannes Berg 		memcpy((void *)ev->cr.resp_ie, resp_ie, resp_ie_len);
7707834704bSNishant Sarmukadam 	}
771667503ddSJohannes Berg 	ev->cr.status = status;
772667503ddSJohannes Berg 
773667503ddSJohannes Berg 	spin_lock_irqsave(&wdev->event_lock, flags);
774667503ddSJohannes Berg 	list_add_tail(&ev->list, &wdev->event_list);
775667503ddSJohannes Berg 	spin_unlock_irqrestore(&wdev->event_lock, flags);
776e60d7443SAlban Browaeys 	queue_work(cfg80211_wq, &rdev->event_work);
777f2129354SJohannes Berg }
778b23aa676SSamuel Ortiz EXPORT_SYMBOL(cfg80211_connect_result);
779b23aa676SSamuel Ortiz 
7800e3a39b5SBen Greear /* Consumes bss object one way or another */
781ed9d0102SJouni Malinen void __cfg80211_roamed(struct wireless_dev *wdev,
782adbde344SVasanthakumar Thiagarajan 		       struct cfg80211_bss *bss,
783b23aa676SSamuel Ortiz 		       const u8 *req_ie, size_t req_ie_len,
784667503ddSJohannes Berg 		       const u8 *resp_ie, size_t resp_ie_len)
785b23aa676SSamuel Ortiz {
7863d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
787b23aa676SSamuel Ortiz 	union iwreq_data wrqu;
788b23aa676SSamuel Ortiz #endif
789667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
790667503ddSJohannes Berg 
791074ac8dfSJohannes Berg 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
792074ac8dfSJohannes Berg 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
793adbde344SVasanthakumar Thiagarajan 		goto out;
794b23aa676SSamuel Ortiz 
795ceca7b71SJohannes Berg 	if (WARN_ON(!wdev->current_bss))
796adbde344SVasanthakumar Thiagarajan 		goto out;
797b23aa676SSamuel Ortiz 
798b23aa676SSamuel Ortiz 	cfg80211_unhold_bss(wdev->current_bss);
7995b112d3dSJohannes Berg 	cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
800b23aa676SSamuel Ortiz 	wdev->current_bss = NULL;
801b23aa676SSamuel Ortiz 
80219957bb3SJohannes Berg 	cfg80211_hold_bss(bss_from_pub(bss));
80319957bb3SJohannes Berg 	wdev->current_bss = bss_from_pub(bss);
804b23aa676SSamuel Ortiz 
805f26cbf40SZhao, Gang 	nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
806f26cbf40SZhao, Gang 			    wdev->netdev, bss->bssid,
807667503ddSJohannes Berg 			    req_ie, req_ie_len, resp_ie, resp_ie_len,
808667503ddSJohannes Berg 			    GFP_KERNEL);
809b23aa676SSamuel Ortiz 
8103d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
811b23aa676SSamuel Ortiz 	if (req_ie) {
812b23aa676SSamuel Ortiz 		memset(&wrqu, 0, sizeof(wrqu));
813b23aa676SSamuel Ortiz 		wrqu.data.length = req_ie_len;
8143409ff77SZhu Yi 		wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
815667503ddSJohannes Berg 				    &wrqu, req_ie);
816b23aa676SSamuel Ortiz 	}
817b23aa676SSamuel Ortiz 
818b23aa676SSamuel Ortiz 	if (resp_ie) {
819b23aa676SSamuel Ortiz 		memset(&wrqu, 0, sizeof(wrqu));
820b23aa676SSamuel Ortiz 		wrqu.data.length = resp_ie_len;
821667503ddSJohannes Berg 		wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
822667503ddSJohannes Berg 				    &wrqu, resp_ie);
823b23aa676SSamuel Ortiz 	}
824b23aa676SSamuel Ortiz 
825b23aa676SSamuel Ortiz 	memset(&wrqu, 0, sizeof(wrqu));
826b23aa676SSamuel Ortiz 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
827adbde344SVasanthakumar Thiagarajan 	memcpy(wrqu.ap_addr.sa_data, bss->bssid, ETH_ALEN);
828adbde344SVasanthakumar Thiagarajan 	memcpy(wdev->wext.prev_bssid, bss->bssid, ETH_ALEN);
829f401a6f7SJohannes Berg 	wdev->wext.prev_bssid_valid = true;
830667503ddSJohannes Berg 	wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
831b23aa676SSamuel Ortiz #endif
832adbde344SVasanthakumar Thiagarajan 
833adbde344SVasanthakumar Thiagarajan 	return;
834adbde344SVasanthakumar Thiagarajan out:
8355b112d3dSJohannes Berg 	cfg80211_put_bss(wdev->wiphy, bss);
836b23aa676SSamuel Ortiz }
837667503ddSJohannes Berg 
838ed9d0102SJouni Malinen void cfg80211_roamed(struct net_device *dev,
839ed9d0102SJouni Malinen 		     struct ieee80211_channel *channel,
840ed9d0102SJouni Malinen 		     const u8 *bssid,
841667503ddSJohannes Berg 		     const u8 *req_ie, size_t req_ie_len,
842667503ddSJohannes Berg 		     const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
843667503ddSJohannes Berg {
844667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
845adbde344SVasanthakumar Thiagarajan 	struct cfg80211_bss *bss;
846adbde344SVasanthakumar Thiagarajan 
847adbde344SVasanthakumar Thiagarajan 	bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
8486eb18137SDedy Lansky 			       wdev->ssid_len,
8496eb18137SDedy Lansky 			       IEEE80211_BSS_TYPE_ESS, IEEE80211_PRIVACY_ANY);
850adbde344SVasanthakumar Thiagarajan 	if (WARN_ON(!bss))
851adbde344SVasanthakumar Thiagarajan 		return;
852adbde344SVasanthakumar Thiagarajan 
853adbde344SVasanthakumar Thiagarajan 	cfg80211_roamed_bss(dev, bss, req_ie, req_ie_len, resp_ie,
854adbde344SVasanthakumar Thiagarajan 			    resp_ie_len, gfp);
855adbde344SVasanthakumar Thiagarajan }
856adbde344SVasanthakumar Thiagarajan EXPORT_SYMBOL(cfg80211_roamed);
857adbde344SVasanthakumar Thiagarajan 
8580e3a39b5SBen Greear /* Consumes bss object one way or another */
859adbde344SVasanthakumar Thiagarajan void cfg80211_roamed_bss(struct net_device *dev,
860adbde344SVasanthakumar Thiagarajan 			 struct cfg80211_bss *bss, const u8 *req_ie,
861adbde344SVasanthakumar Thiagarajan 			 size_t req_ie_len, const u8 *resp_ie,
862adbde344SVasanthakumar Thiagarajan 			 size_t resp_ie_len, gfp_t gfp)
863adbde344SVasanthakumar Thiagarajan {
864adbde344SVasanthakumar Thiagarajan 	struct wireless_dev *wdev = dev->ieee80211_ptr;
865f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
866667503ddSJohannes Berg 	struct cfg80211_event *ev;
867667503ddSJohannes Berg 	unsigned long flags;
868667503ddSJohannes Berg 
869adbde344SVasanthakumar Thiagarajan 	if (WARN_ON(!bss))
870667503ddSJohannes Berg 		return;
871667503ddSJohannes Berg 
872adbde344SVasanthakumar Thiagarajan 	ev = kzalloc(sizeof(*ev) + req_ie_len + resp_ie_len, gfp);
873adbde344SVasanthakumar Thiagarajan 	if (!ev) {
8745b112d3dSJohannes Berg 		cfg80211_put_bss(wdev->wiphy, bss);
875adbde344SVasanthakumar Thiagarajan 		return;
876adbde344SVasanthakumar Thiagarajan 	}
877adbde344SVasanthakumar Thiagarajan 
878667503ddSJohannes Berg 	ev->type = EVENT_ROAMED;
879667503ddSJohannes Berg 	ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
880667503ddSJohannes Berg 	ev->rm.req_ie_len = req_ie_len;
881667503ddSJohannes Berg 	memcpy((void *)ev->rm.req_ie, req_ie, req_ie_len);
882667503ddSJohannes Berg 	ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + req_ie_len;
883667503ddSJohannes Berg 	ev->rm.resp_ie_len = resp_ie_len;
884667503ddSJohannes Berg 	memcpy((void *)ev->rm.resp_ie, resp_ie, resp_ie_len);
885adbde344SVasanthakumar Thiagarajan 	ev->rm.bss = bss;
886667503ddSJohannes Berg 
887667503ddSJohannes Berg 	spin_lock_irqsave(&wdev->event_lock, flags);
888667503ddSJohannes Berg 	list_add_tail(&ev->list, &wdev->event_list);
889667503ddSJohannes Berg 	spin_unlock_irqrestore(&wdev->event_lock, flags);
890e60d7443SAlban Browaeys 	queue_work(cfg80211_wq, &rdev->event_work);
891667503ddSJohannes Berg }
892adbde344SVasanthakumar Thiagarajan EXPORT_SYMBOL(cfg80211_roamed_bss);
893b23aa676SSamuel Ortiz 
894667503ddSJohannes Berg void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
8956829c878SJohannes Berg 			     size_t ie_len, u16 reason, bool from_ap)
896b23aa676SSamuel Ortiz {
897b23aa676SSamuel Ortiz 	struct wireless_dev *wdev = dev->ieee80211_ptr;
898f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
899fffd0934SJohannes Berg 	int i;
9003d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
901b23aa676SSamuel Ortiz 	union iwreq_data wrqu;
902b23aa676SSamuel Ortiz #endif
903b23aa676SSamuel Ortiz 
904667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
905667503ddSJohannes Berg 
906074ac8dfSJohannes Berg 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
907074ac8dfSJohannes Berg 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
908b23aa676SSamuel Ortiz 		return;
909b23aa676SSamuel Ortiz 
910b23aa676SSamuel Ortiz 	if (wdev->current_bss) {
911b23aa676SSamuel Ortiz 		cfg80211_unhold_bss(wdev->current_bss);
9125b112d3dSJohannes Berg 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
913b23aa676SSamuel Ortiz 	}
914b23aa676SSamuel Ortiz 
915b23aa676SSamuel Ortiz 	wdev->current_bss = NULL;
9168dadadb7SJohannes Berg 	wdev->ssid_len = 0;
917b23aa676SSamuel Ortiz 
918fffd0934SJohannes Berg 	nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
919fffd0934SJohannes Berg 
920fffd0934SJohannes Berg 	/*
921fffd0934SJohannes Berg 	 * Delete all the keys ... pairwise keys can't really
922fffd0934SJohannes Berg 	 * exist any more anyway, but default keys might.
923fffd0934SJohannes Berg 	 */
924fffd0934SJohannes Berg 	if (rdev->ops->del_key)
925fffd0934SJohannes Berg 		for (i = 0; i < 6; i++)
926e35e4d28SHila Gonen 			rdev_del_key(rdev, dev, i, false, NULL);
927b23aa676SSamuel Ortiz 
928fa9ffc74SKyeyoon Park 	rdev_set_qos_map(rdev, dev, NULL);
929fa9ffc74SKyeyoon Park 
9303d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
931b23aa676SSamuel Ortiz 	memset(&wrqu, 0, sizeof(wrqu));
932b23aa676SSamuel Ortiz 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
933b23aa676SSamuel Ortiz 	wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
9345f612033SAbhijeet Kolekar 	wdev->wext.connect.ssid_len = 0;
935b23aa676SSamuel Ortiz #endif
93609d989d1SLuis R. Rodriguez 
93709d989d1SLuis R. Rodriguez 	schedule_work(&cfg80211_disconnect_work);
938b23aa676SSamuel Ortiz }
939b23aa676SSamuel Ortiz 
940b23aa676SSamuel Ortiz void cfg80211_disconnected(struct net_device *dev, u16 reason,
941*80279fb7SJohannes Berg 			   const u8 *ie, size_t ie_len,
942*80279fb7SJohannes Berg 			   bool locally_generated, gfp_t gfp)
943b23aa676SSamuel Ortiz {
944667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
945f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
946667503ddSJohannes Berg 	struct cfg80211_event *ev;
947667503ddSJohannes Berg 	unsigned long flags;
948667503ddSJohannes Berg 
949667503ddSJohannes Berg 	ev = kzalloc(sizeof(*ev) + ie_len, gfp);
950667503ddSJohannes Berg 	if (!ev)
951667503ddSJohannes Berg 		return;
952667503ddSJohannes Berg 
953667503ddSJohannes Berg 	ev->type = EVENT_DISCONNECTED;
954667503ddSJohannes Berg 	ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
955667503ddSJohannes Berg 	ev->dc.ie_len = ie_len;
956667503ddSJohannes Berg 	memcpy((void *)ev->dc.ie, ie, ie_len);
957667503ddSJohannes Berg 	ev->dc.reason = reason;
958*80279fb7SJohannes Berg 	ev->dc.locally_generated = locally_generated;
959667503ddSJohannes Berg 
960667503ddSJohannes Berg 	spin_lock_irqsave(&wdev->event_lock, flags);
961667503ddSJohannes Berg 	list_add_tail(&ev->list, &wdev->event_list);
962667503ddSJohannes Berg 	spin_unlock_irqrestore(&wdev->event_lock, flags);
963e60d7443SAlban Browaeys 	queue_work(cfg80211_wq, &rdev->event_work);
964b23aa676SSamuel Ortiz }
965b23aa676SSamuel Ortiz EXPORT_SYMBOL(cfg80211_disconnected);
966b23aa676SSamuel Ortiz 
967ceca7b71SJohannes Berg /*
968ceca7b71SJohannes Berg  * API calls for nl80211/wext compatibility code
969ceca7b71SJohannes Berg  */
97083739b03SJohannes Berg int cfg80211_connect(struct cfg80211_registered_device *rdev,
971b23aa676SSamuel Ortiz 		     struct net_device *dev,
972fffd0934SJohannes Berg 		     struct cfg80211_connect_params *connect,
973f401a6f7SJohannes Berg 		     struct cfg80211_cached_keys *connkeys,
974f401a6f7SJohannes Berg 		     const u8 *prev_bssid)
975b23aa676SSamuel Ortiz {
976b23aa676SSamuel Ortiz 	struct wireless_dev *wdev = dev->ieee80211_ptr;
977667503ddSJohannes Berg 	int err;
978667503ddSJohannes Berg 
979667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
980b23aa676SSamuel Ortiz 
981fffd0934SJohannes Berg 	if (WARN_ON(wdev->connect_keys)) {
982b47f610bSJohannes Berg 		kzfree(wdev->connect_keys);
983fffd0934SJohannes Berg 		wdev->connect_keys = NULL;
984fffd0934SJohannes Berg 	}
985fffd0934SJohannes Berg 
9867e7c8926SBen Greear 	cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
9877e7c8926SBen Greear 				  rdev->wiphy.ht_capa_mod_mask);
9887e7c8926SBen Greear 
989fffd0934SJohannes Berg 	if (connkeys && connkeys->def >= 0) {
990fffd0934SJohannes Berg 		int idx;
991bcba8eaeSSamuel Ortiz 		u32 cipher;
992fffd0934SJohannes Berg 
993fffd0934SJohannes Berg 		idx = connkeys->def;
994bcba8eaeSSamuel Ortiz 		cipher = connkeys->params[idx].cipher;
995fffd0934SJohannes Berg 		/* If given a WEP key we may need it for shared key auth */
996bcba8eaeSSamuel Ortiz 		if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
997bcba8eaeSSamuel Ortiz 		    cipher == WLAN_CIPHER_SUITE_WEP104) {
998fffd0934SJohannes Berg 			connect->key_idx = idx;
999fffd0934SJohannes Berg 			connect->key = connkeys->params[idx].key;
1000fffd0934SJohannes Berg 			connect->key_len = connkeys->params[idx].key_len;
1001bcba8eaeSSamuel Ortiz 
1002bcba8eaeSSamuel Ortiz 			/*
1003bcba8eaeSSamuel Ortiz 			 * If ciphers are not set (e.g. when going through
1004bcba8eaeSSamuel Ortiz 			 * iwconfig), we have to set them appropriately here.
1005bcba8eaeSSamuel Ortiz 			 */
1006bcba8eaeSSamuel Ortiz 			if (connect->crypto.cipher_group == 0)
1007bcba8eaeSSamuel Ortiz 				connect->crypto.cipher_group = cipher;
1008bcba8eaeSSamuel Ortiz 
1009bcba8eaeSSamuel Ortiz 			if (connect->crypto.n_ciphers_pairwise == 0) {
1010bcba8eaeSSamuel Ortiz 				connect->crypto.n_ciphers_pairwise = 1;
1011bcba8eaeSSamuel Ortiz 				connect->crypto.ciphers_pairwise[0] = cipher;
1012bcba8eaeSSamuel Ortiz 			}
1013fffd0934SJohannes Berg 		}
1014fffd0934SJohannes Berg 	}
1015fffd0934SJohannes Berg 
1016ceca7b71SJohannes Berg 	wdev->connect_keys = connkeys;
10176829c878SJohannes Berg 	memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
10186829c878SJohannes Berg 	wdev->ssid_len = connect->ssid_len;
10196829c878SJohannes Berg 
1020ceca7b71SJohannes Berg 	if (!rdev->ops->connect)
1021ceca7b71SJohannes Berg 		err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1022ceca7b71SJohannes Berg 	else
1023ceca7b71SJohannes Berg 		err = rdev_connect(rdev, dev, connect);
10246829c878SJohannes Berg 
102519957bb3SJohannes Berg 	if (err) {
1026fffd0934SJohannes Berg 		wdev->connect_keys = NULL;
10278dadadb7SJohannes Berg 		wdev->ssid_len = 0;
1028b23aa676SSamuel Ortiz 		return err;
1029b23aa676SSamuel Ortiz 	}
1030b23aa676SSamuel Ortiz 
1031b23aa676SSamuel Ortiz 	return 0;
1032b23aa676SSamuel Ortiz }
1033b23aa676SSamuel Ortiz 
103483739b03SJohannes Berg int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1035f2129354SJohannes Berg 			struct net_device *dev, u16 reason, bool wextev)
1036b23aa676SSamuel Ortiz {
10376829c878SJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1038dee8a973SJohannes Berg 	int err = 0;
1039b23aa676SSamuel Ortiz 
1040667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
1041667503ddSJohannes Berg 
1042b47f610bSJohannes Berg 	kzfree(wdev->connect_keys);
1043fffd0934SJohannes Berg 	wdev->connect_keys = NULL;
1044fffd0934SJohannes Berg 
1045dee8a973SJohannes Berg 	if (wdev->conn)
1046ceca7b71SJohannes Berg 		err = cfg80211_sme_disconnect(wdev, reason);
1047dee8a973SJohannes Berg 	else if (!rdev->ops->disconnect)
104819957bb3SJohannes Berg 		cfg80211_mlme_down(rdev, dev);
1049dee8a973SJohannes Berg 	else if (wdev->current_bss)
1050e35e4d28SHila Gonen 		err = rdev_disconnect(rdev, dev, reason);
1051ceca7b71SJohannes Berg 
1052b23aa676SSamuel Ortiz 	return err;
1053b23aa676SSamuel Ortiz }
1054