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