blob: 332ddcb1068a1b937fade056f9250690c6121639 [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
Johannes Berge2ebc742007-07-27 15:43:22 +0200216static int inline is_ieee80211_device(struct net_device *dev,
217 struct net_device *master)
218{
219 return (wdev_priv(dev->ieee80211_ptr) ==
220 wdev_priv(master->ieee80211_ptr));
221}
222
223/* tx handlers */
224
Johannes Berg9ae54c82008-01-31 19:48:20 +0100225static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100226ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200227{
228#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berge039fa42008-05-15 12:55:29 +0200229 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Berge2ebc742007-07-27 15:43:22 +0200230#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
Johannes Berge039fa42008-05-15 12:55:29 +0200231 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berge2ebc742007-07-27 15:43:22 +0200232 u32 sta_flags;
233
Johannes Berge039fa42008-05-15 12:55:29 +0200234 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100235 return TX_CONTINUE;
Johannes Berg58d41852007-09-26 17:53:18 +0200236
Zhu Yiece8edd2007-11-22 10:53:21 +0800237 if (unlikely(tx->local->sta_sw_scanning) &&
Johannes Berge2ebc742007-07-27 15:43:22 +0200238 ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
239 (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100240 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200241
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100242 if (tx->sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT)
243 return TX_CONTINUE;
244
Johannes Berg5cf121c2008-02-25 16:27:43 +0100245 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100246 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200247
Johannes Berg07346f812008-05-03 01:02:02 +0200248 sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
Johannes Berge2ebc742007-07-27 15:43:22 +0200249
Johannes Berg5cf121c2008-02-25 16:27:43 +0100250 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200251 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
Johannes Berg51fb61e2007-12-19 01:31:27 +0100252 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
Johannes Berge2ebc742007-07-27 15:43:22 +0200253 (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
254#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700255 DECLARE_MAC_BUF(mac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200256 printk(KERN_DEBUG "%s: dropped data frame to not "
Joe Perches0795af52007-10-03 17:59:30 -0700257 "associated station %s\n",
258 tx->dev->name, print_mac(mac, hdr->addr1));
Johannes Berge2ebc742007-07-27 15:43:22 +0200259#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
260 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100261 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200262 }
263 } else {
264 if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
265 tx->local->num_sta == 0 &&
Johannes Berg51fb61e2007-12-19 01:31:27 +0100266 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200267 /*
268 * No associated STAs - no need to send multicast
269 * frames.
270 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100271 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200272 }
Johannes Berg9ae54c82008-01-31 19:48:20 +0100273 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200274 }
275
Johannes Berg9ae54c82008-01-31 19:48:20 +0100276 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200277}
278
Johannes Berg9ae54c82008-01-31 19:48:20 +0100279static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100280ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200281{
282 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
283
284 if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
285 ieee80211_include_sequence(tx->sdata, hdr);
286
Johannes Berg9ae54c82008-01-31 19:48:20 +0100287 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200288}
289
290/* This function is called whenever the AP is about to exceed the maximum limit
291 * of buffered frames for power saving STAs. This situation should not really
292 * happen often during normal operation, so dropping the oldest buffered packet
293 * from each queue should be OK to make some room for new frames. */
294static void purge_old_ps_buffers(struct ieee80211_local *local)
295{
296 int total = 0, purged = 0;
297 struct sk_buff *skb;
298 struct ieee80211_sub_if_data *sdata;
299 struct sta_info *sta;
300
Johannes Berg79010422007-09-18 17:29:21 -0400301 /*
302 * virtual interfaces are protected by RCU
303 */
304 rcu_read_lock();
305
306 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200307 struct ieee80211_if_ap *ap;
308 if (sdata->dev == local->mdev ||
Johannes Berg51fb61e2007-12-19 01:31:27 +0100309 sdata->vif.type != IEEE80211_IF_TYPE_AP)
Johannes Berge2ebc742007-07-27 15:43:22 +0200310 continue;
311 ap = &sdata->u.ap;
312 skb = skb_dequeue(&ap->ps_bc_buf);
313 if (skb) {
314 purged++;
315 dev_kfree_skb(skb);
316 }
317 total += skb_queue_len(&ap->ps_bc_buf);
318 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200319
Johannes Bergd0709a62008-02-25 16:27:46 +0100320 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200321 skb = skb_dequeue(&sta->ps_tx_buf);
322 if (skb) {
323 purged++;
324 dev_kfree_skb(skb);
325 }
326 total += skb_queue_len(&sta->ps_tx_buf);
327 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100328
329 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +0200330
331 local->total_ps_buffered = total;
332 printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400333 wiphy_name(local->hw.wiphy), purged);
Johannes Berge2ebc742007-07-27 15:43:22 +0200334}
335
Johannes Berg9ae54c82008-01-31 19:48:20 +0100336static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100337ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200338{
Johannes Berge039fa42008-05-15 12:55:29 +0200339 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
340
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100341 /*
342 * broadcast/multicast frame
343 *
344 * If any of the associated stations is in power save mode,
345 * the frame is buffered to be sent after DTIM beacon frame.
346 * This is done either by the hardware or us.
347 */
348
349 /* not AP/IBSS or ordered frame */
350 if (!tx->sdata->bss || (tx->fc & IEEE80211_FCTL_ORDER))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100351 return TX_CONTINUE;
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100352
353 /* no stations in PS mode */
354 if (!atomic_read(&tx->sdata->bss->num_sta_ps))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100355 return TX_CONTINUE;
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100356
357 /* buffered in mac80211 */
358 if (tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200359 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
360 purge_old_ps_buffers(tx->local);
361 if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
362 AP_MAX_BC_BUFFER) {
363 if (net_ratelimit()) {
364 printk(KERN_DEBUG "%s: BC TX buffer full - "
365 "dropping the oldest frame\n",
366 tx->dev->name);
367 }
368 dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
369 } else
370 tx->local->total_ps_buffered++;
371 skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100372 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200373 }
374
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100375 /* buffered in hardware */
Johannes Berge039fa42008-05-15 12:55:29 +0200376 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100377
Johannes Berg9ae54c82008-01-31 19:48:20 +0100378 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200379}
380
Johannes Berg9ae54c82008-01-31 19:48:20 +0100381static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100382ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200383{
384 struct sta_info *sta = tx->sta;
Johannes Berge039fa42008-05-15 12:55:29 +0200385 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berg07346f812008-05-03 01:02:02 +0200386 u32 staflags;
Joe Perches0795af52007-10-03 17:59:30 -0700387 DECLARE_MAC_BUF(mac);
Johannes Berge2ebc742007-07-27 15:43:22 +0200388
389 if (unlikely(!sta ||
390 ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
391 (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100392 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200393
Johannes Berg07346f812008-05-03 01:02:02 +0200394 staflags = get_sta_flags(sta);
395
396 if (unlikely((staflags & WLAN_STA_PS) &&
397 !(staflags & WLAN_STA_PSPOLL))) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200398#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Joe Perches0795af52007-10-03 17:59:30 -0700399 printk(KERN_DEBUG "STA %s aid %d: PS buffer (entries "
Johannes Berge2ebc742007-07-27 15:43:22 +0200400 "before %d)\n",
Joe Perches0795af52007-10-03 17:59:30 -0700401 print_mac(mac, sta->addr), sta->aid,
Johannes Berge2ebc742007-07-27 15:43:22 +0200402 skb_queue_len(&sta->ps_tx_buf));
403#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berge2ebc742007-07-27 15:43:22 +0200404 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
405 purge_old_ps_buffers(tx->local);
406 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
407 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
408 if (net_ratelimit()) {
Joe Perches0795af52007-10-03 17:59:30 -0700409 printk(KERN_DEBUG "%s: STA %s TX "
Johannes Berge2ebc742007-07-27 15:43:22 +0200410 "buffer full - dropping oldest frame\n",
Joe Perches0795af52007-10-03 17:59:30 -0700411 tx->dev->name, print_mac(mac, sta->addr));
Johannes Berge2ebc742007-07-27 15:43:22 +0200412 }
413 dev_kfree_skb(old);
414 } else
415 tx->local->total_ps_buffered++;
Johannes Berg004c8722008-02-20 11:21:35 +0100416
Johannes Berge2ebc742007-07-27 15:43:22 +0200417 /* Queue frame to be sent after STA sends an PS Poll frame */
Johannes Berg004c8722008-02-20 11:21:35 +0100418 if (skb_queue_empty(&sta->ps_tx_buf))
419 sta_info_set_tim_bit(sta);
420
Johannes Berge039fa42008-05-15 12:55:29 +0200421 info->control.jiffies = jiffies;
Johannes Berge2ebc742007-07-27 15:43:22 +0200422 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100423 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200424 }
425#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berg07346f812008-05-03 01:02:02 +0200426 else if (unlikely(test_sta_flags(sta, WLAN_STA_PS))) {
Joe Perches0795af52007-10-03 17:59:30 -0700427 printk(KERN_DEBUG "%s: STA %s in PS mode, but pspoll "
Johannes Berge2ebc742007-07-27 15:43:22 +0200428 "set -> send frame\n", tx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700429 print_mac(mac, sta->addr));
Johannes Berge2ebc742007-07-27 15:43:22 +0200430 }
431#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berg07346f812008-05-03 01:02:02 +0200432 clear_sta_flags(sta, WLAN_STA_PSPOLL);
Johannes Berge2ebc742007-07-27 15:43:22 +0200433
Johannes Berg9ae54c82008-01-31 19:48:20 +0100434 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200435}
436
Johannes Berg9ae54c82008-01-31 19:48:20 +0100437static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100438ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200439{
Johannes Berg5cf121c2008-02-25 16:27:43 +0100440 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100441 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200442
Johannes Berg5cf121c2008-02-25 16:27:43 +0100443 if (tx->flags & IEEE80211_TX_UNICAST)
Johannes Berge2ebc742007-07-27 15:43:22 +0200444 return ieee80211_tx_h_unicast_ps_buf(tx);
445 else
446 return ieee80211_tx_h_multicast_ps_buf(tx);
447}
448
Johannes Berg9ae54c82008-01-31 19:48:20 +0100449static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100450ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200451{
Johannes Bergd4e46a32007-09-14 11:10:24 -0400452 struct ieee80211_key *key;
Johannes Berge039fa42008-05-15 12:55:29 +0200453 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berg176e4f82007-12-18 15:27:47 +0100454 u16 fc = tx->fc;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400455
Johannes Berge039fa42008-05-15 12:55:29 +0200456 if (unlikely(info->flags & IEEE80211_TX_CTL_DO_NOT_ENCRYPT))
Johannes Berge2ebc742007-07-27 15:43:22 +0200457 tx->key = NULL;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400458 else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
459 tx->key = key;
460 else if ((key = rcu_dereference(tx->sdata->default_key)))
461 tx->key = key;
Johannes Berge2ebc742007-07-27 15:43:22 +0200462 else if (tx->sdata->drop_unencrypted &&
Johannes Berge039fa42008-05-15 12:55:29 +0200463 !(info->flags & IEEE80211_TX_CTL_EAPOL_FRAME) &&
464 !(info->flags & IEEE80211_TX_CTL_INJECTED)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200465 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100466 return TX_DROP;
Johannes Berg176e4f82007-12-18 15:27:47 +0100467 } else
Johannes Berge2ebc742007-07-27 15:43:22 +0200468 tx->key = NULL;
469
470 if (tx->key) {
Johannes Berg176e4f82007-12-18 15:27:47 +0100471 u16 ftype, stype;
472
Johannes Berge2ebc742007-07-27 15:43:22 +0200473 tx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400474 /* TODO: add threshold stuff again */
Johannes Berg176e4f82007-12-18 15:27:47 +0100475
476 switch (tx->key->conf.alg) {
477 case ALG_WEP:
478 ftype = fc & IEEE80211_FCTL_FTYPE;
479 stype = fc & IEEE80211_FCTL_STYPE;
480
481 if (ftype == IEEE80211_FTYPE_MGMT &&
482 stype == IEEE80211_STYPE_AUTH)
483 break;
484 case ALG_TKIP:
485 case ALG_CCMP:
486 if (!WLAN_FC_DATA_PRESENT(fc))
487 tx->key = NULL;
488 break;
489 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200490 }
491
Johannes Berg176e4f82007-12-18 15:27:47 +0100492 if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Johannes Berge039fa42008-05-15 12:55:29 +0200493 info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
Johannes Berg176e4f82007-12-18 15:27:47 +0100494
Johannes Berg9ae54c82008-01-31 19:48:20 +0100495 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200496}
497
Johannes Berg9ae54c82008-01-31 19:48:20 +0100498static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100499ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200500{
Mattias Nissler1abbe492007-12-20 13:50:07 +0100501 struct rate_selection rsel;
Johannes Berg8318d782008-01-24 19:38:38 +0100502 struct ieee80211_supported_band *sband;
Johannes Berge039fa42008-05-15 12:55:29 +0200503 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berg8318d782008-01-24 19:38:38 +0100504
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200505 sband = tx->local->hw.wiphy->bands[tx->channel->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200506
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200507 if (likely(tx->rate_idx < 0)) {
Johannes Berg8318d782008-01-24 19:38:38 +0100508 rate_control_get_rate(tx->dev, sband, tx->skb, &rsel);
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200509 tx->rate_idx = rsel.rate_idx;
510 if (unlikely(rsel.probe_idx >= 0)) {
Johannes Berge039fa42008-05-15 12:55:29 +0200511 info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100512 tx->flags |= IEEE80211_TX_PROBE_LAST_FRAG;
Johannes Berge039fa42008-05-15 12:55:29 +0200513 info->control.alt_retry_rate_idx = tx->rate_idx;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200514 tx->rate_idx = rsel.probe_idx;
Johannes Berg58d41852007-09-26 17:53:18 +0200515 } else
Johannes Berge039fa42008-05-15 12:55:29 +0200516 info->control.alt_retry_rate_idx = -1;
Johannes Berg58d41852007-09-26 17:53:18 +0200517
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200518 if (unlikely(tx->rate_idx < 0))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100519 return TX_DROP;
Johannes Berg58d41852007-09-26 17:53:18 +0200520 } else
Johannes Berge039fa42008-05-15 12:55:29 +0200521 info->control.alt_retry_rate_idx = -1;
Johannes Berg58d41852007-09-26 17:53:18 +0200522
Johannes Berg8318d782008-01-24 19:38:38 +0100523 if (tx->sdata->bss_conf.use_cts_prot &&
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200524 (tx->flags & IEEE80211_TX_FRAGMENTED) && (rsel.nonerp_idx >= 0)) {
525 tx->last_frag_rate_idx = tx->rate_idx;
526 if (rsel.probe_idx >= 0)
Johannes Berg5cf121c2008-02-25 16:27:43 +0100527 tx->flags &= ~IEEE80211_TX_PROBE_LAST_FRAG;
Jiri Slabybadffb72007-08-28 17:01:54 -0400528 else
Johannes Berg5cf121c2008-02-25 16:27:43 +0100529 tx->flags |= IEEE80211_TX_PROBE_LAST_FRAG;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200530 tx->rate_idx = rsel.nonerp_idx;
Johannes Berge039fa42008-05-15 12:55:29 +0200531 info->tx_rate_idx = rsel.nonerp_idx;
532 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200533 } else {
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200534 tx->last_frag_rate_idx = tx->rate_idx;
Johannes Berge039fa42008-05-15 12:55:29 +0200535 info->tx_rate_idx = tx->rate_idx;
Johannes Berge2ebc742007-07-27 15:43:22 +0200536 }
Johannes Berge039fa42008-05-15 12:55:29 +0200537 info->tx_rate_idx = tx->rate_idx;
Johannes Berge2ebc742007-07-27 15:43:22 +0200538
Johannes Berg9ae54c82008-01-31 19:48:20 +0100539 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200540}
541
Johannes Berg9ae54c82008-01-31 19:48:20 +0100542static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100543ieee80211_tx_h_misc(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200544{
545 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200546 u16 fc = le16_to_cpu(hdr->frame_control);
Johannes Berge2ebc742007-07-27 15:43:22 +0200547 u16 dur;
Johannes Berge039fa42008-05-15 12:55:29 +0200548 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200549 struct ieee80211_supported_band *sband;
550
551 sband = tx->local->hw.wiphy->bands[tx->channel->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200552
Johannes Berge039fa42008-05-15 12:55:29 +0200553 if (tx->sta)
554 info->control.aid = tx->sta->aid;
555
556 if (!info->control.retry_limit) {
Johannes Berg58d41852007-09-26 17:53:18 +0200557 if (!is_multicast_ether_addr(hdr->addr1)) {
Johannes Berge039fa42008-05-15 12:55:29 +0200558 int len = min_t(int, tx->skb->len + FCS_LEN,
559 tx->local->fragmentation_threshold);
560 if (len > tx->local->rts_threshold
Johannes Berg58d41852007-09-26 17:53:18 +0200561 && tx->local->rts_threshold <
Johannes Berge039fa42008-05-15 12:55:29 +0200562 IEEE80211_MAX_RTS_THRESHOLD) {
563 info->flags |= IEEE80211_TX_CTL_USE_RTS_CTS;
564 info->flags |=
565 IEEE80211_TX_CTL_LONG_RETRY_LIMIT;
566 info->control.retry_limit =
Johannes Berg58d41852007-09-26 17:53:18 +0200567 tx->local->long_retry_limit;
568 } else {
Johannes Berge039fa42008-05-15 12:55:29 +0200569 info->control.retry_limit =
Johannes Berg58d41852007-09-26 17:53:18 +0200570 tx->local->short_retry_limit;
571 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200572 } else {
Johannes Berge039fa42008-05-15 12:55:29 +0200573 info->control.retry_limit = 1;
Johannes Berge2ebc742007-07-27 15:43:22 +0200574 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200575 }
576
Johannes Berg5cf121c2008-02-25 16:27:43 +0100577 if (tx->flags & IEEE80211_TX_FRAGMENTED) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200578 /* Do not use multiple retry rates when sending fragmented
579 * frames.
580 * TODO: The last fragment could still use multiple retry
581 * rates. */
Johannes Berge039fa42008-05-15 12:55:29 +0200582 info->control.alt_retry_rate_idx = -1;
Johannes Berge2ebc742007-07-27 15:43:22 +0200583 }
584
585 /* Use CTS protection for unicast frames sent using extended rates if
586 * there are associated non-ERP stations and RTS/CTS is not configured
587 * for the frame. */
Johannes Berg8318d782008-01-24 19:38:38 +0100588 if ((tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE) &&
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200589 (sband->bitrates[tx->rate_idx].flags & IEEE80211_RATE_ERP_G) &&
Johannes Berg5cf121c2008-02-25 16:27:43 +0100590 (tx->flags & IEEE80211_TX_UNICAST) &&
Johannes Berg471b3ef2007-12-28 14:32:58 +0100591 tx->sdata->bss_conf.use_cts_prot &&
Johannes Berge039fa42008-05-15 12:55:29 +0200592 !(info->flags & IEEE80211_TX_CTL_USE_RTS_CTS))
593 info->flags |= IEEE80211_TX_CTL_USE_CTS_PROTECT;
Johannes Berge2ebc742007-07-27 15:43:22 +0200594
Daniel Drake7e9ed182007-07-27 15:43:24 +0200595 /* Transmit data frames using short preambles if the driver supports
596 * short preambles at the selected rate and short preambles are
597 * available on the network at the current point in time. */
598 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200599 (sband->bitrates[tx->rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
Johannes Berg471b3ef2007-12-28 14:32:58 +0100600 tx->sdata->bss_conf.use_short_preamble &&
Johannes Berg07346f812008-05-03 01:02:02 +0200601 (!tx->sta || test_sta_flags(tx->sta, WLAN_STA_SHORT_PREAMBLE))) {
Johannes Berge039fa42008-05-15 12:55:29 +0200602 info->flags |= IEEE80211_TX_CTL_SHORT_PREAMBLE;
Daniel Drake7e9ed182007-07-27 15:43:24 +0200603 }
604
Johannes Berge2ebc742007-07-27 15:43:22 +0200605 /* Setup duration field for the first fragment of the frame. Duration
606 * for remaining fragments will be updated when they are being sent
607 * to low-level driver in ieee80211_tx(). */
608 dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
Johannes Berg5cf121c2008-02-25 16:27:43 +0100609 (tx->flags & IEEE80211_TX_FRAGMENTED) ?
610 tx->extra_frag[0]->len : 0);
Johannes Berge2ebc742007-07-27 15:43:22 +0200611 hdr->duration_id = cpu_to_le16(dur);
612
Johannes Berge039fa42008-05-15 12:55:29 +0200613 if ((info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) ||
614 (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)) {
Johannes Berg8318d782008-01-24 19:38:38 +0100615 struct ieee80211_supported_band *sband;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200616 struct ieee80211_rate *rate;
617 s8 baserate = -1;
Johannes Berg8318d782008-01-24 19:38:38 +0100618 int idx;
619
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200620 sband = tx->local->hw.wiphy->bands[tx->channel->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200621
622 /* Do not use multiple retry rates when using RTS/CTS */
Johannes Berge039fa42008-05-15 12:55:29 +0200623 info->control.alt_retry_rate_idx = -1;
Johannes Berge2ebc742007-07-27 15:43:22 +0200624
625 /* Use min(data rate, max base rate) as CTS/RTS rate */
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200626 rate = &sband->bitrates[tx->rate_idx];
Johannes Berge2ebc742007-07-27 15:43:22 +0200627
Johannes Berg8318d782008-01-24 19:38:38 +0100628 for (idx = 0; idx < sband->n_bitrates; idx++) {
629 if (sband->bitrates[idx].bitrate > rate->bitrate)
630 continue;
631 if (tx->sdata->basic_rates & BIT(idx) &&
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200632 (baserate < 0 ||
633 (sband->bitrates[baserate].bitrate
634 < sband->bitrates[idx].bitrate)))
635 baserate = idx;
Johannes Berg8318d782008-01-24 19:38:38 +0100636 }
637
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200638 if (baserate >= 0)
Johannes Berge039fa42008-05-15 12:55:29 +0200639 info->control.rts_cts_rate_idx = baserate;
Johannes Berg8318d782008-01-24 19:38:38 +0100640 else
Johannes Berge039fa42008-05-15 12:55:29 +0200641 info->control.rts_cts_rate_idx = 0;
Johannes Berge2ebc742007-07-27 15:43:22 +0200642 }
643
Johannes Berge2454942008-05-15 12:55:28 +0200644 if (tx->sta)
Johannes Berge039fa42008-05-15 12:55:29 +0200645 info->control.aid = tx->sta->aid;
Johannes Berge2ebc742007-07-27 15:43:22 +0200646
Johannes Berg9ae54c82008-01-31 19:48:20 +0100647 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200648}
649
Johannes Berg9ae54c82008-01-31 19:48:20 +0100650static ieee80211_tx_result
Johannes Berge2454942008-05-15 12:55:28 +0200651ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
652{
653 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
654 size_t hdrlen, per_fragm, num_fragm, payload_len, left;
655 struct sk_buff **frags, *first, *frag;
656 int i;
657 u16 seq;
658 u8 *pos;
659 int frag_threshold = tx->local->fragmentation_threshold;
660
661 if (!(tx->flags & IEEE80211_TX_FRAGMENTED))
662 return TX_CONTINUE;
663
Johannes Bergeefce912008-05-17 00:57:13 +0200664 /*
665 * Warn when submitting a fragmented A-MPDU frame and drop it.
666 * This is an error and needs to be fixed elsewhere, but when
667 * done needs to take care of monitor interfaces (injection)
668 * etc.
669 */
670 if (WARN_ON(tx->flags & IEEE80211_TX_CTL_AMPDU ||
Johannes Berge2530082008-05-17 00:57:14 +0200671 skb_get_queue_mapping(tx->skb) >=
672 ieee80211_num_regular_queues(&tx->local->hw)))
Johannes Bergeefce912008-05-17 00:57:13 +0200673 return TX_DROP;
674
Johannes Berge2454942008-05-15 12:55:28 +0200675 first = tx->skb;
676
677 hdrlen = ieee80211_get_hdrlen(tx->fc);
678 payload_len = first->len - hdrlen;
679 per_fragm = frag_threshold - hdrlen - FCS_LEN;
680 num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
681
682 frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
683 if (!frags)
684 goto fail;
685
686 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
687 seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
688 pos = first->data + hdrlen + per_fragm;
689 left = payload_len - per_fragm;
690 for (i = 0; i < num_fragm - 1; i++) {
691 struct ieee80211_hdr *fhdr;
692 size_t copylen;
693
694 if (left <= 0)
695 goto fail;
696
697 /* reserve enough extra head and tail room for possible
698 * encryption */
699 frag = frags[i] =
700 dev_alloc_skb(tx->local->tx_headroom +
701 frag_threshold +
702 IEEE80211_ENCRYPT_HEADROOM +
703 IEEE80211_ENCRYPT_TAILROOM);
704 if (!frag)
705 goto fail;
706 /* Make sure that all fragments use the same priority so
707 * that they end up using the same TX queue */
708 frag->priority = first->priority;
709 skb_reserve(frag, tx->local->tx_headroom +
710 IEEE80211_ENCRYPT_HEADROOM);
711 fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
712 memcpy(fhdr, first->data, hdrlen);
713 if (i == num_fragm - 2)
714 fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
715 fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
716 copylen = left > per_fragm ? per_fragm : left;
717 memcpy(skb_put(frag, copylen), pos, copylen);
718
719 pos += copylen;
720 left -= copylen;
721 }
722 skb_trim(first, hdrlen + per_fragm);
723
724 tx->num_extra_frag = num_fragm - 1;
725 tx->extra_frag = frags;
726
727 return TX_CONTINUE;
728
729 fail:
730 printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
731 if (frags) {
732 for (i = 0; i < num_fragm - 1; i++)
733 if (frags[i])
734 dev_kfree_skb(frags[i]);
735 kfree(frags);
736 }
737 I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
738 return TX_DROP;
739}
740
741static ieee80211_tx_result
742ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
743{
744 if (!tx->key)
745 return TX_CONTINUE;
746
747 switch (tx->key->conf.alg) {
748 case ALG_WEP:
749 return ieee80211_crypto_wep_encrypt(tx);
750 case ALG_TKIP:
751 return ieee80211_crypto_tkip_encrypt(tx);
752 case ALG_CCMP:
753 return ieee80211_crypto_ccmp_encrypt(tx);
754 }
755
756 /* not reached */
757 WARN_ON(1);
758 return TX_DROP;
759}
760
761static ieee80211_tx_result
762ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200763{
Johannes Berg9e72ebd2008-05-21 17:33:42 +0200764 int i;
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200765
Johannes Berg9e72ebd2008-05-21 17:33:42 +0200766 if (!tx->sta)
767 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200768
Johannes Berg9e72ebd2008-05-21 17:33:42 +0200769 tx->sta->tx_packets++;
770 tx->sta->tx_fragments++;
771 tx->sta->tx_bytes += tx->skb->len;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100772 if (tx->extra_frag) {
Johannes Berg9e72ebd2008-05-21 17:33:42 +0200773 tx->sta->tx_fragments += tx->num_extra_frag;
774 for (i = 0; i < tx->num_extra_frag; i++)
775 tx->sta->tx_bytes += tx->extra_frag[i]->len;
Johannes Berge2454942008-05-15 12:55:28 +0200776 }
777
Johannes Berg9ae54c82008-01-31 19:48:20 +0100778 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200779}
780
Johannes Berge2ebc742007-07-27 15:43:22 +0200781
Johannes Berg5cf121c2008-02-25 16:27:43 +0100782typedef ieee80211_tx_result (*ieee80211_tx_handler)(struct ieee80211_tx_data *);
Johannes Berg58905292008-01-31 19:48:25 +0100783static ieee80211_tx_handler ieee80211_tx_handlers[] =
Johannes Berge2ebc742007-07-27 15:43:22 +0200784{
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,
Johannes Berge2ebc742007-07-27 15:43:22 +0200790 ieee80211_tx_h_rate_ctrl,
791 ieee80211_tx_h_misc,
Johannes Berge2454942008-05-15 12:55:28 +0200792 ieee80211_tx_h_fragment,
Johannes Berge039fa42008-05-15 12:55:29 +0200793 /* handlers after fragment must be aware of tx info fragmentation! */
Johannes Berge2454942008-05-15 12:55:28 +0200794 ieee80211_tx_h_encrypt,
795 ieee80211_tx_h_stats,
Johannes Berge2ebc742007-07-27 15:43:22 +0200796 NULL
797};
798
799/* actual transmit path */
800
801/*
802 * deal with packet injection down monitor interface
803 * with Radiotap Header -- only called for monitor mode interface
804 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100805static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100806__ieee80211_parse_tx_radiotap(struct ieee80211_tx_data *tx,
Johannes Berg58d41852007-09-26 17:53:18 +0200807 struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +0200808{
809 /*
810 * this is the moment to interpret and discard the radiotap header that
811 * must be at the start of the packet injected in Monitor mode
812 *
813 * Need to take some care with endian-ness since radiotap
814 * args are little-endian
815 */
816
817 struct ieee80211_radiotap_iterator iterator;
818 struct ieee80211_radiotap_header *rthdr =
819 (struct ieee80211_radiotap_header *) skb->data;
Johannes Berg8318d782008-01-24 19:38:38 +0100820 struct ieee80211_supported_band *sband;
Johannes Berge2ebc742007-07-27 15:43:22 +0200821 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
Johannes Berge039fa42008-05-15 12:55:29 +0200822 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +0200823
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200824 sband = tx->local->hw.wiphy->bands[tx->channel->band];
Johannes Berg8318d782008-01-24 19:38:38 +0100825
Johannes Berge039fa42008-05-15 12:55:29 +0200826 info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
827 info->flags |= IEEE80211_TX_CTL_INJECTED;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100828 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200829
830 /*
831 * for every radiotap entry that is present
832 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
833 * entries present, or -EINVAL on error)
834 */
835
836 while (!ret) {
837 int i, target_rate;
838
839 ret = ieee80211_radiotap_iterator_next(&iterator);
840
841 if (ret)
842 continue;
843
844 /* see if this argument is something we can use */
845 switch (iterator.this_arg_index) {
846 /*
847 * You must take care when dereferencing iterator.this_arg
848 * for multibyte types... the pointer is not aligned. Use
849 * get_unaligned((type *)iterator.this_arg) to dereference
850 * iterator.this_arg for type "type" safely on all arches.
851 */
852 case IEEE80211_RADIOTAP_RATE:
853 /*
854 * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
855 * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
856 */
857 target_rate = (*iterator.this_arg) * 5;
Johannes Berg8318d782008-01-24 19:38:38 +0100858 for (i = 0; i < sband->n_bitrates; i++) {
859 struct ieee80211_rate *r;
Johannes Berge2ebc742007-07-27 15:43:22 +0200860
Johannes Berg8318d782008-01-24 19:38:38 +0100861 r = &sband->bitrates[i];
862
863 if (r->bitrate == target_rate) {
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200864 tx->rate_idx = i;
Johannes Berg58d41852007-09-26 17:53:18 +0200865 break;
866 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200867 }
868 break;
869
870 case IEEE80211_RADIOTAP_ANTENNA:
871 /*
872 * radiotap uses 0 for 1st ant, mac80211 is 1 for
873 * 1st ant
874 */
Johannes Berge039fa42008-05-15 12:55:29 +0200875 info->antenna_sel_tx = (*iterator.this_arg) + 1;
Johannes Berge2ebc742007-07-27 15:43:22 +0200876 break;
877
Johannes Berg8318d782008-01-24 19:38:38 +0100878#if 0
Johannes Berge2ebc742007-07-27 15:43:22 +0200879 case IEEE80211_RADIOTAP_DBM_TX_POWER:
880 control->power_level = *iterator.this_arg;
881 break;
Johannes Berg8318d782008-01-24 19:38:38 +0100882#endif
Johannes Berge2ebc742007-07-27 15:43:22 +0200883
884 case IEEE80211_RADIOTAP_FLAGS:
885 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
886 /*
887 * this indicates that the skb we have been
888 * handed has the 32-bit FCS CRC at the end...
889 * we should react to that by snipping it off
890 * because it will be recomputed and added
891 * on transmission
892 */
893 if (skb->len < (iterator.max_length + FCS_LEN))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100894 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200895
896 skb_trim(skb, skb->len - FCS_LEN);
897 }
Johannes Berg58d41852007-09-26 17:53:18 +0200898 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
Johannes Berge039fa42008-05-15 12:55:29 +0200899 info->flags &=
900 ~IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
Johannes Berg58d41852007-09-26 17:53:18 +0200901 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
Johannes Berg5cf121c2008-02-25 16:27:43 +0100902 tx->flags |= IEEE80211_TX_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200903 break;
904
Johannes Berg58d41852007-09-26 17:53:18 +0200905 /*
906 * Please update the file
907 * Documentation/networking/mac80211-injection.txt
908 * when parsing new fields here.
909 */
910
Johannes Berge2ebc742007-07-27 15:43:22 +0200911 default:
912 break;
913 }
914 }
915
916 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100917 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200918
919 /*
920 * remove the radiotap header
921 * iterator->max_length was sanity-checked against
922 * skb->len by iterator init
923 */
924 skb_pull(skb, iterator.max_length);
925
Johannes Berg9ae54c82008-01-31 19:48:20 +0100926 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200927}
928
Johannes Berg58d41852007-09-26 17:53:18 +0200929/*
930 * initialises @tx
931 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100932static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100933__ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
Johannes Berge2ebc742007-07-27 15:43:22 +0200934 struct sk_buff *skb,
Johannes Berge039fa42008-05-15 12:55:29 +0200935 struct net_device *dev)
Johannes Berge2ebc742007-07-27 15:43:22 +0200936{
937 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg58d41852007-09-26 17:53:18 +0200938 struct ieee80211_hdr *hdr;
Johannes Berge2ebc742007-07-27 15:43:22 +0200939 struct ieee80211_sub_if_data *sdata;
Johannes Berge039fa42008-05-15 12:55:29 +0200940 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +0200941
942 int hdrlen;
943
944 memset(tx, 0, sizeof(*tx));
945 tx->skb = skb;
946 tx->dev = dev; /* use original interface */
947 tx->local = local;
948 tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berge039fa42008-05-15 12:55:29 +0200949 tx->channel = local->hw.conf.channel;
Johannes Berg5854a322008-06-02 09:38:04 +0200950 tx->rate_idx = -1;
951 tx->last_frag_rate_idx = -1;
Johannes Berge2ebc742007-07-27 15:43:22 +0200952 /*
Johannes Berg58d41852007-09-26 17:53:18 +0200953 * Set this flag (used below to indicate "automatic fragmentation"),
954 * it will be cleared/left by radiotap as desired.
Johannes Berge2ebc742007-07-27 15:43:22 +0200955 */
Johannes Berg5cf121c2008-02-25 16:27:43 +0100956 tx->flags |= IEEE80211_TX_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200957
958 /* process and remove the injection radiotap header */
959 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +0100960 if (unlikely(sdata->vif.type == IEEE80211_IF_TYPE_MNTR)) {
Johannes Berg9ae54c82008-01-31 19:48:20 +0100961 if (__ieee80211_parse_tx_radiotap(tx, skb) == TX_DROP)
962 return TX_DROP;
Johannes Berg58d41852007-09-26 17:53:18 +0200963
Johannes Berge2ebc742007-07-27 15:43:22 +0200964 /*
Johannes Berg58d41852007-09-26 17:53:18 +0200965 * __ieee80211_parse_tx_radiotap has now removed
966 * the radiotap header that was present and pre-filled
967 * 'tx' with tx control information.
Johannes Berge2ebc742007-07-27 15:43:22 +0200968 */
Johannes Berge2ebc742007-07-27 15:43:22 +0200969 }
970
Johannes Berg58d41852007-09-26 17:53:18 +0200971 hdr = (struct ieee80211_hdr *) skb->data;
972
warmcat24338792007-09-14 11:10:25 -0400973 tx->sta = sta_info_get(local, hdr->addr1);
974 tx->fc = le16_to_cpu(hdr->frame_control);
Johannes Berg58d41852007-09-26 17:53:18 +0200975
Jiri Slabybadffb72007-08-28 17:01:54 -0400976 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +0100977 tx->flags &= ~IEEE80211_TX_UNICAST;
Johannes Berge039fa42008-05-15 12:55:29 +0200978 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Jiri Slabybadffb72007-08-28 17:01:54 -0400979 } else {
Johannes Berg5cf121c2008-02-25 16:27:43 +0100980 tx->flags |= IEEE80211_TX_UNICAST;
Johannes Berge039fa42008-05-15 12:55:29 +0200981 info->flags &= ~IEEE80211_TX_CTL_NO_ACK;
Jiri Slabybadffb72007-08-28 17:01:54 -0400982 }
Johannes Berg58d41852007-09-26 17:53:18 +0200983
Johannes Berg5cf121c2008-02-25 16:27:43 +0100984 if (tx->flags & IEEE80211_TX_FRAGMENTED) {
985 if ((tx->flags & IEEE80211_TX_UNICAST) &&
Johannes Berg58d41852007-09-26 17:53:18 +0200986 skb->len + FCS_LEN > local->fragmentation_threshold &&
987 !local->ops->set_frag_threshold)
Johannes Berg5cf121c2008-02-25 16:27:43 +0100988 tx->flags |= IEEE80211_TX_FRAGMENTED;
Johannes Berg58d41852007-09-26 17:53:18 +0200989 else
Johannes Berg5cf121c2008-02-25 16:27:43 +0100990 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
Johannes Berg58d41852007-09-26 17:53:18 +0200991 }
992
Johannes Berge2ebc742007-07-27 15:43:22 +0200993 if (!tx->sta)
Johannes Berge039fa42008-05-15 12:55:29 +0200994 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Johannes Berg07346f812008-05-03 01:02:02 +0200995 else if (test_and_clear_sta_flags(tx->sta, WLAN_STA_CLEAR_PS_FILT))
Johannes Berge039fa42008-05-15 12:55:29 +0200996 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Johannes Berg58d41852007-09-26 17:53:18 +0200997
Johannes Berge2ebc742007-07-27 15:43:22 +0200998 hdrlen = ieee80211_get_hdrlen(tx->fc);
999 if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
1000 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
1001 tx->ethertype = (pos[0] << 8) | pos[1];
1002 }
Johannes Berge039fa42008-05-15 12:55:29 +02001003 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001004
Johannes Berg9ae54c82008-01-31 19:48:20 +01001005 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001006}
1007
Johannes Berg32bfd352007-12-19 01:31:26 +01001008/*
Johannes Berg58d41852007-09-26 17:53:18 +02001009 * NB: @tx is uninitialised when passed in here
1010 */
Johannes Berg5cf121c2008-02-25 16:27:43 +01001011static int ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
Johannes Berg58d41852007-09-26 17:53:18 +02001012 struct sk_buff *skb,
Johannes Berge039fa42008-05-15 12:55:29 +02001013 struct net_device *mdev)
Johannes Berge2ebc742007-07-27 15:43:22 +02001014{
Johannes Berge039fa42008-05-15 12:55:29 +02001015 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001016 struct net_device *dev;
1017
Johannes Berge039fa42008-05-15 12:55:29 +02001018 dev = dev_get_by_index(&init_net, info->control.ifindex);
Johannes Berge2ebc742007-07-27 15:43:22 +02001019 if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
1020 dev_put(dev);
1021 dev = NULL;
1022 }
1023 if (unlikely(!dev))
1024 return -ENODEV;
Johannes Berg58d41852007-09-26 17:53:18 +02001025 /* initialises tx with control */
Johannes Berge039fa42008-05-15 12:55:29 +02001026 __ieee80211_tx_prepare(tx, skb, dev);
Johannes Berg32bfd352007-12-19 01:31:26 +01001027 dev_put(dev);
Johannes Berge2ebc742007-07-27 15:43:22 +02001028 return 0;
1029}
1030
1031static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
Johannes Berg5cf121c2008-02-25 16:27:43 +01001032 struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +02001033{
Johannes Berge039fa42008-05-15 12:55:29 +02001034 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001035 int ret, i;
1036
Johannes Berge2530082008-05-17 00:57:14 +02001037 if (netif_subqueue_stopped(local->mdev, skb))
Johannes Berge2ebc742007-07-27 15:43:22 +02001038 return IEEE80211_TX_AGAIN;
Johannes Berge2530082008-05-17 00:57:14 +02001039
Johannes Berge2ebc742007-07-27 15:43:22 +02001040 if (skb) {
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001041 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
1042 "TX to low-level driver", skb);
Johannes Berge039fa42008-05-15 12:55:29 +02001043 ret = local->ops->tx(local_to_hw(local), skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001044 if (ret)
1045 return IEEE80211_TX_AGAIN;
1046 local->mdev->trans_start = jiffies;
1047 ieee80211_led_tx(local, 1);
1048 }
Johannes Berg5cf121c2008-02-25 16:27:43 +01001049 if (tx->extra_frag) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001050 for (i = 0; i < tx->num_extra_frag; i++) {
1051 if (!tx->extra_frag[i])
Johannes Berge2ebc742007-07-27 15:43:22 +02001052 continue;
Johannes Berge039fa42008-05-15 12:55:29 +02001053 info = IEEE80211_SKB_CB(tx->extra_frag[i]);
1054 info->flags &= ~(IEEE80211_TX_CTL_USE_RTS_CTS |
1055 IEEE80211_TX_CTL_USE_CTS_PROTECT |
1056 IEEE80211_TX_CTL_CLEAR_PS_FILT |
1057 IEEE80211_TX_CTL_FIRST_FRAGMENT);
Johannes Berge2530082008-05-17 00:57:14 +02001058 if (netif_subqueue_stopped(local->mdev,
1059 tx->extra_frag[i]))
Johannes Berge2ebc742007-07-27 15:43:22 +02001060 return IEEE80211_TX_FRAG_AGAIN;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001061 if (i == tx->num_extra_frag) {
Johannes Berge039fa42008-05-15 12:55:29 +02001062 info->tx_rate_idx = tx->last_frag_rate_idx;
Johannes Berg8318d782008-01-24 19:38:38 +01001063
Johannes Berg5cf121c2008-02-25 16:27:43 +01001064 if (tx->flags & IEEE80211_TX_PROBE_LAST_FRAG)
Johannes Berge039fa42008-05-15 12:55:29 +02001065 info->flags |=
1066 IEEE80211_TX_CTL_RATE_CTRL_PROBE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001067 else
Johannes Berge039fa42008-05-15 12:55:29 +02001068 info->flags &=
1069 ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001070 }
1071
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001072 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
Johannes Berge2ebc742007-07-27 15:43:22 +02001073 "TX to low-level driver",
Johannes Berg5cf121c2008-02-25 16:27:43 +01001074 tx->extra_frag[i]);
Johannes Berge2ebc742007-07-27 15:43:22 +02001075 ret = local->ops->tx(local_to_hw(local),
Johannes Berge039fa42008-05-15 12:55:29 +02001076 tx->extra_frag[i]);
Johannes Berge2ebc742007-07-27 15:43:22 +02001077 if (ret)
1078 return IEEE80211_TX_FRAG_AGAIN;
1079 local->mdev->trans_start = jiffies;
1080 ieee80211_led_tx(local, 1);
Johannes Berg5cf121c2008-02-25 16:27:43 +01001081 tx->extra_frag[i] = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02001082 }
Johannes Berg5cf121c2008-02-25 16:27:43 +01001083 kfree(tx->extra_frag);
1084 tx->extra_frag = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02001085 }
1086 return IEEE80211_TX_OK;
1087}
1088
Johannes Berge039fa42008-05-15 12:55:29 +02001089static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001090{
1091 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1092 struct sta_info *sta;
1093 ieee80211_tx_handler *handler;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001094 struct ieee80211_tx_data tx;
Johannes Berg9ae54c82008-01-31 19:48:20 +01001095 ieee80211_tx_result res = TX_DROP, res_prepare;
Johannes Berge039fa42008-05-15 12:55:29 +02001096 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001097 int ret, i;
Johannes Berge2530082008-05-17 00:57:14 +02001098 u16 queue;
Johannes Berge2ebc742007-07-27 15:43:22 +02001099
Johannes Berge2530082008-05-17 00:57:14 +02001100 queue = skb_get_queue_mapping(skb);
1101
1102 WARN_ON(test_bit(queue, local->queues_pending));
Johannes Berge2ebc742007-07-27 15:43:22 +02001103
1104 if (unlikely(skb->len < 10)) {
1105 dev_kfree_skb(skb);
1106 return 0;
1107 }
1108
Johannes Bergd0709a62008-02-25 16:27:46 +01001109 rcu_read_lock();
1110
Johannes Berg58d41852007-09-26 17:53:18 +02001111 /* initialises tx */
Johannes Berge039fa42008-05-15 12:55:29 +02001112 res_prepare = __ieee80211_tx_prepare(&tx, skb, dev);
Johannes Berge2ebc742007-07-27 15:43:22 +02001113
Johannes Berg9ae54c82008-01-31 19:48:20 +01001114 if (res_prepare == TX_DROP) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001115 dev_kfree_skb(skb);
Johannes Bergd0709a62008-02-25 16:27:46 +01001116 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001117 return 0;
1118 }
1119
1120 sta = tx.sta;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001121 tx.channel = local->hw.conf.channel;
Johannes Berge039fa42008-05-15 12:55:29 +02001122 info->band = tx.channel->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02001123
Johannes Berg58905292008-01-31 19:48:25 +01001124 for (handler = ieee80211_tx_handlers; *handler != NULL;
Johannes Berg58d41852007-09-26 17:53:18 +02001125 handler++) {
1126 res = (*handler)(&tx);
Johannes Berg9ae54c82008-01-31 19:48:20 +01001127 if (res != TX_CONTINUE)
Johannes Berg58d41852007-09-26 17:53:18 +02001128 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02001129 }
1130
Johannes Berge039fa42008-05-15 12:55:29 +02001131 if (WARN_ON(tx.skb != skb))
1132 goto drop;
Johannes Berge2ebc742007-07-27 15:43:22 +02001133
Johannes Berg9ae54c82008-01-31 19:48:20 +01001134 if (unlikely(res == TX_DROP)) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001135 I802_DEBUG_INC(local->tx_handlers_drop);
1136 goto drop;
1137 }
1138
Johannes Berg9ae54c82008-01-31 19:48:20 +01001139 if (unlikely(res == TX_QUEUED)) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001140 I802_DEBUG_INC(local->tx_handlers_queued);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001141 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001142 return 0;
1143 }
1144
Johannes Berg5cf121c2008-02-25 16:27:43 +01001145 if (tx.extra_frag) {
1146 for (i = 0; i < tx.num_extra_frag; i++) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001147 int next_len, dur;
1148 struct ieee80211_hdr *hdr =
1149 (struct ieee80211_hdr *)
Johannes Berg5cf121c2008-02-25 16:27:43 +01001150 tx.extra_frag[i]->data;
Johannes Berge2ebc742007-07-27 15:43:22 +02001151
Johannes Berg5cf121c2008-02-25 16:27:43 +01001152 if (i + 1 < tx.num_extra_frag) {
1153 next_len = tx.extra_frag[i + 1]->len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001154 } else {
1155 next_len = 0;
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001156 tx.rate_idx = tx.last_frag_rate_idx;
Johannes Berge2ebc742007-07-27 15:43:22 +02001157 }
1158 dur = ieee80211_duration(&tx, 0, next_len);
1159 hdr->duration_id = cpu_to_le16(dur);
1160 }
1161 }
1162
1163retry:
1164 ret = __ieee80211_tx(local, skb, &tx);
1165 if (ret) {
Johannes Bergeefce912008-05-17 00:57:13 +02001166 struct ieee80211_tx_stored_packet *store;
1167
1168 /*
1169 * Since there are no fragmented frames on A-MPDU
1170 * queues, there's no reason for a driver to reject
1171 * a frame there, warn and drop it.
1172 */
Johannes Berge2530082008-05-17 00:57:14 +02001173 if (WARN_ON(queue >= ieee80211_num_regular_queues(&local->hw)))
Johannes Bergeefce912008-05-17 00:57:13 +02001174 goto drop;
1175
1176 store = &local->pending_packet[queue];
Johannes Berge2ebc742007-07-27 15:43:22 +02001177
1178 if (ret == IEEE80211_TX_FRAG_AGAIN)
1179 skb = NULL;
Johannes Berge2530082008-05-17 00:57:14 +02001180 set_bit(queue, local->queues_pending);
Johannes Berge2ebc742007-07-27 15:43:22 +02001181 smp_mb();
Johannes Berge2530082008-05-17 00:57:14 +02001182 /*
1183 * When the driver gets out of buffers during sending of
1184 * fragments and calls ieee80211_stop_queue, the netif
1185 * subqueue is stopped. There is, however, a small window
1186 * in which the PENDING bit is not yet set. If a buffer
Johannes Berge2ebc742007-07-27 15:43:22 +02001187 * gets available in that window (i.e. driver calls
1188 * ieee80211_wake_queue), we would end up with ieee80211_tx
Johannes Berge2530082008-05-17 00:57:14 +02001189 * called with the PENDING bit still set. Prevent this by
Johannes Berge2ebc742007-07-27 15:43:22 +02001190 * continuing transmitting here when that situation is
Johannes Berge2530082008-05-17 00:57:14 +02001191 * possible to have happened.
1192 */
1193 if (!__netif_subqueue_stopped(local->mdev, queue)) {
1194 clear_bit(queue, local->queues_pending);
Johannes Berge2ebc742007-07-27 15:43:22 +02001195 goto retry;
1196 }
Johannes Berge2ebc742007-07-27 15:43:22 +02001197 store->skb = skb;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001198 store->extra_frag = tx.extra_frag;
1199 store->num_extra_frag = tx.num_extra_frag;
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001200 store->last_frag_rate_idx = tx.last_frag_rate_idx;
Jiri Slabybadffb72007-08-28 17:01:54 -04001201 store->last_frag_rate_ctrl_probe =
Johannes Berg5cf121c2008-02-25 16:27:43 +01001202 !!(tx.flags & IEEE80211_TX_PROBE_LAST_FRAG);
Johannes Berge2ebc742007-07-27 15:43:22 +02001203 }
Johannes Bergd4e46a32007-09-14 11:10:24 -04001204 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001205 return 0;
1206
1207 drop:
1208 if (skb)
1209 dev_kfree_skb(skb);
Johannes Berg5cf121c2008-02-25 16:27:43 +01001210 for (i = 0; i < tx.num_extra_frag; i++)
1211 if (tx.extra_frag[i])
1212 dev_kfree_skb(tx.extra_frag[i]);
1213 kfree(tx.extra_frag);
Johannes Bergd4e46a32007-09-14 11:10:24 -04001214 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001215 return 0;
1216}
1217
1218/* device xmit handlers */
1219
Johannes Berg23c07522008-05-29 10:38:53 +02001220static int ieee80211_skb_resize(struct ieee80211_local *local,
1221 struct sk_buff *skb,
1222 int head_need, bool may_encrypt)
1223{
1224 int tail_need = 0;
1225
1226 /*
1227 * This could be optimised, devices that do full hardware
1228 * crypto (including TKIP MMIC) need no tailroom... But we
1229 * have no drivers for such devices currently.
1230 */
1231 if (may_encrypt) {
1232 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1233 tail_need -= skb_tailroom(skb);
1234 tail_need = max_t(int, tail_need, 0);
1235 }
1236
1237 if (head_need || tail_need) {
1238 /* Sorry. Can't account for this any more */
1239 skb_orphan(skb);
1240 }
1241
1242 if (skb_header_cloned(skb))
1243 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1244 else
1245 I802_DEBUG_INC(local->tx_expand_skb_head);
1246
1247 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1248 printk(KERN_DEBUG "%s: failed to reallocate TX buffer\n",
1249 wiphy_name(local->hw.wiphy));
1250 return -ENOMEM;
1251 }
1252
1253 /* update truesize too */
1254 skb->truesize += head_need + tail_need;
1255
1256 return 0;
1257}
1258
Johannes Berge2ebc742007-07-27 15:43:22 +02001259int ieee80211_master_start_xmit(struct sk_buff *skb,
1260 struct net_device *dev)
1261{
Johannes Berge039fa42008-05-15 12:55:29 +02001262 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001263 struct net_device *odev = NULL;
1264 struct ieee80211_sub_if_data *osdata;
1265 int headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001266 bool may_encrypt;
Johannes Berge2ebc742007-07-27 15:43:22 +02001267 int ret;
1268
Johannes Berge039fa42008-05-15 12:55:29 +02001269 if (info->control.ifindex)
1270 odev = dev_get_by_index(&init_net, info->control.ifindex);
Johannes Berge2ebc742007-07-27 15:43:22 +02001271 if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
1272 dev_put(odev);
1273 odev = NULL;
1274 }
1275 if (unlikely(!odev)) {
1276#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1277 printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
1278 "originating device\n", dev->name);
1279#endif
1280 dev_kfree_skb(skb);
1281 return 0;
1282 }
Johannes Berge039fa42008-05-15 12:55:29 +02001283
Johannes Berge2ebc742007-07-27 15:43:22 +02001284 osdata = IEEE80211_DEV_TO_SUB_IF(odev);
1285
Johannes Berg23c07522008-05-29 10:38:53 +02001286 may_encrypt = !(info->flags & IEEE80211_TX_CTL_DO_NOT_ENCRYPT);
1287
1288 headroom = osdata->local->tx_headroom;
1289 if (may_encrypt)
1290 headroom += IEEE80211_ENCRYPT_HEADROOM;
1291 headroom -= skb_headroom(skb);
1292 headroom = max_t(int, 0, headroom);
1293
1294 if (ieee80211_skb_resize(osdata->local, skb, headroom, may_encrypt)) {
1295 dev_kfree_skb(skb);
1296 dev_put(odev);
1297 return 0;
Johannes Berge2ebc742007-07-27 15:43:22 +02001298 }
1299
Johannes Berge039fa42008-05-15 12:55:29 +02001300 info->control.vif = &osdata->vif;
1301 ret = ieee80211_tx(odev, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001302 dev_put(odev);
1303
1304 return ret;
1305}
1306
1307int ieee80211_monitor_start_xmit(struct sk_buff *skb,
1308 struct net_device *dev)
1309{
1310 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berge039fa42008-05-15 12:55:29 +02001311 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001312 struct ieee80211_radiotap_header *prthdr =
1313 (struct ieee80211_radiotap_header *)skb->data;
Andy Green9b8a74e2007-07-27 15:43:24 +02001314 u16 len_rthdr;
Johannes Berge2ebc742007-07-27 15:43:22 +02001315
Andy Green9b8a74e2007-07-27 15:43:24 +02001316 /* check for not even having the fixed radiotap header part */
1317 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1318 goto fail; /* too short to be possibly valid */
1319
1320 /* is it a header version we can trust to find length from? */
1321 if (unlikely(prthdr->it_version))
1322 goto fail; /* only version 0 is supported */
1323
1324 /* then there must be a radiotap header with a length we can use */
1325 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1326
1327 /* does the skb contain enough to deliver on the alleged length? */
1328 if (unlikely(skb->len < len_rthdr))
1329 goto fail; /* skb too short for claimed rt header extent */
Johannes Berge2ebc742007-07-27 15:43:22 +02001330
1331 skb->dev = local->mdev;
1332
Andy Green9b8a74e2007-07-27 15:43:24 +02001333 /* needed because we set skb device to master */
Johannes Berge039fa42008-05-15 12:55:29 +02001334 info->control.ifindex = dev->ifindex;
Andy Green9b8a74e2007-07-27 15:43:24 +02001335
Johannes Berge039fa42008-05-15 12:55:29 +02001336 info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
Ivo van Doornb0a67172008-05-13 15:03:02 +02001337 /* Interfaces should always request a status report */
Johannes Berge039fa42008-05-15 12:55:29 +02001338 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
Johannes Berge2ebc742007-07-27 15:43:22 +02001339
Johannes Berge2ebc742007-07-27 15:43:22 +02001340 /*
1341 * fix up the pointers accounting for the radiotap
1342 * header still being in there. We are being given
1343 * a precooked IEEE80211 header so no need for
1344 * normal processing
1345 */
Andy Green9b8a74e2007-07-27 15:43:24 +02001346 skb_set_mac_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02001347 /*
Andy Green9b8a74e2007-07-27 15:43:24 +02001348 * these are just fixed to the end of the rt area since we
1349 * don't have any better information and at this point, nobody cares
Johannes Berge2ebc742007-07-27 15:43:22 +02001350 */
Andy Green9b8a74e2007-07-27 15:43:24 +02001351 skb_set_network_header(skb, len_rthdr);
1352 skb_set_transport_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02001353
Andy Green9b8a74e2007-07-27 15:43:24 +02001354 /* pass the radiotap header up to the next stage intact */
1355 dev_queue_xmit(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001356 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02001357
1358fail:
1359 dev_kfree_skb(skb);
1360 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02001361}
1362
1363/**
1364 * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1365 * subinterfaces (wlan#, WDS, and VLAN interfaces)
1366 * @skb: packet to be sent
1367 * @dev: incoming interface
1368 *
1369 * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1370 * not be freed, and caller is responsible for either retrying later or freeing
1371 * skb).
1372 *
1373 * This function takes in an Ethernet header and encapsulates it with suitable
1374 * IEEE 802.11 header based on which interface the packet is coming in. The
1375 * encapsulated packet will then be passed to master interface, wlan#.11, for
1376 * transmission (through low-level driver).
1377 */
1378int ieee80211_subif_start_xmit(struct sk_buff *skb,
1379 struct net_device *dev)
1380{
1381 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berge039fa42008-05-15 12:55:29 +02001382 struct ieee80211_tx_info *info;
Johannes Berge2ebc742007-07-27 15:43:22 +02001383 struct ieee80211_sub_if_data *sdata;
1384 int ret = 1, head_need;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001385 u16 ethertype, hdrlen, meshhdrlen = 0, fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02001386 struct ieee80211_hdr hdr;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001387 struct ieee80211s_hdr mesh_hdr;
Johannes Berge2ebc742007-07-27 15:43:22 +02001388 const u8 *encaps_data;
1389 int encaps_len, skip_header_bytes;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001390 int nh_pos, h_pos;
Johannes Berge2ebc742007-07-27 15:43:22 +02001391 struct sta_info *sta;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001392 u32 sta_flags = 0;
Johannes Berge2ebc742007-07-27 15:43:22 +02001393
1394 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1395 if (unlikely(skb->len < ETH_HLEN)) {
1396 printk(KERN_DEBUG "%s: short skb (len=%d)\n",
1397 dev->name, skb->len);
1398 ret = 0;
1399 goto fail;
1400 }
1401
1402 nh_pos = skb_network_header(skb) - skb->data;
1403 h_pos = skb_transport_header(skb) - skb->data;
1404
1405 /* convert Ethernet header to proper 802.11 header (based on
1406 * operation mode) */
1407 ethertype = (skb->data[12] << 8) | skb->data[13];
Johannes Berge2ebc742007-07-27 15:43:22 +02001408 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
1409
Johannes Berg51fb61e2007-12-19 01:31:27 +01001410 switch (sdata->vif.type) {
Johannes Bergcf966832007-08-28 17:01:54 -04001411 case IEEE80211_IF_TYPE_AP:
1412 case IEEE80211_IF_TYPE_VLAN:
Johannes Berge2ebc742007-07-27 15:43:22 +02001413 fc |= IEEE80211_FCTL_FROMDS;
1414 /* DA BSSID SA */
1415 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1416 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1417 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1418 hdrlen = 24;
Johannes Bergcf966832007-08-28 17:01:54 -04001419 break;
1420 case IEEE80211_IF_TYPE_WDS:
Johannes Berge2ebc742007-07-27 15:43:22 +02001421 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1422 /* RA TA DA SA */
1423 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1424 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1425 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1426 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1427 hdrlen = 30;
Johannes Bergcf966832007-08-28 17:01:54 -04001428 break;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001429#ifdef CONFIG_MAC80211_MESH
1430 case IEEE80211_IF_TYPE_MESH_POINT:
1431 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1432 /* RA TA DA SA */
1433 if (is_multicast_ether_addr(skb->data))
1434 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1435 else if (mesh_nexthop_lookup(hdr.addr1, skb, dev))
1436 return 0;
1437 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1438 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1439 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1440 if (skb->pkt_type == PACKET_OTHERHOST) {
1441 /* Forwarded frame, keep mesh ttl and seqnum */
1442 struct ieee80211s_hdr *prev_meshhdr;
1443 prev_meshhdr = ((struct ieee80211s_hdr *)skb->cb);
1444 meshhdrlen = ieee80211_get_mesh_hdrlen(prev_meshhdr);
1445 memcpy(&mesh_hdr, prev_meshhdr, meshhdrlen);
1446 sdata->u.sta.mshstats.fwded_frames++;
1447 } else {
1448 if (!sdata->u.sta.mshcfg.dot11MeshTTL) {
1449 /* Do not send frames with mesh_ttl == 0 */
1450 sdata->u.sta.mshstats.dropped_frames_ttl++;
1451 ret = 0;
1452 goto fail;
1453 }
1454 meshhdrlen = ieee80211_new_mesh_header(&mesh_hdr,
Johannes Berg902acc72008-02-23 15:17:19 +01001455 sdata);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001456 }
1457 hdrlen = 30;
1458 break;
1459#endif
Johannes Bergcf966832007-08-28 17:01:54 -04001460 case IEEE80211_IF_TYPE_STA:
Johannes Berge2ebc742007-07-27 15:43:22 +02001461 fc |= IEEE80211_FCTL_TODS;
1462 /* BSSID SA DA */
1463 memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
1464 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1465 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1466 hdrlen = 24;
Johannes Bergcf966832007-08-28 17:01:54 -04001467 break;
1468 case IEEE80211_IF_TYPE_IBSS:
Johannes Berge2ebc742007-07-27 15:43:22 +02001469 /* DA SA BSSID */
1470 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1471 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1472 memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
1473 hdrlen = 24;
Johannes Bergcf966832007-08-28 17:01:54 -04001474 break;
1475 default:
Johannes Berge2ebc742007-07-27 15:43:22 +02001476 ret = 0;
1477 goto fail;
1478 }
1479
Johannes Berg7d185b82008-01-28 17:11:43 +01001480 /*
1481 * There's no need to try to look up the destination
1482 * if it is a multicast address (which can only happen
1483 * in AP mode)
1484 */
1485 if (!is_multicast_ether_addr(hdr.addr1)) {
Johannes Bergd0709a62008-02-25 16:27:46 +01001486 rcu_read_lock();
Johannes Berg7d185b82008-01-28 17:11:43 +01001487 sta = sta_info_get(local, hdr.addr1);
Johannes Bergd0709a62008-02-25 16:27:46 +01001488 if (sta)
Johannes Berg07346f812008-05-03 01:02:02 +02001489 sta_flags = get_sta_flags(sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01001490 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001491 }
1492
Johannes Berg3434fbd2008-05-03 00:59:37 +02001493 /* receiver and we are QoS enabled, use a QoS type frame */
Johannes Berge2530082008-05-17 00:57:14 +02001494 if (sta_flags & WLAN_STA_WME &&
1495 ieee80211_num_regular_queues(&local->hw) >= 4) {
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001496 fc |= IEEE80211_STYPE_QOS_DATA;
1497 hdrlen += 2;
1498 }
1499
1500 /*
Johannes Berg238814f2008-01-28 17:19:37 +01001501 * Drop unicast frames to unauthorised stations unless they are
1502 * EAPOL frames from the local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001503 */
Johannes Berg238814f2008-01-28 17:19:37 +01001504 if (unlikely(!is_multicast_ether_addr(hdr.addr1) &&
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001505 !(sta_flags & WLAN_STA_AUTHORIZED) &&
1506 !(ethertype == ETH_P_PAE &&
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001507 compare_ether_addr(dev->dev_addr,
1508 skb->data + ETH_ALEN) == 0))) {
1509#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1510 DECLARE_MAC_BUF(mac);
1511
1512 if (net_ratelimit())
1513 printk(KERN_DEBUG "%s: dropped frame to %s"
1514 " (unauthorized port)\n", dev->name,
1515 print_mac(mac, hdr.addr1));
1516#endif
1517
1518 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
1519
1520 ret = 0;
1521 goto fail;
1522 }
1523
Johannes Berge2ebc742007-07-27 15:43:22 +02001524 hdr.frame_control = cpu_to_le16(fc);
1525 hdr.duration_id = 0;
1526 hdr.seq_ctrl = 0;
1527
1528 skip_header_bytes = ETH_HLEN;
1529 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1530 encaps_data = bridge_tunnel_header;
1531 encaps_len = sizeof(bridge_tunnel_header);
1532 skip_header_bytes -= 2;
1533 } else if (ethertype >= 0x600) {
1534 encaps_data = rfc1042_header;
1535 encaps_len = sizeof(rfc1042_header);
1536 skip_header_bytes -= 2;
1537 } else {
1538 encaps_data = NULL;
1539 encaps_len = 0;
1540 }
1541
1542 skb_pull(skb, skip_header_bytes);
1543 nh_pos -= skip_header_bytes;
1544 h_pos -= skip_header_bytes;
1545
1546 /* TODO: implement support for fragments so that there is no need to
1547 * reallocate and copy payload; it might be enough to support one
1548 * extra fragment that would be copied in the beginning of the frame
1549 * data.. anyway, it would be nice to include this into skb structure
1550 * somehow
1551 *
1552 * There are few options for this:
1553 * use skb->cb as an extra space for 802.11 header
1554 * allocate new buffer if not enough headroom
1555 * make sure that there is enough headroom in every skb by increasing
1556 * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
1557 * alloc_skb() (net/core/skbuff.c)
1558 */
Johannes Berg23c07522008-05-29 10:38:53 +02001559 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001560
Johannes Berg23c07522008-05-29 10:38:53 +02001561 /*
1562 * So we need to modify the skb header and hence need a copy of
1563 * that. The head_need variable above doesn't, so far, include
1564 * the needed header space that we don't need right away. If we
1565 * can, then we don't reallocate right now but only after the
1566 * frame arrives at the master device (if it does...)
1567 *
1568 * If we cannot, however, then we will reallocate to include all
1569 * the ever needed space. Also, if we need to reallocate it anyway,
1570 * make it big enough for everything we may ever need.
1571 */
Johannes Berge2ebc742007-07-27 15:43:22 +02001572
David S. Miller608961a2008-05-12 21:59:32 -07001573 if (head_need > 0 || skb_header_cloned(skb)) {
Johannes Berg23c07522008-05-29 10:38:53 +02001574 head_need += IEEE80211_ENCRYPT_HEADROOM;
1575 head_need += local->tx_headroom;
1576 head_need = max_t(int, 0, head_need);
1577 if (ieee80211_skb_resize(local, skb, head_need, true))
Johannes Berge2ebc742007-07-27 15:43:22 +02001578 goto fail;
Johannes Berge2ebc742007-07-27 15:43:22 +02001579 }
1580
1581 if (encaps_data) {
1582 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1583 nh_pos += encaps_len;
1584 h_pos += encaps_len;
1585 }
Johannes Bergc29b9b92007-09-14 11:10:24 -04001586
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001587 if (meshhdrlen > 0) {
1588 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
1589 nh_pos += meshhdrlen;
1590 h_pos += meshhdrlen;
1591 }
1592
Johannes Bergc29b9b92007-09-14 11:10:24 -04001593 if (fc & IEEE80211_STYPE_QOS_DATA) {
1594 __le16 *qos_control;
1595
1596 qos_control = (__le16*) skb_push(skb, 2);
1597 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
1598 /*
1599 * Maybe we could actually set some fields here, for now just
1600 * initialise to zero to indicate no special operation.
1601 */
1602 *qos_control = 0;
1603 } else
1604 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1605
Johannes Berge2ebc742007-07-27 15:43:22 +02001606 nh_pos += hdrlen;
1607 h_pos += hdrlen;
1608
Johannes Berge039fa42008-05-15 12:55:29 +02001609 info = IEEE80211_SKB_CB(skb);
1610 memset(info, 0, sizeof(*info));
1611 info->control.ifindex = dev->ifindex;
Johannes Berg678f5f72007-12-19 01:31:23 +01001612 if (ethertype == ETH_P_PAE)
Johannes Berge039fa42008-05-15 12:55:29 +02001613 info->flags |= IEEE80211_TX_CTL_EAPOL_FRAME;
Johannes Berge2ebc742007-07-27 15:43:22 +02001614
Ivo van Doornb0a67172008-05-13 15:03:02 +02001615 /* Interfaces should always request a status report */
Johannes Berge039fa42008-05-15 12:55:29 +02001616 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
Ivo van Doornb0a67172008-05-13 15:03:02 +02001617
Johannes Berge2ebc742007-07-27 15:43:22 +02001618 skb->dev = local->mdev;
Stephen Hemminger68aae112007-08-24 11:29:34 -07001619 dev->stats.tx_packets++;
1620 dev->stats.tx_bytes += skb->len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001621
1622 /* Update skb pointers to various headers since this modified frame
1623 * is going to go through Linux networking code that may potentially
1624 * need things like pointer to IP header. */
1625 skb_set_mac_header(skb, 0);
1626 skb_set_network_header(skb, nh_pos);
1627 skb_set_transport_header(skb, h_pos);
1628
1629 dev->trans_start = jiffies;
1630 dev_queue_xmit(skb);
1631
1632 return 0;
1633
1634 fail:
1635 if (!ret)
1636 dev_kfree_skb(skb);
1637
1638 return ret;
1639}
1640
Johannes Berge2ebc742007-07-27 15:43:22 +02001641
Johannes Berge2530082008-05-17 00:57:14 +02001642/*
1643 * ieee80211_clear_tx_pending may not be called in a context where
1644 * it is possible that it packets could come in again.
1645 */
Johannes Berge2ebc742007-07-27 15:43:22 +02001646void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1647{
1648 int i, j;
1649 struct ieee80211_tx_stored_packet *store;
1650
Johannes Berge2530082008-05-17 00:57:14 +02001651 for (i = 0; i < ieee80211_num_regular_queues(&local->hw); i++) {
1652 if (!test_bit(i, local->queues_pending))
Johannes Berge2ebc742007-07-27 15:43:22 +02001653 continue;
1654 store = &local->pending_packet[i];
1655 kfree_skb(store->skb);
1656 for (j = 0; j < store->num_extra_frag; j++)
1657 kfree_skb(store->extra_frag[j]);
1658 kfree(store->extra_frag);
Johannes Berge2530082008-05-17 00:57:14 +02001659 clear_bit(i, local->queues_pending);
Johannes Berge2ebc742007-07-27 15:43:22 +02001660 }
1661}
1662
Johannes Berge2530082008-05-17 00:57:14 +02001663/*
1664 * Transmit all pending packets. Called from tasklet, locks master device
1665 * TX lock so that no new packets can come in.
1666 */
Johannes Berge2ebc742007-07-27 15:43:22 +02001667void ieee80211_tx_pending(unsigned long data)
1668{
1669 struct ieee80211_local *local = (struct ieee80211_local *)data;
1670 struct net_device *dev = local->mdev;
1671 struct ieee80211_tx_stored_packet *store;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001672 struct ieee80211_tx_data tx;
Johannes Berge2530082008-05-17 00:57:14 +02001673 int i, ret;
Johannes Berge2ebc742007-07-27 15:43:22 +02001674
1675 netif_tx_lock_bh(dev);
Johannes Berge2530082008-05-17 00:57:14 +02001676 for (i = 0; i < ieee80211_num_regular_queues(&local->hw); i++) {
1677 /* Check that this queue is ok */
1678 if (__netif_subqueue_stopped(local->mdev, i))
Johannes Berge2ebc742007-07-27 15:43:22 +02001679 continue;
Johannes Berge2530082008-05-17 00:57:14 +02001680
1681 if (!test_bit(i, local->queues_pending)) {
1682 ieee80211_wake_queue(&local->hw, i);
Johannes Berge2ebc742007-07-27 15:43:22 +02001683 continue;
1684 }
Johannes Berge2530082008-05-17 00:57:14 +02001685
Johannes Berge2ebc742007-07-27 15:43:22 +02001686 store = &local->pending_packet[i];
Johannes Berg5cf121c2008-02-25 16:27:43 +01001687 tx.extra_frag = store->extra_frag;
1688 tx.num_extra_frag = store->num_extra_frag;
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001689 tx.last_frag_rate_idx = store->last_frag_rate_idx;
Jiri Slabybadffb72007-08-28 17:01:54 -04001690 tx.flags = 0;
1691 if (store->last_frag_rate_ctrl_probe)
Johannes Berg5cf121c2008-02-25 16:27:43 +01001692 tx.flags |= IEEE80211_TX_PROBE_LAST_FRAG;
Johannes Berge2ebc742007-07-27 15:43:22 +02001693 ret = __ieee80211_tx(local, store->skb, &tx);
1694 if (ret) {
1695 if (ret == IEEE80211_TX_FRAG_AGAIN)
1696 store->skb = NULL;
1697 } else {
Johannes Berge2530082008-05-17 00:57:14 +02001698 clear_bit(i, local->queues_pending);
1699 ieee80211_wake_queue(&local->hw, i);
Johannes Berge2ebc742007-07-27 15:43:22 +02001700 }
1701 }
1702 netif_tx_unlock_bh(dev);
Johannes Berge2ebc742007-07-27 15:43:22 +02001703}
1704
1705/* functions for drivers to get certain frames */
1706
1707static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
1708 struct ieee80211_if_ap *bss,
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001709 struct sk_buff *skb,
1710 struct beacon_data *beacon)
Johannes Berge2ebc742007-07-27 15:43:22 +02001711{
1712 u8 *pos, *tim;
1713 int aid0 = 0;
1714 int i, have_bits = 0, n1, n2;
1715
1716 /* Generate bitmap for TIM only if there are any STAs in power save
1717 * mode. */
Johannes Berge2ebc742007-07-27 15:43:22 +02001718 if (atomic_read(&bss->num_sta_ps) > 0)
1719 /* in the hope that this is faster than
1720 * checking byte-for-byte */
1721 have_bits = !bitmap_empty((unsigned long*)bss->tim,
1722 IEEE80211_MAX_AID+1);
1723
1724 if (bss->dtim_count == 0)
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001725 bss->dtim_count = beacon->dtim_period - 1;
Johannes Berge2ebc742007-07-27 15:43:22 +02001726 else
1727 bss->dtim_count--;
1728
1729 tim = pos = (u8 *) skb_put(skb, 6);
1730 *pos++ = WLAN_EID_TIM;
1731 *pos++ = 4;
1732 *pos++ = bss->dtim_count;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001733 *pos++ = beacon->dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02001734
1735 if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
1736 aid0 = 1;
1737
1738 if (have_bits) {
1739 /* Find largest even number N1 so that bits numbered 1 through
1740 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
1741 * (N2 + 1) x 8 through 2007 are 0. */
1742 n1 = 0;
1743 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
1744 if (bss->tim[i]) {
1745 n1 = i & 0xfe;
1746 break;
1747 }
1748 }
1749 n2 = n1;
1750 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
1751 if (bss->tim[i]) {
1752 n2 = i;
1753 break;
1754 }
1755 }
1756
1757 /* Bitmap control */
1758 *pos++ = n1 | aid0;
1759 /* Part Virt Bitmap */
1760 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
1761
1762 tim[1] = n2 - n1 + 4;
1763 skb_put(skb, n2 - n1);
1764 } else {
1765 *pos++ = aid0; /* Bitmap control */
1766 *pos++ = 0; /* Part Virt Bitmap */
1767 }
Johannes Berge2ebc742007-07-27 15:43:22 +02001768}
1769
Johannes Berg32bfd352007-12-19 01:31:26 +01001770struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
Johannes Berge039fa42008-05-15 12:55:29 +02001771 struct ieee80211_vif *vif)
Johannes Berge2ebc742007-07-27 15:43:22 +02001772{
1773 struct ieee80211_local *local = hw_to_local(hw);
1774 struct sk_buff *skb;
Johannes Berge039fa42008-05-15 12:55:29 +02001775 struct ieee80211_tx_info *info;
Johannes Berge2ebc742007-07-27 15:43:22 +02001776 struct net_device *bdev;
1777 struct ieee80211_sub_if_data *sdata = NULL;
1778 struct ieee80211_if_ap *ap = NULL;
Mattias Nissler1abbe492007-12-20 13:50:07 +01001779 struct rate_selection rsel;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001780 struct beacon_data *beacon;
Johannes Berg8318d782008-01-24 19:38:38 +01001781 struct ieee80211_supported_band *sband;
Johannes Berg902acc72008-02-23 15:17:19 +01001782 struct ieee80211_mgmt *mgmt;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001783 int *num_beacons;
Johannes Berg902acc72008-02-23 15:17:19 +01001784 bool err = true;
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001785 enum ieee80211_band band = local->hw.conf.channel->band;
Johannes Berg902acc72008-02-23 15:17:19 +01001786 u8 *pos;
Johannes Berg8318d782008-01-24 19:38:38 +01001787
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001788 sband = local->hw.wiphy->bands[band];
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001789
1790 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001791
Johannes Berg32bfd352007-12-19 01:31:26 +01001792 sdata = vif_to_sdata(vif);
1793 bdev = sdata->dev;
Johannes Berge2ebc742007-07-27 15:43:22 +02001794
Johannes Berg902acc72008-02-23 15:17:19 +01001795 if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001796 ap = &sdata->u.ap;
1797 beacon = rcu_dereference(ap->beacon);
Johannes Berg902acc72008-02-23 15:17:19 +01001798 if (ap && beacon) {
1799 /*
1800 * headroom, head length,
1801 * tail length and maximum TIM length
1802 */
1803 skb = dev_alloc_skb(local->tx_headroom +
1804 beacon->head_len +
1805 beacon->tail_len + 256);
1806 if (!skb)
1807 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001808
Johannes Berg902acc72008-02-23 15:17:19 +01001809 skb_reserve(skb, local->tx_headroom);
1810 memcpy(skb_put(skb, beacon->head_len), beacon->head,
1811 beacon->head_len);
1812
1813 ieee80211_include_sequence(sdata,
1814 (struct ieee80211_hdr *)skb->data);
1815
Johannes Bergd0709a62008-02-25 16:27:46 +01001816 /*
1817 * Not very nice, but we want to allow the driver to call
1818 * ieee80211_beacon_get() as a response to the set_tim()
1819 * callback. That, however, is already invoked under the
1820 * sta_lock to guarantee consistent and race-free update
1821 * of the tim bitmap in mac80211 and the driver.
1822 */
1823 if (local->tim_in_locked_section) {
1824 ieee80211_beacon_add_tim(local, ap, skb, beacon);
1825 } else {
1826 unsigned long flags;
1827
1828 spin_lock_irqsave(&local->sta_lock, flags);
1829 ieee80211_beacon_add_tim(local, ap, skb, beacon);
1830 spin_unlock_irqrestore(&local->sta_lock, flags);
1831 }
Johannes Berg902acc72008-02-23 15:17:19 +01001832
1833 if (beacon->tail)
1834 memcpy(skb_put(skb, beacon->tail_len),
1835 beacon->tail, beacon->tail_len);
1836
1837 num_beacons = &ap->num_beacons;
1838
1839 err = false;
1840 }
1841 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001842 /* headroom, head length, tail length and maximum TIM length */
Johannes Berg902acc72008-02-23 15:17:19 +01001843 skb = dev_alloc_skb(local->tx_headroom + 400);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001844 if (!skb)
1845 goto out;
1846
Johannes Berg902acc72008-02-23 15:17:19 +01001847 skb_reserve(skb, local->hw.extra_tx_headroom);
1848 mgmt = (struct ieee80211_mgmt *)
1849 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
1850 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
1851 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1852 IEEE80211_STYPE_BEACON);
1853 memset(mgmt->da, 0xff, ETH_ALEN);
1854 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1855 /* BSSID is left zeroed, wildcard value */
1856 mgmt->u.beacon.beacon_int =
1857 cpu_to_le16(local->hw.conf.beacon_int);
1858 mgmt->u.beacon.capab_info = 0x0; /* 0x0 for MPs */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001859
Johannes Berg902acc72008-02-23 15:17:19 +01001860 pos = skb_put(skb, 2);
1861 *pos++ = WLAN_EID_SSID;
1862 *pos++ = 0x0;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001863
Johannes Berg902acc72008-02-23 15:17:19 +01001864 mesh_mgmt_ies_add(skb, sdata->dev);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001865
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001866 num_beacons = &sdata->u.sta.num_beacons;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001867
Johannes Berg902acc72008-02-23 15:17:19 +01001868 err = false;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001869 }
1870
1871 if (err) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001872#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1873 if (net_ratelimit())
Johannes Berg32bfd352007-12-19 01:31:26 +01001874 printk(KERN_DEBUG "no beacon data avail for %s\n",
1875 bdev->name);
Johannes Berge2ebc742007-07-27 15:43:22 +02001876#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001877 skb = NULL;
1878 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02001879 }
1880
Johannes Berge039fa42008-05-15 12:55:29 +02001881 info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001882
Johannes Berge039fa42008-05-15 12:55:29 +02001883 info->band = band;
1884 rate_control_get_rate(local->mdev, sband, skb, &rsel);
1885
1886 if (unlikely(rsel.rate_idx < 0)) {
1887 if (net_ratelimit()) {
1888 printk(KERN_DEBUG "%s: ieee80211_beacon_get: "
1889 "no rate found\n",
1890 wiphy_name(local->hw.wiphy));
1891 }
1892 dev_kfree_skb(skb);
1893 skb = NULL;
1894 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02001895 }
Johannes Berge039fa42008-05-15 12:55:29 +02001896
1897 info->control.vif = vif;
1898 info->tx_rate_idx = rsel.rate_idx;
1899 if (sdata->bss_conf.use_short_preamble &&
1900 sband->bitrates[rsel.rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE)
1901 info->flags |= IEEE80211_TX_CTL_SHORT_PREAMBLE;
1902 info->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
1903 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1904 info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
1905 info->control.retry_limit = 1;
1906 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001907 (*num_beacons)++;
1908out:
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001909 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001910 return skb;
1911}
1912EXPORT_SYMBOL(ieee80211_beacon_get);
1913
Johannes Berg32bfd352007-12-19 01:31:26 +01001914void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001915 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02001916 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02001917 struct ieee80211_rts *rts)
1918{
1919 const struct ieee80211_hdr *hdr = frame;
1920 u16 fctl;
1921
1922 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
1923 rts->frame_control = cpu_to_le16(fctl);
Johannes Berg32bfd352007-12-19 01:31:26 +01001924 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
1925 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02001926 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
1927 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
1928}
1929EXPORT_SYMBOL(ieee80211_rts_get);
1930
Johannes Berg32bfd352007-12-19 01:31:26 +01001931void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02001932 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02001933 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02001934 struct ieee80211_cts *cts)
1935{
1936 const struct ieee80211_hdr *hdr = frame;
1937 u16 fctl;
1938
1939 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
1940 cts->frame_control = cpu_to_le16(fctl);
Johannes Berg32bfd352007-12-19 01:31:26 +01001941 cts->duration = ieee80211_ctstoself_duration(hw, vif,
1942 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02001943 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
1944}
1945EXPORT_SYMBOL(ieee80211_ctstoself_get);
1946
1947struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01001948ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
Johannes Berge039fa42008-05-15 12:55:29 +02001949 struct ieee80211_vif *vif)
Johannes Berge2ebc742007-07-27 15:43:22 +02001950{
1951 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winkler747cf5e2008-05-27 17:50:51 +03001952 struct sk_buff *skb = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02001953 struct sta_info *sta;
1954 ieee80211_tx_handler *handler;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001955 struct ieee80211_tx_data tx;
Johannes Berg9ae54c82008-01-31 19:48:20 +01001956 ieee80211_tx_result res = TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +02001957 struct net_device *bdev;
1958 struct ieee80211_sub_if_data *sdata;
1959 struct ieee80211_if_ap *bss = NULL;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001960 struct beacon_data *beacon;
Johannes Berge039fa42008-05-15 12:55:29 +02001961 struct ieee80211_tx_info *info;
Johannes Berge2ebc742007-07-27 15:43:22 +02001962
Johannes Berg32bfd352007-12-19 01:31:26 +01001963 sdata = vif_to_sdata(vif);
1964 bdev = sdata->dev;
Tomas Winkler747cf5e2008-05-27 17:50:51 +03001965 bss = &sdata->u.ap;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001966
1967 if (!bss)
Johannes Berge2ebc742007-07-27 15:43:22 +02001968 return NULL;
1969
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001970 rcu_read_lock();
1971 beacon = rcu_dereference(bss->beacon);
1972
Tomas Winkler747cf5e2008-05-27 17:50:51 +03001973 if (sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon || !beacon->head)
1974 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001975
Johannes Berge2ebc742007-07-27 15:43:22 +02001976 if (bss->dtim_count != 0)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03001977 goto out; /* send buffered bc/mc only after DTIM beacon */
Johannes Berge039fa42008-05-15 12:55:29 +02001978
Johannes Berge2ebc742007-07-27 15:43:22 +02001979 while (1) {
1980 skb = skb_dequeue(&bss->ps_bc_buf);
1981 if (!skb)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03001982 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02001983 local->total_ps_buffered--;
1984
1985 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
1986 struct ieee80211_hdr *hdr =
1987 (struct ieee80211_hdr *) skb->data;
1988 /* more buffered multicast/broadcast frames ==> set
1989 * MoreData flag in IEEE 802.11 header to inform PS
1990 * STAs */
1991 hdr->frame_control |=
1992 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1993 }
1994
Johannes Berge039fa42008-05-15 12:55:29 +02001995 if (!ieee80211_tx_prepare(&tx, skb, local->mdev))
Johannes Berge2ebc742007-07-27 15:43:22 +02001996 break;
1997 dev_kfree_skb_any(skb);
1998 }
Johannes Berge039fa42008-05-15 12:55:29 +02001999
2000 info = IEEE80211_SKB_CB(skb);
2001
Johannes Berge2ebc742007-07-27 15:43:22 +02002002 sta = tx.sta;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002003 tx.flags |= IEEE80211_TX_PS_BUFFERED;
2004 tx.channel = local->hw.conf.channel;
Johannes Berge039fa42008-05-15 12:55:29 +02002005 info->band = tx.channel->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02002006
Johannes Berg58905292008-01-31 19:48:25 +01002007 for (handler = ieee80211_tx_handlers; *handler != NULL; handler++) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002008 res = (*handler)(&tx);
Johannes Berg9ae54c82008-01-31 19:48:20 +01002009 if (res == TX_DROP || res == TX_QUEUED)
Johannes Berge2ebc742007-07-27 15:43:22 +02002010 break;
2011 }
Johannes Berge039fa42008-05-15 12:55:29 +02002012
2013 if (WARN_ON(tx.skb != skb))
2014 return NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02002015
Johannes Berg9ae54c82008-01-31 19:48:20 +01002016 if (res == TX_DROP) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002017 I802_DEBUG_INC(local->tx_handlers_drop);
2018 dev_kfree_skb(skb);
2019 skb = NULL;
Johannes Berg9ae54c82008-01-31 19:48:20 +01002020 } else if (res == TX_QUEUED) {
Johannes Berge2ebc742007-07-27 15:43:22 +02002021 I802_DEBUG_INC(local->tx_handlers_queued);
2022 skb = NULL;
2023 }
2024
Tomas Winkler747cf5e2008-05-27 17:50:51 +03002025out:
Johannes Bergd0709a62008-02-25 16:27:46 +01002026 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02002027
2028 return skb;
2029}
2030EXPORT_SYMBOL(ieee80211_get_buffered_bc);