iwlwifi: remove iwlcore_low_level_notify
[deliverable/linux.git] / net / mac80211 / tx.c
CommitLineData
e2ebc74d
JB
1/*
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>
d4e46a3d 20#include <linux/rcupdate.h>
881d966b 21#include <net/net_namespace.h>
e2ebc74d
JB
22#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"
2c8dccc7 28#include "led.h"
33b64eb2 29#include "mesh.h"
e2ebc74d
JB
30#include "wep.h"
31#include "wpa.h"
32#include "wme.h"
2c8dccc7 33#include "rate.h"
e2ebc74d
JB
34
35#define IEEE80211_TX_OK 0
36#define IEEE80211_TX_AGAIN 1
37#define IEEE80211_TX_FRAG_AGAIN 2
38
39/* misc utils */
40
41static inline void ieee80211_include_sequence(struct ieee80211_sub_if_data *sdata,
42 struct ieee80211_hdr *hdr)
43{
44 /* Set the sequence number for this frame. */
45 hdr->seq_ctrl = cpu_to_le16(sdata->sequence);
46
47 /* Increase the sequence number. */
48 sdata->sequence = (sdata->sequence + 0x10) & IEEE80211_SCTL_SEQ;
49}
50
51#ifdef CONFIG_MAC80211_LOWTX_FRAME_DUMP
52static void ieee80211_dump_frame(const char *ifname, const char *title,
53 const struct sk_buff *skb)
54{
55 const struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
56 u16 fc;
57 int hdrlen;
0795af57 58 DECLARE_MAC_BUF(mac);
e2ebc74d
JB
59
60 printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
61 if (skb->len < 4) {
62 printk("\n");
63 return;
64 }
65
66 fc = le16_to_cpu(hdr->frame_control);
67 hdrlen = ieee80211_get_hdrlen(fc);
68 if (hdrlen > skb->len)
69 hdrlen = skb->len;
70 if (hdrlen >= 4)
71 printk(" FC=0x%04x DUR=0x%04x",
72 fc, le16_to_cpu(hdr->duration_id));
73 if (hdrlen >= 10)
0795af57 74 printk(" A1=%s", print_mac(mac, hdr->addr1));
e2ebc74d 75 if (hdrlen >= 16)
0795af57 76 printk(" A2=%s", print_mac(mac, hdr->addr2));
e2ebc74d 77 if (hdrlen >= 24)
0795af57 78 printk(" A3=%s", print_mac(mac, hdr->addr3));
e2ebc74d 79 if (hdrlen >= 30)
0795af57 80 printk(" A4=%s", print_mac(mac, hdr->addr4));
e2ebc74d
JB
81 printk("\n");
82}
83#else /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
84static inline void ieee80211_dump_frame(const char *ifname, const char *title,
85 struct sk_buff *skb)
86{
87}
88#endif /* CONFIG_MAC80211_LOWTX_FRAME_DUMP */
89
5cf121c3 90static u16 ieee80211_duration(struct ieee80211_tx_data *tx, int group_addr,
e2ebc74d
JB
91 int next_frag_len)
92{
93 int rate, mrate, erp, dur, i;
2e92e6f2 94 struct ieee80211_rate *txrate;
e2ebc74d 95 struct ieee80211_local *local = tx->local;
8318d78a 96 struct ieee80211_supported_band *sband;
e2ebc74d 97
2e92e6f2
JB
98 sband = local->hw.wiphy->bands[tx->channel->band];
99 txrate = &sband->bitrates[tx->rate_idx];
8318d78a
JB
100
101 erp = 0;
102 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
103 erp = txrate->flags & IEEE80211_RATE_ERP_G;
e2ebc74d
JB
104
105 /*
106 * data and mgmt (except PS Poll):
107 * - during CFP: 32768
108 * - during contention period:
109 * if addr1 is group address: 0
110 * if more fragments = 0 and addr1 is individual address: time to
111 * transmit one ACK plus SIFS
112 * if more fragments = 1 and addr1 is individual address: time to
113 * transmit next fragment plus 2 x ACK plus 3 x SIFS
114 *
115 * IEEE 802.11, 9.6:
116 * - control response frame (CTS or ACK) shall be transmitted using the
117 * same rate as the immediately previous frame in the frame exchange
118 * sequence, if this rate belongs to the PHY mandatory rates, or else
119 * at the highest possible rate belonging to the PHY rates in the
120 * BSSBasicRateSet
121 */
122
123 if ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_CTL) {
124 /* TODO: These control frames are not currently sent by
125 * 80211.o, but should they be implemented, this function
126 * needs to be updated to support duration field calculation.
127 *
128 * RTS: time needed to transmit pending data/mgmt frame plus
129 * one CTS frame plus one ACK frame plus 3 x SIFS
130 * CTS: duration of immediately previous RTS minus time
131 * required to transmit CTS and its SIFS
132 * ACK: 0 if immediately previous directed data/mgmt had
133 * more=0, with more=1 duration in ACK frame is duration
134 * from previous frame minus time needed to transmit ACK
135 * and its SIFS
136 * PS Poll: BIT(15) | BIT(14) | aid
137 */
138 return 0;
139 }
140
141 /* data/mgmt */
142 if (0 /* FIX: data/mgmt during CFP */)
143 return 32768;
144
145 if (group_addr) /* Group address as the destination - no ACK */
146 return 0;
147
148 /* Individual destination address:
149 * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
150 * CTS and ACK frames shall be transmitted using the highest rate in
151 * basic rate set that is less than or equal to the rate of the
152 * immediately previous frame and that is using the same modulation
153 * (CCK or OFDM). If no basic rate set matches with these requirements,
154 * the highest mandatory rate of the PHY that is less than or equal to
155 * the rate of the previous frame is used.
156 * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
157 */
158 rate = -1;
8318d78a
JB
159 /* use lowest available if everything fails */
160 mrate = sband->bitrates[0].bitrate;
161 for (i = 0; i < sband->n_bitrates; i++) {
162 struct ieee80211_rate *r = &sband->bitrates[i];
e2ebc74d 163
8318d78a
JB
164 if (r->bitrate > txrate->bitrate)
165 break;
e2ebc74d 166
8318d78a
JB
167 if (tx->sdata->basic_rates & BIT(i))
168 rate = r->bitrate;
169
170 switch (sband->band) {
171 case IEEE80211_BAND_2GHZ: {
172 u32 flag;
173 if (tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE)
174 flag = IEEE80211_RATE_MANDATORY_G;
175 else
176 flag = IEEE80211_RATE_MANDATORY_B;
177 if (r->flags & flag)
178 mrate = r->bitrate;
179 break;
180 }
181 case IEEE80211_BAND_5GHZ:
182 if (r->flags & IEEE80211_RATE_MANDATORY_A)
183 mrate = r->bitrate;
184 break;
185 case IEEE80211_NUM_BANDS:
186 WARN_ON(1);
187 break;
188 }
e2ebc74d
JB
189 }
190 if (rate == -1) {
191 /* No matching basic rate found; use highest suitable mandatory
192 * PHY rate */
193 rate = mrate;
194 }
195
196 /* Time needed to transmit ACK
197 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
198 * to closest integer */
199
200 dur = ieee80211_frame_duration(local, 10, rate, erp,
471b3efd 201 tx->sdata->bss_conf.use_short_preamble);
e2ebc74d
JB
202
203 if (next_frag_len) {
204 /* Frame is fragmented: duration increases with time needed to
205 * transmit next fragment plus ACK and 2 x SIFS. */
206 dur *= 2; /* ACK + SIFS */
207 /* next fragment */
208 dur += ieee80211_frame_duration(local, next_frag_len,
8318d78a 209 txrate->bitrate, erp,
471b3efd 210 tx->sdata->bss_conf.use_short_preamble);
e2ebc74d
JB
211 }
212
213 return dur;
214}
215
e2ebc74d
JB
216static int inline is_ieee80211_device(struct net_device *dev,
217 struct net_device *master)
218{
219 return (wdev_priv(dev->ieee80211_ptr) ==
220 wdev_priv(master->ieee80211_ptr));
221}
222
223/* tx handlers */
224
9ae54c84 225static ieee80211_tx_result
5cf121c3 226ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
e2ebc74d
JB
227{
228#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
e039fa4a 229 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
e2ebc74d 230#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
e039fa4a 231 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
e2ebc74d
JB
232 u32 sta_flags;
233
e039fa4a 234 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
9ae54c84 235 return TX_CONTINUE;
58d4185e 236
ece8eddd 237 if (unlikely(tx->local->sta_sw_scanning) &&
e2ebc74d
JB
238 ((tx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_MGMT ||
239 (tx->fc & IEEE80211_FCTL_STYPE) != IEEE80211_STYPE_PROBE_REQ))
9ae54c84 240 return TX_DROP;
e2ebc74d 241
33b64eb2
LCC
242 if (tx->sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT)
243 return TX_CONTINUE;
244
5cf121c3 245 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
9ae54c84 246 return TX_CONTINUE;
e2ebc74d 247
07346f81 248 sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
e2ebc74d 249
5cf121c3 250 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
e2ebc74d 251 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
51fb61e7 252 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
e2ebc74d
JB
253 (tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
254#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
0795af57 255 DECLARE_MAC_BUF(mac);
e2ebc74d 256 printk(KERN_DEBUG "%s: dropped data frame to not "
0795af57
JP
257 "associated station %s\n",
258 tx->dev->name, print_mac(mac, hdr->addr1));
e2ebc74d
JB
259#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
260 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
9ae54c84 261 return TX_DROP;
e2ebc74d
JB
262 }
263 } else {
264 if (unlikely((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
265 tx->local->num_sta == 0 &&
51fb61e7 266 tx->sdata->vif.type != IEEE80211_IF_TYPE_IBSS)) {
e2ebc74d
JB
267 /*
268 * No associated STAs - no need to send multicast
269 * frames.
270 */
9ae54c84 271 return TX_DROP;
e2ebc74d 272 }
9ae54c84 273 return TX_CONTINUE;
e2ebc74d
JB
274 }
275
9ae54c84 276 return TX_CONTINUE;
e2ebc74d
JB
277}
278
9ae54c84 279static ieee80211_tx_result
5cf121c3 280ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
e2ebc74d
JB
281{
282 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
283
284 if (ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control)) >= 24)
285 ieee80211_include_sequence(tx->sdata, hdr);
286
9ae54c84 287 return TX_CONTINUE;
e2ebc74d
JB
288}
289
290/* This function is called whenever the AP is about to exceed the maximum limit
291 * of buffered frames for power saving STAs. This situation should not really
292 * happen often during normal operation, so dropping the oldest buffered packet
293 * from each queue should be OK to make some room for new frames. */
294static void purge_old_ps_buffers(struct ieee80211_local *local)
295{
296 int total = 0, purged = 0;
297 struct sk_buff *skb;
298 struct ieee80211_sub_if_data *sdata;
299 struct sta_info *sta;
300
79010420
JB
301 /*
302 * virtual interfaces are protected by RCU
303 */
304 rcu_read_lock();
305
306 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
e2ebc74d
JB
307 struct ieee80211_if_ap *ap;
308 if (sdata->dev == local->mdev ||
51fb61e7 309 sdata->vif.type != IEEE80211_IF_TYPE_AP)
e2ebc74d
JB
310 continue;
311 ap = &sdata->u.ap;
312 skb = skb_dequeue(&ap->ps_bc_buf);
313 if (skb) {
314 purged++;
315 dev_kfree_skb(skb);
316 }
317 total += skb_queue_len(&ap->ps_bc_buf);
318 }
e2ebc74d 319
d0709a65 320 list_for_each_entry_rcu(sta, &local->sta_list, list) {
e2ebc74d
JB
321 skb = skb_dequeue(&sta->ps_tx_buf);
322 if (skb) {
323 purged++;
324 dev_kfree_skb(skb);
325 }
326 total += skb_queue_len(&sta->ps_tx_buf);
327 }
d0709a65
JB
328
329 rcu_read_unlock();
e2ebc74d
JB
330
331 local->total_ps_buffered = total;
332 printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
dd1cd4c6 333 wiphy_name(local->hw.wiphy), purged);
e2ebc74d
JB
334}
335
9ae54c84 336static ieee80211_tx_result
5cf121c3 337ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
e2ebc74d 338{
e039fa4a
JB
339 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
340
7d54d0dd
JB
341 /*
342 * broadcast/multicast frame
343 *
344 * If any of the associated stations is in power save mode,
345 * the frame is buffered to be sent after DTIM beacon frame.
346 * This is done either by the hardware or us.
347 */
348
349 /* not AP/IBSS or ordered frame */
350 if (!tx->sdata->bss || (tx->fc & IEEE80211_FCTL_ORDER))
9ae54c84 351 return TX_CONTINUE;
7d54d0dd
JB
352
353 /* no stations in PS mode */
354 if (!atomic_read(&tx->sdata->bss->num_sta_ps))
9ae54c84 355 return TX_CONTINUE;
7d54d0dd
JB
356
357 /* buffered in mac80211 */
358 if (tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) {
e2ebc74d
JB
359 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
360 purge_old_ps_buffers(tx->local);
361 if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
362 AP_MAX_BC_BUFFER) {
363 if (net_ratelimit()) {
364 printk(KERN_DEBUG "%s: BC TX buffer full - "
365 "dropping the oldest frame\n",
366 tx->dev->name);
367 }
368 dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
369 } else
370 tx->local->total_ps_buffered++;
371 skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
9ae54c84 372 return TX_QUEUED;
e2ebc74d
JB
373 }
374
7d54d0dd 375 /* buffered in hardware */
e039fa4a 376 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
7d54d0dd 377
9ae54c84 378 return TX_CONTINUE;
e2ebc74d
JB
379}
380
9ae54c84 381static ieee80211_tx_result
5cf121c3 382ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
e2ebc74d
JB
383{
384 struct sta_info *sta = tx->sta;
e039fa4a 385 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
07346f81 386 u32 staflags;
0795af57 387 DECLARE_MAC_BUF(mac);
e2ebc74d
JB
388
389 if (unlikely(!sta ||
390 ((tx->fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT &&
391 (tx->fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_PROBE_RESP)))
9ae54c84 392 return TX_CONTINUE;
e2ebc74d 393
07346f81
JB
394 staflags = get_sta_flags(sta);
395
396 if (unlikely((staflags & WLAN_STA_PS) &&
397 !(staflags & WLAN_STA_PSPOLL))) {
e2ebc74d 398#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
0795af57 399 printk(KERN_DEBUG "STA %s aid %d: PS buffer (entries "
e2ebc74d 400 "before %d)\n",
0795af57 401 print_mac(mac, sta->addr), sta->aid,
e2ebc74d
JB
402 skb_queue_len(&sta->ps_tx_buf));
403#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
e2ebc74d
JB
404 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
405 purge_old_ps_buffers(tx->local);
406 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
407 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
408 if (net_ratelimit()) {
0795af57 409 printk(KERN_DEBUG "%s: STA %s TX "
e2ebc74d 410 "buffer full - dropping oldest frame\n",
0795af57 411 tx->dev->name, print_mac(mac, sta->addr));
e2ebc74d
JB
412 }
413 dev_kfree_skb(old);
414 } else
415 tx->local->total_ps_buffered++;
004c872e 416
e2ebc74d 417 /* Queue frame to be sent after STA sends an PS Poll frame */
004c872e
JB
418 if (skb_queue_empty(&sta->ps_tx_buf))
419 sta_info_set_tim_bit(sta);
420
e039fa4a 421 info->control.jiffies = jiffies;
e2ebc74d 422 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
9ae54c84 423 return TX_QUEUED;
e2ebc74d
JB
424 }
425#ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
07346f81 426 else if (unlikely(test_sta_flags(sta, WLAN_STA_PS))) {
0795af57 427 printk(KERN_DEBUG "%s: STA %s in PS mode, but pspoll "
e2ebc74d 428 "set -> send frame\n", tx->dev->name,
0795af57 429 print_mac(mac, sta->addr));
e2ebc74d
JB
430 }
431#endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
07346f81 432 clear_sta_flags(sta, WLAN_STA_PSPOLL);
e2ebc74d 433
9ae54c84 434 return TX_CONTINUE;
e2ebc74d
JB
435}
436
9ae54c84 437static ieee80211_tx_result
5cf121c3 438ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
e2ebc74d 439{
5cf121c3 440 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
9ae54c84 441 return TX_CONTINUE;
e2ebc74d 442
5cf121c3 443 if (tx->flags & IEEE80211_TX_UNICAST)
e2ebc74d
JB
444 return ieee80211_tx_h_unicast_ps_buf(tx);
445 else
446 return ieee80211_tx_h_multicast_ps_buf(tx);
447}
448
9ae54c84 449static ieee80211_tx_result
5cf121c3 450ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
e2ebc74d 451{
d4e46a3d 452 struct ieee80211_key *key;
e039fa4a 453 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
176e4f84 454 u16 fc = tx->fc;
d4e46a3d 455
e039fa4a 456 if (unlikely(info->flags & IEEE80211_TX_CTL_DO_NOT_ENCRYPT))
e2ebc74d 457 tx->key = NULL;
d4e46a3d
JB
458 else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
459 tx->key = key;
460 else if ((key = rcu_dereference(tx->sdata->default_key)))
461 tx->key = key;
e2ebc74d 462 else if (tx->sdata->drop_unencrypted &&
e039fa4a
JB
463 !(info->flags & IEEE80211_TX_CTL_EAPOL_FRAME) &&
464 !(info->flags & IEEE80211_TX_CTL_INJECTED)) {
e2ebc74d 465 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
9ae54c84 466 return TX_DROP;
176e4f84 467 } else
e2ebc74d
JB
468 tx->key = NULL;
469
470 if (tx->key) {
176e4f84
JB
471 u16 ftype, stype;
472
e2ebc74d 473 tx->key->tx_rx_count++;
011bfcc4 474 /* TODO: add threshold stuff again */
176e4f84
JB
475
476 switch (tx->key->conf.alg) {
477 case ALG_WEP:
478 ftype = fc & IEEE80211_FCTL_FTYPE;
479 stype = fc & IEEE80211_FCTL_STYPE;
480
481 if (ftype == IEEE80211_FTYPE_MGMT &&
482 stype == IEEE80211_STYPE_AUTH)
483 break;
484 case ALG_TKIP:
485 case ALG_CCMP:
486 if (!WLAN_FC_DATA_PRESENT(fc))
487 tx->key = NULL;
488 break;
489 }
e2ebc74d
JB
490 }
491
176e4f84 492 if (!tx->key || !(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
e039fa4a 493 info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
176e4f84 494
9ae54c84 495 return TX_CONTINUE;
e2ebc74d
JB
496}
497
9ae54c84 498static ieee80211_tx_result
5cf121c3 499ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
e2ebc74d 500{
1abbe498 501 struct rate_selection rsel;
8318d78a 502 struct ieee80211_supported_band *sband;
e039fa4a 503 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
8318d78a 504
2e92e6f2 505 sband = tx->local->hw.wiphy->bands[tx->channel->band];
e2ebc74d 506
2e92e6f2 507 if (likely(tx->rate_idx < 0)) {
8318d78a 508 rate_control_get_rate(tx->dev, sband, tx->skb, &rsel);
2e92e6f2
JB
509 tx->rate_idx = rsel.rate_idx;
510 if (unlikely(rsel.probe_idx >= 0)) {
e039fa4a 511 info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
5cf121c3 512 tx->flags |= IEEE80211_TX_PROBE_LAST_FRAG;
e039fa4a 513 info->control.alt_retry_rate_idx = tx->rate_idx;
2e92e6f2 514 tx->rate_idx = rsel.probe_idx;
58d4185e 515 } else
e039fa4a 516 info->control.alt_retry_rate_idx = -1;
58d4185e 517
2e92e6f2 518 if (unlikely(tx->rate_idx < 0))
9ae54c84 519 return TX_DROP;
58d4185e 520 } else
e039fa4a 521 info->control.alt_retry_rate_idx = -1;
58d4185e 522
8318d78a 523 if (tx->sdata->bss_conf.use_cts_prot &&
2e92e6f2
JB
524 (tx->flags & IEEE80211_TX_FRAGMENTED) && (rsel.nonerp_idx >= 0)) {
525 tx->last_frag_rate_idx = tx->rate_idx;
526 if (rsel.probe_idx >= 0)
5cf121c3 527 tx->flags &= ~IEEE80211_TX_PROBE_LAST_FRAG;
badffb72 528 else
5cf121c3 529 tx->flags |= IEEE80211_TX_PROBE_LAST_FRAG;
2e92e6f2 530 tx->rate_idx = rsel.nonerp_idx;
e039fa4a
JB
531 info->tx_rate_idx = rsel.nonerp_idx;
532 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
e2ebc74d 533 } else {
2e92e6f2 534 tx->last_frag_rate_idx = tx->rate_idx;
e039fa4a 535 info->tx_rate_idx = tx->rate_idx;
e2ebc74d 536 }
e039fa4a 537 info->tx_rate_idx = tx->rate_idx;
e2ebc74d 538
9ae54c84 539 return TX_CONTINUE;
e2ebc74d
JB
540}
541
9ae54c84 542static ieee80211_tx_result
5cf121c3 543ieee80211_tx_h_misc(struct ieee80211_tx_data *tx)
e2ebc74d
JB
544{
545 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
7e9ed188 546 u16 fc = le16_to_cpu(hdr->frame_control);
e2ebc74d 547 u16 dur;
e039fa4a 548 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
2e92e6f2
JB
549 struct ieee80211_supported_band *sband;
550
551 sband = tx->local->hw.wiphy->bands[tx->channel->band];
e2ebc74d 552
e039fa4a
JB
553 if (tx->sta)
554 info->control.aid = tx->sta->aid;
555
556 if (!info->control.retry_limit) {
58d4185e 557 if (!is_multicast_ether_addr(hdr->addr1)) {
e039fa4a
JB
558 int len = min_t(int, tx->skb->len + FCS_LEN,
559 tx->local->fragmentation_threshold);
560 if (len > tx->local->rts_threshold
58d4185e 561 && tx->local->rts_threshold <
e039fa4a
JB
562 IEEE80211_MAX_RTS_THRESHOLD) {
563 info->flags |= IEEE80211_TX_CTL_USE_RTS_CTS;
564 info->flags |=
565 IEEE80211_TX_CTL_LONG_RETRY_LIMIT;
566 info->control.retry_limit =
58d4185e
JB
567 tx->local->long_retry_limit;
568 } else {
e039fa4a 569 info->control.retry_limit =
58d4185e
JB
570 tx->local->short_retry_limit;
571 }
e2ebc74d 572 } else {
e039fa4a 573 info->control.retry_limit = 1;
e2ebc74d 574 }
e2ebc74d
JB
575 }
576
5cf121c3 577 if (tx->flags & IEEE80211_TX_FRAGMENTED) {
e2ebc74d
JB
578 /* Do not use multiple retry rates when sending fragmented
579 * frames.
580 * TODO: The last fragment could still use multiple retry
581 * rates. */
e039fa4a 582 info->control.alt_retry_rate_idx = -1;
e2ebc74d
JB
583 }
584
585 /* Use CTS protection for unicast frames sent using extended rates if
586 * there are associated non-ERP stations and RTS/CTS is not configured
587 * for the frame. */
8318d78a 588 if ((tx->sdata->flags & IEEE80211_SDATA_OPERATING_GMODE) &&
2e92e6f2 589 (sband->bitrates[tx->rate_idx].flags & IEEE80211_RATE_ERP_G) &&
5cf121c3 590 (tx->flags & IEEE80211_TX_UNICAST) &&
471b3efd 591 tx->sdata->bss_conf.use_cts_prot &&
e039fa4a
JB
592 !(info->flags & IEEE80211_TX_CTL_USE_RTS_CTS))
593 info->flags |= IEEE80211_TX_CTL_USE_CTS_PROTECT;
e2ebc74d 594
7e9ed188
DD
595 /* Transmit data frames using short preambles if the driver supports
596 * short preambles at the selected rate and short preambles are
597 * available on the network at the current point in time. */
598 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
2e92e6f2 599 (sband->bitrates[tx->rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
471b3efd 600 tx->sdata->bss_conf.use_short_preamble &&
07346f81 601 (!tx->sta || test_sta_flags(tx->sta, WLAN_STA_SHORT_PREAMBLE))) {
e039fa4a 602 info->flags |= IEEE80211_TX_CTL_SHORT_PREAMBLE;
7e9ed188
DD
603 }
604
e2ebc74d
JB
605 /* Setup duration field for the first fragment of the frame. Duration
606 * for remaining fragments will be updated when they are being sent
607 * to low-level driver in ieee80211_tx(). */
608 dur = ieee80211_duration(tx, is_multicast_ether_addr(hdr->addr1),
5cf121c3
JB
609 (tx->flags & IEEE80211_TX_FRAGMENTED) ?
610 tx->extra_frag[0]->len : 0);
e2ebc74d
JB
611 hdr->duration_id = cpu_to_le16(dur);
612
e039fa4a
JB
613 if ((info->flags & IEEE80211_TX_CTL_USE_RTS_CTS) ||
614 (info->flags & IEEE80211_TX_CTL_USE_CTS_PROTECT)) {
2e92e6f2
JB
615 struct ieee80211_rate *rate;
616 s8 baserate = -1;
8318d78a
JB
617 int idx;
618
e2ebc74d 619 /* Do not use multiple retry rates when using RTS/CTS */
e039fa4a 620 info->control.alt_retry_rate_idx = -1;
e2ebc74d
JB
621
622 /* Use min(data rate, max base rate) as CTS/RTS rate */
2e92e6f2 623 rate = &sband->bitrates[tx->rate_idx];
8318d78a
JB
624
625 for (idx = 0; idx < sband->n_bitrates; idx++) {
626 if (sband->bitrates[idx].bitrate > rate->bitrate)
627 continue;
628 if (tx->sdata->basic_rates & BIT(idx) &&
2e92e6f2
JB
629 (baserate < 0 ||
630 (sband->bitrates[baserate].bitrate
631 < sband->bitrates[idx].bitrate)))
632 baserate = idx;
8318d78a 633 }
e2ebc74d 634
2e92e6f2 635 if (baserate >= 0)
e039fa4a 636 info->control.rts_cts_rate_idx = baserate;
8318d78a 637 else
e039fa4a 638 info->control.rts_cts_rate_idx = 0;
e2ebc74d
JB
639 }
640
e2454948 641 if (tx->sta)
e039fa4a 642 info->control.aid = tx->sta->aid;
e2454948
JB
643
644 return TX_CONTINUE;
645}
646
647static ieee80211_tx_result
648ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
649{
650 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
651 size_t hdrlen, per_fragm, num_fragm, payload_len, left;
652 struct sk_buff **frags, *first, *frag;
653 int i;
654 u16 seq;
655 u8 *pos;
656 int frag_threshold = tx->local->fragmentation_threshold;
657
658 if (!(tx->flags & IEEE80211_TX_FRAGMENTED))
659 return TX_CONTINUE;
660
eefce91a
JB
661 /*
662 * Warn when submitting a fragmented A-MPDU frame and drop it.
663 * This is an error and needs to be fixed elsewhere, but when
664 * done needs to take care of monitor interfaces (injection)
665 * etc.
666 */
667 if (WARN_ON(tx->flags & IEEE80211_TX_CTL_AMPDU ||
e2530083
JB
668 skb_get_queue_mapping(tx->skb) >=
669 ieee80211_num_regular_queues(&tx->local->hw)))
eefce91a
JB
670 return TX_DROP;
671
e2454948
JB
672 first = tx->skb;
673
674 hdrlen = ieee80211_get_hdrlen(tx->fc);
675 payload_len = first->len - hdrlen;
676 per_fragm = frag_threshold - hdrlen - FCS_LEN;
677 num_fragm = DIV_ROUND_UP(payload_len, per_fragm);
678
679 frags = kzalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
680 if (!frags)
681 goto fail;
682
683 hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
684 seq = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ;
685 pos = first->data + hdrlen + per_fragm;
686 left = payload_len - per_fragm;
687 for (i = 0; i < num_fragm - 1; i++) {
688 struct ieee80211_hdr *fhdr;
689 size_t copylen;
690
691 if (left <= 0)
692 goto fail;
693
694 /* reserve enough extra head and tail room for possible
695 * encryption */
696 frag = frags[i] =
697 dev_alloc_skb(tx->local->tx_headroom +
698 frag_threshold +
699 IEEE80211_ENCRYPT_HEADROOM +
700 IEEE80211_ENCRYPT_TAILROOM);
701 if (!frag)
702 goto fail;
703 /* Make sure that all fragments use the same priority so
704 * that they end up using the same TX queue */
705 frag->priority = first->priority;
706 skb_reserve(frag, tx->local->tx_headroom +
707 IEEE80211_ENCRYPT_HEADROOM);
708 fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
709 memcpy(fhdr, first->data, hdrlen);
710 if (i == num_fragm - 2)
711 fhdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_MOREFRAGS);
712 fhdr->seq_ctrl = cpu_to_le16(seq | ((i + 1) & IEEE80211_SCTL_FRAG));
713 copylen = left > per_fragm ? per_fragm : left;
714 memcpy(skb_put(frag, copylen), pos, copylen);
715
716 pos += copylen;
717 left -= copylen;
e2ebc74d 718 }
e2454948
JB
719 skb_trim(first, hdrlen + per_fragm);
720
721 tx->num_extra_frag = num_fragm - 1;
722 tx->extra_frag = frags;
e2ebc74d 723
9ae54c84 724 return TX_CONTINUE;
e2454948
JB
725
726 fail:
727 printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
728 if (frags) {
729 for (i = 0; i < num_fragm - 1; i++)
730 if (frags[i])
731 dev_kfree_skb(frags[i]);
732 kfree(frags);
733 }
734 I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
735 return TX_DROP;
e2ebc74d
JB
736}
737
9ae54c84 738static ieee80211_tx_result
e2454948
JB
739ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
740{
741 if (!tx->key)
742 return TX_CONTINUE;
743
744 switch (tx->key->conf.alg) {
745 case ALG_WEP:
746 return ieee80211_crypto_wep_encrypt(tx);
747 case ALG_TKIP:
748 return ieee80211_crypto_tkip_encrypt(tx);
749 case ALG_CCMP:
750 return ieee80211_crypto_ccmp_encrypt(tx);
751 }
752
753 /* not reached */
754 WARN_ON(1);
755 return TX_DROP;
756}
757
758static ieee80211_tx_result
759ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
e2ebc74d 760{
9e72ebd6 761 int i;
e2ebc74d 762
9e72ebd6
JB
763 if (!tx->sta)
764 return TX_CONTINUE;
e2ebc74d 765
9e72ebd6
JB
766 tx->sta->tx_packets++;
767 tx->sta->tx_fragments++;
768 tx->sta->tx_bytes += tx->skb->len;
5cf121c3 769 if (tx->extra_frag) {
9e72ebd6
JB
770 tx->sta->tx_fragments += tx->num_extra_frag;
771 for (i = 0; i < tx->num_extra_frag; i++)
772 tx->sta->tx_bytes += tx->extra_frag[i]->len;
e2454948
JB
773 }
774
9ae54c84 775 return TX_CONTINUE;
e2ebc74d
JB
776}
777
e2ebc74d 778
5cf121c3 779typedef ieee80211_tx_result (*ieee80211_tx_handler)(struct ieee80211_tx_data *);
58905290 780static ieee80211_tx_handler ieee80211_tx_handlers[] =
e2ebc74d
JB
781{
782 ieee80211_tx_h_check_assoc,
783 ieee80211_tx_h_sequence,
784 ieee80211_tx_h_ps_buf,
785 ieee80211_tx_h_select_key,
786 ieee80211_tx_h_michael_mic_add,
e2ebc74d
JB
787 ieee80211_tx_h_rate_ctrl,
788 ieee80211_tx_h_misc,
e2454948 789 ieee80211_tx_h_fragment,
e039fa4a 790 /* handlers after fragment must be aware of tx info fragmentation! */
e2454948
JB
791 ieee80211_tx_h_encrypt,
792 ieee80211_tx_h_stats,
e2ebc74d
JB
793 NULL
794};
795
796/* actual transmit path */
797
798/*
799 * deal with packet injection down monitor interface
800 * with Radiotap Header -- only called for monitor mode interface
801 */
9ae54c84 802static ieee80211_tx_result
5cf121c3 803__ieee80211_parse_tx_radiotap(struct ieee80211_tx_data *tx,
58d4185e 804 struct sk_buff *skb)
e2ebc74d
JB
805{
806 /*
807 * this is the moment to interpret and discard the radiotap header that
808 * must be at the start of the packet injected in Monitor mode
809 *
810 * Need to take some care with endian-ness since radiotap
811 * args are little-endian
812 */
813
814 struct ieee80211_radiotap_iterator iterator;
815 struct ieee80211_radiotap_header *rthdr =
816 (struct ieee80211_radiotap_header *) skb->data;
8318d78a 817 struct ieee80211_supported_band *sband;
e2ebc74d 818 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len);
e039fa4a 819 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e2ebc74d 820
2e92e6f2 821 sband = tx->local->hw.wiphy->bands[tx->channel->band];
8318d78a 822
e039fa4a
JB
823 info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
824 info->flags |= IEEE80211_TX_CTL_INJECTED;
5cf121c3 825 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
e2ebc74d
JB
826
827 /*
828 * for every radiotap entry that is present
829 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
830 * entries present, or -EINVAL on error)
831 */
832
833 while (!ret) {
834 int i, target_rate;
835
836 ret = ieee80211_radiotap_iterator_next(&iterator);
837
838 if (ret)
839 continue;
840
841 /* see if this argument is something we can use */
842 switch (iterator.this_arg_index) {
843 /*
844 * You must take care when dereferencing iterator.this_arg
845 * for multibyte types... the pointer is not aligned. Use
846 * get_unaligned((type *)iterator.this_arg) to dereference
847 * iterator.this_arg for type "type" safely on all arches.
848 */
849 case IEEE80211_RADIOTAP_RATE:
850 /*
851 * radiotap rate u8 is in 500kbps units eg, 0x02=1Mbps
852 * ieee80211 rate int is in 100kbps units eg, 0x0a=1Mbps
853 */
854 target_rate = (*iterator.this_arg) * 5;
8318d78a
JB
855 for (i = 0; i < sband->n_bitrates; i++) {
856 struct ieee80211_rate *r;
e2ebc74d 857
8318d78a
JB
858 r = &sband->bitrates[i];
859
860 if (r->bitrate == target_rate) {
2e92e6f2 861 tx->rate_idx = i;
58d4185e
JB
862 break;
863 }
e2ebc74d
JB
864 }
865 break;
866
867 case IEEE80211_RADIOTAP_ANTENNA:
868 /*
869 * radiotap uses 0 for 1st ant, mac80211 is 1 for
870 * 1st ant
871 */
e039fa4a 872 info->antenna_sel_tx = (*iterator.this_arg) + 1;
e2ebc74d
JB
873 break;
874
8318d78a 875#if 0
e2ebc74d
JB
876 case IEEE80211_RADIOTAP_DBM_TX_POWER:
877 control->power_level = *iterator.this_arg;
878 break;
8318d78a 879#endif
e2ebc74d
JB
880
881 case IEEE80211_RADIOTAP_FLAGS:
882 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
883 /*
884 * this indicates that the skb we have been
885 * handed has the 32-bit FCS CRC at the end...
886 * we should react to that by snipping it off
887 * because it will be recomputed and added
888 * on transmission
889 */
890 if (skb->len < (iterator.max_length + FCS_LEN))
9ae54c84 891 return TX_DROP;
e2ebc74d
JB
892
893 skb_trim(skb, skb->len - FCS_LEN);
894 }
58d4185e 895 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
e039fa4a
JB
896 info->flags &=
897 ~IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
58d4185e 898 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
5cf121c3 899 tx->flags |= IEEE80211_TX_FRAGMENTED;
e2ebc74d
JB
900 break;
901
58d4185e
JB
902 /*
903 * Please update the file
904 * Documentation/networking/mac80211-injection.txt
905 * when parsing new fields here.
906 */
907
e2ebc74d
JB
908 default:
909 break;
910 }
911 }
912
913 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
9ae54c84 914 return TX_DROP;
e2ebc74d
JB
915
916 /*
917 * remove the radiotap header
918 * iterator->max_length was sanity-checked against
919 * skb->len by iterator init
920 */
921 skb_pull(skb, iterator.max_length);
922
9ae54c84 923 return TX_CONTINUE;
e2ebc74d
JB
924}
925
58d4185e
JB
926/*
927 * initialises @tx
928 */
9ae54c84 929static ieee80211_tx_result
5cf121c3 930__ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
e2ebc74d 931 struct sk_buff *skb,
e039fa4a 932 struct net_device *dev)
e2ebc74d
JB
933{
934 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
58d4185e 935 struct ieee80211_hdr *hdr;
e2ebc74d 936 struct ieee80211_sub_if_data *sdata;
e039fa4a 937 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e2ebc74d
JB
938
939 int hdrlen;
940
941 memset(tx, 0, sizeof(*tx));
942 tx->skb = skb;
943 tx->dev = dev; /* use original interface */
944 tx->local = local;
945 tx->sdata = IEEE80211_DEV_TO_SUB_IF(dev);
e039fa4a 946 tx->channel = local->hw.conf.channel;
5854a32e
JB
947 tx->rate_idx = -1;
948 tx->last_frag_rate_idx = -1;
e2ebc74d 949 /*
58d4185e
JB
950 * Set this flag (used below to indicate "automatic fragmentation"),
951 * it will be cleared/left by radiotap as desired.
e2ebc74d 952 */
5cf121c3 953 tx->flags |= IEEE80211_TX_FRAGMENTED;
e2ebc74d
JB
954
955 /* process and remove the injection radiotap header */
956 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
51fb61e7 957 if (unlikely(sdata->vif.type == IEEE80211_IF_TYPE_MNTR)) {
9ae54c84
JB
958 if (__ieee80211_parse_tx_radiotap(tx, skb) == TX_DROP)
959 return TX_DROP;
58d4185e 960
e2ebc74d 961 /*
58d4185e
JB
962 * __ieee80211_parse_tx_radiotap has now removed
963 * the radiotap header that was present and pre-filled
964 * 'tx' with tx control information.
e2ebc74d 965 */
e2ebc74d
JB
966 }
967
58d4185e
JB
968 hdr = (struct ieee80211_hdr *) skb->data;
969
24338793 970 tx->sta = sta_info_get(local, hdr->addr1);
971 tx->fc = le16_to_cpu(hdr->frame_control);
58d4185e 972
badffb72 973 if (is_multicast_ether_addr(hdr->addr1)) {
5cf121c3 974 tx->flags &= ~IEEE80211_TX_UNICAST;
e039fa4a 975 info->flags |= IEEE80211_TX_CTL_NO_ACK;
badffb72 976 } else {
5cf121c3 977 tx->flags |= IEEE80211_TX_UNICAST;
e039fa4a 978 info->flags &= ~IEEE80211_TX_CTL_NO_ACK;
badffb72 979 }
58d4185e 980
5cf121c3
JB
981 if (tx->flags & IEEE80211_TX_FRAGMENTED) {
982 if ((tx->flags & IEEE80211_TX_UNICAST) &&
58d4185e
JB
983 skb->len + FCS_LEN > local->fragmentation_threshold &&
984 !local->ops->set_frag_threshold)
5cf121c3 985 tx->flags |= IEEE80211_TX_FRAGMENTED;
58d4185e 986 else
5cf121c3 987 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
58d4185e
JB
988 }
989
e2ebc74d 990 if (!tx->sta)
e039fa4a 991 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
07346f81 992 else if (test_and_clear_sta_flags(tx->sta, WLAN_STA_CLEAR_PS_FILT))
e039fa4a 993 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
58d4185e 994
e2ebc74d
JB
995 hdrlen = ieee80211_get_hdrlen(tx->fc);
996 if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
997 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
998 tx->ethertype = (pos[0] << 8) | pos[1];
999 }
e039fa4a 1000 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
e2ebc74d 1001
9ae54c84 1002 return TX_CONTINUE;
e2ebc74d
JB
1003}
1004
32bfd35d 1005/*
58d4185e
JB
1006 * NB: @tx is uninitialised when passed in here
1007 */
5cf121c3 1008static int ieee80211_tx_prepare(struct ieee80211_tx_data *tx,
58d4185e 1009 struct sk_buff *skb,
e039fa4a 1010 struct net_device *mdev)
e2ebc74d 1011{
e039fa4a 1012 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e2ebc74d
JB
1013 struct net_device *dev;
1014
e039fa4a 1015 dev = dev_get_by_index(&init_net, info->control.ifindex);
e2ebc74d
JB
1016 if (unlikely(dev && !is_ieee80211_device(dev, mdev))) {
1017 dev_put(dev);
1018 dev = NULL;
1019 }
1020 if (unlikely(!dev))
1021 return -ENODEV;
58d4185e 1022 /* initialises tx with control */
e039fa4a 1023 __ieee80211_tx_prepare(tx, skb, dev);
32bfd35d 1024 dev_put(dev);
e2ebc74d
JB
1025 return 0;
1026}
1027
1028static int __ieee80211_tx(struct ieee80211_local *local, struct sk_buff *skb,
5cf121c3 1029 struct ieee80211_tx_data *tx)
e2ebc74d 1030{
e039fa4a 1031 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e2ebc74d
JB
1032 int ret, i;
1033
e2530083 1034 if (netif_subqueue_stopped(local->mdev, skb))
e2ebc74d 1035 return IEEE80211_TX_AGAIN;
e2530083 1036
e2ebc74d 1037 if (skb) {
dd1cd4c6
JB
1038 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
1039 "TX to low-level driver", skb);
e039fa4a 1040 ret = local->ops->tx(local_to_hw(local), skb);
e2ebc74d
JB
1041 if (ret)
1042 return IEEE80211_TX_AGAIN;
1043 local->mdev->trans_start = jiffies;
1044 ieee80211_led_tx(local, 1);
1045 }
5cf121c3 1046 if (tx->extra_frag) {
5cf121c3
JB
1047 for (i = 0; i < tx->num_extra_frag; i++) {
1048 if (!tx->extra_frag[i])
e2ebc74d 1049 continue;
e039fa4a
JB
1050 info = IEEE80211_SKB_CB(tx->extra_frag[i]);
1051 info->flags &= ~(IEEE80211_TX_CTL_USE_RTS_CTS |
1052 IEEE80211_TX_CTL_USE_CTS_PROTECT |
1053 IEEE80211_TX_CTL_CLEAR_PS_FILT |
1054 IEEE80211_TX_CTL_FIRST_FRAGMENT);
e2530083
JB
1055 if (netif_subqueue_stopped(local->mdev,
1056 tx->extra_frag[i]))
e2ebc74d 1057 return IEEE80211_TX_FRAG_AGAIN;
5cf121c3 1058 if (i == tx->num_extra_frag) {
e039fa4a 1059 info->tx_rate_idx = tx->last_frag_rate_idx;
8318d78a 1060
5cf121c3 1061 if (tx->flags & IEEE80211_TX_PROBE_LAST_FRAG)
e039fa4a
JB
1062 info->flags |=
1063 IEEE80211_TX_CTL_RATE_CTRL_PROBE;
e2ebc74d 1064 else
e039fa4a
JB
1065 info->flags &=
1066 ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
e2ebc74d
JB
1067 }
1068
dd1cd4c6 1069 ieee80211_dump_frame(wiphy_name(local->hw.wiphy),
e2ebc74d 1070 "TX to low-level driver",
5cf121c3 1071 tx->extra_frag[i]);
e2ebc74d 1072 ret = local->ops->tx(local_to_hw(local),
e039fa4a 1073 tx->extra_frag[i]);
e2ebc74d
JB
1074 if (ret)
1075 return IEEE80211_TX_FRAG_AGAIN;
1076 local->mdev->trans_start = jiffies;
1077 ieee80211_led_tx(local, 1);
5cf121c3 1078 tx->extra_frag[i] = NULL;
e2ebc74d 1079 }
5cf121c3
JB
1080 kfree(tx->extra_frag);
1081 tx->extra_frag = NULL;
e2ebc74d
JB
1082 }
1083 return IEEE80211_TX_OK;
1084}
1085
e039fa4a 1086static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb)
e2ebc74d
JB
1087{
1088 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1089 struct sta_info *sta;
1090 ieee80211_tx_handler *handler;
5cf121c3 1091 struct ieee80211_tx_data tx;
9ae54c84 1092 ieee80211_tx_result res = TX_DROP, res_prepare;
e039fa4a 1093 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e2ebc74d 1094 int ret, i;
e2530083 1095 u16 queue;
e2ebc74d 1096
e2530083
JB
1097 queue = skb_get_queue_mapping(skb);
1098
1099 WARN_ON(test_bit(queue, local->queues_pending));
e2ebc74d
JB
1100
1101 if (unlikely(skb->len < 10)) {
1102 dev_kfree_skb(skb);
1103 return 0;
1104 }
1105
d0709a65
JB
1106 rcu_read_lock();
1107
58d4185e 1108 /* initialises tx */
e039fa4a 1109 res_prepare = __ieee80211_tx_prepare(&tx, skb, dev);
e2ebc74d 1110
9ae54c84 1111 if (res_prepare == TX_DROP) {
e2ebc74d 1112 dev_kfree_skb(skb);
d0709a65 1113 rcu_read_unlock();
e2ebc74d
JB
1114 return 0;
1115 }
1116
1117 sta = tx.sta;
5cf121c3 1118 tx.channel = local->hw.conf.channel;
e039fa4a 1119 info->band = tx.channel->band;
e2ebc74d 1120
58905290 1121 for (handler = ieee80211_tx_handlers; *handler != NULL;
58d4185e
JB
1122 handler++) {
1123 res = (*handler)(&tx);
9ae54c84 1124 if (res != TX_CONTINUE)
58d4185e 1125 break;
e2ebc74d
JB
1126 }
1127
e039fa4a
JB
1128 if (WARN_ON(tx.skb != skb))
1129 goto drop;
e2ebc74d 1130
9ae54c84 1131 if (unlikely(res == TX_DROP)) {
e2ebc74d
JB
1132 I802_DEBUG_INC(local->tx_handlers_drop);
1133 goto drop;
1134 }
1135
9ae54c84 1136 if (unlikely(res == TX_QUEUED)) {
e2ebc74d 1137 I802_DEBUG_INC(local->tx_handlers_queued);
d4e46a3d 1138 rcu_read_unlock();
e2ebc74d
JB
1139 return 0;
1140 }
1141
5cf121c3
JB
1142 if (tx.extra_frag) {
1143 for (i = 0; i < tx.num_extra_frag; i++) {
e2ebc74d
JB
1144 int next_len, dur;
1145 struct ieee80211_hdr *hdr =
1146 (struct ieee80211_hdr *)
5cf121c3 1147 tx.extra_frag[i]->data;
e2ebc74d 1148
5cf121c3
JB
1149 if (i + 1 < tx.num_extra_frag) {
1150 next_len = tx.extra_frag[i + 1]->len;
e2ebc74d
JB
1151 } else {
1152 next_len = 0;
2e92e6f2 1153 tx.rate_idx = tx.last_frag_rate_idx;
e2ebc74d
JB
1154 }
1155 dur = ieee80211_duration(&tx, 0, next_len);
1156 hdr->duration_id = cpu_to_le16(dur);
1157 }
1158 }
1159
1160retry:
1161 ret = __ieee80211_tx(local, skb, &tx);
1162 if (ret) {
eefce91a
JB
1163 struct ieee80211_tx_stored_packet *store;
1164
1165 /*
1166 * Since there are no fragmented frames on A-MPDU
1167 * queues, there's no reason for a driver to reject
1168 * a frame there, warn and drop it.
1169 */
e2530083 1170 if (WARN_ON(queue >= ieee80211_num_regular_queues(&local->hw)))
eefce91a
JB
1171 goto drop;
1172
1173 store = &local->pending_packet[queue];
e2ebc74d
JB
1174
1175 if (ret == IEEE80211_TX_FRAG_AGAIN)
1176 skb = NULL;
e2530083 1177 set_bit(queue, local->queues_pending);
e2ebc74d 1178 smp_mb();
e2530083
JB
1179 /*
1180 * When the driver gets out of buffers during sending of
1181 * fragments and calls ieee80211_stop_queue, the netif
1182 * subqueue is stopped. There is, however, a small window
1183 * in which the PENDING bit is not yet set. If a buffer
e2ebc74d
JB
1184 * gets available in that window (i.e. driver calls
1185 * ieee80211_wake_queue), we would end up with ieee80211_tx
e2530083 1186 * called with the PENDING bit still set. Prevent this by
e2ebc74d 1187 * continuing transmitting here when that situation is
e2530083
JB
1188 * possible to have happened.
1189 */
1190 if (!__netif_subqueue_stopped(local->mdev, queue)) {
1191 clear_bit(queue, local->queues_pending);
e2ebc74d
JB
1192 goto retry;
1193 }
e2ebc74d 1194 store->skb = skb;
5cf121c3
JB
1195 store->extra_frag = tx.extra_frag;
1196 store->num_extra_frag = tx.num_extra_frag;
2e92e6f2 1197 store->last_frag_rate_idx = tx.last_frag_rate_idx;
badffb72 1198 store->last_frag_rate_ctrl_probe =
5cf121c3 1199 !!(tx.flags & IEEE80211_TX_PROBE_LAST_FRAG);
e2ebc74d 1200 }
d4e46a3d 1201 rcu_read_unlock();
e2ebc74d
JB
1202 return 0;
1203
1204 drop:
1205 if (skb)
1206 dev_kfree_skb(skb);
5cf121c3
JB
1207 for (i = 0; i < tx.num_extra_frag; i++)
1208 if (tx.extra_frag[i])
1209 dev_kfree_skb(tx.extra_frag[i]);
1210 kfree(tx.extra_frag);
d4e46a3d 1211 rcu_read_unlock();
e2ebc74d
JB
1212 return 0;
1213}
1214
1215/* device xmit handlers */
1216
23c0752a
JB
1217static int ieee80211_skb_resize(struct ieee80211_local *local,
1218 struct sk_buff *skb,
1219 int head_need, bool may_encrypt)
1220{
1221 int tail_need = 0;
1222
1223 /*
1224 * This could be optimised, devices that do full hardware
1225 * crypto (including TKIP MMIC) need no tailroom... But we
1226 * have no drivers for such devices currently.
1227 */
1228 if (may_encrypt) {
1229 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1230 tail_need -= skb_tailroom(skb);
1231 tail_need = max_t(int, tail_need, 0);
1232 }
1233
1234 if (head_need || tail_need) {
1235 /* Sorry. Can't account for this any more */
1236 skb_orphan(skb);
1237 }
1238
1239 if (skb_header_cloned(skb))
1240 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1241 else
1242 I802_DEBUG_INC(local->tx_expand_skb_head);
1243
1244 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1245 printk(KERN_DEBUG "%s: failed to reallocate TX buffer\n",
1246 wiphy_name(local->hw.wiphy));
1247 return -ENOMEM;
1248 }
1249
1250 /* update truesize too */
1251 skb->truesize += head_need + tail_need;
1252
1253 return 0;
1254}
1255
e2ebc74d
JB
1256int ieee80211_master_start_xmit(struct sk_buff *skb,
1257 struct net_device *dev)
1258{
e039fa4a 1259 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e2ebc74d
JB
1260 struct net_device *odev = NULL;
1261 struct ieee80211_sub_if_data *osdata;
1262 int headroom;
23c0752a 1263 bool may_encrypt;
e2ebc74d
JB
1264 int ret;
1265
e039fa4a
JB
1266 if (info->control.ifindex)
1267 odev = dev_get_by_index(&init_net, info->control.ifindex);
e2ebc74d
JB
1268 if (unlikely(odev && !is_ieee80211_device(odev, dev))) {
1269 dev_put(odev);
1270 odev = NULL;
1271 }
1272 if (unlikely(!odev)) {
1273#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1274 printk(KERN_DEBUG "%s: Discarded packet with nonexistent "
1275 "originating device\n", dev->name);
1276#endif
1277 dev_kfree_skb(skb);
1278 return 0;
1279 }
e039fa4a 1280
e2ebc74d
JB
1281 osdata = IEEE80211_DEV_TO_SUB_IF(odev);
1282
23c0752a
JB
1283 may_encrypt = !(info->flags & IEEE80211_TX_CTL_DO_NOT_ENCRYPT);
1284
1285 headroom = osdata->local->tx_headroom;
1286 if (may_encrypt)
1287 headroom += IEEE80211_ENCRYPT_HEADROOM;
1288 headroom -= skb_headroom(skb);
1289 headroom = max_t(int, 0, headroom);
1290
1291 if (ieee80211_skb_resize(osdata->local, skb, headroom, may_encrypt)) {
1292 dev_kfree_skb(skb);
1293 dev_put(odev);
1294 return 0;
e2ebc74d
JB
1295 }
1296
e039fa4a
JB
1297 info->control.vif = &osdata->vif;
1298 ret = ieee80211_tx(odev, skb);
e2ebc74d
JB
1299 dev_put(odev);
1300
1301 return ret;
1302}
1303
1304int ieee80211_monitor_start_xmit(struct sk_buff *skb,
1305 struct net_device *dev)
1306{
1307 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
e039fa4a 1308 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
e2ebc74d
JB
1309 struct ieee80211_radiotap_header *prthdr =
1310 (struct ieee80211_radiotap_header *)skb->data;
9b8a74e3 1311 u16 len_rthdr;
e2ebc74d 1312
9b8a74e3
AG
1313 /* check for not even having the fixed radiotap header part */
1314 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1315 goto fail; /* too short to be possibly valid */
1316
1317 /* is it a header version we can trust to find length from? */
1318 if (unlikely(prthdr->it_version))
1319 goto fail; /* only version 0 is supported */
1320
1321 /* then there must be a radiotap header with a length we can use */
1322 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1323
1324 /* does the skb contain enough to deliver on the alleged length? */
1325 if (unlikely(skb->len < len_rthdr))
1326 goto fail; /* skb too short for claimed rt header extent */
e2ebc74d
JB
1327
1328 skb->dev = local->mdev;
1329
9b8a74e3 1330 /* needed because we set skb device to master */
e039fa4a 1331 info->control.ifindex = dev->ifindex;
9b8a74e3 1332
e039fa4a 1333 info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
b0a67179 1334 /* Interfaces should always request a status report */
e039fa4a 1335 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
e2ebc74d 1336
e2ebc74d
JB
1337 /*
1338 * fix up the pointers accounting for the radiotap
1339 * header still being in there. We are being given
1340 * a precooked IEEE80211 header so no need for
1341 * normal processing
1342 */
9b8a74e3 1343 skb_set_mac_header(skb, len_rthdr);
e2ebc74d 1344 /*
9b8a74e3
AG
1345 * these are just fixed to the end of the rt area since we
1346 * don't have any better information and at this point, nobody cares
e2ebc74d 1347 */
9b8a74e3
AG
1348 skb_set_network_header(skb, len_rthdr);
1349 skb_set_transport_header(skb, len_rthdr);
e2ebc74d 1350
9b8a74e3
AG
1351 /* pass the radiotap header up to the next stage intact */
1352 dev_queue_xmit(skb);
e2ebc74d 1353 return NETDEV_TX_OK;
9b8a74e3
AG
1354
1355fail:
1356 dev_kfree_skb(skb);
1357 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
e2ebc74d
JB
1358}
1359
1360/**
1361 * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1362 * subinterfaces (wlan#, WDS, and VLAN interfaces)
1363 * @skb: packet to be sent
1364 * @dev: incoming interface
1365 *
1366 * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1367 * not be freed, and caller is responsible for either retrying later or freeing
1368 * skb).
1369 *
1370 * This function takes in an Ethernet header and encapsulates it with suitable
1371 * IEEE 802.11 header based on which interface the packet is coming in. The
1372 * encapsulated packet will then be passed to master interface, wlan#.11, for
1373 * transmission (through low-level driver).
1374 */
1375int ieee80211_subif_start_xmit(struct sk_buff *skb,
1376 struct net_device *dev)
1377{
1378 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
e039fa4a 1379 struct ieee80211_tx_info *info;
e2ebc74d
JB
1380 struct ieee80211_sub_if_data *sdata;
1381 int ret = 1, head_need;
33b64eb2 1382 u16 ethertype, hdrlen, meshhdrlen = 0, fc;
e2ebc74d 1383 struct ieee80211_hdr hdr;
33b64eb2 1384 struct ieee80211s_hdr mesh_hdr;
e2ebc74d
JB
1385 const u8 *encaps_data;
1386 int encaps_len, skip_header_bytes;
e8bf9649 1387 int nh_pos, h_pos;
e2ebc74d 1388 struct sta_info *sta;
ce3edf6d 1389 u32 sta_flags = 0;
e2ebc74d
JB
1390
1391 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1392 if (unlikely(skb->len < ETH_HLEN)) {
1393 printk(KERN_DEBUG "%s: short skb (len=%d)\n",
1394 dev->name, skb->len);
1395 ret = 0;
1396 goto fail;
1397 }
1398
1399 nh_pos = skb_network_header(skb) - skb->data;
1400 h_pos = skb_transport_header(skb) - skb->data;
1401
1402 /* convert Ethernet header to proper 802.11 header (based on
1403 * operation mode) */
1404 ethertype = (skb->data[12] << 8) | skb->data[13];
e2ebc74d
JB
1405 fc = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA;
1406
51fb61e7 1407 switch (sdata->vif.type) {
cf966838
JB
1408 case IEEE80211_IF_TYPE_AP:
1409 case IEEE80211_IF_TYPE_VLAN:
e2ebc74d
JB
1410 fc |= IEEE80211_FCTL_FROMDS;
1411 /* DA BSSID SA */
1412 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1413 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1414 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1415 hdrlen = 24;
cf966838
JB
1416 break;
1417 case IEEE80211_IF_TYPE_WDS:
e2ebc74d
JB
1418 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1419 /* RA TA DA SA */
1420 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1421 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1422 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1423 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1424 hdrlen = 30;
cf966838 1425 break;
33b64eb2
LCC
1426#ifdef CONFIG_MAC80211_MESH
1427 case IEEE80211_IF_TYPE_MESH_POINT:
1428 fc |= IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS;
1429 /* RA TA DA SA */
1430 if (is_multicast_ether_addr(skb->data))
1431 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1432 else if (mesh_nexthop_lookup(hdr.addr1, skb, dev))
1433 return 0;
1434 memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
1435 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1436 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1437 if (skb->pkt_type == PACKET_OTHERHOST) {
1438 /* Forwarded frame, keep mesh ttl and seqnum */
1439 struct ieee80211s_hdr *prev_meshhdr;
1440 prev_meshhdr = ((struct ieee80211s_hdr *)skb->cb);
1441 meshhdrlen = ieee80211_get_mesh_hdrlen(prev_meshhdr);
1442 memcpy(&mesh_hdr, prev_meshhdr, meshhdrlen);
1443 sdata->u.sta.mshstats.fwded_frames++;
1444 } else {
1445 if (!sdata->u.sta.mshcfg.dot11MeshTTL) {
1446 /* Do not send frames with mesh_ttl == 0 */
1447 sdata->u.sta.mshstats.dropped_frames_ttl++;
1448 ret = 0;
1449 goto fail;
1450 }
1451 meshhdrlen = ieee80211_new_mesh_header(&mesh_hdr,
902acc78 1452 sdata);
33b64eb2
LCC
1453 }
1454 hdrlen = 30;
1455 break;
1456#endif
cf966838 1457 case IEEE80211_IF_TYPE_STA:
e2ebc74d
JB
1458 fc |= IEEE80211_FCTL_TODS;
1459 /* BSSID SA DA */
1460 memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
1461 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1462 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1463 hdrlen = 24;
cf966838
JB
1464 break;
1465 case IEEE80211_IF_TYPE_IBSS:
e2ebc74d
JB
1466 /* DA SA BSSID */
1467 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1468 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1469 memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
1470 hdrlen = 24;
cf966838
JB
1471 break;
1472 default:
e2ebc74d
JB
1473 ret = 0;
1474 goto fail;
1475 }
1476
7d185b8b
JB
1477 /*
1478 * There's no need to try to look up the destination
1479 * if it is a multicast address (which can only happen
1480 * in AP mode)
1481 */
1482 if (!is_multicast_ether_addr(hdr.addr1)) {
d0709a65 1483 rcu_read_lock();
7d185b8b 1484 sta = sta_info_get(local, hdr.addr1);
d0709a65 1485 if (sta)
07346f81 1486 sta_flags = get_sta_flags(sta);
d0709a65 1487 rcu_read_unlock();
e2ebc74d
JB
1488 }
1489
3434fbd3 1490 /* receiver and we are QoS enabled, use a QoS type frame */
e2530083
JB
1491 if (sta_flags & WLAN_STA_WME &&
1492 ieee80211_num_regular_queues(&local->hw) >= 4) {
ce3edf6d
JB
1493 fc |= IEEE80211_STYPE_QOS_DATA;
1494 hdrlen += 2;
1495 }
1496
1497 /*
238814fd
JB
1498 * Drop unicast frames to unauthorised stations unless they are
1499 * EAPOL frames from the local station.
ce3edf6d 1500 */
238814fd 1501 if (unlikely(!is_multicast_ether_addr(hdr.addr1) &&
33b64eb2
LCC
1502 !(sta_flags & WLAN_STA_AUTHORIZED) &&
1503 !(ethertype == ETH_P_PAE &&
ce3edf6d
JB
1504 compare_ether_addr(dev->dev_addr,
1505 skb->data + ETH_ALEN) == 0))) {
1506#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1507 DECLARE_MAC_BUF(mac);
1508
1509 if (net_ratelimit())
1510 printk(KERN_DEBUG "%s: dropped frame to %s"
1511 " (unauthorized port)\n", dev->name,
1512 print_mac(mac, hdr.addr1));
1513#endif
1514
1515 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
1516
1517 ret = 0;
1518 goto fail;
1519 }
1520
e2ebc74d
JB
1521 hdr.frame_control = cpu_to_le16(fc);
1522 hdr.duration_id = 0;
1523 hdr.seq_ctrl = 0;
1524
1525 skip_header_bytes = ETH_HLEN;
1526 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1527 encaps_data = bridge_tunnel_header;
1528 encaps_len = sizeof(bridge_tunnel_header);
1529 skip_header_bytes -= 2;
1530 } else if (ethertype >= 0x600) {
1531 encaps_data = rfc1042_header;
1532 encaps_len = sizeof(rfc1042_header);
1533 skip_header_bytes -= 2;
1534 } else {
1535 encaps_data = NULL;
1536 encaps_len = 0;
1537 }
1538
1539 skb_pull(skb, skip_header_bytes);
1540 nh_pos -= skip_header_bytes;
1541 h_pos -= skip_header_bytes;
1542
1543 /* TODO: implement support for fragments so that there is no need to
1544 * reallocate and copy payload; it might be enough to support one
1545 * extra fragment that would be copied in the beginning of the frame
1546 * data.. anyway, it would be nice to include this into skb structure
1547 * somehow
1548 *
1549 * There are few options for this:
1550 * use skb->cb as an extra space for 802.11 header
1551 * allocate new buffer if not enough headroom
1552 * make sure that there is enough headroom in every skb by increasing
1553 * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
1554 * alloc_skb() (net/core/skbuff.c)
1555 */
23c0752a 1556 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
e2ebc74d 1557
23c0752a
JB
1558 /*
1559 * So we need to modify the skb header and hence need a copy of
1560 * that. The head_need variable above doesn't, so far, include
1561 * the needed header space that we don't need right away. If we
1562 * can, then we don't reallocate right now but only after the
1563 * frame arrives at the master device (if it does...)
1564 *
1565 * If we cannot, however, then we will reallocate to include all
1566 * the ever needed space. Also, if we need to reallocate it anyway,
1567 * make it big enough for everything we may ever need.
1568 */
e2ebc74d 1569
608961a5 1570 if (head_need > 0 || skb_header_cloned(skb)) {
23c0752a
JB
1571 head_need += IEEE80211_ENCRYPT_HEADROOM;
1572 head_need += local->tx_headroom;
1573 head_need = max_t(int, 0, head_need);
1574 if (ieee80211_skb_resize(local, skb, head_need, true))
e2ebc74d 1575 goto fail;
e2ebc74d
JB
1576 }
1577
1578 if (encaps_data) {
1579 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1580 nh_pos += encaps_len;
1581 h_pos += encaps_len;
1582 }
c29b9b9b 1583
33b64eb2
LCC
1584 if (meshhdrlen > 0) {
1585 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
1586 nh_pos += meshhdrlen;
1587 h_pos += meshhdrlen;
1588 }
1589
c29b9b9b
JB
1590 if (fc & IEEE80211_STYPE_QOS_DATA) {
1591 __le16 *qos_control;
1592
1593 qos_control = (__le16*) skb_push(skb, 2);
1594 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
1595 /*
1596 * Maybe we could actually set some fields here, for now just
1597 * initialise to zero to indicate no special operation.
1598 */
1599 *qos_control = 0;
1600 } else
1601 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1602
e2ebc74d
JB
1603 nh_pos += hdrlen;
1604 h_pos += hdrlen;
1605
e039fa4a
JB
1606 info = IEEE80211_SKB_CB(skb);
1607 memset(info, 0, sizeof(*info));
1608 info->control.ifindex = dev->ifindex;
678f5f71 1609 if (ethertype == ETH_P_PAE)
e039fa4a 1610 info->flags |= IEEE80211_TX_CTL_EAPOL_FRAME;
e2ebc74d 1611
b0a67179 1612 /* Interfaces should always request a status report */
e039fa4a 1613 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
b0a67179 1614
e2ebc74d 1615 skb->dev = local->mdev;
68aae116
SH
1616 dev->stats.tx_packets++;
1617 dev->stats.tx_bytes += skb->len;
e2ebc74d
JB
1618
1619 /* Update skb pointers to various headers since this modified frame
1620 * is going to go through Linux networking code that may potentially
1621 * need things like pointer to IP header. */
1622 skb_set_mac_header(skb, 0);
1623 skb_set_network_header(skb, nh_pos);
1624 skb_set_transport_header(skb, h_pos);
1625
1626 dev->trans_start = jiffies;
1627 dev_queue_xmit(skb);
1628
1629 return 0;
1630
1631 fail:
1632 if (!ret)
1633 dev_kfree_skb(skb);
1634
1635 return ret;
1636}
1637
e2ebc74d 1638
e2530083
JB
1639/*
1640 * ieee80211_clear_tx_pending may not be called in a context where
1641 * it is possible that it packets could come in again.
1642 */
e2ebc74d
JB
1643void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1644{
1645 int i, j;
1646 struct ieee80211_tx_stored_packet *store;
1647
e2530083
JB
1648 for (i = 0; i < ieee80211_num_regular_queues(&local->hw); i++) {
1649 if (!test_bit(i, local->queues_pending))
e2ebc74d
JB
1650 continue;
1651 store = &local->pending_packet[i];
1652 kfree_skb(store->skb);
1653 for (j = 0; j < store->num_extra_frag; j++)
1654 kfree_skb(store->extra_frag[j]);
1655 kfree(store->extra_frag);
e2530083 1656 clear_bit(i, local->queues_pending);
e2ebc74d
JB
1657 }
1658}
1659
e2530083
JB
1660/*
1661 * Transmit all pending packets. Called from tasklet, locks master device
1662 * TX lock so that no new packets can come in.
1663 */
e2ebc74d
JB
1664void ieee80211_tx_pending(unsigned long data)
1665{
1666 struct ieee80211_local *local = (struct ieee80211_local *)data;
1667 struct net_device *dev = local->mdev;
1668 struct ieee80211_tx_stored_packet *store;
5cf121c3 1669 struct ieee80211_tx_data tx;
e2530083 1670 int i, ret;
e2ebc74d
JB
1671
1672 netif_tx_lock_bh(dev);
e2530083
JB
1673 for (i = 0; i < ieee80211_num_regular_queues(&local->hw); i++) {
1674 /* Check that this queue is ok */
1675 if (__netif_subqueue_stopped(local->mdev, i))
e2ebc74d 1676 continue;
e2530083
JB
1677
1678 if (!test_bit(i, local->queues_pending)) {
1679 ieee80211_wake_queue(&local->hw, i);
e2ebc74d
JB
1680 continue;
1681 }
e2530083 1682
e2ebc74d 1683 store = &local->pending_packet[i];
5cf121c3
JB
1684 tx.extra_frag = store->extra_frag;
1685 tx.num_extra_frag = store->num_extra_frag;
2e92e6f2 1686 tx.last_frag_rate_idx = store->last_frag_rate_idx;
badffb72
JS
1687 tx.flags = 0;
1688 if (store->last_frag_rate_ctrl_probe)
5cf121c3 1689 tx.flags |= IEEE80211_TX_PROBE_LAST_FRAG;
e2ebc74d
JB
1690 ret = __ieee80211_tx(local, store->skb, &tx);
1691 if (ret) {
1692 if (ret == IEEE80211_TX_FRAG_AGAIN)
1693 store->skb = NULL;
1694 } else {
e2530083
JB
1695 clear_bit(i, local->queues_pending);
1696 ieee80211_wake_queue(&local->hw, i);
e2ebc74d
JB
1697 }
1698 }
1699 netif_tx_unlock_bh(dev);
e2ebc74d
JB
1700}
1701
1702/* functions for drivers to get certain frames */
1703
1704static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
1705 struct ieee80211_if_ap *bss,
5dfdaf58
JB
1706 struct sk_buff *skb,
1707 struct beacon_data *beacon)
e2ebc74d
JB
1708{
1709 u8 *pos, *tim;
1710 int aid0 = 0;
1711 int i, have_bits = 0, n1, n2;
1712
1713 /* Generate bitmap for TIM only if there are any STAs in power save
1714 * mode. */
e2ebc74d
JB
1715 if (atomic_read(&bss->num_sta_ps) > 0)
1716 /* in the hope that this is faster than
1717 * checking byte-for-byte */
1718 have_bits = !bitmap_empty((unsigned long*)bss->tim,
1719 IEEE80211_MAX_AID+1);
1720
1721 if (bss->dtim_count == 0)
5dfdaf58 1722 bss->dtim_count = beacon->dtim_period - 1;
e2ebc74d
JB
1723 else
1724 bss->dtim_count--;
1725
1726 tim = pos = (u8 *) skb_put(skb, 6);
1727 *pos++ = WLAN_EID_TIM;
1728 *pos++ = 4;
1729 *pos++ = bss->dtim_count;
5dfdaf58 1730 *pos++ = beacon->dtim_period;
e2ebc74d
JB
1731
1732 if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
1733 aid0 = 1;
1734
1735 if (have_bits) {
1736 /* Find largest even number N1 so that bits numbered 1 through
1737 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
1738 * (N2 + 1) x 8 through 2007 are 0. */
1739 n1 = 0;
1740 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
1741 if (bss->tim[i]) {
1742 n1 = i & 0xfe;
1743 break;
1744 }
1745 }
1746 n2 = n1;
1747 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
1748 if (bss->tim[i]) {
1749 n2 = i;
1750 break;
1751 }
1752 }
1753
1754 /* Bitmap control */
1755 *pos++ = n1 | aid0;
1756 /* Part Virt Bitmap */
1757 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
1758
1759 tim[1] = n2 - n1 + 4;
1760 skb_put(skb, n2 - n1);
1761 } else {
1762 *pos++ = aid0; /* Bitmap control */
1763 *pos++ = 0; /* Part Virt Bitmap */
1764 }
e2ebc74d
JB
1765}
1766
32bfd35d 1767struct sk_buff *ieee80211_beacon_get(struct ieee80211_hw *hw,
e039fa4a 1768 struct ieee80211_vif *vif)
e2ebc74d
JB
1769{
1770 struct ieee80211_local *local = hw_to_local(hw);
1771 struct sk_buff *skb;
e039fa4a 1772 struct ieee80211_tx_info *info;
e2ebc74d
JB
1773 struct net_device *bdev;
1774 struct ieee80211_sub_if_data *sdata = NULL;
1775 struct ieee80211_if_ap *ap = NULL;
1abbe498 1776 struct rate_selection rsel;
5dfdaf58 1777 struct beacon_data *beacon;
8318d78a 1778 struct ieee80211_supported_band *sband;
902acc78 1779 struct ieee80211_mgmt *mgmt;
33b64eb2 1780 int *num_beacons;
902acc78 1781 bool err = true;
2e92e6f2 1782 enum ieee80211_band band = local->hw.conf.channel->band;
902acc78 1783 u8 *pos;
8318d78a 1784
2e92e6f2 1785 sband = local->hw.wiphy->bands[band];
5dfdaf58
JB
1786
1787 rcu_read_lock();
e2ebc74d 1788
32bfd35d
JB
1789 sdata = vif_to_sdata(vif);
1790 bdev = sdata->dev;
e2ebc74d 1791
902acc78 1792 if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
33b64eb2
LCC
1793 ap = &sdata->u.ap;
1794 beacon = rcu_dereference(ap->beacon);
902acc78
JB
1795 if (ap && beacon) {
1796 /*
1797 * headroom, head length,
1798 * tail length and maximum TIM length
1799 */
1800 skb = dev_alloc_skb(local->tx_headroom +
1801 beacon->head_len +
1802 beacon->tail_len + 256);
1803 if (!skb)
1804 goto out;
33b64eb2 1805
902acc78
JB
1806 skb_reserve(skb, local->tx_headroom);
1807 memcpy(skb_put(skb, beacon->head_len), beacon->head,
1808 beacon->head_len);
33b64eb2 1809
902acc78
JB
1810 ieee80211_include_sequence(sdata,
1811 (struct ieee80211_hdr *)skb->data);
33b64eb2 1812
d0709a65
JB
1813 /*
1814 * Not very nice, but we want to allow the driver to call
1815 * ieee80211_beacon_get() as a response to the set_tim()
1816 * callback. That, however, is already invoked under the
1817 * sta_lock to guarantee consistent and race-free update
1818 * of the tim bitmap in mac80211 and the driver.
1819 */
1820 if (local->tim_in_locked_section) {
1821 ieee80211_beacon_add_tim(local, ap, skb, beacon);
1822 } else {
1823 unsigned long flags;
1824
1825 spin_lock_irqsave(&local->sta_lock, flags);
1826 ieee80211_beacon_add_tim(local, ap, skb, beacon);
1827 spin_unlock_irqrestore(&local->sta_lock, flags);
1828 }
33b64eb2 1829
902acc78
JB
1830 if (beacon->tail)
1831 memcpy(skb_put(skb, beacon->tail_len),
1832 beacon->tail, beacon->tail_len);
33b64eb2 1833
902acc78 1834 num_beacons = &ap->num_beacons;
33b64eb2 1835
902acc78
JB
1836 err = false;
1837 }
1838 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
1839 /* headroom, head length, tail length and maximum TIM length */
1840 skb = dev_alloc_skb(local->tx_headroom + 400);
1841 if (!skb)
1842 goto out;
1843
1844 skb_reserve(skb, local->hw.extra_tx_headroom);
1845 mgmt = (struct ieee80211_mgmt *)
1846 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
1847 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
1848 mgmt->frame_control = IEEE80211_FC(IEEE80211_FTYPE_MGMT,
1849 IEEE80211_STYPE_BEACON);
1850 memset(mgmt->da, 0xff, ETH_ALEN);
1851 memcpy(mgmt->sa, sdata->dev->dev_addr, ETH_ALEN);
1852 /* BSSID is left zeroed, wildcard value */
1853 mgmt->u.beacon.beacon_int =
1854 cpu_to_le16(local->hw.conf.beacon_int);
1855 mgmt->u.beacon.capab_info = 0x0; /* 0x0 for MPs */
1856
1857 pos = skb_put(skb, 2);
1858 *pos++ = WLAN_EID_SSID;
1859 *pos++ = 0x0;
1860
1861 mesh_mgmt_ies_add(skb, sdata->dev);
33b64eb2 1862
33b64eb2 1863 num_beacons = &sdata->u.sta.num_beacons;
33b64eb2 1864
902acc78 1865 err = false;
33b64eb2 1866 }
5dfdaf58 1867
33b64eb2 1868 if (err) {
e2ebc74d
JB
1869#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1870 if (net_ratelimit())
32bfd35d
JB
1871 printk(KERN_DEBUG "no beacon data avail for %s\n",
1872 bdev->name);
e2ebc74d 1873#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
5dfdaf58
JB
1874 skb = NULL;
1875 goto out;
e2ebc74d
JB
1876 }
1877
e039fa4a
JB
1878 info = IEEE80211_SKB_CB(skb);
1879
1880 info->band = band;
1881 rate_control_get_rate(local->mdev, sband, skb, &rsel);
e2ebc74d 1882
e039fa4a
JB
1883 if (unlikely(rsel.rate_idx < 0)) {
1884 if (net_ratelimit()) {
1885 printk(KERN_DEBUG "%s: ieee80211_beacon_get: "
1886 "no rate found\n",
1887 wiphy_name(local->hw.wiphy));
1888 }
1889 dev_kfree_skb(skb);
1890 skb = NULL;
1891 goto out;
e2ebc74d 1892 }
e039fa4a
JB
1893
1894 info->control.vif = vif;
1895 info->tx_rate_idx = rsel.rate_idx;
1896 if (sdata->bss_conf.use_short_preamble &&
1897 sband->bitrates[rsel.rate_idx].flags & IEEE80211_RATE_SHORT_PREAMBLE)
1898 info->flags |= IEEE80211_TX_CTL_SHORT_PREAMBLE;
1899 info->antenna_sel_tx = local->hw.conf.antenna_sel_tx;
1900 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1901 info->flags |= IEEE80211_TX_CTL_DO_NOT_ENCRYPT;
1902 info->control.retry_limit = 1;
1903 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
33b64eb2
LCC
1904 (*num_beacons)++;
1905out:
5dfdaf58 1906 rcu_read_unlock();
e2ebc74d
JB
1907 return skb;
1908}
1909EXPORT_SYMBOL(ieee80211_beacon_get);
1910
32bfd35d 1911void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
e2ebc74d 1912 const void *frame, size_t frame_len,
e039fa4a 1913 const struct ieee80211_tx_info *frame_txctl,
e2ebc74d
JB
1914 struct ieee80211_rts *rts)
1915{
1916 const struct ieee80211_hdr *hdr = frame;
1917 u16 fctl;
1918
1919 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS;
1920 rts->frame_control = cpu_to_le16(fctl);
32bfd35d
JB
1921 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
1922 frame_txctl);
e2ebc74d
JB
1923 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
1924 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
1925}
1926EXPORT_SYMBOL(ieee80211_rts_get);
1927
32bfd35d 1928void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
e2ebc74d 1929 const void *frame, size_t frame_len,
e039fa4a 1930 const struct ieee80211_tx_info *frame_txctl,
e2ebc74d
JB
1931 struct ieee80211_cts *cts)
1932{
1933 const struct ieee80211_hdr *hdr = frame;
1934 u16 fctl;
1935
1936 fctl = IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS;
1937 cts->frame_control = cpu_to_le16(fctl);
32bfd35d
JB
1938 cts->duration = ieee80211_ctstoself_duration(hw, vif,
1939 frame_len, frame_txctl);
e2ebc74d
JB
1940 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
1941}
1942EXPORT_SYMBOL(ieee80211_ctstoself_get);
1943
1944struct sk_buff *
32bfd35d 1945ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
e039fa4a 1946 struct ieee80211_vif *vif)
e2ebc74d
JB
1947{
1948 struct ieee80211_local *local = hw_to_local(hw);
747cf5e9 1949 struct sk_buff *skb = NULL;
e2ebc74d
JB
1950 struct sta_info *sta;
1951 ieee80211_tx_handler *handler;
5cf121c3 1952 struct ieee80211_tx_data tx;
9ae54c84 1953 ieee80211_tx_result res = TX_DROP;
e2ebc74d
JB
1954 struct net_device *bdev;
1955 struct ieee80211_sub_if_data *sdata;
1956 struct ieee80211_if_ap *bss = NULL;
5dfdaf58 1957 struct beacon_data *beacon;
e039fa4a 1958 struct ieee80211_tx_info *info;
e2ebc74d 1959
32bfd35d
JB
1960 sdata = vif_to_sdata(vif);
1961 bdev = sdata->dev;
747cf5e9 1962 bss = &sdata->u.ap;
5dfdaf58
JB
1963
1964 if (!bss)
e2ebc74d
JB
1965 return NULL;
1966
5dfdaf58
JB
1967 rcu_read_lock();
1968 beacon = rcu_dereference(bss->beacon);
1969
747cf5e9
TW
1970 if (sdata->vif.type != IEEE80211_IF_TYPE_AP || !beacon || !beacon->head)
1971 goto out;
5dfdaf58 1972
e2ebc74d 1973 if (bss->dtim_count != 0)
747cf5e9 1974 goto out; /* send buffered bc/mc only after DTIM beacon */
e039fa4a 1975
e2ebc74d
JB
1976 while (1) {
1977 skb = skb_dequeue(&bss->ps_bc_buf);
1978 if (!skb)
747cf5e9 1979 goto out;
e2ebc74d
JB
1980 local->total_ps_buffered--;
1981
1982 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
1983 struct ieee80211_hdr *hdr =
1984 (struct ieee80211_hdr *) skb->data;
1985 /* more buffered multicast/broadcast frames ==> set
1986 * MoreData flag in IEEE 802.11 header to inform PS
1987 * STAs */
1988 hdr->frame_control |=
1989 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1990 }
1991
e039fa4a 1992 if (!ieee80211_tx_prepare(&tx, skb, local->mdev))
e2ebc74d
JB
1993 break;
1994 dev_kfree_skb_any(skb);
1995 }
e039fa4a
JB
1996
1997 info = IEEE80211_SKB_CB(skb);
1998
e2ebc74d 1999 sta = tx.sta;
5cf121c3
JB
2000 tx.flags |= IEEE80211_TX_PS_BUFFERED;
2001 tx.channel = local->hw.conf.channel;
e039fa4a 2002 info->band = tx.channel->band;
e2ebc74d 2003
58905290 2004 for (handler = ieee80211_tx_handlers; *handler != NULL; handler++) {
e2ebc74d 2005 res = (*handler)(&tx);
9ae54c84 2006 if (res == TX_DROP || res == TX_QUEUED)
e2ebc74d
JB
2007 break;
2008 }
e039fa4a
JB
2009
2010 if (WARN_ON(tx.skb != skb))
b97e77e0 2011 res = TX_DROP;
e2ebc74d 2012
9ae54c84 2013 if (res == TX_DROP) {
e2ebc74d
JB
2014 I802_DEBUG_INC(local->tx_handlers_drop);
2015 dev_kfree_skb(skb);
2016 skb = NULL;
9ae54c84 2017 } else if (res == TX_QUEUED) {
e2ebc74d
JB
2018 I802_DEBUG_INC(local->tx_handlers_queued);
2019 skb = NULL;
2020 }
2021
747cf5e9 2022out:
d0709a65 2023 rcu_read_unlock();
e2ebc74d
JB
2024
2025 return skb;
2026}
2027EXPORT_SYMBOL(ieee80211_get_buffered_bc);
This page took 0.394656 seconds and 5 git commands to generate.