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