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