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