blob: 190917a74c3ef4051657b88c813b87b61731e6be [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
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>
Jiri Bencf0706e82007-05-05 11:45:53 -070023#include <linux/bitmap.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070024#include <net/net_namespace.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070025#include <net/cfg80211.h>
26
Jiri Bencf0706e82007-05-05 11:45:53 -070027#include "ieee80211_i.h"
28#include "ieee80211_rate.h"
29#include "wep.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070030#include "wme.h"
31#include "aes_ccm.h"
32#include "ieee80211_led.h"
Michael Wue0eb6852007-09-18 17:29:21 -040033#include "cfg.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070034#include "debugfs.h"
35#include "debugfs_netdev.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070036
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +020037#define SUPP_MCS_SET_LEN 16
38
Johannes Bergb306f452007-07-10 19:32:08 +020039/*
40 * For seeing transmitted packets on monitor interfaces
41 * we have a radiotap header too.
42 */
43struct ieee80211_tx_status_rtap_hdr {
44 struct ieee80211_radiotap_header hdr;
45 __le16 tx_flags;
46 u8 data_retries;
47} __attribute__ ((packed));
48
Johannes Bergb2c258f2007-07-27 15:43:23 +020049/* common interface routines */
Jiri Bencf0706e82007-05-05 11:45:53 -070050
Stephen Hemmingerb95cce32007-09-26 22:13:38 -070051static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
Jiri Bencf0706e82007-05-05 11:45:53 -070052{
Johannes Bergb2c258f2007-07-27 15:43:23 +020053 memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
54 return ETH_ALEN;
Jiri Bencf0706e82007-05-05 11:45:53 -070055}
56
Johannes Berg4150c572007-09-17 01:29:23 -040057/* must be called under mdev tx lock */
58static void ieee80211_configure_filter(struct ieee80211_local *local)
59{
60 unsigned int changed_flags;
61 unsigned int new_flags = 0;
62
Johannes Berg53918992007-09-26 15:19:47 +020063 if (atomic_read(&local->iff_promiscs))
Johannes Berg4150c572007-09-17 01:29:23 -040064 new_flags |= FIF_PROMISC_IN_BSS;
65
Johannes Berg53918992007-09-26 15:19:47 +020066 if (atomic_read(&local->iff_allmultis))
Johannes Berg4150c572007-09-17 01:29:23 -040067 new_flags |= FIF_ALLMULTI;
68
69 if (local->monitors)
Michael Wu8cc9a732008-01-31 19:48:23 +010070 new_flags |= FIF_BCN_PRBRESP_PROMISC;
71
72 if (local->fif_fcsfail)
73 new_flags |= FIF_FCSFAIL;
74
75 if (local->fif_plcpfail)
76 new_flags |= FIF_PLCPFAIL;
77
78 if (local->fif_control)
79 new_flags |= FIF_CONTROL;
80
81 if (local->fif_other_bss)
82 new_flags |= FIF_OTHER_BSS;
Johannes Berg4150c572007-09-17 01:29:23 -040083
84 changed_flags = local->filter_flags ^ new_flags;
85
86 /* be a bit nasty */
87 new_flags |= (1<<31);
88
89 local->ops->configure_filter(local_to_hw(local),
90 changed_flags, &new_flags,
91 local->mdev->mc_count,
92 local->mdev->mc_list);
93
94 WARN_ON(new_flags & (1<<31));
95
96 local->filter_flags = new_flags & ~(1<<31);
97}
98
Johannes Bergb2c258f2007-07-27 15:43:23 +020099/* master interface */
Jiri Bencf0706e82007-05-05 11:45:53 -0700100
101static int ieee80211_master_open(struct net_device *dev)
102{
103 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
104 struct ieee80211_sub_if_data *sdata;
105 int res = -EOPNOTSUPP;
106
Johannes Berg79010422007-09-18 17:29:21 -0400107 /* we hold the RTNL here so can safely walk the list */
108 list_for_each_entry(sdata, &local->interfaces, list) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700109 if (sdata->dev != dev && netif_running(sdata->dev)) {
110 res = 0;
111 break;
112 }
113 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700114 return res;
115}
116
117static int ieee80211_master_stop(struct net_device *dev)
118{
119 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
120 struct ieee80211_sub_if_data *sdata;
121
Johannes Berg79010422007-09-18 17:29:21 -0400122 /* we hold the RTNL here so can safely walk the list */
123 list_for_each_entry(sdata, &local->interfaces, list)
Jiri Bencf0706e82007-05-05 11:45:53 -0700124 if (sdata->dev != dev && netif_running(sdata->dev))
125 dev_close(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700126
127 return 0;
128}
129
Johannes Berg4150c572007-09-17 01:29:23 -0400130static void ieee80211_master_set_multicast_list(struct net_device *dev)
131{
132 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
133
134 ieee80211_configure_filter(local);
135}
136
Johannes Bergb2c258f2007-07-27 15:43:23 +0200137/* regular interfaces */
138
139static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
140{
141 /* FIX: what would be proper limits for MTU?
142 * This interface uses 802.3 frames. */
143 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
144 printk(KERN_WARNING "%s: invalid MTU %d\n",
145 dev->name, new_mtu);
146 return -EINVAL;
147 }
148
149#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
150 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
151#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
152 dev->mtu = new_mtu;
153 return 0;
154}
155
156static inline int identical_mac_addr_allowed(int type1, int type2)
157{
158 return (type1 == IEEE80211_IF_TYPE_MNTR ||
159 type2 == IEEE80211_IF_TYPE_MNTR ||
160 (type1 == IEEE80211_IF_TYPE_AP &&
161 type2 == IEEE80211_IF_TYPE_WDS) ||
162 (type1 == IEEE80211_IF_TYPE_WDS &&
163 (type2 == IEEE80211_IF_TYPE_WDS ||
164 type2 == IEEE80211_IF_TYPE_AP)) ||
165 (type1 == IEEE80211_IF_TYPE_AP &&
166 type2 == IEEE80211_IF_TYPE_VLAN) ||
167 (type1 == IEEE80211_IF_TYPE_VLAN &&
168 (type2 == IEEE80211_IF_TYPE_AP ||
169 type2 == IEEE80211_IF_TYPE_VLAN)));
170}
171
Johannes Bergb2c258f2007-07-27 15:43:23 +0200172static int ieee80211_open(struct net_device *dev)
173{
174 struct ieee80211_sub_if_data *sdata, *nsdata;
175 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
176 struct ieee80211_if_init_conf conf;
177 int res;
Michael Bueschceffefd2008-02-10 14:16:52 +0100178 bool need_hw_reconfig = 0;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200179
180 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400181
Johannes Berg79010422007-09-18 17:29:21 -0400182 /* we hold the RTNL here so can safely walk the list */
183 list_for_each_entry(nsdata, &local->interfaces, list) {
Johannes Bergb2c258f2007-07-27 15:43:23 +0200184 struct net_device *ndev = nsdata->dev;
185
Johannes Berg665e8aa2008-02-21 01:10:07 +0100186 if (ndev != dev && ndev != local->mdev && netif_running(ndev)) {
187 /*
188 * Allow only a single IBSS interface to be up at any
189 * time. This is restricted because beacon distribution
190 * cannot work properly if both are in the same IBSS.
191 *
192 * To remove this restriction we'd have to disallow them
193 * from setting the same SSID on different IBSS interfaces
194 * belonging to the same hardware. Then, however, we're
195 * faced with having to adopt two different TSF timers...
196 */
197 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
198 nsdata->vif.type == IEEE80211_IF_TYPE_IBSS)
199 return -EBUSY;
200
201 /*
202 * Disallow multiple IBSS/STA mode interfaces.
203 *
204 * This is a technical restriction, it is possible although
205 * most likely not IEEE 802.11 compliant to have multiple
206 * STAs with just a single hardware (the TSF timer will not
207 * be adjusted properly.)
208 *
209 * However, because mac80211 uses the master device's BSS
210 * information for each STA/IBSS interface, doing this will
211 * currently corrupt that BSS information completely, unless,
212 * a not very useful case, both STAs are associated to the
213 * same BSS.
214 *
215 * To remove this restriction, the BSS information needs to
216 * be embedded in the STA/IBSS mode sdata instead of using
217 * the master device's BSS structure.
218 */
219 if ((sdata->vif.type == IEEE80211_IF_TYPE_STA ||
220 sdata->vif.type == IEEE80211_IF_TYPE_IBSS) &&
221 (nsdata->vif.type == IEEE80211_IF_TYPE_STA ||
222 nsdata->vif.type == IEEE80211_IF_TYPE_IBSS))
223 return -EBUSY;
224
225 /*
226 * The remaining checks are only performed for interfaces
227 * with the same MAC address.
228 */
229 if (compare_ether_addr(dev->dev_addr, ndev->dev_addr))
230 continue;
231
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400232 /*
233 * check whether it may have the same address
234 */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100235 if (!identical_mac_addr_allowed(sdata->vif.type,
236 nsdata->vif.type))
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400237 return -ENOTUNIQ;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400238
239 /*
240 * can only add VLANs to enabled APs
241 */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100242 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN &&
Johannes Berg665e8aa2008-02-21 01:10:07 +0100243 nsdata->vif.type == IEEE80211_IF_TYPE_AP)
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400244 sdata->u.vlan.ap = nsdata;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200245 }
246 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200247
Johannes Berg51fb61e2007-12-19 01:31:27 +0100248 switch (sdata->vif.type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400249 case IEEE80211_IF_TYPE_WDS:
250 if (is_zero_ether_addr(sdata->u.wds.remote_addr))
251 return -ENOLINK;
252 break;
253 case IEEE80211_IF_TYPE_VLAN:
254 if (!sdata->u.vlan.ap)
255 return -ENOLINK;
256 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +0200257 case IEEE80211_IF_TYPE_AP:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +0200258 case IEEE80211_IF_TYPE_STA:
259 case IEEE80211_IF_TYPE_MNTR:
260 case IEEE80211_IF_TYPE_IBSS:
Johannes Berg6032f932008-02-23 15:17:07 +0100261 case IEEE80211_IF_TYPE_MESH_POINT:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +0200262 /* no special treatment */
263 break;
Johannes Berga2897552007-09-28 14:01:25 +0200264 case IEEE80211_IF_TYPE_INVALID:
265 /* cannot happen */
266 WARN_ON(1);
267 break;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400268 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200269
Johannes Bergb2c258f2007-07-27 15:43:23 +0200270 if (local->open_count == 0) {
271 res = 0;
Johannes Berg4150c572007-09-17 01:29:23 -0400272 if (local->ops->start)
273 res = local->ops->start(local_to_hw(local));
274 if (res)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200275 return res;
Michael Bueschceffefd2008-02-10 14:16:52 +0100276 need_hw_reconfig = 1;
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100277 ieee80211_led_radio(local, local->hw.conf.radio_enabled);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200278 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200279
Johannes Berg51fb61e2007-12-19 01:31:27 +0100280 switch (sdata->vif.type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400281 case IEEE80211_IF_TYPE_VLAN:
282 list_add(&sdata->u.vlan.list, &sdata->u.vlan.ap->u.ap.vlans);
283 /* no need to tell driver */
284 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400285 case IEEE80211_IF_TYPE_MNTR:
Michael Wu3d30d942008-01-31 19:48:27 +0100286 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
287 local->cooked_mntrs++;
288 break;
289 }
290
Johannes Berg4150c572007-09-17 01:29:23 -0400291 /* must be before the call to ieee80211_configure_filter */
Johannes Bergb2c258f2007-07-27 15:43:23 +0200292 local->monitors++;
Michael Wu8cc9a732008-01-31 19:48:23 +0100293 if (local->monitors == 1)
Johannes Berg4150c572007-09-17 01:29:23 -0400294 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
Michael Wu8cc9a732008-01-31 19:48:23 +0100295
296 if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
297 local->fif_fcsfail++;
298 if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
299 local->fif_plcpfail++;
300 if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
301 local->fif_control++;
302 if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
303 local->fif_other_bss++;
304
305 netif_tx_lock_bh(local->mdev);
306 ieee80211_configure_filter(local);
307 netif_tx_unlock_bh(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -0400308 break;
309 case IEEE80211_IF_TYPE_STA:
310 case IEEE80211_IF_TYPE_IBSS:
311 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
312 /* fall through */
313 default:
Johannes Berg32bfd352007-12-19 01:31:26 +0100314 conf.vif = &sdata->vif;
Johannes Berg51fb61e2007-12-19 01:31:27 +0100315 conf.type = sdata->vif.type;
Johannes Berg4150c572007-09-17 01:29:23 -0400316 conf.mac_addr = dev->dev_addr;
317 res = local->ops->add_interface(local_to_hw(local), &conf);
318 if (res && !local->open_count && local->ops->stop)
319 local->ops->stop(local_to_hw(local));
320 if (res)
321 return res;
322
Johannes Bergb2c258f2007-07-27 15:43:23 +0200323 ieee80211_if_config(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200324 ieee80211_reset_erp_info(dev);
Johannes Berg11a843b2007-08-28 17:01:55 -0400325 ieee80211_enable_keys(sdata);
Johannes Berg4150c572007-09-17 01:29:23 -0400326
Johannes Berg51fb61e2007-12-19 01:31:27 +0100327 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Johannes Bergddd3d2b2007-09-26 17:53:20 +0200328 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
Johannes Berg4150c572007-09-17 01:29:23 -0400329 netif_carrier_off(dev);
330 else
331 netif_carrier_on(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200332 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200333
Johannes Berg4150c572007-09-17 01:29:23 -0400334 if (local->open_count == 0) {
335 res = dev_open(local->mdev);
336 WARN_ON(res);
Johannes Berg4150c572007-09-17 01:29:23 -0400337 tasklet_enable(&local->tx_pending_tasklet);
338 tasklet_enable(&local->tasklet);
339 }
340
Johannes Bergc1428b32007-11-16 02:54:53 +0100341 /*
342 * set_multicast_list will be invoked by the networking core
343 * which will check whether any increments here were done in
344 * error and sync them down to the hardware as filter flags.
345 */
346 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
347 atomic_inc(&local->iff_allmultis);
348
349 if (sdata->flags & IEEE80211_SDATA_PROMISC)
350 atomic_inc(&local->iff_promiscs);
351
Johannes Berg4150c572007-09-17 01:29:23 -0400352 local->open_count++;
Michael Bueschceffefd2008-02-10 14:16:52 +0100353 if (need_hw_reconfig)
354 ieee80211_hw_config(local);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200355
356 netif_start_queue(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400357
Johannes Bergb2c258f2007-07-27 15:43:23 +0200358 return 0;
359}
360
Johannes Berg4150c572007-09-17 01:29:23 -0400361static int ieee80211_stop(struct net_device *dev)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200362{
Johannes Berg4150c572007-09-17 01:29:23 -0400363 struct ieee80211_sub_if_data *sdata;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200364 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg4150c572007-09-17 01:29:23 -0400365 struct ieee80211_if_init_conf conf;
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200366 struct sta_info *sta;
367 int i;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200368
Johannes Berg4150c572007-09-17 01:29:23 -0400369 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
370
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200371 list_for_each_entry(sta, &local->sta_list, list) {
Ron Rindjunsky5b3d71d2008-01-13 18:21:58 +0200372 if (sta->dev == dev)
373 for (i = 0; i < STA_TID_NUM; i++)
374 ieee80211_sta_stop_rx_ba_session(sta->dev,
375 sta->addr, i,
376 WLAN_BACK_RECIPIENT,
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200377 WLAN_REASON_QSTA_LEAVE_QBSS);
378 }
379
Johannes Berg4150c572007-09-17 01:29:23 -0400380 netif_stop_queue(dev);
381
Johannes Bergc1428b32007-11-16 02:54:53 +0100382 /*
383 * Don't count this interface for promisc/allmulti while it
384 * is down. dev_mc_unsync() will invoke set_multicast_list
385 * on the master interface which will sync these down to the
386 * hardware as filter flags.
387 */
388 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
389 atomic_dec(&local->iff_allmultis);
390
391 if (sdata->flags & IEEE80211_SDATA_PROMISC)
392 atomic_dec(&local->iff_promiscs);
393
Johannes Berg4150c572007-09-17 01:29:23 -0400394 dev_mc_unsync(local->mdev, dev);
395
Johannes Berg5dfdaf52007-12-19 02:03:33 +0100396 /* APs need special treatment */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100397 if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400398 struct ieee80211_sub_if_data *vlan, *tmp;
Johannes Berg5dfdaf52007-12-19 02:03:33 +0100399 struct beacon_data *old_beacon = sdata->u.ap.beacon;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400400
Johannes Berg5dfdaf52007-12-19 02:03:33 +0100401 /* remove beacon */
402 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
403 synchronize_rcu();
404 kfree(old_beacon);
405
406 /* down all dependent devices, that is VLANs */
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400407 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
408 u.vlan.list)
409 dev_close(vlan->dev);
410 WARN_ON(!list_empty(&sdata->u.ap.vlans));
411 }
412
Johannes Berg4150c572007-09-17 01:29:23 -0400413 local->open_count--;
414
Johannes Berg51fb61e2007-12-19 01:31:27 +0100415 switch (sdata->vif.type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400416 case IEEE80211_IF_TYPE_VLAN:
417 list_del(&sdata->u.vlan.list);
418 sdata->u.vlan.ap = NULL;
419 /* no need to tell driver */
420 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400421 case IEEE80211_IF_TYPE_MNTR:
Michael Wu3d30d942008-01-31 19:48:27 +0100422 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
423 local->cooked_mntrs--;
424 break;
425 }
426
Johannes Berg4150c572007-09-17 01:29:23 -0400427 local->monitors--;
Michael Wu8cc9a732008-01-31 19:48:23 +0100428 if (local->monitors == 0)
Michael Wu8b393f12007-11-28 01:57:08 -0500429 local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
Michael Wu8cc9a732008-01-31 19:48:23 +0100430
431 if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
432 local->fif_fcsfail--;
433 if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
434 local->fif_plcpfail--;
435 if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
436 local->fif_control--;
437 if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
438 local->fif_other_bss--;
439
440 netif_tx_lock_bh(local->mdev);
441 ieee80211_configure_filter(local);
442 netif_tx_unlock_bh(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -0400443 break;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200444 case IEEE80211_IF_TYPE_STA:
445 case IEEE80211_IF_TYPE_IBSS:
446 sdata->u.sta.state = IEEE80211_DISABLED;
447 del_timer_sync(&sdata->u.sta.timer);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400448 /*
Johannes Berg79010422007-09-18 17:29:21 -0400449 * When we get here, the interface is marked down.
450 * Call synchronize_rcu() to wait for the RX path
451 * should it be using the interface and enqueuing
452 * frames at this very time on another CPU.
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400453 */
Johannes Berg79010422007-09-18 17:29:21 -0400454 synchronize_rcu();
Johannes Bergb2c258f2007-07-27 15:43:23 +0200455 skb_queue_purge(&sdata->u.sta.skb_queue);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400456
Zhu Yiece8edd2007-11-22 10:53:21 +0800457 if (local->scan_dev == sdata->dev) {
458 if (!local->ops->hw_scan) {
459 local->sta_sw_scanning = 0;
460 cancel_delayed_work(&local->scan_work);
461 } else
462 local->sta_hw_scanning = 0;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200463 }
Zhu Yiece8edd2007-11-22 10:53:21 +0800464
Johannes Bergb2c258f2007-07-27 15:43:23 +0200465 flush_workqueue(local->hw.workqueue);
Zhu Yia10605e2007-11-22 11:10:22 +0800466
467 sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
468 kfree(sdata->u.sta.extra_ie);
469 sdata->u.sta.extra_ie = NULL;
470 sdata->u.sta.extra_ie_len = 0;
Johannes Berg4150c572007-09-17 01:29:23 -0400471 /* fall through */
472 default:
Johannes Berg32bfd352007-12-19 01:31:26 +0100473 conf.vif = &sdata->vif;
Johannes Berg51fb61e2007-12-19 01:31:27 +0100474 conf.type = sdata->vif.type;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200475 conf.mac_addr = dev->dev_addr;
Johannes Berg4150c572007-09-17 01:29:23 -0400476 /* disable all keys for as long as this netdev is down */
477 ieee80211_disable_keys(sdata);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200478 local->ops->remove_interface(local_to_hw(local), &conf);
479 }
480
Johannes Berg4150c572007-09-17 01:29:23 -0400481 if (local->open_count == 0) {
482 if (netif_running(local->mdev))
483 dev_close(local->mdev);
484
Johannes Berg4150c572007-09-17 01:29:23 -0400485 if (local->ops->stop)
486 local->ops->stop(local_to_hw(local));
487
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100488 ieee80211_led_radio(local, 0);
489
Johannes Berg4150c572007-09-17 01:29:23 -0400490 tasklet_disable(&local->tx_pending_tasklet);
491 tasklet_disable(&local->tasklet);
492 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200493
494 return 0;
495}
496
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200497int ieee80211_start_tx_ba_session(struct ieee80211_hw *hw, u8 *ra, u16 tid)
498{
499 struct ieee80211_local *local = hw_to_local(hw);
500 struct sta_info *sta;
501 struct ieee80211_sub_if_data *sdata;
502 u16 start_seq_num = 0;
503 u8 *state;
504 int ret;
505 DECLARE_MAC_BUF(mac);
506
507 if (tid >= STA_TID_NUM)
508 return -EINVAL;
509
510#ifdef CONFIG_MAC80211_HT_DEBUG
511 printk(KERN_DEBUG "Open BA session requested for %s tid %u\n",
512 print_mac(mac, ra), tid);
513#endif /* CONFIG_MAC80211_HT_DEBUG */
514
515 sta = sta_info_get(local, ra);
516 if (!sta) {
517 printk(KERN_DEBUG "Could not find the station\n");
518 return -ENOENT;
519 }
520
521 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
522
523 /* we have tried too many times, receiver does not want A-MPDU */
524 if (sta->ampdu_mlme.tid_tx[tid].addba_req_num > HT_AGG_MAX_RETRIES) {
525 ret = -EBUSY;
526 goto start_ba_exit;
527 }
528
529 state = &sta->ampdu_mlme.tid_tx[tid].state;
530 /* check if the TID is not in aggregation flow already */
531 if (*state != HT_AGG_STATE_IDLE) {
532#ifdef CONFIG_MAC80211_HT_DEBUG
533 printk(KERN_DEBUG "BA request denied - session is not "
534 "idle on tid %u\n", tid);
535#endif /* CONFIG_MAC80211_HT_DEBUG */
536 ret = -EAGAIN;
537 goto start_ba_exit;
538 }
539
540 /* ensure that TX flow won't interrupt us
541 * until the end of the call to requeue function */
542 spin_lock_bh(&local->mdev->queue_lock);
543
544 /* create a new queue for this aggregation */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200545 ret = ieee80211_ht_agg_queue_add(local, sta, tid);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200546
547 /* case no queue is available to aggregation
548 * don't switch to aggregation */
549 if (ret) {
550#ifdef CONFIG_MAC80211_HT_DEBUG
551 printk(KERN_DEBUG "BA request denied - no queue available for"
552 " tid %d\n", tid);
553#endif /* CONFIG_MAC80211_HT_DEBUG */
554 spin_unlock_bh(&local->mdev->queue_lock);
555 goto start_ba_exit;
556 }
557 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
558
559 /* Ok, the Addba frame hasn't been sent yet, but if the driver calls the
560 * call back right away, it must see that the flow has begun */
561 *state |= HT_ADDBA_REQUESTED_MSK;
562
563 if (local->ops->ampdu_action)
564 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_START,
565 ra, tid, &start_seq_num);
566
567 if (ret) {
568 /* No need to requeue the packets in the agg queue, since we
569 * held the tx lock: no packet could be enqueued to the newly
570 * allocated queue */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200571 ieee80211_ht_agg_queue_remove(local, sta, tid, 0);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200572#ifdef CONFIG_MAC80211_HT_DEBUG
573 printk(KERN_DEBUG "BA request denied - HW or queue unavailable"
574 " for tid %d\n", tid);
575#endif /* CONFIG_MAC80211_HT_DEBUG */
576 spin_unlock_bh(&local->mdev->queue_lock);
577 *state = HT_AGG_STATE_IDLE;
578 goto start_ba_exit;
579 }
580
581 /* Will put all the packets in the new SW queue */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200582 ieee80211_requeue(local, ieee802_1d_to_ac[tid]);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200583 spin_unlock_bh(&local->mdev->queue_lock);
584
585 /* We have most probably almost emptied the legacy queue */
586 /* ieee80211_wake_queue(local_to_hw(local), ieee802_1d_to_ac[tid]); */
587
588 /* send an addBA request */
589 sta->ampdu_mlme.dialog_token_allocator++;
590 sta->ampdu_mlme.tid_tx[tid].dialog_token =
591 sta->ampdu_mlme.dialog_token_allocator;
592 sta->ampdu_mlme.tid_tx[tid].ssn = start_seq_num;
593
594 ieee80211_send_addba_request(sta->dev, ra, tid,
595 sta->ampdu_mlme.tid_tx[tid].dialog_token,
596 sta->ampdu_mlme.tid_tx[tid].ssn,
597 0x40, 5000);
598
599 /* activate the timer for the recipient's addBA response */
600 sta->ampdu_mlme.tid_tx[tid].addba_resp_timer.expires =
601 jiffies + ADDBA_RESP_INTERVAL;
602 add_timer(&sta->ampdu_mlme.tid_tx[tid].addba_resp_timer);
603 printk(KERN_DEBUG "activated addBA response timer on tid %d\n", tid);
604
605start_ba_exit:
606 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
607 sta_info_put(sta);
608 return ret;
609}
610EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
611
612int ieee80211_stop_tx_ba_session(struct ieee80211_hw *hw,
613 u8 *ra, u16 tid,
614 enum ieee80211_back_parties initiator)
615{
616 struct ieee80211_local *local = hw_to_local(hw);
617 struct sta_info *sta;
618 u8 *state;
619 int ret = 0;
620 DECLARE_MAC_BUF(mac);
621
622 if (tid >= STA_TID_NUM)
623 return -EINVAL;
624
625#ifdef CONFIG_MAC80211_HT_DEBUG
626 printk(KERN_DEBUG "Stop a BA session requested for %s tid %u\n",
627 print_mac(mac, ra), tid);
628#endif /* CONFIG_MAC80211_HT_DEBUG */
629
630 sta = sta_info_get(local, ra);
631 if (!sta)
632 return -ENOENT;
633
634 /* check if the TID is in aggregation */
635 state = &sta->ampdu_mlme.tid_tx[tid].state;
636 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
637
638 if (*state != HT_AGG_STATE_OPERATIONAL) {
639#ifdef CONFIG_MAC80211_HT_DEBUG
640 printk(KERN_DEBUG "Try to stop Tx aggregation on"
641 " non active TID\n");
642#endif /* CONFIG_MAC80211_HT_DEBUG */
643 ret = -ENOENT;
644 goto stop_BA_exit;
645 }
646
647 ieee80211_stop_queue(hw, sta->tid_to_tx_q[tid]);
648
649 *state = HT_AGG_STATE_REQ_STOP_BA_MSK |
650 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
651
652 if (local->ops->ampdu_action)
653 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_STOP,
654 ra, tid, NULL);
655
656 /* case HW denied going back to legacy */
657 if (ret) {
658 WARN_ON(ret != -EBUSY);
659 *state = HT_AGG_STATE_OPERATIONAL;
660 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
661 goto stop_BA_exit;
662 }
663
664stop_BA_exit:
665 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
666 sta_info_put(sta);
667 return ret;
668}
669EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
670
671void ieee80211_start_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u16 tid)
672{
673 struct ieee80211_local *local = hw_to_local(hw);
674 struct sta_info *sta;
675 u8 *state;
676 DECLARE_MAC_BUF(mac);
677
678 if (tid >= STA_TID_NUM) {
679 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
680 tid, STA_TID_NUM);
681 return;
682 }
683
684 sta = sta_info_get(local, ra);
685 if (!sta) {
686 printk(KERN_DEBUG "Could not find station: %s\n",
687 print_mac(mac, ra));
688 return;
689 }
690
691 state = &sta->ampdu_mlme.tid_tx[tid].state;
692 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
693
694 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
695 printk(KERN_DEBUG "addBA was not requested yet, state is %d\n",
696 *state);
697 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
698 sta_info_put(sta);
699 return;
700 }
701
702 WARN_ON_ONCE(*state & HT_ADDBA_DRV_READY_MSK);
703
704 *state |= HT_ADDBA_DRV_READY_MSK;
705
706 if (*state == HT_AGG_STATE_OPERATIONAL) {
707 printk(KERN_DEBUG "Aggregation is on for tid %d \n", tid);
708 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
709 }
710 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
711 sta_info_put(sta);
712}
713EXPORT_SYMBOL(ieee80211_start_tx_ba_cb);
714
715void ieee80211_stop_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u8 tid)
716{
717 struct ieee80211_local *local = hw_to_local(hw);
718 struct sta_info *sta;
719 u8 *state;
720 int agg_queue;
721 DECLARE_MAC_BUF(mac);
722
723 if (tid >= STA_TID_NUM) {
724 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
725 tid, STA_TID_NUM);
726 return;
727 }
728
729 printk(KERN_DEBUG "Stop a BA session requested on DA %s tid %d\n",
730 print_mac(mac, ra), tid);
731
732 sta = sta_info_get(local, ra);
733 if (!sta) {
734 printk(KERN_DEBUG "Could not find station: %s\n",
735 print_mac(mac, ra));
736 return;
737 }
738 state = &sta->ampdu_mlme.tid_tx[tid].state;
739
740 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
741 if ((*state & HT_AGG_STATE_REQ_STOP_BA_MSK) == 0) {
742 printk(KERN_DEBUG "unexpected callback to A-MPDU stop\n");
743 sta_info_put(sta);
744 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
745 return;
746 }
747
748 if (*state & HT_AGG_STATE_INITIATOR_MSK)
749 ieee80211_send_delba(sta->dev, ra, tid,
750 WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
751
752 agg_queue = sta->tid_to_tx_q[tid];
753
754 /* avoid ordering issues: we are the only one that can modify
755 * the content of the qdiscs */
756 spin_lock_bh(&local->mdev->queue_lock);
757 /* remove the queue for this aggregation */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200758 ieee80211_ht_agg_queue_remove(local, sta, tid, 1);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200759 spin_unlock_bh(&local->mdev->queue_lock);
760
761 /* we just requeued the all the frames that were in the removed
762 * queue, and since we might miss a softirq we do netif_schedule.
763 * ieee80211_wake_queue is not used here as this queue is not
764 * necessarily stopped */
765 netif_schedule(local->mdev);
766 *state = HT_AGG_STATE_IDLE;
767 sta->ampdu_mlme.tid_tx[tid].addba_req_num = 0;
768 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
769
770 sta_info_put(sta);
771}
772EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb);
773
774void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
775 const u8 *ra, u16 tid)
776{
777 struct ieee80211_local *local = hw_to_local(hw);
778 struct ieee80211_ra_tid *ra_tid;
779 struct sk_buff *skb = dev_alloc_skb(0);
780
781 if (unlikely(!skb)) {
782 if (net_ratelimit())
783 printk(KERN_WARNING "%s: Not enough memory, "
784 "dropping start BA session", skb->dev->name);
785 return;
786 }
787 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
788 memcpy(&ra_tid->ra, ra, ETH_ALEN);
789 ra_tid->tid = tid;
790
791 skb->pkt_type = IEEE80211_ADDBA_MSG;
792 skb_queue_tail(&local->skb_queue, skb);
793 tasklet_schedule(&local->tasklet);
794}
795EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
796
797void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
798 const u8 *ra, u16 tid)
799{
800 struct ieee80211_local *local = hw_to_local(hw);
801 struct ieee80211_ra_tid *ra_tid;
802 struct sk_buff *skb = dev_alloc_skb(0);
803
804 if (unlikely(!skb)) {
805 if (net_ratelimit())
806 printk(KERN_WARNING "%s: Not enough memory, "
807 "dropping stop BA session", skb->dev->name);
808 return;
809 }
810 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
811 memcpy(&ra_tid->ra, ra, ETH_ALEN);
812 ra_tid->tid = tid;
813
814 skb->pkt_type = IEEE80211_DELBA_MSG;
815 skb_queue_tail(&local->skb_queue, skb);
816 tasklet_schedule(&local->tasklet);
817}
818EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
819
Johannes Bergb2c258f2007-07-27 15:43:23 +0200820static void ieee80211_set_multicast_list(struct net_device *dev)
821{
822 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
823 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400824 int allmulti, promisc, sdata_allmulti, sdata_promisc;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200825
Johannes Berg4150c572007-09-17 01:29:23 -0400826 allmulti = !!(dev->flags & IFF_ALLMULTI);
827 promisc = !!(dev->flags & IFF_PROMISC);
Johannes Bergb52f2192007-11-16 01:49:11 +0100828 sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
829 sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
Johannes Berg4150c572007-09-17 01:29:23 -0400830
831 if (allmulti != sdata_allmulti) {
832 if (dev->flags & IFF_ALLMULTI)
Johannes Berg53918992007-09-26 15:19:47 +0200833 atomic_inc(&local->iff_allmultis);
Johannes Berg4150c572007-09-17 01:29:23 -0400834 else
Johannes Berg53918992007-09-26 15:19:47 +0200835 atomic_dec(&local->iff_allmultis);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400836 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200837 }
Johannes Berg4150c572007-09-17 01:29:23 -0400838
839 if (promisc != sdata_promisc) {
840 if (dev->flags & IFF_PROMISC)
Johannes Berg53918992007-09-26 15:19:47 +0200841 atomic_inc(&local->iff_promiscs);
Johannes Berg4150c572007-09-17 01:29:23 -0400842 else
Johannes Berg53918992007-09-26 15:19:47 +0200843 atomic_dec(&local->iff_promiscs);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400844 sdata->flags ^= IEEE80211_SDATA_PROMISC;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200845 }
Johannes Berg4150c572007-09-17 01:29:23 -0400846
847 dev_mc_sync(local->mdev, dev);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200848}
849
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700850static const struct header_ops ieee80211_header_ops = {
851 .create = eth_header,
852 .parse = header_parse_80211,
853 .rebuild = eth_rebuild_header,
854 .cache = eth_header_cache,
855 .cache_update = eth_header_cache_update,
856};
857
Johannes Bergf9d540e2007-09-28 14:02:09 +0200858/* Must not be called for mdev */
Johannes Bergb2c258f2007-07-27 15:43:23 +0200859void ieee80211_if_setup(struct net_device *dev)
860{
861 ether_setup(dev);
862 dev->hard_start_xmit = ieee80211_subif_start_xmit;
863 dev->wireless_handlers = &ieee80211_iw_handler_def;
864 dev->set_multicast_list = ieee80211_set_multicast_list;
865 dev->change_mtu = ieee80211_change_mtu;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200866 dev->open = ieee80211_open;
867 dev->stop = ieee80211_stop;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200868 dev->destructor = ieee80211_if_free;
869}
870
871/* WDS specialties */
872
873int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
874{
875 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
876 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
877 struct sta_info *sta;
Joe Perches0795af52007-10-03 17:59:30 -0700878 DECLARE_MAC_BUF(mac);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200879
880 if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
881 return 0;
882
883 /* Create STA entry for the new peer */
884 sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100885 if (IS_ERR(sta))
886 return PTR_ERR(sta);
Johannes Berg238814f2008-01-28 17:19:37 +0100887
888 sta->flags |= WLAN_STA_AUTHORIZED;
889
Johannes Bergb2c258f2007-07-27 15:43:23 +0200890 sta_info_put(sta);
891
892 /* Remove STA entry for the old peer */
893 sta = sta_info_get(local, sdata->u.wds.remote_addr);
894 if (sta) {
Michael Wube8755e2007-07-27 15:43:23 +0200895 sta_info_free(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200896 sta_info_put(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200897 } else {
898 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
Joe Perches0795af52007-10-03 17:59:30 -0700899 "peer %s\n",
900 dev->name, print_mac(mac, sdata->u.wds.remote_addr));
Johannes Bergb2c258f2007-07-27 15:43:23 +0200901 }
902
903 /* Update WDS link data */
904 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
905
906 return 0;
907}
908
909/* everything else */
910
Johannes Bergb2c258f2007-07-27 15:43:23 +0200911static int __ieee80211_if_config(struct net_device *dev,
912 struct sk_buff *beacon,
913 struct ieee80211_tx_control *control)
914{
915 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
916 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
917 struct ieee80211_if_conf conf;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200918
919 if (!local->ops->config_interface || !netif_running(dev))
920 return 0;
921
922 memset(&conf, 0, sizeof(conf));
Johannes Berg51fb61e2007-12-19 01:31:27 +0100923 conf.type = sdata->vif.type;
924 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
925 sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Johannes Berg4150c572007-09-17 01:29:23 -0400926 conf.bssid = sdata->u.sta.bssid;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200927 conf.ssid = sdata->u.sta.ssid;
928 conf.ssid_len = sdata->u.sta.ssid_len;
Johannes Berg51fb61e2007-12-19 01:31:27 +0100929 } else if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
Johannes Bergb2c258f2007-07-27 15:43:23 +0200930 conf.ssid = sdata->u.ap.ssid;
931 conf.ssid_len = sdata->u.ap.ssid_len;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200932 conf.beacon = beacon;
933 conf.beacon_control = control;
934 }
935 return local->ops->config_interface(local_to_hw(local),
Johannes Berg32bfd352007-12-19 01:31:26 +0100936 &sdata->vif, &conf);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200937}
938
939int ieee80211_if_config(struct net_device *dev)
940{
941 return __ieee80211_if_config(dev, NULL, NULL);
942}
943
944int ieee80211_if_config_beacon(struct net_device *dev)
945{
946 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
947 struct ieee80211_tx_control control;
Johannes Berg32bfd352007-12-19 01:31:26 +0100948 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200949 struct sk_buff *skb;
950
951 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
952 return 0;
Johannes Berg32bfd352007-12-19 01:31:26 +0100953 skb = ieee80211_beacon_get(local_to_hw(local), &sdata->vif,
954 &control);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200955 if (!skb)
956 return -ENOMEM;
957 return __ieee80211_if_config(dev, skb, &control);
958}
959
960int ieee80211_hw_config(struct ieee80211_local *local)
961{
Johannes Bergb2c258f2007-07-27 15:43:23 +0200962 struct ieee80211_channel *chan;
963 int ret = 0;
964
Johannes Berg8318d782008-01-24 19:38:38 +0100965 if (local->sta_sw_scanning)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200966 chan = local->scan_channel;
Johannes Berg8318d782008-01-24 19:38:38 +0100967 else
Johannes Bergb2c258f2007-07-27 15:43:23 +0200968 chan = local->oper_channel;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200969
Johannes Berg8318d782008-01-24 19:38:38 +0100970 local->hw.conf.channel = chan;
971
972 if (!local->hw.conf.power_level)
973 local->hw.conf.power_level = chan->max_power;
974 else
975 local->hw.conf.power_level = min(chan->max_power,
976 local->hw.conf.power_level);
977
978 local->hw.conf.max_antenna_gain = chan->max_antenna_gain;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200979
980#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg8318d782008-01-24 19:38:38 +0100981 printk(KERN_DEBUG "%s: HW CONFIG: freq=%d\n",
982 wiphy_name(local->hw.wiphy), chan->center_freq);
983#endif
Johannes Bergb2c258f2007-07-27 15:43:23 +0200984
Michael Bueschf7c4dae2007-09-24 18:41:49 +0200985 if (local->open_count)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200986 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
987
988 return ret;
989}
990
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +0200991/**
992 * ieee80211_hw_config_ht should be used only after legacy configuration
993 * has been determined, as ht configuration depends upon the hardware's
994 * HT abilities for a _specific_ band.
995 */
996int ieee80211_hw_config_ht(struct ieee80211_local *local, int enable_ht,
997 struct ieee80211_ht_info *req_ht_cap,
998 struct ieee80211_ht_bss_info *req_bss_cap)
999{
1000 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg8318d782008-01-24 19:38:38 +01001001 struct ieee80211_supported_band *sband;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001002 int i;
1003
Johannes Berg8318d782008-01-24 19:38:38 +01001004 sband = local->hw.wiphy->bands[conf->channel->band];
1005
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001006 /* HT is not supported */
Johannes Berg8318d782008-01-24 19:38:38 +01001007 if (!sband->ht_info.ht_supported) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001008 conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
1009 return -EOPNOTSUPP;
1010 }
1011
1012 /* disable HT */
1013 if (!enable_ht) {
1014 conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
1015 } else {
1016 conf->flags |= IEEE80211_CONF_SUPPORT_HT_MODE;
Johannes Berg8318d782008-01-24 19:38:38 +01001017 conf->ht_conf.cap = req_ht_cap->cap & sband->ht_info.cap;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001018 conf->ht_conf.cap &= ~(IEEE80211_HT_CAP_MIMO_PS);
1019 conf->ht_conf.cap |=
Johannes Berg8318d782008-01-24 19:38:38 +01001020 sband->ht_info.cap & IEEE80211_HT_CAP_MIMO_PS;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001021 conf->ht_bss_conf.primary_channel =
1022 req_bss_cap->primary_channel;
1023 conf->ht_bss_conf.bss_cap = req_bss_cap->bss_cap;
1024 conf->ht_bss_conf.bss_op_mode = req_bss_cap->bss_op_mode;
1025 for (i = 0; i < SUPP_MCS_SET_LEN; i++)
1026 conf->ht_conf.supp_mcs_set[i] =
Johannes Berg8318d782008-01-24 19:38:38 +01001027 sband->ht_info.supp_mcs_set[i] &
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001028 req_ht_cap->supp_mcs_set[i];
1029
1030 /* In STA mode, this gives us indication
1031 * to the AP's mode of operation */
1032 conf->ht_conf.ht_supported = 1;
1033 conf->ht_conf.ampdu_factor = req_ht_cap->ampdu_factor;
1034 conf->ht_conf.ampdu_density = req_ht_cap->ampdu_density;
1035 }
1036
1037 local->ops->conf_ht(local_to_hw(local), &local->hw.conf);
1038
1039 return 0;
1040}
1041
Johannes Berg471b3ef2007-12-28 14:32:58 +01001042void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
1043 u32 changed)
Daniel Draked9430a32007-07-27 15:43:24 +02001044{
Johannes Berg471b3ef2007-12-28 14:32:58 +01001045 struct ieee80211_local *local = sdata->local;
1046
1047 if (!changed)
1048 return;
1049
1050 if (local->ops->bss_info_changed)
1051 local->ops->bss_info_changed(local_to_hw(local),
1052 &sdata->vif,
1053 &sdata->bss_conf,
1054 changed);
Daniel Draked9430a32007-07-27 15:43:24 +02001055}
1056
1057void ieee80211_reset_erp_info(struct net_device *dev)
1058{
1059 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1060
Johannes Berg471b3ef2007-12-28 14:32:58 +01001061 sdata->bss_conf.use_cts_prot = 0;
1062 sdata->bss_conf.use_short_preamble = 0;
1063 ieee80211_bss_info_change_notify(sdata,
1064 BSS_CHANGED_ERP_CTS_PROT |
1065 BSS_CHANGED_ERP_PREAMBLE);
Daniel Draked9430a32007-07-27 15:43:24 +02001066}
1067
Jiri Bencf0706e82007-05-05 11:45:53 -07001068void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
1069 struct sk_buff *skb,
1070 struct ieee80211_tx_status *status)
1071{
1072 struct ieee80211_local *local = hw_to_local(hw);
1073 struct ieee80211_tx_status *saved;
1074 int tmp;
1075
1076 skb->dev = local->mdev;
1077 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
1078 if (unlikely(!saved)) {
1079 if (net_ratelimit())
1080 printk(KERN_WARNING "%s: Not enough memory, "
1081 "dropping tx status", skb->dev->name);
1082 /* should be dev_kfree_skb_irq, but due to this function being
1083 * named _irqsafe instead of just _irq we can't be sure that
1084 * people won't call it from non-irq contexts */
1085 dev_kfree_skb_any(skb);
1086 return;
1087 }
1088 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
1089 /* copy pointer to saved status into skb->cb for use by tasklet */
1090 memcpy(skb->cb, &saved, sizeof(saved));
1091
1092 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
1093 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
1094 &local->skb_queue : &local->skb_queue_unreliable, skb);
1095 tmp = skb_queue_len(&local->skb_queue) +
1096 skb_queue_len(&local->skb_queue_unreliable);
1097 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
1098 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
1099 memcpy(&saved, skb->cb, sizeof(saved));
1100 kfree(saved);
1101 dev_kfree_skb_irq(skb);
1102 tmp--;
1103 I802_DEBUG_INC(local->tx_status_drop);
1104 }
1105 tasklet_schedule(&local->tasklet);
1106}
1107EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
1108
1109static void ieee80211_tasklet_handler(unsigned long data)
1110{
1111 struct ieee80211_local *local = (struct ieee80211_local *) data;
1112 struct sk_buff *skb;
1113 struct ieee80211_rx_status rx_status;
1114 struct ieee80211_tx_status *tx_status;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001115 struct ieee80211_ra_tid *ra_tid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001116
1117 while ((skb = skb_dequeue(&local->skb_queue)) ||
1118 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
1119 switch (skb->pkt_type) {
1120 case IEEE80211_RX_MSG:
1121 /* status is in skb->cb */
1122 memcpy(&rx_status, skb->cb, sizeof(rx_status));
Johannes Berg51fb61e2007-12-19 01:31:27 +01001123 /* Clear skb->pkt_type in order to not confuse kernel
Jiri Bencf0706e82007-05-05 11:45:53 -07001124 * netstack. */
1125 skb->pkt_type = 0;
1126 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
1127 break;
1128 case IEEE80211_TX_STATUS_MSG:
1129 /* get pointer to saved status out of skb->cb */
1130 memcpy(&tx_status, skb->cb, sizeof(tx_status));
1131 skb->pkt_type = 0;
1132 ieee80211_tx_status(local_to_hw(local),
1133 skb, tx_status);
1134 kfree(tx_status);
1135 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001136 case IEEE80211_DELBA_MSG:
1137 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
1138 ieee80211_stop_tx_ba_cb(local_to_hw(local),
1139 ra_tid->ra, ra_tid->tid);
1140 dev_kfree_skb(skb);
1141 break;
1142 case IEEE80211_ADDBA_MSG:
1143 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
1144 ieee80211_start_tx_ba_cb(local_to_hw(local),
1145 ra_tid->ra, ra_tid->tid);
1146 dev_kfree_skb(skb);
1147 break ;
Jiri Bencf0706e82007-05-05 11:45:53 -07001148 default: /* should never get here! */
1149 printk(KERN_ERR "%s: Unknown message type (%d)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001150 wiphy_name(local->hw.wiphy), skb->pkt_type);
Jiri Bencf0706e82007-05-05 11:45:53 -07001151 dev_kfree_skb(skb);
1152 break;
1153 }
1154 }
1155}
1156
Jiri Bencf0706e82007-05-05 11:45:53 -07001157/* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
1158 * make a prepared TX frame (one that has been given to hw) to look like brand
1159 * new IEEE 802.11 frame that is ready to go through TX processing again.
1160 * Also, tx_packet_data in cb is restored from tx_control. */
1161static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
1162 struct ieee80211_key *key,
1163 struct sk_buff *skb,
1164 struct ieee80211_tx_control *control)
1165{
1166 int hdrlen, iv_len, mic_len;
1167 struct ieee80211_tx_packet_data *pkt_data;
1168
1169 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
Johannes Berg32bfd352007-12-19 01:31:26 +01001170 pkt_data->ifindex = vif_to_sdata(control->vif)->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001171 pkt_data->flags = 0;
1172 if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
1173 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
1174 if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
1175 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
1176 if (control->flags & IEEE80211_TXCTL_REQUEUE)
1177 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg678f5f712007-12-19 01:31:23 +01001178 if (control->flags & IEEE80211_TXCTL_EAPOL_FRAME)
1179 pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
Jiri Bencf0706e82007-05-05 11:45:53 -07001180 pkt_data->queue = control->queue;
1181
1182 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1183
1184 if (!key)
1185 goto no_key;
1186
Johannes Berg8f20fc22007-08-28 17:01:54 -04001187 switch (key->conf.alg) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001188 case ALG_WEP:
1189 iv_len = WEP_IV_LEN;
1190 mic_len = WEP_ICV_LEN;
1191 break;
1192 case ALG_TKIP:
1193 iv_len = TKIP_IV_LEN;
1194 mic_len = TKIP_ICV_LEN;
1195 break;
1196 case ALG_CCMP:
1197 iv_len = CCMP_HDR_LEN;
1198 mic_len = CCMP_MIC_LEN;
1199 break;
1200 default:
1201 goto no_key;
1202 }
1203
Johannes Berg8f20fc22007-08-28 17:01:54 -04001204 if (skb->len >= mic_len &&
Johannes Berg11a843b2007-08-28 17:01:55 -04001205 !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Jiri Bencf0706e82007-05-05 11:45:53 -07001206 skb_trim(skb, skb->len - mic_len);
1207 if (skb->len >= iv_len && skb->len > hdrlen) {
1208 memmove(skb->data + iv_len, skb->data, hdrlen);
1209 skb_pull(skb, iv_len);
1210 }
1211
1212no_key:
1213 {
1214 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1215 u16 fc = le16_to_cpu(hdr->frame_control);
1216 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
1217 fc &= ~IEEE80211_STYPE_QOS_DATA;
1218 hdr->frame_control = cpu_to_le16(fc);
1219 memmove(skb->data + 2, skb->data, hdrlen - 2);
1220 skb_pull(skb, 2);
1221 }
1222 }
1223}
1224
Johannes Bergd46e1442008-02-20 23:59:33 +01001225static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
1226 struct sta_info *sta,
1227 struct sk_buff *skb,
1228 struct ieee80211_tx_status *status)
1229{
1230 sta->tx_filtered_count++;
1231
1232 /*
1233 * Clear the TX filter mask for this STA when sending the next
1234 * packet. If the STA went to power save mode, this will happen
1235 * happen when it wakes up for the next time.
1236 */
1237 sta->flags |= WLAN_STA_CLEAR_PS_FILT;
1238
1239 /*
1240 * This code races in the following way:
1241 *
1242 * (1) STA sends frame indicating it will go to sleep and does so
1243 * (2) hardware/firmware adds STA to filter list, passes frame up
1244 * (3) hardware/firmware processes TX fifo and suppresses a frame
1245 * (4) we get TX status before having processed the frame and
1246 * knowing that the STA has gone to sleep.
1247 *
1248 * This is actually quite unlikely even when both those events are
1249 * processed from interrupts coming in quickly after one another or
1250 * even at the same time because we queue both TX status events and
1251 * RX frames to be processed by a tasklet and process them in the
1252 * same order that they were received or TX status last. Hence, there
1253 * is no race as long as the frame RX is processed before the next TX
1254 * status, which drivers can ensure, see below.
1255 *
1256 * Note that this can only happen if the hardware or firmware can
1257 * actually add STAs to the filter list, if this is done by the
1258 * driver in response to set_tim() (which will only reduce the race
1259 * this whole filtering tries to solve, not completely solve it)
1260 * this situation cannot happen.
1261 *
1262 * To completely solve this race drivers need to make sure that they
1263 * (a) don't mix the irq-safe/not irq-safe TX status/RX processing
1264 * functions and
1265 * (b) always process RX events before TX status events if ordering
1266 * can be unknown, for example with different interrupt status
1267 * bits.
1268 */
1269 if (sta->flags & WLAN_STA_PS &&
1270 skb_queue_len(&sta->tx_filtered) < STA_MAX_TX_BUFFER) {
1271 ieee80211_remove_tx_extra(local, sta->key, skb,
1272 &status->control);
1273 skb_queue_tail(&sta->tx_filtered, skb);
1274 return;
1275 }
1276
1277 if (!(sta->flags & WLAN_STA_PS) &&
1278 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
1279 /* Software retry the packet once */
1280 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
1281 ieee80211_remove_tx_extra(local, sta->key, skb,
1282 &status->control);
1283 dev_queue_xmit(skb);
1284 return;
1285 }
1286
1287 if (net_ratelimit())
1288 printk(KERN_DEBUG "%s: dropped TX filtered frame, "
1289 "queue_len=%d PS=%d @%lu\n",
1290 wiphy_name(local->hw.wiphy),
1291 skb_queue_len(&sta->tx_filtered),
1292 !!(sta->flags & WLAN_STA_PS), jiffies);
1293 dev_kfree_skb(skb);
1294}
1295
Jiri Bencf0706e82007-05-05 11:45:53 -07001296void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
1297 struct ieee80211_tx_status *status)
1298{
1299 struct sk_buff *skb2;
1300 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1301 struct ieee80211_local *local = hw_to_local(hw);
1302 u16 frag, type;
Johannes Bergb306f452007-07-10 19:32:08 +02001303 struct ieee80211_tx_status_rtap_hdr *rthdr;
1304 struct ieee80211_sub_if_data *sdata;
Michael Wu3d30d942008-01-31 19:48:27 +01001305 struct net_device *prev_dev = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001306
1307 if (!status) {
1308 printk(KERN_ERR
1309 "%s: ieee80211_tx_status called with NULL status\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001310 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001311 dev_kfree_skb(skb);
1312 return;
1313 }
1314
1315 if (status->excessive_retries) {
1316 struct sta_info *sta;
1317 sta = sta_info_get(local, hdr->addr1);
1318 if (sta) {
1319 if (sta->flags & WLAN_STA_PS) {
Johannes Bergd46e1442008-02-20 23:59:33 +01001320 /*
1321 * The STA is in power save mode, so assume
Jiri Bencf0706e82007-05-05 11:45:53 -07001322 * that this TX packet failed because of that.
1323 */
1324 status->excessive_retries = 0;
1325 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
Johannes Bergd46e1442008-02-20 23:59:33 +01001326 ieee80211_handle_filtered_frame(local, sta,
1327 skb, status);
1328 sta_info_put(sta);
1329 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001330 }
1331 sta_info_put(sta);
1332 }
1333 }
1334
1335 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
1336 struct sta_info *sta;
1337 sta = sta_info_get(local, hdr->addr1);
1338 if (sta) {
Johannes Bergd46e1442008-02-20 23:59:33 +01001339 ieee80211_handle_filtered_frame(local, sta, skb,
1340 status);
Jiri Bencf0706e82007-05-05 11:45:53 -07001341 sta_info_put(sta);
1342 return;
1343 }
Mattias Nissler1abbe492007-12-20 13:50:07 +01001344 } else
1345 rate_control_tx_status(local->mdev, skb, status);
Jiri Bencf0706e82007-05-05 11:45:53 -07001346
1347 ieee80211_led_tx(local, 0);
1348
1349 /* SNMP counters
1350 * Fragments are passed to low-level drivers as separate skbs, so these
1351 * are actually fragments, not frames. Update frame counters only for
1352 * the first fragment of the frame. */
1353
1354 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
1355 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
1356
1357 if (status->flags & IEEE80211_TX_STATUS_ACK) {
1358 if (frag == 0) {
1359 local->dot11TransmittedFrameCount++;
1360 if (is_multicast_ether_addr(hdr->addr1))
1361 local->dot11MulticastTransmittedFrameCount++;
1362 if (status->retry_count > 0)
1363 local->dot11RetryCount++;
1364 if (status->retry_count > 1)
1365 local->dot11MultipleRetryCount++;
1366 }
1367
1368 /* This counter shall be incremented for an acknowledged MPDU
1369 * with an individual address in the address 1 field or an MPDU
1370 * with a multicast address in the address 1 field of type Data
1371 * or Management. */
1372 if (!is_multicast_ether_addr(hdr->addr1) ||
1373 type == IEEE80211_FTYPE_DATA ||
1374 type == IEEE80211_FTYPE_MGMT)
1375 local->dot11TransmittedFragmentCount++;
1376 } else {
1377 if (frag == 0)
1378 local->dot11FailedCount++;
1379 }
1380
Johannes Bergb306f452007-07-10 19:32:08 +02001381 /* this was a transmitted frame, but now we want to reuse it */
1382 skb_orphan(skb);
1383
Michael Wu3d30d942008-01-31 19:48:27 +01001384 /*
1385 * This is a bit racy but we can avoid a lot of work
1386 * with this test...
1387 */
1388 if (!local->monitors && !local->cooked_mntrs) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001389 dev_kfree_skb(skb);
1390 return;
1391 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001392
Johannes Bergb306f452007-07-10 19:32:08 +02001393 /* send frame to monitor interfaces now */
1394
1395 if (skb_headroom(skb) < sizeof(*rthdr)) {
1396 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
1397 dev_kfree_skb(skb);
1398 return;
1399 }
1400
1401 rthdr = (struct ieee80211_tx_status_rtap_hdr*)
1402 skb_push(skb, sizeof(*rthdr));
1403
1404 memset(rthdr, 0, sizeof(*rthdr));
1405 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
1406 rthdr->hdr.it_present =
1407 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
1408 (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
1409
1410 if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
1411 !is_multicast_ether_addr(hdr->addr1))
1412 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
1413
1414 if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
1415 (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
1416 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
1417 else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
1418 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
1419
1420 rthdr->data_retries = status->retry_count;
1421
Michael Wu3d30d942008-01-31 19:48:27 +01001422 /* XXX: is this sufficient for BPF? */
1423 skb_set_mac_header(skb, 0);
1424 skb->ip_summed = CHECKSUM_UNNECESSARY;
1425 skb->pkt_type = PACKET_OTHERHOST;
1426 skb->protocol = htons(ETH_P_802_2);
1427 memset(skb->cb, 0, sizeof(skb->cb));
Johannes Bergb306f452007-07-10 19:32:08 +02001428
Michael Wu3d30d942008-01-31 19:48:27 +01001429 rcu_read_lock();
1430 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg51fb61e2007-12-19 01:31:27 +01001431 if (sdata->vif.type == IEEE80211_IF_TYPE_MNTR) {
Johannes Bergb306f452007-07-10 19:32:08 +02001432 if (!netif_running(sdata->dev))
1433 continue;
Michael Wu3d30d942008-01-31 19:48:27 +01001434
1435 if (prev_dev) {
Johannes Berg79010422007-09-18 17:29:21 -04001436 skb2 = skb_clone(skb, GFP_ATOMIC);
Michael Wu3d30d942008-01-31 19:48:27 +01001437 if (skb2) {
1438 skb2->dev = prev_dev;
1439 netif_rx(skb2);
1440 }
1441 }
1442
1443 prev_dev = sdata->dev;
Johannes Bergb306f452007-07-10 19:32:08 +02001444 }
1445 }
Michael Wu3d30d942008-01-31 19:48:27 +01001446 if (prev_dev) {
1447 skb->dev = prev_dev;
1448 netif_rx(skb);
1449 skb = NULL;
1450 }
Johannes Berg79010422007-09-18 17:29:21 -04001451 rcu_read_unlock();
Michael Wu3d30d942008-01-31 19:48:27 +01001452 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -07001453}
1454EXPORT_SYMBOL(ieee80211_tx_status);
1455
Jiri Bencf0706e82007-05-05 11:45:53 -07001456struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
1457 const struct ieee80211_ops *ops)
1458{
Jiri Bencf0706e82007-05-05 11:45:53 -07001459 struct ieee80211_local *local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001460 int priv_size;
1461 struct wiphy *wiphy;
1462
1463 /* Ensure 32-byte alignment of our private data and hw private data.
1464 * We use the wiphy priv data for both our ieee80211_local and for
1465 * the driver's private data
1466 *
1467 * In memory it'll be like this:
1468 *
1469 * +-------------------------+
1470 * | struct wiphy |
1471 * +-------------------------+
1472 * | struct ieee80211_local |
1473 * +-------------------------+
1474 * | driver's private data |
1475 * +-------------------------+
1476 *
1477 */
1478 priv_size = ((sizeof(struct ieee80211_local) +
1479 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
1480 priv_data_len;
1481
1482 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
1483
1484 if (!wiphy)
1485 return NULL;
1486
1487 wiphy->privid = mac80211_wiphy_privid;
1488
1489 local = wiphy_priv(wiphy);
1490 local->hw.wiphy = wiphy;
1491
1492 local->hw.priv = (char *)local +
1493 ((sizeof(struct ieee80211_local) +
1494 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
1495
Johannes Berg4480f15c2007-07-10 19:32:10 +02001496 BUG_ON(!ops->tx);
Johannes Berg4150c572007-09-17 01:29:23 -04001497 BUG_ON(!ops->start);
1498 BUG_ON(!ops->stop);
Johannes Berg4480f15c2007-07-10 19:32:10 +02001499 BUG_ON(!ops->config);
1500 BUG_ON(!ops->add_interface);
Johannes Berg4150c572007-09-17 01:29:23 -04001501 BUG_ON(!ops->remove_interface);
1502 BUG_ON(!ops->configure_filter);
Jiri Bencf0706e82007-05-05 11:45:53 -07001503 local->ops = ops;
1504
Jiri Bencf0706e82007-05-05 11:45:53 -07001505 local->hw.queues = 1; /* default */
1506
Jiri Bencf0706e82007-05-05 11:45:53 -07001507 local->bridge_packets = 1;
1508
1509 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1510 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1511 local->short_retry_limit = 7;
1512 local->long_retry_limit = 4;
1513 local->hw.conf.radio_enabled = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07001514
Johannes Berg79010422007-09-18 17:29:21 -04001515 INIT_LIST_HEAD(&local->interfaces);
Jiri Bencf0706e82007-05-05 11:45:53 -07001516
1517 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
Jiri Bencf0706e82007-05-05 11:45:53 -07001518
1519 sta_info_init(local);
1520
Jiri Bencf0706e82007-05-05 11:45:53 -07001521 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1522 (unsigned long)local);
1523 tasklet_disable(&local->tx_pending_tasklet);
1524
1525 tasklet_init(&local->tasklet,
1526 ieee80211_tasklet_handler,
1527 (unsigned long) local);
1528 tasklet_disable(&local->tasklet);
1529
1530 skb_queue_head_init(&local->skb_queue);
1531 skb_queue_head_init(&local->skb_queue_unreliable);
1532
1533 return local_to_hw(local);
1534}
1535EXPORT_SYMBOL(ieee80211_alloc_hw);
1536
1537int ieee80211_register_hw(struct ieee80211_hw *hw)
1538{
1539 struct ieee80211_local *local = hw_to_local(hw);
1540 const char *name;
1541 int result;
Johannes Berg8318d782008-01-24 19:38:38 +01001542 enum ieee80211_band band;
Johannes Berg96d51052008-02-08 09:48:13 +01001543 struct net_device *mdev;
1544 struct ieee80211_sub_if_data *sdata;
Johannes Berg8318d782008-01-24 19:38:38 +01001545
1546 /*
1547 * generic code guarantees at least one band,
1548 * set this very early because much code assumes
1549 * that hw.conf.channel is assigned
1550 */
1551 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1552 struct ieee80211_supported_band *sband;
1553
1554 sband = local->hw.wiphy->bands[band];
1555 if (sband) {
1556 /* init channel we're on */
1557 local->hw.conf.channel =
1558 local->oper_channel =
1559 local->scan_channel = &sband->channels[0];
1560 break;
1561 }
1562 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001563
1564 result = wiphy_register(local->hw.wiphy);
1565 if (result < 0)
1566 return result;
1567
Johannes Berg96d51052008-02-08 09:48:13 +01001568 /* for now, mdev needs sub_if_data :/ */
1569 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
1570 "wmaster%d", ether_setup);
1571 if (!mdev)
1572 goto fail_mdev_alloc;
1573
1574 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
1575 mdev->ieee80211_ptr = &sdata->wdev;
1576 sdata->wdev.wiphy = local->hw.wiphy;
1577
1578 local->mdev = mdev;
1579
1580 ieee80211_rx_bss_list_init(mdev);
1581
1582 mdev->hard_start_xmit = ieee80211_master_start_xmit;
1583 mdev->open = ieee80211_master_open;
1584 mdev->stop = ieee80211_master_stop;
1585 mdev->type = ARPHRD_IEEE80211;
1586 mdev->header_ops = &ieee80211_header_ops;
1587 mdev->set_multicast_list = ieee80211_master_set_multicast_list;
1588
1589 sdata->vif.type = IEEE80211_IF_TYPE_AP;
1590 sdata->dev = mdev;
1591 sdata->local = local;
1592 sdata->u.ap.force_unicast_rateidx = -1;
1593 sdata->u.ap.max_ratectrl_rateidx = -1;
1594 ieee80211_if_sdata_init(sdata);
1595
1596 /* no RCU needed since we're still during init phase */
1597 list_add_tail(&sdata->list, &local->interfaces);
1598
Jiri Bencf0706e82007-05-05 11:45:53 -07001599 name = wiphy_dev(local->hw.wiphy)->driver->name;
1600 local->hw.workqueue = create_singlethread_workqueue(name);
1601 if (!local->hw.workqueue) {
1602 result = -ENOMEM;
1603 goto fail_workqueue;
1604 }
1605
Johannes Bergb306f452007-07-10 19:32:08 +02001606 /*
1607 * The hardware needs headroom for sending the frame,
1608 * and we need some headroom for passing the frame to monitor
1609 * interfaces, but never both at the same time.
1610 */
Jiri Benc33ccad32007-07-18 17:10:44 +02001611 local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1612 sizeof(struct ieee80211_tx_status_rtap_hdr));
Johannes Bergb306f452007-07-10 19:32:08 +02001613
Jiri Bence9f207f2007-05-05 11:46:38 -07001614 debugfs_hw_add(local);
1615
Jiri Bencf0706e82007-05-05 11:45:53 -07001616 local->hw.conf.beacon_int = 1000;
1617
1618 local->wstats_flags |= local->hw.max_rssi ?
1619 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1620 local->wstats_flags |= local->hw.max_signal ?
1621 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1622 local->wstats_flags |= local->hw.max_noise ?
1623 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1624 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1625 local->wstats_flags |= IW_QUAL_DBM;
1626
1627 result = sta_info_start(local);
1628 if (result < 0)
1629 goto fail_sta_info;
1630
1631 rtnl_lock();
1632 result = dev_alloc_name(local->mdev, local->mdev->name);
1633 if (result < 0)
1634 goto fail_dev;
1635
1636 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1637 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1638
1639 result = register_netdevice(local->mdev);
1640 if (result < 0)
1641 goto fail_dev;
1642
Jiri Bence9f207f2007-05-05 11:46:38 -07001643 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Johannes Berg5b2812e2007-09-26 14:27:23 +02001644 ieee80211_if_set_type(local->mdev, IEEE80211_IF_TYPE_AP);
Jiri Bence9f207f2007-05-05 11:46:38 -07001645
Johannes Berg830f9032007-10-28 14:51:05 +01001646 result = ieee80211_init_rate_ctrl_alg(local,
1647 hw->rate_control_algorithm);
Jiri Bencf0706e82007-05-05 11:45:53 -07001648 if (result < 0) {
1649 printk(KERN_DEBUG "%s: Failed to initialize rate control "
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001650 "algorithm\n", wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001651 goto fail_rate;
1652 }
1653
1654 result = ieee80211_wep_init(local);
1655
1656 if (result < 0) {
1657 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001658 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001659 goto fail_wep;
1660 }
1661
1662 ieee80211_install_qdisc(local->mdev);
1663
1664 /* add one default STA interface */
1665 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
Luis Carlos Coboee385852008-02-23 15:17:11 +01001666 IEEE80211_IF_TYPE_STA, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001667 if (result)
1668 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001669 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001670
1671 local->reg_state = IEEE80211_DEV_REGISTERED;
1672 rtnl_unlock();
1673
1674 ieee80211_led_init(local);
1675
1676 return 0;
1677
1678fail_wep:
1679 rate_control_deinitialize(local);
1680fail_rate:
Jiri Bence9f207f2007-05-05 11:46:38 -07001681 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001682 unregister_netdevice(local->mdev);
1683fail_dev:
1684 rtnl_unlock();
1685 sta_info_stop(local);
1686fail_sta_info:
Jiri Bence9f207f2007-05-05 11:46:38 -07001687 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001688 destroy_workqueue(local->hw.workqueue);
1689fail_workqueue:
Johannes Berg96d51052008-02-08 09:48:13 +01001690 ieee80211_if_free(local->mdev);
1691 local->mdev = NULL;
1692fail_mdev_alloc:
Jiri Bencf0706e82007-05-05 11:45:53 -07001693 wiphy_unregister(local->hw.wiphy);
1694 return result;
1695}
1696EXPORT_SYMBOL(ieee80211_register_hw);
1697
Jiri Bencf0706e82007-05-05 11:45:53 -07001698void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1699{
1700 struct ieee80211_local *local = hw_to_local(hw);
1701 struct ieee80211_sub_if_data *sdata, *tmp;
Jiri Bencf0706e82007-05-05 11:45:53 -07001702
1703 tasklet_kill(&local->tx_pending_tasklet);
1704 tasklet_kill(&local->tasklet);
1705
1706 rtnl_lock();
1707
1708 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1709
1710 local->reg_state = IEEE80211_DEV_UNREGISTERED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001711
Johannes Berg79010422007-09-18 17:29:21 -04001712 /*
1713 * At this point, interface list manipulations are fine
1714 * because the driver cannot be handing us frames any
1715 * more and the tasklet is killed.
1716 */
Johannes Berg5b2812e2007-09-26 14:27:23 +02001717
1718 /*
1719 * First, we remove all non-master interfaces. Do this because they
1720 * may have bss pointer dependency on the master, and when we free
1721 * the master these would be freed as well, breaking our list
1722 * iteration completely.
1723 */
1724 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
1725 if (sdata->dev == local->mdev)
1726 continue;
1727 list_del(&sdata->list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001728 __ieee80211_if_del(local, sdata);
Johannes Berg5b2812e2007-09-26 14:27:23 +02001729 }
1730
1731 /* then, finally, remove the master interface */
1732 __ieee80211_if_del(local, IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001733
1734 rtnl_unlock();
1735
Jiri Bencf0706e82007-05-05 11:45:53 -07001736 ieee80211_rx_bss_list_deinit(local->mdev);
1737 ieee80211_clear_tx_pending(local);
1738 sta_info_stop(local);
1739 rate_control_deinitialize(local);
Jiri Bence9f207f2007-05-05 11:46:38 -07001740 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001741
Jiri Bencf0706e82007-05-05 11:45:53 -07001742 if (skb_queue_len(&local->skb_queue)
1743 || skb_queue_len(&local->skb_queue_unreliable))
1744 printk(KERN_WARNING "%s: skb_queue not empty\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001745 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001746 skb_queue_purge(&local->skb_queue);
1747 skb_queue_purge(&local->skb_queue_unreliable);
1748
1749 destroy_workqueue(local->hw.workqueue);
1750 wiphy_unregister(local->hw.wiphy);
1751 ieee80211_wep_free(local);
1752 ieee80211_led_exit(local);
Johannes Berg96d51052008-02-08 09:48:13 +01001753 ieee80211_if_free(local->mdev);
1754 local->mdev = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001755}
1756EXPORT_SYMBOL(ieee80211_unregister_hw);
1757
1758void ieee80211_free_hw(struct ieee80211_hw *hw)
1759{
1760 struct ieee80211_local *local = hw_to_local(hw);
1761
Jiri Bencf0706e82007-05-05 11:45:53 -07001762 wiphy_free(local->hw.wiphy);
1763}
1764EXPORT_SYMBOL(ieee80211_free_hw);
1765
Jiri Bencf0706e82007-05-05 11:45:53 -07001766static int __init ieee80211_init(void)
1767{
1768 struct sk_buff *skb;
1769 int ret;
1770
1771 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1772
Johannes Berg4b475892008-01-02 15:17:03 +01001773 ret = rc80211_simple_init();
Johannes Bergac71c692007-10-28 14:17:44 +01001774 if (ret)
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001775 goto out;
Mattias Nisslerad018372007-12-19 01:25:57 +01001776
Johannes Berg4b475892008-01-02 15:17:03 +01001777 ret = rc80211_pid_init();
Mattias Nisslerad018372007-12-19 01:25:57 +01001778 if (ret)
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001779 goto out_cleanup_simple;
Johannes Bergac71c692007-10-28 14:17:44 +01001780
Jiri Bencf0706e82007-05-05 11:45:53 -07001781 ret = ieee80211_wme_register();
1782 if (ret) {
1783 printk(KERN_DEBUG "ieee80211_init: failed to "
1784 "initialize WME (err=%d)\n", ret);
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001785 goto out_cleanup_pid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001786 }
1787
Jiri Bence9f207f2007-05-05 11:46:38 -07001788 ieee80211_debugfs_netdev_init();
1789
Jiri Bencf0706e82007-05-05 11:45:53 -07001790 return 0;
Mattias Nisslerad018372007-12-19 01:25:57 +01001791
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001792 out_cleanup_pid:
Johannes Berg4b475892008-01-02 15:17:03 +01001793 rc80211_pid_exit();
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001794 out_cleanup_simple:
1795 rc80211_simple_exit();
1796 out:
Mattias Nisslerad018372007-12-19 01:25:57 +01001797 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07001798}
1799
Jiri Bencf0706e82007-05-05 11:45:53 -07001800static void __exit ieee80211_exit(void)
1801{
Johannes Berg4b475892008-01-02 15:17:03 +01001802 rc80211_simple_exit();
1803 rc80211_pid_exit();
Johannes Bergac71c692007-10-28 14:17:44 +01001804
Jiri Bencf0706e82007-05-05 11:45:53 -07001805 ieee80211_wme_unregister();
Jiri Bence9f207f2007-05-05 11:46:38 -07001806 ieee80211_debugfs_netdev_exit();
Jiri Bencf0706e82007-05-05 11:45:53 -07001807}
1808
1809
Johannes Bergca9938f2007-09-11 12:50:32 +02001810subsys_initcall(ieee80211_init);
Jiri Bencf0706e82007-05-05 11:45:53 -07001811module_exit(ieee80211_exit);
1812
1813MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1814MODULE_LICENSE("GPL");