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