Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / rtl8723au / core / rtw_ieee80211.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  ******************************************************************************/
15 #define _IEEE80211_C
16
17 #include <drv_types.h>
18 #include <linux/ieee80211.h>
19 #include <ieee80211.h>
20 #include <wifi.h>
21 #include <osdep_service.h>
22 #include <wlan_bssdef.h>
23
24 u8 RTW_WPA_OUI23A_TYPE[] = { 0x00, 0x50, 0xf2, 1 };
25 u16 RTW_WPA_VERSION23A = 1;
26 u8 WPA_AUTH_KEY_MGMT_NONE23A[] = { 0x00, 0x50, 0xf2, 0 };
27 u8 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X23A[] = { 0x00, 0x50, 0xf2, 1 };
28 u8 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X23A[] = { 0x00, 0x50, 0xf2, 2 };
29 u8 WPA_CIPHER_SUITE_NONE23A[] = { 0x00, 0x50, 0xf2, 0 };
30 u8 WPA_CIPHER_SUITE_WEP4023A[] = { 0x00, 0x50, 0xf2, 1 };
31 u8 WPA_CIPHER_SUITE_TKIP23A[] = { 0x00, 0x50, 0xf2, 2 };
32 u8 WPA_CIPHER_SUITE_WRAP23A[] = { 0x00, 0x50, 0xf2, 3 };
33 u8 WPA_CIPHER_SUITE_CCMP23A[] = { 0x00, 0x50, 0xf2, 4 };
34 u8 WPA_CIPHER_SUITE_WEP10423A[] = { 0x00, 0x50, 0xf2, 5 };
35
36 u8 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X23A[] = { 0x00, 0x0f, 0xac, 1 };
37 u8 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X23A[] = { 0x00, 0x0f, 0xac, 2 };
38 u8 RSN_CIPHER_SUITE_NONE23A[] = { 0x00, 0x0f, 0xac, 0 };
39 u8 RSN_CIPHER_SUITE_WEP4023A[] = { 0x00, 0x0f, 0xac, 1 };
40 u8 RSN_CIPHER_SUITE_TKIP23A[] = { 0x00, 0x0f, 0xac, 2 };
41 u8 RSN_CIPHER_SUITE_WRAP23A[] = { 0x00, 0x0f, 0xac, 3 };
42 u8 RSN_CIPHER_SUITE_CCMP23A[] = { 0x00, 0x0f, 0xac, 4 };
43 u8 RSN_CIPHER_SUITE_WEP10423A[] = { 0x00, 0x0f, 0xac, 5 };
44 /*  */
45 /*  for adhoc-master to generate ie and provide supported-rate to fw */
46 /*  */
47
48 static u8 WIFI_CCKRATES[] = {
49         IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK,
50         IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK,
51         IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK,
52         IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK
53 };
54
55 static u8 WIFI_OFDMRATES[] = {
56         IEEE80211_OFDM_RATE_6MB,
57         IEEE80211_OFDM_RATE_9MB,
58         IEEE80211_OFDM_RATE_12MB,
59         IEEE80211_OFDM_RATE_18MB,
60         IEEE80211_OFDM_RATE_24MB,
61         IEEE80211_OFDM_RATE_36MB,
62         IEEE80211_OFDM_RATE_48MB,
63         IEEE80211_OFDM_RATE_54MB
64 };
65
66 int rtw_get_bit_value_from_ieee_value23a(u8 val)
67 {
68         unsigned char dot11_rate_table[]=
69                 {2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108, 0};
70
71         int i = 0;
72
73         while (dot11_rate_table[i] != 0) {
74                 if (dot11_rate_table[i] == val)
75                         return BIT(i);
76                 i++;
77         }
78         return 0;
79 }
80
81 static bool rtw_is_cckrates_included(u8 *rate)
82 {
83         u32 i = 0;
84
85         while (rate[i]) {
86                 if ((rate[i] & 0x7f) == 2 || (rate[i] & 0x7f) == 4 ||
87                     (rate[i] & 0x7f) == 11 || (rate[i] & 0x7f) == 22)
88                         return true;
89                 i++;
90         }
91
92         return false;
93 }
94
95 static bool rtw_is_cckratesonly_included(u8 *rate)
96 {
97         u32 i = 0;
98
99         while (rate[i]) {
100                 if ((rate[i] & 0x7f) != 2 && (rate[i] & 0x7f) != 4 &&
101                     (rate[i] & 0x7f) != 11 && (rate[i] & 0x7f) != 22)
102                         return false;
103
104                 i++;
105         }
106
107         return true;
108 }
109
110 int rtw_check_network_type23a(unsigned char *rate, int ratelen, int channel)
111 {
112         if (channel > 14) {
113                 if (rtw_is_cckrates_included(rate))
114                         return WIRELESS_INVALID;
115                 else
116                         return WIRELESS_11A;
117         } else {  /*  could be pure B, pure G, or B/G */
118                 if (rtw_is_cckratesonly_included(rate))
119                         return WIRELESS_11B;
120                 else if (rtw_is_cckrates_included(rate))
121                         return  WIRELESS_11BG;
122                 else
123                         return WIRELESS_11G;
124         }
125 }
126
127 /*  rtw_set_ie23a will update frame length */
128 u8 *rtw_set_ie23a(u8 *pbuf, int index, uint len, const u8 *source, uint *frlen)
129 {
130
131         *pbuf = (u8)index;
132
133         *(pbuf + 1) = (u8)len;
134
135         if (len > 0)
136                 memcpy((void *)(pbuf + 2), (void *)source, len);
137
138         *frlen = *frlen + (len + 2);
139
140         return pbuf + len + 2;
141 }
142
143 inline u8 *rtw_set_ie23a_ch_switch (u8 *buf, u32 *buf_len, u8 ch_switch_mode,
144                                 u8 new_ch, u8 ch_switch_cnt)
145 {
146         u8 ie_data[3];
147
148         ie_data[0] = ch_switch_mode;
149         ie_data[1] = new_ch;
150         ie_data[2] = ch_switch_cnt;
151         return rtw_set_ie23a(buf, WLAN_EID_CHANNEL_SWITCH,  3, ie_data, buf_len);
152 }
153
154 inline u8 hal_ch_offset_to_secondary_ch_offset23a(u8 ch_offset)
155 {
156         if (ch_offset == HAL_PRIME_CHNL_OFFSET_LOWER)
157                 return IEEE80211_HT_PARAM_CHA_SEC_BELOW;
158         else if (ch_offset == HAL_PRIME_CHNL_OFFSET_UPPER)
159                 return IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
160
161         return IEEE80211_HT_PARAM_CHA_SEC_NONE;
162 }
163
164 inline u8 *rtw_set_ie23a_secondary_ch_offset(u8 *buf, u32 *buf_len,
165                                           u8 secondary_ch_offset)
166 {
167         return rtw_set_ie23a(buf, WLAN_EID_SECONDARY_CHANNEL_OFFSET,
168                           1, &secondary_ch_offset, buf_len);
169 }
170
171 /*----------------------------------------------------------------------------
172 index: the information element id index, limit is the limit for search
173 -----------------------------------------------------------------------------*/
174 u8 *rtw_get_ie23a(u8 *pbuf, int index, int *len, int limit)
175 {
176         int tmp, i;
177         u8 *p;
178
179         if (limit < 1) {
180
181                 return NULL;
182         }
183
184         p = pbuf;
185         i = 0;
186         *len = 0;
187         while (1) {
188                 if (*p == index) {
189                         *len = *(p + 1);
190                         return p;
191                 } else {
192                         tmp = *(p + 1);
193                         p += (tmp + 2);
194                         i += (tmp + 2);
195                 }
196                 if (i >= limit)
197                         break;
198         }
199
200         return NULL;
201 }
202
203 /**
204  * rtw_get_ie23a_ex - Search specific IE from a series of IEs
205  * @in_ie: Address of IEs to search
206  * @in_len: Length limit from in_ie
207  * @eid: Element ID to match
208  * @oui: OUI to match
209  * @oui_len: OUI length
210  * @ie: If not NULL and the specific IE is found, the IE will be copied
211  *      to the buf starting from the specific IE
212  * @ielen: If not NULL and the specific IE is found, will set to the length
213  *         of the entire IE
214  *
215  * Returns: The address of the specific IE found, or NULL
216  */
217 u8 *rtw_get_ie23a_ex(u8 *in_ie, uint in_len, u8 eid, u8 *oui, u8 oui_len,
218                   u8 *ie, uint *ielen)
219 {
220         uint cnt;
221         u8 *target_ie = NULL;
222
223         if (ielen)
224                 *ielen = 0;
225
226         if (!in_ie || in_len <= 0)
227                 return target_ie;
228
229         cnt = 0;
230
231         while (cnt < in_len) {
232                 if (eid == in_ie[cnt] &&
233                     (!oui || !memcmp(&in_ie[cnt+2], oui, oui_len))) {
234                         target_ie = &in_ie[cnt];
235
236                         if (ie)
237                                 memcpy(ie, &in_ie[cnt], in_ie[cnt+1]+2);
238
239                         if (ielen)
240                                 *ielen = in_ie[cnt+1]+2;
241                         break;
242                 } else {
243                         cnt += in_ie[cnt + 1] + 2; /* goto next */
244                 }
245         }
246
247         return target_ie;
248 }
249
250 /**
251  * rtw_ies_remove_ie23a - Find matching IEs and remove
252  * @ies: Address of IEs to search
253  * @ies_len: Pointer of length of ies, will update to new length
254  * @offset: The offset to start search
255  * @eid: Element ID to match
256  * @oui: OUI to match
257  * @oui_len: OUI length
258  *
259  * Returns: _SUCCESS: ies is updated, _FAIL: not updated
260  */
261 int rtw_ies_remove_ie23a(u8 *ies, uint *ies_len, uint offset, u8 eid,
262                       u8 *oui, u8 oui_len)
263 {
264         int ret = _FAIL;
265         u8 *target_ie;
266         u32 target_ielen;
267         u8 *start;
268         uint search_len;
269
270         if (!ies || !ies_len || *ies_len <= offset)
271                 goto exit;
272
273         start = ies + offset;
274         search_len = *ies_len - offset;
275
276         while (1) {
277                 target_ie = rtw_get_ie23a_ex(start, search_len, eid, oui, oui_len,
278                                           NULL, &target_ielen);
279                 if (target_ie && target_ielen) {
280                         u8 buf[MAX_IE_SZ] = {0};
281                         u8 *remain_ies = target_ie + target_ielen;
282                         uint remain_len = search_len - (remain_ies - start);
283
284                         memcpy(buf, remain_ies, remain_len);
285                         memcpy(target_ie, buf, remain_len);
286                         *ies_len = *ies_len - target_ielen;
287                         ret = _SUCCESS;
288
289                         start = target_ie;
290                         search_len = remain_len;
291                 } else {
292                         break;
293                 }
294         }
295 exit:
296         return ret;
297 }
298
299 void rtw_set_supported_rate23a(u8 *SupportedRates, uint mode)
300 {
301
302
303         memset(SupportedRates, 0, NDIS_802_11_LENGTH_RATES_EX);
304
305         switch (mode) {
306         case WIRELESS_11B:
307                 memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
308                 break;
309
310         case WIRELESS_11G:
311         case WIRELESS_11A:
312         case WIRELESS_11_5N:
313         case WIRELESS_11A_5N:/* Todo: no basic rate for ofdm ? */
314                 memcpy(SupportedRates, WIFI_OFDMRATES,
315                        IEEE80211_NUM_OFDM_RATESLEN);
316                 break;
317
318         case WIRELESS_11BG:
319         case WIRELESS_11G_24N:
320         case WIRELESS_11_24N:
321         case WIRELESS_11BG_24N:
322                 memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
323                 memcpy(SupportedRates + IEEE80211_CCK_RATE_LEN, WIFI_OFDMRATES,
324                        IEEE80211_NUM_OFDM_RATESLEN);
325                 break;
326         }
327
328 }
329
330 uint rtw_get_rateset_len23a(u8 *rateset)
331 {
332         uint i = 0;
333
334         while(1) {
335                 if (rateset[i] == 0)
336                         break;
337
338                 if (i > 12)
339                         break;
340
341                 i++;
342         }
343
344         return i;
345 }
346
347 int rtw_generate_ie23a(struct registry_priv *pregistrypriv)
348 {
349         u8      wireless_mode;
350         int     sz = 0, rateLen;
351         struct wlan_bssid_ex*   pdev_network = &pregistrypriv->dev_network;
352         u8*     ie = pdev_network->IEs;
353         u16 cap;
354
355         pdev_network->tsf = 0;
356
357         cap = WLAN_CAPABILITY_IBSS;
358
359         if (pregistrypriv->preamble == PREAMBLE_SHORT)
360                 cap |= WLAN_CAPABILITY_SHORT_PREAMBLE;
361
362         if (pdev_network->Privacy)
363                 cap |= WLAN_CAPABILITY_PRIVACY;
364
365         pdev_network->capability = cap;
366
367         /* SSID */
368         ie = rtw_set_ie23a(ie, WLAN_EID_SSID, pdev_network->Ssid.ssid_len,
369                         pdev_network->Ssid.ssid, &sz);
370
371         /* supported rates */
372         if (pregistrypriv->wireless_mode == WIRELESS_11ABGN) {
373                 if (pdev_network->DSConfig > 14)
374                         wireless_mode = WIRELESS_11A_5N;
375                 else
376                         wireless_mode = WIRELESS_11BG_24N;
377         } else {
378                 wireless_mode = pregistrypriv->wireless_mode;
379         }
380
381         rtw_set_supported_rate23a(pdev_network->SupportedRates, wireless_mode) ;
382
383         rateLen = rtw_get_rateset_len23a(pdev_network->SupportedRates);
384
385         if (rateLen > 8) {
386                 ie = rtw_set_ie23a(ie, WLAN_EID_SUPP_RATES, 8,
387                                 pdev_network->SupportedRates, &sz);
388                 /* ie = rtw_set_ie23a(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8), (pdev_network->SupportedRates + 8), &sz); */
389         } else {
390                 ie = rtw_set_ie23a(ie, WLAN_EID_SUPP_RATES, rateLen,
391                                 pdev_network->SupportedRates, &sz);
392         }
393
394         /* DS parameter set */
395         ie = rtw_set_ie23a(ie, WLAN_EID_DS_PARAMS, 1,
396                            (u8 *)&pdev_network->DSConfig, &sz);
397
398         /* IBSS Parameter Set */
399
400         ie = rtw_set_ie23a(ie, WLAN_EID_IBSS_PARAMS, 2,
401                            (u8 *)&pdev_network->ATIMWindow, &sz);
402
403         if (rateLen > 8) {
404                 ie = rtw_set_ie23a(ie, WLAN_EID_EXT_SUPP_RATES, (rateLen - 8),
405                                 (pdev_network->SupportedRates + 8), &sz);
406         }
407
408
409
410         /* return _SUCCESS; */
411
412         return sz;
413 }
414
415 static int rtw_get_wpa_cipher_suite(const u8 *s)
416 {
417         if (!memcmp(s, WPA_CIPHER_SUITE_NONE23A, WPA_SELECTOR_LEN))
418                 return WPA_CIPHER_NONE;
419         if (!memcmp(s, WPA_CIPHER_SUITE_WEP4023A, WPA_SELECTOR_LEN))
420                 return WPA_CIPHER_WEP40;
421         if (!memcmp(s, WPA_CIPHER_SUITE_TKIP23A, WPA_SELECTOR_LEN))
422                 return WPA_CIPHER_TKIP;
423         if (!memcmp(s, WPA_CIPHER_SUITE_CCMP23A, WPA_SELECTOR_LEN))
424                 return WPA_CIPHER_CCMP;
425         if (!memcmp(s, WPA_CIPHER_SUITE_WEP10423A, WPA_SELECTOR_LEN))
426                 return WPA_CIPHER_WEP104;
427
428         return 0;
429 }
430
431 static int rtw_get_wpa2_cipher_suite(const u8 *s)
432 {
433         if (!memcmp(s, RSN_CIPHER_SUITE_NONE23A, RSN_SELECTOR_LEN))
434                 return WPA_CIPHER_NONE;
435         if (!memcmp(s, RSN_CIPHER_SUITE_WEP4023A, RSN_SELECTOR_LEN))
436                 return WPA_CIPHER_WEP40;
437         if (!memcmp(s, RSN_CIPHER_SUITE_TKIP23A, RSN_SELECTOR_LEN))
438                 return WPA_CIPHER_TKIP;
439         if (!memcmp(s, RSN_CIPHER_SUITE_CCMP23A, RSN_SELECTOR_LEN))
440                 return WPA_CIPHER_CCMP;
441         if (!memcmp(s, RSN_CIPHER_SUITE_WEP10423A, RSN_SELECTOR_LEN))
442                 return WPA_CIPHER_WEP104;
443
444         return 0;
445 }
446
447 int rtw_parse_wpa_ie23a(const u8* wpa_ie, int wpa_ie_len, int *group_cipher, int *pairwise_cipher, int *is_8021x)
448 {
449         int i, ret = _SUCCESS;
450         int left, count;
451         const u8 *pos;
452
453         if (wpa_ie_len <= 0) {
454                 /* No WPA IE - fail silently */
455                 return _FAIL;
456         }
457
458         if (wpa_ie[1] != (u8)(wpa_ie_len - 2))
459                 return _FAIL;
460
461         pos = wpa_ie;
462
463         pos += 8;
464         left = wpa_ie_len - 8;
465
466         /* group_cipher */
467         if (left >= WPA_SELECTOR_LEN) {
468
469                 *group_cipher = rtw_get_wpa_cipher_suite(pos);
470
471                 pos += WPA_SELECTOR_LEN;
472                 left -= WPA_SELECTOR_LEN;
473         } else if (left > 0) {
474                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
475                          "%s: ie length mismatch, %u too much\n",
476                          __func__, left);
477
478                 return _FAIL;
479         }
480
481         /* pairwise_cipher */
482         if (left >= 2) {
483                 /* count = le16_to_cpu(*(u16*)pos); */
484                 count = get_unaligned_le16(pos);
485                 pos += 2;
486                 left -= 2;
487
488                 if (count == 0 || left < count * WPA_SELECTOR_LEN) {
489                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
490                                  "%s: ie count botch (pairwise), count %u left %u\n",
491                                  __func__, count, left);
492                         return _FAIL;
493                 }
494
495                 for (i = 0; i < count; i++) {
496                         *pairwise_cipher |= rtw_get_wpa_cipher_suite(pos);
497
498                         pos += WPA_SELECTOR_LEN;
499                         left -= WPA_SELECTOR_LEN;
500                 }
501         } else if (left == 1) {
502                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
503                          "%s: ie too short (for key mgmt)\n", __func__);
504                 return _FAIL;
505         }
506
507         if (is_8021x) {
508                 if (left >= 6) {
509                         pos += 2;
510                         if (!memcmp(pos, RTW_WPA_OUI23A_TYPE, 4)) {
511                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
512                                          "%s : there has 802.1x auth\n",
513                                          __func__);
514                                 *is_8021x = 1;
515                         }
516                 }
517         }
518
519         return ret;
520 }
521
522 int rtw_parse_wpa2_ie23a(const u8 *rsn_ie, int rsn_ie_len, int *group_cipher,
523                       int *pairwise_cipher, int *is_8021x)
524 {
525         int i, ret = _SUCCESS;
526         int left, count;
527         const u8 *pos;
528         u8 SUITE_1X[4] = {0x00, 0x0f, 0xac, 0x01};
529
530         if (rsn_ie_len <= 0) {
531                 /* No RSN IE - fail silently */
532                 return _FAIL;
533         }
534
535         if (*rsn_ie != WLAN_EID_RSN || *(rsn_ie+1) != (u8)(rsn_ie_len - 2)) {
536                 return _FAIL;
537         }
538
539         pos = rsn_ie;
540         pos += 4;
541         left = rsn_ie_len - 4;
542
543         /* group_cipher */
544         if (left >= RSN_SELECTOR_LEN) {
545                 *group_cipher = rtw_get_wpa2_cipher_suite(pos);
546
547                 pos += RSN_SELECTOR_LEN;
548                 left -= RSN_SELECTOR_LEN;
549         } else if (left > 0) {
550                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
551                          "%s: ie length mismatch, %u too much\n",
552                          __func__, left);
553                 return _FAIL;
554         }
555
556         /* pairwise_cipher */
557         if (left >= 2) {
558                 /* count = le16_to_cpu(*(u16*)pos); */
559                 count = get_unaligned_le16(pos);
560                 pos += 2;
561                 left -= 2;
562
563                 if (count == 0 || left < count * RSN_SELECTOR_LEN) {
564                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
565                                  "%s: ie count botch (pairwise), count %u left %u\n",
566                                  __func__, count, left);
567                         return _FAIL;
568                 }
569
570                 for (i = 0; i < count; i++) {
571                         *pairwise_cipher |= rtw_get_wpa2_cipher_suite(pos);
572
573                         pos += RSN_SELECTOR_LEN;
574                         left -= RSN_SELECTOR_LEN;
575                 }
576         } else if (left == 1) {
577                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_,
578                          "%s: ie too short (for key mgmt)\n",  __func__);
579
580                 return _FAIL;
581         }
582
583         if (is_8021x) {
584                 if (left >= 6) {
585                         pos += 2;
586                         if (!memcmp(pos, SUITE_1X, 4)) {
587                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
588                                          "%s (): there has 802.1x auth\n",
589                                          __func__);
590                                 *is_8021x = 1;
591                         }
592                 }
593         }
594
595         return ret;
596 }
597
598 /**
599  * rtw_get_wps_attr23a - Search a specific WPS attribute from a given WPS IE
600  * @wps_ie: Address of WPS IE to search
601  * @wps_ielen: Length limit from wps_ie
602  * @target_attr_id: The attribute ID of WPS attribute to search
603  * @buf_attr: If not NULL and the WPS attribute is found, WPS attribute
604  *            will be copied to the buf starting from buf_attr
605  * @len_attr: If not NULL and the WPS attribute is found, will set to the
606  *            length of the entire WPS attribute
607  *
608  * Returns: the address of the specific WPS attribute found, or NULL
609  */
610 const u8 *rtw_get_wps_attr23a(const u8 *wps_ie, uint wps_ielen,
611                               u16 target_attr_id, u8 *buf_attr, u32 *len_attr)
612 {
613         const u8 *attr_ptr = NULL;
614         const u8 *target_attr_ptr = NULL;
615         u8 wps_oui[4] = {0x00, 0x50, 0xF2, 0x04};
616
617         if (len_attr)
618                 *len_attr = 0;
619
620         if (wps_ie[0] != WLAN_EID_VENDOR_SPECIFIC ||
621             memcmp(wps_ie + 2, wps_oui, 4)) {
622                 return attr_ptr;
623         }
624
625         /*  6 = 1(Element ID) + 1(Length) + 4(WPS OUI) */
626         attr_ptr = wps_ie + 6; /* goto first attr */
627
628         while (attr_ptr - wps_ie < wps_ielen) {
629                 /*  4 = 2(Attribute ID) + 2(Length) */
630                 u16 attr_id = get_unaligned_be16(attr_ptr);
631                 u16 attr_data_len = get_unaligned_be16(attr_ptr + 2);
632                 u16 attr_len = attr_data_len + 4;
633
634                 /* DBG_8723A("%s attr_ptr:%p, id:%u, length:%u\n", __func__, attr_ptr, attr_id, attr_data_len); */
635                 if (attr_id == target_attr_id) {
636                         target_attr_ptr = attr_ptr;
637
638                         if (buf_attr)
639                                 memcpy(buf_attr, attr_ptr, attr_len);
640
641                         if (len_attr)
642                                 *len_attr = attr_len;
643
644                         break;
645                 } else {
646                         attr_ptr += attr_len; /* goto next */
647                 }
648         }
649
650         return target_attr_ptr;
651 }
652
653 /**
654  * rtw_get_wps_attr_content23a - Search a specific WPS attribute content
655  * from a given WPS IE
656  * @wps_ie: Address of WPS IE to search
657  * @wps_ielen: Length limit from wps_ie
658  * @target_attr_id: The attribute ID of WPS attribute to search
659  * @buf_content: If not NULL and the WPS attribute is found, WPS attribute
660  *               content will be copied to the buf starting from buf_content
661  * @len_content: If not NULL and the WPS attribute is found, will set to the
662  *               length of the WPS attribute content
663  *
664  * Returns: the address of the specific WPS attribute content found, or NULL
665  */
666 const u8 *rtw_get_wps_attr_content23a(const u8 *wps_ie, uint wps_ielen,
667                                       u16 target_attr_id, u8 *buf_content)
668 {
669         const u8 *attr_ptr;
670         u32 attr_len;
671
672         attr_ptr = rtw_get_wps_attr23a(wps_ie, wps_ielen, target_attr_id,
673                                     NULL, &attr_len);
674
675         if (attr_ptr && attr_len) {
676                 if (buf_content)
677                         memcpy(buf_content, attr_ptr + 4, attr_len - 4);
678
679                 return attr_ptr + 4;
680         }
681
682         return NULL;
683 }
684
685 static int rtw_get_cipher_info(struct wlan_network *pnetwork)
686 {
687         const u8 *pbuf;
688         int group_cipher = 0, pairwise_cipher = 0, is8021x = 0;
689         int ret = _FAIL;
690         int r, plen;
691         char *pie;
692
693         pie = pnetwork->network.IEs;
694         plen = pnetwork->network.IELength;
695
696         pbuf = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
697                                        WLAN_OUI_TYPE_MICROSOFT_WPA, pie, plen);
698
699         if (pbuf && pbuf[1] > 0) {
700                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
701                          "rtw_get_cipher_info: wpa_ielen: %d\n", pbuf[1]);
702                 r = rtw_parse_wpa_ie23a(pbuf, pbuf[1] + 2, &group_cipher,
703                                      &pairwise_cipher, &is8021x);
704                 if (r == _SUCCESS) {
705                         pnetwork->BcnInfo.pairwise_cipher = pairwise_cipher;
706                         pnetwork->BcnInfo.group_cipher = group_cipher;
707                         pnetwork->BcnInfo.is_8021x = is8021x;
708                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
709                                  "%s: pnetwork->pairwise_cipher: %d, is_8021x is %d\n",
710                                  __func__, pnetwork->BcnInfo.pairwise_cipher,
711                                  pnetwork->BcnInfo.is_8021x);
712                         ret = _SUCCESS;
713                 }
714         } else {
715                 pbuf = cfg80211_find_ie(WLAN_EID_RSN, pie, plen);
716
717                 if (pbuf && pbuf[1] > 0) {
718                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
719                                  "get RSN IE\n");
720                         r = rtw_parse_wpa2_ie23a(pbuf, pbuf[1] + 2,
721                                               &group_cipher, &pairwise_cipher,
722                                               &is8021x);
723                         if (r == _SUCCESS) {
724                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
725                                          "get RSN IE  OK!!!\n");
726                                 pnetwork->BcnInfo.pairwise_cipher =
727                                         pairwise_cipher;
728                                 pnetwork->BcnInfo.group_cipher = group_cipher;
729                                 pnetwork->BcnInfo.is_8021x = is8021x;
730                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
731                                          "%s: pnetwork->pairwise_cipher: %d,pnetwork->group_cipher is %d, is_8021x is %d\n",
732                                          __func__,
733                                          pnetwork->BcnInfo.pairwise_cipher,
734                                          pnetwork->BcnInfo.group_cipher,
735                                          pnetwork->BcnInfo.is_8021x);
736                                 ret = _SUCCESS;
737                         }
738                 }
739         }
740
741         return ret;
742 }
743
744 void rtw_get_bcn_info23a(struct wlan_network *pnetwork)
745 {
746         u8 bencrypt = 0;
747         int pie_len;
748         u8 *pie;
749         const u8 *p;
750
751         if (pnetwork->network.capability & WLAN_CAPABILITY_PRIVACY) {
752                 bencrypt = 1;
753                 pnetwork->network.Privacy = 1;
754         } else
755                 pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_OPENSYS;
756
757         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
758                  "%s: ssid =%s\n", __func__, pnetwork->network.Ssid.ssid);
759
760         pie = pnetwork->network.IEs;
761         pie_len = pnetwork->network.IELength;
762
763         p = cfg80211_find_ie(WLAN_EID_RSN, pie, pie_len);
764         if (p && p[1]) {
765                 pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WPA2;
766         } else if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
767                                            WLAN_OUI_TYPE_MICROSOFT_WPA,
768                                            pie, pie_len)) {
769                 pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WPA;
770         } else {
771                 if (bencrypt)
772                         pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WEP;
773         }
774         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
775                  "%s: pnetwork->encryp_protocol is %x\n", __func__,
776                  pnetwork->BcnInfo.encryp_protocol);
777         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
778                  "%s: pnetwork->encryp_protocol is %x\n", __func__,
779                  pnetwork->BcnInfo.encryp_protocol);
780         rtw_get_cipher_info(pnetwork);
781
782         /* get bwmode and ch_offset */
783 }
784
785 /* show MCS rate, unit: 100Kbps */
786 u16 rtw_mcs_rate23a(u8 rf_type, u8 bw_40MHz, u8 short_GI_20, u8 short_GI_40,
787                     struct ieee80211_mcs_info *mcs)
788 {
789         u16 max_rate = 0;
790
791         if (rf_type == RF_1T1R) {
792                 if (mcs->rx_mask[0] & BIT(7))
793                         max_rate = (bw_40MHz) ? ((short_GI_40)?1500:1350):
794                                 ((short_GI_20)?722:650);
795                 else if (mcs->rx_mask[0] & BIT(6))
796                         max_rate = (bw_40MHz) ? ((short_GI_40)?1350:1215):
797                                 ((short_GI_20)?650:585);
798                 else if (mcs->rx_mask[0] & BIT(5))
799                         max_rate = (bw_40MHz) ? ((short_GI_40)?1200:1080):
800                                 ((short_GI_20)?578:520);
801                 else if (mcs->rx_mask[0] & BIT(4))
802                         max_rate = (bw_40MHz) ? ((short_GI_40)?900:810):
803                                 ((short_GI_20)?433:390);
804                 else if (mcs->rx_mask[0] & BIT(3))
805                         max_rate = (bw_40MHz) ? ((short_GI_40)?600:540):
806                                 ((short_GI_20)?289:260);
807                 else if (mcs->rx_mask[0] & BIT(2))
808                         max_rate = (bw_40MHz) ? ((short_GI_40)?450:405):
809                                 ((short_GI_20)?217:195);
810                 else if (mcs->rx_mask[0] & BIT(1))
811                         max_rate = (bw_40MHz) ? ((short_GI_40)?300:270):
812                                 ((short_GI_20)?144:130);
813                 else if (mcs->rx_mask[0] & BIT(0))
814                         max_rate = (bw_40MHz) ? ((short_GI_40)?150:135):
815                                 ((short_GI_20)?72:65);
816         } else {
817                 if (mcs->rx_mask[1]) {
818                         if (mcs->rx_mask[1] & BIT(7))
819                                 max_rate = (bw_40MHz) ? ((short_GI_40)?3000:2700):((short_GI_20)?1444:1300);
820                         else if (mcs->rx_mask[1] & BIT(6))
821                                 max_rate = (bw_40MHz) ? ((short_GI_40)?2700:2430):((short_GI_20)?1300:1170);
822                         else if (mcs->rx_mask[1] & BIT(5))
823                                 max_rate = (bw_40MHz) ? ((short_GI_40)?2400:2160):((short_GI_20)?1156:1040);
824                         else if (mcs->rx_mask[1] & BIT(4))
825                                 max_rate = (bw_40MHz) ? ((short_GI_40)?1800:1620):((short_GI_20)?867:780);
826                         else if (mcs->rx_mask[1] & BIT(3))
827                                 max_rate = (bw_40MHz) ? ((short_GI_40)?1200:1080):((short_GI_20)?578:520);
828                         else if (mcs->rx_mask[1] & BIT(2))
829                                 max_rate = (bw_40MHz) ? ((short_GI_40)?900:810):((short_GI_20)?433:390);
830                         else if (mcs->rx_mask[1] & BIT(1))
831                                 max_rate = (bw_40MHz) ? ((short_GI_40)?600:540):((short_GI_20)?289:260);
832                         else if (mcs->rx_mask[1] & BIT(0))
833                                 max_rate = (bw_40MHz) ? ((short_GI_40)?300:270):((short_GI_20)?144:130);
834                 } else {
835                         if (mcs->rx_mask[0] & BIT(7))
836                                 max_rate = (bw_40MHz) ? ((short_GI_40)?1500:1350):((short_GI_20)?722:650);
837                         else if (mcs->rx_mask[0] & BIT(6))
838                                 max_rate = (bw_40MHz) ? ((short_GI_40)?1350:1215):((short_GI_20)?650:585);
839                         else if (mcs->rx_mask[0] & BIT(5))
840                                 max_rate = (bw_40MHz) ? ((short_GI_40)?1200:1080):((short_GI_20)?578:520);
841                         else if (mcs->rx_mask[0] & BIT(4))
842                                 max_rate = (bw_40MHz) ? ((short_GI_40)?900:810):((short_GI_20)?433:390);
843                         else if (mcs->rx_mask[0] & BIT(3))
844                                 max_rate = (bw_40MHz) ? ((short_GI_40)?600:540):((short_GI_20)?289:260);
845                         else if (mcs->rx_mask[0] & BIT(2))
846                                 max_rate = (bw_40MHz) ? ((short_GI_40)?450:405):((short_GI_20)?217:195);
847                         else if (mcs->rx_mask[0] & BIT(1))
848                                 max_rate = (bw_40MHz) ? ((short_GI_40)?300:270):((short_GI_20)?144:130);
849                         else if (mcs->rx_mask[0] & BIT(0))
850                                 max_rate = (bw_40MHz) ? ((short_GI_40)?150:135):((short_GI_20)?72:65);
851                 }
852         }
853         return max_rate;
854 }