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