wl1271: Use the ARP configuration function from mac80211
[linux-2.6/x86.git] / net / mac80211 / tx.c
blob698d4718b1a4b062ddaa77f83577a0dec74aee21
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>
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.
12 * Transmit and frame generation functions.
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>
20 #include <linux/rcupdate.h>
21 #include <net/net_namespace.h>
22 #include <net/ieee80211_radiotap.h>
23 #include <net/cfg80211.h>
24 #include <net/mac80211.h>
25 #include <asm/unaligned.h>
27 #include "ieee80211_i.h"
28 #include "driver-ops.h"
29 #include "led.h"
30 #include "mesh.h"
31 #include "wep.h"
32 #include "wpa.h"
33 #include "wme.h"
34 #include "rate.h"
36 #define IEEE80211_TX_OK 0
37 #define IEEE80211_TX_AGAIN 1
38 #define IEEE80211_TX_PENDING 2
40 /* misc utils */
42 static __le16 ieee80211_duration(struct ieee80211_tx_data *tx, int group_addr,
43 int next_frag_len)
45 int rate, mrate, erp, dur, i;
46 struct ieee80211_rate *txrate;
47 struct ieee80211_local *local = tx->local;
48 struct ieee80211_supported_band *sband;
49 struct ieee80211_hdr *hdr;
50 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
52 /* assume HW handles this */
53 if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
54 return 0;
56 /* uh huh? */
57 if (WARN_ON_ONCE(info->control.rates[0].idx < 0))
58 return 0;
60 sband = local->hw.wiphy->bands[tx->channel->band];
61 txrate = &sband->bitrates[info->control.rates[0].idx];
63 erp = txrate->flags & IEEE80211_RATE_ERP_G;
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
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
82 hdr = (struct ieee80211_hdr *)tx->skb->data;
83 if (ieee80211_is_ctl(hdr->frame_control)) {
84 /* TODO: These control frames are not currently sent by
85 * mac80211, but should they be implemented, this function
86 * needs to be updated to support duration field calculation.
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
98 return 0;
101 /* data/mgmt */
102 if (0 /* FIX: data/mgmt during CFP */)
103 return cpu_to_le16(32768);
105 if (group_addr) /* Group address as the destination - no ACK */
106 return 0;
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
118 rate = -1;
119 /* 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];
124 if (r->bitrate > txrate->bitrate)
125 break;
127 if (tx->sdata->vif.bss_conf.basic_rates & BIT(i))
128 rate = r->bitrate;
130 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;
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;
150 if (rate == -1) {
151 /* No matching basic rate found; use highest suitable mandatory
152 * PHY rate */
153 rate = mrate;
156 /* Time needed to transmit ACK
157 * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
158 * to closest integer */
160 dur = ieee80211_frame_duration(local, 10, rate, erp,
161 tx->sdata->vif.bss_conf.use_short_preamble);
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,
169 txrate->bitrate, erp,
170 tx->sdata->vif.bss_conf.use_short_preamble);
173 return cpu_to_le16(dur);
176 static int inline is_ieee80211_device(struct ieee80211_local *local,
177 struct net_device *dev)
179 return local == wdev_priv(dev->ieee80211_ptr);
182 /* tx handlers */
183 static ieee80211_tx_result debug_noinline
184 ieee80211_tx_h_dynamic_ps(struct ieee80211_tx_data *tx)
186 struct ieee80211_local *local = tx->local;
187 struct ieee80211_if_managed *ifmgd;
189 /* driver doesn't support power save */
190 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
191 return TX_CONTINUE;
193 /* hardware does dynamic power save */
194 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
195 return TX_CONTINUE;
197 /* dynamic power save disabled */
198 if (local->hw.conf.dynamic_ps_timeout <= 0)
199 return TX_CONTINUE;
201 /* we are scanning, don't enable power save */
202 if (local->scanning)
203 return TX_CONTINUE;
205 if (!local->ps_sdata)
206 return TX_CONTINUE;
208 /* No point if we're going to suspend */
209 if (local->quiescing)
210 return TX_CONTINUE;
212 /* dynamic ps is supported only in managed mode */
213 if (tx->sdata->vif.type != NL80211_IFTYPE_STATION)
214 return TX_CONTINUE;
216 ifmgd = &tx->sdata->u.mgd;
219 * Don't wakeup from power save if u-apsd is enabled, voip ac has
220 * u-apsd enabled and the frame is in voip class. This effectively
221 * means that even if all access categories have u-apsd enabled, in
222 * practise u-apsd is only used with the voip ac. This is a
223 * workaround for the case when received voip class packets do not
224 * have correct qos tag for some reason, due the network or the
225 * peer application.
227 * Note: local->uapsd_queues access is racy here. If the value is
228 * changed via debugfs, user needs to reassociate manually to have
229 * everything in sync.
231 if ((ifmgd->flags & IEEE80211_STA_UAPSD_ENABLED)
232 && (local->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
233 && skb_get_queue_mapping(tx->skb) == 0)
234 return TX_CONTINUE;
236 if (local->hw.conf.flags & IEEE80211_CONF_PS) {
237 ieee80211_stop_queues_by_reason(&local->hw,
238 IEEE80211_QUEUE_STOP_REASON_PS);
239 ieee80211_queue_work(&local->hw,
240 &local->dynamic_ps_disable_work);
243 mod_timer(&local->dynamic_ps_timer, jiffies +
244 msecs_to_jiffies(local->hw.conf.dynamic_ps_timeout));
246 return TX_CONTINUE;
249 static ieee80211_tx_result debug_noinline
250 ieee80211_tx_h_check_assoc(struct ieee80211_tx_data *tx)
253 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
254 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
255 u32 sta_flags;
257 if (unlikely(info->flags & IEEE80211_TX_CTL_INJECTED))
258 return TX_CONTINUE;
260 if (unlikely(test_bit(SCAN_OFF_CHANNEL, &tx->local->scanning)) &&
261 !ieee80211_is_probe_req(hdr->frame_control) &&
262 !ieee80211_is_nullfunc(hdr->frame_control))
264 * When software scanning only nullfunc frames (to notify
265 * the sleep state to the AP) and probe requests (for the
266 * active scan) are allowed, all other frames should not be
267 * sent and we should not get here, but if we do
268 * nonetheless, drop them to avoid sending them
269 * off-channel. See the link below and
270 * ieee80211_start_scan() for more.
272 * http://article.gmane.org/gmane.linux.kernel.wireless.general/30089
274 return TX_DROP;
276 if (tx->sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
277 return TX_CONTINUE;
279 if (tx->flags & IEEE80211_TX_PS_BUFFERED)
280 return TX_CONTINUE;
282 sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
284 if (likely(tx->flags & IEEE80211_TX_UNICAST)) {
285 if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
286 tx->sdata->vif.type != NL80211_IFTYPE_ADHOC &&
287 ieee80211_is_data(hdr->frame_control))) {
288 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
289 printk(KERN_DEBUG "%s: dropped data frame to not "
290 "associated station %pM\n",
291 tx->sdata->name, hdr->addr1);
292 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
293 I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
294 return TX_DROP;
296 } else {
297 if (unlikely(ieee80211_is_data(hdr->frame_control) &&
298 tx->local->num_sta == 0 &&
299 tx->sdata->vif.type != NL80211_IFTYPE_ADHOC)) {
301 * No associated STAs - no need to send multicast
302 * frames.
304 return TX_DROP;
306 return TX_CONTINUE;
309 return TX_CONTINUE;
312 /* This function is called whenever the AP is about to exceed the maximum limit
313 * of buffered frames for power saving STAs. This situation should not really
314 * happen often during normal operation, so dropping the oldest buffered packet
315 * from each queue should be OK to make some room for new frames. */
316 static void purge_old_ps_buffers(struct ieee80211_local *local)
318 int total = 0, purged = 0;
319 struct sk_buff *skb;
320 struct ieee80211_sub_if_data *sdata;
321 struct sta_info *sta;
324 * virtual interfaces are protected by RCU
326 rcu_read_lock();
328 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
329 struct ieee80211_if_ap *ap;
330 if (sdata->vif.type != NL80211_IFTYPE_AP)
331 continue;
332 ap = &sdata->u.ap;
333 skb = skb_dequeue(&ap->ps_bc_buf);
334 if (skb) {
335 purged++;
336 dev_kfree_skb(skb);
338 total += skb_queue_len(&ap->ps_bc_buf);
341 list_for_each_entry_rcu(sta, &local->sta_list, list) {
342 skb = skb_dequeue(&sta->ps_tx_buf);
343 if (skb) {
344 purged++;
345 dev_kfree_skb(skb);
347 total += skb_queue_len(&sta->ps_tx_buf);
350 rcu_read_unlock();
352 local->total_ps_buffered = total;
353 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
354 printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
355 wiphy_name(local->hw.wiphy), purged);
356 #endif
359 static ieee80211_tx_result
360 ieee80211_tx_h_multicast_ps_buf(struct ieee80211_tx_data *tx)
362 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
363 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
366 * broadcast/multicast frame
368 * If any of the associated stations is in power save mode,
369 * the frame is buffered to be sent after DTIM beacon frame.
370 * This is done either by the hardware or us.
373 /* powersaving STAs only in AP/VLAN mode */
374 if (!tx->sdata->bss)
375 return TX_CONTINUE;
377 /* no buffering for ordered frames */
378 if (ieee80211_has_order(hdr->frame_control))
379 return TX_CONTINUE;
381 /* no stations in PS mode */
382 if (!atomic_read(&tx->sdata->bss->num_sta_ps))
383 return TX_CONTINUE;
385 info->flags |= IEEE80211_TX_CTL_SEND_AFTER_DTIM;
387 /* device releases frame after DTIM beacon */
388 if (!(tx->local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING))
389 return TX_CONTINUE;
391 /* buffered in mac80211 */
392 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
393 purge_old_ps_buffers(tx->local);
395 if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >= AP_MAX_BC_BUFFER) {
396 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
397 if (net_ratelimit())
398 printk(KERN_DEBUG "%s: BC TX buffer full - dropping the oldest frame\n",
399 tx->sdata->name);
400 #endif
401 dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
402 } else
403 tx->local->total_ps_buffered++;
405 skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
407 return TX_QUEUED;
410 static int ieee80211_use_mfp(__le16 fc, struct sta_info *sta,
411 struct sk_buff *skb)
413 if (!ieee80211_is_mgmt(fc))
414 return 0;
416 if (sta == NULL || !test_sta_flags(sta, WLAN_STA_MFP))
417 return 0;
419 if (!ieee80211_is_robust_mgmt_frame((struct ieee80211_hdr *)
420 skb->data))
421 return 0;
423 return 1;
426 static ieee80211_tx_result
427 ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
429 struct sta_info *sta = tx->sta;
430 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
431 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
432 struct ieee80211_local *local = tx->local;
433 u32 staflags;
435 if (unlikely(!sta ||
436 ieee80211_is_probe_resp(hdr->frame_control) ||
437 ieee80211_is_auth(hdr->frame_control) ||
438 ieee80211_is_assoc_resp(hdr->frame_control) ||
439 ieee80211_is_reassoc_resp(hdr->frame_control)))
440 return TX_CONTINUE;
442 staflags = get_sta_flags(sta);
444 if (unlikely((staflags & (WLAN_STA_PS_STA | WLAN_STA_PS_DRIVER)) &&
445 !(info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE))) {
446 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
447 printk(KERN_DEBUG "STA %pM aid %d: PS buffer (entries "
448 "before %d)\n",
449 sta->sta.addr, sta->sta.aid,
450 skb_queue_len(&sta->ps_tx_buf));
451 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
452 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
453 purge_old_ps_buffers(tx->local);
454 if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
455 struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
456 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
457 if (net_ratelimit()) {
458 printk(KERN_DEBUG "%s: STA %pM TX "
459 "buffer full - dropping oldest frame\n",
460 tx->sdata->name, sta->sta.addr);
462 #endif
463 dev_kfree_skb(old);
464 } else
465 tx->local->total_ps_buffered++;
468 * Queue frame to be sent after STA wakes up/polls,
469 * but don't set the TIM bit if the driver is blocking
470 * wakeup or poll response transmissions anyway.
472 if (skb_queue_empty(&sta->ps_tx_buf) &&
473 !(staflags & WLAN_STA_PS_DRIVER))
474 sta_info_set_tim_bit(sta);
476 info->control.jiffies = jiffies;
477 info->control.vif = &tx->sdata->vif;
478 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
479 skb_queue_tail(&sta->ps_tx_buf, tx->skb);
481 if (!timer_pending(&local->sta_cleanup))
482 mod_timer(&local->sta_cleanup,
483 round_jiffies(jiffies +
484 STA_INFO_CLEANUP_INTERVAL));
486 return TX_QUEUED;
488 #ifdef CONFIG_MAC80211_VERBOSE_PS_DEBUG
489 else if (unlikely(staflags & WLAN_STA_PS_STA)) {
490 printk(KERN_DEBUG "%s: STA %pM in PS mode, but pspoll "
491 "set -> send frame\n", tx->sdata->name,
492 sta->sta.addr);
494 #endif /* CONFIG_MAC80211_VERBOSE_PS_DEBUG */
496 return TX_CONTINUE;
499 static ieee80211_tx_result debug_noinline
500 ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
502 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
503 return TX_CONTINUE;
505 if (tx->flags & IEEE80211_TX_UNICAST)
506 return ieee80211_tx_h_unicast_ps_buf(tx);
507 else
508 return ieee80211_tx_h_multicast_ps_buf(tx);
511 static ieee80211_tx_result debug_noinline
512 ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
514 struct ieee80211_key *key = NULL;
515 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
516 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
518 if (unlikely(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT))
519 tx->key = NULL;
520 else if (tx->sta && (key = rcu_dereference(tx->sta->key)))
521 tx->key = key;
522 else if (ieee80211_is_mgmt(hdr->frame_control) &&
523 is_multicast_ether_addr(hdr->addr1) &&
524 ieee80211_is_robust_mgmt_frame(hdr) &&
525 (key = rcu_dereference(tx->sdata->default_mgmt_key)))
526 tx->key = key;
527 else if ((key = rcu_dereference(tx->sdata->default_key)))
528 tx->key = key;
529 else if (tx->sdata->drop_unencrypted &&
530 (tx->skb->protocol != cpu_to_be16(ETH_P_PAE)) &&
531 !(info->flags & IEEE80211_TX_CTL_INJECTED) &&
532 (!ieee80211_is_robust_mgmt_frame(hdr) ||
533 (ieee80211_is_action(hdr->frame_control) &&
534 tx->sta && test_sta_flags(tx->sta, WLAN_STA_MFP)))) {
535 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
536 return TX_DROP;
537 } else
538 tx->key = NULL;
540 if (tx->key) {
541 bool skip_hw = false;
543 tx->key->tx_rx_count++;
544 /* TODO: add threshold stuff again */
546 switch (tx->key->conf.alg) {
547 case ALG_WEP:
548 if (ieee80211_is_auth(hdr->frame_control))
549 break;
550 case ALG_TKIP:
551 if (!ieee80211_is_data_present(hdr->frame_control))
552 tx->key = NULL;
553 break;
554 case ALG_CCMP:
555 if (!ieee80211_is_data_present(hdr->frame_control) &&
556 !ieee80211_use_mfp(hdr->frame_control, tx->sta,
557 tx->skb))
558 tx->key = NULL;
559 else
560 skip_hw = (tx->key->conf.flags &
561 IEEE80211_KEY_FLAG_SW_MGMT) &&
562 ieee80211_is_mgmt(hdr->frame_control);
563 break;
564 case ALG_AES_CMAC:
565 if (!ieee80211_is_mgmt(hdr->frame_control))
566 tx->key = NULL;
567 break;
570 if (!skip_hw && tx->key &&
571 tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
572 info->control.hw_key = &tx->key->conf;
575 return TX_CONTINUE;
578 static ieee80211_tx_result debug_noinline
579 ieee80211_tx_h_sta(struct ieee80211_tx_data *tx)
581 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
583 if (tx->sta && tx->sta->uploaded)
584 info->control.sta = &tx->sta->sta;
586 return TX_CONTINUE;
589 static ieee80211_tx_result debug_noinline
590 ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
592 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
593 struct ieee80211_hdr *hdr = (void *)tx->skb->data;
594 struct ieee80211_supported_band *sband;
595 struct ieee80211_rate *rate;
596 int i;
597 u32 len;
598 bool inval = false, rts = false, short_preamble = false;
599 struct ieee80211_tx_rate_control txrc;
600 u32 sta_flags;
602 memset(&txrc, 0, sizeof(txrc));
604 sband = tx->local->hw.wiphy->bands[tx->channel->band];
606 len = min_t(u32, tx->skb->len + FCS_LEN,
607 tx->local->hw.wiphy->frag_threshold);
609 /* set up the tx rate control struct we give the RC algo */
610 txrc.hw = local_to_hw(tx->local);
611 txrc.sband = sband;
612 txrc.bss_conf = &tx->sdata->vif.bss_conf;
613 txrc.skb = tx->skb;
614 txrc.reported_rate.idx = -1;
615 txrc.rate_idx_mask = tx->sdata->rc_rateidx_mask[tx->channel->band];
616 if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
617 txrc.max_rate_idx = -1;
618 else
619 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
620 txrc.ap = tx->sdata->vif.type == NL80211_IFTYPE_AP;
622 /* set up RTS protection if desired */
623 if (len > tx->local->hw.wiphy->rts_threshold) {
624 txrc.rts = rts = true;
628 * Use short preamble if the BSS can handle it, but not for
629 * management frames unless we know the receiver can handle
630 * that -- the management frame might be to a station that
631 * just wants a probe response.
633 if (tx->sdata->vif.bss_conf.use_short_preamble &&
634 (ieee80211_is_data(hdr->frame_control) ||
635 (tx->sta && test_sta_flags(tx->sta, WLAN_STA_SHORT_PREAMBLE))))
636 txrc.short_preamble = short_preamble = true;
638 sta_flags = tx->sta ? get_sta_flags(tx->sta) : 0;
641 * Lets not bother rate control if we're associated and cannot
642 * talk to the sta. This should not happen.
644 if (WARN(test_bit(SCAN_SW_SCANNING, &tx->local->scanning) &&
645 (sta_flags & WLAN_STA_ASSOC) &&
646 !rate_usable_index_exists(sband, &tx->sta->sta),
647 "%s: Dropped data frame as no usable bitrate found while "
648 "scanning and associated. Target station: "
649 "%pM on %d GHz band\n",
650 tx->sdata->name, hdr->addr1,
651 tx->channel->band ? 5 : 2))
652 return TX_DROP;
655 * If we're associated with the sta at this point we know we can at
656 * least send the frame at the lowest bit rate.
658 rate_control_get_rate(tx->sdata, tx->sta, &txrc);
660 if (unlikely(info->control.rates[0].idx < 0))
661 return TX_DROP;
663 if (txrc.reported_rate.idx < 0)
664 txrc.reported_rate = info->control.rates[0];
666 if (tx->sta)
667 tx->sta->last_tx_rate = txrc.reported_rate;
669 if (unlikely(!info->control.rates[0].count))
670 info->control.rates[0].count = 1;
672 if (WARN_ON_ONCE((info->control.rates[0].count > 1) &&
673 (info->flags & IEEE80211_TX_CTL_NO_ACK)))
674 info->control.rates[0].count = 1;
676 if (is_multicast_ether_addr(hdr->addr1)) {
678 * XXX: verify the rate is in the basic rateset
680 return TX_CONTINUE;
684 * set up the RTS/CTS rate as the fastest basic rate
685 * that is not faster than the data rate
687 * XXX: Should this check all retry rates?
689 if (!(info->control.rates[0].flags & IEEE80211_TX_RC_MCS)) {
690 s8 baserate = 0;
692 rate = &sband->bitrates[info->control.rates[0].idx];
694 for (i = 0; i < sband->n_bitrates; i++) {
695 /* must be a basic rate */
696 if (!(tx->sdata->vif.bss_conf.basic_rates & BIT(i)))
697 continue;
698 /* must not be faster than the data rate */
699 if (sband->bitrates[i].bitrate > rate->bitrate)
700 continue;
701 /* maximum */
702 if (sband->bitrates[baserate].bitrate <
703 sband->bitrates[i].bitrate)
704 baserate = i;
707 info->control.rts_cts_rate_idx = baserate;
710 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
712 * make sure there's no valid rate following
713 * an invalid one, just in case drivers don't
714 * take the API seriously to stop at -1.
716 if (inval) {
717 info->control.rates[i].idx = -1;
718 continue;
720 if (info->control.rates[i].idx < 0) {
721 inval = true;
722 continue;
726 * For now assume MCS is already set up correctly, this
727 * needs to be fixed.
729 if (info->control.rates[i].flags & IEEE80211_TX_RC_MCS) {
730 WARN_ON(info->control.rates[i].idx > 76);
731 continue;
734 /* set up RTS protection if desired */
735 if (rts)
736 info->control.rates[i].flags |=
737 IEEE80211_TX_RC_USE_RTS_CTS;
739 /* RC is busted */
740 if (WARN_ON_ONCE(info->control.rates[i].idx >=
741 sband->n_bitrates)) {
742 info->control.rates[i].idx = -1;
743 continue;
746 rate = &sband->bitrates[info->control.rates[i].idx];
748 /* set up short preamble */
749 if (short_preamble &&
750 rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
751 info->control.rates[i].flags |=
752 IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
754 /* set up G protection */
755 if (!rts && tx->sdata->vif.bss_conf.use_cts_prot &&
756 rate->flags & IEEE80211_RATE_ERP_G)
757 info->control.rates[i].flags |=
758 IEEE80211_TX_RC_USE_CTS_PROTECT;
761 return TX_CONTINUE;
764 static ieee80211_tx_result debug_noinline
765 ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
767 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
768 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
769 u16 *seq;
770 u8 *qc;
771 int tid;
774 * Packet injection may want to control the sequence
775 * number, if we have no matching interface then we
776 * neither assign one ourselves nor ask the driver to.
778 if (unlikely(info->control.vif->type == NL80211_IFTYPE_MONITOR))
779 return TX_CONTINUE;
781 if (unlikely(ieee80211_is_ctl(hdr->frame_control)))
782 return TX_CONTINUE;
784 if (ieee80211_hdrlen(hdr->frame_control) < 24)
785 return TX_CONTINUE;
788 * Anything but QoS data that has a sequence number field
789 * (is long enough) gets a sequence number from the global
790 * counter.
792 if (!ieee80211_is_data_qos(hdr->frame_control)) {
793 /* driver should assign sequence number */
794 info->flags |= IEEE80211_TX_CTL_ASSIGN_SEQ;
795 /* for pure STA mode without beacons, we can do it */
796 hdr->seq_ctrl = cpu_to_le16(tx->sdata->sequence_number);
797 tx->sdata->sequence_number += 0x10;
798 return TX_CONTINUE;
802 * This should be true for injected/management frames only, for
803 * management frames we have set the IEEE80211_TX_CTL_ASSIGN_SEQ
804 * above since they are not QoS-data frames.
806 if (!tx->sta)
807 return TX_CONTINUE;
809 /* include per-STA, per-TID sequence counter */
811 qc = ieee80211_get_qos_ctl(hdr);
812 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
813 seq = &tx->sta->tid_seq[tid];
815 hdr->seq_ctrl = cpu_to_le16(*seq);
817 /* Increase the sequence number. */
818 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
820 return TX_CONTINUE;
823 static int ieee80211_fragment(struct ieee80211_local *local,
824 struct sk_buff *skb, int hdrlen,
825 int frag_threshold)
827 struct sk_buff *tail = skb, *tmp;
828 int per_fragm = frag_threshold - hdrlen - FCS_LEN;
829 int pos = hdrlen + per_fragm;
830 int rem = skb->len - hdrlen - per_fragm;
832 if (WARN_ON(rem < 0))
833 return -EINVAL;
835 while (rem) {
836 int fraglen = per_fragm;
838 if (fraglen > rem)
839 fraglen = rem;
840 rem -= fraglen;
841 tmp = dev_alloc_skb(local->tx_headroom +
842 frag_threshold +
843 IEEE80211_ENCRYPT_HEADROOM +
844 IEEE80211_ENCRYPT_TAILROOM);
845 if (!tmp)
846 return -ENOMEM;
847 tail->next = tmp;
848 tail = tmp;
849 skb_reserve(tmp, local->tx_headroom +
850 IEEE80211_ENCRYPT_HEADROOM);
851 /* copy control information */
852 memcpy(tmp->cb, skb->cb, sizeof(tmp->cb));
853 skb_copy_queue_mapping(tmp, skb);
854 tmp->priority = skb->priority;
855 tmp->dev = skb->dev;
857 /* copy header and data */
858 memcpy(skb_put(tmp, hdrlen), skb->data, hdrlen);
859 memcpy(skb_put(tmp, fraglen), skb->data + pos, fraglen);
861 pos += fraglen;
864 skb->len = hdrlen + per_fragm;
865 return 0;
868 static ieee80211_tx_result debug_noinline
869 ieee80211_tx_h_fragment(struct ieee80211_tx_data *tx)
871 struct sk_buff *skb = tx->skb;
872 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
873 struct ieee80211_hdr *hdr = (void *)skb->data;
874 int frag_threshold = tx->local->hw.wiphy->frag_threshold;
875 int hdrlen;
876 int fragnum;
878 if (!(tx->flags & IEEE80211_TX_FRAGMENTED))
879 return TX_CONTINUE;
882 * Warn when submitting a fragmented A-MPDU frame and drop it.
883 * This scenario is handled in ieee80211_tx_prepare but extra
884 * caution taken here as fragmented ampdu may cause Tx stop.
886 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
887 return TX_DROP;
889 hdrlen = ieee80211_hdrlen(hdr->frame_control);
891 /* internal error, why is TX_FRAGMENTED set? */
892 if (WARN_ON(skb->len + FCS_LEN <= frag_threshold))
893 return TX_DROP;
896 * Now fragment the frame. This will allocate all the fragments and
897 * chain them (using skb as the first fragment) to skb->next.
898 * During transmission, we will remove the successfully transmitted
899 * fragments from this list. When the low-level driver rejects one
900 * of the fragments then we will simply pretend to accept the skb
901 * but store it away as pending.
903 if (ieee80211_fragment(tx->local, skb, hdrlen, frag_threshold))
904 return TX_DROP;
906 /* update duration/seq/flags of fragments */
907 fragnum = 0;
908 do {
909 int next_len;
910 const __le16 morefrags = cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
912 hdr = (void *)skb->data;
913 info = IEEE80211_SKB_CB(skb);
915 if (skb->next) {
916 hdr->frame_control |= morefrags;
917 next_len = skb->next->len;
919 * No multi-rate retries for fragmented frames, that
920 * would completely throw off the NAV at other STAs.
922 info->control.rates[1].idx = -1;
923 info->control.rates[2].idx = -1;
924 info->control.rates[3].idx = -1;
925 info->control.rates[4].idx = -1;
926 BUILD_BUG_ON(IEEE80211_TX_MAX_RATES != 5);
927 info->flags &= ~IEEE80211_TX_CTL_RATE_CTRL_PROBE;
928 } else {
929 hdr->frame_control &= ~morefrags;
930 next_len = 0;
932 hdr->duration_id = ieee80211_duration(tx, 0, next_len);
933 hdr->seq_ctrl |= cpu_to_le16(fragnum & IEEE80211_SCTL_FRAG);
934 fragnum++;
935 } while ((skb = skb->next));
937 return TX_CONTINUE;
940 static ieee80211_tx_result debug_noinline
941 ieee80211_tx_h_stats(struct ieee80211_tx_data *tx)
943 struct sk_buff *skb = tx->skb;
945 if (!tx->sta)
946 return TX_CONTINUE;
948 tx->sta->tx_packets++;
949 do {
950 tx->sta->tx_fragments++;
951 tx->sta->tx_bytes += skb->len;
952 } while ((skb = skb->next));
954 return TX_CONTINUE;
957 static ieee80211_tx_result debug_noinline
958 ieee80211_tx_h_encrypt(struct ieee80211_tx_data *tx)
960 if (!tx->key)
961 return TX_CONTINUE;
963 switch (tx->key->conf.alg) {
964 case ALG_WEP:
965 return ieee80211_crypto_wep_encrypt(tx);
966 case ALG_TKIP:
967 return ieee80211_crypto_tkip_encrypt(tx);
968 case ALG_CCMP:
969 return ieee80211_crypto_ccmp_encrypt(tx);
970 case ALG_AES_CMAC:
971 return ieee80211_crypto_aes_cmac_encrypt(tx);
974 /* not reached */
975 WARN_ON(1);
976 return TX_DROP;
979 static ieee80211_tx_result debug_noinline
980 ieee80211_tx_h_calculate_duration(struct ieee80211_tx_data *tx)
982 struct sk_buff *skb = tx->skb;
983 struct ieee80211_hdr *hdr;
984 int next_len;
985 bool group_addr;
987 do {
988 hdr = (void *) skb->data;
989 if (unlikely(ieee80211_is_pspoll(hdr->frame_control)))
990 break; /* must not overwrite AID */
991 next_len = skb->next ? skb->next->len : 0;
992 group_addr = is_multicast_ether_addr(hdr->addr1);
994 hdr->duration_id =
995 ieee80211_duration(tx, group_addr, next_len);
996 } while ((skb = skb->next));
998 return TX_CONTINUE;
1001 /* actual transmit path */
1004 * deal with packet injection down monitor interface
1005 * with Radiotap Header -- only called for monitor mode interface
1007 static bool __ieee80211_parse_tx_radiotap(struct ieee80211_tx_data *tx,
1008 struct sk_buff *skb)
1011 * this is the moment to interpret and discard the radiotap header that
1012 * must be at the start of the packet injected in Monitor mode
1014 * Need to take some care with endian-ness since radiotap
1015 * args are little-endian
1018 struct ieee80211_radiotap_iterator iterator;
1019 struct ieee80211_radiotap_header *rthdr =
1020 (struct ieee80211_radiotap_header *) skb->data;
1021 struct ieee80211_supported_band *sband;
1022 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1023 int ret = ieee80211_radiotap_iterator_init(&iterator, rthdr, skb->len,
1024 NULL);
1026 sband = tx->local->hw.wiphy->bands[tx->channel->band];
1028 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
1029 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
1032 * for every radiotap entry that is present
1033 * (ieee80211_radiotap_iterator_next returns -ENOENT when no more
1034 * entries present, or -EINVAL on error)
1037 while (!ret) {
1038 ret = ieee80211_radiotap_iterator_next(&iterator);
1040 if (ret)
1041 continue;
1043 /* see if this argument is something we can use */
1044 switch (iterator.this_arg_index) {
1046 * You must take care when dereferencing iterator.this_arg
1047 * for multibyte types... the pointer is not aligned. Use
1048 * get_unaligned((type *)iterator.this_arg) to dereference
1049 * iterator.this_arg for type "type" safely on all arches.
1051 case IEEE80211_RADIOTAP_FLAGS:
1052 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FCS) {
1054 * this indicates that the skb we have been
1055 * handed has the 32-bit FCS CRC at the end...
1056 * we should react to that by snipping it off
1057 * because it will be recomputed and added
1058 * on transmission
1060 if (skb->len < (iterator._max_length + FCS_LEN))
1061 return false;
1063 skb_trim(skb, skb->len - FCS_LEN);
1065 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_WEP)
1066 info->flags &= ~IEEE80211_TX_INTFL_DONT_ENCRYPT;
1067 if (*iterator.this_arg & IEEE80211_RADIOTAP_F_FRAG)
1068 tx->flags |= IEEE80211_TX_FRAGMENTED;
1069 break;
1072 * Please update the file
1073 * Documentation/networking/mac80211-injection.txt
1074 * when parsing new fields here.
1077 default:
1078 break;
1082 if (ret != -ENOENT) /* ie, if we didn't simply run out of fields */
1083 return false;
1086 * remove the radiotap header
1087 * iterator->_max_length was sanity-checked against
1088 * skb->len by iterator init
1090 skb_pull(skb, iterator._max_length);
1092 return true;
1095 static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1096 struct sk_buff *skb,
1097 struct ieee80211_tx_info *info,
1098 struct tid_ampdu_tx *tid_tx,
1099 int tid)
1101 bool queued = false;
1103 if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1104 info->flags |= IEEE80211_TX_CTL_AMPDU;
1105 } else if (test_bit(HT_AGG_STATE_WANT_START, &tid_tx->state)) {
1107 * nothing -- this aggregation session is being started
1108 * but that might still fail with the driver
1110 } else {
1111 spin_lock(&tx->sta->lock);
1113 * Need to re-check now, because we may get here
1115 * 1) in the window during which the setup is actually
1116 * already done, but not marked yet because not all
1117 * packets are spliced over to the driver pending
1118 * queue yet -- if this happened we acquire the lock
1119 * either before or after the splice happens, but
1120 * need to recheck which of these cases happened.
1122 * 2) during session teardown, if the OPERATIONAL bit
1123 * was cleared due to the teardown but the pointer
1124 * hasn't been assigned NULL yet (or we loaded it
1125 * before it was assigned) -- in this case it may
1126 * now be NULL which means we should just let the
1127 * packet pass through because splicing the frames
1128 * back is already done.
1130 tid_tx = tx->sta->ampdu_mlme.tid_tx[tid];
1132 if (!tid_tx) {
1133 /* do nothing, let packet pass through */
1134 } else if (test_bit(HT_AGG_STATE_OPERATIONAL, &tid_tx->state)) {
1135 info->flags |= IEEE80211_TX_CTL_AMPDU;
1136 } else {
1137 queued = true;
1138 info->control.vif = &tx->sdata->vif;
1139 info->flags |= IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1140 __skb_queue_tail(&tid_tx->pending, skb);
1142 spin_unlock(&tx->sta->lock);
1145 return queued;
1149 * initialises @tx
1151 static ieee80211_tx_result
1152 ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1153 struct ieee80211_tx_data *tx,
1154 struct sk_buff *skb)
1156 struct ieee80211_local *local = sdata->local;
1157 struct ieee80211_hdr *hdr;
1158 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1159 int hdrlen, tid;
1160 u8 *qc;
1162 memset(tx, 0, sizeof(*tx));
1163 tx->skb = skb;
1164 tx->local = local;
1165 tx->sdata = sdata;
1166 tx->channel = local->hw.conf.channel;
1168 * Set this flag (used below to indicate "automatic fragmentation"),
1169 * it will be cleared/left by radiotap as desired.
1171 tx->flags |= IEEE80211_TX_FRAGMENTED;
1173 /* process and remove the injection radiotap header */
1174 if (unlikely(info->flags & IEEE80211_TX_INTFL_HAS_RADIOTAP)) {
1175 if (!__ieee80211_parse_tx_radiotap(tx, skb))
1176 return TX_DROP;
1179 * __ieee80211_parse_tx_radiotap has now removed
1180 * the radiotap header that was present and pre-filled
1181 * 'tx' with tx control information.
1183 info->flags &= ~IEEE80211_TX_INTFL_HAS_RADIOTAP;
1187 * If this flag is set to true anywhere, and we get here,
1188 * we are doing the needed processing, so remove the flag
1189 * now.
1191 info->flags &= ~IEEE80211_TX_INTFL_NEED_TXPROCESSING;
1193 hdr = (struct ieee80211_hdr *) skb->data;
1195 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1196 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1197 if (!tx->sta && sdata->dev->ieee80211_ptr->use_4addr)
1198 return TX_DROP;
1199 } else if (info->flags & IEEE80211_TX_CTL_INJECTED) {
1200 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1202 if (!tx->sta)
1203 tx->sta = sta_info_get(sdata, hdr->addr1);
1205 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
1206 (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION)) {
1207 struct tid_ampdu_tx *tid_tx;
1209 qc = ieee80211_get_qos_ctl(hdr);
1210 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
1212 tid_tx = rcu_dereference(tx->sta->ampdu_mlme.tid_tx[tid]);
1213 if (tid_tx) {
1214 bool queued;
1216 queued = ieee80211_tx_prep_agg(tx, skb, info,
1217 tid_tx, tid);
1219 if (unlikely(queued))
1220 return TX_QUEUED;
1224 if (is_multicast_ether_addr(hdr->addr1)) {
1225 tx->flags &= ~IEEE80211_TX_UNICAST;
1226 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1227 } else {
1228 tx->flags |= IEEE80211_TX_UNICAST;
1229 if (unlikely(local->wifi_wme_noack_test))
1230 info->flags |= IEEE80211_TX_CTL_NO_ACK;
1231 else
1232 info->flags &= ~IEEE80211_TX_CTL_NO_ACK;
1235 if (tx->flags & IEEE80211_TX_FRAGMENTED) {
1236 if ((tx->flags & IEEE80211_TX_UNICAST) &&
1237 skb->len + FCS_LEN > local->hw.wiphy->frag_threshold &&
1238 !(info->flags & IEEE80211_TX_CTL_AMPDU))
1239 tx->flags |= IEEE80211_TX_FRAGMENTED;
1240 else
1241 tx->flags &= ~IEEE80211_TX_FRAGMENTED;
1244 if (!tx->sta)
1245 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1246 else if (test_and_clear_sta_flags(tx->sta, WLAN_STA_CLEAR_PS_FILT))
1247 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT;
1249 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1250 if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
1251 u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
1252 tx->ethertype = (pos[0] << 8) | pos[1];
1254 info->flags |= IEEE80211_TX_CTL_FIRST_FRAGMENT;
1256 return TX_CONTINUE;
1259 static int __ieee80211_tx(struct ieee80211_local *local,
1260 struct sk_buff **skbp,
1261 struct sta_info *sta,
1262 bool txpending)
1264 struct sk_buff *skb = *skbp, *next;
1265 struct ieee80211_tx_info *info;
1266 struct ieee80211_sub_if_data *sdata;
1267 unsigned long flags;
1268 int ret, len;
1269 bool fragm = false;
1271 while (skb) {
1272 int q = skb_get_queue_mapping(skb);
1274 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1275 ret = IEEE80211_TX_OK;
1276 if (local->queue_stop_reasons[q] ||
1277 (!txpending && !skb_queue_empty(&local->pending[q])))
1278 ret = IEEE80211_TX_PENDING;
1279 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1280 if (ret != IEEE80211_TX_OK)
1281 return ret;
1283 info = IEEE80211_SKB_CB(skb);
1285 if (fragm)
1286 info->flags &= ~(IEEE80211_TX_CTL_CLEAR_PS_FILT |
1287 IEEE80211_TX_CTL_FIRST_FRAGMENT);
1289 next = skb->next;
1290 len = skb->len;
1292 if (next)
1293 info->flags |= IEEE80211_TX_CTL_MORE_FRAMES;
1295 sdata = vif_to_sdata(info->control.vif);
1297 switch (sdata->vif.type) {
1298 case NL80211_IFTYPE_MONITOR:
1299 info->control.vif = NULL;
1300 break;
1301 case NL80211_IFTYPE_AP_VLAN:
1302 info->control.vif = &container_of(sdata->bss,
1303 struct ieee80211_sub_if_data, u.ap)->vif;
1304 break;
1305 default:
1306 /* keep */
1307 break;
1310 ret = drv_tx(local, skb);
1311 if (WARN_ON(ret != NETDEV_TX_OK && skb->len != len)) {
1312 dev_kfree_skb(skb);
1313 ret = NETDEV_TX_OK;
1315 if (ret != NETDEV_TX_OK) {
1316 info->control.vif = &sdata->vif;
1317 return IEEE80211_TX_AGAIN;
1320 *skbp = skb = next;
1321 ieee80211_led_tx(local, 1);
1322 fragm = true;
1325 return IEEE80211_TX_OK;
1329 * Invoke TX handlers, return 0 on success and non-zero if the
1330 * frame was dropped or queued.
1332 static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1334 struct sk_buff *skb = tx->skb;
1335 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1336 ieee80211_tx_result res = TX_DROP;
1338 #define CALL_TXH(txh) \
1339 do { \
1340 res = txh(tx); \
1341 if (res != TX_CONTINUE) \
1342 goto txh_done; \
1343 } while (0)
1345 CALL_TXH(ieee80211_tx_h_dynamic_ps);
1346 CALL_TXH(ieee80211_tx_h_check_assoc);
1347 CALL_TXH(ieee80211_tx_h_ps_buf);
1348 CALL_TXH(ieee80211_tx_h_select_key);
1349 CALL_TXH(ieee80211_tx_h_sta);
1350 if (!(tx->local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL))
1351 CALL_TXH(ieee80211_tx_h_rate_ctrl);
1353 if (unlikely(info->flags & IEEE80211_TX_INTFL_RETRANSMISSION))
1354 goto txh_done;
1356 CALL_TXH(ieee80211_tx_h_michael_mic_add);
1357 CALL_TXH(ieee80211_tx_h_sequence);
1358 CALL_TXH(ieee80211_tx_h_fragment);
1359 /* handlers after fragment must be aware of tx info fragmentation! */
1360 CALL_TXH(ieee80211_tx_h_stats);
1361 CALL_TXH(ieee80211_tx_h_encrypt);
1362 CALL_TXH(ieee80211_tx_h_calculate_duration);
1363 #undef CALL_TXH
1365 txh_done:
1366 if (unlikely(res == TX_DROP)) {
1367 I802_DEBUG_INC(tx->local->tx_handlers_drop);
1368 while (skb) {
1369 struct sk_buff *next;
1371 next = skb->next;
1372 dev_kfree_skb(skb);
1373 skb = next;
1375 return -1;
1376 } else if (unlikely(res == TX_QUEUED)) {
1377 I802_DEBUG_INC(tx->local->tx_handlers_queued);
1378 return -1;
1381 return 0;
1384 static void ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1385 struct sk_buff *skb, bool txpending)
1387 struct ieee80211_local *local = sdata->local;
1388 struct ieee80211_tx_data tx;
1389 ieee80211_tx_result res_prepare;
1390 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1391 struct sk_buff *next;
1392 unsigned long flags;
1393 int ret, retries;
1394 u16 queue;
1396 queue = skb_get_queue_mapping(skb);
1398 if (unlikely(skb->len < 10)) {
1399 dev_kfree_skb(skb);
1400 return;
1403 rcu_read_lock();
1405 /* initialises tx */
1406 res_prepare = ieee80211_tx_prepare(sdata, &tx, skb);
1408 if (unlikely(res_prepare == TX_DROP)) {
1409 dev_kfree_skb(skb);
1410 rcu_read_unlock();
1411 return;
1412 } else if (unlikely(res_prepare == TX_QUEUED)) {
1413 rcu_read_unlock();
1414 return;
1417 tx.channel = local->hw.conf.channel;
1418 info->band = tx.channel->band;
1420 if (invoke_tx_handlers(&tx))
1421 goto out;
1423 retries = 0;
1424 retry:
1425 ret = __ieee80211_tx(local, &tx.skb, tx.sta, txpending);
1426 switch (ret) {
1427 case IEEE80211_TX_OK:
1428 break;
1429 case IEEE80211_TX_AGAIN:
1431 * Since there are no fragmented frames on A-MPDU
1432 * queues, there's no reason for a driver to reject
1433 * a frame there, warn and drop it.
1435 if (WARN_ON(info->flags & IEEE80211_TX_CTL_AMPDU))
1436 goto drop;
1437 /* fall through */
1438 case IEEE80211_TX_PENDING:
1439 skb = tx.skb;
1441 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
1443 if (local->queue_stop_reasons[queue] ||
1444 !skb_queue_empty(&local->pending[queue])) {
1446 * if queue is stopped, queue up frames for later
1447 * transmission from the tasklet
1449 do {
1450 next = skb->next;
1451 skb->next = NULL;
1452 if (unlikely(txpending))
1453 __skb_queue_head(&local->pending[queue],
1454 skb);
1455 else
1456 __skb_queue_tail(&local->pending[queue],
1457 skb);
1458 } while ((skb = next));
1460 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1461 flags);
1462 } else {
1464 * otherwise retry, but this is a race condition or
1465 * a driver bug (which we warn about if it persists)
1467 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
1468 flags);
1470 retries++;
1471 if (WARN(retries > 10, "tx refused but queue active\n"))
1472 goto drop;
1473 goto retry;
1476 out:
1477 rcu_read_unlock();
1478 return;
1480 drop:
1481 rcu_read_unlock();
1483 skb = tx.skb;
1484 while (skb) {
1485 next = skb->next;
1486 dev_kfree_skb(skb);
1487 skb = next;
1491 /* device xmit handlers */
1493 static int ieee80211_skb_resize(struct ieee80211_local *local,
1494 struct sk_buff *skb,
1495 int head_need, bool may_encrypt)
1497 int tail_need = 0;
1500 * This could be optimised, devices that do full hardware
1501 * crypto (including TKIP MMIC) need no tailroom... But we
1502 * have no drivers for such devices currently.
1504 if (may_encrypt) {
1505 tail_need = IEEE80211_ENCRYPT_TAILROOM;
1506 tail_need -= skb_tailroom(skb);
1507 tail_need = max_t(int, tail_need, 0);
1510 if (head_need || tail_need) {
1511 /* Sorry. Can't account for this any more */
1512 skb_orphan(skb);
1515 if (skb_header_cloned(skb))
1516 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
1517 else
1518 I802_DEBUG_INC(local->tx_expand_skb_head);
1520 if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
1521 printk(KERN_DEBUG "%s: failed to reallocate TX buffer\n",
1522 wiphy_name(local->hw.wiphy));
1523 return -ENOMEM;
1526 /* update truesize too */
1527 skb->truesize += head_need + tail_need;
1529 return 0;
1532 static void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
1533 struct sk_buff *skb)
1535 struct ieee80211_local *local = sdata->local;
1536 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1537 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1538 struct ieee80211_sub_if_data *tmp_sdata;
1539 int headroom;
1540 bool may_encrypt;
1542 rcu_read_lock();
1544 if (unlikely(sdata->vif.type == NL80211_IFTYPE_MONITOR)) {
1545 int hdrlen;
1546 u16 len_rthdr;
1548 info->flags |= IEEE80211_TX_CTL_INJECTED |
1549 IEEE80211_TX_INTFL_HAS_RADIOTAP;
1551 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1552 hdr = (struct ieee80211_hdr *)(skb->data + len_rthdr);
1553 hdrlen = ieee80211_hdrlen(hdr->frame_control);
1555 /* check the header is complete in the frame */
1556 if (likely(skb->len >= len_rthdr + hdrlen)) {
1558 * We process outgoing injected frames that have a
1559 * local address we handle as though they are our
1560 * own frames.
1561 * This code here isn't entirely correct, the local
1562 * MAC address is not necessarily enough to find
1563 * the interface to use; for that proper VLAN/WDS
1564 * support we will need a different mechanism.
1567 list_for_each_entry_rcu(tmp_sdata, &local->interfaces,
1568 list) {
1569 if (!ieee80211_sdata_running(tmp_sdata))
1570 continue;
1571 if (tmp_sdata->vif.type != NL80211_IFTYPE_AP)
1572 continue;
1573 if (compare_ether_addr(tmp_sdata->vif.addr,
1574 hdr->addr2) == 0) {
1575 sdata = tmp_sdata;
1576 break;
1582 may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
1584 headroom = local->tx_headroom;
1585 if (may_encrypt)
1586 headroom += IEEE80211_ENCRYPT_HEADROOM;
1587 headroom -= skb_headroom(skb);
1588 headroom = max_t(int, 0, headroom);
1590 if (ieee80211_skb_resize(local, skb, headroom, may_encrypt)) {
1591 dev_kfree_skb(skb);
1592 rcu_read_unlock();
1593 return;
1596 info->control.vif = &sdata->vif;
1598 if (ieee80211_vif_is_mesh(&sdata->vif) &&
1599 ieee80211_is_data(hdr->frame_control) &&
1600 !is_multicast_ether_addr(hdr->addr1))
1601 if (mesh_nexthop_lookup(skb, sdata)) {
1602 /* skb queued: don't free */
1603 rcu_read_unlock();
1604 return;
1607 ieee80211_set_qos_hdr(local, skb);
1608 ieee80211_tx(sdata, skb, false);
1609 rcu_read_unlock();
1612 netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
1613 struct net_device *dev)
1615 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1616 struct ieee80211_channel *chan = local->hw.conf.channel;
1617 struct ieee80211_radiotap_header *prthdr =
1618 (struct ieee80211_radiotap_header *)skb->data;
1619 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1620 u16 len_rthdr;
1623 * Frame injection is not allowed if beaconing is not allowed
1624 * or if we need radar detection. Beaconing is usually not allowed when
1625 * the mode or operation (Adhoc, AP, Mesh) does not support DFS.
1626 * Passive scan is also used in world regulatory domains where
1627 * your country is not known and as such it should be treated as
1628 * NO TX unless the channel is explicitly allowed in which case
1629 * your current regulatory domain would not have the passive scan
1630 * flag.
1632 * Since AP mode uses monitor interfaces to inject/TX management
1633 * frames we can make AP mode the exception to this rule once it
1634 * supports radar detection as its implementation can deal with
1635 * radar detection by itself. We can do that later by adding a
1636 * monitor flag interfaces used for AP support.
1638 if ((chan->flags & (IEEE80211_CHAN_NO_IBSS | IEEE80211_CHAN_RADAR |
1639 IEEE80211_CHAN_PASSIVE_SCAN)))
1640 goto fail;
1642 /* check for not even having the fixed radiotap header part */
1643 if (unlikely(skb->len < sizeof(struct ieee80211_radiotap_header)))
1644 goto fail; /* too short to be possibly valid */
1646 /* is it a header version we can trust to find length from? */
1647 if (unlikely(prthdr->it_version))
1648 goto fail; /* only version 0 is supported */
1650 /* then there must be a radiotap header with a length we can use */
1651 len_rthdr = ieee80211_get_radiotap_len(skb->data);
1653 /* does the skb contain enough to deliver on the alleged length? */
1654 if (unlikely(skb->len < len_rthdr))
1655 goto fail; /* skb too short for claimed rt header extent */
1658 * fix up the pointers accounting for the radiotap
1659 * header still being in there. We are being given
1660 * a precooked IEEE80211 header so no need for
1661 * normal processing
1663 skb_set_mac_header(skb, len_rthdr);
1665 * these are just fixed to the end of the rt area since we
1666 * don't have any better information and at this point, nobody cares
1668 skb_set_network_header(skb, len_rthdr);
1669 skb_set_transport_header(skb, len_rthdr);
1671 memset(info, 0, sizeof(*info));
1673 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
1675 /* pass the radiotap header up to xmit */
1676 ieee80211_xmit(IEEE80211_DEV_TO_SUB_IF(dev), skb);
1677 return NETDEV_TX_OK;
1679 fail:
1680 dev_kfree_skb(skb);
1681 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
1685 * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
1686 * subinterfaces (wlan#, WDS, and VLAN interfaces)
1687 * @skb: packet to be sent
1688 * @dev: incoming interface
1690 * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
1691 * not be freed, and caller is responsible for either retrying later or freeing
1692 * skb).
1694 * This function takes in an Ethernet header and encapsulates it with suitable
1695 * IEEE 802.11 header based on which interface the packet is coming in. The
1696 * encapsulated packet will then be passed to master interface, wlan#.11, for
1697 * transmission (through low-level driver).
1699 netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
1700 struct net_device *dev)
1702 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1703 struct ieee80211_local *local = sdata->local;
1704 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1705 int ret = NETDEV_TX_BUSY, head_need;
1706 u16 ethertype, hdrlen, meshhdrlen = 0;
1707 __le16 fc;
1708 struct ieee80211_hdr hdr;
1709 struct ieee80211s_hdr mesh_hdr;
1710 const u8 *encaps_data;
1711 int encaps_len, skip_header_bytes;
1712 int nh_pos, h_pos;
1713 struct sta_info *sta = NULL;
1714 u32 sta_flags = 0;
1716 if (unlikely(skb->len < ETH_HLEN)) {
1717 ret = NETDEV_TX_OK;
1718 goto fail;
1721 nh_pos = skb_network_header(skb) - skb->data;
1722 h_pos = skb_transport_header(skb) - skb->data;
1724 /* convert Ethernet header to proper 802.11 header (based on
1725 * operation mode) */
1726 ethertype = (skb->data[12] << 8) | skb->data[13];
1727 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_DATA);
1729 switch (sdata->vif.type) {
1730 case NL80211_IFTYPE_AP_VLAN:
1731 rcu_read_lock();
1732 sta = rcu_dereference(sdata->u.vlan.sta);
1733 if (sta) {
1734 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1735 /* RA TA DA SA */
1736 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
1737 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1738 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1739 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1740 hdrlen = 30;
1741 sta_flags = get_sta_flags(sta);
1743 rcu_read_unlock();
1744 if (sta)
1745 break;
1746 /* fall through */
1747 case NL80211_IFTYPE_AP:
1748 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
1749 /* DA BSSID SA */
1750 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1751 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1752 memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
1753 hdrlen = 24;
1754 break;
1755 case NL80211_IFTYPE_WDS:
1756 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1757 /* RA TA DA SA */
1758 memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
1759 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1760 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1761 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1762 hdrlen = 30;
1763 break;
1764 #ifdef CONFIG_MAC80211_MESH
1765 case NL80211_IFTYPE_MESH_POINT:
1766 if (!sdata->u.mesh.mshcfg.dot11MeshTTL) {
1767 /* Do not send frames with mesh_ttl == 0 */
1768 sdata->u.mesh.mshstats.dropped_frames_ttl++;
1769 ret = NETDEV_TX_OK;
1770 goto fail;
1773 if (compare_ether_addr(sdata->vif.addr,
1774 skb->data + ETH_ALEN) == 0) {
1775 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
1776 skb->data, skb->data + ETH_ALEN);
1777 meshhdrlen = ieee80211_new_mesh_header(&mesh_hdr,
1778 sdata, NULL, NULL, NULL);
1779 } else {
1780 /* packet from other interface */
1781 struct mesh_path *mppath;
1782 int is_mesh_mcast = 1;
1783 const u8 *mesh_da;
1785 rcu_read_lock();
1786 if (is_multicast_ether_addr(skb->data))
1787 /* DA TA mSA AE:SA */
1788 mesh_da = skb->data;
1789 else {
1790 static const u8 bcast[ETH_ALEN] =
1791 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1793 mppath = mpp_path_lookup(skb->data, sdata);
1794 if (mppath) {
1795 /* RA TA mDA mSA AE:DA SA */
1796 mesh_da = mppath->mpp;
1797 is_mesh_mcast = 0;
1798 } else {
1799 /* DA TA mSA AE:SA */
1800 mesh_da = bcast;
1803 hdrlen = ieee80211_fill_mesh_addresses(&hdr, &fc,
1804 mesh_da, sdata->vif.addr);
1805 rcu_read_unlock();
1806 if (is_mesh_mcast)
1807 meshhdrlen =
1808 ieee80211_new_mesh_header(&mesh_hdr,
1809 sdata,
1810 skb->data + ETH_ALEN,
1811 NULL,
1812 NULL);
1813 else
1814 meshhdrlen =
1815 ieee80211_new_mesh_header(&mesh_hdr,
1816 sdata,
1817 NULL,
1818 skb->data,
1819 skb->data + ETH_ALEN);
1822 break;
1823 #endif
1824 case NL80211_IFTYPE_STATION:
1825 memcpy(hdr.addr1, sdata->u.mgd.bssid, ETH_ALEN);
1826 if (sdata->u.mgd.use_4addr && ethertype != ETH_P_PAE) {
1827 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1828 /* RA TA DA SA */
1829 memcpy(hdr.addr2, sdata->vif.addr, ETH_ALEN);
1830 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1831 memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
1832 hdrlen = 30;
1833 } else {
1834 fc |= cpu_to_le16(IEEE80211_FCTL_TODS);
1835 /* BSSID SA DA */
1836 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1837 memcpy(hdr.addr3, skb->data, ETH_ALEN);
1838 hdrlen = 24;
1840 break;
1841 case NL80211_IFTYPE_ADHOC:
1842 /* DA SA BSSID */
1843 memcpy(hdr.addr1, skb->data, ETH_ALEN);
1844 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
1845 memcpy(hdr.addr3, sdata->u.ibss.bssid, ETH_ALEN);
1846 hdrlen = 24;
1847 break;
1848 default:
1849 ret = NETDEV_TX_OK;
1850 goto fail;
1854 * There's no need to try to look up the destination
1855 * if it is a multicast address (which can only happen
1856 * in AP mode)
1858 if (!is_multicast_ether_addr(hdr.addr1)) {
1859 rcu_read_lock();
1860 sta = sta_info_get(sdata, hdr.addr1);
1861 if (sta)
1862 sta_flags = get_sta_flags(sta);
1863 rcu_read_unlock();
1866 /* receiver and we are QoS enabled, use a QoS type frame */
1867 if ((sta_flags & WLAN_STA_WME) && local->hw.queues >= 4) {
1868 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1869 hdrlen += 2;
1873 * Drop unicast frames to unauthorised stations unless they are
1874 * EAPOL frames from the local station.
1876 if (!ieee80211_vif_is_mesh(&sdata->vif) &&
1877 unlikely(!is_multicast_ether_addr(hdr.addr1) &&
1878 !(sta_flags & WLAN_STA_AUTHORIZED) &&
1879 !(ethertype == ETH_P_PAE &&
1880 compare_ether_addr(sdata->vif.addr,
1881 skb->data + ETH_ALEN) == 0))) {
1882 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1883 if (net_ratelimit())
1884 printk(KERN_DEBUG "%s: dropped frame to %pM"
1885 " (unauthorized port)\n", dev->name,
1886 hdr.addr1);
1887 #endif
1889 I802_DEBUG_INC(local->tx_handlers_drop_unauth_port);
1891 ret = NETDEV_TX_OK;
1892 goto fail;
1895 hdr.frame_control = fc;
1896 hdr.duration_id = 0;
1897 hdr.seq_ctrl = 0;
1899 skip_header_bytes = ETH_HLEN;
1900 if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
1901 encaps_data = bridge_tunnel_header;
1902 encaps_len = sizeof(bridge_tunnel_header);
1903 skip_header_bytes -= 2;
1904 } else if (ethertype >= 0x600) {
1905 encaps_data = rfc1042_header;
1906 encaps_len = sizeof(rfc1042_header);
1907 skip_header_bytes -= 2;
1908 } else {
1909 encaps_data = NULL;
1910 encaps_len = 0;
1913 skb_pull(skb, skip_header_bytes);
1914 nh_pos -= skip_header_bytes;
1915 h_pos -= skip_header_bytes;
1917 head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
1920 * So we need to modify the skb header and hence need a copy of
1921 * that. The head_need variable above doesn't, so far, include
1922 * the needed header space that we don't need right away. If we
1923 * can, then we don't reallocate right now but only after the
1924 * frame arrives at the master device (if it does...)
1926 * If we cannot, however, then we will reallocate to include all
1927 * the ever needed space. Also, if we need to reallocate it anyway,
1928 * make it big enough for everything we may ever need.
1931 if (head_need > 0 || skb_cloned(skb)) {
1932 head_need += IEEE80211_ENCRYPT_HEADROOM;
1933 head_need += local->tx_headroom;
1934 head_need = max_t(int, 0, head_need);
1935 if (ieee80211_skb_resize(local, skb, head_need, true))
1936 goto fail;
1939 if (encaps_data) {
1940 memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
1941 nh_pos += encaps_len;
1942 h_pos += encaps_len;
1945 if (meshhdrlen > 0) {
1946 memcpy(skb_push(skb, meshhdrlen), &mesh_hdr, meshhdrlen);
1947 nh_pos += meshhdrlen;
1948 h_pos += meshhdrlen;
1951 if (ieee80211_is_data_qos(fc)) {
1952 __le16 *qos_control;
1954 qos_control = (__le16*) skb_push(skb, 2);
1955 memcpy(skb_push(skb, hdrlen - 2), &hdr, hdrlen - 2);
1957 * Maybe we could actually set some fields here, for now just
1958 * initialise to zero to indicate no special operation.
1960 *qos_control = 0;
1961 } else
1962 memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
1964 nh_pos += hdrlen;
1965 h_pos += hdrlen;
1967 dev->stats.tx_packets++;
1968 dev->stats.tx_bytes += skb->len;
1970 /* Update skb pointers to various headers since this modified frame
1971 * is going to go through Linux networking code that may potentially
1972 * need things like pointer to IP header. */
1973 skb_set_mac_header(skb, 0);
1974 skb_set_network_header(skb, nh_pos);
1975 skb_set_transport_header(skb, h_pos);
1977 memset(info, 0, sizeof(*info));
1979 dev->trans_start = jiffies;
1980 ieee80211_xmit(sdata, skb);
1982 return NETDEV_TX_OK;
1984 fail:
1985 if (ret == NETDEV_TX_OK)
1986 dev_kfree_skb(skb);
1988 return ret;
1993 * ieee80211_clear_tx_pending may not be called in a context where
1994 * it is possible that it packets could come in again.
1996 void ieee80211_clear_tx_pending(struct ieee80211_local *local)
1998 int i;
2000 for (i = 0; i < local->hw.queues; i++)
2001 skb_queue_purge(&local->pending[i]);
2004 static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
2005 struct sk_buff *skb)
2007 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2008 struct ieee80211_sub_if_data *sdata;
2009 struct sta_info *sta;
2010 struct ieee80211_hdr *hdr;
2011 int ret;
2012 bool result = true;
2014 sdata = vif_to_sdata(info->control.vif);
2016 if (info->flags & IEEE80211_TX_INTFL_NEED_TXPROCESSING) {
2017 ieee80211_tx(sdata, skb, true);
2018 } else {
2019 hdr = (struct ieee80211_hdr *)skb->data;
2020 sta = sta_info_get(sdata, hdr->addr1);
2022 ret = __ieee80211_tx(local, &skb, sta, true);
2023 if (ret != IEEE80211_TX_OK)
2024 result = false;
2027 return result;
2031 * Transmit all pending packets. Called from tasklet.
2033 void ieee80211_tx_pending(unsigned long data)
2035 struct ieee80211_local *local = (struct ieee80211_local *)data;
2036 struct ieee80211_sub_if_data *sdata;
2037 unsigned long flags;
2038 int i;
2039 bool txok;
2041 rcu_read_lock();
2043 spin_lock_irqsave(&local->queue_stop_reason_lock, flags);
2044 for (i = 0; i < local->hw.queues; i++) {
2046 * If queue is stopped by something other than due to pending
2047 * frames, or we have no pending frames, proceed to next queue.
2049 if (local->queue_stop_reasons[i] ||
2050 skb_queue_empty(&local->pending[i]))
2051 continue;
2053 while (!skb_queue_empty(&local->pending[i])) {
2054 struct sk_buff *skb = __skb_dequeue(&local->pending[i]);
2055 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2057 if (WARN_ON(!info->control.vif)) {
2058 kfree_skb(skb);
2059 continue;
2062 spin_unlock_irqrestore(&local->queue_stop_reason_lock,
2063 flags);
2065 txok = ieee80211_tx_pending_skb(local, skb);
2066 if (!txok)
2067 __skb_queue_head(&local->pending[i], skb);
2068 spin_lock_irqsave(&local->queue_stop_reason_lock,
2069 flags);
2070 if (!txok)
2071 break;
2074 if (skb_queue_empty(&local->pending[i]))
2075 list_for_each_entry_rcu(sdata, &local->interfaces, list)
2076 netif_tx_wake_queue(
2077 netdev_get_tx_queue(sdata->dev, i));
2079 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
2081 rcu_read_unlock();
2084 /* functions for drivers to get certain frames */
2086 static void ieee80211_beacon_add_tim(struct ieee80211_if_ap *bss,
2087 struct sk_buff *skb,
2088 struct beacon_data *beacon)
2090 u8 *pos, *tim;
2091 int aid0 = 0;
2092 int i, have_bits = 0, n1, n2;
2094 /* Generate bitmap for TIM only if there are any STAs in power save
2095 * mode. */
2096 if (atomic_read(&bss->num_sta_ps) > 0)
2097 /* in the hope that this is faster than
2098 * checking byte-for-byte */
2099 have_bits = !bitmap_empty((unsigned long*)bss->tim,
2100 IEEE80211_MAX_AID+1);
2102 if (bss->dtim_count == 0)
2103 bss->dtim_count = beacon->dtim_period - 1;
2104 else
2105 bss->dtim_count--;
2107 tim = pos = (u8 *) skb_put(skb, 6);
2108 *pos++ = WLAN_EID_TIM;
2109 *pos++ = 4;
2110 *pos++ = bss->dtim_count;
2111 *pos++ = beacon->dtim_period;
2113 if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf))
2114 aid0 = 1;
2116 if (have_bits) {
2117 /* Find largest even number N1 so that bits numbered 1 through
2118 * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
2119 * (N2 + 1) x 8 through 2007 are 0. */
2120 n1 = 0;
2121 for (i = 0; i < IEEE80211_MAX_TIM_LEN; i++) {
2122 if (bss->tim[i]) {
2123 n1 = i & 0xfe;
2124 break;
2127 n2 = n1;
2128 for (i = IEEE80211_MAX_TIM_LEN - 1; i >= n1; i--) {
2129 if (bss->tim[i]) {
2130 n2 = i;
2131 break;
2135 /* Bitmap control */
2136 *pos++ = n1 | aid0;
2137 /* Part Virt Bitmap */
2138 memcpy(pos, bss->tim + n1, n2 - n1 + 1);
2140 tim[1] = n2 - n1 + 4;
2141 skb_put(skb, n2 - n1);
2142 } else {
2143 *pos++ = aid0; /* Bitmap control */
2144 *pos++ = 0; /* Part Virt Bitmap */
2148 struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
2149 struct ieee80211_vif *vif,
2150 u16 *tim_offset, u16 *tim_length)
2152 struct ieee80211_local *local = hw_to_local(hw);
2153 struct sk_buff *skb = NULL;
2154 struct ieee80211_tx_info *info;
2155 struct ieee80211_sub_if_data *sdata = NULL;
2156 struct ieee80211_if_ap *ap = NULL;
2157 struct beacon_data *beacon;
2158 struct ieee80211_supported_band *sband;
2159 enum ieee80211_band band = local->hw.conf.channel->band;
2160 struct ieee80211_tx_rate_control txrc;
2162 sband = local->hw.wiphy->bands[band];
2164 rcu_read_lock();
2166 sdata = vif_to_sdata(vif);
2168 if (tim_offset)
2169 *tim_offset = 0;
2170 if (tim_length)
2171 *tim_length = 0;
2173 if (sdata->vif.type == NL80211_IFTYPE_AP) {
2174 ap = &sdata->u.ap;
2175 beacon = rcu_dereference(ap->beacon);
2176 if (ap && beacon) {
2178 * headroom, head length,
2179 * tail length and maximum TIM length
2181 skb = dev_alloc_skb(local->tx_headroom +
2182 beacon->head_len +
2183 beacon->tail_len + 256);
2184 if (!skb)
2185 goto out;
2187 skb_reserve(skb, local->tx_headroom);
2188 memcpy(skb_put(skb, beacon->head_len), beacon->head,
2189 beacon->head_len);
2192 * Not very nice, but we want to allow the driver to call
2193 * ieee80211_beacon_get() as a response to the set_tim()
2194 * callback. That, however, is already invoked under the
2195 * sta_lock to guarantee consistent and race-free update
2196 * of the tim bitmap in mac80211 and the driver.
2198 if (local->tim_in_locked_section) {
2199 ieee80211_beacon_add_tim(ap, skb, beacon);
2200 } else {
2201 unsigned long flags;
2203 spin_lock_irqsave(&local->sta_lock, flags);
2204 ieee80211_beacon_add_tim(ap, skb, beacon);
2205 spin_unlock_irqrestore(&local->sta_lock, flags);
2208 if (tim_offset)
2209 *tim_offset = beacon->head_len;
2210 if (tim_length)
2211 *tim_length = skb->len - beacon->head_len;
2213 if (beacon->tail)
2214 memcpy(skb_put(skb, beacon->tail_len),
2215 beacon->tail, beacon->tail_len);
2216 } else
2217 goto out;
2218 } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
2219 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2220 struct ieee80211_hdr *hdr;
2221 struct sk_buff *presp = rcu_dereference(ifibss->presp);
2223 if (!presp)
2224 goto out;
2226 skb = skb_copy(presp, GFP_ATOMIC);
2227 if (!skb)
2228 goto out;
2230 hdr = (struct ieee80211_hdr *) skb->data;
2231 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2232 IEEE80211_STYPE_BEACON);
2233 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2234 struct ieee80211_mgmt *mgmt;
2235 u8 *pos;
2237 /* headroom, head length, tail length and maximum TIM length */
2238 skb = dev_alloc_skb(local->tx_headroom + 400);
2239 if (!skb)
2240 goto out;
2242 skb_reserve(skb, local->hw.extra_tx_headroom);
2243 mgmt = (struct ieee80211_mgmt *)
2244 skb_put(skb, 24 + sizeof(mgmt->u.beacon));
2245 memset(mgmt, 0, 24 + sizeof(mgmt->u.beacon));
2246 mgmt->frame_control =
2247 cpu_to_le16(IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_BEACON);
2248 memset(mgmt->da, 0xff, ETH_ALEN);
2249 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
2250 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
2251 mgmt->u.beacon.beacon_int =
2252 cpu_to_le16(sdata->vif.bss_conf.beacon_int);
2253 mgmt->u.beacon.capab_info = 0x0; /* 0x0 for MPs */
2255 pos = skb_put(skb, 2);
2256 *pos++ = WLAN_EID_SSID;
2257 *pos++ = 0x0;
2259 mesh_mgmt_ies_add(skb, sdata);
2260 } else {
2261 WARN_ON(1);
2262 goto out;
2265 info = IEEE80211_SKB_CB(skb);
2267 info->flags |= IEEE80211_TX_INTFL_DONT_ENCRYPT;
2268 info->flags |= IEEE80211_TX_CTL_NO_ACK;
2269 info->band = band;
2271 memset(&txrc, 0, sizeof(txrc));
2272 txrc.hw = hw;
2273 txrc.sband = sband;
2274 txrc.bss_conf = &sdata->vif.bss_conf;
2275 txrc.skb = skb;
2276 txrc.reported_rate.idx = -1;
2277 txrc.rate_idx_mask = sdata->rc_rateidx_mask[band];
2278 if (txrc.rate_idx_mask == (1 << sband->n_bitrates) - 1)
2279 txrc.max_rate_idx = -1;
2280 else
2281 txrc.max_rate_idx = fls(txrc.rate_idx_mask) - 1;
2282 txrc.ap = true;
2283 rate_control_get_rate(sdata, NULL, &txrc);
2285 info->control.vif = vif;
2287 info->flags |= IEEE80211_TX_CTL_CLEAR_PS_FILT |
2288 IEEE80211_TX_CTL_ASSIGN_SEQ |
2289 IEEE80211_TX_CTL_FIRST_FRAGMENT;
2290 out:
2291 rcu_read_unlock();
2292 return skb;
2294 EXPORT_SYMBOL(ieee80211_beacon_get_tim);
2296 struct sk_buff *ieee80211_pspoll_get(struct ieee80211_hw *hw,
2297 struct ieee80211_vif *vif)
2299 struct ieee80211_sub_if_data *sdata;
2300 struct ieee80211_if_managed *ifmgd;
2301 struct ieee80211_pspoll *pspoll;
2302 struct ieee80211_local *local;
2303 struct sk_buff *skb;
2305 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2306 return NULL;
2308 sdata = vif_to_sdata(vif);
2309 ifmgd = &sdata->u.mgd;
2310 local = sdata->local;
2312 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*pspoll));
2313 if (!skb) {
2314 printk(KERN_DEBUG "%s: failed to allocate buffer for "
2315 "pspoll template\n", sdata->name);
2316 return NULL;
2318 skb_reserve(skb, local->hw.extra_tx_headroom);
2320 pspoll = (struct ieee80211_pspoll *) skb_put(skb, sizeof(*pspoll));
2321 memset(pspoll, 0, sizeof(*pspoll));
2322 pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
2323 IEEE80211_STYPE_PSPOLL);
2324 pspoll->aid = cpu_to_le16(ifmgd->aid);
2326 /* aid in PS-Poll has its two MSBs each set to 1 */
2327 pspoll->aid |= cpu_to_le16(1 << 15 | 1 << 14);
2329 memcpy(pspoll->bssid, ifmgd->bssid, ETH_ALEN);
2330 memcpy(pspoll->ta, vif->addr, ETH_ALEN);
2332 return skb;
2334 EXPORT_SYMBOL(ieee80211_pspoll_get);
2336 struct sk_buff *ieee80211_nullfunc_get(struct ieee80211_hw *hw,
2337 struct ieee80211_vif *vif)
2339 struct ieee80211_hdr_3addr *nullfunc;
2340 struct ieee80211_sub_if_data *sdata;
2341 struct ieee80211_if_managed *ifmgd;
2342 struct ieee80211_local *local;
2343 struct sk_buff *skb;
2345 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION))
2346 return NULL;
2348 sdata = vif_to_sdata(vif);
2349 ifmgd = &sdata->u.mgd;
2350 local = sdata->local;
2352 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*nullfunc));
2353 if (!skb) {
2354 printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
2355 "template\n", sdata->name);
2356 return NULL;
2358 skb_reserve(skb, local->hw.extra_tx_headroom);
2360 nullfunc = (struct ieee80211_hdr_3addr *) skb_put(skb,
2361 sizeof(*nullfunc));
2362 memset(nullfunc, 0, sizeof(*nullfunc));
2363 nullfunc->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
2364 IEEE80211_STYPE_NULLFUNC |
2365 IEEE80211_FCTL_TODS);
2366 memcpy(nullfunc->addr1, ifmgd->bssid, ETH_ALEN);
2367 memcpy(nullfunc->addr2, vif->addr, ETH_ALEN);
2368 memcpy(nullfunc->addr3, ifmgd->bssid, ETH_ALEN);
2370 return skb;
2372 EXPORT_SYMBOL(ieee80211_nullfunc_get);
2374 struct sk_buff *ieee80211_probereq_get(struct ieee80211_hw *hw,
2375 struct ieee80211_vif *vif,
2376 const u8 *ssid, size_t ssid_len,
2377 const u8 *ie, size_t ie_len)
2379 struct ieee80211_sub_if_data *sdata;
2380 struct ieee80211_local *local;
2381 struct ieee80211_hdr_3addr *hdr;
2382 struct sk_buff *skb;
2383 size_t ie_ssid_len;
2384 u8 *pos;
2386 sdata = vif_to_sdata(vif);
2387 local = sdata->local;
2388 ie_ssid_len = 2 + ssid_len;
2390 skb = dev_alloc_skb(local->hw.extra_tx_headroom + sizeof(*hdr) +
2391 ie_ssid_len + ie_len);
2392 if (!skb) {
2393 printk(KERN_DEBUG "%s: failed to allocate buffer for probe "
2394 "request template\n", sdata->name);
2395 return NULL;
2398 skb_reserve(skb, local->hw.extra_tx_headroom);
2400 hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
2401 memset(hdr, 0, sizeof(*hdr));
2402 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2403 IEEE80211_STYPE_PROBE_REQ);
2404 memset(hdr->addr1, 0xff, ETH_ALEN);
2405 memcpy(hdr->addr2, vif->addr, ETH_ALEN);
2406 memset(hdr->addr3, 0xff, ETH_ALEN);
2408 pos = skb_put(skb, ie_ssid_len);
2409 *pos++ = WLAN_EID_SSID;
2410 *pos++ = ssid_len;
2411 if (ssid)
2412 memcpy(pos, ssid, ssid_len);
2413 pos += ssid_len;
2415 if (ie) {
2416 pos = skb_put(skb, ie_len);
2417 memcpy(pos, ie, ie_len);
2420 return skb;
2422 EXPORT_SYMBOL(ieee80211_probereq_get);
2424 void ieee80211_rts_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2425 const void *frame, size_t frame_len,
2426 const struct ieee80211_tx_info *frame_txctl,
2427 struct ieee80211_rts *rts)
2429 const struct ieee80211_hdr *hdr = frame;
2431 rts->frame_control =
2432 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_RTS);
2433 rts->duration = ieee80211_rts_duration(hw, vif, frame_len,
2434 frame_txctl);
2435 memcpy(rts->ra, hdr->addr1, sizeof(rts->ra));
2436 memcpy(rts->ta, hdr->addr2, sizeof(rts->ta));
2438 EXPORT_SYMBOL(ieee80211_rts_get);
2440 void ieee80211_ctstoself_get(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2441 const void *frame, size_t frame_len,
2442 const struct ieee80211_tx_info *frame_txctl,
2443 struct ieee80211_cts *cts)
2445 const struct ieee80211_hdr *hdr = frame;
2447 cts->frame_control =
2448 cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_CTS);
2449 cts->duration = ieee80211_ctstoself_duration(hw, vif,
2450 frame_len, frame_txctl);
2451 memcpy(cts->ra, hdr->addr1, sizeof(cts->ra));
2453 EXPORT_SYMBOL(ieee80211_ctstoself_get);
2455 struct sk_buff *
2456 ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
2457 struct ieee80211_vif *vif)
2459 struct ieee80211_local *local = hw_to_local(hw);
2460 struct sk_buff *skb = NULL;
2461 struct sta_info *sta;
2462 struct ieee80211_tx_data tx;
2463 struct ieee80211_sub_if_data *sdata;
2464 struct ieee80211_if_ap *bss = NULL;
2465 struct beacon_data *beacon;
2466 struct ieee80211_tx_info *info;
2468 sdata = vif_to_sdata(vif);
2469 bss = &sdata->u.ap;
2471 rcu_read_lock();
2472 beacon = rcu_dereference(bss->beacon);
2474 if (sdata->vif.type != NL80211_IFTYPE_AP || !beacon || !beacon->head)
2475 goto out;
2477 if (bss->dtim_count != 0)
2478 goto out; /* send buffered bc/mc only after DTIM beacon */
2480 while (1) {
2481 skb = skb_dequeue(&bss->ps_bc_buf);
2482 if (!skb)
2483 goto out;
2484 local->total_ps_buffered--;
2486 if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
2487 struct ieee80211_hdr *hdr =
2488 (struct ieee80211_hdr *) skb->data;
2489 /* more buffered multicast/broadcast frames ==> set
2490 * MoreData flag in IEEE 802.11 header to inform PS
2491 * STAs */
2492 hdr->frame_control |=
2493 cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2496 if (!ieee80211_tx_prepare(sdata, &tx, skb))
2497 break;
2498 dev_kfree_skb_any(skb);
2501 info = IEEE80211_SKB_CB(skb);
2503 sta = tx.sta;
2504 tx.flags |= IEEE80211_TX_PS_BUFFERED;
2505 tx.channel = local->hw.conf.channel;
2506 info->band = tx.channel->band;
2508 if (invoke_tx_handlers(&tx))
2509 skb = NULL;
2510 out:
2511 rcu_read_unlock();
2513 return skb;
2515 EXPORT_SYMBOL(ieee80211_get_buffered_bc);
2517 void ieee80211_tx_skb(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
2519 skb_set_mac_header(skb, 0);
2520 skb_set_network_header(skb, 0);
2521 skb_set_transport_header(skb, 0);
2523 /* send all internal mgmt frames on VO */
2524 skb_set_queue_mapping(skb, 0);
2527 * The other path calling ieee80211_xmit is from the tasklet,
2528 * and while we can handle concurrent transmissions locking
2529 * requirements are that we do not come into tx with bhs on.
2531 local_bh_disable();
2532 ieee80211_xmit(sdata, skb);
2533 local_bh_enable();