1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2011 Realtek Corporation. */
4 #define _RTW_STA_MGT_C_
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/mlme_osdep.h"
11 #include "../include/sta_info.h"
13 static void _rtw_init_stainfo(struct sta_info *psta)
16 memset((u8 *)psta, 0, sizeof (struct sta_info));
18 spin_lock_init(&psta->lock);
19 INIT_LIST_HEAD(&psta->list);
20 INIT_LIST_HEAD(&psta->hash_list);
21 _rtw_init_queue(&psta->sleep_q);
24 _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
25 _rtw_init_sta_recv_priv(&psta->sta_recvpriv);
27 #ifdef CONFIG_88EU_AP_MODE
29 INIT_LIST_HEAD(&psta->asoc_list);
31 INIT_LIST_HEAD(&psta->auth_list);
39 psta->bpairwise_key_installed = false;
41 #ifdef CONFIG_88EU_AP_MODE
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;
50 psta->under_exist_checking = 0;
52 psta->keep_alive_trycnt = 0;
54 #endif /* CONFIG_88EU_AP_MODE */
58 u32 _rtw_init_sta_priv(struct sta_priv *pstapriv)
60 struct sta_info *psta;
63 pstapriv->pallocated_stainfo_buf = rtw_zvmalloc(sizeof(struct sta_info) * NUM_STA + 4);
65 if (!pstapriv->pallocated_stainfo_buf)
68 pstapriv->pstainfo_buf = pstapriv->pallocated_stainfo_buf + 4 -
69 ((size_t)(pstapriv->pallocated_stainfo_buf) & 3);
71 _rtw_init_queue(&pstapriv->free_sta_queue);
73 spin_lock_init(&pstapriv->sta_hash_lock);
75 pstapriv->asoc_sta_count = 0;
76 _rtw_init_queue(&pstapriv->sleep_q);
77 _rtw_init_queue(&pstapriv->wakeup_q);
79 psta = (struct sta_info *)(pstapriv->pstainfo_buf);
81 for (i = 0; i < NUM_STA; i++) {
82 _rtw_init_stainfo(psta);
84 INIT_LIST_HEAD(&pstapriv->sta_hash[i]);
86 list_add_tail(&psta->list, get_list_head(&pstapriv->free_sta_queue));
91 #ifdef CONFIG_88EU_AP_MODE
93 pstapriv->sta_dz_bitmap = 0;
94 pstapriv->tim_bitmap = 0;
96 INIT_LIST_HEAD(&pstapriv->asoc_list);
97 INIT_LIST_HEAD(&pstapriv->auth_list);
98 spin_lock_init(&pstapriv->asoc_list_lock);
99 spin_lock_init(&pstapriv->auth_list_lock);
100 pstapriv->asoc_list_cnt = 0;
101 pstapriv->auth_list_cnt = 0;
103 pstapriv->auth_to = 3; /* 3*2 = 6 sec */
104 pstapriv->assoc_to = 3;
105 pstapriv->expire_to = 3; /* 3*2 = 6 sec */
106 pstapriv->max_num_sta = NUM_STA;
112 inline int rtw_stainfo_offset(struct sta_priv *stapriv, struct sta_info *sta)
114 int offset = (((u8 *)sta) - stapriv->pstainfo_buf)/sizeof(struct sta_info);
116 if (!stainfo_offset_valid(offset))
117 DBG_88E("%s invalid offset(%d), out of range!!!", __func__, offset);
122 inline struct sta_info *rtw_get_stainfo_by_offset(struct sta_priv *stapriv, int offset)
124 if (!stainfo_offset_valid(offset))
125 DBG_88E("%s invalid offset(%d), out of range!!!", __func__, offset);
127 return (struct sta_info *)(stapriv->pstainfo_buf + offset * sizeof(struct sta_info));
130 static void rtw_mfree_sta_priv_lock(struct sta_priv *pstapriv)
132 #ifdef CONFIG_88EU_AP_MODE
133 struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
137 u32 _rtw_free_sta_priv(struct sta_priv *pstapriv)
139 struct list_head *phead, *plist;
140 struct sta_info *psta = NULL;
141 struct recv_reorder_ctrl *preorder_ctrl;
145 /* delete all reordering_ctrl_timer */
146 spin_lock_bh(&pstapriv->sta_hash_lock);
147 for (index = 0; index < NUM_STA; index++) {
148 phead = &pstapriv->sta_hash[index];
151 while (phead != plist) {
153 psta = container_of(plist, struct sta_info, hash_list);
156 for (i = 0; i < 16; i++) {
157 preorder_ctrl = &psta->recvreorder_ctrl[i];
158 _cancel_timer_ex(&preorder_ctrl->reordering_ctrl_timer);
162 spin_unlock_bh(&pstapriv->sta_hash_lock);
163 /*===============================*/
165 rtw_mfree_sta_priv_lock(pstapriv);
167 if (pstapriv->pallocated_stainfo_buf)
168 vfree(pstapriv->pallocated_stainfo_buf);
174 struct sta_info *rtw_alloc_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
177 struct list_head *phash_list;
178 struct sta_info *psta;
179 struct __queue *pfree_sta_queue;
180 struct recv_reorder_ctrl *preorder_ctrl;
182 u16 wRxSeqInitialValue = 0xffff;
184 pfree_sta_queue = &pstapriv->free_sta_queue;
186 spin_lock_bh(&pfree_sta_queue->lock);
188 if (list_empty(&pfree_sta_queue->queue)) {
189 spin_unlock_bh(&pfree_sta_queue->lock);
192 psta = container_of((&pfree_sta_queue->queue)->next, struct sta_info, list);
193 list_del_init(&psta->list);
194 spin_unlock_bh(&pfree_sta_queue->lock);
195 _rtw_init_stainfo(psta);
196 memcpy(psta->hwaddr, hwaddr, ETH_ALEN);
197 index = wifi_mac_hash(hwaddr);
198 if (index >= NUM_STA) {
202 phash_list = &pstapriv->sta_hash[index];
204 spin_lock_bh(&pstapriv->sta_hash_lock);
206 list_add_tail(&psta->hash_list, phash_list);
208 pstapriv->asoc_sta_count++ ;
210 spin_unlock_bh(&pstapriv->sta_hash_lock);
212 /* Commented by Albert 2009/08/13 */
213 /* For the SMC router, the sequence number of first packet of WPS handshake will be 0. */
214 /* 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. */
215 /* So, we initialize the tid_rxseq variable as the 0xffff. */
217 for (i = 0; i < 16; i++)
218 memcpy(&psta->sta_recvpriv.rxcache.tid_rxseq[i], &wRxSeqInitialValue, 2);
220 init_addba_retry_timer(pstapriv->padapter, psta);
222 /* for A-MPDU Rx reordering buffer control */
223 for (i = 0; i < 16; i++) {
224 preorder_ctrl = &psta->recvreorder_ctrl[i];
226 preorder_ctrl->padapter = pstapriv->padapter;
228 preorder_ctrl->enable = false;
230 preorder_ctrl->indicate_seq = 0xffff;
231 preorder_ctrl->wend_b = 0xffff;
232 preorder_ctrl->wsize_b = 64;/* 64; */
234 _rtw_init_queue(&preorder_ctrl->pending_recvframe_queue);
236 rtw_init_recv_timer(preorder_ctrl);
240 psta->rssi_stat.UndecoratedSmoothedPWDB = (-1);
241 psta->rssi_stat.UndecoratedSmoothedCCK = (-1);
243 /* init for the sequence number of received management frame */
244 psta->RxMgmtFrameSeqNum = 0xffff;
252 /* using pstapriv->sta_hash_lock to protect */
253 u32 rtw_free_stainfo(struct adapter *padapter, struct sta_info *psta)
256 struct __queue *pfree_sta_queue;
257 struct recv_reorder_ctrl *preorder_ctrl;
258 struct sta_xmit_priv *pstaxmitpriv;
259 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
260 struct sta_priv *pstapriv = &padapter->stapriv;
265 pfree_sta_queue = &pstapriv->free_sta_queue;
267 pstaxmitpriv = &psta->sta_xmitpriv;
269 spin_lock_bh(&pxmitpriv->lock);
271 rtw_free_xmitframe_queue(pxmitpriv, &psta->sleep_q);
272 psta->sleepq_len = 0;
274 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vo_q.sta_pending);
276 list_del_init(&pstaxmitpriv->vo_q.tx_pending);
278 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vi_q.sta_pending);
280 list_del_init(&pstaxmitpriv->vi_q.tx_pending);
282 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->bk_q.sta_pending);
284 list_del_init(&pstaxmitpriv->bk_q.tx_pending);
286 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->be_q.sta_pending);
288 list_del_init(&pstaxmitpriv->be_q.tx_pending);
290 spin_unlock_bh(&pxmitpriv->lock);
292 list_del_init(&psta->hash_list);
293 pstapriv->asoc_sta_count--;
295 /* re-init sta_info; 20061114 */
296 _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
297 _rtw_init_sta_recv_priv(&psta->sta_recvpriv);
299 _cancel_timer_ex(&psta->addba_retry_timer);
301 /* for A-MPDU Rx reordering buffer control, cancel reordering_ctrl_timer */
302 for (i = 0; i < 16 ; i++) {
303 struct list_head *phead, *plist;
304 struct recv_frame *prframe;
305 struct __queue *ppending_recvframe_queue;
306 struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
308 preorder_ctrl = &psta->recvreorder_ctrl[i];
310 _cancel_timer_ex(&preorder_ctrl->reordering_ctrl_timer);
312 ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
314 spin_lock_bh(&ppending_recvframe_queue->lock);
316 phead = get_list_head(ppending_recvframe_queue);
319 while (!list_empty(phead)) {
320 prframe = container_of(plist, struct recv_frame, list);
324 list_del_init(&prframe->list);
326 rtw_free_recvframe(prframe, pfree_recv_queue);
329 spin_unlock_bh(&ppending_recvframe_queue->lock);
332 if (!(psta->state & WIFI_AP_STATE))
333 rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, false);
335 #ifdef CONFIG_88EU_AP_MODE
337 spin_lock_bh(&pstapriv->auth_list_lock);
338 if (!list_empty(&psta->auth_list)) {
339 list_del_init(&psta->auth_list);
340 pstapriv->auth_list_cnt--;
342 spin_unlock_bh(&pstapriv->auth_list_lock);
346 psta->sleepq_ac_len = 0;
349 psta->max_sp_len = 0;
354 psta->has_legacy_ac = 0;
356 pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
357 pstapriv->tim_bitmap &= ~BIT(psta->aid);
359 if ((psta->aid > 0) && (pstapriv->sta_aid[psta->aid - 1] == psta)) {
360 pstapriv->sta_aid[psta->aid - 1] = NULL;
364 psta->under_exist_checking = 0;
366 #endif /* CONFIG_88EU_AP_MODE */
368 spin_lock_bh(&pfree_sta_queue->lock);
369 list_add_tail(&psta->list, get_list_head(pfree_sta_queue));
370 spin_unlock_bh(&pfree_sta_queue->lock);
377 /* free all stainfo which in sta_hash[all] */
378 void rtw_free_all_stainfo(struct adapter *padapter)
380 struct list_head *plist, *phead;
382 struct sta_info *psta = NULL;
383 struct sta_priv *pstapriv = &padapter->stapriv;
384 struct sta_info *pbcmc_stainfo = rtw_get_bcmc_stainfo(padapter);
386 if (pstapriv->asoc_sta_count == 1)
389 spin_lock_bh(&pstapriv->sta_hash_lock);
391 for (index = 0; index < NUM_STA; index++) {
392 phead = &pstapriv->sta_hash[index];
395 while (phead != plist) {
396 psta = container_of(plist, struct sta_info, hash_list);
400 if (pbcmc_stainfo != psta)
401 rtw_free_stainfo(padapter, psta);
404 spin_unlock_bh(&pstapriv->sta_hash_lock);
407 /* any station allocated can be searched by hash list */
408 struct sta_info *rtw_get_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
410 struct list_head *plist, *phead;
411 struct sta_info *psta = NULL;
414 u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
419 if (IS_MCAST(hwaddr))
424 index = wifi_mac_hash(addr);
426 spin_lock_bh(&pstapriv->sta_hash_lock);
428 phead = &pstapriv->sta_hash[index];
431 while (phead != plist) {
432 psta = container_of(plist, struct sta_info, hash_list);
434 if ((!memcmp(psta->hwaddr, addr, ETH_ALEN))) {
435 /* if found the matched address */
442 spin_unlock_bh(&pstapriv->sta_hash_lock);
447 u32 rtw_init_bcmc_stainfo(struct adapter *padapter)
449 struct sta_info *psta;
451 unsigned char bcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
452 struct sta_priv *pstapriv = &padapter->stapriv;
454 psta = rtw_alloc_stainfo(pstapriv, bcast_addr);
461 /* default broadcast & multicast use macid 1 */
469 struct sta_info *rtw_get_bcmc_stainfo(struct adapter *padapter)
471 struct sta_info *psta;
472 struct sta_priv *pstapriv = &padapter->stapriv;
473 u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
475 psta = rtw_get_stainfo(pstapriv, bc_addr);
480 u8 rtw_access_ctrl(struct adapter *padapter, u8 *mac_addr)
483 #ifdef CONFIG_88EU_AP_MODE
484 struct list_head *plist, *phead;
485 struct rtw_wlan_acl_node *paclnode;
487 struct sta_priv *pstapriv = &padapter->stapriv;
488 struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
489 struct __queue *pacl_node_q = &pacl_list->acl_node_q;
491 spin_lock_bh(&pacl_node_q->lock);
492 phead = get_list_head(pacl_node_q);
494 while (phead != plist) {
495 paclnode = container_of(plist, struct rtw_wlan_acl_node, list);
498 if (!memcmp(paclnode->addr, mac_addr, ETH_ALEN)) {
499 if (paclnode->valid) {
505 spin_unlock_bh(&pacl_node_q->lock);
507 if (pacl_list->mode == 1)/* accept unless in deny list */
508 res = (match) ? false : true;
509 else if (pacl_list->mode == 2)/* deny unless in accept list */
510 res = (match) ? true : false;