Commit | Line | Data |
---|---|---|
f0706e82 JB |
1 | /* |
2 | * Copyright 2002-2005, Instant802 Networks, Inc. | |
3 | * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz> | |
d98ad83e | 4 | * Copyright 2013-2014 Intel Mobile Communications GmbH |
f59374eb | 5 | * Copyright (C) 2015 - 2016 Intel Deutschland GmbH |
f0706e82 JB |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/init.h> | |
888d04df | 14 | #include <linux/etherdevice.h> |
f0706e82 JB |
15 | #include <linux/netdevice.h> |
16 | #include <linux/types.h> | |
17 | #include <linux/slab.h> | |
18 | #include <linux/skbuff.h> | |
19 | #include <linux/if_arp.h> | |
0d174406 | 20 | #include <linux/timer.h> |
d0709a65 | 21 | #include <linux/rtnetlink.h> |
f0706e82 JB |
22 | |
23 | #include <net/mac80211.h> | |
24 | #include "ieee80211_i.h" | |
24487981 | 25 | #include "driver-ops.h" |
2c8dccc7 | 26 | #include "rate.h" |
f0706e82 | 27 | #include "sta_info.h" |
e9f207f0 | 28 | #include "debugfs_sta.h" |
ee385855 | 29 | #include "mesh.h" |
ce662b44 | 30 | #include "wme.h" |
f0706e82 | 31 | |
d0709a65 JB |
32 | /** |
33 | * DOC: STA information lifetime rules | |
34 | * | |
35 | * STA info structures (&struct sta_info) are managed in a hash table | |
36 | * for faster lookup and a list for iteration. They are managed using | |
37 | * RCU, i.e. access to the list and hash table is protected by RCU. | |
38 | * | |
34e89507 JB |
39 | * Upon allocating a STA info structure with sta_info_alloc(), the caller |
40 | * owns that structure. It must then insert it into the hash table using | |
41 | * either sta_info_insert() or sta_info_insert_rcu(); only in the latter | |
42 | * case (which acquires an rcu read section but must not be called from | |
43 | * within one) will the pointer still be valid after the call. Note that | |
44 | * the caller may not do much with the STA info before inserting it, in | |
45 | * particular, it may not start any mesh peer link management or add | |
46 | * encryption keys. | |
93e5deb1 JB |
47 | * |
48 | * When the insertion fails (sta_info_insert()) returns non-zero), the | |
49 | * structure will have been freed by sta_info_insert()! | |
d0709a65 | 50 | * |
34e89507 | 51 | * Station entries are added by mac80211 when you establish a link with a |
7e189a12 LR |
52 | * peer. This means different things for the different type of interfaces |
53 | * we support. For a regular station this mean we add the AP sta when we | |
25985edc | 54 | * receive an association response from the AP. For IBSS this occurs when |
34e89507 | 55 | * get to know about a peer on the same IBSS. For WDS we add the sta for |
25985edc | 56 | * the peer immediately upon device open. When using AP mode we add stations |
34e89507 | 57 | * for each respective station upon request from userspace through nl80211. |
7e189a12 | 58 | * |
34e89507 JB |
59 | * In order to remove a STA info structure, various sta_info_destroy_*() |
60 | * calls are available. | |
d0709a65 | 61 | * |
34e89507 JB |
62 | * There is no concept of ownership on a STA entry, each structure is |
63 | * owned by the global hash table/list until it is removed. All users of | |
64 | * the structure need to be RCU protected so that the structure won't be | |
65 | * freed before they are done using it. | |
d0709a65 | 66 | */ |
f0706e82 | 67 | |
7bedd0cf JB |
68 | static const struct rhashtable_params sta_rht_params = { |
69 | .nelem_hint = 3, /* start small */ | |
caf22d31 | 70 | .automatic_shrinking = true, |
7bedd0cf | 71 | .head_offset = offsetof(struct sta_info, hash_node), |
ac100ce5 | 72 | .key_offset = offsetof(struct sta_info, addr), |
7bedd0cf JB |
73 | .key_len = ETH_ALEN, |
74 | .hashfn = sta_addr_hash, | |
ebd82b39 | 75 | .max_size = CONFIG_MAC80211_STA_HASH_MAX_SIZE, |
7bedd0cf JB |
76 | }; |
77 | ||
4d33960b | 78 | /* Caller must hold local->sta_mtx */ |
be8755e1 MW |
79 | static int sta_info_hash_del(struct ieee80211_local *local, |
80 | struct sta_info *sta) | |
f0706e82 | 81 | { |
7bedd0cf JB |
82 | return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node, |
83 | sta_rht_params); | |
f0706e82 JB |
84 | } |
85 | ||
5108ca82 | 86 | static void __cleanup_single_sta(struct sta_info *sta) |
b22cfcfc | 87 | { |
b22cfcfc EP |
88 | int ac, i; |
89 | struct tid_ampdu_tx *tid_tx; | |
90 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
91 | struct ieee80211_local *local = sdata->local; | |
d012a605 | 92 | struct ps_data *ps; |
b22cfcfc | 93 | |
e3685e03 | 94 | if (test_sta_flag(sta, WLAN_STA_PS_STA) || |
5ac2e350 JB |
95 | test_sta_flag(sta, WLAN_STA_PS_DRIVER) || |
96 | test_sta_flag(sta, WLAN_STA_PS_DELIVER)) { | |
d012a605 MP |
97 | if (sta->sdata->vif.type == NL80211_IFTYPE_AP || |
98 | sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) | |
99 | ps = &sdata->bss->ps; | |
3f52b7e3 MP |
100 | else if (ieee80211_vif_is_mesh(&sdata->vif)) |
101 | ps = &sdata->u.mesh.ps; | |
d012a605 MP |
102 | else |
103 | return; | |
b22cfcfc EP |
104 | |
105 | clear_sta_flag(sta, WLAN_STA_PS_STA); | |
e3685e03 | 106 | clear_sta_flag(sta, WLAN_STA_PS_DRIVER); |
5ac2e350 | 107 | clear_sta_flag(sta, WLAN_STA_PS_DELIVER); |
b22cfcfc | 108 | |
d012a605 | 109 | atomic_dec(&ps->num_sta_ps); |
b22cfcfc EP |
110 | } |
111 | ||
ba8c3d6f FF |
112 | if (sta->sta.txq[0]) { |
113 | for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) { | |
114 | struct txq_info *txqi = to_txq_info(sta->sta.txq[i]); | |
115 | int n = skb_queue_len(&txqi->queue); | |
116 | ||
117 | ieee80211_purge_tx_queue(&local->hw, &txqi->queue); | |
118 | atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]); | |
f2ac7e30 | 119 | txqi->byte_cnt = 0; |
ba8c3d6f FF |
120 | } |
121 | } | |
122 | ||
b22cfcfc EP |
123 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { |
124 | local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]); | |
1f98ab7f FF |
125 | ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]); |
126 | ieee80211_purge_tx_queue(&local->hw, &sta->tx_filtered[ac]); | |
b22cfcfc EP |
127 | } |
128 | ||
45b5028e TP |
129 | if (ieee80211_vif_is_mesh(&sdata->vif)) |
130 | mesh_sta_cleanup(sta); | |
b22cfcfc | 131 | |
5ac2e350 | 132 | cancel_work_sync(&sta->drv_deliver_wk); |
b22cfcfc EP |
133 | |
134 | /* | |
135 | * Destroy aggregation state here. It would be nice to wait for the | |
136 | * driver to finish aggregation stop and then clean up, but for now | |
137 | * drivers have to handle aggregation stop being requested, followed | |
138 | * directly by station destruction. | |
139 | */ | |
5a306f58 | 140 | for (i = 0; i < IEEE80211_NUM_TIDS; i++) { |
661eb381 | 141 | kfree(sta->ampdu_mlme.tid_start_tx[i]); |
b22cfcfc EP |
142 | tid_tx = rcu_dereference_raw(sta->ampdu_mlme.tid_tx[i]); |
143 | if (!tid_tx) | |
144 | continue; | |
1f98ab7f | 145 | ieee80211_purge_tx_queue(&local->hw, &tid_tx->pending); |
b22cfcfc EP |
146 | kfree(tid_tx); |
147 | } | |
5108ca82 | 148 | } |
b22cfcfc | 149 | |
5108ca82 JB |
150 | static void cleanup_single_sta(struct sta_info *sta) |
151 | { | |
152 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
153 | struct ieee80211_local *local = sdata->local; | |
154 | ||
155 | __cleanup_single_sta(sta); | |
b22cfcfc EP |
156 | sta_info_free(local, sta); |
157 | } | |
158 | ||
d0709a65 | 159 | /* protected by RCU */ |
abe60632 JB |
160 | struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata, |
161 | const u8 *addr) | |
f0706e82 | 162 | { |
abe60632 | 163 | struct ieee80211_local *local = sdata->local; |
60f4b626 JB |
164 | struct sta_info *sta; |
165 | struct rhash_head *tmp; | |
166 | const struct bucket_table *tbl; | |
f0706e82 | 167 | |
60f4b626 JB |
168 | rcu_read_lock(); |
169 | tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash); | |
f0706e82 | 170 | |
60f4b626 JB |
171 | for_each_sta_info(local, tbl, addr, sta, tmp) { |
172 | if (sta->sdata == sdata) { | |
173 | rcu_read_unlock(); | |
174 | /* this is safe as the caller must already hold | |
175 | * another rcu read section or the mutex | |
176 | */ | |
177 | return sta; | |
178 | } | |
179 | } | |
180 | rcu_read_unlock(); | |
181 | return NULL; | |
43ba7e95 JB |
182 | } |
183 | ||
0e5ded5a FF |
184 | /* |
185 | * Get sta info either from the specified interface | |
186 | * or from one of its vlans | |
187 | */ | |
188 | struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, | |
189 | const u8 *addr) | |
190 | { | |
191 | struct ieee80211_local *local = sdata->local; | |
192 | struct sta_info *sta; | |
7bedd0cf JB |
193 | struct rhash_head *tmp; |
194 | const struct bucket_table *tbl; | |
0e5ded5a | 195 | |
7bedd0cf JB |
196 | rcu_read_lock(); |
197 | tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash); | |
198 | ||
199 | for_each_sta_info(local, tbl, addr, sta, tmp) { | |
200 | if (sta->sdata == sdata || | |
201 | (sta->sdata->bss && sta->sdata->bss == sdata->bss)) { | |
202 | rcu_read_unlock(); | |
203 | /* this is safe as the caller must already hold | |
204 | * another rcu read section or the mutex | |
205 | */ | |
206 | return sta; | |
207 | } | |
0e5ded5a | 208 | } |
7bedd0cf JB |
209 | rcu_read_unlock(); |
210 | return NULL; | |
0e5ded5a FF |
211 | } |
212 | ||
3b53fde8 JB |
213 | struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, |
214 | int idx) | |
ee385855 | 215 | { |
3b53fde8 | 216 | struct ieee80211_local *local = sdata->local; |
ee385855 LCC |
217 | struct sta_info *sta; |
218 | int i = 0; | |
219 | ||
d0709a65 | 220 | list_for_each_entry_rcu(sta, &local->sta_list, list) { |
3b53fde8 | 221 | if (sdata != sta->sdata) |
2a8ca29a | 222 | continue; |
ee385855 LCC |
223 | if (i < idx) { |
224 | ++i; | |
225 | continue; | |
ee385855 | 226 | } |
2a8ca29a | 227 | return sta; |
ee385855 | 228 | } |
ee385855 LCC |
229 | |
230 | return NULL; | |
231 | } | |
f0706e82 | 232 | |
93e5deb1 | 233 | /** |
d9a7ddb0 | 234 | * sta_info_free - free STA |
93e5deb1 | 235 | * |
6ef307bc | 236 | * @local: pointer to the global information |
93e5deb1 JB |
237 | * @sta: STA info to free |
238 | * | |
239 | * This function must undo everything done by sta_info_alloc() | |
d9a7ddb0 JB |
240 | * that may happen before sta_info_insert(). It may only be |
241 | * called when sta_info_insert() has not been attempted (and | |
242 | * if that fails, the station is freed anyway.) | |
93e5deb1 | 243 | */ |
d9a7ddb0 | 244 | void sta_info_free(struct ieee80211_local *local, struct sta_info *sta) |
93e5deb1 | 245 | { |
889cbb91 | 246 | if (sta->rate_ctrl) |
af65cd96 | 247 | rate_control_free_sta(sta); |
93e5deb1 | 248 | |
bdcbd8e0 | 249 | sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr); |
93e5deb1 | 250 | |
ba8c3d6f FF |
251 | if (sta->sta.txq[0]) |
252 | kfree(to_txq_info(sta->sta.txq[0])); | |
53d04525 | 253 | kfree(rcu_dereference_raw(sta->sta.rates)); |
433f5bc1 JB |
254 | #ifdef CONFIG_MAC80211_MESH |
255 | kfree(sta->mesh); | |
256 | #endif | |
93e5deb1 JB |
257 | kfree(sta); |
258 | } | |
259 | ||
4d33960b | 260 | /* Caller must hold local->sta_mtx */ |
d0709a65 JB |
261 | static void sta_info_hash_add(struct ieee80211_local *local, |
262 | struct sta_info *sta) | |
f0706e82 | 263 | { |
7bedd0cf JB |
264 | rhashtable_insert_fast(&local->sta_hash, &sta->hash_node, |
265 | sta_rht_params); | |
f0706e82 | 266 | } |
f0706e82 | 267 | |
5ac2e350 | 268 | static void sta_deliver_ps_frames(struct work_struct *wk) |
af818581 JB |
269 | { |
270 | struct sta_info *sta; | |
271 | ||
5ac2e350 | 272 | sta = container_of(wk, struct sta_info, drv_deliver_wk); |
af818581 JB |
273 | |
274 | if (sta->dead) | |
275 | return; | |
276 | ||
5ac2e350 JB |
277 | local_bh_disable(); |
278 | if (!test_sta_flag(sta, WLAN_STA_PS_STA)) | |
af818581 | 279 | ieee80211_sta_ps_deliver_wakeup(sta); |
5ac2e350 | 280 | else if (test_and_clear_sta_flag(sta, WLAN_STA_PSPOLL)) |
af818581 | 281 | ieee80211_sta_ps_deliver_poll_response(sta); |
5ac2e350 | 282 | else if (test_and_clear_sta_flag(sta, WLAN_STA_UAPSD)) |
47086fc5 | 283 | ieee80211_sta_ps_deliver_uapsd(sta); |
5ac2e350 | 284 | local_bh_enable(); |
af818581 JB |
285 | } |
286 | ||
af65cd96 JB |
287 | static int sta_prepare_rate_control(struct ieee80211_local *local, |
288 | struct sta_info *sta, gfp_t gfp) | |
289 | { | |
30686bf7 | 290 | if (ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) |
af65cd96 JB |
291 | return 0; |
292 | ||
889cbb91 | 293 | sta->rate_ctrl = local->rate_ctrl; |
af65cd96 | 294 | sta->rate_ctrl_priv = rate_control_alloc_sta(sta->rate_ctrl, |
35c347ac | 295 | sta, gfp); |
889cbb91 | 296 | if (!sta->rate_ctrl_priv) |
af65cd96 | 297 | return -ENOMEM; |
af65cd96 JB |
298 | |
299 | return 0; | |
300 | } | |
301 | ||
73651ee6 | 302 | struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata, |
56544160 | 303 | const u8 *addr, gfp_t gfp) |
f0706e82 | 304 | { |
d0709a65 | 305 | struct ieee80211_local *local = sdata->local; |
ba8c3d6f | 306 | struct ieee80211_hw *hw = &local->hw; |
f0706e82 | 307 | struct sta_info *sta; |
16c5f15c | 308 | int i; |
f0706e82 | 309 | |
ba8c3d6f | 310 | sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp); |
f0706e82 | 311 | if (!sta) |
73651ee6 | 312 | return NULL; |
f0706e82 | 313 | |
07346f81 | 314 | spin_lock_init(&sta->lock); |
1d147bfa | 315 | spin_lock_init(&sta->ps_lock); |
5ac2e350 | 316 | INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames); |
67c282c0 | 317 | INIT_WORK(&sta->ampdu_mlme.work, ieee80211_ba_session_work); |
a93e3644 | 318 | mutex_init(&sta->ampdu_mlme.mtx); |
87f59c70 | 319 | #ifdef CONFIG_MAC80211_MESH |
433f5bc1 JB |
320 | if (ieee80211_vif_is_mesh(&sdata->vif)) { |
321 | sta->mesh = kzalloc(sizeof(*sta->mesh), gfp); | |
322 | if (!sta->mesh) | |
323 | goto free; | |
324 | spin_lock_init(&sta->mesh->plink_lock); | |
325 | if (ieee80211_vif_is_mesh(&sdata->vif) && | |
326 | !sdata->u.mesh.user_mpm) | |
327 | init_timer(&sta->mesh->plink_timer); | |
328 | sta->mesh->nonpeer_pm = NL80211_MESH_POWER_ACTIVE; | |
329 | } | |
87f59c70 | 330 | #endif |
07346f81 | 331 | |
ac100ce5 | 332 | memcpy(sta->addr, addr, ETH_ALEN); |
17741cdc | 333 | memcpy(sta->sta.addr, addr, ETH_ALEN); |
d0709a65 JB |
334 | sta->local = local; |
335 | sta->sdata = sdata; | |
e5a9f8d0 | 336 | sta->rx_stats.last_rx = jiffies; |
f0706e82 | 337 | |
71ec375c JB |
338 | sta->sta_state = IEEE80211_STA_NONE; |
339 | ||
b6da911b LK |
340 | /* Mark TID as unreserved */ |
341 | sta->reserved_tid = IEEE80211_TID_UNRESERVED; | |
342 | ||
84b00607 | 343 | sta->last_connected = ktime_get_seconds(); |
e5a9f8d0 JB |
344 | ewma_signal_init(&sta->rx_stats.avg_signal); |
345 | for (i = 0; i < ARRAY_SIZE(sta->rx_stats.chain_signal_avg); i++) | |
346 | ewma_signal_init(&sta->rx_stats.chain_signal_avg[i]); | |
541a45a1 | 347 | |
ba8c3d6f FF |
348 | if (local->ops->wake_tx_queue) { |
349 | void *txq_data; | |
350 | int size = sizeof(struct txq_info) + | |
351 | ALIGN(hw->txq_data_size, sizeof(void *)); | |
352 | ||
353 | txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp); | |
354 | if (!txq_data) | |
355 | goto free; | |
356 | ||
357 | for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) { | |
358 | struct txq_info *txq = txq_data + i * size; | |
359 | ||
360 | ieee80211_init_tx_queue(sdata, sta, txq, i); | |
361 | } | |
abfbc3af | 362 | } |
f0706e82 | 363 | |
ba8c3d6f FF |
364 | if (sta_prepare_rate_control(local, sta, gfp)) |
365 | goto free_txq; | |
366 | ||
5a306f58 | 367 | for (i = 0; i < IEEE80211_NUM_TIDS; i++) { |
a622ab72 JB |
368 | /* |
369 | * timer_to_tid must be initialized with identity mapping | |
370 | * to enable session_timer's data differentiation. See | |
371 | * sta_rx_agg_session_timer_expired for usage. | |
372 | */ | |
16c5f15c | 373 | sta->timer_to_tid[i] = i; |
16c5f15c | 374 | } |
948d887d JB |
375 | for (i = 0; i < IEEE80211_NUM_ACS; i++) { |
376 | skb_queue_head_init(&sta->ps_tx_buf[i]); | |
377 | skb_queue_head_init(&sta->tx_filtered[i]); | |
378 | } | |
73651ee6 | 379 | |
5a306f58 | 380 | for (i = 0; i < IEEE80211_NUM_TIDS; i++) |
4be929be | 381 | sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); |
cccaec98 | 382 | |
af0ed69b | 383 | sta->sta.smps_mode = IEEE80211_SMPS_OFF; |
687da132 EG |
384 | if (sdata->vif.type == NL80211_IFTYPE_AP || |
385 | sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { | |
386 | struct ieee80211_supported_band *sband = | |
ba8c3d6f | 387 | hw->wiphy->bands[ieee80211_get_sdata_band(sdata)]; |
687da132 EG |
388 | u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> |
389 | IEEE80211_HT_CAP_SM_PS_SHIFT; | |
390 | /* | |
391 | * Assume that hostapd advertises our caps in the beacon and | |
392 | * this is the known_smps_mode for a station that just assciated | |
393 | */ | |
394 | switch (smps) { | |
395 | case WLAN_HT_SMPS_CONTROL_DISABLED: | |
396 | sta->known_smps_mode = IEEE80211_SMPS_OFF; | |
397 | break; | |
398 | case WLAN_HT_SMPS_CONTROL_STATIC: | |
399 | sta->known_smps_mode = IEEE80211_SMPS_STATIC; | |
400 | break; | |
401 | case WLAN_HT_SMPS_CONTROL_DYNAMIC: | |
402 | sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC; | |
403 | break; | |
404 | default: | |
405 | WARN_ON(1); | |
406 | } | |
407 | } | |
af0ed69b | 408 | |
bdcbd8e0 | 409 | sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr); |
ef04a297 | 410 | |
abfbc3af | 411 | return sta; |
ba8c3d6f FF |
412 | |
413 | free_txq: | |
414 | if (sta->sta.txq[0]) | |
415 | kfree(to_txq_info(sta->sta.txq[0])); | |
416 | free: | |
433f5bc1 JB |
417 | #ifdef CONFIG_MAC80211_MESH |
418 | kfree(sta->mesh); | |
419 | #endif | |
ba8c3d6f FF |
420 | kfree(sta); |
421 | return NULL; | |
73651ee6 JB |
422 | } |
423 | ||
8c71df7a | 424 | static int sta_info_insert_check(struct sta_info *sta) |
34e89507 | 425 | { |
34e89507 | 426 | struct ieee80211_sub_if_data *sdata = sta->sdata; |
34e89507 | 427 | |
03e4497e JB |
428 | /* |
429 | * Can't be a WARN_ON because it can be triggered through a race: | |
430 | * something inserts a STA (on one CPU) without holding the RTNL | |
431 | * and another CPU turns off the net device. | |
432 | */ | |
8c71df7a GE |
433 | if (unlikely(!ieee80211_sdata_running(sdata))) |
434 | return -ENETDOWN; | |
03e4497e | 435 | |
b203ca39 | 436 | if (WARN_ON(ether_addr_equal(sta->sta.addr, sdata->vif.addr) || |
8c71df7a GE |
437 | is_multicast_ether_addr(sta->sta.addr))) |
438 | return -EINVAL; | |
439 | ||
31104891 JB |
440 | /* Strictly speaking this isn't necessary as we hold the mutex, but |
441 | * the rhashtable code can't really deal with that distinction. We | |
442 | * do require the mutex for correctness though. | |
443 | */ | |
444 | rcu_read_lock(); | |
445 | lockdep_assert_held(&sdata->local->sta_mtx); | |
446 | if (ieee80211_hw_check(&sdata->local->hw, NEEDS_UNIQUE_STA_ADDR) && | |
447 | ieee80211_find_sta_by_ifaddr(&sdata->local->hw, sta->addr, NULL)) { | |
448 | rcu_read_unlock(); | |
449 | return -ENOTUNIQ; | |
450 | } | |
451 | rcu_read_unlock(); | |
452 | ||
8c71df7a GE |
453 | return 0; |
454 | } | |
455 | ||
f09603a2 JB |
456 | static int sta_info_insert_drv_state(struct ieee80211_local *local, |
457 | struct ieee80211_sub_if_data *sdata, | |
458 | struct sta_info *sta) | |
459 | { | |
460 | enum ieee80211_sta_state state; | |
461 | int err = 0; | |
462 | ||
463 | for (state = IEEE80211_STA_NOTEXIST; state < sta->sta_state; state++) { | |
464 | err = drv_sta_state(local, sdata, sta, state, state + 1); | |
465 | if (err) | |
466 | break; | |
467 | } | |
468 | ||
469 | if (!err) { | |
a4ec45a4 JB |
470 | /* |
471 | * Drivers using legacy sta_add/sta_remove callbacks only | |
472 | * get uploaded set to true after sta_add is called. | |
473 | */ | |
474 | if (!local->ops->sta_add) | |
475 | sta->uploaded = true; | |
f09603a2 JB |
476 | return 0; |
477 | } | |
478 | ||
479 | if (sdata->vif.type == NL80211_IFTYPE_ADHOC) { | |
bdcbd8e0 JB |
480 | sdata_info(sdata, |
481 | "failed to move IBSS STA %pM to state %d (%d) - keeping it anyway\n", | |
482 | sta->sta.addr, state + 1, err); | |
f09603a2 JB |
483 | err = 0; |
484 | } | |
485 | ||
486 | /* unwind on error */ | |
487 | for (; state > IEEE80211_STA_NOTEXIST; state--) | |
488 | WARN_ON(drv_sta_state(local, sdata, sta, state, state - 1)); | |
489 | ||
490 | return err; | |
491 | } | |
492 | ||
8c71df7a GE |
493 | /* |
494 | * should be called with sta_mtx locked | |
495 | * this function replaces the mutex lock | |
496 | * with a RCU lock | |
497 | */ | |
4d33960b | 498 | static int sta_info_insert_finish(struct sta_info *sta) __acquires(RCU) |
8c71df7a GE |
499 | { |
500 | struct ieee80211_local *local = sta->local; | |
501 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
0ef049dc | 502 | struct station_info *sinfo; |
8c71df7a GE |
503 | int err = 0; |
504 | ||
505 | lockdep_assert_held(&local->sta_mtx); | |
34e89507 | 506 | |
0ef049dc AB |
507 | sinfo = kzalloc(sizeof(struct station_info), GFP_KERNEL); |
508 | if (!sinfo) { | |
509 | err = -ENOMEM; | |
510 | goto out_err; | |
511 | } | |
512 | ||
7852e361 JB |
513 | /* check if STA exists already */ |
514 | if (sta_info_get_bss(sdata, sta->sta.addr)) { | |
515 | err = -EEXIST; | |
516 | goto out_err; | |
4d33960b | 517 | } |
32bfd35d | 518 | |
7852e361 JB |
519 | local->num_sta++; |
520 | local->sta_generation++; | |
521 | smp_mb(); | |
4d33960b | 522 | |
5108ca82 JB |
523 | /* simplify things and don't accept BA sessions yet */ |
524 | set_sta_flag(sta, WLAN_STA_BLOCK_BA); | |
525 | ||
7852e361 JB |
526 | /* make the station visible */ |
527 | sta_info_hash_add(local, sta); | |
83d5cc01 | 528 | |
2bad7748 | 529 | list_add_tail_rcu(&sta->list, &local->sta_list); |
4d33960b | 530 | |
5108ca82 JB |
531 | /* notify driver */ |
532 | err = sta_info_insert_drv_state(local, sdata, sta); | |
533 | if (err) | |
534 | goto out_remove; | |
535 | ||
7852e361 | 536 | set_sta_flag(sta, WLAN_STA_INSERTED); |
5108ca82 JB |
537 | /* accept BA sessions now */ |
538 | clear_sta_flag(sta, WLAN_STA_BLOCK_BA); | |
4d33960b | 539 | |
7852e361 JB |
540 | ieee80211_sta_debugfs_add(sta); |
541 | rate_control_add_sta_debugfs(sta); | |
4d33960b | 542 | |
0ef049dc AB |
543 | sinfo->generation = local->sta_generation; |
544 | cfg80211_new_sta(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL); | |
545 | kfree(sinfo); | |
d0709a65 | 546 | |
bdcbd8e0 | 547 | sta_dbg(sdata, "Inserted STA %pM\n", sta->sta.addr); |
f0706e82 | 548 | |
34e89507 JB |
549 | /* move reference to rcu-protected */ |
550 | rcu_read_lock(); | |
551 | mutex_unlock(&local->sta_mtx); | |
e9f207f0 | 552 | |
73651ee6 JB |
553 | if (ieee80211_vif_is_mesh(&sdata->vif)) |
554 | mesh_accept_plinks_update(sdata); | |
555 | ||
8c71df7a | 556 | return 0; |
5108ca82 JB |
557 | out_remove: |
558 | sta_info_hash_del(local, sta); | |
559 | list_del_rcu(&sta->list); | |
560 | local->num_sta--; | |
561 | synchronize_net(); | |
562 | __cleanup_single_sta(sta); | |
4d33960b JB |
563 | out_err: |
564 | mutex_unlock(&local->sta_mtx); | |
ea32f065 | 565 | kfree(sinfo); |
4d33960b JB |
566 | rcu_read_lock(); |
567 | return err; | |
8c71df7a GE |
568 | } |
569 | ||
570 | int sta_info_insert_rcu(struct sta_info *sta) __acquires(RCU) | |
571 | { | |
572 | struct ieee80211_local *local = sta->local; | |
308f7fcf | 573 | int err; |
8c71df7a | 574 | |
4d33960b JB |
575 | might_sleep(); |
576 | ||
31104891 JB |
577 | mutex_lock(&local->sta_mtx); |
578 | ||
8c71df7a GE |
579 | err = sta_info_insert_check(sta); |
580 | if (err) { | |
31104891 | 581 | mutex_unlock(&local->sta_mtx); |
8c71df7a GE |
582 | rcu_read_lock(); |
583 | goto out_free; | |
584 | } | |
585 | ||
4d33960b | 586 | err = sta_info_insert_finish(sta); |
8c71df7a GE |
587 | if (err) |
588 | goto out_free; | |
589 | ||
73651ee6 | 590 | return 0; |
93e5deb1 | 591 | out_free: |
d9a7ddb0 | 592 | sta_info_free(local, sta); |
93e5deb1 | 593 | return err; |
f0706e82 JB |
594 | } |
595 | ||
34e89507 JB |
596 | int sta_info_insert(struct sta_info *sta) |
597 | { | |
598 | int err = sta_info_insert_rcu(sta); | |
599 | ||
600 | rcu_read_unlock(); | |
601 | ||
602 | return err; | |
603 | } | |
604 | ||
d012a605 | 605 | static inline void __bss_tim_set(u8 *tim, u16 id) |
004c872e JB |
606 | { |
607 | /* | |
608 | * This format has been mandated by the IEEE specifications, | |
609 | * so this line may not be changed to use the __set_bit() format. | |
610 | */ | |
d012a605 | 611 | tim[id / 8] |= (1 << (id % 8)); |
004c872e JB |
612 | } |
613 | ||
d012a605 | 614 | static inline void __bss_tim_clear(u8 *tim, u16 id) |
004c872e JB |
615 | { |
616 | /* | |
617 | * This format has been mandated by the IEEE specifications, | |
618 | * so this line may not be changed to use the __clear_bit() format. | |
619 | */ | |
d012a605 | 620 | tim[id / 8] &= ~(1 << (id % 8)); |
004c872e JB |
621 | } |
622 | ||
3d5839b6 IP |
623 | static inline bool __bss_tim_get(u8 *tim, u16 id) |
624 | { | |
625 | /* | |
626 | * This format has been mandated by the IEEE specifications, | |
627 | * so this line may not be changed to use the test_bit() format. | |
628 | */ | |
629 | return tim[id / 8] & (1 << (id % 8)); | |
630 | } | |
631 | ||
948d887d | 632 | static unsigned long ieee80211_tids_for_ac(int ac) |
004c872e | 633 | { |
948d887d JB |
634 | /* If we ever support TIDs > 7, this obviously needs to be adjusted */ |
635 | switch (ac) { | |
636 | case IEEE80211_AC_VO: | |
637 | return BIT(6) | BIT(7); | |
638 | case IEEE80211_AC_VI: | |
639 | return BIT(4) | BIT(5); | |
640 | case IEEE80211_AC_BE: | |
641 | return BIT(0) | BIT(3); | |
642 | case IEEE80211_AC_BK: | |
643 | return BIT(1) | BIT(2); | |
644 | default: | |
645 | WARN_ON(1); | |
646 | return 0; | |
d0709a65 | 647 | } |
004c872e JB |
648 | } |
649 | ||
9b7a86f3 | 650 | static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending) |
004c872e | 651 | { |
c868cb35 | 652 | struct ieee80211_local *local = sta->local; |
d012a605 | 653 | struct ps_data *ps; |
948d887d JB |
654 | bool indicate_tim = false; |
655 | u8 ignore_for_tim = sta->sta.uapsd_queues; | |
656 | int ac; | |
a69bd8e6 | 657 | u16 id = sta->sta.aid; |
d012a605 MP |
658 | |
659 | if (sta->sdata->vif.type == NL80211_IFTYPE_AP || | |
660 | sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { | |
661 | if (WARN_ON_ONCE(!sta->sdata->bss)) | |
662 | return; | |
004c872e | 663 | |
d012a605 | 664 | ps = &sta->sdata->bss->ps; |
3f52b7e3 MP |
665 | #ifdef CONFIG_MAC80211_MESH |
666 | } else if (ieee80211_vif_is_mesh(&sta->sdata->vif)) { | |
667 | ps = &sta->sdata->u.mesh.ps; | |
3f52b7e3 | 668 | #endif |
d012a605 | 669 | } else { |
c868cb35 | 670 | return; |
d012a605 | 671 | } |
3e122be0 | 672 | |
c868cb35 | 673 | /* No need to do anything if the driver does all */ |
30686bf7 | 674 | if (ieee80211_hw_check(&local->hw, AP_LINK_PS)) |
c868cb35 | 675 | return; |
004c872e | 676 | |
c868cb35 JB |
677 | if (sta->dead) |
678 | goto done; | |
3e122be0 | 679 | |
948d887d JB |
680 | /* |
681 | * If all ACs are delivery-enabled then we should build | |
682 | * the TIM bit for all ACs anyway; if only some are then | |
683 | * we ignore those and build the TIM bit using only the | |
684 | * non-enabled ones. | |
685 | */ | |
686 | if (ignore_for_tim == BIT(IEEE80211_NUM_ACS) - 1) | |
687 | ignore_for_tim = 0; | |
688 | ||
9b7a86f3 JB |
689 | if (ignore_pending) |
690 | ignore_for_tim = BIT(IEEE80211_NUM_ACS) - 1; | |
691 | ||
948d887d JB |
692 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { |
693 | unsigned long tids; | |
3e122be0 | 694 | |
948d887d JB |
695 | if (ignore_for_tim & BIT(ac)) |
696 | continue; | |
697 | ||
698 | indicate_tim |= !skb_queue_empty(&sta->tx_filtered[ac]) || | |
699 | !skb_queue_empty(&sta->ps_tx_buf[ac]); | |
700 | if (indicate_tim) | |
701 | break; | |
3e122be0 | 702 | |
948d887d JB |
703 | tids = ieee80211_tids_for_ac(ac); |
704 | ||
705 | indicate_tim |= | |
706 | sta->driver_buffered_tids & tids; | |
ba8c3d6f FF |
707 | indicate_tim |= |
708 | sta->txq_buffered_tids & tids; | |
d0709a65 | 709 | } |
004c872e | 710 | |
c868cb35 | 711 | done: |
65f704a5 | 712 | spin_lock_bh(&local->tim_lock); |
004c872e | 713 | |
3d5839b6 IP |
714 | if (indicate_tim == __bss_tim_get(ps->tim, id)) |
715 | goto out_unlock; | |
716 | ||
948d887d | 717 | if (indicate_tim) |
d012a605 | 718 | __bss_tim_set(ps->tim, id); |
c868cb35 | 719 | else |
d012a605 | 720 | __bss_tim_clear(ps->tim, id); |
004c872e | 721 | |
9b7a86f3 | 722 | if (local->ops->set_tim && !WARN_ON(sta->dead)) { |
c868cb35 | 723 | local->tim_in_locked_section = true; |
948d887d | 724 | drv_set_tim(local, &sta->sta, indicate_tim); |
c868cb35 JB |
725 | local->tim_in_locked_section = false; |
726 | } | |
3e122be0 | 727 | |
3d5839b6 | 728 | out_unlock: |
65f704a5 | 729 | spin_unlock_bh(&local->tim_lock); |
004c872e JB |
730 | } |
731 | ||
9b7a86f3 JB |
732 | void sta_info_recalc_tim(struct sta_info *sta) |
733 | { | |
734 | __sta_info_recalc_tim(sta, false); | |
735 | } | |
736 | ||
cd0b8d89 | 737 | static bool sta_info_buffer_expired(struct sta_info *sta, struct sk_buff *skb) |
f0706e82 | 738 | { |
e039fa4a | 739 | struct ieee80211_tx_info *info; |
f0706e82 JB |
740 | int timeout; |
741 | ||
742 | if (!skb) | |
cd0b8d89 | 743 | return false; |
f0706e82 | 744 | |
e039fa4a | 745 | info = IEEE80211_SKB_CB(skb); |
f0706e82 JB |
746 | |
747 | /* Timeout: (2 * listen_interval * beacon_int * 1024 / 1000000) sec */ | |
57c4d7b4 JB |
748 | timeout = (sta->listen_interval * |
749 | sta->sdata->vif.bss_conf.beacon_int * | |
750 | 32 / 15625) * HZ; | |
f0706e82 JB |
751 | if (timeout < STA_TX_BUFFER_EXPIRE) |
752 | timeout = STA_TX_BUFFER_EXPIRE; | |
e039fa4a | 753 | return time_after(jiffies, info->control.jiffies + timeout); |
f0706e82 JB |
754 | } |
755 | ||
756 | ||
948d887d JB |
757 | static bool sta_info_cleanup_expire_buffered_ac(struct ieee80211_local *local, |
758 | struct sta_info *sta, int ac) | |
f0706e82 JB |
759 | { |
760 | unsigned long flags; | |
761 | struct sk_buff *skb; | |
762 | ||
60750397 JB |
763 | /* |
764 | * First check for frames that should expire on the filtered | |
765 | * queue. Frames here were rejected by the driver and are on | |
766 | * a separate queue to avoid reordering with normal PS-buffered | |
767 | * frames. They also aren't accounted for right now in the | |
768 | * total_ps_buffered counter. | |
769 | */ | |
770 | for (;;) { | |
948d887d JB |
771 | spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); |
772 | skb = skb_peek(&sta->tx_filtered[ac]); | |
60750397 | 773 | if (sta_info_buffer_expired(sta, skb)) |
948d887d | 774 | skb = __skb_dequeue(&sta->tx_filtered[ac]); |
60750397 JB |
775 | else |
776 | skb = NULL; | |
948d887d | 777 | spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); |
60750397 JB |
778 | |
779 | /* | |
780 | * Frames are queued in order, so if this one | |
781 | * hasn't expired yet we can stop testing. If | |
782 | * we actually reached the end of the queue we | |
783 | * also need to stop, of course. | |
784 | */ | |
785 | if (!skb) | |
786 | break; | |
d4fa14cd | 787 | ieee80211_free_txskb(&local->hw, skb); |
60750397 JB |
788 | } |
789 | ||
790 | /* | |
791 | * Now also check the normal PS-buffered queue, this will | |
792 | * only find something if the filtered queue was emptied | |
793 | * since the filtered frames are all before the normal PS | |
794 | * buffered frames. | |
795 | */ | |
f0706e82 | 796 | for (;;) { |
948d887d JB |
797 | spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); |
798 | skb = skb_peek(&sta->ps_tx_buf[ac]); | |
57c4d7b4 | 799 | if (sta_info_buffer_expired(sta, skb)) |
948d887d | 800 | skb = __skb_dequeue(&sta->ps_tx_buf[ac]); |
836341a7 | 801 | else |
f0706e82 | 802 | skb = NULL; |
948d887d | 803 | spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); |
f0706e82 | 804 | |
60750397 JB |
805 | /* |
806 | * frames are queued in order, so if this one | |
807 | * hasn't expired yet (or we reached the end of | |
808 | * the queue) we can stop testing | |
809 | */ | |
836341a7 | 810 | if (!skb) |
f0706e82 | 811 | break; |
836341a7 | 812 | |
836341a7 | 813 | local->total_ps_buffered--; |
bdcbd8e0 JB |
814 | ps_dbg(sta->sdata, "Buffered frame expired (STA %pM)\n", |
815 | sta->sta.addr); | |
d4fa14cd | 816 | ieee80211_free_txskb(&local->hw, skb); |
f0706e82 | 817 | } |
3393a608 | 818 | |
60750397 JB |
819 | /* |
820 | * Finally, recalculate the TIM bit for this station -- it might | |
821 | * now be clear because the station was too slow to retrieve its | |
822 | * frames. | |
823 | */ | |
824 | sta_info_recalc_tim(sta); | |
825 | ||
826 | /* | |
827 | * Return whether there are any frames still buffered, this is | |
828 | * used to check whether the cleanup timer still needs to run, | |
829 | * if there are no frames we don't need to rearm the timer. | |
830 | */ | |
948d887d JB |
831 | return !(skb_queue_empty(&sta->ps_tx_buf[ac]) && |
832 | skb_queue_empty(&sta->tx_filtered[ac])); | |
833 | } | |
834 | ||
835 | static bool sta_info_cleanup_expire_buffered(struct ieee80211_local *local, | |
836 | struct sta_info *sta) | |
837 | { | |
838 | bool have_buffered = false; | |
839 | int ac; | |
840 | ||
3f52b7e3 MP |
841 | /* This is only necessary for stations on BSS/MBSS interfaces */ |
842 | if (!sta->sdata->bss && | |
843 | !ieee80211_vif_is_mesh(&sta->sdata->vif)) | |
948d887d JB |
844 | return false; |
845 | ||
846 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) | |
847 | have_buffered |= | |
848 | sta_info_cleanup_expire_buffered_ac(local, sta, ac); | |
849 | ||
850 | return have_buffered; | |
f0706e82 JB |
851 | } |
852 | ||
d778207b | 853 | static int __must_check __sta_info_destroy_part1(struct sta_info *sta) |
f0706e82 | 854 | { |
34e89507 JB |
855 | struct ieee80211_local *local; |
856 | struct ieee80211_sub_if_data *sdata; | |
6d10e46b | 857 | int ret; |
f0706e82 | 858 | |
34e89507 | 859 | might_sleep(); |
f0706e82 | 860 | |
34e89507 JB |
861 | if (!sta) |
862 | return -ENOENT; | |
5bb644a0 | 863 | |
34e89507 JB |
864 | local = sta->local; |
865 | sdata = sta->sdata; | |
f0706e82 | 866 | |
83d5cc01 JB |
867 | lockdep_assert_held(&local->sta_mtx); |
868 | ||
098a6070 JB |
869 | /* |
870 | * Before removing the station from the driver and | |
871 | * rate control, it might still start new aggregation | |
872 | * sessions -- block that to make sure the tear-down | |
873 | * will be sufficient. | |
874 | */ | |
c2c98fde | 875 | set_sta_flag(sta, WLAN_STA_BLOCK_BA); |
c82c4a80 | 876 | ieee80211_sta_tear_down_BA_sessions(sta, AGG_STOP_DESTROY_STA); |
098a6070 | 877 | |
f59374eb SS |
878 | /* |
879 | * Before removing the station from the driver there might be pending | |
880 | * rx frames on RSS queues sent prior to the disassociation - wait for | |
881 | * all such frames to be processed. | |
882 | */ | |
883 | drv_sync_rx_queues(local, sta); | |
884 | ||
34e89507 | 885 | ret = sta_info_hash_del(local, sta); |
b01711be | 886 | if (WARN_ON(ret)) |
34e89507 JB |
887 | return ret; |
888 | ||
a7a6bdd0 AN |
889 | /* |
890 | * for TDLS peers, make sure to return to the base channel before | |
891 | * removal. | |
892 | */ | |
893 | if (test_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL)) { | |
894 | drv_tdls_cancel_channel_switch(local, sdata, &sta->sta); | |
895 | clear_sta_flag(sta, WLAN_STA_TDLS_OFF_CHANNEL); | |
896 | } | |
897 | ||
794454ce | 898 | list_del_rcu(&sta->list); |
ef044763 | 899 | sta->removed = true; |
4d33960b | 900 | |
6a9d1b91 JB |
901 | drv_sta_pre_rcu_remove(local, sta->sdata, sta); |
902 | ||
a710c816 JB |
903 | if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN && |
904 | rcu_access_pointer(sdata->u.vlan.sta) == sta) | |
905 | RCU_INIT_POINTER(sdata->u.vlan.sta, NULL); | |
906 | ||
d778207b JB |
907 | return 0; |
908 | } | |
909 | ||
910 | static void __sta_info_destroy_part2(struct sta_info *sta) | |
911 | { | |
912 | struct ieee80211_local *local = sta->local; | |
913 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
0ef049dc | 914 | struct station_info *sinfo; |
d778207b JB |
915 | int ret; |
916 | ||
917 | /* | |
918 | * NOTE: This assumes at least synchronize_net() was done | |
919 | * after _part1 and before _part2! | |
920 | */ | |
921 | ||
922 | might_sleep(); | |
923 | lockdep_assert_held(&local->sta_mtx); | |
924 | ||
c8782078 | 925 | /* now keys can no longer be reached */ |
6d10e46b | 926 | ieee80211_free_sta_keys(local, sta); |
34e89507 | 927 | |
9b7a86f3 JB |
928 | /* disable TIM bit - last chance to tell driver */ |
929 | __sta_info_recalc_tim(sta, true); | |
930 | ||
34e89507 JB |
931 | sta->dead = true; |
932 | ||
34e89507 JB |
933 | local->num_sta--; |
934 | local->sta_generation++; | |
935 | ||
83d5cc01 | 936 | while (sta->sta_state > IEEE80211_STA_NONE) { |
f09603a2 JB |
937 | ret = sta_info_move_state(sta, sta->sta_state - 1); |
938 | if (ret) { | |
83d5cc01 JB |
939 | WARN_ON_ONCE(1); |
940 | break; | |
941 | } | |
942 | } | |
d9a7ddb0 | 943 | |
f09603a2 | 944 | if (sta->uploaded) { |
f09603a2 JB |
945 | ret = drv_sta_state(local, sdata, sta, IEEE80211_STA_NONE, |
946 | IEEE80211_STA_NOTEXIST); | |
947 | WARN_ON_ONCE(ret != 0); | |
948 | } | |
34e89507 | 949 | |
bdcbd8e0 JB |
950 | sta_dbg(sdata, "Removed STA %pM\n", sta->sta.addr); |
951 | ||
0ef049dc AB |
952 | sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL); |
953 | if (sinfo) | |
954 | sta_set_sinfo(sta, sinfo); | |
955 | cfg80211_del_sta_sinfo(sdata->dev, sta->sta.addr, sinfo, GFP_KERNEL); | |
956 | kfree(sinfo); | |
ec15e68b | 957 | |
34e89507 JB |
958 | rate_control_remove_sta_debugfs(sta); |
959 | ieee80211_sta_debugfs_remove(sta); | |
960 | ||
d34ba216 | 961 | cleanup_single_sta(sta); |
d778207b JB |
962 | } |
963 | ||
964 | int __must_check __sta_info_destroy(struct sta_info *sta) | |
965 | { | |
966 | int err = __sta_info_destroy_part1(sta); | |
967 | ||
968 | if (err) | |
969 | return err; | |
970 | ||
971 | synchronize_net(); | |
972 | ||
973 | __sta_info_destroy_part2(sta); | |
34e89507 JB |
974 | |
975 | return 0; | |
4d6141c3 JS |
976 | } |
977 | ||
34e89507 | 978 | int sta_info_destroy_addr(struct ieee80211_sub_if_data *sdata, const u8 *addr) |
4d6141c3 | 979 | { |
34e89507 JB |
980 | struct sta_info *sta; |
981 | int ret; | |
4d6141c3 | 982 | |
34e89507 | 983 | mutex_lock(&sdata->local->sta_mtx); |
7852e361 | 984 | sta = sta_info_get(sdata, addr); |
34e89507 JB |
985 | ret = __sta_info_destroy(sta); |
986 | mutex_unlock(&sdata->local->sta_mtx); | |
4d6141c3 JS |
987 | |
988 | return ret; | |
989 | } | |
990 | ||
34e89507 JB |
991 | int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata, |
992 | const u8 *addr) | |
e9f207f0 | 993 | { |
34e89507 JB |
994 | struct sta_info *sta; |
995 | int ret; | |
e9f207f0 | 996 | |
34e89507 | 997 | mutex_lock(&sdata->local->sta_mtx); |
7852e361 | 998 | sta = sta_info_get_bss(sdata, addr); |
34e89507 JB |
999 | ret = __sta_info_destroy(sta); |
1000 | mutex_unlock(&sdata->local->sta_mtx); | |
d0709a65 | 1001 | |
34e89507 JB |
1002 | return ret; |
1003 | } | |
e9f207f0 | 1004 | |
34e89507 JB |
1005 | static void sta_info_cleanup(unsigned long data) |
1006 | { | |
1007 | struct ieee80211_local *local = (struct ieee80211_local *) data; | |
1008 | struct sta_info *sta; | |
3393a608 | 1009 | bool timer_needed = false; |
34e89507 JB |
1010 | |
1011 | rcu_read_lock(); | |
1012 | list_for_each_entry_rcu(sta, &local->sta_list, list) | |
3393a608 JO |
1013 | if (sta_info_cleanup_expire_buffered(local, sta)) |
1014 | timer_needed = true; | |
34e89507 | 1015 | rcu_read_unlock(); |
e9f207f0 | 1016 | |
34e89507 JB |
1017 | if (local->quiescing) |
1018 | return; | |
d0709a65 | 1019 | |
3393a608 JO |
1020 | if (!timer_needed) |
1021 | return; | |
1022 | ||
26d59535 JB |
1023 | mod_timer(&local->sta_cleanup, |
1024 | round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); | |
e9f207f0 | 1025 | } |
e9f207f0 | 1026 | |
7bedd0cf | 1027 | u32 sta_addr_hash(const void *key, u32 length, u32 seed) |
f0706e82 | 1028 | { |
7bedd0cf JB |
1029 | return jhash(key, ETH_ALEN, seed); |
1030 | } | |
1031 | ||
1032 | int sta_info_init(struct ieee80211_local *local) | |
1033 | { | |
1034 | int err; | |
1035 | ||
1036 | err = rhashtable_init(&local->sta_hash, &sta_rht_params); | |
1037 | if (err) | |
1038 | return err; | |
1039 | ||
4d33960b | 1040 | spin_lock_init(&local->tim_lock); |
34e89507 | 1041 | mutex_init(&local->sta_mtx); |
f0706e82 | 1042 | INIT_LIST_HEAD(&local->sta_list); |
f0706e82 | 1043 | |
b24b8a24 PE |
1044 | setup_timer(&local->sta_cleanup, sta_info_cleanup, |
1045 | (unsigned long)local); | |
7bedd0cf | 1046 | return 0; |
f0706e82 JB |
1047 | } |
1048 | ||
1049 | void sta_info_stop(struct ieee80211_local *local) | |
1050 | { | |
a56f992c | 1051 | del_timer_sync(&local->sta_cleanup); |
7bedd0cf | 1052 | rhashtable_destroy(&local->sta_hash); |
f0706e82 JB |
1053 | } |
1054 | ||
051007d9 | 1055 | |
e716251d | 1056 | int __sta_info_flush(struct ieee80211_sub_if_data *sdata, bool vlans) |
f0706e82 | 1057 | { |
b998e8bb | 1058 | struct ieee80211_local *local = sdata->local; |
f0706e82 | 1059 | struct sta_info *sta, *tmp; |
d778207b | 1060 | LIST_HEAD(free_list); |
44213b5e | 1061 | int ret = 0; |
f0706e82 | 1062 | |
d0709a65 | 1063 | might_sleep(); |
be8755e1 | 1064 | |
e716251d JB |
1065 | WARN_ON(vlans && sdata->vif.type != NL80211_IFTYPE_AP); |
1066 | WARN_ON(vlans && !sdata->bss); | |
1067 | ||
34e89507 | 1068 | mutex_lock(&local->sta_mtx); |
d0709a65 | 1069 | list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { |
e716251d JB |
1070 | if (sdata == sta->sdata || |
1071 | (vlans && sdata->bss == sta->sdata->bss)) { | |
d778207b JB |
1072 | if (!WARN_ON(__sta_info_destroy_part1(sta))) |
1073 | list_add(&sta->free_list, &free_list); | |
34316837 JB |
1074 | ret++; |
1075 | } | |
be8755e1 | 1076 | } |
d778207b JB |
1077 | |
1078 | if (!list_empty(&free_list)) { | |
1079 | synchronize_net(); | |
1080 | list_for_each_entry_safe(sta, tmp, &free_list, free_list) | |
1081 | __sta_info_destroy_part2(sta); | |
1082 | } | |
34e89507 | 1083 | mutex_unlock(&local->sta_mtx); |
44213b5e | 1084 | |
051007d9 JB |
1085 | return ret; |
1086 | } | |
1087 | ||
24723d1b JB |
1088 | void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, |
1089 | unsigned long exp_time) | |
1090 | { | |
1091 | struct ieee80211_local *local = sdata->local; | |
1092 | struct sta_info *sta, *tmp; | |
24723d1b | 1093 | |
34e89507 | 1094 | mutex_lock(&local->sta_mtx); |
e46a2cf9 MSS |
1095 | |
1096 | list_for_each_entry_safe(sta, tmp, &local->sta_list, list) { | |
ec2b774e ML |
1097 | if (sdata != sta->sdata) |
1098 | continue; | |
1099 | ||
e5a9f8d0 | 1100 | if (time_after(jiffies, sta->rx_stats.last_rx + exp_time)) { |
eea57d42 MSS |
1101 | sta_dbg(sta->sdata, "expiring inactive STA %pM\n", |
1102 | sta->sta.addr); | |
3f52b7e3 MP |
1103 | |
1104 | if (ieee80211_vif_is_mesh(&sdata->vif) && | |
1105 | test_sta_flag(sta, WLAN_STA_PS_STA)) | |
1106 | atomic_dec(&sdata->u.mesh.ps.num_sta_ps); | |
1107 | ||
34e89507 | 1108 | WARN_ON(__sta_info_destroy(sta)); |
24723d1b | 1109 | } |
e46a2cf9 MSS |
1110 | } |
1111 | ||
34e89507 | 1112 | mutex_unlock(&local->sta_mtx); |
24723d1b | 1113 | } |
17741cdc | 1114 | |
686b9cb9 | 1115 | struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, |
7bedd0cf JB |
1116 | const u8 *addr, |
1117 | const u8 *localaddr) | |
17741cdc | 1118 | { |
7bedd0cf JB |
1119 | struct ieee80211_local *local = hw_to_local(hw); |
1120 | struct sta_info *sta; | |
1121 | struct rhash_head *tmp; | |
1122 | const struct bucket_table *tbl; | |
1123 | ||
1124 | tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash); | |
17741cdc | 1125 | |
686b9cb9 BG |
1126 | /* |
1127 | * Just return a random station if localaddr is NULL | |
1128 | * ... first in list. | |
1129 | */ | |
7bedd0cf | 1130 | for_each_sta_info(local, tbl, addr, sta, tmp) { |
686b9cb9 | 1131 | if (localaddr && |
b203ca39 | 1132 | !ether_addr_equal(sta->sdata->vif.addr, localaddr)) |
686b9cb9 | 1133 | continue; |
f7c65594 JB |
1134 | if (!sta->uploaded) |
1135 | return NULL; | |
abe60632 | 1136 | return &sta->sta; |
f7c65594 JB |
1137 | } |
1138 | ||
abe60632 | 1139 | return NULL; |
17741cdc | 1140 | } |
686b9cb9 | 1141 | EXPORT_SYMBOL_GPL(ieee80211_find_sta_by_ifaddr); |
5ed176e1 JB |
1142 | |
1143 | struct ieee80211_sta *ieee80211_find_sta(struct ieee80211_vif *vif, | |
1144 | const u8 *addr) | |
1145 | { | |
f7c65594 | 1146 | struct sta_info *sta; |
5ed176e1 JB |
1147 | |
1148 | if (!vif) | |
1149 | return NULL; | |
1150 | ||
f7c65594 JB |
1151 | sta = sta_info_get_bss(vif_to_sdata(vif), addr); |
1152 | if (!sta) | |
1153 | return NULL; | |
1154 | ||
1155 | if (!sta->uploaded) | |
1156 | return NULL; | |
5ed176e1 | 1157 | |
f7c65594 | 1158 | return &sta->sta; |
5ed176e1 | 1159 | } |
17741cdc | 1160 | EXPORT_SYMBOL(ieee80211_find_sta); |
af818581 | 1161 | |
e3685e03 JB |
1162 | /* powersave support code */ |
1163 | void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta) | |
50a9432d | 1164 | { |
608383bf | 1165 | struct ieee80211_sub_if_data *sdata = sta->sdata; |
e3685e03 JB |
1166 | struct ieee80211_local *local = sdata->local; |
1167 | struct sk_buff_head pending; | |
ba8c3d6f | 1168 | int filtered = 0, buffered = 0, ac, i; |
e3685e03 | 1169 | unsigned long flags; |
d012a605 MP |
1170 | struct ps_data *ps; |
1171 | ||
3918edb0 FF |
1172 | if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) |
1173 | sdata = container_of(sdata->bss, struct ieee80211_sub_if_data, | |
1174 | u.ap); | |
1175 | ||
1176 | if (sdata->vif.type == NL80211_IFTYPE_AP) | |
d012a605 | 1177 | ps = &sdata->bss->ps; |
3f52b7e3 MP |
1178 | else if (ieee80211_vif_is_mesh(&sdata->vif)) |
1179 | ps = &sdata->u.mesh.ps; | |
d012a605 MP |
1180 | else |
1181 | return; | |
50a9432d | 1182 | |
c2c98fde | 1183 | clear_sta_flag(sta, WLAN_STA_SP); |
47086fc5 | 1184 | |
5a306f58 | 1185 | BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1); |
948d887d | 1186 | sta->driver_buffered_tids = 0; |
ba8c3d6f | 1187 | sta->txq_buffered_tids = 0; |
af818581 | 1188 | |
30686bf7 | 1189 | if (!ieee80211_hw_check(&local->hw, AP_LINK_PS)) |
d057e5a3 | 1190 | drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); |
af818581 | 1191 | |
ba8c3d6f FF |
1192 | if (sta->sta.txq[0]) { |
1193 | for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) { | |
1194 | struct txq_info *txqi = to_txq_info(sta->sta.txq[i]); | |
1195 | ||
1196 | if (!skb_queue_len(&txqi->queue)) | |
1197 | continue; | |
1198 | ||
1199 | drv_wake_tx_queue(local, txqi); | |
1200 | } | |
1201 | } | |
1202 | ||
948d887d | 1203 | skb_queue_head_init(&pending); |
af818581 | 1204 | |
1d147bfa EG |
1205 | /* sync with ieee80211_tx_h_unicast_ps_buf */ |
1206 | spin_lock(&sta->ps_lock); | |
af818581 | 1207 | /* Send all buffered frames to the station */ |
948d887d JB |
1208 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { |
1209 | int count = skb_queue_len(&pending), tmp; | |
1210 | ||
987c285c | 1211 | spin_lock_irqsave(&sta->tx_filtered[ac].lock, flags); |
948d887d | 1212 | skb_queue_splice_tail_init(&sta->tx_filtered[ac], &pending); |
987c285c | 1213 | spin_unlock_irqrestore(&sta->tx_filtered[ac].lock, flags); |
948d887d JB |
1214 | tmp = skb_queue_len(&pending); |
1215 | filtered += tmp - count; | |
1216 | count = tmp; | |
1217 | ||
987c285c | 1218 | spin_lock_irqsave(&sta->ps_tx_buf[ac].lock, flags); |
948d887d | 1219 | skb_queue_splice_tail_init(&sta->ps_tx_buf[ac], &pending); |
987c285c | 1220 | spin_unlock_irqrestore(&sta->ps_tx_buf[ac].lock, flags); |
948d887d JB |
1221 | tmp = skb_queue_len(&pending); |
1222 | buffered += tmp - count; | |
1223 | } | |
1224 | ||
e3685e03 | 1225 | ieee80211_add_pending_skbs(local, &pending); |
5ac2e350 JB |
1226 | |
1227 | /* now we're no longer in the deliver code */ | |
1228 | clear_sta_flag(sta, WLAN_STA_PS_DELIVER); | |
1229 | ||
1230 | /* The station might have polled and then woken up before we responded, | |
1231 | * so clear these flags now to avoid them sticking around. | |
1232 | */ | |
1233 | clear_sta_flag(sta, WLAN_STA_PSPOLL); | |
1234 | clear_sta_flag(sta, WLAN_STA_UAPSD); | |
1d147bfa | 1235 | spin_unlock(&sta->ps_lock); |
948d887d | 1236 | |
e3685e03 JB |
1237 | atomic_dec(&ps->num_sta_ps); |
1238 | ||
687da132 | 1239 | /* This station just woke up and isn't aware of our SMPS state */ |
062f1d6d CYY |
1240 | if (!ieee80211_vif_is_mesh(&sdata->vif) && |
1241 | !ieee80211_smps_is_restrictive(sta->known_smps_mode, | |
687da132 EG |
1242 | sdata->smps_mode) && |
1243 | sta->known_smps_mode != sdata->bss->req_smps && | |
1244 | sta_info_tx_streams(sta) != 1) { | |
1245 | ht_dbg(sdata, | |
1246 | "%pM just woke up and MIMO capable - update SMPS\n", | |
1247 | sta->sta.addr); | |
1248 | ieee80211_send_smps_action(sdata, sdata->bss->req_smps, | |
1249 | sta->sta.addr, | |
1250 | sdata->vif.bss_conf.bssid); | |
1251 | } | |
1252 | ||
af818581 JB |
1253 | local->total_ps_buffered -= buffered; |
1254 | ||
c868cb35 JB |
1255 | sta_info_recalc_tim(sta); |
1256 | ||
bdcbd8e0 JB |
1257 | ps_dbg(sdata, |
1258 | "STA %pM aid %d sending %d filtered/%d PS frames since STA not sleeping anymore\n", | |
1259 | sta->sta.addr, sta->sta.aid, filtered, buffered); | |
17c18bf8 JB |
1260 | |
1261 | ieee80211_check_fast_xmit(sta); | |
af818581 JB |
1262 | } |
1263 | ||
0ead2510 | 1264 | static void ieee80211_send_null_response(struct sta_info *sta, int tid, |
b77cf4f8 | 1265 | enum ieee80211_frame_release_type reason, |
0ead2510 | 1266 | bool call_driver, bool more_data) |
af818581 | 1267 | { |
0ead2510 | 1268 | struct ieee80211_sub_if_data *sdata = sta->sdata; |
af818581 | 1269 | struct ieee80211_local *local = sdata->local; |
ce662b44 | 1270 | struct ieee80211_qos_hdr *nullfunc; |
af818581 | 1271 | struct sk_buff *skb; |
ce662b44 JB |
1272 | int size = sizeof(*nullfunc); |
1273 | __le16 fc; | |
a74a8c84 | 1274 | bool qos = sta->sta.wme; |
ce662b44 | 1275 | struct ieee80211_tx_info *info; |
55de908a | 1276 | struct ieee80211_chanctx_conf *chanctx_conf; |
af818581 | 1277 | |
ce662b44 JB |
1278 | if (qos) { |
1279 | fc = cpu_to_le16(IEEE80211_FTYPE_DATA | | |
1280 | IEEE80211_STYPE_QOS_NULLFUNC | | |
1281 | IEEE80211_FCTL_FROMDS); | |
1282 | } else { | |
1283 | size -= 2; | |
1284 | fc = cpu_to_le16(IEEE80211_FTYPE_DATA | | |
1285 | IEEE80211_STYPE_NULLFUNC | | |
1286 | IEEE80211_FCTL_FROMDS); | |
af818581 | 1287 | } |
af818581 | 1288 | |
ce662b44 JB |
1289 | skb = dev_alloc_skb(local->hw.extra_tx_headroom + size); |
1290 | if (!skb) | |
1291 | return; | |
1292 | ||
1293 | skb_reserve(skb, local->hw.extra_tx_headroom); | |
1294 | ||
1295 | nullfunc = (void *) skb_put(skb, size); | |
1296 | nullfunc->frame_control = fc; | |
1297 | nullfunc->duration_id = 0; | |
1298 | memcpy(nullfunc->addr1, sta->sta.addr, ETH_ALEN); | |
1299 | memcpy(nullfunc->addr2, sdata->vif.addr, ETH_ALEN); | |
1300 | memcpy(nullfunc->addr3, sdata->vif.addr, ETH_ALEN); | |
864a6040 | 1301 | nullfunc->seq_ctrl = 0; |
ce662b44 | 1302 | |
59b66255 JB |
1303 | skb->priority = tid; |
1304 | skb_set_queue_mapping(skb, ieee802_1d_to_ac[tid]); | |
ce662b44 | 1305 | if (qos) { |
ce662b44 JB |
1306 | nullfunc->qos_ctrl = cpu_to_le16(tid); |
1307 | ||
0ead2510 | 1308 | if (reason == IEEE80211_FRAME_RELEASE_UAPSD) { |
ce662b44 JB |
1309 | nullfunc->qos_ctrl |= |
1310 | cpu_to_le16(IEEE80211_QOS_CTL_EOSP); | |
0ead2510 EG |
1311 | if (more_data) |
1312 | nullfunc->frame_control |= | |
1313 | cpu_to_le16(IEEE80211_FCTL_MOREDATA); | |
1314 | } | |
ce662b44 JB |
1315 | } |
1316 | ||
1317 | info = IEEE80211_SKB_CB(skb); | |
1318 | ||
1319 | /* | |
1320 | * Tell TX path to send this frame even though the | |
1321 | * STA may still remain is PS mode after this frame | |
deeaee19 JB |
1322 | * exchange. Also set EOSP to indicate this packet |
1323 | * ends the poll/service period. | |
ce662b44 | 1324 | */ |
02f2f1a9 | 1325 | info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER | |
deeaee19 JB |
1326 | IEEE80211_TX_STATUS_EOSP | |
1327 | IEEE80211_TX_CTL_REQ_TX_STATUS; | |
ce662b44 | 1328 | |
6b127c71 SM |
1329 | info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE; |
1330 | ||
b77cf4f8 JB |
1331 | if (call_driver) |
1332 | drv_allow_buffered_frames(local, sta, BIT(tid), 1, | |
1333 | reason, false); | |
40b96408 | 1334 | |
89afe614 JB |
1335 | skb->dev = sdata->dev; |
1336 | ||
55de908a JB |
1337 | rcu_read_lock(); |
1338 | chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf); | |
1339 | if (WARN_ON(!chanctx_conf)) { | |
1340 | rcu_read_unlock(); | |
1341 | kfree_skb(skb); | |
1342 | return; | |
1343 | } | |
1344 | ||
73c4e195 | 1345 | info->band = chanctx_conf->def.chan->band; |
7c10770f | 1346 | ieee80211_xmit(sdata, sta, skb); |
55de908a | 1347 | rcu_read_unlock(); |
ce662b44 JB |
1348 | } |
1349 | ||
0a1cb809 JB |
1350 | static int find_highest_prio_tid(unsigned long tids) |
1351 | { | |
1352 | /* lower 3 TIDs aren't ordered perfectly */ | |
1353 | if (tids & 0xF8) | |
1354 | return fls(tids) - 1; | |
1355 | /* TID 0 is BE just like TID 3 */ | |
1356 | if (tids & BIT(0)) | |
1357 | return 0; | |
1358 | return fls(tids) - 1; | |
1359 | } | |
1360 | ||
0ead2510 EG |
1361 | /* Indicates if the MORE_DATA bit should be set in the last |
1362 | * frame obtained by ieee80211_sta_ps_get_frames. | |
1363 | * Note that driver_release_tids is relevant only if | |
1364 | * reason = IEEE80211_FRAME_RELEASE_PSPOLL | |
1365 | */ | |
1366 | static bool | |
1367 | ieee80211_sta_ps_more_data(struct sta_info *sta, u8 ignored_acs, | |
1368 | enum ieee80211_frame_release_type reason, | |
1369 | unsigned long driver_release_tids) | |
1370 | { | |
1371 | int ac; | |
1372 | ||
1373 | /* If the driver has data on more than one TID then | |
1374 | * certainly there's more data if we release just a | |
1375 | * single frame now (from a single TID). This will | |
1376 | * only happen for PS-Poll. | |
1377 | */ | |
1378 | if (reason == IEEE80211_FRAME_RELEASE_PSPOLL && | |
1379 | hweight16(driver_release_tids) > 1) | |
1380 | return true; | |
1381 | ||
1382 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { | |
1383 | if (ignored_acs & BIT(ac)) | |
1384 | continue; | |
1385 | ||
1386 | if (!skb_queue_empty(&sta->tx_filtered[ac]) || | |
1387 | !skb_queue_empty(&sta->ps_tx_buf[ac])) | |
1388 | return true; | |
1389 | } | |
1390 | ||
1391 | return false; | |
1392 | } | |
1393 | ||
47086fc5 | 1394 | static void |
0ead2510 EG |
1395 | ieee80211_sta_ps_get_frames(struct sta_info *sta, int n_frames, u8 ignored_acs, |
1396 | enum ieee80211_frame_release_type reason, | |
1397 | struct sk_buff_head *frames, | |
1398 | unsigned long *driver_release_tids) | |
af818581 JB |
1399 | { |
1400 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
1401 | struct ieee80211_local *local = sdata->local; | |
948d887d | 1402 | int ac; |
948d887d | 1403 | |
f9f760b4 | 1404 | /* Get response frame(s) and more data bit for the last one. */ |
948d887d | 1405 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { |
4049e09a JB |
1406 | unsigned long tids; |
1407 | ||
47086fc5 | 1408 | if (ignored_acs & BIT(ac)) |
948d887d JB |
1409 | continue; |
1410 | ||
4049e09a JB |
1411 | tids = ieee80211_tids_for_ac(ac); |
1412 | ||
f9f760b4 JB |
1413 | /* if we already have frames from software, then we can't also |
1414 | * release from hardware queues | |
1415 | */ | |
0ead2510 EG |
1416 | if (skb_queue_empty(frames)) { |
1417 | *driver_release_tids |= | |
1418 | sta->driver_buffered_tids & tids; | |
1419 | *driver_release_tids |= sta->txq_buffered_tids & tids; | |
ba8c3d6f | 1420 | } |
948d887d | 1421 | |
0ead2510 | 1422 | if (!*driver_release_tids) { |
f9f760b4 JB |
1423 | struct sk_buff *skb; |
1424 | ||
1425 | while (n_frames > 0) { | |
1426 | skb = skb_dequeue(&sta->tx_filtered[ac]); | |
1427 | if (!skb) { | |
1428 | skb = skb_dequeue( | |
1429 | &sta->ps_tx_buf[ac]); | |
1430 | if (skb) | |
1431 | local->total_ps_buffered--; | |
1432 | } | |
1433 | if (!skb) | |
1434 | break; | |
1435 | n_frames--; | |
0ead2510 | 1436 | __skb_queue_tail(frames, skb); |
f9f760b4 | 1437 | } |
4049e09a | 1438 | } |
948d887d | 1439 | |
0ead2510 EG |
1440 | /* If we have more frames buffered on this AC, then abort the |
1441 | * loop since we can't send more data from other ACs before | |
1442 | * the buffered frames from this. | |
f9f760b4 | 1443 | */ |
948d887d | 1444 | if (!skb_queue_empty(&sta->tx_filtered[ac]) || |
0ead2510 | 1445 | !skb_queue_empty(&sta->ps_tx_buf[ac])) |
948d887d | 1446 | break; |
af818581 | 1447 | } |
0ead2510 EG |
1448 | } |
1449 | ||
1450 | static void | |
1451 | ieee80211_sta_ps_deliver_response(struct sta_info *sta, | |
1452 | int n_frames, u8 ignored_acs, | |
1453 | enum ieee80211_frame_release_type reason) | |
1454 | { | |
1455 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
1456 | struct ieee80211_local *local = sdata->local; | |
1457 | unsigned long driver_release_tids = 0; | |
1458 | struct sk_buff_head frames; | |
1459 | bool more_data; | |
1460 | ||
1461 | /* Service or PS-Poll period starts */ | |
1462 | set_sta_flag(sta, WLAN_STA_SP); | |
1463 | ||
1464 | __skb_queue_head_init(&frames); | |
1465 | ||
1466 | ieee80211_sta_ps_get_frames(sta, n_frames, ignored_acs, reason, | |
1467 | &frames, &driver_release_tids); | |
1468 | ||
1469 | more_data = ieee80211_sta_ps_more_data(sta, ignored_acs, reason, driver_release_tids); | |
1470 | ||
1a57081a | 1471 | if (driver_release_tids && reason == IEEE80211_FRAME_RELEASE_PSPOLL) |
0ead2510 EG |
1472 | driver_release_tids = |
1473 | BIT(find_highest_prio_tid(driver_release_tids)); | |
af818581 | 1474 | |
f9f760b4 | 1475 | if (skb_queue_empty(&frames) && !driver_release_tids) { |
ce662b44 | 1476 | int tid; |
af818581 JB |
1477 | |
1478 | /* | |
ce662b44 JB |
1479 | * For PS-Poll, this can only happen due to a race condition |
1480 | * when we set the TIM bit and the station notices it, but | |
1481 | * before it can poll for the frame we expire it. | |
1482 | * | |
1483 | * For uAPSD, this is said in the standard (11.2.1.5 h): | |
1484 | * At each unscheduled SP for a non-AP STA, the AP shall | |
1485 | * attempt to transmit at least one MSDU or MMPDU, but no | |
1486 | * more than the value specified in the Max SP Length field | |
1487 | * in the QoS Capability element from delivery-enabled ACs, | |
1488 | * that are destined for the non-AP STA. | |
1489 | * | |
1490 | * Since we have no other MSDU/MMPDU, transmit a QoS null frame. | |
af818581 | 1491 | */ |
af818581 | 1492 | |
ce662b44 JB |
1493 | /* This will evaluate to 1, 3, 5 or 7. */ |
1494 | tid = 7 - ((ffs(~ignored_acs) - 1) << 1); | |
af818581 | 1495 | |
0ead2510 | 1496 | ieee80211_send_null_response(sta, tid, reason, true, false); |
f9f760b4 | 1497 | } else if (!driver_release_tids) { |
47086fc5 JB |
1498 | struct sk_buff_head pending; |
1499 | struct sk_buff *skb; | |
40b96408 JB |
1500 | int num = 0; |
1501 | u16 tids = 0; | |
b77cf4f8 | 1502 | bool need_null = false; |
af818581 | 1503 | |
47086fc5 | 1504 | skb_queue_head_init(&pending); |
af818581 | 1505 | |
47086fc5 JB |
1506 | while ((skb = __skb_dequeue(&frames))) { |
1507 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); | |
1508 | struct ieee80211_hdr *hdr = (void *) skb->data; | |
40b96408 JB |
1509 | u8 *qoshdr = NULL; |
1510 | ||
1511 | num++; | |
af818581 | 1512 | |
47086fc5 JB |
1513 | /* |
1514 | * Tell TX path to send this frame even though the | |
1515 | * STA may still remain is PS mode after this frame | |
1516 | * exchange. | |
1517 | */ | |
6b127c71 SM |
1518 | info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER; |
1519 | info->control.flags |= IEEE80211_TX_CTRL_PS_RESPONSE; | |
47086fc5 JB |
1520 | |
1521 | /* | |
1522 | * Use MoreData flag to indicate whether there are | |
1523 | * more buffered frames for this STA | |
1524 | */ | |
24b9c373 | 1525 | if (more_data || !skb_queue_empty(&frames)) |
47086fc5 JB |
1526 | hdr->frame_control |= |
1527 | cpu_to_le16(IEEE80211_FCTL_MOREDATA); | |
24b9c373 JD |
1528 | else |
1529 | hdr->frame_control &= | |
1530 | cpu_to_le16(~IEEE80211_FCTL_MOREDATA); | |
47086fc5 | 1531 | |
40b96408 JB |
1532 | if (ieee80211_is_data_qos(hdr->frame_control) || |
1533 | ieee80211_is_qos_nullfunc(hdr->frame_control)) | |
1534 | qoshdr = ieee80211_get_qos_ctl(hdr); | |
1535 | ||
b77cf4f8 | 1536 | tids |= BIT(skb->priority); |
52a3f20c | 1537 | |
b77cf4f8 JB |
1538 | __skb_queue_tail(&pending, skb); |
1539 | ||
1540 | /* end service period after last frame or add one */ | |
1541 | if (!skb_queue_empty(&frames)) | |
1542 | continue; | |
1543 | ||
1544 | if (reason != IEEE80211_FRAME_RELEASE_UAPSD) { | |
1545 | /* for PS-Poll, there's only one frame */ | |
52a3f20c MP |
1546 | info->flags |= IEEE80211_TX_STATUS_EOSP | |
1547 | IEEE80211_TX_CTL_REQ_TX_STATUS; | |
b77cf4f8 | 1548 | break; |
52a3f20c | 1549 | } |
deeaee19 | 1550 | |
b77cf4f8 JB |
1551 | /* For uAPSD, things are a bit more complicated. If the |
1552 | * last frame has a QoS header (i.e. is a QoS-data or | |
1553 | * QoS-nulldata frame) then just set the EOSP bit there | |
1554 | * and be done. | |
1555 | * If the frame doesn't have a QoS header (which means | |
1556 | * it should be a bufferable MMPDU) then we can't set | |
1557 | * the EOSP bit in the QoS header; add a QoS-nulldata | |
1558 | * frame to the list to send it after the MMPDU. | |
1559 | * | |
1560 | * Note that this code is only in the mac80211-release | |
1561 | * code path, we assume that the driver will not buffer | |
1562 | * anything but QoS-data frames, or if it does, will | |
1563 | * create the QoS-nulldata frame by itself if needed. | |
1564 | * | |
1565 | * Cf. 802.11-2012 10.2.1.10 (c). | |
1566 | */ | |
1567 | if (qoshdr) { | |
1568 | *qoshdr |= IEEE80211_QOS_CTL_EOSP; | |
40b96408 | 1569 | |
b77cf4f8 JB |
1570 | info->flags |= IEEE80211_TX_STATUS_EOSP | |
1571 | IEEE80211_TX_CTL_REQ_TX_STATUS; | |
1572 | } else { | |
1573 | /* The standard isn't completely clear on this | |
1574 | * as it says the more-data bit should be set | |
1575 | * if there are more BUs. The QoS-Null frame | |
1576 | * we're about to send isn't buffered yet, we | |
1577 | * only create it below, but let's pretend it | |
1578 | * was buffered just in case some clients only | |
1579 | * expect more-data=0 when eosp=1. | |
1580 | */ | |
1581 | hdr->frame_control |= | |
1582 | cpu_to_le16(IEEE80211_FCTL_MOREDATA); | |
1583 | need_null = true; | |
1584 | num++; | |
1585 | } | |
1586 | break; | |
47086fc5 | 1587 | } |
af818581 | 1588 | |
40b96408 JB |
1589 | drv_allow_buffered_frames(local, sta, tids, num, |
1590 | reason, more_data); | |
1591 | ||
47086fc5 | 1592 | ieee80211_add_pending_skbs(local, &pending); |
af818581 | 1593 | |
b77cf4f8 JB |
1594 | if (need_null) |
1595 | ieee80211_send_null_response( | |
0ead2510 EG |
1596 | sta, find_highest_prio_tid(tids), |
1597 | reason, false, false); | |
b77cf4f8 | 1598 | |
c868cb35 | 1599 | sta_info_recalc_tim(sta); |
af818581 | 1600 | } else { |
ba8c3d6f FF |
1601 | unsigned long tids = sta->txq_buffered_tids & driver_release_tids; |
1602 | int tid; | |
1603 | ||
af818581 | 1604 | /* |
4049e09a JB |
1605 | * We need to release a frame that is buffered somewhere in the |
1606 | * driver ... it'll have to handle that. | |
f9f760b4 JB |
1607 | * Note that the driver also has to check the number of frames |
1608 | * on the TIDs we're releasing from - if there are more than | |
1609 | * n_frames it has to set the more-data bit (if we didn't ask | |
1610 | * it to set it anyway due to other buffered frames); if there | |
1611 | * are fewer than n_frames it has to make sure to adjust that | |
1612 | * to allow the service period to end properly. | |
4049e09a JB |
1613 | */ |
1614 | drv_release_buffered_frames(local, sta, driver_release_tids, | |
47086fc5 | 1615 | n_frames, reason, more_data); |
4049e09a JB |
1616 | |
1617 | /* | |
1618 | * Note that we don't recalculate the TIM bit here as it would | |
1619 | * most likely have no effect at all unless the driver told us | |
f9f760b4 | 1620 | * that the TID(s) became empty before returning here from the |
4049e09a | 1621 | * release function. |
f9f760b4 | 1622 | * Either way, however, when the driver tells us that the TID(s) |
ba8c3d6f FF |
1623 | * became empty or we find that a txq became empty, we'll do the |
1624 | * TIM recalculation. | |
af818581 | 1625 | */ |
ba8c3d6f FF |
1626 | |
1627 | if (!sta->sta.txq[0]) | |
1628 | return; | |
1629 | ||
1630 | for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) { | |
1631 | struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]); | |
1632 | ||
1633 | if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue)) | |
1634 | continue; | |
1635 | ||
1636 | sta_info_recalc_tim(sta); | |
1637 | break; | |
1638 | } | |
af818581 JB |
1639 | } |
1640 | } | |
1641 | ||
47086fc5 JB |
1642 | void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta) |
1643 | { | |
1644 | u8 ignore_for_response = sta->sta.uapsd_queues; | |
1645 | ||
1646 | /* | |
1647 | * If all ACs are delivery-enabled then we should reply | |
1648 | * from any of them, if only some are enabled we reply | |
1649 | * only from the non-enabled ones. | |
1650 | */ | |
1651 | if (ignore_for_response == BIT(IEEE80211_NUM_ACS) - 1) | |
1652 | ignore_for_response = 0; | |
1653 | ||
1654 | ieee80211_sta_ps_deliver_response(sta, 1, ignore_for_response, | |
1655 | IEEE80211_FRAME_RELEASE_PSPOLL); | |
1656 | } | |
1657 | ||
1658 | void ieee80211_sta_ps_deliver_uapsd(struct sta_info *sta) | |
1659 | { | |
1660 | int n_frames = sta->sta.max_sp; | |
1661 | u8 delivery_enabled = sta->sta.uapsd_queues; | |
1662 | ||
1663 | /* | |
1664 | * If we ever grow support for TSPEC this might happen if | |
1665 | * the TSPEC update from hostapd comes in between a trigger | |
1666 | * frame setting WLAN_STA_UAPSD in the RX path and this | |
1667 | * actually getting called. | |
1668 | */ | |
1669 | if (!delivery_enabled) | |
1670 | return; | |
1671 | ||
47086fc5 JB |
1672 | switch (sta->sta.max_sp) { |
1673 | case 1: | |
1674 | n_frames = 2; | |
1675 | break; | |
1676 | case 2: | |
1677 | n_frames = 4; | |
1678 | break; | |
1679 | case 3: | |
1680 | n_frames = 6; | |
1681 | break; | |
1682 | case 0: | |
1683 | /* XXX: what is a good value? */ | |
13a8098a | 1684 | n_frames = 128; |
47086fc5 JB |
1685 | break; |
1686 | } | |
1687 | ||
1688 | ieee80211_sta_ps_deliver_response(sta, n_frames, ~delivery_enabled, | |
1689 | IEEE80211_FRAME_RELEASE_UAPSD); | |
1690 | } | |
1691 | ||
af818581 JB |
1692 | void ieee80211_sta_block_awake(struct ieee80211_hw *hw, |
1693 | struct ieee80211_sta *pubsta, bool block) | |
1694 | { | |
1695 | struct sta_info *sta = container_of(pubsta, struct sta_info, sta); | |
1696 | ||
b5878a2d JB |
1697 | trace_api_sta_block_awake(sta->local, pubsta, block); |
1698 | ||
5ac2e350 | 1699 | if (block) { |
c2c98fde | 1700 | set_sta_flag(sta, WLAN_STA_PS_DRIVER); |
17c18bf8 | 1701 | ieee80211_clear_fast_xmit(sta); |
5ac2e350 JB |
1702 | return; |
1703 | } | |
1704 | ||
1705 | if (!test_sta_flag(sta, WLAN_STA_PS_DRIVER)) | |
1706 | return; | |
1707 | ||
1708 | if (!test_sta_flag(sta, WLAN_STA_PS_STA)) { | |
1709 | set_sta_flag(sta, WLAN_STA_PS_DELIVER); | |
1710 | clear_sta_flag(sta, WLAN_STA_PS_DRIVER); | |
1711 | ieee80211_queue_work(hw, &sta->drv_deliver_wk); | |
1712 | } else if (test_sta_flag(sta, WLAN_STA_PSPOLL) || | |
1713 | test_sta_flag(sta, WLAN_STA_UAPSD)) { | |
1714 | /* must be asleep in this case */ | |
1715 | clear_sta_flag(sta, WLAN_STA_PS_DRIVER); | |
1716 | ieee80211_queue_work(hw, &sta->drv_deliver_wk); | |
1717 | } else { | |
1718 | clear_sta_flag(sta, WLAN_STA_PS_DRIVER); | |
17c18bf8 | 1719 | ieee80211_check_fast_xmit(sta); |
5ac2e350 | 1720 | } |
af818581 JB |
1721 | } |
1722 | EXPORT_SYMBOL(ieee80211_sta_block_awake); | |
dcf55fb5 | 1723 | |
e943789e | 1724 | void ieee80211_sta_eosp(struct ieee80211_sta *pubsta) |
37fbd908 JB |
1725 | { |
1726 | struct sta_info *sta = container_of(pubsta, struct sta_info, sta); | |
1727 | struct ieee80211_local *local = sta->local; | |
37fbd908 JB |
1728 | |
1729 | trace_api_eosp(local, pubsta); | |
1730 | ||
e943789e | 1731 | clear_sta_flag(sta, WLAN_STA_SP); |
37fbd908 | 1732 | } |
e943789e | 1733 | EXPORT_SYMBOL(ieee80211_sta_eosp); |
37fbd908 | 1734 | |
0ead2510 EG |
1735 | void ieee80211_send_eosp_nullfunc(struct ieee80211_sta *pubsta, int tid) |
1736 | { | |
1737 | struct sta_info *sta = container_of(pubsta, struct sta_info, sta); | |
1738 | enum ieee80211_frame_release_type reason; | |
1739 | bool more_data; | |
1740 | ||
1741 | trace_api_send_eosp_nullfunc(sta->local, pubsta, tid); | |
1742 | ||
1743 | reason = IEEE80211_FRAME_RELEASE_UAPSD; | |
1744 | more_data = ieee80211_sta_ps_more_data(sta, ~sta->sta.uapsd_queues, | |
1745 | reason, 0); | |
1746 | ||
1747 | ieee80211_send_null_response(sta, tid, reason, false, more_data); | |
1748 | } | |
1749 | EXPORT_SYMBOL(ieee80211_send_eosp_nullfunc); | |
1750 | ||
042ec453 JB |
1751 | void ieee80211_sta_set_buffered(struct ieee80211_sta *pubsta, |
1752 | u8 tid, bool buffered) | |
dcf55fb5 FF |
1753 | { |
1754 | struct sta_info *sta = container_of(pubsta, struct sta_info, sta); | |
1755 | ||
5a306f58 | 1756 | if (WARN_ON(tid >= IEEE80211_NUM_TIDS)) |
042ec453 JB |
1757 | return; |
1758 | ||
1b000789 JB |
1759 | trace_api_sta_set_buffered(sta->local, pubsta, tid, buffered); |
1760 | ||
948d887d JB |
1761 | if (buffered) |
1762 | set_bit(tid, &sta->driver_buffered_tids); | |
1763 | else | |
1764 | clear_bit(tid, &sta->driver_buffered_tids); | |
1765 | ||
c868cb35 | 1766 | sta_info_recalc_tim(sta); |
dcf55fb5 | 1767 | } |
042ec453 | 1768 | EXPORT_SYMBOL(ieee80211_sta_set_buffered); |
d9a7ddb0 | 1769 | |
83d5cc01 JB |
1770 | int sta_info_move_state(struct sta_info *sta, |
1771 | enum ieee80211_sta_state new_state) | |
d9a7ddb0 | 1772 | { |
8bf11d8d | 1773 | might_sleep(); |
d9a7ddb0 JB |
1774 | |
1775 | if (sta->sta_state == new_state) | |
1776 | return 0; | |
1777 | ||
f09603a2 JB |
1778 | /* check allowed transitions first */ |
1779 | ||
1780 | switch (new_state) { | |
1781 | case IEEE80211_STA_NONE: | |
1782 | if (sta->sta_state != IEEE80211_STA_AUTH) | |
1783 | return -EINVAL; | |
1784 | break; | |
1785 | case IEEE80211_STA_AUTH: | |
1786 | if (sta->sta_state != IEEE80211_STA_NONE && | |
1787 | sta->sta_state != IEEE80211_STA_ASSOC) | |
1788 | return -EINVAL; | |
1789 | break; | |
1790 | case IEEE80211_STA_ASSOC: | |
1791 | if (sta->sta_state != IEEE80211_STA_AUTH && | |
1792 | sta->sta_state != IEEE80211_STA_AUTHORIZED) | |
1793 | return -EINVAL; | |
1794 | break; | |
1795 | case IEEE80211_STA_AUTHORIZED: | |
1796 | if (sta->sta_state != IEEE80211_STA_ASSOC) | |
1797 | return -EINVAL; | |
1798 | break; | |
1799 | default: | |
1800 | WARN(1, "invalid state %d", new_state); | |
1801 | return -EINVAL; | |
1802 | } | |
1803 | ||
bdcbd8e0 JB |
1804 | sta_dbg(sta->sdata, "moving STA %pM to state %d\n", |
1805 | sta->sta.addr, new_state); | |
f09603a2 JB |
1806 | |
1807 | /* | |
1808 | * notify the driver before the actual changes so it can | |
1809 | * fail the transition | |
1810 | */ | |
1811 | if (test_sta_flag(sta, WLAN_STA_INSERTED)) { | |
1812 | int err = drv_sta_state(sta->local, sta->sdata, sta, | |
1813 | sta->sta_state, new_state); | |
1814 | if (err) | |
1815 | return err; | |
1816 | } | |
1817 | ||
1818 | /* reflect the change in all state variables */ | |
1819 | ||
d9a7ddb0 JB |
1820 | switch (new_state) { |
1821 | case IEEE80211_STA_NONE: | |
1822 | if (sta->sta_state == IEEE80211_STA_AUTH) | |
1823 | clear_bit(WLAN_STA_AUTH, &sta->_flags); | |
d9a7ddb0 JB |
1824 | break; |
1825 | case IEEE80211_STA_AUTH: | |
a7201a6c | 1826 | if (sta->sta_state == IEEE80211_STA_NONE) { |
d9a7ddb0 | 1827 | set_bit(WLAN_STA_AUTH, &sta->_flags); |
a7201a6c | 1828 | } else if (sta->sta_state == IEEE80211_STA_ASSOC) { |
d9a7ddb0 | 1829 | clear_bit(WLAN_STA_ASSOC, &sta->_flags); |
a7201a6c IP |
1830 | ieee80211_recalc_min_chandef(sta->sdata); |
1831 | } | |
d9a7ddb0 JB |
1832 | break; |
1833 | case IEEE80211_STA_ASSOC: | |
29623892 | 1834 | if (sta->sta_state == IEEE80211_STA_AUTH) { |
d9a7ddb0 | 1835 | set_bit(WLAN_STA_ASSOC, &sta->_flags); |
a7201a6c | 1836 | ieee80211_recalc_min_chandef(sta->sdata); |
29623892 | 1837 | } else if (sta->sta_state == IEEE80211_STA_AUTHORIZED) { |
7e3ed02c FF |
1838 | if (sta->sdata->vif.type == NL80211_IFTYPE_AP || |
1839 | (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && | |
1840 | !sta->sdata->u.vlan.sta)) | |
1841 | atomic_dec(&sta->sdata->bss->num_mcast_sta); | |
d9a7ddb0 | 1842 | clear_bit(WLAN_STA_AUTHORIZED, &sta->_flags); |
17c18bf8 | 1843 | ieee80211_clear_fast_xmit(sta); |
f09603a2 | 1844 | } |
d9a7ddb0 JB |
1845 | break; |
1846 | case IEEE80211_STA_AUTHORIZED: | |
29623892 | 1847 | if (sta->sta_state == IEEE80211_STA_ASSOC) { |
7e3ed02c FF |
1848 | if (sta->sdata->vif.type == NL80211_IFTYPE_AP || |
1849 | (sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN && | |
1850 | !sta->sdata->u.vlan.sta)) | |
1851 | atomic_inc(&sta->sdata->bss->num_mcast_sta); | |
d9a7ddb0 | 1852 | set_bit(WLAN_STA_AUTHORIZED, &sta->_flags); |
17c18bf8 | 1853 | ieee80211_check_fast_xmit(sta); |
f09603a2 | 1854 | } |
d9a7ddb0 JB |
1855 | break; |
1856 | default: | |
f09603a2 | 1857 | break; |
d9a7ddb0 JB |
1858 | } |
1859 | ||
d9a7ddb0 JB |
1860 | sta->sta_state = new_state; |
1861 | ||
1862 | return 0; | |
1863 | } | |
687da132 EG |
1864 | |
1865 | u8 sta_info_tx_streams(struct sta_info *sta) | |
1866 | { | |
1867 | struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap; | |
1868 | u8 rx_streams; | |
1869 | ||
1870 | if (!sta->sta.ht_cap.ht_supported) | |
1871 | return 1; | |
1872 | ||
1873 | if (sta->sta.vht_cap.vht_supported) { | |
1874 | int i; | |
1875 | u16 tx_mcs_map = | |
1876 | le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map); | |
1877 | ||
1878 | for (i = 7; i >= 0; i--) | |
1879 | if ((tx_mcs_map & (0x3 << (i * 2))) != | |
1880 | IEEE80211_VHT_MCS_NOT_SUPPORTED) | |
1881 | return i + 1; | |
1882 | } | |
1883 | ||
1884 | if (ht_cap->mcs.rx_mask[3]) | |
1885 | rx_streams = 4; | |
1886 | else if (ht_cap->mcs.rx_mask[2]) | |
1887 | rx_streams = 3; | |
1888 | else if (ht_cap->mcs.rx_mask[1]) | |
1889 | rx_streams = 2; | |
1890 | else | |
1891 | rx_streams = 1; | |
1892 | ||
1893 | if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF)) | |
1894 | return rx_streams; | |
1895 | ||
1896 | return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK) | |
1897 | >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1; | |
1898 | } | |
b7ffbd7e | 1899 | |
fbd6ff5c JB |
1900 | static void sta_set_rate_info_rx(struct sta_info *sta, struct rate_info *rinfo) |
1901 | { | |
1902 | rinfo->flags = 0; | |
1903 | ||
e5a9f8d0 | 1904 | if (sta->rx_stats.last_rate_flag & RX_FLAG_HT) { |
fbd6ff5c | 1905 | rinfo->flags |= RATE_INFO_FLAGS_MCS; |
e5a9f8d0 JB |
1906 | rinfo->mcs = sta->rx_stats.last_rate_idx; |
1907 | } else if (sta->rx_stats.last_rate_flag & RX_FLAG_VHT) { | |
fbd6ff5c | 1908 | rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS; |
e5a9f8d0 JB |
1909 | rinfo->nss = sta->rx_stats.last_rate_vht_nss; |
1910 | rinfo->mcs = sta->rx_stats.last_rate_idx; | |
fbd6ff5c JB |
1911 | } else { |
1912 | struct ieee80211_supported_band *sband; | |
1913 | int shift = ieee80211_vif_get_shift(&sta->sdata->vif); | |
1914 | u16 brate; | |
1915 | ||
1916 | sband = sta->local->hw.wiphy->bands[ | |
1917 | ieee80211_get_sdata_band(sta->sdata)]; | |
e5a9f8d0 | 1918 | brate = sband->bitrates[sta->rx_stats.last_rate_idx].bitrate; |
fbd6ff5c JB |
1919 | rinfo->legacy = DIV_ROUND_UP(brate, 1 << shift); |
1920 | } | |
1921 | ||
e5a9f8d0 | 1922 | if (sta->rx_stats.last_rate_flag & RX_FLAG_SHORT_GI) |
fbd6ff5c JB |
1923 | rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI; |
1924 | ||
e5a9f8d0 | 1925 | if (sta->rx_stats.last_rate_flag & RX_FLAG_5MHZ) |
fbd6ff5c | 1926 | rinfo->bw = RATE_INFO_BW_5; |
e5a9f8d0 | 1927 | else if (sta->rx_stats.last_rate_flag & RX_FLAG_10MHZ) |
fbd6ff5c | 1928 | rinfo->bw = RATE_INFO_BW_10; |
e5a9f8d0 | 1929 | else if (sta->rx_stats.last_rate_flag & RX_FLAG_40MHZ) |
fbd6ff5c | 1930 | rinfo->bw = RATE_INFO_BW_40; |
e5a9f8d0 | 1931 | else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_80MHZ) |
fbd6ff5c | 1932 | rinfo->bw = RATE_INFO_BW_80; |
e5a9f8d0 | 1933 | else if (sta->rx_stats.last_rate_vht_flag & RX_VHT_FLAG_160MHZ) |
fbd6ff5c JB |
1934 | rinfo->bw = RATE_INFO_BW_160; |
1935 | else | |
1936 | rinfo->bw = RATE_INFO_BW_20; | |
1937 | } | |
1938 | ||
b7ffbd7e JB |
1939 | void sta_set_sinfo(struct sta_info *sta, struct station_info *sinfo) |
1940 | { | |
1941 | struct ieee80211_sub_if_data *sdata = sta->sdata; | |
1942 | struct ieee80211_local *local = sdata->local; | |
9a244409 | 1943 | struct rate_control_ref *ref = NULL; |
b7ffbd7e JB |
1944 | u32 thr = 0; |
1945 | int i, ac; | |
1946 | ||
9a244409 JL |
1947 | if (test_sta_flag(sta, WLAN_STA_RATE_CONTROL)) |
1948 | ref = local->rate_ctrl; | |
1949 | ||
b7ffbd7e JB |
1950 | sinfo->generation = sdata->local->sta_generation; |
1951 | ||
225b8189 JB |
1952 | /* do before driver, so beacon filtering drivers have a |
1953 | * chance to e.g. just add the number of filtered beacons | |
1954 | * (or just modify the value entirely, of course) | |
1955 | */ | |
1956 | if (sdata->vif.type == NL80211_IFTYPE_STATION) | |
1957 | sinfo->rx_beacon = sdata->u.mgd.count_beacon_signal; | |
1958 | ||
2b9a7e1b JB |
1959 | drv_sta_statistics(local, sdata, &sta->sta, sinfo); |
1960 | ||
319090bf JB |
1961 | sinfo->filled |= BIT(NL80211_STA_INFO_INACTIVE_TIME) | |
1962 | BIT(NL80211_STA_INFO_STA_FLAGS) | | |
1963 | BIT(NL80211_STA_INFO_BSS_PARAM) | | |
1964 | BIT(NL80211_STA_INFO_CONNECTED_TIME) | | |
976bd9ef JB |
1965 | BIT(NL80211_STA_INFO_RX_DROP_MISC); |
1966 | ||
1967 | if (sdata->vif.type == NL80211_IFTYPE_STATION) { | |
1968 | sinfo->beacon_loss_count = sdata->u.mgd.beacon_loss_count; | |
1969 | sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_LOSS); | |
1970 | } | |
b7ffbd7e | 1971 | |
84b00607 | 1972 | sinfo->connected_time = ktime_get_seconds() - sta->last_connected; |
e5a9f8d0 JB |
1973 | sinfo->inactive_time = |
1974 | jiffies_to_msecs(jiffies - sta->rx_stats.last_rx); | |
2b9a7e1b | 1975 | |
319090bf JB |
1976 | if (!(sinfo->filled & (BIT(NL80211_STA_INFO_TX_BYTES64) | |
1977 | BIT(NL80211_STA_INFO_TX_BYTES)))) { | |
2b9a7e1b JB |
1978 | sinfo->tx_bytes = 0; |
1979 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) | |
e5a9f8d0 | 1980 | sinfo->tx_bytes += sta->tx_stats.bytes[ac]; |
319090bf | 1981 | sinfo->filled |= BIT(NL80211_STA_INFO_TX_BYTES64); |
2b9a7e1b JB |
1982 | } |
1983 | ||
319090bf | 1984 | if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_PACKETS))) { |
2b9a7e1b JB |
1985 | sinfo->tx_packets = 0; |
1986 | for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) | |
e5a9f8d0 | 1987 | sinfo->tx_packets += sta->tx_stats.packets[ac]; |
319090bf | 1988 | sinfo->filled |= BIT(NL80211_STA_INFO_TX_PACKETS); |
2b9a7e1b JB |
1989 | } |
1990 | ||
319090bf JB |
1991 | if (!(sinfo->filled & (BIT(NL80211_STA_INFO_RX_BYTES64) | |
1992 | BIT(NL80211_STA_INFO_RX_BYTES)))) { | |
e5a9f8d0 | 1993 | sinfo->rx_bytes = sta->rx_stats.bytes; |
319090bf | 1994 | sinfo->filled |= BIT(NL80211_STA_INFO_RX_BYTES64); |
2b9a7e1b JB |
1995 | } |
1996 | ||
319090bf | 1997 | if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_PACKETS))) { |
e5a9f8d0 | 1998 | sinfo->rx_packets = sta->rx_stats.packets; |
319090bf | 1999 | sinfo->filled |= BIT(NL80211_STA_INFO_RX_PACKETS); |
2b9a7e1b JB |
2000 | } |
2001 | ||
319090bf | 2002 | if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_RETRIES))) { |
e5a9f8d0 | 2003 | sinfo->tx_retries = sta->status_stats.retry_count; |
319090bf | 2004 | sinfo->filled |= BIT(NL80211_STA_INFO_TX_RETRIES); |
2b9a7e1b JB |
2005 | } |
2006 | ||
319090bf | 2007 | if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_FAILED))) { |
e5a9f8d0 | 2008 | sinfo->tx_failed = sta->status_stats.retry_failed; |
319090bf | 2009 | sinfo->filled |= BIT(NL80211_STA_INFO_TX_FAILED); |
b7ffbd7e | 2010 | } |
2b9a7e1b | 2011 | |
e5a9f8d0 | 2012 | sinfo->rx_dropped_misc = sta->rx_stats.dropped; |
b7ffbd7e | 2013 | |
225b8189 JB |
2014 | if (sdata->vif.type == NL80211_IFTYPE_STATION && |
2015 | !(sdata->vif.driver_flags & IEEE80211_VIF_BEACON_FILTER)) { | |
2016 | sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX) | | |
2017 | BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG); | |
2018 | sinfo->rx_beacon_signal_avg = ieee80211_ave_rssi(&sdata->vif); | |
2019 | } | |
2020 | ||
30686bf7 JB |
2021 | if (ieee80211_hw_check(&sta->local->hw, SIGNAL_DBM) || |
2022 | ieee80211_hw_check(&sta->local->hw, SIGNAL_UNSPEC)) { | |
319090bf | 2023 | if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL))) { |
e5a9f8d0 | 2024 | sinfo->signal = (s8)sta->rx_stats.last_signal; |
319090bf | 2025 | sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL); |
2b9a7e1b JB |
2026 | } |
2027 | ||
319090bf | 2028 | if (!(sinfo->filled & BIT(NL80211_STA_INFO_SIGNAL_AVG))) { |
40d9a38a | 2029 | sinfo->signal_avg = |
e5a9f8d0 | 2030 | -ewma_signal_read(&sta->rx_stats.avg_signal); |
319090bf | 2031 | sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG); |
2b9a7e1b | 2032 | } |
b7ffbd7e | 2033 | } |
2b9a7e1b | 2034 | |
e5a9f8d0 | 2035 | if (sta->rx_stats.chains && |
319090bf JB |
2036 | !(sinfo->filled & (BIT(NL80211_STA_INFO_CHAIN_SIGNAL) | |
2037 | BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG)))) { | |
2038 | sinfo->filled |= BIT(NL80211_STA_INFO_CHAIN_SIGNAL) | | |
2039 | BIT(NL80211_STA_INFO_CHAIN_SIGNAL_AVG); | |
b7ffbd7e | 2040 | |
e5a9f8d0 | 2041 | sinfo->chains = sta->rx_stats.chains; |
b7ffbd7e | 2042 | for (i = 0; i < ARRAY_SIZE(sinfo->chain_signal); i++) { |
e5a9f8d0 JB |
2043 | sinfo->chain_signal[i] = |
2044 | sta->rx_stats.chain_signal_last[i]; | |
b7ffbd7e | 2045 | sinfo->chain_signal_avg[i] = |
e5a9f8d0 | 2046 | -ewma_signal_read(&sta->rx_stats.chain_signal_avg[i]); |
b7ffbd7e JB |
2047 | } |
2048 | } | |
2049 | ||
319090bf | 2050 | if (!(sinfo->filled & BIT(NL80211_STA_INFO_TX_BITRATE))) { |
e5a9f8d0 JB |
2051 | sta_set_rate_info_tx(sta, &sta->tx_stats.last_rate, |
2052 | &sinfo->txrate); | |
319090bf | 2053 | sinfo->filled |= BIT(NL80211_STA_INFO_TX_BITRATE); |
2b9a7e1b JB |
2054 | } |
2055 | ||
319090bf | 2056 | if (!(sinfo->filled & BIT(NL80211_STA_INFO_RX_BITRATE))) { |
2b9a7e1b | 2057 | sta_set_rate_info_rx(sta, &sinfo->rxrate); |
319090bf | 2058 | sinfo->filled |= BIT(NL80211_STA_INFO_RX_BITRATE); |
2b9a7e1b | 2059 | } |
b7ffbd7e | 2060 | |
79c892b8 JB |
2061 | sinfo->filled |= BIT(NL80211_STA_INFO_TID_STATS); |
2062 | for (i = 0; i < IEEE80211_NUM_TIDS + 1; i++) { | |
2063 | struct cfg80211_tid_stats *tidstats = &sinfo->pertid[i]; | |
2064 | ||
2065 | if (!(tidstats->filled & BIT(NL80211_TID_STATS_RX_MSDU))) { | |
2066 | tidstats->filled |= BIT(NL80211_TID_STATS_RX_MSDU); | |
e5a9f8d0 | 2067 | tidstats->rx_msdu = sta->rx_stats.msdu[i]; |
79c892b8 JB |
2068 | } |
2069 | ||
2070 | if (!(tidstats->filled & BIT(NL80211_TID_STATS_TX_MSDU))) { | |
2071 | tidstats->filled |= BIT(NL80211_TID_STATS_TX_MSDU); | |
e5a9f8d0 | 2072 | tidstats->tx_msdu = sta->tx_stats.msdu[i]; |
79c892b8 JB |
2073 | } |
2074 | ||
2075 | if (!(tidstats->filled & | |
2076 | BIT(NL80211_TID_STATS_TX_MSDU_RETRIES)) && | |
30686bf7 | 2077 | ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) { |
79c892b8 JB |
2078 | tidstats->filled |= |
2079 | BIT(NL80211_TID_STATS_TX_MSDU_RETRIES); | |
e5a9f8d0 JB |
2080 | tidstats->tx_msdu_retries = |
2081 | sta->status_stats.msdu_retries[i]; | |
79c892b8 JB |
2082 | } |
2083 | ||
2084 | if (!(tidstats->filled & | |
2085 | BIT(NL80211_TID_STATS_TX_MSDU_FAILED)) && | |
30686bf7 | 2086 | ieee80211_hw_check(&local->hw, REPORTS_TX_ACK_STATUS)) { |
79c892b8 JB |
2087 | tidstats->filled |= |
2088 | BIT(NL80211_TID_STATS_TX_MSDU_FAILED); | |
e5a9f8d0 JB |
2089 | tidstats->tx_msdu_failed = |
2090 | sta->status_stats.msdu_failed[i]; | |
79c892b8 JB |
2091 | } |
2092 | } | |
2093 | ||
b7ffbd7e JB |
2094 | if (ieee80211_vif_is_mesh(&sdata->vif)) { |
2095 | #ifdef CONFIG_MAC80211_MESH | |
319090bf JB |
2096 | sinfo->filled |= BIT(NL80211_STA_INFO_LLID) | |
2097 | BIT(NL80211_STA_INFO_PLID) | | |
2098 | BIT(NL80211_STA_INFO_PLINK_STATE) | | |
2099 | BIT(NL80211_STA_INFO_LOCAL_PM) | | |
2100 | BIT(NL80211_STA_INFO_PEER_PM) | | |
2101 | BIT(NL80211_STA_INFO_NONPEER_PM); | |
b7ffbd7e | 2102 | |
433f5bc1 JB |
2103 | sinfo->llid = sta->mesh->llid; |
2104 | sinfo->plid = sta->mesh->plid; | |
2105 | sinfo->plink_state = sta->mesh->plink_state; | |
b7ffbd7e | 2106 | if (test_sta_flag(sta, WLAN_STA_TOFFSET_KNOWN)) { |
319090bf | 2107 | sinfo->filled |= BIT(NL80211_STA_INFO_T_OFFSET); |
433f5bc1 | 2108 | sinfo->t_offset = sta->mesh->t_offset; |
b7ffbd7e | 2109 | } |
433f5bc1 JB |
2110 | sinfo->local_pm = sta->mesh->local_pm; |
2111 | sinfo->peer_pm = sta->mesh->peer_pm; | |
2112 | sinfo->nonpeer_pm = sta->mesh->nonpeer_pm; | |
b7ffbd7e JB |
2113 | #endif |
2114 | } | |
2115 | ||
2116 | sinfo->bss_param.flags = 0; | |
2117 | if (sdata->vif.bss_conf.use_cts_prot) | |
2118 | sinfo->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT; | |
2119 | if (sdata->vif.bss_conf.use_short_preamble) | |
2120 | sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE; | |
2121 | if (sdata->vif.bss_conf.use_short_slot) | |
2122 | sinfo->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME; | |
785e21a8 | 2123 | sinfo->bss_param.dtim_period = sdata->vif.bss_conf.dtim_period; |
b7ffbd7e JB |
2124 | sinfo->bss_param.beacon_interval = sdata->vif.bss_conf.beacon_int; |
2125 | ||
2126 | sinfo->sta_flags.set = 0; | |
2127 | sinfo->sta_flags.mask = BIT(NL80211_STA_FLAG_AUTHORIZED) | | |
2128 | BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) | | |
2129 | BIT(NL80211_STA_FLAG_WME) | | |
2130 | BIT(NL80211_STA_FLAG_MFP) | | |
2131 | BIT(NL80211_STA_FLAG_AUTHENTICATED) | | |
2132 | BIT(NL80211_STA_FLAG_ASSOCIATED) | | |
2133 | BIT(NL80211_STA_FLAG_TDLS_PEER); | |
2134 | if (test_sta_flag(sta, WLAN_STA_AUTHORIZED)) | |
2135 | sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHORIZED); | |
2136 | if (test_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE)) | |
2137 | sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE); | |
a74a8c84 | 2138 | if (sta->sta.wme) |
b7ffbd7e JB |
2139 | sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_WME); |
2140 | if (test_sta_flag(sta, WLAN_STA_MFP)) | |
2141 | sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_MFP); | |
2142 | if (test_sta_flag(sta, WLAN_STA_AUTH)) | |
2143 | sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_AUTHENTICATED); | |
2144 | if (test_sta_flag(sta, WLAN_STA_ASSOC)) | |
2145 | sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_ASSOCIATED); | |
2146 | if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) | |
2147 | sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER); | |
2148 | ||
2149 | /* check if the driver has a SW RC implementation */ | |
2150 | if (ref && ref->ops->get_expected_throughput) | |
2151 | thr = ref->ops->get_expected_throughput(sta->rate_ctrl_priv); | |
2152 | else | |
2153 | thr = drv_get_expected_throughput(local, &sta->sta); | |
2154 | ||
2155 | if (thr != 0) { | |
319090bf | 2156 | sinfo->filled |= BIT(NL80211_STA_INFO_EXPECTED_THROUGHPUT); |
b7ffbd7e JB |
2157 | sinfo->expected_throughput = thr; |
2158 | } | |
2159 | } |