1 /******************************************************************************
2  * rtl871x_mlme.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 _RTL871X_MLME_C_
30 
31 #include "osdep_service.h"
32 #include "drv_types.h"
33 #include "recv_osdep.h"
34 #include "xmit_osdep.h"
35 #include "mlme_osdep.h"
36 #include "sta_info.h"
37 #include "wifi.h"
38 #include "wlan_bssdef.h"
39 
40 static void update_ht_cap(struct _adapter *padapter, u8 *pie, uint ie_len);
41 
_init_mlme_priv(struct _adapter * padapter)42 static sint _init_mlme_priv(struct _adapter *padapter)
43 {
44 	sint	i;
45 	u8	*pbuf;
46 	struct wlan_network	*pnetwork;
47 	struct	mlme_priv *pmlmepriv = &padapter->mlmepriv;
48 
49 	memset((u8 *)pmlmepriv, 0, sizeof(struct mlme_priv));
50 	pmlmepriv->nic_hdl = (u8 *)padapter;
51 	pmlmepriv->pscanned = NULL;
52 	pmlmepriv->fw_state = 0;
53 	pmlmepriv->cur_network.network.InfrastructureMode =
54 				 Ndis802_11AutoUnknown;
55 	spin_lock_init(&(pmlmepriv->lock));
56 	spin_lock_init(&(pmlmepriv->lock2));
57 	_init_queue(&(pmlmepriv->free_bss_pool));
58 	_init_queue(&(pmlmepriv->scanned_queue));
59 	set_scanned_network_val(pmlmepriv, 0);
60 	memset(&pmlmepriv->assoc_ssid, 0, sizeof(struct ndis_802_11_ssid));
61 	pbuf = _malloc(MAX_BSS_CNT * (sizeof(struct wlan_network)));
62 	if (pbuf == NULL)
63 		return _FAIL;
64 	pmlmepriv->free_bss_buf = pbuf;
65 	pnetwork = (struct wlan_network *)pbuf;
66 	for (i = 0; i < MAX_BSS_CNT; i++) {
67 		_init_listhead(&(pnetwork->list));
68 		list_insert_tail(&(pnetwork->list),
69 				 &(pmlmepriv->free_bss_pool.queue));
70 		pnetwork++;
71 	}
72 	pmlmepriv->sitesurveyctrl.last_rx_pkts = 0;
73 	pmlmepriv->sitesurveyctrl.last_tx_pkts = 0;
74 	pmlmepriv->sitesurveyctrl.traffic_busy = false;
75 	/* allocate DMA-able/Non-Page memory for cmd_buf and rsp_buf */
76 	r8712_init_mlme_timer(padapter);
77 	return _SUCCESS;
78 }
79 
_r8712_alloc_network(struct mlme_priv * pmlmepriv)80 struct wlan_network *_r8712_alloc_network(struct mlme_priv *pmlmepriv)
81 {
82 	unsigned long irqL;
83 	struct wlan_network *pnetwork;
84 	struct  __queue *free_queue = &pmlmepriv->free_bss_pool;
85 	struct list_head *plist = NULL;
86 
87 	if (_queue_empty(free_queue) == true)
88 		return NULL;
89 	spin_lock_irqsave(&free_queue->lock, irqL);
90 	plist = get_next(&(free_queue->queue));
91 	pnetwork = LIST_CONTAINOR(plist , struct wlan_network, list);
92 	list_delete(&pnetwork->list);
93 	pnetwork->last_scanned = jiffies;
94 	pmlmepriv->num_of_scanned++;
95 	spin_unlock_irqrestore(&free_queue->lock, irqL);
96 	return pnetwork;
97 }
98 
_free_network(struct mlme_priv * pmlmepriv,struct wlan_network * pnetwork)99 static void _free_network(struct mlme_priv *pmlmepriv,
100 			  struct wlan_network *pnetwork)
101 {
102 	u32 curr_time, delta_time;
103 	unsigned long irqL;
104 	struct  __queue *free_queue = &(pmlmepriv->free_bss_pool);
105 
106 	if (pnetwork == NULL)
107 		return;
108 	if (pnetwork->fixed == true)
109 		return;
110 	curr_time = jiffies;
111 	delta_time = (curr_time - (u32)pnetwork->last_scanned) / HZ;
112 	if (delta_time < SCANQUEUE_LIFETIME)
113 		return;
114 	spin_lock_irqsave(&free_queue->lock, irqL);
115 	list_delete(&pnetwork->list);
116 	list_insert_tail(&pnetwork->list, &free_queue->queue);
117 	pmlmepriv->num_of_scanned--;
118 	spin_unlock_irqrestore(&free_queue->lock, irqL);
119 }
120 
_free_network_nolock(struct mlme_priv * pmlmepriv,struct wlan_network * pnetwork)121 static void _free_network_nolock(struct mlme_priv *pmlmepriv,
122 			  struct wlan_network *pnetwork)
123 {
124 	struct  __queue *free_queue = &pmlmepriv->free_bss_pool;
125 
126 	if (pnetwork == NULL)
127 		return;
128 	if (pnetwork->fixed == true)
129 		return;
130 	list_delete(&pnetwork->list);
131 	list_insert_tail(&pnetwork->list, get_list_head(free_queue));
132 	pmlmepriv->num_of_scanned--;
133 }
134 
135 
136 /*
137 	return the wlan_network with the matching addr
138 	Shall be calle under atomic context...
139 	to avoid possible racing condition...
140 */
_r8712_find_network(struct __queue * scanned_queue,u8 * addr)141 static struct wlan_network *_r8712_find_network(struct  __queue *scanned_queue,
142 					 u8 *addr)
143 {
144 	unsigned long irqL;
145 	struct list_head *phead, *plist;
146 	struct wlan_network *pnetwork = NULL;
147 	u8 zero_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
148 
149 	if (!memcmp(zero_addr, addr, ETH_ALEN))
150 		return NULL;
151 	spin_lock_irqsave(&scanned_queue->lock, irqL);
152 	phead = get_list_head(scanned_queue);
153 	plist = get_next(phead);
154 	while (plist != phead) {
155 		pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list);
156 		plist = get_next(plist);
157 		if (!memcmp(addr, pnetwork->network.MacAddress, ETH_ALEN))
158 			break;
159 	}
160 	spin_unlock_irqrestore(&scanned_queue->lock, irqL);
161 	return pnetwork;
162 }
163 
_free_network_queue(struct _adapter * padapter)164 static void _free_network_queue(struct _adapter *padapter)
165 {
166 	unsigned long irqL;
167 	struct list_head *phead, *plist;
168 	struct wlan_network *pnetwork;
169 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
170 	struct  __queue *scanned_queue = &pmlmepriv->scanned_queue;
171 
172 	spin_lock_irqsave(&scanned_queue->lock, irqL);
173 	phead = get_list_head(scanned_queue);
174 	plist = get_next(phead);
175 	while (end_of_queue_search(phead, plist) == false) {
176 		pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list);
177 		plist = get_next(plist);
178 		_free_network(pmlmepriv, pnetwork);
179 	}
180 	spin_unlock_irqrestore(&scanned_queue->lock, irqL);
181 }
182 
r8712_if_up(struct _adapter * padapter)183 sint r8712_if_up(struct _adapter *padapter)
184 {
185 	sint res;
186 
187 	if (padapter->bDriverStopped || padapter->bSurpriseRemoved ||
188 	    (check_fwstate(&padapter->mlmepriv, _FW_LINKED) == false)) {
189 		res = false;
190 	} else
191 		res = true;
192 	return res;
193 }
194 
r8712_generate_random_ibss(u8 * pibss)195 void r8712_generate_random_ibss(u8 *pibss)
196 {
197 	u32 curtime = jiffies;
198 
199 	pibss[0] = 0x02; /*in ad-hoc mode bit1 must set to 1 */
200 	pibss[1] = 0x11;
201 	pibss[2] = 0x87;
202 	pibss[3] = (u8)(curtime & 0xff);
203 	pibss[4] = (u8)((curtime>>8) & 0xff);
204 	pibss[5] = (u8)((curtime>>16) & 0xff);
205 }
206 
r8712_get_ndis_wlan_bssid_ex_sz(struct ndis_wlan_bssid_ex * bss)207 uint r8712_get_ndis_wlan_bssid_ex_sz(struct ndis_wlan_bssid_ex *bss)
208 {
209 	uint t_len;
210 
211 	t_len = sizeof(u32) + 6 * sizeof(unsigned long) + 2 +
212 			sizeof(struct ndis_802_11_ssid) + sizeof(u32) +
213 			sizeof(s32) +
214 			sizeof(enum NDIS_802_11_NETWORK_TYPE) +
215 			sizeof(struct NDIS_802_11_CONFIGURATION) +
216 			sizeof(enum NDIS_802_11_NETWORK_INFRASTRUCTURE) +
217 			sizeof(NDIS_802_11_RATES_EX) +
218 			sizeof(u32) + bss->IELength;
219 	return t_len;
220 }
221 
r8712_get_capability_from_ie(u8 * ie)222 u8 *r8712_get_capability_from_ie(u8 *ie)
223 {
224 	return ie + 8 + 2;
225 }
226 
r8712_init_mlme_priv(struct _adapter * padapter)227 int r8712_init_mlme_priv(struct _adapter *padapter)
228 {
229 	return _init_mlme_priv(padapter);
230 }
231 
r8712_free_mlme_priv(struct mlme_priv * pmlmepriv)232 void r8712_free_mlme_priv(struct mlme_priv *pmlmepriv)
233 {
234 	kfree(pmlmepriv->free_bss_buf);
235 }
236 
alloc_network(struct mlme_priv * pmlmepriv)237 static struct	wlan_network *alloc_network(struct mlme_priv *pmlmepriv)
238 {
239 	return _r8712_alloc_network(pmlmepriv);
240 }
241 
free_network_nolock(struct mlme_priv * pmlmepriv,struct wlan_network * pnetwork)242 static void free_network_nolock(struct mlme_priv *pmlmepriv,
243 			 struct wlan_network *pnetwork)
244 {
245 	_free_network_nolock(pmlmepriv, pnetwork);
246 }
247 
r8712_free_network_queue(struct _adapter * dev)248 void r8712_free_network_queue(struct _adapter *dev)
249 {
250 	_free_network_queue(dev);
251 }
252 
253 /*
254 	return the wlan_network with the matching addr
255 
256 	Shall be calle under atomic context...
257 	to avoid possible racing condition...
258 */
r8712_find_network(struct __queue * scanned_queue,u8 * addr)259 static struct wlan_network *r8712_find_network(struct  __queue *scanned_queue,
260 					       u8 *addr)
261 {
262 	struct wlan_network *pnetwork = _r8712_find_network(scanned_queue,
263 							    addr);
264 
265 	return pnetwork;
266 }
267 
r8712_is_same_ibss(struct _adapter * adapter,struct wlan_network * pnetwork)268 int r8712_is_same_ibss(struct _adapter *adapter, struct wlan_network *pnetwork)
269 {
270 	int ret = true;
271 	struct security_priv *psecuritypriv = &adapter->securitypriv;
272 
273 	if ((psecuritypriv->PrivacyAlgrthm != _NO_PRIVACY_) &&
274 		    (pnetwork->network.Privacy == 0))
275 		ret = false;
276 	else if ((psecuritypriv->PrivacyAlgrthm == _NO_PRIVACY_) &&
277 		 (pnetwork->network.Privacy == 1))
278 		ret = false;
279 	else
280 		ret = true;
281 	return ret;
282 
283 }
284 
is_same_network(struct ndis_wlan_bssid_ex * src,struct ndis_wlan_bssid_ex * dst)285 static int is_same_network(struct ndis_wlan_bssid_ex *src,
286 			   struct ndis_wlan_bssid_ex *dst)
287 {
288 	 u16 s_cap, d_cap;
289 
290 	memcpy((u8 *)&s_cap, r8712_get_capability_from_ie(src->IEs), 2);
291 	memcpy((u8 *)&d_cap, r8712_get_capability_from_ie(dst->IEs), 2);
292 	return (src->Ssid.SsidLength == dst->Ssid.SsidLength) &&
293 			(src->Configuration.DSConfig ==
294 			dst->Configuration.DSConfig) &&
295 			((!memcmp(src->MacAddress, dst->MacAddress,
296 			ETH_ALEN))) &&
297 			((!memcmp(src->Ssid.Ssid,
298 			  dst->Ssid.Ssid,
299 			  src->Ssid.SsidLength))) &&
300 			((s_cap & WLAN_CAPABILITY_IBSS) ==
301 			(d_cap & WLAN_CAPABILITY_IBSS)) &&
302 			((s_cap & WLAN_CAPABILITY_BSS) ==
303 			(d_cap & WLAN_CAPABILITY_BSS));
304 
305 }
306 
r8712_get_oldest_wlan_network(struct __queue * scanned_queue)307 struct	wlan_network *r8712_get_oldest_wlan_network(
308 				struct  __queue *scanned_queue)
309 {
310 	struct list_head *plist, *phead;
311 	struct	wlan_network	*pwlan = NULL;
312 	struct	wlan_network	*oldest = NULL;
313 
314 	phead = get_list_head(scanned_queue);
315 	plist = get_next(phead);
316 	while (1) {
317 		if (end_of_queue_search(phead, plist) ==  true)
318 			break;
319 		pwlan = LIST_CONTAINOR(plist, struct wlan_network, list);
320 		if (pwlan->fixed != true) {
321 			if (oldest == NULL ||
322 			    time_after((unsigned long)oldest->last_scanned,
323 			    (unsigned long)pwlan->last_scanned))
324 				oldest = pwlan;
325 		}
326 		plist = get_next(plist);
327 	}
328 	return oldest;
329 }
330 
update_network(struct ndis_wlan_bssid_ex * dst,struct ndis_wlan_bssid_ex * src,struct _adapter * padapter)331 static void update_network(struct ndis_wlan_bssid_ex *dst,
332 			   struct ndis_wlan_bssid_ex *src,
333 			   struct _adapter *padapter)
334 {
335 	u32 last_evm = 0, tmpVal;
336 
337 	if (check_fwstate(&padapter->mlmepriv, _FW_LINKED) &&
338 	    is_same_network(&(padapter->mlmepriv.cur_network.network), src)) {
339 		if (padapter->recvpriv.signal_qual_data.total_num++ >=
340 		    PHY_LINKQUALITY_SLID_WIN_MAX) {
341 			padapter->recvpriv.signal_qual_data.total_num =
342 				   PHY_LINKQUALITY_SLID_WIN_MAX;
343 			last_evm = padapter->recvpriv.signal_qual_data.
344 				   elements[padapter->recvpriv.
345 				   signal_qual_data.index];
346 			padapter->recvpriv.signal_qual_data.total_val -=
347 				 last_evm;
348 		}
349 		padapter->recvpriv.signal_qual_data.total_val += src->Rssi;
350 
351 		padapter->recvpriv.signal_qual_data.
352 			  elements[padapter->recvpriv.signal_qual_data.
353 			  index++] = src->Rssi;
354 		if (padapter->recvpriv.signal_qual_data.index >=
355 		    PHY_LINKQUALITY_SLID_WIN_MAX)
356 			padapter->recvpriv.signal_qual_data.index = 0;
357 		/* <1> Showed on UI for user, in percentage. */
358 		tmpVal = padapter->recvpriv.signal_qual_data.total_val /
359 			 padapter->recvpriv.signal_qual_data.total_num;
360 		padapter->recvpriv.signal = (u8)tmpVal;
361 
362 		src->Rssi = padapter->recvpriv.signal;
363 	} else
364 		src->Rssi = (src->Rssi + dst->Rssi) / 2;
365 	memcpy((u8 *)dst, (u8 *)src, r8712_get_ndis_wlan_bssid_ex_sz(src));
366 }
367 
update_current_network(struct _adapter * adapter,struct ndis_wlan_bssid_ex * pnetwork)368 static void update_current_network(struct _adapter *adapter,
369 				   struct ndis_wlan_bssid_ex *pnetwork)
370 {
371 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
372 
373 	if (is_same_network(&(pmlmepriv->cur_network.network), pnetwork)) {
374 		update_network(&(pmlmepriv->cur_network.network),
375 			       pnetwork, adapter);
376 		r8712_update_protection(adapter,
377 			       (pmlmepriv->cur_network.network.IEs) +
378 			       sizeof(struct NDIS_802_11_FIXED_IEs),
379 			       pmlmepriv->cur_network.network.IELength);
380 	}
381 }
382 
383 /*
384 Caller must hold pmlmepriv->lock first.
385 */
update_scanned_network(struct _adapter * adapter,struct ndis_wlan_bssid_ex * target)386 static void update_scanned_network(struct _adapter *adapter,
387 			    struct ndis_wlan_bssid_ex *target)
388 {
389 	struct list_head *plist, *phead;
390 
391 	u32 bssid_ex_sz;
392 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
393 	struct  __queue *queue = &pmlmepriv->scanned_queue;
394 	struct wlan_network *pnetwork = NULL;
395 	struct wlan_network *oldest = NULL;
396 
397 	phead = get_list_head(queue);
398 	plist = get_next(phead);
399 
400 	while (1) {
401 		if (end_of_queue_search(phead, plist) == true)
402 			break;
403 
404 		pnetwork = LIST_CONTAINOR(plist, struct wlan_network, list);
405 		if (is_same_network(&pnetwork->network, target))
406 			break;
407 		if ((oldest == ((struct wlan_network *)0)) ||
408 		    time_after((unsigned long)oldest->last_scanned,
409 				(unsigned long)pnetwork->last_scanned))
410 			oldest = pnetwork;
411 
412 		plist = get_next(plist);
413 	}
414 
415 
416 	/* If we didn't find a match, then get a new network slot to initialize
417 	 * with this beacon's information */
418 	if (end_of_queue_search(phead, plist) == true) {
419 		if (_queue_empty(&pmlmepriv->free_bss_pool) == true) {
420 			/* If there are no more slots, expire the oldest */
421 			pnetwork = oldest;
422 			target->Rssi = (pnetwork->network.Rssi +
423 					target->Rssi) / 2;
424 			memcpy(&pnetwork->network, target,
425 				r8712_get_ndis_wlan_bssid_ex_sz(target));
426 			pnetwork->last_scanned = jiffies;
427 		} else {
428 			/* Otherwise just pull from the free list */
429 			/* update scan_time */
430 			pnetwork = alloc_network(pmlmepriv);
431 			if (pnetwork == NULL)
432 				return;
433 			bssid_ex_sz = r8712_get_ndis_wlan_bssid_ex_sz(target);
434 			target->Length = bssid_ex_sz;
435 			memcpy(&pnetwork->network, target, bssid_ex_sz);
436 			list_insert_tail(&pnetwork->list, &queue->queue);
437 		}
438 	} else {
439 		/* we have an entry and we are going to update it. But
440 		 * this entry may be already expired. In this case we
441 		 * do the same as we found a new net and call the new_net
442 		 * handler
443 		 */
444 		update_network(&pnetwork->network, target, adapter);
445 		pnetwork->last_scanned = jiffies;
446 	}
447 }
448 
rtl8711_add_network(struct _adapter * adapter,struct ndis_wlan_bssid_ex * pnetwork)449 static void rtl8711_add_network(struct _adapter *adapter,
450 			 struct ndis_wlan_bssid_ex *pnetwork)
451 {
452 	unsigned long irqL;
453 	struct mlme_priv *pmlmepriv = &(((struct _adapter *)adapter)->mlmepriv);
454 	struct  __queue *queue = &pmlmepriv->scanned_queue;
455 
456 	spin_lock_irqsave(&queue->lock, irqL);
457 	update_current_network(adapter, pnetwork);
458 	update_scanned_network(adapter, pnetwork);
459 	spin_unlock_irqrestore(&queue->lock, irqL);
460 }
461 
462 /*select the desired network based on the capability of the (i)bss.
463  * check items:		(1) security
464  *			(2) network_type
465  *			(3) WMM
466  *			(4) HT
467  *			(5) others
468  */
is_desired_network(struct _adapter * adapter,struct wlan_network * pnetwork)469 static int is_desired_network(struct _adapter *adapter,
470 				struct wlan_network *pnetwork)
471 {
472 	u8 wps_ie[512];
473 	uint wps_ielen;
474 	int bselected = true;
475 	struct	security_priv *psecuritypriv = &adapter->securitypriv;
476 
477 	if (psecuritypriv->wps_phase == true) {
478 		if (r8712_get_wps_ie(pnetwork->network.IEs,
479 		    pnetwork->network.IELength, wps_ie,
480 		    &wps_ielen) == true)
481 			return true;
482 		else
483 			return false;
484 	}
485 	if ((psecuritypriv->PrivacyAlgrthm != _NO_PRIVACY_) &&
486 		    (pnetwork->network.Privacy == 0))
487 		bselected = false;
488 	return bselected;
489 }
490 
491 /* TODO: Perry : For Power Management */
r8712_atimdone_event_callback(struct _adapter * adapter,u8 * pbuf)492 void r8712_atimdone_event_callback(struct _adapter *adapter , u8 *pbuf)
493 {
494 }
495 
r8712_survey_event_callback(struct _adapter * adapter,u8 * pbuf)496 void r8712_survey_event_callback(struct _adapter *adapter, u8 *pbuf)
497 {
498 	unsigned long flags;
499 	u32 len;
500 	struct ndis_wlan_bssid_ex *pnetwork;
501 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
502 
503 	pnetwork = (struct ndis_wlan_bssid_ex *)pbuf;
504 #ifdef __BIG_ENDIAN
505 	/* endian_convert */
506 	pnetwork->Length = le32_to_cpu(pnetwork->Length);
507 	pnetwork->Ssid.SsidLength = le32_to_cpu(pnetwork->Ssid.SsidLength);
508 	pnetwork->Privacy = le32_to_cpu(pnetwork->Privacy);
509 	pnetwork->Rssi = le32_to_cpu(pnetwork->Rssi);
510 	pnetwork->NetworkTypeInUse = le32_to_cpu(pnetwork->NetworkTypeInUse);
511 	pnetwork->Configuration.ATIMWindow =
512 		 le32_to_cpu(pnetwork->Configuration.ATIMWindow);
513 	pnetwork->Configuration.BeaconPeriod =
514 		 le32_to_cpu(pnetwork->Configuration.BeaconPeriod);
515 	pnetwork->Configuration.DSConfig =
516 		 le32_to_cpu(pnetwork->Configuration.DSConfig);
517 	pnetwork->Configuration.FHConfig.DwellTime =
518 		 le32_to_cpu(pnetwork->Configuration.FHConfig.DwellTime);
519 	pnetwork->Configuration.FHConfig.HopPattern =
520 		 le32_to_cpu(pnetwork->Configuration.FHConfig.HopPattern);
521 	pnetwork->Configuration.FHConfig.HopSet =
522 		 le32_to_cpu(pnetwork->Configuration.FHConfig.HopSet);
523 	pnetwork->Configuration.FHConfig.Length =
524 		 le32_to_cpu(pnetwork->Configuration.FHConfig.Length);
525 	pnetwork->Configuration.Length =
526 		 le32_to_cpu(pnetwork->Configuration.Length);
527 	pnetwork->InfrastructureMode =
528 		 le32_to_cpu(pnetwork->InfrastructureMode);
529 	pnetwork->IELength = le32_to_cpu(pnetwork->IELength);
530 #endif
531 	len = r8712_get_ndis_wlan_bssid_ex_sz(pnetwork);
532 	if (len > sizeof(struct wlan_bssid_ex))
533 		return;
534 	spin_lock_irqsave(&pmlmepriv->lock2, flags);
535 	/* update IBSS_network 's timestamp */
536 	if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true) {
537 		if (!memcmp(&(pmlmepriv->cur_network.network.MacAddress),
538 		    pnetwork->MacAddress, ETH_ALEN)) {
539 			struct wlan_network *ibss_wlan = NULL;
540 
541 			memcpy(pmlmepriv->cur_network.network.IEs,
542 				pnetwork->IEs, 8);
543 			ibss_wlan = r8712_find_network(
544 						&pmlmepriv->scanned_queue,
545 						pnetwork->MacAddress);
546 			if (!ibss_wlan) {
547 				memcpy(ibss_wlan->network.IEs,
548 					pnetwork->IEs, 8);
549 				goto exit;
550 			}
551 		}
552 	}
553 	/* lock pmlmepriv->lock when you accessing network_q */
554 	if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING) == false) {
555 		if (pnetwork->Ssid.Ssid[0] != 0)
556 			rtl8711_add_network(adapter, pnetwork);
557 		else {
558 			pnetwork->Ssid.SsidLength = 8;
559 			memcpy(pnetwork->Ssid.Ssid, "<hidden>", 8);
560 			rtl8711_add_network(adapter, pnetwork);
561 		}
562 	}
563 exit:
564 	spin_unlock_irqrestore(&pmlmepriv->lock2, flags);
565 }
566 
r8712_surveydone_event_callback(struct _adapter * adapter,u8 * pbuf)567 void r8712_surveydone_event_callback(struct _adapter *adapter, u8 *pbuf)
568 {
569 	unsigned long irqL;
570 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
571 
572 	spin_lock_irqsave(&pmlmepriv->lock, irqL);
573 
574 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY) == true) {
575 		u8 timer_cancelled;
576 
577 		_cancel_timer(&pmlmepriv->scan_to_timer, &timer_cancelled);
578 
579 		_clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY);
580 	}
581 
582 	if (pmlmepriv->to_join == true) {
583 		if ((check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true)) {
584 			if (check_fwstate(pmlmepriv, _FW_LINKED) == false) {
585 				set_fwstate(pmlmepriv, _FW_UNDER_LINKING);
586 
587 				if (r8712_select_and_join_from_scan(pmlmepriv)
588 				    == _SUCCESS)
589 					_set_timer(&pmlmepriv->assoc_timer,
590 						   MAX_JOIN_TIMEOUT);
591 				else {
592 					struct wlan_bssid_ex *pdev_network =
593 					  &(adapter->registrypriv.dev_network);
594 					u8 *pibss =
595 						 adapter->registrypriv.
596 							dev_network.MacAddress;
597 					pmlmepriv->fw_state ^= _FW_UNDER_SURVEY;
598 					memset(&pdev_network->Ssid, 0,
599 						sizeof(struct
600 						       ndis_802_11_ssid));
601 					memcpy(&pdev_network->Ssid,
602 						&pmlmepriv->assoc_ssid,
603 						sizeof(struct
604 							 ndis_802_11_ssid));
605 					r8712_update_registrypriv_dev_network
606 						(adapter);
607 					r8712_generate_random_ibss(pibss);
608 					pmlmepriv->fw_state =
609 						 WIFI_ADHOC_MASTER_STATE;
610 					pmlmepriv->to_join = false;
611 				}
612 			}
613 		} else {
614 			pmlmepriv->to_join = false;
615 			set_fwstate(pmlmepriv, _FW_UNDER_LINKING);
616 			if (r8712_select_and_join_from_scan(pmlmepriv) ==
617 			    _SUCCESS)
618 				_set_timer(&pmlmepriv->assoc_timer,
619 					   MAX_JOIN_TIMEOUT);
620 			else
621 				_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
622 		}
623 	}
624 	spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
625 }
626 
627 /*
628  *r8712_free_assoc_resources: the caller has to lock pmlmepriv->lock
629  */
r8712_free_assoc_resources(struct _adapter * adapter)630 void r8712_free_assoc_resources(struct _adapter *adapter)
631 {
632 	unsigned long irqL;
633 	struct wlan_network *pwlan = NULL;
634 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
635 	struct sta_priv *pstapriv = &adapter->stapriv;
636 	struct wlan_network *tgt_network = &pmlmepriv->cur_network;
637 
638 	pwlan = r8712_find_network(&pmlmepriv->scanned_queue,
639 				   tgt_network->network.MacAddress);
640 
641 	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE|WIFI_AP_STATE)) {
642 		struct sta_info *psta;
643 
644 		psta = r8712_get_stainfo(&adapter->stapriv,
645 					 tgt_network->network.MacAddress);
646 
647 		spin_lock_irqsave(&pstapriv->sta_hash_lock, irqL);
648 		r8712_free_stainfo(adapter,  psta);
649 		spin_unlock_irqrestore(&pstapriv->sta_hash_lock, irqL);
650 	}
651 
652 	if (check_fwstate(pmlmepriv,
653 	    WIFI_ADHOC_STATE|WIFI_ADHOC_MASTER_STATE|WIFI_AP_STATE))
654 		r8712_free_all_stainfo(adapter);
655 	if (pwlan)
656 		pwlan->fixed = false;
657 
658 	if (((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) &&
659 	     (adapter->stapriv.asoc_sta_count == 1)))
660 		free_network_nolock(pmlmepriv, pwlan);
661 }
662 
663 /*
664 *r8712_indicate_connect: the caller has to lock pmlmepriv->lock
665 */
r8712_indicate_connect(struct _adapter * padapter)666 void r8712_indicate_connect(struct _adapter *padapter)
667 {
668 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
669 
670 	pmlmepriv->to_join = false;
671 	set_fwstate(pmlmepriv, _FW_LINKED);
672 	padapter->ledpriv.LedControlHandler(padapter, LED_CTL_LINK);
673 	r8712_os_indicate_connect(padapter);
674 	if (padapter->registrypriv.power_mgnt > PS_MODE_ACTIVE)
675 		_set_timer(&pmlmepriv->dhcp_timer, 60000);
676 }
677 
678 
679 /*
680 *r8712_ind_disconnect: the caller has to lock pmlmepriv->lock
681 */
r8712_ind_disconnect(struct _adapter * padapter)682 void r8712_ind_disconnect(struct _adapter *padapter)
683 {
684 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
685 
686 	_clr_fwstate_(pmlmepriv, _FW_LINKED);
687 	padapter->ledpriv.LedControlHandler(padapter, LED_CTL_NO_LINK);
688 	r8712_os_indicate_disconnect(padapter);
689 	if (padapter->pwrctrlpriv.pwr_mode !=
690 	    padapter->registrypriv.power_mgnt) {
691 		_cancel_timer_ex(&pmlmepriv->dhcp_timer);
692 		r8712_set_ps_mode(padapter, padapter->registrypriv.power_mgnt,
693 				  padapter->registrypriv.smart_ps);
694 	}
695 }
696 
697 /*Notes:
698  *pnetwork : returns from r8712_joinbss_event_callback
699  *ptarget_wlan: found from scanned_queue
700  *if join_res > 0, for (fw_state==WIFI_STATION_STATE), we check if
701  *  "ptarget_sta" & "ptarget_wlan" exist.
702  *if join_res > 0, for (fw_state==WIFI_ADHOC_STATE), we only check
703  * if "ptarget_wlan" exist.
704  *if join_res > 0, update "cur_network->network" from
705  * "pnetwork->network" if (ptarget_wlan !=NULL).
706  */
r8712_joinbss_event_callback(struct _adapter * adapter,u8 * pbuf)707 void r8712_joinbss_event_callback(struct _adapter *adapter, u8 *pbuf)
708 {
709 	unsigned long irqL = 0, irqL2;
710 	u8 timer_cancelled;
711 	struct sta_info	*ptarget_sta = NULL, *pcur_sta = NULL;
712 	struct sta_priv	*pstapriv = &adapter->stapriv;
713 	struct mlme_priv	*pmlmepriv = &adapter->mlmepriv;
714 	struct wlan_network	*cur_network = &pmlmepriv->cur_network;
715 	struct wlan_network	*pcur_wlan = NULL, *ptarget_wlan = NULL;
716 	unsigned int		the_same_macaddr = false;
717 	struct wlan_network *pnetwork;
718 
719 	if (sizeof(struct list_head) == 4 * sizeof(u32)) {
720 		pnetwork = (struct wlan_network *)
721 			   _malloc(sizeof(struct wlan_network));
722 		memcpy((u8 *)pnetwork+16, (u8 *)pbuf + 8,
723 			 sizeof(struct wlan_network) - 16);
724 	} else
725 		pnetwork = (struct wlan_network *)pbuf;
726 
727 #ifdef __BIG_ENDIAN
728 	/* endian_convert */
729 	pnetwork->join_res = le32_to_cpu(pnetwork->join_res);
730 	pnetwork->network_type = le32_to_cpu(pnetwork->network_type);
731 	pnetwork->network.Length = le32_to_cpu(pnetwork->network.Length);
732 	pnetwork->network.Ssid.SsidLength =
733 		 le32_to_cpu(pnetwork->network.Ssid.SsidLength);
734 	pnetwork->network.Privacy = le32_to_cpu(pnetwork->network.Privacy);
735 	pnetwork->network.Rssi = le32_to_cpu(pnetwork->network.Rssi);
736 	pnetwork->network.NetworkTypeInUse =
737 		 le32_to_cpu(pnetwork->network.NetworkTypeInUse);
738 	pnetwork->network.Configuration.ATIMWindow =
739 		 le32_to_cpu(pnetwork->network.Configuration.ATIMWindow);
740 	pnetwork->network.Configuration.BeaconPeriod =
741 		 le32_to_cpu(pnetwork->network.Configuration.BeaconPeriod);
742 	pnetwork->network.Configuration.DSConfig =
743 		 le32_to_cpu(pnetwork->network.Configuration.DSConfig);
744 	pnetwork->network.Configuration.FHConfig.DwellTime =
745 		 le32_to_cpu(pnetwork->network.Configuration.FHConfig.
746 			     DwellTime);
747 	pnetwork->network.Configuration.FHConfig.HopPattern =
748 		 le32_to_cpu(pnetwork->network.Configuration.
749 			     FHConfig.HopPattern);
750 	pnetwork->network.Configuration.FHConfig.HopSet =
751 		 le32_to_cpu(pnetwork->network.Configuration.FHConfig.HopSet);
752 	pnetwork->network.Configuration.FHConfig.Length =
753 		 le32_to_cpu(pnetwork->network.Configuration.FHConfig.Length);
754 	pnetwork->network.Configuration.Length =
755 		 le32_to_cpu(pnetwork->network.Configuration.Length);
756 	pnetwork->network.InfrastructureMode =
757 		 le32_to_cpu(pnetwork->network.InfrastructureMode);
758 	pnetwork->network.IELength = le32_to_cpu(pnetwork->network.IELength);
759 #endif
760 
761 	the_same_macaddr = !memcmp(pnetwork->network.MacAddress,
762 				   cur_network->network.MacAddress, ETH_ALEN);
763 	pnetwork->network.Length =
764 		 r8712_get_ndis_wlan_bssid_ex_sz(&pnetwork->network);
765 	spin_lock_irqsave(&pmlmepriv->lock, irqL);
766 	if (pnetwork->network.Length > sizeof(struct wlan_bssid_ex))
767 		goto ignore_joinbss_callback;
768 	if (pnetwork->join_res > 0) {
769 		if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING) == true) {
770 			/*s1. find ptarget_wlan*/
771 			if (check_fwstate(pmlmepriv, _FW_LINKED) == true) {
772 				if (the_same_macaddr == true)
773 					ptarget_wlan =
774 					    r8712_find_network(&pmlmepriv->
775 					    scanned_queue,
776 					    cur_network->network.MacAddress);
777 				else {
778 					pcur_wlan =
779 					     r8712_find_network(&pmlmepriv->
780 					     scanned_queue,
781 					     cur_network->network.MacAddress);
782 					pcur_wlan->fixed = false;
783 
784 					pcur_sta = r8712_get_stainfo(pstapriv,
785 					     cur_network->network.MacAddress);
786 					spin_lock_irqsave(&pstapriv->
787 						sta_hash_lock, irqL2);
788 					r8712_free_stainfo(adapter, pcur_sta);
789 					spin_unlock_irqrestore(&(pstapriv->
790 						sta_hash_lock), irqL2);
791 
792 					ptarget_wlan =
793 						 r8712_find_network(&pmlmepriv->
794 						 scanned_queue,
795 						 pnetwork->network.
796 						 MacAddress);
797 					if (ptarget_wlan)
798 						ptarget_wlan->fixed = true;
799 				}
800 			} else {
801 				ptarget_wlan = r8712_find_network(&pmlmepriv->
802 						scanned_queue,
803 						pnetwork->network.MacAddress);
804 				if (ptarget_wlan)
805 					ptarget_wlan->fixed = true;
806 			}
807 
808 			if (ptarget_wlan == NULL) {
809 				if (check_fwstate(pmlmepriv,
810 					_FW_UNDER_LINKING))
811 					pmlmepriv->fw_state ^=
812 						 _FW_UNDER_LINKING;
813 				goto ignore_joinbss_callback;
814 			}
815 
816 			/*s2. find ptarget_sta & update ptarget_sta*/
817 			if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
818 				if (the_same_macaddr == true) {
819 					ptarget_sta =
820 						 r8712_get_stainfo(pstapriv,
821 						 pnetwork->network.MacAddress);
822 					if (ptarget_sta == NULL)
823 						ptarget_sta =
824 						 r8712_alloc_stainfo(pstapriv,
825 						 pnetwork->network.MacAddress);
826 				} else
827 					ptarget_sta =
828 						 r8712_alloc_stainfo(pstapriv,
829 						 pnetwork->network.MacAddress);
830 				if (ptarget_sta) /*update ptarget_sta*/ {
831 					ptarget_sta->aid = pnetwork->join_res;
832 					ptarget_sta->qos_option = 1;
833 					ptarget_sta->mac_id = 5;
834 					if (adapter->securitypriv.
835 					    AuthAlgrthm == 2) {
836 						adapter->securitypriv.
837 							binstallGrpkey =
838 							 false;
839 						adapter->securitypriv.
840 							busetkipkey =
841 							 false;
842 						adapter->securitypriv.
843 							bgrpkey_handshake =
844 							 false;
845 						ptarget_sta->ieee8021x_blocked
846 							 = true;
847 						ptarget_sta->XPrivacy =
848 							 adapter->securitypriv.
849 							 PrivacyAlgrthm;
850 						memset((u8 *)&ptarget_sta->
851 							 x_UncstKey,
852 							 0,
853 							 sizeof(union Keytype));
854 						memset((u8 *)&ptarget_sta->
855 							 tkiprxmickey,
856 							 0,
857 							 sizeof(union Keytype));
858 						memset((u8 *)&ptarget_sta->
859 							 tkiptxmickey,
860 							 0,
861 							 sizeof(union Keytype));
862 						memset((u8 *)&ptarget_sta->
863 							 txpn, 0,
864 							 sizeof(union pn48));
865 						memset((u8 *)&ptarget_sta->
866 							 rxpn, 0,
867 							 sizeof(union pn48));
868 					}
869 				} else {
870 					if (check_fwstate(pmlmepriv,
871 					    _FW_UNDER_LINKING))
872 						pmlmepriv->fw_state ^=
873 							 _FW_UNDER_LINKING;
874 					goto ignore_joinbss_callback;
875 				}
876 			}
877 
878 			/*s3. update cur_network & indicate connect*/
879 			memcpy(&cur_network->network, &pnetwork->network,
880 				pnetwork->network.Length);
881 			cur_network->aid = pnetwork->join_res;
882 			/*update fw_state will clr _FW_UNDER_LINKING*/
883 			switch (pnetwork->network.InfrastructureMode) {
884 			case Ndis802_11Infrastructure:
885 				pmlmepriv->fw_state = WIFI_STATION_STATE;
886 				break;
887 			case Ndis802_11IBSS:
888 				pmlmepriv->fw_state = WIFI_ADHOC_STATE;
889 				break;
890 			default:
891 				pmlmepriv->fw_state = WIFI_NULL_STATE;
892 				break;
893 			}
894 			r8712_update_protection(adapter,
895 					  (cur_network->network.IEs) +
896 					  sizeof(struct NDIS_802_11_FIXED_IEs),
897 					  (cur_network->network.IELength));
898 			/*TODO: update HT_Capability*/
899 			update_ht_cap(adapter, cur_network->network.IEs,
900 				      cur_network->network.IELength);
901 			/*indicate connect*/
902 			if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)
903 				== true)
904 				r8712_indicate_connect(adapter);
905 			_cancel_timer(&pmlmepriv->assoc_timer,
906 				      &timer_cancelled);
907 		} else
908 			goto ignore_joinbss_callback;
909 	} else {
910 		if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING) == true) {
911 			_set_timer(&pmlmepriv->assoc_timer, 1);
912 			_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
913 		}
914 	}
915 ignore_joinbss_callback:
916 	spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
917 	if (sizeof(struct list_head) == 4 * sizeof(u32))
918 		kfree((u8 *)pnetwork);
919 }
920 
r8712_stassoc_event_callback(struct _adapter * adapter,u8 * pbuf)921 void r8712_stassoc_event_callback(struct _adapter *adapter, u8 *pbuf)
922 {
923 	unsigned long irqL;
924 	struct sta_info *psta;
925 	struct mlme_priv *pmlmepriv = &(adapter->mlmepriv);
926 	struct stassoc_event *pstassoc	= (struct stassoc_event *)pbuf;
927 
928 	/* to do: */
929 	if (r8712_access_ctrl(&adapter->acl_list, pstassoc->macaddr) == false)
930 		return;
931 	psta = r8712_get_stainfo(&adapter->stapriv, pstassoc->macaddr);
932 	if (psta != NULL) {
933 		/*the sta have been in sta_info_queue => do nothing
934 		 *(between drv has received this event before and
935 		 *  fw have not yet to set key to CAM_ENTRY) */
936 		return;
937 	}
938 
939 	psta = r8712_alloc_stainfo(&adapter->stapriv, pstassoc->macaddr);
940 	if (psta == NULL)
941 		return;
942 	/* to do : init sta_info variable */
943 	psta->qos_option = 0;
944 	psta->mac_id = le32_to_cpu((uint)pstassoc->cam_id);
945 	/* psta->aid = (uint)pstassoc->cam_id; */
946 
947 	if (adapter->securitypriv.AuthAlgrthm == 2)
948 		psta->XPrivacy = adapter->securitypriv.PrivacyAlgrthm;
949 	psta->ieee8021x_blocked = false;
950 	spin_lock_irqsave(&pmlmepriv->lock, irqL);
951 	if ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) == true) ||
952 	    (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE) == true)) {
953 		if (adapter->stapriv.asoc_sta_count == 2) {
954 			/* a sta + bc/mc_stainfo (not Ibss_stainfo) */
955 			r8712_indicate_connect(adapter);
956 		}
957 	}
958 	spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
959 }
960 
r8712_stadel_event_callback(struct _adapter * adapter,u8 * pbuf)961 void r8712_stadel_event_callback(struct _adapter *adapter, u8 *pbuf)
962 {
963 	unsigned long irqL, irqL2;
964 	struct sta_info *psta;
965 	struct wlan_network *pwlan = NULL;
966 	struct wlan_bssid_ex *pdev_network = NULL;
967 	u8 *pibss = NULL;
968 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
969 	struct stadel_event *pstadel = (struct stadel_event *)pbuf;
970 	struct sta_priv *pstapriv = &adapter->stapriv;
971 	struct wlan_network *tgt_network = &pmlmepriv->cur_network;
972 
973 	spin_lock_irqsave(&pmlmepriv->lock, irqL2);
974 	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE) == true) {
975 		r8712_ind_disconnect(adapter);
976 		r8712_free_assoc_resources(adapter);
977 	}
978 	if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE |
979 	    WIFI_ADHOC_STATE)) {
980 		psta = r8712_get_stainfo(&adapter->stapriv, pstadel->macaddr);
981 		spin_lock_irqsave(&pstapriv->sta_hash_lock, irqL);
982 		r8712_free_stainfo(adapter, psta);
983 		spin_unlock_irqrestore(&pstapriv->sta_hash_lock, irqL);
984 		if (adapter->stapriv.asoc_sta_count == 1) {
985 			/*a sta + bc/mc_stainfo (not Ibss_stainfo) */
986 			pwlan = r8712_find_network(&pmlmepriv->scanned_queue,
987 				tgt_network->network.MacAddress);
988 			if (pwlan) {
989 				pwlan->fixed = false;
990 				free_network_nolock(pmlmepriv, pwlan);
991 			}
992 			/*re-create ibss*/
993 			pdev_network = &(adapter->registrypriv.dev_network);
994 			pibss = adapter->registrypriv.dev_network.MacAddress;
995 			memcpy(pdev_network, &tgt_network->network,
996 				r8712_get_ndis_wlan_bssid_ex_sz(&tgt_network->
997 							network));
998 			memset(&pdev_network->Ssid, 0,
999 				sizeof(struct ndis_802_11_ssid));
1000 			memcpy(&pdev_network->Ssid,
1001 				&pmlmepriv->assoc_ssid,
1002 				sizeof(struct ndis_802_11_ssid));
1003 			r8712_update_registrypriv_dev_network(adapter);
1004 			r8712_generate_random_ibss(pibss);
1005 			if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
1006 				_clr_fwstate_(pmlmepriv, WIFI_ADHOC_STATE);
1007 				set_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE);
1008 			}
1009 		}
1010 	}
1011 	spin_unlock_irqrestore(&pmlmepriv->lock, irqL2);
1012 }
1013 
r8712_cpwm_event_callback(struct _adapter * adapter,u8 * pbuf)1014 void r8712_cpwm_event_callback(struct _adapter *adapter, u8 *pbuf)
1015 {
1016 	struct reportpwrstate_parm *preportpwrstate =
1017 			 (struct reportpwrstate_parm *)pbuf;
1018 
1019 	preportpwrstate->state |= (u8)(adapter->pwrctrlpriv.cpwm_tog + 0x80);
1020 	r8712_cpwm_int_hdl(adapter, preportpwrstate);
1021 }
1022 
1023 /*	When the Netgear 3500 AP is with WPA2PSK-AES mode, it will send
1024  *	 the ADDBA req frame with start seq control = 0 to wifi client after
1025  *	 the WPA handshake and the seqence number of following data packet
1026  *	will be 0. In this case, the Rx reorder sequence is not longer than 0
1027  *	 and the WiFi client will drop the data with seq number 0.
1028  *	So, the 8712 firmware has to inform driver with receiving the
1029  *	 ADDBA-Req frame so that the driver can reset the
1030  *	sequence value of Rx reorder contorl.
1031  */
r8712_got_addbareq_event_callback(struct _adapter * adapter,u8 * pbuf)1032 void r8712_got_addbareq_event_callback(struct _adapter *adapter, u8 *pbuf)
1033 {
1034 	struct	ADDBA_Req_Report_parm *pAddbareq_pram =
1035 			 (struct ADDBA_Req_Report_parm *)pbuf;
1036 	struct	sta_info *psta;
1037 	struct	sta_priv *pstapriv = &adapter->stapriv;
1038 	struct	recv_reorder_ctrl *precvreorder_ctrl = NULL;
1039 
1040 	printk(KERN_INFO "r8712u: [%s] mac = %pM, seq = %d, tid = %d\n",
1041 	     __func__, pAddbareq_pram->MacAddress,
1042 	    pAddbareq_pram->StartSeqNum, pAddbareq_pram->tid);
1043 	psta = r8712_get_stainfo(pstapriv, pAddbareq_pram->MacAddress);
1044 	if (psta) {
1045 		precvreorder_ctrl =
1046 			 &psta->recvreorder_ctrl[pAddbareq_pram->tid];
1047 		/* set the indicate_seq to 0xffff so that the rx reorder
1048 		 * can store any following data packet.
1049 		 */
1050 		precvreorder_ctrl->indicate_seq = 0xffff;
1051 	}
1052 }
1053 
r8712_wpspbc_event_callback(struct _adapter * adapter,u8 * pbuf)1054 void r8712_wpspbc_event_callback(struct _adapter *adapter, u8 *pbuf)
1055 {
1056 	if (adapter->securitypriv.wps_hw_pbc_pressed == false)
1057 		adapter->securitypriv.wps_hw_pbc_pressed = true;
1058 }
1059 
_r8712_sitesurvey_ctrl_handler(struct _adapter * adapter)1060 void _r8712_sitesurvey_ctrl_handler(struct _adapter *adapter)
1061 {
1062 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1063 	struct sitesurvey_ctrl	*psitesurveyctrl = &pmlmepriv->sitesurveyctrl;
1064 	struct registry_priv	*pregistrypriv = &adapter->registrypriv;
1065 	u64 current_tx_pkts;
1066 	uint current_rx_pkts;
1067 
1068 	current_tx_pkts = (adapter->xmitpriv.tx_pkts) -
1069 			  (psitesurveyctrl->last_tx_pkts);
1070 	current_rx_pkts = (adapter->recvpriv.rx_pkts) -
1071 			  (psitesurveyctrl->last_rx_pkts);
1072 	psitesurveyctrl->last_tx_pkts = adapter->xmitpriv.tx_pkts;
1073 	psitesurveyctrl->last_rx_pkts = adapter->recvpriv.rx_pkts;
1074 	if ((current_tx_pkts > pregistrypriv->busy_thresh) ||
1075 	    (current_rx_pkts > pregistrypriv->busy_thresh))
1076 		psitesurveyctrl->traffic_busy = true;
1077 	else
1078 		psitesurveyctrl->traffic_busy = false;
1079 }
1080 
_r8712_join_timeout_handler(struct _adapter * adapter)1081 void _r8712_join_timeout_handler(struct _adapter *adapter)
1082 {
1083 	unsigned long irqL;
1084 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1085 
1086 	if (adapter->bDriverStopped || adapter->bSurpriseRemoved)
1087 		return;
1088 	spin_lock_irqsave(&pmlmepriv->lock, irqL);
1089 	_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
1090 	pmlmepriv->to_join = false;
1091 	if (check_fwstate(pmlmepriv, _FW_LINKED) == true) {
1092 		r8712_os_indicate_disconnect(adapter);
1093 		_clr_fwstate_(pmlmepriv, _FW_LINKED);
1094 	}
1095 	if (adapter->pwrctrlpriv.pwr_mode != adapter->registrypriv.power_mgnt) {
1096 		r8712_set_ps_mode(adapter, adapter->registrypriv.power_mgnt,
1097 				  adapter->registrypriv.smart_ps);
1098 	}
1099 	spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
1100 }
1101 
r8712_scan_timeout_handler(struct _adapter * adapter)1102 void r8712_scan_timeout_handler (struct _adapter *adapter)
1103 {
1104 	unsigned long irqL;
1105 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1106 
1107 	spin_lock_irqsave(&pmlmepriv->lock, irqL);
1108 	_clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY);
1109 	pmlmepriv->to_join = false;	/* scan fail, so clear to_join flag */
1110 	spin_unlock_irqrestore(&pmlmepriv->lock, irqL);
1111 }
1112 
_r8712_dhcp_timeout_handler(struct _adapter * adapter)1113 void _r8712_dhcp_timeout_handler (struct _adapter *adapter)
1114 {
1115 	if (adapter->bDriverStopped || adapter->bSurpriseRemoved)
1116 		return;
1117 	if (adapter->pwrctrlpriv.pwr_mode != adapter->registrypriv.power_mgnt)
1118 		r8712_set_ps_mode(adapter, adapter->registrypriv.power_mgnt,
1119 			    adapter->registrypriv.smart_ps);
1120 }
1121 
_r8712_wdg_timeout_handler(struct _adapter * adapter)1122 void _r8712_wdg_timeout_handler(struct _adapter *adapter)
1123 {
1124 	r8712_wdg_wk_cmd(adapter);
1125 }
1126 
r8712_select_and_join_from_scan(struct mlme_priv * pmlmepriv)1127 int r8712_select_and_join_from_scan(struct mlme_priv *pmlmepriv)
1128 {
1129 	struct list_head *phead;
1130 	unsigned char *dst_ssid, *src_ssid;
1131 	struct _adapter *adapter;
1132 	struct  __queue *queue = NULL;
1133 	struct wlan_network *pnetwork = NULL;
1134 	struct wlan_network *pnetwork_max_rssi = NULL;
1135 
1136 	adapter = (struct _adapter *)pmlmepriv->nic_hdl;
1137 	queue = &pmlmepriv->scanned_queue;
1138 	phead = get_list_head(queue);
1139 	pmlmepriv->pscanned = get_next(phead);
1140 	while (1) {
1141 		if (end_of_queue_search(phead, pmlmepriv->pscanned) == true) {
1142 			if ((pmlmepriv->assoc_by_rssi == true) &&
1143 			    (pnetwork_max_rssi != NULL)) {
1144 				pnetwork = pnetwork_max_rssi;
1145 				goto ask_for_joinbss;
1146 			}
1147 			return _FAIL;
1148 		}
1149 		pnetwork = LIST_CONTAINOR(pmlmepriv->pscanned,
1150 					  struct wlan_network, list);
1151 		if (pnetwork == NULL)
1152 			return _FAIL;
1153 		pmlmepriv->pscanned = get_next(pmlmepriv->pscanned);
1154 		if (pmlmepriv->assoc_by_bssid == true) {
1155 			dst_ssid = pnetwork->network.MacAddress;
1156 			src_ssid = pmlmepriv->assoc_bssid;
1157 			if (!memcmp(dst_ssid, src_ssid, ETH_ALEN)) {
1158 				if (check_fwstate(pmlmepriv, _FW_LINKED)) {
1159 					if (is_same_network(&pmlmepriv->
1160 					    cur_network.network,
1161 					    &pnetwork->network)) {
1162 						_clr_fwstate_(pmlmepriv,
1163 							_FW_UNDER_LINKING);
1164 						/*r8712_indicate_connect again*/
1165 						r8712_indicate_connect(adapter);
1166 						return 2;
1167 					}
1168 					r8712_disassoc_cmd(adapter);
1169 					r8712_ind_disconnect(adapter);
1170 					r8712_free_assoc_resources(adapter);
1171 				}
1172 				goto ask_for_joinbss;
1173 			}
1174 		} else if (pmlmepriv->assoc_ssid.SsidLength == 0)
1175 			goto ask_for_joinbss;
1176 		dst_ssid = pnetwork->network.Ssid.Ssid;
1177 		src_ssid = pmlmepriv->assoc_ssid.Ssid;
1178 		if ((pnetwork->network.Ssid.SsidLength ==
1179 		    pmlmepriv->assoc_ssid.SsidLength) &&
1180 		    (!memcmp(dst_ssid, src_ssid,
1181 		     pmlmepriv->assoc_ssid.SsidLength))) {
1182 			if (pmlmepriv->assoc_by_rssi == true) {
1183 				/* if the ssid is the same, select the bss
1184 				 *  which has the max rssi*/
1185 				if (pnetwork_max_rssi) {
1186 					if (pnetwork->network.Rssi >
1187 					    pnetwork_max_rssi->network.Rssi)
1188 						pnetwork_max_rssi = pnetwork;
1189 				} else
1190 					pnetwork_max_rssi = pnetwork;
1191 			} else if (is_desired_network(adapter, pnetwork)) {
1192 				if (check_fwstate(pmlmepriv, _FW_LINKED)) {
1193 					r8712_disassoc_cmd(adapter);
1194 					r8712_free_assoc_resources(adapter);
1195 				}
1196 				goto ask_for_joinbss;
1197 			}
1198 		}
1199 	}
1200 	return _FAIL;
1201 ask_for_joinbss:
1202 	return r8712_joinbss_cmd(adapter, pnetwork);
1203 }
1204 
r8712_set_auth(struct _adapter * adapter,struct security_priv * psecuritypriv)1205 sint r8712_set_auth(struct _adapter *adapter,
1206 		    struct security_priv *psecuritypriv)
1207 {
1208 	struct cmd_priv	*pcmdpriv = &adapter->cmdpriv;
1209 	struct cmd_obj *pcmd;
1210 	struct setauth_parm *psetauthparm;
1211 	sint ret = _SUCCESS;
1212 
1213 	pcmd = (struct cmd_obj *)_malloc(sizeof(struct cmd_obj));
1214 	if (pcmd == NULL)
1215 		return _FAIL;
1216 
1217 	psetauthparm = (struct setauth_parm *)_malloc(
1218 			sizeof(struct setauth_parm));
1219 	if (psetauthparm == NULL) {
1220 		kfree((unsigned char *)pcmd);
1221 		return _FAIL;
1222 	}
1223 	memset(psetauthparm, 0, sizeof(struct setauth_parm));
1224 	psetauthparm->mode = (u8)psecuritypriv->AuthAlgrthm;
1225 	pcmd->cmdcode = _SetAuth_CMD_;
1226 	pcmd->parmbuf = (unsigned char *)psetauthparm;
1227 	pcmd->cmdsz = sizeof(struct setauth_parm);
1228 	pcmd->rsp = NULL;
1229 	pcmd->rspsz = 0;
1230 	_init_listhead(&pcmd->list);
1231 	r8712_enqueue_cmd(pcmdpriv, pcmd);
1232 	return ret;
1233 }
1234 
r8712_set_key(struct _adapter * adapter,struct security_priv * psecuritypriv,sint keyid)1235 sint r8712_set_key(struct _adapter *adapter,
1236 		   struct security_priv *psecuritypriv,
1237 	     sint keyid)
1238 {
1239 	struct cmd_priv *pcmdpriv = &adapter->cmdpriv;
1240 	struct cmd_obj *pcmd;
1241 	struct setkey_parm *psetkeyparm;
1242 	u8 keylen;
1243 
1244 	pcmd = (struct cmd_obj *)_malloc(sizeof(struct cmd_obj));
1245 	if (pcmd == NULL)
1246 		return _FAIL;
1247 	psetkeyparm = (struct setkey_parm *)_malloc(sizeof(struct setkey_parm));
1248 	if (psetkeyparm == NULL) {
1249 		kfree((unsigned char *)pcmd);
1250 		return _FAIL;
1251 	}
1252 	memset(psetkeyparm, 0, sizeof(struct setkey_parm));
1253 	if (psecuritypriv->AuthAlgrthm == 2) { /* 802.1X */
1254 		psetkeyparm->algorithm =
1255 			 (u8)psecuritypriv->XGrpPrivacy;
1256 	} else { /* WEP */
1257 		psetkeyparm->algorithm =
1258 			 (u8)psecuritypriv->PrivacyAlgrthm;
1259 	}
1260 	psetkeyparm->keyid = (u8)keyid;
1261 
1262 	switch (psetkeyparm->algorithm) {
1263 	case _WEP40_:
1264 		keylen = 5;
1265 		memcpy(psetkeyparm->key,
1266 			psecuritypriv->DefKey[keyid].skey, keylen);
1267 		break;
1268 	case _WEP104_:
1269 		keylen = 13;
1270 		memcpy(psetkeyparm->key,
1271 			psecuritypriv->DefKey[keyid].skey, keylen);
1272 		break;
1273 	case _TKIP_:
1274 		keylen = 16;
1275 		memcpy(psetkeyparm->key,
1276 			&psecuritypriv->XGrpKey[keyid - 1], keylen);
1277 		psetkeyparm->grpkey = 1;
1278 		break;
1279 	case _AES_:
1280 		keylen = 16;
1281 		memcpy(psetkeyparm->key,
1282 			&psecuritypriv->XGrpKey[keyid - 1], keylen);
1283 		psetkeyparm->grpkey = 1;
1284 		break;
1285 	default:
1286 		return _FAIL;
1287 	}
1288 	pcmd->cmdcode = _SetKey_CMD_;
1289 	pcmd->parmbuf = (u8 *)psetkeyparm;
1290 	pcmd->cmdsz =  (sizeof(struct setkey_parm));
1291 	pcmd->rsp = NULL;
1292 	pcmd->rspsz = 0;
1293 	_init_listhead(&pcmd->list);
1294 	r8712_enqueue_cmd(pcmdpriv, pcmd);
1295 	return _SUCCESS;
1296 }
1297 
1298 /* adjust IEs for r8712_joinbss_cmd in WMM */
r8712_restruct_wmm_ie(struct _adapter * adapter,u8 * in_ie,u8 * out_ie,uint in_len,uint initial_out_len)1299 int r8712_restruct_wmm_ie(struct _adapter *adapter, u8 *in_ie, u8 *out_ie,
1300 		    uint in_len, uint initial_out_len)
1301 {
1302 	unsigned int ielength = 0;
1303 	unsigned int i, j;
1304 
1305 	i = 12; /* after the fixed IE */
1306 	while (i < in_len) {
1307 		ielength = initial_out_len;
1308 		if (in_ie[i] == 0xDD && in_ie[i + 2] == 0x00 &&
1309 		    in_ie[i + 3] == 0x50 && in_ie[i + 4] == 0xF2 &&
1310 		    in_ie[i + 5] == 0x02 && i + 5 < in_len) {
1311 			/*WMM element ID and OUI*/
1312 			for (j = i; j < i + 9; j++) {
1313 				out_ie[ielength] = in_ie[j];
1314 				ielength++;
1315 			}
1316 			out_ie[initial_out_len + 1] = 0x07;
1317 			out_ie[initial_out_len + 6] = 0x00;
1318 			out_ie[initial_out_len + 8] = 0x00;
1319 			break;
1320 		}
1321 		i += (in_ie[i + 1] + 2); /* to the next IE element */
1322 	}
1323 	return ielength;
1324 }
1325 
1326 /*
1327  * Ported from 8185: IsInPreAuthKeyList().
1328  *
1329  * Search by BSSID,
1330  * Return Value:
1331  *	-1		:if there is no pre-auth key in the  table
1332  *	>=0		:if there is pre-auth key, and   return the entry id
1333  */
SecIsInPMKIDList(struct _adapter * Adapter,u8 * bssid)1334 static int SecIsInPMKIDList(struct _adapter *Adapter, u8 *bssid)
1335 {
1336 	struct security_priv *psecuritypriv = &Adapter->securitypriv;
1337 	int i = 0;
1338 
1339 	do {
1340 		if (psecuritypriv->PMKIDList[i].bUsed &&
1341 		   (!memcmp(psecuritypriv->PMKIDList[i].Bssid,
1342 			    bssid, ETH_ALEN)))
1343 			break;
1344 		else
1345 			i++;
1346 	} while (i < NUM_PMKID_CACHE);
1347 
1348 	if (i == NUM_PMKID_CACHE) {
1349 		i = -1; /* Could not find. */
1350 	} else {
1351 		; /* There is one Pre-Authentication Key for the
1352 		   * specific BSSID. */
1353 	}
1354 	return i;
1355 }
1356 
r8712_restruct_sec_ie(struct _adapter * adapter,u8 * in_ie,u8 * out_ie,uint in_len)1357 sint r8712_restruct_sec_ie(struct _adapter *adapter, u8 *in_ie,
1358 		     u8 *out_ie, uint in_len)
1359 {
1360 	u8 authmode = 0, securitytype, match;
1361 	u8 sec_ie[255], uncst_oui[4], bkup_ie[255];
1362 	u8 wpa_oui[4] = {0x0, 0x50, 0xf2, 0x01};
1363 	uint ielength, cnt, remove_cnt;
1364 	int iEntry;
1365 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1366 	struct security_priv *psecuritypriv = &adapter->securitypriv;
1367 	uint ndisauthmode = psecuritypriv->ndisauthtype;
1368 	uint ndissecuritytype = psecuritypriv->ndisencryptstatus;
1369 
1370 	if ((ndisauthmode == Ndis802_11AuthModeWPA) ||
1371 	    (ndisauthmode == Ndis802_11AuthModeWPAPSK)) {
1372 		authmode = _WPA_IE_ID_;
1373 		uncst_oui[0] = 0x0;
1374 		uncst_oui[1] = 0x50;
1375 		uncst_oui[2] = 0xf2;
1376 	}
1377 	if ((ndisauthmode == Ndis802_11AuthModeWPA2) ||
1378 	    (ndisauthmode == Ndis802_11AuthModeWPA2PSK)) {
1379 		authmode = _WPA2_IE_ID_;
1380 		uncst_oui[0] = 0x0;
1381 		uncst_oui[1] = 0x0f;
1382 		uncst_oui[2] = 0xac;
1383 	}
1384 	switch (ndissecuritytype) {
1385 	case Ndis802_11Encryption1Enabled:
1386 	case Ndis802_11Encryption1KeyAbsent:
1387 		securitytype = _WEP40_;
1388 		uncst_oui[3] = 0x1;
1389 		break;
1390 	case Ndis802_11Encryption2Enabled:
1391 	case Ndis802_11Encryption2KeyAbsent:
1392 		securitytype = _TKIP_;
1393 		uncst_oui[3] = 0x2;
1394 		break;
1395 	case Ndis802_11Encryption3Enabled:
1396 	case Ndis802_11Encryption3KeyAbsent:
1397 		securitytype = _AES_;
1398 		uncst_oui[3] = 0x4;
1399 		break;
1400 	default:
1401 		securitytype = _NO_PRIVACY_;
1402 		break;
1403 	}
1404 	/*Search required WPA or WPA2 IE and copy to sec_ie[] */
1405 	cnt = 12;
1406 	match = false;
1407 	while (cnt < in_len) {
1408 		if (in_ie[cnt] == authmode) {
1409 			if ((authmode == _WPA_IE_ID_) &&
1410 			    (!memcmp(&in_ie[cnt+2], &wpa_oui[0], 4))) {
1411 				memcpy(&sec_ie[0], &in_ie[cnt],
1412 					in_ie[cnt + 1] + 2);
1413 				match = true;
1414 				break;
1415 			}
1416 			if (authmode == _WPA2_IE_ID_) {
1417 				memcpy(&sec_ie[0], &in_ie[cnt],
1418 					in_ie[cnt + 1] + 2);
1419 				match = true;
1420 				break;
1421 			}
1422 			if (((authmode == _WPA_IE_ID_) &&
1423 			     (!memcmp(&in_ie[cnt + 2], &wpa_oui[0], 4))) ||
1424 			     (authmode == _WPA2_IE_ID_))
1425 				memcpy(&bkup_ie[0], &in_ie[cnt],
1426 					in_ie[cnt + 1] + 2);
1427 		}
1428 		cnt += in_ie[cnt+1] + 2; /*get next*/
1429 	}
1430 	/*restruct WPA IE or WPA2 IE in sec_ie[] */
1431 	if (match == true) {
1432 		if (sec_ie[0] == _WPA_IE_ID_) {
1433 			/* parsing SSN IE to select required encryption
1434 			 * algorithm, and set the bc/mc encryption algorithm */
1435 			while (true) {
1436 				/*check wpa_oui tag*/
1437 				if (memcmp(&sec_ie[2], &wpa_oui[0], 4)) {
1438 					match = false;
1439 					break;
1440 				}
1441 				if ((sec_ie[6] != 0x01) || (sec_ie[7] != 0x0)) {
1442 					/*IE Ver error*/
1443 					match = false;
1444 					break;
1445 				}
1446 				if (!memcmp(&sec_ie[8], &wpa_oui[0], 3)) {
1447 					/* get bc/mc encryption type (group
1448 					 * key type)*/
1449 					switch (sec_ie[11]) {
1450 					case 0x0: /*none*/
1451 						psecuritypriv->XGrpPrivacy =
1452 								_NO_PRIVACY_;
1453 						break;
1454 					case 0x1: /*WEP_40*/
1455 						psecuritypriv->XGrpPrivacy =
1456 								_WEP40_;
1457 						break;
1458 					case 0x2: /*TKIP*/
1459 						psecuritypriv->XGrpPrivacy =
1460 								_TKIP_;
1461 						break;
1462 					case 0x3: /*AESCCMP*/
1463 					case 0x4:
1464 						psecuritypriv->XGrpPrivacy =
1465 								_AES_;
1466 						break;
1467 					case 0x5: /*WEP_104*/
1468 						psecuritypriv->XGrpPrivacy =
1469 								_WEP104_;
1470 						break;
1471 					}
1472 				} else {
1473 					match = false;
1474 					break;
1475 				}
1476 				if (sec_ie[12] == 0x01) {
1477 					/*check the unicast encryption type*/
1478 					if (memcmp(&sec_ie[14],
1479 					    &uncst_oui[0], 4)) {
1480 						match = false;
1481 						break;
1482 
1483 					} /*else the uncst_oui is match*/
1484 				} else { /*mixed mode, unicast_enc_type > 1*/
1485 					/*select the uncst_oui and remove
1486 					 * the other uncst_oui*/
1487 					cnt = sec_ie[12];
1488 					remove_cnt = (cnt-1) * 4;
1489 					sec_ie[12] = 0x01;
1490 					memcpy(&sec_ie[14], &uncst_oui[0], 4);
1491 					/*remove the other unicast suit*/
1492 					memcpy(&sec_ie[18],
1493 						&sec_ie[18 + remove_cnt],
1494 						sec_ie[1] - 18 + 2 -
1495 						remove_cnt);
1496 					sec_ie[1] = sec_ie[1] - remove_cnt;
1497 				}
1498 				break;
1499 			}
1500 		}
1501 		if (authmode == _WPA2_IE_ID_) {
1502 			/* parsing RSN IE to select required encryption
1503 			 * algorithm, and set the bc/mc encryption algorithm */
1504 			while (true) {
1505 				if ((sec_ie[2] != 0x01) || (sec_ie[3] != 0x0)) {
1506 					/*IE Ver error*/
1507 					match = false;
1508 					break;
1509 				}
1510 				if (!memcmp(&sec_ie[4], &uncst_oui[0], 3)) {
1511 					/*get bc/mc encryption type*/
1512 					switch (sec_ie[7]) {
1513 					case 0x1: /*WEP_40*/
1514 						psecuritypriv->XGrpPrivacy =
1515 								_WEP40_;
1516 						break;
1517 					case 0x2: /*TKIP*/
1518 						psecuritypriv->XGrpPrivacy =
1519 								_TKIP_;
1520 						break;
1521 					case 0x4: /*AESWRAP*/
1522 						psecuritypriv->XGrpPrivacy =
1523 								_AES_;
1524 						break;
1525 					case 0x5: /*WEP_104*/
1526 						psecuritypriv->XGrpPrivacy =
1527 								_WEP104_;
1528 						break;
1529 					default: /*one*/
1530 						psecuritypriv->XGrpPrivacy =
1531 								_NO_PRIVACY_;
1532 						break;
1533 					}
1534 				} else {
1535 					match = false;
1536 					break;
1537 				}
1538 				if (sec_ie[8] == 0x01) {
1539 					/*check the unicast encryption type*/
1540 					if (memcmp(&sec_ie[10],
1541 						     &uncst_oui[0], 4)) {
1542 						match = false;
1543 						break;
1544 					} /*else the uncst_oui is match*/
1545 				} else { /*mixed mode, unicast_enc_type > 1*/
1546 					/*select the uncst_oui and remove the
1547 					 * other uncst_oui*/
1548 					cnt = sec_ie[8];
1549 					remove_cnt = (cnt-1)*4;
1550 					sec_ie[8] = 0x01;
1551 					memcpy(&sec_ie[10], &uncst_oui[0], 4);
1552 					/*remove the other unicast suit*/
1553 					memcpy(&sec_ie[14],
1554 						&sec_ie[14 + remove_cnt],
1555 						(sec_ie[1] - 14 + 2 -
1556 						remove_cnt));
1557 					sec_ie[1] = sec_ie[1]-remove_cnt;
1558 				}
1559 				break;
1560 			}
1561 		}
1562 	}
1563 	if ((authmode == _WPA_IE_ID_) || (authmode == _WPA2_IE_ID_)) {
1564 		/*copy fixed ie*/
1565 		memcpy(out_ie, in_ie, 12);
1566 		ielength = 12;
1567 		/*copy RSN or SSN*/
1568 		if (match == true) {
1569 			memcpy(&out_ie[ielength], &sec_ie[0], sec_ie[1]+2);
1570 			ielength += sec_ie[1] + 2;
1571 			if (authmode == _WPA2_IE_ID_) {
1572 				/*the Pre-Authentication bit should be zero*/
1573 				out_ie[ielength - 1] = 0;
1574 				out_ie[ielength - 2] = 0;
1575 			}
1576 			r8712_report_sec_ie(adapter, authmode, sec_ie);
1577 		}
1578 	} else {
1579 		/*copy fixed ie only*/
1580 		memcpy(out_ie, in_ie, 12);
1581 		ielength = 12;
1582 		if (psecuritypriv->wps_phase == true) {
1583 			memcpy(out_ie+ielength, psecuritypriv->wps_ie,
1584 				psecuritypriv->wps_ie_len);
1585 			ielength += psecuritypriv->wps_ie_len;
1586 		}
1587 	}
1588 	iEntry = SecIsInPMKIDList(adapter, pmlmepriv->assoc_bssid);
1589 	if (iEntry < 0)
1590 		return ielength;
1591 	else {
1592 		if (authmode == _WPA2_IE_ID_) {
1593 			out_ie[ielength] = 1;
1594 			ielength++;
1595 			out_ie[ielength] = 0;	/*PMKID count = 0x0100*/
1596 			ielength++;
1597 			memcpy(&out_ie[ielength],
1598 				&psecuritypriv->PMKIDList[iEntry].PMKID, 16);
1599 			ielength += 16;
1600 			out_ie[13] += 18;/*PMKID length = 2+16*/
1601 		}
1602 	}
1603 	return ielength;
1604 }
1605 
r8712_init_registrypriv_dev_network(struct _adapter * adapter)1606 void r8712_init_registrypriv_dev_network(struct _adapter *adapter)
1607 {
1608 	struct registry_priv *pregistrypriv = &adapter->registrypriv;
1609 	struct eeprom_priv *peepriv = &adapter->eeprompriv;
1610 	struct wlan_bssid_ex *pdev_network = &pregistrypriv->dev_network;
1611 	u8 *myhwaddr = myid(peepriv);
1612 
1613 	memcpy(pdev_network->MacAddress, myhwaddr, ETH_ALEN);
1614 	memcpy(&pdev_network->Ssid, &pregistrypriv->ssid,
1615 		sizeof(struct ndis_802_11_ssid));
1616 	pdev_network->Configuration.Length =
1617 			 sizeof(struct NDIS_802_11_CONFIGURATION);
1618 	pdev_network->Configuration.BeaconPeriod = 100;
1619 	pdev_network->Configuration.FHConfig.Length = 0;
1620 	pdev_network->Configuration.FHConfig.HopPattern = 0;
1621 	pdev_network->Configuration.FHConfig.HopSet = 0;
1622 	pdev_network->Configuration.FHConfig.DwellTime = 0;
1623 }
1624 
r8712_update_registrypriv_dev_network(struct _adapter * adapter)1625 void r8712_update_registrypriv_dev_network(struct _adapter *adapter)
1626 {
1627 	int sz = 0;
1628 	struct registry_priv	*pregistrypriv = &adapter->registrypriv;
1629 	struct wlan_bssid_ex	*pdev_network = &pregistrypriv->dev_network;
1630 	struct security_priv	*psecuritypriv = &adapter->securitypriv;
1631 	struct wlan_network	*cur_network = &adapter->mlmepriv.cur_network;
1632 
1633 	pdev_network->Privacy = cpu_to_le32(psecuritypriv->PrivacyAlgrthm
1634 					    > 0 ? 1 : 0) ; /* adhoc no 802.1x */
1635 	pdev_network->Rssi = 0;
1636 	switch (pregistrypriv->wireless_mode) {
1637 	case WIRELESS_11B:
1638 		pdev_network->NetworkTypeInUse = cpu_to_le32(Ndis802_11DS);
1639 		break;
1640 	case WIRELESS_11G:
1641 	case WIRELESS_11BG:
1642 		pdev_network->NetworkTypeInUse = cpu_to_le32(Ndis802_11OFDM24);
1643 		break;
1644 	case WIRELESS_11A:
1645 		pdev_network->NetworkTypeInUse = cpu_to_le32(Ndis802_11OFDM5);
1646 		break;
1647 	default:
1648 		/* TODO */
1649 		break;
1650 	}
1651 	pdev_network->Configuration.DSConfig = cpu_to_le32(
1652 					       pregistrypriv->channel);
1653 	if (cur_network->network.InfrastructureMode == Ndis802_11IBSS)
1654 		pdev_network->Configuration.ATIMWindow = cpu_to_le32(3);
1655 	pdev_network->InfrastructureMode = cpu_to_le32(
1656 				cur_network->network.InfrastructureMode);
1657 	/* 1. Supported rates
1658 	 * 2. IE
1659 	 */
1660 	sz = r8712_generate_ie(pregistrypriv, adapter);
1661 	pdev_network->IELength = sz;
1662 	pdev_network->Length = r8712_get_ndis_wlan_bssid_ex_sz(
1663 			      (struct ndis_wlan_bssid_ex *)pdev_network);
1664 }
1665 
1666 /*the function is at passive_level*/
r8712_joinbss_reset(struct _adapter * padapter)1667 void r8712_joinbss_reset(struct _adapter *padapter)
1668 {
1669 	int i;
1670 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
1671 	struct ht_priv		*phtpriv = &pmlmepriv->htpriv;
1672 
1673 	/* todo: if you want to do something io/reg/hw setting before join_bss,
1674 	 * please add code here */
1675 	phtpriv->ampdu_enable = false;/*reset to disabled*/
1676 	for (i = 0; i < 16; i++)
1677 		phtpriv->baddbareq_issued[i] = false;/*reset it*/
1678 	if (phtpriv->ht_option) {
1679 		/* validate  usb rx aggregation */
1680 		r8712_write8(padapter, 0x102500D9, 48);/*TH = 48 pages, 6k*/
1681 	} else {
1682 		/* invalidate  usb rx aggregation */
1683 		/* TH=1 => means that invalidate usb rx aggregation */
1684 		r8712_write8(padapter, 0x102500D9, 1);
1685 	}
1686 }
1687 
1688 /*the function is >= passive_level*/
r8712_restructure_ht_ie(struct _adapter * padapter,u8 * in_ie,u8 * out_ie,uint in_len,uint * pout_len)1689 unsigned int r8712_restructure_ht_ie(struct _adapter *padapter, u8 *in_ie,
1690 				     u8 *out_ie, uint in_len, uint *pout_len)
1691 {
1692 	u32 ielen, out_len;
1693 	unsigned char *p, *pframe;
1694 	struct ieee80211_ht_cap ht_capie;
1695 	unsigned char WMM_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01, 0x00};
1696 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1697 	struct qos_priv *pqospriv = &pmlmepriv->qospriv;
1698 	struct ht_priv *phtpriv = &pmlmepriv->htpriv;
1699 
1700 	phtpriv->ht_option = 0;
1701 	p = r8712_get_ie(in_ie+12, _HT_CAPABILITY_IE_, &ielen, in_len-12);
1702 	if (p && (ielen > 0)) {
1703 		if (pqospriv->qos_option == 0) {
1704 			out_len = *pout_len;
1705 			pframe = r8712_set_ie(out_ie+out_len,
1706 					      _VENDOR_SPECIFIC_IE_,
1707 					      _WMM_IE_Length_,
1708 					       WMM_IE, pout_len);
1709 			pqospriv->qos_option = 1;
1710 		}
1711 		out_len = *pout_len;
1712 		memset(&ht_capie, 0, sizeof(struct ieee80211_ht_cap));
1713 		ht_capie.cap_info = IEEE80211_HT_CAP_SUP_WIDTH |
1714 				    IEEE80211_HT_CAP_SGI_20 |
1715 				    IEEE80211_HT_CAP_SGI_40 |
1716 				    IEEE80211_HT_CAP_TX_STBC |
1717 				    IEEE80211_HT_CAP_MAX_AMSDU |
1718 				    IEEE80211_HT_CAP_DSSSCCK40;
1719 		ht_capie.ampdu_params_info = (IEEE80211_HT_CAP_AMPDU_FACTOR &
1720 				0x03) | (IEEE80211_HT_CAP_AMPDU_DENSITY & 0x00);
1721 		pframe = r8712_set_ie(out_ie+out_len, _HT_CAPABILITY_IE_,
1722 				sizeof(struct ieee80211_ht_cap),
1723 				(unsigned char *)&ht_capie, pout_len);
1724 		phtpriv->ht_option = 1;
1725 	}
1726 	return phtpriv->ht_option;
1727 }
1728 
1729 /* the function is > passive_level (in critical_section) */
update_ht_cap(struct _adapter * padapter,u8 * pie,uint ie_len)1730 static void update_ht_cap(struct _adapter *padapter, u8 *pie, uint ie_len)
1731 {
1732 	u8 *p, max_ampdu_sz;
1733 	int i, len;
1734 	struct sta_info *bmc_sta, *psta;
1735 	struct ieee80211_ht_cap *pht_capie;
1736 	struct ieee80211_ht_addt_info *pht_addtinfo;
1737 	struct recv_reorder_ctrl *preorder_ctrl;
1738 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1739 	struct ht_priv *phtpriv = &pmlmepriv->htpriv;
1740 	struct registry_priv *pregistrypriv = &padapter->registrypriv;
1741 	struct wlan_network *pcur_network = &(pmlmepriv->cur_network);
1742 
1743 	if (!phtpriv->ht_option)
1744 		return;
1745 	/* maybe needs check if ap supports rx ampdu. */
1746 	if ((phtpriv->ampdu_enable == false) &&
1747 	    (pregistrypriv->ampdu_enable == 1))
1748 		phtpriv->ampdu_enable = true;
1749 	/*check Max Rx A-MPDU Size*/
1750 	len = 0;
1751 	p = r8712_get_ie(pie + sizeof(struct NDIS_802_11_FIXED_IEs),
1752 				_HT_CAPABILITY_IE_,
1753 				&len, ie_len -
1754 				sizeof(struct NDIS_802_11_FIXED_IEs));
1755 	if (p && len > 0) {
1756 		pht_capie = (struct ieee80211_ht_cap *)(p+2);
1757 		max_ampdu_sz = (pht_capie->ampdu_params_info &
1758 				IEEE80211_HT_CAP_AMPDU_FACTOR);
1759 		/* max_ampdu_sz (kbytes); */
1760 		max_ampdu_sz = 1 << (max_ampdu_sz+3);
1761 		phtpriv->rx_ampdu_maxlen = max_ampdu_sz;
1762 	}
1763 	/* for A-MPDU Rx reordering buffer control for bmc_sta & sta_info
1764 	 * if A-MPDU Rx is enabled, reseting rx_ordering_ctrl
1765 	 * wstart_b(indicate_seq) to default value=0xffff
1766 	 * todo: check if AP can send A-MPDU packets
1767 	 */
1768 	bmc_sta = r8712_get_bcmc_stainfo(padapter);
1769 	if (bmc_sta) {
1770 		for (i = 0; i < 16; i++) {
1771 			preorder_ctrl = &bmc_sta->recvreorder_ctrl[i];
1772 			preorder_ctrl->indicate_seq = 0xffff;
1773 			preorder_ctrl->wend_b = 0xffff;
1774 		}
1775 	}
1776 	psta = r8712_get_stainfo(&padapter->stapriv,
1777 				 pcur_network->network.MacAddress);
1778 	if (psta) {
1779 		for (i = 0; i < 16 ; i++) {
1780 			preorder_ctrl = &psta->recvreorder_ctrl[i];
1781 			preorder_ctrl->indicate_seq = 0xffff;
1782 			preorder_ctrl->wend_b = 0xffff;
1783 		}
1784 	}
1785 	len = 0;
1786 	p = r8712_get_ie(pie + sizeof(struct NDIS_802_11_FIXED_IEs),
1787 		   _HT_ADD_INFO_IE_, &len,
1788 		   ie_len-sizeof(struct NDIS_802_11_FIXED_IEs));
1789 	if (p && len > 0)
1790 		pht_addtinfo = (struct ieee80211_ht_addt_info *)(p + 2);
1791 }
1792 
r8712_issue_addbareq_cmd(struct _adapter * padapter,int priority)1793 void r8712_issue_addbareq_cmd(struct _adapter *padapter, int priority)
1794 {
1795 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1796 	struct ht_priv	 *phtpriv = &pmlmepriv->htpriv;
1797 
1798 	if ((phtpriv->ht_option == 1) && (phtpriv->ampdu_enable == true)) {
1799 		if (phtpriv->baddbareq_issued[priority] == false) {
1800 			r8712_addbareq_cmd(padapter, (u8)priority);
1801 			phtpriv->baddbareq_issued[priority] = true;
1802 		}
1803 	}
1804 }
1805 
1806 /*the function is >= passive_level*/
r8712_add_ht_addt_info(struct _adapter * padapter,u8 * in_ie,u8 * out_ie,uint in_len,uint * pout_len)1807 unsigned int r8712_add_ht_addt_info(struct _adapter *padapter,
1808 			      u8 *in_ie, u8 *out_ie,
1809 			      uint in_len, uint *pout_len)
1810 {
1811 	u32 ielen, out_len =  0;
1812 	unsigned char *p, *pframe;
1813 	struct ieee80211_ht_addt_info ht_addt_info;
1814 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1815 	struct ht_priv *phtpriv = &pmlmepriv->htpriv;
1816 	struct registry_priv *pregistrypriv = &padapter->registrypriv;
1817 	out_len = *pout_len;
1818 
1819 	if (pregistrypriv->ht_enable == 1) {
1820 		p = r8712_get_ie(in_ie+12, _HT_ADD_INFO_IE_,
1821 				 &ielen, in_len - 12);
1822 		if (p && (ielen > 0)) {
1823 			; /* dummy branch */
1824 		} else {
1825 			if (p == NULL) {
1826 				int sz = sizeof(struct ieee80211_ht_addt_info);
1827 				memset(&ht_addt_info, 0, sz);
1828 				/*need to add the HT additional IEs*/
1829 				ht_addt_info.control_chan =
1830 						pregistrypriv->channel;
1831 				pframe = r8712_set_ie(out_ie + out_len,
1832 						_HT_ADD_INFO_IE_,
1833 						sz,
1834 						(unsigned char *)&ht_addt_info,
1835 						pout_len);
1836 			}
1837 		}
1838 	}
1839 	return phtpriv->ht_option;
1840 }
1841