mac80211: implement fair queueing per txq
[cascardo/linux.git] / net / mac80211 / tx.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  * Copyright 2013-2014  Intel Mobile Communications GmbH
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  *
13  * Transmit and frame generation functions.
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/etherdevice.h>
20 #include <linux/bitmap.h>
21 #include <linux/rcupdate.h>
22 #include <linux/export.h>
23 #include <net/net_namespace.h>
24 #include <net/ieee80211_radiotap.h>
25 #include <net/cfg80211.h>
26 #include <net/mac80211.h>
27 #include <asm/unaligned.h>
28 #include <net/fq_impl.h>
29
30 #include "ieee80211_i.h"
31 #include "driver-ops.h"
32 #include "led.h"
33 #include "mesh.h"
34 #include "wep.h"
35 #include "wpa.h"
36 #include "wme.h"
37 #include "rate.h"
38
39 /* misc utils */
40
41 static inline void ieee80211_tx_stats(struct net_device *dev, u32 len)
42 {
43         struct pcpu_sw_netstats *tstats = this_cpu_ptr(dev->tstats);
44
45         u64_stats_update_begin(&tstats->syncp);
46         tstats->tx_packets++;
47         tstats->tx_bytes += len;
48         u64_stats_update_end(&tstats->syncp);
49 }
50
51 static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
52                                  struct sk_buff *skb, int group_addr,
53                                  int next_frag_len)
54 {
55         int rate, mrate, erp, dur, i, shift = 0;
56         struct ieee80211_rate *txrate;
57         struct ieee80211_local *local = tx->local;
58         struct ieee80211_supported_band *sband;
59         struct ieee80211_hdr *hdr;
60         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
61         struct ieee80211_chanctx_conf *chanctx_conf;
62         u32 rate_flags = 0;
63
64         rcu_read_lock();
65         chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
66         if (chanctx_conf) {
67                 shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
68                 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
69         }
70         rcu_read_unlock();
71
72         /* assume HW handles this */
73         if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
74                 return 0;
75
76         /* uh huh? */
77         if (WARN_ON_ONCE(tx->rate.idx < 0))
78                 return 0;
79
80         sband = local->hw.wiphy->bands[info->band];
81         txrate = &sband->bitrates[tx->rate.idx];
82
83         erp = txrate->flags & IEEE80211_RATE_ERP_G;
84
85         /*
86          * data and mgmt (except PS Poll):
87          * - during CFP: 32768
88          * - during contention period:
89          *   if addr1 is group address: 0
90          *   if more fragments = 0 and addr1 is individual address: time to
91          *      transmit one ACK plus SIFS
92          *   if more fragments = 1 and addr1 is individual address: time to
93          *      transmit next fragment plus 2 x ACK plus 3 x SIFS
94          *
95          * IEEE 802.11, 9.6:
96          * - control response frame (CTS or ACK) shall be transmitted using the
97          *   same rate as the immediately previous frame in the frame exchange
98          *   sequence, if this rate belongs to the PHY mandatory rates, or else
99          *   at the highest possible rate belonging to the PHY rates in the
100          *   BSSBasicRateSet
101          */
102         hdr = (struct ieee80211_hdr *)skb->data;
103         if (ieee80211_is_ctl(hdr->frame_control)) {
104                 /* TODO: These control frames are not currently sent by
105                  * mac80211, but should they be implemented, this function
106                  * needs to be updated to support duration field calculation.
107                  *
108                  * RTS: time needed to transmit pending data/mgmt frame plus
109                  *    one CTS frame plus one ACK frame plus 3 x SIFS
110                  * CTS: duration of immediately previous RTS minus time
111                  *    required to transmit CTS and its SIFS
112                  * ACK: 0 if immediately previous directed data/mgmt had
113                  *    more=0, with more=1 duration in ACK frame is duration
114                  *    from previous frame minus time needed to transmit ACK
115                  *    and its SIFS
116                  * PS Poll: BIT(15) | BIT(14) | aid
117                  */
118                 return 0;
119         }
120
121         /* data/mgmt */
122         if (0 /* FIX: data/mgmt during CFP */)
123                 return cpu_to_le16(32768);
124
125         if (group_addr) /* Group address as the destination - no ACK */
126                 return 0;
127
128         /* Individual destination address:
129          * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
130          * CTS and ACK frames shall be transmitted using the highest rate in
131          * basic rate set that is less than or equal to the rate of the
132          * immediately previous frame and that is using the same modulation
133          * (CCK or OFDM). If no basic rate set matches with these requirements,
134          * the highest mandatory rate of the PHY that is less than or equal to
135          * the rate of the previous frame is used.
136          * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
137          */
138         rate = -1;
139         /* use lowest available if everything fails */
140         mrate = sband->bitrates[0].bitrate;
141         for (i = 0; i < sband->n_bitrates; i++) {
142                 struct ieee80211_rate *r = &sband->bitrates[i];
143
144                 if (r->bitrate > txrate->bitrate)
145                         break;
146
147                 if ((rate_flags & r->flags) != rate_flags)
148                         continue;
149
150                 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
151                         rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
152
153                 switch (sband->band) {
154                 case NL80211_BAND_2GHZ: {
155                         u32 flag;
156                         if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
157                                 flag = IEEE80211_RATE_MANDATORY_G;
158                         else
159                                 flag = IEEE80211_RATE_MANDATORY_B;
160                         if (r->flags & flag)
161                                 mrate = r->bitrate;
162                         break;
163                 }
164                 case NL80211_BAND_5GHZ:
165                         if (r->flags & IEEE80211_RATE_MANDATORY_A)
166                                 mrate = r->bitrate;
167                         break;
168                 case NL80211_BAND_60GHZ:
169                         /* TODO, for now fall through */
170                 case NUM_NL80211_BANDS:
171                         WARN_ON(1);
172                         break;
173                 }
174         }
175         if (rate == -1) {
176                 /* No matching basic rate found; use highest suitable mandatory
177                  * PHY rate */
178                 rate = DIV_ROUND_UP(mrate, 1 << shift);
179         }
180
181         /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
182         if (ieee80211_is_data_qos(hdr->frame_control) &&
183             *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
184                 dur = 0;
185         else
186                 /* Time needed to transmit ACK
187                  * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
188                  * to closest integer */
189                 dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
190                                 tx->sdata->vif.bss_conf.use_short_preamble,
191                                 shift);
192
193         if (next_frag_len) {
194                 /* Frame is fragmented: duration increases with time needed to
195                  * transmit next fragment plus ACK and 2 x SIFS. */
196                 dur *= 2; /* ACK + SIFS */
197                 /* next fragment */
198                 dur += ieee80211_frame_duration(sband->band, next_frag_len,
199                                 txrate->bitrate, erp,
200                                 tx->sdata->vif.bss_conf.use_short_preamble,
201                                 shift);
202         }
203
204         return cpu_to_le16(dur);
205 }
206
207 /* tx handlers */
208 static ieee80211_tx_result debug_noinline
209 ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
210 {
211         struct ieee80211_local *local = tx->local;
212         struct ieee80211_if_managed *ifmgd;
213
214         /* driver doesn't support power save */
215         if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
216                 return TX_CONTINUE;
217
218         /* hardware does dynamic power save */
219         if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
220                 return TX_CONTINUE;
221
222         /* dynamic power save disabled */
223         if (local->hw.conf.dynamic_ps_timeout <= 0)
224                 return TX_CONTINUE;
225
226         /* we are scanning, don't enable power save */
227         if (local->scanning)
228                 return TX_CONTINUE;
229
230         if (!local->ps_sdata)
231                 return TX_CONTINUE;
232
233         /* No point if we're going to suspend */
234         if (local->quiescing)
235                 return TX_CONTINUE;
236
237         /* dynamic ps is supported only in managed mode */
238         if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
239                 return TX_CONTINUE;
240
241         ifmgd = &tx->sdata->u.mgd;
242
243         /*
244          * Don't wakeup from power save if u-apsd is enabled, voip ac has
245          * u-apsd enabled and the frame is in voip class. This effectively
246          * means that even if all access categories have u-apsd enabled, in
247          * practise u-apsd is only used with the voip ac. This is a
248          * workaround for the case when received voip class packets do not
249          * have correct qos tag for some reason, due the network or the
250          * peer application.
251          *
252          * Note: ifmgd->uapsd_queues access is racy here. If the value is
253          * changed via debugfs, user needs to reassociate manually to have
254          * everything in sync.
255          */
256         if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
257             (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
258             skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
259                 return TX_CONTINUE;
260
261         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
262                 ieee80211_stop_queues_by_reason(&local->hw,
263                                                 IEEE80211_MAX_QUEUE_MAP,
264                                                 IEEE80211_QUEUE_STOP_REASON_PS,
265                                                 false);
266                 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
267                 ieee80211_queue_work(&local->hw,
268                                      &local->dynamic_ps_disable_work);
269         }
270
271         /* Don't restart the timer if we're not disassociated */
272         if (!ifmgd->associated)
273                 return TX_CONTINUE;
274
275         mod_timer(&local->dynamic_ps_timer, jiffies +
276                   msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
277
278         return TX_CONTINUE;
279 }
280
281 static ieee80211_tx_result debug_noinline
282 ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
283 {
284
285         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
286         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
287         bool assoc = false;
288
289         if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
290                 return TX_CONTINUE;
291
292         if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
293             test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
294             !ieee80211_is_probe_req(hdr->frame_control) &&
295             !ieee80211_is_nullfunc(hdr->frame_control))
296                 /*
297                  * When software scanning only nullfunc frames (to notify
298                  * the sleep state to the AP) and probe requests (for the
299                  * active scan) are allowed, all other frames should not be
300                  * sent and we should not get here, but if we do
301                  * nonetheless, drop them to avoid sending them
302                  * off-channel. See the link below and
303                  * ieee80211_start_scan() for more.
304                  *
305                  * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
306                  */
307                 return TX_DROP;
308
309         if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
310                 return TX_CONTINUE;
311
312         if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
313                 return TX_CONTINUE;
314
315         if (tx->flags & IEEE80211_TX_PS_BUFFERED)
316                 return TX_CONTINUE;
317
318         if (tx->sta)
319                 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
320
321         if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
322                 if (unlikely(!assoc &&
323                              ieee80211_is_data(hdr->frame_control))) {
324 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
325                         sdata_info(tx->sdata,
326                                    "dropped data frame to not associated station %pM\n",
327                                    hdr->addr1);
328 #endif
329                         I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
330                         return TX_DROP;
331                 }
332         } else if (unlikely(tx->sdata->vif.type == NL80211_IFTYPE_AP &&
333                             ieee80211_is_data(hdr->frame_control) &&
334                             !atomic_read(&tx->sdata->u.ap.num_mcast_sta))) {
335                 /*
336                  * No associated STAs - no need to send multicast
337                  * frames.
338                  */
339                 return TX_DROP;
340         }
341
342         return TX_CONTINUE;
343 }
344
345 /* This function is called whenever the AP is about to exceed the maximum limit
346  * of buffered frames for power saving STAs. This situation should not really
347  * happen often during normal operation, so dropping the oldest buffered packet
348  * from each queue should be OK to make some room for new frames. */
349 static void purge_old_ps_buffers(struct ieee80211_local *local)
350 {
351         int total = 0, purged = 0;
352         struct sk_buff *skb;
353         struct ieee80211_sub_if_data *sdata;
354         struct sta_info *sta;
355
356         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
357                 struct ps_data *ps;
358
359                 if (sdata->vif.type == NL80211_IFTYPE_AP)
360                         ps = &sdata->u.ap.ps;
361                 else if (ieee80211_vif_is_mesh(&sdata->vif))
362                         ps = &sdata->u.mesh.ps;
363                 else
364                         continue;
365
366                 skb = skb_dequeue(&ps->bc_buf);
367                 if (skb) {
368                         purged++;
369                         dev_kfree_skb(skb);
370                 }
371                 total += skb_queue_len(&ps->bc_buf);
372         }
373
374         /*
375          * Drop one frame from each station from the lowest-priority
376          * AC that has frames at all.
377          */
378         list_for_each_entry_rcu(sta, &local->sta_list, list) {
379                 int ac;
380
381                 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
382                         skb = skb_dequeue(&sta->ps_tx_buf[ac]);
383                         total += skb_queue_len(&sta->ps_tx_buf[ac]);
384                         if (skb) {
385                                 purged++;
386                                 ieee80211_free_txskb(&local->hw, skb);
387                                 break;
388                         }
389                 }
390         }
391
392         local->total_ps_buffered = total;
393         ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
394 }
395
396 static ieee80211_tx_result
397 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
398 {
399         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
400         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
401         struct ps_data *ps;
402
403         /*
404          * broadcast/multicast frame
405          *
406          * If any of the associated/peer stations is in power save mode,
407          * the frame is buffered to be sent after DTIM beacon frame.
408          * This is done either by the hardware or us.
409          */
410
411         /* powersaving STAs currently only in AP/VLAN/mesh mode */
412         if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
413             tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
414                 if (!tx->sdata->bss)
415                         return TX_CONTINUE;
416
417                 ps = &tx->sdata->bss->ps;
418         } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
419                 ps = &tx->sdata->u.mesh.ps;
420         } else {
421                 return TX_CONTINUE;
422         }
423
424
425         /* no buffering for ordered frames */
426         if (ieee80211_has_order(hdr->frame_control))
427                 return TX_CONTINUE;
428
429         if (ieee80211_is_probe_req(hdr->frame_control))
430                 return TX_CONTINUE;
431
432         if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL))
433                 info->hw_queue = tx->sdata->vif.cab_queue;
434
435         /* no stations in PS mode */
436         if (!atomic_read(&ps->num_sta_ps))
437                 return TX_CONTINUE;
438
439         info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
440
441         /* device releases frame after DTIM beacon */
442         if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING))
443                 return TX_CONTINUE;
444
445         /* buffered in mac80211 */
446         if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
447                 purge_old_ps_buffers(tx->local);
448
449         if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
450                 ps_dbg(tx->sdata,
451                        "BC TX buffer full - dropping the oldest frame\n");
452                 dev_kfree_skb(skb_dequeue(&ps->bc_buf));
453         } else
454                 tx->local->total_ps_buffered++;
455
456         skb_queue_tail(&ps->bc_buf, tx->skb);
457
458         return TX_QUEUED;
459 }
460
461 static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
462                              struct sk_buff *skb)
463 {
464         if (!ieee80211_is_mgmt(fc))
465                 return 0;
466
467         if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
468                 return 0;
469
470         if (!ieee80211_is_robust_mgmt_frame(skb))
471                 return 0;
472
473         return 1;
474 }
475
476 static ieee80211_tx_result
477 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
478 {
479         struct sta_info *sta = tx->sta;
480         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
481         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
482         struct ieee80211_local *local = tx->local;
483
484         if (unlikely(!sta))
485                 return TX_CONTINUE;
486
487         if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
488                       test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
489                       test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
490                      !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
491                 int ac = skb_get_queue_mapping(tx->skb);
492
493                 if (ieee80211_is_mgmt(hdr->frame_control) &&
494                     !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
495                         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
496                         return TX_CONTINUE;
497                 }
498
499                 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
500                        sta->sta.addr, sta->sta.aid, ac);
501                 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
502                         purge_old_ps_buffers(tx->local);
503
504                 /* sync with ieee80211_sta_ps_deliver_wakeup */
505                 spin_lock(&sta->ps_lock);
506                 /*
507                  * STA woke up the meantime and all the frames on ps_tx_buf have
508                  * been queued to pending queue. No reordering can happen, go
509                  * ahead and Tx the packet.
510                  */
511                 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
512                     !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
513                     !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
514                         spin_unlock(&sta->ps_lock);
515                         return TX_CONTINUE;
516                 }
517
518                 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
519                         struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
520                         ps_dbg(tx->sdata,
521                                "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
522                                sta->sta.addr, ac);
523                         ieee80211_free_txskb(&local->hw, old);
524                 } else
525                         tx->local->total_ps_buffered++;
526
527                 info->control.jiffies = jiffies;
528                 info->control.vif = &tx->sdata->vif;
529                 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
530                 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
531                 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
532                 spin_unlock(&sta->ps_lock);
533
534                 if (!timer_pending(&local->sta_cleanup))
535                         mod_timer(&local->sta_cleanup,
536                                   round_jiffies(jiffies +
537                                                 STA_INFO_CLEANUP_INTERVAL));
538
539                 /*
540                  * We queued up some frames, so the TIM bit might
541                  * need to be set, recalculate it.
542                  */
543                 sta_info_recalc_tim(sta);
544
545                 return TX_QUEUED;
546         } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
547                 ps_dbg(tx->sdata,
548                        "STA %pM in PS mode, but polling/in SP -> send frame\n",
549                        sta->sta.addr);
550         }
551
552         return TX_CONTINUE;
553 }
554
555 static ieee80211_tx_result debug_noinline
556 ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
557 {
558         if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
559                 return TX_CONTINUE;
560
561         if (tx->flags & IEEE80211_TX_UNICAST)
562                 return ieee80211_tx_h_unicast_ps_buf(tx);
563         else
564                 return ieee80211_tx_h_multicast_ps_buf(tx);
565 }
566
567 static ieee80211_tx_result debug_noinline
568 ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
569 {
570         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
571
572         if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
573                 if (tx->sdata->control_port_no_encrypt)
574                         info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
575                 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
576                 info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
577         }
578
579         return TX_CONTINUE;
580 }
581
582 static ieee80211_tx_result debug_noinline
583 ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
584 {
585         struct ieee80211_key *key;
586         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
587         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
588
589         if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
590                 tx->key = NULL;
591         else if (tx->sta &&
592                  (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
593                 tx->key = key;
594         else if (ieee80211_is_mgmt(hdr->frame_control) &&
595                  is_multicast_ether_addr(hdr->addr1) &&
596                  ieee80211_is_robust_mgmt_frame(tx->skb) &&
597                  (key = rcu_dereference(tx->sdata->default_mgmt_key)))
598                 tx->key = key;
599         else if (is_multicast_ether_addr(hdr->addr1) &&
600                  (key = rcu_dereference(tx->sdata->default_multicast_key)))
601                 tx->key = key;
602         else if (!is_multicast_ether_addr(hdr->addr1) &&
603                  (key = rcu_dereference(tx->sdata->default_unicast_key)))
604                 tx->key = key;
605         else
606                 tx->key = NULL;
607
608         if (tx->key) {
609                 bool skip_hw = false;
610
611                 /* TODO: add threshold stuff again */
612
613                 switch (tx->key->conf.cipher) {
614                 case WLAN_CIPHER_SUITE_WEP40:
615                 case WLAN_CIPHER_SUITE_WEP104:
616                 case WLAN_CIPHER_SUITE_TKIP:
617                         if (!ieee80211_is_data_present(hdr->frame_control))
618                                 tx->key = NULL;
619                         break;
620                 case WLAN_CIPHER_SUITE_CCMP:
621                 case WLAN_CIPHER_SUITE_CCMP_256:
622                 case WLAN_CIPHER_SUITE_GCMP:
623                 case WLAN_CIPHER_SUITE_GCMP_256:
624                         if (!ieee80211_is_data_present(hdr->frame_control) &&
625                             !ieee80211_use_mfp(hdr->frame_control, tx->sta,
626                                                tx->skb))
627                                 tx->key = NULL;
628                         else
629                                 skip_hw = (tx->key->conf.flags &
630                                            IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
631                                         ieee80211_is_mgmt(hdr->frame_control);
632                         break;
633                 case WLAN_CIPHER_SUITE_AES_CMAC:
634                 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
635                 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
636                 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
637                         if (!ieee80211_is_mgmt(hdr->frame_control))
638                                 tx->key = NULL;
639                         break;
640                 }
641
642                 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
643                              !ieee80211_is_deauth(hdr->frame_control)))
644                         return TX_DROP;
645
646                 if (!skip_hw && tx->key &&
647                     tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
648                         info->control.hw_key = &tx->key->conf;
649         }
650
651         return TX_CONTINUE;
652 }
653
654 static ieee80211_tx_result debug_noinline
655 ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
656 {
657         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
658         struct ieee80211_hdr *hdr = (void *)tx->skb->data;
659         struct ieee80211_supported_band *sband;
660         u32 len;
661         struct ieee80211_tx_rate_control txrc;
662         struct ieee80211_sta_rates *ratetbl = NULL;
663         bool assoc = false;
664
665         memset(&txrc, 0, sizeof(txrc));
666
667         sband = tx->local->hw.wiphy->bands[info->band];
668
669         len = min_t(u32, tx->skb->len + FCS_LEN,
670                          tx->local->hw.wiphy->frag_threshold);
671
672         /* set up the tx rate control struct we give the RC algo */
673         txrc.hw = &tx->local->hw;
674         txrc.sband = sband;
675         txrc.bss_conf = &tx->sdata->vif.bss_conf;
676         txrc.skb = tx->skb;
677         txrc.reported_rate.idx = -1;
678         txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
679         if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
680                 txrc.max_rate_idx = -1;
681         else
682                 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
683
684         if (tx->sdata->rc_has_mcs_mask[info->band])
685                 txrc.rate_idx_mcs_mask =
686                         tx->sdata->rc_rateidx_mcs_mask[info->band];
687
688         txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
689                     tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
690                     tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
691                     tx->sdata->vif.type == NL80211_IFTYPE_OCB);
692
693         /* set up RTS protection if desired */
694         if (len > tx->local->hw.wiphy->rts_threshold) {
695                 txrc.rts = true;
696         }
697
698         info->control.use_rts = txrc.rts;
699         info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
700
701         /*
702          * Use short preamble if the BSS can handle it, but not for
703          * management frames unless we know the receiver can handle
704          * that -- the management frame might be to a station that
705          * just wants a probe response.
706          */
707         if (tx->sdata->vif.bss_conf.use_short_preamble &&
708             (ieee80211_is_data(hdr->frame_control) ||
709              (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
710                 txrc.short_preamble = true;
711
712         info->control.short_preamble = txrc.short_preamble;
713
714         /* don't ask rate control when rate already injected via radiotap */
715         if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
716                 return TX_CONTINUE;
717
718         if (tx->sta)
719                 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
720
721         /*
722          * Lets not bother rate control if we're associated and cannot
723          * talk to the sta. This should not happen.
724          */
725         if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
726                  !rate_usable_index_exists(sband, &tx->sta->sta),
727                  "%s: Dropped data frame as no usable bitrate found while "
728                  "scanning and associated. Target station: "
729                  "%pM on %d GHz band\n",
730                  tx->sdata->name, hdr->addr1,
731                  info->band ? 5 : 2))
732                 return TX_DROP;
733
734         /*
735          * If we're associated with the sta at this point we know we can at
736          * least send the frame at the lowest bit rate.
737          */
738         rate_control_get_rate(tx->sdata, tx->sta, &txrc);
739
740         if (tx->sta && !info->control.skip_table)
741                 ratetbl = rcu_dereference(tx->sta->sta.rates);
742
743         if (unlikely(info->control.rates[0].idx < 0)) {
744                 if (ratetbl) {
745                         struct ieee80211_tx_rate rate = {
746                                 .idx = ratetbl->rate[0].idx,
747                                 .flags = ratetbl->rate[0].flags,
748                                 .count = ratetbl->rate[0].count
749                         };
750
751                         if (ratetbl->rate[0].idx < 0)
752                                 return TX_DROP;
753
754                         tx->rate = rate;
755                 } else {
756                         return TX_DROP;
757                 }
758         } else {
759                 tx->rate = info->control.rates[0];
760         }
761
762         if (txrc.reported_rate.idx < 0) {
763                 txrc.reported_rate = tx->rate;
764                 if (tx->sta && ieee80211_is_data(hdr->frame_control))
765                         tx->sta->tx_stats.last_rate = txrc.reported_rate;
766         } else if (tx->sta)
767                 tx->sta->tx_stats.last_rate = txrc.reported_rate;
768
769         if (ratetbl)
770                 return TX_CONTINUE;
771
772         if (unlikely(!info->control.rates[0].count))
773                 info->control.rates[0].count = 1;
774
775         if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
776                          (info->flags & IEEE80211_TX_CTL_NO_ACK)))
777                 info->control.rates[0].count = 1;
778
779         return TX_CONTINUE;
780 }
781
782 static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
783 {
784         u16 *seq = &sta->tid_seq[tid];
785         __le16 ret = cpu_to_le16(*seq);
786
787         /* Increase the sequence number. */
788         *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
789
790         return ret;
791 }
792
793 static ieee80211_tx_result debug_noinline
794 ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
795 {
796         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
797         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
798         u8 *qc;
799         int tid;
800
801         /*
802          * Packet injection may want to control the sequence
803          * number, if we have no matching interface then we
804          * neither assign one ourselves nor ask the driver to.
805          */
806         if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
807                 return TX_CONTINUE;
808
809         if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
810                 return TX_CONTINUE;
811
812         if (ieee80211_hdrlen(hdr->frame_control) < 24)
813                 return TX_CONTINUE;
814
815         if (ieee80211_is_qos_nullfunc(hdr->frame_control))
816                 return TX_CONTINUE;
817
818         /*
819          * Anything but QoS data that has a sequence number field
820          * (is long enough) gets a sequence number from the global
821          * counter.  QoS data frames with a multicast destination
822          * also use the global counter (802.11-2012 9.3.2.10).
823          */
824         if (!ieee80211_is_data_qos(hdr->frame_control) ||
825             is_multicast_ether_addr(hdr->addr1)) {
826                 /* driver should assign sequence number */
827                 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
828                 /* for pure STA mode without beacons, we can do it */
829                 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
830                 tx->sdata->sequence_number += 0x10;
831                 if (tx->sta)
832                         tx->sta->tx_stats.msdu[IEEE80211_NUM_TIDS]++;
833                 return TX_CONTINUE;
834         }
835
836         /*
837          * This should be true for injected/management frames only, for
838          * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
839          * above since they are not QoS-data frames.
840          */
841         if (!tx->sta)
842                 return TX_CONTINUE;
843
844         /* include per-STA, per-TID sequence counter */
845
846         qc = ieee80211_get_qos_ctl(hdr);
847         tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
848         tx->sta->tx_stats.msdu[tid]++;
849
850         if (!tx->sta->sta.txq[0])
851                 hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
852
853         return TX_CONTINUE;
854 }
855
856 static int ieee80211_fragment(struct ieee80211_tx_data *tx,
857                               struct sk_buff *skb, int hdrlen,
858                               int frag_threshold)
859 {
860         struct ieee80211_local *local = tx->local;
861         struct ieee80211_tx_info *info;
862         struct sk_buff *tmp;
863         int per_fragm = frag_threshold - hdrlen - FCS_LEN;
864         int pos = hdrlen + per_fragm;
865         int rem = skb->len - hdrlen - per_fragm;
866
867         if (WARN_ON(rem < 0))
868                 return -EINVAL;
869
870         /* first fragment was already added to queue by caller */
871
872         while (rem) {
873                 int fraglen = per_fragm;
874
875                 if (fraglen > rem)
876                         fraglen = rem;
877                 rem -= fraglen;
878                 tmp = dev_alloc_skb(local->tx_headroom +
879                                     frag_threshold +
880                                     tx->sdata->encrypt_headroom +
881                                     IEEE80211_ENCRYPT_TAILROOM);
882                 if (!tmp)
883                         return -ENOMEM;
884
885                 __skb_queue_tail(&tx->skbs, tmp);
886
887                 skb_reserve(tmp,
888                             local->tx_headroom + tx->sdata->encrypt_headroom);
889
890                 /* copy control information */
891                 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
892
893                 info = IEEE80211_SKB_CB(tmp);
894                 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
895                                  IEEE80211_TX_CTL_FIRST_FRAGMENT);
896
897                 if (rem)
898                         info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
899
900                 skb_copy_queue_mapping(tmp, skb);
901                 tmp->priority = skb->priority;
902                 tmp->dev = skb->dev;
903
904                 /* copy header and data */
905                 memcpy(skb_put(tmp, hdrlen), skb->data, hdrlen);
906                 memcpy(skb_put(tmp, fraglen), skb->data + pos, fraglen);
907
908                 pos += fraglen;
909         }
910
911         /* adjust first fragment's length */
912         skb_trim(skb, hdrlen + per_fragm);
913         return 0;
914 }
915
916 static ieee80211_tx_result debug_noinline
917 ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
918 {
919         struct sk_buff *skb = tx->skb;
920         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
921         struct ieee80211_hdr *hdr = (void *)skb->data;
922         int frag_threshold = tx->local->hw.wiphy->frag_threshold;
923         int hdrlen;
924         int fragnum;
925
926         /* no matter what happens, tx->skb moves to tx->skbs */
927         __skb_queue_tail(&tx->skbs, skb);
928         tx->skb = NULL;
929
930         if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
931                 return TX_CONTINUE;
932
933         if (tx->local->ops->set_frag_threshold)
934                 return TX_CONTINUE;
935
936         /*
937          * Warn when submitting a fragmented A-MPDU frame and drop it.
938          * This scenario is handled in ieee80211_tx_prepare but extra
939          * caution taken here as fragmented ampdu may cause Tx stop.
940          */
941         if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
942                 return TX_DROP;
943
944         hdrlen = ieee80211_hdrlen(hdr->frame_control);
945
946         /* internal error, why isn't DONTFRAG set? */
947         if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
948                 return TX_DROP;
949
950         /*
951          * Now fragment the frame. This will allocate all the fragments and
952          * chain them (using skb as the first fragment) to skb->next.
953          * During transmission, we will remove the successfully transmitted
954          * fragments from this list. When the low-level driver rejects one
955          * of the fragments then we will simply pretend to accept the skb
956          * but store it away as pending.
957          */
958         if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
959                 return TX_DROP;
960
961         /* update duration/seq/flags of fragments */
962         fragnum = 0;
963
964         skb_queue_walk(&tx->skbs, skb) {
965                 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
966
967                 hdr = (void *)skb->data;
968                 info = IEEE80211_SKB_CB(skb);
969
970                 if (!skb_queue_is_last(&tx->skbs, skb)) {
971                         hdr->frame_control |= morefrags;
972                         /*
973                          * No multi-rate retries for fragmented frames, that
974                          * would completely throw off the NAV at other STAs.
975                          */
976                         info->control.rates[1].idx = -1;
977                         info->control.rates[2].idx = -1;
978                         info->control.rates[3].idx = -1;
979                         BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
980                         info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
981                 } else {
982                         hdr->frame_control &= ~morefrags;
983                 }
984                 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
985                 fragnum++;
986         }
987
988         return TX_CONTINUE;
989 }
990
991 static ieee80211_tx_result debug_noinline
992 ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
993 {
994         struct sk_buff *skb;
995         int ac = -1;
996
997         if (!tx->sta)
998                 return TX_CONTINUE;
999
1000         skb_queue_walk(&tx->skbs, skb) {
1001                 ac = skb_get_queue_mapping(skb);
1002                 tx->sta->tx_stats.bytes[ac] += skb->len;
1003         }
1004         if (ac >= 0)
1005                 tx->sta->tx_stats.packets[ac]++;
1006
1007         return TX_CONTINUE;
1008 }
1009
1010 static ieee80211_tx_result debug_noinline
1011 ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1012 {
1013         if (!tx->key)
1014                 return TX_CONTINUE;
1015
1016         switch (tx->key->conf.cipher) {
1017         case WLAN_CIPHER_SUITE_WEP40:
1018         case WLAN_CIPHER_SUITE_WEP104:
1019                 return ieee80211_crypto_wep_encrypt(tx);
1020         case WLAN_CIPHER_SUITE_TKIP:
1021                 return ieee80211_crypto_tkip_encrypt(tx);
1022         case WLAN_CIPHER_SUITE_CCMP:
1023                 return ieee80211_crypto_ccmp_encrypt(
1024                         tx, IEEE80211_CCMP_MIC_LEN);
1025         case WLAN_CIPHER_SUITE_CCMP_256:
1026                 return ieee80211_crypto_ccmp_encrypt(
1027                         tx, IEEE80211_CCMP_256_MIC_LEN);
1028         case WLAN_CIPHER_SUITE_AES_CMAC:
1029                 return ieee80211_crypto_aes_cmac_encrypt(tx);
1030         case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1031                 return ieee80211_crypto_aes_cmac_256_encrypt(tx);
1032         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1033         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1034                 return ieee80211_crypto_aes_gmac_encrypt(tx);
1035         case WLAN_CIPHER_SUITE_GCMP:
1036         case WLAN_CIPHER_SUITE_GCMP_256:
1037                 return ieee80211_crypto_gcmp_encrypt(tx);
1038         default:
1039                 return ieee80211_crypto_hw_encrypt(tx);
1040         }
1041
1042         return TX_DROP;
1043 }
1044
1045 static ieee80211_tx_result debug_noinline
1046 ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1047 {
1048         struct sk_buff *skb;
1049         struct ieee80211_hdr *hdr;
1050         int next_len;
1051         bool group_addr;
1052
1053         skb_queue_walk(&tx->skbs, skb) {
1054                 hdr = (void *) skb->data;
1055                 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1056                         break; /* must not overwrite AID */
1057                 if (!skb_queue_is_last(&tx->skbs, skb)) {
1058                         struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1059                         next_len = next->len;
1060                 } else
1061                         next_len = 0;
1062                 group_addr = is_multicast_ether_addr(hdr->addr1);
1063
1064                 hdr->duration_id =
1065                         ieee80211_duration(tx, skb, group_addr, next_len);
1066         }
1067
1068         return TX_CONTINUE;
1069 }
1070
1071 /* actual transmit path */
1072
1073 static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1074                                   struct sk_buff *skb,
1075                                   struct ieee80211_tx_info *info,
1076                                   struct tid_ampdu_tx *tid_tx,
1077                                   int tid)
1078 {
1079         bool queued = false;
1080         bool reset_agg_timer = false;
1081         struct sk_buff *purge_skb = NULL;
1082
1083         if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1084                 info->flags |= IEEE80211_TX_CTL_AMPDU;
1085                 reset_agg_timer = true;
1086         } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1087                 /*
1088                  * nothing -- this aggregation session is being started
1089                  * but that might still fail with the driver
1090                  */
1091         } else if (!tx->sta->sta.txq[tid]) {
1092                 spin_lock(&tx->sta->lock);
1093                 /*
1094                  * Need to re-check now, because we may get here
1095                  *
1096                  *  1) in the window during which the setup is actually
1097                  *     already done, but not marked yet because not all
1098                  *     packets are spliced over to the driver pending
1099                  *     queue yet -- if this happened we acquire the lock
1100                  *     either before or after the splice happens, but
1101                  *     need to recheck which of these cases happened.
1102                  *
1103                  *  2) during session teardown, if the OPERATIONAL bit
1104                  *     was cleared due to the teardown but the pointer
1105                  *     hasn't been assigned NULL yet (or we loaded it
1106                  *     before it was assigned) -- in this case it may
1107                  *     now be NULL which means we should just let the
1108                  *     packet pass through because splicing the frames
1109                  *     back is already done.
1110                  */
1111                 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
1112
1113                 if (!tid_tx) {
1114                         /* do nothing, let packet pass through */
1115                 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1116                         info->flags |= IEEE80211_TX_CTL_AMPDU;
1117                         reset_agg_timer = true;
1118                 } else {
1119                         queued = true;
1120                         if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1121                                 clear_sta_flag(tx->sta, WLAN_STA_SP);
1122                                 ps_dbg(tx->sta->sdata,
1123                                        "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
1124                                        tx->sta->sta.addr, tx->sta->sta.aid);
1125                         }
1126                         info->control.vif = &tx->sdata->vif;
1127                         info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1128                         info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
1129                         __skb_queue_tail(&tid_tx->pending, skb);
1130                         if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1131                                 purge_skb = __skb_dequeue(&tid_tx->pending);
1132                 }
1133                 spin_unlock(&tx->sta->lock);
1134
1135                 if (purge_skb)
1136                         ieee80211_free_txskb(&tx->local->hw, purge_skb);
1137         }
1138
1139         /* reset session timer */
1140         if (reset_agg_timer && tid_tx->timeout)
1141                 tid_tx->last_tx = jiffies;
1142
1143         return queued;
1144 }
1145
1146 /*
1147  * initialises @tx
1148  * pass %NULL for the station if unknown, a valid pointer if known
1149  * or an ERR_PTR() if the station is known not to exist
1150  */
1151 static ieee80211_tx_result
1152 ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1153                      struct ieee80211_tx_data *tx,
1154                      struct sta_info *sta, struct sk_buff *skb)
1155 {
1156         struct ieee80211_local *local = sdata->local;
1157         struct ieee80211_hdr *hdr;
1158         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1159         int tid;
1160         u8 *qc;
1161
1162         memset(tx, 0, sizeof(*tx));
1163         tx->skb = skb;
1164         tx->local = local;
1165         tx->sdata = sdata;
1166         __skb_queue_head_init(&tx->skbs);
1167
1168         /*
1169          * If this flag is set to true anywhere, and we get here,
1170          * we are doing the needed processing, so remove the flag
1171          * now.
1172          */
1173         info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1174
1175         hdr = (struct ieee80211_hdr *) skb->data;
1176
1177         if (likely(sta)) {
1178                 if (!IS_ERR(sta))
1179                         tx->sta = sta;
1180         } else {
1181                 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1182                         tx->sta = rcu_dereference(sdata->u.vlan.sta);
1183                         if (!tx->sta && sdata->wdev.use_4addr)
1184                                 return TX_DROP;
1185                 } else if (info->flags & (IEEE80211_TX_INTFL_NL80211_FRAME_TX |
1186                                           IEEE80211_TX_CTL_INJECTED) ||
1187                            tx->sdata->control_port_protocol == tx->skb->protocol) {
1188                         tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1189                 }
1190                 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
1191                         tx->sta = sta_info_get(sdata, hdr->addr1);
1192         }
1193
1194         if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
1195             !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
1196             ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1197             !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
1198                 struct tid_ampdu_tx *tid_tx;
1199
1200                 qc = ieee80211_get_qos_ctl(hdr);
1201                 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
1202
1203                 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1204                 if (tid_tx) {
1205                         bool queued;
1206
1207                         queued = ieee80211_tx_prep_agg(tx, skb, info,
1208                                                        tid_tx, tid);
1209
1210                         if (unlikely(queued))
1211                                 return TX_QUEUED;
1212                 }
1213         }
1214
1215         if (is_multicast_ether_addr(hdr->addr1)) {
1216                 tx->flags &= ~IEEE80211_TX_UNICAST;
1217                 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1218         } else
1219                 tx->flags |= IEEE80211_TX_UNICAST;
1220
1221         if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1222                 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1223                     skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1224                     info->flags & IEEE80211_TX_CTL_AMPDU)
1225                         info->flags |= IEEE80211_TX_CTL_DONTFRAG;
1226         }
1227
1228         if (!tx->sta)
1229                 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1230         else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
1231                 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1232                 ieee80211_check_fast_xmit(tx->sta);
1233         }
1234
1235         info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
1236
1237         return TX_CONTINUE;
1238 }
1239
1240 static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
1241                                           struct ieee80211_vif *vif,
1242                                           struct ieee80211_sta *pubsta,
1243                                           struct sk_buff *skb)
1244 {
1245         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1246         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1247         struct ieee80211_txq *txq = NULL;
1248
1249         if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
1250             (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
1251                 return NULL;
1252
1253         if (!ieee80211_is_data(hdr->frame_control))
1254                 return NULL;
1255
1256         if (pubsta) {
1257                 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1258
1259                 txq = pubsta->txq[tid];
1260         } else if (vif) {
1261                 txq = vif->txq;
1262         }
1263
1264         if (!txq)
1265                 return NULL;
1266
1267         return to_txq_info(txq);
1268 }
1269
1270 static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1271                                            struct fq_tin *tin,
1272                                            struct fq_flow *flow)
1273 {
1274         return fq_flow_dequeue(fq, flow);
1275 }
1276
1277 static void fq_skb_free_func(struct fq *fq,
1278                              struct fq_tin *tin,
1279                              struct fq_flow *flow,
1280                              struct sk_buff *skb)
1281 {
1282         struct ieee80211_local *local;
1283
1284         local = container_of(fq, struct ieee80211_local, fq);
1285         ieee80211_free_txskb(&local->hw, skb);
1286 }
1287
1288 static struct fq_flow *fq_flow_get_default_func(struct fq *fq,
1289                                                 struct fq_tin *tin,
1290                                                 int idx,
1291                                                 struct sk_buff *skb)
1292 {
1293         struct txq_info *txqi;
1294
1295         txqi = container_of(tin, struct txq_info, tin);
1296         return &txqi->def_flow;
1297 }
1298
1299 static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1300                                   struct txq_info *txqi,
1301                                   struct sk_buff *skb)
1302 {
1303         struct fq *fq = &local->fq;
1304         struct fq_tin *tin = &txqi->tin;
1305
1306         fq_tin_enqueue(fq, tin, skb,
1307                        fq_skb_free_func,
1308                        fq_flow_get_default_func);
1309 }
1310
1311 void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1312                         struct sta_info *sta,
1313                         struct txq_info *txqi, int tid)
1314 {
1315         fq_tin_init(&txqi->tin);
1316         fq_flow_init(&txqi->def_flow);
1317
1318         txqi->txq.vif = &sdata->vif;
1319
1320         if (sta) {
1321                 txqi->txq.sta = &sta->sta;
1322                 sta->sta.txq[tid] = &txqi->txq;
1323                 txqi->txq.tid = tid;
1324                 txqi->txq.ac = ieee802_1d_to_ac[tid & 7];
1325         } else {
1326                 sdata->vif.txq = &txqi->txq;
1327                 txqi->txq.tid = 0;
1328                 txqi->txq.ac = IEEE80211_AC_BE;
1329         }
1330 }
1331
1332 void ieee80211_txq_purge(struct ieee80211_local *local,
1333                          struct txq_info *txqi)
1334 {
1335         struct fq *fq = &local->fq;
1336         struct fq_tin *tin = &txqi->tin;
1337
1338         fq_tin_reset(fq, tin, fq_skb_free_func);
1339 }
1340
1341 int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1342 {
1343         struct fq *fq = &local->fq;
1344         int ret;
1345
1346         if (!local->ops->wake_tx_queue)
1347                 return 0;
1348
1349         ret = fq_init(fq, 4096);
1350         if (ret)
1351                 return ret;
1352
1353         return 0;
1354 }
1355
1356 void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1357 {
1358         struct fq *fq = &local->fq;
1359
1360         if (!local->ops->wake_tx_queue)
1361                 return;
1362
1363         fq_reset(fq, fq_skb_free_func);
1364 }
1365
1366 struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
1367                                      struct ieee80211_txq *txq)
1368 {
1369         struct ieee80211_local *local = hw_to_local(hw);
1370         struct txq_info *txqi = container_of(txq, struct txq_info, txq);
1371         struct ieee80211_hdr *hdr;
1372         struct sk_buff *skb = NULL;
1373         struct fq *fq = &local->fq;
1374         struct fq_tin *tin = &txqi->tin;
1375
1376         spin_lock_bh(&fq->lock);
1377
1378         if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags))
1379                 goto out;
1380
1381         skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
1382         if (!skb)
1383                 goto out;
1384
1385         hdr = (struct ieee80211_hdr *)skb->data;
1386         if (txq->sta && ieee80211_is_data_qos(hdr->frame_control)) {
1387                 struct sta_info *sta = container_of(txq->sta, struct sta_info,
1388                                                     sta);
1389                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1390
1391                 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, txq->tid);
1392                 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
1393                         info->flags |= IEEE80211_TX_CTL_AMPDU;
1394                 else
1395                         info->flags &= ~IEEE80211_TX_CTL_AMPDU;
1396         }
1397
1398 out:
1399         spin_unlock_bh(&fq->lock);
1400
1401         if (skb && skb_has_frag_list(skb) &&
1402             !ieee80211_hw_check(&local->hw, TX_FRAG_LIST))
1403                 skb_linearize(skb);
1404
1405         return skb;
1406 }
1407 EXPORT_SYMBOL(ieee80211_tx_dequeue);
1408
1409 static bool ieee80211_tx_frags(struct ieee80211_local *local,
1410                                struct ieee80211_vif *vif,
1411                                struct ieee80211_sta *sta,
1412                                struct sk_buff_head *skbs,
1413                                bool txpending)
1414 {
1415         struct ieee80211_tx_control control = {};
1416         struct fq *fq = &local->fq;
1417         struct sk_buff *skb, *tmp;
1418         struct txq_info *txqi;
1419         unsigned long flags;
1420
1421         skb_queue_walk_safe(skbs, skb, tmp) {
1422                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1423                 int q = info->hw_queue;
1424
1425 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1426                 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1427                         __skb_unlink(skb, skbs);
1428                         ieee80211_free_txskb(&local->hw, skb);
1429                         continue;
1430                 }
1431 #endif
1432
1433                 txqi = ieee80211_get_txq(local, vif, sta, skb);
1434                 if (txqi) {
1435                         info->control.vif = vif;
1436
1437                         __skb_unlink(skb, skbs);
1438
1439                         spin_lock_bh(&fq->lock);
1440                         ieee80211_txq_enqueue(local, txqi, skb);
1441                         spin_unlock_bh(&fq->lock);
1442
1443                         drv_wake_tx_queue(local, txqi);
1444
1445                         continue;
1446                 }
1447
1448                 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1449                 if (local->queue_stop_reasons[q] ||
1450                     (!txpending && !skb_queue_empty(&local->pending[q]))) {
1451                         if (unlikely(info->flags &
1452                                      IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1453                                 if (local->queue_stop_reasons[q] &
1454                                     ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1455                                         /*
1456                                          * Drop off-channel frames if queues
1457                                          * are stopped for any reason other
1458                                          * than off-channel operation. Never
1459                                          * queue them.
1460                                          */
1461                                         spin_unlock_irqrestore(
1462                                                 &local->queue_stop_reason_lock,
1463                                                 flags);
1464                                         ieee80211_purge_tx_queue(&local->hw,
1465                                                                  skbs);
1466                                         return true;
1467                                 }
1468                         } else {
1469
1470                                 /*
1471                                  * Since queue is stopped, queue up frames for
1472                                  * later transmission from the tx-pending
1473                                  * tasklet when the queue is woken again.
1474                                  */
1475                                 if (txpending)
1476                                         skb_queue_splice_init(skbs,
1477                                                               &local->pending[q]);
1478                                 else
1479                                         skb_queue_splice_tail_init(skbs,
1480                                                                    &local->pending[q]);
1481
1482                                 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1483                                                        flags);
1484                                 return false;
1485                         }
1486                 }
1487                 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1488
1489                 info->control.vif = vif;
1490                 control.sta = sta;
1491
1492                 __skb_unlink(skb, skbs);
1493                 drv_tx(local, &control, skb);
1494         }
1495
1496         return true;
1497 }
1498
1499 /*
1500  * Returns false if the frame couldn't be transmitted but was queued instead.
1501  */
1502 static bool __ieee80211_tx(struct ieee80211_local *local,
1503                            struct sk_buff_head *skbs, int led_len,
1504                            struct sta_info *sta, bool txpending)
1505 {
1506         struct ieee80211_tx_info *info;
1507         struct ieee80211_sub_if_data *sdata;
1508         struct ieee80211_vif *vif;
1509         struct ieee80211_sta *pubsta;
1510         struct sk_buff *skb;
1511         bool result = true;
1512         __le16 fc;
1513
1514         if (WARN_ON(skb_queue_empty(skbs)))
1515                 return true;
1516
1517         skb = skb_peek(skbs);
1518         fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1519         info = IEEE80211_SKB_CB(skb);
1520         sdata = vif_to_sdata(info->control.vif);
1521         if (sta && !sta->uploaded)
1522                 sta = NULL;
1523
1524         if (sta)
1525                 pubsta = &sta->sta;
1526         else
1527                 pubsta = NULL;
1528
1529         switch (sdata->vif.type) {
1530         case NL80211_IFTYPE_MONITOR:
1531                 if (sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE) {
1532                         vif = &sdata->vif;
1533                         break;
1534                 }
1535                 sdata = rcu_dereference(local->monitor_sdata);
1536                 if (sdata) {
1537                         vif = &sdata->vif;
1538                         info->hw_queue =
1539                                 vif->hw_queue[skb_get_queue_mapping(skb)];
1540                 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
1541                         ieee80211_purge_tx_queue(&local->hw, skbs);
1542                         return true;
1543                 } else
1544                         vif = NULL;
1545                 break;
1546         case NL80211_IFTYPE_AP_VLAN:
1547                 sdata = container_of(sdata->bss,
1548                                      struct ieee80211_sub_if_data, u.ap);
1549                 /* fall through */
1550         default:
1551                 vif = &sdata->vif;
1552                 break;
1553         }
1554
1555         result = ieee80211_tx_frags(local, vif, pubsta, skbs,
1556                                     txpending);
1557
1558         ieee80211_tpt_led_trig_tx(local, fc, led_len);
1559
1560         WARN_ON_ONCE(!skb_queue_empty(skbs));
1561
1562         return result;
1563 }
1564
1565 /*
1566  * Invoke TX handlers, return 0 on success and non-zero if the
1567  * frame was dropped or queued.
1568  */
1569 static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1570 {
1571         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1572         ieee80211_tx_result res = TX_DROP;
1573
1574 #define CALL_TXH(txh) \
1575         do {                            \
1576                 res = txh(tx);          \
1577                 if (res != TX_CONTINUE) \
1578                         goto txh_done;  \
1579         } while (0)
1580
1581         CALL_TXH(ieee80211_tx_h_dynamic_ps);
1582         CALL_TXH(ieee80211_tx_h_check_assoc);
1583         CALL_TXH(ieee80211_tx_h_ps_buf);
1584         CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
1585         CALL_TXH(ieee80211_tx_h_select_key);
1586         if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1587                 CALL_TXH(ieee80211_tx_h_rate_ctrl);
1588
1589         if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1590                 __skb_queue_tail(&tx->skbs, tx->skb);
1591                 tx->skb = NULL;
1592                 goto txh_done;
1593         }
1594
1595         CALL_TXH(ieee80211_tx_h_michael_mic_add);
1596         CALL_TXH(ieee80211_tx_h_sequence);
1597         CALL_TXH(ieee80211_tx_h_fragment);
1598         /* handlers after fragment must be aware of tx info fragmentation! */
1599         CALL_TXH(ieee80211_tx_h_stats);
1600         CALL_TXH(ieee80211_tx_h_encrypt);
1601         if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1602                 CALL_TXH(ieee80211_tx_h_calculate_duration);
1603 #undef CALL_TXH
1604
1605  txh_done:
1606         if (unlikely(res == TX_DROP)) {
1607                 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1608                 if (tx->skb)
1609                         ieee80211_free_txskb(&tx->local->hw, tx->skb);
1610                 else
1611                         ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1612                 return -1;
1613         } else if (unlikely(res == TX_QUEUED)) {
1614                 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1615                 return -1;
1616         }
1617
1618         return 0;
1619 }
1620
1621 bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1622                               struct ieee80211_vif *vif, struct sk_buff *skb,
1623                               int band, struct ieee80211_sta **sta)
1624 {
1625         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1626         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1627         struct ieee80211_tx_data tx;
1628         struct sk_buff *skb2;
1629
1630         if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
1631                 return false;
1632
1633         info->band = band;
1634         info->control.vif = vif;
1635         info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1636
1637         if (invoke_tx_handlers(&tx))
1638                 return false;
1639
1640         if (sta) {
1641                 if (tx.sta)
1642                         *sta = &tx.sta->sta;
1643                 else
1644                         *sta = NULL;
1645         }
1646
1647         /* this function isn't suitable for fragmented data frames */
1648         skb2 = __skb_dequeue(&tx.skbs);
1649         if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1650                 ieee80211_free_txskb(hw, skb2);
1651                 ieee80211_purge_tx_queue(hw, &tx.skbs);
1652                 return false;
1653         }
1654
1655         return true;
1656 }
1657 EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1658
1659 /*
1660  * Returns false if the frame couldn't be transmitted but was queued instead.
1661  */
1662 static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1663                          struct sta_info *sta, struct sk_buff *skb,
1664                          bool txpending)
1665 {
1666         struct ieee80211_local *local = sdata->local;
1667         struct ieee80211_tx_data tx;
1668         ieee80211_tx_result res_prepare;
1669         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1670         bool result = true;
1671         int led_len;
1672
1673         if (unlikely(skb->len < 10)) {
1674                 dev_kfree_skb(skb);
1675                 return true;
1676         }
1677
1678         /* initialises tx */
1679         led_len = skb->len;
1680         res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
1681
1682         if (unlikely(res_prepare == TX_DROP)) {
1683                 ieee80211_free_txskb(&local->hw, skb);
1684                 return true;
1685         } else if (unlikely(res_prepare == TX_QUEUED)) {
1686                 return true;
1687         }
1688
1689         /* set up hw_queue value early */
1690         if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
1691             !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
1692                 info->hw_queue =
1693                         sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1694
1695         if (!invoke_tx_handlers(&tx))
1696                 result = __ieee80211_tx(local, &tx.skbs, led_len,
1697                                         tx.sta, txpending);
1698
1699         return result;
1700 }
1701
1702 /* device xmit handlers */
1703
1704 static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
1705                                 struct sk_buff *skb,
1706                                 int head_need, bool may_encrypt)
1707 {
1708         struct ieee80211_local *local = sdata->local;
1709         int tail_need = 0;
1710
1711         if (may_encrypt && sdata->crypto_tx_tailroom_needed_cnt) {
1712                 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1713                 tail_need -= skb_tailroom(skb);
1714                 tail_need = max_t(int, tail_need, 0);
1715         }
1716
1717         if (skb_cloned(skb) &&
1718             (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
1719              !skb_clone_writable(skb, ETH_HLEN) ||
1720              (may_encrypt && sdata->crypto_tx_tailroom_needed_cnt)))
1721                 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1722         else if (head_need || tail_need)
1723                 I802_DEBUG_INC(local->tx_expand_skb_head);
1724         else
1725                 return 0;
1726
1727         if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1728                 wiphy_debug(local->hw.wiphy,
1729                             "failed to reallocate TX buffer\n");
1730                 return -ENOMEM;
1731         }
1732
1733         return 0;
1734 }
1735
1736 void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
1737                     struct sta_info *sta, struct sk_buff *skb)
1738 {
1739         struct ieee80211_local *local = sdata->local;
1740         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1741         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1742         int headroom;
1743         bool may_encrypt;
1744
1745         may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
1746
1747         headroom = local->tx_headroom;
1748         if (may_encrypt)
1749                 headroom += sdata->encrypt_headroom;
1750         headroom -= skb_headroom(skb);
1751         headroom = max_t(int, 0, headroom);
1752
1753         if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
1754                 ieee80211_free_txskb(&local->hw, skb);
1755                 return;
1756         }
1757
1758         hdr = (struct ieee80211_hdr *) skb->data;
1759         info->control.vif = &sdata->vif;
1760
1761         if (ieee80211_vif_is_mesh(&sdata->vif)) {
1762                 if (ieee80211_is_data(hdr->frame_control) &&
1763                     is_unicast_ether_addr(hdr->addr1)) {
1764                         if (mesh_nexthop_resolve(sdata, skb))
1765                                 return; /* skb queued: don't free */
1766                 } else {
1767                         ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
1768                 }
1769         }
1770
1771         ieee80211_set_qos_hdr(sdata, skb);
1772         ieee80211_tx(sdata, sta, skb, false);
1773 }
1774
1775 static bool ieee80211_parse_tx_radiotap(struct ieee80211_local *local,
1776                                         struct sk_buff *skb)
1777 {
1778         struct ieee80211_radiotap_iterator iterator;
1779         struct ieee80211_radiotap_header *rthdr =
1780                 (struct ieee80211_radiotap_header *) skb->data;
1781         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1782         struct ieee80211_supported_band *sband =
1783                 local->hw.wiphy->bands[info->band];
1784         int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
1785                                                    NULL);
1786         u16 txflags;
1787         u16 rate = 0;
1788         bool rate_found = false;
1789         u8 rate_retries = 0;
1790         u16 rate_flags = 0;
1791         u8 mcs_known, mcs_flags, mcs_bw;
1792         u16 vht_known;
1793         u8 vht_mcs = 0, vht_nss = 0;
1794         int i;
1795
1796         info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1797                        IEEE80211_TX_CTL_DONTFRAG;
1798
1799         /*
1800          * for every radiotap entry that is present
1801          * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
1802          * entries present, or -EINVAL on error)
1803          */
1804
1805         while (!ret) {
1806                 ret = ieee80211_radiotap_iterator_next(&iterator);
1807
1808                 if (ret)
1809                         continue;
1810
1811                 /* see if this argument is something we can use */
1812                 switch (iterator.this_arg_index) {
1813                 /*
1814                  * You must take care when dereferencing iterator.this_arg
1815                  * for multibyte types... the pointer is not aligned.  Use
1816                  * get_unaligned((type *)iterator.this_arg) to dereference
1817                  * iterator.this_arg for type "type" safely on all arches.
1818                 */
1819                 case IEEE80211_RADIOTAP_FLAGS:
1820                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
1821                                 /*
1822                                  * this indicates that the skb we have been
1823                                  * handed has the 32-bit FCS CRC at the end...
1824                                  * we should react to that by snipping it off
1825                                  * because it will be recomputed and added
1826                                  * on transmission
1827                                  */
1828                                 if (skb->len < (iterator._max_length + FCS_LEN))
1829                                         return false;
1830
1831                                 skb_trim(skb, skb->len - FCS_LEN);
1832                         }
1833                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
1834                                 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
1835                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
1836                                 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
1837                         break;
1838
1839                 case IEEE80211_RADIOTAP_TX_FLAGS:
1840                         txflags = get_unaligned_le16(iterator.this_arg);
1841                         if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
1842                                 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1843                         break;
1844
1845                 case IEEE80211_RADIOTAP_RATE:
1846                         rate = *iterator.this_arg;
1847                         rate_flags = 0;
1848                         rate_found = true;
1849                         break;
1850
1851                 case IEEE80211_RADIOTAP_DATA_RETRIES:
1852                         rate_retries = *iterator.this_arg;
1853                         break;
1854
1855                 case IEEE80211_RADIOTAP_MCS:
1856                         mcs_known = iterator.this_arg[0];
1857                         mcs_flags = iterator.this_arg[1];
1858                         if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
1859                                 break;
1860
1861                         rate_found = true;
1862                         rate = iterator.this_arg[2];
1863                         rate_flags = IEEE80211_TX_RC_MCS;
1864
1865                         if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
1866                             mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
1867                                 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
1868
1869                         mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
1870                         if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
1871                             mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
1872                                 rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
1873                         break;
1874
1875                 case IEEE80211_RADIOTAP_VHT:
1876                         vht_known = get_unaligned_le16(iterator.this_arg);
1877                         rate_found = true;
1878
1879                         rate_flags = IEEE80211_TX_RC_VHT_MCS;
1880                         if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
1881                             (iterator.this_arg[2] &
1882                              IEEE80211_RADIOTAP_VHT_FLAG_SGI))
1883                                 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
1884                         if (vht_known &
1885                             IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
1886                                 if (iterator.this_arg[3] == 1)
1887                                         rate_flags |=
1888                                                 IEEE80211_TX_RC_40_MHZ_WIDTH;
1889                                 else if (iterator.this_arg[3] == 4)
1890                                         rate_flags |=
1891                                                 IEEE80211_TX_RC_80_MHZ_WIDTH;
1892                                 else if (iterator.this_arg[3] == 11)
1893                                         rate_flags |=
1894                                                 IEEE80211_TX_RC_160_MHZ_WIDTH;
1895                         }
1896
1897                         vht_mcs = iterator.this_arg[4] >> 4;
1898                         vht_nss = iterator.this_arg[4] & 0xF;
1899                         break;
1900
1901                 /*
1902                  * Please update the file
1903                  * Documentation/networking/mac80211-injection.txt
1904                  * when parsing new fields here.
1905                  */
1906
1907                 default:
1908                         break;
1909                 }
1910         }
1911
1912         if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
1913                 return false;
1914
1915         if (rate_found) {
1916                 info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
1917
1918                 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1919                         info->control.rates[i].idx = -1;
1920                         info->control.rates[i].flags = 0;
1921                         info->control.rates[i].count = 0;
1922                 }
1923
1924                 if (rate_flags & IEEE80211_TX_RC_MCS) {
1925                         info->control.rates[0].idx = rate;
1926                 } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
1927                         ieee80211_rate_set_vht(info->control.rates, vht_mcs,
1928                                                vht_nss);
1929                 } else {
1930                         for (i = 0; i < sband->n_bitrates; i++) {
1931                                 if (rate * 5 != sband->bitrates[i].bitrate)
1932                                         continue;
1933
1934                                 info->control.rates[0].idx = i;
1935                                 break;
1936                         }
1937                 }
1938
1939                 if (info->control.rates[0].idx < 0)
1940                         info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
1941
1942                 info->control.rates[0].flags = rate_flags;
1943                 info->control.rates[0].count = min_t(u8, rate_retries + 1,
1944                                                      local->hw.max_rate_tries);
1945         }
1946
1947         /*
1948          * remove the radiotap header
1949          * iterator->_max_length was sanity-checked against
1950          * skb->len by iterator init
1951          */
1952         skb_pull(skb, iterator._max_length);
1953
1954         return true;
1955 }
1956
1957 netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
1958                                          struct net_device *dev)
1959 {
1960         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1961         struct ieee80211_chanctx_conf *chanctx_conf;
1962         struct ieee80211_radiotap_header *prthdr =
1963                 (struct ieee80211_radiotap_header *)skb->data;
1964         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1965         struct ieee80211_hdr *hdr;
1966         struct ieee80211_sub_if_data *tmp_sdata, *sdata;
1967         struct cfg80211_chan_def *chandef;
1968         u16 len_rthdr;
1969         int hdrlen;
1970
1971         /* check for not even having the fixed radiotap header part */
1972         if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1973                 goto fail; /* too short to be possibly valid */
1974
1975         /* is it a header version we can trust to find length from? */
1976         if (unlikely(prthdr->it_version))
1977                 goto fail; /* only version 0 is supported */
1978
1979         /* then there must be a radiotap header with a length we can use */
1980         len_rthdr = ieee80211_get_radiotap_len(skb->data);
1981
1982         /* does the skb contain enough to deliver on the alleged length? */
1983         if (unlikely(skb->len < len_rthdr))
1984                 goto fail; /* skb too short for claimed rt header extent */
1985
1986         /*
1987          * fix up the pointers accounting for the radiotap
1988          * header still being in there.  We are being given
1989          * a precooked IEEE80211 header so no need for
1990          * normal processing
1991          */
1992         skb_set_mac_header(skb, len_rthdr);
1993         /*
1994          * these are just fixed to the end of the rt area since we
1995          * don't have any better information and at this point, nobody cares
1996          */
1997         skb_set_network_header(skb, len_rthdr);
1998         skb_set_transport_header(skb, len_rthdr);
1999
2000         if (skb->len < len_rthdr + 2)
2001                 goto fail;
2002
2003         hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2004         hdrlen = ieee80211_hdrlen(hdr->frame_control);
2005
2006         if (skb->len < len_rthdr + hdrlen)
2007                 goto fail;
2008
2009         /*
2010          * Initialize skb->protocol if the injected frame is a data frame
2011          * carrying a rfc1042 header
2012          */
2013         if (ieee80211_is_data(hdr->frame_control) &&
2014             skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2015                 u8 *payload = (u8 *)hdr + hdrlen;
2016
2017                 if (ether_addr_equal(payload, rfc1042_header))
2018                         skb->protocol = cpu_to_be16((payload[6] << 8) |
2019                                                     payload[7]);
2020         }
2021
2022         memset(info, 0, sizeof(*info));
2023
2024         info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2025                       IEEE80211_TX_CTL_INJECTED;
2026
2027         rcu_read_lock();
2028
2029         /*
2030          * We process outgoing injected frames that have a local address
2031          * we handle as though they are non-injected frames.
2032          * This code here isn't entirely correct, the local MAC address
2033          * isn't always enough to find the interface to use; for proper
2034          * VLAN/WDS support we will need a different mechanism (which
2035          * likely isn't going to be monitor interfaces).
2036          */
2037         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2038
2039         list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2040                 if (!ieee80211_sdata_running(tmp_sdata))
2041                         continue;
2042                 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2043                     tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2044                     tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
2045                         continue;
2046                 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
2047                         sdata = tmp_sdata;
2048                         break;
2049                 }
2050         }
2051
2052         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2053         if (!chanctx_conf) {
2054                 tmp_sdata = rcu_dereference(local->monitor_sdata);
2055                 if (tmp_sdata)
2056                         chanctx_conf =
2057                                 rcu_dereference(tmp_sdata->vif.chanctx_conf);
2058         }
2059
2060         if (chanctx_conf)
2061                 chandef = &chanctx_conf->def;
2062         else if (!local->use_chanctx)
2063                 chandef = &local->_oper_chandef;
2064         else
2065                 goto fail_rcu;
2066
2067         /*
2068          * Frame injection is not allowed if beaconing is not allowed
2069          * or if we need radar detection. Beaconing is usually not allowed when
2070          * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2071          * Passive scan is also used in world regulatory domains where
2072          * your country is not known and as such it should be treated as
2073          * NO TX unless the channel is explicitly allowed in which case
2074          * your current regulatory domain would not have the passive scan
2075          * flag.
2076          *
2077          * Since AP mode uses monitor interfaces to inject/TX management
2078          * frames we can make AP mode the exception to this rule once it
2079          * supports radar detection as its implementation can deal with
2080          * radar detection by itself. We can do that later by adding a
2081          * monitor flag interfaces used for AP support.
2082          */
2083         if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2084                                      sdata->vif.type))
2085                 goto fail_rcu;
2086
2087         info->band = chandef->chan->band;
2088
2089         /* process and remove the injection radiotap header */
2090         if (!ieee80211_parse_tx_radiotap(local, skb))
2091                 goto fail_rcu;
2092
2093         ieee80211_xmit(sdata, NULL, skb);
2094         rcu_read_unlock();
2095
2096         return NETDEV_TX_OK;
2097
2098 fail_rcu:
2099         rcu_read_unlock();
2100 fail:
2101         dev_kfree_skb(skb);
2102         return NETDEV_TX_OK; /* meaning, we dealt with the skb */
2103 }
2104
2105 static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
2106 {
2107         u16 ethertype = (skb->data[12] << 8) | skb->data[13];
2108
2109         return ethertype == ETH_P_TDLS &&
2110                skb->len > 14 &&
2111                skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2112 }
2113
2114 static int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2115                                    struct sk_buff *skb,
2116                                    struct sta_info **sta_out)
2117 {
2118         struct sta_info *sta;
2119
2120         switch (sdata->vif.type) {
2121         case NL80211_IFTYPE_AP_VLAN:
2122                 sta = rcu_dereference(sdata->u.vlan.sta);
2123                 if (sta) {
2124                         *sta_out = sta;
2125                         return 0;
2126                 } else if (sdata->wdev.use_4addr) {
2127                         return -ENOLINK;
2128                 }
2129                 /* fall through */
2130         case NL80211_IFTYPE_AP:
2131         case NL80211_IFTYPE_OCB:
2132         case NL80211_IFTYPE_ADHOC:
2133                 if (is_multicast_ether_addr(skb->data)) {
2134                         *sta_out = ERR_PTR(-ENOENT);
2135                         return 0;
2136                 }
2137                 sta = sta_info_get_bss(sdata, skb->data);
2138                 break;
2139         case NL80211_IFTYPE_WDS:
2140                 sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
2141                 break;
2142 #ifdef CONFIG_MAC80211_MESH
2143         case NL80211_IFTYPE_MESH_POINT:
2144                 /* determined much later */
2145                 *sta_out = NULL;
2146                 return 0;
2147 #endif
2148         case NL80211_IFTYPE_STATION:
2149                 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2150                         sta = sta_info_get(sdata, skb->data);
2151                         if (sta) {
2152                                 bool tdls_peer, tdls_auth;
2153
2154                                 tdls_peer = test_sta_flag(sta,
2155                                                           WLAN_STA_TDLS_PEER);
2156                                 tdls_auth = test_sta_flag(sta,
2157                                                 WLAN_STA_TDLS_PEER_AUTH);
2158
2159                                 if (tdls_peer && tdls_auth) {
2160                                         *sta_out = sta;
2161                                         return 0;
2162                                 }
2163
2164                                 /*
2165                                  * TDLS link during setup - throw out frames to
2166                                  * peer. Allow TDLS-setup frames to unauthorized
2167                                  * peers for the special case of a link teardown
2168                                  * after a TDLS sta is removed due to being
2169                                  * unreachable.
2170                                  */
2171                                 if (tdls_peer && !tdls_auth &&
2172                                     !ieee80211_is_tdls_setup(skb))
2173                                         return -EINVAL;
2174                         }
2175
2176                 }
2177
2178                 sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2179                 if (!sta)
2180                         return -ENOLINK;
2181                 break;
2182         default:
2183                 return -EINVAL;
2184         }
2185
2186         *sta_out = sta ?: ERR_PTR(-ENOENT);
2187         return 0;
2188 }
2189
2190 /**
2191  * ieee80211_build_hdr - build 802.11 header in the given frame
2192  * @sdata: virtual interface to build the header for
2193  * @skb: the skb to build the header in
2194  * @info_flags: skb flags to set
2195  *
2196  * This function takes the skb with 802.3 header and reformats the header to
2197  * the appropriate IEEE 802.11 header based on which interface the packet is
2198  * being transmitted on.
2199  *
2200  * Note that this function also takes care of the TX status request and
2201  * potential unsharing of the SKB - this needs to be interleaved with the
2202  * header building.
2203  *
2204  * The function requires the read-side RCU lock held
2205  *
2206  * Returns: the (possibly reallocated) skb or an ERR_PTR() code
2207  */
2208 static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
2209                                            struct sk_buff *skb, u32 info_flags,
2210                                            struct sta_info *sta)
2211 {
2212         struct ieee80211_local *local = sdata->local;
2213         struct ieee80211_tx_info *info;
2214         int head_need;
2215         u16 ethertype, hdrlen,  meshhdrlen = 0;
2216         __le16 fc;
2217         struct ieee80211_hdr hdr;
2218         struct ieee80211s_hdr mesh_hdr __maybe_unused;
2219         struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
2220         const u8 *encaps_data;
2221         int encaps_len, skip_header_bytes;
2222         int nh_pos, h_pos;
2223         bool wme_sta = false, authorized = false;
2224         bool tdls_peer;
2225         bool multicast;
2226         u16 info_id = 0;
2227         struct ieee80211_chanctx_conf *chanctx_conf;
2228         struct ieee80211_sub_if_data *ap_sdata;
2229         enum nl80211_band band;
2230         int ret;
2231
2232         if (IS_ERR(sta))
2233                 sta = NULL;
2234
2235         /* convert Ethernet header to proper 802.11 header (based on
2236          * operation mode) */
2237         ethertype = (skb->data[12] << 8) | skb->data[13];
2238         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2239
2240         switch (sdata->vif.type) {
2241         case NL80211_IFTYPE_AP_VLAN:
2242                 if (sdata->wdev.use_4addr) {
2243                         fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2244                         /* RA TA DA SA */
2245                         memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
2246                         memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2247                         memcpy(hdr.addr3, skb->data, ETH_ALEN);
2248                         memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2249                         hdrlen = 30;
2250                         authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2251                         wme_sta = sta->sta.wme;
2252                 }
2253                 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2254                                         u.ap);
2255                 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
2256                 if (!chanctx_conf) {
2257                         ret = -ENOTCONN;
2258                         goto free;
2259                 }
2260                 band = chanctx_conf->def.chan->band;
2261                 if (sdata->wdev.use_4addr)
2262                         break;
2263                 /* fall through */
2264         case NL80211_IFTYPE_AP:
2265                 if (sdata->vif.type == NL80211_IFTYPE_AP)
2266                         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2267                 if (!chanctx_conf) {
2268                         ret = -ENOTCONN;
2269                         goto free;
2270                 }
2271                 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2272                 /* DA BSSID SA */
2273                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2274                 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2275                 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2276                 hdrlen = 24;
2277                 band = chanctx_conf->def.chan->band;
2278                 break;
2279         case NL80211_IFTYPE_WDS:
2280                 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2281                 /* RA TA DA SA */
2282                 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
2283                 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2284                 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2285                 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2286                 hdrlen = 30;
2287                 /*
2288                  * This is the exception! WDS style interfaces are prohibited
2289                  * when channel contexts are in used so this must be valid
2290                  */
2291                 band = local->hw.conf.chandef.chan->band;
2292                 break;
2293 #ifdef CONFIG_MAC80211_MESH
2294         case NL80211_IFTYPE_MESH_POINT:
2295                 if (!is_multicast_ether_addr(skb->data)) {
2296                         struct sta_info *next_hop;
2297                         bool mpp_lookup = true;
2298
2299                         mpath = mesh_path_lookup(sdata, skb->data);
2300                         if (mpath) {
2301                                 mpp_lookup = false;
2302                                 next_hop = rcu_dereference(mpath->next_hop);
2303                                 if (!next_hop ||
2304                                     !(mpath->flags & (MESH_PATH_ACTIVE |
2305                                                       MESH_PATH_RESOLVING)))
2306                                         mpp_lookup = true;
2307                         }
2308
2309                         if (mpp_lookup) {
2310                                 mppath = mpp_path_lookup(sdata, skb->data);
2311                                 if (mppath)
2312                                         mppath->exp_time = jiffies;
2313                         }
2314
2315                         if (mppath && mpath)
2316                                 mesh_path_del(sdata, mpath->dst);
2317                 }
2318
2319                 /*
2320                  * Use address extension if it is a packet from
2321                  * another interface or if we know the destination
2322                  * is being proxied by a portal (i.e. portal address
2323                  * differs from proxied address)
2324                  */
2325                 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2326                     !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
2327                         hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2328                                         skb->data, skb->data + ETH_ALEN);
2329                         meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2330                                                                NULL, NULL);
2331                 } else {
2332                         /* DS -> MBSS (802.11-2012 13.11.3.3).
2333                          * For unicast with unknown forwarding information,
2334                          * destination might be in the MBSS or if that fails
2335                          * forwarded to another mesh gate. In either case
2336                          * resolution will be handled in ieee80211_xmit(), so
2337                          * leave the original DA. This also works for mcast */
2338                         const u8 *mesh_da = skb->data;
2339
2340                         if (mppath)
2341                                 mesh_da = mppath->mpp;
2342                         else if (mpath)
2343                                 mesh_da = mpath->dst;
2344
2345                         hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2346                                         mesh_da, sdata->vif.addr);
2347                         if (is_multicast_ether_addr(mesh_da))
2348                                 /* DA TA mSA AE:SA */
2349                                 meshhdrlen = ieee80211_new_mesh_header(
2350                                                 sdata, &mesh_hdr,
2351                                                 skb->data + ETH_ALEN, NULL);
2352                         else
2353                                 /* RA TA mDA mSA AE:DA SA */
2354                                 meshhdrlen = ieee80211_new_mesh_header(
2355                                                 sdata, &mesh_hdr, skb->data,
2356                                                 skb->data + ETH_ALEN);
2357
2358                 }
2359                 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2360                 if (!chanctx_conf) {
2361                         ret = -ENOTCONN;
2362                         goto free;
2363                 }
2364                 band = chanctx_conf->def.chan->band;
2365                 break;
2366 #endif
2367         case NL80211_IFTYPE_STATION:
2368                 /* we already did checks when looking up the RA STA */
2369                 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
2370
2371                 if (tdls_peer) {
2372                         /* DA SA BSSID */
2373                         memcpy(hdr.addr1, skb->data, ETH_ALEN);
2374                         memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2375                         memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2376                         hdrlen = 24;
2377                 }  else if (sdata->u.mgd.use_4addr &&
2378                             cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2379                         fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2380                                           IEEE80211_FCTL_TODS);
2381                         /* RA TA DA SA */
2382                         memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2383                         memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2384                         memcpy(hdr.addr3, skb->data, ETH_ALEN);
2385                         memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2386                         hdrlen = 30;
2387                 } else {
2388                         fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2389                         /* BSSID SA DA */
2390                         memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2391                         memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2392                         memcpy(hdr.addr3, skb->data, ETH_ALEN);
2393                         hdrlen = 24;
2394                 }
2395                 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2396                 if (!chanctx_conf) {
2397                         ret = -ENOTCONN;
2398                         goto free;
2399                 }
2400                 band = chanctx_conf->def.chan->band;
2401                 break;
2402         case NL80211_IFTYPE_OCB:
2403                 /* DA SA BSSID */
2404                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2405                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2406                 eth_broadcast_addr(hdr.addr3);
2407                 hdrlen = 24;
2408                 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2409                 if (!chanctx_conf) {
2410                         ret = -ENOTCONN;
2411                         goto free;
2412                 }
2413                 band = chanctx_conf->def.chan->band;
2414                 break;
2415         case NL80211_IFTYPE_ADHOC:
2416                 /* DA SA BSSID */
2417                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2418                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2419                 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
2420                 hdrlen = 24;
2421                 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2422                 if (!chanctx_conf) {
2423                         ret = -ENOTCONN;
2424                         goto free;
2425                 }
2426                 band = chanctx_conf->def.chan->band;
2427                 break;
2428         default:
2429                 ret = -EINVAL;
2430                 goto free;
2431         }
2432
2433         multicast = is_multicast_ether_addr(hdr.addr1);
2434
2435         /* sta is always NULL for mesh */
2436         if (sta) {
2437                 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2438                 wme_sta = sta->sta.wme;
2439         } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2440                 /* For mesh, the use of the QoS header is mandatory */
2441                 wme_sta = true;
2442         }
2443
2444         /* receiver does QoS (which also means we do) use it */
2445         if (wme_sta) {
2446                 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2447                 hdrlen += 2;
2448         }
2449
2450         /*
2451          * Drop unicast frames to unauthorised stations unless they are
2452          * EAPOL frames from the local station.
2453          */
2454         if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
2455                      (sdata->vif.type != NL80211_IFTYPE_OCB) &&
2456                      !multicast && !authorized &&
2457                      (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
2458                       !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
2459 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2460                 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
2461                                     sdata->name, hdr.addr1);
2462 #endif
2463
2464                 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2465
2466                 ret = -EPERM;
2467                 goto free;
2468         }
2469
2470         if (unlikely(!multicast && skb->sk &&
2471                      skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
2472                 struct sk_buff *ack_skb = skb_clone_sk(skb);
2473
2474                 if (ack_skb) {
2475                         unsigned long flags;
2476                         int id;
2477
2478                         spin_lock_irqsave(&local->ack_status_lock, flags);
2479                         id = idr_alloc(&local->ack_status_frames, ack_skb,
2480                                        1, 0x10000, GFP_ATOMIC);
2481                         spin_unlock_irqrestore(&local->ack_status_lock, flags);
2482
2483                         if (id >= 0) {
2484                                 info_id = id;
2485                                 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2486                         } else {
2487                                 kfree_skb(ack_skb);
2488                         }
2489                 }
2490         }
2491
2492         /*
2493          * If the skb is shared we need to obtain our own copy.
2494          */
2495         if (skb_shared(skb)) {
2496                 struct sk_buff *tmp_skb = skb;
2497
2498                 /* can't happen -- skb is a clone if info_id != 0 */
2499                 WARN_ON(info_id);
2500
2501                 skb = skb_clone(skb, GFP_ATOMIC);
2502                 kfree_skb(tmp_skb);
2503
2504                 if (!skb) {
2505                         ret = -ENOMEM;
2506                         goto free;
2507                 }
2508         }
2509
2510         hdr.frame_control = fc;
2511         hdr.duration_id = 0;
2512         hdr.seq_ctrl = 0;
2513
2514         skip_header_bytes = ETH_HLEN;
2515         if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2516                 encaps_data = bridge_tunnel_header;
2517                 encaps_len = sizeof(bridge_tunnel_header);
2518                 skip_header_bytes -= 2;
2519         } else if (ethertype >= ETH_P_802_3_MIN) {
2520                 encaps_data = rfc1042_header;
2521                 encaps_len = sizeof(rfc1042_header);
2522                 skip_header_bytes -= 2;
2523         } else {
2524                 encaps_data = NULL;
2525                 encaps_len = 0;
2526         }
2527
2528         nh_pos = skb_network_header(skb) - skb->data;
2529         h_pos = skb_transport_header(skb) - skb->data;
2530
2531         skb_pull(skb, skip_header_bytes);
2532         nh_pos -= skip_header_bytes;
2533         h_pos -= skip_header_bytes;
2534
2535         head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
2536
2537         /*
2538          * So we need to modify the skb header and hence need a copy of
2539          * that. The head_need variable above doesn't, so far, include
2540          * the needed header space that we don't need right away. If we
2541          * can, then we don't reallocate right now but only after the
2542          * frame arrives at the master device (if it does...)
2543          *
2544          * If we cannot, however, then we will reallocate to include all
2545          * the ever needed space. Also, if we need to reallocate it anyway,
2546          * make it big enough for everything we may ever need.
2547          */
2548
2549         if (head_need > 0 || skb_cloned(skb)) {
2550                 head_need += sdata->encrypt_headroom;
2551                 head_need += local->tx_headroom;
2552                 head_need = max_t(int, 0, head_need);
2553                 if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
2554                         ieee80211_free_txskb(&local->hw, skb);
2555                         skb = NULL;
2556                         return ERR_PTR(-ENOMEM);
2557                 }
2558         }
2559
2560         if (encaps_data) {
2561                 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
2562                 nh_pos += encaps_len;
2563                 h_pos += encaps_len;
2564         }
2565
2566 #ifdef CONFIG_MAC80211_MESH
2567         if (meshhdrlen > 0) {
2568                 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
2569                 nh_pos += meshhdrlen;
2570                 h_pos += meshhdrlen;
2571         }
2572 #endif
2573
2574         if (ieee80211_is_data_qos(fc)) {
2575                 __le16 *qos_control;
2576
2577                 qos_control = (__le16 *) skb_push(skb, 2);
2578                 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2579                 /*
2580                  * Maybe we could actually set some fields here, for now just
2581                  * initialise to zero to indicate no special operation.
2582                  */
2583                 *qos_control = 0;
2584         } else
2585                 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2586
2587         nh_pos += hdrlen;
2588         h_pos += hdrlen;
2589
2590         /* Update skb pointers to various headers since this modified frame
2591          * is going to go through Linux networking code that may potentially
2592          * need things like pointer to IP header. */
2593         skb_reset_mac_header(skb);
2594         skb_set_network_header(skb, nh_pos);
2595         skb_set_transport_header(skb, h_pos);
2596
2597         info = IEEE80211_SKB_CB(skb);
2598         memset(info, 0, sizeof(*info));
2599
2600         info->flags = info_flags;
2601         info->ack_frame_id = info_id;
2602         info->band = band;
2603
2604         return skb;
2605  free:
2606         kfree_skb(skb);
2607         return ERR_PTR(ret);
2608 }
2609
2610 /*
2611  * fast-xmit overview
2612  *
2613  * The core idea of this fast-xmit is to remove per-packet checks by checking
2614  * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2615  * checks that are needed to get the sta->fast_tx pointer assigned, after which
2616  * much less work can be done per packet. For example, fragmentation must be
2617  * disabled or the fast_tx pointer will not be set. All the conditions are seen
2618  * in the code here.
2619  *
2620  * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2621  * header and other data to aid packet processing in ieee80211_xmit_fast().
2622  *
2623  * The most difficult part of this is that when any of these assumptions
2624  * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2625  * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2626  * since the per-packet code no longer checks the conditions. This is reflected
2627  * by the calls to these functions throughout the rest of the code, and must be
2628  * maintained if any of the TX path checks change.
2629  */
2630
2631 void ieee80211_check_fast_xmit(struct sta_info *sta)
2632 {
2633         struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2634         struct ieee80211_local *local = sta->local;
2635         struct ieee80211_sub_if_data *sdata = sta->sdata;
2636         struct ieee80211_hdr *hdr = (void *)build.hdr;
2637         struct ieee80211_chanctx_conf *chanctx_conf;
2638         __le16 fc;
2639
2640         if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
2641                 return;
2642
2643         /* Locking here protects both the pointer itself, and against concurrent
2644          * invocations winning data access races to, e.g., the key pointer that
2645          * is used.
2646          * Without it, the invocation of this function right after the key
2647          * pointer changes wouldn't be sufficient, as another CPU could access
2648          * the pointer, then stall, and then do the cache update after the CPU
2649          * that invalidated the key.
2650          * With the locking, such scenarios cannot happen as the check for the
2651          * key and the fast-tx assignment are done atomically, so the CPU that
2652          * modifies the key will either wait or other one will see the key
2653          * cleared/changed already.
2654          */
2655         spin_lock_bh(&sta->lock);
2656         if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2657             !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2658             sdata->vif.type == NL80211_IFTYPE_STATION)
2659                 goto out;
2660
2661         if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2662                 goto out;
2663
2664         if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2665             test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
2666             test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2667             test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
2668                 goto out;
2669
2670         if (sdata->noack_map)
2671                 goto out;
2672
2673         /* fast-xmit doesn't handle fragmentation at all */
2674         if (local->hw.wiphy->frag_threshold != (u32)-1 &&
2675             !local->ops->set_frag_threshold)
2676                 goto out;
2677
2678         rcu_read_lock();
2679         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2680         if (!chanctx_conf) {
2681                 rcu_read_unlock();
2682                 goto out;
2683         }
2684         build.band = chanctx_conf->def.chan->band;
2685         rcu_read_unlock();
2686
2687         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2688
2689         switch (sdata->vif.type) {
2690         case NL80211_IFTYPE_ADHOC:
2691                 /* DA SA BSSID */
2692                 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2693                 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2694                 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
2695                 build.hdr_len = 24;
2696                 break;
2697         case NL80211_IFTYPE_STATION:
2698                 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2699                         /* DA SA BSSID */
2700                         build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2701                         build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2702                         memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2703                         build.hdr_len = 24;
2704                         break;
2705                 }
2706
2707                 if (sdata->u.mgd.use_4addr) {
2708                         /* non-regular ethertype cannot use the fastpath */
2709                         fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2710                                           IEEE80211_FCTL_TODS);
2711                         /* RA TA DA SA */
2712                         memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2713                         memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2714                         build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2715                         build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2716                         build.hdr_len = 30;
2717                         break;
2718                 }
2719                 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2720                 /* BSSID SA DA */
2721                 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2722                 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2723                 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2724                 build.hdr_len = 24;
2725                 break;
2726         case NL80211_IFTYPE_AP_VLAN:
2727                 if (sdata->wdev.use_4addr) {
2728                         fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2729                                           IEEE80211_FCTL_TODS);
2730                         /* RA TA DA SA */
2731                         memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
2732                         memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2733                         build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2734                         build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2735                         build.hdr_len = 30;
2736                         break;
2737                 }
2738                 /* fall through */
2739         case NL80211_IFTYPE_AP:
2740                 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2741                 /* DA BSSID SA */
2742                 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2743                 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2744                 build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
2745                 build.hdr_len = 24;
2746                 break;
2747         default:
2748                 /* not handled on fast-xmit */
2749                 goto out;
2750         }
2751
2752         if (sta->sta.wme) {
2753                 build.hdr_len += 2;
2754                 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2755         }
2756
2757         /* We store the key here so there's no point in using rcu_dereference()
2758          * but that's fine because the code that changes the pointers will call
2759          * this function after doing so. For a single CPU that would be enough,
2760          * for multiple see the comment above.
2761          */
2762         build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
2763         if (!build.key)
2764                 build.key = rcu_access_pointer(sdata->default_unicast_key);
2765         if (build.key) {
2766                 bool gen_iv, iv_spc, mmic;
2767
2768                 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
2769                 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2770                 mmic = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC;
2771
2772                 /* don't handle software crypto */
2773                 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
2774                         goto out;
2775
2776                 switch (build.key->conf.cipher) {
2777                 case WLAN_CIPHER_SUITE_CCMP:
2778                 case WLAN_CIPHER_SUITE_CCMP_256:
2779                         /* add fixed key ID */
2780                         if (gen_iv) {
2781                                 (build.hdr + build.hdr_len)[3] =
2782                                         0x20 | (build.key->conf.keyidx << 6);
2783                                 build.pn_offs = build.hdr_len;
2784                         }
2785                         if (gen_iv || iv_spc)
2786                                 build.hdr_len += IEEE80211_CCMP_HDR_LEN;
2787                         break;
2788                 case WLAN_CIPHER_SUITE_GCMP:
2789                 case WLAN_CIPHER_SUITE_GCMP_256:
2790                         /* add fixed key ID */
2791                         if (gen_iv) {
2792                                 (build.hdr + build.hdr_len)[3] =
2793                                         0x20 | (build.key->conf.keyidx << 6);
2794                                 build.pn_offs = build.hdr_len;
2795                         }
2796                         if (gen_iv || iv_spc)
2797                                 build.hdr_len += IEEE80211_GCMP_HDR_LEN;
2798                         break;
2799                 case WLAN_CIPHER_SUITE_TKIP:
2800                         /* cannot handle MMIC or IV generation in xmit-fast */
2801                         if (mmic || gen_iv)
2802                                 goto out;
2803                         if (iv_spc)
2804                                 build.hdr_len += IEEE80211_TKIP_IV_LEN;
2805                         break;
2806                 case WLAN_CIPHER_SUITE_WEP40:
2807                 case WLAN_CIPHER_SUITE_WEP104:
2808                         /* cannot handle IV generation in fast-xmit */
2809                         if (gen_iv)
2810                                 goto out;
2811                         if (iv_spc)
2812                                 build.hdr_len += IEEE80211_WEP_IV_LEN;
2813                         break;
2814                 case WLAN_CIPHER_SUITE_AES_CMAC:
2815                 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2816                 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2817                 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2818                         WARN(1,
2819                              "management cipher suite 0x%x enabled for data\n",
2820                              build.key->conf.cipher);
2821                         goto out;
2822                 default:
2823                         /* we don't know how to generate IVs for this at all */
2824                         if (WARN_ON(gen_iv))
2825                                 goto out;
2826                         /* pure hardware keys are OK, of course */
2827                         if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
2828                                 break;
2829                         /* cipher scheme might require space allocation */
2830                         if (iv_spc &&
2831                             build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
2832                                 goto out;
2833                         if (iv_spc)
2834                                 build.hdr_len += build.key->conf.iv_len;
2835                 }
2836
2837                 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
2838         }
2839
2840         hdr->frame_control = fc;
2841
2842         memcpy(build.hdr + build.hdr_len,
2843                rfc1042_header,  sizeof(rfc1042_header));
2844         build.hdr_len += sizeof(rfc1042_header);
2845
2846         fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
2847         /* if the kmemdup fails, continue w/o fast_tx */
2848         if (!fast_tx)
2849                 goto out;
2850
2851  out:
2852         /* we might have raced against another call to this function */
2853         old = rcu_dereference_protected(sta->fast_tx,
2854                                         lockdep_is_held(&sta->lock));
2855         rcu_assign_pointer(sta->fast_tx, fast_tx);
2856         if (old)
2857                 kfree_rcu(old, rcu_head);
2858         spin_unlock_bh(&sta->lock);
2859 }
2860
2861 void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
2862 {
2863         struct sta_info *sta;
2864
2865         rcu_read_lock();
2866         list_for_each_entry_rcu(sta, &local->sta_list, list)
2867                 ieee80211_check_fast_xmit(sta);
2868         rcu_read_unlock();
2869 }
2870
2871 void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
2872 {
2873         struct ieee80211_local *local = sdata->local;
2874         struct sta_info *sta;
2875
2876         rcu_read_lock();
2877
2878         list_for_each_entry_rcu(sta, &local->sta_list, list) {
2879                 if (sdata != sta->sdata &&
2880                     (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
2881                         continue;
2882                 ieee80211_check_fast_xmit(sta);
2883         }
2884
2885         rcu_read_unlock();
2886 }
2887
2888 void ieee80211_clear_fast_xmit(struct sta_info *sta)
2889 {
2890         struct ieee80211_fast_tx *fast_tx;
2891
2892         spin_lock_bh(&sta->lock);
2893         fast_tx = rcu_dereference_protected(sta->fast_tx,
2894                                             lockdep_is_held(&sta->lock));
2895         RCU_INIT_POINTER(sta->fast_tx, NULL);
2896         spin_unlock_bh(&sta->lock);
2897
2898         if (fast_tx)
2899                 kfree_rcu(fast_tx, rcu_head);
2900 }
2901
2902 static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
2903                                         struct sk_buff *skb, int headroom,
2904                                         int *subframe_len)
2905 {
2906         int amsdu_len = *subframe_len + sizeof(struct ethhdr);
2907         int padding = (4 - amsdu_len) & 3;
2908
2909         if (skb_headroom(skb) < headroom || skb_tailroom(skb) < padding) {
2910                 I802_DEBUG_INC(local->tx_expand_skb_head);
2911
2912                 if (pskb_expand_head(skb, headroom, padding, GFP_ATOMIC)) {
2913                         wiphy_debug(local->hw.wiphy,
2914                                     "failed to reallocate TX buffer\n");
2915                         return false;
2916                 }
2917         }
2918
2919         if (padding) {
2920                 *subframe_len += padding;
2921                 memset(skb_put(skb, padding), 0, padding);
2922         }
2923
2924         return true;
2925 }
2926
2927 static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
2928                                          struct ieee80211_fast_tx *fast_tx,
2929                                          struct sk_buff *skb)
2930 {
2931         struct ieee80211_local *local = sdata->local;
2932         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2933         struct ieee80211_hdr *hdr;
2934         struct ethhdr amsdu_hdr;
2935         int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
2936         int subframe_len = skb->len - hdr_len;
2937         void *data;
2938         u8 *qc;
2939
2940         if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
2941                 return false;
2942
2943         if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
2944                 return true;
2945
2946         if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(amsdu_hdr),
2947                                          &subframe_len))
2948                 return false;
2949
2950         amsdu_hdr.h_proto = cpu_to_be16(subframe_len);
2951         memcpy(amsdu_hdr.h_source, skb->data + fast_tx->sa_offs, ETH_ALEN);
2952         memcpy(amsdu_hdr.h_dest, skb->data + fast_tx->da_offs, ETH_ALEN);
2953
2954         data = skb_push(skb, sizeof(amsdu_hdr));
2955         memmove(data, data + sizeof(amsdu_hdr), hdr_len);
2956         memcpy(data + hdr_len, &amsdu_hdr, sizeof(amsdu_hdr));
2957
2958         hdr = data;
2959         qc = ieee80211_get_qos_ctl(hdr);
2960         *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
2961
2962         info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
2963
2964         return true;
2965 }
2966
2967 static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
2968                                       struct sta_info *sta,
2969                                       struct ieee80211_fast_tx *fast_tx,
2970                                       struct sk_buff *skb)
2971 {
2972         struct ieee80211_local *local = sdata->local;
2973         struct fq *fq = &local->fq;
2974         struct fq_tin *tin;
2975         struct fq_flow *flow;
2976         u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
2977         struct ieee80211_txq *txq = sta->sta.txq[tid];
2978         struct txq_info *txqi;
2979         struct sk_buff **frag_tail, *head;
2980         int subframe_len = skb->len - ETH_ALEN;
2981         u8 max_subframes = sta->sta.max_amsdu_subframes;
2982         int max_frags = local->hw.max_tx_fragments;
2983         int max_amsdu_len = sta->sta.max_amsdu_len;
2984         __be16 len;
2985         void *data;
2986         bool ret = false;
2987         unsigned int orig_len;
2988         int n = 1, nfrags;
2989
2990         if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
2991                 return false;
2992
2993         if (!txq)
2994                 return false;
2995
2996         txqi = to_txq_info(txq);
2997         if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
2998                 return false;
2999
3000         if (sta->sta.max_rc_amsdu_len)
3001                 max_amsdu_len = min_t(int, max_amsdu_len,
3002                                       sta->sta.max_rc_amsdu_len);
3003
3004         spin_lock_bh(&fq->lock);
3005
3006         /* TODO: Ideally aggregation should be done on dequeue to remain
3007          * responsive to environment changes.
3008          */
3009
3010         tin = &txqi->tin;
3011         flow = fq_flow_classify(fq, tin, skb, fq_flow_get_default_func);
3012         head = skb_peek_tail(&flow->queue);
3013         if (!head)
3014                 goto out;
3015
3016         orig_len = head->len;
3017
3018         if (skb->len + head->len > max_amsdu_len)
3019                 goto out;
3020
3021         if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
3022                 goto out;
3023
3024         nfrags = 1 + skb_shinfo(skb)->nr_frags;
3025         nfrags += 1 + skb_shinfo(head)->nr_frags;
3026         frag_tail = &skb_shinfo(head)->frag_list;
3027         while (*frag_tail) {
3028                 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3029                 frag_tail = &(*frag_tail)->next;
3030                 n++;
3031         }
3032
3033         if (max_subframes && n > max_subframes)
3034                 goto out;
3035
3036         if (max_frags && nfrags > max_frags)
3037                 goto out;
3038
3039         if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) + 2,
3040                                          &subframe_len))
3041                 goto out;
3042
3043         ret = true;
3044         data = skb_push(skb, ETH_ALEN + 2);
3045         memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3046
3047         data += 2 * ETH_ALEN;
3048         len = cpu_to_be16(subframe_len);
3049         memcpy(data, &len, 2);
3050         memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3051
3052         head->len += skb->len;
3053         head->data_len += skb->len;
3054         *frag_tail = skb;
3055
3056         flow->backlog += head->len - orig_len;
3057         tin->backlog_bytes += head->len - orig_len;
3058
3059         fq_recalc_backlog(fq, tin, flow);
3060
3061 out:
3062         spin_unlock_bh(&fq->lock);
3063
3064         return ret;
3065 }
3066
3067 static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
3068                                 struct net_device *dev, struct sta_info *sta,
3069                                 struct ieee80211_fast_tx *fast_tx,
3070                                 struct sk_buff *skb)
3071 {
3072         struct ieee80211_local *local = sdata->local;
3073         u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3074         int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3075         int hw_headroom = sdata->local->hw.extra_tx_headroom;
3076         struct ethhdr eth;
3077         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3078         struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3079         struct ieee80211_tx_data tx;
3080         ieee80211_tx_result r;
3081         struct tid_ampdu_tx *tid_tx = NULL;
3082         u8 tid = IEEE80211_NUM_TIDS;
3083
3084         /* control port protocol needs a lot of special handling */
3085         if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3086                 return false;
3087
3088         /* only RFC 1042 SNAP */
3089         if (ethertype < ETH_P_802_3_MIN)
3090                 return false;
3091
3092         /* don't handle TX status request here either */
3093         if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3094                 return false;
3095
3096         if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3097                 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3098                 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
3099                 if (tid_tx) {
3100                         if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3101                                 return false;
3102                         if (tid_tx->timeout)
3103                                 tid_tx->last_tx = jiffies;
3104                 }
3105         }
3106
3107         /* after this point (skb is modified) we cannot return false */
3108
3109         if (skb_shared(skb)) {
3110                 struct sk_buff *tmp_skb = skb;
3111
3112                 skb = skb_clone(skb, GFP_ATOMIC);
3113                 kfree_skb(tmp_skb);
3114
3115                 if (!skb)
3116                         return true;
3117         }
3118
3119         ieee80211_tx_stats(dev, skb->len + extra_head);
3120
3121         if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3122             ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3123                 return true;
3124
3125         /* will not be crypto-handled beyond what we do here, so use false
3126          * as the may-encrypt argument for the resize to not account for
3127          * more room than we already have in 'extra_head'
3128          */
3129         if (unlikely(ieee80211_skb_resize(sdata, skb,
3130                                           max_t(int, extra_head + hw_headroom -
3131                                                      skb_headroom(skb), 0),
3132                                           false))) {
3133                 kfree_skb(skb);
3134                 return true;
3135         }
3136
3137         memcpy(&eth, skb->data, ETH_HLEN - 2);
3138         hdr = (void *)skb_push(skb, extra_head);
3139         memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3140         memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3141         memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3142
3143         memset(info, 0, sizeof(*info));
3144         info->band = fast_tx->band;
3145         info->control.vif = &sdata->vif;
3146         info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3147                       IEEE80211_TX_CTL_DONTFRAG |
3148                       (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
3149
3150         if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3151                 *ieee80211_get_qos_ctl(hdr) = tid;
3152                 if (!sta->sta.txq[0])
3153                         hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3154         } else {
3155                 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3156                 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3157                 sdata->sequence_number += 0x10;
3158         }
3159
3160         if (skb_shinfo(skb)->gso_size)
3161                 sta->tx_stats.msdu[tid] +=
3162                         DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3163         else
3164                 sta->tx_stats.msdu[tid]++;
3165
3166         info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3167
3168         __skb_queue_head_init(&tx.skbs);
3169
3170         tx.flags = IEEE80211_TX_UNICAST;
3171         tx.local = local;
3172         tx.sdata = sdata;
3173         tx.sta = sta;
3174         tx.key = fast_tx->key;
3175
3176         if (fast_tx->key)
3177                 info->control.hw_key = &fast_tx->key->conf;
3178
3179         if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
3180                 tx.skb = skb;
3181                 r = ieee80211_tx_h_rate_ctrl(&tx);
3182                 skb = tx.skb;
3183                 tx.skb = NULL;
3184
3185                 if (r != TX_CONTINUE) {
3186                         if (r != TX_QUEUED)
3187                                 kfree_skb(skb);
3188                         return true;
3189                 }
3190         }
3191
3192         /* statistics normally done by ieee80211_tx_h_stats (but that
3193          * has to consider fragmentation, so is more complex)
3194          */
3195         sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3196         sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3197
3198         if (fast_tx->pn_offs) {
3199                 u64 pn;
3200                 u8 *crypto_hdr = skb->data + fast_tx->pn_offs;
3201
3202                 switch (fast_tx->key->conf.cipher) {
3203                 case WLAN_CIPHER_SUITE_CCMP:
3204                 case WLAN_CIPHER_SUITE_CCMP_256:
3205                 case WLAN_CIPHER_SUITE_GCMP:
3206                 case WLAN_CIPHER_SUITE_GCMP_256:
3207                         pn = atomic64_inc_return(&fast_tx->key->conf.tx_pn);
3208                         crypto_hdr[0] = pn;
3209                         crypto_hdr[1] = pn >> 8;
3210                         crypto_hdr[4] = pn >> 16;
3211                         crypto_hdr[5] = pn >> 24;
3212                         crypto_hdr[6] = pn >> 32;
3213                         crypto_hdr[7] = pn >> 40;
3214                         break;
3215                 }
3216         }
3217
3218         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3219                 sdata = container_of(sdata->bss,
3220                                      struct ieee80211_sub_if_data, u.ap);
3221
3222         __skb_queue_tail(&tx.skbs, skb);
3223         ieee80211_tx_frags(local, &sdata->vif, &sta->sta, &tx.skbs, false);
3224         return true;
3225 }
3226
3227 void __ieee80211_subif_start_xmit(struct sk_buff *skb,
3228                                   struct net_device *dev,
3229                                   u32 info_flags)
3230 {
3231         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3232         struct sta_info *sta;
3233         struct sk_buff *next;
3234
3235         if (unlikely(skb->len < ETH_HLEN)) {
3236                 kfree_skb(skb);
3237                 return;
3238         }
3239
3240         rcu_read_lock();
3241
3242         if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
3243                 goto out_free;
3244
3245         if (!IS_ERR_OR_NULL(sta)) {
3246                 struct ieee80211_fast_tx *fast_tx;
3247
3248                 fast_tx = rcu_dereference(sta->fast_tx);
3249
3250                 if (fast_tx &&
3251                     ieee80211_xmit_fast(sdata, dev, sta, fast_tx, skb))
3252                         goto out;
3253         }
3254
3255         if (skb_is_gso(skb)) {
3256                 struct sk_buff *segs;
3257
3258                 segs = skb_gso_segment(skb, 0);
3259                 if (IS_ERR(segs)) {
3260                         goto out_free;
3261                 } else if (segs) {
3262                         consume_skb(skb);
3263                         skb = segs;
3264                 }
3265         } else {
3266                 /* we cannot process non-linear frames on this path */
3267                 if (skb_linearize(skb)) {
3268                         kfree_skb(skb);
3269                         goto out;
3270                 }
3271
3272                 /* the frame could be fragmented, software-encrypted, and other
3273                  * things so we cannot really handle checksum offload with it -
3274                  * fix it up in software before we handle anything else.
3275                  */
3276                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3277                         skb_set_transport_header(skb,
3278                                                  skb_checksum_start_offset(skb));
3279                         if (skb_checksum_help(skb))
3280                                 goto out_free;
3281                 }
3282         }
3283
3284         next = skb;
3285         while (next) {
3286                 skb = next;
3287                 next = skb->next;
3288
3289                 skb->prev = NULL;
3290                 skb->next = NULL;
3291
3292                 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta);
3293                 if (IS_ERR(skb))
3294                         goto out;
3295
3296                 ieee80211_tx_stats(dev, skb->len);
3297
3298                 ieee80211_xmit(sdata, sta, skb);
3299         }
3300         goto out;
3301  out_free:
3302         kfree_skb(skb);
3303  out:
3304         rcu_read_unlock();
3305 }
3306
3307 /**
3308  * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
3309  * @skb: packet to be sent
3310  * @dev: incoming interface
3311  *
3312  * On failure skb will be freed.
3313  */
3314 netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
3315                                        struct net_device *dev)
3316 {
3317         __ieee80211_subif_start_xmit(skb, dev, 0);
3318         return NETDEV_TX_OK;
3319 }
3320
3321 struct sk_buff *
3322 ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
3323                               struct sk_buff *skb, u32 info_flags)
3324 {
3325         struct ieee80211_hdr *hdr;
3326         struct ieee80211_tx_data tx = {
3327                 .local = sdata->local,
3328                 .sdata = sdata,
3329         };
3330         struct sta_info *sta;
3331
3332         rcu_read_lock();
3333
3334         if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
3335                 kfree_skb(skb);
3336                 skb = ERR_PTR(-EINVAL);
3337                 goto out;
3338         }
3339
3340         skb = ieee80211_build_hdr(sdata, skb, info_flags, sta);
3341         if (IS_ERR(skb))
3342                 goto out;
3343
3344         hdr = (void *)skb->data;
3345         tx.sta = sta_info_get(sdata, hdr->addr1);
3346         tx.skb = skb;
3347
3348         if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
3349                 rcu_read_unlock();
3350                 kfree_skb(skb);
3351                 return ERR_PTR(-EINVAL);
3352         }
3353
3354 out:
3355         rcu_read_unlock();
3356         return skb;
3357 }
3358
3359 /*
3360  * ieee80211_clear_tx_pending may not be called in a context where
3361  * it is possible that it packets could come in again.
3362  */
3363 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
3364 {
3365         struct sk_buff *skb;
3366         int i;
3367
3368         for (i = 0; i < local->hw.queues; i++) {
3369                 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
3370                         ieee80211_free_txskb(&local->hw, skb);
3371         }
3372 }
3373
3374 /*
3375  * Returns false if the frame couldn't be transmitted but was queued instead,
3376  * which in this case means re-queued -- take as an indication to stop sending
3377  * more pending frames.
3378  */
3379 static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
3380                                      struct sk_buff *skb)
3381 {
3382         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3383         struct ieee80211_sub_if_data *sdata;
3384         struct sta_info *sta;
3385         struct ieee80211_hdr *hdr;
3386         bool result;
3387         struct ieee80211_chanctx_conf *chanctx_conf;
3388
3389         sdata = vif_to_sdata(info->control.vif);
3390
3391         if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
3392                 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3393                 if (unlikely(!chanctx_conf)) {
3394                         dev_kfree_skb(skb);
3395                         return true;
3396                 }
3397                 info->band = chanctx_conf->def.chan->band;
3398                 result = ieee80211_tx(sdata, NULL, skb, true);
3399         } else {
3400                 struct sk_buff_head skbs;
3401
3402                 __skb_queue_head_init(&skbs);
3403                 __skb_queue_tail(&skbs, skb);
3404
3405                 hdr = (struct ieee80211_hdr *)skb->data;
3406                 sta = sta_info_get(sdata, hdr->addr1);
3407
3408                 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
3409         }
3410
3411         return result;
3412 }
3413
3414 /*
3415  * Transmit all pending packets. Called from tasklet.
3416  */
3417 void ieee80211_tx_pending(unsigned long data)
3418 {
3419         struct ieee80211_local *local = (struct ieee80211_local *)data;
3420         unsigned long flags;
3421         int i;
3422         bool txok;
3423
3424         rcu_read_lock();
3425
3426         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
3427         for (i = 0; i < local->hw.queues; i++) {
3428                 /*
3429                  * If queue is stopped by something other than due to pending
3430                  * frames, or we have no pending frames, proceed to next queue.
3431                  */
3432                 if (local->queue_stop_reasons[i] ||
3433                     skb_queue_empty(&local->pending[i]))
3434                         continue;
3435
3436                 while (!skb_queue_empty(&local->pending[i])) {
3437                         struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
3438                         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3439
3440                         if (WARN_ON(!info->control.vif)) {
3441                                 ieee80211_free_txskb(&local->hw, skb);
3442                                 continue;
3443                         }
3444
3445                         spin_unlock_irqrestore(&local->queue_stop_reason_lock,
3446                                                 flags);
3447
3448                         txok = ieee80211_tx_pending_skb(local, skb);
3449                         spin_lock_irqsave(&local->queue_stop_reason_lock,
3450                                           flags);
3451                         if (!txok)
3452                                 break;
3453                 }
3454
3455                 if (skb_queue_empty(&local->pending[i]))
3456                         ieee80211_propagate_queue_wake(local, i);
3457         }
3458         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
3459
3460         rcu_read_unlock();
3461 }
3462
3463 /* functions for drivers to get certain frames */
3464
3465 static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
3466                                        struct ps_data *ps, struct sk_buff *skb,
3467                                        bool is_template)
3468 {
3469         u8 *pos, *tim;
3470         int aid0 = 0;
3471         int i, have_bits = 0, n1, n2;
3472
3473         /* Generate bitmap for TIM only if there are any STAs in power save
3474          * mode. */
3475         if (atomic_read(&ps->num_sta_ps) > 0)
3476                 /* in the hope that this is faster than
3477                  * checking byte-for-byte */
3478                 have_bits = !bitmap_empty((unsigned long *)ps->tim,
3479                                           IEEE80211_MAX_AID+1);
3480         if (!is_template) {
3481                 if (ps->dtim_count == 0)
3482                         ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
3483                 else
3484                         ps->dtim_count--;
3485         }
3486
3487         tim = pos = (u8 *) skb_put(skb, 6);
3488         *pos++ = WLAN_EID_TIM;
3489         *pos++ = 4;
3490         *pos++ = ps->dtim_count;
3491         *pos++ = sdata->vif.bss_conf.dtim_period;
3492
3493         if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
3494                 aid0 = 1;
3495
3496         ps->dtim_bc_mc = aid0 == 1;
3497
3498         if (have_bits) {
3499                 /* Find largest even number N1 so that bits numbered 1 through
3500                  * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
3501                  * (N2 + 1) x 8 through 2007 are 0. */
3502                 n1 = 0;
3503                 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
3504                         if (ps->tim[i]) {
3505                                 n1 = i & 0xfe;
3506                                 break;
3507                         }
3508                 }
3509                 n2 = n1;
3510                 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
3511                         if (ps->tim[i]) {
3512                                 n2 = i;
3513                                 break;
3514                         }
3515                 }
3516
3517                 /* Bitmap control */
3518                 *pos++ = n1 | aid0;
3519                 /* Part Virt Bitmap */
3520                 skb_put(skb, n2 - n1);
3521                 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
3522
3523                 tim[1] = n2 - n1 + 4;
3524         } else {
3525                 *pos++ = aid0; /* Bitmap control */
3526                 *pos++ = 0; /* Part Virt Bitmap */
3527         }
3528 }
3529
3530 static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
3531                                     struct ps_data *ps, struct sk_buff *skb,
3532                                     bool is_template)
3533 {
3534         struct ieee80211_local *local = sdata->local;
3535
3536         /*
3537          * Not very nice, but we want to allow the driver to call
3538          * ieee80211_beacon_get() as a response to the set_tim()
3539          * callback. That, however, is already invoked under the
3540          * sta_lock to guarantee consistent and race-free update
3541          * of the tim bitmap in mac80211 and the driver.
3542          */
3543         if (local->tim_in_locked_section) {
3544                 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
3545         } else {
3546                 spin_lock_bh(&local->tim_lock);
3547                 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
3548                 spin_unlock_bh(&local->tim_lock);
3549         }
3550
3551         return 0;
3552 }
3553
3554 static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
3555                               struct beacon_data *beacon)
3556 {
3557         struct probe_resp *resp;
3558         u8 *beacon_data;
3559         size_t beacon_data_len;
3560         int i;
3561         u8 count = beacon->csa_current_counter;
3562
3563         switch (sdata->vif.type) {
3564         case NL80211_IFTYPE_AP:
3565                 beacon_data = beacon->tail;
3566                 beacon_data_len = beacon->tail_len;
3567                 break;
3568         case NL80211_IFTYPE_ADHOC:
3569                 beacon_data = beacon->head;
3570                 beacon_data_len = beacon->head_len;
3571                 break;
3572         case NL80211_IFTYPE_MESH_POINT:
3573                 beacon_data = beacon->head;
3574                 beacon_data_len = beacon->head_len;
3575                 break;
3576         default:
3577                 return;
3578         }
3579
3580         rcu_read_lock();
3581         for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
3582                 resp = rcu_dereference(sdata->u.ap.probe_resp);
3583
3584                 if (beacon->csa_counter_offsets[i]) {
3585                         if (WARN_ON_ONCE(beacon->csa_counter_offsets[i] >=
3586                                          beacon_data_len)) {
3587                                 rcu_read_unlock();
3588                                 return;
3589                         }
3590
3591                         beacon_data[beacon->csa_counter_offsets[i]] = count;
3592                 }
3593
3594                 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
3595                         resp->data[resp->csa_counter_offsets[i]] = count;
3596         }
3597         rcu_read_unlock();
3598 }
3599
3600 static u8 __ieee80211_csa_update_counter(struct beacon_data *beacon)
3601 {
3602         beacon->csa_current_counter--;
3603
3604         /* the counter should never reach 0 */
3605         WARN_ON_ONCE(!beacon->csa_current_counter);
3606
3607         return beacon->csa_current_counter;
3608 }
3609
3610 u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
3611 {
3612         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3613         struct beacon_data *beacon = NULL;
3614         u8 count = 0;
3615
3616         rcu_read_lock();
3617
3618         if (sdata->vif.type == NL80211_IFTYPE_AP)
3619                 beacon = rcu_dereference(sdata->u.ap.beacon);
3620         else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
3621                 beacon = rcu_dereference(sdata->u.ibss.presp);
3622         else if (ieee80211_vif_is_mesh(&sdata->vif))
3623                 beacon = rcu_dereference(sdata->u.mesh.beacon);
3624
3625         if (!beacon)
3626                 goto unlock;
3627
3628         count = __ieee80211_csa_update_counter(beacon);
3629
3630 unlock:
3631         rcu_read_unlock();
3632         return count;
3633 }
3634 EXPORT_SYMBOL(ieee80211_csa_update_counter);
3635
3636 bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
3637 {
3638         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3639         struct beacon_data *beacon = NULL;
3640         u8 *beacon_data;
3641         size_t beacon_data_len;
3642         int ret = false;
3643
3644         if (!ieee80211_sdata_running(sdata))
3645                 return false;
3646
3647         rcu_read_lock();
3648         if (vif->type == NL80211_IFTYPE_AP) {
3649                 struct ieee80211_if_ap *ap = &sdata->u.ap;
3650
3651                 beacon = rcu_dereference(ap->beacon);
3652                 if (WARN_ON(!beacon || !beacon->tail))
3653                         goto out;
3654                 beacon_data = beacon->tail;
3655                 beacon_data_len = beacon->tail_len;
3656         } else if (vif->type == NL80211_IFTYPE_ADHOC) {
3657                 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
3658
3659                 beacon = rcu_dereference(ifibss->presp);
3660                 if (!beacon)
3661                         goto out;
3662
3663                 beacon_data = beacon->head;
3664                 beacon_data_len = beacon->head_len;
3665         } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
3666                 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
3667
3668                 beacon = rcu_dereference(ifmsh->beacon);
3669                 if (!beacon)
3670                         goto out;
3671
3672                 beacon_data = beacon->head;
3673                 beacon_data_len = beacon->head_len;
3674         } else {
3675                 WARN_ON(1);
3676                 goto out;
3677         }
3678
3679         if (!beacon->csa_counter_offsets[0])
3680                 goto out;
3681
3682         if (WARN_ON_ONCE(beacon->csa_counter_offsets[0] > beacon_data_len))
3683                 goto out;
3684
3685         if (beacon_data[beacon->csa_counter_offsets[0]] == 1)
3686                 ret = true;
3687  out:
3688         rcu_read_unlock();
3689
3690         return ret;
3691 }
3692 EXPORT_SYMBOL(ieee80211_csa_is_complete);
3693
3694 static struct sk_buff *
3695 __ieee80211_beacon_get(struct ieee80211_hw *hw,
3696                        struct ieee80211_vif *vif,
3697                        struct ieee80211_mutable_offsets *offs,
3698                        bool is_template)
3699 {
3700         struct ieee80211_local *local = hw_to_local(hw);
3701         struct beacon_data *beacon = NULL;
3702         struct sk_buff *skb = NULL;
3703         struct ieee80211_tx_info *info;
3704         struct ieee80211_sub_if_data *sdata = NULL;
3705         enum nl80211_band band;
3706         struct ieee80211_tx_rate_control txrc;
3707         struct ieee80211_chanctx_conf *chanctx_conf;
3708         int csa_off_base = 0;
3709
3710         rcu_read_lock();
3711
3712         sdata = vif_to_sdata(vif);
3713         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
3714
3715         if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
3716                 goto out;
3717
3718         if (offs)
3719                 memset(offs, 0, sizeof(*offs));
3720
3721         if (sdata->vif.type == NL80211_IFTYPE_AP) {
3722                 struct ieee80211_if_ap *ap = &sdata->u.ap;
3723
3724                 beacon = rcu_dereference(ap->beacon);
3725                 if (beacon) {
3726                         if (beacon->csa_counter_offsets[0]) {
3727                                 if (!is_template)
3728                                         __ieee80211_csa_update_counter(beacon);
3729
3730                                 ieee80211_set_csa(sdata, beacon);
3731                         }
3732
3733                         /*
3734                          * headroom, head length,
3735                          * tail length and maximum TIM length
3736                          */
3737                         skb = dev_alloc_skb(local->tx_headroom +
3738                                             beacon->head_len +
3739                                             beacon->tail_len + 256 +
3740                                             local->hw.extra_beacon_tailroom);
3741                         if (!skb)
3742                                 goto out;
3743
3744                         skb_reserve(skb, local->tx_headroom);
3745                         memcpy(skb_put(skb, beacon->head_len), beacon->head,
3746                                beacon->head_len);
3747
3748                         ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
3749                                                  is_template);
3750
3751                         if (offs) {
3752                                 offs->tim_offset = beacon->head_len;
3753                                 offs->tim_length = skb->len - beacon->head_len;
3754
3755                                 /* for AP the csa offsets are from tail */
3756                                 csa_off_base = skb->len;
3757                         }
3758
3759                         if (beacon->tail)
3760                                 memcpy(skb_put(skb, beacon->tail_len),
3761                                        beacon->tail, beacon->tail_len);
3762                 } else
3763                         goto out;
3764         } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
3765                 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
3766                 struct ieee80211_hdr *hdr;
3767
3768                 beacon = rcu_dereference(ifibss->presp);
3769                 if (!beacon)
3770                         goto out;
3771
3772                 if (beacon->csa_counter_offsets[0]) {
3773                         if (!is_template)
3774                                 __ieee80211_csa_update_counter(beacon);
3775
3776                         ieee80211_set_csa(sdata, beacon);
3777                 }
3778
3779                 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
3780                                     local->hw.extra_beacon_tailroom);
3781                 if (!skb)
3782                         goto out;
3783                 skb_reserve(skb, local->tx_headroom);
3784                 memcpy(skb_put(skb, beacon->head_len), beacon->head,
3785                        beacon->head_len);
3786
3787                 hdr = (struct ieee80211_hdr *) skb->data;
3788                 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3789                                                  IEEE80211_STYPE_BEACON);
3790         } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
3791                 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
3792
3793                 beacon = rcu_dereference(ifmsh->beacon);
3794                 if (!beacon)
3795                         goto out;
3796
3797                 if (beacon->csa_counter_offsets[0]) {
3798                         if (!is_template)
3799                                 /* TODO: For mesh csa_counter is in TU, so
3800                                  * decrementing it by one isn't correct, but
3801                                  * for now we leave it consistent with overall
3802                                  * mac80211's behavior.
3803                                  */
3804                                 __ieee80211_csa_update_counter(beacon);
3805
3806                         ieee80211_set_csa(sdata, beacon);
3807                 }
3808
3809                 if (ifmsh->sync_ops)
3810                         ifmsh->sync_ops->adjust_tbtt(sdata, beacon);
3811
3812                 skb = dev_alloc_skb(local->tx_headroom +
3813                                     beacon->head_len +
3814                                     256 + /* TIM IE */
3815                                     beacon->tail_len +
3816                                     local->hw.extra_beacon_tailroom);
3817                 if (!skb)
3818                         goto out;
3819                 skb_reserve(skb, local->tx_headroom);
3820                 memcpy(skb_put(skb, beacon->head_len), beacon->head,
3821                        beacon->head_len);
3822                 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
3823
3824                 if (offs) {
3825                         offs->tim_offset = beacon->head_len;
3826                         offs->tim_length = skb->len - beacon->head_len;
3827                 }
3828
3829                 memcpy(skb_put(skb, beacon->tail_len), beacon->tail,
3830                        beacon->tail_len);
3831         } else {
3832                 WARN_ON(1);
3833                 goto out;
3834         }
3835
3836         /* CSA offsets */
3837         if (offs && beacon) {
3838                 int i;
3839
3840                 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
3841                         u16 csa_off = beacon->csa_counter_offsets[i];
3842
3843                         if (!csa_off)
3844                                 continue;
3845
3846                         offs->csa_counter_offs[i] = csa_off_base + csa_off;
3847                 }
3848         }
3849
3850         band = chanctx_conf->def.chan->band;
3851
3852         info = IEEE80211_SKB_CB(skb);
3853
3854         info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
3855         info->flags |= IEEE80211_TX_CTL_NO_ACK;
3856         info->band = band;
3857
3858         memset(&txrc, 0, sizeof(txrc));
3859         txrc.hw = hw;
3860         txrc.sband = local->hw.wiphy->bands[band];
3861         txrc.bss_conf = &sdata->vif.bss_conf;
3862         txrc.skb = skb;
3863         txrc.reported_rate.idx = -1;
3864         txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
3865         if (txrc.rate_idx_mask == (1 << txrc.sband->n_bitrates) - 1)
3866                 txrc.max_rate_idx = -1;
3867         else
3868                 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
3869         txrc.bss = true;
3870         rate_control_get_rate(sdata, NULL, &txrc);
3871
3872         info->control.vif = vif;
3873
3874         info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
3875                         IEEE80211_TX_CTL_ASSIGN_SEQ |
3876                         IEEE80211_TX_CTL_FIRST_FRAGMENT;
3877  out:
3878         rcu_read_unlock();
3879         return skb;
3880
3881 }
3882
3883 struct sk_buff *
3884 ieee80211_beacon_get_template(struct ieee80211_hw *hw,
3885                               struct ieee80211_vif *vif,
3886                               struct ieee80211_mutable_offsets *offs)
3887 {
3888         return __ieee80211_beacon_get(hw, vif, offs, true);
3889 }
3890 EXPORT_SYMBOL(ieee80211_beacon_get_template);
3891
3892 struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
3893                                          struct ieee80211_vif *vif,
3894                                          u16 *tim_offset, u16 *tim_length)
3895 {
3896         struct ieee80211_mutable_offsets offs = {};
3897         struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
3898         struct sk_buff *copy;
3899         struct ieee80211_supported_band *sband;
3900         int shift;
3901
3902         if (!bcn)
3903                 return bcn;
3904
3905         if (tim_offset)
3906                 *tim_offset = offs.tim_offset;
3907
3908         if (tim_length)
3909                 *tim_length = offs.tim_length;
3910
3911         if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
3912             !hw_to_local(hw)->monitors)
3913                 return bcn;
3914
3915         /* send a copy to monitor interfaces */
3916         copy = skb_copy(bcn, GFP_ATOMIC);
3917         if (!copy)
3918                 return bcn;
3919
3920         shift = ieee80211_vif_get_shift(vif);
3921         sband = hw->wiphy->bands[ieee80211_get_sdata_band(vif_to_sdata(vif))];
3922         ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false);
3923
3924         return bcn;
3925 }
3926 EXPORT_SYMBOL(ieee80211_beacon_get_tim);
3927
3928 struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
3929                                         struct ieee80211_vif *vif)
3930 {
3931         struct ieee80211_if_ap *ap = NULL;
3932         struct sk_buff *skb = NULL;
3933         struct probe_resp *presp = NULL;
3934         struct ieee80211_hdr *hdr;
3935         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
3936
3937         if (sdata->vif.type != NL80211_IFTYPE_AP)
3938                 return NULL;
3939
3940         rcu_read_lock();
3941
3942         ap = &sdata->u.ap;
3943         presp = rcu_dereference(ap->probe_resp);
3944         if (!presp)
3945                 goto out;
3946
3947         skb = dev_alloc_skb(presp->len);
3948         if (!skb)
3949                 goto out;
3950
3951         memcpy(skb_put(skb, presp->len), presp->data, presp->len);
3952
3953         hdr = (struct ieee80211_hdr *) skb->data;
3954         memset(hdr->addr1, 0, sizeof(hdr->addr1));
3955
3956 out:
3957         rcu_read_unlock();
3958         return skb;
3959 }
3960 EXPORT_SYMBOL(ieee80211_proberesp_get);
3961
3962 struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
3963                                      struct ieee80211_vif *vif)
3964 {
3965         struct ieee80211_sub_if_data *sdata;
3966         struct ieee80211_if_managed *ifmgd;
3967         struct ieee80211_pspoll *pspoll;
3968         struct ieee80211_local *local;
3969         struct sk_buff *skb;
3970
3971         if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
3972                 return NULL;
3973
3974         sdata = vif_to_sdata(vif);
3975         ifmgd = &sdata->u.mgd;
3976         local = sdata->local;
3977
3978         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
3979         if (!skb)
3980                 return NULL;
3981
3982         skb_reserve(skb, local->hw.extra_tx_headroom);
3983
3984         pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
3985         memset(pspoll, 0, sizeof(*pspoll));
3986         pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
3987                                             IEEE80211_STYPE_PSPOLL);
3988         pspoll->aid = cpu_to_le16(ifmgd->aid);
3989
3990         /* aid in PS-Poll has its two MSBs each set to 1 */
3991         pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
3992
3993         memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
3994         memcpy(pspoll->ta, vif->addr, ETH_ALEN);
3995
3996         return skb;
3997 }
3998 EXPORT_SYMBOL(ieee80211_pspoll_get);
3999
4000 struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
4001                                        struct ieee80211_vif *vif)
4002 {
4003         struct ieee80211_hdr_3addr *nullfunc;
4004         struct ieee80211_sub_if_data *sdata;
4005         struct ieee80211_if_managed *ifmgd;
4006         struct ieee80211_local *local;
4007         struct sk_buff *skb;
4008
4009         if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
4010                 return NULL;
4011
4012         sdata = vif_to_sdata(vif);
4013         ifmgd = &sdata->u.mgd;
4014         local = sdata->local;
4015
4016         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*nullfunc));
4017         if (!skb)
4018                 return NULL;
4019
4020         skb_reserve(skb, local->hw.extra_tx_headroom);
4021
4022         nullfunc = (struct ieee80211_hdr_3addr *) skb_put(skb,
4023                                                           sizeof(*nullfunc));
4024         memset(nullfunc, 0, sizeof(*nullfunc));
4025         nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
4026                                               IEEE80211_STYPE_NULLFUNC |
4027                                               IEEE80211_FCTL_TODS);
4028         memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
4029         memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
4030         memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
4031
4032         return skb;
4033 }
4034 EXPORT_SYMBOL(ieee80211_nullfunc_get);
4035
4036 struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
4037                                        const u8 *src_addr,
4038                                        const u8 *ssid, size_t ssid_len,
4039                                        size_t tailroom)
4040 {
4041         struct ieee80211_local *local = hw_to_local(hw);
4042         struct ieee80211_hdr_3addr *hdr;
4043         struct sk_buff *skb;
4044         size_t ie_ssid_len;
4045         u8 *pos;
4046
4047         ie_ssid_len = 2 + ssid_len;
4048
4049         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
4050                             ie_ssid_len + tailroom);
4051         if (!skb)
4052                 return NULL;
4053
4054         skb_reserve(skb, local->hw.extra_tx_headroom);
4055
4056         hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
4057         memset(hdr, 0, sizeof(*hdr));
4058         hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4059                                          IEEE80211_STYPE_PROBE_REQ);
4060         eth_broadcast_addr(hdr->addr1);
4061         memcpy(hdr->addr2, src_addr, ETH_ALEN);
4062         eth_broadcast_addr(hdr->addr3);
4063
4064         pos = skb_put(skb, ie_ssid_len);
4065         *pos++ = WLAN_EID_SSID;
4066         *pos++ = ssid_len;
4067         if (ssid_len)
4068                 memcpy(pos, ssid, ssid_len);
4069         pos += ssid_len;
4070
4071         return skb;
4072 }
4073 EXPORT_SYMBOL(ieee80211_probereq_get);
4074
4075 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4076                        const void *frame, size_t frame_len,
4077                        const struct ieee80211_tx_info *frame_txctl,
4078                        struct ieee80211_rts *rts)
4079 {
4080         const struct ieee80211_hdr *hdr = frame;
4081
4082         rts->frame_control =
4083             cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
4084         rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
4085                                                frame_txctl);
4086         memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
4087         memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
4088 }
4089 EXPORT_SYMBOL(ieee80211_rts_get);
4090
4091 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4092                              const void *frame, size_t frame_len,
4093                              const struct ieee80211_tx_info *frame_txctl,
4094                              struct ieee80211_cts *cts)
4095 {
4096         const struct ieee80211_hdr *hdr = frame;
4097
4098         cts->frame_control =
4099             cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
4100         cts->duration = ieee80211_ctstoself_duration(hw, vif,
4101                                                      frame_len, frame_txctl);
4102         memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
4103 }
4104 EXPORT_SYMBOL(ieee80211_ctstoself_get);
4105
4106 struct sk_buff *
4107 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
4108                           struct ieee80211_vif *vif)
4109 {
4110         struct ieee80211_local *local = hw_to_local(hw);
4111         struct sk_buff *skb = NULL;
4112         struct ieee80211_tx_data tx;
4113         struct ieee80211_sub_if_data *sdata;
4114         struct ps_data *ps;
4115         struct ieee80211_tx_info *info;
4116         struct ieee80211_chanctx_conf *chanctx_conf;
4117
4118         sdata = vif_to_sdata(vif);
4119
4120         rcu_read_lock();
4121         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4122
4123         if (!chanctx_conf)
4124                 goto out;
4125
4126         if (sdata->vif.type == NL80211_IFTYPE_AP) {
4127                 struct beacon_data *beacon =
4128                                 rcu_dereference(sdata->u.ap.beacon);
4129
4130                 if (!beacon || !beacon->head)
4131                         goto out;
4132
4133                 ps = &sdata->u.ap.ps;
4134         } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4135                 ps = &sdata->u.mesh.ps;
4136         } else {
4137                 goto out;
4138         }
4139
4140         if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
4141                 goto out; /* send buffered bc/mc only after DTIM beacon */
4142
4143         while (1) {
4144                 skb = skb_dequeue(&ps->bc_buf);
4145                 if (!skb)
4146                         goto out;
4147                 local->total_ps_buffered--;
4148
4149                 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
4150                         struct ieee80211_hdr *hdr =
4151                                 (struct ieee80211_hdr *) skb->data;
4152                         /* more buffered multicast/broadcast frames ==> set
4153                          * MoreData flag in IEEE 802.11 header to inform PS
4154                          * STAs */
4155                         hdr->frame_control |=
4156                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
4157                 }
4158
4159                 if (sdata->vif.type == NL80211_IFTYPE_AP)
4160                         sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
4161                 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
4162                         break;
4163                 dev_kfree_skb_any(skb);
4164         }
4165
4166         info = IEEE80211_SKB_CB(skb);
4167
4168         tx.flags |= IEEE80211_TX_PS_BUFFERED;
4169         info->band = chanctx_conf->def.chan->band;
4170
4171         if (invoke_tx_handlers(&tx))
4172                 skb = NULL;
4173  out:
4174         rcu_read_unlock();
4175
4176         return skb;
4177 }
4178 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
4179
4180 int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
4181 {
4182         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
4183         struct ieee80211_sub_if_data *sdata = sta->sdata;
4184         struct ieee80211_local *local = sdata->local;
4185         int ret;
4186         u32 queues;
4187
4188         lockdep_assert_held(&local->sta_mtx);
4189
4190         /* only some cases are supported right now */
4191         switch (sdata->vif.type) {
4192         case NL80211_IFTYPE_STATION:
4193         case NL80211_IFTYPE_AP:
4194         case NL80211_IFTYPE_AP_VLAN:
4195                 break;
4196         default:
4197                 WARN_ON(1);
4198                 return -EINVAL;
4199         }
4200
4201         if (WARN_ON(tid >= IEEE80211_NUM_UPS))
4202                 return -EINVAL;
4203
4204         if (sta->reserved_tid == tid) {
4205                 ret = 0;
4206                 goto out;
4207         }
4208
4209         if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
4210                 sdata_err(sdata, "TID reservation already active\n");
4211                 ret = -EALREADY;
4212                 goto out;
4213         }
4214
4215         ieee80211_stop_vif_queues(sdata->local, sdata,
4216                                   IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
4217
4218         synchronize_net();
4219
4220         /* Tear down BA sessions so we stop aggregating on this TID */
4221         if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
4222                 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
4223                 __ieee80211_stop_tx_ba_session(sta, tid,
4224                                                AGG_STOP_LOCAL_REQUEST);
4225         }
4226
4227         queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
4228         __ieee80211_flush_queues(local, sdata, queues, false);
4229
4230         sta->reserved_tid = tid;
4231
4232         ieee80211_wake_vif_queues(local, sdata,
4233                                   IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
4234
4235         if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
4236                 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
4237
4238         ret = 0;
4239  out:
4240         return ret;
4241 }
4242 EXPORT_SYMBOL(ieee80211_reserve_tid);
4243
4244 void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
4245 {
4246         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
4247         struct ieee80211_sub_if_data *sdata = sta->sdata;
4248
4249         lockdep_assert_held(&sdata->local->sta_mtx);
4250
4251         /* only some cases are supported right now */
4252         switch (sdata->vif.type) {
4253         case NL80211_IFTYPE_STATION:
4254         case NL80211_IFTYPE_AP:
4255         case NL80211_IFTYPE_AP_VLAN:
4256                 break;
4257         default:
4258                 WARN_ON(1);
4259                 return;
4260         }
4261
4262         if (tid != sta->reserved_tid) {
4263                 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
4264                 return;
4265         }
4266
4267         sta->reserved_tid = IEEE80211_TID_UNRESERVED;
4268 }
4269 EXPORT_SYMBOL(ieee80211_unreserve_tid);
4270
4271 void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
4272                                  struct sk_buff *skb, int tid,
4273                                  enum nl80211_band band)
4274 {
4275         int ac = ieee802_1d_to_ac[tid & 7];
4276
4277         skb_reset_mac_header(skb);
4278         skb_reset_network_header(skb);
4279         skb_reset_transport_header(skb);
4280
4281         skb_set_queue_mapping(skb, ac);
4282         skb->priority = tid;
4283
4284         skb->dev = sdata->dev;
4285
4286         /*
4287          * The other path calling ieee80211_xmit is from the tasklet,
4288          * and while we can handle concurrent transmissions locking
4289          * requirements are that we do not come into tx with bhs on.
4290          */
4291         local_bh_disable();
4292         IEEE80211_SKB_CB(skb)->band = band;
4293         ieee80211_xmit(sdata, NULL, skb);
4294         local_bh_enable();
4295 }