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