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