mac80211: create tx handler for dynamic ps
[safe/jmp/linux-2.6] / net / mac80211 / tx.c
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2005-2006, Devicescape Software, Inc.
4  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
5  * Copyright 2007       Johannes Berg <johannes@sipsolutions.net>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  *
12  * Transmit and frame generation functions.
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/skbuff.h>
18 #include <linux/etherdevice.h>
19 #include <linux/bitmap.h>
20 #include <linux/rcupdate.h>
21 #include <net/net_namespace.h>
22 #include <net/ieee80211_radiotap.h>
23 #include <net/cfg80211.h>
24 #include <net/mac80211.h>
25 #include <asm/unaligned.h>
26
27 #include "ieee80211_i.h"
28 #include "driver-ops.h"
29 #include "led.h"
30 #include "mesh.h"
31 #include "wep.h"
32 #include "wpa.h"
33 #include "wme.h"
34 #include "rate.h"
35
36 #define IEEE80211_TX_OK         0
37 #define IEEE80211_TX_AGAIN      1
38 #define IEEE80211_TX_PENDING    2
39
40 /* misc utils */
41
42 static __le16 ieee80211_duration(struct ieee80211_tx_data *tx, int group_addr,
43                                  int next_frag_len)
44 {
45         int rate, mrate, erp, dur, i;
46         struct ieee80211_rate *txrate;
47         struct ieee80211_local *local = tx->local;
48         struct ieee80211_supported_band *sband;
49         struct ieee80211_hdr *hdr;
50         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
51
52         /* assume HW handles this */
53         if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
54                 return 0;
55
56         /* uh huh? */
57         if (WARN_ON_ONCE(info->control.rates[0].idx < 0))
58                 return 0;
59
60         sband = local->hw.wiphy->bands[tx->channel->band];
61         txrate = &sband->bitrates[info->control.rates[0].idx];
62
63         erp = txrate->flags & IEEE80211_RATE_ERP_G;
64
65         /*
66          * data and mgmt (except PS Poll):
67          * - during CFP: 32768
68          * - during contention period:
69          *   if addr1 is group address: 0
70          *   if more fragments = 0 and addr1 is individual address: time to
71          *      transmit one ACK plus SIFS
72          *   if more fragments = 1 and addr1 is individual address: time to
73          *      transmit next fragment plus 2 x ACK plus 3 x SIFS
74          *
75          * IEEE 802.11, 9.6:
76          * - control response frame (CTS or ACK) shall be transmitted using the
77          *   same rate as the immediately previous frame in the frame exchange
78          *   sequence, if this rate belongs to the PHY mandatory rates, or else
79          *   at the highest possible rate belonging to the PHY rates in the
80          *   BSSBasicRateSet
81          */
82         hdr = (struct ieee80211_hdr *)tx->skb->data;
83         if (ieee80211_is_ctl(hdr->frame_control)) {
84                 /* TODO: These control frames are not currently sent by
85                  * mac80211, but should they be implemented, this function
86                  * needs to be updated to support duration field calculation.
87                  *
88                  * RTS: time needed to transmit pending data/mgmt frame plus
89                  *    one CTS frame plus one ACK frame plus 3 x SIFS
90                  * CTS: duration of immediately previous RTS minus time
91                  *    required to transmit CTS and its SIFS
92                  * ACK: 0 if immediately previous directed data/mgmt had
93                  *    more=0, with more=1 duration in ACK frame is duration
94                  *    from previous frame minus time needed to transmit ACK
95                  *    and its SIFS
96                  * PS Poll: BIT(15) | BIT(14) | aid
97                  */
98                 return 0;
99         }
100
101         /* data/mgmt */
102         if (0 /* FIX: data/mgmt during CFP */)
103                 return cpu_to_le16(32768);
104
105         if (group_addr) /* Group address as the destination - no ACK */
106                 return 0;
107
108         /* Individual destination address:
109          * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
110          * CTS and ACK frames shall be transmitted using the highest rate in
111          * basic rate set that is less than or equal to the rate of the
112          * immediately previous frame and that is using the same modulation
113          * (CCK or OFDM). If no basic rate set matches with these requirements,
114          * the highest mandatory rate of the PHY that is less than or equal to
115          * the rate of the previous frame is used.
116          * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
117          */
118         rate = -1;
119         /* use lowest available if everything fails */
120         mrate = sband->bitrates[0].bitrate;
121         for (i = 0; i < sband->n_bitrates; i++) {
122                 struct ieee80211_rate *r = &sband->bitrates[i];
123
124                 if (r->bitrate > txrate->bitrate)
125                         break;
126
127                 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
128                         rate = r->bitrate;
129
130                 switch (sband->band) {
131                 case IEEE80211_BAND_2GHZ: {
132                         u32 flag;
133                         if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
134                                 flag = IEEE80211_RATE_MANDATORY_G;
135                         else
136                                 flag = IEEE80211_RATE_MANDATORY_B;
137                         if (r->flags & flag)
138                                 mrate = r->bitrate;
139                         break;
140                 }
141                 case IEEE80211_BAND_5GHZ:
142                         if (r->flags & IEEE80211_RATE_MANDATORY_A)
143                                 mrate = r->bitrate;
144                         break;
145                 case IEEE80211_NUM_BANDS:
146                         WARN_ON(1);
147                         break;
148                 }
149         }
150         if (rate == -1) {
151                 /* No matching basic rate found; use highest suitable mandatory
152                  * PHY rate */
153                 rate = mrate;
154         }
155
156         /* Time needed to transmit ACK
157          * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
158          * to closest integer */
159
160         dur = ieee80211_frame_duration(local, 10, rate, erp,
161                                 tx->sdata->vif.bss_conf.use_short_preamble);
162
163         if (next_frag_len) {
164                 /* Frame is fragmented: duration increases with time needed to
165                  * transmit next fragment plus ACK and 2 x SIFS. */
166                 dur *= 2; /* ACK + SIFS */
167                 /* next fragment */
168                 dur += ieee80211_frame_duration(local, next_frag_len,
169                                 txrate->bitrate, erp,
170                                 tx->sdata->vif.bss_conf.use_short_preamble);
171         }
172
173         return cpu_to_le16(dur);
174 }
175
176 static int inline is_ieee80211_device(struct ieee80211_local *local,
177                                       struct net_device *dev)
178 {
179         return local == wdev_priv(dev->ieee80211_ptr);
180 }
181
182 /* tx handlers */
183 static ieee80211_tx_result debug_noinline
184 ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
185 {
186         struct ieee80211_local *local = tx->local;
187
188         /* driver doesn't support power save */
189         if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
190                 return TX_CONTINUE;
191
192         /* hardware does dynamic power save */
193         if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
194                 return TX_CONTINUE;
195
196         /* dynamic power save disabled */
197         if (local->hw.conf.dynamic_ps_timeout <= 0)
198                 return TX_CONTINUE;
199
200         /* we are scanning, don't enable power save */
201         if (local->scanning)
202                 return TX_CONTINUE;
203
204         if (!local->ps_sdata)
205                 return TX_CONTINUE;
206
207         /* No point if we're going to suspend */
208         if (local->quiescing)
209                 return TX_CONTINUE;
210
211         if (local->hw.conf.flags & IEEE80211_CONF_PS) {
212                 ieee80211_stop_queues_by_reason(&local->hw,
213                                                 IEEE80211_QUEUE_STOP_REASON_PS);
214                 ieee80211_queue_work(&local->hw,
215                                      &local->dynamic_ps_disable_work);
216         }
217
218         mod_timer(&local->dynamic_ps_timer, jiffies +
219                   msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
220
221         return TX_CONTINUE;
222 }
223
224 static ieee80211_tx_result debug_noinline
225 ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
226 {
227
228         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
229         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
230         u32 sta_flags;
231
232         if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
233                 return TX_CONTINUE;
234
235         if (unlikely(test_bit(SCAN_OFF_CHANNEL, &tx->local->scanning)) &&
236             !ieee80211_is_probe_req(hdr->frame_control) &&
237             !ieee80211_is_nullfunc(hdr->frame_control))
238                 /*
239                  * When software scanning only nullfunc frames (to notify
240                  * the sleep state to the AP) and probe requests (for the
241                  * active scan) are allowed, all other frames should not be
242                  * sent and we should not get here, but if we do
243                  * nonetheless, drop them to avoid sending them
244                  * off-channel. See the link below and
245                  * ieee80211_start_scan() for more.
246                  *
247                  * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
248                  */
249                 return TX_DROP;
250
251         if (tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
252                 return TX_CONTINUE;
253
254         if (tx->flags & IEEE80211_TX_PS_BUFFERED)
255                 return TX_CONTINUE;
256
257         sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
258
259         if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
260                 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
261                              tx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
262                              ieee80211_is_data(hdr->frame_control))) {
263 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
264                         printk(KERN_DEBUG "%s: dropped data frame to not "
265                                "associated station %pM\n",
266                                tx->sdata->name, hdr->addr1);
267 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
268                         I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
269                         return TX_DROP;
270                 }
271         } else {
272                 if (unlikely(ieee80211_is_data(hdr->frame_control) &&
273                              tx->local->num_sta == 0 &&
274                              tx->sdata->vif.type != NL80211_IFTYPE_ADHOC)) {
275                         /*
276                          * No associated STAs - no need to send multicast
277                          * frames.
278                          */
279                         return TX_DROP;
280                 }
281                 return TX_CONTINUE;
282         }
283
284         return TX_CONTINUE;
285 }
286
287 /* This function is called whenever the AP is about to exceed the maximum limit
288  * of buffered frames for power saving STAs. This situation should not really
289  * happen often during normal operation, so dropping the oldest buffered packet
290  * from each queue should be OK to make some room for new frames. */
291 static void purge_old_ps_buffers(struct ieee80211_local *local)
292 {
293         int total = 0, purged = 0;
294         struct sk_buff *skb;
295         struct ieee80211_sub_if_data *sdata;
296         struct sta_info *sta;
297
298         /*
299          * virtual interfaces are protected by RCU
300          */
301         rcu_read_lock();
302
303         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
304                 struct ieee80211_if_ap *ap;
305                 if (sdata->vif.type != NL80211_IFTYPE_AP)
306                         continue;
307                 ap = &sdata->u.ap;
308                 skb = skb_dequeue(&ap->ps_bc_buf);
309                 if (skb) {
310                         purged++;
311                         dev_kfree_skb(skb);
312                 }
313                 total += skb_queue_len(&ap->ps_bc_buf);
314         }
315
316         list_for_each_entry_rcu(sta, &local->sta_list, list) {
317                 skb = skb_dequeue(&sta->ps_tx_buf);
318                 if (skb) {
319                         purged++;
320                         dev_kfree_skb(skb);
321                 }
322                 total += skb_queue_len(&sta->ps_tx_buf);
323         }
324
325         rcu_read_unlock();
326
327         local->total_ps_buffered = total;
328 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
329         printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
330                wiphy_name(local->hw.wiphy), purged);
331 #endif
332 }
333
334 static ieee80211_tx_result
335 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
336 {
337         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
338         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
339
340         /*
341          * broadcast/multicast frame
342          *
343          * If any of the associated stations is in power save mode,
344          * the frame is buffered to be sent after DTIM beacon frame.
345          * This is done either by the hardware or us.
346          */
347
348         /* powersaving STAs only in AP/VLAN mode */
349         if (!tx->sdata->bss)
350                 return TX_CONTINUE;
351
352         /* no buffering for ordered frames */
353         if (ieee80211_has_order(hdr->frame_control))
354                 return TX_CONTINUE;
355
356         /* no stations in PS mode */
357         if (!atomic_read(&tx->sdata->bss->num_sta_ps))
358                 return TX_CONTINUE;
359
360         info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
361
362         /* device releases frame after DTIM beacon */
363         if (!(tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING))
364                 return TX_CONTINUE;
365
366         /* buffered in mac80211 */
367         if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
368                 purge_old_ps_buffers(tx->local);
369
370         if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >= AP_MAX_BC_BUFFER) {
371 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
372                 if (net_ratelimit())
373                         printk(KERN_DEBUG "%s: BC TX buffer full - dropping the oldest frame\n",
374                                tx->sdata->name);
375 #endif
376                 dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
377         } else
378                 tx->local->total_ps_buffered++;
379
380         skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
381
382         return TX_QUEUED;
383 }
384
385 static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
386                              struct sk_buff *skb)
387 {
388         if (!ieee80211_is_mgmt(fc))
389                 return 0;
390
391         if (sta == NULL || !test_sta_flags(sta, WLAN_STA_MFP))
392                 return 0;
393
394         if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *)
395                                             skb->data))
396                 return 0;
397
398         return 1;
399 }
400
401 static ieee80211_tx_result
402 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
403 {
404         struct sta_info *sta = tx->sta;
405         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
406         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
407         u32 staflags;
408
409         if (unlikely(!sta ||
410                      ieee80211_is_probe_resp(hdr->frame_control) ||
411                      ieee80211_is_auth(hdr->frame_control) ||
412                      ieee80211_is_assoc_resp(hdr->frame_control) ||
413                      ieee80211_is_reassoc_resp(hdr->frame_control)))
414                 return TX_CONTINUE;
415
416         staflags = get_sta_flags(sta);
417
418         if (unlikely((staflags & (WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) &&
419                      !(info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE))) {
420 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
421                 printk(KERN_DEBUG "STA %pM aid %d: PS buffer (entries "
422                        "before %d)\n",
423                        sta->sta.addr, sta->sta.aid,
424                        skb_queue_len(&sta->ps_tx_buf));
425 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
426                 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
427                         purge_old_ps_buffers(tx->local);
428                 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
429                         struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
430 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
431                         if (net_ratelimit()) {
432                                 printk(KERN_DEBUG "%s: STA %pM TX "
433                                        "buffer full - dropping oldest frame\n",
434                                        tx->sdata->name, sta->sta.addr);
435                         }
436 #endif
437                         dev_kfree_skb(old);
438                 } else
439                         tx->local->total_ps_buffered++;
440
441                 /*
442                  * Queue frame to be sent after STA wakes up/polls,
443                  * but don't set the TIM bit if the driver is blocking
444                  * wakeup or poll response transmissions anyway.
445                  */
446                 if (skb_queue_empty(&sta->ps_tx_buf) &&
447                     !(staflags & WLAN_STA_PS_DRIVER))
448                         sta_info_set_tim_bit(sta);
449
450                 info->control.jiffies = jiffies;
451                 info->control.vif = &tx->sdata->vif;
452                 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
453                 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
454                 return TX_QUEUED;
455         }
456 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
457         else if (unlikely(staflags & WLAN_STA_PS_STA)) {
458                 printk(KERN_DEBUG "%s: STA %pM in PS mode, but pspoll "
459                        "set -> send frame\n", tx->sdata->name,
460                        sta->sta.addr);
461         }
462 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
463
464         return TX_CONTINUE;
465 }
466
467 static ieee80211_tx_result debug_noinline
468 ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
469 {
470         if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
471                 return TX_CONTINUE;
472
473         if (tx->flags & IEEE80211_TX_UNICAST)
474                 return ieee80211_tx_h_unicast_ps_buf(tx);
475         else
476                 return ieee80211_tx_h_multicast_ps_buf(tx);
477 }
478
479 static ieee80211_tx_result debug_noinline
480 ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
481 {
482         struct ieee80211_key *key = NULL;
483         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
484         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
485
486         if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
487                 tx->key = NULL;
488         else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
489                 tx->key = key;
490         else if (ieee80211_is_mgmt(hdr->frame_control) &&
491                  (key = rcu_dereference(tx->sdata->default_mgmt_key)))
492                 tx->key = key;
493         else if ((key = rcu_dereference(tx->sdata->default_key)))
494                 tx->key = key;
495         else if (tx->sdata->drop_unencrypted &&
496                  (tx->skb->protocol != cpu_to_be16(ETH_P_PAE)) &&
497                  !(info->flags & IEEE80211_TX_CTL_INJECTED) &&
498                  (!ieee80211_is_robust_mgmt_frame(hdr) ||
499                   (ieee80211_is_action(hdr->frame_control) &&
500                    tx->sta && test_sta_flags(tx->sta, WLAN_STA_MFP)))) {
501                 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
502                 return TX_DROP;
503         } else
504                 tx->key = NULL;
505
506         if (tx->key) {
507                 tx->key->tx_rx_count++;
508                 /* TODO: add threshold stuff again */
509
510                 switch (tx->key->conf.alg) {
511                 case ALG_WEP:
512                         if (ieee80211_is_auth(hdr->frame_control))
513                                 break;
514                 case ALG_TKIP:
515                         if (!ieee80211_is_data_present(hdr->frame_control))
516                                 tx->key = NULL;
517                         break;
518                 case ALG_CCMP:
519                         if (!ieee80211_is_data_present(hdr->frame_control) &&
520                             !ieee80211_use_mfp(hdr->frame_control, tx->sta,
521                                                tx->skb))
522                                 tx->key = NULL;
523                         break;
524                 case ALG_AES_CMAC:
525                         if (!ieee80211_is_mgmt(hdr->frame_control))
526                                 tx->key = NULL;
527                         break;
528                 }
529         }
530
531         if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
532                 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
533
534         return TX_CONTINUE;
535 }
536
537 static ieee80211_tx_result debug_noinline
538 ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
539 {
540         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
541         struct ieee80211_hdr *hdr = (void *)tx->skb->data;
542         struct ieee80211_supported_band *sband;
543         struct ieee80211_rate *rate;
544         int i, len;
545         bool inval = false, rts = false, short_preamble = false;
546         struct ieee80211_tx_rate_control txrc;
547         u32 sta_flags;
548
549         memset(&txrc, 0, sizeof(txrc));
550
551         sband = tx->local->hw.wiphy->bands[tx->channel->band];
552
553         len = min_t(int, tx->skb->len + FCS_LEN,
554                          tx->local->hw.wiphy->frag_threshold);
555
556         /* set up the tx rate control struct we give the RC algo */
557         txrc.hw = local_to_hw(tx->local);
558         txrc.sband = sband;
559         txrc.bss_conf = &tx->sdata->vif.bss_conf;
560         txrc.skb = tx->skb;
561         txrc.reported_rate.idx = -1;
562         txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[tx->channel->band];
563         if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
564                 txrc.max_rate_idx = -1;
565         else
566                 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
567         txrc.ap = tx->sdata->vif.type == NL80211_IFTYPE_AP;
568
569         /* set up RTS protection if desired */
570         if (len > tx->local->hw.wiphy->rts_threshold) {
571                 txrc.rts = rts = true;
572         }
573
574         /*
575          * Use short preamble if the BSS can handle it, but not for
576          * management frames unless we know the receiver can handle
577          * that -- the management frame might be to a station that
578          * just wants a probe response.
579          */
580         if (tx->sdata->vif.bss_conf.use_short_preamble &&
581             (ieee80211_is_data(hdr->frame_control) ||
582              (tx->sta && test_sta_flags(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
583                 txrc.short_preamble = short_preamble = true;
584
585         sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
586
587         /*
588          * Lets not bother rate control if we're associated and cannot
589          * talk to the sta. This should not happen.
590          */
591         if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) &&
592                  (sta_flags & WLAN_STA_ASSOC) &&
593                  !rate_usable_index_exists(sband, &tx->sta->sta),
594                  "%s: Dropped data frame as no usable bitrate found while "
595                  "scanning and associated. Target station: "
596                  "%pM on %d GHz band\n",
597                  tx->sdata->name, hdr->addr1,
598                  tx->channel->band ? 5 : 2))
599                 return TX_DROP;
600
601         /*
602          * If we're associated with the sta at this point we know we can at
603          * least send the frame at the lowest bit rate.
604          */
605         rate_control_get_rate(tx->sdata, tx->sta, &txrc);
606
607         if (unlikely(info->control.rates[0].idx < 0))
608                 return TX_DROP;
609
610         if (txrc.reported_rate.idx < 0)
611                 txrc.reported_rate = info->control.rates[0];
612
613         if (tx->sta)
614                 tx->sta->last_tx_rate = txrc.reported_rate;
615
616         if (unlikely(!info->control.rates[0].count))
617                 info->control.rates[0].count = 1;
618
619         if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
620                          (info->flags & IEEE80211_TX_CTL_NO_ACK)))
621                 info->control.rates[0].count = 1;
622
623         if (is_multicast_ether_addr(hdr->addr1)) {
624                 /*
625                  * XXX: verify the rate is in the basic rateset
626                  */
627                 return TX_CONTINUE;
628         }
629
630         /*
631          * set up the RTS/CTS rate as the fastest basic rate
632          * that is not faster than the data rate
633          *
634          * XXX: Should this check all retry rates?
635          */
636         if (!(info->control.rates[0].flags & IEEE80211_TX_RC_MCS)) {
637                 s8 baserate = 0;
638
639                 rate = &sband->bitrates[info->control.rates[0].idx];
640
641                 for (i = 0; i < sband->n_bitrates; i++) {
642                         /* must be a basic rate */
643                         if (!(tx->sdata->vif.bss_conf.basic_rates & BIT(i)))
644                                 continue;
645                         /* must not be faster than the data rate */
646                         if (sband->bitrates[i].bitrate > rate->bitrate)
647                                 continue;
648                         /* maximum */
649                         if (sband->bitrates[baserate].bitrate <
650                              sband->bitrates[i].bitrate)
651                                 baserate = i;
652                 }
653
654                 info->control.rts_cts_rate_idx = baserate;
655         }
656
657         for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
658                 /*
659                  * make sure there's no valid rate following
660                  * an invalid one, just in case drivers don't
661                  * take the API seriously to stop at -1.
662                  */
663                 if (inval) {
664                         info->control.rates[i].idx = -1;
665                         continue;
666                 }
667                 if (info->control.rates[i].idx < 0) {
668                         inval = true;
669                         continue;
670                 }
671
672                 /*
673                  * For now assume MCS is already set up correctly, this
674                  * needs to be fixed.
675                  */
676                 if (info->control.rates[i].flags & IEEE80211_TX_RC_MCS) {
677                         WARN_ON(info->control.rates[i].idx > 76);
678                         continue;
679                 }
680
681                 /* set up RTS protection if desired */
682                 if (rts)
683                         info->control.rates[i].flags |=
684                                 IEEE80211_TX_RC_USE_RTS_CTS;
685
686                 /* RC is busted */
687                 if (WARN_ON_ONCE(info->control.rates[i].idx >=
688                                  sband->n_bitrates)) {
689                         info->control.rates[i].idx = -1;
690                         continue;
691                 }
692
693                 rate = &sband->bitrates[info->control.rates[i].idx];
694
695                 /* set up short preamble */
696                 if (short_preamble &&
697                     rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
698                         info->control.rates[i].flags |=
699                                 IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
700
701                 /* set up G protection */
702                 if (!rts && tx->sdata->vif.bss_conf.use_cts_prot &&
703                     rate->flags & IEEE80211_RATE_ERP_G)
704                         info->control.rates[i].flags |=
705                                 IEEE80211_TX_RC_USE_CTS_PROTECT;
706         }
707
708         return TX_CONTINUE;
709 }
710
711 static ieee80211_tx_result debug_noinline
712 ieee80211_tx_h_misc(struct ieee80211_tx_data *tx)
713 {
714         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
715
716         if (tx->sta)
717                 info->control.sta = &tx->sta->sta;
718
719         return TX_CONTINUE;
720 }
721
722 static ieee80211_tx_result debug_noinline
723 ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
724 {
725         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
726         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
727         u16 *seq;
728         u8 *qc;
729         int tid;
730
731         /*
732          * Packet injection may want to control the sequence
733          * number, if we have no matching interface then we
734          * neither assign one ourselves nor ask the driver to.
735          */
736         if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
737                 return TX_CONTINUE;
738
739         if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
740                 return TX_CONTINUE;
741
742         if (ieee80211_hdrlen(hdr->frame_control) < 24)
743                 return TX_CONTINUE;
744
745         /*
746          * Anything but QoS data that has a sequence number field
747          * (is long enough) gets a sequence number from the global
748          * counter.
749          */
750         if (!ieee80211_is_data_qos(hdr->frame_control)) {
751                 /* driver should assign sequence number */
752                 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
753                 /* for pure STA mode without beacons, we can do it */
754                 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
755                 tx->sdata->sequence_number += 0x10;
756                 return TX_CONTINUE;
757         }
758
759         /*
760          * This should be true for injected/management frames only, for
761          * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
762          * above since they are not QoS-data frames.
763          */
764         if (!tx->sta)
765                 return TX_CONTINUE;
766
767         /* include per-STA, per-TID sequence counter */
768
769         qc = ieee80211_get_qos_ctl(hdr);
770         tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
771         seq = &tx->sta->tid_seq[tid];
772
773         hdr->seq_ctrl = cpu_to_le16(*seq);
774
775         /* Increase the sequence number. */
776         *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
777
778         return TX_CONTINUE;
779 }
780
781 static int ieee80211_fragment(struct ieee80211_local *local,
782                               struct sk_buff *skb, int hdrlen,
783                               int frag_threshold)
784 {
785         struct sk_buff *tail = skb, *tmp;
786         int per_fragm = frag_threshold - hdrlen - FCS_LEN;
787         int pos = hdrlen + per_fragm;
788         int rem = skb->len - hdrlen - per_fragm;
789
790         if (WARN_ON(rem < 0))
791                 return -EINVAL;
792
793         while (rem) {
794                 int fraglen = per_fragm;
795
796                 if (fraglen > rem)
797                         fraglen = rem;
798                 rem -= fraglen;
799                 tmp = dev_alloc_skb(local->tx_headroom +
800                                     frag_threshold +
801                                     IEEE80211_ENCRYPT_HEADROOM +
802                                     IEEE80211_ENCRYPT_TAILROOM);
803                 if (!tmp)
804                         return -ENOMEM;
805                 tail->next = tmp;
806                 tail = tmp;
807                 skb_reserve(tmp, local->tx_headroom +
808                                  IEEE80211_ENCRYPT_HEADROOM);
809                 /* copy control information */
810                 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
811                 skb_copy_queue_mapping(tmp, skb);
812                 tmp->priority = skb->priority;
813                 tmp->dev = skb->dev;
814
815                 /* copy header and data */
816                 memcpy(skb_put(tmp, hdrlen), skb->data, hdrlen);
817                 memcpy(skb_put(tmp, fraglen), skb->data + pos, fraglen);
818
819                 pos += fraglen;
820         }
821
822         skb->len = hdrlen + per_fragm;
823         return 0;
824 }
825
826 static ieee80211_tx_result debug_noinline
827 ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
828 {
829         struct sk_buff *skb = tx->skb;
830         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
831         struct ieee80211_hdr *hdr = (void *)skb->data;
832         int frag_threshold = tx->local->hw.wiphy->frag_threshold;
833         int hdrlen;
834         int fragnum;
835
836         if (!(tx->flags & IEEE80211_TX_FRAGMENTED))
837                 return TX_CONTINUE;
838
839         /*
840          * Warn when submitting a fragmented A-MPDU frame and drop it.
841          * This scenario is handled in ieee80211_tx_prepare but extra
842          * caution taken here as fragmented ampdu may cause Tx stop.
843          */
844         if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
845                 return TX_DROP;
846
847         hdrlen = ieee80211_hdrlen(hdr->frame_control);
848
849         /* internal error, why is TX_FRAGMENTED set? */
850         if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
851                 return TX_DROP;
852
853         /*
854          * Now fragment the frame. This will allocate all the fragments and
855          * chain them (using skb as the first fragment) to skb->next.
856          * During transmission, we will remove the successfully transmitted
857          * fragments from this list. When the low-level driver rejects one
858          * of the fragments then we will simply pretend to accept the skb
859          * but store it away as pending.
860          */
861         if (ieee80211_fragment(tx->local, skb, hdrlen, frag_threshold))
862                 return TX_DROP;
863
864         /* update duration/seq/flags of fragments */
865         fragnum = 0;
866         do {
867                 int next_len;
868                 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
869
870                 hdr = (void *)skb->data;
871                 info = IEEE80211_SKB_CB(skb);
872
873                 if (skb->next) {
874                         hdr->frame_control |= morefrags;
875                         next_len = skb->next->len;
876                         /*
877                          * No multi-rate retries for fragmented frames, that
878                          * would completely throw off the NAV at other STAs.
879                          */
880                         info->control.rates[1].idx = -1;
881                         info->control.rates[2].idx = -1;
882                         info->control.rates[3].idx = -1;
883                         info->control.rates[4].idx = -1;
884                         BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 5);
885                         info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
886                 } else {
887                         hdr->frame_control &= ~morefrags;
888                         next_len = 0;
889                 }
890                 hdr->duration_id = ieee80211_duration(tx, 0, next_len);
891                 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
892                 fragnum++;
893         } while ((skb = skb->next));
894
895         return TX_CONTINUE;
896 }
897
898 static ieee80211_tx_result debug_noinline
899 ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
900 {
901         struct sk_buff *skb = tx->skb;
902
903         if (!tx->sta)
904                 return TX_CONTINUE;
905
906         tx->sta->tx_packets++;
907         do {
908                 tx->sta->tx_fragments++;
909                 tx->sta->tx_bytes += skb->len;
910         } while ((skb = skb->next));
911
912         return TX_CONTINUE;
913 }
914
915 static ieee80211_tx_result debug_noinline
916 ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
917 {
918         if (!tx->key)
919                 return TX_CONTINUE;
920
921         switch (tx->key->conf.alg) {
922         case ALG_WEP:
923                 return ieee80211_crypto_wep_encrypt(tx);
924         case ALG_TKIP:
925                 return ieee80211_crypto_tkip_encrypt(tx);
926         case ALG_CCMP:
927                 return ieee80211_crypto_ccmp_encrypt(tx);
928         case ALG_AES_CMAC:
929                 return ieee80211_crypto_aes_cmac_encrypt(tx);
930         }
931
932         /* not reached */
933         WARN_ON(1);
934         return TX_DROP;
935 }
936
937 static ieee80211_tx_result debug_noinline
938 ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
939 {
940         struct sk_buff *skb = tx->skb;
941         struct ieee80211_hdr *hdr;
942         int next_len;
943         bool group_addr;
944
945         do {
946                 hdr = (void *) skb->data;
947                 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
948                         break; /* must not overwrite AID */
949                 next_len = skb->next ? skb->next->len : 0;
950                 group_addr = is_multicast_ether_addr(hdr->addr1);
951
952                 hdr->duration_id =
953                         ieee80211_duration(tx, group_addr, next_len);
954         } while ((skb = skb->next));
955
956         return TX_CONTINUE;
957 }
958
959 /* actual transmit path */
960
961 /*
962  * deal with packet injection down monitor interface
963  * with Radiotap Header -- only called for monitor mode interface
964  */
965 static bool __ieee80211_parse_tx_radiotap(struct ieee80211_tx_data *tx,
966                                           struct sk_buff *skb)
967 {
968         /*
969          * this is the moment to interpret and discard the radiotap header that
970          * must be at the start of the packet injected in Monitor mode
971          *
972          * Need to take some care with endian-ness since radiotap
973          * args are little-endian
974          */
975
976         struct ieee80211_radiotap_iterator iterator;
977         struct ieee80211_radiotap_header *rthdr =
978                 (struct ieee80211_radiotap_header *) skb->data;
979         struct ieee80211_supported_band *sband;
980         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
981         int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
982
983         sband = tx->local->hw.wiphy->bands[tx->channel->band];
984
985         info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
986         tx->flags &= ~IEEE80211_TX_FRAGMENTED;
987
988         /*
989          * for every radiotap entry that is present
990          * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
991          * entries present, or -EINVAL on error)
992          */
993
994         while (!ret) {
995                 ret = ieee80211_radiotap_iterator_next(&iterator);
996
997                 if (ret)
998                         continue;
999
1000                 /* see if this argument is something we can use */
1001                 switch (iterator.this_arg_index) {
1002                 /*
1003                  * You must take care when dereferencing iterator.this_arg
1004                  * for multibyte types... the pointer is not aligned.  Use
1005                  * get_unaligned((type *)iterator.this_arg) to dereference
1006                  * iterator.this_arg for type "type" safely on all arches.
1007                 */
1008                 case IEEE80211_RADIOTAP_FLAGS:
1009                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
1010                                 /*
1011                                  * this indicates that the skb we have been
1012                                  * handed has the 32-bit FCS CRC at the end...
1013                                  * we should react to that by snipping it off
1014                                  * because it will be recomputed and added
1015                                  * on transmission
1016                                  */
1017                                 if (skb->len < (iterator.max_length + FCS_LEN))
1018                                         return false;
1019
1020                                 skb_trim(skb, skb->len - FCS_LEN);
1021                         }
1022                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
1023                                 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
1024                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
1025                                 tx->flags |= IEEE80211_TX_FRAGMENTED;
1026                         break;
1027
1028                 /*
1029                  * Please update the file
1030                  * Documentation/networking/mac80211-injection.txt
1031                  * when parsing new fields here.
1032                  */
1033
1034                 default:
1035                         break;
1036                 }
1037         }
1038
1039         if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
1040                 return false;
1041
1042         /*
1043          * remove the radiotap header
1044          * iterator->max_length was sanity-checked against
1045          * skb->len by iterator init
1046          */
1047         skb_pull(skb, iterator.max_length);
1048
1049         return true;
1050 }
1051
1052 /*
1053  * initialises @tx
1054  */
1055 static ieee80211_tx_result
1056 ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1057                      struct ieee80211_tx_data *tx,
1058                      struct sk_buff *skb)
1059 {
1060         struct ieee80211_local *local = sdata->local;
1061         struct ieee80211_hdr *hdr;
1062         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1063         int hdrlen, tid;
1064         u8 *qc, *state;
1065         bool queued = false;
1066
1067         memset(tx, 0, sizeof(*tx));
1068         tx->skb = skb;
1069         tx->local = local;
1070         tx->sdata = sdata;
1071         tx->channel = local->hw.conf.channel;
1072         /*
1073          * Set this flag (used below to indicate "automatic fragmentation"),
1074          * it will be cleared/left by radiotap as desired.
1075          */
1076         tx->flags |= IEEE80211_TX_FRAGMENTED;
1077
1078         /* process and remove the injection radiotap header */
1079         if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED)) {
1080                 if (!__ieee80211_parse_tx_radiotap(tx, skb))
1081                         return TX_DROP;
1082
1083                 /*
1084                  * __ieee80211_parse_tx_radiotap has now removed
1085                  * the radiotap header that was present and pre-filled
1086                  * 'tx' with tx control information.
1087                  */
1088         }
1089
1090         /*
1091          * If this flag is set to true anywhere, and we get here,
1092          * we are doing the needed processing, so remove the flag
1093          * now.
1094          */
1095         info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1096
1097         hdr = (struct ieee80211_hdr *) skb->data;
1098
1099         if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1100                 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1101                 if (!tx->sta && sdata->dev->ieee80211_ptr->use_4addr)
1102                         return TX_DROP;
1103         }
1104         if (!tx->sta)
1105                 tx->sta = sta_info_get(sdata, hdr->addr1);
1106
1107         if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
1108             (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION)) {
1109                 unsigned long flags;
1110                 struct tid_ampdu_tx *tid_tx;
1111
1112                 qc = ieee80211_get_qos_ctl(hdr);
1113                 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
1114
1115                 spin_lock_irqsave(&tx->sta->lock, flags);
1116                 /*
1117                  * XXX: This spinlock could be fairly expensive, but see the
1118                  *      comment in agg-tx.c:ieee80211_agg_tx_operational().
1119                  *      One way to solve this would be to do something RCU-like
1120                  *      for managing the tid_tx struct and using atomic bitops
1121                  *      for the actual state -- by introducing an actual
1122                  *      'operational' bit that would be possible. It would
1123                  *      require changing ieee80211_agg_tx_operational() to
1124                  *      set that bit, and changing the way tid_tx is managed
1125                  *      everywhere, including races between that bit and
1126                  *      tid_tx going away (tid_tx being added can be easily
1127                  *      committed to memory before the 'operational' bit).
1128                  */
1129                 tid_tx = tx->sta->ampdu_mlme.tid_tx[tid];
1130                 state = &tx->sta->ampdu_mlme.tid_state_tx[tid];
1131                 if (*state == HT_AGG_STATE_OPERATIONAL) {
1132                         info->flags |= IEEE80211_TX_CTL_AMPDU;
1133                 } else if (*state != HT_AGG_STATE_IDLE) {
1134                         /* in progress */
1135                         queued = true;
1136                         info->control.vif = &sdata->vif;
1137                         info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1138                         __skb_queue_tail(&tid_tx->pending, skb);
1139                 }
1140                 spin_unlock_irqrestore(&tx->sta->lock, flags);
1141
1142                 if (unlikely(queued))
1143                         return TX_QUEUED;
1144         }
1145
1146         if (is_multicast_ether_addr(hdr->addr1)) {
1147                 tx->flags &= ~IEEE80211_TX_UNICAST;
1148                 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1149         } else {
1150                 tx->flags |= IEEE80211_TX_UNICAST;
1151                 if (unlikely(local->wifi_wme_noack_test))
1152                         info->flags |= IEEE80211_TX_CTL_NO_ACK;
1153                 else
1154                         info->flags &= ~IEEE80211_TX_CTL_NO_ACK;
1155         }
1156
1157         if (tx->flags & IEEE80211_TX_FRAGMENTED) {
1158                 if ((tx->flags & IEEE80211_TX_UNICAST) &&
1159                     skb->len + FCS_LEN > local->hw.wiphy->frag_threshold &&
1160                     !(info->flags & IEEE80211_TX_CTL_AMPDU))
1161                         tx->flags |= IEEE80211_TX_FRAGMENTED;
1162                 else
1163                         tx->flags &= ~IEEE80211_TX_FRAGMENTED;
1164         }
1165
1166         if (!tx->sta)
1167                 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1168         else if (test_and_clear_sta_flags(tx->sta, WLAN_STA_CLEAR_PS_FILT))
1169                 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1170
1171         hdrlen = ieee80211_hdrlen(hdr->frame_control);
1172         if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
1173                 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
1174                 tx->ethertype = (pos[0] << 8) | pos[1];
1175         }
1176         info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
1177
1178         return TX_CONTINUE;
1179 }
1180
1181 static int __ieee80211_tx(struct ieee80211_local *local,
1182                           struct sk_buff **skbp,
1183                           struct sta_info *sta,
1184                           bool txpending)
1185 {
1186         struct sk_buff *skb = *skbp, *next;
1187         struct ieee80211_tx_info *info;
1188         struct ieee80211_sub_if_data *sdata;
1189         unsigned long flags;
1190         int ret, len;
1191         bool fragm = false;
1192
1193         while (skb) {
1194                 int q = skb_get_queue_mapping(skb);
1195
1196                 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1197                 ret = IEEE80211_TX_OK;
1198                 if (local->queue_stop_reasons[q] ||
1199                     (!txpending && !skb_queue_empty(&local->pending[q])))
1200                         ret = IEEE80211_TX_PENDING;
1201                 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1202                 if (ret != IEEE80211_TX_OK)
1203                         return ret;
1204
1205                 info = IEEE80211_SKB_CB(skb);
1206
1207                 if (fragm)
1208                         info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
1209                                          IEEE80211_TX_CTL_FIRST_FRAGMENT);
1210
1211                 next = skb->next;
1212                 len = skb->len;
1213
1214                 if (next)
1215                         info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
1216
1217                 sdata = vif_to_sdata(info->control.vif);
1218
1219                 switch (sdata->vif.type) {
1220                 case NL80211_IFTYPE_MONITOR:
1221                         info->control.vif = NULL;
1222                         break;
1223                 case NL80211_IFTYPE_AP_VLAN:
1224                         info->control.vif = &container_of(sdata->bss,
1225                                 struct ieee80211_sub_if_data, u.ap)->vif;
1226                         break;
1227                 default:
1228                         /* keep */
1229                         break;
1230                 }
1231
1232                 ret = drv_tx(local, skb);
1233                 if (WARN_ON(ret != NETDEV_TX_OK && skb->len != len)) {
1234                         dev_kfree_skb(skb);
1235                         ret = NETDEV_TX_OK;
1236                 }
1237                 if (ret != NETDEV_TX_OK) {
1238                         info->control.vif = &sdata->vif;
1239                         return IEEE80211_TX_AGAIN;
1240                 }
1241
1242                 *skbp = skb = next;
1243                 ieee80211_led_tx(local, 1);
1244                 fragm = true;
1245         }
1246
1247         return IEEE80211_TX_OK;
1248 }
1249
1250 /*
1251  * Invoke TX handlers, return 0 on success and non-zero if the
1252  * frame was dropped or queued.
1253  */
1254 static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1255 {
1256         struct sk_buff *skb = tx->skb;
1257         ieee80211_tx_result res = TX_DROP;
1258
1259 #define CALL_TXH(txh) \
1260         do {                            \
1261                 res = txh(tx);          \
1262                 if (res != TX_CONTINUE) \
1263                         goto txh_done;  \
1264         } while (0)
1265
1266         CALL_TXH(ieee80211_tx_h_dynamic_ps);
1267         CALL_TXH(ieee80211_tx_h_check_assoc);
1268         CALL_TXH(ieee80211_tx_h_ps_buf);
1269         CALL_TXH(ieee80211_tx_h_select_key);
1270         CALL_TXH(ieee80211_tx_h_michael_mic_add);
1271         if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
1272                 CALL_TXH(ieee80211_tx_h_rate_ctrl);
1273         CALL_TXH(ieee80211_tx_h_misc);
1274         CALL_TXH(ieee80211_tx_h_sequence);
1275         CALL_TXH(ieee80211_tx_h_fragment);
1276         /* handlers after fragment must be aware of tx info fragmentation! */
1277         CALL_TXH(ieee80211_tx_h_stats);
1278         CALL_TXH(ieee80211_tx_h_encrypt);
1279         CALL_TXH(ieee80211_tx_h_calculate_duration);
1280 #undef CALL_TXH
1281
1282  txh_done:
1283         if (unlikely(res == TX_DROP)) {
1284                 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1285                 while (skb) {
1286                         struct sk_buff *next;
1287
1288                         next = skb->next;
1289                         dev_kfree_skb(skb);
1290                         skb = next;
1291                 }
1292                 return -1;
1293         } else if (unlikely(res == TX_QUEUED)) {
1294                 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1295                 return -1;
1296         }
1297
1298         return 0;
1299 }
1300
1301 static void ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1302                          struct sk_buff *skb, bool txpending)
1303 {
1304         struct ieee80211_local *local = sdata->local;
1305         struct ieee80211_tx_data tx;
1306         ieee80211_tx_result res_prepare;
1307         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1308         struct sk_buff *next;
1309         unsigned long flags;
1310         int ret, retries;
1311         u16 queue;
1312
1313         queue = skb_get_queue_mapping(skb);
1314
1315         if (unlikely(skb->len < 10)) {
1316                 dev_kfree_skb(skb);
1317                 return;
1318         }
1319
1320         rcu_read_lock();
1321
1322         /* initialises tx */
1323         res_prepare = ieee80211_tx_prepare(sdata, &tx, skb);
1324
1325         if (unlikely(res_prepare == TX_DROP)) {
1326                 dev_kfree_skb(skb);
1327                 rcu_read_unlock();
1328                 return;
1329         } else if (unlikely(res_prepare == TX_QUEUED)) {
1330                 rcu_read_unlock();
1331                 return;
1332         }
1333
1334         tx.channel = local->hw.conf.channel;
1335         info->band = tx.channel->band;
1336
1337         if (invoke_tx_handlers(&tx))
1338                 goto out;
1339
1340         retries = 0;
1341  retry:
1342         ret = __ieee80211_tx(local, &tx.skb, tx.sta, txpending);
1343         switch (ret) {
1344         case IEEE80211_TX_OK:
1345                 break;
1346         case IEEE80211_TX_AGAIN:
1347                 /*
1348                  * Since there are no fragmented frames on A-MPDU
1349                  * queues, there's no reason for a driver to reject
1350                  * a frame there, warn and drop it.
1351                  */
1352                 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
1353                         goto drop;
1354                 /* fall through */
1355         case IEEE80211_TX_PENDING:
1356                 skb = tx.skb;
1357
1358                 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1359
1360                 if (local->queue_stop_reasons[queue] ||
1361                     !skb_queue_empty(&local->pending[queue])) {
1362                         /*
1363                          * if queue is stopped, queue up frames for later
1364                          * transmission from the tasklet
1365                          */
1366                         do {
1367                                 next = skb->next;
1368                                 skb->next = NULL;
1369                                 if (unlikely(txpending))
1370                                         __skb_queue_head(&local->pending[queue],
1371                                                          skb);
1372                                 else
1373                                         __skb_queue_tail(&local->pending[queue],
1374                                                          skb);
1375                         } while ((skb = next));
1376
1377                         spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1378                                                flags);
1379                 } else {
1380                         /*
1381                          * otherwise retry, but this is a race condition or
1382                          * a driver bug (which we warn about if it persists)
1383                          */
1384                         spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1385                                                flags);
1386
1387                         retries++;
1388                         if (WARN(retries > 10, "tx refused but queue active\n"))
1389                                 goto drop;
1390                         goto retry;
1391                 }
1392         }
1393  out:
1394         rcu_read_unlock();
1395         return;
1396
1397  drop:
1398         rcu_read_unlock();
1399
1400         skb = tx.skb;
1401         while (skb) {
1402                 next = skb->next;
1403                 dev_kfree_skb(skb);
1404                 skb = next;
1405         }
1406 }
1407
1408 /* device xmit handlers */
1409
1410 static int ieee80211_skb_resize(struct ieee80211_local *local,
1411                                 struct sk_buff *skb,
1412                                 int head_need, bool may_encrypt)
1413 {
1414         int tail_need = 0;
1415
1416         /*
1417          * This could be optimised, devices that do full hardware
1418          * crypto (including TKIP MMIC) need no tailroom... But we
1419          * have no drivers for such devices currently.
1420          */
1421         if (may_encrypt) {
1422                 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1423                 tail_need -= skb_tailroom(skb);
1424                 tail_need = max_t(int, tail_need, 0);
1425         }
1426
1427         if (head_need || tail_need) {
1428                 /* Sorry. Can't account for this any more */
1429                 skb_orphan(skb);
1430         }
1431
1432         if (skb_header_cloned(skb))
1433                 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1434         else
1435                 I802_DEBUG_INC(local->tx_expand_skb_head);
1436
1437         if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1438                 printk(KERN_DEBUG "%s: failed to reallocate TX buffer\n",
1439                        wiphy_name(local->hw.wiphy));
1440                 return -ENOMEM;
1441         }
1442
1443         /* update truesize too */
1444         skb->truesize += head_need + tail_need;
1445
1446         return 0;
1447 }
1448
1449 static void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
1450                            struct sk_buff *skb)
1451 {
1452         struct ieee80211_local *local = sdata->local;
1453         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1454         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1455         struct ieee80211_sub_if_data *tmp_sdata;
1456         int headroom;
1457         bool may_encrypt;
1458
1459         rcu_read_lock();
1460
1461         if (unlikely(sdata->vif.type == NL80211_IFTYPE_MONITOR)) {
1462                 int hdrlen;
1463                 u16 len_rthdr;
1464
1465                 info->flags |= IEEE80211_TX_CTL_INJECTED;
1466
1467                 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1468                 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
1469                 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1470
1471                 /* check the header is complete in the frame */
1472                 if (likely(skb->len >= len_rthdr + hdrlen)) {
1473                         /*
1474                          * We process outgoing injected frames that have a
1475                          * local address we handle as though they are our
1476                          * own frames.
1477                          * This code here isn't entirely correct, the local
1478                          * MAC address is not necessarily enough to find
1479                          * the interface to use; for that proper VLAN/WDS
1480                          * support we will need a different mechanism.
1481                          */
1482
1483                         list_for_each_entry_rcu(tmp_sdata, &local->interfaces,
1484                                                 list) {
1485                                 if (!ieee80211_sdata_running(tmp_sdata))
1486                                         continue;
1487                                 if (tmp_sdata->vif.type != NL80211_IFTYPE_AP)
1488                                         continue;
1489                                 if (compare_ether_addr(tmp_sdata->vif.addr,
1490                                                        hdr->addr2) == 0) {
1491                                         sdata = tmp_sdata;
1492                                         break;
1493                                 }
1494                         }
1495                 }
1496         }
1497
1498         may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
1499
1500         headroom = local->tx_headroom;
1501         if (may_encrypt)
1502                 headroom += IEEE80211_ENCRYPT_HEADROOM;
1503         headroom -= skb_headroom(skb);
1504         headroom = max_t(int, 0, headroom);
1505
1506         if (ieee80211_skb_resize(local, skb, headroom, may_encrypt)) {
1507                 dev_kfree_skb(skb);
1508                 rcu_read_unlock();
1509                 return;
1510         }
1511
1512         info->control.vif = &sdata->vif;
1513
1514         if (ieee80211_vif_is_mesh(&sdata->vif) &&
1515             ieee80211_is_data(hdr->frame_control) &&
1516                 !is_multicast_ether_addr(hdr->addr1))
1517                         if (mesh_nexthop_lookup(skb, sdata)) {
1518                                 /* skb queued: don't free */
1519                                 rcu_read_unlock();
1520                                 return;
1521                         }
1522
1523         ieee80211_set_qos_hdr(local, skb);
1524         ieee80211_tx(sdata, skb, false);
1525         rcu_read_unlock();
1526 }
1527
1528 netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
1529                                          struct net_device *dev)
1530 {
1531         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1532         struct ieee80211_channel *chan = local->hw.conf.channel;
1533         struct ieee80211_radiotap_header *prthdr =
1534                 (struct ieee80211_radiotap_header *)skb->data;
1535         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1536         u16 len_rthdr;
1537
1538         /*
1539          * Frame injection is not allowed if beaconing is not allowed
1540          * or if we need radar detection. Beaconing is usually not allowed when
1541          * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
1542          * Passive scan is also used in world regulatory domains where
1543          * your country is not known and as such it should be treated as
1544          * NO TX unless the channel is explicitly allowed in which case
1545          * your current regulatory domain would not have the passive scan
1546          * flag.
1547          *
1548          * Since AP mode uses monitor interfaces to inject/TX management
1549          * frames we can make AP mode the exception to this rule once it
1550          * supports radar detection as its implementation can deal with
1551          * radar detection by itself. We can do that later by adding a
1552          * monitor flag interfaces used for AP support.
1553          */
1554         if ((chan->flags & (IEEE80211_CHAN_NO_IBSS | IEEE80211_CHAN_RADAR |
1555              IEEE80211_CHAN_PASSIVE_SCAN)))
1556                 goto fail;
1557
1558         /* check for not even having the fixed radiotap header part */
1559         if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1560                 goto fail; /* too short to be possibly valid */
1561
1562         /* is it a header version we can trust to find length from? */
1563         if (unlikely(prthdr->it_version))
1564                 goto fail; /* only version 0 is supported */
1565
1566         /* then there must be a radiotap header with a length we can use */
1567         len_rthdr = ieee80211_get_radiotap_len(skb->data);
1568
1569         /* does the skb contain enough to deliver on the alleged length? */
1570         if (unlikely(skb->len < len_rthdr))
1571                 goto fail; /* skb too short for claimed rt header extent */
1572
1573         /*
1574          * fix up the pointers accounting for the radiotap
1575          * header still being in there.  We are being given
1576          * a precooked IEEE80211 header so no need for
1577          * normal processing
1578          */
1579         skb_set_mac_header(skb, len_rthdr);
1580         /*
1581          * these are just fixed to the end of the rt area since we
1582          * don't have any better information and at this point, nobody cares
1583          */
1584         skb_set_network_header(skb, len_rthdr);
1585         skb_set_transport_header(skb, len_rthdr);
1586
1587         memset(info, 0, sizeof(*info));
1588
1589         info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1590
1591         /* pass the radiotap header up to xmit */
1592         ieee80211_xmit(IEEE80211_DEV_TO_SUB_IF(dev), skb);
1593         return NETDEV_TX_OK;
1594
1595 fail:
1596         dev_kfree_skb(skb);
1597         return NETDEV_TX_OK; /* meaning, we dealt with the skb */
1598 }
1599
1600 /**
1601  * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1602  * subinterfaces (wlan#, WDS, and VLAN interfaces)
1603  * @skb: packet to be sent
1604  * @dev: incoming interface
1605  *
1606  * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1607  * not be freed, and caller is responsible for either retrying later or freeing
1608  * skb).
1609  *
1610  * This function takes in an Ethernet header and encapsulates it with suitable
1611  * IEEE 802.11 header based on which interface the packet is coming in. The
1612  * encapsulated packet will then be passed to master interface, wlan#.11, for
1613  * transmission (through low-level driver).
1614  */
1615 netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
1616                                     struct net_device *dev)
1617 {
1618         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1619         struct ieee80211_local *local = sdata->local;
1620         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1621         int ret = NETDEV_TX_BUSY, head_need;
1622         u16 ethertype, hdrlen,  meshhdrlen = 0;
1623         __le16 fc;
1624         struct ieee80211_hdr hdr;
1625         struct ieee80211s_hdr mesh_hdr;
1626         const u8 *encaps_data;
1627         int encaps_len, skip_header_bytes;
1628         int nh_pos, h_pos;
1629         struct sta_info *sta = NULL;
1630         u32 sta_flags = 0;
1631
1632         if (unlikely(skb->len < ETH_HLEN)) {
1633                 ret = NETDEV_TX_OK;
1634                 goto fail;
1635         }
1636
1637         nh_pos = skb_network_header(skb) - skb->data;
1638         h_pos = skb_transport_header(skb) - skb->data;
1639
1640         /* convert Ethernet header to proper 802.11 header (based on
1641          * operation mode) */
1642         ethertype = (skb->data[12] << 8) | skb->data[13];
1643         fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
1644
1645         switch (sdata->vif.type) {
1646         case NL80211_IFTYPE_AP_VLAN:
1647                 rcu_read_lock();
1648                 sta = rcu_dereference(sdata->u.vlan.sta);
1649                 if (sta) {
1650                         fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1651                         /* RA TA DA SA */
1652                         memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
1653                         memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1654                         memcpy(hdr.addr3, skb->data, ETH_ALEN);
1655                         memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1656                         hdrlen = 30;
1657                         sta_flags = get_sta_flags(sta);
1658                 }
1659                 rcu_read_unlock();
1660                 if (sta)
1661                         break;
1662                 /* fall through */
1663         case NL80211_IFTYPE_AP:
1664                 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
1665                 /* DA BSSID SA */
1666                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1667                 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1668                 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1669                 hdrlen = 24;
1670                 break;
1671         case NL80211_IFTYPE_WDS:
1672                 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1673                 /* RA TA DA SA */
1674                 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1675                 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1676                 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1677                 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1678                 hdrlen = 30;
1679                 break;
1680 #ifdef CONFIG_MAC80211_MESH
1681         case NL80211_IFTYPE_MESH_POINT:
1682                 if (!sdata->u.mesh.mshcfg.dot11MeshTTL) {
1683                         /* Do not send frames with mesh_ttl == 0 */
1684                         sdata->u.mesh.mshstats.dropped_frames_ttl++;
1685                         ret = NETDEV_TX_OK;
1686                         goto fail;
1687                 }
1688
1689                 if (compare_ether_addr(sdata->vif.addr,
1690                                        skb->data + ETH_ALEN) == 0) {
1691                         hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
1692                                         skb->data, skb->data + ETH_ALEN);
1693                         meshhdrlen = ieee80211_new_mesh_header(&mesh_hdr,
1694                                         sdata, NULL, NULL, NULL);
1695                 } else {
1696                         /* packet from other interface */
1697                         struct mesh_path *mppath;
1698                         int is_mesh_mcast = 1;
1699                         const u8 *mesh_da;
1700
1701                         rcu_read_lock();
1702                         if (is_multicast_ether_addr(skb->data))
1703                                 /* DA TA mSA AE:SA */
1704                                 mesh_da = skb->data;
1705                         else {
1706                                 static const u8 bcast[ETH_ALEN] =
1707                                         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1708
1709                                 mppath = mpp_path_lookup(skb->data, sdata);
1710                                 if (mppath) {
1711                                         /* RA TA mDA mSA AE:DA SA */
1712                                         mesh_da = mppath->mpp;
1713                                         is_mesh_mcast = 0;
1714                                 } else {
1715                                         /* DA TA mSA AE:SA */
1716                                         mesh_da = bcast;
1717                                 }
1718                         }
1719                         hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
1720                                         mesh_da, sdata->vif.addr);
1721                         rcu_read_unlock();
1722                         if (is_mesh_mcast)
1723                                 meshhdrlen =
1724                                         ieee80211_new_mesh_header(&mesh_hdr,
1725                                                         sdata,
1726                                                         skb->data + ETH_ALEN,
1727                                                         NULL,
1728                                                         NULL);
1729                         else
1730                                 meshhdrlen =
1731                                         ieee80211_new_mesh_header(&mesh_hdr,
1732                                                         sdata,
1733                                                         NULL,
1734                                                         skb->data,
1735                                                         skb->data + ETH_ALEN);
1736
1737                 }
1738                 break;
1739 #endif
1740         case NL80211_IFTYPE_STATION:
1741                 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
1742                 if (sdata->u.mgd.use_4addr && ethertype != ETH_P_PAE) {
1743                         fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1744                         /* RA TA DA SA */
1745                         memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1746                         memcpy(hdr.addr3, skb->data, ETH_ALEN);
1747                         memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1748                         hdrlen = 30;
1749                 } else {
1750                         fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
1751                         /* BSSID SA DA */
1752                         memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1753                         memcpy(hdr.addr3, skb->data, ETH_ALEN);
1754                         hdrlen = 24;
1755                 }
1756                 break;
1757         case NL80211_IFTYPE_ADHOC:
1758                 /* DA SA BSSID */
1759                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1760                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1761                 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
1762                 hdrlen = 24;
1763                 break;
1764         default:
1765                 ret = NETDEV_TX_OK;
1766                 goto fail;
1767         }
1768
1769         /*
1770          * There's no need to try to look up the destination
1771          * if it is a multicast address (which can only happen
1772          * in AP mode)
1773          */
1774         if (!is_multicast_ether_addr(hdr.addr1)) {
1775                 rcu_read_lock();
1776                 sta = sta_info_get(sdata, hdr.addr1);
1777                 if (sta)
1778                         sta_flags = get_sta_flags(sta);
1779                 rcu_read_unlock();
1780         }
1781
1782         /* receiver and we are QoS enabled, use a QoS type frame */
1783         if ((sta_flags & WLAN_STA_WME) && local->hw.queues >= 4) {
1784                 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1785                 hdrlen += 2;
1786         }
1787
1788         /*
1789          * Drop unicast frames to unauthorised stations unless they are
1790          * EAPOL frames from the local station.
1791          */
1792         if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1793                 unlikely(!is_multicast_ether_addr(hdr.addr1) &&
1794                       !(sta_flags & WLAN_STA_AUTHORIZED) &&
1795                       !(ethertype == ETH_P_PAE &&
1796                        compare_ether_addr(sdata->vif.addr,
1797                                           skb->data + ETH_ALEN) == 0))) {
1798 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1799                 if (net_ratelimit())
1800                         printk(KERN_DEBUG "%s: dropped frame to %pM"
1801                                " (unauthorized port)\n", dev->name,
1802                                hdr.addr1);
1803 #endif
1804
1805                 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
1806
1807                 ret = NETDEV_TX_OK;
1808                 goto fail;
1809         }
1810
1811         hdr.frame_control = fc;
1812         hdr.duration_id = 0;
1813         hdr.seq_ctrl = 0;
1814
1815         skip_header_bytes = ETH_HLEN;
1816         if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1817                 encaps_data = bridge_tunnel_header;
1818                 encaps_len = sizeof(bridge_tunnel_header);
1819                 skip_header_bytes -= 2;
1820         } else if (ethertype >= 0x600) {
1821                 encaps_data = rfc1042_header;
1822                 encaps_len = sizeof(rfc1042_header);
1823                 skip_header_bytes -= 2;
1824         } else {
1825                 encaps_data = NULL;
1826                 encaps_len = 0;
1827         }
1828
1829         skb_pull(skb, skip_header_bytes);
1830         nh_pos -= skip_header_bytes;
1831         h_pos -= skip_header_bytes;
1832
1833         head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
1834
1835         /*
1836          * So we need to modify the skb header and hence need a copy of
1837          * that. The head_need variable above doesn't, so far, include
1838          * the needed header space that we don't need right away. If we
1839          * can, then we don't reallocate right now but only after the
1840          * frame arrives at the master device (if it does...)
1841          *
1842          * If we cannot, however, then we will reallocate to include all
1843          * the ever needed space. Also, if we need to reallocate it anyway,
1844          * make it big enough for everything we may ever need.
1845          */
1846
1847         if (head_need > 0 || skb_cloned(skb)) {
1848                 head_need += IEEE80211_ENCRYPT_HEADROOM;
1849                 head_need += local->tx_headroom;
1850                 head_need = max_t(int, 0, head_need);
1851                 if (ieee80211_skb_resize(local, skb, head_need, true))
1852                         goto fail;
1853         }
1854
1855         if (encaps_data) {
1856                 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1857                 nh_pos += encaps_len;
1858                 h_pos += encaps_len;
1859         }
1860
1861         if (meshhdrlen > 0) {
1862                 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
1863                 nh_pos += meshhdrlen;
1864                 h_pos += meshhdrlen;
1865         }
1866
1867         if (ieee80211_is_data_qos(fc)) {
1868                 __le16 *qos_control;
1869
1870                 qos_control = (__le16*) skb_push(skb, 2);
1871                 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
1872                 /*
1873                  * Maybe we could actually set some fields here, for now just
1874                  * initialise to zero to indicate no special operation.
1875                  */
1876                 *qos_control = 0;
1877         } else
1878                 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1879
1880         nh_pos += hdrlen;
1881         h_pos += hdrlen;
1882
1883         dev->stats.tx_packets++;
1884         dev->stats.tx_bytes += skb->len;
1885
1886         /* Update skb pointers to various headers since this modified frame
1887          * is going to go through Linux networking code that may potentially
1888          * need things like pointer to IP header. */
1889         skb_set_mac_header(skb, 0);
1890         skb_set_network_header(skb, nh_pos);
1891         skb_set_transport_header(skb, h_pos);
1892
1893         memset(info, 0, sizeof(*info));
1894
1895         dev->trans_start = jiffies;
1896         ieee80211_xmit(sdata, skb);
1897
1898         return NETDEV_TX_OK;
1899
1900  fail:
1901         if (ret == NETDEV_TX_OK)
1902                 dev_kfree_skb(skb);
1903
1904         return ret;
1905 }
1906
1907
1908 /*
1909  * ieee80211_clear_tx_pending may not be called in a context where
1910  * it is possible that it packets could come in again.
1911  */
1912 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1913 {
1914         int i;
1915
1916         for (i = 0; i < local->hw.queues; i++)
1917                 skb_queue_purge(&local->pending[i]);
1918 }
1919
1920 static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
1921                                      struct sk_buff *skb)
1922 {
1923         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1924         struct ieee80211_sub_if_data *sdata;
1925         struct sta_info *sta;
1926         struct ieee80211_hdr *hdr;
1927         int ret;
1928         bool result = true;
1929
1930         sdata = vif_to_sdata(info->control.vif);
1931
1932         if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
1933                 ieee80211_tx(sdata, skb, true);
1934         } else {
1935                 hdr = (struct ieee80211_hdr *)skb->data;
1936                 sta = sta_info_get(sdata, hdr->addr1);
1937
1938                 ret = __ieee80211_tx(local, &skb, sta, true);
1939                 if (ret != IEEE80211_TX_OK)
1940                         result = false;
1941         }
1942
1943         return result;
1944 }
1945
1946 /*
1947  * Transmit all pending packets. Called from tasklet.
1948  */
1949 void ieee80211_tx_pending(unsigned long data)
1950 {
1951         struct ieee80211_local *local = (struct ieee80211_local *)data;
1952         unsigned long flags;
1953         int i;
1954         bool txok;
1955
1956         rcu_read_lock();
1957
1958         spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1959         for (i = 0; i < local->hw.queues; i++) {
1960                 /*
1961                  * If queue is stopped by something other than due to pending
1962                  * frames, or we have no pending frames, proceed to next queue.
1963                  */
1964                 if (local->queue_stop_reasons[i] ||
1965                     skb_queue_empty(&local->pending[i]))
1966                         continue;
1967
1968                 while (!skb_queue_empty(&local->pending[i])) {
1969                         struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
1970                         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1971                         struct ieee80211_sub_if_data *sdata;
1972
1973                         if (WARN_ON(!info->control.vif)) {
1974                                 kfree_skb(skb);
1975                                 continue;
1976                         }
1977
1978                         sdata = vif_to_sdata(info->control.vif);
1979                         spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1980                                                 flags);
1981
1982                         txok = ieee80211_tx_pending_skb(local, skb);
1983                         if (!txok)
1984                                 __skb_queue_head(&local->pending[i], skb);
1985                         spin_lock_irqsave(&local->queue_stop_reason_lock,
1986                                           flags);
1987                         if (!txok)
1988                                 break;
1989                 }
1990         }
1991         spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1992
1993         rcu_read_unlock();
1994 }
1995
1996 /* functions for drivers to get certain frames */
1997
1998 static void ieee80211_beacon_add_tim(struct ieee80211_if_ap *bss,
1999                                      struct sk_buff *skb,
2000                                      struct beacon_data *beacon)
2001 {
2002         u8 *pos, *tim;
2003         int aid0 = 0;
2004         int i, have_bits = 0, n1, n2;
2005
2006         /* Generate bitmap for TIM only if there are any STAs in power save
2007          * mode. */
2008         if (atomic_read(&bss->num_sta_ps) > 0)
2009                 /* in the hope that this is faster than
2010                  * checking byte-for-byte */
2011                 have_bits = !bitmap_empty((unsigned long*)bss->tim,
2012                                           IEEE80211_MAX_AID+1);
2013
2014         if (bss->dtim_count == 0)
2015                 bss->dtim_count = beacon->dtim_period - 1;
2016         else
2017                 bss->dtim_count--;
2018
2019         tim = pos = (u8 *) skb_put(skb, 6);
2020         *pos++ = WLAN_EID_TIM;
2021         *pos++ = 4;
2022         *pos++ = bss->dtim_count;
2023         *pos++ = beacon->dtim_period;
2024
2025         if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
2026                 aid0 = 1;
2027
2028         if (have_bits) {
2029                 /* Find largest even number N1 so that bits numbered 1 through
2030                  * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
2031                  * (N2 + 1) x 8 through 2007 are 0. */
2032                 n1 = 0;
2033                 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
2034                         if (bss->tim[i]) {
2035                                 n1 = i & 0xfe;
2036                                 break;
2037                         }
2038                 }
2039                 n2 = n1;
2040                 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
2041                         if (bss->tim[i]) {
2042                                 n2 = i;
2043                                 break;
2044                         }
2045                 }
2046
2047                 /* Bitmap control */
2048                 *pos++ = n1 | aid0;
2049                 /* Part Virt Bitmap */
2050                 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
2051
2052                 tim[1] = n2 - n1 + 4;
2053                 skb_put(skb, n2 - n1);
2054         } else {
2055                 *pos++ = aid0; /* Bitmap control */
2056                 *pos++ = 0; /* Part Virt Bitmap */
2057         }
2058 }
2059
2060 struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
2061                                          struct ieee80211_vif *vif,
2062                                          u16 *tim_offset, u16 *tim_length)
2063 {
2064         struct ieee80211_local *local = hw_to_local(hw);
2065         struct sk_buff *skb = NULL;
2066         struct ieee80211_tx_info *info;
2067         struct ieee80211_sub_if_data *sdata = NULL;
2068         struct ieee80211_if_ap *ap = NULL;
2069         struct beacon_data *beacon;
2070         struct ieee80211_supported_band *sband;
2071         enum ieee80211_band band = local->hw.conf.channel->band;
2072         struct ieee80211_tx_rate_control txrc;
2073
2074         sband = local->hw.wiphy->bands[band];
2075
2076         rcu_read_lock();
2077
2078         sdata = vif_to_sdata(vif);
2079
2080         if (tim_offset)
2081                 *tim_offset = 0;
2082         if (tim_length)
2083                 *tim_length = 0;
2084
2085         if (sdata->vif.type == NL80211_IFTYPE_AP) {
2086                 ap = &sdata->u.ap;
2087                 beacon = rcu_dereference(ap->beacon);
2088                 if (ap && beacon) {
2089                         /*
2090                          * headroom, head length,
2091                          * tail length and maximum TIM length
2092                          */
2093                         skb = dev_alloc_skb(local->tx_headroom +
2094                                             beacon->head_len +
2095                                             beacon->tail_len + 256);
2096                         if (!skb)
2097                                 goto out;
2098
2099                         skb_reserve(skb, local->tx_headroom);
2100                         memcpy(skb_put(skb, beacon->head_len), beacon->head,
2101                                beacon->head_len);
2102
2103                         /*
2104                          * Not very nice, but we want to allow the driver to call
2105                          * ieee80211_beacon_get() as a response to the set_tim()
2106                          * callback. That, however, is already invoked under the
2107                          * sta_lock to guarantee consistent and race-free update
2108                          * of the tim bitmap in mac80211 and the driver.
2109                          */
2110                         if (local->tim_in_locked_section) {
2111                                 ieee80211_beacon_add_tim(ap, skb, beacon);
2112                         } else {
2113                                 unsigned long flags;
2114
2115                                 spin_lock_irqsave(&local->sta_lock, flags);
2116                                 ieee80211_beacon_add_tim(ap, skb, beacon);
2117                                 spin_unlock_irqrestore(&local->sta_lock, flags);
2118                         }
2119
2120                         if (tim_offset)
2121                                 *tim_offset = beacon->head_len;
2122                         if (tim_length)
2123                                 *tim_length = skb->len - beacon->head_len;
2124
2125                         if (beacon->tail)
2126                                 memcpy(skb_put(skb, beacon->tail_len),
2127                                        beacon->tail, beacon->tail_len);
2128                 } else
2129                         goto out;
2130         } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
2131                 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2132                 struct ieee80211_hdr *hdr;
2133                 struct sk_buff *presp = rcu_dereference(ifibss->presp);
2134
2135                 if (!presp)
2136                         goto out;
2137
2138                 skb = skb_copy(presp, GFP_ATOMIC);
2139                 if (!skb)
2140                         goto out;
2141
2142                 hdr = (struct ieee80211_hdr *) skb->data;
2143                 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2144                                                  IEEE80211_STYPE_BEACON);
2145         } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2146                 struct ieee80211_mgmt *mgmt;
2147                 u8 *pos;
2148
2149                 /* headroom, head length, tail length and maximum TIM length */
2150                 skb = dev_alloc_skb(local->tx_headroom + 400);
2151                 if (!skb)
2152                         goto out;
2153
2154                 skb_reserve(skb, local->hw.extra_tx_headroom);
2155                 mgmt = (struct ieee80211_mgmt *)
2156                         skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2157                 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2158                 mgmt->frame_control =
2159                     cpu_to_le16(IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_BEACON);
2160                 memset(mgmt->da, 0xff, ETH_ALEN);
2161                 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
2162                 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
2163                 mgmt->u.beacon.beacon_int =
2164                         cpu_to_le16(sdata->vif.bss_conf.beacon_int);
2165                 mgmt->u.beacon.capab_info = 0x0; /* 0x0 for MPs */
2166
2167                 pos = skb_put(skb, 2);
2168                 *pos++ = WLAN_EID_SSID;
2169                 *pos++ = 0x0;
2170
2171                 mesh_mgmt_ies_add(skb, sdata);
2172         } else {
2173                 WARN_ON(1);
2174                 goto out;
2175         }
2176
2177         info = IEEE80211_SKB_CB(skb);
2178
2179         info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
2180         info->flags |= IEEE80211_TX_CTL_NO_ACK;
2181         info->band = band;
2182
2183         memset(&txrc, 0, sizeof(txrc));
2184         txrc.hw = hw;
2185         txrc.sband = sband;
2186         txrc.bss_conf = &sdata->vif.bss_conf;
2187         txrc.skb = skb;
2188         txrc.reported_rate.idx = -1;
2189         txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
2190         if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
2191                 txrc.max_rate_idx = -1;
2192         else
2193                 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
2194         txrc.ap = true;
2195         rate_control_get_rate(sdata, NULL, &txrc);
2196
2197         info->control.vif = vif;
2198
2199         info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
2200         info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
2201  out:
2202         rcu_read_unlock();
2203         return skb;
2204 }
2205 EXPORT_SYMBOL(ieee80211_beacon_get_tim);
2206
2207 struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
2208                                      struct ieee80211_vif *vif)
2209 {
2210         struct ieee80211_sub_if_data *sdata;
2211         struct ieee80211_if_managed *ifmgd;
2212         struct ieee80211_pspoll *pspoll;
2213         struct ieee80211_local *local;
2214         struct sk_buff *skb;
2215
2216         if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2217                 return NULL;
2218
2219         sdata = vif_to_sdata(vif);
2220         ifmgd = &sdata->u.mgd;
2221         local = sdata->local;
2222
2223         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
2224         if (!skb) {
2225                 printk(KERN_DEBUG "%s: failed to allocate buffer for "
2226                        "pspoll template\n", sdata->name);
2227                 return NULL;
2228         }
2229         skb_reserve(skb, local->hw.extra_tx_headroom);
2230
2231         pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
2232         memset(pspoll, 0, sizeof(*pspoll));
2233         pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
2234                                             IEEE80211_STYPE_PSPOLL);
2235         pspoll->aid = cpu_to_le16(ifmgd->aid);
2236
2237         /* aid in PS-Poll has its two MSBs each set to 1 */
2238         pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
2239
2240         memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
2241         memcpy(pspoll->ta, vif->addr, ETH_ALEN);
2242
2243         return skb;
2244 }
2245 EXPORT_SYMBOL(ieee80211_pspoll_get);
2246
2247 struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
2248                                        struct ieee80211_vif *vif)
2249 {
2250         struct ieee80211_hdr_3addr *nullfunc;
2251         struct ieee80211_sub_if_data *sdata;
2252         struct ieee80211_if_managed *ifmgd;
2253         struct ieee80211_local *local;
2254         struct sk_buff *skb;
2255
2256         if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2257                 return NULL;
2258
2259         sdata = vif_to_sdata(vif);
2260         ifmgd = &sdata->u.mgd;
2261         local = sdata->local;
2262
2263         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*nullfunc));
2264         if (!skb) {
2265                 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2266                        "template\n", sdata->name);
2267                 return NULL;
2268         }
2269         skb_reserve(skb, local->hw.extra_tx_headroom);
2270
2271         nullfunc = (struct ieee80211_hdr_3addr *) skb_put(skb,
2272                                                           sizeof(*nullfunc));
2273         memset(nullfunc, 0, sizeof(*nullfunc));
2274         nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
2275                                               IEEE80211_STYPE_NULLFUNC |
2276                                               IEEE80211_FCTL_TODS);
2277         memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
2278         memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
2279         memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
2280
2281         return skb;
2282 }
2283 EXPORT_SYMBOL(ieee80211_nullfunc_get);
2284
2285 struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
2286                                        struct ieee80211_vif *vif,
2287                                        const u8 *ssid, size_t ssid_len,
2288                                        const u8 *ie, size_t ie_len)
2289 {
2290         struct ieee80211_sub_if_data *sdata;
2291         struct ieee80211_local *local;
2292         struct ieee80211_hdr_3addr *hdr;
2293         struct sk_buff *skb;
2294         size_t ie_ssid_len;
2295         u8 *pos;
2296
2297         sdata = vif_to_sdata(vif);
2298         local = sdata->local;
2299         ie_ssid_len = 2 + ssid_len;
2300
2301         skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
2302                             ie_ssid_len + ie_len);
2303         if (!skb) {
2304                 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
2305                        "request template\n", sdata->name);
2306                 return NULL;
2307         }
2308
2309         skb_reserve(skb, local->hw.extra_tx_headroom);
2310
2311         hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
2312         memset(hdr, 0, sizeof(*hdr));
2313         hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2314                                          IEEE80211_STYPE_PROBE_REQ);
2315         memset(hdr->addr1, 0xff, ETH_ALEN);
2316         memcpy(hdr->addr2, vif->addr, ETH_ALEN);
2317         memset(hdr->addr3, 0xff, ETH_ALEN);
2318
2319         pos = skb_put(skb, ie_ssid_len);
2320         *pos++ = WLAN_EID_SSID;
2321         *pos++ = ssid_len;
2322         if (ssid)
2323                 memcpy(pos, ssid, ssid_len);
2324         pos += ssid_len;
2325
2326         if (ie) {
2327                 pos = skb_put(skb, ie_len);
2328                 memcpy(pos, ie, ie_len);
2329         }
2330
2331         return skb;
2332 }
2333 EXPORT_SYMBOL(ieee80211_probereq_get);
2334
2335 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2336                        const void *frame, size_t frame_len,
2337                        const struct ieee80211_tx_info *frame_txctl,
2338                        struct ieee80211_rts *rts)
2339 {
2340         const struct ieee80211_hdr *hdr = frame;
2341
2342         rts->frame_control =
2343             cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
2344         rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
2345                                                frame_txctl);
2346         memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
2347         memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
2348 }
2349 EXPORT_SYMBOL(ieee80211_rts_get);
2350
2351 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2352                              const void *frame, size_t frame_len,
2353                              const struct ieee80211_tx_info *frame_txctl,
2354                              struct ieee80211_cts *cts)
2355 {
2356         const struct ieee80211_hdr *hdr = frame;
2357
2358         cts->frame_control =
2359             cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
2360         cts->duration = ieee80211_ctstoself_duration(hw, vif,
2361                                                      frame_len, frame_txctl);
2362         memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
2363 }
2364 EXPORT_SYMBOL(ieee80211_ctstoself_get);
2365
2366 struct sk_buff *
2367 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
2368                           struct ieee80211_vif *vif)
2369 {
2370         struct ieee80211_local *local = hw_to_local(hw);
2371         struct sk_buff *skb = NULL;
2372         struct sta_info *sta;
2373         struct ieee80211_tx_data tx;
2374         struct ieee80211_sub_if_data *sdata;
2375         struct ieee80211_if_ap *bss = NULL;
2376         struct beacon_data *beacon;
2377         struct ieee80211_tx_info *info;
2378
2379         sdata = vif_to_sdata(vif);
2380         bss = &sdata->u.ap;
2381
2382         rcu_read_lock();
2383         beacon = rcu_dereference(bss->beacon);
2384
2385         if (sdata->vif.type != NL80211_IFTYPE_AP || !beacon || !beacon->head)
2386                 goto out;
2387
2388         if (bss->dtim_count != 0)
2389                 goto out; /* send buffered bc/mc only after DTIM beacon */
2390
2391         while (1) {
2392                 skb = skb_dequeue(&bss->ps_bc_buf);
2393                 if (!skb)
2394                         goto out;
2395                 local->total_ps_buffered--;
2396
2397                 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
2398                         struct ieee80211_hdr *hdr =
2399                                 (struct ieee80211_hdr *) skb->data;
2400                         /* more buffered multicast/broadcast frames ==> set
2401                          * MoreData flag in IEEE 802.11 header to inform PS
2402                          * STAs */
2403                         hdr->frame_control |=
2404                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2405                 }
2406
2407                 if (!ieee80211_tx_prepare(sdata, &tx, skb))
2408                         break;
2409                 dev_kfree_skb_any(skb);
2410         }
2411
2412         info = IEEE80211_SKB_CB(skb);
2413
2414         sta = tx.sta;
2415         tx.flags |= IEEE80211_TX_PS_BUFFERED;
2416         tx.channel = local->hw.conf.channel;
2417         info->band = tx.channel->band;
2418
2419         if (invoke_tx_handlers(&tx))
2420                 skb = NULL;
2421  out:
2422         rcu_read_unlock();
2423
2424         return skb;
2425 }
2426 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
2427
2428 void ieee80211_tx_skb(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
2429 {
2430         skb_set_mac_header(skb, 0);
2431         skb_set_network_header(skb, 0);
2432         skb_set_transport_header(skb, 0);
2433
2434         /* send all internal mgmt frames on VO */
2435         skb_set_queue_mapping(skb, 0);
2436
2437         /*
2438          * The other path calling ieee80211_xmit is from the tasklet,
2439          * and while we can handle concurrent transmissions locking
2440          * requirements are that we do not come into tx with bhs on.
2441          */
2442         local_bh_disable();
2443         ieee80211_xmit(sdata, skb);
2444         local_bh_enable();
2445 }