Linux-libre 3.16.85-gnu
[librecmc/linux-libre.git] / net / mac80211 / sta_info.c
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/etherdevice.h>
13 #include <linux/netdevice.h>
14 #include <linux/types.h>
15 #include <linux/slab.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
18 #include <linux/timer.h>
19 #include <linux/rtnetlink.h>
20
21 #include <net/mac80211.h>
22 #include "ieee80211_i.h"
23 #include "driver-ops.h"
24 #include "rate.h"
25 #include "sta_info.h"
26 #include "debugfs_sta.h"
27 #include "mesh.h"
28 #include "wme.h"
29
30 /**
31  * DOC: STA information lifetime rules
32  *
33  * STA info structures (&struct sta_info) are managed in a hash table
34  * for faster lookup and a list for iteration. They are managed using
35  * RCU, i.e. access to the list and hash table is protected by RCU.
36  *
37  * Upon allocating a STA info structure with sta_info_alloc(), the caller
38  * owns that structure. It must then insert it into the hash table using
39  * either sta_info_insert() or sta_info_insert_rcu(); only in the latter
40  * case (which acquires an rcu read section but must not be called from
41  * within one) will the pointer still be valid after the call. Note that
42  * the caller may not do much with the STA info before inserting it, in
43  * particular, it may not start any mesh peer link management or add
44  * encryption keys.
45  *
46  * When the insertion fails (sta_info_insert()) returns non-zero), the
47  * structure will have been freed by sta_info_insert()!
48  *
49  * Station entries are added by mac80211 when you establish a link with a
50  * peer. This means different things for the different type of interfaces
51  * we support. For a regular station this mean we add the AP sta when we
52  * receive an association response from the AP. For IBSS this occurs when
53  * get to know about a peer on the same IBSS. For WDS we add the sta for
54  * the peer immediately upon device open. When using AP mode we add stations
55  * for each respective station upon request from userspace through nl80211.
56  *
57  * In order to remove a STA info structure, various sta_info_destroy_*()
58  * calls are available.
59  *
60  * There is no concept of ownership on a STA entry, each structure is
61  * owned by the global hash table/list until it is removed. All users of
62  * the structure need to be RCU protected so that the structure won't be
63  * freed before they are done using it.
64  */
65
66 /* Caller must hold local->sta_mtx */
67 static int sta_info_hash_del(struct ieee80211_local *local,
68                              struct sta_info *sta)
69 {
70         struct sta_info *s;
71
72         s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
73                                       lockdep_is_held(&local->sta_mtx));
74         if (!s)
75                 return -ENOENT;
76         if (s == sta) {
77                 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
78                                    s->hnext);
79                 return 0;
80         }
81
82         while (rcu_access_pointer(s->hnext) &&
83                rcu_access_pointer(s->hnext) != sta)
84                 s = rcu_dereference_protected(s->hnext,
85                                         lockdep_is_held(&local->sta_mtx));
86         if (rcu_access_pointer(s->hnext)) {
87                 rcu_assign_pointer(s->hnext, sta->hnext);
88                 return 0;
89         }
90
91         return -ENOENT;
92 }
93
94 static void __cleanup_single_sta(struct sta_info *sta)
95 {
96         int ac, i;
97         struct tid_ampdu_tx *tid_tx;
98         struct ieee80211_sub_if_data *sdata = sta->sdata;
99         struct ieee80211_local *local = sdata->local;
100         struct ps_data *ps;
101
102         if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
103             test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
104                 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
105                     sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
106                         ps = &sdata->bss->ps;
107                 else if (ieee80211_vif_is_mesh(&sdata->vif))
108                         ps = &sdata->u.mesh.ps;
109                 else
110                         return;
111
112                 clear_sta_flag(sta, WLAN_STA_PS_STA);
113                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
114
115                 atomic_dec(&ps->num_sta_ps);
116                 sta_info_recalc_tim(sta);
117         }
118
119         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
120                 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
121                 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
122                 ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]);
123         }
124
125         if (ieee80211_vif_is_mesh(&sdata->vif))
126                 mesh_sta_cleanup(sta);
127
128         cancel_work_sync(&sta->drv_unblock_wk);
129
130         /*
131          * Destroy aggregation state here. It would be nice to wait for the
132          * driver to finish aggregation stop and then clean up, but for now
133          * drivers have to handle aggregation stop being requested, followed
134          * directly by station destruction.
135          */
136         for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
137                 kfree(sta->ampdu_mlme.tid_start_tx[i]);
138                 tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]);
139                 if (!tid_tx)
140                         continue;
141                 ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending);
142                 kfree(tid_tx);
143         }
144 }
145
146 static void cleanup_single_sta(struct sta_info *sta)
147 {
148         struct ieee80211_sub_if_data *sdata = sta->sdata;
149         struct ieee80211_local *local = sdata->local;
150
151         __cleanup_single_sta(sta);
152         sta_info_free(local, sta);
153 }
154
155 /* protected by RCU */
156 struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
157                               const u8 *addr)
158 {
159         struct ieee80211_local *local = sdata->local;
160         struct sta_info *sta;
161
162         sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
163                                     lockdep_is_held(&local->sta_mtx));
164         while (sta) {
165                 if (sta->sdata == sdata &&
166                     ether_addr_equal(sta->sta.addr, addr))
167                         break;
168                 sta = rcu_dereference_check(sta->hnext,
169                                             lockdep_is_held(&local->sta_mtx));
170         }
171         return sta;
172 }
173
174 /*
175  * Get sta info either from the specified interface
176  * or from one of its vlans
177  */
178 struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
179                                   const u8 *addr)
180 {
181         struct ieee80211_local *local = sdata->local;
182         struct sta_info *sta;
183
184         sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)],
185                                     lockdep_is_held(&local->sta_mtx));
186         while (sta) {
187                 if ((sta->sdata == sdata ||
188                      (sta->sdata->bss && sta->sdata->bss == sdata->bss)) &&
189                     ether_addr_equal(sta->sta.addr, addr))
190                         break;
191                 sta = rcu_dereference_check(sta->hnext,
192                                             lockdep_is_held(&local->sta_mtx));
193         }
194         return sta;
195 }
196
197 struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
198                                      int idx)
199 {
200         struct ieee80211_local *local = sdata->local;
201         struct sta_info *sta;
202         int i = 0;
203
204         list_for_each_entry_rcu(sta, &local->sta_list, list) {
205                 if (sdata != sta->sdata)
206                         continue;
207                 if (i < idx) {
208                         ++i;
209                         continue;
210                 }
211                 return sta;
212         }
213
214         return NULL;
215 }
216
217 /**
218  * sta_info_free - free STA
219  *
220  * @local: pointer to the global information
221  * @sta: STA info to free
222  *
223  * This function must undo everything done by sta_info_alloc()
224  * that may happen before sta_info_insert(). It may only be
225  * called when sta_info_insert() has not been attempted (and
226  * if that fails, the station is freed anyway.)
227  */
228 void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
229 {
230         int i;
231
232         if (sta->rate_ctrl)
233                 rate_control_free_sta(sta);
234
235         if (sta->tx_lat) {
236                 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
237                         kfree(sta->tx_lat[i].bins);
238                 kfree(sta->tx_lat);
239         }
240
241         sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
242
243         kfree(rcu_dereference_raw(sta->sta.rates));
244         kfree(sta);
245 }
246
247 /* Caller must hold local->sta_mtx */
248 static void sta_info_hash_add(struct ieee80211_local *local,
249                               struct sta_info *sta)
250 {
251         lockdep_assert_held(&local->sta_mtx);
252         sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)];
253         rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
254 }
255
256 static void sta_unblock(struct work_struct *wk)
257 {
258         struct sta_info *sta;
259
260         sta = container_of(wk, struct sta_info, drv_unblock_wk);
261
262         if (sta->dead)
263                 return;
264
265         if (!test_sta_flag(sta, WLAN_STA_PS_STA)) {
266                 local_bh_disable();
267                 ieee80211_sta_ps_deliver_wakeup(sta);
268                 local_bh_enable();
269         } else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) {
270                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
271
272                 local_bh_disable();
273                 ieee80211_sta_ps_deliver_poll_response(sta);
274                 local_bh_enable();
275         } else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) {
276                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
277
278                 local_bh_disable();
279                 ieee80211_sta_ps_deliver_uapsd(sta);
280                 local_bh_enable();
281         } else
282                 clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
283 }
284
285 static int sta_prepare_rate_control(struct ieee80211_local *local,
286                                     struct sta_info *sta, gfp_t gfp)
287 {
288         if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
289                 return 0;
290
291         sta->rate_ctrl = local->rate_ctrl;
292         sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl,
293                                                      &sta->sta, gfp);
294         if (!sta->rate_ctrl_priv)
295                 return -ENOMEM;
296
297         return 0;
298 }
299
300 struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
301                                 const u8 *addr, gfp_t gfp)
302 {
303         struct ieee80211_local *local = sdata->local;
304         struct sta_info *sta;
305         struct timespec uptime;
306         struct ieee80211_tx_latency_bin_ranges *tx_latency;
307         int i;
308
309         sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp);
310         if (!sta)
311                 return NULL;
312
313         rcu_read_lock();
314         tx_latency = rcu_dereference(local->tx_latency);
315         /* init stations Tx latency statistics && TID bins */
316         if (tx_latency) {
317                 sta->tx_lat = kzalloc(IEEE80211_NUM_TIDS *
318                                       sizeof(struct ieee80211_tx_latency_stat),
319                                       GFP_ATOMIC);
320                 if (!sta->tx_lat) {
321                         rcu_read_unlock();
322                         goto free;
323                 }
324
325                 if (tx_latency->n_ranges) {
326                         for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
327                                 /* size of bins is size of the ranges +1 */
328                                 sta->tx_lat[i].bin_count =
329                                         tx_latency->n_ranges + 1;
330                                 sta->tx_lat[i].bins =
331                                         kcalloc(sta->tx_lat[i].bin_count,
332                                                 sizeof(u32), GFP_ATOMIC);
333                                 if (!sta->tx_lat[i].bins) {
334                                         rcu_read_unlock();
335                                         goto free;
336                                 }
337                         }
338                 }
339         }
340         rcu_read_unlock();
341
342         spin_lock_init(&sta->lock);
343         spin_lock_init(&sta->ps_lock);
344         INIT_WORK(&sta->drv_unblock_wk, sta_unblock);
345         INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work);
346         mutex_init(&sta->ampdu_mlme.mtx);
347 #ifdef CONFIG_MAC80211_MESH
348         if (ieee80211_vif_is_mesh(&sdata->vif) &&
349             !sdata->u.mesh.user_mpm)
350                 init_timer(&sta->plink_timer);
351         sta->nonpeer_pm = NL80211_MESH_POWER_ACTIVE;
352 #endif
353
354         memcpy(sta->sta.addr, addr, ETH_ALEN);
355         sta->local = local;
356         sta->sdata = sdata;
357         sta->last_rx = jiffies;
358
359         sta->sta_state = IEEE80211_STA_NONE;
360
361         do_posix_clock_monotonic_gettime(&uptime);
362         sta->last_connected = uptime.tv_sec;
363         ewma_init(&sta->avg_signal, 1024, 8);
364         for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
365                 ewma_init(&sta->chain_signal_avg[i], 1024, 8);
366
367         if (sta_prepare_rate_control(local, sta, gfp))
368                 goto free;
369
370         for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
371                 /*
372                  * timer_to_tid must be initialized with identity mapping
373                  * to enable session_timer's data differentiation. See
374                  * sta_rx_agg_session_timer_expired for usage.
375                  */
376                 sta->timer_to_tid[i] = i;
377         }
378         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
379                 skb_queue_head_init(&sta->ps_tx_buf[i]);
380                 skb_queue_head_init(&sta->tx_filtered[i]);
381         }
382
383         for (i = 0; i < IEEE80211_NUM_TIDS; i++)
384                 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
385
386         sta->sta.smps_mode = IEEE80211_SMPS_OFF;
387         if (sdata->vif.type == NL80211_IFTYPE_AP ||
388             sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
389                 struct ieee80211_supported_band *sband =
390                         local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
391                 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
392                                 IEEE80211_HT_CAP_SM_PS_SHIFT;
393                 /*
394                  * Assume that hostapd advertises our caps in the beacon and
395                  * this is the known_smps_mode for a station that just assciated
396                  */
397                 switch (smps) {
398                 case WLAN_HT_SMPS_CONTROL_DISABLED:
399                         sta->known_smps_mode = IEEE80211_SMPS_OFF;
400                         break;
401                 case WLAN_HT_SMPS_CONTROL_STATIC:
402                         sta->known_smps_mode = IEEE80211_SMPS_STATIC;
403                         break;
404                 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
405                         sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
406                         break;
407                 default:
408                         WARN_ON(1);
409                 }
410         }
411
412         sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
413         return sta;
414
415 free:
416         if (sta->tx_lat) {
417                 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
418                         kfree(sta->tx_lat[i].bins);
419                 kfree(sta->tx_lat);
420         }
421         kfree(sta);
422         return NULL;
423 }
424
425 static int sta_info_insert_check(struct sta_info *sta)
426 {
427         struct ieee80211_sub_if_data *sdata = sta->sdata;
428
429         /*
430          * Can't be a WARN_ON because it can be triggered through a race:
431          * something inserts a STA (on one CPU) without holding the RTNL
432          * and another CPU turns off the net device.
433          */
434         if (unlikely(!ieee80211_sdata_running(sdata)))
435                 return -ENETDOWN;
436
437         if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) ||
438                     is_multicast_ether_addr(sta->sta.addr)))
439                 return -EINVAL;
440
441         return 0;
442 }
443
444 static int sta_info_insert_drv_state(struct ieee80211_local *local,
445                                      struct ieee80211_sub_if_data *sdata,
446                                      struct sta_info *sta)
447 {
448         enum ieee80211_sta_state state;
449         int err = 0;
450
451         for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) {
452                 err = drv_sta_state(local, sdata, sta, state, state + 1);
453                 if (err)
454                         break;
455         }
456
457         if (!err) {
458                 /*
459                  * Drivers using legacy sta_add/sta_remove callbacks only
460                  * get uploaded set to true after sta_add is called.
461                  */
462                 if (!local->ops->sta_add)
463                         sta->uploaded = true;
464                 return 0;
465         }
466
467         if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
468                 sdata_info(sdata,
469                            "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n",
470                            sta->sta.addr, state + 1, err);
471                 err = 0;
472         }
473
474         /* unwind on error */
475         for (; state > IEEE80211_STA_NOTEXIST; state--)
476                 WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1));
477
478         return err;
479 }
480
481 /*
482  * should be called with sta_mtx locked
483  * this function replaces the mutex lock
484  * with a RCU lock
485  */
486 static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU)
487 {
488         struct ieee80211_local *local = sta->local;
489         struct ieee80211_sub_if_data *sdata = sta->sdata;
490         struct station_info *sinfo;
491         int err = 0;
492
493         lockdep_assert_held(&local->sta_mtx);
494
495         sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL);
496         if (!sinfo) {
497                 err = -ENOMEM;
498                 goto out_err;
499         }
500
501         /* check if STA exists already */
502         if (sta_info_get_bss(sdata, sta->sta.addr)) {
503                 err = -EEXIST;
504                 goto out_err;
505         }
506
507         local->num_sta++;
508         local->sta_generation++;
509         smp_mb();
510
511         /* simplify things and don't accept BA sessions yet */
512         set_sta_flag(sta, WLAN_STA_BLOCK_BA);
513
514         /* make the station visible */
515         sta_info_hash_add(local, sta);
516
517         list_add_rcu(&sta->list, &local->sta_list);
518
519         /* notify driver */
520         err = sta_info_insert_drv_state(local, sdata, sta);
521         if (err)
522                 goto out_remove;
523
524         set_sta_flag(sta, WLAN_STA_INSERTED);
525         /* accept BA sessions now */
526         clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
527
528         ieee80211_recalc_min_chandef(sdata);
529         ieee80211_sta_debugfs_add(sta);
530         rate_control_add_sta_debugfs(sta);
531
532         sinfo->generation = local->sta_generation;
533         cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL);
534         kfree(sinfo);
535
536         sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr);
537
538         /* move reference to rcu-protected */
539         rcu_read_lock();
540         mutex_unlock(&local->sta_mtx);
541
542         if (ieee80211_vif_is_mesh(&sdata->vif))
543                 mesh_accept_plinks_update(sdata);
544
545         return 0;
546  out_remove:
547         sta_info_hash_del(local, sta);
548         list_del_rcu(&sta->list);
549         local->num_sta--;
550         synchronize_net();
551         __cleanup_single_sta(sta);
552  out_err:
553         mutex_unlock(&local->sta_mtx);
554         kfree(sinfo);
555         rcu_read_lock();
556         return err;
557 }
558
559 int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU)
560 {
561         struct ieee80211_local *local = sta->local;
562         int err;
563
564         might_sleep();
565
566         err = sta_info_insert_check(sta);
567         if (err) {
568                 rcu_read_lock();
569                 goto out_free;
570         }
571
572         mutex_lock(&local->sta_mtx);
573
574         err = sta_info_insert_finish(sta);
575         if (err)
576                 goto out_free;
577
578         return 0;
579  out_free:
580         sta_info_free(local, sta);
581         return err;
582 }
583
584 int sta_info_insert(struct sta_info *sta)
585 {
586         int err = sta_info_insert_rcu(sta);
587
588         rcu_read_unlock();
589
590         return err;
591 }
592
593 static inline void __bss_tim_set(u8 *tim, u16 id)
594 {
595         /*
596          * This format has been mandated by the IEEE specifications,
597          * so this line may not be changed to use the __set_bit() format.
598          */
599         tim[id / 8] |= (1 << (id % 8));
600 }
601
602 static inline void __bss_tim_clear(u8 *tim, u16 id)
603 {
604         /*
605          * This format has been mandated by the IEEE specifications,
606          * so this line may not be changed to use the __clear_bit() format.
607          */
608         tim[id / 8] &= ~(1 << (id % 8));
609 }
610
611 static inline bool __bss_tim_get(u8 *tim, u16 id)
612 {
613         /*
614          * This format has been mandated by the IEEE specifications,
615          * so this line may not be changed to use the test_bit() format.
616          */
617         return tim[id / 8] & (1 << (id % 8));
618 }
619
620 static unsigned long ieee80211_tids_for_ac(int ac)
621 {
622         /* If we ever support TIDs > 7, this obviously needs to be adjusted */
623         switch (ac) {
624         case IEEE80211_AC_VO:
625                 return BIT(6) | BIT(7);
626         case IEEE80211_AC_VI:
627                 return BIT(4) | BIT(5);
628         case IEEE80211_AC_BE:
629                 return BIT(0) | BIT(3);
630         case IEEE80211_AC_BK:
631                 return BIT(1) | BIT(2);
632         default:
633                 WARN_ON(1);
634                 return 0;
635         }
636 }
637
638 void sta_info_recalc_tim(struct sta_info *sta)
639 {
640         struct ieee80211_local *local = sta->local;
641         struct ps_data *ps;
642         bool indicate_tim = false;
643         u8 ignore_for_tim = sta->sta.uapsd_queues;
644         int ac;
645         u16 id;
646
647         if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
648             sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
649                 if (WARN_ON_ONCE(!sta->sdata->bss))
650                         return;
651
652                 ps = &sta->sdata->bss->ps;
653                 id = sta->sta.aid;
654 #ifdef CONFIG_MAC80211_MESH
655         } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) {
656                 ps = &sta->sdata->u.mesh.ps;
657                 /* TIM map only for 1 <= PLID <= IEEE80211_MAX_AID */
658                 id = sta->plid % (IEEE80211_MAX_AID + 1);
659 #endif
660         } else {
661                 return;
662         }
663
664         /* No need to do anything if the driver does all */
665         if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
666                 return;
667
668         if (sta->dead)
669                 goto done;
670
671         /*
672          * If all ACs are delivery-enabled then we should build
673          * the TIM bit for all ACs anyway; if only some are then
674          * we ignore those and build the TIM bit using only the
675          * non-enabled ones.
676          */
677         if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1)
678                 ignore_for_tim = 0;
679
680         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
681                 unsigned long tids;
682
683                 if (ignore_for_tim & BIT(ac))
684                         continue;
685
686                 indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) ||
687                                 !skb_queue_empty(&sta->ps_tx_buf[ac]);
688                 if (indicate_tim)
689                         break;
690
691                 tids = ieee80211_tids_for_ac(ac);
692
693                 indicate_tim |=
694                         sta->driver_buffered_tids & tids;
695         }
696
697  done:
698         spin_lock_bh(&local->tim_lock);
699
700         if (indicate_tim == __bss_tim_get(ps->tim, id))
701                 goto out_unlock;
702
703         if (indicate_tim)
704                 __bss_tim_set(ps->tim, id);
705         else
706                 __bss_tim_clear(ps->tim, id);
707
708         if (local->ops->set_tim) {
709                 local->tim_in_locked_section = true;
710                 drv_set_tim(local, &sta->sta, indicate_tim);
711                 local->tim_in_locked_section = false;
712         }
713
714 out_unlock:
715         spin_unlock_bh(&local->tim_lock);
716 }
717
718 static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb)
719 {
720         struct ieee80211_tx_info *info;
721         int timeout;
722
723         if (!skb)
724                 return false;
725
726         info = IEEE80211_SKB_CB(skb);
727
728         /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */
729         timeout = (sta->listen_interval *
730                    sta->sdata->vif.bss_conf.beacon_int *
731                    32 / 15625) * HZ;
732         if (timeout < STA_TX_BUFFER_EXPIRE)
733                 timeout = STA_TX_BUFFER_EXPIRE;
734         return time_after(jiffies, info->control.jiffies + timeout);
735 }
736
737
738 static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local,
739                                                 struct sta_info *sta, int ac)
740 {
741         unsigned long flags;
742         struct sk_buff *skb;
743
744         /*
745          * First check for frames that should expire on the filtered
746          * queue. Frames here were rejected by the driver and are on
747          * a separate queue to avoid reordering with normal PS-buffered
748          * frames. They also aren't accounted for right now in the
749          * total_ps_buffered counter.
750          */
751         for (;;) {
752                 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
753                 skb = skb_peek(&sta->tx_filtered[ac]);
754                 if (sta_info_buffer_expired(sta, skb))
755                         skb = __skb_dequeue(&sta->tx_filtered[ac]);
756                 else
757                         skb = NULL;
758                 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
759
760                 /*
761                  * Frames are queued in order, so if this one
762                  * hasn't expired yet we can stop testing. If
763                  * we actually reached the end of the queue we
764                  * also need to stop, of course.
765                  */
766                 if (!skb)
767                         break;
768                 ieee80211_free_txskb(&local->hw, skb);
769         }
770
771         /*
772          * Now also check the normal PS-buffered queue, this will
773          * only find something if the filtered queue was emptied
774          * since the filtered frames are all before the normal PS
775          * buffered frames.
776          */
777         for (;;) {
778                 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
779                 skb = skb_peek(&sta->ps_tx_buf[ac]);
780                 if (sta_info_buffer_expired(sta, skb))
781                         skb = __skb_dequeue(&sta->ps_tx_buf[ac]);
782                 else
783                         skb = NULL;
784                 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
785
786                 /*
787                  * frames are queued in order, so if this one
788                  * hasn't expired yet (or we reached the end of
789                  * the queue) we can stop testing
790                  */
791                 if (!skb)
792                         break;
793
794                 local->total_ps_buffered--;
795                 ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n",
796                        sta->sta.addr);
797                 ieee80211_free_txskb(&local->hw, skb);
798         }
799
800         /*
801          * Finally, recalculate the TIM bit for this station -- it might
802          * now be clear because the station was too slow to retrieve its
803          * frames.
804          */
805         sta_info_recalc_tim(sta);
806
807         /*
808          * Return whether there are any frames still buffered, this is
809          * used to check whether the cleanup timer still needs to run,
810          * if there are no frames we don't need to rearm the timer.
811          */
812         return !(skb_queue_empty(&sta->ps_tx_buf[ac]) &&
813                  skb_queue_empty(&sta->tx_filtered[ac]));
814 }
815
816 static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local,
817                                              struct sta_info *sta)
818 {
819         bool have_buffered = false;
820         int ac;
821
822         /* This is only necessary for stations on BSS/MBSS interfaces */
823         if (!sta->sdata->bss &&
824             !ieee80211_vif_is_mesh(&sta->sdata->vif))
825                 return false;
826
827         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
828                 have_buffered |=
829                         sta_info_cleanup_expire_buffered_ac(local, sta, ac);
830
831         return have_buffered;
832 }
833
834 static int __must_check __sta_info_destroy_part1(struct sta_info *sta)
835 {
836         struct ieee80211_local *local;
837         struct ieee80211_sub_if_data *sdata;
838         int ret;
839
840         might_sleep();
841
842         if (!sta)
843                 return -ENOENT;
844
845         local = sta->local;
846         sdata = sta->sdata;
847
848         lockdep_assert_held(&local->sta_mtx);
849
850         /*
851          * Before removing the station from the driver and
852          * rate control, it might still start new aggregation
853          * sessions -- block that to make sure the tear-down
854          * will be sufficient.
855          */
856         set_sta_flag(sta, WLAN_STA_BLOCK_BA);
857         ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA);
858
859         ret = sta_info_hash_del(local, sta);
860         if (WARN_ON(ret))
861                 return ret;
862
863         list_del_rcu(&sta->list);
864
865         drv_sta_pre_rcu_remove(local, sta->sdata, sta);
866
867         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
868             rcu_access_pointer(sdata->u.vlan.sta) == sta)
869                 RCU_INIT_POINTER(sdata->u.vlan.sta, NULL);
870
871         return 0;
872 }
873
874 static void __sta_info_destroy_part2(struct sta_info *sta)
875 {
876         struct ieee80211_local *local = sta->local;
877         struct ieee80211_sub_if_data *sdata = sta->sdata;
878         int ret;
879
880         /*
881          * NOTE: This assumes at least synchronize_net() was done
882          *       after _part1 and before _part2!
883          */
884
885         might_sleep();
886         lockdep_assert_held(&local->sta_mtx);
887
888         /* now keys can no longer be reached */
889         ieee80211_free_sta_keys(local, sta);
890
891         sta->dead = true;
892
893         local->num_sta--;
894         local->sta_generation++;
895
896         while (sta->sta_state > IEEE80211_STA_NONE) {
897                 ret = sta_info_move_state(sta, sta->sta_state - 1);
898                 if (ret) {
899                         WARN_ON_ONCE(1);
900                         break;
901                 }
902         }
903
904         if (sta->uploaded) {
905                 ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE,
906                                     IEEE80211_STA_NOTEXIST);
907                 WARN_ON_ONCE(ret != 0);
908         }
909
910         sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr);
911
912         cfg80211_del_sta(sdata->dev, sta->sta.addr, GFP_KERNEL);
913
914         rate_control_remove_sta_debugfs(sta);
915         ieee80211_sta_debugfs_remove(sta);
916         ieee80211_recalc_min_chandef(sdata);
917
918         cleanup_single_sta(sta);
919 }
920
921 int __must_check __sta_info_destroy(struct sta_info *sta)
922 {
923         int err = __sta_info_destroy_part1(sta);
924
925         if (err)
926                 return err;
927
928         synchronize_net();
929
930         __sta_info_destroy_part2(sta);
931
932         return 0;
933 }
934
935 int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr)
936 {
937         struct sta_info *sta;
938         int ret;
939
940         mutex_lock(&sdata->local->sta_mtx);
941         sta = sta_info_get(sdata, addr);
942         ret = __sta_info_destroy(sta);
943         mutex_unlock(&sdata->local->sta_mtx);
944
945         return ret;
946 }
947
948 int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
949                               const u8 *addr)
950 {
951         struct sta_info *sta;
952         int ret;
953
954         mutex_lock(&sdata->local->sta_mtx);
955         sta = sta_info_get_bss(sdata, addr);
956         ret = __sta_info_destroy(sta);
957         mutex_unlock(&sdata->local->sta_mtx);
958
959         return ret;
960 }
961
962 static void sta_info_cleanup(unsigned long data)
963 {
964         struct ieee80211_local *local = (struct ieee80211_local *) data;
965         struct sta_info *sta;
966         bool timer_needed = false;
967
968         rcu_read_lock();
969         list_for_each_entry_rcu(sta, &local->sta_list, list)
970                 if (sta_info_cleanup_expire_buffered(local, sta))
971                         timer_needed = true;
972         rcu_read_unlock();
973
974         if (local->quiescing)
975                 return;
976
977         if (!timer_needed)
978                 return;
979
980         mod_timer(&local->sta_cleanup,
981                   round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
982 }
983
984 void sta_info_init(struct ieee80211_local *local)
985 {
986         spin_lock_init(&local->tim_lock);
987         mutex_init(&local->sta_mtx);
988         INIT_LIST_HEAD(&local->sta_list);
989
990         setup_timer(&local->sta_cleanup, sta_info_cleanup,
991                     (unsigned long)local);
992 }
993
994 void sta_info_stop(struct ieee80211_local *local)
995 {
996         del_timer_sync(&local->sta_cleanup);
997 }
998
999
1000 int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans)
1001 {
1002         struct ieee80211_local *local = sdata->local;
1003         struct sta_info *sta, *tmp;
1004         LIST_HEAD(free_list);
1005         int ret = 0;
1006
1007         might_sleep();
1008
1009         WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP);
1010         WARN_ON(vlans && !sdata->bss);
1011
1012         mutex_lock(&local->sta_mtx);
1013         list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1014                 if (sdata == sta->sdata ||
1015                     (vlans && sdata->bss == sta->sdata->bss)) {
1016                         if (!WARN_ON(__sta_info_destroy_part1(sta)))
1017                                 list_add(&sta->free_list, &free_list);
1018                         ret++;
1019                 }
1020         }
1021
1022         if (!list_empty(&free_list)) {
1023                 synchronize_net();
1024                 list_for_each_entry_safe(sta, tmp, &free_list, free_list)
1025                         __sta_info_destroy_part2(sta);
1026         }
1027         mutex_unlock(&local->sta_mtx);
1028
1029         return ret;
1030 }
1031
1032 void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1033                           unsigned long exp_time)
1034 {
1035         struct ieee80211_local *local = sdata->local;
1036         struct sta_info *sta, *tmp;
1037
1038         mutex_lock(&local->sta_mtx);
1039
1040         list_for_each_entry_safe(sta, tmp, &local->sta_list, list) {
1041                 if (sdata != sta->sdata)
1042                         continue;
1043
1044                 if (time_after(jiffies, sta->last_rx + exp_time)) {
1045                         sta_dbg(sta->sdata, "expiring inactive STA %pM\n",
1046                                 sta->sta.addr);
1047
1048                         if (ieee80211_vif_is_mesh(&sdata->vif) &&
1049                             test_sta_flag(sta, WLAN_STA_PS_STA))
1050                                 atomic_dec(&sdata->u.mesh.ps.num_sta_ps);
1051
1052                         WARN_ON(__sta_info_destroy(sta));
1053                 }
1054         }
1055
1056         mutex_unlock(&local->sta_mtx);
1057 }
1058
1059 struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1060                                                const u8 *addr,
1061                                                const u8 *localaddr)
1062 {
1063         struct sta_info *sta, *nxt;
1064
1065         /*
1066          * Just return a random station if localaddr is NULL
1067          * ... first in list.
1068          */
1069         for_each_sta_info(hw_to_local(hw), addr, sta, nxt) {
1070                 if (localaddr &&
1071                     !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1072                         continue;
1073                 if (!sta->uploaded)
1074                         return NULL;
1075                 return &sta->sta;
1076         }
1077
1078         return NULL;
1079 }
1080 EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr);
1081
1082 struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif,
1083                                          const u8 *addr)
1084 {
1085         struct sta_info *sta;
1086
1087         if (!vif)
1088                 return NULL;
1089
1090         sta = sta_info_get_bss(vif_to_sdata(vif), addr);
1091         if (!sta)
1092                 return NULL;
1093
1094         if (!sta->uploaded)
1095                 return NULL;
1096
1097         return &sta->sta;
1098 }
1099 EXPORT_SYMBOL(ieee80211_find_sta);
1100
1101 /* powersave support code */
1102 void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1103 {
1104         struct ieee80211_sub_if_data *sdata = sta->sdata;
1105         struct ieee80211_local *local = sdata->local;
1106         struct sk_buff_head pending;
1107         int filtered = 0, buffered = 0, ac;
1108         unsigned long flags;
1109         struct ps_data *ps;
1110
1111         if (sdata->vif.type == NL80211_IFTYPE_AP ||
1112             sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1113                 ps = &sdata->bss->ps;
1114         else if (ieee80211_vif_is_mesh(&sdata->vif))
1115                 ps = &sdata->u.mesh.ps;
1116         else
1117                 return;
1118
1119         clear_sta_flag(sta, WLAN_STA_SP);
1120
1121         BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1122         sta->driver_buffered_tids = 0;
1123
1124         if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1125                 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1126
1127         skb_queue_head_init(&pending);
1128
1129         /* sync with ieee80211_tx_h_unicast_ps_buf */
1130         spin_lock(&sta->ps_lock);
1131         /* Send all buffered frames to the station */
1132         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1133                 int count = skb_queue_len(&pending), tmp;
1134
1135                 spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags);
1136                 skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending);
1137                 spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags);
1138                 tmp = skb_queue_len(&pending);
1139                 filtered += tmp - count;
1140                 count = tmp;
1141
1142                 spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags);
1143                 skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending);
1144                 spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags);
1145                 tmp = skb_queue_len(&pending);
1146                 buffered += tmp - count;
1147         }
1148
1149         ieee80211_add_pending_skbs(local, &pending);
1150         clear_sta_flag(sta, WLAN_STA_PS_DRIVER);
1151         clear_sta_flag(sta, WLAN_STA_PS_STA);
1152         spin_unlock(&sta->ps_lock);
1153
1154         atomic_dec(&ps->num_sta_ps);
1155
1156         /* This station just woke up and isn't aware of our SMPS state */
1157         if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1158             !ieee80211_smps_is_restrictive(sta->known_smps_mode,
1159                                            sdata->smps_mode) &&
1160             sta->known_smps_mode != sdata->bss->req_smps &&
1161             sta_info_tx_streams(sta) != 1) {
1162                 ht_dbg(sdata,
1163                        "%pM just woke up and MIMO capable - update SMPS\n",
1164                        sta->sta.addr);
1165                 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1166                                            sta->sta.addr,
1167                                            sdata->vif.bss_conf.bssid);
1168         }
1169
1170         local->total_ps_buffered -= buffered;
1171
1172         sta_info_recalc_tim(sta);
1173
1174         ps_dbg(sdata,
1175                "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n",
1176                sta->sta.addr, sta->sta.aid, filtered, buffered);
1177 }
1178
1179 static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1180                                          struct sta_info *sta, int tid,
1181                                          enum ieee80211_frame_release_type reason,
1182                                          bool call_driver)
1183 {
1184         struct ieee80211_local *local = sdata->local;
1185         struct ieee80211_qos_hdr *nullfunc;
1186         struct sk_buff *skb;
1187         int size = sizeof(*nullfunc);
1188         __le16 fc;
1189         bool qos = test_sta_flag(sta, WLAN_STA_WME);
1190         struct ieee80211_tx_info *info;
1191         struct ieee80211_chanctx_conf *chanctx_conf;
1192
1193         if (qos) {
1194                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1195                                  IEEE80211_STYPE_QOS_NULLFUNC |
1196                                  IEEE80211_FCTL_FROMDS);
1197         } else {
1198                 size -= 2;
1199                 fc = cpu_to_le16(IEEE80211_FTYPE_DATA |
1200                                  IEEE80211_STYPE_NULLFUNC |
1201                                  IEEE80211_FCTL_FROMDS);
1202         }
1203
1204         skb = dev_alloc_skb(local->hw.extra_tx_headroom + size);
1205         if (!skb)
1206                 return;
1207
1208         skb_reserve(skb, local->hw.extra_tx_headroom);
1209
1210         nullfunc = (void *) skb_put(skb, size);
1211         nullfunc->frame_control = fc;
1212         nullfunc->duration_id = 0;
1213         memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN);
1214         memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN);
1215         memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN);
1216         nullfunc->seq_ctrl = 0;
1217
1218         skb->priority = tid;
1219         skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]);
1220         if (qos) {
1221                 nullfunc->qos_ctrl = cpu_to_le16(tid);
1222
1223                 if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
1224                         nullfunc->qos_ctrl |=
1225                                 cpu_to_le16(IEEE80211_QOS_CTL_EOSP);
1226         }
1227
1228         info = IEEE80211_SKB_CB(skb);
1229
1230         /*
1231          * Tell TX path to send this frame even though the
1232          * STA may still remain is PS mode after this frame
1233          * exchange. Also set EOSP to indicate this packet
1234          * ends the poll/service period.
1235          */
1236         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1237                        IEEE80211_TX_CTL_PS_RESPONSE |
1238                        IEEE80211_TX_STATUS_EOSP |
1239                        IEEE80211_TX_CTL_REQ_TX_STATUS;
1240
1241         if (call_driver)
1242                 drv_allow_buffered_frames(local, sta, BIT(tid), 1,
1243                                           reason, false);
1244
1245         skb->dev = sdata->dev;
1246
1247         rcu_read_lock();
1248         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1249         if (WARN_ON(!chanctx_conf)) {
1250                 rcu_read_unlock();
1251                 kfree_skb(skb);
1252                 return;
1253         }
1254
1255         ieee80211_xmit(sdata, skb, chanctx_conf->def.chan->band);
1256         rcu_read_unlock();
1257 }
1258
1259 static int find_highest_prio_tid(unsigned long tids)
1260 {
1261         /* lower 3 TIDs aren't ordered perfectly */
1262         if (tids & 0xF8)
1263                 return fls(tids) - 1;
1264         /* TID 0 is BE just like TID 3 */
1265         if (tids & BIT(0))
1266                 return 0;
1267         return fls(tids) - 1;
1268 }
1269
1270 static void
1271 ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1272                                   int n_frames, u8 ignored_acs,
1273                                   enum ieee80211_frame_release_type reason)
1274 {
1275         struct ieee80211_sub_if_data *sdata = sta->sdata;
1276         struct ieee80211_local *local = sdata->local;
1277         bool more_data = false;
1278         int ac;
1279         unsigned long driver_release_tids = 0;
1280         struct sk_buff_head frames;
1281
1282         /* Service or PS-Poll period starts */
1283         set_sta_flag(sta, WLAN_STA_SP);
1284
1285         __skb_queue_head_init(&frames);
1286
1287         /* Get response frame(s) and more data bit for the last one. */
1288         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1289                 unsigned long tids;
1290
1291                 if (ignored_acs & BIT(ac))
1292                         continue;
1293
1294                 tids = ieee80211_tids_for_ac(ac);
1295
1296                 /* if we already have frames from software, then we can't also
1297                  * release from hardware queues
1298                  */
1299                 if (skb_queue_empty(&frames))
1300                         driver_release_tids |= sta->driver_buffered_tids & tids;
1301
1302                 if (driver_release_tids) {
1303                         /* If the driver has data on more than one TID then
1304                          * certainly there's more data if we release just a
1305                          * single frame now (from a single TID). This will
1306                          * only happen for PS-Poll.
1307                          */
1308                         if (reason == IEEE80211_FRAME_RELEASE_PSPOLL &&
1309                             hweight16(driver_release_tids) > 1) {
1310                                 more_data = true;
1311                                 driver_release_tids =
1312                                         BIT(find_highest_prio_tid(
1313                                                 driver_release_tids));
1314                                 break;
1315                         }
1316                 } else {
1317                         struct sk_buff *skb;
1318
1319                         while (n_frames > 0) {
1320                                 skb = skb_dequeue(&sta->tx_filtered[ac]);
1321                                 if (!skb) {
1322                                         skb = skb_dequeue(
1323                                                 &sta->ps_tx_buf[ac]);
1324                                         if (skb)
1325                                                 local->total_ps_buffered--;
1326                                 }
1327                                 if (!skb)
1328                                         break;
1329                                 n_frames--;
1330                                 __skb_queue_tail(&frames, skb);
1331                         }
1332                 }
1333
1334                 /* If we have more frames buffered on this AC, then set the
1335                  * more-data bit and abort the loop since we can't send more
1336                  * data from other ACs before the buffered frames from this.
1337                  */
1338                 if (!skb_queue_empty(&sta->tx_filtered[ac]) ||
1339                     !skb_queue_empty(&sta->ps_tx_buf[ac])) {
1340                         more_data = true;
1341                         break;
1342                 }
1343         }
1344
1345         if (skb_queue_empty(&frames) && !driver_release_tids) {
1346                 int tid;
1347
1348                 /*
1349                  * For PS-Poll, this can only happen due to a race condition
1350                  * when we set the TIM bit and the station notices it, but
1351                  * before it can poll for the frame we expire it.
1352                  *
1353                  * For uAPSD, this is said in the standard (11.2.1.5 h):
1354                  *      At each unscheduled SP for a non-AP STA, the AP shall
1355                  *      attempt to transmit at least one MSDU or MMPDU, but no
1356                  *      more than the value specified in the Max SP Length field
1357                  *      in the QoS Capability element from delivery-enabled ACs,
1358                  *      that are destined for the non-AP STA.
1359                  *
1360                  * Since we have no other MSDU/MMPDU, transmit a QoS null frame.
1361                  */
1362
1363                 /* This will evaluate to 1, 3, 5 or 7. */
1364                 tid = 7 - ((ffs(~ignored_acs) - 1) << 1);
1365
1366                 ieee80211_send_null_response(sdata, sta, tid, reason, true);
1367         } else if (!driver_release_tids) {
1368                 struct sk_buff_head pending;
1369                 struct sk_buff *skb;
1370                 int num = 0;
1371                 u16 tids = 0;
1372                 bool need_null = false;
1373
1374                 skb_queue_head_init(&pending);
1375
1376                 while ((skb = __skb_dequeue(&frames))) {
1377                         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1378                         struct ieee80211_hdr *hdr = (void *) skb->data;
1379                         u8 *qoshdr = NULL;
1380
1381                         num++;
1382
1383                         /*
1384                          * Tell TX path to send this frame even though the
1385                          * STA may still remain is PS mode after this frame
1386                          * exchange.
1387                          */
1388                         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER |
1389                                        IEEE80211_TX_CTL_PS_RESPONSE;
1390
1391                         /*
1392                          * Use MoreData flag to indicate whether there are
1393                          * more buffered frames for this STA
1394                          */
1395                         if (more_data || !skb_queue_empty(&frames))
1396                                 hdr->frame_control |=
1397                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1398                         else
1399                                 hdr->frame_control &=
1400                                         cpu_to_le16(~IEEE80211_FCTL_MOREDATA);
1401
1402                         if (ieee80211_is_data_qos(hdr->frame_control) ||
1403                             ieee80211_is_qos_nullfunc(hdr->frame_control))
1404                                 qoshdr = ieee80211_get_qos_ctl(hdr);
1405
1406                         tids |= BIT(skb->priority);
1407
1408                         __skb_queue_tail(&pending, skb);
1409
1410                         /* end service period after last frame or add one */
1411                         if (!skb_queue_empty(&frames))
1412                                 continue;
1413
1414                         if (reason != IEEE80211_FRAME_RELEASE_UAPSD) {
1415                                 /* for PS-Poll, there's only one frame */
1416                                 info->flags |= IEEE80211_TX_STATUS_EOSP |
1417                                                IEEE80211_TX_CTL_REQ_TX_STATUS;
1418                                 break;
1419                         }
1420
1421                         /* For uAPSD, things are a bit more complicated. If the
1422                          * last frame has a QoS header (i.e. is a QoS-data or
1423                          * QoS-nulldata frame) then just set the EOSP bit there
1424                          * and be done.
1425                          * If the frame doesn't have a QoS header (which means
1426                          * it should be a bufferable MMPDU) then we can't set
1427                          * the EOSP bit in the QoS header; add a QoS-nulldata
1428                          * frame to the list to send it after the MMPDU.
1429                          *
1430                          * Note that this code is only in the mac80211-release
1431                          * code path, we assume that the driver will not buffer
1432                          * anything but QoS-data frames, or if it does, will
1433                          * create the QoS-nulldata frame by itself if needed.
1434                          *
1435                          * Cf. 802.11-2012 10.2.1.10 (c).
1436                          */
1437                         if (qoshdr) {
1438                                 *qoshdr |= IEEE80211_QOS_CTL_EOSP;
1439
1440                                 info->flags |= IEEE80211_TX_STATUS_EOSP |
1441                                                IEEE80211_TX_CTL_REQ_TX_STATUS;
1442                         } else {
1443                                 /* The standard isn't completely clear on this
1444                                  * as it says the more-data bit should be set
1445                                  * if there are more BUs. The QoS-Null frame
1446                                  * we're about to send isn't buffered yet, we
1447                                  * only create it below, but let's pretend it
1448                                  * was buffered just in case some clients only
1449                                  * expect more-data=0 when eosp=1.
1450                                  */
1451                                 hdr->frame_control |=
1452                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1453                                 need_null = true;
1454                                 num++;
1455                         }
1456                         break;
1457                 }
1458
1459                 drv_allow_buffered_frames(local, sta, tids, num,
1460                                           reason, more_data);
1461
1462                 ieee80211_add_pending_skbs(local, &pending);
1463
1464                 if (need_null)
1465                         ieee80211_send_null_response(
1466                                 sdata, sta, find_highest_prio_tid(tids),
1467                                 reason, false);
1468
1469                 sta_info_recalc_tim(sta);
1470         } else {
1471                 /*
1472                  * We need to release a frame that is buffered somewhere in the
1473                  * driver ... it'll have to handle that.
1474                  * Note that the driver also has to check the number of frames
1475                  * on the TIDs we're releasing from - if there are more than
1476                  * n_frames it has to set the more-data bit (if we didn't ask
1477                  * it to set it anyway due to other buffered frames); if there
1478                  * are fewer than n_frames it has to make sure to adjust that
1479                  * to allow the service period to end properly.
1480                  */
1481                 drv_release_buffered_frames(local, sta, driver_release_tids,
1482                                             n_frames, reason, more_data);
1483
1484                 /*
1485                  * Note that we don't recalculate the TIM bit here as it would
1486                  * most likely have no effect at all unless the driver told us
1487                  * that the TID(s) became empty before returning here from the
1488                  * release function.
1489                  * Either way, however, when the driver tells us that the TID(s)
1490                  * became empty we'll do the TIM recalculation.
1491                  */
1492         }
1493 }
1494
1495 void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta)
1496 {
1497         u8 ignore_for_response = sta->sta.uapsd_queues;
1498
1499         /*
1500          * If all ACs are delivery-enabled then we should reply
1501          * from any of them, if only some are enabled we reply
1502          * only from the non-enabled ones.
1503          */
1504         if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1)
1505                 ignore_for_response = 0;
1506
1507         ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response,
1508                                           IEEE80211_FRAME_RELEASE_PSPOLL);
1509 }
1510
1511 void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta)
1512 {
1513         int n_frames = sta->sta.max_sp;
1514         u8 delivery_enabled = sta->sta.uapsd_queues;
1515
1516         /*
1517          * If we ever grow support for TSPEC this might happen if
1518          * the TSPEC update from hostapd comes in between a trigger
1519          * frame setting WLAN_STA_UAPSD in the RX path and this
1520          * actually getting called.
1521          */
1522         if (!delivery_enabled)
1523                 return;
1524
1525         switch (sta->sta.max_sp) {
1526         case 1:
1527                 n_frames = 2;
1528                 break;
1529         case 2:
1530                 n_frames = 4;
1531                 break;
1532         case 3:
1533                 n_frames = 6;
1534                 break;
1535         case 0:
1536                 /* XXX: what is a good value? */
1537                 n_frames = 8;
1538                 break;
1539         }
1540
1541         ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled,
1542                                           IEEE80211_FRAME_RELEASE_UAPSD);
1543 }
1544
1545 void ieee80211_sta_block_awake(struct ieee80211_hw *hw,
1546                                struct ieee80211_sta *pubsta, bool block)
1547 {
1548         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1549
1550         trace_api_sta_block_awake(sta->local, pubsta, block);
1551
1552         if (block)
1553                 set_sta_flag(sta, WLAN_STA_PS_DRIVER);
1554         else if (test_sta_flag(sta, WLAN_STA_PS_DRIVER))
1555                 ieee80211_queue_work(hw, &sta->drv_unblock_wk);
1556 }
1557 EXPORT_SYMBOL(ieee80211_sta_block_awake);
1558
1559 void ieee80211_sta_eosp(struct ieee80211_sta *pubsta)
1560 {
1561         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1562         struct ieee80211_local *local = sta->local;
1563
1564         trace_api_eosp(local, pubsta);
1565
1566         clear_sta_flag(sta, WLAN_STA_SP);
1567 }
1568 EXPORT_SYMBOL(ieee80211_sta_eosp);
1569
1570 void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta,
1571                                 u8 tid, bool buffered)
1572 {
1573         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
1574
1575         if (WARN_ON(tid >= IEEE80211_NUM_TIDS))
1576                 return;
1577
1578         trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered);
1579
1580         if (buffered)
1581                 set_bit(tid, &sta->driver_buffered_tids);
1582         else
1583                 clear_bit(tid, &sta->driver_buffered_tids);
1584
1585         sta_info_recalc_tim(sta);
1586 }
1587 EXPORT_SYMBOL(ieee80211_sta_set_buffered);
1588
1589 int sta_info_move_state(struct sta_info *sta,
1590                         enum ieee80211_sta_state new_state)
1591 {
1592         might_sleep();
1593
1594         if (sta->sta_state == new_state)
1595                 return 0;
1596
1597         /* check allowed transitions first */
1598
1599         switch (new_state) {
1600         case IEEE80211_STA_NONE:
1601                 if (sta->sta_state != IEEE80211_STA_AUTH)
1602                         return -EINVAL;
1603                 break;
1604         case IEEE80211_STA_AUTH:
1605                 if (sta->sta_state != IEEE80211_STA_NONE &&
1606                     sta->sta_state != IEEE80211_STA_ASSOC)
1607                         return -EINVAL;
1608                 break;
1609         case IEEE80211_STA_ASSOC:
1610                 if (sta->sta_state != IEEE80211_STA_AUTH &&
1611                     sta->sta_state != IEEE80211_STA_AUTHORIZED)
1612                         return -EINVAL;
1613                 break;
1614         case IEEE80211_STA_AUTHORIZED:
1615                 if (sta->sta_state != IEEE80211_STA_ASSOC)
1616                         return -EINVAL;
1617                 break;
1618         default:
1619                 WARN(1, "invalid state %d", new_state);
1620                 return -EINVAL;
1621         }
1622
1623         sta_dbg(sta->sdata, "moving STA %pM to state %d\n",
1624                 sta->sta.addr, new_state);
1625
1626         /*
1627          * notify the driver before the actual changes so it can
1628          * fail the transition
1629          */
1630         if (test_sta_flag(sta, WLAN_STA_INSERTED)) {
1631                 int err = drv_sta_state(sta->local, sta->sdata, sta,
1632                                         sta->sta_state, new_state);
1633                 if (err)
1634                         return err;
1635         }
1636
1637         /* reflect the change in all state variables */
1638
1639         switch (new_state) {
1640         case IEEE80211_STA_NONE:
1641                 if (sta->sta_state == IEEE80211_STA_AUTH)
1642                         clear_bit(WLAN_STA_AUTH, &sta->_flags);
1643                 break;
1644         case IEEE80211_STA_AUTH:
1645                 if (sta->sta_state == IEEE80211_STA_NONE)
1646                         set_bit(WLAN_STA_AUTH, &sta->_flags);
1647                 else if (sta->sta_state == IEEE80211_STA_ASSOC)
1648                         clear_bit(WLAN_STA_ASSOC, &sta->_flags);
1649                 break;
1650         case IEEE80211_STA_ASSOC:
1651                 if (sta->sta_state == IEEE80211_STA_AUTH) {
1652                         set_bit(WLAN_STA_ASSOC, &sta->_flags);
1653                 } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) {
1654                         if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1655                             (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1656                              !sta->sdata->u.vlan.sta))
1657                                 atomic_dec(&sta->sdata->bss->num_mcast_sta);
1658                         clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1659                 }
1660                 break;
1661         case IEEE80211_STA_AUTHORIZED:
1662                 if (sta->sta_state == IEEE80211_STA_ASSOC) {
1663                         if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1664                             (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN &&
1665                              !sta->sdata->u.vlan.sta))
1666                                 atomic_inc(&sta->sdata->bss->num_mcast_sta);
1667                         set_bit(WLAN_STA_AUTHORIZED, &sta->_flags);
1668                 }
1669                 if (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1670                     sta->sdata->vif.type == NL80211_IFTYPE_AP)
1671                         cfg80211_send_layer2_update(sta->sdata->dev,
1672                                                     sta->sta.addr);
1673                 break;
1674         default:
1675                 break;
1676         }
1677
1678         sta->sta_state = new_state;
1679
1680         return 0;
1681 }
1682
1683 u8 sta_info_tx_streams(struct sta_info *sta)
1684 {
1685         struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1686         u8 rx_streams;
1687
1688         if (!sta->sta.ht_cap.ht_supported)
1689                 return 1;
1690
1691         if (sta->sta.vht_cap.vht_supported) {
1692                 int i;
1693                 u16 tx_mcs_map =
1694                         le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1695
1696                 for (i = 7; i >= 0; i--)
1697                         if ((tx_mcs_map & (0x3 << (i * 2))) !=
1698                             IEEE80211_VHT_MCS_NOT_SUPPORTED)
1699                                 return i + 1;
1700         }
1701
1702         if (ht_cap->mcs.rx_mask[3])
1703                 rx_streams = 4;
1704         else if (ht_cap->mcs.rx_mask[2])
1705                 rx_streams = 3;
1706         else if (ht_cap->mcs.rx_mask[1])
1707                 rx_streams = 2;
1708         else
1709                 rx_streams = 1;
1710
1711         if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1712                 return rx_streams;
1713
1714         return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1715                         >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1716 }