mac80211: introduce refcount for queue_stop_reasons
[deliverable/linux.git] / net / mac80211 / util.c
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>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * utilities for mac80211
12 */
13
14 #include <net/mac80211.h>
15 #include <linux/netdevice.h>
16 #include <linux/export.h>
17 #include <linux/types.h>
18 #include <linux/slab.h>
19 #include <linux/skbuff.h>
20 #include <linux/etherdevice.h>
21 #include <linux/if_arp.h>
22 #include <linux/bitmap.h>
23 #include <linux/crc32.h>
24 #include <net/net_namespace.h>
25 #include <net/cfg80211.h>
26 #include <net/rtnetlink.h>
27
28 #include "ieee80211_i.h"
29 #include "driver-ops.h"
30 #include "rate.h"
31 #include "mesh.h"
32 #include "wme.h"
33 #include "led.h"
34 #include "wep.h"
35
36 /* privid for wiphys to determine whether they belong to us or not */
37 const void *const mac80211_wiphy_privid = &mac80211_wiphy_privid;
38
39 struct ieee80211_hw *wiphy_to_ieee80211_hw(struct wiphy *wiphy)
40 {
41 struct ieee80211_local *local;
42 BUG_ON(!wiphy);
43
44 local = wiphy_priv(wiphy);
45 return &local->hw;
46 }
47 EXPORT_SYMBOL(wiphy_to_ieee80211_hw);
48
49 u8 *ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len,
50 enum nl80211_iftype type)
51 {
52 __le16 fc = hdr->frame_control;
53
54 /* drop ACK/CTS frames and incorrect hdr len (ctrl) */
55 if (len < 16)
56 return NULL;
57
58 if (ieee80211_is_data(fc)) {
59 if (len < 24) /* drop incorrect hdr len (data) */
60 return NULL;
61
62 if (ieee80211_has_a4(fc))
63 return NULL;
64 if (ieee80211_has_tods(fc))
65 return hdr->addr1;
66 if (ieee80211_has_fromds(fc))
67 return hdr->addr2;
68
69 return hdr->addr3;
70 }
71
72 if (ieee80211_is_mgmt(fc)) {
73 if (len < 24) /* drop incorrect hdr len (mgmt) */
74 return NULL;
75 return hdr->addr3;
76 }
77
78 if (ieee80211_is_ctl(fc)) {
79 if (ieee80211_is_pspoll(fc))
80 return hdr->addr1;
81
82 if (ieee80211_is_back_req(fc)) {
83 switch (type) {
84 case NL80211_IFTYPE_STATION:
85 return hdr->addr2;
86 case NL80211_IFTYPE_AP:
87 case NL80211_IFTYPE_AP_VLAN:
88 return hdr->addr1;
89 default:
90 break; /* fall through to the return */
91 }
92 }
93 }
94
95 return NULL;
96 }
97
98 void ieee80211_tx_set_protected(struct ieee80211_tx_data *tx)
99 {
100 struct sk_buff *skb;
101 struct ieee80211_hdr *hdr;
102
103 skb_queue_walk(&tx->skbs, skb) {
104 hdr = (struct ieee80211_hdr *) skb->data;
105 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
106 }
107 }
108
109 int ieee80211_frame_duration(enum ieee80211_band band, size_t len,
110 int rate, int erp, int short_preamble,
111 int shift)
112 {
113 int dur;
114
115 /* calculate duration (in microseconds, rounded up to next higher
116 * integer if it includes a fractional microsecond) to send frame of
117 * len bytes (does not include FCS) at the given rate. Duration will
118 * also include SIFS.
119 *
120 * rate is in 100 kbps, so divident is multiplied by 10 in the
121 * DIV_ROUND_UP() operations.
122 *
123 * shift may be 2 for 5 MHz channels or 1 for 10 MHz channels, and
124 * is assumed to be 0 otherwise.
125 */
126
127 if (band == IEEE80211_BAND_5GHZ || erp) {
128 /*
129 * OFDM:
130 *
131 * N_DBPS = DATARATE x 4
132 * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
133 * (16 = SIGNAL time, 6 = tail bits)
134 * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
135 *
136 * T_SYM = 4 usec
137 * 802.11a - 18.5.2: aSIFSTime = 16 usec
138 * 802.11g - 19.8.4: aSIFSTime = 10 usec +
139 * signal ext = 6 usec
140 */
141 dur = 16; /* SIFS + signal ext */
142 dur += 16; /* IEEE 802.11-2012 18.3.2.4: T_PREAMBLE = 16 usec */
143 dur += 4; /* IEEE 802.11-2012 18.3.2.4: T_SIGNAL = 4 usec */
144
145 /* IEEE 802.11-2012 18.3.2.4: all values above are:
146 * * times 4 for 5 MHz
147 * * times 2 for 10 MHz
148 */
149 dur *= 1 << shift;
150
151 /* rates should already consider the channel bandwidth,
152 * don't apply divisor again.
153 */
154 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
155 4 * rate); /* T_SYM x N_SYM */
156 } else {
157 /*
158 * 802.11b or 802.11g with 802.11b compatibility:
159 * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
160 * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
161 *
162 * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
163 * aSIFSTime = 10 usec
164 * aPreambleLength = 144 usec or 72 usec with short preamble
165 * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
166 */
167 dur = 10; /* aSIFSTime = 10 usec */
168 dur += short_preamble ? (72 + 24) : (144 + 48);
169
170 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
171 }
172
173 return dur;
174 }
175
176 /* Exported duration function for driver use */
177 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
178 struct ieee80211_vif *vif,
179 enum ieee80211_band band,
180 size_t frame_len,
181 struct ieee80211_rate *rate)
182 {
183 struct ieee80211_sub_if_data *sdata;
184 u16 dur;
185 int erp, shift = 0;
186 bool short_preamble = false;
187
188 erp = 0;
189 if (vif) {
190 sdata = vif_to_sdata(vif);
191 short_preamble = sdata->vif.bss_conf.use_short_preamble;
192 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
193 erp = rate->flags & IEEE80211_RATE_ERP_G;
194 shift = ieee80211_vif_get_shift(vif);
195 }
196
197 dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
198 short_preamble, shift);
199
200 return cpu_to_le16(dur);
201 }
202 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
203
204 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
205 struct ieee80211_vif *vif, size_t frame_len,
206 const struct ieee80211_tx_info *frame_txctl)
207 {
208 struct ieee80211_local *local = hw_to_local(hw);
209 struct ieee80211_rate *rate;
210 struct ieee80211_sub_if_data *sdata;
211 bool short_preamble;
212 int erp, shift = 0, bitrate;
213 u16 dur;
214 struct ieee80211_supported_band *sband;
215
216 sband = local->hw.wiphy->bands[frame_txctl->band];
217
218 short_preamble = false;
219
220 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
221
222 erp = 0;
223 if (vif) {
224 sdata = vif_to_sdata(vif);
225 short_preamble = sdata->vif.bss_conf.use_short_preamble;
226 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
227 erp = rate->flags & IEEE80211_RATE_ERP_G;
228 shift = ieee80211_vif_get_shift(vif);
229 }
230
231 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
232
233 /* CTS duration */
234 dur = ieee80211_frame_duration(sband->band, 10, bitrate,
235 erp, short_preamble, shift);
236 /* Data frame duration */
237 dur += ieee80211_frame_duration(sband->band, frame_len, bitrate,
238 erp, short_preamble, shift);
239 /* ACK duration */
240 dur += ieee80211_frame_duration(sband->band, 10, bitrate,
241 erp, short_preamble, shift);
242
243 return cpu_to_le16(dur);
244 }
245 EXPORT_SYMBOL(ieee80211_rts_duration);
246
247 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
248 struct ieee80211_vif *vif,
249 size_t frame_len,
250 const struct ieee80211_tx_info *frame_txctl)
251 {
252 struct ieee80211_local *local = hw_to_local(hw);
253 struct ieee80211_rate *rate;
254 struct ieee80211_sub_if_data *sdata;
255 bool short_preamble;
256 int erp, shift = 0, bitrate;
257 u16 dur;
258 struct ieee80211_supported_band *sband;
259
260 sband = local->hw.wiphy->bands[frame_txctl->band];
261
262 short_preamble = false;
263
264 rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
265 erp = 0;
266 if (vif) {
267 sdata = vif_to_sdata(vif);
268 short_preamble = sdata->vif.bss_conf.use_short_preamble;
269 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
270 erp = rate->flags & IEEE80211_RATE_ERP_G;
271 shift = ieee80211_vif_get_shift(vif);
272 }
273
274 bitrate = DIV_ROUND_UP(rate->bitrate, 1 << shift);
275
276 /* Data frame duration */
277 dur = ieee80211_frame_duration(sband->band, frame_len, bitrate,
278 erp, short_preamble, shift);
279 if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
280 /* ACK duration */
281 dur += ieee80211_frame_duration(sband->band, 10, bitrate,
282 erp, short_preamble, shift);
283 }
284
285 return cpu_to_le16(dur);
286 }
287 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
288
289 void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
290 {
291 struct ieee80211_sub_if_data *sdata;
292 int n_acs = IEEE80211_NUM_ACS;
293
294 if (local->hw.queues < IEEE80211_NUM_ACS)
295 n_acs = 1;
296
297 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
298 int ac;
299
300 if (!sdata->dev)
301 continue;
302
303 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
304 local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
305 continue;
306
307 for (ac = 0; ac < n_acs; ac++) {
308 int ac_queue = sdata->vif.hw_queue[ac];
309
310 if (ac_queue == queue ||
311 (sdata->vif.cab_queue == queue &&
312 local->queue_stop_reasons[ac_queue] == 0 &&
313 skb_queue_empty(&local->pending[ac_queue])))
314 netif_wake_subqueue(sdata->dev, ac);
315 }
316 }
317 }
318
319 static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
320 enum queue_stop_reason reason,
321 bool refcounted)
322 {
323 struct ieee80211_local *local = hw_to_local(hw);
324
325 trace_wake_queue(local, queue, reason);
326
327 if (WARN_ON(queue >= hw->queues))
328 return;
329
330 if (!test_bit(reason, &local->queue_stop_reasons[queue]))
331 return;
332
333 if (!refcounted)
334 local->q_stop_reasons[queue][reason] = 0;
335 else
336 local->q_stop_reasons[queue][reason]--;
337
338 if (local->q_stop_reasons[queue][reason] == 0)
339 __clear_bit(reason, &local->queue_stop_reasons[queue]);
340
341 if (local->queue_stop_reasons[queue] != 0)
342 /* someone still has this queue stopped */
343 return;
344
345 if (skb_queue_empty(&local->pending[queue])) {
346 rcu_read_lock();
347 ieee80211_propagate_queue_wake(local, queue);
348 rcu_read_unlock();
349 } else
350 tasklet_schedule(&local->tx_pending_tasklet);
351 }
352
353 void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
354 enum queue_stop_reason reason,
355 bool refcounted)
356 {
357 struct ieee80211_local *local = hw_to_local(hw);
358 unsigned long flags;
359
360 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
361 __ieee80211_wake_queue(hw, queue, reason, refcounted);
362 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
363 }
364
365 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
366 {
367 ieee80211_wake_queue_by_reason(hw, queue,
368 IEEE80211_QUEUE_STOP_REASON_DRIVER,
369 false);
370 }
371 EXPORT_SYMBOL(ieee80211_wake_queue);
372
373 static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
374 enum queue_stop_reason reason,
375 bool refcounted)
376 {
377 struct ieee80211_local *local = hw_to_local(hw);
378 struct ieee80211_sub_if_data *sdata;
379 int n_acs = IEEE80211_NUM_ACS;
380
381 trace_stop_queue(local, queue, reason);
382
383 if (WARN_ON(queue >= hw->queues))
384 return;
385
386 if (!refcounted)
387 local->q_stop_reasons[queue][reason] = 1;
388 else
389 local->q_stop_reasons[queue][reason]++;
390
391 if (__test_and_set_bit(reason, &local->queue_stop_reasons[queue]))
392 return;
393
394 if (local->hw.queues < IEEE80211_NUM_ACS)
395 n_acs = 1;
396
397 rcu_read_lock();
398 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
399 int ac;
400
401 if (!sdata->dev)
402 continue;
403
404 for (ac = 0; ac < n_acs; ac++) {
405 if (sdata->vif.hw_queue[ac] == queue ||
406 sdata->vif.cab_queue == queue)
407 netif_stop_subqueue(sdata->dev, ac);
408 }
409 }
410 rcu_read_unlock();
411 }
412
413 void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
414 enum queue_stop_reason reason,
415 bool refcounted)
416 {
417 struct ieee80211_local *local = hw_to_local(hw);
418 unsigned long flags;
419
420 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
421 __ieee80211_stop_queue(hw, queue, reason, refcounted);
422 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
423 }
424
425 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
426 {
427 ieee80211_stop_queue_by_reason(hw, queue,
428 IEEE80211_QUEUE_STOP_REASON_DRIVER,
429 false);
430 }
431 EXPORT_SYMBOL(ieee80211_stop_queue);
432
433 void ieee80211_add_pending_skb(struct ieee80211_local *local,
434 struct sk_buff *skb)
435 {
436 struct ieee80211_hw *hw = &local->hw;
437 unsigned long flags;
438 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
439 int queue = info->hw_queue;
440
441 if (WARN_ON(!info->control.vif)) {
442 ieee80211_free_txskb(&local->hw, skb);
443 return;
444 }
445
446 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
447 __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
448 false);
449 __skb_queue_tail(&local->pending[queue], skb);
450 __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
451 false);
452 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
453 }
454
455 void ieee80211_add_pending_skbs(struct ieee80211_local *local,
456 struct sk_buff_head *skbs)
457 {
458 struct ieee80211_hw *hw = &local->hw;
459 struct sk_buff *skb;
460 unsigned long flags;
461 int queue, i;
462
463 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
464 while ((skb = skb_dequeue(skbs))) {
465 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
466
467 if (WARN_ON(!info->control.vif)) {
468 ieee80211_free_txskb(&local->hw, skb);
469 continue;
470 }
471
472 queue = info->hw_queue;
473
474 __ieee80211_stop_queue(hw, queue,
475 IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
476 false);
477
478 __skb_queue_tail(&local->pending[queue], skb);
479 }
480
481 for (i = 0; i < hw->queues; i++)
482 __ieee80211_wake_queue(hw, i,
483 IEEE80211_QUEUE_STOP_REASON_SKB_ADD,
484 false);
485 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
486 }
487
488 void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
489 unsigned long queues,
490 enum queue_stop_reason reason,
491 bool refcounted)
492 {
493 struct ieee80211_local *local = hw_to_local(hw);
494 unsigned long flags;
495 int i;
496
497 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
498
499 for_each_set_bit(i, &queues, hw->queues)
500 __ieee80211_stop_queue(hw, i, reason, refcounted);
501
502 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
503 }
504
505 void ieee80211_stop_queues(struct ieee80211_hw *hw)
506 {
507 ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
508 IEEE80211_QUEUE_STOP_REASON_DRIVER,
509 false);
510 }
511 EXPORT_SYMBOL(ieee80211_stop_queues);
512
513 int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
514 {
515 struct ieee80211_local *local = hw_to_local(hw);
516 unsigned long flags;
517 int ret;
518
519 if (WARN_ON(queue >= hw->queues))
520 return true;
521
522 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
523 ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
524 &local->queue_stop_reasons[queue]);
525 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
526 return ret;
527 }
528 EXPORT_SYMBOL(ieee80211_queue_stopped);
529
530 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
531 unsigned long queues,
532 enum queue_stop_reason reason,
533 bool refcounted)
534 {
535 struct ieee80211_local *local = hw_to_local(hw);
536 unsigned long flags;
537 int i;
538
539 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
540
541 for_each_set_bit(i, &queues, hw->queues)
542 __ieee80211_wake_queue(hw, i, reason, refcounted);
543
544 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
545 }
546
547 void ieee80211_wake_queues(struct ieee80211_hw *hw)
548 {
549 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
550 IEEE80211_QUEUE_STOP_REASON_DRIVER,
551 false);
552 }
553 EXPORT_SYMBOL(ieee80211_wake_queues);
554
555 void ieee80211_flush_queues(struct ieee80211_local *local,
556 struct ieee80211_sub_if_data *sdata)
557 {
558 u32 queues;
559
560 if (!local->ops->flush)
561 return;
562
563 if (sdata && local->hw.flags & IEEE80211_HW_QUEUE_CONTROL) {
564 int ac;
565
566 queues = 0;
567
568 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
569 queues |= BIT(sdata->vif.hw_queue[ac]);
570 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
571 queues |= BIT(sdata->vif.cab_queue);
572 } else {
573 /* all queues */
574 queues = BIT(local->hw.queues) - 1;
575 }
576
577 ieee80211_stop_queues_by_reason(&local->hw, queues,
578 IEEE80211_QUEUE_STOP_REASON_FLUSH,
579 false);
580
581 drv_flush(local, sdata, queues, false);
582
583 ieee80211_wake_queues_by_reason(&local->hw, queues,
584 IEEE80211_QUEUE_STOP_REASON_FLUSH,
585 false);
586 }
587
588 static void __iterate_active_interfaces(struct ieee80211_local *local,
589 u32 iter_flags,
590 void (*iterator)(void *data, u8 *mac,
591 struct ieee80211_vif *vif),
592 void *data)
593 {
594 struct ieee80211_sub_if_data *sdata;
595
596 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
597 switch (sdata->vif.type) {
598 case NL80211_IFTYPE_MONITOR:
599 if (!(sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE))
600 continue;
601 break;
602 case NL80211_IFTYPE_AP_VLAN:
603 continue;
604 default:
605 break;
606 }
607 if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
608 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
609 continue;
610 if (ieee80211_sdata_running(sdata))
611 iterator(data, sdata->vif.addr,
612 &sdata->vif);
613 }
614
615 sdata = rcu_dereference_check(local->monitor_sdata,
616 lockdep_is_held(&local->iflist_mtx) ||
617 lockdep_rtnl_is_held());
618 if (sdata &&
619 (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
620 sdata->flags & IEEE80211_SDATA_IN_DRIVER))
621 iterator(data, sdata->vif.addr, &sdata->vif);
622 }
623
624 void ieee80211_iterate_active_interfaces(
625 struct ieee80211_hw *hw, u32 iter_flags,
626 void (*iterator)(void *data, u8 *mac,
627 struct ieee80211_vif *vif),
628 void *data)
629 {
630 struct ieee80211_local *local = hw_to_local(hw);
631
632 mutex_lock(&local->iflist_mtx);
633 __iterate_active_interfaces(local, iter_flags, iterator, data);
634 mutex_unlock(&local->iflist_mtx);
635 }
636 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
637
638 void ieee80211_iterate_active_interfaces_atomic(
639 struct ieee80211_hw *hw, u32 iter_flags,
640 void (*iterator)(void *data, u8 *mac,
641 struct ieee80211_vif *vif),
642 void *data)
643 {
644 struct ieee80211_local *local = hw_to_local(hw);
645
646 rcu_read_lock();
647 __iterate_active_interfaces(local, iter_flags, iterator, data);
648 rcu_read_unlock();
649 }
650 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
651
652 void ieee80211_iterate_active_interfaces_rtnl(
653 struct ieee80211_hw *hw, u32 iter_flags,
654 void (*iterator)(void *data, u8 *mac,
655 struct ieee80211_vif *vif),
656 void *data)
657 {
658 struct ieee80211_local *local = hw_to_local(hw);
659
660 ASSERT_RTNL();
661
662 __iterate_active_interfaces(local, iter_flags, iterator, data);
663 }
664 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_rtnl);
665
666 struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
667 {
668 struct ieee80211_sub_if_data *sdata = IEEE80211_WDEV_TO_SUB_IF(wdev);
669
670 if (!ieee80211_sdata_running(sdata) ||
671 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
672 return NULL;
673 return &sdata->vif;
674 }
675 EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
676
677 /*
678 * Nothing should have been stuffed into the workqueue during
679 * the suspend->resume cycle. If this WARN is seen then there
680 * is a bug with either the driver suspend or something in
681 * mac80211 stuffing into the workqueue which we haven't yet
682 * cleared during mac80211's suspend cycle.
683 */
684 static bool ieee80211_can_queue_work(struct ieee80211_local *local)
685 {
686 if (WARN(local->suspended && !local->resuming,
687 "queueing ieee80211 work while going to suspend\n"))
688 return false;
689
690 return true;
691 }
692
693 void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
694 {
695 struct ieee80211_local *local = hw_to_local(hw);
696
697 if (!ieee80211_can_queue_work(local))
698 return;
699
700 queue_work(local->workqueue, work);
701 }
702 EXPORT_SYMBOL(ieee80211_queue_work);
703
704 void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
705 struct delayed_work *dwork,
706 unsigned long delay)
707 {
708 struct ieee80211_local *local = hw_to_local(hw);
709
710 if (!ieee80211_can_queue_work(local))
711 return;
712
713 queue_delayed_work(local->workqueue, dwork, delay);
714 }
715 EXPORT_SYMBOL(ieee80211_queue_delayed_work);
716
717 u32 ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
718 struct ieee802_11_elems *elems,
719 u64 filter, u32 crc)
720 {
721 size_t left = len;
722 const u8 *pos = start;
723 bool calc_crc = filter != 0;
724 DECLARE_BITMAP(seen_elems, 256);
725 const u8 *ie;
726
727 bitmap_zero(seen_elems, 256);
728 memset(elems, 0, sizeof(*elems));
729 elems->ie_start = start;
730 elems->total_len = len;
731
732 while (left >= 2) {
733 u8 id, elen;
734 bool elem_parse_failed;
735
736 id = *pos++;
737 elen = *pos++;
738 left -= 2;
739
740 if (elen > left) {
741 elems->parse_error = true;
742 break;
743 }
744
745 switch (id) {
746 case WLAN_EID_SSID:
747 case WLAN_EID_SUPP_RATES:
748 case WLAN_EID_FH_PARAMS:
749 case WLAN_EID_DS_PARAMS:
750 case WLAN_EID_CF_PARAMS:
751 case WLAN_EID_TIM:
752 case WLAN_EID_IBSS_PARAMS:
753 case WLAN_EID_CHALLENGE:
754 case WLAN_EID_RSN:
755 case WLAN_EID_ERP_INFO:
756 case WLAN_EID_EXT_SUPP_RATES:
757 case WLAN_EID_HT_CAPABILITY:
758 case WLAN_EID_HT_OPERATION:
759 case WLAN_EID_VHT_CAPABILITY:
760 case WLAN_EID_VHT_OPERATION:
761 case WLAN_EID_MESH_ID:
762 case WLAN_EID_MESH_CONFIG:
763 case WLAN_EID_PEER_MGMT:
764 case WLAN_EID_PREQ:
765 case WLAN_EID_PREP:
766 case WLAN_EID_PERR:
767 case WLAN_EID_RANN:
768 case WLAN_EID_CHANNEL_SWITCH:
769 case WLAN_EID_EXT_CHANSWITCH_ANN:
770 case WLAN_EID_COUNTRY:
771 case WLAN_EID_PWR_CONSTRAINT:
772 case WLAN_EID_TIMEOUT_INTERVAL:
773 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
774 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
775 case WLAN_EID_CHAN_SWITCH_PARAM:
776 /*
777 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
778 * that if the content gets bigger it might be needed more than once
779 */
780 if (test_bit(id, seen_elems)) {
781 elems->parse_error = true;
782 left -= elen;
783 pos += elen;
784 continue;
785 }
786 break;
787 }
788
789 if (calc_crc && id < 64 && (filter & (1ULL << id)))
790 crc = crc32_be(crc, pos - 2, elen + 2);
791
792 elem_parse_failed = false;
793
794 switch (id) {
795 case WLAN_EID_SSID:
796 elems->ssid = pos;
797 elems->ssid_len = elen;
798 break;
799 case WLAN_EID_SUPP_RATES:
800 elems->supp_rates = pos;
801 elems->supp_rates_len = elen;
802 break;
803 case WLAN_EID_DS_PARAMS:
804 if (elen >= 1)
805 elems->ds_params = pos;
806 else
807 elem_parse_failed = true;
808 break;
809 case WLAN_EID_TIM:
810 if (elen >= sizeof(struct ieee80211_tim_ie)) {
811 elems->tim = (void *)pos;
812 elems->tim_len = elen;
813 } else
814 elem_parse_failed = true;
815 break;
816 case WLAN_EID_CHALLENGE:
817 elems->challenge = pos;
818 elems->challenge_len = elen;
819 break;
820 case WLAN_EID_VENDOR_SPECIFIC:
821 if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
822 pos[2] == 0xf2) {
823 /* Microsoft OUI (00:50:F2) */
824
825 if (calc_crc)
826 crc = crc32_be(crc, pos - 2, elen + 2);
827
828 if (elen >= 5 && pos[3] == 2) {
829 /* OUI Type 2 - WMM IE */
830 if (pos[4] == 0) {
831 elems->wmm_info = pos;
832 elems->wmm_info_len = elen;
833 } else if (pos[4] == 1) {
834 elems->wmm_param = pos;
835 elems->wmm_param_len = elen;
836 }
837 }
838 }
839 break;
840 case WLAN_EID_RSN:
841 elems->rsn = pos;
842 elems->rsn_len = elen;
843 break;
844 case WLAN_EID_ERP_INFO:
845 if (elen >= 1)
846 elems->erp_info = pos;
847 else
848 elem_parse_failed = true;
849 break;
850 case WLAN_EID_EXT_SUPP_RATES:
851 elems->ext_supp_rates = pos;
852 elems->ext_supp_rates_len = elen;
853 break;
854 case WLAN_EID_HT_CAPABILITY:
855 if (elen >= sizeof(struct ieee80211_ht_cap))
856 elems->ht_cap_elem = (void *)pos;
857 else
858 elem_parse_failed = true;
859 break;
860 case WLAN_EID_HT_OPERATION:
861 if (elen >= sizeof(struct ieee80211_ht_operation))
862 elems->ht_operation = (void *)pos;
863 else
864 elem_parse_failed = true;
865 break;
866 case WLAN_EID_VHT_CAPABILITY:
867 if (elen >= sizeof(struct ieee80211_vht_cap))
868 elems->vht_cap_elem = (void *)pos;
869 else
870 elem_parse_failed = true;
871 break;
872 case WLAN_EID_VHT_OPERATION:
873 if (elen >= sizeof(struct ieee80211_vht_operation))
874 elems->vht_operation = (void *)pos;
875 else
876 elem_parse_failed = true;
877 break;
878 case WLAN_EID_OPMODE_NOTIF:
879 if (elen > 0)
880 elems->opmode_notif = pos;
881 else
882 elem_parse_failed = true;
883 break;
884 case WLAN_EID_MESH_ID:
885 elems->mesh_id = pos;
886 elems->mesh_id_len = elen;
887 break;
888 case WLAN_EID_MESH_CONFIG:
889 if (elen >= sizeof(struct ieee80211_meshconf_ie))
890 elems->mesh_config = (void *)pos;
891 else
892 elem_parse_failed = true;
893 break;
894 case WLAN_EID_PEER_MGMT:
895 elems->peering = pos;
896 elems->peering_len = elen;
897 break;
898 case WLAN_EID_MESH_AWAKE_WINDOW:
899 if (elen >= 2)
900 elems->awake_window = (void *)pos;
901 break;
902 case WLAN_EID_PREQ:
903 elems->preq = pos;
904 elems->preq_len = elen;
905 break;
906 case WLAN_EID_PREP:
907 elems->prep = pos;
908 elems->prep_len = elen;
909 break;
910 case WLAN_EID_PERR:
911 elems->perr = pos;
912 elems->perr_len = elen;
913 break;
914 case WLAN_EID_RANN:
915 if (elen >= sizeof(struct ieee80211_rann_ie))
916 elems->rann = (void *)pos;
917 else
918 elem_parse_failed = true;
919 break;
920 case WLAN_EID_CHANNEL_SWITCH:
921 if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
922 elem_parse_failed = true;
923 break;
924 }
925 elems->ch_switch_ie = (void *)pos;
926 break;
927 case WLAN_EID_EXT_CHANSWITCH_ANN:
928 if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
929 elem_parse_failed = true;
930 break;
931 }
932 elems->ext_chansw_ie = (void *)pos;
933 break;
934 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
935 if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
936 elem_parse_failed = true;
937 break;
938 }
939 elems->sec_chan_offs = (void *)pos;
940 break;
941 case WLAN_EID_CHAN_SWITCH_PARAM:
942 if (elen !=
943 sizeof(*elems->mesh_chansw_params_ie)) {
944 elem_parse_failed = true;
945 break;
946 }
947 elems->mesh_chansw_params_ie = (void *)pos;
948 break;
949 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
950 if (!action ||
951 elen != sizeof(*elems->wide_bw_chansw_ie)) {
952 elem_parse_failed = true;
953 break;
954 }
955 elems->wide_bw_chansw_ie = (void *)pos;
956 break;
957 case WLAN_EID_CHANNEL_SWITCH_WRAPPER:
958 if (action) {
959 elem_parse_failed = true;
960 break;
961 }
962 /*
963 * This is a bit tricky, but as we only care about
964 * the wide bandwidth channel switch element, so
965 * just parse it out manually.
966 */
967 ie = cfg80211_find_ie(WLAN_EID_WIDE_BW_CHANNEL_SWITCH,
968 pos, elen);
969 if (ie) {
970 if (ie[1] == sizeof(*elems->wide_bw_chansw_ie))
971 elems->wide_bw_chansw_ie =
972 (void *)(ie + 2);
973 else
974 elem_parse_failed = true;
975 }
976 break;
977 case WLAN_EID_COUNTRY:
978 elems->country_elem = pos;
979 elems->country_elem_len = elen;
980 break;
981 case WLAN_EID_PWR_CONSTRAINT:
982 if (elen != 1) {
983 elem_parse_failed = true;
984 break;
985 }
986 elems->pwr_constr_elem = pos;
987 break;
988 case WLAN_EID_TIMEOUT_INTERVAL:
989 if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
990 elems->timeout_int = (void *)pos;
991 else
992 elem_parse_failed = true;
993 break;
994 default:
995 break;
996 }
997
998 if (elem_parse_failed)
999 elems->parse_error = true;
1000 else
1001 __set_bit(id, seen_elems);
1002
1003 left -= elen;
1004 pos += elen;
1005 }
1006
1007 if (left != 0)
1008 elems->parse_error = true;
1009
1010 return crc;
1011 }
1012
1013 void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
1014 bool bss_notify)
1015 {
1016 struct ieee80211_local *local = sdata->local;
1017 struct ieee80211_tx_queue_params qparam;
1018 struct ieee80211_chanctx_conf *chanctx_conf;
1019 int ac;
1020 bool use_11b, enable_qos;
1021 int aCWmin, aCWmax;
1022
1023 if (!local->ops->conf_tx)
1024 return;
1025
1026 if (local->hw.queues < IEEE80211_NUM_ACS)
1027 return;
1028
1029 memset(&qparam, 0, sizeof(qparam));
1030
1031 rcu_read_lock();
1032 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1033 use_11b = (chanctx_conf &&
1034 chanctx_conf->def.chan->band == IEEE80211_BAND_2GHZ) &&
1035 !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
1036 rcu_read_unlock();
1037
1038 /*
1039 * By default disable QoS in STA mode for old access points, which do
1040 * not support 802.11e. New APs will provide proper queue parameters,
1041 * that we will configure later.
1042 */
1043 enable_qos = (sdata->vif.type != NL80211_IFTYPE_STATION);
1044
1045 /* Set defaults according to 802.11-2007 Table 7-37 */
1046 aCWmax = 1023;
1047 if (use_11b)
1048 aCWmin = 31;
1049 else
1050 aCWmin = 15;
1051
1052 /* Confiure old 802.11b/g medium access rules. */
1053 qparam.cw_max = aCWmax;
1054 qparam.cw_min = aCWmin;
1055 qparam.txop = 0;
1056 qparam.aifs = 2;
1057
1058 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1059 /* Update if QoS is enabled. */
1060 if (enable_qos) {
1061 switch (ac) {
1062 case IEEE80211_AC_BK:
1063 qparam.cw_max = aCWmax;
1064 qparam.cw_min = aCWmin;
1065 qparam.txop = 0;
1066 qparam.aifs = 7;
1067 break;
1068 /* never happens but let's not leave undefined */
1069 default:
1070 case IEEE80211_AC_BE:
1071 qparam.cw_max = aCWmax;
1072 qparam.cw_min = aCWmin;
1073 qparam.txop = 0;
1074 qparam.aifs = 3;
1075 break;
1076 case IEEE80211_AC_VI:
1077 qparam.cw_max = aCWmin;
1078 qparam.cw_min = (aCWmin + 1) / 2 - 1;
1079 if (use_11b)
1080 qparam.txop = 6016/32;
1081 else
1082 qparam.txop = 3008/32;
1083 qparam.aifs = 2;
1084 break;
1085 case IEEE80211_AC_VO:
1086 qparam.cw_max = (aCWmin + 1) / 2 - 1;
1087 qparam.cw_min = (aCWmin + 1) / 4 - 1;
1088 if (use_11b)
1089 qparam.txop = 3264/32;
1090 else
1091 qparam.txop = 1504/32;
1092 qparam.aifs = 2;
1093 break;
1094 }
1095 }
1096
1097 qparam.uapsd = false;
1098
1099 sdata->tx_conf[ac] = qparam;
1100 drv_conf_tx(local, sdata, ac, &qparam);
1101 }
1102
1103 if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1104 sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE) {
1105 sdata->vif.bss_conf.qos = enable_qos;
1106 if (bss_notify)
1107 ieee80211_bss_info_change_notify(sdata,
1108 BSS_CHANGED_QOS);
1109 }
1110 }
1111
1112 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
1113 u16 transaction, u16 auth_alg, u16 status,
1114 const u8 *extra, size_t extra_len, const u8 *da,
1115 const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
1116 u32 tx_flags)
1117 {
1118 struct ieee80211_local *local = sdata->local;
1119 struct sk_buff *skb;
1120 struct ieee80211_mgmt *mgmt;
1121 int err;
1122
1123 /* 24 + 6 = header + auth_algo + auth_transaction + status_code */
1124 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24 + 6 + extra_len);
1125 if (!skb)
1126 return;
1127
1128 skb_reserve(skb, local->hw.extra_tx_headroom);
1129
1130 mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
1131 memset(mgmt, 0, 24 + 6);
1132 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1133 IEEE80211_STYPE_AUTH);
1134 memcpy(mgmt->da, da, ETH_ALEN);
1135 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1136 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1137 mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
1138 mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
1139 mgmt->u.auth.status_code = cpu_to_le16(status);
1140 if (extra)
1141 memcpy(skb_put(skb, extra_len), extra, extra_len);
1142
1143 if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
1144 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1145 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
1146 WARN_ON(err);
1147 }
1148
1149 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1150 tx_flags;
1151 ieee80211_tx_skb(sdata, skb);
1152 }
1153
1154 void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
1155 const u8 *bssid, u16 stype, u16 reason,
1156 bool send_frame, u8 *frame_buf)
1157 {
1158 struct ieee80211_local *local = sdata->local;
1159 struct sk_buff *skb;
1160 struct ieee80211_mgmt *mgmt = (void *)frame_buf;
1161
1162 /* build frame */
1163 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1164 mgmt->duration = 0; /* initialize only */
1165 mgmt->seq_ctrl = 0; /* initialize only */
1166 memcpy(mgmt->da, bssid, ETH_ALEN);
1167 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1168 memcpy(mgmt->bssid, bssid, ETH_ALEN);
1169 /* u.deauth.reason_code == u.disassoc.reason_code */
1170 mgmt->u.deauth.reason_code = cpu_to_le16(reason);
1171
1172 if (send_frame) {
1173 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
1174 IEEE80211_DEAUTH_FRAME_LEN);
1175 if (!skb)
1176 return;
1177
1178 skb_reserve(skb, local->hw.extra_tx_headroom);
1179
1180 /* copy in frame */
1181 memcpy(skb_put(skb, IEEE80211_DEAUTH_FRAME_LEN),
1182 mgmt, IEEE80211_DEAUTH_FRAME_LEN);
1183
1184 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1185 !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
1186 IEEE80211_SKB_CB(skb)->flags |=
1187 IEEE80211_TX_INTFL_DONT_ENCRYPT;
1188
1189 ieee80211_tx_skb(sdata, skb);
1190 }
1191 }
1192
1193 int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
1194 size_t buffer_len, const u8 *ie, size_t ie_len,
1195 enum ieee80211_band band, u32 rate_mask,
1196 struct cfg80211_chan_def *chandef)
1197 {
1198 struct ieee80211_supported_band *sband;
1199 u8 *pos = buffer, *end = buffer + buffer_len;
1200 size_t offset = 0, noffset;
1201 int supp_rates_len, i;
1202 u8 rates[32];
1203 int num_rates;
1204 int ext_rates_len;
1205 int shift;
1206 u32 rate_flags;
1207
1208 sband = local->hw.wiphy->bands[band];
1209 if (WARN_ON_ONCE(!sband))
1210 return 0;
1211
1212 rate_flags = ieee80211_chandef_rate_flags(chandef);
1213 shift = ieee80211_chandef_get_shift(chandef);
1214
1215 num_rates = 0;
1216 for (i = 0; i < sband->n_bitrates; i++) {
1217 if ((BIT(i) & rate_mask) == 0)
1218 continue; /* skip rate */
1219 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
1220 continue;
1221
1222 rates[num_rates++] =
1223 (u8) DIV_ROUND_UP(sband->bitrates[i].bitrate,
1224 (1 << shift) * 5);
1225 }
1226
1227 supp_rates_len = min_t(int, num_rates, 8);
1228
1229 if (end - pos < 2 + supp_rates_len)
1230 goto out_err;
1231 *pos++ = WLAN_EID_SUPP_RATES;
1232 *pos++ = supp_rates_len;
1233 memcpy(pos, rates, supp_rates_len);
1234 pos += supp_rates_len;
1235
1236 /* insert "request information" if in custom IEs */
1237 if (ie && ie_len) {
1238 static const u8 before_extrates[] = {
1239 WLAN_EID_SSID,
1240 WLAN_EID_SUPP_RATES,
1241 WLAN_EID_REQUEST,
1242 };
1243 noffset = ieee80211_ie_split(ie, ie_len,
1244 before_extrates,
1245 ARRAY_SIZE(before_extrates),
1246 offset);
1247 if (end - pos < noffset - offset)
1248 goto out_err;
1249 memcpy(pos, ie + offset, noffset - offset);
1250 pos += noffset - offset;
1251 offset = noffset;
1252 }
1253
1254 ext_rates_len = num_rates - supp_rates_len;
1255 if (ext_rates_len > 0) {
1256 if (end - pos < 2 + ext_rates_len)
1257 goto out_err;
1258 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1259 *pos++ = ext_rates_len;
1260 memcpy(pos, rates + supp_rates_len, ext_rates_len);
1261 pos += ext_rates_len;
1262 }
1263
1264 if (chandef->chan && sband->band == IEEE80211_BAND_2GHZ) {
1265 if (end - pos < 3)
1266 goto out_err;
1267 *pos++ = WLAN_EID_DS_PARAMS;
1268 *pos++ = 1;
1269 *pos++ = ieee80211_frequency_to_channel(
1270 chandef->chan->center_freq);
1271 }
1272
1273 /* insert custom IEs that go before HT */
1274 if (ie && ie_len) {
1275 static const u8 before_ht[] = {
1276 WLAN_EID_SSID,
1277 WLAN_EID_SUPP_RATES,
1278 WLAN_EID_REQUEST,
1279 WLAN_EID_EXT_SUPP_RATES,
1280 WLAN_EID_DS_PARAMS,
1281 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
1282 };
1283 noffset = ieee80211_ie_split(ie, ie_len,
1284 before_ht, ARRAY_SIZE(before_ht),
1285 offset);
1286 if (end - pos < noffset - offset)
1287 goto out_err;
1288 memcpy(pos, ie + offset, noffset - offset);
1289 pos += noffset - offset;
1290 offset = noffset;
1291 }
1292
1293 if (sband->ht_cap.ht_supported) {
1294 if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
1295 goto out_err;
1296 pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
1297 sband->ht_cap.cap);
1298 }
1299
1300 /*
1301 * If adding more here, adjust code in main.c
1302 * that calculates local->scan_ies_len.
1303 */
1304
1305 /* insert custom IEs that go before VHT */
1306 if (ie && ie_len) {
1307 static const u8 before_vht[] = {
1308 WLAN_EID_SSID,
1309 WLAN_EID_SUPP_RATES,
1310 WLAN_EID_REQUEST,
1311 WLAN_EID_EXT_SUPP_RATES,
1312 WLAN_EID_DS_PARAMS,
1313 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
1314 WLAN_EID_HT_CAPABILITY,
1315 WLAN_EID_BSS_COEX_2040,
1316 WLAN_EID_EXT_CAPABILITY,
1317 WLAN_EID_SSID_LIST,
1318 WLAN_EID_CHANNEL_USAGE,
1319 WLAN_EID_INTERWORKING,
1320 /* mesh ID can't happen here */
1321 /* 60 GHz can't happen here right now */
1322 };
1323 noffset = ieee80211_ie_split(ie, ie_len,
1324 before_vht, ARRAY_SIZE(before_vht),
1325 offset);
1326 if (end - pos < noffset - offset)
1327 goto out_err;
1328 memcpy(pos, ie + offset, noffset - offset);
1329 pos += noffset - offset;
1330 offset = noffset;
1331 }
1332
1333 if (sband->vht_cap.vht_supported) {
1334 if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
1335 goto out_err;
1336 pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
1337 sband->vht_cap.cap);
1338 }
1339
1340 /* add any remaining custom IEs */
1341 if (ie && ie_len) {
1342 noffset = ie_len;
1343 if (end - pos < noffset - offset)
1344 goto out_err;
1345 memcpy(pos, ie + offset, noffset - offset);
1346 pos += noffset - offset;
1347 }
1348
1349 return pos - buffer;
1350 out_err:
1351 WARN_ONCE(1, "not enough space for preq IEs\n");
1352 return pos - buffer;
1353 }
1354
1355 struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
1356 u8 *dst, u32 ratemask,
1357 struct ieee80211_channel *chan,
1358 const u8 *ssid, size_t ssid_len,
1359 const u8 *ie, size_t ie_len,
1360 bool directed)
1361 {
1362 struct ieee80211_local *local = sdata->local;
1363 struct cfg80211_chan_def chandef;
1364 struct sk_buff *skb;
1365 struct ieee80211_mgmt *mgmt;
1366 int ies_len;
1367
1368 /*
1369 * Do not send DS Channel parameter for directed probe requests
1370 * in order to maximize the chance that we get a response. Some
1371 * badly-behaved APs don't respond when this parameter is included.
1372 */
1373 chandef.width = sdata->vif.bss_conf.chandef.width;
1374 if (directed)
1375 chandef.chan = NULL;
1376 else
1377 chandef.chan = chan;
1378
1379 skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
1380 ssid, ssid_len, 100 + ie_len);
1381 if (!skb)
1382 return NULL;
1383
1384 ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
1385 skb_tailroom(skb),
1386 ie, ie_len, chan->band,
1387 ratemask, &chandef);
1388 skb_put(skb, ies_len);
1389
1390 if (dst) {
1391 mgmt = (struct ieee80211_mgmt *) skb->data;
1392 memcpy(mgmt->da, dst, ETH_ALEN);
1393 memcpy(mgmt->bssid, dst, ETH_ALEN);
1394 }
1395
1396 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1397
1398 return skb;
1399 }
1400
1401 void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
1402 const u8 *ssid, size_t ssid_len,
1403 const u8 *ie, size_t ie_len,
1404 u32 ratemask, bool directed, u32 tx_flags,
1405 struct ieee80211_channel *channel, bool scan)
1406 {
1407 struct sk_buff *skb;
1408
1409 skb = ieee80211_build_probe_req(sdata, dst, ratemask, channel,
1410 ssid, ssid_len,
1411 ie, ie_len, directed);
1412 if (skb) {
1413 IEEE80211_SKB_CB(skb)->flags |= tx_flags;
1414 if (scan)
1415 ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
1416 else
1417 ieee80211_tx_skb(sdata, skb);
1418 }
1419 }
1420
1421 u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
1422 struct ieee802_11_elems *elems,
1423 enum ieee80211_band band, u32 *basic_rates)
1424 {
1425 struct ieee80211_supported_band *sband;
1426 size_t num_rates;
1427 u32 supp_rates, rate_flags;
1428 int i, j, shift;
1429 sband = sdata->local->hw.wiphy->bands[band];
1430
1431 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
1432 shift = ieee80211_vif_get_shift(&sdata->vif);
1433
1434 if (WARN_ON(!sband))
1435 return 1;
1436
1437 num_rates = sband->n_bitrates;
1438 supp_rates = 0;
1439 for (i = 0; i < elems->supp_rates_len +
1440 elems->ext_supp_rates_len; i++) {
1441 u8 rate = 0;
1442 int own_rate;
1443 bool is_basic;
1444 if (i < elems->supp_rates_len)
1445 rate = elems->supp_rates[i];
1446 else if (elems->ext_supp_rates)
1447 rate = elems->ext_supp_rates
1448 [i - elems->supp_rates_len];
1449 own_rate = 5 * (rate & 0x7f);
1450 is_basic = !!(rate & 0x80);
1451
1452 if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
1453 continue;
1454
1455 for (j = 0; j < num_rates; j++) {
1456 int brate;
1457 if ((rate_flags & sband->bitrates[j].flags)
1458 != rate_flags)
1459 continue;
1460
1461 brate = DIV_ROUND_UP(sband->bitrates[j].bitrate,
1462 1 << shift);
1463
1464 if (brate == own_rate) {
1465 supp_rates |= BIT(j);
1466 if (basic_rates && is_basic)
1467 *basic_rates |= BIT(j);
1468 }
1469 }
1470 }
1471 return supp_rates;
1472 }
1473
1474 void ieee80211_stop_device(struct ieee80211_local *local)
1475 {
1476 ieee80211_led_radio(local, false);
1477 ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
1478
1479 cancel_work_sync(&local->reconfig_filter);
1480
1481 flush_workqueue(local->workqueue);
1482 drv_stop(local);
1483 }
1484
1485 static void ieee80211_handle_reconfig_failure(struct ieee80211_local *local)
1486 {
1487 struct ieee80211_sub_if_data *sdata;
1488 struct ieee80211_chanctx *ctx;
1489
1490 /*
1491 * We get here if during resume the device can't be restarted properly.
1492 * We might also get here if this happens during HW reset, which is a
1493 * slightly different situation and we need to drop all connections in
1494 * the latter case.
1495 *
1496 * Ask cfg80211 to turn off all interfaces, this will result in more
1497 * warnings but at least we'll then get into a clean stopped state.
1498 */
1499
1500 local->resuming = false;
1501 local->suspended = false;
1502 local->started = false;
1503
1504 /* scheduled scan clearly can't be running any more, but tell
1505 * cfg80211 and clear local state
1506 */
1507 ieee80211_sched_scan_end(local);
1508
1509 list_for_each_entry(sdata, &local->interfaces, list)
1510 sdata->flags &= ~IEEE80211_SDATA_IN_DRIVER;
1511
1512 /* Mark channel contexts as not being in the driver any more to avoid
1513 * removing them from the driver during the shutdown process...
1514 */
1515 mutex_lock(&local->chanctx_mtx);
1516 list_for_each_entry(ctx, &local->chanctx_list, list)
1517 ctx->driver_present = false;
1518 mutex_unlock(&local->chanctx_mtx);
1519
1520 cfg80211_shutdown_all_interfaces(local->hw.wiphy);
1521 }
1522
1523 static void ieee80211_assign_chanctx(struct ieee80211_local *local,
1524 struct ieee80211_sub_if_data *sdata)
1525 {
1526 struct ieee80211_chanctx_conf *conf;
1527 struct ieee80211_chanctx *ctx;
1528
1529 if (!local->use_chanctx)
1530 return;
1531
1532 mutex_lock(&local->chanctx_mtx);
1533 conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1534 lockdep_is_held(&local->chanctx_mtx));
1535 if (conf) {
1536 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1537 drv_assign_vif_chanctx(local, sdata, ctx);
1538 }
1539 mutex_unlock(&local->chanctx_mtx);
1540 }
1541
1542 int ieee80211_reconfig(struct ieee80211_local *local)
1543 {
1544 struct ieee80211_hw *hw = &local->hw;
1545 struct ieee80211_sub_if_data *sdata;
1546 struct ieee80211_chanctx *ctx;
1547 struct sta_info *sta;
1548 int res, i;
1549 bool reconfig_due_to_wowlan = false;
1550 struct ieee80211_sub_if_data *sched_scan_sdata;
1551 bool sched_scan_stopped = false;
1552
1553 #ifdef CONFIG_PM
1554 if (local->suspended)
1555 local->resuming = true;
1556
1557 if (local->wowlan) {
1558 res = drv_resume(local);
1559 local->wowlan = false;
1560 if (res < 0) {
1561 local->resuming = false;
1562 return res;
1563 }
1564 if (res == 0)
1565 goto wake_up;
1566 WARN_ON(res > 1);
1567 /*
1568 * res is 1, which means the driver requested
1569 * to go through a regular reset on wakeup.
1570 */
1571 reconfig_due_to_wowlan = true;
1572 }
1573 #endif
1574 /* everything else happens only if HW was up & running */
1575 if (!local->open_count)
1576 goto wake_up;
1577
1578 /*
1579 * Upon resume hardware can sometimes be goofy due to
1580 * various platform / driver / bus issues, so restarting
1581 * the device may at times not work immediately. Propagate
1582 * the error.
1583 */
1584 res = drv_start(local);
1585 if (res) {
1586 if (local->suspended)
1587 WARN(1, "Hardware became unavailable upon resume. This could be a software issue prior to suspend or a hardware issue.\n");
1588 else
1589 WARN(1, "Hardware became unavailable during restart.\n");
1590 ieee80211_handle_reconfig_failure(local);
1591 return res;
1592 }
1593
1594 /* setup fragmentation threshold */
1595 drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
1596
1597 /* setup RTS threshold */
1598 drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
1599
1600 /* reset coverage class */
1601 drv_set_coverage_class(local, hw->wiphy->coverage_class);
1602
1603 ieee80211_led_radio(local, true);
1604 ieee80211_mod_tpt_led_trig(local,
1605 IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1606
1607 /* add interfaces */
1608 sdata = rtnl_dereference(local->monitor_sdata);
1609 if (sdata) {
1610 /* in HW restart it exists already */
1611 WARN_ON(local->resuming);
1612 res = drv_add_interface(local, sdata);
1613 if (WARN_ON(res)) {
1614 RCU_INIT_POINTER(local->monitor_sdata, NULL);
1615 synchronize_net();
1616 kfree(sdata);
1617 }
1618 }
1619
1620 list_for_each_entry(sdata, &local->interfaces, list) {
1621 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1622 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1623 ieee80211_sdata_running(sdata))
1624 res = drv_add_interface(local, sdata);
1625 }
1626
1627 /* add channel contexts */
1628 if (local->use_chanctx) {
1629 mutex_lock(&local->chanctx_mtx);
1630 list_for_each_entry(ctx, &local->chanctx_list, list)
1631 WARN_ON(drv_add_chanctx(local, ctx));
1632 mutex_unlock(&local->chanctx_mtx);
1633
1634 list_for_each_entry(sdata, &local->interfaces, list) {
1635 if (!ieee80211_sdata_running(sdata))
1636 continue;
1637 ieee80211_assign_chanctx(local, sdata);
1638 }
1639
1640 sdata = rtnl_dereference(local->monitor_sdata);
1641 if (sdata && ieee80211_sdata_running(sdata))
1642 ieee80211_assign_chanctx(local, sdata);
1643 }
1644
1645 /* add STAs back */
1646 mutex_lock(&local->sta_mtx);
1647 list_for_each_entry(sta, &local->sta_list, list) {
1648 enum ieee80211_sta_state state;
1649
1650 if (!sta->uploaded)
1651 continue;
1652
1653 /* AP-mode stations will be added later */
1654 if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
1655 continue;
1656
1657 for (state = IEEE80211_STA_NOTEXIST;
1658 state < sta->sta_state; state++)
1659 WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
1660 state + 1));
1661 }
1662 mutex_unlock(&local->sta_mtx);
1663
1664 /* reconfigure tx conf */
1665 if (hw->queues >= IEEE80211_NUM_ACS) {
1666 list_for_each_entry(sdata, &local->interfaces, list) {
1667 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1668 sdata->vif.type == NL80211_IFTYPE_MONITOR ||
1669 !ieee80211_sdata_running(sdata))
1670 continue;
1671
1672 for (i = 0; i < IEEE80211_NUM_ACS; i++)
1673 drv_conf_tx(local, sdata, i,
1674 &sdata->tx_conf[i]);
1675 }
1676 }
1677
1678 /* reconfigure hardware */
1679 ieee80211_hw_config(local, ~0);
1680
1681 ieee80211_configure_filter(local);
1682
1683 /* Finally also reconfigure all the BSS information */
1684 list_for_each_entry(sdata, &local->interfaces, list) {
1685 u32 changed;
1686
1687 if (!ieee80211_sdata_running(sdata))
1688 continue;
1689
1690 /* common change flags for all interface types */
1691 changed = BSS_CHANGED_ERP_CTS_PROT |
1692 BSS_CHANGED_ERP_PREAMBLE |
1693 BSS_CHANGED_ERP_SLOT |
1694 BSS_CHANGED_HT |
1695 BSS_CHANGED_BASIC_RATES |
1696 BSS_CHANGED_BEACON_INT |
1697 BSS_CHANGED_BSSID |
1698 BSS_CHANGED_CQM |
1699 BSS_CHANGED_QOS |
1700 BSS_CHANGED_IDLE |
1701 BSS_CHANGED_TXPOWER;
1702
1703 switch (sdata->vif.type) {
1704 case NL80211_IFTYPE_STATION:
1705 changed |= BSS_CHANGED_ASSOC |
1706 BSS_CHANGED_ARP_FILTER |
1707 BSS_CHANGED_PS;
1708
1709 /* Re-send beacon info report to the driver */
1710 if (sdata->u.mgd.have_beacon)
1711 changed |= BSS_CHANGED_BEACON_INFO;
1712
1713 sdata_lock(sdata);
1714 ieee80211_bss_info_change_notify(sdata, changed);
1715 sdata_unlock(sdata);
1716 break;
1717 case NL80211_IFTYPE_ADHOC:
1718 changed |= BSS_CHANGED_IBSS;
1719 /* fall through */
1720 case NL80211_IFTYPE_AP:
1721 changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
1722
1723 if (sdata->vif.type == NL80211_IFTYPE_AP) {
1724 changed |= BSS_CHANGED_AP_PROBE_RESP;
1725
1726 if (rcu_access_pointer(sdata->u.ap.beacon))
1727 drv_start_ap(local, sdata);
1728 }
1729
1730 /* fall through */
1731 case NL80211_IFTYPE_MESH_POINT:
1732 if (sdata->vif.bss_conf.enable_beacon) {
1733 changed |= BSS_CHANGED_BEACON |
1734 BSS_CHANGED_BEACON_ENABLED;
1735 ieee80211_bss_info_change_notify(sdata, changed);
1736 }
1737 break;
1738 case NL80211_IFTYPE_WDS:
1739 case NL80211_IFTYPE_AP_VLAN:
1740 case NL80211_IFTYPE_MONITOR:
1741 case NL80211_IFTYPE_P2P_DEVICE:
1742 /* nothing to do */
1743 break;
1744 case NL80211_IFTYPE_UNSPECIFIED:
1745 case NUM_NL80211_IFTYPES:
1746 case NL80211_IFTYPE_P2P_CLIENT:
1747 case NL80211_IFTYPE_P2P_GO:
1748 WARN_ON(1);
1749 break;
1750 }
1751 }
1752
1753 ieee80211_recalc_ps(local, -1);
1754
1755 /*
1756 * The sta might be in psm against the ap (e.g. because
1757 * this was the state before a hw restart), so we
1758 * explicitly send a null packet in order to make sure
1759 * it'll sync against the ap (and get out of psm).
1760 */
1761 if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
1762 list_for_each_entry(sdata, &local->interfaces, list) {
1763 if (sdata->vif.type != NL80211_IFTYPE_STATION)
1764 continue;
1765 if (!sdata->u.mgd.associated)
1766 continue;
1767
1768 ieee80211_send_nullfunc(local, sdata, 0);
1769 }
1770 }
1771
1772 /* APs are now beaconing, add back stations */
1773 mutex_lock(&local->sta_mtx);
1774 list_for_each_entry(sta, &local->sta_list, list) {
1775 enum ieee80211_sta_state state;
1776
1777 if (!sta->uploaded)
1778 continue;
1779
1780 if (sta->sdata->vif.type != NL80211_IFTYPE_AP)
1781 continue;
1782
1783 for (state = IEEE80211_STA_NOTEXIST;
1784 state < sta->sta_state; state++)
1785 WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
1786 state + 1));
1787 }
1788 mutex_unlock(&local->sta_mtx);
1789
1790 /* add back keys */
1791 list_for_each_entry(sdata, &local->interfaces, list)
1792 if (ieee80211_sdata_running(sdata))
1793 ieee80211_enable_keys(sdata);
1794
1795 wake_up:
1796 local->in_reconfig = false;
1797 barrier();
1798
1799 if (local->monitors == local->open_count && local->monitors > 0)
1800 ieee80211_add_virtual_monitor(local);
1801
1802 /*
1803 * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
1804 * sessions can be established after a resume.
1805 *
1806 * Also tear down aggregation sessions since reconfiguring
1807 * them in a hardware restart scenario is not easily done
1808 * right now, and the hardware will have lost information
1809 * about the sessions, but we and the AP still think they
1810 * are active. This is really a workaround though.
1811 */
1812 if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1813 mutex_lock(&local->sta_mtx);
1814
1815 list_for_each_entry(sta, &local->sta_list, list) {
1816 ieee80211_sta_tear_down_BA_sessions(
1817 sta, AGG_STOP_LOCAL_REQUEST);
1818 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
1819 }
1820
1821 mutex_unlock(&local->sta_mtx);
1822 }
1823
1824 ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
1825 IEEE80211_QUEUE_STOP_REASON_SUSPEND,
1826 false);
1827
1828 /*
1829 * Reconfigure sched scan if it was interrupted by FW restart or
1830 * suspend.
1831 */
1832 mutex_lock(&local->mtx);
1833 sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
1834 lockdep_is_held(&local->mtx));
1835 if (sched_scan_sdata && local->sched_scan_req)
1836 /*
1837 * Sched scan stopped, but we don't want to report it. Instead,
1838 * we're trying to reschedule.
1839 */
1840 if (__ieee80211_request_sched_scan_start(sched_scan_sdata,
1841 local->sched_scan_req))
1842 sched_scan_stopped = true;
1843 mutex_unlock(&local->mtx);
1844
1845 if (sched_scan_stopped)
1846 cfg80211_sched_scan_stopped_rtnl(local->hw.wiphy);
1847
1848 /*
1849 * If this is for hw restart things are still running.
1850 * We may want to change that later, however.
1851 */
1852 if (!local->suspended || reconfig_due_to_wowlan)
1853 drv_restart_complete(local);
1854
1855 if (!local->suspended)
1856 return 0;
1857
1858 #ifdef CONFIG_PM
1859 /* first set suspended false, then resuming */
1860 local->suspended = false;
1861 mb();
1862 local->resuming = false;
1863
1864 list_for_each_entry(sdata, &local->interfaces, list) {
1865 if (!ieee80211_sdata_running(sdata))
1866 continue;
1867 if (sdata->vif.type == NL80211_IFTYPE_STATION)
1868 ieee80211_sta_restart(sdata);
1869 }
1870
1871 mod_timer(&local->sta_cleanup, jiffies + 1);
1872 #else
1873 WARN_ON(1);
1874 #endif
1875
1876 return 0;
1877 }
1878
1879 void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
1880 {
1881 struct ieee80211_sub_if_data *sdata;
1882 struct ieee80211_local *local;
1883 struct ieee80211_key *key;
1884
1885 if (WARN_ON(!vif))
1886 return;
1887
1888 sdata = vif_to_sdata(vif);
1889 local = sdata->local;
1890
1891 if (WARN_ON(!local->resuming))
1892 return;
1893
1894 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1895 return;
1896
1897 sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
1898
1899 mutex_lock(&local->key_mtx);
1900 list_for_each_entry(key, &sdata->key_list, list)
1901 key->flags |= KEY_FLAG_TAINTED;
1902 mutex_unlock(&local->key_mtx);
1903 }
1904 EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
1905
1906 void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
1907 {
1908 struct ieee80211_local *local = sdata->local;
1909 struct ieee80211_chanctx_conf *chanctx_conf;
1910 struct ieee80211_chanctx *chanctx;
1911
1912 mutex_lock(&local->chanctx_mtx);
1913
1914 chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1915 lockdep_is_held(&local->chanctx_mtx));
1916
1917 if (WARN_ON_ONCE(!chanctx_conf))
1918 goto unlock;
1919
1920 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
1921 ieee80211_recalc_smps_chanctx(local, chanctx);
1922 unlock:
1923 mutex_unlock(&local->chanctx_mtx);
1924 }
1925
1926 void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata)
1927 {
1928 struct ieee80211_local *local = sdata->local;
1929 struct ieee80211_chanctx_conf *chanctx_conf;
1930 struct ieee80211_chanctx *chanctx;
1931
1932 mutex_lock(&local->chanctx_mtx);
1933
1934 chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1935 lockdep_is_held(&local->chanctx_mtx));
1936
1937 if (WARN_ON_ONCE(!chanctx_conf))
1938 goto unlock;
1939
1940 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
1941 ieee80211_recalc_chanctx_min_def(local, chanctx);
1942 unlock:
1943 mutex_unlock(&local->chanctx_mtx);
1944 }
1945
1946 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1947 {
1948 int i;
1949
1950 for (i = 0; i < n_ids; i++)
1951 if (ids[i] == id)
1952 return true;
1953 return false;
1954 }
1955
1956 /**
1957 * ieee80211_ie_split - split an IE buffer according to ordering
1958 *
1959 * @ies: the IE buffer
1960 * @ielen: the length of the IE buffer
1961 * @ids: an array with element IDs that are allowed before
1962 * the split
1963 * @n_ids: the size of the element ID array
1964 * @offset: offset where to start splitting in the buffer
1965 *
1966 * This function splits an IE buffer by updating the @offset
1967 * variable to point to the location where the buffer should be
1968 * split.
1969 *
1970 * It assumes that the given IE buffer is well-formed, this
1971 * has to be guaranteed by the caller!
1972 *
1973 * It also assumes that the IEs in the buffer are ordered
1974 * correctly, if not the result of using this function will not
1975 * be ordered correctly either, i.e. it does no reordering.
1976 *
1977 * The function returns the offset where the next part of the
1978 * buffer starts, which may be @ielen if the entire (remainder)
1979 * of the buffer should be used.
1980 */
1981 size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
1982 const u8 *ids, int n_ids, size_t offset)
1983 {
1984 size_t pos = offset;
1985
1986 while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
1987 pos += 2 + ies[pos + 1];
1988
1989 return pos;
1990 }
1991
1992 size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
1993 {
1994 size_t pos = offset;
1995
1996 while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
1997 pos += 2 + ies[pos + 1];
1998
1999 return pos;
2000 }
2001
2002 static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
2003 int rssi_min_thold,
2004 int rssi_max_thold)
2005 {
2006 trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
2007
2008 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
2009 return;
2010
2011 /*
2012 * Scale up threshold values before storing it, as the RSSI averaging
2013 * algorithm uses a scaled up value as well. Change this scaling
2014 * factor if the RSSI averaging algorithm changes.
2015 */
2016 sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
2017 sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
2018 }
2019
2020 void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
2021 int rssi_min_thold,
2022 int rssi_max_thold)
2023 {
2024 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2025
2026 WARN_ON(rssi_min_thold == rssi_max_thold ||
2027 rssi_min_thold > rssi_max_thold);
2028
2029 _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
2030 rssi_max_thold);
2031 }
2032 EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
2033
2034 void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
2035 {
2036 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2037
2038 _ieee80211_enable_rssi_reports(sdata, 0, 0);
2039 }
2040 EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
2041
2042 u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
2043 u16 cap)
2044 {
2045 __le16 tmp;
2046
2047 *pos++ = WLAN_EID_HT_CAPABILITY;
2048 *pos++ = sizeof(struct ieee80211_ht_cap);
2049 memset(pos, 0, sizeof(struct ieee80211_ht_cap));
2050
2051 /* capability flags */
2052 tmp = cpu_to_le16(cap);
2053 memcpy(pos, &tmp, sizeof(u16));
2054 pos += sizeof(u16);
2055
2056 /* AMPDU parameters */
2057 *pos++ = ht_cap->ampdu_factor |
2058 (ht_cap->ampdu_density <<
2059 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
2060
2061 /* MCS set */
2062 memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
2063 pos += sizeof(ht_cap->mcs);
2064
2065 /* extended capabilities */
2066 pos += sizeof(__le16);
2067
2068 /* BF capabilities */
2069 pos += sizeof(__le32);
2070
2071 /* antenna selection */
2072 pos += sizeof(u8);
2073
2074 return pos;
2075 }
2076
2077 u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
2078 u32 cap)
2079 {
2080 __le32 tmp;
2081
2082 *pos++ = WLAN_EID_VHT_CAPABILITY;
2083 *pos++ = sizeof(struct ieee80211_vht_cap);
2084 memset(pos, 0, sizeof(struct ieee80211_vht_cap));
2085
2086 /* capability flags */
2087 tmp = cpu_to_le32(cap);
2088 memcpy(pos, &tmp, sizeof(u32));
2089 pos += sizeof(u32);
2090
2091 /* VHT MCS set */
2092 memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
2093 pos += sizeof(vht_cap->vht_mcs);
2094
2095 return pos;
2096 }
2097
2098 u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
2099 const struct cfg80211_chan_def *chandef,
2100 u16 prot_mode)
2101 {
2102 struct ieee80211_ht_operation *ht_oper;
2103 /* Build HT Information */
2104 *pos++ = WLAN_EID_HT_OPERATION;
2105 *pos++ = sizeof(struct ieee80211_ht_operation);
2106 ht_oper = (struct ieee80211_ht_operation *)pos;
2107 ht_oper->primary_chan = ieee80211_frequency_to_channel(
2108 chandef->chan->center_freq);
2109 switch (chandef->width) {
2110 case NL80211_CHAN_WIDTH_160:
2111 case NL80211_CHAN_WIDTH_80P80:
2112 case NL80211_CHAN_WIDTH_80:
2113 case NL80211_CHAN_WIDTH_40:
2114 if (chandef->center_freq1 > chandef->chan->center_freq)
2115 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
2116 else
2117 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
2118 break;
2119 default:
2120 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
2121 break;
2122 }
2123 if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
2124 chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
2125 chandef->width != NL80211_CHAN_WIDTH_20)
2126 ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
2127
2128 ht_oper->operation_mode = cpu_to_le16(prot_mode);
2129 ht_oper->stbc_param = 0x0000;
2130
2131 /* It seems that Basic MCS set and Supported MCS set
2132 are identical for the first 10 bytes */
2133 memset(&ht_oper->basic_set, 0, 16);
2134 memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
2135
2136 return pos + sizeof(struct ieee80211_ht_operation);
2137 }
2138
2139 void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan,
2140 const struct ieee80211_ht_operation *ht_oper,
2141 struct cfg80211_chan_def *chandef)
2142 {
2143 enum nl80211_channel_type channel_type;
2144
2145 if (!ht_oper) {
2146 cfg80211_chandef_create(chandef, control_chan,
2147 NL80211_CHAN_NO_HT);
2148 return;
2149 }
2150
2151 switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
2152 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
2153 channel_type = NL80211_CHAN_HT20;
2154 break;
2155 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
2156 channel_type = NL80211_CHAN_HT40PLUS;
2157 break;
2158 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
2159 channel_type = NL80211_CHAN_HT40MINUS;
2160 break;
2161 default:
2162 channel_type = NL80211_CHAN_NO_HT;
2163 }
2164
2165 cfg80211_chandef_create(chandef, control_chan, channel_type);
2166 }
2167
2168 int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
2169 const struct ieee80211_supported_band *sband,
2170 const u8 *srates, int srates_len, u32 *rates)
2171 {
2172 u32 rate_flags = ieee80211_chandef_rate_flags(chandef);
2173 int shift = ieee80211_chandef_get_shift(chandef);
2174 struct ieee80211_rate *br;
2175 int brate, rate, i, j, count = 0;
2176
2177 *rates = 0;
2178
2179 for (i = 0; i < srates_len; i++) {
2180 rate = srates[i] & 0x7f;
2181
2182 for (j = 0; j < sband->n_bitrates; j++) {
2183 br = &sband->bitrates[j];
2184 if ((rate_flags & br->flags) != rate_flags)
2185 continue;
2186
2187 brate = DIV_ROUND_UP(br->bitrate, (1 << shift) * 5);
2188 if (brate == rate) {
2189 *rates |= BIT(j);
2190 count++;
2191 break;
2192 }
2193 }
2194 }
2195 return count;
2196 }
2197
2198 int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
2199 struct sk_buff *skb, bool need_basic,
2200 enum ieee80211_band band)
2201 {
2202 struct ieee80211_local *local = sdata->local;
2203 struct ieee80211_supported_band *sband;
2204 int rate, shift;
2205 u8 i, rates, *pos;
2206 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
2207 u32 rate_flags;
2208
2209 shift = ieee80211_vif_get_shift(&sdata->vif);
2210 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
2211 sband = local->hw.wiphy->bands[band];
2212 rates = 0;
2213 for (i = 0; i < sband->n_bitrates; i++) {
2214 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
2215 continue;
2216 rates++;
2217 }
2218 if (rates > 8)
2219 rates = 8;
2220
2221 if (skb_tailroom(skb) < rates + 2)
2222 return -ENOMEM;
2223
2224 pos = skb_put(skb, rates + 2);
2225 *pos++ = WLAN_EID_SUPP_RATES;
2226 *pos++ = rates;
2227 for (i = 0; i < rates; i++) {
2228 u8 basic = 0;
2229 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
2230 continue;
2231
2232 if (need_basic && basic_rates & BIT(i))
2233 basic = 0x80;
2234 rate = sband->bitrates[i].bitrate;
2235 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
2236 5 * (1 << shift));
2237 *pos++ = basic | (u8) rate;
2238 }
2239
2240 return 0;
2241 }
2242
2243 int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
2244 struct sk_buff *skb, bool need_basic,
2245 enum ieee80211_band band)
2246 {
2247 struct ieee80211_local *local = sdata->local;
2248 struct ieee80211_supported_band *sband;
2249 int rate, shift;
2250 u8 i, exrates, *pos;
2251 u32 basic_rates = sdata->vif.bss_conf.basic_rates;
2252 u32 rate_flags;
2253
2254 rate_flags = ieee80211_chandef_rate_flags(&sdata->vif.bss_conf.chandef);
2255 shift = ieee80211_vif_get_shift(&sdata->vif);
2256
2257 sband = local->hw.wiphy->bands[band];
2258 exrates = 0;
2259 for (i = 0; i < sband->n_bitrates; i++) {
2260 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
2261 continue;
2262 exrates++;
2263 }
2264
2265 if (exrates > 8)
2266 exrates -= 8;
2267 else
2268 exrates = 0;
2269
2270 if (skb_tailroom(skb) < exrates + 2)
2271 return -ENOMEM;
2272
2273 if (exrates) {
2274 pos = skb_put(skb, exrates + 2);
2275 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2276 *pos++ = exrates;
2277 for (i = 8; i < sband->n_bitrates; i++) {
2278 u8 basic = 0;
2279 if ((rate_flags & sband->bitrates[i].flags)
2280 != rate_flags)
2281 continue;
2282 if (need_basic && basic_rates & BIT(i))
2283 basic = 0x80;
2284 rate = DIV_ROUND_UP(sband->bitrates[i].bitrate,
2285 5 * (1 << shift));
2286 *pos++ = basic | (u8) rate;
2287 }
2288 }
2289 return 0;
2290 }
2291
2292 int ieee80211_ave_rssi(struct ieee80211_vif *vif)
2293 {
2294 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2295 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2296
2297 if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
2298 /* non-managed type inferfaces */
2299 return 0;
2300 }
2301 return ifmgd->ave_beacon_signal / 16;
2302 }
2303 EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
2304
2305 u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
2306 {
2307 if (!mcs)
2308 return 1;
2309
2310 /* TODO: consider rx_highest */
2311
2312 if (mcs->rx_mask[3])
2313 return 4;
2314 if (mcs->rx_mask[2])
2315 return 3;
2316 if (mcs->rx_mask[1])
2317 return 2;
2318 return 1;
2319 }
2320
2321 /**
2322 * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
2323 * @local: mac80211 hw info struct
2324 * @status: RX status
2325 * @mpdu_len: total MPDU length (including FCS)
2326 * @mpdu_offset: offset into MPDU to calculate timestamp at
2327 *
2328 * This function calculates the RX timestamp at the given MPDU offset, taking
2329 * into account what the RX timestamp was. An offset of 0 will just normalize
2330 * the timestamp to TSF at beginning of MPDU reception.
2331 */
2332 u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
2333 struct ieee80211_rx_status *status,
2334 unsigned int mpdu_len,
2335 unsigned int mpdu_offset)
2336 {
2337 u64 ts = status->mactime;
2338 struct rate_info ri;
2339 u16 rate;
2340
2341 if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
2342 return 0;
2343
2344 memset(&ri, 0, sizeof(ri));
2345
2346 /* Fill cfg80211 rate info */
2347 if (status->flag & RX_FLAG_HT) {
2348 ri.mcs = status->rate_idx;
2349 ri.flags |= RATE_INFO_FLAGS_MCS;
2350 if (status->flag & RX_FLAG_40MHZ)
2351 ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
2352 if (status->flag & RX_FLAG_SHORT_GI)
2353 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
2354 } else if (status->flag & RX_FLAG_VHT) {
2355 ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
2356 ri.mcs = status->rate_idx;
2357 ri.nss = status->vht_nss;
2358 if (status->flag & RX_FLAG_40MHZ)
2359 ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
2360 if (status->vht_flag & RX_VHT_FLAG_80MHZ)
2361 ri.flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
2362 if (status->vht_flag & RX_VHT_FLAG_80P80MHZ)
2363 ri.flags |= RATE_INFO_FLAGS_80P80_MHZ_WIDTH;
2364 if (status->vht_flag & RX_VHT_FLAG_160MHZ)
2365 ri.flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
2366 if (status->flag & RX_FLAG_SHORT_GI)
2367 ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
2368 } else {
2369 struct ieee80211_supported_band *sband;
2370 int shift = 0;
2371 int bitrate;
2372
2373 if (status->flag & RX_FLAG_10MHZ)
2374 shift = 1;
2375 if (status->flag & RX_FLAG_5MHZ)
2376 shift = 2;
2377
2378 sband = local->hw.wiphy->bands[status->band];
2379 bitrate = sband->bitrates[status->rate_idx].bitrate;
2380 ri.legacy = DIV_ROUND_UP(bitrate, (1 << shift));
2381 }
2382
2383 rate = cfg80211_calculate_bitrate(&ri);
2384 if (WARN_ONCE(!rate,
2385 "Invalid bitrate: flags=0x%x, idx=%d, vht_nss=%d\n",
2386 status->flag, status->rate_idx, status->vht_nss))
2387 return 0;
2388
2389 /* rewind from end of MPDU */
2390 if (status->flag & RX_FLAG_MACTIME_END)
2391 ts -= mpdu_len * 8 * 10 / rate;
2392
2393 ts += mpdu_offset * 8 * 10 / rate;
2394
2395 return ts;
2396 }
2397
2398 void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
2399 {
2400 struct ieee80211_sub_if_data *sdata;
2401 struct cfg80211_chan_def chandef;
2402
2403 mutex_lock(&local->mtx);
2404 mutex_lock(&local->iflist_mtx);
2405 list_for_each_entry(sdata, &local->interfaces, list) {
2406 /* it might be waiting for the local->mtx, but then
2407 * by the time it gets it, sdata->wdev.cac_started
2408 * will no longer be true
2409 */
2410 cancel_delayed_work(&sdata->dfs_cac_timer_work);
2411
2412 if (sdata->wdev.cac_started) {
2413 chandef = sdata->vif.bss_conf.chandef;
2414 ieee80211_vif_release_channel(sdata);
2415 cfg80211_cac_event(sdata->dev,
2416 &chandef,
2417 NL80211_RADAR_CAC_ABORTED,
2418 GFP_KERNEL);
2419 }
2420 }
2421 mutex_unlock(&local->iflist_mtx);
2422 mutex_unlock(&local->mtx);
2423 }
2424
2425 void ieee80211_dfs_radar_detected_work(struct work_struct *work)
2426 {
2427 struct ieee80211_local *local =
2428 container_of(work, struct ieee80211_local, radar_detected_work);
2429 struct cfg80211_chan_def chandef = local->hw.conf.chandef;
2430
2431 ieee80211_dfs_cac_cancel(local);
2432
2433 if (local->use_chanctx)
2434 /* currently not handled */
2435 WARN_ON(1);
2436 else
2437 cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
2438 }
2439
2440 void ieee80211_radar_detected(struct ieee80211_hw *hw)
2441 {
2442 struct ieee80211_local *local = hw_to_local(hw);
2443
2444 trace_api_radar_detected(local);
2445
2446 ieee80211_queue_work(hw, &local->radar_detected_work);
2447 }
2448 EXPORT_SYMBOL(ieee80211_radar_detected);
2449
2450 u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
2451 {
2452 u32 ret;
2453 int tmp;
2454
2455 switch (c->width) {
2456 case NL80211_CHAN_WIDTH_20:
2457 c->width = NL80211_CHAN_WIDTH_20_NOHT;
2458 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2459 break;
2460 case NL80211_CHAN_WIDTH_40:
2461 c->width = NL80211_CHAN_WIDTH_20;
2462 c->center_freq1 = c->chan->center_freq;
2463 ret = IEEE80211_STA_DISABLE_40MHZ |
2464 IEEE80211_STA_DISABLE_VHT;
2465 break;
2466 case NL80211_CHAN_WIDTH_80:
2467 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
2468 /* n_P40 */
2469 tmp /= 2;
2470 /* freq_P40 */
2471 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
2472 c->width = NL80211_CHAN_WIDTH_40;
2473 ret = IEEE80211_STA_DISABLE_VHT;
2474 break;
2475 case NL80211_CHAN_WIDTH_80P80:
2476 c->center_freq2 = 0;
2477 c->width = NL80211_CHAN_WIDTH_80;
2478 ret = IEEE80211_STA_DISABLE_80P80MHZ |
2479 IEEE80211_STA_DISABLE_160MHZ;
2480 break;
2481 case NL80211_CHAN_WIDTH_160:
2482 /* n_P20 */
2483 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
2484 /* n_P80 */
2485 tmp /= 4;
2486 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
2487 c->width = NL80211_CHAN_WIDTH_80;
2488 ret = IEEE80211_STA_DISABLE_80P80MHZ |
2489 IEEE80211_STA_DISABLE_160MHZ;
2490 break;
2491 default:
2492 case NL80211_CHAN_WIDTH_20_NOHT:
2493 WARN_ON_ONCE(1);
2494 c->width = NL80211_CHAN_WIDTH_20_NOHT;
2495 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2496 break;
2497 case NL80211_CHAN_WIDTH_5:
2498 case NL80211_CHAN_WIDTH_10:
2499 WARN_ON_ONCE(1);
2500 /* keep c->width */
2501 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2502 break;
2503 }
2504
2505 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
2506
2507 return ret;
2508 }
2509
2510 /*
2511 * Returns true if smps_mode_new is strictly more restrictive than
2512 * smps_mode_old.
2513 */
2514 bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
2515 enum ieee80211_smps_mode smps_mode_new)
2516 {
2517 if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
2518 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
2519 return false;
2520
2521 switch (smps_mode_old) {
2522 case IEEE80211_SMPS_STATIC:
2523 return false;
2524 case IEEE80211_SMPS_DYNAMIC:
2525 return smps_mode_new == IEEE80211_SMPS_STATIC;
2526 case IEEE80211_SMPS_OFF:
2527 return smps_mode_new != IEEE80211_SMPS_OFF;
2528 default:
2529 WARN_ON(1);
2530 }
2531
2532 return false;
2533 }
2534
2535 int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
2536 struct cfg80211_csa_settings *csa_settings)
2537 {
2538 struct sk_buff *skb;
2539 struct ieee80211_mgmt *mgmt;
2540 struct ieee80211_local *local = sdata->local;
2541 int freq;
2542 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.chan_switch) +
2543 sizeof(mgmt->u.action.u.chan_switch);
2544 u8 *pos;
2545
2546 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2547 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
2548 return -EOPNOTSUPP;
2549
2550 skb = dev_alloc_skb(local->tx_headroom + hdr_len +
2551 5 + /* channel switch announcement element */
2552 3 + /* secondary channel offset element */
2553 8); /* mesh channel switch parameters element */
2554 if (!skb)
2555 return -ENOMEM;
2556
2557 skb_reserve(skb, local->tx_headroom);
2558 mgmt = (struct ieee80211_mgmt *)skb_put(skb, hdr_len);
2559 memset(mgmt, 0, hdr_len);
2560 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2561 IEEE80211_STYPE_ACTION);
2562
2563 eth_broadcast_addr(mgmt->da);
2564 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
2565 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2566 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
2567 } else {
2568 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2569 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
2570 }
2571 mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
2572 mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
2573 pos = skb_put(skb, 5);
2574 *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
2575 *pos++ = 3; /* IE length */
2576 *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
2577 freq = csa_settings->chandef.chan->center_freq;
2578 *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
2579 *pos++ = csa_settings->count; /* count */
2580
2581 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
2582 enum nl80211_channel_type ch_type;
2583
2584 skb_put(skb, 3);
2585 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
2586 *pos++ = 1; /* IE length */
2587 ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
2588 if (ch_type == NL80211_CHAN_HT40PLUS)
2589 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
2590 else
2591 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
2592 }
2593
2594 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2595 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2596
2597 skb_put(skb, 8);
2598 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
2599 *pos++ = 6; /* IE length */
2600 *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
2601 *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
2602 *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
2603 *pos++ |= csa_settings->block_tx ?
2604 WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
2605 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
2606 pos += 2;
2607 put_unaligned_le16(ifmsh->pre_value, pos);/* Precedence Value */
2608 pos += 2;
2609 }
2610
2611 ieee80211_tx_skb(sdata, skb);
2612 return 0;
2613 }
2614
2615 bool ieee80211_cs_valid(const struct ieee80211_cipher_scheme *cs)
2616 {
2617 return !(cs == NULL || cs->cipher == 0 ||
2618 cs->hdr_len < cs->pn_len + cs->pn_off ||
2619 cs->hdr_len <= cs->key_idx_off ||
2620 cs->key_idx_shift > 7 ||
2621 cs->key_idx_mask == 0);
2622 }
2623
2624 bool ieee80211_cs_list_valid(const struct ieee80211_cipher_scheme *cs, int n)
2625 {
2626 int i;
2627
2628 /* Ensure we have enough iftype bitmap space for all iftype values */
2629 WARN_ON((NUM_NL80211_IFTYPES / 8 + 1) > sizeof(cs[0].iftype));
2630
2631 for (i = 0; i < n; i++)
2632 if (!ieee80211_cs_valid(&cs[i]))
2633 return false;
2634
2635 return true;
2636 }
2637
2638 const struct ieee80211_cipher_scheme *
2639 ieee80211_cs_get(struct ieee80211_local *local, u32 cipher,
2640 enum nl80211_iftype iftype)
2641 {
2642 const struct ieee80211_cipher_scheme *l = local->hw.cipher_schemes;
2643 int n = local->hw.n_cipher_schemes;
2644 int i;
2645 const struct ieee80211_cipher_scheme *cs = NULL;
2646
2647 for (i = 0; i < n; i++) {
2648 if (l[i].cipher == cipher) {
2649 cs = &l[i];
2650 break;
2651 }
2652 }
2653
2654 if (!cs || !(cs->iftype & BIT(iftype)))
2655 return NULL;
2656
2657 return cs;
2658 }
2659
2660 int ieee80211_cs_headroom(struct ieee80211_local *local,
2661 struct cfg80211_crypto_settings *crypto,
2662 enum nl80211_iftype iftype)
2663 {
2664 const struct ieee80211_cipher_scheme *cs;
2665 int headroom = IEEE80211_ENCRYPT_HEADROOM;
2666 int i;
2667
2668 for (i = 0; i < crypto->n_ciphers_pairwise; i++) {
2669 cs = ieee80211_cs_get(local, crypto->ciphers_pairwise[i],
2670 iftype);
2671
2672 if (cs && headroom < cs->hdr_len)
2673 headroom = cs->hdr_len;
2674 }
2675
2676 cs = ieee80211_cs_get(local, crypto->cipher_group, iftype);
2677 if (cs && headroom < cs->hdr_len)
2678 headroom = cs->hdr_len;
2679
2680 return headroom;
2681 }
2682
2683 static bool
2684 ieee80211_extend_noa_desc(struct ieee80211_noa_data *data, u32 tsf, int i)
2685 {
2686 s32 end = data->desc[i].start + data->desc[i].duration - (tsf + 1);
2687 int skip;
2688
2689 if (end > 0)
2690 return false;
2691
2692 /* End time is in the past, check for repetitions */
2693 skip = DIV_ROUND_UP(-end, data->desc[i].interval);
2694 if (data->count[i] < 255) {
2695 if (data->count[i] <= skip) {
2696 data->count[i] = 0;
2697 return false;
2698 }
2699
2700 data->count[i] -= skip;
2701 }
2702
2703 data->desc[i].start += skip * data->desc[i].interval;
2704
2705 return true;
2706 }
2707
2708 static bool
2709 ieee80211_extend_absent_time(struct ieee80211_noa_data *data, u32 tsf,
2710 s32 *offset)
2711 {
2712 bool ret = false;
2713 int i;
2714
2715 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
2716 s32 cur;
2717
2718 if (!data->count[i])
2719 continue;
2720
2721 if (ieee80211_extend_noa_desc(data, tsf + *offset, i))
2722 ret = true;
2723
2724 cur = data->desc[i].start - tsf;
2725 if (cur > *offset)
2726 continue;
2727
2728 cur = data->desc[i].start + data->desc[i].duration - tsf;
2729 if (cur > *offset)
2730 *offset = cur;
2731 }
2732
2733 return ret;
2734 }
2735
2736 static u32
2737 ieee80211_get_noa_absent_time(struct ieee80211_noa_data *data, u32 tsf)
2738 {
2739 s32 offset = 0;
2740 int tries = 0;
2741 /*
2742 * arbitrary limit, used to avoid infinite loops when combined NoA
2743 * descriptors cover the full time period.
2744 */
2745 int max_tries = 5;
2746
2747 ieee80211_extend_absent_time(data, tsf, &offset);
2748 do {
2749 if (!ieee80211_extend_absent_time(data, tsf, &offset))
2750 break;
2751
2752 tries++;
2753 } while (tries < max_tries);
2754
2755 return offset;
2756 }
2757
2758 void ieee80211_update_p2p_noa(struct ieee80211_noa_data *data, u32 tsf)
2759 {
2760 u32 next_offset = BIT(31) - 1;
2761 int i;
2762
2763 data->absent = 0;
2764 data->has_next_tsf = false;
2765 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
2766 s32 start;
2767
2768 if (!data->count[i])
2769 continue;
2770
2771 ieee80211_extend_noa_desc(data, tsf, i);
2772 start = data->desc[i].start - tsf;
2773 if (start <= 0)
2774 data->absent |= BIT(i);
2775
2776 if (next_offset > start)
2777 next_offset = start;
2778
2779 data->has_next_tsf = true;
2780 }
2781
2782 if (data->absent)
2783 next_offset = ieee80211_get_noa_absent_time(data, tsf);
2784
2785 data->next_tsf = tsf + next_offset;
2786 }
2787 EXPORT_SYMBOL(ieee80211_update_p2p_noa);
2788
2789 int ieee80211_parse_p2p_noa(const struct ieee80211_p2p_noa_attr *attr,
2790 struct ieee80211_noa_data *data, u32 tsf)
2791 {
2792 int ret = 0;
2793 int i;
2794
2795 memset(data, 0, sizeof(*data));
2796
2797 for (i = 0; i < IEEE80211_P2P_NOA_DESC_MAX; i++) {
2798 const struct ieee80211_p2p_noa_desc *desc = &attr->desc[i];
2799
2800 if (!desc->count || !desc->duration)
2801 continue;
2802
2803 data->count[i] = desc->count;
2804 data->desc[i].start = le32_to_cpu(desc->start_time);
2805 data->desc[i].duration = le32_to_cpu(desc->duration);
2806 data->desc[i].interval = le32_to_cpu(desc->interval);
2807
2808 if (data->count[i] > 1 &&
2809 data->desc[i].interval < data->desc[i].duration)
2810 continue;
2811
2812 ieee80211_extend_noa_desc(data, tsf, i);
2813 ret++;
2814 }
2815
2816 if (ret)
2817 ieee80211_update_p2p_noa(data, tsf);
2818
2819 return ret;
2820 }
2821 EXPORT_SYMBOL(ieee80211_parse_p2p_noa);
2822
2823 void ieee80211_recalc_dtim(struct ieee80211_local *local,
2824 struct ieee80211_sub_if_data *sdata)
2825 {
2826 u64 tsf = drv_get_tsf(local, sdata);
2827 u64 dtim_count = 0;
2828 u16 beacon_int = sdata->vif.bss_conf.beacon_int * 1024;
2829 u8 dtim_period = sdata->vif.bss_conf.dtim_period;
2830 struct ps_data *ps;
2831 u8 bcns_from_dtim;
2832
2833 if (tsf == -1ULL || !beacon_int || !dtim_period)
2834 return;
2835
2836 if (sdata->vif.type == NL80211_IFTYPE_AP ||
2837 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
2838 if (!sdata->bss)
2839 return;
2840
2841 ps = &sdata->bss->ps;
2842 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2843 ps = &sdata->u.mesh.ps;
2844 } else {
2845 return;
2846 }
2847
2848 /*
2849 * actually finds last dtim_count, mac80211 will update in
2850 * __beacon_add_tim().
2851 * dtim_count = dtim_period - (tsf / bcn_int) % dtim_period
2852 */
2853 do_div(tsf, beacon_int);
2854 bcns_from_dtim = do_div(tsf, dtim_period);
2855 /* just had a DTIM */
2856 if (!bcns_from_dtim)
2857 dtim_count = 0;
2858 else
2859 dtim_count = dtim_period - bcns_from_dtim;
2860
2861 ps->dtim_count = dtim_count;
2862 }
2863
2864 int ieee80211_check_combinations(struct ieee80211_sub_if_data *sdata,
2865 const struct cfg80211_chan_def *chandef,
2866 enum ieee80211_chanctx_mode chanmode,
2867 u8 radar_detect)
2868 {
2869 struct ieee80211_local *local = sdata->local;
2870 struct ieee80211_sub_if_data *sdata_iter;
2871 enum nl80211_iftype iftype = sdata->wdev.iftype;
2872 int num[NUM_NL80211_IFTYPES];
2873 struct ieee80211_chanctx *ctx;
2874 int num_different_channels = 0;
2875 int total = 1;
2876
2877 lockdep_assert_held(&local->chanctx_mtx);
2878
2879 if (WARN_ON(hweight32(radar_detect) > 1))
2880 return -EINVAL;
2881
2882 if (WARN_ON(chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
2883 !chandef->chan))
2884 return -EINVAL;
2885
2886 if (chandef)
2887 num_different_channels = 1;
2888
2889 if (WARN_ON(iftype >= NUM_NL80211_IFTYPES))
2890 return -EINVAL;
2891
2892 /* Always allow software iftypes */
2893 if (local->hw.wiphy->software_iftypes & BIT(iftype)) {
2894 if (radar_detect)
2895 return -EINVAL;
2896 return 0;
2897 }
2898
2899 memset(num, 0, sizeof(num));
2900
2901 if (iftype != NL80211_IFTYPE_UNSPECIFIED)
2902 num[iftype] = 1;
2903
2904 list_for_each_entry(ctx, &local->chanctx_list, list) {
2905 if (ctx->conf.radar_enabled)
2906 radar_detect |= BIT(ctx->conf.def.width);
2907 if (ctx->mode == IEEE80211_CHANCTX_EXCLUSIVE) {
2908 num_different_channels++;
2909 continue;
2910 }
2911 if (chandef && chanmode == IEEE80211_CHANCTX_SHARED &&
2912 cfg80211_chandef_compatible(chandef,
2913 &ctx->conf.def))
2914 continue;
2915 num_different_channels++;
2916 }
2917
2918 list_for_each_entry_rcu(sdata_iter, &local->interfaces, list) {
2919 struct wireless_dev *wdev_iter;
2920
2921 wdev_iter = &sdata_iter->wdev;
2922
2923 if (sdata_iter == sdata ||
2924 rcu_access_pointer(sdata_iter->vif.chanctx_conf) == NULL ||
2925 local->hw.wiphy->software_iftypes & BIT(wdev_iter->iftype))
2926 continue;
2927
2928 num[wdev_iter->iftype]++;
2929 total++;
2930 }
2931
2932 if (total == 1 && !radar_detect)
2933 return 0;
2934
2935 return cfg80211_check_combinations(local->hw.wiphy,
2936 num_different_channels,
2937 radar_detect, num);
2938 }
2939
2940 static void
2941 ieee80211_iter_max_chans(const struct ieee80211_iface_combination *c,
2942 void *data)
2943 {
2944 u32 *max_num_different_channels = data;
2945
2946 *max_num_different_channels = max(*max_num_different_channels,
2947 c->num_different_channels);
2948 }
2949
2950 int ieee80211_max_num_channels(struct ieee80211_local *local)
2951 {
2952 struct ieee80211_sub_if_data *sdata;
2953 int num[NUM_NL80211_IFTYPES] = {};
2954 struct ieee80211_chanctx *ctx;
2955 int num_different_channels = 0;
2956 u8 radar_detect = 0;
2957 u32 max_num_different_channels = 1;
2958 int err;
2959
2960 lockdep_assert_held(&local->chanctx_mtx);
2961
2962 list_for_each_entry(ctx, &local->chanctx_list, list) {
2963 num_different_channels++;
2964
2965 if (ctx->conf.radar_enabled)
2966 radar_detect |= BIT(ctx->conf.def.width);
2967 }
2968
2969 list_for_each_entry_rcu(sdata, &local->interfaces, list)
2970 num[sdata->wdev.iftype]++;
2971
2972 err = cfg80211_iter_combinations(local->hw.wiphy,
2973 num_different_channels, radar_detect,
2974 num, ieee80211_iter_max_chans,
2975 &max_num_different_channels);
2976 if (err < 0)
2977 return err;
2978
2979 return max_num_different_channels;
2980 }
This page took 0.143958 seconds and 5 git commands to generate.