Linux Audio

Check our new training course

Loading...
v4.17
  1/******************************************************************************
  2 * ieee80211.c
  3 *
  4 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
  5 * Linux device driver for RTL8192SU
  6 *
  7 * This program is free software; you can redistribute it and/or modify it
  8 * under the terms of version 2 of the GNU General Public License as
  9 * published by the Free Software Foundation.
 10 *
 11 * This program is distributed in the hope that it will be useful, but WITHOUT
 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 14 * more details.
 15 *
 
 
 
 
 16 * Modifications for inclusion into the Linux staging tree are
 17 * Copyright(c) 2010 Larry Finger. All rights reserved.
 18 *
 19 * Contact information:
 20 * WLAN FAE <wlanfae@realtek.com>.
 21 * Larry Finger <Larry.Finger@lwfinger.net>
 22 *
 23 ******************************************************************************/
 24
 25#define _IEEE80211_C
 26
 27#include "drv_types.h"
 28#include "ieee80211.h"
 29#include "wifi.h"
 30#include "osdep_service.h"
 31#include "wlan_bssdef.h"
 32
 33static const u8 WPA_OUI_TYPE[] = {0x00, 0x50, 0xf2, 1};
 34static const u8 WPA_CIPHER_SUITE_NONE[] = {0x00, 0x50, 0xf2, 0};
 35static const u8 WPA_CIPHER_SUITE_WEP40[] = {0x00, 0x50, 0xf2, 1};
 36static const u8 WPA_CIPHER_SUITE_TKIP[] = {0x00, 0x50, 0xf2, 2};
 37static const u8 WPA_CIPHER_SUITE_CCMP[] = {0x00, 0x50, 0xf2, 4};
 38static const u8 WPA_CIPHER_SUITE_WEP104[] = {0x00, 0x50, 0xf2, 5};
 39
 40static const u8 RSN_CIPHER_SUITE_NONE[] = {0x00, 0x0f, 0xac, 0};
 41static const u8 RSN_CIPHER_SUITE_WEP40[] = {0x00, 0x0f, 0xac, 1};
 42static const u8 RSN_CIPHER_SUITE_TKIP[] = {0x00, 0x0f, 0xac, 2};
 43static const u8 RSN_CIPHER_SUITE_CCMP[] = {0x00, 0x0f, 0xac, 4};
 44static const u8 RSN_CIPHER_SUITE_WEP104[] = {0x00, 0x0f, 0xac, 5};
 45
 46/*-----------------------------------------------------------
 47 * for adhoc-master to generate ie and provide supported-rate to fw
 48 *-----------------------------------------------------------
 49 */
 50
 51static u8 WIFI_CCKRATES[] =  {
 52	(IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK),
 53	(IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK),
 54	(IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK),
 55	(IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK)
 56};
 57
 58static u8 WIFI_OFDMRATES[] = {
 59	(IEEE80211_OFDM_RATE_6MB),
 60	(IEEE80211_OFDM_RATE_9MB),
 61	(IEEE80211_OFDM_RATE_12MB),
 62	(IEEE80211_OFDM_RATE_18MB),
 63	(IEEE80211_OFDM_RATE_24MB),
 64	(IEEE80211_OFDM_RATE_36MB),
 65	(IEEE80211_OFDM_RATE_48MB),
 66	(IEEE80211_OFDM_RATE_54MB)
 67};
 68
 69uint r8712_is_cckrates_included(u8 *rate)
 70{
 71	u32 i = 0;
 72
 73	while (rate[i] != 0) {
 74		if ((((rate[i]) & 0x7f) == 2) || (((rate[i]) & 0x7f) == 4) ||
 75		    (((rate[i]) & 0x7f) == 11) || (((rate[i]) & 0x7f) == 22))
 76			return true;
 77		i++;
 78	}
 79	return false;
 80}
 81
 82uint r8712_is_cckratesonly_included(u8 *rate)
 83{
 84	u32 i = 0;
 85
 86	while (rate[i] != 0) {
 87		if ((((rate[i]) & 0x7f) != 2) && (((rate[i]) & 0x7f) != 4) &&
 88		    (((rate[i]) & 0x7f) != 11)  && (((rate[i]) & 0x7f) != 22))
 89			return false;
 90		i++;
 91	}
 92	return true;
 93}
 94
 95/* r8712_set_ie will update frame length */
 96u8 *r8712_set_ie(u8 *pbuf, sint index, uint len, u8 *source, uint *frlen)
 97{
 98	*pbuf = (u8)index;
 99	*(pbuf + 1) = (u8)len;
100	if (len > 0)
101		memcpy((void *)(pbuf + 2), (void *)source, len);
102	*frlen = *frlen + (len + 2);
103	return pbuf + len + 2;
104}
105
106/* ---------------------------------------------------------------------------
107 * index: the information element id index, limit is the limit for search
108 * ---------------------------------------------------------------------------
109 */
110u8 *r8712_get_ie(u8 *pbuf, sint index, uint *len, sint limit)
111{
112	sint tmp, i;
113	u8 *p;
114
115	if (limit < 1)
116		return NULL;
117	p = pbuf;
118	i = 0;
119	*len = 0;
120	while (1) {
121		if (*p == index) {
122			*len = *(p + 1);
123			return p;
 
 
 
 
124		}
125		tmp = *(p + 1);
126		p += (tmp + 2);
127		i += (tmp + 2);
128		if (i >= limit)
129			break;
130	}
131	return NULL;
132}
133
134static void set_supported_rate(u8 *rates, uint mode)
135{
136	memset(rates, 0, NDIS_802_11_LENGTH_RATES_EX);
137	switch (mode) {
138	case WIRELESS_11B:
139		memcpy(rates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
 
140		break;
141	case WIRELESS_11G:
142	case WIRELESS_11A:
143		memcpy(rates, WIFI_OFDMRATES, IEEE80211_NUM_OFDM_RATESLEN);
 
144		break;
145	case WIRELESS_11BG:
146		memcpy(rates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
147		memcpy(rates + IEEE80211_CCK_RATE_LEN, WIFI_OFDMRATES,
148		       IEEE80211_NUM_OFDM_RATESLEN);
149		break;
150	}
151}
152
153static uint r8712_get_rateset_len(u8 *rateset)
154{
155	uint i = 0;
156
157	while (1) {
158		if ((rateset[i]) == 0)
159			break;
160		if (i > 12)
161			break;
162		i++;
163	}
164	return i;
165}
166
167int r8712_generate_ie(struct registry_priv *pregistrypriv)
 
168{
169	int rate_len;
170	uint sz = 0;
171	struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
172	u8 *ie = pdev_network->IEs;
173	u16 beaconPeriod = (u16)pdev_network->Configuration.BeaconPeriod;
 
 
 
 
174
175	/*timestamp will be inserted by hardware*/
176	sz += 8;
177	ie += sz;
178	/*beacon interval : 2bytes*/
179	*(__le16 *)ie = cpu_to_le16(beaconPeriod);
180	sz += 2;
181	ie += 2;
182	/*capability info*/
183	*(u16 *)ie = 0;
184	*(__le16 *)ie |= cpu_to_le16(cap_IBSS);
185	if (pregistrypriv->preamble == PREAMBLE_SHORT)
186		*(__le16 *)ie |= cpu_to_le16(cap_ShortPremble);
187	if (pdev_network->Privacy)
188		*(__le16 *)ie |= cpu_to_le16(cap_Privacy);
189	sz += 2;
190	ie += 2;
191	/*SSID*/
192	ie = r8712_set_ie(ie, _SSID_IE_, pdev_network->Ssid.SsidLength,
193			  pdev_network->Ssid.Ssid, &sz);
194	/*supported rates*/
195	set_supported_rate(pdev_network->rates, pregistrypriv->wireless_mode);
196	rate_len = r8712_get_rateset_len(pdev_network->rates);
197	if (rate_len > 8) {
 
198		ie = r8712_set_ie(ie, _SUPPORTEDRATES_IE_, 8,
199				  pdev_network->rates, &sz);
200		ie = r8712_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rate_len - 8),
201				  (pdev_network->rates + 8), &sz);
202	} else {
203		ie = r8712_set_ie(ie, _SUPPORTEDRATES_IE_,
204				  rate_len, pdev_network->rates, &sz);
205	}
206	/*DS parameter set*/
207	ie = r8712_set_ie(ie, _DSSET_IE_, 1,
208			  (u8 *)&pdev_network->Configuration.DSConfig, &sz);
209	/*IBSS Parameter Set*/
210	ie = r8712_set_ie(ie, _IBSS_PARA_IE_, 2,
211			  (u8 *)&pdev_network->Configuration.ATIMWindow, &sz);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212	return sz;
213}
214
215unsigned char *r8712_get_wpa_ie(unsigned char *pie, uint *wpa_ie_len, int limit)
216{
217	u32 len;
218	u16 val16;
219	unsigned char wpa_oui_type[] = {0x00, 0x50, 0xf2, 0x01};
220	u8 *pbuf = pie;
221
222	while (1) {
223		pbuf = r8712_get_ie(pbuf, _WPA_IE_ID_, &len, limit);
224		if (pbuf) {
225			/*check if oui matches...*/
226			if (memcmp((pbuf + 2), wpa_oui_type,
227				   sizeof(wpa_oui_type)))
228				goto check_next_ie;
229			/*check version...*/
230			memcpy((u8 *)&val16, (pbuf + 6), sizeof(val16));
231			le16_to_cpus(&val16);
232			if (val16 != 0x0001)
233				goto check_next_ie;
234			*wpa_ie_len = *(pbuf + 1);
235			return pbuf;
 
 
 
236		}
237		*wpa_ie_len = 0;
238		return NULL;
239check_next_ie:
240		limit = limit - (pbuf - pie) - 2 - len;
241		if (limit <= 0)
242			break;
243		pbuf += (2 + len);
244	}
245	*wpa_ie_len = 0;
246	return NULL;
247}
248
249unsigned char *r8712_get_wpa2_ie(unsigned char *pie, uint *rsn_ie_len, int limit)
250{
251	return r8712_get_ie(pie, _WPA2_IE_ID_, rsn_ie_len, limit);
252}
253
254static int r8712_get_wpa_cipher_suite(u8 *s)
255{
256	if (!memcmp(s, (void *)WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN))
257		return WPA_CIPHER_NONE;
258	if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN))
259		return WPA_CIPHER_WEP40;
260	if (!memcmp(s, (void *)WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN))
261		return WPA_CIPHER_TKIP;
262	if (!memcmp(s, (void *)WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN))
263		return WPA_CIPHER_CCMP;
264	if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN))
265		return WPA_CIPHER_WEP104;
266	return 0;
267}
268
269static int r8712_get_wpa2_cipher_suite(u8 *s)
270{
271	if (!memcmp(s, (void *)RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN))
272		return WPA_CIPHER_NONE;
273	if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN))
274		return WPA_CIPHER_WEP40;
275	if (!memcmp(s, (void *)RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN))
276		return WPA_CIPHER_TKIP;
277	if (!memcmp(s, (void *)RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN))
278		return WPA_CIPHER_CCMP;
279	if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN))
280		return WPA_CIPHER_WEP104;
281	return 0;
282}
283
284int r8712_parse_wpa_ie(u8 *wpa_ie, int wpa_ie_len, int *group_cipher,
285		       int *pairwise_cipher)
286{
287	int i;
288	int left, count;
289	u8 *pos;
290
291	if (wpa_ie_len <= 0) {
292		/* No WPA IE - fail silently */
293		return _FAIL;
294	}
295	if ((*wpa_ie != _WPA_IE_ID_) ||
296	    (*(wpa_ie + 1) != (u8)(wpa_ie_len - 2)) ||
297	    (memcmp(wpa_ie + 2, (void *)WPA_OUI_TYPE, WPA_SELECTOR_LEN)))
298		return _FAIL;
299	pos = wpa_ie;
300	pos += 8;
301	left = wpa_ie_len - 8;
302	/*group_cipher*/
303	if (left >= WPA_SELECTOR_LEN) {
304		*group_cipher = r8712_get_wpa_cipher_suite(pos);
305		pos += WPA_SELECTOR_LEN;
306		left -= WPA_SELECTOR_LEN;
307	} else if (left > 0) {
308		return _FAIL;
309	}
310	/*pairwise_cipher*/
311	if (left >= 2) {
312		count = le16_to_cpu(*(__le16 *)pos);
313		pos += 2;
314		left -= 2;
315		if (count == 0 || left < count * WPA_SELECTOR_LEN)
316			return _FAIL;
317		for (i = 0; i < count; i++) {
318			*pairwise_cipher |= r8712_get_wpa_cipher_suite(pos);
319			pos += WPA_SELECTOR_LEN;
320			left -= WPA_SELECTOR_LEN;
321		}
322	} else if (left == 1) {
323		return _FAIL;
324	}
325	return _SUCCESS;
326}
327
328int r8712_parse_wpa2_ie(u8 *rsn_ie, int rsn_ie_len, int *group_cipher,
329			int *pairwise_cipher)
330{
331	int i;
332	int left, count;
333	u8 *pos;
334
335	if (rsn_ie_len <= 0) {
336		/* No RSN IE - fail silently */
337		return _FAIL;
338	}
339	if ((*rsn_ie != _WPA2_IE_ID_) ||
340	    (*(rsn_ie + 1) != (u8)(rsn_ie_len - 2)))
341		return _FAIL;
342	pos = rsn_ie;
343	pos += 4;
344	left = rsn_ie_len - 4;
345	/*group_cipher*/
346	if (left >= RSN_SELECTOR_LEN) {
347		*group_cipher = r8712_get_wpa2_cipher_suite(pos);
348		pos += RSN_SELECTOR_LEN;
349		left -= RSN_SELECTOR_LEN;
350	} else if (left > 0) {
351		return _FAIL;
352	}
353	/*pairwise_cipher*/
354	if (left >= 2) {
355		count = le16_to_cpu(*(__le16 *)pos);
356		pos += 2;
357		left -= 2;
358		if (count == 0 || left < count * RSN_SELECTOR_LEN)
359			return _FAIL;
360		for (i = 0; i < count; i++) {
361			*pairwise_cipher |= r8712_get_wpa2_cipher_suite(pos);
362			pos += RSN_SELECTOR_LEN;
363			left -= RSN_SELECTOR_LEN;
364		}
365	} else if (left == 1) {
366		return _FAIL;
367	}
368	return _SUCCESS;
369}
370
371int r8712_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len,
372		     u8 *wpa_ie, u16 *wpa_len)
373{
374	u8 authmode;
375	u8 wpa_oui[4] = {0x0, 0x50, 0xf2, 0x01};
376	uint cnt;
377
378	/*Search required WPA or WPA2 IE and copy to sec_ie[ ]*/
379	cnt = _TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_;
 
380	while (cnt < in_len) {
381		authmode = in_ie[cnt];
382		if ((authmode == _WPA_IE_ID_) &&
383		    (!memcmp(&in_ie[cnt + 2], &wpa_oui[0], 4))) {
384			memcpy(wpa_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
385			*wpa_len = in_ie[cnt + 1] + 2;
386			cnt += in_ie[cnt + 1] + 2;  /*get next */
387		} else {
388			if (authmode == _WPA2_IE_ID_) {
389				memcpy(rsn_ie, &in_ie[cnt],
390				       in_ie[cnt + 1] + 2);
391				*rsn_len = in_ie[cnt + 1] + 2;
392				cnt += in_ie[cnt + 1] + 2;  /*get next*/
393			} else {
394				cnt += in_ie[cnt + 1] + 2;   /*get next*/
395			}
396		}
397	}
398	return *rsn_len + *wpa_len;
399}
400
401int r8712_get_wps_ie(u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen)
402{
403	int match;
404	uint cnt;
405	u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
406
407	cnt = 12;
408	match = false;
409	while (cnt < in_len) {
410		eid = in_ie[cnt];
411		if ((eid == _WPA_IE_ID_) &&
412		    (!memcmp(&in_ie[cnt + 2], wps_oui, 4))) {
413			memcpy(wps_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
414			*wps_ielen = in_ie[cnt + 1] + 2;
415			cnt += in_ie[cnt + 1] + 2;
416			match = true;
417			break;
418		}
419			cnt += in_ie[cnt + 1] + 2; /* goto next */
420	}
421	return match;
422}
v3.1
  1/******************************************************************************
  2 * ieee80211.c
  3 *
  4 * Copyright(c) 2007 - 2010 Realtek Corporation. All rights reserved.
  5 * Linux device driver for RTL8192SU
  6 *
  7 * This program is free software; you can redistribute it and/or modify it
  8 * under the terms of version 2 of the GNU General Public License as
  9 * published by the Free Software Foundation.
 10 *
 11 * This program is distributed in the hope that it will be useful, but WITHOUT
 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 14 * more details.
 15 *
 16 * You should have received a copy of the GNU General Public License along with
 17 * this program; if not, write to the Free Software Foundation, Inc.,
 18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 19 *
 20 * Modifications for inclusion into the Linux staging tree are
 21 * Copyright(c) 2010 Larry Finger. All rights reserved.
 22 *
 23 * Contact information:
 24 * WLAN FAE <wlanfae@realtek.com>.
 25 * Larry Finger <Larry.Finger@lwfinger.net>
 26 *
 27 ******************************************************************************/
 28
 29#define _IEEE80211_C
 30
 31#include "drv_types.h"
 32#include "ieee80211.h"
 33#include "wifi.h"
 34#include "osdep_service.h"
 35#include "wlan_bssdef.h"
 36
 37static const u8 WPA_OUI_TYPE[] = {0x00, 0x50, 0xf2, 1};
 38static const u8 WPA_CIPHER_SUITE_NONE[] = {0x00, 0x50, 0xf2, 0};
 39static const u8 WPA_CIPHER_SUITE_WEP40[] = {0x00, 0x50, 0xf2, 1};
 40static const u8 WPA_CIPHER_SUITE_TKIP[] = {0x00, 0x50, 0xf2, 2};
 41static const u8 WPA_CIPHER_SUITE_CCMP[] = {0x00, 0x50, 0xf2, 4};
 42static const u8 WPA_CIPHER_SUITE_WEP104[] = {0x00, 0x50, 0xf2, 5};
 43
 44static const u8 RSN_CIPHER_SUITE_NONE[] = {0x00, 0x0f, 0xac, 0};
 45static const u8 RSN_CIPHER_SUITE_WEP40[] = {0x00, 0x0f, 0xac, 1};
 46static const u8 RSN_CIPHER_SUITE_TKIP[] = {0x00, 0x0f, 0xac, 2};
 47static const u8 RSN_CIPHER_SUITE_CCMP[] = {0x00, 0x0f, 0xac, 4};
 48static const u8 RSN_CIPHER_SUITE_WEP104[] = {0x00, 0x0f, 0xac, 5};
 49
 50/*-----------------------------------------------------------
 51 * for adhoc-master to generate ie and provide supported-rate to fw
 52 *-----------------------------------------------------------
 53 */
 54
 55static u8 WIFI_CCKRATES[] =  {
 56	(IEEE80211_CCK_RATE_1MB | IEEE80211_BASIC_RATE_MASK),
 57	(IEEE80211_CCK_RATE_2MB | IEEE80211_BASIC_RATE_MASK),
 58	(IEEE80211_CCK_RATE_5MB | IEEE80211_BASIC_RATE_MASK),
 59	(IEEE80211_CCK_RATE_11MB | IEEE80211_BASIC_RATE_MASK)
 60};
 61
 62static u8 WIFI_OFDMRATES[] = {
 63	(IEEE80211_OFDM_RATE_6MB),
 64	(IEEE80211_OFDM_RATE_9MB),
 65	(IEEE80211_OFDM_RATE_12MB),
 66	(IEEE80211_OFDM_RATE_18MB),
 67	(IEEE80211_OFDM_RATE_24MB),
 68	(IEEE80211_OFDM_RATE_36MB),
 69	(IEEE80211_OFDM_RATE_48MB),
 70	(IEEE80211_OFDM_RATE_54MB)
 71};
 72
 73uint r8712_is_cckrates_included(u8 *rate)
 74{
 75	u32 i = 0;
 76
 77	while (rate[i] != 0) {
 78		if ((((rate[i]) & 0x7f) == 2) || (((rate[i]) & 0x7f) == 4) ||
 79		    (((rate[i]) & 0x7f) == 11) || (((rate[i]) & 0x7f) == 22))
 80			return true;
 81			i++;
 82		}
 83		return false;
 84}
 85
 86uint r8712_is_cckratesonly_included(u8 *rate)
 87{
 88	u32 i = 0;
 89
 90	while (rate[i] != 0) {
 91		if ((((rate[i]) & 0x7f) != 2) && (((rate[i]) & 0x7f) != 4) &&
 92		    (((rate[i]) & 0x7f) != 11)  && (((rate[i]) & 0x7f) != 22))
 93			return false;
 94		i++;
 95	}
 96	return true;
 97}
 98
 99/* r8712_set_ie will update frame length */
100u8 *r8712_set_ie(u8 *pbuf, sint index, uint len, u8 *source, uint *frlen)
101{
102	*pbuf = (u8)index;
103	*(pbuf + 1) = (u8)len;
104	if (len > 0)
105		memcpy((void *)(pbuf + 2), (void *)source, len);
106	*frlen = *frlen + (len + 2);
107	return pbuf + len + 2;
108}
109
110/*----------------------------------------------------------------------------
111index: the information element id index, limit is the limit for search
112-----------------------------------------------------------------------------*/
113u8 *r8712_get_ie(u8 *pbuf, sint index, sint *len, sint limit)
 
114{
115	sint tmp, i;
116	u8 *p;
117
118	if (limit < 1)
119		return NULL;
120	p = pbuf;
121	i = 0;
122	*len = 0;
123	while (1) {
124		if (*p == index) {
125			*len = *(p + 1);
126			return p;
127		} else {
128			tmp = *(p + 1);
129			p += (tmp + 2);
130			i += (tmp + 2);
131		}
 
 
 
132		if (i >= limit)
133			break;
134	}
135	return NULL;
136}
137
138static void set_supported_rate(u8 *SupportedRates, uint mode)
139{
140	memset(SupportedRates, 0, NDIS_802_11_LENGTH_RATES_EX);
141	switch (mode) {
142	case WIRELESS_11B:
143		memcpy(SupportedRates, WIFI_CCKRATES,
144			IEEE80211_CCK_RATE_LEN);
145		break;
146	case WIRELESS_11G:
147	case WIRELESS_11A:
148		memcpy(SupportedRates, WIFI_OFDMRATES,
149			IEEE80211_NUM_OFDM_RATESLEN);
150		break;
151	case WIRELESS_11BG:
152		memcpy(SupportedRates, WIFI_CCKRATES, IEEE80211_CCK_RATE_LEN);
153		memcpy(SupportedRates + IEEE80211_CCK_RATE_LEN, WIFI_OFDMRATES,
154			IEEE80211_NUM_OFDM_RATESLEN);
155		break;
156	}
157}
158
159static uint r8712_get_rateset_len(u8 *rateset)
160{
161	uint i = 0;
162
163	while (1) {
164		if ((rateset[i]) == 0)
165			break;
166		if (i > 12)
167			break;
168		i++;
169	}
170	return i;
171}
172
173int r8712_generate_ie(struct registry_priv *pregistrypriv,
174		      struct _adapter *padapter)
175{
176	int sz = 0, rateLen;
 
177	struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
178	u8 *ie = pdev_network->IEs;
179	struct ieee80211_ht_cap ht_capie;
180	struct ieee80211_ht_addt_info ht_addt_info;
181	unsigned char WMM_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01, 0x00};
182	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
183	struct qos_priv *pqospriv = &pmlmepriv->qospriv;
184
185	/*timestamp will be inserted by hardware*/
186	sz += 8;
187	ie += sz;
188	/*beacon interval : 2bytes*/
189	*(u16 *)ie = cpu_to_le16((u16)pdev_network->Configuration.BeaconPeriod);
190	sz += 2;
191	ie += 2;
192	/*capability info*/
193	*(u16 *)ie = 0;
194	*(u16 *)ie |= cpu_to_le16(cap_IBSS);
195	if (pregistrypriv->preamble == PREAMBLE_SHORT)
196		*(u16 *)ie |= cpu_to_le16(cap_ShortPremble);
197	if (pdev_network->Privacy)
198		*(u16 *)ie |= cpu_to_le16(cap_Privacy);
199	sz += 2;
200	ie += 2;
201	/*SSID*/
202	ie = r8712_set_ie(ie, _SSID_IE_, pdev_network->Ssid.SsidLength,
203		    pdev_network->Ssid.Ssid, &sz);
204	/*supported rates*/
205	set_supported_rate(pdev_network->SupportedRates,
206			   pregistrypriv->wireless_mode);
207	rateLen = r8712_get_rateset_len(pdev_network->SupportedRates);
208	if (rateLen > 8) {
209		ie = r8712_set_ie(ie, _SUPPORTEDRATES_IE_, 8,
210			    pdev_network->SupportedRates, &sz);
211		ie = r8712_set_ie(ie, _EXT_SUPPORTEDRATES_IE_, (rateLen - 8),
212			    (pdev_network->SupportedRates + 8), &sz);
213	} else
214		ie = r8712_set_ie(ie, _SUPPORTEDRATES_IE_,
215			    rateLen, pdev_network->SupportedRates, &sz);
 
216	/*DS parameter set*/
217	ie = r8712_set_ie(ie, _DSSET_IE_, 1,
218		    (u8 *)&(pdev_network->Configuration.DSConfig), &sz);
219	/*IBSS Parameter Set*/
220	ie = r8712_set_ie(ie, _IBSS_PARA_IE_, 2,
221		    (u8 *)&(pdev_network->Configuration.ATIMWindow), &sz);
222	if (pregistrypriv->ht_enable == 1) {
223		if (pqospriv->qos_option == 0) {
224			ie = r8712_set_ie(ie, _VENDOR_SPECIFIC_IE_,
225					   _WMM_IE_Length_, WMM_IE, &sz);
226			pqospriv->qos_option = 1;
227		}
228		memset(&ht_capie, 0, sizeof(struct ieee80211_ht_cap));
229		ht_capie.cap_info = IEEE80211_HT_CAP_SUP_WIDTH |
230				    IEEE80211_HT_CAP_SGI_20 |
231				    IEEE80211_HT_CAP_SGI_40 |
232				    IEEE80211_HT_CAP_TX_STBC |
233				    IEEE80211_HT_CAP_MAX_AMSDU |
234				    IEEE80211_HT_CAP_DSSSCCK40;
235		ht_capie.ampdu_params_info = (IEEE80211_HT_CAP_AMPDU_FACTOR &
236				0x03) | (IEEE80211_HT_CAP_AMPDU_DENSITY & 0x00);
237		ie = r8712_set_ie(ie, _HT_CAPABILITY_IE_,
238			    sizeof(struct ieee80211_ht_cap),
239			    (unsigned char *)&ht_capie, &sz);
240		/*add HT info ie*/
241		memset(&ht_addt_info, 0,
242			sizeof(struct ieee80211_ht_addt_info));
243		/*need to add the HT additional IEs*/
244		ht_addt_info.control_chan = pregistrypriv->channel;
245		ie = r8712_set_ie(ie, _HT_ADD_INFO_IE_,
246			    sizeof(struct ieee80211_ht_addt_info),
247			    (unsigned char *)&ht_addt_info, &sz);
248	}
249	return sz;
250}
251
252unsigned char *r8712_get_wpa_ie(unsigned char *pie, int *wpa_ie_len, int limit)
253{
254	int len;
255	u16 val16;
256	unsigned char wpa_oui_type[] = {0x00, 0x50, 0xf2, 0x01};
257	u8 *pbuf = pie;
258
259	while (1) {
260		pbuf = r8712_get_ie(pbuf, _WPA_IE_ID_, &len, limit);
261		if (pbuf) {
262			/*check if oui matches...*/
263			if (memcmp((pbuf + 2), wpa_oui_type,
264			    sizeof(wpa_oui_type)))
265				goto check_next_ie;
266			/*check version...*/
267			memcpy((u8 *)&val16, (pbuf + 6), sizeof(val16));
268			val16 = le16_to_cpu(val16);
269			if (val16 != 0x0001)
270				goto check_next_ie;
271			*wpa_ie_len = *(pbuf + 1);
272			return pbuf;
273		} else {
274			*wpa_ie_len = 0;
275			return NULL;
276		}
 
 
277check_next_ie:
278		limit = limit - (pbuf - pie) - 2 - len;
279		if (limit <= 0)
280			break;
281		pbuf += (2 + len);
282	}
283	*wpa_ie_len = 0;
284	return NULL;
285}
286
287unsigned char *r8712_get_wpa2_ie(unsigned char *pie, int *rsn_ie_len, int limit)
288{
289	return r8712_get_ie(pie, _WPA2_IE_ID_, rsn_ie_len, limit);
290}
291
292static int r8712_get_wpa_cipher_suite(u8 *s)
293{
294	if (!memcmp(s, (void *)WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN))
295		return WPA_CIPHER_NONE;
296	if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN))
297		return WPA_CIPHER_WEP40;
298	if (!memcmp(s, (void *)WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN))
299		return WPA_CIPHER_TKIP;
300	if (!memcmp(s, (void *)WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN))
301		return WPA_CIPHER_CCMP;
302	if (!memcmp(s, (void *)WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN))
303		return WPA_CIPHER_WEP104;
304	return 0;
305}
306
307static int r8712_get_wpa2_cipher_suite(u8 *s)
308{
309	if (!memcmp(s, (void *)RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN))
310		return WPA_CIPHER_NONE;
311	if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN))
312		return WPA_CIPHER_WEP40;
313	if (!memcmp(s, (void *)RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN))
314		return WPA_CIPHER_TKIP;
315	if (!memcmp(s, (void *)RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN))
316		return WPA_CIPHER_CCMP;
317	if (!memcmp(s, (void *)RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN))
318		return WPA_CIPHER_WEP104;
319	return 0;
320}
321
322int r8712_parse_wpa_ie(u8 *wpa_ie, int wpa_ie_len, int *group_cipher,
323		 int *pairwise_cipher)
324{
325	int i, ret = _SUCCESS;
326	int left, count;
327	u8 *pos;
328
329	if (wpa_ie_len <= 0) {
330		/* No WPA IE - fail silently */
331		return _FAIL;
332	}
333	if ((*wpa_ie != _WPA_IE_ID_) || (*(wpa_ie + 1) != (u8)(wpa_ie_len - 2))
334	     || (memcmp(wpa_ie + 2, (void *)WPA_OUI_TYPE, WPA_SELECTOR_LEN)))
 
335		return _FAIL;
336	pos = wpa_ie;
337	pos += 8;
338	left = wpa_ie_len - 8;
339	/*group_cipher*/
340	if (left >= WPA_SELECTOR_LEN) {
341		*group_cipher = r8712_get_wpa_cipher_suite(pos);
342		pos += WPA_SELECTOR_LEN;
343		left -= WPA_SELECTOR_LEN;
344	} else if (left > 0)
345		return _FAIL;
 
346	/*pairwise_cipher*/
347	if (left >= 2) {
348		count = le16_to_cpu(*(u16 *)pos);
349		pos += 2;
350		left -= 2;
351		if (count == 0 || left < count * WPA_SELECTOR_LEN)
352			return _FAIL;
353		for (i = 0; i < count; i++) {
354			*pairwise_cipher |= r8712_get_wpa_cipher_suite(pos);
355			pos += WPA_SELECTOR_LEN;
356			left -= WPA_SELECTOR_LEN;
357		}
358	} else if (left == 1)
359		return _FAIL;
360	return ret;
 
361}
362
363int r8712_parse_wpa2_ie(u8 *rsn_ie, int rsn_ie_len, int *group_cipher,
364		  int *pairwise_cipher)
365{
366	int i, ret = _SUCCESS;
367	int left, count;
368	u8 *pos;
369
370	if (rsn_ie_len <= 0) {
371		/* No RSN IE - fail silently */
372		return _FAIL;
373	}
374	if ((*rsn_ie != _WPA2_IE_ID_) || (*(rsn_ie+1) != (u8)(rsn_ie_len - 2)))
 
375		return _FAIL;
376	pos = rsn_ie;
377	pos += 4;
378	left = rsn_ie_len - 4;
379	/*group_cipher*/
380	if (left >= RSN_SELECTOR_LEN) {
381		*group_cipher = r8712_get_wpa2_cipher_suite(pos);
382		pos += RSN_SELECTOR_LEN;
383		left -= RSN_SELECTOR_LEN;
384	} else if (left > 0)
385		return _FAIL;
 
386	/*pairwise_cipher*/
387	if (left >= 2) {
388		count = le16_to_cpu(*(u16 *)pos);
389		pos += 2;
390		left -= 2;
391		if (count == 0 || left < count * RSN_SELECTOR_LEN)
392			return _FAIL;
393		for (i = 0; i < count; i++) {
394			*pairwise_cipher |= r8712_get_wpa2_cipher_suite(pos);
395			pos += RSN_SELECTOR_LEN;
396			left -= RSN_SELECTOR_LEN;
397		}
398	} else if (left == 1)
399		return _FAIL;
400	return ret;
 
401}
402
403int r8712_get_sec_ie(u8 *in_ie, uint in_len, u8 *rsn_ie, u16 *rsn_len,
404	       u8 *wpa_ie, u16 *wpa_len)
405{
406	u8 authmode, sec_idx;
407	u8 wpa_oui[4] = {0x0, 0x50, 0xf2, 0x01};
408	uint cnt;
409
410	/*Search required WPA or WPA2 IE and copy to sec_ie[ ]*/
411	cnt = (_TIMESTAMP_ + _BEACON_ITERVAL_ + _CAPABILITY_);
412	sec_idx = 0;
413	while (cnt < in_len) {
414		authmode = in_ie[cnt];
415		if ((authmode == _WPA_IE_ID_) &&
416		    (!memcmp(&in_ie[cnt + 2], &wpa_oui[0], 4))) {
417			memcpy(wpa_ie, &in_ie[cnt], in_ie[cnt + 1] + 2);
418			*wpa_len = in_ie[cnt+1]+2;
419			cnt += in_ie[cnt + 1] + 2;  /*get next */
420		} else {
421			if (authmode == _WPA2_IE_ID_) {
422				memcpy(rsn_ie, &in_ie[cnt],
423					in_ie[cnt + 1] + 2);
424				*rsn_len = in_ie[cnt+1] + 2;
425				cnt += in_ie[cnt+1] + 2;  /*get next*/
426			} else
427				cnt += in_ie[cnt+1] + 2;   /*get next*/
 
428		}
429	}
430	return *rsn_len + *wpa_len;
431}
432
433int r8712_get_wps_ie(u8 *in_ie, uint in_len, u8 *wps_ie, uint *wps_ielen)
434{
435	int match;
436	uint cnt;
437	u8 eid, wps_oui[4] = {0x0, 0x50, 0xf2, 0x04};
438
439	cnt = 12;
440	match = false;
441	while (cnt < in_len) {
442		eid = in_ie[cnt];
443		if ((eid == _WPA_IE_ID_) &&
444		    (!memcmp(&in_ie[cnt+2], wps_oui, 4))) {
445			memcpy(wps_ie, &in_ie[cnt], in_ie[cnt+1]+2);
446			*wps_ielen = in_ie[cnt+1]+2;
447			cnt += in_ie[cnt+1]+2;
448			match = true;
449			break;
450		} else
451			cnt += in_ie[cnt+1]+2; /* goto next */
452	}
453	return match;
454}