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