main.c revision 078e1e60dd6c6b0d4bc8d58ccb80c008e8efc9ff
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 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <net/mac80211.h>
12#include <net/ieee80211_radiotap.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/netdevice.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/etherdevice.h>
20#include <linux/if_arp.h>
21#include <linux/wireless.h>
22#include <linux/rtnetlink.h>
23#include <linux/bitmap.h>
24#include <net/net_namespace.h>
25#include <net/cfg80211.h>
26
27#include "ieee80211_i.h"
28#include "rate.h"
29#include "mesh.h"
30#include "wep.h"
31#include "wme.h"
32#include "aes_ccm.h"
33#include "led.h"
34#include "cfg.h"
35#include "debugfs.h"
36#include "debugfs_netdev.h"
37
38/*
39 * For seeing transmitted packets on monitor interfaces
40 * we have a radiotap header too.
41 */
42struct ieee80211_tx_status_rtap_hdr {
43	struct ieee80211_radiotap_header hdr;
44	u8 rate;
45	u8 padding_for_rate;
46	__le16 tx_flags;
47	u8 data_retries;
48} __attribute__ ((packed));
49
50
51/* must be called under mdev tx lock */
52void ieee80211_configure_filter(struct ieee80211_local *local)
53{
54	unsigned int changed_flags;
55	unsigned int new_flags = 0;
56
57	if (atomic_read(&local->iff_promiscs))
58		new_flags |= FIF_PROMISC_IN_BSS;
59
60	if (atomic_read(&local->iff_allmultis))
61		new_flags |= FIF_ALLMULTI;
62
63	if (local->monitors)
64		new_flags |= FIF_BCN_PRBRESP_PROMISC;
65
66	if (local->fif_fcsfail)
67		new_flags |= FIF_FCSFAIL;
68
69	if (local->fif_plcpfail)
70		new_flags |= FIF_PLCPFAIL;
71
72	if (local->fif_control)
73		new_flags |= FIF_CONTROL;
74
75	if (local->fif_other_bss)
76		new_flags |= FIF_OTHER_BSS;
77
78	changed_flags = local->filter_flags ^ new_flags;
79
80	/* be a bit nasty */
81	new_flags |= (1<<31);
82
83	local->ops->configure_filter(local_to_hw(local),
84				     changed_flags, &new_flags,
85				     local->mdev->mc_count,
86				     local->mdev->mc_list);
87
88	WARN_ON(new_flags & (1<<31));
89
90	local->filter_flags = new_flags & ~(1<<31);
91}
92
93/* master interface */
94
95static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
96{
97	memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
98	return ETH_ALEN;
99}
100
101static const struct header_ops ieee80211_header_ops = {
102	.create		= eth_header,
103	.parse		= header_parse_80211,
104	.rebuild	= eth_rebuild_header,
105	.cache		= eth_header_cache,
106	.cache_update	= eth_header_cache_update,
107};
108
109static int ieee80211_master_open(struct net_device *dev)
110{
111	struct ieee80211_master_priv *mpriv = netdev_priv(dev);
112	struct ieee80211_local *local = mpriv->local;
113	struct ieee80211_sub_if_data *sdata;
114	int res = -EOPNOTSUPP;
115
116	/* we hold the RTNL here so can safely walk the list */
117	list_for_each_entry(sdata, &local->interfaces, list) {
118		if (netif_running(sdata->dev)) {
119			res = 0;
120			break;
121		}
122	}
123
124	if (res)
125		return res;
126
127	netif_tx_start_all_queues(local->mdev);
128
129	return 0;
130}
131
132static int ieee80211_master_stop(struct net_device *dev)
133{
134	struct ieee80211_master_priv *mpriv = netdev_priv(dev);
135	struct ieee80211_local *local = mpriv->local;
136	struct ieee80211_sub_if_data *sdata;
137
138	/* we hold the RTNL here so can safely walk the list */
139	list_for_each_entry(sdata, &local->interfaces, list)
140		if (netif_running(sdata->dev))
141			dev_close(sdata->dev);
142
143	return 0;
144}
145
146static void ieee80211_master_set_multicast_list(struct net_device *dev)
147{
148	struct ieee80211_master_priv *mpriv = netdev_priv(dev);
149	struct ieee80211_local *local = mpriv->local;
150
151	ieee80211_configure_filter(local);
152}
153
154/* everything else */
155
156int ieee80211_if_config(struct ieee80211_sub_if_data *sdata, u32 changed)
157{
158	struct ieee80211_local *local = sdata->local;
159	struct ieee80211_if_conf conf;
160
161	if (WARN_ON(!netif_running(sdata->dev)))
162		return 0;
163
164	if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
165		return -EINVAL;
166
167	if (!local->ops->config_interface)
168		return 0;
169
170	memset(&conf, 0, sizeof(conf));
171
172	if (sdata->vif.type == NL80211_IFTYPE_STATION ||
173	    sdata->vif.type == NL80211_IFTYPE_ADHOC)
174		conf.bssid = sdata->u.sta.bssid;
175	else if (sdata->vif.type == NL80211_IFTYPE_AP)
176		conf.bssid = sdata->dev->dev_addr;
177	else if (ieee80211_vif_is_mesh(&sdata->vif)) {
178		static const u8 zero[ETH_ALEN] = { 0 };
179		conf.bssid = zero;
180	} else {
181		WARN_ON(1);
182		return -EINVAL;
183	}
184
185	switch (sdata->vif.type) {
186	case NL80211_IFTYPE_AP:
187	case NL80211_IFTYPE_ADHOC:
188	case NL80211_IFTYPE_MESH_POINT:
189		break;
190	default:
191		/* do not warn to simplify caller in scan.c */
192		changed &= ~IEEE80211_IFCC_BEACON_ENABLED;
193		if (WARN_ON(changed & IEEE80211_IFCC_BEACON))
194			return -EINVAL;
195		changed &= ~IEEE80211_IFCC_BEACON;
196		break;
197	}
198
199	if (changed & IEEE80211_IFCC_BEACON_ENABLED) {
200		if (local->sw_scanning) {
201			conf.enable_beacon = false;
202		} else {
203			/*
204			 * Beacon should be enabled, but AP mode must
205			 * check whether there is a beacon configured.
206			 */
207			switch (sdata->vif.type) {
208			case NL80211_IFTYPE_AP:
209				conf.enable_beacon =
210					!!rcu_dereference(sdata->u.ap.beacon);
211				break;
212			case NL80211_IFTYPE_ADHOC:
213			case NL80211_IFTYPE_MESH_POINT:
214				conf.enable_beacon = true;
215				break;
216			default:
217				/* not reached */
218				WARN_ON(1);
219				break;
220			}
221		}
222	}
223
224	if (WARN_ON(!conf.bssid && (changed & IEEE80211_IFCC_BSSID)))
225		return -EINVAL;
226
227	conf.changed = changed;
228
229	return local->ops->config_interface(local_to_hw(local),
230					    &sdata->vif, &conf);
231}
232
233int ieee80211_hw_config(struct ieee80211_local *local, u32 changed)
234{
235	struct ieee80211_channel *chan;
236	int ret = 0;
237	int power;
238	enum nl80211_channel_type channel_type;
239
240	might_sleep();
241
242	if (local->sw_scanning) {
243		chan = local->scan_channel;
244		channel_type = NL80211_CHAN_NO_HT;
245	} else {
246		chan = local->oper_channel;
247		channel_type = local->oper_channel_type;
248	}
249
250	if (chan != local->hw.conf.channel ||
251	    channel_type != local->hw.conf.channel_type) {
252		local->hw.conf.channel = chan;
253		local->hw.conf.channel_type = channel_type;
254		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
255	}
256
257	if (local->sw_scanning)
258		power = chan->max_power;
259	else
260		power = local->power_constr_level ?
261			(chan->max_power - local->power_constr_level) :
262			chan->max_power;
263
264	if (local->user_power_level)
265		power = min(power, local->user_power_level);
266
267	if (local->hw.conf.power_level != power) {
268		changed |= IEEE80211_CONF_CHANGE_POWER;
269		local->hw.conf.power_level = power;
270	}
271
272	if (changed && local->open_count) {
273		ret = local->ops->config(local_to_hw(local), changed);
274		/*
275		 * Goal:
276		 * HW reconfiguration should never fail, the driver has told
277		 * us what it can support so it should live up to that promise.
278		 *
279		 * Current status:
280		 * rfkill is not integrated with mac80211 and a
281		 * configuration command can thus fail if hardware rfkill
282		 * is enabled
283		 *
284		 * FIXME: integrate rfkill with mac80211 and then add this
285		 * WARN_ON() back
286		 *
287		 */
288		/* WARN_ON(ret); */
289	}
290
291	return ret;
292}
293
294void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
295				      u32 changed)
296{
297	struct ieee80211_local *local = sdata->local;
298
299	if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
300		return;
301
302	if (!changed)
303		return;
304
305	if (local->ops->bss_info_changed)
306		local->ops->bss_info_changed(local_to_hw(local),
307					     &sdata->vif,
308					     &sdata->vif.bss_conf,
309					     changed);
310}
311
312u32 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata)
313{
314	sdata->vif.bss_conf.use_cts_prot = false;
315	sdata->vif.bss_conf.use_short_preamble = false;
316	sdata->vif.bss_conf.use_short_slot = false;
317	return BSS_CHANGED_ERP_CTS_PROT |
318	       BSS_CHANGED_ERP_PREAMBLE |
319	       BSS_CHANGED_ERP_SLOT;
320}
321
322void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
323				 struct sk_buff *skb)
324{
325	struct ieee80211_local *local = hw_to_local(hw);
326	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
327	int tmp;
328
329	skb->dev = local->mdev;
330	skb->pkt_type = IEEE80211_TX_STATUS_MSG;
331	skb_queue_tail(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS ?
332		       &local->skb_queue : &local->skb_queue_unreliable, skb);
333	tmp = skb_queue_len(&local->skb_queue) +
334		skb_queue_len(&local->skb_queue_unreliable);
335	while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
336	       (skb = skb_dequeue(&local->skb_queue_unreliable))) {
337		dev_kfree_skb_irq(skb);
338		tmp--;
339		I802_DEBUG_INC(local->tx_status_drop);
340	}
341	tasklet_schedule(&local->tasklet);
342}
343EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
344
345static void ieee80211_tasklet_handler(unsigned long data)
346{
347	struct ieee80211_local *local = (struct ieee80211_local *) data;
348	struct sk_buff *skb;
349	struct ieee80211_rx_status rx_status;
350	struct ieee80211_ra_tid *ra_tid;
351
352	while ((skb = skb_dequeue(&local->skb_queue)) ||
353	       (skb = skb_dequeue(&local->skb_queue_unreliable))) {
354		switch (skb->pkt_type) {
355		case IEEE80211_RX_MSG:
356			/* status is in skb->cb */
357			memcpy(&rx_status, skb->cb, sizeof(rx_status));
358			/* Clear skb->pkt_type in order to not confuse kernel
359			 * netstack. */
360			skb->pkt_type = 0;
361			__ieee80211_rx(local_to_hw(local), skb, &rx_status);
362			break;
363		case IEEE80211_TX_STATUS_MSG:
364			skb->pkt_type = 0;
365			ieee80211_tx_status(local_to_hw(local), skb);
366			break;
367		case IEEE80211_DELBA_MSG:
368			ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
369			ieee80211_stop_tx_ba_cb(local_to_hw(local),
370						ra_tid->ra, ra_tid->tid);
371			dev_kfree_skb(skb);
372			break;
373		case IEEE80211_ADDBA_MSG:
374			ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
375			ieee80211_start_tx_ba_cb(local_to_hw(local),
376						 ra_tid->ra, ra_tid->tid);
377			dev_kfree_skb(skb);
378			break ;
379		default:
380			WARN(1, "mac80211: Packet is of unknown type %d\n",
381			     skb->pkt_type);
382			dev_kfree_skb(skb);
383			break;
384		}
385	}
386}
387
388/* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
389 * make a prepared TX frame (one that has been given to hw) to look like brand
390 * new IEEE 802.11 frame that is ready to go through TX processing again.
391 */
392static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
393				      struct ieee80211_key *key,
394				      struct sk_buff *skb)
395{
396	unsigned int hdrlen, iv_len, mic_len;
397	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
398
399	hdrlen = ieee80211_hdrlen(hdr->frame_control);
400
401	if (!key)
402		goto no_key;
403
404	switch (key->conf.alg) {
405	case ALG_WEP:
406		iv_len = WEP_IV_LEN;
407		mic_len = WEP_ICV_LEN;
408		break;
409	case ALG_TKIP:
410		iv_len = TKIP_IV_LEN;
411		mic_len = TKIP_ICV_LEN;
412		break;
413	case ALG_CCMP:
414		iv_len = CCMP_HDR_LEN;
415		mic_len = CCMP_MIC_LEN;
416		break;
417	default:
418		goto no_key;
419	}
420
421	if (skb->len >= hdrlen + mic_len &&
422	    !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
423		skb_trim(skb, skb->len - mic_len);
424	if (skb->len >= hdrlen + iv_len) {
425		memmove(skb->data + iv_len, skb->data, hdrlen);
426		hdr = (struct ieee80211_hdr *)skb_pull(skb, iv_len);
427	}
428
429no_key:
430	if (ieee80211_is_data_qos(hdr->frame_control)) {
431		hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
432		memmove(skb->data + IEEE80211_QOS_CTL_LEN, skb->data,
433			hdrlen - IEEE80211_QOS_CTL_LEN);
434		skb_pull(skb, IEEE80211_QOS_CTL_LEN);
435	}
436}
437
438static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
439					    struct sta_info *sta,
440					    struct sk_buff *skb)
441{
442	sta->tx_filtered_count++;
443
444	/*
445	 * Clear the TX filter mask for this STA when sending the next
446	 * packet. If the STA went to power save mode, this will happen
447	 * when it wakes up for the next time.
448	 */
449	set_sta_flags(sta, WLAN_STA_CLEAR_PS_FILT);
450
451	/*
452	 * This code races in the following way:
453	 *
454	 *  (1) STA sends frame indicating it will go to sleep and does so
455	 *  (2) hardware/firmware adds STA to filter list, passes frame up
456	 *  (3) hardware/firmware processes TX fifo and suppresses a frame
457	 *  (4) we get TX status before having processed the frame and
458	 *	knowing that the STA has gone to sleep.
459	 *
460	 * This is actually quite unlikely even when both those events are
461	 * processed from interrupts coming in quickly after one another or
462	 * even at the same time because we queue both TX status events and
463	 * RX frames to be processed by a tasklet and process them in the
464	 * same order that they were received or TX status last. Hence, there
465	 * is no race as long as the frame RX is processed before the next TX
466	 * status, which drivers can ensure, see below.
467	 *
468	 * Note that this can only happen if the hardware or firmware can
469	 * actually add STAs to the filter list, if this is done by the
470	 * driver in response to set_tim() (which will only reduce the race
471	 * this whole filtering tries to solve, not completely solve it)
472	 * this situation cannot happen.
473	 *
474	 * To completely solve this race drivers need to make sure that they
475	 *  (a) don't mix the irq-safe/not irq-safe TX status/RX processing
476	 *	functions and
477	 *  (b) always process RX events before TX status events if ordering
478	 *      can be unknown, for example with different interrupt status
479	 *	bits.
480	 */
481	if (test_sta_flags(sta, WLAN_STA_PS) &&
482	    skb_queue_len(&sta->tx_filtered) < STA_MAX_TX_BUFFER) {
483		ieee80211_remove_tx_extra(local, sta->key, skb);
484		skb_queue_tail(&sta->tx_filtered, skb);
485		return;
486	}
487
488	if (!test_sta_flags(sta, WLAN_STA_PS) && !skb->requeue) {
489		/* Software retry the packet once */
490		skb->requeue = 1;
491		ieee80211_remove_tx_extra(local, sta->key, skb);
492		dev_queue_xmit(skb);
493		return;
494	}
495
496#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
497	if (net_ratelimit())
498		printk(KERN_DEBUG "%s: dropped TX filtered frame, "
499		       "queue_len=%d PS=%d @%lu\n",
500		       wiphy_name(local->hw.wiphy),
501		       skb_queue_len(&sta->tx_filtered),
502		       !!test_sta_flags(sta, WLAN_STA_PS), jiffies);
503#endif
504	dev_kfree_skb(skb);
505}
506
507void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
508{
509	struct sk_buff *skb2;
510	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
511	struct ieee80211_local *local = hw_to_local(hw);
512	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
513	u16 frag, type;
514	__le16 fc;
515	struct ieee80211_supported_band *sband;
516	struct ieee80211_tx_status_rtap_hdr *rthdr;
517	struct ieee80211_sub_if_data *sdata;
518	struct net_device *prev_dev = NULL;
519	struct sta_info *sta;
520	int retry_count = -1, i;
521
522	for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
523		/* the HW cannot have attempted that rate */
524		if (i >= hw->max_rates) {
525			info->status.rates[i].idx = -1;
526			info->status.rates[i].count = 0;
527		}
528
529		retry_count += info->status.rates[i].count;
530	}
531	if (retry_count < 0)
532		retry_count = 0;
533
534	rcu_read_lock();
535
536	sband = local->hw.wiphy->bands[info->band];
537
538	sta = sta_info_get(local, hdr->addr1);
539
540	if (sta) {
541		if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
542		    test_sta_flags(sta, WLAN_STA_PS)) {
543			/*
544			 * The STA is in power save mode, so assume
545			 * that this TX packet failed because of that.
546			 */
547			ieee80211_handle_filtered_frame(local, sta, skb);
548			rcu_read_unlock();
549			return;
550		}
551
552		fc = hdr->frame_control;
553
554		if ((info->flags & IEEE80211_TX_STAT_AMPDU_NO_BACK) &&
555		    (ieee80211_is_data_qos(fc))) {
556			u16 tid, ssn;
557			u8 *qc;
558
559			qc = ieee80211_get_qos_ctl(hdr);
560			tid = qc[0] & 0xf;
561			ssn = ((le16_to_cpu(hdr->seq_ctrl) + 0x10)
562						& IEEE80211_SCTL_SEQ);
563			ieee80211_send_bar(sta->sdata, hdr->addr1,
564					   tid, ssn);
565		}
566
567		if (info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
568			ieee80211_handle_filtered_frame(local, sta, skb);
569			rcu_read_unlock();
570			return;
571		} else {
572			if (!(info->flags & IEEE80211_TX_STAT_ACK))
573				sta->tx_retry_failed++;
574			sta->tx_retry_count += retry_count;
575		}
576
577		rate_control_tx_status(local, sband, sta, skb);
578	}
579
580	rcu_read_unlock();
581
582	ieee80211_led_tx(local, 0);
583
584	/* SNMP counters
585	 * Fragments are passed to low-level drivers as separate skbs, so these
586	 * are actually fragments, not frames. Update frame counters only for
587	 * the first fragment of the frame. */
588
589	frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
590	type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
591
592	if (info->flags & IEEE80211_TX_STAT_ACK) {
593		if (frag == 0) {
594			local->dot11TransmittedFrameCount++;
595			if (is_multicast_ether_addr(hdr->addr1))
596				local->dot11MulticastTransmittedFrameCount++;
597			if (retry_count > 0)
598				local->dot11RetryCount++;
599			if (retry_count > 1)
600				local->dot11MultipleRetryCount++;
601		}
602
603		/* This counter shall be incremented for an acknowledged MPDU
604		 * with an individual address in the address 1 field or an MPDU
605		 * with a multicast address in the address 1 field of type Data
606		 * or Management. */
607		if (!is_multicast_ether_addr(hdr->addr1) ||
608		    type == IEEE80211_FTYPE_DATA ||
609		    type == IEEE80211_FTYPE_MGMT)
610			local->dot11TransmittedFragmentCount++;
611	} else {
612		if (frag == 0)
613			local->dot11FailedCount++;
614	}
615
616	/* this was a transmitted frame, but now we want to reuse it */
617	skb_orphan(skb);
618
619	/*
620	 * This is a bit racy but we can avoid a lot of work
621	 * with this test...
622	 */
623	if (!local->monitors && !local->cooked_mntrs) {
624		dev_kfree_skb(skb);
625		return;
626	}
627
628	/* send frame to monitor interfaces now */
629
630	if (skb_headroom(skb) < sizeof(*rthdr)) {
631		printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
632		dev_kfree_skb(skb);
633		return;
634	}
635
636	rthdr = (struct ieee80211_tx_status_rtap_hdr *)
637				skb_push(skb, sizeof(*rthdr));
638
639	memset(rthdr, 0, sizeof(*rthdr));
640	rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
641	rthdr->hdr.it_present =
642		cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
643			    (1 << IEEE80211_RADIOTAP_DATA_RETRIES) |
644			    (1 << IEEE80211_RADIOTAP_RATE));
645
646	if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
647	    !is_multicast_ether_addr(hdr->addr1))
648		rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
649
650	/*
651	 * XXX: Once radiotap gets the bitmap reset thing the vendor
652	 *	extensions proposal contains, we can actually report
653	 *	the whole set of tries we did.
654	 */
655	if ((info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
656	    (info->status.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
657		rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
658	else if (info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
659		rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
660	if (info->status.rates[0].idx >= 0 &&
661	    !(info->status.rates[0].flags & IEEE80211_TX_RC_MCS))
662		rthdr->rate = sband->bitrates[
663				info->status.rates[0].idx].bitrate / 5;
664
665	/* for now report the total retry_count */
666	rthdr->data_retries = retry_count;
667
668	/* XXX: is this sufficient for BPF? */
669	skb_set_mac_header(skb, 0);
670	skb->ip_summed = CHECKSUM_UNNECESSARY;
671	skb->pkt_type = PACKET_OTHERHOST;
672	skb->protocol = htons(ETH_P_802_2);
673	memset(skb->cb, 0, sizeof(skb->cb));
674
675	rcu_read_lock();
676	list_for_each_entry_rcu(sdata, &local->interfaces, list) {
677		if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
678			if (!netif_running(sdata->dev))
679				continue;
680
681			if (prev_dev) {
682				skb2 = skb_clone(skb, GFP_ATOMIC);
683				if (skb2) {
684					skb2->dev = prev_dev;
685					netif_rx(skb2);
686				}
687			}
688
689			prev_dev = sdata->dev;
690		}
691	}
692	if (prev_dev) {
693		skb->dev = prev_dev;
694		netif_rx(skb);
695		skb = NULL;
696	}
697	rcu_read_unlock();
698	dev_kfree_skb(skb);
699}
700EXPORT_SYMBOL(ieee80211_tx_status);
701
702struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
703					const struct ieee80211_ops *ops)
704{
705	struct ieee80211_local *local;
706	int priv_size;
707	struct wiphy *wiphy;
708
709	/* Ensure 32-byte alignment of our private data and hw private data.
710	 * We use the wiphy priv data for both our ieee80211_local and for
711	 * the driver's private data
712	 *
713	 * In memory it'll be like this:
714	 *
715	 * +-------------------------+
716	 * | struct wiphy	    |
717	 * +-------------------------+
718	 * | struct ieee80211_local  |
719	 * +-------------------------+
720	 * | driver's private data   |
721	 * +-------------------------+
722	 *
723	 */
724	priv_size = ((sizeof(struct ieee80211_local) +
725		      NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
726		    priv_data_len;
727
728	wiphy = wiphy_new(&mac80211_config_ops, priv_size);
729
730	if (!wiphy)
731		return NULL;
732
733	wiphy->privid = mac80211_wiphy_privid;
734
735	local = wiphy_priv(wiphy);
736	local->hw.wiphy = wiphy;
737
738	local->hw.priv = (char *)local +
739			 ((sizeof(struct ieee80211_local) +
740			   NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
741
742	BUG_ON(!ops->tx);
743	BUG_ON(!ops->start);
744	BUG_ON(!ops->stop);
745	BUG_ON(!ops->config);
746	BUG_ON(!ops->add_interface);
747	BUG_ON(!ops->remove_interface);
748	BUG_ON(!ops->configure_filter);
749	local->ops = ops;
750
751	/* set up some defaults */
752	local->hw.queues = 1;
753	local->hw.max_rates = 1;
754	local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
755	local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
756	local->hw.conf.long_frame_max_tx_count = 4;
757	local->hw.conf.short_frame_max_tx_count = 7;
758	local->hw.conf.radio_enabled = true;
759
760	INIT_LIST_HEAD(&local->interfaces);
761
762	spin_lock_init(&local->key_lock);
763
764	spin_lock_init(&local->queue_stop_reason_lock);
765
766	INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
767
768	INIT_WORK(&local->dynamic_ps_enable_work,
769		  ieee80211_dynamic_ps_enable_work);
770	INIT_WORK(&local->dynamic_ps_disable_work,
771		  ieee80211_dynamic_ps_disable_work);
772	setup_timer(&local->dynamic_ps_timer,
773		    ieee80211_dynamic_ps_timer, (unsigned long) local);
774
775	sta_info_init(local);
776
777	tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
778		     (unsigned long)local);
779	tasklet_disable(&local->tx_pending_tasklet);
780
781	tasklet_init(&local->tasklet,
782		     ieee80211_tasklet_handler,
783		     (unsigned long) local);
784	tasklet_disable(&local->tasklet);
785
786	skb_queue_head_init(&local->skb_queue);
787	skb_queue_head_init(&local->skb_queue_unreliable);
788
789	return local_to_hw(local);
790}
791EXPORT_SYMBOL(ieee80211_alloc_hw);
792
793int ieee80211_register_hw(struct ieee80211_hw *hw)
794{
795	struct ieee80211_local *local = hw_to_local(hw);
796	int result;
797	enum ieee80211_band band;
798	struct net_device *mdev;
799	struct ieee80211_master_priv *mpriv;
800
801	/*
802	 * generic code guarantees at least one band,
803	 * set this very early because much code assumes
804	 * that hw.conf.channel is assigned
805	 */
806	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
807		struct ieee80211_supported_band *sband;
808
809		sband = local->hw.wiphy->bands[band];
810		if (sband) {
811			/* init channel we're on */
812			local->hw.conf.channel =
813			local->oper_channel =
814			local->scan_channel = &sband->channels[0];
815			break;
816		}
817	}
818
819	/* if low-level driver supports AP, we also support VLAN */
820	if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP))
821		local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
822
823	/* mac80211 always supports monitor */
824	local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
825
826	result = wiphy_register(local->hw.wiphy);
827	if (result < 0)
828		return result;
829
830	/*
831	 * We use the number of queues for feature tests (QoS, HT) internally
832	 * so restrict them appropriately.
833	 */
834	if (hw->queues > IEEE80211_MAX_QUEUES)
835		hw->queues = IEEE80211_MAX_QUEUES;
836	if (hw->ampdu_queues > IEEE80211_MAX_AMPDU_QUEUES)
837		hw->ampdu_queues = IEEE80211_MAX_AMPDU_QUEUES;
838	if (hw->queues < 4)
839		hw->ampdu_queues = 0;
840
841	mdev = alloc_netdev_mq(sizeof(struct ieee80211_master_priv),
842			       "wmaster%d", ether_setup,
843			       ieee80211_num_queues(hw));
844	if (!mdev)
845		goto fail_mdev_alloc;
846
847	mpriv = netdev_priv(mdev);
848	mpriv->local = local;
849	local->mdev = mdev;
850
851	ieee80211_rx_bss_list_init(local);
852
853	mdev->hard_start_xmit = ieee80211_master_start_xmit;
854	mdev->open = ieee80211_master_open;
855	mdev->stop = ieee80211_master_stop;
856	mdev->type = ARPHRD_IEEE80211;
857	mdev->header_ops = &ieee80211_header_ops;
858	mdev->set_multicast_list = ieee80211_master_set_multicast_list;
859
860	local->hw.workqueue =
861		create_freezeable_workqueue(wiphy_name(local->hw.wiphy));
862	if (!local->hw.workqueue) {
863		result = -ENOMEM;
864		goto fail_workqueue;
865	}
866
867	/*
868	 * The hardware needs headroom for sending the frame,
869	 * and we need some headroom for passing the frame to monitor
870	 * interfaces, but never both at the same time.
871	 */
872	local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
873				   sizeof(struct ieee80211_tx_status_rtap_hdr));
874
875	debugfs_hw_add(local);
876
877	if (local->hw.conf.beacon_int < 10)
878		local->hw.conf.beacon_int = 100;
879
880	if (local->hw.max_listen_interval == 0)
881		local->hw.max_listen_interval = 1;
882
883	local->hw.conf.listen_interval = local->hw.max_listen_interval;
884
885	local->wstats_flags |= local->hw.flags & (IEEE80211_HW_SIGNAL_UNSPEC |
886						  IEEE80211_HW_SIGNAL_DB |
887						  IEEE80211_HW_SIGNAL_DBM) ?
888			       IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
889	local->wstats_flags |= local->hw.flags & IEEE80211_HW_NOISE_DBM ?
890			       IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
891	if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
892		local->wstats_flags |= IW_QUAL_DBM;
893
894	result = sta_info_start(local);
895	if (result < 0)
896		goto fail_sta_info;
897
898	rtnl_lock();
899	result = dev_alloc_name(local->mdev, local->mdev->name);
900	if (result < 0)
901		goto fail_dev;
902
903	memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
904	SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
905
906	result = register_netdevice(local->mdev);
907	if (result < 0)
908		goto fail_dev;
909
910	result = ieee80211_init_rate_ctrl_alg(local,
911					      hw->rate_control_algorithm);
912	if (result < 0) {
913		printk(KERN_DEBUG "%s: Failed to initialize rate control "
914		       "algorithm\n", wiphy_name(local->hw.wiphy));
915		goto fail_rate;
916	}
917
918	result = ieee80211_wep_init(local);
919
920	if (result < 0) {
921		printk(KERN_DEBUG "%s: Failed to initialize wep: %d\n",
922		       wiphy_name(local->hw.wiphy), result);
923		goto fail_wep;
924	}
925
926	local->mdev->select_queue = ieee80211_select_queue;
927
928	/* add one default STA interface if supported */
929	if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_STATION)) {
930		result = ieee80211_if_add(local, "wlan%d", NULL,
931					  NL80211_IFTYPE_STATION, NULL);
932		if (result)
933			printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
934			       wiphy_name(local->hw.wiphy));
935	}
936
937	rtnl_unlock();
938
939	ieee80211_led_init(local);
940
941	return 0;
942
943fail_wep:
944	rate_control_deinitialize(local);
945fail_rate:
946	unregister_netdevice(local->mdev);
947	local->mdev = NULL;
948fail_dev:
949	rtnl_unlock();
950	sta_info_stop(local);
951fail_sta_info:
952	debugfs_hw_del(local);
953	destroy_workqueue(local->hw.workqueue);
954fail_workqueue:
955	if (local->mdev)
956		free_netdev(local->mdev);
957fail_mdev_alloc:
958	wiphy_unregister(local->hw.wiphy);
959	return result;
960}
961EXPORT_SYMBOL(ieee80211_register_hw);
962
963void ieee80211_unregister_hw(struct ieee80211_hw *hw)
964{
965	struct ieee80211_local *local = hw_to_local(hw);
966
967	tasklet_kill(&local->tx_pending_tasklet);
968	tasklet_kill(&local->tasklet);
969
970	rtnl_lock();
971
972	/*
973	 * At this point, interface list manipulations are fine
974	 * because the driver cannot be handing us frames any
975	 * more and the tasklet is killed.
976	 */
977
978	/* First, we remove all virtual interfaces. */
979	ieee80211_remove_interfaces(local);
980
981	/* then, finally, remove the master interface */
982	unregister_netdevice(local->mdev);
983
984	rtnl_unlock();
985
986	ieee80211_rx_bss_list_deinit(local);
987	ieee80211_clear_tx_pending(local);
988	sta_info_stop(local);
989	rate_control_deinitialize(local);
990	debugfs_hw_del(local);
991
992	if (skb_queue_len(&local->skb_queue)
993			|| skb_queue_len(&local->skb_queue_unreliable))
994		printk(KERN_WARNING "%s: skb_queue not empty\n",
995		       wiphy_name(local->hw.wiphy));
996	skb_queue_purge(&local->skb_queue);
997	skb_queue_purge(&local->skb_queue_unreliable);
998
999	destroy_workqueue(local->hw.workqueue);
1000	wiphy_unregister(local->hw.wiphy);
1001	ieee80211_wep_free(local);
1002	ieee80211_led_exit(local);
1003	free_netdev(local->mdev);
1004}
1005EXPORT_SYMBOL(ieee80211_unregister_hw);
1006
1007void ieee80211_free_hw(struct ieee80211_hw *hw)
1008{
1009	struct ieee80211_local *local = hw_to_local(hw);
1010
1011	wiphy_free(local->hw.wiphy);
1012}
1013EXPORT_SYMBOL(ieee80211_free_hw);
1014
1015static int __init ieee80211_init(void)
1016{
1017	struct sk_buff *skb;
1018	int ret;
1019
1020	BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
1021	BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
1022		     IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
1023
1024	ret = rc80211_minstrel_init();
1025	if (ret)
1026		return ret;
1027
1028	ret = rc80211_pid_init();
1029	if (ret)
1030		return ret;
1031
1032	ieee80211_debugfs_netdev_init();
1033
1034	return 0;
1035}
1036
1037static void __exit ieee80211_exit(void)
1038{
1039	rc80211_pid_exit();
1040	rc80211_minstrel_exit();
1041
1042	/*
1043	 * For key todo, it'll be empty by now but the work
1044	 * might still be scheduled.
1045	 */
1046	flush_scheduled_work();
1047
1048	if (mesh_allocated)
1049		ieee80211s_stop();
1050
1051	ieee80211_debugfs_netdev_exit();
1052}
1053
1054
1055subsys_initcall(ieee80211_init);
1056module_exit(ieee80211_exit);
1057
1058MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1059MODULE_LICENSE("GPL");
1060