xref: /openbmc/linux/net/wireless/sme.c (revision 4d797fce)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2b23aa676SSamuel Ortiz /*
3ceca7b71SJohannes Berg  * SME code for cfg80211
4ceca7b71SJohannes Berg  * both driver SME event handling and the SME implementation
5ceca7b71SJohannes Berg  * (for nl80211's connect() and wext)
6b23aa676SSamuel Ortiz  *
7b23aa676SSamuel Ortiz  * Copyright 2009	Johannes Berg <johannes@sipsolutions.net>
8b23aa676SSamuel Ortiz  * Copyright (C) 2009   Intel Corporation. All rights reserved.
929ce6ecbSAvraham Stern  * Copyright 2017	Intel Deutschland GmbH
10b23aa676SSamuel Ortiz  */
11b23aa676SSamuel Ortiz 
12b23aa676SSamuel Ortiz #include <linux/etherdevice.h>
13b23aa676SSamuel Ortiz #include <linux/if_arp.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
15b23aa676SSamuel Ortiz #include <linux/workqueue.h>
16a9a11622SJohannes Berg #include <linux/wireless.h>
17bc3b2d7fSPaul Gortmaker #include <linux/export.h>
18a9a11622SJohannes Berg #include <net/iw_handler.h>
19b23aa676SSamuel Ortiz #include <net/cfg80211.h>
20b23aa676SSamuel Ortiz #include <net/rtnetlink.h>
21b23aa676SSamuel Ortiz #include "nl80211.h"
228b19e6caSLuis R. Rodriguez #include "reg.h"
23e35e4d28SHila Gonen #include "rdev-ops.h"
24b23aa676SSamuel Ortiz 
25ceca7b71SJohannes Berg /*
26ceca7b71SJohannes Berg  * Software SME in cfg80211, using auth/assoc/deauth calls to the
27ceca7b71SJohannes Berg  * driver. This is is for implementing nl80211's connect/disconnect
28ceca7b71SJohannes Berg  * and wireless extensions (if configured.)
29ceca7b71SJohannes Berg  */
30ceca7b71SJohannes Berg 
316829c878SJohannes Berg struct cfg80211_conn {
326829c878SJohannes Berg 	struct cfg80211_connect_params params;
336829c878SJohannes Berg 	/* these are sub-states of the _CONNECTING sme_state */
346829c878SJohannes Berg 	enum {
356829c878SJohannes Berg 		CFG80211_CONN_SCANNING,
366829c878SJohannes Berg 		CFG80211_CONN_SCAN_AGAIN,
376829c878SJohannes Berg 		CFG80211_CONN_AUTHENTICATE_NEXT,
386829c878SJohannes Berg 		CFG80211_CONN_AUTHENTICATING,
393093ebbeSPurushottam Kushwaha 		CFG80211_CONN_AUTH_FAILED_TIMEOUT,
406829c878SJohannes Berg 		CFG80211_CONN_ASSOCIATE_NEXT,
416829c878SJohannes Berg 		CFG80211_CONN_ASSOCIATING,
42923a0e7dSJohannes Berg 		CFG80211_CONN_ASSOC_FAILED,
433093ebbeSPurushottam Kushwaha 		CFG80211_CONN_ASSOC_FAILED_TIMEOUT,
44ceca7b71SJohannes Berg 		CFG80211_CONN_DEAUTH,
45e6f462dfSJohannes Berg 		CFG80211_CONN_ABANDON,
46ceca7b71SJohannes Berg 		CFG80211_CONN_CONNECTED,
476829c878SJohannes Berg 	} state;
48f401a6f7SJohannes Berg 	u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
4946b9d180SJohannes Berg 	const u8 *ie;
506829c878SJohannes Berg 	size_t ie_len;
51f401a6f7SJohannes Berg 	bool auto_auth, prev_bssid_valid;
526829c878SJohannes Berg };
536829c878SJohannes Berg 
54ceca7b71SJohannes Berg static void cfg80211_sme_free(struct wireless_dev *wdev)
5509d989d1SLuis R. Rodriguez {
56ceca7b71SJohannes Berg 	if (!wdev->conn)
57ceca7b71SJohannes Berg 		return;
5809d989d1SLuis R. Rodriguez 
59ceca7b71SJohannes Berg 	kfree(wdev->conn->ie);
60ceca7b71SJohannes Berg 	kfree(wdev->conn);
61ceca7b71SJohannes Berg 	wdev->conn = NULL;
6209d989d1SLuis R. Rodriguez }
636829c878SJohannes Berg 
646829c878SJohannes Berg static int cfg80211_conn_scan(struct wireless_dev *wdev)
656829c878SJohannes Berg {
66f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
676829c878SJohannes Berg 	struct cfg80211_scan_request *request;
686829c878SJohannes Berg 	int n_channels, err;
696829c878SJohannes Berg 
706829c878SJohannes Berg 	ASSERT_RTNL();
71667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
726829c878SJohannes Berg 
73f9d15d16SJohannes Berg 	if (rdev->scan_req || rdev->scan_msg)
746829c878SJohannes Berg 		return -EBUSY;
756829c878SJohannes Berg 
76bdfbec2dSIlan Peer 	if (wdev->conn->params.channel)
776829c878SJohannes Berg 		n_channels = 1;
78bdfbec2dSIlan Peer 	else
79bdfbec2dSIlan Peer 		n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
806829c878SJohannes Berg 
816829c878SJohannes Berg 	request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
826829c878SJohannes Berg 			  sizeof(request->channels[0]) * n_channels,
836829c878SJohannes Berg 			  GFP_KERNEL);
846829c878SJohannes Berg 	if (!request)
856829c878SJohannes Berg 		return -ENOMEM;
866829c878SJohannes Berg 
872a84ee86SKarl Beldan 	if (wdev->conn->params.channel) {
8857fbcce3SJohannes Berg 		enum nl80211_band band = wdev->conn->params.channel->band;
892a84ee86SKarl Beldan 		struct ieee80211_supported_band *sband =
902a84ee86SKarl Beldan 			wdev->wiphy->bands[band];
912a84ee86SKarl Beldan 
922a84ee86SKarl Beldan 		if (!sband) {
932a84ee86SKarl Beldan 			kfree(request);
942a84ee86SKarl Beldan 			return -EINVAL;
952a84ee86SKarl Beldan 		}
966829c878SJohannes Berg 		request->channels[0] = wdev->conn->params.channel;
972a84ee86SKarl Beldan 		request->rates[band] = (1 << sband->n_bitrates) - 1;
982a84ee86SKarl Beldan 	} else {
996829c878SJohannes Berg 		int i = 0, j;
10057fbcce3SJohannes Berg 		enum nl80211_band band;
101e3081501SRajkumar Manoharan 		struct ieee80211_supported_band *bands;
102e3081501SRajkumar Manoharan 		struct ieee80211_channel *channel;
1036829c878SJohannes Berg 
10457fbcce3SJohannes Berg 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
105e3081501SRajkumar Manoharan 			bands = wdev->wiphy->bands[band];
106e3081501SRajkumar Manoharan 			if (!bands)
1076829c878SJohannes Berg 				continue;
108e3081501SRajkumar Manoharan 			for (j = 0; j < bands->n_channels; j++) {
109e3081501SRajkumar Manoharan 				channel = &bands->channels[j];
110e3081501SRajkumar Manoharan 				if (channel->flags & IEEE80211_CHAN_DISABLED)
111e3081501SRajkumar Manoharan 					continue;
112e3081501SRajkumar Manoharan 				request->channels[i++] = channel;
1136829c878SJohannes Berg 			}
114e3081501SRajkumar Manoharan 			request->rates[band] = (1 << bands->n_bitrates) - 1;
115e3081501SRajkumar Manoharan 		}
116e3081501SRajkumar Manoharan 		n_channels = i;
1176829c878SJohannes Berg 	}
1186829c878SJohannes Berg 	request->n_channels = n_channels;
1195ba63533SJohannes Berg 	request->ssids = (void *)&request->channels[n_channels];
1206829c878SJohannes Berg 	request->n_ssids = 1;
1216829c878SJohannes Berg 
1226829c878SJohannes Berg 	memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
1236829c878SJohannes Berg 		wdev->conn->params.ssid_len);
1246829c878SJohannes Berg 	request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
1256829c878SJohannes Berg 
126818965d3SJouni Malinen 	eth_broadcast_addr(request->bssid);
127818965d3SJouni Malinen 
128fd014284SJohannes Berg 	request->wdev = wdev;
12979c97e97SJohannes Berg 	request->wiphy = &rdev->wiphy;
13015d6030bSSam Leffler 	request->scan_start = jiffies;
1316829c878SJohannes Berg 
13279c97e97SJohannes Berg 	rdev->scan_req = request;
1336829c878SJohannes Berg 
134e35e4d28SHila Gonen 	err = rdev_scan(rdev, request);
1356829c878SJohannes Berg 	if (!err) {
1366829c878SJohannes Berg 		wdev->conn->state = CFG80211_CONN_SCANNING;
137fd014284SJohannes Berg 		nl80211_send_scan_start(rdev, wdev);
138463d0183SJohannes Berg 		dev_hold(wdev->netdev);
1396829c878SJohannes Berg 	} else {
14079c97e97SJohannes Berg 		rdev->scan_req = NULL;
1416829c878SJohannes Berg 		kfree(request);
1426829c878SJohannes Berg 	}
1436829c878SJohannes Berg 	return err;
1446829c878SJohannes Berg }
1456829c878SJohannes Berg 
1463093ebbeSPurushottam Kushwaha static int cfg80211_conn_do_work(struct wireless_dev *wdev,
1473093ebbeSPurushottam Kushwaha 				 enum nl80211_timeout_reason *treason)
1486829c878SJohannes Berg {
149f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
15019957bb3SJohannes Berg 	struct cfg80211_connect_params *params;
151f62fab73SJohannes Berg 	struct cfg80211_assoc_request req = {};
15219957bb3SJohannes Berg 	int err;
1536829c878SJohannes Berg 
154667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
155667503ddSJohannes Berg 
1566829c878SJohannes Berg 	if (!wdev->conn)
1576829c878SJohannes Berg 		return 0;
1586829c878SJohannes Berg 
15919957bb3SJohannes Berg 	params = &wdev->conn->params;
16019957bb3SJohannes Berg 
1616829c878SJohannes Berg 	switch (wdev->conn->state) {
162ceca7b71SJohannes Berg 	case CFG80211_CONN_SCANNING:
163ceca7b71SJohannes Berg 		/* didn't find it during scan ... */
164ceca7b71SJohannes Berg 		return -ENOENT;
1656829c878SJohannes Berg 	case CFG80211_CONN_SCAN_AGAIN:
1666829c878SJohannes Berg 		return cfg80211_conn_scan(wdev);
1676829c878SJohannes Berg 	case CFG80211_CONN_AUTHENTICATE_NEXT:
1682fd05115SJohannes Berg 		if (WARN_ON(!rdev->ops->auth))
1692fd05115SJohannes Berg 			return -EOPNOTSUPP;
17019957bb3SJohannes Berg 		wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
17191bf9b26SJohannes Berg 		return cfg80211_mlme_auth(rdev, wdev->netdev,
17219957bb3SJohannes Berg 					  params->channel, params->auth_type,
17319957bb3SJohannes Berg 					  params->bssid,
17419957bb3SJohannes Berg 					  params->ssid, params->ssid_len,
175fffd0934SJohannes Berg 					  NULL, 0,
176fffd0934SJohannes Berg 					  params->key, params->key_len,
177e39e5b5eSJouni Malinen 					  params->key_idx, NULL, 0);
1783093ebbeSPurushottam Kushwaha 	case CFG80211_CONN_AUTH_FAILED_TIMEOUT:
1793093ebbeSPurushottam Kushwaha 		*treason = NL80211_TIMEOUT_AUTH;
180923a0e7dSJohannes Berg 		return -ENOTCONN;
1816829c878SJohannes Berg 	case CFG80211_CONN_ASSOCIATE_NEXT:
1822fd05115SJohannes Berg 		if (WARN_ON(!rdev->ops->assoc))
1832fd05115SJohannes Berg 			return -EOPNOTSUPP;
18419957bb3SJohannes Berg 		wdev->conn->state = CFG80211_CONN_ASSOCIATING;
185f401a6f7SJohannes Berg 		if (wdev->conn->prev_bssid_valid)
186f62fab73SJohannes Berg 			req.prev_bssid = wdev->conn->prev_bssid;
187f62fab73SJohannes Berg 		req.ie = params->ie;
188f62fab73SJohannes Berg 		req.ie_len = params->ie_len;
189f62fab73SJohannes Berg 		req.use_mfp = params->mfp != NL80211_MFP_NO;
190f62fab73SJohannes Berg 		req.crypto = params->crypto;
191f62fab73SJohannes Berg 		req.flags = params->flags;
192f62fab73SJohannes Berg 		req.ht_capa = params->ht_capa;
193f62fab73SJohannes Berg 		req.ht_capa_mask = params->ht_capa_mask;
194f62fab73SJohannes Berg 		req.vht_capa = params->vht_capa;
195f62fab73SJohannes Berg 		req.vht_capa_mask = params->vht_capa_mask;
196f62fab73SJohannes Berg 
19791bf9b26SJohannes Berg 		err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
198f62fab73SJohannes Berg 					  params->bssid, params->ssid,
199f62fab73SJohannes Berg 					  params->ssid_len, &req);
20019957bb3SJohannes Berg 		if (err)
20191bf9b26SJohannes Berg 			cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
202667503ddSJohannes Berg 					     NULL, 0,
203d5cdfacbSJouni Malinen 					     WLAN_REASON_DEAUTH_LEAVING,
204d5cdfacbSJouni Malinen 					     false);
20519957bb3SJohannes Berg 		return err;
2063093ebbeSPurushottam Kushwaha 	case CFG80211_CONN_ASSOC_FAILED_TIMEOUT:
2073093ebbeSPurushottam Kushwaha 		*treason = NL80211_TIMEOUT_ASSOC;
2083093ebbeSPurushottam Kushwaha 		/* fall through */
209923a0e7dSJohannes Berg 	case CFG80211_CONN_ASSOC_FAILED:
210923a0e7dSJohannes Berg 		cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
211923a0e7dSJohannes Berg 				     NULL, 0,
212923a0e7dSJohannes Berg 				     WLAN_REASON_DEAUTH_LEAVING, false);
213923a0e7dSJohannes Berg 		return -ENOTCONN;
214ceca7b71SJohannes Berg 	case CFG80211_CONN_DEAUTH:
21591bf9b26SJohannes Berg 		cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
2167d930bc3SJohannes Berg 				     NULL, 0,
217d5cdfacbSJouni Malinen 				     WLAN_REASON_DEAUTH_LEAVING, false);
218e6f462dfSJohannes Berg 		/* fall through */
219e6f462dfSJohannes Berg 	case CFG80211_CONN_ABANDON:
220923a0e7dSJohannes Berg 		/* free directly, disconnected event already sent */
221923a0e7dSJohannes Berg 		cfg80211_sme_free(wdev);
222ceca7b71SJohannes Berg 		return 0;
2236829c878SJohannes Berg 	default:
2246829c878SJohannes Berg 		return 0;
2256829c878SJohannes Berg 	}
2266829c878SJohannes Berg }
2276829c878SJohannes Berg 
2286829c878SJohannes Berg void cfg80211_conn_work(struct work_struct *work)
2296829c878SJohannes Berg {
23079c97e97SJohannes Berg 	struct cfg80211_registered_device *rdev =
2316829c878SJohannes Berg 		container_of(work, struct cfg80211_registered_device, conn_work);
2326829c878SJohannes Berg 	struct wireless_dev *wdev;
2337400f42eSJohannes Berg 	u8 bssid_buf[ETH_ALEN], *bssid = NULL;
2343093ebbeSPurushottam Kushwaha 	enum nl80211_timeout_reason treason;
2356829c878SJohannes Berg 
2366829c878SJohannes Berg 	rtnl_lock();
2376829c878SJohannes Berg 
23853873f13SJohannes Berg 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
239c8157976SJohannes Berg 		if (!wdev->netdev)
240c8157976SJohannes Berg 			continue;
241c8157976SJohannes Berg 
242667503ddSJohannes Berg 		wdev_lock(wdev);
243667503ddSJohannes Berg 		if (!netif_running(wdev->netdev)) {
244667503ddSJohannes Berg 			wdev_unlock(wdev);
2456829c878SJohannes Berg 			continue;
246667503ddSJohannes Berg 		}
247ceca7b71SJohannes Berg 		if (!wdev->conn ||
248ceca7b71SJohannes Berg 		    wdev->conn->state == CFG80211_CONN_CONNECTED) {
249667503ddSJohannes Berg 			wdev_unlock(wdev);
2506829c878SJohannes Berg 			continue;
251667503ddSJohannes Berg 		}
2527400f42eSJohannes Berg 		if (wdev->conn->params.bssid) {
2537400f42eSJohannes Berg 			memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
2547400f42eSJohannes Berg 			bssid = bssid_buf;
2557400f42eSJohannes Berg 		}
2563093ebbeSPurushottam Kushwaha 		treason = NL80211_TIMEOUT_UNSPECIFIED;
2573093ebbeSPurushottam Kushwaha 		if (cfg80211_conn_do_work(wdev, &treason)) {
2585349a0f7SVidyullatha Kanchanapally 			struct cfg80211_connect_resp_params cr;
2595349a0f7SVidyullatha Kanchanapally 
2605349a0f7SVidyullatha Kanchanapally 			memset(&cr, 0, sizeof(cr));
2615349a0f7SVidyullatha Kanchanapally 			cr.status = -1;
2625349a0f7SVidyullatha Kanchanapally 			cr.bssid = bssid;
2635349a0f7SVidyullatha Kanchanapally 			cr.timeout_reason = treason;
2645349a0f7SVidyullatha Kanchanapally 			__cfg80211_connect_result(wdev->netdev, &cr, false);
265ceca7b71SJohannes Berg 		}
266667503ddSJohannes Berg 		wdev_unlock(wdev);
2676829c878SJohannes Berg 	}
2686829c878SJohannes Berg 
2696829c878SJohannes Berg 	rtnl_unlock();
2706829c878SJohannes Berg }
2716829c878SJohannes Berg 
2720e3a39b5SBen Greear /* Returned bss is reference counted and must be cleaned up appropriately. */
273bbac31f4SJohannes Berg static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
2746829c878SJohannes Berg {
275f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
2766829c878SJohannes Berg 	struct cfg80211_bss *bss;
2776829c878SJohannes Berg 
278667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
279667503ddSJohannes Berg 
280ed9d0102SJouni Malinen 	bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
281ed9d0102SJouni Malinen 			       wdev->conn->params.bssid,
2826829c878SJohannes Berg 			       wdev->conn->params.ssid,
2836829c878SJohannes Berg 			       wdev->conn->params.ssid_len,
28434d50519SLior David 			       wdev->conn_bss_type,
2856eb18137SDedy Lansky 			       IEEE80211_PRIVACY(wdev->conn->params.privacy));
2866829c878SJohannes Berg 	if (!bss)
287bbac31f4SJohannes Berg 		return NULL;
2886829c878SJohannes Berg 
2896829c878SJohannes Berg 	memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
2906829c878SJohannes Berg 	wdev->conn->params.bssid = wdev->conn->bssid;
2916829c878SJohannes Berg 	wdev->conn->params.channel = bss->channel;
2926829c878SJohannes Berg 	wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
29379c97e97SJohannes Berg 	schedule_work(&rdev->conn_work);
2946829c878SJohannes Berg 
295bbac31f4SJohannes Berg 	return bss;
2966829c878SJohannes Berg }
2976829c878SJohannes Berg 
298667503ddSJohannes Berg static void __cfg80211_sme_scan_done(struct net_device *dev)
2996829c878SJohannes Berg {
3006829c878SJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
301f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
302bbac31f4SJohannes Berg 	struct cfg80211_bss *bss;
3036829c878SJohannes Berg 
304667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
305667503ddSJohannes Berg 
306d4b1a687SZhu Yi 	if (!wdev->conn)
3076829c878SJohannes Berg 		return;
3086829c878SJohannes Berg 
3096829c878SJohannes Berg 	if (wdev->conn->state != CFG80211_CONN_SCANNING &&
3106829c878SJohannes Berg 	    wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
3116829c878SJohannes Berg 		return;
3126829c878SJohannes Berg 
313bbac31f4SJohannes Berg 	bss = cfg80211_get_conn_bss(wdev);
314ceca7b71SJohannes Berg 	if (bss)
3155b112d3dSJohannes Berg 		cfg80211_put_bss(&rdev->wiphy, bss);
3166829c878SJohannes Berg 	else
317ceca7b71SJohannes Berg 		schedule_work(&rdev->conn_work);
3186829c878SJohannes Berg }
3196829c878SJohannes Berg 
320667503ddSJohannes Berg void cfg80211_sme_scan_done(struct net_device *dev)
321667503ddSJohannes Berg {
322667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
323667503ddSJohannes Berg 
324667503ddSJohannes Berg 	wdev_lock(wdev);
325667503ddSJohannes Berg 	__cfg80211_sme_scan_done(dev);
326667503ddSJohannes Berg 	wdev_unlock(wdev);
327667503ddSJohannes Berg }
328667503ddSJohannes Berg 
329ceca7b71SJohannes Berg void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
3306829c878SJohannes Berg {
3316829c878SJohannes Berg 	struct wiphy *wiphy = wdev->wiphy;
332f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
3336829c878SJohannes Berg 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
3346829c878SJohannes Berg 	u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
3356829c878SJohannes Berg 
336667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
337667503ddSJohannes Berg 
338ceca7b71SJohannes Berg 	if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
3396829c878SJohannes Berg 		return;
3406829c878SJohannes Berg 
3416829c878SJohannes Berg 	if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
3426829c878SJohannes Berg 	    wdev->conn->auto_auth &&
3436829c878SJohannes Berg 	    wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
3446829c878SJohannes Berg 		/* select automatically between only open, shared, leap */
3456829c878SJohannes Berg 		switch (wdev->conn->params.auth_type) {
3466829c878SJohannes Berg 		case NL80211_AUTHTYPE_OPEN_SYSTEM:
347fffd0934SJohannes Berg 			if (wdev->connect_keys)
3486829c878SJohannes Berg 				wdev->conn->params.auth_type =
3496829c878SJohannes Berg 					NL80211_AUTHTYPE_SHARED_KEY;
350fffd0934SJohannes Berg 			else
351fffd0934SJohannes Berg 				wdev->conn->params.auth_type =
352fffd0934SJohannes Berg 					NL80211_AUTHTYPE_NETWORK_EAP;
3536829c878SJohannes Berg 			break;
3546829c878SJohannes Berg 		case NL80211_AUTHTYPE_SHARED_KEY:
3556829c878SJohannes Berg 			wdev->conn->params.auth_type =
3566829c878SJohannes Berg 				NL80211_AUTHTYPE_NETWORK_EAP;
3576829c878SJohannes Berg 			break;
3586829c878SJohannes Berg 		default:
3596829c878SJohannes Berg 			/* huh? */
3606829c878SJohannes Berg 			wdev->conn->params.auth_type =
3616829c878SJohannes Berg 				NL80211_AUTHTYPE_OPEN_SYSTEM;
3626829c878SJohannes Berg 			break;
3636829c878SJohannes Berg 		}
3646829c878SJohannes Berg 		wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
3656829c878SJohannes Berg 		schedule_work(&rdev->conn_work);
36619957bb3SJohannes Berg 	} else if (status_code != WLAN_STATUS_SUCCESS) {
3675349a0f7SVidyullatha Kanchanapally 		struct cfg80211_connect_resp_params cr;
3685349a0f7SVidyullatha Kanchanapally 
3695349a0f7SVidyullatha Kanchanapally 		memset(&cr, 0, sizeof(cr));
3705349a0f7SVidyullatha Kanchanapally 		cr.status = status_code;
3715349a0f7SVidyullatha Kanchanapally 		cr.bssid = mgmt->bssid;
3725349a0f7SVidyullatha Kanchanapally 		cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED;
3735349a0f7SVidyullatha Kanchanapally 		__cfg80211_connect_result(wdev->netdev, &cr, false);
374ceca7b71SJohannes Berg 	} else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
3756829c878SJohannes Berg 		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
3766829c878SJohannes Berg 		schedule_work(&rdev->conn_work);
3776829c878SJohannes Berg 	}
3786829c878SJohannes Berg }
379b23aa676SSamuel Ortiz 
380ceca7b71SJohannes Berg bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
381f401a6f7SJohannes Berg {
382f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
383f401a6f7SJohannes Berg 
384ceca7b71SJohannes Berg 	if (!wdev->conn)
385f401a6f7SJohannes Berg 		return false;
386f401a6f7SJohannes Berg 
387ceca7b71SJohannes Berg 	if (status == WLAN_STATUS_SUCCESS) {
388ceca7b71SJohannes Berg 		wdev->conn->state = CFG80211_CONN_CONNECTED;
389f401a6f7SJohannes Berg 		return false;
390ceca7b71SJohannes Berg 	}
391f401a6f7SJohannes Berg 
392ceca7b71SJohannes Berg 	if (wdev->conn->prev_bssid_valid) {
393f401a6f7SJohannes Berg 		/*
394f401a6f7SJohannes Berg 		 * Some stupid APs don't accept reassoc, so we
395ceca7b71SJohannes Berg 		 * need to fall back to trying regular assoc;
396ceca7b71SJohannes Berg 		 * return true so no event is sent to userspace.
397f401a6f7SJohannes Berg 		 */
398f401a6f7SJohannes Berg 		wdev->conn->prev_bssid_valid = false;
399f401a6f7SJohannes Berg 		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
400f401a6f7SJohannes Berg 		schedule_work(&rdev->conn_work);
401f401a6f7SJohannes Berg 		return true;
402f401a6f7SJohannes Berg 	}
403f401a6f7SJohannes Berg 
404923a0e7dSJohannes Berg 	wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
405ceca7b71SJohannes Berg 	schedule_work(&rdev->conn_work);
406ceca7b71SJohannes Berg 	return false;
407ceca7b71SJohannes Berg }
4087d930bc3SJohannes Berg 
409ceca7b71SJohannes Berg void cfg80211_sme_deauth(struct wireless_dev *wdev)
410ceca7b71SJohannes Berg {
411ceca7b71SJohannes Berg 	cfg80211_sme_free(wdev);
412ceca7b71SJohannes Berg }
413ceca7b71SJohannes Berg 
414ceca7b71SJohannes Berg void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
415ceca7b71SJohannes Berg {
416f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
417923a0e7dSJohannes Berg 
418923a0e7dSJohannes Berg 	if (!wdev->conn)
419923a0e7dSJohannes Berg 		return;
420923a0e7dSJohannes Berg 
4213093ebbeSPurushottam Kushwaha 	wdev->conn->state = CFG80211_CONN_AUTH_FAILED_TIMEOUT;
422923a0e7dSJohannes Berg 	schedule_work(&rdev->conn_work);
423ceca7b71SJohannes Berg }
424ceca7b71SJohannes Berg 
425ceca7b71SJohannes Berg void cfg80211_sme_disassoc(struct wireless_dev *wdev)
426ceca7b71SJohannes Berg {
427f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
428ceca7b71SJohannes Berg 
429ceca7b71SJohannes Berg 	if (!wdev->conn)
430ceca7b71SJohannes Berg 		return;
431ceca7b71SJohannes Berg 
432ceca7b71SJohannes Berg 	wdev->conn->state = CFG80211_CONN_DEAUTH;
4337d930bc3SJohannes Berg 	schedule_work(&rdev->conn_work);
4347d930bc3SJohannes Berg }
4357d930bc3SJohannes Berg 
436ceca7b71SJohannes Berg void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
437ceca7b71SJohannes Berg {
438f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
439923a0e7dSJohannes Berg 
440923a0e7dSJohannes Berg 	if (!wdev->conn)
441923a0e7dSJohannes Berg 		return;
442923a0e7dSJohannes Berg 
4433093ebbeSPurushottam Kushwaha 	wdev->conn->state = CFG80211_CONN_ASSOC_FAILED_TIMEOUT;
444923a0e7dSJohannes Berg 	schedule_work(&rdev->conn_work);
445ceca7b71SJohannes Berg }
446ceca7b71SJohannes Berg 
447e6f462dfSJohannes Berg void cfg80211_sme_abandon_assoc(struct wireless_dev *wdev)
448e6f462dfSJohannes Berg {
449e6f462dfSJohannes Berg 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
450e6f462dfSJohannes Berg 
451e6f462dfSJohannes Berg 	if (!wdev->conn)
452e6f462dfSJohannes Berg 		return;
453e6f462dfSJohannes Berg 
454e6f462dfSJohannes Berg 	wdev->conn->state = CFG80211_CONN_ABANDON;
455e6f462dfSJohannes Berg 	schedule_work(&rdev->conn_work);
456e6f462dfSJohannes Berg }
457e6f462dfSJohannes Berg 
45846b9d180SJohannes Berg static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
45946b9d180SJohannes Berg 				     const u8 *ies, size_t ies_len,
46046b9d180SJohannes Berg 				     const u8 **out_ies, size_t *out_ies_len)
46146b9d180SJohannes Berg {
46246b9d180SJohannes Berg 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
46346b9d180SJohannes Berg 	u8 *buf;
46446b9d180SJohannes Berg 	size_t offs;
46546b9d180SJohannes Berg 
46646b9d180SJohannes Berg 	if (!rdev->wiphy.extended_capabilities_len ||
46746b9d180SJohannes Berg 	    (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
46846b9d180SJohannes Berg 		*out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
46946b9d180SJohannes Berg 		if (!*out_ies)
47046b9d180SJohannes Berg 			return -ENOMEM;
47146b9d180SJohannes Berg 		*out_ies_len = ies_len;
47246b9d180SJohannes Berg 		return 0;
47346b9d180SJohannes Berg 	}
47446b9d180SJohannes Berg 
47546b9d180SJohannes Berg 	buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
47646b9d180SJohannes Berg 		      GFP_KERNEL);
47746b9d180SJohannes Berg 	if (!buf)
47846b9d180SJohannes Berg 		return -ENOMEM;
47946b9d180SJohannes Berg 
48046b9d180SJohannes Berg 	if (ies_len) {
48146b9d180SJohannes Berg 		static const u8 before_extcapa[] = {
48246b9d180SJohannes Berg 			/* not listing IEs expected to be created by driver */
48346b9d180SJohannes Berg 			WLAN_EID_RSN,
48446b9d180SJohannes Berg 			WLAN_EID_QOS_CAPA,
48546b9d180SJohannes Berg 			WLAN_EID_RRM_ENABLED_CAPABILITIES,
48646b9d180SJohannes Berg 			WLAN_EID_MOBILITY_DOMAIN,
48746b9d180SJohannes Berg 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
48846b9d180SJohannes Berg 			WLAN_EID_BSS_COEX_2040,
48946b9d180SJohannes Berg 		};
49046b9d180SJohannes Berg 
49146b9d180SJohannes Berg 		offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
49246b9d180SJohannes Berg 					  ARRAY_SIZE(before_extcapa), 0);
49346b9d180SJohannes Berg 		memcpy(buf, ies, offs);
49446b9d180SJohannes Berg 		/* leave a whole for extended capabilities IE */
49546b9d180SJohannes Berg 		memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
49646b9d180SJohannes Berg 		       ies + offs, ies_len - offs);
49746b9d180SJohannes Berg 	} else {
49846b9d180SJohannes Berg 		offs = 0;
49946b9d180SJohannes Berg 	}
50046b9d180SJohannes Berg 
50146b9d180SJohannes Berg 	/* place extended capabilities IE (with only driver capabilities) */
50246b9d180SJohannes Berg 	buf[offs] = WLAN_EID_EXT_CAPABILITY;
50346b9d180SJohannes Berg 	buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
50446b9d180SJohannes Berg 	memcpy(buf + offs + 2,
50546b9d180SJohannes Berg 	       rdev->wiphy.extended_capabilities,
50646b9d180SJohannes Berg 	       rdev->wiphy.extended_capabilities_len);
50746b9d180SJohannes Berg 
50846b9d180SJohannes Berg 	*out_ies = buf;
50946b9d180SJohannes Berg 	*out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
51046b9d180SJohannes Berg 
51146b9d180SJohannes Berg 	return 0;
51246b9d180SJohannes Berg }
51346b9d180SJohannes Berg 
514ceca7b71SJohannes Berg static int cfg80211_sme_connect(struct wireless_dev *wdev,
515ceca7b71SJohannes Berg 				struct cfg80211_connect_params *connect,
516ceca7b71SJohannes Berg 				const u8 *prev_bssid)
517ceca7b71SJohannes Berg {
518f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
519ceca7b71SJohannes Berg 	struct cfg80211_bss *bss;
520ceca7b71SJohannes Berg 	int err;
521ceca7b71SJohannes Berg 
522ceca7b71SJohannes Berg 	if (!rdev->ops->auth || !rdev->ops->assoc)
523ceca7b71SJohannes Berg 		return -EOPNOTSUPP;
524ceca7b71SJohannes Berg 
5254ce2bd9cSJouni Malinen 	if (wdev->current_bss) {
5264ce2bd9cSJouni Malinen 		cfg80211_unhold_bss(wdev->current_bss);
5274ce2bd9cSJouni Malinen 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
5284ce2bd9cSJouni Malinen 		wdev->current_bss = NULL;
5294ce2bd9cSJouni Malinen 
5304ce2bd9cSJouni Malinen 		cfg80211_sme_free(wdev);
5314ce2bd9cSJouni Malinen 	}
532ceca7b71SJohannes Berg 
533ceca7b71SJohannes Berg 	if (WARN_ON(wdev->conn))
534ceca7b71SJohannes Berg 		return -EINPROGRESS;
535ceca7b71SJohannes Berg 
536ceca7b71SJohannes Berg 	wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
537ceca7b71SJohannes Berg 	if (!wdev->conn)
538ceca7b71SJohannes Berg 		return -ENOMEM;
539ceca7b71SJohannes Berg 
540ceca7b71SJohannes Berg 	/*
541ceca7b71SJohannes Berg 	 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
542ceca7b71SJohannes Berg 	 */
543ceca7b71SJohannes Berg 	memcpy(&wdev->conn->params, connect, sizeof(*connect));
544ceca7b71SJohannes Berg 	if (connect->bssid) {
545ceca7b71SJohannes Berg 		wdev->conn->params.bssid = wdev->conn->bssid;
546ceca7b71SJohannes Berg 		memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
547ceca7b71SJohannes Berg 	}
548ceca7b71SJohannes Berg 
54946b9d180SJohannes Berg 	if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
55046b9d180SJohannes Berg 				      &wdev->conn->ie,
55146b9d180SJohannes Berg 				      &wdev->conn->params.ie_len)) {
552ceca7b71SJohannes Berg 		kfree(wdev->conn);
553ceca7b71SJohannes Berg 		wdev->conn = NULL;
554ceca7b71SJohannes Berg 		return -ENOMEM;
555ceca7b71SJohannes Berg 	}
55646b9d180SJohannes Berg 	wdev->conn->params.ie = wdev->conn->ie;
557ceca7b71SJohannes Berg 
558ceca7b71SJohannes Berg 	if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
559ceca7b71SJohannes Berg 		wdev->conn->auto_auth = true;
560ceca7b71SJohannes Berg 		/* start with open system ... should mostly work */
561ceca7b71SJohannes Berg 		wdev->conn->params.auth_type =
562ceca7b71SJohannes Berg 			NL80211_AUTHTYPE_OPEN_SYSTEM;
563ceca7b71SJohannes Berg 	} else {
564ceca7b71SJohannes Berg 		wdev->conn->auto_auth = false;
565ceca7b71SJohannes Berg 	}
566ceca7b71SJohannes Berg 
567ceca7b71SJohannes Berg 	wdev->conn->params.ssid = wdev->ssid;
568babd3a27SZhao, Gang 	wdev->conn->params.ssid_len = wdev->ssid_len;
569ceca7b71SJohannes Berg 
570ceca7b71SJohannes Berg 	/* see if we have the bss already */
571ceca7b71SJohannes Berg 	bss = cfg80211_get_conn_bss(wdev);
572ceca7b71SJohannes Berg 
573ceca7b71SJohannes Berg 	if (prev_bssid) {
574ceca7b71SJohannes Berg 		memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
575ceca7b71SJohannes Berg 		wdev->conn->prev_bssid_valid = true;
576ceca7b71SJohannes Berg 	}
577ceca7b71SJohannes Berg 
578ceca7b71SJohannes Berg 	/* we're good if we have a matching bss struct */
579ceca7b71SJohannes Berg 	if (bss) {
5803093ebbeSPurushottam Kushwaha 		enum nl80211_timeout_reason treason;
5813093ebbeSPurushottam Kushwaha 
5823093ebbeSPurushottam Kushwaha 		err = cfg80211_conn_do_work(wdev, &treason);
583ceca7b71SJohannes Berg 		cfg80211_put_bss(wdev->wiphy, bss);
584ceca7b71SJohannes Berg 	} else {
585ceca7b71SJohannes Berg 		/* otherwise we'll need to scan for the AP first */
586ceca7b71SJohannes Berg 		err = cfg80211_conn_scan(wdev);
587ceca7b71SJohannes Berg 
588ceca7b71SJohannes Berg 		/*
589ceca7b71SJohannes Berg 		 * If we can't scan right now, then we need to scan again
590ceca7b71SJohannes Berg 		 * after the current scan finished, since the parameters
591ceca7b71SJohannes Berg 		 * changed (unless we find a good AP anyway).
592ceca7b71SJohannes Berg 		 */
593ceca7b71SJohannes Berg 		if (err == -EBUSY) {
594ceca7b71SJohannes Berg 			err = 0;
595ceca7b71SJohannes Berg 			wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
596ceca7b71SJohannes Berg 		}
597ceca7b71SJohannes Berg 	}
598ceca7b71SJohannes Berg 
599ceca7b71SJohannes Berg 	if (err)
600ceca7b71SJohannes Berg 		cfg80211_sme_free(wdev);
601ceca7b71SJohannes Berg 
602ceca7b71SJohannes Berg 	return err;
603ceca7b71SJohannes Berg }
604ceca7b71SJohannes Berg 
605ceca7b71SJohannes Berg static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
606ceca7b71SJohannes Berg {
607f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
608ceca7b71SJohannes Berg 	int err;
609ceca7b71SJohannes Berg 
610ceca7b71SJohannes Berg 	if (!wdev->conn)
611ceca7b71SJohannes Berg 		return 0;
612ceca7b71SJohannes Berg 
613ceca7b71SJohannes Berg 	if (!rdev->ops->deauth)
614ceca7b71SJohannes Berg 		return -EOPNOTSUPP;
615ceca7b71SJohannes Berg 
616ceca7b71SJohannes Berg 	if (wdev->conn->state == CFG80211_CONN_SCANNING ||
617ceca7b71SJohannes Berg 	    wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
618ceca7b71SJohannes Berg 		err = 0;
619ceca7b71SJohannes Berg 		goto out;
620ceca7b71SJohannes Berg 	}
621ceca7b71SJohannes Berg 
622ceca7b71SJohannes Berg 	/* wdev->conn->params.bssid must be set if > SCANNING */
623ceca7b71SJohannes Berg 	err = cfg80211_mlme_deauth(rdev, wdev->netdev,
624ceca7b71SJohannes Berg 				   wdev->conn->params.bssid,
625ceca7b71SJohannes Berg 				   NULL, 0, reason, false);
626ceca7b71SJohannes Berg  out:
627ceca7b71SJohannes Berg 	cfg80211_sme_free(wdev);
628ceca7b71SJohannes Berg 	return err;
629ceca7b71SJohannes Berg }
630ceca7b71SJohannes Berg 
631ceca7b71SJohannes Berg /*
632ceca7b71SJohannes Berg  * code shared for in-device and software SME
633ceca7b71SJohannes Berg  */
634ceca7b71SJohannes Berg 
635ceca7b71SJohannes Berg static bool cfg80211_is_all_idle(void)
636ceca7b71SJohannes Berg {
637ceca7b71SJohannes Berg 	struct cfg80211_registered_device *rdev;
638ceca7b71SJohannes Berg 	struct wireless_dev *wdev;
639ceca7b71SJohannes Berg 	bool is_all_idle = true;
640ceca7b71SJohannes Berg 
641ceca7b71SJohannes Berg 	/*
642ceca7b71SJohannes Berg 	 * All devices must be idle as otherwise if you are actively
643ceca7b71SJohannes Berg 	 * scanning some new beacon hints could be learned and would
644ceca7b71SJohannes Berg 	 * count as new regulatory hints.
645113f3aaaSSriram R 	 * Also if there is any other active beaconing interface we
646113f3aaaSSriram R 	 * need not issue a disconnect hint and reset any info such
647113f3aaaSSriram R 	 * as chan dfs state, etc.
648ceca7b71SJohannes Berg 	 */
649ceca7b71SJohannes Berg 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
65053873f13SJohannes Berg 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
651ceca7b71SJohannes Berg 			wdev_lock(wdev);
652113f3aaaSSriram R 			if (wdev->conn || wdev->current_bss ||
653113f3aaaSSriram R 			    cfg80211_beaconing_iface_active(wdev))
654ceca7b71SJohannes Berg 				is_all_idle = false;
655ceca7b71SJohannes Berg 			wdev_unlock(wdev);
656ceca7b71SJohannes Berg 		}
657ceca7b71SJohannes Berg 	}
658ceca7b71SJohannes Berg 
659ceca7b71SJohannes Berg 	return is_all_idle;
660ceca7b71SJohannes Berg }
661ceca7b71SJohannes Berg 
662ceca7b71SJohannes Berg static void disconnect_work(struct work_struct *work)
663ceca7b71SJohannes Berg {
664ceca7b71SJohannes Berg 	rtnl_lock();
665ceca7b71SJohannes Berg 	if (cfg80211_is_all_idle())
666ceca7b71SJohannes Berg 		regulatory_hint_disconnect();
667ceca7b71SJohannes Berg 	rtnl_unlock();
668ceca7b71SJohannes Berg }
669ceca7b71SJohannes Berg 
670e005bd7dSJohannes Berg DECLARE_WORK(cfg80211_disconnect_work, disconnect_work);
671ceca7b71SJohannes Berg 
672ceca7b71SJohannes Berg 
673ceca7b71SJohannes Berg /*
674ceca7b71SJohannes Berg  * API calls for drivers implementing connect/disconnect and
675ceca7b71SJohannes Berg  * SME event handling
676ceca7b71SJohannes Berg  */
677ceca7b71SJohannes Berg 
6786f390908SBen Greear /* This method must consume bss one way or another */
6795349a0f7SVidyullatha Kanchanapally void __cfg80211_connect_result(struct net_device *dev,
6805349a0f7SVidyullatha Kanchanapally 			       struct cfg80211_connect_resp_params *cr,
6815349a0f7SVidyullatha Kanchanapally 			       bool wextev)
682b23aa676SSamuel Ortiz {
683b23aa676SSamuel Ortiz 	struct wireless_dev *wdev = dev->ieee80211_ptr;
6849caf0364SJohannes Berg 	const u8 *country_ie;
6853d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
686b23aa676SSamuel Ortiz 	union iwreq_data wrqu;
687b23aa676SSamuel Ortiz #endif
688b23aa676SSamuel Ortiz 
689667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
690667503ddSJohannes Berg 
691074ac8dfSJohannes Berg 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
6926f390908SBen Greear 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)) {
6935349a0f7SVidyullatha Kanchanapally 		cfg80211_put_bss(wdev->wiphy, cr->bss);
694b23aa676SSamuel Ortiz 		return;
6956f390908SBen Greear 	}
696b23aa676SSamuel Ortiz 
697*4d797fceSJouni Malinen 	wdev->unprot_beacon_reported = 0;
6985349a0f7SVidyullatha Kanchanapally 	nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev, cr,
6995349a0f7SVidyullatha Kanchanapally 				    GFP_KERNEL);
700e45cd82aSJohannes Berg 
7013d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
702e45cd82aSJohannes Berg 	if (wextev) {
7035349a0f7SVidyullatha Kanchanapally 		if (cr->req_ie && cr->status == WLAN_STATUS_SUCCESS) {
704e45cd82aSJohannes Berg 			memset(&wrqu, 0, sizeof(wrqu));
7055349a0f7SVidyullatha Kanchanapally 			wrqu.data.length = cr->req_ie_len;
7065349a0f7SVidyullatha Kanchanapally 			wireless_send_event(dev, IWEVASSOCREQIE, &wrqu,
7075349a0f7SVidyullatha Kanchanapally 					    cr->req_ie);
708e45cd82aSJohannes Berg 		}
709e45cd82aSJohannes Berg 
7105349a0f7SVidyullatha Kanchanapally 		if (cr->resp_ie && cr->status == WLAN_STATUS_SUCCESS) {
711e45cd82aSJohannes Berg 			memset(&wrqu, 0, sizeof(wrqu));
7125349a0f7SVidyullatha Kanchanapally 			wrqu.data.length = cr->resp_ie_len;
7135349a0f7SVidyullatha Kanchanapally 			wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu,
7145349a0f7SVidyullatha Kanchanapally 					    cr->resp_ie);
715e45cd82aSJohannes Berg 		}
716e45cd82aSJohannes Berg 
717e45cd82aSJohannes Berg 		memset(&wrqu, 0, sizeof(wrqu));
718e45cd82aSJohannes Berg 		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
7195349a0f7SVidyullatha Kanchanapally 		if (cr->bssid && cr->status == WLAN_STATUS_SUCCESS) {
7205349a0f7SVidyullatha Kanchanapally 			memcpy(wrqu.ap_addr.sa_data, cr->bssid, ETH_ALEN);
7215349a0f7SVidyullatha Kanchanapally 			memcpy(wdev->wext.prev_bssid, cr->bssid, ETH_ALEN);
722f401a6f7SJohannes Berg 			wdev->wext.prev_bssid_valid = true;
723f401a6f7SJohannes Berg 		}
724e45cd82aSJohannes Berg 		wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
725e45cd82aSJohannes Berg 	}
726e45cd82aSJohannes Berg #endif
727e45cd82aSJohannes Berg 
7285349a0f7SVidyullatha Kanchanapally 	if (!cr->bss && (cr->status == WLAN_STATUS_SUCCESS)) {
729f26cbf40SZhao, Gang 		WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
7305349a0f7SVidyullatha Kanchanapally 		cr->bss = cfg80211_get_bss(wdev->wiphy, NULL, cr->bssid,
7314c4d684aSUjjal Roy 					   wdev->ssid, wdev->ssid_len,
73234d50519SLior David 					   wdev->conn_bss_type,
7336eb18137SDedy Lansky 					   IEEE80211_PRIVACY_ANY);
7345349a0f7SVidyullatha Kanchanapally 		if (cr->bss)
7355349a0f7SVidyullatha Kanchanapally 			cfg80211_hold_bss(bss_from_pub(cr->bss));
7364c4d684aSUjjal Roy 	}
7374c4d684aSUjjal Roy 
738df7fc0f9SJohannes Berg 	if (wdev->current_bss) {
739df7fc0f9SJohannes Berg 		cfg80211_unhold_bss(wdev->current_bss);
7405b112d3dSJohannes Berg 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
741df7fc0f9SJohannes Berg 		wdev->current_bss = NULL;
742df7fc0f9SJohannes Berg 	}
743df7fc0f9SJohannes Berg 
7445349a0f7SVidyullatha Kanchanapally 	if (cr->status != WLAN_STATUS_SUCCESS) {
745b47f610bSJohannes Berg 		kzfree(wdev->connect_keys);
746fffd0934SJohannes Berg 		wdev->connect_keys = NULL;
7478dadadb7SJohannes Berg 		wdev->ssid_len = 0;
748bd2522b1SAndrzej Zaborowski 		wdev->conn_owner_nlportid = 0;
7495349a0f7SVidyullatha Kanchanapally 		if (cr->bss) {
7505349a0f7SVidyullatha Kanchanapally 			cfg80211_unhold_bss(bss_from_pub(cr->bss));
7515349a0f7SVidyullatha Kanchanapally 			cfg80211_put_bss(wdev->wiphy, cr->bss);
752f1940c57SJohannes Berg 		}
753c1fbb258SEliad Peller 		cfg80211_sme_free(wdev);
754fffd0934SJohannes Berg 		return;
755fffd0934SJohannes Berg 	}
756fffd0934SJohannes Berg 
7575349a0f7SVidyullatha Kanchanapally 	if (WARN_ON(!cr->bss))
758b23aa676SSamuel Ortiz 		return;
759f1940c57SJohannes Berg 
7605349a0f7SVidyullatha Kanchanapally 	wdev->current_bss = bss_from_pub(cr->bss);
761b23aa676SSamuel Ortiz 
762b8676221SDavid Spinadel 	if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
763fffd0934SJohannes Berg 		cfg80211_upload_connect_keys(wdev);
7648b19e6caSLuis R. Rodriguez 
7659caf0364SJohannes Berg 	rcu_read_lock();
7665349a0f7SVidyullatha Kanchanapally 	country_ie = ieee80211_bss_get_ie(cr->bss, WLAN_EID_COUNTRY);
7679caf0364SJohannes Berg 	if (!country_ie) {
7689caf0364SJohannes Berg 		rcu_read_unlock();
7699caf0364SJohannes Berg 		return;
7709caf0364SJohannes Berg 	}
7719caf0364SJohannes Berg 
7729caf0364SJohannes Berg 	country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
7739caf0364SJohannes Berg 	rcu_read_unlock();
7748b19e6caSLuis R. Rodriguez 
7758b19e6caSLuis R. Rodriguez 	if (!country_ie)
7768b19e6caSLuis R. Rodriguez 		return;
7778b19e6caSLuis R. Rodriguez 
7788b19e6caSLuis R. Rodriguez 	/*
7798b19e6caSLuis R. Rodriguez 	 * ieee80211_bss_get_ie() ensures we can access:
7808b19e6caSLuis R. Rodriguez 	 * - country_ie + 2, the start of the country ie data, and
7818b19e6caSLuis R. Rodriguez 	 * - and country_ie[1] which is the IE length
7828b19e6caSLuis R. Rodriguez 	 */
7835349a0f7SVidyullatha Kanchanapally 	regulatory_hint_country_ie(wdev->wiphy, cr->bss->channel->band,
7841a919318SJohannes Berg 				   country_ie + 2, country_ie[1]);
7859caf0364SJohannes Berg 	kfree(country_ie);
786b23aa676SSamuel Ortiz }
787f2129354SJohannes Berg 
788e7054989SKanchanapally, Vidyullatha /* Consumes bss object one way or another */
7895349a0f7SVidyullatha Kanchanapally void cfg80211_connect_done(struct net_device *dev,
7905349a0f7SVidyullatha Kanchanapally 			   struct cfg80211_connect_resp_params *params,
7915349a0f7SVidyullatha Kanchanapally 			   gfp_t gfp)
792f2129354SJohannes Berg {
793667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
794f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
795667503ddSJohannes Berg 	struct cfg80211_event *ev;
796667503ddSJohannes Berg 	unsigned long flags;
7975349a0f7SVidyullatha Kanchanapally 	u8 *next;
798667503ddSJohannes Berg 
7995349a0f7SVidyullatha Kanchanapally 	if (params->bss) {
8005349a0f7SVidyullatha Kanchanapally 		struct cfg80211_internal_bss *ibss = bss_from_pub(params->bss);
801e7054989SKanchanapally, Vidyullatha 
802a3ce17d1SChaitanya Tata 		if (list_empty(&ibss->list)) {
803a3ce17d1SChaitanya Tata 			struct cfg80211_bss *found = NULL, *tmp = params->bss;
804a3ce17d1SChaitanya Tata 
805a3ce17d1SChaitanya Tata 			found = cfg80211_get_bss(wdev->wiphy, NULL,
806a3ce17d1SChaitanya Tata 						 params->bss->bssid,
807a3ce17d1SChaitanya Tata 						 wdev->ssid, wdev->ssid_len,
808a3ce17d1SChaitanya Tata 						 wdev->conn_bss_type,
809a3ce17d1SChaitanya Tata 						 IEEE80211_PRIVACY_ANY);
810a3ce17d1SChaitanya Tata 			if (found) {
811a3ce17d1SChaitanya Tata 				/* The same BSS is already updated so use it
812a3ce17d1SChaitanya Tata 				 * instead, as it has latest info.
813a3ce17d1SChaitanya Tata 				 */
814a3ce17d1SChaitanya Tata 				params->bss = found;
815a3ce17d1SChaitanya Tata 			} else {
816a3ce17d1SChaitanya Tata 				/* Update with BSS provided by driver, it will
817a3ce17d1SChaitanya Tata 				 * be freshly added and ref cnted, we can free
818a3ce17d1SChaitanya Tata 				 * the old one.
819a3ce17d1SChaitanya Tata 				 *
820a3ce17d1SChaitanya Tata 				 * signal_valid can be false, as we are not
821a3ce17d1SChaitanya Tata 				 * expecting the BSS to be found.
822a3ce17d1SChaitanya Tata 				 *
823a3ce17d1SChaitanya Tata 				 * keep the old timestamp to avoid confusion
824a3ce17d1SChaitanya Tata 				 */
825a3ce17d1SChaitanya Tata 				cfg80211_bss_update(rdev, ibss, false,
826a3ce17d1SChaitanya Tata 						    ibss->ts);
827a3ce17d1SChaitanya Tata 			}
828a3ce17d1SChaitanya Tata 
829a3ce17d1SChaitanya Tata 			cfg80211_put_bss(wdev->wiphy, tmp);
830e7054989SKanchanapally, Vidyullatha 		}
831e7054989SKanchanapally, Vidyullatha 	}
832e7054989SKanchanapally, Vidyullatha 
8335349a0f7SVidyullatha Kanchanapally 	ev = kzalloc(sizeof(*ev) + (params->bssid ? ETH_ALEN : 0) +
834a3caf744SVidyullatha Kanchanapally 		     params->req_ie_len + params->resp_ie_len +
83576804d28SArend Van Spriel 		     params->fils.kek_len + params->fils.pmk_len +
83676804d28SArend Van Spriel 		     (params->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
837e7054989SKanchanapally, Vidyullatha 	if (!ev) {
8385349a0f7SVidyullatha Kanchanapally 		cfg80211_put_bss(wdev->wiphy, params->bss);
839e7054989SKanchanapally, Vidyullatha 		return;
840e7054989SKanchanapally, Vidyullatha 	}
841667503ddSJohannes Berg 
842667503ddSJohannes Berg 	ev->type = EVENT_CONNECT_RESULT;
8435349a0f7SVidyullatha Kanchanapally 	next = ((u8 *)ev) + sizeof(*ev);
8445349a0f7SVidyullatha Kanchanapally 	if (params->bssid) {
8455349a0f7SVidyullatha Kanchanapally 		ev->cr.bssid = next;
8465349a0f7SVidyullatha Kanchanapally 		memcpy((void *)ev->cr.bssid, params->bssid, ETH_ALEN);
8475349a0f7SVidyullatha Kanchanapally 		next += ETH_ALEN;
8487834704bSNishant Sarmukadam 	}
8495349a0f7SVidyullatha Kanchanapally 	if (params->req_ie_len) {
8505349a0f7SVidyullatha Kanchanapally 		ev->cr.req_ie = next;
8515349a0f7SVidyullatha Kanchanapally 		ev->cr.req_ie_len = params->req_ie_len;
8525349a0f7SVidyullatha Kanchanapally 		memcpy((void *)ev->cr.req_ie, params->req_ie,
8535349a0f7SVidyullatha Kanchanapally 		       params->req_ie_len);
8545349a0f7SVidyullatha Kanchanapally 		next += params->req_ie_len;
8557834704bSNishant Sarmukadam 	}
8565349a0f7SVidyullatha Kanchanapally 	if (params->resp_ie_len) {
8575349a0f7SVidyullatha Kanchanapally 		ev->cr.resp_ie = next;
8585349a0f7SVidyullatha Kanchanapally 		ev->cr.resp_ie_len = params->resp_ie_len;
8595349a0f7SVidyullatha Kanchanapally 		memcpy((void *)ev->cr.resp_ie, params->resp_ie,
8605349a0f7SVidyullatha Kanchanapally 		       params->resp_ie_len);
8615349a0f7SVidyullatha Kanchanapally 		next += params->resp_ie_len;
8625349a0f7SVidyullatha Kanchanapally 	}
86376804d28SArend Van Spriel 	if (params->fils.kek_len) {
86476804d28SArend Van Spriel 		ev->cr.fils.kek = next;
86576804d28SArend Van Spriel 		ev->cr.fils.kek_len = params->fils.kek_len;
86676804d28SArend Van Spriel 		memcpy((void *)ev->cr.fils.kek, params->fils.kek,
86776804d28SArend Van Spriel 		       params->fils.kek_len);
86876804d28SArend Van Spriel 		next += params->fils.kek_len;
869a3caf744SVidyullatha Kanchanapally 	}
87076804d28SArend Van Spriel 	if (params->fils.pmk_len) {
87176804d28SArend Van Spriel 		ev->cr.fils.pmk = next;
87276804d28SArend Van Spriel 		ev->cr.fils.pmk_len = params->fils.pmk_len;
87376804d28SArend Van Spriel 		memcpy((void *)ev->cr.fils.pmk, params->fils.pmk,
87476804d28SArend Van Spriel 		       params->fils.pmk_len);
87576804d28SArend Van Spriel 		next += params->fils.pmk_len;
876a3caf744SVidyullatha Kanchanapally 	}
87776804d28SArend Van Spriel 	if (params->fils.pmkid) {
87876804d28SArend Van Spriel 		ev->cr.fils.pmkid = next;
87976804d28SArend Van Spriel 		memcpy((void *)ev->cr.fils.pmkid, params->fils.pmkid,
88076804d28SArend Van Spriel 		       WLAN_PMKID_LEN);
881a3caf744SVidyullatha Kanchanapally 		next += WLAN_PMKID_LEN;
882a3caf744SVidyullatha Kanchanapally 	}
88376804d28SArend Van Spriel 	ev->cr.fils.update_erp_next_seq_num = params->fils.update_erp_next_seq_num;
88476804d28SArend Van Spriel 	if (params->fils.update_erp_next_seq_num)
88576804d28SArend Van Spriel 		ev->cr.fils.erp_next_seq_num = params->fils.erp_next_seq_num;
8865349a0f7SVidyullatha Kanchanapally 	if (params->bss)
8875349a0f7SVidyullatha Kanchanapally 		cfg80211_hold_bss(bss_from_pub(params->bss));
8885349a0f7SVidyullatha Kanchanapally 	ev->cr.bss = params->bss;
8895349a0f7SVidyullatha Kanchanapally 	ev->cr.status = params->status;
8905349a0f7SVidyullatha Kanchanapally 	ev->cr.timeout_reason = params->timeout_reason;
891667503ddSJohannes Berg 
892667503ddSJohannes Berg 	spin_lock_irqsave(&wdev->event_lock, flags);
893667503ddSJohannes Berg 	list_add_tail(&ev->list, &wdev->event_list);
894667503ddSJohannes Berg 	spin_unlock_irqrestore(&wdev->event_lock, flags);
895e60d7443SAlban Browaeys 	queue_work(cfg80211_wq, &rdev->event_work);
896f2129354SJohannes Berg }
8975349a0f7SVidyullatha Kanchanapally EXPORT_SYMBOL(cfg80211_connect_done);
898b23aa676SSamuel Ortiz 
8990e3a39b5SBen Greear /* Consumes bss object one way or another */
900ed9d0102SJouni Malinen void __cfg80211_roamed(struct wireless_dev *wdev,
90129ce6ecbSAvraham Stern 		       struct cfg80211_roam_info *info)
902b23aa676SSamuel Ortiz {
9033d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
904b23aa676SSamuel Ortiz 	union iwreq_data wrqu;
905b23aa676SSamuel Ortiz #endif
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))
910adbde344SVasanthakumar Thiagarajan 		goto out;
911b23aa676SSamuel Ortiz 
912ceca7b71SJohannes Berg 	if (WARN_ON(!wdev->current_bss))
913adbde344SVasanthakumar Thiagarajan 		goto out;
914b23aa676SSamuel Ortiz 
915b23aa676SSamuel Ortiz 	cfg80211_unhold_bss(wdev->current_bss);
9165b112d3dSJohannes Berg 	cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
917b23aa676SSamuel Ortiz 	wdev->current_bss = NULL;
918b23aa676SSamuel Ortiz 
91929ce6ecbSAvraham Stern 	if (WARN_ON(!info->bss))
92029ce6ecbSAvraham Stern 		return;
92129ce6ecbSAvraham Stern 
92229ce6ecbSAvraham Stern 	cfg80211_hold_bss(bss_from_pub(info->bss));
92329ce6ecbSAvraham Stern 	wdev->current_bss = bss_from_pub(info->bss);
924b23aa676SSamuel Ortiz 
925*4d797fceSJouni Malinen 	wdev->unprot_beacon_reported = 0;
926f26cbf40SZhao, Gang 	nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
92729ce6ecbSAvraham Stern 			    wdev->netdev, info, GFP_KERNEL);
928b23aa676SSamuel Ortiz 
9293d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
93029ce6ecbSAvraham Stern 	if (info->req_ie) {
931b23aa676SSamuel Ortiz 		memset(&wrqu, 0, sizeof(wrqu));
93229ce6ecbSAvraham Stern 		wrqu.data.length = info->req_ie_len;
9333409ff77SZhu Yi 		wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
93429ce6ecbSAvraham Stern 				    &wrqu, info->req_ie);
935b23aa676SSamuel Ortiz 	}
936b23aa676SSamuel Ortiz 
93729ce6ecbSAvraham Stern 	if (info->resp_ie) {
938b23aa676SSamuel Ortiz 		memset(&wrqu, 0, sizeof(wrqu));
93929ce6ecbSAvraham Stern 		wrqu.data.length = info->resp_ie_len;
940667503ddSJohannes Berg 		wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
94129ce6ecbSAvraham Stern 				    &wrqu, info->resp_ie);
942b23aa676SSamuel Ortiz 	}
943b23aa676SSamuel Ortiz 
944b23aa676SSamuel Ortiz 	memset(&wrqu, 0, sizeof(wrqu));
945b23aa676SSamuel Ortiz 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
94629ce6ecbSAvraham Stern 	memcpy(wrqu.ap_addr.sa_data, info->bss->bssid, ETH_ALEN);
94729ce6ecbSAvraham Stern 	memcpy(wdev->wext.prev_bssid, info->bss->bssid, ETH_ALEN);
948f401a6f7SJohannes Berg 	wdev->wext.prev_bssid_valid = true;
949667503ddSJohannes Berg 	wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
950b23aa676SSamuel Ortiz #endif
951adbde344SVasanthakumar Thiagarajan 
952adbde344SVasanthakumar Thiagarajan 	return;
953adbde344SVasanthakumar Thiagarajan out:
95429ce6ecbSAvraham Stern 	cfg80211_put_bss(wdev->wiphy, info->bss);
955b23aa676SSamuel Ortiz }
956667503ddSJohannes Berg 
95729ce6ecbSAvraham Stern /* Consumes info->bss object one way or another */
95829ce6ecbSAvraham Stern void cfg80211_roamed(struct net_device *dev, struct cfg80211_roam_info *info,
95929ce6ecbSAvraham Stern 		     gfp_t gfp)
960adbde344SVasanthakumar Thiagarajan {
961adbde344SVasanthakumar Thiagarajan 	struct wireless_dev *wdev = dev->ieee80211_ptr;
962f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
963667503ddSJohannes Berg 	struct cfg80211_event *ev;
964667503ddSJohannes Berg 	unsigned long flags;
965e841b7b1SArend Van Spriel 	u8 *next;
966667503ddSJohannes Berg 
96729ce6ecbSAvraham Stern 	if (!info->bss) {
96829ce6ecbSAvraham Stern 		info->bss = cfg80211_get_bss(wdev->wiphy, info->channel,
96929ce6ecbSAvraham Stern 					     info->bssid, wdev->ssid,
97029ce6ecbSAvraham Stern 					     wdev->ssid_len,
97129ce6ecbSAvraham Stern 					     wdev->conn_bss_type,
97229ce6ecbSAvraham Stern 					     IEEE80211_PRIVACY_ANY);
97329ce6ecbSAvraham Stern 	}
97429ce6ecbSAvraham Stern 
97529ce6ecbSAvraham Stern 	if (WARN_ON(!info->bss))
976667503ddSJohannes Berg 		return;
977667503ddSJohannes Berg 
978e841b7b1SArend Van Spriel 	ev = kzalloc(sizeof(*ev) + info->req_ie_len + info->resp_ie_len +
979e841b7b1SArend Van Spriel 		     info->fils.kek_len + info->fils.pmk_len +
980e841b7b1SArend Van Spriel 		     (info->fils.pmkid ? WLAN_PMKID_LEN : 0), gfp);
981adbde344SVasanthakumar Thiagarajan 	if (!ev) {
98229ce6ecbSAvraham Stern 		cfg80211_put_bss(wdev->wiphy, info->bss);
983adbde344SVasanthakumar Thiagarajan 		return;
984adbde344SVasanthakumar Thiagarajan 	}
985adbde344SVasanthakumar Thiagarajan 
986667503ddSJohannes Berg 	ev->type = EVENT_ROAMED;
987e841b7b1SArend Van Spriel 	next = ((u8 *)ev) + sizeof(*ev);
988e841b7b1SArend Van Spriel 	if (info->req_ie_len) {
989e841b7b1SArend Van Spriel 		ev->rm.req_ie = next;
99029ce6ecbSAvraham Stern 		ev->rm.req_ie_len = info->req_ie_len;
99129ce6ecbSAvraham Stern 		memcpy((void *)ev->rm.req_ie, info->req_ie, info->req_ie_len);
992e841b7b1SArend Van Spriel 		next += info->req_ie_len;
993e841b7b1SArend Van Spriel 	}
994e841b7b1SArend Van Spriel 	if (info->resp_ie_len) {
995e841b7b1SArend Van Spriel 		ev->rm.resp_ie = next;
99629ce6ecbSAvraham Stern 		ev->rm.resp_ie_len = info->resp_ie_len;
997e841b7b1SArend Van Spriel 		memcpy((void *)ev->rm.resp_ie, info->resp_ie,
998e841b7b1SArend Van Spriel 		       info->resp_ie_len);
999e841b7b1SArend Van Spriel 		next += info->resp_ie_len;
1000e841b7b1SArend Van Spriel 	}
1001e841b7b1SArend Van Spriel 	if (info->fils.kek_len) {
1002e841b7b1SArend Van Spriel 		ev->rm.fils.kek = next;
1003e841b7b1SArend Van Spriel 		ev->rm.fils.kek_len = info->fils.kek_len;
1004e841b7b1SArend Van Spriel 		memcpy((void *)ev->rm.fils.kek, info->fils.kek,
1005e841b7b1SArend Van Spriel 		       info->fils.kek_len);
1006e841b7b1SArend Van Spriel 		next += info->fils.kek_len;
1007e841b7b1SArend Van Spriel 	}
1008e841b7b1SArend Van Spriel 	if (info->fils.pmk_len) {
1009e841b7b1SArend Van Spriel 		ev->rm.fils.pmk = next;
1010e841b7b1SArend Van Spriel 		ev->rm.fils.pmk_len = info->fils.pmk_len;
1011e841b7b1SArend Van Spriel 		memcpy((void *)ev->rm.fils.pmk, info->fils.pmk,
1012e841b7b1SArend Van Spriel 		       info->fils.pmk_len);
1013e841b7b1SArend Van Spriel 		next += info->fils.pmk_len;
1014e841b7b1SArend Van Spriel 	}
1015e841b7b1SArend Van Spriel 	if (info->fils.pmkid) {
1016e841b7b1SArend Van Spriel 		ev->rm.fils.pmkid = next;
1017e841b7b1SArend Van Spriel 		memcpy((void *)ev->rm.fils.pmkid, info->fils.pmkid,
1018e841b7b1SArend Van Spriel 		       WLAN_PMKID_LEN);
1019e841b7b1SArend Van Spriel 		next += WLAN_PMKID_LEN;
1020e841b7b1SArend Van Spriel 	}
1021e841b7b1SArend Van Spriel 	ev->rm.fils.update_erp_next_seq_num = info->fils.update_erp_next_seq_num;
1022e841b7b1SArend Van Spriel 	if (info->fils.update_erp_next_seq_num)
1023e841b7b1SArend Van Spriel 		ev->rm.fils.erp_next_seq_num = info->fils.erp_next_seq_num;
102429ce6ecbSAvraham Stern 	ev->rm.bss = info->bss;
1025667503ddSJohannes Berg 
1026667503ddSJohannes Berg 	spin_lock_irqsave(&wdev->event_lock, flags);
1027667503ddSJohannes Berg 	list_add_tail(&ev->list, &wdev->event_list);
1028667503ddSJohannes Berg 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1029e60d7443SAlban Browaeys 	queue_work(cfg80211_wq, &rdev->event_work);
1030667503ddSJohannes Berg }
103129ce6ecbSAvraham Stern EXPORT_SYMBOL(cfg80211_roamed);
1032b23aa676SSamuel Ortiz 
1033503c1fb9SAvraham Stern void __cfg80211_port_authorized(struct wireless_dev *wdev, const u8 *bssid)
1034503c1fb9SAvraham Stern {
1035503c1fb9SAvraham Stern 	ASSERT_WDEV_LOCK(wdev);
1036503c1fb9SAvraham Stern 
1037503c1fb9SAvraham Stern 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
1038503c1fb9SAvraham Stern 		return;
1039503c1fb9SAvraham Stern 
1040503c1fb9SAvraham Stern 	if (WARN_ON(!wdev->current_bss) ||
1041503c1fb9SAvraham Stern 	    WARN_ON(!ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
1042503c1fb9SAvraham Stern 		return;
1043503c1fb9SAvraham Stern 
1044503c1fb9SAvraham Stern 	nl80211_send_port_authorized(wiphy_to_rdev(wdev->wiphy), wdev->netdev,
1045503c1fb9SAvraham Stern 				     bssid);
1046503c1fb9SAvraham Stern }
1047503c1fb9SAvraham Stern 
1048503c1fb9SAvraham Stern void cfg80211_port_authorized(struct net_device *dev, const u8 *bssid,
1049503c1fb9SAvraham Stern 			      gfp_t gfp)
1050503c1fb9SAvraham Stern {
1051503c1fb9SAvraham Stern 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1052503c1fb9SAvraham Stern 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1053503c1fb9SAvraham Stern 	struct cfg80211_event *ev;
1054503c1fb9SAvraham Stern 	unsigned long flags;
1055503c1fb9SAvraham Stern 
1056503c1fb9SAvraham Stern 	if (WARN_ON(!bssid))
1057503c1fb9SAvraham Stern 		return;
1058503c1fb9SAvraham Stern 
1059503c1fb9SAvraham Stern 	ev = kzalloc(sizeof(*ev), gfp);
1060503c1fb9SAvraham Stern 	if (!ev)
1061503c1fb9SAvraham Stern 		return;
1062503c1fb9SAvraham Stern 
1063503c1fb9SAvraham Stern 	ev->type = EVENT_PORT_AUTHORIZED;
1064503c1fb9SAvraham Stern 	memcpy(ev->pa.bssid, bssid, ETH_ALEN);
1065503c1fb9SAvraham Stern 
1066503c1fb9SAvraham Stern 	/*
1067503c1fb9SAvraham Stern 	 * Use the wdev event list so that if there are pending
1068503c1fb9SAvraham Stern 	 * connected/roamed events, they will be reported first.
1069503c1fb9SAvraham Stern 	 */
1070503c1fb9SAvraham Stern 	spin_lock_irqsave(&wdev->event_lock, flags);
1071503c1fb9SAvraham Stern 	list_add_tail(&ev->list, &wdev->event_list);
1072503c1fb9SAvraham Stern 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1073503c1fb9SAvraham Stern 	queue_work(cfg80211_wq, &rdev->event_work);
1074503c1fb9SAvraham Stern }
1075503c1fb9SAvraham Stern EXPORT_SYMBOL(cfg80211_port_authorized);
1076503c1fb9SAvraham Stern 
1077667503ddSJohannes Berg void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
10786829c878SJohannes Berg 			     size_t ie_len, u16 reason, bool from_ap)
1079b23aa676SSamuel Ortiz {
1080b23aa676SSamuel Ortiz 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1081f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1082fffd0934SJohannes Berg 	int i;
10833d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
1084b23aa676SSamuel Ortiz 	union iwreq_data wrqu;
1085b23aa676SSamuel Ortiz #endif
1086b23aa676SSamuel Ortiz 
1087667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
1088667503ddSJohannes Berg 
1089074ac8dfSJohannes Berg 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1090074ac8dfSJohannes Berg 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
1091b23aa676SSamuel Ortiz 		return;
1092b23aa676SSamuel Ortiz 
1093b23aa676SSamuel Ortiz 	if (wdev->current_bss) {
1094b23aa676SSamuel Ortiz 		cfg80211_unhold_bss(wdev->current_bss);
10955b112d3dSJohannes Berg 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
1096b23aa676SSamuel Ortiz 	}
1097b23aa676SSamuel Ortiz 
1098b23aa676SSamuel Ortiz 	wdev->current_bss = NULL;
10998dadadb7SJohannes Berg 	wdev->ssid_len = 0;
1100bd2522b1SAndrzej Zaborowski 	wdev->conn_owner_nlportid = 0;
11013027a8e7SAvraham Stern 	kzfree(wdev->connect_keys);
11023027a8e7SAvraham Stern 	wdev->connect_keys = NULL;
1103b23aa676SSamuel Ortiz 
1104fffd0934SJohannes Berg 	nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
1105fffd0934SJohannes Berg 
1106b8607152SArend van Spriel 	/* stop critical protocol if supported */
1107b8607152SArend van Spriel 	if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
1108b8607152SArend van Spriel 		rdev->crit_proto_nlportid = 0;
1109b8607152SArend van Spriel 		rdev_crit_proto_stop(rdev, wdev);
1110b8607152SArend van Spriel 	}
1111b8607152SArend van Spriel 
1112fffd0934SJohannes Berg 	/*
1113fffd0934SJohannes Berg 	 * Delete all the keys ... pairwise keys can't really
1114fffd0934SJohannes Berg 	 * exist any more anyway, but default keys might.
1115fffd0934SJohannes Berg 	 */
111656be393fSJouni Malinen 	if (rdev->ops->del_key) {
111756be393fSJouni Malinen 		int max_key_idx = 5;
111856be393fSJouni Malinen 
111956be393fSJouni Malinen 		if (wiphy_ext_feature_isset(
112056be393fSJouni Malinen 			    wdev->wiphy,
112156be393fSJouni Malinen 			    NL80211_EXT_FEATURE_BEACON_PROTECTION))
112256be393fSJouni Malinen 			max_key_idx = 7;
112356be393fSJouni Malinen 		for (i = 0; i <= max_key_idx; i++)
1124e35e4d28SHila Gonen 			rdev_del_key(rdev, dev, i, false, NULL);
112556be393fSJouni Malinen 	}
1126b23aa676SSamuel Ortiz 
1127fa9ffc74SKyeyoon Park 	rdev_set_qos_map(rdev, dev, NULL);
1128fa9ffc74SKyeyoon Park 
11293d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
1130b23aa676SSamuel Ortiz 	memset(&wrqu, 0, sizeof(wrqu));
1131b23aa676SSamuel Ortiz 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1132b23aa676SSamuel Ortiz 	wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
11335f612033SAbhijeet Kolekar 	wdev->wext.connect.ssid_len = 0;
1134b23aa676SSamuel Ortiz #endif
113509d989d1SLuis R. Rodriguez 
113609d989d1SLuis R. Rodriguez 	schedule_work(&cfg80211_disconnect_work);
1137b23aa676SSamuel Ortiz }
1138b23aa676SSamuel Ortiz 
1139b23aa676SSamuel Ortiz void cfg80211_disconnected(struct net_device *dev, u16 reason,
114080279fb7SJohannes Berg 			   const u8 *ie, size_t ie_len,
114180279fb7SJohannes Berg 			   bool locally_generated, gfp_t gfp)
1142b23aa676SSamuel Ortiz {
1143667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1144f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1145667503ddSJohannes Berg 	struct cfg80211_event *ev;
1146667503ddSJohannes Berg 	unsigned long flags;
1147667503ddSJohannes Berg 
1148667503ddSJohannes Berg 	ev = kzalloc(sizeof(*ev) + ie_len, gfp);
1149667503ddSJohannes Berg 	if (!ev)
1150667503ddSJohannes Berg 		return;
1151667503ddSJohannes Berg 
1152667503ddSJohannes Berg 	ev->type = EVENT_DISCONNECTED;
1153667503ddSJohannes Berg 	ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
1154667503ddSJohannes Berg 	ev->dc.ie_len = ie_len;
1155667503ddSJohannes Berg 	memcpy((void *)ev->dc.ie, ie, ie_len);
1156667503ddSJohannes Berg 	ev->dc.reason = reason;
115780279fb7SJohannes Berg 	ev->dc.locally_generated = locally_generated;
1158667503ddSJohannes Berg 
1159667503ddSJohannes Berg 	spin_lock_irqsave(&wdev->event_lock, flags);
1160667503ddSJohannes Berg 	list_add_tail(&ev->list, &wdev->event_list);
1161667503ddSJohannes Berg 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1162e60d7443SAlban Browaeys 	queue_work(cfg80211_wq, &rdev->event_work);
1163b23aa676SSamuel Ortiz }
1164b23aa676SSamuel Ortiz EXPORT_SYMBOL(cfg80211_disconnected);
1165b23aa676SSamuel Ortiz 
1166ceca7b71SJohannes Berg /*
1167ceca7b71SJohannes Berg  * API calls for nl80211/wext compatibility code
1168ceca7b71SJohannes Berg  */
116983739b03SJohannes Berg int cfg80211_connect(struct cfg80211_registered_device *rdev,
1170b23aa676SSamuel Ortiz 		     struct net_device *dev,
1171fffd0934SJohannes Berg 		     struct cfg80211_connect_params *connect,
1172f401a6f7SJohannes Berg 		     struct cfg80211_cached_keys *connkeys,
1173f401a6f7SJohannes Berg 		     const u8 *prev_bssid)
1174b23aa676SSamuel Ortiz {
1175b23aa676SSamuel Ortiz 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1176667503ddSJohannes Berg 	int err;
1177667503ddSJohannes Berg 
1178667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
1179b23aa676SSamuel Ortiz 
118051e13359SJohannes Berg 	/*
118151e13359SJohannes Berg 	 * If we have an ssid_len, we're trying to connect or are
118251e13359SJohannes Berg 	 * already connected, so reject a new SSID unless it's the
118351e13359SJohannes Berg 	 * same (which is the case for re-association.)
118451e13359SJohannes Berg 	 */
118551e13359SJohannes Berg 	if (wdev->ssid_len &&
118651e13359SJohannes Berg 	    (wdev->ssid_len != connect->ssid_len ||
118751e13359SJohannes Berg 	     memcmp(wdev->ssid, connect->ssid, wdev->ssid_len)))
118851e13359SJohannes Berg 		return -EALREADY;
118951e13359SJohannes Berg 
119051e13359SJohannes Berg 	/*
119151e13359SJohannes Berg 	 * If connected, reject (re-)association unless prev_bssid
119251e13359SJohannes Berg 	 * matches the current BSSID.
119351e13359SJohannes Berg 	 */
119451e13359SJohannes Berg 	if (wdev->current_bss) {
119551e13359SJohannes Berg 		if (!prev_bssid)
119651e13359SJohannes Berg 			return -EALREADY;
119751e13359SJohannes Berg 		if (!ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
119851e13359SJohannes Berg 			return -ENOTCONN;
1199fffd0934SJohannes Berg 	}
1200fffd0934SJohannes Berg 
120151e13359SJohannes Berg 	/*
120251e13359SJohannes Berg 	 * Reject if we're in the process of connecting with WEP,
120351e13359SJohannes Berg 	 * this case isn't very interesting and trying to handle
120451e13359SJohannes Berg 	 * it would make the code much more complex.
120551e13359SJohannes Berg 	 */
120651e13359SJohannes Berg 	if (wdev->connect_keys)
120751e13359SJohannes Berg 		return -EINPROGRESS;
120851e13359SJohannes Berg 
12097e7c8926SBen Greear 	cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
12107e7c8926SBen Greear 				  rdev->wiphy.ht_capa_mod_mask);
121181c5dce2SSergey Matyukevich 	cfg80211_oper_and_vht_capa(&connect->vht_capa_mask,
121281c5dce2SSergey Matyukevich 				   rdev->wiphy.vht_capa_mod_mask);
12137e7c8926SBen Greear 
1214fffd0934SJohannes Berg 	if (connkeys && connkeys->def >= 0) {
1215fffd0934SJohannes Berg 		int idx;
1216bcba8eaeSSamuel Ortiz 		u32 cipher;
1217fffd0934SJohannes Berg 
1218fffd0934SJohannes Berg 		idx = connkeys->def;
1219bcba8eaeSSamuel Ortiz 		cipher = connkeys->params[idx].cipher;
1220fffd0934SJohannes Berg 		/* If given a WEP key we may need it for shared key auth */
1221bcba8eaeSSamuel Ortiz 		if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1222bcba8eaeSSamuel Ortiz 		    cipher == WLAN_CIPHER_SUITE_WEP104) {
1223fffd0934SJohannes Berg 			connect->key_idx = idx;
1224fffd0934SJohannes Berg 			connect->key = connkeys->params[idx].key;
1225fffd0934SJohannes Berg 			connect->key_len = connkeys->params[idx].key_len;
1226bcba8eaeSSamuel Ortiz 
1227bcba8eaeSSamuel Ortiz 			/*
1228bcba8eaeSSamuel Ortiz 			 * If ciphers are not set (e.g. when going through
1229bcba8eaeSSamuel Ortiz 			 * iwconfig), we have to set them appropriately here.
1230bcba8eaeSSamuel Ortiz 			 */
1231bcba8eaeSSamuel Ortiz 			if (connect->crypto.cipher_group == 0)
1232bcba8eaeSSamuel Ortiz 				connect->crypto.cipher_group = cipher;
1233bcba8eaeSSamuel Ortiz 
1234bcba8eaeSSamuel Ortiz 			if (connect->crypto.n_ciphers_pairwise == 0) {
1235bcba8eaeSSamuel Ortiz 				connect->crypto.n_ciphers_pairwise = 1;
1236bcba8eaeSSamuel Ortiz 				connect->crypto.ciphers_pairwise[0] = cipher;
1237bcba8eaeSSamuel Ortiz 			}
1238fffd0934SJohannes Berg 		}
1239b8676221SDavid Spinadel 
1240b8676221SDavid Spinadel 		connect->crypto.wep_keys = connkeys->params;
1241b8676221SDavid Spinadel 		connect->crypto.wep_tx_key = connkeys->def;
1242f1c1f17aSJohannes Berg 	} else {
1243f1c1f17aSJohannes Berg 		if (WARN_ON(connkeys))
1244f1c1f17aSJohannes Berg 			return -EINVAL;
1245fffd0934SJohannes Berg 	}
1246fffd0934SJohannes Berg 
1247ceca7b71SJohannes Berg 	wdev->connect_keys = connkeys;
12486829c878SJohannes Berg 	memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
12496829c878SJohannes Berg 	wdev->ssid_len = connect->ssid_len;
12506829c878SJohannes Berg 
125134d50519SLior David 	wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
125234d50519SLior David 					      IEEE80211_BSS_TYPE_ESS;
125334d50519SLior David 
1254ceca7b71SJohannes Berg 	if (!rdev->ops->connect)
1255ceca7b71SJohannes Berg 		err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1256ceca7b71SJohannes Berg 	else
1257ceca7b71SJohannes Berg 		err = rdev_connect(rdev, dev, connect);
12586829c878SJohannes Berg 
125919957bb3SJohannes Berg 	if (err) {
1260fffd0934SJohannes Berg 		wdev->connect_keys = NULL;
126151e13359SJohannes Berg 		/*
126251e13359SJohannes Berg 		 * This could be reassoc getting refused, don't clear
126351e13359SJohannes Berg 		 * ssid_len in that case.
126451e13359SJohannes Berg 		 */
126551e13359SJohannes Berg 		if (!wdev->current_bss)
12668dadadb7SJohannes Berg 			wdev->ssid_len = 0;
1267b23aa676SSamuel Ortiz 		return err;
1268b23aa676SSamuel Ortiz 	}
1269b23aa676SSamuel Ortiz 
1270b23aa676SSamuel Ortiz 	return 0;
1271b23aa676SSamuel Ortiz }
1272b23aa676SSamuel Ortiz 
127383739b03SJohannes Berg int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1274f2129354SJohannes Berg 			struct net_device *dev, u16 reason, bool wextev)
1275b23aa676SSamuel Ortiz {
12766829c878SJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1277dee8a973SJohannes Berg 	int err = 0;
1278b23aa676SSamuel Ortiz 
1279667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
1280667503ddSJohannes Berg 
1281b47f610bSJohannes Berg 	kzfree(wdev->connect_keys);
1282fffd0934SJohannes Berg 	wdev->connect_keys = NULL;
1283fffd0934SJohannes Berg 
1284bd2522b1SAndrzej Zaborowski 	wdev->conn_owner_nlportid = 0;
1285bd2522b1SAndrzej Zaborowski 
1286dee8a973SJohannes Berg 	if (wdev->conn)
1287ceca7b71SJohannes Berg 		err = cfg80211_sme_disconnect(wdev, reason);
1288dee8a973SJohannes Berg 	else if (!rdev->ops->disconnect)
128919957bb3SJohannes Berg 		cfg80211_mlme_down(rdev, dev);
12900711d638SIlan Peer 	else if (wdev->ssid_len)
1291e35e4d28SHila Gonen 		err = rdev_disconnect(rdev, dev, reason);
1292ceca7b71SJohannes Berg 
129351e13359SJohannes Berg 	/*
129451e13359SJohannes Berg 	 * Clear ssid_len unless we actually were fully connected,
129551e13359SJohannes Berg 	 * in which case cfg80211_disconnected() will take care of
129651e13359SJohannes Berg 	 * this later.
129751e13359SJohannes Berg 	 */
129851e13359SJohannes Berg 	if (!wdev->current_bss)
129951e13359SJohannes Berg 		wdev->ssid_len = 0;
130051e13359SJohannes Berg 
1301b23aa676SSamuel Ortiz 	return err;
1302b23aa676SSamuel Ortiz }
1303bd2522b1SAndrzej Zaborowski 
1304bd2522b1SAndrzej Zaborowski /*
1305bd2522b1SAndrzej Zaborowski  * Used to clean up after the connection / connection attempt owner socket
1306bd2522b1SAndrzej Zaborowski  * disconnects
1307bd2522b1SAndrzej Zaborowski  */
1308bd2522b1SAndrzej Zaborowski void cfg80211_autodisconnect_wk(struct work_struct *work)
1309bd2522b1SAndrzej Zaborowski {
1310bd2522b1SAndrzej Zaborowski 	struct wireless_dev *wdev =
1311bd2522b1SAndrzej Zaborowski 		container_of(work, struct wireless_dev, disconnect_wk);
1312bd2522b1SAndrzej Zaborowski 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1313bd2522b1SAndrzej Zaborowski 
1314bd2522b1SAndrzej Zaborowski 	wdev_lock(wdev);
1315bd2522b1SAndrzej Zaborowski 
1316bd2522b1SAndrzej Zaborowski 	if (wdev->conn_owner_nlportid) {
131737b1c004SDenis Kenzior 		switch (wdev->iftype) {
131837b1c004SDenis Kenzior 		case NL80211_IFTYPE_ADHOC:
13195a128a08SMarkus Theil 			__cfg80211_leave_ibss(rdev, wdev->netdev, false);
132037b1c004SDenis Kenzior 			break;
132137b1c004SDenis Kenzior 		case NL80211_IFTYPE_AP:
132237b1c004SDenis Kenzior 		case NL80211_IFTYPE_P2P_GO:
13235a128a08SMarkus Theil 			__cfg80211_stop_ap(rdev, wdev->netdev, false);
132437b1c004SDenis Kenzior 			break;
132537b1c004SDenis Kenzior 		case NL80211_IFTYPE_MESH_POINT:
13265a128a08SMarkus Theil 			__cfg80211_leave_mesh(rdev, wdev->netdev);
132737b1c004SDenis Kenzior 			break;
132837b1c004SDenis Kenzior 		case NL80211_IFTYPE_STATION:
132937b1c004SDenis Kenzior 		case NL80211_IFTYPE_P2P_CLIENT:
1330bd2522b1SAndrzej Zaborowski 			/*
133137b1c004SDenis Kenzior 			 * Use disconnect_bssid if still connecting and
133237b1c004SDenis Kenzior 			 * ops->disconnect not implemented.  Otherwise we can
133337b1c004SDenis Kenzior 			 * use cfg80211_disconnect.
1334bd2522b1SAndrzej Zaborowski 			 */
1335bd2522b1SAndrzej Zaborowski 			if (rdev->ops->disconnect || wdev->current_bss)
1336bd2522b1SAndrzej Zaborowski 				cfg80211_disconnect(rdev, wdev->netdev,
133737b1c004SDenis Kenzior 						    WLAN_REASON_DEAUTH_LEAVING,
133837b1c004SDenis Kenzior 						    true);
1339bd2522b1SAndrzej Zaborowski 			else
1340bd2522b1SAndrzej Zaborowski 				cfg80211_mlme_deauth(rdev, wdev->netdev,
134137b1c004SDenis Kenzior 						     wdev->disconnect_bssid,
134237b1c004SDenis Kenzior 						     NULL, 0,
134337b1c004SDenis Kenzior 						     WLAN_REASON_DEAUTH_LEAVING,
134437b1c004SDenis Kenzior 						     false);
134537b1c004SDenis Kenzior 			break;
134637b1c004SDenis Kenzior 		default:
134737b1c004SDenis Kenzior 			break;
134837b1c004SDenis Kenzior 		}
1349bd2522b1SAndrzej Zaborowski 	}
1350bd2522b1SAndrzej Zaborowski 
1351bd2522b1SAndrzej Zaborowski 	wdev_unlock(wdev);
1352bd2522b1SAndrzej Zaborowski }
1353