1 /******************************************************************************
3 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19 ******************************************************************************/
20 #define _RTW_STA_MGT_C_
22 #include <osdep_service.h>
23 #include <drv_types.h>
24 #include <recv_osdep.h>
25 #include <xmit_osdep.h>
26 #include <mlme_osdep.h>
29 static void _rtw_init_stainfo(struct sta_info *psta)
32 _rtw_memset((u8 *)psta, 0, sizeof (struct sta_info));
34 _rtw_spinlock_init(&psta->lock);
35 _rtw_init_listhead(&psta->list);
36 _rtw_init_listhead(&psta->hash_list);
37 _rtw_init_queue(&psta->sleep_q);
40 _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
41 _rtw_init_sta_recv_priv(&psta->sta_recvpriv);
43 #ifdef CONFIG_88EU_AP_MODE
45 _rtw_init_listhead(&psta->asoc_list);
47 _rtw_init_listhead(&psta->auth_list);
55 psta->bpairwise_key_installed = false;
57 #ifdef CONFIG_88EU_AP_MODE
59 psta->no_short_slot_time_set = 0;
60 psta->no_short_preamble_set = 0;
61 psta->no_ht_gf_set = 0;
63 psta->ht_20mhz_set = 0;
66 psta->under_exist_checking = 0;
68 psta->keep_alive_trycnt = 0;
70 #endif /* CONFIG_88EU_AP_MODE */
75 u32 _rtw_init_sta_priv(struct sta_priv *pstapriv)
77 struct sta_info *psta;
82 pstapriv->pallocated_stainfo_buf = rtw_zvmalloc(sizeof(struct sta_info) * NUM_STA + 4);
84 if (!pstapriv->pallocated_stainfo_buf)
87 pstapriv->pstainfo_buf = pstapriv->pallocated_stainfo_buf + 4 -
88 ((size_t)(pstapriv->pallocated_stainfo_buf) & 3);
90 _rtw_init_queue(&pstapriv->free_sta_queue);
92 _rtw_spinlock_init(&pstapriv->sta_hash_lock);
94 pstapriv->asoc_sta_count = 0;
95 _rtw_init_queue(&pstapriv->sleep_q);
96 _rtw_init_queue(&pstapriv->wakeup_q);
98 psta = (struct sta_info *)(pstapriv->pstainfo_buf);
100 for (i = 0; i < NUM_STA; i++) {
101 _rtw_init_stainfo(psta);
103 _rtw_init_listhead(&(pstapriv->sta_hash[i]));
105 rtw_list_insert_tail(&psta->list, get_list_head(&pstapriv->free_sta_queue));
110 #ifdef CONFIG_88EU_AP_MODE
112 pstapriv->sta_dz_bitmap = 0;
113 pstapriv->tim_bitmap = 0;
115 _rtw_init_listhead(&pstapriv->asoc_list);
116 _rtw_init_listhead(&pstapriv->auth_list);
117 _rtw_spinlock_init(&pstapriv->asoc_list_lock);
118 _rtw_spinlock_init(&pstapriv->auth_list_lock);
119 pstapriv->asoc_list_cnt = 0;
120 pstapriv->auth_list_cnt = 0;
122 pstapriv->auth_to = 3; /* 3*2 = 6 sec */
123 pstapriv->assoc_to = 3;
124 pstapriv->expire_to = 3; /* 3*2 = 6 sec */
125 pstapriv->max_num_sta = NUM_STA;
133 inline int rtw_stainfo_offset(struct sta_priv *stapriv, struct sta_info *sta)
135 int offset = (((u8 *)sta) - stapriv->pstainfo_buf)/sizeof(struct sta_info);
137 if (!stainfo_offset_valid(offset))
138 DBG_88E("%s invalid offset(%d), out of range!!!", __func__, offset);
143 inline struct sta_info *rtw_get_stainfo_by_offset(struct sta_priv *stapriv, int offset)
145 if (!stainfo_offset_valid(offset))
146 DBG_88E("%s invalid offset(%d), out of range!!!", __func__, offset);
148 return (struct sta_info *)(stapriv->pstainfo_buf + offset * sizeof(struct sta_info));
151 void _rtw_free_sta_xmit_priv_lock(struct sta_xmit_priv *psta_xmitpriv);
152 void _rtw_free_sta_xmit_priv_lock(struct sta_xmit_priv *psta_xmitpriv)
156 _rtw_spinlock_free(&psta_xmitpriv->lock);
158 _rtw_spinlock_free(&(psta_xmitpriv->be_q.sta_pending.lock));
159 _rtw_spinlock_free(&(psta_xmitpriv->bk_q.sta_pending.lock));
160 _rtw_spinlock_free(&(psta_xmitpriv->vi_q.sta_pending.lock));
161 _rtw_spinlock_free(&(psta_xmitpriv->vo_q.sta_pending.lock));
165 static void _rtw_free_sta_recv_priv_lock(struct sta_recv_priv *psta_recvpriv)
169 _rtw_spinlock_free(&psta_recvpriv->lock);
171 _rtw_spinlock_free(&(psta_recvpriv->defrag_q.lock));
176 void rtw_mfree_stainfo(struct sta_info *psta);
177 void rtw_mfree_stainfo(struct sta_info *psta)
181 if (&psta->lock != NULL)
182 _rtw_spinlock_free(&psta->lock);
184 _rtw_free_sta_xmit_priv_lock(&psta->sta_xmitpriv);
185 _rtw_free_sta_recv_priv_lock(&psta->sta_recvpriv);
190 /* this function is used to free the memory of lock || sema for all stainfos */
191 void rtw_mfree_all_stainfo(struct sta_priv *pstapriv);
192 void rtw_mfree_all_stainfo(struct sta_priv *pstapriv)
195 struct list_head *plist, *phead;
196 struct sta_info *psta = NULL;
200 _enter_critical_bh(&pstapriv->sta_hash_lock, &irql);
202 phead = get_list_head(&pstapriv->free_sta_queue);
203 plist = get_next(phead);
205 while ((rtw_end_of_queue_search(phead, plist)) == false) {
206 psta = LIST_CONTAINOR(plist, struct sta_info , list);
207 plist = get_next(plist);
209 rtw_mfree_stainfo(psta);
212 _exit_critical_bh(&pstapriv->sta_hash_lock, &irql);
217 static void rtw_mfree_sta_priv_lock(struct sta_priv *pstapriv)
219 #ifdef CONFIG_88EU_AP_MODE
220 struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
223 rtw_mfree_all_stainfo(pstapriv); /* be done before free sta_hash_lock */
225 _rtw_spinlock_free(&pstapriv->free_sta_queue.lock);
227 _rtw_spinlock_free(&pstapriv->sta_hash_lock);
228 _rtw_spinlock_free(&pstapriv->wakeup_q.lock);
229 _rtw_spinlock_free(&pstapriv->sleep_q.lock);
231 #ifdef CONFIG_88EU_AP_MODE
232 _rtw_spinlock_free(&pstapriv->asoc_list_lock);
233 _rtw_spinlock_free(&pstapriv->auth_list_lock);
234 _rtw_spinlock_free(&pacl_list->acl_node_q.lock);
238 u32 _rtw_free_sta_priv(struct sta_priv *pstapriv)
241 struct list_head *phead, *plist;
242 struct sta_info *psta = NULL;
243 struct recv_reorder_ctrl *preorder_ctrl;
248 /* delete all reordering_ctrl_timer */
249 _enter_critical_bh(&pstapriv->sta_hash_lock, &irql);
250 for (index = 0; index < NUM_STA; index++) {
251 phead = &(pstapriv->sta_hash[index]);
252 plist = get_next(phead);
254 while ((rtw_end_of_queue_search(phead, plist)) == false) {
256 psta = LIST_CONTAINOR(plist, struct sta_info , hash_list);
257 plist = get_next(plist);
259 for (i = 0; i < 16; i++) {
260 preorder_ctrl = &psta->recvreorder_ctrl[i];
261 _cancel_timer_ex(&preorder_ctrl->reordering_ctrl_timer);
265 _exit_critical_bh(&pstapriv->sta_hash_lock, &irql);
266 /*===============================*/
268 rtw_mfree_sta_priv_lock(pstapriv);
270 if (pstapriv->pallocated_stainfo_buf)
271 rtw_vmfree(pstapriv->pallocated_stainfo_buf, sizeof(struct sta_info)*NUM_STA+4);
278 struct sta_info *rtw_alloc_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
280 unsigned long irql, irql2;
282 struct list_head *phash_list;
283 struct sta_info *psta;
284 struct __queue *pfree_sta_queue;
285 struct recv_reorder_ctrl *preorder_ctrl;
287 u16 wRxSeqInitialValue = 0xffff;
291 pfree_sta_queue = &pstapriv->free_sta_queue;
293 _enter_critical_bh(&(pfree_sta_queue->lock), &irql);
295 if (_rtw_queue_empty(pfree_sta_queue) == true) {
296 _exit_critical_bh(&(pfree_sta_queue->lock), &irql);
299 psta = LIST_CONTAINOR(get_next(&pfree_sta_queue->queue), struct sta_info, list);
300 rtw_list_delete(&(psta->list));
301 _exit_critical_bh(&(pfree_sta_queue->lock), &irql);
302 _rtw_init_stainfo(psta);
303 memcpy(psta->hwaddr, hwaddr, ETH_ALEN);
304 index = wifi_mac_hash(hwaddr);
305 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_info_, ("rtw_alloc_stainfo: index=%x", index));
306 if (index >= NUM_STA) {
307 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_, ("ERROR => rtw_alloc_stainfo: index >= NUM_STA"));
311 phash_list = &(pstapriv->sta_hash[index]);
313 _enter_critical_bh(&(pstapriv->sta_hash_lock), &irql2);
315 rtw_list_insert_tail(&psta->hash_list, phash_list);
317 pstapriv->asoc_sta_count++;
319 _exit_critical_bh(&(pstapriv->sta_hash_lock), &irql2);
321 /* Commented by Albert 2009/08/13 */
322 /* For the SMC router, the sequence number of first packet of WPS handshake will be 0. */
323 /* 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. */
324 /* So, we initialize the tid_rxseq variable as the 0xffff. */
326 for (i = 0; i < 16; i++)
327 memcpy(&psta->sta_recvpriv.rxcache.tid_rxseq[i], &wRxSeqInitialValue, 2);
329 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_info_,
330 ("alloc number_%d stainfo with hwaddr = %pM\n",
331 pstapriv->asoc_sta_count , hwaddr));
333 init_addba_retry_timer(pstapriv->padapter, psta);
335 /* for A-MPDU Rx reordering buffer control */
336 for (i = 0; i < 16; i++) {
337 preorder_ctrl = &psta->recvreorder_ctrl[i];
339 preorder_ctrl->padapter = pstapriv->padapter;
341 preorder_ctrl->enable = false;
343 preorder_ctrl->indicate_seq = 0xffff;
344 preorder_ctrl->wend_b = 0xffff;
345 preorder_ctrl->wsize_b = 64;/* 64; */
347 _rtw_init_queue(&preorder_ctrl->pending_recvframe_queue);
349 rtw_init_recv_timer(preorder_ctrl);
353 psta->rssi_stat.UndecoratedSmoothedPWDB = (-1);
354 psta->rssi_stat.UndecoratedSmoothedCCK = (-1);
356 /* init for the sequence number of received management frame */
357 psta->RxMgmtFrameSeqNum = 0xffff;
367 /* using pstapriv->sta_hash_lock to protect */
368 u32 rtw_free_stainfo(struct adapter *padapter , struct sta_info *psta)
372 struct __queue *pfree_sta_queue;
373 struct recv_reorder_ctrl *preorder_ctrl;
374 struct sta_xmit_priv *pstaxmitpriv;
375 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
376 struct sta_priv *pstapriv = &padapter->stapriv;
383 pfree_sta_queue = &pstapriv->free_sta_queue;
385 pstaxmitpriv = &psta->sta_xmitpriv;
387 _enter_critical_bh(&pxmitpriv->lock, &irql0);
389 rtw_free_xmitframe_queue(pxmitpriv, &psta->sleep_q);
390 psta->sleepq_len = 0;
392 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vo_q.sta_pending);
394 rtw_list_delete(&(pstaxmitpriv->vo_q.tx_pending));
396 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vi_q.sta_pending);
398 rtw_list_delete(&(pstaxmitpriv->vi_q.tx_pending));
400 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->bk_q.sta_pending);
402 rtw_list_delete(&(pstaxmitpriv->bk_q.tx_pending));
404 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->be_q.sta_pending);
406 rtw_list_delete(&(pstaxmitpriv->be_q.tx_pending));
408 _exit_critical_bh(&pxmitpriv->lock, &irql0);
410 rtw_list_delete(&psta->hash_list);
411 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_, ("\n free number_%d stainfo with hwaddr=0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n", pstapriv->asoc_sta_count , psta->hwaddr[0], psta->hwaddr[1], psta->hwaddr[2], psta->hwaddr[3], psta->hwaddr[4], psta->hwaddr[5]));
412 pstapriv->asoc_sta_count--;
414 /* re-init sta_info; 20061114 */
415 _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
416 _rtw_init_sta_recv_priv(&psta->sta_recvpriv);
418 _cancel_timer_ex(&psta->addba_retry_timer);
420 /* for A-MPDU Rx reordering buffer control, cancel reordering_ctrl_timer */
421 for (i = 0; i < 16; i++) {
423 struct list_head *phead, *plist;
424 union recv_frame *prframe;
425 struct __queue *ppending_recvframe_queue;
426 struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
428 preorder_ctrl = &psta->recvreorder_ctrl[i];
430 _cancel_timer_ex(&preorder_ctrl->reordering_ctrl_timer);
432 ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
434 _enter_critical_bh(&ppending_recvframe_queue->lock, &irql);
436 phead = get_list_head(ppending_recvframe_queue);
437 plist = get_next(phead);
439 while (!rtw_is_list_empty(phead)) {
440 prframe = LIST_CONTAINOR(plist, union recv_frame, u);
442 plist = get_next(plist);
444 rtw_list_delete(&(prframe->u.hdr.list));
446 rtw_free_recvframe(prframe, pfree_recv_queue);
449 _exit_critical_bh(&ppending_recvframe_queue->lock, &irql);
452 if (!(psta->state & WIFI_AP_STATE))
453 rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, false);
455 #ifdef CONFIG_88EU_AP_MODE
457 _enter_critical_bh(&pstapriv->auth_list_lock, &irql0);
458 if (!rtw_is_list_empty(&psta->auth_list)) {
459 rtw_list_delete(&psta->auth_list);
460 pstapriv->auth_list_cnt--;
462 _exit_critical_bh(&pstapriv->auth_list_lock, &irql0);
466 psta->sleepq_ac_len = 0;
469 psta->max_sp_len = 0;
474 psta->has_legacy_ac = 0;
476 pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
477 pstapriv->tim_bitmap &= ~BIT(psta->aid);
479 if ((psta->aid > 0) && (pstapriv->sta_aid[psta->aid - 1] == psta)) {
480 pstapriv->sta_aid[psta->aid - 1] = NULL;
484 psta->under_exist_checking = 0;
486 #endif /* CONFIG_88EU_AP_MODE */
488 _enter_critical_bh(&(pfree_sta_queue->lock), &irql0);
489 rtw_list_insert_tail(&psta->list, get_list_head(pfree_sta_queue));
490 _exit_critical_bh(&(pfree_sta_queue->lock), &irql0);
499 /* free all stainfo which in sta_hash[all] */
500 void rtw_free_all_stainfo(struct adapter *padapter)
503 struct list_head *plist, *phead;
505 struct sta_info *psta = NULL;
506 struct sta_priv *pstapriv = &padapter->stapriv;
507 struct sta_info *pbcmc_stainfo = rtw_get_bcmc_stainfo(padapter);
511 if (pstapriv->asoc_sta_count == 1)
514 _enter_critical_bh(&pstapriv->sta_hash_lock, &irql);
516 for (index = 0; index < NUM_STA; index++) {
517 phead = &(pstapriv->sta_hash[index]);
518 plist = get_next(phead);
520 while ((!rtw_end_of_queue_search(phead, plist))) {
521 psta = LIST_CONTAINOR(plist, struct sta_info , hash_list);
523 plist = get_next(plist);
525 if (pbcmc_stainfo != psta)
526 rtw_free_stainfo(padapter , psta);
530 _exit_critical_bh(&pstapriv->sta_hash_lock, &irql);
537 /* any station allocated can be searched by hash list */
538 struct sta_info *rtw_get_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
541 struct list_head *plist, *phead;
542 struct sta_info *psta = NULL;
545 u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
552 if (IS_MCAST(hwaddr))
557 index = wifi_mac_hash(addr);
559 _enter_critical_bh(&pstapriv->sta_hash_lock, &irql);
561 phead = &(pstapriv->sta_hash[index]);
562 plist = get_next(phead);
564 while ((!rtw_end_of_queue_search(phead, plist))) {
565 psta = LIST_CONTAINOR(plist, struct sta_info, hash_list);
567 if ((_rtw_memcmp(psta->hwaddr, addr, ETH_ALEN)) == true) {
568 /* if found the matched address */
572 plist = get_next(plist);
575 _exit_critical_bh(&pstapriv->sta_hash_lock, &irql);
580 u32 rtw_init_bcmc_stainfo(struct adapter *padapter)
582 struct sta_info *psta;
584 unsigned char bcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
585 struct sta_priv *pstapriv = &padapter->stapriv;
589 psta = rtw_alloc_stainfo(pstapriv, bcast_addr);
593 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_, ("rtw_alloc_stainfo fail"));
597 /* default broadcast & multicast use macid 1 */
605 struct sta_info *rtw_get_bcmc_stainfo(struct adapter *padapter)
607 struct sta_info *psta;
608 struct sta_priv *pstapriv = &padapter->stapriv;
609 u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
611 psta = rtw_get_stainfo(pstapriv, bc_addr);
616 u8 rtw_access_ctrl(struct adapter *padapter, u8 *mac_addr)
619 #ifdef CONFIG_88EU_AP_MODE
621 struct list_head *plist, *phead;
622 struct rtw_wlan_acl_node *paclnode;
624 struct sta_priv *pstapriv = &padapter->stapriv;
625 struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
626 struct __queue *pacl_node_q = &pacl_list->acl_node_q;
628 _enter_critical_bh(&(pacl_node_q->lock), &irql);
629 phead = get_list_head(pacl_node_q);
630 plist = get_next(phead);
631 while ((!rtw_end_of_queue_search(phead, plist))) {
632 paclnode = LIST_CONTAINOR(plist, struct rtw_wlan_acl_node, list);
633 plist = get_next(plist);
635 if (_rtw_memcmp(paclnode->addr, mac_addr, ETH_ALEN)) {
636 if (paclnode->valid) {
642 _exit_critical_bh(&(pacl_node_q->lock), &irql);
644 if (pacl_list->mode == 1)/* accept unless in deny list */
645 res = (match) ? false : true;
646 else if (pacl_list->mode == 2)/* deny unless in accept list */
647 res = (match) ? true : false;