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