blob: 6fcc85a2806a4de3ed55cc3dad7336854cd39de7 [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 Berg24487982009-04-23 18:52:52 +020028#include "driver-ops.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040029#include "led.h"
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +010030#include "mesh.h"
Johannes Berge2ebc742007-07-27 15:43:22 +020031#include "wep.h"
32#include "wpa.h"
33#include "wme.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040034#include "rate.h"
Johannes Berge2ebc742007-07-27 15:43:22 +020035
36#define IEEE80211_TX_OK 0
37#define IEEE80211_TX_AGAIN 1
Johannes Berg2de8e0d2009-03-23 17:28:35 +010038#define IEEE80211_TX_PENDING 2
Johannes Berge2ebc742007-07-27 15:43:22 +020039
40/* misc utils */
41
Johannes Berg03f93c32008-06-25 14:36:27 +030042static __le16 ieee80211_duration(struct ieee80211_tx_data *tx, int group_addr,
43 int next_frag_len)
Johannes Berge2ebc742007-07-27 15:43:22 +020044{
45 int rate, mrate, erp, dur, i;
Johannes Berg2e92e6f2008-05-15 12:55:27 +020046 struct ieee80211_rate *txrate;
Johannes Berge2ebc742007-07-27 15:43:22 +020047 struct ieee80211_local *local = tx->local;
Johannes Berg8318d782008-01-24 19:38:38 +010048 struct ieee80211_supported_band *sband;
Harvey Harrison358c8d92008-07-15 18:44:13 -070049 struct ieee80211_hdr *hdr;
Johannes Berge6a98542008-10-21 12:40:02 +020050 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
51
52 /* assume HW handles this */
53 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
54 return 0;
55
56 /* uh huh? */
57 if (WARN_ON_ONCE(info->control.rates[0].idx < 0))
58 return 0;
Johannes Berge2ebc742007-07-27 15:43:22 +020059
Johannes Berg2e92e6f2008-05-15 12:55:27 +020060 sband = local->hw.wiphy->bands[tx->channel->band];
Johannes Berge6a98542008-10-21 12:40:02 +020061 txrate = &sband->bitrates[info->control.rates[0].idx];
Johannes Berg8318d782008-01-24 19:38:38 +010062
Johannes Berge6a98542008-10-21 12:40:02 +020063 erp = txrate->flags & IEEE80211_RATE_ERP_G;
Johannes Berge2ebc742007-07-27 15:43:22 +020064
65 /*
66 * data and mgmt (except PS Poll):
67 * - during CFP: 32768
68 * - during contention period:
69 * if addr1 is group address: 0
70 * if more fragments = 0 and addr1 is individual address: time to
71 * transmit one ACK plus SIFS
72 * if more fragments = 1 and addr1 is individual address: time to
73 * transmit next fragment plus 2 x ACK plus 3 x SIFS
74 *
75 * IEEE 802.11, 9.6:
76 * - control response frame (CTS or ACK) shall be transmitted using the
77 * same rate as the immediately previous frame in the frame exchange
78 * sequence, if this rate belongs to the PHY mandatory rates, or else
79 * at the highest possible rate belonging to the PHY rates in the
80 * BSSBasicRateSet
81 */
Harvey Harrison358c8d92008-07-15 18:44:13 -070082 hdr = (struct ieee80211_hdr *)tx->skb->data;
83 if (ieee80211_is_ctl(hdr->frame_control)) {
Johannes Berge2ebc742007-07-27 15:43:22 +020084 /* TODO: These control frames are not currently sent by
Johannes Bergccd7b362008-09-11 00:01:56 +020085 * mac80211, but should they be implemented, this function
Johannes Berge2ebc742007-07-27 15:43:22 +020086 * needs to be updated to support duration field calculation.
87 *
88 * RTS: time needed to transmit pending data/mgmt frame plus
89 * one CTS frame plus one ACK frame plus 3 x SIFS
90 * CTS: duration of immediately previous RTS minus time
91 * required to transmit CTS and its SIFS
92 * ACK: 0 if immediately previous directed data/mgmt had
93 * more=0, with more=1 duration in ACK frame is duration
94 * from previous frame minus time needed to transmit ACK
95 * and its SIFS
96 * PS Poll: BIT(15) | BIT(14) | aid
97 */
98 return 0;
99 }
100
101 /* data/mgmt */
102 if (0 /* FIX: data/mgmt during CFP */)
Johannes Berg03f93c32008-06-25 14:36:27 +0300103 return cpu_to_le16(32768);
Johannes Berge2ebc742007-07-27 15:43:22 +0200104
105 if (group_addr) /* Group address as the destination - no ACK */
106 return 0;
107
108 /* Individual destination address:
109 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
110 * CTS and ACK frames shall be transmitted using the highest rate in
111 * basic rate set that is less than or equal to the rate of the
112 * immediately previous frame and that is using the same modulation
113 * (CCK or OFDM). If no basic rate set matches with these requirements,
114 * the highest mandatory rate of the PHY that is less than or equal to
115 * the rate of the previous frame is used.
116 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
117 */
118 rate = -1;
Johannes Berg8318d782008-01-24 19:38:38 +0100119 /* use lowest available if everything fails */
120 mrate = sband->bitrates[0].bitrate;
121 for (i = 0; i < sband->n_bitrates; i++) {
122 struct ieee80211_rate *r = &sband->bitrates[i];
123
124 if (r->bitrate > txrate->bitrate)
Johannes Berge2ebc742007-07-27 15:43:22 +0200125 break;
126
Johannes Bergbda39332008-10-11 01:51:51 +0200127 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
Johannes Berg8318d782008-01-24 19:38:38 +0100128 rate = r->bitrate;
Johannes Berge2ebc742007-07-27 15:43:22 +0200129
Johannes Berg8318d782008-01-24 19:38:38 +0100130 switch (sband->band) {
131 case IEEE80211_BAND_2GHZ: {
132 u32 flag;
133 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
134 flag = IEEE80211_RATE_MANDATORY_G;
135 else
136 flag = IEEE80211_RATE_MANDATORY_B;
137 if (r->flags & flag)
138 mrate = r->bitrate;
139 break;
140 }
141 case IEEE80211_BAND_5GHZ:
142 if (r->flags & IEEE80211_RATE_MANDATORY_A)
143 mrate = r->bitrate;
144 break;
145 case IEEE80211_NUM_BANDS:
146 WARN_ON(1);
147 break;
148 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200149 }
150 if (rate == -1) {
151 /* No matching basic rate found; use highest suitable mandatory
152 * PHY rate */
153 rate = mrate;
154 }
155
156 /* Time needed to transmit ACK
157 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
158 * to closest integer */
159
160 dur = ieee80211_frame_duration(local, 10, rate, erp,
Johannes Bergbda39332008-10-11 01:51:51 +0200161 tx->sdata->vif.bss_conf.use_short_preamble);
Johannes Berge2ebc742007-07-27 15:43:22 +0200162
163 if (next_frag_len) {
164 /* Frame is fragmented: duration increases with time needed to
165 * transmit next fragment plus ACK and 2 x SIFS. */
166 dur *= 2; /* ACK + SIFS */
167 /* next fragment */
168 dur += ieee80211_frame_duration(local, next_frag_len,
Johannes Berg8318d782008-01-24 19:38:38 +0100169 txrate->bitrate, erp,
Johannes Bergbda39332008-10-11 01:51:51 +0200170 tx->sdata->vif.bss_conf.use_short_preamble);
Johannes Berge2ebc742007-07-27 15:43:22 +0200171 }
172
Johannes Berg03f93c32008-06-25 14:36:27 +0300173 return cpu_to_le16(dur);
Johannes Berge2ebc742007-07-27 15:43:22 +0200174}
175
Johannes Berg133b8222008-09-16 14:18:59 +0200176static int inline is_ieee80211_device(struct ieee80211_local *local,
177 struct net_device *dev)
Johannes Berge2ebc742007-07-27 15:43:22 +0200178{
Johannes Berg133b8222008-09-16 14:18:59 +0200179 return local == wdev_priv(dev->ieee80211_ptr);
Johannes Berge2ebc742007-07-27 15:43:22 +0200180}
181
182/* tx handlers */
Kalle Valo5c1b98a2010-01-12 10:42:46 +0200183static ieee80211_tx_result debug_noinline
184ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
185{
186 struct ieee80211_local *local = tx->local;
187
188 /* driver doesn't support power save */
189 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
190 return TX_CONTINUE;
191
192 /* hardware does dynamic power save */
193 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
194 return TX_CONTINUE;
195
196 /* dynamic power save disabled */
197 if (local->hw.conf.dynamic_ps_timeout <= 0)
198 return TX_CONTINUE;
199
200 /* we are scanning, don't enable power save */
201 if (local->scanning)
202 return TX_CONTINUE;
203
204 if (!local->ps_sdata)
205 return TX_CONTINUE;
206
207 /* No point if we're going to suspend */
208 if (local->quiescing)
209 return TX_CONTINUE;
210
211 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
212 ieee80211_stop_queues_by_reason(&local->hw,
213 IEEE80211_QUEUE_STOP_REASON_PS);
214 ieee80211_queue_work(&local->hw,
215 &local->dynamic_ps_disable_work);
216 }
217
218 mod_timer(&local->dynamic_ps_timer, jiffies +
219 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
220
221 return TX_CONTINUE;
222}
Johannes Berge2ebc742007-07-27 15:43:22 +0200223
Johannes Bergd9e8a702008-06-30 15:10:44 +0200224static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100225ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200226{
Harvey Harrison358c8d92008-07-15 18:44:13 -0700227
Johannes Berge039fa42008-05-15 12:55:29 +0200228 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200229 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berge2ebc742007-07-27 15:43:22 +0200230 u32 sta_flags;
231
Johannes Berge039fa42008-05-15 12:55:29 +0200232 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100233 return TX_CONTINUE;
Johannes Berg58d41852007-09-26 17:53:18 +0200234
Helmut Schaa142b9f52009-07-23 13:18:01 +0200235 if (unlikely(test_bit(SCAN_OFF_CHANNEL, &tx->local->scanning)) &&
Kalle Valoa9a6fff2009-03-18 14:06:44 +0200236 !ieee80211_is_probe_req(hdr->frame_control) &&
237 !ieee80211_is_nullfunc(hdr->frame_control))
238 /*
239 * When software scanning only nullfunc frames (to notify
240 * the sleep state to the AP) and probe requests (for the
241 * active scan) are allowed, all other frames should not be
242 * sent and we should not get here, but if we do
243 * nonetheless, drop them to avoid sending them
244 * off-channel. See the link below and
245 * ieee80211_start_scan() for more.
246 *
247 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
248 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100249 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200250
Johannes Berg05c914f2008-09-11 00:01:58 +0200251 if (tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +0100252 return TX_CONTINUE;
253
Johannes Berg5cf121c2008-02-25 16:27:43 +0100254 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
Johannes Berg9ae54c82008-01-31 19:48:20 +0100255 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200256
Johannes Berg07346f812008-05-03 01:02:02 +0200257 sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
Johannes Berge2ebc742007-07-27 15:43:22 +0200258
Johannes Berg5cf121c2008-02-25 16:27:43 +0100259 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200260 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200261 tx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
Harvey Harrison358c8d92008-07-15 18:44:13 -0700262 ieee80211_is_data(hdr->frame_control))) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200263#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
264 printk(KERN_DEBUG "%s: dropped data frame to not "
Johannes Berg0c68ae262008-10-27 15:56:10 -0700265 "associated station %pM\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100266 tx->sdata->name, hdr->addr1);
Johannes Berge2ebc742007-07-27 15:43:22 +0200267#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
268 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100269 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200270 }
271 } else {
Harvey Harrison358c8d92008-07-15 18:44:13 -0700272 if (unlikely(ieee80211_is_data(hdr->frame_control) &&
Johannes Berge2ebc742007-07-27 15:43:22 +0200273 tx->local->num_sta == 0 &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200274 tx->sdata->vif.type != NL80211_IFTYPE_ADHOC)) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200275 /*
276 * No associated STAs - no need to send multicast
277 * frames.
278 */
Johannes Berg9ae54c82008-01-31 19:48:20 +0100279 return TX_DROP;
Johannes Berge2ebc742007-07-27 15:43:22 +0200280 }
Johannes Berg9ae54c82008-01-31 19:48:20 +0100281 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200282 }
283
Johannes Berg9ae54c82008-01-31 19:48:20 +0100284 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200285}
286
Johannes Berge2ebc742007-07-27 15:43:22 +0200287/* This function is called whenever the AP is about to exceed the maximum limit
288 * of buffered frames for power saving STAs. This situation should not really
289 * happen often during normal operation, so dropping the oldest buffered packet
290 * from each queue should be OK to make some room for new frames. */
291static void purge_old_ps_buffers(struct ieee80211_local *local)
292{
293 int total = 0, purged = 0;
294 struct sk_buff *skb;
295 struct ieee80211_sub_if_data *sdata;
296 struct sta_info *sta;
297
Johannes Berg79010422007-09-18 17:29:21 -0400298 /*
299 * virtual interfaces are protected by RCU
300 */
301 rcu_read_lock();
302
303 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200304 struct ieee80211_if_ap *ap;
Johannes Berg05c914f2008-09-11 00:01:58 +0200305 if (sdata->vif.type != NL80211_IFTYPE_AP)
Johannes Berge2ebc742007-07-27 15:43:22 +0200306 continue;
307 ap = &sdata->u.ap;
308 skb = skb_dequeue(&ap->ps_bc_buf);
309 if (skb) {
310 purged++;
311 dev_kfree_skb(skb);
312 }
313 total += skb_queue_len(&ap->ps_bc_buf);
314 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200315
Johannes Bergd0709a62008-02-25 16:27:46 +0100316 list_for_each_entry_rcu(sta, &local->sta_list, list) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200317 skb = skb_dequeue(&sta->ps_tx_buf);
318 if (skb) {
319 purged++;
320 dev_kfree_skb(skb);
321 }
322 total += skb_queue_len(&sta->ps_tx_buf);
323 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100324
325 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +0200326
327 local->total_ps_buffered = total;
Rami Rosen54223992008-07-24 10:40:37 +0300328#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berge2ebc742007-07-27 15:43:22 +0200329 printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400330 wiphy_name(local->hw.wiphy), purged);
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200331#endif
Johannes Berge2ebc742007-07-27 15:43:22 +0200332}
333
Johannes Berg9ae54c82008-01-31 19:48:20 +0100334static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100335ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200336{
Johannes Berge039fa42008-05-15 12:55:29 +0200337 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700338 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +0200339
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100340 /*
341 * broadcast/multicast frame
342 *
343 * If any of the associated stations is in power save mode,
344 * the frame is buffered to be sent after DTIM beacon frame.
345 * This is done either by the hardware or us.
346 */
347
Johannes Berg3e122be2008-07-09 14:40:34 +0200348 /* powersaving STAs only in AP/VLAN mode */
349 if (!tx->sdata->bss)
350 return TX_CONTINUE;
351
352 /* no buffering for ordered frames */
Harvey Harrison358c8d92008-07-15 18:44:13 -0700353 if (ieee80211_has_order(hdr->frame_control))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100354 return TX_CONTINUE;
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100355
356 /* no stations in PS mode */
357 if (!atomic_read(&tx->sdata->bss->num_sta_ps))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100358 return TX_CONTINUE;
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100359
Johannes Berg62b517c2009-10-29 12:19:21 +0100360 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
Johannes Berg62b12082009-08-10 16:04:15 +0200361
Johannes Berg62b517c2009-10-29 12:19:21 +0100362 /* device releases frame after DTIM beacon */
363 if (!(tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING))
Johannes Berg62b12082009-08-10 16:04:15 +0200364 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200365
Johannes Berg62b12082009-08-10 16:04:15 +0200366 /* buffered in mac80211 */
367 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
368 purge_old_ps_buffers(tx->local);
Johannes Berg7d54d0d2007-12-19 01:31:25 +0100369
Johannes Berg62b12082009-08-10 16:04:15 +0200370 if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >= AP_MAX_BC_BUFFER) {
371#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
372 if (net_ratelimit())
373 printk(KERN_DEBUG "%s: BC TX buffer full - dropping the oldest frame\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100374 tx->sdata->name);
Johannes Berg62b12082009-08-10 16:04:15 +0200375#endif
376 dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
377 } else
378 tx->local->total_ps_buffered++;
379
380 skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
381
382 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200383}
384
Jouni Malinenfb733332009-01-08 13:32:00 +0200385static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
386 struct sk_buff *skb)
387{
388 if (!ieee80211_is_mgmt(fc))
389 return 0;
390
391 if (sta == NULL || !test_sta_flags(sta, WLAN_STA_MFP))
392 return 0;
393
394 if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *)
395 skb->data))
396 return 0;
397
398 return 1;
399}
400
Johannes Berg9ae54c82008-01-31 19:48:20 +0100401static ieee80211_tx_result
Johannes Berg5cf121c2008-02-25 16:27:43 +0100402ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200403{
404 struct sta_info *sta = tx->sta;
Johannes Berge039fa42008-05-15 12:55:29 +0200405 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700406 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Berg07346f812008-05-03 01:02:02 +0200407 u32 staflags;
Johannes Berge2ebc742007-07-27 15:43:22 +0200408
Joe Perchesf64f9e72009-11-29 16:55:45 -0800409 if (unlikely(!sta ||
410 ieee80211_is_probe_resp(hdr->frame_control) ||
411 ieee80211_is_auth(hdr->frame_control) ||
412 ieee80211_is_assoc_resp(hdr->frame_control) ||
413 ieee80211_is_reassoc_resp(hdr->frame_control)))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100414 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200415
Johannes Berg07346f812008-05-03 01:02:02 +0200416 staflags = get_sta_flags(sta);
417
Johannes Bergaf818582009-11-06 11:35:50 +0100418 if (unlikely((staflags & (WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) &&
Johannes Berg3fa52052009-07-24 13:23:09 +0200419 !(info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE))) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200420#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berg0c68ae262008-10-27 15:56:10 -0700421 printk(KERN_DEBUG "STA %pM aid %d: PS buffer (entries "
Johannes Berge2ebc742007-07-27 15:43:22 +0200422 "before %d)\n",
Johannes Berg0c68ae262008-10-27 15:56:10 -0700423 sta->sta.addr, sta->sta.aid,
Johannes Berge2ebc742007-07-27 15:43:22 +0200424 skb_queue_len(&sta->ps_tx_buf));
425#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berge2ebc742007-07-27 15:43:22 +0200426 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
427 purge_old_ps_buffers(tx->local);
428 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
429 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
Rami Rosen54223992008-07-24 10:40:37 +0300430#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Berge2ebc742007-07-27 15:43:22 +0200431 if (net_ratelimit()) {
Johannes Berg0c68ae262008-10-27 15:56:10 -0700432 printk(KERN_DEBUG "%s: STA %pM TX "
Johannes Berge2ebc742007-07-27 15:43:22 +0200433 "buffer full - dropping oldest frame\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100434 tx->sdata->name, sta->sta.addr);
Johannes Berge2ebc742007-07-27 15:43:22 +0200435 }
Johannes Bergf4ea83d2008-06-30 15:10:46 +0200436#endif
Johannes Berge2ebc742007-07-27 15:43:22 +0200437 dev_kfree_skb(old);
438 } else
439 tx->local->total_ps_buffered++;
Johannes Berg004c8722008-02-20 11:21:35 +0100440
Johannes Bergaf818582009-11-06 11:35:50 +0100441 /*
442 * Queue frame to be sent after STA wakes up/polls,
443 * but don't set the TIM bit if the driver is blocking
444 * wakeup or poll response transmissions anyway.
445 */
446 if (skb_queue_empty(&sta->ps_tx_buf) &&
447 !(staflags & WLAN_STA_PS_DRIVER))
Johannes Berg004c8722008-02-20 11:21:35 +0100448 sta_info_set_tim_bit(sta);
449
Johannes Berge039fa42008-05-15 12:55:29 +0200450 info->control.jiffies = jiffies;
Johannes Berg5061b0c2009-07-14 00:33:34 +0200451 info->control.vif = &tx->sdata->vif;
Johannes Berg8f77f382009-06-07 21:58:37 +0200452 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
Johannes Berge2ebc742007-07-27 15:43:22 +0200453 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100454 return TX_QUEUED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200455 }
456#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
Johannes Bergaf818582009-11-06 11:35:50 +0100457 else if (unlikely(staflags & WLAN_STA_PS_STA)) {
Johannes Berg0c68ae262008-10-27 15:56:10 -0700458 printk(KERN_DEBUG "%s: STA %pM in PS mode, but pspoll "
Johannes Berg47846c92009-11-25 17:46:19 +0100459 "set -> send frame\n", tx->sdata->name,
Johannes Berg0c68ae262008-10-27 15:56:10 -0700460 sta->sta.addr);
Johannes Berge2ebc742007-07-27 15:43:22 +0200461 }
462#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
Johannes Berge2ebc742007-07-27 15:43:22 +0200463
Johannes Berg9ae54c82008-01-31 19:48:20 +0100464 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200465}
466
Johannes Bergd9e8a702008-06-30 15:10:44 +0200467static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100468ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200469{
Johannes Berg5cf121c2008-02-25 16:27:43 +0100470 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
Johannes Berg9ae54c82008-01-31 19:48:20 +0100471 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200472
Johannes Berg5cf121c2008-02-25 16:27:43 +0100473 if (tx->flags & IEEE80211_TX_UNICAST)
Johannes Berge2ebc742007-07-27 15:43:22 +0200474 return ieee80211_tx_h_unicast_ps_buf(tx);
475 else
476 return ieee80211_tx_h_multicast_ps_buf(tx);
477}
478
Johannes Bergd9e8a702008-06-30 15:10:44 +0200479static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100480ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200481{
John W. Linvilled3feaf52009-04-24 15:35:42 -0400482 struct ieee80211_key *key = NULL;
Johannes Berge039fa42008-05-15 12:55:29 +0200483 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Harvey Harrison358c8d92008-07-15 18:44:13 -0700484 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400485
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200486 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
Johannes Berge2ebc742007-07-27 15:43:22 +0200487 tx->key = NULL;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400488 else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
489 tx->key = key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200490 else if (ieee80211_is_mgmt(hdr->frame_control) &&
491 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
492 tx->key = key;
Johannes Bergd4e46a32007-09-14 11:10:24 -0400493 else if ((key = rcu_dereference(tx->sdata->default_key)))
494 tx->key = key;
Johannes Berge2ebc742007-07-27 15:43:22 +0200495 else if (tx->sdata->drop_unencrypted &&
Johannes Bergd0f09802008-07-29 11:32:07 +0200496 (tx->skb->protocol != cpu_to_be16(ETH_P_PAE)) &&
Jouni Malinene0463f52009-01-19 16:52:00 +0200497 !(info->flags & IEEE80211_TX_CTL_INJECTED) &&
498 (!ieee80211_is_robust_mgmt_frame(hdr) ||
499 (ieee80211_is_action(hdr->frame_control) &&
500 tx->sta && test_sta_flags(tx->sta, WLAN_STA_MFP)))) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200501 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
Johannes Berg9ae54c82008-01-31 19:48:20 +0100502 return TX_DROP;
Johannes Berg176e4f82007-12-18 15:27:47 +0100503 } else
Johannes Berge2ebc742007-07-27 15:43:22 +0200504 tx->key = NULL;
505
506 if (tx->key) {
507 tx->key->tx_rx_count++;
Johannes Berg011bfcc2007-09-17 01:29:25 -0400508 /* TODO: add threshold stuff again */
Johannes Berg176e4f82007-12-18 15:27:47 +0100509
510 switch (tx->key->conf.alg) {
511 case ALG_WEP:
Harvey Harrison358c8d92008-07-15 18:44:13 -0700512 if (ieee80211_is_auth(hdr->frame_control))
Johannes Berg176e4f82007-12-18 15:27:47 +0100513 break;
514 case ALG_TKIP:
Harvey Harrison358c8d92008-07-15 18:44:13 -0700515 if (!ieee80211_is_data_present(hdr->frame_control))
Johannes Berg176e4f82007-12-18 15:27:47 +0100516 tx->key = NULL;
517 break;
Jouni Malinenfb733332009-01-08 13:32:00 +0200518 case ALG_CCMP:
519 if (!ieee80211_is_data_present(hdr->frame_control) &&
520 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
521 tx->skb))
522 tx->key = NULL;
523 break;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200524 case ALG_AES_CMAC:
525 if (!ieee80211_is_mgmt(hdr->frame_control))
526 tx->key = NULL;
527 break;
Johannes Berg176e4f82007-12-18 15:27:47 +0100528 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200529 }
530
Johannes Berg176e4f82007-12-18 15:27:47 +0100531 if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200532 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg176e4f82007-12-18 15:27:47 +0100533
Johannes Berg9ae54c82008-01-31 19:48:20 +0100534 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200535}
536
Johannes Bergd9e8a702008-06-30 15:10:44 +0200537static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100538ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200539{
Johannes Berge039fa42008-05-15 12:55:29 +0200540 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200541 struct ieee80211_hdr *hdr = (void *)tx->skb->data;
542 struct ieee80211_supported_band *sband;
543 struct ieee80211_rate *rate;
544 int i, len;
545 bool inval = false, rts = false, short_preamble = false;
546 struct ieee80211_tx_rate_control txrc;
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700547 u32 sta_flags;
Johannes Berge6a98542008-10-21 12:40:02 +0200548
549 memset(&txrc, 0, sizeof(txrc));
Johannes Berg8318d782008-01-24 19:38:38 +0100550
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200551 sband = tx->local->hw.wiphy->bands[tx->channel->band];
Johannes Berge2ebc742007-07-27 15:43:22 +0200552
Johannes Berge6a98542008-10-21 12:40:02 +0200553 len = min_t(int, tx->skb->len + FCS_LEN,
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200554 tx->local->hw.wiphy->frag_threshold);
Johannes Berg58d41852007-09-26 17:53:18 +0200555
Johannes Berge6a98542008-10-21 12:40:02 +0200556 /* set up the tx rate control struct we give the RC algo */
557 txrc.hw = local_to_hw(tx->local);
558 txrc.sband = sband;
559 txrc.bss_conf = &tx->sdata->vif.bss_conf;
560 txrc.skb = tx->skb;
561 txrc.reported_rate.idx = -1;
Jouni Malinen37eb0b12010-01-06 13:09:08 +0200562 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[tx->channel->band];
563 if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
564 txrc.max_rate_idx = -1;
565 else
566 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
Jouni Malinene00cfce2009-12-29 12:59:19 +0200567 txrc.ap = tx->sdata->vif.type == NL80211_IFTYPE_AP;
Johannes Berg58d41852007-09-26 17:53:18 +0200568
Johannes Berge6a98542008-10-21 12:40:02 +0200569 /* set up RTS protection if desired */
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200570 if (len > tx->local->hw.wiphy->rts_threshold) {
Johannes Berge6a98542008-10-21 12:40:02 +0200571 txrc.rts = rts = true;
Johannes Berge2ebc742007-07-27 15:43:22 +0200572 }
Johannes Berge6a98542008-10-21 12:40:02 +0200573
574 /*
575 * Use short preamble if the BSS can handle it, but not for
576 * management frames unless we know the receiver can handle
577 * that -- the management frame might be to a station that
578 * just wants a probe response.
579 */
580 if (tx->sdata->vif.bss_conf.use_short_preamble &&
581 (ieee80211_is_data(hdr->frame_control) ||
582 (tx->sta && test_sta_flags(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
583 txrc.short_preamble = short_preamble = true;
584
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700585 sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
Johannes Berge6a98542008-10-21 12:40:02 +0200586
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700587 /*
588 * Lets not bother rate control if we're associated and cannot
589 * talk to the sta. This should not happen.
590 */
Helmut Schaafbe9c422009-07-23 12:14:04 +0200591 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) &&
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700592 (sta_flags & WLAN_STA_ASSOC) &&
593 !rate_usable_index_exists(sband, &tx->sta->sta),
594 "%s: Dropped data frame as no usable bitrate found while "
595 "scanning and associated. Target station: "
596 "%pM on %d GHz band\n",
Johannes Berg47846c92009-11-25 17:46:19 +0100597 tx->sdata->name, hdr->addr1,
Luis R. Rodriguezb770b432009-07-16 10:15:09 -0700598 tx->channel->band ? 5 : 2))
599 return TX_DROP;
600
601 /*
602 * If we're associated with the sta at this point we know we can at
603 * least send the frame at the lowest bit rate.
604 */
Johannes Berge6a98542008-10-21 12:40:02 +0200605 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
606
607 if (unlikely(info->control.rates[0].idx < 0))
608 return TX_DROP;
609
610 if (txrc.reported_rate.idx < 0)
611 txrc.reported_rate = info->control.rates[0];
612
613 if (tx->sta)
614 tx->sta->last_tx_rate = txrc.reported_rate;
615
616 if (unlikely(!info->control.rates[0].count))
617 info->control.rates[0].count = 1;
618
Gábor Stefanik99551512009-04-23 19:36:14 +0200619 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
620 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
621 info->control.rates[0].count = 1;
622
Johannes Berge6a98542008-10-21 12:40:02 +0200623 if (is_multicast_ether_addr(hdr->addr1)) {
624 /*
625 * XXX: verify the rate is in the basic rateset
626 */
627 return TX_CONTINUE;
628 }
629
630 /*
631 * set up the RTS/CTS rate as the fastest basic rate
632 * that is not faster than the data rate
633 *
634 * XXX: Should this check all retry rates?
635 */
636 if (!(info->control.rates[0].flags & IEEE80211_TX_RC_MCS)) {
637 s8 baserate = 0;
638
639 rate = &sband->bitrates[info->control.rates[0].idx];
640
641 for (i = 0; i < sband->n_bitrates; i++) {
642 /* must be a basic rate */
643 if (!(tx->sdata->vif.bss_conf.basic_rates & BIT(i)))
644 continue;
645 /* must not be faster than the data rate */
646 if (sband->bitrates[i].bitrate > rate->bitrate)
647 continue;
648 /* maximum */
649 if (sband->bitrates[baserate].bitrate <
650 sband->bitrates[i].bitrate)
651 baserate = i;
652 }
653
654 info->control.rts_cts_rate_idx = baserate;
655 }
656
657 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
658 /*
659 * make sure there's no valid rate following
660 * an invalid one, just in case drivers don't
661 * take the API seriously to stop at -1.
662 */
663 if (inval) {
664 info->control.rates[i].idx = -1;
665 continue;
666 }
667 if (info->control.rates[i].idx < 0) {
668 inval = true;
669 continue;
670 }
671
672 /*
673 * For now assume MCS is already set up correctly, this
674 * needs to be fixed.
675 */
676 if (info->control.rates[i].flags & IEEE80211_TX_RC_MCS) {
677 WARN_ON(info->control.rates[i].idx > 76);
678 continue;
679 }
680
681 /* set up RTS protection if desired */
682 if (rts)
683 info->control.rates[i].flags |=
684 IEEE80211_TX_RC_USE_RTS_CTS;
685
686 /* RC is busted */
Sujith075cbc92008-10-23 12:14:02 +0530687 if (WARN_ON_ONCE(info->control.rates[i].idx >=
688 sband->n_bitrates)) {
Johannes Berge6a98542008-10-21 12:40:02 +0200689 info->control.rates[i].idx = -1;
690 continue;
691 }
692
693 rate = &sband->bitrates[info->control.rates[i].idx];
694
695 /* set up short preamble */
696 if (short_preamble &&
697 rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
698 info->control.rates[i].flags |=
699 IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
700
701 /* set up G protection */
702 if (!rts && tx->sdata->vif.bss_conf.use_cts_prot &&
703 rate->flags & IEEE80211_RATE_ERP_G)
704 info->control.rates[i].flags |=
705 IEEE80211_TX_RC_USE_CTS_PROTECT;
706 }
Johannes Berge2ebc742007-07-27 15:43:22 +0200707
Johannes Berg9ae54c82008-01-31 19:48:20 +0100708 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200709}
710
Johannes Bergd9e8a702008-06-30 15:10:44 +0200711static ieee80211_tx_result debug_noinline
Johannes Berg5cf121c2008-02-25 16:27:43 +0100712ieee80211_tx_h_misc(struct ieee80211_tx_data *tx)
Johannes Berge2ebc742007-07-27 15:43:22 +0200713{
Johannes Berge039fa42008-05-15 12:55:29 +0200714 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
Johannes Berge2ebc742007-07-27 15:43:22 +0200715
Johannes Berge2454942008-05-15 12:55:28 +0200716 if (tx->sta)
Johannes Berg17741cd2008-09-11 00:02:02 +0200717 info->control.sta = &tx->sta->sta;
Johannes Berge2ebc742007-07-27 15:43:22 +0200718
Johannes Berg9ae54c82008-01-31 19:48:20 +0100719 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +0200720}
721
Johannes Bergd9e8a702008-06-30 15:10:44 +0200722static ieee80211_tx_result debug_noinline
Johannes Bergf591fa52008-07-10 11:21:26 +0200723ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
724{
725 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
726 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
727 u16 *seq;
728 u8 *qc;
729 int tid;
730
Johannes Berg25d834e2008-09-12 22:52:47 +0200731 /*
732 * Packet injection may want to control the sequence
733 * number, if we have no matching interface then we
734 * neither assign one ourselves nor ask the driver to.
735 */
Johannes Berg5061b0c2009-07-14 00:33:34 +0200736 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
Johannes Berg25d834e2008-09-12 22:52:47 +0200737 return TX_CONTINUE;
738
Johannes Bergf591fa52008-07-10 11:21:26 +0200739 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
740 return TX_CONTINUE;
741
742 if (ieee80211_hdrlen(hdr->frame_control) < 24)
743 return TX_CONTINUE;
744
Johannes Berg94778282008-10-10 13:21:59 +0200745 /*
746 * Anything but QoS data that has a sequence number field
747 * (is long enough) gets a sequence number from the global
748 * counter.
749 */
Johannes Bergf591fa52008-07-10 11:21:26 +0200750 if (!ieee80211_is_data_qos(hdr->frame_control)) {
Johannes Berg94778282008-10-10 13:21:59 +0200751 /* driver should assign sequence number */
Johannes Bergf591fa52008-07-10 11:21:26 +0200752 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
Johannes Berg94778282008-10-10 13:21:59 +0200753 /* for pure STA mode without beacons, we can do it */
754 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
755 tx->sdata->sequence_number += 0x10;
Johannes Bergf591fa52008-07-10 11:21:26 +0200756 return TX_CONTINUE;
757 }
758
759 /*
760 * This should be true for injected/management frames only, for
761 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
762 * above since they are not QoS-data frames.
763 */
764 if (!tx->sta)
765 return TX_CONTINUE;
766
767 /* include per-STA, per-TID sequence counter */
768
769 qc = ieee80211_get_qos_ctl(hdr);
770 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
771 seq = &tx->sta->tid_seq[tid];
772
773 hdr->seq_ctrl = cpu_to_le16(*seq);
774
775 /* Increase the sequence number. */
776 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
777
778 return TX_CONTINUE;
779}
780
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100781static int ieee80211_fragment(struct ieee80211_local *local,
782 struct sk_buff *skb, int hdrlen,
783 int frag_threshold)
784{
785 struct sk_buff *tail = skb, *tmp;
786 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
787 int pos = hdrlen + per_fragm;
788 int rem = skb->len - hdrlen - per_fragm;
789
790 if (WARN_ON(rem < 0))
791 return -EINVAL;
792
793 while (rem) {
794 int fraglen = per_fragm;
795
796 if (fraglen > rem)
797 fraglen = rem;
798 rem -= fraglen;
799 tmp = dev_alloc_skb(local->tx_headroom +
800 frag_threshold +
801 IEEE80211_ENCRYPT_HEADROOM +
802 IEEE80211_ENCRYPT_TAILROOM);
803 if (!tmp)
804 return -ENOMEM;
805 tail->next = tmp;
806 tail = tmp;
807 skb_reserve(tmp, local->tx_headroom +
808 IEEE80211_ENCRYPT_HEADROOM);
809 /* copy control information */
810 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
811 skb_copy_queue_mapping(tmp, skb);
812 tmp->priority = skb->priority;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100813 tmp->dev = skb->dev;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100814
815 /* copy header and data */
816 memcpy(skb_put(tmp, hdrlen), skb->data, hdrlen);
817 memcpy(skb_put(tmp, fraglen), skb->data + pos, fraglen);
818
819 pos += fraglen;
820 }
821
822 skb->len = hdrlen + per_fragm;
823 return 0;
824}
825
Johannes Bergf591fa52008-07-10 11:21:26 +0200826static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +0200827ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
828{
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100829 struct sk_buff *skb = tx->skb;
830 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
831 struct ieee80211_hdr *hdr = (void *)skb->data;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200832 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100833 int hdrlen;
834 int fragnum;
Johannes Berge2454942008-05-15 12:55:28 +0200835
836 if (!(tx->flags & IEEE80211_TX_FRAGMENTED))
837 return TX_CONTINUE;
838
Johannes Bergeefce912008-05-17 00:57:13 +0200839 /*
840 * Warn when submitting a fragmented A-MPDU frame and drop it.
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200841 * This scenario is handled in ieee80211_tx_prepare but extra
Ron Rindjunsky8d5e0d52008-06-12 15:42:29 +0300842 * caution taken here as fragmented ampdu may cause Tx stop.
Johannes Bergeefce912008-05-17 00:57:13 +0200843 */
Sujith8b30b1f2008-10-24 09:55:27 +0530844 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
Johannes Bergeefce912008-05-17 00:57:13 +0200845 return TX_DROP;
846
Harvey Harrison065e96052008-06-22 16:45:27 -0700847 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berge2454942008-05-15 12:55:28 +0200848
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100849 /* internal error, why is TX_FRAGMENTED set? */
Johannes Berg8ccd8f22009-04-29 23:35:56 +0200850 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100851 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200852
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100853 /*
854 * Now fragment the frame. This will allocate all the fragments and
855 * chain them (using skb as the first fragment) to skb->next.
856 * During transmission, we will remove the successfully transmitted
857 * fragments from this list. When the low-level driver rejects one
858 * of the fragments then we will simply pretend to accept the skb
859 * but store it away as pending.
860 */
861 if (ieee80211_fragment(tx->local, skb, hdrlen, frag_threshold))
862 return TX_DROP;
Johannes Berge2454942008-05-15 12:55:28 +0200863
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100864 /* update duration/seq/flags of fragments */
865 fragnum = 0;
866 do {
867 int next_len;
868 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
Johannes Berge2454942008-05-15 12:55:28 +0200869
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100870 hdr = (void *)skb->data;
871 info = IEEE80211_SKB_CB(skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200872
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100873 if (skb->next) {
874 hdr->frame_control |= morefrags;
875 next_len = skb->next->len;
Johannes Berge6a98542008-10-21 12:40:02 +0200876 /*
877 * No multi-rate retries for fragmented frames, that
878 * would completely throw off the NAV at other STAs.
879 */
880 info->control.rates[1].idx = -1;
881 info->control.rates[2].idx = -1;
882 info->control.rates[3].idx = -1;
883 info->control.rates[4].idx = -1;
884 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 5);
885 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100886 } else {
887 hdr->frame_control &= ~morefrags;
888 next_len = 0;
Johannes Berge6a98542008-10-21 12:40:02 +0200889 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100890 hdr->duration_id = ieee80211_duration(tx, 0, next_len);
891 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
892 fragnum++;
893 } while ((skb = skb->next));
Johannes Berge2454942008-05-15 12:55:28 +0200894
895 return TX_CONTINUE;
Johannes Berge2454942008-05-15 12:55:28 +0200896}
897
Johannes Bergd9e8a702008-06-30 15:10:44 +0200898static ieee80211_tx_result debug_noinline
Johannes Bergfeff1f22009-08-10 16:01:54 +0200899ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
900{
901 struct sk_buff *skb = tx->skb;
902
903 if (!tx->sta)
904 return TX_CONTINUE;
905
906 tx->sta->tx_packets++;
907 do {
908 tx->sta->tx_fragments++;
909 tx->sta->tx_bytes += skb->len;
910 } while ((skb = skb->next));
911
912 return TX_CONTINUE;
913}
914
915static ieee80211_tx_result debug_noinline
Johannes Berge2454942008-05-15 12:55:28 +0200916ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
917{
918 if (!tx->key)
919 return TX_CONTINUE;
920
921 switch (tx->key->conf.alg) {
922 case ALG_WEP:
923 return ieee80211_crypto_wep_encrypt(tx);
924 case ALG_TKIP:
925 return ieee80211_crypto_tkip_encrypt(tx);
926 case ALG_CCMP:
927 return ieee80211_crypto_ccmp_encrypt(tx);
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200928 case ALG_AES_CMAC:
929 return ieee80211_crypto_aes_cmac_encrypt(tx);
Johannes Berge2454942008-05-15 12:55:28 +0200930 }
931
932 /* not reached */
933 WARN_ON(1);
934 return TX_DROP;
935}
936
Johannes Bergd9e8a702008-06-30 15:10:44 +0200937static ieee80211_tx_result debug_noinline
Johannes Berg03f93c32008-06-25 14:36:27 +0300938ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
939{
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100940 struct sk_buff *skb = tx->skb;
941 struct ieee80211_hdr *hdr;
942 int next_len;
943 bool group_addr;
Johannes Berg03f93c32008-06-25 14:36:27 +0300944
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100945 do {
946 hdr = (void *) skb->data;
Jouni Malinen7e0aae42009-05-19 19:25:58 +0300947 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
948 break; /* must not overwrite AID */
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100949 next_len = skb->next ? skb->next->len : 0;
950 group_addr = is_multicast_ether_addr(hdr->addr1);
Johannes Berg03f93c32008-06-25 14:36:27 +0300951
Johannes Berg2de8e0d2009-03-23 17:28:35 +0100952 hdr->duration_id =
953 ieee80211_duration(tx, group_addr, next_len);
954 } while ((skb = skb->next));
Johannes Berg03f93c32008-06-25 14:36:27 +0300955
956 return TX_CONTINUE;
957}
958
Johannes Berge2ebc742007-07-27 15:43:22 +0200959/* actual transmit path */
960
961/*
962 * deal with packet injection down monitor interface
963 * with Radiotap Header -- only called for monitor mode interface
964 */
Johannes Berg27004b12009-06-09 19:48:25 +0200965static bool __ieee80211_parse_tx_radiotap(struct ieee80211_tx_data *tx,
966 struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +0200967{
968 /*
969 * this is the moment to interpret and discard the radiotap header that
970 * must be at the start of the packet injected in Monitor mode
971 *
972 * Need to take some care with endian-ness since radiotap
973 * args are little-endian
974 */
975
976 struct ieee80211_radiotap_iterator iterator;
977 struct ieee80211_radiotap_header *rthdr =
978 (struct ieee80211_radiotap_header *) skb->data;
Johannes Berg8318d782008-01-24 19:38:38 +0100979 struct ieee80211_supported_band *sband;
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200980 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +0200981 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
982
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200983 sband = tx->local->hw.wiphy->bands[tx->channel->band];
Johannes Berg8318d782008-01-24 19:38:38 +0100984
Johannes Berg3b8d81e02009-06-17 17:43:56 +0200985 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg5cf121c2008-02-25 16:27:43 +0100986 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +0200987
988 /*
989 * for every radiotap entry that is present
990 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
991 * entries present, or -EINVAL on error)
992 */
993
994 while (!ret) {
Johannes Berge2ebc742007-07-27 15:43:22 +0200995 ret = ieee80211_radiotap_iterator_next(&iterator);
996
997 if (ret)
998 continue;
999
1000 /* see if this argument is something we can use */
1001 switch (iterator.this_arg_index) {
1002 /*
1003 * You must take care when dereferencing iterator.this_arg
1004 * for multibyte types... the pointer is not aligned. Use
1005 * get_unaligned((type *)iterator.this_arg) to dereference
1006 * iterator.this_arg for type "type" safely on all arches.
1007 */
Johannes Berge2ebc742007-07-27 15:43:22 +02001008 case IEEE80211_RADIOTAP_FLAGS:
1009 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
1010 /*
1011 * this indicates that the skb we have been
1012 * handed has the 32-bit FCS CRC at the end...
1013 * we should react to that by snipping it off
1014 * because it will be recomputed and added
1015 * on transmission
1016 */
1017 if (skb->len < (iterator.max_length + FCS_LEN))
Johannes Berg27004b12009-06-09 19:48:25 +02001018 return false;
Johannes Berge2ebc742007-07-27 15:43:22 +02001019
1020 skb_trim(skb, skb->len - FCS_LEN);
1021 }
Johannes Berg58d41852007-09-26 17:53:18 +02001022 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001023 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
Johannes Berg58d41852007-09-26 17:53:18 +02001024 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
Johannes Berg5cf121c2008-02-25 16:27:43 +01001025 tx->flags |= IEEE80211_TX_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +02001026 break;
1027
Johannes Berg58d41852007-09-26 17:53:18 +02001028 /*
1029 * Please update the file
1030 * Documentation/networking/mac80211-injection.txt
1031 * when parsing new fields here.
1032 */
1033
Johannes Berge2ebc742007-07-27 15:43:22 +02001034 default:
1035 break;
1036 }
1037 }
1038
1039 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
Johannes Berg27004b12009-06-09 19:48:25 +02001040 return false;
Johannes Berge2ebc742007-07-27 15:43:22 +02001041
1042 /*
1043 * remove the radiotap header
1044 * iterator->max_length was sanity-checked against
1045 * skb->len by iterator init
1046 */
1047 skb_pull(skb, iterator.max_length);
1048
Johannes Berg27004b12009-06-09 19:48:25 +02001049 return true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001050}
1051
Johannes Berg58d41852007-09-26 17:53:18 +02001052/*
1053 * initialises @tx
1054 */
Johannes Berg9ae54c82008-01-31 19:48:20 +01001055static ieee80211_tx_result
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001056ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1057 struct ieee80211_tx_data *tx,
1058 struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001059{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001060 struct ieee80211_local *local = sdata->local;
Johannes Berg58d41852007-09-26 17:53:18 +02001061 struct ieee80211_hdr *hdr;
Johannes Berge039fa42008-05-15 12:55:29 +02001062 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Sujith8b30b1f2008-10-24 09:55:27 +05301063 int hdrlen, tid;
1064 u8 *qc, *state;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001065 bool queued = false;
Johannes Berge2ebc742007-07-27 15:43:22 +02001066
1067 memset(tx, 0, sizeof(*tx));
1068 tx->skb = skb;
Johannes Berge2ebc742007-07-27 15:43:22 +02001069 tx->local = local;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001070 tx->sdata = sdata;
Johannes Berge039fa42008-05-15 12:55:29 +02001071 tx->channel = local->hw.conf.channel;
Johannes Berge2ebc742007-07-27 15:43:22 +02001072 /*
Johannes Berg58d41852007-09-26 17:53:18 +02001073 * Set this flag (used below to indicate "automatic fragmentation"),
1074 * it will be cleared/left by radiotap as desired.
Johannes Berge2ebc742007-07-27 15:43:22 +02001075 */
Johannes Berg5cf121c2008-02-25 16:27:43 +01001076 tx->flags |= IEEE80211_TX_FRAGMENTED;
Johannes Berge2ebc742007-07-27 15:43:22 +02001077
1078 /* process and remove the injection radiotap header */
Johannes Berg25d834e2008-09-12 22:52:47 +02001079 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED)) {
Johannes Berg27004b12009-06-09 19:48:25 +02001080 if (!__ieee80211_parse_tx_radiotap(tx, skb))
Johannes Berg9ae54c82008-01-31 19:48:20 +01001081 return TX_DROP;
Johannes Berg58d41852007-09-26 17:53:18 +02001082
Johannes Berge2ebc742007-07-27 15:43:22 +02001083 /*
Johannes Berg58d41852007-09-26 17:53:18 +02001084 * __ieee80211_parse_tx_radiotap has now removed
1085 * the radiotap header that was present and pre-filled
1086 * 'tx' with tx control information.
Johannes Berge2ebc742007-07-27 15:43:22 +02001087 */
Johannes Berge2ebc742007-07-27 15:43:22 +02001088 }
1089
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001090 /*
1091 * If this flag is set to true anywhere, and we get here,
1092 * we are doing the needed processing, so remove the flag
1093 * now.
1094 */
1095 info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1096
Johannes Berg58d41852007-09-26 17:53:18 +02001097 hdr = (struct ieee80211_hdr *) skb->data;
1098
Felix Fietkau3f0e0b22010-01-08 18:15:13 +01001099 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001100 tx->sta = rcu_dereference(sdata->u.vlan.sta);
Felix Fietkau3f0e0b22010-01-08 18:15:13 +01001101 if (!tx->sta && sdata->dev->ieee80211_ptr->use_4addr)
1102 return TX_DROP;
1103 }
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001104 if (!tx->sta)
Johannes Bergabe60632009-11-25 17:46:18 +01001105 tx->sta = sta_info_get(sdata, hdr->addr1);
Johannes Berg58d41852007-09-26 17:53:18 +02001106
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001107 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
1108 (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION)) {
Johannes Berg96f5e662009-02-12 00:51:53 +01001109 unsigned long flags;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001110 struct tid_ampdu_tx *tid_tx;
1111
Sujith8b30b1f2008-10-24 09:55:27 +05301112 qc = ieee80211_get_qos_ctl(hdr);
1113 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
1114
Johannes Berg96f5e662009-02-12 00:51:53 +01001115 spin_lock_irqsave(&tx->sta->lock, flags);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001116 /*
1117 * XXX: This spinlock could be fairly expensive, but see the
1118 * comment in agg-tx.c:ieee80211_agg_tx_operational().
1119 * One way to solve this would be to do something RCU-like
1120 * for managing the tid_tx struct and using atomic bitops
1121 * for the actual state -- by introducing an actual
1122 * 'operational' bit that would be possible. It would
1123 * require changing ieee80211_agg_tx_operational() to
1124 * set that bit, and changing the way tid_tx is managed
1125 * everywhere, including races between that bit and
1126 * tid_tx going away (tid_tx being added can be easily
1127 * committed to memory before the 'operational' bit).
1128 */
1129 tid_tx = tx->sta->ampdu_mlme.tid_tx[tid];
Sujith8b30b1f2008-10-24 09:55:27 +05301130 state = &tx->sta->ampdu_mlme.tid_state_tx[tid];
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001131 if (*state == HT_AGG_STATE_OPERATIONAL) {
Sujith8b30b1f2008-10-24 09:55:27 +05301132 info->flags |= IEEE80211_TX_CTL_AMPDU;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001133 } else if (*state != HT_AGG_STATE_IDLE) {
1134 /* in progress */
1135 queued = true;
Johannes Berg5061b0c2009-07-14 00:33:34 +02001136 info->control.vif = &sdata->vif;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001137 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1138 __skb_queue_tail(&tid_tx->pending, skb);
1139 }
Johannes Berg96f5e662009-02-12 00:51:53 +01001140 spin_unlock_irqrestore(&tx->sta->lock, flags);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001141
1142 if (unlikely(queued))
1143 return TX_QUEUED;
Sujith8b30b1f2008-10-24 09:55:27 +05301144 }
1145
Jiri Slabybadffb72007-08-28 17:01:54 -04001146 if (is_multicast_ether_addr(hdr->addr1)) {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001147 tx->flags &= ~IEEE80211_TX_UNICAST;
Johannes Berge039fa42008-05-15 12:55:29 +02001148 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Jiri Slabybadffb72007-08-28 17:01:54 -04001149 } else {
Johannes Berg5cf121c2008-02-25 16:27:43 +01001150 tx->flags |= IEEE80211_TX_UNICAST;
Johannes Bergd3707d92009-05-12 22:05:40 +02001151 if (unlikely(local->wifi_wme_noack_test))
1152 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1153 else
1154 info->flags &= ~IEEE80211_TX_CTL_NO_ACK;
Jiri Slabybadffb72007-08-28 17:01:54 -04001155 }
Johannes Berg58d41852007-09-26 17:53:18 +02001156
Johannes Berg5cf121c2008-02-25 16:27:43 +01001157 if (tx->flags & IEEE80211_TX_FRAGMENTED) {
1158 if ((tx->flags & IEEE80211_TX_UNICAST) &&
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001159 skb->len + FCS_LEN > local->hw.wiphy->frag_threshold &&
Ron Rindjunsky8d5e0d52008-06-12 15:42:29 +03001160 !(info->flags & IEEE80211_TX_CTL_AMPDU))
Johannes Berg5cf121c2008-02-25 16:27:43 +01001161 tx->flags |= IEEE80211_TX_FRAGMENTED;
Johannes Berg58d41852007-09-26 17:53:18 +02001162 else
Johannes Berg5cf121c2008-02-25 16:27:43 +01001163 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
Johannes Berg58d41852007-09-26 17:53:18 +02001164 }
1165
Johannes Berge2ebc742007-07-27 15:43:22 +02001166 if (!tx->sta)
Johannes Berge039fa42008-05-15 12:55:29 +02001167 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Johannes Berg07346f812008-05-03 01:02:02 +02001168 else if (test_and_clear_sta_flags(tx->sta, WLAN_STA_CLEAR_PS_FILT))
Johannes Berge039fa42008-05-15 12:55:29 +02001169 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
Johannes Berg58d41852007-09-26 17:53:18 +02001170
Harvey Harrisonb73d70a2008-07-15 18:44:12 -07001171 hdrlen = ieee80211_hdrlen(hdr->frame_control);
Johannes Berge2ebc742007-07-27 15:43:22 +02001172 if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
1173 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
1174 tx->ethertype = (pos[0] << 8) | pos[1];
1175 }
Johannes Berge039fa42008-05-15 12:55:29 +02001176 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
Johannes Berge2ebc742007-07-27 15:43:22 +02001177
Johannes Berg9ae54c82008-01-31 19:48:20 +01001178 return TX_CONTINUE;
Johannes Berge2ebc742007-07-27 15:43:22 +02001179}
1180
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001181static int __ieee80211_tx(struct ieee80211_local *local,
Johannes Berg1870cd72009-03-23 17:28:38 +01001182 struct sk_buff **skbp,
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001183 struct sta_info *sta,
1184 bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001185{
Johannes Berg1870cd72009-03-23 17:28:38 +01001186 struct sk_buff *skb = *skbp, *next;
Tomas Winklerf8e79dd2008-07-24 18:46:44 +03001187 struct ieee80211_tx_info *info;
Johannes Berg5061b0c2009-07-14 00:33:34 +02001188 struct ieee80211_sub_if_data *sdata;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001189 unsigned long flags;
Johannes Berga2208582009-03-23 17:28:40 +01001190 int ret, len;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001191 bool fragm = false;
Johannes Berge2ebc742007-07-27 15:43:22 +02001192
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001193 while (skb) {
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001194 int q = skb_get_queue_mapping(skb);
1195
1196 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1197 ret = IEEE80211_TX_OK;
1198 if (local->queue_stop_reasons[q] ||
1199 (!txpending && !skb_queue_empty(&local->pending[q])))
1200 ret = IEEE80211_TX_PENDING;
1201 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1202 if (ret != IEEE80211_TX_OK)
1203 return ret;
Tomas Winklerf8e79dd2008-07-24 18:46:44 +03001204
Johannes Bergf0e72852009-03-23 17:28:36 +01001205 info = IEEE80211_SKB_CB(skb);
1206
1207 if (fragm)
Johannes Berge6a98542008-10-21 12:40:02 +02001208 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
Johannes Berge039fa42008-05-15 12:55:29 +02001209 IEEE80211_TX_CTL_FIRST_FRAGMENT);
Johannes Bergf0e72852009-03-23 17:28:36 +01001210
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001211 next = skb->next;
Johannes Berga2208582009-03-23 17:28:40 +01001212 len = skb->len;
Johannes Berg5061b0c2009-07-14 00:33:34 +02001213
Johannes Bergad5351d2009-08-07 16:42:15 +02001214 if (next)
1215 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
1216
Johannes Berg5061b0c2009-07-14 00:33:34 +02001217 sdata = vif_to_sdata(info->control.vif);
1218
1219 switch (sdata->vif.type) {
1220 case NL80211_IFTYPE_MONITOR:
1221 info->control.vif = NULL;
1222 break;
1223 case NL80211_IFTYPE_AP_VLAN:
1224 info->control.vif = &container_of(sdata->bss,
1225 struct ieee80211_sub_if_data, u.ap)->vif;
1226 break;
1227 default:
1228 /* keep */
1229 break;
1230 }
1231
Johannes Berg24487982009-04-23 18:52:52 +02001232 ret = drv_tx(local, skb);
Johannes Berga2208582009-03-23 17:28:40 +01001233 if (WARN_ON(ret != NETDEV_TX_OK && skb->len != len)) {
1234 dev_kfree_skb(skb);
1235 ret = NETDEV_TX_OK;
1236 }
Luis R. Rodriguez21f5fc72009-07-24 19:57:25 -04001237 if (ret != NETDEV_TX_OK) {
1238 info->control.vif = &sdata->vif;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001239 return IEEE80211_TX_AGAIN;
Luis R. Rodriguez21f5fc72009-07-24 19:57:25 -04001240 }
1241
Johannes Berg1870cd72009-03-23 17:28:38 +01001242 *skbp = skb = next;
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001243 ieee80211_led_tx(local, 1);
1244 fragm = true;
Johannes Berge2ebc742007-07-27 15:43:22 +02001245 }
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001246
Johannes Berge2ebc742007-07-27 15:43:22 +02001247 return IEEE80211_TX_OK;
1248}
1249
Johannes Berg97b045d2008-06-20 01:22:30 +02001250/*
1251 * Invoke TX handlers, return 0 on success and non-zero if the
1252 * frame was dropped or queued.
1253 */
1254static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1255{
Johannes Berg97b045d2008-06-20 01:22:30 +02001256 struct sk_buff *skb = tx->skb;
Johannes Berg97b045d2008-06-20 01:22:30 +02001257 ieee80211_tx_result res = TX_DROP;
Johannes Berg97b045d2008-06-20 01:22:30 +02001258
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001259#define CALL_TXH(txh) \
1260 do { \
1261 res = txh(tx); \
1262 if (res != TX_CONTINUE) \
1263 goto txh_done; \
1264 } while (0)
Johannes Berg97b045d2008-06-20 01:22:30 +02001265
Kalle Valo5c1b98a2010-01-12 10:42:46 +02001266 CALL_TXH(ieee80211_tx_h_dynamic_ps);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001267 CALL_TXH(ieee80211_tx_h_check_assoc);
1268 CALL_TXH(ieee80211_tx_h_ps_buf);
1269 CALL_TXH(ieee80211_tx_h_select_key);
1270 CALL_TXH(ieee80211_tx_h_michael_mic_add);
Johannes Bergaf65cd962009-11-17 18:18:36 +01001271 if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
1272 CALL_TXH(ieee80211_tx_h_rate_ctrl);
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001273 CALL_TXH(ieee80211_tx_h_misc);
1274 CALL_TXH(ieee80211_tx_h_sequence);
1275 CALL_TXH(ieee80211_tx_h_fragment);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001276 /* handlers after fragment must be aware of tx info fragmentation! */
Johannes Berg9aa4aee2009-10-29 08:43:48 +01001277 CALL_TXH(ieee80211_tx_h_stats);
1278 CALL_TXH(ieee80211_tx_h_encrypt);
1279 CALL_TXH(ieee80211_tx_h_calculate_duration);
Johannes Bergd9e8a702008-06-30 15:10:44 +02001280#undef CALL_TXH
1281
1282 txh_done:
Johannes Berg97b045d2008-06-20 01:22:30 +02001283 if (unlikely(res == TX_DROP)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001284 I802_DEBUG_INC(tx->local->tx_handlers_drop);
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001285 while (skb) {
1286 struct sk_buff *next;
1287
1288 next = skb->next;
1289 dev_kfree_skb(skb);
1290 skb = next;
1291 }
Johannes Berg97b045d2008-06-20 01:22:30 +02001292 return -1;
1293 } else if (unlikely(res == TX_QUEUED)) {
Tomas Winkler5479d0e2008-06-28 03:15:03 +03001294 I802_DEBUG_INC(tx->local->tx_handlers_queued);
Johannes Berg97b045d2008-06-20 01:22:30 +02001295 return -1;
1296 }
1297
1298 return 0;
1299}
1300
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001301static void ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1302 struct sk_buff *skb, bool txpending)
Johannes Berge2ebc742007-07-27 15:43:22 +02001303{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001304 struct ieee80211_local *local = sdata->local;
Johannes Berg5cf121c2008-02-25 16:27:43 +01001305 struct ieee80211_tx_data tx;
Johannes Berg97b045d2008-06-20 01:22:30 +02001306 ieee80211_tx_result res_prepare;
Johannes Berge039fa42008-05-15 12:55:29 +02001307 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg2a577d92009-03-23 17:28:37 +01001308 struct sk_buff *next;
1309 unsigned long flags;
1310 int ret, retries;
Johannes Berge2530082008-05-17 00:57:14 +02001311 u16 queue;
Johannes Berge2ebc742007-07-27 15:43:22 +02001312
Johannes Berge2530082008-05-17 00:57:14 +02001313 queue = skb_get_queue_mapping(skb);
1314
Johannes Berge2ebc742007-07-27 15:43:22 +02001315 if (unlikely(skb->len < 10)) {
1316 dev_kfree_skb(skb);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001317 return;
Johannes Berge2ebc742007-07-27 15:43:22 +02001318 }
1319
Johannes Bergd0709a62008-02-25 16:27:46 +01001320 rcu_read_lock();
1321
Johannes Berg58d41852007-09-26 17:53:18 +02001322 /* initialises tx */
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001323 res_prepare = ieee80211_tx_prepare(sdata, &tx, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001324
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001325 if (unlikely(res_prepare == TX_DROP)) {
Johannes Berge2ebc742007-07-27 15:43:22 +02001326 dev_kfree_skb(skb);
Johannes Bergd0709a62008-02-25 16:27:46 +01001327 rcu_read_unlock();
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001328 return;
1329 } else if (unlikely(res_prepare == TX_QUEUED)) {
1330 rcu_read_unlock();
1331 return;
Johannes Berge2ebc742007-07-27 15:43:22 +02001332 }
1333
Johannes Berg5cf121c2008-02-25 16:27:43 +01001334 tx.channel = local->hw.conf.channel;
Johannes Berge039fa42008-05-15 12:55:29 +02001335 info->band = tx.channel->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02001336
Johannes Berg97b045d2008-06-20 01:22:30 +02001337 if (invoke_tx_handlers(&tx))
1338 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02001339
Johannes Berg2a577d92009-03-23 17:28:37 +01001340 retries = 0;
1341 retry:
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001342 ret = __ieee80211_tx(local, &tx.skb, tx.sta, txpending);
Johannes Berg2a577d92009-03-23 17:28:37 +01001343 switch (ret) {
1344 case IEEE80211_TX_OK:
1345 break;
1346 case IEEE80211_TX_AGAIN:
Johannes Bergeefce912008-05-17 00:57:13 +02001347 /*
1348 * Since there are no fragmented frames on A-MPDU
1349 * queues, there's no reason for a driver to reject
1350 * a frame there, warn and drop it.
1351 */
Johannes Berg2a577d92009-03-23 17:28:37 +01001352 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
1353 goto drop;
1354 /* fall through */
1355 case IEEE80211_TX_PENDING:
1356 skb = tx.skb;
1357
1358 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1359
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001360 if (local->queue_stop_reasons[queue] ||
1361 !skb_queue_empty(&local->pending[queue])) {
1362 /*
1363 * if queue is stopped, queue up frames for later
1364 * transmission from the tasklet
1365 */
Johannes Berg2a577d92009-03-23 17:28:37 +01001366 do {
1367 next = skb->next;
1368 skb->next = NULL;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001369 if (unlikely(txpending))
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001370 __skb_queue_head(&local->pending[queue],
1371 skb);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001372 else
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001373 __skb_queue_tail(&local->pending[queue],
1374 skb);
Johannes Berg2a577d92009-03-23 17:28:37 +01001375 } while ((skb = next));
1376
Johannes Berg2a577d92009-03-23 17:28:37 +01001377 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1378 flags);
1379 } else {
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001380 /*
1381 * otherwise retry, but this is a race condition or
1382 * a driver bug (which we warn about if it persists)
1383 */
Johannes Berg2a577d92009-03-23 17:28:37 +01001384 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1385 flags);
1386
1387 retries++;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001388 if (WARN(retries > 10, "tx refused but queue active\n"))
Vivek Natarajan97d97b82009-02-05 20:05:15 +05301389 goto drop;
Johannes Berge2ebc742007-07-27 15:43:22 +02001390 goto retry;
1391 }
Johannes Berge2ebc742007-07-27 15:43:22 +02001392 }
Johannes Berg97b045d2008-06-20 01:22:30 +02001393 out:
Johannes Bergd4e46a32007-09-14 11:10:24 -04001394 rcu_read_unlock();
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001395 return;
Johannes Berge2ebc742007-07-27 15:43:22 +02001396
1397 drop:
Johannes Bergd4e46a32007-09-14 11:10:24 -04001398 rcu_read_unlock();
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001399
1400 skb = tx.skb;
1401 while (skb) {
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001402 next = skb->next;
1403 dev_kfree_skb(skb);
1404 skb = next;
1405 }
Johannes Berge2ebc742007-07-27 15:43:22 +02001406}
1407
1408/* device xmit handlers */
1409
Johannes Berg23c07522008-05-29 10:38:53 +02001410static int ieee80211_skb_resize(struct ieee80211_local *local,
1411 struct sk_buff *skb,
1412 int head_need, bool may_encrypt)
1413{
1414 int tail_need = 0;
1415
1416 /*
1417 * This could be optimised, devices that do full hardware
1418 * crypto (including TKIP MMIC) need no tailroom... But we
1419 * have no drivers for such devices currently.
1420 */
1421 if (may_encrypt) {
1422 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1423 tail_need -= skb_tailroom(skb);
1424 tail_need = max_t(int, tail_need, 0);
1425 }
1426
1427 if (head_need || tail_need) {
1428 /* Sorry. Can't account for this any more */
1429 skb_orphan(skb);
1430 }
1431
1432 if (skb_header_cloned(skb))
1433 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1434 else
1435 I802_DEBUG_INC(local->tx_expand_skb_head);
1436
1437 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1438 printk(KERN_DEBUG "%s: failed to reallocate TX buffer\n",
1439 wiphy_name(local->hw.wiphy));
1440 return -ENOMEM;
1441 }
1442
1443 /* update truesize too */
1444 skb->truesize += head_need + tail_need;
1445
1446 return 0;
1447}
1448
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001449static void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
1450 struct sk_buff *skb)
Johannes Berge2ebc742007-07-27 15:43:22 +02001451{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001452 struct ieee80211_local *local = sdata->local;
Johannes Berge039fa42008-05-15 12:55:29 +02001453 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001454 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001455 struct ieee80211_sub_if_data *tmp_sdata;
Johannes Berge2ebc742007-07-27 15:43:22 +02001456 int headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001457 bool may_encrypt;
Johannes Berge2ebc742007-07-27 15:43:22 +02001458
Johannes Bergd84f3232009-11-16 23:20:41 +01001459 rcu_read_lock();
1460
Javier Cardonacca89492009-08-10 17:29:29 -07001461 if (unlikely(sdata->vif.type == NL80211_IFTYPE_MONITOR)) {
Johannes Berg25d834e2008-09-12 22:52:47 +02001462 int hdrlen;
1463 u16 len_rthdr;
1464
1465 info->flags |= IEEE80211_TX_CTL_INJECTED;
Johannes Berg25d834e2008-09-12 22:52:47 +02001466
1467 len_rthdr = ieee80211_get_radiotap_len(skb->data);
Pavel Roskin8ef86c72009-07-10 16:42:29 -04001468 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
Johannes Berg25d834e2008-09-12 22:52:47 +02001469 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1470
1471 /* check the header is complete in the frame */
1472 if (likely(skb->len >= len_rthdr + hdrlen)) {
1473 /*
1474 * We process outgoing injected frames that have a
1475 * local address we handle as though they are our
1476 * own frames.
1477 * This code here isn't entirely correct, the local
1478 * MAC address is not necessarily enough to find
1479 * the interface to use; for that proper VLAN/WDS
1480 * support we will need a different mechanism.
1481 */
1482
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001483 list_for_each_entry_rcu(tmp_sdata, &local->interfaces,
Johannes Berg25d834e2008-09-12 22:52:47 +02001484 list) {
Johannes Berg9607e6b2009-12-23 13:15:31 +01001485 if (!ieee80211_sdata_running(tmp_sdata))
Johannes Berg25d834e2008-09-12 22:52:47 +02001486 continue;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001487 if (tmp_sdata->vif.type != NL80211_IFTYPE_AP)
Johannes Bergf1b33cb2009-02-06 00:27:32 +01001488 continue;
Johannes Berg47846c92009-11-25 17:46:19 +01001489 if (compare_ether_addr(tmp_sdata->vif.addr,
Björn Smedman9b1ce522009-10-24 20:55:09 +02001490 hdr->addr2) == 0) {
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001491 sdata = tmp_sdata;
Johannes Berg25d834e2008-09-12 22:52:47 +02001492 break;
1493 }
1494 }
Johannes Berg25d834e2008-09-12 22:52:47 +02001495 }
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001496 }
1497
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001498 may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
Johannes Berg23c07522008-05-29 10:38:53 +02001499
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001500 headroom = local->tx_headroom;
Johannes Berg23c07522008-05-29 10:38:53 +02001501 if (may_encrypt)
1502 headroom += IEEE80211_ENCRYPT_HEADROOM;
1503 headroom -= skb_headroom(skb);
1504 headroom = max_t(int, 0, headroom);
1505
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001506 if (ieee80211_skb_resize(local, skb, headroom, may_encrypt)) {
Johannes Berg23c07522008-05-29 10:38:53 +02001507 dev_kfree_skb(skb);
Johannes Bergd84f3232009-11-16 23:20:41 +01001508 rcu_read_unlock();
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001509 return;
Johannes Berge2ebc742007-07-27 15:43:22 +02001510 }
1511
Johannes Berg5061b0c2009-07-14 00:33:34 +02001512 info->control.vif = &sdata->vif;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001513
Javier Cardonacca89492009-08-10 17:29:29 -07001514 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1515 ieee80211_is_data(hdr->frame_control) &&
1516 !is_multicast_ether_addr(hdr->addr1))
1517 if (mesh_nexthop_lookup(skb, sdata)) {
1518 /* skb queued: don't free */
Johannes Bergd84f3232009-11-16 23:20:41 +01001519 rcu_read_unlock();
Javier Cardonacca89492009-08-10 17:29:29 -07001520 return;
1521 }
1522
Johannes Bergcf0277e2010-01-05 18:00:58 +01001523 ieee80211_set_qos_hdr(local, skb);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001524 ieee80211_tx(sdata, skb, false);
Johannes Bergd84f3232009-11-16 23:20:41 +01001525 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001526}
1527
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +00001528netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
1529 struct net_device *dev)
Johannes Berge2ebc742007-07-27 15:43:22 +02001530{
1531 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Luis R. Rodriguez47f4d882009-01-30 09:08:29 -08001532 struct ieee80211_channel *chan = local->hw.conf.channel;
Johannes Berge2ebc742007-07-27 15:43:22 +02001533 struct ieee80211_radiotap_header *prthdr =
1534 (struct ieee80211_radiotap_header *)skb->data;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001535 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Andy Green9b8a74e2007-07-27 15:43:24 +02001536 u16 len_rthdr;
Johannes Berge2ebc742007-07-27 15:43:22 +02001537
Luis R. Rodriguez47f4d882009-01-30 09:08:29 -08001538 /*
1539 * Frame injection is not allowed if beaconing is not allowed
1540 * or if we need radar detection. Beaconing is usually not allowed when
1541 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
1542 * Passive scan is also used in world regulatory domains where
1543 * your country is not known and as such it should be treated as
1544 * NO TX unless the channel is explicitly allowed in which case
1545 * your current regulatory domain would not have the passive scan
1546 * flag.
1547 *
1548 * Since AP mode uses monitor interfaces to inject/TX management
1549 * frames we can make AP mode the exception to this rule once it
1550 * supports radar detection as its implementation can deal with
1551 * radar detection by itself. We can do that later by adding a
1552 * monitor flag interfaces used for AP support.
1553 */
1554 if ((chan->flags & (IEEE80211_CHAN_NO_IBSS | IEEE80211_CHAN_RADAR |
1555 IEEE80211_CHAN_PASSIVE_SCAN)))
1556 goto fail;
1557
Andy Green9b8a74e2007-07-27 15:43:24 +02001558 /* check for not even having the fixed radiotap header part */
1559 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1560 goto fail; /* too short to be possibly valid */
1561
1562 /* is it a header version we can trust to find length from? */
1563 if (unlikely(prthdr->it_version))
1564 goto fail; /* only version 0 is supported */
1565
1566 /* then there must be a radiotap header with a length we can use */
1567 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1568
1569 /* does the skb contain enough to deliver on the alleged length? */
1570 if (unlikely(skb->len < len_rthdr))
1571 goto fail; /* skb too short for claimed rt header extent */
Johannes Berge2ebc742007-07-27 15:43:22 +02001572
Johannes Berge2ebc742007-07-27 15:43:22 +02001573 /*
1574 * fix up the pointers accounting for the radiotap
1575 * header still being in there. We are being given
1576 * a precooked IEEE80211 header so no need for
1577 * normal processing
1578 */
Andy Green9b8a74e2007-07-27 15:43:24 +02001579 skb_set_mac_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02001580 /*
Andy Green9b8a74e2007-07-27 15:43:24 +02001581 * these are just fixed to the end of the rt area since we
1582 * don't have any better information and at this point, nobody cares
Johannes Berge2ebc742007-07-27 15:43:22 +02001583 */
Andy Green9b8a74e2007-07-27 15:43:24 +02001584 skb_set_network_header(skb, len_rthdr);
1585 skb_set_transport_header(skb, len_rthdr);
Johannes Berge2ebc742007-07-27 15:43:22 +02001586
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001587 memset(info, 0, sizeof(*info));
1588
Johannes Berg7351c6b2009-11-19 01:08:30 +01001589 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1590
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001591 /* pass the radiotap header up to xmit */
1592 ieee80211_xmit(IEEE80211_DEV_TO_SUB_IF(dev), skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001593 return NETDEV_TX_OK;
Andy Green9b8a74e2007-07-27 15:43:24 +02001594
1595fail:
1596 dev_kfree_skb(skb);
1597 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
Johannes Berge2ebc742007-07-27 15:43:22 +02001598}
1599
1600/**
1601 * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1602 * subinterfaces (wlan#, WDS, and VLAN interfaces)
1603 * @skb: packet to be sent
1604 * @dev: incoming interface
1605 *
1606 * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1607 * not be freed, and caller is responsible for either retrying later or freeing
1608 * skb).
1609 *
1610 * This function takes in an Ethernet header and encapsulates it with suitable
1611 * IEEE 802.11 header based on which interface the packet is coming in. The
1612 * encapsulated packet will then be passed to master interface, wlan#.11, for
1613 * transmission (through low-level driver).
1614 */
Stephen Hemmingerd0cf9c02009-08-31 19:50:57 +00001615netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
1616 struct net_device *dev)
Johannes Berge2ebc742007-07-27 15:43:22 +02001617{
Johannes Berg133b8222008-09-16 14:18:59 +02001618 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1619 struct ieee80211_local *local = sdata->local;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001620 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Patrick McHardy5b548142009-06-12 06:22:29 +00001621 int ret = NETDEV_TX_BUSY, head_need;
Harvey Harrison065e96052008-06-22 16:45:27 -07001622 u16 ethertype, hdrlen, meshhdrlen = 0;
1623 __le16 fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02001624 struct ieee80211_hdr hdr;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001625 struct ieee80211s_hdr mesh_hdr;
Johannes Berge2ebc742007-07-27 15:43:22 +02001626 const u8 *encaps_data;
1627 int encaps_len, skip_header_bytes;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001628 int nh_pos, h_pos;
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001629 struct sta_info *sta = NULL;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001630 u32 sta_flags = 0;
Johannes Berge2ebc742007-07-27 15:43:22 +02001631
Johannes Berge2ebc742007-07-27 15:43:22 +02001632 if (unlikely(skb->len < ETH_HLEN)) {
Patrick McHardyec634fe2009-07-05 19:23:38 -07001633 ret = NETDEV_TX_OK;
Johannes Berge2ebc742007-07-27 15:43:22 +02001634 goto fail;
1635 }
1636
1637 nh_pos = skb_network_header(skb) - skb->data;
1638 h_pos = skb_transport_header(skb) - skb->data;
1639
1640 /* convert Ethernet header to proper 802.11 header (based on
1641 * operation mode) */
1642 ethertype = (skb->data[12] << 8) | skb->data[13];
Harvey Harrison065e96052008-06-22 16:45:27 -07001643 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
Johannes Berge2ebc742007-07-27 15:43:22 +02001644
Johannes Berg51fb61e2007-12-19 01:31:27 +01001645 switch (sdata->vif.type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001646 case NL80211_IFTYPE_AP_VLAN:
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001647 rcu_read_lock();
Johannes Berg9bc383d2009-11-19 11:55:19 +01001648 sta = rcu_dereference(sdata->u.vlan.sta);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001649 if (sta) {
1650 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1651 /* RA TA DA SA */
1652 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01001653 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001654 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1655 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1656 hdrlen = 30;
1657 sta_flags = get_sta_flags(sta);
1658 }
1659 rcu_read_unlock();
1660 if (sta)
1661 break;
1662 /* fall through */
1663 case NL80211_IFTYPE_AP:
Harvey Harrison065e96052008-06-22 16:45:27 -07001664 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
Johannes Berge2ebc742007-07-27 15:43:22 +02001665 /* DA BSSID SA */
1666 memcpy(hdr.addr1, skb->data, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01001667 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02001668 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1669 hdrlen = 24;
Johannes Bergcf966832007-08-28 17:01:54 -04001670 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02001671 case NL80211_IFTYPE_WDS:
Harvey Harrison065e96052008-06-22 16:45:27 -07001672 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
Johannes Berge2ebc742007-07-27 15:43:22 +02001673 /* RA TA DA SA */
1674 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01001675 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02001676 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1677 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1678 hdrlen = 30;
Johannes Bergcf966832007-08-28 17:01:54 -04001679 break;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001680#ifdef CONFIG_MAC80211_MESH
Johannes Berg05c914f2008-09-11 00:01:58 +02001681 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg472dbc42008-09-11 00:01:49 +02001682 if (!sdata->u.mesh.mshcfg.dot11MeshTTL) {
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001683 /* Do not send frames with mesh_ttl == 0 */
Johannes Berg472dbc42008-09-11 00:01:49 +02001684 sdata->u.mesh.mshstats.dropped_frames_ttl++;
Patrick McHardyec634fe2009-07-05 19:23:38 -07001685 ret = NETDEV_TX_OK;
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001686 goto fail;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001687 }
YanBo79617de2008-09-22 13:30:32 +08001688
Johannes Berg47846c92009-11-25 17:46:19 +01001689 if (compare_ether_addr(sdata->vif.addr,
1690 skb->data + ETH_ALEN) == 0) {
Javier Cardona3c5772a2009-08-10 12:15:48 -07001691 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
1692 skb->data, skb->data + ETH_ALEN);
1693 meshhdrlen = ieee80211_new_mesh_header(&mesh_hdr,
1694 sdata, NULL, NULL, NULL);
YanBo79617de2008-09-22 13:30:32 +08001695 } else {
1696 /* packet from other interface */
1697 struct mesh_path *mppath;
Javier Cardona3c5772a2009-08-10 12:15:48 -07001698 int is_mesh_mcast = 1;
Johannes Berg15ff6362009-11-17 13:34:04 +01001699 const u8 *mesh_da;
YanBo79617de2008-09-22 13:30:32 +08001700
Javier Cardona3c5772a2009-08-10 12:15:48 -07001701 rcu_read_lock();
YanBo79617de2008-09-22 13:30:32 +08001702 if (is_multicast_ether_addr(skb->data))
Javier Cardona3c5772a2009-08-10 12:15:48 -07001703 /* DA TA mSA AE:SA */
1704 mesh_da = skb->data;
YanBo79617de2008-09-22 13:30:32 +08001705 else {
Johannes Berg15ff6362009-11-17 13:34:04 +01001706 static const u8 bcast[ETH_ALEN] =
1707 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1708
YanBo79617de2008-09-22 13:30:32 +08001709 mppath = mpp_path_lookup(skb->data, sdata);
Javier Cardona3c5772a2009-08-10 12:15:48 -07001710 if (mppath) {
1711 /* RA TA mDA mSA AE:DA SA */
1712 mesh_da = mppath->mpp;
1713 is_mesh_mcast = 0;
Johannes Berg15ff6362009-11-17 13:34:04 +01001714 } else {
Javier Cardona3c5772a2009-08-10 12:15:48 -07001715 /* DA TA mSA AE:SA */
Johannes Berg15ff6362009-11-17 13:34:04 +01001716 mesh_da = bcast;
1717 }
YanBo79617de2008-09-22 13:30:32 +08001718 }
Javier Cardona3c5772a2009-08-10 12:15:48 -07001719 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
Johannes Berg47846c92009-11-25 17:46:19 +01001720 mesh_da, sdata->vif.addr);
Javier Cardona3c5772a2009-08-10 12:15:48 -07001721 rcu_read_unlock();
1722 if (is_mesh_mcast)
1723 meshhdrlen =
1724 ieee80211_new_mesh_header(&mesh_hdr,
1725 sdata,
1726 skb->data + ETH_ALEN,
1727 NULL,
1728 NULL);
1729 else
1730 meshhdrlen =
1731 ieee80211_new_mesh_header(&mesh_hdr,
1732 sdata,
1733 NULL,
1734 skb->data,
1735 skb->data + ETH_ALEN);
YanBo79617de2008-09-22 13:30:32 +08001736
YanBo79617de2008-09-22 13:30:32 +08001737 }
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001738 break;
1739#endif
Johannes Berg05c914f2008-09-11 00:01:58 +02001740 case NL80211_IFTYPE_STATION:
Johannes Berg46900292009-02-15 12:44:28 +01001741 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001742 if (sdata->u.mgd.use_4addr && ethertype != ETH_P_PAE) {
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001743 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1744 /* RA TA DA SA */
Johannes Berg47846c92009-11-25 17:46:19 +01001745 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
Felix Fietkauf14543ee2009-11-10 20:10:05 +01001746 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1747 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1748 hdrlen = 30;
1749 } else {
1750 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
1751 /* BSSID SA DA */
1752 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1753 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1754 hdrlen = 24;
1755 }
Johannes Bergcf966832007-08-28 17:01:54 -04001756 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02001757 case NL80211_IFTYPE_ADHOC:
Johannes Berge2ebc742007-07-27 15:43:22 +02001758 /* DA SA BSSID */
1759 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1760 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
Johannes Berg46900292009-02-15 12:44:28 +01001761 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
Johannes Berge2ebc742007-07-27 15:43:22 +02001762 hdrlen = 24;
Johannes Bergcf966832007-08-28 17:01:54 -04001763 break;
1764 default:
Patrick McHardyec634fe2009-07-05 19:23:38 -07001765 ret = NETDEV_TX_OK;
Johannes Berge2ebc742007-07-27 15:43:22 +02001766 goto fail;
1767 }
1768
Johannes Berg7d185b82008-01-28 17:11:43 +01001769 /*
1770 * There's no need to try to look up the destination
1771 * if it is a multicast address (which can only happen
1772 * in AP mode)
1773 */
1774 if (!is_multicast_ether_addr(hdr.addr1)) {
Johannes Bergd0709a62008-02-25 16:27:46 +01001775 rcu_read_lock();
Johannes Bergabe60632009-11-25 17:46:18 +01001776 sta = sta_info_get(sdata, hdr.addr1);
1777 if (sta)
Johannes Berg07346f812008-05-03 01:02:02 +02001778 sta_flags = get_sta_flags(sta);
Johannes Bergd0709a62008-02-25 16:27:46 +01001779 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001780 }
1781
Johannes Berg3434fbd2008-05-03 00:59:37 +02001782 /* receiver and we are QoS enabled, use a QoS type frame */
Johannes Berg176be722009-03-12 23:49:28 +01001783 if ((sta_flags & WLAN_STA_WME) && local->hw.queues >= 4) {
Harvey Harrison065e96052008-06-22 16:45:27 -07001784 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001785 hdrlen += 2;
1786 }
1787
1788 /*
Johannes Berg238814f2008-01-28 17:19:37 +01001789 * Drop unicast frames to unauthorised stations unless they are
1790 * EAPOL frames from the local station.
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001791 */
Luis Carlos Coboe32f85f2008-08-05 19:34:52 +02001792 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1793 unlikely(!is_multicast_ether_addr(hdr.addr1) &&
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001794 !(sta_flags & WLAN_STA_AUTHORIZED) &&
1795 !(ethertype == ETH_P_PAE &&
Johannes Berg47846c92009-11-25 17:46:19 +01001796 compare_ether_addr(sdata->vif.addr,
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001797 skb->data + ETH_ALEN) == 0))) {
1798#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001799 if (net_ratelimit())
Johannes Berg0c68ae262008-10-27 15:56:10 -07001800 printk(KERN_DEBUG "%s: dropped frame to %pM"
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001801 " (unauthorized port)\n", dev->name,
Johannes Berg0c68ae262008-10-27 15:56:10 -07001802 hdr.addr1);
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001803#endif
1804
1805 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
1806
Patrick McHardyec634fe2009-07-05 19:23:38 -07001807 ret = NETDEV_TX_OK;
Johannes Bergce3edf6d02007-12-19 01:31:22 +01001808 goto fail;
1809 }
1810
Harvey Harrison065e96052008-06-22 16:45:27 -07001811 hdr.frame_control = fc;
Johannes Berge2ebc742007-07-27 15:43:22 +02001812 hdr.duration_id = 0;
1813 hdr.seq_ctrl = 0;
1814
1815 skip_header_bytes = ETH_HLEN;
1816 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1817 encaps_data = bridge_tunnel_header;
1818 encaps_len = sizeof(bridge_tunnel_header);
1819 skip_header_bytes -= 2;
1820 } else if (ethertype >= 0x600) {
1821 encaps_data = rfc1042_header;
1822 encaps_len = sizeof(rfc1042_header);
1823 skip_header_bytes -= 2;
1824 } else {
1825 encaps_data = NULL;
1826 encaps_len = 0;
1827 }
1828
1829 skb_pull(skb, skip_header_bytes);
1830 nh_pos -= skip_header_bytes;
1831 h_pos -= skip_header_bytes;
1832
Johannes Berg23c07522008-05-29 10:38:53 +02001833 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001834
Johannes Berg23c07522008-05-29 10:38:53 +02001835 /*
1836 * So we need to modify the skb header and hence need a copy of
1837 * that. The head_need variable above doesn't, so far, include
1838 * the needed header space that we don't need right away. If we
1839 * can, then we don't reallocate right now but only after the
1840 * frame arrives at the master device (if it does...)
1841 *
1842 * If we cannot, however, then we will reallocate to include all
1843 * the ever needed space. Also, if we need to reallocate it anyway,
1844 * make it big enough for everything we may ever need.
1845 */
Johannes Berge2ebc742007-07-27 15:43:22 +02001846
David S. Miller3a5be7d2008-06-18 01:19:51 -07001847 if (head_need > 0 || skb_cloned(skb)) {
Johannes Berg23c07522008-05-29 10:38:53 +02001848 head_need += IEEE80211_ENCRYPT_HEADROOM;
1849 head_need += local->tx_headroom;
1850 head_need = max_t(int, 0, head_need);
1851 if (ieee80211_skb_resize(local, skb, head_need, true))
Johannes Berge2ebc742007-07-27 15:43:22 +02001852 goto fail;
Johannes Berge2ebc742007-07-27 15:43:22 +02001853 }
1854
1855 if (encaps_data) {
1856 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1857 nh_pos += encaps_len;
1858 h_pos += encaps_len;
1859 }
Johannes Bergc29b9b92007-09-14 11:10:24 -04001860
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01001861 if (meshhdrlen > 0) {
1862 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
1863 nh_pos += meshhdrlen;
1864 h_pos += meshhdrlen;
1865 }
1866
Harvey Harrison065e96052008-06-22 16:45:27 -07001867 if (ieee80211_is_data_qos(fc)) {
Johannes Bergc29b9b92007-09-14 11:10:24 -04001868 __le16 *qos_control;
1869
1870 qos_control = (__le16*) skb_push(skb, 2);
1871 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
1872 /*
1873 * Maybe we could actually set some fields here, for now just
1874 * initialise to zero to indicate no special operation.
1875 */
1876 *qos_control = 0;
1877 } else
1878 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1879
Johannes Berge2ebc742007-07-27 15:43:22 +02001880 nh_pos += hdrlen;
1881 h_pos += hdrlen;
1882
Stephen Hemminger68aae112007-08-24 11:29:34 -07001883 dev->stats.tx_packets++;
1884 dev->stats.tx_bytes += skb->len;
Johannes Berge2ebc742007-07-27 15:43:22 +02001885
1886 /* Update skb pointers to various headers since this modified frame
1887 * is going to go through Linux networking code that may potentially
1888 * need things like pointer to IP header. */
1889 skb_set_mac_header(skb, 0);
1890 skb_set_network_header(skb, nh_pos);
1891 skb_set_transport_header(skb, h_pos);
1892
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001893 memset(info, 0, sizeof(*info));
1894
Johannes Berge2ebc742007-07-27 15:43:22 +02001895 dev->trans_start = jiffies;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001896 ieee80211_xmit(sdata, skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02001897
Patrick McHardyec634fe2009-07-05 19:23:38 -07001898 return NETDEV_TX_OK;
Johannes Berge2ebc742007-07-27 15:43:22 +02001899
1900 fail:
Patrick McHardyec634fe2009-07-05 19:23:38 -07001901 if (ret == NETDEV_TX_OK)
Johannes Berge2ebc742007-07-27 15:43:22 +02001902 dev_kfree_skb(skb);
1903
1904 return ret;
1905}
1906
Johannes Berge2ebc742007-07-27 15:43:22 +02001907
Johannes Berge2530082008-05-17 00:57:14 +02001908/*
1909 * ieee80211_clear_tx_pending may not be called in a context where
1910 * it is possible that it packets could come in again.
1911 */
Johannes Berge2ebc742007-07-27 15:43:22 +02001912void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1913{
Johannes Berg2de8e0d2009-03-23 17:28:35 +01001914 int i;
Johannes Berge2ebc742007-07-27 15:43:22 +02001915
Johannes Berg2a577d92009-03-23 17:28:37 +01001916 for (i = 0; i < local->hw.queues; i++)
1917 skb_queue_purge(&local->pending[i]);
Johannes Berge2ebc742007-07-27 15:43:22 +02001918}
1919
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001920static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
1921 struct sk_buff *skb)
1922{
1923 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1924 struct ieee80211_sub_if_data *sdata;
1925 struct sta_info *sta;
1926 struct ieee80211_hdr *hdr;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001927 int ret;
1928 bool result = true;
1929
Johannes Berg5061b0c2009-07-14 00:33:34 +02001930 sdata = vif_to_sdata(info->control.vif);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001931
1932 if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
Johannes Berg5061b0c2009-07-14 00:33:34 +02001933 ieee80211_tx(sdata, skb, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001934 } else {
1935 hdr = (struct ieee80211_hdr *)skb->data;
Johannes Bergabe60632009-11-25 17:46:18 +01001936 sta = sta_info_get(sdata, hdr->addr1);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001937
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001938 ret = __ieee80211_tx(local, &skb, sta, true);
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001939 if (ret != IEEE80211_TX_OK)
1940 result = false;
1941 }
1942
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001943 return result;
1944}
1945
Johannes Berge2530082008-05-17 00:57:14 +02001946/*
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001947 * Transmit all pending packets. Called from tasklet.
Johannes Berge2530082008-05-17 00:57:14 +02001948 */
Johannes Berge2ebc742007-07-27 15:43:22 +02001949void ieee80211_tx_pending(unsigned long data)
1950{
1951 struct ieee80211_local *local = (struct ieee80211_local *)data;
Johannes Berg2a577d92009-03-23 17:28:37 +01001952 unsigned long flags;
Johannes Bergcd8ffc82009-03-23 17:28:41 +01001953 int i;
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001954 bool txok;
Johannes Berge2ebc742007-07-27 15:43:22 +02001955
Johannes Bergf0e72852009-03-23 17:28:36 +01001956 rcu_read_lock();
Johannes Berg2a577d92009-03-23 17:28:37 +01001957
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001958 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
Johannes Berg176be722009-03-12 23:49:28 +01001959 for (i = 0; i < local->hw.queues; i++) {
Johannes Berg2a577d92009-03-23 17:28:37 +01001960 /*
1961 * If queue is stopped by something other than due to pending
1962 * frames, or we have no pending frames, proceed to next queue.
1963 */
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001964 if (local->queue_stop_reasons[i] ||
Johannes Berg2a577d92009-03-23 17:28:37 +01001965 skb_queue_empty(&local->pending[i]))
Johannes Berge2ebc742007-07-27 15:43:22 +02001966 continue;
Johannes Berge2530082008-05-17 00:57:14 +02001967
Johannes Berg2a577d92009-03-23 17:28:37 +01001968 while (!skb_queue_empty(&local->pending[i])) {
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001969 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
Johannes Berg5061b0c2009-07-14 00:33:34 +02001970 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1971 struct ieee80211_sub_if_data *sdata;
1972
Johannes Berga7bc3762009-07-27 10:33:31 +02001973 if (WARN_ON(!info->control.vif)) {
1974 kfree_skb(skb);
1975 continue;
1976 }
1977
Johannes Berg5061b0c2009-07-14 00:33:34 +02001978 sdata = vif_to_sdata(info->control.vif);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001979 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1980 flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01001981
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001982 txok = ieee80211_tx_pending_skb(local, skb);
1983 if (!txok)
1984 __skb_queue_head(&local->pending[i], skb);
1985 spin_lock_irqsave(&local->queue_stop_reason_lock,
1986 flags);
1987 if (!txok)
Johannes Berg2a577d92009-03-23 17:28:37 +01001988 break;
Johannes Berge2ebc742007-07-27 15:43:22 +02001989 }
1990 }
Johannes Berg3b8d81e02009-06-17 17:43:56 +02001991 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
Johannes Berg2a577d92009-03-23 17:28:37 +01001992
Johannes Bergf0e72852009-03-23 17:28:36 +01001993 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02001994}
1995
1996/* functions for drivers to get certain frames */
1997
Rami Rosen8e7be8d2008-12-01 13:56:55 +02001998static void ieee80211_beacon_add_tim(struct ieee80211_if_ap *bss,
Johannes Berg5dfdaf52007-12-19 02:03:33 +01001999 struct sk_buff *skb,
2000 struct beacon_data *beacon)
Johannes Berge2ebc742007-07-27 15:43:22 +02002001{
2002 u8 *pos, *tim;
2003 int aid0 = 0;
2004 int i, have_bits = 0, n1, n2;
2005
2006 /* Generate bitmap for TIM only if there are any STAs in power save
2007 * mode. */
Johannes Berge2ebc742007-07-27 15:43:22 +02002008 if (atomic_read(&bss->num_sta_ps) > 0)
2009 /* in the hope that this is faster than
2010 * checking byte-for-byte */
2011 have_bits = !bitmap_empty((unsigned long*)bss->tim,
2012 IEEE80211_MAX_AID+1);
2013
2014 if (bss->dtim_count == 0)
Johannes Berg5dfdaf52007-12-19 02:03:33 +01002015 bss->dtim_count = beacon->dtim_period - 1;
Johannes Berge2ebc742007-07-27 15:43:22 +02002016 else
2017 bss->dtim_count--;
2018
2019 tim = pos = (u8 *) skb_put(skb, 6);
2020 *pos++ = WLAN_EID_TIM;
2021 *pos++ = 4;
2022 *pos++ = bss->dtim_count;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01002023 *pos++ = beacon->dtim_period;
Johannes Berge2ebc742007-07-27 15:43:22 +02002024
2025 if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
2026 aid0 = 1;
2027
2028 if (have_bits) {
2029 /* Find largest even number N1 so that bits numbered 1 through
2030 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
2031 * (N2 + 1) x 8 through 2007 are 0. */
2032 n1 = 0;
2033 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
2034 if (bss->tim[i]) {
2035 n1 = i & 0xfe;
2036 break;
2037 }
2038 }
2039 n2 = n1;
2040 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
2041 if (bss->tim[i]) {
2042 n2 = i;
2043 break;
2044 }
2045 }
2046
2047 /* Bitmap control */
2048 *pos++ = n1 | aid0;
2049 /* Part Virt Bitmap */
2050 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
2051
2052 tim[1] = n2 - n1 + 4;
2053 skb_put(skb, n2 - n1);
2054 } else {
2055 *pos++ = aid0; /* Bitmap control */
2056 *pos++ = 0; /* Part Virt Bitmap */
2057 }
Johannes Berge2ebc742007-07-27 15:43:22 +02002058}
2059
Johannes Bergeddcbb92009-10-29 08:30:35 +01002060struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
2061 struct ieee80211_vif *vif,
2062 u16 *tim_offset, u16 *tim_length)
Johannes Berge2ebc742007-07-27 15:43:22 +02002063{
2064 struct ieee80211_local *local = hw_to_local(hw);
Johannes Berg9d139c82008-07-09 14:40:37 +02002065 struct sk_buff *skb = NULL;
Johannes Berge039fa42008-05-15 12:55:29 +02002066 struct ieee80211_tx_info *info;
Johannes Berge2ebc742007-07-27 15:43:22 +02002067 struct ieee80211_sub_if_data *sdata = NULL;
2068 struct ieee80211_if_ap *ap = NULL;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01002069 struct beacon_data *beacon;
Johannes Berg8318d782008-01-24 19:38:38 +01002070 struct ieee80211_supported_band *sband;
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002071 enum ieee80211_band band = local->hw.conf.channel->band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02002072 struct ieee80211_tx_rate_control txrc;
Johannes Berg8318d782008-01-24 19:38:38 +01002073
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002074 sband = local->hw.wiphy->bands[band];
Johannes Berg5dfdaf52007-12-19 02:03:33 +01002075
2076 rcu_read_lock();
Johannes Berge2ebc742007-07-27 15:43:22 +02002077
Johannes Berg32bfd352007-12-19 01:31:26 +01002078 sdata = vif_to_sdata(vif);
Johannes Berge2ebc742007-07-27 15:43:22 +02002079
Johannes Bergeddcbb92009-10-29 08:30:35 +01002080 if (tim_offset)
2081 *tim_offset = 0;
2082 if (tim_length)
2083 *tim_length = 0;
2084
Johannes Berg05c914f2008-09-11 00:01:58 +02002085 if (sdata->vif.type == NL80211_IFTYPE_AP) {
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002086 ap = &sdata->u.ap;
2087 beacon = rcu_dereference(ap->beacon);
Johannes Berg902acc72008-02-23 15:17:19 +01002088 if (ap && beacon) {
2089 /*
2090 * headroom, head length,
2091 * tail length and maximum TIM length
2092 */
2093 skb = dev_alloc_skb(local->tx_headroom +
2094 beacon->head_len +
2095 beacon->tail_len + 256);
2096 if (!skb)
2097 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01002098
Johannes Berg902acc72008-02-23 15:17:19 +01002099 skb_reserve(skb, local->tx_headroom);
2100 memcpy(skb_put(skb, beacon->head_len), beacon->head,
2101 beacon->head_len);
2102
Johannes Bergd0709a62008-02-25 16:27:46 +01002103 /*
2104 * Not very nice, but we want to allow the driver to call
2105 * ieee80211_beacon_get() as a response to the set_tim()
2106 * callback. That, however, is already invoked under the
2107 * sta_lock to guarantee consistent and race-free update
2108 * of the tim bitmap in mac80211 and the driver.
2109 */
2110 if (local->tim_in_locked_section) {
Rami Rosen8e7be8d2008-12-01 13:56:55 +02002111 ieee80211_beacon_add_tim(ap, skb, beacon);
Johannes Bergd0709a62008-02-25 16:27:46 +01002112 } else {
2113 unsigned long flags;
2114
2115 spin_lock_irqsave(&local->sta_lock, flags);
Rami Rosen8e7be8d2008-12-01 13:56:55 +02002116 ieee80211_beacon_add_tim(ap, skb, beacon);
Johannes Bergd0709a62008-02-25 16:27:46 +01002117 spin_unlock_irqrestore(&local->sta_lock, flags);
2118 }
Johannes Berg902acc72008-02-23 15:17:19 +01002119
Johannes Bergeddcbb92009-10-29 08:30:35 +01002120 if (tim_offset)
2121 *tim_offset = beacon->head_len;
2122 if (tim_length)
2123 *tim_length = skb->len - beacon->head_len;
2124
Johannes Berg902acc72008-02-23 15:17:19 +01002125 if (beacon->tail)
2126 memcpy(skb_put(skb, beacon->tail_len),
2127 beacon->tail, beacon->tail_len);
Johannes Berg9d139c82008-07-09 14:40:37 +02002128 } else
2129 goto out;
Johannes Berg05c914f2008-09-11 00:01:58 +02002130 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
Johannes Berg46900292009-02-15 12:44:28 +01002131 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
Johannes Berg9d139c82008-07-09 14:40:37 +02002132 struct ieee80211_hdr *hdr;
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02002133 struct sk_buff *presp = rcu_dereference(ifibss->presp);
Johannes Berg902acc72008-02-23 15:17:19 +01002134
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02002135 if (!presp)
Johannes Berg9d139c82008-07-09 14:40:37 +02002136 goto out;
2137
Johannes Bergaf8cdcd2009-04-19 21:25:43 +02002138 skb = skb_copy(presp, GFP_ATOMIC);
Johannes Berg9d139c82008-07-09 14:40:37 +02002139 if (!skb)
2140 goto out;
2141
2142 hdr = (struct ieee80211_hdr *) skb->data;
Harvey Harrisone7827a72008-07-15 18:44:13 -07002143 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2144 IEEE80211_STYPE_BEACON);
Johannes Berg902acc72008-02-23 15:17:19 +01002145 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
Johannes Berg472dbc42008-09-11 00:01:49 +02002146 struct ieee80211_mgmt *mgmt;
2147 u8 *pos;
2148
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002149 /* headroom, head length, tail length and maximum TIM length */
Johannes Berg902acc72008-02-23 15:17:19 +01002150 skb = dev_alloc_skb(local->tx_headroom + 400);
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002151 if (!skb)
2152 goto out;
2153
Johannes Berg902acc72008-02-23 15:17:19 +01002154 skb_reserve(skb, local->hw.extra_tx_headroom);
2155 mgmt = (struct ieee80211_mgmt *)
2156 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2157 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
Harvey Harrison065e96052008-06-22 16:45:27 -07002158 mgmt->frame_control =
2159 cpu_to_le16(IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_BEACON);
Johannes Berg902acc72008-02-23 15:17:19 +01002160 memset(mgmt->da, 0xff, ETH_ALEN);
Johannes Berg47846c92009-11-25 17:46:19 +01002161 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
2162 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
Johannes Berg902acc72008-02-23 15:17:19 +01002163 mgmt->u.beacon.beacon_int =
Johannes Berg57c4d7b2009-04-23 16:10:04 +02002164 cpu_to_le16(sdata->vif.bss_conf.beacon_int);
Johannes Berg902acc72008-02-23 15:17:19 +01002165 mgmt->u.beacon.capab_info = 0x0; /* 0x0 for MPs */
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002166
Johannes Berg902acc72008-02-23 15:17:19 +01002167 pos = skb_put(skb, 2);
2168 *pos++ = WLAN_EID_SSID;
2169 *pos++ = 0x0;
Luis Carlos Cobo33b64eb2008-02-23 15:17:10 +01002170
Jasper Bryant-Greenef698d852008-08-03 12:04:37 +12002171 mesh_mgmt_ies_add(skb, sdata);
Johannes Berg9d139c82008-07-09 14:40:37 +02002172 } else {
2173 WARN_ON(1);
Johannes Berg5dfdaf52007-12-19 02:03:33 +01002174 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02002175 }
2176
Johannes Berge039fa42008-05-15 12:55:29 +02002177 info = IEEE80211_SKB_CB(skb);
Johannes Berge2ebc742007-07-27 15:43:22 +02002178
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002179 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
Jouni Malinene00cfce2009-12-29 12:59:19 +02002180 info->flags |= IEEE80211_TX_CTL_NO_ACK;
Johannes Berge039fa42008-05-15 12:55:29 +02002181 info->band = band;
Jouni Malinene00cfce2009-12-29 12:59:19 +02002182
2183 memset(&txrc, 0, sizeof(txrc));
2184 txrc.hw = hw;
2185 txrc.sband = sband;
2186 txrc.bss_conf = &sdata->vif.bss_conf;
2187 txrc.skb = skb;
2188 txrc.reported_rate.idx = -1;
Jouni Malinen37eb0b12010-01-06 13:09:08 +02002189 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
2190 if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
2191 txrc.max_rate_idx = -1;
2192 else
2193 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
Jouni Malinene00cfce2009-12-29 12:59:19 +02002194 txrc.ap = true;
2195 rate_control_get_rate(sdata, NULL, &txrc);
Johannes Berge039fa42008-05-15 12:55:29 +02002196
2197 info->control.vif = vif;
Johannes Bergf591fa52008-07-10 11:21:26 +02002198
Johannes Bergf591fa52008-07-10 11:21:26 +02002199 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
2200 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
Johannes Berge6a98542008-10-21 12:40:02 +02002201 out:
Johannes Berg5dfdaf52007-12-19 02:03:33 +01002202 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02002203 return skb;
2204}
Johannes Bergeddcbb92009-10-29 08:30:35 +01002205EXPORT_SYMBOL(ieee80211_beacon_get_tim);
Johannes Berge2ebc742007-07-27 15:43:22 +02002206
Kalle Valo7044cc52010-01-05 20:16:19 +02002207struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
2208 struct ieee80211_vif *vif)
2209{
2210 struct ieee80211_sub_if_data *sdata;
2211 struct ieee80211_if_managed *ifmgd;
2212 struct ieee80211_pspoll *pspoll;
2213 struct ieee80211_local *local;
2214 struct sk_buff *skb;
2215
2216 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2217 return NULL;
2218
2219 sdata = vif_to_sdata(vif);
2220 ifmgd = &sdata->u.mgd;
2221 local = sdata->local;
2222
2223 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
2224 if (!skb) {
2225 printk(KERN_DEBUG "%s: failed to allocate buffer for "
2226 "pspoll template\n", sdata->name);
2227 return NULL;
2228 }
2229 skb_reserve(skb, local->hw.extra_tx_headroom);
2230
2231 pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
2232 memset(pspoll, 0, sizeof(*pspoll));
2233 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
2234 IEEE80211_STYPE_PSPOLL);
2235 pspoll->aid = cpu_to_le16(ifmgd->aid);
2236
2237 /* aid in PS-Poll has its two MSBs each set to 1 */
2238 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
2239
2240 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
2241 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
2242
2243 return skb;
2244}
2245EXPORT_SYMBOL(ieee80211_pspoll_get);
2246
2247struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
2248 struct ieee80211_vif *vif)
2249{
2250 struct ieee80211_hdr_3addr *nullfunc;
2251 struct ieee80211_sub_if_data *sdata;
2252 struct ieee80211_if_managed *ifmgd;
2253 struct ieee80211_local *local;
2254 struct sk_buff *skb;
2255
2256 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2257 return NULL;
2258
2259 sdata = vif_to_sdata(vif);
2260 ifmgd = &sdata->u.mgd;
2261 local = sdata->local;
2262
2263 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*nullfunc));
2264 if (!skb) {
2265 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2266 "template\n", sdata->name);
2267 return NULL;
2268 }
2269 skb_reserve(skb, local->hw.extra_tx_headroom);
2270
2271 nullfunc = (struct ieee80211_hdr_3addr *) skb_put(skb,
2272 sizeof(*nullfunc));
2273 memset(nullfunc, 0, sizeof(*nullfunc));
2274 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
2275 IEEE80211_STYPE_NULLFUNC |
2276 IEEE80211_FCTL_TODS);
2277 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
2278 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
2279 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
2280
2281 return skb;
2282}
2283EXPORT_SYMBOL(ieee80211_nullfunc_get);
2284
Kalle Valo05e54ea2010-01-05 20:16:38 +02002285struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
2286 struct ieee80211_vif *vif,
2287 const u8 *ssid, size_t ssid_len,
2288 const u8 *ie, size_t ie_len)
2289{
2290 struct ieee80211_sub_if_data *sdata;
2291 struct ieee80211_local *local;
2292 struct ieee80211_hdr_3addr *hdr;
2293 struct sk_buff *skb;
2294 size_t ie_ssid_len;
2295 u8 *pos;
2296
2297 sdata = vif_to_sdata(vif);
2298 local = sdata->local;
2299 ie_ssid_len = 2 + ssid_len;
2300
2301 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
2302 ie_ssid_len + ie_len);
2303 if (!skb) {
2304 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
2305 "request template\n", sdata->name);
2306 return NULL;
2307 }
2308
2309 skb_reserve(skb, local->hw.extra_tx_headroom);
2310
2311 hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
2312 memset(hdr, 0, sizeof(*hdr));
2313 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2314 IEEE80211_STYPE_PROBE_REQ);
2315 memset(hdr->addr1, 0xff, ETH_ALEN);
2316 memcpy(hdr->addr2, vif->addr, ETH_ALEN);
2317 memset(hdr->addr3, 0xff, ETH_ALEN);
2318
2319 pos = skb_put(skb, ie_ssid_len);
2320 *pos++ = WLAN_EID_SSID;
2321 *pos++ = ssid_len;
2322 if (ssid)
2323 memcpy(pos, ssid, ssid_len);
2324 pos += ssid_len;
2325
2326 if (ie) {
2327 pos = skb_put(skb, ie_len);
2328 memcpy(pos, ie, ie_len);
2329 }
2330
2331 return skb;
2332}
2333EXPORT_SYMBOL(ieee80211_probereq_get);
2334
Johannes Berg32bfd352007-12-19 01:31:26 +01002335void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02002336 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02002337 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02002338 struct ieee80211_rts *rts)
2339{
2340 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02002341
Harvey Harrison065e96052008-06-22 16:45:27 -07002342 rts->frame_control =
2343 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01002344 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
2345 frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02002346 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
2347 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
2348}
2349EXPORT_SYMBOL(ieee80211_rts_get);
2350
Johannes Berg32bfd352007-12-19 01:31:26 +01002351void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Johannes Berge2ebc742007-07-27 15:43:22 +02002352 const void *frame, size_t frame_len,
Johannes Berge039fa42008-05-15 12:55:29 +02002353 const struct ieee80211_tx_info *frame_txctl,
Johannes Berge2ebc742007-07-27 15:43:22 +02002354 struct ieee80211_cts *cts)
2355{
2356 const struct ieee80211_hdr *hdr = frame;
Johannes Berge2ebc742007-07-27 15:43:22 +02002357
Harvey Harrison065e96052008-06-22 16:45:27 -07002358 cts->frame_control =
2359 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
Johannes Berg32bfd352007-12-19 01:31:26 +01002360 cts->duration = ieee80211_ctstoself_duration(hw, vif,
2361 frame_len, frame_txctl);
Johannes Berge2ebc742007-07-27 15:43:22 +02002362 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
2363}
2364EXPORT_SYMBOL(ieee80211_ctstoself_get);
2365
2366struct sk_buff *
Johannes Berg32bfd352007-12-19 01:31:26 +01002367ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
Johannes Berge039fa42008-05-15 12:55:29 +02002368 struct ieee80211_vif *vif)
Johannes Berge2ebc742007-07-27 15:43:22 +02002369{
2370 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winkler747cf5e2008-05-27 17:50:51 +03002371 struct sk_buff *skb = NULL;
Johannes Berge2ebc742007-07-27 15:43:22 +02002372 struct sta_info *sta;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002373 struct ieee80211_tx_data tx;
Johannes Berge2ebc742007-07-27 15:43:22 +02002374 struct ieee80211_sub_if_data *sdata;
2375 struct ieee80211_if_ap *bss = NULL;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01002376 struct beacon_data *beacon;
Johannes Berge039fa42008-05-15 12:55:29 +02002377 struct ieee80211_tx_info *info;
Johannes Berge2ebc742007-07-27 15:43:22 +02002378
Johannes Berg32bfd352007-12-19 01:31:26 +01002379 sdata = vif_to_sdata(vif);
Tomas Winkler747cf5e2008-05-27 17:50:51 +03002380 bss = &sdata->u.ap;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01002381
Johannes Berg5dfdaf52007-12-19 02:03:33 +01002382 rcu_read_lock();
2383 beacon = rcu_dereference(bss->beacon);
2384
Johannes Berg05c914f2008-09-11 00:01:58 +02002385 if (sdata->vif.type != NL80211_IFTYPE_AP || !beacon || !beacon->head)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03002386 goto out;
Johannes Berg5dfdaf52007-12-19 02:03:33 +01002387
Johannes Berge2ebc742007-07-27 15:43:22 +02002388 if (bss->dtim_count != 0)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03002389 goto out; /* send buffered bc/mc only after DTIM beacon */
Johannes Berge039fa42008-05-15 12:55:29 +02002390
Johannes Berge2ebc742007-07-27 15:43:22 +02002391 while (1) {
2392 skb = skb_dequeue(&bss->ps_bc_buf);
2393 if (!skb)
Tomas Winkler747cf5e2008-05-27 17:50:51 +03002394 goto out;
Johannes Berge2ebc742007-07-27 15:43:22 +02002395 local->total_ps_buffered--;
2396
2397 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
2398 struct ieee80211_hdr *hdr =
2399 (struct ieee80211_hdr *) skb->data;
2400 /* more buffered multicast/broadcast frames ==> set
2401 * MoreData flag in IEEE 802.11 header to inform PS
2402 * STAs */
2403 hdr->frame_control |=
2404 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2405 }
2406
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002407 if (!ieee80211_tx_prepare(sdata, &tx, skb))
Johannes Berge2ebc742007-07-27 15:43:22 +02002408 break;
2409 dev_kfree_skb_any(skb);
2410 }
Johannes Berge039fa42008-05-15 12:55:29 +02002411
2412 info = IEEE80211_SKB_CB(skb);
2413
Johannes Berge2ebc742007-07-27 15:43:22 +02002414 sta = tx.sta;
Johannes Berg5cf121c2008-02-25 16:27:43 +01002415 tx.flags |= IEEE80211_TX_PS_BUFFERED;
2416 tx.channel = local->hw.conf.channel;
Johannes Berge039fa42008-05-15 12:55:29 +02002417 info->band = tx.channel->band;
Johannes Berge2ebc742007-07-27 15:43:22 +02002418
Johannes Berg97b045d2008-06-20 01:22:30 +02002419 if (invoke_tx_handlers(&tx))
Johannes Berge2ebc742007-07-27 15:43:22 +02002420 skb = NULL;
Johannes Berg97b045d2008-06-20 01:22:30 +02002421 out:
Johannes Bergd0709a62008-02-25 16:27:46 +01002422 rcu_read_unlock();
Johannes Berge2ebc742007-07-27 15:43:22 +02002423
2424 return skb;
2425}
2426EXPORT_SYMBOL(ieee80211_get_buffered_bc);
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002427
Johannes Berg62ae67b2009-11-18 18:42:05 +01002428void ieee80211_tx_skb(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002429{
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002430 skb_set_mac_header(skb, 0);
2431 skb_set_network_header(skb, 0);
2432 skb_set_transport_header(skb, 0);
2433
Johannes Bergcf0277e2010-01-05 18:00:58 +01002434 /* send all internal mgmt frames on VO */
2435 skb_set_queue_mapping(skb, 0);
2436
Johannes Berg3d34deb2009-06-18 17:25:11 +02002437 /*
2438 * The other path calling ieee80211_xmit is from the tasklet,
2439 * and while we can handle concurrent transmissions locking
2440 * requirements are that we do not come into tx with bhs on.
2441 */
2442 local_bh_disable();
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002443 ieee80211_xmit(sdata, skb);
Johannes Berg3d34deb2009-06-18 17:25:11 +02002444 local_bh_enable();
Johannes Berg3b8d81e02009-06-17 17:43:56 +02002445}