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