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