7a14a39ebd788624bdb26fb455aa9d1493fddfa1
[pandora-kernel.git] / net / mac80211 / tx.c
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2005-2006, Devicescape Software, Inc.
4  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
5  * Copyright 2007       Johannes Berg <johannes@sipsolutions.net>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  *
12  * Transmit and frame generation functions.
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/skbuff.h>
18 #include <linux/etherdevice.h>
19 #include <linux/bitmap.h>
20 #include <linux/rcupdate.h>
21 #include <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 "led.h"
29 #include "mesh.h"
30 #include "wep.h"
31 #include "wpa.h"
32 #include "wme.h"
33 #include "rate.h"
34
35 #define IEEE80211_TX_OK         0
36 #define IEEE80211_TX_AGAIN      1
37 #define IEEE80211_TX_FRAG_AGAIN 2
38
39 /* misc utils */
40
41 static inline void ieee80211_include_sequence(struct ieee80211_sub_if_data *sdata,
42                                               struct ieee80211_hdr *hdr)
43 {
44         /* Set the sequence number for this frame. */
45         hdr->seq_ctrl = cpu_to_le16(sdata->sequence);
46
47         /* Increase the sequence number. */
48         sdata->sequence = (sdata->sequence + 0x10) & IEEE80211_SCTL_SEQ;
49 }
50
51 #ifdef CONFIG_MAC80211_LOWTX_FRAME_DUMP
52 static void ieee80211_dump_frame(const char *ifname, const char *title,
53                                  const struct sk_buff *skb)
54 {
55         const struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
56         u16 fc;
57         int hdrlen;
58         DECLARE_MAC_BUF(mac);
59
60         printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
61         if (skb->len < 4) {
62                 printk("\n");
63                 return;
64         }
65
66         fc = le16_to_cpu(hdr->frame_control);
67         hdrlen = ieee80211_get_hdrlen(fc);
68         if (hdrlen > skb->len)
69                 hdrlen = skb->len;
70         if (hdrlen >= 4)
71                 printk(" FC=0x%04x DUR=0x%04x",
72                        fc, le16_to_cpu(hdr->duration_id));
73         if (hdrlen >= 10)
74                 printk(" A1=%s", print_mac(mac, hdr->addr1));
75         if (hdrlen >= 16)
76                 printk(" A2=%s", print_mac(mac, hdr->addr2));
77         if (hdrlen >= 24)
78                 printk(" A3=%s", print_mac(mac, hdr->addr3));
79         if (hdrlen >= 30)
80                 printk(" A4=%s", print_mac(mac, hdr->addr4));
81         printk("\n");
82 }
83 #else /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
84 static inline void ieee80211_dump_frame(const char *ifname, const char *title,
85                                         struct sk_buff *skb)
86 {
87 }
88 #endif /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
89
90 static u16 ieee80211_duration(struct ieee80211_tx_data *tx, int group_addr,
91                               int next_frag_len)
92 {
93         int rate, mrate, erp, dur, i;
94         struct ieee80211_rate *txrate;
95         struct ieee80211_local *local = tx->local;
96         struct ieee80211_supported_band *sband;
97
98         sband = local->hw.wiphy->bands[tx->channel->band];
99         txrate = &sband->bitrates[tx->rate_idx];
100
101         erp = 0;
102         if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
103                 erp = txrate->flags & IEEE80211_RATE_ERP_G;
104
105         /*
106          * data and mgmt (except PS Poll):
107          * - during CFP: 32768
108          * - during contention period:
109          *   if addr1 is group address: 0
110          *   if more fragments = 0 and addr1 is individual address: time to
111          *      transmit one ACK plus SIFS
112          *   if more fragments = 1 and addr1 is individual address: time to
113          *      transmit next fragment plus 2 x ACK plus 3 x SIFS
114          *
115          * IEEE 802.11, 9.6:
116          * - control response frame (CTS or ACK) shall be transmitted using the
117          *   same rate as the immediately previous frame in the frame exchange
118          *   sequence, if this rate belongs to the PHY mandatory rates, or else
119          *   at the highest possible rate belonging to the PHY rates in the
120          *   BSSBasicRateSet
121          */
122
123         if ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) {
124                 /* TODO: These control frames are not currently sent by
125                  * 80211.o, but should they be implemented, this function
126                  * needs to be updated to support duration field calculation.
127                  *
128                  * RTS: time needed to transmit pending data/mgmt frame plus
129                  *    one CTS frame plus one ACK frame plus 3 x SIFS
130                  * CTS: duration of immediately previous RTS minus time
131                  *    required to transmit CTS and its SIFS
132                  * ACK: 0 if immediately previous directed data/mgmt had
133                  *    more=0, with more=1 duration in ACK frame is duration
134                  *    from previous frame minus time needed to transmit ACK
135                  *    and its SIFS
136                  * PS Poll: BIT(15) | BIT(14) | aid
137                  */
138                 return 0;
139         }
140
141         /* data/mgmt */
142         if (0 /* FIX: data/mgmt during CFP */)
143                 return 32768;
144
145         if (group_addr) /* Group address as the destination - no ACK */
146                 return 0;
147
148         /* Individual destination address:
149          * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
150          * CTS and ACK frames shall be transmitted using the highest rate in
151          * basic rate set that is less than or equal to the rate of the
152          * immediately previous frame and that is using the same modulation
153          * (CCK or OFDM). If no basic rate set matches with these requirements,
154          * the highest mandatory rate of the PHY that is less than or equal to
155          * the rate of the previous frame is used.
156          * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
157          */
158         rate = -1;
159         /* use lowest available if everything fails */
160         mrate = sband->bitrates[0].bitrate;
161         for (i = 0; i < sband->n_bitrates; i++) {
162                 struct ieee80211_rate *r = &sband->bitrates[i];
163
164                 if (r->bitrate > txrate->bitrate)
165                         break;
166
167                 if (tx->sdata->basic_rates & BIT(i))
168                         rate = r->bitrate;
169
170                 switch (sband->band) {
171                 case IEEE80211_BAND_2GHZ: {
172                         u32 flag;
173                         if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
174                                 flag = IEEE80211_RATE_MANDATORY_G;
175                         else
176                                 flag = IEEE80211_RATE_MANDATORY_B;
177                         if (r->flags & flag)
178                                 mrate = r->bitrate;
179                         break;
180                 }
181                 case IEEE80211_BAND_5GHZ:
182                         if (r->flags & IEEE80211_RATE_MANDATORY_A)
183                                 mrate = r->bitrate;
184                         break;
185                 case IEEE80211_NUM_BANDS:
186                         WARN_ON(1);
187                         break;
188                 }
189         }
190         if (rate == -1) {
191                 /* No matching basic rate found; use highest suitable mandatory
192                  * PHY rate */
193                 rate = mrate;
194         }
195
196         /* Time needed to transmit ACK
197          * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
198          * to closest integer */
199
200         dur = ieee80211_frame_duration(local, 10, rate, erp,
201                                 tx->sdata->bss_conf.use_short_preamble);
202
203         if (next_frag_len) {
204                 /* Frame is fragmented: duration increases with time needed to
205                  * transmit next fragment plus ACK and 2 x SIFS. */
206                 dur *= 2; /* ACK + SIFS */
207                 /* next fragment */
208                 dur += ieee80211_frame_duration(local, next_frag_len,
209                                 txrate->bitrate, erp,
210                                 tx->sdata->bss_conf.use_short_preamble);
211         }
212
213         return dur;
214 }
215
216 static int inline is_ieee80211_device(struct net_device *dev,
217                                       struct net_device *master)
218 {
219         return (wdev_priv(dev->ieee80211_ptr) ==
220                 wdev_priv(master->ieee80211_ptr));
221 }
222
223 /* tx handlers */
224
225 static ieee80211_tx_result
226 ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
227 {
228 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
229         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
230 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
231         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
232         u32 sta_flags;
233
234         if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
235                 return TX_CONTINUE;
236
237         if (unlikely(tx->local->sta_sw_scanning) &&
238             ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
239              (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
240                 return TX_DROP;
241
242         if (tx->sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT)
243                 return TX_CONTINUE;
244
245         if (tx->flags & IEEE80211_TX_PS_BUFFERED)
246                 return TX_CONTINUE;
247
248         sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
249
250         if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
251                 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
252                              tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
253                              (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
254 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
255                         DECLARE_MAC_BUF(mac);
256                         printk(KERN_DEBUG "%s: dropped data frame to not "
257                                "associated station %s\n",
258                                tx->dev->name, print_mac(mac, hdr->addr1));
259 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
260                         I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
261                         return TX_DROP;
262                 }
263         } else {
264                 if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
265                              tx->local->num_sta == 0 &&
266                              tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS)) {
267                         /*
268                          * No associated STAs - no need to send multicast
269                          * frames.
270                          */
271                         return TX_DROP;
272                 }
273                 return TX_CONTINUE;
274         }
275
276         return TX_CONTINUE;
277 }
278
279 static ieee80211_tx_result
280 ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
281 {
282         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
283
284         if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
285                 ieee80211_include_sequence(tx->sdata, hdr);
286
287         return TX_CONTINUE;
288 }
289
290 /* This function is called whenever the AP is about to exceed the maximum limit
291  * of buffered frames for power saving STAs. This situation should not really
292  * happen often during normal operation, so dropping the oldest buffered packet
293  * from each queue should be OK to make some room for new frames. */
294 static void purge_old_ps_buffers(struct ieee80211_local *local)
295 {
296         int total = 0, purged = 0;
297         struct sk_buff *skb;
298         struct ieee80211_sub_if_data *sdata;
299         struct sta_info *sta;
300
301         /*
302          * virtual interfaces are protected by RCU
303          */
304         rcu_read_lock();
305
306         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
307                 struct ieee80211_if_ap *ap;
308                 if (sdata->dev == local->mdev ||
309                     sdata->vif.type != IEEE80211_IF_TYPE_AP)
310                         continue;
311                 ap = &sdata->u.ap;
312                 skb = skb_dequeue(&ap->ps_bc_buf);
313                 if (skb) {
314                         purged++;
315                         dev_kfree_skb(skb);
316                 }
317                 total += skb_queue_len(&ap->ps_bc_buf);
318         }
319
320         list_for_each_entry_rcu(sta, &local->sta_list, list) {
321                 skb = skb_dequeue(&sta->ps_tx_buf);
322                 if (skb) {
323                         purged++;
324                         dev_kfree_skb(skb);
325                 }
326                 total += skb_queue_len(&sta->ps_tx_buf);
327         }
328
329         rcu_read_unlock();
330
331         local->total_ps_buffered = total;
332         printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
333                wiphy_name(local->hw.wiphy), purged);
334 }
335
336 static ieee80211_tx_result
337 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
338 {
339         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
340
341         /*
342          * broadcast/multicast frame
343          *
344          * If any of the associated stations is in power save mode,
345          * the frame is buffered to be sent after DTIM beacon frame.
346          * This is done either by the hardware or us.
347          */
348
349         /* not AP/IBSS or ordered frame */
350         if (!tx->sdata->bss || (tx->fc & IEEE80211_FCTL_ORDER))
351                 return TX_CONTINUE;
352
353         /* no stations in PS mode */
354         if (!atomic_read(&tx->sdata->bss->num_sta_ps))
355                 return TX_CONTINUE;
356
357         /* buffered in mac80211 */
358         if (tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) {
359                 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
360                         purge_old_ps_buffers(tx->local);
361                 if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
362                     AP_MAX_BC_BUFFER) {
363                         if (net_ratelimit()) {
364                                 printk(KERN_DEBUG "%s: BC TX buffer full - "
365                                        "dropping the oldest frame\n",
366                                        tx->dev->name);
367                         }
368                         dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
369                 } else
370                         tx->local->total_ps_buffered++;
371                 skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
372                 return TX_QUEUED;
373         }
374
375         /* buffered in hardware */
376         info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
377
378         return TX_CONTINUE;
379 }
380
381 static ieee80211_tx_result
382 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
383 {
384         struct sta_info *sta = tx->sta;
385         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
386         u32 staflags;
387         DECLARE_MAC_BUF(mac);
388
389         if (unlikely(!sta ||
390                      ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
391                       (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
392                 return TX_CONTINUE;
393
394         staflags = get_sta_flags(sta);
395
396         if (unlikely((staflags & WLAN_STA_PS) &&
397                      !(staflags & WLAN_STA_PSPOLL))) {
398 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
399                 printk(KERN_DEBUG "STA %s aid %d: PS buffer (entries "
400                        "before %d)\n",
401                        print_mac(mac, sta->addr), sta->aid,
402                        skb_queue_len(&sta->ps_tx_buf));
403 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
404                 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
405                         purge_old_ps_buffers(tx->local);
406                 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
407                         struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
408                         if (net_ratelimit()) {
409                                 printk(KERN_DEBUG "%s: STA %s TX "
410                                        "buffer full - dropping oldest frame\n",
411                                        tx->dev->name, print_mac(mac, sta->addr));
412                         }
413                         dev_kfree_skb(old);
414                 } else
415                         tx->local->total_ps_buffered++;
416
417                 /* Queue frame to be sent after STA sends an PS Poll frame */
418                 if (skb_queue_empty(&sta->ps_tx_buf))
419                         sta_info_set_tim_bit(sta);
420
421                 info->control.jiffies = jiffies;
422                 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
423                 return TX_QUEUED;
424         }
425 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
426         else if (unlikely(test_sta_flags(sta, WLAN_STA_PS))) {
427                 printk(KERN_DEBUG "%s: STA %s in PS mode, but pspoll "
428                        "set -> send frame\n", tx->dev->name,
429                        print_mac(mac, sta->addr));
430         }
431 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
432         clear_sta_flags(sta, WLAN_STA_PSPOLL);
433
434         return TX_CONTINUE;
435 }
436
437 static ieee80211_tx_result
438 ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
439 {
440         if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
441                 return TX_CONTINUE;
442
443         if (tx->flags & IEEE80211_TX_UNICAST)
444                 return ieee80211_tx_h_unicast_ps_buf(tx);
445         else
446                 return ieee80211_tx_h_multicast_ps_buf(tx);
447 }
448
449 static ieee80211_tx_result
450 ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
451 {
452         struct ieee80211_key *key;
453         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
454         u16 fc = tx->fc;
455
456         if (unlikely(info->flags & IEEE80211_TX_CTL_DO_NOT_ENCRYPT))
457                 tx->key = NULL;
458         else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
459                 tx->key = key;
460         else if ((key = rcu_dereference(tx->sdata->default_key)))
461                 tx->key = key;
462         else if (tx->sdata->drop_unencrypted &&
463                  !(info->flags & IEEE80211_TX_CTL_EAPOL_FRAME) &&
464                  !(info->flags & IEEE80211_TX_CTL_INJECTED)) {
465                 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
466                 return TX_DROP;
467         } else
468                 tx->key = NULL;
469
470         if (tx->key) {
471                 u16 ftype, stype;
472
473                 tx->key->tx_rx_count++;
474                 /* TODO: add threshold stuff again */
475
476                 switch (tx->key->conf.alg) {
477                 case ALG_WEP:
478                         ftype = fc & IEEE80211_FCTL_FTYPE;
479                         stype = fc & IEEE80211_FCTL_STYPE;
480
481                         if (ftype == IEEE80211_FTYPE_MGMT &&
482                             stype == IEEE80211_STYPE_AUTH)
483                                 break;
484                 case ALG_TKIP:
485                 case ALG_CCMP:
486                         if (!WLAN_FC_DATA_PRESENT(fc))
487                                 tx->key = NULL;
488                         break;
489                 }
490         }
491
492         if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
493                 info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
494
495         return TX_CONTINUE;
496 }
497
498 static ieee80211_tx_result
499 ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
500 {
501         struct rate_selection rsel;
502         struct ieee80211_supported_band *sband;
503         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
504
505         sband = tx->local->hw.wiphy->bands[tx->channel->band];
506
507         if (likely(tx->rate_idx < 0)) {
508                 rate_control_get_rate(tx->dev, sband, tx->skb, &rsel);
509                 tx->rate_idx = rsel.rate_idx;
510                 if (unlikely(rsel.probe_idx >= 0)) {
511                         info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
512                         tx->flags |= IEEE80211_TX_PROBE_LAST_FRAG;
513                         info->control.alt_retry_rate_idx = tx->rate_idx;
514                         tx->rate_idx = rsel.probe_idx;
515                 } else
516                         info->control.alt_retry_rate_idx = -1;
517
518                 if (unlikely(tx->rate_idx < 0))
519                         return TX_DROP;
520         } else
521                 info->control.alt_retry_rate_idx = -1;
522
523         if (tx->sdata->bss_conf.use_cts_prot &&
524             (tx->flags & IEEE80211_TX_FRAGMENTED) && (rsel.nonerp_idx >= 0)) {
525                 tx->last_frag_rate_idx = tx->rate_idx;
526                 if (rsel.probe_idx >= 0)
527                         tx->flags &= ~IEEE80211_TX_PROBE_LAST_FRAG;
528                 else
529                         tx->flags |= IEEE80211_TX_PROBE_LAST_FRAG;
530                 tx->rate_idx = rsel.nonerp_idx;
531                 info->tx_rate_idx = rsel.nonerp_idx;
532                 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
533         } else {
534                 tx->last_frag_rate_idx = tx->rate_idx;
535                 info->tx_rate_idx = tx->rate_idx;
536         }
537         info->tx_rate_idx = tx->rate_idx;
538
539         return TX_CONTINUE;
540 }
541
542 static ieee80211_tx_result
543 ieee80211_tx_h_misc(struct ieee80211_tx_data *tx)
544 {
545         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
546         u16 fc = le16_to_cpu(hdr->frame_control);
547         u16 dur;
548         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
549         struct ieee80211_supported_band *sband;
550
551         sband = tx->local->hw.wiphy->bands[tx->channel->band];
552
553         if (tx->sta)
554                 info->control.aid = tx->sta->aid;
555
556         if (!info->control.retry_limit) {
557                 if (!is_multicast_ether_addr(hdr->addr1)) {
558                         int len = min_t(int, tx->skb->len + FCS_LEN,
559                                         tx->local->fragmentation_threshold);
560                         if (len > tx->local->rts_threshold
561                             && tx->local->rts_threshold <
562                                                 IEEE80211_MAX_RTS_THRESHOLD) {
563                                 info->flags |= IEEE80211_TX_CTL_USE_RTS_CTS;
564                                 info->flags |=
565                                         IEEE80211_TX_CTL_LONG_RETRY_LIMIT;
566                                 info->control.retry_limit =
567                                         tx->local->long_retry_limit;
568                         } else {
569                                 info->control.retry_limit =
570                                         tx->local->short_retry_limit;
571                         }
572                 } else {
573                         info->control.retry_limit = 1;
574                 }
575         }
576
577         if (tx->flags & IEEE80211_TX_FRAGMENTED) {
578                 /* Do not use multiple retry rates when sending fragmented
579                  * frames.
580                  * TODO: The last fragment could still use multiple retry
581                  * rates. */
582                 info->control.alt_retry_rate_idx = -1;
583         }
584
585         /* Use CTS protection for unicast frames sent using extended rates if
586          * there are associated non-ERP stations and RTS/CTS is not configured
587          * for the frame. */
588         if ((tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE) &&
589             (sband->bitrates[tx->rate_idx].flags & IEEE80211_RATE_ERP_G) &&
590             (tx->flags & IEEE80211_TX_UNICAST) &&
591             tx->sdata->bss_conf.use_cts_prot &&
592             !(info->flags & IEEE80211_TX_CTL_USE_RTS_CTS))
593                 info->flags |= IEEE80211_TX_CTL_USE_CTS_PROTECT;
594
595         /* Transmit data frames using short preambles if the driver supports
596          * short preambles at the selected rate and short preambles are
597          * available on the network at the current point in time. */
598         if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
599             (sband->bitrates[tx->rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
600             tx->sdata->bss_conf.use_short_preamble &&
601             (!tx->sta || test_sta_flags(tx->sta, WLAN_STA_SHORT_PREAMBLE))) {
602                 info->flags |= IEEE80211_TX_CTL_SHORT_PREAMBLE;
603         }
604
605         /* Setup duration field for the first fragment of the frame. Duration
606          * for remaining fragments will be updated when they are being sent
607          * to low-level driver in ieee80211_tx(). */
608         dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
609                                  (tx->flags & IEEE80211_TX_FRAGMENTED) ?
610                                  tx->extra_frag[0]->len : 0);
611         hdr->duration_id = cpu_to_le16(dur);
612
613         if ((info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) ||
614             (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)) {
615                 struct ieee80211_rate *rate;
616                 s8 baserate = -1;
617                 int idx;
618
619                 /* Do not use multiple retry rates when using RTS/CTS */
620                 info->control.alt_retry_rate_idx = -1;
621
622                 /* Use min(data rate, max base rate) as CTS/RTS rate */
623                 rate = &sband->bitrates[tx->rate_idx];
624
625                 for (idx = 0; idx < sband->n_bitrates; idx++) {
626                         if (sband->bitrates[idx].bitrate > rate->bitrate)
627                                 continue;
628                         if (tx->sdata->basic_rates & BIT(idx) &&
629                             (baserate < 0 ||
630                              (sband->bitrates[baserate].bitrate
631                               < sband->bitrates[idx].bitrate)))
632                                 baserate = idx;
633                 }
634
635                 if (baserate >= 0)
636                         info->control.rts_cts_rate_idx = baserate;
637                 else
638                         info->control.rts_cts_rate_idx = 0;
639         }
640
641         if (tx->sta)
642                 info->control.aid = tx->sta->aid;
643
644         return TX_CONTINUE;
645 }
646
647 static ieee80211_tx_result
648 ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
649 {
650         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
651         size_t hdrlen, per_fragm, num_fragm, payload_len, left;
652         struct sk_buff **frags, *first, *frag;
653         int i;
654         u16 seq;
655         u8 *pos;
656         int frag_threshold = tx->local->fragmentation_threshold;
657
658         if (!(tx->flags & IEEE80211_TX_FRAGMENTED))
659                 return TX_CONTINUE;
660
661         /*
662          * Warn when submitting a fragmented A-MPDU frame and drop it.
663          * This scenario is handled in __ieee80211_tx_prepare but extra
664          * caution taken here as fragmented ampdu may cause Tx stop.
665          */
666         if (WARN_ON(tx->flags & IEEE80211_TX_CTL_AMPDU ||
667                     skb_get_queue_mapping(tx->skb) >=
668                         ieee80211_num_regular_queues(&tx->local->hw)))
669                 return TX_DROP;
670
671         first = tx->skb;
672
673         hdrlen = ieee80211_get_hdrlen(tx->fc);
674         payload_len = first->len - hdrlen;
675         per_fragm = frag_threshold - hdrlen - FCS_LEN;
676         num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
677
678         frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
679         if (!frags)
680                 goto fail;
681
682         hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
683         seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
684         pos = first->data + hdrlen + per_fragm;
685         left = payload_len - per_fragm;
686         for (i = 0; i < num_fragm - 1; i++) {
687                 struct ieee80211_hdr *fhdr;
688                 size_t copylen;
689
690                 if (left <= 0)
691                         goto fail;
692
693                 /* reserve enough extra head and tail room for possible
694                  * encryption */
695                 frag = frags[i] =
696                         dev_alloc_skb(tx->local->tx_headroom +
697                                       frag_threshold +
698                                       IEEE80211_ENCRYPT_HEADROOM +
699                                       IEEE80211_ENCRYPT_TAILROOM);
700                 if (!frag)
701                         goto fail;
702                 /* Make sure that all fragments use the same priority so
703                  * that they end up using the same TX queue */
704                 frag->priority = first->priority;
705                 skb_reserve(frag, tx->local->tx_headroom +
706                                   IEEE80211_ENCRYPT_HEADROOM);
707                 fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
708                 memcpy(fhdr, first->data, hdrlen);
709                 if (i == num_fragm - 2)
710                         fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
711                 fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
712                 copylen = left > per_fragm ? per_fragm : left;
713                 memcpy(skb_put(frag, copylen), pos, copylen);
714
715                 pos += copylen;
716                 left -= copylen;
717         }
718         skb_trim(first, hdrlen + per_fragm);
719
720         tx->num_extra_frag = num_fragm - 1;
721         tx->extra_frag = frags;
722
723         return TX_CONTINUE;
724
725  fail:
726         printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
727         if (frags) {
728                 for (i = 0; i < num_fragm - 1; i++)
729                         if (frags[i])
730                                 dev_kfree_skb(frags[i]);
731                 kfree(frags);
732         }
733         I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
734         return TX_DROP;
735 }
736
737 static ieee80211_tx_result
738 ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
739 {
740         if (!tx->key)
741                 return TX_CONTINUE;
742
743         switch (tx->key->conf.alg) {
744         case ALG_WEP:
745                 return ieee80211_crypto_wep_encrypt(tx);
746         case ALG_TKIP:
747                 return ieee80211_crypto_tkip_encrypt(tx);
748         case ALG_CCMP:
749                 return ieee80211_crypto_ccmp_encrypt(tx);
750         }
751
752         /* not reached */
753         WARN_ON(1);
754         return TX_DROP;
755 }
756
757 static ieee80211_tx_result
758 ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
759 {
760         int i;
761
762         if (!tx->sta)
763                 return TX_CONTINUE;
764
765         tx->sta->tx_packets++;
766         tx->sta->tx_fragments++;
767         tx->sta->tx_bytes += tx->skb->len;
768         if (tx->extra_frag) {
769                 tx->sta->tx_fragments += tx->num_extra_frag;
770                 for (i = 0; i < tx->num_extra_frag; i++)
771                         tx->sta->tx_bytes += tx->extra_frag[i]->len;
772         }
773
774         return TX_CONTINUE;
775 }
776
777
778 typedef ieee80211_tx_result (*ieee80211_tx_handler)(struct ieee80211_tx_data *);
779 static ieee80211_tx_handler ieee80211_tx_handlers[] =
780 {
781         ieee80211_tx_h_check_assoc,
782         ieee80211_tx_h_sequence,
783         ieee80211_tx_h_ps_buf,
784         ieee80211_tx_h_select_key,
785         ieee80211_tx_h_michael_mic_add,
786         ieee80211_tx_h_rate_ctrl,
787         ieee80211_tx_h_misc,
788         ieee80211_tx_h_fragment,
789         /* handlers after fragment must be aware of tx info fragmentation! */
790         ieee80211_tx_h_encrypt,
791         ieee80211_tx_h_stats,
792         NULL
793 };
794
795 /* actual transmit path */
796
797 /*
798  * deal with packet injection down monitor interface
799  * with Radiotap Header -- only called for monitor mode interface
800  */
801 static ieee80211_tx_result
802 __ieee80211_parse_tx_radiotap(struct ieee80211_tx_data *tx,
803                               struct sk_buff *skb)
804 {
805         /*
806          * this is the moment to interpret and discard the radiotap header that
807          * must be at the start of the packet injected in Monitor mode
808          *
809          * Need to take some care with endian-ness since radiotap
810          * args are little-endian
811          */
812
813         struct ieee80211_radiotap_iterator iterator;
814         struct ieee80211_radiotap_header *rthdr =
815                 (struct ieee80211_radiotap_header *) skb->data;
816         struct ieee80211_supported_band *sband;
817         int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
818         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
819
820         sband = tx->local->hw.wiphy->bands[tx->channel->band];
821
822         info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
823         info->flags |= IEEE80211_TX_CTL_INJECTED;
824         tx->flags &= ~IEEE80211_TX_FRAGMENTED;
825
826         /*
827          * for every radiotap entry that is present
828          * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
829          * entries present, or -EINVAL on error)
830          */
831
832         while (!ret) {
833                 int i, target_rate;
834
835                 ret = ieee80211_radiotap_iterator_next(&iterator);
836
837                 if (ret)
838                         continue;
839
840                 /* see if this argument is something we can use */
841                 switch (iterator.this_arg_index) {
842                 /*
843                  * You must take care when dereferencing iterator.this_arg
844                  * for multibyte types... the pointer is not aligned.  Use
845                  * get_unaligned((type *)iterator.this_arg) to dereference
846                  * iterator.this_arg for type "type" safely on all arches.
847                 */
848                 case IEEE80211_RADIOTAP_RATE:
849                         /*
850                          * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
851                          * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
852                          */
853                         target_rate = (*iterator.this_arg) * 5;
854                         for (i = 0; i < sband->n_bitrates; i++) {
855                                 struct ieee80211_rate *r;
856
857                                 r = &sband->bitrates[i];
858
859                                 if (r->bitrate == target_rate) {
860                                         tx->rate_idx = i;
861                                         break;
862                                 }
863                         }
864                         break;
865
866                 case IEEE80211_RADIOTAP_ANTENNA:
867                         /*
868                          * radiotap uses 0 for 1st ant, mac80211 is 1 for
869                          * 1st ant
870                          */
871                         info->antenna_sel_tx = (*iterator.this_arg) + 1;
872                         break;
873
874 #if 0
875                 case IEEE80211_RADIOTAP_DBM_TX_POWER:
876                         control->power_level = *iterator.this_arg;
877                         break;
878 #endif
879
880                 case IEEE80211_RADIOTAP_FLAGS:
881                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
882                                 /*
883                                  * this indicates that the skb we have been
884                                  * handed has the 32-bit FCS CRC at the end...
885                                  * we should react to that by snipping it off
886                                  * because it will be recomputed and added
887                                  * on transmission
888                                  */
889                                 if (skb->len < (iterator.max_length + FCS_LEN))
890                                         return TX_DROP;
891
892                                 skb_trim(skb, skb->len - FCS_LEN);
893                         }
894                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
895                                 info->flags &=
896                                         ~IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
897                         if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
898                                 tx->flags |= IEEE80211_TX_FRAGMENTED;
899                         break;
900
901                 /*
902                  * Please update the file
903                  * Documentation/networking/mac80211-injection.txt
904                  * when parsing new fields here.
905                  */
906
907                 default:
908                         break;
909                 }
910         }
911
912         if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
913                 return TX_DROP;
914
915         /*
916          * remove the radiotap header
917          * iterator->max_length was sanity-checked against
918          * skb->len by iterator init
919          */
920         skb_pull(skb, iterator.max_length);
921
922         return TX_CONTINUE;
923 }
924
925 /*
926  * initialises @tx
927  */
928 static ieee80211_tx_result
929 __ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
930                        struct sk_buff *skb,
931                        struct net_device *dev)
932 {
933         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
934         struct ieee80211_hdr *hdr;
935         struct ieee80211_sub_if_data *sdata;
936         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
937
938         int hdrlen;
939
940         memset(tx, 0, sizeof(*tx));
941         tx->skb = skb;
942         tx->dev = dev; /* use original interface */
943         tx->local = local;
944         tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
945         tx->channel = local->hw.conf.channel;
946         tx->rate_idx = -1;
947         tx->last_frag_rate_idx = -1;
948         /*
949          * Set this flag (used below to indicate "automatic fragmentation"),
950          * it will be cleared/left by radiotap as desired.
951          */
952         tx->flags |= IEEE80211_TX_FRAGMENTED;
953
954         /* process and remove the injection radiotap header */
955         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
956         if (unlikely(sdata->vif.type == IEEE80211_IF_TYPE_MNTR)) {
957                 if (__ieee80211_parse_tx_radiotap(tx, skb) == TX_DROP)
958                         return TX_DROP;
959
960                 /*
961                  * __ieee80211_parse_tx_radiotap has now removed
962                  * the radiotap header that was present and pre-filled
963                  * 'tx' with tx control information.
964                  */
965         }
966
967         hdr = (struct ieee80211_hdr *) skb->data;
968
969         tx->sta = sta_info_get(local, hdr->addr1);
970         tx->fc = le16_to_cpu(hdr->frame_control);
971
972         if (is_multicast_ether_addr(hdr->addr1)) {
973                 tx->flags &= ~IEEE80211_TX_UNICAST;
974                 info->flags |= IEEE80211_TX_CTL_NO_ACK;
975         } else {
976                 tx->flags |= IEEE80211_TX_UNICAST;
977                 info->flags &= ~IEEE80211_TX_CTL_NO_ACK;
978         }
979
980         if (tx->flags & IEEE80211_TX_FRAGMENTED) {
981                 if ((tx->flags & IEEE80211_TX_UNICAST) &&
982                     skb->len + FCS_LEN > local->fragmentation_threshold &&
983                     !local->ops->set_frag_threshold &&
984                     !(info->flags & IEEE80211_TX_CTL_AMPDU))
985                         tx->flags |= IEEE80211_TX_FRAGMENTED;
986                 else
987                         tx->flags &= ~IEEE80211_TX_FRAGMENTED;
988         }
989
990         if (!tx->sta)
991                 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
992         else if (test_and_clear_sta_flags(tx->sta, WLAN_STA_CLEAR_PS_FILT))
993                 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
994
995         hdrlen = ieee80211_get_hdrlen(tx->fc);
996         if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
997                 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
998                 tx->ethertype = (pos[0] << 8) | pos[1];
999         }
1000         info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
1001
1002         return TX_CONTINUE;
1003 }
1004
1005 /*
1006  * NB: @tx is uninitialised when passed in here
1007  */
1008 static int ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
1009                                 struct sk_buff *skb,
1010                                 struct net_device *mdev)
1011 {
1012         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1013         struct net_device *dev;
1014
1015         dev = dev_get_by_index(&init_net, info->control.ifindex);
1016         if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
1017                 dev_put(dev);
1018                 dev = NULL;
1019         }
1020         if (unlikely(!dev))
1021                 return -ENODEV;
1022         /* initialises tx with control */
1023         __ieee80211_tx_prepare(tx, skb, dev);
1024         dev_put(dev);
1025         return 0;
1026 }
1027
1028 static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
1029                           struct ieee80211_tx_data *tx)
1030 {
1031         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1032         int ret, i;
1033
1034         if (netif_subqueue_stopped(local->mdev, skb))
1035                 return IEEE80211_TX_AGAIN;
1036
1037         if (skb) {
1038                 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
1039                                      "TX to low-level driver", skb);
1040                 ret = local->ops->tx(local_to_hw(local), skb);
1041                 if (ret)
1042                         return IEEE80211_TX_AGAIN;
1043                 local->mdev->trans_start = jiffies;
1044                 ieee80211_led_tx(local, 1);
1045         }
1046         if (tx->extra_frag) {
1047                 for (i = 0; i < tx->num_extra_frag; i++) {
1048                         if (!tx->extra_frag[i])
1049                                 continue;
1050                         info = IEEE80211_SKB_CB(tx->extra_frag[i]);
1051                         info->flags &= ~(IEEE80211_TX_CTL_USE_RTS_CTS |
1052                                          IEEE80211_TX_CTL_USE_CTS_PROTECT |
1053                                          IEEE80211_TX_CTL_CLEAR_PS_FILT |
1054                                          IEEE80211_TX_CTL_FIRST_FRAGMENT);
1055                         if (netif_subqueue_stopped(local->mdev,
1056                                                    tx->extra_frag[i]))
1057                                 return IEEE80211_TX_FRAG_AGAIN;
1058                         if (i == tx->num_extra_frag) {
1059                                 info->tx_rate_idx = tx->last_frag_rate_idx;
1060
1061                                 if (tx->flags & IEEE80211_TX_PROBE_LAST_FRAG)
1062                                         info->flags |=
1063                                                 IEEE80211_TX_CTL_RATE_CTRL_PROBE;
1064                                 else
1065                                         info->flags &=
1066                                                 ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
1067                         }
1068
1069                         ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
1070                                              "TX to low-level driver",
1071                                              tx->extra_frag[i]);
1072                         ret = local->ops->tx(local_to_hw(local),
1073                                             tx->extra_frag[i]);
1074                         if (ret)
1075                                 return IEEE80211_TX_FRAG_AGAIN;
1076                         local->mdev->trans_start = jiffies;
1077                         ieee80211_led_tx(local, 1);
1078                         tx->extra_frag[i] = NULL;
1079                 }
1080                 kfree(tx->extra_frag);
1081                 tx->extra_frag = NULL;
1082         }
1083         return IEEE80211_TX_OK;
1084 }
1085
1086 /*
1087  * Invoke TX handlers, return 0 on success and non-zero if the
1088  * frame was dropped or queued.
1089  */
1090 static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1091 {
1092         struct ieee80211_local *local = tx->local;
1093         struct sk_buff *skb = tx->skb;
1094         ieee80211_tx_handler *handler;
1095         ieee80211_tx_result res = TX_DROP;
1096         int i;
1097
1098         for (handler = ieee80211_tx_handlers; *handler != NULL; handler++) {
1099                 res = (*handler)(tx);
1100                 if (res != TX_CONTINUE)
1101                         break;
1102         }
1103
1104         if (unlikely(res == TX_DROP)) {
1105                 I802_DEBUG_INC(local->tx_handlers_drop);
1106                 dev_kfree_skb(skb);
1107                 for (i = 0; i < tx->num_extra_frag; i++)
1108                         if (tx->extra_frag[i])
1109                                 dev_kfree_skb(tx->extra_frag[i]);
1110                 kfree(tx->extra_frag);
1111                 return -1;
1112         } else if (unlikely(res == TX_QUEUED)) {
1113                 I802_DEBUG_INC(local->tx_handlers_queued);
1114                 return -1;
1115         }
1116
1117         return 0;
1118 }
1119
1120 static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb)
1121 {
1122         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1123         struct sta_info *sta;
1124         struct ieee80211_tx_data tx;
1125         ieee80211_tx_result res_prepare;
1126         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1127         int ret, i;
1128         u16 queue;
1129
1130         queue = skb_get_queue_mapping(skb);
1131
1132         WARN_ON(test_bit(queue, local->queues_pending));
1133
1134         if (unlikely(skb->len < 10)) {
1135                 dev_kfree_skb(skb);
1136                 return 0;
1137         }
1138
1139         rcu_read_lock();
1140
1141         /* initialises tx */
1142         res_prepare = __ieee80211_tx_prepare(&tx, skb, dev);
1143
1144         if (res_prepare == TX_DROP) {
1145                 dev_kfree_skb(skb);
1146                 rcu_read_unlock();
1147                 return 0;
1148         }
1149
1150         sta = tx.sta;
1151         tx.channel = local->hw.conf.channel;
1152         info->band = tx.channel->band;
1153
1154         if (invoke_tx_handlers(&tx))
1155                 goto out;
1156
1157         if (tx.extra_frag) {
1158                 for (i = 0; i < tx.num_extra_frag; i++) {
1159                         int next_len, dur;
1160                         struct ieee80211_hdr *hdr =
1161                                 (struct ieee80211_hdr *)
1162                                 tx.extra_frag[i]->data;
1163
1164                         if (i + 1 < tx.num_extra_frag) {
1165                                 next_len = tx.extra_frag[i + 1]->len;
1166                         } else {
1167                                 next_len = 0;
1168                                 tx.rate_idx = tx.last_frag_rate_idx;
1169                         }
1170                         dur = ieee80211_duration(&tx, 0, next_len);
1171                         hdr->duration_id = cpu_to_le16(dur);
1172                 }
1173         }
1174
1175 retry:
1176         ret = __ieee80211_tx(local, skb, &tx);
1177         if (ret) {
1178                 struct ieee80211_tx_stored_packet *store;
1179
1180                 /*
1181                  * Since there are no fragmented frames on A-MPDU
1182                  * queues, there's no reason for a driver to reject
1183                  * a frame there, warn and drop it.
1184                  */
1185                 if (WARN_ON(queue >= ieee80211_num_regular_queues(&local->hw)))
1186                         goto drop;
1187
1188                 store = &local->pending_packet[queue];
1189
1190                 if (ret == IEEE80211_TX_FRAG_AGAIN)
1191                         skb = NULL;
1192                 set_bit(queue, local->queues_pending);
1193                 smp_mb();
1194                 /*
1195                  * When the driver gets out of buffers during sending of
1196                  * fragments and calls ieee80211_stop_queue, the netif
1197                  * subqueue is stopped. There is, however, a small window
1198                  * in which the PENDING bit is not yet set. If a buffer
1199                  * gets available in that window (i.e. driver calls
1200                  * ieee80211_wake_queue), we would end up with ieee80211_tx
1201                  * called with the PENDING bit still set. Prevent this by
1202                  * continuing transmitting here when that situation is
1203                  * possible to have happened.
1204                  */
1205                 if (!__netif_subqueue_stopped(local->mdev, queue)) {
1206                         clear_bit(queue, local->queues_pending);
1207                         goto retry;
1208                 }
1209                 store->skb = skb;
1210                 store->extra_frag = tx.extra_frag;
1211                 store->num_extra_frag = tx.num_extra_frag;
1212                 store->last_frag_rate_idx = tx.last_frag_rate_idx;
1213                 store->last_frag_rate_ctrl_probe =
1214                         !!(tx.flags & IEEE80211_TX_PROBE_LAST_FRAG);
1215         }
1216  out:
1217         rcu_read_unlock();
1218         return 0;
1219
1220  drop:
1221         if (skb)
1222                 dev_kfree_skb(skb);
1223         for (i = 0; i < tx.num_extra_frag; i++)
1224                 if (tx.extra_frag[i])
1225                         dev_kfree_skb(tx.extra_frag[i]);
1226         kfree(tx.extra_frag);
1227         rcu_read_unlock();
1228         return 0;
1229 }
1230
1231 /* device xmit handlers */
1232
1233 static int ieee80211_skb_resize(struct ieee80211_local *local,
1234                                 struct sk_buff *skb,
1235                                 int head_need, bool may_encrypt)
1236 {
1237         int tail_need = 0;
1238
1239         /*
1240          * This could be optimised, devices that do full hardware
1241          * crypto (including TKIP MMIC) need no tailroom... But we
1242          * have no drivers for such devices currently.
1243          */
1244         if (may_encrypt) {
1245                 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1246                 tail_need -= skb_tailroom(skb);
1247                 tail_need = max_t(int, tail_need, 0);
1248         }
1249
1250         if (head_need || tail_need) {
1251                 /* Sorry. Can't account for this any more */
1252                 skb_orphan(skb);
1253         }
1254
1255         if (skb_header_cloned(skb))
1256                 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1257         else
1258                 I802_DEBUG_INC(local->tx_expand_skb_head);
1259
1260         if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1261                 printk(KERN_DEBUG "%s: failed to reallocate TX buffer\n",
1262                        wiphy_name(local->hw.wiphy));
1263                 return -ENOMEM;
1264         }
1265
1266         /* update truesize too */
1267         skb->truesize += head_need + tail_need;
1268
1269         return 0;
1270 }
1271
1272 int ieee80211_master_start_xmit(struct sk_buff *skb,
1273                                 struct net_device *dev)
1274 {
1275         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1276         struct net_device *odev = NULL;
1277         struct ieee80211_sub_if_data *osdata;
1278         int headroom;
1279         bool may_encrypt;
1280         int ret;
1281
1282         if (info->control.ifindex)
1283                 odev = dev_get_by_index(&init_net, info->control.ifindex);
1284         if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
1285                 dev_put(odev);
1286                 odev = NULL;
1287         }
1288         if (unlikely(!odev)) {
1289 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1290                 printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
1291                        "originating device\n", dev->name);
1292 #endif
1293                 dev_kfree_skb(skb);
1294                 return 0;
1295         }
1296
1297         osdata = IEEE80211_DEV_TO_SUB_IF(odev);
1298
1299         may_encrypt = !(info->flags & IEEE80211_TX_CTL_DO_NOT_ENCRYPT);
1300
1301         headroom = osdata->local->tx_headroom;
1302         if (may_encrypt)
1303                 headroom += IEEE80211_ENCRYPT_HEADROOM;
1304         headroom -= skb_headroom(skb);
1305         headroom = max_t(int, 0, headroom);
1306
1307         if (ieee80211_skb_resize(osdata->local, skb, headroom, may_encrypt)) {
1308                 dev_kfree_skb(skb);
1309                 dev_put(odev);
1310                 return 0;
1311         }
1312
1313         info->control.vif = &osdata->vif;
1314         ret = ieee80211_tx(odev, skb);
1315         dev_put(odev);
1316
1317         return ret;
1318 }
1319
1320 int ieee80211_monitor_start_xmit(struct sk_buff *skb,
1321                                  struct net_device *dev)
1322 {
1323         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1324         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1325         struct ieee80211_radiotap_header *prthdr =
1326                 (struct ieee80211_radiotap_header *)skb->data;
1327         u16 len_rthdr;
1328
1329         /* check for not even having the fixed radiotap header part */
1330         if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1331                 goto fail; /* too short to be possibly valid */
1332
1333         /* is it a header version we can trust to find length from? */
1334         if (unlikely(prthdr->it_version))
1335                 goto fail; /* only version 0 is supported */
1336
1337         /* then there must be a radiotap header with a length we can use */
1338         len_rthdr = ieee80211_get_radiotap_len(skb->data);
1339
1340         /* does the skb contain enough to deliver on the alleged length? */
1341         if (unlikely(skb->len < len_rthdr))
1342                 goto fail; /* skb too short for claimed rt header extent */
1343
1344         skb->dev = local->mdev;
1345
1346         /* needed because we set skb device to master */
1347         info->control.ifindex = dev->ifindex;
1348
1349         info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
1350         /* Interfaces should always request a status report */
1351         info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1352
1353         /*
1354          * fix up the pointers accounting for the radiotap
1355          * header still being in there.  We are being given
1356          * a precooked IEEE80211 header so no need for
1357          * normal processing
1358          */
1359         skb_set_mac_header(skb, len_rthdr);
1360         /*
1361          * these are just fixed to the end of the rt area since we
1362          * don't have any better information and at this point, nobody cares
1363          */
1364         skb_set_network_header(skb, len_rthdr);
1365         skb_set_transport_header(skb, len_rthdr);
1366
1367         /* pass the radiotap header up to the next stage intact */
1368         dev_queue_xmit(skb);
1369         return NETDEV_TX_OK;
1370
1371 fail:
1372         dev_kfree_skb(skb);
1373         return NETDEV_TX_OK; /* meaning, we dealt with the skb */
1374 }
1375
1376 /**
1377  * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1378  * subinterfaces (wlan#, WDS, and VLAN interfaces)
1379  * @skb: packet to be sent
1380  * @dev: incoming interface
1381  *
1382  * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1383  * not be freed, and caller is responsible for either retrying later or freeing
1384  * skb).
1385  *
1386  * This function takes in an Ethernet header and encapsulates it with suitable
1387  * IEEE 802.11 header based on which interface the packet is coming in. The
1388  * encapsulated packet will then be passed to master interface, wlan#.11, for
1389  * transmission (through low-level driver).
1390  */
1391 int ieee80211_subif_start_xmit(struct sk_buff *skb,
1392                                struct net_device *dev)
1393 {
1394         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1395         struct ieee80211_tx_info *info;
1396         struct ieee80211_sub_if_data *sdata;
1397         int ret = 1, head_need;
1398         u16 ethertype, hdrlen,  meshhdrlen = 0, fc;
1399         struct ieee80211_hdr hdr;
1400         struct ieee80211s_hdr mesh_hdr;
1401         const u8 *encaps_data;
1402         int encaps_len, skip_header_bytes;
1403         int nh_pos, h_pos;
1404         struct sta_info *sta;
1405         u32 sta_flags = 0;
1406
1407         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1408         if (unlikely(skb->len < ETH_HLEN)) {
1409                 printk(KERN_DEBUG "%s: short skb (len=%d)\n",
1410                        dev->name, skb->len);
1411                 ret = 0;
1412                 goto fail;
1413         }
1414
1415         nh_pos = skb_network_header(skb) - skb->data;
1416         h_pos = skb_transport_header(skb) - skb->data;
1417
1418         /* convert Ethernet header to proper 802.11 header (based on
1419          * operation mode) */
1420         ethertype = (skb->data[12] << 8) | skb->data[13];
1421         fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
1422
1423         switch (sdata->vif.type) {
1424         case IEEE80211_IF_TYPE_AP:
1425         case IEEE80211_IF_TYPE_VLAN:
1426                 fc |= IEEE80211_FCTL_FROMDS;
1427                 /* DA BSSID SA */
1428                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1429                 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1430                 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1431                 hdrlen = 24;
1432                 break;
1433         case IEEE80211_IF_TYPE_WDS:
1434                 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1435                 /* RA TA DA SA */
1436                 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1437                 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1438                 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1439                 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1440                 hdrlen = 30;
1441                 break;
1442 #ifdef CONFIG_MAC80211_MESH
1443         case IEEE80211_IF_TYPE_MESH_POINT:
1444                 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1445                 /* RA TA DA SA */
1446                 if (is_multicast_ether_addr(skb->data))
1447                         memcpy(hdr.addr1, skb->data, ETH_ALEN);
1448                 else if (mesh_nexthop_lookup(hdr.addr1, skb, dev))
1449                                 return 0;
1450                 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1451                 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1452                 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1453                 if (skb->pkt_type == PACKET_OTHERHOST) {
1454                         /* Forwarded frame, keep mesh ttl and seqnum */
1455                         struct ieee80211s_hdr *prev_meshhdr;
1456                         prev_meshhdr = ((struct ieee80211s_hdr *)skb->cb);
1457                         meshhdrlen = ieee80211_get_mesh_hdrlen(prev_meshhdr);
1458                         memcpy(&mesh_hdr, prev_meshhdr, meshhdrlen);
1459                         sdata->u.sta.mshstats.fwded_frames++;
1460                 } else {
1461                         if (!sdata->u.sta.mshcfg.dot11MeshTTL) {
1462                                 /* Do not send frames with mesh_ttl == 0 */
1463                                 sdata->u.sta.mshstats.dropped_frames_ttl++;
1464                                 ret = 0;
1465                                 goto fail;
1466                         }
1467                         meshhdrlen = ieee80211_new_mesh_header(&mesh_hdr,
1468                                                                sdata);
1469                 }
1470                 hdrlen = 30;
1471                 break;
1472 #endif
1473         case IEEE80211_IF_TYPE_STA:
1474                 fc |= IEEE80211_FCTL_TODS;
1475                 /* BSSID SA DA */
1476                 memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
1477                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1478                 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1479                 hdrlen = 24;
1480                 break;
1481         case IEEE80211_IF_TYPE_IBSS:
1482                 /* DA SA BSSID */
1483                 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1484                 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1485                 memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
1486                 hdrlen = 24;
1487                 break;
1488         default:
1489                 ret = 0;
1490                 goto fail;
1491         }
1492
1493         /*
1494          * There's no need to try to look up the destination
1495          * if it is a multicast address (which can only happen
1496          * in AP mode)
1497          */
1498         if (!is_multicast_ether_addr(hdr.addr1)) {
1499                 rcu_read_lock();
1500                 sta = sta_info_get(local, hdr.addr1);
1501                 if (sta)
1502                         sta_flags = get_sta_flags(sta);
1503                 rcu_read_unlock();
1504         }
1505
1506         /* receiver and we are QoS enabled, use a QoS type frame */
1507         if (sta_flags & WLAN_STA_WME &&
1508             ieee80211_num_regular_queues(&local->hw) >= 4) {
1509                 fc |= IEEE80211_STYPE_QOS_DATA;
1510                 hdrlen += 2;
1511         }
1512
1513         /*
1514          * Drop unicast frames to unauthorised stations unless they are
1515          * EAPOL frames from the local station.
1516          */
1517         if (unlikely(!is_multicast_ether_addr(hdr.addr1) &&
1518                       !(sta_flags & WLAN_STA_AUTHORIZED) &&
1519                       !(ethertype == ETH_P_PAE &&
1520                        compare_ether_addr(dev->dev_addr,
1521                                           skb->data + ETH_ALEN) == 0))) {
1522 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1523                 DECLARE_MAC_BUF(mac);
1524
1525                 if (net_ratelimit())
1526                         printk(KERN_DEBUG "%s: dropped frame to %s"
1527                                " (unauthorized port)\n", dev->name,
1528                                print_mac(mac, hdr.addr1));
1529 #endif
1530
1531                 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
1532
1533                 ret = 0;
1534                 goto fail;
1535         }
1536
1537         hdr.frame_control = cpu_to_le16(fc);
1538         hdr.duration_id = 0;
1539         hdr.seq_ctrl = 0;
1540
1541         skip_header_bytes = ETH_HLEN;
1542         if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1543                 encaps_data = bridge_tunnel_header;
1544                 encaps_len = sizeof(bridge_tunnel_header);
1545                 skip_header_bytes -= 2;
1546         } else if (ethertype >= 0x600) {
1547                 encaps_data = rfc1042_header;
1548                 encaps_len = sizeof(rfc1042_header);
1549                 skip_header_bytes -= 2;
1550         } else {
1551                 encaps_data = NULL;
1552                 encaps_len = 0;
1553         }
1554
1555         skb_pull(skb, skip_header_bytes);
1556         nh_pos -= skip_header_bytes;
1557         h_pos -= skip_header_bytes;
1558
1559         /* TODO: implement support for fragments so that there is no need to
1560          * reallocate and copy payload; it might be enough to support one
1561          * extra fragment that would be copied in the beginning of the frame
1562          * data.. anyway, it would be nice to include this into skb structure
1563          * somehow
1564          *
1565          * There are few options for this:
1566          * use skb->cb as an extra space for 802.11 header
1567          * allocate new buffer if not enough headroom
1568          * make sure that there is enough headroom in every skb by increasing
1569          * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
1570          * alloc_skb() (net/core/skbuff.c)
1571          */
1572         head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
1573
1574         /*
1575          * So we need to modify the skb header and hence need a copy of
1576          * that. The head_need variable above doesn't, so far, include
1577          * the needed header space that we don't need right away. If we
1578          * can, then we don't reallocate right now but only after the
1579          * frame arrives at the master device (if it does...)
1580          *
1581          * If we cannot, however, then we will reallocate to include all
1582          * the ever needed space. Also, if we need to reallocate it anyway,
1583          * make it big enough for everything we may ever need.
1584          */
1585
1586         if (head_need > 0 || skb_cloned(skb)) {
1587                 head_need += IEEE80211_ENCRYPT_HEADROOM;
1588                 head_need += local->tx_headroom;
1589                 head_need = max_t(int, 0, head_need);
1590                 if (ieee80211_skb_resize(local, skb, head_need, true))
1591                         goto fail;
1592         }
1593
1594         if (encaps_data) {
1595                 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1596                 nh_pos += encaps_len;
1597                 h_pos += encaps_len;
1598         }
1599
1600         if (meshhdrlen > 0) {
1601                 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
1602                 nh_pos += meshhdrlen;
1603                 h_pos += meshhdrlen;
1604         }
1605
1606         if (fc & IEEE80211_STYPE_QOS_DATA) {
1607                 __le16 *qos_control;
1608
1609                 qos_control = (__le16*) skb_push(skb, 2);
1610                 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
1611                 /*
1612                  * Maybe we could actually set some fields here, for now just
1613                  * initialise to zero to indicate no special operation.
1614                  */
1615                 *qos_control = 0;
1616         } else
1617                 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1618
1619         nh_pos += hdrlen;
1620         h_pos += hdrlen;
1621
1622         info = IEEE80211_SKB_CB(skb);
1623         memset(info, 0, sizeof(*info));
1624         info->control.ifindex = dev->ifindex;
1625         if (ethertype == ETH_P_PAE)
1626                 info->flags |= IEEE80211_TX_CTL_EAPOL_FRAME;
1627
1628         /* Interfaces should always request a status report */
1629         info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1630
1631         skb->dev = local->mdev;
1632         dev->stats.tx_packets++;
1633         dev->stats.tx_bytes += skb->len;
1634
1635         /* Update skb pointers to various headers since this modified frame
1636          * is going to go through Linux networking code that may potentially
1637          * need things like pointer to IP header. */
1638         skb_set_mac_header(skb, 0);
1639         skb_set_network_header(skb, nh_pos);
1640         skb_set_transport_header(skb, h_pos);
1641
1642         dev->trans_start = jiffies;
1643         dev_queue_xmit(skb);
1644
1645         return 0;
1646
1647  fail:
1648         if (!ret)
1649                 dev_kfree_skb(skb);
1650
1651         return ret;
1652 }
1653
1654
1655 /*
1656  * ieee80211_clear_tx_pending may not be called in a context where
1657  * it is possible that it packets could come in again.
1658  */
1659 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1660 {
1661         int i, j;
1662         struct ieee80211_tx_stored_packet *store;
1663
1664         for (i = 0; i < ieee80211_num_regular_queues(&local->hw); i++) {
1665                 if (!test_bit(i, local->queues_pending))
1666                         continue;
1667                 store = &local->pending_packet[i];
1668                 kfree_skb(store->skb);
1669                 for (j = 0; j < store->num_extra_frag; j++)
1670                         kfree_skb(store->extra_frag[j]);
1671                 kfree(store->extra_frag);
1672                 clear_bit(i, local->queues_pending);
1673         }
1674 }
1675
1676 /*
1677  * Transmit all pending packets. Called from tasklet, locks master device
1678  * TX lock so that no new packets can come in.
1679  */
1680 void ieee80211_tx_pending(unsigned long data)
1681 {
1682         struct ieee80211_local *local = (struct ieee80211_local *)data;
1683         struct net_device *dev = local->mdev;
1684         struct ieee80211_tx_stored_packet *store;
1685         struct ieee80211_tx_data tx;
1686         int i, ret;
1687
1688         netif_tx_lock_bh(dev);
1689         for (i = 0; i < ieee80211_num_regular_queues(&local->hw); i++) {
1690                 /* Check that this queue is ok */
1691                 if (__netif_subqueue_stopped(local->mdev, i))
1692                         continue;
1693
1694                 if (!test_bit(i, local->queues_pending)) {
1695                         ieee80211_wake_queue(&local->hw, i);
1696                         continue;
1697                 }
1698
1699                 store = &local->pending_packet[i];
1700                 tx.extra_frag = store->extra_frag;
1701                 tx.num_extra_frag = store->num_extra_frag;
1702                 tx.last_frag_rate_idx = store->last_frag_rate_idx;
1703                 tx.flags = 0;
1704                 if (store->last_frag_rate_ctrl_probe)
1705                         tx.flags |= IEEE80211_TX_PROBE_LAST_FRAG;
1706                 ret = __ieee80211_tx(local, store->skb, &tx);
1707                 if (ret) {
1708                         if (ret == IEEE80211_TX_FRAG_AGAIN)
1709                                 store->skb = NULL;
1710                 } else {
1711                         clear_bit(i, local->queues_pending);
1712                         ieee80211_wake_queue(&local->hw, i);
1713                 }
1714         }
1715         netif_tx_unlock_bh(dev);
1716 }
1717
1718 /* functions for drivers to get certain frames */
1719
1720 static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
1721                                      struct ieee80211_if_ap *bss,
1722                                      struct sk_buff *skb,
1723                                      struct beacon_data *beacon)
1724 {
1725         u8 *pos, *tim;
1726         int aid0 = 0;
1727         int i, have_bits = 0, n1, n2;
1728
1729         /* Generate bitmap for TIM only if there are any STAs in power save
1730          * mode. */
1731         if (atomic_read(&bss->num_sta_ps) > 0)
1732                 /* in the hope that this is faster than
1733                  * checking byte-for-byte */
1734                 have_bits = !bitmap_empty((unsigned long*)bss->tim,
1735                                           IEEE80211_MAX_AID+1);
1736
1737         if (bss->dtim_count == 0)
1738                 bss->dtim_count = beacon->dtim_period - 1;
1739         else
1740                 bss->dtim_count--;
1741
1742         tim = pos = (u8 *) skb_put(skb, 6);
1743         *pos++ = WLAN_EID_TIM;
1744         *pos++ = 4;
1745         *pos++ = bss->dtim_count;
1746         *pos++ = beacon->dtim_period;
1747
1748         if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
1749                 aid0 = 1;
1750
1751         if (have_bits) {
1752                 /* Find largest even number N1 so that bits numbered 1 through
1753                  * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
1754                  * (N2 + 1) x 8 through 2007 are 0. */
1755                 n1 = 0;
1756                 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
1757                         if (bss->tim[i]) {
1758                                 n1 = i & 0xfe;
1759                                 break;
1760                         }
1761                 }
1762                 n2 = n1;
1763                 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
1764                         if (bss->tim[i]) {
1765                                 n2 = i;
1766                                 break;
1767                         }
1768                 }
1769
1770                 /* Bitmap control */
1771                 *pos++ = n1 | aid0;
1772                 /* Part Virt Bitmap */
1773                 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
1774
1775                 tim[1] = n2 - n1 + 4;
1776                 skb_put(skb, n2 - n1);
1777         } else {
1778                 *pos++ = aid0; /* Bitmap control */
1779                 *pos++ = 0; /* Part Virt Bitmap */
1780         }
1781 }
1782
1783 struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
1784                                      struct ieee80211_vif *vif)
1785 {
1786         struct ieee80211_local *local = hw_to_local(hw);
1787         struct sk_buff *skb;
1788         struct ieee80211_tx_info *info;
1789         struct net_device *bdev;
1790         struct ieee80211_sub_if_data *sdata = NULL;
1791         struct ieee80211_if_ap *ap = NULL;
1792         struct rate_selection rsel;
1793         struct beacon_data *beacon;
1794         struct ieee80211_supported_band *sband;
1795         struct ieee80211_mgmt *mgmt;
1796         int *num_beacons;
1797         bool err = true;
1798         enum ieee80211_band band = local->hw.conf.channel->band;
1799         u8 *pos;
1800
1801         sband = local->hw.wiphy->bands[band];
1802
1803         rcu_read_lock();
1804
1805         sdata = vif_to_sdata(vif);
1806         bdev = sdata->dev;
1807
1808         if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
1809                 ap = &sdata->u.ap;
1810                 beacon = rcu_dereference(ap->beacon);
1811                 if (ap && beacon) {
1812                         /*
1813                          * headroom, head length,
1814                          * tail length and maximum TIM length
1815                          */
1816                         skb = dev_alloc_skb(local->tx_headroom +
1817                                             beacon->head_len +
1818                                             beacon->tail_len + 256);
1819                         if (!skb)
1820                                 goto out;
1821
1822                         skb_reserve(skb, local->tx_headroom);
1823                         memcpy(skb_put(skb, beacon->head_len), beacon->head,
1824                                beacon->head_len);
1825
1826                         ieee80211_include_sequence(sdata,
1827                                         (struct ieee80211_hdr *)skb->data);
1828
1829                         /*
1830                          * Not very nice, but we want to allow the driver to call
1831                          * ieee80211_beacon_get() as a response to the set_tim()
1832                          * callback. That, however, is already invoked under the
1833                          * sta_lock to guarantee consistent and race-free update
1834                          * of the tim bitmap in mac80211 and the driver.
1835                          */
1836                         if (local->tim_in_locked_section) {
1837                                 ieee80211_beacon_add_tim(local, ap, skb, beacon);
1838                         } else {
1839                                 unsigned long flags;
1840
1841                                 spin_lock_irqsave(&local->sta_lock, flags);
1842                                 ieee80211_beacon_add_tim(local, ap, skb, beacon);
1843                                 spin_unlock_irqrestore(&local->sta_lock, flags);
1844                         }
1845
1846                         if (beacon->tail)
1847                                 memcpy(skb_put(skb, beacon->tail_len),
1848                                        beacon->tail, beacon->tail_len);
1849
1850                         num_beacons = &ap->num_beacons;
1851
1852                         err = false;
1853                 }
1854         } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
1855                 /* headroom, head length, tail length and maximum TIM length */
1856                 skb = dev_alloc_skb(local->tx_headroom + 400);
1857                 if (!skb)
1858                         goto out;
1859
1860                 skb_reserve(skb, local->hw.extra_tx_headroom);
1861                 mgmt = (struct ieee80211_mgmt *)
1862                         skb_put(skb, 24 + sizeof(mgmt->u.beacon));
1863                 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
1864                 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1865                                                    IEEE80211_STYPE_BEACON);
1866                 memset(mgmt->da, 0xff, ETH_ALEN);
1867                 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1868                 /* BSSID is left zeroed, wildcard value */
1869                 mgmt->u.beacon.beacon_int =
1870                         cpu_to_le16(local->hw.conf.beacon_int);
1871                 mgmt->u.beacon.capab_info = 0x0; /* 0x0 for MPs */
1872
1873                 pos = skb_put(skb, 2);
1874                 *pos++ = WLAN_EID_SSID;
1875                 *pos++ = 0x0;
1876
1877                 mesh_mgmt_ies_add(skb, sdata->dev);
1878
1879                 num_beacons = &sdata->u.sta.num_beacons;
1880
1881                 err = false;
1882         }
1883
1884         if (err) {
1885 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1886                 if (net_ratelimit())
1887                         printk(KERN_DEBUG "no beacon data avail for %s\n",
1888                                bdev->name);
1889 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
1890                 skb = NULL;
1891                 goto out;
1892         }
1893
1894         info = IEEE80211_SKB_CB(skb);
1895
1896         info->band = band;
1897         rate_control_get_rate(local->mdev, sband, skb, &rsel);
1898
1899         if (unlikely(rsel.rate_idx < 0)) {
1900                 if (net_ratelimit()) {
1901                         printk(KERN_DEBUG "%s: ieee80211_beacon_get: "
1902                                "no rate found\n",
1903                                wiphy_name(local->hw.wiphy));
1904                 }
1905                 dev_kfree_skb(skb);
1906                 skb = NULL;
1907                 goto out;
1908         }
1909
1910         info->control.vif = vif;
1911         info->tx_rate_idx = rsel.rate_idx;
1912         if (sdata->bss_conf.use_short_preamble &&
1913             sband->bitrates[rsel.rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE)
1914                 info->flags |= IEEE80211_TX_CTL_SHORT_PREAMBLE;
1915         info->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
1916         info->flags |= IEEE80211_TX_CTL_NO_ACK;
1917         info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
1918         info->control.retry_limit = 1;
1919         info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1920         (*num_beacons)++;
1921 out:
1922         rcu_read_unlock();
1923         return skb;
1924 }
1925 EXPORT_SYMBOL(ieee80211_beacon_get);
1926
1927 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1928                        const void *frame, size_t frame_len,
1929                        const struct ieee80211_tx_info *frame_txctl,
1930                        struct ieee80211_rts *rts)
1931 {
1932         const struct ieee80211_hdr *hdr = frame;
1933         u16 fctl;
1934
1935         fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
1936         rts->frame_control = cpu_to_le16(fctl);
1937         rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
1938                                                frame_txctl);
1939         memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
1940         memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
1941 }
1942 EXPORT_SYMBOL(ieee80211_rts_get);
1943
1944 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1945                              const void *frame, size_t frame_len,
1946                              const struct ieee80211_tx_info *frame_txctl,
1947                              struct ieee80211_cts *cts)
1948 {
1949         const struct ieee80211_hdr *hdr = frame;
1950         u16 fctl;
1951
1952         fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
1953         cts->frame_control = cpu_to_le16(fctl);
1954         cts->duration = ieee80211_ctstoself_duration(hw, vif,
1955                                                      frame_len, frame_txctl);
1956         memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
1957 }
1958 EXPORT_SYMBOL(ieee80211_ctstoself_get);
1959
1960 struct sk_buff *
1961 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
1962                           struct ieee80211_vif *vif)
1963 {
1964         struct ieee80211_local *local = hw_to_local(hw);
1965         struct sk_buff *skb = NULL;
1966         struct sta_info *sta;
1967         struct ieee80211_tx_data tx;
1968         struct net_device *bdev;
1969         struct ieee80211_sub_if_data *sdata;
1970         struct ieee80211_if_ap *bss = NULL;
1971         struct beacon_data *beacon;
1972         struct ieee80211_tx_info *info;
1973
1974         sdata = vif_to_sdata(vif);
1975         bdev = sdata->dev;
1976         bss = &sdata->u.ap;
1977
1978         if (!bss)
1979                 return NULL;
1980
1981         rcu_read_lock();
1982         beacon = rcu_dereference(bss->beacon);
1983
1984         if (sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon || !beacon->head)
1985                 goto out;
1986
1987         if (bss->dtim_count != 0)
1988                 goto out; /* send buffered bc/mc only after DTIM beacon */
1989
1990         while (1) {
1991                 skb = skb_dequeue(&bss->ps_bc_buf);
1992                 if (!skb)
1993                         goto out;
1994                 local->total_ps_buffered--;
1995
1996                 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
1997                         struct ieee80211_hdr *hdr =
1998                                 (struct ieee80211_hdr *) skb->data;
1999                         /* more buffered multicast/broadcast frames ==> set
2000                          * MoreData flag in IEEE 802.11 header to inform PS
2001                          * STAs */
2002                         hdr->frame_control |=
2003                                 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2004                 }
2005
2006                 if (!ieee80211_tx_prepare(&tx, skb, local->mdev))
2007                         break;
2008                 dev_kfree_skb_any(skb);
2009         }
2010
2011         info = IEEE80211_SKB_CB(skb);
2012
2013         sta = tx.sta;
2014         tx.flags |= IEEE80211_TX_PS_BUFFERED;
2015         tx.channel = local->hw.conf.channel;
2016         info->band = tx.channel->band;
2017
2018         if (invoke_tx_handlers(&tx))
2019                 skb = NULL;
2020  out:
2021         rcu_read_unlock();
2022
2023         return skb;
2024 }
2025 EXPORT_SYMBOL(ieee80211_get_buffered_bc);