xref: /linux/drivers/staging/rtl8723bs/os_dep/ioctl_cfg80211.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 
8 #include <linux/etherdevice.h>
9 #include <drv_types.h>
10 #include <linux/jiffies.h>
11 
12 #include <rtw_wifi_regd.h>
13 
14 #define RTW_MAX_MGMT_TX_CNT (8)
15 
16 #define RTW_SCAN_IE_LEN_MAX      2304
17 #define RTW_MAX_REMAIN_ON_CHANNEL_DURATION 5000 /* ms */
18 #define RTW_MAX_NUM_PMKIDS 4
19 
20 static const u32 rtw_cipher_suites[] = {
21 	WLAN_CIPHER_SUITE_WEP40,
22 	WLAN_CIPHER_SUITE_WEP104,
23 	WLAN_CIPHER_SUITE_TKIP,
24 	WLAN_CIPHER_SUITE_CCMP,
25 	WLAN_CIPHER_SUITE_AES_CMAC,
26 };
27 
28 #define RATETAB_ENT(_rate, _rateid, _flags) \
29 	{								\
30 		.bitrate	= (_rate),				\
31 		.hw_value	= (_rateid),				\
32 		.flags		= (_flags),				\
33 	}
34 
35 #define CHAN2G(_channel, _freq, _flags) {			\
36 	.band			= NL80211_BAND_2GHZ,		\
37 	.center_freq		= (_freq),			\
38 	.hw_value		= (_channel),			\
39 	.flags			= (_flags),			\
40 	.max_antenna_gain	= 0,				\
41 	.max_power		= 30,				\
42 }
43 
44 /* if wowlan is not supported, kernel generate a disconnect at each suspend
45  * cf: /net/wireless/sysfs.c, so register a stub wowlan.
46  * Moreover wowlan has to be enabled via a the nl80211_set_wowlan callback.
47  * (from user space, e.g. iw phy0 wowlan enable)
48  */
49 static __maybe_unused const struct wiphy_wowlan_support wowlan_stub = {
50 	.flags = WIPHY_WOWLAN_ANY,
51 	.n_patterns = 0,
52 	.pattern_max_len = 0,
53 	.pattern_min_len = 0,
54 	.max_pkt_offset = 0,
55 };
56 
57 static struct ieee80211_rate rtw_rates[] = {
58 	RATETAB_ENT(10,  0x1,   0),
59 	RATETAB_ENT(20,  0x2,   0),
60 	RATETAB_ENT(55,  0x4,   0),
61 	RATETAB_ENT(110, 0x8,   0),
62 	RATETAB_ENT(60,  0x10,  0),
63 	RATETAB_ENT(90,  0x20,  0),
64 	RATETAB_ENT(120, 0x40,  0),
65 	RATETAB_ENT(180, 0x80,  0),
66 	RATETAB_ENT(240, 0x100, 0),
67 	RATETAB_ENT(360, 0x200, 0),
68 	RATETAB_ENT(480, 0x400, 0),
69 	RATETAB_ENT(540, 0x800, 0),
70 };
71 
72 #define rtw_g_rates		(rtw_rates + 0)
73 #define RTW_G_RATES_NUM	12
74 
75 #define RTW_2G_CHANNELS_NUM 14
76 
77 static struct ieee80211_channel rtw_2ghz_channels[] = {
78 	CHAN2G(1, 2412, 0),
79 	CHAN2G(2, 2417, 0),
80 	CHAN2G(3, 2422, 0),
81 	CHAN2G(4, 2427, 0),
82 	CHAN2G(5, 2432, 0),
83 	CHAN2G(6, 2437, 0),
84 	CHAN2G(7, 2442, 0),
85 	CHAN2G(8, 2447, 0),
86 	CHAN2G(9, 2452, 0),
87 	CHAN2G(10, 2457, 0),
88 	CHAN2G(11, 2462, 0),
89 	CHAN2G(12, 2467, 0),
90 	CHAN2G(13, 2472, 0),
91 	CHAN2G(14, 2484, 0),
92 };
93 
rtw_2g_channels_init(struct ieee80211_channel * channels)94 static void rtw_2g_channels_init(struct ieee80211_channel *channels)
95 {
96 	memcpy((void *)channels, (void *)rtw_2ghz_channels,
97 	       sizeof(struct ieee80211_channel) * RTW_2G_CHANNELS_NUM
98 	);
99 }
100 
rtw_2g_rates_init(struct ieee80211_rate * rates)101 static void rtw_2g_rates_init(struct ieee80211_rate *rates)
102 {
103 	memcpy(rates, rtw_g_rates,
104 	       sizeof(struct ieee80211_rate) * RTW_G_RATES_NUM
105 	);
106 }
107 
rtw_spt_band_alloc(enum nl80211_band band)108 static struct ieee80211_supported_band *rtw_spt_band_alloc(
109 	enum nl80211_band band
110 	)
111 {
112 	struct ieee80211_supported_band *spt_band = NULL;
113 	int n_channels, n_bitrates;
114 
115 	if (band == NL80211_BAND_2GHZ) {
116 		n_channels = RTW_2G_CHANNELS_NUM;
117 		n_bitrates = RTW_G_RATES_NUM;
118 	} else {
119 		goto exit;
120 	}
121 
122 	spt_band = rtw_zmalloc(sizeof(struct ieee80211_supported_band) +
123 			       sizeof(struct ieee80211_channel) * n_channels +
124 			       sizeof(struct ieee80211_rate) * n_bitrates);
125 	if (!spt_band)
126 		goto exit;
127 
128 	spt_band->channels = (struct ieee80211_channel *)(((u8 *)spt_band) + sizeof(struct ieee80211_supported_band));
129 	spt_band->bitrates = (struct ieee80211_rate *)(((u8 *)spt_band->channels) + sizeof(struct ieee80211_channel) * n_channels);
130 	spt_band->band = band;
131 	spt_band->n_channels = n_channels;
132 	spt_band->n_bitrates = n_bitrates;
133 
134 	if (band == NL80211_BAND_2GHZ) {
135 		rtw_2g_channels_init(spt_band->channels);
136 		rtw_2g_rates_init(spt_band->bitrates);
137 	}
138 
139 	/* spt_band.ht_cap */
140 
141 exit:
142 
143 	return spt_band;
144 }
145 
146 static const struct ieee80211_txrx_stypes
147 rtw_cfg80211_default_mgmt_stypes[NUM_NL80211_IFTYPES] = {
148 	[NL80211_IFTYPE_ADHOC] = {
149 		.tx = 0xffff,
150 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4)
151 	},
152 	[NL80211_IFTYPE_STATION] = {
153 		.tx = 0xffff,
154 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
155 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
156 	},
157 	[NL80211_IFTYPE_AP] = {
158 		.tx = 0xffff,
159 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
160 		BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
161 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
162 		BIT(IEEE80211_STYPE_DISASSOC >> 4) |
163 		BIT(IEEE80211_STYPE_AUTH >> 4) |
164 		BIT(IEEE80211_STYPE_DEAUTH >> 4) |
165 		BIT(IEEE80211_STYPE_ACTION >> 4)
166 	},
167 	[NL80211_IFTYPE_AP_VLAN] = {
168 		/* copy AP */
169 		.tx = 0xffff,
170 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
171 		BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
172 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
173 		BIT(IEEE80211_STYPE_DISASSOC >> 4) |
174 		BIT(IEEE80211_STYPE_AUTH >> 4) |
175 		BIT(IEEE80211_STYPE_DEAUTH >> 4) |
176 		BIT(IEEE80211_STYPE_ACTION >> 4)
177 	},
178 	[NL80211_IFTYPE_P2P_CLIENT] = {
179 		.tx = 0xffff,
180 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
181 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
182 	},
183 	[NL80211_IFTYPE_P2P_GO] = {
184 		.tx = 0xffff,
185 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
186 		BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
187 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
188 		BIT(IEEE80211_STYPE_DISASSOC >> 4) |
189 		BIT(IEEE80211_STYPE_AUTH >> 4) |
190 		BIT(IEEE80211_STYPE_DEAUTH >> 4) |
191 		BIT(IEEE80211_STYPE_ACTION >> 4)
192 	},
193 };
194 
rtw_ieee80211_channel_to_frequency(int chan,int band)195 static int rtw_ieee80211_channel_to_frequency(int chan, int band)
196 {
197 	if (band == NL80211_BAND_2GHZ) {
198 		if (chan == 14)
199 			return 2484;
200 		else if (chan < 14)
201 			return 2407 + chan * 5;
202 	}
203 
204 	return 0; /* not supported */
205 }
206 
207 #define MAX_BSSINFO_LEN 1000
rtw_cfg80211_inform_bss(struct adapter * padapter,struct wlan_network * pnetwork)208 struct cfg80211_bss *rtw_cfg80211_inform_bss(struct adapter *padapter, struct wlan_network *pnetwork)
209 {
210 	struct ieee80211_channel *notify_channel;
211 	struct cfg80211_bss *bss = NULL;
212 	/* struct ieee80211_supported_band *band; */
213 	u16 channel;
214 	u32 freq;
215 	u64 notify_timestamp;
216 	s32 notify_signal;
217 	u8 *buf = NULL, *pbuf;
218 	size_t len, bssinf_len = 0;
219 	struct ieee80211_hdr *pwlanhdr;
220 	__le16 *fctrl;
221 
222 	struct wireless_dev *wdev = padapter->rtw_wdev;
223 	struct wiphy *wiphy = wdev->wiphy;
224 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
225 
226 	bssinf_len = pnetwork->network.ie_length + sizeof(struct ieee80211_hdr_3addr);
227 	if (bssinf_len > MAX_BSSINFO_LEN)
228 		goto exit;
229 
230 	{
231 		u16 wapi_len = 0;
232 
233 		if (rtw_get_wapi_ie(pnetwork->network.ies, pnetwork->network.ie_length, NULL, &wapi_len) > 0) {
234 			if (wapi_len > 0)
235 				goto exit;
236 		}
237 	}
238 
239 	/* To reduce PBC Overlap rate */
240 	/* spin_lock_bh(&pwdev_priv->scan_req_lock); */
241 	if (adapter_wdev_data(padapter)->scan_request) {
242 		u8 *psr = NULL, sr = 0;
243 		struct ndis_802_11_ssid *pssid = &pnetwork->network.ssid;
244 		struct cfg80211_scan_request *request = adapter_wdev_data(padapter)->scan_request;
245 		struct cfg80211_ssid *ssids = request->ssids;
246 		u32 wpsielen = 0;
247 		u8 *wpsie = NULL;
248 
249 		wpsie = rtw_get_wps_ie(pnetwork->network.ies + _FIXED_IE_LENGTH_, pnetwork->network.ie_length - _FIXED_IE_LENGTH_, NULL, &wpsielen);
250 
251 		if (wpsie && wpsielen > 0)
252 			psr = rtw_get_wps_attr_content(wpsie, wpsielen, WPS_ATTR_SELECTED_REGISTRAR, (u8 *)(&sr), NULL);
253 
254 		if (sr != 0) {
255 			/* it means under processing WPS */
256 			if (request->n_ssids == 1 && request->n_channels == 1) {
257 				if (ssids[0].ssid_len != 0 &&
258 				    (pssid->ssid_length != ssids[0].ssid_len ||
259 				     memcmp(pssid->ssid, ssids[0].ssid, ssids[0].ssid_len))) {
260 					if (psr)
261 						*psr = 0; /* clear sr */
262 				}
263 			}
264 		}
265 	}
266 	/* spin_unlock_bh(&pwdev_priv->scan_req_lock); */
267 
268 	channel = pnetwork->network.configuration.ds_config;
269 	freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
270 
271 	notify_channel = ieee80211_get_channel(wiphy, freq);
272 
273 	notify_timestamp = ktime_to_us(ktime_get_boottime());
274 
275 	/* We've set wiphy's signal_type as CFG80211_SIGNAL_TYPE_MBM: signal strength in mBm (100*dBm) */
276 	if (check_fwstate(pmlmepriv, _FW_LINKED) == true &&
277 	    is_same_network(&pmlmepriv->cur_network.network, &pnetwork->network, 0)) {
278 		notify_signal = 100 * translate_percentage_to_dbm(padapter->recvpriv.signal_strength);/* dbm */
279 	} else {
280 		notify_signal = 100 * translate_percentage_to_dbm(pnetwork->network.phy_info.signal_strength);/* dbm */
281 	}
282 
283 	buf = kzalloc(MAX_BSSINFO_LEN, GFP_ATOMIC);
284 	if (!buf)
285 		goto exit;
286 	pbuf = buf;
287 
288 	pwlanhdr = (struct ieee80211_hdr *)pbuf;
289 	fctrl = &(pwlanhdr->frame_control);
290 	*(fctrl) = 0;
291 
292 	SetSeqNum(pwlanhdr, 0/*pmlmeext->mgnt_seq*/);
293 	/* pmlmeext->mgnt_seq++; */
294 
295 	if (pnetwork->network.reserved[0] == 1) { /*  WIFI_BEACON */
296 		eth_broadcast_addr(pwlanhdr->addr1);
297 		SetFrameSubType(pbuf, WIFI_BEACON);
298 	} else {
299 		memcpy(pwlanhdr->addr1, myid(&(padapter->eeprompriv)), ETH_ALEN);
300 		SetFrameSubType(pbuf, WIFI_PROBERSP);
301 	}
302 
303 	memcpy(pwlanhdr->addr2, pnetwork->network.mac_address, ETH_ALEN);
304 	memcpy(pwlanhdr->addr3, pnetwork->network.mac_address, ETH_ALEN);
305 
306 	pbuf += sizeof(struct ieee80211_hdr_3addr);
307 	len = sizeof(struct ieee80211_hdr_3addr);
308 
309 	memcpy(pbuf, pnetwork->network.ies, pnetwork->network.ie_length);
310 	len += pnetwork->network.ie_length;
311 
312 	*((__le64 *)pbuf) = cpu_to_le64(notify_timestamp);
313 
314 	bss = cfg80211_inform_bss_frame(wiphy, notify_channel, (struct ieee80211_mgmt *)buf,
315 					len, notify_signal, GFP_ATOMIC);
316 
317 	if (unlikely(!bss))
318 		goto exit;
319 
320 	cfg80211_put_bss(wiphy, bss);
321 	kfree(buf);
322 
323 exit:
324 	return bss;
325 }
326 
327 /*
328  *	Check the given bss is valid by kernel API cfg80211_get_bss()
329  *	@padapter : the given adapter
330  *
331  *	return true if bss is valid,  false for not found.
332  */
rtw_cfg80211_check_bss(struct adapter * padapter)333 int rtw_cfg80211_check_bss(struct adapter *padapter)
334 {
335 	struct wlan_bssid_ex  *pnetwork = &(padapter->mlmeextpriv.mlmext_info.network);
336 	struct cfg80211_bss *bss = NULL;
337 	struct ieee80211_channel *notify_channel = NULL;
338 	u32 freq;
339 
340 	if (!(pnetwork) || !(padapter->rtw_wdev))
341 		return false;
342 
343 	freq = rtw_ieee80211_channel_to_frequency(pnetwork->configuration.ds_config, NL80211_BAND_2GHZ);
344 
345 	notify_channel = ieee80211_get_channel(padapter->rtw_wdev->wiphy, freq);
346 	bss = cfg80211_get_bss(padapter->rtw_wdev->wiphy, notify_channel,
347 			       pnetwork->mac_address, pnetwork->ssid.ssid,
348 			       pnetwork->ssid.ssid_length,
349 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
350 
351 	cfg80211_put_bss(padapter->rtw_wdev->wiphy, bss);
352 
353 	return	(bss != NULL);
354 }
355 
rtw_cfg80211_ibss_indicate_connect(struct adapter * padapter)356 void rtw_cfg80211_ibss_indicate_connect(struct adapter *padapter)
357 {
358 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
359 	struct wlan_network  *cur_network = &(pmlmepriv->cur_network);
360 	struct wireless_dev *pwdev = padapter->rtw_wdev;
361 	struct wiphy *wiphy = pwdev->wiphy;
362 	int freq = (int)cur_network->network.configuration.ds_config;
363 	struct ieee80211_channel *chan;
364 
365 	if (pwdev->iftype != NL80211_IFTYPE_ADHOC)
366 		return;
367 
368 	if (!rtw_cfg80211_check_bss(padapter)) {
369 		struct wlan_bssid_ex  *pnetwork = &(padapter->mlmeextpriv.mlmext_info.network);
370 		struct wlan_network *scanned = pmlmepriv->cur_network_scanned;
371 
372 		if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true) {
373 			memcpy(&cur_network->network, pnetwork, sizeof(struct wlan_bssid_ex));
374 			rtw_cfg80211_inform_bss(padapter, cur_network);
375 		} else {
376 			if (!scanned) {
377 				rtw_warn_on(1);
378 				return;
379 			}
380 			if (!memcmp(&(scanned->network.ssid), &(pnetwork->ssid), sizeof(struct ndis_802_11_ssid))
381 				&& !memcmp(scanned->network.mac_address, pnetwork->mac_address, sizeof(NDIS_802_11_MAC_ADDRESS))
382 			)
383 				rtw_cfg80211_inform_bss(padapter, scanned);
384 			else
385 				rtw_warn_on(1);
386 		}
387 
388 		if (!rtw_cfg80211_check_bss(padapter))
389 			netdev_dbg(padapter->pnetdev,
390 				   FUNC_ADPT_FMT " BSS not found !!\n",
391 				   FUNC_ADPT_ARG(padapter));
392 	}
393 	/* notify cfg80211 that device joined an IBSS */
394 	chan = ieee80211_get_channel(wiphy, freq);
395 	cfg80211_ibss_joined(padapter->pnetdev, cur_network->network.mac_address, chan, GFP_ATOMIC);
396 }
397 
rtw_cfg80211_indicate_connect(struct adapter * padapter)398 void rtw_cfg80211_indicate_connect(struct adapter *padapter)
399 {
400 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
401 	struct wlan_network  *cur_network = &(pmlmepriv->cur_network);
402 	struct wireless_dev *pwdev = padapter->rtw_wdev;
403 
404 	if (pwdev->iftype != NL80211_IFTYPE_STATION
405 		&& pwdev->iftype != NL80211_IFTYPE_P2P_CLIENT
406 	) {
407 		return;
408 	}
409 
410 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true)
411 		return;
412 
413 	{
414 		struct wlan_bssid_ex  *pnetwork = &(padapter->mlmeextpriv.mlmext_info.network);
415 		struct wlan_network *scanned = pmlmepriv->cur_network_scanned;
416 
417 		if (!scanned) {
418 			rtw_warn_on(1);
419 			goto check_bss;
420 		}
421 
422 		if (!memcmp(scanned->network.mac_address, pnetwork->mac_address, sizeof(NDIS_802_11_MAC_ADDRESS))
423 			&& !memcmp(&(scanned->network.ssid), &(pnetwork->ssid), sizeof(struct ndis_802_11_ssid))
424 		)
425 			rtw_cfg80211_inform_bss(padapter, scanned);
426 		else
427 			rtw_warn_on(1);
428 	}
429 
430 check_bss:
431 	if (!rtw_cfg80211_check_bss(padapter))
432 		netdev_dbg(padapter->pnetdev,
433 			   FUNC_ADPT_FMT " BSS not found !!\n",
434 			   FUNC_ADPT_ARG(padapter));
435 
436 	if (rtw_to_roam(padapter) > 0) {
437 		struct wiphy *wiphy = pwdev->wiphy;
438 		struct ieee80211_channel *notify_channel;
439 		u32 freq;
440 		u16 channel = cur_network->network.configuration.ds_config;
441 		struct cfg80211_roam_info roam_info = {};
442 
443 		freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
444 
445 		notify_channel = ieee80211_get_channel(wiphy, freq);
446 
447 		roam_info.links[0].channel = notify_channel;
448 		roam_info.links[0].bssid = cur_network->network.mac_address;
449 		roam_info.req_ie =
450 			pmlmepriv->assoc_req + sizeof(struct ieee80211_hdr_3addr) + 2;
451 		roam_info.req_ie_len =
452 			pmlmepriv->assoc_req_len - sizeof(struct ieee80211_hdr_3addr) - 2;
453 		roam_info.resp_ie =
454 			pmlmepriv->assoc_rsp + sizeof(struct ieee80211_hdr_3addr) + 6;
455 		roam_info.resp_ie_len =
456 			pmlmepriv->assoc_rsp_len - sizeof(struct ieee80211_hdr_3addr) - 6;
457 		cfg80211_roamed(padapter->pnetdev, &roam_info, GFP_ATOMIC);
458 	} else {
459 		cfg80211_connect_result(padapter->pnetdev, cur_network->network.mac_address
460 			, pmlmepriv->assoc_req + sizeof(struct ieee80211_hdr_3addr) + 2
461 			, pmlmepriv->assoc_req_len - sizeof(struct ieee80211_hdr_3addr) - 2
462 			, pmlmepriv->assoc_rsp + sizeof(struct ieee80211_hdr_3addr) + 6
463 			, pmlmepriv->assoc_rsp_len - sizeof(struct ieee80211_hdr_3addr) - 6
464 			, WLAN_STATUS_SUCCESS, GFP_ATOMIC);
465 	}
466 }
467 
rtw_cfg80211_indicate_disconnect(struct adapter * padapter)468 void rtw_cfg80211_indicate_disconnect(struct adapter *padapter)
469 {
470 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
471 	struct wireless_dev *pwdev = padapter->rtw_wdev;
472 
473 	if (pwdev->iftype != NL80211_IFTYPE_STATION
474 		&& pwdev->iftype != NL80211_IFTYPE_P2P_CLIENT
475 	) {
476 		return;
477 	}
478 
479 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true)
480 		return;
481 
482 	if (!padapter->mlmepriv.not_indic_disco) {
483 		if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
484 			cfg80211_disconnected(padapter->pnetdev, 0,
485 					      NULL, 0, true, GFP_ATOMIC);
486 		} else {
487 			cfg80211_connect_result(padapter->pnetdev, NULL, NULL, 0, NULL, 0,
488 						WLAN_STATUS_UNSPECIFIED_FAILURE, GFP_ATOMIC/*GFP_KERNEL*/);
489 		}
490 	}
491 }
492 
rtw_cfg80211_ap_set_encryption(struct net_device * dev,struct ieee_param * param,u32 param_len)493 static int rtw_cfg80211_ap_set_encryption(struct net_device *dev, struct ieee_param *param, u32 param_len)
494 {
495 	int ret = 0;
496 	u32 wep_key_idx, wep_key_len;
497 	struct sta_info *psta = NULL, *pbcmc_sta = NULL;
498 	struct adapter *padapter = rtw_netdev_priv(dev);
499 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
500 	struct security_priv *psecuritypriv =  &(padapter->securitypriv);
501 	struct sta_priv *pstapriv = &padapter->stapriv;
502 	char *grpkey = padapter->securitypriv.dot118021XGrpKey[param->u.crypt.idx].skey;
503 	char *txkey = padapter->securitypriv.dot118021XGrptxmickey[param->u.crypt.idx].skey;
504 	char *rxkey = padapter->securitypriv.dot118021XGrprxmickey[param->u.crypt.idx].skey;
505 
506 	param->u.crypt.err = 0;
507 	param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0';
508 
509 	if (param_len !=  sizeof(struct ieee_param) + param->u.crypt.key_len) {
510 		ret =  -EINVAL;
511 		goto exit;
512 	}
513 
514 	if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
515 	    param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
516 	    param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
517 		if (param->u.crypt.idx >= WEP_KEYS) {
518 			ret = -EINVAL;
519 			goto exit;
520 		}
521 	} else {
522 		psta = rtw_get_stainfo(pstapriv, param->sta_addr);
523 		if (!psta)
524 			/* ret = -EINVAL; */
525 			goto exit;
526 	}
527 
528 	if (strcmp(param->u.crypt.alg, "none") == 0 && !psta)
529 		goto exit;
530 
531 	if (strcmp(param->u.crypt.alg, "WEP") == 0 && !psta) {
532 		wep_key_idx = param->u.crypt.idx;
533 		wep_key_len = param->u.crypt.key_len;
534 
535 		if ((wep_key_idx >= WEP_KEYS) || (wep_key_len <= 0)) {
536 			ret = -EINVAL;
537 			goto exit;
538 		}
539 
540 		if (wep_key_len > 0)
541 			wep_key_len = wep_key_len <= 5 ? 5 : 13;
542 
543 		if (psecuritypriv->bWepDefaultKeyIdxSet == 0) {
544 			/* wep default key has not been set, so use this key index as default key. */
545 
546 			psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Auto;
547 			psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled;
548 			psecuritypriv->dot11PrivacyAlgrthm = _WEP40_;
549 			psecuritypriv->dot118021XGrpPrivacy = _WEP40_;
550 
551 			if (wep_key_len == 13) {
552 				psecuritypriv->dot11PrivacyAlgrthm = _WEP104_;
553 				psecuritypriv->dot118021XGrpPrivacy = _WEP104_;
554 			}
555 
556 			psecuritypriv->dot11PrivacyKeyIndex = wep_key_idx;
557 		}
558 
559 		memcpy(&(psecuritypriv->dot11DefKey[wep_key_idx].skey[0]), param->u.crypt.key, wep_key_len);
560 
561 		psecuritypriv->dot11DefKeylen[wep_key_idx] = wep_key_len;
562 
563 		rtw_ap_set_wep_key(padapter, param->u.crypt.key, wep_key_len, wep_key_idx, 1);
564 
565 		goto exit;
566 	}
567 
568 	/* group key */
569 	if (!psta && check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
570 		/* group key */
571 		if (param->u.crypt.set_tx == 0) {
572 			if (strcmp(param->u.crypt.alg, "WEP") == 0) {
573 				memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
574 
575 				psecuritypriv->dot118021XGrpPrivacy = _WEP40_;
576 				if (param->u.crypt.key_len == 13)
577 					psecuritypriv->dot118021XGrpPrivacy = _WEP104_;
578 
579 			} else if (strcmp(param->u.crypt.alg, "TKIP") == 0) {
580 				psecuritypriv->dot118021XGrpPrivacy = _TKIP_;
581 
582 				memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
583 
584 				/* DEBUG_ERR("set key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len); */
585 				/* set mic key */
586 				memcpy(txkey, &(param->u.crypt.key[16]), 8);
587 				memcpy(rxkey, &(param->u.crypt.key[24]), 8);
588 
589 				psecuritypriv->busetkipkey = true;
590 
591 			} else if (strcmp(param->u.crypt.alg, "CCMP") == 0) {
592 				psecuritypriv->dot118021XGrpPrivacy = _AES_;
593 
594 				memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
595 			} else {
596 				psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_;
597 			}
598 
599 			psecuritypriv->dot118021XGrpKeyid = param->u.crypt.idx;
600 
601 			psecuritypriv->binstallGrpkey = true;
602 
603 			psecuritypriv->dot11PrivacyAlgrthm = psecuritypriv->dot118021XGrpPrivacy;/*  */
604 
605 			rtw_ap_set_group_key(padapter, param->u.crypt.key, psecuritypriv->dot118021XGrpPrivacy, param->u.crypt.idx);
606 
607 			pbcmc_sta = rtw_get_bcmc_stainfo(padapter);
608 			if (pbcmc_sta) {
609 				pbcmc_sta->ieee8021x_blocked = false;
610 				pbcmc_sta->dot118021XPrivacy = psecuritypriv->dot118021XGrpPrivacy;/* rx will use bmc_sta's dot118021XPrivacy */
611 			}
612 		}
613 
614 		goto exit;
615 	}
616 
617 	if (psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_8021X && psta) { /*  psk/802_1x */
618 		if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
619 			if (param->u.crypt.set_tx == 1) { /* pairwise key */
620 				memcpy(psta->dot118021x_UncstKey.skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
621 
622 				if (strcmp(param->u.crypt.alg, "WEP") == 0) {
623 					psta->dot118021XPrivacy = _WEP40_;
624 					if (param->u.crypt.key_len == 13)
625 						psta->dot118021XPrivacy = _WEP104_;
626 				} else if (strcmp(param->u.crypt.alg, "TKIP") == 0) {
627 					psta->dot118021XPrivacy = _TKIP_;
628 
629 					/* DEBUG_ERR("set key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len); */
630 					/* set mic key */
631 					memcpy(psta->dot11tkiptxmickey.skey, &(param->u.crypt.key[16]), 8);
632 					memcpy(psta->dot11tkiprxmickey.skey, &(param->u.crypt.key[24]), 8);
633 
634 					psecuritypriv->busetkipkey = true;
635 
636 				} else if (strcmp(param->u.crypt.alg, "CCMP") == 0) {
637 					psta->dot118021XPrivacy = _AES_;
638 				} else {
639 					psta->dot118021XPrivacy = _NO_PRIVACY_;
640 				}
641 
642 				rtw_ap_set_pairwise_key(padapter, psta);
643 
644 				psta->ieee8021x_blocked = false;
645 
646 				psta->bpairwise_key_installed = true;
647 
648 			} else { /* group key??? */
649 				if (strcmp(param->u.crypt.alg, "WEP") == 0) {
650 					memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
651 
652 					psecuritypriv->dot118021XGrpPrivacy = _WEP40_;
653 					if (param->u.crypt.key_len == 13)
654 						psecuritypriv->dot118021XGrpPrivacy = _WEP104_;
655 				} else if (strcmp(param->u.crypt.alg, "TKIP") == 0) {
656 					psecuritypriv->dot118021XGrpPrivacy = _TKIP_;
657 
658 					memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
659 
660 					/* DEBUG_ERR("set key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len); */
661 					/* set mic key */
662 					memcpy(txkey, &(param->u.crypt.key[16]), 8);
663 					memcpy(rxkey, &(param->u.crypt.key[24]), 8);
664 
665 					psecuritypriv->busetkipkey = true;
666 
667 				} else if (strcmp(param->u.crypt.alg, "CCMP") == 0) {
668 					psecuritypriv->dot118021XGrpPrivacy = _AES_;
669 
670 					memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
671 				} else {
672 					psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_;
673 				}
674 
675 				psecuritypriv->dot118021XGrpKeyid = param->u.crypt.idx;
676 
677 				psecuritypriv->binstallGrpkey = true;
678 
679 				psecuritypriv->dot11PrivacyAlgrthm = psecuritypriv->dot118021XGrpPrivacy;/*  */
680 
681 				rtw_ap_set_group_key(padapter, param->u.crypt.key, psecuritypriv->dot118021XGrpPrivacy, param->u.crypt.idx);
682 
683 				pbcmc_sta = rtw_get_bcmc_stainfo(padapter);
684 				if (pbcmc_sta) {
685 					pbcmc_sta->ieee8021x_blocked = false;
686 					pbcmc_sta->dot118021XPrivacy = psecuritypriv->dot118021XGrpPrivacy;/* rx will use bmc_sta's dot118021XPrivacy */
687 				}
688 			}
689 		}
690 	}
691 
692 exit:
693 
694 	return ret;
695 }
696 
rtw_cfg80211_set_encryption(struct net_device * dev,struct ieee_param * param,u32 param_len)697 static int rtw_cfg80211_set_encryption(struct net_device *dev, struct ieee_param *param, u32 param_len)
698 {
699 	int ret = 0;
700 	u8 max_idx;
701 	u32 wep_key_idx, wep_key_len;
702 	struct adapter *padapter = rtw_netdev_priv(dev);
703 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
704 	struct security_priv *psecuritypriv = &padapter->securitypriv;
705 
706 	param->u.crypt.err = 0;
707 	param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0';
708 
709 	if (param_len < (u32)((u8 *)param->u.crypt.key - (u8 *)param) + param->u.crypt.key_len) {
710 		ret =  -EINVAL;
711 		goto exit;
712 	}
713 
714 	if (param->sta_addr[0] != 0xff || param->sta_addr[1] != 0xff ||
715 	    param->sta_addr[2] != 0xff || param->sta_addr[3] != 0xff ||
716 	    param->sta_addr[4] != 0xff || param->sta_addr[5] != 0xff) {
717 		ret = -EINVAL;
718 		goto exit;
719 	}
720 
721 	if (strcmp(param->u.crypt.alg, "WEP") == 0)
722 		max_idx = WEP_KEYS - 1;
723 	else
724 		max_idx = BIP_MAX_KEYID;
725 
726 	if (param->u.crypt.idx > max_idx) {
727 		netdev_err(dev, "Error crypt.idx %d > %d\n", param->u.crypt.idx, max_idx);
728 		ret = -EINVAL;
729 		goto exit;
730 	}
731 
732 	if (strcmp(param->u.crypt.alg, "WEP") == 0) {
733 		wep_key_idx = param->u.crypt.idx;
734 		wep_key_len = param->u.crypt.key_len;
735 
736 		if (wep_key_len <= 0) {
737 			ret = -EINVAL;
738 			goto exit;
739 		}
740 
741 		if (psecuritypriv->bWepDefaultKeyIdxSet == 0) {
742 			/* wep default key has not been set, so use this key index as default key. */
743 
744 			wep_key_len = wep_key_len <= 5 ? 5 : 13;
745 
746 			psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled;
747 			psecuritypriv->dot11PrivacyAlgrthm = _WEP40_;
748 			psecuritypriv->dot118021XGrpPrivacy = _WEP40_;
749 
750 			if (wep_key_len == 13) {
751 				psecuritypriv->dot11PrivacyAlgrthm = _WEP104_;
752 				psecuritypriv->dot118021XGrpPrivacy = _WEP104_;
753 			}
754 
755 			psecuritypriv->dot11PrivacyKeyIndex = wep_key_idx;
756 		}
757 
758 		memcpy(&(psecuritypriv->dot11DefKey[wep_key_idx].skey[0]), param->u.crypt.key, wep_key_len);
759 
760 		psecuritypriv->dot11DefKeylen[wep_key_idx] = wep_key_len;
761 
762 		rtw_set_key(padapter, psecuritypriv, wep_key_idx, 0, true);
763 
764 		goto exit;
765 	}
766 
767 	if (padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) { /*  802_1x */
768 		struct sta_info *psta, *pbcmc_sta;
769 		struct sta_priv *pstapriv = &padapter->stapriv;
770 
771 		if (check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_MP_STATE) == true) { /* sta mode */
772 			psta = rtw_get_stainfo(pstapriv, get_bssid(pmlmepriv));
773 			if (psta) {
774 				/* Jeff: don't disable ieee8021x_blocked while clearing key */
775 				if (strcmp(param->u.crypt.alg, "none") != 0)
776 					psta->ieee8021x_blocked = false;
777 
778 				if ((padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption2Enabled) ||
779 				    (padapter->securitypriv.ndisencryptstatus ==  Ndis802_11Encryption3Enabled)) {
780 					psta->dot118021XPrivacy = padapter->securitypriv.dot11PrivacyAlgrthm;
781 				}
782 
783 				if (param->u.crypt.set_tx == 1) { /* pairwise key */
784 
785 					memcpy(psta->dot118021x_UncstKey.skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
786 
787 					if (strcmp(param->u.crypt.alg, "TKIP") == 0) { /* set mic key */
788 						/* DEBUG_ERR(("\nset key length :param->u.crypt.key_len =%d\n", param->u.crypt.key_len)); */
789 						memcpy(psta->dot11tkiptxmickey.skey, &(param->u.crypt.key[16]), 8);
790 						memcpy(psta->dot11tkiprxmickey.skey, &(param->u.crypt.key[24]), 8);
791 
792 						padapter->securitypriv.busetkipkey = false;
793 						/* _set_timer(&padapter->securitypriv.tkip_timer, 50); */
794 					}
795 
796 					rtw_setstakey_cmd(padapter, psta, true, true);
797 				} else { /* group key */
798 					if (strcmp(param->u.crypt.alg, "TKIP") == 0 || strcmp(param->u.crypt.alg, "CCMP") == 0) {
799 						memcpy(padapter->securitypriv.dot118021XGrpKey[param->u.crypt.idx].skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
800 						memcpy(padapter->securitypriv.dot118021XGrptxmickey[param->u.crypt.idx].skey, &(param->u.crypt.key[16]), 8);
801 						memcpy(padapter->securitypriv.dot118021XGrprxmickey[param->u.crypt.idx].skey, &(param->u.crypt.key[24]), 8);
802 						padapter->securitypriv.binstallGrpkey = true;
803 
804 						padapter->securitypriv.dot118021XGrpKeyid = param->u.crypt.idx;
805 						rtw_set_key(padapter, &padapter->securitypriv, param->u.crypt.idx, 1, true);
806 					} else if (strcmp(param->u.crypt.alg, "BIP") == 0) {
807 						/* save the IGTK key, length 16 bytes */
808 						memcpy(padapter->securitypriv.dot11wBIPKey[param->u.crypt.idx].skey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
809 						/*
810 						for (no = 0;no<16;no++)
811 							printk(" %02x ", padapter->securitypriv.dot11wBIPKey[param->u.crypt.idx].skey[no]);
812 						*/
813 						padapter->securitypriv.dot11wBIPKeyid = param->u.crypt.idx;
814 						padapter->securitypriv.binstallBIPkey = true;
815 					}
816 				}
817 			}
818 
819 			pbcmc_sta = rtw_get_bcmc_stainfo(padapter);
820 			if (!pbcmc_sta) {
821 				/* DEBUG_ERR(("Set OID_802_11_ADD_KEY: bcmc stainfo is null\n")); */
822 			} else {
823 				/* Jeff: don't disable ieee8021x_blocked while clearing key */
824 				if (strcmp(param->u.crypt.alg, "none") != 0)
825 					pbcmc_sta->ieee8021x_blocked = false;
826 
827 				if ((padapter->securitypriv.ndisencryptstatus == Ndis802_11Encryption2Enabled) ||
828 				    (padapter->securitypriv.ndisencryptstatus ==  Ndis802_11Encryption3Enabled)) {
829 					pbcmc_sta->dot118021XPrivacy = padapter->securitypriv.dot11PrivacyAlgrthm;
830 				}
831 			}
832 		} else if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) { /* adhoc mode */
833 		}
834 	}
835 
836 exit:
837 
838 	return ret;
839 }
840 
cfg80211_rtw_add_key(struct wiphy * wiphy,struct net_device * ndev,int link_id,u8 key_index,bool pairwise,const u8 * mac_addr,struct key_params * params)841 static int cfg80211_rtw_add_key(struct wiphy *wiphy, struct net_device *ndev,
842 				int link_id, u8 key_index, bool pairwise,
843 				const u8 *mac_addr, struct key_params *params)
844 {
845 	char *alg_name;
846 	u32 param_len;
847 	struct ieee_param *param = NULL;
848 	int ret = 0;
849 	struct adapter *padapter = rtw_netdev_priv(ndev);
850 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
851 
852 	param_len = sizeof(struct ieee_param) + params->key_len;
853 	param = rtw_malloc(param_len);
854 	if (!param)
855 		return -1;
856 
857 	memset(param, 0, param_len);
858 
859 	param->cmd = IEEE_CMD_SET_ENCRYPTION;
860 	eth_broadcast_addr(param->sta_addr);
861 
862 	switch (params->cipher) {
863 	case IW_AUTH_CIPHER_NONE:
864 		/* todo: remove key */
865 		/* remove = 1; */
866 		alg_name = "none";
867 		break;
868 	case WLAN_CIPHER_SUITE_WEP40:
869 	case WLAN_CIPHER_SUITE_WEP104:
870 		alg_name = "WEP";
871 		break;
872 	case WLAN_CIPHER_SUITE_TKIP:
873 		alg_name = "TKIP";
874 		break;
875 	case WLAN_CIPHER_SUITE_CCMP:
876 		alg_name = "CCMP";
877 		break;
878 	case WLAN_CIPHER_SUITE_AES_CMAC:
879 		alg_name = "BIP";
880 		break;
881 	default:
882 		ret = -ENOTSUPP;
883 		goto addkey_end;
884 	}
885 
886 	strscpy(param->u.crypt.alg, alg_name);
887 
888 	if (!mac_addr || is_broadcast_ether_addr(mac_addr))
889 		param->u.crypt.set_tx = 0; /* for wpa/wpa2 group key */
890 	else
891 		param->u.crypt.set_tx = 1; /* for wpa/wpa2 pairwise key */
892 
893 	param->u.crypt.idx = key_index;
894 
895 	if (params->seq_len && params->seq)
896 		memcpy(param->u.crypt.seq, (u8 *)params->seq, params->seq_len);
897 
898 	if (params->key_len && params->key) {
899 		param->u.crypt.key_len = params->key_len;
900 		memcpy(param->u.crypt.key, (u8 *)params->key, params->key_len);
901 	}
902 
903 	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) {
904 		ret =  rtw_cfg80211_set_encryption(ndev, param, param_len);
905 	} else if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
906 		if (mac_addr)
907 			memcpy(param->sta_addr, (void *)mac_addr, ETH_ALEN);
908 
909 		ret = rtw_cfg80211_ap_set_encryption(ndev, param, param_len);
910 	} else if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true
911 		|| check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true) {
912 		ret =  rtw_cfg80211_set_encryption(ndev, param, param_len);
913 	}
914 
915 addkey_end:
916 	kfree(param);
917 
918 	return ret;
919 }
920 
cfg80211_rtw_get_key(struct wiphy * wiphy,struct net_device * ndev,int link_id,u8 key_index,bool pairwise,const u8 * mac_addr,void * cookie,void (* callback)(void * cookie,struct key_params *))921 static int cfg80211_rtw_get_key(struct wiphy *wiphy, struct net_device *ndev,
922 				int link_id, u8 key_index, bool pairwise,
923 				const u8 *mac_addr, void *cookie,
924 				void (*callback)(void *cookie,
925 						 struct key_params*))
926 {
927 	return 0;
928 }
929 
cfg80211_rtw_del_key(struct wiphy * wiphy,struct net_device * ndev,int link_id,u8 key_index,bool pairwise,const u8 * mac_addr)930 static int cfg80211_rtw_del_key(struct wiphy *wiphy, struct net_device *ndev,
931 				int link_id, u8 key_index, bool pairwise,
932 				const u8 *mac_addr)
933 {
934 	struct adapter *padapter = rtw_netdev_priv(ndev);
935 	struct security_priv *psecuritypriv = &padapter->securitypriv;
936 
937 	if (key_index == psecuritypriv->dot11PrivacyKeyIndex) {
938 		/* clear the flag of wep default key set. */
939 		psecuritypriv->bWepDefaultKeyIdxSet = 0;
940 	}
941 
942 	return 0;
943 }
944 
cfg80211_rtw_set_default_key(struct wiphy * wiphy,struct net_device * ndev,int link_id,u8 key_index,bool unicast,bool multicast)945 static int cfg80211_rtw_set_default_key(struct wiphy *wiphy,
946 					struct net_device *ndev, int link_id,
947 					u8 key_index, bool unicast,
948 					bool multicast)
949 {
950 	struct adapter *padapter = rtw_netdev_priv(ndev);
951 	struct security_priv *psecuritypriv = &padapter->securitypriv;
952 
953 	if ((key_index < WEP_KEYS) && ((psecuritypriv->dot11PrivacyAlgrthm == _WEP40_) || (psecuritypriv->dot11PrivacyAlgrthm == _WEP104_))) { /* set wep default key */
954 		psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled;
955 
956 		psecuritypriv->dot11PrivacyKeyIndex = key_index;
957 
958 		psecuritypriv->dot11PrivacyAlgrthm = _WEP40_;
959 		psecuritypriv->dot118021XGrpPrivacy = _WEP40_;
960 		if (psecuritypriv->dot11DefKeylen[key_index] == 13) {
961 			psecuritypriv->dot11PrivacyAlgrthm = _WEP104_;
962 			psecuritypriv->dot118021XGrpPrivacy = _WEP104_;
963 		}
964 
965 		psecuritypriv->bWepDefaultKeyIdxSet = 1; /* set the flag to represent that wep default key has been set */
966 	}
967 
968 	return 0;
969 }
970 
cfg80211_rtw_get_station(struct wiphy * wiphy,struct net_device * ndev,const u8 * mac,struct station_info * sinfo)971 static int cfg80211_rtw_get_station(struct wiphy *wiphy,
972 				    struct net_device *ndev,
973 				const u8 *mac,
974 				struct station_info *sinfo)
975 {
976 	int ret = 0;
977 	struct adapter *padapter = rtw_netdev_priv(ndev);
978 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
979 	struct sta_info *psta = NULL;
980 	struct sta_priv *pstapriv = &padapter->stapriv;
981 
982 	sinfo->filled = 0;
983 
984 	if (!mac) {
985 		ret = -ENOENT;
986 		goto exit;
987 	}
988 
989 	psta = rtw_get_stainfo(pstapriv, (u8 *)mac);
990 	if (!psta) {
991 		ret = -ENOENT;
992 		goto exit;
993 	}
994 
995 	/* for infra./P2PClient mode */
996 	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)
997 		&& check_fwstate(pmlmepriv, _FW_LINKED)) {
998 		struct wlan_network  *cur_network = &(pmlmepriv->cur_network);
999 
1000 		if (memcmp((u8 *)mac, cur_network->network.mac_address, ETH_ALEN)) {
1001 			ret = -ENOENT;
1002 			goto exit;
1003 		}
1004 
1005 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
1006 		sinfo->signal = translate_percentage_to_dbm(padapter->recvpriv.signal_strength);
1007 
1008 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
1009 		sinfo->txrate.legacy = rtw_get_cur_max_rate(padapter);
1010 
1011 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
1012 		sinfo->rx_packets = sta_rx_data_pkts(psta);
1013 
1014 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
1015 		sinfo->tx_packets = psta->sta_stats.tx_pkts;
1016 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
1017 	}
1018 
1019 	/* for Ad-Hoc/AP mode */
1020 	if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) ||
1021 	     check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) ||
1022 	     check_fwstate(pmlmepriv, WIFI_AP_STATE)) &&
1023 	    check_fwstate(pmlmepriv, _FW_LINKED)) {
1024 		/* TODO: should acquire station info... */
1025 	}
1026 
1027 exit:
1028 	return ret;
1029 }
1030 
cfg80211_rtw_change_iface(struct wiphy * wiphy,struct net_device * ndev,enum nl80211_iftype type,struct vif_params * params)1031 static int cfg80211_rtw_change_iface(struct wiphy *wiphy,
1032 				     struct net_device *ndev,
1033 				     enum nl80211_iftype type,
1034 				     struct vif_params *params)
1035 {
1036 	enum nl80211_iftype old_type;
1037 	enum ndis_802_11_network_infrastructure networkType;
1038 	struct adapter *padapter = rtw_netdev_priv(ndev);
1039 	struct wireless_dev *rtw_wdev = padapter->rtw_wdev;
1040 	struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
1041 	int ret = 0;
1042 
1043 	if (adapter_to_dvobj(padapter)->processing_dev_remove == true) {
1044 		ret = -EPERM;
1045 		goto exit;
1046 	}
1047 
1048 	{
1049 		if (netdev_open(ndev) != 0) {
1050 			ret = -EPERM;
1051 			goto exit;
1052 		}
1053 	}
1054 
1055 	if (rtw_pwr_wakeup(padapter) == _FAIL) {
1056 		ret = -EPERM;
1057 		goto exit;
1058 	}
1059 
1060 	old_type = rtw_wdev->iftype;
1061 
1062 	if (old_type != type) {
1063 		pmlmeext->action_public_rxseq = 0xffff;
1064 		pmlmeext->action_public_dialog_token = 0xff;
1065 	}
1066 
1067 	switch (type) {
1068 	case NL80211_IFTYPE_ADHOC:
1069 		networkType = Ndis802_11IBSS;
1070 		break;
1071 	case NL80211_IFTYPE_STATION:
1072 		networkType = Ndis802_11Infrastructure;
1073 		break;
1074 	case NL80211_IFTYPE_AP:
1075 		networkType = Ndis802_11APMode;
1076 		break;
1077 	default:
1078 		ret = -EOPNOTSUPP;
1079 		goto exit;
1080 	}
1081 
1082 	rtw_wdev->iftype = type;
1083 
1084 	if (rtw_set_802_11_infrastructure_mode(padapter, networkType) == false) {
1085 		rtw_wdev->iftype = old_type;
1086 		ret = -EPERM;
1087 		goto exit;
1088 	}
1089 
1090 	rtw_setopmode_cmd(padapter, networkType, true);
1091 
1092 exit:
1093 
1094 	return ret;
1095 }
1096 
rtw_cfg80211_indicate_scan_done(struct adapter * adapter,bool aborted)1097 void rtw_cfg80211_indicate_scan_done(struct adapter *adapter, bool aborted)
1098 {
1099 	struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(adapter);
1100 	struct cfg80211_scan_info info = {
1101 		.aborted = aborted
1102 	};
1103 
1104 	spin_lock_bh(&pwdev_priv->scan_req_lock);
1105 	if (pwdev_priv->scan_request) {
1106 		/* avoid WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req); */
1107 		if (pwdev_priv->scan_request->wiphy == pwdev_priv->rtw_wdev->wiphy)
1108 			cfg80211_scan_done(pwdev_priv->scan_request, &info);
1109 
1110 		pwdev_priv->scan_request = NULL;
1111 	}
1112 	spin_unlock_bh(&pwdev_priv->scan_req_lock);
1113 }
1114 
rtw_cfg80211_unlink_bss(struct adapter * padapter,struct wlan_network * pnetwork)1115 void rtw_cfg80211_unlink_bss(struct adapter *padapter, struct wlan_network *pnetwork)
1116 {
1117 	struct wireless_dev *pwdev = padapter->rtw_wdev;
1118 	struct wiphy *wiphy = pwdev->wiphy;
1119 	struct cfg80211_bss *bss = NULL;
1120 	struct wlan_bssid_ex *select_network = &pnetwork->network;
1121 
1122 	bss = cfg80211_get_bss(wiphy, NULL/*notify_channel*/,
1123 			       select_network->mac_address,
1124 			       select_network->ssid.ssid,
1125 			       select_network->ssid.ssid_length,
1126 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
1127 
1128 	if (bss) {
1129 		cfg80211_unlink_bss(wiphy, bss);
1130 		cfg80211_put_bss(padapter->rtw_wdev->wiphy, bss);
1131 	}
1132 }
1133 
rtw_cfg80211_surveydone_event_callback(struct adapter * padapter)1134 void rtw_cfg80211_surveydone_event_callback(struct adapter *padapter)
1135 {
1136 	struct list_head					*plist, *phead;
1137 	struct	mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1138 	struct __queue *queue	= &(pmlmepriv->scanned_queue);
1139 	struct	wlan_network	*pnetwork = NULL;
1140 
1141 	spin_lock_bh(&(pmlmepriv->scanned_queue.lock));
1142 
1143 	phead = get_list_head(queue);
1144 	list_for_each(plist, phead)
1145 	{
1146 		pnetwork = list_entry(plist, struct wlan_network, list);
1147 
1148 		/* report network only if the current channel set contains the channel to which this network belongs */
1149 		if (rtw_ch_set_search_ch(padapter->mlmeextpriv.channel_set, pnetwork->network.configuration.ds_config) >= 0
1150 			&& true == rtw_validate_ssid(&(pnetwork->network.ssid))) {
1151 			/* ev =translate_scan(padapter, a, pnetwork, ev, stop); */
1152 			rtw_cfg80211_inform_bss(padapter, pnetwork);
1153 		}
1154 	}
1155 
1156 	spin_unlock_bh(&(pmlmepriv->scanned_queue.lock));
1157 }
1158 
rtw_cfg80211_set_probe_req_wpsp2pie(struct adapter * padapter,char * buf,int len)1159 static int rtw_cfg80211_set_probe_req_wpsp2pie(struct adapter *padapter, char *buf, int len)
1160 {
1161 	int ret = 0;
1162 	uint wps_ielen = 0;
1163 	u8 *wps_ie;
1164 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
1165 
1166 	if (len > 0) {
1167 		wps_ie = rtw_get_wps_ie(buf, len, NULL, &wps_ielen);
1168 		if (wps_ie) {
1169 			if (pmlmepriv->wps_probe_req_ie) {
1170 				pmlmepriv->wps_probe_req_ie_len = 0;
1171 				kfree(pmlmepriv->wps_probe_req_ie);
1172 				pmlmepriv->wps_probe_req_ie = NULL;
1173 			}
1174 
1175 			pmlmepriv->wps_probe_req_ie = rtw_malloc(wps_ielen);
1176 			if (!pmlmepriv->wps_probe_req_ie)
1177 				return -EINVAL;
1178 
1179 			memcpy(pmlmepriv->wps_probe_req_ie, wps_ie, wps_ielen);
1180 			pmlmepriv->wps_probe_req_ie_len = wps_ielen;
1181 		}
1182 	}
1183 
1184 	return ret;
1185 }
1186 
cfg80211_rtw_scan(struct wiphy * wiphy,struct cfg80211_scan_request * request)1187 static int cfg80211_rtw_scan(struct wiphy *wiphy
1188 	, struct cfg80211_scan_request *request)
1189 {
1190 	struct net_device *ndev = wdev_to_ndev(request->wdev);
1191 	int i;
1192 	u8 _status = false;
1193 	int ret = 0;
1194 	struct ndis_802_11_ssid *ssid = NULL;
1195 	struct rtw_ieee80211_channel ch[RTW_CHANNEL_SCAN_AMOUNT];
1196 	u8 survey_times = 3;
1197 	u8 survey_times_for_one_ch = 6;
1198 	struct cfg80211_ssid *ssids = request->ssids;
1199 	int j = 0;
1200 	bool need_indicate_scan_done = false;
1201 
1202 	struct adapter *padapter;
1203 	struct rtw_wdev_priv *pwdev_priv;
1204 	struct mlme_priv *pmlmepriv;
1205 
1206 	if (!ndev) {
1207 		ret = -EINVAL;
1208 		goto exit;
1209 	}
1210 
1211 	padapter = rtw_netdev_priv(ndev);
1212 	pwdev_priv = adapter_wdev_data(padapter);
1213 	pmlmepriv = &padapter->mlmepriv;
1214 /* endif */
1215 
1216 	spin_lock_bh(&pwdev_priv->scan_req_lock);
1217 	pwdev_priv->scan_request = request;
1218 	spin_unlock_bh(&pwdev_priv->scan_req_lock);
1219 
1220 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true) {
1221 		if (check_fwstate(pmlmepriv, WIFI_UNDER_WPS | _FW_UNDER_SURVEY | _FW_UNDER_LINKING) == true) {
1222 			need_indicate_scan_done = true;
1223 			goto check_need_indicate_scan_done;
1224 		}
1225 	}
1226 
1227 	rtw_ps_deny(padapter, PS_DENY_SCAN);
1228 	if (rtw_pwr_wakeup(padapter) == _FAIL) {
1229 		need_indicate_scan_done = true;
1230 		goto check_need_indicate_scan_done;
1231 	}
1232 
1233 	if (request->ie && request->ie_len > 0)
1234 		rtw_cfg80211_set_probe_req_wpsp2pie(padapter, (u8 *)request->ie, request->ie_len);
1235 
1236 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY) == true) {
1237 		need_indicate_scan_done = true;
1238 		goto check_need_indicate_scan_done;
1239 	} else if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING) == true) {
1240 		ret = -EBUSY;
1241 		goto check_need_indicate_scan_done;
1242 	}
1243 
1244 	if (pmlmepriv->LinkDetectInfo.bBusyTraffic == true) {
1245 		static unsigned long lastscantime;
1246 		unsigned long passtime;
1247 
1248 		passtime = jiffies_to_msecs(jiffies - lastscantime);
1249 		lastscantime = jiffies;
1250 		if (passtime > 12000) {
1251 			need_indicate_scan_done = true;
1252 			goto check_need_indicate_scan_done;
1253 		}
1254 	}
1255 
1256 	if (rtw_is_scan_deny(padapter)) {
1257 		need_indicate_scan_done = true;
1258 		goto check_need_indicate_scan_done;
1259 	}
1260 
1261 	ssid = kcalloc(RTW_SSID_SCAN_AMOUNT, sizeof(*ssid), GFP_KERNEL);
1262 	if (!ssid) {
1263 		ret = -ENOMEM;
1264 		goto check_need_indicate_scan_done;
1265 	}
1266 
1267 	/* parsing request ssids, n_ssids */
1268 	for (i = 0; i < request->n_ssids && i < RTW_SSID_SCAN_AMOUNT; i++) {
1269 		memcpy(ssid[i].ssid, ssids[i].ssid, ssids[i].ssid_len);
1270 		ssid[i].ssid_length = ssids[i].ssid_len;
1271 	}
1272 
1273 	/* parsing channels, n_channels */
1274 	memset(ch, 0, sizeof(struct rtw_ieee80211_channel) * RTW_CHANNEL_SCAN_AMOUNT);
1275 	for (i = 0; i < request->n_channels && i < RTW_CHANNEL_SCAN_AMOUNT; i++) {
1276 		ch[i].hw_value = request->channels[i]->hw_value;
1277 		ch[i].flags = request->channels[i]->flags;
1278 	}
1279 
1280 	spin_lock_bh(&pmlmepriv->lock);
1281 	if (request->n_channels == 1) {
1282 		for (i = 1; i < survey_times_for_one_ch; i++)
1283 			memcpy(&ch[i], &ch[0], sizeof(struct rtw_ieee80211_channel));
1284 		_status = rtw_sitesurvey_cmd(padapter, ssid, RTW_SSID_SCAN_AMOUNT, ch, survey_times_for_one_ch);
1285 	} else if (request->n_channels <= 4) {
1286 		for (j = request->n_channels - 1; j >= 0; j--)
1287 			for (i = 0; i < survey_times; i++)
1288 				memcpy(&ch[j * survey_times + i], &ch[j], sizeof(struct rtw_ieee80211_channel));
1289 		_status = rtw_sitesurvey_cmd(padapter, ssid, RTW_SSID_SCAN_AMOUNT, ch, survey_times * request->n_channels);
1290 	} else {
1291 		_status = rtw_sitesurvey_cmd(padapter, ssid, RTW_SSID_SCAN_AMOUNT, NULL, 0);
1292 	}
1293 	spin_unlock_bh(&pmlmepriv->lock);
1294 
1295 	if (_status == false)
1296 		ret = -1;
1297 
1298 check_need_indicate_scan_done:
1299 	kfree(ssid);
1300 	if (need_indicate_scan_done) {
1301 		rtw_cfg80211_surveydone_event_callback(padapter);
1302 		rtw_cfg80211_indicate_scan_done(padapter, false);
1303 	}
1304 
1305 	rtw_ps_deny_cancel(padapter, PS_DENY_SCAN);
1306 
1307 exit:
1308 	return ret;
1309 }
1310 
cfg80211_rtw_set_wiphy_params(struct wiphy * wiphy,u32 changed)1311 static int cfg80211_rtw_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1312 {
1313 	return 0;
1314 }
1315 
rtw_cfg80211_set_wpa_version(struct security_priv * psecuritypriv,u32 wpa_version)1316 static int rtw_cfg80211_set_wpa_version(struct security_priv *psecuritypriv, u32 wpa_version)
1317 {
1318 	if (!wpa_version) {
1319 		psecuritypriv->ndisauthtype = Ndis802_11AuthModeOpen;
1320 		return 0;
1321 	}
1322 
1323 	if (wpa_version & (NL80211_WPA_VERSION_1 | NL80211_WPA_VERSION_2))
1324 		psecuritypriv->ndisauthtype = Ndis802_11AuthModeWPAPSK;
1325 
1326 	return 0;
1327 }
1328 
rtw_cfg80211_set_auth_type(struct security_priv * psecuritypriv,enum nl80211_auth_type sme_auth_type)1329 static int rtw_cfg80211_set_auth_type(struct security_priv *psecuritypriv,
1330 				      enum nl80211_auth_type sme_auth_type)
1331 {
1332 	switch (sme_auth_type) {
1333 	case NL80211_AUTHTYPE_AUTOMATIC:
1334 
1335 		psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Auto;
1336 
1337 		break;
1338 	case NL80211_AUTHTYPE_OPEN_SYSTEM:
1339 
1340 		psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open;
1341 
1342 		if (psecuritypriv->ndisauthtype > Ndis802_11AuthModeWPA)
1343 			psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1344 
1345 		break;
1346 	case NL80211_AUTHTYPE_SHARED_KEY:
1347 
1348 		psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Shared;
1349 
1350 		psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled;
1351 
1352 		break;
1353 	default:
1354 		psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open;
1355 		/* return -ENOTSUPP; */
1356 	}
1357 
1358 	return 0;
1359 }
1360 
rtw_cfg80211_set_cipher(struct security_priv * psecuritypriv,u32 cipher,bool ucast)1361 static int rtw_cfg80211_set_cipher(struct security_priv *psecuritypriv, u32 cipher, bool ucast)
1362 {
1363 	u32 ndisencryptstatus = Ndis802_11EncryptionDisabled;
1364 
1365 	u32 *profile_cipher = ucast ? &psecuritypriv->dot11PrivacyAlgrthm :
1366 		&psecuritypriv->dot118021XGrpPrivacy;
1367 
1368 	if (!cipher) {
1369 		*profile_cipher = _NO_PRIVACY_;
1370 		psecuritypriv->ndisencryptstatus = ndisencryptstatus;
1371 		return 0;
1372 	}
1373 
1374 	switch (cipher) {
1375 	case IW_AUTH_CIPHER_NONE:
1376 		*profile_cipher = _NO_PRIVACY_;
1377 		ndisencryptstatus = Ndis802_11EncryptionDisabled;
1378 		break;
1379 	case WLAN_CIPHER_SUITE_WEP40:
1380 		*profile_cipher = _WEP40_;
1381 		ndisencryptstatus = Ndis802_11Encryption1Enabled;
1382 		break;
1383 	case WLAN_CIPHER_SUITE_WEP104:
1384 		*profile_cipher = _WEP104_;
1385 		ndisencryptstatus = Ndis802_11Encryption1Enabled;
1386 		break;
1387 	case WLAN_CIPHER_SUITE_TKIP:
1388 		*profile_cipher = _TKIP_;
1389 		ndisencryptstatus = Ndis802_11Encryption2Enabled;
1390 		break;
1391 	case WLAN_CIPHER_SUITE_CCMP:
1392 		*profile_cipher = _AES_;
1393 		ndisencryptstatus = Ndis802_11Encryption3Enabled;
1394 		break;
1395 	default:
1396 		return -ENOTSUPP;
1397 	}
1398 
1399 	if (ucast) {
1400 		psecuritypriv->ndisencryptstatus = ndisencryptstatus;
1401 
1402 		/* if (psecuritypriv->dot11PrivacyAlgrthm >= _AES_) */
1403 		/*	psecuritypriv->ndisauthtype = Ndis802_11AuthModeWPA2PSK; */
1404 	}
1405 
1406 	return 0;
1407 }
1408 
rtw_cfg80211_set_key_mgt(struct security_priv * psecuritypriv,u32 key_mgt)1409 static int rtw_cfg80211_set_key_mgt(struct security_priv *psecuritypriv, u32 key_mgt)
1410 {
1411 	if (key_mgt == WLAN_AKM_SUITE_8021X)
1412 		/* auth_type = UMAC_AUTH_TYPE_8021X; */
1413 		psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1414 	else if (key_mgt == WLAN_AKM_SUITE_PSK) {
1415 		psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1416 	}
1417 
1418 	return 0;
1419 }
1420 
rtw_cfg80211_set_wpa_ie(struct adapter * padapter,u8 * pie,size_t ielen)1421 static int rtw_cfg80211_set_wpa_ie(struct adapter *padapter, u8 *pie, size_t ielen)
1422 {
1423 	u8 *buf = NULL;
1424 	int group_cipher = 0, pairwise_cipher = 0;
1425 	int ret = 0;
1426 	int wpa_ielen = 0;
1427 	int wpa2_ielen = 0;
1428 	u8 *pwpa, *pwpa2;
1429 	u8 null_addr[] = {0, 0, 0, 0, 0, 0};
1430 
1431 	if (!pie || !ielen) {
1432 		/* Treat this as normal case, but need to clear WIFI_UNDER_WPS */
1433 		_clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS);
1434 		goto exit;
1435 	}
1436 
1437 	if (ielen > MAX_WPA_IE_LEN + MAX_WPS_IE_LEN + MAX_P2P_IE_LEN) {
1438 		ret = -EINVAL;
1439 		goto exit;
1440 	}
1441 
1442 	buf = rtw_zmalloc(ielen);
1443 	if (!buf) {
1444 		ret =  -ENOMEM;
1445 		goto exit;
1446 	}
1447 
1448 	memcpy(buf, pie, ielen);
1449 
1450 	if (ielen < RSN_HEADER_LEN) {
1451 		ret  = -1;
1452 		goto exit;
1453 	}
1454 
1455 	pwpa = rtw_get_wpa_ie(buf, &wpa_ielen, ielen);
1456 	if (pwpa && wpa_ielen > 0) {
1457 		if (rtw_parse_wpa_ie(pwpa, wpa_ielen + 2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
1458 			padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1459 			padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPAPSK;
1460 			memcpy(padapter->securitypriv.supplicant_ie, &pwpa[0], wpa_ielen + 2);
1461 		}
1462 	}
1463 
1464 	pwpa2 = rtw_get_wpa2_ie(buf, &wpa2_ielen, ielen);
1465 	if (pwpa2 && wpa2_ielen > 0) {
1466 		if (rtw_parse_wpa2_ie(pwpa2, wpa2_ielen + 2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
1467 			padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1468 			padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPA2PSK;
1469 			memcpy(padapter->securitypriv.supplicant_ie, &pwpa2[0], wpa2_ielen + 2);
1470 		}
1471 	}
1472 
1473 	if (group_cipher == 0)
1474 		group_cipher = WPA_CIPHER_NONE;
1475 
1476 	if (pairwise_cipher == 0)
1477 		pairwise_cipher = WPA_CIPHER_NONE;
1478 
1479 	switch (group_cipher) {
1480 	case WPA_CIPHER_NONE:
1481 		padapter->securitypriv.dot118021XGrpPrivacy = _NO_PRIVACY_;
1482 		padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled;
1483 		break;
1484 	case WPA_CIPHER_WEP40:
1485 		padapter->securitypriv.dot118021XGrpPrivacy = _WEP40_;
1486 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
1487 		break;
1488 	case WPA_CIPHER_TKIP:
1489 		padapter->securitypriv.dot118021XGrpPrivacy = _TKIP_;
1490 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption2Enabled;
1491 		break;
1492 	case WPA_CIPHER_CCMP:
1493 		padapter->securitypriv.dot118021XGrpPrivacy = _AES_;
1494 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled;
1495 		break;
1496 	case WPA_CIPHER_WEP104:
1497 		padapter->securitypriv.dot118021XGrpPrivacy = _WEP104_;
1498 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
1499 		break;
1500 	}
1501 
1502 	switch (pairwise_cipher) {
1503 	case WPA_CIPHER_NONE:
1504 		padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_;
1505 		padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled;
1506 		break;
1507 	case WPA_CIPHER_WEP40:
1508 		padapter->securitypriv.dot11PrivacyAlgrthm = _WEP40_;
1509 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
1510 		break;
1511 	case WPA_CIPHER_TKIP:
1512 		padapter->securitypriv.dot11PrivacyAlgrthm = _TKIP_;
1513 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption2Enabled;
1514 		break;
1515 	case WPA_CIPHER_CCMP:
1516 		padapter->securitypriv.dot11PrivacyAlgrthm = _AES_;
1517 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled;
1518 		break;
1519 	case WPA_CIPHER_WEP104:
1520 		padapter->securitypriv.dot11PrivacyAlgrthm = _WEP104_;
1521 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
1522 		break;
1523 	}
1524 
1525 	{/* handle wps_ie */
1526 		uint wps_ielen;
1527 		u8 *wps_ie;
1528 
1529 		wps_ie = rtw_get_wps_ie(buf, ielen, NULL, &wps_ielen);
1530 		if (wps_ie && wps_ielen > 0) {
1531 			padapter->securitypriv.wps_ie_len = min_t(uint, wps_ielen, MAX_WPS_IE_LEN);
1532 			memcpy(padapter->securitypriv.wps_ie, wps_ie, padapter->securitypriv.wps_ie_len);
1533 			set_fwstate(&padapter->mlmepriv, WIFI_UNDER_WPS);
1534 		} else {
1535 			_clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS);
1536 		}
1537 	}
1538 
1539 	/* TKIP and AES disallow multicast packets until installing group key */
1540 	if (padapter->securitypriv.dot11PrivacyAlgrthm == _TKIP_
1541 		|| padapter->securitypriv.dot11PrivacyAlgrthm == _TKIP_WTMIC_
1542 		|| padapter->securitypriv.dot11PrivacyAlgrthm == _AES_)
1543 		/* WPS open need to enable multicast */
1544 		/*  check_fwstate(&padapter->mlmepriv, WIFI_UNDER_WPS) == true) */
1545 		rtw_hal_set_hwreg(padapter, HW_VAR_OFF_RCR_AM, null_addr);
1546 
1547 exit:
1548 	kfree(buf);
1549 	if (ret)
1550 		_clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS);
1551 	return ret;
1552 }
1553 
cfg80211_rtw_join_ibss(struct wiphy * wiphy,struct net_device * ndev,struct cfg80211_ibss_params * params)1554 static int cfg80211_rtw_join_ibss(struct wiphy *wiphy, struct net_device *ndev,
1555 				  struct cfg80211_ibss_params *params)
1556 {
1557 	struct adapter *padapter = rtw_netdev_priv(ndev);
1558 	struct ndis_802_11_ssid ndis_ssid;
1559 	struct security_priv *psecuritypriv = &padapter->securitypriv;
1560 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1561 	int ret = 0;
1562 
1563 	if (rtw_pwr_wakeup(padapter) == _FAIL) {
1564 		ret = -EPERM;
1565 		goto exit;
1566 	}
1567 
1568 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1569 		ret = -EPERM;
1570 		goto exit;
1571 	}
1572 
1573 	if (!params->ssid || !params->ssid_len) {
1574 		ret = -EINVAL;
1575 		goto exit;
1576 	}
1577 
1578 	if (params->ssid_len > IW_ESSID_MAX_SIZE) {
1579 		ret = -E2BIG;
1580 		goto exit;
1581 	}
1582 
1583 	memset(&ndis_ssid, 0, sizeof(struct ndis_802_11_ssid));
1584 	ndis_ssid.ssid_length = params->ssid_len;
1585 	memcpy(ndis_ssid.ssid, (u8 *)params->ssid, params->ssid_len);
1586 
1587 	psecuritypriv->ndisencryptstatus = Ndis802_11EncryptionDisabled;
1588 	psecuritypriv->dot11PrivacyAlgrthm = _NO_PRIVACY_;
1589 	psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_;
1590 	psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */
1591 	psecuritypriv->ndisauthtype = Ndis802_11AuthModeOpen;
1592 
1593 	ret = rtw_cfg80211_set_auth_type(psecuritypriv, NL80211_AUTHTYPE_OPEN_SYSTEM);
1594 	rtw_set_802_11_authentication_mode(padapter, psecuritypriv->ndisauthtype);
1595 
1596 	if (rtw_set_802_11_ssid(padapter, &ndis_ssid) == false) {
1597 		ret = -1;
1598 		goto exit;
1599 	}
1600 
1601 exit:
1602 	return ret;
1603 }
1604 
cfg80211_rtw_leave_ibss(struct wiphy * wiphy,struct net_device * ndev)1605 static int cfg80211_rtw_leave_ibss(struct wiphy *wiphy, struct net_device *ndev)
1606 {
1607 	struct adapter *padapter = rtw_netdev_priv(ndev);
1608 	struct wireless_dev *rtw_wdev = padapter->rtw_wdev;
1609 	enum nl80211_iftype old_type;
1610 	int ret = 0;
1611 
1612 	old_type = rtw_wdev->iftype;
1613 
1614 	rtw_set_to_roam(padapter, 0);
1615 
1616 	if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
1617 		rtw_scan_abort(padapter);
1618 		LeaveAllPowerSaveMode(padapter);
1619 
1620 		rtw_wdev->iftype = NL80211_IFTYPE_STATION;
1621 
1622 		if (rtw_set_802_11_infrastructure_mode(padapter, Ndis802_11Infrastructure) == false) {
1623 			rtw_wdev->iftype = old_type;
1624 			ret = -EPERM;
1625 			goto leave_ibss;
1626 		}
1627 		rtw_setopmode_cmd(padapter, Ndis802_11Infrastructure, true);
1628 	}
1629 
1630 leave_ibss:
1631 	return ret;
1632 }
1633 
cfg80211_rtw_connect(struct wiphy * wiphy,struct net_device * ndev,struct cfg80211_connect_params * sme)1634 static int cfg80211_rtw_connect(struct wiphy *wiphy, struct net_device *ndev,
1635 				struct cfg80211_connect_params *sme)
1636 {
1637 	int ret = 0;
1638 	enum ndis_802_11_authentication_mode authmode;
1639 	struct ndis_802_11_ssid ndis_ssid;
1640 	struct adapter *padapter = rtw_netdev_priv(ndev);
1641 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1642 	struct security_priv *psecuritypriv = &padapter->securitypriv;
1643 
1644 	padapter->mlmepriv.not_indic_disco = true;
1645 
1646 	if (adapter_wdev_data(padapter)->block == true) {
1647 		ret = -EBUSY;
1648 		goto exit;
1649 	}
1650 
1651 	rtw_ps_deny(padapter, PS_DENY_JOIN);
1652 	if (rtw_pwr_wakeup(padapter) == _FAIL) {
1653 		ret = -EPERM;
1654 		goto exit;
1655 	}
1656 
1657 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1658 		ret = -EPERM;
1659 		goto exit;
1660 	}
1661 
1662 	if (!sme->ssid || !sme->ssid_len) {
1663 		ret = -EINVAL;
1664 		goto exit;
1665 	}
1666 
1667 	if (sme->ssid_len > IW_ESSID_MAX_SIZE) {
1668 		ret = -E2BIG;
1669 		goto exit;
1670 	}
1671 
1672 	memset(&ndis_ssid, 0, sizeof(struct ndis_802_11_ssid));
1673 	ndis_ssid.ssid_length = sme->ssid_len;
1674 	memcpy(ndis_ssid.ssid, (u8 *)sme->ssid, sme->ssid_len);
1675 
1676 	if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING) == true) {
1677 		ret = -EBUSY;
1678 		goto exit;
1679 	}
1680 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY) == true)
1681 		rtw_scan_abort(padapter);
1682 
1683 	psecuritypriv->ndisencryptstatus = Ndis802_11EncryptionDisabled;
1684 	psecuritypriv->dot11PrivacyAlgrthm = _NO_PRIVACY_;
1685 	psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_;
1686 	psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */
1687 	psecuritypriv->ndisauthtype = Ndis802_11AuthModeOpen;
1688 
1689 	ret = rtw_cfg80211_set_wpa_version(psecuritypriv, sme->crypto.wpa_versions);
1690 	if (ret < 0)
1691 		goto exit;
1692 
1693 	ret = rtw_cfg80211_set_auth_type(psecuritypriv, sme->auth_type);
1694 
1695 	if (ret < 0)
1696 		goto exit;
1697 
1698 	ret = rtw_cfg80211_set_wpa_ie(padapter, (u8 *)sme->ie, sme->ie_len);
1699 	if (ret < 0)
1700 		goto exit;
1701 
1702 	if (sme->crypto.n_ciphers_pairwise) {
1703 		ret = rtw_cfg80211_set_cipher(psecuritypriv, sme->crypto.ciphers_pairwise[0], true);
1704 		if (ret < 0)
1705 			goto exit;
1706 	}
1707 
1708 	/* For WEP Shared auth */
1709 	if ((psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_Shared ||
1710 	     psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_Auto) && sme->key) {
1711 		u32 wep_key_idx, wep_key_len, wep_total_len;
1712 		struct ndis_802_11_wep	 *pwep = NULL;
1713 
1714 		wep_key_idx = sme->key_idx;
1715 		wep_key_len = sme->key_len;
1716 
1717 		if (sme->key_idx > WEP_KEYS) {
1718 			ret = -EINVAL;
1719 			goto exit;
1720 		}
1721 
1722 		if (wep_key_len > 0) {
1723 			wep_key_len = wep_key_len <= 5 ? 5 : 13;
1724 			wep_total_len = wep_key_len + FIELD_OFFSET(struct ndis_802_11_wep, key_material);
1725 			pwep = rtw_malloc(wep_total_len);
1726 			if (!pwep) {
1727 				ret = -ENOMEM;
1728 				goto exit;
1729 			}
1730 
1731 			memset(pwep, 0, wep_total_len);
1732 
1733 			pwep->key_length = wep_key_len;
1734 			pwep->length = wep_total_len;
1735 
1736 			if (wep_key_len == 13) {
1737 				padapter->securitypriv.dot11PrivacyAlgrthm = _WEP104_;
1738 				padapter->securitypriv.dot118021XGrpPrivacy = _WEP104_;
1739 			}
1740 		} else {
1741 			ret = -EINVAL;
1742 			goto exit;
1743 		}
1744 
1745 		pwep->key_index = wep_key_idx;
1746 		pwep->key_index |= 0x80000000;
1747 
1748 		memcpy(pwep->key_material,  (void *)sme->key, pwep->key_length);
1749 
1750 		if (rtw_set_802_11_add_wep(padapter, pwep) == (u8)_FAIL)
1751 			ret = -EOPNOTSUPP;
1752 
1753 		kfree(pwep);
1754 
1755 		if (ret < 0)
1756 			goto exit;
1757 	}
1758 
1759 	ret = rtw_cfg80211_set_cipher(psecuritypriv, sme->crypto.cipher_group, false);
1760 	if (ret < 0)
1761 		return ret;
1762 
1763 	if (sme->crypto.n_akm_suites) {
1764 		ret = rtw_cfg80211_set_key_mgt(psecuritypriv, sme->crypto.akm_suites[0]);
1765 		if (ret < 0)
1766 			goto exit;
1767 	}
1768 
1769 	authmode = psecuritypriv->ndisauthtype;
1770 	rtw_set_802_11_authentication_mode(padapter, authmode);
1771 
1772 	/* rtw_set_802_11_encryption_mode(padapter, padapter->securitypriv.ndisencryptstatus); */
1773 
1774 	if (rtw_set_802_11_connect(padapter, (u8 *)sme->bssid, &ndis_ssid) == false) {
1775 		ret = -1;
1776 		goto exit;
1777 	}
1778 
1779 exit:
1780 
1781 	rtw_ps_deny_cancel(padapter, PS_DENY_JOIN);
1782 
1783 	padapter->mlmepriv.not_indic_disco = false;
1784 
1785 	return ret;
1786 }
1787 
cfg80211_rtw_disconnect(struct wiphy * wiphy,struct net_device * ndev,u16 reason_code)1788 static int cfg80211_rtw_disconnect(struct wiphy *wiphy, struct net_device *ndev,
1789 				   u16 reason_code)
1790 {
1791 	struct adapter *padapter = rtw_netdev_priv(ndev);
1792 
1793 	rtw_set_to_roam(padapter, 0);
1794 
1795 	rtw_scan_abort(padapter);
1796 	LeaveAllPowerSaveMode(padapter);
1797 	rtw_disassoc_cmd(padapter, 500, false);
1798 
1799 	rtw_indicate_disconnect(padapter);
1800 
1801 	rtw_free_assoc_resources(padapter, 1);
1802 	rtw_pwr_wakeup(padapter);
1803 
1804 	return 0;
1805 }
1806 
cfg80211_rtw_set_txpower(struct wiphy * wiphy,struct wireless_dev * wdev,enum nl80211_tx_power_setting type,int mbm)1807 static int cfg80211_rtw_set_txpower(struct wiphy *wiphy,
1808 				    struct wireless_dev *wdev,
1809 				    enum nl80211_tx_power_setting type, int mbm)
1810 {
1811 	return 0;
1812 }
1813 
cfg80211_rtw_get_txpower(struct wiphy * wiphy,struct wireless_dev * wdev,int * dbm)1814 static int cfg80211_rtw_get_txpower(struct wiphy *wiphy,
1815 				    struct wireless_dev *wdev, int *dbm)
1816 {
1817 	*dbm = (12);
1818 
1819 	return 0;
1820 }
1821 
rtw_cfg80211_pwr_mgmt(struct adapter * adapter)1822 inline bool rtw_cfg80211_pwr_mgmt(struct adapter *adapter)
1823 {
1824 	struct rtw_wdev_priv *rtw_wdev_priv = adapter_wdev_data(adapter);
1825 
1826 	return rtw_wdev_priv->power_mgmt;
1827 }
1828 
cfg80211_rtw_set_power_mgmt(struct wiphy * wiphy,struct net_device * ndev,bool enabled,int timeout)1829 static int cfg80211_rtw_set_power_mgmt(struct wiphy *wiphy,
1830 				       struct net_device *ndev,
1831 				       bool enabled, int timeout)
1832 {
1833 	struct adapter *padapter = rtw_netdev_priv(ndev);
1834 	struct rtw_wdev_priv *rtw_wdev_priv = adapter_wdev_data(padapter);
1835 
1836 	rtw_wdev_priv->power_mgmt = enabled;
1837 
1838 	if (!enabled)
1839 		LPS_Leave(padapter, "CFG80211_PWRMGMT");
1840 
1841 	return 0;
1842 }
1843 
cfg80211_rtw_set_pmksa(struct wiphy * wiphy,struct net_device * ndev,struct cfg80211_pmksa * pmksa)1844 static int cfg80211_rtw_set_pmksa(struct wiphy *wiphy,
1845 				  struct net_device *ndev,
1846 				  struct cfg80211_pmksa *pmksa)
1847 {
1848 	u8 index, blInserted = false;
1849 	struct adapter *padapter = rtw_netdev_priv(ndev);
1850 	struct security_priv *psecuritypriv = &padapter->securitypriv;
1851 
1852 	if (is_zero_ether_addr((u8 *)pmksa->bssid))
1853 		return -EINVAL;
1854 
1855 	blInserted = false;
1856 
1857 	/* overwrite PMKID */
1858 	for (index = 0 ; index < NUM_PMKID_CACHE; index++) {
1859 		if (!memcmp(psecuritypriv->PMKIDList[index].Bssid, (u8 *)pmksa->bssid, ETH_ALEN)) {
1860 			memcpy(psecuritypriv->PMKIDList[index].PMKID, (u8 *)pmksa->pmkid, WLAN_PMKID_LEN);
1861 			psecuritypriv->PMKIDList[index].bUsed = true;
1862 			psecuritypriv->PMKIDIndex = index + 1;
1863 			blInserted = true;
1864 			break;
1865 		}
1866 	}
1867 
1868 	if (!blInserted) {
1869 		memcpy(psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].Bssid, (u8 *)pmksa->bssid, ETH_ALEN);
1870 		memcpy(psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].PMKID, (u8 *)pmksa->pmkid, WLAN_PMKID_LEN);
1871 
1872 		psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].bUsed = true;
1873 		psecuritypriv->PMKIDIndex++;
1874 		if (psecuritypriv->PMKIDIndex == 16)
1875 			psecuritypriv->PMKIDIndex = 0;
1876 	}
1877 
1878 	return 0;
1879 }
1880 
cfg80211_rtw_del_pmksa(struct wiphy * wiphy,struct net_device * ndev,struct cfg80211_pmksa * pmksa)1881 static int cfg80211_rtw_del_pmksa(struct wiphy *wiphy,
1882 				  struct net_device *ndev,
1883 				  struct cfg80211_pmksa *pmksa)
1884 {
1885 	u8 index, bMatched = false;
1886 	struct adapter *padapter = rtw_netdev_priv(ndev);
1887 	struct security_priv *psecuritypriv = &padapter->securitypriv;
1888 
1889 	for (index = 0 ; index < NUM_PMKID_CACHE; index++) {
1890 		if (!memcmp(psecuritypriv->PMKIDList[index].Bssid, (u8 *)pmksa->bssid, ETH_ALEN)) {
1891 			/*
1892 			 * BSSID is matched, the same AP => Remove this PMKID information
1893 			 * and reset it.
1894 			 */
1895 			eth_zero_addr(psecuritypriv->PMKIDList[index].Bssid);
1896 			memset(psecuritypriv->PMKIDList[index].PMKID, 0x00, WLAN_PMKID_LEN);
1897 			psecuritypriv->PMKIDList[index].bUsed = false;
1898 			bMatched = true;
1899 			break;
1900 		}
1901 	}
1902 
1903 	if (!bMatched)
1904 		return -EINVAL;
1905 
1906 	return 0;
1907 }
1908 
cfg80211_rtw_flush_pmksa(struct wiphy * wiphy,struct net_device * ndev)1909 static int cfg80211_rtw_flush_pmksa(struct wiphy *wiphy,
1910 				    struct net_device *ndev)
1911 {
1912 	struct adapter *padapter = rtw_netdev_priv(ndev);
1913 	struct security_priv *psecuritypriv = &padapter->securitypriv;
1914 
1915 	memset(&psecuritypriv->PMKIDList[0], 0x00, sizeof(struct rt_pmkid_list) * NUM_PMKID_CACHE);
1916 	psecuritypriv->PMKIDIndex = 0;
1917 
1918 	return 0;
1919 }
1920 
rtw_cfg80211_indicate_sta_assoc(struct adapter * padapter,u8 * pmgmt_frame,uint frame_len)1921 void rtw_cfg80211_indicate_sta_assoc(struct adapter *padapter, u8 *pmgmt_frame, uint frame_len)
1922 {
1923 	struct net_device *ndev = padapter->pnetdev;
1924 
1925 	{
1926 		struct station_info sinfo = {};
1927 		u8 ie_offset;
1928 
1929 		if (GetFrameSubType(pmgmt_frame) == WIFI_ASSOCREQ)
1930 			ie_offset = _ASOCREQ_IE_OFFSET_;
1931 		else /*  WIFI_REASSOCREQ */
1932 			ie_offset = _REASOCREQ_IE_OFFSET_;
1933 
1934 		sinfo.filled = 0;
1935 		sinfo.assoc_req_ies = pmgmt_frame + WLAN_HDR_A3_LEN + ie_offset;
1936 		sinfo.assoc_req_ies_len = frame_len - WLAN_HDR_A3_LEN - ie_offset;
1937 		cfg80211_new_sta(ndev, GetAddr2Ptr(pmgmt_frame), &sinfo, GFP_ATOMIC);
1938 	}
1939 }
1940 
rtw_cfg80211_indicate_sta_disassoc(struct adapter * padapter,unsigned char * da,unsigned short reason)1941 void rtw_cfg80211_indicate_sta_disassoc(struct adapter *padapter, unsigned char *da, unsigned short reason)
1942 {
1943 	struct net_device *ndev = padapter->pnetdev;
1944 
1945 	cfg80211_del_sta(ndev, da, GFP_ATOMIC);
1946 }
1947 
rtw_get_chan_type(struct adapter * adapter)1948 static u8 rtw_get_chan_type(struct adapter *adapter)
1949 {
1950 	struct mlme_ext_priv *mlme_ext = &adapter->mlmeextpriv;
1951 
1952 	switch (mlme_ext->cur_bwmode) {
1953 	case CHANNEL_WIDTH_20:
1954 		if (is_supported_ht(adapter->registrypriv.wireless_mode))
1955 			return NL80211_CHAN_HT20;
1956 		else
1957 			return NL80211_CHAN_NO_HT;
1958 	case CHANNEL_WIDTH_40:
1959 		if (mlme_ext->cur_ch_offset == HAL_PRIME_CHNL_OFFSET_UPPER)
1960 			return NL80211_CHAN_HT40PLUS;
1961 		else
1962 			return NL80211_CHAN_HT40MINUS;
1963 	default:
1964 		return NL80211_CHAN_HT20;
1965 	}
1966 
1967 	return NL80211_CHAN_HT20;
1968 }
1969 
cfg80211_rtw_get_channel(struct wiphy * wiphy,struct wireless_dev * wdev,unsigned int link_id,struct cfg80211_chan_def * chandef)1970 static int cfg80211_rtw_get_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
1971 				    unsigned int link_id,
1972 				    struct cfg80211_chan_def *chandef)
1973 {
1974 	struct adapter *adapter = wiphy_to_adapter(wiphy);
1975 	struct registry_priv *registrypriv = &adapter->registrypriv;
1976 	enum nl80211_channel_type chan_type;
1977 	struct ieee80211_channel *chan = NULL;
1978 	int channel;
1979 	int freq;
1980 
1981 	if (!adapter->rtw_wdev)
1982 		return -ENODEV;
1983 
1984 	channel = rtw_get_oper_ch(adapter);
1985 	if (!channel)
1986 		return -ENODATA;
1987 
1988 	freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
1989 
1990 	chan = ieee80211_get_channel(adapter->rtw_wdev->wiphy, freq);
1991 
1992 	if (registrypriv->ht_enable) {
1993 		chan_type = rtw_get_chan_type(adapter);
1994 		cfg80211_chandef_create(chandef, chan, chan_type);
1995 	} else {
1996 		cfg80211_chandef_create(chandef, chan, NL80211_CHAN_NO_HT);
1997 	}
1998 
1999 	return 0;
2000 }
2001 
rtw_cfg80211_monitor_if_xmit_entry(struct sk_buff * skb,struct net_device * ndev)2002 static netdev_tx_t rtw_cfg80211_monitor_if_xmit_entry(struct sk_buff *skb, struct net_device *ndev)
2003 {
2004 	int rtap_len;
2005 	int qos_len = 0;
2006 	int dot11_hdr_len = 24;
2007 	int snap_len = 6;
2008 	unsigned char *pdata;
2009 	u16 frame_control;
2010 	unsigned char src_mac_addr[6];
2011 	unsigned char dst_mac_addr[6];
2012 	struct ieee80211_hdr *dot11_hdr;
2013 	struct ieee80211_radiotap_header *rtap_hdr;
2014 	struct adapter *padapter = rtw_netdev_priv(ndev);
2015 
2016 	if (!skb)
2017 		goto fail;
2018 
2019 	if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2020 		goto fail;
2021 
2022 	rtap_hdr = (struct ieee80211_radiotap_header *)skb->data;
2023 	if (unlikely(rtap_hdr->it_version))
2024 		goto fail;
2025 
2026 	rtap_len = ieee80211_get_radiotap_len(skb->data);
2027 	if (unlikely(skb->len < rtap_len))
2028 		goto fail;
2029 
2030 	if (rtap_len != 14)
2031 		goto fail;
2032 
2033 	/* Skip the ratio tap header */
2034 	skb_pull(skb, rtap_len);
2035 
2036 	dot11_hdr = (struct ieee80211_hdr *)skb->data;
2037 	frame_control = le16_to_cpu(dot11_hdr->frame_control);
2038 	/* Check if the QoS bit is set */
2039 	if ((frame_control & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) {
2040 		/* Check if this ia a Wireless Distribution System (WDS) frame
2041 		 * which has 4 MAC addresses
2042 		 */
2043 		if (frame_control & 0x0080)
2044 			qos_len = 2;
2045 		if ((frame_control & 0x0300) == 0x0300)
2046 			dot11_hdr_len += 6;
2047 
2048 		memcpy(dst_mac_addr, dot11_hdr->addr1, sizeof(dst_mac_addr));
2049 		memcpy(src_mac_addr, dot11_hdr->addr2, sizeof(src_mac_addr));
2050 
2051 		/* Skip the 802.11 header, QoS (if any) and SNAP, but leave spaces for
2052 		 * two MAC addresses
2053 		 */
2054 		skb_pull(skb, dot11_hdr_len + qos_len + snap_len - sizeof(src_mac_addr) * 2);
2055 		pdata = (unsigned char *)skb->data;
2056 		memcpy(pdata, dst_mac_addr, sizeof(dst_mac_addr));
2057 		memcpy(pdata + sizeof(dst_mac_addr), src_mac_addr, sizeof(src_mac_addr));
2058 
2059 		/* Use the real net device to transmit the packet */
2060 		_rtw_xmit_entry(skb, padapter->pnetdev);
2061 		return NETDEV_TX_OK;
2062 
2063 	} else if ((frame_control & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) ==
2064 		   (IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION)) {
2065 		/* only for action frames */
2066 		struct xmit_frame		*pmgntframe;
2067 		struct pkt_attrib	*pattrib;
2068 		unsigned char *pframe;
2069 		/* u8 category, action, OUI_Subtype, dialogToken = 0; */
2070 		/* unsigned char *frame_body; */
2071 		struct ieee80211_hdr *pwlanhdr;
2072 		struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
2073 		struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
2074 		u8 *buf = skb->data;
2075 		u32 len = skb->len;
2076 		u8 category, action;
2077 
2078 		if (rtw_action_frame_parse(buf, len, &category, &action) == false)
2079 			goto fail;
2080 
2081 		/* starting alloc mgmt frame to dump it */
2082 		pmgntframe = alloc_mgtxmitframe(pxmitpriv);
2083 		if (!pmgntframe)
2084 			goto fail;
2085 
2086 		/* update attribute */
2087 		pattrib = &pmgntframe->attrib;
2088 		update_mgntframe_attrib(padapter, pattrib);
2089 		pattrib->retry_ctrl = false;
2090 
2091 		memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
2092 
2093 		pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
2094 
2095 		memcpy(pframe, (void *)buf, len);
2096 		pattrib->pktlen = len;
2097 
2098 		pwlanhdr = (struct ieee80211_hdr *)pframe;
2099 		/* update seq number */
2100 		pmlmeext->mgnt_seq = GetSequence(pwlanhdr);
2101 		pattrib->seqnum = pmlmeext->mgnt_seq;
2102 		pmlmeext->mgnt_seq++;
2103 
2104 		pattrib->last_txcmdsz = pattrib->pktlen;
2105 
2106 		dump_mgntframe(padapter, pmgntframe);
2107 	}
2108 
2109 fail:
2110 
2111 	dev_kfree_skb_any(skb);
2112 
2113 	return NETDEV_TX_OK;
2114 }
2115 
2116 static const struct net_device_ops rtw_cfg80211_monitor_if_ops = {
2117 	.ndo_start_xmit = rtw_cfg80211_monitor_if_xmit_entry,
2118 };
2119 
rtw_cfg80211_add_monitor_if(struct adapter * padapter,char * name,struct net_device ** ndev)2120 static int rtw_cfg80211_add_monitor_if(struct adapter *padapter, char *name, struct net_device **ndev)
2121 {
2122 	int ret = 0;
2123 	struct net_device *mon_ndev = NULL;
2124 	struct wireless_dev *mon_wdev = NULL;
2125 	struct rtw_netdev_priv_indicator *pnpi;
2126 	struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(padapter);
2127 
2128 	if (!name) {
2129 		ret = -EINVAL;
2130 		goto out;
2131 	}
2132 
2133 	if (pwdev_priv->pmon_ndev) {
2134 		ret = -EBUSY;
2135 		goto out;
2136 	}
2137 
2138 	mon_ndev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator));
2139 	if (!mon_ndev) {
2140 		ret = -ENOMEM;
2141 		goto out;
2142 	}
2143 
2144 	mon_ndev->type = ARPHRD_IEEE80211_RADIOTAP;
2145 	strscpy(mon_ndev->name, name);
2146 	mon_ndev->needs_free_netdev = true;
2147 	mon_ndev->priv_destructor = rtw_ndev_destructor;
2148 
2149 	mon_ndev->netdev_ops = &rtw_cfg80211_monitor_if_ops;
2150 
2151 	pnpi = netdev_priv(mon_ndev);
2152 	pnpi->priv = padapter;
2153 	pnpi->sizeof_priv = sizeof(struct adapter);
2154 
2155 	/*  wdev */
2156 	mon_wdev = rtw_zmalloc(sizeof(struct wireless_dev));
2157 	if (!mon_wdev) {
2158 		ret = -ENOMEM;
2159 		goto out;
2160 	}
2161 
2162 	mon_wdev->wiphy = padapter->rtw_wdev->wiphy;
2163 	mon_wdev->netdev = mon_ndev;
2164 	mon_wdev->iftype = NL80211_IFTYPE_MONITOR;
2165 	mon_ndev->ieee80211_ptr = mon_wdev;
2166 
2167 	ret = cfg80211_register_netdevice(mon_ndev);
2168 	if (ret)
2169 		goto out;
2170 
2171 	*ndev = pwdev_priv->pmon_ndev = mon_ndev;
2172 	memcpy(pwdev_priv->ifname_mon, name, IFNAMSIZ + 1);
2173 
2174 out:
2175 	if (ret && mon_wdev) {
2176 		kfree(mon_wdev);
2177 		mon_wdev = NULL;
2178 	}
2179 
2180 	if (ret && mon_ndev) {
2181 		free_netdev(mon_ndev);
2182 		*ndev = mon_ndev = NULL;
2183 	}
2184 
2185 	return ret;
2186 }
2187 
2188 static struct wireless_dev *
cfg80211_rtw_add_virtual_intf(struct wiphy * wiphy,const char * name,unsigned char name_assign_type,enum nl80211_iftype type,struct vif_params * params)2189 	cfg80211_rtw_add_virtual_intf(
2190 		struct wiphy *wiphy,
2191 		const char *name,
2192 		unsigned char name_assign_type,
2193 		enum nl80211_iftype type, struct vif_params *params)
2194 {
2195 	int ret = 0;
2196 	struct net_device *ndev = NULL;
2197 	struct adapter *padapter = wiphy_to_adapter(wiphy);
2198 
2199 	switch (type) {
2200 	case NL80211_IFTYPE_ADHOC:
2201 	case NL80211_IFTYPE_AP_VLAN:
2202 	case NL80211_IFTYPE_WDS:
2203 	case NL80211_IFTYPE_MESH_POINT:
2204 		ret = -ENODEV;
2205 		break;
2206 	case NL80211_IFTYPE_MONITOR:
2207 		ret = rtw_cfg80211_add_monitor_if(padapter, (char *)name, &ndev);
2208 		break;
2209 	case NL80211_IFTYPE_P2P_CLIENT:
2210 	case NL80211_IFTYPE_STATION:
2211 		ret = -ENODEV;
2212 		break;
2213 	case NL80211_IFTYPE_P2P_GO:
2214 	case NL80211_IFTYPE_AP:
2215 		ret = -ENODEV;
2216 		break;
2217 	default:
2218 		ret = -ENODEV;
2219 		break;
2220 	}
2221 
2222 	return ndev ? ndev->ieee80211_ptr : ERR_PTR(ret);
2223 }
2224 
cfg80211_rtw_del_virtual_intf(struct wiphy * wiphy,struct wireless_dev * wdev)2225 static int cfg80211_rtw_del_virtual_intf(struct wiphy *wiphy,
2226 					 struct wireless_dev *wdev
2227 )
2228 {
2229 	struct net_device *ndev = wdev_to_ndev(wdev);
2230 	int ret = 0;
2231 	struct adapter *adapter;
2232 	struct rtw_wdev_priv *pwdev_priv;
2233 
2234 	if (!ndev) {
2235 		ret = -EINVAL;
2236 		goto exit;
2237 	}
2238 
2239 	adapter = rtw_netdev_priv(ndev);
2240 	pwdev_priv = adapter_wdev_data(adapter);
2241 
2242 	cfg80211_unregister_netdevice(ndev);
2243 
2244 	if (ndev == pwdev_priv->pmon_ndev) {
2245 		pwdev_priv->pmon_ndev = NULL;
2246 		pwdev_priv->ifname_mon[0] = '\0';
2247 	}
2248 
2249 exit:
2250 	return ret;
2251 }
2252 
rtw_add_beacon(struct adapter * adapter,const u8 * head,size_t head_len,const u8 * tail,size_t tail_len)2253 static int rtw_add_beacon(struct adapter *adapter, const u8 *head, size_t head_len, const u8 *tail, size_t tail_len)
2254 {
2255 	int ret = 0;
2256 	u8 *pbuf = NULL;
2257 	uint len, wps_ielen = 0;
2258 	struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
2259 
2260 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != true)
2261 		return -EINVAL;
2262 
2263 	if (head_len < 24)
2264 		return -EINVAL;
2265 
2266 	pbuf = rtw_zmalloc(head_len + tail_len);
2267 	if (!pbuf)
2268 		return -ENOMEM;
2269 
2270 	memcpy(pbuf, (void *)head + 24, head_len - 24);/*  24 =beacon header len. */
2271 	memcpy(pbuf + head_len - 24, (void *)tail, tail_len);
2272 
2273 	len = head_len + tail_len - 24;
2274 
2275 	/* check wps ie if inclued */
2276 	rtw_get_wps_ie(pbuf + _FIXED_IE_LENGTH_, len - _FIXED_IE_LENGTH_, NULL, &wps_ielen);
2277 
2278 	/* pbss_network->ies will not include p2p_ie, wfd ie */
2279 	rtw_ies_remove_ie(pbuf, &len, _BEACON_IE_OFFSET_, WLAN_EID_VENDOR_SPECIFIC, P2P_OUI, 4);
2280 	rtw_ies_remove_ie(pbuf, &len, _BEACON_IE_OFFSET_, WLAN_EID_VENDOR_SPECIFIC, WFD_OUI, 4);
2281 
2282 	if (rtw_check_beacon_data(adapter, pbuf,  len) == _SUCCESS)
2283 		ret = 0;
2284 	else
2285 		ret = -EINVAL;
2286 
2287 	kfree(pbuf);
2288 
2289 	return ret;
2290 }
2291 
cfg80211_rtw_start_ap(struct wiphy * wiphy,struct net_device * ndev,struct cfg80211_ap_settings * settings)2292 static int cfg80211_rtw_start_ap(struct wiphy *wiphy, struct net_device *ndev,
2293 				 struct cfg80211_ap_settings *settings)
2294 {
2295 	int ret = 0;
2296 	struct adapter *adapter = rtw_netdev_priv(ndev);
2297 
2298 	ret = rtw_add_beacon(adapter, settings->beacon.head,
2299 			     settings->beacon.head_len, settings->beacon.tail,
2300 			     settings->beacon.tail_len);
2301 
2302 	adapter->mlmeextpriv.mlmext_info.hidden_ssid_mode = settings->hidden_ssid;
2303 
2304 	if (settings->ssid && settings->ssid_len) {
2305 		struct wlan_bssid_ex *pbss_network = &adapter->mlmepriv.cur_network.network;
2306 		struct wlan_bssid_ex *pbss_network_ext = &adapter->mlmeextpriv.mlmext_info.network;
2307 
2308 		memcpy(pbss_network->ssid.ssid, (void *)settings->ssid, settings->ssid_len);
2309 		pbss_network->ssid.ssid_length = settings->ssid_len;
2310 		memcpy(pbss_network_ext->ssid.ssid, (void *)settings->ssid, settings->ssid_len);
2311 		pbss_network_ext->ssid.ssid_length = settings->ssid_len;
2312 	}
2313 
2314 	return ret;
2315 }
2316 
cfg80211_rtw_change_beacon(struct wiphy * wiphy,struct net_device * ndev,struct cfg80211_ap_update * info)2317 static int cfg80211_rtw_change_beacon(struct wiphy *wiphy, struct net_device *ndev,
2318 		struct cfg80211_ap_update *info)
2319 {
2320 	struct adapter *adapter = rtw_netdev_priv(ndev);
2321 
2322 	return rtw_add_beacon(adapter, info->beacon.head,
2323 			      info->beacon.head_len, info->beacon.tail,
2324 			      info->beacon.tail_len);
2325 }
2326 
cfg80211_rtw_stop_ap(struct wiphy * wiphy,struct net_device * ndev,unsigned int link_id)2327 static int cfg80211_rtw_stop_ap(struct wiphy *wiphy, struct net_device *ndev,
2328 				unsigned int link_id)
2329 {
2330 	return 0;
2331 }
2332 
cfg80211_rtw_add_station(struct wiphy * wiphy,struct net_device * ndev,const u8 * mac,struct station_parameters * params)2333 static int	cfg80211_rtw_add_station(struct wiphy *wiphy,
2334 					 struct net_device *ndev,
2335 					 const u8 *mac,
2336 					 struct station_parameters *params)
2337 {
2338 	return 0;
2339 }
2340 
cfg80211_rtw_del_station(struct wiphy * wiphy,struct net_device * ndev,struct station_del_parameters * params)2341 static int cfg80211_rtw_del_station(struct wiphy *wiphy, struct net_device *ndev,
2342 				    struct station_del_parameters *params)
2343 {
2344 	int ret = 0;
2345 	struct list_head *phead, *plist, *tmp;
2346 	u8 updated = false;
2347 	struct sta_info *psta = NULL;
2348 	struct adapter *padapter = rtw_netdev_priv(ndev);
2349 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
2350 	struct sta_priv *pstapriv = &padapter->stapriv;
2351 	const u8 *mac = params->mac;
2352 
2353 	if (check_fwstate(pmlmepriv, (_FW_LINKED | WIFI_AP_STATE)) != true)
2354 		return -EINVAL;
2355 
2356 	if (!mac) {
2357 		flush_all_cam_entry(padapter);	/* clear CAM */
2358 
2359 		rtw_sta_flush(padapter);
2360 
2361 		return 0;
2362 	}
2363 
2364 	if (mac[0] == 0xff && mac[1] == 0xff &&
2365 	    mac[2] == 0xff && mac[3] == 0xff &&
2366 	    mac[4] == 0xff && mac[5] == 0xff) {
2367 		return -EINVAL;
2368 	}
2369 
2370 	spin_lock_bh(&pstapriv->asoc_list_lock);
2371 
2372 	phead = &pstapriv->asoc_list;
2373 	/* check asoc_queue */
2374 	list_for_each_safe(plist, tmp, phead) {
2375 		psta = list_entry(plist, struct sta_info, asoc_list);
2376 
2377 		if (!memcmp((u8 *)mac, psta->hwaddr, ETH_ALEN)) {
2378 			if (psta->dot8021xalg != 1 || psta->bpairwise_key_installed) {
2379 				list_del_init(&psta->asoc_list);
2380 				pstapriv->asoc_list_cnt--;
2381 
2382 				updated = ap_free_sta(padapter, psta, true, WLAN_REASON_DEAUTH_LEAVING);
2383 
2384 				psta = NULL;
2385 
2386 				break;
2387 			}
2388 		}
2389 	}
2390 
2391 	spin_unlock_bh(&pstapriv->asoc_list_lock);
2392 
2393 	associated_clients_update(padapter, updated);
2394 
2395 	return ret;
2396 }
2397 
cfg80211_rtw_change_station(struct wiphy * wiphy,struct net_device * ndev,const u8 * mac,struct station_parameters * params)2398 static int cfg80211_rtw_change_station(struct wiphy *wiphy,
2399 				       struct net_device *ndev,
2400 				       const u8 *mac,
2401 				       struct station_parameters *params)
2402 {
2403 	return 0;
2404 }
2405 
rtw_sta_info_get_by_idx(const int idx,struct sta_priv * pstapriv)2406 static struct sta_info *rtw_sta_info_get_by_idx(const int idx, struct sta_priv *pstapriv)
2407 
2408 {
2409 	struct list_head	*phead, *plist;
2410 	struct sta_info *psta = NULL;
2411 	int i = 0;
2412 
2413 	phead = &pstapriv->asoc_list;
2414 	plist = get_next(phead);
2415 
2416 	/* check asoc_queue */
2417 	while (phead != plist) {
2418 		if (idx == i)
2419 			psta = container_of(plist, struct sta_info, asoc_list);
2420 		plist = get_next(plist);
2421 		i++;
2422 	}
2423 	return psta;
2424 }
2425 
cfg80211_rtw_dump_station(struct wiphy * wiphy,struct net_device * ndev,int idx,u8 * mac,struct station_info * sinfo)2426 static int	cfg80211_rtw_dump_station(struct wiphy *wiphy,
2427 					  struct net_device *ndev,
2428 					  int idx, u8 *mac,
2429 					  struct station_info *sinfo)
2430 {
2431 	int ret = 0;
2432 	struct adapter *padapter = rtw_netdev_priv(ndev);
2433 	struct sta_info *psta = NULL;
2434 	struct sta_priv *pstapriv = &padapter->stapriv;
2435 
2436 	spin_lock_bh(&pstapriv->asoc_list_lock);
2437 	psta = rtw_sta_info_get_by_idx(idx, pstapriv);
2438 	spin_unlock_bh(&pstapriv->asoc_list_lock);
2439 	if (psta == NULL) {
2440 		ret = -ENOENT;
2441 		goto exit;
2442 	}
2443 	memcpy(mac, psta->hwaddr, ETH_ALEN);
2444 	sinfo->filled = BIT_ULL(NL80211_STA_INFO_SIGNAL);
2445 	sinfo->signal = psta->rssi;
2446 
2447 exit:
2448 	return ret;
2449 }
2450 
cfg80211_rtw_change_bss(struct wiphy * wiphy,struct net_device * ndev,struct bss_parameters * params)2451 static int	cfg80211_rtw_change_bss(struct wiphy *wiphy,
2452 					struct net_device *ndev,
2453 					struct bss_parameters *params)
2454 {
2455 	return 0;
2456 }
2457 
rtw_cfg80211_rx_action(struct adapter * adapter,u8 * frame,uint frame_len,const char * msg)2458 void rtw_cfg80211_rx_action(struct adapter *adapter, u8 *frame, uint frame_len, const char *msg)
2459 {
2460 	s32 freq;
2461 	int channel;
2462 	u8 category, action;
2463 
2464 	channel = rtw_get_oper_ch(adapter);
2465 
2466 	rtw_action_frame_parse(frame, frame_len, &category, &action);
2467 
2468 	freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
2469 
2470 	rtw_cfg80211_rx_mgmt(adapter, freq, 0, frame, frame_len, GFP_ATOMIC);
2471 }
2472 
_cfg80211_rtw_mgmt_tx(struct adapter * padapter,u8 tx_ch,const u8 * buf,size_t len)2473 static int _cfg80211_rtw_mgmt_tx(struct adapter *padapter, u8 tx_ch, const u8 *buf, size_t len)
2474 {
2475 	struct xmit_frame	*pmgntframe;
2476 	struct pkt_attrib	*pattrib;
2477 	unsigned char *pframe;
2478 	int ret = _FAIL;
2479 	bool __maybe_unused ack = true;
2480 	struct ieee80211_hdr *pwlanhdr;
2481 	struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
2482 	struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
2483 
2484 	rtw_set_scan_deny(padapter, 1000);
2485 
2486 	rtw_scan_abort(padapter);
2487 	if (tx_ch != rtw_get_oper_ch(padapter)) {
2488 		if (!check_fwstate(&padapter->mlmepriv, _FW_LINKED))
2489 			pmlmeext->cur_channel = tx_ch;
2490 		set_channel_bwmode(padapter, tx_ch, HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20);
2491 	}
2492 
2493 	/* starting alloc mgmt frame to dump it */
2494 	pmgntframe = alloc_mgtxmitframe(pxmitpriv);
2495 	if (!pmgntframe) {
2496 		/* ret = -ENOMEM; */
2497 		ret = _FAIL;
2498 		goto exit;
2499 	}
2500 
2501 	/* update attribute */
2502 	pattrib = &pmgntframe->attrib;
2503 	update_mgntframe_attrib(padapter, pattrib);
2504 	pattrib->retry_ctrl = false;
2505 
2506 	memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
2507 
2508 	pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
2509 
2510 	memcpy(pframe, (void *)buf, len);
2511 	pattrib->pktlen = len;
2512 
2513 	pwlanhdr = (struct ieee80211_hdr *)pframe;
2514 	/* update seq number */
2515 	pmlmeext->mgnt_seq = GetSequence(pwlanhdr);
2516 	pattrib->seqnum = pmlmeext->mgnt_seq;
2517 	pmlmeext->mgnt_seq++;
2518 
2519 	pattrib->last_txcmdsz = pattrib->pktlen;
2520 
2521 	if (dump_mgntframe_and_wait_ack(padapter, pmgntframe) != _SUCCESS) {
2522 		ack = false;
2523 		ret = _FAIL;
2524 
2525 	} else {
2526 		msleep(50);
2527 
2528 		ret = _SUCCESS;
2529 	}
2530 
2531 exit:
2532 
2533 	return ret;
2534 }
2535 
cfg80211_rtw_mgmt_tx(struct wiphy * wiphy,struct wireless_dev * wdev,struct cfg80211_mgmt_tx_params * params,u64 * cookie)2536 static int cfg80211_rtw_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
2537 				struct cfg80211_mgmt_tx_params *params,
2538 				u64 *cookie)
2539 {
2540 	struct net_device *ndev = wdev_to_ndev(wdev);
2541 	struct ieee80211_channel *chan = params->chan;
2542 	const u8 *buf = params->buf;
2543 	size_t len = params->len;
2544 	int ret = 0;
2545 	int tx_ret;
2546 	u32 dump_limit = RTW_MAX_MGMT_TX_CNT;
2547 	u32 dump_cnt = 0;
2548 	bool ack = true;
2549 	u8 tx_ch = (u8)ieee80211_frequency_to_channel(chan->center_freq);
2550 	u8 category, action;
2551 	struct adapter *padapter;
2552 
2553 	if (!ndev) {
2554 		ret = -EINVAL;
2555 		goto exit;
2556 	}
2557 
2558 	padapter = rtw_netdev_priv(ndev);
2559 
2560 	/* cookie generation */
2561 	*cookie = (unsigned long)buf;
2562 
2563 	/* indicate ack before issue frame to avoid racing with rsp frame */
2564 	rtw_cfg80211_mgmt_tx_status(padapter, *cookie, buf, len, ack, GFP_KERNEL);
2565 
2566 	if (rtw_action_frame_parse(buf, len, &category, &action) == false)
2567 		goto exit;
2568 
2569 	rtw_ps_deny(padapter, PS_DENY_MGNT_TX);
2570 	if (rtw_pwr_wakeup(padapter) == _FAIL) {
2571 		ret = -EFAULT;
2572 		goto cancel_ps_deny;
2573 	}
2574 
2575 	do {
2576 		dump_cnt++;
2577 		tx_ret = _cfg80211_rtw_mgmt_tx(padapter, tx_ch, buf, len);
2578 	} while (dump_cnt < dump_limit && tx_ret != _SUCCESS);
2579 
2580 cancel_ps_deny:
2581 	rtw_ps_deny_cancel(padapter, PS_DENY_MGNT_TX);
2582 exit:
2583 	return ret;
2584 }
2585 
rtw_cfg80211_init_ht_capab(struct ieee80211_sta_ht_cap * ht_cap,enum nl80211_band band)2586 static void rtw_cfg80211_init_ht_capab(struct ieee80211_sta_ht_cap *ht_cap, enum nl80211_band band)
2587 {
2588 #define MAX_BIT_RATE_40MHZ_MCS7		150	/* Mbps */
2589 
2590 	ht_cap->ht_supported = true;
2591 
2592 	ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2593 					IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_SGI_20 |
2594 					IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
2595 
2596 	/*
2597 	 *Maximum length of AMPDU that the STA can receive.
2598 	 *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
2599 	 */
2600 	ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2601 
2602 	/*Minimum MPDU start spacing , */
2603 	ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
2604 
2605 	ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2606 
2607 	/*
2608 	 *hw->wiphy->bands[NL80211_BAND_2GHZ]
2609 	 *base on ant_num
2610 	 *rx_mask: RX mask
2611 	 *if rx_ant = 1 rx_mask[0]= 0xff;==>MCS0-MCS7
2612 	 *if rx_ant =2 rx_mask[1]= 0xff;==>MCS8-MCS15
2613 	 *if rx_ant >=3 rx_mask[2]= 0xff;
2614 	 *if BW_40 rx_mask[4]= 0x01;
2615 	 *highest supported RX rate
2616 	 */
2617 	ht_cap->mcs.rx_mask[0] = 0xFF;
2618 	ht_cap->mcs.rx_mask[1] = 0x00;
2619 	ht_cap->mcs.rx_mask[4] = 0x01;
2620 
2621 	ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
2622 }
2623 
rtw_cfg80211_init_wiphy(struct adapter * padapter)2624 void rtw_cfg80211_init_wiphy(struct adapter *padapter)
2625 {
2626 	struct ieee80211_supported_band *bands;
2627 	struct wireless_dev *pwdev = padapter->rtw_wdev;
2628 	struct wiphy *wiphy = pwdev->wiphy;
2629 
2630 	{
2631 		bands = wiphy->bands[NL80211_BAND_2GHZ];
2632 		if (bands)
2633 			rtw_cfg80211_init_ht_capab(&bands->ht_cap, NL80211_BAND_2GHZ);
2634 	}
2635 
2636 	/* copy mac_addr to wiphy */
2637 	memcpy(wiphy->perm_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
2638 }
2639 
rtw_cfg80211_preinit_wiphy(struct adapter * padapter,struct wiphy * wiphy)2640 static void rtw_cfg80211_preinit_wiphy(struct adapter *padapter, struct wiphy *wiphy)
2641 {
2642 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
2643 
2644 	wiphy->max_scan_ssids = RTW_SSID_SCAN_AMOUNT;
2645 	wiphy->max_scan_ie_len = RTW_SCAN_IE_LEN_MAX;
2646 	wiphy->max_num_pmkids = RTW_MAX_NUM_PMKIDS;
2647 
2648 	wiphy->max_remain_on_channel_duration = RTW_MAX_REMAIN_ON_CHANNEL_DURATION;
2649 
2650 	wiphy->interface_modes =	BIT(NL80211_IFTYPE_STATION)
2651 								| BIT(NL80211_IFTYPE_ADHOC)
2652 								| BIT(NL80211_IFTYPE_AP)
2653 								| BIT(NL80211_IFTYPE_MONITOR)
2654 								;
2655 
2656 	wiphy->mgmt_stypes = rtw_cfg80211_default_mgmt_stypes;
2657 
2658 	wiphy->software_iftypes |= BIT(NL80211_IFTYPE_MONITOR);
2659 
2660 	wiphy->cipher_suites = rtw_cipher_suites;
2661 	wiphy->n_cipher_suites = ARRAY_SIZE(rtw_cipher_suites);
2662 
2663 	/* if (padapter->registrypriv.wireless_mode & WIRELESS_11G) */
2664 	wiphy->bands[NL80211_BAND_2GHZ] = rtw_spt_band_alloc(NL80211_BAND_2GHZ);
2665 
2666 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
2667 	wiphy->flags |= WIPHY_FLAG_OFFCHAN_TX | WIPHY_FLAG_HAVE_AP_SME;
2668 
2669 #if defined(CONFIG_PM)
2670 	wiphy->max_sched_scan_reqs = 1;
2671 #endif
2672 
2673 #if defined(CONFIG_PM)
2674 	wiphy->wowlan = &wowlan_stub;
2675 #endif
2676 
2677 	if (padapter->registrypriv.power_mgnt != PS_MODE_ACTIVE)
2678 		wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
2679 	else
2680 		wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
2681 }
2682 
2683 static struct cfg80211_ops rtw_cfg80211_ops = {
2684 	.change_virtual_intf = cfg80211_rtw_change_iface,
2685 	.add_key = cfg80211_rtw_add_key,
2686 	.get_key = cfg80211_rtw_get_key,
2687 	.del_key = cfg80211_rtw_del_key,
2688 	.set_default_key = cfg80211_rtw_set_default_key,
2689 	.get_station = cfg80211_rtw_get_station,
2690 	.scan = cfg80211_rtw_scan,
2691 	.set_wiphy_params = cfg80211_rtw_set_wiphy_params,
2692 	.connect = cfg80211_rtw_connect,
2693 	.disconnect = cfg80211_rtw_disconnect,
2694 	.join_ibss = cfg80211_rtw_join_ibss,
2695 	.leave_ibss = cfg80211_rtw_leave_ibss,
2696 	.set_tx_power = cfg80211_rtw_set_txpower,
2697 	.get_tx_power = cfg80211_rtw_get_txpower,
2698 	.set_power_mgmt = cfg80211_rtw_set_power_mgmt,
2699 	.set_pmksa = cfg80211_rtw_set_pmksa,
2700 	.del_pmksa = cfg80211_rtw_del_pmksa,
2701 	.flush_pmksa = cfg80211_rtw_flush_pmksa,
2702 	.get_channel = cfg80211_rtw_get_channel,
2703 	.add_virtual_intf = cfg80211_rtw_add_virtual_intf,
2704 	.del_virtual_intf = cfg80211_rtw_del_virtual_intf,
2705 
2706 	.start_ap = cfg80211_rtw_start_ap,
2707 	.change_beacon = cfg80211_rtw_change_beacon,
2708 	.stop_ap = cfg80211_rtw_stop_ap,
2709 
2710 	.add_station = cfg80211_rtw_add_station,
2711 	.del_station = cfg80211_rtw_del_station,
2712 	.change_station = cfg80211_rtw_change_station,
2713 	.dump_station = cfg80211_rtw_dump_station,
2714 	.change_bss = cfg80211_rtw_change_bss,
2715 
2716 	.mgmt_tx = cfg80211_rtw_mgmt_tx,
2717 };
2718 
rtw_wdev_alloc(struct adapter * padapter,struct device * dev)2719 int rtw_wdev_alloc(struct adapter *padapter, struct device *dev)
2720 {
2721 	int ret = 0;
2722 	struct wiphy *wiphy;
2723 	struct wireless_dev *wdev;
2724 	struct rtw_wdev_priv *pwdev_priv;
2725 	struct net_device *pnetdev = padapter->pnetdev;
2726 
2727 	/* wiphy */
2728 	wiphy = wiphy_new(&rtw_cfg80211_ops, sizeof(struct adapter *));
2729 	if (!wiphy) {
2730 		ret = -ENOMEM;
2731 		goto exit;
2732 	}
2733 	set_wiphy_dev(wiphy, dev);
2734 	*((struct adapter **)wiphy_priv(wiphy)) = padapter;
2735 	rtw_cfg80211_preinit_wiphy(padapter, wiphy);
2736 
2737 	/* init regulary domain */
2738 	rtw_regd_init(wiphy, rtw_reg_notifier);
2739 
2740 	ret = wiphy_register(wiphy);
2741 	if (ret < 0)
2742 		goto free_wiphy;
2743 
2744 	/*  wdev */
2745 	wdev = rtw_zmalloc(sizeof(struct wireless_dev));
2746 	if (!wdev) {
2747 		ret = -ENOMEM;
2748 		goto unregister_wiphy;
2749 	}
2750 	wdev->wiphy = wiphy;
2751 	wdev->netdev = pnetdev;
2752 
2753 	wdev->iftype = NL80211_IFTYPE_STATION; /*  will be init in rtw_hal_init() */
2754 					   /*  Must sync with _rtw_init_mlme_priv() */
2755 					   /*  pmlmepriv->fw_state = WIFI_STATION_STATE */
2756 	padapter->rtw_wdev = wdev;
2757 	pnetdev->ieee80211_ptr = wdev;
2758 
2759 	/* init pwdev_priv */
2760 	pwdev_priv = adapter_wdev_data(padapter);
2761 	pwdev_priv->rtw_wdev = wdev;
2762 	pwdev_priv->pmon_ndev = NULL;
2763 	pwdev_priv->ifname_mon[0] = '\0';
2764 	pwdev_priv->padapter = padapter;
2765 	pwdev_priv->scan_request = NULL;
2766 	spin_lock_init(&pwdev_priv->scan_req_lock);
2767 
2768 	pwdev_priv->p2p_enabled = false;
2769 	pwdev_priv->provdisc_req_issued = false;
2770 	rtw_wdev_invit_info_init(&pwdev_priv->invit_info);
2771 	rtw_wdev_nego_info_init(&pwdev_priv->nego_info);
2772 
2773 	pwdev_priv->bandroid_scan = false;
2774 
2775 	if (padapter->registrypriv.power_mgnt != PS_MODE_ACTIVE)
2776 		pwdev_priv->power_mgmt = true;
2777 	else
2778 		pwdev_priv->power_mgmt = false;
2779 
2780 	return ret;
2781 
2782 unregister_wiphy:
2783 	wiphy_unregister(wiphy);
2784  free_wiphy:
2785 	wiphy_free(wiphy);
2786 exit:
2787 	return ret;
2788 }
2789 
rtw_wdev_free(struct wireless_dev * wdev)2790 void rtw_wdev_free(struct wireless_dev *wdev)
2791 {
2792 	if (!wdev)
2793 		return;
2794 
2795 	kfree(wdev->wiphy->bands[NL80211_BAND_2GHZ]);
2796 
2797 	wiphy_free(wdev->wiphy);
2798 
2799 	kfree(wdev);
2800 }
2801 
rtw_wdev_unregister(struct wireless_dev * wdev)2802 void rtw_wdev_unregister(struct wireless_dev *wdev)
2803 {
2804 	struct net_device *ndev;
2805 	struct adapter *adapter;
2806 	struct rtw_wdev_priv *pwdev_priv;
2807 
2808 	if (!wdev)
2809 		return;
2810 	ndev = wdev_to_ndev(wdev);
2811 	if (!ndev)
2812 		return;
2813 
2814 	adapter = rtw_netdev_priv(ndev);
2815 	pwdev_priv = adapter_wdev_data(adapter);
2816 
2817 	rtw_cfg80211_indicate_scan_done(adapter, true);
2818 
2819 	if (pwdev_priv->pmon_ndev)
2820 		unregister_netdev(pwdev_priv->pmon_ndev);
2821 
2822 	wiphy_unregister(wdev->wiphy);
2823 }
2824