GNU Linux-libre 4.14.259-gnu1
[releases.git] / drivers / staging / rtl8188eu / core / rtw_sta_mgt.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
4  *
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.
8  *
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
12  * more details.
13  *
14  ******************************************************************************/
15 #define _RTW_STA_MGT_C_
16
17 #include <osdep_service.h>
18 #include <drv_types.h>
19 #include <recv_osdep.h>
20 #include <xmit_osdep.h>
21 #include <mlme_osdep.h>
22 #include <sta_info.h>
23 #include <linux/vmalloc.h>
24
25 static void _rtw_init_stainfo(struct sta_info *psta)
26 {
27         memset((u8 *)psta, 0, sizeof(struct sta_info));
28
29          spin_lock_init(&psta->lock);
30         INIT_LIST_HEAD(&psta->list);
31         INIT_LIST_HEAD(&psta->hash_list);
32         _rtw_init_queue(&psta->sleep_q);
33         psta->sleepq_len = 0;
34
35         _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
36         _rtw_init_sta_recv_priv(&psta->sta_recvpriv);
37
38 #ifdef CONFIG_88EU_AP_MODE
39
40         INIT_LIST_HEAD(&psta->asoc_list);
41
42         INIT_LIST_HEAD(&psta->auth_list);
43
44         psta->expire_to = 0;
45
46         psta->flags = 0;
47
48         psta->capability = 0;
49
50         psta->bpairwise_key_installed = false;
51
52         psta->nonerp_set = 0;
53         psta->no_short_slot_time_set = 0;
54         psta->no_short_preamble_set = 0;
55         psta->no_ht_gf_set = 0;
56         psta->no_ht_set = 0;
57         psta->ht_20mhz_set = 0;
58
59         psta->under_exist_checking = 0;
60
61         psta->keep_alive_trycnt = 0;
62
63 #endif  /*  CONFIG_88EU_AP_MODE */
64 }
65
66 u32     _rtw_init_sta_priv(struct       sta_priv *pstapriv)
67 {
68         struct sta_info *psta;
69         s32 i;
70
71         pstapriv->pallocated_stainfo_buf = vzalloc(sizeof(struct sta_info) * NUM_STA + 4);
72
73         if (!pstapriv->pallocated_stainfo_buf)
74                 return _FAIL;
75
76         pstapriv->pstainfo_buf = pstapriv->pallocated_stainfo_buf + 4 -
77                 ((size_t)(pstapriv->pallocated_stainfo_buf) & 3);
78
79         _rtw_init_queue(&pstapriv->free_sta_queue);
80
81         spin_lock_init(&pstapriv->sta_hash_lock);
82
83         pstapriv->asoc_sta_count = 0;
84         _rtw_init_queue(&pstapriv->sleep_q);
85         _rtw_init_queue(&pstapriv->wakeup_q);
86
87         psta = (struct sta_info *)(pstapriv->pstainfo_buf);
88
89         for (i = 0; i < NUM_STA; i++) {
90                 _rtw_init_stainfo(psta);
91
92                 INIT_LIST_HEAD(&(pstapriv->sta_hash[i]));
93
94                 list_add_tail(&psta->list, get_list_head(&pstapriv->free_sta_queue));
95
96                 psta++;
97         }
98
99 #ifdef CONFIG_88EU_AP_MODE
100
101         pstapriv->sta_dz_bitmap = 0;
102         pstapriv->tim_bitmap = 0;
103
104         INIT_LIST_HEAD(&pstapriv->asoc_list);
105         INIT_LIST_HEAD(&pstapriv->auth_list);
106         spin_lock_init(&pstapriv->asoc_list_lock);
107         spin_lock_init(&pstapriv->auth_list_lock);
108         pstapriv->asoc_list_cnt = 0;
109         pstapriv->auth_list_cnt = 0;
110
111         pstapriv->auth_to = 3; /*  3*2 = 6 sec */
112         pstapriv->assoc_to = 3;
113         pstapriv->expire_to = 3; /*  3*2 = 6 sec */
114         pstapriv->max_num_sta = NUM_STA;
115 #endif
116
117         return _SUCCESS;
118 }
119
120 inline int rtw_stainfo_offset(struct sta_priv *stapriv, struct sta_info *sta)
121 {
122         int offset = (((u8 *)sta) - stapriv->pstainfo_buf)/sizeof(struct sta_info);
123
124         if (!stainfo_offset_valid(offset))
125                 DBG_88E("%s invalid offset(%d), out of range!!!", __func__, offset);
126
127         return offset;
128 }
129
130 inline struct sta_info *rtw_get_stainfo_by_offset(struct sta_priv *stapriv, int offset)
131 {
132         if (!stainfo_offset_valid(offset))
133                 DBG_88E("%s invalid offset(%d), out of range!!!", __func__, offset);
134
135         return (struct sta_info *)(stapriv->pstainfo_buf + offset * sizeof(struct sta_info));
136 }
137
138 u32     _rtw_free_sta_priv(struct       sta_priv *pstapriv)
139 {
140         struct list_head *phead, *plist;
141         struct sta_info *psta = NULL;
142         struct recv_reorder_ctrl *preorder_ctrl;
143         int     index;
144
145         if (pstapriv) {
146                 /*      delete all reordering_ctrl_timer                */
147                 spin_lock_bh(&pstapriv->sta_hash_lock);
148                 for (index = 0; index < NUM_STA; index++) {
149                         phead = &(pstapriv->sta_hash[index]);
150                         plist = phead->next;
151
152                         while (phead != plist) {
153                                 int i;
154
155                                 psta = container_of(plist, struct sta_info,
156                                                     hash_list);
157                                 plist = plist->next;
158
159                                 for (i = 0; i < 16; i++) {
160                                         preorder_ctrl = &psta->recvreorder_ctrl[i];
161                                         del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
162                                 }
163                         }
164                 }
165                 spin_unlock_bh(&pstapriv->sta_hash_lock);
166                 /*===============================*/
167
168                 vfree(pstapriv->pallocated_stainfo_buf);
169         }
170
171         return _SUCCESS;
172 }
173
174 struct sta_info *rtw_alloc_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
175 {
176         s32 index;
177         struct list_head *phash_list;
178         struct sta_info *psta;
179         struct __queue *pfree_sta_queue;
180         struct recv_reorder_ctrl *preorder_ctrl;
181         int i = 0;
182         u16  wRxSeqInitialValue = 0xffff;
183
184         pfree_sta_queue = &pstapriv->free_sta_queue;
185
186         spin_lock_bh(&pfree_sta_queue->lock);
187         psta = list_first_entry_or_null(&pfree_sta_queue->queue,
188                                         struct sta_info, list);
189         if (!psta) {
190                 spin_unlock_bh(&pfree_sta_queue->lock);
191         } else {
192                 list_del_init(&psta->list);
193                 spin_unlock_bh(&pfree_sta_queue->lock);
194                 _rtw_init_stainfo(psta);
195                 memcpy(psta->hwaddr, hwaddr, ETH_ALEN);
196                 index = wifi_mac_hash(hwaddr);
197                 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_info_, ("rtw_alloc_stainfo: index=%x", index));
198                 if (index >= NUM_STA) {
199                         RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_, ("ERROR => rtw_alloc_stainfo: index >= NUM_STA"));
200                         psta = NULL;
201                         goto exit;
202                 }
203                 phash_list = &pstapriv->sta_hash[index];
204
205                 spin_lock_bh(&pstapriv->sta_hash_lock);
206                 list_add_tail(&psta->hash_list, phash_list);
207                 pstapriv->asoc_sta_count++;
208                 spin_unlock_bh(&pstapriv->sta_hash_lock);
209
210 /*  Commented by Albert 2009/08/13 */
211 /*  For the SMC router, the sequence number of first packet of WPS handshake will be 0. */
212 /*  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. */
213 /*  So, we initialize the tid_rxseq variable as the 0xffff. */
214
215                 for (i = 0; i < 16; i++)
216                         memcpy(&psta->sta_recvpriv.rxcache.tid_rxseq[i], &wRxSeqInitialValue, 2);
217
218                 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_info_,
219                          ("alloc number_%d stainfo  with hwaddr = %pM\n",
220                          pstapriv->asoc_sta_count, hwaddr));
221
222                 init_addba_retry_timer(pstapriv->padapter, psta);
223
224                 /* for A-MPDU Rx reordering buffer control */
225                 for (i = 0; i < 16; i++) {
226                         preorder_ctrl = &psta->recvreorder_ctrl[i];
227
228                         preorder_ctrl->padapter = pstapriv->padapter;
229
230                         preorder_ctrl->enable = false;
231
232                         preorder_ctrl->indicate_seq = 0xffff;
233                         preorder_ctrl->wend_b = 0xffff;
234                         preorder_ctrl->wsize_b = 64;/* 64; */
235
236                         _rtw_init_queue(&preorder_ctrl->pending_recvframe_queue);
237
238                         rtw_init_recv_timer(preorder_ctrl);
239                 }
240
241                 /* init for DM */
242                 psta->rssi_stat.UndecoratedSmoothedPWDB = (-1);
243                 psta->rssi_stat.UndecoratedSmoothedCCK = (-1);
244
245                 /* init for the sequence number of received management frame */
246                 psta->RxMgmtFrameSeqNum = 0xffff;
247         }
248
249 exit:
250         return psta;
251 }
252
253 /*  using pstapriv->sta_hash_lock to protect */
254 u32     rtw_free_stainfo(struct adapter *padapter, struct sta_info *psta)
255 {
256         int i;
257         struct __queue *pfree_sta_queue;
258         struct recv_reorder_ctrl *preorder_ctrl;
259         struct  sta_xmit_priv   *pstaxmitpriv;
260         struct  xmit_priv       *pxmitpriv = &padapter->xmitpriv;
261         struct  sta_priv *pstapriv = &padapter->stapriv;
262
263         if (!psta)
264                 goto exit;
265
266         pfree_sta_queue = &pstapriv->free_sta_queue;
267
268         pstaxmitpriv = &psta->sta_xmitpriv;
269
270         spin_lock_bh(&pxmitpriv->lock);
271
272         rtw_free_xmitframe_queue(pxmitpriv, &psta->sleep_q);
273         psta->sleepq_len = 0;
274
275         rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vo_q.sta_pending);
276
277         list_del_init(&(pstaxmitpriv->vo_q.tx_pending));
278
279         rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->vi_q.sta_pending);
280
281         list_del_init(&(pstaxmitpriv->vi_q.tx_pending));
282
283         rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->bk_q.sta_pending);
284
285         list_del_init(&(pstaxmitpriv->bk_q.tx_pending));
286
287         rtw_free_xmitframe_queue(pxmitpriv, &pstaxmitpriv->be_q.sta_pending);
288
289         list_del_init(&(pstaxmitpriv->be_q.tx_pending));
290
291         spin_unlock_bh(&pxmitpriv->lock);
292
293         list_del_init(&psta->hash_list);
294         RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_,
295                  ("\n free number_%d stainfo with hwaddr=0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
296                  pstapriv->asoc_sta_count, psta->hwaddr[0], psta->hwaddr[1],
297                  psta->hwaddr[2], psta->hwaddr[3], psta->hwaddr[4],
298                  psta->hwaddr[5]));
299         pstapriv->asoc_sta_count--;
300
301         /*  re-init sta_info; 20061114 */
302         _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
303         _rtw_init_sta_recv_priv(&psta->sta_recvpriv);
304
305         del_timer_sync(&psta->addba_retry_timer);
306
307         /* for A-MPDU Rx reordering buffer control, cancel reordering_ctrl_timer */
308         for (i = 0; i < 16; i++) {
309                 struct list_head *phead, *plist;
310                 struct recv_frame *prframe;
311                 struct __queue *ppending_recvframe_queue;
312                 struct __queue *pfree_recv_queue = &padapter->recvpriv.free_recv_queue;
313
314                 preorder_ctrl = &psta->recvreorder_ctrl[i];
315
316                 del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
317
318                 ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
319
320                 spin_lock_bh(&ppending_recvframe_queue->lock);
321
322                 phead =         get_list_head(ppending_recvframe_queue);
323                 plist = phead->next;
324
325                 while (!list_empty(phead)) {
326                         prframe = container_of(plist, struct recv_frame, list);
327
328                         plist = plist->next;
329
330                         list_del_init(&(prframe->list));
331
332                         rtw_free_recvframe(prframe, pfree_recv_queue);
333                 }
334
335                 spin_unlock_bh(&ppending_recvframe_queue->lock);
336         }
337
338         if (!(psta->state & WIFI_AP_STATE))
339                 rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, false);
340
341 #ifdef CONFIG_88EU_AP_MODE
342
343         spin_lock_bh(&pstapriv->auth_list_lock);
344         if (!list_empty(&psta->auth_list)) {
345                 list_del_init(&psta->auth_list);
346                 pstapriv->auth_list_cnt--;
347         }
348         spin_unlock_bh(&pstapriv->auth_list_lock);
349
350         psta->expire_to = 0;
351
352         psta->sleepq_ac_len = 0;
353         psta->qos_info = 0;
354
355         psta->max_sp_len = 0;
356         psta->uapsd_bk = 0;
357         psta->uapsd_be = 0;
358         psta->uapsd_vi = 0;
359         psta->uapsd_vo = 0;
360         psta->has_legacy_ac = 0;
361
362         pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
363         pstapriv->tim_bitmap &= ~BIT(psta->aid);
364
365         if ((psta->aid > 0) && (pstapriv->sta_aid[psta->aid - 1] == psta)) {
366                 pstapriv->sta_aid[psta->aid - 1] = NULL;
367                 psta->aid = 0;
368         }
369
370         psta->under_exist_checking = 0;
371
372 #endif  /*  CONFIG_88EU_AP_MODE */
373
374         spin_lock_bh(&(pfree_sta_queue->lock));
375         list_add_tail(&psta->list, get_list_head(pfree_sta_queue));
376         spin_unlock_bh(&pfree_sta_queue->lock);
377
378 exit:
379
380         return _SUCCESS;
381 }
382
383 /*  free all stainfo which in sta_hash[all] */
384 void rtw_free_all_stainfo(struct adapter *padapter)
385 {
386         struct list_head *plist, *phead;
387         s32     index;
388         struct sta_info *psta = NULL;
389         struct  sta_priv *pstapriv = &padapter->stapriv;
390         struct sta_info *pbcmc_stainfo = rtw_get_bcmc_stainfo(padapter);
391
392         if (pstapriv->asoc_sta_count == 1)
393                 return;
394
395         spin_lock_bh(&pstapriv->sta_hash_lock);
396
397         for (index = 0; index < NUM_STA; index++) {
398                 phead = &(pstapriv->sta_hash[index]);
399                 plist = phead->next;
400
401                 while (phead != plist) {
402                         psta = container_of(plist, struct sta_info, hash_list);
403
404                         plist = plist->next;
405
406                         if (pbcmc_stainfo != psta)
407                                 rtw_free_stainfo(padapter, psta);
408                 }
409         }
410         spin_unlock_bh(&pstapriv->sta_hash_lock);
411 }
412
413 /* any station allocated can be searched by hash list */
414 struct sta_info *rtw_get_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
415 {
416         struct list_head *plist, *phead;
417         struct sta_info *psta = NULL;
418         u32     index;
419         u8 *addr;
420         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
421
422         if (!hwaddr)
423                 return NULL;
424
425         if (IS_MCAST(hwaddr))
426                 addr = bc_addr;
427         else
428                 addr = hwaddr;
429
430         index = wifi_mac_hash(addr);
431
432         spin_lock_bh(&pstapriv->sta_hash_lock);
433
434         phead = &(pstapriv->sta_hash[index]);
435         plist = phead->next;
436
437         while (phead != plist) {
438                 psta = container_of(plist, struct sta_info, hash_list);
439
440                 if ((!memcmp(psta->hwaddr, addr, ETH_ALEN)) == true) {
441                         /*  if found the matched address */
442                         break;
443                 }
444                 psta = NULL;
445                 plist = plist->next;
446         }
447
448         spin_unlock_bh(&pstapriv->sta_hash_lock);
449         return psta;
450 }
451
452 u32 rtw_init_bcmc_stainfo(struct adapter *padapter)
453 {
454         struct sta_info         *psta;
455         u32 res = _SUCCESS;
456         unsigned char bcast_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
457         struct  sta_priv *pstapriv = &padapter->stapriv;
458
459         psta = rtw_alloc_stainfo(pstapriv, bcast_addr);
460
461         if (!psta) {
462                 res = _FAIL;
463                 RT_TRACE(_module_rtl871x_sta_mgt_c_, _drv_err_, ("rtw_alloc_stainfo fail"));
464                 goto exit;
465         }
466
467         /*  default broadcast & multicast use macid 1 */
468         psta->mac_id = 1;
469
470 exit:
471         return res;
472 }
473
474 struct sta_info *rtw_get_bcmc_stainfo(struct adapter *padapter)
475 {
476         struct sta_priv         *pstapriv = &padapter->stapriv;
477         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
478          return rtw_get_stainfo(pstapriv, bc_addr);
479 }
480
481 u8 rtw_access_ctrl(struct adapter *padapter, u8 *mac_addr)
482 {
483         u8 res = true;
484 #ifdef CONFIG_88EU_AP_MODE
485         struct list_head *plist, *phead;
486         struct rtw_wlan_acl_node *paclnode;
487         u8 match = false;
488         struct sta_priv *pstapriv = &padapter->stapriv;
489         struct wlan_acl_pool *pacl_list = &pstapriv->acl_list;
490         struct __queue *pacl_node_q = &pacl_list->acl_node_q;
491
492         spin_lock_bh(&(pacl_node_q->lock));
493         phead = get_list_head(pacl_node_q);
494         plist = phead->next;
495         while (phead != plist) {
496                 paclnode = container_of(plist, struct rtw_wlan_acl_node, list);
497                 plist = plist->next;
498
499                 if (!memcmp(paclnode->addr, mac_addr, ETH_ALEN)) {
500                         if (paclnode->valid) {
501                                 match = true;
502                                 break;
503                         }
504                 }
505         }
506         spin_unlock_bh(&pacl_node_q->lock);
507
508         if (pacl_list->mode == 1)/* accept unless in deny list */
509                 res = (match) ? false : true;
510         else if (pacl_list->mode == 2)/* deny unless in accept list */
511                 res = (match) ? true : false;
512         else
513                  res = true;
514
515 #endif
516
517         return res;
518 }