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