xref: /OK3568_Linux_fs/kernel/net/wireless/mlme.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * cfg80211 MLME SAP interface
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
6*4882a593Smuzhiyun  * Copyright (c) 2015		Intel Deutschland GmbH
7*4882a593Smuzhiyun  * Copyright (C) 2019 Intel Corporation
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/kernel.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/etherdevice.h>
13*4882a593Smuzhiyun #include <linux/netdevice.h>
14*4882a593Smuzhiyun #include <linux/nl80211.h>
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun #include <linux/wireless.h>
17*4882a593Smuzhiyun #include <net/cfg80211.h>
18*4882a593Smuzhiyun #include <net/iw_handler.h>
19*4882a593Smuzhiyun #include "core.h"
20*4882a593Smuzhiyun #include "nl80211.h"
21*4882a593Smuzhiyun #include "rdev-ops.h"
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun 
cfg80211_rx_assoc_resp(struct net_device * dev,struct cfg80211_bss * bss,const u8 * buf,size_t len,int uapsd_queues,const u8 * req_ies,size_t req_ies_len)24*4882a593Smuzhiyun void cfg80211_rx_assoc_resp(struct net_device *dev, struct cfg80211_bss *bss,
25*4882a593Smuzhiyun 			    const u8 *buf, size_t len, int uapsd_queues,
26*4882a593Smuzhiyun 			    const u8 *req_ies, size_t req_ies_len)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun 	struct wireless_dev *wdev = dev->ieee80211_ptr;
29*4882a593Smuzhiyun 	struct wiphy *wiphy = wdev->wiphy;
30*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
31*4882a593Smuzhiyun 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
32*4882a593Smuzhiyun 	struct cfg80211_connect_resp_params cr;
33*4882a593Smuzhiyun 	const u8 *resp_ie = mgmt->u.assoc_resp.variable;
34*4882a593Smuzhiyun 	size_t resp_ie_len = len - offsetof(struct ieee80211_mgmt,
35*4882a593Smuzhiyun 					    u.assoc_resp.variable);
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	if (bss->channel->band == NL80211_BAND_S1GHZ) {
38*4882a593Smuzhiyun 		resp_ie = (u8 *)&mgmt->u.s1g_assoc_resp.variable;
39*4882a593Smuzhiyun 		resp_ie_len = len - offsetof(struct ieee80211_mgmt,
40*4882a593Smuzhiyun 					     u.s1g_assoc_resp.variable);
41*4882a593Smuzhiyun 	}
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	memset(&cr, 0, sizeof(cr));
44*4882a593Smuzhiyun 	cr.status = (int)le16_to_cpu(mgmt->u.assoc_resp.status_code);
45*4882a593Smuzhiyun 	cr.bssid = mgmt->bssid;
46*4882a593Smuzhiyun 	cr.bss = bss;
47*4882a593Smuzhiyun 	cr.req_ie = req_ies;
48*4882a593Smuzhiyun 	cr.req_ie_len = req_ies_len;
49*4882a593Smuzhiyun 	cr.resp_ie = resp_ie;
50*4882a593Smuzhiyun 	cr.resp_ie_len = resp_ie_len;
51*4882a593Smuzhiyun 	cr.timeout_reason = NL80211_TIMEOUT_UNSPECIFIED;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	trace_cfg80211_send_rx_assoc(dev, bss);
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	/*
56*4882a593Smuzhiyun 	 * This is a bit of a hack, we don't notify userspace of
57*4882a593Smuzhiyun 	 * a (re-)association reply if we tried to send a reassoc
58*4882a593Smuzhiyun 	 * and got a reject -- we only try again with an assoc
59*4882a593Smuzhiyun 	 * frame instead of reassoc.
60*4882a593Smuzhiyun 	 */
61*4882a593Smuzhiyun 	if (cfg80211_sme_rx_assoc_resp(wdev, cr.status)) {
62*4882a593Smuzhiyun 		cfg80211_unhold_bss(bss_from_pub(bss));
63*4882a593Smuzhiyun 		cfg80211_put_bss(wiphy, bss);
64*4882a593Smuzhiyun 		return;
65*4882a593Smuzhiyun 	}
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	nl80211_send_rx_assoc(rdev, dev, buf, len, GFP_KERNEL, uapsd_queues,
68*4882a593Smuzhiyun 			      req_ies, req_ies_len);
69*4882a593Smuzhiyun 	/* update current_bss etc., consumes the bss reference */
70*4882a593Smuzhiyun 	__cfg80211_connect_result(dev, &cr, cr.status == WLAN_STATUS_SUCCESS);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun EXPORT_SYMBOL(cfg80211_rx_assoc_resp);
73*4882a593Smuzhiyun 
cfg80211_process_auth(struct wireless_dev * wdev,const u8 * buf,size_t len)74*4882a593Smuzhiyun static void cfg80211_process_auth(struct wireless_dev *wdev,
75*4882a593Smuzhiyun 				  const u8 *buf, size_t len)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	nl80211_send_rx_auth(rdev, wdev->netdev, buf, len, GFP_KERNEL);
80*4882a593Smuzhiyun 	cfg80211_sme_rx_auth(wdev, buf, len);
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun 
cfg80211_process_deauth(struct wireless_dev * wdev,const u8 * buf,size_t len)83*4882a593Smuzhiyun static void cfg80211_process_deauth(struct wireless_dev *wdev,
84*4882a593Smuzhiyun 				    const u8 *buf, size_t len)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
87*4882a593Smuzhiyun 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
88*4882a593Smuzhiyun 	const u8 *bssid = mgmt->bssid;
89*4882a593Smuzhiyun 	u16 reason_code = le16_to_cpu(mgmt->u.deauth.reason_code);
90*4882a593Smuzhiyun 	bool from_ap = !ether_addr_equal(mgmt->sa, wdev->netdev->dev_addr);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	nl80211_send_deauth(rdev, wdev->netdev, buf, len, GFP_KERNEL);
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun 	if (!wdev->current_bss ||
95*4882a593Smuzhiyun 	    !ether_addr_equal(wdev->current_bss->pub.bssid, bssid))
96*4882a593Smuzhiyun 		return;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	__cfg80211_disconnected(wdev->netdev, NULL, 0, reason_code, from_ap);
99*4882a593Smuzhiyun 	cfg80211_sme_deauth(wdev);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun 
cfg80211_process_disassoc(struct wireless_dev * wdev,const u8 * buf,size_t len)102*4882a593Smuzhiyun static void cfg80211_process_disassoc(struct wireless_dev *wdev,
103*4882a593Smuzhiyun 				      const u8 *buf, size_t len)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
106*4882a593Smuzhiyun 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buf;
107*4882a593Smuzhiyun 	const u8 *bssid = mgmt->bssid;
108*4882a593Smuzhiyun 	u16 reason_code = le16_to_cpu(mgmt->u.disassoc.reason_code);
109*4882a593Smuzhiyun 	bool from_ap = !ether_addr_equal(mgmt->sa, wdev->netdev->dev_addr);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	nl80211_send_disassoc(rdev, wdev->netdev, buf, len, GFP_KERNEL);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	if (WARN_ON(!wdev->current_bss ||
114*4882a593Smuzhiyun 		    !ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
115*4882a593Smuzhiyun 		return;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	__cfg80211_disconnected(wdev->netdev, NULL, 0, reason_code, from_ap);
118*4882a593Smuzhiyun 	cfg80211_sme_disassoc(wdev);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun 
cfg80211_rx_mlme_mgmt(struct net_device * dev,const u8 * buf,size_t len)121*4882a593Smuzhiyun void cfg80211_rx_mlme_mgmt(struct net_device *dev, const u8 *buf, size_t len)
122*4882a593Smuzhiyun {
123*4882a593Smuzhiyun 	struct wireless_dev *wdev = dev->ieee80211_ptr;
124*4882a593Smuzhiyun 	struct ieee80211_mgmt *mgmt = (void *)buf;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	ASSERT_WDEV_LOCK(wdev);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	trace_cfg80211_rx_mlme_mgmt(dev, buf, len);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	if (WARN_ON(len < 2))
131*4882a593Smuzhiyun 		return;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	if (ieee80211_is_auth(mgmt->frame_control))
134*4882a593Smuzhiyun 		cfg80211_process_auth(wdev, buf, len);
135*4882a593Smuzhiyun 	else if (ieee80211_is_deauth(mgmt->frame_control))
136*4882a593Smuzhiyun 		cfg80211_process_deauth(wdev, buf, len);
137*4882a593Smuzhiyun 	else if (ieee80211_is_disassoc(mgmt->frame_control))
138*4882a593Smuzhiyun 		cfg80211_process_disassoc(wdev, buf, len);
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun EXPORT_SYMBOL(cfg80211_rx_mlme_mgmt);
141*4882a593Smuzhiyun 
cfg80211_auth_timeout(struct net_device * dev,const u8 * addr)142*4882a593Smuzhiyun void cfg80211_auth_timeout(struct net_device *dev, const u8 *addr)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	struct wireless_dev *wdev = dev->ieee80211_ptr;
145*4882a593Smuzhiyun 	struct wiphy *wiphy = wdev->wiphy;
146*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	trace_cfg80211_send_auth_timeout(dev, addr);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	nl80211_send_auth_timeout(rdev, dev, addr, GFP_KERNEL);
151*4882a593Smuzhiyun 	cfg80211_sme_auth_timeout(wdev);
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun EXPORT_SYMBOL(cfg80211_auth_timeout);
154*4882a593Smuzhiyun 
cfg80211_assoc_timeout(struct net_device * dev,struct cfg80211_bss * bss)155*4882a593Smuzhiyun void cfg80211_assoc_timeout(struct net_device *dev, struct cfg80211_bss *bss)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	struct wireless_dev *wdev = dev->ieee80211_ptr;
158*4882a593Smuzhiyun 	struct wiphy *wiphy = wdev->wiphy;
159*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	trace_cfg80211_send_assoc_timeout(dev, bss->bssid);
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	nl80211_send_assoc_timeout(rdev, dev, bss->bssid, GFP_KERNEL);
164*4882a593Smuzhiyun 	cfg80211_sme_assoc_timeout(wdev);
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	cfg80211_unhold_bss(bss_from_pub(bss));
167*4882a593Smuzhiyun 	cfg80211_put_bss(wiphy, bss);
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun EXPORT_SYMBOL(cfg80211_assoc_timeout);
170*4882a593Smuzhiyun 
cfg80211_abandon_assoc(struct net_device * dev,struct cfg80211_bss * bss)171*4882a593Smuzhiyun void cfg80211_abandon_assoc(struct net_device *dev, struct cfg80211_bss *bss)
172*4882a593Smuzhiyun {
173*4882a593Smuzhiyun 	struct wireless_dev *wdev = dev->ieee80211_ptr;
174*4882a593Smuzhiyun 	struct wiphy *wiphy = wdev->wiphy;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	cfg80211_sme_abandon_assoc(wdev);
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	cfg80211_unhold_bss(bss_from_pub(bss));
179*4882a593Smuzhiyun 	cfg80211_put_bss(wiphy, bss);
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun EXPORT_SYMBOL(cfg80211_abandon_assoc);
182*4882a593Smuzhiyun 
cfg80211_tx_mlme_mgmt(struct net_device * dev,const u8 * buf,size_t len)183*4882a593Smuzhiyun void cfg80211_tx_mlme_mgmt(struct net_device *dev, const u8 *buf, size_t len)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun 	struct wireless_dev *wdev = dev->ieee80211_ptr;
186*4882a593Smuzhiyun 	struct ieee80211_mgmt *mgmt = (void *)buf;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	ASSERT_WDEV_LOCK(wdev);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	trace_cfg80211_tx_mlme_mgmt(dev, buf, len);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	if (WARN_ON(len < 2))
193*4882a593Smuzhiyun 		return;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	if (ieee80211_is_deauth(mgmt->frame_control))
196*4882a593Smuzhiyun 		cfg80211_process_deauth(wdev, buf, len);
197*4882a593Smuzhiyun 	else
198*4882a593Smuzhiyun 		cfg80211_process_disassoc(wdev, buf, len);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun EXPORT_SYMBOL(cfg80211_tx_mlme_mgmt);
201*4882a593Smuzhiyun 
cfg80211_michael_mic_failure(struct net_device * dev,const u8 * addr,enum nl80211_key_type key_type,int key_id,const u8 * tsc,gfp_t gfp)202*4882a593Smuzhiyun void cfg80211_michael_mic_failure(struct net_device *dev, const u8 *addr,
203*4882a593Smuzhiyun 				  enum nl80211_key_type key_type, int key_id,
204*4882a593Smuzhiyun 				  const u8 *tsc, gfp_t gfp)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
207*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
208*4882a593Smuzhiyun #ifdef CONFIG_CFG80211_WEXT
209*4882a593Smuzhiyun 	union iwreq_data wrqu;
210*4882a593Smuzhiyun 	char *buf = kmalloc(128, gfp);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	if (buf) {
213*4882a593Smuzhiyun 		sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
214*4882a593Smuzhiyun 			"keyid=%d %scast addr=%pM)", key_id,
215*4882a593Smuzhiyun 			key_type == NL80211_KEYTYPE_GROUP ? "broad" : "uni",
216*4882a593Smuzhiyun 			addr);
217*4882a593Smuzhiyun 		memset(&wrqu, 0, sizeof(wrqu));
218*4882a593Smuzhiyun 		wrqu.data.length = strlen(buf);
219*4882a593Smuzhiyun 		wireless_send_event(dev, IWEVCUSTOM, &wrqu, buf);
220*4882a593Smuzhiyun 		kfree(buf);
221*4882a593Smuzhiyun 	}
222*4882a593Smuzhiyun #endif
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	trace_cfg80211_michael_mic_failure(dev, addr, key_type, key_id, tsc);
225*4882a593Smuzhiyun 	nl80211_michael_mic_failure(rdev, dev, addr, key_type, key_id, tsc, gfp);
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun EXPORT_SYMBOL(cfg80211_michael_mic_failure);
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun /* some MLME handling for userspace SME */
cfg80211_mlme_auth(struct cfg80211_registered_device * rdev,struct net_device * dev,struct ieee80211_channel * chan,enum nl80211_auth_type auth_type,const u8 * bssid,const u8 * ssid,int ssid_len,const u8 * ie,int ie_len,const u8 * key,int key_len,int key_idx,const u8 * auth_data,int auth_data_len)230*4882a593Smuzhiyun int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
231*4882a593Smuzhiyun 		       struct net_device *dev,
232*4882a593Smuzhiyun 		       struct ieee80211_channel *chan,
233*4882a593Smuzhiyun 		       enum nl80211_auth_type auth_type,
234*4882a593Smuzhiyun 		       const u8 *bssid,
235*4882a593Smuzhiyun 		       const u8 *ssid, int ssid_len,
236*4882a593Smuzhiyun 		       const u8 *ie, int ie_len,
237*4882a593Smuzhiyun 		       const u8 *key, int key_len, int key_idx,
238*4882a593Smuzhiyun 		       const u8 *auth_data, int auth_data_len)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun 	struct wireless_dev *wdev = dev->ieee80211_ptr;
241*4882a593Smuzhiyun 	struct cfg80211_auth_request req = {
242*4882a593Smuzhiyun 		.ie = ie,
243*4882a593Smuzhiyun 		.ie_len = ie_len,
244*4882a593Smuzhiyun 		.auth_data = auth_data,
245*4882a593Smuzhiyun 		.auth_data_len = auth_data_len,
246*4882a593Smuzhiyun 		.auth_type = auth_type,
247*4882a593Smuzhiyun 		.key = key,
248*4882a593Smuzhiyun 		.key_len = key_len,
249*4882a593Smuzhiyun 		.key_idx = key_idx,
250*4882a593Smuzhiyun 	};
251*4882a593Smuzhiyun 	int err;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	ASSERT_WDEV_LOCK(wdev);
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	if (auth_type == NL80211_AUTHTYPE_SHARED_KEY)
256*4882a593Smuzhiyun 		if (!key || !key_len || key_idx < 0 || key_idx > 3)
257*4882a593Smuzhiyun 			return -EINVAL;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	if (wdev->current_bss &&
260*4882a593Smuzhiyun 	    ether_addr_equal(bssid, wdev->current_bss->pub.bssid))
261*4882a593Smuzhiyun 		return -EALREADY;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
264*4882a593Smuzhiyun 				   IEEE80211_BSS_TYPE_ESS,
265*4882a593Smuzhiyun 				   IEEE80211_PRIVACY_ANY);
266*4882a593Smuzhiyun 	if (!req.bss)
267*4882a593Smuzhiyun 		return -ENOENT;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	err = rdev_auth(rdev, dev, &req);
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	cfg80211_put_bss(&rdev->wiphy, req.bss);
272*4882a593Smuzhiyun 	return err;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun /*  Do a logical ht_capa &= ht_capa_mask.  */
cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap * ht_capa,const struct ieee80211_ht_cap * ht_capa_mask)276*4882a593Smuzhiyun void cfg80211_oper_and_ht_capa(struct ieee80211_ht_cap *ht_capa,
277*4882a593Smuzhiyun 			       const struct ieee80211_ht_cap *ht_capa_mask)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun 	int i;
280*4882a593Smuzhiyun 	u8 *p1, *p2;
281*4882a593Smuzhiyun 	if (!ht_capa_mask) {
282*4882a593Smuzhiyun 		memset(ht_capa, 0, sizeof(*ht_capa));
283*4882a593Smuzhiyun 		return;
284*4882a593Smuzhiyun 	}
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	p1 = (u8*)(ht_capa);
287*4882a593Smuzhiyun 	p2 = (u8*)(ht_capa_mask);
288*4882a593Smuzhiyun 	for (i = 0; i < sizeof(*ht_capa); i++)
289*4882a593Smuzhiyun 		p1[i] &= p2[i];
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun /*  Do a logical vht_capa &= vht_capa_mask.  */
cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap * vht_capa,const struct ieee80211_vht_cap * vht_capa_mask)293*4882a593Smuzhiyun void cfg80211_oper_and_vht_capa(struct ieee80211_vht_cap *vht_capa,
294*4882a593Smuzhiyun 				const struct ieee80211_vht_cap *vht_capa_mask)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun 	int i;
297*4882a593Smuzhiyun 	u8 *p1, *p2;
298*4882a593Smuzhiyun 	if (!vht_capa_mask) {
299*4882a593Smuzhiyun 		memset(vht_capa, 0, sizeof(*vht_capa));
300*4882a593Smuzhiyun 		return;
301*4882a593Smuzhiyun 	}
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	p1 = (u8*)(vht_capa);
304*4882a593Smuzhiyun 	p2 = (u8*)(vht_capa_mask);
305*4882a593Smuzhiyun 	for (i = 0; i < sizeof(*vht_capa); i++)
306*4882a593Smuzhiyun 		p1[i] &= p2[i];
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun 
cfg80211_mlme_assoc(struct cfg80211_registered_device * rdev,struct net_device * dev,struct ieee80211_channel * chan,const u8 * bssid,const u8 * ssid,int ssid_len,struct cfg80211_assoc_request * req)309*4882a593Smuzhiyun int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
310*4882a593Smuzhiyun 			struct net_device *dev,
311*4882a593Smuzhiyun 			struct ieee80211_channel *chan,
312*4882a593Smuzhiyun 			const u8 *bssid,
313*4882a593Smuzhiyun 			const u8 *ssid, int ssid_len,
314*4882a593Smuzhiyun 			struct cfg80211_assoc_request *req)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun 	struct wireless_dev *wdev = dev->ieee80211_ptr;
317*4882a593Smuzhiyun 	int err;
318*4882a593Smuzhiyun 
319*4882a593Smuzhiyun 	ASSERT_WDEV_LOCK(wdev);
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun 	if (wdev->current_bss &&
322*4882a593Smuzhiyun 	    (!req->prev_bssid || !ether_addr_equal(wdev->current_bss->pub.bssid,
323*4882a593Smuzhiyun 						   req->prev_bssid)))
324*4882a593Smuzhiyun 		return -EALREADY;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	cfg80211_oper_and_ht_capa(&req->ht_capa_mask,
327*4882a593Smuzhiyun 				  rdev->wiphy.ht_capa_mod_mask);
328*4882a593Smuzhiyun 	cfg80211_oper_and_vht_capa(&req->vht_capa_mask,
329*4882a593Smuzhiyun 				   rdev->wiphy.vht_capa_mod_mask);
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	req->bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
332*4882a593Smuzhiyun 				    IEEE80211_BSS_TYPE_ESS,
333*4882a593Smuzhiyun 				    IEEE80211_PRIVACY_ANY);
334*4882a593Smuzhiyun 	if (!req->bss)
335*4882a593Smuzhiyun 		return -ENOENT;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	err = rdev_assoc(rdev, dev, req);
338*4882a593Smuzhiyun 	if (!err)
339*4882a593Smuzhiyun 		cfg80211_hold_bss(bss_from_pub(req->bss));
340*4882a593Smuzhiyun 	else
341*4882a593Smuzhiyun 		cfg80211_put_bss(&rdev->wiphy, req->bss);
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	return err;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun 
cfg80211_mlme_deauth(struct cfg80211_registered_device * rdev,struct net_device * dev,const u8 * bssid,const u8 * ie,int ie_len,u16 reason,bool local_state_change)346*4882a593Smuzhiyun int cfg80211_mlme_deauth(struct cfg80211_registered_device *rdev,
347*4882a593Smuzhiyun 			 struct net_device *dev, const u8 *bssid,
348*4882a593Smuzhiyun 			 const u8 *ie, int ie_len, u16 reason,
349*4882a593Smuzhiyun 			 bool local_state_change)
350*4882a593Smuzhiyun {
351*4882a593Smuzhiyun 	struct wireless_dev *wdev = dev->ieee80211_ptr;
352*4882a593Smuzhiyun 	struct cfg80211_deauth_request req = {
353*4882a593Smuzhiyun 		.bssid = bssid,
354*4882a593Smuzhiyun 		.reason_code = reason,
355*4882a593Smuzhiyun 		.ie = ie,
356*4882a593Smuzhiyun 		.ie_len = ie_len,
357*4882a593Smuzhiyun 		.local_state_change = local_state_change,
358*4882a593Smuzhiyun 	};
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	ASSERT_WDEV_LOCK(wdev);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	if (local_state_change &&
363*4882a593Smuzhiyun 	    (!wdev->current_bss ||
364*4882a593Smuzhiyun 	     !ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
365*4882a593Smuzhiyun 		return 0;
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	if (ether_addr_equal(wdev->disconnect_bssid, bssid) ||
368*4882a593Smuzhiyun 	    (wdev->current_bss &&
369*4882a593Smuzhiyun 	     ether_addr_equal(wdev->current_bss->pub.bssid, bssid)))
370*4882a593Smuzhiyun 		wdev->conn_owner_nlportid = 0;
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	return rdev_deauth(rdev, dev, &req);
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun 
cfg80211_mlme_disassoc(struct cfg80211_registered_device * rdev,struct net_device * dev,const u8 * bssid,const u8 * ie,int ie_len,u16 reason,bool local_state_change)375*4882a593Smuzhiyun int cfg80211_mlme_disassoc(struct cfg80211_registered_device *rdev,
376*4882a593Smuzhiyun 			   struct net_device *dev, const u8 *bssid,
377*4882a593Smuzhiyun 			   const u8 *ie, int ie_len, u16 reason,
378*4882a593Smuzhiyun 			   bool local_state_change)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun 	struct wireless_dev *wdev = dev->ieee80211_ptr;
381*4882a593Smuzhiyun 	struct cfg80211_disassoc_request req = {
382*4882a593Smuzhiyun 		.reason_code = reason,
383*4882a593Smuzhiyun 		.local_state_change = local_state_change,
384*4882a593Smuzhiyun 		.ie = ie,
385*4882a593Smuzhiyun 		.ie_len = ie_len,
386*4882a593Smuzhiyun 	};
387*4882a593Smuzhiyun 	int err;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	ASSERT_WDEV_LOCK(wdev);
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	if (!wdev->current_bss)
392*4882a593Smuzhiyun 		return -ENOTCONN;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	if (ether_addr_equal(wdev->current_bss->pub.bssid, bssid))
395*4882a593Smuzhiyun 		req.bss = &wdev->current_bss->pub;
396*4882a593Smuzhiyun 	else
397*4882a593Smuzhiyun 		return -ENOTCONN;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun 	err = rdev_disassoc(rdev, dev, &req);
400*4882a593Smuzhiyun 	if (err)
401*4882a593Smuzhiyun 		return err;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	/* driver should have reported the disassoc */
404*4882a593Smuzhiyun 	WARN_ON(wdev->current_bss);
405*4882a593Smuzhiyun 	return 0;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun 
cfg80211_mlme_down(struct cfg80211_registered_device * rdev,struct net_device * dev)408*4882a593Smuzhiyun void cfg80211_mlme_down(struct cfg80211_registered_device *rdev,
409*4882a593Smuzhiyun 			struct net_device *dev)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun 	struct wireless_dev *wdev = dev->ieee80211_ptr;
412*4882a593Smuzhiyun 	u8 bssid[ETH_ALEN];
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	ASSERT_WDEV_LOCK(wdev);
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	if (!rdev->ops->deauth)
417*4882a593Smuzhiyun 		return;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	if (!wdev->current_bss)
420*4882a593Smuzhiyun 		return;
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
423*4882a593Smuzhiyun 	cfg80211_mlme_deauth(rdev, dev, bssid, NULL, 0,
424*4882a593Smuzhiyun 			     WLAN_REASON_DEAUTH_LEAVING, false);
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun struct cfg80211_mgmt_registration {
428*4882a593Smuzhiyun 	struct list_head list;
429*4882a593Smuzhiyun 	struct wireless_dev *wdev;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	u32 nlportid;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	int match_len;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	__le16 frame_type;
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	bool multicast_rx;
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	u8 match[];
440*4882a593Smuzhiyun };
441*4882a593Smuzhiyun 
cfg80211_mgmt_registrations_update(struct wireless_dev * wdev)442*4882a593Smuzhiyun static void cfg80211_mgmt_registrations_update(struct wireless_dev *wdev)
443*4882a593Smuzhiyun {
444*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
445*4882a593Smuzhiyun 	struct wireless_dev *tmp;
446*4882a593Smuzhiyun 	struct cfg80211_mgmt_registration *reg;
447*4882a593Smuzhiyun 	struct mgmt_frame_regs upd = {};
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 	ASSERT_RTNL();
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	spin_lock_bh(&rdev->mgmt_registrations_lock);
452*4882a593Smuzhiyun 	if (!wdev->mgmt_registrations_need_update) {
453*4882a593Smuzhiyun 		spin_unlock_bh(&rdev->mgmt_registrations_lock);
454*4882a593Smuzhiyun 		return;
455*4882a593Smuzhiyun 	}
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	rcu_read_lock();
458*4882a593Smuzhiyun 	list_for_each_entry_rcu(tmp, &rdev->wiphy.wdev_list, list) {
459*4882a593Smuzhiyun 		list_for_each_entry(reg, &tmp->mgmt_registrations, list) {
460*4882a593Smuzhiyun 			u32 mask = BIT(le16_to_cpu(reg->frame_type) >> 4);
461*4882a593Smuzhiyun 			u32 mcast_mask = 0;
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 			if (reg->multicast_rx)
464*4882a593Smuzhiyun 				mcast_mask = mask;
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 			upd.global_stypes |= mask;
467*4882a593Smuzhiyun 			upd.global_mcast_stypes |= mcast_mask;
468*4882a593Smuzhiyun 
469*4882a593Smuzhiyun 			if (tmp == wdev) {
470*4882a593Smuzhiyun 				upd.interface_stypes |= mask;
471*4882a593Smuzhiyun 				upd.interface_mcast_stypes |= mcast_mask;
472*4882a593Smuzhiyun 			}
473*4882a593Smuzhiyun 		}
474*4882a593Smuzhiyun 	}
475*4882a593Smuzhiyun 	rcu_read_unlock();
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	wdev->mgmt_registrations_need_update = 0;
478*4882a593Smuzhiyun 	spin_unlock_bh(&rdev->mgmt_registrations_lock);
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 	rdev_update_mgmt_frame_registrations(rdev, wdev, &upd);
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun 
cfg80211_mgmt_registrations_update_wk(struct work_struct * wk)483*4882a593Smuzhiyun void cfg80211_mgmt_registrations_update_wk(struct work_struct *wk)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev;
486*4882a593Smuzhiyun 	struct wireless_dev *wdev;
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	rdev = container_of(wk, struct cfg80211_registered_device,
489*4882a593Smuzhiyun 			    mgmt_registrations_update_wk);
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	rtnl_lock();
492*4882a593Smuzhiyun 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list)
493*4882a593Smuzhiyun 		cfg80211_mgmt_registrations_update(wdev);
494*4882a593Smuzhiyun 	rtnl_unlock();
495*4882a593Smuzhiyun }
496*4882a593Smuzhiyun 
cfg80211_mlme_register_mgmt(struct wireless_dev * wdev,u32 snd_portid,u16 frame_type,const u8 * match_data,int match_len,bool multicast_rx,struct netlink_ext_ack * extack)497*4882a593Smuzhiyun int cfg80211_mlme_register_mgmt(struct wireless_dev *wdev, u32 snd_portid,
498*4882a593Smuzhiyun 				u16 frame_type, const u8 *match_data,
499*4882a593Smuzhiyun 				int match_len, bool multicast_rx,
500*4882a593Smuzhiyun 				struct netlink_ext_ack *extack)
501*4882a593Smuzhiyun {
502*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
503*4882a593Smuzhiyun 	struct cfg80211_mgmt_registration *reg, *nreg;
504*4882a593Smuzhiyun 	int err = 0;
505*4882a593Smuzhiyun 	u16 mgmt_type;
506*4882a593Smuzhiyun 	bool update_multicast = false;
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	if (!wdev->wiphy->mgmt_stypes)
509*4882a593Smuzhiyun 		return -EOPNOTSUPP;
510*4882a593Smuzhiyun 
511*4882a593Smuzhiyun 	if ((frame_type & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT) {
512*4882a593Smuzhiyun 		NL_SET_ERR_MSG(extack, "frame type not management");
513*4882a593Smuzhiyun 		return -EINVAL;
514*4882a593Smuzhiyun 	}
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	if (frame_type & ~(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) {
517*4882a593Smuzhiyun 		NL_SET_ERR_MSG(extack, "Invalid frame type");
518*4882a593Smuzhiyun 		return -EINVAL;
519*4882a593Smuzhiyun 	}
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
522*4882a593Smuzhiyun 	if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].rx & BIT(mgmt_type))) {
523*4882a593Smuzhiyun 		NL_SET_ERR_MSG(extack,
524*4882a593Smuzhiyun 			       "Registration to specific type not supported");
525*4882a593Smuzhiyun 		return -EINVAL;
526*4882a593Smuzhiyun 	}
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	/*
529*4882a593Smuzhiyun 	 * To support Pre Association Security Negotiation (PASN), registration
530*4882a593Smuzhiyun 	 * for authentication frames should be supported. However, as some
531*4882a593Smuzhiyun 	 * versions of the user space daemons wrongly register to all types of
532*4882a593Smuzhiyun 	 * authentication frames (which might result in unexpected behavior)
533*4882a593Smuzhiyun 	 * allow such registration if the request is for a specific
534*4882a593Smuzhiyun 	 * authentication algorithm number.
535*4882a593Smuzhiyun 	 */
536*4882a593Smuzhiyun 	if (wdev->iftype == NL80211_IFTYPE_STATION &&
537*4882a593Smuzhiyun 	    (frame_type & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_AUTH &&
538*4882a593Smuzhiyun 	    !(match_data && match_len >= 2)) {
539*4882a593Smuzhiyun 		NL_SET_ERR_MSG(extack,
540*4882a593Smuzhiyun 			       "Authentication algorithm number required");
541*4882a593Smuzhiyun 		return -EINVAL;
542*4882a593Smuzhiyun 	}
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	nreg = kzalloc(sizeof(*reg) + match_len, GFP_KERNEL);
545*4882a593Smuzhiyun 	if (!nreg)
546*4882a593Smuzhiyun 		return -ENOMEM;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	spin_lock_bh(&rdev->mgmt_registrations_lock);
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
551*4882a593Smuzhiyun 		int mlen = min(match_len, reg->match_len);
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 		if (frame_type != le16_to_cpu(reg->frame_type))
554*4882a593Smuzhiyun 			continue;
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 		if (memcmp(reg->match, match_data, mlen) == 0) {
557*4882a593Smuzhiyun 			if (reg->multicast_rx != multicast_rx) {
558*4882a593Smuzhiyun 				update_multicast = true;
559*4882a593Smuzhiyun 				reg->multicast_rx = multicast_rx;
560*4882a593Smuzhiyun 				break;
561*4882a593Smuzhiyun 			}
562*4882a593Smuzhiyun 			NL_SET_ERR_MSG(extack, "Match already configured");
563*4882a593Smuzhiyun 			err = -EALREADY;
564*4882a593Smuzhiyun 			break;
565*4882a593Smuzhiyun 		}
566*4882a593Smuzhiyun 	}
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	if (err)
569*4882a593Smuzhiyun 		goto out;
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun 	if (update_multicast) {
572*4882a593Smuzhiyun 		kfree(nreg);
573*4882a593Smuzhiyun 	} else {
574*4882a593Smuzhiyun 		memcpy(nreg->match, match_data, match_len);
575*4882a593Smuzhiyun 		nreg->match_len = match_len;
576*4882a593Smuzhiyun 		nreg->nlportid = snd_portid;
577*4882a593Smuzhiyun 		nreg->frame_type = cpu_to_le16(frame_type);
578*4882a593Smuzhiyun 		nreg->wdev = wdev;
579*4882a593Smuzhiyun 		nreg->multicast_rx = multicast_rx;
580*4882a593Smuzhiyun 		list_add(&nreg->list, &wdev->mgmt_registrations);
581*4882a593Smuzhiyun 	}
582*4882a593Smuzhiyun 	wdev->mgmt_registrations_need_update = 1;
583*4882a593Smuzhiyun 	spin_unlock_bh(&rdev->mgmt_registrations_lock);
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	cfg80211_mgmt_registrations_update(wdev);
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	return 0;
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun  out:
590*4882a593Smuzhiyun 	kfree(nreg);
591*4882a593Smuzhiyun 	spin_unlock_bh(&rdev->mgmt_registrations_lock);
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 	return err;
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun 
cfg80211_mlme_unregister_socket(struct wireless_dev * wdev,u32 nlportid)596*4882a593Smuzhiyun void cfg80211_mlme_unregister_socket(struct wireless_dev *wdev, u32 nlportid)
597*4882a593Smuzhiyun {
598*4882a593Smuzhiyun 	struct wiphy *wiphy = wdev->wiphy;
599*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
600*4882a593Smuzhiyun 	struct cfg80211_mgmt_registration *reg, *tmp;
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	spin_lock_bh(&rdev->mgmt_registrations_lock);
603*4882a593Smuzhiyun 
604*4882a593Smuzhiyun 	list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
605*4882a593Smuzhiyun 		if (reg->nlportid != nlportid)
606*4882a593Smuzhiyun 			continue;
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 		list_del(&reg->list);
609*4882a593Smuzhiyun 		kfree(reg);
610*4882a593Smuzhiyun 
611*4882a593Smuzhiyun 		wdev->mgmt_registrations_need_update = 1;
612*4882a593Smuzhiyun 		schedule_work(&rdev->mgmt_registrations_update_wk);
613*4882a593Smuzhiyun 	}
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	spin_unlock_bh(&rdev->mgmt_registrations_lock);
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun 	if (nlportid && rdev->crit_proto_nlportid == nlportid) {
618*4882a593Smuzhiyun 		rdev->crit_proto_nlportid = 0;
619*4882a593Smuzhiyun 		rdev_crit_proto_stop(rdev, wdev);
620*4882a593Smuzhiyun 	}
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	if (nlportid == wdev->ap_unexpected_nlportid)
623*4882a593Smuzhiyun 		wdev->ap_unexpected_nlportid = 0;
624*4882a593Smuzhiyun }
625*4882a593Smuzhiyun 
cfg80211_mlme_purge_registrations(struct wireless_dev * wdev)626*4882a593Smuzhiyun void cfg80211_mlme_purge_registrations(struct wireless_dev *wdev)
627*4882a593Smuzhiyun {
628*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
629*4882a593Smuzhiyun 	struct cfg80211_mgmt_registration *reg, *tmp;
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun 	spin_lock_bh(&rdev->mgmt_registrations_lock);
632*4882a593Smuzhiyun 	list_for_each_entry_safe(reg, tmp, &wdev->mgmt_registrations, list) {
633*4882a593Smuzhiyun 		list_del(&reg->list);
634*4882a593Smuzhiyun 		kfree(reg);
635*4882a593Smuzhiyun 	}
636*4882a593Smuzhiyun 	wdev->mgmt_registrations_need_update = 1;
637*4882a593Smuzhiyun 	spin_unlock_bh(&rdev->mgmt_registrations_lock);
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	cfg80211_mgmt_registrations_update(wdev);
640*4882a593Smuzhiyun }
641*4882a593Smuzhiyun 
cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device * rdev,struct wireless_dev * wdev,struct cfg80211_mgmt_tx_params * params,u64 * cookie)642*4882a593Smuzhiyun int cfg80211_mlme_mgmt_tx(struct cfg80211_registered_device *rdev,
643*4882a593Smuzhiyun 			  struct wireless_dev *wdev,
644*4882a593Smuzhiyun 			  struct cfg80211_mgmt_tx_params *params, u64 *cookie)
645*4882a593Smuzhiyun {
646*4882a593Smuzhiyun 	const struct ieee80211_mgmt *mgmt;
647*4882a593Smuzhiyun 	u16 stype;
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun 	if (!wdev->wiphy->mgmt_stypes)
650*4882a593Smuzhiyun 		return -EOPNOTSUPP;
651*4882a593Smuzhiyun 
652*4882a593Smuzhiyun 	if (!rdev->ops->mgmt_tx)
653*4882a593Smuzhiyun 		return -EOPNOTSUPP;
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 	if (params->len < 24 + 1)
656*4882a593Smuzhiyun 		return -EINVAL;
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 	mgmt = (const struct ieee80211_mgmt *)params->buf;
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	if (!ieee80211_is_mgmt(mgmt->frame_control))
661*4882a593Smuzhiyun 		return -EINVAL;
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
664*4882a593Smuzhiyun 	if (!(wdev->wiphy->mgmt_stypes[wdev->iftype].tx & BIT(stype >> 4)))
665*4882a593Smuzhiyun 		return -EINVAL;
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 	if (ieee80211_is_action(mgmt->frame_control) &&
668*4882a593Smuzhiyun 	    mgmt->u.action.category != WLAN_CATEGORY_PUBLIC) {
669*4882a593Smuzhiyun 		int err = 0;
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 		wdev_lock(wdev);
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 		switch (wdev->iftype) {
674*4882a593Smuzhiyun 		case NL80211_IFTYPE_ADHOC:
675*4882a593Smuzhiyun 		case NL80211_IFTYPE_STATION:
676*4882a593Smuzhiyun 		case NL80211_IFTYPE_P2P_CLIENT:
677*4882a593Smuzhiyun 			if (!wdev->current_bss) {
678*4882a593Smuzhiyun 				err = -ENOTCONN;
679*4882a593Smuzhiyun 				break;
680*4882a593Smuzhiyun 			}
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 			if (!ether_addr_equal(wdev->current_bss->pub.bssid,
683*4882a593Smuzhiyun 					      mgmt->bssid)) {
684*4882a593Smuzhiyun 				err = -ENOTCONN;
685*4882a593Smuzhiyun 				break;
686*4882a593Smuzhiyun 			}
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 			/*
689*4882a593Smuzhiyun 			 * check for IBSS DA must be done by driver as
690*4882a593Smuzhiyun 			 * cfg80211 doesn't track the stations
691*4882a593Smuzhiyun 			 */
692*4882a593Smuzhiyun 			if (wdev->iftype == NL80211_IFTYPE_ADHOC)
693*4882a593Smuzhiyun 				break;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 			/* for station, check that DA is the AP */
696*4882a593Smuzhiyun 			if (!ether_addr_equal(wdev->current_bss->pub.bssid,
697*4882a593Smuzhiyun 					      mgmt->da)) {
698*4882a593Smuzhiyun 				err = -ENOTCONN;
699*4882a593Smuzhiyun 				break;
700*4882a593Smuzhiyun 			}
701*4882a593Smuzhiyun 			break;
702*4882a593Smuzhiyun 		case NL80211_IFTYPE_AP:
703*4882a593Smuzhiyun 		case NL80211_IFTYPE_P2P_GO:
704*4882a593Smuzhiyun 		case NL80211_IFTYPE_AP_VLAN:
705*4882a593Smuzhiyun 			if (!ether_addr_equal(mgmt->bssid, wdev_address(wdev)))
706*4882a593Smuzhiyun 				err = -EINVAL;
707*4882a593Smuzhiyun 			break;
708*4882a593Smuzhiyun 		case NL80211_IFTYPE_MESH_POINT:
709*4882a593Smuzhiyun 			if (!ether_addr_equal(mgmt->sa, mgmt->bssid)) {
710*4882a593Smuzhiyun 				err = -EINVAL;
711*4882a593Smuzhiyun 				break;
712*4882a593Smuzhiyun 			}
713*4882a593Smuzhiyun 			/*
714*4882a593Smuzhiyun 			 * check for mesh DA must be done by driver as
715*4882a593Smuzhiyun 			 * cfg80211 doesn't track the stations
716*4882a593Smuzhiyun 			 */
717*4882a593Smuzhiyun 			break;
718*4882a593Smuzhiyun 		case NL80211_IFTYPE_P2P_DEVICE:
719*4882a593Smuzhiyun 			/*
720*4882a593Smuzhiyun 			 * fall through, P2P device only supports
721*4882a593Smuzhiyun 			 * public action frames
722*4882a593Smuzhiyun 			 */
723*4882a593Smuzhiyun 		case NL80211_IFTYPE_NAN:
724*4882a593Smuzhiyun 		default:
725*4882a593Smuzhiyun 			err = -EOPNOTSUPP;
726*4882a593Smuzhiyun 			break;
727*4882a593Smuzhiyun 		}
728*4882a593Smuzhiyun 		wdev_unlock(wdev);
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 		if (err)
731*4882a593Smuzhiyun 			return err;
732*4882a593Smuzhiyun 	}
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	if (!ether_addr_equal(mgmt->sa, wdev_address(wdev))) {
735*4882a593Smuzhiyun 		/* Allow random TA to be used with Public Action frames if the
736*4882a593Smuzhiyun 		 * driver has indicated support for this. Otherwise, only allow
737*4882a593Smuzhiyun 		 * the local address to be used.
738*4882a593Smuzhiyun 		 */
739*4882a593Smuzhiyun 		if (!ieee80211_is_action(mgmt->frame_control) ||
740*4882a593Smuzhiyun 		    mgmt->u.action.category != WLAN_CATEGORY_PUBLIC)
741*4882a593Smuzhiyun 			return -EINVAL;
742*4882a593Smuzhiyun 		if (!wdev->current_bss &&
743*4882a593Smuzhiyun 		    !wiphy_ext_feature_isset(
744*4882a593Smuzhiyun 			    &rdev->wiphy,
745*4882a593Smuzhiyun 			    NL80211_EXT_FEATURE_MGMT_TX_RANDOM_TA))
746*4882a593Smuzhiyun 			return -EINVAL;
747*4882a593Smuzhiyun 		if (wdev->current_bss &&
748*4882a593Smuzhiyun 		    !wiphy_ext_feature_isset(
749*4882a593Smuzhiyun 			    &rdev->wiphy,
750*4882a593Smuzhiyun 			    NL80211_EXT_FEATURE_MGMT_TX_RANDOM_TA_CONNECTED))
751*4882a593Smuzhiyun 			return -EINVAL;
752*4882a593Smuzhiyun 	}
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	/* Transmit the Action frame as requested by user space */
755*4882a593Smuzhiyun 	return rdev_mgmt_tx(rdev, wdev, params, cookie);
756*4882a593Smuzhiyun }
757*4882a593Smuzhiyun 
cfg80211_rx_mgmt_khz(struct wireless_dev * wdev,int freq,int sig_dbm,const u8 * buf,size_t len,u32 flags)758*4882a593Smuzhiyun bool cfg80211_rx_mgmt_khz(struct wireless_dev *wdev, int freq, int sig_dbm,
759*4882a593Smuzhiyun 			  const u8 *buf, size_t len, u32 flags)
760*4882a593Smuzhiyun {
761*4882a593Smuzhiyun 	struct wiphy *wiphy = wdev->wiphy;
762*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
763*4882a593Smuzhiyun 	struct cfg80211_mgmt_registration *reg;
764*4882a593Smuzhiyun 	const struct ieee80211_txrx_stypes *stypes =
765*4882a593Smuzhiyun 		&wiphy->mgmt_stypes[wdev->iftype];
766*4882a593Smuzhiyun 	struct ieee80211_mgmt *mgmt = (void *)buf;
767*4882a593Smuzhiyun 	const u8 *data;
768*4882a593Smuzhiyun 	int data_len;
769*4882a593Smuzhiyun 	bool result = false;
770*4882a593Smuzhiyun 	__le16 ftype = mgmt->frame_control &
771*4882a593Smuzhiyun 		cpu_to_le16(IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE);
772*4882a593Smuzhiyun 	u16 stype;
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	trace_cfg80211_rx_mgmt(wdev, freq, sig_dbm);
775*4882a593Smuzhiyun 	stype = (le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE) >> 4;
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	if (!(stypes->rx & BIT(stype))) {
778*4882a593Smuzhiyun 		trace_cfg80211_return_bool(false);
779*4882a593Smuzhiyun 		return false;
780*4882a593Smuzhiyun 	}
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	data = buf + ieee80211_hdrlen(mgmt->frame_control);
783*4882a593Smuzhiyun 	data_len = len - ieee80211_hdrlen(mgmt->frame_control);
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun 	spin_lock_bh(&rdev->mgmt_registrations_lock);
786*4882a593Smuzhiyun 
787*4882a593Smuzhiyun 	list_for_each_entry(reg, &wdev->mgmt_registrations, list) {
788*4882a593Smuzhiyun 		if (reg->frame_type != ftype)
789*4882a593Smuzhiyun 			continue;
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 		if (reg->match_len > data_len)
792*4882a593Smuzhiyun 			continue;
793*4882a593Smuzhiyun 
794*4882a593Smuzhiyun 		if (memcmp(reg->match, data, reg->match_len))
795*4882a593Smuzhiyun 			continue;
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 		/* found match! */
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 		/* Indicate the received Action frame to user space */
800*4882a593Smuzhiyun 		if (nl80211_send_mgmt(rdev, wdev, reg->nlportid,
801*4882a593Smuzhiyun 				      freq, sig_dbm,
802*4882a593Smuzhiyun 				      buf, len, flags, GFP_ATOMIC))
803*4882a593Smuzhiyun 			continue;
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 		result = true;
806*4882a593Smuzhiyun 		break;
807*4882a593Smuzhiyun 	}
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	spin_unlock_bh(&rdev->mgmt_registrations_lock);
810*4882a593Smuzhiyun 
811*4882a593Smuzhiyun 	trace_cfg80211_return_bool(result);
812*4882a593Smuzhiyun 	return result;
813*4882a593Smuzhiyun }
814*4882a593Smuzhiyun EXPORT_SYMBOL(cfg80211_rx_mgmt_khz);
815*4882a593Smuzhiyun 
cfg80211_sched_dfs_chan_update(struct cfg80211_registered_device * rdev)816*4882a593Smuzhiyun void cfg80211_sched_dfs_chan_update(struct cfg80211_registered_device *rdev)
817*4882a593Smuzhiyun {
818*4882a593Smuzhiyun 	cancel_delayed_work(&rdev->dfs_update_channels_wk);
819*4882a593Smuzhiyun 	queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk, 0);
820*4882a593Smuzhiyun }
821*4882a593Smuzhiyun 
cfg80211_dfs_channels_update_work(struct work_struct * work)822*4882a593Smuzhiyun void cfg80211_dfs_channels_update_work(struct work_struct *work)
823*4882a593Smuzhiyun {
824*4882a593Smuzhiyun 	struct delayed_work *delayed_work = to_delayed_work(work);
825*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev;
826*4882a593Smuzhiyun 	struct cfg80211_chan_def chandef;
827*4882a593Smuzhiyun 	struct ieee80211_supported_band *sband;
828*4882a593Smuzhiyun 	struct ieee80211_channel *c;
829*4882a593Smuzhiyun 	struct wiphy *wiphy;
830*4882a593Smuzhiyun 	bool check_again = false;
831*4882a593Smuzhiyun 	unsigned long timeout, next_time = 0;
832*4882a593Smuzhiyun 	unsigned long time_dfs_update;
833*4882a593Smuzhiyun 	enum nl80211_radar_event radar_event;
834*4882a593Smuzhiyun 	int bandid, i;
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 	rdev = container_of(delayed_work, struct cfg80211_registered_device,
837*4882a593Smuzhiyun 			    dfs_update_channels_wk);
838*4882a593Smuzhiyun 	wiphy = &rdev->wiphy;
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 	rtnl_lock();
841*4882a593Smuzhiyun 	for (bandid = 0; bandid < NUM_NL80211_BANDS; bandid++) {
842*4882a593Smuzhiyun 		sband = wiphy->bands[bandid];
843*4882a593Smuzhiyun 		if (!sband)
844*4882a593Smuzhiyun 			continue;
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 		for (i = 0; i < sband->n_channels; i++) {
847*4882a593Smuzhiyun 			c = &sband->channels[i];
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 			if (!(c->flags & IEEE80211_CHAN_RADAR))
850*4882a593Smuzhiyun 				continue;
851*4882a593Smuzhiyun 
852*4882a593Smuzhiyun 			if (c->dfs_state != NL80211_DFS_UNAVAILABLE &&
853*4882a593Smuzhiyun 			    c->dfs_state != NL80211_DFS_AVAILABLE)
854*4882a593Smuzhiyun 				continue;
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 			if (c->dfs_state == NL80211_DFS_UNAVAILABLE) {
857*4882a593Smuzhiyun 				time_dfs_update = IEEE80211_DFS_MIN_NOP_TIME_MS;
858*4882a593Smuzhiyun 				radar_event = NL80211_RADAR_NOP_FINISHED;
859*4882a593Smuzhiyun 			} else {
860*4882a593Smuzhiyun 				if (regulatory_pre_cac_allowed(wiphy) ||
861*4882a593Smuzhiyun 				    cfg80211_any_wiphy_oper_chan(wiphy, c))
862*4882a593Smuzhiyun 					continue;
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 				time_dfs_update = REG_PRE_CAC_EXPIRY_GRACE_MS;
865*4882a593Smuzhiyun 				radar_event = NL80211_RADAR_PRE_CAC_EXPIRED;
866*4882a593Smuzhiyun 			}
867*4882a593Smuzhiyun 
868*4882a593Smuzhiyun 			timeout = c->dfs_state_entered +
869*4882a593Smuzhiyun 				  msecs_to_jiffies(time_dfs_update);
870*4882a593Smuzhiyun 
871*4882a593Smuzhiyun 			if (time_after_eq(jiffies, timeout)) {
872*4882a593Smuzhiyun 				c->dfs_state = NL80211_DFS_USABLE;
873*4882a593Smuzhiyun 				c->dfs_state_entered = jiffies;
874*4882a593Smuzhiyun 
875*4882a593Smuzhiyun 				cfg80211_chandef_create(&chandef, c,
876*4882a593Smuzhiyun 							NL80211_CHAN_NO_HT);
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 				nl80211_radar_notify(rdev, &chandef,
879*4882a593Smuzhiyun 						     radar_event, NULL,
880*4882a593Smuzhiyun 						     GFP_ATOMIC);
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 				regulatory_propagate_dfs_state(wiphy, &chandef,
883*4882a593Smuzhiyun 							       c->dfs_state,
884*4882a593Smuzhiyun 							       radar_event);
885*4882a593Smuzhiyun 				continue;
886*4882a593Smuzhiyun 			}
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 			if (!check_again)
889*4882a593Smuzhiyun 				next_time = timeout - jiffies;
890*4882a593Smuzhiyun 			else
891*4882a593Smuzhiyun 				next_time = min(next_time, timeout - jiffies);
892*4882a593Smuzhiyun 			check_again = true;
893*4882a593Smuzhiyun 		}
894*4882a593Smuzhiyun 	}
895*4882a593Smuzhiyun 	rtnl_unlock();
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	/* reschedule if there are other channels waiting to be cleared again */
898*4882a593Smuzhiyun 	if (check_again)
899*4882a593Smuzhiyun 		queue_delayed_work(cfg80211_wq, &rdev->dfs_update_channels_wk,
900*4882a593Smuzhiyun 				   next_time);
901*4882a593Smuzhiyun }
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun 
cfg80211_radar_event(struct wiphy * wiphy,struct cfg80211_chan_def * chandef,gfp_t gfp)904*4882a593Smuzhiyun void cfg80211_radar_event(struct wiphy *wiphy,
905*4882a593Smuzhiyun 			  struct cfg80211_chan_def *chandef,
906*4882a593Smuzhiyun 			  gfp_t gfp)
907*4882a593Smuzhiyun {
908*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun 	trace_cfg80211_radar_event(wiphy, chandef);
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun 	/* only set the chandef supplied channel to unavailable, in
913*4882a593Smuzhiyun 	 * case the radar is detected on only one of multiple channels
914*4882a593Smuzhiyun 	 * spanned by the chandef.
915*4882a593Smuzhiyun 	 */
916*4882a593Smuzhiyun 	cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_UNAVAILABLE);
917*4882a593Smuzhiyun 
918*4882a593Smuzhiyun 	cfg80211_sched_dfs_chan_update(rdev);
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun 	nl80211_radar_notify(rdev, chandef, NL80211_RADAR_DETECTED, NULL, gfp);
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	memcpy(&rdev->radar_chandef, chandef, sizeof(struct cfg80211_chan_def));
923*4882a593Smuzhiyun 	queue_work(cfg80211_wq, &rdev->propagate_radar_detect_wk);
924*4882a593Smuzhiyun }
925*4882a593Smuzhiyun EXPORT_SYMBOL(cfg80211_radar_event);
926*4882a593Smuzhiyun 
cfg80211_cac_event(struct net_device * netdev,const struct cfg80211_chan_def * chandef,enum nl80211_radar_event event,gfp_t gfp)927*4882a593Smuzhiyun void cfg80211_cac_event(struct net_device *netdev,
928*4882a593Smuzhiyun 			const struct cfg80211_chan_def *chandef,
929*4882a593Smuzhiyun 			enum nl80211_radar_event event, gfp_t gfp)
930*4882a593Smuzhiyun {
931*4882a593Smuzhiyun 	struct wireless_dev *wdev = netdev->ieee80211_ptr;
932*4882a593Smuzhiyun 	struct wiphy *wiphy = wdev->wiphy;
933*4882a593Smuzhiyun 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
934*4882a593Smuzhiyun 	unsigned long timeout;
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	trace_cfg80211_cac_event(netdev, event);
937*4882a593Smuzhiyun 
938*4882a593Smuzhiyun 	if (WARN_ON(!wdev->cac_started && event != NL80211_RADAR_CAC_STARTED))
939*4882a593Smuzhiyun 		return;
940*4882a593Smuzhiyun 
941*4882a593Smuzhiyun 	if (WARN_ON(!wdev->chandef.chan))
942*4882a593Smuzhiyun 		return;
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun 	switch (event) {
945*4882a593Smuzhiyun 	case NL80211_RADAR_CAC_FINISHED:
946*4882a593Smuzhiyun 		timeout = wdev->cac_start_time +
947*4882a593Smuzhiyun 			  msecs_to_jiffies(wdev->cac_time_ms);
948*4882a593Smuzhiyun 		WARN_ON(!time_after_eq(jiffies, timeout));
949*4882a593Smuzhiyun 		cfg80211_set_dfs_state(wiphy, chandef, NL80211_DFS_AVAILABLE);
950*4882a593Smuzhiyun 		memcpy(&rdev->cac_done_chandef, chandef,
951*4882a593Smuzhiyun 		       sizeof(struct cfg80211_chan_def));
952*4882a593Smuzhiyun 		queue_work(cfg80211_wq, &rdev->propagate_cac_done_wk);
953*4882a593Smuzhiyun 		cfg80211_sched_dfs_chan_update(rdev);
954*4882a593Smuzhiyun 		fallthrough;
955*4882a593Smuzhiyun 	case NL80211_RADAR_CAC_ABORTED:
956*4882a593Smuzhiyun 		wdev->cac_started = false;
957*4882a593Smuzhiyun 		break;
958*4882a593Smuzhiyun 	case NL80211_RADAR_CAC_STARTED:
959*4882a593Smuzhiyun 		wdev->cac_started = true;
960*4882a593Smuzhiyun 		break;
961*4882a593Smuzhiyun 	default:
962*4882a593Smuzhiyun 		WARN_ON(1);
963*4882a593Smuzhiyun 		return;
964*4882a593Smuzhiyun 	}
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun 	nl80211_radar_notify(rdev, chandef, event, netdev, gfp);
967*4882a593Smuzhiyun }
968*4882a593Smuzhiyun EXPORT_SYMBOL(cfg80211_cac_event);
969