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>
28 #include <linux/vmalloc.h>
30 static void _rtw_init_stainfo(struct sta_info *psta)
32 memset((u8 *)psta, 0, sizeof(struct sta_info));
34 spin_lock_init(&psta->lock);
35 INIT_LIST_HEAD(&psta->list);
36 INIT_LIST_HEAD(&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 INIT_LIST_HEAD(&psta->asoc_list);
47 INIT_LIST_HEAD(&psta->auth_list);
55 psta->bpairwise_key_installed = false;
58 psta->no_short_slot_time_set = 0;
59 psta->no_short_preamble_set = 0;
60 psta->no_ht_gf_set = 0;
62 psta->ht_20mhz_set = 0;
64 psta->under_exist_checking = 0;
66 psta->keep_alive_trycnt = 0;
68 #endif /* CONFIG_88EU_AP_MODE */
72 u32 _rtw_init_sta_priv(struct sta_priv *pstapriv)
74 struct sta_info *psta;
78 pstapriv->pallocated_stainfo_buf = vzalloc(sizeof(struct sta_info) * NUM_STA + 4);
80 if (!pstapriv->pallocated_stainfo_buf)
83 pstapriv->pstainfo_buf = pstapriv->pallocated_stainfo_buf + 4 -
84 ((size_t)(pstapriv->pallocated_stainfo_buf) & 3);
86 _rtw_init_queue(&pstapriv->free_sta_queue);
88 spin_lock_init(&pstapriv->sta_hash_lock);
90 pstapriv->asoc_sta_count = 0;
91 _rtw_init_queue(&pstapriv->sleep_q);
92 _rtw_init_queue(&pstapriv->wakeup_q);
94 psta = (struct sta_info *)(pstapriv->pstainfo_buf);
96 for (i = 0; i < NUM_STA; i++) {
97 _rtw_init_stainfo(psta);
99 INIT_LIST_HEAD(&(pstapriv->sta_hash[i]));
101 list_add_tail(&psta->list, get_list_head(&pstapriv->free_sta_queue));
106 #ifdef CONFIG_88EU_AP_MODE
108 pstapriv->sta_dz_bitmap = 0;
109 pstapriv->tim_bitmap = 0;
111 INIT_LIST_HEAD(&pstapriv->asoc_list);
112 INIT_LIST_HEAD(&pstapriv->auth_list);
113 spin_lock_init(&pstapriv->asoc_list_lock);
114 spin_lock_init(&pstapriv->auth_list_lock);
115 pstapriv->asoc_list_cnt = 0;
116 pstapriv->auth_list_cnt = 0;
118 pstapriv->auth_to = 3; /* 3*2 = 6 sec */
119 pstapriv->assoc_to = 3;
120 pstapriv->expire_to = 3; /* 3*2 = 6 sec */
121 pstapriv->max_num_sta = NUM_STA;
128 inline int rtw_stainfo_offset(struct sta_priv *stapriv, struct sta_info *sta)
130 int offset = (((u8 *)sta) - stapriv->pstainfo_buf)/sizeof(struct sta_info);
132 if (!stainfo_offset_valid(offset))
133 DBG_88E("%s invalid offset(%d), out of range!!!", __func__, offset);
138 inline struct sta_info *rtw_get_stainfo_by_offset(struct sta_priv *stapriv, int offset)
140 if (!stainfo_offset_valid(offset))
141 DBG_88E("%s invalid offset(%d), out of range!!!", __func__, offset);
143 return (struct sta_info *)(stapriv->pstainfo_buf + offset * sizeof(struct sta_info));
146 u32 _rtw_free_sta_priv(struct sta_priv *pstapriv)
148 struct list_head *phead, *plist;
149 struct sta_info *psta = NULL;
150 struct recv_reorder_ctrl *preorder_ctrl;
154 /* delete all reordering_ctrl_timer */
155 spin_lock_bh(&pstapriv->sta_hash_lock);
156 for (index = 0; index < NUM_STA; index++) {
157 phead = &(pstapriv->sta_hash[index]);
160 while (phead != plist) {
162 psta = container_of(plist, struct sta_info,
166 for (i = 0; i < 16; i++) {
167 preorder_ctrl = &psta->recvreorder_ctrl[i];
168 del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
172 spin_unlock_bh(&pstapriv->sta_hash_lock);
173 /*===============================*/
175 if (pstapriv->pallocated_stainfo_buf)
176 vfree(pstapriv->pallocated_stainfo_buf);
182 struct sta_info *rtw_alloc_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
185 struct list_head *phash_list;
186 struct sta_info *psta;
187 struct __queue *pfree_sta_queue;
188 struct recv_reorder_ctrl *preorder_ctrl;
190 u16 wRxSeqInitialValue = 0xffff;
193 pfree_sta_queue = &pstapriv->free_sta_queue;
195 spin_lock_bh(&(pfree_sta_queue->lock));
197 if (list_empty(&pfree_sta_queue->queue)) {
198 spin_unlock_bh(&pfree_sta_queue->lock);
201 psta = container_of((&pfree_sta_queue->queue)->next, struct sta_info, list);
202 list_del_init(&(psta->list));
203 spin_unlock_bh(&pfree_sta_queue->lock);
204 _rtw_init_stainfo(psta);
205 memcpy(psta->hwaddr, hwaddr, ETH_ALEN);
206 index = wifi_mac_hash(hwaddr);
207 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_info_, ("rtw_alloc_stainfo: index=%x", index));
208 if (index >= NUM_STA) {
209 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_, ("ERROR => rtw_alloc_stainfo: index >= NUM_STA"));
213 phash_list = &(pstapriv->sta_hash[index]);
215 spin_lock_bh(&(pstapriv->sta_hash_lock));
217 list_add_tail(&psta->hash_list, phash_list);
219 pstapriv->asoc_sta_count++;
221 spin_unlock_bh(&pstapriv->sta_hash_lock);
223 /* Commented by Albert 2009/08/13 */
224 /* For the SMC router, the sequence number of first packet of WPS handshake will be 0. */
225 /* 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. */
226 /* So, we initialize the tid_rxseq variable as the 0xffff. */
228 for (i = 0; i < 16; i++)
229 memcpy(&psta->sta_recvpriv.rxcache.tid_rxseq[i], &wRxSeqInitialValue, 2);
231 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_info_,
232 ("alloc number_%d stainfo with hwaddr = %pM\n",
233 pstapriv->asoc_sta_count, hwaddr));
235 init_addba_retry_timer(pstapriv->padapter, psta);
237 /* for A-MPDU Rx reordering buffer control */
238 for (i = 0; i < 16; i++) {
239 preorder_ctrl = &psta->recvreorder_ctrl[i];
241 preorder_ctrl->padapter = pstapriv->padapter;
243 preorder_ctrl->enable = false;
245 preorder_ctrl->indicate_seq = 0xffff;
246 preorder_ctrl->wend_b = 0xffff;
247 preorder_ctrl->wsize_b = 64;/* 64; */
249 _rtw_init_queue(&preorder_ctrl->pending_recvframe_queue);
251 rtw_init_recv_timer(preorder_ctrl);
255 psta->rssi_stat.UndecoratedSmoothedPWDB = (-1);
256 psta->rssi_stat.UndecoratedSmoothedCCK = (-1);
258 /* init for the sequence number of received management frame */
259 psta->RxMgmtFrameSeqNum = 0xffff;
266 /* using pstapriv->sta_hash_lock to protect */
267 u32 rtw_free_stainfo(struct adapter *padapter, struct sta_info *psta)
270 struct __queue *pfree_sta_queue;
271 struct recv_reorder_ctrl *preorder_ctrl;
272 struct sta_xmit_priv *pstaxmitpriv;
273 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
274 struct sta_priv *pstapriv = &padapter->stapriv;
280 pfree_sta_queue = &pstapriv->free_sta_queue;
282 pstaxmitpriv = &psta->sta_xmitpriv;
284 spin_lock_bh(&pxmitpriv->lock);
286 rtw_free_xmitframe_queue(pxmitpriv, &psta->sleep_q);
287 psta->sleepq_len = 0;
289 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vo_q.sta_pending);
291 list_del_init(&(pstaxmitpriv->vo_q.tx_pending));
293 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vi_q.sta_pending);
295 list_del_init(&(pstaxmitpriv->vi_q.tx_pending));
297 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->bk_q.sta_pending);
299 list_del_init(&(pstaxmitpriv->bk_q.tx_pending));
301 rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->be_q.sta_pending);
303 list_del_init(&(pstaxmitpriv->be_q.tx_pending));
305 spin_unlock_bh(&pxmitpriv->lock);
307 list_del_init(&psta->hash_list);
308 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_,
309 ("\n free number_%d stainfo with hwaddr=0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
310 pstapriv->asoc_sta_count, psta->hwaddr[0], psta->hwaddr[1],
311 psta->hwaddr[2], psta->hwaddr[3], psta->hwaddr[4],
313 pstapriv->asoc_sta_count--;
315 /* re-init sta_info; 20061114 */
316 _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
317 _rtw_init_sta_recv_priv(&psta->sta_recvpriv);
319 del_timer_sync(&psta->addba_retry_timer);
321 /* for A-MPDU Rx reordering buffer control, cancel reordering_ctrl_timer */
322 for (i = 0; i < 16; i++) {
323 struct list_head *phead, *plist;
324 struct recv_frame *prhdr;
325 struct recv_frame *prframe;
326 struct __queue *ppending_recvframe_queue;
327 struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
329 preorder_ctrl = &psta->recvreorder_ctrl[i];
331 del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
333 ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
335 spin_lock_bh(&ppending_recvframe_queue->lock);
337 phead = get_list_head(ppending_recvframe_queue);
340 while (!list_empty(phead)) {
341 prhdr = container_of(plist, struct recv_frame, list);
342 prframe = (struct recv_frame *)prhdr;
346 list_del_init(&(prframe->list));
348 rtw_free_recvframe(prframe, pfree_recv_queue);
351 spin_unlock_bh(&ppending_recvframe_queue->lock);
354 if (!(psta->state & WIFI_AP_STATE))
355 rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, false);
357 #ifdef CONFIG_88EU_AP_MODE
359 spin_lock_bh(&pstapriv->auth_list_lock);
360 if (!list_empty(&psta->auth_list)) {
361 list_del_init(&psta->auth_list);
362 pstapriv->auth_list_cnt--;
364 spin_unlock_bh(&pstapriv->auth_list_lock);
368 psta->sleepq_ac_len = 0;
371 psta->max_sp_len = 0;
376 psta->has_legacy_ac = 0;
378 pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
379 pstapriv->tim_bitmap &= ~BIT(psta->aid);
381 if ((psta->aid > 0) && (pstapriv->sta_aid[psta->aid - 1] == psta)) {
382 pstapriv->sta_aid[psta->aid - 1] = NULL;
386 psta->under_exist_checking = 0;
388 #endif /* CONFIG_88EU_AP_MODE */
390 spin_lock_bh(&(pfree_sta_queue->lock));
391 list_add_tail(&psta->list, get_list_head(pfree_sta_queue));
392 spin_unlock_bh(&pfree_sta_queue->lock);
400 /* free all stainfo which in sta_hash[all] */
401 void rtw_free_all_stainfo(struct adapter *padapter)
403 struct list_head *plist, *phead;
405 struct sta_info *psta = NULL;
406 struct sta_priv *pstapriv = &padapter->stapriv;
407 struct sta_info *pbcmc_stainfo = rtw_get_bcmc_stainfo(padapter);
410 if (pstapriv->asoc_sta_count == 1)
413 spin_lock_bh(&pstapriv->sta_hash_lock);
415 for (index = 0; index < NUM_STA; index++) {
416 phead = &(pstapriv->sta_hash[index]);
419 while (phead != plist) {
420 psta = container_of(plist, struct sta_info, hash_list);
424 if (pbcmc_stainfo != psta)
425 rtw_free_stainfo(padapter, psta);
428 spin_unlock_bh(&pstapriv->sta_hash_lock);
431 /* any station allocated can be searched by hash list */
432 struct sta_info *rtw_get_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
434 struct list_head *plist, *phead;
435 struct sta_info *psta = NULL;
438 u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
444 if (IS_MCAST(hwaddr))
449 index = wifi_mac_hash(addr);
451 spin_lock_bh(&pstapriv->sta_hash_lock);
453 phead = &(pstapriv->sta_hash[index]);
456 while (phead != plist) {
457 psta = container_of(plist, struct sta_info, hash_list);
459 if ((!memcmp(psta->hwaddr, addr, ETH_ALEN)) == true) {
460 /* if found the matched address */
467 spin_unlock_bh(&pstapriv->sta_hash_lock);
471 u32 rtw_init_bcmc_stainfo(struct adapter *padapter)
473 struct sta_info *psta;
475 unsigned char bcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
476 struct sta_priv *pstapriv = &padapter->stapriv;
479 psta = rtw_alloc_stainfo(pstapriv, bcast_addr);
483 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_, ("rtw_alloc_stainfo fail"));
487 /* default broadcast & multicast use macid 1 */
494 struct sta_info *rtw_get_bcmc_stainfo(struct adapter *padapter)
496 struct sta_info *psta;
497 struct sta_priv *pstapriv = &padapter->stapriv;
498 u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
499 psta = rtw_get_stainfo(pstapriv, bc_addr);
503 u8 rtw_access_ctrl(struct adapter *padapter, u8 *mac_addr)
506 #ifdef CONFIG_88EU_AP_MODE
507 struct list_head *plist, *phead;
508 struct rtw_wlan_acl_node *paclnode;
510 struct sta_priv *pstapriv = &padapter->stapriv;
511 struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
512 struct __queue *pacl_node_q = &pacl_list->acl_node_q;
514 spin_lock_bh(&(pacl_node_q->lock));
515 phead = get_list_head(pacl_node_q);
517 while (phead != plist) {
518 paclnode = container_of(plist, struct rtw_wlan_acl_node, list);
521 if (!memcmp(paclnode->addr, mac_addr, ETH_ALEN)) {
522 if (paclnode->valid) {
528 spin_unlock_bh(&pacl_node_q->lock);
530 if (pacl_list->mode == 1)/* accept unless in deny list */
531 res = (match) ? false : true;
532 else if (pacl_list->mode == 2)/* deny unless in accept list */
533 res = (match) ? true : false;