1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
4 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
6 ******************************************************************************/
10 void _rtw_init_stainfo(struct sta_info *psta);
11 void _rtw_init_stainfo(struct sta_info *psta)
13 memset((u8 *)psta, 0, sizeof(struct sta_info));
15 spin_lock_init(&psta->lock);
16 INIT_LIST_HEAD(&psta->list);
17 INIT_LIST_HEAD(&psta->hash_list);
18 /* INIT_LIST_HEAD(&psta->asoc_list); */
19 /* INIT_LIST_HEAD(&psta->sleep_list); */
20 /* INIT_LIST_HEAD(&psta->wakeup_list); */
22 INIT_LIST_HEAD(&psta->sleep_q.queue);
23 spin_lock_init(&psta->sleep_q.lock);
26 _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
27 _rtw_init_sta_recv_priv(&psta->sta_recvpriv);
29 INIT_LIST_HEAD(&psta->asoc_list);
31 INIT_LIST_HEAD(&psta->auth_list);
39 psta->bpairwise_key_installed = false;
42 psta->no_short_slot_time_set = 0;
43 psta->no_short_preamble_set = 0;
44 psta->no_ht_gf_set = 0;
46 psta->ht_20mhz_set = 0;
48 psta->under_exist_checking = 0;
50 psta->keep_alive_trycnt = 0;
53 u32 _rtw_init_sta_priv(struct sta_priv *pstapriv)
55 struct sta_info *psta;
58 pstapriv->pallocated_stainfo_buf = vzalloc(sizeof(struct sta_info) * NUM_STA+4);
60 if (!pstapriv->pallocated_stainfo_buf)
63 pstapriv->pstainfo_buf = pstapriv->pallocated_stainfo_buf + 4 -
64 ((SIZE_PTR)(pstapriv->pallocated_stainfo_buf) & 3);
66 INIT_LIST_HEAD(&pstapriv->free_sta_queue.queue);
67 spin_lock_init(&pstapriv->free_sta_queue.lock);
69 spin_lock_init(&pstapriv->sta_hash_lock);
71 /* _rtw_init_queue(&pstapriv->asoc_q); */
72 pstapriv->asoc_sta_count = 0;
73 INIT_LIST_HEAD(&pstapriv->sleep_q.queue);
74 spin_lock_init(&pstapriv->sleep_q.lock);
75 INIT_LIST_HEAD(&pstapriv->wakeup_q.queue);
76 spin_lock_init(&pstapriv->wakeup_q.lock);
78 psta = (struct sta_info *)(pstapriv->pstainfo_buf);
80 for (i = 0; i < NUM_STA; i++) {
81 _rtw_init_stainfo(psta);
83 INIT_LIST_HEAD(&(pstapriv->sta_hash[i]));
85 list_add_tail(&psta->list, get_list_head(&pstapriv->free_sta_queue));
90 pstapriv->sta_dz_bitmap = 0;
91 pstapriv->tim_bitmap = 0;
93 INIT_LIST_HEAD(&pstapriv->asoc_list);
94 INIT_LIST_HEAD(&pstapriv->auth_list);
95 spin_lock_init(&pstapriv->asoc_list_lock);
96 spin_lock_init(&pstapriv->auth_list_lock);
97 pstapriv->asoc_list_cnt = 0;
98 pstapriv->auth_list_cnt = 0;
100 pstapriv->auth_to = 3; /* 3*2 = 6 sec */
101 pstapriv->assoc_to = 3;
102 pstapriv->expire_to = 3; /* 3*2 = 6 sec */
103 pstapriv->max_num_sta = NUM_STA;
107 inline int rtw_stainfo_offset(struct sta_priv *stapriv, struct sta_info *sta)
109 int offset = (((u8 *)sta) - stapriv->pstainfo_buf)/sizeof(struct sta_info);
114 inline struct sta_info *rtw_get_stainfo_by_offset(struct sta_priv *stapriv, int offset)
116 return (struct sta_info *)(stapriv->pstainfo_buf + offset * sizeof(struct sta_info));
119 /* this function is used to free the memory of lock || sema for all stainfos */
120 void kfree_all_stainfo(struct sta_priv *pstapriv);
121 void kfree_all_stainfo(struct sta_priv *pstapriv)
123 struct list_head *plist, *phead;
125 spin_lock_bh(&pstapriv->sta_hash_lock);
127 phead = get_list_head(&pstapriv->free_sta_queue);
128 plist = get_next(phead);
130 while (phead != plist) {
131 plist = get_next(plist);
134 spin_unlock_bh(&pstapriv->sta_hash_lock);
137 void kfree_sta_priv_lock(struct sta_priv *pstapriv);
138 void kfree_sta_priv_lock(struct sta_priv *pstapriv)
140 kfree_all_stainfo(pstapriv); /* be done before free sta_hash_lock */
143 u32 _rtw_free_sta_priv(struct sta_priv *pstapriv)
145 struct list_head *phead, *plist;
146 struct sta_info *psta = NULL;
147 struct recv_reorder_ctrl *preorder_ctrl;
151 /*delete all reordering_ctrl_timer */
152 spin_lock_bh(&pstapriv->sta_hash_lock);
153 for (index = 0; index < NUM_STA; index++) {
154 phead = &(pstapriv->sta_hash[index]);
155 list_for_each(plist, phead) {
158 psta = list_entry(plist, struct sta_info,
161 for (i = 0; i < 16 ; i++) {
162 preorder_ctrl = &psta->recvreorder_ctrl[i];
163 del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
167 spin_unlock_bh(&pstapriv->sta_hash_lock);
168 /*===============================*/
170 kfree_sta_priv_lock(pstapriv);
172 vfree(pstapriv->pallocated_stainfo_buf);
177 /* struct sta_info *rtw_alloc_stainfo(_queue *pfree_sta_queue, unsigned char *hwaddr) */
178 struct sta_info *rtw_alloc_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
181 struct list_head *phash_list;
182 struct sta_info *psta;
183 struct __queue *pfree_sta_queue;
184 struct recv_reorder_ctrl *preorder_ctrl;
186 u16 wRxSeqInitialValue = 0xffff;
188 pfree_sta_queue = &pstapriv->free_sta_queue;
190 /* spin_lock_bh(&(pfree_sta_queue->lock)); */
191 spin_lock_bh(&(pstapriv->sta_hash_lock));
192 if (list_empty(&pfree_sta_queue->queue)) {
193 /* spin_unlock_bh(&(pfree_sta_queue->lock)); */
194 spin_unlock_bh(&(pstapriv->sta_hash_lock));
197 psta = container_of(get_next(&pfree_sta_queue->queue), struct sta_info, list);
199 list_del_init(&(psta->list));
201 /* spin_unlock_bh(&(pfree_sta_queue->lock)); */
203 _rtw_init_stainfo(psta);
205 psta->padapter = pstapriv->padapter;
207 memcpy(psta->hwaddr, hwaddr, ETH_ALEN);
209 index = wifi_mac_hash(hwaddr);
211 if (index >= NUM_STA) {
212 spin_unlock_bh(&(pstapriv->sta_hash_lock));
216 phash_list = &(pstapriv->sta_hash[index]);
218 /* spin_lock_bh(&(pstapriv->sta_hash_lock)); */
220 list_add_tail(&psta->hash_list, phash_list);
222 pstapriv->asoc_sta_count++;
224 /* spin_unlock_bh(&(pstapriv->sta_hash_lock)); */
226 /* Commented by Albert 2009/08/13 */
227 /* For the SMC router, the sequence number of first packet of WPS handshake will be 0. */
228 /* 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. */
229 /* So, we initialize the tid_rxseq variable as the 0xffff. */
231 for (i = 0; i < 16; i++)
232 memcpy(&psta->sta_recvpriv.rxcache.tid_rxseq[i], &wRxSeqInitialValue, 2);
234 init_addba_retry_timer(pstapriv->padapter, psta);
236 /* for A-MPDU Rx reordering buffer control */
237 for (i = 0; i < 16 ; i++) {
238 preorder_ctrl = &psta->recvreorder_ctrl[i];
240 preorder_ctrl->padapter = pstapriv->padapter;
242 preorder_ctrl->enable = false;
244 preorder_ctrl->indicate_seq = 0xffff;
245 preorder_ctrl->wend_b = 0xffff;
246 /* preorder_ctrl->wsize_b = (NR_RECVBUFF-2); */
247 preorder_ctrl->wsize_b = 64;/* 64; */
249 INIT_LIST_HEAD(&preorder_ctrl->pending_recvframe_queue.queue);
250 spin_lock_init(&preorder_ctrl->pending_recvframe_queue.lock);
252 rtw_init_recv_timer(preorder_ctrl);
256 psta->rssi_stat.UndecoratedSmoothedPWDB = (-1);
257 psta->rssi_stat.UndecoratedSmoothedCCK = (-1);
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);
271 u32 rtw_free_stainfo(struct adapter *padapter, struct sta_info *psta)
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;
284 spin_lock_bh(&psta->lock);
285 psta->state &= ~_FW_LINKED;
286 spin_unlock_bh(&psta->lock);
288 pfree_sta_queue = &pstapriv->free_sta_queue;
290 pstaxmitpriv = &psta->sta_xmitpriv;
292 /* list_del_init(&psta->sleep_list); */
294 /* list_del_init(&psta->wakeup_list); */
296 spin_lock_bh(&pxmitpriv->lock);
298 rtw_free_xmitframe_queue(pxmitpriv, &psta->sleep_q);
299 psta->sleepq_len = 0;
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)); */
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)); */
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)); */
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)); */
337 spin_unlock_bh(&pxmitpriv->lock);
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);
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); */
348 del_timer_sync(&psta->addba_retry_timer);
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;
357 preorder_ctrl = &psta->recvreorder_ctrl[i];
359 del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
361 ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
363 spin_lock_bh(&ppending_recvframe_queue->lock);
365 phead = get_list_head(ppending_recvframe_queue);
366 plist = get_next(phead);
368 while (!list_empty(phead)) {
369 prframe = (union recv_frame *)plist;
371 plist = get_next(plist);
373 list_del_init(&(prframe->u.hdr.list));
375 rtw_free_recvframe(prframe, pfree_recv_queue);
378 spin_unlock_bh(&ppending_recvframe_queue->lock);
381 if (!(psta->state & WIFI_AP_STATE))
382 rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, false);
384 /* release mac id for non-bc/mc station, */
385 rtw_release_macid(pstapriv->padapter, psta);
388 spin_lock_bh(&pstapriv->asoc_list_lock);
389 list_del_init(&psta->asoc_list);
390 spin_unlock_bh(&pstapriv->asoc_list_lock);
392 spin_lock_bh(&pstapriv->auth_list_lock);
393 if (!list_empty(&psta->auth_list)) {
394 list_del_init(&psta->auth_list);
395 pstapriv->auth_list_cnt--;
397 spin_unlock_bh(&pstapriv->auth_list_lock);
400 psta->sleepq_ac_len = 0;
403 psta->max_sp_len = 0;
409 psta->has_legacy_ac = 0;
411 pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
412 pstapriv->tim_bitmap &= ~BIT(psta->aid);
414 if ((psta->aid > 0) && (pstapriv->sta_aid[psta->aid - 1] == psta)) {
415 pstapriv->sta_aid[psta->aid - 1] = NULL;
419 psta->under_exist_checking = 0;
421 /* spin_lock_bh(&(pfree_sta_queue->lock)); */
422 list_add_tail(&psta->list, get_list_head(pfree_sta_queue));
423 /* spin_unlock_bh(&(pfree_sta_queue->lock)); */
429 /* free all stainfo which in sta_hash[all] */
430 void rtw_free_all_stainfo(struct adapter *padapter)
432 struct list_head *plist, *phead, *tmp;
434 struct sta_info *psta = NULL;
435 struct sta_priv *pstapriv = &padapter->stapriv;
436 struct sta_info *pbcmc_stainfo = rtw_get_bcmc_stainfo(padapter);
437 LIST_HEAD(stainfo_free_list);
439 if (pstapriv->asoc_sta_count == 1)
442 spin_lock_bh(&pstapriv->sta_hash_lock);
444 for (index = 0; index < NUM_STA; index++) {
445 phead = &(pstapriv->sta_hash[index]);
446 list_for_each_safe(plist, tmp, phead) {
447 psta = list_entry(plist, struct sta_info, hash_list);
449 if (pbcmc_stainfo != psta)
450 list_move(&psta->hash_list, &stainfo_free_list);
454 spin_unlock_bh(&pstapriv->sta_hash_lock);
456 list_for_each_safe(plist, tmp, &stainfo_free_list) {
457 psta = list_entry(plist, struct sta_info, hash_list);
458 rtw_free_stainfo(padapter, psta);
462 /* any station allocated can be searched by hash list */
463 struct sta_info *rtw_get_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
465 struct list_head *plist, *phead;
466 struct sta_info *psta = NULL;
469 u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
474 if (IS_MCAST(hwaddr))
479 index = wifi_mac_hash(addr);
481 spin_lock_bh(&pstapriv->sta_hash_lock);
483 phead = &(pstapriv->sta_hash[index]);
484 list_for_each(plist, phead) {
485 psta = list_entry(plist, struct sta_info, hash_list);
487 if ((!memcmp(psta->hwaddr, addr, ETH_ALEN)))
488 /* if found the matched address */
494 spin_unlock_bh(&pstapriv->sta_hash_lock);
498 u32 rtw_init_bcmc_stainfo(struct adapter *padapter)
500 struct sta_info *psta;
501 NDIS_802_11_MAC_ADDRESS bcast_addr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
503 struct sta_priv *pstapriv = &padapter->stapriv;
504 /* struct __queue *pstapending = &padapter->xmitpriv.bm_pending; */
506 psta = rtw_alloc_stainfo(pstapriv, bcast_addr);
511 /* default broadcast & multicast use macid 1 */
517 struct sta_info *rtw_get_bcmc_stainfo(struct adapter *padapter)
519 struct sta_priv *pstapriv = &padapter->stapriv;
520 u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
522 return rtw_get_stainfo(pstapriv, bc_addr);
525 u8 rtw_access_ctrl(struct adapter *padapter, u8 *mac_addr)
528 struct list_head *plist, *phead;
529 struct rtw_wlan_acl_node *paclnode;
531 struct sta_priv *pstapriv = &padapter->stapriv;
532 struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
533 struct __queue *pacl_node_q = &pacl_list->acl_node_q;
535 spin_lock_bh(&(pacl_node_q->lock));
536 phead = get_list_head(pacl_node_q);
537 list_for_each(plist, phead) {
538 paclnode = list_entry(plist, struct rtw_wlan_acl_node, list);
540 if (!memcmp(paclnode->addr, mac_addr, ETH_ALEN))
541 if (paclnode->valid == true) {
546 spin_unlock_bh(&(pacl_node_q->lock));
548 if (pacl_list->mode == 1) /* accept unless in deny list */
551 else if (pacl_list->mode == 2)/* deny unless in accept list */