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