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