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