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