GNU Linux-libre 5.19-rc6-gnu
[releases.git] / drivers / staging / rtl8723bs / core / rtw_sta_mgt.c
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #include <drv_types.h>
8 #include <rtw_debug.h>
9
10 void _rtw_init_stainfo(struct sta_info *psta);
11 void _rtw_init_stainfo(struct sta_info *psta)
12 {
13         memset((u8 *)psta, 0, sizeof(struct sta_info));
14
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); */
21
22         INIT_LIST_HEAD(&psta->sleep_q.queue);
23         spin_lock_init(&psta->sleep_q.lock);
24         psta->sleepq_len = 0;
25
26         _rtw_init_sta_xmit_priv(&psta->sta_xmitpriv);
27         _rtw_init_sta_recv_priv(&psta->sta_recvpriv);
28
29         INIT_LIST_HEAD(&psta->asoc_list);
30
31         INIT_LIST_HEAD(&psta->auth_list);
32
33         psta->expire_to = 0;
34
35         psta->flags = 0;
36
37         psta->capability = 0;
38
39         psta->bpairwise_key_installed = false;
40
41         psta->nonerp_set = 0;
42         psta->no_short_slot_time_set = 0;
43         psta->no_short_preamble_set = 0;
44         psta->no_ht_gf_set = 0;
45         psta->no_ht_set = 0;
46         psta->ht_20mhz_set = 0;
47
48         psta->under_exist_checking = 0;
49
50         psta->keep_alive_trycnt = 0;
51 }
52
53 u32 _rtw_init_sta_priv(struct   sta_priv *pstapriv)
54 {
55         struct sta_info *psta;
56         s32 i;
57
58         pstapriv->pallocated_stainfo_buf = vzalloc(sizeof(struct sta_info) * NUM_STA+4);
59
60         if (!pstapriv->pallocated_stainfo_buf)
61                 return _FAIL;
62
63         pstapriv->pstainfo_buf = pstapriv->pallocated_stainfo_buf + 4 -
64                 ((SIZE_PTR)(pstapriv->pallocated_stainfo_buf) & 3);
65
66         INIT_LIST_HEAD(&pstapriv->free_sta_queue.queue);
67         spin_lock_init(&pstapriv->free_sta_queue.lock);
68
69         spin_lock_init(&pstapriv->sta_hash_lock);
70
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);
77
78         psta = (struct sta_info *)(pstapriv->pstainfo_buf);
79
80         for (i = 0; i < NUM_STA; i++) {
81                 _rtw_init_stainfo(psta);
82
83                 INIT_LIST_HEAD(&(pstapriv->sta_hash[i]));
84
85                 list_add_tail(&psta->list, get_list_head(&pstapriv->free_sta_queue));
86
87                 psta++;
88         }
89
90         pstapriv->sta_dz_bitmap = 0;
91         pstapriv->tim_bitmap = 0;
92
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;
99
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;
104         return _SUCCESS;
105 }
106
107 inline int rtw_stainfo_offset(struct sta_priv *stapriv, struct sta_info *sta)
108 {
109         int offset = (((u8 *)sta) - stapriv->pstainfo_buf)/sizeof(struct sta_info);
110
111         return offset;
112 }
113
114 inline struct sta_info *rtw_get_stainfo_by_offset(struct sta_priv *stapriv, int offset)
115 {
116         return (struct sta_info *)(stapriv->pstainfo_buf + offset * sizeof(struct sta_info));
117 }
118
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)
122 {
123         struct list_head        *plist, *phead;
124
125         spin_lock_bh(&pstapriv->sta_hash_lock);
126
127         phead = get_list_head(&pstapriv->free_sta_queue);
128         plist = get_next(phead);
129
130         while (phead != plist) {
131                 plist = get_next(plist);
132         }
133
134         spin_unlock_bh(&pstapriv->sta_hash_lock);
135 }
136
137 void kfree_sta_priv_lock(struct sta_priv *pstapriv);
138 void kfree_sta_priv_lock(struct sta_priv *pstapriv)
139 {
140          kfree_all_stainfo(pstapriv); /* be done before free sta_hash_lock */
141 }
142
143 u32 _rtw_free_sta_priv(struct   sta_priv *pstapriv)
144 {
145         struct list_head        *phead, *plist;
146         struct sta_info *psta = NULL;
147         struct recv_reorder_ctrl *preorder_ctrl;
148         int     index;
149
150         if (pstapriv) {
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) {
156                                 int i;
157
158                                 psta = list_entry(plist, struct sta_info,
159                                                   hash_list);
160
161                                 for (i = 0; i < 16 ; i++) {
162                                         preorder_ctrl = &psta->recvreorder_ctrl[i];
163                                         del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
164                                 }
165                         }
166                 }
167                 spin_unlock_bh(&pstapriv->sta_hash_lock);
168                 /*===============================*/
169
170                 kfree_sta_priv_lock(pstapriv);
171
172                 vfree(pstapriv->pallocated_stainfo_buf);
173         }
174         return _SUCCESS;
175 }
176
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)
179 {
180         s32     index;
181         struct list_head        *phash_list;
182         struct sta_info *psta;
183         struct __queue *pfree_sta_queue;
184         struct recv_reorder_ctrl *preorder_ctrl;
185         int i = 0;
186         u16  wRxSeqInitialValue = 0xffff;
187
188         pfree_sta_queue = &pstapriv->free_sta_queue;
189
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));
195                 return NULL;
196         } else {
197                 psta = container_of(get_next(&pfree_sta_queue->queue), struct sta_info, list);
198
199                 list_del_init(&(psta->list));
200
201                 /* spin_unlock_bh(&(pfree_sta_queue->lock)); */
202
203                 _rtw_init_stainfo(psta);
204
205                 psta->padapter = pstapriv->padapter;
206
207                 memcpy(psta->hwaddr, hwaddr, ETH_ALEN);
208
209                 index = wifi_mac_hash(hwaddr);
210
211                 if (index >= NUM_STA) {
212                         spin_unlock_bh(&(pstapriv->sta_hash_lock));
213                         psta = NULL;
214                         goto exit;
215                 }
216                 phash_list = &(pstapriv->sta_hash[index]);
217
218                 /* spin_lock_bh(&(pstapriv->sta_hash_lock)); */
219
220                 list_add_tail(&psta->hash_list, phash_list);
221
222                 pstapriv->asoc_sta_count++;
223
224                 /* spin_unlock_bh(&(pstapriv->sta_hash_lock)); */
225
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. */
230
231                 for (i = 0; i < 16; i++)
232                         memcpy(&psta->sta_recvpriv.rxcache.tid_rxseq[i], &wRxSeqInitialValue, 2);
233
234                 init_addba_retry_timer(pstapriv->padapter, psta);
235
236                 /* for A-MPDU Rx reordering buffer control */
237                 for (i = 0; i < 16 ; i++) {
238                         preorder_ctrl = &psta->recvreorder_ctrl[i];
239
240                         preorder_ctrl->padapter = pstapriv->padapter;
241
242                         preorder_ctrl->enable = false;
243
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; */
248
249                         INIT_LIST_HEAD(&preorder_ctrl->pending_recvframe_queue.queue);
250                         spin_lock_init(&preorder_ctrl->pending_recvframe_queue.lock);
251
252                         rtw_init_recv_timer(preorder_ctrl);
253                 }
254
255                 /* init for DM */
256                 psta->rssi_stat.UndecoratedSmoothedPWDB = (-1);
257                 psta->rssi_stat.UndecoratedSmoothedCCK = (-1);
258
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);
264         }
265
266 exit:
267
268         return psta;
269 }
270
271 u32 rtw_free_stainfo(struct adapter *padapter, struct sta_info *psta)
272 {
273         int i;
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;
280
281         if (!psta)
282                 goto exit;
283
284         spin_lock_bh(&psta->lock);
285         psta->state &= ~_FW_LINKED;
286         spin_unlock_bh(&psta->lock);
287
288         pfree_sta_queue = &pstapriv->free_sta_queue;
289
290         pstaxmitpriv = &psta->sta_xmitpriv;
291
292         /* list_del_init(&psta->sleep_list); */
293
294         /* list_del_init(&psta->wakeup_list); */
295
296         spin_lock_bh(&pxmitpriv->lock);
297
298         rtw_free_xmitframe_queue(pxmitpriv, &psta->sleep_q);
299         psta->sleepq_len = 0;
300
301         /* vo */
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)); */
309
310         /* vi */
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)); */
318
319         /* be */
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)); */
327
328         /* bk */
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)); */
336
337         spin_unlock_bh(&pxmitpriv->lock);
338
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);
343
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); */
347
348         del_timer_sync(&psta->addba_retry_timer);
349
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;
356
357                 preorder_ctrl = &psta->recvreorder_ctrl[i];
358
359                 del_timer_sync(&preorder_ctrl->reordering_ctrl_timer);
360
361                 ppending_recvframe_queue = &preorder_ctrl->pending_recvframe_queue;
362
363                 spin_lock_bh(&ppending_recvframe_queue->lock);
364
365                 phead =         get_list_head(ppending_recvframe_queue);
366                 plist = get_next(phead);
367
368                 while (!list_empty(phead)) {
369                         prframe = (union recv_frame *)plist;
370
371                         plist = get_next(plist);
372
373                         list_del_init(&(prframe->u.hdr.list));
374
375                         rtw_free_recvframe(prframe, pfree_recv_queue);
376                 }
377
378                 spin_unlock_bh(&ppending_recvframe_queue->lock);
379         }
380
381         if (!(psta->state & WIFI_AP_STATE))
382                 rtw_hal_set_odm_var(padapter, HAL_ODM_STA_INFO, psta, false);
383
384         /* release mac id for non-bc/mc station, */
385         rtw_release_macid(pstapriv->padapter, psta);
386
387 /*
388         spin_lock_bh(&pstapriv->asoc_list_lock);
389         list_del_init(&psta->asoc_list);
390         spin_unlock_bh(&pstapriv->asoc_list_lock);
391 */
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--;
396         }
397         spin_unlock_bh(&pstapriv->auth_list_lock);
398
399         psta->expire_to = 0;
400         psta->sleepq_ac_len = 0;
401         psta->qos_info = 0;
402
403         psta->max_sp_len = 0;
404         psta->uapsd_bk = 0;
405         psta->uapsd_be = 0;
406         psta->uapsd_vi = 0;
407         psta->uapsd_vo = 0;
408
409         psta->has_legacy_ac = 0;
410
411         pstapriv->sta_dz_bitmap &= ~BIT(psta->aid);
412         pstapriv->tim_bitmap &= ~BIT(psta->aid);
413
414         if ((psta->aid > 0) && (pstapriv->sta_aid[psta->aid - 1] == psta)) {
415                 pstapriv->sta_aid[psta->aid - 1] = NULL;
416                 psta->aid = 0;
417         }
418
419         psta->under_exist_checking = 0;
420
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)); */
424
425 exit:
426         return _SUCCESS;
427 }
428
429 /*  free all stainfo which in sta_hash[all] */
430 void rtw_free_all_stainfo(struct adapter *padapter)
431 {
432         struct list_head *plist, *phead, *tmp;
433         s32     index;
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);
438
439         if (pstapriv->asoc_sta_count == 1)
440                 return;
441
442         spin_lock_bh(&pstapriv->sta_hash_lock);
443
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);
448
449                         if (pbcmc_stainfo != psta)
450                                 list_move(&psta->hash_list, &stainfo_free_list);
451                 }
452         }
453
454         spin_unlock_bh(&pstapriv->sta_hash_lock);
455
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);
459         }
460 }
461
462 /* any station allocated can be searched by hash list */
463 struct sta_info *rtw_get_stainfo(struct sta_priv *pstapriv, u8 *hwaddr)
464 {
465         struct list_head        *plist, *phead;
466         struct sta_info *psta = NULL;
467         u32 index;
468         u8 *addr;
469         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
470
471         if (!hwaddr)
472                 return NULL;
473
474         if (IS_MCAST(hwaddr))
475                 addr = bc_addr;
476         else
477                 addr = hwaddr;
478
479         index = wifi_mac_hash(addr);
480
481         spin_lock_bh(&pstapriv->sta_hash_lock);
482
483         phead = &(pstapriv->sta_hash[index]);
484         list_for_each(plist, phead) {
485                 psta = list_entry(plist, struct sta_info, hash_list);
486
487                 if ((!memcmp(psta->hwaddr, addr, ETH_ALEN)))
488                  /*  if found the matched address */
489                         break;
490
491                 psta = NULL;
492         }
493
494         spin_unlock_bh(&pstapriv->sta_hash_lock);
495         return psta;
496 }
497
498 u32 rtw_init_bcmc_stainfo(struct adapter *padapter)
499 {
500         struct sta_info *psta;
501         NDIS_802_11_MAC_ADDRESS bcast_addr = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
502
503         struct  sta_priv *pstapriv = &padapter->stapriv;
504         /* struct __queue       *pstapending = &padapter->xmitpriv.bm_pending; */
505
506         psta = rtw_alloc_stainfo(pstapriv, bcast_addr);
507
508         if (!psta)
509                 return _FAIL;
510
511         /*  default broadcast & multicast use macid 1 */
512         psta->mac_id = 1;
513
514         return _SUCCESS;
515 }
516
517 struct sta_info *rtw_get_bcmc_stainfo(struct adapter *padapter)
518 {
519         struct sta_priv *pstapriv = &padapter->stapriv;
520         u8 bc_addr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
521
522         return rtw_get_stainfo(pstapriv, bc_addr);
523 }
524
525 u8 rtw_access_ctrl(struct adapter *padapter, u8 *mac_addr)
526 {
527         bool res = true;
528         struct list_head        *plist, *phead;
529         struct rtw_wlan_acl_node *paclnode;
530         bool match = false;
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;
534
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);
539
540                 if (!memcmp(paclnode->addr, mac_addr, ETH_ALEN))
541                         if (paclnode->valid == true) {
542                                 match = true;
543                                 break;
544                         }
545         }
546         spin_unlock_bh(&(pacl_node_q->lock));
547
548         if (pacl_list->mode == 1) /* accept unless in deny list */
549                 res = !match;
550
551         else if (pacl_list->mode == 2)/* deny unless in accept list */
552                 res = match;
553         else
554                  res = true;
555
556         return res;
557 }