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