blob: 38e1b2bd82452378bde7604924709ccc9a720051 [file] [log] [blame]
Johannes Berge2ebc742007-07-27 15:43:22 +02001/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 * Copyright 2007 Johannes Berg <johannes@sipsolutions.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 *
12 * Transmit and frame generation functions.
13 */
14
15#include <linux/kernel.h>
16#include <linux/slab.h>
17#include <linux/skbuff.h>
18#include <linux/etherdevice.h>
19#include <linux/bitmap.h>
Johannes Bergd4e46a32007-09-14 11:10:24 -040020#include <linux/rcupdate.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070021#include <net/net_namespace.h>
Johannes Berge2ebc742007-07-27 15:43:22 +020022#include <net/ieee80211_radiotap.h>
23#include <net/cfg80211.h>
24#include <net/mac80211.h>
25#include <asm/unaligned.h>
26
27#include "ieee80211_i.h"
28#include "ieee80211_led.h"
29#include "wep.h"
30#include "wpa.h"
31#include "wme.h"
32#include "ieee80211_rate.h"
33
34#define IEEE80211_TX_OK 0
35#define IEEE80211_TX_AGAIN 1
36#define IEEE80211_TX_FRAG_AGAIN 2
37
38/* misc utils */
39
40static inline void ieee80211_include_sequence(struct ieee80211_sub_if_data *sdata,
41 struct ieee80211_hdr *hdr)
42{
43 /* Set the sequence number for this frame. */
44 hdr->seq_ctrl = cpu_to_le16(sdata->sequence);
45
46 /* Increase the sequence number. */
47 sdata->sequence = (sdata->sequence + 0x10) & IEEE80211_SCTL_SEQ;
48}
49
50#ifdef CONFIG_MAC80211_LOWTX_FRAME_DUMP
51static void ieee80211_dump_frame(const char *ifname, const char *title,
52 const struct sk_buff *skb)
53{
54 const struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
55 u16 fc;
56 int hdrlen;
Joe Perches0795af52007-10-03 17:59:30 -070057 DECLARE_MAC_BUF(mac);
Johannes Berge2ebc742007-07-27 15:43:22 +020058
59 printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
60 if (skb->len < 4) {
61 printk("\n");
62 return;
63 }
64
65 fc = le16_to_cpu(hdr->frame_control);
66 hdrlen = ieee80211_get_hdrlen(fc);
67 if (hdrlen > skb->len)
68 hdrlen = skb->len;
69 if (hdrlen >= 4)
70 printk(" FC=0x%04x DUR=0x%04x",
71 fc, le16_to_cpu(hdr->duration_id));
72 if (hdrlen >= 10)
Joe Perches0795af52007-10-03 17:59:30 -070073 printk(" A1=%s", print_mac(mac, hdr->addr1));
Johannes Berge2ebc742007-07-27 15:43:22 +020074 if (hdrlen >= 16)
Joe Perches0795af52007-10-03 17:59:30 -070075 printk(" A2=%s", print_mac(mac, hdr->addr2));
Johannes Berge2ebc742007-07-27 15:43:22 +020076 if (hdrlen >= 24)
Joe Perches0795af52007-10-03 17:59:30 -070077 printk(" A3=%s", print_mac(mac, hdr->addr3));
Johannes Berge2ebc742007-07-27 15:43:22 +020078 if (hdrlen >= 30)
Joe Perches0795af52007-10-03 17:59:30 -070079 printk(" A4=%s", print_mac(mac, hdr->addr4));
Johannes Berge2ebc742007-07-27 15:43:22 +020080 printk("\n");
81}
82#else /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
83static inline void ieee80211_dump_frame(const char *ifname, const char *title,
84 struct sk_buff *skb)
85{
86}
87#endif /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
88
89static u16 ieee80211_duration(struct ieee80211_txrx_data *tx, int group_addr,
90 int next_frag_len)
91{
92 int rate, mrate, erp, dur, i;
93 struct ieee80211_rate *txrate = tx->u.tx.rate;
94 struct ieee80211_local *local = tx->local;
95 struct ieee80211_hw_mode *mode = tx->u.tx.mode;
96
97 erp = txrate->flags & IEEE80211_RATE_ERP;
98
99 /*
100 * data and mgmt (except PS Poll):
101 * - during CFP: 32768
102 * - during contention period:
103 * if addr1 is group address: 0
104 * if more fragments = 0 and addr1 is individual address: time to
105 * transmit one ACK plus SIFS
106 * if more fragments = 1 and addr1 is individual address: time to
107 * transmit next fragment plus 2 x ACK plus 3 x SIFS
108 *
109 * IEEE 802.11, 9.6:
110 * - control response frame (CTS or ACK) shall be transmitted using the
111 * same rate as the immediately previous frame in the frame exchange
112 * sequence, if this rate belongs to the PHY mandatory rates, or else
113 * at the highest possible rate belonging to the PHY rates in the
114 * BSSBasicRateSet
115 */
116
117 if ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) {
118 /* TODO: These control frames are not currently sent by
119 * 80211.o, but should they be implemented, this function
120 * needs to be updated to support duration field calculation.
121 *
122 * RTS: time needed to transmit pending data/mgmt frame plus
123 * one CTS frame plus one ACK frame plus 3 x SIFS
124 * CTS: duration of immediately previous RTS minus time
125 * required to transmit CTS and its SIFS
126 * ACK: 0 if immediately previous directed data/mgmt had
127 * more=0, with more=1 duration in ACK frame is duration
128 * from previous frame minus time needed to transmit ACK
129 * and its SIFS
130 * PS Poll: BIT(15) | BIT(14) | aid
131 */
132 return 0;
133 }
134
135 /* data/mgmt */
136 if (0 /* FIX: data/mgmt during CFP */)
137 return 32768;
138
139 if (group_addr) /* Group address as the destination - no ACK */
140 return 0;
141
142 /* Individual destination address:
143 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
144 * CTS and ACK frames shall be transmitted using the highest rate in
145 * basic rate set that is less than or equal to the rate of the
146 * immediately previous frame and that is using the same modulation
147 * (CCK or OFDM). If no basic rate set matches with these requirements,
148 * the highest mandatory rate of the PHY that is less than or equal to
149 * the rate of the previous frame is used.
150 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
151 */
152 rate = -1;
153 mrate = 10; /* use 1 Mbps if everything fails */
154 for (i = 0; i < mode->num_rates; i++) {
155 struct ieee80211_rate *r = &mode->rates[i];
156 if (r->rate > txrate->rate)
157 break;
158
159 if (IEEE80211_RATE_MODULATION(txrate->flags) !=
160 IEEE80211_RATE_MODULATION(r->flags))
161 continue;
162
163 if (r->flags & IEEE80211_RATE_BASIC)
164 rate = r->rate;
165 else if (r->flags & IEEE80211_RATE_MANDATORY)
166 mrate = r->rate;
167 }
168 if (rate == -1) {
169 /* No matching basic rate found; use highest suitable mandatory
170 * PHY rate */
171 rate = mrate;
172 }
173
174 /* Time needed to transmit ACK
175 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
176 * to closest integer */
177
178 dur = ieee80211_frame_duration(local, 10, rate, erp,
Johannes Berg471b3ef2007-12-28 14:32:58 +0100179 tx->sdata->bss_conf.use_short_preamble);
Johannes Berge2ebc742007-07-27 15:43:22 +0200180
181 if (next_frag_len) {
182 /* Frame is fragmented: duration increases with time needed to
183 * transmit next fragment plus ACK and 2 x SIFS. */
184 dur *= 2; /* ACK + SIFS */
185 /* next fragment */
186 dur += ieee80211_frame_duration(local, next_frag_len,
Jiri Slaby13262ff2007-08-28 17:01:54 -0400187 txrate->rate, erp,
Johannes Berg471b3ef2007-12-28 14:32:58 +0100188 tx->sdata->bss_conf.use_short_preamble);
Johannes Berge2ebc742007-07-27 15:43:22 +0200189 }
190
191 return dur;
192}
193
194static inline int __ieee80211_queue_stopped(const struct ieee80211_local *local,
195 int queue)
196{
197 return test_bit(IEEE80211_LINK_STATE_XOFF, &local->state[queue]);
198}
199
200static inline int __ieee80211_queue_pending(const struct ieee80211_local *local,
201 int queue)
202{
203 return test_bit(IEEE80211_LINK_STATE_PENDING, &local->state[queue]);
204}
205
206static int inline is_ieee80211_device(struct net_device *dev,
207 struct net_device *master)
208{
209 return (wdev_priv(dev->ieee80211_ptr) ==
210 wdev_priv(master->ieee80211_ptr));
211}
212
213/* tx handlers */
214
215static ieee80211_txrx_result
216ieee80211_tx_h_check_assoc(struct ieee80211_txrx_data *tx)
217{
218#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
219 struct sk_buff *skb = tx->skb;
220 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
221#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
222 u32 sta_flags;
223
Johannes Berg58d41852007-09-26 17:53:18 +0200224 if (unlikely(tx->flags & IEEE80211_TXRXD_TX_INJECTED))
225 return TXRX_CONTINUE;
226
Zhu Yiece8edd2007-11-22 10:53:21 +0800227 if (unlikely(tx->local->sta_sw_scanning) &&
Johannes Berge2ebc742007-07-27 15:43:22 +0200228 ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
229 (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
230 return TXRX_DROP;
231
Jiri Slabybadffb72007-08-28 17:01:54 -0400232 if (tx->flags & IEEE80211_TXRXD_TXPS_BUFFERED)
Johannes Berge2ebc742007-07-27 15:43:22 +0200233 return TXRX_CONTINUE;
234
235 sta_flags = tx->sta ? tx->sta->flags : 0;
236
Jiri Slabybadffb72007-08-28 17:01:54 -0400237 if (likely(tx->flags & IEEE80211_TXRXD_TXUNICAST)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200238 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
Johannes Berg51fb61e2007-12-19 01:31:27 +0100239 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Berge2ebc742007-07-27 15:43:22 +0200240 (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
241#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700242 DECLARE_MAC_BUF(mac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200243 printk(KERN_DEBUG "%s: dropped data frame to not "
Joe Perches0795af52007-10-03 17:59:30 -0700244 "associated station %s\n",
245 tx->dev->name, print_mac(mac, hdr->addr1));
Johannes Berge2ebc742007-07-27 15:43:22 +0200246#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
247 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
248 return TXRX_DROP;
249 }
250 } else {
251 if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
252 tx->local->num_sta == 0 &&
Johannes Berg51fb61e2007-12-19 01:31:27 +0100253 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200254 /*
255 * No associated STAs - no need to send multicast
256 * frames.
257 */
258 return TXRX_DROP;
259 }
260 return TXRX_CONTINUE;
261 }
262
Johannes Berge2ebc742007-07-27 15:43:22 +0200263 return TXRX_CONTINUE;
264}
265
266static ieee80211_txrx_result
267ieee80211_tx_h_sequence(struct ieee80211_txrx_data *tx)
268{
269 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
270
271 if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
272 ieee80211_include_sequence(tx->sdata, hdr);
273
274 return TXRX_CONTINUE;
275}
276
277/* This function is called whenever the AP is about to exceed the maximum limit
278 * of buffered frames for power saving STAs. This situation should not really
279 * happen often during normal operation, so dropping the oldest buffered packet
280 * from each queue should be OK to make some room for new frames. */
281static void purge_old_ps_buffers(struct ieee80211_local *local)
282{
283 int total = 0, purged = 0;
284 struct sk_buff *skb;
285 struct ieee80211_sub_if_data *sdata;
286 struct sta_info *sta;
287
Johannes Berg79010422007-09-18 17:29:21 -0400288 /*
289 * virtual interfaces are protected by RCU
290 */
291 rcu_read_lock();
292
293 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200294 struct ieee80211_if_ap *ap;
295 if (sdata->dev == local->mdev ||
Johannes Berg51fb61e2007-12-19 01:31:27 +0100296 sdata->vif.type != IEEE80211_IF_TYPE_AP)
Johannes Berge2ebc742007-07-27 15:43:22 +0200297 continue;
298 ap = &sdata->u.ap;
299 skb = skb_dequeue(&ap->ps_bc_buf);
300 if (skb) {
301 purged++;
302 dev_kfree_skb(skb);
303 }
304 total += skb_queue_len(&ap->ps_bc_buf);
305 }
Johannes Berg79010422007-09-18 17:29:21 -0400306 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +0200307
Michael Wube8755e2007-07-27 15:43:23 +0200308 read_lock_bh(&local->sta_lock);
Johannes Berge2ebc742007-07-27 15:43:22 +0200309 list_for_each_entry(sta, &local->sta_list, list) {
310 skb = skb_dequeue(&sta->ps_tx_buf);
311 if (skb) {
312 purged++;
313 dev_kfree_skb(skb);
314 }
315 total += skb_queue_len(&sta->ps_tx_buf);
316 }
Michael Wube8755e2007-07-27 15:43:23 +0200317 read_unlock_bh(&local->sta_lock);
Johannes Berge2ebc742007-07-27 15:43:22 +0200318
319 local->total_ps_buffered = total;
320 printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400321 wiphy_name(local->hw.wiphy), purged);
Johannes Berge2ebc742007-07-27 15:43:22 +0200322}
323
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100324static ieee80211_txrx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200325ieee80211_tx_h_multicast_ps_buf(struct ieee80211_txrx_data *tx)
326{
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100327 /*
328 * broadcast/multicast frame
329 *
330 * If any of the associated stations is in power save mode,
331 * the frame is buffered to be sent after DTIM beacon frame.
332 * This is done either by the hardware or us.
333 */
334
335 /* not AP/IBSS or ordered frame */
336 if (!tx->sdata->bss || (tx->fc & IEEE80211_FCTL_ORDER))
337 return TXRX_CONTINUE;
338
339 /* no stations in PS mode */
340 if (!atomic_read(&tx->sdata->bss->num_sta_ps))
341 return TXRX_CONTINUE;
342
343 /* buffered in mac80211 */
344 if (tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200345 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
346 purge_old_ps_buffers(tx->local);
347 if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
348 AP_MAX_BC_BUFFER) {
349 if (net_ratelimit()) {
350 printk(KERN_DEBUG "%s: BC TX buffer full - "
351 "dropping the oldest frame\n",
352 tx->dev->name);
353 }
354 dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
355 } else
356 tx->local->total_ps_buffered++;
357 skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
358 return TXRX_QUEUED;
359 }
360
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100361 /* buffered in hardware */
362 tx->u.tx.control->flags |= IEEE80211_TXCTL_SEND_AFTER_DTIM;
363
Johannes Berge2ebc742007-07-27 15:43:22 +0200364 return TXRX_CONTINUE;
365}
366
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100367static ieee80211_txrx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200368ieee80211_tx_h_unicast_ps_buf(struct ieee80211_txrx_data *tx)
369{
370 struct sta_info *sta = tx->sta;
Joe Perches0795af52007-10-03 17:59:30 -0700371 DECLARE_MAC_BUF(mac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200372
373 if (unlikely(!sta ||
374 ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
375 (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
376 return TXRX_CONTINUE;
377
378 if (unlikely((sta->flags & WLAN_STA_PS) && !sta->pspoll)) {
379 struct ieee80211_tx_packet_data *pkt_data;
380#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700381 printk(KERN_DEBUG "STA %s aid %d: PS buffer (entries "
Johannes Berge2ebc742007-07-27 15:43:22 +0200382 "before %d)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700383 print_mac(mac, sta->addr), sta->aid,
Johannes Berge2ebc742007-07-27 15:43:22 +0200384 skb_queue_len(&sta->ps_tx_buf));
385#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
386 sta->flags |= WLAN_STA_TIM;
387 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
388 purge_old_ps_buffers(tx->local);
389 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
390 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
391 if (net_ratelimit()) {
Joe Perches0795af52007-10-03 17:59:30 -0700392 printk(KERN_DEBUG "%s: STA %s TX "
Johannes Berge2ebc742007-07-27 15:43:22 +0200393 "buffer full - dropping oldest frame\n",
Joe Perches0795af52007-10-03 17:59:30 -0700394 tx->dev->name, print_mac(mac, sta->addr));
Johannes Berge2ebc742007-07-27 15:43:22 +0200395 }
396 dev_kfree_skb(old);
397 } else
398 tx->local->total_ps_buffered++;
399 /* Queue frame to be sent after STA sends an PS Poll frame */
400 if (skb_queue_empty(&sta->ps_tx_buf)) {
401 if (tx->local->ops->set_tim)
402 tx->local->ops->set_tim(local_to_hw(tx->local),
403 sta->aid, 1);
404 if (tx->sdata->bss)
405 bss_tim_set(tx->local, tx->sdata->bss, sta->aid);
406 }
407 pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
408 pkt_data->jiffies = jiffies;
409 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
410 return TXRX_QUEUED;
411 }
412#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
413 else if (unlikely(sta->flags & WLAN_STA_PS)) {
Joe Perches0795af52007-10-03 17:59:30 -0700414 printk(KERN_DEBUG "%s: STA %s in PS mode, but pspoll "
Johannes Berge2ebc742007-07-27 15:43:22 +0200415 "set -> send frame\n", tx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700416 print_mac(mac, sta->addr));
Johannes Berge2ebc742007-07-27 15:43:22 +0200417 }
418#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
419 sta->pspoll = 0;
420
421 return TXRX_CONTINUE;
422}
423
Johannes Berge2ebc742007-07-27 15:43:22 +0200424static ieee80211_txrx_result
425ieee80211_tx_h_ps_buf(struct ieee80211_txrx_data *tx)
426{
Jiri Slabybadffb72007-08-28 17:01:54 -0400427 if (unlikely(tx->flags & IEEE80211_TXRXD_TXPS_BUFFERED))
Johannes Berge2ebc742007-07-27 15:43:22 +0200428 return TXRX_CONTINUE;
429
Jiri Slabybadffb72007-08-28 17:01:54 -0400430 if (tx->flags & IEEE80211_TXRXD_TXUNICAST)
Johannes Berge2ebc742007-07-27 15:43:22 +0200431 return ieee80211_tx_h_unicast_ps_buf(tx);
432 else
433 return ieee80211_tx_h_multicast_ps_buf(tx);
434}
435
Johannes Berge2ebc742007-07-27 15:43:22 +0200436static ieee80211_txrx_result
437ieee80211_tx_h_select_key(struct ieee80211_txrx_data *tx)
438{
Johannes Bergd4e46a32007-09-14 11:10:24 -0400439 struct ieee80211_key *key;
Johannes Berg176e4f82007-12-18 15:27:47 +0100440 u16 fc = tx->fc;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400441
Johannes Berge2ebc742007-07-27 15:43:22 +0200442 if (unlikely(tx->u.tx.control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
443 tx->key = NULL;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400444 else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
445 tx->key = key;
446 else if ((key = rcu_dereference(tx->sdata->default_key)))
447 tx->key = key;
Johannes Berge2ebc742007-07-27 15:43:22 +0200448 else if (tx->sdata->drop_unencrypted &&
Johannes Berg678f5f712007-12-19 01:31:23 +0100449 !(tx->u.tx.control->flags & IEEE80211_TXCTL_EAPOL_FRAME) &&
450 !(tx->flags & IEEE80211_TXRXD_TX_INJECTED)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200451 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
452 return TXRX_DROP;
Johannes Berg176e4f82007-12-18 15:27:47 +0100453 } else
Johannes Berge2ebc742007-07-27 15:43:22 +0200454 tx->key = NULL;
455
456 if (tx->key) {
Johannes Berg176e4f82007-12-18 15:27:47 +0100457 u16 ftype, stype;
458
Johannes Berge2ebc742007-07-27 15:43:22 +0200459 tx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400460 /* TODO: add threshold stuff again */
Johannes Berg176e4f82007-12-18 15:27:47 +0100461
462 switch (tx->key->conf.alg) {
463 case ALG_WEP:
464 ftype = fc & IEEE80211_FCTL_FTYPE;
465 stype = fc & IEEE80211_FCTL_STYPE;
466
467 if (ftype == IEEE80211_FTYPE_MGMT &&
468 stype == IEEE80211_STYPE_AUTH)
469 break;
470 case ALG_TKIP:
471 case ALG_CCMP:
472 if (!WLAN_FC_DATA_PRESENT(fc))
473 tx->key = NULL;
474 break;
475 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200476 }
477
Johannes Berg176e4f82007-12-18 15:27:47 +0100478 if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
479 tx->u.tx.control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
480
Johannes Berge2ebc742007-07-27 15:43:22 +0200481 return TXRX_CONTINUE;
482}
483
484static ieee80211_txrx_result
485ieee80211_tx_h_fragment(struct ieee80211_txrx_data *tx)
486{
487 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
488 size_t hdrlen, per_fragm, num_fragm, payload_len, left;
489 struct sk_buff **frags, *first, *frag;
490 int i;
491 u16 seq;
492 u8 *pos;
493 int frag_threshold = tx->local->fragmentation_threshold;
494
Jiri Slabybadffb72007-08-28 17:01:54 -0400495 if (!(tx->flags & IEEE80211_TXRXD_FRAGMENTED))
Johannes Berge2ebc742007-07-27 15:43:22 +0200496 return TXRX_CONTINUE;
497
498 first = tx->skb;
499
500 hdrlen = ieee80211_get_hdrlen(tx->fc);
501 payload_len = first->len - hdrlen;
502 per_fragm = frag_threshold - hdrlen - FCS_LEN;
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700503 num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
Johannes Berge2ebc742007-07-27 15:43:22 +0200504
505 frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
506 if (!frags)
507 goto fail;
508
509 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
510 seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
511 pos = first->data + hdrlen + per_fragm;
512 left = payload_len - per_fragm;
513 for (i = 0; i < num_fragm - 1; i++) {
514 struct ieee80211_hdr *fhdr;
515 size_t copylen;
516
517 if (left <= 0)
518 goto fail;
519
520 /* reserve enough extra head and tail room for possible
521 * encryption */
522 frag = frags[i] =
523 dev_alloc_skb(tx->local->tx_headroom +
524 frag_threshold +
525 IEEE80211_ENCRYPT_HEADROOM +
526 IEEE80211_ENCRYPT_TAILROOM);
527 if (!frag)
528 goto fail;
529 /* Make sure that all fragments use the same priority so
530 * that they end up using the same TX queue */
531 frag->priority = first->priority;
532 skb_reserve(frag, tx->local->tx_headroom +
533 IEEE80211_ENCRYPT_HEADROOM);
534 fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
535 memcpy(fhdr, first->data, hdrlen);
536 if (i == num_fragm - 2)
537 fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
538 fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
539 copylen = left > per_fragm ? per_fragm : left;
540 memcpy(skb_put(frag, copylen), pos, copylen);
541
542 pos += copylen;
543 left -= copylen;
544 }
545 skb_trim(first, hdrlen + per_fragm);
546
547 tx->u.tx.num_extra_frag = num_fragm - 1;
548 tx->u.tx.extra_frag = frags;
549
550 return TXRX_CONTINUE;
551
552 fail:
553 printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
554 if (frags) {
555 for (i = 0; i < num_fragm - 1; i++)
556 if (frags[i])
557 dev_kfree_skb(frags[i]);
558 kfree(frags);
559 }
560 I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
561 return TXRX_DROP;
562}
563
Johannes Berge2ebc742007-07-27 15:43:22 +0200564static ieee80211_txrx_result
Johannes Berg6a22a592007-09-26 15:19:41 +0200565ieee80211_tx_h_encrypt(struct ieee80211_txrx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200566{
Johannes Berg6a22a592007-09-26 15:19:41 +0200567 if (!tx->key)
Johannes Berge2ebc742007-07-27 15:43:22 +0200568 return TXRX_CONTINUE;
569
Johannes Berg6a22a592007-09-26 15:19:41 +0200570 switch (tx->key->conf.alg) {
571 case ALG_WEP:
572 return ieee80211_crypto_wep_encrypt(tx);
573 case ALG_TKIP:
574 return ieee80211_crypto_tkip_encrypt(tx);
575 case ALG_CCMP:
576 return ieee80211_crypto_ccmp_encrypt(tx);
Johannes Berge2ebc742007-07-27 15:43:22 +0200577 }
578
Johannes Berg6a22a592007-09-26 15:19:41 +0200579 /* not reached */
580 WARN_ON(1);
581 return TXRX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200582}
583
584static ieee80211_txrx_result
585ieee80211_tx_h_rate_ctrl(struct ieee80211_txrx_data *tx)
586{
Mattias Nissler1abbe492007-12-20 13:50:07 +0100587 struct rate_selection rsel;
Johannes Berge2ebc742007-07-27 15:43:22 +0200588
Johannes Berg58d41852007-09-26 17:53:18 +0200589 if (likely(!tx->u.tx.rate)) {
Mattias Nissler1abbe492007-12-20 13:50:07 +0100590 rate_control_get_rate(tx->dev, tx->u.tx.mode, tx->skb, &rsel);
591 tx->u.tx.rate = rsel.rate;
592 if (unlikely(rsel.probe != NULL)) {
Johannes Berg58d41852007-09-26 17:53:18 +0200593 tx->u.tx.control->flags |=
594 IEEE80211_TXCTL_RATE_CTRL_PROBE;
595 tx->flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
596 tx->u.tx.control->alt_retry_rate = tx->u.tx.rate->val;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100597 tx->u.tx.rate = rsel.probe;
Johannes Berg58d41852007-09-26 17:53:18 +0200598 } else
599 tx->u.tx.control->alt_retry_rate = -1;
600
601 if (!tx->u.tx.rate)
602 return TXRX_DROP;
603 } else
Johannes Berge2ebc742007-07-27 15:43:22 +0200604 tx->u.tx.control->alt_retry_rate = -1;
Johannes Berg58d41852007-09-26 17:53:18 +0200605
Johannes Berge2ebc742007-07-27 15:43:22 +0200606 if (tx->u.tx.mode->mode == MODE_IEEE80211G &&
Johannes Berg471b3ef2007-12-28 14:32:58 +0100607 tx->sdata->bss_conf.use_cts_prot &&
Mattias Nissler1abbe492007-12-20 13:50:07 +0100608 (tx->flags & IEEE80211_TXRXD_FRAGMENTED) && rsel.nonerp) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200609 tx->u.tx.last_frag_rate = tx->u.tx.rate;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100610 if (rsel.probe)
Jiri Slabybadffb72007-08-28 17:01:54 -0400611 tx->flags &= ~IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
612 else
613 tx->flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
Mattias Nissler1abbe492007-12-20 13:50:07 +0100614 tx->u.tx.rate = rsel.nonerp;
615 tx->u.tx.control->rate = rsel.nonerp;
Johannes Berge2ebc742007-07-27 15:43:22 +0200616 tx->u.tx.control->flags &= ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
617 } else {
618 tx->u.tx.last_frag_rate = tx->u.tx.rate;
619 tx->u.tx.control->rate = tx->u.tx.rate;
620 }
621 tx->u.tx.control->tx_rate = tx->u.tx.rate->val;
Johannes Berge2ebc742007-07-27 15:43:22 +0200622
623 return TXRX_CONTINUE;
624}
625
626static ieee80211_txrx_result
627ieee80211_tx_h_misc(struct ieee80211_txrx_data *tx)
628{
629 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200630 u16 fc = le16_to_cpu(hdr->frame_control);
Johannes Berge2ebc742007-07-27 15:43:22 +0200631 u16 dur;
632 struct ieee80211_tx_control *control = tx->u.tx.control;
633 struct ieee80211_hw_mode *mode = tx->u.tx.mode;
634
Johannes Berg58d41852007-09-26 17:53:18 +0200635 if (!control->retry_limit) {
636 if (!is_multicast_ether_addr(hdr->addr1)) {
637 if (tx->skb->len + FCS_LEN > tx->local->rts_threshold
638 && tx->local->rts_threshold <
639 IEEE80211_MAX_RTS_THRESHOLD) {
640 control->flags |=
641 IEEE80211_TXCTL_USE_RTS_CTS;
642 control->flags |=
643 IEEE80211_TXCTL_LONG_RETRY_LIMIT;
644 control->retry_limit =
645 tx->local->long_retry_limit;
646 } else {
647 control->retry_limit =
648 tx->local->short_retry_limit;
649 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200650 } else {
Johannes Berg58d41852007-09-26 17:53:18 +0200651 control->retry_limit = 1;
Johannes Berge2ebc742007-07-27 15:43:22 +0200652 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200653 }
654
Jiri Slabybadffb72007-08-28 17:01:54 -0400655 if (tx->flags & IEEE80211_TXRXD_FRAGMENTED) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200656 /* Do not use multiple retry rates when sending fragmented
657 * frames.
658 * TODO: The last fragment could still use multiple retry
659 * rates. */
660 control->alt_retry_rate = -1;
661 }
662
663 /* Use CTS protection for unicast frames sent using extended rates if
664 * there are associated non-ERP stations and RTS/CTS is not configured
665 * for the frame. */
666 if (mode->mode == MODE_IEEE80211G &&
667 (tx->u.tx.rate->flags & IEEE80211_RATE_ERP) &&
Jiri Slabybadffb72007-08-28 17:01:54 -0400668 (tx->flags & IEEE80211_TXRXD_TXUNICAST) &&
Johannes Berg471b3ef2007-12-28 14:32:58 +0100669 tx->sdata->bss_conf.use_cts_prot &&
Johannes Berge2ebc742007-07-27 15:43:22 +0200670 !(control->flags & IEEE80211_TXCTL_USE_RTS_CTS))
671 control->flags |= IEEE80211_TXCTL_USE_CTS_PROTECT;
672
Daniel Drake7e9ed182007-07-27 15:43:24 +0200673 /* Transmit data frames using short preambles if the driver supports
674 * short preambles at the selected rate and short preambles are
675 * available on the network at the current point in time. */
676 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
677 (tx->u.tx.rate->flags & IEEE80211_RATE_PREAMBLE2) &&
Johannes Berg471b3ef2007-12-28 14:32:58 +0100678 tx->sdata->bss_conf.use_short_preamble &&
Daniel Drake7e9ed182007-07-27 15:43:24 +0200679 (!tx->sta || (tx->sta->flags & WLAN_STA_SHORT_PREAMBLE))) {
680 tx->u.tx.control->tx_rate = tx->u.tx.rate->val2;
681 }
682
Johannes Berge2ebc742007-07-27 15:43:22 +0200683 /* Setup duration field for the first fragment of the frame. Duration
684 * for remaining fragments will be updated when they are being sent
685 * to low-level driver in ieee80211_tx(). */
686 dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
Jiri Slabybadffb72007-08-28 17:01:54 -0400687 (tx->flags & IEEE80211_TXRXD_FRAGMENTED) ?
688 tx->u.tx.extra_frag[0]->len : 0);
Johannes Berge2ebc742007-07-27 15:43:22 +0200689 hdr->duration_id = cpu_to_le16(dur);
690
691 if ((control->flags & IEEE80211_TXCTL_USE_RTS_CTS) ||
692 (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)) {
693 struct ieee80211_rate *rate;
694
695 /* Do not use multiple retry rates when using RTS/CTS */
696 control->alt_retry_rate = -1;
697
698 /* Use min(data rate, max base rate) as CTS/RTS rate */
699 rate = tx->u.tx.rate;
700 while (rate > mode->rates &&
701 !(rate->flags & IEEE80211_RATE_BASIC))
702 rate--;
703
704 control->rts_cts_rate = rate->val;
705 control->rts_rate = rate;
706 }
707
708 if (tx->sta) {
709 tx->sta->tx_packets++;
710 tx->sta->tx_fragments++;
711 tx->sta->tx_bytes += tx->skb->len;
712 if (tx->u.tx.extra_frag) {
713 int i;
714 tx->sta->tx_fragments += tx->u.tx.num_extra_frag;
715 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
716 tx->sta->tx_bytes +=
717 tx->u.tx.extra_frag[i]->len;
718 }
719 }
720 }
721
722 return TXRX_CONTINUE;
723}
724
725static ieee80211_txrx_result
726ieee80211_tx_h_load_stats(struct ieee80211_txrx_data *tx)
727{
728 struct ieee80211_local *local = tx->local;
729 struct ieee80211_hw_mode *mode = tx->u.tx.mode;
730 struct sk_buff *skb = tx->skb;
731 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
732 u32 load = 0, hdrtime;
733
734 /* TODO: this could be part of tx_status handling, so that the number
735 * of retries would be known; TX rate should in that case be stored
736 * somewhere with the packet */
737
738 /* Estimate total channel use caused by this frame */
739
740 /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
741 * 1 usec = 1/8 * (1080 / 10) = 13.5 */
742
743 if (mode->mode == MODE_IEEE80211A ||
Johannes Berge2ebc742007-07-27 15:43:22 +0200744 (mode->mode == MODE_IEEE80211G &&
745 tx->u.tx.rate->flags & IEEE80211_RATE_ERP))
746 hdrtime = CHAN_UTIL_HDR_SHORT;
747 else
748 hdrtime = CHAN_UTIL_HDR_LONG;
749
750 load = hdrtime;
751 if (!is_multicast_ether_addr(hdr->addr1))
752 load += hdrtime;
753
754 if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_RTS_CTS)
755 load += 2 * hdrtime;
756 else if (tx->u.tx.control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
757 load += hdrtime;
758
759 load += skb->len * tx->u.tx.rate->rate_inv;
760
761 if (tx->u.tx.extra_frag) {
762 int i;
763 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
764 load += 2 * hdrtime;
765 load += tx->u.tx.extra_frag[i]->len *
766 tx->u.tx.rate->rate;
767 }
768 }
769
770 /* Divide channel_use by 8 to avoid wrapping around the counter */
771 load >>= CHAN_UTIL_SHIFT;
772 local->channel_use_raw += load;
773 if (tx->sta)
774 tx->sta->channel_use_raw += load;
775 tx->sdata->channel_use_raw += load;
776
777 return TXRX_CONTINUE;
778}
779
780/* TODO: implement register/unregister functions for adding TX/RX handlers
781 * into ordered list */
782
783ieee80211_tx_handler ieee80211_tx_handlers[] =
784{
785 ieee80211_tx_h_check_assoc,
786 ieee80211_tx_h_sequence,
787 ieee80211_tx_h_ps_buf,
788 ieee80211_tx_h_select_key,
789 ieee80211_tx_h_michael_mic_add,
790 ieee80211_tx_h_fragment,
Johannes Berg6a22a592007-09-26 15:19:41 +0200791 ieee80211_tx_h_encrypt,
Johannes Berge2ebc742007-07-27 15:43:22 +0200792 ieee80211_tx_h_rate_ctrl,
793 ieee80211_tx_h_misc,
794 ieee80211_tx_h_load_stats,
795 NULL
796};
797
798/* actual transmit path */
799
800/*
801 * deal with packet injection down monitor interface
802 * with Radiotap Header -- only called for monitor mode interface
803 */
804static ieee80211_txrx_result
Johannes Berg58d41852007-09-26 17:53:18 +0200805__ieee80211_parse_tx_radiotap(struct ieee80211_txrx_data *tx,
806 struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +0200807{
808 /*
809 * this is the moment to interpret and discard the radiotap header that
810 * must be at the start of the packet injected in Monitor mode
811 *
812 * Need to take some care with endian-ness since radiotap
813 * args are little-endian
814 */
815
816 struct ieee80211_radiotap_iterator iterator;
817 struct ieee80211_radiotap_header *rthdr =
818 (struct ieee80211_radiotap_header *) skb->data;
819 struct ieee80211_hw_mode *mode = tx->local->hw.conf.mode;
820 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
Johannes Berg58d41852007-09-26 17:53:18 +0200821 struct ieee80211_tx_control *control = tx->u.tx.control;
Johannes Berge2ebc742007-07-27 15:43:22 +0200822
Johannes Berg58d41852007-09-26 17:53:18 +0200823 control->flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
824 tx->flags |= IEEE80211_TXRXD_TX_INJECTED;
825 tx->flags &= ~IEEE80211_TXRXD_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200826
827 /*
828 * for every radiotap entry that is present
829 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
830 * entries present, or -EINVAL on error)
831 */
832
833 while (!ret) {
834 int i, target_rate;
835
836 ret = ieee80211_radiotap_iterator_next(&iterator);
837
838 if (ret)
839 continue;
840
841 /* see if this argument is something we can use */
842 switch (iterator.this_arg_index) {
843 /*
844 * You must take care when dereferencing iterator.this_arg
845 * for multibyte types... the pointer is not aligned. Use
846 * get_unaligned((type *)iterator.this_arg) to dereference
847 * iterator.this_arg for type "type" safely on all arches.
848 */
849 case IEEE80211_RADIOTAP_RATE:
850 /*
851 * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
852 * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
853 */
854 target_rate = (*iterator.this_arg) * 5;
855 for (i = 0; i < mode->num_rates; i++) {
856 struct ieee80211_rate *r = &mode->rates[i];
857
Johannes Berg58d41852007-09-26 17:53:18 +0200858 if (r->rate == target_rate) {
859 tx->u.tx.rate = r;
860 break;
861 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200862 }
863 break;
864
865 case IEEE80211_RADIOTAP_ANTENNA:
866 /*
867 * radiotap uses 0 for 1st ant, mac80211 is 1 for
868 * 1st ant
869 */
870 control->antenna_sel_tx = (*iterator.this_arg) + 1;
871 break;
872
873 case IEEE80211_RADIOTAP_DBM_TX_POWER:
874 control->power_level = *iterator.this_arg;
875 break;
876
877 case IEEE80211_RADIOTAP_FLAGS:
878 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
879 /*
880 * this indicates that the skb we have been
881 * handed has the 32-bit FCS CRC at the end...
882 * we should react to that by snipping it off
883 * because it will be recomputed and added
884 * on transmission
885 */
886 if (skb->len < (iterator.max_length + FCS_LEN))
887 return TXRX_DROP;
888
889 skb_trim(skb, skb->len - FCS_LEN);
890 }
Johannes Berg58d41852007-09-26 17:53:18 +0200891 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
892 control->flags &=
893 ~IEEE80211_TXCTL_DO_NOT_ENCRYPT;
894 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
895 tx->flags |= IEEE80211_TXRXD_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200896 break;
897
Johannes Berg58d41852007-09-26 17:53:18 +0200898 /*
899 * Please update the file
900 * Documentation/networking/mac80211-injection.txt
901 * when parsing new fields here.
902 */
903
Johannes Berge2ebc742007-07-27 15:43:22 +0200904 default:
905 break;
906 }
907 }
908
909 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
910 return TXRX_DROP;
911
912 /*
913 * remove the radiotap header
914 * iterator->max_length was sanity-checked against
915 * skb->len by iterator init
916 */
917 skb_pull(skb, iterator.max_length);
918
919 return TXRX_CONTINUE;
920}
921
Johannes Berg58d41852007-09-26 17:53:18 +0200922/*
923 * initialises @tx
924 */
925static ieee80211_txrx_result
Johannes Berge2ebc742007-07-27 15:43:22 +0200926__ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
927 struct sk_buff *skb,
928 struct net_device *dev,
929 struct ieee80211_tx_control *control)
930{
931 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg58d41852007-09-26 17:53:18 +0200932 struct ieee80211_hdr *hdr;
Johannes Berge2ebc742007-07-27 15:43:22 +0200933 struct ieee80211_sub_if_data *sdata;
Johannes Berge2ebc742007-07-27 15:43:22 +0200934
935 int hdrlen;
936
937 memset(tx, 0, sizeof(*tx));
938 tx->skb = skb;
939 tx->dev = dev; /* use original interface */
940 tx->local = local;
941 tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg58d41852007-09-26 17:53:18 +0200942 tx->u.tx.control = control;
Johannes Berge2ebc742007-07-27 15:43:22 +0200943 /*
Johannes Berg58d41852007-09-26 17:53:18 +0200944 * Set this flag (used below to indicate "automatic fragmentation"),
945 * it will be cleared/left by radiotap as desired.
Johannes Berge2ebc742007-07-27 15:43:22 +0200946 */
Johannes Berg58d41852007-09-26 17:53:18 +0200947 tx->flags |= IEEE80211_TXRXD_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200948
949 /* process and remove the injection radiotap header */
950 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +0100951 if (unlikely(sdata->vif.type == IEEE80211_IF_TYPE_MNTR)) {
Johannes Berg58d41852007-09-26 17:53:18 +0200952 if (__ieee80211_parse_tx_radiotap(tx, skb) == TXRX_DROP)
Johannes Berge2ebc742007-07-27 15:43:22 +0200953 return TXRX_DROP;
Johannes Berg58d41852007-09-26 17:53:18 +0200954
Johannes Berge2ebc742007-07-27 15:43:22 +0200955 /*
Johannes Berg58d41852007-09-26 17:53:18 +0200956 * __ieee80211_parse_tx_radiotap has now removed
957 * the radiotap header that was present and pre-filled
958 * 'tx' with tx control information.
Johannes Berge2ebc742007-07-27 15:43:22 +0200959 */
Johannes Berge2ebc742007-07-27 15:43:22 +0200960 }
961
Johannes Berg58d41852007-09-26 17:53:18 +0200962 hdr = (struct ieee80211_hdr *) skb->data;
963
warmcat24338792007-09-14 11:10:25 -0400964 tx->sta = sta_info_get(local, hdr->addr1);
965 tx->fc = le16_to_cpu(hdr->frame_control);
Johannes Berg58d41852007-09-26 17:53:18 +0200966
Jiri Slabybadffb72007-08-28 17:01:54 -0400967 if (is_multicast_ether_addr(hdr->addr1)) {
968 tx->flags &= ~IEEE80211_TXRXD_TXUNICAST;
Johannes Berge2ebc742007-07-27 15:43:22 +0200969 control->flags |= IEEE80211_TXCTL_NO_ACK;
Jiri Slabybadffb72007-08-28 17:01:54 -0400970 } else {
971 tx->flags |= IEEE80211_TXRXD_TXUNICAST;
Johannes Berge2ebc742007-07-27 15:43:22 +0200972 control->flags &= ~IEEE80211_TXCTL_NO_ACK;
Jiri Slabybadffb72007-08-28 17:01:54 -0400973 }
Johannes Berg58d41852007-09-26 17:53:18 +0200974
975 if (tx->flags & IEEE80211_TXRXD_FRAGMENTED) {
976 if ((tx->flags & IEEE80211_TXRXD_TXUNICAST) &&
977 skb->len + FCS_LEN > local->fragmentation_threshold &&
978 !local->ops->set_frag_threshold)
979 tx->flags |= IEEE80211_TXRXD_FRAGMENTED;
980 else
981 tx->flags &= ~IEEE80211_TXRXD_FRAGMENTED;
982 }
983
Johannes Berge2ebc742007-07-27 15:43:22 +0200984 if (!tx->sta)
985 control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
986 else if (tx->sta->clear_dst_mask) {
987 control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
988 tx->sta->clear_dst_mask = 0;
989 }
Johannes Berg58d41852007-09-26 17:53:18 +0200990
Johannes Berge2ebc742007-07-27 15:43:22 +0200991 hdrlen = ieee80211_get_hdrlen(tx->fc);
992 if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
993 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
994 tx->ethertype = (pos[0] << 8) | pos[1];
995 }
996 control->flags |= IEEE80211_TXCTL_FIRST_FRAGMENT;
997
Johannes Berg6d65f5d2007-12-23 10:11:55 +0100998 return TXRX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200999}
1000
Johannes Berg32bfd352007-12-19 01:31:26 +01001001/*
Johannes Berg58d41852007-09-26 17:53:18 +02001002 * NB: @tx is uninitialised when passed in here
1003 */
1004static int ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
1005 struct sk_buff *skb,
1006 struct net_device *mdev,
1007 struct ieee80211_tx_control *control)
Johannes Berge2ebc742007-07-27 15:43:22 +02001008{
1009 struct ieee80211_tx_packet_data *pkt_data;
1010 struct net_device *dev;
1011
1012 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001013 dev = dev_get_by_index(&init_net, pkt_data->ifindex);
Johannes Berge2ebc742007-07-27 15:43:22 +02001014 if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
1015 dev_put(dev);
1016 dev = NULL;
1017 }
1018 if (unlikely(!dev))
1019 return -ENODEV;
Johannes Berg58d41852007-09-26 17:53:18 +02001020 /* initialises tx with control */
Johannes Berge2ebc742007-07-27 15:43:22 +02001021 __ieee80211_tx_prepare(tx, skb, dev, control);
Johannes Berg32bfd352007-12-19 01:31:26 +01001022 dev_put(dev);
Johannes Berge2ebc742007-07-27 15:43:22 +02001023 return 0;
1024}
1025
1026static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
1027 struct ieee80211_txrx_data *tx)
1028{
1029 struct ieee80211_tx_control *control = tx->u.tx.control;
1030 int ret, i;
1031
1032 if (!ieee80211_qdisc_installed(local->mdev) &&
1033 __ieee80211_queue_stopped(local, 0)) {
1034 netif_stop_queue(local->mdev);
1035 return IEEE80211_TX_AGAIN;
1036 }
1037 if (skb) {
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001038 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
1039 "TX to low-level driver", skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001040 ret = local->ops->tx(local_to_hw(local), skb, control);
1041 if (ret)
1042 return IEEE80211_TX_AGAIN;
1043 local->mdev->trans_start = jiffies;
1044 ieee80211_led_tx(local, 1);
1045 }
1046 if (tx->u.tx.extra_frag) {
1047 control->flags &= ~(IEEE80211_TXCTL_USE_RTS_CTS |
1048 IEEE80211_TXCTL_USE_CTS_PROTECT |
1049 IEEE80211_TXCTL_CLEAR_DST_MASK |
1050 IEEE80211_TXCTL_FIRST_FRAGMENT);
1051 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
1052 if (!tx->u.tx.extra_frag[i])
1053 continue;
1054 if (__ieee80211_queue_stopped(local, control->queue))
1055 return IEEE80211_TX_FRAG_AGAIN;
1056 if (i == tx->u.tx.num_extra_frag) {
1057 control->tx_rate = tx->u.tx.last_frag_hwrate;
1058 control->rate = tx->u.tx.last_frag_rate;
Jiri Slabybadffb72007-08-28 17:01:54 -04001059 if (tx->flags & IEEE80211_TXRXD_TXPROBE_LAST_FRAG)
Johannes Berge2ebc742007-07-27 15:43:22 +02001060 control->flags |=
1061 IEEE80211_TXCTL_RATE_CTRL_PROBE;
1062 else
1063 control->flags &=
1064 ~IEEE80211_TXCTL_RATE_CTRL_PROBE;
1065 }
1066
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001067 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
Johannes Berge2ebc742007-07-27 15:43:22 +02001068 "TX to low-level driver",
1069 tx->u.tx.extra_frag[i]);
1070 ret = local->ops->tx(local_to_hw(local),
1071 tx->u.tx.extra_frag[i],
1072 control);
1073 if (ret)
1074 return IEEE80211_TX_FRAG_AGAIN;
1075 local->mdev->trans_start = jiffies;
1076 ieee80211_led_tx(local, 1);
1077 tx->u.tx.extra_frag[i] = NULL;
1078 }
1079 kfree(tx->u.tx.extra_frag);
1080 tx->u.tx.extra_frag = NULL;
1081 }
1082 return IEEE80211_TX_OK;
1083}
1084
1085static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
Johannes Bergf9d540e2007-09-28 14:02:09 +02001086 struct ieee80211_tx_control *control)
Johannes Berge2ebc742007-07-27 15:43:22 +02001087{
1088 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1089 struct sta_info *sta;
1090 ieee80211_tx_handler *handler;
1091 struct ieee80211_txrx_data tx;
1092 ieee80211_txrx_result res = TXRX_DROP, res_prepare;
1093 int ret, i;
1094
1095 WARN_ON(__ieee80211_queue_pending(local, control->queue));
1096
1097 if (unlikely(skb->len < 10)) {
1098 dev_kfree_skb(skb);
1099 return 0;
1100 }
1101
Johannes Berg58d41852007-09-26 17:53:18 +02001102 /* initialises tx */
Johannes Berge2ebc742007-07-27 15:43:22 +02001103 res_prepare = __ieee80211_tx_prepare(&tx, skb, dev, control);
1104
1105 if (res_prepare == TXRX_DROP) {
1106 dev_kfree_skb(skb);
1107 return 0;
1108 }
1109
Johannes Bergd4e46a32007-09-14 11:10:24 -04001110 /*
1111 * key references are protected using RCU and this requires that
1112 * we are in a read-site RCU section during receive processing
1113 */
1114 rcu_read_lock();
1115
Johannes Berge2ebc742007-07-27 15:43:22 +02001116 sta = tx.sta;
Johannes Berge2ebc742007-07-27 15:43:22 +02001117 tx.u.tx.mode = local->hw.conf.mode;
1118
Johannes Berg58d41852007-09-26 17:53:18 +02001119 for (handler = local->tx_handlers; *handler != NULL;
1120 handler++) {
1121 res = (*handler)(&tx);
1122 if (res != TXRX_CONTINUE)
1123 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02001124 }
1125
1126 skb = tx.skb; /* handlers are allowed to change skb */
1127
1128 if (sta)
1129 sta_info_put(sta);
1130
1131 if (unlikely(res == TXRX_DROP)) {
1132 I802_DEBUG_INC(local->tx_handlers_drop);
1133 goto drop;
1134 }
1135
1136 if (unlikely(res == TXRX_QUEUED)) {
1137 I802_DEBUG_INC(local->tx_handlers_queued);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001138 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001139 return 0;
1140 }
1141
1142 if (tx.u.tx.extra_frag) {
1143 for (i = 0; i < tx.u.tx.num_extra_frag; i++) {
1144 int next_len, dur;
1145 struct ieee80211_hdr *hdr =
1146 (struct ieee80211_hdr *)
1147 tx.u.tx.extra_frag[i]->data;
1148
1149 if (i + 1 < tx.u.tx.num_extra_frag) {
1150 next_len = tx.u.tx.extra_frag[i + 1]->len;
1151 } else {
1152 next_len = 0;
1153 tx.u.tx.rate = tx.u.tx.last_frag_rate;
1154 tx.u.tx.last_frag_hwrate = tx.u.tx.rate->val;
1155 }
1156 dur = ieee80211_duration(&tx, 0, next_len);
1157 hdr->duration_id = cpu_to_le16(dur);
1158 }
1159 }
1160
1161retry:
1162 ret = __ieee80211_tx(local, skb, &tx);
1163 if (ret) {
1164 struct ieee80211_tx_stored_packet *store =
1165 &local->pending_packet[control->queue];
1166
1167 if (ret == IEEE80211_TX_FRAG_AGAIN)
1168 skb = NULL;
1169 set_bit(IEEE80211_LINK_STATE_PENDING,
1170 &local->state[control->queue]);
1171 smp_mb();
1172 /* When the driver gets out of buffers during sending of
1173 * fragments and calls ieee80211_stop_queue, there is
1174 * a small window between IEEE80211_LINK_STATE_XOFF and
1175 * IEEE80211_LINK_STATE_PENDING flags are set. If a buffer
1176 * gets available in that window (i.e. driver calls
1177 * ieee80211_wake_queue), we would end up with ieee80211_tx
1178 * called with IEEE80211_LINK_STATE_PENDING. Prevent this by
1179 * continuing transmitting here when that situation is
1180 * possible to have happened. */
1181 if (!__ieee80211_queue_stopped(local, control->queue)) {
1182 clear_bit(IEEE80211_LINK_STATE_PENDING,
1183 &local->state[control->queue]);
1184 goto retry;
1185 }
1186 memcpy(&store->control, control,
1187 sizeof(struct ieee80211_tx_control));
1188 store->skb = skb;
1189 store->extra_frag = tx.u.tx.extra_frag;
1190 store->num_extra_frag = tx.u.tx.num_extra_frag;
1191 store->last_frag_hwrate = tx.u.tx.last_frag_hwrate;
1192 store->last_frag_rate = tx.u.tx.last_frag_rate;
Jiri Slabybadffb72007-08-28 17:01:54 -04001193 store->last_frag_rate_ctrl_probe =
1194 !!(tx.flags & IEEE80211_TXRXD_TXPROBE_LAST_FRAG);
Johannes Berge2ebc742007-07-27 15:43:22 +02001195 }
Johannes Bergd4e46a32007-09-14 11:10:24 -04001196 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001197 return 0;
1198
1199 drop:
1200 if (skb)
1201 dev_kfree_skb(skb);
1202 for (i = 0; i < tx.u.tx.num_extra_frag; i++)
1203 if (tx.u.tx.extra_frag[i])
1204 dev_kfree_skb(tx.u.tx.extra_frag[i]);
1205 kfree(tx.u.tx.extra_frag);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001206 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001207 return 0;
1208}
1209
1210/* device xmit handlers */
1211
1212int ieee80211_master_start_xmit(struct sk_buff *skb,
1213 struct net_device *dev)
1214{
1215 struct ieee80211_tx_control control;
1216 struct ieee80211_tx_packet_data *pkt_data;
1217 struct net_device *odev = NULL;
1218 struct ieee80211_sub_if_data *osdata;
1219 int headroom;
1220 int ret;
1221
1222 /*
1223 * copy control out of the skb so other people can use skb->cb
1224 */
1225 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1226 memset(&control, 0, sizeof(struct ieee80211_tx_control));
1227
1228 if (pkt_data->ifindex)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001229 odev = dev_get_by_index(&init_net, pkt_data->ifindex);
Johannes Berge2ebc742007-07-27 15:43:22 +02001230 if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
1231 dev_put(odev);
1232 odev = NULL;
1233 }
1234 if (unlikely(!odev)) {
1235#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1236 printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
1237 "originating device\n", dev->name);
1238#endif
1239 dev_kfree_skb(skb);
1240 return 0;
1241 }
1242 osdata = IEEE80211_DEV_TO_SUB_IF(odev);
1243
1244 headroom = osdata->local->tx_headroom + IEEE80211_ENCRYPT_HEADROOM;
1245 if (skb_headroom(skb) < headroom) {
1246 if (pskb_expand_head(skb, headroom, 0, GFP_ATOMIC)) {
1247 dev_kfree_skb(skb);
1248 dev_put(odev);
1249 return 0;
1250 }
1251 }
1252
Johannes Berg32bfd352007-12-19 01:31:26 +01001253 control.vif = &osdata->vif;
Johannes Berg51fb61e2007-12-19 01:31:27 +01001254 control.type = osdata->vif.type;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001255 if (pkt_data->flags & IEEE80211_TXPD_REQ_TX_STATUS)
Johannes Berge2ebc742007-07-27 15:43:22 +02001256 control.flags |= IEEE80211_TXCTL_REQ_TX_STATUS;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001257 if (pkt_data->flags & IEEE80211_TXPD_DO_NOT_ENCRYPT)
Johannes Berge2ebc742007-07-27 15:43:22 +02001258 control.flags |= IEEE80211_TXCTL_DO_NOT_ENCRYPT;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001259 if (pkt_data->flags & IEEE80211_TXPD_REQUEUE)
Johannes Berge2ebc742007-07-27 15:43:22 +02001260 control.flags |= IEEE80211_TXCTL_REQUEUE;
Johannes Berg678f5f712007-12-19 01:31:23 +01001261 if (pkt_data->flags & IEEE80211_TXPD_EAPOL_FRAME)
1262 control.flags |= IEEE80211_TXCTL_EAPOL_FRAME;
Ron Rindjunsky9e723492008-01-28 14:07:18 +02001263 if (pkt_data->flags & IEEE80211_TXPD_AMPDU)
1264 control.flags |= IEEE80211_TXCTL_AMPDU;
Johannes Berge2ebc742007-07-27 15:43:22 +02001265 control.queue = pkt_data->queue;
1266
Johannes Bergf9d540e2007-09-28 14:02:09 +02001267 ret = ieee80211_tx(odev, skb, &control);
Johannes Berge2ebc742007-07-27 15:43:22 +02001268 dev_put(odev);
1269
1270 return ret;
1271}
1272
1273int ieee80211_monitor_start_xmit(struct sk_buff *skb,
1274 struct net_device *dev)
1275{
1276 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1277 struct ieee80211_tx_packet_data *pkt_data;
1278 struct ieee80211_radiotap_header *prthdr =
1279 (struct ieee80211_radiotap_header *)skb->data;
Andy Green9b8a74e2007-07-27 15:43:24 +02001280 u16 len_rthdr;
Johannes Berge2ebc742007-07-27 15:43:22 +02001281
Andy Green9b8a74e2007-07-27 15:43:24 +02001282 /* check for not even having the fixed radiotap header part */
1283 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1284 goto fail; /* too short to be possibly valid */
1285
1286 /* is it a header version we can trust to find length from? */
1287 if (unlikely(prthdr->it_version))
1288 goto fail; /* only version 0 is supported */
1289
1290 /* then there must be a radiotap header with a length we can use */
1291 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1292
1293 /* does the skb contain enough to deliver on the alleged length? */
1294 if (unlikely(skb->len < len_rthdr))
1295 goto fail; /* skb too short for claimed rt header extent */
Johannes Berge2ebc742007-07-27 15:43:22 +02001296
1297 skb->dev = local->mdev;
1298
1299 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1300 memset(pkt_data, 0, sizeof(*pkt_data));
Andy Green9b8a74e2007-07-27 15:43:24 +02001301 /* needed because we set skb device to master */
Johannes Berge2ebc742007-07-27 15:43:22 +02001302 pkt_data->ifindex = dev->ifindex;
Andy Green9b8a74e2007-07-27 15:43:24 +02001303
Jiri Slabye8bf9642007-08-28 17:01:54 -04001304 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001305
Johannes Berge2ebc742007-07-27 15:43:22 +02001306 /*
1307 * fix up the pointers accounting for the radiotap
1308 * header still being in there. We are being given
1309 * a precooked IEEE80211 header so no need for
1310 * normal processing
1311 */
Andy Green9b8a74e2007-07-27 15:43:24 +02001312 skb_set_mac_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02001313 /*
Andy Green9b8a74e2007-07-27 15:43:24 +02001314 * these are just fixed to the end of the rt area since we
1315 * don't have any better information and at this point, nobody cares
Johannes Berge2ebc742007-07-27 15:43:22 +02001316 */
Andy Green9b8a74e2007-07-27 15:43:24 +02001317 skb_set_network_header(skb, len_rthdr);
1318 skb_set_transport_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02001319
Andy Green9b8a74e2007-07-27 15:43:24 +02001320 /* pass the radiotap header up to the next stage intact */
1321 dev_queue_xmit(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001322 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02001323
1324fail:
1325 dev_kfree_skb(skb);
1326 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02001327}
1328
1329/**
1330 * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1331 * subinterfaces (wlan#, WDS, and VLAN interfaces)
1332 * @skb: packet to be sent
1333 * @dev: incoming interface
1334 *
1335 * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1336 * not be freed, and caller is responsible for either retrying later or freeing
1337 * skb).
1338 *
1339 * This function takes in an Ethernet header and encapsulates it with suitable
1340 * IEEE 802.11 header based on which interface the packet is coming in. The
1341 * encapsulated packet will then be passed to master interface, wlan#.11, for
1342 * transmission (through low-level driver).
1343 */
1344int ieee80211_subif_start_xmit(struct sk_buff *skb,
1345 struct net_device *dev)
1346{
1347 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1348 struct ieee80211_tx_packet_data *pkt_data;
1349 struct ieee80211_sub_if_data *sdata;
1350 int ret = 1, head_need;
1351 u16 ethertype, hdrlen, fc;
1352 struct ieee80211_hdr hdr;
1353 const u8 *encaps_data;
1354 int encaps_len, skip_header_bytes;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001355 int nh_pos, h_pos;
Johannes Berge2ebc742007-07-27 15:43:22 +02001356 struct sta_info *sta;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001357 u32 sta_flags = 0;
Johannes Berge2ebc742007-07-27 15:43:22 +02001358
1359 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1360 if (unlikely(skb->len < ETH_HLEN)) {
1361 printk(KERN_DEBUG "%s: short skb (len=%d)\n",
1362 dev->name, skb->len);
1363 ret = 0;
1364 goto fail;
1365 }
1366
1367 nh_pos = skb_network_header(skb) - skb->data;
1368 h_pos = skb_transport_header(skb) - skb->data;
1369
1370 /* convert Ethernet header to proper 802.11 header (based on
1371 * operation mode) */
1372 ethertype = (skb->data[12] << 8) | skb->data[13];
Johannes Berge2ebc742007-07-27 15:43:22 +02001373 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
1374
Johannes Berg51fb61e2007-12-19 01:31:27 +01001375 switch (sdata->vif.type) {
Johannes Bergcf966832007-08-28 17:01:54 -04001376 case IEEE80211_IF_TYPE_AP:
1377 case IEEE80211_IF_TYPE_VLAN:
Johannes Berge2ebc742007-07-27 15:43:22 +02001378 fc |= IEEE80211_FCTL_FROMDS;
1379 /* DA BSSID SA */
1380 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1381 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1382 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1383 hdrlen = 24;
Johannes Bergcf966832007-08-28 17:01:54 -04001384 break;
1385 case IEEE80211_IF_TYPE_WDS:
Johannes Berge2ebc742007-07-27 15:43:22 +02001386 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1387 /* RA TA DA SA */
1388 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1389 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1390 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1391 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1392 hdrlen = 30;
Johannes Bergcf966832007-08-28 17:01:54 -04001393 break;
1394 case IEEE80211_IF_TYPE_STA:
Johannes Berge2ebc742007-07-27 15:43:22 +02001395 fc |= IEEE80211_FCTL_TODS;
1396 /* BSSID SA DA */
1397 memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
1398 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1399 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1400 hdrlen = 24;
Johannes Bergcf966832007-08-28 17:01:54 -04001401 break;
1402 case IEEE80211_IF_TYPE_IBSS:
Johannes Berge2ebc742007-07-27 15:43:22 +02001403 /* DA SA BSSID */
1404 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1405 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1406 memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
1407 hdrlen = 24;
Johannes Bergcf966832007-08-28 17:01:54 -04001408 break;
1409 default:
Johannes Berge2ebc742007-07-27 15:43:22 +02001410 ret = 0;
1411 goto fail;
1412 }
1413
Johannes Berg7d185b82008-01-28 17:11:43 +01001414 /*
1415 * There's no need to try to look up the destination
1416 * if it is a multicast address (which can only happen
1417 * in AP mode)
1418 */
1419 if (!is_multicast_ether_addr(hdr.addr1)) {
1420 sta = sta_info_get(local, hdr.addr1);
1421 if (sta) {
1422 sta_flags = sta->flags;
1423 sta_info_put(sta);
1424 }
Johannes Berge2ebc742007-07-27 15:43:22 +02001425 }
1426
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001427 /* receiver is QoS enabled, use a QoS type frame */
1428 if (sta_flags & WLAN_STA_WME) {
1429 fc |= IEEE80211_STYPE_QOS_DATA;
1430 hdrlen += 2;
1431 }
1432
1433 /*
Johannes Berg7d185b82008-01-28 17:11:43 +01001434 * If port access control is enabled, drop unicast frames to
1435 * unauthorised stations unless they are EAPOL frames from the
1436 * local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001437 */
1438 if (unlikely(sdata->ieee802_1x_pac &&
Johannes Berg7d185b82008-01-28 17:11:43 +01001439 !is_multicast_ether_addr(hdr.addr1) &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001440 !(sta_flags & WLAN_STA_AUTHORIZED) &&
1441 !(ethertype == ETH_P_PAE &&
1442 compare_ether_addr(dev->dev_addr,
1443 skb->data + ETH_ALEN) == 0))) {
1444#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1445 DECLARE_MAC_BUF(mac);
1446
1447 if (net_ratelimit())
1448 printk(KERN_DEBUG "%s: dropped frame to %s"
1449 " (unauthorized port)\n", dev->name,
1450 print_mac(mac, hdr.addr1));
1451#endif
1452
1453 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
1454
1455 ret = 0;
1456 goto fail;
1457 }
1458
Johannes Berge2ebc742007-07-27 15:43:22 +02001459 hdr.frame_control = cpu_to_le16(fc);
1460 hdr.duration_id = 0;
1461 hdr.seq_ctrl = 0;
1462
1463 skip_header_bytes = ETH_HLEN;
1464 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1465 encaps_data = bridge_tunnel_header;
1466 encaps_len = sizeof(bridge_tunnel_header);
1467 skip_header_bytes -= 2;
1468 } else if (ethertype >= 0x600) {
1469 encaps_data = rfc1042_header;
1470 encaps_len = sizeof(rfc1042_header);
1471 skip_header_bytes -= 2;
1472 } else {
1473 encaps_data = NULL;
1474 encaps_len = 0;
1475 }
1476
1477 skb_pull(skb, skip_header_bytes);
1478 nh_pos -= skip_header_bytes;
1479 h_pos -= skip_header_bytes;
1480
1481 /* TODO: implement support for fragments so that there is no need to
1482 * reallocate and copy payload; it might be enough to support one
1483 * extra fragment that would be copied in the beginning of the frame
1484 * data.. anyway, it would be nice to include this into skb structure
1485 * somehow
1486 *
1487 * There are few options for this:
1488 * use skb->cb as an extra space for 802.11 header
1489 * allocate new buffer if not enough headroom
1490 * make sure that there is enough headroom in every skb by increasing
1491 * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
1492 * alloc_skb() (net/core/skbuff.c)
1493 */
1494 head_need = hdrlen + encaps_len + local->tx_headroom;
1495 head_need -= skb_headroom(skb);
1496
1497 /* We are going to modify skb data, so make a copy of it if happens to
1498 * be cloned. This could happen, e.g., with Linux bridge code passing
1499 * us broadcast frames. */
1500
1501 if (head_need > 0 || skb_cloned(skb)) {
1502#if 0
1503 printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
1504 "of headroom\n", dev->name, head_need);
1505#endif
1506
1507 if (skb_cloned(skb))
1508 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1509 else
1510 I802_DEBUG_INC(local->tx_expand_skb_head);
1511 /* Since we have to reallocate the buffer, make sure that there
1512 * is enough room for possible WEP IV/ICV and TKIP (8 bytes
1513 * before payload and 12 after). */
1514 if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
1515 12, GFP_ATOMIC)) {
1516 printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
1517 "\n", dev->name);
1518 goto fail;
1519 }
1520 }
1521
1522 if (encaps_data) {
1523 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1524 nh_pos += encaps_len;
1525 h_pos += encaps_len;
1526 }
Johannes Bergc29b9b92007-09-14 11:10:24 -04001527
1528 if (fc & IEEE80211_STYPE_QOS_DATA) {
1529 __le16 *qos_control;
1530
1531 qos_control = (__le16*) skb_push(skb, 2);
1532 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
1533 /*
1534 * Maybe we could actually set some fields here, for now just
1535 * initialise to zero to indicate no special operation.
1536 */
1537 *qos_control = 0;
1538 } else
1539 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1540
Johannes Berge2ebc742007-07-27 15:43:22 +02001541 nh_pos += hdrlen;
1542 h_pos += hdrlen;
1543
1544 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1545 memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
1546 pkt_data->ifindex = dev->ifindex;
Johannes Berg678f5f712007-12-19 01:31:23 +01001547 if (ethertype == ETH_P_PAE)
1548 pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
Johannes Berge2ebc742007-07-27 15:43:22 +02001549
1550 skb->dev = local->mdev;
Stephen Hemminger68aae112007-08-24 11:29:34 -07001551 dev->stats.tx_packets++;
1552 dev->stats.tx_bytes += skb->len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001553
1554 /* Update skb pointers to various headers since this modified frame
1555 * is going to go through Linux networking code that may potentially
1556 * need things like pointer to IP header. */
1557 skb_set_mac_header(skb, 0);
1558 skb_set_network_header(skb, nh_pos);
1559 skb_set_transport_header(skb, h_pos);
1560
1561 dev->trans_start = jiffies;
1562 dev_queue_xmit(skb);
1563
1564 return 0;
1565
1566 fail:
1567 if (!ret)
1568 dev_kfree_skb(skb);
1569
1570 return ret;
1571}
1572
Johannes Berge2ebc742007-07-27 15:43:22 +02001573/* helper functions for pending packets for when queues are stopped */
1574
1575void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1576{
1577 int i, j;
1578 struct ieee80211_tx_stored_packet *store;
1579
1580 for (i = 0; i < local->hw.queues; i++) {
1581 if (!__ieee80211_queue_pending(local, i))
1582 continue;
1583 store = &local->pending_packet[i];
1584 kfree_skb(store->skb);
1585 for (j = 0; j < store->num_extra_frag; j++)
1586 kfree_skb(store->extra_frag[j]);
1587 kfree(store->extra_frag);
1588 clear_bit(IEEE80211_LINK_STATE_PENDING, &local->state[i]);
1589 }
1590}
1591
1592void ieee80211_tx_pending(unsigned long data)
1593{
1594 struct ieee80211_local *local = (struct ieee80211_local *)data;
1595 struct net_device *dev = local->mdev;
1596 struct ieee80211_tx_stored_packet *store;
1597 struct ieee80211_txrx_data tx;
1598 int i, ret, reschedule = 0;
1599
1600 netif_tx_lock_bh(dev);
1601 for (i = 0; i < local->hw.queues; i++) {
1602 if (__ieee80211_queue_stopped(local, i))
1603 continue;
1604 if (!__ieee80211_queue_pending(local, i)) {
1605 reschedule = 1;
1606 continue;
1607 }
1608 store = &local->pending_packet[i];
1609 tx.u.tx.control = &store->control;
1610 tx.u.tx.extra_frag = store->extra_frag;
1611 tx.u.tx.num_extra_frag = store->num_extra_frag;
1612 tx.u.tx.last_frag_hwrate = store->last_frag_hwrate;
1613 tx.u.tx.last_frag_rate = store->last_frag_rate;
Jiri Slabybadffb72007-08-28 17:01:54 -04001614 tx.flags = 0;
1615 if (store->last_frag_rate_ctrl_probe)
1616 tx.flags |= IEEE80211_TXRXD_TXPROBE_LAST_FRAG;
Johannes Berge2ebc742007-07-27 15:43:22 +02001617 ret = __ieee80211_tx(local, store->skb, &tx);
1618 if (ret) {
1619 if (ret == IEEE80211_TX_FRAG_AGAIN)
1620 store->skb = NULL;
1621 } else {
1622 clear_bit(IEEE80211_LINK_STATE_PENDING,
1623 &local->state[i]);
1624 reschedule = 1;
1625 }
1626 }
1627 netif_tx_unlock_bh(dev);
1628 if (reschedule) {
1629 if (!ieee80211_qdisc_installed(dev)) {
1630 if (!__ieee80211_queue_stopped(local, 0))
1631 netif_wake_queue(dev);
1632 } else
1633 netif_schedule(dev);
1634 }
1635}
1636
1637/* functions for drivers to get certain frames */
1638
1639static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
1640 struct ieee80211_if_ap *bss,
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001641 struct sk_buff *skb,
1642 struct beacon_data *beacon)
Johannes Berge2ebc742007-07-27 15:43:22 +02001643{
1644 u8 *pos, *tim;
1645 int aid0 = 0;
1646 int i, have_bits = 0, n1, n2;
1647
1648 /* Generate bitmap for TIM only if there are any STAs in power save
1649 * mode. */
Michael Wube8755e2007-07-27 15:43:23 +02001650 read_lock_bh(&local->sta_lock);
Johannes Berge2ebc742007-07-27 15:43:22 +02001651 if (atomic_read(&bss->num_sta_ps) > 0)
1652 /* in the hope that this is faster than
1653 * checking byte-for-byte */
1654 have_bits = !bitmap_empty((unsigned long*)bss->tim,
1655 IEEE80211_MAX_AID+1);
1656
1657 if (bss->dtim_count == 0)
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001658 bss->dtim_count = beacon->dtim_period - 1;
Johannes Berge2ebc742007-07-27 15:43:22 +02001659 else
1660 bss->dtim_count--;
1661
1662 tim = pos = (u8 *) skb_put(skb, 6);
1663 *pos++ = WLAN_EID_TIM;
1664 *pos++ = 4;
1665 *pos++ = bss->dtim_count;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001666 *pos++ = beacon->dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02001667
1668 if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
1669 aid0 = 1;
1670
1671 if (have_bits) {
1672 /* Find largest even number N1 so that bits numbered 1 through
1673 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
1674 * (N2 + 1) x 8 through 2007 are 0. */
1675 n1 = 0;
1676 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
1677 if (bss->tim[i]) {
1678 n1 = i & 0xfe;
1679 break;
1680 }
1681 }
1682 n2 = n1;
1683 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
1684 if (bss->tim[i]) {
1685 n2 = i;
1686 break;
1687 }
1688 }
1689
1690 /* Bitmap control */
1691 *pos++ = n1 | aid0;
1692 /* Part Virt Bitmap */
1693 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
1694
1695 tim[1] = n2 - n1 + 4;
1696 skb_put(skb, n2 - n1);
1697 } else {
1698 *pos++ = aid0; /* Bitmap control */
1699 *pos++ = 0; /* Part Virt Bitmap */
1700 }
Michael Wube8755e2007-07-27 15:43:23 +02001701 read_unlock_bh(&local->sta_lock);
Johannes Berge2ebc742007-07-27 15:43:22 +02001702}
1703
Johannes Berg32bfd352007-12-19 01:31:26 +01001704struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
1705 struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001706 struct ieee80211_tx_control *control)
1707{
1708 struct ieee80211_local *local = hw_to_local(hw);
1709 struct sk_buff *skb;
1710 struct net_device *bdev;
1711 struct ieee80211_sub_if_data *sdata = NULL;
1712 struct ieee80211_if_ap *ap = NULL;
Mattias Nissler1abbe492007-12-20 13:50:07 +01001713 struct rate_selection rsel;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001714 struct beacon_data *beacon;
1715
1716 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001717
Johannes Berg32bfd352007-12-19 01:31:26 +01001718 sdata = vif_to_sdata(vif);
1719 bdev = sdata->dev;
1720 ap = &sdata->u.ap;
Johannes Berge2ebc742007-07-27 15:43:22 +02001721
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001722 beacon = rcu_dereference(ap->beacon);
1723
1724 if (!ap || sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001725#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1726 if (net_ratelimit())
Johannes Berg32bfd352007-12-19 01:31:26 +01001727 printk(KERN_DEBUG "no beacon data avail for %s\n",
1728 bdev->name);
Johannes Berge2ebc742007-07-27 15:43:22 +02001729#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001730 skb = NULL;
1731 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02001732 }
1733
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001734 /* headroom, head length, tail length and maximum TIM length */
1735 skb = dev_alloc_skb(local->tx_headroom + beacon->head_len +
1736 beacon->tail_len + 256);
Johannes Berge2ebc742007-07-27 15:43:22 +02001737 if (!skb)
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001738 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02001739
1740 skb_reserve(skb, local->tx_headroom);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001741 memcpy(skb_put(skb, beacon->head_len), beacon->head,
1742 beacon->head_len);
Johannes Berge2ebc742007-07-27 15:43:22 +02001743
1744 ieee80211_include_sequence(sdata, (struct ieee80211_hdr *)skb->data);
1745
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001746 ieee80211_beacon_add_tim(local, ap, skb, beacon);
Johannes Berge2ebc742007-07-27 15:43:22 +02001747
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001748 if (beacon->tail)
1749 memcpy(skb_put(skb, beacon->tail_len), beacon->tail,
1750 beacon->tail_len);
Johannes Berge2ebc742007-07-27 15:43:22 +02001751
1752 if (control) {
Mattias Nissler1abbe492007-12-20 13:50:07 +01001753 rate_control_get_rate(local->mdev, local->oper_hw_mode, skb,
1754 &rsel);
1755 if (!rsel.rate) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001756 if (net_ratelimit()) {
Mattias Nissler1abbe492007-12-20 13:50:07 +01001757 printk(KERN_DEBUG "%s: ieee80211_beacon_get: "
1758 "no rate found\n",
1759 wiphy_name(local->hw.wiphy));
Johannes Berge2ebc742007-07-27 15:43:22 +02001760 }
1761 dev_kfree_skb(skb);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001762 skb = NULL;
1763 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02001764 }
1765
Ivo van Doorn0f7054e2008-01-13 14:16:47 +01001766 control->vif = vif;
Jiri Slaby13262ff2007-08-28 17:01:54 -04001767 control->tx_rate =
Johannes Berg471b3ef2007-12-28 14:32:58 +01001768 (sdata->bss_conf.use_short_preamble &&
Mattias Nissler1abbe492007-12-20 13:50:07 +01001769 (rsel.rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
1770 rsel.rate->val2 : rsel.rate->val;
Johannes Berge2ebc742007-07-27 15:43:22 +02001771 control->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
1772 control->power_level = local->hw.conf.power_level;
1773 control->flags |= IEEE80211_TXCTL_NO_ACK;
1774 control->retry_limit = 1;
1775 control->flags |= IEEE80211_TXCTL_CLEAR_DST_MASK;
1776 }
1777
1778 ap->num_beacons++;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001779
1780 out:
1781 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001782 return skb;
1783}
1784EXPORT_SYMBOL(ieee80211_beacon_get);
1785
Johannes Berg32bfd352007-12-19 01:31:26 +01001786void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001787 const void *frame, size_t frame_len,
1788 const struct ieee80211_tx_control *frame_txctl,
1789 struct ieee80211_rts *rts)
1790{
1791 const struct ieee80211_hdr *hdr = frame;
1792 u16 fctl;
1793
1794 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
1795 rts->frame_control = cpu_to_le16(fctl);
Johannes Berg32bfd352007-12-19 01:31:26 +01001796 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
1797 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02001798 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
1799 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
1800}
1801EXPORT_SYMBOL(ieee80211_rts_get);
1802
Johannes Berg32bfd352007-12-19 01:31:26 +01001803void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001804 const void *frame, size_t frame_len,
1805 const struct ieee80211_tx_control *frame_txctl,
1806 struct ieee80211_cts *cts)
1807{
1808 const struct ieee80211_hdr *hdr = frame;
1809 u16 fctl;
1810
1811 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
1812 cts->frame_control = cpu_to_le16(fctl);
Johannes Berg32bfd352007-12-19 01:31:26 +01001813 cts->duration = ieee80211_ctstoself_duration(hw, vif,
1814 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02001815 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
1816}
1817EXPORT_SYMBOL(ieee80211_ctstoself_get);
1818
1819struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01001820ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
1821 struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001822 struct ieee80211_tx_control *control)
1823{
1824 struct ieee80211_local *local = hw_to_local(hw);
1825 struct sk_buff *skb;
1826 struct sta_info *sta;
1827 ieee80211_tx_handler *handler;
1828 struct ieee80211_txrx_data tx;
1829 ieee80211_txrx_result res = TXRX_DROP;
1830 struct net_device *bdev;
1831 struct ieee80211_sub_if_data *sdata;
1832 struct ieee80211_if_ap *bss = NULL;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001833 struct beacon_data *beacon;
Johannes Berge2ebc742007-07-27 15:43:22 +02001834
Johannes Berg32bfd352007-12-19 01:31:26 +01001835 sdata = vif_to_sdata(vif);
1836 bdev = sdata->dev;
1837
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001838
1839 if (!bss)
Johannes Berge2ebc742007-07-27 15:43:22 +02001840 return NULL;
1841
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001842 rcu_read_lock();
1843 beacon = rcu_dereference(bss->beacon);
1844
1845 if (sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon ||
1846 !beacon->head) {
1847 rcu_read_unlock();
1848 return NULL;
1849 }
1850 rcu_read_unlock();
1851
Johannes Berge2ebc742007-07-27 15:43:22 +02001852 if (bss->dtim_count != 0)
1853 return NULL; /* send buffered bc/mc only after DTIM beacon */
1854 memset(control, 0, sizeof(*control));
1855 while (1) {
1856 skb = skb_dequeue(&bss->ps_bc_buf);
1857 if (!skb)
1858 return NULL;
1859 local->total_ps_buffered--;
1860
1861 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
1862 struct ieee80211_hdr *hdr =
1863 (struct ieee80211_hdr *) skb->data;
1864 /* more buffered multicast/broadcast frames ==> set
1865 * MoreData flag in IEEE 802.11 header to inform PS
1866 * STAs */
1867 hdr->frame_control |=
1868 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1869 }
1870
Johannes Berg58d41852007-09-26 17:53:18 +02001871 if (!ieee80211_tx_prepare(&tx, skb, local->mdev, control))
Johannes Berge2ebc742007-07-27 15:43:22 +02001872 break;
1873 dev_kfree_skb_any(skb);
1874 }
1875 sta = tx.sta;
Jiri Slabybadffb72007-08-28 17:01:54 -04001876 tx.flags |= IEEE80211_TXRXD_TXPS_BUFFERED;
Tomas Winkler475fa492007-09-02 22:58:21 +03001877 tx.u.tx.mode = local->hw.conf.mode;
Johannes Berge2ebc742007-07-27 15:43:22 +02001878
1879 for (handler = local->tx_handlers; *handler != NULL; handler++) {
1880 res = (*handler)(&tx);
1881 if (res == TXRX_DROP || res == TXRX_QUEUED)
1882 break;
1883 }
Johannes Berge2ebc742007-07-27 15:43:22 +02001884 skb = tx.skb; /* handlers are allowed to change skb */
1885
1886 if (res == TXRX_DROP) {
1887 I802_DEBUG_INC(local->tx_handlers_drop);
1888 dev_kfree_skb(skb);
1889 skb = NULL;
1890 } else if (res == TXRX_QUEUED) {
1891 I802_DEBUG_INC(local->tx_handlers_queued);
1892 skb = NULL;
1893 }
1894
1895 if (sta)
1896 sta_info_put(sta);
1897
1898 return skb;
1899}
1900EXPORT_SYMBOL(ieee80211_get_buffered_bc);