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