1 // SPDX-License-Identifier: GPL-2.0 2 /****************************************************************************** 3 * 4 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved. 5 * 6 ******************************************************************************/ 7 8 #include <drv_types.h> 9 #include <linux/of.h> 10 #include <linux/unaligned.h> 11 12 u8 RTW_WPA_OUI_TYPE[] = { 0x00, 0x50, 0xf2, 1 }; 13 u16 RTW_WPA_VERSION = 1; 14 u8 WPA_AUTH_KEY_MGMT_NONE[] = { 0x00, 0x50, 0xf2, 0 }; 15 u8 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x50, 0xf2, 1 }; 16 u8 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x50, 0xf2, 2 }; 17 u8 WPA_CIPHER_SUITE_NONE[] = { 0x00, 0x50, 0xf2, 0 }; 18 u8 WPA_CIPHER_SUITE_WEP40[] = { 0x00, 0x50, 0xf2, 1 }; 19 u8 WPA_CIPHER_SUITE_TKIP[] = { 0x00, 0x50, 0xf2, 2 }; 20 u8 WPA_CIPHER_SUITE_WRAP[] = { 0x00, 0x50, 0xf2, 3 }; 21 u8 WPA_CIPHER_SUITE_CCMP[] = { 0x00, 0x50, 0xf2, 4 }; 22 u8 WPA_CIPHER_SUITE_WEP104[] = { 0x00, 0x50, 0xf2, 5 }; 23 24 u16 RSN_VERSION_BSD = 1; 25 u8 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x0f, 0xac, 1 }; 26 u8 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x0f, 0xac, 2 }; 27 u8 RSN_CIPHER_SUITE_NONE[] = { 0x00, 0x0f, 0xac, 0 }; 28 u8 RSN_CIPHER_SUITE_WEP40[] = { 0x00, 0x0f, 0xac, 1 }; 29 u8 RSN_CIPHER_SUITE_TKIP[] = { 0x00, 0x0f, 0xac, 2 }; 30 u8 RSN_CIPHER_SUITE_WRAP[] = { 0x00, 0x0f, 0xac, 3 }; 31 u8 RSN_CIPHER_SUITE_CCMP[] = { 0x00, 0x0f, 0xac, 4 }; 32 u8 RSN_CIPHER_SUITE_WEP104[] = { 0x00, 0x0f, 0xac, 5 }; 33 /* */ 34 /* for adhoc-master to generate ie and provide supported-rate to fw */ 35 /* */ 36 37 static u8 WIFI_CCKRATES[] = { 38 (IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK), 39 (IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK), 40 (IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK), 41 (IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK) 42 }; 43 44 static u8 WIFI_OFDMRATES[] = { 45 (IEEE80211_OFDM_RATE_6MB), 46 (IEEE80211_OFDM_RATE_9MB), 47 (IEEE80211_OFDM_RATE_12MB), 48 (IEEE80211_OFDM_RATE_18MB), 49 (IEEE80211_OFDM_RATE_24MB), 50 IEEE80211_OFDM_RATE_36MB, 51 IEEE80211_OFDM_RATE_48MB, 52 IEEE80211_OFDM_RATE_54MB 53 }; 54 rtw_get_bit_value_from_ieee_value(u8 val)55 int rtw_get_bit_value_from_ieee_value(u8 val) 56 { 57 static const unsigned char dot11_rate_table[] = { 58 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108, 0 59 }; /* last element must be zero!! */ 60 int i = 0; 61 62 while (dot11_rate_table[i] != 0) { 63 if (dot11_rate_table[i] == val) 64 return BIT(i); 65 i++; 66 } 67 return 0; 68 } 69 rtw_is_cckrates_included(u8 * rate)70 bool rtw_is_cckrates_included(u8 *rate) 71 { 72 while (*rate) { 73 u8 r = *rate & 0x7f; 74 75 if (r == 2 || r == 4 || r == 11 || r == 22) 76 return true; 77 rate++; 78 } 79 80 return false; 81 } 82 rtw_is_cckratesonly_included(u8 * rate)83 bool rtw_is_cckratesonly_included(u8 *rate) 84 { 85 while (*rate) { 86 u8 r = *rate & 0x7f; 87 88 if (r != 2 && r != 4 && r != 11 && r != 22) 89 return false; 90 rate++; 91 } 92 93 return true; 94 } 95 rtw_check_network_type(unsigned char * rate,int ratelen,int channel)96 int rtw_check_network_type(unsigned char *rate, int ratelen, int channel) 97 { 98 if (channel > 14) 99 return WIRELESS_INVALID; 100 /* could be pure B, pure G, or B/G */ 101 if (rtw_is_cckratesonly_included(rate)) 102 return WIRELESS_11B; 103 if (rtw_is_cckrates_included(rate)) 104 return WIRELESS_11BG; 105 return WIRELESS_11G; 106 } 107 rtw_set_fixed_ie(unsigned char * pbuf,unsigned int len,unsigned char * source,unsigned int * frlen)108 u8 *rtw_set_fixed_ie(unsigned char *pbuf, unsigned int len, unsigned char *source, 109 unsigned int *frlen) 110 { 111 memcpy((void *)pbuf, (void *)source, len); 112 *frlen = *frlen + len; 113 return pbuf + len; 114 } 115 116 /* rtw_set_ie will update frame length */ rtw_set_ie(u8 * pbuf,signed int index,uint len,u8 * source,uint * frlen)117 u8 *rtw_set_ie(u8 *pbuf, 118 signed int index, 119 uint len, 120 u8 *source, 121 uint *frlen) /* frame length */ 122 { 123 *pbuf = (u8)index; 124 125 *(pbuf + 1) = (u8)len; 126 127 if (len > 0) 128 memcpy((void *)(pbuf + 2), (void *)source, len); 129 130 *frlen = *frlen + (len + 2); 131 132 return pbuf + len + 2; 133 } 134 135 /*---------------------------------------------------------------------------- 136 index: the information element id index, limit is the limit for search 137 -----------------------------------------------------------------------------*/ rtw_get_ie(u8 * pbuf,signed int index,signed int * len,signed int limit)138 u8 *rtw_get_ie(u8 *pbuf, signed int index, signed int *len, signed int limit) 139 { 140 signed int tmp, i; 141 u8 *p; 142 143 if (limit < 1) 144 return NULL; 145 146 p = pbuf; 147 i = 0; 148 *len = 0; 149 while (1) { 150 if (*p == index) { 151 *len = *(p + 1); 152 return p; 153 } 154 tmp = *(p + 1); 155 p += (tmp + 2); 156 i += (tmp + 2); 157 if (i >= limit) 158 break; 159 } 160 return NULL; 161 } 162 163 /** 164 * rtw_get_ie_ex - Search specific IE from a series of IEs 165 * @in_ie: Address of IEs to search 166 * @in_len: Length limit from in_ie 167 * @eid: Element ID to match 168 * @oui: OUI to match 169 * @oui_len: OUI length 170 * @ie: If not NULL and the specific IE is found, the IE will be copied to the buf starting from the specific IE 171 * @ielen: If not NULL and the specific IE is found, will set to the length of the entire IE 172 * 173 * Returns: The address of the specific IE found, or NULL 174 */ rtw_get_ie_ex(u8 * in_ie,uint in_len,u8 eid,u8 * oui,u8 oui_len,u8 * ie,uint * ielen)175 u8 *rtw_get_ie_ex(u8 *in_ie, uint in_len, u8 eid, u8 *oui, u8 oui_len, u8 *ie, uint *ielen) 176 { 177 uint cnt; 178 u8 *target_ie = NULL; 179 180 if (ielen) 181 *ielen = 0; 182 183 if (!in_ie || in_len <= 0) 184 return target_ie; 185 186 cnt = 0; 187 188 while (cnt < in_len) { 189 if (eid == in_ie[cnt] 190 && (!oui || !memcmp(&in_ie[cnt+2], oui, oui_len))) { 191 target_ie = &in_ie[cnt]; 192 193 if (ie) 194 memcpy(ie, &in_ie[cnt], in_ie[cnt+1]+2); 195 196 if (ielen) 197 *ielen = in_ie[cnt+1]+2; 198 199 break; 200 } 201 cnt += in_ie[cnt+1]+2; /* goto next */ 202 } 203 204 return target_ie; 205 } 206 207 /** 208 * rtw_ies_remove_ie - Find matching IEs and remove 209 * @ies: Address of IEs to search 210 * @ies_len: Pointer of length of ies, will update to new length 211 * @offset: The offset to start search 212 * @eid: Element ID to match 213 * @oui: OUI to match 214 * @oui_len: OUI length 215 * 216 * Returns: _SUCCESS: ies is updated, _FAIL: not updated 217 */ rtw_ies_remove_ie(u8 * ies,uint * ies_len,uint offset,u8 eid,u8 * oui,u8 oui_len)218 int rtw_ies_remove_ie(u8 *ies, uint *ies_len, uint offset, u8 eid, u8 *oui, u8 oui_len) 219 { 220 int ret = _FAIL; 221 u8 *target_ie; 222 u32 target_ielen; 223 u8 *start; 224 uint search_len; 225 226 if (!ies || !ies_len || *ies_len <= offset) 227 goto exit; 228 229 start = ies + offset; 230 search_len = *ies_len - offset; 231 232 while (1) { 233 target_ie = rtw_get_ie_ex(start, search_len, eid, oui, oui_len, NULL, &target_ielen); 234 if (target_ie && target_ielen) { 235 u8 *remain_ies = target_ie + target_ielen; 236 uint remain_len = search_len - (remain_ies - start); 237 238 memcpy(target_ie, remain_ies, remain_len); 239 *ies_len = *ies_len - target_ielen; 240 ret = _SUCCESS; 241 242 start = target_ie; 243 search_len = remain_len; 244 } else { 245 break; 246 } 247 } 248 exit: 249 return ret; 250 } 251 rtw_set_supported_rate(u8 * supported_rates,uint mode)252 void rtw_set_supported_rate(u8 *supported_rates, uint mode) 253 { 254 memset(supported_rates, 0, NDIS_802_11_LENGTH_RATES_EX); 255 256 switch (mode) { 257 case WIRELESS_11B: 258 memcpy(supported_rates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN); 259 break; 260 261 case WIRELESS_11G: 262 memcpy(supported_rates, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN); 263 break; 264 265 case WIRELESS_11BG: 266 case WIRELESS_11G_24N: 267 case WIRELESS_11_24N: 268 case WIRELESS_11BG_24N: 269 memcpy(supported_rates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN); 270 memcpy(supported_rates + IEEE80211_CCK_RATE_LEN, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN); 271 break; 272 } 273 } 274 rtw_get_rateset_len(u8 * rateset)275 uint rtw_get_rateset_len(u8 *rateset) 276 { 277 uint i; 278 279 for (i = 0; i < 13; i++) 280 if (rateset[i] == 0) 281 break; 282 return i; 283 } 284 rtw_generate_ie(struct registry_priv * pregistrypriv)285 int rtw_generate_ie(struct registry_priv *pregistrypriv) 286 { 287 u8 wireless_mode; 288 int sz = 0, rateLen; 289 struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network; 290 u8 *ie = pdev_network->ies; 291 292 /* timestamp will be inserted by hardware */ 293 sz += 8; 294 ie += sz; 295 296 /* beacon interval : 2bytes */ 297 *(__le16 *)ie = cpu_to_le16((u16)pdev_network->configuration.beacon_period);/* BCN_INTERVAL; */ 298 sz += 2; 299 ie += 2; 300 301 /* capability info */ 302 *(u16 *)ie = 0; 303 304 *(__le16 *)ie |= cpu_to_le16(WLAN_CAPABILITY_IBSS); 305 306 if (pregistrypriv->preamble == PREAMBLE_SHORT) 307 *(__le16 *)ie |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE); 308 309 if (pdev_network->privacy) 310 *(__le16 *)ie |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY); 311 312 sz += 2; 313 ie += 2; 314 315 /* SSID */ 316 ie = rtw_set_ie(ie, WLAN_EID_SSID, pdev_network->ssid.ssid_length, pdev_network->ssid.ssid, &sz); 317 318 /* supported rates */ 319 wireless_mode = pregistrypriv->wireless_mode; 320 321 rtw_set_supported_rate(pdev_network->supported_rates, wireless_mode); 322 323 rateLen = rtw_get_rateset_len(pdev_network->supported_rates); 324 325 if (rateLen > 8) { 326 ie = rtw_set_ie(ie, WLAN_EID_SUPP_RATES, 8, pdev_network->supported_rates, &sz); 327 /* ie = rtw_set_ie(ie, WLAN_EID_EXT_SUPP_RATES, (rateLen - 8), (pdev_network->supported_rates + 8), &sz); */ 328 } else { 329 ie = rtw_set_ie(ie, WLAN_EID_SUPP_RATES, rateLen, pdev_network->supported_rates, &sz); 330 } 331 332 /* DS parameter set */ 333 ie = rtw_set_ie(ie, WLAN_EID_DS_PARAMS, 1, (u8 *)&(pdev_network->configuration.ds_config), &sz); 334 335 /* IBSS Parameter Set */ 336 337 ie = rtw_set_ie(ie, WLAN_EID_IBSS_PARAMS, 2, (u8 *)&(pdev_network->configuration.atim_window), &sz); 338 339 if (rateLen > 8) 340 ie = rtw_set_ie(ie, WLAN_EID_EXT_SUPP_RATES, (rateLen - 8), (pdev_network->supported_rates + 8), &sz); 341 342 /* HT Cap. */ 343 if ((pregistrypriv->wireless_mode & WIRELESS_11_24N) && 344 (pregistrypriv->ht_enable == true)) { 345 /* todo: */ 346 } 347 348 /* pdev_network->ie_length = sz; update ie_length */ 349 350 /* return _SUCCESS; */ 351 352 return sz; 353 } 354 rtw_get_wpa_ie(unsigned char * pie,int * wpa_ie_len,int limit)355 unsigned char *rtw_get_wpa_ie(unsigned char *pie, int *wpa_ie_len, int limit) 356 { 357 int len; 358 u16 val16; 359 unsigned char wpa_oui_type[] = {0x00, 0x50, 0xf2, 0x01}; 360 u8 *pbuf = pie; 361 int limit_new = limit; 362 __le16 le_tmp; 363 364 while (1) { 365 pbuf = rtw_get_ie(pbuf, WLAN_EID_VENDOR_SPECIFIC, &len, limit_new); 366 367 if (pbuf) { 368 /* check if oui matches... */ 369 if (memcmp((pbuf + 2), wpa_oui_type, sizeof(wpa_oui_type))) 370 goto check_next_ie; 371 372 /* check version... */ 373 memcpy((u8 *)&le_tmp, (pbuf + 6), sizeof(val16)); 374 375 val16 = le16_to_cpu(le_tmp); 376 if (val16 != 0x0001) 377 goto check_next_ie; 378 379 *wpa_ie_len = *(pbuf + 1); 380 381 return pbuf; 382 383 } else { 384 *wpa_ie_len = 0; 385 return NULL; 386 } 387 388 check_next_ie: 389 390 limit_new = limit - (pbuf - pie) - 2 - len; 391 392 if (limit_new <= 0) 393 break; 394 395 pbuf += (2 + len); 396 } 397 398 *wpa_ie_len = 0; 399 400 return NULL; 401 } 402 rtw_get_wpa2_ie(unsigned char * pie,int * rsn_ie_len,int limit)403 unsigned char *rtw_get_wpa2_ie(unsigned char *pie, int *rsn_ie_len, int limit) 404 { 405 return rtw_get_ie(pie, WLAN_EID_RSN, rsn_ie_len, limit); 406 } 407 rtw_get_wpa_cipher_suite(u8 * s)408 int rtw_get_wpa_cipher_suite(u8 *s) 409 { 410 if (!memcmp(s, WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN)) 411 return WPA_CIPHER_NONE; 412 if (!memcmp(s, WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN)) 413 return WPA_CIPHER_WEP40; 414 if (!memcmp(s, WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN)) 415 return WPA_CIPHER_TKIP; 416 if (!memcmp(s, WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN)) 417 return WPA_CIPHER_CCMP; 418 if (!memcmp(s, WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN)) 419 return WPA_CIPHER_WEP104; 420 421 return 0; 422 } 423 rtw_get_wpa2_cipher_suite(u8 * s)424 int rtw_get_wpa2_cipher_suite(u8 *s) 425 { 426 if (!memcmp(s, RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN)) 427 return WPA_CIPHER_NONE; 428 if (!memcmp(s, RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN)) 429 return WPA_CIPHER_WEP40; 430 if (!memcmp(s, RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN)) 431 return WPA_CIPHER_TKIP; 432 if (!memcmp(s, RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN)) 433 return WPA_CIPHER_CCMP; 434 if (!memcmp(s, RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN)) 435 return WPA_CIPHER_WEP104; 436 437 return 0; 438 } 439 rtw_parse_wpa_ie(u8 * wpa_ie,int wpa_ie_len,int * group_cipher,int * pairwise_cipher,int * is_8021x)440 int rtw_parse_wpa_ie(u8 *wpa_ie, int wpa_ie_len, int *group_cipher, int *pairwise_cipher, int *is_8021x) 441 { 442 int i, ret = _SUCCESS; 443 int left, count; 444 u8 *pos; 445 u8 SUITE_1X[4] = {0x00, 0x50, 0xf2, 1}; 446 447 if (wpa_ie_len <= 0) { 448 /* No WPA IE - fail silently */ 449 return _FAIL; 450 } 451 452 if ((*wpa_ie != WLAN_EID_VENDOR_SPECIFIC) || (*(wpa_ie+1) != (u8)(wpa_ie_len - 2)) || 453 (memcmp(wpa_ie+2, RTW_WPA_OUI_TYPE, WPA_SELECTOR_LEN))) { 454 return _FAIL; 455 } 456 457 pos = wpa_ie; 458 459 pos += 8; 460 left = wpa_ie_len - 8; 461 462 /* group_cipher */ 463 if (left >= WPA_SELECTOR_LEN) { 464 *group_cipher = rtw_get_wpa_cipher_suite(pos); 465 466 pos += WPA_SELECTOR_LEN; 467 left -= WPA_SELECTOR_LEN; 468 469 } else if (left > 0) 470 return _FAIL; 471 472 /* pairwise_cipher */ 473 if (left >= 2) { 474 /* count = le16_to_cpu(*(u16*)pos); */ 475 count = get_unaligned_le16(pos); 476 pos += 2; 477 left -= 2; 478 479 if (count == 0 || left < count * WPA_SELECTOR_LEN) 480 return _FAIL; 481 482 for (i = 0; i < count; i++) { 483 *pairwise_cipher |= rtw_get_wpa_cipher_suite(pos); 484 485 pos += WPA_SELECTOR_LEN; 486 left -= WPA_SELECTOR_LEN; 487 } 488 489 } else if (left == 1) 490 return _FAIL; 491 492 if (is_8021x) { 493 if (left >= 6) { 494 pos += 2; 495 if (!memcmp(pos, SUITE_1X, 4)) 496 *is_8021x = 1; 497 } 498 } 499 500 return ret; 501 } 502 rtw_parse_wpa2_ie(u8 * rsn_ie,int rsn_ie_len,int * group_cipher,int * pairwise_cipher,int * is_8021x)503 int rtw_parse_wpa2_ie(u8 *rsn_ie, int rsn_ie_len, int *group_cipher, int *pairwise_cipher, int *is_8021x) 504 { 505 int i, ret = _SUCCESS; 506 int left, count; 507 u8 *pos; 508 u8 SUITE_1X[4] = {0x00, 0x0f, 0xac, 0x01}; 509 510 if (rsn_ie_len <= 0) { 511 /* No RSN IE - fail silently */ 512 return _FAIL; 513 } 514 515 if ((*rsn_ie != WLAN_EID_RSN) || (*(rsn_ie+1) != (u8)(rsn_ie_len - 2))) 516 return _FAIL; 517 518 pos = rsn_ie; 519 pos += 4; 520 left = rsn_ie_len - 4; 521 522 /* group_cipher */ 523 if (left >= RSN_SELECTOR_LEN) { 524 *group_cipher = rtw_get_wpa2_cipher_suite(pos); 525 526 pos += RSN_SELECTOR_LEN; 527 left -= RSN_SELECTOR_LEN; 528 529 } else if (left > 0) 530 return _FAIL; 531 532 /* pairwise_cipher */ 533 if (left >= 2) { 534 /* count = le16_to_cpu(*(u16*)pos); */ 535 count = get_unaligned_le16(pos); 536 pos += 2; 537 left -= 2; 538 539 if (count == 0 || left < count * RSN_SELECTOR_LEN) 540 return _FAIL; 541 542 for (i = 0; i < count; i++) { 543 *pairwise_cipher |= rtw_get_wpa2_cipher_suite(pos); 544 545 pos += RSN_SELECTOR_LEN; 546 left -= RSN_SELECTOR_LEN; 547 } 548 549 } else if (left == 1) 550 return _FAIL; 551 552 if (is_8021x) { 553 if (left >= 6) { 554 pos += 2; 555 if (!memcmp(pos, SUITE_1X, 4)) 556 *is_8021x = 1; 557 } 558 } 559 560 return ret; 561 } 562 563 /* ifdef CONFIG_WAPI_SUPPORT */ rtw_get_wapi_ie(u8 * in_ie,uint in_len,u8 * wapi_ie,u16 * wapi_len)564 int rtw_get_wapi_ie(u8 *in_ie, uint in_len, u8 *wapi_ie, u16 *wapi_len) 565 { 566 int len = 0; 567 u8 authmode; 568 uint cnt; 569 u8 wapi_oui1[4] = {0x0, 0x14, 0x72, 0x01}; 570 u8 wapi_oui2[4] = {0x0, 0x14, 0x72, 0x02}; 571 572 if (wapi_len) 573 *wapi_len = 0; 574 575 if (!in_ie || in_len <= 0) 576 return len; 577 578 cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_); 579 580 while (cnt < in_len) { 581 authmode = in_ie[cnt]; 582 583 /* if (authmode == WLAN_EID_BSS_AC_ACCESS_DELAY) */ 584 if (authmode == WLAN_EID_BSS_AC_ACCESS_DELAY && (!memcmp(&in_ie[cnt+6], wapi_oui1, 4) || 585 !memcmp(&in_ie[cnt+6], wapi_oui2, 4))) { 586 if (wapi_ie) 587 memcpy(wapi_ie, &in_ie[cnt], in_ie[cnt+1]+2); 588 589 if (wapi_len) 590 *wapi_len = in_ie[cnt+1]+2; 591 592 cnt += in_ie[cnt+1]+2; /* get next */ 593 } else { 594 cnt += in_ie[cnt+1]+2; /* get next */ 595 } 596 } 597 598 if (wapi_len) 599 len = *wapi_len; 600 601 return len; 602 } 603 /* endif */ 604 rtw_get_sec_ie(u8 * in_ie,uint in_len,u8 * rsn_ie,u16 * rsn_len,u8 * wpa_ie,u16 * wpa_len)605 void rtw_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len, u8 *wpa_ie, u16 *wpa_len) 606 { 607 u8 authmode; 608 u8 wpa_oui[4] = {0x0, 0x50, 0xf2, 0x01}; 609 uint cnt; 610 611 /* Search required WPA or WPA2 IE and copy to sec_ie[ ] */ 612 613 cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_); 614 615 while (cnt < in_len) { 616 authmode = in_ie[cnt]; 617 618 if ((authmode == WLAN_EID_VENDOR_SPECIFIC) && (!memcmp(&in_ie[cnt+2], &wpa_oui[0], 4))) { 619 if (wpa_ie) 620 memcpy(wpa_ie, &in_ie[cnt], in_ie[cnt+1]+2); 621 622 *wpa_len = in_ie[cnt + 1] + 2; 623 cnt += in_ie[cnt + 1] + 2; /* get next */ 624 } else { 625 if (authmode == WLAN_EID_RSN) { 626 if (rsn_ie) 627 memcpy(rsn_ie, &in_ie[cnt], in_ie[cnt + 1] + 2); 628 629 *rsn_len = in_ie[cnt+1]+2; 630 cnt += in_ie[cnt+1]+2; /* get next */ 631 } else { 632 cnt += in_ie[cnt+1]+2; /* get next */ 633 } 634 } 635 } 636 } 637 638 /** 639 * rtw_get_wps_ie - Search WPS IE from a series of IEs 640 * @in_ie: Address of IEs to search 641 * @in_len: Length limit from in_ie 642 * @wps_ie: If not NULL and WPS IE is found, WPS IE will be copied to the buf starting from wps_ie 643 * @wps_ielen: If not NULL and WPS IE is found, will set to the length of the entire WPS IE 644 * 645 * Returns: The address of the WPS IE found, or NULL 646 */ rtw_get_wps_ie(u8 * in_ie,uint in_len,u8 * wps_ie,uint * wps_ielen)647 u8 *rtw_get_wps_ie(u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen) 648 { 649 uint cnt; 650 u8 *wpsie_ptr = NULL; 651 u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04}; 652 653 if (wps_ielen) 654 *wps_ielen = 0; 655 656 if (!in_ie || in_len <= 0) 657 return wpsie_ptr; 658 659 cnt = 0; 660 661 while (cnt < in_len) { 662 eid = in_ie[cnt]; 663 664 if ((eid == WLAN_EID_VENDOR_SPECIFIC) && (!memcmp(&in_ie[cnt+2], wps_oui, 4))) { 665 wpsie_ptr = &in_ie[cnt]; 666 667 if (wps_ie) 668 memcpy(wps_ie, &in_ie[cnt], in_ie[cnt+1]+2); 669 670 if (wps_ielen) 671 *wps_ielen = in_ie[cnt+1]+2; 672 673 cnt += in_ie[cnt+1]+2; 674 675 break; 676 } 677 cnt += in_ie[cnt+1]+2; /* goto next */ 678 } 679 680 return wpsie_ptr; 681 } 682 683 /** 684 * rtw_get_wps_attr - Search a specific WPS attribute from a given WPS IE 685 * @wps_ie: Address of WPS IE to search 686 * @wps_ielen: Length limit from wps_ie 687 * @target_attr_id: The attribute ID of WPS attribute to search 688 * @buf_attr: If not NULL and the WPS attribute is found, WPS attribute will be copied to the buf starting from buf_attr 689 * @len_attr: If not NULL and the WPS attribute is found, will set to the length of the entire WPS attribute 690 * 691 * Returns: the address of the specific WPS attribute found, or NULL 692 */ rtw_get_wps_attr(u8 * wps_ie,uint wps_ielen,u16 target_attr_id,u8 * buf_attr,u32 * len_attr)693 u8 *rtw_get_wps_attr(u8 *wps_ie, uint wps_ielen, u16 target_attr_id, u8 *buf_attr, u32 *len_attr) 694 { 695 u8 *attr_ptr = NULL; 696 u8 *target_attr_ptr = NULL; 697 u8 wps_oui[4] = {0x00, 0x50, 0xF2, 0x04}; 698 699 if (len_attr) 700 *len_attr = 0; 701 702 if ((wps_ie[0] != WLAN_EID_VENDOR_SPECIFIC) || 703 (memcmp(wps_ie + 2, wps_oui, 4))) { 704 return attr_ptr; 705 } 706 707 /* 6 = 1(Element ID) + 1(Length) + 4(WPS OUI) */ 708 attr_ptr = wps_ie + 6; /* goto first attr */ 709 710 while (attr_ptr - wps_ie < wps_ielen) { 711 /* 4 = 2(Attribute ID) + 2(Length) */ 712 u16 attr_id = get_unaligned_be16(attr_ptr); 713 u16 attr_data_len = get_unaligned_be16(attr_ptr + 2); 714 u16 attr_len = attr_data_len + 4; 715 716 if (attr_id == target_attr_id) { 717 target_attr_ptr = attr_ptr; 718 719 if (buf_attr) 720 memcpy(buf_attr, attr_ptr, attr_len); 721 722 if (len_attr) 723 *len_attr = attr_len; 724 725 break; 726 } 727 attr_ptr += attr_len; /* goto next */ 728 } 729 730 return target_attr_ptr; 731 } 732 733 /** 734 * rtw_get_wps_attr_content - Search a specific WPS attribute content from a given WPS IE 735 * @wps_ie: Address of WPS IE to search 736 * @wps_ielen: Length limit from wps_ie 737 * @target_attr_id: The attribute ID of WPS attribute to search 738 * @buf_content: If not NULL and the WPS attribute is found, WPS attribute content will be copied to the buf starting from buf_content 739 * @len_content: If not NULL and the WPS attribute is found, will set to the length of the WPS attribute content 740 * 741 * Returns: the address of the specific WPS attribute content found, or NULL 742 */ rtw_get_wps_attr_content(u8 * wps_ie,uint wps_ielen,u16 target_attr_id,u8 * buf_content,uint * len_content)743 u8 *rtw_get_wps_attr_content(u8 *wps_ie, uint wps_ielen, u16 target_attr_id, u8 *buf_content, uint *len_content) 744 { 745 u8 *attr_ptr; 746 u32 attr_len; 747 748 if (len_content) 749 *len_content = 0; 750 751 attr_ptr = rtw_get_wps_attr(wps_ie, wps_ielen, target_attr_id, NULL, &attr_len); 752 753 if (attr_ptr && attr_len) { 754 if (buf_content) 755 memcpy(buf_content, attr_ptr+4, attr_len-4); 756 757 if (len_content) 758 *len_content = attr_len-4; 759 760 return attr_ptr+4; 761 } 762 763 return NULL; 764 } 765 rtw_ieee802_11_parse_vendor_specific(u8 * pos,uint elen,struct rtw_ieee802_11_elems * elems,int show_errors)766 static int rtw_ieee802_11_parse_vendor_specific(u8 *pos, uint elen, 767 struct rtw_ieee802_11_elems *elems, 768 int show_errors) 769 { 770 unsigned int oui; 771 772 /* first 3 bytes in vendor specific information element are the IEEE 773 * OUI of the vendor. The following byte is used a vendor specific 774 * sub-type. */ 775 if (elen < 4) 776 return -1; 777 778 oui = get_unaligned_be24(pos); 779 switch (oui) { 780 case OUI_MICROSOFT: 781 /* Microsoft/Wi-Fi information elements are further typed and 782 * subtyped */ 783 switch (pos[3]) { 784 case 1: 785 /* Microsoft OUI (00:50:F2) with OUI Type 1: 786 * real WPA information element */ 787 elems->wpa_ie = pos; 788 elems->wpa_ie_len = elen; 789 break; 790 case WME_OUI_TYPE: /* this is a Wi-Fi WME info. element */ 791 if (elen < 5) 792 return -1; 793 794 switch (pos[4]) { 795 case WME_OUI_SUBTYPE_INFORMATION_ELEMENT: 796 case WME_OUI_SUBTYPE_PARAMETER_ELEMENT: 797 elems->wme = pos; 798 elems->wme_len = elen; 799 break; 800 case WME_OUI_SUBTYPE_TSPEC_ELEMENT: 801 elems->wme_tspec = pos; 802 elems->wme_tspec_len = elen; 803 break; 804 default: 805 return -1; 806 } 807 break; 808 case 4: 809 /* Wi-Fi Protected Setup (WPS) IE */ 810 elems->wps_ie = pos; 811 elems->wps_ie_len = elen; 812 break; 813 default: 814 return -1; 815 } 816 break; 817 818 case OUI_BROADCOM: 819 switch (pos[3]) { 820 case VENDOR_HT_CAPAB_OUI_TYPE: 821 elems->vendor_ht_cap = pos; 822 elems->vendor_ht_cap_len = elen; 823 break; 824 default: 825 return -1; 826 } 827 break; 828 829 default: 830 return -1; 831 } 832 833 return 0; 834 } 835 836 /** 837 * rtw_ieee802_11_parse_elems - Parse information elements in management frames 838 * @start: Pointer to the start of IEs 839 * @len: Length of IE buffer in octets 840 * @elems: Data structure for parsed elements 841 * @show_errors: Whether to show parsing errors in debug log 842 * Returns: Parsing result 843 */ rtw_ieee802_11_parse_elems(u8 * start,uint len,struct rtw_ieee802_11_elems * elems,int show_errors)844 enum ParseRes rtw_ieee802_11_parse_elems(u8 *start, uint len, 845 struct rtw_ieee802_11_elems *elems, 846 int show_errors) 847 { 848 uint left = len; 849 u8 *pos = start; 850 int unknown = 0; 851 852 memset(elems, 0, sizeof(*elems)); 853 854 while (left >= 2) { 855 u8 id, elen; 856 857 id = *pos++; 858 elen = *pos++; 859 left -= 2; 860 861 if (elen > left) 862 return ParseFailed; 863 864 switch (id) { 865 case WLAN_EID_SSID: 866 elems->ssid = pos; 867 elems->ssid_len = elen; 868 break; 869 case WLAN_EID_SUPP_RATES: 870 elems->supp_rates = pos; 871 elems->supp_rates_len = elen; 872 break; 873 case WLAN_EID_FH_PARAMS: 874 elems->fh_params = pos; 875 elems->fh_params_len = elen; 876 break; 877 case WLAN_EID_DS_PARAMS: 878 elems->ds_params = pos; 879 elems->ds_params_len = elen; 880 break; 881 case WLAN_EID_CF_PARAMS: 882 elems->cf_params = pos; 883 elems->cf_params_len = elen; 884 break; 885 case WLAN_EID_TIM: 886 elems->tim = pos; 887 elems->tim_len = elen; 888 break; 889 case WLAN_EID_IBSS_PARAMS: 890 elems->ibss_params = pos; 891 elems->ibss_params_len = elen; 892 break; 893 case WLAN_EID_CHALLENGE: 894 elems->challenge = pos; 895 elems->challenge_len = elen; 896 break; 897 case WLAN_EID_ERP_INFO: 898 elems->erp_info = pos; 899 elems->erp_info_len = elen; 900 break; 901 case WLAN_EID_EXT_SUPP_RATES: 902 elems->ext_supp_rates = pos; 903 elems->ext_supp_rates_len = elen; 904 break; 905 case WLAN_EID_VENDOR_SPECIFIC: 906 if (rtw_ieee802_11_parse_vendor_specific(pos, elen, 907 elems, 908 show_errors)) 909 unknown++; 910 break; 911 case WLAN_EID_RSN: 912 elems->rsn_ie = pos; 913 elems->rsn_ie_len = elen; 914 break; 915 case WLAN_EID_PWR_CAPABILITY: 916 elems->power_cap = pos; 917 elems->power_cap_len = elen; 918 break; 919 case WLAN_EID_SUPPORTED_CHANNELS: 920 elems->supp_channels = pos; 921 elems->supp_channels_len = elen; 922 break; 923 case WLAN_EID_MOBILITY_DOMAIN: 924 elems->mdie = pos; 925 elems->mdie_len = elen; 926 break; 927 case WLAN_EID_FAST_BSS_TRANSITION: 928 elems->ftie = pos; 929 elems->ftie_len = elen; 930 break; 931 case WLAN_EID_TIMEOUT_INTERVAL: 932 elems->timeout_int = pos; 933 elems->timeout_int_len = elen; 934 break; 935 case WLAN_EID_HT_CAPABILITY: 936 elems->ht_capabilities = pos; 937 elems->ht_capabilities_len = elen; 938 break; 939 case WLAN_EID_HT_OPERATION: 940 elems->ht_operation = pos; 941 elems->ht_operation_len = elen; 942 break; 943 case WLAN_EID_VHT_CAPABILITY: 944 elems->vht_capabilities = pos; 945 elems->vht_capabilities_len = elen; 946 break; 947 case WLAN_EID_VHT_OPERATION: 948 elems->vht_operation = pos; 949 elems->vht_operation_len = elen; 950 break; 951 case WLAN_EID_OPMODE_NOTIF: 952 elems->vht_op_mode_notify = pos; 953 elems->vht_op_mode_notify_len = elen; 954 break; 955 default: 956 unknown++; 957 break; 958 } 959 960 left -= elen; 961 pos += elen; 962 } 963 964 if (left) 965 return ParseFailed; 966 967 return unknown ? ParseUnknown : ParseOK; 968 } 969 rtw_macaddr_cfg(struct device * dev,u8 * mac_addr)970 void rtw_macaddr_cfg(struct device *dev, u8 *mac_addr) 971 { 972 u8 mac[ETH_ALEN]; 973 struct device_node *np = dev->of_node; 974 const unsigned char *addr; 975 int len; 976 977 if (!mac_addr) 978 return; 979 980 if (rtw_initmac && mac_pton(rtw_initmac, mac)) { 981 /* Users specify the mac address */ 982 ether_addr_copy(mac_addr, mac); 983 } else { 984 /* Use the mac address stored in the Efuse */ 985 ether_addr_copy(mac, mac_addr); 986 } 987 988 if (is_broadcast_ether_addr(mac) || is_zero_ether_addr(mac)) { 989 addr = of_get_property(np, "local-mac-address", &len); 990 991 if (addr && len == ETH_ALEN) { 992 ether_addr_copy(mac_addr, addr); 993 } else { 994 eth_random_addr(mac_addr); 995 } 996 } 997 } 998 rtw_get_cipher_info(struct wlan_network * pnetwork)999 static int rtw_get_cipher_info(struct wlan_network *pnetwork) 1000 { 1001 u32 wpa_ielen; 1002 unsigned char *pbuf; 1003 int group_cipher = 0, pairwise_cipher = 0, is8021x = 0; 1004 int ret = _FAIL; 1005 1006 pbuf = rtw_get_wpa_ie(&pnetwork->network.ies[12], &wpa_ielen, pnetwork->network.ie_length-12); 1007 1008 if (pbuf && (wpa_ielen > 0)) { 1009 if (_SUCCESS == rtw_parse_wpa_ie(pbuf, wpa_ielen+2, &group_cipher, &pairwise_cipher, &is8021x)) { 1010 pnetwork->bcn_info.pairwise_cipher = pairwise_cipher; 1011 pnetwork->bcn_info.group_cipher = group_cipher; 1012 pnetwork->bcn_info.is_8021x = is8021x; 1013 ret = _SUCCESS; 1014 } 1015 } else { 1016 pbuf = rtw_get_wpa2_ie(&pnetwork->network.ies[12], &wpa_ielen, pnetwork->network.ie_length-12); 1017 1018 if (pbuf && (wpa_ielen > 0)) { 1019 if (_SUCCESS == rtw_parse_wpa2_ie(pbuf, wpa_ielen+2, &group_cipher, &pairwise_cipher, &is8021x)) { 1020 pnetwork->bcn_info.pairwise_cipher = pairwise_cipher; 1021 pnetwork->bcn_info.group_cipher = group_cipher; 1022 pnetwork->bcn_info.is_8021x = is8021x; 1023 ret = _SUCCESS; 1024 } 1025 } 1026 } 1027 1028 return ret; 1029 } 1030 rtw_get_bcn_info(struct wlan_network * pnetwork)1031 void rtw_get_bcn_info(struct wlan_network *pnetwork) 1032 { 1033 unsigned short cap = 0; 1034 u8 bencrypt = 0; 1035 /* u8 wpa_ie[255], rsn_ie[255]; */ 1036 u16 wpa_len = 0, rsn_len = 0; 1037 struct HT_info_element *pht_info = NULL; 1038 struct ieee80211_ht_cap *pht_cap = NULL; 1039 unsigned int len; 1040 unsigned char *p; 1041 __le16 le_cap; 1042 1043 memcpy((u8 *)&le_cap, rtw_get_capability_from_ie(pnetwork->network.ies), 2); 1044 cap = le16_to_cpu(le_cap); 1045 if (cap & WLAN_CAPABILITY_PRIVACY) { 1046 bencrypt = 1; 1047 pnetwork->network.privacy = 1; 1048 } else { 1049 pnetwork->bcn_info.encryp_protocol = ENCRYP_PROTOCOL_OPENSYS; 1050 } 1051 rtw_get_sec_ie(pnetwork->network.ies, pnetwork->network.ie_length, NULL, &rsn_len, NULL, &wpa_len); 1052 1053 if (rsn_len > 0) { 1054 pnetwork->bcn_info.encryp_protocol = ENCRYP_PROTOCOL_WPA2; 1055 } else if (wpa_len > 0) { 1056 pnetwork->bcn_info.encryp_protocol = ENCRYP_PROTOCOL_WPA; 1057 } else { 1058 if (bencrypt) 1059 pnetwork->bcn_info.encryp_protocol = ENCRYP_PROTOCOL_WEP; 1060 } 1061 rtw_get_cipher_info(pnetwork); 1062 1063 /* get bwmode and ch_offset */ 1064 /* parsing HT_CAP_IE */ 1065 p = rtw_get_ie(pnetwork->network.ies + _FIXED_IE_LENGTH_, WLAN_EID_HT_CAPABILITY, &len, pnetwork->network.ie_length - _FIXED_IE_LENGTH_); 1066 if (p && len > 0) { 1067 pht_cap = (struct ieee80211_ht_cap *)(p + 2); 1068 pnetwork->bcn_info.ht_cap_info = le16_to_cpu(pht_cap->cap_info); 1069 } else { 1070 pnetwork->bcn_info.ht_cap_info = 0; 1071 } 1072 /* parsing HT_INFO_IE */ 1073 p = rtw_get_ie(pnetwork->network.ies + _FIXED_IE_LENGTH_, WLAN_EID_HT_OPERATION, &len, pnetwork->network.ie_length - _FIXED_IE_LENGTH_); 1074 if (p && len > 0) { 1075 pht_info = (struct HT_info_element *)(p + 2); 1076 pnetwork->bcn_info.ht_info_infos_0 = pht_info->infos[0]; 1077 } else { 1078 pnetwork->bcn_info.ht_info_infos_0 = 0; 1079 } 1080 } 1081 1082 /* show MCS rate, unit: 100Kbps */ rtw_mcs_rate(u8 bw_40MHz,u8 short_GI,unsigned char * MCS_rate)1083 u16 rtw_mcs_rate(u8 bw_40MHz, u8 short_GI, unsigned char *MCS_rate) 1084 { 1085 u16 max_rate = 0; 1086 1087 if (MCS_rate[0] & BIT(7)) 1088 max_rate = (bw_40MHz) ? ((short_GI)?1500:1350):((short_GI)?722:650); 1089 else if (MCS_rate[0] & BIT(6)) 1090 max_rate = (bw_40MHz) ? ((short_GI)?1350:1215):((short_GI)?650:585); 1091 else if (MCS_rate[0] & BIT(5)) 1092 max_rate = (bw_40MHz) ? ((short_GI)?1200:1080):((short_GI)?578:520); 1093 else if (MCS_rate[0] & BIT(4)) 1094 max_rate = (bw_40MHz) ? ((short_GI)?900:810):((short_GI)?433:390); 1095 else if (MCS_rate[0] & BIT(3)) 1096 max_rate = (bw_40MHz) ? ((short_GI)?600:540):((short_GI)?289:260); 1097 else if (MCS_rate[0] & BIT(2)) 1098 max_rate = (bw_40MHz) ? ((short_GI)?450:405):((short_GI)?217:195); 1099 else if (MCS_rate[0] & BIT(1)) 1100 max_rate = (bw_40MHz) ? ((short_GI)?300:270):((short_GI)?144:130); 1101 else if (MCS_rate[0] & BIT(0)) 1102 max_rate = (bw_40MHz) ? ((short_GI)?150:135):((short_GI)?72:65); 1103 1104 return max_rate; 1105 } 1106 rtw_action_frame_parse(const u8 * frame,u32 frame_len,u8 * category,u8 * action)1107 int rtw_action_frame_parse(const u8 *frame, u32 frame_len, u8 *category, u8 *action) 1108 { 1109 const u8 *frame_body = frame + sizeof(struct ieee80211_hdr_3addr); 1110 u16 fc; 1111 u8 c; 1112 u8 a = ACT_PUBLIC_MAX; 1113 1114 fc = le16_to_cpu(((struct ieee80211_hdr_3addr *)frame)->frame_control); 1115 1116 if ((fc & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE)) 1117 != (IEEE80211_FTYPE_MGMT|IEEE80211_STYPE_ACTION) 1118 ) { 1119 return false; 1120 } 1121 1122 c = frame_body[0]; 1123 1124 switch (c) { 1125 case RTW_WLAN_CATEGORY_P2P: /* vendor-specific */ 1126 break; 1127 default: 1128 a = frame_body[1]; 1129 } 1130 1131 if (category) 1132 *category = c; 1133 if (action) 1134 *action = a; 1135 1136 return true; 1137 } 1138 1139 static const char *_action_public_str[] = { 1140 "ACT_PUB_BSSCOEXIST", 1141 "ACT_PUB_DSE_ENABLE", 1142 "ACT_PUB_DSE_DEENABLE", 1143 "ACT_PUB_DSE_REG_LOCATION", 1144 "ACT_PUB_EXT_CHL_SWITCH", 1145 "ACT_PUB_DSE_MSR_REQ", 1146 "ACT_PUB_DSE_MSR_RPRT", 1147 "ACT_PUB_MP", 1148 "ACT_PUB_DSE_PWR_CONSTRAINT", 1149 "ACT_PUB_VENDOR", 1150 "ACT_PUB_GAS_INITIAL_REQ", 1151 "ACT_PUB_GAS_INITIAL_RSP", 1152 "ACT_PUB_GAS_COMEBACK_REQ", 1153 "ACT_PUB_GAS_COMEBACK_RSP", 1154 "ACT_PUB_TDLS_DISCOVERY_RSP", 1155 "ACT_PUB_LOCATION_TRACK", 1156 "ACT_PUB_RSVD", 1157 }; 1158 action_public_str(u8 action)1159 const char *action_public_str(u8 action) 1160 { 1161 action = (action >= ACT_PUBLIC_MAX) ? ACT_PUBLIC_MAX : action; 1162 return _action_public_str[action]; 1163 } 1164