xref: /openbmc/linux/net/wireless/sme.c (revision 503c1fb9)
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.
829ce6ecbSAvraham Stern  * Copyright 2017	Intel Deutschland GmbH
9b23aa676SSamuel Ortiz  */
10b23aa676SSamuel Ortiz 
11b23aa676SSamuel Ortiz #include <linux/etherdevice.h>
12b23aa676SSamuel Ortiz #include <linux/if_arp.h>
135a0e3ad6STejun Heo #include <linux/slab.h>
14b23aa676SSamuel Ortiz #include <linux/workqueue.h>
15a9a11622SJohannes Berg #include <linux/wireless.h>
16bc3b2d7fSPaul Gortmaker #include <linux/export.h>
17a9a11622SJohannes Berg #include <net/iw_handler.h>
18b23aa676SSamuel Ortiz #include <net/cfg80211.h>
19b23aa676SSamuel Ortiz #include <net/rtnetlink.h>
20b23aa676SSamuel Ortiz #include "nl80211.h"
218b19e6caSLuis R. Rodriguez #include "reg.h"
22e35e4d28SHila Gonen #include "rdev-ops.h"
23b23aa676SSamuel Ortiz 
24ceca7b71SJohannes Berg /*
25ceca7b71SJohannes Berg  * Software SME in cfg80211, using auth/assoc/deauth calls to the
26ceca7b71SJohannes Berg  * driver. This is is for implementing nl80211's connect/disconnect
27ceca7b71SJohannes Berg  * and wireless extensions (if configured.)
28ceca7b71SJohannes Berg  */
29ceca7b71SJohannes Berg 
306829c878SJohannes Berg struct cfg80211_conn {
316829c878SJohannes Berg 	struct cfg80211_connect_params params;
326829c878SJohannes Berg 	/* these are sub-states of the _CONNECTING sme_state */
336829c878SJohannes Berg 	enum {
346829c878SJohannes Berg 		CFG80211_CONN_SCANNING,
356829c878SJohannes Berg 		CFG80211_CONN_SCAN_AGAIN,
366829c878SJohannes Berg 		CFG80211_CONN_AUTHENTICATE_NEXT,
376829c878SJohannes Berg 		CFG80211_CONN_AUTHENTICATING,
383093ebbeSPurushottam Kushwaha 		CFG80211_CONN_AUTH_FAILED_TIMEOUT,
396829c878SJohannes Berg 		CFG80211_CONN_ASSOCIATE_NEXT,
406829c878SJohannes Berg 		CFG80211_CONN_ASSOCIATING,
41923a0e7dSJohannes Berg 		CFG80211_CONN_ASSOC_FAILED,
423093ebbeSPurushottam Kushwaha 		CFG80211_CONN_ASSOC_FAILED_TIMEOUT,
43ceca7b71SJohannes Berg 		CFG80211_CONN_DEAUTH,
44e6f462dfSJohannes Berg 		CFG80211_CONN_ABANDON,
45ceca7b71SJohannes Berg 		CFG80211_CONN_CONNECTED,
466829c878SJohannes Berg 	} state;
47f401a6f7SJohannes Berg 	u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
4846b9d180SJohannes Berg 	const u8 *ie;
496829c878SJohannes Berg 	size_t ie_len;
50f401a6f7SJohannes Berg 	bool auto_auth, prev_bssid_valid;
516829c878SJohannes Berg };
526829c878SJohannes Berg 
53ceca7b71SJohannes Berg static void cfg80211_sme_free(struct wireless_dev *wdev)
5409d989d1SLuis R. Rodriguez {
55ceca7b71SJohannes Berg 	if (!wdev->conn)
56ceca7b71SJohannes Berg 		return;
5709d989d1SLuis R. Rodriguez 
58ceca7b71SJohannes Berg 	kfree(wdev->conn->ie);
59ceca7b71SJohannes Berg 	kfree(wdev->conn);
60ceca7b71SJohannes Berg 	wdev->conn = NULL;
6109d989d1SLuis R. Rodriguez }
626829c878SJohannes Berg 
636829c878SJohannes Berg static int cfg80211_conn_scan(struct wireless_dev *wdev)
646829c878SJohannes Berg {
65f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
666829c878SJohannes Berg 	struct cfg80211_scan_request *request;
676829c878SJohannes Berg 	int n_channels, err;
686829c878SJohannes Berg 
696829c878SJohannes Berg 	ASSERT_RTNL();
70667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
716829c878SJohannes Berg 
72f9d15d16SJohannes Berg 	if (rdev->scan_req || rdev->scan_msg)
736829c878SJohannes Berg 		return -EBUSY;
746829c878SJohannes Berg 
75bdfbec2dSIlan Peer 	if (wdev->conn->params.channel)
766829c878SJohannes Berg 		n_channels = 1;
77bdfbec2dSIlan Peer 	else
78bdfbec2dSIlan Peer 		n_channels = ieee80211_get_num_supported_channels(wdev->wiphy);
796829c878SJohannes Berg 
806829c878SJohannes Berg 	request = kzalloc(sizeof(*request) + sizeof(request->ssids[0]) +
816829c878SJohannes Berg 			  sizeof(request->channels[0]) * n_channels,
826829c878SJohannes Berg 			  GFP_KERNEL);
836829c878SJohannes Berg 	if (!request)
846829c878SJohannes Berg 		return -ENOMEM;
856829c878SJohannes Berg 
862a84ee86SKarl Beldan 	if (wdev->conn->params.channel) {
8757fbcce3SJohannes Berg 		enum nl80211_band band = wdev->conn->params.channel->band;
882a84ee86SKarl Beldan 		struct ieee80211_supported_band *sband =
892a84ee86SKarl Beldan 			wdev->wiphy->bands[band];
902a84ee86SKarl Beldan 
912a84ee86SKarl Beldan 		if (!sband) {
922a84ee86SKarl Beldan 			kfree(request);
932a84ee86SKarl Beldan 			return -EINVAL;
942a84ee86SKarl Beldan 		}
956829c878SJohannes Berg 		request->channels[0] = wdev->conn->params.channel;
962a84ee86SKarl Beldan 		request->rates[band] = (1 << sband->n_bitrates) - 1;
972a84ee86SKarl Beldan 	} else {
986829c878SJohannes Berg 		int i = 0, j;
9957fbcce3SJohannes Berg 		enum nl80211_band band;
100e3081501SRajkumar Manoharan 		struct ieee80211_supported_band *bands;
101e3081501SRajkumar Manoharan 		struct ieee80211_channel *channel;
1026829c878SJohannes Berg 
10357fbcce3SJohannes Berg 		for (band = 0; band < NUM_NL80211_BANDS; band++) {
104e3081501SRajkumar Manoharan 			bands = wdev->wiphy->bands[band];
105e3081501SRajkumar Manoharan 			if (!bands)
1066829c878SJohannes Berg 				continue;
107e3081501SRajkumar Manoharan 			for (j = 0; j < bands->n_channels; j++) {
108e3081501SRajkumar Manoharan 				channel = &bands->channels[j];
109e3081501SRajkumar Manoharan 				if (channel->flags & IEEE80211_CHAN_DISABLED)
110e3081501SRajkumar Manoharan 					continue;
111e3081501SRajkumar Manoharan 				request->channels[i++] = channel;
1126829c878SJohannes Berg 			}
113e3081501SRajkumar Manoharan 			request->rates[band] = (1 << bands->n_bitrates) - 1;
114e3081501SRajkumar Manoharan 		}
115e3081501SRajkumar Manoharan 		n_channels = i;
1166829c878SJohannes Berg 	}
1176829c878SJohannes Berg 	request->n_channels = n_channels;
1185ba63533SJohannes Berg 	request->ssids = (void *)&request->channels[n_channels];
1196829c878SJohannes Berg 	request->n_ssids = 1;
1206829c878SJohannes Berg 
1216829c878SJohannes Berg 	memcpy(request->ssids[0].ssid, wdev->conn->params.ssid,
1226829c878SJohannes Berg 		wdev->conn->params.ssid_len);
1236829c878SJohannes Berg 	request->ssids[0].ssid_len = wdev->conn->params.ssid_len;
1246829c878SJohannes Berg 
125818965d3SJouni Malinen 	eth_broadcast_addr(request->bssid);
126818965d3SJouni Malinen 
127fd014284SJohannes Berg 	request->wdev = wdev;
12879c97e97SJohannes Berg 	request->wiphy = &rdev->wiphy;
12915d6030bSSam Leffler 	request->scan_start = jiffies;
1306829c878SJohannes Berg 
13179c97e97SJohannes Berg 	rdev->scan_req = request;
1326829c878SJohannes Berg 
133e35e4d28SHila Gonen 	err = rdev_scan(rdev, request);
1346829c878SJohannes Berg 	if (!err) {
1356829c878SJohannes Berg 		wdev->conn->state = CFG80211_CONN_SCANNING;
136fd014284SJohannes Berg 		nl80211_send_scan_start(rdev, wdev);
137463d0183SJohannes Berg 		dev_hold(wdev->netdev);
1386829c878SJohannes Berg 	} else {
13979c97e97SJohannes Berg 		rdev->scan_req = NULL;
1406829c878SJohannes Berg 		kfree(request);
1416829c878SJohannes Berg 	}
1426829c878SJohannes Berg 	return err;
1436829c878SJohannes Berg }
1446829c878SJohannes Berg 
1453093ebbeSPurushottam Kushwaha static int cfg80211_conn_do_work(struct wireless_dev *wdev,
1463093ebbeSPurushottam Kushwaha 				 enum nl80211_timeout_reason *treason)
1476829c878SJohannes Berg {
148f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
14919957bb3SJohannes Berg 	struct cfg80211_connect_params *params;
150f62fab73SJohannes Berg 	struct cfg80211_assoc_request req = {};
15119957bb3SJohannes Berg 	int err;
1526829c878SJohannes Berg 
153667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
154667503ddSJohannes Berg 
1556829c878SJohannes Berg 	if (!wdev->conn)
1566829c878SJohannes Berg 		return 0;
1576829c878SJohannes Berg 
15819957bb3SJohannes Berg 	params = &wdev->conn->params;
15919957bb3SJohannes Berg 
1606829c878SJohannes Berg 	switch (wdev->conn->state) {
161ceca7b71SJohannes Berg 	case CFG80211_CONN_SCANNING:
162ceca7b71SJohannes Berg 		/* didn't find it during scan ... */
163ceca7b71SJohannes Berg 		return -ENOENT;
1646829c878SJohannes Berg 	case CFG80211_CONN_SCAN_AGAIN:
1656829c878SJohannes Berg 		return cfg80211_conn_scan(wdev);
1666829c878SJohannes Berg 	case CFG80211_CONN_AUTHENTICATE_NEXT:
1672fd05115SJohannes Berg 		if (WARN_ON(!rdev->ops->auth))
1682fd05115SJohannes Berg 			return -EOPNOTSUPP;
16919957bb3SJohannes Berg 		wdev->conn->state = CFG80211_CONN_AUTHENTICATING;
17091bf9b26SJohannes Berg 		return cfg80211_mlme_auth(rdev, wdev->netdev,
17119957bb3SJohannes Berg 					  params->channel, params->auth_type,
17219957bb3SJohannes Berg 					  params->bssid,
17319957bb3SJohannes Berg 					  params->ssid, params->ssid_len,
174fffd0934SJohannes Berg 					  NULL, 0,
175fffd0934SJohannes Berg 					  params->key, params->key_len,
176e39e5b5eSJouni Malinen 					  params->key_idx, NULL, 0);
1773093ebbeSPurushottam Kushwaha 	case CFG80211_CONN_AUTH_FAILED_TIMEOUT:
1783093ebbeSPurushottam Kushwaha 		*treason = NL80211_TIMEOUT_AUTH;
179923a0e7dSJohannes Berg 		return -ENOTCONN;
1806829c878SJohannes Berg 	case CFG80211_CONN_ASSOCIATE_NEXT:
1812fd05115SJohannes Berg 		if (WARN_ON(!rdev->ops->assoc))
1822fd05115SJohannes Berg 			return -EOPNOTSUPP;
18319957bb3SJohannes Berg 		wdev->conn->state = CFG80211_CONN_ASSOCIATING;
184f401a6f7SJohannes Berg 		if (wdev->conn->prev_bssid_valid)
185f62fab73SJohannes Berg 			req.prev_bssid = wdev->conn->prev_bssid;
186f62fab73SJohannes Berg 		req.ie = params->ie;
187f62fab73SJohannes Berg 		req.ie_len = params->ie_len;
188f62fab73SJohannes Berg 		req.use_mfp = params->mfp != NL80211_MFP_NO;
189f62fab73SJohannes Berg 		req.crypto = params->crypto;
190f62fab73SJohannes Berg 		req.flags = params->flags;
191f62fab73SJohannes Berg 		req.ht_capa = params->ht_capa;
192f62fab73SJohannes Berg 		req.ht_capa_mask = params->ht_capa_mask;
193f62fab73SJohannes Berg 		req.vht_capa = params->vht_capa;
194f62fab73SJohannes Berg 		req.vht_capa_mask = params->vht_capa_mask;
195f62fab73SJohannes Berg 
19691bf9b26SJohannes Berg 		err = cfg80211_mlme_assoc(rdev, wdev->netdev, params->channel,
197f62fab73SJohannes Berg 					  params->bssid, params->ssid,
198f62fab73SJohannes Berg 					  params->ssid_len, &req);
19919957bb3SJohannes Berg 		if (err)
20091bf9b26SJohannes Berg 			cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
201667503ddSJohannes Berg 					     NULL, 0,
202d5cdfacbSJouni Malinen 					     WLAN_REASON_DEAUTH_LEAVING,
203d5cdfacbSJouni Malinen 					     false);
20419957bb3SJohannes Berg 		return err;
2053093ebbeSPurushottam Kushwaha 	case CFG80211_CONN_ASSOC_FAILED_TIMEOUT:
2063093ebbeSPurushottam Kushwaha 		*treason = NL80211_TIMEOUT_ASSOC;
2073093ebbeSPurushottam Kushwaha 		/* fall through */
208923a0e7dSJohannes Berg 	case CFG80211_CONN_ASSOC_FAILED:
209923a0e7dSJohannes Berg 		cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
210923a0e7dSJohannes Berg 				     NULL, 0,
211923a0e7dSJohannes Berg 				     WLAN_REASON_DEAUTH_LEAVING, false);
212923a0e7dSJohannes Berg 		return -ENOTCONN;
213ceca7b71SJohannes Berg 	case CFG80211_CONN_DEAUTH:
21491bf9b26SJohannes Berg 		cfg80211_mlme_deauth(rdev, wdev->netdev, params->bssid,
2157d930bc3SJohannes Berg 				     NULL, 0,
216d5cdfacbSJouni Malinen 				     WLAN_REASON_DEAUTH_LEAVING, false);
217e6f462dfSJohannes Berg 		/* fall through */
218e6f462dfSJohannes Berg 	case CFG80211_CONN_ABANDON:
219923a0e7dSJohannes Berg 		/* free directly, disconnected event already sent */
220923a0e7dSJohannes Berg 		cfg80211_sme_free(wdev);
221ceca7b71SJohannes Berg 		return 0;
2226829c878SJohannes Berg 	default:
2236829c878SJohannes Berg 		return 0;
2246829c878SJohannes Berg 	}
2256829c878SJohannes Berg }
2266829c878SJohannes Berg 
2276829c878SJohannes Berg void cfg80211_conn_work(struct work_struct *work)
2286829c878SJohannes Berg {
22979c97e97SJohannes Berg 	struct cfg80211_registered_device *rdev =
2306829c878SJohannes Berg 		container_of(work, struct cfg80211_registered_device, conn_work);
2316829c878SJohannes Berg 	struct wireless_dev *wdev;
2327400f42eSJohannes Berg 	u8 bssid_buf[ETH_ALEN], *bssid = NULL;
2333093ebbeSPurushottam Kushwaha 	enum nl80211_timeout_reason treason;
2346829c878SJohannes Berg 
2356829c878SJohannes Berg 	rtnl_lock();
2366829c878SJohannes Berg 
23753873f13SJohannes Berg 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
238c8157976SJohannes Berg 		if (!wdev->netdev)
239c8157976SJohannes Berg 			continue;
240c8157976SJohannes Berg 
241667503ddSJohannes Berg 		wdev_lock(wdev);
242667503ddSJohannes Berg 		if (!netif_running(wdev->netdev)) {
243667503ddSJohannes Berg 			wdev_unlock(wdev);
2446829c878SJohannes Berg 			continue;
245667503ddSJohannes Berg 		}
246ceca7b71SJohannes Berg 		if (!wdev->conn ||
247ceca7b71SJohannes Berg 		    wdev->conn->state == CFG80211_CONN_CONNECTED) {
248667503ddSJohannes Berg 			wdev_unlock(wdev);
2496829c878SJohannes Berg 			continue;
250667503ddSJohannes Berg 		}
2517400f42eSJohannes Berg 		if (wdev->conn->params.bssid) {
2527400f42eSJohannes Berg 			memcpy(bssid_buf, wdev->conn->params.bssid, ETH_ALEN);
2537400f42eSJohannes Berg 			bssid = bssid_buf;
2547400f42eSJohannes Berg 		}
2553093ebbeSPurushottam Kushwaha 		treason = NL80211_TIMEOUT_UNSPECIFIED;
2563093ebbeSPurushottam Kushwaha 		if (cfg80211_conn_do_work(wdev, &treason)) {
2575349a0f7SVidyullatha Kanchanapally 			struct cfg80211_connect_resp_params cr;
2585349a0f7SVidyullatha Kanchanapally 
2595349a0f7SVidyullatha Kanchanapally 			memset(&cr, 0, sizeof(cr));
2605349a0f7SVidyullatha Kanchanapally 			cr.status = -1;
2615349a0f7SVidyullatha Kanchanapally 			cr.bssid = bssid;
2625349a0f7SVidyullatha Kanchanapally 			cr.timeout_reason = treason;
2635349a0f7SVidyullatha Kanchanapally 			__cfg80211_connect_result(wdev->netdev, &cr, false);
264ceca7b71SJohannes Berg 		}
265667503ddSJohannes Berg 		wdev_unlock(wdev);
2666829c878SJohannes Berg 	}
2676829c878SJohannes Berg 
2686829c878SJohannes Berg 	rtnl_unlock();
2696829c878SJohannes Berg }
2706829c878SJohannes Berg 
2710e3a39b5SBen Greear /* Returned bss is reference counted and must be cleaned up appropriately. */
272bbac31f4SJohannes Berg static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
2736829c878SJohannes Berg {
274f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
2756829c878SJohannes Berg 	struct cfg80211_bss *bss;
2766829c878SJohannes Berg 
277667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
278667503ddSJohannes Berg 
279ed9d0102SJouni Malinen 	bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
280ed9d0102SJouni Malinen 			       wdev->conn->params.bssid,
2816829c878SJohannes Berg 			       wdev->conn->params.ssid,
2826829c878SJohannes Berg 			       wdev->conn->params.ssid_len,
28334d50519SLior David 			       wdev->conn_bss_type,
2846eb18137SDedy Lansky 			       IEEE80211_PRIVACY(wdev->conn->params.privacy));
2856829c878SJohannes Berg 	if (!bss)
286bbac31f4SJohannes Berg 		return NULL;
2876829c878SJohannes Berg 
2886829c878SJohannes Berg 	memcpy(wdev->conn->bssid, bss->bssid, ETH_ALEN);
2896829c878SJohannes Berg 	wdev->conn->params.bssid = wdev->conn->bssid;
2906829c878SJohannes Berg 	wdev->conn->params.channel = bss->channel;
2916829c878SJohannes Berg 	wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
29279c97e97SJohannes Berg 	schedule_work(&rdev->conn_work);
2936829c878SJohannes Berg 
294bbac31f4SJohannes Berg 	return bss;
2956829c878SJohannes Berg }
2966829c878SJohannes Berg 
297667503ddSJohannes Berg static void __cfg80211_sme_scan_done(struct net_device *dev)
2986829c878SJohannes Berg {
2996829c878SJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
300f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
301bbac31f4SJohannes Berg 	struct cfg80211_bss *bss;
3026829c878SJohannes Berg 
303667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
304667503ddSJohannes Berg 
305d4b1a687SZhu Yi 	if (!wdev->conn)
3066829c878SJohannes Berg 		return;
3076829c878SJohannes Berg 
3086829c878SJohannes Berg 	if (wdev->conn->state != CFG80211_CONN_SCANNING &&
3096829c878SJohannes Berg 	    wdev->conn->state != CFG80211_CONN_SCAN_AGAIN)
3106829c878SJohannes Berg 		return;
3116829c878SJohannes Berg 
312bbac31f4SJohannes Berg 	bss = cfg80211_get_conn_bss(wdev);
313ceca7b71SJohannes Berg 	if (bss)
3145b112d3dSJohannes Berg 		cfg80211_put_bss(&rdev->wiphy, bss);
3156829c878SJohannes Berg 	else
316ceca7b71SJohannes Berg 		schedule_work(&rdev->conn_work);
3176829c878SJohannes Berg }
3186829c878SJohannes Berg 
319667503ddSJohannes Berg void cfg80211_sme_scan_done(struct net_device *dev)
320667503ddSJohannes Berg {
321667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
322667503ddSJohannes Berg 
323667503ddSJohannes Berg 	wdev_lock(wdev);
324667503ddSJohannes Berg 	__cfg80211_sme_scan_done(dev);
325667503ddSJohannes Berg 	wdev_unlock(wdev);
326667503ddSJohannes Berg }
327667503ddSJohannes Berg 
328ceca7b71SJohannes Berg void cfg80211_sme_rx_auth(struct wireless_dev *wdev, const u8 *buf, size_t len)
3296829c878SJohannes Berg {
3306829c878SJohannes Berg 	struct wiphy *wiphy = wdev->wiphy;
331f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
3326829c878SJohannes Berg 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
3336829c878SJohannes Berg 	u16 status_code = le16_to_cpu(mgmt->u.auth.status_code);
3346829c878SJohannes Berg 
335667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
336667503ddSJohannes Berg 
337ceca7b71SJohannes Berg 	if (!wdev->conn || wdev->conn->state == CFG80211_CONN_CONNECTED)
3386829c878SJohannes Berg 		return;
3396829c878SJohannes Berg 
3406829c878SJohannes Berg 	if (status_code == WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG &&
3416829c878SJohannes Berg 	    wdev->conn->auto_auth &&
3426829c878SJohannes Berg 	    wdev->conn->params.auth_type != NL80211_AUTHTYPE_NETWORK_EAP) {
3436829c878SJohannes Berg 		/* select automatically between only open, shared, leap */
3446829c878SJohannes Berg 		switch (wdev->conn->params.auth_type) {
3456829c878SJohannes Berg 		case NL80211_AUTHTYPE_OPEN_SYSTEM:
346fffd0934SJohannes Berg 			if (wdev->connect_keys)
3476829c878SJohannes Berg 				wdev->conn->params.auth_type =
3486829c878SJohannes Berg 					NL80211_AUTHTYPE_SHARED_KEY;
349fffd0934SJohannes Berg 			else
350fffd0934SJohannes Berg 				wdev->conn->params.auth_type =
351fffd0934SJohannes Berg 					NL80211_AUTHTYPE_NETWORK_EAP;
3526829c878SJohannes Berg 			break;
3536829c878SJohannes Berg 		case NL80211_AUTHTYPE_SHARED_KEY:
3546829c878SJohannes Berg 			wdev->conn->params.auth_type =
3556829c878SJohannes Berg 				NL80211_AUTHTYPE_NETWORK_EAP;
3566829c878SJohannes Berg 			break;
3576829c878SJohannes Berg 		default:
3586829c878SJohannes Berg 			/* huh? */
3596829c878SJohannes Berg 			wdev->conn->params.auth_type =
3606829c878SJohannes Berg 				NL80211_AUTHTYPE_OPEN_SYSTEM;
3616829c878SJohannes Berg 			break;
3626829c878SJohannes Berg 		}
3636829c878SJohannes Berg 		wdev->conn->state = CFG80211_CONN_AUTHENTICATE_NEXT;
3646829c878SJohannes Berg 		schedule_work(&rdev->conn_work);
36519957bb3SJohannes Berg 	} else if (status_code != WLAN_STATUS_SUCCESS) {
3665349a0f7SVidyullatha Kanchanapally 		struct cfg80211_connect_resp_params cr;
3675349a0f7SVidyullatha Kanchanapally 
3685349a0f7SVidyullatha Kanchanapally 		memset(&cr, 0, sizeof(cr));
3695349a0f7SVidyullatha Kanchanapally 		cr.status = status_code;
3705349a0f7SVidyullatha Kanchanapally 		cr.bssid = mgmt->bssid;
3715349a0f7SVidyullatha Kanchanapally 		cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED;
3725349a0f7SVidyullatha Kanchanapally 		__cfg80211_connect_result(wdev->netdev, &cr, false);
373ceca7b71SJohannes Berg 	} else if (wdev->conn->state == CFG80211_CONN_AUTHENTICATING) {
3746829c878SJohannes Berg 		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
3756829c878SJohannes Berg 		schedule_work(&rdev->conn_work);
3766829c878SJohannes Berg 	}
3776829c878SJohannes Berg }
378b23aa676SSamuel Ortiz 
379ceca7b71SJohannes Berg bool cfg80211_sme_rx_assoc_resp(struct wireless_dev *wdev, u16 status)
380f401a6f7SJohannes Berg {
381f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
382f401a6f7SJohannes Berg 
383ceca7b71SJohannes Berg 	if (!wdev->conn)
384f401a6f7SJohannes Berg 		return false;
385f401a6f7SJohannes Berg 
386ceca7b71SJohannes Berg 	if (status == WLAN_STATUS_SUCCESS) {
387ceca7b71SJohannes Berg 		wdev->conn->state = CFG80211_CONN_CONNECTED;
388f401a6f7SJohannes Berg 		return false;
389ceca7b71SJohannes Berg 	}
390f401a6f7SJohannes Berg 
391ceca7b71SJohannes Berg 	if (wdev->conn->prev_bssid_valid) {
392f401a6f7SJohannes Berg 		/*
393f401a6f7SJohannes Berg 		 * Some stupid APs don't accept reassoc, so we
394ceca7b71SJohannes Berg 		 * need to fall back to trying regular assoc;
395ceca7b71SJohannes Berg 		 * return true so no event is sent to userspace.
396f401a6f7SJohannes Berg 		 */
397f401a6f7SJohannes Berg 		wdev->conn->prev_bssid_valid = false;
398f401a6f7SJohannes Berg 		wdev->conn->state = CFG80211_CONN_ASSOCIATE_NEXT;
399f401a6f7SJohannes Berg 		schedule_work(&rdev->conn_work);
400f401a6f7SJohannes Berg 		return true;
401f401a6f7SJohannes Berg 	}
402f401a6f7SJohannes Berg 
403923a0e7dSJohannes Berg 	wdev->conn->state = CFG80211_CONN_ASSOC_FAILED;
404ceca7b71SJohannes Berg 	schedule_work(&rdev->conn_work);
405ceca7b71SJohannes Berg 	return false;
406ceca7b71SJohannes Berg }
4077d930bc3SJohannes Berg 
408ceca7b71SJohannes Berg void cfg80211_sme_deauth(struct wireless_dev *wdev)
409ceca7b71SJohannes Berg {
410ceca7b71SJohannes Berg 	cfg80211_sme_free(wdev);
411ceca7b71SJohannes Berg }
412ceca7b71SJohannes Berg 
413ceca7b71SJohannes Berg void cfg80211_sme_auth_timeout(struct wireless_dev *wdev)
414ceca7b71SJohannes Berg {
415f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
416923a0e7dSJohannes Berg 
417923a0e7dSJohannes Berg 	if (!wdev->conn)
418923a0e7dSJohannes Berg 		return;
419923a0e7dSJohannes Berg 
4203093ebbeSPurushottam Kushwaha 	wdev->conn->state = CFG80211_CONN_AUTH_FAILED_TIMEOUT;
421923a0e7dSJohannes Berg 	schedule_work(&rdev->conn_work);
422ceca7b71SJohannes Berg }
423ceca7b71SJohannes Berg 
424ceca7b71SJohannes Berg void cfg80211_sme_disassoc(struct wireless_dev *wdev)
425ceca7b71SJohannes Berg {
426f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
427ceca7b71SJohannes Berg 
428ceca7b71SJohannes Berg 	if (!wdev->conn)
429ceca7b71SJohannes Berg 		return;
430ceca7b71SJohannes Berg 
431ceca7b71SJohannes Berg 	wdev->conn->state = CFG80211_CONN_DEAUTH;
4327d930bc3SJohannes Berg 	schedule_work(&rdev->conn_work);
4337d930bc3SJohannes Berg }
4347d930bc3SJohannes Berg 
435ceca7b71SJohannes Berg void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
436ceca7b71SJohannes Berg {
437f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
438923a0e7dSJohannes Berg 
439923a0e7dSJohannes Berg 	if (!wdev->conn)
440923a0e7dSJohannes Berg 		return;
441923a0e7dSJohannes Berg 
4423093ebbeSPurushottam Kushwaha 	wdev->conn->state = CFG80211_CONN_ASSOC_FAILED_TIMEOUT;
443923a0e7dSJohannes Berg 	schedule_work(&rdev->conn_work);
444ceca7b71SJohannes Berg }
445ceca7b71SJohannes Berg 
446e6f462dfSJohannes Berg void cfg80211_sme_abandon_assoc(struct wireless_dev *wdev)
447e6f462dfSJohannes Berg {
448e6f462dfSJohannes Berg 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
449e6f462dfSJohannes Berg 
450e6f462dfSJohannes Berg 	if (!wdev->conn)
451e6f462dfSJohannes Berg 		return;
452e6f462dfSJohannes Berg 
453e6f462dfSJohannes Berg 	wdev->conn->state = CFG80211_CONN_ABANDON;
454e6f462dfSJohannes Berg 	schedule_work(&rdev->conn_work);
455e6f462dfSJohannes Berg }
456e6f462dfSJohannes Berg 
45746b9d180SJohannes Berg static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
45846b9d180SJohannes Berg 				     const u8 *ies, size_t ies_len,
45946b9d180SJohannes Berg 				     const u8 **out_ies, size_t *out_ies_len)
46046b9d180SJohannes Berg {
46146b9d180SJohannes Berg 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
46246b9d180SJohannes Berg 	u8 *buf;
46346b9d180SJohannes Berg 	size_t offs;
46446b9d180SJohannes Berg 
46546b9d180SJohannes Berg 	if (!rdev->wiphy.extended_capabilities_len ||
46646b9d180SJohannes Berg 	    (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
46746b9d180SJohannes Berg 		*out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
46846b9d180SJohannes Berg 		if (!*out_ies)
46946b9d180SJohannes Berg 			return -ENOMEM;
47046b9d180SJohannes Berg 		*out_ies_len = ies_len;
47146b9d180SJohannes Berg 		return 0;
47246b9d180SJohannes Berg 	}
47346b9d180SJohannes Berg 
47446b9d180SJohannes Berg 	buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
47546b9d180SJohannes Berg 		      GFP_KERNEL);
47646b9d180SJohannes Berg 	if (!buf)
47746b9d180SJohannes Berg 		return -ENOMEM;
47846b9d180SJohannes Berg 
47946b9d180SJohannes Berg 	if (ies_len) {
48046b9d180SJohannes Berg 		static const u8 before_extcapa[] = {
48146b9d180SJohannes Berg 			/* not listing IEs expected to be created by driver */
48246b9d180SJohannes Berg 			WLAN_EID_RSN,
48346b9d180SJohannes Berg 			WLAN_EID_QOS_CAPA,
48446b9d180SJohannes Berg 			WLAN_EID_RRM_ENABLED_CAPABILITIES,
48546b9d180SJohannes Berg 			WLAN_EID_MOBILITY_DOMAIN,
48646b9d180SJohannes Berg 			WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
48746b9d180SJohannes Berg 			WLAN_EID_BSS_COEX_2040,
48846b9d180SJohannes Berg 		};
48946b9d180SJohannes Berg 
49046b9d180SJohannes Berg 		offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
49146b9d180SJohannes Berg 					  ARRAY_SIZE(before_extcapa), 0);
49246b9d180SJohannes Berg 		memcpy(buf, ies, offs);
49346b9d180SJohannes Berg 		/* leave a whole for extended capabilities IE */
49446b9d180SJohannes Berg 		memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
49546b9d180SJohannes Berg 		       ies + offs, ies_len - offs);
49646b9d180SJohannes Berg 	} else {
49746b9d180SJohannes Berg 		offs = 0;
49846b9d180SJohannes Berg 	}
49946b9d180SJohannes Berg 
50046b9d180SJohannes Berg 	/* place extended capabilities IE (with only driver capabilities) */
50146b9d180SJohannes Berg 	buf[offs] = WLAN_EID_EXT_CAPABILITY;
50246b9d180SJohannes Berg 	buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
50346b9d180SJohannes Berg 	memcpy(buf + offs + 2,
50446b9d180SJohannes Berg 	       rdev->wiphy.extended_capabilities,
50546b9d180SJohannes Berg 	       rdev->wiphy.extended_capabilities_len);
50646b9d180SJohannes Berg 
50746b9d180SJohannes Berg 	*out_ies = buf;
50846b9d180SJohannes Berg 	*out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
50946b9d180SJohannes Berg 
51046b9d180SJohannes Berg 	return 0;
51146b9d180SJohannes Berg }
51246b9d180SJohannes Berg 
513ceca7b71SJohannes Berg static int cfg80211_sme_connect(struct wireless_dev *wdev,
514ceca7b71SJohannes Berg 				struct cfg80211_connect_params *connect,
515ceca7b71SJohannes Berg 				const u8 *prev_bssid)
516ceca7b71SJohannes Berg {
517f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
518ceca7b71SJohannes Berg 	struct cfg80211_bss *bss;
519ceca7b71SJohannes Berg 	int err;
520ceca7b71SJohannes Berg 
521ceca7b71SJohannes Berg 	if (!rdev->ops->auth || !rdev->ops->assoc)
522ceca7b71SJohannes Berg 		return -EOPNOTSUPP;
523ceca7b71SJohannes Berg 
5244ce2bd9cSJouni Malinen 	if (wdev->current_bss) {
5254ce2bd9cSJouni Malinen 		if (!prev_bssid)
526ceca7b71SJohannes Berg 			return -EALREADY;
5274ce2bd9cSJouni Malinen 		if (prev_bssid &&
5284ce2bd9cSJouni Malinen 		    !ether_addr_equal(prev_bssid, wdev->current_bss->pub.bssid))
5294ce2bd9cSJouni Malinen 			return -ENOTCONN;
5304ce2bd9cSJouni Malinen 		cfg80211_unhold_bss(wdev->current_bss);
5314ce2bd9cSJouni Malinen 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
5324ce2bd9cSJouni Malinen 		wdev->current_bss = NULL;
5334ce2bd9cSJouni Malinen 
5344ce2bd9cSJouni Malinen 		cfg80211_sme_free(wdev);
5354ce2bd9cSJouni Malinen 	}
536ceca7b71SJohannes Berg 
537ceca7b71SJohannes Berg 	if (WARN_ON(wdev->conn))
538ceca7b71SJohannes Berg 		return -EINPROGRESS;
539ceca7b71SJohannes Berg 
540ceca7b71SJohannes Berg 	wdev->conn = kzalloc(sizeof(*wdev->conn), GFP_KERNEL);
541ceca7b71SJohannes Berg 	if (!wdev->conn)
542ceca7b71SJohannes Berg 		return -ENOMEM;
543ceca7b71SJohannes Berg 
544ceca7b71SJohannes Berg 	/*
545ceca7b71SJohannes Berg 	 * Copy all parameters, and treat explicitly IEs, BSSID, SSID.
546ceca7b71SJohannes Berg 	 */
547ceca7b71SJohannes Berg 	memcpy(&wdev->conn->params, connect, sizeof(*connect));
548ceca7b71SJohannes Berg 	if (connect->bssid) {
549ceca7b71SJohannes Berg 		wdev->conn->params.bssid = wdev->conn->bssid;
550ceca7b71SJohannes Berg 		memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
551ceca7b71SJohannes Berg 	}
552ceca7b71SJohannes Berg 
55346b9d180SJohannes Berg 	if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
55446b9d180SJohannes Berg 				      &wdev->conn->ie,
55546b9d180SJohannes Berg 				      &wdev->conn->params.ie_len)) {
556ceca7b71SJohannes Berg 		kfree(wdev->conn);
557ceca7b71SJohannes Berg 		wdev->conn = NULL;
558ceca7b71SJohannes Berg 		return -ENOMEM;
559ceca7b71SJohannes Berg 	}
56046b9d180SJohannes Berg 	wdev->conn->params.ie = wdev->conn->ie;
561ceca7b71SJohannes Berg 
562ceca7b71SJohannes Berg 	if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
563ceca7b71SJohannes Berg 		wdev->conn->auto_auth = true;
564ceca7b71SJohannes Berg 		/* start with open system ... should mostly work */
565ceca7b71SJohannes Berg 		wdev->conn->params.auth_type =
566ceca7b71SJohannes Berg 			NL80211_AUTHTYPE_OPEN_SYSTEM;
567ceca7b71SJohannes Berg 	} else {
568ceca7b71SJohannes Berg 		wdev->conn->auto_auth = false;
569ceca7b71SJohannes Berg 	}
570ceca7b71SJohannes Berg 
571ceca7b71SJohannes Berg 	wdev->conn->params.ssid = wdev->ssid;
572babd3a27SZhao, Gang 	wdev->conn->params.ssid_len = wdev->ssid_len;
573ceca7b71SJohannes Berg 
574ceca7b71SJohannes Berg 	/* see if we have the bss already */
575ceca7b71SJohannes Berg 	bss = cfg80211_get_conn_bss(wdev);
576ceca7b71SJohannes Berg 
577ceca7b71SJohannes Berg 	if (prev_bssid) {
578ceca7b71SJohannes Berg 		memcpy(wdev->conn->prev_bssid, prev_bssid, ETH_ALEN);
579ceca7b71SJohannes Berg 		wdev->conn->prev_bssid_valid = true;
580ceca7b71SJohannes Berg 	}
581ceca7b71SJohannes Berg 
582ceca7b71SJohannes Berg 	/* we're good if we have a matching bss struct */
583ceca7b71SJohannes Berg 	if (bss) {
5843093ebbeSPurushottam Kushwaha 		enum nl80211_timeout_reason treason;
5853093ebbeSPurushottam Kushwaha 
5863093ebbeSPurushottam Kushwaha 		err = cfg80211_conn_do_work(wdev, &treason);
587ceca7b71SJohannes Berg 		cfg80211_put_bss(wdev->wiphy, bss);
588ceca7b71SJohannes Berg 	} else {
589ceca7b71SJohannes Berg 		/* otherwise we'll need to scan for the AP first */
590ceca7b71SJohannes Berg 		err = cfg80211_conn_scan(wdev);
591ceca7b71SJohannes Berg 
592ceca7b71SJohannes Berg 		/*
593ceca7b71SJohannes Berg 		 * If we can't scan right now, then we need to scan again
594ceca7b71SJohannes Berg 		 * after the current scan finished, since the parameters
595ceca7b71SJohannes Berg 		 * changed (unless we find a good AP anyway).
596ceca7b71SJohannes Berg 		 */
597ceca7b71SJohannes Berg 		if (err == -EBUSY) {
598ceca7b71SJohannes Berg 			err = 0;
599ceca7b71SJohannes Berg 			wdev->conn->state = CFG80211_CONN_SCAN_AGAIN;
600ceca7b71SJohannes Berg 		}
601ceca7b71SJohannes Berg 	}
602ceca7b71SJohannes Berg 
603ceca7b71SJohannes Berg 	if (err)
604ceca7b71SJohannes Berg 		cfg80211_sme_free(wdev);
605ceca7b71SJohannes Berg 
606ceca7b71SJohannes Berg 	return err;
607ceca7b71SJohannes Berg }
608ceca7b71SJohannes Berg 
609ceca7b71SJohannes Berg static int cfg80211_sme_disconnect(struct wireless_dev *wdev, u16 reason)
610ceca7b71SJohannes Berg {
611f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
612ceca7b71SJohannes Berg 	int err;
613ceca7b71SJohannes Berg 
614ceca7b71SJohannes Berg 	if (!wdev->conn)
615ceca7b71SJohannes Berg 		return 0;
616ceca7b71SJohannes Berg 
617ceca7b71SJohannes Berg 	if (!rdev->ops->deauth)
618ceca7b71SJohannes Berg 		return -EOPNOTSUPP;
619ceca7b71SJohannes Berg 
620ceca7b71SJohannes Berg 	if (wdev->conn->state == CFG80211_CONN_SCANNING ||
621ceca7b71SJohannes Berg 	    wdev->conn->state == CFG80211_CONN_SCAN_AGAIN) {
622ceca7b71SJohannes Berg 		err = 0;
623ceca7b71SJohannes Berg 		goto out;
624ceca7b71SJohannes Berg 	}
625ceca7b71SJohannes Berg 
626ceca7b71SJohannes Berg 	/* wdev->conn->params.bssid must be set if > SCANNING */
627ceca7b71SJohannes Berg 	err = cfg80211_mlme_deauth(rdev, wdev->netdev,
628ceca7b71SJohannes Berg 				   wdev->conn->params.bssid,
629ceca7b71SJohannes Berg 				   NULL, 0, reason, false);
630ceca7b71SJohannes Berg  out:
631ceca7b71SJohannes Berg 	cfg80211_sme_free(wdev);
632ceca7b71SJohannes Berg 	return err;
633ceca7b71SJohannes Berg }
634ceca7b71SJohannes Berg 
635ceca7b71SJohannes Berg /*
636ceca7b71SJohannes Berg  * code shared for in-device and software SME
637ceca7b71SJohannes Berg  */
638ceca7b71SJohannes Berg 
639ceca7b71SJohannes Berg static bool cfg80211_is_all_idle(void)
640ceca7b71SJohannes Berg {
641ceca7b71SJohannes Berg 	struct cfg80211_registered_device *rdev;
642ceca7b71SJohannes Berg 	struct wireless_dev *wdev;
643ceca7b71SJohannes Berg 	bool is_all_idle = true;
644ceca7b71SJohannes Berg 
645ceca7b71SJohannes Berg 	/*
646ceca7b71SJohannes Berg 	 * All devices must be idle as otherwise if you are actively
647ceca7b71SJohannes Berg 	 * scanning some new beacon hints could be learned and would
648ceca7b71SJohannes Berg 	 * count as new regulatory hints.
649ceca7b71SJohannes Berg 	 */
650ceca7b71SJohannes Berg 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
65153873f13SJohannes Berg 		list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
652ceca7b71SJohannes Berg 			wdev_lock(wdev);
653ceca7b71SJohannes Berg 			if (wdev->conn || wdev->current_bss)
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 
670ceca7b71SJohannes Berg static 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 
6975349a0f7SVidyullatha Kanchanapally 	nl80211_send_connect_result(wiphy_to_rdev(wdev->wiphy), dev, cr,
6985349a0f7SVidyullatha Kanchanapally 				    GFP_KERNEL);
699e45cd82aSJohannes Berg 
7003d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
701e45cd82aSJohannes Berg 	if (wextev) {
7025349a0f7SVidyullatha Kanchanapally 		if (cr->req_ie && cr->status == WLAN_STATUS_SUCCESS) {
703e45cd82aSJohannes Berg 			memset(&wrqu, 0, sizeof(wrqu));
7045349a0f7SVidyullatha Kanchanapally 			wrqu.data.length = cr->req_ie_len;
7055349a0f7SVidyullatha Kanchanapally 			wireless_send_event(dev, IWEVASSOCREQIE, &wrqu,
7065349a0f7SVidyullatha Kanchanapally 					    cr->req_ie);
707e45cd82aSJohannes Berg 		}
708e45cd82aSJohannes Berg 
7095349a0f7SVidyullatha Kanchanapally 		if (cr->resp_ie && cr->status == WLAN_STATUS_SUCCESS) {
710e45cd82aSJohannes Berg 			memset(&wrqu, 0, sizeof(wrqu));
7115349a0f7SVidyullatha Kanchanapally 			wrqu.data.length = cr->resp_ie_len;
7125349a0f7SVidyullatha Kanchanapally 			wireless_send_event(dev, IWEVASSOCRESPIE, &wrqu,
7135349a0f7SVidyullatha Kanchanapally 					    cr->resp_ie);
714e45cd82aSJohannes Berg 		}
715e45cd82aSJohannes Berg 
716e45cd82aSJohannes Berg 		memset(&wrqu, 0, sizeof(wrqu));
717e45cd82aSJohannes Berg 		wrqu.ap_addr.sa_family = ARPHRD_ETHER;
7185349a0f7SVidyullatha Kanchanapally 		if (cr->bssid && cr->status == WLAN_STATUS_SUCCESS) {
7195349a0f7SVidyullatha Kanchanapally 			memcpy(wrqu.ap_addr.sa_data, cr->bssid, ETH_ALEN);
7205349a0f7SVidyullatha Kanchanapally 			memcpy(wdev->wext.prev_bssid, cr->bssid, ETH_ALEN);
721f401a6f7SJohannes Berg 			wdev->wext.prev_bssid_valid = true;
722f401a6f7SJohannes Berg 		}
723e45cd82aSJohannes Berg 		wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
724e45cd82aSJohannes Berg 	}
725e45cd82aSJohannes Berg #endif
726e45cd82aSJohannes Berg 
7275349a0f7SVidyullatha Kanchanapally 	if (!cr->bss && (cr->status == WLAN_STATUS_SUCCESS)) {
728f26cbf40SZhao, Gang 		WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
7295349a0f7SVidyullatha Kanchanapally 		cr->bss = cfg80211_get_bss(wdev->wiphy, NULL, cr->bssid,
7304c4d684aSUjjal Roy 					   wdev->ssid, wdev->ssid_len,
73134d50519SLior David 					   wdev->conn_bss_type,
7326eb18137SDedy Lansky 					   IEEE80211_PRIVACY_ANY);
7335349a0f7SVidyullatha Kanchanapally 		if (cr->bss)
7345349a0f7SVidyullatha Kanchanapally 			cfg80211_hold_bss(bss_from_pub(cr->bss));
7354c4d684aSUjjal Roy 	}
7364c4d684aSUjjal Roy 
737df7fc0f9SJohannes Berg 	if (wdev->current_bss) {
738df7fc0f9SJohannes Berg 		cfg80211_unhold_bss(wdev->current_bss);
7395b112d3dSJohannes Berg 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
740df7fc0f9SJohannes Berg 		wdev->current_bss = NULL;
741df7fc0f9SJohannes Berg 	}
742df7fc0f9SJohannes Berg 
7435349a0f7SVidyullatha Kanchanapally 	if (cr->status != WLAN_STATUS_SUCCESS) {
744b47f610bSJohannes Berg 		kzfree(wdev->connect_keys);
745fffd0934SJohannes Berg 		wdev->connect_keys = NULL;
7468dadadb7SJohannes Berg 		wdev->ssid_len = 0;
747bd2522b1SAndrzej Zaborowski 		wdev->conn_owner_nlportid = 0;
7485349a0f7SVidyullatha Kanchanapally 		if (cr->bss) {
7495349a0f7SVidyullatha Kanchanapally 			cfg80211_unhold_bss(bss_from_pub(cr->bss));
7505349a0f7SVidyullatha Kanchanapally 			cfg80211_put_bss(wdev->wiphy, cr->bss);
751f1940c57SJohannes Berg 		}
752c1fbb258SEliad Peller 		cfg80211_sme_free(wdev);
753fffd0934SJohannes Berg 		return;
754fffd0934SJohannes Berg 	}
755fffd0934SJohannes Berg 
7565349a0f7SVidyullatha Kanchanapally 	if (WARN_ON(!cr->bss))
757b23aa676SSamuel Ortiz 		return;
758f1940c57SJohannes Berg 
7595349a0f7SVidyullatha Kanchanapally 	wdev->current_bss = bss_from_pub(cr->bss);
760b23aa676SSamuel Ortiz 
761b8676221SDavid Spinadel 	if (!(wdev->wiphy->flags & WIPHY_FLAG_HAS_STATIC_WEP))
762fffd0934SJohannes Berg 		cfg80211_upload_connect_keys(wdev);
7638b19e6caSLuis R. Rodriguez 
7649caf0364SJohannes Berg 	rcu_read_lock();
7655349a0f7SVidyullatha Kanchanapally 	country_ie = ieee80211_bss_get_ie(cr->bss, WLAN_EID_COUNTRY);
7669caf0364SJohannes Berg 	if (!country_ie) {
7679caf0364SJohannes Berg 		rcu_read_unlock();
7689caf0364SJohannes Berg 		return;
7699caf0364SJohannes Berg 	}
7709caf0364SJohannes Berg 
7719caf0364SJohannes Berg 	country_ie = kmemdup(country_ie, 2 + country_ie[1], GFP_ATOMIC);
7729caf0364SJohannes Berg 	rcu_read_unlock();
7738b19e6caSLuis R. Rodriguez 
7748b19e6caSLuis R. Rodriguez 	if (!country_ie)
7758b19e6caSLuis R. Rodriguez 		return;
7768b19e6caSLuis R. Rodriguez 
7778b19e6caSLuis R. Rodriguez 	/*
7788b19e6caSLuis R. Rodriguez 	 * ieee80211_bss_get_ie() ensures we can access:
7798b19e6caSLuis R. Rodriguez 	 * - country_ie + 2, the start of the country ie data, and
7808b19e6caSLuis R. Rodriguez 	 * - and country_ie[1] which is the IE length
7818b19e6caSLuis R. Rodriguez 	 */
7825349a0f7SVidyullatha Kanchanapally 	regulatory_hint_country_ie(wdev->wiphy, cr->bss->channel->band,
7831a919318SJohannes Berg 				   country_ie + 2, country_ie[1]);
7849caf0364SJohannes Berg 	kfree(country_ie);
785b23aa676SSamuel Ortiz }
786f2129354SJohannes Berg 
787e7054989SKanchanapally, Vidyullatha /* Consumes bss object one way or another */
7885349a0f7SVidyullatha Kanchanapally void cfg80211_connect_done(struct net_device *dev,
7895349a0f7SVidyullatha Kanchanapally 			   struct cfg80211_connect_resp_params *params,
7905349a0f7SVidyullatha Kanchanapally 			   gfp_t gfp)
791f2129354SJohannes Berg {
792667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
793f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
794667503ddSJohannes Berg 	struct cfg80211_event *ev;
795667503ddSJohannes Berg 	unsigned long flags;
7965349a0f7SVidyullatha Kanchanapally 	u8 *next;
797667503ddSJohannes Berg 
7985349a0f7SVidyullatha Kanchanapally 	if (params->bss) {
799e7054989SKanchanapally, Vidyullatha 		/* Make sure the bss entry provided by the driver is valid. */
8005349a0f7SVidyullatha Kanchanapally 		struct cfg80211_internal_bss *ibss = bss_from_pub(params->bss);
801e7054989SKanchanapally, Vidyullatha 
802e7054989SKanchanapally, Vidyullatha 		if (WARN_ON(list_empty(&ibss->list))) {
8035349a0f7SVidyullatha Kanchanapally 			cfg80211_put_bss(wdev->wiphy, params->bss);
804667503ddSJohannes Berg 			return;
805e7054989SKanchanapally, Vidyullatha 		}
806e7054989SKanchanapally, Vidyullatha 	}
807e7054989SKanchanapally, Vidyullatha 
8085349a0f7SVidyullatha Kanchanapally 	ev = kzalloc(sizeof(*ev) + (params->bssid ? ETH_ALEN : 0) +
809a3caf744SVidyullatha Kanchanapally 		     params->req_ie_len + params->resp_ie_len +
810a3caf744SVidyullatha Kanchanapally 		     params->fils_kek_len + params->pmk_len +
811a3caf744SVidyullatha Kanchanapally 		     (params->pmkid ? WLAN_PMKID_LEN : 0), gfp);
812e7054989SKanchanapally, Vidyullatha 	if (!ev) {
8135349a0f7SVidyullatha Kanchanapally 		cfg80211_put_bss(wdev->wiphy, params->bss);
814e7054989SKanchanapally, Vidyullatha 		return;
815e7054989SKanchanapally, Vidyullatha 	}
816667503ddSJohannes Berg 
817667503ddSJohannes Berg 	ev->type = EVENT_CONNECT_RESULT;
8185349a0f7SVidyullatha Kanchanapally 	next = ((u8 *)ev) + sizeof(*ev);
8195349a0f7SVidyullatha Kanchanapally 	if (params->bssid) {
8205349a0f7SVidyullatha Kanchanapally 		ev->cr.bssid = next;
8215349a0f7SVidyullatha Kanchanapally 		memcpy((void *)ev->cr.bssid, params->bssid, ETH_ALEN);
8225349a0f7SVidyullatha Kanchanapally 		next += ETH_ALEN;
8237834704bSNishant Sarmukadam 	}
8245349a0f7SVidyullatha Kanchanapally 	if (params->req_ie_len) {
8255349a0f7SVidyullatha Kanchanapally 		ev->cr.req_ie = next;
8265349a0f7SVidyullatha Kanchanapally 		ev->cr.req_ie_len = params->req_ie_len;
8275349a0f7SVidyullatha Kanchanapally 		memcpy((void *)ev->cr.req_ie, params->req_ie,
8285349a0f7SVidyullatha Kanchanapally 		       params->req_ie_len);
8295349a0f7SVidyullatha Kanchanapally 		next += params->req_ie_len;
8307834704bSNishant Sarmukadam 	}
8315349a0f7SVidyullatha Kanchanapally 	if (params->resp_ie_len) {
8325349a0f7SVidyullatha Kanchanapally 		ev->cr.resp_ie = next;
8335349a0f7SVidyullatha Kanchanapally 		ev->cr.resp_ie_len = params->resp_ie_len;
8345349a0f7SVidyullatha Kanchanapally 		memcpy((void *)ev->cr.resp_ie, params->resp_ie,
8355349a0f7SVidyullatha Kanchanapally 		       params->resp_ie_len);
8365349a0f7SVidyullatha Kanchanapally 		next += params->resp_ie_len;
8375349a0f7SVidyullatha Kanchanapally 	}
838a3caf744SVidyullatha Kanchanapally 	if (params->fils_kek_len) {
839a3caf744SVidyullatha Kanchanapally 		ev->cr.fils_kek = next;
840a3caf744SVidyullatha Kanchanapally 		ev->cr.fils_kek_len = params->fils_kek_len;
841a3caf744SVidyullatha Kanchanapally 		memcpy((void *)ev->cr.fils_kek, params->fils_kek,
842a3caf744SVidyullatha Kanchanapally 		       params->fils_kek_len);
843a3caf744SVidyullatha Kanchanapally 		next += params->fils_kek_len;
844a3caf744SVidyullatha Kanchanapally 	}
845a3caf744SVidyullatha Kanchanapally 	if (params->pmk_len) {
846a3caf744SVidyullatha Kanchanapally 		ev->cr.pmk = next;
847a3caf744SVidyullatha Kanchanapally 		ev->cr.pmk_len = params->pmk_len;
848a3caf744SVidyullatha Kanchanapally 		memcpy((void *)ev->cr.pmk, params->pmk, params->pmk_len);
849a3caf744SVidyullatha Kanchanapally 		next += params->pmk_len;
850a3caf744SVidyullatha Kanchanapally 	}
851a3caf744SVidyullatha Kanchanapally 	if (params->pmkid) {
852a3caf744SVidyullatha Kanchanapally 		ev->cr.pmkid = next;
853a3caf744SVidyullatha Kanchanapally 		memcpy((void *)ev->cr.pmkid, params->pmkid, WLAN_PMKID_LEN);
854a3caf744SVidyullatha Kanchanapally 		next += WLAN_PMKID_LEN;
855a3caf744SVidyullatha Kanchanapally 	}
856a3caf744SVidyullatha Kanchanapally 	ev->cr.update_erp_next_seq_num = params->update_erp_next_seq_num;
857a3caf744SVidyullatha Kanchanapally 	if (params->update_erp_next_seq_num)
858a3caf744SVidyullatha Kanchanapally 		ev->cr.fils_erp_next_seq_num = params->fils_erp_next_seq_num;
8595349a0f7SVidyullatha Kanchanapally 	if (params->bss)
8605349a0f7SVidyullatha Kanchanapally 		cfg80211_hold_bss(bss_from_pub(params->bss));
8615349a0f7SVidyullatha Kanchanapally 	ev->cr.bss = params->bss;
8625349a0f7SVidyullatha Kanchanapally 	ev->cr.status = params->status;
8635349a0f7SVidyullatha Kanchanapally 	ev->cr.timeout_reason = params->timeout_reason;
864667503ddSJohannes Berg 
865667503ddSJohannes Berg 	spin_lock_irqsave(&wdev->event_lock, flags);
866667503ddSJohannes Berg 	list_add_tail(&ev->list, &wdev->event_list);
867667503ddSJohannes Berg 	spin_unlock_irqrestore(&wdev->event_lock, flags);
868e60d7443SAlban Browaeys 	queue_work(cfg80211_wq, &rdev->event_work);
869f2129354SJohannes Berg }
8705349a0f7SVidyullatha Kanchanapally EXPORT_SYMBOL(cfg80211_connect_done);
871b23aa676SSamuel Ortiz 
8720e3a39b5SBen Greear /* Consumes bss object one way or another */
873ed9d0102SJouni Malinen void __cfg80211_roamed(struct wireless_dev *wdev,
87429ce6ecbSAvraham Stern 		       struct cfg80211_roam_info *info)
875b23aa676SSamuel Ortiz {
8763d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
877b23aa676SSamuel Ortiz 	union iwreq_data wrqu;
878b23aa676SSamuel Ortiz #endif
879667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
880667503ddSJohannes Berg 
881074ac8dfSJohannes Berg 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
882074ac8dfSJohannes Berg 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
883adbde344SVasanthakumar Thiagarajan 		goto out;
884b23aa676SSamuel Ortiz 
885ceca7b71SJohannes Berg 	if (WARN_ON(!wdev->current_bss))
886adbde344SVasanthakumar Thiagarajan 		goto out;
887b23aa676SSamuel Ortiz 
888b23aa676SSamuel Ortiz 	cfg80211_unhold_bss(wdev->current_bss);
8895b112d3dSJohannes Berg 	cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
890b23aa676SSamuel Ortiz 	wdev->current_bss = NULL;
891b23aa676SSamuel Ortiz 
89229ce6ecbSAvraham Stern 	if (WARN_ON(!info->bss))
89329ce6ecbSAvraham Stern 		return;
89429ce6ecbSAvraham Stern 
89529ce6ecbSAvraham Stern 	cfg80211_hold_bss(bss_from_pub(info->bss));
89629ce6ecbSAvraham Stern 	wdev->current_bss = bss_from_pub(info->bss);
897b23aa676SSamuel Ortiz 
898f26cbf40SZhao, Gang 	nl80211_send_roamed(wiphy_to_rdev(wdev->wiphy),
89929ce6ecbSAvraham Stern 			    wdev->netdev, info, GFP_KERNEL);
900b23aa676SSamuel Ortiz 
9013d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
90229ce6ecbSAvraham Stern 	if (info->req_ie) {
903b23aa676SSamuel Ortiz 		memset(&wrqu, 0, sizeof(wrqu));
90429ce6ecbSAvraham Stern 		wrqu.data.length = info->req_ie_len;
9053409ff77SZhu Yi 		wireless_send_event(wdev->netdev, IWEVASSOCREQIE,
90629ce6ecbSAvraham Stern 				    &wrqu, info->req_ie);
907b23aa676SSamuel Ortiz 	}
908b23aa676SSamuel Ortiz 
90929ce6ecbSAvraham Stern 	if (info->resp_ie) {
910b23aa676SSamuel Ortiz 		memset(&wrqu, 0, sizeof(wrqu));
91129ce6ecbSAvraham Stern 		wrqu.data.length = info->resp_ie_len;
912667503ddSJohannes Berg 		wireless_send_event(wdev->netdev, IWEVASSOCRESPIE,
91329ce6ecbSAvraham Stern 				    &wrqu, info->resp_ie);
914b23aa676SSamuel Ortiz 	}
915b23aa676SSamuel Ortiz 
916b23aa676SSamuel Ortiz 	memset(&wrqu, 0, sizeof(wrqu));
917b23aa676SSamuel Ortiz 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
91829ce6ecbSAvraham Stern 	memcpy(wrqu.ap_addr.sa_data, info->bss->bssid, ETH_ALEN);
91929ce6ecbSAvraham Stern 	memcpy(wdev->wext.prev_bssid, info->bss->bssid, ETH_ALEN);
920f401a6f7SJohannes Berg 	wdev->wext.prev_bssid_valid = true;
921667503ddSJohannes Berg 	wireless_send_event(wdev->netdev, SIOCGIWAP, &wrqu, NULL);
922b23aa676SSamuel Ortiz #endif
923adbde344SVasanthakumar Thiagarajan 
924adbde344SVasanthakumar Thiagarajan 	return;
925adbde344SVasanthakumar Thiagarajan out:
92629ce6ecbSAvraham Stern 	cfg80211_put_bss(wdev->wiphy, info->bss);
927b23aa676SSamuel Ortiz }
928667503ddSJohannes Berg 
92929ce6ecbSAvraham Stern /* Consumes info->bss object one way or another */
93029ce6ecbSAvraham Stern void cfg80211_roamed(struct net_device *dev, struct cfg80211_roam_info *info,
93129ce6ecbSAvraham Stern 		     gfp_t gfp)
932adbde344SVasanthakumar Thiagarajan {
933adbde344SVasanthakumar Thiagarajan 	struct wireless_dev *wdev = dev->ieee80211_ptr;
934f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
935667503ddSJohannes Berg 	struct cfg80211_event *ev;
936667503ddSJohannes Berg 	unsigned long flags;
937667503ddSJohannes Berg 
93829ce6ecbSAvraham Stern 	if (!info->bss) {
93929ce6ecbSAvraham Stern 		info->bss = cfg80211_get_bss(wdev->wiphy, info->channel,
94029ce6ecbSAvraham Stern 					     info->bssid, wdev->ssid,
94129ce6ecbSAvraham Stern 					     wdev->ssid_len,
94229ce6ecbSAvraham Stern 					     wdev->conn_bss_type,
94329ce6ecbSAvraham Stern 					     IEEE80211_PRIVACY_ANY);
94429ce6ecbSAvraham Stern 	}
94529ce6ecbSAvraham Stern 
94629ce6ecbSAvraham Stern 	if (WARN_ON(!info->bss))
947667503ddSJohannes Berg 		return;
948667503ddSJohannes Berg 
94929ce6ecbSAvraham Stern 	ev = kzalloc(sizeof(*ev) + info->req_ie_len + info->resp_ie_len, gfp);
950adbde344SVasanthakumar Thiagarajan 	if (!ev) {
95129ce6ecbSAvraham Stern 		cfg80211_put_bss(wdev->wiphy, info->bss);
952adbde344SVasanthakumar Thiagarajan 		return;
953adbde344SVasanthakumar Thiagarajan 	}
954adbde344SVasanthakumar Thiagarajan 
955667503ddSJohannes Berg 	ev->type = EVENT_ROAMED;
956667503ddSJohannes Berg 	ev->rm.req_ie = ((u8 *)ev) + sizeof(*ev);
95729ce6ecbSAvraham Stern 	ev->rm.req_ie_len = info->req_ie_len;
95829ce6ecbSAvraham Stern 	memcpy((void *)ev->rm.req_ie, info->req_ie, info->req_ie_len);
95929ce6ecbSAvraham Stern 	ev->rm.resp_ie = ((u8 *)ev) + sizeof(*ev) + info->req_ie_len;
96029ce6ecbSAvraham Stern 	ev->rm.resp_ie_len = info->resp_ie_len;
96129ce6ecbSAvraham Stern 	memcpy((void *)ev->rm.resp_ie, info->resp_ie, info->resp_ie_len);
96229ce6ecbSAvraham Stern 	ev->rm.bss = info->bss;
963667503ddSJohannes Berg 
964667503ddSJohannes Berg 	spin_lock_irqsave(&wdev->event_lock, flags);
965667503ddSJohannes Berg 	list_add_tail(&ev->list, &wdev->event_list);
966667503ddSJohannes Berg 	spin_unlock_irqrestore(&wdev->event_lock, flags);
967e60d7443SAlban Browaeys 	queue_work(cfg80211_wq, &rdev->event_work);
968667503ddSJohannes Berg }
96929ce6ecbSAvraham Stern EXPORT_SYMBOL(cfg80211_roamed);
970b23aa676SSamuel Ortiz 
971*503c1fb9SAvraham Stern void __cfg80211_port_authorized(struct wireless_dev *wdev, const u8 *bssid)
972*503c1fb9SAvraham Stern {
973*503c1fb9SAvraham Stern 	ASSERT_WDEV_LOCK(wdev);
974*503c1fb9SAvraham Stern 
975*503c1fb9SAvraham Stern 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION))
976*503c1fb9SAvraham Stern 		return;
977*503c1fb9SAvraham Stern 
978*503c1fb9SAvraham Stern 	if (WARN_ON(!wdev->current_bss) ||
979*503c1fb9SAvraham Stern 	    WARN_ON(!ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
980*503c1fb9SAvraham Stern 		return;
981*503c1fb9SAvraham Stern 
982*503c1fb9SAvraham Stern 	nl80211_send_port_authorized(wiphy_to_rdev(wdev->wiphy), wdev->netdev,
983*503c1fb9SAvraham Stern 				     bssid);
984*503c1fb9SAvraham Stern }
985*503c1fb9SAvraham Stern 
986*503c1fb9SAvraham Stern void cfg80211_port_authorized(struct net_device *dev, const u8 *bssid,
987*503c1fb9SAvraham Stern 			      gfp_t gfp)
988*503c1fb9SAvraham Stern {
989*503c1fb9SAvraham Stern 	struct wireless_dev *wdev = dev->ieee80211_ptr;
990*503c1fb9SAvraham Stern 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
991*503c1fb9SAvraham Stern 	struct cfg80211_event *ev;
992*503c1fb9SAvraham Stern 	unsigned long flags;
993*503c1fb9SAvraham Stern 
994*503c1fb9SAvraham Stern 	if (WARN_ON(!bssid))
995*503c1fb9SAvraham Stern 		return;
996*503c1fb9SAvraham Stern 
997*503c1fb9SAvraham Stern 	ev = kzalloc(sizeof(*ev), gfp);
998*503c1fb9SAvraham Stern 	if (!ev)
999*503c1fb9SAvraham Stern 		return;
1000*503c1fb9SAvraham Stern 
1001*503c1fb9SAvraham Stern 	ev->type = EVENT_PORT_AUTHORIZED;
1002*503c1fb9SAvraham Stern 	memcpy(ev->pa.bssid, bssid, ETH_ALEN);
1003*503c1fb9SAvraham Stern 
1004*503c1fb9SAvraham Stern 	/*
1005*503c1fb9SAvraham Stern 	 * Use the wdev event list so that if there are pending
1006*503c1fb9SAvraham Stern 	 * connected/roamed events, they will be reported first.
1007*503c1fb9SAvraham Stern 	 */
1008*503c1fb9SAvraham Stern 	spin_lock_irqsave(&wdev->event_lock, flags);
1009*503c1fb9SAvraham Stern 	list_add_tail(&ev->list, &wdev->event_list);
1010*503c1fb9SAvraham Stern 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1011*503c1fb9SAvraham Stern 	queue_work(cfg80211_wq, &rdev->event_work);
1012*503c1fb9SAvraham Stern }
1013*503c1fb9SAvraham Stern EXPORT_SYMBOL(cfg80211_port_authorized);
1014*503c1fb9SAvraham Stern 
1015667503ddSJohannes Berg void __cfg80211_disconnected(struct net_device *dev, const u8 *ie,
10166829c878SJohannes Berg 			     size_t ie_len, u16 reason, bool from_ap)
1017b23aa676SSamuel Ortiz {
1018b23aa676SSamuel Ortiz 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1019f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1020fffd0934SJohannes Berg 	int i;
10213d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
1022b23aa676SSamuel Ortiz 	union iwreq_data wrqu;
1023b23aa676SSamuel Ortiz #endif
1024b23aa676SSamuel Ortiz 
1025667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
1026667503ddSJohannes Berg 
1027074ac8dfSJohannes Berg 	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_STATION &&
1028074ac8dfSJohannes Berg 		    wdev->iftype != NL80211_IFTYPE_P2P_CLIENT))
1029b23aa676SSamuel Ortiz 		return;
1030b23aa676SSamuel Ortiz 
1031b23aa676SSamuel Ortiz 	if (wdev->current_bss) {
1032b23aa676SSamuel Ortiz 		cfg80211_unhold_bss(wdev->current_bss);
10335b112d3dSJohannes Berg 		cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
1034b23aa676SSamuel Ortiz 	}
1035b23aa676SSamuel Ortiz 
1036b23aa676SSamuel Ortiz 	wdev->current_bss = NULL;
10378dadadb7SJohannes Berg 	wdev->ssid_len = 0;
1038bd2522b1SAndrzej Zaborowski 	wdev->conn_owner_nlportid = 0;
1039b23aa676SSamuel Ortiz 
1040fffd0934SJohannes Berg 	nl80211_send_disconnected(rdev, dev, reason, ie, ie_len, from_ap);
1041fffd0934SJohannes Berg 
1042b8607152SArend van Spriel 	/* stop critical protocol if supported */
1043b8607152SArend van Spriel 	if (rdev->ops->crit_proto_stop && rdev->crit_proto_nlportid) {
1044b8607152SArend van Spriel 		rdev->crit_proto_nlportid = 0;
1045b8607152SArend van Spriel 		rdev_crit_proto_stop(rdev, wdev);
1046b8607152SArend van Spriel 	}
1047b8607152SArend van Spriel 
1048fffd0934SJohannes Berg 	/*
1049fffd0934SJohannes Berg 	 * Delete all the keys ... pairwise keys can't really
1050fffd0934SJohannes Berg 	 * exist any more anyway, but default keys might.
1051fffd0934SJohannes Berg 	 */
1052fffd0934SJohannes Berg 	if (rdev->ops->del_key)
1053fffd0934SJohannes Berg 		for (i = 0; i < 6; i++)
1054e35e4d28SHila Gonen 			rdev_del_key(rdev, dev, i, false, NULL);
1055b23aa676SSamuel Ortiz 
1056fa9ffc74SKyeyoon Park 	rdev_set_qos_map(rdev, dev, NULL);
1057fa9ffc74SKyeyoon Park 
10583d23e349SJohannes Berg #ifdef CONFIG_CFG80211_WEXT
1059b23aa676SSamuel Ortiz 	memset(&wrqu, 0, sizeof(wrqu));
1060b23aa676SSamuel Ortiz 	wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1061b23aa676SSamuel Ortiz 	wireless_send_event(dev, SIOCGIWAP, &wrqu, NULL);
10625f612033SAbhijeet Kolekar 	wdev->wext.connect.ssid_len = 0;
1063b23aa676SSamuel Ortiz #endif
106409d989d1SLuis R. Rodriguez 
106509d989d1SLuis R. Rodriguez 	schedule_work(&cfg80211_disconnect_work);
1066b23aa676SSamuel Ortiz }
1067b23aa676SSamuel Ortiz 
1068b23aa676SSamuel Ortiz void cfg80211_disconnected(struct net_device *dev, u16 reason,
106980279fb7SJohannes Berg 			   const u8 *ie, size_t ie_len,
107080279fb7SJohannes Berg 			   bool locally_generated, gfp_t gfp)
1071b23aa676SSamuel Ortiz {
1072667503ddSJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1073f26cbf40SZhao, Gang 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1074667503ddSJohannes Berg 	struct cfg80211_event *ev;
1075667503ddSJohannes Berg 	unsigned long flags;
1076667503ddSJohannes Berg 
1077667503ddSJohannes Berg 	ev = kzalloc(sizeof(*ev) + ie_len, gfp);
1078667503ddSJohannes Berg 	if (!ev)
1079667503ddSJohannes Berg 		return;
1080667503ddSJohannes Berg 
1081667503ddSJohannes Berg 	ev->type = EVENT_DISCONNECTED;
1082667503ddSJohannes Berg 	ev->dc.ie = ((u8 *)ev) + sizeof(*ev);
1083667503ddSJohannes Berg 	ev->dc.ie_len = ie_len;
1084667503ddSJohannes Berg 	memcpy((void *)ev->dc.ie, ie, ie_len);
1085667503ddSJohannes Berg 	ev->dc.reason = reason;
108680279fb7SJohannes Berg 	ev->dc.locally_generated = locally_generated;
1087667503ddSJohannes Berg 
1088667503ddSJohannes Berg 	spin_lock_irqsave(&wdev->event_lock, flags);
1089667503ddSJohannes Berg 	list_add_tail(&ev->list, &wdev->event_list);
1090667503ddSJohannes Berg 	spin_unlock_irqrestore(&wdev->event_lock, flags);
1091e60d7443SAlban Browaeys 	queue_work(cfg80211_wq, &rdev->event_work);
1092b23aa676SSamuel Ortiz }
1093b23aa676SSamuel Ortiz EXPORT_SYMBOL(cfg80211_disconnected);
1094b23aa676SSamuel Ortiz 
1095ceca7b71SJohannes Berg /*
1096ceca7b71SJohannes Berg  * API calls for nl80211/wext compatibility code
1097ceca7b71SJohannes Berg  */
109883739b03SJohannes Berg int cfg80211_connect(struct cfg80211_registered_device *rdev,
1099b23aa676SSamuel Ortiz 		     struct net_device *dev,
1100fffd0934SJohannes Berg 		     struct cfg80211_connect_params *connect,
1101f401a6f7SJohannes Berg 		     struct cfg80211_cached_keys *connkeys,
1102f401a6f7SJohannes Berg 		     const u8 *prev_bssid)
1103b23aa676SSamuel Ortiz {
1104b23aa676SSamuel Ortiz 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1105667503ddSJohannes Berg 	int err;
1106667503ddSJohannes Berg 
1107667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
1108b23aa676SSamuel Ortiz 
1109fffd0934SJohannes Berg 	if (WARN_ON(wdev->connect_keys)) {
1110b47f610bSJohannes Berg 		kzfree(wdev->connect_keys);
1111fffd0934SJohannes Berg 		wdev->connect_keys = NULL;
1112fffd0934SJohannes Berg 	}
1113fffd0934SJohannes Berg 
11147e7c8926SBen Greear 	cfg80211_oper_and_ht_capa(&connect->ht_capa_mask,
11157e7c8926SBen Greear 				  rdev->wiphy.ht_capa_mod_mask);
11167e7c8926SBen Greear 
1117fffd0934SJohannes Berg 	if (connkeys && connkeys->def >= 0) {
1118fffd0934SJohannes Berg 		int idx;
1119bcba8eaeSSamuel Ortiz 		u32 cipher;
1120fffd0934SJohannes Berg 
1121fffd0934SJohannes Berg 		idx = connkeys->def;
1122bcba8eaeSSamuel Ortiz 		cipher = connkeys->params[idx].cipher;
1123fffd0934SJohannes Berg 		/* If given a WEP key we may need it for shared key auth */
1124bcba8eaeSSamuel Ortiz 		if (cipher == WLAN_CIPHER_SUITE_WEP40 ||
1125bcba8eaeSSamuel Ortiz 		    cipher == WLAN_CIPHER_SUITE_WEP104) {
1126fffd0934SJohannes Berg 			connect->key_idx = idx;
1127fffd0934SJohannes Berg 			connect->key = connkeys->params[idx].key;
1128fffd0934SJohannes Berg 			connect->key_len = connkeys->params[idx].key_len;
1129bcba8eaeSSamuel Ortiz 
1130bcba8eaeSSamuel Ortiz 			/*
1131bcba8eaeSSamuel Ortiz 			 * If ciphers are not set (e.g. when going through
1132bcba8eaeSSamuel Ortiz 			 * iwconfig), we have to set them appropriately here.
1133bcba8eaeSSamuel Ortiz 			 */
1134bcba8eaeSSamuel Ortiz 			if (connect->crypto.cipher_group == 0)
1135bcba8eaeSSamuel Ortiz 				connect->crypto.cipher_group = cipher;
1136bcba8eaeSSamuel Ortiz 
1137bcba8eaeSSamuel Ortiz 			if (connect->crypto.n_ciphers_pairwise == 0) {
1138bcba8eaeSSamuel Ortiz 				connect->crypto.n_ciphers_pairwise = 1;
1139bcba8eaeSSamuel Ortiz 				connect->crypto.ciphers_pairwise[0] = cipher;
1140bcba8eaeSSamuel Ortiz 			}
1141fffd0934SJohannes Berg 		}
1142b8676221SDavid Spinadel 
1143b8676221SDavid Spinadel 		connect->crypto.wep_keys = connkeys->params;
1144b8676221SDavid Spinadel 		connect->crypto.wep_tx_key = connkeys->def;
1145f1c1f17aSJohannes Berg 	} else {
1146f1c1f17aSJohannes Berg 		if (WARN_ON(connkeys))
1147f1c1f17aSJohannes Berg 			return -EINVAL;
1148fffd0934SJohannes Berg 	}
1149fffd0934SJohannes Berg 
1150ceca7b71SJohannes Berg 	wdev->connect_keys = connkeys;
11516829c878SJohannes Berg 	memcpy(wdev->ssid, connect->ssid, connect->ssid_len);
11526829c878SJohannes Berg 	wdev->ssid_len = connect->ssid_len;
11536829c878SJohannes Berg 
115434d50519SLior David 	wdev->conn_bss_type = connect->pbss ? IEEE80211_BSS_TYPE_PBSS :
115534d50519SLior David 					      IEEE80211_BSS_TYPE_ESS;
115634d50519SLior David 
1157ceca7b71SJohannes Berg 	if (!rdev->ops->connect)
1158ceca7b71SJohannes Berg 		err = cfg80211_sme_connect(wdev, connect, prev_bssid);
1159ceca7b71SJohannes Berg 	else
1160ceca7b71SJohannes Berg 		err = rdev_connect(rdev, dev, connect);
11616829c878SJohannes Berg 
116219957bb3SJohannes Berg 	if (err) {
1163fffd0934SJohannes Berg 		wdev->connect_keys = NULL;
11648dadadb7SJohannes Berg 		wdev->ssid_len = 0;
1165b23aa676SSamuel Ortiz 		return err;
1166b23aa676SSamuel Ortiz 	}
1167b23aa676SSamuel Ortiz 
1168b23aa676SSamuel Ortiz 	return 0;
1169b23aa676SSamuel Ortiz }
1170b23aa676SSamuel Ortiz 
117183739b03SJohannes Berg int cfg80211_disconnect(struct cfg80211_registered_device *rdev,
1172f2129354SJohannes Berg 			struct net_device *dev, u16 reason, bool wextev)
1173b23aa676SSamuel Ortiz {
11746829c878SJohannes Berg 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1175dee8a973SJohannes Berg 	int err = 0;
1176b23aa676SSamuel Ortiz 
1177667503ddSJohannes Berg 	ASSERT_WDEV_LOCK(wdev);
1178667503ddSJohannes Berg 
1179b47f610bSJohannes Berg 	kzfree(wdev->connect_keys);
1180fffd0934SJohannes Berg 	wdev->connect_keys = NULL;
1181fffd0934SJohannes Berg 
1182bd2522b1SAndrzej Zaborowski 	wdev->conn_owner_nlportid = 0;
1183bd2522b1SAndrzej Zaborowski 
1184dee8a973SJohannes Berg 	if (wdev->conn)
1185ceca7b71SJohannes Berg 		err = cfg80211_sme_disconnect(wdev, reason);
1186dee8a973SJohannes Berg 	else if (!rdev->ops->disconnect)
118719957bb3SJohannes Berg 		cfg80211_mlme_down(rdev, dev);
11880711d638SIlan Peer 	else if (wdev->ssid_len)
1189e35e4d28SHila Gonen 		err = rdev_disconnect(rdev, dev, reason);
1190ceca7b71SJohannes Berg 
1191b23aa676SSamuel Ortiz 	return err;
1192b23aa676SSamuel Ortiz }
1193bd2522b1SAndrzej Zaborowski 
1194bd2522b1SAndrzej Zaborowski /*
1195bd2522b1SAndrzej Zaborowski  * Used to clean up after the connection / connection attempt owner socket
1196bd2522b1SAndrzej Zaborowski  * disconnects
1197bd2522b1SAndrzej Zaborowski  */
1198bd2522b1SAndrzej Zaborowski void cfg80211_autodisconnect_wk(struct work_struct *work)
1199bd2522b1SAndrzej Zaborowski {
1200bd2522b1SAndrzej Zaborowski 	struct wireless_dev *wdev =
1201bd2522b1SAndrzej Zaborowski 		container_of(work, struct wireless_dev, disconnect_wk);
1202bd2522b1SAndrzej Zaborowski 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1203bd2522b1SAndrzej Zaborowski 
1204bd2522b1SAndrzej Zaborowski 	wdev_lock(wdev);
1205bd2522b1SAndrzej Zaborowski 
1206bd2522b1SAndrzej Zaborowski 	if (wdev->conn_owner_nlportid) {
1207bd2522b1SAndrzej Zaborowski 		/*
1208bd2522b1SAndrzej Zaborowski 		 * Use disconnect_bssid if still connecting and ops->disconnect
1209bd2522b1SAndrzej Zaborowski 		 * not implemented.  Otherwise we can use cfg80211_disconnect.
1210bd2522b1SAndrzej Zaborowski 		 */
1211bd2522b1SAndrzej Zaborowski 		if (rdev->ops->disconnect || wdev->current_bss)
1212bd2522b1SAndrzej Zaborowski 			cfg80211_disconnect(rdev, wdev->netdev,
1213bd2522b1SAndrzej Zaborowski 					    WLAN_REASON_DEAUTH_LEAVING, true);
1214bd2522b1SAndrzej Zaborowski 		else
1215bd2522b1SAndrzej Zaborowski 			cfg80211_mlme_deauth(rdev, wdev->netdev,
1216bd2522b1SAndrzej Zaborowski 					     wdev->disconnect_bssid, NULL, 0,
1217bd2522b1SAndrzej Zaborowski 					     WLAN_REASON_DEAUTH_LEAVING, false);
1218bd2522b1SAndrzej Zaborowski 	}
1219bd2522b1SAndrzej Zaborowski 
1220bd2522b1SAndrzej Zaborowski 	wdev_unlock(wdev);
1221bd2522b1SAndrzej Zaborowski }
1222