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