Linux-libre 3.16.41-gnu
[librecmc/linux-libre.git] / net / mac80211 / tx.c
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2005-2006, Devicescape Software, Inc.
4  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
5  * Copyright 2007       Johannes Berg <johannes@sipsolutions.net>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  *
12  * Transmit and frame generation functions.
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/skbuff.h>
18 #include <linux/etherdevice.h>
19 #include <linux/bitmap.h>
20 #include <linux/rcupdate.h>
21 #include <linux/export.h>
22 #include <linux/time.h>
23 #include <net/net_namespace.h>
24 #include <net/ieee80211_radiotap.h>
25 #include <net/cfg80211.h>
26 #include <net/mac80211.h>
27 #include <asm/unaligned.h>
28
29 #include "ieee80211_i.h"
30 #include "driver-ops.h"
31 #include "led.h"
32 #include "mesh.h"
33 #include "wep.h"
34 #include "wpa.h"
35 #include "wme.h"
36 #include "rate.h"
37
38 /* misc utils */
39
40 static __le16 ieee80211_duration(struct ieee80211_tx_data *tx,
41                                  struct sk_buff *skb, int group_addr,
42                                  int next_frag_len)
43 {
44         int rate, mrate, erp, dur, i, shift = 0;
45         struct ieee80211_rate *txrate;
46         struct ieee80211_local *local = tx->local;
47         struct ieee80211_supported_band *sband;
48         struct ieee80211_hdr *hdr;
49         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
50         struct ieee80211_chanctx_conf *chanctx_conf;
51         u32 rate_flags = 0;
52
53         rcu_read_lock();
54         chanctx_conf = rcu_dereference(tx->sdata->vif.chanctx_conf);
55         if (chanctx_conf) {
56                 shift = ieee80211_chandef_get_shift(&chanctx_conf->def);
57                 rate_flags = ieee80211_chandef_rate_flags(&chanctx_conf->def);
58         }
59         rcu_read_unlock();
60
61         /* assume HW handles this */
62         if (tx->rate.flags & IEEE80211_TX_RC_MCS)
63                 return 0;
64
65         /* uh huh? */
66         if (WARN_ON_ONCE(tx->rate.idx < 0))
67                 return 0;
68
69         sband = local->hw.wiphy->bands[info->band];
70         txrate = &sband->bitrates[tx->rate.idx];
71
72         erp = txrate->flags & IEEE80211_RATE_ERP_G;
73
74         /*
75          * data and mgmt (except PS Poll):
76          * - during CFP: 32768
77          * - during contention period:
78          *   if addr1 is group address: 0
79          *   if more fragments = 0 and addr1 is individual address: time to
80          *      transmit one ACK plus SIFS
81          *   if more fragments = 1 and addr1 is individual address: time to
82          *      transmit next fragment plus 2 x ACK plus 3 x SIFS
83          *
84          * IEEE 802.11, 9.6:
85          * - control response frame (CTS or ACK) shall be transmitted using the
86          *   same rate as the immediately previous frame in the frame exchange
87          *   sequence, if this rate belongs to the PHY mandatory rates, or else
88          *   at the highest possible rate belonging to the PHY rates in the
89          *   BSSBasicRateSet
90          */
91         hdr = (struct ieee80211_hdr *)skb->data;
92         if (ieee80211_is_ctl(hdr->frame_control)) {
93                 /* TODO: These control frames are not currently sent by
94                  * mac80211, but should they be implemented, this function
95                  * needs to be updated to support duration field calculation.
96                  *
97                  * RTS: time needed to transmit pending data/mgmt frame plus
98                  *    one CTS frame plus one ACK frame plus 3 x SIFS
99                  * CTS: duration of immediately previous RTS minus time
100                  *    required to transmit CTS and its SIFS
101                  * ACK: 0 if immediately previous directed data/mgmt had
102                  *    more=0, with more=1 duration in ACK frame is duration
103                  *    from previous frame minus time needed to transmit ACK
104                  *    and its SIFS
105                  * PS Poll: BIT(15) | BIT(14) | aid
106                  */
107                 return 0;
108         }
109
110         /* data/mgmt */
111         if (0 /* FIX: data/mgmt during CFP */)
112                 return cpu_to_le16(32768);
113
114         if (group_addr) /* Group address as the destination - no ACK */
115                 return 0;
116
117         /* Individual destination address:
118          * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
119          * CTS and ACK frames shall be transmitted using the highest rate in
120          * basic rate set that is less than or equal to the rate of the
121          * immediately previous frame and that is using the same modulation
122          * (CCK or OFDM). If no basic rate set matches with these requirements,
123          * the highest mandatory rate of the PHY that is less than or equal to
124          * the rate of the previous frame is used.
125          * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
126          */
127         rate = -1;
128         /* use lowest available if everything fails */
129         mrate = sband->bitrates[0].bitrate;
130         for (i = 0; i < sband->n_bitrates; i++) {
131                 struct ieee80211_rate *r = &sband->bitrates[i];
132
133                 if (r->bitrate > txrate->bitrate)
134                         break;
135
136                 if ((rate_flags & r->flags) != rate_flags)
137                         continue;
138
139                 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
140                         rate = DIV_ROUND_UP(r->bitrate, 1 << shift);
141
142                 switch (sband->band) {
143                 case IEEE80211_BAND_2GHZ: {
144                         u32 flag;
145                         if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
146                                 flag = IEEE80211_RATE_MANDATORY_G;
147                         else
148                                 flag = IEEE80211_RATE_MANDATORY_B;
149                         if (r->flags & flag)
150                                 mrate = r->bitrate;
151                         break;
152                 }
153                 case IEEE80211_BAND_5GHZ:
154                         if (r->flags & IEEE80211_RATE_MANDATORY_A)
155                                 mrate = r->bitrate;
156                         break;
157                 case IEEE80211_BAND_60GHZ:
158                         /* TODO, for now fall through */
159                 case IEEE80211_NUM_BANDS:
160                         WARN_ON(1);
161                         break;
162                 }
163         }
164         if (rate == -1) {
165                 /* No matching basic rate found; use highest suitable mandatory
166                  * PHY rate */
167                 rate = DIV_ROUND_UP(mrate, 1 << shift);
168         }
169
170         /* Don't calculate ACKs for QoS Frames with NoAck Policy set */
171         if (ieee80211_is_data_qos(hdr->frame_control) &&
172             *(ieee80211_get_qos_ctl(hdr)) & IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
173                 dur = 0;
174         else
175                 /* Time needed to transmit ACK
176                  * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
177                  * to closest integer */
178                 dur = ieee80211_frame_duration(sband->band, 10, rate, erp,
179                                 tx->sdata->vif.bss_conf.use_short_preamble,
180                                 shift);
181
182         if (next_frag_len) {
183                 /* Frame is fragmented: duration increases with time needed to
184                  * transmit next fragment plus ACK and 2 x SIFS. */
185                 dur *= 2; /* ACK + SIFS */
186                 /* next fragment */
187                 dur += ieee80211_frame_duration(sband->band, next_frag_len,
188                                 txrate->bitrate, erp,
189                                 tx->sdata->vif.bss_conf.use_short_preamble,
190                                 shift);
191         }
192
193         return cpu_to_le16(dur);
194 }
195
196 /* tx handlers */
197 static ieee80211_tx_result debug_noinline
198 ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
199 {
200         struct ieee80211_local *local = tx->local;
201         struct ieee80211_if_managed *ifmgd;
202
203         /* driver doesn't support power save */
204         if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
205                 return TX_CONTINUE;
206
207         /* hardware does dynamic power save */
208         if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
209                 return TX_CONTINUE;
210
211         /* dynamic power save disabled */
212         if (local->hw.conf.dynamic_ps_timeout <= 0)
213                 return TX_CONTINUE;
214
215         /* we are scanning, don't enable power save */
216         if (local->scanning)
217                 return TX_CONTINUE;
218
219         if (!local->ps_sdata)
220                 return TX_CONTINUE;
221
222         /* No point if we're going to suspend */
223         if (local->quiescing)
224                 return TX_CONTINUE;
225
226         /* dynamic ps is supported only in managed mode */
227         if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
228                 return TX_CONTINUE;
229
230         ifmgd = &tx->sdata->u.mgd;
231
232         /*
233          * Don't wakeup from power save if u-apsd is enabled, voip ac has
234          * u-apsd enabled and the frame is in voip class. This effectively
235          * means that even if all access categories have u-apsd enabled, in
236          * practise u-apsd is only used with the voip ac. This is a
237          * workaround for the case when received voip class packets do not
238          * have correct qos tag for some reason, due the network or the
239          * peer application.
240          *
241          * Note: ifmgd->uapsd_queues access is racy here. If the value is
242          * changed via debugfs, user needs to reassociate manually to have
243          * everything in sync.
244          */
245         if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED) &&
246             (ifmgd->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO) &&
247             skb_get_queue_mapping(tx->skb) == IEEE80211_AC_VO)
248                 return TX_CONTINUE;
249
250         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
251                 ieee80211_stop_queues_by_reason(&local->hw,
252                                                 IEEE80211_MAX_QUEUE_MAP,
253                                                 IEEE80211_QUEUE_STOP_REASON_PS);
254                 ifmgd->flags &= ~IEEE80211_STA_NULLFUNC_ACKED;
255                 ieee80211_queue_work(&local->hw,
256                                      &local->dynamic_ps_disable_work);
257         }
258
259         /* Don't restart the timer if we're not disassociated */
260         if (!ifmgd->associated)
261                 return TX_CONTINUE;
262
263         mod_timer(&local->dynamic_ps_timer, jiffies +
264                   msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
265
266         return TX_CONTINUE;
267 }
268
269 static ieee80211_tx_result debug_noinline
270 ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
271 {
272
273         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
274         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
275         bool assoc = false;
276
277         if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
278                 return TX_CONTINUE;
279
280         if (unlikely(test_bit(SCAN_SW_SCANNING, &tx->local->scanning)) &&
281             test_bit(SDATA_STATE_OFFCHANNEL, &tx->sdata->state) &&
282             !ieee80211_is_probe_req(hdr->frame_control) &&
283             !ieee80211_is_nullfunc(hdr->frame_control))
284                 /*
285                  * When software scanning only nullfunc frames (to notify
286                  * the sleep state to the AP) and probe requests (for the
287                  * active scan) are allowed, all other frames should not be
288                  * sent and we should not get here, but if we do
289                  * nonetheless, drop them to avoid sending them
290                  * off-channel. See the link below and
291                  * ieee80211_start_scan() for more.
292                  *
293                  * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
294                  */
295                 return TX_DROP;
296
297         if (tx->sdata->vif.type == NL80211_IFTYPE_WDS)
298                 return TX_CONTINUE;
299
300         if (tx->flags & IEEE80211_TX_PS_BUFFERED)
301                 return TX_CONTINUE;
302
303         if (tx->sta)
304                 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
305
306         if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
307                 if (unlikely(!assoc &&
308                              ieee80211_is_data(hdr->frame_control))) {
309 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
310                         sdata_info(tx->sdata,
311                                    "dropped data frame to not associated station %pM\n",
312                                    hdr->addr1);
313 #endif
314                         I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
315                         return TX_DROP;
316                 }
317         } else if (unlikely(tx->sdata->vif.type == NL80211_IFTYPE_AP &&
318                             ieee80211_is_data(hdr->frame_control) &&
319                             !atomic_read(&tx->sdata->u.ap.num_mcast_sta))) {
320                 /*
321                  * No associated STAs - no need to send multicast
322                  * frames.
323                  */
324                 return TX_DROP;
325         }
326
327         return TX_CONTINUE;
328 }
329
330 /* This function is called whenever the AP is about to exceed the maximum limit
331  * of buffered frames for power saving STAs. This situation should not really
332  * happen often during normal operation, so dropping the oldest buffered packet
333  * from each queue should be OK to make some room for new frames. */
334 static void purge_old_ps_buffers(struct ieee80211_local *local)
335 {
336         int total = 0, purged = 0;
337         struct sk_buff *skb;
338         struct ieee80211_sub_if_data *sdata;
339         struct sta_info *sta;
340
341         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
342                 struct ps_data *ps;
343
344                 if (sdata->vif.type == NL80211_IFTYPE_AP)
345                         ps = &sdata->u.ap.ps;
346                 else if (ieee80211_vif_is_mesh(&sdata->vif))
347                         ps = &sdata->u.mesh.ps;
348                 else
349                         continue;
350
351                 skb = skb_dequeue(&ps->bc_buf);
352                 if (skb) {
353                         purged++;
354                         ieee80211_free_txskb(&local->hw, skb);
355                 }
356                 total += skb_queue_len(&ps->bc_buf);
357         }
358
359         /*
360          * Drop one frame from each station from the lowest-priority
361          * AC that has frames at all.
362          */
363         list_for_each_entry_rcu(sta, &local->sta_list, list) {
364                 int ac;
365
366                 for (ac = IEEE80211_AC_BK; ac >= IEEE80211_AC_VO; ac--) {
367                         skb = skb_dequeue(&sta->ps_tx_buf[ac]);
368                         total += skb_queue_len(&sta->ps_tx_buf[ac]);
369                         if (skb) {
370                                 purged++;
371                                 ieee80211_free_txskb(&local->hw, skb);
372                                 break;
373                         }
374                 }
375         }
376
377         local->total_ps_buffered = total;
378         ps_dbg_hw(&local->hw, "PS buffers full - purged %d frames\n", purged);
379 }
380
381 static ieee80211_tx_result
382 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
383 {
384         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
385         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
386         struct ps_data *ps;
387
388         /*
389          * broadcast/multicast frame
390          *
391          * If any of the associated/peer stations is in power save mode,
392          * the frame is buffered to be sent after DTIM beacon frame.
393          * This is done either by the hardware or us.
394          */
395
396         /* powersaving STAs currently only in AP/VLAN/mesh mode */
397         if (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
398             tx->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
399                 if (!tx->sdata->bss)
400                         return TX_CONTINUE;
401
402                 ps = &tx->sdata->bss->ps;
403         } else if (ieee80211_vif_is_mesh(&tx->sdata->vif)) {
404                 ps = &tx->sdata->u.mesh.ps;
405         } else {
406                 return TX_CONTINUE;
407         }
408
409
410         /* no buffering for ordered frames */
411         if (ieee80211_has_order(hdr->frame_control))
412                 return TX_CONTINUE;
413
414         if (ieee80211_is_probe_req(hdr->frame_control))
415                 return TX_CONTINUE;
416
417         if (tx->local->hw.flags & IEEE80211_HW_QUEUE_CONTROL)
418                 info->hw_queue = tx->sdata->vif.cab_queue;
419
420         /* no stations in PS mode */
421         if (!atomic_read(&ps->num_sta_ps))
422                 return TX_CONTINUE;
423
424         info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
425
426         /* device releases frame after DTIM beacon */
427         if (!(tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING))
428                 return TX_CONTINUE;
429
430         /* buffered in mac80211 */
431         if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
432                 purge_old_ps_buffers(tx->local);
433
434         if (skb_queue_len(&ps->bc_buf) >= AP_MAX_BC_BUFFER) {
435                 ps_dbg(tx->sdata,
436                        "BC TX buffer full - dropping the oldest frame\n");
437                 ieee80211_free_txskb(&tx->local->hw, skb_dequeue(&ps->bc_buf));
438         } else
439                 tx->local->total_ps_buffered++;
440
441         skb_queue_tail(&ps->bc_buf, tx->skb);
442
443         return TX_QUEUED;
444 }
445
446 static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
447                              struct sk_buff *skb)
448 {
449         if (!ieee80211_is_mgmt(fc))
450                 return 0;
451
452         if (sta == NULL || !test_sta_flag(sta, WLAN_STA_MFP))
453                 return 0;
454
455         if (!ieee80211_is_robust_mgmt_frame(skb))
456                 return 0;
457
458         return 1;
459 }
460
461 static ieee80211_tx_result
462 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
463 {
464         struct sta_info *sta = tx->sta;
465         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
466         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
467         struct ieee80211_local *local = tx->local;
468
469         if (unlikely(!sta))
470                 return TX_CONTINUE;
471
472         if (unlikely((test_sta_flag(sta, WLAN_STA_PS_STA) ||
473                       test_sta_flag(sta, WLAN_STA_PS_DRIVER)) &&
474                      !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
475                 int ac = skb_get_queue_mapping(tx->skb);
476
477                 if (ieee80211_is_mgmt(hdr->frame_control) &&
478                     !ieee80211_is_bufferable_mmpdu(hdr->frame_control)) {
479                         info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
480                         return TX_CONTINUE;
481                 }
482
483                 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
484                        sta->sta.addr, sta->sta.aid, ac);
485                 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
486                         purge_old_ps_buffers(tx->local);
487
488                 /* sync with ieee80211_sta_ps_deliver_wakeup */
489                 spin_lock(&sta->ps_lock);
490                 /*
491                  * STA woke up the meantime and all the frames on ps_tx_buf have
492                  * been queued to pending queue. No reordering can happen, go
493                  * ahead and Tx the packet.
494                  */
495                 if (!test_sta_flag(sta, WLAN_STA_PS_STA) &&
496                     !test_sta_flag(sta, WLAN_STA_PS_DRIVER)) {
497                         spin_unlock(&sta->ps_lock);
498                         return TX_CONTINUE;
499                 }
500
501                 if (skb_queue_len(&sta->ps_tx_buf[ac]) >= STA_MAX_TX_BUFFER) {
502                         struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf[ac]);
503                         ps_dbg(tx->sdata,
504                                "STA %pM TX buffer for AC %d full - dropping oldest frame\n",
505                                sta->sta.addr, ac);
506                         ieee80211_free_txskb(&local->hw, old);
507                 } else
508                         tx->local->total_ps_buffered++;
509
510                 info->control.jiffies = jiffies;
511                 info->control.vif = &tx->sdata->vif;
512                 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
513                 info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
514                 skb_queue_tail(&sta->ps_tx_buf[ac], tx->skb);
515                 spin_unlock(&sta->ps_lock);
516
517                 if (!timer_pending(&local->sta_cleanup))
518                         mod_timer(&local->sta_cleanup,
519                                   round_jiffies(jiffies +
520                                                 STA_INFO_CLEANUP_INTERVAL));
521
522                 /*
523                  * We queued up some frames, so the TIM bit might
524                  * need to be set, recalculate it.
525                  */
526                 sta_info_recalc_tim(sta);
527
528                 return TX_QUEUED;
529         } else if (unlikely(test_sta_flag(sta, WLAN_STA_PS_STA))) {
530                 ps_dbg(tx->sdata,
531                        "STA %pM in PS mode, but polling/in SP -> send frame\n",
532                        sta->sta.addr);
533         }
534
535         return TX_CONTINUE;
536 }
537
538 static ieee80211_tx_result debug_noinline
539 ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
540 {
541         if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
542                 return TX_CONTINUE;
543
544         if (tx->flags & IEEE80211_TX_UNICAST)
545                 return ieee80211_tx_h_unicast_ps_buf(tx);
546         else
547                 return ieee80211_tx_h_multicast_ps_buf(tx);
548 }
549
550 static ieee80211_tx_result debug_noinline
551 ieee80211_tx_h_check_control_port_protocol(struct ieee80211_tx_data *tx)
552 {
553         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
554
555         if (unlikely(tx->sdata->control_port_protocol == tx->skb->protocol)) {
556                 if (tx->sdata->control_port_no_encrypt)
557                         info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
558                 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
559                 info->flags |= IEEE80211_TX_CTL_USE_MINRATE;
560         }
561
562         return TX_CONTINUE;
563 }
564
565 static ieee80211_tx_result debug_noinline
566 ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
567 {
568         struct ieee80211_key *key;
569         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
570         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
571
572         if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
573                 tx->key = NULL;
574         else if (tx->sta &&
575                  (key = rcu_dereference(tx->sta->ptk[tx->sta->ptk_idx])))
576                 tx->key = key;
577         else if (ieee80211_is_mgmt(hdr->frame_control) &&
578                  is_multicast_ether_addr(hdr->addr1) &&
579                  ieee80211_is_robust_mgmt_frame(tx->skb) &&
580                  (key = rcu_dereference(tx->sdata->default_mgmt_key)))
581                 tx->key = key;
582         else if (is_multicast_ether_addr(hdr->addr1) &&
583                  (key = rcu_dereference(tx->sdata->default_multicast_key)))
584                 tx->key = key;
585         else if (!is_multicast_ether_addr(hdr->addr1) &&
586                  (key = rcu_dereference(tx->sdata->default_unicast_key)))
587                 tx->key = key;
588         else if (info->flags & IEEE80211_TX_CTL_INJECTED)
589                 tx->key = NULL;
590         else if (!tx->sdata->drop_unencrypted)
591                 tx->key = NULL;
592         else if (tx->skb->protocol == tx->sdata->control_port_protocol)
593                 tx->key = NULL;
594         else if (ieee80211_is_robust_mgmt_frame(tx->skb) &&
595                  !(ieee80211_is_action(hdr->frame_control) &&
596                    tx->sta && test_sta_flag(tx->sta, WLAN_STA_MFP)))
597                 tx->key = NULL;
598         else if (ieee80211_is_mgmt(hdr->frame_control) &&
599                  !ieee80211_is_robust_mgmt_frame(tx->skb))
600                 tx->key = NULL;
601         else {
602                 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
603                 return TX_DROP;
604         }
605
606         if (tx->key) {
607                 bool skip_hw = false;
608
609                 tx->key->tx_rx_count++;
610                 /* TODO: add threshold stuff again */
611
612                 switch (tx->key->conf.cipher) {
613                 case WLAN_CIPHER_SUITE_WEP40:
614                 case WLAN_CIPHER_SUITE_WEP104:
615                 case WLAN_CIPHER_SUITE_TKIP:
616                         if (!ieee80211_is_data_present(hdr->frame_control))
617                                 tx->key = NULL;
618                         break;
619                 case WLAN_CIPHER_SUITE_CCMP:
620                         if (!ieee80211_is_data_present(hdr->frame_control) &&
621                             !ieee80211_use_mfp(hdr->frame_control, tx->sta,
622                                                tx->skb))
623                                 tx->key = NULL;
624                         else
625                                 skip_hw = (tx->key->conf.flags &
626                                            IEEE80211_KEY_FLAG_SW_MGMT_TX) &&
627                                         ieee80211_is_mgmt(hdr->frame_control);
628                         break;
629                 case WLAN_CIPHER_SUITE_AES_CMAC:
630                         if (!ieee80211_is_mgmt(hdr->frame_control))
631                                 tx->key = NULL;
632                         break;
633                 }
634
635                 if (unlikely(tx->key && tx->key->flags & KEY_FLAG_TAINTED &&
636                              !ieee80211_is_deauth(hdr->frame_control)))
637                         return TX_DROP;
638
639                 if (!skip_hw && tx->key &&
640                     tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
641                         info->control.hw_key = &tx->key->conf;
642         }
643
644         return TX_CONTINUE;
645 }
646
647 static ieee80211_tx_result debug_noinline
648 ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
649 {
650         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
651         struct ieee80211_hdr *hdr = (void *)tx->skb->data;
652         struct ieee80211_supported_band *sband;
653         u32 len;
654         struct ieee80211_tx_rate_control txrc;
655         struct ieee80211_sta_rates *ratetbl = NULL;
656         bool assoc = false;
657
658         memset(&txrc, 0, sizeof(txrc));
659
660         sband = tx->local->hw.wiphy->bands[info->band];
661
662         len = min_t(u32, tx->skb->len + FCS_LEN,
663                          tx->local->hw.wiphy->frag_threshold);
664
665         /* set up the tx rate control struct we give the RC algo */
666         txrc.hw = &tx->local->hw;
667         txrc.sband = sband;
668         txrc.bss_conf = &tx->sdata->vif.bss_conf;
669         txrc.skb = tx->skb;
670         txrc.reported_rate.idx = -1;
671         txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[info->band];
672         if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
673                 txrc.max_rate_idx = -1;
674         else
675                 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
676
677         if (tx->sdata->rc_has_mcs_mask[info->band])
678                 txrc.rate_idx_mcs_mask =
679                         tx->sdata->rc_rateidx_mcs_mask[info->band];
680
681         txrc.bss = (tx->sdata->vif.type == NL80211_IFTYPE_AP ||
682                     tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT ||
683                     tx->sdata->vif.type == NL80211_IFTYPE_ADHOC);
684
685         /* set up RTS protection if desired */
686         if (len > tx->local->hw.wiphy->rts_threshold) {
687                 txrc.rts = true;
688         }
689
690         info->control.use_rts = txrc.rts;
691         info->control.use_cts_prot = tx->sdata->vif.bss_conf.use_cts_prot;
692
693         /*
694          * Use short preamble if the BSS can handle it, but not for
695          * management frames unless we know the receiver can handle
696          * that -- the management frame might be to a station that
697          * just wants a probe response.
698          */
699         if (tx->sdata->vif.bss_conf.use_short_preamble &&
700             (ieee80211_is_data(hdr->frame_control) ||
701              (tx->sta && test_sta_flag(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
702                 txrc.short_preamble = true;
703
704         info->control.short_preamble = txrc.short_preamble;
705
706         if (tx->sta)
707                 assoc = test_sta_flag(tx->sta, WLAN_STA_ASSOC);
708
709         /*
710          * Lets not bother rate control if we're associated and cannot
711          * talk to the sta. This should not happen.
712          */
713         if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) && assoc &&
714                  !rate_usable_index_exists(sband, &tx->sta->sta),
715                  "%s: Dropped data frame as no usable bitrate found while "
716                  "scanning and associated. Target station: "
717                  "%pM on %d GHz band\n",
718                  tx->sdata->name, hdr->addr1,
719                  info->band ? 5 : 2))
720                 return TX_DROP;
721
722         /*
723          * If we're associated with the sta at this point we know we can at
724          * least send the frame at the lowest bit rate.
725          */
726         rate_control_get_rate(tx->sdata, tx->sta, &txrc);
727
728         if (tx->sta && !info->control.skip_table)
729                 ratetbl = rcu_dereference(tx->sta->sta.rates);
730
731         if (unlikely(info->control.rates[0].idx < 0)) {
732                 if (ratetbl) {
733                         struct ieee80211_tx_rate rate = {
734                                 .idx = ratetbl->rate[0].idx,
735                                 .flags = ratetbl->rate[0].flags,
736                                 .count = ratetbl->rate[0].count
737                         };
738
739                         if (ratetbl->rate[0].idx < 0)
740                                 return TX_DROP;
741
742                         tx->rate = rate;
743                 } else {
744                         return TX_DROP;
745                 }
746         } else {
747                 tx->rate = info->control.rates[0];
748         }
749
750         if (txrc.reported_rate.idx < 0) {
751                 txrc.reported_rate = tx->rate;
752                 if (tx->sta && ieee80211_is_data(hdr->frame_control))
753                         tx->sta->last_tx_rate = txrc.reported_rate;
754         } else if (tx->sta)
755                 tx->sta->last_tx_rate = txrc.reported_rate;
756
757         if (ratetbl)
758                 return TX_CONTINUE;
759
760         if (unlikely(!info->control.rates[0].count))
761                 info->control.rates[0].count = 1;
762
763         if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
764                          (info->flags & IEEE80211_TX_CTL_NO_ACK)))
765                 info->control.rates[0].count = 1;
766
767         return TX_CONTINUE;
768 }
769
770 static ieee80211_tx_result debug_noinline
771 ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
772 {
773         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
774         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
775         u16 *seq;
776         u8 *qc;
777         int tid;
778
779         /*
780          * Packet injection may want to control the sequence
781          * number, if we have no matching interface then we
782          * neither assign one ourselves nor ask the driver to.
783          */
784         if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
785                 return TX_CONTINUE;
786
787         if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
788                 return TX_CONTINUE;
789
790         if (ieee80211_hdrlen(hdr->frame_control) < 24)
791                 return TX_CONTINUE;
792
793         if (ieee80211_is_qos_nullfunc(hdr->frame_control))
794                 return TX_CONTINUE;
795
796         /*
797          * Anything but QoS data that has a sequence number field
798          * (is long enough) gets a sequence number from the global
799          * counter.  QoS data frames with a multicast destination
800          * also use the global counter (802.11-2012 9.3.2.10).
801          */
802         if (!ieee80211_is_data_qos(hdr->frame_control) ||
803             is_multicast_ether_addr(hdr->addr1)) {
804                 /* driver should assign sequence number */
805                 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
806                 /* for pure STA mode without beacons, we can do it */
807                 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
808                 tx->sdata->sequence_number += 0x10;
809                 return TX_CONTINUE;
810         }
811
812         /*
813          * This should be true for injected/management frames only, for
814          * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
815          * above since they are not QoS-data frames.
816          */
817         if (!tx->sta)
818                 return TX_CONTINUE;
819
820         /* include per-STA, per-TID sequence counter */
821
822         qc = ieee80211_get_qos_ctl(hdr);
823         tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
824         seq = &tx->sta->tid_seq[tid];
825
826         hdr->seq_ctrl = cpu_to_le16(*seq);
827
828         /* Increase the sequence number. */
829         *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
830
831         return TX_CONTINUE;
832 }
833
834 static int ieee80211_fragment(struct ieee80211_tx_data *tx,
835                               struct sk_buff *skb, int hdrlen,
836                               int frag_threshold)
837 {
838         struct ieee80211_local *local = tx->local;
839         struct ieee80211_tx_info *info;
840         struct sk_buff *tmp;
841         int per_fragm = frag_threshold - hdrlen - FCS_LEN;
842         int pos = hdrlen + per_fragm;
843         int rem = skb->len - hdrlen - per_fragm;
844
845         if (WARN_ON(rem < 0))
846                 return -EINVAL;
847
848         /* first fragment was already added to queue by caller */
849
850         while (rem) {
851                 int fraglen = per_fragm;
852
853                 if (fraglen > rem)
854                         fraglen = rem;
855                 rem -= fraglen;
856                 tmp = dev_alloc_skb(local->tx_headroom +
857                                     frag_threshold +
858                                     tx->sdata->encrypt_headroom +
859                                     IEEE80211_ENCRYPT_TAILROOM);
860                 if (!tmp)
861                         return -ENOMEM;
862
863                 __skb_queue_tail(&tx->skbs, tmp);
864
865                 skb_reserve(tmp,
866                             local->tx_headroom + tx->sdata->encrypt_headroom);
867
868                 /* copy control information */
869                 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
870
871                 info = IEEE80211_SKB_CB(tmp);
872                 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
873                                  IEEE80211_TX_CTL_FIRST_FRAGMENT);
874
875                 if (rem)
876                         info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
877
878                 skb_copy_queue_mapping(tmp, skb);
879                 tmp->priority = skb->priority;
880                 tmp->dev = skb->dev;
881
882                 /* copy header and data */
883                 memcpy(skb_put(tmp, hdrlen), skb->data, hdrlen);
884                 memcpy(skb_put(tmp, fraglen), skb->data + pos, fraglen);
885
886                 pos += fraglen;
887         }
888
889         /* adjust first fragment's length */
890         skb_trim(skb, hdrlen + per_fragm);
891         return 0;
892 }
893
894 static ieee80211_tx_result debug_noinline
895 ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
896 {
897         struct sk_buff *skb = tx->skb;
898         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
899         struct ieee80211_hdr *hdr = (void *)skb->data;
900         int frag_threshold = tx->local->hw.wiphy->frag_threshold;
901         int hdrlen;
902         int fragnum;
903
904         /* no matter what happens, tx->skb moves to tx->skbs */
905         __skb_queue_tail(&tx->skbs, skb);
906         tx->skb = NULL;
907
908         if (info->flags & IEEE80211_TX_CTL_DONTFRAG)
909                 return TX_CONTINUE;
910
911         if (tx->local->ops->set_frag_threshold)
912                 return TX_CONTINUE;
913
914         /*
915          * Warn when submitting a fragmented A-MPDU frame and drop it.
916          * This scenario is handled in ieee80211_tx_prepare but extra
917          * caution taken here as fragmented ampdu may cause Tx stop.
918          */
919         if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
920                 return TX_DROP;
921
922         hdrlen = ieee80211_hdrlen(hdr->frame_control);
923
924         /* internal error, why isn't DONTFRAG set? */
925         if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
926                 return TX_DROP;
927
928         /*
929          * Now fragment the frame. This will allocate all the fragments and
930          * chain them (using skb as the first fragment) to skb->next.
931          * During transmission, we will remove the successfully transmitted
932          * fragments from this list. When the low-level driver rejects one
933          * of the fragments then we will simply pretend to accept the skb
934          * but store it away as pending.
935          */
936         if (ieee80211_fragment(tx, skb, hdrlen, frag_threshold))
937                 return TX_DROP;
938
939         /* update duration/seq/flags of fragments */
940         fragnum = 0;
941
942         skb_queue_walk(&tx->skbs, skb) {
943                 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
944
945                 hdr = (void *)skb->data;
946                 info = IEEE80211_SKB_CB(skb);
947
948                 if (!skb_queue_is_last(&tx->skbs, skb)) {
949                         hdr->frame_control |= morefrags;
950                         /*
951                          * No multi-rate retries for fragmented frames, that
952                          * would completely throw off the NAV at other STAs.
953                          */
954                         info->control.rates[1].idx = -1;
955                         info->control.rates[2].idx = -1;
956                         info->control.rates[3].idx = -1;
957                         BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 4);
958                         info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
959                 } else {
960                         hdr->frame_control &= ~morefrags;
961                 }
962                 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
963                 fragnum++;
964         }
965
966         return TX_CONTINUE;
967 }
968
969 static ieee80211_tx_result debug_noinline
970 ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
971 {
972         struct sk_buff *skb;
973         int ac = -1;
974
975         if (!tx->sta)
976                 return TX_CONTINUE;
977
978         skb_queue_walk(&tx->skbs, skb) {
979                 ac = skb_get_queue_mapping(skb);
980                 tx->sta->tx_fragments++;
981                 tx->sta->tx_bytes[ac] += skb->len;
982         }
983         if (ac >= 0)
984                 tx->sta->tx_packets[ac]++;
985
986         return TX_CONTINUE;
987 }
988
989 static ieee80211_tx_result debug_noinline
990 ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
991 {
992         if (!tx->key)
993                 return TX_CONTINUE;
994
995         switch (tx->key->conf.cipher) {
996         case WLAN_CIPHER_SUITE_WEP40:
997         case WLAN_CIPHER_SUITE_WEP104:
998                 return ieee80211_crypto_wep_encrypt(tx);
999         case WLAN_CIPHER_SUITE_TKIP:
1000                 return ieee80211_crypto_tkip_encrypt(tx);
1001         case WLAN_CIPHER_SUITE_CCMP:
1002                 return ieee80211_crypto_ccmp_encrypt(tx);
1003         case WLAN_CIPHER_SUITE_AES_CMAC:
1004                 return ieee80211_crypto_aes_cmac_encrypt(tx);
1005         default:
1006                 return ieee80211_crypto_hw_encrypt(tx);
1007         }
1008
1009         return TX_DROP;
1010 }
1011
1012 static ieee80211_tx_result debug_noinline
1013 ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
1014 {
1015         struct sk_buff *skb;
1016         struct ieee80211_hdr *hdr;
1017         int next_len;
1018         bool group_addr;
1019
1020         skb_queue_walk(&tx->skbs, skb) {
1021                 hdr = (void *) skb->data;
1022                 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
1023                         break; /* must not overwrite AID */
1024                 if (!skb_queue_is_last(&tx->skbs, skb)) {
1025                         struct sk_buff *next = skb_queue_next(&tx->skbs, skb);
1026                         next_len = next->len;
1027                 } else
1028                         next_len = 0;
1029                 group_addr = is_multicast_ether_addr(hdr->addr1);
1030
1031                 hdr->duration_id =
1032                         ieee80211_duration(tx, skb, group_addr, next_len);
1033         }
1034
1035         return TX_CONTINUE;
1036 }
1037
1038 /* actual transmit path */
1039
1040 static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1041                                   struct sk_buff *skb,
1042                                   struct ieee80211_tx_info *info,
1043                                   struct tid_ampdu_tx *tid_tx,
1044                                   int tid)
1045 {
1046         bool queued = false;
1047         bool reset_agg_timer = false;
1048         struct sk_buff *purge_skb = NULL;
1049
1050         if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1051                 info->flags |= IEEE80211_TX_CTL_AMPDU;
1052                 reset_agg_timer = true;
1053         } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1054                 /*
1055                  * nothing -- this aggregation session is being started
1056                  * but that might still fail with the driver
1057                  */
1058         } else {
1059                 spin_lock(&tx->sta->lock);
1060                 /*
1061                  * Need to re-check now, because we may get here
1062                  *
1063                  *  1) in the window during which the setup is actually
1064                  *     already done, but not marked yet because not all
1065                  *     packets are spliced over to the driver pending
1066                  *     queue yet -- if this happened we acquire the lock
1067                  *     either before or after the splice happens, but
1068                  *     need to recheck which of these cases happened.
1069                  *
1070                  *  2) during session teardown, if the OPERATIONAL bit
1071                  *     was cleared due to the teardown but the pointer
1072                  *     hasn't been assigned NULL yet (or we loaded it
1073                  *     before it was assigned) -- in this case it may
1074                  *     now be NULL which means we should just let the
1075                  *     packet pass through because splicing the frames
1076                  *     back is already done.
1077                  */
1078                 tid_tx = rcu_dereference_protected_tid_tx(tx->sta, tid);
1079
1080                 if (!tid_tx) {
1081                         /* do nothing, let packet pass through */
1082                 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1083                         info->flags |= IEEE80211_TX_CTL_AMPDU;
1084                         reset_agg_timer = true;
1085                 } else {
1086                         queued = true;
1087                         info->control.vif = &tx->sdata->vif;
1088                         info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1089                         info->flags &= ~IEEE80211_TX_TEMPORARY_FLAGS;
1090                         __skb_queue_tail(&tid_tx->pending, skb);
1091                         if (skb_queue_len(&tid_tx->pending) > STA_MAX_TX_BUFFER)
1092                                 purge_skb = __skb_dequeue(&tid_tx->pending);
1093                 }
1094                 spin_unlock(&tx->sta->lock);
1095
1096                 if (purge_skb)
1097                         ieee80211_free_txskb(&tx->local->hw, purge_skb);
1098         }
1099
1100         /* reset session timer */
1101         if (reset_agg_timer && tid_tx->timeout)
1102                 tid_tx->last_tx = jiffies;
1103
1104         return queued;
1105 }
1106
1107 /*
1108  * initialises @tx
1109  */
1110 static ieee80211_tx_result
1111 ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1112                      struct ieee80211_tx_data *tx,
1113                      struct sk_buff *skb)
1114 {
1115         struct ieee80211_local *local = sdata->local;
1116         struct ieee80211_hdr *hdr;
1117         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1118         int tid;
1119         u8 *qc;
1120
1121         memset(tx, 0, sizeof(*tx));
1122         tx->skb = skb;
1123         tx->local = local;
1124         tx->sdata = sdata;
1125         __skb_queue_head_init(&tx->skbs);
1126
1127         /*
1128          * If this flag is set to true anywhere, and we get here,
1129          * we are doing the needed processing, so remove the flag
1130          * now.
1131          */
1132         info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1133
1134         hdr = (struct ieee80211_hdr *) skb->data;
1135
1136         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1137                 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1138                 if (!tx->sta && sdata->dev->ieee80211_ptr->use_4addr)
1139                         return TX_DROP;
1140         } else if (info->flags & (IEEE80211_TX_CTL_INJECTED |
1141                                   IEEE80211_TX_INTFL_NL80211_FRAME_TX) ||
1142                    tx->sdata->control_port_protocol == tx->skb->protocol) {
1143                 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1144         }
1145         if (!tx->sta)
1146                 tx->sta = sta_info_get(sdata, hdr->addr1);
1147
1148         if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
1149             !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
1150             (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION) &&
1151             !(local->hw.flags & IEEE80211_HW_TX_AMPDU_SETUP_IN_HW)) {
1152                 struct tid_ampdu_tx *tid_tx;
1153
1154                 qc = ieee80211_get_qos_ctl(hdr);
1155                 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
1156
1157                 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1158                 if (tid_tx) {
1159                         bool queued;
1160
1161                         queued = ieee80211_tx_prep_agg(tx, skb, info,
1162                                                        tid_tx, tid);
1163
1164                         if (unlikely(queued))
1165                                 return TX_QUEUED;
1166                 }
1167         }
1168
1169         if (is_multicast_ether_addr(hdr->addr1)) {
1170                 tx->flags &= ~IEEE80211_TX_UNICAST;
1171                 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1172         } else
1173                 tx->flags |= IEEE80211_TX_UNICAST;
1174
1175         if (!(info->flags & IEEE80211_TX_CTL_DONTFRAG)) {
1176                 if (!(tx->flags & IEEE80211_TX_UNICAST) ||
1177                     skb->len + FCS_LEN <= local->hw.wiphy->frag_threshold ||
1178                     info->flags & IEEE80211_TX_CTL_AMPDU)
1179                         info->flags |= IEEE80211_TX_CTL_DONTFRAG;
1180         }
1181
1182         if (!tx->sta)
1183                 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1184         else if (test_and_clear_sta_flag(tx->sta, WLAN_STA_CLEAR_PS_FILT))
1185                 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1186
1187         info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
1188
1189         return TX_CONTINUE;
1190 }
1191
1192 static bool ieee80211_tx_frags(struct ieee80211_local *local,
1193                                struct ieee80211_vif *vif,
1194                                struct ieee80211_sta *sta,
1195                                struct sk_buff_head *skbs,
1196                                bool txpending)
1197 {
1198         struct ieee80211_tx_control control;
1199         struct sk_buff *skb, *tmp;
1200         unsigned long flags;
1201
1202         skb_queue_walk_safe(skbs, skb, tmp) {
1203                 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1204                 int q = info->hw_queue;
1205
1206 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1207                 if (WARN_ON_ONCE(q >= local->hw.queues)) {
1208                         __skb_unlink(skb, skbs);
1209                         ieee80211_free_txskb(&local->hw, skb);
1210                         continue;
1211                 }
1212 #endif
1213
1214                 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1215                 if (local->queue_stop_reasons[q] ||
1216                     (!txpending && !skb_queue_empty(&local->pending[q]))) {
1217                         if (unlikely(info->flags &
1218                                      IEEE80211_TX_INTFL_OFFCHAN_TX_OK)) {
1219                                 if (local->queue_stop_reasons[q] &
1220                                     ~BIT(IEEE80211_QUEUE_STOP_REASON_OFFCHANNEL)) {
1221                                         /*
1222                                          * Drop off-channel frames if queues
1223                                          * are stopped for any reason other
1224                                          * than off-channel operation. Never
1225                                          * queue them.
1226                                          */
1227                                         spin_unlock_irqrestore(
1228                                                 &local->queue_stop_reason_lock,
1229                                                 flags);
1230                                         ieee80211_purge_tx_queue(&local->hw,
1231                                                                  skbs);
1232                                         return true;
1233                                 }
1234                         } else {
1235
1236                                 /*
1237                                  * Since queue is stopped, queue up frames for
1238                                  * later transmission from the tx-pending
1239                                  * tasklet when the queue is woken again.
1240                                  */
1241                                 if (txpending)
1242                                         skb_queue_splice_init(skbs,
1243                                                               &local->pending[q]);
1244                                 else
1245                                         skb_queue_splice_tail_init(skbs,
1246                                                                    &local->pending[q]);
1247
1248                                 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1249                                                        flags);
1250                                 return false;
1251                         }
1252                 }
1253                 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1254
1255                 info->control.vif = vif;
1256                 control.sta = sta;
1257
1258                 __skb_unlink(skb, skbs);
1259                 drv_tx(local, &control, skb);
1260         }
1261
1262         return true;
1263 }
1264
1265 /*
1266  * Returns false if the frame couldn't be transmitted but was queued instead.
1267  */
1268 static bool __ieee80211_tx(struct ieee80211_local *local,
1269                            struct sk_buff_head *skbs, int led_len,
1270                            struct sta_info *sta, bool txpending)
1271 {
1272         struct ieee80211_tx_info *info;
1273         struct ieee80211_sub_if_data *sdata;
1274         struct ieee80211_vif *vif;
1275         struct ieee80211_sta *pubsta;
1276         struct sk_buff *skb;
1277         bool result = true;
1278         __le16 fc;
1279
1280         if (WARN_ON(skb_queue_empty(skbs)))
1281                 return true;
1282
1283         skb = skb_peek(skbs);
1284         fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
1285         info = IEEE80211_SKB_CB(skb);
1286         sdata = vif_to_sdata(info->control.vif);
1287         if (sta && !sta->uploaded)
1288                 sta = NULL;
1289
1290         if (sta)
1291                 pubsta = &sta->sta;
1292         else
1293                 pubsta = NULL;
1294
1295         switch (sdata->vif.type) {
1296         case NL80211_IFTYPE_MONITOR:
1297                 if (sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE) {
1298                         vif = &sdata->vif;
1299                         break;
1300                 }
1301                 sdata = rcu_dereference(local->monitor_sdata);
1302                 if (sdata) {
1303                         vif = &sdata->vif;
1304                         info->hw_queue =
1305                                 vif->hw_queue[skb_get_queue_mapping(skb)];
1306                 } else if (local->hw.flags & IEEE80211_HW_QUEUE_CONTROL) {
1307                         dev_kfree_skb(skb);
1308                         return true;
1309                 } else
1310                         vif = NULL;
1311                 break;
1312         case NL80211_IFTYPE_AP_VLAN:
1313                 sdata = container_of(sdata->bss,
1314                                      struct ieee80211_sub_if_data, u.ap);
1315                 /* fall through */
1316         default:
1317                 vif = &sdata->vif;
1318                 break;
1319         }
1320
1321         result = ieee80211_tx_frags(local, vif, pubsta, skbs,
1322                                     txpending);
1323
1324         ieee80211_tpt_led_trig_tx(local, fc, led_len);
1325
1326         WARN_ON_ONCE(!skb_queue_empty(skbs));
1327
1328         return result;
1329 }
1330
1331 /*
1332  * Invoke TX handlers, return 0 on success and non-zero if the
1333  * frame was dropped or queued.
1334  */
1335 static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1336 {
1337         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
1338         ieee80211_tx_result res = TX_DROP;
1339
1340 #define CALL_TXH(txh) \
1341         do {                            \
1342                 res = txh(tx);          \
1343                 if (res != TX_CONTINUE) \
1344                         goto txh_done;  \
1345         } while (0)
1346
1347         CALL_TXH(ieee80211_tx_h_dynamic_ps);
1348         CALL_TXH(ieee80211_tx_h_check_assoc);
1349         CALL_TXH(ieee80211_tx_h_ps_buf);
1350         CALL_TXH(ieee80211_tx_h_check_control_port_protocol);
1351         CALL_TXH(ieee80211_tx_h_select_key);
1352         if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
1353                 CALL_TXH(ieee80211_tx_h_rate_ctrl);
1354
1355         if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION)) {
1356                 __skb_queue_tail(&tx->skbs, tx->skb);
1357                 tx->skb = NULL;
1358                 goto txh_done;
1359         }
1360
1361         CALL_TXH(ieee80211_tx_h_michael_mic_add);
1362         CALL_TXH(ieee80211_tx_h_sequence);
1363         CALL_TXH(ieee80211_tx_h_fragment);
1364         /* handlers after fragment must be aware of tx info fragmentation! */
1365         CALL_TXH(ieee80211_tx_h_stats);
1366         CALL_TXH(ieee80211_tx_h_encrypt);
1367         if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
1368                 CALL_TXH(ieee80211_tx_h_calculate_duration);
1369 #undef CALL_TXH
1370
1371  txh_done:
1372         if (unlikely(res == TX_DROP)) {
1373                 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1374                 if (tx->skb)
1375                         ieee80211_free_txskb(&tx->local->hw, tx->skb);
1376                 else
1377                         ieee80211_purge_tx_queue(&tx->local->hw, &tx->skbs);
1378                 return -1;
1379         } else if (unlikely(res == TX_QUEUED)) {
1380                 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1381                 return -1;
1382         }
1383
1384         return 0;
1385 }
1386
1387 bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1388                               struct ieee80211_vif *vif, struct sk_buff *skb,
1389                               int band, struct ieee80211_sta **sta)
1390 {
1391         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1392         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1393         struct ieee80211_tx_data tx;
1394
1395         if (ieee80211_tx_prepare(sdata, &tx, skb) == TX_DROP)
1396                 return false;
1397
1398         info->band = band;
1399         info->control.vif = vif;
1400         info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1401
1402         if (invoke_tx_handlers(&tx))
1403                 return false;
1404
1405         if (sta) {
1406                 if (tx.sta)
1407                         *sta = &tx.sta->sta;
1408                 else
1409                         *sta = NULL;
1410         }
1411
1412         return true;
1413 }
1414 EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1415
1416 /*
1417  * Returns false if the frame couldn't be transmitted but was queued instead.
1418  */
1419 static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1420                          struct sk_buff *skb, bool txpending,
1421                          enum ieee80211_band band)
1422 {
1423         struct ieee80211_local *local = sdata->local;
1424         struct ieee80211_tx_data tx;
1425         ieee80211_tx_result res_prepare;
1426         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1427         bool result = true;
1428         int led_len;
1429
1430         if (unlikely(skb->len < 10)) {
1431                 dev_kfree_skb(skb);
1432                 return true;
1433         }
1434
1435         /* initialises tx */
1436         led_len = skb->len;
1437         res_prepare = ieee80211_tx_prepare(sdata, &tx, skb);
1438
1439         if (unlikely(res_prepare == TX_DROP)) {
1440                 ieee80211_free_txskb(&local->hw, skb);
1441                 return true;
1442         } else if (unlikely(res_prepare == TX_QUEUED)) {
1443                 return true;
1444         }
1445
1446         info->band = band;
1447
1448         /* set up hw_queue value early */
1449         if (!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) ||
1450             !(local->hw.flags & IEEE80211_HW_QUEUE_CONTROL))
1451                 info->hw_queue =
1452                         sdata->vif.hw_queue[skb_get_queue_mapping(skb)];
1453
1454         if (!invoke_tx_handlers(&tx))
1455                 result = __ieee80211_tx(local, &tx.skbs, led_len,
1456                                         tx.sta, txpending);
1457
1458         return result;
1459 }
1460
1461 /* device xmit handlers */
1462
1463 static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
1464                                 struct sk_buff *skb,
1465                                 int head_need, bool may_encrypt)
1466 {
1467         struct ieee80211_local *local = sdata->local;
1468         int tail_need = 0;
1469
1470         if (may_encrypt && sdata->crypto_tx_tailroom_needed_cnt) {
1471                 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1472                 tail_need -= skb_tailroom(skb);
1473                 tail_need = max_t(int, tail_need, 0);
1474         }
1475
1476         if (skb_cloned(skb))
1477                 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1478         else if (head_need || tail_need)
1479                 I802_DEBUG_INC(local->tx_expand_skb_head);
1480         else
1481                 return 0;
1482
1483         if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1484                 wiphy_debug(local->hw.wiphy,
1485                             "failed to reallocate TX buffer\n");
1486                 return -ENOMEM;
1487         }
1488
1489         return 0;
1490 }
1491
1492 void ieee80211_xmit(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
1493                     enum ieee80211_band band)
1494 {
1495         struct ieee80211_local *local = sdata->local;
1496         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1497         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1498         int headroom;
1499         bool may_encrypt;
1500
1501         may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
1502
1503         headroom = local->tx_headroom;
1504         if (may_encrypt)
1505                 headroom += sdata->encrypt_headroom;
1506         headroom -= skb_headroom(skb);
1507         headroom = max_t(int, 0, headroom);
1508
1509         if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
1510                 ieee80211_free_txskb(&local->hw, skb);
1511                 return;
1512         }
1513
1514         hdr = (struct ieee80211_hdr *) skb->data;
1515         info->control.vif = &sdata->vif;
1516
1517         if (ieee80211_vif_is_mesh(&sdata->vif)) {
1518                 if (ieee80211_is_data(hdr->frame_control) &&
1519                     is_unicast_ether_addr(hdr->addr1)) {
1520                         if (mesh_nexthop_resolve(sdata, skb))
1521                                 return; /* skb queued: don't free */
1522                 } else {
1523                         ieee80211_mps_set_frame_flags(sdata, NULL, hdr);
1524                 }
1525         }
1526
1527         ieee80211_set_qos_hdr(sdata, skb);
1528         ieee80211_tx(sdata, skb, false, band);
1529 }
1530
1531 static bool ieee80211_parse_tx_radiotap(struct sk_buff *skb)
1532 {
1533         struct ieee80211_radiotap_iterator iterator;
1534         struct ieee80211_radiotap_header *rthdr =
1535                 (struct ieee80211_radiotap_header *) skb->data;
1536         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1537         int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
1538                                                    NULL);
1539         u16 txflags;
1540
1541         info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT |
1542                        IEEE80211_TX_CTL_DONTFRAG;
1543
1544         /*
1545          * for every radiotap entry that is present
1546          * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
1547          * entries present, or -EINVAL on error)
1548          */
1549
1550         while (!ret) {
1551                 ret = ieee80211_radiotap_iterator_next(&iterator);
1552
1553                 if (ret)
1554                         continue;
1555
1556                 /* see if this argument is something we can use */
1557                 switch (iterator.this_arg_index) {
1558                 /*
1559                  * You must take care when dereferencing iterator.this_arg
1560                  * for multibyte types... the pointer is not aligned.  Use
1561                  * get_unaligned((type *)iterator.this_arg) to dereference
1562                  * iterator.this_arg for type "type" safely on all arches.
1563                 */
1564                 case IEEE80211_RADIOTAP_FLAGS:
1565                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
1566                                 /*
1567                                  * this indicates that the skb we have been
1568                                  * handed has the 32-bit FCS CRC at the end...
1569                                  * we should react to that by snipping it off
1570                                  * because it will be recomputed and added
1571                                  * on transmission
1572                                  */
1573                                 if (skb->len < (iterator._max_length + FCS_LEN))
1574                                         return false;
1575
1576                                 skb_trim(skb, skb->len - FCS_LEN);
1577                         }
1578                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
1579                                 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
1580                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
1581                                 info->flags &= ~IEEE80211_TX_CTL_DONTFRAG;
1582                         break;
1583
1584                 case IEEE80211_RADIOTAP_TX_FLAGS:
1585                         txflags = get_unaligned_le16(iterator.this_arg);
1586                         if (txflags & IEEE80211_RADIOTAP_F_TX_NOACK)
1587                                 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1588                         break;
1589
1590                 /*
1591                  * Please update the file
1592                  * Documentation/networking/mac80211-injection.txt
1593                  * when parsing new fields here.
1594                  */
1595
1596                 default:
1597                         break;
1598                 }
1599         }
1600
1601         if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
1602                 return false;
1603
1604         /*
1605          * remove the radiotap header
1606          * iterator->_max_length was sanity-checked against
1607          * skb->len by iterator init
1608          */
1609         skb_pull(skb, iterator._max_length);
1610
1611         return true;
1612 }
1613
1614 netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
1615                                          struct net_device *dev)
1616 {
1617         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1618         struct ieee80211_chanctx_conf *chanctx_conf;
1619         struct ieee80211_channel *chan;
1620         struct ieee80211_radiotap_header *prthdr =
1621                 (struct ieee80211_radiotap_header *)skb->data;
1622         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1623         struct ieee80211_hdr *hdr;
1624         struct ieee80211_sub_if_data *tmp_sdata, *sdata;
1625         u16 len_rthdr;
1626         int hdrlen;
1627
1628         /* check for not even having the fixed radiotap header part */
1629         if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1630                 goto fail; /* too short to be possibly valid */
1631
1632         /* is it a header version we can trust to find length from? */
1633         if (unlikely(prthdr->it_version))
1634                 goto fail; /* only version 0 is supported */
1635
1636         /* then there must be a radiotap header with a length we can use */
1637         len_rthdr = ieee80211_get_radiotap_len(skb->data);
1638
1639         /* does the skb contain enough to deliver on the alleged length? */
1640         if (unlikely(skb->len < len_rthdr))
1641                 goto fail; /* skb too short for claimed rt header extent */
1642
1643         /*
1644          * fix up the pointers accounting for the radiotap
1645          * header still being in there.  We are being given
1646          * a precooked IEEE80211 header so no need for
1647          * normal processing
1648          */
1649         skb_set_mac_header(skb, len_rthdr);
1650         /*
1651          * these are just fixed to the end of the rt area since we
1652          * don't have any better information and at this point, nobody cares
1653          */
1654         skb_set_network_header(skb, len_rthdr);
1655         skb_set_transport_header(skb, len_rthdr);
1656
1657         if (skb->len < len_rthdr + 2)
1658                 goto fail;
1659
1660         hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
1661         hdrlen = ieee80211_hdrlen(hdr->frame_control);
1662
1663         if (skb->len < len_rthdr + hdrlen)
1664                 goto fail;
1665
1666         /*
1667          * Initialize skb->protocol if the injected frame is a data frame
1668          * carrying a rfc1042 header
1669          */
1670         if (ieee80211_is_data(hdr->frame_control) &&
1671             skb->len >= len_rthdr + hdrlen + sizeof(rfc1042_header) + 2) {
1672                 u8 *payload = (u8 *)hdr + hdrlen;
1673
1674                 if (ether_addr_equal(payload, rfc1042_header))
1675                         skb->protocol = cpu_to_be16((payload[6] << 8) |
1676                                                     payload[7]);
1677         }
1678
1679         memset(info, 0, sizeof(*info));
1680
1681         info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS |
1682                       IEEE80211_TX_CTL_INJECTED;
1683
1684         /* process and remove the injection radiotap header */
1685         if (!ieee80211_parse_tx_radiotap(skb))
1686                 goto fail;
1687
1688         rcu_read_lock();
1689
1690         /*
1691          * We process outgoing injected frames that have a local address
1692          * we handle as though they are non-injected frames.
1693          * This code here isn't entirely correct, the local MAC address
1694          * isn't always enough to find the interface to use; for proper
1695          * VLAN/WDS support we will need a different mechanism (which
1696          * likely isn't going to be monitor interfaces).
1697          */
1698         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1699
1700         list_for_each_entry_rcu(tmp_sdata, &local->interfaces, list) {
1701                 if (!ieee80211_sdata_running(tmp_sdata))
1702                         continue;
1703                 if (tmp_sdata->vif.type == NL80211_IFTYPE_MONITOR ||
1704                     tmp_sdata->vif.type == NL80211_IFTYPE_AP_VLAN ||
1705                     tmp_sdata->vif.type == NL80211_IFTYPE_WDS)
1706                         continue;
1707                 if (ether_addr_equal(tmp_sdata->vif.addr, hdr->addr2)) {
1708                         sdata = tmp_sdata;
1709                         break;
1710                 }
1711         }
1712
1713         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1714         if (!chanctx_conf) {
1715                 tmp_sdata = rcu_dereference(local->monitor_sdata);
1716                 if (tmp_sdata)
1717                         chanctx_conf =
1718                                 rcu_dereference(tmp_sdata->vif.chanctx_conf);
1719         }
1720
1721         if (chanctx_conf)
1722                 chan = chanctx_conf->def.chan;
1723         else if (!local->use_chanctx)
1724                 chan = local->_oper_chandef.chan;
1725         else
1726                 goto fail_rcu;
1727
1728         /*
1729          * Frame injection is not allowed if beaconing is not allowed
1730          * or if we need radar detection. Beaconing is usually not allowed when
1731          * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
1732          * Passive scan is also used in world regulatory domains where
1733          * your country is not known and as such it should be treated as
1734          * NO TX unless the channel is explicitly allowed in which case
1735          * your current regulatory domain would not have the passive scan
1736          * flag.
1737          *
1738          * Since AP mode uses monitor interfaces to inject/TX management
1739          * frames we can make AP mode the exception to this rule once it
1740          * supports radar detection as its implementation can deal with
1741          * radar detection by itself. We can do that later by adding a
1742          * monitor flag interfaces used for AP support.
1743          */
1744         if ((chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)))
1745                 goto fail_rcu;
1746
1747         ieee80211_xmit(sdata, skb, chan->band);
1748         rcu_read_unlock();
1749
1750         return NETDEV_TX_OK;
1751
1752 fail_rcu:
1753         rcu_read_unlock();
1754 fail:
1755         dev_kfree_skb(skb);
1756         return NETDEV_TX_OK; /* meaning, we dealt with the skb */
1757 }
1758
1759 /*
1760  * Measure Tx frame arrival time for Tx latency statistics calculation
1761  * A single Tx frame latency should be measured from when it is entering the
1762  * Kernel until we receive Tx complete confirmation indication and the skb is
1763  * freed.
1764  */
1765 static void ieee80211_tx_latency_start_msrmnt(struct ieee80211_local *local,
1766                                               struct sk_buff *skb)
1767 {
1768         struct timespec skb_arv;
1769         struct ieee80211_tx_latency_bin_ranges *tx_latency;
1770
1771         tx_latency = rcu_dereference(local->tx_latency);
1772         if (!tx_latency)
1773                 return;
1774
1775         ktime_get_ts(&skb_arv);
1776         skb->tstamp = ktime_set(skb_arv.tv_sec, skb_arv.tv_nsec);
1777 }
1778
1779 /**
1780  * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1781  * subinterfaces (wlan#, WDS, and VLAN interfaces)
1782  * @skb: packet to be sent
1783  * @dev: incoming interface
1784  *
1785  * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1786  * not be freed, and caller is responsible for either retrying later or freeing
1787  * skb).
1788  *
1789  * This function takes in an Ethernet header and encapsulates it with suitable
1790  * IEEE 802.11 header based on which interface the packet is coming in. The
1791  * encapsulated packet will then be passed to master interface, wlan#.11, for
1792  * transmission (through low-level driver).
1793  */
1794 netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
1795                                     struct net_device *dev)
1796 {
1797         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1798         struct ieee80211_local *local = sdata->local;
1799         struct ieee80211_tx_info *info;
1800         int head_need;
1801         u16 ethertype, hdrlen,  meshhdrlen = 0;
1802         __le16 fc;
1803         struct ieee80211_hdr hdr;
1804         struct ieee80211s_hdr mesh_hdr __maybe_unused;
1805         struct mesh_path __maybe_unused *mppath = NULL, *mpath = NULL;
1806         const u8 *encaps_data;
1807         int encaps_len, skip_header_bytes;
1808         int nh_pos, h_pos;
1809         struct sta_info *sta = NULL;
1810         bool wme_sta = false, authorized = false, tdls_auth = false;
1811         bool tdls_direct = false;
1812         bool multicast;
1813         u32 info_flags = 0;
1814         u16 info_id = 0;
1815         struct ieee80211_chanctx_conf *chanctx_conf;
1816         struct ieee80211_sub_if_data *ap_sdata;
1817         enum ieee80211_band band;
1818
1819         if (unlikely(skb->len < ETH_HLEN))
1820                 goto fail;
1821
1822         /* convert Ethernet header to proper 802.11 header (based on
1823          * operation mode) */
1824         ethertype = (skb->data[12] << 8) | skb->data[13];
1825         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
1826
1827         rcu_read_lock();
1828
1829         /* Measure frame arrival for Tx latency statistics calculation */
1830         ieee80211_tx_latency_start_msrmnt(local, skb);
1831
1832         switch (sdata->vif.type) {
1833         case NL80211_IFTYPE_AP_VLAN:
1834                 sta = rcu_dereference(sdata->u.vlan.sta);
1835                 if (sta) {
1836                         fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1837                         /* RA TA DA SA */
1838                         memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
1839                         memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1840                         memcpy(hdr.addr3, skb->data, ETH_ALEN);
1841                         memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1842                         hdrlen = 30;
1843                         authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
1844                         wme_sta = test_sta_flag(sta, WLAN_STA_WME);
1845                 }
1846                 ap_sdata = container_of(sdata->bss, struct ieee80211_sub_if_data,
1847                                         u.ap);
1848                 chanctx_conf = rcu_dereference(ap_sdata->vif.chanctx_conf);
1849                 if (!chanctx_conf)
1850                         goto fail_rcu;
1851                 band = chanctx_conf->def.chan->band;
1852                 if (sta)
1853                         break;
1854                 /* fall through */
1855         case NL80211_IFTYPE_AP:
1856                 if (sdata->vif.type == NL80211_IFTYPE_AP)
1857                         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1858                 if (!chanctx_conf)
1859                         goto fail_rcu;
1860                 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
1861                 /* DA BSSID SA */
1862                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1863                 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1864                 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1865                 hdrlen = 24;
1866                 band = chanctx_conf->def.chan->band;
1867                 break;
1868         case NL80211_IFTYPE_WDS:
1869                 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1870                 /* RA TA DA SA */
1871                 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1872                 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1873                 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1874                 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1875                 hdrlen = 30;
1876                 /*
1877                  * This is the exception! WDS style interfaces are prohibited
1878                  * when channel contexts are in used so this must be valid
1879                  */
1880                 band = local->hw.conf.chandef.chan->band;
1881                 break;
1882 #ifdef CONFIG_MAC80211_MESH
1883         case NL80211_IFTYPE_MESH_POINT:
1884                 if (!is_multicast_ether_addr(skb->data)) {
1885                         struct sta_info *next_hop;
1886                         bool mpp_lookup = true;
1887
1888                         mpath = mesh_path_lookup(sdata, skb->data);
1889                         if (mpath) {
1890                                 mpp_lookup = false;
1891                                 next_hop = rcu_dereference(mpath->next_hop);
1892                                 if (!next_hop ||
1893                                     !(mpath->flags & (MESH_PATH_ACTIVE |
1894                                                       MESH_PATH_RESOLVING)))
1895                                         mpp_lookup = true;
1896                         }
1897
1898                         if (mpp_lookup)
1899                                 mppath = mpp_path_lookup(sdata, skb->data);
1900
1901                         if (mppath && mpath)
1902                                 mesh_path_del(mpath->sdata, mpath->dst);
1903                 }
1904
1905                 /*
1906                  * Use address extension if it is a packet from
1907                  * another interface or if we know the destination
1908                  * is being proxied by a portal (i.e. portal address
1909                  * differs from proxied address)
1910                  */
1911                 if (ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN) &&
1912                     !(mppath && !ether_addr_equal(mppath->mpp, skb->data))) {
1913                         hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
1914                                         skb->data, skb->data + ETH_ALEN);
1915                         meshhdrlen = ieee80211_new_mesh_header(sdata, &mesh_hdr,
1916                                                                NULL, NULL);
1917                 } else {
1918                         /* DS -> MBSS (802.11-2012 13.11.3.3).
1919                          * For unicast with unknown forwarding information,
1920                          * destination might be in the MBSS or if that fails
1921                          * forwarded to another mesh gate. In either case
1922                          * resolution will be handled in ieee80211_xmit(), so
1923                          * leave the original DA. This also works for mcast */
1924                         const u8 *mesh_da = skb->data;
1925
1926                         if (mppath)
1927                                 mesh_da = mppath->mpp;
1928                         else if (mpath)
1929                                 mesh_da = mpath->dst;
1930
1931                         hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
1932                                         mesh_da, sdata->vif.addr);
1933                         if (is_multicast_ether_addr(mesh_da))
1934                                 /* DA TA mSA AE:SA */
1935                                 meshhdrlen = ieee80211_new_mesh_header(
1936                                                 sdata, &mesh_hdr,
1937                                                 skb->data + ETH_ALEN, NULL);
1938                         else
1939                                 /* RA TA mDA mSA AE:DA SA */
1940                                 meshhdrlen = ieee80211_new_mesh_header(
1941                                                 sdata, &mesh_hdr, skb->data,
1942                                                 skb->data + ETH_ALEN);
1943
1944                 }
1945                 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
1946                 if (!chanctx_conf)
1947                         goto fail_rcu;
1948                 band = chanctx_conf->def.chan->band;
1949                 break;
1950 #endif
1951         case NL80211_IFTYPE_STATION:
1952                 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1953                         bool tdls_peer = false;
1954
1955                         sta = sta_info_get(sdata, skb->data);
1956                         if (sta) {
1957                                 authorized = test_sta_flag(sta,
1958                                                         WLAN_STA_AUTHORIZED);
1959                                 wme_sta = test_sta_flag(sta, WLAN_STA_WME);
1960                                 tdls_peer = test_sta_flag(sta,
1961                                                          WLAN_STA_TDLS_PEER);
1962                                 tdls_auth = test_sta_flag(sta,
1963                                                 WLAN_STA_TDLS_PEER_AUTH);
1964                         }
1965
1966                         /*
1967                          * If the TDLS link is enabled, send everything
1968                          * directly. Otherwise, allow TDLS setup frames
1969                          * to be transmitted indirectly.
1970                          */
1971                         tdls_direct = tdls_peer && (tdls_auth ||
1972                                  !(ethertype == ETH_P_TDLS && skb->len > 14 &&
1973                                    skb->data[14] == WLAN_TDLS_SNAP_RFTYPE));
1974                 }
1975
1976                 if (tdls_direct) {
1977                         /* link during setup - throw out frames to peer */
1978                         if (!tdls_auth)
1979                                 goto fail_rcu;
1980
1981                         /* DA SA BSSID */
1982                         memcpy(hdr.addr1, skb->data, ETH_ALEN);
1983                         memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1984                         memcpy(hdr.addr3, sdata->u.mgd.bssid, ETH_ALEN);
1985                         hdrlen = 24;
1986                 }  else if (sdata->u.mgd.use_4addr &&
1987                             cpu_to_be16(ethertype) != sdata->control_port_protocol) {
1988                         fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
1989                                           IEEE80211_FCTL_TODS);
1990                         /* RA TA DA SA */
1991                         memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
1992                         memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1993                         memcpy(hdr.addr3, skb->data, ETH_ALEN);
1994                         memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1995                         hdrlen = 30;
1996                 } else {
1997                         fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
1998                         /* BSSID SA DA */
1999                         memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
2000                         memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2001                         memcpy(hdr.addr3, skb->data, ETH_ALEN);
2002                         hdrlen = 24;
2003                 }
2004                 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2005                 if (!chanctx_conf)
2006                         goto fail_rcu;
2007                 band = chanctx_conf->def.chan->band;
2008                 break;
2009         case NL80211_IFTYPE_ADHOC:
2010                 /* DA SA BSSID */
2011                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2012                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
2013                 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
2014                 hdrlen = 24;
2015                 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2016                 if (!chanctx_conf)
2017                         goto fail_rcu;
2018                 band = chanctx_conf->def.chan->band;
2019                 break;
2020         default:
2021                 goto fail_rcu;
2022         }
2023
2024         /*
2025          * There's no need to try to look up the destination
2026          * if it is a multicast address (which can only happen
2027          * in AP mode)
2028          */
2029         multicast = is_multicast_ether_addr(hdr.addr1);
2030         if (!multicast) {
2031                 sta = sta_info_get(sdata, hdr.addr1);
2032                 if (sta) {
2033                         authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2034                         wme_sta = test_sta_flag(sta, WLAN_STA_WME);
2035                 }
2036         }
2037
2038         /* For mesh, the use of the QoS header is mandatory */
2039         if (ieee80211_vif_is_mesh(&sdata->vif))
2040                 wme_sta = true;
2041
2042         /* receiver and we are QoS enabled, use a QoS type frame */
2043         if (wme_sta && local->hw.queues >= IEEE80211_NUM_ACS) {
2044                 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2045                 hdrlen += 2;
2046         }
2047
2048         /*
2049          * Drop unicast frames to unauthorised stations unless they are
2050          * EAPOL frames from the local station.
2051          */
2052         if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
2053                      !multicast && !authorized &&
2054                      (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
2055                       !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
2056 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
2057                 net_info_ratelimited("%s: dropped frame to %pM (unauthorized port)\n",
2058                                     dev->name, hdr.addr1);
2059 #endif
2060
2061                 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
2062
2063                 goto fail_rcu;
2064         }
2065
2066         if (unlikely(!multicast && skb->sk &&
2067                      skb_shinfo(skb)->tx_flags & SKBTX_WIFI_STATUS)) {
2068                 struct sk_buff *orig_skb = skb;
2069
2070                 skb = skb_clone(skb, GFP_ATOMIC);
2071                 if (skb) {
2072                         unsigned long flags;
2073                         int id;
2074
2075                         spin_lock_irqsave(&local->ack_status_lock, flags);
2076                         id = idr_alloc(&local->ack_status_frames, orig_skb,
2077                                        1, 0x10000, GFP_ATOMIC);
2078                         spin_unlock_irqrestore(&local->ack_status_lock, flags);
2079
2080                         if (id >= 0) {
2081                                 info_id = id;
2082                                 info_flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2083                         } else if (skb_shared(skb)) {
2084                                 kfree_skb(orig_skb);
2085                         } else {
2086                                 kfree_skb(skb);
2087                                 skb = orig_skb;
2088                         }
2089                 } else {
2090                         /* couldn't clone -- lose tx status ... */
2091                         skb = orig_skb;
2092                 }
2093         }
2094
2095         /*
2096          * If the skb is shared we need to obtain our own copy.
2097          */
2098         if (skb_shared(skb)) {
2099                 struct sk_buff *tmp_skb = skb;
2100
2101                 /* can't happen -- skb is a clone if info_id != 0 */
2102                 WARN_ON(info_id);
2103
2104                 skb = skb_clone(skb, GFP_ATOMIC);
2105                 kfree_skb(tmp_skb);
2106
2107                 if (!skb)
2108                         goto fail_rcu;
2109         }
2110
2111         hdr.frame_control = fc;
2112         hdr.duration_id = 0;
2113         hdr.seq_ctrl = 0;
2114
2115         skip_header_bytes = ETH_HLEN;
2116         if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
2117                 encaps_data = bridge_tunnel_header;
2118                 encaps_len = sizeof(bridge_tunnel_header);
2119                 skip_header_bytes -= 2;
2120         } else if (ethertype >= ETH_P_802_3_MIN) {
2121                 encaps_data = rfc1042_header;
2122                 encaps_len = sizeof(rfc1042_header);
2123                 skip_header_bytes -= 2;
2124         } else {
2125                 encaps_data = NULL;
2126                 encaps_len = 0;
2127         }
2128
2129         nh_pos = skb_network_header(skb) - skb->data;
2130         h_pos = skb_transport_header(skb) - skb->data;
2131
2132         skb_pull(skb, skip_header_bytes);
2133         nh_pos -= skip_header_bytes;
2134         h_pos -= skip_header_bytes;
2135
2136         head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
2137
2138         /*
2139          * So we need to modify the skb header and hence need a copy of
2140          * that. The head_need variable above doesn't, so far, include
2141          * the needed header space that we don't need right away. If we
2142          * can, then we don't reallocate right now but only after the
2143          * frame arrives at the master device (if it does...)
2144          *
2145          * If we cannot, however, then we will reallocate to include all
2146          * the ever needed space. Also, if we need to reallocate it anyway,
2147          * make it big enough for everything we may ever need.
2148          */
2149
2150         if (head_need > 0 || skb_cloned(skb)) {
2151                 head_need += sdata->encrypt_headroom;
2152                 head_need += local->tx_headroom;
2153                 head_need = max_t(int, 0, head_need);
2154                 if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
2155                         ieee80211_free_txskb(&local->hw, skb);
2156                         skb = NULL;
2157                         goto fail_rcu;
2158                 }
2159         }
2160
2161         if (encaps_data) {
2162                 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
2163                 nh_pos += encaps_len;
2164                 h_pos += encaps_len;
2165         }
2166
2167 #ifdef CONFIG_MAC80211_MESH
2168         if (meshhdrlen > 0) {
2169                 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
2170                 nh_pos += meshhdrlen;
2171                 h_pos += meshhdrlen;
2172         }
2173 #endif
2174
2175         if (ieee80211_is_data_qos(fc)) {
2176                 __le16 *qos_control;
2177
2178                 qos_control = (__le16 *) skb_push(skb, 2);
2179                 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
2180                 /*
2181                  * Maybe we could actually set some fields here, for now just
2182                  * initialise to zero to indicate no special operation.
2183                  */
2184                 *qos_control = 0;
2185         } else
2186                 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
2187
2188         nh_pos += hdrlen;
2189         h_pos += hdrlen;
2190
2191         dev->stats.tx_packets++;
2192         dev->stats.tx_bytes += skb->len;
2193
2194         /* Update skb pointers to various headers since this modified frame
2195          * is going to go through Linux networking code that may potentially
2196          * need things like pointer to IP header. */
2197         skb_set_mac_header(skb, 0);
2198         skb_set_network_header(skb, nh_pos);
2199         skb_set_transport_header(skb, h_pos);
2200
2201         info = IEEE80211_SKB_CB(skb);
2202         memset(info, 0, sizeof(*info));
2203
2204         dev->trans_start = jiffies;
2205
2206         info->flags = info_flags;
2207         info->ack_frame_id = info_id;
2208
2209         ieee80211_xmit(sdata, skb, band);
2210         rcu_read_unlock();
2211
2212         return NETDEV_TX_OK;
2213
2214  fail_rcu:
2215         rcu_read_unlock();
2216  fail:
2217         dev_kfree_skb(skb);
2218         return NETDEV_TX_OK;
2219 }
2220
2221
2222 /*
2223  * ieee80211_clear_tx_pending may not be called in a context where
2224  * it is possible that it packets could come in again.
2225  */
2226 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
2227 {
2228         struct sk_buff *skb;
2229         int i;
2230
2231         for (i = 0; i < local->hw.queues; i++) {
2232                 while ((skb = skb_dequeue(&local->pending[i])) != NULL)
2233                         ieee80211_free_txskb(&local->hw, skb);
2234         }
2235 }
2236
2237 /*
2238  * Returns false if the frame couldn't be transmitted but was queued instead,
2239  * which in this case means re-queued -- take as an indication to stop sending
2240  * more pending frames.
2241  */
2242 static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
2243                                      struct sk_buff *skb)
2244 {
2245         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2246         struct ieee80211_sub_if_data *sdata;
2247         struct sta_info *sta;
2248         struct ieee80211_hdr *hdr;
2249         bool result;
2250         struct ieee80211_chanctx_conf *chanctx_conf;
2251
2252         sdata = vif_to_sdata(info->control.vif);
2253
2254         if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
2255                 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2256                 if (unlikely(!chanctx_conf)) {
2257                         dev_kfree_skb(skb);
2258                         return true;
2259                 }
2260                 result = ieee80211_tx(sdata, skb, true,
2261                                       chanctx_conf->def.chan->band);
2262         } else {
2263                 struct sk_buff_head skbs;
2264
2265                 __skb_queue_head_init(&skbs);
2266                 __skb_queue_tail(&skbs, skb);
2267
2268                 hdr = (struct ieee80211_hdr *)skb->data;
2269                 sta = sta_info_get(sdata, hdr->addr1);
2270
2271                 result = __ieee80211_tx(local, &skbs, skb->len, sta, true);
2272         }
2273
2274         return result;
2275 }
2276
2277 /*
2278  * Transmit all pending packets. Called from tasklet.
2279  */
2280 void ieee80211_tx_pending(unsigned long data)
2281 {
2282         struct ieee80211_local *local = (struct ieee80211_local *)data;
2283         unsigned long flags;
2284         int i;
2285         bool txok;
2286
2287         rcu_read_lock();
2288
2289         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
2290         for (i = 0; i < local->hw.queues; i++) {
2291                 /*
2292                  * If queue is stopped by something other than due to pending
2293                  * frames, or we have no pending frames, proceed to next queue.
2294                  */
2295                 if (local->queue_stop_reasons[i] ||
2296                     skb_queue_empty(&local->pending[i]))
2297                         continue;
2298
2299                 while (!skb_queue_empty(&local->pending[i])) {
2300                         struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
2301                         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2302
2303                         if (WARN_ON(!info->control.vif)) {
2304                                 ieee80211_free_txskb(&local->hw, skb);
2305                                 continue;
2306                         }
2307
2308                         spin_unlock_irqrestore(&local->queue_stop_reason_lock,
2309                                                 flags);
2310
2311                         txok = ieee80211_tx_pending_skb(local, skb);
2312                         spin_lock_irqsave(&local->queue_stop_reason_lock,
2313                                           flags);
2314                         if (!txok)
2315                                 break;
2316                 }
2317
2318                 if (skb_queue_empty(&local->pending[i]))
2319                         ieee80211_propagate_queue_wake(local, i);
2320         }
2321         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
2322
2323         rcu_read_unlock();
2324 }
2325
2326 /* functions for drivers to get certain frames */
2327
2328 static void __ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
2329                                        struct ps_data *ps, struct sk_buff *skb,
2330                                        bool is_template)
2331 {
2332         u8 *pos, *tim;
2333         int aid0 = 0;
2334         int i, have_bits = 0, n1, n2;
2335
2336         /* Generate bitmap for TIM only if there are any STAs in power save
2337          * mode. */
2338         if (atomic_read(&ps->num_sta_ps) > 0)
2339                 /* in the hope that this is faster than
2340                  * checking byte-for-byte */
2341                 have_bits = !bitmap_empty((unsigned long *)ps->tim,
2342                                           IEEE80211_MAX_AID+1);
2343         if (!is_template) {
2344                 if (ps->dtim_count == 0)
2345                         ps->dtim_count = sdata->vif.bss_conf.dtim_period - 1;
2346                 else
2347                         ps->dtim_count--;
2348         }
2349
2350         tim = pos = (u8 *) skb_put(skb, 6);
2351         *pos++ = WLAN_EID_TIM;
2352         *pos++ = 4;
2353         *pos++ = ps->dtim_count;
2354         *pos++ = sdata->vif.bss_conf.dtim_period;
2355
2356         if (ps->dtim_count == 0 && !skb_queue_empty(&ps->bc_buf))
2357                 aid0 = 1;
2358
2359         ps->dtim_bc_mc = aid0 == 1;
2360
2361         if (have_bits) {
2362                 /* Find largest even number N1 so that bits numbered 1 through
2363                  * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
2364                  * (N2 + 1) x 8 through 2007 are 0. */
2365                 n1 = 0;
2366                 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
2367                         if (ps->tim[i]) {
2368                                 n1 = i & 0xfe;
2369                                 break;
2370                         }
2371                 }
2372                 n2 = n1;
2373                 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
2374                         if (ps->tim[i]) {
2375                                 n2 = i;
2376                                 break;
2377                         }
2378                 }
2379
2380                 /* Bitmap control */
2381                 *pos++ = n1 | aid0;
2382                 /* Part Virt Bitmap */
2383                 skb_put(skb, n2 - n1);
2384                 memcpy(pos, ps->tim + n1, n2 - n1 + 1);
2385
2386                 tim[1] = n2 - n1 + 4;
2387         } else {
2388                 *pos++ = aid0; /* Bitmap control */
2389                 *pos++ = 0; /* Part Virt Bitmap */
2390         }
2391 }
2392
2393 static int ieee80211_beacon_add_tim(struct ieee80211_sub_if_data *sdata,
2394                                     struct ps_data *ps, struct sk_buff *skb,
2395                                     bool is_template)
2396 {
2397         struct ieee80211_local *local = sdata->local;
2398
2399         /*
2400          * Not very nice, but we want to allow the driver to call
2401          * ieee80211_beacon_get() as a response to the set_tim()
2402          * callback. That, however, is already invoked under the
2403          * sta_lock to guarantee consistent and race-free update
2404          * of the tim bitmap in mac80211 and the driver.
2405          */
2406         if (local->tim_in_locked_section) {
2407                 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
2408         } else {
2409                 spin_lock_bh(&local->tim_lock);
2410                 __ieee80211_beacon_add_tim(sdata, ps, skb, is_template);
2411                 spin_unlock_bh(&local->tim_lock);
2412         }
2413
2414         return 0;
2415 }
2416
2417 static void ieee80211_set_csa(struct ieee80211_sub_if_data *sdata,
2418                               struct beacon_data *beacon)
2419 {
2420         struct probe_resp *resp;
2421         u8 *beacon_data;
2422         size_t beacon_data_len;
2423         int i;
2424         u8 count = sdata->csa_current_counter;
2425
2426         switch (sdata->vif.type) {
2427         case NL80211_IFTYPE_AP:
2428                 beacon_data = beacon->tail;
2429                 beacon_data_len = beacon->tail_len;
2430                 break;
2431         case NL80211_IFTYPE_ADHOC:
2432                 beacon_data = beacon->head;
2433                 beacon_data_len = beacon->head_len;
2434                 break;
2435         case NL80211_IFTYPE_MESH_POINT:
2436                 beacon_data = beacon->head;
2437                 beacon_data_len = beacon->head_len;
2438                 break;
2439         default:
2440                 return;
2441         }
2442
2443         for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; ++i) {
2444                 u16 counter_offset_beacon =
2445                         sdata->csa_counter_offset_beacon[i];
2446                 u16 counter_offset_presp = sdata->csa_counter_offset_presp[i];
2447
2448                 if (counter_offset_beacon) {
2449                         if (WARN_ON(counter_offset_beacon >= beacon_data_len))
2450                                 return;
2451
2452                         beacon_data[counter_offset_beacon] = count;
2453                 }
2454
2455                 if (sdata->vif.type == NL80211_IFTYPE_AP &&
2456                     counter_offset_presp) {
2457                         rcu_read_lock();
2458                         resp = rcu_dereference(sdata->u.ap.probe_resp);
2459
2460                         /* If nl80211 accepted the offset, this should
2461                          * not happen.
2462                          */
2463                         if (WARN_ON(!resp)) {
2464                                 rcu_read_unlock();
2465                                 return;
2466                         }
2467                         resp->data[counter_offset_presp] = count;
2468                         rcu_read_unlock();
2469                 }
2470         }
2471 }
2472
2473 u8 ieee80211_csa_update_counter(struct ieee80211_vif *vif)
2474 {
2475         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2476
2477         sdata->csa_current_counter--;
2478
2479         /* the counter should never reach 0 */
2480         WARN_ON(!sdata->csa_current_counter);
2481
2482         return sdata->csa_current_counter;
2483 }
2484 EXPORT_SYMBOL(ieee80211_csa_update_counter);
2485
2486 bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
2487 {
2488         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2489         struct beacon_data *beacon = NULL;
2490         u8 *beacon_data;
2491         size_t beacon_data_len;
2492         int counter_beacon = sdata->csa_counter_offset_beacon[0];
2493         int ret = false;
2494
2495         if (!ieee80211_sdata_running(sdata))
2496                 return false;
2497
2498         rcu_read_lock();
2499         if (vif->type == NL80211_IFTYPE_AP) {
2500                 struct ieee80211_if_ap *ap = &sdata->u.ap;
2501
2502                 beacon = rcu_dereference(ap->beacon);
2503                 if (WARN_ON(!beacon || !beacon->tail))
2504                         goto out;
2505                 beacon_data = beacon->tail;
2506                 beacon_data_len = beacon->tail_len;
2507         } else if (vif->type == NL80211_IFTYPE_ADHOC) {
2508                 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2509
2510                 beacon = rcu_dereference(ifibss->presp);
2511                 if (!beacon)
2512                         goto out;
2513
2514                 beacon_data = beacon->head;
2515                 beacon_data_len = beacon->head_len;
2516         } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
2517                 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2518
2519                 beacon = rcu_dereference(ifmsh->beacon);
2520                 if (!beacon)
2521                         goto out;
2522
2523                 beacon_data = beacon->head;
2524                 beacon_data_len = beacon->head_len;
2525         } else {
2526                 WARN_ON(1);
2527                 goto out;
2528         }
2529
2530         if (WARN_ON(counter_beacon > beacon_data_len))
2531                 goto out;
2532
2533         if (beacon_data[counter_beacon] == 1)
2534                 ret = true;
2535  out:
2536         rcu_read_unlock();
2537
2538         return ret;
2539 }
2540 EXPORT_SYMBOL(ieee80211_csa_is_complete);
2541
2542 static struct sk_buff *
2543 __ieee80211_beacon_get(struct ieee80211_hw *hw,
2544                        struct ieee80211_vif *vif,
2545                        struct ieee80211_mutable_offsets *offs,
2546                        bool is_template)
2547 {
2548         struct ieee80211_local *local = hw_to_local(hw);
2549         struct sk_buff *skb = NULL;
2550         struct ieee80211_tx_info *info;
2551         struct ieee80211_sub_if_data *sdata = NULL;
2552         enum ieee80211_band band;
2553         struct ieee80211_tx_rate_control txrc;
2554         struct ieee80211_chanctx_conf *chanctx_conf;
2555         int csa_off_base = 0;
2556
2557         rcu_read_lock();
2558
2559         sdata = vif_to_sdata(vif);
2560         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2561
2562         if (!ieee80211_sdata_running(sdata) || !chanctx_conf)
2563                 goto out;
2564
2565         if (offs)
2566                 memset(offs, 0, sizeof(*offs));
2567
2568         if (sdata->vif.type == NL80211_IFTYPE_AP) {
2569                 struct ieee80211_if_ap *ap = &sdata->u.ap;
2570                 struct beacon_data *beacon = rcu_dereference(ap->beacon);
2571
2572                 if (beacon) {
2573                         if (sdata->vif.csa_active) {
2574                                 if (!is_template)
2575                                         ieee80211_csa_update_counter(vif);
2576
2577                                 ieee80211_set_csa(sdata, beacon);
2578                         }
2579
2580                         /*
2581                          * headroom, head length,
2582                          * tail length and maximum TIM length
2583                          */
2584                         skb = dev_alloc_skb(local->tx_headroom +
2585                                             beacon->head_len +
2586                                             beacon->tail_len + 256 +
2587                                             local->hw.extra_beacon_tailroom);
2588                         if (!skb)
2589                                 goto out;
2590
2591                         skb_reserve(skb, local->tx_headroom);
2592                         memcpy(skb_put(skb, beacon->head_len), beacon->head,
2593                                beacon->head_len);
2594
2595                         ieee80211_beacon_add_tim(sdata, &ap->ps, skb,
2596                                                  is_template);
2597
2598                         if (offs) {
2599                                 offs->tim_offset = beacon->head_len;
2600                                 offs->tim_length = skb->len - beacon->head_len;
2601
2602                                 /* for AP the csa offsets are from tail */
2603                                 csa_off_base = skb->len;
2604                         }
2605
2606                         if (beacon->tail)
2607                                 memcpy(skb_put(skb, beacon->tail_len),
2608                                        beacon->tail, beacon->tail_len);
2609                 } else
2610                         goto out;
2611         } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
2612                 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2613                 struct ieee80211_hdr *hdr;
2614                 struct beacon_data *presp = rcu_dereference(ifibss->presp);
2615
2616                 if (!presp)
2617                         goto out;
2618
2619                 if (sdata->vif.csa_active) {
2620                         if (!is_template)
2621                                 ieee80211_csa_update_counter(vif);
2622
2623                         ieee80211_set_csa(sdata, presp);
2624                 }
2625
2626                 skb = dev_alloc_skb(local->tx_headroom + presp->head_len +
2627                                     local->hw.extra_beacon_tailroom);
2628                 if (!skb)
2629                         goto out;
2630                 skb_reserve(skb, local->tx_headroom);
2631                 memcpy(skb_put(skb, presp->head_len), presp->head,
2632                        presp->head_len);
2633
2634                 hdr = (struct ieee80211_hdr *) skb->data;
2635                 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2636                                                  IEEE80211_STYPE_BEACON);
2637         } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2638                 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2639                 struct beacon_data *bcn = rcu_dereference(ifmsh->beacon);
2640
2641                 if (!bcn)
2642                         goto out;
2643
2644                 if (sdata->vif.csa_active) {
2645                         if (!is_template)
2646                                 /* TODO: For mesh csa_counter is in TU, so
2647                                  * decrementing it by one isn't correct, but
2648                                  * for now we leave it consistent with overall
2649                                  * mac80211's behavior.
2650                                  */
2651                                 ieee80211_csa_update_counter(vif);
2652
2653                         ieee80211_set_csa(sdata, bcn);
2654                 }
2655
2656                 if (ifmsh->sync_ops)
2657                         ifmsh->sync_ops->adjust_tbtt(sdata, bcn);
2658
2659                 skb = dev_alloc_skb(local->tx_headroom +
2660                                     bcn->head_len +
2661                                     256 + /* TIM IE */
2662                                     bcn->tail_len +
2663                                     local->hw.extra_beacon_tailroom);
2664                 if (!skb)
2665                         goto out;
2666                 skb_reserve(skb, local->tx_headroom);
2667                 memcpy(skb_put(skb, bcn->head_len), bcn->head, bcn->head_len);
2668                 ieee80211_beacon_add_tim(sdata, &ifmsh->ps, skb, is_template);
2669
2670                 if (offs) {
2671                         offs->tim_offset = bcn->head_len;
2672                         offs->tim_length = skb->len - bcn->head_len;
2673                 }
2674
2675                 memcpy(skb_put(skb, bcn->tail_len), bcn->tail, bcn->tail_len);
2676         } else {
2677                 WARN_ON(1);
2678                 goto out;
2679         }
2680
2681         /* CSA offsets */
2682         if (offs) {
2683                 int i;
2684
2685                 for (i = 0; i < IEEE80211_MAX_CSA_COUNTERS_NUM; i++) {
2686                         u16 csa_off = sdata->csa_counter_offset_beacon[i];
2687
2688                         if (!csa_off)
2689                                 continue;
2690
2691                         offs->csa_counter_offs[i] = csa_off_base + csa_off;
2692                 }
2693         }
2694
2695         band = chanctx_conf->def.chan->band;
2696
2697         info = IEEE80211_SKB_CB(skb);
2698
2699         info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
2700         info->flags |= IEEE80211_TX_CTL_NO_ACK;
2701         info->band = band;
2702
2703         memset(&txrc, 0, sizeof(txrc));
2704         txrc.hw = hw;
2705         txrc.sband = local->hw.wiphy->bands[band];
2706         txrc.bss_conf = &sdata->vif.bss_conf;
2707         txrc.skb = skb;
2708         txrc.reported_rate.idx = -1;
2709         txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
2710         if (txrc.rate_idx_mask == (1 << txrc.sband->n_bitrates) - 1)
2711                 txrc.max_rate_idx = -1;
2712         else
2713                 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
2714         txrc.bss = true;
2715         rate_control_get_rate(sdata, NULL, &txrc);
2716
2717         info->control.vif = vif;
2718
2719         info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
2720                         IEEE80211_TX_CTL_ASSIGN_SEQ |
2721                         IEEE80211_TX_CTL_FIRST_FRAGMENT;
2722  out:
2723         rcu_read_unlock();
2724         return skb;
2725
2726 }
2727
2728 struct sk_buff *
2729 ieee80211_beacon_get_template(struct ieee80211_hw *hw,
2730                               struct ieee80211_vif *vif,
2731                               struct ieee80211_mutable_offsets *offs)
2732 {
2733         return __ieee80211_beacon_get(hw, vif, offs, true);
2734 }
2735 EXPORT_SYMBOL(ieee80211_beacon_get_template);
2736
2737 struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
2738                                          struct ieee80211_vif *vif,
2739                                          u16 *tim_offset, u16 *tim_length)
2740 {
2741         struct ieee80211_mutable_offsets offs = {};
2742         struct sk_buff *bcn = __ieee80211_beacon_get(hw, vif, &offs, false);
2743
2744         if (tim_offset)
2745                 *tim_offset = offs.tim_offset;
2746
2747         if (tim_length)
2748                 *tim_length = offs.tim_length;
2749
2750         return bcn;
2751 }
2752 EXPORT_SYMBOL(ieee80211_beacon_get_tim);
2753
2754 struct sk_buff *ieee80211_proberesp_get(struct ieee80211_hw *hw,
2755                                         struct ieee80211_vif *vif)
2756 {
2757         struct ieee80211_if_ap *ap = NULL;
2758         struct sk_buff *skb = NULL;
2759         struct probe_resp *presp = NULL;
2760         struct ieee80211_hdr *hdr;
2761         struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
2762
2763         if (sdata->vif.type != NL80211_IFTYPE_AP)
2764                 return NULL;
2765
2766         rcu_read_lock();
2767
2768         ap = &sdata->u.ap;
2769         presp = rcu_dereference(ap->probe_resp);
2770         if (!presp)
2771                 goto out;
2772
2773         skb = dev_alloc_skb(presp->len);
2774         if (!skb)
2775                 goto out;
2776
2777         memcpy(skb_put(skb, presp->len), presp->data, presp->len);
2778
2779         hdr = (struct ieee80211_hdr *) skb->data;
2780         memset(hdr->addr1, 0, sizeof(hdr->addr1));
2781
2782 out:
2783         rcu_read_unlock();
2784         return skb;
2785 }
2786 EXPORT_SYMBOL(ieee80211_proberesp_get);
2787
2788 struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
2789                                      struct ieee80211_vif *vif)
2790 {
2791         struct ieee80211_sub_if_data *sdata;
2792         struct ieee80211_if_managed *ifmgd;
2793         struct ieee80211_pspoll *pspoll;
2794         struct ieee80211_local *local;
2795         struct sk_buff *skb;
2796
2797         if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2798                 return NULL;
2799
2800         sdata = vif_to_sdata(vif);
2801         ifmgd = &sdata->u.mgd;
2802         local = sdata->local;
2803
2804         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
2805         if (!skb)
2806                 return NULL;
2807
2808         skb_reserve(skb, local->hw.extra_tx_headroom);
2809
2810         pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
2811         memset(pspoll, 0, sizeof(*pspoll));
2812         pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
2813                                             IEEE80211_STYPE_PSPOLL);
2814         pspoll->aid = cpu_to_le16(ifmgd->aid);
2815
2816         /* aid in PS-Poll has its two MSBs each set to 1 */
2817         pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
2818
2819         memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
2820         memcpy(pspoll->ta, vif->addr, ETH_ALEN);
2821
2822         return skb;
2823 }
2824 EXPORT_SYMBOL(ieee80211_pspoll_get);
2825
2826 struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
2827                                        struct ieee80211_vif *vif)
2828 {
2829         struct ieee80211_hdr_3addr *nullfunc;
2830         struct ieee80211_sub_if_data *sdata;
2831         struct ieee80211_if_managed *ifmgd;
2832         struct ieee80211_local *local;
2833         struct sk_buff *skb;
2834
2835         if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2836                 return NULL;
2837
2838         sdata = vif_to_sdata(vif);
2839         ifmgd = &sdata->u.mgd;
2840         local = sdata->local;
2841
2842         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*nullfunc));
2843         if (!skb)
2844                 return NULL;
2845
2846         skb_reserve(skb, local->hw.extra_tx_headroom);
2847
2848         nullfunc = (struct ieee80211_hdr_3addr *) skb_put(skb,
2849                                                           sizeof(*nullfunc));
2850         memset(nullfunc, 0, sizeof(*nullfunc));
2851         nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
2852                                               IEEE80211_STYPE_NULLFUNC |
2853                                               IEEE80211_FCTL_TODS);
2854         memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
2855         memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
2856         memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
2857
2858         return skb;
2859 }
2860 EXPORT_SYMBOL(ieee80211_nullfunc_get);
2861
2862 struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
2863                                        struct ieee80211_vif *vif,
2864                                        const u8 *ssid, size_t ssid_len,
2865                                        size_t tailroom)
2866 {
2867         struct ieee80211_sub_if_data *sdata;
2868         struct ieee80211_local *local;
2869         struct ieee80211_hdr_3addr *hdr;
2870         struct sk_buff *skb;
2871         size_t ie_ssid_len;
2872         u8 *pos;
2873
2874         sdata = vif_to_sdata(vif);
2875         local = sdata->local;
2876         ie_ssid_len = 2 + ssid_len;
2877
2878         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
2879                             ie_ssid_len + tailroom);
2880         if (!skb)
2881                 return NULL;
2882
2883         skb_reserve(skb, local->hw.extra_tx_headroom);
2884
2885         hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
2886         memset(hdr, 0, sizeof(*hdr));
2887         hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2888                                          IEEE80211_STYPE_PROBE_REQ);
2889         eth_broadcast_addr(hdr->addr1);
2890         memcpy(hdr->addr2, vif->addr, ETH_ALEN);
2891         eth_broadcast_addr(hdr->addr3);
2892
2893         pos = skb_put(skb, ie_ssid_len);
2894         *pos++ = WLAN_EID_SSID;
2895         *pos++ = ssid_len;
2896         if (ssid_len)
2897                 memcpy(pos, ssid, ssid_len);
2898         pos += ssid_len;
2899
2900         return skb;
2901 }
2902 EXPORT_SYMBOL(ieee80211_probereq_get);
2903
2904 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2905                        const void *frame, size_t frame_len,
2906                        const struct ieee80211_tx_info *frame_txctl,
2907                        struct ieee80211_rts *rts)
2908 {
2909         const struct ieee80211_hdr *hdr = frame;
2910
2911         rts->frame_control =
2912             cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
2913         rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
2914                                                frame_txctl);
2915         memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
2916         memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
2917 }
2918 EXPORT_SYMBOL(ieee80211_rts_get);
2919
2920 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2921                              const void *frame, size_t frame_len,
2922                              const struct ieee80211_tx_info *frame_txctl,
2923                              struct ieee80211_cts *cts)
2924 {
2925         const struct ieee80211_hdr *hdr = frame;
2926
2927         cts->frame_control =
2928             cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
2929         cts->duration = ieee80211_ctstoself_duration(hw, vif,
2930                                                      frame_len, frame_txctl);
2931         memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
2932 }
2933 EXPORT_SYMBOL(ieee80211_ctstoself_get);
2934
2935 struct sk_buff *
2936 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
2937                           struct ieee80211_vif *vif)
2938 {
2939         struct ieee80211_local *local = hw_to_local(hw);
2940         struct sk_buff *skb = NULL;
2941         struct ieee80211_tx_data tx;
2942         struct ieee80211_sub_if_data *sdata;
2943         struct ps_data *ps;
2944         struct ieee80211_tx_info *info;
2945         struct ieee80211_chanctx_conf *chanctx_conf;
2946
2947         sdata = vif_to_sdata(vif);
2948
2949         rcu_read_lock();
2950         chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
2951
2952         if (!chanctx_conf)
2953                 goto out;
2954
2955         if (sdata->vif.type == NL80211_IFTYPE_AP) {
2956                 struct beacon_data *beacon =
2957                                 rcu_dereference(sdata->u.ap.beacon);
2958
2959                 if (!beacon || !beacon->head)
2960                         goto out;
2961
2962                 ps = &sdata->u.ap.ps;
2963         } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2964                 ps = &sdata->u.mesh.ps;
2965         } else {
2966                 goto out;
2967         }
2968
2969         if (ps->dtim_count != 0 || !ps->dtim_bc_mc)
2970                 goto out; /* send buffered bc/mc only after DTIM beacon */
2971
2972         while (1) {
2973                 skb = skb_dequeue(&ps->bc_buf);
2974                 if (!skb)
2975                         goto out;
2976                 local->total_ps_buffered--;
2977
2978                 if (!skb_queue_empty(&ps->bc_buf) && skb->len >= 2) {
2979                         struct ieee80211_hdr *hdr =
2980                                 (struct ieee80211_hdr *) skb->data;
2981                         /* more buffered multicast/broadcast frames ==> set
2982                          * MoreData flag in IEEE 802.11 header to inform PS
2983                          * STAs */
2984                         hdr->frame_control |=
2985                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2986                 }
2987
2988                 if (sdata->vif.type == NL80211_IFTYPE_AP)
2989                         sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
2990                 if (!ieee80211_tx_prepare(sdata, &tx, skb))
2991                         break;
2992                 ieee80211_free_txskb(hw, skb);
2993         }
2994
2995         info = IEEE80211_SKB_CB(skb);
2996
2997         tx.flags |= IEEE80211_TX_PS_BUFFERED;
2998         info->band = chanctx_conf->def.chan->band;
2999
3000         if (invoke_tx_handlers(&tx))
3001                 skb = NULL;
3002  out:
3003         rcu_read_unlock();
3004
3005         return skb;
3006 }
3007 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
3008
3009 void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
3010                                  struct sk_buff *skb, int tid,
3011                                  enum ieee80211_band band)
3012 {
3013         int ac = ieee802_1d_to_ac[tid & 7];
3014
3015         skb_set_mac_header(skb, 0);
3016         skb_set_network_header(skb, 0);
3017         skb_set_transport_header(skb, 0);
3018
3019         skb_set_queue_mapping(skb, ac);
3020         skb->priority = tid;
3021
3022         skb->dev = sdata->dev;
3023
3024         /*
3025          * The other path calling ieee80211_xmit is from the tasklet,
3026          * and while we can handle concurrent transmissions locking
3027          * requirements are that we do not come into tx with bhs on.
3028          */
3029         local_bh_disable();
3030         ieee80211_xmit(sdata, skb, band);
3031         local_bh_enable();
3032 }