xref: /linux/drivers/staging/rtl8723bs/core/rtw_sta_mgt.c (revision a5f22b9b139762685810aa5a41fd0181488aea13)
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 					timer_delete_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 	}
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 	timer_setup(&psta->addba_retry_timer, addba_timer_hdl, 0);
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 		/* init recv timer */
251 		timer_setup(&preorder_ctrl->reordering_ctrl_timer,
252 				rtw_reordering_ctrl_timeout_handler, 0);
253 	}
254 
255 	/* init for DM */
256 	psta->rssi_stat.UndecoratedSmoothedPWDB = (-1);
257 	psta->rssi_stat.UndecoratedSmoothedCCK = (-1);
258 
259 	/* init for the sequence number of received management frame */
260 	psta->RxMgmtFrameSeqNum = 0xffff;
261 	spin_unlock_bh(&(pstapriv->sta_hash_lock));
262 	/* alloc mac id for non-bc/mc station, */
263 	rtw_alloc_macid(pstapriv->padapter, psta);
264 
265 exit:
266 
267 	return psta;
268 }
269 
rtw_free_stainfo(struct adapter * padapter,struct sta_info * psta)270 u32 rtw_free_stainfo(struct adapter *padapter, struct sta_info *psta)
271 {
272 	int i;
273 	struct __queue *pfree_sta_queue;
274 	struct recv_reorder_ctrl *preorder_ctrl;
275 	struct	sta_xmit_priv *pstaxmitpriv;
276 	struct	xmit_priv *pxmitpriv = &padapter->xmitpriv;
277 	struct	sta_priv *pstapriv = &padapter->stapriv;
278 	struct hw_xmit *phwxmit;
279 
280 	if (!psta)
281 		goto exit;
282 
283 	spin_lock_bh(&psta->lock);
284 	psta->state &= ~_FW_LINKED;
285 	spin_unlock_bh(&psta->lock);
286 
287 	pfree_sta_queue = &pstapriv->free_sta_queue;
288 
289 	pstaxmitpriv = &psta->sta_xmitpriv;
290 
291 	/* list_del_init(&psta->sleep_list); */
292 
293 	/* list_del_init(&psta->wakeup_list); */
294 
295 	spin_lock_bh(&pxmitpriv->lock);
296 
297 	rtw_free_xmitframe_queue(pxmitpriv, &psta->sleep_q);
298 	psta->sleepq_len = 0;
299 
300 	/* vo */
301 	/* spin_lock_bh(&(pxmitpriv->vo_pending.lock)); */
302 	rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vo_q.sta_pending);
303 	list_del_init(&(pstaxmitpriv->vo_q.tx_pending));
304 	phwxmit = pxmitpriv->hwxmits;
305 	phwxmit->accnt -= pstaxmitpriv->vo_q.qcnt;
306 	pstaxmitpriv->vo_q.qcnt = 0;
307 	/* spin_unlock_bh(&(pxmitpriv->vo_pending.lock)); */
308 
309 	/* vi */
310 	/* spin_lock_bh(&(pxmitpriv->vi_pending.lock)); */
311 	rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vi_q.sta_pending);
312 	list_del_init(&(pstaxmitpriv->vi_q.tx_pending));
313 	phwxmit = pxmitpriv->hwxmits + 1;
314 	phwxmit->accnt -= pstaxmitpriv->vi_q.qcnt;
315 	pstaxmitpriv->vi_q.qcnt = 0;
316 	/* spin_unlock_bh(&(pxmitpriv->vi_pending.lock)); */
317 
318 	/* be */
319 	/* spin_lock_bh(&(pxmitpriv->be_pending.lock)); */
320 	rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->be_q.sta_pending);
321 	list_del_init(&(pstaxmitpriv->be_q.tx_pending));
322 	phwxmit = pxmitpriv->hwxmits + 2;
323 	phwxmit->accnt -= pstaxmitpriv->be_q.qcnt;
324 	pstaxmitpriv->be_q.qcnt = 0;
325 	/* spin_unlock_bh(&(pxmitpriv->be_pending.lock)); */
326 
327 	/* bk */
328 	/* spin_lock_bh(&(pxmitpriv->bk_pending.lock)); */
329 	rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->bk_q.sta_pending);
330 	list_del_init(&(pstaxmitpriv->bk_q.tx_pending));
331 	phwxmit = pxmitpriv->hwxmits + 3;
332 	phwxmit->accnt -= pstaxmitpriv->bk_q.qcnt;
333 	pstaxmitpriv->bk_q.qcnt = 0;
334 	/* spin_unlock_bh(&(pxmitpriv->bk_pending.lock)); */
335 
336 	spin_unlock_bh(&pxmitpriv->lock);
337 
338 	spin_lock_bh(&pstapriv->sta_hash_lock);
339 	list_del_init(&psta->hash_list);
340 	pstapriv->asoc_sta_count--;
341 	spin_unlock_bh(&pstapriv->sta_hash_lock);
342 
343 	/*  re-init sta_info; 20061114 will be init in alloc_stainfo */
344 	/* _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv); */
345 	/* _rtw_init_sta_recv_priv(&psta->sta_recvpriv); */
346 
347 	timer_delete_sync(&psta->addba_retry_timer);
348 
349 	/* for A-MPDU Rx reordering buffer control, cancel reordering_ctrl_timer */
350 	for (i = 0; i < 16 ; i++) {
351 		struct list_head	*phead, *plist;
352 		union recv_frame *prframe;
353 		struct __queue *ppending_recvframe_queue;
354 		struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
355 
356 		preorder_ctrl = &psta->recvreorder_ctrl[i];
357 
358 		timer_delete_sync(&preorder_ctrl->reordering_ctrl_timer);
359 
360 		ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
361 
362 		spin_lock_bh(&ppending_recvframe_queue->lock);
363 
364 		phead =		get_list_head(ppending_recvframe_queue);
365 		plist = get_next(phead);
366 
367 		while (!list_empty(phead)) {
368 			prframe = (union recv_frame *)plist;
369 
370 			plist = get_next(plist);
371 
372 			list_del_init(&(prframe->u.hdr.list));
373 
374 			rtw_free_recvframe(prframe, pfree_recv_queue);
375 		}
376 
377 		spin_unlock_bh(&ppending_recvframe_queue->lock);
378 	}
379 
380 	if (!(psta->state & WIFI_AP_STATE))
381 		rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, false);
382 
383 	/* release mac id for non-bc/mc station, */
384 	rtw_release_macid(pstapriv->padapter, psta);
385 	spin_lock_bh(&pstapriv->auth_list_lock);
386 	if (!list_empty(&psta->auth_list)) {
387 		list_del_init(&psta->auth_list);
388 		pstapriv->auth_list_cnt--;
389 	}
390 	spin_unlock_bh(&pstapriv->auth_list_lock);
391 
392 	psta->expire_to = 0;
393 	psta->sleepq_ac_len = 0;
394 	psta->qos_info = 0;
395 
396 	psta->max_sp_len = 0;
397 	psta->uapsd_bk = 0;
398 	psta->uapsd_be = 0;
399 	psta->uapsd_vi = 0;
400 	psta->uapsd_vo = 0;
401 
402 	psta->has_legacy_ac = 0;
403 
404 	pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
405 	pstapriv->tim_bitmap &= ~BIT(psta->aid);
406 
407 	if ((psta->aid > 0) && (pstapriv->sta_aid[psta->aid - 1] == psta)) {
408 		pstapriv->sta_aid[psta->aid - 1] = NULL;
409 		psta->aid = 0;
410 	}
411 
412 	psta->under_exist_checking = 0;
413 
414 	/* spin_lock_bh(&(pfree_sta_queue->lock)); */
415 	list_add_tail(&psta->list, get_list_head(pfree_sta_queue));
416 	/* spin_unlock_bh(&(pfree_sta_queue->lock)); */
417 
418 exit:
419 	return _SUCCESS;
420 }
421 
422 /*  free all stainfo which in sta_hash[all] */
rtw_free_all_stainfo(struct adapter * padapter)423 void rtw_free_all_stainfo(struct adapter *padapter)
424 {
425 	struct list_head *plist, *phead, *tmp;
426 	s32	index;
427 	struct sta_info *psta = NULL;
428 	struct	sta_priv *pstapriv = &padapter->stapriv;
429 	struct sta_info *pbcmc_stainfo = rtw_get_bcmc_stainfo(padapter);
430 	LIST_HEAD(stainfo_free_list);
431 
432 	if (pstapriv->asoc_sta_count == 1)
433 		return;
434 
435 	spin_lock_bh(&pstapriv->sta_hash_lock);
436 
437 	for (index = 0; index < NUM_STA; index++) {
438 		phead = &(pstapriv->sta_hash[index]);
439 		list_for_each_safe(plist, tmp, phead) {
440 			psta = list_entry(plist, struct sta_info, hash_list);
441 
442 			if (pbcmc_stainfo != psta)
443 				list_move(&psta->hash_list, &stainfo_free_list);
444 		}
445 	}
446 
447 	spin_unlock_bh(&pstapriv->sta_hash_lock);
448 
449 	list_for_each_safe(plist, tmp, &stainfo_free_list) {
450 		psta = list_entry(plist, struct sta_info, hash_list);
451 		rtw_free_stainfo(padapter, psta);
452 	}
453 }
454 
455 /* any station allocated can be searched by hash list */
rtw_get_stainfo(struct sta_priv * pstapriv,u8 * hwaddr)456 struct sta_info *rtw_get_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
457 {
458 	struct list_head	*plist, *phead;
459 	struct sta_info *psta = NULL;
460 	u32 index;
461 	u8 *addr;
462 	u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
463 
464 	if (!hwaddr)
465 		return NULL;
466 
467 	if (is_multicast_ether_addr(hwaddr))
468 		addr = bc_addr;
469 	else
470 		addr = hwaddr;
471 
472 	index = wifi_mac_hash(addr);
473 
474 	spin_lock_bh(&pstapriv->sta_hash_lock);
475 
476 	phead = &(pstapriv->sta_hash[index]);
477 	list_for_each(plist, phead) {
478 		psta = list_entry(plist, struct sta_info, hash_list);
479 
480 		if ((!memcmp(psta->hwaddr, addr, ETH_ALEN)))
481 		 /*  if found the matched address */
482 			break;
483 
484 		psta = NULL;
485 	}
486 
487 	spin_unlock_bh(&pstapriv->sta_hash_lock);
488 	return psta;
489 }
490 
rtw_init_bcmc_stainfo(struct adapter * padapter)491 u32 rtw_init_bcmc_stainfo(struct adapter *padapter)
492 {
493 	struct sta_info *psta;
494 	NDIS_802_11_MAC_ADDRESS	bcast_addr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
495 
496 	struct	sta_priv *pstapriv = &padapter->stapriv;
497 	/* struct __queue	*pstapending = &padapter->xmitpriv.bm_pending; */
498 
499 	psta = rtw_alloc_stainfo(pstapriv, bcast_addr);
500 
501 	if (!psta)
502 		return _FAIL;
503 
504 	/*  default broadcast & multicast use macid 1 */
505 	psta->mac_id = 1;
506 
507 	return _SUCCESS;
508 }
509 
rtw_get_bcmc_stainfo(struct adapter * padapter)510 struct sta_info *rtw_get_bcmc_stainfo(struct adapter *padapter)
511 {
512 	struct sta_priv *pstapriv = &padapter->stapriv;
513 	u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
514 
515 	return rtw_get_stainfo(pstapriv, bc_addr);
516 }
517 
rtw_access_ctrl(struct adapter * padapter,u8 * mac_addr)518 u8 rtw_access_ctrl(struct adapter *padapter, u8 *mac_addr)
519 {
520 	bool res = true;
521 	struct list_head	*plist, *phead;
522 	struct rtw_wlan_acl_node *paclnode;
523 	bool match = false;
524 	struct sta_priv *pstapriv = &padapter->stapriv;
525 	struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
526 	struct __queue	*pacl_node_q = &pacl_list->acl_node_q;
527 
528 	spin_lock_bh(&(pacl_node_q->lock));
529 	phead = get_list_head(pacl_node_q);
530 	list_for_each(plist, phead) {
531 		paclnode = list_entry(plist, struct rtw_wlan_acl_node, list);
532 
533 		if (!memcmp(paclnode->addr, mac_addr, ETH_ALEN))
534 			if (paclnode->valid == true) {
535 				match = true;
536 				break;
537 			}
538 	}
539 	spin_unlock_bh(&(pacl_node_q->lock));
540 
541 	if (pacl_list->mode == 1) /* accept unless in deny list */
542 		res = !match;
543 
544 	else if (pacl_list->mode == 2)/* deny unless in accept list */
545 		res = match;
546 	else
547 		res = true;
548 
549 	return res;
550 }
551