Linux-libre 3.16.85-gnu
[librecmc/linux-libre.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  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _IEEE80211_C
21
22 #include <drv_types.h>
23 #include <ieee80211.h>
24 #include <wifi.h>
25 #include <osdep_service.h>
26 #include <wlan_bssdef.h>
27 #include <usb_osintf.h>
28
29 u8 RTW_WPA_OUI_TYPE[] = { 0x00, 0x50, 0xf2, 1 };
30 u16 RTW_WPA_VERSION = 1;
31 u8 WPA_AUTH_KEY_MGMT_NONE[] = { 0x00, 0x50, 0xf2, 0 };
32 u8 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x50, 0xf2, 1 };
33 u8 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x50, 0xf2, 2 };
34 u8 WPA_CIPHER_SUITE_NONE[] = { 0x00, 0x50, 0xf2, 0 };
35 u8 WPA_CIPHER_SUITE_WEP40[] = { 0x00, 0x50, 0xf2, 1 };
36 u8 WPA_CIPHER_SUITE_TKIP[] = { 0x00, 0x50, 0xf2, 2 };
37 u8 WPA_CIPHER_SUITE_WRAP[] = { 0x00, 0x50, 0xf2, 3 };
38 u8 WPA_CIPHER_SUITE_CCMP[] = { 0x00, 0x50, 0xf2, 4 };
39 u8 WPA_CIPHER_SUITE_WEP104[] = { 0x00, 0x50, 0xf2, 5 };
40
41 u16 RSN_VERSION_BSD = 1;
42 u8 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x0f, 0xac, 1 };
43 u8 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x0f, 0xac, 2 };
44 u8 RSN_CIPHER_SUITE_NONE[] = { 0x00, 0x0f, 0xac, 0 };
45 u8 RSN_CIPHER_SUITE_WEP40[] = { 0x00, 0x0f, 0xac, 1 };
46 u8 RSN_CIPHER_SUITE_TKIP[] = { 0x00, 0x0f, 0xac, 2 };
47 u8 RSN_CIPHER_SUITE_WRAP[] = { 0x00, 0x0f, 0xac, 3 };
48 u8 RSN_CIPHER_SUITE_CCMP[] = { 0x00, 0x0f, 0xac, 4 };
49 u8 RSN_CIPHER_SUITE_WEP104[] = { 0x00, 0x0f, 0xac, 5 };
50 /*  */
51 /*  for adhoc-master to generate ie and provide supported-rate to fw */
52 /*  */
53
54 static u8       WIFI_CCKRATES[] = {
55         (IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK),
56         (IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK),
57         (IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK),
58         (IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK)
59         };
60
61 static u8       WIFI_OFDMRATES[] = {
62          (IEEE80211_OFDM_RATE_6MB),
63          (IEEE80211_OFDM_RATE_9MB),
64          (IEEE80211_OFDM_RATE_12MB),
65          (IEEE80211_OFDM_RATE_18MB),
66          (IEEE80211_OFDM_RATE_24MB),
67          IEEE80211_OFDM_RATE_36MB,
68          IEEE80211_OFDM_RATE_48MB,
69          IEEE80211_OFDM_RATE_54MB
70         };
71
72
73 int rtw_get_bit_value_from_ieee_value(u8 val)
74 {
75         unsigned char dot11_rate_table[] = {
76                 2, 4, 11, 22, 12, 18, 24, 36, 48,
77                 72, 96, 108, 0}; /*  last element must be zero!! */
78
79         int i = 0;
80         while (dot11_rate_table[i] != 0) {
81                 if (dot11_rate_table[i] == val)
82                         return BIT(i);
83                 i++;
84         }
85         return 0;
86 }
87
88 uint    rtw_is_cckrates_included(u8 *rate)
89 {
90         u32     i = 0;
91
92         while (rate[i] != 0) {
93                 if  ((((rate[i]) & 0x7f) == 2) || (((rate[i]) & 0x7f) == 4) ||
94                      (((rate[i]) & 0x7f) == 11)  || (((rate[i]) & 0x7f) == 22))
95                         return true;
96                 i++;
97         }
98         return false;
99 }
100
101 uint    rtw_is_cckratesonly_included(u8 *rate)
102 {
103         u32 i = 0;
104
105         while (rate[i] != 0) {
106                 if  ((((rate[i]) & 0x7f) != 2) && (((rate[i]) & 0x7f) != 4) &&
107                      (((rate[i]) & 0x7f) != 11)  && (((rate[i]) & 0x7f) != 22))
108                         return false;
109                 i++;
110         }
111
112         return true;
113 }
114
115 int rtw_check_network_type(unsigned char *rate, int ratelen, int channel)
116 {
117         if (channel > 14) {
118                 if ((rtw_is_cckrates_included(rate)) == true)
119                         return WIRELESS_INVALID;
120                 else
121                         return WIRELESS_11A;
122         } else {  /*  could be pure B, pure G, or B/G */
123                 if ((rtw_is_cckratesonly_included(rate)) == true)
124                         return WIRELESS_11B;
125                 else if ((rtw_is_cckrates_included(rate)) == true)
126                         return  WIRELESS_11BG;
127                 else
128                         return WIRELESS_11G;
129         }
130 }
131
132 u8 *rtw_set_fixed_ie(unsigned char *pbuf, unsigned int len, unsigned char *source,
133                                 unsigned int *frlen)
134 {
135         memcpy((void *)pbuf, (void *)source, len);
136         *frlen = *frlen + len;
137         return pbuf + len;
138 }
139
140 /*  rtw_set_ie will update frame length */
141 u8 *rtw_set_ie
142 (
143         u8 *pbuf,
144         int index,
145         uint len,
146         u8 *source,
147         uint *frlen /* frame length */
148 )
149 {
150         *pbuf = (u8)index;
151
152         *(pbuf + 1) = (u8)len;
153
154         if (len > 0)
155                 memcpy((void *)(pbuf + 2), (void *)source, len);
156
157         *frlen = *frlen + (len + 2);
158
159         return pbuf + len + 2;
160 }
161
162 inline u8 *rtw_set_ie_ch_switch (u8 *buf, u32 *buf_len, u8 ch_switch_mode,
163         u8 new_ch, u8 ch_switch_cnt)
164 {
165         u8 ie_data[3];
166
167         ie_data[0] = ch_switch_mode;
168         ie_data[1] = new_ch;
169         ie_data[2] = ch_switch_cnt;
170         return rtw_set_ie(buf, WLAN_EID_CHANNEL_SWITCH,  3, ie_data, buf_len);
171 }
172
173 inline u8 secondary_ch_offset_to_hal_ch_offset(u8 ch_offset)
174 {
175         if (ch_offset == SCN)
176                 return HAL_PRIME_CHNL_OFFSET_DONT_CARE;
177         else if (ch_offset == SCA)
178                 return HAL_PRIME_CHNL_OFFSET_UPPER;
179         else if (ch_offset == SCB)
180                 return HAL_PRIME_CHNL_OFFSET_LOWER;
181
182         return HAL_PRIME_CHNL_OFFSET_DONT_CARE;
183 }
184
185 inline u8 hal_ch_offset_to_secondary_ch_offset(u8 ch_offset)
186 {
187         if (ch_offset == HAL_PRIME_CHNL_OFFSET_DONT_CARE)
188                 return SCN;
189         else if (ch_offset == HAL_PRIME_CHNL_OFFSET_LOWER)
190                 return SCB;
191         else if (ch_offset == HAL_PRIME_CHNL_OFFSET_UPPER)
192                 return SCA;
193
194         return SCN;
195 }
196
197 inline u8 *rtw_set_ie_secondary_ch_offset(u8 *buf, u32 *buf_len, u8 secondary_ch_offset)
198 {
199         return rtw_set_ie(buf, WLAN_EID_SECONDARY_CHANNEL_OFFSET,  1, &secondary_ch_offset, buf_len);
200 }
201
202 inline u8 *rtw_set_ie_mesh_ch_switch_parm(u8 *buf, u32 *buf_len, u8 ttl,
203         u8 flags, u16 reason, u16 precedence)
204 {
205         u8 ie_data[6];
206
207         ie_data[0] = ttl;
208         ie_data[1] = flags;
209         RTW_PUT_LE16((u8 *)&ie_data[2], reason);
210         RTW_PUT_LE16((u8 *)&ie_data[4], precedence);
211
212         return rtw_set_ie(buf, 0x118,  6, ie_data, buf_len);
213 }
214
215 /*----------------------------------------------------------------------------
216 index: the information element id index, limit is the limit for search
217 -----------------------------------------------------------------------------*/
218 u8 *rtw_get_ie(u8 *pbuf, int index, int *len, int limit)
219 {
220         int tmp, i;
221         u8 *p;
222         if (limit < 1)
223                 return NULL;
224
225         p = pbuf;
226         i = 0;
227         *len = 0;
228         while (1) {
229                 if (*p == index) {
230                         *len = *(p + 1);
231                         return p;
232                 } else {
233                         tmp = *(p + 1);
234                         p += (tmp + 2);
235                         i += (tmp + 2);
236                 }
237                 if (i >= limit)
238                         break;
239         }
240         return NULL;
241 }
242
243 /**
244  * rtw_get_ie_ex - Search specific IE from a series of IEs
245  * @in_ie: Address of IEs to search
246  * @in_len: Length limit from in_ie
247  * @eid: Element ID to match
248  * @oui: OUI to match
249  * @oui_len: OUI length
250  * @ie: If not NULL and the specific IE is found, the IE will be copied to the buf starting from the specific IE
251  * @ielen: If not NULL and the specific IE is found, will set to the length of the entire IE
252  *
253  * Returns: The address of the specific IE found, or NULL
254  */
255 u8 *rtw_get_ie_ex(u8 *in_ie, uint in_len, u8 eid, u8 *oui, u8 oui_len, u8 *ie, uint *ielen)
256 {
257         uint cnt;
258         u8 *target_ie = NULL;
259
260
261         if (ielen)
262                 *ielen = 0;
263
264         if (!in_ie || in_len <= 0)
265                 return target_ie;
266
267         cnt = 0;
268
269         while (cnt < in_len) {
270                 if (eid == in_ie[cnt] && (!oui || !memcmp(&in_ie[cnt+2], oui, oui_len))) {
271                         target_ie = &in_ie[cnt];
272
273                         if (ie)
274                                 memcpy(ie, &in_ie[cnt], in_ie[cnt+1]+2);
275
276                         if (ielen)
277                                 *ielen = in_ie[cnt+1]+2;
278
279                         break;
280                 } else {
281                         cnt += in_ie[cnt+1]+2; /* goto next */
282                 }
283         }
284         return target_ie;
285 }
286
287 /**
288  * rtw_ies_remove_ie - Find matching IEs and remove
289  * @ies: Address of IEs to search
290  * @ies_len: Pointer of length of ies, will update to new length
291  * @offset: The offset to start scarch
292  * @eid: Element ID to match
293  * @oui: OUI to match
294  * @oui_len: OUI length
295  *
296  * Returns: _SUCCESS: ies is updated, _FAIL: not updated
297  */
298 int rtw_ies_remove_ie(u8 *ies, uint *ies_len, uint offset, u8 eid, u8 *oui, u8 oui_len)
299 {
300         int ret = _FAIL;
301         u8 *target_ie;
302         u32 target_ielen;
303         u8 *start;
304         uint search_len;
305
306         if (!ies || !ies_len || *ies_len <= offset)
307                 goto exit;
308
309         start = ies + offset;
310         search_len = *ies_len - offset;
311
312         while (1) {
313                 target_ie = rtw_get_ie_ex(start, search_len, eid, oui, oui_len, NULL, &target_ielen);
314                 if (target_ie && target_ielen) {
315                         u8 buf[MAX_IE_SZ] = {0};
316                         u8 *remain_ies = target_ie + target_ielen;
317                         uint remain_len = search_len - (remain_ies - start);
318
319                         memcpy(buf, remain_ies, remain_len);
320                         memcpy(target_ie, buf, remain_len);
321                         *ies_len = *ies_len - target_ielen;
322                         ret = _SUCCESS;
323
324                         start = target_ie;
325                         search_len = remain_len;
326                 } else {
327                         break;
328                 }
329         }
330 exit:
331         return ret;
332 }
333
334 void rtw_set_supported_rate(u8 *SupportedRates, uint mode)
335 {
336
337         _rtw_memset(SupportedRates, 0, NDIS_802_11_LENGTH_RATES_EX);
338
339         switch (mode) {
340         case WIRELESS_11B:
341                 memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
342                 break;
343         case WIRELESS_11G:
344         case WIRELESS_11A:
345         case WIRELESS_11_5N:
346         case WIRELESS_11A_5N:/* Todo: no basic rate for ofdm ? */
347                 memcpy(SupportedRates, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN);
348                 break;
349         case WIRELESS_11BG:
350         case WIRELESS_11G_24N:
351         case WIRELESS_11_24N:
352         case WIRELESS_11BG_24N:
353                 memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
354                 memcpy(SupportedRates + IEEE80211_CCK_RATE_LEN, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN);
355                 break;
356         }
357 }
358
359 uint    rtw_get_rateset_len(u8  *rateset)
360 {
361         uint i = 0;
362         while (1) {
363                 if ((rateset[i]) == 0)
364                         break;
365                 if (i > 12)
366                         break;
367                 i++;
368         }
369         return i;
370 }
371
372 int rtw_generate_ie(struct registry_priv *pregistrypriv)
373 {
374         u8      wireless_mode;
375         int     sz = 0, rateLen;
376         struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
377         u8 *ie = pdev_network->IEs;
378
379
380         /* timestamp will be inserted by hardware */
381         sz += 8;
382         ie += sz;
383
384         /* beacon interval : 2bytes */
385         *(__le16 *)ie = cpu_to_le16((u16)pdev_network->Configuration.BeaconPeriod);/* BCN_INTERVAL; */
386         sz += 2;
387         ie += 2;
388
389         /* capability info */
390         *(u16 *)ie = 0;
391
392         *(__le16 *)ie |= cpu_to_le16(cap_IBSS);
393
394         if (pregistrypriv->preamble == PREAMBLE_SHORT)
395                 *(__le16 *)ie |= cpu_to_le16(cap_ShortPremble);
396
397         if (pdev_network->Privacy)
398                 *(__le16 *)ie |= cpu_to_le16(cap_Privacy);
399
400         sz += 2;
401         ie += 2;
402
403         /* SSID */
404         ie = rtw_set_ie(ie, _SSID_IE_, pdev_network->Ssid.SsidLength, pdev_network->Ssid.Ssid, &sz);
405
406         /* supported rates */
407         if (pregistrypriv->wireless_mode == WIRELESS_11ABGN) {
408                 if (pdev_network->Configuration.DSConfig > 14)
409                         wireless_mode = WIRELESS_11A_5N;
410                 else
411                         wireless_mode = WIRELESS_11BG_24N;
412         } else {
413                 wireless_mode = pregistrypriv->wireless_mode;
414         }
415
416                 rtw_set_supported_rate(pdev_network->SupportedRates, wireless_mode);
417
418         rateLen = rtw_get_rateset_len(pdev_network->SupportedRates);
419
420         if (rateLen > 8) {
421                 ie = rtw_set_ie(ie, _SUPPORTEDRATES_IE_, 8, pdev_network->SupportedRates, &sz);
422                 /* ie = rtw_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8), (pdev_network->SupportedRates + 8), &sz); */
423         } else {
424                 ie = rtw_set_ie(ie, _SUPPORTEDRATES_IE_, rateLen, pdev_network->SupportedRates, &sz);
425         }
426
427         /* DS parameter set */
428         ie = rtw_set_ie(ie, _DSSET_IE_, 1, (u8 *)&(pdev_network->Configuration.DSConfig), &sz);
429
430         /* IBSS Parameter Set */
431
432         ie = rtw_set_ie(ie, _IBSS_PARA_IE_, 2, (u8 *)&(pdev_network->Configuration.ATIMWindow), &sz);
433
434         if (rateLen > 8)
435                 ie = rtw_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8), (pdev_network->SupportedRates + 8), &sz);
436
437         return sz;
438 }
439
440 unsigned char *rtw_get_wpa_ie(unsigned char *pie, int *wpa_ie_len, int limit)
441 {
442         int len;
443         u16 val16;
444         __le16 le_tmp;
445         unsigned char wpa_oui_type[] = {0x00, 0x50, 0xf2, 0x01};
446         u8 *pbuf = pie;
447         int limit_new = limit;
448
449         while (1) {
450                 pbuf = rtw_get_ie(pbuf, _WPA_IE_ID_, &len, limit_new);
451
452                 if (pbuf) {
453                         /* check if oui matches... */
454                         if (!memcmp((pbuf + 2), wpa_oui_type, sizeof(wpa_oui_type)) == false)
455                                 goto check_next_ie;
456
457                         /* check version... */
458                         memcpy((u8 *)&le_tmp, (pbuf + 6), sizeof(val16));
459
460                         val16 = le16_to_cpu(le_tmp);
461                         if (val16 != 0x0001)
462                                 goto check_next_ie;
463                         *wpa_ie_len = *(pbuf + 1);
464                         return pbuf;
465                 } else {
466                         *wpa_ie_len = 0;
467                         return NULL;
468                 }
469
470 check_next_ie:
471                 limit_new = limit - (pbuf - pie) - 2 - len;
472                 if (limit_new <= 0)
473                         break;
474                 pbuf += (2 + len);
475         }
476         *wpa_ie_len = 0;
477         return NULL;
478 }
479
480 unsigned char *rtw_get_wpa2_ie(unsigned char *pie, int *rsn_ie_len, int limit)
481 {
482
483         return rtw_get_ie(pie, _WPA2_IE_ID_, rsn_ie_len, limit);
484 }
485
486 int rtw_get_wpa_cipher_suite(u8 *s)
487 {
488         if (!memcmp(s, WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN))
489                 return WPA_CIPHER_NONE;
490         if (!memcmp(s, WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN))
491                 return WPA_CIPHER_WEP40;
492         if (!memcmp(s, WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN))
493                 return WPA_CIPHER_TKIP;
494         if (!memcmp(s, WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN))
495                 return WPA_CIPHER_CCMP;
496         if (!memcmp(s, WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN))
497                 return WPA_CIPHER_WEP104;
498
499         return 0;
500 }
501
502 int rtw_get_wpa2_cipher_suite(u8 *s)
503 {
504         if (!memcmp(s, RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN))
505                 return WPA_CIPHER_NONE;
506         if (!memcmp(s, RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN))
507                 return WPA_CIPHER_WEP40;
508         if (!memcmp(s, RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN))
509                 return WPA_CIPHER_TKIP;
510         if (!memcmp(s, RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN))
511                 return WPA_CIPHER_CCMP;
512         if (!memcmp(s, RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN))
513                 return WPA_CIPHER_WEP104;
514
515         return 0;
516 }
517
518
519 int rtw_parse_wpa_ie(u8 *wpa_ie, int wpa_ie_len, int *group_cipher, int *pairwise_cipher, int *is_8021x)
520 {
521         int i, ret = _SUCCESS;
522         int left, count;
523         u8 *pos;
524         u8 SUITE_1X[4] = {0x00, 0x50, 0xf2, 1};
525
526         if (wpa_ie_len <= 0) {
527                 /* No WPA IE - fail silently */
528                 return _FAIL;
529         }
530
531
532         if ((*wpa_ie != _WPA_IE_ID_) || (*(wpa_ie+1) != (u8)(wpa_ie_len - 2)) ||
533             (memcmp(wpa_ie+2, RTW_WPA_OUI_TYPE, WPA_SELECTOR_LEN)))
534                 return _FAIL;
535
536         pos = wpa_ie;
537
538         pos += 8;
539         left = wpa_ie_len - 8;
540
541
542         /* group_cipher */
543         if (left >= WPA_SELECTOR_LEN) {
544                 *group_cipher = rtw_get_wpa_cipher_suite(pos);
545                 pos += WPA_SELECTOR_LEN;
546                 left -= WPA_SELECTOR_LEN;
547         } else if (left > 0) {
548                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie length mismatch, %u too much", __func__, left));
549                 return _FAIL;
550         }
551
552         /* pairwise_cipher */
553         if (left >= 2) {
554                 count = RTW_GET_LE16(pos);
555                 pos += 2;
556                 left -= 2;
557
558                 if (count == 0 || left < count * WPA_SELECTOR_LEN) {
559                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie count botch (pairwise), "
560                                                 "count %u left %u", __func__, count, left));
561                         return _FAIL;
562                 }
563
564                 for (i = 0; i < count; i++) {
565                         *pairwise_cipher |= rtw_get_wpa_cipher_suite(pos);
566
567                         pos += WPA_SELECTOR_LEN;
568                         left -= WPA_SELECTOR_LEN;
569                 }
570         } else if (left == 1) {
571                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie too short (for key mgmt)",   __func__));
572                 return _FAIL;
573         }
574
575         if (is_8021x) {
576                 if (left >= 6) {
577                         pos += 2;
578                         if (!memcmp(pos, SUITE_1X, 4)) {
579                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("%s : there has 802.1x auth\n", __func__));
580                                 *is_8021x = 1;
581                         }
582                 }
583         }
584
585         return ret;
586 }
587
588 int rtw_parse_wpa2_ie(u8 *rsn_ie, int rsn_ie_len, int *group_cipher, int *pairwise_cipher, int *is_8021x)
589 {
590         int i, ret = _SUCCESS;
591         int left, count;
592         u8 *pos;
593         u8 SUITE_1X[4] = {0x00, 0x0f, 0xac, 0x01};
594
595         if (rsn_ie_len <= 0) {
596                 /* No RSN IE - fail silently */
597                 return _FAIL;
598         }
599
600
601         if ((*rsn_ie != _WPA2_IE_ID_) || (*(rsn_ie+1) != (u8)(rsn_ie_len - 2)))
602                 return _FAIL;
603
604         pos = rsn_ie;
605         pos += 4;
606         left = rsn_ie_len - 4;
607
608         /* group_cipher */
609         if (left >= RSN_SELECTOR_LEN) {
610                 *group_cipher = rtw_get_wpa2_cipher_suite(pos);
611
612                 pos += RSN_SELECTOR_LEN;
613                 left -= RSN_SELECTOR_LEN;
614
615         } else if (left > 0) {
616                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie length mismatch, %u too much", __func__, left));
617                 return _FAIL;
618         }
619
620         /* pairwise_cipher */
621         if (left >= 2) {
622                 count = RTW_GET_LE16(pos);
623                 pos += 2;
624                 left -= 2;
625
626                 if (count == 0 || left < count * RSN_SELECTOR_LEN) {
627                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie count botch (pairwise), "
628                                                  "count %u left %u", __func__, count, left));
629                         return _FAIL;
630                 }
631
632                 for (i = 0; i < count; i++) {
633                         *pairwise_cipher |= rtw_get_wpa2_cipher_suite(pos);
634
635                         pos += RSN_SELECTOR_LEN;
636                         left -= RSN_SELECTOR_LEN;
637                 }
638
639         } else if (left == 1) {
640                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_err_, ("%s: ie too short (for key mgmt)",  __func__));
641
642                 return _FAIL;
643         }
644
645         if (is_8021x) {
646                 if (left >= 6) {
647                         pos += 2;
648                         if (!memcmp(pos, SUITE_1X, 4)) {
649                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("%s (): there has 802.1x auth\n", __func__));
650                                 *is_8021x = 1;
651                         }
652                 }
653         }
654         return ret;
655 }
656
657 int rtw_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len, u8 *wpa_ie, u16 *wpa_len)
658 {
659         u8 authmode, sec_idx, i;
660         u8 wpa_oui[4] = {0x0, 0x50, 0xf2, 0x01};
661         uint    cnt;
662
663
664         /* Search required WPA or WPA2 IE and copy to sec_ie[] */
665
666         cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_);
667
668         sec_idx = 0;
669
670         while (cnt < in_len) {
671                 authmode = in_ie[cnt];
672
673                 if ((authmode == _WPA_IE_ID_) && (!memcmp(&in_ie[cnt+2], &wpa_oui[0], 4))) {
674                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
675                                          ("\n rtw_get_wpa_ie: sec_idx =%d in_ie[cnt+1]+2 =%d\n",
676                                          sec_idx, in_ie[cnt+1]+2));
677
678                                 if (wpa_ie) {
679                                         memcpy(wpa_ie, &in_ie[cnt], in_ie[cnt+1]+2);
680
681                                         for (i = 0; i < (in_ie[cnt+1]+2); i += 8) {
682                                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
683                                                          ("\n %2x,%2x,%2x,%2x,%2x,%2x,%2x,%2x\n",
684                                                          wpa_ie[i], wpa_ie[i+1], wpa_ie[i+2], wpa_ie[i+3], wpa_ie[i+4],
685                                                          wpa_ie[i+5], wpa_ie[i+6], wpa_ie[i+7]));
686                                         }
687                                 }
688
689                                 *wpa_len = in_ie[cnt+1]+2;
690                                 cnt += in_ie[cnt+1]+2;  /* get next */
691                 } else {
692                         if (authmode == _WPA2_IE_ID_) {
693                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
694                                          ("\n get_rsn_ie: sec_idx =%d in_ie[cnt+1]+2 =%d\n",
695                                          sec_idx, in_ie[cnt+1]+2));
696
697                                 if (rsn_ie) {
698                                         memcpy(rsn_ie, &in_ie[cnt], in_ie[cnt+1]+2);
699
700                                         for (i = 0; i < (in_ie[cnt+1]+2); i += 8) {
701                                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_,
702                                                          ("\n %2x,%2x,%2x,%2x,%2x,%2x,%2x,%2x\n",
703                                                          rsn_ie[i], rsn_ie[i+1], rsn_ie[i+2], rsn_ie[i+3], rsn_ie[i+4],
704                                                          rsn_ie[i+5], rsn_ie[i+6], rsn_ie[i+7]));
705                                                 }
706                                 }
707
708                                 *rsn_len = in_ie[cnt+1]+2;
709                                 cnt += in_ie[cnt+1]+2;  /* get next */
710                         } else {
711                                 cnt += in_ie[cnt+1]+2;   /* get next */
712                         }
713                 }
714         }
715
716
717         return *rsn_len + *wpa_len;
718 }
719
720 u8 rtw_is_wps_ie(u8 *ie_ptr, uint *wps_ielen)
721 {
722         u8 match = false;
723         u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
724
725         if (ie_ptr == NULL)
726                 return match;
727
728         eid = ie_ptr[0];
729
730         if ((eid == _WPA_IE_ID_) && (!memcmp(&ie_ptr[2], wps_oui, 4))) {
731                 *wps_ielen = ie_ptr[1]+2;
732                 match = true;
733         }
734         return match;
735 }
736
737 /**
738  * rtw_get_wps_ie - Search WPS IE from a series of IEs
739  * @in_ie: Address of IEs to search
740  * @in_len: Length limit from in_ie
741  * @wps_ie: If not NULL and WPS IE is found, WPS IE will be copied to the buf starting from wps_ie
742  * @wps_ielen: If not NULL and WPS IE is found, will set to the length of the entire WPS IE
743  *
744  * Returns: The address of the WPS IE found, or NULL
745  */
746 u8 *rtw_get_wps_ie(u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen)
747 {
748         uint cnt;
749         u8 *wpsie_ptr = NULL;
750         u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
751
752         if (wps_ielen)
753                 *wps_ielen = 0;
754
755         if (!in_ie || in_len <= 0)
756                 return wpsie_ptr;
757
758         cnt = 0;
759
760         while (cnt < in_len) {
761                 eid = in_ie[cnt];
762
763                 if ((eid == _WPA_IE_ID_) && (!memcmp(&in_ie[cnt+2], wps_oui, 4))) {
764                         wpsie_ptr = &in_ie[cnt];
765
766                         if (wps_ie)
767                                 memcpy(wps_ie, &in_ie[cnt], in_ie[cnt+1]+2);
768
769                         if (wps_ielen)
770                                 *wps_ielen = in_ie[cnt+1]+2;
771
772                         cnt += in_ie[cnt+1]+2;
773
774                         break;
775                 } else {
776                         cnt += in_ie[cnt+1]+2; /* goto next */
777                 }
778         }
779         return wpsie_ptr;
780 }
781
782 /**
783  * rtw_get_wps_attr - Search a specific WPS attribute from a given WPS IE
784  * @wps_ie: Address of WPS IE to search
785  * @wps_ielen: Length limit from wps_ie
786  * @target_attr_id: The attribute ID of WPS attribute to search
787  * @buf_attr: If not NULL and the WPS attribute is found, WPS attribute will be copied to the buf starting from buf_attr
788  * @len_attr: If not NULL and the WPS attribute is found, will set to the length of the entire WPS attribute
789  *
790  * Returns: the address of the specific WPS attribute found, or NULL
791  */
792 u8 *rtw_get_wps_attr(u8 *wps_ie, uint wps_ielen, u16 target_attr_id , u8 *buf_attr, u32 *len_attr)
793 {
794         u8 *attr_ptr = NULL;
795         u8 *target_attr_ptr = NULL;
796         u8 wps_oui[4] = {0x00, 0x50, 0xF2, 0x04};
797
798         if (len_attr)
799                 *len_attr = 0;
800
801         if ((wps_ie[0] != _VENDOR_SPECIFIC_IE_) ||
802             (memcmp(wps_ie + 2, wps_oui , 4)))
803                 return attr_ptr;
804
805         /*  6 = 1(Element ID) + 1(Length) + 4(WPS OUI) */
806         attr_ptr = wps_ie + 6; /* goto first attr */
807
808         while (attr_ptr - wps_ie < wps_ielen) {
809                 /*  4 = 2(Attribute ID) + 2(Length) */
810                 u16 attr_id = RTW_GET_BE16(attr_ptr);
811                 u16 attr_data_len = RTW_GET_BE16(attr_ptr + 2);
812                 u16 attr_len = attr_data_len + 4;
813
814                 if (attr_id == target_attr_id) {
815                         target_attr_ptr = attr_ptr;
816                         if (buf_attr)
817                                 memcpy(buf_attr, attr_ptr, attr_len);
818                         if (len_attr)
819                                 *len_attr = attr_len;
820                         break;
821                 } else {
822                         attr_ptr += attr_len; /* goto next */
823                 }
824         }
825         return target_attr_ptr;
826 }
827
828 /**
829  * rtw_get_wps_attr_content - Search a specific WPS attribute content from a given WPS IE
830  * @wps_ie: Address of WPS IE to search
831  * @wps_ielen: Length limit from wps_ie
832  * @target_attr_id: The attribute ID of WPS attribute to search
833  * @buf_content: If not NULL and the WPS attribute is found, WPS attribute content will be copied to the buf starting from buf_content
834  * @len_content: If not NULL and the WPS attribute is found, will set to the length of the WPS attribute content
835  *
836  * Returns: the address of the specific WPS attribute content found, or NULL
837  */
838 u8 *rtw_get_wps_attr_content(u8 *wps_ie, uint wps_ielen, u16 target_attr_id , u8 *buf_content, uint *len_content)
839 {
840         u8 *attr_ptr;
841         u32 attr_len;
842
843         if (len_content)
844                 *len_content = 0;
845
846         attr_ptr = rtw_get_wps_attr(wps_ie, wps_ielen, target_attr_id, NULL, &attr_len);
847
848         if (attr_ptr && attr_len) {
849                 if (buf_content)
850                         memcpy(buf_content, attr_ptr+4, attr_len-4);
851
852                 if (len_content)
853                         *len_content = attr_len-4;
854
855                 return attr_ptr+4;
856         }
857
858         return NULL;
859 }
860
861 static int rtw_ieee802_11_parse_vendor_specific(u8 *pos, uint elen,
862                                             struct rtw_ieee802_11_elems *elems,
863                                             int show_errors)
864 {
865         unsigned int oui;
866
867         /* first 3 bytes in vendor specific information element are the IEEE
868          * OUI of the vendor. The following byte is used a vendor specific
869          * sub-type. */
870         if (elen < 4) {
871                 if (show_errors) {
872                         DBG_88E("short vendor specific information element ignored (len=%lu)\n",
873                                 (unsigned long) elen);
874                 }
875                 return -1;
876         }
877
878         oui = RTW_GET_BE24(pos);
879         switch (oui) {
880         case OUI_MICROSOFT:
881                 /* Microsoft/Wi-Fi information elements are further typed and
882                  * subtyped */
883                 switch (pos[3]) {
884                 case 1:
885                         /* Microsoft OUI (00:50:F2) with OUI Type 1:
886                          * real WPA information element */
887                         elems->wpa_ie = pos;
888                         elems->wpa_ie_len = elen;
889                         break;
890                 case WME_OUI_TYPE: /* this is a Wi-Fi WME info. element */
891                         if (elen < 5) {
892                                 DBG_88E("short WME information element ignored (len=%lu)\n",
893                                         (unsigned long) elen);
894                                 return -1;
895                         }
896                         switch (pos[4]) {
897                         case WME_OUI_SUBTYPE_INFORMATION_ELEMENT:
898                         case WME_OUI_SUBTYPE_PARAMETER_ELEMENT:
899                                 elems->wme = pos;
900                                 elems->wme_len = elen;
901                                 break;
902                         case WME_OUI_SUBTYPE_TSPEC_ELEMENT:
903                                 elems->wme_tspec = pos;
904                                 elems->wme_tspec_len = elen;
905                                 break;
906                         default:
907                                 DBG_88E("unknown WME information element ignored (subtype=%d len=%lu)\n",
908                                         pos[4], (unsigned long) elen);
909                                 return -1;
910                         }
911                         break;
912                 case 4:
913                         /* Wi-Fi Protected Setup (WPS) IE */
914                         elems->wps_ie = pos;
915                         elems->wps_ie_len = elen;
916                         break;
917                 default:
918                         DBG_88E("Unknown Microsoft information element ignored (type=%d len=%lu)\n",
919                                 pos[3], (unsigned long) elen);
920                         return -1;
921                 }
922                 break;
923
924         case OUI_BROADCOM:
925                 switch (pos[3]) {
926                 case VENDOR_HT_CAPAB_OUI_TYPE:
927                         elems->vendor_ht_cap = pos;
928                         elems->vendor_ht_cap_len = elen;
929                         break;
930                 default:
931                         DBG_88E("Unknown Broadcom information element ignored (type=%d len=%lu)\n",
932                                 pos[3], (unsigned long) elen);
933                         return -1;
934                 }
935                 break;
936         default:
937                 DBG_88E("unknown vendor specific information element ignored (vendor OUI %02x:%02x:%02x len=%lu)\n",
938                         pos[0], pos[1], pos[2], (unsigned long) elen);
939                 return -1;
940         }
941         return 0;
942 }
943
944 /**
945  * ieee802_11_parse_elems - Parse information elements in management frames
946  * @start: Pointer to the start of IEs
947  * @len: Length of IE buffer in octets
948  * @elems: Data structure for parsed elements
949  * @show_errors: Whether to show parsing errors in debug log
950  * Returns: Parsing result
951  */
952 enum parse_res rtw_ieee802_11_parse_elems(u8 *start, uint len,
953                                 struct rtw_ieee802_11_elems *elems,
954                                 int show_errors)
955 {
956         uint left = len;
957         u8 *pos = start;
958         int unknown = 0;
959
960         _rtw_memset(elems, 0, sizeof(*elems));
961
962         while (left >= 2) {
963                 u8 id, elen;
964
965                 id = *pos++;
966                 elen = *pos++;
967                 left -= 2;
968
969                 if (elen > left) {
970                         if (show_errors) {
971                                 DBG_88E("IEEE 802.11 element parse failed (id=%d elen=%d left=%lu)\n",
972                                         id, elen, (unsigned long) left);
973                         }
974                         return ParseFailed;
975                 }
976
977                 switch (id) {
978                 case WLAN_EID_SSID:
979                         elems->ssid = pos;
980                         elems->ssid_len = elen;
981                         break;
982                 case WLAN_EID_SUPP_RATES:
983                         elems->supp_rates = pos;
984                         elems->supp_rates_len = elen;
985                         break;
986                 case WLAN_EID_FH_PARAMS:
987                         elems->fh_params = pos;
988                         elems->fh_params_len = elen;
989                         break;
990                 case WLAN_EID_DS_PARAMS:
991                         elems->ds_params = pos;
992                         elems->ds_params_len = elen;
993                         break;
994                 case WLAN_EID_CF_PARAMS:
995                         elems->cf_params = pos;
996                         elems->cf_params_len = elen;
997                         break;
998                 case WLAN_EID_TIM:
999                         elems->tim = pos;
1000                         elems->tim_len = elen;
1001                         break;
1002                 case WLAN_EID_IBSS_PARAMS:
1003                         elems->ibss_params = pos;
1004                         elems->ibss_params_len = elen;
1005                         break;
1006                 case WLAN_EID_CHALLENGE:
1007                         elems->challenge = pos;
1008                         elems->challenge_len = elen;
1009                         break;
1010                 case WLAN_EID_ERP_INFO:
1011                         elems->erp_info = pos;
1012                         elems->erp_info_len = elen;
1013                         break;
1014                 case WLAN_EID_EXT_SUPP_RATES:
1015                         elems->ext_supp_rates = pos;
1016                         elems->ext_supp_rates_len = elen;
1017                         break;
1018                 case WLAN_EID_VENDOR_SPECIFIC:
1019                         if (rtw_ieee802_11_parse_vendor_specific(pos, elen, elems, show_errors))
1020                                 unknown++;
1021                         break;
1022                 case WLAN_EID_RSN:
1023                         elems->rsn_ie = pos;
1024                         elems->rsn_ie_len = elen;
1025                         break;
1026                 case WLAN_EID_PWR_CAPABILITY:
1027                         elems->power_cap = pos;
1028                         elems->power_cap_len = elen;
1029                         break;
1030                 case WLAN_EID_SUPPORTED_CHANNELS:
1031                         elems->supp_channels = pos;
1032                         elems->supp_channels_len = elen;
1033                         break;
1034                 case WLAN_EID_MOBILITY_DOMAIN:
1035                         elems->mdie = pos;
1036                         elems->mdie_len = elen;
1037                         break;
1038                 case WLAN_EID_FAST_BSS_TRANSITION:
1039                         elems->ftie = pos;
1040                         elems->ftie_len = elen;
1041                         break;
1042                 case WLAN_EID_TIMEOUT_INTERVAL:
1043                         elems->timeout_int = pos;
1044                         elems->timeout_int_len = elen;
1045                         break;
1046                 case WLAN_EID_HT_CAP:
1047                         elems->ht_capabilities = pos;
1048                         elems->ht_capabilities_len = elen;
1049                         break;
1050                 case WLAN_EID_HT_OPERATION:
1051                         elems->ht_operation = pos;
1052                         elems->ht_operation_len = elen;
1053                         break;
1054                 default:
1055                         unknown++;
1056                         if (!show_errors)
1057                                 break;
1058                         DBG_88E("IEEE 802.11 element parse ignored unknown element (id=%d elen=%d)\n",
1059                                 id, elen);
1060                         break;
1061                 }
1062                 left -= elen;
1063                 pos += elen;
1064         }
1065         if (left)
1066                 return ParseFailed;
1067         return unknown ? ParseUnknown : ParseOK;
1068 }
1069
1070 u8 key_char2num(u8 ch)
1071 {
1072         if ((ch >= '0') && (ch <= '9'))
1073                 return ch - '0';
1074         else if ((ch >= 'a') && (ch <= 'f'))
1075                 return ch - 'a' + 10;
1076         else if ((ch >= 'A') && (ch <= 'F'))
1077                 return ch - 'A' + 10;
1078         else
1079                 return 0xff;
1080 }
1081
1082 u8 str_2char2num(u8 hch, u8 lch)
1083 {
1084     return (key_char2num(hch) * 10) + key_char2num(lch);
1085 }
1086
1087 u8 key_2char2num(u8 hch, u8 lch)
1088 {
1089     return (key_char2num(hch) << 4) | key_char2num(lch);
1090 }
1091
1092 void rtw_macaddr_cfg(u8 *mac_addr)
1093 {
1094         u8 mac[ETH_ALEN];
1095         if (mac_addr == NULL)
1096                 return;
1097
1098         if (rtw_initmac) {      /* Users specify the mac address */
1099                 int jj, kk;
1100
1101                 for (jj = 0, kk = 0; jj < ETH_ALEN; jj++, kk += 3)
1102                         mac[jj] = key_2char2num(rtw_initmac[kk], rtw_initmac[kk + 1]);
1103                 memcpy(mac_addr, mac, ETH_ALEN);
1104         } else {        /* Use the mac address stored in the Efuse */
1105                 memcpy(mac, mac_addr, ETH_ALEN);
1106         }
1107
1108         if (((mac[0] == 0xff) && (mac[1] == 0xff) && (mac[2] == 0xff) &&
1109              (mac[3] == 0xff) && (mac[4] == 0xff) && (mac[5] == 0xff)) ||
1110             ((mac[0] == 0x0) && (mac[1] == 0x0) && (mac[2] == 0x0) &&
1111              (mac[3] == 0x0) && (mac[4] == 0x0) && (mac[5] == 0x0))) {
1112                 mac[0] = 0x00;
1113                 mac[1] = 0xe0;
1114                 mac[2] = 0x4c;
1115                 mac[3] = 0x87;
1116                 mac[4] = 0x00;
1117                 mac[5] = 0x00;
1118                 /*  use default mac address */
1119                 memcpy(mac_addr, mac, ETH_ALEN);
1120                 DBG_88E("MAC Address from efuse error, assign default one !!!\n");
1121         }
1122
1123         DBG_88E("rtw_macaddr_cfg MAC Address  = %pM\n", (mac_addr));
1124 }
1125
1126 void dump_ies(u8 *buf, u32 buf_len)
1127 {
1128         u8 *pos = (u8 *)buf;
1129         u8 id, len;
1130
1131         while (pos-buf <= buf_len) {
1132                 id = *pos;
1133                 len = *(pos+1);
1134
1135                 DBG_88E("%s ID:%u, LEN:%u\n", __func__, id, len);
1136                 #ifdef CONFIG_88EU_P2P
1137                 dump_p2p_ie(pos, len);
1138                 #endif
1139                 dump_wps_ie(pos, len);
1140
1141                 pos += (2 + len);
1142         }
1143 }
1144
1145 void dump_wps_ie(u8 *ie, u32 ie_len)
1146 {
1147         u8 *pos = (u8 *)ie;
1148         u16 id;
1149         u16 len;
1150         u8 *wps_ie;
1151         uint wps_ielen;
1152
1153         wps_ie = rtw_get_wps_ie(ie, ie_len, NULL, &wps_ielen);
1154         if (wps_ie != ie || wps_ielen == 0)
1155                 return;
1156
1157         pos += 6;
1158         while (pos-ie < ie_len) {
1159                 id = RTW_GET_BE16(pos);
1160                 len = RTW_GET_BE16(pos + 2);
1161                 DBG_88E("%s ID:0x%04x, LEN:%u\n", __func__, id, len);
1162                 pos += (4+len);
1163         }
1164 }
1165
1166 #ifdef CONFIG_88EU_P2P
1167 void dump_p2p_ie(u8 *ie, u32 ie_len)
1168 {
1169         u8 *pos = (u8 *)ie;
1170         u8 id;
1171         u16 len;
1172         u8 *p2p_ie;
1173         uint p2p_ielen;
1174
1175         p2p_ie = rtw_get_p2p_ie(ie, ie_len, NULL, &p2p_ielen);
1176         if (p2p_ie != ie || p2p_ielen == 0)
1177                 return;
1178
1179         pos += 6;
1180         while (pos-ie < ie_len) {
1181                 id = *pos;
1182                 len = RTW_GET_LE16(pos+1);
1183                 DBG_88E("%s ID:%u, LEN:%u\n", __func__, id, len);
1184                 pos += (3+len);
1185         }
1186 }
1187
1188 /**
1189  * rtw_get_p2p_ie - Search P2P IE from a series of IEs
1190  * @in_ie: Address of IEs to search
1191  * @in_len: Length limit from in_ie
1192  * @p2p_ie: If not NULL and P2P IE is found, P2P IE will be copied to the buf starting from p2p_ie
1193  * @p2p_ielen: If not NULL and P2P IE is found, will set to the length of the entire P2P IE
1194  *
1195  * Returns: The address of the P2P IE found, or NULL
1196  */
1197 u8 *rtw_get_p2p_ie(u8 *in_ie, int in_len, u8 *p2p_ie, uint *p2p_ielen)
1198 {
1199         uint cnt = 0;
1200         u8 *p2p_ie_ptr;
1201         u8 eid, p2p_oui[4] = {0x50, 0x6F, 0x9A, 0x09};
1202
1203         if (p2p_ielen != NULL)
1204                 *p2p_ielen = 0;
1205
1206         while (cnt < in_len) {
1207                 eid = in_ie[cnt];
1208                 if ((in_len < 0) || (cnt > MAX_IE_SZ)) {
1209                         dump_stack();
1210                         return NULL;
1211                 }
1212                 if ((eid == _VENDOR_SPECIFIC_IE_) && (!memcmp(&in_ie[cnt+2], p2p_oui, 4))) {
1213                         p2p_ie_ptr = in_ie + cnt;
1214
1215                         if (p2p_ie != NULL)
1216                                 memcpy(p2p_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
1217                         if (p2p_ielen != NULL)
1218                                 *p2p_ielen = in_ie[cnt + 1] + 2;
1219                         return p2p_ie_ptr;
1220                 } else {
1221                         cnt += in_ie[cnt + 1] + 2; /* goto next */
1222                 }
1223         }
1224         return NULL;
1225 }
1226
1227 /**
1228  * rtw_get_p2p_attr - Search a specific P2P attribute from a given P2P IE
1229  * @p2p_ie: Address of P2P IE to search
1230  * @p2p_ielen: Length limit from p2p_ie
1231  * @target_attr_id: The attribute ID of P2P attribute to search
1232  * @buf_attr: If not NULL and the P2P attribute is found, P2P attribute will be copied to the buf starting from buf_attr
1233  * @len_attr: If not NULL and the P2P attribute is found, will set to the length of the entire P2P attribute
1234  *
1235  * Returns: the address of the specific WPS attribute found, or NULL
1236  */
1237 u8 *rtw_get_p2p_attr(u8 *p2p_ie, uint p2p_ielen, u8 target_attr_id , u8 *buf_attr, u32 *len_attr)
1238 {
1239         u8 *attr_ptr = NULL;
1240         u8 *target_attr_ptr = NULL;
1241         u8 p2p_oui[4] = {0x50, 0x6F, 0x9A, 0x09};
1242
1243         if (len_attr)
1244                 *len_attr = 0;
1245
1246         if (!p2p_ie || (p2p_ie[0] != _VENDOR_SPECIFIC_IE_) ||
1247             (memcmp(p2p_ie + 2, p2p_oui , 4)))
1248                 return attr_ptr;
1249
1250         /*  6 = 1(Element ID) + 1(Length) + 3 (OUI) + 1(OUI Type) */
1251         attr_ptr = p2p_ie + 6; /* goto first attr */
1252
1253         while (attr_ptr - p2p_ie < p2p_ielen) {
1254                 /*  3 = 1(Attribute ID) + 2(Length) */
1255                 u8 attr_id = *attr_ptr;
1256                 u16 attr_data_len = RTW_GET_LE16(attr_ptr + 1);
1257                 u16 attr_len = attr_data_len + 3;
1258
1259                 if (attr_id == target_attr_id) {
1260                         target_attr_ptr = attr_ptr;
1261
1262                         if (buf_attr)
1263                                 memcpy(buf_attr, attr_ptr, attr_len);
1264                         if (len_attr)
1265                                 *len_attr = attr_len;
1266                         break;
1267                 } else {
1268                         attr_ptr += attr_len; /* goto next */
1269                 }
1270         }
1271         return target_attr_ptr;
1272 }
1273
1274 /**
1275  * rtw_get_p2p_attr_content - Search a specific P2P attribute content from a given P2P IE
1276  * @p2p_ie: Address of P2P IE to search
1277  * @p2p_ielen: Length limit from p2p_ie
1278  * @target_attr_id: The attribute ID of P2P attribute to search
1279  * @buf_content: If not NULL and the P2P attribute is found, P2P attribute content will be copied to the buf starting from buf_content
1280  * @len_content: If not NULL and the P2P attribute is found, will set to the length of the P2P attribute content
1281  *
1282  * Returns: the address of the specific P2P attribute content found, or NULL
1283  */
1284 u8 *rtw_get_p2p_attr_content(u8 *p2p_ie, uint p2p_ielen, u8 target_attr_id , u8 *buf_content, uint *len_content)
1285 {
1286         u8 *attr_ptr;
1287         u32 attr_len;
1288
1289         if (len_content)
1290                 *len_content = 0;
1291
1292         attr_ptr = rtw_get_p2p_attr(p2p_ie, p2p_ielen, target_attr_id, NULL, &attr_len);
1293
1294         if (attr_ptr && attr_len) {
1295                 if (buf_content)
1296                         memcpy(buf_content, attr_ptr+3, attr_len-3);
1297
1298                 if (len_content)
1299                         *len_content = attr_len-3;
1300
1301                 return attr_ptr+3;
1302         }
1303
1304         return NULL;
1305 }
1306
1307 u32 rtw_set_p2p_attr_content(u8 *pbuf, u8 attr_id, u16 attr_len, u8 *pdata_attr)
1308 {
1309         u32 a_len;
1310
1311         *pbuf = attr_id;
1312
1313         /* u16*)(pbuf + 1) = cpu_to_le16(attr_len); */
1314         RTW_PUT_LE16(pbuf + 1, attr_len);
1315
1316         if (pdata_attr)
1317                 memcpy(pbuf + 3, pdata_attr, attr_len);
1318
1319         a_len = attr_len + 3;
1320
1321         return a_len;
1322 }
1323
1324 static uint rtw_p2p_attr_remove(u8 *ie, uint ielen_ori, u8 attr_id)
1325 {
1326         u8 *target_attr;
1327         u32 target_attr_len;
1328         uint ielen = ielen_ori;
1329
1330         while (1) {
1331                 target_attr = rtw_get_p2p_attr(ie, ielen, attr_id, NULL, &target_attr_len);
1332                 if (target_attr && target_attr_len) {
1333                         u8 *next_attr = target_attr+target_attr_len;
1334                         uint remain_len = ielen-(next_attr-ie);
1335
1336                         _rtw_memset(target_attr, 0, target_attr_len);
1337                         memcpy(target_attr, next_attr, remain_len);
1338                         _rtw_memset(target_attr+remain_len, 0, target_attr_len);
1339                         *(ie+1) -= target_attr_len;
1340                         ielen -= target_attr_len;
1341                 } else {
1342                         break;
1343                 }
1344         }
1345         return ielen;
1346 }
1347
1348 void rtw_wlan_bssid_ex_remove_p2p_attr(struct wlan_bssid_ex *bss_ex, u8 attr_id)
1349 {
1350         u8 *p2p_ie;
1351         uint p2p_ielen, p2p_ielen_ori;
1352
1353         p2p_ie = rtw_get_p2p_ie(bss_ex->IEs+_FIXED_IE_LENGTH_, bss_ex->IELength-_FIXED_IE_LENGTH_, NULL, &p2p_ielen_ori);
1354         if (p2p_ie) {
1355                 p2p_ielen = rtw_p2p_attr_remove(p2p_ie, p2p_ielen_ori, attr_id);
1356                 if (p2p_ielen != p2p_ielen_ori) {
1357                         u8 *next_ie_ori = p2p_ie+p2p_ielen_ori;
1358                         u8 *next_ie = p2p_ie+p2p_ielen;
1359                         uint remain_len = bss_ex->IELength-(next_ie_ori-bss_ex->IEs);
1360
1361                         memcpy(next_ie, next_ie_ori, remain_len);
1362                         _rtw_memset(next_ie+remain_len, 0, p2p_ielen_ori-p2p_ielen);
1363                         bss_ex->IELength -= p2p_ielen_ori-p2p_ielen;
1364                 }
1365         }
1366 }
1367
1368 #endif /* CONFIG_88EU_P2P */
1369
1370 /* Baron adds to avoid FreeBSD warning */
1371 int ieee80211_is_empty_essid(const char *essid, int essid_len)
1372 {
1373         /* Single white space is for Linksys APs */
1374         if (essid_len == 1 && essid[0] == ' ')
1375                 return 1;
1376
1377         /* Otherwise, if the entire essid is 0, we assume it is hidden */
1378         while (essid_len) {
1379                 essid_len--;
1380                 if (essid[essid_len] != '\0')
1381                         return 0;
1382         }
1383
1384         return 1;
1385 }
1386
1387 int ieee80211_get_hdrlen(u16 fc)
1388 {
1389         int hdrlen = 24;
1390
1391         switch (WLAN_FC_GET_TYPE(fc)) {
1392         case RTW_IEEE80211_FTYPE_DATA:
1393                 if (fc & RTW_IEEE80211_STYPE_QOS_DATA)
1394                         hdrlen += 2;
1395                 if ((fc & RTW_IEEE80211_FCTL_FROMDS) && (fc & RTW_IEEE80211_FCTL_TODS))
1396                         hdrlen += 6; /* Addr4 */
1397                 break;
1398         case RTW_IEEE80211_FTYPE_CTL:
1399                 switch (WLAN_FC_GET_STYPE(fc)) {
1400                 case RTW_IEEE80211_STYPE_CTS:
1401                 case RTW_IEEE80211_STYPE_ACK:
1402                         hdrlen = 10;
1403                         break;
1404                 default:
1405                         hdrlen = 16;
1406                         break;
1407                 }
1408                 break;
1409         }
1410
1411         return hdrlen;
1412 }
1413
1414 static int rtw_get_cipher_info(struct wlan_network *pnetwork)
1415 {
1416         u32 wpa_ielen;
1417         unsigned char *pbuf;
1418         int group_cipher = 0, pairwise_cipher = 0, is8021x = 0;
1419         int ret = _FAIL;
1420         pbuf = rtw_get_wpa_ie(&pnetwork->network.IEs[12], &wpa_ielen, pnetwork->network.IELength-12);
1421
1422         if (pbuf && (wpa_ielen > 0)) {
1423                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_cipher_info: wpa_ielen: %d", wpa_ielen));
1424                 if (_SUCCESS == rtw_parse_wpa_ie(pbuf, wpa_ielen+2, &group_cipher, &pairwise_cipher, &is8021x)) {
1425                         pnetwork->BcnInfo.pairwise_cipher = pairwise_cipher;
1426                         pnetwork->BcnInfo.group_cipher = group_cipher;
1427                         pnetwork->BcnInfo.is_8021x = is8021x;
1428                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("%s: pnetwork->pairwise_cipher: %d, is_8021x is %d",
1429                                  __func__, pnetwork->BcnInfo.pairwise_cipher, pnetwork->BcnInfo.is_8021x));
1430                         ret = _SUCCESS;
1431                 }
1432         } else {
1433                 pbuf = rtw_get_wpa2_ie(&pnetwork->network.IEs[12], &wpa_ielen, pnetwork->network.IELength-12);
1434
1435                 if (pbuf && (wpa_ielen > 0)) {
1436                         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("get RSN IE\n"));
1437                         if (_SUCCESS == rtw_parse_wpa2_ie(pbuf, wpa_ielen+2, &group_cipher, &pairwise_cipher, &is8021x)) {
1438                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("get RSN IE  OK!!!\n"));
1439                                 pnetwork->BcnInfo.pairwise_cipher = pairwise_cipher;
1440                                 pnetwork->BcnInfo.group_cipher = group_cipher;
1441                                 pnetwork->BcnInfo.is_8021x = is8021x;
1442                                 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("%s: pnetwork->pairwise_cipher: %d,"
1443                                                         "pnetwork->group_cipher is %d, is_8021x is %d", __func__, pnetwork->BcnInfo.pairwise_cipher,
1444                                                         pnetwork->BcnInfo.group_cipher, pnetwork->BcnInfo.is_8021x));
1445                                 ret = _SUCCESS;
1446                         }
1447                 }
1448         }
1449
1450         return ret;
1451 }
1452
1453 void rtw_get_bcn_info(struct wlan_network *pnetwork)
1454 {
1455         unsigned short cap = 0;
1456         u8 bencrypt = 0;
1457         __le16 le_tmp;
1458         u16 wpa_len = 0, rsn_len = 0;
1459         struct HT_info_element *pht_info = NULL;
1460         struct rtw_ieee80211_ht_cap *pht_cap = NULL;
1461         unsigned int            len;
1462         unsigned char           *p;
1463
1464         memcpy(&le_tmp, rtw_get_capability_from_ie(pnetwork->network.IEs), 2);
1465         cap = le16_to_cpu(le_tmp);
1466         if (cap & WLAN_CAPABILITY_PRIVACY) {
1467                 bencrypt = 1;
1468                 pnetwork->network.Privacy = 1;
1469         } else {
1470                 pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_OPENSYS;
1471         }
1472         rtw_get_sec_ie(pnetwork->network.IEs , pnetwork->network.IELength, NULL, &rsn_len, NULL, &wpa_len);
1473         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: ssid =%s\n", pnetwork->network.Ssid.Ssid));
1474         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: wpa_len =%d rsn_len =%d\n", wpa_len, rsn_len));
1475         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: ssid =%s\n", pnetwork->network.Ssid.Ssid));
1476         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: wpa_len =%d rsn_len =%d\n", wpa_len, rsn_len));
1477
1478         if (rsn_len > 0) {
1479                 pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WPA2;
1480         } else if (wpa_len > 0) {
1481                 pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WPA;
1482         } else {
1483                 if (bencrypt)
1484                         pnetwork->BcnInfo.encryp_protocol = ENCRYP_PROTOCOL_WEP;
1485         }
1486         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: pnetwork->encryp_protocol is %x\n",
1487                  pnetwork->BcnInfo.encryp_protocol));
1488         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("rtw_get_bcn_info: pnetwork->encryp_protocol is %x\n",
1489                  pnetwork->BcnInfo.encryp_protocol));
1490         rtw_get_cipher_info(pnetwork);
1491
1492         /* get bwmode and ch_offset */
1493         /* parsing HT_CAP_IE */
1494         p = rtw_get_ie(pnetwork->network.IEs + _FIXED_IE_LENGTH_, _HT_CAPABILITY_IE_, &len, pnetwork->network.IELength - _FIXED_IE_LENGTH_);
1495         if (p && len > 0) {
1496                         pht_cap = (struct rtw_ieee80211_ht_cap *)(p + 2);
1497                         pnetwork->BcnInfo.ht_cap_info = pht_cap->cap_info;
1498         } else {
1499                         pnetwork->BcnInfo.ht_cap_info = 0;
1500         }
1501         /* parsing HT_INFO_IE */
1502         p = rtw_get_ie(pnetwork->network.IEs + _FIXED_IE_LENGTH_, _HT_ADD_INFO_IE_, &len, pnetwork->network.IELength - _FIXED_IE_LENGTH_);
1503         if (p && len > 0) {
1504                         pht_info = (struct HT_info_element *)(p + 2);
1505                         pnetwork->BcnInfo.ht_info_infos_0 = pht_info->infos[0];
1506         } else {
1507                         pnetwork->BcnInfo.ht_info_infos_0 = 0;
1508         }
1509 }
1510
1511 /* show MCS rate, unit: 100Kbps */
1512 u16 rtw_mcs_rate(u8 rf_type, u8 bw_40MHz, u8 short_GI_20, u8 short_GI_40, unsigned char *MCS_rate)
1513 {
1514         u16 max_rate = 0;
1515
1516         if (rf_type == RF_1T1R) {
1517                 if (MCS_rate[0] & BIT(7))
1518                         max_rate = (bw_40MHz) ? ((short_GI_40) ? 1500 : 1350) : ((short_GI_20) ? 722 : 650);
1519                 else if (MCS_rate[0] & BIT(6))
1520                         max_rate = (bw_40MHz) ? ((short_GI_40) ? 1350 : 1215) : ((short_GI_20) ? 650 : 585);
1521                 else if (MCS_rate[0] & BIT(5))
1522                         max_rate = (bw_40MHz) ? ((short_GI_40) ? 1200 : 1080) : ((short_GI_20) ? 578 : 520);
1523                 else if (MCS_rate[0] & BIT(4))
1524                         max_rate = (bw_40MHz) ? ((short_GI_40) ? 900 : 810) : ((short_GI_20) ? 433 : 390);
1525                 else if (MCS_rate[0] & BIT(3))
1526                         max_rate = (bw_40MHz) ? ((short_GI_40) ? 600 : 540) : ((short_GI_20) ? 289 : 260);
1527                 else if (MCS_rate[0] & BIT(2))
1528                         max_rate = (bw_40MHz) ? ((short_GI_40) ? 450 : 405) : ((short_GI_20) ? 217 : 195);
1529                 else if (MCS_rate[0] & BIT(1))
1530                         max_rate = (bw_40MHz) ? ((short_GI_40) ? 300 : 270) : ((short_GI_20) ? 144 : 130);
1531                 else if (MCS_rate[0] & BIT(0))
1532                         max_rate = (bw_40MHz) ? ((short_GI_40) ? 150 : 135) : ((short_GI_20) ? 72 : 65);
1533         } else {
1534                 if (MCS_rate[1]) {
1535                         if (MCS_rate[1] & BIT(7))
1536                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 3000 : 2700) : ((short_GI_20) ? 1444 : 1300);
1537                         else if (MCS_rate[1] & BIT(6))
1538                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 2700 : 2430) : ((short_GI_20) ? 1300 : 1170);
1539                         else if (MCS_rate[1] & BIT(5))
1540                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 2400 : 2160) : ((short_GI_20) ? 1156 : 1040);
1541                         else if (MCS_rate[1] & BIT(4))
1542                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 1800 : 1620) : ((short_GI_20) ? 867 : 780);
1543                         else if (MCS_rate[1] & BIT(3))
1544                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 1200 : 1080) : ((short_GI_20) ? 578 : 520);
1545                         else if (MCS_rate[1] & BIT(2))
1546                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 900 : 810) : ((short_GI_20) ? 433 : 390);
1547                         else if (MCS_rate[1] & BIT(1))
1548                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 600 : 540) : ((short_GI_20) ? 289 : 260);
1549                         else if (MCS_rate[1] & BIT(0))
1550                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 300 : 270) : ((short_GI_20) ? 144 : 130);
1551                 } else {
1552                         if (MCS_rate[0] & BIT(7))
1553                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 1500 : 1350) : ((short_GI_20) ? 722 : 650);
1554                         else if (MCS_rate[0] & BIT(6))
1555                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 1350 : 1215) : ((short_GI_20) ? 650 : 585);
1556                         else if (MCS_rate[0] & BIT(5))
1557                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 1200 : 1080) : ((short_GI_20) ? 578 : 520);
1558                         else if (MCS_rate[0] & BIT(4))
1559                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 900 : 810) : ((short_GI_20) ? 433 : 390);
1560                         else if (MCS_rate[0] & BIT(3))
1561                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 600 : 540) : ((short_GI_20) ? 289 : 260);
1562                         else if (MCS_rate[0] & BIT(2))
1563                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 450 : 405) : ((short_GI_20) ? 217 : 195);
1564                         else if (MCS_rate[0] & BIT(1))
1565                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 300 : 270) : ((short_GI_20) ? 144 : 130);
1566                         else if (MCS_rate[0] & BIT(0))
1567                                 max_rate = (bw_40MHz) ? ((short_GI_40) ? 150 : 135) : ((short_GI_20) ? 72 : 65);
1568                 }
1569         }
1570         return max_rate;
1571 }
1572
1573 int rtw_action_frame_parse(const u8 *frame, u32 frame_len, u8 *category, u8 *action)
1574 {
1575         const u8 *frame_body = frame + sizeof(struct rtw_ieee80211_hdr_3addr);
1576         u16 fc;
1577         u8 c, a = 0;
1578
1579         fc = le16_to_cpu(((struct rtw_ieee80211_hdr_3addr *)frame)->frame_ctl);
1580
1581         if ((fc & (RTW_IEEE80211_FCTL_FTYPE|RTW_IEEE80211_FCTL_STYPE)) !=
1582             (RTW_IEEE80211_FTYPE_MGMT|RTW_IEEE80211_STYPE_ACTION))
1583                 return false;
1584
1585         c = frame_body[0];
1586
1587         switch (c) {
1588         case RTW_WLAN_CATEGORY_P2P: /* vendor-specific */
1589                 break;
1590         default:
1591                 a = frame_body[1];
1592         }
1593
1594         if (category)
1595                 *category = c;
1596         if (action)
1597                 *action = a;
1598
1599         return true;
1600 }
1601
1602 static const char *_action_public_str[] = {
1603         "ACT_PUB_BSSCOEXIST",
1604         "ACT_PUB_DSE_ENABLE",
1605         "ACT_PUB_DSE_DEENABLE",
1606         "ACT_PUB_DSE_REG_LOCATION",
1607         "ACT_PUB_EXT_CHL_SWITCH",
1608         "ACT_PUB_DSE_MSR_REQ",
1609         "ACT_PUB_DSE_MSR_RPRT",
1610         "ACT_PUB_MP",
1611         "ACT_PUB_DSE_PWR_CONSTRAINT",
1612         "ACT_PUB_VENDOR",
1613         "ACT_PUB_GAS_INITIAL_REQ",
1614         "ACT_PUB_GAS_INITIAL_RSP",
1615         "ACT_PUB_GAS_COMEBACK_REQ",
1616         "ACT_PUB_GAS_COMEBACK_RSP",
1617         "ACT_PUB_TDLS_DISCOVERY_RSP",
1618         "ACT_PUB_LOCATION_TRACK",
1619         "ACT_PUB_RSVD",
1620 };
1621
1622 const char *action_public_str(u8 action)
1623 {
1624         action = (action >= ACT_PUBLIC_MAX) ? ACT_PUBLIC_MAX : action;
1625         return _action_public_str[action];
1626 }