xref: /linux/drivers/staging/rtl8723bs/core/rtw_sta_mgt.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #include <drv_types.h>
8 
9 void _rtw_init_stainfo(struct sta_info *psta);
_rtw_init_stainfo(struct sta_info * psta)10 void _rtw_init_stainfo(struct sta_info *psta)
11 {
12 	memset((u8 *)psta, 0, sizeof(struct sta_info));
13 
14 	spin_lock_init(&psta->lock);
15 	INIT_LIST_HEAD(&psta->list);
16 	INIT_LIST_HEAD(&psta->hash_list);
17 	/* INIT_LIST_HEAD(&psta->asoc_list); */
18 	/* INIT_LIST_HEAD(&psta->sleep_list); */
19 	/* INIT_LIST_HEAD(&psta->wakeup_list); */
20 
21 	INIT_LIST_HEAD(&psta->sleep_q.queue);
22 	spin_lock_init(&psta->sleep_q.lock);
23 	psta->sleepq_len = 0;
24 
25 	_rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
26 	_rtw_init_sta_recv_priv(&psta->sta_recvpriv);
27 
28 	INIT_LIST_HEAD(&psta->asoc_list);
29 
30 	INIT_LIST_HEAD(&psta->auth_list);
31 
32 	psta->expire_to = 0;
33 
34 	psta->flags = 0;
35 
36 	psta->capability = 0;
37 
38 	psta->bpairwise_key_installed = false;
39 
40 	psta->nonerp_set = 0;
41 	psta->no_short_slot_time_set = 0;
42 	psta->no_short_preamble_set = 0;
43 	psta->no_ht_gf_set = 0;
44 	psta->no_ht_set = 0;
45 	psta->ht_20mhz_set = 0;
46 
47 	psta->under_exist_checking = 0;
48 
49 	psta->keep_alive_trycnt = 0;
50 }
51 
_rtw_init_sta_priv(struct sta_priv * pstapriv)52 u32 _rtw_init_sta_priv(struct	sta_priv *pstapriv)
53 {
54 	struct sta_info *psta;
55 	s32 i;
56 
57 	pstapriv->pallocated_stainfo_buf = vzalloc(sizeof(struct sta_info) * NUM_STA+4);
58 
59 	if (!pstapriv->pallocated_stainfo_buf)
60 		return _FAIL;
61 
62 	pstapriv->pstainfo_buf = pstapriv->pallocated_stainfo_buf + 4 -
63 		((SIZE_PTR)(pstapriv->pallocated_stainfo_buf) & 3);
64 
65 	INIT_LIST_HEAD(&pstapriv->free_sta_queue.queue);
66 	spin_lock_init(&pstapriv->free_sta_queue.lock);
67 
68 	spin_lock_init(&pstapriv->sta_hash_lock);
69 
70 	/* _rtw_init_queue(&pstapriv->asoc_q); */
71 	pstapriv->asoc_sta_count = 0;
72 	INIT_LIST_HEAD(&pstapriv->sleep_q.queue);
73 	spin_lock_init(&pstapriv->sleep_q.lock);
74 	INIT_LIST_HEAD(&pstapriv->wakeup_q.queue);
75 	spin_lock_init(&pstapriv->wakeup_q.lock);
76 
77 	psta = (struct sta_info *)(pstapriv->pstainfo_buf);
78 
79 	for (i = 0; i < NUM_STA; i++) {
80 		_rtw_init_stainfo(psta);
81 
82 		INIT_LIST_HEAD(&(pstapriv->sta_hash[i]));
83 
84 		list_add_tail(&psta->list, get_list_head(&pstapriv->free_sta_queue));
85 
86 		psta++;
87 	}
88 
89 	pstapriv->sta_dz_bitmap = 0;
90 	pstapriv->tim_bitmap = 0;
91 
92 	INIT_LIST_HEAD(&pstapriv->asoc_list);
93 	INIT_LIST_HEAD(&pstapriv->auth_list);
94 	spin_lock_init(&pstapriv->asoc_list_lock);
95 	spin_lock_init(&pstapriv->auth_list_lock);
96 	pstapriv->asoc_list_cnt = 0;
97 	pstapriv->auth_list_cnt = 0;
98 
99 	pstapriv->auth_to = 3; /*  3*2 = 6 sec */
100 	pstapriv->assoc_to = 3;
101 	pstapriv->expire_to = 3; /*  3*2 = 6 sec */
102 	pstapriv->max_num_sta = NUM_STA;
103 	return _SUCCESS;
104 }
105 
rtw_stainfo_offset(struct sta_priv * stapriv,struct sta_info * sta)106 inline int rtw_stainfo_offset(struct sta_priv *stapriv, struct sta_info *sta)
107 {
108 	int offset = (((u8 *)sta) - stapriv->pstainfo_buf)/sizeof(struct sta_info);
109 
110 	return offset;
111 }
112 
rtw_get_stainfo_by_offset(struct sta_priv * stapriv,int offset)113 inline struct sta_info *rtw_get_stainfo_by_offset(struct sta_priv *stapriv, int offset)
114 {
115 	return (struct sta_info *)(stapriv->pstainfo_buf + offset * sizeof(struct sta_info));
116 }
117 
118 /*  this function is used to free the memory of lock || sema for all stainfos */
119 void kfree_all_stainfo(struct sta_priv *pstapriv);
kfree_all_stainfo(struct sta_priv * pstapriv)120 void kfree_all_stainfo(struct sta_priv *pstapriv)
121 {
122 	struct list_head	*plist, *phead;
123 
124 	spin_lock_bh(&pstapriv->sta_hash_lock);
125 
126 	phead = get_list_head(&pstapriv->free_sta_queue);
127 	plist = get_next(phead);
128 
129 	while (phead != plist)
130 		plist = get_next(plist);
131 
132 	spin_unlock_bh(&pstapriv->sta_hash_lock);
133 }
134 
135 void kfree_sta_priv_lock(struct	sta_priv *pstapriv);
kfree_sta_priv_lock(struct sta_priv * pstapriv)136 void kfree_sta_priv_lock(struct	sta_priv *pstapriv)
137 {
138 	 kfree_all_stainfo(pstapriv); /* be done before free sta_hash_lock */
139 }
140 
_rtw_free_sta_priv(struct sta_priv * pstapriv)141 u32 _rtw_free_sta_priv(struct	sta_priv *pstapriv)
142 {
143 	struct list_head	*phead, *plist;
144 	struct sta_info *psta = NULL;
145 	struct recv_reorder_ctrl *preorder_ctrl;
146 	int	index;
147 
148 	if (pstapriv) {
149 		/*delete all reordering_ctrl_timer		*/
150 		spin_lock_bh(&pstapriv->sta_hash_lock);
151 		for (index = 0; index < NUM_STA; index++) {
152 			phead = &(pstapriv->sta_hash[index]);
153 			list_for_each(plist, phead) {
154 				int i;
155 
156 				psta = list_entry(plist, struct sta_info,
157 						  hash_list);
158 
159 				for (i = 0; i < 16 ; i++) {
160 					preorder_ctrl = &psta->recvreorder_ctrl[i];
161 					del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
162 				}
163 			}
164 		}
165 		spin_unlock_bh(&pstapriv->sta_hash_lock);
166 		/*===============================*/
167 
168 		kfree_sta_priv_lock(pstapriv);
169 
170 		vfree(pstapriv->pallocated_stainfo_buf);
171 	}
172 	return _SUCCESS;
173 }
174 
175 /* struct	sta_info *rtw_alloc_stainfo(_queue *pfree_sta_queue, unsigned char *hwaddr) */
rtw_alloc_stainfo(struct sta_priv * pstapriv,u8 * hwaddr)176 struct	sta_info *rtw_alloc_stainfo(struct	sta_priv *pstapriv, u8 *hwaddr)
177 {
178 	s32	index;
179 	struct list_head	*phash_list;
180 	struct sta_info *psta;
181 	struct __queue *pfree_sta_queue;
182 	struct recv_reorder_ctrl *preorder_ctrl;
183 	int i = 0;
184 	u16  wRxSeqInitialValue = 0xffff;
185 
186 	pfree_sta_queue = &pstapriv->free_sta_queue;
187 
188 	/* spin_lock_bh(&(pfree_sta_queue->lock)); */
189 	spin_lock_bh(&(pstapriv->sta_hash_lock));
190 	if (list_empty(&pfree_sta_queue->queue)) {
191 		/* spin_unlock_bh(&(pfree_sta_queue->lock)); */
192 		spin_unlock_bh(&(pstapriv->sta_hash_lock));
193 		return NULL;
194 	} else {
195 		psta = container_of(get_next(&pfree_sta_queue->queue), struct sta_info, list);
196 
197 		list_del_init(&(psta->list));
198 
199 		/* spin_unlock_bh(&(pfree_sta_queue->lock)); */
200 
201 		_rtw_init_stainfo(psta);
202 
203 		psta->padapter = pstapriv->padapter;
204 
205 		memcpy(psta->hwaddr, hwaddr, ETH_ALEN);
206 
207 		index = wifi_mac_hash(hwaddr);
208 
209 		if (index >= NUM_STA) {
210 			spin_unlock_bh(&(pstapriv->sta_hash_lock));
211 			psta = NULL;
212 			goto exit;
213 		}
214 		phash_list = &(pstapriv->sta_hash[index]);
215 
216 		/* spin_lock_bh(&(pstapriv->sta_hash_lock)); */
217 
218 		list_add_tail(&psta->hash_list, phash_list);
219 
220 		pstapriv->asoc_sta_count++;
221 
222 		/* spin_unlock_bh(&(pstapriv->sta_hash_lock)); */
223 
224 /*  Commented by Albert 2009/08/13 */
225 /*  For the SMC router, the sequence number of first packet of WPS handshake will be 0. */
226 /*  In this case, this packet will be dropped by recv_decache function if we use the 0x00 as the default value for tid_rxseq variable. */
227 /*  So, we initialize the tid_rxseq variable as the 0xffff. */
228 
229 		for (i = 0; i < 16; i++)
230 			memcpy(&psta->sta_recvpriv.rxcache.tid_rxseq[i], &wRxSeqInitialValue, 2);
231 
232 		init_addba_retry_timer(pstapriv->padapter, psta);
233 
234 		/* for A-MPDU Rx reordering buffer control */
235 		for (i = 0; i < 16 ; i++) {
236 			preorder_ctrl = &psta->recvreorder_ctrl[i];
237 
238 			preorder_ctrl->padapter = pstapriv->padapter;
239 
240 			preorder_ctrl->enable = false;
241 
242 			preorder_ctrl->indicate_seq = 0xffff;
243 			preorder_ctrl->wend_b = 0xffff;
244 			/* preorder_ctrl->wsize_b = (NR_RECVBUFF-2); */
245 			preorder_ctrl->wsize_b = 64;/* 64; */
246 
247 			INIT_LIST_HEAD(&preorder_ctrl->pending_recvframe_queue.queue);
248 			spin_lock_init(&preorder_ctrl->pending_recvframe_queue.lock);
249 
250 			rtw_init_recv_timer(preorder_ctrl);
251 		}
252 
253 		/* init for DM */
254 		psta->rssi_stat.UndecoratedSmoothedPWDB = (-1);
255 		psta->rssi_stat.UndecoratedSmoothedCCK = (-1);
256 
257 		/* init for the sequence number of received management frame */
258 		psta->RxMgmtFrameSeqNum = 0xffff;
259 		spin_unlock_bh(&(pstapriv->sta_hash_lock));
260 		/* alloc mac id for non-bc/mc station, */
261 		rtw_alloc_macid(pstapriv->padapter, psta);
262 	}
263 
264 exit:
265 
266 	return psta;
267 }
268 
rtw_free_stainfo(struct adapter * padapter,struct sta_info * psta)269 u32 rtw_free_stainfo(struct adapter *padapter, struct sta_info *psta)
270 {
271 	int i;
272 	struct __queue *pfree_sta_queue;
273 	struct recv_reorder_ctrl *preorder_ctrl;
274 	struct	sta_xmit_priv *pstaxmitpriv;
275 	struct	xmit_priv *pxmitpriv = &padapter->xmitpriv;
276 	struct	sta_priv *pstapriv = &padapter->stapriv;
277 	struct hw_xmit *phwxmit;
278 
279 	if (!psta)
280 		goto exit;
281 
282 	spin_lock_bh(&psta->lock);
283 	psta->state &= ~_FW_LINKED;
284 	spin_unlock_bh(&psta->lock);
285 
286 	pfree_sta_queue = &pstapriv->free_sta_queue;
287 
288 	pstaxmitpriv = &psta->sta_xmitpriv;
289 
290 	/* list_del_init(&psta->sleep_list); */
291 
292 	/* list_del_init(&psta->wakeup_list); */
293 
294 	spin_lock_bh(&pxmitpriv->lock);
295 
296 	rtw_free_xmitframe_queue(pxmitpriv, &psta->sleep_q);
297 	psta->sleepq_len = 0;
298 
299 	/* vo */
300 	/* spin_lock_bh(&(pxmitpriv->vo_pending.lock)); */
301 	rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vo_q.sta_pending);
302 	list_del_init(&(pstaxmitpriv->vo_q.tx_pending));
303 	phwxmit = pxmitpriv->hwxmits;
304 	phwxmit->accnt -= pstaxmitpriv->vo_q.qcnt;
305 	pstaxmitpriv->vo_q.qcnt = 0;
306 	/* spin_unlock_bh(&(pxmitpriv->vo_pending.lock)); */
307 
308 	/* vi */
309 	/* spin_lock_bh(&(pxmitpriv->vi_pending.lock)); */
310 	rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vi_q.sta_pending);
311 	list_del_init(&(pstaxmitpriv->vi_q.tx_pending));
312 	phwxmit = pxmitpriv->hwxmits+1;
313 	phwxmit->accnt -= pstaxmitpriv->vi_q.qcnt;
314 	pstaxmitpriv->vi_q.qcnt = 0;
315 	/* spin_unlock_bh(&(pxmitpriv->vi_pending.lock)); */
316 
317 	/* be */
318 	/* spin_lock_bh(&(pxmitpriv->be_pending.lock)); */
319 	rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->be_q.sta_pending);
320 	list_del_init(&(pstaxmitpriv->be_q.tx_pending));
321 	phwxmit = pxmitpriv->hwxmits+2;
322 	phwxmit->accnt -= pstaxmitpriv->be_q.qcnt;
323 	pstaxmitpriv->be_q.qcnt = 0;
324 	/* spin_unlock_bh(&(pxmitpriv->be_pending.lock)); */
325 
326 	/* bk */
327 	/* spin_lock_bh(&(pxmitpriv->bk_pending.lock)); */
328 	rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->bk_q.sta_pending);
329 	list_del_init(&(pstaxmitpriv->bk_q.tx_pending));
330 	phwxmit = pxmitpriv->hwxmits+3;
331 	phwxmit->accnt -= pstaxmitpriv->bk_q.qcnt;
332 	pstaxmitpriv->bk_q.qcnt = 0;
333 	/* spin_unlock_bh(&(pxmitpriv->bk_pending.lock)); */
334 
335 	spin_unlock_bh(&pxmitpriv->lock);
336 
337 	spin_lock_bh(&pstapriv->sta_hash_lock);
338 	list_del_init(&psta->hash_list);
339 	pstapriv->asoc_sta_count--;
340 	spin_unlock_bh(&pstapriv->sta_hash_lock);
341 
342 	/*  re-init sta_info; 20061114 will be init in alloc_stainfo */
343 	/* _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv); */
344 	/* _rtw_init_sta_recv_priv(&psta->sta_recvpriv); */
345 
346 	del_timer_sync(&psta->addba_retry_timer);
347 
348 	/* for A-MPDU Rx reordering buffer control, cancel reordering_ctrl_timer */
349 	for (i = 0; i < 16 ; i++) {
350 		struct list_head	*phead, *plist;
351 		union recv_frame *prframe;
352 		struct __queue *ppending_recvframe_queue;
353 		struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
354 
355 		preorder_ctrl = &psta->recvreorder_ctrl[i];
356 
357 		del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
358 
359 		ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
360 
361 		spin_lock_bh(&ppending_recvframe_queue->lock);
362 
363 		phead =		get_list_head(ppending_recvframe_queue);
364 		plist = get_next(phead);
365 
366 		while (!list_empty(phead)) {
367 			prframe = (union recv_frame *)plist;
368 
369 			plist = get_next(plist);
370 
371 			list_del_init(&(prframe->u.hdr.list));
372 
373 			rtw_free_recvframe(prframe, pfree_recv_queue);
374 		}
375 
376 		spin_unlock_bh(&ppending_recvframe_queue->lock);
377 	}
378 
379 	if (!(psta->state & WIFI_AP_STATE))
380 		rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, false);
381 
382 	/* release mac id for non-bc/mc station, */
383 	rtw_release_macid(pstapriv->padapter, psta);
384 
385 /*
386 	spin_lock_bh(&pstapriv->asoc_list_lock);
387 	list_del_init(&psta->asoc_list);
388 	spin_unlock_bh(&pstapriv->asoc_list_lock);
389 */
390 	spin_lock_bh(&pstapriv->auth_list_lock);
391 	if (!list_empty(&psta->auth_list)) {
392 		list_del_init(&psta->auth_list);
393 		pstapriv->auth_list_cnt--;
394 	}
395 	spin_unlock_bh(&pstapriv->auth_list_lock);
396 
397 	psta->expire_to = 0;
398 	psta->sleepq_ac_len = 0;
399 	psta->qos_info = 0;
400 
401 	psta->max_sp_len = 0;
402 	psta->uapsd_bk = 0;
403 	psta->uapsd_be = 0;
404 	psta->uapsd_vi = 0;
405 	psta->uapsd_vo = 0;
406 
407 	psta->has_legacy_ac = 0;
408 
409 	pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
410 	pstapriv->tim_bitmap &= ~BIT(psta->aid);
411 
412 	if ((psta->aid > 0) && (pstapriv->sta_aid[psta->aid - 1] == psta)) {
413 		pstapriv->sta_aid[psta->aid - 1] = NULL;
414 		psta->aid = 0;
415 	}
416 
417 	psta->under_exist_checking = 0;
418 
419 	/* spin_lock_bh(&(pfree_sta_queue->lock)); */
420 	list_add_tail(&psta->list, get_list_head(pfree_sta_queue));
421 	/* spin_unlock_bh(&(pfree_sta_queue->lock)); */
422 
423 exit:
424 	return _SUCCESS;
425 }
426 
427 /*  free all stainfo which in sta_hash[all] */
rtw_free_all_stainfo(struct adapter * padapter)428 void rtw_free_all_stainfo(struct adapter *padapter)
429 {
430 	struct list_head *plist, *phead, *tmp;
431 	s32	index;
432 	struct sta_info *psta = NULL;
433 	struct	sta_priv *pstapriv = &padapter->stapriv;
434 	struct sta_info *pbcmc_stainfo = rtw_get_bcmc_stainfo(padapter);
435 	LIST_HEAD(stainfo_free_list);
436 
437 	if (pstapriv->asoc_sta_count == 1)
438 		return;
439 
440 	spin_lock_bh(&pstapriv->sta_hash_lock);
441 
442 	for (index = 0; index < NUM_STA; index++) {
443 		phead = &(pstapriv->sta_hash[index]);
444 		list_for_each_safe(plist, tmp, phead) {
445 			psta = list_entry(plist, struct sta_info, hash_list);
446 
447 			if (pbcmc_stainfo != psta)
448 				list_move(&psta->hash_list, &stainfo_free_list);
449 		}
450 	}
451 
452 	spin_unlock_bh(&pstapriv->sta_hash_lock);
453 
454 	list_for_each_safe(plist, tmp, &stainfo_free_list) {
455 		psta = list_entry(plist, struct sta_info, hash_list);
456 		rtw_free_stainfo(padapter, psta);
457 	}
458 }
459 
460 /* any station allocated can be searched by hash list */
rtw_get_stainfo(struct sta_priv * pstapriv,u8 * hwaddr)461 struct sta_info *rtw_get_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
462 {
463 	struct list_head	*plist, *phead;
464 	struct sta_info *psta = NULL;
465 	u32 index;
466 	u8 *addr;
467 	u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
468 
469 	if (!hwaddr)
470 		return NULL;
471 
472 	if (is_multicast_ether_addr(hwaddr))
473 		addr = bc_addr;
474 	else
475 		addr = hwaddr;
476 
477 	index = wifi_mac_hash(addr);
478 
479 	spin_lock_bh(&pstapriv->sta_hash_lock);
480 
481 	phead = &(pstapriv->sta_hash[index]);
482 	list_for_each(plist, phead) {
483 		psta = list_entry(plist, struct sta_info, hash_list);
484 
485 		if ((!memcmp(psta->hwaddr, addr, ETH_ALEN)))
486 		 /*  if found the matched address */
487 			break;
488 
489 		psta = NULL;
490 	}
491 
492 	spin_unlock_bh(&pstapriv->sta_hash_lock);
493 	return psta;
494 }
495 
rtw_init_bcmc_stainfo(struct adapter * padapter)496 u32 rtw_init_bcmc_stainfo(struct adapter *padapter)
497 {
498 	struct sta_info *psta;
499 	NDIS_802_11_MAC_ADDRESS	bcast_addr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
500 
501 	struct	sta_priv *pstapriv = &padapter->stapriv;
502 	/* struct __queue	*pstapending = &padapter->xmitpriv.bm_pending; */
503 
504 	psta = rtw_alloc_stainfo(pstapriv, bcast_addr);
505 
506 	if (!psta)
507 		return _FAIL;
508 
509 	/*  default broadcast & multicast use macid 1 */
510 	psta->mac_id = 1;
511 
512 	return _SUCCESS;
513 }
514 
rtw_get_bcmc_stainfo(struct adapter * padapter)515 struct sta_info *rtw_get_bcmc_stainfo(struct adapter *padapter)
516 {
517 	struct sta_priv *pstapriv = &padapter->stapriv;
518 	u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
519 
520 	return rtw_get_stainfo(pstapriv, bc_addr);
521 }
522 
rtw_access_ctrl(struct adapter * padapter,u8 * mac_addr)523 u8 rtw_access_ctrl(struct adapter *padapter, u8 *mac_addr)
524 {
525 	bool res = true;
526 	struct list_head	*plist, *phead;
527 	struct rtw_wlan_acl_node *paclnode;
528 	bool match = false;
529 	struct sta_priv *pstapriv = &padapter->stapriv;
530 	struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
531 	struct __queue	*pacl_node_q = &pacl_list->acl_node_q;
532 
533 	spin_lock_bh(&(pacl_node_q->lock));
534 	phead = get_list_head(pacl_node_q);
535 	list_for_each(plist, phead) {
536 		paclnode = list_entry(plist, struct rtw_wlan_acl_node, list);
537 
538 		if (!memcmp(paclnode->addr, mac_addr, ETH_ALEN))
539 			if (paclnode->valid == true) {
540 				match = true;
541 				break;
542 			}
543 	}
544 	spin_unlock_bh(&(pacl_node_q->lock));
545 
546 	if (pacl_list->mode == 1) /* accept unless in deny list */
547 		res = !match;
548 
549 	else if (pacl_list->mode == 2)/* deny unless in accept list */
550 		res = match;
551 	else
552 		res = true;
553 
554 	return res;
555 }
556