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