Merge branch 'i2c/for-current' of git://git.kernel.org/pub/scm/linux/kernel/git/wsa...
[linux-2.6-microblaze.git] / net / mac80211 / tx.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2002-2005, Instant802 Networks, Inc.
4  * Copyright 2005-2006, Devicescape Software, Inc.
5  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
6  * Copyright 2007       Johannes Berg <johannes@sipsolutions.net>
7  * Copyright 2013-2014  Intel Mobile Communications GmbH
8  * Copyright (C) 2018-2020 Intel Corporation
9  *
10  * Transmit and frame generation functions.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/skbuff.h>
16 #include <linux/if_vlan.h>
17 #include <linux/etherdevice.h>
18 #include <linux/bitmap.h>
19 #include <linux/rcupdate.h>
20 #include <linux/export.h>
21 #include <net/net_namespace.h>
22 #include <net/ieee80211_radiotap.h>
23 #include <net/cfg80211.h>
24 #include <net/mac80211.h>
25 #include <net/codel.h>
26 #include <net/codel_impl.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         /* assume HW handles this */
65         if (tx->rate.flags & (IEEE80211_TX_RC_MCS | IEEE80211_TX_RC_VHT_MCS))
66                 return 0;
67
68         rcu_read_lock();
69         chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
70         if (chanctx_conf) {
71                 shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
72                 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
73         }
74         rcu_read_unlock();
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         /* device is expected to do this */
86         if (sband->band == NL80211_BAND_S1GHZ)
87                 return 0;
88
89         /*
90          * data and mgmt (except PS Poll):
91          * - during CFP: 32768
92          * - during contention period:
93          *   if addr1 is group address: 0
94          *   if more fragments = 0 and addr1 is individual address: time to
95          *      transmit one ACK plus SIFS
96          *   if more fragments = 1 and addr1 is individual address: time to
97          *      transmit next fragment plus 2 x ACK plus 3 x SIFS
98          *
99          * IEEE 802.11, 9.6:
100          * - control response frame (CTS or ACK) shall be transmitted using the
101          *   same rate as the immediately previous frame in the frame exchange
102          *   sequence, if this rate belongs to the PHY mandatory rates, or else
103          *   at the highest possible rate belonging to the PHY rates in the
104          *   BSSBasicRateSet
105          */
106         hdr = (struct ieee80211_hdr *)skb->data;
107         if (ieee80211_is_ctl(hdr->frame_control)) {
108                 /* TODO: These control frames are not currently sent by
109                  * mac80211, but should they be implemented, this function
110                  * needs to be updated to support duration field calculation.
111                  *
112                  * RTS: time needed to transmit pending data/mgmt frame plus
113                  *    one CTS frame plus one ACK frame plus 3 x SIFS
114                  * CTS: duration of immediately previous RTS minus time
115                  *    required to transmit CTS and its SIFS
116                  * ACK: 0 if immediately previous directed data/mgmt had
117                  *    more=0, with more=1 duration in ACK frame is duration
118                  *    from previous frame minus time needed to transmit ACK
119                  *    and its SIFS
120                  * PS Poll: BIT(15) | BIT(14) | aid
121                  */
122                 return 0;
123         }
124
125         /* data/mgmt */
126         if (0 /* FIX: data/mgmt during CFP */)
127                 return cpu_to_le16(32768);
128
129         if (group_addr) /* Group address as the destination - no ACK */
130                 return 0;
131
132         /* Individual destination address:
133          * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
134          * CTS and ACK frames shall be transmitted using the highest rate in
135          * basic rate set that is less than or equal to the rate of the
136          * immediately previous frame and that is using the same modulation
137          * (CCK or OFDM). If no basic rate set matches with these requirements,
138          * the highest mandatory rate of the PHY that is less than or equal to
139          * the rate of the previous frame is used.
140          * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
141          */
142         rate = -1;
143         /* use lowest available if everything fails */
144         mrate = sband->bitrates[0].bitrate;
145         for (i = 0; i < sband->n_bitrates; i++) {
146                 struct ieee80211_rate *r = &sband->bitrates[i];
147
148                 if (r->bitrate > txrate->bitrate)
149                         break;
150
151                 if ((rate_flags & r->flags) != rate_flags)
152                         continue;
153
154                 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
155                         rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
156
157                 switch (sband->band) {
158                 case NL80211_BAND_2GHZ: {
159                         u32 flag;
160                         if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
161                                 flag = IEEE80211_RATE_MANDATORY_G;
162                         else
163                                 flag = IEEE80211_RATE_MANDATORY_B;
164                         if (r->flags & flag)
165                                 mrate = r->bitrate;
166                         break;
167                 }
168                 case NL80211_BAND_5GHZ:
169                 case NL80211_BAND_6GHZ:
170                         if (r->flags & IEEE80211_RATE_MANDATORY_A)
171                                 mrate = r->bitrate;
172                         break;
173                 case NL80211_BAND_S1GHZ:
174                 case NL80211_BAND_60GHZ:
175                         /* TODO, for now fall through */
176                 case NUM_NL80211_BANDS:
177                         WARN_ON(1);
178                         break;
179                 }
180         }
181         if (rate == -1) {
182                 /* No matching basic rate found; use highest suitable mandatory
183                  * PHY rate */
184                 rate = DIV_ROUND_UP(mrate, 1 << shift);
185         }
186
187         /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
188         if (ieee80211_is_data_qos(hdr->frame_control) &&
189             *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
190                 dur = 0;
191         else
192                 /* Time needed to transmit ACK
193                  * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
194                  * to closest integer */
195                 dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
196                                 tx->sdata->vif.bss_conf.use_short_preamble,
197                                 shift);
198
199         if (next_frag_len) {
200                 /* Frame is fragmented: duration increases with time needed to
201                  * transmit next fragment plus ACK and 2 x SIFS. */
202                 dur *= 2; /* ACK + SIFS */
203                 /* next fragment */
204                 dur += ieee80211_frame_duration(sband->band, next_frag_len,
205                                 txrate->bitrate, erp,
206                                 tx->sdata->vif.bss_conf.use_short_preamble,
207                                 shift);
208         }
209
210         return cpu_to_le16(dur);
211 }
212
213 /* tx handlers */
214 static ieee80211_tx_result debug_noinline
215 ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
216 {
217         struct ieee80211_local *local = tx->local;
218         struct ieee80211_if_managed *ifmgd;
219         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
220
221         /* driver doesn't support power save */
222         if (!ieee80211_hw_check(&local->hw, SUPPORTS_PS))
223                 return TX_CONTINUE;
224
225         /* hardware does dynamic power save */
226         if (ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS))
227                 return TX_CONTINUE;
228
229         /* dynamic power save disabled */
230         if (local->hw.conf.dynamic_ps_timeout <= 0)
231                 return TX_CONTINUE;
232
233         /* we are scanning, don't enable power save */
234         if (local->scanning)
235                 return TX_CONTINUE;
236
237         if (!local->ps_sdata)
238                 return TX_CONTINUE;
239
240         /* No point if we're going to suspend */
241         if (local->quiescing)
242                 return TX_CONTINUE;
243
244         /* dynamic ps is supported only in managed mode */
245         if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
246                 return TX_CONTINUE;
247
248         if (unlikely(info->flags & IEEE80211_TX_INTFL_OFFCHAN_TX_OK))
249                 return TX_CONTINUE;
250
251         ifmgd = &tx->sdata->u.mgd;
252
253         /*
254          * Don't wakeup from power save if u-apsd is enabled, voip ac has
255          * u-apsd enabled and the frame is in voip class. This effectively
256          * means that even if all access categories have u-apsd enabled, in
257          * practise u-apsd is only used with the voip ac. This is a
258          * workaround for the case when received voip class packets do not
259          * have correct qos tag for some reason, due the network or the
260          * peer application.
261          *
262          * Note: ifmgd->uapsd_queues access is racy here. If the value is
263          * changed via debugfs, user needs to reassociate manually to have
264          * everything in sync.
265          */
266         if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
267             (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
268             skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
269                 return TX_CONTINUE;
270
271         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
272                 ieee80211_stop_queues_by_reason(&local->hw,
273                                                 IEEE80211_MAX_QUEUE_MAP,
274                                                 IEEE80211_QUEUE_STOP_REASON_PS,
275                                                 false);
276                 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
277                 ieee80211_queue_work(&local->hw,
278                                      &local->dynamic_ps_disable_work);
279         }
280
281         /* Don't restart the timer if we're not disassociated */
282         if (!ifmgd->associated)
283                 return TX_CONTINUE;
284
285         mod_timer(&local->dynamic_ps_timer, jiffies +
286                   msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
287
288         return TX_CONTINUE;
289 }
290
291 static ieee80211_tx_result debug_noinline
292 ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
293 {
294
295         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
296         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
297         bool assoc = false;
298
299         if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
300                 return TX_CONTINUE;
301
302         if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
303             test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
304             !ieee80211_is_probe_req(hdr->frame_control) &&
305             !ieee80211_is_any_nullfunc(hdr->frame_control))
306                 /*
307                  * When software scanning only nullfunc frames (to notify
308                  * the sleep state to the AP) and probe requests (for the
309                  * active scan) are allowed, all other frames should not be
310                  * sent and we should not get here, but if we do
311                  * nonetheless, drop them to avoid sending them
312                  * off-channel. See the link below and
313                  * ieee80211_start_scan() for more.
314                  *
315                  * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
316                  */
317                 return TX_DROP;
318
319         if (tx->sdata->vif.type == NL80211_IFTYPE_OCB)
320                 return TX_CONTINUE;
321
322         if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
323                 return TX_CONTINUE;
324
325         if (tx->flags & IEEE80211_TX_PS_BUFFERED)
326                 return TX_CONTINUE;
327
328         if (tx->sta)
329                 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
330
331         if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
332                 if (unlikely(!assoc &&
333                              ieee80211_is_data(hdr->frame_control))) {
334 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
335                         sdata_info(tx->sdata,
336                                    "dropped data frame to not associated station %pM\n",
337                                    hdr->addr1);
338 #endif
339                         I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
340                         return TX_DROP;
341                 }
342         } else if (unlikely(ieee80211_is_data(hdr->frame_control) &&
343                             ieee80211_vif_get_num_mcast_if(tx->sdata) == 0)) {
344                 /*
345                  * No associated STAs - no need to send multicast
346                  * frames.
347                  */
348                 return TX_DROP;
349         }
350
351         return TX_CONTINUE;
352 }
353
354 /* This function is called whenever the AP is about to exceed the maximum limit
355  * of buffered frames for power saving STAs. This situation should not really
356  * happen often during normal operation, so dropping the oldest buffered packet
357  * from each queue should be OK to make some room for new frames. */
358 static void purge_old_ps_buffers(struct ieee80211_local *local)
359 {
360         int total = 0, purged = 0;
361         struct sk_buff *skb;
362         struct ieee80211_sub_if_data *sdata;
363         struct sta_info *sta;
364
365         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
366                 struct ps_data *ps;
367
368                 if (sdata->vif.type == NL80211_IFTYPE_AP)
369                         ps = &sdata->u.ap.ps;
370                 else if (ieee80211_vif_is_mesh(&sdata->vif))
371                         ps = &sdata->u.mesh.ps;
372                 else
373                         continue;
374
375                 skb = skb_dequeue(&ps->bc_buf);
376                 if (skb) {
377                         purged++;
378                         ieee80211_free_txskb(&local->hw, skb);
379                 }
380                 total += skb_queue_len(&ps->bc_buf);
381         }
382
383         /*
384          * Drop one frame from each station from the lowest-priority
385          * AC that has frames at all.
386          */
387         list_for_each_entry_rcu(sta, &local->sta_list, list) {
388                 int ac;
389
390                 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
391                         skb = skb_dequeue(&sta->ps_tx_buf[ac]);
392                         total += skb_queue_len(&sta->ps_tx_buf[ac]);
393                         if (skb) {
394                                 purged++;
395                                 ieee80211_free_txskb(&local->hw, skb);
396                                 break;
397                         }
398                 }
399         }
400
401         local->total_ps_buffered = total;
402         ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
403 }
404
405 static ieee80211_tx_result
406 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
407 {
408         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
409         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
410         struct ps_data *ps;
411
412         /*
413          * broadcast/multicast frame
414          *
415          * If any of the associated/peer stations is in power save mode,
416          * the frame is buffered to be sent after DTIM beacon frame.
417          * This is done either by the hardware or us.
418          */
419
420         /* powersaving STAs currently only in AP/VLAN/mesh mode */
421         if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
422             tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
423                 if (!tx->sdata->bss)
424                         return TX_CONTINUE;
425
426                 ps = &tx->sdata->bss->ps;
427         } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
428                 ps = &tx->sdata->u.mesh.ps;
429         } else {
430                 return TX_CONTINUE;
431         }
432
433
434         /* no buffering for ordered frames */
435         if (ieee80211_has_order(hdr->frame_control))
436                 return TX_CONTINUE;
437
438         if (ieee80211_is_probe_req(hdr->frame_control))
439                 return TX_CONTINUE;
440
441         if (ieee80211_hw_check(&tx->local->hw, QUEUE_CONTROL))
442                 info->hw_queue = tx->sdata->vif.cab_queue;
443
444         /* no stations in PS mode and no buffered packets */
445         if (!atomic_read(&ps->num_sta_ps) && skb_queue_empty(&ps->bc_buf))
446                 return TX_CONTINUE;
447
448         info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
449
450         /* device releases frame after DTIM beacon */
451         if (!ieee80211_hw_check(&tx->local->hw, HOST_BROADCAST_PS_BUFFERING))
452                 return TX_CONTINUE;
453
454         /* buffered in mac80211 */
455         if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
456                 purge_old_ps_buffers(tx->local);
457
458         if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
459                 ps_dbg(tx->sdata,
460                        "BC TX buffer full - dropping the oldest frame\n");
461                 ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf));
462         } else
463                 tx->local->total_ps_buffered++;
464
465         skb_queue_tail(&ps->bc_buf, tx->skb);
466
467         return TX_QUEUED;
468 }
469
470 static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
471                              struct sk_buff *skb)
472 {
473         if (!ieee80211_is_mgmt(fc))
474                 return 0;
475
476         if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
477                 return 0;
478
479         if (!ieee80211_is_robust_mgmt_frame(skb))
480                 return 0;
481
482         return 1;
483 }
484
485 static ieee80211_tx_result
486 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
487 {
488         struct sta_info *sta = tx->sta;
489         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
490         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
491         struct ieee80211_local *local = tx->local;
492
493         if (unlikely(!sta))
494                 return TX_CONTINUE;
495
496         if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
497                       test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
498                       test_sta_flag(sta, WLAN_STA_PS_DELIVER)) &&
499                      !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
500                 int ac = skb_get_queue_mapping(tx->skb);
501
502                 if (ieee80211_is_mgmt(hdr->frame_control) &&
503                     !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
504                         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
505                         return TX_CONTINUE;
506                 }
507
508                 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
509                        sta->sta.addr, sta->sta.aid, ac);
510                 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
511                         purge_old_ps_buffers(tx->local);
512
513                 /* sync with ieee80211_sta_ps_deliver_wakeup */
514                 spin_lock(&sta->ps_lock);
515                 /*
516                  * STA woke up the meantime and all the frames on ps_tx_buf have
517                  * been queued to pending queue. No reordering can happen, go
518                  * ahead and Tx the packet.
519                  */
520                 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
521                     !test_sta_flag(sta, WLAN_STA_PS_DRIVER) &&
522                     !test_sta_flag(sta, WLAN_STA_PS_DELIVER)) {
523                         spin_unlock(&sta->ps_lock);
524                         return TX_CONTINUE;
525                 }
526
527                 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
528                         struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
529                         ps_dbg(tx->sdata,
530                                "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
531                                sta->sta.addr, ac);
532                         ieee80211_free_txskb(&local->hw, old);
533                 } else
534                         tx->local->total_ps_buffered++;
535
536                 info->control.jiffies = jiffies;
537                 info->control.vif = &tx->sdata->vif;
538                 info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
539                 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
540                 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
541                 spin_unlock(&sta->ps_lock);
542
543                 if (!timer_pending(&local->sta_cleanup))
544                         mod_timer(&local->sta_cleanup,
545                                   round_jiffies(jiffies +
546                                                 STA_INFO_CLEANUP_INTERVAL));
547
548                 /*
549                  * We queued up some frames, so the TIM bit might
550                  * need to be set, recalculate it.
551                  */
552                 sta_info_recalc_tim(sta);
553
554                 return TX_QUEUED;
555         } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
556                 ps_dbg(tx->sdata,
557                        "STA %pM in PS mode, but polling/in SP -> send frame\n",
558                        sta->sta.addr);
559         }
560
561         return TX_CONTINUE;
562 }
563
564 static ieee80211_tx_result debug_noinline
565 ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
566 {
567         if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
568                 return TX_CONTINUE;
569
570         if (tx->flags & IEEE80211_TX_UNICAST)
571                 return ieee80211_tx_h_unicast_ps_buf(tx);
572         else
573                 return ieee80211_tx_h_multicast_ps_buf(tx);
574 }
575
576 static ieee80211_tx_result debug_noinline
577 ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
578 {
579         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
580
581         if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
582                 if (tx->sdata->control_port_no_encrypt)
583                         info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
584                 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
585                 info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
586         }
587
588         return TX_CONTINUE;
589 }
590
591 static ieee80211_tx_result debug_noinline
592 ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
593 {
594         struct ieee80211_key *key;
595         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
596         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
597
598         if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)) {
599                 tx->key = NULL;
600                 return TX_CONTINUE;
601         }
602
603         if (tx->sta &&
604             (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
605                 tx->key = key;
606         else if (ieee80211_is_group_privacy_action(tx->skb) &&
607                 (key = rcu_dereference(tx->sdata->default_multicast_key)))
608                 tx->key = key;
609         else if (ieee80211_is_mgmt(hdr->frame_control) &&
610                  is_multicast_ether_addr(hdr->addr1) &&
611                  ieee80211_is_robust_mgmt_frame(tx->skb) &&
612                  (key = rcu_dereference(tx->sdata->default_mgmt_key)))
613                 tx->key = key;
614         else if (is_multicast_ether_addr(hdr->addr1) &&
615                  (key = rcu_dereference(tx->sdata->default_multicast_key)))
616                 tx->key = key;
617         else if (!is_multicast_ether_addr(hdr->addr1) &&
618                  (key = rcu_dereference(tx->sdata->default_unicast_key)))
619                 tx->key = key;
620         else
621                 tx->key = NULL;
622
623         if (tx->key) {
624                 bool skip_hw = false;
625
626                 /* TODO: add threshold stuff again */
627
628                 switch (tx->key->conf.cipher) {
629                 case WLAN_CIPHER_SUITE_WEP40:
630                 case WLAN_CIPHER_SUITE_WEP104:
631                 case WLAN_CIPHER_SUITE_TKIP:
632                         if (!ieee80211_is_data_present(hdr->frame_control))
633                                 tx->key = NULL;
634                         break;
635                 case WLAN_CIPHER_SUITE_CCMP:
636                 case WLAN_CIPHER_SUITE_CCMP_256:
637                 case WLAN_CIPHER_SUITE_GCMP:
638                 case WLAN_CIPHER_SUITE_GCMP_256:
639                         if (!ieee80211_is_data_present(hdr->frame_control) &&
640                             !ieee80211_use_mfp(hdr->frame_control, tx->sta,
641                                                tx->skb) &&
642                             !ieee80211_is_group_privacy_action(tx->skb))
643                                 tx->key = NULL;
644                         else
645                                 skip_hw = (tx->key->conf.flags &
646                                            IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
647                                         ieee80211_is_mgmt(hdr->frame_control);
648                         break;
649                 case WLAN_CIPHER_SUITE_AES_CMAC:
650                 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
651                 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
652                 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
653                         if (!ieee80211_is_mgmt(hdr->frame_control))
654                                 tx->key = NULL;
655                         break;
656                 }
657
658                 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
659                              !ieee80211_is_deauth(hdr->frame_control)))
660                         return TX_DROP;
661
662                 if (!skip_hw && tx->key &&
663                     tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
664                         info->control.hw_key = &tx->key->conf;
665         } else if (!ieee80211_is_mgmt(hdr->frame_control) && tx->sta &&
666                    test_sta_flag(tx->sta, WLAN_STA_USES_ENCRYPTION)) {
667                 return TX_DROP;
668         }
669
670         return TX_CONTINUE;
671 }
672
673 static ieee80211_tx_result debug_noinline
674 ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
675 {
676         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
677         struct ieee80211_hdr *hdr = (void *)tx->skb->data;
678         struct ieee80211_supported_band *sband;
679         u32 len;
680         struct ieee80211_tx_rate_control txrc;
681         struct ieee80211_sta_rates *ratetbl = NULL;
682         bool assoc = false;
683
684         memset(&txrc, 0, sizeof(txrc));
685
686         sband = tx->local->hw.wiphy->bands[info->band];
687
688         len = min_t(u32, tx->skb->len + FCS_LEN,
689                          tx->local->hw.wiphy->frag_threshold);
690
691         /* set up the tx rate control struct we give the RC algo */
692         txrc.hw = &tx->local->hw;
693         txrc.sband = sband;
694         txrc.bss_conf = &tx->sdata->vif.bss_conf;
695         txrc.skb = tx->skb;
696         txrc.reported_rate.idx = -1;
697         txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
698
699         if (tx->sdata->rc_has_mcs_mask[info->band])
700                 txrc.rate_idx_mcs_mask =
701                         tx->sdata->rc_rateidx_mcs_mask[info->band];
702
703         txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
704                     tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
705                     tx->sdata->vif.type == NL80211_IFTYPE_ADHOC ||
706                     tx->sdata->vif.type == NL80211_IFTYPE_OCB);
707
708         /* set up RTS protection if desired */
709         if (len > tx->local->hw.wiphy->rts_threshold) {
710                 txrc.rts = true;
711         }
712
713         info->control.use_rts = txrc.rts;
714         info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
715
716         /*
717          * Use short preamble if the BSS can handle it, but not for
718          * management frames unless we know the receiver can handle
719          * that -- the management frame might be to a station that
720          * just wants a probe response.
721          */
722         if (tx->sdata->vif.bss_conf.use_short_preamble &&
723             (ieee80211_is_data(hdr->frame_control) ||
724              (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
725                 txrc.short_preamble = true;
726
727         info->control.short_preamble = txrc.short_preamble;
728
729         /* don't ask rate control when rate already injected via radiotap */
730         if (info->control.flags & IEEE80211_TX_CTRL_RATE_INJECT)
731                 return TX_CONTINUE;
732
733         if (tx->sta)
734                 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
735
736         /*
737          * Lets not bother rate control if we're associated and cannot
738          * talk to the sta. This should not happen.
739          */
740         if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
741                  !rate_usable_index_exists(sband, &tx->sta->sta),
742                  "%s: Dropped data frame as no usable bitrate found while "
743                  "scanning and associated. Target station: "
744                  "%pM on %d GHz band\n",
745                  tx->sdata->name, hdr->addr1,
746                  info->band ? 5 : 2))
747                 return TX_DROP;
748
749         /*
750          * If we're associated with the sta at this point we know we can at
751          * least send the frame at the lowest bit rate.
752          */
753         rate_control_get_rate(tx->sdata, tx->sta, &txrc);
754
755         if (tx->sta && !info->control.skip_table)
756                 ratetbl = rcu_dereference(tx->sta->sta.rates);
757
758         if (unlikely(info->control.rates[0].idx < 0)) {
759                 if (ratetbl) {
760                         struct ieee80211_tx_rate rate = {
761                                 .idx = ratetbl->rate[0].idx,
762                                 .flags = ratetbl->rate[0].flags,
763                                 .count = ratetbl->rate[0].count
764                         };
765
766                         if (ratetbl->rate[0].idx < 0)
767                                 return TX_DROP;
768
769                         tx->rate = rate;
770                 } else {
771                         return TX_DROP;
772                 }
773         } else {
774                 tx->rate = info->control.rates[0];
775         }
776
777         if (txrc.reported_rate.idx < 0) {
778                 txrc.reported_rate = tx->rate;
779                 if (tx->sta && ieee80211_is_data(hdr->frame_control))
780                         tx->sta->tx_stats.last_rate = txrc.reported_rate;
781         } else if (tx->sta)
782                 tx->sta->tx_stats.last_rate = txrc.reported_rate;
783
784         if (ratetbl)
785                 return TX_CONTINUE;
786
787         if (unlikely(!info->control.rates[0].count))
788                 info->control.rates[0].count = 1;
789
790         if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
791                          (info->flags & IEEE80211_TX_CTL_NO_ACK)))
792                 info->control.rates[0].count = 1;
793
794         return TX_CONTINUE;
795 }
796
797 static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
798 {
799         u16 *seq = &sta->tid_seq[tid];
800         __le16 ret = cpu_to_le16(*seq);
801
802         /* Increase the sequence number. */
803         *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
804
805         return ret;
806 }
807
808 static ieee80211_tx_result debug_noinline
809 ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
810 {
811         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
812         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
813         int tid;
814
815         /*
816          * Packet injection may want to control the sequence
817          * number, if we have no matching interface then we
818          * neither assign one ourselves nor ask the driver to.
819          */
820         if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
821                 return TX_CONTINUE;
822
823         if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
824                 return TX_CONTINUE;
825
826         if (ieee80211_hdrlen(hdr->frame_control) < 24)
827                 return TX_CONTINUE;
828
829         if (ieee80211_is_qos_nullfunc(hdr->frame_control))
830                 return TX_CONTINUE;
831
832         if (info->control.flags & IEEE80211_TX_CTRL_NO_SEQNO)
833                 return TX_CONTINUE;
834
835         /*
836          * Anything but QoS data that has a sequence number field
837          * (is long enough) gets a sequence number from the global
838          * counter.  QoS data frames with a multicast destination
839          * also use the global counter (802.11-2012 9.3.2.10).
840          */
841         if (!ieee80211_is_data_qos(hdr->frame_control) ||
842             is_multicast_ether_addr(hdr->addr1)) {
843                 /* driver should assign sequence number */
844                 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
845                 /* for pure STA mode without beacons, we can do it */
846                 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
847                 tx->sdata->sequence_number += 0x10;
848                 if (tx->sta)
849                         tx->sta->tx_stats.msdu[IEEE80211_NUM_TIDS]++;
850                 return TX_CONTINUE;
851         }
852
853         /*
854          * This should be true for injected/management frames only, for
855          * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
856          * above since they are not QoS-data frames.
857          */
858         if (!tx->sta)
859                 return TX_CONTINUE;
860
861         /* include per-STA, per-TID sequence counter */
862         tid = ieee80211_get_tid(hdr);
863         tx->sta->tx_stats.msdu[tid]++;
864
865         hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
866
867         return TX_CONTINUE;
868 }
869
870 static int ieee80211_fragment(struct ieee80211_tx_data *tx,
871                               struct sk_buff *skb, int hdrlen,
872                               int frag_threshold)
873 {
874         struct ieee80211_local *local = tx->local;
875         struct ieee80211_tx_info *info;
876         struct sk_buff *tmp;
877         int per_fragm = frag_threshold - hdrlen - FCS_LEN;
878         int pos = hdrlen + per_fragm;
879         int rem = skb->len - hdrlen - per_fragm;
880
881         if (WARN_ON(rem < 0))
882                 return -EINVAL;
883
884         /* first fragment was already added to queue by caller */
885
886         while (rem) {
887                 int fraglen = per_fragm;
888
889                 if (fraglen > rem)
890                         fraglen = rem;
891                 rem -= fraglen;
892                 tmp = dev_alloc_skb(local->tx_headroom +
893                                     frag_threshold +
894                                     tx->sdata->encrypt_headroom +
895                                     IEEE80211_ENCRYPT_TAILROOM);
896                 if (!tmp)
897                         return -ENOMEM;
898
899                 __skb_queue_tail(&tx->skbs, tmp);
900
901                 skb_reserve(tmp,
902                             local->tx_headroom + tx->sdata->encrypt_headroom);
903
904                 /* copy control information */
905                 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
906
907                 info = IEEE80211_SKB_CB(tmp);
908                 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
909                                  IEEE80211_TX_CTL_FIRST_FRAGMENT);
910
911                 if (rem)
912                         info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
913
914                 skb_copy_queue_mapping(tmp, skb);
915                 tmp->priority = skb->priority;
916                 tmp->dev = skb->dev;
917
918                 /* copy header and data */
919                 skb_put_data(tmp, skb->data, hdrlen);
920                 skb_put_data(tmp, skb->data + pos, fraglen);
921
922                 pos += fraglen;
923         }
924
925         /* adjust first fragment's length */
926         skb_trim(skb, hdrlen + per_fragm);
927         return 0;
928 }
929
930 static ieee80211_tx_result debug_noinline
931 ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
932 {
933         struct sk_buff *skb = tx->skb;
934         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
935         struct ieee80211_hdr *hdr = (void *)skb->data;
936         int frag_threshold = tx->local->hw.wiphy->frag_threshold;
937         int hdrlen;
938         int fragnum;
939
940         /* no matter what happens, tx->skb moves to tx->skbs */
941         __skb_queue_tail(&tx->skbs, skb);
942         tx->skb = NULL;
943
944         if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
945                 return TX_CONTINUE;
946
947         if (ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG))
948                 return TX_CONTINUE;
949
950         /*
951          * Warn when submitting a fragmented A-MPDU frame and drop it.
952          * This scenario is handled in ieee80211_tx_prepare but extra
953          * caution taken here as fragmented ampdu may cause Tx stop.
954          */
955         if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
956                 return TX_DROP;
957
958         hdrlen = ieee80211_hdrlen(hdr->frame_control);
959
960         /* internal error, why isn't DONTFRAG set? */
961         if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
962                 return TX_DROP;
963
964         /*
965          * Now fragment the frame. This will allocate all the fragments and
966          * chain them (using skb as the first fragment) to skb->next.
967          * During transmission, we will remove the successfully transmitted
968          * fragments from this list. When the low-level driver rejects one
969          * of the fragments then we will simply pretend to accept the skb
970          * but store it away as pending.
971          */
972         if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
973                 return TX_DROP;
974
975         /* update duration/seq/flags of fragments */
976         fragnum = 0;
977
978         skb_queue_walk(&tx->skbs, skb) {
979                 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
980
981                 hdr = (void *)skb->data;
982                 info = IEEE80211_SKB_CB(skb);
983
984                 if (!skb_queue_is_last(&tx->skbs, skb)) {
985                         hdr->frame_control |= morefrags;
986                         /*
987                          * No multi-rate retries for fragmented frames, that
988                          * would completely throw off the NAV at other STAs.
989                          */
990                         info->control.rates[1].idx = -1;
991                         info->control.rates[2].idx = -1;
992                         info->control.rates[3].idx = -1;
993                         BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
994                         info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
995                 } else {
996                         hdr->frame_control &= ~morefrags;
997                 }
998                 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
999                 fragnum++;
1000         }
1001
1002         return TX_CONTINUE;
1003 }
1004
1005 static ieee80211_tx_result debug_noinline
1006 ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
1007 {
1008         struct sk_buff *skb;
1009         int ac = -1;
1010
1011         if (!tx->sta)
1012                 return TX_CONTINUE;
1013
1014         skb_queue_walk(&tx->skbs, skb) {
1015                 ac = skb_get_queue_mapping(skb);
1016                 tx->sta->tx_stats.bytes[ac] += skb->len;
1017         }
1018         if (ac >= 0)
1019                 tx->sta->tx_stats.packets[ac]++;
1020
1021         return TX_CONTINUE;
1022 }
1023
1024 static ieee80211_tx_result debug_noinline
1025 ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
1026 {
1027         if (!tx->key)
1028                 return TX_CONTINUE;
1029
1030         switch (tx->key->conf.cipher) {
1031         case WLAN_CIPHER_SUITE_WEP40:
1032         case WLAN_CIPHER_SUITE_WEP104:
1033                 return ieee80211_crypto_wep_encrypt(tx);
1034         case WLAN_CIPHER_SUITE_TKIP:
1035                 return ieee80211_crypto_tkip_encrypt(tx);
1036         case WLAN_CIPHER_SUITE_CCMP:
1037                 return ieee80211_crypto_ccmp_encrypt(
1038                         tx, IEEE80211_CCMP_MIC_LEN);
1039         case WLAN_CIPHER_SUITE_CCMP_256:
1040                 return ieee80211_crypto_ccmp_encrypt(
1041                         tx, IEEE80211_CCMP_256_MIC_LEN);
1042         case WLAN_CIPHER_SUITE_AES_CMAC:
1043                 return ieee80211_crypto_aes_cmac_encrypt(tx);
1044         case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1045                 return ieee80211_crypto_aes_cmac_256_encrypt(tx);
1046         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1047         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1048                 return ieee80211_crypto_aes_gmac_encrypt(tx);
1049         case WLAN_CIPHER_SUITE_GCMP:
1050         case WLAN_CIPHER_SUITE_GCMP_256:
1051                 return ieee80211_crypto_gcmp_encrypt(tx);
1052         default:
1053                 return ieee80211_crypto_hw_encrypt(tx);
1054         }
1055
1056         return TX_DROP;
1057 }
1058
1059 static ieee80211_tx_result debug_noinline
1060 ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1061 {
1062         struct sk_buff *skb;
1063         struct ieee80211_hdr *hdr;
1064         int next_len;
1065         bool group_addr;
1066
1067         skb_queue_walk(&tx->skbs, skb) {
1068                 hdr = (void *) skb->data;
1069                 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1070                         break; /* must not overwrite AID */
1071                 if (!skb_queue_is_last(&tx->skbs, skb)) {
1072                         struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1073                         next_len = next->len;
1074                 } else
1075                         next_len = 0;
1076                 group_addr = is_multicast_ether_addr(hdr->addr1);
1077
1078                 hdr->duration_id =
1079                         ieee80211_duration(tx, skb, group_addr, next_len);
1080         }
1081
1082         return TX_CONTINUE;
1083 }
1084
1085 /* actual transmit path */
1086
1087 static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1088                                   struct sk_buff *skb,
1089                                   struct ieee80211_tx_info *info,
1090                                   struct tid_ampdu_tx *tid_tx,
1091                                   int tid)
1092 {
1093         bool queued = false;
1094         bool reset_agg_timer = false;
1095         struct sk_buff *purge_skb = NULL;
1096
1097         if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1098                 info->flags |= IEEE80211_TX_CTL_AMPDU;
1099                 reset_agg_timer = true;
1100         } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1101                 /*
1102                  * nothing -- this aggregation session is being started
1103                  * but that might still fail with the driver
1104                  */
1105         } else if (!tx->sta->sta.txq[tid]) {
1106                 spin_lock(&tx->sta->lock);
1107                 /*
1108                  * Need to re-check now, because we may get here
1109                  *
1110                  *  1) in the window during which the setup is actually
1111                  *     already done, but not marked yet because not all
1112                  *     packets are spliced over to the driver pending
1113                  *     queue yet -- if this happened we acquire the lock
1114                  *     either before or after the splice happens, but
1115                  *     need to recheck which of these cases happened.
1116                  *
1117                  *  2) during session teardown, if the OPERATIONAL bit
1118                  *     was cleared due to the teardown but the pointer
1119                  *     hasn't been assigned NULL yet (or we loaded it
1120                  *     before it was assigned) -- in this case it may
1121                  *     now be NULL which means we should just let the
1122                  *     packet pass through because splicing the frames
1123                  *     back is already done.
1124                  */
1125                 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
1126
1127                 if (!tid_tx) {
1128                         /* do nothing, let packet pass through */
1129                 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1130                         info->flags |= IEEE80211_TX_CTL_AMPDU;
1131                         reset_agg_timer = true;
1132                 } else {
1133                         queued = true;
1134                         if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1135                                 clear_sta_flag(tx->sta, WLAN_STA_SP);
1136                                 ps_dbg(tx->sta->sdata,
1137                                        "STA %pM aid %d: SP frame queued, close the SP w/o telling the peer\n",
1138                                        tx->sta->sta.addr, tx->sta->sta.aid);
1139                         }
1140                         info->control.vif = &tx->sdata->vif;
1141                         info->control.flags |= IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
1142                         info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
1143                         __skb_queue_tail(&tid_tx->pending, skb);
1144                         if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1145                                 purge_skb = __skb_dequeue(&tid_tx->pending);
1146                 }
1147                 spin_unlock(&tx->sta->lock);
1148
1149                 if (purge_skb)
1150                         ieee80211_free_txskb(&tx->local->hw, purge_skb);
1151         }
1152
1153         /* reset session timer */
1154         if (reset_agg_timer)
1155                 tid_tx->last_tx = jiffies;
1156
1157         return queued;
1158 }
1159
1160 /*
1161  * initialises @tx
1162  * pass %NULL for the station if unknown, a valid pointer if known
1163  * or an ERR_PTR() if the station is known not to exist
1164  */
1165 static ieee80211_tx_result
1166 ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1167                      struct ieee80211_tx_data *tx,
1168                      struct sta_info *sta, struct sk_buff *skb)
1169 {
1170         struct ieee80211_local *local = sdata->local;
1171         struct ieee80211_hdr *hdr;
1172         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1173         int tid;
1174
1175         memset(tx, 0, sizeof(*tx));
1176         tx->skb = skb;
1177         tx->local = local;
1178         tx->sdata = sdata;
1179         __skb_queue_head_init(&tx->skbs);
1180
1181         /*
1182          * If this flag is set to true anywhere, and we get here,
1183          * we are doing the needed processing, so remove the flag
1184          * now.
1185          */
1186         info->control.flags &= ~IEEE80211_TX_INTCFL_NEED_TXPROCESSING;
1187
1188         hdr = (struct ieee80211_hdr *) skb->data;
1189
1190         if (likely(sta)) {
1191                 if (!IS_ERR(sta))
1192                         tx->sta = sta;
1193         } else {
1194                 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1195                         tx->sta = rcu_dereference(sdata->u.vlan.sta);
1196                         if (!tx->sta && sdata->wdev.use_4addr)
1197                                 return TX_DROP;
1198                 } else if (info->flags & (IEEE80211_TX_INTFL_NL80211_FRAME_TX |
1199                                           IEEE80211_TX_CTL_INJECTED) ||
1200                            tx->sdata->control_port_protocol == tx->skb->protocol) {
1201                         tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1202                 }
1203                 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
1204                         tx->sta = sta_info_get(sdata, hdr->addr1);
1205         }
1206
1207         if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
1208             !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
1209             ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION) &&
1210             !ieee80211_hw_check(&local->hw, TX_AMPDU_SETUP_IN_HW)) {
1211                 struct tid_ampdu_tx *tid_tx;
1212
1213                 tid = ieee80211_get_tid(hdr);
1214
1215                 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1216                 if (tid_tx) {
1217                         bool queued;
1218
1219                         queued = ieee80211_tx_prep_agg(tx, skb, info,
1220                                                        tid_tx, tid);
1221
1222                         if (unlikely(queued))
1223                                 return TX_QUEUED;
1224                 }
1225         }
1226
1227         if (is_multicast_ether_addr(hdr->addr1)) {
1228                 tx->flags &= ~IEEE80211_TX_UNICAST;
1229                 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1230         } else
1231                 tx->flags |= IEEE80211_TX_UNICAST;
1232
1233         if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1234                 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1235                     skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1236                     info->flags & IEEE80211_TX_CTL_AMPDU)
1237                         info->flags |= IEEE80211_TX_CTL_DONTFRAG;
1238         }
1239
1240         if (!tx->sta)
1241                 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1242         else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT)) {
1243                 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1244                 ieee80211_check_fast_xmit(tx->sta);
1245         }
1246
1247         info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
1248
1249         return TX_CONTINUE;
1250 }
1251
1252 static struct txq_info *ieee80211_get_txq(struct ieee80211_local *local,
1253                                           struct ieee80211_vif *vif,
1254                                           struct sta_info *sta,
1255                                           struct sk_buff *skb)
1256 {
1257         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1258         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1259         struct ieee80211_txq *txq = NULL;
1260
1261         if ((info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) ||
1262             (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE))
1263                 return NULL;
1264
1265         if (!(info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) &&
1266             unlikely(!ieee80211_is_data_present(hdr->frame_control))) {
1267                 if ((!ieee80211_is_mgmt(hdr->frame_control) ||
1268                      ieee80211_is_bufferable_mmpdu(hdr->frame_control) ||
1269                      vif->type == NL80211_IFTYPE_STATION) &&
1270                     sta && sta->uploaded) {
1271                         /*
1272                          * This will be NULL if the driver didn't set the
1273                          * opt-in hardware flag.
1274                          */
1275                         txq = sta->sta.txq[IEEE80211_NUM_TIDS];
1276                 }
1277         } else if (sta) {
1278                 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1279
1280                 if (!sta->uploaded)
1281                         return NULL;
1282
1283                 txq = sta->sta.txq[tid];
1284         } else if (vif) {
1285                 txq = vif->txq;
1286         }
1287
1288         if (!txq)
1289                 return NULL;
1290
1291         return to_txq_info(txq);
1292 }
1293
1294 static void ieee80211_set_skb_enqueue_time(struct sk_buff *skb)
1295 {
1296         IEEE80211_SKB_CB(skb)->control.enqueue_time = codel_get_time();
1297 }
1298
1299 static u32 codel_skb_len_func(const struct sk_buff *skb)
1300 {
1301         return skb->len;
1302 }
1303
1304 static codel_time_t codel_skb_time_func(const struct sk_buff *skb)
1305 {
1306         const struct ieee80211_tx_info *info;
1307
1308         info = (const struct ieee80211_tx_info *)skb->cb;
1309         return info->control.enqueue_time;
1310 }
1311
1312 static struct sk_buff *codel_dequeue_func(struct codel_vars *cvars,
1313                                           void *ctx)
1314 {
1315         struct ieee80211_local *local;
1316         struct txq_info *txqi;
1317         struct fq *fq;
1318         struct fq_flow *flow;
1319
1320         txqi = ctx;
1321         local = vif_to_sdata(txqi->txq.vif)->local;
1322         fq = &local->fq;
1323
1324         if (cvars == &txqi->def_cvars)
1325                 flow = &txqi->def_flow;
1326         else
1327                 flow = &fq->flows[cvars - local->cvars];
1328
1329         return fq_flow_dequeue(fq, flow);
1330 }
1331
1332 static void codel_drop_func(struct sk_buff *skb,
1333                             void *ctx)
1334 {
1335         struct ieee80211_local *local;
1336         struct ieee80211_hw *hw;
1337         struct txq_info *txqi;
1338
1339         txqi = ctx;
1340         local = vif_to_sdata(txqi->txq.vif)->local;
1341         hw = &local->hw;
1342
1343         ieee80211_free_txskb(hw, skb);
1344 }
1345
1346 static struct sk_buff *fq_tin_dequeue_func(struct fq *fq,
1347                                            struct fq_tin *tin,
1348                                            struct fq_flow *flow)
1349 {
1350         struct ieee80211_local *local;
1351         struct txq_info *txqi;
1352         struct codel_vars *cvars;
1353         struct codel_params *cparams;
1354         struct codel_stats *cstats;
1355
1356         local = container_of(fq, struct ieee80211_local, fq);
1357         txqi = container_of(tin, struct txq_info, tin);
1358         cstats = &txqi->cstats;
1359
1360         if (txqi->txq.sta) {
1361                 struct sta_info *sta = container_of(txqi->txq.sta,
1362                                                     struct sta_info, sta);
1363                 cparams = &sta->cparams;
1364         } else {
1365                 cparams = &local->cparams;
1366         }
1367
1368         if (flow == &txqi->def_flow)
1369                 cvars = &txqi->def_cvars;
1370         else
1371                 cvars = &local->cvars[flow - fq->flows];
1372
1373         return codel_dequeue(txqi,
1374                              &flow->backlog,
1375                              cparams,
1376                              cvars,
1377                              cstats,
1378                              codel_skb_len_func,
1379                              codel_skb_time_func,
1380                              codel_drop_func,
1381                              codel_dequeue_func);
1382 }
1383
1384 static void fq_skb_free_func(struct fq *fq,
1385                              struct fq_tin *tin,
1386                              struct fq_flow *flow,
1387                              struct sk_buff *skb)
1388 {
1389         struct ieee80211_local *local;
1390
1391         local = container_of(fq, struct ieee80211_local, fq);
1392         ieee80211_free_txskb(&local->hw, skb);
1393 }
1394
1395 static struct fq_flow *fq_flow_get_default_func(struct fq *fq,
1396                                                 struct fq_tin *tin,
1397                                                 int idx,
1398                                                 struct sk_buff *skb)
1399 {
1400         struct txq_info *txqi;
1401
1402         txqi = container_of(tin, struct txq_info, tin);
1403         return &txqi->def_flow;
1404 }
1405
1406 static void ieee80211_txq_enqueue(struct ieee80211_local *local,
1407                                   struct txq_info *txqi,
1408                                   struct sk_buff *skb)
1409 {
1410         struct fq *fq = &local->fq;
1411         struct fq_tin *tin = &txqi->tin;
1412         u32 flow_idx = fq_flow_idx(fq, skb);
1413
1414         ieee80211_set_skb_enqueue_time(skb);
1415
1416         spin_lock_bh(&fq->lock);
1417         fq_tin_enqueue(fq, tin, flow_idx, skb,
1418                        fq_skb_free_func,
1419                        fq_flow_get_default_func);
1420         spin_unlock_bh(&fq->lock);
1421 }
1422
1423 static bool fq_vlan_filter_func(struct fq *fq, struct fq_tin *tin,
1424                                 struct fq_flow *flow, struct sk_buff *skb,
1425                                 void *data)
1426 {
1427         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1428
1429         return info->control.vif == data;
1430 }
1431
1432 void ieee80211_txq_remove_vlan(struct ieee80211_local *local,
1433                                struct ieee80211_sub_if_data *sdata)
1434 {
1435         struct fq *fq = &local->fq;
1436         struct txq_info *txqi;
1437         struct fq_tin *tin;
1438         struct ieee80211_sub_if_data *ap;
1439
1440         if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP_VLAN))
1441                 return;
1442
1443         ap = container_of(sdata->bss, struct ieee80211_sub_if_data, u.ap);
1444
1445         if (!ap->vif.txq)
1446                 return;
1447
1448         txqi = to_txq_info(ap->vif.txq);
1449         tin = &txqi->tin;
1450
1451         spin_lock_bh(&fq->lock);
1452         fq_tin_filter(fq, tin, fq_vlan_filter_func, &sdata->vif,
1453                       fq_skb_free_func);
1454         spin_unlock_bh(&fq->lock);
1455 }
1456
1457 void ieee80211_txq_init(struct ieee80211_sub_if_data *sdata,
1458                         struct sta_info *sta,
1459                         struct txq_info *txqi, int tid)
1460 {
1461         fq_tin_init(&txqi->tin);
1462         fq_flow_init(&txqi->def_flow);
1463         codel_vars_init(&txqi->def_cvars);
1464         codel_stats_init(&txqi->cstats);
1465         __skb_queue_head_init(&txqi->frags);
1466         INIT_LIST_HEAD(&txqi->schedule_order);
1467
1468         txqi->txq.vif = &sdata->vif;
1469
1470         if (!sta) {
1471                 sdata->vif.txq = &txqi->txq;
1472                 txqi->txq.tid = 0;
1473                 txqi->txq.ac = IEEE80211_AC_BE;
1474
1475                 return;
1476         }
1477
1478         if (tid == IEEE80211_NUM_TIDS) {
1479                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
1480                         /* Drivers need to opt in to the management MPDU TXQ */
1481                         if (!ieee80211_hw_check(&sdata->local->hw,
1482                                                 STA_MMPDU_TXQ))
1483                                 return;
1484                 } else if (!ieee80211_hw_check(&sdata->local->hw,
1485                                                BUFF_MMPDU_TXQ)) {
1486                         /* Drivers need to opt in to the bufferable MMPDU TXQ */
1487                         return;
1488                 }
1489                 txqi->txq.ac = IEEE80211_AC_VO;
1490         } else {
1491                 txqi->txq.ac = ieee80211_ac_from_tid(tid);
1492         }
1493
1494         txqi->txq.sta = &sta->sta;
1495         txqi->txq.tid = tid;
1496         sta->sta.txq[tid] = &txqi->txq;
1497 }
1498
1499 void ieee80211_txq_purge(struct ieee80211_local *local,
1500                          struct txq_info *txqi)
1501 {
1502         struct fq *fq = &local->fq;
1503         struct fq_tin *tin = &txqi->tin;
1504
1505         spin_lock_bh(&fq->lock);
1506         fq_tin_reset(fq, tin, fq_skb_free_func);
1507         ieee80211_purge_tx_queue(&local->hw, &txqi->frags);
1508         spin_unlock_bh(&fq->lock);
1509
1510         spin_lock_bh(&local->active_txq_lock[txqi->txq.ac]);
1511         list_del_init(&txqi->schedule_order);
1512         spin_unlock_bh(&local->active_txq_lock[txqi->txq.ac]);
1513 }
1514
1515 void ieee80211_txq_set_params(struct ieee80211_local *local)
1516 {
1517         if (local->hw.wiphy->txq_limit)
1518                 local->fq.limit = local->hw.wiphy->txq_limit;
1519         else
1520                 local->hw.wiphy->txq_limit = local->fq.limit;
1521
1522         if (local->hw.wiphy->txq_memory_limit)
1523                 local->fq.memory_limit = local->hw.wiphy->txq_memory_limit;
1524         else
1525                 local->hw.wiphy->txq_memory_limit = local->fq.memory_limit;
1526
1527         if (local->hw.wiphy->txq_quantum)
1528                 local->fq.quantum = local->hw.wiphy->txq_quantum;
1529         else
1530                 local->hw.wiphy->txq_quantum = local->fq.quantum;
1531 }
1532
1533 int ieee80211_txq_setup_flows(struct ieee80211_local *local)
1534 {
1535         struct fq *fq = &local->fq;
1536         int ret;
1537         int i;
1538         bool supp_vht = false;
1539         enum nl80211_band band;
1540
1541         if (!local->ops->wake_tx_queue)
1542                 return 0;
1543
1544         ret = fq_init(fq, 4096);
1545         if (ret)
1546                 return ret;
1547
1548         /*
1549          * If the hardware doesn't support VHT, it is safe to limit the maximum
1550          * queue size. 4 Mbytes is 64 max-size aggregates in 802.11n.
1551          */
1552         for (band = 0; band < NUM_NL80211_BANDS; band++) {
1553                 struct ieee80211_supported_band *sband;
1554
1555                 sband = local->hw.wiphy->bands[band];
1556                 if (!sband)
1557                         continue;
1558
1559                 supp_vht = supp_vht || sband->vht_cap.vht_supported;
1560         }
1561
1562         if (!supp_vht)
1563                 fq->memory_limit = 4 << 20; /* 4 Mbytes */
1564
1565         codel_params_init(&local->cparams);
1566         local->cparams.interval = MS2TIME(100);
1567         local->cparams.target = MS2TIME(20);
1568         local->cparams.ecn = true;
1569
1570         local->cvars = kcalloc(fq->flows_cnt, sizeof(local->cvars[0]),
1571                                GFP_KERNEL);
1572         if (!local->cvars) {
1573                 spin_lock_bh(&fq->lock);
1574                 fq_reset(fq, fq_skb_free_func);
1575                 spin_unlock_bh(&fq->lock);
1576                 return -ENOMEM;
1577         }
1578
1579         for (i = 0; i < fq->flows_cnt; i++)
1580                 codel_vars_init(&local->cvars[i]);
1581
1582         ieee80211_txq_set_params(local);
1583
1584         return 0;
1585 }
1586
1587 void ieee80211_txq_teardown_flows(struct ieee80211_local *local)
1588 {
1589         struct fq *fq = &local->fq;
1590
1591         if (!local->ops->wake_tx_queue)
1592                 return;
1593
1594         kfree(local->cvars);
1595         local->cvars = NULL;
1596
1597         spin_lock_bh(&fq->lock);
1598         fq_reset(fq, fq_skb_free_func);
1599         spin_unlock_bh(&fq->lock);
1600 }
1601
1602 static bool ieee80211_queue_skb(struct ieee80211_local *local,
1603                                 struct ieee80211_sub_if_data *sdata,
1604                                 struct sta_info *sta,
1605                                 struct sk_buff *skb)
1606 {
1607         struct ieee80211_vif *vif;
1608         struct txq_info *txqi;
1609
1610         if (!local->ops->wake_tx_queue ||
1611             sdata->vif.type == NL80211_IFTYPE_MONITOR)
1612                 return false;
1613
1614         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
1615                 sdata = container_of(sdata->bss,
1616                                      struct ieee80211_sub_if_data, u.ap);
1617
1618         vif = &sdata->vif;
1619         txqi = ieee80211_get_txq(local, vif, sta, skb);
1620
1621         if (!txqi)
1622                 return false;
1623
1624         ieee80211_txq_enqueue(local, txqi, skb);
1625
1626         schedule_and_wake_txq(local, txqi);
1627
1628         return true;
1629 }
1630
1631 static bool ieee80211_tx_frags(struct ieee80211_local *local,
1632                                struct ieee80211_vif *vif,
1633                                struct sta_info *sta,
1634                                struct sk_buff_head *skbs,
1635                                bool txpending)
1636 {
1637         struct ieee80211_tx_control control = {};
1638         struct sk_buff *skb, *tmp;
1639         unsigned long flags;
1640
1641         skb_queue_walk_safe(skbs, skb, tmp) {
1642                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1643                 int q = info->hw_queue;
1644
1645 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1646                 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1647                         __skb_unlink(skb, skbs);
1648                         ieee80211_free_txskb(&local->hw, skb);
1649                         continue;
1650                 }
1651 #endif
1652
1653                 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1654                 if (local->queue_stop_reasons[q] ||
1655                     (!txpending && !skb_queue_empty(&local->pending[q]))) {
1656                         if (unlikely(info->flags &
1657                                      IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1658                                 if (local->queue_stop_reasons[q] &
1659                                     ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1660                                         /*
1661                                          * Drop off-channel frames if queues
1662                                          * are stopped for any reason other
1663                                          * than off-channel operation. Never
1664                                          * queue them.
1665                                          */
1666                                         spin_unlock_irqrestore(
1667                                                 &local->queue_stop_reason_lock,
1668                                                 flags);
1669                                         ieee80211_purge_tx_queue(&local->hw,
1670                                                                  skbs);
1671                                         return true;
1672                                 }
1673                         } else {
1674
1675                                 /*
1676                                  * Since queue is stopped, queue up frames for
1677                                  * later transmission from the tx-pending
1678                                  * tasklet when the queue is woken again.
1679                                  */
1680                                 if (txpending)
1681                                         skb_queue_splice_init(skbs,
1682                                                               &local->pending[q]);
1683                                 else
1684                                         skb_queue_splice_tail_init(skbs,
1685                                                                    &local->pending[q]);
1686
1687                                 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1688                                                        flags);
1689                                 return false;
1690                         }
1691                 }
1692                 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1693
1694                 info->control.vif = vif;
1695                 control.sta = sta ? &sta->sta : NULL;
1696
1697                 __skb_unlink(skb, skbs);
1698                 drv_tx(local, &control, skb);
1699         }
1700
1701         return true;
1702 }
1703
1704 /*
1705  * Returns false if the frame couldn't be transmitted but was queued instead.
1706  */
1707 static bool __ieee80211_tx(struct ieee80211_local *local,
1708                            struct sk_buff_head *skbs, int led_len,
1709                            struct sta_info *sta, bool txpending)
1710 {
1711         struct ieee80211_tx_info *info;
1712         struct ieee80211_sub_if_data *sdata;
1713         struct ieee80211_vif *vif;
1714         struct sk_buff *skb;
1715         bool result = true;
1716         __le16 fc;
1717
1718         if (WARN_ON(skb_queue_empty(skbs)))
1719                 return true;
1720
1721         skb = skb_peek(skbs);
1722         fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1723         info = IEEE80211_SKB_CB(skb);
1724         sdata = vif_to_sdata(info->control.vif);
1725         if (sta && !sta->uploaded)
1726                 sta = NULL;
1727
1728         switch (sdata->vif.type) {
1729         case NL80211_IFTYPE_MONITOR:
1730                 if (sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
1731                         vif = &sdata->vif;
1732                         break;
1733                 }
1734                 sdata = rcu_dereference(local->monitor_sdata);
1735                 if (sdata) {
1736                         vif = &sdata->vif;
1737                         info->hw_queue =
1738                                 vif->hw_queue[skb_get_queue_mapping(skb)];
1739                 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
1740                         ieee80211_purge_tx_queue(&local->hw, skbs);
1741                         return true;
1742                 } else
1743                         vif = NULL;
1744                 break;
1745         case NL80211_IFTYPE_AP_VLAN:
1746                 sdata = container_of(sdata->bss,
1747                                      struct ieee80211_sub_if_data, u.ap);
1748                 fallthrough;
1749         default:
1750                 vif = &sdata->vif;
1751                 break;
1752         }
1753
1754         result = ieee80211_tx_frags(local, vif, sta, skbs, txpending);
1755
1756         ieee80211_tpt_led_trig_tx(local, fc, led_len);
1757
1758         WARN_ON_ONCE(!skb_queue_empty(skbs));
1759
1760         return result;
1761 }
1762
1763 /*
1764  * Invoke TX handlers, return 0 on success and non-zero if the
1765  * frame was dropped or queued.
1766  *
1767  * The handlers are split into an early and late part. The latter is everything
1768  * that can be sensitive to reordering, and will be deferred to after packets
1769  * are dequeued from the intermediate queues (when they are enabled).
1770  */
1771 static int invoke_tx_handlers_early(struct ieee80211_tx_data *tx)
1772 {
1773         ieee80211_tx_result res = TX_DROP;
1774
1775 #define CALL_TXH(txh) \
1776         do {                            \
1777                 res = txh(tx);          \
1778                 if (res != TX_CONTINUE) \
1779                         goto txh_done;  \
1780         } while (0)
1781
1782         CALL_TXH(ieee80211_tx_h_dynamic_ps);
1783         CALL_TXH(ieee80211_tx_h_check_assoc);
1784         CALL_TXH(ieee80211_tx_h_ps_buf);
1785         CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
1786         CALL_TXH(ieee80211_tx_h_select_key);
1787         if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1788                 CALL_TXH(ieee80211_tx_h_rate_ctrl);
1789
1790  txh_done:
1791         if (unlikely(res == TX_DROP)) {
1792                 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1793                 if (tx->skb)
1794                         ieee80211_free_txskb(&tx->local->hw, tx->skb);
1795                 else
1796                         ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1797                 return -1;
1798         } else if (unlikely(res == TX_QUEUED)) {
1799                 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1800                 return -1;
1801         }
1802
1803         return 0;
1804 }
1805
1806 /*
1807  * Late handlers can be called while the sta lock is held. Handlers that can
1808  * cause packets to be generated will cause deadlock!
1809  */
1810 static int invoke_tx_handlers_late(struct ieee80211_tx_data *tx)
1811 {
1812         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1813         ieee80211_tx_result res = TX_CONTINUE;
1814
1815         if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1816                 __skb_queue_tail(&tx->skbs, tx->skb);
1817                 tx->skb = NULL;
1818                 goto txh_done;
1819         }
1820
1821         CALL_TXH(ieee80211_tx_h_michael_mic_add);
1822         CALL_TXH(ieee80211_tx_h_sequence);
1823         CALL_TXH(ieee80211_tx_h_fragment);
1824         /* handlers after fragment must be aware of tx info fragmentation! */
1825         CALL_TXH(ieee80211_tx_h_stats);
1826         CALL_TXH(ieee80211_tx_h_encrypt);
1827         if (!ieee80211_hw_check(&tx->local->hw, HAS_RATE_CONTROL))
1828                 CALL_TXH(ieee80211_tx_h_calculate_duration);
1829 #undef CALL_TXH
1830
1831  txh_done:
1832         if (unlikely(res == TX_DROP)) {
1833                 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1834                 if (tx->skb)
1835                         ieee80211_free_txskb(&tx->local->hw, tx->skb);
1836                 else
1837                         ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1838                 return -1;
1839         } else if (unlikely(res == TX_QUEUED)) {
1840                 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1841                 return -1;
1842         }
1843
1844         return 0;
1845 }
1846
1847 static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1848 {
1849         int r = invoke_tx_handlers_early(tx);
1850
1851         if (r)
1852                 return r;
1853         return invoke_tx_handlers_late(tx);
1854 }
1855
1856 bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1857                               struct ieee80211_vif *vif, struct sk_buff *skb,
1858                               int band, struct ieee80211_sta **sta)
1859 {
1860         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1861         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1862         struct ieee80211_tx_data tx;
1863         struct sk_buff *skb2;
1864
1865         if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
1866                 return false;
1867
1868         info->band = band;
1869         info->control.vif = vif;
1870         info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1871
1872         if (invoke_tx_handlers(&tx))
1873                 return false;
1874
1875         if (sta) {
1876                 if (tx.sta)
1877                         *sta = &tx.sta->sta;
1878                 else
1879                         *sta = NULL;
1880         }
1881
1882         /* this function isn't suitable for fragmented data frames */
1883         skb2 = __skb_dequeue(&tx.skbs);
1884         if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1885                 ieee80211_free_txskb(hw, skb2);
1886                 ieee80211_purge_tx_queue(hw, &tx.skbs);
1887                 return false;
1888         }
1889
1890         return true;
1891 }
1892 EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1893
1894 /*
1895  * Returns false if the frame couldn't be transmitted but was queued instead.
1896  */
1897 static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1898                          struct sta_info *sta, struct sk_buff *skb,
1899                          bool txpending)
1900 {
1901         struct ieee80211_local *local = sdata->local;
1902         struct ieee80211_tx_data tx;
1903         ieee80211_tx_result res_prepare;
1904         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1905         bool result = true;
1906         int led_len;
1907
1908         if (unlikely(skb->len < 10)) {
1909                 dev_kfree_skb(skb);
1910                 return true;
1911         }
1912
1913         /* initialises tx */
1914         led_len = skb->len;
1915         res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
1916
1917         if (unlikely(res_prepare == TX_DROP)) {
1918                 ieee80211_free_txskb(&local->hw, skb);
1919                 return true;
1920         } else if (unlikely(res_prepare == TX_QUEUED)) {
1921                 return true;
1922         }
1923
1924         /* set up hw_queue value early */
1925         if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
1926             !ieee80211_hw_check(&local->hw, QUEUE_CONTROL))
1927                 info->hw_queue =
1928                         sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1929
1930         if (invoke_tx_handlers_early(&tx))
1931                 return true;
1932
1933         if (ieee80211_queue_skb(local, sdata, tx.sta, tx.skb))
1934                 return true;
1935
1936         if (!invoke_tx_handlers_late(&tx))
1937                 result = __ieee80211_tx(local, &tx.skbs, led_len,
1938                                         tx.sta, txpending);
1939
1940         return result;
1941 }
1942
1943 /* device xmit handlers */
1944
1945 enum ieee80211_encrypt {
1946         ENCRYPT_NO,
1947         ENCRYPT_MGMT,
1948         ENCRYPT_DATA,
1949 };
1950
1951 static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
1952                                 struct sk_buff *skb,
1953                                 int head_need,
1954                                 enum ieee80211_encrypt encrypt)
1955 {
1956         struct ieee80211_local *local = sdata->local;
1957         bool enc_tailroom;
1958         int tail_need = 0;
1959
1960         enc_tailroom = encrypt == ENCRYPT_MGMT ||
1961                        (encrypt == ENCRYPT_DATA &&
1962                         sdata->crypto_tx_tailroom_needed_cnt);
1963
1964         if (enc_tailroom) {
1965                 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1966                 tail_need -= skb_tailroom(skb);
1967                 tail_need = max_t(int, tail_need, 0);
1968         }
1969
1970         if (skb_cloned(skb) &&
1971             (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
1972              !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
1973                 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1974         else if (head_need || tail_need)
1975                 I802_DEBUG_INC(local->tx_expand_skb_head);
1976         else
1977                 return 0;
1978
1979         if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1980                 wiphy_debug(local->hw.wiphy,
1981                             "failed to reallocate TX buffer\n");
1982                 return -ENOMEM;
1983         }
1984
1985         return 0;
1986 }
1987
1988 void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
1989                     struct sta_info *sta, struct sk_buff *skb)
1990 {
1991         struct ieee80211_local *local = sdata->local;
1992         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1993         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1994         int headroom;
1995         enum ieee80211_encrypt encrypt;
1996
1997         if (info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)
1998                 encrypt = ENCRYPT_NO;
1999         else if (ieee80211_is_mgmt(hdr->frame_control))
2000                 encrypt = ENCRYPT_MGMT;
2001         else
2002                 encrypt = ENCRYPT_DATA;
2003
2004         headroom = local->tx_headroom;
2005         if (encrypt != ENCRYPT_NO)
2006                 headroom += sdata->encrypt_headroom;
2007         headroom -= skb_headroom(skb);
2008         headroom = max_t(int, 0, headroom);
2009
2010         if (ieee80211_skb_resize(sdata, skb, headroom, encrypt)) {
2011                 ieee80211_free_txskb(&local->hw, skb);
2012                 return;
2013         }
2014
2015         /* reload after potential resize */
2016         hdr = (struct ieee80211_hdr *) skb->data;
2017         info->control.vif = &sdata->vif;
2018
2019         if (ieee80211_vif_is_mesh(&sdata->vif)) {
2020                 if (ieee80211_is_data(hdr->frame_control) &&
2021                     is_unicast_ether_addr(hdr->addr1)) {
2022                         if (mesh_nexthop_resolve(sdata, skb))
2023                                 return; /* skb queued: don't free */
2024                 } else {
2025                         ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
2026                 }
2027         }
2028
2029         ieee80211_set_qos_hdr(sdata, skb);
2030         ieee80211_tx(sdata, sta, skb, false);
2031 }
2032
2033 bool ieee80211_parse_tx_radiotap(struct sk_buff *skb,
2034                                  struct net_device *dev)
2035 {
2036         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2037         struct ieee80211_radiotap_iterator iterator;
2038         struct ieee80211_radiotap_header *rthdr =
2039                 (struct ieee80211_radiotap_header *) skb->data;
2040         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2041         struct ieee80211_supported_band *sband =
2042                 local->hw.wiphy->bands[info->band];
2043         int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
2044                                                    NULL);
2045         u16 txflags;
2046         u16 rate = 0;
2047         bool rate_found = false;
2048         u8 rate_retries = 0;
2049         u16 rate_flags = 0;
2050         u8 mcs_known, mcs_flags, mcs_bw;
2051         u16 vht_known;
2052         u8 vht_mcs = 0, vht_nss = 0;
2053         int i;
2054
2055         /* check for not even having the fixed radiotap header part */
2056         if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
2057                 return false; /* too short to be possibly valid */
2058
2059         /* is it a header version we can trust to find length from? */
2060         if (unlikely(rthdr->it_version))
2061                 return false; /* only version 0 is supported */
2062
2063         /* does the skb contain enough to deliver on the alleged length? */
2064         if (unlikely(skb->len < ieee80211_get_radiotap_len(skb->data)))
2065                 return false; /* skb too short for claimed rt header extent */
2066
2067         info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
2068                        IEEE80211_TX_CTL_DONTFRAG;
2069
2070         /*
2071          * for every radiotap entry that is present
2072          * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
2073          * entries present, or -EINVAL on error)
2074          */
2075
2076         while (!ret) {
2077                 ret = ieee80211_radiotap_iterator_next(&iterator);
2078
2079                 if (ret)
2080                         continue;
2081
2082                 /* see if this argument is something we can use */
2083                 switch (iterator.this_arg_index) {
2084                 /*
2085                  * You must take care when dereferencing iterator.this_arg
2086                  * for multibyte types... the pointer is not aligned.  Use
2087                  * get_unaligned((type *)iterator.this_arg) to dereference
2088                  * iterator.this_arg for type "type" safely on all arches.
2089                 */
2090                 case IEEE80211_RADIOTAP_FLAGS:
2091                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
2092                                 /*
2093                                  * this indicates that the skb we have been
2094                                  * handed has the 32-bit FCS CRC at the end...
2095                                  * we should react to that by snipping it off
2096                                  * because it will be recomputed and added
2097                                  * on transmission
2098                                  */
2099                                 if (skb->len < (iterator._max_length + FCS_LEN))
2100                                         return false;
2101
2102                                 skb_trim(skb, skb->len - FCS_LEN);
2103                         }
2104                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
2105                                 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
2106                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
2107                                 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
2108                         break;
2109
2110                 case IEEE80211_RADIOTAP_TX_FLAGS:
2111                         txflags = get_unaligned_le16(iterator.this_arg);
2112                         if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
2113                                 info->flags |= IEEE80211_TX_CTL_NO_ACK;
2114                         if (txflags & IEEE80211_RADIOTAP_F_TX_NOSEQNO)
2115                                 info->control.flags |= IEEE80211_TX_CTRL_NO_SEQNO;
2116                         break;
2117
2118                 case IEEE80211_RADIOTAP_RATE:
2119                         rate = *iterator.this_arg;
2120                         rate_flags = 0;
2121                         rate_found = true;
2122                         break;
2123
2124                 case IEEE80211_RADIOTAP_DATA_RETRIES:
2125                         rate_retries = *iterator.this_arg;
2126                         break;
2127
2128                 case IEEE80211_RADIOTAP_MCS:
2129                         mcs_known = iterator.this_arg[0];
2130                         mcs_flags = iterator.this_arg[1];
2131                         if (!(mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_MCS))
2132                                 break;
2133
2134                         rate_found = true;
2135                         rate = iterator.this_arg[2];
2136                         rate_flags = IEEE80211_TX_RC_MCS;
2137
2138                         if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_GI &&
2139                             mcs_flags & IEEE80211_RADIOTAP_MCS_SGI)
2140                                 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2141
2142                         mcs_bw = mcs_flags & IEEE80211_RADIOTAP_MCS_BW_MASK;
2143                         if (mcs_known & IEEE80211_RADIOTAP_MCS_HAVE_BW &&
2144                             mcs_bw == IEEE80211_RADIOTAP_MCS_BW_40)
2145                                 rate_flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
2146                         break;
2147
2148                 case IEEE80211_RADIOTAP_VHT:
2149                         vht_known = get_unaligned_le16(iterator.this_arg);
2150                         rate_found = true;
2151
2152                         rate_flags = IEEE80211_TX_RC_VHT_MCS;
2153                         if ((vht_known & IEEE80211_RADIOTAP_VHT_KNOWN_GI) &&
2154                             (iterator.this_arg[2] &
2155                              IEEE80211_RADIOTAP_VHT_FLAG_SGI))
2156                                 rate_flags |= IEEE80211_TX_RC_SHORT_GI;
2157                         if (vht_known &
2158                             IEEE80211_RADIOTAP_VHT_KNOWN_BANDWIDTH) {
2159                                 if (iterator.this_arg[3] == 1)
2160                                         rate_flags |=
2161                                                 IEEE80211_TX_RC_40_MHZ_WIDTH;
2162                                 else if (iterator.this_arg[3] == 4)
2163                                         rate_flags |=
2164                                                 IEEE80211_TX_RC_80_MHZ_WIDTH;
2165                                 else if (iterator.this_arg[3] == 11)
2166                                         rate_flags |=
2167                                                 IEEE80211_TX_RC_160_MHZ_WIDTH;
2168                         }
2169
2170                         vht_mcs = iterator.this_arg[4] >> 4;
2171                         vht_nss = iterator.this_arg[4] & 0xF;
2172                         break;
2173
2174                 /*
2175                  * Please update the file
2176                  * Documentation/networking/mac80211-injection.rst
2177                  * when parsing new fields here.
2178                  */
2179
2180                 default:
2181                         break;
2182                 }
2183         }
2184
2185         if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
2186                 return false;
2187
2188         if (rate_found) {
2189                 info->control.flags |= IEEE80211_TX_CTRL_RATE_INJECT;
2190
2191                 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
2192                         info->control.rates[i].idx = -1;
2193                         info->control.rates[i].flags = 0;
2194                         info->control.rates[i].count = 0;
2195                 }
2196
2197                 if (rate_flags & IEEE80211_TX_RC_MCS) {
2198                         info->control.rates[0].idx = rate;
2199                 } else if (rate_flags & IEEE80211_TX_RC_VHT_MCS) {
2200                         ieee80211_rate_set_vht(info->control.rates, vht_mcs,
2201                                                vht_nss);
2202                 } else {
2203                         for (i = 0; i < sband->n_bitrates; i++) {
2204                                 if (rate * 5 != sband->bitrates[i].bitrate)
2205                                         continue;
2206
2207                                 info->control.rates[0].idx = i;
2208                                 break;
2209                         }
2210                 }
2211
2212                 if (info->control.rates[0].idx < 0)
2213                         info->control.flags &= ~IEEE80211_TX_CTRL_RATE_INJECT;
2214
2215                 info->control.rates[0].flags = rate_flags;
2216                 info->control.rates[0].count = min_t(u8, rate_retries + 1,
2217                                                      local->hw.max_rate_tries);
2218         }
2219
2220         return true;
2221 }
2222
2223 netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
2224                                          struct net_device *dev)
2225 {
2226         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2227         struct ieee80211_chanctx_conf *chanctx_conf;
2228         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2229         struct ieee80211_hdr *hdr;
2230         struct ieee80211_sub_if_data *tmp_sdata, *sdata;
2231         struct cfg80211_chan_def *chandef;
2232         u16 len_rthdr;
2233         int hdrlen;
2234
2235         memset(info, 0, sizeof(*info));
2236         info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
2237                       IEEE80211_TX_CTL_INJECTED;
2238
2239         /* Sanity-check and process the injection radiotap header */
2240         if (!ieee80211_parse_tx_radiotap(skb, dev))
2241                 goto fail;
2242
2243         /* we now know there is a radiotap header with a length we can use */
2244         len_rthdr = ieee80211_get_radiotap_len(skb->data);
2245
2246         /*
2247          * fix up the pointers accounting for the radiotap
2248          * header still being in there.  We are being given
2249          * a precooked IEEE80211 header so no need for
2250          * normal processing
2251          */
2252         skb_set_mac_header(skb, len_rthdr);
2253         /*
2254          * these are just fixed to the end of the rt area since we
2255          * don't have any better information and at this point, nobody cares
2256          */
2257         skb_set_network_header(skb, len_rthdr);
2258         skb_set_transport_header(skb, len_rthdr);
2259
2260         if (skb->len < len_rthdr + 2)
2261                 goto fail;
2262
2263         hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
2264         hdrlen = ieee80211_hdrlen(hdr->frame_control);
2265
2266         if (skb->len < len_rthdr + hdrlen)
2267                 goto fail;
2268
2269         /*
2270          * Initialize skb->protocol if the injected frame is a data frame
2271          * carrying a rfc1042 header
2272          */
2273         if (ieee80211_is_data(hdr->frame_control) &&
2274             skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
2275                 u8 *payload = (u8 *)hdr + hdrlen;
2276
2277                 if (ether_addr_equal(payload, rfc1042_header))
2278                         skb->protocol = cpu_to_be16((payload[6] << 8) |
2279                                                     payload[7]);
2280         }
2281
2282         /*
2283          * Initialize skb->priority for QoS frames. This is put in the TID field
2284          * of the frame before passing it to the driver.
2285          */
2286         if (ieee80211_is_data_qos(hdr->frame_control)) {
2287                 u8 *p = ieee80211_get_qos_ctl(hdr);
2288                 skb->priority = *p & IEEE80211_QOS_CTL_TAG1D_MASK;
2289         }
2290
2291         rcu_read_lock();
2292
2293         /*
2294          * We process outgoing injected frames that have a local address
2295          * we handle as though they are non-injected frames.
2296          * This code here isn't entirely correct, the local MAC address
2297          * isn't always enough to find the interface to use; for proper
2298          * VLAN/WDS support we will need a different mechanism (which
2299          * likely isn't going to be monitor interfaces).
2300          *
2301          * This is necessary, for example, for old hostapd versions that
2302          * don't use nl80211-based management TX/RX.
2303          */
2304         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2305
2306         list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
2307                 if (!ieee80211_sdata_running(tmp_sdata))
2308                         continue;
2309                 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
2310                     tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
2311                     tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
2312                         continue;
2313                 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
2314                         sdata = tmp_sdata;
2315                         break;
2316                 }
2317         }
2318
2319         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2320         if (!chanctx_conf) {
2321                 tmp_sdata = rcu_dereference(local->monitor_sdata);
2322                 if (tmp_sdata)
2323                         chanctx_conf =
2324                                 rcu_dereference(tmp_sdata->vif.chanctx_conf);
2325         }
2326
2327         if (chanctx_conf)
2328                 chandef = &chanctx_conf->def;
2329         else if (!local->use_chanctx)
2330                 chandef = &local->_oper_chandef;
2331         else
2332                 goto fail_rcu;
2333
2334         /*
2335          * Frame injection is not allowed if beaconing is not allowed
2336          * or if we need radar detection. Beaconing is usually not allowed when
2337          * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
2338          * Passive scan is also used in world regulatory domains where
2339          * your country is not known and as such it should be treated as
2340          * NO TX unless the channel is explicitly allowed in which case
2341          * your current regulatory domain would not have the passive scan
2342          * flag.
2343          *
2344          * Since AP mode uses monitor interfaces to inject/TX management
2345          * frames we can make AP mode the exception to this rule once it
2346          * supports radar detection as its implementation can deal with
2347          * radar detection by itself. We can do that later by adding a
2348          * monitor flag interfaces used for AP support.
2349          */
2350         if (!cfg80211_reg_can_beacon(local->hw.wiphy, chandef,
2351                                      sdata->vif.type))
2352                 goto fail_rcu;
2353
2354         info->band = chandef->chan->band;
2355
2356         /* remove the injection radiotap header */
2357         skb_pull(skb, len_rthdr);
2358
2359         ieee80211_xmit(sdata, NULL, skb);
2360         rcu_read_unlock();
2361
2362         return NETDEV_TX_OK;
2363
2364 fail_rcu:
2365         rcu_read_unlock();
2366 fail:
2367         dev_kfree_skb(skb);
2368         return NETDEV_TX_OK; /* meaning, we dealt with the skb */
2369 }
2370
2371 static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
2372 {
2373         u16 ethertype = (skb->data[12] << 8) | skb->data[13];
2374
2375         return ethertype == ETH_P_TDLS &&
2376                skb->len > 14 &&
2377                skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
2378 }
2379
2380 int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
2381                             struct sk_buff *skb,
2382                             struct sta_info **sta_out)
2383 {
2384         struct sta_info *sta;
2385
2386         switch (sdata->vif.type) {
2387         case NL80211_IFTYPE_AP_VLAN:
2388                 sta = rcu_dereference(sdata->u.vlan.sta);
2389                 if (sta) {
2390                         *sta_out = sta;
2391                         return 0;
2392                 } else if (sdata->wdev.use_4addr) {
2393                         return -ENOLINK;
2394                 }
2395                 fallthrough;
2396         case NL80211_IFTYPE_AP:
2397         case NL80211_IFTYPE_OCB:
2398         case NL80211_IFTYPE_ADHOC:
2399                 if (is_multicast_ether_addr(skb->data)) {
2400                         *sta_out = ERR_PTR(-ENOENT);
2401                         return 0;
2402                 }
2403                 sta = sta_info_get_bss(sdata, skb->data);
2404                 break;
2405         case NL80211_IFTYPE_WDS:
2406                 sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
2407                 break;
2408 #ifdef CONFIG_MAC80211_MESH
2409         case NL80211_IFTYPE_MESH_POINT:
2410                 /* determined much later */
2411                 *sta_out = NULL;
2412                 return 0;
2413 #endif
2414         case NL80211_IFTYPE_STATION:
2415                 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
2416                         sta = sta_info_get(sdata, skb->data);
2417                         if (sta && test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2418                                 if (test_sta_flag(sta,
2419                                                   WLAN_STA_TDLS_PEER_AUTH)) {
2420                                         *sta_out = sta;
2421                                         return 0;
2422                                 }
2423
2424                                 /*
2425                                  * TDLS link during setup - throw out frames to
2426                                  * peer. Allow TDLS-setup frames to unauthorized
2427                                  * peers for the special case of a link teardown
2428                                  * after a TDLS sta is removed due to being
2429                                  * unreachable.
2430                                  */
2431                                 if (!ieee80211_is_tdls_setup(skb))
2432                                         return -EINVAL;
2433                         }
2434
2435                 }
2436
2437                 sta = sta_info_get(sdata, sdata->u.mgd.bssid);
2438                 if (!sta)
2439                         return -ENOLINK;
2440                 break;
2441         default:
2442                 return -EINVAL;
2443         }
2444
2445         *sta_out = sta ?: ERR_PTR(-ENOENT);
2446         return 0;
2447 }
2448
2449 static u16 ieee80211_store_ack_skb(struct ieee80211_local *local,
2450                                    struct sk_buff *skb,
2451                                    u32 *info_flags,
2452                                    u64 *cookie)
2453 {
2454         struct sk_buff *ack_skb;
2455         u16 info_id = 0;
2456
2457         if (skb->sk)
2458                 ack_skb = skb_clone_sk(skb);
2459         else
2460                 ack_skb = skb_clone(skb, GFP_ATOMIC);
2461
2462         if (ack_skb) {
2463                 unsigned long flags;
2464                 int id;
2465
2466                 spin_lock_irqsave(&local->ack_status_lock, flags);
2467                 id = idr_alloc(&local->ack_status_frames, ack_skb,
2468                                1, 0x2000, GFP_ATOMIC);
2469                 spin_unlock_irqrestore(&local->ack_status_lock, flags);
2470
2471                 if (id >= 0) {
2472                         info_id = id;
2473                         *info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2474                         if (cookie) {
2475                                 *cookie = ieee80211_mgmt_tx_cookie(local);
2476                                 IEEE80211_SKB_CB(ack_skb)->ack.cookie = *cookie;
2477                         }
2478                 } else {
2479                         kfree_skb(ack_skb);
2480                 }
2481         }
2482
2483         return info_id;
2484 }
2485
2486 /**
2487  * ieee80211_build_hdr - build 802.11 header in the given frame
2488  * @sdata: virtual interface to build the header for
2489  * @skb: the skb to build the header in
2490  * @info_flags: skb flags to set
2491  * @sta: the station pointer
2492  * @ctrl_flags: info control flags to set
2493  * @cookie: cookie pointer to fill (if not %NULL)
2494  *
2495  * This function takes the skb with 802.3 header and reformats the header to
2496  * the appropriate IEEE 802.11 header based on which interface the packet is
2497  * being transmitted on.
2498  *
2499  * Note that this function also takes care of the TX status request and
2500  * potential unsharing of the SKB - this needs to be interleaved with the
2501  * header building.
2502  *
2503  * The function requires the read-side RCU lock held
2504  *
2505  * Returns: the (possibly reallocated) skb or an ERR_PTR() code
2506  */
2507 static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
2508                                            struct sk_buff *skb, u32 info_flags,
2509                                            struct sta_info *sta, u32 ctrl_flags,
2510                                            u64 *cookie)
2511 {
2512         struct ieee80211_local *local = sdata->local;
2513         struct ieee80211_tx_info *info;
2514         int head_need;
2515         u16 ethertype, hdrlen,  meshhdrlen = 0;
2516         __le16 fc;
2517         struct ieee80211_hdr hdr;
2518         struct ieee80211s_hdr mesh_hdr __maybe_unused;
2519         struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
2520         const u8 *encaps_data;
2521         int encaps_len, skip_header_bytes;
2522         bool wme_sta = false, authorized = false;
2523         bool tdls_peer;
2524         bool multicast;
2525         u16 info_id = 0;
2526         struct ieee80211_chanctx_conf *chanctx_conf;
2527         struct ieee80211_sub_if_data *ap_sdata;
2528         enum nl80211_band band;
2529         int ret;
2530
2531         if (IS_ERR(sta))
2532                 sta = NULL;
2533
2534 #ifdef CONFIG_MAC80211_DEBUGFS
2535         if (local->force_tx_status)
2536                 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2537 #endif
2538
2539         /* convert Ethernet header to proper 802.11 header (based on
2540          * operation mode) */
2541         ethertype = (skb->data[12] << 8) | skb->data[13];
2542         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2543
2544         switch (sdata->vif.type) {
2545         case NL80211_IFTYPE_AP_VLAN:
2546                 if (sdata->wdev.use_4addr) {
2547                         fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2548                         /* RA TA DA SA */
2549                         memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
2550                         memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2551                         memcpy(hdr.addr3, skb->data, ETH_ALEN);
2552                         memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2553                         hdrlen = 30;
2554                         authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2555                         wme_sta = sta->sta.wme;
2556                 }
2557                 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
2558                                         u.ap);
2559                 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
2560                 if (!chanctx_conf) {
2561                         ret = -ENOTCONN;
2562                         goto free;
2563                 }
2564                 band = chanctx_conf->def.chan->band;
2565                 if (sdata->wdev.use_4addr)
2566                         break;
2567                 fallthrough;
2568         case NL80211_IFTYPE_AP:
2569                 if (sdata->vif.type == NL80211_IFTYPE_AP)
2570                         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2571                 if (!chanctx_conf) {
2572                         ret = -ENOTCONN;
2573                         goto free;
2574                 }
2575                 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
2576                 /* DA BSSID SA */
2577                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2578                 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2579                 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
2580                 hdrlen = 24;
2581                 band = chanctx_conf->def.chan->band;
2582                 break;
2583         case NL80211_IFTYPE_WDS:
2584                 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
2585                 /* RA TA DA SA */
2586                 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
2587                 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2588                 memcpy(hdr.addr3, skb->data, ETH_ALEN);
2589                 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2590                 hdrlen = 30;
2591                 /*
2592                  * This is the exception! WDS style interfaces are prohibited
2593                  * when channel contexts are in used so this must be valid
2594                  */
2595                 band = local->hw.conf.chandef.chan->band;
2596                 break;
2597 #ifdef CONFIG_MAC80211_MESH
2598         case NL80211_IFTYPE_MESH_POINT:
2599                 if (!is_multicast_ether_addr(skb->data)) {
2600                         struct sta_info *next_hop;
2601                         bool mpp_lookup = true;
2602
2603                         mpath = mesh_path_lookup(sdata, skb->data);
2604                         if (mpath) {
2605                                 mpp_lookup = false;
2606                                 next_hop = rcu_dereference(mpath->next_hop);
2607                                 if (!next_hop ||
2608                                     !(mpath->flags & (MESH_PATH_ACTIVE |
2609                                                       MESH_PATH_RESOLVING)))
2610                                         mpp_lookup = true;
2611                         }
2612
2613                         if (mpp_lookup) {
2614                                 mppath = mpp_path_lookup(sdata, skb->data);
2615                                 if (mppath)
2616                                         mppath->exp_time = jiffies;
2617                         }
2618
2619                         if (mppath && mpath)
2620                                 mesh_path_del(sdata, mpath->dst);
2621                 }
2622
2623                 /*
2624                  * Use address extension if it is a packet from
2625                  * another interface or if we know the destination
2626                  * is being proxied by a portal (i.e. portal address
2627                  * differs from proxied address)
2628                  */
2629                 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
2630                     !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
2631                         hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2632                                         skb->data, skb->data + ETH_ALEN);
2633                         meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
2634                                                                NULL, NULL);
2635                 } else {
2636                         /* DS -> MBSS (802.11-2012 13.11.3.3).
2637                          * For unicast with unknown forwarding information,
2638                          * destination might be in the MBSS or if that fails
2639                          * forwarded to another mesh gate. In either case
2640                          * resolution will be handled in ieee80211_xmit(), so
2641                          * leave the original DA. This also works for mcast */
2642                         const u8 *mesh_da = skb->data;
2643
2644                         if (mppath)
2645                                 mesh_da = mppath->mpp;
2646                         else if (mpath)
2647                                 mesh_da = mpath->dst;
2648
2649                         hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
2650                                         mesh_da, sdata->vif.addr);
2651                         if (is_multicast_ether_addr(mesh_da))
2652                                 /* DA TA mSA AE:SA */
2653                                 meshhdrlen = ieee80211_new_mesh_header(
2654                                                 sdata, &mesh_hdr,
2655                                                 skb->data + ETH_ALEN, NULL);
2656                         else
2657                                 /* RA TA mDA mSA AE:DA SA */
2658                                 meshhdrlen = ieee80211_new_mesh_header(
2659                                                 sdata, &mesh_hdr, skb->data,
2660                                                 skb->data + ETH_ALEN);
2661
2662                 }
2663                 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2664                 if (!chanctx_conf) {
2665                         ret = -ENOTCONN;
2666                         goto free;
2667                 }
2668                 band = chanctx_conf->def.chan->band;
2669
2670                 /* For injected frames, fill RA right away as nexthop lookup
2671                  * will be skipped.
2672                  */
2673                 if ((ctrl_flags & IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP) &&
2674                     is_zero_ether_addr(hdr.addr1))
2675                         memcpy(hdr.addr1, skb->data, ETH_ALEN);
2676                 break;
2677 #endif
2678         case NL80211_IFTYPE_STATION:
2679                 /* we already did checks when looking up the RA STA */
2680                 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
2681
2682                 if (tdls_peer) {
2683                         /* DA SA BSSID */
2684                         memcpy(hdr.addr1, skb->data, ETH_ALEN);
2685                         memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2686                         memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
2687                         hdrlen = 24;
2688                 }  else if (sdata->u.mgd.use_4addr &&
2689                             cpu_to_be16(ethertype) != sdata->control_port_protocol) {
2690                         fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2691                                           IEEE80211_FCTL_TODS);
2692                         /* RA TA DA SA */
2693                         memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2694                         memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
2695                         memcpy(hdr.addr3, skb->data, ETH_ALEN);
2696                         memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
2697                         hdrlen = 30;
2698                 } else {
2699                         fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2700                         /* BSSID SA DA */
2701                         memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2702                         memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2703                         memcpy(hdr.addr3, skb->data, ETH_ALEN);
2704                         hdrlen = 24;
2705                 }
2706                 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2707                 if (!chanctx_conf) {
2708                         ret = -ENOTCONN;
2709                         goto free;
2710                 }
2711                 band = chanctx_conf->def.chan->band;
2712                 break;
2713         case NL80211_IFTYPE_OCB:
2714                 /* DA SA BSSID */
2715                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2716                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2717                 eth_broadcast_addr(hdr.addr3);
2718                 hdrlen = 24;
2719                 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2720                 if (!chanctx_conf) {
2721                         ret = -ENOTCONN;
2722                         goto free;
2723                 }
2724                 band = chanctx_conf->def.chan->band;
2725                 break;
2726         case NL80211_IFTYPE_ADHOC:
2727                 /* DA SA BSSID */
2728                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2729                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2730                 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
2731                 hdrlen = 24;
2732                 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2733                 if (!chanctx_conf) {
2734                         ret = -ENOTCONN;
2735                         goto free;
2736                 }
2737                 band = chanctx_conf->def.chan->band;
2738                 break;
2739         default:
2740                 ret = -EINVAL;
2741                 goto free;
2742         }
2743
2744         multicast = is_multicast_ether_addr(hdr.addr1);
2745
2746         /* sta is always NULL for mesh */
2747         if (sta) {
2748                 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2749                 wme_sta = sta->sta.wme;
2750         } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2751                 /* For mesh, the use of the QoS header is mandatory */
2752                 wme_sta = true;
2753         }
2754
2755         /* receiver does QoS (which also means we do) use it */
2756         if (wme_sta) {
2757                 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2758                 hdrlen += 2;
2759         }
2760
2761         /*
2762          * Drop unicast frames to unauthorised stations unless they are
2763          * EAPOL frames from the local station.
2764          */
2765         if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
2766                      (sdata->vif.type != NL80211_IFTYPE_OCB) &&
2767                      !multicast && !authorized &&
2768                      (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
2769                       !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
2770 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2771                 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
2772                                     sdata->name, hdr.addr1);
2773 #endif
2774
2775                 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2776
2777                 ret = -EPERM;
2778                 goto free;
2779         }
2780
2781         if (unlikely(!multicast && ((skb->sk &&
2782                      skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS) ||
2783                      ctrl_flags & IEEE80211_TX_CTL_REQ_TX_STATUS)))
2784                 info_id = ieee80211_store_ack_skb(local, skb, &info_flags,
2785                                                   cookie);
2786
2787         /*
2788          * If the skb is shared we need to obtain our own copy.
2789          */
2790         if (skb_shared(skb)) {
2791                 struct sk_buff *tmp_skb = skb;
2792
2793                 /* can't happen -- skb is a clone if info_id != 0 */
2794                 WARN_ON(info_id);
2795
2796                 skb = skb_clone(skb, GFP_ATOMIC);
2797                 kfree_skb(tmp_skb);
2798
2799                 if (!skb) {
2800                         ret = -ENOMEM;
2801                         goto free;
2802                 }
2803         }
2804
2805         hdr.frame_control = fc;
2806         hdr.duration_id = 0;
2807         hdr.seq_ctrl = 0;
2808
2809         skip_header_bytes = ETH_HLEN;
2810         if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2811                 encaps_data = bridge_tunnel_header;
2812                 encaps_len = sizeof(bridge_tunnel_header);
2813                 skip_header_bytes -= 2;
2814         } else if (ethertype >= ETH_P_802_3_MIN) {
2815                 encaps_data = rfc1042_header;
2816                 encaps_len = sizeof(rfc1042_header);
2817                 skip_header_bytes -= 2;
2818         } else {
2819                 encaps_data = NULL;
2820                 encaps_len = 0;
2821         }
2822
2823         skb_pull(skb, skip_header_bytes);
2824         head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
2825
2826         /*
2827          * So we need to modify the skb header and hence need a copy of
2828          * that. The head_need variable above doesn't, so far, include
2829          * the needed header space that we don't need right away. If we
2830          * can, then we don't reallocate right now but only after the
2831          * frame arrives at the master device (if it does...)
2832          *
2833          * If we cannot, however, then we will reallocate to include all
2834          * the ever needed space. Also, if we need to reallocate it anyway,
2835          * make it big enough for everything we may ever need.
2836          */
2837
2838         if (head_need > 0 || skb_cloned(skb)) {
2839                 head_need += sdata->encrypt_headroom;
2840                 head_need += local->tx_headroom;
2841                 head_need = max_t(int, 0, head_need);
2842                 if (ieee80211_skb_resize(sdata, skb, head_need, ENCRYPT_DATA)) {
2843                         ieee80211_free_txskb(&local->hw, skb);
2844                         skb = NULL;
2845                         return ERR_PTR(-ENOMEM);
2846                 }
2847         }
2848
2849         if (encaps_data)
2850                 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
2851
2852 #ifdef CONFIG_MAC80211_MESH
2853         if (meshhdrlen > 0)
2854                 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
2855 #endif
2856
2857         if (ieee80211_is_data_qos(fc)) {
2858                 __le16 *qos_control;
2859
2860                 qos_control = skb_push(skb, 2);
2861                 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2862                 /*
2863                  * Maybe we could actually set some fields here, for now just
2864                  * initialise to zero to indicate no special operation.
2865                  */
2866                 *qos_control = 0;
2867         } else
2868                 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2869
2870         skb_reset_mac_header(skb);
2871
2872         info = IEEE80211_SKB_CB(skb);
2873         memset(info, 0, sizeof(*info));
2874
2875         info->flags = info_flags;
2876         info->ack_frame_id = info_id;
2877         info->band = band;
2878         info->control.flags = ctrl_flags;
2879
2880         return skb;
2881  free:
2882         kfree_skb(skb);
2883         return ERR_PTR(ret);
2884 }
2885
2886 /*
2887  * fast-xmit overview
2888  *
2889  * The core idea of this fast-xmit is to remove per-packet checks by checking
2890  * them out of band. ieee80211_check_fast_xmit() implements the out-of-band
2891  * checks that are needed to get the sta->fast_tx pointer assigned, after which
2892  * much less work can be done per packet. For example, fragmentation must be
2893  * disabled or the fast_tx pointer will not be set. All the conditions are seen
2894  * in the code here.
2895  *
2896  * Once assigned, the fast_tx data structure also caches the per-packet 802.11
2897  * header and other data to aid packet processing in ieee80211_xmit_fast().
2898  *
2899  * The most difficult part of this is that when any of these assumptions
2900  * change, an external trigger (i.e. a call to ieee80211_clear_fast_xmit(),
2901  * ieee80211_check_fast_xmit() or friends) is required to reset the data,
2902  * since the per-packet code no longer checks the conditions. This is reflected
2903  * by the calls to these functions throughout the rest of the code, and must be
2904  * maintained if any of the TX path checks change.
2905  */
2906
2907 void ieee80211_check_fast_xmit(struct sta_info *sta)
2908 {
2909         struct ieee80211_fast_tx build = {}, *fast_tx = NULL, *old;
2910         struct ieee80211_local *local = sta->local;
2911         struct ieee80211_sub_if_data *sdata = sta->sdata;
2912         struct ieee80211_hdr *hdr = (void *)build.hdr;
2913         struct ieee80211_chanctx_conf *chanctx_conf;
2914         __le16 fc;
2915
2916         if (!ieee80211_hw_check(&local->hw, SUPPORT_FAST_XMIT))
2917                 return;
2918
2919         /* Locking here protects both the pointer itself, and against concurrent
2920          * invocations winning data access races to, e.g., the key pointer that
2921          * is used.
2922          * Without it, the invocation of this function right after the key
2923          * pointer changes wouldn't be sufficient, as another CPU could access
2924          * the pointer, then stall, and then do the cache update after the CPU
2925          * that invalidated the key.
2926          * With the locking, such scenarios cannot happen as the check for the
2927          * key and the fast-tx assignment are done atomically, so the CPU that
2928          * modifies the key will either wait or other one will see the key
2929          * cleared/changed already.
2930          */
2931         spin_lock_bh(&sta->lock);
2932         if (ieee80211_hw_check(&local->hw, SUPPORTS_PS) &&
2933             !ieee80211_hw_check(&local->hw, SUPPORTS_DYNAMIC_PS) &&
2934             sdata->vif.type == NL80211_IFTYPE_STATION)
2935                 goto out;
2936
2937         if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2938                 goto out;
2939
2940         if (test_sta_flag(sta, WLAN_STA_PS_STA) ||
2941             test_sta_flag(sta, WLAN_STA_PS_DRIVER) ||
2942             test_sta_flag(sta, WLAN_STA_PS_DELIVER) ||
2943             test_sta_flag(sta, WLAN_STA_CLEAR_PS_FILT))
2944                 goto out;
2945
2946         if (sdata->noack_map)
2947                 goto out;
2948
2949         /* fast-xmit doesn't handle fragmentation at all */
2950         if (local->hw.wiphy->frag_threshold != (u32)-1 &&
2951             !ieee80211_hw_check(&local->hw, SUPPORTS_TX_FRAG))
2952                 goto out;
2953
2954         rcu_read_lock();
2955         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2956         if (!chanctx_conf) {
2957                 rcu_read_unlock();
2958                 goto out;
2959         }
2960         build.band = chanctx_conf->def.chan->band;
2961         rcu_read_unlock();
2962
2963         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
2964
2965         switch (sdata->vif.type) {
2966         case NL80211_IFTYPE_ADHOC:
2967                 /* DA SA BSSID */
2968                 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2969                 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2970                 memcpy(hdr->addr3, sdata->u.ibss.bssid, ETH_ALEN);
2971                 build.hdr_len = 24;
2972                 break;
2973         case NL80211_IFTYPE_STATION:
2974                 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
2975                         /* DA SA BSSID */
2976                         build.da_offs = offsetof(struct ieee80211_hdr, addr1);
2977                         build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
2978                         memcpy(hdr->addr3, sdata->u.mgd.bssid, ETH_ALEN);
2979                         build.hdr_len = 24;
2980                         break;
2981                 }
2982
2983                 if (sdata->u.mgd.use_4addr) {
2984                         /* non-regular ethertype cannot use the fastpath */
2985                         fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
2986                                           IEEE80211_FCTL_TODS);
2987                         /* RA TA DA SA */
2988                         memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2989                         memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
2990                         build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2991                         build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
2992                         build.hdr_len = 30;
2993                         break;
2994                 }
2995                 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
2996                 /* BSSID SA DA */
2997                 memcpy(hdr->addr1, sdata->u.mgd.bssid, ETH_ALEN);
2998                 build.da_offs = offsetof(struct ieee80211_hdr, addr3);
2999                 build.sa_offs = offsetof(struct ieee80211_hdr, addr2);
3000                 build.hdr_len = 24;
3001                 break;
3002         case NL80211_IFTYPE_AP_VLAN:
3003                 if (sdata->wdev.use_4addr) {
3004                         fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
3005                                           IEEE80211_FCTL_TODS);
3006                         /* RA TA DA SA */
3007                         memcpy(hdr->addr1, sta->sta.addr, ETH_ALEN);
3008                         memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3009                         build.da_offs = offsetof(struct ieee80211_hdr, addr3);
3010                         build.sa_offs = offsetof(struct ieee80211_hdr, addr4);
3011                         build.hdr_len = 30;
3012                         break;
3013                 }
3014                 fallthrough;
3015         case NL80211_IFTYPE_AP:
3016                 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
3017                 /* DA BSSID SA */
3018                 build.da_offs = offsetof(struct ieee80211_hdr, addr1);
3019                 memcpy(hdr->addr2, sdata->vif.addr, ETH_ALEN);
3020                 build.sa_offs = offsetof(struct ieee80211_hdr, addr3);
3021                 build.hdr_len = 24;
3022                 break;
3023         default:
3024                 /* not handled on fast-xmit */
3025                 goto out;
3026         }
3027
3028         if (sta->sta.wme) {
3029                 build.hdr_len += 2;
3030                 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3031         }
3032
3033         /* We store the key here so there's no point in using rcu_dereference()
3034          * but that's fine because the code that changes the pointers will call
3035          * this function after doing so. For a single CPU that would be enough,
3036          * for multiple see the comment above.
3037          */
3038         build.key = rcu_access_pointer(sta->ptk[sta->ptk_idx]);
3039         if (!build.key)
3040                 build.key = rcu_access_pointer(sdata->default_unicast_key);
3041         if (build.key) {
3042                 bool gen_iv, iv_spc, mmic;
3043
3044                 gen_iv = build.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV;
3045                 iv_spc = build.key->conf.flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3046                 mmic = build.key->conf.flags &
3047                         (IEEE80211_KEY_FLAG_GENERATE_MMIC |
3048                          IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
3049
3050                 /* don't handle software crypto */
3051                 if (!(build.key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
3052                         goto out;
3053
3054                 /* Key is being removed */
3055                 if (build.key->flags & KEY_FLAG_TAINTED)
3056                         goto out;
3057
3058                 switch (build.key->conf.cipher) {
3059                 case WLAN_CIPHER_SUITE_CCMP:
3060                 case WLAN_CIPHER_SUITE_CCMP_256:
3061                         if (gen_iv)
3062                                 build.pn_offs = build.hdr_len;
3063                         if (gen_iv || iv_spc)
3064                                 build.hdr_len += IEEE80211_CCMP_HDR_LEN;
3065                         break;
3066                 case WLAN_CIPHER_SUITE_GCMP:
3067                 case WLAN_CIPHER_SUITE_GCMP_256:
3068                         if (gen_iv)
3069                                 build.pn_offs = build.hdr_len;
3070                         if (gen_iv || iv_spc)
3071                                 build.hdr_len += IEEE80211_GCMP_HDR_LEN;
3072                         break;
3073                 case WLAN_CIPHER_SUITE_TKIP:
3074                         /* cannot handle MMIC or IV generation in xmit-fast */
3075                         if (mmic || gen_iv)
3076                                 goto out;
3077                         if (iv_spc)
3078                                 build.hdr_len += IEEE80211_TKIP_IV_LEN;
3079                         break;
3080                 case WLAN_CIPHER_SUITE_WEP40:
3081                 case WLAN_CIPHER_SUITE_WEP104:
3082                         /* cannot handle IV generation in fast-xmit */
3083                         if (gen_iv)
3084                                 goto out;
3085                         if (iv_spc)
3086                                 build.hdr_len += IEEE80211_WEP_IV_LEN;
3087                         break;
3088                 case WLAN_CIPHER_SUITE_AES_CMAC:
3089                 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3090                 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3091                 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3092                         WARN(1,
3093                              "management cipher suite 0x%x enabled for data\n",
3094                              build.key->conf.cipher);
3095                         goto out;
3096                 default:
3097                         /* we don't know how to generate IVs for this at all */
3098                         if (WARN_ON(gen_iv))
3099                                 goto out;
3100                         /* pure hardware keys are OK, of course */
3101                         if (!(build.key->flags & KEY_FLAG_CIPHER_SCHEME))
3102                                 break;
3103                         /* cipher scheme might require space allocation */
3104                         if (iv_spc &&
3105                             build.key->conf.iv_len > IEEE80211_FAST_XMIT_MAX_IV)
3106                                 goto out;
3107                         if (iv_spc)
3108                                 build.hdr_len += build.key->conf.iv_len;
3109                 }
3110
3111                 fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
3112         }
3113
3114         hdr->frame_control = fc;
3115
3116         memcpy(build.hdr + build.hdr_len,
3117                rfc1042_header,  sizeof(rfc1042_header));
3118         build.hdr_len += sizeof(rfc1042_header);
3119
3120         fast_tx = kmemdup(&build, sizeof(build), GFP_ATOMIC);
3121         /* if the kmemdup fails, continue w/o fast_tx */
3122         if (!fast_tx)
3123                 goto out;
3124
3125  out:
3126         /* we might have raced against another call to this function */
3127         old = rcu_dereference_protected(sta->fast_tx,
3128                                         lockdep_is_held(&sta->lock));
3129         rcu_assign_pointer(sta->fast_tx, fast_tx);
3130         if (old)
3131                 kfree_rcu(old, rcu_head);
3132         spin_unlock_bh(&sta->lock);
3133 }
3134
3135 void ieee80211_check_fast_xmit_all(struct ieee80211_local *local)
3136 {
3137         struct sta_info *sta;
3138
3139         rcu_read_lock();
3140         list_for_each_entry_rcu(sta, &local->sta_list, list)
3141                 ieee80211_check_fast_xmit(sta);
3142         rcu_read_unlock();
3143 }
3144
3145 void ieee80211_check_fast_xmit_iface(struct ieee80211_sub_if_data *sdata)
3146 {
3147         struct ieee80211_local *local = sdata->local;
3148         struct sta_info *sta;
3149
3150         rcu_read_lock();
3151
3152         list_for_each_entry_rcu(sta, &local->sta_list, list) {
3153                 if (sdata != sta->sdata &&
3154                     (!sta->sdata->bss || sta->sdata->bss != sdata->bss))
3155                         continue;
3156                 ieee80211_check_fast_xmit(sta);
3157         }
3158
3159         rcu_read_unlock();
3160 }
3161
3162 void ieee80211_clear_fast_xmit(struct sta_info *sta)
3163 {
3164         struct ieee80211_fast_tx *fast_tx;
3165
3166         spin_lock_bh(&sta->lock);
3167         fast_tx = rcu_dereference_protected(sta->fast_tx,
3168                                             lockdep_is_held(&sta->lock));
3169         RCU_INIT_POINTER(sta->fast_tx, NULL);
3170         spin_unlock_bh(&sta->lock);
3171
3172         if (fast_tx)
3173                 kfree_rcu(fast_tx, rcu_head);
3174 }
3175
3176 static bool ieee80211_amsdu_realloc_pad(struct ieee80211_local *local,
3177                                         struct sk_buff *skb, int headroom)
3178 {
3179         if (skb_headroom(skb) < headroom) {
3180                 I802_DEBUG_INC(local->tx_expand_skb_head);
3181
3182                 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
3183                         wiphy_debug(local->hw.wiphy,
3184                                     "failed to reallocate TX buffer\n");
3185                         return false;
3186                 }
3187         }
3188
3189         return true;
3190 }
3191
3192 static bool ieee80211_amsdu_prepare_head(struct ieee80211_sub_if_data *sdata,
3193                                          struct ieee80211_fast_tx *fast_tx,
3194                                          struct sk_buff *skb)
3195 {
3196         struct ieee80211_local *local = sdata->local;
3197         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3198         struct ieee80211_hdr *hdr;
3199         struct ethhdr *amsdu_hdr;
3200         int hdr_len = fast_tx->hdr_len - sizeof(rfc1042_header);
3201         int subframe_len = skb->len - hdr_len;
3202         void *data;
3203         u8 *qc, *h_80211_src, *h_80211_dst;
3204         const u8 *bssid;
3205
3206         if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE)
3207                 return false;
3208
3209         if (info->control.flags & IEEE80211_TX_CTRL_AMSDU)
3210                 return true;
3211
3212         if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(*amsdu_hdr)))
3213                 return false;
3214
3215         data = skb_push(skb, sizeof(*amsdu_hdr));
3216         memmove(data, data + sizeof(*amsdu_hdr), hdr_len);
3217         hdr = data;
3218         amsdu_hdr = data + hdr_len;
3219         /* h_80211_src/dst is addr* field within hdr */
3220         h_80211_src = data + fast_tx->sa_offs;
3221         h_80211_dst = data + fast_tx->da_offs;
3222
3223         amsdu_hdr->h_proto = cpu_to_be16(subframe_len);
3224         ether_addr_copy(amsdu_hdr->h_source, h_80211_src);
3225         ether_addr_copy(amsdu_hdr->h_dest, h_80211_dst);
3226
3227         /* according to IEEE 802.11-2012 8.3.2 table 8-19, the outer SA/DA
3228          * fields needs to be changed to BSSID for A-MSDU frames depending
3229          * on FromDS/ToDS values.
3230          */
3231         switch (sdata->vif.type) {
3232         case NL80211_IFTYPE_STATION:
3233                 bssid = sdata->u.mgd.bssid;
3234                 break;
3235         case NL80211_IFTYPE_AP:
3236         case NL80211_IFTYPE_AP_VLAN:
3237                 bssid = sdata->vif.addr;
3238                 break;
3239         default:
3240                 bssid = NULL;
3241         }
3242
3243         if (bssid && ieee80211_has_fromds(hdr->frame_control))
3244                 ether_addr_copy(h_80211_src, bssid);
3245
3246         if (bssid && ieee80211_has_tods(hdr->frame_control))
3247                 ether_addr_copy(h_80211_dst, bssid);
3248
3249         qc = ieee80211_get_qos_ctl(hdr);
3250         *qc |= IEEE80211_QOS_CTL_A_MSDU_PRESENT;
3251
3252         info->control.flags |= IEEE80211_TX_CTRL_AMSDU;
3253
3254         return true;
3255 }
3256
3257 static bool ieee80211_amsdu_aggregate(struct ieee80211_sub_if_data *sdata,
3258                                       struct sta_info *sta,
3259                                       struct ieee80211_fast_tx *fast_tx,
3260                                       struct sk_buff *skb)
3261 {
3262         struct ieee80211_local *local = sdata->local;
3263         struct fq *fq = &local->fq;
3264         struct fq_tin *tin;
3265         struct fq_flow *flow;
3266         u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3267         struct ieee80211_txq *txq = sta->sta.txq[tid];
3268         struct txq_info *txqi;
3269         struct sk_buff **frag_tail, *head;
3270         int subframe_len = skb->len - ETH_ALEN;
3271         u8 max_subframes = sta->sta.max_amsdu_subframes;
3272         int max_frags = local->hw.max_tx_fragments;
3273         int max_amsdu_len = sta->sta.max_amsdu_len;
3274         int orig_truesize;
3275         u32 flow_idx;
3276         __be16 len;
3277         void *data;
3278         bool ret = false;
3279         unsigned int orig_len;
3280         int n = 2, nfrags, pad = 0;
3281         u16 hdrlen;
3282
3283         if (!ieee80211_hw_check(&local->hw, TX_AMSDU))
3284                 return false;
3285
3286         if (skb_is_gso(skb))
3287                 return false;
3288
3289         if (!txq)
3290                 return false;
3291
3292         txqi = to_txq_info(txq);
3293         if (test_bit(IEEE80211_TXQ_NO_AMSDU, &txqi->flags))
3294                 return false;
3295
3296         if (sta->sta.max_rc_amsdu_len)
3297                 max_amsdu_len = min_t(int, max_amsdu_len,
3298                                       sta->sta.max_rc_amsdu_len);
3299
3300         if (sta->sta.max_tid_amsdu_len[tid])
3301                 max_amsdu_len = min_t(int, max_amsdu_len,
3302                                       sta->sta.max_tid_amsdu_len[tid]);
3303
3304         flow_idx = fq_flow_idx(fq, skb);
3305
3306         spin_lock_bh(&fq->lock);
3307
3308         /* TODO: Ideally aggregation should be done on dequeue to remain
3309          * responsive to environment changes.
3310          */
3311
3312         tin = &txqi->tin;
3313         flow = fq_flow_classify(fq, tin, flow_idx, skb,
3314                                 fq_flow_get_default_func);
3315         head = skb_peek_tail(&flow->queue);
3316         if (!head || skb_is_gso(head))
3317                 goto out;
3318
3319         orig_truesize = head->truesize;
3320         orig_len = head->len;
3321
3322         if (skb->len + head->len > max_amsdu_len)
3323                 goto out;
3324
3325         nfrags = 1 + skb_shinfo(skb)->nr_frags;
3326         nfrags += 1 + skb_shinfo(head)->nr_frags;
3327         frag_tail = &skb_shinfo(head)->frag_list;
3328         while (*frag_tail) {
3329                 nfrags += 1 + skb_shinfo(*frag_tail)->nr_frags;
3330                 frag_tail = &(*frag_tail)->next;
3331                 n++;
3332         }
3333
3334         if (max_subframes && n > max_subframes)
3335                 goto out;
3336
3337         if (max_frags && nfrags > max_frags)
3338                 goto out;
3339
3340         if (!drv_can_aggregate_in_amsdu(local, head, skb))
3341                 goto out;
3342
3343         if (!ieee80211_amsdu_prepare_head(sdata, fast_tx, head))
3344                 goto out;
3345
3346         /*
3347          * Pad out the previous subframe to a multiple of 4 by adding the
3348          * padding to the next one, that's being added. Note that head->len
3349          * is the length of the full A-MSDU, but that works since each time
3350          * we add a new subframe we pad out the previous one to a multiple
3351          * of 4 and thus it no longer matters in the next round.
3352          */
3353         hdrlen = fast_tx->hdr_len - sizeof(rfc1042_header);
3354         if ((head->len - hdrlen) & 3)
3355                 pad = 4 - ((head->len - hdrlen) & 3);
3356
3357         if (!ieee80211_amsdu_realloc_pad(local, skb, sizeof(rfc1042_header) +
3358                                                      2 + pad))
3359                 goto out_recalc;
3360
3361         ret = true;
3362         data = skb_push(skb, ETH_ALEN + 2);
3363         memmove(data, data + ETH_ALEN + 2, 2 * ETH_ALEN);
3364
3365         data += 2 * ETH_ALEN;
3366         len = cpu_to_be16(subframe_len);
3367         memcpy(data, &len, 2);
3368         memcpy(data + 2, rfc1042_header, sizeof(rfc1042_header));
3369
3370         memset(skb_push(skb, pad), 0, pad);
3371
3372         head->len += skb->len;
3373         head->data_len += skb->len;
3374         *frag_tail = skb;
3375
3376 out_recalc:
3377         fq->memory_usage += head->truesize - orig_truesize;
3378         if (head->len != orig_len) {
3379                 flow->backlog += head->len - orig_len;
3380                 tin->backlog_bytes += head->len - orig_len;
3381
3382                 fq_recalc_backlog(fq, tin, flow);
3383         }
3384 out:
3385         spin_unlock_bh(&fq->lock);
3386
3387         return ret;
3388 }
3389
3390 /*
3391  * Can be called while the sta lock is held. Anything that can cause packets to
3392  * be generated will cause deadlock!
3393  */
3394 static void ieee80211_xmit_fast_finish(struct ieee80211_sub_if_data *sdata,
3395                                        struct sta_info *sta, u8 pn_offs,
3396                                        struct ieee80211_key *key,
3397                                        struct sk_buff *skb)
3398 {
3399         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3400         struct ieee80211_hdr *hdr = (void *)skb->data;
3401         u8 tid = IEEE80211_NUM_TIDS;
3402
3403         if (key)
3404                 info->control.hw_key = &key->conf;
3405
3406         ieee80211_tx_stats(skb->dev, skb->len);
3407
3408         if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3409                 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3410                 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, tid);
3411         } else {
3412                 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
3413                 hdr->seq_ctrl = cpu_to_le16(sdata->sequence_number);
3414                 sdata->sequence_number += 0x10;
3415         }
3416
3417         if (skb_shinfo(skb)->gso_size)
3418                 sta->tx_stats.msdu[tid] +=
3419                         DIV_ROUND_UP(skb->len, skb_shinfo(skb)->gso_size);
3420         else
3421                 sta->tx_stats.msdu[tid]++;
3422
3423         info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
3424
3425         /* statistics normally done by ieee80211_tx_h_stats (but that
3426          * has to consider fragmentation, so is more complex)
3427          */
3428         sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
3429         sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
3430
3431         if (pn_offs) {
3432                 u64 pn;
3433                 u8 *crypto_hdr = skb->data + pn_offs;
3434
3435                 switch (key->conf.cipher) {
3436                 case WLAN_CIPHER_SUITE_CCMP:
3437                 case WLAN_CIPHER_SUITE_CCMP_256:
3438                 case WLAN_CIPHER_SUITE_GCMP:
3439                 case WLAN_CIPHER_SUITE_GCMP_256:
3440                         pn = atomic64_inc_return(&key->conf.tx_pn);
3441                         crypto_hdr[0] = pn;
3442                         crypto_hdr[1] = pn >> 8;
3443                         crypto_hdr[3] = 0x20 | (key->conf.keyidx << 6);
3444                         crypto_hdr[4] = pn >> 16;
3445                         crypto_hdr[5] = pn >> 24;
3446                         crypto_hdr[6] = pn >> 32;
3447                         crypto_hdr[7] = pn >> 40;
3448                         break;
3449                 }
3450         }
3451 }
3452
3453 static bool ieee80211_xmit_fast(struct ieee80211_sub_if_data *sdata,
3454                                 struct sta_info *sta,
3455                                 struct ieee80211_fast_tx *fast_tx,
3456                                 struct sk_buff *skb)
3457 {
3458         struct ieee80211_local *local = sdata->local;
3459         u16 ethertype = (skb->data[12] << 8) | skb->data[13];
3460         int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
3461         int hw_headroom = sdata->local->hw.extra_tx_headroom;
3462         struct ethhdr eth;
3463         struct ieee80211_tx_info *info;
3464         struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
3465         struct ieee80211_tx_data tx;
3466         ieee80211_tx_result r;
3467         struct tid_ampdu_tx *tid_tx = NULL;
3468         u8 tid = IEEE80211_NUM_TIDS;
3469
3470         /* control port protocol needs a lot of special handling */
3471         if (cpu_to_be16(ethertype) == sdata->control_port_protocol)
3472                 return false;
3473
3474         /* only RFC 1042 SNAP */
3475         if (ethertype < ETH_P_802_3_MIN)
3476                 return false;
3477
3478         /* don't handle TX status request here either */
3479         if (skb->sk && skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)
3480                 return false;
3481
3482         if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3483                 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3484                 tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
3485                 if (tid_tx) {
3486                         if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state))
3487                                 return false;
3488                         if (tid_tx->timeout)
3489                                 tid_tx->last_tx = jiffies;
3490                 }
3491         }
3492
3493         /* after this point (skb is modified) we cannot return false */
3494
3495         if (skb_shared(skb)) {
3496                 struct sk_buff *tmp_skb = skb;
3497
3498                 skb = skb_clone(skb, GFP_ATOMIC);
3499                 kfree_skb(tmp_skb);
3500
3501                 if (!skb)
3502                         return true;
3503         }
3504
3505         if ((hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) &&
3506             ieee80211_amsdu_aggregate(sdata, sta, fast_tx, skb))
3507                 return true;
3508
3509         /* will not be crypto-handled beyond what we do here, so use false
3510          * as the may-encrypt argument for the resize to not account for
3511          * more room than we already have in 'extra_head'
3512          */
3513         if (unlikely(ieee80211_skb_resize(sdata, skb,
3514                                           max_t(int, extra_head + hw_headroom -
3515                                                      skb_headroom(skb), 0),
3516                                           ENCRYPT_NO))) {
3517                 kfree_skb(skb);
3518                 return true;
3519         }
3520
3521         memcpy(&eth, skb->data, ETH_HLEN - 2);
3522         hdr = skb_push(skb, extra_head);
3523         memcpy(skb->data, fast_tx->hdr, fast_tx->hdr_len);
3524         memcpy(skb->data + fast_tx->da_offs, eth.h_dest, ETH_ALEN);
3525         memcpy(skb->data + fast_tx->sa_offs, eth.h_source, ETH_ALEN);
3526
3527         info = IEEE80211_SKB_CB(skb);
3528         memset(info, 0, sizeof(*info));
3529         info->band = fast_tx->band;
3530         info->control.vif = &sdata->vif;
3531         info->flags = IEEE80211_TX_CTL_FIRST_FRAGMENT |
3532                       IEEE80211_TX_CTL_DONTFRAG |
3533                       (tid_tx ? IEEE80211_TX_CTL_AMPDU : 0);
3534         info->control.flags = IEEE80211_TX_CTRL_FAST_XMIT;
3535
3536 #ifdef CONFIG_MAC80211_DEBUGFS
3537         if (local->force_tx_status)
3538                 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3539 #endif
3540
3541         if (hdr->frame_control & cpu_to_le16(IEEE80211_STYPE_QOS_DATA)) {
3542                 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
3543                 *ieee80211_get_qos_ctl(hdr) = tid;
3544         }
3545
3546         __skb_queue_head_init(&tx.skbs);
3547
3548         tx.flags = IEEE80211_TX_UNICAST;
3549         tx.local = local;
3550         tx.sdata = sdata;
3551         tx.sta = sta;
3552         tx.key = fast_tx->key;
3553
3554         if (!ieee80211_hw_check(&local->hw, HAS_RATE_CONTROL)) {
3555                 tx.skb = skb;
3556                 r = ieee80211_tx_h_rate_ctrl(&tx);
3557                 skb = tx.skb;
3558                 tx.skb = NULL;
3559
3560                 if (r != TX_CONTINUE) {
3561                         if (r != TX_QUEUED)
3562                                 kfree_skb(skb);
3563                         return true;
3564                 }
3565         }
3566
3567         if (ieee80211_queue_skb(local, sdata, sta, skb))
3568                 return true;
3569
3570         ieee80211_xmit_fast_finish(sdata, sta, fast_tx->pn_offs,
3571                                    fast_tx->key, skb);
3572
3573         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
3574                 sdata = container_of(sdata->bss,
3575                                      struct ieee80211_sub_if_data, u.ap);
3576
3577         __skb_queue_tail(&tx.skbs, skb);
3578         ieee80211_tx_frags(local, &sdata->vif, sta, &tx.skbs, false);
3579         return true;
3580 }
3581
3582 struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3583                                      struct ieee80211_txq *txq)
3584 {
3585         struct ieee80211_local *local = hw_to_local(hw);
3586         struct txq_info *txqi = container_of(txq, struct txq_info, txq);
3587         struct ieee80211_hdr *hdr;
3588         struct sk_buff *skb = NULL;
3589         struct fq *fq = &local->fq;
3590         struct fq_tin *tin = &txqi->tin;
3591         struct ieee80211_tx_info *info;
3592         struct ieee80211_tx_data tx;
3593         ieee80211_tx_result r;
3594         struct ieee80211_vif *vif = txq->vif;
3595
3596         WARN_ON_ONCE(softirq_count() == 0);
3597
3598         if (!ieee80211_txq_airtime_check(hw, txq))
3599                 return NULL;
3600
3601 begin:
3602         spin_lock_bh(&fq->lock);
3603
3604         if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags) ||
3605             test_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags))
3606                 goto out;
3607
3608         if (vif->txqs_stopped[ieee80211_ac_from_tid(txq->tid)]) {
3609                 set_bit(IEEE80211_TXQ_STOP_NETIF_TX, &txqi->flags);
3610                 goto out;
3611         }
3612
3613         /* Make sure fragments stay together. */
3614         skb = __skb_dequeue(&txqi->frags);
3615         if (skb)
3616                 goto out;
3617
3618         skb = fq_tin_dequeue(fq, tin, fq_tin_dequeue_func);
3619         if (!skb)
3620                 goto out;
3621
3622         spin_unlock_bh(&fq->lock);
3623
3624         hdr = (struct ieee80211_hdr *)skb->data;
3625         info = IEEE80211_SKB_CB(skb);
3626
3627         memset(&tx, 0, sizeof(tx));
3628         __skb_queue_head_init(&tx.skbs);
3629         tx.local = local;
3630         tx.skb = skb;
3631         tx.sdata = vif_to_sdata(info->control.vif);
3632
3633         if (txq->sta) {
3634                 tx.sta = container_of(txq->sta, struct sta_info, sta);
3635                 /*
3636                  * Drop unicast frames to unauthorised stations unless they are
3637                  * injected frames or EAPOL frames from the local station.
3638                  */
3639                 if (unlikely(!(info->flags & IEEE80211_TX_CTL_INJECTED) &&
3640                              ieee80211_is_data(hdr->frame_control) &&
3641                              !ieee80211_vif_is_mesh(&tx.sdata->vif) &&
3642                              tx.sdata->vif.type != NL80211_IFTYPE_OCB &&
3643                              !is_multicast_ether_addr(hdr->addr1) &&
3644                              !test_sta_flag(tx.sta, WLAN_STA_AUTHORIZED) &&
3645                              (!(info->control.flags &
3646                                 IEEE80211_TX_CTRL_PORT_CTRL_PROTO) ||
3647                               !ether_addr_equal(tx.sdata->vif.addr,
3648                                                 hdr->addr2)))) {
3649                         I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
3650                         ieee80211_free_txskb(&local->hw, skb);
3651                         goto begin;
3652                 }
3653         }
3654
3655         /*
3656          * The key can be removed while the packet was queued, so need to call
3657          * this here to get the current key.
3658          */
3659         r = ieee80211_tx_h_select_key(&tx);
3660         if (r != TX_CONTINUE) {
3661                 ieee80211_free_txskb(&local->hw, skb);
3662                 goto begin;
3663         }
3664
3665         if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
3666                 info->flags |= IEEE80211_TX_CTL_AMPDU;
3667         else
3668                 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
3669
3670         if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP)
3671                 goto encap_out;
3672
3673         if (info->control.flags & IEEE80211_TX_CTRL_FAST_XMIT) {
3674                 struct sta_info *sta = container_of(txq->sta, struct sta_info,
3675                                                     sta);
3676                 u8 pn_offs = 0;
3677
3678                 if (tx.key &&
3679                     (tx.key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV))
3680                         pn_offs = ieee80211_hdrlen(hdr->frame_control);
3681
3682                 ieee80211_xmit_fast_finish(sta->sdata, sta, pn_offs,
3683                                            tx.key, skb);
3684         } else {
3685                 if (invoke_tx_handlers_late(&tx))
3686                         goto begin;
3687
3688                 skb = __skb_dequeue(&tx.skbs);
3689
3690                 if (!skb_queue_empty(&tx.skbs)) {
3691                         spin_lock_bh(&fq->lock);
3692                         skb_queue_splice_tail(&tx.skbs, &txqi->frags);
3693                         spin_unlock_bh(&fq->lock);
3694                 }
3695         }
3696
3697         if (skb_has_frag_list(skb) &&
3698             !ieee80211_hw_check(&local->hw, TX_FRAG_LIST)) {
3699                 if (skb_linearize(skb)) {
3700                         ieee80211_free_txskb(&local->hw, skb);
3701                         goto begin;
3702                 }
3703         }
3704
3705         switch (tx.sdata->vif.type) {
3706         case NL80211_IFTYPE_MONITOR:
3707                 if (tx.sdata->u.mntr.flags & MONITOR_FLAG_ACTIVE) {
3708                         vif = &tx.sdata->vif;
3709                         break;
3710                 }
3711                 tx.sdata = rcu_dereference(local->monitor_sdata);
3712                 if (tx.sdata) {
3713                         vif = &tx.sdata->vif;
3714                         info->hw_queue =
3715                                 vif->hw_queue[skb_get_queue_mapping(skb)];
3716                 } else if (ieee80211_hw_check(&local->hw, QUEUE_CONTROL)) {
3717                         ieee80211_free_txskb(&local->hw, skb);
3718                         goto begin;
3719                 } else {
3720                         vif = NULL;
3721                 }
3722                 break;
3723         case NL80211_IFTYPE_AP_VLAN:
3724                 tx.sdata = container_of(tx.sdata->bss,
3725                                         struct ieee80211_sub_if_data, u.ap);
3726                 fallthrough;
3727         default:
3728                 vif = &tx.sdata->vif;
3729                 break;
3730         }
3731
3732 encap_out:
3733         IEEE80211_SKB_CB(skb)->control.vif = vif;
3734
3735         if (vif &&
3736             wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL)) {
3737                 bool ampdu = txq->ac != IEEE80211_AC_VO;
3738                 u32 airtime;
3739
3740                 airtime = ieee80211_calc_expected_tx_airtime(hw, vif, txq->sta,
3741                                                              skb->len, ampdu);
3742                 if (airtime) {
3743                         airtime = ieee80211_info_set_tx_time_est(info, airtime);
3744                         ieee80211_sta_update_pending_airtime(local, tx.sta,
3745                                                              txq->ac,
3746                                                              airtime,
3747                                                              false);
3748                 }
3749         }
3750
3751         return skb;
3752
3753 out:
3754         spin_unlock_bh(&fq->lock);
3755
3756         return skb;
3757 }
3758 EXPORT_SYMBOL(ieee80211_tx_dequeue);
3759
3760 struct ieee80211_txq *ieee80211_next_txq(struct ieee80211_hw *hw, u8 ac)
3761 {
3762         struct ieee80211_local *local = hw_to_local(hw);
3763         struct ieee80211_txq *ret = NULL;
3764         struct txq_info *txqi = NULL, *head = NULL;
3765         bool found_eligible_txq = false;
3766
3767         spin_lock_bh(&local->active_txq_lock[ac]);
3768
3769  begin:
3770         txqi = list_first_entry_or_null(&local->active_txqs[ac],
3771                                         struct txq_info,
3772                                         schedule_order);
3773         if (!txqi)
3774                 goto out;
3775
3776         if (txqi == head) {
3777                 if (!found_eligible_txq)
3778                         goto out;
3779                 else
3780                         found_eligible_txq = false;
3781         }
3782
3783         if (!head)
3784                 head = txqi;
3785
3786         if (txqi->txq.sta) {
3787                 struct sta_info *sta = container_of(txqi->txq.sta,
3788                                                     struct sta_info, sta);
3789                 bool aql_check = ieee80211_txq_airtime_check(hw, &txqi->txq);
3790                 s64 deficit = sta->airtime[txqi->txq.ac].deficit;
3791
3792                 if (aql_check)
3793                         found_eligible_txq = true;
3794
3795                 if (deficit < 0)
3796                         sta->airtime[txqi->txq.ac].deficit +=
3797                                 sta->airtime_weight;
3798
3799                 if (deficit < 0 || !aql_check) {
3800                         list_move_tail(&txqi->schedule_order,
3801                                        &local->active_txqs[txqi->txq.ac]);
3802                         goto begin;
3803                 }
3804         }
3805
3806
3807         if (txqi->schedule_round == local->schedule_round[ac])
3808                 goto out;
3809
3810         list_del_init(&txqi->schedule_order);
3811         txqi->schedule_round = local->schedule_round[ac];
3812         ret = &txqi->txq;
3813
3814 out:
3815         spin_unlock_bh(&local->active_txq_lock[ac]);
3816         return ret;
3817 }
3818 EXPORT_SYMBOL(ieee80211_next_txq);
3819
3820 void __ieee80211_schedule_txq(struct ieee80211_hw *hw,
3821                               struct ieee80211_txq *txq,
3822                               bool force)
3823 {
3824         struct ieee80211_local *local = hw_to_local(hw);
3825         struct txq_info *txqi = to_txq_info(txq);
3826
3827         spin_lock_bh(&local->active_txq_lock[txq->ac]);
3828
3829         if (list_empty(&txqi->schedule_order) &&
3830             (force || !skb_queue_empty(&txqi->frags) ||
3831              txqi->tin.backlog_packets)) {
3832                 /* If airtime accounting is active, always enqueue STAs at the
3833                  * head of the list to ensure that they only get moved to the
3834                  * back by the airtime DRR scheduler once they have a negative
3835                  * deficit. A station that already has a negative deficit will
3836                  * get immediately moved to the back of the list on the next
3837                  * call to ieee80211_next_txq().
3838                  */
3839                 if (txqi->txq.sta &&
3840                     wiphy_ext_feature_isset(local->hw.wiphy,
3841                                             NL80211_EXT_FEATURE_AIRTIME_FAIRNESS))
3842                         list_add(&txqi->schedule_order,
3843                                  &local->active_txqs[txq->ac]);
3844                 else
3845                         list_add_tail(&txqi->schedule_order,
3846                                       &local->active_txqs[txq->ac]);
3847         }
3848
3849         spin_unlock_bh(&local->active_txq_lock[txq->ac]);
3850 }
3851 EXPORT_SYMBOL(__ieee80211_schedule_txq);
3852
3853 bool ieee80211_txq_airtime_check(struct ieee80211_hw *hw,
3854                                  struct ieee80211_txq *txq)
3855 {
3856         struct sta_info *sta;
3857         struct ieee80211_local *local = hw_to_local(hw);
3858
3859         if (!wiphy_ext_feature_isset(local->hw.wiphy, NL80211_EXT_FEATURE_AQL))
3860                 return true;
3861
3862         if (!txq->sta)
3863                 return true;
3864
3865         sta = container_of(txq->sta, struct sta_info, sta);
3866         if (atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3867             sta->airtime[txq->ac].aql_limit_low)
3868                 return true;
3869
3870         if (atomic_read(&local->aql_total_pending_airtime) <
3871             local->aql_threshold &&
3872             atomic_read(&sta->airtime[txq->ac].aql_tx_pending) <
3873             sta->airtime[txq->ac].aql_limit_high)
3874                 return true;
3875
3876         return false;
3877 }
3878 EXPORT_SYMBOL(ieee80211_txq_airtime_check);
3879
3880 bool ieee80211_txq_may_transmit(struct ieee80211_hw *hw,
3881                                 struct ieee80211_txq *txq)
3882 {
3883         struct ieee80211_local *local = hw_to_local(hw);
3884         struct txq_info *iter, *tmp, *txqi = to_txq_info(txq);
3885         struct sta_info *sta;
3886         u8 ac = txq->ac;
3887
3888         spin_lock_bh(&local->active_txq_lock[ac]);
3889
3890         if (!txqi->txq.sta)
3891                 goto out;
3892
3893         if (list_empty(&txqi->schedule_order))
3894                 goto out;
3895
3896         list_for_each_entry_safe(iter, tmp, &local->active_txqs[ac],
3897                                  schedule_order) {
3898                 if (iter == txqi)
3899                         break;
3900
3901                 if (!iter->txq.sta) {
3902                         list_move_tail(&iter->schedule_order,
3903                                        &local->active_txqs[ac]);
3904                         continue;
3905                 }
3906                 sta = container_of(iter->txq.sta, struct sta_info, sta);
3907                 if (sta->airtime[ac].deficit < 0)
3908                         sta->airtime[ac].deficit += sta->airtime_weight;
3909                 list_move_tail(&iter->schedule_order, &local->active_txqs[ac]);
3910         }
3911
3912         sta = container_of(txqi->txq.sta, struct sta_info, sta);
3913         if (sta->airtime[ac].deficit >= 0)
3914                 goto out;
3915
3916         sta->airtime[ac].deficit += sta->airtime_weight;
3917         list_move_tail(&txqi->schedule_order, &local->active_txqs[ac]);
3918         spin_unlock_bh(&local->active_txq_lock[ac]);
3919
3920         return false;
3921 out:
3922         if (!list_empty(&txqi->schedule_order))
3923                 list_del_init(&txqi->schedule_order);
3924         spin_unlock_bh(&local->active_txq_lock[ac]);
3925
3926         return true;
3927 }
3928 EXPORT_SYMBOL(ieee80211_txq_may_transmit);
3929
3930 void ieee80211_txq_schedule_start(struct ieee80211_hw *hw, u8 ac)
3931 {
3932         struct ieee80211_local *local = hw_to_local(hw);
3933
3934         spin_lock_bh(&local->active_txq_lock[ac]);
3935         local->schedule_round[ac]++;
3936         spin_unlock_bh(&local->active_txq_lock[ac]);
3937 }
3938 EXPORT_SYMBOL(ieee80211_txq_schedule_start);
3939
3940 void __ieee80211_subif_start_xmit(struct sk_buff *skb,
3941                                   struct net_device *dev,
3942                                   u32 info_flags,
3943                                   u32 ctrl_flags,
3944                                   u64 *cookie)
3945 {
3946         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
3947         struct ieee80211_local *local = sdata->local;
3948         struct sta_info *sta;
3949         struct sk_buff *next;
3950
3951         if (unlikely(skb->len < ETH_HLEN)) {
3952                 kfree_skb(skb);
3953                 return;
3954         }
3955
3956         rcu_read_lock();
3957
3958         if (ieee80211_lookup_ra_sta(sdata, skb, &sta))
3959                 goto out_free;
3960
3961         if (IS_ERR(sta))
3962                 sta = NULL;
3963
3964         if (local->ops->wake_tx_queue) {
3965                 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
3966                 skb_set_queue_mapping(skb, queue);
3967                 skb_get_hash(skb);
3968         }
3969
3970         if (sta) {
3971                 struct ieee80211_fast_tx *fast_tx;
3972
3973                 sk_pacing_shift_update(skb->sk, sdata->local->hw.tx_sk_pacing_shift);
3974
3975                 fast_tx = rcu_dereference(sta->fast_tx);
3976
3977                 if (fast_tx &&
3978                     ieee80211_xmit_fast(sdata, sta, fast_tx, skb))
3979                         goto out;
3980         }
3981
3982         if (skb_is_gso(skb)) {
3983                 struct sk_buff *segs;
3984
3985                 segs = skb_gso_segment(skb, 0);
3986                 if (IS_ERR(segs)) {
3987                         goto out_free;
3988                 } else if (segs) {
3989                         consume_skb(skb);
3990                         skb = segs;
3991                 }
3992         } else {
3993                 /* we cannot process non-linear frames on this path */
3994                 if (skb_linearize(skb)) {
3995                         kfree_skb(skb);
3996                         goto out;
3997                 }
3998
3999                 /* the frame could be fragmented, software-encrypted, and other
4000                  * things so we cannot really handle checksum offload with it -
4001                  * fix it up in software before we handle anything else.
4002                  */
4003                 if (skb->ip_summed == CHECKSUM_PARTIAL) {
4004                         skb_set_transport_header(skb,
4005                                                  skb_checksum_start_offset(skb));
4006                         if (skb_checksum_help(skb))
4007                                 goto out_free;
4008                 }
4009         }
4010
4011         skb_list_walk_safe(skb, skb, next) {
4012                 skb_mark_not_on_list(skb);
4013
4014                 if (skb->protocol == sdata->control_port_protocol)
4015                         ctrl_flags |= IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
4016
4017                 skb = ieee80211_build_hdr(sdata, skb, info_flags,
4018                                           sta, ctrl_flags, cookie);
4019                 if (IS_ERR(skb)) {
4020                         kfree_skb_list(next);
4021                         goto out;
4022                 }
4023
4024                 ieee80211_tx_stats(dev, skb->len);
4025
4026                 ieee80211_xmit(sdata, sta, skb);
4027         }
4028         goto out;
4029  out_free:
4030         kfree_skb(skb);
4031  out:
4032         rcu_read_unlock();
4033 }
4034
4035 static int ieee80211_change_da(struct sk_buff *skb, struct sta_info *sta)
4036 {
4037         struct ethhdr *eth;
4038         int err;
4039
4040         err = skb_ensure_writable(skb, ETH_HLEN);
4041         if (unlikely(err))
4042                 return err;
4043
4044         eth = (void *)skb->data;
4045         ether_addr_copy(eth->h_dest, sta->sta.addr);
4046
4047         return 0;
4048 }
4049
4050 static bool ieee80211_multicast_to_unicast(struct sk_buff *skb,
4051                                            struct net_device *dev)
4052 {
4053         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4054         const struct ethhdr *eth = (void *)skb->data;
4055         const struct vlan_ethhdr *ethvlan = (void *)skb->data;
4056         __be16 ethertype;
4057
4058         if (likely(!is_multicast_ether_addr(eth->h_dest)))
4059                 return false;
4060
4061         switch (sdata->vif.type) {
4062         case NL80211_IFTYPE_AP_VLAN:
4063                 if (sdata->u.vlan.sta)
4064                         return false;
4065                 if (sdata->wdev.use_4addr)
4066                         return false;
4067                 fallthrough;
4068         case NL80211_IFTYPE_AP:
4069                 /* check runtime toggle for this bss */
4070                 if (!sdata->bss->multicast_to_unicast)
4071                         return false;
4072                 break;
4073         default:
4074                 return false;
4075         }
4076
4077         /* multicast to unicast conversion only for some payload */
4078         ethertype = eth->h_proto;
4079         if (ethertype == htons(ETH_P_8021Q) && skb->len >= VLAN_ETH_HLEN)
4080                 ethertype = ethvlan->h_vlan_encapsulated_proto;
4081         switch (ethertype) {
4082         case htons(ETH_P_ARP):
4083         case htons(ETH_P_IP):
4084         case htons(ETH_P_IPV6):
4085                 break;
4086         default:
4087                 return false;
4088         }
4089
4090         return true;
4091 }
4092
4093 static void
4094 ieee80211_convert_to_unicast(struct sk_buff *skb, struct net_device *dev,
4095                              struct sk_buff_head *queue)
4096 {
4097         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4098         struct ieee80211_local *local = sdata->local;
4099         const struct ethhdr *eth = (struct ethhdr *)skb->data;
4100         struct sta_info *sta, *first = NULL;
4101         struct sk_buff *cloned_skb;
4102
4103         rcu_read_lock();
4104
4105         list_for_each_entry_rcu(sta, &local->sta_list, list) {
4106                 if (sdata != sta->sdata)
4107                         /* AP-VLAN mismatch */
4108                         continue;
4109                 if (unlikely(ether_addr_equal(eth->h_source, sta->sta.addr)))
4110                         /* do not send back to source */
4111                         continue;
4112                 if (!first) {
4113                         first = sta;
4114                         continue;
4115                 }
4116                 cloned_skb = skb_clone(skb, GFP_ATOMIC);
4117                 if (!cloned_skb)
4118                         goto multicast;
4119                 if (unlikely(ieee80211_change_da(cloned_skb, sta))) {
4120                         dev_kfree_skb(cloned_skb);
4121                         goto multicast;
4122                 }
4123                 __skb_queue_tail(queue, cloned_skb);
4124         }
4125
4126         if (likely(first)) {
4127                 if (unlikely(ieee80211_change_da(skb, first)))
4128                         goto multicast;
4129                 __skb_queue_tail(queue, skb);
4130         } else {
4131                 /* no STA connected, drop */
4132                 kfree_skb(skb);
4133                 skb = NULL;
4134         }
4135
4136         goto out;
4137 multicast:
4138         __skb_queue_purge(queue);
4139         __skb_queue_tail(queue, skb);
4140 out:
4141         rcu_read_unlock();
4142 }
4143
4144 /**
4145  * ieee80211_subif_start_xmit - netif start_xmit function for 802.3 vifs
4146  * @skb: packet to be sent
4147  * @dev: incoming interface
4148  *
4149  * On failure skb will be freed.
4150  */
4151 netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
4152                                        struct net_device *dev)
4153 {
4154         if (unlikely(ieee80211_multicast_to_unicast(skb, dev))) {
4155                 struct sk_buff_head queue;
4156
4157                 __skb_queue_head_init(&queue);
4158                 ieee80211_convert_to_unicast(skb, dev, &queue);
4159                 while ((skb = __skb_dequeue(&queue)))
4160                         __ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
4161         } else {
4162                 __ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
4163         }
4164
4165         return NETDEV_TX_OK;
4166 }
4167
4168 static bool ieee80211_tx_8023(struct ieee80211_sub_if_data *sdata,
4169                               struct sk_buff *skb, int led_len,
4170                               struct sta_info *sta,
4171                               bool txpending)
4172 {
4173         struct ieee80211_local *local = sdata->local;
4174         struct ieee80211_tx_control control = {};
4175         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4176         struct ieee80211_sta *pubsta = NULL;
4177         unsigned long flags;
4178         int q = info->hw_queue;
4179
4180         if (ieee80211_queue_skb(local, sdata, sta, skb))
4181                 return true;
4182
4183         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4184
4185         if (local->queue_stop_reasons[q] ||
4186             (!txpending && !skb_queue_empty(&local->pending[q]))) {
4187                 if (txpending)
4188                         skb_queue_head(&local->pending[q], skb);
4189                 else
4190                         skb_queue_tail(&local->pending[q], skb);
4191
4192                 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4193
4194                 return false;
4195         }
4196
4197         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4198
4199         if (sta && sta->uploaded)
4200                 pubsta = &sta->sta;
4201
4202         control.sta = pubsta;
4203
4204         drv_tx(local, &control, skb);
4205
4206         return true;
4207 }
4208
4209 static void ieee80211_8023_xmit(struct ieee80211_sub_if_data *sdata,
4210                                 struct net_device *dev, struct sta_info *sta,
4211                                 struct ieee80211_key *key, struct sk_buff *skb)
4212 {
4213         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4214         struct ieee80211_local *local = sdata->local;
4215         struct tid_ampdu_tx *tid_tx;
4216         u8 tid;
4217
4218         if (local->ops->wake_tx_queue) {
4219                 u16 queue = __ieee80211_select_queue(sdata, sta, skb);
4220                 skb_set_queue_mapping(skb, queue);
4221                 skb_get_hash(skb);
4222         }
4223
4224         if (unlikely(test_bit(SCAN_SW_SCANNING, &local->scanning)) &&
4225             test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
4226                 goto out_free;
4227
4228         memset(info, 0, sizeof(*info));
4229
4230         tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
4231         tid_tx = rcu_dereference(sta->ampdu_mlme.tid_tx[tid]);
4232         if (tid_tx) {
4233                 if (!test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
4234                         /* fall back to non-offload slow path */
4235                         __ieee80211_subif_start_xmit(skb, dev, 0, 0, NULL);
4236                         return;
4237                 }
4238
4239                 info->flags |= IEEE80211_TX_CTL_AMPDU;
4240                 if (tid_tx->timeout)
4241                         tid_tx->last_tx = jiffies;
4242         }
4243
4244         if (unlikely(skb->sk &&
4245                      skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS))
4246                 info->ack_frame_id = ieee80211_store_ack_skb(local, skb,
4247                                                              &info->flags, NULL);
4248
4249         info->hw_queue = sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
4250
4251         ieee80211_tx_stats(dev, skb->len);
4252
4253         sta->tx_stats.bytes[skb_get_queue_mapping(skb)] += skb->len;
4254         sta->tx_stats.packets[skb_get_queue_mapping(skb)]++;
4255
4256         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
4257                 sdata = container_of(sdata->bss,
4258                                      struct ieee80211_sub_if_data, u.ap);
4259
4260         info->flags |= IEEE80211_TX_CTL_HW_80211_ENCAP;
4261         info->control.vif = &sdata->vif;
4262
4263         if (key)
4264                 info->control.hw_key = &key->conf;
4265
4266         ieee80211_tx_8023(sdata, skb, skb->len, sta, false);
4267
4268         return;
4269
4270 out_free:
4271         kfree_skb(skb);
4272 }
4273
4274 netdev_tx_t ieee80211_subif_start_xmit_8023(struct sk_buff *skb,
4275                                             struct net_device *dev)
4276 {
4277         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4278         struct ethhdr *ehdr = (struct ethhdr *)skb->data;
4279         struct ieee80211_key *key;
4280         struct sta_info *sta;
4281         bool offload = true;
4282
4283         if (unlikely(skb->len < ETH_HLEN)) {
4284                 kfree_skb(skb);
4285                 return NETDEV_TX_OK;
4286         }
4287
4288         rcu_read_lock();
4289
4290         if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4291                 kfree_skb(skb);
4292                 goto out;
4293         }
4294
4295         if (unlikely(IS_ERR_OR_NULL(sta) || !sta->uploaded ||
4296             !test_sta_flag(sta, WLAN_STA_AUTHORIZED) ||
4297                 sdata->control_port_protocol == ehdr->h_proto))
4298                 offload = false;
4299         else if ((key = rcu_dereference(sta->ptk[sta->ptk_idx])) &&
4300                  (!(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) ||
4301                   key->conf.cipher == WLAN_CIPHER_SUITE_TKIP))
4302                 offload = false;
4303
4304         if (offload)
4305                 ieee80211_8023_xmit(sdata, dev, sta, key, skb);
4306         else
4307                 ieee80211_subif_start_xmit(skb, dev);
4308
4309 out:
4310         rcu_read_unlock();
4311
4312         return NETDEV_TX_OK;
4313 }
4314
4315 struct sk_buff *
4316 ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
4317                               struct sk_buff *skb, u32 info_flags)
4318 {
4319         struct ieee80211_hdr *hdr;
4320         struct ieee80211_tx_data tx = {
4321                 .local = sdata->local,
4322                 .sdata = sdata,
4323         };
4324         struct sta_info *sta;
4325
4326         rcu_read_lock();
4327
4328         if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4329                 kfree_skb(skb);
4330                 skb = ERR_PTR(-EINVAL);
4331                 goto out;
4332         }
4333
4334         skb = ieee80211_build_hdr(sdata, skb, info_flags, sta, 0, NULL);
4335         if (IS_ERR(skb))
4336                 goto out;
4337
4338         hdr = (void *)skb->data;
4339         tx.sta = sta_info_get(sdata, hdr->addr1);
4340         tx.skb = skb;
4341
4342         if (ieee80211_tx_h_select_key(&tx) != TX_CONTINUE) {
4343                 rcu_read_unlock();
4344                 kfree_skb(skb);
4345                 return ERR_PTR(-EINVAL);
4346         }
4347
4348 out:
4349         rcu_read_unlock();
4350         return skb;
4351 }
4352
4353 /*
4354  * ieee80211_clear_tx_pending may not be called in a context where
4355  * it is possible that it packets could come in again.
4356  */
4357 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
4358 {
4359         struct sk_buff *skb;
4360         int i;
4361
4362         for (i = 0; i < local->hw.queues; i++) {
4363                 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
4364                         ieee80211_free_txskb(&local->hw, skb);
4365         }
4366 }
4367
4368 /*
4369  * Returns false if the frame couldn't be transmitted but was queued instead,
4370  * which in this case means re-queued -- take as an indication to stop sending
4371  * more pending frames.
4372  */
4373 static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
4374                                      struct sk_buff *skb)
4375 {
4376         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4377         struct ieee80211_sub_if_data *sdata;
4378         struct sta_info *sta;
4379         struct ieee80211_hdr *hdr;
4380         bool result;
4381         struct ieee80211_chanctx_conf *chanctx_conf;
4382
4383         sdata = vif_to_sdata(info->control.vif);
4384
4385         if (info->control.flags & IEEE80211_TX_INTCFL_NEED_TXPROCESSING) {
4386                 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4387                 if (unlikely(!chanctx_conf)) {
4388                         dev_kfree_skb(skb);
4389                         return true;
4390                 }
4391                 info->band = chanctx_conf->def.chan->band;
4392                 result = ieee80211_tx(sdata, NULL, skb, true);
4393         } else if (info->flags & IEEE80211_TX_CTL_HW_80211_ENCAP) {
4394                 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
4395                         dev_kfree_skb(skb);
4396                         return true;
4397                 }
4398
4399                 if (IS_ERR(sta) || (sta && !sta->uploaded))
4400                         sta = NULL;
4401
4402                 result = ieee80211_tx_8023(sdata, skb, skb->len, sta, true);
4403         } else {
4404                 struct sk_buff_head skbs;
4405
4406                 __skb_queue_head_init(&skbs);
4407                 __skb_queue_tail(&skbs, skb);
4408
4409                 hdr = (struct ieee80211_hdr *)skb->data;
4410                 sta = sta_info_get(sdata, hdr->addr1);
4411
4412                 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
4413         }
4414
4415         return result;
4416 }
4417
4418 /*
4419  * Transmit all pending packets. Called from tasklet.
4420  */
4421 void ieee80211_tx_pending(unsigned long data)
4422 {
4423         struct ieee80211_local *local = (struct ieee80211_local *)data;
4424         unsigned long flags;
4425         int i;
4426         bool txok;
4427
4428         rcu_read_lock();
4429
4430         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
4431         for (i = 0; i < local->hw.queues; i++) {
4432                 /*
4433                  * If queue is stopped by something other than due to pending
4434                  * frames, or we have no pending frames, proceed to next queue.
4435                  */
4436                 if (local->queue_stop_reasons[i] ||
4437                     skb_queue_empty(&local->pending[i]))
4438                         continue;
4439
4440                 while (!skb_queue_empty(&local->pending[i])) {
4441                         struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
4442                         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4443
4444                         if (WARN_ON(!info->control.vif)) {
4445                                 ieee80211_free_txskb(&local->hw, skb);
4446                                 continue;
4447                         }
4448
4449                         spin_unlock_irqrestore(&local->queue_stop_reason_lock,
4450                                                 flags);
4451
4452                         txok = ieee80211_tx_pending_skb(local, skb);
4453                         spin_lock_irqsave(&local->queue_stop_reason_lock,
4454                                           flags);
4455                         if (!txok)
4456                                 break;
4457                 }
4458
4459                 if (skb_queue_empty(&local->pending[i]))
4460                         ieee80211_propagate_queue_wake(local, i);
4461         }
4462         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
4463
4464         rcu_read_unlock();
4465 }
4466
4467 /* functions for drivers to get certain frames */
4468
4469 static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
4470                                        struct ps_data *ps, struct sk_buff *skb,
4471                                        bool is_template)
4472 {
4473         u8 *pos, *tim;
4474         int aid0 = 0;
4475         int i, have_bits = 0, n1, n2;
4476
4477         /* Generate bitmap for TIM only if there are any STAs in power save
4478          * mode. */
4479         if (atomic_read(&ps->num_sta_ps) > 0)
4480                 /* in the hope that this is faster than
4481                  * checking byte-for-byte */
4482                 have_bits = !bitmap_empty((unsigned long *)ps->tim,
4483                                           IEEE80211_MAX_AID+1);
4484         if (!is_template) {
4485                 if (ps->dtim_count == 0)
4486                         ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
4487                 else
4488                         ps->dtim_count--;
4489         }
4490
4491         tim = pos = skb_put(skb, 6);
4492         *pos++ = WLAN_EID_TIM;
4493         *pos++ = 4;
4494         *pos++ = ps->dtim_count;
4495         *pos++ = sdata->vif.bss_conf.dtim_period;
4496
4497         if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
4498                 aid0 = 1;
4499
4500         ps->dtim_bc_mc = aid0 == 1;
4501
4502         if (have_bits) {
4503                 /* Find largest even number N1 so that bits numbered 1 through
4504                  * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
4505                  * (N2 + 1) x 8 through 2007 are 0. */
4506                 n1 = 0;
4507                 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
4508                         if (ps->tim[i]) {
4509                                 n1 = i & 0xfe;
4510                                 break;
4511                         }
4512                 }
4513                 n2 = n1;
4514                 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
4515                         if (ps->tim[i]) {
4516                                 n2 = i;
4517                                 break;
4518                         }
4519                 }
4520
4521                 /* Bitmap control */
4522                 *pos++ = n1 | aid0;
4523                 /* Part Virt Bitmap */
4524                 skb_put(skb, n2 - n1);
4525                 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
4526
4527                 tim[1] = n2 - n1 + 4;
4528         } else {
4529                 *pos++ = aid0; /* Bitmap control */
4530                 *pos++ = 0; /* Part Virt Bitmap */
4531         }
4532 }
4533
4534 static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
4535                                     struct ps_data *ps, struct sk_buff *skb,
4536                                     bool is_template)
4537 {
4538         struct ieee80211_local *local = sdata->local;
4539
4540         /*
4541          * Not very nice, but we want to allow the driver to call
4542          * ieee80211_beacon_get() as a response to the set_tim()
4543          * callback. That, however, is already invoked under the
4544          * sta_lock to guarantee consistent and race-free update
4545          * of the tim bitmap in mac80211 and the driver.
4546          */
4547         if (local->tim_in_locked_section) {
4548                 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
4549         } else {
4550                 spin_lock_bh(&local->tim_lock);
4551                 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
4552                 spin_unlock_bh(&local->tim_lock);
4553         }
4554
4555         return 0;
4556 }
4557
4558 static void ieee80211_set_beacon_cntdwn(struct ieee80211_sub_if_data *sdata,
4559                                         struct beacon_data *beacon)
4560 {
4561         struct probe_resp *resp;
4562         u8 *beacon_data;
4563         size_t beacon_data_len;
4564         int i;
4565         u8 count = beacon->cntdwn_current_counter;
4566
4567         switch (sdata->vif.type) {
4568         case NL80211_IFTYPE_AP:
4569                 beacon_data = beacon->tail;
4570                 beacon_data_len = beacon->tail_len;
4571                 break;
4572         case NL80211_IFTYPE_ADHOC:
4573                 beacon_data = beacon->head;
4574                 beacon_data_len = beacon->head_len;
4575                 break;
4576         case NL80211_IFTYPE_MESH_POINT:
4577                 beacon_data = beacon->head;
4578                 beacon_data_len = beacon->head_len;
4579                 break;
4580         default:
4581                 return;
4582         }
4583
4584         rcu_read_lock();
4585         for (i = 0; i < IEEE80211_MAX_CNTDWN_COUNTERS_NUM; ++i) {
4586                 resp = rcu_dereference(sdata->u.ap.probe_resp);
4587
4588                 if (beacon->cntdwn_counter_offsets[i]) {
4589                         if (WARN_ON_ONCE(beacon->cntdwn_counter_offsets[i] >=
4590                                          beacon_data_len)) {
4591                                 rcu_read_unlock();
4592                                 return;
4593                         }
4594
4595                         beacon_data[beacon->cntdwn_counter_offsets[i]] = count;
4596                 }
4597
4598                 if (sdata->vif.type == NL80211_IFTYPE_AP && resp)
4599                         resp->data[resp->cntdwn_counter_offsets[i]] = count;
4600         }
4601         rcu_read_unlock();
4602 }
4603
4604 static u8 __ieee80211_beacon_update_cntdwn(struct beacon_data *beacon)
4605 {
4606         beacon->cntdwn_current_counter--;
4607
4608         /* the counter should never reach 0 */
4609         WARN_ON_ONCE(!beacon->cntdwn_current_counter);
4610
4611         return beacon->cntdwn_current_counter;
4612 }
4613
4614 u8 ieee80211_beacon_update_cntdwn(struct ieee80211_vif *vif)
4615 {
4616         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4617         struct beacon_data *beacon = NULL;
4618         u8 count = 0;
4619
4620         rcu_read_lock();
4621
4622         if (sdata->vif.type == NL80211_IFTYPE_AP)
4623                 beacon = rcu_dereference(sdata->u.ap.beacon);
4624         else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4625                 beacon = rcu_dereference(sdata->u.ibss.presp);
4626         else if (ieee80211_vif_is_mesh(&sdata->vif))
4627                 beacon = rcu_dereference(sdata->u.mesh.beacon);
4628
4629         if (!beacon)
4630                 goto unlock;
4631
4632         count = __ieee80211_beacon_update_cntdwn(beacon);
4633
4634 unlock:
4635         rcu_read_unlock();
4636         return count;
4637 }
4638 EXPORT_SYMBOL(ieee80211_beacon_update_cntdwn);
4639
4640 void ieee80211_beacon_set_cntdwn(struct ieee80211_vif *vif, u8 counter)
4641 {
4642         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4643         struct beacon_data *beacon = NULL;
4644
4645         rcu_read_lock();
4646
4647         if (sdata->vif.type == NL80211_IFTYPE_AP)
4648                 beacon = rcu_dereference(sdata->u.ap.beacon);
4649         else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
4650                 beacon = rcu_dereference(sdata->u.ibss.presp);
4651         else if (ieee80211_vif_is_mesh(&sdata->vif))
4652                 beacon = rcu_dereference(sdata->u.mesh.beacon);
4653
4654         if (!beacon)
4655                 goto unlock;
4656
4657         if (counter < beacon->cntdwn_current_counter)
4658                 beacon->cntdwn_current_counter = counter;
4659
4660 unlock:
4661         rcu_read_unlock();
4662 }
4663 EXPORT_SYMBOL(ieee80211_beacon_set_cntdwn);
4664
4665 bool ieee80211_beacon_cntdwn_is_complete(struct ieee80211_vif *vif)
4666 {
4667         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4668         struct beacon_data *beacon = NULL;
4669         u8 *beacon_data;
4670         size_t beacon_data_len;
4671         int ret = false;
4672
4673         if (!ieee80211_sdata_running(sdata))
4674                 return false;
4675
4676         rcu_read_lock();
4677         if (vif->type == NL80211_IFTYPE_AP) {
4678                 struct ieee80211_if_ap *ap = &sdata->u.ap;
4679
4680                 beacon = rcu_dereference(ap->beacon);
4681                 if (WARN_ON(!beacon || !beacon->tail))
4682                         goto out;
4683                 beacon_data = beacon->tail;
4684                 beacon_data_len = beacon->tail_len;
4685         } else if (vif->type == NL80211_IFTYPE_ADHOC) {
4686                 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4687
4688                 beacon = rcu_dereference(ifibss->presp);
4689                 if (!beacon)
4690                         goto out;
4691
4692                 beacon_data = beacon->head;
4693                 beacon_data_len = beacon->head_len;
4694         } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
4695                 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4696
4697                 beacon = rcu_dereference(ifmsh->beacon);
4698                 if (!beacon)
4699                         goto out;
4700
4701                 beacon_data = beacon->head;
4702                 beacon_data_len = beacon->head_len;
4703         } else {
4704                 WARN_ON(1);
4705                 goto out;
4706         }
4707
4708         if (!beacon->cntdwn_counter_offsets[0])
4709                 goto out;
4710
4711         if (WARN_ON_ONCE(beacon->cntdwn_counter_offsets[0] > beacon_data_len))
4712                 goto out;
4713
4714         if (beacon_data[beacon->cntdwn_counter_offsets[0]] == 1)
4715                 ret = true;
4716
4717  out:
4718         rcu_read_unlock();
4719
4720         return ret;
4721 }
4722 EXPORT_SYMBOL(ieee80211_beacon_cntdwn_is_complete);
4723
4724 static int ieee80211_beacon_protect(struct sk_buff *skb,
4725                                     struct ieee80211_local *local,
4726                                     struct ieee80211_sub_if_data *sdata)
4727 {
4728         ieee80211_tx_result res;
4729         struct ieee80211_tx_data tx;
4730         struct sk_buff *check_skb;
4731
4732         memset(&tx, 0, sizeof(tx));
4733         tx.key = rcu_dereference(sdata->default_beacon_key);
4734         if (!tx.key)
4735                 return 0;
4736         tx.local = local;
4737         tx.sdata = sdata;
4738         __skb_queue_head_init(&tx.skbs);
4739         __skb_queue_tail(&tx.skbs, skb);
4740         res = ieee80211_tx_h_encrypt(&tx);
4741         check_skb = __skb_dequeue(&tx.skbs);
4742         /* we may crash after this, but it'd be a bug in crypto */
4743         WARN_ON(check_skb != skb);
4744         if (WARN_ON_ONCE(res != TX_CONTINUE))
4745                 return -EINVAL;
4746
4747         return 0;
4748 }
4749
4750 static struct sk_buff *
4751 __ieee80211_beacon_get(struct ieee80211_hw *hw,
4752                        struct ieee80211_vif *vif,
4753                        struct ieee80211_mutable_offsets *offs,
4754                        bool is_template)
4755 {
4756         struct ieee80211_local *local = hw_to_local(hw);
4757         struct beacon_data *beacon = NULL;
4758         struct sk_buff *skb = NULL;
4759         struct ieee80211_tx_info *info;
4760         struct ieee80211_sub_if_data *sdata = NULL;
4761         enum nl80211_band band;
4762         struct ieee80211_tx_rate_control txrc;
4763         struct ieee80211_chanctx_conf *chanctx_conf;
4764         int csa_off_base = 0;
4765
4766         rcu_read_lock();
4767
4768         sdata = vif_to_sdata(vif);
4769         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
4770
4771         if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
4772                 goto out;
4773
4774         if (offs)
4775                 memset(offs, 0, sizeof(*offs));
4776
4777         if (sdata->vif.type == NL80211_IFTYPE_AP) {
4778                 struct ieee80211_if_ap *ap = &sdata->u.ap;
4779
4780                 beacon = rcu_dereference(ap->beacon);
4781                 if (beacon) {
4782                         if (beacon->cntdwn_counter_offsets[0]) {
4783                                 if (!is_template)
4784                                         ieee80211_beacon_update_cntdwn(vif);
4785
4786                                 ieee80211_set_beacon_cntdwn(sdata, beacon);
4787                         }
4788
4789                         /*
4790                          * headroom, head length,
4791                          * tail length and maximum TIM length
4792                          */
4793                         skb = dev_alloc_skb(local->tx_headroom +
4794                                             beacon->head_len +
4795                                             beacon->tail_len + 256 +
4796                                             local->hw.extra_beacon_tailroom);
4797                         if (!skb)
4798                                 goto out;
4799
4800                         skb_reserve(skb, local->tx_headroom);
4801                         skb_put_data(skb, beacon->head, beacon->head_len);
4802
4803                         ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
4804                                                  is_template);
4805
4806                         if (offs) {
4807                                 offs->tim_offset = beacon->head_len;
4808                                 offs->tim_length = skb->len - beacon->head_len;
4809
4810                                 /* for AP the csa offsets are from tail */
4811                                 csa_off_base = skb->len;
4812                         }
4813
4814                         if (beacon->tail)
4815                                 skb_put_data(skb, beacon->tail,
4816                                              beacon->tail_len);
4817
4818                         if (ieee80211_beacon_protect(skb, local, sdata) < 0)
4819                                 goto out;
4820                 } else
4821                         goto out;
4822         } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
4823                 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
4824                 struct ieee80211_hdr *hdr;
4825
4826                 beacon = rcu_dereference(ifibss->presp);
4827                 if (!beacon)
4828                         goto out;
4829
4830                 if (beacon->cntdwn_counter_offsets[0]) {
4831                         if (!is_template)
4832                                 __ieee80211_beacon_update_cntdwn(beacon);
4833
4834                         ieee80211_set_beacon_cntdwn(sdata, beacon);
4835                 }
4836
4837                 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
4838                                     local->hw.extra_beacon_tailroom);
4839                 if (!skb)
4840                         goto out;
4841                 skb_reserve(skb, local->tx_headroom);
4842                 skb_put_data(skb, beacon->head, beacon->head_len);
4843
4844                 hdr = (struct ieee80211_hdr *) skb->data;
4845                 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4846                                                  IEEE80211_STYPE_BEACON);
4847         } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
4848                 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
4849
4850                 beacon = rcu_dereference(ifmsh->beacon);
4851                 if (!beacon)
4852                         goto out;
4853
4854                 if (beacon->cntdwn_counter_offsets[0]) {
4855                         if (!is_template)
4856                                 /* TODO: For mesh csa_counter is in TU, so
4857                                  * decrementing it by one isn't correct, but
4858                                  * for now we leave it consistent with overall
4859                                  * mac80211's behavior.
4860                                  */
4861                                 __ieee80211_beacon_update_cntdwn(beacon);
4862
4863                         ieee80211_set_beacon_cntdwn(sdata, beacon);
4864                 }
4865
4866                 if (ifmsh->sync_ops)
4867                         ifmsh->sync_ops->adjust_tsf(sdata, beacon);
4868
4869                 skb = dev_alloc_skb(local->tx_headroom +
4870                                     beacon->head_len +
4871                                     256 + /* TIM IE */
4872                                     beacon->tail_len +
4873                                     local->hw.extra_beacon_tailroom);
4874                 if (!skb)
4875                         goto out;
4876                 skb_reserve(skb, local->tx_headroom);
4877                 skb_put_data(skb, beacon->head, beacon->head_len);
4878                 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
4879
4880                 if (offs) {
4881                         offs->tim_offset = beacon->head_len;
4882                         offs->tim_length = skb->len - beacon->head_len;
4883                 }
4884
4885                 skb_put_data(skb, beacon->tail, beacon->tail_len);
4886         } else {
4887                 WARN_ON(1);
4888                 goto out;
4889         }
4890
4891         /* CSA offsets */
4892         if (offs && beacon) {
4893                 int i;
4894
4895                 for (i = 0; i < IEEE80211_MAX_CNTDWN_COUNTERS_NUM; i++) {
4896                         u16 csa_off = beacon->cntdwn_counter_offsets[i];
4897
4898                         if (!csa_off)
4899                                 continue;
4900
4901                         offs->cntdwn_counter_offs[i] = csa_off_base + csa_off;
4902                 }
4903         }
4904
4905         band = chanctx_conf->def.chan->band;
4906
4907         info = IEEE80211_SKB_CB(skb);
4908
4909         info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
4910         info->flags |= IEEE80211_TX_CTL_NO_ACK;
4911         info->band = band;
4912
4913         memset(&txrc, 0, sizeof(txrc));
4914         txrc.hw = hw;
4915         txrc.sband = local->hw.wiphy->bands[band];
4916         txrc.bss_conf = &sdata->vif.bss_conf;
4917         txrc.skb = skb;
4918         txrc.reported_rate.idx = -1;
4919         if (sdata->beacon_rate_set && sdata->beacon_rateidx_mask[band])
4920                 txrc.rate_idx_mask = sdata->beacon_rateidx_mask[band];
4921         else
4922                 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
4923         txrc.bss = true;
4924         rate_control_get_rate(sdata, NULL, &txrc);
4925
4926         info->control.vif = vif;
4927
4928         info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
4929                         IEEE80211_TX_CTL_ASSIGN_SEQ |
4930                         IEEE80211_TX_CTL_FIRST_FRAGMENT;
4931  out:
4932         rcu_read_unlock();
4933         return skb;
4934
4935 }
4936
4937 struct sk_buff *
4938 ieee80211_beacon_get_template(struct ieee80211_hw *hw,
4939                               struct ieee80211_vif *vif,
4940                               struct ieee80211_mutable_offsets *offs)
4941 {
4942         return __ieee80211_beacon_get(hw, vif, offs, true);
4943 }
4944 EXPORT_SYMBOL(ieee80211_beacon_get_template);
4945
4946 struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
4947                                          struct ieee80211_vif *vif,
4948                                          u16 *tim_offset, u16 *tim_length)
4949 {
4950         struct ieee80211_mutable_offsets offs = {};
4951         struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
4952         struct sk_buff *copy;
4953         struct ieee80211_supported_band *sband;
4954         int shift;
4955
4956         if (!bcn)
4957                 return bcn;
4958
4959         if (tim_offset)
4960                 *tim_offset = offs.tim_offset;
4961
4962         if (tim_length)
4963                 *tim_length = offs.tim_length;
4964
4965         if (ieee80211_hw_check(hw, BEACON_TX_STATUS) ||
4966             !hw_to_local(hw)->monitors)
4967                 return bcn;
4968
4969         /* send a copy to monitor interfaces */
4970         copy = skb_copy(bcn, GFP_ATOMIC);
4971         if (!copy)
4972                 return bcn;
4973
4974         shift = ieee80211_vif_get_shift(vif);
4975         sband = ieee80211_get_sband(vif_to_sdata(vif));
4976         if (!sband)
4977                 return bcn;
4978
4979         ieee80211_tx_monitor(hw_to_local(hw), copy, sband, 1, shift, false,
4980                              NULL);
4981
4982         return bcn;
4983 }
4984 EXPORT_SYMBOL(ieee80211_beacon_get_tim);
4985
4986 struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
4987                                         struct ieee80211_vif *vif)
4988 {
4989         struct ieee80211_if_ap *ap = NULL;
4990         struct sk_buff *skb = NULL;
4991         struct probe_resp *presp = NULL;
4992         struct ieee80211_hdr *hdr;
4993         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
4994
4995         if (sdata->vif.type != NL80211_IFTYPE_AP)
4996                 return NULL;
4997
4998         rcu_read_lock();
4999
5000         ap = &sdata->u.ap;
5001         presp = rcu_dereference(ap->probe_resp);
5002         if (!presp)
5003                 goto out;
5004
5005         skb = dev_alloc_skb(presp->len);
5006         if (!skb)
5007                 goto out;
5008
5009         skb_put_data(skb, presp->data, presp->len);
5010
5011         hdr = (struct ieee80211_hdr *) skb->data;
5012         memset(hdr->addr1, 0, sizeof(hdr->addr1));
5013
5014 out:
5015         rcu_read_unlock();
5016         return skb;
5017 }
5018 EXPORT_SYMBOL(ieee80211_proberesp_get);
5019
5020 struct sk_buff *ieee80211_get_fils_discovery_tmpl(struct ieee80211_hw *hw,
5021                                                   struct ieee80211_vif *vif)
5022 {
5023         struct sk_buff *skb = NULL;
5024         struct fils_discovery_data *tmpl = NULL;
5025         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5026
5027         if (sdata->vif.type != NL80211_IFTYPE_AP)
5028                 return NULL;
5029
5030         rcu_read_lock();
5031         tmpl = rcu_dereference(sdata->u.ap.fils_discovery);
5032         if (!tmpl) {
5033                 rcu_read_unlock();
5034                 return NULL;
5035         }
5036
5037         skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
5038         if (skb) {
5039                 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
5040                 skb_put_data(skb, tmpl->data, tmpl->len);
5041         }
5042
5043         rcu_read_unlock();
5044         return skb;
5045 }
5046 EXPORT_SYMBOL(ieee80211_get_fils_discovery_tmpl);
5047
5048 struct sk_buff *
5049 ieee80211_get_unsol_bcast_probe_resp_tmpl(struct ieee80211_hw *hw,
5050                                           struct ieee80211_vif *vif)
5051 {
5052         struct sk_buff *skb = NULL;
5053         struct unsol_bcast_probe_resp_data *tmpl = NULL;
5054         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
5055
5056         if (sdata->vif.type != NL80211_IFTYPE_AP)
5057                 return NULL;
5058
5059         rcu_read_lock();
5060         tmpl = rcu_dereference(sdata->u.ap.unsol_bcast_probe_resp);
5061         if (!tmpl) {
5062                 rcu_read_unlock();
5063                 return NULL;
5064         }
5065
5066         skb = dev_alloc_skb(sdata->local->hw.extra_tx_headroom + tmpl->len);
5067         if (skb) {
5068                 skb_reserve(skb, sdata->local->hw.extra_tx_headroom);
5069                 skb_put_data(skb, tmpl->data, tmpl->len);
5070         }
5071
5072         rcu_read_unlock();
5073         return skb;
5074 }
5075 EXPORT_SYMBOL(ieee80211_get_unsol_bcast_probe_resp_tmpl);
5076
5077 struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
5078                                      struct ieee80211_vif *vif)
5079 {
5080         struct ieee80211_sub_if_data *sdata;
5081         struct ieee80211_if_managed *ifmgd;
5082         struct ieee80211_pspoll *pspoll;
5083         struct ieee80211_local *local;
5084         struct sk_buff *skb;
5085
5086         if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5087                 return NULL;
5088
5089         sdata = vif_to_sdata(vif);
5090         ifmgd = &sdata->u.mgd;
5091         local = sdata->local;
5092
5093         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
5094         if (!skb)
5095                 return NULL;
5096
5097         skb_reserve(skb, local->hw.extra_tx_headroom);
5098
5099         pspoll = skb_put_zero(skb, sizeof(*pspoll));
5100         pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
5101                                             IEEE80211_STYPE_PSPOLL);
5102         pspoll->aid = cpu_to_le16(sdata->vif.bss_conf.aid);
5103
5104         /* aid in PS-Poll has its two MSBs each set to 1 */
5105         pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
5106
5107         memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
5108         memcpy(pspoll->ta, vif->addr, ETH_ALEN);
5109
5110         return skb;
5111 }
5112 EXPORT_SYMBOL(ieee80211_pspoll_get);
5113
5114 struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
5115                                        struct ieee80211_vif *vif,
5116                                        bool qos_ok)
5117 {
5118         struct ieee80211_hdr_3addr *nullfunc;
5119         struct ieee80211_sub_if_data *sdata;
5120         struct ieee80211_if_managed *ifmgd;
5121         struct ieee80211_local *local;
5122         struct sk_buff *skb;
5123         bool qos = false;
5124
5125         if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
5126                 return NULL;
5127
5128         sdata = vif_to_sdata(vif);
5129         ifmgd = &sdata->u.mgd;
5130         local = sdata->local;
5131
5132         if (qos_ok) {
5133                 struct sta_info *sta;
5134
5135                 rcu_read_lock();
5136                 sta = sta_info_get(sdata, ifmgd->bssid);
5137                 qos = sta && sta->sta.wme;
5138                 rcu_read_unlock();
5139         }
5140
5141         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5142                             sizeof(*nullfunc) + 2);
5143         if (!skb)
5144                 return NULL;
5145
5146         skb_reserve(skb, local->hw.extra_tx_headroom);
5147
5148         nullfunc = skb_put_zero(skb, sizeof(*nullfunc));
5149         nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
5150                                               IEEE80211_STYPE_NULLFUNC |
5151                                               IEEE80211_FCTL_TODS);
5152         if (qos) {
5153                 __le16 qoshdr = cpu_to_le16(7);
5154
5155                 BUILD_BUG_ON((IEEE80211_STYPE_QOS_NULLFUNC |
5156                               IEEE80211_STYPE_NULLFUNC) !=
5157                              IEEE80211_STYPE_QOS_NULLFUNC);
5158                 nullfunc->frame_control |=
5159                         cpu_to_le16(IEEE80211_STYPE_QOS_NULLFUNC);
5160                 skb->priority = 7;
5161                 skb_set_queue_mapping(skb, IEEE80211_AC_VO);
5162                 skb_put_data(skb, &qoshdr, sizeof(qoshdr));
5163         }
5164
5165         memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
5166         memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
5167         memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
5168
5169         return skb;
5170 }
5171 EXPORT_SYMBOL(ieee80211_nullfunc_get);
5172
5173 struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
5174                                        const u8 *src_addr,
5175                                        const u8 *ssid, size_t ssid_len,
5176                                        size_t tailroom)
5177 {
5178         struct ieee80211_local *local = hw_to_local(hw);
5179         struct ieee80211_hdr_3addr *hdr;
5180         struct sk_buff *skb;
5181         size_t ie_ssid_len;
5182         u8 *pos;
5183
5184         ie_ssid_len = 2 + ssid_len;
5185
5186         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
5187                             ie_ssid_len + tailroom);
5188         if (!skb)
5189                 return NULL;
5190
5191         skb_reserve(skb, local->hw.extra_tx_headroom);
5192
5193         hdr = skb_put_zero(skb, sizeof(*hdr));
5194         hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
5195                                          IEEE80211_STYPE_PROBE_REQ);
5196         eth_broadcast_addr(hdr->addr1);
5197         memcpy(hdr->addr2, src_addr, ETH_ALEN);
5198         eth_broadcast_addr(hdr->addr3);
5199
5200         pos = skb_put(skb, ie_ssid_len);
5201         *pos++ = WLAN_EID_SSID;
5202         *pos++ = ssid_len;
5203         if (ssid_len)
5204                 memcpy(pos, ssid, ssid_len);
5205         pos += ssid_len;
5206
5207         return skb;
5208 }
5209 EXPORT_SYMBOL(ieee80211_probereq_get);
5210
5211 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5212                        const void *frame, size_t frame_len,
5213                        const struct ieee80211_tx_info *frame_txctl,
5214                        struct ieee80211_rts *rts)
5215 {
5216         const struct ieee80211_hdr *hdr = frame;
5217
5218         rts->frame_control =
5219             cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
5220         rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
5221                                                frame_txctl);
5222         memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
5223         memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
5224 }
5225 EXPORT_SYMBOL(ieee80211_rts_get);
5226
5227 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5228                              const void *frame, size_t frame_len,
5229                              const struct ieee80211_tx_info *frame_txctl,
5230                              struct ieee80211_cts *cts)
5231 {
5232         const struct ieee80211_hdr *hdr = frame;
5233
5234         cts->frame_control =
5235             cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
5236         cts->duration = ieee80211_ctstoself_duration(hw, vif,
5237                                                      frame_len, frame_txctl);
5238         memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
5239 }
5240 EXPORT_SYMBOL(ieee80211_ctstoself_get);
5241
5242 struct sk_buff *
5243 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
5244                           struct ieee80211_vif *vif)
5245 {
5246         struct ieee80211_local *local = hw_to_local(hw);
5247         struct sk_buff *skb = NULL;
5248         struct ieee80211_tx_data tx;
5249         struct ieee80211_sub_if_data *sdata;
5250         struct ps_data *ps;
5251         struct ieee80211_tx_info *info;
5252         struct ieee80211_chanctx_conf *chanctx_conf;
5253
5254         sdata = vif_to_sdata(vif);
5255
5256         rcu_read_lock();
5257         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
5258
5259         if (!chanctx_conf)
5260                 goto out;
5261
5262         if (sdata->vif.type == NL80211_IFTYPE_AP) {
5263                 struct beacon_data *beacon =
5264                                 rcu_dereference(sdata->u.ap.beacon);
5265
5266                 if (!beacon || !beacon->head)
5267                         goto out;
5268
5269                 ps = &sdata->u.ap.ps;
5270         } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
5271                 ps = &sdata->u.mesh.ps;
5272         } else {
5273                 goto out;
5274         }
5275
5276         if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
5277                 goto out; /* send buffered bc/mc only after DTIM beacon */
5278
5279         while (1) {
5280                 skb = skb_dequeue(&ps->bc_buf);
5281                 if (!skb)
5282                         goto out;
5283                 local->total_ps_buffered--;
5284
5285                 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
5286                         struct ieee80211_hdr *hdr =
5287                                 (struct ieee80211_hdr *) skb->data;
5288                         /* more buffered multicast/broadcast frames ==> set
5289                          * MoreData flag in IEEE 802.11 header to inform PS
5290                          * STAs */
5291                         hdr->frame_control |=
5292                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
5293                 }
5294
5295                 if (sdata->vif.type == NL80211_IFTYPE_AP)
5296                         sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
5297                 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
5298                         break;
5299                 ieee80211_free_txskb(hw, skb);
5300         }
5301
5302         info = IEEE80211_SKB_CB(skb);
5303
5304         tx.flags |= IEEE80211_TX_PS_BUFFERED;
5305         info->band = chanctx_conf->def.chan->band;
5306
5307         if (invoke_tx_handlers(&tx))
5308                 skb = NULL;
5309  out:
5310         rcu_read_unlock();
5311
5312         return skb;
5313 }
5314 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
5315
5316 int ieee80211_reserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5317 {
5318         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5319         struct ieee80211_sub_if_data *sdata = sta->sdata;
5320         struct ieee80211_local *local = sdata->local;
5321         int ret;
5322         u32 queues;
5323
5324         lockdep_assert_held(&local->sta_mtx);
5325
5326         /* only some cases are supported right now */
5327         switch (sdata->vif.type) {
5328         case NL80211_IFTYPE_STATION:
5329         case NL80211_IFTYPE_AP:
5330         case NL80211_IFTYPE_AP_VLAN:
5331                 break;
5332         default:
5333                 WARN_ON(1);
5334                 return -EINVAL;
5335         }
5336
5337         if (WARN_ON(tid >= IEEE80211_NUM_UPS))
5338                 return -EINVAL;
5339
5340         if (sta->reserved_tid == tid) {
5341                 ret = 0;
5342                 goto out;
5343         }
5344
5345         if (sta->reserved_tid != IEEE80211_TID_UNRESERVED) {
5346                 sdata_err(sdata, "TID reservation already active\n");
5347                 ret = -EALREADY;
5348                 goto out;
5349         }
5350
5351         ieee80211_stop_vif_queues(sdata->local, sdata,
5352                                   IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5353
5354         synchronize_net();
5355
5356         /* Tear down BA sessions so we stop aggregating on this TID */
5357         if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION)) {
5358                 set_sta_flag(sta, WLAN_STA_BLOCK_BA);
5359                 __ieee80211_stop_tx_ba_session(sta, tid,
5360                                                AGG_STOP_LOCAL_REQUEST);
5361         }
5362
5363         queues = BIT(sdata->vif.hw_queue[ieee802_1d_to_ac[tid]]);
5364         __ieee80211_flush_queues(local, sdata, queues, false);
5365
5366         sta->reserved_tid = tid;
5367
5368         ieee80211_wake_vif_queues(local, sdata,
5369                                   IEEE80211_QUEUE_STOP_REASON_RESERVE_TID);
5370
5371         if (ieee80211_hw_check(&local->hw, AMPDU_AGGREGATION))
5372                 clear_sta_flag(sta, WLAN_STA_BLOCK_BA);
5373
5374         ret = 0;
5375  out:
5376         return ret;
5377 }
5378 EXPORT_SYMBOL(ieee80211_reserve_tid);
5379
5380 void ieee80211_unreserve_tid(struct ieee80211_sta *pubsta, u8 tid)
5381 {
5382         struct sta_info *sta = container_of(pubsta, struct sta_info, sta);
5383         struct ieee80211_sub_if_data *sdata = sta->sdata;
5384
5385         lockdep_assert_held(&sdata->local->sta_mtx);
5386
5387         /* only some cases are supported right now */
5388         switch (sdata->vif.type) {
5389         case NL80211_IFTYPE_STATION:
5390         case NL80211_IFTYPE_AP:
5391         case NL80211_IFTYPE_AP_VLAN:
5392                 break;
5393         default:
5394                 WARN_ON(1);
5395                 return;
5396         }
5397
5398         if (tid != sta->reserved_tid) {
5399                 sdata_err(sdata, "TID to unreserve (%d) isn't reserved\n", tid);
5400                 return;
5401         }
5402
5403         sta->reserved_tid = IEEE80211_TID_UNRESERVED;
5404 }
5405 EXPORT_SYMBOL(ieee80211_unreserve_tid);
5406
5407 void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
5408                                  struct sk_buff *skb, int tid,
5409                                  enum nl80211_band band)
5410 {
5411         int ac = ieee80211_ac_from_tid(tid);
5412
5413         skb_reset_mac_header(skb);
5414         skb_set_queue_mapping(skb, ac);
5415         skb->priority = tid;
5416
5417         skb->dev = sdata->dev;
5418
5419         /*
5420          * The other path calling ieee80211_xmit is from the tasklet,
5421          * and while we can handle concurrent transmissions locking
5422          * requirements are that we do not come into tx with bhs on.
5423          */
5424         local_bh_disable();
5425         IEEE80211_SKB_CB(skb)->band = band;
5426         ieee80211_xmit(sdata, NULL, skb);
5427         local_bh_enable();
5428 }
5429
5430 int ieee80211_tx_control_port(struct wiphy *wiphy, struct net_device *dev,
5431                               const u8 *buf, size_t len,
5432                               const u8 *dest, __be16 proto, bool unencrypted,
5433                               u64 *cookie)
5434 {
5435         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5436         struct ieee80211_local *local = sdata->local;
5437         struct sk_buff *skb;
5438         struct ethhdr *ehdr;
5439         u32 ctrl_flags = 0;
5440         u32 flags = 0;
5441
5442         /* Only accept CONTROL_PORT_PROTOCOL configured in CONNECT/ASSOCIATE
5443          * or Pre-Authentication
5444          */
5445         if (proto != sdata->control_port_protocol &&
5446             proto != cpu_to_be16(ETH_P_PREAUTH))
5447                 return -EINVAL;
5448
5449         if (proto == sdata->control_port_protocol)
5450                 ctrl_flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO |
5451                               IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP;
5452
5453         if (unencrypted)
5454                 flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
5455
5456         if (cookie)
5457                 ctrl_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
5458
5459         flags |= IEEE80211_TX_INTFL_NL80211_FRAME_TX |
5460                  IEEE80211_TX_CTL_INJECTED;
5461
5462         skb = dev_alloc_skb(local->hw.extra_tx_headroom +
5463                             sizeof(struct ethhdr) + len);
5464         if (!skb)
5465                 return -ENOMEM;
5466
5467         skb_reserve(skb, local->hw.extra_tx_headroom + sizeof(struct ethhdr));
5468
5469         skb_put_data(skb, buf, len);
5470
5471         ehdr = skb_push(skb, sizeof(struct ethhdr));
5472         memcpy(ehdr->h_dest, dest, ETH_ALEN);
5473         memcpy(ehdr->h_source, sdata->vif.addr, ETH_ALEN);
5474         ehdr->h_proto = proto;
5475
5476         skb->dev = dev;
5477         skb->protocol = htons(ETH_P_802_3);
5478         skb_reset_network_header(skb);
5479         skb_reset_mac_header(skb);
5480
5481         /* mutex lock is only needed for incrementing the cookie counter */
5482         mutex_lock(&local->mtx);
5483
5484         local_bh_disable();
5485         __ieee80211_subif_start_xmit(skb, skb->dev, flags, ctrl_flags, cookie);
5486         local_bh_enable();
5487
5488         mutex_unlock(&local->mtx);
5489
5490         return 0;
5491 }
5492
5493 int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
5494                               const u8 *buf, size_t len)
5495 {
5496         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
5497         struct ieee80211_local *local = sdata->local;
5498         struct sk_buff *skb;
5499
5500         skb = dev_alloc_skb(local->hw.extra_tx_headroom + len +
5501                             30 + /* header size */
5502                             18); /* 11s header size */
5503         if (!skb)
5504                 return -ENOMEM;
5505
5506         skb_reserve(skb, local->hw.extra_tx_headroom);
5507         skb_put_data(skb, buf, len);
5508
5509         skb->dev = dev;
5510         skb->protocol = htons(ETH_P_802_3);
5511         skb_reset_network_header(skb);
5512         skb_reset_mac_header(skb);
5513
5514         local_bh_disable();
5515         __ieee80211_subif_start_xmit(skb, skb->dev, 0,
5516                                      IEEE80211_TX_CTRL_SKIP_MPATH_LOOKUP,
5517                                      NULL);
5518         local_bh_enable();
5519
5520         return 0;
5521 }