1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 
8 #include <linux/etherdevice.h>
9 #include <drv_types.h>
10 #include <linux/jiffies.h>
11 
12 #include <rtw_wifi_regd.h>
13 
14 #define RTW_MAX_MGMT_TX_CNT (8)
15 
16 #define RTW_SCAN_IE_LEN_MAX      2304
17 #define RTW_MAX_REMAIN_ON_CHANNEL_DURATION 5000 /* ms */
18 #define RTW_MAX_NUM_PMKIDS 4
19 
20 static const u32 rtw_cipher_suites[] = {
21 	WLAN_CIPHER_SUITE_WEP40,
22 	WLAN_CIPHER_SUITE_WEP104,
23 	WLAN_CIPHER_SUITE_TKIP,
24 	WLAN_CIPHER_SUITE_CCMP,
25 	WLAN_CIPHER_SUITE_AES_CMAC,
26 };
27 
28 #define RATETAB_ENT(_rate, _rateid, _flags) \
29 	{								\
30 		.bitrate	= (_rate),				\
31 		.hw_value	= (_rateid),				\
32 		.flags		= (_flags),				\
33 	}
34 
35 #define CHAN2G(_channel, _freq, _flags) {			\
36 	.band			= NL80211_BAND_2GHZ,		\
37 	.center_freq		= (_freq),			\
38 	.hw_value		= (_channel),			\
39 	.flags			= (_flags),			\
40 	.max_antenna_gain	= 0,				\
41 	.max_power		= 30,				\
42 }
43 
44 /* if wowlan is not supported, kernel generate a disconnect at each suspend
45  * cf: /net/wireless/sysfs.c, so register a stub wowlan.
46  * Moreover wowlan has to be enabled via a the nl80211_set_wowlan callback.
47  * (from user space, e.g. iw phy0 wowlan enable)
48  */
49 static __maybe_unused const struct wiphy_wowlan_support wowlan_stub = {
50 	.flags = WIPHY_WOWLAN_ANY,
51 	.n_patterns = 0,
52 	.pattern_max_len = 0,
53 	.pattern_min_len = 0,
54 	.max_pkt_offset = 0,
55 };
56 
57 static struct ieee80211_rate rtw_rates[] = {
58 	RATETAB_ENT(10,  0x1,   0),
59 	RATETAB_ENT(20,  0x2,   0),
60 	RATETAB_ENT(55,  0x4,   0),
61 	RATETAB_ENT(110, 0x8,   0),
62 	RATETAB_ENT(60,  0x10,  0),
63 	RATETAB_ENT(90,  0x20,  0),
64 	RATETAB_ENT(120, 0x40,  0),
65 	RATETAB_ENT(180, 0x80,  0),
66 	RATETAB_ENT(240, 0x100, 0),
67 	RATETAB_ENT(360, 0x200, 0),
68 	RATETAB_ENT(480, 0x400, 0),
69 	RATETAB_ENT(540, 0x800, 0),
70 };
71 
72 #define rtw_g_rates		(rtw_rates + 0)
73 #define RTW_G_RATES_NUM	12
74 
75 #define RTW_2G_CHANNELS_NUM 14
76 
77 static struct ieee80211_channel rtw_2ghz_channels[] = {
78 	CHAN2G(1, 2412, 0),
79 	CHAN2G(2, 2417, 0),
80 	CHAN2G(3, 2422, 0),
81 	CHAN2G(4, 2427, 0),
82 	CHAN2G(5, 2432, 0),
83 	CHAN2G(6, 2437, 0),
84 	CHAN2G(7, 2442, 0),
85 	CHAN2G(8, 2447, 0),
86 	CHAN2G(9, 2452, 0),
87 	CHAN2G(10, 2457, 0),
88 	CHAN2G(11, 2462, 0),
89 	CHAN2G(12, 2467, 0),
90 	CHAN2G(13, 2472, 0),
91 	CHAN2G(14, 2484, 0),
92 };
93 
rtw_2g_channels_init(struct ieee80211_channel * channels)94 static void rtw_2g_channels_init(struct ieee80211_channel *channels)
95 {
96 	memcpy((void *)channels, (void *)rtw_2ghz_channels,
97 	       sizeof(struct ieee80211_channel) * RTW_2G_CHANNELS_NUM
98 	);
99 }
100 
rtw_2g_rates_init(struct ieee80211_rate * rates)101 static void rtw_2g_rates_init(struct ieee80211_rate *rates)
102 {
103 	memcpy(rates, rtw_g_rates,
104 	       sizeof(struct ieee80211_rate) * RTW_G_RATES_NUM
105 	);
106 }
107 
rtw_spt_band_alloc(enum nl80211_band band)108 static struct ieee80211_supported_band *rtw_spt_band_alloc(
109 	enum nl80211_band band
110 	)
111 {
112 	struct ieee80211_supported_band *spt_band = NULL;
113 	int n_channels, n_bitrates;
114 
115 	if (band == NL80211_BAND_2GHZ) {
116 		n_channels = RTW_2G_CHANNELS_NUM;
117 		n_bitrates = RTW_G_RATES_NUM;
118 	} else {
119 		goto exit;
120 	}
121 
122 	spt_band = rtw_zmalloc(sizeof(struct ieee80211_supported_band) +
123 			       sizeof(struct ieee80211_channel) * n_channels +
124 			       sizeof(struct ieee80211_rate) * n_bitrates);
125 	if (!spt_band)
126 		goto exit;
127 
128 	spt_band->channels = (struct ieee80211_channel *)(((u8 *)spt_band) + sizeof(struct ieee80211_supported_band));
129 	spt_band->bitrates = (struct ieee80211_rate *)(((u8 *)spt_band->channels) + sizeof(struct ieee80211_channel) * n_channels);
130 	spt_band->band = band;
131 	spt_band->n_channels = n_channels;
132 	spt_band->n_bitrates = n_bitrates;
133 
134 	if (band == NL80211_BAND_2GHZ) {
135 		rtw_2g_channels_init(spt_band->channels);
136 		rtw_2g_rates_init(spt_band->bitrates);
137 	}
138 
139 	/* spt_band.ht_cap */
140 
141 exit:
142 
143 	return spt_band;
144 }
145 
146 static const struct ieee80211_txrx_stypes
147 rtw_cfg80211_default_mgmt_stypes[NUM_NL80211_IFTYPES] = {
148 	[NL80211_IFTYPE_ADHOC] = {
149 		.tx = 0xffff,
150 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4)
151 	},
152 	[NL80211_IFTYPE_STATION] = {
153 		.tx = 0xffff,
154 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
155 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
156 	},
157 	[NL80211_IFTYPE_AP] = {
158 		.tx = 0xffff,
159 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
160 		BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
161 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
162 		BIT(IEEE80211_STYPE_DISASSOC >> 4) |
163 		BIT(IEEE80211_STYPE_AUTH >> 4) |
164 		BIT(IEEE80211_STYPE_DEAUTH >> 4) |
165 		BIT(IEEE80211_STYPE_ACTION >> 4)
166 	},
167 	[NL80211_IFTYPE_AP_VLAN] = {
168 		/* copy AP */
169 		.tx = 0xffff,
170 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
171 		BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
172 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
173 		BIT(IEEE80211_STYPE_DISASSOC >> 4) |
174 		BIT(IEEE80211_STYPE_AUTH >> 4) |
175 		BIT(IEEE80211_STYPE_DEAUTH >> 4) |
176 		BIT(IEEE80211_STYPE_ACTION >> 4)
177 	},
178 	[NL80211_IFTYPE_P2P_CLIENT] = {
179 		.tx = 0xffff,
180 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
181 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
182 	},
183 	[NL80211_IFTYPE_P2P_GO] = {
184 		.tx = 0xffff,
185 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
186 		BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
187 		BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
188 		BIT(IEEE80211_STYPE_DISASSOC >> 4) |
189 		BIT(IEEE80211_STYPE_AUTH >> 4) |
190 		BIT(IEEE80211_STYPE_DEAUTH >> 4) |
191 		BIT(IEEE80211_STYPE_ACTION >> 4)
192 	},
193 };
194 
rtw_ieee80211_channel_to_frequency(int chan,int band)195 static int rtw_ieee80211_channel_to_frequency(int chan, int band)
196 {
197 	if (band == NL80211_BAND_2GHZ) {
198 		if (chan == 14)
199 			return 2484;
200 		else if (chan < 14)
201 			return 2407 + chan * 5;
202 	}
203 
204 	return 0; /* not supported */
205 }
206 
207 #define MAX_BSSINFO_LEN 1000
rtw_cfg80211_inform_bss(struct adapter * padapter,struct wlan_network * pnetwork)208 struct cfg80211_bss *rtw_cfg80211_inform_bss(struct adapter *padapter, struct wlan_network *pnetwork)
209 {
210 	struct ieee80211_channel *notify_channel;
211 	struct cfg80211_bss *bss = NULL;
212 	/* struct ieee80211_supported_band *band; */
213 	u16 channel;
214 	u32 freq;
215 	u64 notify_timestamp;
216 	s32 notify_signal;
217 	u8 *buf = NULL, *pbuf;
218 	size_t len, bssinf_len = 0;
219 	struct ieee80211_hdr *pwlanhdr;
220 	__le16 *fctrl;
221 
222 	struct wireless_dev *wdev = padapter->rtw_wdev;
223 	struct wiphy *wiphy = wdev->wiphy;
224 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
225 
226 	bssinf_len = pnetwork->network.ie_length + sizeof(struct ieee80211_hdr_3addr);
227 	if (bssinf_len > MAX_BSSINFO_LEN)
228 		goto exit;
229 
230 	{
231 		u16 wapi_len = 0;
232 
233 		if (rtw_get_wapi_ie(pnetwork->network.ies, pnetwork->network.ie_length, NULL, &wapi_len) > 0) {
234 			if (wapi_len > 0)
235 				goto exit;
236 		}
237 	}
238 
239 	/* To reduce PBC Overlap rate */
240 	/* spin_lock_bh(&pwdev_priv->scan_req_lock); */
241 	if (adapter_wdev_data(padapter)->scan_request) {
242 		u8 *psr = NULL, sr = 0;
243 		struct ndis_802_11_ssid *pssid = &pnetwork->network.ssid;
244 		struct cfg80211_scan_request *request = adapter_wdev_data(padapter)->scan_request;
245 		struct cfg80211_ssid *ssids = request->ssids;
246 		u32 wpsielen = 0;
247 		u8 *wpsie = NULL;
248 
249 		wpsie = rtw_get_wps_ie(pnetwork->network.ies + _FIXED_IE_LENGTH_, pnetwork->network.ie_length - _FIXED_IE_LENGTH_, NULL, &wpsielen);
250 
251 		if (wpsie && wpsielen > 0)
252 			psr = rtw_get_wps_attr_content(wpsie, wpsielen, WPS_ATTR_SELECTED_REGISTRAR, (u8 *)(&sr), NULL);
253 
254 		if (sr != 0) {
255 			/* it means under processing WPS */
256 			if (request->n_ssids == 1 && request->n_channels == 1) {
257 				if (ssids[0].ssid_len != 0 &&
258 				    (pssid->ssid_length != ssids[0].ssid_len ||
259 				     memcmp(pssid->ssid, ssids[0].ssid, ssids[0].ssid_len))) {
260 					if (psr)
261 						*psr = 0; /* clear sr */
262 				}
263 			}
264 		}
265 	}
266 	/* spin_unlock_bh(&pwdev_priv->scan_req_lock); */
267 
268 	channel = pnetwork->network.configuration.ds_config;
269 	freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
270 
271 	notify_channel = ieee80211_get_channel(wiphy, freq);
272 
273 	notify_timestamp = ktime_to_us(ktime_get_boottime());
274 
275 	/* We've set wiphy's signal_type as CFG80211_SIGNAL_TYPE_MBM: signal strength in mBm (100*dBm) */
276 	if (check_fwstate(pmlmepriv, _FW_LINKED) == true &&
277 	    is_same_network(&pmlmepriv->cur_network.network, &pnetwork->network, 0)) {
278 		notify_signal = 100 * translate_percentage_to_dbm(padapter->recvpriv.signal_strength);/* dbm */
279 	} else {
280 		notify_signal = 100 * translate_percentage_to_dbm(pnetwork->network.phy_info.signal_strength);/* dbm */
281 	}
282 
283 	buf = kzalloc(MAX_BSSINFO_LEN, GFP_ATOMIC);
284 	if (!buf)
285 		goto exit;
286 	pbuf = buf;
287 
288 	pwlanhdr = (struct ieee80211_hdr *)pbuf;
289 	fctrl = &(pwlanhdr->frame_control);
290 	*(fctrl) = 0;
291 
292 	SetSeqNum(pwlanhdr, 0/*pmlmeext->mgnt_seq*/);
293 	/* pmlmeext->mgnt_seq++; */
294 
295 	if (pnetwork->network.reserved[0] == 1) { /*  WIFI_BEACON */
296 		eth_broadcast_addr(pwlanhdr->addr1);
297 		SetFrameSubType(pbuf, WIFI_BEACON);
298 	} else {
299 		memcpy(pwlanhdr->addr1, myid(&(padapter->eeprompriv)), ETH_ALEN);
300 		SetFrameSubType(pbuf, WIFI_PROBERSP);
301 	}
302 
303 	memcpy(pwlanhdr->addr2, pnetwork->network.mac_address, ETH_ALEN);
304 	memcpy(pwlanhdr->addr3, pnetwork->network.mac_address, ETH_ALEN);
305 
306 	pbuf += sizeof(struct ieee80211_hdr_3addr);
307 	len = sizeof(struct ieee80211_hdr_3addr);
308 
309 	memcpy(pbuf, pnetwork->network.ies, pnetwork->network.ie_length);
310 	len += pnetwork->network.ie_length;
311 
312 	*((__le64 *)pbuf) = cpu_to_le64(notify_timestamp);
313 
314 	bss = cfg80211_inform_bss_frame(wiphy, notify_channel, (struct ieee80211_mgmt *)buf,
315 					len, notify_signal, GFP_ATOMIC);
316 
317 	if (unlikely(!bss))
318 		goto exit;
319 
320 	cfg80211_put_bss(wiphy, bss);
321 	kfree(buf);
322 
323 exit:
324 	return bss;
325 }
326 
327 /*
328  *	Check the given bss is valid by kernel API cfg80211_get_bss()
329  *	@padapter : the given adapter
330  *
331  *	return true if bss is valid,  false for not found.
332  */
rtw_cfg80211_check_bss(struct adapter * padapter)333 int rtw_cfg80211_check_bss(struct adapter *padapter)
334 {
335 	struct wlan_bssid_ex  *pnetwork = &(padapter->mlmeextpriv.mlmext_info.network);
336 	struct cfg80211_bss *bss = NULL;
337 	struct ieee80211_channel *notify_channel = NULL;
338 	u32 freq;
339 
340 	if (!(pnetwork) || !(padapter->rtw_wdev))
341 		return false;
342 
343 	freq = rtw_ieee80211_channel_to_frequency(pnetwork->configuration.ds_config, NL80211_BAND_2GHZ);
344 
345 	notify_channel = ieee80211_get_channel(padapter->rtw_wdev->wiphy, freq);
346 	bss = cfg80211_get_bss(padapter->rtw_wdev->wiphy, notify_channel,
347 			       pnetwork->mac_address, pnetwork->ssid.ssid,
348 			       pnetwork->ssid.ssid_length,
349 			       IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
350 
351 	cfg80211_put_bss(padapter->rtw_wdev->wiphy, bss);
352 
353 	return	(bss != NULL);
354 }
355 
rtw_cfg80211_ibss_indicate_connect(struct adapter * padapter)356 void rtw_cfg80211_ibss_indicate_connect(struct adapter *padapter)
357 {
358 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
359 	struct wlan_network  *cur_network = &(pmlmepriv->cur_network);
360 	struct wireless_dev *pwdev = padapter->rtw_wdev;
361 	struct wiphy *wiphy = pwdev->wiphy;
362 	int freq = (int)cur_network->network.configuration.ds_config;
363 	struct ieee80211_channel *chan;
364 
365 	if (pwdev->iftype != NL80211_IFTYPE_ADHOC)
366 		return;
367 
368 	if (!rtw_cfg80211_check_bss(padapter)) {
369 		struct wlan_bssid_ex  *pnetwork = &(padapter->mlmeextpriv.mlmext_info.network);
370 		struct wlan_network *scanned = pmlmepriv->cur_network_scanned;
371 
372 		if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true) {
373 			memcpy(&cur_network->network, pnetwork, sizeof(struct wlan_bssid_ex));
374 			rtw_cfg80211_inform_bss(padapter, cur_network);
375 		} else {
376 			if (!scanned) {
377 				rtw_warn_on(1);
378 				return;
379 			}
380 			if (!memcmp(&(scanned->network.ssid), &(pnetwork->ssid), sizeof(struct ndis_802_11_ssid))
381 				&& !memcmp(scanned->network.mac_address, pnetwork->mac_address, sizeof(NDIS_802_11_MAC_ADDRESS))
382 			)
383 				rtw_cfg80211_inform_bss(padapter, scanned);
384 			else
385 				rtw_warn_on(1);
386 		}
387 
388 		if (!rtw_cfg80211_check_bss(padapter))
389 			netdev_dbg(padapter->pnetdev,
390 				   FUNC_ADPT_FMT " BSS not found !!\n",
391 				   FUNC_ADPT_ARG(padapter));
392 	}
393 	/* notify cfg80211 that device joined an IBSS */
394 	chan = ieee80211_get_channel(wiphy, freq);
395 	cfg80211_ibss_joined(padapter->pnetdev, cur_network->network.mac_address, chan, GFP_ATOMIC);
396 }
397 
rtw_cfg80211_indicate_connect(struct adapter * padapter)398 void rtw_cfg80211_indicate_connect(struct adapter *padapter)
399 {
400 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
401 	struct wlan_network  *cur_network = &(pmlmepriv->cur_network);
402 	struct wireless_dev *pwdev = padapter->rtw_wdev;
403 
404 	if (pwdev->iftype != NL80211_IFTYPE_STATION
405 		&& pwdev->iftype != NL80211_IFTYPE_P2P_CLIENT
406 	) {
407 		return;
408 	}
409 
410 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true)
411 		return;
412 
413 	{
414 		struct wlan_bssid_ex  *pnetwork = &(padapter->mlmeextpriv.mlmext_info.network);
415 		struct wlan_network *scanned = pmlmepriv->cur_network_scanned;
416 
417 		if (!scanned) {
418 			rtw_warn_on(1);
419 			goto check_bss;
420 		}
421 
422 		if (!memcmp(scanned->network.mac_address, pnetwork->mac_address, sizeof(NDIS_802_11_MAC_ADDRESS))
423 			&& !memcmp(&(scanned->network.ssid), &(pnetwork->ssid), sizeof(struct ndis_802_11_ssid))
424 		)
425 			rtw_cfg80211_inform_bss(padapter, scanned);
426 		else
427 			rtw_warn_on(1);
428 	}
429 
430 check_bss:
431 	if (!rtw_cfg80211_check_bss(padapter))
432 		netdev_dbg(padapter->pnetdev,
433 			   FUNC_ADPT_FMT " BSS not found !!\n",
434 			   FUNC_ADPT_ARG(padapter));
435 
436 	if (rtw_to_roam(padapter) > 0) {
437 		struct wiphy *wiphy = pwdev->wiphy;
438 		struct ieee80211_channel *notify_channel;
439 		u32 freq;
440 		u16 channel = cur_network->network.configuration.ds_config;
441 		struct cfg80211_roam_info roam_info = {};
442 
443 		freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
444 
445 		notify_channel = ieee80211_get_channel(wiphy, freq);
446 
447 		roam_info.links[0].channel = notify_channel;
448 		roam_info.links[0].bssid = cur_network->network.mac_address;
449 		roam_info.req_ie =
450 			pmlmepriv->assoc_req + sizeof(struct ieee80211_hdr_3addr) + 2;
451 		roam_info.req_ie_len =
452 			pmlmepriv->assoc_req_len - sizeof(struct ieee80211_hdr_3addr) - 2;
453 		roam_info.resp_ie =
454 			pmlmepriv->assoc_rsp + sizeof(struct ieee80211_hdr_3addr) + 6;
455 		roam_info.resp_ie_len =
456 			pmlmepriv->assoc_rsp_len - sizeof(struct ieee80211_hdr_3addr) - 6;
457 		cfg80211_roamed(padapter->pnetdev, &roam_info, GFP_ATOMIC);
458 	} else {
459 		cfg80211_connect_result(padapter->pnetdev, cur_network->network.mac_address
460 			, pmlmepriv->assoc_req + sizeof(struct ieee80211_hdr_3addr) + 2
461 			, pmlmepriv->assoc_req_len - sizeof(struct ieee80211_hdr_3addr) - 2
462 			, pmlmepriv->assoc_rsp + sizeof(struct ieee80211_hdr_3addr) + 6
463 			, pmlmepriv->assoc_rsp_len - sizeof(struct ieee80211_hdr_3addr) - 6
464 			, WLAN_STATUS_SUCCESS, GFP_ATOMIC);
465 	}
466 }
467 
rtw_cfg80211_indicate_disconnect(struct adapter * padapter)468 void rtw_cfg80211_indicate_disconnect(struct adapter *padapter)
469 {
470 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
471 	struct wireless_dev *pwdev = padapter->rtw_wdev;
472 
473 	if (pwdev->iftype != NL80211_IFTYPE_STATION
474 		&& pwdev->iftype != NL80211_IFTYPE_P2P_CLIENT
475 	) {
476 		return;
477 	}
478 
479 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE) == true)
480 		return;
481 
482 	if (!padapter->mlmepriv.not_indic_disco) {
483 		if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
484 			cfg80211_disconnected(padapter->pnetdev, 0,
485 					      NULL, 0, true, GFP_ATOMIC);
486 		} else {
487 			cfg80211_connect_result(padapter->pnetdev, NULL, NULL, 0, NULL, 0,
488 						WLAN_STATUS_UNSPECIFIED_FAILURE, GFP_ATOMIC/*GFP_KERNEL*/);
489 		}
490 	}
491 }
492 
rtw_cfg80211_ap_set_encryption(struct net_device * dev,struct ieee_param * param,u32 param_len)493 static int rtw_cfg80211_ap_set_encryption(struct net_device *dev, struct ieee_param *param, u32 param_len)
494 {
495 	int ret = 0;
496 	u32 wep_key_idx, wep_key_len;
497 	struct sta_info *psta = NULL, *pbcmc_sta = NULL;
498 	struct adapter *padapter = rtw_netdev_priv(dev);
499 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
500 	struct security_priv *psecuritypriv =  &(padapter->securitypriv);
501 	struct sta_priv *pstapriv = &padapter->stapriv;
502 	char *grpkey = padapter->securitypriv.dot118021XGrpKey[param->u.crypt.idx].skey;
503 	char *txkey = padapter->securitypriv.dot118021XGrptxmickey[param->u.crypt.idx].skey;
504 	char *rxkey = padapter->securitypriv.dot118021XGrprxmickey[param->u.crypt.idx].skey;
505 
506 	param->u.crypt.err = 0;
507 	param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0';
508 
509 	if (param_len !=  sizeof(struct ieee_param) + param->u.crypt.key_len) {
510 		ret =  -EINVAL;
511 		goto exit;
512 	}
513 
514 	if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
515 	    param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
516 	    param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
517 		if (param->u.crypt.idx >= WEP_KEYS) {
518 			ret = -EINVAL;
519 			goto exit;
520 		}
521 	} else {
522 		psta = rtw_get_stainfo(pstapriv, param->sta_addr);
523 		if (!psta)
524 			/* ret = -EINVAL; */
525 			goto exit;
526 	}
527 
528 	if (strcmp(param->u.crypt.alg, "none") == 0 && !psta)
529 		goto exit;
530 
531 	if (strcmp(param->u.crypt.alg, "WEP") == 0 && !psta) {
532 		wep_key_idx = param->u.crypt.idx;
533 		wep_key_len = param->u.crypt.key_len;
534 
535 		if ((wep_key_idx >= WEP_KEYS) || (wep_key_len <= 0)) {
536 			ret = -EINVAL;
537 			goto exit;
538 		}
539 
540 		if (wep_key_len > 0)
541 			wep_key_len = wep_key_len <= 5 ? 5 : 13;
542 
543 		if (psecuritypriv->bWepDefaultKeyIdxSet == 0) {
544 			/* wep default key has not been set, so use this key index as default key. */
545 
546 			psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Auto;
547 			psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled;
548 			psecuritypriv->dot11PrivacyAlgrthm = _WEP40_;
549 			psecuritypriv->dot118021XGrpPrivacy = _WEP40_;
550 
551 			if (wep_key_len == 13) {
552 				psecuritypriv->dot11PrivacyAlgrthm = _WEP104_;
553 				psecuritypriv->dot118021XGrpPrivacy = _WEP104_;
554 			}
555 
556 			psecuritypriv->dot11PrivacyKeyIndex = wep_key_idx;
557 		}
558 
559 		memcpy(&(psecuritypriv->dot11DefKey[wep_key_idx].skey[0]), param->u.crypt.key, wep_key_len);
560 
561 		psecuritypriv->dot11DefKeylen[wep_key_idx] = wep_key_len;
562 
563 		rtw_ap_set_wep_key(padapter, param->u.crypt.key, wep_key_len, wep_key_idx, 1);
564 
565 		goto exit;
566 	}
567 
568 	/* group key */
569 	if (!psta && check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
570 		/* group key */
571 		if (param->u.crypt.set_tx == 0) {
572 			if (strcmp(param->u.crypt.alg, "WEP") == 0) {
573 				memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
574 
575 				psecuritypriv->dot118021XGrpPrivacy = _WEP40_;
576 				if (param->u.crypt.key_len == 13)
577 					psecuritypriv->dot118021XGrpPrivacy = _WEP104_;
578 
579 			} else if (strcmp(param->u.crypt.alg, "TKIP") == 0) {
580 				psecuritypriv->dot118021XGrpPrivacy = _TKIP_;
581 
582 				memcpy(grpkey, param->u.crypt.key, (param->u.crypt.key_len > 16 ? 16 : param->u.crypt.key_len));
583 
584 				/* 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,u32 changed)1301 static int cfg80211_rtw_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1302 {
1303 	return 0;
1304 }
1305 
rtw_cfg80211_set_wpa_version(struct security_priv * psecuritypriv,u32 wpa_version)1306 static int rtw_cfg80211_set_wpa_version(struct security_priv *psecuritypriv, u32 wpa_version)
1307 {
1308 	if (!wpa_version) {
1309 		psecuritypriv->ndisauthtype = Ndis802_11AuthModeOpen;
1310 		return 0;
1311 	}
1312 
1313 	if (wpa_version & (NL80211_WPA_VERSION_1 | NL80211_WPA_VERSION_2))
1314 		psecuritypriv->ndisauthtype = Ndis802_11AuthModeWPAPSK;
1315 
1316 	return 0;
1317 }
1318 
rtw_cfg80211_set_auth_type(struct security_priv * psecuritypriv,enum nl80211_auth_type sme_auth_type)1319 static int rtw_cfg80211_set_auth_type(struct security_priv *psecuritypriv,
1320 				      enum nl80211_auth_type sme_auth_type)
1321 {
1322 	switch (sme_auth_type) {
1323 	case NL80211_AUTHTYPE_AUTOMATIC:
1324 
1325 		psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Auto;
1326 
1327 		break;
1328 	case NL80211_AUTHTYPE_OPEN_SYSTEM:
1329 
1330 		psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open;
1331 
1332 		if (psecuritypriv->ndisauthtype > Ndis802_11AuthModeWPA)
1333 			psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1334 
1335 		break;
1336 	case NL80211_AUTHTYPE_SHARED_KEY:
1337 
1338 		psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Shared;
1339 
1340 		psecuritypriv->ndisencryptstatus = Ndis802_11Encryption1Enabled;
1341 
1342 		break;
1343 	default:
1344 		psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open;
1345 		/* return -ENOTSUPP; */
1346 	}
1347 
1348 	return 0;
1349 }
1350 
rtw_cfg80211_set_cipher(struct security_priv * psecuritypriv,u32 cipher,bool ucast)1351 static int rtw_cfg80211_set_cipher(struct security_priv *psecuritypriv, u32 cipher, bool ucast)
1352 {
1353 	u32 ndisencryptstatus = Ndis802_11EncryptionDisabled;
1354 
1355 	u32 *profile_cipher = ucast ? &psecuritypriv->dot11PrivacyAlgrthm :
1356 		&psecuritypriv->dot118021XGrpPrivacy;
1357 
1358 	if (!cipher) {
1359 		*profile_cipher = _NO_PRIVACY_;
1360 		psecuritypriv->ndisencryptstatus = ndisencryptstatus;
1361 		return 0;
1362 	}
1363 
1364 	switch (cipher) {
1365 	case IW_AUTH_CIPHER_NONE:
1366 		*profile_cipher = _NO_PRIVACY_;
1367 		ndisencryptstatus = Ndis802_11EncryptionDisabled;
1368 		break;
1369 	case WLAN_CIPHER_SUITE_WEP40:
1370 		*profile_cipher = _WEP40_;
1371 		ndisencryptstatus = Ndis802_11Encryption1Enabled;
1372 		break;
1373 	case WLAN_CIPHER_SUITE_WEP104:
1374 		*profile_cipher = _WEP104_;
1375 		ndisencryptstatus = Ndis802_11Encryption1Enabled;
1376 		break;
1377 	case WLAN_CIPHER_SUITE_TKIP:
1378 		*profile_cipher = _TKIP_;
1379 		ndisencryptstatus = Ndis802_11Encryption2Enabled;
1380 		break;
1381 	case WLAN_CIPHER_SUITE_CCMP:
1382 		*profile_cipher = _AES_;
1383 		ndisencryptstatus = Ndis802_11Encryption3Enabled;
1384 		break;
1385 	default:
1386 		return -ENOTSUPP;
1387 	}
1388 
1389 	if (ucast) {
1390 		psecuritypriv->ndisencryptstatus = ndisencryptstatus;
1391 
1392 		/* if (psecuritypriv->dot11PrivacyAlgrthm >= _AES_) */
1393 		/*	psecuritypriv->ndisauthtype = Ndis802_11AuthModeWPA2PSK; */
1394 	}
1395 
1396 	return 0;
1397 }
1398 
rtw_cfg80211_set_key_mgt(struct security_priv * psecuritypriv,u32 key_mgt)1399 static int rtw_cfg80211_set_key_mgt(struct security_priv *psecuritypriv, u32 key_mgt)
1400 {
1401 	if (key_mgt == WLAN_AKM_SUITE_8021X)
1402 		/* auth_type = UMAC_AUTH_TYPE_8021X; */
1403 		psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1404 	else if (key_mgt == WLAN_AKM_SUITE_PSK) {
1405 		psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1406 	}
1407 
1408 	return 0;
1409 }
1410 
rtw_cfg80211_set_wpa_ie(struct adapter * padapter,u8 * pie,size_t ielen)1411 static int rtw_cfg80211_set_wpa_ie(struct adapter *padapter, u8 *pie, size_t ielen)
1412 {
1413 	u8 *buf = NULL;
1414 	int group_cipher = 0, pairwise_cipher = 0;
1415 	int ret = 0;
1416 	int wpa_ielen = 0;
1417 	int wpa2_ielen = 0;
1418 	u8 *pwpa, *pwpa2;
1419 	u8 null_addr[] = {0, 0, 0, 0, 0, 0};
1420 
1421 	if (!pie || !ielen) {
1422 		/* Treat this as normal case, but need to clear WIFI_UNDER_WPS */
1423 		_clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS);
1424 		goto exit;
1425 	}
1426 
1427 	if (ielen > MAX_WPA_IE_LEN + MAX_WPS_IE_LEN + MAX_P2P_IE_LEN) {
1428 		ret = -EINVAL;
1429 		goto exit;
1430 	}
1431 
1432 	buf = rtw_zmalloc(ielen);
1433 	if (!buf) {
1434 		ret =  -ENOMEM;
1435 		goto exit;
1436 	}
1437 
1438 	memcpy(buf, pie, ielen);
1439 
1440 	if (ielen < RSN_HEADER_LEN) {
1441 		ret  = -1;
1442 		goto exit;
1443 	}
1444 
1445 	pwpa = rtw_get_wpa_ie(buf, &wpa_ielen, ielen);
1446 	if (pwpa && wpa_ielen > 0) {
1447 		if (rtw_parse_wpa_ie(pwpa, wpa_ielen + 2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
1448 			padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1449 			padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPAPSK;
1450 			memcpy(padapter->securitypriv.supplicant_ie, &pwpa[0], wpa_ielen + 2);
1451 		}
1452 	}
1453 
1454 	pwpa2 = rtw_get_wpa2_ie(buf, &wpa2_ielen, ielen);
1455 	if (pwpa2 && wpa2_ielen > 0) {
1456 		if (rtw_parse_wpa2_ie(pwpa2, wpa2_ielen + 2, &group_cipher, &pairwise_cipher, NULL) == _SUCCESS) {
1457 			padapter->securitypriv.dot11AuthAlgrthm = dot11AuthAlgrthm_8021X;
1458 			padapter->securitypriv.ndisauthtype = Ndis802_11AuthModeWPA2PSK;
1459 			memcpy(padapter->securitypriv.supplicant_ie, &pwpa2[0], wpa2_ielen + 2);
1460 		}
1461 	}
1462 
1463 	if (group_cipher == 0)
1464 		group_cipher = WPA_CIPHER_NONE;
1465 
1466 	if (pairwise_cipher == 0)
1467 		pairwise_cipher = WPA_CIPHER_NONE;
1468 
1469 	switch (group_cipher) {
1470 	case WPA_CIPHER_NONE:
1471 		padapter->securitypriv.dot118021XGrpPrivacy = _NO_PRIVACY_;
1472 		padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled;
1473 		break;
1474 	case WPA_CIPHER_WEP40:
1475 		padapter->securitypriv.dot118021XGrpPrivacy = _WEP40_;
1476 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
1477 		break;
1478 	case WPA_CIPHER_TKIP:
1479 		padapter->securitypriv.dot118021XGrpPrivacy = _TKIP_;
1480 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption2Enabled;
1481 		break;
1482 	case WPA_CIPHER_CCMP:
1483 		padapter->securitypriv.dot118021XGrpPrivacy = _AES_;
1484 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled;
1485 		break;
1486 	case WPA_CIPHER_WEP104:
1487 		padapter->securitypriv.dot118021XGrpPrivacy = _WEP104_;
1488 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
1489 		break;
1490 	}
1491 
1492 	switch (pairwise_cipher) {
1493 	case WPA_CIPHER_NONE:
1494 		padapter->securitypriv.dot11PrivacyAlgrthm = _NO_PRIVACY_;
1495 		padapter->securitypriv.ndisencryptstatus = Ndis802_11EncryptionDisabled;
1496 		break;
1497 	case WPA_CIPHER_WEP40:
1498 		padapter->securitypriv.dot11PrivacyAlgrthm = _WEP40_;
1499 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
1500 		break;
1501 	case WPA_CIPHER_TKIP:
1502 		padapter->securitypriv.dot11PrivacyAlgrthm = _TKIP_;
1503 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption2Enabled;
1504 		break;
1505 	case WPA_CIPHER_CCMP:
1506 		padapter->securitypriv.dot11PrivacyAlgrthm = _AES_;
1507 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption3Enabled;
1508 		break;
1509 	case WPA_CIPHER_WEP104:
1510 		padapter->securitypriv.dot11PrivacyAlgrthm = _WEP104_;
1511 		padapter->securitypriv.ndisencryptstatus = Ndis802_11Encryption1Enabled;
1512 		break;
1513 	}
1514 
1515 	{/* handle wps_ie */
1516 		uint wps_ielen;
1517 		u8 *wps_ie;
1518 
1519 		wps_ie = rtw_get_wps_ie(buf, ielen, NULL, &wps_ielen);
1520 		if (wps_ie && wps_ielen > 0) {
1521 			padapter->securitypriv.wps_ie_len = min_t(uint, wps_ielen, MAX_WPS_IE_LEN);
1522 			memcpy(padapter->securitypriv.wps_ie, wps_ie, padapter->securitypriv.wps_ie_len);
1523 			set_fwstate(&padapter->mlmepriv, WIFI_UNDER_WPS);
1524 		} else {
1525 			_clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS);
1526 		}
1527 	}
1528 
1529 	/* TKIP and AES disallow multicast packets until installing group key */
1530 	if (padapter->securitypriv.dot11PrivacyAlgrthm == _TKIP_
1531 		|| padapter->securitypriv.dot11PrivacyAlgrthm == _TKIP_WTMIC_
1532 		|| padapter->securitypriv.dot11PrivacyAlgrthm == _AES_)
1533 		/* WPS open need to enable multicast */
1534 		/*  check_fwstate(&padapter->mlmepriv, WIFI_UNDER_WPS) == true) */
1535 		rtw_hal_set_hwreg(padapter, HW_VAR_OFF_RCR_AM, null_addr);
1536 
1537 exit:
1538 	kfree(buf);
1539 	if (ret)
1540 		_clr_fwstate_(&padapter->mlmepriv, WIFI_UNDER_WPS);
1541 	return ret;
1542 }
1543 
cfg80211_rtw_join_ibss(struct wiphy * wiphy,struct net_device * ndev,struct cfg80211_ibss_params * params)1544 static int cfg80211_rtw_join_ibss(struct wiphy *wiphy, struct net_device *ndev,
1545 				  struct cfg80211_ibss_params *params)
1546 {
1547 	struct adapter *padapter = rtw_netdev_priv(ndev);
1548 	struct ndis_802_11_ssid ndis_ssid;
1549 	struct security_priv *psecuritypriv = &padapter->securitypriv;
1550 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1551 	int ret = 0;
1552 
1553 	if (rtw_pwr_wakeup(padapter) == _FAIL) {
1554 		ret = -EPERM;
1555 		goto exit;
1556 	}
1557 
1558 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1559 		ret = -EPERM;
1560 		goto exit;
1561 	}
1562 
1563 	if (!params->ssid || !params->ssid_len) {
1564 		ret = -EINVAL;
1565 		goto exit;
1566 	}
1567 
1568 	if (params->ssid_len > IW_ESSID_MAX_SIZE) {
1569 		ret = -E2BIG;
1570 		goto exit;
1571 	}
1572 
1573 	memset(&ndis_ssid, 0, sizeof(struct ndis_802_11_ssid));
1574 	ndis_ssid.ssid_length = params->ssid_len;
1575 	memcpy(ndis_ssid.ssid, (u8 *)params->ssid, params->ssid_len);
1576 
1577 	psecuritypriv->ndisencryptstatus = Ndis802_11EncryptionDisabled;
1578 	psecuritypriv->dot11PrivacyAlgrthm = _NO_PRIVACY_;
1579 	psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_;
1580 	psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */
1581 	psecuritypriv->ndisauthtype = Ndis802_11AuthModeOpen;
1582 
1583 	ret = rtw_cfg80211_set_auth_type(psecuritypriv, NL80211_AUTHTYPE_OPEN_SYSTEM);
1584 	rtw_set_802_11_authentication_mode(padapter, psecuritypriv->ndisauthtype);
1585 
1586 	if (rtw_set_802_11_ssid(padapter, &ndis_ssid) == false) {
1587 		ret = -1;
1588 		goto exit;
1589 	}
1590 
1591 exit:
1592 	return ret;
1593 }
1594 
cfg80211_rtw_leave_ibss(struct wiphy * wiphy,struct net_device * ndev)1595 static int cfg80211_rtw_leave_ibss(struct wiphy *wiphy, struct net_device *ndev)
1596 {
1597 	struct adapter *padapter = rtw_netdev_priv(ndev);
1598 	struct wireless_dev *rtw_wdev = padapter->rtw_wdev;
1599 	enum nl80211_iftype old_type;
1600 	int ret = 0;
1601 
1602 	old_type = rtw_wdev->iftype;
1603 
1604 	rtw_set_to_roam(padapter, 0);
1605 
1606 	if (check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
1607 		rtw_scan_abort(padapter);
1608 		LeaveAllPowerSaveMode(padapter);
1609 
1610 		rtw_wdev->iftype = NL80211_IFTYPE_STATION;
1611 
1612 		if (rtw_set_802_11_infrastructure_mode(padapter, Ndis802_11Infrastructure) == false) {
1613 			rtw_wdev->iftype = old_type;
1614 			ret = -EPERM;
1615 			goto leave_ibss;
1616 		}
1617 		rtw_setopmode_cmd(padapter, Ndis802_11Infrastructure, true);
1618 	}
1619 
1620 leave_ibss:
1621 	return ret;
1622 }
1623 
cfg80211_rtw_connect(struct wiphy * wiphy,struct net_device * ndev,struct cfg80211_connect_params * sme)1624 static int cfg80211_rtw_connect(struct wiphy *wiphy, struct net_device *ndev,
1625 				struct cfg80211_connect_params *sme)
1626 {
1627 	int ret = 0;
1628 	enum ndis_802_11_authentication_mode authmode;
1629 	struct ndis_802_11_ssid ndis_ssid;
1630 	struct adapter *padapter = rtw_netdev_priv(ndev);
1631 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1632 	struct security_priv *psecuritypriv = &padapter->securitypriv;
1633 
1634 	padapter->mlmepriv.not_indic_disco = true;
1635 
1636 	if (adapter_wdev_data(padapter)->block == true) {
1637 		ret = -EBUSY;
1638 		goto exit;
1639 	}
1640 
1641 	rtw_ps_deny(padapter, PS_DENY_JOIN);
1642 	if (rtw_pwr_wakeup(padapter) == _FAIL) {
1643 		ret = -EPERM;
1644 		goto exit;
1645 	}
1646 
1647 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1648 		ret = -EPERM;
1649 		goto exit;
1650 	}
1651 
1652 	if (!sme->ssid || !sme->ssid_len) {
1653 		ret = -EINVAL;
1654 		goto exit;
1655 	}
1656 
1657 	if (sme->ssid_len > IW_ESSID_MAX_SIZE) {
1658 		ret = -E2BIG;
1659 		goto exit;
1660 	}
1661 
1662 	memset(&ndis_ssid, 0, sizeof(struct ndis_802_11_ssid));
1663 	ndis_ssid.ssid_length = sme->ssid_len;
1664 	memcpy(ndis_ssid.ssid, (u8 *)sme->ssid, sme->ssid_len);
1665 
1666 	if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING) == true) {
1667 		ret = -EBUSY;
1668 		goto exit;
1669 	}
1670 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY) == true)
1671 		rtw_scan_abort(padapter);
1672 
1673 	psecuritypriv->ndisencryptstatus = Ndis802_11EncryptionDisabled;
1674 	psecuritypriv->dot11PrivacyAlgrthm = _NO_PRIVACY_;
1675 	psecuritypriv->dot118021XGrpPrivacy = _NO_PRIVACY_;
1676 	psecuritypriv->dot11AuthAlgrthm = dot11AuthAlgrthm_Open; /* open system */
1677 	psecuritypriv->ndisauthtype = Ndis802_11AuthModeOpen;
1678 
1679 	ret = rtw_cfg80211_set_wpa_version(psecuritypriv, sme->crypto.wpa_versions);
1680 	if (ret < 0)
1681 		goto exit;
1682 
1683 	ret = rtw_cfg80211_set_auth_type(psecuritypriv, sme->auth_type);
1684 
1685 	if (ret < 0)
1686 		goto exit;
1687 
1688 	ret = rtw_cfg80211_set_wpa_ie(padapter, (u8 *)sme->ie, sme->ie_len);
1689 	if (ret < 0)
1690 		goto exit;
1691 
1692 	if (sme->crypto.n_ciphers_pairwise) {
1693 		ret = rtw_cfg80211_set_cipher(psecuritypriv, sme->crypto.ciphers_pairwise[0], true);
1694 		if (ret < 0)
1695 			goto exit;
1696 	}
1697 
1698 	/* For WEP Shared auth */
1699 	if ((psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_Shared ||
1700 	     psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_Auto) && sme->key) {
1701 		u32 wep_key_idx, wep_key_len, wep_total_len;
1702 		struct ndis_802_11_wep	 *pwep = NULL;
1703 
1704 		wep_key_idx = sme->key_idx;
1705 		wep_key_len = sme->key_len;
1706 
1707 		if (sme->key_idx > WEP_KEYS) {
1708 			ret = -EINVAL;
1709 			goto exit;
1710 		}
1711 
1712 		if (wep_key_len > 0) {
1713 			wep_key_len = wep_key_len <= 5 ? 5 : 13;
1714 			wep_total_len = wep_key_len + FIELD_OFFSET(struct ndis_802_11_wep, key_material);
1715 			pwep = rtw_malloc(wep_total_len);
1716 			if (!pwep) {
1717 				ret = -ENOMEM;
1718 				goto exit;
1719 			}
1720 
1721 			memset(pwep, 0, wep_total_len);
1722 
1723 			pwep->key_length = wep_key_len;
1724 			pwep->length = wep_total_len;
1725 
1726 			if (wep_key_len == 13) {
1727 				padapter->securitypriv.dot11PrivacyAlgrthm = _WEP104_;
1728 				padapter->securitypriv.dot118021XGrpPrivacy = _WEP104_;
1729 			}
1730 		} else {
1731 			ret = -EINVAL;
1732 			goto exit;
1733 		}
1734 
1735 		pwep->key_index = wep_key_idx;
1736 		pwep->key_index |= 0x80000000;
1737 
1738 		memcpy(pwep->key_material,  (void *)sme->key, pwep->key_length);
1739 
1740 		if (rtw_set_802_11_add_wep(padapter, pwep) == (u8)_FAIL)
1741 			ret = -EOPNOTSUPP;
1742 
1743 		kfree(pwep);
1744 
1745 		if (ret < 0)
1746 			goto exit;
1747 	}
1748 
1749 	ret = rtw_cfg80211_set_cipher(psecuritypriv, sme->crypto.cipher_group, false);
1750 	if (ret < 0)
1751 		return ret;
1752 
1753 	if (sme->crypto.n_akm_suites) {
1754 		ret = rtw_cfg80211_set_key_mgt(psecuritypriv, sme->crypto.akm_suites[0]);
1755 		if (ret < 0)
1756 			goto exit;
1757 	}
1758 
1759 	authmode = psecuritypriv->ndisauthtype;
1760 	rtw_set_802_11_authentication_mode(padapter, authmode);
1761 
1762 	/* rtw_set_802_11_encryption_mode(padapter, padapter->securitypriv.ndisencryptstatus); */
1763 
1764 	if (rtw_set_802_11_connect(padapter, (u8 *)sme->bssid, &ndis_ssid) == false) {
1765 		ret = -1;
1766 		goto exit;
1767 	}
1768 
1769 exit:
1770 
1771 	rtw_ps_deny_cancel(padapter, PS_DENY_JOIN);
1772 
1773 	padapter->mlmepriv.not_indic_disco = false;
1774 
1775 	return ret;
1776 }
1777 
cfg80211_rtw_disconnect(struct wiphy * wiphy,struct net_device * ndev,u16 reason_code)1778 static int cfg80211_rtw_disconnect(struct wiphy *wiphy, struct net_device *ndev,
1779 				   u16 reason_code)
1780 {
1781 	struct adapter *padapter = rtw_netdev_priv(ndev);
1782 
1783 	rtw_set_to_roam(padapter, 0);
1784 
1785 	rtw_scan_abort(padapter);
1786 	LeaveAllPowerSaveMode(padapter);
1787 	rtw_disassoc_cmd(padapter, 500, false);
1788 
1789 	rtw_indicate_disconnect(padapter);
1790 
1791 	rtw_free_assoc_resources(padapter, 1);
1792 	rtw_pwr_wakeup(padapter);
1793 
1794 	return 0;
1795 }
1796 
cfg80211_rtw_set_txpower(struct wiphy * wiphy,struct wireless_dev * wdev,enum nl80211_tx_power_setting type,int mbm)1797 static int cfg80211_rtw_set_txpower(struct wiphy *wiphy,
1798 				    struct wireless_dev *wdev,
1799 				    enum nl80211_tx_power_setting type, int mbm)
1800 {
1801 	return 0;
1802 }
1803 
cfg80211_rtw_get_txpower(struct wiphy * wiphy,struct wireless_dev * wdev,unsigned int link_id,int * dbm)1804 static int cfg80211_rtw_get_txpower(struct wiphy *wiphy,
1805 				    struct wireless_dev *wdev,
1806 				    unsigned int link_id, int *dbm)
1807 {
1808 	*dbm = (12);
1809 
1810 	return 0;
1811 }
1812 
rtw_cfg80211_pwr_mgmt(struct adapter * adapter)1813 inline bool rtw_cfg80211_pwr_mgmt(struct adapter *adapter)
1814 {
1815 	struct rtw_wdev_priv *rtw_wdev_priv = adapter_wdev_data(adapter);
1816 
1817 	return rtw_wdev_priv->power_mgmt;
1818 }
1819 
cfg80211_rtw_set_power_mgmt(struct wiphy * wiphy,struct net_device * ndev,bool enabled,int timeout)1820 static int cfg80211_rtw_set_power_mgmt(struct wiphy *wiphy,
1821 				       struct net_device *ndev,
1822 				       bool enabled, int timeout)
1823 {
1824 	struct adapter *padapter = rtw_netdev_priv(ndev);
1825 	struct rtw_wdev_priv *rtw_wdev_priv = adapter_wdev_data(padapter);
1826 
1827 	rtw_wdev_priv->power_mgmt = enabled;
1828 
1829 	if (!enabled)
1830 		LPS_Leave(padapter, "CFG80211_PWRMGMT");
1831 
1832 	return 0;
1833 }
1834 
cfg80211_rtw_set_pmksa(struct wiphy * wiphy,struct net_device * ndev,struct cfg80211_pmksa * pmksa)1835 static int cfg80211_rtw_set_pmksa(struct wiphy *wiphy,
1836 				  struct net_device *ndev,
1837 				  struct cfg80211_pmksa *pmksa)
1838 {
1839 	u8 index, blInserted = false;
1840 	struct adapter *padapter = rtw_netdev_priv(ndev);
1841 	struct security_priv *psecuritypriv = &padapter->securitypriv;
1842 
1843 	if (is_zero_ether_addr((u8 *)pmksa->bssid))
1844 		return -EINVAL;
1845 
1846 	blInserted = false;
1847 
1848 	/* overwrite PMKID */
1849 	for (index = 0 ; index < NUM_PMKID_CACHE; index++) {
1850 		if (!memcmp(psecuritypriv->PMKIDList[index].Bssid, (u8 *)pmksa->bssid, ETH_ALEN)) {
1851 			memcpy(psecuritypriv->PMKIDList[index].PMKID, (u8 *)pmksa->pmkid, WLAN_PMKID_LEN);
1852 			psecuritypriv->PMKIDList[index].bUsed = true;
1853 			psecuritypriv->PMKIDIndex = index + 1;
1854 			blInserted = true;
1855 			break;
1856 		}
1857 	}
1858 
1859 	if (!blInserted) {
1860 		memcpy(psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].Bssid, (u8 *)pmksa->bssid, ETH_ALEN);
1861 		memcpy(psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].PMKID, (u8 *)pmksa->pmkid, WLAN_PMKID_LEN);
1862 
1863 		psecuritypriv->PMKIDList[psecuritypriv->PMKIDIndex].bUsed = true;
1864 		psecuritypriv->PMKIDIndex++;
1865 		if (psecuritypriv->PMKIDIndex == 16)
1866 			psecuritypriv->PMKIDIndex = 0;
1867 	}
1868 
1869 	return 0;
1870 }
1871 
cfg80211_rtw_del_pmksa(struct wiphy * wiphy,struct net_device * ndev,struct cfg80211_pmksa * pmksa)1872 static int cfg80211_rtw_del_pmksa(struct wiphy *wiphy,
1873 				  struct net_device *ndev,
1874 				  struct cfg80211_pmksa *pmksa)
1875 {
1876 	u8 index, bMatched = false;
1877 	struct adapter *padapter = rtw_netdev_priv(ndev);
1878 	struct security_priv *psecuritypriv = &padapter->securitypriv;
1879 
1880 	for (index = 0 ; index < NUM_PMKID_CACHE; index++) {
1881 		if (!memcmp(psecuritypriv->PMKIDList[index].Bssid, (u8 *)pmksa->bssid, ETH_ALEN)) {
1882 			/*
1883 			 * BSSID is matched, the same AP => Remove this PMKID information
1884 			 * and reset it.
1885 			 */
1886 			eth_zero_addr(psecuritypriv->PMKIDList[index].Bssid);
1887 			memset(psecuritypriv->PMKIDList[index].PMKID, 0x00, WLAN_PMKID_LEN);
1888 			psecuritypriv->PMKIDList[index].bUsed = false;
1889 			bMatched = true;
1890 			break;
1891 		}
1892 	}
1893 
1894 	if (!bMatched)
1895 		return -EINVAL;
1896 
1897 	return 0;
1898 }
1899 
cfg80211_rtw_flush_pmksa(struct wiphy * wiphy,struct net_device * ndev)1900 static int cfg80211_rtw_flush_pmksa(struct wiphy *wiphy,
1901 				    struct net_device *ndev)
1902 {
1903 	struct adapter *padapter = rtw_netdev_priv(ndev);
1904 	struct security_priv *psecuritypriv = &padapter->securitypriv;
1905 
1906 	memset(&psecuritypriv->PMKIDList[0], 0x00, sizeof(struct rt_pmkid_list) * NUM_PMKID_CACHE);
1907 	psecuritypriv->PMKIDIndex = 0;
1908 
1909 	return 0;
1910 }
1911 
rtw_cfg80211_indicate_sta_assoc(struct adapter * padapter,u8 * pmgmt_frame,uint frame_len)1912 void rtw_cfg80211_indicate_sta_assoc(struct adapter *padapter, u8 *pmgmt_frame, uint frame_len)
1913 {
1914 	struct net_device *ndev = padapter->pnetdev;
1915 
1916 	{
1917 		struct station_info sinfo = {};
1918 		u8 ie_offset;
1919 
1920 		if (GetFrameSubType(pmgmt_frame) == WIFI_ASSOCREQ)
1921 			ie_offset = _ASOCREQ_IE_OFFSET_;
1922 		else /*  WIFI_REASSOCREQ */
1923 			ie_offset = _REASOCREQ_IE_OFFSET_;
1924 
1925 		sinfo.filled = 0;
1926 		sinfo.assoc_req_ies = pmgmt_frame + WLAN_HDR_A3_LEN + ie_offset;
1927 		sinfo.assoc_req_ies_len = frame_len - WLAN_HDR_A3_LEN - ie_offset;
1928 		cfg80211_new_sta(ndev, GetAddr2Ptr(pmgmt_frame), &sinfo, GFP_ATOMIC);
1929 	}
1930 }
1931 
rtw_cfg80211_indicate_sta_disassoc(struct adapter * padapter,unsigned char * da,unsigned short reason)1932 void rtw_cfg80211_indicate_sta_disassoc(struct adapter *padapter, unsigned char *da, unsigned short reason)
1933 {
1934 	struct net_device *ndev = padapter->pnetdev;
1935 
1936 	cfg80211_del_sta(ndev, da, GFP_ATOMIC);
1937 }
1938 
rtw_get_chan_type(struct adapter * adapter)1939 static u8 rtw_get_chan_type(struct adapter *adapter)
1940 {
1941 	struct mlme_ext_priv *mlme_ext = &adapter->mlmeextpriv;
1942 
1943 	switch (mlme_ext->cur_bwmode) {
1944 	case CHANNEL_WIDTH_20:
1945 		if (is_supported_ht(adapter->registrypriv.wireless_mode))
1946 			return NL80211_CHAN_HT20;
1947 		else
1948 			return NL80211_CHAN_NO_HT;
1949 	case CHANNEL_WIDTH_40:
1950 		if (mlme_ext->cur_ch_offset == HAL_PRIME_CHNL_OFFSET_UPPER)
1951 			return NL80211_CHAN_HT40PLUS;
1952 		else
1953 			return NL80211_CHAN_HT40MINUS;
1954 	default:
1955 		return NL80211_CHAN_HT20;
1956 	}
1957 
1958 	return NL80211_CHAN_HT20;
1959 }
1960 
cfg80211_rtw_get_channel(struct wiphy * wiphy,struct wireless_dev * wdev,unsigned int link_id,struct cfg80211_chan_def * chandef)1961 static int cfg80211_rtw_get_channel(struct wiphy *wiphy, struct wireless_dev *wdev,
1962 				    unsigned int link_id,
1963 				    struct cfg80211_chan_def *chandef)
1964 {
1965 	struct adapter *adapter = wiphy_to_adapter(wiphy);
1966 	struct registry_priv *registrypriv = &adapter->registrypriv;
1967 	enum nl80211_channel_type chan_type;
1968 	struct ieee80211_channel *chan = NULL;
1969 	int channel;
1970 	int freq;
1971 
1972 	if (!adapter->rtw_wdev)
1973 		return -ENODEV;
1974 
1975 	channel = rtw_get_oper_ch(adapter);
1976 	if (!channel)
1977 		return -ENODATA;
1978 
1979 	freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
1980 
1981 	chan = ieee80211_get_channel(adapter->rtw_wdev->wiphy, freq);
1982 
1983 	if (registrypriv->ht_enable) {
1984 		chan_type = rtw_get_chan_type(adapter);
1985 		cfg80211_chandef_create(chandef, chan, chan_type);
1986 	} else {
1987 		cfg80211_chandef_create(chandef, chan, NL80211_CHAN_NO_HT);
1988 	}
1989 
1990 	return 0;
1991 }
1992 
rtw_cfg80211_monitor_if_xmit_entry(struct sk_buff * skb,struct net_device * ndev)1993 static netdev_tx_t rtw_cfg80211_monitor_if_xmit_entry(struct sk_buff *skb, struct net_device *ndev)
1994 {
1995 	int rtap_len;
1996 	int qos_len = 0;
1997 	int dot11_hdr_len = 24;
1998 	int snap_len = 6;
1999 	unsigned char *pdata;
2000 	u16 frame_control;
2001 	unsigned char src_mac_addr[6];
2002 	unsigned char dst_mac_addr[6];
2003 	struct ieee80211_hdr *dot11_hdr;
2004 	struct ieee80211_radiotap_header *rtap_hdr;
2005 	struct adapter *padapter = rtw_netdev_priv(ndev);
2006 
2007 	if (!skb)
2008 		goto fail;
2009 
2010 	if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2011 		goto fail;
2012 
2013 	rtap_hdr = (struct ieee80211_radiotap_header *)skb->data;
2014 	if (unlikely(rtap_hdr->it_version))
2015 		goto fail;
2016 
2017 	rtap_len = ieee80211_get_radiotap_len(skb->data);
2018 	if (unlikely(skb->len < rtap_len))
2019 		goto fail;
2020 
2021 	if (rtap_len != 14)
2022 		goto fail;
2023 
2024 	/* Skip the ratio tap header */
2025 	skb_pull(skb, rtap_len);
2026 
2027 	dot11_hdr = (struct ieee80211_hdr *)skb->data;
2028 	frame_control = le16_to_cpu(dot11_hdr->frame_control);
2029 	/* Check if the QoS bit is set */
2030 	if ((frame_control & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) {
2031 		/* Check if this ia a Wireless Distribution System (WDS) frame
2032 		 * which has 4 MAC addresses
2033 		 */
2034 		if (frame_control & 0x0080)
2035 			qos_len = 2;
2036 		if ((frame_control & 0x0300) == 0x0300)
2037 			dot11_hdr_len += 6;
2038 
2039 		memcpy(dst_mac_addr, dot11_hdr->addr1, sizeof(dst_mac_addr));
2040 		memcpy(src_mac_addr, dot11_hdr->addr2, sizeof(src_mac_addr));
2041 
2042 		/* Skip the 802.11 header, QoS (if any) and SNAP, but leave spaces for
2043 		 * two MAC addresses
2044 		 */
2045 		skb_pull(skb, dot11_hdr_len + qos_len + snap_len - sizeof(src_mac_addr) * 2);
2046 		pdata = (unsigned char *)skb->data;
2047 		memcpy(pdata, dst_mac_addr, sizeof(dst_mac_addr));
2048 		memcpy(pdata + sizeof(dst_mac_addr), src_mac_addr, sizeof(src_mac_addr));
2049 
2050 		/* Use the real net device to transmit the packet */
2051 		_rtw_xmit_entry(skb, padapter->pnetdev);
2052 		return NETDEV_TX_OK;
2053 
2054 	} else if ((frame_control & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) ==
2055 		   (IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION)) {
2056 		/* only for action frames */
2057 		struct xmit_frame		*pmgntframe;
2058 		struct pkt_attrib	*pattrib;
2059 		unsigned char *pframe;
2060 		/* u8 category, action, OUI_Subtype, dialogToken = 0; */
2061 		/* unsigned char *frame_body; */
2062 		struct ieee80211_hdr *pwlanhdr;
2063 		struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
2064 		struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
2065 		u8 *buf = skb->data;
2066 		u32 len = skb->len;
2067 		u8 category, action;
2068 
2069 		if (rtw_action_frame_parse(buf, len, &category, &action) == false)
2070 			goto fail;
2071 
2072 		/* starting alloc mgmt frame to dump it */
2073 		pmgntframe = alloc_mgtxmitframe(pxmitpriv);
2074 		if (!pmgntframe)
2075 			goto fail;
2076 
2077 		/* update attribute */
2078 		pattrib = &pmgntframe->attrib;
2079 		update_mgntframe_attrib(padapter, pattrib);
2080 		pattrib->retry_ctrl = false;
2081 
2082 		memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
2083 
2084 		pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
2085 
2086 		memcpy(pframe, (void *)buf, len);
2087 		pattrib->pktlen = len;
2088 
2089 		pwlanhdr = (struct ieee80211_hdr *)pframe;
2090 		/* update seq number */
2091 		pmlmeext->mgnt_seq = GetSequence(pwlanhdr);
2092 		pattrib->seqnum = pmlmeext->mgnt_seq;
2093 		pmlmeext->mgnt_seq++;
2094 
2095 		pattrib->last_txcmdsz = pattrib->pktlen;
2096 
2097 		dump_mgntframe(padapter, pmgntframe);
2098 	}
2099 
2100 fail:
2101 
2102 	dev_kfree_skb_any(skb);
2103 
2104 	return NETDEV_TX_OK;
2105 }
2106 
2107 static const struct net_device_ops rtw_cfg80211_monitor_if_ops = {
2108 	.ndo_start_xmit = rtw_cfg80211_monitor_if_xmit_entry,
2109 };
2110 
rtw_cfg80211_add_monitor_if(struct adapter * padapter,char * name,struct net_device ** ndev)2111 static int rtw_cfg80211_add_monitor_if(struct adapter *padapter, char *name, struct net_device **ndev)
2112 {
2113 	int ret = 0;
2114 	struct net_device *mon_ndev = NULL;
2115 	struct wireless_dev *mon_wdev = NULL;
2116 	struct rtw_netdev_priv_indicator *pnpi;
2117 	struct rtw_wdev_priv *pwdev_priv = adapter_wdev_data(padapter);
2118 
2119 	if (!name) {
2120 		ret = -EINVAL;
2121 		goto out;
2122 	}
2123 
2124 	if (pwdev_priv->pmon_ndev) {
2125 		ret = -EBUSY;
2126 		goto out;
2127 	}
2128 
2129 	mon_ndev = alloc_etherdev(sizeof(struct rtw_netdev_priv_indicator));
2130 	if (!mon_ndev) {
2131 		ret = -ENOMEM;
2132 		goto out;
2133 	}
2134 
2135 	mon_ndev->type = ARPHRD_IEEE80211_RADIOTAP;
2136 	strscpy(mon_ndev->name, name);
2137 	mon_ndev->needs_free_netdev = true;
2138 	mon_ndev->priv_destructor = rtw_ndev_destructor;
2139 
2140 	mon_ndev->netdev_ops = &rtw_cfg80211_monitor_if_ops;
2141 
2142 	pnpi = netdev_priv(mon_ndev);
2143 	pnpi->priv = padapter;
2144 	pnpi->sizeof_priv = sizeof(struct adapter);
2145 
2146 	/*  wdev */
2147 	mon_wdev = rtw_zmalloc(sizeof(struct wireless_dev));
2148 	if (!mon_wdev) {
2149 		ret = -ENOMEM;
2150 		goto out;
2151 	}
2152 
2153 	mon_wdev->wiphy = padapter->rtw_wdev->wiphy;
2154 	mon_wdev->netdev = mon_ndev;
2155 	mon_wdev->iftype = NL80211_IFTYPE_MONITOR;
2156 	mon_ndev->ieee80211_ptr = mon_wdev;
2157 
2158 	ret = cfg80211_register_netdevice(mon_ndev);
2159 	if (ret)
2160 		goto out;
2161 
2162 	*ndev = pwdev_priv->pmon_ndev = mon_ndev;
2163 	memcpy(pwdev_priv->ifname_mon, name, IFNAMSIZ + 1);
2164 
2165 out:
2166 	if (ret && mon_wdev) {
2167 		kfree(mon_wdev);
2168 		mon_wdev = NULL;
2169 	}
2170 
2171 	if (ret && mon_ndev) {
2172 		free_netdev(mon_ndev);
2173 		*ndev = mon_ndev = NULL;
2174 	}
2175 
2176 	return ret;
2177 }
2178 
2179 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)2180 	cfg80211_rtw_add_virtual_intf(
2181 		struct wiphy *wiphy,
2182 		const char *name,
2183 		unsigned char name_assign_type,
2184 		enum nl80211_iftype type, struct vif_params *params)
2185 {
2186 	int ret = 0;
2187 	struct net_device *ndev = NULL;
2188 	struct adapter *padapter = wiphy_to_adapter(wiphy);
2189 
2190 	switch (type) {
2191 	case NL80211_IFTYPE_ADHOC:
2192 	case NL80211_IFTYPE_AP_VLAN:
2193 	case NL80211_IFTYPE_WDS:
2194 	case NL80211_IFTYPE_MESH_POINT:
2195 		ret = -ENODEV;
2196 		break;
2197 	case NL80211_IFTYPE_MONITOR:
2198 		ret = rtw_cfg80211_add_monitor_if(padapter, (char *)name, &ndev);
2199 		break;
2200 	case NL80211_IFTYPE_P2P_CLIENT:
2201 	case NL80211_IFTYPE_STATION:
2202 		ret = -ENODEV;
2203 		break;
2204 	case NL80211_IFTYPE_P2P_GO:
2205 	case NL80211_IFTYPE_AP:
2206 		ret = -ENODEV;
2207 		break;
2208 	default:
2209 		ret = -ENODEV;
2210 		break;
2211 	}
2212 
2213 	return ndev ? ndev->ieee80211_ptr : ERR_PTR(ret);
2214 }
2215 
cfg80211_rtw_del_virtual_intf(struct wiphy * wiphy,struct wireless_dev * wdev)2216 static int cfg80211_rtw_del_virtual_intf(struct wiphy *wiphy,
2217 					 struct wireless_dev *wdev
2218 )
2219 {
2220 	struct net_device *ndev = wdev_to_ndev(wdev);
2221 	int ret = 0;
2222 	struct adapter *adapter;
2223 	struct rtw_wdev_priv *pwdev_priv;
2224 
2225 	if (!ndev) {
2226 		ret = -EINVAL;
2227 		goto exit;
2228 	}
2229 
2230 	adapter = rtw_netdev_priv(ndev);
2231 	pwdev_priv = adapter_wdev_data(adapter);
2232 
2233 	cfg80211_unregister_netdevice(ndev);
2234 
2235 	if (ndev == pwdev_priv->pmon_ndev) {
2236 		pwdev_priv->pmon_ndev = NULL;
2237 		pwdev_priv->ifname_mon[0] = '\0';
2238 	}
2239 
2240 exit:
2241 	return ret;
2242 }
2243 
rtw_add_beacon(struct adapter * adapter,const u8 * head,size_t head_len,const u8 * tail,size_t tail_len)2244 static int rtw_add_beacon(struct adapter *adapter, const u8 *head, size_t head_len, const u8 *tail, size_t tail_len)
2245 {
2246 	int ret = 0;
2247 	u8 *pbuf = NULL;
2248 	uint len, wps_ielen = 0;
2249 	struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
2250 
2251 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE) != true)
2252 		return -EINVAL;
2253 
2254 	if (head_len < 24)
2255 		return -EINVAL;
2256 
2257 	pbuf = rtw_zmalloc(head_len + tail_len);
2258 	if (!pbuf)
2259 		return -ENOMEM;
2260 
2261 	memcpy(pbuf, (void *)head + 24, head_len - 24);/*  24 =beacon header len. */
2262 	memcpy(pbuf + head_len - 24, (void *)tail, tail_len);
2263 
2264 	len = head_len + tail_len - 24;
2265 
2266 	/* check wps ie if inclued */
2267 	rtw_get_wps_ie(pbuf + _FIXED_IE_LENGTH_, len - _FIXED_IE_LENGTH_, NULL, &wps_ielen);
2268 
2269 	/* pbss_network->ies will not include p2p_ie, wfd ie */
2270 	rtw_ies_remove_ie(pbuf, &len, _BEACON_IE_OFFSET_, WLAN_EID_VENDOR_SPECIFIC, P2P_OUI, 4);
2271 	rtw_ies_remove_ie(pbuf, &len, _BEACON_IE_OFFSET_, WLAN_EID_VENDOR_SPECIFIC, WFD_OUI, 4);
2272 
2273 	if (rtw_check_beacon_data(adapter, pbuf,  len) == _SUCCESS)
2274 		ret = 0;
2275 	else
2276 		ret = -EINVAL;
2277 
2278 	kfree(pbuf);
2279 
2280 	return ret;
2281 }
2282 
cfg80211_rtw_start_ap(struct wiphy * wiphy,struct net_device * ndev,struct cfg80211_ap_settings * settings)2283 static int cfg80211_rtw_start_ap(struct wiphy *wiphy, struct net_device *ndev,
2284 				 struct cfg80211_ap_settings *settings)
2285 {
2286 	int ret = 0;
2287 	struct adapter *adapter = rtw_netdev_priv(ndev);
2288 
2289 	ret = rtw_add_beacon(adapter, settings->beacon.head,
2290 			     settings->beacon.head_len, settings->beacon.tail,
2291 			     settings->beacon.tail_len);
2292 
2293 	adapter->mlmeextpriv.mlmext_info.hidden_ssid_mode = settings->hidden_ssid;
2294 
2295 	if (settings->ssid && settings->ssid_len) {
2296 		struct wlan_bssid_ex *pbss_network = &adapter->mlmepriv.cur_network.network;
2297 		struct wlan_bssid_ex *pbss_network_ext = &adapter->mlmeextpriv.mlmext_info.network;
2298 
2299 		memcpy(pbss_network->ssid.ssid, (void *)settings->ssid, settings->ssid_len);
2300 		pbss_network->ssid.ssid_length = settings->ssid_len;
2301 		memcpy(pbss_network_ext->ssid.ssid, (void *)settings->ssid, settings->ssid_len);
2302 		pbss_network_ext->ssid.ssid_length = settings->ssid_len;
2303 	}
2304 
2305 	return ret;
2306 }
2307 
cfg80211_rtw_change_beacon(struct wiphy * wiphy,struct net_device * ndev,struct cfg80211_ap_update * info)2308 static int cfg80211_rtw_change_beacon(struct wiphy *wiphy, struct net_device *ndev,
2309 		struct cfg80211_ap_update *info)
2310 {
2311 	struct adapter *adapter = rtw_netdev_priv(ndev);
2312 
2313 	return rtw_add_beacon(adapter, info->beacon.head,
2314 			      info->beacon.head_len, info->beacon.tail,
2315 			      info->beacon.tail_len);
2316 }
2317 
cfg80211_rtw_stop_ap(struct wiphy * wiphy,struct net_device * ndev,unsigned int link_id)2318 static int cfg80211_rtw_stop_ap(struct wiphy *wiphy, struct net_device *ndev,
2319 				unsigned int link_id)
2320 {
2321 	return 0;
2322 }
2323 
cfg80211_rtw_add_station(struct wiphy * wiphy,struct net_device * ndev,const u8 * mac,struct station_parameters * params)2324 static int	cfg80211_rtw_add_station(struct wiphy *wiphy,
2325 					 struct net_device *ndev,
2326 					 const u8 *mac,
2327 					 struct station_parameters *params)
2328 {
2329 	return 0;
2330 }
2331 
cfg80211_rtw_del_station(struct wiphy * wiphy,struct net_device * ndev,struct station_del_parameters * params)2332 static int cfg80211_rtw_del_station(struct wiphy *wiphy, struct net_device *ndev,
2333 				    struct station_del_parameters *params)
2334 {
2335 	int ret = 0;
2336 	struct list_head *phead, *plist, *tmp;
2337 	u8 updated = false;
2338 	struct sta_info *psta = NULL;
2339 	struct adapter *padapter = rtw_netdev_priv(ndev);
2340 	struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
2341 	struct sta_priv *pstapriv = &padapter->stapriv;
2342 	const u8 *mac = params->mac;
2343 
2344 	if (check_fwstate(pmlmepriv, (_FW_LINKED | WIFI_AP_STATE)) != true)
2345 		return -EINVAL;
2346 
2347 	if (!mac) {
2348 		flush_all_cam_entry(padapter);	/* clear CAM */
2349 
2350 		rtw_sta_flush(padapter);
2351 
2352 		return 0;
2353 	}
2354 
2355 	if (mac[0] == 0xff && mac[1] == 0xff &&
2356 	    mac[2] == 0xff && mac[3] == 0xff &&
2357 	    mac[4] == 0xff && mac[5] == 0xff) {
2358 		return -EINVAL;
2359 	}
2360 
2361 	spin_lock_bh(&pstapriv->asoc_list_lock);
2362 
2363 	phead = &pstapriv->asoc_list;
2364 	/* check asoc_queue */
2365 	list_for_each_safe(plist, tmp, phead) {
2366 		psta = list_entry(plist, struct sta_info, asoc_list);
2367 
2368 		if (!memcmp((u8 *)mac, psta->hwaddr, ETH_ALEN)) {
2369 			if (psta->dot8021xalg != 1 || psta->bpairwise_key_installed) {
2370 				list_del_init(&psta->asoc_list);
2371 				pstapriv->asoc_list_cnt--;
2372 
2373 				updated = ap_free_sta(padapter, psta, true, WLAN_REASON_DEAUTH_LEAVING);
2374 
2375 				psta = NULL;
2376 
2377 				break;
2378 			}
2379 		}
2380 	}
2381 
2382 	spin_unlock_bh(&pstapriv->asoc_list_lock);
2383 
2384 	associated_clients_update(padapter, updated);
2385 
2386 	return ret;
2387 }
2388 
cfg80211_rtw_change_station(struct wiphy * wiphy,struct net_device * ndev,const u8 * mac,struct station_parameters * params)2389 static int cfg80211_rtw_change_station(struct wiphy *wiphy,
2390 				       struct net_device *ndev,
2391 				       const u8 *mac,
2392 				       struct station_parameters *params)
2393 {
2394 	return 0;
2395 }
2396 
rtw_sta_info_get_by_idx(const int idx,struct sta_priv * pstapriv)2397 static struct sta_info *rtw_sta_info_get_by_idx(const int idx, struct sta_priv *pstapriv)
2398 
2399 {
2400 	struct list_head	*phead, *plist;
2401 	struct sta_info *psta = NULL;
2402 	int i = 0;
2403 
2404 	phead = &pstapriv->asoc_list;
2405 	plist = get_next(phead);
2406 
2407 	/* check asoc_queue */
2408 	while (phead != plist) {
2409 		if (idx == i)
2410 			psta = container_of(plist, struct sta_info, asoc_list);
2411 		plist = get_next(plist);
2412 		i++;
2413 	}
2414 	return psta;
2415 }
2416 
cfg80211_rtw_dump_station(struct wiphy * wiphy,struct net_device * ndev,int idx,u8 * mac,struct station_info * sinfo)2417 static int	cfg80211_rtw_dump_station(struct wiphy *wiphy,
2418 					  struct net_device *ndev,
2419 					  int idx, u8 *mac,
2420 					  struct station_info *sinfo)
2421 {
2422 	int ret = 0;
2423 	struct adapter *padapter = rtw_netdev_priv(ndev);
2424 	struct sta_info *psta = NULL;
2425 	struct sta_priv *pstapriv = &padapter->stapriv;
2426 
2427 	spin_lock_bh(&pstapriv->asoc_list_lock);
2428 	psta = rtw_sta_info_get_by_idx(idx, pstapriv);
2429 	spin_unlock_bh(&pstapriv->asoc_list_lock);
2430 	if (psta == NULL) {
2431 		ret = -ENOENT;
2432 		goto exit;
2433 	}
2434 	memcpy(mac, psta->hwaddr, ETH_ALEN);
2435 	sinfo->filled = BIT_ULL(NL80211_STA_INFO_SIGNAL);
2436 	sinfo->signal = psta->rssi;
2437 
2438 exit:
2439 	return ret;
2440 }
2441 
cfg80211_rtw_change_bss(struct wiphy * wiphy,struct net_device * ndev,struct bss_parameters * params)2442 static int	cfg80211_rtw_change_bss(struct wiphy *wiphy,
2443 					struct net_device *ndev,
2444 					struct bss_parameters *params)
2445 {
2446 	return 0;
2447 }
2448 
rtw_cfg80211_rx_action(struct adapter * adapter,u8 * frame,uint frame_len,const char * msg)2449 void rtw_cfg80211_rx_action(struct adapter *adapter, u8 *frame, uint frame_len, const char *msg)
2450 {
2451 	s32 freq;
2452 	int channel;
2453 	u8 category, action;
2454 
2455 	channel = rtw_get_oper_ch(adapter);
2456 
2457 	rtw_action_frame_parse(frame, frame_len, &category, &action);
2458 
2459 	freq = rtw_ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
2460 
2461 	rtw_cfg80211_rx_mgmt(adapter, freq, 0, frame, frame_len, GFP_ATOMIC);
2462 }
2463 
_cfg80211_rtw_mgmt_tx(struct adapter * padapter,u8 tx_ch,const u8 * buf,size_t len)2464 static int _cfg80211_rtw_mgmt_tx(struct adapter *padapter, u8 tx_ch, const u8 *buf, size_t len)
2465 {
2466 	struct xmit_frame	*pmgntframe;
2467 	struct pkt_attrib	*pattrib;
2468 	unsigned char *pframe;
2469 	int ret = _FAIL;
2470 	bool __maybe_unused ack = true;
2471 	struct ieee80211_hdr *pwlanhdr;
2472 	struct xmit_priv *pxmitpriv = &(padapter->xmitpriv);
2473 	struct mlme_ext_priv *pmlmeext = &(padapter->mlmeextpriv);
2474 
2475 	rtw_set_scan_deny(padapter, 1000);
2476 
2477 	rtw_scan_abort(padapter);
2478 	if (tx_ch != rtw_get_oper_ch(padapter)) {
2479 		if (!check_fwstate(&padapter->mlmepriv, _FW_LINKED))
2480 			pmlmeext->cur_channel = tx_ch;
2481 		set_channel_bwmode(padapter, tx_ch, HAL_PRIME_CHNL_OFFSET_DONT_CARE, CHANNEL_WIDTH_20);
2482 	}
2483 
2484 	/* starting alloc mgmt frame to dump it */
2485 	pmgntframe = alloc_mgtxmitframe(pxmitpriv);
2486 	if (!pmgntframe) {
2487 		/* ret = -ENOMEM; */
2488 		ret = _FAIL;
2489 		goto exit;
2490 	}
2491 
2492 	/* update attribute */
2493 	pattrib = &pmgntframe->attrib;
2494 	update_mgntframe_attrib(padapter, pattrib);
2495 	pattrib->retry_ctrl = false;
2496 
2497 	memset(pmgntframe->buf_addr, 0, WLANHDR_OFFSET + TXDESC_OFFSET);
2498 
2499 	pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
2500 
2501 	memcpy(pframe, (void *)buf, len);
2502 	pattrib->pktlen = len;
2503 
2504 	pwlanhdr = (struct ieee80211_hdr *)pframe;
2505 	/* update seq number */
2506 	pmlmeext->mgnt_seq = GetSequence(pwlanhdr);
2507 	pattrib->seqnum = pmlmeext->mgnt_seq;
2508 	pmlmeext->mgnt_seq++;
2509 
2510 	pattrib->last_txcmdsz = pattrib->pktlen;
2511 
2512 	if (dump_mgntframe_and_wait_ack(padapter, pmgntframe) != _SUCCESS) {
2513 		ack = false;
2514 		ret = _FAIL;
2515 
2516 	} else {
2517 		msleep(50);
2518 
2519 		ret = _SUCCESS;
2520 	}
2521 
2522 exit:
2523 
2524 	return ret;
2525 }
2526 
cfg80211_rtw_mgmt_tx(struct wiphy * wiphy,struct wireless_dev * wdev,struct cfg80211_mgmt_tx_params * params,u64 * cookie)2527 static int cfg80211_rtw_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
2528 				struct cfg80211_mgmt_tx_params *params,
2529 				u64 *cookie)
2530 {
2531 	struct net_device *ndev = wdev_to_ndev(wdev);
2532 	struct ieee80211_channel *chan = params->chan;
2533 	const u8 *buf = params->buf;
2534 	size_t len = params->len;
2535 	int ret = 0;
2536 	int tx_ret;
2537 	u32 dump_limit = RTW_MAX_MGMT_TX_CNT;
2538 	u32 dump_cnt = 0;
2539 	bool ack = true;
2540 	u8 tx_ch = (u8)ieee80211_frequency_to_channel(chan->center_freq);
2541 	u8 category, action;
2542 	struct adapter *padapter;
2543 
2544 	if (!ndev) {
2545 		ret = -EINVAL;
2546 		goto exit;
2547 	}
2548 
2549 	padapter = rtw_netdev_priv(ndev);
2550 
2551 	/* cookie generation */
2552 	*cookie = (unsigned long)buf;
2553 
2554 	/* indicate ack before issue frame to avoid racing with rsp frame */
2555 	rtw_cfg80211_mgmt_tx_status(padapter, *cookie, buf, len, ack, GFP_KERNEL);
2556 
2557 	if (rtw_action_frame_parse(buf, len, &category, &action) == false)
2558 		goto exit;
2559 
2560 	rtw_ps_deny(padapter, PS_DENY_MGNT_TX);
2561 	if (rtw_pwr_wakeup(padapter) == _FAIL) {
2562 		ret = -EFAULT;
2563 		goto cancel_ps_deny;
2564 	}
2565 
2566 	do {
2567 		dump_cnt++;
2568 		tx_ret = _cfg80211_rtw_mgmt_tx(padapter, tx_ch, buf, len);
2569 	} while (dump_cnt < dump_limit && tx_ret != _SUCCESS);
2570 
2571 cancel_ps_deny:
2572 	rtw_ps_deny_cancel(padapter, PS_DENY_MGNT_TX);
2573 exit:
2574 	return ret;
2575 }
2576 
rtw_cfg80211_init_ht_capab(struct ieee80211_sta_ht_cap * ht_cap,enum nl80211_band band)2577 static void rtw_cfg80211_init_ht_capab(struct ieee80211_sta_ht_cap *ht_cap, enum nl80211_band band)
2578 {
2579 #define MAX_BIT_RATE_40MHZ_MCS7		150	/* Mbps */
2580 
2581 	ht_cap->ht_supported = true;
2582 
2583 	ht_cap->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2584 					IEEE80211_HT_CAP_SGI_40 | IEEE80211_HT_CAP_SGI_20 |
2585 					IEEE80211_HT_CAP_DSSSCCK40 | IEEE80211_HT_CAP_MAX_AMSDU;
2586 
2587 	/*
2588 	 *Maximum length of AMPDU that the STA can receive.
2589 	 *Length = 2 ^ (13 + max_ampdu_length_exp) - 1 (octets)
2590 	 */
2591 	ht_cap->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2592 
2593 	/*Minimum MPDU start spacing , */
2594 	ht_cap->ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
2595 
2596 	ht_cap->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2597 
2598 	/*
2599 	 *hw->wiphy->bands[NL80211_BAND_2GHZ]
2600 	 *base on ant_num
2601 	 *rx_mask: RX mask
2602 	 *if rx_ant = 1 rx_mask[0]= 0xff;==>MCS0-MCS7
2603 	 *if rx_ant =2 rx_mask[1]= 0xff;==>MCS8-MCS15
2604 	 *if rx_ant >=3 rx_mask[2]= 0xff;
2605 	 *if BW_40 rx_mask[4]= 0x01;
2606 	 *highest supported RX rate
2607 	 */
2608 	ht_cap->mcs.rx_mask[0] = 0xFF;
2609 	ht_cap->mcs.rx_mask[1] = 0x00;
2610 	ht_cap->mcs.rx_mask[4] = 0x01;
2611 
2612 	ht_cap->mcs.rx_highest = cpu_to_le16(MAX_BIT_RATE_40MHZ_MCS7);
2613 }
2614 
rtw_cfg80211_init_wiphy(struct adapter * padapter)2615 void rtw_cfg80211_init_wiphy(struct adapter *padapter)
2616 {
2617 	struct ieee80211_supported_band *bands;
2618 	struct wireless_dev *pwdev = padapter->rtw_wdev;
2619 	struct wiphy *wiphy = pwdev->wiphy;
2620 
2621 	{
2622 		bands = wiphy->bands[NL80211_BAND_2GHZ];
2623 		if (bands)
2624 			rtw_cfg80211_init_ht_capab(&bands->ht_cap, NL80211_BAND_2GHZ);
2625 	}
2626 
2627 	/* copy mac_addr to wiphy */
2628 	memcpy(wiphy->perm_addr, padapter->eeprompriv.mac_addr, ETH_ALEN);
2629 }
2630 
rtw_cfg80211_preinit_wiphy(struct adapter * padapter,struct wiphy * wiphy)2631 static void rtw_cfg80211_preinit_wiphy(struct adapter *padapter, struct wiphy *wiphy)
2632 {
2633 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
2634 
2635 	wiphy->max_scan_ssids = RTW_SSID_SCAN_AMOUNT;
2636 	wiphy->max_scan_ie_len = RTW_SCAN_IE_LEN_MAX;
2637 	wiphy->max_num_pmkids = RTW_MAX_NUM_PMKIDS;
2638 
2639 	wiphy->max_remain_on_channel_duration = RTW_MAX_REMAIN_ON_CHANNEL_DURATION;
2640 
2641 	wiphy->interface_modes =	BIT(NL80211_IFTYPE_STATION)
2642 								| BIT(NL80211_IFTYPE_ADHOC)
2643 								| BIT(NL80211_IFTYPE_AP)
2644 								| BIT(NL80211_IFTYPE_MONITOR)
2645 								;
2646 
2647 	wiphy->mgmt_stypes = rtw_cfg80211_default_mgmt_stypes;
2648 
2649 	wiphy->software_iftypes |= BIT(NL80211_IFTYPE_MONITOR);
2650 
2651 	wiphy->cipher_suites = rtw_cipher_suites;
2652 	wiphy->n_cipher_suites = ARRAY_SIZE(rtw_cipher_suites);
2653 
2654 	/* if (padapter->registrypriv.wireless_mode & WIRELESS_11G) */
2655 	wiphy->bands[NL80211_BAND_2GHZ] = rtw_spt_band_alloc(NL80211_BAND_2GHZ);
2656 
2657 	wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
2658 	wiphy->flags |= WIPHY_FLAG_OFFCHAN_TX | WIPHY_FLAG_HAVE_AP_SME;
2659 
2660 #if defined(CONFIG_PM)
2661 	wiphy->max_sched_scan_reqs = 1;
2662 #endif
2663 
2664 #if defined(CONFIG_PM)
2665 	wiphy->wowlan = &wowlan_stub;
2666 #endif
2667 
2668 	if (padapter->registrypriv.power_mgnt != PS_MODE_ACTIVE)
2669 		wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
2670 	else
2671 		wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
2672 }
2673 
2674 static struct cfg80211_ops rtw_cfg80211_ops = {
2675 	.change_virtual_intf = cfg80211_rtw_change_iface,
2676 	.add_key = cfg80211_rtw_add_key,
2677 	.get_key = cfg80211_rtw_get_key,
2678 	.del_key = cfg80211_rtw_del_key,
2679 	.set_default_key = cfg80211_rtw_set_default_key,
2680 	.get_station = cfg80211_rtw_get_station,
2681 	.scan = cfg80211_rtw_scan,
2682 	.set_wiphy_params = cfg80211_rtw_set_wiphy_params,
2683 	.connect = cfg80211_rtw_connect,
2684 	.disconnect = cfg80211_rtw_disconnect,
2685 	.join_ibss = cfg80211_rtw_join_ibss,
2686 	.leave_ibss = cfg80211_rtw_leave_ibss,
2687 	.set_tx_power = cfg80211_rtw_set_txpower,
2688 	.get_tx_power = cfg80211_rtw_get_txpower,
2689 	.set_power_mgmt = cfg80211_rtw_set_power_mgmt,
2690 	.set_pmksa = cfg80211_rtw_set_pmksa,
2691 	.del_pmksa = cfg80211_rtw_del_pmksa,
2692 	.flush_pmksa = cfg80211_rtw_flush_pmksa,
2693 	.get_channel = cfg80211_rtw_get_channel,
2694 	.add_virtual_intf = cfg80211_rtw_add_virtual_intf,
2695 	.del_virtual_intf = cfg80211_rtw_del_virtual_intf,
2696 
2697 	.start_ap = cfg80211_rtw_start_ap,
2698 	.change_beacon = cfg80211_rtw_change_beacon,
2699 	.stop_ap = cfg80211_rtw_stop_ap,
2700 
2701 	.add_station = cfg80211_rtw_add_station,
2702 	.del_station = cfg80211_rtw_del_station,
2703 	.change_station = cfg80211_rtw_change_station,
2704 	.dump_station = cfg80211_rtw_dump_station,
2705 	.change_bss = cfg80211_rtw_change_bss,
2706 
2707 	.mgmt_tx = cfg80211_rtw_mgmt_tx,
2708 };
2709 
rtw_wdev_alloc(struct adapter * padapter,struct device * dev)2710 int rtw_wdev_alloc(struct adapter *padapter, struct device *dev)
2711 {
2712 	int ret = 0;
2713 	struct wiphy *wiphy;
2714 	struct wireless_dev *wdev;
2715 	struct rtw_wdev_priv *pwdev_priv;
2716 	struct net_device *pnetdev = padapter->pnetdev;
2717 
2718 	/* wiphy */
2719 	wiphy = wiphy_new(&rtw_cfg80211_ops, sizeof(struct adapter *));
2720 	if (!wiphy) {
2721 		ret = -ENOMEM;
2722 		goto exit;
2723 	}
2724 	set_wiphy_dev(wiphy, dev);
2725 	*((struct adapter **)wiphy_priv(wiphy)) = padapter;
2726 	rtw_cfg80211_preinit_wiphy(padapter, wiphy);
2727 
2728 	/* init regulary domain */
2729 	rtw_regd_init(wiphy, rtw_reg_notifier);
2730 
2731 	ret = wiphy_register(wiphy);
2732 	if (ret < 0)
2733 		goto free_wiphy;
2734 
2735 	/*  wdev */
2736 	wdev = rtw_zmalloc(sizeof(struct wireless_dev));
2737 	if (!wdev) {
2738 		ret = -ENOMEM;
2739 		goto unregister_wiphy;
2740 	}
2741 	wdev->wiphy = wiphy;
2742 	wdev->netdev = pnetdev;
2743 
2744 	wdev->iftype = NL80211_IFTYPE_STATION; /*  will be init in rtw_hal_init() */
2745 					   /*  Must sync with _rtw_init_mlme_priv() */
2746 					   /*  pmlmepriv->fw_state = WIFI_STATION_STATE */
2747 	padapter->rtw_wdev = wdev;
2748 	pnetdev->ieee80211_ptr = wdev;
2749 
2750 	/* init pwdev_priv */
2751 	pwdev_priv = adapter_wdev_data(padapter);
2752 	pwdev_priv->rtw_wdev = wdev;
2753 	pwdev_priv->pmon_ndev = NULL;
2754 	pwdev_priv->ifname_mon[0] = '\0';
2755 	pwdev_priv->padapter = padapter;
2756 	pwdev_priv->scan_request = NULL;
2757 	spin_lock_init(&pwdev_priv->scan_req_lock);
2758 
2759 	pwdev_priv->p2p_enabled = false;
2760 	pwdev_priv->provdisc_req_issued = false;
2761 	rtw_wdev_invit_info_init(&pwdev_priv->invit_info);
2762 	rtw_wdev_nego_info_init(&pwdev_priv->nego_info);
2763 
2764 	pwdev_priv->bandroid_scan = false;
2765 
2766 	if (padapter->registrypriv.power_mgnt != PS_MODE_ACTIVE)
2767 		pwdev_priv->power_mgmt = true;
2768 	else
2769 		pwdev_priv->power_mgmt = false;
2770 
2771 	return ret;
2772 
2773 unregister_wiphy:
2774 	wiphy_unregister(wiphy);
2775  free_wiphy:
2776 	wiphy_free(wiphy);
2777 exit:
2778 	return ret;
2779 }
2780 
rtw_wdev_free(struct wireless_dev * wdev)2781 void rtw_wdev_free(struct wireless_dev *wdev)
2782 {
2783 	if (!wdev)
2784 		return;
2785 
2786 	kfree(wdev->wiphy->bands[NL80211_BAND_2GHZ]);
2787 
2788 	wiphy_free(wdev->wiphy);
2789 
2790 	kfree(wdev);
2791 }
2792 
rtw_wdev_unregister(struct wireless_dev * wdev)2793 void rtw_wdev_unregister(struct wireless_dev *wdev)
2794 {
2795 	struct net_device *ndev;
2796 	struct adapter *adapter;
2797 	struct rtw_wdev_priv *pwdev_priv;
2798 
2799 	if (!wdev)
2800 		return;
2801 	ndev = wdev_to_ndev(wdev);
2802 	if (!ndev)
2803 		return;
2804 
2805 	adapter = rtw_netdev_priv(ndev);
2806 	pwdev_priv = adapter_wdev_data(adapter);
2807 
2808 	rtw_cfg80211_indicate_scan_done(adapter, true);
2809 
2810 	if (pwdev_priv->pmon_ndev)
2811 		unregister_netdev(pwdev_priv->pmon_ndev);
2812 
2813 	wiphy_unregister(wdev->wiphy);
2814 }
2815