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