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