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