blob: 666158f02a891fe2037a327b1688ae2dc3f2ecda [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"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040028#include "led.h"
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +010029#include "mesh.h"
Johannes Berge2ebc742007-07-27 15:43:22 +020030#include "wep.h"
31#include "wpa.h"
32#include "wme.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040033#include "rate.h"
Johannes Berge2ebc742007-07-27 15:43:22 +020034
35#define IEEE80211_TX_OK 0
36#define IEEE80211_TX_AGAIN 1
37#define IEEE80211_TX_FRAG_AGAIN 2
38
39/* misc utils */
40
41static inline void ieee80211_include_sequence(struct ieee80211_sub_if_data *sdata,
42 struct ieee80211_hdr *hdr)
43{
44 /* Set the sequence number for this frame. */
45 hdr->seq_ctrl = cpu_to_le16(sdata->sequence);
46
47 /* Increase the sequence number. */
48 sdata->sequence = (sdata->sequence + 0x10) & IEEE80211_SCTL_SEQ;
49}
50
51#ifdef CONFIG_MAC80211_LOWTX_FRAME_DUMP
52static void ieee80211_dump_frame(const char *ifname, const char *title,
53 const struct sk_buff *skb)
54{
55 const struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
56 u16 fc;
57 int hdrlen;
Joe Perches0795af52007-10-03 17:59:30 -070058 DECLARE_MAC_BUF(mac);
Johannes Berge2ebc742007-07-27 15:43:22 +020059
60 printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
61 if (skb->len < 4) {
62 printk("\n");
63 return;
64 }
65
66 fc = le16_to_cpu(hdr->frame_control);
67 hdrlen = ieee80211_get_hdrlen(fc);
68 if (hdrlen > skb->len)
69 hdrlen = skb->len;
70 if (hdrlen >= 4)
71 printk(" FC=0x%04x DUR=0x%04x",
72 fc, le16_to_cpu(hdr->duration_id));
73 if (hdrlen >= 10)
Joe Perches0795af52007-10-03 17:59:30 -070074 printk(" A1=%s", print_mac(mac, hdr->addr1));
Johannes Berge2ebc742007-07-27 15:43:22 +020075 if (hdrlen >= 16)
Joe Perches0795af52007-10-03 17:59:30 -070076 printk(" A2=%s", print_mac(mac, hdr->addr2));
Johannes Berge2ebc742007-07-27 15:43:22 +020077 if (hdrlen >= 24)
Joe Perches0795af52007-10-03 17:59:30 -070078 printk(" A3=%s", print_mac(mac, hdr->addr3));
Johannes Berge2ebc742007-07-27 15:43:22 +020079 if (hdrlen >= 30)
Joe Perches0795af52007-10-03 17:59:30 -070080 printk(" A4=%s", print_mac(mac, hdr->addr4));
Johannes Berge2ebc742007-07-27 15:43:22 +020081 printk("\n");
82}
83#else /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
84static inline void ieee80211_dump_frame(const char *ifname, const char *title,
85 struct sk_buff *skb)
86{
87}
88#endif /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
89
Johannes Berg5cf121c2008-02-25 16:27:43 +010090static u16 ieee80211_duration(struct ieee80211_tx_data *tx, int group_addr,
Johannes Berge2ebc742007-07-27 15:43:22 +020091 int next_frag_len)
92{
93 int rate, mrate, erp, dur, i;
Johannes Berg2e92e6f2008-05-15 12:55:27 +020094 struct ieee80211_rate *txrate;
Johannes Berge2ebc742007-07-27 15:43:22 +020095 struct ieee80211_local *local = tx->local;
Johannes Berg8318d782008-01-24 19:38:38 +010096 struct ieee80211_supported_band *sband;
Johannes Berge2ebc742007-07-27 15:43:22 +020097
Johannes Berg2e92e6f2008-05-15 12:55:27 +020098 sband = local->hw.wiphy->bands[tx->channel->band];
99 txrate = &sband->bitrates[tx->rate_idx];
Johannes Berg8318d782008-01-24 19:38:38 +0100100
101 erp = 0;
102 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
103 erp = txrate->flags & IEEE80211_RATE_ERP_G;
Johannes Berge2ebc742007-07-27 15:43:22 +0200104
105 /*
106 * data and mgmt (except PS Poll):
107 * - during CFP: 32768
108 * - during contention period:
109 * if addr1 is group address: 0
110 * if more fragments = 0 and addr1 is individual address: time to
111 * transmit one ACK plus SIFS
112 * if more fragments = 1 and addr1 is individual address: time to
113 * transmit next fragment plus 2 x ACK plus 3 x SIFS
114 *
115 * IEEE 802.11, 9.6:
116 * - control response frame (CTS or ACK) shall be transmitted using the
117 * same rate as the immediately previous frame in the frame exchange
118 * sequence, if this rate belongs to the PHY mandatory rates, or else
119 * at the highest possible rate belonging to the PHY rates in the
120 * BSSBasicRateSet
121 */
122
123 if ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) {
124 /* TODO: These control frames are not currently sent by
125 * 80211.o, but should they be implemented, this function
126 * needs to be updated to support duration field calculation.
127 *
128 * RTS: time needed to transmit pending data/mgmt frame plus
129 * one CTS frame plus one ACK frame plus 3 x SIFS
130 * CTS: duration of immediately previous RTS minus time
131 * required to transmit CTS and its SIFS
132 * ACK: 0 if immediately previous directed data/mgmt had
133 * more=0, with more=1 duration in ACK frame is duration
134 * from previous frame minus time needed to transmit ACK
135 * and its SIFS
136 * PS Poll: BIT(15) | BIT(14) | aid
137 */
138 return 0;
139 }
140
141 /* data/mgmt */
142 if (0 /* FIX: data/mgmt during CFP */)
143 return 32768;
144
145 if (group_addr) /* Group address as the destination - no ACK */
146 return 0;
147
148 /* Individual destination address:
149 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
150 * CTS and ACK frames shall be transmitted using the highest rate in
151 * basic rate set that is less than or equal to the rate of the
152 * immediately previous frame and that is using the same modulation
153 * (CCK or OFDM). If no basic rate set matches with these requirements,
154 * the highest mandatory rate of the PHY that is less than or equal to
155 * the rate of the previous frame is used.
156 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
157 */
158 rate = -1;
Johannes Berg8318d782008-01-24 19:38:38 +0100159 /* use lowest available if everything fails */
160 mrate = sband->bitrates[0].bitrate;
161 for (i = 0; i < sband->n_bitrates; i++) {
162 struct ieee80211_rate *r = &sband->bitrates[i];
163
164 if (r->bitrate > txrate->bitrate)
Johannes Berge2ebc742007-07-27 15:43:22 +0200165 break;
166
Johannes Berg8318d782008-01-24 19:38:38 +0100167 if (tx->sdata->basic_rates & BIT(i))
168 rate = r->bitrate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200169
Johannes Berg8318d782008-01-24 19:38:38 +0100170 switch (sband->band) {
171 case IEEE80211_BAND_2GHZ: {
172 u32 flag;
173 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
174 flag = IEEE80211_RATE_MANDATORY_G;
175 else
176 flag = IEEE80211_RATE_MANDATORY_B;
177 if (r->flags & flag)
178 mrate = r->bitrate;
179 break;
180 }
181 case IEEE80211_BAND_5GHZ:
182 if (r->flags & IEEE80211_RATE_MANDATORY_A)
183 mrate = r->bitrate;
184 break;
185 case IEEE80211_NUM_BANDS:
186 WARN_ON(1);
187 break;
188 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200189 }
190 if (rate == -1) {
191 /* No matching basic rate found; use highest suitable mandatory
192 * PHY rate */
193 rate = mrate;
194 }
195
196 /* Time needed to transmit ACK
197 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
198 * to closest integer */
199
200 dur = ieee80211_frame_duration(local, 10, rate, erp,
Johannes Berg471b3ef2007-12-28 14:32:58 +0100201 tx->sdata->bss_conf.use_short_preamble);
Johannes Berge2ebc742007-07-27 15:43:22 +0200202
203 if (next_frag_len) {
204 /* Frame is fragmented: duration increases with time needed to
205 * transmit next fragment plus ACK and 2 x SIFS. */
206 dur *= 2; /* ACK + SIFS */
207 /* next fragment */
208 dur += ieee80211_frame_duration(local, next_frag_len,
Johannes Berg8318d782008-01-24 19:38:38 +0100209 txrate->bitrate, erp,
Johannes Berg471b3ef2007-12-28 14:32:58 +0100210 tx->sdata->bss_conf.use_short_preamble);
Johannes Berge2ebc742007-07-27 15:43:22 +0200211 }
212
213 return dur;
214}
215
216static inline int __ieee80211_queue_stopped(const struct ieee80211_local *local,
217 int queue)
218{
219 return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
220}
221
222static inline int __ieee80211_queue_pending(const struct ieee80211_local *local,
223 int queue)
224{
225 return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
226}
227
228static int inline is_ieee80211_device(struct net_device *dev,
229 struct net_device *master)
230{
231 return (wdev_priv(dev->ieee80211_ptr) ==
232 wdev_priv(master->ieee80211_ptr));
233}
234
235/* tx handlers */
236
Johannes Berg9ae54c82008-01-31 19:48:20 +0100237static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100238ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200239{
240#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
241 struct sk_buff *skb = tx->skb;
242 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
243#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
244 u32 sta_flags;
245
Johannes Berg5cf121c2008-02-25 16:27:43 +0100246 if (unlikely(tx->flags & IEEE80211_TX_INJECTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100247 return TX_CONTINUE;
Johannes Berg58d41852007-09-26 17:53:18 +0200248
Zhu Yiece8edd2007-11-22 10:53:21 +0800249 if (unlikely(tx->local->sta_sw_scanning) &&
Johannes Berge2ebc742007-07-27 15:43:22 +0200250 ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
251 (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100252 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200253
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100254 if (tx->sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT)
255 return TX_CONTINUE;
256
Johannes Berg5cf121c2008-02-25 16:27:43 +0100257 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100258 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200259
Johannes Berg07346f812008-05-03 01:02:02 +0200260 sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
Johannes Berge2ebc742007-07-27 15:43:22 +0200261
Johannes Berg5cf121c2008-02-25 16:27:43 +0100262 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200263 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
Johannes Berg51fb61e2007-12-19 01:31:27 +0100264 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Berge2ebc742007-07-27 15:43:22 +0200265 (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
266#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700267 DECLARE_MAC_BUF(mac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200268 printk(KERN_DEBUG "%s: dropped data frame to not "
Joe Perches0795af52007-10-03 17:59:30 -0700269 "associated station %s\n",
270 tx->dev->name, print_mac(mac, hdr->addr1));
Johannes Berge2ebc742007-07-27 15:43:22 +0200271#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
272 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100273 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200274 }
275 } else {
276 if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
277 tx->local->num_sta == 0 &&
Johannes Berg51fb61e2007-12-19 01:31:27 +0100278 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200279 /*
280 * No associated STAs - no need to send multicast
281 * frames.
282 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100283 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200284 }
Johannes Berg9ae54c82008-01-31 19:48:20 +0100285 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200286 }
287
Johannes Berg9ae54c82008-01-31 19:48:20 +0100288 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200289}
290
Johannes Berg9ae54c82008-01-31 19:48:20 +0100291static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100292ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200293{
294 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
295
296 if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
297 ieee80211_include_sequence(tx->sdata, hdr);
298
Johannes Berg9ae54c82008-01-31 19:48:20 +0100299 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200300}
301
302/* This function is called whenever the AP is about to exceed the maximum limit
303 * of buffered frames for power saving STAs. This situation should not really
304 * happen often during normal operation, so dropping the oldest buffered packet
305 * from each queue should be OK to make some room for new frames. */
306static void purge_old_ps_buffers(struct ieee80211_local *local)
307{
308 int total = 0, purged = 0;
309 struct sk_buff *skb;
310 struct ieee80211_sub_if_data *sdata;
311 struct sta_info *sta;
312
Johannes Berg79010422007-09-18 17:29:21 -0400313 /*
314 * virtual interfaces are protected by RCU
315 */
316 rcu_read_lock();
317
318 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200319 struct ieee80211_if_ap *ap;
320 if (sdata->dev == local->mdev ||
Johannes Berg51fb61e2007-12-19 01:31:27 +0100321 sdata->vif.type != IEEE80211_IF_TYPE_AP)
Johannes Berge2ebc742007-07-27 15:43:22 +0200322 continue;
323 ap = &sdata->u.ap;
324 skb = skb_dequeue(&ap->ps_bc_buf);
325 if (skb) {
326 purged++;
327 dev_kfree_skb(skb);
328 }
329 total += skb_queue_len(&ap->ps_bc_buf);
330 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200331
Johannes Bergd0709a62008-02-25 16:27:46 +0100332 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200333 skb = skb_dequeue(&sta->ps_tx_buf);
334 if (skb) {
335 purged++;
336 dev_kfree_skb(skb);
337 }
338 total += skb_queue_len(&sta->ps_tx_buf);
339 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100340
341 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +0200342
343 local->total_ps_buffered = total;
344 printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400345 wiphy_name(local->hw.wiphy), purged);
Johannes Berge2ebc742007-07-27 15:43:22 +0200346}
347
Johannes Berg9ae54c82008-01-31 19:48:20 +0100348static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100349ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200350{
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100351 /*
352 * broadcast/multicast frame
353 *
354 * If any of the associated stations is in power save mode,
355 * the frame is buffered to be sent after DTIM beacon frame.
356 * This is done either by the hardware or us.
357 */
358
359 /* not AP/IBSS or ordered frame */
360 if (!tx->sdata->bss || (tx->fc & IEEE80211_FCTL_ORDER))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100361 return TX_CONTINUE;
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100362
363 /* no stations in PS mode */
364 if (!atomic_read(&tx->sdata->bss->num_sta_ps))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100365 return TX_CONTINUE;
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100366
367 /* buffered in mac80211 */
368 if (tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200369 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
370 purge_old_ps_buffers(tx->local);
371 if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
372 AP_MAX_BC_BUFFER) {
373 if (net_ratelimit()) {
374 printk(KERN_DEBUG "%s: BC TX buffer full - "
375 "dropping the oldest frame\n",
376 tx->dev->name);
377 }
378 dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
379 } else
380 tx->local->total_ps_buffered++;
381 skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100382 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200383 }
384
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100385 /* buffered in hardware */
Johannes Berg5cf121c2008-02-25 16:27:43 +0100386 tx->control->flags |= IEEE80211_TXCTL_SEND_AFTER_DTIM;
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100387
Johannes Berg9ae54c82008-01-31 19:48:20 +0100388 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200389}
390
Johannes Berg9ae54c82008-01-31 19:48:20 +0100391static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100392ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200393{
394 struct sta_info *sta = tx->sta;
Johannes Berg07346f812008-05-03 01:02:02 +0200395 u32 staflags;
Joe Perches0795af52007-10-03 17:59:30 -0700396 DECLARE_MAC_BUF(mac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200397
398 if (unlikely(!sta ||
399 ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
400 (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100401 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200402
Johannes Berg07346f812008-05-03 01:02:02 +0200403 staflags = get_sta_flags(sta);
404
405 if (unlikely((staflags & WLAN_STA_PS) &&
406 !(staflags & WLAN_STA_PSPOLL))) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200407 struct ieee80211_tx_packet_data *pkt_data;
408#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700409 printk(KERN_DEBUG "STA %s aid %d: PS buffer (entries "
Johannes Berge2ebc742007-07-27 15:43:22 +0200410 "before %d)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700411 print_mac(mac, sta->addr), sta->aid,
Johannes Berge2ebc742007-07-27 15:43:22 +0200412 skb_queue_len(&sta->ps_tx_buf));
413#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berge2ebc742007-07-27 15:43:22 +0200414 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
415 purge_old_ps_buffers(tx->local);
416 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
417 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
418 if (net_ratelimit()) {
Joe Perches0795af52007-10-03 17:59:30 -0700419 printk(KERN_DEBUG "%s: STA %s TX "
Johannes Berge2ebc742007-07-27 15:43:22 +0200420 "buffer full - dropping oldest frame\n",
Joe Perches0795af52007-10-03 17:59:30 -0700421 tx->dev->name, print_mac(mac, sta->addr));
Johannes Berge2ebc742007-07-27 15:43:22 +0200422 }
423 dev_kfree_skb(old);
424 } else
425 tx->local->total_ps_buffered++;
Johannes Berg004c8722008-02-20 11:21:35 +0100426
Johannes Berge2ebc742007-07-27 15:43:22 +0200427 /* Queue frame to be sent after STA sends an PS Poll frame */
Johannes Berg004c8722008-02-20 11:21:35 +0100428 if (skb_queue_empty(&sta->ps_tx_buf))
429 sta_info_set_tim_bit(sta);
430
Johannes Berge2ebc742007-07-27 15:43:22 +0200431 pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
432 pkt_data->jiffies = jiffies;
433 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100434 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200435 }
436#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berg07346f812008-05-03 01:02:02 +0200437 else if (unlikely(test_sta_flags(sta, WLAN_STA_PS))) {
Joe Perches0795af52007-10-03 17:59:30 -0700438 printk(KERN_DEBUG "%s: STA %s in PS mode, but pspoll "
Johannes Berge2ebc742007-07-27 15:43:22 +0200439 "set -> send frame\n", tx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700440 print_mac(mac, sta->addr));
Johannes Berge2ebc742007-07-27 15:43:22 +0200441 }
442#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berg07346f812008-05-03 01:02:02 +0200443 clear_sta_flags(sta, WLAN_STA_PSPOLL);
Johannes Berge2ebc742007-07-27 15:43:22 +0200444
Johannes Berg9ae54c82008-01-31 19:48:20 +0100445 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200446}
447
Johannes Berg9ae54c82008-01-31 19:48:20 +0100448static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100449ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200450{
Johannes Berg5cf121c2008-02-25 16:27:43 +0100451 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100452 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200453
Johannes Berg5cf121c2008-02-25 16:27:43 +0100454 if (tx->flags & IEEE80211_TX_UNICAST)
Johannes Berge2ebc742007-07-27 15:43:22 +0200455 return ieee80211_tx_h_unicast_ps_buf(tx);
456 else
457 return ieee80211_tx_h_multicast_ps_buf(tx);
458}
459
Johannes Berg9ae54c82008-01-31 19:48:20 +0100460static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100461ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200462{
Johannes Bergd4e46a32007-09-14 11:10:24 -0400463 struct ieee80211_key *key;
Johannes Berg176e4f82007-12-18 15:27:47 +0100464 u16 fc = tx->fc;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400465
Johannes Berg5cf121c2008-02-25 16:27:43 +0100466 if (unlikely(tx->control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Johannes Berge2ebc742007-07-27 15:43:22 +0200467 tx->key = NULL;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400468 else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
469 tx->key = key;
470 else if ((key = rcu_dereference(tx->sdata->default_key)))
471 tx->key = key;
Johannes Berge2ebc742007-07-27 15:43:22 +0200472 else if (tx->sdata->drop_unencrypted &&
Johannes Berg5cf121c2008-02-25 16:27:43 +0100473 !(tx->control->flags & IEEE80211_TXCTL_EAPOL_FRAME) &&
474 !(tx->flags & IEEE80211_TX_INJECTED)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200475 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100476 return TX_DROP;
Johannes Berg176e4f82007-12-18 15:27:47 +0100477 } else
Johannes Berge2ebc742007-07-27 15:43:22 +0200478 tx->key = NULL;
479
480 if (tx->key) {
Johannes Berg176e4f82007-12-18 15:27:47 +0100481 u16 ftype, stype;
482
Johannes Berge2ebc742007-07-27 15:43:22 +0200483 tx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400484 /* TODO: add threshold stuff again */
Johannes Berg176e4f82007-12-18 15:27:47 +0100485
486 switch (tx->key->conf.alg) {
487 case ALG_WEP:
488 ftype = fc & IEEE80211_FCTL_FTYPE;
489 stype = fc & IEEE80211_FCTL_STYPE;
490
491 if (ftype == IEEE80211_FTYPE_MGMT &&
492 stype == IEEE80211_STYPE_AUTH)
493 break;
494 case ALG_TKIP:
495 case ALG_CCMP:
496 if (!WLAN_FC_DATA_PRESENT(fc))
497 tx->key = NULL;
498 break;
499 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200500 }
501
Johannes Berg176e4f82007-12-18 15:27:47 +0100502 if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Johannes Berg5cf121c2008-02-25 16:27:43 +0100503 tx->control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
Johannes Berg176e4f82007-12-18 15:27:47 +0100504
Johannes Berg9ae54c82008-01-31 19:48:20 +0100505 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200506}
507
Johannes Berg9ae54c82008-01-31 19:48:20 +0100508static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100509ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200510{
Mattias Nissler1abbe492007-12-20 13:50:07 +0100511 struct rate_selection rsel;
Johannes Berg8318d782008-01-24 19:38:38 +0100512 struct ieee80211_supported_band *sband;
513
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200514 sband = tx->local->hw.wiphy->bands[tx->channel->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200515
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200516 if (likely(tx->rate_idx < 0)) {
Johannes Berg8318d782008-01-24 19:38:38 +0100517 rate_control_get_rate(tx->dev, sband, tx->skb, &rsel);
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200518 tx->rate_idx = rsel.rate_idx;
519 if (unlikely(rsel.probe_idx >= 0)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +0100520 tx->control->flags |=
Johannes Berg58d41852007-09-26 17:53:18 +0200521 IEEE80211_TXCTL_RATE_CTRL_PROBE;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100522 tx->flags |= IEEE80211_TX_PROBE_LAST_FRAG;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200523 tx->control->alt_retry_rate_idx = tx->rate_idx;
524 tx->rate_idx = rsel.probe_idx;
Johannes Berg58d41852007-09-26 17:53:18 +0200525 } else
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200526 tx->control->alt_retry_rate_idx = -1;
Johannes Berg58d41852007-09-26 17:53:18 +0200527
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200528 if (unlikely(tx->rate_idx < 0))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100529 return TX_DROP;
Johannes Berg58d41852007-09-26 17:53:18 +0200530 } else
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200531 tx->control->alt_retry_rate_idx = -1;
Johannes Berg58d41852007-09-26 17:53:18 +0200532
Johannes Berg8318d782008-01-24 19:38:38 +0100533 if (tx->sdata->bss_conf.use_cts_prot &&
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200534 (tx->flags & IEEE80211_TX_FRAGMENTED) && (rsel.nonerp_idx >= 0)) {
535 tx->last_frag_rate_idx = tx->rate_idx;
536 if (rsel.probe_idx >= 0)
Johannes Berg5cf121c2008-02-25 16:27:43 +0100537 tx->flags &= ~IEEE80211_TX_PROBE_LAST_FRAG;
Jiri Slabybadffb72007-08-28 17:01:54 -0400538 else
Johannes Berg5cf121c2008-02-25 16:27:43 +0100539 tx->flags |= IEEE80211_TX_PROBE_LAST_FRAG;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200540 tx->rate_idx = rsel.nonerp_idx;
541 tx->control->tx_rate_idx = rsel.nonerp_idx;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100542 tx->control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200543 } else {
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200544 tx->last_frag_rate_idx = tx->rate_idx;
545 tx->control->tx_rate_idx = tx->rate_idx;
Johannes Berge2ebc742007-07-27 15:43:22 +0200546 }
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200547 tx->control->tx_rate_idx = tx->rate_idx;
Johannes Berge2ebc742007-07-27 15:43:22 +0200548
Johannes Berg9ae54c82008-01-31 19:48:20 +0100549 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200550}
551
Johannes Berg9ae54c82008-01-31 19:48:20 +0100552static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100553ieee80211_tx_h_misc(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200554{
555 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200556 u16 fc = le16_to_cpu(hdr->frame_control);
Johannes Berge2ebc742007-07-27 15:43:22 +0200557 u16 dur;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100558 struct ieee80211_tx_control *control = tx->control;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200559 struct ieee80211_supported_band *sband;
560
561 sband = tx->local->hw.wiphy->bands[tx->channel->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200562
Johannes Berg58d41852007-09-26 17:53:18 +0200563 if (!control->retry_limit) {
564 if (!is_multicast_ether_addr(hdr->addr1)) {
565 if (tx->skb->len + FCS_LEN > tx->local->rts_threshold
566 && tx->local->rts_threshold <
567 IEEE80211_MAX_RTS_THRESHOLD) {
568 control->flags |=
569 IEEE80211_TXCTL_USE_RTS_CTS;
570 control->flags |=
571 IEEE80211_TXCTL_LONG_RETRY_LIMIT;
572 control->retry_limit =
573 tx->local->long_retry_limit;
574 } else {
575 control->retry_limit =
576 tx->local->short_retry_limit;
577 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200578 } else {
Johannes Berg58d41852007-09-26 17:53:18 +0200579 control->retry_limit = 1;
Johannes Berge2ebc742007-07-27 15:43:22 +0200580 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200581 }
582
Johannes Berg5cf121c2008-02-25 16:27:43 +0100583 if (tx->flags & IEEE80211_TX_FRAGMENTED) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200584 /* Do not use multiple retry rates when sending fragmented
585 * frames.
586 * TODO: The last fragment could still use multiple retry
587 * rates. */
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200588 control->alt_retry_rate_idx = -1;
Johannes Berge2ebc742007-07-27 15:43:22 +0200589 }
590
591 /* Use CTS protection for unicast frames sent using extended rates if
592 * there are associated non-ERP stations and RTS/CTS is not configured
593 * for the frame. */
Johannes Berg8318d782008-01-24 19:38:38 +0100594 if ((tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE) &&
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200595 (sband->bitrates[tx->rate_idx].flags & IEEE80211_RATE_ERP_G) &&
Johannes Berg5cf121c2008-02-25 16:27:43 +0100596 (tx->flags & IEEE80211_TX_UNICAST) &&
Johannes Berg471b3ef2007-12-28 14:32:58 +0100597 tx->sdata->bss_conf.use_cts_prot &&
Johannes Berge2ebc742007-07-27 15:43:22 +0200598 !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
599 control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
600
Daniel Drake7e9ed182007-07-27 15:43:24 +0200601 /* Transmit data frames using short preambles if the driver supports
602 * short preambles at the selected rate and short preambles are
603 * available on the network at the current point in time. */
604 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200605 (sband->bitrates[tx->rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
Johannes Berg471b3ef2007-12-28 14:32:58 +0100606 tx->sdata->bss_conf.use_short_preamble &&
Johannes Berg07346f812008-05-03 01:02:02 +0200607 (!tx->sta || test_sta_flags(tx->sta, WLAN_STA_SHORT_PREAMBLE))) {
Johannes Berg5cf121c2008-02-25 16:27:43 +0100608 tx->control->flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200609 }
610
Johannes Berge2ebc742007-07-27 15:43:22 +0200611 /* Setup duration field for the first fragment of the frame. Duration
612 * for remaining fragments will be updated when they are being sent
613 * to low-level driver in ieee80211_tx(). */
614 dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
Johannes Berg5cf121c2008-02-25 16:27:43 +0100615 (tx->flags & IEEE80211_TX_FRAGMENTED) ?
616 tx->extra_frag[0]->len : 0);
Johannes Berge2ebc742007-07-27 15:43:22 +0200617 hdr->duration_id = cpu_to_le16(dur);
618
619 if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
620 (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
Johannes Berg8318d782008-01-24 19:38:38 +0100621 struct ieee80211_supported_band *sband;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200622 struct ieee80211_rate *rate;
623 s8 baserate = -1;
Johannes Berg8318d782008-01-24 19:38:38 +0100624 int idx;
625
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200626 sband = tx->local->hw.wiphy->bands[tx->channel->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200627
628 /* Do not use multiple retry rates when using RTS/CTS */
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200629 control->alt_retry_rate_idx = -1;
Johannes Berge2ebc742007-07-27 15:43:22 +0200630
631 /* Use min(data rate, max base rate) as CTS/RTS rate */
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200632 rate = &sband->bitrates[tx->rate_idx];
Johannes Berge2ebc742007-07-27 15:43:22 +0200633
Johannes Berg8318d782008-01-24 19:38:38 +0100634 for (idx = 0; idx < sband->n_bitrates; idx++) {
635 if (sband->bitrates[idx].bitrate > rate->bitrate)
636 continue;
637 if (tx->sdata->basic_rates & BIT(idx) &&
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200638 (baserate < 0 ||
639 (sband->bitrates[baserate].bitrate
640 < sband->bitrates[idx].bitrate)))
641 baserate = idx;
Johannes Berg8318d782008-01-24 19:38:38 +0100642 }
643
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200644 if (baserate >= 0)
645 control->rts_cts_rate_idx = baserate;
Johannes Berg8318d782008-01-24 19:38:38 +0100646 else
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200647 control->rts_cts_rate_idx = 0;
Johannes Berge2ebc742007-07-27 15:43:22 +0200648 }
649
Johannes Berge2454942008-05-15 12:55:28 +0200650 if (tx->sta)
Chrfff77102008-04-01 21:45:18 +0200651 control->aid = tx->sta->aid;
Johannes Berge2ebc742007-07-27 15:43:22 +0200652
Johannes Berg9ae54c82008-01-31 19:48:20 +0100653 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200654}
655
Johannes Berg9ae54c82008-01-31 19:48:20 +0100656static ieee80211_tx_result
Johannes Berge2454942008-05-15 12:55:28 +0200657ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
658{
659 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
660 size_t hdrlen, per_fragm, num_fragm, payload_len, left;
661 struct sk_buff **frags, *first, *frag;
662 int i;
663 u16 seq;
664 u8 *pos;
665 int frag_threshold = tx->local->fragmentation_threshold;
666
667 if (!(tx->flags & IEEE80211_TX_FRAGMENTED))
668 return TX_CONTINUE;
669
670 first = tx->skb;
671
672 hdrlen = ieee80211_get_hdrlen(tx->fc);
673 payload_len = first->len - hdrlen;
674 per_fragm = frag_threshold - hdrlen - FCS_LEN;
675 num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
676
677 frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
678 if (!frags)
679 goto fail;
680
681 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
682 seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
683 pos = first->data + hdrlen + per_fragm;
684 left = payload_len - per_fragm;
685 for (i = 0; i < num_fragm - 1; i++) {
686 struct ieee80211_hdr *fhdr;
687 size_t copylen;
688
689 if (left <= 0)
690 goto fail;
691
692 /* reserve enough extra head and tail room for possible
693 * encryption */
694 frag = frags[i] =
695 dev_alloc_skb(tx->local->tx_headroom +
696 frag_threshold +
697 IEEE80211_ENCRYPT_HEADROOM +
698 IEEE80211_ENCRYPT_TAILROOM);
699 if (!frag)
700 goto fail;
701 /* Make sure that all fragments use the same priority so
702 * that they end up using the same TX queue */
703 frag->priority = first->priority;
704 skb_reserve(frag, tx->local->tx_headroom +
705 IEEE80211_ENCRYPT_HEADROOM);
706 fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
707 memcpy(fhdr, first->data, hdrlen);
708 if (i == num_fragm - 2)
709 fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
710 fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
711 copylen = left > per_fragm ? per_fragm : left;
712 memcpy(skb_put(frag, copylen), pos, copylen);
713
714 pos += copylen;
715 left -= copylen;
716 }
717 skb_trim(first, hdrlen + per_fragm);
718
719 tx->num_extra_frag = num_fragm - 1;
720 tx->extra_frag = frags;
721
722 return TX_CONTINUE;
723
724 fail:
725 printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
726 if (frags) {
727 for (i = 0; i < num_fragm - 1; i++)
728 if (frags[i])
729 dev_kfree_skb(frags[i]);
730 kfree(frags);
731 }
732 I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
733 return TX_DROP;
734}
735
736static ieee80211_tx_result
737ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
738{
739 if (!tx->key)
740 return TX_CONTINUE;
741
742 switch (tx->key->conf.alg) {
743 case ALG_WEP:
744 return ieee80211_crypto_wep_encrypt(tx);
745 case ALG_TKIP:
746 return ieee80211_crypto_tkip_encrypt(tx);
747 case ALG_CCMP:
748 return ieee80211_crypto_ccmp_encrypt(tx);
749 }
750
751 /* not reached */
752 WARN_ON(1);
753 return TX_DROP;
754}
755
756static ieee80211_tx_result
757ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200758{
759 struct ieee80211_local *local = tx->local;
Johannes Berge2ebc742007-07-27 15:43:22 +0200760 struct sk_buff *skb = tx->skb;
761 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
762 u32 load = 0, hdrtime;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200763 struct ieee80211_rate *rate;
764 struct ieee80211_supported_band *sband;
765
766 sband = tx->local->hw.wiphy->bands[tx->channel->band];
767 rate = &sband->bitrates[tx->rate_idx];
Johannes Berge2ebc742007-07-27 15:43:22 +0200768
769 /* TODO: this could be part of tx_status handling, so that the number
770 * of retries would be known; TX rate should in that case be stored
771 * somewhere with the packet */
772
773 /* Estimate total channel use caused by this frame */
774
775 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
776 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
777
Johannes Berg5cf121c2008-02-25 16:27:43 +0100778 if (tx->channel->band == IEEE80211_BAND_5GHZ ||
779 (tx->channel->band == IEEE80211_BAND_2GHZ &&
Johannes Berg8318d782008-01-24 19:38:38 +0100780 rate->flags & IEEE80211_RATE_ERP_G))
Johannes Berge2ebc742007-07-27 15:43:22 +0200781 hdrtime = CHAN_UTIL_HDR_SHORT;
782 else
783 hdrtime = CHAN_UTIL_HDR_LONG;
784
785 load = hdrtime;
786 if (!is_multicast_ether_addr(hdr->addr1))
787 load += hdrtime;
788
Johannes Berg5cf121c2008-02-25 16:27:43 +0100789 if (tx->control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
Johannes Berge2ebc742007-07-27 15:43:22 +0200790 load += 2 * hdrtime;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100791 else if (tx->control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
Johannes Berge2ebc742007-07-27 15:43:22 +0200792 load += hdrtime;
793
Johannes Berg8318d782008-01-24 19:38:38 +0100794 /* TODO: optimise again */
795 load += skb->len * CHAN_UTIL_RATE_LCM / rate->bitrate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200796
Johannes Berg5cf121c2008-02-25 16:27:43 +0100797 if (tx->extra_frag) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200798 int i;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100799 for (i = 0; i < tx->num_extra_frag; i++) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200800 load += 2 * hdrtime;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100801 load += tx->extra_frag[i]->len *
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200802 rate->bitrate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200803 }
804 }
805
806 /* Divide channel_use by 8 to avoid wrapping around the counter */
807 load >>= CHAN_UTIL_SHIFT;
808 local->channel_use_raw += load;
809 if (tx->sta)
810 tx->sta->channel_use_raw += load;
811 tx->sdata->channel_use_raw += load;
812
Johannes Berge2454942008-05-15 12:55:28 +0200813 if (tx->sta) {
814 tx->sta->tx_packets++;
815 tx->sta->tx_fragments++;
816 tx->sta->tx_bytes += tx->skb->len;
817 if (tx->extra_frag) {
818 int i;
819 tx->sta->tx_fragments += tx->num_extra_frag;
820 for (i = 0; i < tx->num_extra_frag; i++) {
821 tx->sta->tx_bytes +=
822 tx->extra_frag[i]->len;
823 }
824 }
825 }
826
Johannes Berg9ae54c82008-01-31 19:48:20 +0100827 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200828}
829
Johannes Berge2ebc742007-07-27 15:43:22 +0200830
Johannes Berg5cf121c2008-02-25 16:27:43 +0100831typedef ieee80211_tx_result (*ieee80211_tx_handler)(struct ieee80211_tx_data *);
Johannes Berg58905292008-01-31 19:48:25 +0100832static ieee80211_tx_handler ieee80211_tx_handlers[] =
Johannes Berge2ebc742007-07-27 15:43:22 +0200833{
834 ieee80211_tx_h_check_assoc,
835 ieee80211_tx_h_sequence,
836 ieee80211_tx_h_ps_buf,
837 ieee80211_tx_h_select_key,
838 ieee80211_tx_h_michael_mic_add,
Johannes Berge2ebc742007-07-27 15:43:22 +0200839 ieee80211_tx_h_rate_ctrl,
840 ieee80211_tx_h_misc,
Johannes Berge2454942008-05-15 12:55:28 +0200841 ieee80211_tx_h_fragment,
842 ieee80211_tx_h_encrypt,
843 ieee80211_tx_h_stats,
Johannes Berge2ebc742007-07-27 15:43:22 +0200844 NULL
845};
846
847/* actual transmit path */
848
849/*
850 * deal with packet injection down monitor interface
851 * with Radiotap Header -- only called for monitor mode interface
852 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100853static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100854__ieee80211_parse_tx_radiotap(struct ieee80211_tx_data *tx,
Johannes Berg58d41852007-09-26 17:53:18 +0200855 struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +0200856{
857 /*
858 * this is the moment to interpret and discard the radiotap header that
859 * must be at the start of the packet injected in Monitor mode
860 *
861 * Need to take some care with endian-ness since radiotap
862 * args are little-endian
863 */
864
865 struct ieee80211_radiotap_iterator iterator;
866 struct ieee80211_radiotap_header *rthdr =
867 (struct ieee80211_radiotap_header *) skb->data;
Johannes Berg8318d782008-01-24 19:38:38 +0100868 struct ieee80211_supported_band *sband;
Johannes Berge2ebc742007-07-27 15:43:22 +0200869 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
Johannes Berg5cf121c2008-02-25 16:27:43 +0100870 struct ieee80211_tx_control *control = tx->control;
Johannes Berge2ebc742007-07-27 15:43:22 +0200871
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200872 sband = tx->local->hw.wiphy->bands[tx->channel->band];
Johannes Berg8318d782008-01-24 19:38:38 +0100873
Johannes Berg58d41852007-09-26 17:53:18 +0200874 control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100875 tx->flags |= IEEE80211_TX_INJECTED;
876 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200877
878 /*
879 * for every radiotap entry that is present
880 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
881 * entries present, or -EINVAL on error)
882 */
883
884 while (!ret) {
885 int i, target_rate;
886
887 ret = ieee80211_radiotap_iterator_next(&iterator);
888
889 if (ret)
890 continue;
891
892 /* see if this argument is something we can use */
893 switch (iterator.this_arg_index) {
894 /*
895 * You must take care when dereferencing iterator.this_arg
896 * for multibyte types... the pointer is not aligned. Use
897 * get_unaligned((type *)iterator.this_arg) to dereference
898 * iterator.this_arg for type "type" safely on all arches.
899 */
900 case IEEE80211_RADIOTAP_RATE:
901 /*
902 * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
903 * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
904 */
905 target_rate = (*iterator.this_arg) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +0100906 for (i = 0; i < sband->n_bitrates; i++) {
907 struct ieee80211_rate *r;
Johannes Berge2ebc742007-07-27 15:43:22 +0200908
Johannes Berg8318d782008-01-24 19:38:38 +0100909 r = &sband->bitrates[i];
910
911 if (r->bitrate == target_rate) {
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200912 tx->rate_idx = i;
Johannes Berg58d41852007-09-26 17:53:18 +0200913 break;
914 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200915 }
916 break;
917
918 case IEEE80211_RADIOTAP_ANTENNA:
919 /*
920 * radiotap uses 0 for 1st ant, mac80211 is 1 for
921 * 1st ant
922 */
923 control->antenna_sel_tx = (*iterator.this_arg) + 1;
924 break;
925
Johannes Berg8318d782008-01-24 19:38:38 +0100926#if 0
Johannes Berge2ebc742007-07-27 15:43:22 +0200927 case IEEE80211_RADIOTAP_DBM_TX_POWER:
928 control->power_level = *iterator.this_arg;
929 break;
Johannes Berg8318d782008-01-24 19:38:38 +0100930#endif
Johannes Berge2ebc742007-07-27 15:43:22 +0200931
932 case IEEE80211_RADIOTAP_FLAGS:
933 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
934 /*
935 * this indicates that the skb we have been
936 * handed has the 32-bit FCS CRC at the end...
937 * we should react to that by snipping it off
938 * because it will be recomputed and added
939 * on transmission
940 */
941 if (skb->len < (iterator.max_length + FCS_LEN))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100942 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200943
944 skb_trim(skb, skb->len - FCS_LEN);
945 }
Johannes Berg58d41852007-09-26 17:53:18 +0200946 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
947 control->flags &=
948 ~IEEE80211_TXCTL_DO_NOT_ENCRYPT;
949 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
Johannes Berg5cf121c2008-02-25 16:27:43 +0100950 tx->flags |= IEEE80211_TX_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200951 break;
952
Johannes Berg58d41852007-09-26 17:53:18 +0200953 /*
954 * Please update the file
955 * Documentation/networking/mac80211-injection.txt
956 * when parsing new fields here.
957 */
958
Johannes Berge2ebc742007-07-27 15:43:22 +0200959 default:
960 break;
961 }
962 }
963
964 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100965 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200966
967 /*
968 * remove the radiotap header
969 * iterator->max_length was sanity-checked against
970 * skb->len by iterator init
971 */
972 skb_pull(skb, iterator.max_length);
973
Johannes Berg9ae54c82008-01-31 19:48:20 +0100974 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200975}
976
Johannes Berg58d41852007-09-26 17:53:18 +0200977/*
978 * initialises @tx
979 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100980static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100981__ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
Johannes Berge2ebc742007-07-27 15:43:22 +0200982 struct sk_buff *skb,
983 struct net_device *dev,
984 struct ieee80211_tx_control *control)
985{
986 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg58d41852007-09-26 17:53:18 +0200987 struct ieee80211_hdr *hdr;
Johannes Berge2ebc742007-07-27 15:43:22 +0200988 struct ieee80211_sub_if_data *sdata;
Johannes Berge2ebc742007-07-27 15:43:22 +0200989
990 int hdrlen;
991
992 memset(tx, 0, sizeof(*tx));
993 tx->skb = skb;
994 tx->dev = dev; /* use original interface */
995 tx->local = local;
996 tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg5cf121c2008-02-25 16:27:43 +0100997 tx->control = control;
Johannes Berge2ebc742007-07-27 15:43:22 +0200998 /*
Johannes Berg58d41852007-09-26 17:53:18 +0200999 * Set this flag (used below to indicate "automatic fragmentation"),
1000 * it will be cleared/left by radiotap as desired.
Johannes Berge2ebc742007-07-27 15:43:22 +02001001 */
Johannes Berg5cf121c2008-02-25 16:27:43 +01001002 tx->flags |= IEEE80211_TX_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +02001003
1004 /* process and remove the injection radiotap header */
1005 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +01001006 if (unlikely(sdata->vif.type == IEEE80211_IF_TYPE_MNTR)) {
Johannes Berg9ae54c82008-01-31 19:48:20 +01001007 if (__ieee80211_parse_tx_radiotap(tx, skb) == TX_DROP)
1008 return TX_DROP;
Johannes Berg58d41852007-09-26 17:53:18 +02001009
Johannes Berge2ebc742007-07-27 15:43:22 +02001010 /*
Johannes Berg58d41852007-09-26 17:53:18 +02001011 * __ieee80211_parse_tx_radiotap has now removed
1012 * the radiotap header that was present and pre-filled
1013 * 'tx' with tx control information.
Johannes Berge2ebc742007-07-27 15:43:22 +02001014 */
Johannes Berge2ebc742007-07-27 15:43:22 +02001015 }
1016
Johannes Berg58d41852007-09-26 17:53:18 +02001017 hdr = (struct ieee80211_hdr *) skb->data;
1018
warmcat24338792007-09-14 11:10:25 -04001019 tx->sta = sta_info_get(local, hdr->addr1);
1020 tx->fc = le16_to_cpu(hdr->frame_control);
Johannes Berg58d41852007-09-26 17:53:18 +02001021
Jiri Slabybadffb72007-08-28 17:01:54 -04001022 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001023 tx->flags &= ~IEEE80211_TX_UNICAST;
Johannes Berge2ebc742007-07-27 15:43:22 +02001024 control->flags |= IEEE80211_TXCTL_NO_ACK;
Jiri Slabybadffb72007-08-28 17:01:54 -04001025 } else {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001026 tx->flags |= IEEE80211_TX_UNICAST;
Johannes Berge2ebc742007-07-27 15:43:22 +02001027 control->flags &= ~IEEE80211_TXCTL_NO_ACK;
Jiri Slabybadffb72007-08-28 17:01:54 -04001028 }
Johannes Berg58d41852007-09-26 17:53:18 +02001029
Johannes Berg5cf121c2008-02-25 16:27:43 +01001030 if (tx->flags & IEEE80211_TX_FRAGMENTED) {
1031 if ((tx->flags & IEEE80211_TX_UNICAST) &&
Johannes Berg58d41852007-09-26 17:53:18 +02001032 skb->len + FCS_LEN > local->fragmentation_threshold &&
1033 !local->ops->set_frag_threshold)
Johannes Berg5cf121c2008-02-25 16:27:43 +01001034 tx->flags |= IEEE80211_TX_FRAGMENTED;
Johannes Berg58d41852007-09-26 17:53:18 +02001035 else
Johannes Berg5cf121c2008-02-25 16:27:43 +01001036 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
Johannes Berg58d41852007-09-26 17:53:18 +02001037 }
1038
Johannes Berge2ebc742007-07-27 15:43:22 +02001039 if (!tx->sta)
Johannes Bergd46e1442008-02-20 23:59:33 +01001040 control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
Johannes Berg07346f812008-05-03 01:02:02 +02001041 else if (test_and_clear_sta_flags(tx->sta, WLAN_STA_CLEAR_PS_FILT))
Johannes Bergd46e1442008-02-20 23:59:33 +01001042 control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
Johannes Berg58d41852007-09-26 17:53:18 +02001043
Johannes Berge2ebc742007-07-27 15:43:22 +02001044 hdrlen = ieee80211_get_hdrlen(tx->fc);
1045 if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
1046 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
1047 tx->ethertype = (pos[0] << 8) | pos[1];
1048 }
1049 control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
1050
Johannes Berg9ae54c82008-01-31 19:48:20 +01001051 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001052}
1053
Johannes Berg32bfd352007-12-19 01:31:26 +01001054/*
Johannes Berg58d41852007-09-26 17:53:18 +02001055 * NB: @tx is uninitialised when passed in here
1056 */
Johannes Berg5cf121c2008-02-25 16:27:43 +01001057static int ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
Johannes Berg58d41852007-09-26 17:53:18 +02001058 struct sk_buff *skb,
1059 struct net_device *mdev,
1060 struct ieee80211_tx_control *control)
Johannes Berge2ebc742007-07-27 15:43:22 +02001061{
1062 struct ieee80211_tx_packet_data *pkt_data;
1063 struct net_device *dev;
1064
1065 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001066 dev = dev_get_by_index(&init_net, pkt_data->ifindex);
Johannes Berge2ebc742007-07-27 15:43:22 +02001067 if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
1068 dev_put(dev);
1069 dev = NULL;
1070 }
1071 if (unlikely(!dev))
1072 return -ENODEV;
Johannes Berg58d41852007-09-26 17:53:18 +02001073 /* initialises tx with control */
Johannes Berge2ebc742007-07-27 15:43:22 +02001074 __ieee80211_tx_prepare(tx, skb, dev, control);
Johannes Berg32bfd352007-12-19 01:31:26 +01001075 dev_put(dev);
Johannes Berge2ebc742007-07-27 15:43:22 +02001076 return 0;
1077}
1078
1079static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
Johannes Berg5cf121c2008-02-25 16:27:43 +01001080 struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +02001081{
Johannes Berg5cf121c2008-02-25 16:27:43 +01001082 struct ieee80211_tx_control *control = tx->control;
Johannes Berge2ebc742007-07-27 15:43:22 +02001083 int ret, i;
1084
1085 if (!ieee80211_qdisc_installed(local->mdev) &&
1086 __ieee80211_queue_stopped(local, 0)) {
1087 netif_stop_queue(local->mdev);
1088 return IEEE80211_TX_AGAIN;
1089 }
1090 if (skb) {
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001091 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
1092 "TX to low-level driver", skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001093 ret = local->ops->tx(local_to_hw(local), skb, control);
1094 if (ret)
1095 return IEEE80211_TX_AGAIN;
1096 local->mdev->trans_start = jiffies;
1097 ieee80211_led_tx(local, 1);
1098 }
Johannes Berg5cf121c2008-02-25 16:27:43 +01001099 if (tx->extra_frag) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001100 control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
1101 IEEE80211_TXCTL_USE_CTS_PROTECT |
Johannes Bergd46e1442008-02-20 23:59:33 +01001102 IEEE80211_TXCTL_CLEAR_PS_FILT |
Johannes Berge2ebc742007-07-27 15:43:22 +02001103 IEEE80211_TXCTL_FIRST_FRAGMENT);
Johannes Berg5cf121c2008-02-25 16:27:43 +01001104 for (i = 0; i < tx->num_extra_frag; i++) {
1105 if (!tx->extra_frag[i])
Johannes Berge2ebc742007-07-27 15:43:22 +02001106 continue;
1107 if (__ieee80211_queue_stopped(local, control->queue))
1108 return IEEE80211_TX_FRAG_AGAIN;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001109 if (i == tx->num_extra_frag) {
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001110 control->tx_rate_idx = tx->last_frag_rate_idx;
Johannes Berg8318d782008-01-24 19:38:38 +01001111
Johannes Berg5cf121c2008-02-25 16:27:43 +01001112 if (tx->flags & IEEE80211_TX_PROBE_LAST_FRAG)
Johannes Berge2ebc742007-07-27 15:43:22 +02001113 control->flags |=
1114 IEEE80211_TXCTL_RATE_CTRL_PROBE;
1115 else
1116 control->flags &=
1117 ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
1118 }
1119
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001120 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
Johannes Berge2ebc742007-07-27 15:43:22 +02001121 "TX to low-level driver",
Johannes Berg5cf121c2008-02-25 16:27:43 +01001122 tx->extra_frag[i]);
Johannes Berge2ebc742007-07-27 15:43:22 +02001123 ret = local->ops->tx(local_to_hw(local),
Johannes Berg5cf121c2008-02-25 16:27:43 +01001124 tx->extra_frag[i],
Johannes Berge2ebc742007-07-27 15:43:22 +02001125 control);
1126 if (ret)
1127 return IEEE80211_TX_FRAG_AGAIN;
1128 local->mdev->trans_start = jiffies;
1129 ieee80211_led_tx(local, 1);
Johannes Berg5cf121c2008-02-25 16:27:43 +01001130 tx->extra_frag[i] = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02001131 }
Johannes Berg5cf121c2008-02-25 16:27:43 +01001132 kfree(tx->extra_frag);
1133 tx->extra_frag = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02001134 }
1135 return IEEE80211_TX_OK;
1136}
1137
1138static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
Johannes Bergf9d540e2007-09-28 14:02:09 +02001139 struct ieee80211_tx_control *control)
Johannes Berge2ebc742007-07-27 15:43:22 +02001140{
1141 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1142 struct sta_info *sta;
1143 ieee80211_tx_handler *handler;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001144 struct ieee80211_tx_data tx;
Johannes Berg9ae54c82008-01-31 19:48:20 +01001145 ieee80211_tx_result res = TX_DROP, res_prepare;
Johannes Berge2ebc742007-07-27 15:43:22 +02001146 int ret, i;
1147
1148 WARN_ON(__ieee80211_queue_pending(local, control->queue));
1149
1150 if (unlikely(skb->len < 10)) {
1151 dev_kfree_skb(skb);
1152 return 0;
1153 }
1154
Johannes Bergd0709a62008-02-25 16:27:46 +01001155 rcu_read_lock();
1156
Johannes Berg58d41852007-09-26 17:53:18 +02001157 /* initialises tx */
Johannes Berge2ebc742007-07-27 15:43:22 +02001158 res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
1159
Johannes Berg9ae54c82008-01-31 19:48:20 +01001160 if (res_prepare == TX_DROP) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001161 dev_kfree_skb(skb);
Johannes Bergd0709a62008-02-25 16:27:46 +01001162 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001163 return 0;
1164 }
1165
1166 sta = tx.sta;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001167 tx.channel = local->hw.conf.channel;
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001168 control->band = tx.channel->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02001169
Johannes Berg58905292008-01-31 19:48:25 +01001170 for (handler = ieee80211_tx_handlers; *handler != NULL;
Johannes Berg58d41852007-09-26 17:53:18 +02001171 handler++) {
1172 res = (*handler)(&tx);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001173 if (res != TX_CONTINUE)
Johannes Berg58d41852007-09-26 17:53:18 +02001174 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02001175 }
1176
1177 skb = tx.skb; /* handlers are allowed to change skb */
1178
Johannes Berg9ae54c82008-01-31 19:48:20 +01001179 if (unlikely(res == TX_DROP)) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001180 I802_DEBUG_INC(local->tx_handlers_drop);
1181 goto drop;
1182 }
1183
Johannes Berg9ae54c82008-01-31 19:48:20 +01001184 if (unlikely(res == TX_QUEUED)) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001185 I802_DEBUG_INC(local->tx_handlers_queued);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001186 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001187 return 0;
1188 }
1189
Johannes Berg5cf121c2008-02-25 16:27:43 +01001190 if (tx.extra_frag) {
1191 for (i = 0; i < tx.num_extra_frag; i++) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001192 int next_len, dur;
1193 struct ieee80211_hdr *hdr =
1194 (struct ieee80211_hdr *)
Johannes Berg5cf121c2008-02-25 16:27:43 +01001195 tx.extra_frag[i]->data;
Johannes Berge2ebc742007-07-27 15:43:22 +02001196
Johannes Berg5cf121c2008-02-25 16:27:43 +01001197 if (i + 1 < tx.num_extra_frag) {
1198 next_len = tx.extra_frag[i + 1]->len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001199 } else {
1200 next_len = 0;
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001201 tx.rate_idx = tx.last_frag_rate_idx;
Johannes Berge2ebc742007-07-27 15:43:22 +02001202 }
1203 dur = ieee80211_duration(&tx, 0, next_len);
1204 hdr->duration_id = cpu_to_le16(dur);
1205 }
1206 }
1207
1208retry:
1209 ret = __ieee80211_tx(local, skb, &tx);
1210 if (ret) {
1211 struct ieee80211_tx_stored_packet *store =
1212 &local->pending_packet[control->queue];
1213
1214 if (ret == IEEE80211_TX_FRAG_AGAIN)
1215 skb = NULL;
1216 set_bit(IEEE80211_LINK_STATE_PENDING,
1217 &local->state[control->queue]);
1218 smp_mb();
1219 /* When the driver gets out of buffers during sending of
1220 * fragments and calls ieee80211_stop_queue, there is
1221 * a small window between IEEE80211_LINK_STATE_XOFF and
1222 * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
1223 * gets available in that window (i.e. driver calls
1224 * ieee80211_wake_queue), we would end up with ieee80211_tx
1225 * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
1226 * continuing transmitting here when that situation is
1227 * possible to have happened. */
1228 if (!__ieee80211_queue_stopped(local, control->queue)) {
1229 clear_bit(IEEE80211_LINK_STATE_PENDING,
1230 &local->state[control->queue]);
1231 goto retry;
1232 }
1233 memcpy(&store->control, control,
1234 sizeof(struct ieee80211_tx_control));
1235 store->skb = skb;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001236 store->extra_frag = tx.extra_frag;
1237 store->num_extra_frag = tx.num_extra_frag;
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001238 store->last_frag_rate_idx = tx.last_frag_rate_idx;
Jiri Slabybadffb72007-08-28 17:01:54 -04001239 store->last_frag_rate_ctrl_probe =
Johannes Berg5cf121c2008-02-25 16:27:43 +01001240 !!(tx.flags & IEEE80211_TX_PROBE_LAST_FRAG);
Johannes Berge2ebc742007-07-27 15:43:22 +02001241 }
Johannes Bergd4e46a32007-09-14 11:10:24 -04001242 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001243 return 0;
1244
1245 drop:
1246 if (skb)
1247 dev_kfree_skb(skb);
Johannes Berg5cf121c2008-02-25 16:27:43 +01001248 for (i = 0; i < tx.num_extra_frag; i++)
1249 if (tx.extra_frag[i])
1250 dev_kfree_skb(tx.extra_frag[i]);
1251 kfree(tx.extra_frag);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001252 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001253 return 0;
1254}
1255
1256/* device xmit handlers */
1257
1258int ieee80211_master_start_xmit(struct sk_buff *skb,
1259 struct net_device *dev)
1260{
1261 struct ieee80211_tx_control control;
1262 struct ieee80211_tx_packet_data *pkt_data;
1263 struct net_device *odev = NULL;
1264 struct ieee80211_sub_if_data *osdata;
1265 int headroom;
1266 int ret;
1267
1268 /*
1269 * copy control out of the skb so other people can use skb->cb
1270 */
1271 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1272 memset(&control, 0, sizeof(struct ieee80211_tx_control));
1273
1274 if (pkt_data->ifindex)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001275 odev = dev_get_by_index(&init_net, pkt_data->ifindex);
Johannes Berge2ebc742007-07-27 15:43:22 +02001276 if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
1277 dev_put(odev);
1278 odev = NULL;
1279 }
1280 if (unlikely(!odev)) {
1281#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1282 printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
1283 "originating device\n", dev->name);
1284#endif
1285 dev_kfree_skb(skb);
1286 return 0;
1287 }
1288 osdata = IEEE80211_DEV_TO_SUB_IF(odev);
1289
1290 headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
1291 if (skb_headroom(skb) < headroom) {
1292 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
1293 dev_kfree_skb(skb);
1294 dev_put(odev);
1295 return 0;
1296 }
1297 }
1298
Johannes Berg32bfd352007-12-19 01:31:26 +01001299 control.vif = &osdata->vif;
Johannes Berg51fb61e2007-12-19 01:31:27 +01001300 control.type = osdata->vif.type;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001301 if (pkt_data->flags & IEEE80211_TXPD_REQ_TX_STATUS)
Johannes Berge2ebc742007-07-27 15:43:22 +02001302 control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001303 if (pkt_data->flags & IEEE80211_TXPD_DO_NOT_ENCRYPT)
Johannes Berge2ebc742007-07-27 15:43:22 +02001304 control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001305 if (pkt_data->flags & IEEE80211_TXPD_REQUEUE)
Johannes Berge2ebc742007-07-27 15:43:22 +02001306 control.flags |= IEEE80211_TXCTL_REQUEUE;
Johannes Berg678f5f712007-12-19 01:31:23 +01001307 if (pkt_data->flags & IEEE80211_TXPD_EAPOL_FRAME)
1308 control.flags |= IEEE80211_TXCTL_EAPOL_FRAME;
Ron Rindjunsky9e723492008-01-28 14:07:18 +02001309 if (pkt_data->flags & IEEE80211_TXPD_AMPDU)
1310 control.flags |= IEEE80211_TXCTL_AMPDU;
Johannes Berge2ebc742007-07-27 15:43:22 +02001311 control.queue = pkt_data->queue;
1312
Johannes Bergf9d540e2007-09-28 14:02:09 +02001313 ret = ieee80211_tx(odev, skb, &control);
Johannes Berge2ebc742007-07-27 15:43:22 +02001314 dev_put(odev);
1315
1316 return ret;
1317}
1318
1319int ieee80211_monitor_start_xmit(struct sk_buff *skb,
1320 struct net_device *dev)
1321{
1322 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1323 struct ieee80211_tx_packet_data *pkt_data;
1324 struct ieee80211_radiotap_header *prthdr =
1325 (struct ieee80211_radiotap_header *)skb->data;
Andy Green9b8a74e2007-07-27 15:43:24 +02001326 u16 len_rthdr;
Johannes Berge2ebc742007-07-27 15:43:22 +02001327
Andy Green9b8a74e2007-07-27 15:43:24 +02001328 /* check for not even having the fixed radiotap header part */
1329 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1330 goto fail; /* too short to be possibly valid */
1331
1332 /* is it a header version we can trust to find length from? */
1333 if (unlikely(prthdr->it_version))
1334 goto fail; /* only version 0 is supported */
1335
1336 /* then there must be a radiotap header with a length we can use */
1337 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1338
1339 /* does the skb contain enough to deliver on the alleged length? */
1340 if (unlikely(skb->len < len_rthdr))
1341 goto fail; /* skb too short for claimed rt header extent */
Johannes Berge2ebc742007-07-27 15:43:22 +02001342
1343 skb->dev = local->mdev;
1344
1345 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1346 memset(pkt_data, 0, sizeof(*pkt_data));
Andy Green9b8a74e2007-07-27 15:43:24 +02001347 /* needed because we set skb device to master */
Johannes Berge2ebc742007-07-27 15:43:22 +02001348 pkt_data->ifindex = dev->ifindex;
Andy Green9b8a74e2007-07-27 15:43:24 +02001349
Jiri Slabye8bf9642007-08-28 17:01:54 -04001350 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Ivo van Doornb0a67172008-05-13 15:03:02 +02001351 /* Interfaces should always request a status report */
1352 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
Johannes Berge2ebc742007-07-27 15:43:22 +02001353
Johannes Berge2ebc742007-07-27 15:43:22 +02001354 /*
1355 * fix up the pointers accounting for the radiotap
1356 * header still being in there. We are being given
1357 * a precooked IEEE80211 header so no need for
1358 * normal processing
1359 */
Andy Green9b8a74e2007-07-27 15:43:24 +02001360 skb_set_mac_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02001361 /*
Andy Green9b8a74e2007-07-27 15:43:24 +02001362 * these are just fixed to the end of the rt area since we
1363 * don't have any better information and at this point, nobody cares
Johannes Berge2ebc742007-07-27 15:43:22 +02001364 */
Andy Green9b8a74e2007-07-27 15:43:24 +02001365 skb_set_network_header(skb, len_rthdr);
1366 skb_set_transport_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02001367
Andy Green9b8a74e2007-07-27 15:43:24 +02001368 /* pass the radiotap header up to the next stage intact */
1369 dev_queue_xmit(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001370 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02001371
1372fail:
1373 dev_kfree_skb(skb);
1374 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02001375}
1376
1377/**
1378 * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1379 * subinterfaces (wlan#, WDS, and VLAN interfaces)
1380 * @skb: packet to be sent
1381 * @dev: incoming interface
1382 *
1383 * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1384 * not be freed, and caller is responsible for either retrying later or freeing
1385 * skb).
1386 *
1387 * This function takes in an Ethernet header and encapsulates it with suitable
1388 * IEEE 802.11 header based on which interface the packet is coming in. The
1389 * encapsulated packet will then be passed to master interface, wlan#.11, for
1390 * transmission (through low-level driver).
1391 */
1392int ieee80211_subif_start_xmit(struct sk_buff *skb,
1393 struct net_device *dev)
1394{
1395 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1396 struct ieee80211_tx_packet_data *pkt_data;
1397 struct ieee80211_sub_if_data *sdata;
1398 int ret = 1, head_need;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001399 u16 ethertype, hdrlen, meshhdrlen = 0, fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02001400 struct ieee80211_hdr hdr;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001401 struct ieee80211s_hdr mesh_hdr;
Johannes Berge2ebc742007-07-27 15:43:22 +02001402 const u8 *encaps_data;
1403 int encaps_len, skip_header_bytes;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001404 int nh_pos, h_pos;
Johannes Berge2ebc742007-07-27 15:43:22 +02001405 struct sta_info *sta;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001406 u32 sta_flags = 0;
Johannes Berge2ebc742007-07-27 15:43:22 +02001407
1408 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1409 if (unlikely(skb->len < ETH_HLEN)) {
1410 printk(KERN_DEBUG "%s: short skb (len=%d)\n",
1411 dev->name, skb->len);
1412 ret = 0;
1413 goto fail;
1414 }
1415
1416 nh_pos = skb_network_header(skb) - skb->data;
1417 h_pos = skb_transport_header(skb) - skb->data;
1418
1419 /* convert Ethernet header to proper 802.11 header (based on
1420 * operation mode) */
1421 ethertype = (skb->data[12] << 8) | skb->data[13];
Johannes Berge2ebc742007-07-27 15:43:22 +02001422 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
1423
Johannes Berg51fb61e2007-12-19 01:31:27 +01001424 switch (sdata->vif.type) {
Johannes Bergcf966832007-08-28 17:01:54 -04001425 case IEEE80211_IF_TYPE_AP:
1426 case IEEE80211_IF_TYPE_VLAN:
Johannes Berge2ebc742007-07-27 15:43:22 +02001427 fc |= IEEE80211_FCTL_FROMDS;
1428 /* DA BSSID SA */
1429 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1430 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1431 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1432 hdrlen = 24;
Johannes Bergcf966832007-08-28 17:01:54 -04001433 break;
1434 case IEEE80211_IF_TYPE_WDS:
Johannes Berge2ebc742007-07-27 15:43:22 +02001435 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1436 /* RA TA DA SA */
1437 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1438 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1439 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1440 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1441 hdrlen = 30;
Johannes Bergcf966832007-08-28 17:01:54 -04001442 break;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001443#ifdef CONFIG_MAC80211_MESH
1444 case IEEE80211_IF_TYPE_MESH_POINT:
1445 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1446 /* RA TA DA SA */
1447 if (is_multicast_ether_addr(skb->data))
1448 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1449 else if (mesh_nexthop_lookup(hdr.addr1, skb, dev))
1450 return 0;
1451 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1452 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1453 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1454 if (skb->pkt_type == PACKET_OTHERHOST) {
1455 /* Forwarded frame, keep mesh ttl and seqnum */
1456 struct ieee80211s_hdr *prev_meshhdr;
1457 prev_meshhdr = ((struct ieee80211s_hdr *)skb->cb);
1458 meshhdrlen = ieee80211_get_mesh_hdrlen(prev_meshhdr);
1459 memcpy(&mesh_hdr, prev_meshhdr, meshhdrlen);
1460 sdata->u.sta.mshstats.fwded_frames++;
1461 } else {
1462 if (!sdata->u.sta.mshcfg.dot11MeshTTL) {
1463 /* Do not send frames with mesh_ttl == 0 */
1464 sdata->u.sta.mshstats.dropped_frames_ttl++;
1465 ret = 0;
1466 goto fail;
1467 }
1468 meshhdrlen = ieee80211_new_mesh_header(&mesh_hdr,
Johannes Berg902acc72008-02-23 15:17:19 +01001469 sdata);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001470 }
1471 hdrlen = 30;
1472 break;
1473#endif
Johannes Bergcf966832007-08-28 17:01:54 -04001474 case IEEE80211_IF_TYPE_STA:
Johannes Berge2ebc742007-07-27 15:43:22 +02001475 fc |= IEEE80211_FCTL_TODS;
1476 /* BSSID SA DA */
1477 memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
1478 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1479 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1480 hdrlen = 24;
Johannes Bergcf966832007-08-28 17:01:54 -04001481 break;
1482 case IEEE80211_IF_TYPE_IBSS:
Johannes Berge2ebc742007-07-27 15:43:22 +02001483 /* DA SA BSSID */
1484 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1485 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1486 memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
1487 hdrlen = 24;
Johannes Bergcf966832007-08-28 17:01:54 -04001488 break;
1489 default:
Johannes Berge2ebc742007-07-27 15:43:22 +02001490 ret = 0;
1491 goto fail;
1492 }
1493
Johannes Berg7d185b82008-01-28 17:11:43 +01001494 /*
1495 * There's no need to try to look up the destination
1496 * if it is a multicast address (which can only happen
1497 * in AP mode)
1498 */
1499 if (!is_multicast_ether_addr(hdr.addr1)) {
Johannes Bergd0709a62008-02-25 16:27:46 +01001500 rcu_read_lock();
Johannes Berg7d185b82008-01-28 17:11:43 +01001501 sta = sta_info_get(local, hdr.addr1);
Johannes Bergd0709a62008-02-25 16:27:46 +01001502 if (sta)
Johannes Berg07346f812008-05-03 01:02:02 +02001503 sta_flags = get_sta_flags(sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01001504 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001505 }
1506
Johannes Berg3434fbd2008-05-03 00:59:37 +02001507 /* receiver and we are QoS enabled, use a QoS type frame */
1508 if (sta_flags & WLAN_STA_WME && local->hw.queues >= 4) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001509 fc |= IEEE80211_STYPE_QOS_DATA;
1510 hdrlen += 2;
1511 }
1512
1513 /*
Johannes Berg238814f2008-01-28 17:19:37 +01001514 * Drop unicast frames to unauthorised stations unless they are
1515 * EAPOL frames from the local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001516 */
Johannes Berg238814f2008-01-28 17:19:37 +01001517 if (unlikely(!is_multicast_ether_addr(hdr.addr1) &&
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001518 !(sta_flags & WLAN_STA_AUTHORIZED) &&
1519 !(ethertype == ETH_P_PAE &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001520 compare_ether_addr(dev->dev_addr,
1521 skb->data + ETH_ALEN) == 0))) {
1522#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1523 DECLARE_MAC_BUF(mac);
1524
1525 if (net_ratelimit())
1526 printk(KERN_DEBUG "%s: dropped frame to %s"
1527 " (unauthorized port)\n", dev->name,
1528 print_mac(mac, hdr.addr1));
1529#endif
1530
1531 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
1532
1533 ret = 0;
1534 goto fail;
1535 }
1536
Johannes Berge2ebc742007-07-27 15:43:22 +02001537 hdr.frame_control = cpu_to_le16(fc);
1538 hdr.duration_id = 0;
1539 hdr.seq_ctrl = 0;
1540
1541 skip_header_bytes = ETH_HLEN;
1542 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1543 encaps_data = bridge_tunnel_header;
1544 encaps_len = sizeof(bridge_tunnel_header);
1545 skip_header_bytes -= 2;
1546 } else if (ethertype >= 0x600) {
1547 encaps_data = rfc1042_header;
1548 encaps_len = sizeof(rfc1042_header);
1549 skip_header_bytes -= 2;
1550 } else {
1551 encaps_data = NULL;
1552 encaps_len = 0;
1553 }
1554
1555 skb_pull(skb, skip_header_bytes);
1556 nh_pos -= skip_header_bytes;
1557 h_pos -= skip_header_bytes;
1558
1559 /* TODO: implement support for fragments so that there is no need to
1560 * reallocate and copy payload; it might be enough to support one
1561 * extra fragment that would be copied in the beginning of the frame
1562 * data.. anyway, it would be nice to include this into skb structure
1563 * somehow
1564 *
1565 * There are few options for this:
1566 * use skb->cb as an extra space for 802.11 header
1567 * allocate new buffer if not enough headroom
1568 * make sure that there is enough headroom in every skb by increasing
1569 * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
1570 * alloc_skb() (net/core/skbuff.c)
1571 */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001572 head_need = hdrlen + encaps_len + meshhdrlen + local->tx_headroom;
Johannes Berge2ebc742007-07-27 15:43:22 +02001573 head_need -= skb_headroom(skb);
1574
1575 /* We are going to modify skb data, so make a copy of it if happens to
1576 * be cloned. This could happen, e.g., with Linux bridge code passing
1577 * us broadcast frames. */
1578
David S. Miller608961a2008-05-12 21:59:32 -07001579 if (head_need > 0 || skb_header_cloned(skb)) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001580#if 0
1581 printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
1582 "of headroom\n", dev->name, head_need);
1583#endif
1584
David S. Miller608961a2008-05-12 21:59:32 -07001585 if (skb_header_cloned(skb))
Johannes Berge2ebc742007-07-27 15:43:22 +02001586 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1587 else
1588 I802_DEBUG_INC(local->tx_expand_skb_head);
1589 /* Since we have to reallocate the buffer, make sure that there
1590 * is enough room for possible WEP IV/ICV and TKIP (8 bytes
1591 * before payload and 12 after). */
1592 if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
1593 12, GFP_ATOMIC)) {
1594 printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
1595 "\n", dev->name);
1596 goto fail;
1597 }
1598 }
1599
1600 if (encaps_data) {
1601 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1602 nh_pos += encaps_len;
1603 h_pos += encaps_len;
1604 }
Johannes Bergc29b9b92007-09-14 11:10:24 -04001605
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001606 if (meshhdrlen > 0) {
1607 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
1608 nh_pos += meshhdrlen;
1609 h_pos += meshhdrlen;
1610 }
1611
Johannes Bergc29b9b92007-09-14 11:10:24 -04001612 if (fc & IEEE80211_STYPE_QOS_DATA) {
1613 __le16 *qos_control;
1614
1615 qos_control = (__le16*) skb_push(skb, 2);
1616 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
1617 /*
1618 * Maybe we could actually set some fields here, for now just
1619 * initialise to zero to indicate no special operation.
1620 */
1621 *qos_control = 0;
1622 } else
1623 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1624
Johannes Berge2ebc742007-07-27 15:43:22 +02001625 nh_pos += hdrlen;
1626 h_pos += hdrlen;
1627
1628 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1629 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
1630 pkt_data->ifindex = dev->ifindex;
Johannes Berg678f5f712007-12-19 01:31:23 +01001631 if (ethertype == ETH_P_PAE)
1632 pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
Johannes Berge2ebc742007-07-27 15:43:22 +02001633
Ivo van Doornb0a67172008-05-13 15:03:02 +02001634 /* Interfaces should always request a status report */
1635 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
1636
Johannes Berge2ebc742007-07-27 15:43:22 +02001637 skb->dev = local->mdev;
Stephen Hemminger68aae112007-08-24 11:29:34 -07001638 dev->stats.tx_packets++;
1639 dev->stats.tx_bytes += skb->len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001640
1641 /* Update skb pointers to various headers since this modified frame
1642 * is going to go through Linux networking code that may potentially
1643 * need things like pointer to IP header. */
1644 skb_set_mac_header(skb, 0);
1645 skb_set_network_header(skb, nh_pos);
1646 skb_set_transport_header(skb, h_pos);
1647
1648 dev->trans_start = jiffies;
1649 dev_queue_xmit(skb);
1650
1651 return 0;
1652
1653 fail:
1654 if (!ret)
1655 dev_kfree_skb(skb);
1656
1657 return ret;
1658}
1659
Johannes Berge2ebc742007-07-27 15:43:22 +02001660/* helper functions for pending packets for when queues are stopped */
1661
1662void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1663{
1664 int i, j;
1665 struct ieee80211_tx_stored_packet *store;
1666
1667 for (i = 0; i < local->hw.queues; i++) {
1668 if (!__ieee80211_queue_pending(local, i))
1669 continue;
1670 store = &local->pending_packet[i];
1671 kfree_skb(store->skb);
1672 for (j = 0; j < store->num_extra_frag; j++)
1673 kfree_skb(store->extra_frag[j]);
1674 kfree(store->extra_frag);
1675 clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
1676 }
1677}
1678
1679void ieee80211_tx_pending(unsigned long data)
1680{
1681 struct ieee80211_local *local = (struct ieee80211_local *)data;
1682 struct net_device *dev = local->mdev;
1683 struct ieee80211_tx_stored_packet *store;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001684 struct ieee80211_tx_data tx;
Johannes Berge2ebc742007-07-27 15:43:22 +02001685 int i, ret, reschedule = 0;
1686
1687 netif_tx_lock_bh(dev);
1688 for (i = 0; i < local->hw.queues; i++) {
1689 if (__ieee80211_queue_stopped(local, i))
1690 continue;
1691 if (!__ieee80211_queue_pending(local, i)) {
1692 reschedule = 1;
1693 continue;
1694 }
1695 store = &local->pending_packet[i];
Johannes Berg5cf121c2008-02-25 16:27:43 +01001696 tx.control = &store->control;
1697 tx.extra_frag = store->extra_frag;
1698 tx.num_extra_frag = store->num_extra_frag;
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001699 tx.last_frag_rate_idx = store->last_frag_rate_idx;
Jiri Slabybadffb72007-08-28 17:01:54 -04001700 tx.flags = 0;
1701 if (store->last_frag_rate_ctrl_probe)
Johannes Berg5cf121c2008-02-25 16:27:43 +01001702 tx.flags |= IEEE80211_TX_PROBE_LAST_FRAG;
Johannes Berge2ebc742007-07-27 15:43:22 +02001703 ret = __ieee80211_tx(local, store->skb, &tx);
1704 if (ret) {
1705 if (ret == IEEE80211_TX_FRAG_AGAIN)
1706 store->skb = NULL;
1707 } else {
1708 clear_bit(IEEE80211_LINK_STATE_PENDING,
1709 &local->state[i]);
1710 reschedule = 1;
1711 }
1712 }
1713 netif_tx_unlock_bh(dev);
1714 if (reschedule) {
1715 if (!ieee80211_qdisc_installed(dev)) {
1716 if (!__ieee80211_queue_stopped(local, 0))
1717 netif_wake_queue(dev);
1718 } else
1719 netif_schedule(dev);
1720 }
1721}
1722
1723/* functions for drivers to get certain frames */
1724
1725static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
1726 struct ieee80211_if_ap *bss,
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001727 struct sk_buff *skb,
1728 struct beacon_data *beacon)
Johannes Berge2ebc742007-07-27 15:43:22 +02001729{
1730 u8 *pos, *tim;
1731 int aid0 = 0;
1732 int i, have_bits = 0, n1, n2;
1733
1734 /* Generate bitmap for TIM only if there are any STAs in power save
1735 * mode. */
Johannes Berge2ebc742007-07-27 15:43:22 +02001736 if (atomic_read(&bss->num_sta_ps) > 0)
1737 /* in the hope that this is faster than
1738 * checking byte-for-byte */
1739 have_bits = !bitmap_empty((unsigned long*)bss->tim,
1740 IEEE80211_MAX_AID+1);
1741
1742 if (bss->dtim_count == 0)
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001743 bss->dtim_count = beacon->dtim_period - 1;
Johannes Berge2ebc742007-07-27 15:43:22 +02001744 else
1745 bss->dtim_count--;
1746
1747 tim = pos = (u8 *) skb_put(skb, 6);
1748 *pos++ = WLAN_EID_TIM;
1749 *pos++ = 4;
1750 *pos++ = bss->dtim_count;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001751 *pos++ = beacon->dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02001752
1753 if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
1754 aid0 = 1;
1755
1756 if (have_bits) {
1757 /* Find largest even number N1 so that bits numbered 1 through
1758 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
1759 * (N2 + 1) x 8 through 2007 are 0. */
1760 n1 = 0;
1761 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
1762 if (bss->tim[i]) {
1763 n1 = i & 0xfe;
1764 break;
1765 }
1766 }
1767 n2 = n1;
1768 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
1769 if (bss->tim[i]) {
1770 n2 = i;
1771 break;
1772 }
1773 }
1774
1775 /* Bitmap control */
1776 *pos++ = n1 | aid0;
1777 /* Part Virt Bitmap */
1778 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
1779
1780 tim[1] = n2 - n1 + 4;
1781 skb_put(skb, n2 - n1);
1782 } else {
1783 *pos++ = aid0; /* Bitmap control */
1784 *pos++ = 0; /* Part Virt Bitmap */
1785 }
Johannes Berge2ebc742007-07-27 15:43:22 +02001786}
1787
Johannes Berg32bfd352007-12-19 01:31:26 +01001788struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
1789 struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001790 struct ieee80211_tx_control *control)
1791{
1792 struct ieee80211_local *local = hw_to_local(hw);
1793 struct sk_buff *skb;
1794 struct net_device *bdev;
1795 struct ieee80211_sub_if_data *sdata = NULL;
1796 struct ieee80211_if_ap *ap = NULL;
Mattias Nissler1abbe492007-12-20 13:50:07 +01001797 struct rate_selection rsel;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001798 struct beacon_data *beacon;
Johannes Berg8318d782008-01-24 19:38:38 +01001799 struct ieee80211_supported_band *sband;
Johannes Berg902acc72008-02-23 15:17:19 +01001800 struct ieee80211_mgmt *mgmt;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001801 int *num_beacons;
Johannes Berg902acc72008-02-23 15:17:19 +01001802 bool err = true;
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001803 enum ieee80211_band band = local->hw.conf.channel->band;
Johannes Berg902acc72008-02-23 15:17:19 +01001804 u8 *pos;
Johannes Berg8318d782008-01-24 19:38:38 +01001805
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001806 sband = local->hw.wiphy->bands[band];
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001807
1808 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001809
Johannes Berg32bfd352007-12-19 01:31:26 +01001810 sdata = vif_to_sdata(vif);
1811 bdev = sdata->dev;
Johannes Berge2ebc742007-07-27 15:43:22 +02001812
Johannes Berg902acc72008-02-23 15:17:19 +01001813 if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001814 ap = &sdata->u.ap;
1815 beacon = rcu_dereference(ap->beacon);
Johannes Berg902acc72008-02-23 15:17:19 +01001816 if (ap && beacon) {
1817 /*
1818 * headroom, head length,
1819 * tail length and maximum TIM length
1820 */
1821 skb = dev_alloc_skb(local->tx_headroom +
1822 beacon->head_len +
1823 beacon->tail_len + 256);
1824 if (!skb)
1825 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001826
Johannes Berg902acc72008-02-23 15:17:19 +01001827 skb_reserve(skb, local->tx_headroom);
1828 memcpy(skb_put(skb, beacon->head_len), beacon->head,
1829 beacon->head_len);
1830
1831 ieee80211_include_sequence(sdata,
1832 (struct ieee80211_hdr *)skb->data);
1833
Johannes Bergd0709a62008-02-25 16:27:46 +01001834 /*
1835 * Not very nice, but we want to allow the driver to call
1836 * ieee80211_beacon_get() as a response to the set_tim()
1837 * callback. That, however, is already invoked under the
1838 * sta_lock to guarantee consistent and race-free update
1839 * of the tim bitmap in mac80211 and the driver.
1840 */
1841 if (local->tim_in_locked_section) {
1842 ieee80211_beacon_add_tim(local, ap, skb, beacon);
1843 } else {
1844 unsigned long flags;
1845
1846 spin_lock_irqsave(&local->sta_lock, flags);
1847 ieee80211_beacon_add_tim(local, ap, skb, beacon);
1848 spin_unlock_irqrestore(&local->sta_lock, flags);
1849 }
Johannes Berg902acc72008-02-23 15:17:19 +01001850
1851 if (beacon->tail)
1852 memcpy(skb_put(skb, beacon->tail_len),
1853 beacon->tail, beacon->tail_len);
1854
1855 num_beacons = &ap->num_beacons;
1856
1857 err = false;
1858 }
1859 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001860 /* headroom, head length, tail length and maximum TIM length */
Johannes Berg902acc72008-02-23 15:17:19 +01001861 skb = dev_alloc_skb(local->tx_headroom + 400);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001862 if (!skb)
1863 goto out;
1864
Johannes Berg902acc72008-02-23 15:17:19 +01001865 skb_reserve(skb, local->hw.extra_tx_headroom);
1866 mgmt = (struct ieee80211_mgmt *)
1867 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
1868 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
1869 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1870 IEEE80211_STYPE_BEACON);
1871 memset(mgmt->da, 0xff, ETH_ALEN);
1872 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1873 /* BSSID is left zeroed, wildcard value */
1874 mgmt->u.beacon.beacon_int =
1875 cpu_to_le16(local->hw.conf.beacon_int);
1876 mgmt->u.beacon.capab_info = 0x0; /* 0x0 for MPs */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001877
Johannes Berg902acc72008-02-23 15:17:19 +01001878 pos = skb_put(skb, 2);
1879 *pos++ = WLAN_EID_SSID;
1880 *pos++ = 0x0;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001881
Johannes Berg902acc72008-02-23 15:17:19 +01001882 mesh_mgmt_ies_add(skb, sdata->dev);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001883
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001884 num_beacons = &sdata->u.sta.num_beacons;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001885
Johannes Berg902acc72008-02-23 15:17:19 +01001886 err = false;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001887 }
1888
1889 if (err) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001890#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1891 if (net_ratelimit())
Johannes Berg32bfd352007-12-19 01:31:26 +01001892 printk(KERN_DEBUG "no beacon data avail for %s\n",
1893 bdev->name);
Johannes Berge2ebc742007-07-27 15:43:22 +02001894#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001895 skb = NULL;
1896 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02001897 }
1898
Johannes Berge2ebc742007-07-27 15:43:22 +02001899 if (control) {
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001900 control->band = band;
Johannes Berg8318d782008-01-24 19:38:38 +01001901 rate_control_get_rate(local->mdev, sband, skb, &rsel);
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001902 if (unlikely(rsel.rate_idx < 0)) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001903 if (net_ratelimit()) {
Mattias Nissler1abbe492007-12-20 13:50:07 +01001904 printk(KERN_DEBUG "%s: ieee80211_beacon_get: "
1905 "no rate found\n",
1906 wiphy_name(local->hw.wiphy));
Johannes Berge2ebc742007-07-27 15:43:22 +02001907 }
1908 dev_kfree_skb(skb);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001909 skb = NULL;
1910 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02001911 }
1912
Ivo van Doorn0f7054e2008-01-13 14:16:47 +01001913 control->vif = vif;
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001914 control->tx_rate_idx = rsel.rate_idx;
Johannes Berg8318d782008-01-24 19:38:38 +01001915 if (sdata->bss_conf.use_short_preamble &&
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001916 sband->bitrates[rsel.rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE)
Johannes Berg8318d782008-01-24 19:38:38 +01001917 control->flags |= IEEE80211_TXCTL_SHORT_PREAMBLE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001918 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
Johannes Berge2ebc742007-07-27 15:43:22 +02001919 control->flags |= IEEE80211_TXCTL_NO_ACK;
Ivo van Doorndf442052008-05-05 20:40:35 +02001920 control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001921 control->retry_limit = 1;
Johannes Bergd46e1442008-02-20 23:59:33 +01001922 control->flags |= IEEE80211_TXCTL_CLEAR_PS_FILT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001923 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001924 (*num_beacons)++;
1925out:
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001926 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001927 return skb;
1928}
1929EXPORT_SYMBOL(ieee80211_beacon_get);
1930
Johannes Berg32bfd352007-12-19 01:31:26 +01001931void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001932 const void *frame, size_t frame_len,
1933 const struct ieee80211_tx_control *frame_txctl,
1934 struct ieee80211_rts *rts)
1935{
1936 const struct ieee80211_hdr *hdr = frame;
1937 u16 fctl;
1938
1939 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
1940 rts->frame_control = cpu_to_le16(fctl);
Johannes Berg32bfd352007-12-19 01:31:26 +01001941 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
1942 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02001943 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
1944 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
1945}
1946EXPORT_SYMBOL(ieee80211_rts_get);
1947
Johannes Berg32bfd352007-12-19 01:31:26 +01001948void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001949 const void *frame, size_t frame_len,
1950 const struct ieee80211_tx_control *frame_txctl,
1951 struct ieee80211_cts *cts)
1952{
1953 const struct ieee80211_hdr *hdr = frame;
1954 u16 fctl;
1955
1956 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
1957 cts->frame_control = cpu_to_le16(fctl);
Johannes Berg32bfd352007-12-19 01:31:26 +01001958 cts->duration = ieee80211_ctstoself_duration(hw, vif,
1959 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02001960 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
1961}
1962EXPORT_SYMBOL(ieee80211_ctstoself_get);
1963
1964struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01001965ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
1966 struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001967 struct ieee80211_tx_control *control)
1968{
1969 struct ieee80211_local *local = hw_to_local(hw);
1970 struct sk_buff *skb;
1971 struct sta_info *sta;
1972 ieee80211_tx_handler *handler;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001973 struct ieee80211_tx_data tx;
Johannes Berg9ae54c82008-01-31 19:48:20 +01001974 ieee80211_tx_result res = TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +02001975 struct net_device *bdev;
1976 struct ieee80211_sub_if_data *sdata;
1977 struct ieee80211_if_ap *bss = NULL;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001978 struct beacon_data *beacon;
Johannes Berge2ebc742007-07-27 15:43:22 +02001979
Johannes Berg32bfd352007-12-19 01:31:26 +01001980 sdata = vif_to_sdata(vif);
1981 bdev = sdata->dev;
1982
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001983
1984 if (!bss)
Johannes Berge2ebc742007-07-27 15:43:22 +02001985 return NULL;
1986
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001987 rcu_read_lock();
1988 beacon = rcu_dereference(bss->beacon);
1989
1990 if (sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon ||
1991 !beacon->head) {
1992 rcu_read_unlock();
1993 return NULL;
1994 }
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001995
Johannes Berge2ebc742007-07-27 15:43:22 +02001996 if (bss->dtim_count != 0)
1997 return NULL; /* send buffered bc/mc only after DTIM beacon */
1998 memset(control, 0, sizeof(*control));
1999 while (1) {
2000 skb = skb_dequeue(&bss->ps_bc_buf);
2001 if (!skb)
2002 return NULL;
2003 local->total_ps_buffered--;
2004
2005 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
2006 struct ieee80211_hdr *hdr =
2007 (struct ieee80211_hdr *) skb->data;
2008 /* more buffered multicast/broadcast frames ==> set
2009 * MoreData flag in IEEE 802.11 header to inform PS
2010 * STAs */
2011 hdr->frame_control |=
2012 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2013 }
2014
Johannes Berg58d41852007-09-26 17:53:18 +02002015 if (!ieee80211_tx_prepare(&tx, skb, local->mdev, control))
Johannes Berge2ebc742007-07-27 15:43:22 +02002016 break;
2017 dev_kfree_skb_any(skb);
2018 }
2019 sta = tx.sta;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002020 tx.flags |= IEEE80211_TX_PS_BUFFERED;
2021 tx.channel = local->hw.conf.channel;
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002022 control->band = tx.channel->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02002023
Johannes Berg58905292008-01-31 19:48:25 +01002024 for (handler = ieee80211_tx_handlers; *handler != NULL; handler++) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002025 res = (*handler)(&tx);
Johannes Berg9ae54c82008-01-31 19:48:20 +01002026 if (res == TX_DROP || res == TX_QUEUED)
Johannes Berge2ebc742007-07-27 15:43:22 +02002027 break;
2028 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002029 skb = tx.skb; /* handlers are allowed to change skb */
2030
Johannes Berg9ae54c82008-01-31 19:48:20 +01002031 if (res == TX_DROP) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002032 I802_DEBUG_INC(local->tx_handlers_drop);
2033 dev_kfree_skb(skb);
2034 skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002035 } else if (res == TX_QUEUED) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002036 I802_DEBUG_INC(local->tx_handlers_queued);
2037 skb = NULL;
2038 }
2039
Johannes Bergd0709a62008-02-25 16:27:46 +01002040 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02002041
2042 return skb;
2043}
2044EXPORT_SYMBOL(ieee80211_get_buffered_bc);