mac80211: support secondary channel offset in CSA
[cascardo/linux.git] / net / mac80211 / util.c
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2005-2006, Devicescape Software, Inc.
4  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
5  * Copyright 2007       Johannes Berg <johannes@sipsolutions.net>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * utilities for mac80211
12  */
13
14 #include <net/mac80211.h>
15 #include <linux/netdevice.h>
16 #include <linux/export.h>
17 #include <linux/types.h>
18 #include <linux/slab.h>
19 #include <linux/skbuff.h>
20 #include <linux/etherdevice.h>
21 #include <linux/if_arp.h>
22 #include <linux/bitmap.h>
23 #include <linux/crc32.h>
24 #include <net/net_namespace.h>
25 #include <net/cfg80211.h>
26 #include <net/rtnetlink.h>
27
28 #include "ieee80211_i.h"
29 #include "driver-ops.h"
30 #include "rate.h"
31 #include "mesh.h"
32 #include "wme.h"
33 #include "led.h"
34 #include "wep.h"
35
36 /* privid for wiphys to determine whether they belong to us or not */
37 void *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 {
112         int dur;
113
114         /* calculate duration (in microseconds, rounded up to next higher
115          * integer if it includes a fractional microsecond) to send frame of
116          * len bytes (does not include FCS) at the given rate. Duration will
117          * also include SIFS.
118          *
119          * rate is in 100 kbps, so divident is multiplied by 10 in the
120          * DIV_ROUND_UP() operations.
121          */
122
123         if (band == IEEE80211_BAND_5GHZ || erp) {
124                 /*
125                  * OFDM:
126                  *
127                  * N_DBPS = DATARATE x 4
128                  * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
129                  *      (16 = SIGNAL time, 6 = tail bits)
130                  * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
131                  *
132                  * T_SYM = 4 usec
133                  * 802.11a - 17.5.2: aSIFSTime = 16 usec
134                  * 802.11g - 19.8.4: aSIFSTime = 10 usec +
135                  *      signal ext = 6 usec
136                  */
137                 dur = 16; /* SIFS + signal ext */
138                 dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
139                 dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
140                 dur += 4 * DIV_ROUND_UP((16 + 8 * (len + 4) + 6) * 10,
141                                         4 * rate); /* T_SYM x N_SYM */
142         } else {
143                 /*
144                  * 802.11b or 802.11g with 802.11b compatibility:
145                  * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
146                  * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
147                  *
148                  * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
149                  * aSIFSTime = 10 usec
150                  * aPreambleLength = 144 usec or 72 usec with short preamble
151                  * aPLCPHeaderLength = 48 usec or 24 usec with short preamble
152                  */
153                 dur = 10; /* aSIFSTime = 10 usec */
154                 dur += short_preamble ? (72 + 24) : (144 + 48);
155
156                 dur += DIV_ROUND_UP(8 * (len + 4) * 10, rate);
157         }
158
159         return dur;
160 }
161
162 /* Exported duration function for driver use */
163 __le16 ieee80211_generic_frame_duration(struct ieee80211_hw *hw,
164                                         struct ieee80211_vif *vif,
165                                         enum ieee80211_band band,
166                                         size_t frame_len,
167                                         struct ieee80211_rate *rate)
168 {
169         struct ieee80211_sub_if_data *sdata;
170         u16 dur;
171         int erp;
172         bool short_preamble = false;
173
174         erp = 0;
175         if (vif) {
176                 sdata = vif_to_sdata(vif);
177                 short_preamble = sdata->vif.bss_conf.use_short_preamble;
178                 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
179                         erp = rate->flags & IEEE80211_RATE_ERP_G;
180         }
181
182         dur = ieee80211_frame_duration(band, frame_len, rate->bitrate, erp,
183                                        short_preamble);
184
185         return cpu_to_le16(dur);
186 }
187 EXPORT_SYMBOL(ieee80211_generic_frame_duration);
188
189 __le16 ieee80211_rts_duration(struct ieee80211_hw *hw,
190                               struct ieee80211_vif *vif, size_t frame_len,
191                               const struct ieee80211_tx_info *frame_txctl)
192 {
193         struct ieee80211_local *local = hw_to_local(hw);
194         struct ieee80211_rate *rate;
195         struct ieee80211_sub_if_data *sdata;
196         bool short_preamble;
197         int erp;
198         u16 dur;
199         struct ieee80211_supported_band *sband;
200
201         sband = local->hw.wiphy->bands[frame_txctl->band];
202
203         short_preamble = false;
204
205         rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
206
207         erp = 0;
208         if (vif) {
209                 sdata = vif_to_sdata(vif);
210                 short_preamble = sdata->vif.bss_conf.use_short_preamble;
211                 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
212                         erp = rate->flags & IEEE80211_RATE_ERP_G;
213         }
214
215         /* CTS duration */
216         dur = ieee80211_frame_duration(sband->band, 10, rate->bitrate,
217                                        erp, short_preamble);
218         /* Data frame duration */
219         dur += ieee80211_frame_duration(sband->band, frame_len, rate->bitrate,
220                                         erp, short_preamble);
221         /* ACK duration */
222         dur += ieee80211_frame_duration(sband->band, 10, rate->bitrate,
223                                         erp, short_preamble);
224
225         return cpu_to_le16(dur);
226 }
227 EXPORT_SYMBOL(ieee80211_rts_duration);
228
229 __le16 ieee80211_ctstoself_duration(struct ieee80211_hw *hw,
230                                     struct ieee80211_vif *vif,
231                                     size_t frame_len,
232                                     const struct ieee80211_tx_info *frame_txctl)
233 {
234         struct ieee80211_local *local = hw_to_local(hw);
235         struct ieee80211_rate *rate;
236         struct ieee80211_sub_if_data *sdata;
237         bool short_preamble;
238         int erp;
239         u16 dur;
240         struct ieee80211_supported_band *sband;
241
242         sband = local->hw.wiphy->bands[frame_txctl->band];
243
244         short_preamble = false;
245
246         rate = &sband->bitrates[frame_txctl->control.rts_cts_rate_idx];
247         erp = 0;
248         if (vif) {
249                 sdata = vif_to_sdata(vif);
250                 short_preamble = sdata->vif.bss_conf.use_short_preamble;
251                 if (sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
252                         erp = rate->flags & IEEE80211_RATE_ERP_G;
253         }
254
255         /* Data frame duration */
256         dur = ieee80211_frame_duration(sband->band, frame_len, rate->bitrate,
257                                        erp, short_preamble);
258         if (!(frame_txctl->flags & IEEE80211_TX_CTL_NO_ACK)) {
259                 /* ACK duration */
260                 dur += ieee80211_frame_duration(sband->band, 10, rate->bitrate,
261                                                 erp, short_preamble);
262         }
263
264         return cpu_to_le16(dur);
265 }
266 EXPORT_SYMBOL(ieee80211_ctstoself_duration);
267
268 void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
269 {
270         struct ieee80211_sub_if_data *sdata;
271         int n_acs = IEEE80211_NUM_ACS;
272
273         if (local->hw.queues < IEEE80211_NUM_ACS)
274                 n_acs = 1;
275
276         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
277                 int ac;
278
279                 if (!sdata->dev)
280                         continue;
281
282                 if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
283                         continue;
284
285                 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
286                     local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
287                         continue;
288
289                 for (ac = 0; ac < n_acs; ac++) {
290                         int ac_queue = sdata->vif.hw_queue[ac];
291
292                         if (ac_queue == queue ||
293                             (sdata->vif.cab_queue == queue &&
294                              local->queue_stop_reasons[ac_queue] == 0 &&
295                              skb_queue_empty(&local->pending[ac_queue])))
296                                 netif_wake_subqueue(sdata->dev, ac);
297                 }
298         }
299 }
300
301 static void __ieee80211_wake_queue(struct ieee80211_hw *hw, int queue,
302                                    enum queue_stop_reason reason)
303 {
304         struct ieee80211_local *local = hw_to_local(hw);
305
306         trace_wake_queue(local, queue, reason);
307
308         if (WARN_ON(queue >= hw->queues))
309                 return;
310
311         if (!test_bit(reason, &local->queue_stop_reasons[queue]))
312                 return;
313
314         __clear_bit(reason, &local->queue_stop_reasons[queue]);
315
316         if (local->queue_stop_reasons[queue] != 0)
317                 /* someone still has this queue stopped */
318                 return;
319
320         if (skb_queue_empty(&local->pending[queue])) {
321                 rcu_read_lock();
322                 ieee80211_propagate_queue_wake(local, queue);
323                 rcu_read_unlock();
324         } else
325                 tasklet_schedule(&local->tx_pending_tasklet);
326 }
327
328 void ieee80211_wake_queue_by_reason(struct ieee80211_hw *hw, int queue,
329                                     enum queue_stop_reason reason)
330 {
331         struct ieee80211_local *local = hw_to_local(hw);
332         unsigned long flags;
333
334         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
335         __ieee80211_wake_queue(hw, queue, reason);
336         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
337 }
338
339 void ieee80211_wake_queue(struct ieee80211_hw *hw, int queue)
340 {
341         ieee80211_wake_queue_by_reason(hw, queue,
342                                        IEEE80211_QUEUE_STOP_REASON_DRIVER);
343 }
344 EXPORT_SYMBOL(ieee80211_wake_queue);
345
346 static void __ieee80211_stop_queue(struct ieee80211_hw *hw, int queue,
347                                    enum queue_stop_reason reason)
348 {
349         struct ieee80211_local *local = hw_to_local(hw);
350         struct ieee80211_sub_if_data *sdata;
351         int n_acs = IEEE80211_NUM_ACS;
352
353         trace_stop_queue(local, queue, reason);
354
355         if (WARN_ON(queue >= hw->queues))
356                 return;
357
358         if (test_bit(reason, &local->queue_stop_reasons[queue]))
359                 return;
360
361         __set_bit(reason, &local->queue_stop_reasons[queue]);
362
363         if (local->hw.queues < IEEE80211_NUM_ACS)
364                 n_acs = 1;
365
366         rcu_read_lock();
367         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
368                 int ac;
369
370                 if (!sdata->dev)
371                         continue;
372
373                 for (ac = 0; ac < n_acs; ac++) {
374                         if (sdata->vif.hw_queue[ac] == queue ||
375                             sdata->vif.cab_queue == queue)
376                                 netif_stop_subqueue(sdata->dev, ac);
377                 }
378         }
379         rcu_read_unlock();
380 }
381
382 void ieee80211_stop_queue_by_reason(struct ieee80211_hw *hw, int queue,
383                                     enum queue_stop_reason reason)
384 {
385         struct ieee80211_local *local = hw_to_local(hw);
386         unsigned long flags;
387
388         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
389         __ieee80211_stop_queue(hw, queue, reason);
390         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
391 }
392
393 void ieee80211_stop_queue(struct ieee80211_hw *hw, int queue)
394 {
395         ieee80211_stop_queue_by_reason(hw, queue,
396                                        IEEE80211_QUEUE_STOP_REASON_DRIVER);
397 }
398 EXPORT_SYMBOL(ieee80211_stop_queue);
399
400 void ieee80211_add_pending_skb(struct ieee80211_local *local,
401                                struct sk_buff *skb)
402 {
403         struct ieee80211_hw *hw = &local->hw;
404         unsigned long flags;
405         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
406         int queue = info->hw_queue;
407
408         if (WARN_ON(!info->control.vif)) {
409                 ieee80211_free_txskb(&local->hw, skb);
410                 return;
411         }
412
413         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
414         __ieee80211_stop_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
415         __skb_queue_tail(&local->pending[queue], skb);
416         __ieee80211_wake_queue(hw, queue, IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
417         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
418 }
419
420 void ieee80211_add_pending_skbs_fn(struct ieee80211_local *local,
421                                    struct sk_buff_head *skbs,
422                                    void (*fn)(void *data), void *data)
423 {
424         struct ieee80211_hw *hw = &local->hw;
425         struct sk_buff *skb;
426         unsigned long flags;
427         int queue, i;
428
429         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
430         while ((skb = skb_dequeue(skbs))) {
431                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
432
433                 if (WARN_ON(!info->control.vif)) {
434                         ieee80211_free_txskb(&local->hw, skb);
435                         continue;
436                 }
437
438                 queue = info->hw_queue;
439
440                 __ieee80211_stop_queue(hw, queue,
441                                 IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
442
443                 __skb_queue_tail(&local->pending[queue], skb);
444         }
445
446         if (fn)
447                 fn(data);
448
449         for (i = 0; i < hw->queues; i++)
450                 __ieee80211_wake_queue(hw, i,
451                         IEEE80211_QUEUE_STOP_REASON_SKB_ADD);
452         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
453 }
454
455 void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
456                                      unsigned long queues,
457                                      enum queue_stop_reason reason)
458 {
459         struct ieee80211_local *local = hw_to_local(hw);
460         unsigned long flags;
461         int i;
462
463         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
464
465         for_each_set_bit(i, &queues, hw->queues)
466                 __ieee80211_stop_queue(hw, i, reason);
467
468         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
469 }
470
471 void ieee80211_stop_queues(struct ieee80211_hw *hw)
472 {
473         ieee80211_stop_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
474                                         IEEE80211_QUEUE_STOP_REASON_DRIVER);
475 }
476 EXPORT_SYMBOL(ieee80211_stop_queues);
477
478 int ieee80211_queue_stopped(struct ieee80211_hw *hw, int queue)
479 {
480         struct ieee80211_local *local = hw_to_local(hw);
481         unsigned long flags;
482         int ret;
483
484         if (WARN_ON(queue >= hw->queues))
485                 return true;
486
487         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
488         ret = test_bit(IEEE80211_QUEUE_STOP_REASON_DRIVER,
489                        &local->queue_stop_reasons[queue]);
490         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
491         return ret;
492 }
493 EXPORT_SYMBOL(ieee80211_queue_stopped);
494
495 void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
496                                      unsigned long queues,
497                                      enum queue_stop_reason reason)
498 {
499         struct ieee80211_local *local = hw_to_local(hw);
500         unsigned long flags;
501         int i;
502
503         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
504
505         for_each_set_bit(i, &queues, hw->queues)
506                 __ieee80211_wake_queue(hw, i, reason);
507
508         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
509 }
510
511 void ieee80211_wake_queues(struct ieee80211_hw *hw)
512 {
513         ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
514                                         IEEE80211_QUEUE_STOP_REASON_DRIVER);
515 }
516 EXPORT_SYMBOL(ieee80211_wake_queues);
517
518 void ieee80211_flush_queues(struct ieee80211_local *local,
519                             struct ieee80211_sub_if_data *sdata)
520 {
521         u32 queues;
522
523         if (!local->ops->flush)
524                 return;
525
526         if (sdata && local->hw.flags & IEEE80211_HW_QUEUE_CONTROL) {
527                 int ac;
528
529                 queues = 0;
530
531                 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
532                         queues |= BIT(sdata->vif.hw_queue[ac]);
533                 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE)
534                         queues |= BIT(sdata->vif.cab_queue);
535         } else {
536                 /* all queues */
537                 queues = BIT(local->hw.queues) - 1;
538         }
539
540         ieee80211_stop_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
541                                         IEEE80211_QUEUE_STOP_REASON_FLUSH);
542
543         drv_flush(local, queues, false);
544
545         ieee80211_wake_queues_by_reason(&local->hw, IEEE80211_MAX_QUEUE_MAP,
546                                         IEEE80211_QUEUE_STOP_REASON_FLUSH);
547 }
548
549 void ieee80211_iterate_active_interfaces(
550         struct ieee80211_hw *hw, u32 iter_flags,
551         void (*iterator)(void *data, u8 *mac,
552                          struct ieee80211_vif *vif),
553         void *data)
554 {
555         struct ieee80211_local *local = hw_to_local(hw);
556         struct ieee80211_sub_if_data *sdata;
557
558         mutex_lock(&local->iflist_mtx);
559
560         list_for_each_entry(sdata, &local->interfaces, list) {
561                 switch (sdata->vif.type) {
562                 case NL80211_IFTYPE_MONITOR:
563                 case NL80211_IFTYPE_AP_VLAN:
564                         continue;
565                 default:
566                         break;
567                 }
568                 if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
569                     !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
570                         continue;
571                 if (ieee80211_sdata_running(sdata))
572                         iterator(data, sdata->vif.addr,
573                                  &sdata->vif);
574         }
575
576         sdata = rcu_dereference_protected(local->monitor_sdata,
577                                           lockdep_is_held(&local->iflist_mtx));
578         if (sdata &&
579             (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
580              sdata->flags & IEEE80211_SDATA_IN_DRIVER))
581                 iterator(data, sdata->vif.addr, &sdata->vif);
582
583         mutex_unlock(&local->iflist_mtx);
584 }
585 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
586
587 void ieee80211_iterate_active_interfaces_atomic(
588         struct ieee80211_hw *hw, u32 iter_flags,
589         void (*iterator)(void *data, u8 *mac,
590                          struct ieee80211_vif *vif),
591         void *data)
592 {
593         struct ieee80211_local *local = hw_to_local(hw);
594         struct ieee80211_sub_if_data *sdata;
595
596         rcu_read_lock();
597
598         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
599                 switch (sdata->vif.type) {
600                 case NL80211_IFTYPE_MONITOR:
601                 case NL80211_IFTYPE_AP_VLAN:
602                         continue;
603                 default:
604                         break;
605                 }
606                 if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
607                     !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
608                         continue;
609                 if (ieee80211_sdata_running(sdata))
610                         iterator(data, sdata->vif.addr,
611                                  &sdata->vif);
612         }
613
614         sdata = rcu_dereference(local->monitor_sdata);
615         if (sdata &&
616             (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
617              sdata->flags & IEEE80211_SDATA_IN_DRIVER))
618                 iterator(data, sdata->vif.addr, &sdata->vif);
619
620         rcu_read_unlock();
621 }
622 EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
623
624 /*
625  * Nothing should have been stuffed into the workqueue during
626  * the suspend->resume cycle. If this WARN is seen then there
627  * is a bug with either the driver suspend or something in
628  * mac80211 stuffing into the workqueue which we haven't yet
629  * cleared during mac80211's suspend cycle.
630  */
631 static bool ieee80211_can_queue_work(struct ieee80211_local *local)
632 {
633         if (WARN(local->suspended && !local->resuming,
634                  "queueing ieee80211 work while going to suspend\n"))
635                 return false;
636
637         return true;
638 }
639
640 void ieee80211_queue_work(struct ieee80211_hw *hw, struct work_struct *work)
641 {
642         struct ieee80211_local *local = hw_to_local(hw);
643
644         if (!ieee80211_can_queue_work(local))
645                 return;
646
647         queue_work(local->workqueue, work);
648 }
649 EXPORT_SYMBOL(ieee80211_queue_work);
650
651 void ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
652                                   struct delayed_work *dwork,
653                                   unsigned long delay)
654 {
655         struct ieee80211_local *local = hw_to_local(hw);
656
657         if (!ieee80211_can_queue_work(local))
658                 return;
659
660         queue_delayed_work(local->workqueue, dwork, delay);
661 }
662 EXPORT_SYMBOL(ieee80211_queue_delayed_work);
663
664 u32 ieee802_11_parse_elems_crc(u8 *start, size_t len,
665                                struct ieee802_11_elems *elems,
666                                u64 filter, u32 crc)
667 {
668         size_t left = len;
669         u8 *pos = start;
670         bool calc_crc = filter != 0;
671         DECLARE_BITMAP(seen_elems, 256);
672
673         bitmap_zero(seen_elems, 256);
674         memset(elems, 0, sizeof(*elems));
675         elems->ie_start = start;
676         elems->total_len = len;
677
678         while (left >= 2) {
679                 u8 id, elen;
680                 bool elem_parse_failed;
681
682                 id = *pos++;
683                 elen = *pos++;
684                 left -= 2;
685
686                 if (elen > left) {
687                         elems->parse_error = true;
688                         break;
689                 }
690
691                 switch (id) {
692                 case WLAN_EID_SSID:
693                 case WLAN_EID_SUPP_RATES:
694                 case WLAN_EID_FH_PARAMS:
695                 case WLAN_EID_DS_PARAMS:
696                 case WLAN_EID_CF_PARAMS:
697                 case WLAN_EID_TIM:
698                 case WLAN_EID_IBSS_PARAMS:
699                 case WLAN_EID_CHALLENGE:
700                 case WLAN_EID_RSN:
701                 case WLAN_EID_ERP_INFO:
702                 case WLAN_EID_EXT_SUPP_RATES:
703                 case WLAN_EID_HT_CAPABILITY:
704                 case WLAN_EID_HT_OPERATION:
705                 case WLAN_EID_VHT_CAPABILITY:
706                 case WLAN_EID_VHT_OPERATION:
707                 case WLAN_EID_MESH_ID:
708                 case WLAN_EID_MESH_CONFIG:
709                 case WLAN_EID_PEER_MGMT:
710                 case WLAN_EID_PREQ:
711                 case WLAN_EID_PREP:
712                 case WLAN_EID_PERR:
713                 case WLAN_EID_RANN:
714                 case WLAN_EID_CHANNEL_SWITCH:
715                 case WLAN_EID_EXT_CHANSWITCH_ANN:
716                 case WLAN_EID_COUNTRY:
717                 case WLAN_EID_PWR_CONSTRAINT:
718                 case WLAN_EID_TIMEOUT_INTERVAL:
719                 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
720                         if (test_bit(id, seen_elems)) {
721                                 elems->parse_error = true;
722                                 left -= elen;
723                                 pos += elen;
724                                 continue;
725                         }
726                         break;
727                 }
728
729                 if (calc_crc && id < 64 && (filter & (1ULL << id)))
730                         crc = crc32_be(crc, pos - 2, elen + 2);
731
732                 elem_parse_failed = false;
733
734                 switch (id) {
735                 case WLAN_EID_SSID:
736                         elems->ssid = pos;
737                         elems->ssid_len = elen;
738                         break;
739                 case WLAN_EID_SUPP_RATES:
740                         elems->supp_rates = pos;
741                         elems->supp_rates_len = elen;
742                         break;
743                 case WLAN_EID_DS_PARAMS:
744                         if (elen >= 1)
745                                 elems->ds_params = pos;
746                         else
747                                 elem_parse_failed = true;
748                         break;
749                 case WLAN_EID_TIM:
750                         if (elen >= sizeof(struct ieee80211_tim_ie)) {
751                                 elems->tim = (void *)pos;
752                                 elems->tim_len = elen;
753                         } else
754                                 elem_parse_failed = true;
755                         break;
756                 case WLAN_EID_CHALLENGE:
757                         elems->challenge = pos;
758                         elems->challenge_len = elen;
759                         break;
760                 case WLAN_EID_VENDOR_SPECIFIC:
761                         if (elen >= 4 && pos[0] == 0x00 && pos[1] == 0x50 &&
762                             pos[2] == 0xf2) {
763                                 /* Microsoft OUI (00:50:F2) */
764
765                                 if (calc_crc)
766                                         crc = crc32_be(crc, pos - 2, elen + 2);
767
768                                 if (elen >= 5 && pos[3] == 2) {
769                                         /* OUI Type 2 - WMM IE */
770                                         if (pos[4] == 0) {
771                                                 elems->wmm_info = pos;
772                                                 elems->wmm_info_len = elen;
773                                         } else if (pos[4] == 1) {
774                                                 elems->wmm_param = pos;
775                                                 elems->wmm_param_len = elen;
776                                         }
777                                 }
778                         }
779                         break;
780                 case WLAN_EID_RSN:
781                         elems->rsn = pos;
782                         elems->rsn_len = elen;
783                         break;
784                 case WLAN_EID_ERP_INFO:
785                         if (elen >= 1)
786                                 elems->erp_info = pos;
787                         else
788                                 elem_parse_failed = true;
789                         break;
790                 case WLAN_EID_EXT_SUPP_RATES:
791                         elems->ext_supp_rates = pos;
792                         elems->ext_supp_rates_len = elen;
793                         break;
794                 case WLAN_EID_HT_CAPABILITY:
795                         if (elen >= sizeof(struct ieee80211_ht_cap))
796                                 elems->ht_cap_elem = (void *)pos;
797                         else
798                                 elem_parse_failed = true;
799                         break;
800                 case WLAN_EID_HT_OPERATION:
801                         if (elen >= sizeof(struct ieee80211_ht_operation))
802                                 elems->ht_operation = (void *)pos;
803                         else
804                                 elem_parse_failed = true;
805                         break;
806                 case WLAN_EID_VHT_CAPABILITY:
807                         if (elen >= sizeof(struct ieee80211_vht_cap))
808                                 elems->vht_cap_elem = (void *)pos;
809                         else
810                                 elem_parse_failed = true;
811                         break;
812                 case WLAN_EID_VHT_OPERATION:
813                         if (elen >= sizeof(struct ieee80211_vht_operation))
814                                 elems->vht_operation = (void *)pos;
815                         else
816                                 elem_parse_failed = true;
817                         break;
818                 case WLAN_EID_OPMODE_NOTIF:
819                         if (elen > 0)
820                                 elems->opmode_notif = pos;
821                         else
822                                 elem_parse_failed = true;
823                         break;
824                 case WLAN_EID_MESH_ID:
825                         elems->mesh_id = pos;
826                         elems->mesh_id_len = elen;
827                         break;
828                 case WLAN_EID_MESH_CONFIG:
829                         if (elen >= sizeof(struct ieee80211_meshconf_ie))
830                                 elems->mesh_config = (void *)pos;
831                         else
832                                 elem_parse_failed = true;
833                         break;
834                 case WLAN_EID_PEER_MGMT:
835                         elems->peering = pos;
836                         elems->peering_len = elen;
837                         break;
838                 case WLAN_EID_MESH_AWAKE_WINDOW:
839                         if (elen >= 2)
840                                 elems->awake_window = (void *)pos;
841                         break;
842                 case WLAN_EID_PREQ:
843                         elems->preq = pos;
844                         elems->preq_len = elen;
845                         break;
846                 case WLAN_EID_PREP:
847                         elems->prep = pos;
848                         elems->prep_len = elen;
849                         break;
850                 case WLAN_EID_PERR:
851                         elems->perr = pos;
852                         elems->perr_len = elen;
853                         break;
854                 case WLAN_EID_RANN:
855                         if (elen >= sizeof(struct ieee80211_rann_ie))
856                                 elems->rann = (void *)pos;
857                         else
858                                 elem_parse_failed = true;
859                         break;
860                 case WLAN_EID_CHANNEL_SWITCH:
861                         if (elen != sizeof(struct ieee80211_channel_sw_ie)) {
862                                 elem_parse_failed = true;
863                                 break;
864                         }
865                         elems->ch_switch_ie = (void *)pos;
866                         break;
867                 case WLAN_EID_EXT_CHANSWITCH_ANN:
868                         if (elen != sizeof(struct ieee80211_ext_chansw_ie)) {
869                                 elem_parse_failed = true;
870                                 break;
871                         }
872                         elems->ext_chansw_ie = (void *)pos;
873                         break;
874                 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
875                         if (elen != sizeof(struct ieee80211_sec_chan_offs_ie)) {
876                                 elem_parse_failed = true;
877                                 break;
878                         }
879                         elems->sec_chan_offs = (void *)pos;
880                         break;
881                 case WLAN_EID_COUNTRY:
882                         elems->country_elem = pos;
883                         elems->country_elem_len = elen;
884                         break;
885                 case WLAN_EID_PWR_CONSTRAINT:
886                         if (elen != 1) {
887                                 elem_parse_failed = true;
888                                 break;
889                         }
890                         elems->pwr_constr_elem = pos;
891                         break;
892                 case WLAN_EID_TIMEOUT_INTERVAL:
893                         if (elen >= sizeof(struct ieee80211_timeout_interval_ie))
894                                 elems->timeout_int = (void *)pos;
895                         else
896                                 elem_parse_failed = true;
897                         break;
898                 default:
899                         break;
900                 }
901
902                 if (elem_parse_failed)
903                         elems->parse_error = true;
904                 else
905                         __set_bit(id, seen_elems);
906
907                 left -= elen;
908                 pos += elen;
909         }
910
911         if (left != 0)
912                 elems->parse_error = true;
913
914         return crc;
915 }
916
917 void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
918                                bool bss_notify)
919 {
920         struct ieee80211_local *local = sdata->local;
921         struct ieee80211_tx_queue_params qparam;
922         struct ieee80211_chanctx_conf *chanctx_conf;
923         int ac;
924         bool use_11b, enable_qos;
925         int aCWmin, aCWmax;
926
927         if (!local->ops->conf_tx)
928                 return;
929
930         if (local->hw.queues < IEEE80211_NUM_ACS)
931                 return;
932
933         memset(&qparam, 0, sizeof(qparam));
934
935         rcu_read_lock();
936         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
937         use_11b = (chanctx_conf &&
938                    chanctx_conf->def.chan->band == IEEE80211_BAND_2GHZ) &&
939                  !(sdata->flags & IEEE80211_SDATA_OPERATING_GMODE);
940         rcu_read_unlock();
941
942         /*
943          * By default disable QoS in STA mode for old access points, which do
944          * not support 802.11e. New APs will provide proper queue parameters,
945          * that we will configure later.
946          */
947         enable_qos = (sdata->vif.type != NL80211_IFTYPE_STATION);
948
949         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
950                 /* Set defaults according to 802.11-2007 Table 7-37 */
951                 aCWmax = 1023;
952                 if (use_11b)
953                         aCWmin = 31;
954                 else
955                         aCWmin = 15;
956
957                 if (enable_qos) {
958                         switch (ac) {
959                         case IEEE80211_AC_BK:
960                                 qparam.cw_max = aCWmax;
961                                 qparam.cw_min = aCWmin;
962                                 qparam.txop = 0;
963                                 qparam.aifs = 7;
964                                 break;
965                         /* never happens but let's not leave undefined */
966                         default:
967                         case IEEE80211_AC_BE:
968                                 qparam.cw_max = aCWmax;
969                                 qparam.cw_min = aCWmin;
970                                 qparam.txop = 0;
971                                 qparam.aifs = 3;
972                                 break;
973                         case IEEE80211_AC_VI:
974                                 qparam.cw_max = aCWmin;
975                                 qparam.cw_min = (aCWmin + 1) / 2 - 1;
976                                 if (use_11b)
977                                         qparam.txop = 6016/32;
978                                 else
979                                         qparam.txop = 3008/32;
980                                 qparam.aifs = 2;
981                                 break;
982                         case IEEE80211_AC_VO:
983                                 qparam.cw_max = (aCWmin + 1) / 2 - 1;
984                                 qparam.cw_min = (aCWmin + 1) / 4 - 1;
985                                 if (use_11b)
986                                         qparam.txop = 3264/32;
987                                 else
988                                         qparam.txop = 1504/32;
989                                 qparam.aifs = 2;
990                                 break;
991                         }
992                 } else {
993                         /* Confiure old 802.11b/g medium access rules. */
994                         qparam.cw_max = aCWmax;
995                         qparam.cw_min = aCWmin;
996                         qparam.txop = 0;
997                         qparam.aifs = 2;
998                 }
999
1000                 qparam.uapsd = false;
1001
1002                 sdata->tx_conf[ac] = qparam;
1003                 drv_conf_tx(local, sdata, ac, &qparam);
1004         }
1005
1006         if (sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1007             sdata->vif.type != NL80211_IFTYPE_P2P_DEVICE) {
1008                 sdata->vif.bss_conf.qos = enable_qos;
1009                 if (bss_notify)
1010                         ieee80211_bss_info_change_notify(sdata,
1011                                                          BSS_CHANGED_QOS);
1012         }
1013 }
1014
1015 void ieee80211_sta_def_wmm_params(struct ieee80211_sub_if_data *sdata,
1016                                   const size_t supp_rates_len,
1017                                   const u8 *supp_rates)
1018 {
1019         struct ieee80211_chanctx_conf *chanctx_conf;
1020         int i, have_higher_than_11mbit = 0;
1021
1022         /* cf. IEEE 802.11 9.2.12 */
1023         for (i = 0; i < supp_rates_len; i++)
1024                 if ((supp_rates[i] & 0x7f) * 5 > 110)
1025                         have_higher_than_11mbit = 1;
1026
1027         rcu_read_lock();
1028         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1029
1030         if (chanctx_conf &&
1031             chanctx_conf->def.chan->band == IEEE80211_BAND_2GHZ &&
1032             have_higher_than_11mbit)
1033                 sdata->flags |= IEEE80211_SDATA_OPERATING_GMODE;
1034         else
1035                 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
1036         rcu_read_unlock();
1037
1038         ieee80211_set_wmm_default(sdata, true);
1039 }
1040
1041 u32 ieee80211_mandatory_rates(struct ieee80211_local *local,
1042                               enum ieee80211_band band)
1043 {
1044         struct ieee80211_supported_band *sband;
1045         struct ieee80211_rate *bitrates;
1046         u32 mandatory_rates;
1047         enum ieee80211_rate_flags mandatory_flag;
1048         int i;
1049
1050         sband = local->hw.wiphy->bands[band];
1051         if (WARN_ON(!sband))
1052                 return 1;
1053
1054         if (band == IEEE80211_BAND_2GHZ)
1055                 mandatory_flag = IEEE80211_RATE_MANDATORY_B;
1056         else
1057                 mandatory_flag = IEEE80211_RATE_MANDATORY_A;
1058
1059         bitrates = sband->bitrates;
1060         mandatory_rates = 0;
1061         for (i = 0; i < sband->n_bitrates; i++)
1062                 if (bitrates[i].flags & mandatory_flag)
1063                         mandatory_rates |= BIT(i);
1064         return mandatory_rates;
1065 }
1066
1067 void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
1068                          u16 transaction, u16 auth_alg, u16 status,
1069                          const u8 *extra, size_t extra_len, const u8 *da,
1070                          const u8 *bssid, const u8 *key, u8 key_len, u8 key_idx,
1071                          u32 tx_flags)
1072 {
1073         struct ieee80211_local *local = sdata->local;
1074         struct sk_buff *skb;
1075         struct ieee80211_mgmt *mgmt;
1076         int err;
1077
1078         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
1079                             sizeof(*mgmt) + 6 + extra_len);
1080         if (!skb)
1081                 return;
1082
1083         skb_reserve(skb, local->hw.extra_tx_headroom);
1084
1085         mgmt = (struct ieee80211_mgmt *) skb_put(skb, 24 + 6);
1086         memset(mgmt, 0, 24 + 6);
1087         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
1088                                           IEEE80211_STYPE_AUTH);
1089         memcpy(mgmt->da, da, ETH_ALEN);
1090         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1091         memcpy(mgmt->bssid, bssid, ETH_ALEN);
1092         mgmt->u.auth.auth_alg = cpu_to_le16(auth_alg);
1093         mgmt->u.auth.auth_transaction = cpu_to_le16(transaction);
1094         mgmt->u.auth.status_code = cpu_to_le16(status);
1095         if (extra)
1096                 memcpy(skb_put(skb, extra_len), extra, extra_len);
1097
1098         if (auth_alg == WLAN_AUTH_SHARED_KEY && transaction == 3) {
1099                 mgmt->frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
1100                 err = ieee80211_wep_encrypt(local, skb, key, key_len, key_idx);
1101                 WARN_ON(err);
1102         }
1103
1104         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1105                                         tx_flags;
1106         ieee80211_tx_skb(sdata, skb);
1107 }
1108
1109 void ieee80211_send_deauth_disassoc(struct ieee80211_sub_if_data *sdata,
1110                                     const u8 *bssid, u16 stype, u16 reason,
1111                                     bool send_frame, u8 *frame_buf)
1112 {
1113         struct ieee80211_local *local = sdata->local;
1114         struct sk_buff *skb;
1115         struct ieee80211_mgmt *mgmt = (void *)frame_buf;
1116
1117         /* build frame */
1118         mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1119         mgmt->duration = 0; /* initialize only */
1120         mgmt->seq_ctrl = 0; /* initialize only */
1121         memcpy(mgmt->da, bssid, ETH_ALEN);
1122         memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
1123         memcpy(mgmt->bssid, bssid, ETH_ALEN);
1124         /* u.deauth.reason_code == u.disassoc.reason_code */
1125         mgmt->u.deauth.reason_code = cpu_to_le16(reason);
1126
1127         if (send_frame) {
1128                 skb = dev_alloc_skb(local->hw.extra_tx_headroom +
1129                                     IEEE80211_DEAUTH_FRAME_LEN);
1130                 if (!skb)
1131                         return;
1132
1133                 skb_reserve(skb, local->hw.extra_tx_headroom);
1134
1135                 /* copy in frame */
1136                 memcpy(skb_put(skb, IEEE80211_DEAUTH_FRAME_LEN),
1137                        mgmt, IEEE80211_DEAUTH_FRAME_LEN);
1138
1139                 if (sdata->vif.type != NL80211_IFTYPE_STATION ||
1140                     !(sdata->u.mgd.flags & IEEE80211_STA_MFP_ENABLED))
1141                         IEEE80211_SKB_CB(skb)->flags |=
1142                                 IEEE80211_TX_INTFL_DONT_ENCRYPT;
1143
1144                 ieee80211_tx_skb(sdata, skb);
1145         }
1146 }
1147
1148 int ieee80211_build_preq_ies(struct ieee80211_local *local, u8 *buffer,
1149                              size_t buffer_len, const u8 *ie, size_t ie_len,
1150                              enum ieee80211_band band, u32 rate_mask,
1151                              u8 channel)
1152 {
1153         struct ieee80211_supported_band *sband;
1154         u8 *pos = buffer, *end = buffer + buffer_len;
1155         size_t offset = 0, noffset;
1156         int supp_rates_len, i;
1157         u8 rates[32];
1158         int num_rates;
1159         int ext_rates_len;
1160
1161         sband = local->hw.wiphy->bands[band];
1162         if (WARN_ON_ONCE(!sband))
1163                 return 0;
1164
1165         num_rates = 0;
1166         for (i = 0; i < sband->n_bitrates; i++) {
1167                 if ((BIT(i) & rate_mask) == 0)
1168                         continue; /* skip rate */
1169                 rates[num_rates++] = (u8) (sband->bitrates[i].bitrate / 5);
1170         }
1171
1172         supp_rates_len = min_t(int, num_rates, 8);
1173
1174         if (end - pos < 2 + supp_rates_len)
1175                 goto out_err;
1176         *pos++ = WLAN_EID_SUPP_RATES;
1177         *pos++ = supp_rates_len;
1178         memcpy(pos, rates, supp_rates_len);
1179         pos += supp_rates_len;
1180
1181         /* insert "request information" if in custom IEs */
1182         if (ie && ie_len) {
1183                 static const u8 before_extrates[] = {
1184                         WLAN_EID_SSID,
1185                         WLAN_EID_SUPP_RATES,
1186                         WLAN_EID_REQUEST,
1187                 };
1188                 noffset = ieee80211_ie_split(ie, ie_len,
1189                                              before_extrates,
1190                                              ARRAY_SIZE(before_extrates),
1191                                              offset);
1192                 if (end - pos < noffset - offset)
1193                         goto out_err;
1194                 memcpy(pos, ie + offset, noffset - offset);
1195                 pos += noffset - offset;
1196                 offset = noffset;
1197         }
1198
1199         ext_rates_len = num_rates - supp_rates_len;
1200         if (ext_rates_len > 0) {
1201                 if (end - pos < 2 + ext_rates_len)
1202                         goto out_err;
1203                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1204                 *pos++ = ext_rates_len;
1205                 memcpy(pos, rates + supp_rates_len, ext_rates_len);
1206                 pos += ext_rates_len;
1207         }
1208
1209         if (channel && sband->band == IEEE80211_BAND_2GHZ) {
1210                 if (end - pos < 3)
1211                         goto out_err;
1212                 *pos++ = WLAN_EID_DS_PARAMS;
1213                 *pos++ = 1;
1214                 *pos++ = channel;
1215         }
1216
1217         /* insert custom IEs that go before HT */
1218         if (ie && ie_len) {
1219                 static const u8 before_ht[] = {
1220                         WLAN_EID_SSID,
1221                         WLAN_EID_SUPP_RATES,
1222                         WLAN_EID_REQUEST,
1223                         WLAN_EID_EXT_SUPP_RATES,
1224                         WLAN_EID_DS_PARAMS,
1225                         WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
1226                 };
1227                 noffset = ieee80211_ie_split(ie, ie_len,
1228                                              before_ht, ARRAY_SIZE(before_ht),
1229                                              offset);
1230                 if (end - pos < noffset - offset)
1231                         goto out_err;
1232                 memcpy(pos, ie + offset, noffset - offset);
1233                 pos += noffset - offset;
1234                 offset = noffset;
1235         }
1236
1237         if (sband->ht_cap.ht_supported) {
1238                 if (end - pos < 2 + sizeof(struct ieee80211_ht_cap))
1239                         goto out_err;
1240                 pos = ieee80211_ie_build_ht_cap(pos, &sband->ht_cap,
1241                                                 sband->ht_cap.cap);
1242         }
1243
1244         /*
1245          * If adding more here, adjust code in main.c
1246          * that calculates local->scan_ies_len.
1247          */
1248
1249         /* add any remaining custom IEs */
1250         if (ie && ie_len) {
1251                 noffset = ie_len;
1252                 if (end - pos < noffset - offset)
1253                         goto out_err;
1254                 memcpy(pos, ie + offset, noffset - offset);
1255                 pos += noffset - offset;
1256         }
1257
1258         if (sband->vht_cap.vht_supported) {
1259                 if (end - pos < 2 + sizeof(struct ieee80211_vht_cap))
1260                         goto out_err;
1261                 pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
1262                                                  sband->vht_cap.cap);
1263         }
1264
1265         return pos - buffer;
1266  out_err:
1267         WARN_ONCE(1, "not enough space for preq IEs\n");
1268         return pos - buffer;
1269 }
1270
1271 struct sk_buff *ieee80211_build_probe_req(struct ieee80211_sub_if_data *sdata,
1272                                           u8 *dst, u32 ratemask,
1273                                           struct ieee80211_channel *chan,
1274                                           const u8 *ssid, size_t ssid_len,
1275                                           const u8 *ie, size_t ie_len,
1276                                           bool directed)
1277 {
1278         struct ieee80211_local *local = sdata->local;
1279         struct sk_buff *skb;
1280         struct ieee80211_mgmt *mgmt;
1281         u8 chan_no;
1282         int ies_len;
1283
1284         /*
1285          * Do not send DS Channel parameter for directed probe requests
1286          * in order to maximize the chance that we get a response.  Some
1287          * badly-behaved APs don't respond when this parameter is included.
1288          */
1289         if (directed)
1290                 chan_no = 0;
1291         else
1292                 chan_no = ieee80211_frequency_to_channel(chan->center_freq);
1293
1294         skb = ieee80211_probereq_get(&local->hw, &sdata->vif,
1295                                      ssid, ssid_len, 100 + ie_len);
1296         if (!skb)
1297                 return NULL;
1298
1299         ies_len = ieee80211_build_preq_ies(local, skb_tail_pointer(skb),
1300                                            skb_tailroom(skb),
1301                                            ie, ie_len, chan->band,
1302                                            ratemask, chan_no);
1303         skb_put(skb, ies_len);
1304
1305         if (dst) {
1306                 mgmt = (struct ieee80211_mgmt *) skb->data;
1307                 memcpy(mgmt->da, dst, ETH_ALEN);
1308                 memcpy(mgmt->bssid, dst, ETH_ALEN);
1309         }
1310
1311         IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1312
1313         return skb;
1314 }
1315
1316 void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
1317                               const u8 *ssid, size_t ssid_len,
1318                               const u8 *ie, size_t ie_len,
1319                               u32 ratemask, bool directed, u32 tx_flags,
1320                               struct ieee80211_channel *channel, bool scan)
1321 {
1322         struct sk_buff *skb;
1323
1324         skb = ieee80211_build_probe_req(sdata, dst, ratemask, channel,
1325                                         ssid, ssid_len,
1326                                         ie, ie_len, directed);
1327         if (skb) {
1328                 IEEE80211_SKB_CB(skb)->flags |= tx_flags;
1329                 if (scan)
1330                         ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
1331                 else
1332                         ieee80211_tx_skb(sdata, skb);
1333         }
1334 }
1335
1336 u32 ieee80211_sta_get_rates(struct ieee80211_local *local,
1337                             struct ieee802_11_elems *elems,
1338                             enum ieee80211_band band, u32 *basic_rates)
1339 {
1340         struct ieee80211_supported_band *sband;
1341         struct ieee80211_rate *bitrates;
1342         size_t num_rates;
1343         u32 supp_rates;
1344         int i, j;
1345         sband = local->hw.wiphy->bands[band];
1346
1347         if (WARN_ON(!sband))
1348                 return 1;
1349
1350         bitrates = sband->bitrates;
1351         num_rates = sband->n_bitrates;
1352         supp_rates = 0;
1353         for (i = 0; i < elems->supp_rates_len +
1354                      elems->ext_supp_rates_len; i++) {
1355                 u8 rate = 0;
1356                 int own_rate;
1357                 bool is_basic;
1358                 if (i < elems->supp_rates_len)
1359                         rate = elems->supp_rates[i];
1360                 else if (elems->ext_supp_rates)
1361                         rate = elems->ext_supp_rates
1362                                 [i - elems->supp_rates_len];
1363                 own_rate = 5 * (rate & 0x7f);
1364                 is_basic = !!(rate & 0x80);
1365
1366                 if (is_basic && (rate & 0x7f) == BSS_MEMBERSHIP_SELECTOR_HT_PHY)
1367                         continue;
1368
1369                 for (j = 0; j < num_rates; j++) {
1370                         if (bitrates[j].bitrate == own_rate) {
1371                                 supp_rates |= BIT(j);
1372                                 if (basic_rates && is_basic)
1373                                         *basic_rates |= BIT(j);
1374                         }
1375                 }
1376         }
1377         return supp_rates;
1378 }
1379
1380 void ieee80211_stop_device(struct ieee80211_local *local)
1381 {
1382         ieee80211_led_radio(local, false);
1383         ieee80211_mod_tpt_led_trig(local, 0, IEEE80211_TPT_LEDTRIG_FL_RADIO);
1384
1385         cancel_work_sync(&local->reconfig_filter);
1386
1387         flush_workqueue(local->workqueue);
1388         drv_stop(local);
1389 }
1390
1391 static void ieee80211_assign_chanctx(struct ieee80211_local *local,
1392                                      struct ieee80211_sub_if_data *sdata)
1393 {
1394         struct ieee80211_chanctx_conf *conf;
1395         struct ieee80211_chanctx *ctx;
1396
1397         if (!local->use_chanctx)
1398                 return;
1399
1400         mutex_lock(&local->chanctx_mtx);
1401         conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1402                                          lockdep_is_held(&local->chanctx_mtx));
1403         if (conf) {
1404                 ctx = container_of(conf, struct ieee80211_chanctx, conf);
1405                 drv_assign_vif_chanctx(local, sdata, ctx);
1406         }
1407         mutex_unlock(&local->chanctx_mtx);
1408 }
1409
1410 int ieee80211_reconfig(struct ieee80211_local *local)
1411 {
1412         struct ieee80211_hw *hw = &local->hw;
1413         struct ieee80211_sub_if_data *sdata;
1414         struct ieee80211_chanctx *ctx;
1415         struct sta_info *sta;
1416         int res, i;
1417         bool reconfig_due_to_wowlan = false;
1418
1419 #ifdef CONFIG_PM
1420         if (local->suspended)
1421                 local->resuming = true;
1422
1423         if (local->wowlan) {
1424                 local->wowlan = false;
1425                 res = drv_resume(local);
1426                 if (res < 0) {
1427                         local->resuming = false;
1428                         return res;
1429                 }
1430                 if (res == 0)
1431                         goto wake_up;
1432                 WARN_ON(res > 1);
1433                 /*
1434                  * res is 1, which means the driver requested
1435                  * to go through a regular reset on wakeup.
1436                  */
1437                 reconfig_due_to_wowlan = true;
1438         }
1439 #endif
1440         /* everything else happens only if HW was up & running */
1441         if (!local->open_count)
1442                 goto wake_up;
1443
1444         /*
1445          * Upon resume hardware can sometimes be goofy due to
1446          * various platform / driver / bus issues, so restarting
1447          * the device may at times not work immediately. Propagate
1448          * the error.
1449          */
1450         res = drv_start(local);
1451         if (res) {
1452                 WARN(local->suspended, "Hardware became unavailable "
1453                      "upon resume. This could be a software issue "
1454                      "prior to suspend or a hardware issue.\n");
1455                 return res;
1456         }
1457
1458         /* setup fragmentation threshold */
1459         drv_set_frag_threshold(local, hw->wiphy->frag_threshold);
1460
1461         /* setup RTS threshold */
1462         drv_set_rts_threshold(local, hw->wiphy->rts_threshold);
1463
1464         /* reset coverage class */
1465         drv_set_coverage_class(local, hw->wiphy->coverage_class);
1466
1467         ieee80211_led_radio(local, true);
1468         ieee80211_mod_tpt_led_trig(local,
1469                                    IEEE80211_TPT_LEDTRIG_FL_RADIO, 0);
1470
1471         /* add interfaces */
1472         sdata = rtnl_dereference(local->monitor_sdata);
1473         if (sdata) {
1474                 /* in HW restart it exists already */
1475                 WARN_ON(local->resuming);
1476                 res = drv_add_interface(local, sdata);
1477                 if (WARN_ON(res)) {
1478                         rcu_assign_pointer(local->monitor_sdata, NULL);
1479                         synchronize_net();
1480                         kfree(sdata);
1481                 }
1482         }
1483
1484         list_for_each_entry(sdata, &local->interfaces, list) {
1485                 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1486                     sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1487                     ieee80211_sdata_running(sdata))
1488                         res = drv_add_interface(local, sdata);
1489         }
1490
1491         /* add channel contexts */
1492         if (local->use_chanctx) {
1493                 mutex_lock(&local->chanctx_mtx);
1494                 list_for_each_entry(ctx, &local->chanctx_list, list)
1495                         WARN_ON(drv_add_chanctx(local, ctx));
1496                 mutex_unlock(&local->chanctx_mtx);
1497         }
1498
1499         list_for_each_entry(sdata, &local->interfaces, list) {
1500                 if (!ieee80211_sdata_running(sdata))
1501                         continue;
1502                 ieee80211_assign_chanctx(local, sdata);
1503         }
1504
1505         sdata = rtnl_dereference(local->monitor_sdata);
1506         if (sdata && ieee80211_sdata_running(sdata))
1507                 ieee80211_assign_chanctx(local, sdata);
1508
1509         /* add STAs back */
1510         mutex_lock(&local->sta_mtx);
1511         list_for_each_entry(sta, &local->sta_list, list) {
1512                 enum ieee80211_sta_state state;
1513
1514                 if (!sta->uploaded)
1515                         continue;
1516
1517                 /* AP-mode stations will be added later */
1518                 if (sta->sdata->vif.type == NL80211_IFTYPE_AP)
1519                         continue;
1520
1521                 for (state = IEEE80211_STA_NOTEXIST;
1522                      state < sta->sta_state; state++)
1523                         WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
1524                                               state + 1));
1525         }
1526         mutex_unlock(&local->sta_mtx);
1527
1528         /* reconfigure tx conf */
1529         if (hw->queues >= IEEE80211_NUM_ACS) {
1530                 list_for_each_entry(sdata, &local->interfaces, list) {
1531                         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1532                             sdata->vif.type == NL80211_IFTYPE_MONITOR ||
1533                             !ieee80211_sdata_running(sdata))
1534                                 continue;
1535
1536                         for (i = 0; i < IEEE80211_NUM_ACS; i++)
1537                                 drv_conf_tx(local, sdata, i,
1538                                             &sdata->tx_conf[i]);
1539                 }
1540         }
1541
1542         /* reconfigure hardware */
1543         ieee80211_hw_config(local, ~0);
1544
1545         ieee80211_configure_filter(local);
1546
1547         /* Finally also reconfigure all the BSS information */
1548         list_for_each_entry(sdata, &local->interfaces, list) {
1549                 u32 changed;
1550
1551                 if (!ieee80211_sdata_running(sdata))
1552                         continue;
1553
1554                 /* common change flags for all interface types */
1555                 changed = BSS_CHANGED_ERP_CTS_PROT |
1556                           BSS_CHANGED_ERP_PREAMBLE |
1557                           BSS_CHANGED_ERP_SLOT |
1558                           BSS_CHANGED_HT |
1559                           BSS_CHANGED_BASIC_RATES |
1560                           BSS_CHANGED_BEACON_INT |
1561                           BSS_CHANGED_BSSID |
1562                           BSS_CHANGED_CQM |
1563                           BSS_CHANGED_QOS |
1564                           BSS_CHANGED_IDLE |
1565                           BSS_CHANGED_TXPOWER;
1566
1567                 switch (sdata->vif.type) {
1568                 case NL80211_IFTYPE_STATION:
1569                         changed |= BSS_CHANGED_ASSOC |
1570                                    BSS_CHANGED_ARP_FILTER |
1571                                    BSS_CHANGED_PS;
1572
1573                         if (sdata->u.mgd.dtim_period)
1574                                 changed |= BSS_CHANGED_DTIM_PERIOD;
1575
1576                         mutex_lock(&sdata->u.mgd.mtx);
1577                         ieee80211_bss_info_change_notify(sdata, changed);
1578                         mutex_unlock(&sdata->u.mgd.mtx);
1579                         break;
1580                 case NL80211_IFTYPE_ADHOC:
1581                         changed |= BSS_CHANGED_IBSS;
1582                         /* fall through */
1583                 case NL80211_IFTYPE_AP:
1584                         changed |= BSS_CHANGED_SSID | BSS_CHANGED_P2P_PS;
1585
1586                         if (sdata->vif.type == NL80211_IFTYPE_AP) {
1587                                 changed |= BSS_CHANGED_AP_PROBE_RESP;
1588
1589                                 if (rcu_access_pointer(sdata->u.ap.beacon))
1590                                         drv_start_ap(local, sdata);
1591                         }
1592
1593                         /* fall through */
1594                 case NL80211_IFTYPE_MESH_POINT:
1595                         if (sdata->vif.bss_conf.enable_beacon) {
1596                                 changed |= BSS_CHANGED_BEACON |
1597                                            BSS_CHANGED_BEACON_ENABLED;
1598                                 ieee80211_bss_info_change_notify(sdata, changed);
1599                         }
1600                         break;
1601                 case NL80211_IFTYPE_WDS:
1602                         break;
1603                 case NL80211_IFTYPE_AP_VLAN:
1604                 case NL80211_IFTYPE_MONITOR:
1605                         /* ignore virtual */
1606                         break;
1607                 case NL80211_IFTYPE_P2P_DEVICE:
1608                         changed = BSS_CHANGED_IDLE;
1609                         break;
1610                 case NL80211_IFTYPE_UNSPECIFIED:
1611                 case NUM_NL80211_IFTYPES:
1612                 case NL80211_IFTYPE_P2P_CLIENT:
1613                 case NL80211_IFTYPE_P2P_GO:
1614                         WARN_ON(1);
1615                         break;
1616                 }
1617         }
1618
1619         ieee80211_recalc_ps(local, -1);
1620
1621         /*
1622          * The sta might be in psm against the ap (e.g. because
1623          * this was the state before a hw restart), so we
1624          * explicitly send a null packet in order to make sure
1625          * it'll sync against the ap (and get out of psm).
1626          */
1627         if (!(local->hw.conf.flags & IEEE80211_CONF_PS)) {
1628                 list_for_each_entry(sdata, &local->interfaces, list) {
1629                         if (sdata->vif.type != NL80211_IFTYPE_STATION)
1630                                 continue;
1631                         if (!sdata->u.mgd.associated)
1632                                 continue;
1633
1634                         ieee80211_send_nullfunc(local, sdata, 0);
1635                 }
1636         }
1637
1638         /* APs are now beaconing, add back stations */
1639         mutex_lock(&local->sta_mtx);
1640         list_for_each_entry(sta, &local->sta_list, list) {
1641                 enum ieee80211_sta_state state;
1642
1643                 if (!sta->uploaded)
1644                         continue;
1645
1646                 if (sta->sdata->vif.type != NL80211_IFTYPE_AP)
1647                         continue;
1648
1649                 for (state = IEEE80211_STA_NOTEXIST;
1650                      state < sta->sta_state; state++)
1651                         WARN_ON(drv_sta_state(local, sta->sdata, sta, state,
1652                                               state + 1));
1653         }
1654         mutex_unlock(&local->sta_mtx);
1655
1656         /* add back keys */
1657         list_for_each_entry(sdata, &local->interfaces, list)
1658                 if (ieee80211_sdata_running(sdata))
1659                         ieee80211_enable_keys(sdata);
1660
1661  wake_up:
1662         local->in_reconfig = false;
1663         barrier();
1664
1665         if (local->monitors == local->open_count && local->monitors > 0)
1666                 ieee80211_add_virtual_monitor(local);
1667
1668         /*
1669          * Clear the WLAN_STA_BLOCK_BA flag so new aggregation
1670          * sessions can be established after a resume.
1671          *
1672          * Also tear down aggregation sessions since reconfiguring
1673          * them in a hardware restart scenario is not easily done
1674          * right now, and the hardware will have lost information
1675          * about the sessions, but we and the AP still think they
1676          * are active. This is really a workaround though.
1677          */
1678         if (hw->flags & IEEE80211_HW_AMPDU_AGGREGATION) {
1679                 mutex_lock(&local->sta_mtx);
1680
1681                 list_for_each_entry(sta, &local->sta_list, list) {
1682                         ieee80211_sta_tear_down_BA_sessions(
1683                                         sta, AGG_STOP_LOCAL_REQUEST);
1684                         clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
1685                 }
1686
1687                 mutex_unlock(&local->sta_mtx);
1688         }
1689
1690         ieee80211_wake_queues_by_reason(hw, IEEE80211_MAX_QUEUE_MAP,
1691                                         IEEE80211_QUEUE_STOP_REASON_SUSPEND);
1692
1693         /*
1694          * If this is for hw restart things are still running.
1695          * We may want to change that later, however.
1696          */
1697         if (!local->suspended || reconfig_due_to_wowlan)
1698                 drv_restart_complete(local);
1699
1700         if (!local->suspended)
1701                 return 0;
1702
1703 #ifdef CONFIG_PM
1704         /* first set suspended false, then resuming */
1705         local->suspended = false;
1706         mb();
1707         local->resuming = false;
1708
1709         mod_timer(&local->sta_cleanup, jiffies + 1);
1710 #else
1711         WARN_ON(1);
1712 #endif
1713         return 0;
1714 }
1715
1716 void ieee80211_resume_disconnect(struct ieee80211_vif *vif)
1717 {
1718         struct ieee80211_sub_if_data *sdata;
1719         struct ieee80211_local *local;
1720         struct ieee80211_key *key;
1721
1722         if (WARN_ON(!vif))
1723                 return;
1724
1725         sdata = vif_to_sdata(vif);
1726         local = sdata->local;
1727
1728         if (WARN_ON(!local->resuming))
1729                 return;
1730
1731         if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
1732                 return;
1733
1734         sdata->flags |= IEEE80211_SDATA_DISCONNECT_RESUME;
1735
1736         mutex_lock(&local->key_mtx);
1737         list_for_each_entry(key, &sdata->key_list, list)
1738                 key->flags |= KEY_FLAG_TAINTED;
1739         mutex_unlock(&local->key_mtx);
1740 }
1741 EXPORT_SYMBOL_GPL(ieee80211_resume_disconnect);
1742
1743 void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata)
1744 {
1745         struct ieee80211_local *local = sdata->local;
1746         struct ieee80211_chanctx_conf *chanctx_conf;
1747         struct ieee80211_chanctx *chanctx;
1748
1749         mutex_lock(&local->chanctx_mtx);
1750
1751         chanctx_conf = rcu_dereference_protected(sdata->vif.chanctx_conf,
1752                                         lockdep_is_held(&local->chanctx_mtx));
1753
1754         if (WARN_ON_ONCE(!chanctx_conf))
1755                 goto unlock;
1756
1757         chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
1758         ieee80211_recalc_smps_chanctx(local, chanctx);
1759  unlock:
1760         mutex_unlock(&local->chanctx_mtx);
1761 }
1762
1763 static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1764 {
1765         int i;
1766
1767         for (i = 0; i < n_ids; i++)
1768                 if (ids[i] == id)
1769                         return true;
1770         return false;
1771 }
1772
1773 /**
1774  * ieee80211_ie_split - split an IE buffer according to ordering
1775  *
1776  * @ies: the IE buffer
1777  * @ielen: the length of the IE buffer
1778  * @ids: an array with element IDs that are allowed before
1779  *      the split
1780  * @n_ids: the size of the element ID array
1781  * @offset: offset where to start splitting in the buffer
1782  *
1783  * This function splits an IE buffer by updating the @offset
1784  * variable to point to the location where the buffer should be
1785  * split.
1786  *
1787  * It assumes that the given IE buffer is well-formed, this
1788  * has to be guaranteed by the caller!
1789  *
1790  * It also assumes that the IEs in the buffer are ordered
1791  * correctly, if not the result of using this function will not
1792  * be ordered correctly either, i.e. it does no reordering.
1793  *
1794  * The function returns the offset where the next part of the
1795  * buffer starts, which may be @ielen if the entire (remainder)
1796  * of the buffer should be used.
1797  */
1798 size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
1799                           const u8 *ids, int n_ids, size_t offset)
1800 {
1801         size_t pos = offset;
1802
1803         while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos]))
1804                 pos += 2 + ies[pos + 1];
1805
1806         return pos;
1807 }
1808
1809 size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
1810 {
1811         size_t pos = offset;
1812
1813         while (pos < ielen && ies[pos] != WLAN_EID_VENDOR_SPECIFIC)
1814                 pos += 2 + ies[pos + 1];
1815
1816         return pos;
1817 }
1818
1819 static void _ieee80211_enable_rssi_reports(struct ieee80211_sub_if_data *sdata,
1820                                             int rssi_min_thold,
1821                                             int rssi_max_thold)
1822 {
1823         trace_api_enable_rssi_reports(sdata, rssi_min_thold, rssi_max_thold);
1824
1825         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_STATION))
1826                 return;
1827
1828         /*
1829          * Scale up threshold values before storing it, as the RSSI averaging
1830          * algorithm uses a scaled up value as well. Change this scaling
1831          * factor if the RSSI averaging algorithm changes.
1832          */
1833         sdata->u.mgd.rssi_min_thold = rssi_min_thold*16;
1834         sdata->u.mgd.rssi_max_thold = rssi_max_thold*16;
1835 }
1836
1837 void ieee80211_enable_rssi_reports(struct ieee80211_vif *vif,
1838                                     int rssi_min_thold,
1839                                     int rssi_max_thold)
1840 {
1841         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1842
1843         WARN_ON(rssi_min_thold == rssi_max_thold ||
1844                 rssi_min_thold > rssi_max_thold);
1845
1846         _ieee80211_enable_rssi_reports(sdata, rssi_min_thold,
1847                                        rssi_max_thold);
1848 }
1849 EXPORT_SYMBOL(ieee80211_enable_rssi_reports);
1850
1851 void ieee80211_disable_rssi_reports(struct ieee80211_vif *vif)
1852 {
1853         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1854
1855         _ieee80211_enable_rssi_reports(sdata, 0, 0);
1856 }
1857 EXPORT_SYMBOL(ieee80211_disable_rssi_reports);
1858
1859 u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
1860                               u16 cap)
1861 {
1862         __le16 tmp;
1863
1864         *pos++ = WLAN_EID_HT_CAPABILITY;
1865         *pos++ = sizeof(struct ieee80211_ht_cap);
1866         memset(pos, 0, sizeof(struct ieee80211_ht_cap));
1867
1868         /* capability flags */
1869         tmp = cpu_to_le16(cap);
1870         memcpy(pos, &tmp, sizeof(u16));
1871         pos += sizeof(u16);
1872
1873         /* AMPDU parameters */
1874         *pos++ = ht_cap->ampdu_factor |
1875                  (ht_cap->ampdu_density <<
1876                         IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT);
1877
1878         /* MCS set */
1879         memcpy(pos, &ht_cap->mcs, sizeof(ht_cap->mcs));
1880         pos += sizeof(ht_cap->mcs);
1881
1882         /* extended capabilities */
1883         pos += sizeof(__le16);
1884
1885         /* BF capabilities */
1886         pos += sizeof(__le32);
1887
1888         /* antenna selection */
1889         pos += sizeof(u8);
1890
1891         return pos;
1892 }
1893
1894 u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
1895                                u32 cap)
1896 {
1897         __le32 tmp;
1898
1899         *pos++ = WLAN_EID_VHT_CAPABILITY;
1900         *pos++ = sizeof(struct ieee80211_vht_cap);
1901         memset(pos, 0, sizeof(struct ieee80211_vht_cap));
1902
1903         /* capability flags */
1904         tmp = cpu_to_le32(cap);
1905         memcpy(pos, &tmp, sizeof(u32));
1906         pos += sizeof(u32);
1907
1908         /* VHT MCS set */
1909         memcpy(pos, &vht_cap->vht_mcs, sizeof(vht_cap->vht_mcs));
1910         pos += sizeof(vht_cap->vht_mcs);
1911
1912         return pos;
1913 }
1914
1915 u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
1916                                const struct cfg80211_chan_def *chandef,
1917                                u16 prot_mode)
1918 {
1919         struct ieee80211_ht_operation *ht_oper;
1920         /* Build HT Information */
1921         *pos++ = WLAN_EID_HT_OPERATION;
1922         *pos++ = sizeof(struct ieee80211_ht_operation);
1923         ht_oper = (struct ieee80211_ht_operation *)pos;
1924         ht_oper->primary_chan = ieee80211_frequency_to_channel(
1925                                         chandef->chan->center_freq);
1926         switch (chandef->width) {
1927         case NL80211_CHAN_WIDTH_160:
1928         case NL80211_CHAN_WIDTH_80P80:
1929         case NL80211_CHAN_WIDTH_80:
1930         case NL80211_CHAN_WIDTH_40:
1931                 if (chandef->center_freq1 > chandef->chan->center_freq)
1932                         ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
1933                 else
1934                         ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1935                 break;
1936         default:
1937                 ht_oper->ht_param = IEEE80211_HT_PARAM_CHA_SEC_NONE;
1938                 break;
1939         }
1940         if (ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
1941             chandef->width != NL80211_CHAN_WIDTH_20_NOHT &&
1942             chandef->width != NL80211_CHAN_WIDTH_20)
1943                 ht_oper->ht_param |= IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
1944
1945         ht_oper->operation_mode = cpu_to_le16(prot_mode);
1946         ht_oper->stbc_param = 0x0000;
1947
1948         /* It seems that Basic MCS set and Supported MCS set
1949            are identical for the first 10 bytes */
1950         memset(&ht_oper->basic_set, 0, 16);
1951         memcpy(&ht_oper->basic_set, &ht_cap->mcs, 10);
1952
1953         return pos + sizeof(struct ieee80211_ht_operation);
1954 }
1955
1956 void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan,
1957                                   const struct ieee80211_ht_operation *ht_oper,
1958                                   struct cfg80211_chan_def *chandef)
1959 {
1960         enum nl80211_channel_type channel_type;
1961
1962         if (!ht_oper) {
1963                 cfg80211_chandef_create(chandef, control_chan,
1964                                         NL80211_CHAN_NO_HT);
1965                 return;
1966         }
1967
1968         switch (ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
1969         case IEEE80211_HT_PARAM_CHA_SEC_NONE:
1970                 channel_type = NL80211_CHAN_HT20;
1971                 break;
1972         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1973                 channel_type = NL80211_CHAN_HT40PLUS;
1974                 break;
1975         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1976                 channel_type = NL80211_CHAN_HT40MINUS;
1977                 break;
1978         default:
1979                 channel_type = NL80211_CHAN_NO_HT;
1980         }
1981
1982         cfg80211_chandef_create(chandef, control_chan, channel_type);
1983 }
1984
1985 int ieee80211_add_srates_ie(struct ieee80211_sub_if_data *sdata,
1986                             struct sk_buff *skb, bool need_basic,
1987                             enum ieee80211_band band)
1988 {
1989         struct ieee80211_local *local = sdata->local;
1990         struct ieee80211_supported_band *sband;
1991         int rate;
1992         u8 i, rates, *pos;
1993         u32 basic_rates = sdata->vif.bss_conf.basic_rates;
1994
1995         sband = local->hw.wiphy->bands[band];
1996         rates = sband->n_bitrates;
1997         if (rates > 8)
1998                 rates = 8;
1999
2000         if (skb_tailroom(skb) < rates + 2)
2001                 return -ENOMEM;
2002
2003         pos = skb_put(skb, rates + 2);
2004         *pos++ = WLAN_EID_SUPP_RATES;
2005         *pos++ = rates;
2006         for (i = 0; i < rates; i++) {
2007                 u8 basic = 0;
2008                 if (need_basic && basic_rates & BIT(i))
2009                         basic = 0x80;
2010                 rate = sband->bitrates[i].bitrate;
2011                 *pos++ = basic | (u8) (rate / 5);
2012         }
2013
2014         return 0;
2015 }
2016
2017 int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
2018                                 struct sk_buff *skb, bool need_basic,
2019                                 enum ieee80211_band band)
2020 {
2021         struct ieee80211_local *local = sdata->local;
2022         struct ieee80211_supported_band *sband;
2023         int rate;
2024         u8 i, exrates, *pos;
2025         u32 basic_rates = sdata->vif.bss_conf.basic_rates;
2026
2027         sband = local->hw.wiphy->bands[band];
2028         exrates = sband->n_bitrates;
2029         if (exrates > 8)
2030                 exrates -= 8;
2031         else
2032                 exrates = 0;
2033
2034         if (skb_tailroom(skb) < exrates + 2)
2035                 return -ENOMEM;
2036
2037         if (exrates) {
2038                 pos = skb_put(skb, exrates + 2);
2039                 *pos++ = WLAN_EID_EXT_SUPP_RATES;
2040                 *pos++ = exrates;
2041                 for (i = 8; i < sband->n_bitrates; i++) {
2042                         u8 basic = 0;
2043                         if (need_basic && basic_rates & BIT(i))
2044                                 basic = 0x80;
2045                         rate = sband->bitrates[i].bitrate;
2046                         *pos++ = basic | (u8) (rate / 5);
2047                 }
2048         }
2049         return 0;
2050 }
2051
2052 int ieee80211_ave_rssi(struct ieee80211_vif *vif)
2053 {
2054         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2055         struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
2056
2057         if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION)) {
2058                 /* non-managed type inferfaces */
2059                 return 0;
2060         }
2061         return ifmgd->ave_beacon_signal / 16;
2062 }
2063 EXPORT_SYMBOL_GPL(ieee80211_ave_rssi);
2064
2065 u8 ieee80211_mcs_to_chains(const struct ieee80211_mcs_info *mcs)
2066 {
2067         if (!mcs)
2068                 return 1;
2069
2070         /* TODO: consider rx_highest */
2071
2072         if (mcs->rx_mask[3])
2073                 return 4;
2074         if (mcs->rx_mask[2])
2075                 return 3;
2076         if (mcs->rx_mask[1])
2077                 return 2;
2078         return 1;
2079 }
2080
2081 /**
2082  * ieee80211_calculate_rx_timestamp - calculate timestamp in frame
2083  * @local: mac80211 hw info struct
2084  * @status: RX status
2085  * @mpdu_len: total MPDU length (including FCS)
2086  * @mpdu_offset: offset into MPDU to calculate timestamp at
2087  *
2088  * This function calculates the RX timestamp at the given MPDU offset, taking
2089  * into account what the RX timestamp was. An offset of 0 will just normalize
2090  * the timestamp to TSF at beginning of MPDU reception.
2091  */
2092 u64 ieee80211_calculate_rx_timestamp(struct ieee80211_local *local,
2093                                      struct ieee80211_rx_status *status,
2094                                      unsigned int mpdu_len,
2095                                      unsigned int mpdu_offset)
2096 {
2097         u64 ts = status->mactime;
2098         struct rate_info ri;
2099         u16 rate;
2100
2101         if (WARN_ON(!ieee80211_have_rx_timestamp(status)))
2102                 return 0;
2103
2104         memset(&ri, 0, sizeof(ri));
2105
2106         /* Fill cfg80211 rate info */
2107         if (status->flag & RX_FLAG_HT) {
2108                 ri.mcs = status->rate_idx;
2109                 ri.flags |= RATE_INFO_FLAGS_MCS;
2110                 if (status->flag & RX_FLAG_40MHZ)
2111                         ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
2112                 if (status->flag & RX_FLAG_SHORT_GI)
2113                         ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
2114         } else if (status->flag & RX_FLAG_VHT) {
2115                 ri.flags |= RATE_INFO_FLAGS_VHT_MCS;
2116                 ri.mcs = status->rate_idx;
2117                 ri.nss = status->vht_nss;
2118                 if (status->flag & RX_FLAG_40MHZ)
2119                         ri.flags |= RATE_INFO_FLAGS_40_MHZ_WIDTH;
2120                 if (status->flag & RX_FLAG_80MHZ)
2121                         ri.flags |= RATE_INFO_FLAGS_80_MHZ_WIDTH;
2122                 if (status->flag & RX_FLAG_80P80MHZ)
2123                         ri.flags |= RATE_INFO_FLAGS_80P80_MHZ_WIDTH;
2124                 if (status->flag & RX_FLAG_160MHZ)
2125                         ri.flags |= RATE_INFO_FLAGS_160_MHZ_WIDTH;
2126                 if (status->flag & RX_FLAG_SHORT_GI)
2127                         ri.flags |= RATE_INFO_FLAGS_SHORT_GI;
2128         } else {
2129                 struct ieee80211_supported_band *sband;
2130
2131                 sband = local->hw.wiphy->bands[status->band];
2132                 ri.legacy = sband->bitrates[status->rate_idx].bitrate;
2133         }
2134
2135         rate = cfg80211_calculate_bitrate(&ri);
2136
2137         /* rewind from end of MPDU */
2138         if (status->flag & RX_FLAG_MACTIME_END)
2139                 ts -= mpdu_len * 8 * 10 / rate;
2140
2141         ts += mpdu_offset * 8 * 10 / rate;
2142
2143         return ts;
2144 }
2145
2146 void ieee80211_dfs_cac_cancel(struct ieee80211_local *local)
2147 {
2148         struct ieee80211_sub_if_data *sdata;
2149
2150         mutex_lock(&local->iflist_mtx);
2151         list_for_each_entry(sdata, &local->interfaces, list) {
2152                 cancel_delayed_work_sync(&sdata->dfs_cac_timer_work);
2153
2154                 if (sdata->wdev.cac_started) {
2155                         ieee80211_vif_release_channel(sdata);
2156                         cfg80211_cac_event(sdata->dev,
2157                                            NL80211_RADAR_CAC_ABORTED,
2158                                            GFP_KERNEL);
2159                 }
2160         }
2161         mutex_unlock(&local->iflist_mtx);
2162 }
2163
2164 void ieee80211_dfs_radar_detected_work(struct work_struct *work)
2165 {
2166         struct ieee80211_local *local =
2167                 container_of(work, struct ieee80211_local, radar_detected_work);
2168         struct cfg80211_chan_def chandef;
2169
2170         ieee80211_dfs_cac_cancel(local);
2171
2172         if (local->use_chanctx)
2173                 /* currently not handled */
2174                 WARN_ON(1);
2175         else {
2176                 chandef = local->hw.conf.chandef;
2177                 cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
2178         }
2179 }
2180
2181 void ieee80211_radar_detected(struct ieee80211_hw *hw)
2182 {
2183         struct ieee80211_local *local = hw_to_local(hw);
2184
2185         trace_api_radar_detected(local);
2186
2187         ieee80211_queue_work(hw, &local->radar_detected_work);
2188 }
2189 EXPORT_SYMBOL(ieee80211_radar_detected);