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