blob: fc1ffb55ed5cf62fdff0fe65fedffd725439289a [file] [log] [blame]
Johannes Berge2ebc742007-07-27 15:43:22 +02001/*
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>
Johannes Bergd4e46a32007-09-14 11:10:24 -040020#include <linux/rcupdate.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070021#include <net/net_namespace.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020022#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"
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +010029#ifdef CONFIG_MAC80211_MESH
30#include "mesh.h"
31#endif
Johannes Berge2ebc742007-07-27 15:43:22 +020032#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;
Joe Perches0795af52007-10-03 17:59:30 -070060 DECLARE_MAC_BUF(mac);
Johannes Berge2ebc742007-07-27 15:43:22 +020061
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)
Joe Perches0795af52007-10-03 17:59:30 -070076 printk(" A1=%s", print_mac(mac, hdr->addr1));
Johannes Berge2ebc742007-07-27 15:43:22 +020077 if (hdrlen >= 16)
Joe Perches0795af52007-10-03 17:59:30 -070078 printk(" A2=%s", print_mac(mac, hdr->addr2));
Johannes Berge2ebc742007-07-27 15:43:22 +020079 if (hdrlen >= 24)
Joe Perches0795af52007-10-03 17:59:30 -070080 printk(" A3=%s", print_mac(mac, hdr->addr3));
Johannes Berge2ebc742007-07-27 15:43:22 +020081 if (hdrlen >= 30)
Joe Perches0795af52007-10-03 17:59:30 -070082 printk(" A4=%s", print_mac(mac, hdr->addr4));
Johannes Berge2ebc742007-07-27 15:43:22 +020083 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;
Johannes Berg8318d782008-01-24 19:38:38 +010098 struct ieee80211_supported_band *sband;
Johannes Berge2ebc742007-07-27 15:43:22 +020099
Johannes Berg8318d782008-01-24 19:38:38 +0100100 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;
Johannes Berge2ebc742007-07-27 15:43:22 +0200105
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;
Johannes Berg8318d782008-01-24 19:38:38 +0100160 /* 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];
164
165 if (r->bitrate > txrate->bitrate)
Johannes Berge2ebc742007-07-27 15:43:22 +0200166 break;
167
Johannes Berg8318d782008-01-24 19:38:38 +0100168 if (tx->sdata->basic_rates & BIT(i))
169 rate = r->bitrate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200170
Johannes Berg8318d782008-01-24 19:38:38 +0100171 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 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200190 }
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,
Johannes Berg471b3ef2007-12-28 14:32:58 +0100202 tx->sdata->bss_conf.use_short_preamble);
Johannes Berge2ebc742007-07-27 15:43:22 +0200203
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,
Johannes Berg8318d782008-01-24 19:38:38 +0100210 txrate->bitrate, erp,
Johannes Berg471b3ef2007-12-28 14:32:58 +0100211 tx->sdata->bss_conf.use_short_preamble);
Johannes Berge2ebc742007-07-27 15:43:22 +0200212 }
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
Johannes Berg9ae54c82008-01-31 19:48:20 +0100238static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200239ieee80211_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
Johannes Berg58d41852007-09-26 17:53:18 +0200247 if (unlikely(tx->flags & IEEE80211_TXRXD_TX_INJECTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100248 return TX_CONTINUE;
Johannes Berg58d41852007-09-26 17:53:18 +0200249
Zhu Yiece8edd2007-11-22 10:53:21 +0800250 if (unlikely(tx->local->sta_sw_scanning) &&
Johannes Berge2ebc742007-07-27 15:43:22 +0200251 ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
252 (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100253 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200254
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100255 if (tx->sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT)
256 return TX_CONTINUE;
257
Jiri Slabybadffb72007-08-28 17:01:54 -0400258 if (tx->flags & IEEE80211_TXRXD_TXPS_BUFFERED)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100259 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200260
261 sta_flags = tx->sta ? tx->sta->flags : 0;
262
Jiri Slabybadffb72007-08-28 17:01:54 -0400263 if (likely(tx->flags & IEEE80211_TXRXD_TXUNICAST)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200264 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
Johannes Berg51fb61e2007-12-19 01:31:27 +0100265 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Berge2ebc742007-07-27 15:43:22 +0200266 (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
267#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700268 DECLARE_MAC_BUF(mac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200269 printk(KERN_DEBUG "%s: dropped data frame to not "
Joe Perches0795af52007-10-03 17:59:30 -0700270 "associated station %s\n",
271 tx->dev->name, print_mac(mac, hdr->addr1));
Johannes Berge2ebc742007-07-27 15:43:22 +0200272#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
273 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100274 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200275 }
276 } else {
277 if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
278 tx->local->num_sta == 0 &&
Johannes Berg51fb61e2007-12-19 01:31:27 +0100279 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200280 /*
281 * No associated STAs - no need to send multicast
282 * frames.
283 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100284 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200285 }
Johannes Berg9ae54c82008-01-31 19:48:20 +0100286 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200287 }
288
Johannes Berg9ae54c82008-01-31 19:48:20 +0100289 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200290}
291
Johannes Berg9ae54c82008-01-31 19:48:20 +0100292static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200293ieee80211_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
Johannes Berg9ae54c82008-01-31 19:48:20 +0100300 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200301}
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
Johannes Berg79010422007-09-18 17:29:21 -0400314 /*
315 * virtual interfaces are protected by RCU
316 */
317 rcu_read_lock();
318
319 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200320 struct ieee80211_if_ap *ap;
321 if (sdata->dev == local->mdev ||
Johannes Berg51fb61e2007-12-19 01:31:27 +0100322 sdata->vif.type != IEEE80211_IF_TYPE_AP)
Johannes Berge2ebc742007-07-27 15:43:22 +0200323 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 }
Johannes Berg79010422007-09-18 17:29:21 -0400332 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +0200333
Michael Wube8755e2007-07-27 15:43:23 +0200334 read_lock_bh(&local->sta_lock);
Johannes Berge2ebc742007-07-27 15:43:22 +0200335 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 }
Michael Wube8755e2007-07-27 15:43:23 +0200343 read_unlock_bh(&local->sta_lock);
Johannes Berge2ebc742007-07-27 15:43:22 +0200344
345 local->total_ps_buffered = total;
346 printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400347 wiphy_name(local->hw.wiphy), purged);
Johannes Berge2ebc742007-07-27 15:43:22 +0200348}
349
Johannes Berg9ae54c82008-01-31 19:48:20 +0100350static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200351ieee80211_tx_h_multicast_ps_buf(struct ieee80211_txrx_data *tx)
352{
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100353 /*
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))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100363 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100364
365 /* no stations in PS mode */
366 if (!atomic_read(&tx->sdata->bss->num_sta_ps))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100367 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100368
369 /* buffered in mac80211 */
370 if (tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200371 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);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100384 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200385 }
386
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100387 /* buffered in hardware */
388 tx->u.tx.control->flags |= IEEE80211_TXCTL_SEND_AFTER_DTIM;
389
Johannes Berg9ae54c82008-01-31 19:48:20 +0100390 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200391}
392
Johannes Berg9ae54c82008-01-31 19:48:20 +0100393static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200394ieee80211_tx_h_unicast_ps_buf(struct ieee80211_txrx_data *tx)
395{
396 struct sta_info *sta = tx->sta;
Joe Perches0795af52007-10-03 17:59:30 -0700397 DECLARE_MAC_BUF(mac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200398
399 if (unlikely(!sta ||
400 ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
401 (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100402 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200403
Johannes Berg4a9a66e2008-02-19 11:31:14 +0100404 if (unlikely((sta->flags & WLAN_STA_PS) &&
405 !(sta->flags & WLAN_STA_PSPOLL))) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200406 struct ieee80211_tx_packet_data *pkt_data;
407#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700408 printk(KERN_DEBUG "STA %s aid %d: PS buffer (entries "
Johannes Berge2ebc742007-07-27 15:43:22 +0200409 "before %d)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700410 print_mac(mac, sta->addr), sta->aid,
Johannes Berge2ebc742007-07-27 15:43:22 +0200411 skb_queue_len(&sta->ps_tx_buf));
412#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berge2ebc742007-07-27 15:43:22 +0200413 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()) {
Joe Perches0795af52007-10-03 17:59:30 -0700418 printk(KERN_DEBUG "%s: STA %s TX "
Johannes Berge2ebc742007-07-27 15:43:22 +0200419 "buffer full - dropping oldest frame\n",
Joe Perches0795af52007-10-03 17:59:30 -0700420 tx->dev->name, print_mac(mac, sta->addr));
Johannes Berge2ebc742007-07-27 15:43:22 +0200421 }
422 dev_kfree_skb(old);
423 } else
424 tx->local->total_ps_buffered++;
Johannes Berg004c8722008-02-20 11:21:35 +0100425
Johannes Berge2ebc742007-07-27 15:43:22 +0200426 /* Queue frame to be sent after STA sends an PS Poll frame */
Johannes Berg004c8722008-02-20 11:21:35 +0100427 if (skb_queue_empty(&sta->ps_tx_buf))
428 sta_info_set_tim_bit(sta);
429
Johannes Berge2ebc742007-07-27 15:43:22 +0200430 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);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100433 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200434 }
435#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
436 else if (unlikely(sta->flags & WLAN_STA_PS)) {
Joe Perches0795af52007-10-03 17:59:30 -0700437 printk(KERN_DEBUG "%s: STA %s in PS mode, but pspoll "
Johannes Berge2ebc742007-07-27 15:43:22 +0200438 "set -> send frame\n", tx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700439 print_mac(mac, sta->addr));
Johannes Berge2ebc742007-07-27 15:43:22 +0200440 }
441#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berg4a9a66e2008-02-19 11:31:14 +0100442 sta->flags &= ~WLAN_STA_PSPOLL;
Johannes Berge2ebc742007-07-27 15:43:22 +0200443
Johannes Berg9ae54c82008-01-31 19:48:20 +0100444 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200445}
446
Johannes Berg9ae54c82008-01-31 19:48:20 +0100447static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200448ieee80211_tx_h_ps_buf(struct ieee80211_txrx_data *tx)
449{
Jiri Slabybadffb72007-08-28 17:01:54 -0400450 if (unlikely(tx->flags & IEEE80211_TXRXD_TXPS_BUFFERED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100451 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200452
Jiri Slabybadffb72007-08-28 17:01:54 -0400453 if (tx->flags & IEEE80211_TXRXD_TXUNICAST)
Johannes Berge2ebc742007-07-27 15:43:22 +0200454 return ieee80211_tx_h_unicast_ps_buf(tx);
455 else
456 return ieee80211_tx_h_multicast_ps_buf(tx);
457}
458
Johannes Berg9ae54c82008-01-31 19:48:20 +0100459static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200460ieee80211_tx_h_select_key(struct ieee80211_txrx_data *tx)
461{
Johannes Bergd4e46a32007-09-14 11:10:24 -0400462 struct ieee80211_key *key;
Johannes Berg176e4f82007-12-18 15:27:47 +0100463 u16 fc = tx->fc;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400464
Johannes Berge2ebc742007-07-27 15:43:22 +0200465 if (unlikely(tx->u.tx.control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
466 tx->key = NULL;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400467 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;
Johannes Berge2ebc742007-07-27 15:43:22 +0200471 else if (tx->sdata->drop_unencrypted &&
Johannes Berg678f5f712007-12-19 01:31:23 +0100472 !(tx->u.tx.control->flags & IEEE80211_TXCTL_EAPOL_FRAME) &&
473 !(tx->flags & IEEE80211_TXRXD_TX_INJECTED)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200474 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100475 return TX_DROP;
Johannes Berg176e4f82007-12-18 15:27:47 +0100476 } else
Johannes Berge2ebc742007-07-27 15:43:22 +0200477 tx->key = NULL;
478
479 if (tx->key) {
Johannes Berg176e4f82007-12-18 15:27:47 +0100480 u16 ftype, stype;
481
Johannes Berge2ebc742007-07-27 15:43:22 +0200482 tx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400483 /* TODO: add threshold stuff again */
Johannes Berg176e4f82007-12-18 15:27:47 +0100484
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 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200499 }
500
Johannes Berg176e4f82007-12-18 15:27:47 +0100501 if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
502 tx->u.tx.control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
503
Johannes Berg9ae54c82008-01-31 19:48:20 +0100504 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200505}
506
Johannes Berg9ae54c82008-01-31 19:48:20 +0100507static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200508ieee80211_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
Jiri Slabybadffb72007-08-28 17:01:54 -0400518 if (!(tx->flags & IEEE80211_TXRXD_FRAGMENTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100519 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200520
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;
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700526 num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
Johannes Berge2ebc742007-07-27 15:43:22 +0200527
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
Johannes Berg9ae54c82008-01-31 19:48:20 +0100573 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200574
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);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100584 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200585}
586
Johannes Berg9ae54c82008-01-31 19:48:20 +0100587static ieee80211_tx_result
Johannes Berg6a22a592007-09-26 15:19:41 +0200588ieee80211_tx_h_encrypt(struct ieee80211_txrx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200589{
Johannes Berg6a22a592007-09-26 15:19:41 +0200590 if (!tx->key)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100591 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200592
Johannes Berg6a22a592007-09-26 15:19:41 +0200593 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);
Johannes Berge2ebc742007-07-27 15:43:22 +0200600 }
601
Johannes Berg6a22a592007-09-26 15:19:41 +0200602 /* not reached */
603 WARN_ON(1);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100604 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200605}
606
Johannes Berg9ae54c82008-01-31 19:48:20 +0100607static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200608ieee80211_tx_h_rate_ctrl(struct ieee80211_txrx_data *tx)
609{
Mattias Nissler1abbe492007-12-20 13:50:07 +0100610 struct rate_selection rsel;
Johannes Berg8318d782008-01-24 19:38:38 +0100611 struct ieee80211_supported_band *sband;
612
613 sband = tx->local->hw.wiphy->bands[tx->local->hw.conf.channel->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200614
Johannes Berg58d41852007-09-26 17:53:18 +0200615 if (likely(!tx->u.tx.rate)) {
Johannes Berg8318d782008-01-24 19:38:38 +0100616 rate_control_get_rate(tx->dev, sband, tx->skb, &rsel);
Mattias Nissler1abbe492007-12-20 13:50:07 +0100617 tx->u.tx.rate = rsel.rate;
Johannes Berg8318d782008-01-24 19:38:38 +0100618 if (unlikely(rsel.probe)) {
Johannes Berg58d41852007-09-26 17:53:18 +0200619 tx->u.tx.control->flags |=
620 IEEE80211_TXCTL_RATE_CTRL_PROBE;
621 tx->flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
Johannes Berg8318d782008-01-24 19:38:38 +0100622 tx->u.tx.control->alt_retry_rate = tx->u.tx.rate;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100623 tx->u.tx.rate = rsel.probe;
Johannes Berg58d41852007-09-26 17:53:18 +0200624 } else
Johannes Berg8318d782008-01-24 19:38:38 +0100625 tx->u.tx.control->alt_retry_rate = NULL;
Johannes Berg58d41852007-09-26 17:53:18 +0200626
627 if (!tx->u.tx.rate)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100628 return TX_DROP;
Johannes Berg58d41852007-09-26 17:53:18 +0200629 } else
Johannes Berg8318d782008-01-24 19:38:38 +0100630 tx->u.tx.control->alt_retry_rate = NULL;
Johannes Berg58d41852007-09-26 17:53:18 +0200631
Johannes Berg8318d782008-01-24 19:38:38 +0100632 if (tx->sdata->bss_conf.use_cts_prot &&
Mattias Nissler1abbe492007-12-20 13:50:07 +0100633 (tx->flags & IEEE80211_TXRXD_FRAGMENTED) && rsel.nonerp) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200634 tx->u.tx.last_frag_rate = tx->u.tx.rate;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100635 if (rsel.probe)
Jiri Slabybadffb72007-08-28 17:01:54 -0400636 tx->flags &= ~IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
637 else
638 tx->flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100639 tx->u.tx.rate = rsel.nonerp;
Johannes Berg8318d782008-01-24 19:38:38 +0100640 tx->u.tx.control->tx_rate = rsel.nonerp;
Johannes Berge2ebc742007-07-27 15:43:22 +0200641 tx->u.tx.control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
642 } else {
643 tx->u.tx.last_frag_rate = tx->u.tx.rate;
Johannes Berg8318d782008-01-24 19:38:38 +0100644 tx->u.tx.control->tx_rate = tx->u.tx.rate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200645 }
Johannes Berg8318d782008-01-24 19:38:38 +0100646 tx->u.tx.control->tx_rate = tx->u.tx.rate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200647
Johannes Berg9ae54c82008-01-31 19:48:20 +0100648 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200649}
650
Johannes Berg9ae54c82008-01-31 19:48:20 +0100651static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200652ieee80211_tx_h_misc(struct ieee80211_txrx_data *tx)
653{
654 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200655 u16 fc = le16_to_cpu(hdr->frame_control);
Johannes Berge2ebc742007-07-27 15:43:22 +0200656 u16 dur;
657 struct ieee80211_tx_control *control = tx->u.tx.control;
Johannes Berge2ebc742007-07-27 15:43:22 +0200658
Johannes Berg58d41852007-09-26 17:53:18 +0200659 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 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200674 } else {
Johannes Berg58d41852007-09-26 17:53:18 +0200675 control->retry_limit = 1;
Johannes Berge2ebc742007-07-27 15:43:22 +0200676 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200677 }
678
Jiri Slabybadffb72007-08-28 17:01:54 -0400679 if (tx->flags & IEEE80211_TXRXD_FRAGMENTED) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200680 /* Do not use multiple retry rates when sending fragmented
681 * frames.
682 * TODO: The last fragment could still use multiple retry
683 * rates. */
Johannes Berg8318d782008-01-24 19:38:38 +0100684 control->alt_retry_rate = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +0200685 }
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. */
Johannes Berg8318d782008-01-24 19:38:38 +0100690 if ((tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE) &&
691 (tx->u.tx.rate->flags & IEEE80211_RATE_ERP_G) &&
Jiri Slabybadffb72007-08-28 17:01:54 -0400692 (tx->flags & IEEE80211_TXRXD_TXUNICAST) &&
Johannes Berg471b3ef2007-12-28 14:32:58 +0100693 tx->sdata->bss_conf.use_cts_prot &&
Johannes Berge2ebc742007-07-27 15:43:22 +0200694 !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
695 control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
696
Daniel Drake7e9ed182007-07-27 15:43:24 +0200697 /* 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) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100701 (tx->u.tx.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
Johannes Berg471b3ef2007-12-28 14:32:58 +0100702 tx->sdata->bss_conf.use_short_preamble &&
Daniel Drake7e9ed182007-07-27 15:43:24 +0200703 (!tx->sta || (tx->sta->flags & WLAN_STA_SHORT_PREAMBLE))) {
Johannes Berg8318d782008-01-24 19:38:38 +0100704 tx->u.tx.control->flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200705 }
706
Johannes Berge2ebc742007-07-27 15:43:22 +0200707 /* 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),
Jiri Slabybadffb72007-08-28 17:01:54 -0400711 (tx->flags & IEEE80211_TXRXD_FRAGMENTED) ?
712 tx->u.tx.extra_frag[0]->len : 0);
Johannes Berge2ebc742007-07-27 15:43:22 +0200713 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)) {
Johannes Berg8318d782008-01-24 19:38:38 +0100717 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];
Johannes Berge2ebc742007-07-27 15:43:22 +0200723
724 /* Do not use multiple retry rates when using RTS/CTS */
Johannes Berg8318d782008-01-24 19:38:38 +0100725 control->alt_retry_rate = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +0200726
727 /* Use min(data rate, max base rate) as CTS/RTS rate */
728 rate = tx->u.tx.rate;
Johannes Berg8318d782008-01-24 19:38:38 +0100729 baserate = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +0200730
Johannes Berg8318d782008-01-24 19:38:38 +0100731 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 }
739
740 if (baserate)
741 control->rts_cts_rate = baserate;
742 else
743 control->rts_cts_rate = &sband->bitrates[0];
Johannes Berge2ebc742007-07-27 15:43:22 +0200744 }
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
Johannes Berg9ae54c82008-01-31 19:48:20 +0100760 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200761}
762
Johannes Berg9ae54c82008-01-31 19:48:20 +0100763static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200764ieee80211_tx_h_load_stats(struct ieee80211_txrx_data *tx)
765{
766 struct ieee80211_local *local = tx->local;
Johannes Berge2ebc742007-07-27 15:43:22 +0200767 struct sk_buff *skb = tx->skb;
768 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
769 u32 load = 0, hdrtime;
Johannes Berg8318d782008-01-24 19:38:38 +0100770 struct ieee80211_rate *rate = tx->u.tx.rate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200771
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
Johannes Berg8318d782008-01-24 19:38:38 +0100781 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))
Johannes Berge2ebc742007-07-27 15:43:22 +0200784 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
Johannes Berg8318d782008-01-24 19:38:38 +0100797 /* TODO: optimise again */
798 load += skb->len * CHAN_UTIL_RATE_LCM / rate->bitrate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200799
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 *
Johannes Berg8318d782008-01-24 19:38:38 +0100805 tx->u.tx.rate->bitrate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200806 }
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
Johannes Berg9ae54c82008-01-31 19:48:20 +0100816 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200817}
818
Johannes Berge2ebc742007-07-27 15:43:22 +0200819
Johannes Berg58905292008-01-31 19:48:25 +0100820typedef ieee80211_tx_result (*ieee80211_tx_handler)(struct ieee80211_txrx_data *);
821static ieee80211_tx_handler ieee80211_tx_handlers[] =
Johannes Berge2ebc742007-07-27 15:43:22 +0200822{
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,
Johannes Berg6a22a592007-09-26 15:19:41 +0200829 ieee80211_tx_h_encrypt,
Johannes Berge2ebc742007-07-27 15:43:22 +0200830 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 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100842static ieee80211_tx_result
Johannes Berg58d41852007-09-26 17:53:18 +0200843__ieee80211_parse_tx_radiotap(struct ieee80211_txrx_data *tx,
844 struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +0200845{
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;
Johannes Berg8318d782008-01-24 19:38:38 +0100857 struct ieee80211_supported_band *sband;
Johannes Berge2ebc742007-07-27 15:43:22 +0200858 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
Johannes Berg58d41852007-09-26 17:53:18 +0200859 struct ieee80211_tx_control *control = tx->u.tx.control;
Johannes Berge2ebc742007-07-27 15:43:22 +0200860
Johannes Berg8318d782008-01-24 19:38:38 +0100861 sband = tx->local->hw.wiphy->bands[tx->local->hw.conf.channel->band];
862
Johannes Berg58d41852007-09-26 17:53:18 +0200863 control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
864 tx->flags |= IEEE80211_TXRXD_TX_INJECTED;
865 tx->flags &= ~IEEE80211_TXRXD_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200866
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;
Johannes Berg8318d782008-01-24 19:38:38 +0100895 for (i = 0; i < sband->n_bitrates; i++) {
896 struct ieee80211_rate *r;
Johannes Berge2ebc742007-07-27 15:43:22 +0200897
Johannes Berg8318d782008-01-24 19:38:38 +0100898 r = &sband->bitrates[i];
899
900 if (r->bitrate == target_rate) {
Johannes Berg58d41852007-09-26 17:53:18 +0200901 tx->u.tx.rate = r;
902 break;
903 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200904 }
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
Johannes Berg8318d782008-01-24 19:38:38 +0100915#if 0
Johannes Berge2ebc742007-07-27 15:43:22 +0200916 case IEEE80211_RADIOTAP_DBM_TX_POWER:
917 control->power_level = *iterator.this_arg;
918 break;
Johannes Berg8318d782008-01-24 19:38:38 +0100919#endif
Johannes Berge2ebc742007-07-27 15:43:22 +0200920
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))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100931 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200932
933 skb_trim(skb, skb->len - FCS_LEN);
934 }
Johannes Berg58d41852007-09-26 17:53:18 +0200935 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;
Johannes Berge2ebc742007-07-27 15:43:22 +0200940 break;
941
Johannes Berg58d41852007-09-26 17:53:18 +0200942 /*
943 * Please update the file
944 * Documentation/networking/mac80211-injection.txt
945 * when parsing new fields here.
946 */
947
Johannes Berge2ebc742007-07-27 15:43:22 +0200948 default:
949 break;
950 }
951 }
952
953 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100954 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200955
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
Johannes Berg9ae54c82008-01-31 19:48:20 +0100963 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200964}
965
Johannes Berg58d41852007-09-26 17:53:18 +0200966/*
967 * initialises @tx
968 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100969static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200970__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);
Johannes Berg58d41852007-09-26 17:53:18 +0200976 struct ieee80211_hdr *hdr;
Johannes Berge2ebc742007-07-27 15:43:22 +0200977 struct ieee80211_sub_if_data *sdata;
Johannes Berge2ebc742007-07-27 15:43:22 +0200978
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);
Johannes Berg58d41852007-09-26 17:53:18 +0200986 tx->u.tx.control = control;
Johannes Berge2ebc742007-07-27 15:43:22 +0200987 /*
Johannes Berg58d41852007-09-26 17:53:18 +0200988 * Set this flag (used below to indicate "automatic fragmentation"),
989 * it will be cleared/left by radiotap as desired.
Johannes Berge2ebc742007-07-27 15:43:22 +0200990 */
Johannes Berg58d41852007-09-26 17:53:18 +0200991 tx->flags |= IEEE80211_TXRXD_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200992
993 /* process and remove the injection radiotap header */
994 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +0100995 if (unlikely(sdata->vif.type == IEEE80211_IF_TYPE_MNTR)) {
Johannes Berg9ae54c82008-01-31 19:48:20 +0100996 if (__ieee80211_parse_tx_radiotap(tx, skb) == TX_DROP)
997 return TX_DROP;
Johannes Berg58d41852007-09-26 17:53:18 +0200998
Johannes Berge2ebc742007-07-27 15:43:22 +0200999 /*
Johannes Berg58d41852007-09-26 17:53:18 +02001000 * __ieee80211_parse_tx_radiotap has now removed
1001 * the radiotap header that was present and pre-filled
1002 * 'tx' with tx control information.
Johannes Berge2ebc742007-07-27 15:43:22 +02001003 */
Johannes Berge2ebc742007-07-27 15:43:22 +02001004 }
1005
Johannes Berg58d41852007-09-26 17:53:18 +02001006 hdr = (struct ieee80211_hdr *) skb->data;
1007
warmcat24338792007-09-14 11:10:25 -04001008 tx->sta = sta_info_get(local, hdr->addr1);
1009 tx->fc = le16_to_cpu(hdr->frame_control);
Johannes Berg58d41852007-09-26 17:53:18 +02001010
Jiri Slabybadffb72007-08-28 17:01:54 -04001011 if (is_multicast_ether_addr(hdr->addr1)) {
1012 tx->flags &= ~IEEE80211_TXRXD_TXUNICAST;
Johannes Berge2ebc742007-07-27 15:43:22 +02001013 control->flags |= IEEE80211_TXCTL_NO_ACK;
Jiri Slabybadffb72007-08-28 17:01:54 -04001014 } else {
1015 tx->flags |= IEEE80211_TXRXD_TXUNICAST;
Johannes Berge2ebc742007-07-27 15:43:22 +02001016 control->flags &= ~IEEE80211_TXCTL_NO_ACK;
Jiri Slabybadffb72007-08-28 17:01:54 -04001017 }
Johannes Berg58d41852007-09-26 17:53:18 +02001018
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
Johannes Berge2ebc742007-07-27 15:43:22 +02001028 if (!tx->sta)
Johannes Bergd46e1442008-02-20 23:59:33 +01001029 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;
Johannes Berge2ebc742007-07-27 15:43:22 +02001033 }
Johannes Berg58d41852007-09-26 17:53:18 +02001034
Johannes Berge2ebc742007-07-27 15:43:22 +02001035 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
Johannes Berg9ae54c82008-01-31 19:48:20 +01001042 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001043}
1044
Johannes Berg32bfd352007-12-19 01:31:26 +01001045/*
Johannes Berg58d41852007-09-26 17:53:18 +02001046 * 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)
Johannes Berge2ebc742007-07-27 15:43:22 +02001052{
1053 struct ieee80211_tx_packet_data *pkt_data;
1054 struct net_device *dev;
1055
1056 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001057 dev = dev_get_by_index(&init_net, pkt_data->ifindex);
Johannes Berge2ebc742007-07-27 15:43:22 +02001058 if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
1059 dev_put(dev);
1060 dev = NULL;
1061 }
1062 if (unlikely(!dev))
1063 return -ENODEV;
Johannes Berg58d41852007-09-26 17:53:18 +02001064 /* initialises tx with control */
Johannes Berge2ebc742007-07-27 15:43:22 +02001065 __ieee80211_tx_prepare(tx, skb, dev, control);
Johannes Berg32bfd352007-12-19 01:31:26 +01001066 dev_put(dev);
Johannes Berge2ebc742007-07-27 15:43:22 +02001067 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) {
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001082 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
1083 "TX to low-level driver", skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001084 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 |
Johannes Bergd46e1442008-02-20 23:59:33 +01001093 IEEE80211_TXCTL_CLEAR_PS_FILT |
Johannes Berge2ebc742007-07-27 15:43:22 +02001094 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) {
Johannes Berg8318d782008-01-24 19:38:38 +01001101 control->tx_rate = tx->u.tx.last_frag_rate;
1102
Jiri Slabybadffb72007-08-28 17:01:54 -04001103 if (tx->flags & IEEE80211_TXRXD_TXPROBE_LAST_FRAG)
Johannes Berge2ebc742007-07-27 15:43:22 +02001104 control->flags |=
1105 IEEE80211_TXCTL_RATE_CTRL_PROBE;
1106 else
1107 control->flags &=
1108 ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
1109 }
1110
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001111 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
Johannes Berge2ebc742007-07-27 15:43:22 +02001112 "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,
Johannes Bergf9d540e2007-09-28 14:02:09 +02001130 struct ieee80211_tx_control *control)
Johannes Berge2ebc742007-07-27 15:43:22 +02001131{
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;
Johannes Berg9ae54c82008-01-31 19:48:20 +01001136 ieee80211_tx_result res = TX_DROP, res_prepare;
Johannes Berge2ebc742007-07-27 15:43:22 +02001137 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
Johannes Berg58d41852007-09-26 17:53:18 +02001146 /* initialises tx */
Johannes Berge2ebc742007-07-27 15:43:22 +02001147 res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
1148
Johannes Berg9ae54c82008-01-31 19:48:20 +01001149 if (res_prepare == TX_DROP) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001150 dev_kfree_skb(skb);
1151 return 0;
1152 }
1153
Johannes Bergd4e46a32007-09-14 11:10:24 -04001154 /*
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
Johannes Berge2ebc742007-07-27 15:43:22 +02001160 sta = tx.sta;
Johannes Berg8318d782008-01-24 19:38:38 +01001161 tx.u.tx.channel = local->hw.conf.channel;
Johannes Berge2ebc742007-07-27 15:43:22 +02001162
Johannes Berg58905292008-01-31 19:48:25 +01001163 for (handler = ieee80211_tx_handlers; *handler != NULL;
Johannes Berg58d41852007-09-26 17:53:18 +02001164 handler++) {
1165 res = (*handler)(&tx);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001166 if (res != TX_CONTINUE)
Johannes Berg58d41852007-09-26 17:53:18 +02001167 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02001168 }
1169
1170 skb = tx.skb; /* handlers are allowed to change skb */
1171
1172 if (sta)
1173 sta_info_put(sta);
1174
Johannes Berg9ae54c82008-01-31 19:48:20 +01001175 if (unlikely(res == TX_DROP)) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001176 I802_DEBUG_INC(local->tx_handlers_drop);
1177 goto drop;
1178 }
1179
Johannes Berg9ae54c82008-01-31 19:48:20 +01001180 if (unlikely(res == TX_QUEUED)) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001181 I802_DEBUG_INC(local->tx_handlers_queued);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001182 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001183 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;
Johannes Berge2ebc742007-07-27 15:43:22 +02001198 }
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;
Johannes Berge2ebc742007-07-27 15:43:22 +02001234 store->last_frag_rate = tx.u.tx.last_frag_rate;
Jiri Slabybadffb72007-08-28 17:01:54 -04001235 store->last_frag_rate_ctrl_probe =
1236 !!(tx.flags & IEEE80211_TXRXD_TXPROBE_LAST_FRAG);
Johannes Berge2ebc742007-07-27 15:43:22 +02001237 }
Johannes Bergd4e46a32007-09-14 11:10:24 -04001238 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001239 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);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001248 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001249 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)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001271 odev = dev_get_by_index(&init_net, pkt_data->ifindex);
Johannes Berge2ebc742007-07-27 15:43:22 +02001272 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
Johannes Berg32bfd352007-12-19 01:31:26 +01001295 control.vif = &osdata->vif;
Johannes Berg51fb61e2007-12-19 01:31:27 +01001296 control.type = osdata->vif.type;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001297 if (pkt_data->flags & IEEE80211_TXPD_REQ_TX_STATUS)
Johannes Berge2ebc742007-07-27 15:43:22 +02001298 control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001299 if (pkt_data->flags & IEEE80211_TXPD_DO_NOT_ENCRYPT)
Johannes Berge2ebc742007-07-27 15:43:22 +02001300 control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001301 if (pkt_data->flags & IEEE80211_TXPD_REQUEUE)
Johannes Berge2ebc742007-07-27 15:43:22 +02001302 control.flags |= IEEE80211_TXCTL_REQUEUE;
Johannes Berg678f5f712007-12-19 01:31:23 +01001303 if (pkt_data->flags & IEEE80211_TXPD_EAPOL_FRAME)
1304 control.flags |= IEEE80211_TXCTL_EAPOL_FRAME;
Ron Rindjunsky9e723492008-01-28 14:07:18 +02001305 if (pkt_data->flags & IEEE80211_TXPD_AMPDU)
1306 control.flags |= IEEE80211_TXCTL_AMPDU;
Johannes Berge2ebc742007-07-27 15:43:22 +02001307 control.queue = pkt_data->queue;
1308
Johannes Bergf9d540e2007-09-28 14:02:09 +02001309 ret = ieee80211_tx(odev, skb, &control);
Johannes Berge2ebc742007-07-27 15:43:22 +02001310 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;
Andy Green9b8a74e2007-07-27 15:43:24 +02001322 u16 len_rthdr;
Johannes Berge2ebc742007-07-27 15:43:22 +02001323
Andy Green9b8a74e2007-07-27 15:43:24 +02001324 /* 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 */
Johannes Berge2ebc742007-07-27 15:43:22 +02001338
1339 skb->dev = local->mdev;
1340
1341 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1342 memset(pkt_data, 0, sizeof(*pkt_data));
Andy Green9b8a74e2007-07-27 15:43:24 +02001343 /* needed because we set skb device to master */
Johannes Berge2ebc742007-07-27 15:43:22 +02001344 pkt_data->ifindex = dev->ifindex;
Andy Green9b8a74e2007-07-27 15:43:24 +02001345
Jiri Slabye8bf9642007-08-28 17:01:54 -04001346 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001347
Johannes Berge2ebc742007-07-27 15:43:22 +02001348 /*
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 */
Andy Green9b8a74e2007-07-27 15:43:24 +02001354 skb_set_mac_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02001355 /*
Andy Green9b8a74e2007-07-27 15:43:24 +02001356 * 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
Johannes Berge2ebc742007-07-27 15:43:22 +02001358 */
Andy Green9b8a74e2007-07-27 15:43:24 +02001359 skb_set_network_header(skb, len_rthdr);
1360 skb_set_transport_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02001361
Andy Green9b8a74e2007-07-27 15:43:24 +02001362 /* pass the radiotap header up to the next stage intact */
1363 dev_queue_xmit(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001364 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02001365
1366fail:
1367 dev_kfree_skb(skb);
1368 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02001369}
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;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001393 u16 ethertype, hdrlen, meshhdrlen = 0, fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02001394 struct ieee80211_hdr hdr;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001395 struct ieee80211s_hdr mesh_hdr;
Johannes Berge2ebc742007-07-27 15:43:22 +02001396 const u8 *encaps_data;
1397 int encaps_len, skip_header_bytes;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001398 int nh_pos, h_pos;
Johannes Berge2ebc742007-07-27 15:43:22 +02001399 struct sta_info *sta;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001400 u32 sta_flags = 0;
Johannes Berge2ebc742007-07-27 15:43:22 +02001401
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];
Johannes Berge2ebc742007-07-27 15:43:22 +02001416 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
1417
Johannes Berg51fb61e2007-12-19 01:31:27 +01001418 switch (sdata->vif.type) {
Johannes Bergcf966832007-08-28 17:01:54 -04001419 case IEEE80211_IF_TYPE_AP:
1420 case IEEE80211_IF_TYPE_VLAN:
Johannes Berge2ebc742007-07-27 15:43:22 +02001421 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;
Johannes Bergcf966832007-08-28 17:01:54 -04001427 break;
1428 case IEEE80211_IF_TYPE_WDS:
Johannes Berge2ebc742007-07-27 15:43:22 +02001429 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;
Johannes Bergcf966832007-08-28 17:01:54 -04001436 break;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001437#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
Johannes Bergcf966832007-08-28 17:01:54 -04001468 case IEEE80211_IF_TYPE_STA:
Johannes Berge2ebc742007-07-27 15:43:22 +02001469 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;
Johannes Bergcf966832007-08-28 17:01:54 -04001475 break;
1476 case IEEE80211_IF_TYPE_IBSS:
Johannes Berge2ebc742007-07-27 15:43:22 +02001477 /* 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;
Johannes Bergcf966832007-08-28 17:01:54 -04001482 break;
1483 default:
Johannes Berge2ebc742007-07-27 15:43:22 +02001484 ret = 0;
1485 goto fail;
1486 }
1487
Johannes Berg7d185b82008-01-28 17:11:43 +01001488 /*
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 }
Johannes Berge2ebc742007-07-27 15:43:22 +02001499 }
1500
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001501 /* 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 /*
Johannes Berg238814f2008-01-28 17:19:37 +01001508 * Drop unicast frames to unauthorised stations unless they are
1509 * EAPOL frames from the local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001510 */
Johannes Berg238814f2008-01-28 17:19:37 +01001511 if (unlikely(!is_multicast_ether_addr(hdr.addr1) &&
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001512 !(sta_flags & WLAN_STA_AUTHORIZED) &&
1513 !(ethertype == ETH_P_PAE &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001514 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
Johannes Berge2ebc742007-07-27 15:43:22 +02001531 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 */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001566 head_need = hdrlen + encaps_len + meshhdrlen + local->tx_headroom;
Johannes Berge2ebc742007-07-27 15:43:22 +02001567 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 }
Johannes Bergc29b9b92007-09-14 11:10:24 -04001599
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001600 if (meshhdrlen > 0) {
1601 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
1602 nh_pos += meshhdrlen;
1603 h_pos += meshhdrlen;
1604 }
1605
Johannes Bergc29b9b92007-09-14 11:10:24 -04001606 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
Johannes Berge2ebc742007-07-27 15:43:22 +02001619 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;
Johannes Berg678f5f712007-12-19 01:31:23 +01001625 if (ethertype == ETH_P_PAE)
1626 pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
Johannes Berge2ebc742007-07-27 15:43:22 +02001627
1628 skb->dev = local->mdev;
Stephen Hemminger68aae112007-08-24 11:29:34 -07001629 dev->stats.tx_packets++;
1630 dev->stats.tx_bytes += skb->len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001631
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
Johannes Berge2ebc742007-07-27 15:43:22 +02001651/* 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;
Johannes Berge2ebc742007-07-27 15:43:22 +02001690 tx.u.tx.last_frag_rate = store->last_frag_rate;
Jiri Slabybadffb72007-08-28 17:01:54 -04001691 tx.flags = 0;
1692 if (store->last_frag_rate_ctrl_probe)
1693 tx.flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
Johannes Berge2ebc742007-07-27 15:43:22 +02001694 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,
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001718 struct sk_buff *skb,
1719 struct beacon_data *beacon)
Johannes Berge2ebc742007-07-27 15:43:22 +02001720{
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. */
Michael Wube8755e2007-07-27 15:43:23 +02001727 read_lock_bh(&local->sta_lock);
Johannes Berge2ebc742007-07-27 15:43:22 +02001728 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)
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001735 bss->dtim_count = beacon->dtim_period - 1;
Johannes Berge2ebc742007-07-27 15:43:22 +02001736 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;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001743 *pos++ = beacon->dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02001744
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 }
Michael Wube8755e2007-07-27 15:43:23 +02001778 read_unlock_bh(&local->sta_lock);
Johannes Berge2ebc742007-07-27 15:43:22 +02001779}
1780
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001781#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
Johannes Berg32bfd352007-12-19 01:31:26 +01001815struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
1816 struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001817 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;
Mattias Nissler1abbe492007-12-20 13:50:07 +01001824 struct rate_selection rsel;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001825 struct beacon_data *beacon;
Johannes Berg8318d782008-01-24 19:38:38 +01001826 struct ieee80211_supported_band *sband;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001827 int *num_beacons;
1828 int err = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01001829
1830 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001831
1832 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001833
Johannes Berg32bfd352007-12-19 01:31:26 +01001834 sdata = vif_to_sdata(vif);
1835 bdev = sdata->dev;
Johannes Berge2ebc742007-07-27 15:43:22 +02001836
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001837 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 }
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001845
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001846 /* 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 }
1879
1880 if (err) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001881#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1882 if (net_ratelimit())
Johannes Berg32bfd352007-12-19 01:31:26 +01001883 printk(KERN_DEBUG "no beacon data avail for %s\n",
1884 bdev->name);
Johannes Berge2ebc742007-07-27 15:43:22 +02001885#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001886 skb = NULL;
1887 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02001888 }
1889
Johannes Berge2ebc742007-07-27 15:43:22 +02001890 if (control) {
Johannes Berg8318d782008-01-24 19:38:38 +01001891 rate_control_get_rate(local->mdev, sband, skb, &rsel);
Mattias Nissler1abbe492007-12-20 13:50:07 +01001892 if (!rsel.rate) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001893 if (net_ratelimit()) {
Mattias Nissler1abbe492007-12-20 13:50:07 +01001894 printk(KERN_DEBUG "%s: ieee80211_beacon_get: "
1895 "no rate found\n",
1896 wiphy_name(local->hw.wiphy));
Johannes Berge2ebc742007-07-27 15:43:22 +02001897 }
1898 dev_kfree_skb(skb);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001899 skb = NULL;
1900 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02001901 }
1902
Ivo van Doorn0f7054e2008-01-13 14:16:47 +01001903 control->vif = vif;
Johannes Berg8318d782008-01-24 19:38:38 +01001904 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;
Johannes Berge2ebc742007-07-27 15:43:22 +02001908 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
Johannes Berge2ebc742007-07-27 15:43:22 +02001909 control->flags |= IEEE80211_TXCTL_NO_ACK;
1910 control->retry_limit = 1;
Johannes Bergd46e1442008-02-20 23:59:33 +01001911 control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001912 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001913 (*num_beacons)++;
1914out:
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001915 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001916 return skb;
1917}
1918EXPORT_SYMBOL(ieee80211_beacon_get);
1919
Johannes Berg32bfd352007-12-19 01:31:26 +01001920void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001921 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);
Johannes Berg32bfd352007-12-19 01:31:26 +01001930 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
1931 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02001932 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
1933 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
1934}
1935EXPORT_SYMBOL(ieee80211_rts_get);
1936
Johannes Berg32bfd352007-12-19 01:31:26 +01001937void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001938 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);
Johannes Berg32bfd352007-12-19 01:31:26 +01001947 cts->duration = ieee80211_ctstoself_duration(hw, vif,
1948 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02001949 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
1950}
1951EXPORT_SYMBOL(ieee80211_ctstoself_get);
1952
1953struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01001954ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
1955 struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001956 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;
Johannes Berg9ae54c82008-01-31 19:48:20 +01001963 ieee80211_tx_result res = TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +02001964 struct net_device *bdev;
1965 struct ieee80211_sub_if_data *sdata;
1966 struct ieee80211_if_ap *bss = NULL;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001967 struct beacon_data *beacon;
Johannes Berge2ebc742007-07-27 15:43:22 +02001968
Johannes Berg32bfd352007-12-19 01:31:26 +01001969 sdata = vif_to_sdata(vif);
1970 bdev = sdata->dev;
1971
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001972
1973 if (!bss)
Johannes Berge2ebc742007-07-27 15:43:22 +02001974 return NULL;
1975
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001976 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
Johannes Berge2ebc742007-07-27 15:43:22 +02001986 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
Johannes Berg58d41852007-09-26 17:53:18 +02002005 if (!ieee80211_tx_prepare(&tx, skb, local->mdev, control))
Johannes Berge2ebc742007-07-27 15:43:22 +02002006 break;
2007 dev_kfree_skb_any(skb);
2008 }
2009 sta = tx.sta;
Jiri Slabybadffb72007-08-28 17:01:54 -04002010 tx.flags |= IEEE80211_TXRXD_TXPS_BUFFERED;
Johannes Berg8318d782008-01-24 19:38:38 +01002011 tx.u.tx.channel = local->hw.conf.channel;
Johannes Berge2ebc742007-07-27 15:43:22 +02002012
Johannes Berg58905292008-01-31 19:48:25 +01002013 for (handler = ieee80211_tx_handlers; *handler != NULL; handler++) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002014 res = (*handler)(&tx);
Johannes Berg9ae54c82008-01-31 19:48:20 +01002015 if (res == TX_DROP || res == TX_QUEUED)
Johannes Berge2ebc742007-07-27 15:43:22 +02002016 break;
2017 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002018 skb = tx.skb; /* handlers are allowed to change skb */
2019
Johannes Berg9ae54c82008-01-31 19:48:20 +01002020 if (res == TX_DROP) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002021 I802_DEBUG_INC(local->tx_handlers_drop);
2022 dev_kfree_skb(skb);
2023 skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002024 } else if (res == TX_QUEUED) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002025 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);