1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
4 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
6 ******************************************************************************/
10 #include <rtw_debug.h>
12 #include <asm/unaligned.h>
14 u8 RTW_WPA_OUI_TYPE[] = { 0x00, 0x50, 0xf2, 1 };
15 u16 RTW_WPA_VERSION = 1;
16 u8 WPA_AUTH_KEY_MGMT_NONE[] = { 0x00, 0x50, 0xf2, 0 };
17 u8 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x50, 0xf2, 1 };
18 u8 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x50, 0xf2, 2 };
19 u8 WPA_CIPHER_SUITE_NONE[] = { 0x00, 0x50, 0xf2, 0 };
20 u8 WPA_CIPHER_SUITE_WEP40[] = { 0x00, 0x50, 0xf2, 1 };
21 u8 WPA_CIPHER_SUITE_TKIP[] = { 0x00, 0x50, 0xf2, 2 };
22 u8 WPA_CIPHER_SUITE_WRAP[] = { 0x00, 0x50, 0xf2, 3 };
23 u8 WPA_CIPHER_SUITE_CCMP[] = { 0x00, 0x50, 0xf2, 4 };
24 u8 WPA_CIPHER_SUITE_WEP104[] = { 0x00, 0x50, 0xf2, 5 };
26 u16 RSN_VERSION_BSD = 1;
27 u8 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x0f, 0xac, 1 };
28 u8 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x0f, 0xac, 2 };
29 u8 RSN_CIPHER_SUITE_NONE[] = { 0x00, 0x0f, 0xac, 0 };
30 u8 RSN_CIPHER_SUITE_WEP40[] = { 0x00, 0x0f, 0xac, 1 };
31 u8 RSN_CIPHER_SUITE_TKIP[] = { 0x00, 0x0f, 0xac, 2 };
32 u8 RSN_CIPHER_SUITE_WRAP[] = { 0x00, 0x0f, 0xac, 3 };
33 u8 RSN_CIPHER_SUITE_CCMP[] = { 0x00, 0x0f, 0xac, 4 };
34 u8 RSN_CIPHER_SUITE_WEP104[] = { 0x00, 0x0f, 0xac, 5 };
36 /* for adhoc-master to generate ie and provide supported-rate to fw */
39 static u8 WIFI_CCKRATES[] = {
40 (IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK),
41 (IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK),
42 (IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK),
43 (IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK)
46 static u8 WIFI_OFDMRATES[] = {
47 (IEEE80211_OFDM_RATE_6MB),
48 (IEEE80211_OFDM_RATE_9MB),
49 (IEEE80211_OFDM_RATE_12MB),
50 (IEEE80211_OFDM_RATE_18MB),
51 (IEEE80211_OFDM_RATE_24MB),
52 IEEE80211_OFDM_RATE_36MB,
53 IEEE80211_OFDM_RATE_48MB,
54 IEEE80211_OFDM_RATE_54MB
57 int rtw_get_bit_value_from_ieee_value(u8 val)
59 unsigned char dot11_rate_table[] = {2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108, 0}; /* last element must be zero!! */
62 while (dot11_rate_table[i] != 0) {
63 if (dot11_rate_table[i] == val)
70 bool rtw_is_cckrates_included(u8 *rate)
75 if (r == 2 || r == 4 || r == 11 || r == 22)
83 bool rtw_is_cckratesonly_included(u8 *rate)
88 if (r != 2 && r != 4 && r != 11 && r != 22)
96 int rtw_check_network_type(unsigned char *rate, int ratelen, int channel)
99 if (rtw_is_cckrates_included(rate))
100 return WIRELESS_INVALID;
103 } else { /* could be pure B, pure G, or B/G */
104 if (rtw_is_cckratesonly_included(rate))
106 else if (rtw_is_cckrates_included(rate))
107 return WIRELESS_11BG;
113 u8 *rtw_set_fixed_ie(unsigned char *pbuf, unsigned int len, unsigned char *source,
116 memcpy((void *)pbuf, (void *)source, len);
117 *frlen = *frlen + len;
121 /* rtw_set_ie will update frame length */
128 uint *frlen /* frame length */
133 *(pbuf + 1) = (u8)len;
136 memcpy((void *)(pbuf + 2), (void *)source, len);
138 *frlen = *frlen + (len + 2);
140 return pbuf + len + 2;
143 /*----------------------------------------------------------------------------
144 index: the information element id index, limit is the limit for search
145 -----------------------------------------------------------------------------*/
146 u8 *rtw_get_ie(u8 *pbuf, sint index, sint *len, sint limit)
173 * rtw_get_ie_ex - Search specific IE from a series of IEs
174 * @in_ie: Address of IEs to search
175 * @in_len: Length limit from in_ie
176 * @eid: Element ID to match
178 * @oui_len: OUI length
179 * @ie: If not NULL and the specific IE is found, the IE will be copied to the buf starting from the specific IE
180 * @ielen: If not NULL and the specific IE is found, will set to the length of the entire IE
182 * Returns: The address of the specific IE found, or NULL
184 u8 *rtw_get_ie_ex(u8 *in_ie, uint in_len, u8 eid, u8 *oui, u8 oui_len, u8 *ie, uint *ielen)
187 u8 *target_ie = NULL;
192 if (!in_ie || in_len <= 0)
197 while (cnt < in_len) {
198 if (eid == in_ie[cnt]
199 && (!oui || !memcmp(&in_ie[cnt+2], oui, oui_len))) {
200 target_ie = &in_ie[cnt];
203 memcpy(ie, &in_ie[cnt], in_ie[cnt+1]+2);
206 *ielen = in_ie[cnt+1]+2;
210 cnt += in_ie[cnt+1]+2; /* goto next */
218 * rtw_ies_remove_ie - Find matching IEs and remove
219 * @ies: Address of IEs to search
220 * @ies_len: Pointer of length of ies, will update to new length
221 * @offset: The offset to start search
222 * @eid: Element ID to match
224 * @oui_len: OUI length
226 * Returns: _SUCCESS: ies is updated, _FAIL: not updated
228 int rtw_ies_remove_ie(u8 *ies, uint *ies_len, uint offset, u8 eid, u8 *oui, u8 oui_len)
236 if (!ies || !ies_len || *ies_len <= offset)
239 start = ies + offset;
240 search_len = *ies_len - offset;
243 target_ie = rtw_get_ie_ex(start, search_len, eid, oui, oui_len, NULL, &target_ielen);
244 if (target_ie && target_ielen) {
245 u8 buf[MAX_IE_SZ] = {0};
246 u8 *remain_ies = target_ie + target_ielen;
247 uint remain_len = search_len - (remain_ies - start);
249 memcpy(buf, remain_ies, remain_len);
250 memcpy(target_ie, buf, remain_len);
251 *ies_len = *ies_len - target_ielen;
255 search_len = remain_len;
264 void rtw_set_supported_rate(u8 *SupportedRates, uint mode)
266 memset(SupportedRates, 0, NDIS_802_11_LENGTH_RATES_EX);
270 memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
276 case WIRELESS_11A_5N:/* Todo: no basic rate for ofdm ? */
277 case WIRELESS_11_5AC:
278 memcpy(SupportedRates, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN);
282 case WIRELESS_11G_24N:
283 case WIRELESS_11_24N:
284 case WIRELESS_11BG_24N:
285 memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
286 memcpy(SupportedRates + IEEE80211_CCK_RATE_LEN, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN);
291 uint rtw_get_rateset_len(u8 *rateset)
295 for (i = 0; i < 13; i++)
301 int rtw_generate_ie(struct registry_priv *pregistrypriv)
305 struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
306 u8 *ie = pdev_network->IEs;
308 /* timestamp will be inserted by hardware */
312 /* beacon interval : 2bytes */
313 *(__le16 *)ie = cpu_to_le16((u16)pdev_network->Configuration.BeaconPeriod);/* BCN_INTERVAL; */
317 /* capability info */
320 *(__le16 *)ie |= cpu_to_le16(cap_IBSS);
322 if (pregistrypriv->preamble == PREAMBLE_SHORT)
323 *(__le16 *)ie |= cpu_to_le16(cap_ShortPremble);
325 if (pdev_network->Privacy)
326 *(__le16 *)ie |= cpu_to_le16(cap_Privacy);
332 ie = rtw_set_ie(ie, _SSID_IE_, pdev_network->Ssid.SsidLength, pdev_network->Ssid.Ssid, &sz);
334 /* supported rates */
335 if (pregistrypriv->wireless_mode == WIRELESS_11ABGN) {
336 if (pdev_network->Configuration.DSConfig > 14)
337 wireless_mode = WIRELESS_11A_5N;
339 wireless_mode = WIRELESS_11BG_24N;
341 wireless_mode = pregistrypriv->wireless_mode;
344 rtw_set_supported_rate(pdev_network->SupportedRates, wireless_mode);
346 rateLen = rtw_get_rateset_len(pdev_network->SupportedRates);
349 ie = rtw_set_ie(ie, _SUPPORTEDRATES_IE_, 8, pdev_network->SupportedRates, &sz);
350 /* ie = rtw_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8), (pdev_network->SupportedRates + 8), &sz); */
352 ie = rtw_set_ie(ie, _SUPPORTEDRATES_IE_, rateLen, pdev_network->SupportedRates, &sz);
355 /* DS parameter set */
356 ie = rtw_set_ie(ie, _DSSET_IE_, 1, (u8 *)&(pdev_network->Configuration.DSConfig), &sz);
358 /* IBSS Parameter Set */
360 ie = rtw_set_ie(ie, _IBSS_PARA_IE_, 2, (u8 *)&(pdev_network->Configuration.ATIMWindow), &sz);
363 ie = rtw_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8), (pdev_network->SupportedRates + 8), &sz);
367 if (((pregistrypriv->wireless_mode&WIRELESS_11_5N) || (pregistrypriv->wireless_mode&WIRELESS_11_24N))
368 && (pregistrypriv->ht_enable == true)) {
372 /* pdev_network->IELength = sz; update IELength */
374 /* return _SUCCESS; */
379 unsigned char *rtw_get_wpa_ie(unsigned char *pie, int *wpa_ie_len, int limit)
383 unsigned char wpa_oui_type[] = {0x00, 0x50, 0xf2, 0x01};
385 int limit_new = limit;
389 pbuf = rtw_get_ie(pbuf, _WPA_IE_ID_, &len, limit_new);
392 /* check if oui matches... */
393 if (memcmp((pbuf + 2), wpa_oui_type, sizeof(wpa_oui_type))) {
397 /* check version... */
398 memcpy((u8 *)&le_tmp, (pbuf + 6), sizeof(val16));
400 val16 = le16_to_cpu(le_tmp);
404 *wpa_ie_len = *(pbuf + 1);
415 limit_new = limit - (pbuf - pie) - 2 - len;
428 unsigned char *rtw_get_wpa2_ie(unsigned char *pie, int *rsn_ie_len, int limit)
430 return rtw_get_ie(pie, _WPA2_IE_ID_, rsn_ie_len, limit);
433 int rtw_get_wpa_cipher_suite(u8 *s)
435 if (!memcmp(s, WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN))
436 return WPA_CIPHER_NONE;
437 if (!memcmp(s, WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN))
438 return WPA_CIPHER_WEP40;
439 if (!memcmp(s, WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN))
440 return WPA_CIPHER_TKIP;
441 if (!memcmp(s, WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN))
442 return WPA_CIPHER_CCMP;
443 if (!memcmp(s, WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN))
444 return WPA_CIPHER_WEP104;
449 int rtw_get_wpa2_cipher_suite(u8 *s)
451 if (!memcmp(s, RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN))
452 return WPA_CIPHER_NONE;
453 if (!memcmp(s, RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN))
454 return WPA_CIPHER_WEP40;
455 if (!memcmp(s, RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN))
456 return WPA_CIPHER_TKIP;
457 if (!memcmp(s, RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN))
458 return WPA_CIPHER_CCMP;
459 if (!memcmp(s, RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN))
460 return WPA_CIPHER_WEP104;
465 int rtw_parse_wpa_ie(u8 *wpa_ie, int wpa_ie_len, int *group_cipher, int *pairwise_cipher, int *is_8021x)
467 int i, ret = _SUCCESS;
470 u8 SUITE_1X[4] = {0x00, 0x50, 0xf2, 1};
472 if (wpa_ie_len <= 0) {
473 /* No WPA IE - fail silently */
477 if ((*wpa_ie != _WPA_IE_ID_) || (*(wpa_ie+1) != (u8)(wpa_ie_len - 2)) ||
478 (memcmp(wpa_ie+2, RTW_WPA_OUI_TYPE, WPA_SELECTOR_LEN))) {
485 left = wpa_ie_len - 8;
488 if (left >= WPA_SELECTOR_LEN) {
489 *group_cipher = rtw_get_wpa_cipher_suite(pos);
491 pos += WPA_SELECTOR_LEN;
492 left -= WPA_SELECTOR_LEN;
494 } else if (left > 0) {
495 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie length mismatch, %u too much", __func__, left));
500 /* pairwise_cipher */
502 /* count = le16_to_cpu(*(u16*)pos); */
503 count = get_unaligned_le16(pos);
507 if (count == 0 || left < count * WPA_SELECTOR_LEN) {
508 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie count botch (pairwise), "
509 "count %u left %u", __func__, count, left));
513 for (i = 0; i < count; i++) {
514 *pairwise_cipher |= rtw_get_wpa_cipher_suite(pos);
516 pos += WPA_SELECTOR_LEN;
517 left -= WPA_SELECTOR_LEN;
520 } else if (left == 1) {
521 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie too short (for key mgmt)", __func__));
528 if (!memcmp(pos, SUITE_1X, 4)) {
529 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("%s : there has 802.1x auth\n", __func__));
538 int rtw_parse_wpa2_ie(u8 *rsn_ie, int rsn_ie_len, int *group_cipher, int *pairwise_cipher, int *is_8021x)
540 int i, ret = _SUCCESS;
543 u8 SUITE_1X[4] = {0x00, 0x0f, 0xac, 0x01};
545 if (rsn_ie_len <= 0) {
546 /* No RSN IE - fail silently */
550 if ((*rsn_ie != _WPA2_IE_ID_) || (*(rsn_ie+1) != (u8)(rsn_ie_len - 2))) {
556 left = rsn_ie_len - 4;
559 if (left >= RSN_SELECTOR_LEN) {
560 *group_cipher = rtw_get_wpa2_cipher_suite(pos);
562 pos += RSN_SELECTOR_LEN;
563 left -= RSN_SELECTOR_LEN;
565 } else if (left > 0) {
566 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie length mismatch, %u too much", __func__, left));
570 /* pairwise_cipher */
572 /* count = le16_to_cpu(*(u16*)pos); */
573 count = get_unaligned_le16(pos);
577 if (count == 0 || left < count * RSN_SELECTOR_LEN) {
578 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie count botch (pairwise), "
579 "count %u left %u", __func__, count, left));
583 for (i = 0; i < count; i++) {
584 *pairwise_cipher |= rtw_get_wpa2_cipher_suite(pos);
586 pos += RSN_SELECTOR_LEN;
587 left -= RSN_SELECTOR_LEN;
590 } else if (left == 1) {
591 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie too short (for key mgmt)", __func__));
599 if (!memcmp(pos, SUITE_1X, 4)) {
600 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("%s (): there has 802.1x auth\n", __func__));
609 /* ifdef CONFIG_WAPI_SUPPORT */
610 int rtw_get_wapi_ie(u8 *in_ie, uint in_len, u8 *wapi_ie, u16 *wapi_len)
615 u8 wapi_oui1[4] = {0x0, 0x14, 0x72, 0x01};
616 u8 wapi_oui2[4] = {0x0, 0x14, 0x72, 0x02};
621 if (!in_ie || in_len <= 0)
624 cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_);
626 while (cnt < in_len) {
627 authmode = in_ie[cnt];
629 /* if (authmode == _WAPI_IE_) */
630 if (authmode == _WAPI_IE_ && (!memcmp(&in_ie[cnt+6], wapi_oui1, 4) ||
631 !memcmp(&in_ie[cnt+6], wapi_oui2, 4))) {
633 memcpy(wapi_ie, &in_ie[cnt], in_ie[cnt+1]+2);
635 for (i = 0; i < (in_ie[cnt+1]+2); i = i+8) {
636 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("\n %2x,%2x,%2x,%2x,%2x,%2x,%2x,%2x\n",
637 wapi_ie[i], wapi_ie[i+1], wapi_ie[i+2], wapi_ie[i+3], wapi_ie[i+4],
638 wapi_ie[i+5], wapi_ie[i+6], wapi_ie[i+7]));
643 *wapi_len = in_ie[cnt+1]+2;
645 cnt += in_ie[cnt+1]+2; /* get next */
647 cnt += in_ie[cnt+1]+2; /* get next */
658 void rtw_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len, u8 *wpa_ie, u16 *wpa_len)
660 u8 authmode, sec_idx, i;
661 u8 wpa_oui[4] = {0x0, 0x50, 0xf2, 0x01};
664 /* Search required WPA or WPA2 IE and copy to sec_ie[ ] */
666 cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_);
670 while (cnt < in_len) {
671 authmode = in_ie[cnt];
673 if ((authmode == _WPA_IE_ID_) && (!memcmp(&in_ie[cnt+2], &wpa_oui[0], 4))) {
674 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("\n rtw_get_wpa_ie: sec_idx =%d in_ie[cnt+1]+2 =%d\n", sec_idx, in_ie[cnt+1]+2));
677 memcpy(wpa_ie, &in_ie[cnt], in_ie[cnt+1]+2);
679 for (i = 0; i < (in_ie[cnt+1]+2); i = i+8) {
680 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("\n %2x,%2x,%2x,%2x,%2x,%2x,%2x,%2x\n",
681 wpa_ie[i], wpa_ie[i+1], wpa_ie[i+2], wpa_ie[i+3], wpa_ie[i+4],
682 wpa_ie[i+5], wpa_ie[i+6], wpa_ie[i+7]));
686 *wpa_len = in_ie[cnt+1]+2;
687 cnt += in_ie[cnt+1]+2; /* get next */
689 if (authmode == _WPA2_IE_ID_) {
690 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("\n get_rsn_ie: sec_idx =%d in_ie[cnt+1]+2 =%d\n", sec_idx, in_ie[cnt+1]+2));
693 memcpy(rsn_ie, &in_ie[cnt], in_ie[cnt+1]+2);
695 for (i = 0; i < (in_ie[cnt+1]+2); i = i+8) {
696 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("\n %2x,%2x,%2x,%2x,%2x,%2x,%2x,%2x\n",
697 rsn_ie[i], rsn_ie[i+1], rsn_ie[i+2], rsn_ie[i+3], rsn_ie[i+4],
698 rsn_ie[i+5], rsn_ie[i+6], rsn_ie[i+7]));
702 *rsn_len = in_ie[cnt+1]+2;
703 cnt += in_ie[cnt+1]+2; /* get next */
705 cnt += in_ie[cnt+1]+2; /* get next */
711 u8 rtw_is_wps_ie(u8 *ie_ptr, uint *wps_ielen)
714 u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
721 if ((eid == _WPA_IE_ID_) && (!memcmp(&ie_ptr[2], wps_oui, 4))) {
722 /* DBG_8192C("==> found WPS_IE.....\n"); */
723 *wps_ielen = ie_ptr[1]+2;
730 * rtw_get_wps_ie - Search WPS IE from a series of IEs
731 * @in_ie: Address of IEs to search
732 * @in_len: Length limit from in_ie
733 * @wps_ie: If not NULL and WPS IE is found, WPS IE will be copied to the buf starting from wps_ie
734 * @wps_ielen: If not NULL and WPS IE is found, will set to the length of the entire WPS IE
736 * Returns: The address of the WPS IE found, or NULL
738 u8 *rtw_get_wps_ie(u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen)
741 u8 *wpsie_ptr = NULL;
742 u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
747 if (!in_ie || in_len <= 0)
752 while (cnt < in_len) {
755 if ((eid == _WPA_IE_ID_) && (!memcmp(&in_ie[cnt+2], wps_oui, 4))) {
756 wpsie_ptr = &in_ie[cnt];
759 memcpy(wps_ie, &in_ie[cnt], in_ie[cnt+1]+2);
762 *wps_ielen = in_ie[cnt+1]+2;
764 cnt += in_ie[cnt+1]+2;
768 cnt += in_ie[cnt+1]+2; /* goto next */
776 * rtw_get_wps_attr - Search a specific WPS attribute from a given WPS IE
777 * @wps_ie: Address of WPS IE to search
778 * @wps_ielen: Length limit from wps_ie
779 * @target_attr_id: The attribute ID of WPS attribute to search
780 * @buf_attr: If not NULL and the WPS attribute is found, WPS attribute will be copied to the buf starting from buf_attr
781 * @len_attr: If not NULL and the WPS attribute is found, will set to the length of the entire WPS attribute
783 * Returns: the address of the specific WPS attribute found, or NULL
785 u8 *rtw_get_wps_attr(u8 *wps_ie, uint wps_ielen, u16 target_attr_id, u8 *buf_attr, u32 *len_attr)
788 u8 *target_attr_ptr = NULL;
789 u8 wps_oui[4] = {0x00, 0x50, 0xF2, 0x04};
794 if ((wps_ie[0] != _VENDOR_SPECIFIC_IE_) ||
795 (memcmp(wps_ie + 2, wps_oui, 4))) {
799 /* 6 = 1(Element ID) + 1(Length) + 4(WPS OUI) */
800 attr_ptr = wps_ie + 6; /* goto first attr */
802 while (attr_ptr - wps_ie < wps_ielen) {
803 /* 4 = 2(Attribute ID) + 2(Length) */
804 u16 attr_id = get_unaligned_be16(attr_ptr);
805 u16 attr_data_len = get_unaligned_be16(attr_ptr + 2);
806 u16 attr_len = attr_data_len + 4;
808 /* DBG_871X("%s attr_ptr:%p, id:%u, length:%u\n", __func__, attr_ptr, attr_id, attr_data_len); */
809 if (attr_id == target_attr_id) {
810 target_attr_ptr = attr_ptr;
813 memcpy(buf_attr, attr_ptr, attr_len);
816 *len_attr = attr_len;
820 attr_ptr += attr_len; /* goto next */
824 return target_attr_ptr;
828 * rtw_get_wps_attr_content - Search a specific WPS attribute content from a given WPS IE
829 * @wps_ie: Address of WPS IE to search
830 * @wps_ielen: Length limit from wps_ie
831 * @target_attr_id: The attribute ID of WPS attribute to search
832 * @buf_content: If not NULL and the WPS attribute is found, WPS attribute content will be copied to the buf starting from buf_content
833 * @len_content: If not NULL and the WPS attribute is found, will set to the length of the WPS attribute content
835 * Returns: the address of the specific WPS attribute content found, or NULL
837 u8 *rtw_get_wps_attr_content(u8 *wps_ie, uint wps_ielen, u16 target_attr_id, u8 *buf_content, uint *len_content)
845 attr_ptr = rtw_get_wps_attr(wps_ie, wps_ielen, target_attr_id, NULL, &attr_len);
847 if (attr_ptr && attr_len) {
849 memcpy(buf_content, attr_ptr+4, attr_len-4);
852 *len_content = attr_len-4;
860 static int rtw_ieee802_11_parse_vendor_specific(u8 *pos, uint elen,
861 struct rtw_ieee802_11_elems *elems,
866 /* first 3 bytes in vendor specific information element are the IEEE
867 * OUI of the vendor. The following byte is used a vendor specific
871 DBG_871X("short vendor specific "
872 "information element ignored (len =%lu)\n",
873 (unsigned long) elen);
878 oui = get_unaligned_be24(pos);
881 /* Microsoft/Wi-Fi information elements are further typed and
885 /* Microsoft OUI (00:50:F2) with OUI Type 1:
886 * real WPA information element */
888 elems->wpa_ie_len = elen;
890 case WME_OUI_TYPE: /* this is a Wi-Fi WME info. element */
892 DBG_871X("short WME "
893 "information element ignored "
895 (unsigned long) elen);
899 case WME_OUI_SUBTYPE_INFORMATION_ELEMENT:
900 case WME_OUI_SUBTYPE_PARAMETER_ELEMENT:
902 elems->wme_len = elen;
904 case WME_OUI_SUBTYPE_TSPEC_ELEMENT:
905 elems->wme_tspec = pos;
906 elems->wme_tspec_len = elen;
909 DBG_871X("unknown WME "
910 "information element ignored "
911 "(subtype =%d len =%lu)\n",
912 pos[4], (unsigned long) elen);
917 /* Wi-Fi Protected Setup (WPS) IE */
919 elems->wps_ie_len = elen;
922 DBG_871X("Unknown Microsoft "
923 "information element ignored "
924 "(type =%d len =%lu)\n",
925 pos[3], (unsigned long) elen);
932 case VENDOR_HT_CAPAB_OUI_TYPE:
933 elems->vendor_ht_cap = pos;
934 elems->vendor_ht_cap_len = elen;
937 DBG_871X("Unknown Broadcom "
938 "information element ignored "
939 "(type =%d len =%lu)\n",
940 pos[3], (unsigned long) elen);
946 DBG_871X("unknown vendor specific information "
947 "element ignored (vendor OUI %02x:%02x:%02x "
949 pos[0], pos[1], pos[2], (unsigned long) elen);
957 * ieee802_11_parse_elems - Parse information elements in management frames
958 * @start: Pointer to the start of IEs
959 * @len: Length of IE buffer in octets
960 * @elems: Data structure for parsed elements
961 * @show_errors: Whether to show parsing errors in debug log
962 * Returns: Parsing result
964 ParseRes rtw_ieee802_11_parse_elems(u8 *start, uint len,
965 struct rtw_ieee802_11_elems *elems,
972 memset(elems, 0, sizeof(*elems));
983 DBG_871X("IEEE 802.11 element "
984 "parse failed (id =%d elen =%d "
986 id, elen, (unsigned long) left);
994 elems->ssid_len = elen;
996 case WLAN_EID_SUPP_RATES:
997 elems->supp_rates = pos;
998 elems->supp_rates_len = elen;
1000 case WLAN_EID_FH_PARAMS:
1001 elems->fh_params = pos;
1002 elems->fh_params_len = elen;
1004 case WLAN_EID_DS_PARAMS:
1005 elems->ds_params = pos;
1006 elems->ds_params_len = elen;
1008 case WLAN_EID_CF_PARAMS:
1009 elems->cf_params = pos;
1010 elems->cf_params_len = elen;
1014 elems->tim_len = elen;
1016 case WLAN_EID_IBSS_PARAMS:
1017 elems->ibss_params = pos;
1018 elems->ibss_params_len = elen;
1020 case WLAN_EID_CHALLENGE:
1021 elems->challenge = pos;
1022 elems->challenge_len = elen;
1024 case WLAN_EID_ERP_INFO:
1025 elems->erp_info = pos;
1026 elems->erp_info_len = elen;
1028 case WLAN_EID_EXT_SUPP_RATES:
1029 elems->ext_supp_rates = pos;
1030 elems->ext_supp_rates_len = elen;
1032 case WLAN_EID_VENDOR_SPECIFIC:
1033 if (rtw_ieee802_11_parse_vendor_specific(pos, elen,
1039 elems->rsn_ie = pos;
1040 elems->rsn_ie_len = elen;
1042 case WLAN_EID_PWR_CAPABILITY:
1043 elems->power_cap = pos;
1044 elems->power_cap_len = elen;
1046 case WLAN_EID_SUPPORTED_CHANNELS:
1047 elems->supp_channels = pos;
1048 elems->supp_channels_len = elen;
1050 case WLAN_EID_MOBILITY_DOMAIN:
1052 elems->mdie_len = elen;
1054 case WLAN_EID_FAST_BSS_TRANSITION:
1056 elems->ftie_len = elen;
1058 case WLAN_EID_TIMEOUT_INTERVAL:
1059 elems->timeout_int = pos;
1060 elems->timeout_int_len = elen;
1062 case WLAN_EID_HT_CAP:
1063 elems->ht_capabilities = pos;
1064 elems->ht_capabilities_len = elen;
1066 case WLAN_EID_HT_OPERATION:
1067 elems->ht_operation = pos;
1068 elems->ht_operation_len = elen;
1070 case WLAN_EID_VHT_CAPABILITY:
1071 elems->vht_capabilities = pos;
1072 elems->vht_capabilities_len = elen;
1074 case WLAN_EID_VHT_OPERATION:
1075 elems->vht_operation = pos;
1076 elems->vht_operation_len = elen;
1078 case WLAN_EID_VHT_OP_MODE_NOTIFY:
1079 elems->vht_op_mode_notify = pos;
1080 elems->vht_op_mode_notify_len = elen;
1086 DBG_871X("IEEE 802.11 element parse "
1087 "ignored unknown element (id =%d elen =%d)\n",
1099 return unknown ? ParseUnknown : ParseOK;
1102 void rtw_macaddr_cfg(struct device *dev, u8 *mac_addr)
1105 struct device_node *np = dev->of_node;
1106 const unsigned char *addr;
1112 if (rtw_initmac && mac_pton(rtw_initmac, mac)) {
1113 /* Users specify the mac address */
1114 ether_addr_copy(mac_addr, mac);
1116 /* Use the mac address stored in the Efuse */
1117 ether_addr_copy(mac, mac_addr);
1120 if (is_broadcast_ether_addr(mac) || is_zero_ether_addr(mac)) {
1121 if ((addr = of_get_property(np, "local-mac-address", &len)) &&
1123 ether_addr_copy(mac_addr, addr);
1125 eth_random_addr(mac_addr);
1126 DBG_871X("MAC Address from efuse error, assign random one !!!\n");
1130 DBG_871X("rtw_macaddr_cfg MAC Address = "MAC_FMT"\n", MAC_ARG(mac_addr));
1133 static int rtw_get_cipher_info(struct wlan_network *pnetwork)
1136 unsigned char *pbuf;
1137 int group_cipher = 0, pairwise_cipher = 0, is8021x = 0;
1140 pbuf = rtw_get_wpa_ie(&pnetwork->network.IEs[12], &wpa_ielen, pnetwork->network.IELength-12);
1142 if (pbuf && (wpa_ielen > 0)) {
1143 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_cipher_info: wpa_ielen: %d", wpa_ielen));
1144 if (_SUCCESS == rtw_parse_wpa_ie(pbuf, wpa_ielen+2, &group_cipher, &pairwise_cipher, &is8021x)) {
1145 pnetwork->BcnInfo.pairwise_cipher = pairwise_cipher;
1146 pnetwork->BcnInfo.group_cipher = group_cipher;
1147 pnetwork->BcnInfo.is_8021x = is8021x;
1148 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("%s: pnetwork->pairwise_cipher: %d, is_8021x is %d",
1149 __func__, pnetwork->BcnInfo.pairwise_cipher, pnetwork->BcnInfo.is_8021x));
1153 pbuf = rtw_get_wpa2_ie(&pnetwork->network.IEs[12], &wpa_ielen, pnetwork->network.IELength-12);
1155 if (pbuf && (wpa_ielen > 0)) {
1156 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("get RSN IE\n"));
1157 if (_SUCCESS == rtw_parse_wpa2_ie(pbuf, wpa_ielen+2, &group_cipher, &pairwise_cipher, &is8021x)) {
1158 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("get RSN IE OK!!!\n"));
1159 pnetwork->BcnInfo.pairwise_cipher = pairwise_cipher;
1160 pnetwork->BcnInfo.group_cipher = group_cipher;
1161 pnetwork->BcnInfo.is_8021x = is8021x;
1162 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("%s: pnetwork->pairwise_cipher: %d,"
1163 "pnetwork->group_cipher is %d, is_8021x is %d", __func__, pnetwork->BcnInfo.pairwise_cipher,
1164 pnetwork->BcnInfo.group_cipher, pnetwork->BcnInfo.is_8021x));
1173 void rtw_get_bcn_info(struct wlan_network *pnetwork)
1175 unsigned short cap = 0;
1177 /* u8 wpa_ie[255], rsn_ie[255]; */
1178 u16 wpa_len = 0, rsn_len = 0;
1179 struct HT_info_element *pht_info = NULL;
1180 struct ieee80211_ht_cap *pht_cap = NULL;
1185 memcpy((u8 *)&le_cap, rtw_get_capability_from_ie(pnetwork->network.IEs), 2);
1186 cap = le16_to_cpu(le_cap);
1187 if (cap & WLAN_CAPABILITY_PRIVACY) {
1189 pnetwork->network.Privacy = 1;
1191 pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_OPENSYS;
1193 rtw_get_sec_ie(pnetwork->network.IEs, pnetwork->network.IELength, NULL, &rsn_len, NULL, &wpa_len);
1194 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: ssid =%s\n", pnetwork->network.Ssid.Ssid));
1195 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: wpa_len =%d rsn_len =%d\n", wpa_len, rsn_len));
1196 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: ssid =%s\n", pnetwork->network.Ssid.Ssid));
1197 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: wpa_len =%d rsn_len =%d\n", wpa_len, rsn_len));
1200 pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WPA2;
1201 } else if (wpa_len > 0) {
1202 pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WPA;
1205 pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WEP;
1207 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: pnetwork->encryp_protocol is %x\n",
1208 pnetwork->BcnInfo.encryp_protocol));
1209 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: pnetwork->encryp_protocol is %x\n",
1210 pnetwork->BcnInfo.encryp_protocol));
1211 rtw_get_cipher_info(pnetwork);
1213 /* get bwmode and ch_offset */
1214 /* parsing HT_CAP_IE */
1215 p = rtw_get_ie(pnetwork->network.IEs + _FIXED_IE_LENGTH_, _HT_CAPABILITY_IE_, &len, pnetwork->network.IELength - _FIXED_IE_LENGTH_);
1217 pht_cap = (struct ieee80211_ht_cap *)(p + 2);
1218 pnetwork->BcnInfo.ht_cap_info = le16_to_cpu(pht_cap->cap_info);
1220 pnetwork->BcnInfo.ht_cap_info = 0;
1222 /* parsing HT_INFO_IE */
1223 p = rtw_get_ie(pnetwork->network.IEs + _FIXED_IE_LENGTH_, _HT_ADD_INFO_IE_, &len, pnetwork->network.IELength - _FIXED_IE_LENGTH_);
1225 pht_info = (struct HT_info_element *)(p + 2);
1226 pnetwork->BcnInfo.ht_info_infos_0 = pht_info->infos[0];
1228 pnetwork->BcnInfo.ht_info_infos_0 = 0;
1232 /* show MCS rate, unit: 100Kbps */
1233 u16 rtw_mcs_rate(u8 rf_type, u8 bw_40MHz, u8 short_GI, unsigned char *MCS_rate)
1237 if (rf_type == RF_1T1R) {
1238 if (MCS_rate[0] & BIT(7))
1239 max_rate = (bw_40MHz) ? ((short_GI)?1500:1350):((short_GI)?722:650);
1240 else if (MCS_rate[0] & BIT(6))
1241 max_rate = (bw_40MHz) ? ((short_GI)?1350:1215):((short_GI)?650:585);
1242 else if (MCS_rate[0] & BIT(5))
1243 max_rate = (bw_40MHz) ? ((short_GI)?1200:1080):((short_GI)?578:520);
1244 else if (MCS_rate[0] & BIT(4))
1245 max_rate = (bw_40MHz) ? ((short_GI)?900:810):((short_GI)?433:390);
1246 else if (MCS_rate[0] & BIT(3))
1247 max_rate = (bw_40MHz) ? ((short_GI)?600:540):((short_GI)?289:260);
1248 else if (MCS_rate[0] & BIT(2))
1249 max_rate = (bw_40MHz) ? ((short_GI)?450:405):((short_GI)?217:195);
1250 else if (MCS_rate[0] & BIT(1))
1251 max_rate = (bw_40MHz) ? ((short_GI)?300:270):((short_GI)?144:130);
1252 else if (MCS_rate[0] & BIT(0))
1253 max_rate = (bw_40MHz) ? ((short_GI)?150:135):((short_GI)?72:65);
1256 if (MCS_rate[1] & BIT(7))
1257 max_rate = (bw_40MHz) ? ((short_GI)?3000:2700):((short_GI)?1444:1300);
1258 else if (MCS_rate[1] & BIT(6))
1259 max_rate = (bw_40MHz) ? ((short_GI)?2700:2430):((short_GI)?1300:1170);
1260 else if (MCS_rate[1] & BIT(5))
1261 max_rate = (bw_40MHz) ? ((short_GI)?2400:2160):((short_GI)?1156:1040);
1262 else if (MCS_rate[1] & BIT(4))
1263 max_rate = (bw_40MHz) ? ((short_GI)?1800:1620):((short_GI)?867:780);
1264 else if (MCS_rate[1] & BIT(3))
1265 max_rate = (bw_40MHz) ? ((short_GI)?1200:1080):((short_GI)?578:520);
1266 else if (MCS_rate[1] & BIT(2))
1267 max_rate = (bw_40MHz) ? ((short_GI)?900:810):((short_GI)?433:390);
1268 else if (MCS_rate[1] & BIT(1))
1269 max_rate = (bw_40MHz) ? ((short_GI)?600:540):((short_GI)?289:260);
1270 else if (MCS_rate[1] & BIT(0))
1271 max_rate = (bw_40MHz) ? ((short_GI)?300:270):((short_GI)?144:130);
1273 if (MCS_rate[0] & BIT(7))
1274 max_rate = (bw_40MHz) ? ((short_GI)?1500:1350):((short_GI)?722:650);
1275 else if (MCS_rate[0] & BIT(6))
1276 max_rate = (bw_40MHz) ? ((short_GI)?1350:1215):((short_GI)?650:585);
1277 else if (MCS_rate[0] & BIT(5))
1278 max_rate = (bw_40MHz) ? ((short_GI)?1200:1080):((short_GI)?578:520);
1279 else if (MCS_rate[0] & BIT(4))
1280 max_rate = (bw_40MHz) ? ((short_GI)?900:810):((short_GI)?433:390);
1281 else if (MCS_rate[0] & BIT(3))
1282 max_rate = (bw_40MHz) ? ((short_GI)?600:540):((short_GI)?289:260);
1283 else if (MCS_rate[0] & BIT(2))
1284 max_rate = (bw_40MHz) ? ((short_GI)?450:405):((short_GI)?217:195);
1285 else if (MCS_rate[0] & BIT(1))
1286 max_rate = (bw_40MHz) ? ((short_GI)?300:270):((short_GI)?144:130);
1287 else if (MCS_rate[0] & BIT(0))
1288 max_rate = (bw_40MHz) ? ((short_GI)?150:135):((short_GI)?72:65);
1294 int rtw_action_frame_parse(const u8 *frame, u32 frame_len, u8 *category, u8 *action)
1296 const u8 *frame_body = frame + sizeof(struct ieee80211_hdr_3addr);
1299 u8 a = ACT_PUBLIC_MAX;
1301 fc = le16_to_cpu(((struct ieee80211_hdr_3addr *)frame)->frame_control);
1303 if ((fc & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE))
1304 != (IEEE80211_FTYPE_MGMT|IEEE80211_STYPE_ACTION)
1312 case RTW_WLAN_CATEGORY_P2P: /* vendor-specific */
1326 static const char *_action_public_str[] = {
1327 "ACT_PUB_BSSCOEXIST",
1328 "ACT_PUB_DSE_ENABLE",
1329 "ACT_PUB_DSE_DEENABLE",
1330 "ACT_PUB_DSE_REG_LOCATION",
1331 "ACT_PUB_EXT_CHL_SWITCH",
1332 "ACT_PUB_DSE_MSR_REQ",
1333 "ACT_PUB_DSE_MSR_RPRT",
1335 "ACT_PUB_DSE_PWR_CONSTRAINT",
1337 "ACT_PUB_GAS_INITIAL_REQ",
1338 "ACT_PUB_GAS_INITIAL_RSP",
1339 "ACT_PUB_GAS_COMEBACK_REQ",
1340 "ACT_PUB_GAS_COMEBACK_RSP",
1341 "ACT_PUB_TDLS_DISCOVERY_RSP",
1342 "ACT_PUB_LOCATION_TRACK",
1346 const char *action_public_str(u8 action)
1348 action = (action >= ACT_PUBLIC_MAX) ? ACT_PUBLIC_MAX : action;
1349 return _action_public_str[action];