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