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_
9 #include <osdep_service.h>
10 #include <drv_types.h>
11 #include <recv_osdep.h>
12 #include <xmit_osdep.h>
13 #include <mlme_osdep.h>
15 #include <linux/vmalloc.h>
17 static void _rtw_init_stainfo(struct sta_info *psta)
19 memset((u8 *)psta, 0, sizeof(struct sta_info));
21 spin_lock_init(&psta->lock);
22 INIT_LIST_HEAD(&psta->list);
23 INIT_LIST_HEAD(&psta->hash_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 #ifdef CONFIG_88EU_AP_MODE
32 INIT_LIST_HEAD(&psta->asoc_list);
34 INIT_LIST_HEAD(&psta->auth_list);
42 psta->bpairwise_key_installed = false;
45 psta->no_short_slot_time_set = 0;
46 psta->no_short_preamble_set = 0;
47 psta->no_ht_gf_set = 0;
49 psta->ht_20mhz_set = 0;
51 psta->under_exist_checking = 0;
53 psta->keep_alive_trycnt = 0;
55 #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 = vzalloc(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,
87 get_list_head(&pstapriv->free_sta_queue));
92 #ifdef CONFIG_88EU_AP_MODE
94 pstapriv->sta_dz_bitmap = 0;
95 pstapriv->tim_bitmap = 0;
97 INIT_LIST_HEAD(&pstapriv->asoc_list);
98 INIT_LIST_HEAD(&pstapriv->auth_list);
99 spin_lock_init(&pstapriv->asoc_list_lock);
100 spin_lock_init(&pstapriv->auth_list_lock);
101 pstapriv->asoc_list_cnt = 0;
102 pstapriv->auth_list_cnt = 0;
104 pstapriv->auth_to = 3; /* 3*2 = 6 sec */
105 pstapriv->assoc_to = 3;
106 pstapriv->expire_to = 3; /* 3*2 = 6 sec */
107 pstapriv->max_num_sta = NUM_STA;
113 inline int rtw_stainfo_offset(struct sta_priv *stapriv, struct sta_info *sta)
115 int offset = (((u8 *)sta) - stapriv->pstainfo_buf) / sizeof(struct sta_info);
117 if (!stainfo_offset_valid(offset))
118 DBG_88E("%s invalid offset(%d), out of range!!!", __func__, offset);
123 inline struct sta_info *rtw_get_stainfo_by_offset(struct sta_priv *stapriv, int offset)
125 if (!stainfo_offset_valid(offset))
126 DBG_88E("%s invalid offset(%d), out of range!!!", __func__, offset);
128 return (struct sta_info *)(stapriv->pstainfo_buf + offset * sizeof(struct sta_info));
131 u32 _rtw_free_sta_priv(struct sta_priv *pstapriv)
133 struct list_head *phead, *plist;
134 struct sta_info *psta = NULL;
135 struct recv_reorder_ctrl *preorder_ctrl;
141 /* delete all reordering_ctrl_timer */
142 spin_lock_bh(&pstapriv->sta_hash_lock);
143 for (index = 0; index < NUM_STA; index++) {
144 phead = &pstapriv->sta_hash[index];
147 while (phead != plist) {
150 psta = container_of(plist, struct sta_info, hash_list);
153 for (i = 0; i < 16; i++) {
154 preorder_ctrl = &psta->recvreorder_ctrl[i];
155 del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
159 spin_unlock_bh(&pstapriv->sta_hash_lock);
161 vfree(pstapriv->pallocated_stainfo_buf);
166 struct sta_info *rtw_alloc_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
169 struct list_head *phash_list;
170 struct sta_info *psta;
171 struct __queue *pfree_sta_queue;
172 struct recv_reorder_ctrl *preorder_ctrl;
174 u16 wRxSeqInitialValue = 0xffff;
176 pfree_sta_queue = &pstapriv->free_sta_queue;
178 spin_lock_bh(&pfree_sta_queue->lock);
179 psta = list_first_entry_or_null(&pfree_sta_queue->queue,
180 struct sta_info, list);
182 spin_unlock_bh(&pfree_sta_queue->lock);
186 list_del_init(&psta->list);
187 spin_unlock_bh(&pfree_sta_queue->lock);
188 _rtw_init_stainfo(psta);
189 memcpy(psta->hwaddr, hwaddr, ETH_ALEN);
190 index = wifi_mac_hash(hwaddr);
191 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_info_,
192 ("%s: index=%x", __func__, index));
193 if (index >= NUM_STA) {
194 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_,
195 ("ERROR => %s: index >= NUM_STA", __func__));
198 phash_list = &pstapriv->sta_hash[index];
200 spin_lock_bh(&pstapriv->sta_hash_lock);
201 list_add_tail(&psta->hash_list, phash_list);
202 pstapriv->asoc_sta_count++;
203 spin_unlock_bh(&pstapriv->sta_hash_lock);
205 /* Commented by Albert 2009/08/13
206 * For the SMC router, the sequence number of first packet of
207 * WPS handshake will be 0. In this case, this packet will be
208 * dropped by recv_decache function if we use the 0x00 as the
209 * default value for tid_rxseq variable. So, we initialize the
210 * tid_rxseq variable as the 0xffff.
213 for (i = 0; i < 16; i++)
214 memcpy(&psta->sta_recvpriv.rxcache.tid_rxseq[i],
215 &wRxSeqInitialValue, 2);
217 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_info_,
218 ("alloc number_%d stainfo with hwaddr = %pM\n",
219 pstapriv->asoc_sta_count, hwaddr));
221 init_addba_retry_timer(pstapriv->padapter, psta);
223 /* for A-MPDU Rx reordering buffer control */
224 for (i = 0; i < 16; i++) {
225 preorder_ctrl = &psta->recvreorder_ctrl[i];
227 preorder_ctrl->padapter = pstapriv->padapter;
229 preorder_ctrl->enable = false;
231 preorder_ctrl->indicate_seq = 0xffff;
232 preorder_ctrl->wend_b = 0xffff;
233 preorder_ctrl->wsize_b = 64;/* 64; */
235 _rtw_init_queue(&preorder_ctrl->pending_recvframe_queue);
237 rtw_init_recv_timer(preorder_ctrl);
241 psta->rssi_stat.UndecoratedSmoothedPWDB = -1;
242 psta->rssi_stat.UndecoratedSmoothedCCK = -1;
244 /* init for the sequence number of received management frame */
245 psta->RxMgmtFrameSeqNum = 0xffff;
250 /* using pstapriv->sta_hash_lock to protect */
251 u32 rtw_free_stainfo(struct adapter *padapter, struct sta_info *psta)
254 struct __queue *pfree_sta_queue;
255 struct recv_reorder_ctrl *preorder_ctrl;
256 struct sta_xmit_priv *pstaxmitpriv;
257 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
258 struct sta_priv *pstapriv = &padapter->stapriv;
263 pfree_sta_queue = &pstapriv->free_sta_queue;
265 pstaxmitpriv = &psta->sta_xmitpriv;
267 spin_lock_bh(&pxmitpriv->lock);
269 rtw_free_xmitframe_queue(pxmitpriv, &psta->sleep_q);
270 psta->sleepq_len = 0;
272 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vo_q.sta_pending);
274 list_del_init(&pstaxmitpriv->vo_q.tx_pending);
276 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vi_q.sta_pending);
278 list_del_init(&pstaxmitpriv->vi_q.tx_pending);
280 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->bk_q.sta_pending);
282 list_del_init(&pstaxmitpriv->bk_q.tx_pending);
284 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->be_q.sta_pending);
286 list_del_init(&pstaxmitpriv->be_q.tx_pending);
288 spin_unlock_bh(&pxmitpriv->lock);
290 list_del_init(&psta->hash_list);
291 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_,
292 ("\n free number_%d stainfo with hwaddr=0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
293 pstapriv->asoc_sta_count, psta->hwaddr[0], psta->hwaddr[1],
294 psta->hwaddr[2], psta->hwaddr[3], psta->hwaddr[4],
296 pstapriv->asoc_sta_count--;
298 /* re-init sta_info; 20061114 */
299 _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
300 _rtw_init_sta_recv_priv(&psta->sta_recvpriv);
302 del_timer_sync(&psta->addba_retry_timer);
304 /* for A-MPDU Rx reordering buffer control, cancel
305 * reordering_ctrl_timer
307 for (i = 0; i < 16; i++) {
308 struct list_head *phead, *plist;
309 struct recv_frame *prframe;
310 struct __queue *ppending_recvframe_queue;
311 struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
313 preorder_ctrl = &psta->recvreorder_ctrl[i];
315 del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
317 ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
319 spin_lock_bh(&ppending_recvframe_queue->lock);
321 phead = get_list_head(ppending_recvframe_queue);
324 while (!list_empty(phead)) {
325 prframe = container_of(plist, struct recv_frame, list);
329 list_del_init(&prframe->list);
331 rtw_free_recvframe(prframe, pfree_recv_queue);
334 spin_unlock_bh(&ppending_recvframe_queue->lock);
337 if (!(psta->state & WIFI_AP_STATE))
338 rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, false);
340 #ifdef CONFIG_88EU_AP_MODE
342 spin_lock_bh(&pstapriv->auth_list_lock);
343 if (!list_empty(&psta->auth_list)) {
344 list_del_init(&psta->auth_list);
345 pstapriv->auth_list_cnt--;
347 spin_unlock_bh(&pstapriv->auth_list_lock);
351 psta->sleepq_ac_len = 0;
354 psta->max_sp_len = 0;
359 psta->has_legacy_ac = 0;
361 pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
362 pstapriv->tim_bitmap &= ~BIT(psta->aid);
364 if ((psta->aid > 0) && (pstapriv->sta_aid[psta->aid - 1] == psta)) {
365 pstapriv->sta_aid[psta->aid - 1] = NULL;
369 psta->under_exist_checking = 0;
371 #endif /* CONFIG_88EU_AP_MODE */
373 spin_lock_bh(&pfree_sta_queue->lock);
374 list_add_tail(&psta->list, get_list_head(pfree_sta_queue));
375 spin_unlock_bh(&pfree_sta_queue->lock);
382 /* free all stainfo which in sta_hash[all] */
383 void rtw_free_all_stainfo(struct adapter *padapter)
385 struct list_head *plist, *phead;
387 struct sta_info *psta = NULL;
388 struct sta_priv *pstapriv = &padapter->stapriv;
389 struct sta_info *pbcmc_stainfo = rtw_get_bcmc_stainfo(padapter);
391 if (pstapriv->asoc_sta_count == 1)
394 spin_lock_bh(&pstapriv->sta_hash_lock);
396 for (index = 0; index < NUM_STA; index++) {
397 phead = &pstapriv->sta_hash[index];
400 while (phead != plist) {
401 psta = container_of(plist, struct sta_info, hash_list);
405 if (pbcmc_stainfo != psta)
406 rtw_free_stainfo(padapter, psta);
409 spin_unlock_bh(&pstapriv->sta_hash_lock);
412 /* any station allocated can be searched by hash list */
413 struct sta_info *rtw_get_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
415 struct list_head *plist, *phead;
416 struct sta_info *psta = NULL;
419 u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
424 if (is_multicast_ether_addr(hwaddr))
429 index = wifi_mac_hash(addr);
431 spin_lock_bh(&pstapriv->sta_hash_lock);
433 phead = &pstapriv->sta_hash[index];
436 while (phead != plist) {
437 psta = container_of(plist, struct sta_info, hash_list);
439 if (!memcmp(psta->hwaddr, addr, ETH_ALEN)) {
440 /* if found the matched address */
447 spin_unlock_bh(&pstapriv->sta_hash_lock);
451 u32 rtw_init_bcmc_stainfo(struct adapter *padapter)
453 struct sta_info *psta;
454 u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
455 struct sta_priv *pstapriv = &padapter->stapriv;
457 psta = rtw_alloc_stainfo(pstapriv, bc_addr);
460 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_,
461 ("rtw_alloc_stainfo fail"));
465 /* default broadcast & multicast use macid 1 */
471 struct sta_info *rtw_get_bcmc_stainfo(struct adapter *padapter)
473 struct sta_priv *pstapriv = &padapter->stapriv;
474 u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
476 return rtw_get_stainfo(pstapriv, bc_addr);
479 bool rtw_access_ctrl(struct adapter *padapter, u8 *mac_addr)
482 #ifdef CONFIG_88EU_AP_MODE
483 struct list_head *plist, *phead;
484 struct rtw_wlan_acl_node *paclnode;
486 struct sta_priv *pstapriv = &padapter->stapriv;
487 struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
488 struct __queue *pacl_node_q = &pacl_list->acl_node_q;
490 spin_lock_bh(&pacl_node_q->lock);
491 phead = get_list_head(pacl_node_q);
493 while (phead != plist) {
494 paclnode = container_of(plist, struct rtw_wlan_acl_node, list);
497 if (!memcmp(paclnode->addr, mac_addr, ETH_ALEN)) {
498 if (paclnode->valid) {
504 spin_unlock_bh(&pacl_node_q->lock);
506 if (pacl_list->mode == 1)/* accept unless in deny list */
508 else if (pacl_list->mode == 2)/* deny unless in accept list */