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