• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2011 Realtek Corporation. */
3 
4 #define _RTW_MLME_C_
5 
6 #include "../include/osdep_service.h"
7 #include "../include/drv_types.h"
8 #include "../include/recv_osdep.h"
9 #include "../include/xmit_osdep.h"
10 #include "../include/hal_intf.h"
11 #include "../include/mlme_osdep.h"
12 #include "../include/sta_info.h"
13 #include "../include/wifi.h"
14 #include "../include/wlan_bssdef.h"
15 #include "../include/rtw_ioctl_set.h"
16 #include "../include/usb_osintf.h"
17 
18 extern unsigned char	MCS_rate_2R[16];
19 extern unsigned char	MCS_rate_1R[16];
20 
rtw_set_roaming(struct adapter * adapter,u8 to_roaming)21 void rtw_set_roaming(struct adapter *adapter, u8 to_roaming)
22 {
23 	if (to_roaming == 0)
24 		adapter->mlmepriv.to_join = false;
25 	adapter->mlmepriv.to_roaming = to_roaming;
26 }
27 
rtw_to_roaming(struct adapter * adapter)28 u8 rtw_to_roaming(struct adapter *adapter)
29 {
30 	return adapter->mlmepriv.to_roaming;
31 }
32 
_rtw_init_mlme_priv(struct adapter * padapter)33 int _rtw_init_mlme_priv(struct adapter *padapter)
34 {
35 	int	i;
36 	u8	*pbuf;
37 	struct wlan_network	*pnetwork;
38 	struct mlme_priv		*pmlmepriv = &padapter->mlmepriv;
39 	int	res = _SUCCESS;
40 
41 	/*  We don't need to memset padapter->XXX to zero, because adapter is allocated by vzalloc(). */
42 
43 	pmlmepriv->nic_hdl = (u8 *)padapter;
44 
45 	pmlmepriv->pscanned = NULL;
46 	pmlmepriv->fw_state = 0;
47 	pmlmepriv->cur_network.network.InfrastructureMode = Ndis802_11AutoUnknown;
48 	pmlmepriv->scan_mode = SCAN_ACTIVE;/*  1: active, 0: pasive. Maybe someday we should rename this varable to "active_mode" (Jeff) */
49 
50 	spin_lock_init(&pmlmepriv->lock);
51 	_rtw_init_queue(&pmlmepriv->free_bss_pool);
52 	_rtw_init_queue(&pmlmepriv->scanned_queue);
53 
54 	set_scanned_network_val(pmlmepriv, 0);
55 
56 	memset(&pmlmepriv->assoc_ssid, 0, sizeof(struct ndis_802_11_ssid));
57 
58 	pbuf = vzalloc(MAX_BSS_CNT * (sizeof(struct wlan_network)));
59 
60 	if (!pbuf) {
61 		res = _FAIL;
62 		goto exit;
63 	}
64 	pmlmepriv->free_bss_buf = pbuf;
65 
66 	pnetwork = (struct wlan_network *)pbuf;
67 
68 	for (i = 0; i < MAX_BSS_CNT; i++) {
69 		INIT_LIST_HEAD(&pnetwork->list);
70 
71 		list_add_tail(&pnetwork->list, &pmlmepriv->free_bss_pool.queue);
72 
73 		pnetwork++;
74 	}
75 
76 	/* allocate DMA-able/Non-Page memory for cmd_buf and rsp_buf */
77 
78 	rtw_clear_scan_deny(padapter);
79 
80 	rtw_init_mlme_timer(padapter);
81 
82 exit:
83 
84 	return res;
85 }
86 
rtw_mfree_mlme_priv_lock(struct mlme_priv * pmlmepriv)87 static void rtw_mfree_mlme_priv_lock(struct mlme_priv *pmlmepriv)
88 {
89 }
90 
91 #if defined(CONFIG_88EU_AP_MODE)
rtw_free_mlme_ie_data(u8 ** ppie,u32 * plen)92 static void rtw_free_mlme_ie_data(u8 **ppie, u32 *plen)
93 {
94 	kfree(*ppie);
95 	*plen = 0;
96 	*ppie = NULL;
97 }
98 
rtw_free_mlme_priv_ie_data(struct mlme_priv * pmlmepriv)99 void rtw_free_mlme_priv_ie_data(struct mlme_priv *pmlmepriv)
100 {
101 	kfree(pmlmepriv->assoc_req);
102 	kfree(pmlmepriv->assoc_rsp);
103 	rtw_free_mlme_ie_data(&pmlmepriv->wps_beacon_ie, &pmlmepriv->wps_beacon_ie_len);
104 	rtw_free_mlme_ie_data(&pmlmepriv->wps_probe_req_ie, &pmlmepriv->wps_probe_req_ie_len);
105 	rtw_free_mlme_ie_data(&pmlmepriv->wps_probe_resp_ie, &pmlmepriv->wps_probe_resp_ie_len);
106 	rtw_free_mlme_ie_data(&pmlmepriv->wps_assoc_resp_ie, &pmlmepriv->wps_assoc_resp_ie_len);
107 
108 	rtw_free_mlme_ie_data(&pmlmepriv->p2p_beacon_ie, &pmlmepriv->p2p_beacon_ie_len);
109 	rtw_free_mlme_ie_data(&pmlmepriv->p2p_probe_req_ie, &pmlmepriv->p2p_probe_req_ie_len);
110 	rtw_free_mlme_ie_data(&pmlmepriv->p2p_probe_resp_ie, &pmlmepriv->p2p_probe_resp_ie_len);
111 	rtw_free_mlme_ie_data(&pmlmepriv->p2p_go_probe_resp_ie, &pmlmepriv->p2p_go_probe_resp_ie_len);
112 	rtw_free_mlme_ie_data(&pmlmepriv->p2p_assoc_req_ie, &pmlmepriv->p2p_assoc_req_ie_len);
113 }
114 #else
rtw_free_mlme_priv_ie_data(struct mlme_priv * pmlmepriv)115 void rtw_free_mlme_priv_ie_data(struct mlme_priv *pmlmepriv)
116 {
117 }
118 #endif
119 
_rtw_free_mlme_priv(struct mlme_priv * pmlmepriv)120 void _rtw_free_mlme_priv(struct mlme_priv *pmlmepriv)
121 {
122 
123 	rtw_free_mlme_priv_ie_data(pmlmepriv);
124 
125 	if (pmlmepriv) {
126 		rtw_mfree_mlme_priv_lock(pmlmepriv);
127 
128 		vfree(pmlmepriv->free_bss_buf);
129 	}
130 
131 }
132 
_rtw_enqueue_network(struct __queue * queue,struct wlan_network * pnetwork)133 int	_rtw_enqueue_network(struct __queue *queue, struct wlan_network *pnetwork)
134 {
135 
136 	if (!pnetwork)
137 		goto exit;
138 
139 	spin_lock_bh(&queue->lock);
140 
141 	list_add_tail(&pnetwork->list, &queue->queue);
142 
143 	spin_unlock_bh(&queue->lock);
144 
145 exit:
146 
147 	return _SUCCESS;
148 }
149 
_rtw_dequeue_network(struct __queue * queue)150 struct	wlan_network *_rtw_dequeue_network(struct __queue *queue)
151 {
152 	struct wlan_network *pnetwork;
153 
154 	spin_lock_bh(&queue->lock);
155 
156 	if (list_empty(&queue->queue)) {
157 		pnetwork = NULL;
158 	} else {
159 		pnetwork = container_of((&queue->queue)->next, struct wlan_network, list);
160 
161 		list_del_init(&pnetwork->list);
162 	}
163 
164 	spin_unlock_bh(&queue->lock);
165 
166 	return pnetwork;
167 }
168 
_rtw_alloc_network(struct mlme_priv * pmlmepriv)169 struct	wlan_network *_rtw_alloc_network(struct	mlme_priv *pmlmepriv)/* _queue *free_queue) */
170 {
171 	struct	wlan_network	*pnetwork;
172 	struct __queue *free_queue = &pmlmepriv->free_bss_pool;
173 	struct list_head *plist = NULL;
174 
175 	spin_lock_bh(&free_queue->lock);
176 
177 	if (list_empty(&free_queue->queue)) {
178 		pnetwork = NULL;
179 		goto exit;
180 	}
181 	plist = (&free_queue->queue)->next;
182 
183 	pnetwork = container_of(plist, struct wlan_network, list);
184 
185 	list_del_init(&pnetwork->list);
186 
187 	pnetwork->network_type = 0;
188 	pnetwork->fixed = false;
189 	pnetwork->last_scanned = jiffies;
190 	pnetwork->aid = 0;
191 	pnetwork->join_res = 0;
192 
193 	pmlmepriv->num_of_scanned++;
194 
195 exit:
196 	spin_unlock_bh(&free_queue->lock);
197 
198 	return pnetwork;
199 }
200 
_rtw_free_network(struct mlme_priv * pmlmepriv,struct wlan_network * pnetwork,u8 isfreeall)201 void _rtw_free_network(struct mlme_priv *pmlmepriv, struct wlan_network *pnetwork, u8 isfreeall)
202 {
203 	u32 curr_time, delta_time;
204 	u32 lifetime = SCANQUEUE_LIFETIME;
205 	struct __queue *free_queue = &pmlmepriv->free_bss_pool;
206 
207 	if (!pnetwork)
208 		return;
209 
210 	if (pnetwork->fixed)
211 		return;
212 	curr_time = jiffies;
213 	if ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) ||
214 	    (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)))
215 		lifetime = 1;
216 	if (!isfreeall) {
217 		delta_time = (curr_time - pnetwork->last_scanned) / HZ;
218 		if (delta_time < lifetime)/*  unit:sec */
219 			return;
220 	}
221 	spin_lock_bh(&free_queue->lock);
222 	list_del_init(&pnetwork->list);
223 	list_add_tail(&pnetwork->list, &free_queue->queue);
224 	pmlmepriv->num_of_scanned--;
225 	spin_unlock_bh(&free_queue->lock);
226 }
227 
_rtw_free_network_nolock(struct mlme_priv * pmlmepriv,struct wlan_network * pnetwork)228 void _rtw_free_network_nolock(struct	mlme_priv *pmlmepriv, struct wlan_network *pnetwork)
229 {
230 	struct __queue *free_queue = &pmlmepriv->free_bss_pool;
231 
232 	if (!pnetwork)
233 		return;
234 	if (pnetwork->fixed)
235 		return;
236 	list_del_init(&pnetwork->list);
237 	list_add_tail(&pnetwork->list, get_list_head(free_queue));
238 	pmlmepriv->num_of_scanned--;
239 }
240 
241 /*
242 	return the wlan_network with the matching addr
243 
244 	Shall be calle under atomic context... to avoid possible racing condition...
245 */
_rtw_find_network(struct __queue * scanned_queue,u8 * addr)246 struct wlan_network *_rtw_find_network(struct __queue *scanned_queue, u8 *addr)
247 {
248 	struct list_head *phead, *plist;
249 	struct	wlan_network *pnetwork = NULL;
250 	u8 zero_addr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
251 
252 	if (!memcmp(zero_addr, addr, ETH_ALEN)) {
253 		pnetwork = NULL;
254 		goto exit;
255 	}
256 	phead = get_list_head(scanned_queue);
257 	plist = phead->next;
258 
259 	while (plist != phead) {
260 		pnetwork = container_of(plist, struct wlan_network, list);
261 		if (!memcmp(addr, pnetwork->network.MacAddress, ETH_ALEN))
262 			break;
263 		plist = plist->next;
264 	}
265 	if (plist == phead)
266 		pnetwork = NULL;
267 exit:
268 
269 	return pnetwork;
270 }
271 
_rtw_free_network_queue(struct adapter * padapter,u8 isfreeall)272 void _rtw_free_network_queue(struct adapter *padapter, u8 isfreeall)
273 {
274 	struct list_head *phead, *plist;
275 	struct wlan_network *pnetwork;
276 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
277 	struct __queue *scanned_queue = &pmlmepriv->scanned_queue;
278 
279 	spin_lock_bh(&scanned_queue->lock);
280 
281 	phead = get_list_head(scanned_queue);
282 	plist = phead->next;
283 
284 	while (phead != plist) {
285 		pnetwork = container_of(plist, struct wlan_network, list);
286 
287 		plist = plist->next;
288 
289 		_rtw_free_network(pmlmepriv, pnetwork, isfreeall);
290 	}
291 	spin_unlock_bh(&scanned_queue->lock);
292 
293 }
294 
rtw_if_up(struct adapter * padapter)295 int rtw_if_up(struct adapter *padapter)
296 {
297 	int res;
298 
299 	if (padapter->bDriverStopped || padapter->bSurpriseRemoved ||
300 	    !check_fwstate(&padapter->mlmepriv, _FW_LINKED))
301 		res = false;
302 	else
303 		res =  true;
304 
305 	return res;
306 }
307 
rtw_generate_random_ibss(u8 * pibss)308 void rtw_generate_random_ibss(u8 *pibss)
309 {
310 	u32	curtime = jiffies;
311 
312 	pibss[0] = 0x02;  /* in ad-hoc mode bit1 must set to 1 */
313 	pibss[1] = 0x11;
314 	pibss[2] = 0x87;
315 	pibss[3] = (u8)(curtime & 0xff);/* p[0]; */
316 	pibss[4] = (u8)((curtime >> 8) & 0xff);/* p[1]; */
317 	pibss[5] = (u8)((curtime >> 16) & 0xff);/* p[2]; */
318 }
319 
rtw_get_capability_from_ie(u8 * ie)320 u8 *rtw_get_capability_from_ie(u8 *ie)
321 {
322 	return ie + 8 + 2;
323 }
324 
rtw_get_capability(struct wlan_bssid_ex * bss)325 u16 rtw_get_capability(struct wlan_bssid_ex *bss)
326 {
327 	__le16	val;
328 
329 	memcpy((u8 *)&val, rtw_get_capability_from_ie(bss->IEs), 2);
330 
331 	return le16_to_cpu(val);
332 }
333 
rtw_get_timestampe_from_ie(u8 * ie)334 u8 *rtw_get_timestampe_from_ie(u8 *ie)
335 {
336 	return ie + 0;
337 }
338 
rtw_get_beacon_interval_from_ie(u8 * ie)339 u8 *rtw_get_beacon_interval_from_ie(u8 *ie)
340 {
341 	return ie + 8;
342 }
343 
rtw_init_mlme_priv(struct adapter * padapter)344 int rtw_init_mlme_priv(struct adapter *padapter)/* struct	mlme_priv *pmlmepriv) */
345 {
346 	int	res;
347 
348 	res = _rtw_init_mlme_priv(padapter);/*  (pmlmepriv); */
349 
350 	return res;
351 }
352 
rtw_free_mlme_priv(struct mlme_priv * pmlmepriv)353 void rtw_free_mlme_priv(struct mlme_priv *pmlmepriv)
354 {
355 	_rtw_free_mlme_priv(pmlmepriv);
356 }
357 
rtw_alloc_network(struct mlme_priv * pmlmepriv)358 static struct wlan_network *rtw_alloc_network(struct mlme_priv *pmlmepriv)
359 {
360 	struct	wlan_network	*pnetwork;
361 
362 	pnetwork = _rtw_alloc_network(pmlmepriv);
363 
364 	return pnetwork;
365 }
366 
rtw_free_network_nolock(struct mlme_priv * pmlmepriv,struct wlan_network * pnetwork)367 static void rtw_free_network_nolock(struct mlme_priv *pmlmepriv,
368 				    struct wlan_network *pnetwork)
369 {
370 
371 	_rtw_free_network_nolock(pmlmepriv, pnetwork);
372 
373 }
374 
rtw_free_network_queue(struct adapter * dev,u8 isfreeall)375 void rtw_free_network_queue(struct adapter *dev, u8 isfreeall)
376 {
377 
378 	_rtw_free_network_queue(dev, isfreeall);
379 
380 }
381 
382 /*
383 	return the wlan_network with the matching addr
384 
385 	Shall be calle under atomic context... to avoid possible racing condition...
386 */
rtw_find_network(struct __queue * scanned_queue,u8 * addr)387 struct	wlan_network *rtw_find_network(struct __queue *scanned_queue, u8 *addr)
388 {
389 	struct	wlan_network *pnetwork = _rtw_find_network(scanned_queue, addr);
390 
391 	return pnetwork;
392 }
393 
rtw_is_same_ibss(struct adapter * adapter,struct wlan_network * pnetwork)394 int rtw_is_same_ibss(struct adapter *adapter, struct wlan_network *pnetwork)
395 {
396 	int ret = true;
397 	struct security_priv *psecuritypriv = &adapter->securitypriv;
398 
399 	if ((psecuritypriv->dot11PrivacyAlgrthm != _NO_PRIVACY_) &&
400 	    (pnetwork->network.Privacy == 0))
401 		ret = false;
402 	else if ((psecuritypriv->dot11PrivacyAlgrthm == _NO_PRIVACY_) &&
403 		 (pnetwork->network.Privacy == 1))
404 		ret = false;
405 	else
406 		ret = true;
407 	return ret;
408 }
409 
is_same_ess(struct wlan_bssid_ex * a,struct wlan_bssid_ex * b)410 static int is_same_ess(struct wlan_bssid_ex *a, struct wlan_bssid_ex *b)
411 {
412 	return (a->Ssid.SsidLength == b->Ssid.SsidLength) &&
413 	       !memcmp(a->Ssid.Ssid, b->Ssid.Ssid, a->Ssid.SsidLength);
414 }
415 
is_same_network(struct wlan_bssid_ex * src,struct wlan_bssid_ex * dst)416 int is_same_network(struct wlan_bssid_ex *src, struct wlan_bssid_ex *dst)
417 {
418 	u16 s_cap, d_cap;
419 	__le16 le_scap, le_dcap;
420 
421 	memcpy((u8 *)&le_scap, rtw_get_capability_from_ie(src->IEs), 2);
422 	memcpy((u8 *)&le_dcap, rtw_get_capability_from_ie(dst->IEs), 2);
423 
424 	s_cap = le16_to_cpu(le_scap);
425 	d_cap = le16_to_cpu(le_dcap);
426 
427 	return ((src->Ssid.SsidLength == dst->Ssid.SsidLength) &&
428 		((!memcmp(src->MacAddress, dst->MacAddress, ETH_ALEN))) &&
429 		((!memcmp(src->Ssid.Ssid, dst->Ssid.Ssid, src->Ssid.SsidLength))) &&
430 		((s_cap & WLAN_CAPABILITY_IBSS) ==
431 		(d_cap & WLAN_CAPABILITY_IBSS)) &&
432 		((s_cap & WLAN_CAPABILITY_BSS) ==
433 		(d_cap & WLAN_CAPABILITY_BSS)));
434 }
435 
rtw_get_oldest_wlan_network(struct __queue * scanned_queue)436 struct	wlan_network	*rtw_get_oldest_wlan_network(struct __queue *scanned_queue)
437 {
438 	struct list_head *plist, *phead;
439 	struct	wlan_network	*pwlan = NULL;
440 	struct	wlan_network	*oldest = NULL;
441 
442 	phead = get_list_head(scanned_queue);
443 
444 	plist = phead->next;
445 
446 	while (1) {
447 		if (phead == plist)
448 			break;
449 
450 		pwlan = container_of(plist, struct wlan_network, list);
451 
452 		if (!pwlan->fixed) {
453 			if (!oldest || time_after(oldest->last_scanned, pwlan->last_scanned))
454 				oldest = pwlan;
455 		}
456 
457 		plist = plist->next;
458 	}
459 
460 	return oldest;
461 }
462 
update_network(struct wlan_bssid_ex * dst,struct wlan_bssid_ex * src,struct adapter * padapter,bool update_ie)463 void update_network(struct wlan_bssid_ex *dst, struct wlan_bssid_ex *src,
464 	struct adapter *padapter, bool update_ie)
465 {
466 	long rssi_ori = dst->Rssi;
467 	u8 sq_smp = src->PhyInfo.SignalQuality;
468 	u8 ss_final;
469 	u8 sq_final;
470 	long rssi_final;
471 
472 	rtw_hal_antdiv_rssi_compared(padapter, dst, src); /* this will update src.Rssi, need consider again */
473 
474 	/* The rule below is 1/5 for sample value, 4/5 for history value */
475 	if (check_fwstate(&padapter->mlmepriv, _FW_LINKED) && is_same_network(&padapter->mlmepriv.cur_network.network, src)) {
476 		/* Take the recvpriv's value for the connected AP*/
477 		ss_final = padapter->recvpriv.signal_strength;
478 		sq_final = padapter->recvpriv.signal_qual;
479 		/* the rssi value here is undecorated, and will be used for antenna diversity */
480 		if (sq_smp != 101) /* from the right channel */
481 			rssi_final = dst->Rssi; //(src->Rssi+dst->Rssi*4)/5;
482 		else
483 			rssi_final = rssi_ori;
484 	} else {
485 //		if (sq_smp != 101) { /* from the right channel */
486 			ss_final = (u32)dst->PhyInfo.SignalStrength; //((u32)(src->PhyInfo.SignalStrength)+(u32)(dst->PhyInfo.SignalStrength)*4)/5;
487 			sq_final = (u32)dst->PhyInfo.SignalQuality; //((u32)(src->PhyInfo.SignalQuality)+(u32)(dst->PhyInfo.SignalQuality)*4)/5;
488 			rssi_final = dst->Rssi; //(src->Rssi+dst->Rssi*4)/5;
489 //		} else {
490 //			/* bss info not receiving from the right channel, use the original RX signal infos */
491 //			ss_final = dst->PhyInfo.SignalStrength;
492 //			sq_final = dst->PhyInfo.SignalQuality;
493 //			rssi_final = dst->Rssi;
494 //		}
495 	}
496 	if (update_ie) {
497 		dst->Reserved[0] = src->Reserved[0];
498 		dst->Reserved[1] = src->Reserved[1];
499 		memcpy((u8 *)dst, (u8 *)src, get_wlan_bssid_ex_sz(src));
500 	}
501 	dst->PhyInfo.SignalStrength = ss_final;
502 	dst->PhyInfo.SignalQuality = sq_final;
503 	dst->Rssi = rssi_final;
504 
505 }
506 
update_current_network(struct adapter * adapter,struct wlan_bssid_ex * pnetwork)507 static void update_current_network(struct adapter *adapter, struct wlan_bssid_ex *pnetwork)
508 {
509 	struct	mlme_priv	*pmlmepriv = &adapter->mlmepriv;
510 
511 	if (check_fwstate(pmlmepriv, _FW_LINKED) &&
512 	    is_same_network(&pmlmepriv->cur_network.network, pnetwork)) {
513 		update_network(&pmlmepriv->cur_network.network, pnetwork, adapter, true);
514 		rtw_update_protection(adapter, (pmlmepriv->cur_network.network.IEs) + sizeof(struct ndis_802_11_fixed_ie),
515 				      pmlmepriv->cur_network.network.IELength);
516 	}
517 
518 }
519 
520 /*
521 Caller must hold pmlmepriv->lock first.
522 */
rtw_update_scanned_network(struct adapter * adapter,struct wlan_bssid_ex * target)523 void rtw_update_scanned_network(struct adapter *adapter, struct wlan_bssid_ex *target)
524 {
525 	struct list_head *plist, *phead;
526 	u32	bssid_ex_sz;
527 	struct mlme_priv	*pmlmepriv = &adapter->mlmepriv;
528 	struct __queue *queue	= &pmlmepriv->scanned_queue;
529 	struct wlan_network	*pnetwork = NULL;
530 	struct wlan_network	*oldest = NULL;
531 
532 	spin_lock_bh(&queue->lock);
533 	phead = get_list_head(queue);
534 	plist = phead->next;
535 
536 	while (phead != plist) {
537 		pnetwork	= container_of(plist, struct wlan_network, list);
538 
539 		if (is_same_network(&pnetwork->network, target))
540 			break;
541 		if ((oldest == ((struct wlan_network *)0)) ||
542 		    time_after(oldest->last_scanned, pnetwork->last_scanned))
543 			oldest = pnetwork;
544 		plist = plist->next;
545 	}
546 	/* If we didn't find a match, then get a new network slot to initialize
547 	 * with this beacon's information */
548 	if (phead == plist) {
549 		if (list_empty(&pmlmepriv->free_bss_pool.queue)) {
550 			/* If there are no more slots, expire the oldest */
551 			pnetwork = oldest;
552 
553 			rtw_hal_get_def_var(adapter, HAL_DEF_CURRENT_ANTENNA, &target->PhyInfo.Optimum_antenna);
554 			memcpy(&pnetwork->network, target,  get_wlan_bssid_ex_sz(target));
555 			/*  variable initialize */
556 			pnetwork->fixed = false;
557 			pnetwork->last_scanned = jiffies;
558 
559 			pnetwork->network_type = 0;
560 			pnetwork->aid = 0;
561 			pnetwork->join_res = 0;
562 
563 			/* bss info not receiving from the right channel */
564 			if (pnetwork->network.PhyInfo.SignalQuality == 101)
565 				pnetwork->network.PhyInfo.SignalQuality = 0;
566 		} else {
567 			/* Otherwise just pull from the free list */
568 
569 			pnetwork = rtw_alloc_network(pmlmepriv); /*  will update scan_time */
570 
571 			if (!pnetwork)
572 				goto exit;
573 
574 			bssid_ex_sz = get_wlan_bssid_ex_sz(target);
575 			target->Length = bssid_ex_sz;
576 			rtw_hal_get_def_var(adapter, HAL_DEF_CURRENT_ANTENNA, &target->PhyInfo.Optimum_antenna);
577 			memcpy(&pnetwork->network, target, bssid_ex_sz);
578 
579 			pnetwork->last_scanned = jiffies;
580 
581 			/* bss info not receiving from the right channel */
582 			if (pnetwork->network.PhyInfo.SignalQuality == 101)
583 				pnetwork->network.PhyInfo.SignalQuality = 0;
584 			list_add_tail(&pnetwork->list, &queue->queue);
585 		}
586 	} else {
587 		/* we have an entry and we are going to update it. But this entry may
588 		 * be already expired. In this case we do the same as we found a new
589 		 * net and call the new_net handler
590 		 */
591 		bool update_ie = true;
592 
593 		pnetwork->last_scanned = jiffies;
594 
595 		/* target.Reserved[0]== 1, means that scanned network is a bcn frame. */
596 		/* probe resp(3) > beacon(1) > probe req(2) */
597 		if ((target->Reserved[0] != 2) &&
598 		    (target->Reserved[0] >= pnetwork->network.Reserved[0]))
599 			update_ie = true;
600 		else
601 			update_ie = false;
602 		update_network(&pnetwork->network, target, adapter, update_ie);
603 	}
604 
605 exit:
606 	spin_unlock_bh(&queue->lock);
607 
608 }
609 
rtw_add_network(struct adapter * adapter,struct wlan_bssid_ex * pnetwork)610 static void rtw_add_network(struct adapter *adapter,
611 			    struct wlan_bssid_ex *pnetwork)
612 {
613 
614 #if defined(CONFIG_88EU_P2P)
615 	rtw_wlan_bssid_ex_remove_p2p_attr(pnetwork, P2P_ATTR_GROUP_INFO);
616 #endif
617 	update_current_network(adapter, pnetwork);
618 	rtw_update_scanned_network(adapter, pnetwork);
619 
620 }
621 
622 /* select the desired network based on the capability of the (i)bss. */
623 /*  check items:	(1) security */
624 /* 			(2) network_type */
625 /* 			(3) WMM */
626 /*			(4) HT */
627 /*			(5) others */
rtw_is_desired_network(struct adapter * adapter,struct wlan_network * pnetwork)628 static int rtw_is_desired_network(struct adapter *adapter, struct wlan_network *pnetwork)
629 {
630 	struct security_priv *psecuritypriv = &adapter->securitypriv;
631 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
632 	u32 desired_encmode;
633 	u32 privacy;
634 
635 	/* u8 wps_ie[512]; */
636 	uint wps_ielen;
637 
638 	int bselected = true;
639 
640 	desired_encmode = psecuritypriv->ndisencryptstatus;
641 	privacy = pnetwork->network.Privacy;
642 
643 	if (check_fwstate(pmlmepriv, WIFI_UNDER_WPS)) {
644 		if (rtw_get_wps_ie(pnetwork->network.IEs + _FIXED_IE_LENGTH_, pnetwork->network.IELength - _FIXED_IE_LENGTH_, NULL, &wps_ielen))
645 			return true;
646 		else
647 			return false;
648 	}
649 	if (adapter->registrypriv.wifi_spec == 1) { /* for  correct flow of 8021X  to do.... */
650 		u8 *p = NULL;
651 		uint ie_len = 0;
652 
653 		if ((desired_encmode == Ndis802_11EncryptionDisabled) && (privacy != 0))
654 			bselected = false;
655 		if (psecuritypriv->ndisauthtype == Ndis802_11AuthModeWPA2PSK) {
656 			p = rtw_get_ie(pnetwork->network.IEs + _BEACON_IE_OFFSET_,
657 				       _RSN_IE_2_, &ie_len,
658 				       (pnetwork->network.IELength -
659 				       _BEACON_IE_OFFSET_));
660 			if (p && ie_len > 0)
661 				bselected = true;
662 			else
663 				bselected = false;
664 		}
665 	}
666 
667 	if ((desired_encmode != Ndis802_11EncryptionDisabled) && (privacy == 0)) {
668 		DBG_88E("desired_encmode: %d, privacy: %d\n", desired_encmode, privacy);
669 		bselected = false;
670 	}
671 
672 	if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
673 		if (pnetwork->network.InfrastructureMode != pmlmepriv->cur_network.network.InfrastructureMode)
674 			bselected = false;
675 	}
676 
677 	return bselected;
678 }
679 
rtw_survey_event_callback(struct adapter * adapter,u8 * pbuf)680 void rtw_survey_event_callback(struct adapter	*adapter, u8 *pbuf)
681 {
682 	u32 len;
683 	struct wlan_bssid_ex *pnetwork;
684 	struct	mlme_priv	*pmlmepriv = &adapter->mlmepriv;
685 
686 	pnetwork = (struct wlan_bssid_ex *)pbuf;
687 
688 	len = get_wlan_bssid_ex_sz(pnetwork);
689 	if (len > (sizeof(struct wlan_bssid_ex)))
690 		return;
691 	spin_lock_bh(&pmlmepriv->lock);
692 
693 	/*  update IBSS_network 's timestamp */
694 	if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) {
695 		if (!memcmp(&pmlmepriv->cur_network.network.MacAddress, pnetwork->MacAddress, ETH_ALEN)) {
696 			struct wlan_network *ibss_wlan = NULL;
697 
698 			memcpy(pmlmepriv->cur_network.network.IEs, pnetwork->IEs, 8);
699 			spin_lock_bh(&pmlmepriv->scanned_queue.lock);
700 			ibss_wlan = rtw_find_network(&pmlmepriv->scanned_queue,  pnetwork->MacAddress);
701 			if (ibss_wlan) {
702 				memcpy(ibss_wlan->network.IEs, pnetwork->IEs, 8);
703 				spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
704 				goto exit;
705 			}
706 			spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
707 		}
708 	}
709 
710 	/*  lock pmlmepriv->lock when you accessing network_q */
711 	if (!check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) {
712 		if (pnetwork->Ssid.Ssid[0] == 0)
713 			pnetwork->Ssid.SsidLength = 0;
714 		rtw_add_network(adapter, pnetwork);
715 	}
716 
717 exit:
718 
719 	spin_unlock_bh(&pmlmepriv->lock);
720 }
721 
rtw_surveydone_event_callback(struct adapter * adapter,u8 * pbuf)722 void rtw_surveydone_event_callback(struct adapter	*adapter, u8 *pbuf)
723 {
724 	struct	mlme_priv *pmlmepriv = &adapter->mlmepriv;
725 	u8 timer_cancelled = 0;
726 
727 	spin_lock_bh(&pmlmepriv->lock);
728 
729 	if (pmlmepriv->wps_probe_req_ie) {
730 		pmlmepriv->wps_probe_req_ie_len = 0;
731 		kfree(pmlmepriv->wps_probe_req_ie);
732 		pmlmepriv->wps_probe_req_ie = NULL;
733 	}
734 
735 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY)) {
736 		timer_cancelled = 1;
737 
738 		_clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY);
739 	}
740 
741 	spin_unlock_bh(&pmlmepriv->lock);
742 
743 	if (timer_cancelled)
744 		_cancel_timer(&pmlmepriv->scan_to_timer, &timer_cancelled);
745 
746 	spin_lock_bh(&pmlmepriv->lock);
747 	rtw_set_signal_stat_timer(&adapter->recvpriv);
748 
749 	if (pmlmepriv->to_join) {
750 		if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
751 			if (!check_fwstate(pmlmepriv, _FW_LINKED)) {
752 				set_fwstate(pmlmepriv, _FW_UNDER_LINKING);
753 
754 				if (rtw_select_and_join_from_scanned_queue(pmlmepriv) == _SUCCESS) {
755 					_set_timer(&pmlmepriv->assoc_timer, MAX_JOIN_TIMEOUT);
756 				} else {
757 					struct wlan_bssid_ex    *pdev_network = &adapter->registrypriv.dev_network;
758 					u8 *pibss = adapter->registrypriv.dev_network.MacAddress;
759 
760 					_clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY);
761 
762 					memset(&pdev_network->Ssid, 0, sizeof(struct ndis_802_11_ssid));
763 					memcpy(&pdev_network->Ssid, &pmlmepriv->assoc_ssid, sizeof(struct ndis_802_11_ssid));
764 
765 					rtw_update_registrypriv_dev_network(adapter);
766 					rtw_generate_random_ibss(pibss);
767 
768 					pmlmepriv->fw_state = WIFI_ADHOC_MASTER_STATE;
769 
770 					rtw_createbss_cmd(adapter);
771 					pmlmepriv->to_join = false;
772 				}
773 			}
774 		} else {
775 			int s_ret;
776 			set_fwstate(pmlmepriv, _FW_UNDER_LINKING);
777 			pmlmepriv->to_join = false;
778 			s_ret = rtw_select_and_join_from_scanned_queue(pmlmepriv);
779 			if (_SUCCESS == s_ret) {
780 			     _set_timer(&pmlmepriv->assoc_timer, MAX_JOIN_TIMEOUT);
781 			} else if (s_ret == 2) { /* there is no need to wait for join */
782 				_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
783 				rtw_indicate_connect(adapter);
784 			} else {
785 				DBG_88E("try_to_join, but select scanning queue fail, to_roaming:%d\n",
786 					pmlmepriv->to_roaming);
787 				if (rtw_to_roaming(adapter) != 0) {
788 					if (--pmlmepriv->to_roaming == 0 ||
789 					    _SUCCESS != rtw_sitesurvey_cmd(adapter, &pmlmepriv->assoc_ssid, 1, NULL, 0)) {
790 						rtw_set_roaming(adapter, 0);
791 						rtw_free_assoc_resources(adapter, 1);
792 						rtw_indicate_disconnect(adapter);
793 					} else {
794 						pmlmepriv->to_join = true;
795 					}
796 				} else {
797 					rtw_indicate_disconnect(adapter);
798 				}
799 				_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
800 			}
801 		}
802 	}
803 
804 	indicate_wx_scan_complete_event(adapter);
805 
806 	spin_unlock_bh(&pmlmepriv->lock);
807 
808 	if (check_fwstate(pmlmepriv, _FW_LINKED))
809 		p2p_ps_wk_cmd(adapter, P2P_PS_SCAN_DONE, 0);
810 
811 	rtw_os_xmit_schedule(adapter);
812 }
813 
rtw_dummy_event_callback(struct adapter * adapter,u8 * pbuf)814 void rtw_dummy_event_callback(struct adapter *adapter, u8 *pbuf)
815 {
816 }
817 
rtw_fwdbg_event_callback(struct adapter * adapter,u8 * pbuf)818 void rtw_fwdbg_event_callback(struct adapter *adapter, u8 *pbuf)
819 {
820 }
821 
free_scanqueue(struct mlme_priv * pmlmepriv)822 static void free_scanqueue(struct	mlme_priv *pmlmepriv)
823 {
824 	struct __queue *free_queue = &pmlmepriv->free_bss_pool;
825 	struct __queue *scan_queue = &pmlmepriv->scanned_queue;
826 	struct list_head *plist, *phead, *ptemp;
827 
828 	spin_lock_bh(&scan_queue->lock);
829 	spin_lock_bh(&free_queue->lock);
830 
831 	phead = get_list_head(scan_queue);
832 	plist = phead->next;
833 
834 	while (plist != phead) {
835 		ptemp = plist->next;
836 		list_del_init(plist);
837 		list_add_tail(plist, &free_queue->queue);
838 		plist = ptemp;
839 		pmlmepriv->num_of_scanned--;
840 	}
841 
842 	spin_unlock_bh(&free_queue->lock);
843 	spin_unlock_bh(&scan_queue->lock);
844 }
845 
846 /*
847 *rtw_free_assoc_resources: the caller has to lock pmlmepriv->lock
848 */
rtw_free_assoc_resources(struct adapter * adapter,int lock_scanned_queue)849 void rtw_free_assoc_resources(struct adapter *adapter, int lock_scanned_queue)
850 {
851 	struct wlan_network *pwlan = NULL;
852 	struct	mlme_priv *pmlmepriv = &adapter->mlmepriv;
853 	struct	sta_priv *pstapriv = &adapter->stapriv;
854 	struct wlan_network *tgt_network = &pmlmepriv->cur_network;
855 
856 	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_AP_STATE)) {
857 		struct sta_info *psta;
858 
859 		psta = rtw_get_stainfo(&adapter->stapriv, tgt_network->network.MacAddress);
860 
861 		spin_lock_bh(&pstapriv->sta_hash_lock);
862 		rtw_free_stainfo(adapter,  psta);
863 		spin_unlock_bh(&pstapriv->sta_hash_lock);
864 	}
865 
866 	if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE | WIFI_AP_STATE)) {
867 		struct sta_info *psta;
868 
869 		rtw_free_all_stainfo(adapter);
870 
871 		psta = rtw_get_bcmc_stainfo(adapter);
872 		spin_lock_bh(&pstapriv->sta_hash_lock);
873 		rtw_free_stainfo(adapter, psta);
874 		spin_unlock_bh(&pstapriv->sta_hash_lock);
875 
876 		rtw_init_bcmc_stainfo(adapter);
877 	}
878 
879 	if (lock_scanned_queue)
880 		spin_lock_bh(&pmlmepriv->scanned_queue.lock);
881 
882 	pwlan = rtw_find_network(&pmlmepriv->scanned_queue, tgt_network->network.MacAddress);
883 	if (pwlan)
884 		pwlan->fixed = false;
885 
886 	if ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) && (adapter->stapriv.asoc_sta_count == 1)))
887 		rtw_free_network_nolock(pmlmepriv, pwlan);
888 
889 	if (lock_scanned_queue)
890 		spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
891 	pmlmepriv->key_mask = 0;
892 
893 }
894 
895 /*
896 *rtw_indicate_connect: the caller has to lock pmlmepriv->lock
897 */
rtw_indicate_connect(struct adapter * padapter)898 void rtw_indicate_connect(struct adapter *padapter)
899 {
900 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
901 
902 	pmlmepriv->to_join = false;
903 
904 	if (!check_fwstate(&padapter->mlmepriv, _FW_LINKED)) {
905 		set_fwstate(pmlmepriv, _FW_LINKED);
906 
907 		rtw_led_control(padapter, LED_CTL_LINK);
908 
909 		rtw_os_indicate_connect(padapter);
910 	}
911 
912 	pmlmepriv->to_roaming = 0;
913 
914 	rtw_set_scan_deny(padapter, 3000);
915 }
916 
917 /*
918 *rtw_indicate_disconnect: the caller has to lock pmlmepriv->lock
919 */
rtw_indicate_disconnect(struct adapter * padapter)920 void rtw_indicate_disconnect(struct adapter *padapter)
921 {
922 	struct	mlme_priv *pmlmepriv = &padapter->mlmepriv;
923 
924 	_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING | WIFI_UNDER_WPS);
925 
926 	if (pmlmepriv->to_roaming > 0)
927 		_clr_fwstate_(pmlmepriv, _FW_LINKED);
928 
929 	if (check_fwstate(&padapter->mlmepriv, _FW_LINKED) ||
930 	    (pmlmepriv->to_roaming <= 0)) {
931 		rtw_os_indicate_disconnect(padapter);
932 
933 		_clr_fwstate_(pmlmepriv, _FW_LINKED);
934 		rtw_led_control(padapter, LED_CTL_NO_LINK);
935 		rtw_clear_scan_deny(padapter);
936 	}
937 	p2p_ps_wk_cmd(padapter, P2P_PS_DISABLE, 1);
938 
939 	rtw_lps_ctrl_wk_cmd(padapter, LPS_CTRL_DISCONNECT, 1);
940 
941 }
942 
rtw_indicate_scan_done(struct adapter * padapter,bool aborted)943 inline void rtw_indicate_scan_done(struct adapter *padapter, bool aborted)
944 {
945 	rtw_os_indicate_scan_done(padapter, aborted);
946 }
947 
rtw_scan_abort(struct adapter * adapter)948 void rtw_scan_abort(struct adapter *adapter)
949 {
950 	u32 start;
951 	struct mlme_priv	*pmlmepriv = &adapter->mlmepriv;
952 	struct mlme_ext_priv	*pmlmeext = &adapter->mlmeextpriv;
953 
954 	start = jiffies;
955 	pmlmeext->scan_abort = true;
956 	while (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY) &&
957 	       rtw_get_passing_time_ms(start) <= 200) {
958 		if (adapter->bDriverStopped || adapter->bSurpriseRemoved)
959 			break;
960 		DBG_88E(FUNC_NDEV_FMT"fw_state=_FW_UNDER_SURVEY!\n", FUNC_NDEV_ARG(adapter->pnetdev));
961 		msleep(20);
962 	}
963 	if (check_fwstate(pmlmepriv, _FW_UNDER_SURVEY)) {
964 		if (!adapter->bDriverStopped && !adapter->bSurpriseRemoved)
965 			DBG_88E(FUNC_NDEV_FMT"waiting for scan_abort time out!\n", FUNC_NDEV_ARG(adapter->pnetdev));
966 		rtw_indicate_scan_done(adapter, true);
967 	}
968 	pmlmeext->scan_abort = false;
969 }
970 
rtw_joinbss_update_stainfo(struct adapter * padapter,struct wlan_network * pnetwork)971 static struct sta_info *rtw_joinbss_update_stainfo(struct adapter *padapter, struct wlan_network *pnetwork)
972 {
973 	int i;
974 	struct sta_info *bmc_sta, *psta = NULL;
975 	struct recv_reorder_ctrl *preorder_ctrl;
976 	struct sta_priv *pstapriv = &padapter->stapriv;
977 
978 	psta = rtw_get_stainfo(pstapriv, pnetwork->network.MacAddress);
979 	if (!psta)
980 		psta = rtw_alloc_stainfo(pstapriv, pnetwork->network.MacAddress);
981 
982 	if (psta) { /* update ptarget_sta */
983 		DBG_88E("%s\n", __func__);
984 		psta->aid  = pnetwork->join_res;
985 		psta->mac_id = 0;
986 		/* sta mode */
987 		rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, true);
988 		/* security related */
989 		if (padapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X) {
990 			padapter->securitypriv.binstallGrpkey = false;
991 			padapter->securitypriv.busetkipkey = false;
992 			padapter->securitypriv.bgrpkey_handshake = false;
993 			psta->ieee8021x_blocked = true;
994 			psta->dot118021XPrivacy = padapter->securitypriv.dot11PrivacyAlgrthm;
995 			memset((u8 *)&psta->dot118021x_UncstKey, 0, sizeof(union Keytype));
996 			memset((u8 *)&psta->dot11tkiprxmickey, 0, sizeof(union Keytype));
997 			memset((u8 *)&psta->dot11tkiptxmickey, 0, sizeof(union Keytype));
998 			memset((u8 *)&psta->dot11txpn, 0, sizeof(union pn48));
999 			memset((u8 *)&psta->dot11rxpn, 0, sizeof(union pn48));
1000 		}
1001 		/* 	Commented by Albert 2012/07/21 */
1002 		/* 	When doing the WPS, the wps_ie_len won't equal to 0 */
1003 		/* 	And the Wi-Fi driver shouldn't allow the data packet to be tramsmitted. */
1004 		if (padapter->securitypriv.wps_ie_len != 0) {
1005 			psta->ieee8021x_blocked = true;
1006 			padapter->securitypriv.wps_ie_len = 0;
1007 		}
1008 		/* for A-MPDU Rx reordering buffer control for bmc_sta & sta_info */
1009 		/* if A-MPDU Rx is enabled, resetting  rx_ordering_ctrl wstart_b(indicate_seq) to default value = 0xffff */
1010 		/* todo: check if AP can send A-MPDU packets */
1011 		for (i = 0; i < 16; i++) {
1012 			/* preorder_ctrl = &precvpriv->recvreorder_ctrl[i]; */
1013 			preorder_ctrl = &psta->recvreorder_ctrl[i];
1014 			preorder_ctrl->enable = false;
1015 			preorder_ctrl->indicate_seq = 0xffff;
1016 			preorder_ctrl->wend_b = 0xffff;
1017 			preorder_ctrl->wsize_b = 64;/* max_ampdu_sz; ex. 32(kbytes) -> wsize_b = 32 */
1018 		}
1019 		bmc_sta = rtw_get_bcmc_stainfo(padapter);
1020 		if (bmc_sta) {
1021 			for (i = 0; i < 16; i++) {
1022 				/* preorder_ctrl = &precvpriv->recvreorder_ctrl[i]; */
1023 				preorder_ctrl = &bmc_sta->recvreorder_ctrl[i];
1024 				preorder_ctrl->enable = false;
1025 				preorder_ctrl->indicate_seq = 0xffff;
1026 				preorder_ctrl->wend_b = 0xffff;
1027 				preorder_ctrl->wsize_b = 64;/* max_ampdu_sz; ex. 32(kbytes) -> wsize_b = 32 */
1028 			}
1029 		}
1030 		/* misc. */
1031 		update_sta_info(padapter, psta);
1032 	}
1033 	return psta;
1034 }
1035 
1036 /* pnetwork: returns from rtw_joinbss_event_callback */
1037 /* ptarget_wlan: found from scanned_queue */
rtw_joinbss_update_network(struct adapter * padapter,struct wlan_network * ptarget_wlan,struct wlan_network * pnetwork)1038 static void rtw_joinbss_update_network(struct adapter *padapter, struct wlan_network *ptarget_wlan, struct wlan_network  *pnetwork)
1039 {
1040 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
1041 	struct wlan_network  *cur_network = &pmlmepriv->cur_network;
1042 
1043 	DBG_88E("%s\n", __func__);
1044 
1045 	/*  why not use ptarget_wlan?? */
1046 	memcpy(&cur_network->network, &pnetwork->network, pnetwork->network.Length);
1047 	/*  some IEs in pnetwork is wrong, so we should use ptarget_wlan IEs */
1048 	cur_network->network.IELength = ptarget_wlan->network.IELength;
1049 	memcpy(&cur_network->network.IEs[0], &ptarget_wlan->network.IEs[0], MAX_IE_SZ);
1050 
1051 	cur_network->aid = pnetwork->join_res;
1052 
1053 	rtw_set_signal_stat_timer(&padapter->recvpriv);
1054 	padapter->recvpriv.signal_strength = ptarget_wlan->network.PhyInfo.SignalStrength;
1055 	padapter->recvpriv.signal_qual = ptarget_wlan->network.PhyInfo.SignalQuality;
1056 	/* the ptarget_wlan->network.Rssi is raw data, we use ptarget_wlan->network.PhyInfo.SignalStrength instead (has scaled) */
1057 	padapter->recvpriv.rssi = translate_percentage_to_dbm(ptarget_wlan->network.PhyInfo.SignalStrength);
1058 	rtw_set_signal_stat_timer(&padapter->recvpriv);
1059 
1060 	/* update fw_state will clr _FW_UNDER_LINKING here indirectly */
1061 	switch (pnetwork->network.InfrastructureMode) {
1062 	case Ndis802_11Infrastructure:
1063 		if (pmlmepriv->fw_state & WIFI_UNDER_WPS)
1064 			pmlmepriv->fw_state = WIFI_STATION_STATE | WIFI_UNDER_WPS;
1065 		else
1066 			pmlmepriv->fw_state = WIFI_STATION_STATE;
1067 		break;
1068 	case Ndis802_11IBSS:
1069 		pmlmepriv->fw_state = WIFI_ADHOC_STATE;
1070 		break;
1071 	default:
1072 		pmlmepriv->fw_state = WIFI_NULL_STATE;
1073 		break;
1074 	}
1075 
1076 	rtw_update_protection(padapter, (cur_network->network.IEs) +
1077 			      sizeof(struct ndis_802_11_fixed_ie),
1078 			      (cur_network->network.IELength));
1079 	rtw_update_ht_cap(padapter, cur_network->network.IEs, cur_network->network.IELength);
1080 }
1081 
1082 /* Notes: the function could be > passive_level (the same context as Rx tasklet) */
1083 /* pnetwork: returns from rtw_joinbss_event_callback */
1084 /* ptarget_wlan: found from scanned_queue */
1085 /* if join_res > 0, for (fw_state == WIFI_STATION_STATE), we check if  "ptarget_sta" & "ptarget_wlan" exist. */
1086 /* if join_res > 0, for (fw_state == WIFI_ADHOC_STATE), we only check if "ptarget_wlan" exist. */
1087 /* if join_res > 0, update "cur_network->network" from "pnetwork->network" if (ptarget_wlan != NULL). */
1088 
rtw_joinbss_event_prehandle(struct adapter * adapter,u8 * pbuf)1089 void rtw_joinbss_event_prehandle(struct adapter *adapter, u8 *pbuf)
1090 {
1091 	u8 timer_cancelled;
1092 	struct sta_info *ptarget_sta = NULL, *pcur_sta = NULL;
1093 	struct	sta_priv *pstapriv = &adapter->stapriv;
1094 	struct	mlme_priv	*pmlmepriv = &adapter->mlmepriv;
1095 	struct wlan_network	*pnetwork	= (struct wlan_network *)pbuf;
1096 	struct wlan_network	*cur_network = &pmlmepriv->cur_network;
1097 	struct wlan_network	*pcur_wlan = NULL, *ptarget_wlan = NULL;
1098 	unsigned int		the_same_macaddr = false;
1099 
1100 	rtw_get_encrypt_decrypt_from_registrypriv(adapter);
1101 
1102 	the_same_macaddr = !memcmp(pnetwork->network.MacAddress, cur_network->network.MacAddress, ETH_ALEN);
1103 
1104 	pnetwork->network.Length = get_wlan_bssid_ex_sz(&pnetwork->network);
1105 	if (pnetwork->network.Length > sizeof(struct wlan_bssid_ex))
1106 		return;
1107 
1108 	spin_lock_bh(&pmlmepriv->lock);
1109 
1110 	if (pnetwork->join_res > 0) {
1111 		spin_lock_bh(&pmlmepriv->scanned_queue.lock);
1112 		if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING)) {
1113 			/* s1. find ptarget_wlan */
1114 			if (check_fwstate(pmlmepriv, _FW_LINKED)) {
1115 				if (the_same_macaddr) {
1116 					ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.MacAddress);
1117 				} else {
1118 					pcur_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.MacAddress);
1119 					if (pcur_wlan)
1120 						pcur_wlan->fixed = false;
1121 
1122 					pcur_sta = rtw_get_stainfo(pstapriv, cur_network->network.MacAddress);
1123 					if (pcur_sta) {
1124 						spin_lock_bh(&pstapriv->sta_hash_lock);
1125 						rtw_free_stainfo(adapter,  pcur_sta);
1126 						spin_unlock_bh(&pstapriv->sta_hash_lock);
1127 					}
1128 
1129 					ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, pnetwork->network.MacAddress);
1130 					if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
1131 						if (ptarget_wlan)
1132 							ptarget_wlan->fixed = true;
1133 					}
1134 				}
1135 			} else {
1136 				ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, pnetwork->network.MacAddress);
1137 				if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
1138 					if (ptarget_wlan)
1139 						ptarget_wlan->fixed = true;
1140 				}
1141 			}
1142 
1143 			/* s2. update cur_network */
1144 			if (ptarget_wlan) {
1145 				rtw_joinbss_update_network(adapter, ptarget_wlan, pnetwork);
1146 			} else {
1147 				spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1148 				goto ignore_joinbss_callback;
1149 			}
1150 
1151 			/* s3. find ptarget_sta & update ptarget_sta after update cur_network only for station mode */
1152 			if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
1153 				ptarget_sta = rtw_joinbss_update_stainfo(adapter, pnetwork);
1154 				if (!ptarget_sta) {
1155 					spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1156 					goto ignore_joinbss_callback;
1157 				}
1158 			}
1159 
1160 			/* s4. indicate connect */
1161 			if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
1162 				pmlmepriv->cur_network_scanned = ptarget_wlan;
1163 				rtw_indicate_connect(adapter);
1164 			}
1165 
1166 			/* s5. Cancle assoc_timer */
1167 			_cancel_timer(&pmlmepriv->assoc_timer, &timer_cancelled);
1168 		} else {
1169 			spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1170 			goto ignore_joinbss_callback;
1171 		}
1172 
1173 		spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1174 
1175 	} else if (pnetwork->join_res == -4) {
1176 		rtw_reset_securitypriv(adapter);
1177 		_set_timer(&pmlmepriv->assoc_timer, 1);
1178 
1179 		if (check_fwstate(pmlmepriv, _FW_UNDER_LINKING))
1180 			_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
1181 	} else { /* if join_res < 0 (join fails), then try again */
1182 		_set_timer(&pmlmepriv->assoc_timer, 1);
1183 		_clr_fwstate_(pmlmepriv, _FW_UNDER_LINKING);
1184 	}
1185 
1186 ignore_joinbss_callback:
1187 	spin_unlock_bh(&pmlmepriv->lock);
1188 }
1189 
rtw_joinbss_event_callback(struct adapter * adapter,u8 * pbuf)1190 void rtw_joinbss_event_callback(struct adapter *adapter, u8 *pbuf)
1191 {
1192 	struct wlan_network	*pnetwork	= (struct wlan_network *)pbuf;
1193 
1194 	mlmeext_joinbss_event_callback(adapter, pnetwork->join_res);
1195 
1196 	rtw_os_xmit_schedule(adapter);
1197 
1198 }
1199 
search_max_mac_id(struct adapter * padapter)1200 static u8 search_max_mac_id(struct adapter *padapter)
1201 {
1202 	u8 mac_id;
1203 #if defined(CONFIG_88EU_AP_MODE)
1204 	u8 aid;
1205 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1206 	struct sta_priv *pstapriv = &padapter->stapriv;
1207 #endif
1208 	struct mlme_ext_priv *pmlmeext = &padapter->mlmeextpriv;
1209 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
1210 
1211 #if defined(CONFIG_88EU_AP_MODE)
1212 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1213 		for (aid = (pstapriv->max_num_sta); aid > 0; aid--) {
1214 			if (pstapriv->sta_aid[aid - 1])
1215 				break;
1216 		}
1217 		mac_id = aid + 1;
1218 	} else
1219 #endif
1220 	{/* adhoc  id =  31~2 */
1221 		for (mac_id = (NUM_STA - 1); mac_id >= IBSS_START_MAC_ID; mac_id--) {
1222 			if (pmlmeinfo->FW_sta_info[mac_id].status == 1)
1223 				break;
1224 		}
1225 	}
1226 	return mac_id;
1227 }
1228 
1229 /* FOR AP , AD-HOC mode */
rtw_sta_media_status_rpt(struct adapter * adapter,struct sta_info * psta,u32 mstatus)1230 void rtw_sta_media_status_rpt(struct adapter *adapter, struct sta_info *psta,
1231 			      u32 mstatus)
1232 {
1233 	u16 media_status_rpt;
1234 	u8 macid;
1235 
1236 	if (!psta)
1237 		return;
1238 
1239 	macid = search_max_mac_id(adapter);
1240 	rtw_hal_set_hwreg(adapter, HW_VAR_TX_RPT_MAX_MACID, (u8 *)&macid);
1241 	/* MACID|OPMODE:1 connect */
1242 	media_status_rpt = (u16)((psta->mac_id << 8) | mstatus);
1243 	rtw_hal_set_hwreg(adapter, HW_VAR_H2C_MEDIA_STATUS_RPT,
1244 			  (u8 *)&media_status_rpt);
1245 }
1246 
rtw_stassoc_event_callback(struct adapter * adapter,u8 * pbuf)1247 void rtw_stassoc_event_callback(struct adapter *adapter, u8 *pbuf)
1248 {
1249 	struct sta_info *psta;
1250 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1251 	struct stassoc_event	*pstassoc = (struct stassoc_event *)pbuf;
1252 	struct wlan_network	*cur_network = &pmlmepriv->cur_network;
1253 	struct wlan_network	*ptarget_wlan = NULL;
1254 
1255 	if (!rtw_access_ctrl(adapter, pstassoc->macaddr))
1256 		return;
1257 
1258 #if defined(CONFIG_88EU_AP_MODE)
1259 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE)) {
1260 		psta = rtw_get_stainfo(&adapter->stapriv, pstassoc->macaddr);
1261 		if (psta)
1262 			rtw_indicate_sta_assoc_event(adapter, psta);
1263 		return;
1264 	}
1265 #endif
1266 	/* for AD-HOC mode */
1267 	psta = rtw_get_stainfo(&adapter->stapriv, pstassoc->macaddr);
1268 	if (psta)
1269 		/* the sta have been in sta_info_queue => do nothing */
1270 		return; /* between drv has received this event before and  fw have not yet to set key to CAM_ENTRY) */
1271 	psta = rtw_alloc_stainfo(&adapter->stapriv, pstassoc->macaddr);
1272 	if (!psta)
1273 		return;
1274 	/* to do: init sta_info variable */
1275 	psta->qos_option = 0;
1276 	psta->mac_id = (uint)pstassoc->cam_id;
1277 	DBG_88E("%s\n", __func__);
1278 	/* for ad-hoc mode */
1279 	rtw_hal_set_odm_var(adapter, HAL_ODM_STA_INFO, psta, true);
1280 	rtw_sta_media_status_rpt(adapter, psta, 1);
1281 	if (adapter->securitypriv.dot11AuthAlgrthm == dot11AuthAlgrthm_8021X)
1282 		psta->dot118021XPrivacy = adapter->securitypriv.dot11PrivacyAlgrthm;
1283 	psta->ieee8021x_blocked = false;
1284 	spin_lock_bh(&pmlmepriv->lock);
1285 	if ((check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE)) ||
1286 	    (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE))) {
1287 		if (adapter->stapriv.asoc_sta_count == 2) {
1288 			spin_lock_bh(&pmlmepriv->scanned_queue.lock);
1289 			ptarget_wlan = rtw_find_network(&pmlmepriv->scanned_queue, cur_network->network.MacAddress);
1290 			pmlmepriv->cur_network_scanned = ptarget_wlan;
1291 			if (ptarget_wlan)
1292 				ptarget_wlan->fixed = true;
1293 			spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1294 			/*  a sta + bc/mc_stainfo (not Ibss_stainfo) */
1295 			rtw_indicate_connect(adapter);
1296 		}
1297 	}
1298 	spin_unlock_bh(&pmlmepriv->lock);
1299 	mlmeext_sta_add_event_callback(adapter, psta);
1300 }
1301 
rtw_stadel_event_callback(struct adapter * adapter,u8 * pbuf)1302 void rtw_stadel_event_callback(struct adapter *adapter, u8 *pbuf)
1303 {
1304 	int mac_id = -1;
1305 	struct sta_info *psta;
1306 	struct wlan_network *pwlan = NULL;
1307 	struct wlan_bssid_ex *pdev_network = NULL;
1308 	u8 *pibss = NULL;
1309 	struct	mlme_priv *pmlmepriv = &adapter->mlmepriv;
1310 	struct	stadel_event *pstadel = (struct stadel_event *)pbuf;
1311 	struct	sta_priv *pstapriv = &adapter->stapriv;
1312 	struct wlan_network *tgt_network = &pmlmepriv->cur_network;
1313 
1314 	psta = rtw_get_stainfo(&adapter->stapriv, pstadel->macaddr);
1315 	if (psta)
1316 		mac_id = psta->mac_id;
1317 	else
1318 		mac_id = pstadel->mac_id;
1319 
1320 	DBG_88E("%s(mac_id=%d)=%pM\n", __func__, mac_id, pstadel->macaddr);
1321 
1322 	if (mac_id >= 0) {
1323 		u16 media_status;
1324 		media_status = (mac_id << 8) | 0; /*   MACID|OPMODE:0 means disconnect */
1325 		/* for STA, AP, ADHOC mode, report disconnect stauts to FW */
1326 		rtw_hal_set_hwreg(adapter, HW_VAR_H2C_MEDIA_STATUS_RPT, (u8 *)&media_status);
1327 	}
1328 
1329 	if (check_fwstate(pmlmepriv, WIFI_AP_STATE))
1330 		return;
1331 
1332 	mlmeext_sta_del_event_callback(adapter);
1333 
1334 	spin_lock_bh(&pmlmepriv->lock);
1335 
1336 	if (check_fwstate(pmlmepriv, WIFI_STATION_STATE)) {
1337 		if (adapter->registrypriv.wifi_spec == 1)
1338 			rtw_set_roaming(adapter, 0); /* don't roam */
1339 		else if (rtw_to_roaming(adapter) > 0)
1340 			pmlmepriv->to_roaming--; /* this stadel_event is caused by roaming, decrease to_roaming */
1341 		else if (rtw_to_roaming(adapter) == 0)
1342 			rtw_set_roaming(adapter,
1343 					adapter->registrypriv.max_roaming_times);
1344 
1345 		if (*((unsigned short *)(pstadel->rsvd)) != WLAN_REASON_EXPIRATION_CHK)
1346 			rtw_set_roaming(adapter, 0); /* don't roam */
1347 
1348 		rtw_free_uc_swdec_pending_queue(adapter);
1349 
1350 		rtw_free_assoc_resources(adapter, 1);
1351 		rtw_indicate_disconnect(adapter);
1352 		spin_lock_bh(&pmlmepriv->scanned_queue.lock);
1353 		/*  remove the network entry in scanned_queue */
1354 		pwlan = rtw_find_network(&pmlmepriv->scanned_queue, tgt_network->network.MacAddress);
1355 		if (pwlan) {
1356 			pwlan->fixed = false;
1357 			rtw_free_network_nolock(pmlmepriv, pwlan);
1358 		}
1359 		spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1360 		_rtw_roaming(adapter, tgt_network);
1361 	}
1362 	if (check_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE) ||
1363 	    check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
1364 		spin_lock_bh(&pstapriv->sta_hash_lock);
1365 		rtw_free_stainfo(adapter,  psta);
1366 		spin_unlock_bh(&pstapriv->sta_hash_lock);
1367 
1368 		if (adapter->stapriv.asoc_sta_count == 1) { /* a sta + bc/mc_stainfo (not Ibss_stainfo) */
1369 			spin_lock_bh(&pmlmepriv->scanned_queue.lock);
1370 			/* free old ibss network */
1371 			pwlan = rtw_find_network(&pmlmepriv->scanned_queue, tgt_network->network.MacAddress);
1372 			if (pwlan) {
1373 				pwlan->fixed = false;
1374 				rtw_free_network_nolock(pmlmepriv, pwlan);
1375 			}
1376 			spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1377 			/* re-create ibss */
1378 			pdev_network = &adapter->registrypriv.dev_network;
1379 			pibss = adapter->registrypriv.dev_network.MacAddress;
1380 
1381 			memcpy(pdev_network, &tgt_network->network, get_wlan_bssid_ex_sz(&tgt_network->network));
1382 
1383 			memset(&pdev_network->Ssid, 0, sizeof(struct ndis_802_11_ssid));
1384 			memcpy(&pdev_network->Ssid, &pmlmepriv->assoc_ssid, sizeof(struct ndis_802_11_ssid));
1385 
1386 			rtw_update_registrypriv_dev_network(adapter);
1387 
1388 			rtw_generate_random_ibss(pibss);
1389 
1390 			if (check_fwstate(pmlmepriv, WIFI_ADHOC_STATE)) {
1391 				set_fwstate(pmlmepriv, WIFI_ADHOC_MASTER_STATE);
1392 				_clr_fwstate_(pmlmepriv, WIFI_ADHOC_STATE);
1393 			}
1394 
1395 			rtw_createbss_cmd(adapter);
1396 		}
1397 	}
1398 	spin_unlock_bh(&pmlmepriv->lock);
1399 
1400 }
1401 
1402 /*
1403 * _rtw_join_timeout_handler - Timeout/faliure handler for CMD JoinBss
1404 * @adapter: pointer to struct adapter structure
1405 */
_rtw_join_timeout_handler(struct adapter * adapter)1406 void _rtw_join_timeout_handler (struct adapter *adapter)
1407 {
1408 	struct	mlme_priv *pmlmepriv = &adapter->mlmepriv;
1409 	int do_join_r;
1410 
1411 	DBG_88E("%s, fw_state=%x\n", __func__, get_fwstate(pmlmepriv));
1412 
1413 	if (adapter->bDriverStopped || adapter->bSurpriseRemoved)
1414 		return;
1415 
1416 	spin_lock_bh(&pmlmepriv->lock);
1417 
1418 	if (rtw_to_roaming(adapter) > 0) { /* join timeout caused by roaming */
1419 		while (1) {
1420 			pmlmepriv->to_roaming--;
1421 			if (rtw_to_roaming(adapter) != 0) { /* try another */
1422 				DBG_88E("%s try another roaming\n", __func__);
1423 				do_join_r = rtw_do_join(adapter);
1424 				if (_SUCCESS != do_join_r) {
1425 					DBG_88E("%s roaming do_join return %d\n", __func__, do_join_r);
1426 					continue;
1427 				}
1428 				break;
1429 			} else {
1430 				DBG_88E("%s We've try roaming but fail\n", __func__);
1431 				rtw_indicate_disconnect(adapter);
1432 				break;
1433 			}
1434 		}
1435 	} else {
1436 		rtw_indicate_disconnect(adapter);
1437 		free_scanqueue(pmlmepriv);/*  */
1438 	}
1439 	spin_unlock_bh(&pmlmepriv->lock);
1440 
1441 }
1442 
1443 /*
1444 * rtw_scan_timeout_handler - Timeout/Faliure handler for CMD SiteSurvey
1445 * @adapter: pointer to struct adapter structure
1446 */
rtw_scan_timeout_handler(struct adapter * adapter)1447 void rtw_scan_timeout_handler (struct adapter *adapter)
1448 {
1449 	struct	mlme_priv *pmlmepriv = &adapter->mlmepriv;
1450 
1451 	DBG_88E(FUNC_ADPT_FMT" fw_state=%x\n", FUNC_ADPT_ARG(adapter), get_fwstate(pmlmepriv));
1452 	spin_lock_bh(&pmlmepriv->lock);
1453 	_clr_fwstate_(pmlmepriv, _FW_UNDER_SURVEY);
1454 	spin_unlock_bh(&pmlmepriv->lock);
1455 	rtw_indicate_scan_done(adapter, true);
1456 }
1457 
rtw_auto_scan_handler(struct adapter * padapter)1458 static void rtw_auto_scan_handler(struct adapter *padapter)
1459 {
1460 	struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
1461 
1462 	/* auto site survey per 60sec */
1463 	if (pmlmepriv->scan_interval > 0) {
1464 		pmlmepriv->scan_interval--;
1465 		if (pmlmepriv->scan_interval == 0) {
1466 			DBG_88E("%s\n", __func__);
1467 			rtw_set_802_11_bssid_list_scan(padapter, NULL, 0);
1468 			pmlmepriv->scan_interval = SCAN_INTERVAL;/*  30*2 sec = 60sec */
1469 		}
1470 	}
1471 }
1472 
rtw_dynamic_check_timer_handlder(struct adapter * adapter)1473 void rtw_dynamic_check_timer_handlder(struct adapter *adapter)
1474 {
1475 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1476 	struct registry_priv *pregistrypriv = &adapter->registrypriv;
1477 
1478 	if (!adapter)
1479 		return;
1480 	if (!adapter->hw_init_completed)
1481 		return;
1482 	if ((adapter->bDriverStopped) || (adapter->bSurpriseRemoved))
1483 		return;
1484 	if (adapter->net_closed)
1485 		return;
1486 	rtw_dynamic_chk_wk_cmd(adapter);
1487 
1488 	if (pregistrypriv->wifi_spec == 1) {
1489 #ifdef CONFIG_88EU_P2P
1490 		struct wifidirect_info *pwdinfo = &adapter->wdinfo;
1491 		if (rtw_p2p_chk_state(pwdinfo, P2P_STATE_NONE))
1492 #endif
1493 		{
1494 			/* auto site survey */
1495 			rtw_auto_scan_handler(adapter);
1496 		}
1497 	}
1498 
1499 	rcu_read_lock();
1500 
1501 	if (rcu_dereference(adapter->pnetdev->rx_handler_data) &&
1502 	    check_fwstate(pmlmepriv, WIFI_STATION_STATE | WIFI_ADHOC_STATE)) {
1503 		/*  expire NAT2.5 entry */
1504 		nat25_db_expire(adapter);
1505 
1506 		if (adapter->pppoe_connection_in_progress > 0) {
1507 			adapter->pppoe_connection_in_progress--;
1508 		}
1509 
1510 		/*  due to rtw_dynamic_check_timer_handlder() is called every 2 seconds */
1511 		if (adapter->pppoe_connection_in_progress > 0) {
1512 			adapter->pppoe_connection_in_progress--;
1513 		}
1514 	}
1515 
1516 	rcu_read_unlock();
1517 }
1518 
1519 #define RTW_SCAN_RESULT_EXPIRE 2000
1520 
1521 /*
1522 * Select a new join candidate from the original @param candidate and @param competitor
1523 * @return true: candidate is updated
1524 * @return false: candidate is not updated
1525 */
rtw_check_join_candidate(struct mlme_priv * pmlmepriv,struct wlan_network ** candidate,struct wlan_network * competitor)1526 static int rtw_check_join_candidate(struct mlme_priv *pmlmepriv
1527 	, struct wlan_network **candidate, struct wlan_network *competitor)
1528 {
1529 	int updated = false;
1530 	struct adapter *adapter = container_of(pmlmepriv, struct adapter, mlmepriv);
1531 
1532 	/* check bssid, if needed */
1533 	if (pmlmepriv->assoc_by_bssid) {
1534 		if (memcmp(competitor->network.MacAddress, pmlmepriv->assoc_bssid, ETH_ALEN))
1535 			goto exit;
1536 	}
1537 
1538 	/* check ssid, if needed */
1539 	if (pmlmepriv->assoc_ssid.SsidLength) {
1540 		if (competitor->network.Ssid.SsidLength != pmlmepriv->assoc_ssid.SsidLength ||
1541 		    memcmp(competitor->network.Ssid.Ssid, pmlmepriv->assoc_ssid.Ssid, pmlmepriv->assoc_ssid.SsidLength))
1542 			goto exit;
1543 	}
1544 
1545 	if (!rtw_is_desired_network(adapter, competitor))
1546 		goto exit;
1547 
1548 	if (rtw_to_roaming(adapter) > 0) {
1549 		if (rtw_get_passing_time_ms((u32)competitor->last_scanned) >= RTW_SCAN_RESULT_EXPIRE ||
1550 		    !is_same_ess(&competitor->network, &pmlmepriv->cur_network.network))
1551 			goto exit;
1552 	}
1553 
1554 	if (!*candidate || (*candidate)->network.Rssi < competitor->network.Rssi) {
1555 		*candidate = competitor;
1556 		updated = true;
1557 	}
1558 	if (updated) {
1559 		DBG_88E("[by_bssid:%u][assoc_ssid:%s]new candidate: %s(%pM rssi:%d\n",
1560 			pmlmepriv->assoc_by_bssid,
1561 			pmlmepriv->assoc_ssid.Ssid,
1562 			(*candidate)->network.Ssid.Ssid,
1563 			(*candidate)->network.MacAddress,
1564 			(int)(*candidate)->network.Rssi);
1565 		DBG_88E("[to_roaming:%u]\n", rtw_to_roaming(adapter));
1566 	}
1567 
1568 exit:
1569 	return updated;
1570 }
1571 
1572 /*
1573 Calling context:
1574 The caller of the sub-routine will be in critical section...
1575 The caller must hold the following spinlock
1576 pmlmepriv->lock
1577 */
1578 
rtw_select_and_join_from_scanned_queue(struct mlme_priv * pmlmepriv)1579 int rtw_select_and_join_from_scanned_queue(struct mlme_priv *pmlmepriv)
1580 {
1581 	int ret;
1582 	struct list_head *phead;
1583 	struct adapter *adapter;
1584 	struct __queue *queue	= &pmlmepriv->scanned_queue;
1585 	struct	wlan_network	*pnetwork = NULL;
1586 	struct	wlan_network	*candidate = NULL;
1587 	u8	supp_ant_div = false;
1588 
1589 	spin_lock_bh(&pmlmepriv->scanned_queue.lock);
1590 	phead = get_list_head(queue);
1591 	adapter = (struct adapter *)pmlmepriv->nic_hdl;
1592 	pmlmepriv->pscanned = phead->next;
1593 	while (phead != pmlmepriv->pscanned) {
1594 		pnetwork = container_of(pmlmepriv->pscanned, struct wlan_network, list);
1595 		if (!pnetwork) {
1596 			ret = _FAIL;
1597 			goto exit;
1598 		}
1599 		pmlmepriv->pscanned = pmlmepriv->pscanned->next;
1600 		rtw_check_join_candidate(pmlmepriv, &candidate, pnetwork);
1601 	}
1602 	if (!candidate) {
1603 		DBG_88E("%s: return _FAIL(candidate==NULL)\n", __func__);
1604 		ret = _FAIL;
1605 		goto exit;
1606 	} else {
1607 		DBG_88E("%s: candidate: %s(%pM ch:%u)\n", __func__,
1608 			candidate->network.Ssid.Ssid, candidate->network.MacAddress,
1609 			candidate->network.Configuration.DSConfig);
1610 	}
1611 
1612 	/*  check for situation of  _FW_LINKED */
1613 	if (check_fwstate(pmlmepriv, _FW_LINKED)) {
1614 		DBG_88E("%s: _FW_LINKED while ask_for_joinbss!!!\n", __func__);
1615 
1616 		rtw_disassoc_cmd(adapter, 0, true);
1617 		rtw_indicate_disconnect(adapter);
1618 		rtw_free_assoc_resources(adapter, 0);
1619 	}
1620 
1621 	rtw_hal_get_def_var(adapter, HAL_DEF_IS_SUPPORT_ANT_DIV, &(supp_ant_div));
1622 	if (supp_ant_div) {
1623 		u8 cur_ant;
1624 		rtw_hal_get_def_var(adapter, HAL_DEF_CURRENT_ANTENNA, &(cur_ant));
1625 		DBG_88E("#### Opt_Ant_(%s), cur_Ant(%s)\n",
1626 			(2 == candidate->network.PhyInfo.Optimum_antenna) ? "A" : "B",
1627 			(2 == cur_ant) ? "A" : "B"
1628 		);
1629 	}
1630 
1631 	ret = rtw_joinbss_cmd(adapter, candidate);
1632 
1633 exit:
1634 	spin_unlock_bh(&pmlmepriv->scanned_queue.lock);
1635 
1636 	return ret;
1637 }
1638 
rtw_set_auth(struct adapter * adapter,struct security_priv * psecuritypriv)1639 int rtw_set_auth(struct adapter *adapter, struct security_priv *psecuritypriv)
1640 {
1641 	struct	cmd_obj *pcmd;
1642 	struct	setauth_parm *psetauthparm;
1643 	struct	cmd_priv *pcmdpriv = &adapter->cmdpriv;
1644 	int		res = _SUCCESS;
1645 
1646 	pcmd = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
1647 	if (!pcmd) {
1648 		res = _FAIL;  /* try again */
1649 		goto exit;
1650 	}
1651 
1652 	psetauthparm = kzalloc(sizeof(struct setauth_parm), GFP_KERNEL);
1653 	if (!psetauthparm) {
1654 		kfree(pcmd);
1655 		res = _FAIL;
1656 		goto exit;
1657 	}
1658 	memset(psetauthparm, 0, sizeof(struct setauth_parm));
1659 	psetauthparm->mode = (unsigned char)psecuritypriv->dot11AuthAlgrthm;
1660 	pcmd->cmdcode = _SetAuth_CMD_;
1661 	pcmd->parmbuf = (unsigned char *)psetauthparm;
1662 	pcmd->cmdsz =  (sizeof(struct setauth_parm));
1663 	pcmd->rsp = NULL;
1664 	pcmd->rspsz = 0;
1665 	INIT_LIST_HEAD(&pcmd->list);
1666 	res = rtw_enqueue_cmd(pcmdpriv, pcmd);
1667 exit:
1668 
1669 	return res;
1670 }
1671 
rtw_set_key(struct adapter * adapter,struct security_priv * psecuritypriv,int keyid,u8 set_tx)1672 int rtw_set_key(struct adapter *adapter, struct security_priv *psecuritypriv, int keyid, u8 set_tx)
1673 {
1674 	u8	keylen;
1675 	struct cmd_obj		*pcmd;
1676 	struct setkey_parm	*psetkeyparm;
1677 	struct cmd_priv		*pcmdpriv = &adapter->cmdpriv;
1678 	struct mlme_priv		*pmlmepriv = &adapter->mlmepriv;
1679 	int	res = _SUCCESS;
1680 
1681 	pcmd = kzalloc(sizeof(struct cmd_obj), GFP_KERNEL);
1682 	if (!pcmd) {
1683 		res = _FAIL;  /* try again */
1684 		goto exit;
1685 	}
1686 	psetkeyparm = kzalloc(sizeof(struct setkey_parm), GFP_KERNEL);
1687 	if (!psetkeyparm) {
1688 		kfree(pcmd);
1689 		res = _FAIL;
1690 		goto exit;
1691 	}
1692 
1693 	memset(psetkeyparm, 0, sizeof(struct setkey_parm));
1694 
1695 	if (psecuritypriv->dot11AuthAlgrthm == dot11AuthAlgrthm_8021X)
1696 		psetkeyparm->algorithm = (unsigned char)psecuritypriv->dot118021XGrpPrivacy;
1697 	else
1698 		psetkeyparm->algorithm = (u8)psecuritypriv->dot11PrivacyAlgrthm;
1699 	psetkeyparm->keyid = (u8)keyid;/* 0~3 */
1700 	psetkeyparm->set_tx = set_tx;
1701 	pmlmepriv->key_mask |= BIT(psetkeyparm->keyid);
1702 	DBG_88E("==> rtw_set_key algorithm(%x), keyid(%x), key_mask(%x)\n",
1703 		psetkeyparm->algorithm, psetkeyparm->keyid, pmlmepriv->key_mask);
1704 
1705 	switch (psetkeyparm->algorithm) {
1706 	case _WEP40_:
1707 		keylen = 5;
1708 		memcpy(&psetkeyparm->key[0], &psecuritypriv->dot11DefKey[keyid].skey[0], keylen);
1709 		break;
1710 	case _WEP104_:
1711 		keylen = 13;
1712 		memcpy(&psetkeyparm->key[0], &psecuritypriv->dot11DefKey[keyid].skey[0], keylen);
1713 		break;
1714 	case _TKIP_:
1715 		keylen = 16;
1716 		memcpy(&psetkeyparm->key, &psecuritypriv->dot118021XGrpKey[keyid], keylen);
1717 		psetkeyparm->grpkey = 1;
1718 		break;
1719 	case _AES_:
1720 		keylen = 16;
1721 		memcpy(&psetkeyparm->key, &psecuritypriv->dot118021XGrpKey[keyid], keylen);
1722 		psetkeyparm->grpkey = 1;
1723 		break;
1724 	default:
1725 		kfree(psetkeyparm);
1726 		kfree(pcmd);
1727 		res = _FAIL;
1728 		goto exit;
1729 	}
1730 	pcmd->cmdcode = _SetKey_CMD_;
1731 	pcmd->parmbuf = (u8 *)psetkeyparm;
1732 	pcmd->cmdsz =  (sizeof(struct setkey_parm));
1733 	pcmd->rsp = NULL;
1734 	pcmd->rspsz = 0;
1735 	INIT_LIST_HEAD(&pcmd->list);
1736 	res = rtw_enqueue_cmd(pcmdpriv, pcmd);
1737 exit:
1738 	return res;
1739 }
1740 
1741 /* adjust IEs for rtw_joinbss_cmd in WMM */
rtw_restruct_wmm_ie(struct adapter * adapter,u8 * in_ie,u8 * out_ie,uint in_len,uint initial_out_len)1742 int rtw_restruct_wmm_ie(struct adapter *adapter, u8 *in_ie, u8 *out_ie, uint in_len, uint initial_out_len)
1743 {
1744 	unsigned	int ielength = 0;
1745 	unsigned int i, j;
1746 
1747 	i = 12; /* after the fixed IE */
1748 	while (i < in_len) {
1749 		ielength = initial_out_len;
1750 
1751 		if (in_ie[i] == 0xDD && in_ie[i + 2] == 0x00 && in_ie[i + 3] == 0x50  && in_ie[i + 4] == 0xF2 && in_ie[i + 5] == 0x02 && i + 5 < in_len) {
1752 			/* WMM element ID and OUI */
1753 			/* Append WMM IE to the last index of out_ie */
1754 
1755 			for (j = i; j < i + 9; j++) {
1756 				out_ie[ielength] = in_ie[j];
1757 				ielength++;
1758 			}
1759 			out_ie[initial_out_len + 1] = 0x07;
1760 			out_ie[initial_out_len + 6] = 0x00;
1761 			out_ie[initial_out_len + 8] = 0x00;
1762 			break;
1763 		}
1764 		i += (in_ie[i + 1] + 2); /*  to the next IE element */
1765 	}
1766 	return ielength;
1767 }
1768 
1769 /*  */
1770 /*  Ported from 8185: IsInPreAuthKeyList(). (Renamed from SecIsInPreAuthKeyList(), 2006-10-13.) */
1771 /*  Added by Annie, 2006-05-07. */
1772 /*  */
1773 /*  Search by BSSID, */
1774 /*  Return Value: */
1775 /* 		-1		:if there is no pre-auth key in the  table */
1776 /* 		>= 0		:if there is pre-auth key, and   return the entry id */
1777 /*  */
1778 /*  */
1779 
SecIsInPMKIDList(struct adapter * Adapter,u8 * bssid)1780 static int SecIsInPMKIDList(struct adapter *Adapter, u8 *bssid)
1781 {
1782 	struct security_priv *psecuritypriv = &Adapter->securitypriv;
1783 	int i = 0;
1784 
1785 	do {
1786 		if ((psecuritypriv->PMKIDList[i].bUsed) &&
1787 		    (!memcmp(psecuritypriv->PMKIDList[i].Bssid, bssid, ETH_ALEN))) {
1788 			break;
1789 		} else {
1790 			i++;
1791 			/* continue; */
1792 		}
1793 
1794 	} while (i < NUM_PMKID_CACHE);
1795 
1796 	if (i == NUM_PMKID_CACHE) {
1797 		i = -1;/*  Could not find. */
1798 	} else {
1799 		/*  There is one Pre-Authentication Key for the specific BSSID. */
1800 	}
1801 	return i;
1802 }
1803 
1804 /*  */
1805 /*  Check the RSN IE length */
1806 /*  If the RSN IE length <= 20, the RSN IE didn't include the PMKID information */
1807 /*  0-11th element in the array are the fixed IE */
1808 /*  12th element in the array is the IE */
1809 /*  13th element in the array is the IE length */
1810 /*  */
1811 
rtw_append_pmkid(struct adapter * Adapter,int iEntry,u8 * ie,uint ie_len)1812 static int rtw_append_pmkid(struct adapter *Adapter, int iEntry, u8 *ie, uint ie_len)
1813 {
1814 	struct security_priv *psecuritypriv = &Adapter->securitypriv;
1815 
1816 	if (ie[13] <= 20) {
1817 		/*  The RSN IE didn't include the PMK ID, append the PMK information */
1818 		ie[ie_len] = 1;
1819 		ie_len++;
1820 		ie[ie_len] = 0;	/* PMKID count = 0x0100 */
1821 		ie_len++;
1822 		memcpy(&ie[ie_len], &psecuritypriv->PMKIDList[iEntry].PMKID, 16);
1823 
1824 		ie_len += 16;
1825 		ie[13] += 18;/* PMKID length = 2+16 */
1826 	}
1827 	return ie_len;
1828 }
1829 
rtw_restruct_sec_ie(struct adapter * adapter,u8 * in_ie,u8 * out_ie,uint in_len)1830 int rtw_restruct_sec_ie(struct adapter *adapter, u8 *in_ie, u8 *out_ie, uint in_len)
1831 {
1832 	u8 authmode = 0;
1833 	uint	ielength;
1834 	int iEntry;
1835 	struct mlme_priv *pmlmepriv = &adapter->mlmepriv;
1836 	struct security_priv *psecuritypriv = &adapter->securitypriv;
1837 	uint	ndisauthmode = psecuritypriv->ndisauthtype;
1838 
1839 	/* copy fixed ie only */
1840 	memcpy(out_ie, in_ie, 12);
1841 	ielength = 12;
1842 	if ((ndisauthmode == Ndis802_11AuthModeWPA) ||
1843 	    (ndisauthmode == Ndis802_11AuthModeWPAPSK))
1844 			authmode = _WPA_IE_ID_;
1845 	if ((ndisauthmode == Ndis802_11AuthModeWPA2) ||
1846 	    (ndisauthmode == Ndis802_11AuthModeWPA2PSK))
1847 		authmode = _WPA2_IE_ID_;
1848 
1849 	if (check_fwstate(pmlmepriv, WIFI_UNDER_WPS)) {
1850 		memcpy(out_ie + ielength, psecuritypriv->wps_ie, psecuritypriv->wps_ie_len);
1851 
1852 		ielength += psecuritypriv->wps_ie_len;
1853 	} else if ((authmode == _WPA_IE_ID_) || (authmode == _WPA2_IE_ID_)) {
1854 		/* copy RSN or SSN */
1855 		memcpy(&out_ie[ielength], &psecuritypriv->supplicant_ie[0], psecuritypriv->supplicant_ie[1] + 2);
1856 		ielength += psecuritypriv->supplicant_ie[1] + 2;
1857 		rtw_report_sec_ie(adapter, authmode, psecuritypriv->supplicant_ie);
1858 	}
1859 
1860 	iEntry = SecIsInPMKIDList(adapter, pmlmepriv->assoc_bssid);
1861 	if (iEntry < 0) {
1862 		return ielength;
1863 	} else {
1864 		if (authmode == _WPA2_IE_ID_)
1865 			ielength = rtw_append_pmkid(adapter, iEntry, out_ie, ielength);
1866 	}
1867 
1868 	return ielength;
1869 }
1870 
rtw_init_registrypriv_dev_network(struct adapter * adapter)1871 void rtw_init_registrypriv_dev_network(struct adapter *adapter)
1872 {
1873 	struct registry_priv *pregistrypriv = &adapter->registrypriv;
1874 	struct eeprom_priv *peepriv = &adapter->eeprompriv;
1875 	struct wlan_bssid_ex    *pdev_network = &pregistrypriv->dev_network;
1876 	u8 *myhwaddr = myid(peepriv);
1877 
1878 	memcpy(pdev_network->MacAddress, myhwaddr, ETH_ALEN);
1879 
1880 	memcpy(&pdev_network->Ssid, &pregistrypriv->ssid, sizeof(struct ndis_802_11_ssid));
1881 
1882 	pdev_network->Configuration.Length = sizeof(struct ndis_802_11_config);
1883 	pdev_network->Configuration.BeaconPeriod = 100;
1884 	pdev_network->Configuration.FHConfig.Length = 0;
1885 	pdev_network->Configuration.FHConfig.HopPattern = 0;
1886 	pdev_network->Configuration.FHConfig.HopSet = 0;
1887 	pdev_network->Configuration.FHConfig.DwellTime = 0;
1888 
1889 }
1890 
rtw_update_registrypriv_dev_network(struct adapter * adapter)1891 void rtw_update_registrypriv_dev_network(struct adapter *adapter)
1892 {
1893 	int sz = 0;
1894 	struct registry_priv *pregistrypriv = &adapter->registrypriv;
1895 	struct wlan_bssid_ex    *pdev_network = &pregistrypriv->dev_network;
1896 	struct	security_priv *psecuritypriv = &adapter->securitypriv;
1897 	struct	wlan_network	*cur_network = &adapter->mlmepriv.cur_network;
1898 
1899 	pdev_network->Privacy = (psecuritypriv->dot11PrivacyAlgrthm > 0 ? 1 : 0); /*  adhoc no 802.1x */
1900 
1901 	pdev_network->Rssi = 0;
1902 
1903 	switch (pregistrypriv->wireless_mode) {
1904 	case WIRELESS_11B:
1905 		pdev_network->NetworkTypeInUse = (Ndis802_11DS);
1906 		break;
1907 	case WIRELESS_11G:
1908 	case WIRELESS_11BG:
1909 	case WIRELESS_11_24N:
1910 	case WIRELESS_11G_24N:
1911 	case WIRELESS_11BG_24N:
1912 		pdev_network->NetworkTypeInUse = (Ndis802_11OFDM24);
1913 		break;
1914 	default:
1915 		/*  TODO */
1916 		break;
1917 	}
1918 
1919 	pdev_network->Configuration.DSConfig = (pregistrypriv->channel);
1920 
1921 	if (cur_network->network.InfrastructureMode == Ndis802_11IBSS)
1922 		pdev_network->Configuration.ATIMWindow = (0);
1923 
1924 	pdev_network->InfrastructureMode = (cur_network->network.InfrastructureMode);
1925 
1926 	/*  1. Supported rates */
1927 	/*  2. IE */
1928 
1929 	sz = rtw_generate_ie(pregistrypriv);
1930 	pdev_network->IELength = sz;
1931 	pdev_network->Length = get_wlan_bssid_ex_sz((struct wlan_bssid_ex  *)pdev_network);
1932 
1933 	/* notes: translate IELength & Length after assign the Length to cmdsz in createbss_cmd(); */
1934 	/* pdev_network->IELength = cpu_to_le32(sz); */
1935 
1936 }
1937 
rtw_get_encrypt_decrypt_from_registrypriv(struct adapter * adapter)1938 void rtw_get_encrypt_decrypt_from_registrypriv(struct adapter *adapter)
1939 {
1940 
1941 }
1942 
1943 /* the function is at passive_level */
rtw_joinbss_reset(struct adapter * padapter)1944 void rtw_joinbss_reset(struct adapter *padapter)
1945 {
1946 	u8	threshold;
1947 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
1948 	struct ht_priv		*phtpriv = &pmlmepriv->htpriv;
1949 
1950 	/* todo: if you want to do something io/reg/hw setting before join_bss, please add code here */
1951 	pmlmepriv->num_FortyMHzIntolerant = 0;
1952 
1953 	pmlmepriv->num_sta_no_ht = 0;
1954 
1955 	phtpriv->ampdu_enable = false;/* reset to disabled */
1956 
1957 	/*  TH = 1 => means that invalidate usb rx aggregation */
1958 	/*  TH = 0 => means that validate usb rx aggregation, use init value. */
1959 	if (phtpriv->ht_option) {
1960 		if (padapter->registrypriv.wifi_spec == 1)
1961 			threshold = 1;
1962 		else
1963 			threshold = 0;
1964 		rtw_hal_set_hwreg(padapter, HW_VAR_RXDMA_AGG_PG_TH, (u8 *)(&threshold));
1965 	} else {
1966 		threshold = 1;
1967 		rtw_hal_set_hwreg(padapter, HW_VAR_RXDMA_AGG_PG_TH, (u8 *)(&threshold));
1968 	}
1969 }
1970 
1971 /* the function is >= passive_level */
rtw_restructure_ht_ie(struct adapter * padapter,u8 * in_ie,u8 * out_ie,uint in_len,uint * pout_len)1972 unsigned int rtw_restructure_ht_ie(struct adapter *padapter, u8 *in_ie, u8 *out_ie, uint in_len, uint *pout_len)
1973 {
1974 	u32 ielen, out_len;
1975 	enum ht_cap_ampdu_factor max_rx_ampdu_factor;
1976 	unsigned char *p;
1977 	struct ieee80211_ht_cap ht_capie;
1978 	unsigned char WMM_IE[] = {0x00, 0x50, 0xf2, 0x02, 0x00, 0x01, 0x00};
1979 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
1980 	struct qos_priv		*pqospriv = &pmlmepriv->qospriv;
1981 	struct ht_priv		*phtpriv = &pmlmepriv->htpriv;
1982 	u32 rx_packet_offset, max_recvbuf_sz;
1983 
1984 	phtpriv->ht_option = false;
1985 
1986 	p = rtw_get_ie(in_ie + 12, _HT_CAPABILITY_IE_, &ielen, in_len - 12);
1987 
1988 	if (p && ielen > 0) {
1989 		if (pqospriv->qos_option == 0) {
1990 			out_len = *pout_len;
1991 			rtw_set_ie(out_ie + out_len, _VENDOR_SPECIFIC_IE_,
1992 				   _WMM_IE_Length_, WMM_IE, pout_len);
1993 
1994 			pqospriv->qos_option = 1;
1995 		}
1996 
1997 		out_len = *pout_len;
1998 
1999 		memset(&ht_capie, 0, sizeof(struct ieee80211_ht_cap));
2000 
2001 		ht_capie.cap_info = cpu_to_le16(IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
2002 						IEEE80211_HT_CAP_SGI_20 |
2003 						IEEE80211_HT_CAP_SGI_40 |
2004 						IEEE80211_HT_CAP_TX_STBC |
2005 						IEEE80211_HT_CAP_DSSSCCK40);
2006 
2007 		rtw_hal_get_def_var(padapter, HAL_DEF_RX_PACKET_OFFSET, &rx_packet_offset);
2008 		rtw_hal_get_def_var(padapter, HAL_DEF_MAX_RECVBUF_SZ, &max_recvbuf_sz);
2009 
2010 		/*
2011 		AMPDU_para [1:0]:Max AMPDU Len => 0:8k , 1:16k, 2:32k, 3:64k
2012 		AMPDU_para [4:2]:Min MPDU Start Spacing
2013 		*/
2014 
2015 		rtw_hal_get_def_var(padapter, HW_VAR_MAX_RX_AMPDU_FACTOR, &max_rx_ampdu_factor);
2016 		ht_capie.ampdu_params_info = (max_rx_ampdu_factor & 0x03);
2017 
2018 		if (padapter->securitypriv.dot11PrivacyAlgrthm == _AES_)
2019 			ht_capie.ampdu_params_info |= (IEEE80211_HT_AMPDU_PARM_DENSITY & (0x07 << 2));
2020 		else
2021 			ht_capie.ampdu_params_info |= (IEEE80211_HT_AMPDU_PARM_DENSITY & 0x00);
2022 
2023 		rtw_set_ie(out_ie + out_len, _HT_CAPABILITY_IE_,
2024 			   sizeof(struct ieee80211_ht_cap), (unsigned char *)&ht_capie, pout_len);
2025 
2026 		phtpriv->ht_option = true;
2027 
2028 		p = rtw_get_ie(in_ie + 12, _HT_ADD_INFO_IE_, &ielen, in_len - 12);
2029 		if (p && (ielen == sizeof(struct ieee80211_ht_addt_info))) {
2030 			out_len = *pout_len;
2031 			rtw_set_ie(out_ie + out_len, _HT_ADD_INFO_IE_, ielen, p + 2, pout_len);
2032 		}
2033 	}
2034 	return phtpriv->ht_option;
2035 }
2036 
2037 /* the function is > passive_level (in critical_section) */
rtw_update_ht_cap(struct adapter * padapter,u8 * pie,uint ie_len)2038 void rtw_update_ht_cap(struct adapter *padapter, u8 *pie, uint ie_len)
2039 {
2040 	u8 *p, max_ampdu_sz;
2041 	int len;
2042 	struct ieee80211_ht_cap *pht_capie;
2043 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
2044 	struct ht_priv		*phtpriv = &pmlmepriv->htpriv;
2045 	struct registry_priv *pregistrypriv = &padapter->registrypriv;
2046 	struct mlme_ext_priv	*pmlmeext = &padapter->mlmeextpriv;
2047 	struct mlme_ext_info	*pmlmeinfo = &pmlmeext->mlmext_info;
2048 
2049 	if (!phtpriv->ht_option)
2050 		return;
2051 
2052 	if ((!pmlmeinfo->HT_info_enable) || (!pmlmeinfo->HT_caps_enable))
2053 		return;
2054 
2055 	DBG_88E("+rtw_update_ht_cap()\n");
2056 
2057 	/* maybe needs check if ap supports rx ampdu. */
2058 	if ((!phtpriv->ampdu_enable) && (pregistrypriv->ampdu_enable == 1)) {
2059 		if (pregistrypriv->wifi_spec == 1)
2060 			phtpriv->ampdu_enable = false;
2061 		else
2062 			phtpriv->ampdu_enable = true;
2063 	} else if (pregistrypriv->ampdu_enable == 2) {
2064 		phtpriv->ampdu_enable = true;
2065 	}
2066 
2067 	/* check Max Rx A-MPDU Size */
2068 	len = 0;
2069 	p = rtw_get_ie(pie + sizeof(struct ndis_802_11_fixed_ie), _HT_CAPABILITY_IE_, &len, ie_len - sizeof(struct ndis_802_11_fixed_ie));
2070 	if (p && len > 0) {
2071 		pht_capie = (struct ieee80211_ht_cap *)(p + 2);
2072 		max_ampdu_sz = (pht_capie->ampdu_params_info & IEEE80211_HT_AMPDU_PARM_FACTOR);
2073 		max_ampdu_sz = 1 << (max_ampdu_sz + 3); /*  max_ampdu_sz (kbytes); */
2074 		phtpriv->rx_ampdu_maxlen = max_ampdu_sz;
2075 	}
2076 	len = 0;
2077 	p = rtw_get_ie(pie + sizeof(struct ndis_802_11_fixed_ie), _HT_ADD_INFO_IE_, &len, ie_len - sizeof(struct ndis_802_11_fixed_ie));
2078 
2079 	/* update cur_bwmode & cur_ch_offset */
2080 	if ((pregistrypriv->cbw40_enable) &&
2081 	    (le16_to_cpu(pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info) & BIT(1)) &&
2082 	    (pmlmeinfo->HT_info.infos[0] & BIT(2))) {
2083 		int i;
2084 		u8	rf_type;
2085 
2086 		padapter->HalFunc.GetHwRegHandler(padapter, HW_VAR_RF_TYPE, (u8 *)(&rf_type));
2087 
2088 		/* update the MCS rates */
2089 		for (i = 0; i < 16; i++) {
2090 			if ((rf_type == RF_1T1R) || (rf_type == RF_1T2R))
2091 				pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate[i] &= MCS_rate_1R[i];
2092 			else
2093 				pmlmeinfo->HT_caps.u.HT_cap_element.MCS_rate[i] &= MCS_rate_2R[i];
2094 		}
2095 		/* switch to the 40M Hz mode according to the AP */
2096 		pmlmeext->cur_bwmode = HT_CHANNEL_WIDTH_40;
2097 		switch ((pmlmeinfo->HT_info.infos[0] & 0x3)) {
2098 		case HT_EXTCHNL_OFFSET_UPPER:
2099 			pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_LOWER;
2100 			break;
2101 		case HT_EXTCHNL_OFFSET_LOWER:
2102 			pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_UPPER;
2103 			break;
2104 		default:
2105 			pmlmeext->cur_ch_offset = HAL_PRIME_CHNL_OFFSET_DONT_CARE;
2106 			break;
2107 		}
2108 	}
2109 
2110 	/*  Config SM Power Save setting */
2111 	pmlmeinfo->SM_PS = (le16_to_cpu(pmlmeinfo->HT_caps.u.HT_cap_element.HT_caps_info) & 0x0C) >> 2;
2112 	if (pmlmeinfo->SM_PS == WLAN_HT_CAP_SM_PS_STATIC)
2113 		DBG_88E("%s(): WLAN_HT_CAP_SM_PS_STATIC\n", __func__);
2114 
2115 	/*  Config current HT Protection mode. */
2116 	pmlmeinfo->HT_protection = pmlmeinfo->HT_info.infos[1] & 0x3;
2117 }
2118 
rtw_issue_addbareq_cmd(struct adapter * padapter,struct xmit_frame * pxmitframe)2119 void rtw_issue_addbareq_cmd(struct adapter *padapter, struct xmit_frame *pxmitframe)
2120 {
2121 	u8 issued;
2122 	int priority;
2123 	struct sta_info *psta = NULL;
2124 	struct ht_priv	*phtpriv;
2125 	struct pkt_attrib *pattrib = &pxmitframe->attrib;
2126 
2127 	if (is_multicast_ether_addr(pattrib->ra) ||
2128 	    padapter->mlmepriv.LinkDetectInfo.NumTxOkInPeriod < 100)
2129 		return;
2130 
2131 	priority = pattrib->priority;
2132 
2133 	if (pattrib->psta)
2134 		psta = pattrib->psta;
2135 	else
2136 		psta = rtw_get_stainfo(&padapter->stapriv, pattrib->ra);
2137 
2138 	if (!psta)
2139 		return;
2140 
2141 	phtpriv = &psta->htpriv;
2142 
2143 	if ((phtpriv->ht_option) && (phtpriv->ampdu_enable)) {
2144 		issued = (phtpriv->agg_enable_bitmap >> priority) & 0x1;
2145 		issued |= (phtpriv->candidate_tid_bitmap >> priority) & 0x1;
2146 
2147 		if (0 == issued) {
2148 			DBG_88E("rtw_issue_addbareq_cmd, p=%d\n", priority);
2149 			psta->htpriv.candidate_tid_bitmap |= BIT((u8)priority);
2150 			rtw_addbareq_cmd(padapter, (u8)priority, pattrib->ra);
2151 		}
2152 	}
2153 }
2154 
rtw_roaming(struct adapter * padapter,struct wlan_network * tgt_network)2155 void rtw_roaming(struct adapter *padapter, struct wlan_network *tgt_network)
2156 {
2157 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
2158 
2159 	spin_lock_bh(&pmlmepriv->lock);
2160 	_rtw_roaming(padapter, tgt_network);
2161 	spin_unlock_bh(&pmlmepriv->lock);
2162 }
_rtw_roaming(struct adapter * padapter,struct wlan_network * tgt_network)2163 void _rtw_roaming(struct adapter *padapter, struct wlan_network *tgt_network)
2164 {
2165 	struct mlme_priv	*pmlmepriv = &padapter->mlmepriv;
2166 	int do_join_r;
2167 
2168 	struct wlan_network *pnetwork;
2169 
2170 	if (tgt_network)
2171 		pnetwork = tgt_network;
2172 	else
2173 		pnetwork = &pmlmepriv->cur_network;
2174 
2175 	if (0 < rtw_to_roaming(padapter)) {
2176 		DBG_88E("roaming from %s(%pM length:%d\n",
2177 			pnetwork->network.Ssid.Ssid, pnetwork->network.MacAddress,
2178 			pnetwork->network.Ssid.SsidLength);
2179 		memcpy(&pmlmepriv->assoc_ssid, &pnetwork->network.Ssid, sizeof(struct ndis_802_11_ssid));
2180 
2181 		pmlmepriv->assoc_by_bssid = false;
2182 
2183 		while (1) {
2184 			do_join_r = rtw_do_join(padapter);
2185 			if (_SUCCESS == do_join_r) {
2186 				break;
2187 			} else {
2188 				DBG_88E("roaming do_join return %d\n", do_join_r);
2189 				pmlmepriv->to_roaming--;
2190 
2191 				if (0 < pmlmepriv->to_roaming) {
2192 					continue;
2193 				} else {
2194 					DBG_88E("%s(%d) -to roaming fail, indicate_disconnect\n", __func__, __LINE__);
2195 					rtw_indicate_disconnect(padapter);
2196 					break;
2197 				}
2198 			}
2199 		}
2200 	}
2201 }
2202