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