]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/mac80211/util.c
mac80211: split ieee80211_send_probe_req()
[mirror_ubuntu-jammy-kernel.git] / net / mac80211 / util.c
CommitLineData
c2d1560a
JB
1/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
d98ad83e 6 * Copyright 2013-2014 Intel Mobile Communications GmbH
da6a4352 7 * Copyright (C) 2015-2017 Intel Deutschland GmbH
e552af05 8 * Copyright (C) 2018 Intel Corporation
c2d1560a
JB
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * utilities for mac80211
15 */
16
17#include <net/mac80211.h>
18#include <linux/netdevice.h>
bc3b2d7f 19#include <linux/export.h>
c2d1560a
JB
20#include <linux/types.h>
21#include <linux/slab.h>
22#include <linux/skbuff.h>
23#include <linux/etherdevice.h>
24#include <linux/if_arp.h>
c2d1560a 25#include <linux/bitmap.h>
dd76986b 26#include <linux/crc32.h>
881d966b 27#include <net/net_namespace.h>
c2d1560a 28#include <net/cfg80211.h>
dabeb344 29#include <net/rtnetlink.h>
c2d1560a
JB
30
31#include "ieee80211_i.h"
24487981 32#include "driver-ops.h"
2c8dccc7 33#include "rate.h"
ee385855 34#include "mesh.h"
c2d1560a 35#include "wme.h"
f2753ddb 36#include "led.h"
fffd0934 37#include "wep.h"
c2d1560a
JB
38
39/* privid for wiphys to determine whether they belong to us or not */
8a47cea7 40const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
c2d1560a 41
9a95371a
LR
42struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
43{
44 struct ieee80211_local *local;
45 BUG_ON(!wiphy);
46
47 local = wiphy_priv(wiphy);
48 return &local->hw;
49}
50EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
c2d1560a 51
5cf121c3 52void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
c2d1560a 53{
252b86c4 54 struct sk_buff *skb;
2de8e0d9
JB
55 struct ieee80211_hdr *hdr;
56
252b86c4 57 skb_queue_walk(&tx->skbs, skb) {
2de8e0d9
JB
58 hdr = (struct ieee80211_hdr *) skb->data;
59 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
252b86c4 60 }
c2d1560a
JB
61}
62
57fbcce3 63int ieee80211_frame_duration(enum nl80211_band band, size_t len,
438b61b7
SW
64 int rate, int erp, int short_preamble,
65 int shift)
c2d1560a
JB
66{
67 int dur;
68
69 /* calculate duration (in microseconds, rounded up to next higher
70 * integer if it includes a fractional microsecond) to send frame of
71 * len bytes (does not include FCS) at the given rate. Duration will
72 * also include SIFS.
73 *
74 * rate is in 100 kbps, so divident is multiplied by 10 in the
75 * DIV_ROUND_UP() operations.
438b61b7
SW
76 *
77 * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
78 * is assumed to be 0 otherwise.
c2d1560a
JB
79 */
80
57fbcce3 81 if (band == NL80211_BAND_5GHZ || erp) {
c2d1560a
JB
82 /*
83 * OFDM:
84 *
85 * N_DBPS = DATARATE x 4
86 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
87 * (16 = SIGNAL time, 6 = tail bits)
88 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
89 *
90 * T_SYM = 4 usec
438b61b7 91 * 802.11a - 18.5.2: aSIFSTime = 16 usec
c2d1560a
JB
92 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
93 * signal ext = 6 usec
94 */
c2d1560a 95 dur = 16; /* SIFS + signal ext */
438b61b7
SW
96 dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
97 dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
438b61b7
SW
98
99 /* IEEE 802.11-2012 18.3.2.4: all values above are:
100 * * times 4 for 5 MHz
101 * * times 2 for 10 MHz
102 */
103 dur *= 1 << shift;
2103dec1
SW
104
105 /* rates should already consider the channel bandwidth,
106 * don't apply divisor again.
107 */
108 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
109 4 * rate); /* T_SYM x N_SYM */
c2d1560a
JB
110 } else {
111 /*
112 * 802.11b or 802.11g with 802.11b compatibility:
113 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
114 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
115 *
116 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
117 * aSIFSTime = 10 usec
118 * aPreambleLength = 144 usec or 72 usec with short preamble
119 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
120 */
121 dur = 10; /* aSIFSTime = 10 usec */
122 dur += short_preamble ? (72 + 24) : (144 + 48);
123
124 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
125 }
126
127 return dur;
128}
129
130/* Exported duration function for driver use */
32bfd35d
JB
131__le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
132 struct ieee80211_vif *vif,
57fbcce3 133 enum nl80211_band band,
8318d78a
JB
134 size_t frame_len,
135 struct ieee80211_rate *rate)
c2d1560a 136{
25d834e1 137 struct ieee80211_sub_if_data *sdata;
c2d1560a 138 u16 dur;
438b61b7 139 int erp, shift = 0;
25d834e1 140 bool short_preamble = false;
c2d1560a 141
8318d78a 142 erp = 0;
25d834e1
JB
143 if (vif) {
144 sdata = vif_to_sdata(vif);
bda3933a 145 short_preamble = sdata->vif.bss_conf.use_short_preamble;
25d834e1
JB
146 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
147 erp = rate->flags & IEEE80211_RATE_ERP_G;
438b61b7 148 shift = ieee80211_vif_get_shift(vif);
25d834e1 149 }
8318d78a 150
4ee73f33 151 dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
438b61b7 152 short_preamble, shift);
c2d1560a
JB
153
154 return cpu_to_le16(dur);
155}
156EXPORT_SYMBOL(ieee80211_generic_frame_duration);
157
32bfd35d
JB
158__le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
159 struct ieee80211_vif *vif, size_t frame_len,
e039fa4a 160 const struct ieee80211_tx_info *frame_txctl)
c2d1560a
JB
161{
162 struct ieee80211_local *local = hw_to_local(hw);
163 struct ieee80211_rate *rate;
25d834e1 164 struct ieee80211_sub_if_data *sdata;
471b3efd 165 bool short_preamble;
2103dec1 166 int erp, shift = 0, bitrate;
c2d1560a 167 u16 dur;
2e92e6f2
JB
168 struct ieee80211_supported_band *sband;
169
4ee73f33 170 sband = local->hw.wiphy->bands[frame_txctl->band];
c2d1560a 171
25d834e1 172 short_preamble = false;
7e9ed188 173
e039fa4a 174 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
8318d78a
JB
175
176 erp = 0;
25d834e1
JB
177 if (vif) {
178 sdata = vif_to_sdata(vif);
bda3933a 179 short_preamble = sdata->vif.bss_conf.use_short_preamble;
25d834e1
JB
180 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
181 erp = rate->flags & IEEE80211_RATE_ERP_G;
438b61b7 182 shift = ieee80211_vif_get_shift(vif);
25d834e1 183 }
c2d1560a 184
2103dec1
SW
185 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
186
c2d1560a 187 /* CTS duration */
2103dec1 188 dur = ieee80211_frame_duration(sband->band, 10, bitrate,
438b61b7 189 erp, short_preamble, shift);
c2d1560a 190 /* Data frame duration */
2103dec1 191 dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
438b61b7 192 erp, short_preamble, shift);
c2d1560a 193 /* ACK duration */
2103dec1 194 dur += ieee80211_frame_duration(sband->band, 10, bitrate,
438b61b7 195 erp, short_preamble, shift);
c2d1560a
JB
196
197 return cpu_to_le16(dur);
198}
199EXPORT_SYMBOL(ieee80211_rts_duration);
200
32bfd35d
JB
201__le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
202 struct ieee80211_vif *vif,
c2d1560a 203 size_t frame_len,
e039fa4a 204 const struct ieee80211_tx_info *frame_txctl)
c2d1560a
JB
205{
206 struct ieee80211_local *local = hw_to_local(hw);
207 struct ieee80211_rate *rate;
25d834e1 208 struct ieee80211_sub_if_data *sdata;
471b3efd 209 bool short_preamble;
2103dec1 210 int erp, shift = 0, bitrate;
c2d1560a 211 u16 dur;
2e92e6f2
JB
212 struct ieee80211_supported_band *sband;
213
4ee73f33 214 sband = local->hw.wiphy->bands[frame_txctl->band];
c2d1560a 215
25d834e1 216 short_preamble = false;
7e9ed188 217
e039fa4a 218 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
8318d78a 219 erp = 0;
25d834e1
JB
220 if (vif) {
221 sdata = vif_to_sdata(vif);
bda3933a 222 short_preamble = sdata->vif.bss_conf.use_short_preamble;
25d834e1
JB
223 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
224 erp = rate->flags & IEEE80211_RATE_ERP_G;
438b61b7 225 shift = ieee80211_vif_get_shift(vif);
25d834e1 226 }
c2d1560a 227
2103dec1
SW
228 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
229
c2d1560a 230 /* Data frame duration */
2103dec1 231 dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
438b61b7 232 erp, short_preamble, shift);
e039fa4a 233 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
c2d1560a 234 /* ACK duration */
2103dec1 235 dur += ieee80211_frame_duration(sband->band, 10, bitrate,
438b61b7 236 erp, short_preamble, shift);
c2d1560a
JB
237 }
238
239 return cpu_to_le16(dur);
240}
241EXPORT_SYMBOL(ieee80211_ctstoself_duration);
242
3a25a8c8
JB
243void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
244{
245 struct ieee80211_sub_if_data *sdata;
a6f38ac3
JB
246 int n_acs = IEEE80211_NUM_ACS;
247
80a83cfc
MK
248 if (local->ops->wake_tx_queue)
249 return;
250
a6f38ac3
JB
251 if (local->hw.queues < IEEE80211_NUM_ACS)
252 n_acs = 1;
3a25a8c8
JB
253
254 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
255 int ac;
256
f142c6b9
JB
257 if (!sdata->dev)
258 continue;
259
3a25a8c8
JB
260 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
261 local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
262 continue;
263
a6f38ac3 264 for (ac = 0; ac < n_acs; ac++) {
3a25a8c8
JB
265 int ac_queue = sdata->vif.hw_queue[ac];
266
267 if (ac_queue == queue ||
268 (sdata->vif.cab_queue == queue &&
269 local->queue_stop_reasons[ac_queue] == 0 &&
270 skb_queue_empty(&local->pending[ac_queue])))
271 netif_wake_subqueue(sdata->dev, ac);
272 }
273 }
274}
275
ce7c9111 276static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
cca07b00
LC
277 enum queue_stop_reason reason,
278 bool refcounted)
c2d1560a
JB
279{
280 struct ieee80211_local *local = hw_to_local(hw);
281
b5878a2d
JB
282 trace_wake_queue(local, queue, reason);
283
e4e72fb4
JB
284 if (WARN_ON(queue >= hw->queues))
285 return;
ce7c9111 286
ada15125
JB
287 if (!test_bit(reason, &local->queue_stop_reasons[queue]))
288 return;
289
856142cd 290 if (!refcounted) {
cca07b00 291 local->q_stop_reasons[queue][reason] = 0;
856142cd 292 } else {
cca07b00 293 local->q_stop_reasons[queue][reason]--;
856142cd
JB
294 if (WARN_ON(local->q_stop_reasons[queue][reason] < 0))
295 local->q_stop_reasons[queue][reason] = 0;
296 }
cca07b00
LC
297
298 if (local->q_stop_reasons[queue][reason] == 0)
299 __clear_bit(reason, &local->queue_stop_reasons[queue]);
96f5e66e
JB
300
301 if (local->queue_stop_reasons[queue] != 0)
302 /* someone still has this queue stopped */
303 return;
304
7236fe29
JB
305 if (skb_queue_empty(&local->pending[queue])) {
306 rcu_read_lock();
3a25a8c8 307 ieee80211_propagate_queue_wake(local, queue);
7236fe29
JB
308 rcu_read_unlock();
309 } else
3b8d81e0 310 tasklet_schedule(&local->tx_pending_tasklet);
c2d1560a 311}
ce7c9111 312
96f5e66e 313void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
cca07b00
LC
314 enum queue_stop_reason reason,
315 bool refcounted)
ce7c9111
KV
316{
317 struct ieee80211_local *local = hw_to_local(hw);
318 unsigned long flags;
319
320 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
cca07b00 321 __ieee80211_wake_queue(hw, queue, reason, refcounted);
ce7c9111
KV
322 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
323}
324
325void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
326{
327 ieee80211_wake_queue_by_reason(hw, queue,
cca07b00
LC
328 IEEE80211_QUEUE_STOP_REASON_DRIVER,
329 false);
ce7c9111 330}
c2d1560a
JB
331EXPORT_SYMBOL(ieee80211_wake_queue);
332
ce7c9111 333static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
cca07b00
LC
334 enum queue_stop_reason reason,
335 bool refcounted)
c2d1560a
JB
336{
337 struct ieee80211_local *local = hw_to_local(hw);
cf0277e7 338 struct ieee80211_sub_if_data *sdata;
a6f38ac3 339 int n_acs = IEEE80211_NUM_ACS;
c2d1560a 340
b5878a2d
JB
341 trace_stop_queue(local, queue, reason);
342
e4e72fb4
JB
343 if (WARN_ON(queue >= hw->queues))
344 return;
96f5e66e 345
cca07b00
LC
346 if (!refcounted)
347 local->q_stop_reasons[queue][reason] = 1;
348 else
349 local->q_stop_reasons[queue][reason]++;
ada15125 350
cca07b00
LC
351 if (__test_and_set_bit(reason, &local->queue_stop_reasons[queue]))
352 return;
cf0277e7 353
80a83cfc
MK
354 if (local->ops->wake_tx_queue)
355 return;
356
a6f38ac3
JB
357 if (local->hw.queues < IEEE80211_NUM_ACS)
358 n_acs = 1;
359
cf0277e7 360 rcu_read_lock();
3a25a8c8
JB
361 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
362 int ac;
363
f142c6b9
JB
364 if (!sdata->dev)
365 continue;
366
a6f38ac3 367 for (ac = 0; ac < n_acs; ac++) {
3a25a8c8
JB
368 if (sdata->vif.hw_queue[ac] == queue ||
369 sdata->vif.cab_queue == queue)
370 netif_stop_subqueue(sdata->dev, ac);
371 }
372 }
cf0277e7 373 rcu_read_unlock();
c2d1560a 374}
ce7c9111 375
96f5e66e 376void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
cca07b00
LC
377 enum queue_stop_reason reason,
378 bool refcounted)
ce7c9111
KV
379{
380 struct ieee80211_local *local = hw_to_local(hw);
381 unsigned long flags;
382
383 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
cca07b00 384 __ieee80211_stop_queue(hw, queue, reason, refcounted);
ce7c9111
KV
385 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
386}
387
388void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
389{
390 ieee80211_stop_queue_by_reason(hw, queue,
cca07b00
LC
391 IEEE80211_QUEUE_STOP_REASON_DRIVER,
392 false);
ce7c9111 393}
c2d1560a
JB
394EXPORT_SYMBOL(ieee80211_stop_queue);
395
8f77f384
JB
396void ieee80211_add_pending_skb(struct ieee80211_local *local,
397 struct sk_buff *skb)
398{
399 struct ieee80211_hw *hw = &local->hw;
400 unsigned long flags;
a7bc376c 401 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3a25a8c8 402 int queue = info->hw_queue;
a7bc376c
JB
403
404 if (WARN_ON(!info->control.vif)) {
d4fa14cd 405 ieee80211_free_txskb(&local->hw, skb);
a7bc376c
JB
406 return;
407 }
8f77f384
JB
408
409 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
cca07b00
LC
410 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
411 false);
3b8d81e0 412 __skb_queue_tail(&local->pending[queue], skb);
cca07b00
LC
413 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
414 false);
8f77f384
JB
415 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
416}
417
e3685e03
JB
418void ieee80211_add_pending_skbs(struct ieee80211_local *local,
419 struct sk_buff_head *skbs)
8f77f384
JB
420{
421 struct ieee80211_hw *hw = &local->hw;
422 struct sk_buff *skb;
423 unsigned long flags;
b0b97a8a 424 int queue, i;
8f77f384
JB
425
426 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
8f77f384 427 while ((skb = skb_dequeue(skbs))) {
a7bc376c
JB
428 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
429
430 if (WARN_ON(!info->control.vif)) {
d4fa14cd 431 ieee80211_free_txskb(&local->hw, skb);
a7bc376c
JB
432 continue;
433 }
434
3a25a8c8 435 queue = info->hw_queue;
4644ae89
JB
436
437 __ieee80211_stop_queue(hw, queue,
cca07b00
LC
438 IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
439 false);
4644ae89 440
3b8d81e0 441 __skb_queue_tail(&local->pending[queue], skb);
8f77f384
JB
442 }
443
3b8d81e0 444 for (i = 0; i < hw->queues; i++)
8f77f384 445 __ieee80211_wake_queue(hw, i,
cca07b00
LC
446 IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
447 false);
8f77f384 448 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
8f77f384
JB
449}
450
ce7c9111 451void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
445ea4e8 452 unsigned long queues,
cca07b00
LC
453 enum queue_stop_reason reason,
454 bool refcounted)
c2d1560a 455{
ce7c9111
KV
456 struct ieee80211_local *local = hw_to_local(hw);
457 unsigned long flags;
c2d1560a
JB
458 int i;
459
ce7c9111
KV
460 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
461
445ea4e8 462 for_each_set_bit(i, &queues, hw->queues)
cca07b00 463 __ieee80211_stop_queue(hw, i, reason, refcounted);
ce7c9111
KV
464
465 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
466}
467
468void ieee80211_stop_queues(struct ieee80211_hw *hw)
469{
445ea4e8 470 ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
cca07b00
LC
471 IEEE80211_QUEUE_STOP_REASON_DRIVER,
472 false);
c2d1560a
JB
473}
474EXPORT_SYMBOL(ieee80211_stop_queues);
475
92ab8535
TW
476int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
477{
478 struct ieee80211_local *local = hw_to_local(hw);
3b8d81e0
JB
479 unsigned long flags;
480 int ret;
96f5e66e 481
e4e72fb4
JB
482 if (WARN_ON(queue >= hw->queues))
483 return true;
96f5e66e 484
3b8d81e0 485 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
2419ea14
TP
486 ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
487 &local->queue_stop_reasons[queue]);
3b8d81e0
JB
488 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
489 return ret;
92ab8535
TW
490}
491EXPORT_SYMBOL(ieee80211_queue_stopped);
492
ce7c9111 493void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
445ea4e8 494 unsigned long queues,
cca07b00
LC
495 enum queue_stop_reason reason,
496 bool refcounted)
c2d1560a 497{
ce7c9111
KV
498 struct ieee80211_local *local = hw_to_local(hw);
499 unsigned long flags;
c2d1560a
JB
500 int i;
501
ce7c9111
KV
502 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
503
445ea4e8 504 for_each_set_bit(i, &queues, hw->queues)
cca07b00 505 __ieee80211_wake_queue(hw, i, reason, refcounted);
ce7c9111
KV
506
507 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
508}
509
510void ieee80211_wake_queues(struct ieee80211_hw *hw)
511{
445ea4e8 512 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
cca07b00
LC
513 IEEE80211_QUEUE_STOP_REASON_DRIVER,
514 false);
c2d1560a
JB
515}
516EXPORT_SYMBOL(ieee80211_wake_queues);
dabeb344 517
26da23b6
LC
518static unsigned int
519ieee80211_get_vif_queues(struct ieee80211_local *local,
520 struct ieee80211_sub_if_data *sdata)
39ecc01d 521{
26da23b6 522 unsigned int queues;
39ecc01d 523
30686bf7 524 if (sdata && ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
39ecc01d
JB
525 int ac;
526
527 queues = 0;
528
529 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
530 queues |= BIT(sdata->vif.hw_queue[ac]);
531 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
532 queues |= BIT(sdata->vif.cab_queue);
533 } else {
534 /* all queues */
535 queues = BIT(local->hw.queues) - 1;
536 }
537
26da23b6
LC
538 return queues;
539}
540
4f9610d5
LK
541void __ieee80211_flush_queues(struct ieee80211_local *local,
542 struct ieee80211_sub_if_data *sdata,
3b24f4c6 543 unsigned int queues, bool drop)
26da23b6 544{
26da23b6
LC
545 if (!local->ops->flush)
546 return;
547
4f9610d5
LK
548 /*
549 * If no queue was set, or if the HW doesn't support
550 * IEEE80211_HW_QUEUE_CONTROL - flush all queues
551 */
30686bf7 552 if (!queues || !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
4f9610d5 553 queues = ieee80211_get_vif_queues(local, sdata);
26da23b6 554
59f48fe2 555 ieee80211_stop_queues_by_reason(&local->hw, queues,
cca07b00
LC
556 IEEE80211_QUEUE_STOP_REASON_FLUSH,
557 false);
445ea4e8 558
3b24f4c6 559 drv_flush(local, sdata, queues, drop);
445ea4e8 560
59f48fe2 561 ieee80211_wake_queues_by_reason(&local->hw, queues,
cca07b00
LC
562 IEEE80211_QUEUE_STOP_REASON_FLUSH,
563 false);
39ecc01d
JB
564}
565
4f9610d5 566void ieee80211_flush_queues(struct ieee80211_local *local,
3b24f4c6 567 struct ieee80211_sub_if_data *sdata, bool drop)
4f9610d5 568{
3b24f4c6 569 __ieee80211_flush_queues(local, sdata, 0, drop);
4f9610d5
LK
570}
571
26da23b6
LC
572void ieee80211_stop_vif_queues(struct ieee80211_local *local,
573 struct ieee80211_sub_if_data *sdata,
574 enum queue_stop_reason reason)
575{
576 ieee80211_stop_queues_by_reason(&local->hw,
577 ieee80211_get_vif_queues(local, sdata),
578 reason, true);
579}
580
581void ieee80211_wake_vif_queues(struct ieee80211_local *local,
582 struct ieee80211_sub_if_data *sdata,
583 enum queue_stop_reason reason)
584{
585 ieee80211_wake_queues_by_reason(&local->hw,
586 ieee80211_get_vif_queues(local, sdata),
587 reason, true);
588}
589
3384d757
AN
590static void __iterate_interfaces(struct ieee80211_local *local,
591 u32 iter_flags,
592 void (*iterator)(void *data, u8 *mac,
593 struct ieee80211_vif *vif),
594 void *data)
dabeb344 595{
dabeb344 596 struct ieee80211_sub_if_data *sdata;
3384d757 597 bool active_only = iter_flags & IEEE80211_IFACE_ITER_ACTIVE;
dabeb344 598
c7c71066 599 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
2f561feb 600 switch (sdata->vif.type) {
05c914fe 601 case NL80211_IFTYPE_MONITOR:
d8212184 602 if (!(sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE))
31eba5bc
FF
603 continue;
604 break;
05c914fe 605 case NL80211_IFTYPE_AP_VLAN:
2f561feb 606 continue;
2ca27bcf 607 default:
2f561feb
ID
608 break;
609 }
8b2c9824 610 if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
3384d757 611 active_only && !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
8b2c9824 612 continue;
3384d757 613 if (ieee80211_sdata_running(sdata) || !active_only)
47846c9b 614 iterator(data, sdata->vif.addr,
2f561feb
ID
615 &sdata->vif);
616 }
617
c7c71066
JB
618 sdata = rcu_dereference_check(local->monitor_sdata,
619 lockdep_is_held(&local->iflist_mtx) ||
620 lockdep_rtnl_is_held());
8b2c9824 621 if (sdata &&
3384d757 622 (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || !active_only ||
8b2c9824 623 sdata->flags & IEEE80211_SDATA_IN_DRIVER))
685fb72b 624 iterator(data, sdata->vif.addr, &sdata->vif);
c7c71066 625}
685fb72b 626
3384d757 627void ieee80211_iterate_interfaces(
c7c71066
JB
628 struct ieee80211_hw *hw, u32 iter_flags,
629 void (*iterator)(void *data, u8 *mac,
630 struct ieee80211_vif *vif),
631 void *data)
632{
633 struct ieee80211_local *local = hw_to_local(hw);
634
635 mutex_lock(&local->iflist_mtx);
3384d757 636 __iterate_interfaces(local, iter_flags, iterator, data);
c771c9d8 637 mutex_unlock(&local->iflist_mtx);
2f561feb 638}
3384d757 639EXPORT_SYMBOL_GPL(ieee80211_iterate_interfaces);
2f561feb
ID
640
641void ieee80211_iterate_active_interfaces_atomic(
8b2c9824 642 struct ieee80211_hw *hw, u32 iter_flags,
2f561feb
ID
643 void (*iterator)(void *data, u8 *mac,
644 struct ieee80211_vif *vif),
645 void *data)
646{
647 struct ieee80211_local *local = hw_to_local(hw);
2f561feb 648
e38bad47 649 rcu_read_lock();
3384d757
AN
650 __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
651 iterator, data);
c7c71066
JB
652 rcu_read_unlock();
653}
654EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
dabeb344 655
c7c71066
JB
656void ieee80211_iterate_active_interfaces_rtnl(
657 struct ieee80211_hw *hw, u32 iter_flags,
658 void (*iterator)(void *data, u8 *mac,
659 struct ieee80211_vif *vif),
660 void *data)
661{
662 struct ieee80211_local *local = hw_to_local(hw);
e38bad47 663
c7c71066 664 ASSERT_RTNL();
685fb72b 665
3384d757
AN
666 __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
667 iterator, data);
dabeb344 668}
c7c71066 669EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_rtnl);
37ffc8da 670
0fc1e049
AN
671static void __iterate_stations(struct ieee80211_local *local,
672 void (*iterator)(void *data,
673 struct ieee80211_sta *sta),
674 void *data)
675{
676 struct sta_info *sta;
677
678 list_for_each_entry_rcu(sta, &local->sta_list, list) {
679 if (!sta->uploaded)
680 continue;
681
682 iterator(data, &sta->sta);
683 }
684}
685
686void ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
687 void (*iterator)(void *data,
688 struct ieee80211_sta *sta),
689 void *data)
690{
691 struct ieee80211_local *local = hw_to_local(hw);
692
693 rcu_read_lock();
694 __iterate_stations(local, iterator, data);
695 rcu_read_unlock();
696}
697EXPORT_SYMBOL_GPL(ieee80211_iterate_stations_atomic);
698
ad7e718c
JB
699struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
700{
701 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
702
703 if (!ieee80211_sdata_running(sdata) ||
704 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
705 return NULL;
706 return &sdata->vif;
707}
708EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
709
dc5a1ad7
EG
710struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
711{
6513e98e
JB
712 struct ieee80211_sub_if_data *sdata;
713
714 if (!vif)
715 return NULL;
716
717 sdata = vif_to_sdata(vif);
dc5a1ad7
EG
718
719 if (!ieee80211_sdata_running(sdata) ||
720 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
721 return NULL;
722
723 return &sdata->wdev;
724}
725EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev);
726
42935eca
LR
727/*
728 * Nothing should have been stuffed into the workqueue during
4afaff17
EG
729 * the suspend->resume cycle. Since we can't check each caller
730 * of this function if we are already quiescing / suspended,
731 * check here and don't WARN since this can actually happen when
732 * the rx path (for example) is racing against __ieee80211_suspend
733 * and suspending / quiescing was set after the rx path checked
734 * them.
42935eca
LR
735 */
736static bool ieee80211_can_queue_work(struct ieee80211_local *local)
737{
4afaff17
EG
738 if (local->quiescing || (local->suspended && !local->resuming)) {
739 pr_warn("queueing ieee80211 work while going to suspend\n");
ceb99fe0 740 return false;
4afaff17 741 }
42935eca
LR
742
743 return true;
744}
745
746void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
747{
748 struct ieee80211_local *local = hw_to_local(hw);
749
750 if (!ieee80211_can_queue_work(local))
751 return;
752
753 queue_work(local->workqueue, work);
754}
755EXPORT_SYMBOL(ieee80211_queue_work);
756
757void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
758 struct delayed_work *dwork,
759 unsigned long delay)
760{
761 struct ieee80211_local *local = hw_to_local(hw);
762
763 if (!ieee80211_can_queue_work(local))
764 return;
765
766 queue_delayed_work(local->workqueue, dwork, delay);
767}
768EXPORT_SYMBOL(ieee80211_queue_delayed_work);
769
35d865af 770u32 ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
dd76986b
JB
771 struct ieee802_11_elems *elems,
772 u64 filter, u32 crc)
773{
774 size_t left = len;
35d865af 775 const u8 *pos = start;
dd76986b 776 bool calc_crc = filter != 0;
fcff4f10 777 DECLARE_BITMAP(seen_elems, 256);
b2e506bf 778 const u8 *ie;
dd76986b 779
fcff4f10 780 bitmap_zero(seen_elems, 256);
dd76986b
JB
781 memset(elems, 0, sizeof(*elems));
782 elems->ie_start = start;
783 elems->total_len = len;
784
785 while (left >= 2) {
786 u8 id, elen;
fcff4f10 787 bool elem_parse_failed;
dd76986b
JB
788
789 id = *pos++;
790 elen = *pos++;
791 left -= 2;
792
fcff4f10
PS
793 if (elen > left) {
794 elems->parse_error = true;
dd76986b 795 break;
fcff4f10
PS
796 }
797
9690fb16
JB
798 switch (id) {
799 case WLAN_EID_SSID:
800 case WLAN_EID_SUPP_RATES:
801 case WLAN_EID_FH_PARAMS:
802 case WLAN_EID_DS_PARAMS:
803 case WLAN_EID_CF_PARAMS:
804 case WLAN_EID_TIM:
805 case WLAN_EID_IBSS_PARAMS:
806 case WLAN_EID_CHALLENGE:
807 case WLAN_EID_RSN:
808 case WLAN_EID_ERP_INFO:
809 case WLAN_EID_EXT_SUPP_RATES:
810 case WLAN_EID_HT_CAPABILITY:
811 case WLAN_EID_HT_OPERATION:
812 case WLAN_EID_VHT_CAPABILITY:
813 case WLAN_EID_VHT_OPERATION:
814 case WLAN_EID_MESH_ID:
815 case WLAN_EID_MESH_CONFIG:
816 case WLAN_EID_PEER_MGMT:
817 case WLAN_EID_PREQ:
818 case WLAN_EID_PREP:
819 case WLAN_EID_PERR:
820 case WLAN_EID_RANN:
821 case WLAN_EID_CHANNEL_SWITCH:
822 case WLAN_EID_EXT_CHANSWITCH_ANN:
823 case WLAN_EID_COUNTRY:
824 case WLAN_EID_PWR_CONSTRAINT:
825 case WLAN_EID_TIMEOUT_INTERVAL:
85220d71 826 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
b2e506bf 827 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
8f2535b9 828 case WLAN_EID_CHAN_SWITCH_PARAM:
9041c1fa 829 case WLAN_EID_EXT_CAPABILITY:
53837584
AN
830 case WLAN_EID_CHAN_SWITCH_TIMING:
831 case WLAN_EID_LINK_ID:
e38a017b 832 case WLAN_EID_BSS_MAX_IDLE_PERIOD:
b2e506bf
JB
833 /*
834 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
835 * that if the content gets bigger it might be needed more than once
836 */
9690fb16
JB
837 if (test_bit(id, seen_elems)) {
838 elems->parse_error = true;
839 left -= elen;
840 pos += elen;
841 continue;
842 }
843 break;
fcff4f10 844 }
dd76986b
JB
845
846 if (calc_crc && id < 64 && (filter & (1ULL << id)))
847 crc = crc32_be(crc, pos - 2, elen + 2);
848
fcff4f10
PS
849 elem_parse_failed = false;
850
dd76986b 851 switch (id) {
53837584
AN
852 case WLAN_EID_LINK_ID:
853 if (elen + 2 != sizeof(struct ieee80211_tdls_lnkie)) {
854 elem_parse_failed = true;
855 break;
856 }
857 elems->lnk_id = (void *)(pos - 2);
858 break;
859 case WLAN_EID_CHAN_SWITCH_TIMING:
860 if (elen != sizeof(struct ieee80211_ch_switch_timing)) {
861 elem_parse_failed = true;
862 break;
863 }
864 elems->ch_sw_timing = (void *)pos;
865 break;
9041c1fa
AN
866 case WLAN_EID_EXT_CAPABILITY:
867 elems->ext_capab = pos;
868 elems->ext_capab_len = elen;
869 break;
dd76986b
JB
870 case WLAN_EID_SSID:
871 elems->ssid = pos;
872 elems->ssid_len = elen;
873 break;
874 case WLAN_EID_SUPP_RATES:
875 elems->supp_rates = pos;
876 elems->supp_rates_len = elen;
877 break;
dd76986b 878 case WLAN_EID_DS_PARAMS:
1cd8e88e
JB
879 if (elen >= 1)
880 elems->ds_params = pos;
881 else
882 elem_parse_failed = true;
dd76986b 883 break;
dd76986b
JB
884 case WLAN_EID_TIM:
885 if (elen >= sizeof(struct ieee80211_tim_ie)) {
886 elems->tim = (void *)pos;
887 elems->tim_len = elen;
fcff4f10
PS
888 } else
889 elem_parse_failed = true;
dd76986b 890 break;
dd76986b
JB
891 case WLAN_EID_CHALLENGE:
892 elems->challenge = pos;
893 elems->challenge_len = elen;
894 break;
895 case WLAN_EID_VENDOR_SPECIFIC:
896 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
897 pos[2] == 0xf2) {
898 /* Microsoft OUI (00:50:F2) */
899
900 if (calc_crc)
901 crc = crc32_be(crc, pos - 2, elen + 2);
902
441a33ba 903 if (elen >= 5 && pos[3] == 2) {
dd76986b
JB
904 /* OUI Type 2 - WMM IE */
905 if (pos[4] == 0) {
906 elems->wmm_info = pos;
907 elems->wmm_info_len = elen;
908 } else if (pos[4] == 1) {
909 elems->wmm_param = pos;
910 elems->wmm_param_len = elen;
911 }
912 }
913 }
914 break;
915 case WLAN_EID_RSN:
916 elems->rsn = pos;
917 elems->rsn_len = elen;
918 break;
919 case WLAN_EID_ERP_INFO:
1946bed9
JB
920 if (elen >= 1)
921 elems->erp_info = pos;
922 else
923 elem_parse_failed = true;
dd76986b
JB
924 break;
925 case WLAN_EID_EXT_SUPP_RATES:
926 elems->ext_supp_rates = pos;
927 elems->ext_supp_rates_len = elen;
928 break;
929 case WLAN_EID_HT_CAPABILITY:
930 if (elen >= sizeof(struct ieee80211_ht_cap))
931 elems->ht_cap_elem = (void *)pos;
fcff4f10
PS
932 else
933 elem_parse_failed = true;
dd76986b 934 break;
074d46d1
JB
935 case WLAN_EID_HT_OPERATION:
936 if (elen >= sizeof(struct ieee80211_ht_operation))
937 elems->ht_operation = (void *)pos;
fcff4f10
PS
938 else
939 elem_parse_failed = true;
dd76986b 940 break;
818255ea
MP
941 case WLAN_EID_VHT_CAPABILITY:
942 if (elen >= sizeof(struct ieee80211_vht_cap))
943 elems->vht_cap_elem = (void *)pos;
944 else
945 elem_parse_failed = true;
946 break;
947 case WLAN_EID_VHT_OPERATION:
948 if (elen >= sizeof(struct ieee80211_vht_operation))
949 elems->vht_operation = (void *)pos;
950 else
951 elem_parse_failed = true;
952 break;
bee7f586
JB
953 case WLAN_EID_OPMODE_NOTIF:
954 if (elen > 0)
955 elems->opmode_notif = pos;
956 else
957 elem_parse_failed = true;
958 break;
dd76986b
JB
959 case WLAN_EID_MESH_ID:
960 elems->mesh_id = pos;
961 elems->mesh_id_len = elen;
962 break;
963 case WLAN_EID_MESH_CONFIG:
964 if (elen >= sizeof(struct ieee80211_meshconf_ie))
965 elems->mesh_config = (void *)pos;
fcff4f10
PS
966 else
967 elem_parse_failed = true;
dd76986b
JB
968 break;
969 case WLAN_EID_PEER_MGMT:
970 elems->peering = pos;
971 elems->peering_len = elen;
972 break;
3f52b7e3
MP
973 case WLAN_EID_MESH_AWAKE_WINDOW:
974 if (elen >= 2)
975 elems->awake_window = (void *)pos;
976 break;
dd76986b
JB
977 case WLAN_EID_PREQ:
978 elems->preq = pos;
979 elems->preq_len = elen;
980 break;
981 case WLAN_EID_PREP:
982 elems->prep = pos;
983 elems->prep_len = elen;
984 break;
985 case WLAN_EID_PERR:
986 elems->perr = pos;
987 elems->perr_len = elen;
988 break;
989 case WLAN_EID_RANN:
990 if (elen >= sizeof(struct ieee80211_rann_ie))
991 elems->rann = (void *)pos;
fcff4f10
PS
992 else
993 elem_parse_failed = true;
dd76986b
JB
994 break;
995 case WLAN_EID_CHANNEL_SWITCH:
5bc1420b
JB
996 if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
997 elem_parse_failed = true;
998 break;
999 }
1000 elems->ch_switch_ie = (void *)pos;
dd76986b 1001 break;
b4f286a1
JB
1002 case WLAN_EID_EXT_CHANSWITCH_ANN:
1003 if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
1004 elem_parse_failed = true;
1005 break;
1006 }
1007 elems->ext_chansw_ie = (void *)pos;
1008 break;
85220d71
JB
1009 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
1010 if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
1011 elem_parse_failed = true;
1012 break;
1013 }
1014 elems->sec_chan_offs = (void *)pos;
1015 break;
8f2535b9
CYY
1016 case WLAN_EID_CHAN_SWITCH_PARAM:
1017 if (elen !=
1018 sizeof(*elems->mesh_chansw_params_ie)) {
1019 elem_parse_failed = true;
1020 break;
1021 }
1022 elems->mesh_chansw_params_ie = (void *)pos;
1023 break;
b2e506bf
JB
1024 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
1025 if (!action ||
1026 elen != sizeof(*elems->wide_bw_chansw_ie)) {
1027 elem_parse_failed = true;
1028 break;
1029 }
1030 elems->wide_bw_chansw_ie = (void *)pos;
1031 break;
1032 case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
1033 if (action) {
1034 elem_parse_failed = true;
1035 break;
1036 }
1037 /*
1038 * This is a bit tricky, but as we only care about
1039 * the wide bandwidth channel switch element, so
1040 * just parse it out manually.
1041 */
1042 ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
1043 pos, elen);
1044 if (ie) {
1045 if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
1046 elems->wide_bw_chansw_ie =
1047 (void *)(ie + 2);
1048 else
1049 elem_parse_failed = true;
1050 }
1051 break;
dd76986b
JB
1052 case WLAN_EID_COUNTRY:
1053 elems->country_elem = pos;
1054 elems->country_elem_len = elen;
1055 break;
1056 case WLAN_EID_PWR_CONSTRAINT:
761a48d2
JB
1057 if (elen != 1) {
1058 elem_parse_failed = true;
1059 break;
1060 }
dd76986b 1061 elems->pwr_constr_elem = pos;
dd76986b 1062 break;
c8d65917
SG
1063 case WLAN_EID_CISCO_VENDOR_SPECIFIC:
1064 /* Lots of different options exist, but we only care
1065 * about the Dynamic Transmit Power Control element.
1066 * First check for the Cisco OUI, then for the DTPC
1067 * tag (0x00).
1068 */
1069 if (elen < 4) {
1070 elem_parse_failed = true;
1071 break;
1072 }
1073
1074 if (pos[0] != 0x00 || pos[1] != 0x40 ||
1075 pos[2] != 0x96 || pos[3] != 0x00)
1076 break;
1077
1078 if (elen != 6) {
1079 elem_parse_failed = true;
1080 break;
1081 }
1082
1083 if (calc_crc)
1084 crc = crc32_be(crc, pos - 2, elen + 2);
1085
1086 elems->cisco_dtpc_elem = pos;
1087 break;
dd76986b 1088 case WLAN_EID_TIMEOUT_INTERVAL:
79ba1d89
JB
1089 if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
1090 elems->timeout_int = (void *)pos;
1091 else
1092 elem_parse_failed = true;
dd76986b 1093 break;
e38a017b
AS
1094 case WLAN_EID_BSS_MAX_IDLE_PERIOD:
1095 if (elen >= sizeof(*elems->max_idle_period_ie))
1096 elems->max_idle_period_ie = (void *)pos;
1097 break;
dd76986b
JB
1098 default:
1099 break;
1100 }
1101
fcff4f10
PS
1102 if (elem_parse_failed)
1103 elems->parse_error = true;
1104 else
5df45690 1105 __set_bit(id, seen_elems);
fcff4f10 1106
dd76986b
JB
1107 left -= elen;
1108 pos += elen;
1109 }
1110
fcff4f10
PS
1111 if (left != 0)
1112 elems->parse_error = true;
1113
dd76986b
JB
1114 return crc;
1115}
1116
e552af05
HD
1117void ieee80211_regulatory_limit_wmm_params(struct ieee80211_sub_if_data *sdata,
1118 struct ieee80211_tx_queue_params
1119 *qparam, int ac)
1120{
1121 struct ieee80211_chanctx_conf *chanctx_conf;
1122 const struct ieee80211_reg_rule *rrule;
1123 struct ieee80211_wmm_ac *wmm_ac;
1124 u16 center_freq = 0;
1125
1126 if (sdata->vif.type != NL80211_IFTYPE_AP &&
1127 sdata->vif.type != NL80211_IFTYPE_STATION)
1128 return;
1129
1130 rcu_read_lock();
1131 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1132 if (chanctx_conf)
1133 center_freq = chanctx_conf->def.chan->center_freq;
1134
1135 if (!center_freq) {
1136 rcu_read_unlock();
1137 return;
1138 }
1139
1140 rrule = freq_reg_info(sdata->wdev.wiphy, MHZ_TO_KHZ(center_freq));
1141
1142 if (IS_ERR_OR_NULL(rrule) || !rrule->wmm_rule) {
1143 rcu_read_unlock();
1144 return;
1145 }
1146
1147 if (sdata->vif.type == NL80211_IFTYPE_AP)
1148 wmm_ac = &rrule->wmm_rule->ap[ac];
1149 else
1150 wmm_ac = &rrule->wmm_rule->client[ac];
1151 qparam->cw_min = max_t(u16, qparam->cw_min, wmm_ac->cw_min);
1152 qparam->cw_max = max_t(u16, qparam->cw_max, wmm_ac->cw_max);
1153 qparam->aifs = max_t(u8, qparam->aifs, wmm_ac->aifsn);
1154 qparam->txop = !qparam->txop ? wmm_ac->cot / 32 :
1155 min_t(u16, qparam->txop, wmm_ac->cot / 32);
1156 rcu_read_unlock();
1157}
1158
3abead59 1159void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
cec66283 1160 bool bss_notify, bool enable_qos)
5825fe10
JB
1161{
1162 struct ieee80211_local *local = sdata->local;
1163 struct ieee80211_tx_queue_params qparam;
55de908a 1164 struct ieee80211_chanctx_conf *chanctx_conf;
54bcbc69 1165 int ac;
cec66283 1166 bool use_11b;
239281f8 1167 bool is_ocb; /* Use another EDCA parameters if dot11OCBActivated=true */
aa837e1d 1168 int aCWmin, aCWmax;
5825fe10
JB
1169
1170 if (!local->ops->conf_tx)
1171 return;
1172
54bcbc69
JB
1173 if (local->hw.queues < IEEE80211_NUM_ACS)
1174 return;
1175
5825fe10
JB
1176 memset(&qparam, 0, sizeof(qparam));
1177
55de908a
JB
1178 rcu_read_lock();
1179 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1180 use_11b = (chanctx_conf &&
57fbcce3 1181 chanctx_conf->def.chan->band == NL80211_BAND_2GHZ) &&
aa837e1d 1182 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
55de908a 1183 rcu_read_unlock();
5825fe10 1184
239281f8
RL
1185 is_ocb = (sdata->vif.type == NL80211_IFTYPE_OCB);
1186
1f4ffde8
FZ
1187 /* Set defaults according to 802.11-2007 Table 7-37 */
1188 aCWmax = 1023;
1189 if (use_11b)
1190 aCWmin = 31;
1191 else
1192 aCWmin = 15;
1193
1194 /* Confiure old 802.11b/g medium access rules. */
1195 qparam.cw_max = aCWmax;
1196 qparam.cw_min = aCWmin;
1197 qparam.txop = 0;
1198 qparam.aifs = 2;
aa837e1d 1199
1f4ffde8
FZ
1200 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1201 /* Update if QoS is enabled. */
a8ce8544
SG
1202 if (enable_qos) {
1203 switch (ac) {
1204 case IEEE80211_AC_BK:
1205 qparam.cw_max = aCWmax;
1206 qparam.cw_min = aCWmin;
1207 qparam.txop = 0;
239281f8
RL
1208 if (is_ocb)
1209 qparam.aifs = 9;
1210 else
1211 qparam.aifs = 7;
a8ce8544
SG
1212 break;
1213 /* never happens but let's not leave undefined */
1214 default:
1215 case IEEE80211_AC_BE:
1216 qparam.cw_max = aCWmax;
1217 qparam.cw_min = aCWmin;
1218 qparam.txop = 0;
239281f8
RL
1219 if (is_ocb)
1220 qparam.aifs = 6;
1221 else
1222 qparam.aifs = 3;
a8ce8544
SG
1223 break;
1224 case IEEE80211_AC_VI:
1225 qparam.cw_max = aCWmin;
1226 qparam.cw_min = (aCWmin + 1) / 2 - 1;
239281f8
RL
1227 if (is_ocb)
1228 qparam.txop = 0;
1229 else if (use_11b)
a8ce8544
SG
1230 qparam.txop = 6016/32;
1231 else
1232 qparam.txop = 3008/32;
239281f8
RL
1233
1234 if (is_ocb)
1235 qparam.aifs = 3;
1236 else
1237 qparam.aifs = 2;
a8ce8544
SG
1238 break;
1239 case IEEE80211_AC_VO:
1240 qparam.cw_max = (aCWmin + 1) / 2 - 1;
1241 qparam.cw_min = (aCWmin + 1) / 4 - 1;
239281f8
RL
1242 if (is_ocb)
1243 qparam.txop = 0;
1244 else if (use_11b)
a8ce8544
SG
1245 qparam.txop = 3264/32;
1246 else
1247 qparam.txop = 1504/32;
1248 qparam.aifs = 2;
1249 break;
1250 }
aa837e1d 1251 }
e552af05 1252 ieee80211_regulatory_limit_wmm_params(sdata, &qparam, ac);
5825fe10 1253
ab13315a
KV
1254 qparam.uapsd = false;
1255
54bcbc69
JB
1256 sdata->tx_conf[ac] = qparam;
1257 drv_conf_tx(local, sdata, ac, &qparam);
aa837e1d 1258 }
e1b3ec1a 1259
f142c6b9 1260 if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
708d50ed
AB
1261 sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE &&
1262 sdata->vif.type != NL80211_IFTYPE_NAN) {
a8ce8544 1263 sdata->vif.bss_conf.qos = enable_qos;
3abead59
JB
1264 if (bss_notify)
1265 ieee80211_bss_info_change_notify(sdata,
1266 BSS_CHANGED_QOS);
d9734979 1267 }
5825fe10 1268}
e50db65c 1269
46900298 1270void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
700e8ea6 1271 u16 transaction, u16 auth_alg, u16 status,
4a3cb702 1272 const u8 *extra, size_t extra_len, const u8 *da,
1672c0e3
JB
1273 const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
1274 u32 tx_flags)
46900298
JB
1275{
1276 struct ieee80211_local *local = sdata->local;
1277 struct sk_buff *skb;
1278 struct ieee80211_mgmt *mgmt;
fffd0934 1279 int err;
46900298 1280
15e230ab 1281 /* 24 + 6 = header + auth_algo + auth_transaction + status_code */
744462a9
MS
1282 skb = dev_alloc_skb(local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN +
1283 24 + 6 + extra_len + IEEE80211_WEP_ICV_LEN);
d15b8459 1284 if (!skb)
46900298 1285 return;
d15b8459 1286
744462a9 1287 skb_reserve(skb, local->hw.extra_tx_headroom + IEEE80211_WEP_IV_LEN);
46900298 1288
b080db58 1289 mgmt = skb_put_zero(skb, 24 + 6);
46900298
JB
1290 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1291 IEEE80211_STYPE_AUTH);
efa6a09d 1292 memcpy(mgmt->da, da, ETH_ALEN);
47846c9b 1293 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
46900298
JB
1294 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1295 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
1296 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
700e8ea6 1297 mgmt->u.auth.status_code = cpu_to_le16(status);
46900298 1298 if (extra)
59ae1d12 1299 skb_put_data(skb, extra, extra_len);
46900298 1300
fffd0934
JB
1301 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
1302 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1303 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
1304 WARN_ON(err);
1305 }
1306
1672c0e3
JB
1307 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1308 tx_flags;
62ae67be 1309 ieee80211_tx_skb(sdata, skb);
46900298
JB
1310}
1311
6ae16775
AQ
1312void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
1313 const u8 *bssid, u16 stype, u16 reason,
1314 bool send_frame, u8 *frame_buf)
1315{
1316 struct ieee80211_local *local = sdata->local;
1317 struct sk_buff *skb;
1318 struct ieee80211_mgmt *mgmt = (void *)frame_buf;
1319
1320 /* build frame */
1321 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1322 mgmt->duration = 0; /* initialize only */
1323 mgmt->seq_ctrl = 0; /* initialize only */
1324 memcpy(mgmt->da, bssid, ETH_ALEN);
1325 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1326 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1327 /* u.deauth.reason_code == u.disassoc.reason_code */
1328 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
1329
1330 if (send_frame) {
1331 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
1332 IEEE80211_DEAUTH_FRAME_LEN);
1333 if (!skb)
1334 return;
1335
1336 skb_reserve(skb, local->hw.extra_tx_headroom);
1337
1338 /* copy in frame */
59ae1d12 1339 skb_put_data(skb, mgmt, IEEE80211_DEAUTH_FRAME_LEN);
6ae16775
AQ
1340
1341 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1342 !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
1343 IEEE80211_SKB_CB(skb)->flags |=
1344 IEEE80211_TX_INTFL_DONT_ENCRYPT;
1345
1346 ieee80211_tx_skb(sdata, skb);
1347 }
1348}
1349
c56ef672
DS
1350static int ieee80211_build_preq_ies_band(struct ieee80211_local *local,
1351 u8 *buffer, size_t buffer_len,
1352 const u8 *ie, size_t ie_len,
57fbcce3 1353 enum nl80211_band band,
c56ef672
DS
1354 u32 rate_mask,
1355 struct cfg80211_chan_def *chandef,
00387f32 1356 size_t *offset, u32 flags)
de95a54b
JB
1357{
1358 struct ieee80211_supported_band *sband;
c604b9f2 1359 u8 *pos = buffer, *end = buffer + buffer_len;
c56ef672 1360 size_t noffset;
8e664fb3 1361 int supp_rates_len, i;
8dcb2003
JM
1362 u8 rates[32];
1363 int num_rates;
1364 int ext_rates_len;
2103dec1
SW
1365 int shift;
1366 u32 rate_flags;
40a11ca8 1367 bool have_80mhz = false;
de95a54b 1368
c56ef672
DS
1369 *offset = 0;
1370
4d36ec58 1371 sband = local->hw.wiphy->bands[band];
d811b3d5
AN
1372 if (WARN_ON_ONCE(!sband))
1373 return 0;
de95a54b 1374
2103dec1
SW
1375 rate_flags = ieee80211_chandef_rate_flags(chandef);
1376 shift = ieee80211_chandef_get_shift(chandef);
1377
8dcb2003
JM
1378 num_rates = 0;
1379 for (i = 0; i < sband->n_bitrates; i++) {
1380 if ((BIT(i) & rate_mask) == 0)
1381 continue; /* skip rate */
2103dec1
SW
1382 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1383 continue;
1384
1385 rates[num_rates++] =
1386 (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
1387 (1 << shift) * 5);
8dcb2003
JM
1388 }
1389
1390 supp_rates_len = min_t(int, num_rates, 8);
8e664fb3 1391
c604b9f2
JB
1392 if (end - pos < 2 + supp_rates_len)
1393 goto out_err;
de95a54b 1394 *pos++ = WLAN_EID_SUPP_RATES;
8e664fb3 1395 *pos++ = supp_rates_len;
8dcb2003
JM
1396 memcpy(pos, rates, supp_rates_len);
1397 pos += supp_rates_len;
8e664fb3
JB
1398
1399 /* insert "request information" if in custom IEs */
1400 if (ie && ie_len) {
1401 static const u8 before_extrates[] = {
1402 WLAN_EID_SSID,
1403 WLAN_EID_SUPP_RATES,
1404 WLAN_EID_REQUEST,
1405 };
1406 noffset = ieee80211_ie_split(ie, ie_len,
1407 before_extrates,
1408 ARRAY_SIZE(before_extrates),
c56ef672
DS
1409 *offset);
1410 if (end - pos < noffset - *offset)
c604b9f2 1411 goto out_err;
c56ef672
DS
1412 memcpy(pos, ie + *offset, noffset - *offset);
1413 pos += noffset - *offset;
1414 *offset = noffset;
8e664fb3
JB
1415 }
1416
8dcb2003
JM
1417 ext_rates_len = num_rates - supp_rates_len;
1418 if (ext_rates_len > 0) {
c604b9f2
JB
1419 if (end - pos < 2 + ext_rates_len)
1420 goto out_err;
8e664fb3 1421 *pos++ = WLAN_EID_EXT_SUPP_RATES;
8dcb2003
JM
1422 *pos++ = ext_rates_len;
1423 memcpy(pos, rates + supp_rates_len, ext_rates_len);
1424 pos += ext_rates_len;
8e664fb3
JB
1425 }
1426
57fbcce3 1427 if (chandef->chan && sband->band == NL80211_BAND_2GHZ) {
c604b9f2
JB
1428 if (end - pos < 3)
1429 goto out_err;
651b5225
JM
1430 *pos++ = WLAN_EID_DS_PARAMS;
1431 *pos++ = 1;
2103dec1
SW
1432 *pos++ = ieee80211_frequency_to_channel(
1433 chandef->chan->center_freq);
651b5225
JM
1434 }
1435
8e664fb3
JB
1436 /* insert custom IEs that go before HT */
1437 if (ie && ie_len) {
1438 static const u8 before_ht[] = {
a7f26d80
JB
1439 /*
1440 * no need to list the ones split off already
1441 * (or generated here)
1442 */
8e664fb3
JB
1443 WLAN_EID_DS_PARAMS,
1444 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
1445 };
1446 noffset = ieee80211_ie_split(ie, ie_len,
1447 before_ht, ARRAY_SIZE(before_ht),
c56ef672
DS
1448 *offset);
1449 if (end - pos < noffset - *offset)
c604b9f2 1450 goto out_err;
c56ef672
DS
1451 memcpy(pos, ie + *offset, noffset - *offset);
1452 pos += noffset - *offset;
1453 *offset = noffset;
de95a54b
JB
1454 }
1455
c604b9f2
JB
1456 if (sband->ht_cap.ht_supported) {
1457 if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
1458 goto out_err;
ef96a842
BG
1459 pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
1460 sband->ht_cap.cap);
c604b9f2 1461 }
5ef2d41a 1462
de95a54b
JB
1463 /*
1464 * If adding more here, adjust code in main.c
1465 * that calculates local->scan_ies_len.
1466 */
1467
4d952300 1468 /* insert custom IEs that go before VHT */
8e664fb3 1469 if (ie && ie_len) {
4d952300 1470 static const u8 before_vht[] = {
a7f26d80
JB
1471 /*
1472 * no need to list the ones split off already
1473 * (or generated here)
1474 */
4d952300
JB
1475 WLAN_EID_BSS_COEX_2040,
1476 WLAN_EID_EXT_CAPABILITY,
1477 WLAN_EID_SSID_LIST,
1478 WLAN_EID_CHANNEL_USAGE,
1479 WLAN_EID_INTERWORKING,
b44eebea 1480 WLAN_EID_MESH_ID,
a7f26d80 1481 /* 60 GHz (Multi-band, DMG, MMS) can't happen */
4d952300
JB
1482 };
1483 noffset = ieee80211_ie_split(ie, ie_len,
1484 before_vht, ARRAY_SIZE(before_vht),
c56ef672
DS
1485 *offset);
1486 if (end - pos < noffset - *offset)
c604b9f2 1487 goto out_err;
c56ef672
DS
1488 memcpy(pos, ie + *offset, noffset - *offset);
1489 pos += noffset - *offset;
1490 *offset = noffset;
de95a54b
JB
1491 }
1492
40a11ca8
JB
1493 /* Check if any channel in this sband supports at least 80 MHz */
1494 for (i = 0; i < sband->n_channels; i++) {
fa44b988
AN
1495 if (sband->channels[i].flags & (IEEE80211_CHAN_DISABLED |
1496 IEEE80211_CHAN_NO_80MHZ))
1497 continue;
1498
1499 have_80mhz = true;
1500 break;
40a11ca8
JB
1501 }
1502
1503 if (sband->vht_cap.vht_supported && have_80mhz) {
c604b9f2
JB
1504 if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
1505 goto out_err;
ba0afa2f
MP
1506 pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
1507 sband->vht_cap.cap);
c604b9f2 1508 }
ba0afa2f 1509
c604b9f2
JB
1510 return pos - buffer;
1511 out_err:
1512 WARN_ONCE(1, "not enough space for preq IEs\n");
de95a54b
JB
1513 return pos - buffer;
1514}
1515
c56ef672
DS
1516int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
1517 size_t buffer_len,
1518 struct ieee80211_scan_ies *ie_desc,
1519 const u8 *ie, size_t ie_len,
1520 u8 bands_used, u32 *rate_masks,
00387f32
JB
1521 struct cfg80211_chan_def *chandef,
1522 u32 flags)
c56ef672
DS
1523{
1524 size_t pos = 0, old_pos = 0, custom_ie_offset = 0;
1525 int i;
1526
1527 memset(ie_desc, 0, sizeof(*ie_desc));
1528
57fbcce3 1529 for (i = 0; i < NUM_NL80211_BANDS; i++) {
c56ef672
DS
1530 if (bands_used & BIT(i)) {
1531 pos += ieee80211_build_preq_ies_band(local,
1532 buffer + pos,
1533 buffer_len - pos,
1534 ie, ie_len, i,
1535 rate_masks[i],
1536 chandef,
00387f32
JB
1537 &custom_ie_offset,
1538 flags);
c56ef672
DS
1539 ie_desc->ies[i] = buffer + old_pos;
1540 ie_desc->len[i] = pos - old_pos;
1541 old_pos = pos;
1542 }
1543 }
1544
1545 /* add any remaining custom IEs */
1546 if (ie && ie_len) {
1547 if (WARN_ONCE(buffer_len - pos < ie_len - custom_ie_offset,
1548 "not enough space for preq custom IEs\n"))
1549 return pos;
1550 memcpy(buffer + pos, ie + custom_ie_offset,
1551 ie_len - custom_ie_offset);
1552 ie_desc->common_ies = buffer + pos;
1553 ie_desc->common_ie_len = ie_len - custom_ie_offset;
1554 pos += ie_len - custom_ie_offset;
1555 }
1556
1557 return pos;
1558};
1559
a619a4c0 1560struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
a344d677
JB
1561 const u8 *src, const u8 *dst,
1562 u32 ratemask,
6b77863b 1563 struct ieee80211_channel *chan,
a619a4c0 1564 const u8 *ssid, size_t ssid_len,
a806c558 1565 const u8 *ie, size_t ie_len,
00387f32 1566 u32 flags)
46900298
JB
1567{
1568 struct ieee80211_local *local = sdata->local;
2103dec1 1569 struct cfg80211_chan_def chandef;
46900298
JB
1570 struct sk_buff *skb;
1571 struct ieee80211_mgmt *mgmt;
b9a9ada1 1572 int ies_len;
57fbcce3 1573 u32 rate_masks[NUM_NL80211_BANDS] = {};
c56ef672 1574 struct ieee80211_scan_ies dummy_ie_desc;
46900298 1575
a806c558
PS
1576 /*
1577 * Do not send DS Channel parameter for directed probe requests
1578 * in order to maximize the chance that we get a response. Some
1579 * badly-behaved APs don't respond when this parameter is included.
1580 */
2103dec1 1581 chandef.width = sdata->vif.bss_conf.chandef.width;
00387f32 1582 if (flags & IEEE80211_PROBE_FLAG_DIRECTED)
2103dec1 1583 chandef.chan = NULL;
a806c558 1584 else
2103dec1 1585 chandef.chan = chan;
651b5225 1586
a344d677
JB
1587 skb = ieee80211_probereq_get(&local->hw, src, ssid, ssid_len,
1588 100 + ie_len);
5b2bbf75 1589 if (!skb)
b9a9ada1
JB
1590 return NULL;
1591
c56ef672 1592 rate_masks[chan->band] = ratemask;
b9a9ada1 1593 ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
c56ef672
DS
1594 skb_tailroom(skb), &dummy_ie_desc,
1595 ie, ie_len, BIT(chan->band),
00387f32 1596 rate_masks, &chandef, flags);
b9a9ada1 1597 skb_put(skb, ies_len);
7c12ce8b 1598
46900298 1599 if (dst) {
7c12ce8b 1600 mgmt = (struct ieee80211_mgmt *) skb->data;
46900298
JB
1601 memcpy(mgmt->da, dst, ETH_ALEN);
1602 memcpy(mgmt->bssid, dst, ETH_ALEN);
46900298 1603 }
46900298 1604
62ae67be 1605 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
5b2bbf75 1606
a619a4c0
JO
1607 return skb;
1608}
1609
2103dec1 1610u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
46900298 1611 struct ieee802_11_elems *elems,
57fbcce3 1612 enum nl80211_band band, u32 *basic_rates)
46900298
JB
1613{
1614 struct ieee80211_supported_band *sband;
46900298 1615 size_t num_rates;
2103dec1
SW
1616 u32 supp_rates, rate_flags;
1617 int i, j, shift;
21a8e9dd 1618
2103dec1 1619 sband = sdata->local->hw.wiphy->bands[band];
21a8e9dd
MSS
1620 if (WARN_ON(!sband))
1621 return 1;
2103dec1
SW
1622
1623 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
1624 shift = ieee80211_vif_get_shift(&sdata->vif);
46900298 1625
46900298
JB
1626 num_rates = sband->n_bitrates;
1627 supp_rates = 0;
1628 for (i = 0; i < elems->supp_rates_len +
1629 elems->ext_supp_rates_len; i++) {
1630 u8 rate = 0;
1631 int own_rate;
9ebb61a2 1632 bool is_basic;
46900298
JB
1633 if (i < elems->supp_rates_len)
1634 rate = elems->supp_rates[i];
1635 else if (elems->ext_supp_rates)
1636 rate = elems->ext_supp_rates
1637 [i - elems->supp_rates_len];
1638 own_rate = 5 * (rate & 0x7f);
9ebb61a2
AN
1639 is_basic = !!(rate & 0x80);
1640
1641 if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
1642 continue;
1643
1644 for (j = 0; j < num_rates; j++) {
2103dec1
SW
1645 int brate;
1646 if ((rate_flags & sband->bitrates[j].flags)
1647 != rate_flags)
1648 continue;
1649
1650 brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
1651 1 << shift);
1652
1653 if (brate == own_rate) {
46900298 1654 supp_rates |= BIT(j);
9ebb61a2
AN
1655 if (basic_rates && is_basic)
1656 *basic_rates |= BIT(j);
1657 }
1658 }
46900298
JB
1659 }
1660 return supp_rates;
1661}
f2753ddb 1662
84f6a01c
JB
1663void ieee80211_stop_device(struct ieee80211_local *local)
1664{
1665 ieee80211_led_radio(local, false);
67408c8c 1666 ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
84f6a01c
JB
1667
1668 cancel_work_sync(&local->reconfig_filter);
84f6a01c
JB
1669
1670 flush_workqueue(local->workqueue);
678f415f 1671 drv_stop(local);
84f6a01c
JB
1672}
1673
74430f94
JB
1674static void ieee80211_flush_completed_scan(struct ieee80211_local *local,
1675 bool aborted)
1676{
1677 /* It's possible that we don't handle the scan completion in
1678 * time during suspend, so if it's still marked as completed
1679 * here, queue the work and flush it to clean things up.
1680 * Instead of calling the worker function directly here, we
1681 * really queue it to avoid potential races with other flows
1682 * scheduling the same work.
1683 */
1684 if (test_bit(SCAN_COMPLETED, &local->scanning)) {
1685 /* If coming from reconfiguration failure, abort the scan so
1686 * we don't attempt to continue a partial HW scan - which is
1687 * possible otherwise if (e.g.) the 2.4 GHz portion was the
1688 * completed scan, and a 5 GHz portion is still pending.
1689 */
1690 if (aborted)
1691 set_bit(SCAN_ABORTED, &local->scanning);
1692 ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
1693 flush_delayed_work(&local->scan_work);
1694 }
1695}
1696
f6837ba8
JB
1697static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
1698{
1699 struct ieee80211_sub_if_data *sdata;
1700 struct ieee80211_chanctx *ctx;
1701
1702 /*
1703 * We get here if during resume the device can't be restarted properly.
1704 * We might also get here if this happens during HW reset, which is a
1705 * slightly different situation and we need to drop all connections in
1706 * the latter case.
1707 *
1708 * Ask cfg80211 to turn off all interfaces, this will result in more
1709 * warnings but at least we'll then get into a clean stopped state.
1710 */
1711
1712 local->resuming = false;
1713 local->suspended = false;
7584f88f 1714 local->in_reconfig = false;
f6837ba8 1715
74430f94
JB
1716 ieee80211_flush_completed_scan(local, true);
1717
f6837ba8
JB
1718 /* scheduled scan clearly can't be running any more, but tell
1719 * cfg80211 and clear local state
1720 */
1721 ieee80211_sched_scan_end(local);
1722
1723 list_for_each_entry(sdata, &local->interfaces, list)
1724 sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
1725
1726 /* Mark channel contexts as not being in the driver any more to avoid
1727 * removing them from the driver during the shutdown process...
1728 */
1729 mutex_lock(&local->chanctx_mtx);
1730 list_for_each_entry(ctx, &local->chanctx_list, list)
1731 ctx->driver_present = false;
1732 mutex_unlock(&local->chanctx_mtx);
1733
1734 cfg80211_shutdown_all_interfaces(local->hw.wiphy);
1735}
1736
153a5fc4
SG
1737static void ieee80211_assign_chanctx(struct ieee80211_local *local,
1738 struct ieee80211_sub_if_data *sdata)
1739{
1740 struct ieee80211_chanctx_conf *conf;
1741 struct ieee80211_chanctx *ctx;
1742
1743 if (!local->use_chanctx)
1744 return;
1745
1746 mutex_lock(&local->chanctx_mtx);
1747 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1748 lockdep_is_held(&local->chanctx_mtx));
1749 if (conf) {
1750 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1751 drv_assign_vif_chanctx(local, sdata, ctx);
1752 }
1753 mutex_unlock(&local->chanctx_mtx);
1754}
1755
1ea2c864
JB
1756static void ieee80211_reconfig_stations(struct ieee80211_sub_if_data *sdata)
1757{
1758 struct ieee80211_local *local = sdata->local;
1759 struct sta_info *sta;
1760
1761 /* add STAs back */
1762 mutex_lock(&local->sta_mtx);
1763 list_for_each_entry(sta, &local->sta_list, list) {
1764 enum ieee80211_sta_state state;
1765
1766 if (!sta->uploaded || sta->sdata != sdata)
1767 continue;
1768
1769 for (state = IEEE80211_STA_NOTEXIST;
1770 state < sta->sta_state; state++)
1771 WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
1772 state + 1));
1773 }
1774 mutex_unlock(&local->sta_mtx);
1775}
1776
167e33f4
AB
1777static int ieee80211_reconfig_nan(struct ieee80211_sub_if_data *sdata)
1778{
1779 struct cfg80211_nan_func *func, **funcs;
1780 int res, id, i = 0;
1781
1782 res = drv_start_nan(sdata->local, sdata,
1783 &sdata->u.nan.conf);
1784 if (WARN_ON(res))
1785 return res;
1786
1787 funcs = kzalloc((sdata->local->hw.max_nan_de_entries + 1) *
1788 sizeof(*funcs), GFP_KERNEL);
1789 if (!funcs)
1790 return -ENOMEM;
1791
1792 /* Add all the functions:
1793 * This is a little bit ugly. We need to call a potentially sleeping
1794 * callback for each NAN function, so we can't hold the spinlock.
1795 */
1796 spin_lock_bh(&sdata->u.nan.func_lock);
1797
1798 idr_for_each_entry(&sdata->u.nan.function_inst_ids, func, id)
1799 funcs[i++] = func;
1800
1801 spin_unlock_bh(&sdata->u.nan.func_lock);
1802
1803 for (i = 0; funcs[i]; i++) {
1804 res = drv_add_nan_func(sdata->local, sdata, funcs[i]);
1805 if (WARN_ON(res))
1806 ieee80211_nan_func_terminated(&sdata->vif,
1807 funcs[i]->instance_id,
1808 NL80211_NAN_FUNC_TERM_REASON_ERROR,
1809 GFP_KERNEL);
1810 }
1811
1812 kfree(funcs);
1813
1814 return 0;
1815}
1816
f2753ddb
JB
1817int ieee80211_reconfig(struct ieee80211_local *local)
1818{
1819 struct ieee80211_hw *hw = &local->hw;
1820 struct ieee80211_sub_if_data *sdata;
55de908a 1821 struct ieee80211_chanctx *ctx;
f2753ddb 1822 struct sta_info *sta;
2683d65b 1823 int res, i;
d888130a 1824 bool reconfig_due_to_wowlan = false;
d43c6b6e 1825 struct ieee80211_sub_if_data *sched_scan_sdata;
6ea0a69c 1826 struct cfg80211_sched_scan_request *sched_scan_req;
d43c6b6e 1827 bool sched_scan_stopped = false;
b0485e9f 1828 bool suspended = local->suspended;
d888130a 1829
0f8b8245
EP
1830 /* nothing to do if HW shouldn't run */
1831 if (!local->open_count)
1832 goto wake_up;
1833
8f21b0ad 1834#ifdef CONFIG_PM
b0485e9f 1835 if (suspended)
ceb99fe0 1836 local->resuming = true;
f2753ddb 1837
eecc4800 1838 if (local->wowlan) {
b0485e9f
EP
1839 /*
1840 * In the wowlan case, both mac80211 and the device
1841 * are functional when the resume op is called, so
1842 * clear local->suspended so the device could operate
1843 * normally (e.g. pass rx frames).
1844 */
1845 local->suspended = false;
eecc4800 1846 res = drv_resume(local);
27b3eb9c 1847 local->wowlan = false;
eecc4800
JB
1848 if (res < 0) {
1849 local->resuming = false;
1850 return res;
1851 }
1852 if (res == 0)
1853 goto wake_up;
1854 WARN_ON(res > 1);
1855 /*
1856 * res is 1, which means the driver requested
1857 * to go through a regular reset on wakeup.
b0485e9f 1858 * restore local->suspended in this case.
eecc4800 1859 */
d888130a 1860 reconfig_due_to_wowlan = true;
b0485e9f 1861 local->suspended = true;
eecc4800
JB
1862 }
1863#endif
f2753ddb 1864
43d6df00
EP
1865 /*
1866 * In case of hw_restart during suspend (without wowlan),
1867 * cancel restart work, as we are reconfiguring the device
1868 * anyway.
1869 * Note that restart_work is scheduled on a frozen workqueue,
1870 * so we can't deadlock in this case.
1871 */
1872 if (suspended && local->in_reconfig && !reconfig_due_to_wowlan)
1873 cancel_work_sync(&local->restart_work);
1874
968a76ce
EP
1875 local->started = false;
1876
94f9b97b
JB
1877 /*
1878 * Upon resume hardware can sometimes be goofy due to
1879 * various platform / driver / bus issues, so restarting
1880 * the device may at times not work immediately. Propagate
1881 * the error.
1882 */
1883 res = drv_start(local);
1884 if (res) {
b0485e9f 1885 if (suspended)
f6837ba8
JB
1886 WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
1887 else
1888 WARN(1, "Hardware became unavailable during restart.\n");
1889 ieee80211_handle_reconfig_failure(local);
94f9b97b 1890 return res;
f2753ddb
JB
1891 }
1892
7f281975
YAP
1893 /* setup fragmentation threshold */
1894 drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
1895
1896 /* setup RTS threshold */
1897 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
1898
1899 /* reset coverage class */
1900 drv_set_coverage_class(local, hw->wiphy->coverage_class);
1901
94f9b97b
JB
1902 ieee80211_led_radio(local, true);
1903 ieee80211_mod_tpt_led_trig(local,
1904 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1905
f2753ddb 1906 /* add interfaces */
4b6f1dd6
JB
1907 sdata = rtnl_dereference(local->monitor_sdata);
1908 if (sdata) {
3c3e21e7
JB
1909 /* in HW restart it exists already */
1910 WARN_ON(local->resuming);
4b6f1dd6
JB
1911 res = drv_add_interface(local, sdata);
1912 if (WARN_ON(res)) {
0c2bef46 1913 RCU_INIT_POINTER(local->monitor_sdata, NULL);
4b6f1dd6
JB
1914 synchronize_net();
1915 kfree(sdata);
1916 }
1917 }
1918
f2753ddb
JB
1919 list_for_each_entry(sdata, &local->interfaces, list) {
1920 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1921 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
c8fff3dc 1922 ieee80211_sdata_running(sdata)) {
7b7eab6f 1923 res = drv_add_interface(local, sdata);
c8fff3dc
LC
1924 if (WARN_ON(res))
1925 break;
1926 }
1927 }
1928
1929 /* If adding any of the interfaces failed above, roll back and
1930 * report failure.
1931 */
1932 if (res) {
1933 list_for_each_entry_continue_reverse(sdata, &local->interfaces,
1934 list)
1935 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1936 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1937 ieee80211_sdata_running(sdata))
1938 drv_remove_interface(local, sdata);
1939 ieee80211_handle_reconfig_failure(local);
1940 return res;
f2753ddb
JB
1941 }
1942
55de908a 1943 /* add channel contexts */
f0dea9c7
AS
1944 if (local->use_chanctx) {
1945 mutex_lock(&local->chanctx_mtx);
1946 list_for_each_entry(ctx, &local->chanctx_list, list)
5bcae31d
MK
1947 if (ctx->replace_state !=
1948 IEEE80211_CHANCTX_REPLACES_OTHER)
1949 WARN_ON(drv_add_chanctx(local, ctx));
f0dea9c7 1950 mutex_unlock(&local->chanctx_mtx);
55de908a 1951
7df180f7
ZG
1952 sdata = rtnl_dereference(local->monitor_sdata);
1953 if (sdata && ieee80211_sdata_running(sdata))
1954 ieee80211_assign_chanctx(local, sdata);
1955 }
fe5f2559 1956
f2753ddb
JB
1957 /* reconfigure hardware */
1958 ieee80211_hw_config(local, ~0);
1959
f2753ddb 1960 ieee80211_configure_filter(local);
f2753ddb
JB
1961
1962 /* Finally also reconfigure all the BSS information */
1963 list_for_each_entry(sdata, &local->interfaces, list) {
ac8dd506
JB
1964 u32 changed;
1965
9607e6b6 1966 if (!ieee80211_sdata_running(sdata))
f2753ddb 1967 continue;
ac8dd506 1968
1ea2c864
JB
1969 ieee80211_assign_chanctx(local, sdata);
1970
1971 switch (sdata->vif.type) {
1972 case NL80211_IFTYPE_AP_VLAN:
1973 case NL80211_IFTYPE_MONITOR:
1974 break;
1975 default:
1976 ieee80211_reconfig_stations(sdata);
1977 /* fall through */
1978 case NL80211_IFTYPE_AP: /* AP stations are handled later */
1979 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1980 drv_conf_tx(local, sdata, i,
1981 &sdata->tx_conf[i]);
1982 break;
1983 }
1984
ac8dd506
JB
1985 /* common change flags for all interface types */
1986 changed = BSS_CHANGED_ERP_CTS_PROT |
1987 BSS_CHANGED_ERP_PREAMBLE |
1988 BSS_CHANGED_ERP_SLOT |
1989 BSS_CHANGED_HT |
1990 BSS_CHANGED_BASIC_RATES |
1991 BSS_CHANGED_BEACON_INT |
1992 BSS_CHANGED_BSSID |
4ced3f74 1993 BSS_CHANGED_CQM |
55de47f6 1994 BSS_CHANGED_QOS |
1ea6f9c0 1995 BSS_CHANGED_IDLE |
dcbe73ca
PKC
1996 BSS_CHANGED_TXPOWER |
1997 BSS_CHANGED_MCAST_RATE;
ac8dd506 1998
b5a33d52 1999 if (sdata->vif.mu_mimo_owner)
23a1f8d4
SS
2000 changed |= BSS_CHANGED_MU_GROUPS;
2001
f2753ddb
JB
2002 switch (sdata->vif.type) {
2003 case NL80211_IFTYPE_STATION:
0d392e93 2004 changed |= BSS_CHANGED_ASSOC |
ab095877
EP
2005 BSS_CHANGED_ARP_FILTER |
2006 BSS_CHANGED_PS;
c65dd147 2007
989c6505
AB
2008 /* Re-send beacon info report to the driver */
2009 if (sdata->u.mgd.have_beacon)
2010 changed |= BSS_CHANGED_BEACON_INFO;
c65dd147 2011
e38a017b
AS
2012 if (sdata->vif.bss_conf.max_idle_period ||
2013 sdata->vif.bss_conf.protected_keep_alive)
2014 changed |= BSS_CHANGED_KEEP_ALIVE;
2015
8d61ffa5 2016 sdata_lock(sdata);
ac8dd506 2017 ieee80211_bss_info_change_notify(sdata, changed);
8d61ffa5 2018 sdata_unlock(sdata);
ac8dd506 2019 break;
6e0bd6c3 2020 case NL80211_IFTYPE_OCB:
239281f8
RL
2021 changed |= BSS_CHANGED_OCB;
2022 ieee80211_bss_info_change_notify(sdata, changed);
6e0bd6c3 2023 break;
f2753ddb 2024 case NL80211_IFTYPE_ADHOC:
ac8dd506
JB
2025 changed |= BSS_CHANGED_IBSS;
2026 /* fall through */
f2753ddb 2027 case NL80211_IFTYPE_AP:
339afbf4 2028 changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
e7979ac7 2029
1041638f 2030 if (sdata->vif.type == NL80211_IFTYPE_AP) {
e7979ac7
AN
2031 changed |= BSS_CHANGED_AP_PROBE_RESP;
2032
1041638f
JB
2033 if (rcu_access_pointer(sdata->u.ap.beacon))
2034 drv_start_ap(local, sdata);
2035 }
2036
7827493b 2037 /* fall through */
f2753ddb 2038 case NL80211_IFTYPE_MESH_POINT:
8da34932
JB
2039 if (sdata->vif.bss_conf.enable_beacon) {
2040 changed |= BSS_CHANGED_BEACON |
2041 BSS_CHANGED_BEACON_ENABLED;
2042 ieee80211_bss_info_change_notify(sdata, changed);
2043 }
f2753ddb 2044 break;
167e33f4
AB
2045 case NL80211_IFTYPE_NAN:
2046 res = ieee80211_reconfig_nan(sdata);
2047 if (res < 0) {
2048 ieee80211_handle_reconfig_failure(local);
2049 return res;
2050 }
2051 break;
f2753ddb 2052 case NL80211_IFTYPE_WDS:
f2753ddb
JB
2053 case NL80211_IFTYPE_AP_VLAN:
2054 case NL80211_IFTYPE_MONITOR:
98104fde 2055 case NL80211_IFTYPE_P2P_DEVICE:
b205786e 2056 /* nothing to do */
f142c6b9 2057 break;
f2753ddb 2058 case NL80211_IFTYPE_UNSPECIFIED:
2e161f78 2059 case NUM_NL80211_IFTYPES:
2ca27bcf
JB
2060 case NL80211_IFTYPE_P2P_CLIENT:
2061 case NL80211_IFTYPE_P2P_GO:
f2753ddb
JB
2062 WARN_ON(1);
2063 break;
2064 }
2065 }
2066
4a733ef1 2067 ieee80211_recalc_ps(local);
8e1b23b9 2068
6e1b1b24
EP
2069 /*
2070 * The sta might be in psm against the ap (e.g. because
2071 * this was the state before a hw restart), so we
2072 * explicitly send a null packet in order to make sure
2073 * it'll sync against the ap (and get out of psm).
2074 */
2075 if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
2076 list_for_each_entry(sdata, &local->interfaces, list) {
2077 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2078 continue;
20f544ee
JB
2079 if (!sdata->u.mgd.associated)
2080 continue;
6e1b1b24 2081
076cdcb1 2082 ieee80211_send_nullfunc(local, sdata, false);
6e1b1b24
EP
2083 }
2084 }
2085
2e8d397e
AN
2086 /* APs are now beaconing, add back stations */
2087 mutex_lock(&local->sta_mtx);
2088 list_for_each_entry(sta, &local->sta_list, list) {
2089 enum ieee80211_sta_state state;
2090
2091 if (!sta->uploaded)
2092 continue;
2093
2094 if (sta->sdata->vif.type != NL80211_IFTYPE_AP)
2095 continue;
2096
2097 for (state = IEEE80211_STA_NOTEXIST;
2098 state < sta->sta_state; state++)
2099 WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
2100 state + 1));
2101 }
2102 mutex_unlock(&local->sta_mtx);
2103
7b21aea0 2104 /* add back keys */
f9dca80b
MK
2105 list_for_each_entry(sdata, &local->interfaces, list)
2106 ieee80211_reset_crypto_tx_tailroom(sdata);
2107
7b21aea0
ES
2108 list_for_each_entry(sdata, &local->interfaces, list)
2109 if (ieee80211_sdata_running(sdata))
2110 ieee80211_enable_keys(sdata);
2111
0d440ea2
EP
2112 /* Reconfigure sched scan if it was interrupted by FW restart */
2113 mutex_lock(&local->mtx);
2114 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
2115 lockdep_is_held(&local->mtx));
2116 sched_scan_req = rcu_dereference_protected(local->sched_scan_req,
2117 lockdep_is_held(&local->mtx));
2118 if (sched_scan_sdata && sched_scan_req)
2119 /*
2120 * Sched scan stopped, but we don't want to report it. Instead,
2121 * we're trying to reschedule. However, if more than one scan
2122 * plan was set, we cannot reschedule since we don't know which
2123 * scan plan was currently running (and some scan plans may have
2124 * already finished).
2125 */
2126 if (sched_scan_req->n_scan_plans > 1 ||
2127 __ieee80211_request_sched_scan_start(sched_scan_sdata,
b9f628fc
EP
2128 sched_scan_req)) {
2129 RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
2130 RCU_INIT_POINTER(local->sched_scan_req, NULL);
0d440ea2 2131 sched_scan_stopped = true;
b9f628fc 2132 }
0d440ea2
EP
2133 mutex_unlock(&local->mtx);
2134
2135 if (sched_scan_stopped)
b34939b9 2136 cfg80211_sched_scan_stopped_rtnl(local->hw.wiphy, 0);
0d440ea2 2137
c6209488 2138 wake_up:
04800ada 2139
3c3e21e7
JB
2140 if (local->monitors == local->open_count && local->monitors > 0)
2141 ieee80211_add_virtual_monitor(local);
2142
2a419056
JB
2143 /*
2144 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
2145 * sessions can be established after a resume.
2146 *
2147 * Also tear down aggregation sessions since reconfiguring
2148 * them in a hardware restart scenario is not easily done
2149 * right now, and the hardware will have lost information
2150 * about the sessions, but we and the AP still think they
2151 * are active. This is really a workaround though.
2152 */
30686bf7 2153 if (ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
2a419056
JB
2154 mutex_lock(&local->sta_mtx);
2155
2156 list_for_each_entry(sta, &local->sta_list, list) {
27392719
EP
2157 if (!local->resuming)
2158 ieee80211_sta_tear_down_BA_sessions(
2159 sta, AGG_STOP_LOCAL_REQUEST);
c2c98fde 2160 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
74e2bd1f 2161 }
2a419056
JB
2162
2163 mutex_unlock(&local->sta_mtx);
74e2bd1f 2164 }
74e2bd1f 2165
2316380f
SS
2166 if (local->in_reconfig) {
2167 local->in_reconfig = false;
2168 barrier();
2169
2170 /* Restart deferred ROCs */
2171 mutex_lock(&local->mtx);
2172 ieee80211_start_next_roc(local);
2173 mutex_unlock(&local->mtx);
2174 }
2175
445ea4e8 2176 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
cca07b00
LC
2177 IEEE80211_QUEUE_STOP_REASON_SUSPEND,
2178 false);
f2753ddb 2179
5bb644a0
JB
2180 /*
2181 * If this is for hw restart things are still running.
2182 * We may want to change that later, however.
2183 */
b0485e9f 2184 if (local->open_count && (!suspended || reconfig_due_to_wowlan))
cf2c92d8 2185 drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_RESTART);
8f21b0ad 2186
b0485e9f 2187 if (!suspended)
5bb644a0
JB
2188 return 0;
2189
2190#ifdef CONFIG_PM
ceb99fe0 2191 /* first set suspended false, then resuming */
5bb644a0 2192 local->suspended = false;
ceb99fe0
JB
2193 mb();
2194 local->resuming = false;
5bb644a0 2195
74430f94 2196 ieee80211_flush_completed_scan(local, false);
9120d94e 2197
0f8b8245 2198 if (local->open_count && !reconfig_due_to_wowlan)
cf2c92d8
EP
2199 drv_reconfig_complete(local, IEEE80211_RECONFIG_TYPE_SUSPEND);
2200
b8360ab8
JB
2201 list_for_each_entry(sdata, &local->interfaces, list) {
2202 if (!ieee80211_sdata_running(sdata))
2203 continue;
2204 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2205 ieee80211_sta_restart(sdata);
2206 }
2207
26d59535 2208 mod_timer(&local->sta_cleanup, jiffies + 1);
5bb644a0
JB
2209#else
2210 WARN_ON(1);
2211#endif
d43c6b6e 2212
f2753ddb
JB
2213 return 0;
2214}
42935eca 2215
95acac61
JB
2216void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
2217{
2218 struct ieee80211_sub_if_data *sdata;
2219 struct ieee80211_local *local;
2220 struct ieee80211_key *key;
2221
2222 if (WARN_ON(!vif))
2223 return;
2224
2225 sdata = vif_to_sdata(vif);
2226 local = sdata->local;
2227
2228 if (WARN_ON(!local->resuming))
2229 return;
2230
2231 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2232 return;
2233
2234 sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
2235
2236 mutex_lock(&local->key_mtx);
2237 list_for_each_entry(key, &sdata->key_list, list)
2238 key->flags |= KEY_FLAG_TAINTED;
2239 mutex_unlock(&local->key_mtx);
2240}
2241EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
2242
04ecd257 2243void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
0f78231b 2244{
04ecd257
JB
2245 struct ieee80211_local *local = sdata->local;
2246 struct ieee80211_chanctx_conf *chanctx_conf;
2247 struct ieee80211_chanctx *chanctx;
025e6be2 2248
04ecd257 2249 mutex_lock(&local->chanctx_mtx);
0f78231b 2250
04ecd257
JB
2251 chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
2252 lockdep_is_held(&local->chanctx_mtx));
0f78231b 2253
c189a685
AO
2254 /*
2255 * This function can be called from a work, thus it may be possible
2256 * that the chanctx_conf is removed (due to a disconnection, for
2257 * example).
2258 * So nothing should be done in such case.
2259 */
2260 if (!chanctx_conf)
5d8e4237 2261 goto unlock;
0f78231b 2262
04ecd257
JB
2263 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
2264 ieee80211_recalc_smps_chanctx(local, chanctx);
5d8e4237 2265 unlock:
04ecd257 2266 mutex_unlock(&local->chanctx_mtx);
0f78231b 2267}
8e664fb3 2268
21f659bf
EP
2269void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata)
2270{
2271 struct ieee80211_local *local = sdata->local;
2272 struct ieee80211_chanctx_conf *chanctx_conf;
2273 struct ieee80211_chanctx *chanctx;
2274
2275 mutex_lock(&local->chanctx_mtx);
2276
2277 chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
2278 lockdep_is_held(&local->chanctx_mtx));
2279
2280 if (WARN_ON_ONCE(!chanctx_conf))
2281 goto unlock;
2282
2283 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
2284 ieee80211_recalc_chanctx_min_def(local, chanctx);
2285 unlock:
2286 mutex_unlock(&local->chanctx_mtx);
2287}
2288
8e664fb3
JB
2289size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
2290{
2291 size_t pos = offset;
2292
2293 while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
2294 pos += 2 + ies[pos + 1];
2295
2296 return pos;
2297}
615f7b9b
MV
2298
2299static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
2300 int rssi_min_thold,
2301 int rssi_max_thold)
2302{
2303 trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
2304
2305 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2306 return;
2307
2308 /*
2309 * Scale up threshold values before storing it, as the RSSI averaging
2310 * algorithm uses a scaled up value as well. Change this scaling
2311 * factor if the RSSI averaging algorithm changes.
2312 */
2313 sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
2314 sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
2315}
2316
2317void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
2318 int rssi_min_thold,
2319 int rssi_max_thold)
2320{
2321 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2322
2323 WARN_ON(rssi_min_thold == rssi_max_thold ||
2324 rssi_min_thold > rssi_max_thold);
2325
2326 _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
2327 rssi_max_thold);
2328}
2329EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
2330
2331void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
2332{
2333 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2334
2335 _ieee80211_enable_rssi_reports(sdata, 0, 0);
2336}
2337EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
768db343 2338
ef96a842 2339u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
42e7aa77
AS
2340 u16 cap)
2341{
2342 __le16 tmp;
2343
2344 *pos++ = WLAN_EID_HT_CAPABILITY;
2345 *pos++ = sizeof(struct ieee80211_ht_cap);
2346 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
2347
2348 /* capability flags */
2349 tmp = cpu_to_le16(cap);
2350 memcpy(pos, &tmp, sizeof(u16));
2351 pos += sizeof(u16);
2352
2353 /* AMPDU parameters */
ef96a842
BG
2354 *pos++ = ht_cap->ampdu_factor |
2355 (ht_cap->ampdu_density <<
42e7aa77
AS
2356 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
2357
2358 /* MCS set */
ef96a842
BG
2359 memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
2360 pos += sizeof(ht_cap->mcs);
42e7aa77
AS
2361
2362 /* extended capabilities */
2363 pos += sizeof(__le16);
2364
2365 /* BF capabilities */
2366 pos += sizeof(__le32);
2367
2368 /* antenna selection */
2369 pos += sizeof(u8);
2370
2371 return pos;
2372}
2373
ba0afa2f 2374u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
cc3983d8 2375 u32 cap)
ba0afa2f
MP
2376{
2377 __le32 tmp;
2378
2379 *pos++ = WLAN_EID_VHT_CAPABILITY;
d4950281
MP
2380 *pos++ = sizeof(struct ieee80211_vht_cap);
2381 memset(pos, 0, sizeof(struct ieee80211_vht_cap));
ba0afa2f
MP
2382
2383 /* capability flags */
2384 tmp = cpu_to_le32(cap);
2385 memcpy(pos, &tmp, sizeof(u32));
2386 pos += sizeof(u32);
2387
2388 /* VHT MCS set */
2389 memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
2390 pos += sizeof(vht_cap->vht_mcs);
2391
2392 return pos;
2393}
2394
074d46d1 2395u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
4bf88530 2396 const struct cfg80211_chan_def *chandef,
57f255f5 2397 u16 prot_mode, bool rifs_mode)
42e7aa77 2398{
074d46d1 2399 struct ieee80211_ht_operation *ht_oper;
42e7aa77 2400 /* Build HT Information */
074d46d1
JB
2401 *pos++ = WLAN_EID_HT_OPERATION;
2402 *pos++ = sizeof(struct ieee80211_ht_operation);
2403 ht_oper = (struct ieee80211_ht_operation *)pos;
4bf88530
JB
2404 ht_oper->primary_chan = ieee80211_frequency_to_channel(
2405 chandef->chan->center_freq);
2406 switch (chandef->width) {
2407 case NL80211_CHAN_WIDTH_160:
2408 case NL80211_CHAN_WIDTH_80P80:
2409 case NL80211_CHAN_WIDTH_80:
2410 case NL80211_CHAN_WIDTH_40:
2411 if (chandef->center_freq1 > chandef->chan->center_freq)
2412 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
2413 else
2414 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
42e7aa77 2415 break;
42e7aa77 2416 default:
074d46d1 2417 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
42e7aa77
AS
2418 break;
2419 }
aee286c2 2420 if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
4bf88530
JB
2421 chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
2422 chandef->width != NL80211_CHAN_WIDTH_20)
074d46d1 2423 ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
ff3cc5f4 2424
57f255f5
AN
2425 if (rifs_mode)
2426 ht_oper->ht_param |= IEEE80211_HT_PARAM_RIFS_MODE;
2427
431e3154 2428 ht_oper->operation_mode = cpu_to_le16(prot_mode);
074d46d1 2429 ht_oper->stbc_param = 0x0000;
42e7aa77
AS
2430
2431 /* It seems that Basic MCS set and Supported MCS set
2432 are identical for the first 10 bytes */
074d46d1
JB
2433 memset(&ht_oper->basic_set, 0, 16);
2434 memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
42e7aa77 2435
074d46d1 2436 return pos + sizeof(struct ieee80211_ht_operation);
42e7aa77
AS
2437}
2438
75d627d5
SW
2439void ieee80211_ie_build_wide_bw_cs(u8 *pos,
2440 const struct cfg80211_chan_def *chandef)
2441{
2442 *pos++ = WLAN_EID_WIDE_BW_CHANNEL_SWITCH; /* EID */
2443 *pos++ = 3; /* IE length */
2444 /* New channel width */
2445 switch (chandef->width) {
2446 case NL80211_CHAN_WIDTH_80:
2447 *pos++ = IEEE80211_VHT_CHANWIDTH_80MHZ;
2448 break;
2449 case NL80211_CHAN_WIDTH_160:
2450 *pos++ = IEEE80211_VHT_CHANWIDTH_160MHZ;
2451 break;
2452 case NL80211_CHAN_WIDTH_80P80:
2453 *pos++ = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
2454 break;
2455 default:
2456 *pos++ = IEEE80211_VHT_CHANWIDTH_USE_HT;
2457 }
2458
2459 /* new center frequency segment 0 */
2460 *pos++ = ieee80211_frequency_to_channel(chandef->center_freq1);
2461 /* new center frequency segment 1 */
2462 if (chandef->center_freq2)
2463 *pos++ = ieee80211_frequency_to_channel(chandef->center_freq2);
2464 else
2465 *pos++ = 0;
2466}
2467
fb28ec0c
AN
2468u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
2469 const struct cfg80211_chan_def *chandef)
2470{
2471 struct ieee80211_vht_operation *vht_oper;
2472
2473 *pos++ = WLAN_EID_VHT_OPERATION;
2474 *pos++ = sizeof(struct ieee80211_vht_operation);
2475 vht_oper = (struct ieee80211_vht_operation *)pos;
2fb51c35 2476 vht_oper->center_freq_seg0_idx = ieee80211_frequency_to_channel(
fb28ec0c
AN
2477 chandef->center_freq1);
2478 if (chandef->center_freq2)
2fb51c35 2479 vht_oper->center_freq_seg1_idx =
fb28ec0c 2480 ieee80211_frequency_to_channel(chandef->center_freq2);
f020ae40 2481 else
2fb51c35 2482 vht_oper->center_freq_seg1_idx = 0x00;
fb28ec0c
AN
2483
2484 switch (chandef->width) {
2485 case NL80211_CHAN_WIDTH_160:
23665aaf
JM
2486 /*
2487 * Convert 160 MHz channel width to new style as interop
2488 * workaround.
2489 */
2490 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
2fb51c35 2491 vht_oper->center_freq_seg1_idx = vht_oper->center_freq_seg0_idx;
23665aaf 2492 if (chandef->chan->center_freq < chandef->center_freq1)
2fb51c35 2493 vht_oper->center_freq_seg0_idx -= 8;
23665aaf 2494 else
2fb51c35 2495 vht_oper->center_freq_seg0_idx += 8;
fb28ec0c
AN
2496 break;
2497 case NL80211_CHAN_WIDTH_80P80:
23665aaf
JM
2498 /*
2499 * Convert 80+80 MHz channel width to new style as interop
2500 * workaround.
2501 */
2502 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
fb28ec0c
AN
2503 break;
2504 case NL80211_CHAN_WIDTH_80:
2505 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
2506 break;
2507 default:
2508 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
2509 break;
2510 }
2511
2512 /* don't require special VHT peer rates */
2513 vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
2514
2515 return pos + sizeof(struct ieee80211_vht_operation);
2516}
2517
8ac3c704
JB
2518bool ieee80211_chandef_ht_oper(const struct ieee80211_ht_operation *ht_oper,
2519 struct cfg80211_chan_def *chandef)
42e7aa77
AS
2520{
2521 enum nl80211_channel_type channel_type;
2522
8ac3c704
JB
2523 if (!ht_oper)
2524 return false;
42e7aa77 2525
074d46d1 2526 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
42e7aa77
AS
2527 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
2528 channel_type = NL80211_CHAN_HT20;
2529 break;
2530 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
2531 channel_type = NL80211_CHAN_HT40PLUS;
2532 break;
2533 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
2534 channel_type = NL80211_CHAN_HT40MINUS;
2535 break;
2536 default:
2537 channel_type = NL80211_CHAN_NO_HT;
8ac3c704 2538 return false;
42e7aa77
AS
2539 }
2540
8ac3c704
JB
2541 cfg80211_chandef_create(chandef, chandef->chan, channel_type);
2542 return true;
42e7aa77
AS
2543}
2544
8ac3c704
JB
2545bool ieee80211_chandef_vht_oper(const struct ieee80211_vht_operation *oper,
2546 struct cfg80211_chan_def *chandef)
abcff6ef 2547{
8ac3c704
JB
2548 struct cfg80211_chan_def new = *chandef;
2549 int cf1, cf2;
2550
abcff6ef 2551 if (!oper)
8ac3c704 2552 return false;
abcff6ef 2553
2fb51c35 2554 cf1 = ieee80211_channel_to_frequency(oper->center_freq_seg0_idx,
8ac3c704 2555 chandef->chan->band);
2fb51c35 2556 cf2 = ieee80211_channel_to_frequency(oper->center_freq_seg1_idx,
8ac3c704 2557 chandef->chan->band);
abcff6ef
JD
2558
2559 switch (oper->chan_width) {
2560 case IEEE80211_VHT_CHANWIDTH_USE_HT:
2561 break;
2562 case IEEE80211_VHT_CHANWIDTH_80MHZ:
8ac3c704
JB
2563 new.width = NL80211_CHAN_WIDTH_80;
2564 new.center_freq1 = cf1;
23665aaf 2565 /* If needed, adjust based on the newer interop workaround. */
2fb51c35 2566 if (oper->center_freq_seg1_idx) {
23665aaf
JM
2567 unsigned int diff;
2568
2fb51c35
JB
2569 diff = abs(oper->center_freq_seg1_idx -
2570 oper->center_freq_seg0_idx);
23665aaf
JM
2571 if (diff == 8) {
2572 new.width = NL80211_CHAN_WIDTH_160;
2573 new.center_freq1 = cf2;
2574 } else if (diff > 8) {
2575 new.width = NL80211_CHAN_WIDTH_80P80;
2576 new.center_freq2 = cf2;
2577 }
2578 }
abcff6ef
JD
2579 break;
2580 case IEEE80211_VHT_CHANWIDTH_160MHZ:
8ac3c704
JB
2581 new.width = NL80211_CHAN_WIDTH_160;
2582 new.center_freq1 = cf1;
abcff6ef
JD
2583 break;
2584 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
8ac3c704
JB
2585 new.width = NL80211_CHAN_WIDTH_80P80;
2586 new.center_freq1 = cf1;
2587 new.center_freq2 = cf2;
abcff6ef
JD
2588 break;
2589 default:
8ac3c704 2590 return false;
abcff6ef
JD
2591 }
2592
8ac3c704
JB
2593 if (!cfg80211_chandef_valid(&new))
2594 return false;
2595
2596 *chandef = new;
2597 return true;
abcff6ef
JD
2598}
2599
2103dec1
SW
2600int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
2601 const struct ieee80211_supported_band *sband,
2602 const u8 *srates, int srates_len, u32 *rates)
2603{
2604 u32 rate_flags = ieee80211_chandef_rate_flags(chandef);
2605 int shift = ieee80211_chandef_get_shift(chandef);
2606 struct ieee80211_rate *br;
2607 int brate, rate, i, j, count = 0;
2608
2609 *rates = 0;
2610
2611 for (i = 0; i < srates_len; i++) {
2612 rate = srates[i] & 0x7f;
2613
2614 for (j = 0; j < sband->n_bitrates; j++) {
2615 br = &sband->bitrates[j];
2616 if ((rate_flags & br->flags) != rate_flags)
2617 continue;
2618
2619 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
2620 if (brate == rate) {
2621 *rates |= BIT(j);
2622 count++;
2623 break;
2624 }
2625 }
2626 }
2627 return count;
2628}
2629
fc8a7321 2630int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
6b77863b 2631 struct sk_buff *skb, bool need_basic,
57fbcce3 2632 enum nl80211_band band)
768db343 2633{
768db343
AN
2634 struct ieee80211_local *local = sdata->local;
2635 struct ieee80211_supported_band *sband;
2103dec1 2636 int rate, shift;
768db343 2637 u8 i, rates, *pos;
fc8a7321 2638 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
2103dec1 2639 u32 rate_flags;
768db343 2640
2103dec1
SW
2641 shift = ieee80211_vif_get_shift(&sdata->vif);
2642 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
6b77863b 2643 sband = local->hw.wiphy->bands[band];
2103dec1
SW
2644 rates = 0;
2645 for (i = 0; i < sband->n_bitrates; i++) {
2646 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
2647 continue;
2648 rates++;
2649 }
768db343
AN
2650 if (rates > 8)
2651 rates = 8;
2652
2653 if (skb_tailroom(skb) < rates + 2)
2654 return -ENOMEM;
2655
2656 pos = skb_put(skb, rates + 2);
2657 *pos++ = WLAN_EID_SUPP_RATES;
2658 *pos++ = rates;
2659 for (i = 0; i < rates; i++) {
657c3e0c 2660 u8 basic = 0;
2103dec1
SW
2661 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
2662 continue;
2663
657c3e0c
AN
2664 if (need_basic && basic_rates & BIT(i))
2665 basic = 0x80;
2103dec1
SW
2666 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
2667 5 * (1 << shift));
2668 *pos++ = basic | (u8) rate;
768db343
AN
2669 }
2670
2671 return 0;
2672}
2673
fc8a7321 2674int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
6b77863b 2675 struct sk_buff *skb, bool need_basic,
57fbcce3 2676 enum nl80211_band band)
768db343 2677{
768db343
AN
2678 struct ieee80211_local *local = sdata->local;
2679 struct ieee80211_supported_band *sband;
cc63ec76 2680 int rate, shift;
768db343 2681 u8 i, exrates, *pos;
fc8a7321 2682 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
2103dec1
SW
2683 u32 rate_flags;
2684
2685 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
2686 shift = ieee80211_vif_get_shift(&sdata->vif);
768db343 2687
6b77863b 2688 sband = local->hw.wiphy->bands[band];
2103dec1
SW
2689 exrates = 0;
2690 for (i = 0; i < sband->n_bitrates; i++) {
2691 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
2692 continue;
2693 exrates++;
2694 }
2695
768db343
AN
2696 if (exrates > 8)
2697 exrates -= 8;
2698 else
2699 exrates = 0;
2700
2701 if (skb_tailroom(skb) < exrates + 2)
2702 return -ENOMEM;
2703
2704 if (exrates) {
2705 pos = skb_put(skb, exrates + 2);
2706 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2707 *pos++ = exrates;
2708 for (i = 8; i < sband->n_bitrates; i++) {
657c3e0c 2709 u8 basic = 0;
2103dec1
SW
2710 if ((rate_flags & sband->bitrates[i].flags)
2711 != rate_flags)
2712 continue;
657c3e0c
AN
2713 if (need_basic && basic_rates & BIT(i))
2714 basic = 0x80;
2103dec1
SW
2715 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
2716 5 * (1 << shift));
2717 *pos++ = basic | (u8) rate;
768db343
AN
2718 }
2719 }
2720 return 0;
2721}
1dae27f8
WYG
2722
2723int ieee80211_ave_rssi(struct ieee80211_vif *vif)
2724{
2725 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2726 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2727
be6bcabc
WYG
2728 if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
2729 /* non-managed type inferfaces */
2730 return 0;
2731 }
338c17ae 2732 return -ewma_beacon_signal_read(&ifmgd->ave_beacon_signal);
1dae27f8 2733}
0d8a0a17 2734EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
04ecd257
JB
2735
2736u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
2737{
2738 if (!mcs)
2739 return 1;
2740
2741 /* TODO: consider rx_highest */
2742
2743 if (mcs->rx_mask[3])
2744 return 4;
2745 if (mcs->rx_mask[2])
2746 return 3;
2747 if (mcs->rx_mask[1])
2748 return 2;
2749 return 1;
2750}
f4bda337
TP
2751
2752/**
2753 * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
2754 * @local: mac80211 hw info struct
2755 * @status: RX status
2756 * @mpdu_len: total MPDU length (including FCS)
2757 * @mpdu_offset: offset into MPDU to calculate timestamp at
2758 *
2759 * This function calculates the RX timestamp at the given MPDU offset, taking
2760 * into account what the RX timestamp was. An offset of 0 will just normalize
2761 * the timestamp to TSF at beginning of MPDU reception.
2762 */
2763u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
2764 struct ieee80211_rx_status *status,
2765 unsigned int mpdu_len,
2766 unsigned int mpdu_offset)
2767{
2768 u64 ts = status->mactime;
2769 struct rate_info ri;
2770 u16 rate;
2771
2772 if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
2773 return 0;
2774
2775 memset(&ri, 0, sizeof(ri));
2776
35f4962c
JB
2777 ri.bw = status->bw;
2778
f4bda337 2779 /* Fill cfg80211 rate info */
da6a4352
JB
2780 switch (status->encoding) {
2781 case RX_ENC_HT:
f4bda337
TP
2782 ri.mcs = status->rate_idx;
2783 ri.flags |= RATE_INFO_FLAGS_MCS;
7fdd69c5 2784 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
f4bda337 2785 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
da6a4352
JB
2786 break;
2787 case RX_ENC_VHT:
5614618e
JB
2788 ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
2789 ri.mcs = status->rate_idx;
8613c948 2790 ri.nss = status->nss;
7fdd69c5 2791 if (status->enc_flags & RX_ENC_FLAG_SHORT_GI)
5614618e 2792 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
da6a4352
JB
2793 break;
2794 default:
2795 WARN_ON(1);
2796 /* fall through */
2797 case RX_ENC_LEGACY: {
f4bda337 2798 struct ieee80211_supported_band *sband;
2103dec1
SW
2799 int shift = 0;
2800 int bitrate;
2801
da6a4352
JB
2802 switch (status->bw) {
2803 case RATE_INFO_BW_10:
2103dec1 2804 shift = 1;
da6a4352
JB
2805 break;
2806 case RATE_INFO_BW_5:
2103dec1 2807 shift = 2;
da6a4352 2808 break;
b51f3bee 2809 }
f4bda337
TP
2810
2811 sband = local->hw.wiphy->bands[status->band];
2103dec1
SW
2812 bitrate = sband->bitrates[status->rate_idx].bitrate;
2813 ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
f4a0f0c5
JB
2814
2815 if (status->flag & RX_FLAG_MACTIME_PLCP_START) {
2816 /* TODO: handle HT/VHT preambles */
57fbcce3 2817 if (status->band == NL80211_BAND_5GHZ) {
f4a0f0c5
JB
2818 ts += 20 << shift;
2819 mpdu_offset += 2;
7fdd69c5 2820 } else if (status->enc_flags & RX_ENC_FLAG_SHORTPRE) {
f4a0f0c5
JB
2821 ts += 96;
2822 } else {
2823 ts += 192;
2824 }
2825 }
da6a4352
JB
2826 break;
2827 }
f4bda337
TP
2828 }
2829
2830 rate = cfg80211_calculate_bitrate(&ri);
d86aa4f8 2831 if (WARN_ONCE(!rate,
f980ebc0
SS
2832 "Invalid bitrate: flags=0x%llx, idx=%d, vht_nss=%d\n",
2833 (unsigned long long)status->flag, status->rate_idx,
8613c948 2834 status->nss))
d86aa4f8 2835 return 0;
f4bda337
TP
2836
2837 /* rewind from end of MPDU */
2838 if (status->flag & RX_FLAG_MACTIME_END)
2839 ts -= mpdu_len * 8 * 10 / rate;
2840
2841 ts += mpdu_offset * 8 * 10 / rate;
2842
2843 return ts;
2844}
164eb02d
SW
2845
2846void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
2847{
2848 struct ieee80211_sub_if_data *sdata;
d2859df5 2849 struct cfg80211_chan_def chandef;
164eb02d 2850
4a199068
JB
2851 /* for interface list, to avoid linking iflist_mtx and chanctx_mtx */
2852 ASSERT_RTNL();
2853
34a3740d 2854 mutex_lock(&local->mtx);
164eb02d 2855 list_for_each_entry(sdata, &local->interfaces, list) {
34a3740d
JB
2856 /* it might be waiting for the local->mtx, but then
2857 * by the time it gets it, sdata->wdev.cac_started
2858 * will no longer be true
2859 */
2860 cancel_delayed_work(&sdata->dfs_cac_timer_work);
164eb02d
SW
2861
2862 if (sdata->wdev.cac_started) {
d2859df5 2863 chandef = sdata->vif.bss_conf.chandef;
164eb02d
SW
2864 ieee80211_vif_release_channel(sdata);
2865 cfg80211_cac_event(sdata->dev,
d2859df5 2866 &chandef,
164eb02d
SW
2867 NL80211_RADAR_CAC_ABORTED,
2868 GFP_KERNEL);
2869 }
2870 }
34a3740d 2871 mutex_unlock(&local->mtx);
164eb02d
SW
2872}
2873
2874void ieee80211_dfs_radar_detected_work(struct work_struct *work)
2875{
2876 struct ieee80211_local *local =
2877 container_of(work, struct ieee80211_local, radar_detected_work);
84a3d1c9 2878 struct cfg80211_chan_def chandef = local->hw.conf.chandef;
486cf4c0
MK
2879 struct ieee80211_chanctx *ctx;
2880 int num_chanctx = 0;
2881
2882 mutex_lock(&local->chanctx_mtx);
2883 list_for_each_entry(ctx, &local->chanctx_list, list) {
2884 if (ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER)
2885 continue;
2886
2887 num_chanctx++;
2888 chandef = ctx->conf.def;
2889 }
2890 mutex_unlock(&local->chanctx_mtx);
164eb02d 2891
4a199068 2892 rtnl_lock();
164eb02d 2893 ieee80211_dfs_cac_cancel(local);
4a199068 2894 rtnl_unlock();
164eb02d 2895
486cf4c0
MK
2896 if (num_chanctx > 1)
2897 /* XXX: multi-channel is not supported yet */
164eb02d 2898 WARN_ON(1);
84a3d1c9 2899 else
164eb02d 2900 cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
164eb02d
SW
2901}
2902
2903void ieee80211_radar_detected(struct ieee80211_hw *hw)
2904{
2905 struct ieee80211_local *local = hw_to_local(hw);
2906
2907 trace_api_radar_detected(local);
2908
4a199068 2909 schedule_work(&local->radar_detected_work);
164eb02d
SW
2910}
2911EXPORT_SYMBOL(ieee80211_radar_detected);
e6b7cde4
SW
2912
2913u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
2914{
2915 u32 ret;
2916 int tmp;
2917
2918 switch (c->width) {
2919 case NL80211_CHAN_WIDTH_20:
2920 c->width = NL80211_CHAN_WIDTH_20_NOHT;
2921 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2922 break;
2923 case NL80211_CHAN_WIDTH_40:
2924 c->width = NL80211_CHAN_WIDTH_20;
2925 c->center_freq1 = c->chan->center_freq;
2926 ret = IEEE80211_STA_DISABLE_40MHZ |
2927 IEEE80211_STA_DISABLE_VHT;
2928 break;
2929 case NL80211_CHAN_WIDTH_80:
2930 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
2931 /* n_P40 */
2932 tmp /= 2;
2933 /* freq_P40 */
2934 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
2935 c->width = NL80211_CHAN_WIDTH_40;
2936 ret = IEEE80211_STA_DISABLE_VHT;
2937 break;
2938 case NL80211_CHAN_WIDTH_80P80:
2939 c->center_freq2 = 0;
2940 c->width = NL80211_CHAN_WIDTH_80;
2941 ret = IEEE80211_STA_DISABLE_80P80MHZ |
2942 IEEE80211_STA_DISABLE_160MHZ;
2943 break;
2944 case NL80211_CHAN_WIDTH_160:
2945 /* n_P20 */
2946 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
2947 /* n_P80 */
2948 tmp /= 4;
2949 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
2950 c->width = NL80211_CHAN_WIDTH_80;
2951 ret = IEEE80211_STA_DISABLE_80P80MHZ |
2952 IEEE80211_STA_DISABLE_160MHZ;
2953 break;
2954 default:
2955 case NL80211_CHAN_WIDTH_20_NOHT:
2956 WARN_ON_ONCE(1);
2957 c->width = NL80211_CHAN_WIDTH_20_NOHT;
2958 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2959 break;
2960 case NL80211_CHAN_WIDTH_5:
2961 case NL80211_CHAN_WIDTH_10:
2962 WARN_ON_ONCE(1);
2963 /* keep c->width */
2964 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2965 break;
2966 }
2967
2968 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
2969
2970 return ret;
2971}
687da132
EG
2972
2973/*
2974 * Returns true if smps_mode_new is strictly more restrictive than
2975 * smps_mode_old.
2976 */
2977bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
2978 enum ieee80211_smps_mode smps_mode_new)
2979{
2980 if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
2981 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
2982 return false;
2983
2984 switch (smps_mode_old) {
2985 case IEEE80211_SMPS_STATIC:
2986 return false;
2987 case IEEE80211_SMPS_DYNAMIC:
2988 return smps_mode_new == IEEE80211_SMPS_STATIC;
2989 case IEEE80211_SMPS_OFF:
2990 return smps_mode_new != IEEE80211_SMPS_OFF;
2991 default:
2992 WARN_ON(1);
2993 }
2994
2995 return false;
2996}
c6da674a
CYY
2997
2998int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
2999 struct cfg80211_csa_settings *csa_settings)
3000{
3001 struct sk_buff *skb;
3002 struct ieee80211_mgmt *mgmt;
3003 struct ieee80211_local *local = sdata->local;
3004 int freq;
4c121fd6
JB
3005 int hdr_len = offsetofend(struct ieee80211_mgmt,
3006 u.action.u.chan_switch);
c6da674a
CYY
3007 u8 *pos;
3008
3009 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
3010 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
3011 return -EOPNOTSUPP;
3012
3013 skb = dev_alloc_skb(local->tx_headroom + hdr_len +
3014 5 + /* channel switch announcement element */
3015 3 + /* secondary channel offset element */
75d627d5 3016 5 + /* wide bandwidth channel switch announcement */
c6da674a
CYY
3017 8); /* mesh channel switch parameters element */
3018 if (!skb)
3019 return -ENOMEM;
3020
3021 skb_reserve(skb, local->tx_headroom);
b080db58 3022 mgmt = skb_put_zero(skb, hdr_len);
c6da674a
CYY
3023 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3024 IEEE80211_STYPE_ACTION);
3025
3026 eth_broadcast_addr(mgmt->da);
3027 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
3028 if (ieee80211_vif_is_mesh(&sdata->vif)) {
3029 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
3030 } else {
3031 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
3032 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
3033 }
3034 mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
3035 mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
3036 pos = skb_put(skb, 5);
3037 *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
3038 *pos++ = 3; /* IE length */
3039 *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
3040 freq = csa_settings->chandef.chan->center_freq;
3041 *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
3042 *pos++ = csa_settings->count; /* count */
3043
3044 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
3045 enum nl80211_channel_type ch_type;
3046
3047 skb_put(skb, 3);
3048 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
3049 *pos++ = 1; /* IE length */
3050 ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
3051 if (ch_type == NL80211_CHAN_HT40PLUS)
3052 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
3053 else
3054 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
3055 }
3056
3057 if (ieee80211_vif_is_mesh(&sdata->vif)) {
3058 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
c6da674a
CYY
3059
3060 skb_put(skb, 8);
3061 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
3062 *pos++ = 6; /* IE length */
3063 *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
3064 *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
3065 *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
3066 *pos++ |= csa_settings->block_tx ?
3067 WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
3068 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
3069 pos += 2;
ca91dc97 3070 put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
c6da674a 3071 pos += 2;
c6da674a
CYY
3072 }
3073
75d627d5
SW
3074 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_80 ||
3075 csa_settings->chandef.width == NL80211_CHAN_WIDTH_80P80 ||
3076 csa_settings->chandef.width == NL80211_CHAN_WIDTH_160) {
3077 skb_put(skb, 5);
3078 ieee80211_ie_build_wide_bw_cs(pos, &csa_settings->chandef);
3079 }
3080
c6da674a
CYY
3081 ieee80211_tx_skb(sdata, skb);
3082 return 0;
3083}
2475b1cc
MS
3084
3085bool ieee80211_cs_valid(const struct ieee80211_cipher_scheme *cs)
3086{
3087 return !(cs == NULL || cs->cipher == 0 ||
3088 cs->hdr_len < cs->pn_len + cs->pn_off ||
3089 cs->hdr_len <= cs->key_idx_off ||
3090 cs->key_idx_shift > 7 ||
3091 cs->key_idx_mask == 0);
3092}
3093
3094bool ieee80211_cs_list_valid(const struct ieee80211_cipher_scheme *cs, int n)
3095{
3096 int i;
3097
3098 /* Ensure we have enough iftype bitmap space for all iftype values */
3099 WARN_ON((NUM_NL80211_IFTYPES / 8 + 1) > sizeof(cs[0].iftype));
3100
3101 for (i = 0; i < n; i++)
3102 if (!ieee80211_cs_valid(&cs[i]))
3103 return false;
3104
3105 return true;
3106}
3107
3108const struct ieee80211_cipher_scheme *
3109ieee80211_cs_get(struct ieee80211_local *local, u32 cipher,
3110 enum nl80211_iftype iftype)
3111{
3112 const struct ieee80211_cipher_scheme *l = local->hw.cipher_schemes;
3113 int n = local->hw.n_cipher_schemes;
3114 int i;
3115 const struct ieee80211_cipher_scheme *cs = NULL;
3116
3117 for (i = 0; i < n; i++) {
3118 if (l[i].cipher == cipher) {
3119 cs = &l[i];
3120 break;
3121 }
3122 }
3123
3124 if (!cs || !(cs->iftype & BIT(iftype)))
3125 return NULL;
3126
3127 return cs;
3128}
3129
3130int ieee80211_cs_headroom(struct ieee80211_local *local,
3131 struct cfg80211_crypto_settings *crypto,
3132 enum nl80211_iftype iftype)
3133{
3134 const struct ieee80211_cipher_scheme *cs;
3135 int headroom = IEEE80211_ENCRYPT_HEADROOM;
3136 int i;
3137
3138 for (i = 0; i < crypto->n_ciphers_pairwise; i++) {
3139 cs = ieee80211_cs_get(local, crypto->ciphers_pairwise[i],
3140 iftype);
3141
3142 if (cs && headroom < cs->hdr_len)
3143 headroom = cs->hdr_len;
3144 }
3145
3146 cs = ieee80211_cs_get(local, crypto->cipher_group, iftype);
3147 if (cs && headroom < cs->hdr_len)
3148 headroom = cs->hdr_len;
3149
3150 return headroom;
3151}
a7022e65
FF
3152
3153static bool
3154ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
3155{
3156 s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
3157 int skip;
3158
3159 if (end > 0)
3160 return false;
3161
519ee691
JD
3162 /* One shot NOA */
3163 if (data->count[i] == 1)
3164 return false;
3165
3166 if (data->desc[i].interval == 0)
3167 return false;
3168
a7022e65
FF
3169 /* End time is in the past, check for repetitions */
3170 skip = DIV_ROUND_UP(-end, data->desc[i].interval);
3171 if (data->count[i] < 255) {
3172 if (data->count[i] <= skip) {
3173 data->count[i] = 0;
3174 return false;
3175 }
3176
3177 data->count[i] -= skip;
3178 }
3179
3180 data->desc[i].start += skip * data->desc[i].interval;
3181
3182 return true;
3183}
3184
3185static bool
3186ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
3187 s32 *offset)
3188{
3189 bool ret = false;
3190 int i;
3191
3192 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3193 s32 cur;
3194
3195 if (!data->count[i])
3196 continue;
3197
3198 if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
3199 ret = true;
3200
3201 cur = data->desc[i].start - tsf;
3202 if (cur > *offset)
3203 continue;
3204
3205 cur = data->desc[i].start + data->desc[i].duration - tsf;
3206 if (cur > *offset)
3207 *offset = cur;
3208 }
3209
3210 return ret;
3211}
3212
3213static u32
3214ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
3215{
3216 s32 offset = 0;
3217 int tries = 0;
3218 /*
3219 * arbitrary limit, used to avoid infinite loops when combined NoA
3220 * descriptors cover the full time period.
3221 */
3222 int max_tries = 5;
3223
3224 ieee80211_extend_absent_time(data, tsf, &offset);
3225 do {
3226 if (!ieee80211_extend_absent_time(data, tsf, &offset))
3227 break;
3228
3229 tries++;
3230 } while (tries < max_tries);
3231
3232 return offset;
3233}
3234
3235void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
3236{
3237 u32 next_offset = BIT(31) - 1;
3238 int i;
3239
3240 data->absent = 0;
3241 data->has_next_tsf = false;
3242 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3243 s32 start;
3244
3245 if (!data->count[i])
3246 continue;
3247
3248 ieee80211_extend_noa_desc(data, tsf, i);
3249 start = data->desc[i].start - tsf;
3250 if (start <= 0)
3251 data->absent |= BIT(i);
3252
3253 if (next_offset > start)
3254 next_offset = start;
3255
3256 data->has_next_tsf = true;
3257 }
3258
3259 if (data->absent)
3260 next_offset = ieee80211_get_noa_absent_time(data, tsf);
3261
3262 data->next_tsf = tsf + next_offset;
3263}
3264EXPORT_SYMBOL(ieee80211_update_p2p_noa);
3265
3266int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
3267 struct ieee80211_noa_data *data, u32 tsf)
3268{
3269 int ret = 0;
3270 int i;
3271
3272 memset(data, 0, sizeof(*data));
3273
3274 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
3275 const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
3276
3277 if (!desc->count || !desc->duration)
3278 continue;
3279
3280 data->count[i] = desc->count;
3281 data->desc[i].start = le32_to_cpu(desc->start_time);
3282 data->desc[i].duration = le32_to_cpu(desc->duration);
3283 data->desc[i].interval = le32_to_cpu(desc->interval);
3284
3285 if (data->count[i] > 1 &&
3286 data->desc[i].interval < data->desc[i].duration)
3287 continue;
3288
3289 ieee80211_extend_noa_desc(data, tsf, i);
3290 ret++;
3291 }
3292
3293 if (ret)
3294 ieee80211_update_p2p_noa(data, tsf);
3295
3296 return ret;
3297}
3298EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
057d5f4b
TP
3299
3300void ieee80211_recalc_dtim(struct ieee80211_local *local,
3301 struct ieee80211_sub_if_data *sdata)
3302{
3303 u64 tsf = drv_get_tsf(local, sdata);
3304 u64 dtim_count = 0;
3305 u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
3306 u8 dtim_period = sdata->vif.bss_conf.dtim_period;
3307 struct ps_data *ps;
3308 u8 bcns_from_dtim;
3309
3310 if (tsf == -1ULL || !beacon_int || !dtim_period)
3311 return;
3312
3313 if (sdata->vif.type == NL80211_IFTYPE_AP ||
3314 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
3315 if (!sdata->bss)
3316 return;
3317
3318 ps = &sdata->bss->ps;
3319 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
3320 ps = &sdata->u.mesh.ps;
3321 } else {
3322 return;
3323 }
3324
3325 /*
3326 * actually finds last dtim_count, mac80211 will update in
3327 * __beacon_add_tim().
3328 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
3329 */
3330 do_div(tsf, beacon_int);
3331 bcns_from_dtim = do_div(tsf, dtim_period);
3332 /* just had a DTIM */
3333 if (!bcns_from_dtim)
3334 dtim_count = 0;
3335 else
3336 dtim_count = dtim_period - bcns_from_dtim;
3337
3338 ps->dtim_count = dtim_count;
3339}
73de86a3 3340
71e6195e
MK
3341static u8 ieee80211_chanctx_radar_detect(struct ieee80211_local *local,
3342 struct ieee80211_chanctx *ctx)
3343{
3344 struct ieee80211_sub_if_data *sdata;
3345 u8 radar_detect = 0;
3346
3347 lockdep_assert_held(&local->chanctx_mtx);
3348
3349 if (WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED))
3350 return 0;
3351
3352 list_for_each_entry(sdata, &ctx->reserved_vifs, reserved_chanctx_list)
3353 if (sdata->reserved_radar_required)
3354 radar_detect |= BIT(sdata->reserved_chandef.width);
3355
3356 /*
3357 * An in-place reservation context should not have any assigned vifs
3358 * until it replaces the other context.
3359 */
3360 WARN_ON(ctx->replace_state == IEEE80211_CHANCTX_REPLACES_OTHER &&
3361 !list_empty(&ctx->assigned_vifs));
3362
3363 list_for_each_entry(sdata, &ctx->assigned_vifs, assigned_chanctx_list)
3364 if (sdata->radar_required)
3365 radar_detect |= BIT(sdata->vif.bss_conf.chandef.width);
3366
3367 return radar_detect;
3368}
3369
73de86a3
LC
3370int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
3371 const struct cfg80211_chan_def *chandef,
3372 enum ieee80211_chanctx_mode chanmode,
3373 u8 radar_detect)
3374{
3375 struct ieee80211_local *local = sdata->local;
3376 struct ieee80211_sub_if_data *sdata_iter;
3377 enum nl80211_iftype iftype = sdata->wdev.iftype;
73de86a3 3378 struct ieee80211_chanctx *ctx;
73de86a3 3379 int total = 1;
e227300c
PK
3380 struct iface_combination_params params = {
3381 .radar_detect = radar_detect,
3382 };
73de86a3
LC
3383
3384 lockdep_assert_held(&local->chanctx_mtx);
3385
3386 if (WARN_ON(hweight32(radar_detect) > 1))
3387 return -EINVAL;
3388
b6a55015
LC
3389 if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
3390 !chandef->chan))
73de86a3
LC
3391 return -EINVAL;
3392
3393 if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
3394 return -EINVAL;
3395
ac668afe
JB
3396 if (sdata->vif.type == NL80211_IFTYPE_AP ||
3397 sdata->vif.type == NL80211_IFTYPE_MESH_POINT) {
3398 /*
3399 * always passing this is harmless, since it'll be the
3400 * same value that cfg80211 finds if it finds the same
3401 * interface ... and that's always allowed
3402 */
3403 params.new_beacon_int = sdata->vif.bss_conf.beacon_int;
3404 }
3405
73de86a3
LC
3406 /* Always allow software iftypes */
3407 if (local->hw.wiphy->software_iftypes & BIT(iftype)) {
3408 if (radar_detect)
3409 return -EINVAL;
3410 return 0;
3411 }
3412
e227300c
PK
3413 if (chandef)
3414 params.num_different_channels = 1;
73de86a3
LC
3415
3416 if (iftype != NL80211_IFTYPE_UNSPECIFIED)
e227300c 3417 params.iftype_num[iftype] = 1;
73de86a3
LC
3418
3419 list_for_each_entry(ctx, &local->chanctx_list, list) {
5bcae31d
MK
3420 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
3421 continue;
e227300c
PK
3422 params.radar_detect |=
3423 ieee80211_chanctx_radar_detect(local, ctx);
73de86a3 3424 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
e227300c 3425 params.num_different_channels++;
73de86a3
LC
3426 continue;
3427 }
b6a55015 3428 if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
73de86a3
LC
3429 cfg80211_chandef_compatible(chandef,
3430 &ctx->conf.def))
3431 continue;
e227300c 3432 params.num_different_channels++;
73de86a3
LC
3433 }
3434
3435 list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
3436 struct wireless_dev *wdev_iter;
3437
3438 wdev_iter = &sdata_iter->wdev;
3439
3440 if (sdata_iter == sdata ||
0f611d28 3441 !ieee80211_sdata_running(sdata_iter) ||
73de86a3
LC
3442 local->hw.wiphy->software_iftypes & BIT(wdev_iter->iftype))
3443 continue;
3444
e227300c 3445 params.iftype_num[wdev_iter->iftype]++;
73de86a3
LC
3446 total++;
3447 }
3448
e227300c 3449 if (total == 1 && !params.radar_detect)
73de86a3
LC
3450 return 0;
3451
e227300c 3452 return cfg80211_check_combinations(local->hw.wiphy, &params);
73de86a3 3453}
6fa001bc
MK
3454
3455static void
3456ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
3457 void *data)
3458{
3459 u32 *max_num_different_channels = data;
3460
3461 *max_num_different_channels = max(*max_num_different_channels,
3462 c->num_different_channels);
3463}
3464
3465int ieee80211_max_num_channels(struct ieee80211_local *local)
3466{
3467 struct ieee80211_sub_if_data *sdata;
6fa001bc 3468 struct ieee80211_chanctx *ctx;
6fa001bc
MK
3469 u32 max_num_different_channels = 1;
3470 int err;
e227300c 3471 struct iface_combination_params params = {0};
6fa001bc
MK
3472
3473 lockdep_assert_held(&local->chanctx_mtx);
3474
3475 list_for_each_entry(ctx, &local->chanctx_list, list) {
5bcae31d
MK
3476 if (ctx->replace_state == IEEE80211_CHANCTX_WILL_BE_REPLACED)
3477 continue;
3478
e227300c 3479 params.num_different_channels++;
6fa001bc 3480
e227300c
PK
3481 params.radar_detect |=
3482 ieee80211_chanctx_radar_detect(local, ctx);
6fa001bc
MK
3483 }
3484
3485 list_for_each_entry_rcu(sdata, &local->interfaces, list)
e227300c 3486 params.iftype_num[sdata->wdev.iftype]++;
6fa001bc 3487
e227300c
PK
3488 err = cfg80211_iter_combinations(local->hw.wiphy, &params,
3489 ieee80211_iter_max_chans,
6fa001bc
MK
3490 &max_num_different_channels);
3491 if (err < 0)
3492 return err;
3493
3494 return max_num_different_channels;
3495}
40b861a0
AN
3496
3497u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
3498{
3499 *buf++ = WLAN_EID_VENDOR_SPECIFIC;
3500 *buf++ = 7; /* len */
3501 *buf++ = 0x00; /* Microsoft OUI 00:50:F2 */
3502 *buf++ = 0x50;
3503 *buf++ = 0xf2;
3504 *buf++ = 2; /* WME */
3505 *buf++ = 0; /* WME info */
3506 *buf++ = 1; /* WME ver */
3507 *buf++ = qosinfo; /* U-APSD no in use */
3508
3509 return buf;
3510}
ba8c3d6f 3511
f2ac7e30
MK
3512void ieee80211_txq_get_depth(struct ieee80211_txq *txq,
3513 unsigned long *frame_cnt,
3514 unsigned long *byte_cnt)
3515{
3516 struct txq_info *txqi = to_txq_info(txq);
bb42f2d1
THJ
3517 u32 frag_cnt = 0, frag_bytes = 0;
3518 struct sk_buff *skb;
3519
3520 skb_queue_walk(&txqi->frags, skb) {
3521 frag_cnt++;
3522 frag_bytes += skb->len;
3523 }
f2ac7e30
MK
3524
3525 if (frame_cnt)
bb42f2d1 3526 *frame_cnt = txqi->tin.backlog_packets + frag_cnt;
f2ac7e30
MK
3527
3528 if (byte_cnt)
bb42f2d1 3529 *byte_cnt = txqi->tin.backlog_bytes + frag_bytes;
f2ac7e30
MK
3530}
3531EXPORT_SYMBOL(ieee80211_txq_get_depth);
f438ceb8
EG
3532
3533const u8 ieee80211_ac_to_qos_mask[IEEE80211_NUM_ACS] = {
3534 IEEE80211_WMM_IE_STA_QOSINFO_AC_VO,
3535 IEEE80211_WMM_IE_STA_QOSINFO_AC_VI,
3536 IEEE80211_WMM_IE_STA_QOSINFO_AC_BE,
3537 IEEE80211_WMM_IE_STA_QOSINFO_AC_BK
3538};