1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
4 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
6 ******************************************************************************/
7 #define _RTW_STA_MGT_C_
10 #include <rtw_debug.h>
12 void _rtw_init_stainfo(struct sta_info *psta);
13 void _rtw_init_stainfo(struct sta_info *psta)
15 memset((u8 *)psta, 0, sizeof(struct sta_info));
17 spin_lock_init(&psta->lock);
18 INIT_LIST_HEAD(&psta->list);
19 INIT_LIST_HEAD(&psta->hash_list);
20 /* INIT_LIST_HEAD(&psta->asoc_list); */
21 /* INIT_LIST_HEAD(&psta->sleep_list); */
22 /* INIT_LIST_HEAD(&psta->wakeup_list); */
24 _rtw_init_queue(&psta->sleep_q);
27 _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
28 _rtw_init_sta_recv_priv(&psta->sta_recvpriv);
30 INIT_LIST_HEAD(&psta->asoc_list);
32 INIT_LIST_HEAD(&psta->auth_list);
40 psta->bpairwise_key_installed = false;
43 psta->no_short_slot_time_set = 0;
44 psta->no_short_preamble_set = 0;
45 psta->no_ht_gf_set = 0;
47 psta->ht_20mhz_set = 0;
49 psta->under_exist_checking = 0;
51 psta->keep_alive_trycnt = 0;
54 u32 _rtw_init_sta_priv(struct sta_priv *pstapriv)
56 struct sta_info *psta;
59 pstapriv->pallocated_stainfo_buf = vzalloc(sizeof(struct sta_info) * NUM_STA+4);
61 if (!pstapriv->pallocated_stainfo_buf)
64 pstapriv->pstainfo_buf = pstapriv->pallocated_stainfo_buf + 4 -
65 ((SIZE_PTR)(pstapriv->pallocated_stainfo_buf) & 3);
67 _rtw_init_queue(&pstapriv->free_sta_queue);
69 spin_lock_init(&pstapriv->sta_hash_lock);
71 /* _rtw_init_queue(&pstapriv->asoc_q); */
72 pstapriv->asoc_sta_count = 0;
73 _rtw_init_queue(&pstapriv->sleep_q);
74 _rtw_init_queue(&pstapriv->wakeup_q);
76 psta = (struct sta_info *)(pstapriv->pstainfo_buf);
79 for (i = 0; i < NUM_STA; i++) {
80 _rtw_init_stainfo(psta);
82 INIT_LIST_HEAD(&(pstapriv->sta_hash[i]));
84 list_add_tail(&psta->list, get_list_head(&pstapriv->free_sta_queue));
89 pstapriv->sta_dz_bitmap = 0;
90 pstapriv->tim_bitmap = 0;
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;
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;
106 inline int rtw_stainfo_offset(struct sta_priv *stapriv, struct sta_info *sta)
108 int offset = (((u8 *)sta) - stapriv->pstainfo_buf)/sizeof(struct sta_info);
110 if (!stainfo_offset_valid(offset))
111 DBG_871X("%s invalid offset(%d), out of range!!!", __func__, offset);
116 inline struct sta_info *rtw_get_stainfo_by_offset(struct sta_priv *stapriv, int offset)
118 if (!stainfo_offset_valid(offset))
119 DBG_871X("%s invalid offset(%d), out of range!!!", __func__, offset);
121 return (struct sta_info *)(stapriv->pstainfo_buf + offset * sizeof(struct sta_info));
124 /* this function is used to free the memory of lock || sema for all stainfos */
125 void kfree_all_stainfo(struct sta_priv *pstapriv);
126 void kfree_all_stainfo(struct sta_priv *pstapriv)
128 struct list_head *plist, *phead;
129 struct sta_info *psta = NULL;
131 spin_lock_bh(&pstapriv->sta_hash_lock);
133 phead = get_list_head(&pstapriv->free_sta_queue);
134 plist = get_next(phead);
136 while (phead != plist) {
137 psta = LIST_CONTAINOR(plist, struct sta_info, list);
138 plist = get_next(plist);
141 spin_unlock_bh(&pstapriv->sta_hash_lock);
144 void kfree_sta_priv_lock(struct sta_priv *pstapriv);
145 void kfree_sta_priv_lock(struct sta_priv *pstapriv)
147 kfree_all_stainfo(pstapriv); /* be done before free sta_hash_lock */
150 u32 _rtw_free_sta_priv(struct sta_priv *pstapriv)
152 struct list_head *phead, *plist;
153 struct sta_info *psta = NULL;
154 struct recv_reorder_ctrl *preorder_ctrl;
159 /*delete all reordering_ctrl_timer */
160 spin_lock_bh(&pstapriv->sta_hash_lock);
161 for (index = 0; index < NUM_STA; index++) {
162 phead = &(pstapriv->sta_hash[index]);
163 plist = get_next(phead);
165 while (phead != plist) {
167 psta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
168 plist = get_next(plist);
170 for (i = 0; i < 16 ; i++) {
171 preorder_ctrl = &psta->recvreorder_ctrl[i];
172 del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
176 spin_unlock_bh(&pstapriv->sta_hash_lock);
177 /*===============================*/
179 kfree_sta_priv_lock(pstapriv);
181 if (pstapriv->pallocated_stainfo_buf)
182 vfree(pstapriv->pallocated_stainfo_buf);
188 /* struct sta_info *rtw_alloc_stainfo(_queue *pfree_sta_queue, unsigned char *hwaddr) */
189 struct sta_info *rtw_alloc_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
193 struct list_head *phash_list;
194 struct sta_info *psta;
195 struct __queue *pfree_sta_queue;
196 struct recv_reorder_ctrl *preorder_ctrl;
198 u16 wRxSeqInitialValue = 0xffff;
200 pfree_sta_queue = &pstapriv->free_sta_queue;
202 /* spin_lock_bh(&(pfree_sta_queue->lock)); */
203 spin_lock_bh(&(pstapriv->sta_hash_lock));
204 if (list_empty(&pfree_sta_queue->queue)) {
205 /* spin_unlock_bh(&(pfree_sta_queue->lock)); */
206 spin_unlock_bh(&(pstapriv->sta_hash_lock));
210 psta = LIST_CONTAINOR(get_next(&pfree_sta_queue->queue), struct sta_info, list);
212 list_del_init(&(psta->list));
214 /* spin_unlock_bh(&(pfree_sta_queue->lock)); */
218 _rtw_init_stainfo(psta);
220 psta->padapter = pstapriv->padapter;
222 memcpy(psta->hwaddr, hwaddr, ETH_ALEN);
224 index = wifi_mac_hash(hwaddr);
226 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_info_, ("rtw_alloc_stainfo: index = %x", index));
228 if (index >= NUM_STA) {
229 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_, ("ERROR => rtw_alloc_stainfo: index >= NUM_STA"));
230 spin_unlock_bh(&(pstapriv->sta_hash_lock));
234 phash_list = &(pstapriv->sta_hash[index]);
236 /* spin_lock_bh(&(pstapriv->sta_hash_lock)); */
238 list_add_tail(&psta->hash_list, phash_list);
240 pstapriv->asoc_sta_count++;
242 /* spin_unlock_bh(&(pstapriv->sta_hash_lock)); */
244 /* Commented by Albert 2009/08/13 */
245 /* For the SMC router, the sequence number of first packet of WPS handshake will be 0. */
246 /* 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. */
247 /* So, we initialize the tid_rxseq variable as the 0xffff. */
249 for (i = 0; i < 16; i++) {
250 memcpy(&psta->sta_recvpriv.rxcache.tid_rxseq[i], &wRxSeqInitialValue, 2);
254 _module_rtl871x_sta_mgt_c_,
256 "alloc number_%d stainfo with hwaddr = %x %x %x %x %x %x \n",
257 pstapriv->asoc_sta_count,
267 init_addba_retry_timer(pstapriv->padapter, psta);
269 /* for A-MPDU Rx reordering buffer control */
270 for (i = 0; i < 16 ; i++) {
271 preorder_ctrl = &psta->recvreorder_ctrl[i];
273 preorder_ctrl->padapter = pstapriv->padapter;
275 preorder_ctrl->enable = false;
277 preorder_ctrl->indicate_seq = 0xffff;
279 DBG_871X("DBG_RX_SEQ %s:%d IndicateSeq: %d\n", __func__, __LINE__,
280 preorder_ctrl->indicate_seq);
282 preorder_ctrl->wend_b = 0xffff;
283 /* preorder_ctrl->wsize_b = (NR_RECVBUFF-2); */
284 preorder_ctrl->wsize_b = 64;/* 64; */
286 _rtw_init_queue(&preorder_ctrl->pending_recvframe_queue);
288 rtw_init_recv_timer(preorder_ctrl);
293 psta->rssi_stat.UndecoratedSmoothedPWDB = (-1);
294 psta->rssi_stat.UndecoratedSmoothedCCK = (-1);
296 /* init for the sequence number of received management frame */
297 psta->RxMgmtFrameSeqNum = 0xffff;
298 spin_unlock_bh(&(pstapriv->sta_hash_lock));
299 /* alloc mac id for non-bc/mc station, */
300 rtw_alloc_macid(pstapriv->padapter, psta);
310 /* using pstapriv->sta_hash_lock to protect */
311 u32 rtw_free_stainfo(struct adapter *padapter, struct sta_info *psta)
314 struct __queue *pfree_sta_queue;
315 struct recv_reorder_ctrl *preorder_ctrl;
316 struct sta_xmit_priv *pstaxmitpriv;
317 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
318 struct sta_priv *pstapriv = &padapter->stapriv;
319 struct hw_xmit *phwxmit;
325 spin_lock_bh(&psta->lock);
326 psta->state &= ~_FW_LINKED;
327 spin_unlock_bh(&psta->lock);
329 pfree_sta_queue = &pstapriv->free_sta_queue;
332 pstaxmitpriv = &psta->sta_xmitpriv;
334 /* list_del_init(&psta->sleep_list); */
336 /* list_del_init(&psta->wakeup_list); */
338 spin_lock_bh(&pxmitpriv->lock);
340 rtw_free_xmitframe_queue(pxmitpriv, &psta->sleep_q);
341 psta->sleepq_len = 0;
344 /* spin_lock_bh(&(pxmitpriv->vo_pending.lock)); */
345 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vo_q.sta_pending);
346 list_del_init(&(pstaxmitpriv->vo_q.tx_pending));
347 phwxmit = pxmitpriv->hwxmits;
348 phwxmit->accnt -= pstaxmitpriv->vo_q.qcnt;
349 pstaxmitpriv->vo_q.qcnt = 0;
350 /* spin_unlock_bh(&(pxmitpriv->vo_pending.lock)); */
353 /* spin_lock_bh(&(pxmitpriv->vi_pending.lock)); */
354 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vi_q.sta_pending);
355 list_del_init(&(pstaxmitpriv->vi_q.tx_pending));
356 phwxmit = pxmitpriv->hwxmits+1;
357 phwxmit->accnt -= pstaxmitpriv->vi_q.qcnt;
358 pstaxmitpriv->vi_q.qcnt = 0;
359 /* spin_unlock_bh(&(pxmitpriv->vi_pending.lock)); */
362 /* spin_lock_bh(&(pxmitpriv->be_pending.lock)); */
363 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->be_q.sta_pending);
364 list_del_init(&(pstaxmitpriv->be_q.tx_pending));
365 phwxmit = pxmitpriv->hwxmits+2;
366 phwxmit->accnt -= pstaxmitpriv->be_q.qcnt;
367 pstaxmitpriv->be_q.qcnt = 0;
368 /* spin_unlock_bh(&(pxmitpriv->be_pending.lock)); */
371 /* spin_lock_bh(&(pxmitpriv->bk_pending.lock)); */
372 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->bk_q.sta_pending);
373 list_del_init(&(pstaxmitpriv->bk_q.tx_pending));
374 phwxmit = pxmitpriv->hwxmits+3;
375 phwxmit->accnt -= pstaxmitpriv->bk_q.qcnt;
376 pstaxmitpriv->bk_q.qcnt = 0;
377 /* spin_unlock_bh(&(pxmitpriv->bk_pending.lock)); */
379 spin_unlock_bh(&pxmitpriv->lock);
381 list_del_init(&psta->hash_list);
383 _module_rtl871x_sta_mgt_c_,
385 "\n free number_%d stainfo with hwaddr = 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x \n",
386 pstapriv->asoc_sta_count,
395 pstapriv->asoc_sta_count--;
398 /* re-init sta_info; 20061114 will be init in alloc_stainfo */
399 /* _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv); */
400 /* _rtw_init_sta_recv_priv(&psta->sta_recvpriv); */
402 del_timer_sync(&psta->addba_retry_timer);
404 /* for A-MPDU Rx reordering buffer control, cancel reordering_ctrl_timer */
405 for (i = 0; i < 16 ; i++) {
406 struct list_head *phead, *plist;
407 union recv_frame *prframe;
408 struct __queue *ppending_recvframe_queue;
409 struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
411 preorder_ctrl = &psta->recvreorder_ctrl[i];
413 del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
416 ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
418 spin_lock_bh(&ppending_recvframe_queue->lock);
420 phead = get_list_head(ppending_recvframe_queue);
421 plist = get_next(phead);
423 while (!list_empty(phead)) {
424 prframe = (union recv_frame *)plist;
426 plist = get_next(plist);
428 list_del_init(&(prframe->u.hdr.list));
430 rtw_free_recvframe(prframe, pfree_recv_queue);
433 spin_unlock_bh(&ppending_recvframe_queue->lock);
437 if (!(psta->state & WIFI_AP_STATE))
438 rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, false);
441 /* release mac id for non-bc/mc station, */
442 rtw_release_macid(pstapriv->padapter, psta);
445 spin_lock_bh(&pstapriv->asoc_list_lock);
446 list_del_init(&psta->asoc_list);
447 spin_unlock_bh(&pstapriv->asoc_list_lock);
449 spin_lock_bh(&pstapriv->auth_list_lock);
450 if (!list_empty(&psta->auth_list)) {
451 list_del_init(&psta->auth_list);
452 pstapriv->auth_list_cnt--;
454 spin_unlock_bh(&pstapriv->auth_list_lock);
457 psta->sleepq_ac_len = 0;
460 psta->max_sp_len = 0;
466 psta->has_legacy_ac = 0;
468 pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
469 pstapriv->tim_bitmap &= ~BIT(psta->aid);
471 if ((psta->aid > 0) && (pstapriv->sta_aid[psta->aid - 1] == psta)) {
472 pstapriv->sta_aid[psta->aid - 1] = NULL;
476 psta->under_exist_checking = 0;
478 /* spin_lock_bh(&(pfree_sta_queue->lock)); */
479 list_add_tail(&psta->list, get_list_head(pfree_sta_queue));
480 /* spin_unlock_bh(&(pfree_sta_queue->lock)); */
486 /* free all stainfo which in sta_hash[all] */
487 void rtw_free_all_stainfo(struct adapter *padapter)
489 struct list_head *plist, *phead;
491 struct sta_info *psta = NULL;
492 struct sta_priv *pstapriv = &padapter->stapriv;
493 struct sta_info *pbcmc_stainfo = rtw_get_bcmc_stainfo(padapter);
495 if (pstapriv->asoc_sta_count == 1)
498 spin_lock_bh(&pstapriv->sta_hash_lock);
500 for (index = 0; index < NUM_STA; index++) {
501 phead = &(pstapriv->sta_hash[index]);
502 plist = get_next(phead);
504 while (phead != plist) {
505 psta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
507 plist = get_next(plist);
509 if (pbcmc_stainfo != psta)
510 rtw_free_stainfo(padapter, psta);
515 spin_unlock_bh(&pstapriv->sta_hash_lock);
518 /* any station allocated can be searched by hash list */
519 struct sta_info *rtw_get_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
521 struct list_head *plist, *phead;
522 struct sta_info *psta = NULL;
525 u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
530 if (IS_MCAST(hwaddr))
535 index = wifi_mac_hash(addr);
537 spin_lock_bh(&pstapriv->sta_hash_lock);
539 phead = &(pstapriv->sta_hash[index]);
540 plist = get_next(phead);
543 while (phead != plist) {
545 psta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
547 if ((!memcmp(psta->hwaddr, addr, ETH_ALEN)))
548 /* if found the matched address */
552 plist = get_next(plist);
555 spin_unlock_bh(&pstapriv->sta_hash_lock);
559 u32 rtw_init_bcmc_stainfo(struct adapter *padapter)
562 struct sta_info *psta;
563 struct tx_servq *ptxservq;
565 NDIS_802_11_MAC_ADDRESS bcast_addr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
567 struct sta_priv *pstapriv = &padapter->stapriv;
568 /* struct __queue *pstapending = &padapter->xmitpriv.bm_pending; */
570 psta = rtw_alloc_stainfo(pstapriv, bcast_addr);
574 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_, ("rtw_alloc_stainfo fail"));
578 /* default broadcast & multicast use macid 1 */
581 ptxservq = &(psta->sta_xmitpriv.be_q);
587 struct sta_info *rtw_get_bcmc_stainfo(struct adapter *padapter)
589 struct sta_info *psta;
590 struct sta_priv *pstapriv = &padapter->stapriv;
591 u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
593 psta = rtw_get_stainfo(pstapriv, bc_addr);
597 u8 rtw_access_ctrl(struct adapter *padapter, u8 *mac_addr)
600 struct list_head *plist, *phead;
601 struct rtw_wlan_acl_node *paclnode;
603 struct sta_priv *pstapriv = &padapter->stapriv;
604 struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
605 struct __queue *pacl_node_q = &pacl_list->acl_node_q;
607 spin_lock_bh(&(pacl_node_q->lock));
608 phead = get_list_head(pacl_node_q);
609 plist = get_next(phead);
610 while (phead != plist) {
611 paclnode = LIST_CONTAINOR(plist, struct rtw_wlan_acl_node, list);
612 plist = get_next(plist);
614 if (!memcmp(paclnode->addr, mac_addr, ETH_ALEN))
615 if (paclnode->valid == true) {
621 spin_unlock_bh(&(pacl_node_q->lock));
624 if (pacl_list->mode == 1) /* accept unless in deny list */
627 else if (pacl_list->mode == 2)/* deny unless in accept list */