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