~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/mac80211/tx.c

Version: ~ [ linux-5.12-rc7 ] ~ [ linux-5.11.13 ] ~ [ linux-5.10.29 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.111 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.186 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.230 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.266 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.266 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp