blob: 1cd58e01f1eefa8d3a276a96053d402f2ca06c15 [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"
29#include "wep.h"
30#include "wpa.h"
31#include "wme.h"
32#include "ieee80211_rate.h"
33
34#define IEEE80211_TX_OK 0
35#define IEEE80211_TX_AGAIN 1
36#define IEEE80211_TX_FRAG_AGAIN 2
37
38/* misc utils */
39
40static inline void ieee80211_include_sequence(struct ieee80211_sub_if_data *sdata,
41 struct ieee80211_hdr *hdr)
42{
43 /* Set the sequence number for this frame. */
44 hdr->seq_ctrl = cpu_to_le16(sdata->sequence);
45
46 /* Increase the sequence number. */
47 sdata->sequence = (sdata->sequence + 0x10) & IEEE80211_SCTL_SEQ;
48}
49
50#ifdef CONFIG_MAC80211_LOWTX_FRAME_DUMP
51static void ieee80211_dump_frame(const char *ifname, const char *title,
52 const struct sk_buff *skb)
53{
54 const struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
55 u16 fc;
56 int hdrlen;
Joe Perches0795af52007-10-03 17:59:30 -070057 DECLARE_MAC_BUF(mac);
Johannes Berge2ebc742007-07-27 15:43:22 +020058
59 printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
60 if (skb->len < 4) {
61 printk("\n");
62 return;
63 }
64
65 fc = le16_to_cpu(hdr->frame_control);
66 hdrlen = ieee80211_get_hdrlen(fc);
67 if (hdrlen > skb->len)
68 hdrlen = skb->len;
69 if (hdrlen >= 4)
70 printk(" FC=0x%04x DUR=0x%04x",
71 fc, le16_to_cpu(hdr->duration_id));
72 if (hdrlen >= 10)
Joe Perches0795af52007-10-03 17:59:30 -070073 printk(" A1=%s", print_mac(mac, hdr->addr1));
Johannes Berge2ebc742007-07-27 15:43:22 +020074 if (hdrlen >= 16)
Joe Perches0795af52007-10-03 17:59:30 -070075 printk(" A2=%s", print_mac(mac, hdr->addr2));
Johannes Berge2ebc742007-07-27 15:43:22 +020076 if (hdrlen >= 24)
Joe Perches0795af52007-10-03 17:59:30 -070077 printk(" A3=%s", print_mac(mac, hdr->addr3));
Johannes Berge2ebc742007-07-27 15:43:22 +020078 if (hdrlen >= 30)
Joe Perches0795af52007-10-03 17:59:30 -070079 printk(" A4=%s", print_mac(mac, hdr->addr4));
Johannes Berge2ebc742007-07-27 15:43:22 +020080 printk("\n");
81}
82#else /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
83static inline void ieee80211_dump_frame(const char *ifname, const char *title,
84 struct sk_buff *skb)
85{
86}
87#endif /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
88
89static u16 ieee80211_duration(struct ieee80211_txrx_data *tx, int group_addr,
90 int next_frag_len)
91{
92 int rate, mrate, erp, dur, i;
93 struct ieee80211_rate *txrate = tx->u.tx.rate;
94 struct ieee80211_local *local = tx->local;
Johannes Berg8318d782008-01-24 19:38:38 +010095 struct ieee80211_supported_band *sband;
Johannes Berge2ebc742007-07-27 15:43:22 +020096
Johannes Berg8318d782008-01-24 19:38:38 +010097 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
98
99 erp = 0;
100 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
101 erp = txrate->flags & IEEE80211_RATE_ERP_G;
Johannes Berge2ebc742007-07-27 15:43:22 +0200102
103 /*
104 * data and mgmt (except PS Poll):
105 * - during CFP: 32768
106 * - during contention period:
107 * if addr1 is group address: 0
108 * if more fragments = 0 and addr1 is individual address: time to
109 * transmit one ACK plus SIFS
110 * if more fragments = 1 and addr1 is individual address: time to
111 * transmit next fragment plus 2 x ACK plus 3 x SIFS
112 *
113 * IEEE 802.11, 9.6:
114 * - control response frame (CTS or ACK) shall be transmitted using the
115 * same rate as the immediately previous frame in the frame exchange
116 * sequence, if this rate belongs to the PHY mandatory rates, or else
117 * at the highest possible rate belonging to the PHY rates in the
118 * BSSBasicRateSet
119 */
120
121 if ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) {
122 /* TODO: These control frames are not currently sent by
123 * 80211.o, but should they be implemented, this function
124 * needs to be updated to support duration field calculation.
125 *
126 * RTS: time needed to transmit pending data/mgmt frame plus
127 * one CTS frame plus one ACK frame plus 3 x SIFS
128 * CTS: duration of immediately previous RTS minus time
129 * required to transmit CTS and its SIFS
130 * ACK: 0 if immediately previous directed data/mgmt had
131 * more=0, with more=1 duration in ACK frame is duration
132 * from previous frame minus time needed to transmit ACK
133 * and its SIFS
134 * PS Poll: BIT(15) | BIT(14) | aid
135 */
136 return 0;
137 }
138
139 /* data/mgmt */
140 if (0 /* FIX: data/mgmt during CFP */)
141 return 32768;
142
143 if (group_addr) /* Group address as the destination - no ACK */
144 return 0;
145
146 /* Individual destination address:
147 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
148 * CTS and ACK frames shall be transmitted using the highest rate in
149 * basic rate set that is less than or equal to the rate of the
150 * immediately previous frame and that is using the same modulation
151 * (CCK or OFDM). If no basic rate set matches with these requirements,
152 * the highest mandatory rate of the PHY that is less than or equal to
153 * the rate of the previous frame is used.
154 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
155 */
156 rate = -1;
Johannes Berg8318d782008-01-24 19:38:38 +0100157 /* use lowest available if everything fails */
158 mrate = sband->bitrates[0].bitrate;
159 for (i = 0; i < sband->n_bitrates; i++) {
160 struct ieee80211_rate *r = &sband->bitrates[i];
161
162 if (r->bitrate > txrate->bitrate)
Johannes Berge2ebc742007-07-27 15:43:22 +0200163 break;
164
Johannes Berg8318d782008-01-24 19:38:38 +0100165 if (tx->sdata->basic_rates & BIT(i))
166 rate = r->bitrate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200167
Johannes Berg8318d782008-01-24 19:38:38 +0100168 switch (sband->band) {
169 case IEEE80211_BAND_2GHZ: {
170 u32 flag;
171 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
172 flag = IEEE80211_RATE_MANDATORY_G;
173 else
174 flag = IEEE80211_RATE_MANDATORY_B;
175 if (r->flags & flag)
176 mrate = r->bitrate;
177 break;
178 }
179 case IEEE80211_BAND_5GHZ:
180 if (r->flags & IEEE80211_RATE_MANDATORY_A)
181 mrate = r->bitrate;
182 break;
183 case IEEE80211_NUM_BANDS:
184 WARN_ON(1);
185 break;
186 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200187 }
188 if (rate == -1) {
189 /* No matching basic rate found; use highest suitable mandatory
190 * PHY rate */
191 rate = mrate;
192 }
193
194 /* Time needed to transmit ACK
195 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
196 * to closest integer */
197
198 dur = ieee80211_frame_duration(local, 10, rate, erp,
Johannes Berg471b3ef2007-12-28 14:32:58 +0100199 tx->sdata->bss_conf.use_short_preamble);
Johannes Berge2ebc742007-07-27 15:43:22 +0200200
201 if (next_frag_len) {
202 /* Frame is fragmented: duration increases with time needed to
203 * transmit next fragment plus ACK and 2 x SIFS. */
204 dur *= 2; /* ACK + SIFS */
205 /* next fragment */
206 dur += ieee80211_frame_duration(local, next_frag_len,
Johannes Berg8318d782008-01-24 19:38:38 +0100207 txrate->bitrate, erp,
Johannes Berg471b3ef2007-12-28 14:32:58 +0100208 tx->sdata->bss_conf.use_short_preamble);
Johannes Berge2ebc742007-07-27 15:43:22 +0200209 }
210
211 return dur;
212}
213
214static inline int __ieee80211_queue_stopped(const struct ieee80211_local *local,
215 int queue)
216{
217 return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
218}
219
220static inline int __ieee80211_queue_pending(const struct ieee80211_local *local,
221 int queue)
222{
223 return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
224}
225
226static int inline is_ieee80211_device(struct net_device *dev,
227 struct net_device *master)
228{
229 return (wdev_priv(dev->ieee80211_ptr) ==
230 wdev_priv(master->ieee80211_ptr));
231}
232
233/* tx handlers */
234
Johannes Berg9ae54c82008-01-31 19:48:20 +0100235static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200236ieee80211_tx_h_check_assoc(struct ieee80211_txrx_data *tx)
237{
238#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
239 struct sk_buff *skb = tx->skb;
240 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
241#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
242 u32 sta_flags;
243
Johannes Berg58d41852007-09-26 17:53:18 +0200244 if (unlikely(tx->flags & IEEE80211_TXRXD_TX_INJECTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100245 return TX_CONTINUE;
Johannes Berg58d41852007-09-26 17:53:18 +0200246
Zhu Yiece8edd2007-11-22 10:53:21 +0800247 if (unlikely(tx->local->sta_sw_scanning) &&
Johannes Berge2ebc742007-07-27 15:43:22 +0200248 ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
249 (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100250 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200251
Jiri Slabybadffb72007-08-28 17:01:54 -0400252 if (tx->flags & IEEE80211_TXRXD_TXPS_BUFFERED)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100253 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200254
255 sta_flags = tx->sta ? tx->sta->flags : 0;
256
Jiri Slabybadffb72007-08-28 17:01:54 -0400257 if (likely(tx->flags & IEEE80211_TXRXD_TXUNICAST)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200258 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
Johannes Berg51fb61e2007-12-19 01:31:27 +0100259 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Berge2ebc742007-07-27 15:43:22 +0200260 (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
261#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700262 DECLARE_MAC_BUF(mac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200263 printk(KERN_DEBUG "%s: dropped data frame to not "
Joe Perches0795af52007-10-03 17:59:30 -0700264 "associated station %s\n",
265 tx->dev->name, print_mac(mac, hdr->addr1));
Johannes Berge2ebc742007-07-27 15:43:22 +0200266#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
267 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100268 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200269 }
270 } else {
271 if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
272 tx->local->num_sta == 0 &&
Johannes Berg51fb61e2007-12-19 01:31:27 +0100273 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200274 /*
275 * No associated STAs - no need to send multicast
276 * frames.
277 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100278 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200279 }
Johannes Berg9ae54c82008-01-31 19:48:20 +0100280 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200281 }
282
Johannes Berg9ae54c82008-01-31 19:48:20 +0100283 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200284}
285
Johannes Berg9ae54c82008-01-31 19:48:20 +0100286static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200287ieee80211_tx_h_sequence(struct ieee80211_txrx_data *tx)
288{
289 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
290
291 if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
292 ieee80211_include_sequence(tx->sdata, hdr);
293
Johannes Berg9ae54c82008-01-31 19:48:20 +0100294 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200295}
296
297/* This function is called whenever the AP is about to exceed the maximum limit
298 * of buffered frames for power saving STAs. This situation should not really
299 * happen often during normal operation, so dropping the oldest buffered packet
300 * from each queue should be OK to make some room for new frames. */
301static void purge_old_ps_buffers(struct ieee80211_local *local)
302{
303 int total = 0, purged = 0;
304 struct sk_buff *skb;
305 struct ieee80211_sub_if_data *sdata;
306 struct sta_info *sta;
307
Johannes Berg79010422007-09-18 17:29:21 -0400308 /*
309 * virtual interfaces are protected by RCU
310 */
311 rcu_read_lock();
312
313 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200314 struct ieee80211_if_ap *ap;
315 if (sdata->dev == local->mdev ||
Johannes Berg51fb61e2007-12-19 01:31:27 +0100316 sdata->vif.type != IEEE80211_IF_TYPE_AP)
Johannes Berge2ebc742007-07-27 15:43:22 +0200317 continue;
318 ap = &sdata->u.ap;
319 skb = skb_dequeue(&ap->ps_bc_buf);
320 if (skb) {
321 purged++;
322 dev_kfree_skb(skb);
323 }
324 total += skb_queue_len(&ap->ps_bc_buf);
325 }
Johannes Berg79010422007-09-18 17:29:21 -0400326 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +0200327
Michael Wube8755e2007-07-27 15:43:23 +0200328 read_lock_bh(&local->sta_lock);
Johannes Berge2ebc742007-07-27 15:43:22 +0200329 list_for_each_entry(sta, &local->sta_list, list) {
330 skb = skb_dequeue(&sta->ps_tx_buf);
331 if (skb) {
332 purged++;
333 dev_kfree_skb(skb);
334 }
335 total += skb_queue_len(&sta->ps_tx_buf);
336 }
Michael Wube8755e2007-07-27 15:43:23 +0200337 read_unlock_bh(&local->sta_lock);
Johannes Berge2ebc742007-07-27 15:43:22 +0200338
339 local->total_ps_buffered = total;
340 printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400341 wiphy_name(local->hw.wiphy), purged);
Johannes Berge2ebc742007-07-27 15:43:22 +0200342}
343
Johannes Berg9ae54c82008-01-31 19:48:20 +0100344static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200345ieee80211_tx_h_multicast_ps_buf(struct ieee80211_txrx_data *tx)
346{
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100347 /*
348 * broadcast/multicast frame
349 *
350 * If any of the associated stations is in power save mode,
351 * the frame is buffered to be sent after DTIM beacon frame.
352 * This is done either by the hardware or us.
353 */
354
355 /* not AP/IBSS or ordered frame */
356 if (!tx->sdata->bss || (tx->fc & IEEE80211_FCTL_ORDER))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100357 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100358
359 /* no stations in PS mode */
360 if (!atomic_read(&tx->sdata->bss->num_sta_ps))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100361 return TX_CONTINUE;
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100362
363 /* buffered in mac80211 */
364 if (tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200365 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
366 purge_old_ps_buffers(tx->local);
367 if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
368 AP_MAX_BC_BUFFER) {
369 if (net_ratelimit()) {
370 printk(KERN_DEBUG "%s: BC TX buffer full - "
371 "dropping the oldest frame\n",
372 tx->dev->name);
373 }
374 dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
375 } else
376 tx->local->total_ps_buffered++;
377 skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100378 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200379 }
380
Johannes Berg7d54d0dd2007-12-19 01:31:25 +0100381 /* buffered in hardware */
382 tx->u.tx.control->flags |= IEEE80211_TXCTL_SEND_AFTER_DTIM;
383
Johannes Berg9ae54c82008-01-31 19:48:20 +0100384 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200385}
386
Johannes Berg9ae54c82008-01-31 19:48:20 +0100387static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200388ieee80211_tx_h_unicast_ps_buf(struct ieee80211_txrx_data *tx)
389{
390 struct sta_info *sta = tx->sta;
Joe Perches0795af52007-10-03 17:59:30 -0700391 DECLARE_MAC_BUF(mac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200392
393 if (unlikely(!sta ||
394 ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
395 (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100396 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200397
Johannes Berg4a9a66e2008-02-19 11:31:14 +0100398 if (unlikely((sta->flags & WLAN_STA_PS) &&
399 !(sta->flags & WLAN_STA_PSPOLL))) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200400 struct ieee80211_tx_packet_data *pkt_data;
401#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700402 printk(KERN_DEBUG "STA %s aid %d: PS buffer (entries "
Johannes Berge2ebc742007-07-27 15:43:22 +0200403 "before %d)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700404 print_mac(mac, sta->addr), sta->aid,
Johannes Berge2ebc742007-07-27 15:43:22 +0200405 skb_queue_len(&sta->ps_tx_buf));
406#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berge2ebc742007-07-27 15:43:22 +0200407 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
408 purge_old_ps_buffers(tx->local);
409 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
410 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
411 if (net_ratelimit()) {
Joe Perches0795af52007-10-03 17:59:30 -0700412 printk(KERN_DEBUG "%s: STA %s TX "
Johannes Berge2ebc742007-07-27 15:43:22 +0200413 "buffer full - dropping oldest frame\n",
Joe Perches0795af52007-10-03 17:59:30 -0700414 tx->dev->name, print_mac(mac, sta->addr));
Johannes Berge2ebc742007-07-27 15:43:22 +0200415 }
416 dev_kfree_skb(old);
417 } else
418 tx->local->total_ps_buffered++;
Johannes Berg004c8722008-02-20 11:21:35 +0100419
Johannes Berge2ebc742007-07-27 15:43:22 +0200420 /* Queue frame to be sent after STA sends an PS Poll frame */
Johannes Berg004c8722008-02-20 11:21:35 +0100421 if (skb_queue_empty(&sta->ps_tx_buf))
422 sta_info_set_tim_bit(sta);
423
Johannes Berge2ebc742007-07-27 15:43:22 +0200424 pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
425 pkt_data->jiffies = jiffies;
426 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100427 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200428 }
429#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
430 else if (unlikely(sta->flags & WLAN_STA_PS)) {
Joe Perches0795af52007-10-03 17:59:30 -0700431 printk(KERN_DEBUG "%s: STA %s in PS mode, but pspoll "
Johannes Berge2ebc742007-07-27 15:43:22 +0200432 "set -> send frame\n", tx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700433 print_mac(mac, sta->addr));
Johannes Berge2ebc742007-07-27 15:43:22 +0200434 }
435#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berg4a9a66e2008-02-19 11:31:14 +0100436 sta->flags &= ~WLAN_STA_PSPOLL;
Johannes Berge2ebc742007-07-27 15:43:22 +0200437
Johannes Berg9ae54c82008-01-31 19:48:20 +0100438 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200439}
440
Johannes Berg9ae54c82008-01-31 19:48:20 +0100441static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200442ieee80211_tx_h_ps_buf(struct ieee80211_txrx_data *tx)
443{
Jiri Slabybadffb72007-08-28 17:01:54 -0400444 if (unlikely(tx->flags & IEEE80211_TXRXD_TXPS_BUFFERED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100445 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200446
Jiri Slabybadffb72007-08-28 17:01:54 -0400447 if (tx->flags & IEEE80211_TXRXD_TXUNICAST)
Johannes Berge2ebc742007-07-27 15:43:22 +0200448 return ieee80211_tx_h_unicast_ps_buf(tx);
449 else
450 return ieee80211_tx_h_multicast_ps_buf(tx);
451}
452
Johannes Berg9ae54c82008-01-31 19:48:20 +0100453static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200454ieee80211_tx_h_select_key(struct ieee80211_txrx_data *tx)
455{
Johannes Bergd4e46a32007-09-14 11:10:24 -0400456 struct ieee80211_key *key;
Johannes Berg176e4f82007-12-18 15:27:47 +0100457 u16 fc = tx->fc;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400458
Johannes Berge2ebc742007-07-27 15:43:22 +0200459 if (unlikely(tx->u.tx.control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
460 tx->key = NULL;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400461 else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
462 tx->key = key;
463 else if ((key = rcu_dereference(tx->sdata->default_key)))
464 tx->key = key;
Johannes Berge2ebc742007-07-27 15:43:22 +0200465 else if (tx->sdata->drop_unencrypted &&
Johannes Berg678f5f712007-12-19 01:31:23 +0100466 !(tx->u.tx.control->flags & IEEE80211_TXCTL_EAPOL_FRAME) &&
467 !(tx->flags & IEEE80211_TXRXD_TX_INJECTED)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200468 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100469 return TX_DROP;
Johannes Berg176e4f82007-12-18 15:27:47 +0100470 } else
Johannes Berge2ebc742007-07-27 15:43:22 +0200471 tx->key = NULL;
472
473 if (tx->key) {
Johannes Berg176e4f82007-12-18 15:27:47 +0100474 u16 ftype, stype;
475
Johannes Berge2ebc742007-07-27 15:43:22 +0200476 tx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400477 /* TODO: add threshold stuff again */
Johannes Berg176e4f82007-12-18 15:27:47 +0100478
479 switch (tx->key->conf.alg) {
480 case ALG_WEP:
481 ftype = fc & IEEE80211_FCTL_FTYPE;
482 stype = fc & IEEE80211_FCTL_STYPE;
483
484 if (ftype == IEEE80211_FTYPE_MGMT &&
485 stype == IEEE80211_STYPE_AUTH)
486 break;
487 case ALG_TKIP:
488 case ALG_CCMP:
489 if (!WLAN_FC_DATA_PRESENT(fc))
490 tx->key = NULL;
491 break;
492 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200493 }
494
Johannes Berg176e4f82007-12-18 15:27:47 +0100495 if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
496 tx->u.tx.control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
497
Johannes Berg9ae54c82008-01-31 19:48:20 +0100498 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200499}
500
Johannes Berg9ae54c82008-01-31 19:48:20 +0100501static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200502ieee80211_tx_h_fragment(struct ieee80211_txrx_data *tx)
503{
504 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
505 size_t hdrlen, per_fragm, num_fragm, payload_len, left;
506 struct sk_buff **frags, *first, *frag;
507 int i;
508 u16 seq;
509 u8 *pos;
510 int frag_threshold = tx->local->fragmentation_threshold;
511
Jiri Slabybadffb72007-08-28 17:01:54 -0400512 if (!(tx->flags & IEEE80211_TXRXD_FRAGMENTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100513 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200514
515 first = tx->skb;
516
517 hdrlen = ieee80211_get_hdrlen(tx->fc);
518 payload_len = first->len - hdrlen;
519 per_fragm = frag_threshold - hdrlen - FCS_LEN;
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700520 num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
Johannes Berge2ebc742007-07-27 15:43:22 +0200521
522 frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
523 if (!frags)
524 goto fail;
525
526 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
527 seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
528 pos = first->data + hdrlen + per_fragm;
529 left = payload_len - per_fragm;
530 for (i = 0; i < num_fragm - 1; i++) {
531 struct ieee80211_hdr *fhdr;
532 size_t copylen;
533
534 if (left <= 0)
535 goto fail;
536
537 /* reserve enough extra head and tail room for possible
538 * encryption */
539 frag = frags[i] =
540 dev_alloc_skb(tx->local->tx_headroom +
541 frag_threshold +
542 IEEE80211_ENCRYPT_HEADROOM +
543 IEEE80211_ENCRYPT_TAILROOM);
544 if (!frag)
545 goto fail;
546 /* Make sure that all fragments use the same priority so
547 * that they end up using the same TX queue */
548 frag->priority = first->priority;
549 skb_reserve(frag, tx->local->tx_headroom +
550 IEEE80211_ENCRYPT_HEADROOM);
551 fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
552 memcpy(fhdr, first->data, hdrlen);
553 if (i == num_fragm - 2)
554 fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
555 fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
556 copylen = left > per_fragm ? per_fragm : left;
557 memcpy(skb_put(frag, copylen), pos, copylen);
558
559 pos += copylen;
560 left -= copylen;
561 }
562 skb_trim(first, hdrlen + per_fragm);
563
564 tx->u.tx.num_extra_frag = num_fragm - 1;
565 tx->u.tx.extra_frag = frags;
566
Johannes Berg9ae54c82008-01-31 19:48:20 +0100567 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200568
569 fail:
570 printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
571 if (frags) {
572 for (i = 0; i < num_fragm - 1; i++)
573 if (frags[i])
574 dev_kfree_skb(frags[i]);
575 kfree(frags);
576 }
577 I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100578 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200579}
580
Johannes Berg9ae54c82008-01-31 19:48:20 +0100581static ieee80211_tx_result
Johannes Berg6a22a592007-09-26 15:19:41 +0200582ieee80211_tx_h_encrypt(struct ieee80211_txrx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200583{
Johannes Berg6a22a592007-09-26 15:19:41 +0200584 if (!tx->key)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100585 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200586
Johannes Berg6a22a592007-09-26 15:19:41 +0200587 switch (tx->key->conf.alg) {
588 case ALG_WEP:
589 return ieee80211_crypto_wep_encrypt(tx);
590 case ALG_TKIP:
591 return ieee80211_crypto_tkip_encrypt(tx);
592 case ALG_CCMP:
593 return ieee80211_crypto_ccmp_encrypt(tx);
Johannes Berge2ebc742007-07-27 15:43:22 +0200594 }
595
Johannes Berg6a22a592007-09-26 15:19:41 +0200596 /* not reached */
597 WARN_ON(1);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100598 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200599}
600
Johannes Berg9ae54c82008-01-31 19:48:20 +0100601static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200602ieee80211_tx_h_rate_ctrl(struct ieee80211_txrx_data *tx)
603{
Mattias Nissler1abbe492007-12-20 13:50:07 +0100604 struct rate_selection rsel;
Johannes Berg8318d782008-01-24 19:38:38 +0100605 struct ieee80211_supported_band *sband;
606
607 sband = tx->local->hw.wiphy->bands[tx->local->hw.conf.channel->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200608
Johannes Berg58d41852007-09-26 17:53:18 +0200609 if (likely(!tx->u.tx.rate)) {
Johannes Berg8318d782008-01-24 19:38:38 +0100610 rate_control_get_rate(tx->dev, sband, tx->skb, &rsel);
Mattias Nissler1abbe492007-12-20 13:50:07 +0100611 tx->u.tx.rate = rsel.rate;
Johannes Berg8318d782008-01-24 19:38:38 +0100612 if (unlikely(rsel.probe)) {
Johannes Berg58d41852007-09-26 17:53:18 +0200613 tx->u.tx.control->flags |=
614 IEEE80211_TXCTL_RATE_CTRL_PROBE;
615 tx->flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
Johannes Berg8318d782008-01-24 19:38:38 +0100616 tx->u.tx.control->alt_retry_rate = tx->u.tx.rate;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100617 tx->u.tx.rate = rsel.probe;
Johannes Berg58d41852007-09-26 17:53:18 +0200618 } else
Johannes Berg8318d782008-01-24 19:38:38 +0100619 tx->u.tx.control->alt_retry_rate = NULL;
Johannes Berg58d41852007-09-26 17:53:18 +0200620
621 if (!tx->u.tx.rate)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100622 return TX_DROP;
Johannes Berg58d41852007-09-26 17:53:18 +0200623 } else
Johannes Berg8318d782008-01-24 19:38:38 +0100624 tx->u.tx.control->alt_retry_rate = NULL;
Johannes Berg58d41852007-09-26 17:53:18 +0200625
Johannes Berg8318d782008-01-24 19:38:38 +0100626 if (tx->sdata->bss_conf.use_cts_prot &&
Mattias Nissler1abbe492007-12-20 13:50:07 +0100627 (tx->flags & IEEE80211_TXRXD_FRAGMENTED) && rsel.nonerp) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200628 tx->u.tx.last_frag_rate = tx->u.tx.rate;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100629 if (rsel.probe)
Jiri Slabybadffb72007-08-28 17:01:54 -0400630 tx->flags &= ~IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
631 else
632 tx->flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100633 tx->u.tx.rate = rsel.nonerp;
Johannes Berg8318d782008-01-24 19:38:38 +0100634 tx->u.tx.control->tx_rate = rsel.nonerp;
Johannes Berge2ebc742007-07-27 15:43:22 +0200635 tx->u.tx.control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
636 } else {
637 tx->u.tx.last_frag_rate = tx->u.tx.rate;
Johannes Berg8318d782008-01-24 19:38:38 +0100638 tx->u.tx.control->tx_rate = tx->u.tx.rate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200639 }
Johannes Berg8318d782008-01-24 19:38:38 +0100640 tx->u.tx.control->tx_rate = tx->u.tx.rate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200641
Johannes Berg9ae54c82008-01-31 19:48:20 +0100642 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200643}
644
Johannes Berg9ae54c82008-01-31 19:48:20 +0100645static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200646ieee80211_tx_h_misc(struct ieee80211_txrx_data *tx)
647{
648 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200649 u16 fc = le16_to_cpu(hdr->frame_control);
Johannes Berge2ebc742007-07-27 15:43:22 +0200650 u16 dur;
651 struct ieee80211_tx_control *control = tx->u.tx.control;
Johannes Berge2ebc742007-07-27 15:43:22 +0200652
Johannes Berg58d41852007-09-26 17:53:18 +0200653 if (!control->retry_limit) {
654 if (!is_multicast_ether_addr(hdr->addr1)) {
655 if (tx->skb->len + FCS_LEN > tx->local->rts_threshold
656 && tx->local->rts_threshold <
657 IEEE80211_MAX_RTS_THRESHOLD) {
658 control->flags |=
659 IEEE80211_TXCTL_USE_RTS_CTS;
660 control->flags |=
661 IEEE80211_TXCTL_LONG_RETRY_LIMIT;
662 control->retry_limit =
663 tx->local->long_retry_limit;
664 } else {
665 control->retry_limit =
666 tx->local->short_retry_limit;
667 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200668 } else {
Johannes Berg58d41852007-09-26 17:53:18 +0200669 control->retry_limit = 1;
Johannes Berge2ebc742007-07-27 15:43:22 +0200670 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200671 }
672
Jiri Slabybadffb72007-08-28 17:01:54 -0400673 if (tx->flags & IEEE80211_TXRXD_FRAGMENTED) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200674 /* Do not use multiple retry rates when sending fragmented
675 * frames.
676 * TODO: The last fragment could still use multiple retry
677 * rates. */
Johannes Berg8318d782008-01-24 19:38:38 +0100678 control->alt_retry_rate = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +0200679 }
680
681 /* Use CTS protection for unicast frames sent using extended rates if
682 * there are associated non-ERP stations and RTS/CTS is not configured
683 * for the frame. */
Johannes Berg8318d782008-01-24 19:38:38 +0100684 if ((tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE) &&
685 (tx->u.tx.rate->flags & IEEE80211_RATE_ERP_G) &&
Jiri Slabybadffb72007-08-28 17:01:54 -0400686 (tx->flags & IEEE80211_TXRXD_TXUNICAST) &&
Johannes Berg471b3ef2007-12-28 14:32:58 +0100687 tx->sdata->bss_conf.use_cts_prot &&
Johannes Berge2ebc742007-07-27 15:43:22 +0200688 !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
689 control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
690
Daniel Drake7e9ed182007-07-27 15:43:24 +0200691 /* Transmit data frames using short preambles if the driver supports
692 * short preambles at the selected rate and short preambles are
693 * available on the network at the current point in time. */
694 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100695 (tx->u.tx.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
Johannes Berg471b3ef2007-12-28 14:32:58 +0100696 tx->sdata->bss_conf.use_short_preamble &&
Daniel Drake7e9ed182007-07-27 15:43:24 +0200697 (!tx->sta || (tx->sta->flags & WLAN_STA_SHORT_PREAMBLE))) {
Johannes Berg8318d782008-01-24 19:38:38 +0100698 tx->u.tx.control->flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200699 }
700
Johannes Berge2ebc742007-07-27 15:43:22 +0200701 /* Setup duration field for the first fragment of the frame. Duration
702 * for remaining fragments will be updated when they are being sent
703 * to low-level driver in ieee80211_tx(). */
704 dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
Jiri Slabybadffb72007-08-28 17:01:54 -0400705 (tx->flags & IEEE80211_TXRXD_FRAGMENTED) ?
706 tx->u.tx.extra_frag[0]->len : 0);
Johannes Berge2ebc742007-07-27 15:43:22 +0200707 hdr->duration_id = cpu_to_le16(dur);
708
709 if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
710 (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
Johannes Berg8318d782008-01-24 19:38:38 +0100711 struct ieee80211_supported_band *sband;
712 struct ieee80211_rate *rate, *baserate;
713 int idx;
714
715 sband = tx->local->hw.wiphy->bands[
716 tx->local->hw.conf.channel->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200717
718 /* Do not use multiple retry rates when using RTS/CTS */
Johannes Berg8318d782008-01-24 19:38:38 +0100719 control->alt_retry_rate = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +0200720
721 /* Use min(data rate, max base rate) as CTS/RTS rate */
722 rate = tx->u.tx.rate;
Johannes Berg8318d782008-01-24 19:38:38 +0100723 baserate = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +0200724
Johannes Berg8318d782008-01-24 19:38:38 +0100725 for (idx = 0; idx < sband->n_bitrates; idx++) {
726 if (sband->bitrates[idx].bitrate > rate->bitrate)
727 continue;
728 if (tx->sdata->basic_rates & BIT(idx) &&
729 (!baserate ||
730 (baserate->bitrate < sband->bitrates[idx].bitrate)))
731 baserate = &sband->bitrates[idx];
732 }
733
734 if (baserate)
735 control->rts_cts_rate = baserate;
736 else
737 control->rts_cts_rate = &sband->bitrates[0];
Johannes Berge2ebc742007-07-27 15:43:22 +0200738 }
739
740 if (tx->sta) {
741 tx->sta->tx_packets++;
742 tx->sta->tx_fragments++;
743 tx->sta->tx_bytes += tx->skb->len;
744 if (tx->u.tx.extra_frag) {
745 int i;
746 tx->sta->tx_fragments += tx->u.tx.num_extra_frag;
747 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
748 tx->sta->tx_bytes +=
749 tx->u.tx.extra_frag[i]->len;
750 }
751 }
752 }
753
Johannes Berg9ae54c82008-01-31 19:48:20 +0100754 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200755}
756
Johannes Berg9ae54c82008-01-31 19:48:20 +0100757static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200758ieee80211_tx_h_load_stats(struct ieee80211_txrx_data *tx)
759{
760 struct ieee80211_local *local = tx->local;
Johannes Berge2ebc742007-07-27 15:43:22 +0200761 struct sk_buff *skb = tx->skb;
762 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
763 u32 load = 0, hdrtime;
Johannes Berg8318d782008-01-24 19:38:38 +0100764 struct ieee80211_rate *rate = tx->u.tx.rate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200765
766 /* TODO: this could be part of tx_status handling, so that the number
767 * of retries would be known; TX rate should in that case be stored
768 * somewhere with the packet */
769
770 /* Estimate total channel use caused by this frame */
771
772 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
773 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
774
Johannes Berg8318d782008-01-24 19:38:38 +0100775 if (tx->u.tx.channel->band == IEEE80211_BAND_5GHZ ||
776 (tx->u.tx.channel->band == IEEE80211_BAND_2GHZ &&
777 rate->flags & IEEE80211_RATE_ERP_G))
Johannes Berge2ebc742007-07-27 15:43:22 +0200778 hdrtime = CHAN_UTIL_HDR_SHORT;
779 else
780 hdrtime = CHAN_UTIL_HDR_LONG;
781
782 load = hdrtime;
783 if (!is_multicast_ether_addr(hdr->addr1))
784 load += hdrtime;
785
786 if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
787 load += 2 * hdrtime;
788 else if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
789 load += hdrtime;
790
Johannes Berg8318d782008-01-24 19:38:38 +0100791 /* TODO: optimise again */
792 load += skb->len * CHAN_UTIL_RATE_LCM / rate->bitrate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200793
794 if (tx->u.tx.extra_frag) {
795 int i;
796 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
797 load += 2 * hdrtime;
798 load += tx->u.tx.extra_frag[i]->len *
Johannes Berg8318d782008-01-24 19:38:38 +0100799 tx->u.tx.rate->bitrate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200800 }
801 }
802
803 /* Divide channel_use by 8 to avoid wrapping around the counter */
804 load >>= CHAN_UTIL_SHIFT;
805 local->channel_use_raw += load;
806 if (tx->sta)
807 tx->sta->channel_use_raw += load;
808 tx->sdata->channel_use_raw += load;
809
Johannes Berg9ae54c82008-01-31 19:48:20 +0100810 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200811}
812
Johannes Berge2ebc742007-07-27 15:43:22 +0200813
Johannes Berg58905292008-01-31 19:48:25 +0100814typedef ieee80211_tx_result (*ieee80211_tx_handler)(struct ieee80211_txrx_data *);
815static ieee80211_tx_handler ieee80211_tx_handlers[] =
Johannes Berge2ebc742007-07-27 15:43:22 +0200816{
817 ieee80211_tx_h_check_assoc,
818 ieee80211_tx_h_sequence,
819 ieee80211_tx_h_ps_buf,
820 ieee80211_tx_h_select_key,
821 ieee80211_tx_h_michael_mic_add,
822 ieee80211_tx_h_fragment,
Johannes Berg6a22a592007-09-26 15:19:41 +0200823 ieee80211_tx_h_encrypt,
Johannes Berge2ebc742007-07-27 15:43:22 +0200824 ieee80211_tx_h_rate_ctrl,
825 ieee80211_tx_h_misc,
826 ieee80211_tx_h_load_stats,
827 NULL
828};
829
830/* actual transmit path */
831
832/*
833 * deal with packet injection down monitor interface
834 * with Radiotap Header -- only called for monitor mode interface
835 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100836static ieee80211_tx_result
Johannes Berg58d41852007-09-26 17:53:18 +0200837__ieee80211_parse_tx_radiotap(struct ieee80211_txrx_data *tx,
838 struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +0200839{
840 /*
841 * this is the moment to interpret and discard the radiotap header that
842 * must be at the start of the packet injected in Monitor mode
843 *
844 * Need to take some care with endian-ness since radiotap
845 * args are little-endian
846 */
847
848 struct ieee80211_radiotap_iterator iterator;
849 struct ieee80211_radiotap_header *rthdr =
850 (struct ieee80211_radiotap_header *) skb->data;
Johannes Berg8318d782008-01-24 19:38:38 +0100851 struct ieee80211_supported_band *sband;
Johannes Berge2ebc742007-07-27 15:43:22 +0200852 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
Johannes Berg58d41852007-09-26 17:53:18 +0200853 struct ieee80211_tx_control *control = tx->u.tx.control;
Johannes Berge2ebc742007-07-27 15:43:22 +0200854
Johannes Berg8318d782008-01-24 19:38:38 +0100855 sband = tx->local->hw.wiphy->bands[tx->local->hw.conf.channel->band];
856
Johannes Berg58d41852007-09-26 17:53:18 +0200857 control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
858 tx->flags |= IEEE80211_TXRXD_TX_INJECTED;
859 tx->flags &= ~IEEE80211_TXRXD_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200860
861 /*
862 * for every radiotap entry that is present
863 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
864 * entries present, or -EINVAL on error)
865 */
866
867 while (!ret) {
868 int i, target_rate;
869
870 ret = ieee80211_radiotap_iterator_next(&iterator);
871
872 if (ret)
873 continue;
874
875 /* see if this argument is something we can use */
876 switch (iterator.this_arg_index) {
877 /*
878 * You must take care when dereferencing iterator.this_arg
879 * for multibyte types... the pointer is not aligned. Use
880 * get_unaligned((type *)iterator.this_arg) to dereference
881 * iterator.this_arg for type "type" safely on all arches.
882 */
883 case IEEE80211_RADIOTAP_RATE:
884 /*
885 * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
886 * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
887 */
888 target_rate = (*iterator.this_arg) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +0100889 for (i = 0; i < sband->n_bitrates; i++) {
890 struct ieee80211_rate *r;
Johannes Berge2ebc742007-07-27 15:43:22 +0200891
Johannes Berg8318d782008-01-24 19:38:38 +0100892 r = &sband->bitrates[i];
893
894 if (r->bitrate == target_rate) {
Johannes Berg58d41852007-09-26 17:53:18 +0200895 tx->u.tx.rate = r;
896 break;
897 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200898 }
899 break;
900
901 case IEEE80211_RADIOTAP_ANTENNA:
902 /*
903 * radiotap uses 0 for 1st ant, mac80211 is 1 for
904 * 1st ant
905 */
906 control->antenna_sel_tx = (*iterator.this_arg) + 1;
907 break;
908
Johannes Berg8318d782008-01-24 19:38:38 +0100909#if 0
Johannes Berge2ebc742007-07-27 15:43:22 +0200910 case IEEE80211_RADIOTAP_DBM_TX_POWER:
911 control->power_level = *iterator.this_arg;
912 break;
Johannes Berg8318d782008-01-24 19:38:38 +0100913#endif
Johannes Berge2ebc742007-07-27 15:43:22 +0200914
915 case IEEE80211_RADIOTAP_FLAGS:
916 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
917 /*
918 * this indicates that the skb we have been
919 * handed has the 32-bit FCS CRC at the end...
920 * we should react to that by snipping it off
921 * because it will be recomputed and added
922 * on transmission
923 */
924 if (skb->len < (iterator.max_length + FCS_LEN))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100925 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200926
927 skb_trim(skb, skb->len - FCS_LEN);
928 }
Johannes Berg58d41852007-09-26 17:53:18 +0200929 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
930 control->flags &=
931 ~IEEE80211_TXCTL_DO_NOT_ENCRYPT;
932 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
933 tx->flags |= IEEE80211_TXRXD_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200934 break;
935
Johannes Berg58d41852007-09-26 17:53:18 +0200936 /*
937 * Please update the file
938 * Documentation/networking/mac80211-injection.txt
939 * when parsing new fields here.
940 */
941
Johannes Berge2ebc742007-07-27 15:43:22 +0200942 default:
943 break;
944 }
945 }
946
947 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100948 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200949
950 /*
951 * remove the radiotap header
952 * iterator->max_length was sanity-checked against
953 * skb->len by iterator init
954 */
955 skb_pull(skb, iterator.max_length);
956
Johannes Berg9ae54c82008-01-31 19:48:20 +0100957 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200958}
959
Johannes Berg58d41852007-09-26 17:53:18 +0200960/*
961 * initialises @tx
962 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100963static ieee80211_tx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200964__ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
965 struct sk_buff *skb,
966 struct net_device *dev,
967 struct ieee80211_tx_control *control)
968{
969 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg58d41852007-09-26 17:53:18 +0200970 struct ieee80211_hdr *hdr;
Johannes Berge2ebc742007-07-27 15:43:22 +0200971 struct ieee80211_sub_if_data *sdata;
Johannes Berge2ebc742007-07-27 15:43:22 +0200972
973 int hdrlen;
974
975 memset(tx, 0, sizeof(*tx));
976 tx->skb = skb;
977 tx->dev = dev; /* use original interface */
978 tx->local = local;
979 tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg58d41852007-09-26 17:53:18 +0200980 tx->u.tx.control = control;
Johannes Berge2ebc742007-07-27 15:43:22 +0200981 /*
Johannes Berg58d41852007-09-26 17:53:18 +0200982 * Set this flag (used below to indicate "automatic fragmentation"),
983 * it will be cleared/left by radiotap as desired.
Johannes Berge2ebc742007-07-27 15:43:22 +0200984 */
Johannes Berg58d41852007-09-26 17:53:18 +0200985 tx->flags |= IEEE80211_TXRXD_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200986
987 /* process and remove the injection radiotap header */
988 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +0100989 if (unlikely(sdata->vif.type == IEEE80211_IF_TYPE_MNTR)) {
Johannes Berg9ae54c82008-01-31 19:48:20 +0100990 if (__ieee80211_parse_tx_radiotap(tx, skb) == TX_DROP)
991 return TX_DROP;
Johannes Berg58d41852007-09-26 17:53:18 +0200992
Johannes Berge2ebc742007-07-27 15:43:22 +0200993 /*
Johannes Berg58d41852007-09-26 17:53:18 +0200994 * __ieee80211_parse_tx_radiotap has now removed
995 * the radiotap header that was present and pre-filled
996 * 'tx' with tx control information.
Johannes Berge2ebc742007-07-27 15:43:22 +0200997 */
Johannes Berge2ebc742007-07-27 15:43:22 +0200998 }
999
Johannes Berg58d41852007-09-26 17:53:18 +02001000 hdr = (struct ieee80211_hdr *) skb->data;
1001
warmcat24338792007-09-14 11:10:25 -04001002 tx->sta = sta_info_get(local, hdr->addr1);
1003 tx->fc = le16_to_cpu(hdr->frame_control);
Johannes Berg58d41852007-09-26 17:53:18 +02001004
Jiri Slabybadffb72007-08-28 17:01:54 -04001005 if (is_multicast_ether_addr(hdr->addr1)) {
1006 tx->flags &= ~IEEE80211_TXRXD_TXUNICAST;
Johannes Berge2ebc742007-07-27 15:43:22 +02001007 control->flags |= IEEE80211_TXCTL_NO_ACK;
Jiri Slabybadffb72007-08-28 17:01:54 -04001008 } else {
1009 tx->flags |= IEEE80211_TXRXD_TXUNICAST;
Johannes Berge2ebc742007-07-27 15:43:22 +02001010 control->flags &= ~IEEE80211_TXCTL_NO_ACK;
Jiri Slabybadffb72007-08-28 17:01:54 -04001011 }
Johannes Berg58d41852007-09-26 17:53:18 +02001012
1013 if (tx->flags & IEEE80211_TXRXD_FRAGMENTED) {
1014 if ((tx->flags & IEEE80211_TXRXD_TXUNICAST) &&
1015 skb->len + FCS_LEN > local->fragmentation_threshold &&
1016 !local->ops->set_frag_threshold)
1017 tx->flags |= IEEE80211_TXRXD_FRAGMENTED;
1018 else
1019 tx->flags &= ~IEEE80211_TXRXD_FRAGMENTED;
1020 }
1021
Johannes Berge2ebc742007-07-27 15:43:22 +02001022 if (!tx->sta)
Johannes Bergd46e1442008-02-20 23:59:33 +01001023 control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
1024 else if (tx->sta->flags & WLAN_STA_CLEAR_PS_FILT) {
1025 control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
1026 tx->sta->flags &= ~WLAN_STA_CLEAR_PS_FILT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001027 }
Johannes Berg58d41852007-09-26 17:53:18 +02001028
Johannes Berge2ebc742007-07-27 15:43:22 +02001029 hdrlen = ieee80211_get_hdrlen(tx->fc);
1030 if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
1031 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
1032 tx->ethertype = (pos[0] << 8) | pos[1];
1033 }
1034 control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
1035
Johannes Berg9ae54c82008-01-31 19:48:20 +01001036 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001037}
1038
Johannes Berg32bfd352007-12-19 01:31:26 +01001039/*
Johannes Berg58d41852007-09-26 17:53:18 +02001040 * NB: @tx is uninitialised when passed in here
1041 */
1042static int ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
1043 struct sk_buff *skb,
1044 struct net_device *mdev,
1045 struct ieee80211_tx_control *control)
Johannes Berge2ebc742007-07-27 15:43:22 +02001046{
1047 struct ieee80211_tx_packet_data *pkt_data;
1048 struct net_device *dev;
1049
1050 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001051 dev = dev_get_by_index(&init_net, pkt_data->ifindex);
Johannes Berge2ebc742007-07-27 15:43:22 +02001052 if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
1053 dev_put(dev);
1054 dev = NULL;
1055 }
1056 if (unlikely(!dev))
1057 return -ENODEV;
Johannes Berg58d41852007-09-26 17:53:18 +02001058 /* initialises tx with control */
Johannes Berge2ebc742007-07-27 15:43:22 +02001059 __ieee80211_tx_prepare(tx, skb, dev, control);
Johannes Berg32bfd352007-12-19 01:31:26 +01001060 dev_put(dev);
Johannes Berge2ebc742007-07-27 15:43:22 +02001061 return 0;
1062}
1063
1064static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
1065 struct ieee80211_txrx_data *tx)
1066{
1067 struct ieee80211_tx_control *control = tx->u.tx.control;
1068 int ret, i;
1069
1070 if (!ieee80211_qdisc_installed(local->mdev) &&
1071 __ieee80211_queue_stopped(local, 0)) {
1072 netif_stop_queue(local->mdev);
1073 return IEEE80211_TX_AGAIN;
1074 }
1075 if (skb) {
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001076 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
1077 "TX to low-level driver", skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001078 ret = local->ops->tx(local_to_hw(local), skb, control);
1079 if (ret)
1080 return IEEE80211_TX_AGAIN;
1081 local->mdev->trans_start = jiffies;
1082 ieee80211_led_tx(local, 1);
1083 }
1084 if (tx->u.tx.extra_frag) {
1085 control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
1086 IEEE80211_TXCTL_USE_CTS_PROTECT |
Johannes Bergd46e1442008-02-20 23:59:33 +01001087 IEEE80211_TXCTL_CLEAR_PS_FILT |
Johannes Berge2ebc742007-07-27 15:43:22 +02001088 IEEE80211_TXCTL_FIRST_FRAGMENT);
1089 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
1090 if (!tx->u.tx.extra_frag[i])
1091 continue;
1092 if (__ieee80211_queue_stopped(local, control->queue))
1093 return IEEE80211_TX_FRAG_AGAIN;
1094 if (i == tx->u.tx.num_extra_frag) {
Johannes Berg8318d782008-01-24 19:38:38 +01001095 control->tx_rate = tx->u.tx.last_frag_rate;
1096
Jiri Slabybadffb72007-08-28 17:01:54 -04001097 if (tx->flags & IEEE80211_TXRXD_TXPROBE_LAST_FRAG)
Johannes Berge2ebc742007-07-27 15:43:22 +02001098 control->flags |=
1099 IEEE80211_TXCTL_RATE_CTRL_PROBE;
1100 else
1101 control->flags &=
1102 ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
1103 }
1104
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001105 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
Johannes Berge2ebc742007-07-27 15:43:22 +02001106 "TX to low-level driver",
1107 tx->u.tx.extra_frag[i]);
1108 ret = local->ops->tx(local_to_hw(local),
1109 tx->u.tx.extra_frag[i],
1110 control);
1111 if (ret)
1112 return IEEE80211_TX_FRAG_AGAIN;
1113 local->mdev->trans_start = jiffies;
1114 ieee80211_led_tx(local, 1);
1115 tx->u.tx.extra_frag[i] = NULL;
1116 }
1117 kfree(tx->u.tx.extra_frag);
1118 tx->u.tx.extra_frag = NULL;
1119 }
1120 return IEEE80211_TX_OK;
1121}
1122
1123static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
Johannes Bergf9d540e2007-09-28 14:02:09 +02001124 struct ieee80211_tx_control *control)
Johannes Berge2ebc742007-07-27 15:43:22 +02001125{
1126 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1127 struct sta_info *sta;
1128 ieee80211_tx_handler *handler;
1129 struct ieee80211_txrx_data tx;
Johannes Berg9ae54c82008-01-31 19:48:20 +01001130 ieee80211_tx_result res = TX_DROP, res_prepare;
Johannes Berge2ebc742007-07-27 15:43:22 +02001131 int ret, i;
1132
1133 WARN_ON(__ieee80211_queue_pending(local, control->queue));
1134
1135 if (unlikely(skb->len < 10)) {
1136 dev_kfree_skb(skb);
1137 return 0;
1138 }
1139
Johannes Berg58d41852007-09-26 17:53:18 +02001140 /* initialises tx */
Johannes Berge2ebc742007-07-27 15:43:22 +02001141 res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
1142
Johannes Berg9ae54c82008-01-31 19:48:20 +01001143 if (res_prepare == TX_DROP) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001144 dev_kfree_skb(skb);
1145 return 0;
1146 }
1147
Johannes Bergd4e46a32007-09-14 11:10:24 -04001148 /*
1149 * key references are protected using RCU and this requires that
1150 * we are in a read-site RCU section during receive processing
1151 */
1152 rcu_read_lock();
1153
Johannes Berge2ebc742007-07-27 15:43:22 +02001154 sta = tx.sta;
Johannes Berg8318d782008-01-24 19:38:38 +01001155 tx.u.tx.channel = local->hw.conf.channel;
Johannes Berge2ebc742007-07-27 15:43:22 +02001156
Johannes Berg58905292008-01-31 19:48:25 +01001157 for (handler = ieee80211_tx_handlers; *handler != NULL;
Johannes Berg58d41852007-09-26 17:53:18 +02001158 handler++) {
1159 res = (*handler)(&tx);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001160 if (res != TX_CONTINUE)
Johannes Berg58d41852007-09-26 17:53:18 +02001161 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02001162 }
1163
1164 skb = tx.skb; /* handlers are allowed to change skb */
1165
1166 if (sta)
1167 sta_info_put(sta);
1168
Johannes Berg9ae54c82008-01-31 19:48:20 +01001169 if (unlikely(res == TX_DROP)) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001170 I802_DEBUG_INC(local->tx_handlers_drop);
1171 goto drop;
1172 }
1173
Johannes Berg9ae54c82008-01-31 19:48:20 +01001174 if (unlikely(res == TX_QUEUED)) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001175 I802_DEBUG_INC(local->tx_handlers_queued);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001176 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001177 return 0;
1178 }
1179
1180 if (tx.u.tx.extra_frag) {
1181 for (i = 0; i < tx.u.tx.num_extra_frag; i++) {
1182 int next_len, dur;
1183 struct ieee80211_hdr *hdr =
1184 (struct ieee80211_hdr *)
1185 tx.u.tx.extra_frag[i]->data;
1186
1187 if (i + 1 < tx.u.tx.num_extra_frag) {
1188 next_len = tx.u.tx.extra_frag[i + 1]->len;
1189 } else {
1190 next_len = 0;
1191 tx.u.tx.rate = tx.u.tx.last_frag_rate;
Johannes Berge2ebc742007-07-27 15:43:22 +02001192 }
1193 dur = ieee80211_duration(&tx, 0, next_len);
1194 hdr->duration_id = cpu_to_le16(dur);
1195 }
1196 }
1197
1198retry:
1199 ret = __ieee80211_tx(local, skb, &tx);
1200 if (ret) {
1201 struct ieee80211_tx_stored_packet *store =
1202 &local->pending_packet[control->queue];
1203
1204 if (ret == IEEE80211_TX_FRAG_AGAIN)
1205 skb = NULL;
1206 set_bit(IEEE80211_LINK_STATE_PENDING,
1207 &local->state[control->queue]);
1208 smp_mb();
1209 /* When the driver gets out of buffers during sending of
1210 * fragments and calls ieee80211_stop_queue, there is
1211 * a small window between IEEE80211_LINK_STATE_XOFF and
1212 * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
1213 * gets available in that window (i.e. driver calls
1214 * ieee80211_wake_queue), we would end up with ieee80211_tx
1215 * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
1216 * continuing transmitting here when that situation is
1217 * possible to have happened. */
1218 if (!__ieee80211_queue_stopped(local, control->queue)) {
1219 clear_bit(IEEE80211_LINK_STATE_PENDING,
1220 &local->state[control->queue]);
1221 goto retry;
1222 }
1223 memcpy(&store->control, control,
1224 sizeof(struct ieee80211_tx_control));
1225 store->skb = skb;
1226 store->extra_frag = tx.u.tx.extra_frag;
1227 store->num_extra_frag = tx.u.tx.num_extra_frag;
Johannes Berge2ebc742007-07-27 15:43:22 +02001228 store->last_frag_rate = tx.u.tx.last_frag_rate;
Jiri Slabybadffb72007-08-28 17:01:54 -04001229 store->last_frag_rate_ctrl_probe =
1230 !!(tx.flags & IEEE80211_TXRXD_TXPROBE_LAST_FRAG);
Johannes Berge2ebc742007-07-27 15:43:22 +02001231 }
Johannes Bergd4e46a32007-09-14 11:10:24 -04001232 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001233 return 0;
1234
1235 drop:
1236 if (skb)
1237 dev_kfree_skb(skb);
1238 for (i = 0; i < tx.u.tx.num_extra_frag; i++)
1239 if (tx.u.tx.extra_frag[i])
1240 dev_kfree_skb(tx.u.tx.extra_frag[i]);
1241 kfree(tx.u.tx.extra_frag);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001242 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001243 return 0;
1244}
1245
1246/* device xmit handlers */
1247
1248int ieee80211_master_start_xmit(struct sk_buff *skb,
1249 struct net_device *dev)
1250{
1251 struct ieee80211_tx_control control;
1252 struct ieee80211_tx_packet_data *pkt_data;
1253 struct net_device *odev = NULL;
1254 struct ieee80211_sub_if_data *osdata;
1255 int headroom;
1256 int ret;
1257
1258 /*
1259 * copy control out of the skb so other people can use skb->cb
1260 */
1261 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1262 memset(&control, 0, sizeof(struct ieee80211_tx_control));
1263
1264 if (pkt_data->ifindex)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001265 odev = dev_get_by_index(&init_net, pkt_data->ifindex);
Johannes Berge2ebc742007-07-27 15:43:22 +02001266 if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
1267 dev_put(odev);
1268 odev = NULL;
1269 }
1270 if (unlikely(!odev)) {
1271#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1272 printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
1273 "originating device\n", dev->name);
1274#endif
1275 dev_kfree_skb(skb);
1276 return 0;
1277 }
1278 osdata = IEEE80211_DEV_TO_SUB_IF(odev);
1279
1280 headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
1281 if (skb_headroom(skb) < headroom) {
1282 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
1283 dev_kfree_skb(skb);
1284 dev_put(odev);
1285 return 0;
1286 }
1287 }
1288
Johannes Berg32bfd352007-12-19 01:31:26 +01001289 control.vif = &osdata->vif;
Johannes Berg51fb61e2007-12-19 01:31:27 +01001290 control.type = osdata->vif.type;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001291 if (pkt_data->flags & IEEE80211_TXPD_REQ_TX_STATUS)
Johannes Berge2ebc742007-07-27 15:43:22 +02001292 control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001293 if (pkt_data->flags & IEEE80211_TXPD_DO_NOT_ENCRYPT)
Johannes Berge2ebc742007-07-27 15:43:22 +02001294 control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001295 if (pkt_data->flags & IEEE80211_TXPD_REQUEUE)
Johannes Berge2ebc742007-07-27 15:43:22 +02001296 control.flags |= IEEE80211_TXCTL_REQUEUE;
Johannes Berg678f5f712007-12-19 01:31:23 +01001297 if (pkt_data->flags & IEEE80211_TXPD_EAPOL_FRAME)
1298 control.flags |= IEEE80211_TXCTL_EAPOL_FRAME;
Ron Rindjunsky9e723492008-01-28 14:07:18 +02001299 if (pkt_data->flags & IEEE80211_TXPD_AMPDU)
1300 control.flags |= IEEE80211_TXCTL_AMPDU;
Johannes Berge2ebc742007-07-27 15:43:22 +02001301 control.queue = pkt_data->queue;
1302
Johannes Bergf9d540e2007-09-28 14:02:09 +02001303 ret = ieee80211_tx(odev, skb, &control);
Johannes Berge2ebc742007-07-27 15:43:22 +02001304 dev_put(odev);
1305
1306 return ret;
1307}
1308
1309int ieee80211_monitor_start_xmit(struct sk_buff *skb,
1310 struct net_device *dev)
1311{
1312 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1313 struct ieee80211_tx_packet_data *pkt_data;
1314 struct ieee80211_radiotap_header *prthdr =
1315 (struct ieee80211_radiotap_header *)skb->data;
Andy Green9b8a74e2007-07-27 15:43:24 +02001316 u16 len_rthdr;
Johannes Berge2ebc742007-07-27 15:43:22 +02001317
Andy Green9b8a74e2007-07-27 15:43:24 +02001318 /* check for not even having the fixed radiotap header part */
1319 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1320 goto fail; /* too short to be possibly valid */
1321
1322 /* is it a header version we can trust to find length from? */
1323 if (unlikely(prthdr->it_version))
1324 goto fail; /* only version 0 is supported */
1325
1326 /* then there must be a radiotap header with a length we can use */
1327 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1328
1329 /* does the skb contain enough to deliver on the alleged length? */
1330 if (unlikely(skb->len < len_rthdr))
1331 goto fail; /* skb too short for claimed rt header extent */
Johannes Berge2ebc742007-07-27 15:43:22 +02001332
1333 skb->dev = local->mdev;
1334
1335 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1336 memset(pkt_data, 0, sizeof(*pkt_data));
Andy Green9b8a74e2007-07-27 15:43:24 +02001337 /* needed because we set skb device to master */
Johannes Berge2ebc742007-07-27 15:43:22 +02001338 pkt_data->ifindex = dev->ifindex;
Andy Green9b8a74e2007-07-27 15:43:24 +02001339
Jiri Slabye8bf9642007-08-28 17:01:54 -04001340 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001341
Johannes Berge2ebc742007-07-27 15:43:22 +02001342 /*
1343 * fix up the pointers accounting for the radiotap
1344 * header still being in there. We are being given
1345 * a precooked IEEE80211 header so no need for
1346 * normal processing
1347 */
Andy Green9b8a74e2007-07-27 15:43:24 +02001348 skb_set_mac_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02001349 /*
Andy Green9b8a74e2007-07-27 15:43:24 +02001350 * these are just fixed to the end of the rt area since we
1351 * don't have any better information and at this point, nobody cares
Johannes Berge2ebc742007-07-27 15:43:22 +02001352 */
Andy Green9b8a74e2007-07-27 15:43:24 +02001353 skb_set_network_header(skb, len_rthdr);
1354 skb_set_transport_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02001355
Andy Green9b8a74e2007-07-27 15:43:24 +02001356 /* pass the radiotap header up to the next stage intact */
1357 dev_queue_xmit(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001358 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02001359
1360fail:
1361 dev_kfree_skb(skb);
1362 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02001363}
1364
1365/**
1366 * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1367 * subinterfaces (wlan#, WDS, and VLAN interfaces)
1368 * @skb: packet to be sent
1369 * @dev: incoming interface
1370 *
1371 * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1372 * not be freed, and caller is responsible for either retrying later or freeing
1373 * skb).
1374 *
1375 * This function takes in an Ethernet header and encapsulates it with suitable
1376 * IEEE 802.11 header based on which interface the packet is coming in. The
1377 * encapsulated packet will then be passed to master interface, wlan#.11, for
1378 * transmission (through low-level driver).
1379 */
1380int ieee80211_subif_start_xmit(struct sk_buff *skb,
1381 struct net_device *dev)
1382{
1383 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1384 struct ieee80211_tx_packet_data *pkt_data;
1385 struct ieee80211_sub_if_data *sdata;
1386 int ret = 1, head_need;
1387 u16 ethertype, hdrlen, fc;
1388 struct ieee80211_hdr hdr;
1389 const u8 *encaps_data;
1390 int encaps_len, skip_header_bytes;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001391 int nh_pos, h_pos;
Johannes Berge2ebc742007-07-27 15:43:22 +02001392 struct sta_info *sta;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001393 u32 sta_flags = 0;
Johannes Berge2ebc742007-07-27 15:43:22 +02001394
1395 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1396 if (unlikely(skb->len < ETH_HLEN)) {
1397 printk(KERN_DEBUG "%s: short skb (len=%d)\n",
1398 dev->name, skb->len);
1399 ret = 0;
1400 goto fail;
1401 }
1402
1403 nh_pos = skb_network_header(skb) - skb->data;
1404 h_pos = skb_transport_header(skb) - skb->data;
1405
1406 /* convert Ethernet header to proper 802.11 header (based on
1407 * operation mode) */
1408 ethertype = (skb->data[12] << 8) | skb->data[13];
Johannes Berge2ebc742007-07-27 15:43:22 +02001409 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
1410
Johannes Berg51fb61e2007-12-19 01:31:27 +01001411 switch (sdata->vif.type) {
Johannes Bergcf966832007-08-28 17:01:54 -04001412 case IEEE80211_IF_TYPE_AP:
1413 case IEEE80211_IF_TYPE_VLAN:
Johannes Berge2ebc742007-07-27 15:43:22 +02001414 fc |= IEEE80211_FCTL_FROMDS;
1415 /* DA BSSID SA */
1416 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1417 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1418 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1419 hdrlen = 24;
Johannes Bergcf966832007-08-28 17:01:54 -04001420 break;
1421 case IEEE80211_IF_TYPE_WDS:
Johannes Berge2ebc742007-07-27 15:43:22 +02001422 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1423 /* RA TA DA SA */
1424 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1425 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1426 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1427 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1428 hdrlen = 30;
Johannes Bergcf966832007-08-28 17:01:54 -04001429 break;
1430 case IEEE80211_IF_TYPE_STA:
Johannes Berge2ebc742007-07-27 15:43:22 +02001431 fc |= IEEE80211_FCTL_TODS;
1432 /* BSSID SA DA */
1433 memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
1434 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1435 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1436 hdrlen = 24;
Johannes Bergcf966832007-08-28 17:01:54 -04001437 break;
1438 case IEEE80211_IF_TYPE_IBSS:
Johannes Berge2ebc742007-07-27 15:43:22 +02001439 /* DA SA BSSID */
1440 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1441 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1442 memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
1443 hdrlen = 24;
Johannes Bergcf966832007-08-28 17:01:54 -04001444 break;
1445 default:
Johannes Berge2ebc742007-07-27 15:43:22 +02001446 ret = 0;
1447 goto fail;
1448 }
1449
Johannes Berg7d185b82008-01-28 17:11:43 +01001450 /*
1451 * There's no need to try to look up the destination
1452 * if it is a multicast address (which can only happen
1453 * in AP mode)
1454 */
1455 if (!is_multicast_ether_addr(hdr.addr1)) {
1456 sta = sta_info_get(local, hdr.addr1);
1457 if (sta) {
1458 sta_flags = sta->flags;
1459 sta_info_put(sta);
1460 }
Johannes Berge2ebc742007-07-27 15:43:22 +02001461 }
1462
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001463 /* receiver is QoS enabled, use a QoS type frame */
1464 if (sta_flags & WLAN_STA_WME) {
1465 fc |= IEEE80211_STYPE_QOS_DATA;
1466 hdrlen += 2;
1467 }
1468
1469 /*
Johannes Berg238814f2008-01-28 17:19:37 +01001470 * Drop unicast frames to unauthorised stations unless they are
1471 * EAPOL frames from the local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001472 */
Johannes Berg238814f2008-01-28 17:19:37 +01001473 if (unlikely(!is_multicast_ether_addr(hdr.addr1) &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001474 !(sta_flags & WLAN_STA_AUTHORIZED) &&
1475 !(ethertype == ETH_P_PAE &&
1476 compare_ether_addr(dev->dev_addr,
1477 skb->data + ETH_ALEN) == 0))) {
1478#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1479 DECLARE_MAC_BUF(mac);
1480
1481 if (net_ratelimit())
1482 printk(KERN_DEBUG "%s: dropped frame to %s"
1483 " (unauthorized port)\n", dev->name,
1484 print_mac(mac, hdr.addr1));
1485#endif
1486
1487 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
1488
1489 ret = 0;
1490 goto fail;
1491 }
1492
Johannes Berge2ebc742007-07-27 15:43:22 +02001493 hdr.frame_control = cpu_to_le16(fc);
1494 hdr.duration_id = 0;
1495 hdr.seq_ctrl = 0;
1496
1497 skip_header_bytes = ETH_HLEN;
1498 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1499 encaps_data = bridge_tunnel_header;
1500 encaps_len = sizeof(bridge_tunnel_header);
1501 skip_header_bytes -= 2;
1502 } else if (ethertype >= 0x600) {
1503 encaps_data = rfc1042_header;
1504 encaps_len = sizeof(rfc1042_header);
1505 skip_header_bytes -= 2;
1506 } else {
1507 encaps_data = NULL;
1508 encaps_len = 0;
1509 }
1510
1511 skb_pull(skb, skip_header_bytes);
1512 nh_pos -= skip_header_bytes;
1513 h_pos -= skip_header_bytes;
1514
1515 /* TODO: implement support for fragments so that there is no need to
1516 * reallocate and copy payload; it might be enough to support one
1517 * extra fragment that would be copied in the beginning of the frame
1518 * data.. anyway, it would be nice to include this into skb structure
1519 * somehow
1520 *
1521 * There are few options for this:
1522 * use skb->cb as an extra space for 802.11 header
1523 * allocate new buffer if not enough headroom
1524 * make sure that there is enough headroom in every skb by increasing
1525 * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
1526 * alloc_skb() (net/core/skbuff.c)
1527 */
1528 head_need = hdrlen + encaps_len + local->tx_headroom;
1529 head_need -= skb_headroom(skb);
1530
1531 /* We are going to modify skb data, so make a copy of it if happens to
1532 * be cloned. This could happen, e.g., with Linux bridge code passing
1533 * us broadcast frames. */
1534
1535 if (head_need > 0 || skb_cloned(skb)) {
1536#if 0
1537 printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
1538 "of headroom\n", dev->name, head_need);
1539#endif
1540
1541 if (skb_cloned(skb))
1542 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1543 else
1544 I802_DEBUG_INC(local->tx_expand_skb_head);
1545 /* Since we have to reallocate the buffer, make sure that there
1546 * is enough room for possible WEP IV/ICV and TKIP (8 bytes
1547 * before payload and 12 after). */
1548 if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
1549 12, GFP_ATOMIC)) {
1550 printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
1551 "\n", dev->name);
1552 goto fail;
1553 }
1554 }
1555
1556 if (encaps_data) {
1557 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1558 nh_pos += encaps_len;
1559 h_pos += encaps_len;
1560 }
Johannes Bergc29b9b92007-09-14 11:10:24 -04001561
1562 if (fc & IEEE80211_STYPE_QOS_DATA) {
1563 __le16 *qos_control;
1564
1565 qos_control = (__le16*) skb_push(skb, 2);
1566 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
1567 /*
1568 * Maybe we could actually set some fields here, for now just
1569 * initialise to zero to indicate no special operation.
1570 */
1571 *qos_control = 0;
1572 } else
1573 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1574
Johannes Berge2ebc742007-07-27 15:43:22 +02001575 nh_pos += hdrlen;
1576 h_pos += hdrlen;
1577
1578 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1579 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
1580 pkt_data->ifindex = dev->ifindex;
Johannes Berg678f5f712007-12-19 01:31:23 +01001581 if (ethertype == ETH_P_PAE)
1582 pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
Johannes Berge2ebc742007-07-27 15:43:22 +02001583
1584 skb->dev = local->mdev;
Stephen Hemminger68aae112007-08-24 11:29:34 -07001585 dev->stats.tx_packets++;
1586 dev->stats.tx_bytes += skb->len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001587
1588 /* Update skb pointers to various headers since this modified frame
1589 * is going to go through Linux networking code that may potentially
1590 * need things like pointer to IP header. */
1591 skb_set_mac_header(skb, 0);
1592 skb_set_network_header(skb, nh_pos);
1593 skb_set_transport_header(skb, h_pos);
1594
1595 dev->trans_start = jiffies;
1596 dev_queue_xmit(skb);
1597
1598 return 0;
1599
1600 fail:
1601 if (!ret)
1602 dev_kfree_skb(skb);
1603
1604 return ret;
1605}
1606
Johannes Berge2ebc742007-07-27 15:43:22 +02001607/* helper functions for pending packets for when queues are stopped */
1608
1609void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1610{
1611 int i, j;
1612 struct ieee80211_tx_stored_packet *store;
1613
1614 for (i = 0; i < local->hw.queues; i++) {
1615 if (!__ieee80211_queue_pending(local, i))
1616 continue;
1617 store = &local->pending_packet[i];
1618 kfree_skb(store->skb);
1619 for (j = 0; j < store->num_extra_frag; j++)
1620 kfree_skb(store->extra_frag[j]);
1621 kfree(store->extra_frag);
1622 clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
1623 }
1624}
1625
1626void ieee80211_tx_pending(unsigned long data)
1627{
1628 struct ieee80211_local *local = (struct ieee80211_local *)data;
1629 struct net_device *dev = local->mdev;
1630 struct ieee80211_tx_stored_packet *store;
1631 struct ieee80211_txrx_data tx;
1632 int i, ret, reschedule = 0;
1633
1634 netif_tx_lock_bh(dev);
1635 for (i = 0; i < local->hw.queues; i++) {
1636 if (__ieee80211_queue_stopped(local, i))
1637 continue;
1638 if (!__ieee80211_queue_pending(local, i)) {
1639 reschedule = 1;
1640 continue;
1641 }
1642 store = &local->pending_packet[i];
1643 tx.u.tx.control = &store->control;
1644 tx.u.tx.extra_frag = store->extra_frag;
1645 tx.u.tx.num_extra_frag = store->num_extra_frag;
Johannes Berge2ebc742007-07-27 15:43:22 +02001646 tx.u.tx.last_frag_rate = store->last_frag_rate;
Jiri Slabybadffb72007-08-28 17:01:54 -04001647 tx.flags = 0;
1648 if (store->last_frag_rate_ctrl_probe)
1649 tx.flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
Johannes Berge2ebc742007-07-27 15:43:22 +02001650 ret = __ieee80211_tx(local, store->skb, &tx);
1651 if (ret) {
1652 if (ret == IEEE80211_TX_FRAG_AGAIN)
1653 store->skb = NULL;
1654 } else {
1655 clear_bit(IEEE80211_LINK_STATE_PENDING,
1656 &local->state[i]);
1657 reschedule = 1;
1658 }
1659 }
1660 netif_tx_unlock_bh(dev);
1661 if (reschedule) {
1662 if (!ieee80211_qdisc_installed(dev)) {
1663 if (!__ieee80211_queue_stopped(local, 0))
1664 netif_wake_queue(dev);
1665 } else
1666 netif_schedule(dev);
1667 }
1668}
1669
1670/* functions for drivers to get certain frames */
1671
1672static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
1673 struct ieee80211_if_ap *bss,
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001674 struct sk_buff *skb,
1675 struct beacon_data *beacon)
Johannes Berge2ebc742007-07-27 15:43:22 +02001676{
1677 u8 *pos, *tim;
1678 int aid0 = 0;
1679 int i, have_bits = 0, n1, n2;
1680
1681 /* Generate bitmap for TIM only if there are any STAs in power save
1682 * mode. */
Michael Wube8755e2007-07-27 15:43:23 +02001683 read_lock_bh(&local->sta_lock);
Johannes Berge2ebc742007-07-27 15:43:22 +02001684 if (atomic_read(&bss->num_sta_ps) > 0)
1685 /* in the hope that this is faster than
1686 * checking byte-for-byte */
1687 have_bits = !bitmap_empty((unsigned long*)bss->tim,
1688 IEEE80211_MAX_AID+1);
1689
1690 if (bss->dtim_count == 0)
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001691 bss->dtim_count = beacon->dtim_period - 1;
Johannes Berge2ebc742007-07-27 15:43:22 +02001692 else
1693 bss->dtim_count--;
1694
1695 tim = pos = (u8 *) skb_put(skb, 6);
1696 *pos++ = WLAN_EID_TIM;
1697 *pos++ = 4;
1698 *pos++ = bss->dtim_count;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001699 *pos++ = beacon->dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02001700
1701 if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
1702 aid0 = 1;
1703
1704 if (have_bits) {
1705 /* Find largest even number N1 so that bits numbered 1 through
1706 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
1707 * (N2 + 1) x 8 through 2007 are 0. */
1708 n1 = 0;
1709 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
1710 if (bss->tim[i]) {
1711 n1 = i & 0xfe;
1712 break;
1713 }
1714 }
1715 n2 = n1;
1716 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
1717 if (bss->tim[i]) {
1718 n2 = i;
1719 break;
1720 }
1721 }
1722
1723 /* Bitmap control */
1724 *pos++ = n1 | aid0;
1725 /* Part Virt Bitmap */
1726 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
1727
1728 tim[1] = n2 - n1 + 4;
1729 skb_put(skb, n2 - n1);
1730 } else {
1731 *pos++ = aid0; /* Bitmap control */
1732 *pos++ = 0; /* Part Virt Bitmap */
1733 }
Michael Wube8755e2007-07-27 15:43:23 +02001734 read_unlock_bh(&local->sta_lock);
Johannes Berge2ebc742007-07-27 15:43:22 +02001735}
1736
Johannes Berg32bfd352007-12-19 01:31:26 +01001737struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
1738 struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001739 struct ieee80211_tx_control *control)
1740{
1741 struct ieee80211_local *local = hw_to_local(hw);
1742 struct sk_buff *skb;
1743 struct net_device *bdev;
1744 struct ieee80211_sub_if_data *sdata = NULL;
1745 struct ieee80211_if_ap *ap = NULL;
Mattias Nissler1abbe492007-12-20 13:50:07 +01001746 struct rate_selection rsel;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001747 struct beacon_data *beacon;
Johannes Berg8318d782008-01-24 19:38:38 +01001748 struct ieee80211_supported_band *sband;
1749
1750 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001751
1752 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001753
Johannes Berg32bfd352007-12-19 01:31:26 +01001754 sdata = vif_to_sdata(vif);
1755 bdev = sdata->dev;
1756 ap = &sdata->u.ap;
Johannes Berge2ebc742007-07-27 15:43:22 +02001757
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001758 beacon = rcu_dereference(ap->beacon);
1759
1760 if (!ap || sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001761#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1762 if (net_ratelimit())
Johannes Berg32bfd352007-12-19 01:31:26 +01001763 printk(KERN_DEBUG "no beacon data avail for %s\n",
1764 bdev->name);
Johannes Berge2ebc742007-07-27 15:43:22 +02001765#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001766 skb = NULL;
1767 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02001768 }
1769
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001770 /* headroom, head length, tail length and maximum TIM length */
1771 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
1772 beacon->tail_len + 256);
Johannes Berge2ebc742007-07-27 15:43:22 +02001773 if (!skb)
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001774 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02001775
1776 skb_reserve(skb, local->tx_headroom);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001777 memcpy(skb_put(skb, beacon->head_len), beacon->head,
1778 beacon->head_len);
Johannes Berge2ebc742007-07-27 15:43:22 +02001779
1780 ieee80211_include_sequence(sdata, (struct ieee80211_hdr *)skb->data);
1781
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001782 ieee80211_beacon_add_tim(local, ap, skb, beacon);
Johannes Berge2ebc742007-07-27 15:43:22 +02001783
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001784 if (beacon->tail)
1785 memcpy(skb_put(skb, beacon->tail_len), beacon->tail,
1786 beacon->tail_len);
Johannes Berge2ebc742007-07-27 15:43:22 +02001787
1788 if (control) {
Johannes Berg8318d782008-01-24 19:38:38 +01001789 rate_control_get_rate(local->mdev, sband, skb, &rsel);
Mattias Nissler1abbe492007-12-20 13:50:07 +01001790 if (!rsel.rate) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001791 if (net_ratelimit()) {
Mattias Nissler1abbe492007-12-20 13:50:07 +01001792 printk(KERN_DEBUG "%s: ieee80211_beacon_get: "
1793 "no rate found\n",
1794 wiphy_name(local->hw.wiphy));
Johannes Berge2ebc742007-07-27 15:43:22 +02001795 }
1796 dev_kfree_skb(skb);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001797 skb = NULL;
1798 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02001799 }
1800
Ivo van Doorn0f7054e2008-01-13 14:16:47 +01001801 control->vif = vif;
Johannes Berg8318d782008-01-24 19:38:38 +01001802 control->tx_rate = rsel.rate;
1803 if (sdata->bss_conf.use_short_preamble &&
1804 rsel.rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
1805 control->flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001806 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
Johannes Berge2ebc742007-07-27 15:43:22 +02001807 control->flags |= IEEE80211_TXCTL_NO_ACK;
1808 control->retry_limit = 1;
Johannes Bergd46e1442008-02-20 23:59:33 +01001809 control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001810 }
1811
1812 ap->num_beacons++;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001813
1814 out:
1815 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001816 return skb;
1817}
1818EXPORT_SYMBOL(ieee80211_beacon_get);
1819
Johannes Berg32bfd352007-12-19 01:31:26 +01001820void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001821 const void *frame, size_t frame_len,
1822 const struct ieee80211_tx_control *frame_txctl,
1823 struct ieee80211_rts *rts)
1824{
1825 const struct ieee80211_hdr *hdr = frame;
1826 u16 fctl;
1827
1828 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
1829 rts->frame_control = cpu_to_le16(fctl);
Johannes Berg32bfd352007-12-19 01:31:26 +01001830 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
1831 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02001832 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
1833 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
1834}
1835EXPORT_SYMBOL(ieee80211_rts_get);
1836
Johannes Berg32bfd352007-12-19 01:31:26 +01001837void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001838 const void *frame, size_t frame_len,
1839 const struct ieee80211_tx_control *frame_txctl,
1840 struct ieee80211_cts *cts)
1841{
1842 const struct ieee80211_hdr *hdr = frame;
1843 u16 fctl;
1844
1845 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
1846 cts->frame_control = cpu_to_le16(fctl);
Johannes Berg32bfd352007-12-19 01:31:26 +01001847 cts->duration = ieee80211_ctstoself_duration(hw, vif,
1848 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02001849 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
1850}
1851EXPORT_SYMBOL(ieee80211_ctstoself_get);
1852
1853struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01001854ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
1855 struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001856 struct ieee80211_tx_control *control)
1857{
1858 struct ieee80211_local *local = hw_to_local(hw);
1859 struct sk_buff *skb;
1860 struct sta_info *sta;
1861 ieee80211_tx_handler *handler;
1862 struct ieee80211_txrx_data tx;
Johannes Berg9ae54c82008-01-31 19:48:20 +01001863 ieee80211_tx_result res = TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +02001864 struct net_device *bdev;
1865 struct ieee80211_sub_if_data *sdata;
1866 struct ieee80211_if_ap *bss = NULL;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001867 struct beacon_data *beacon;
Johannes Berge2ebc742007-07-27 15:43:22 +02001868
Johannes Berg32bfd352007-12-19 01:31:26 +01001869 sdata = vif_to_sdata(vif);
1870 bdev = sdata->dev;
1871
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001872
1873 if (!bss)
Johannes Berge2ebc742007-07-27 15:43:22 +02001874 return NULL;
1875
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001876 rcu_read_lock();
1877 beacon = rcu_dereference(bss->beacon);
1878
1879 if (sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon ||
1880 !beacon->head) {
1881 rcu_read_unlock();
1882 return NULL;
1883 }
1884 rcu_read_unlock();
1885
Johannes Berge2ebc742007-07-27 15:43:22 +02001886 if (bss->dtim_count != 0)
1887 return NULL; /* send buffered bc/mc only after DTIM beacon */
1888 memset(control, 0, sizeof(*control));
1889 while (1) {
1890 skb = skb_dequeue(&bss->ps_bc_buf);
1891 if (!skb)
1892 return NULL;
1893 local->total_ps_buffered--;
1894
1895 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
1896 struct ieee80211_hdr *hdr =
1897 (struct ieee80211_hdr *) skb->data;
1898 /* more buffered multicast/broadcast frames ==> set
1899 * MoreData flag in IEEE 802.11 header to inform PS
1900 * STAs */
1901 hdr->frame_control |=
1902 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1903 }
1904
Johannes Berg58d41852007-09-26 17:53:18 +02001905 if (!ieee80211_tx_prepare(&tx, skb, local->mdev, control))
Johannes Berge2ebc742007-07-27 15:43:22 +02001906 break;
1907 dev_kfree_skb_any(skb);
1908 }
1909 sta = tx.sta;
Jiri Slabybadffb72007-08-28 17:01:54 -04001910 tx.flags |= IEEE80211_TXRXD_TXPS_BUFFERED;
Johannes Berg8318d782008-01-24 19:38:38 +01001911 tx.u.tx.channel = local->hw.conf.channel;
Johannes Berge2ebc742007-07-27 15:43:22 +02001912
Johannes Berg58905292008-01-31 19:48:25 +01001913 for (handler = ieee80211_tx_handlers; *handler != NULL; handler++) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001914 res = (*handler)(&tx);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001915 if (res == TX_DROP || res == TX_QUEUED)
Johannes Berge2ebc742007-07-27 15:43:22 +02001916 break;
1917 }
Johannes Berge2ebc742007-07-27 15:43:22 +02001918 skb = tx.skb; /* handlers are allowed to change skb */
1919
Johannes Berg9ae54c82008-01-31 19:48:20 +01001920 if (res == TX_DROP) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001921 I802_DEBUG_INC(local->tx_handlers_drop);
1922 dev_kfree_skb(skb);
1923 skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +01001924 } else if (res == TX_QUEUED) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001925 I802_DEBUG_INC(local->tx_handlers_queued);
1926 skb = NULL;
1927 }
1928
1929 if (sta)
1930 sta_info_put(sta);
1931
1932 return skb;
1933}
1934EXPORT_SYMBOL(ieee80211_get_buffered_bc);