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