blob: f277407f0f5a848d3484036f5e4a992d84903b20 [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"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040028#include "rate.h"
Luis Carlos Cobof7a92142008-02-23 15:17:18 +010029#include "mesh.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070030#include "wep.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070031#include "wme.h"
32#include "aes_ccm.h"
Johannes Berg2c8dccc2008-04-08 15:14:40 -040033#include "led.h"
Michael Wue0eb6852007-09-18 17:29:21 -040034#include "cfg.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070035#include "debugfs.h"
36#include "debugfs_netdev.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070037
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +020038#define SUPP_MCS_SET_LEN 16
39
Johannes Bergb306f452007-07-10 19:32:08 +020040/*
41 * For seeing transmitted packets on monitor interfaces
42 * we have a radiotap header too.
43 */
44struct ieee80211_tx_status_rtap_hdr {
45 struct ieee80211_radiotap_header hdr;
46 __le16 tx_flags;
47 u8 data_retries;
48} __attribute__ ((packed));
49
Johannes Bergb2c258f2007-07-27 15:43:23 +020050/* common interface routines */
Jiri Bencf0706e82007-05-05 11:45:53 -070051
Stephen Hemmingerb95cce32007-09-26 22:13:38 -070052static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
Jiri Bencf0706e82007-05-05 11:45:53 -070053{
Johannes Bergb2c258f2007-07-27 15:43:23 +020054 memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
55 return ETH_ALEN;
Jiri Bencf0706e82007-05-05 11:45:53 -070056}
57
Johannes Berg4150c572007-09-17 01:29:23 -040058/* must be called under mdev tx lock */
59static void ieee80211_configure_filter(struct ieee80211_local *local)
60{
61 unsigned int changed_flags;
62 unsigned int new_flags = 0;
63
Johannes Berg53918992007-09-26 15:19:47 +020064 if (atomic_read(&local->iff_promiscs))
Johannes Berg4150c572007-09-17 01:29:23 -040065 new_flags |= FIF_PROMISC_IN_BSS;
66
Johannes Berg53918992007-09-26 15:19:47 +020067 if (atomic_read(&local->iff_allmultis))
Johannes Berg4150c572007-09-17 01:29:23 -040068 new_flags |= FIF_ALLMULTI;
69
70 if (local->monitors)
Michael Wu8cc9a732008-01-31 19:48:23 +010071 new_flags |= FIF_BCN_PRBRESP_PROMISC;
72
73 if (local->fif_fcsfail)
74 new_flags |= FIF_FCSFAIL;
75
76 if (local->fif_plcpfail)
77 new_flags |= FIF_PLCPFAIL;
78
79 if (local->fif_control)
80 new_flags |= FIF_CONTROL;
81
82 if (local->fif_other_bss)
83 new_flags |= FIF_OTHER_BSS;
Johannes Berg4150c572007-09-17 01:29:23 -040084
85 changed_flags = local->filter_flags ^ new_flags;
86
87 /* be a bit nasty */
88 new_flags |= (1<<31);
89
90 local->ops->configure_filter(local_to_hw(local),
91 changed_flags, &new_flags,
92 local->mdev->mc_count,
93 local->mdev->mc_list);
94
95 WARN_ON(new_flags & (1<<31));
96
97 local->filter_flags = new_flags & ~(1<<31);
98}
99
Johannes Bergb2c258f2007-07-27 15:43:23 +0200100/* master interface */
Jiri Bencf0706e82007-05-05 11:45:53 -0700101
102static int ieee80211_master_open(struct net_device *dev)
103{
104 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
105 struct ieee80211_sub_if_data *sdata;
106 int res = -EOPNOTSUPP;
107
Johannes Berg79010422007-09-18 17:29:21 -0400108 /* we hold the RTNL here so can safely walk the list */
109 list_for_each_entry(sdata, &local->interfaces, list) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700110 if (sdata->dev != dev && netif_running(sdata->dev)) {
111 res = 0;
112 break;
113 }
114 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700115 return res;
116}
117
118static int ieee80211_master_stop(struct net_device *dev)
119{
120 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
121 struct ieee80211_sub_if_data *sdata;
122
Johannes Berg79010422007-09-18 17:29:21 -0400123 /* we hold the RTNL here so can safely walk the list */
124 list_for_each_entry(sdata, &local->interfaces, list)
Jiri Bencf0706e82007-05-05 11:45:53 -0700125 if (sdata->dev != dev && netif_running(sdata->dev))
126 dev_close(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700127
128 return 0;
129}
130
Johannes Berg4150c572007-09-17 01:29:23 -0400131static void ieee80211_master_set_multicast_list(struct net_device *dev)
132{
133 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
134
135 ieee80211_configure_filter(local);
136}
137
Johannes Bergb2c258f2007-07-27 15:43:23 +0200138/* regular interfaces */
139
140static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
141{
Luis Carlos Cobof7a92142008-02-23 15:17:18 +0100142 int meshhdrlen;
143 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
144
145 meshhdrlen = (sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT) ? 5 : 0;
146
Johannes Bergb2c258f2007-07-27 15:43:23 +0200147 /* FIX: what would be proper limits for MTU?
148 * This interface uses 802.3 frames. */
Luis Carlos Cobof7a92142008-02-23 15:17:18 +0100149 if (new_mtu < 256 ||
150 new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6 - meshhdrlen) {
Johannes Bergb2c258f2007-07-27 15:43:23 +0200151 printk(KERN_WARNING "%s: invalid MTU %d\n",
152 dev->name, new_mtu);
153 return -EINVAL;
154 }
155
156#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
157 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
158#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
159 dev->mtu = new_mtu;
160 return 0;
161}
162
163static inline int identical_mac_addr_allowed(int type1, int type2)
164{
165 return (type1 == IEEE80211_IF_TYPE_MNTR ||
166 type2 == IEEE80211_IF_TYPE_MNTR ||
167 (type1 == IEEE80211_IF_TYPE_AP &&
168 type2 == IEEE80211_IF_TYPE_WDS) ||
169 (type1 == IEEE80211_IF_TYPE_WDS &&
170 (type2 == IEEE80211_IF_TYPE_WDS ||
171 type2 == IEEE80211_IF_TYPE_AP)) ||
172 (type1 == IEEE80211_IF_TYPE_AP &&
173 type2 == IEEE80211_IF_TYPE_VLAN) ||
174 (type1 == IEEE80211_IF_TYPE_VLAN &&
175 (type2 == IEEE80211_IF_TYPE_AP ||
176 type2 == IEEE80211_IF_TYPE_VLAN)));
177}
178
Johannes Bergb2c258f2007-07-27 15:43:23 +0200179static int ieee80211_open(struct net_device *dev)
180{
181 struct ieee80211_sub_if_data *sdata, *nsdata;
182 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
183 struct ieee80211_if_init_conf conf;
184 int res;
Michael Bueschceffefd2008-02-10 14:16:52 +0100185 bool need_hw_reconfig = 0;
Johannes Berg44213b52008-02-25 16:27:49 +0100186 struct sta_info *sta;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200187
188 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400189
Johannes Berg79010422007-09-18 17:29:21 -0400190 /* we hold the RTNL here so can safely walk the list */
191 list_for_each_entry(nsdata, &local->interfaces, list) {
Johannes Bergb2c258f2007-07-27 15:43:23 +0200192 struct net_device *ndev = nsdata->dev;
193
Johannes Berg665e8aa2008-02-21 01:10:07 +0100194 if (ndev != dev && ndev != local->mdev && netif_running(ndev)) {
195 /*
196 * Allow only a single IBSS interface to be up at any
197 * time. This is restricted because beacon distribution
198 * cannot work properly if both are in the same IBSS.
199 *
200 * To remove this restriction we'd have to disallow them
201 * from setting the same SSID on different IBSS interfaces
202 * belonging to the same hardware. Then, however, we're
203 * faced with having to adopt two different TSF timers...
204 */
205 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
206 nsdata->vif.type == IEEE80211_IF_TYPE_IBSS)
207 return -EBUSY;
208
209 /*
210 * Disallow multiple IBSS/STA mode interfaces.
211 *
212 * This is a technical restriction, it is possible although
213 * most likely not IEEE 802.11 compliant to have multiple
214 * STAs with just a single hardware (the TSF timer will not
215 * be adjusted properly.)
216 *
217 * However, because mac80211 uses the master device's BSS
218 * information for each STA/IBSS interface, doing this will
219 * currently corrupt that BSS information completely, unless,
220 * a not very useful case, both STAs are associated to the
221 * same BSS.
222 *
223 * To remove this restriction, the BSS information needs to
224 * be embedded in the STA/IBSS mode sdata instead of using
225 * the master device's BSS structure.
226 */
227 if ((sdata->vif.type == IEEE80211_IF_TYPE_STA ||
228 sdata->vif.type == IEEE80211_IF_TYPE_IBSS) &&
229 (nsdata->vif.type == IEEE80211_IF_TYPE_STA ||
230 nsdata->vif.type == IEEE80211_IF_TYPE_IBSS))
231 return -EBUSY;
232
233 /*
234 * The remaining checks are only performed for interfaces
235 * with the same MAC address.
236 */
237 if (compare_ether_addr(dev->dev_addr, ndev->dev_addr))
238 continue;
239
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400240 /*
241 * check whether it may have the same address
242 */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100243 if (!identical_mac_addr_allowed(sdata->vif.type,
244 nsdata->vif.type))
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400245 return -ENOTUNIQ;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400246
247 /*
248 * can only add VLANs to enabled APs
249 */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100250 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN &&
Johannes Berg665e8aa2008-02-21 01:10:07 +0100251 nsdata->vif.type == IEEE80211_IF_TYPE_AP)
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400252 sdata->u.vlan.ap = nsdata;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200253 }
254 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200255
Johannes Berg51fb61e2007-12-19 01:31:27 +0100256 switch (sdata->vif.type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400257 case IEEE80211_IF_TYPE_WDS:
Johannes Berge94e1062008-04-24 14:16:36 +0200258 if (!is_valid_ether_addr(sdata->u.wds.remote_addr))
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400259 return -ENOLINK;
260 break;
261 case IEEE80211_IF_TYPE_VLAN:
262 if (!sdata->u.vlan.ap)
263 return -ENOLINK;
264 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +0200265 case IEEE80211_IF_TYPE_AP:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +0200266 case IEEE80211_IF_TYPE_STA:
267 case IEEE80211_IF_TYPE_MNTR:
268 case IEEE80211_IF_TYPE_IBSS:
Johannes Berg6032f932008-02-23 15:17:07 +0100269 case IEEE80211_IF_TYPE_MESH_POINT:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +0200270 /* no special treatment */
271 break;
Johannes Berga2897552007-09-28 14:01:25 +0200272 case IEEE80211_IF_TYPE_INVALID:
273 /* cannot happen */
274 WARN_ON(1);
275 break;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400276 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200277
Johannes Bergb2c258f2007-07-27 15:43:23 +0200278 if (local->open_count == 0) {
279 res = 0;
Johannes Berg4150c572007-09-17 01:29:23 -0400280 if (local->ops->start)
281 res = local->ops->start(local_to_hw(local));
282 if (res)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200283 return res;
Michael Bueschceffefd2008-02-10 14:16:52 +0100284 need_hw_reconfig = 1;
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100285 ieee80211_led_radio(local, local->hw.conf.radio_enabled);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200286 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200287
Johannes Berg51fb61e2007-12-19 01:31:27 +0100288 switch (sdata->vif.type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400289 case IEEE80211_IF_TYPE_VLAN:
290 list_add(&sdata->u.vlan.list, &sdata->u.vlan.ap->u.ap.vlans);
291 /* no need to tell driver */
292 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400293 case IEEE80211_IF_TYPE_MNTR:
Michael Wu3d30d942008-01-31 19:48:27 +0100294 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
295 local->cooked_mntrs++;
296 break;
297 }
298
Johannes Berg4150c572007-09-17 01:29:23 -0400299 /* must be before the call to ieee80211_configure_filter */
Johannes Bergb2c258f2007-07-27 15:43:23 +0200300 local->monitors++;
Michael Wu8cc9a732008-01-31 19:48:23 +0100301 if (local->monitors == 1)
Johannes Berg4150c572007-09-17 01:29:23 -0400302 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
Michael Wu8cc9a732008-01-31 19:48:23 +0100303
304 if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
305 local->fif_fcsfail++;
306 if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
307 local->fif_plcpfail++;
308 if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
309 local->fif_control++;
310 if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
311 local->fif_other_bss++;
312
313 netif_tx_lock_bh(local->mdev);
314 ieee80211_configure_filter(local);
315 netif_tx_unlock_bh(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -0400316 break;
317 case IEEE80211_IF_TYPE_STA:
318 case IEEE80211_IF_TYPE_IBSS:
319 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
320 /* fall through */
321 default:
Johannes Berg32bfd352007-12-19 01:31:26 +0100322 conf.vif = &sdata->vif;
Johannes Berg51fb61e2007-12-19 01:31:27 +0100323 conf.type = sdata->vif.type;
Johannes Berg4150c572007-09-17 01:29:23 -0400324 conf.mac_addr = dev->dev_addr;
325 res = local->ops->add_interface(local_to_hw(local), &conf);
Johannes Berg4150c572007-09-17 01:29:23 -0400326 if (res)
Johannes Berg636c5d42008-04-24 14:18:37 +0200327 goto err_stop;
Johannes Berg4150c572007-09-17 01:29:23 -0400328
Johannes Bergb2c258f2007-07-27 15:43:23 +0200329 ieee80211_if_config(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200330 ieee80211_reset_erp_info(dev);
Johannes Berg11a843b2007-08-28 17:01:55 -0400331 ieee80211_enable_keys(sdata);
Johannes Berg4150c572007-09-17 01:29:23 -0400332
Johannes Berg51fb61e2007-12-19 01:31:27 +0100333 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Johannes Bergddd3d2b2007-09-26 17:53:20 +0200334 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
Johannes Berg4150c572007-09-17 01:29:23 -0400335 netif_carrier_off(dev);
336 else
337 netif_carrier_on(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200338 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200339
Johannes Berg636c5d42008-04-24 14:18:37 +0200340 if (sdata->vif.type == IEEE80211_IF_TYPE_WDS) {
341 /* Create STA entry for the WDS peer */
342 sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
343 GFP_KERNEL);
344 if (!sta) {
345 res = -ENOMEM;
346 goto err_del_interface;
347 }
348
Johannes Berg07346f812008-05-03 01:02:02 +0200349 /* no locking required since STA is not live yet */
Johannes Berg636c5d42008-04-24 14:18:37 +0200350 sta->flags |= WLAN_STA_AUTHORIZED;
351
352 res = sta_info_insert(sta);
353 if (res) {
354 /* STA has been freed */
355 goto err_del_interface;
356 }
357 }
358
Johannes Berg4150c572007-09-17 01:29:23 -0400359 if (local->open_count == 0) {
360 res = dev_open(local->mdev);
361 WARN_ON(res);
Johannes Berg636c5d42008-04-24 14:18:37 +0200362 if (res)
363 goto err_del_interface;
Johannes Berg4150c572007-09-17 01:29:23 -0400364 tasklet_enable(&local->tx_pending_tasklet);
365 tasklet_enable(&local->tasklet);
366 }
367
Johannes Bergc1428b32007-11-16 02:54:53 +0100368 /*
369 * set_multicast_list will be invoked by the networking core
370 * which will check whether any increments here were done in
371 * error and sync them down to the hardware as filter flags.
372 */
373 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
374 atomic_inc(&local->iff_allmultis);
375
376 if (sdata->flags & IEEE80211_SDATA_PROMISC)
377 atomic_inc(&local->iff_promiscs);
378
Johannes Berg4150c572007-09-17 01:29:23 -0400379 local->open_count++;
Michael Bueschceffefd2008-02-10 14:16:52 +0100380 if (need_hw_reconfig)
381 ieee80211_hw_config(local);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200382
Jan Niehusmann64f851e2008-03-23 20:23:56 +0100383 /*
384 * ieee80211_sta_work is disabled while network interface
385 * is down. Therefore, some configuration changes may not
386 * yet be effective. Trigger execution of ieee80211_sta_work
387 * to fix this.
388 */
Johannes Berg988c0f72008-04-17 19:21:22 +0200389 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
390 sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Jan Niehusmann64f851e2008-03-23 20:23:56 +0100391 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
392 queue_work(local->hw.workqueue, &ifsta->work);
393 }
394
Johannes Bergb2c258f2007-07-27 15:43:23 +0200395 netif_start_queue(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400396
Johannes Bergb2c258f2007-07-27 15:43:23 +0200397 return 0;
Johannes Berg636c5d42008-04-24 14:18:37 +0200398 err_del_interface:
399 local->ops->remove_interface(local_to_hw(local), &conf);
400 err_stop:
401 if (!local->open_count && local->ops->stop)
402 local->ops->stop(local_to_hw(local));
403 return res;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200404}
405
Johannes Berg4150c572007-09-17 01:29:23 -0400406static int ieee80211_stop(struct net_device *dev)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200407{
Johannes Berg44213b52008-02-25 16:27:49 +0100408 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
409 struct ieee80211_local *local = sdata->local;
Johannes Berg4150c572007-09-17 01:29:23 -0400410 struct ieee80211_if_init_conf conf;
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200411 struct sta_info *sta;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200412
Johannes Berg44213b52008-02-25 16:27:49 +0100413 /*
414 * Stop TX on this interface first.
415 */
416 netif_stop_queue(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400417
Johannes Berg44213b52008-02-25 16:27:49 +0100418 /*
419 * Now delete all active aggregation sessions.
420 */
Johannes Bergd0709a62008-02-25 16:27:46 +0100421 rcu_read_lock();
422
423 list_for_each_entry_rcu(sta, &local->sta_list, list) {
424 if (sta->sdata == sdata)
Ron Rindjunsky85249e52008-03-18 15:00:32 -0700425 ieee80211_sta_tear_down_BA_sessions(dev, sta->addr);
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200426 }
427
Johannes Bergd0709a62008-02-25 16:27:46 +0100428 rcu_read_unlock();
429
Johannes Berg44213b52008-02-25 16:27:49 +0100430 /*
431 * Remove all stations associated with this interface.
432 *
433 * This must be done before calling ops->remove_interface()
434 * because otherwise we can later invoke ops->sta_notify()
435 * whenever the STAs are removed, and that invalidates driver
436 * assumptions about always getting a vif pointer that is valid
437 * (because if we remove a STA after ops->remove_interface()
438 * the driver will have removed the vif info already!)
439 *
440 * We could relax this and only unlink the stations from the
441 * hash table and list but keep them on a per-sdata list that
442 * will be inserted back again when the interface is brought
443 * up again, but I don't currently see a use case for that,
444 * except with WDS which gets a STA entry created when it is
445 * brought up.
446 */
447 sta_info_flush(local, sdata);
Johannes Berg4150c572007-09-17 01:29:23 -0400448
Johannes Bergc1428b32007-11-16 02:54:53 +0100449 /*
450 * Don't count this interface for promisc/allmulti while it
451 * is down. dev_mc_unsync() will invoke set_multicast_list
452 * on the master interface which will sync these down to the
453 * hardware as filter flags.
454 */
455 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
456 atomic_dec(&local->iff_allmultis);
457
458 if (sdata->flags & IEEE80211_SDATA_PROMISC)
459 atomic_dec(&local->iff_promiscs);
460
Johannes Berg4150c572007-09-17 01:29:23 -0400461 dev_mc_unsync(local->mdev, dev);
462
Johannes Berg5dfdaf52007-12-19 02:03:33 +0100463 /* APs need special treatment */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100464 if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400465 struct ieee80211_sub_if_data *vlan, *tmp;
Johannes Berg5dfdaf52007-12-19 02:03:33 +0100466 struct beacon_data *old_beacon = sdata->u.ap.beacon;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400467
Johannes Berg5dfdaf52007-12-19 02:03:33 +0100468 /* remove beacon */
469 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
470 synchronize_rcu();
471 kfree(old_beacon);
472
473 /* down all dependent devices, that is VLANs */
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400474 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
475 u.vlan.list)
476 dev_close(vlan->dev);
477 WARN_ON(!list_empty(&sdata->u.ap.vlans));
478 }
479
Johannes Berg4150c572007-09-17 01:29:23 -0400480 local->open_count--;
481
Johannes Berg51fb61e2007-12-19 01:31:27 +0100482 switch (sdata->vif.type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400483 case IEEE80211_IF_TYPE_VLAN:
484 list_del(&sdata->u.vlan.list);
485 sdata->u.vlan.ap = NULL;
486 /* no need to tell driver */
487 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400488 case IEEE80211_IF_TYPE_MNTR:
Michael Wu3d30d942008-01-31 19:48:27 +0100489 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
490 local->cooked_mntrs--;
491 break;
492 }
493
Johannes Berg4150c572007-09-17 01:29:23 -0400494 local->monitors--;
Michael Wu8cc9a732008-01-31 19:48:23 +0100495 if (local->monitors == 0)
Michael Wu8b393f12007-11-28 01:57:08 -0500496 local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
Michael Wu8cc9a732008-01-31 19:48:23 +0100497
498 if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
499 local->fif_fcsfail--;
500 if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
501 local->fif_plcpfail--;
502 if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
503 local->fif_control--;
504 if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
505 local->fif_other_bss--;
506
507 netif_tx_lock_bh(local->mdev);
508 ieee80211_configure_filter(local);
509 netif_tx_unlock_bh(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -0400510 break;
Luis Carlos Cobof7a92142008-02-23 15:17:18 +0100511 case IEEE80211_IF_TYPE_MESH_POINT:
Johannes Bergb2c258f2007-07-27 15:43:23 +0200512 case IEEE80211_IF_TYPE_STA:
513 case IEEE80211_IF_TYPE_IBSS:
514 sdata->u.sta.state = IEEE80211_DISABLED;
515 del_timer_sync(&sdata->u.sta.timer);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400516 /*
Johannes Berg79010422007-09-18 17:29:21 -0400517 * When we get here, the interface is marked down.
518 * Call synchronize_rcu() to wait for the RX path
519 * should it be using the interface and enqueuing
520 * frames at this very time on another CPU.
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400521 */
Johannes Berg79010422007-09-18 17:29:21 -0400522 synchronize_rcu();
Johannes Bergb2c258f2007-07-27 15:43:23 +0200523 skb_queue_purge(&sdata->u.sta.skb_queue);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400524
Zhu Yiece8edd2007-11-22 10:53:21 +0800525 if (local->scan_dev == sdata->dev) {
526 if (!local->ops->hw_scan) {
527 local->sta_sw_scanning = 0;
528 cancel_delayed_work(&local->scan_work);
529 } else
530 local->sta_hw_scanning = 0;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200531 }
Zhu Yiece8edd2007-11-22 10:53:21 +0800532
Johannes Bergb2c258f2007-07-27 15:43:23 +0200533 flush_workqueue(local->hw.workqueue);
Zhu Yia10605e2007-11-22 11:10:22 +0800534
535 sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
536 kfree(sdata->u.sta.extra_ie);
537 sdata->u.sta.extra_ie = NULL;
538 sdata->u.sta.extra_ie_len = 0;
Johannes Berg4150c572007-09-17 01:29:23 -0400539 /* fall through */
540 default:
Johannes Berg32bfd352007-12-19 01:31:26 +0100541 conf.vif = &sdata->vif;
Johannes Berg51fb61e2007-12-19 01:31:27 +0100542 conf.type = sdata->vif.type;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200543 conf.mac_addr = dev->dev_addr;
Johannes Berg4150c572007-09-17 01:29:23 -0400544 /* disable all keys for as long as this netdev is down */
545 ieee80211_disable_keys(sdata);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200546 local->ops->remove_interface(local_to_hw(local), &conf);
547 }
548
Johannes Berg4150c572007-09-17 01:29:23 -0400549 if (local->open_count == 0) {
550 if (netif_running(local->mdev))
551 dev_close(local->mdev);
552
Johannes Berg4150c572007-09-17 01:29:23 -0400553 if (local->ops->stop)
554 local->ops->stop(local_to_hw(local));
555
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100556 ieee80211_led_radio(local, 0);
557
Johannes Berg4150c572007-09-17 01:29:23 -0400558 tasklet_disable(&local->tx_pending_tasklet);
559 tasklet_disable(&local->tasklet);
560 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200561
562 return 0;
563}
564
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200565int ieee80211_start_tx_ba_session(struct ieee80211_hw *hw, u8 *ra, u16 tid)
566{
567 struct ieee80211_local *local = hw_to_local(hw);
568 struct sta_info *sta;
569 struct ieee80211_sub_if_data *sdata;
570 u16 start_seq_num = 0;
571 u8 *state;
572 int ret;
573 DECLARE_MAC_BUF(mac);
574
575 if (tid >= STA_TID_NUM)
576 return -EINVAL;
577
578#ifdef CONFIG_MAC80211_HT_DEBUG
579 printk(KERN_DEBUG "Open BA session requested for %s tid %u\n",
580 print_mac(mac, ra), tid);
581#endif /* CONFIG_MAC80211_HT_DEBUG */
582
Johannes Bergd0709a62008-02-25 16:27:46 +0100583 rcu_read_lock();
584
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200585 sta = sta_info_get(local, ra);
586 if (!sta) {
587 printk(KERN_DEBUG "Could not find the station\n");
Johannes Bergd0709a62008-02-25 16:27:46 +0100588 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200589 return -ENOENT;
590 }
591
Johannes Berg07346f812008-05-03 01:02:02 +0200592 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200593
594 /* we have tried too many times, receiver does not want A-MPDU */
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200595 if (sta->ampdu_mlme.addba_req_num[tid] > HT_AGG_MAX_RETRIES) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200596 ret = -EBUSY;
597 goto start_ba_exit;
598 }
599
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200600 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200601 /* check if the TID is not in aggregation flow already */
602 if (*state != HT_AGG_STATE_IDLE) {
603#ifdef CONFIG_MAC80211_HT_DEBUG
604 printk(KERN_DEBUG "BA request denied - session is not "
605 "idle on tid %u\n", tid);
606#endif /* CONFIG_MAC80211_HT_DEBUG */
607 ret = -EAGAIN;
608 goto start_ba_exit;
609 }
610
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200611 /* prepare A-MPDU MLME for Tx aggregation */
612 sta->ampdu_mlme.tid_tx[tid] =
613 kmalloc(sizeof(struct tid_ampdu_tx), GFP_ATOMIC);
614 if (!sta->ampdu_mlme.tid_tx[tid]) {
615 if (net_ratelimit())
616 printk(KERN_ERR "allocate tx mlme to tid %d failed\n",
617 tid);
618 ret = -ENOMEM;
619 goto start_ba_exit;
620 }
621 /* Tx timer */
622 sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.function =
623 sta_addba_resp_timer_expired;
624 sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.data =
625 (unsigned long)&sta->timer_to_tid[tid];
626 init_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
627
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200628 /* ensure that TX flow won't interrupt us
629 * until the end of the call to requeue function */
630 spin_lock_bh(&local->mdev->queue_lock);
631
632 /* create a new queue for this aggregation */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200633 ret = ieee80211_ht_agg_queue_add(local, sta, tid);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200634
635 /* case no queue is available to aggregation
636 * don't switch to aggregation */
637 if (ret) {
638#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200639 printk(KERN_DEBUG "BA request denied - queue unavailable for"
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200640 " tid %d\n", tid);
641#endif /* CONFIG_MAC80211_HT_DEBUG */
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200642 goto start_ba_err;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200643 }
Johannes Bergd0709a62008-02-25 16:27:46 +0100644 sdata = sta->sdata;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200645
646 /* Ok, the Addba frame hasn't been sent yet, but if the driver calls the
647 * call back right away, it must see that the flow has begun */
648 *state |= HT_ADDBA_REQUESTED_MSK;
649
650 if (local->ops->ampdu_action)
651 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_START,
652 ra, tid, &start_seq_num);
653
654 if (ret) {
655 /* No need to requeue the packets in the agg queue, since we
656 * held the tx lock: no packet could be enqueued to the newly
657 * allocated queue */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200658 ieee80211_ht_agg_queue_remove(local, sta, tid, 0);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200659#ifdef CONFIG_MAC80211_HT_DEBUG
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200660 printk(KERN_DEBUG "BA request denied - HW unavailable for"
661 " tid %d\n", tid);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200662#endif /* CONFIG_MAC80211_HT_DEBUG */
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200663 *state = HT_AGG_STATE_IDLE;
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200664 goto start_ba_err;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200665 }
666
667 /* Will put all the packets in the new SW queue */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200668 ieee80211_requeue(local, ieee802_1d_to_ac[tid]);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200669 spin_unlock_bh(&local->mdev->queue_lock);
670
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200671 /* send an addBA request */
672 sta->ampdu_mlme.dialog_token_allocator++;
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200673 sta->ampdu_mlme.tid_tx[tid]->dialog_token =
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200674 sta->ampdu_mlme.dialog_token_allocator;
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200675 sta->ampdu_mlme.tid_tx[tid]->ssn = start_seq_num;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200676
Johannes Bergd0709a62008-02-25 16:27:46 +0100677 ieee80211_send_addba_request(sta->sdata->dev, ra, tid,
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200678 sta->ampdu_mlme.tid_tx[tid]->dialog_token,
679 sta->ampdu_mlme.tid_tx[tid]->ssn,
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200680 0x40, 5000);
681
682 /* activate the timer for the recipient's addBA response */
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200683 sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer.expires =
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200684 jiffies + ADDBA_RESP_INTERVAL;
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200685 add_timer(&sta->ampdu_mlme.tid_tx[tid]->addba_resp_timer);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200686 printk(KERN_DEBUG "activated addBA response timer on tid %d\n", tid);
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200687 goto start_ba_exit;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200688
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200689start_ba_err:
690 kfree(sta->ampdu_mlme.tid_tx[tid]);
691 sta->ampdu_mlme.tid_tx[tid] = NULL;
692 spin_unlock_bh(&local->mdev->queue_lock);
693 ret = -EBUSY;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200694start_ba_exit:
Johannes Berg07346f812008-05-03 01:02:02 +0200695 spin_unlock_bh(&sta->lock);
Johannes Bergd0709a62008-02-25 16:27:46 +0100696 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200697 return ret;
698}
699EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
700
701int ieee80211_stop_tx_ba_session(struct ieee80211_hw *hw,
702 u8 *ra, u16 tid,
703 enum ieee80211_back_parties initiator)
704{
705 struct ieee80211_local *local = hw_to_local(hw);
706 struct sta_info *sta;
707 u8 *state;
708 int ret = 0;
709 DECLARE_MAC_BUF(mac);
710
711 if (tid >= STA_TID_NUM)
712 return -EINVAL;
713
Johannes Bergd0709a62008-02-25 16:27:46 +0100714 rcu_read_lock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200715 sta = sta_info_get(local, ra);
Johannes Bergd0709a62008-02-25 16:27:46 +0100716 if (!sta) {
717 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200718 return -ENOENT;
Johannes Bergd0709a62008-02-25 16:27:46 +0100719 }
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200720
721 /* check if the TID is in aggregation */
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200722 state = &sta->ampdu_mlme.tid_state_tx[tid];
Johannes Berg07346f812008-05-03 01:02:02 +0200723 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200724
725 if (*state != HT_AGG_STATE_OPERATIONAL) {
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200726 ret = -ENOENT;
727 goto stop_BA_exit;
728 }
729
Ron Rindjunsky513a1022008-04-07 10:16:56 -0700730#ifdef CONFIG_MAC80211_HT_DEBUG
731 printk(KERN_DEBUG "Tx BA session stop requested for %s tid %u\n",
732 print_mac(mac, ra), tid);
733#endif /* CONFIG_MAC80211_HT_DEBUG */
734
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200735 ieee80211_stop_queue(hw, sta->tid_to_tx_q[tid]);
736
737 *state = HT_AGG_STATE_REQ_STOP_BA_MSK |
738 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
739
740 if (local->ops->ampdu_action)
741 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_STOP,
742 ra, tid, NULL);
743
744 /* case HW denied going back to legacy */
745 if (ret) {
746 WARN_ON(ret != -EBUSY);
747 *state = HT_AGG_STATE_OPERATIONAL;
748 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
749 goto stop_BA_exit;
750 }
751
752stop_BA_exit:
Johannes Berg07346f812008-05-03 01:02:02 +0200753 spin_unlock_bh(&sta->lock);
Johannes Bergd0709a62008-02-25 16:27:46 +0100754 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200755 return ret;
756}
757EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
758
759void ieee80211_start_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u16 tid)
760{
761 struct ieee80211_local *local = hw_to_local(hw);
762 struct sta_info *sta;
763 u8 *state;
764 DECLARE_MAC_BUF(mac);
765
766 if (tid >= STA_TID_NUM) {
767 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
768 tid, STA_TID_NUM);
769 return;
770 }
771
Johannes Bergd0709a62008-02-25 16:27:46 +0100772 rcu_read_lock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200773 sta = sta_info_get(local, ra);
774 if (!sta) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100775 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200776 printk(KERN_DEBUG "Could not find station: %s\n",
777 print_mac(mac, ra));
778 return;
779 }
780
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200781 state = &sta->ampdu_mlme.tid_state_tx[tid];
Johannes Berg07346f812008-05-03 01:02:02 +0200782 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200783
784 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
785 printk(KERN_DEBUG "addBA was not requested yet, state is %d\n",
786 *state);
Johannes Berg07346f812008-05-03 01:02:02 +0200787 spin_unlock_bh(&sta->lock);
Johannes Bergd0709a62008-02-25 16:27:46 +0100788 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200789 return;
790 }
791
792 WARN_ON_ONCE(*state & HT_ADDBA_DRV_READY_MSK);
793
794 *state |= HT_ADDBA_DRV_READY_MSK;
795
796 if (*state == HT_AGG_STATE_OPERATIONAL) {
797 printk(KERN_DEBUG "Aggregation is on for tid %d \n", tid);
798 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
799 }
Johannes Berg07346f812008-05-03 01:02:02 +0200800 spin_unlock_bh(&sta->lock);
Johannes Bergd0709a62008-02-25 16:27:46 +0100801 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200802}
803EXPORT_SYMBOL(ieee80211_start_tx_ba_cb);
804
805void ieee80211_stop_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u8 tid)
806{
807 struct ieee80211_local *local = hw_to_local(hw);
808 struct sta_info *sta;
809 u8 *state;
810 int agg_queue;
811 DECLARE_MAC_BUF(mac);
812
813 if (tid >= STA_TID_NUM) {
814 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
815 tid, STA_TID_NUM);
816 return;
817 }
818
Ron Rindjunsky513a1022008-04-07 10:16:56 -0700819#ifdef CONFIG_MAC80211_HT_DEBUG
820 printk(KERN_DEBUG "Stopping Tx BA session for %s tid %d\n",
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200821 print_mac(mac, ra), tid);
Ron Rindjunsky513a1022008-04-07 10:16:56 -0700822#endif /* CONFIG_MAC80211_HT_DEBUG */
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200823
Johannes Bergd0709a62008-02-25 16:27:46 +0100824 rcu_read_lock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200825 sta = sta_info_get(local, ra);
826 if (!sta) {
827 printk(KERN_DEBUG "Could not find station: %s\n",
828 print_mac(mac, ra));
Johannes Bergd0709a62008-02-25 16:27:46 +0100829 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200830 return;
831 }
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200832 state = &sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200833
Johannes Berg07346f812008-05-03 01:02:02 +0200834 spin_lock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200835 if ((*state & HT_AGG_STATE_REQ_STOP_BA_MSK) == 0) {
836 printk(KERN_DEBUG "unexpected callback to A-MPDU stop\n");
Johannes Berg07346f812008-05-03 01:02:02 +0200837 spin_unlock_bh(&sta->lock);
Johannes Bergd0709a62008-02-25 16:27:46 +0100838 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200839 return;
840 }
841
842 if (*state & HT_AGG_STATE_INITIATOR_MSK)
Johannes Bergd0709a62008-02-25 16:27:46 +0100843 ieee80211_send_delba(sta->sdata->dev, ra, tid,
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200844 WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
845
846 agg_queue = sta->tid_to_tx_q[tid];
847
848 /* avoid ordering issues: we are the only one that can modify
849 * the content of the qdiscs */
850 spin_lock_bh(&local->mdev->queue_lock);
851 /* remove the queue for this aggregation */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200852 ieee80211_ht_agg_queue_remove(local, sta, tid, 1);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200853 spin_unlock_bh(&local->mdev->queue_lock);
854
855 /* we just requeued the all the frames that were in the removed
856 * queue, and since we might miss a softirq we do netif_schedule.
857 * ieee80211_wake_queue is not used here as this queue is not
858 * necessarily stopped */
859 netif_schedule(local->mdev);
860 *state = HT_AGG_STATE_IDLE;
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200861 sta->ampdu_mlme.addba_req_num[tid] = 0;
862 kfree(sta->ampdu_mlme.tid_tx[tid]);
863 sta->ampdu_mlme.tid_tx[tid] = NULL;
Johannes Berg07346f812008-05-03 01:02:02 +0200864 spin_unlock_bh(&sta->lock);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200865
Johannes Bergd0709a62008-02-25 16:27:46 +0100866 rcu_read_unlock();
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200867}
868EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb);
869
870void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
871 const u8 *ra, u16 tid)
872{
873 struct ieee80211_local *local = hw_to_local(hw);
874 struct ieee80211_ra_tid *ra_tid;
875 struct sk_buff *skb = dev_alloc_skb(0);
876
877 if (unlikely(!skb)) {
878 if (net_ratelimit())
879 printk(KERN_WARNING "%s: Not enough memory, "
880 "dropping start BA session", skb->dev->name);
881 return;
882 }
883 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
884 memcpy(&ra_tid->ra, ra, ETH_ALEN);
885 ra_tid->tid = tid;
886
887 skb->pkt_type = IEEE80211_ADDBA_MSG;
888 skb_queue_tail(&local->skb_queue, skb);
889 tasklet_schedule(&local->tasklet);
890}
891EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
892
893void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
894 const u8 *ra, u16 tid)
895{
896 struct ieee80211_local *local = hw_to_local(hw);
897 struct ieee80211_ra_tid *ra_tid;
898 struct sk_buff *skb = dev_alloc_skb(0);
899
900 if (unlikely(!skb)) {
901 if (net_ratelimit())
902 printk(KERN_WARNING "%s: Not enough memory, "
903 "dropping stop BA session", skb->dev->name);
904 return;
905 }
906 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
907 memcpy(&ra_tid->ra, ra, ETH_ALEN);
908 ra_tid->tid = tid;
909
910 skb->pkt_type = IEEE80211_DELBA_MSG;
911 skb_queue_tail(&local->skb_queue, skb);
912 tasklet_schedule(&local->tasklet);
913}
914EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
915
Johannes Bergb2c258f2007-07-27 15:43:23 +0200916static void ieee80211_set_multicast_list(struct net_device *dev)
917{
918 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
919 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400920 int allmulti, promisc, sdata_allmulti, sdata_promisc;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200921
Johannes Berg4150c572007-09-17 01:29:23 -0400922 allmulti = !!(dev->flags & IFF_ALLMULTI);
923 promisc = !!(dev->flags & IFF_PROMISC);
Johannes Bergb52f2192007-11-16 01:49:11 +0100924 sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
925 sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
Johannes Berg4150c572007-09-17 01:29:23 -0400926
927 if (allmulti != sdata_allmulti) {
928 if (dev->flags & IFF_ALLMULTI)
Johannes Berg53918992007-09-26 15:19:47 +0200929 atomic_inc(&local->iff_allmultis);
Johannes Berg4150c572007-09-17 01:29:23 -0400930 else
Johannes Berg53918992007-09-26 15:19:47 +0200931 atomic_dec(&local->iff_allmultis);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400932 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200933 }
Johannes Berg4150c572007-09-17 01:29:23 -0400934
935 if (promisc != sdata_promisc) {
936 if (dev->flags & IFF_PROMISC)
Johannes Berg53918992007-09-26 15:19:47 +0200937 atomic_inc(&local->iff_promiscs);
Johannes Berg4150c572007-09-17 01:29:23 -0400938 else
Johannes Berg53918992007-09-26 15:19:47 +0200939 atomic_dec(&local->iff_promiscs);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400940 sdata->flags ^= IEEE80211_SDATA_PROMISC;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200941 }
Johannes Berg4150c572007-09-17 01:29:23 -0400942
943 dev_mc_sync(local->mdev, dev);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200944}
945
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700946static const struct header_ops ieee80211_header_ops = {
947 .create = eth_header,
948 .parse = header_parse_80211,
949 .rebuild = eth_rebuild_header,
950 .cache = eth_header_cache,
951 .cache_update = eth_header_cache_update,
952};
953
Johannes Bergf9d540e2007-09-28 14:02:09 +0200954/* Must not be called for mdev */
Johannes Bergb2c258f2007-07-27 15:43:23 +0200955void ieee80211_if_setup(struct net_device *dev)
956{
957 ether_setup(dev);
958 dev->hard_start_xmit = ieee80211_subif_start_xmit;
959 dev->wireless_handlers = &ieee80211_iw_handler_def;
960 dev->set_multicast_list = ieee80211_set_multicast_list;
961 dev->change_mtu = ieee80211_change_mtu;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200962 dev->open = ieee80211_open;
963 dev->stop = ieee80211_stop;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200964 dev->destructor = ieee80211_if_free;
965}
966
Johannes Bergb2c258f2007-07-27 15:43:23 +0200967/* everything else */
968
Johannes Bergb2c258f2007-07-27 15:43:23 +0200969static int __ieee80211_if_config(struct net_device *dev,
970 struct sk_buff *beacon,
971 struct ieee80211_tx_control *control)
972{
973 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
974 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
975 struct ieee80211_if_conf conf;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200976
977 if (!local->ops->config_interface || !netif_running(dev))
978 return 0;
979
980 memset(&conf, 0, sizeof(conf));
Johannes Berg51fb61e2007-12-19 01:31:27 +0100981 conf.type = sdata->vif.type;
982 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
983 sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Johannes Berg4150c572007-09-17 01:29:23 -0400984 conf.bssid = sdata->u.sta.bssid;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200985 conf.ssid = sdata->u.sta.ssid;
986 conf.ssid_len = sdata->u.sta.ssid_len;
Johannes Berg902acc72008-02-23 15:17:19 +0100987 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
Luis Carlos Cobof7a92142008-02-23 15:17:18 +0100988 conf.beacon = beacon;
Johannes Berg8b808bf2008-04-23 23:35:09 +0200989 conf.beacon_control = control;
Luis Carlos Cobof7a92142008-02-23 15:17:18 +0100990 ieee80211_start_mesh(dev);
Johannes Berg51fb61e2007-12-19 01:31:27 +0100991 } else if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
Johannes Bergb2c258f2007-07-27 15:43:23 +0200992 conf.ssid = sdata->u.ap.ssid;
993 conf.ssid_len = sdata->u.ap.ssid_len;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200994 conf.beacon = beacon;
995 conf.beacon_control = control;
996 }
997 return local->ops->config_interface(local_to_hw(local),
Johannes Berg32bfd352007-12-19 01:31:26 +0100998 &sdata->vif, &conf);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200999}
1000
1001int ieee80211_if_config(struct net_device *dev)
1002{
Luis Carlos Cobof7a92142008-02-23 15:17:18 +01001003 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1004 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1005 if (sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT &&
1006 (local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
1007 return ieee80211_if_config_beacon(dev);
Johannes Bergb2c258f2007-07-27 15:43:23 +02001008 return __ieee80211_if_config(dev, NULL, NULL);
1009}
1010
1011int ieee80211_if_config_beacon(struct net_device *dev)
1012{
1013 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1014 struct ieee80211_tx_control control;
Johannes Berg32bfd352007-12-19 01:31:26 +01001015 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Bergb2c258f2007-07-27 15:43:23 +02001016 struct sk_buff *skb;
1017
1018 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
1019 return 0;
Johannes Berg32bfd352007-12-19 01:31:26 +01001020 skb = ieee80211_beacon_get(local_to_hw(local), &sdata->vif,
1021 &control);
Johannes Bergb2c258f2007-07-27 15:43:23 +02001022 if (!skb)
1023 return -ENOMEM;
1024 return __ieee80211_if_config(dev, skb, &control);
1025}
1026
1027int ieee80211_hw_config(struct ieee80211_local *local)
1028{
Johannes Bergb2c258f2007-07-27 15:43:23 +02001029 struct ieee80211_channel *chan;
1030 int ret = 0;
1031
Johannes Berg8318d782008-01-24 19:38:38 +01001032 if (local->sta_sw_scanning)
Johannes Bergb2c258f2007-07-27 15:43:23 +02001033 chan = local->scan_channel;
Johannes Berg8318d782008-01-24 19:38:38 +01001034 else
Johannes Bergb2c258f2007-07-27 15:43:23 +02001035 chan = local->oper_channel;
Johannes Bergb2c258f2007-07-27 15:43:23 +02001036
Johannes Berg8318d782008-01-24 19:38:38 +01001037 local->hw.conf.channel = chan;
1038
1039 if (!local->hw.conf.power_level)
1040 local->hw.conf.power_level = chan->max_power;
1041 else
1042 local->hw.conf.power_level = min(chan->max_power,
1043 local->hw.conf.power_level);
1044
1045 local->hw.conf.max_antenna_gain = chan->max_antenna_gain;
Johannes Bergb2c258f2007-07-27 15:43:23 +02001046
1047#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg8318d782008-01-24 19:38:38 +01001048 printk(KERN_DEBUG "%s: HW CONFIG: freq=%d\n",
1049 wiphy_name(local->hw.wiphy), chan->center_freq);
1050#endif
Johannes Bergb2c258f2007-07-27 15:43:23 +02001051
Michael Bueschf7c4dae2007-09-24 18:41:49 +02001052 if (local->open_count)
Johannes Bergb2c258f2007-07-27 15:43:23 +02001053 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
1054
1055 return ret;
1056}
1057
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001058/**
Tomas Winkler38668c02008-03-28 16:33:32 -07001059 * ieee80211_handle_ht should be used only after legacy configuration
1060 * has been determined namely band, as ht configuration depends upon
1061 * the hardware's HT abilities for a _specific_ band.
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001062 */
Tomas Winkler38668c02008-03-28 16:33:32 -07001063u32 ieee80211_handle_ht(struct ieee80211_local *local, int enable_ht,
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001064 struct ieee80211_ht_info *req_ht_cap,
1065 struct ieee80211_ht_bss_info *req_bss_cap)
1066{
1067 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg8318d782008-01-24 19:38:38 +01001068 struct ieee80211_supported_band *sband;
Tomas Winkler38668c02008-03-28 16:33:32 -07001069 struct ieee80211_ht_info ht_conf;
1070 struct ieee80211_ht_bss_info ht_bss_conf;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001071 int i;
Tomas Winkler38668c02008-03-28 16:33:32 -07001072 u32 changed = 0;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001073
Johannes Berg8318d782008-01-24 19:38:38 +01001074 sband = local->hw.wiphy->bands[conf->channel->band];
1075
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001076 /* HT is not supported */
Johannes Berg8318d782008-01-24 19:38:38 +01001077 if (!sband->ht_info.ht_supported) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001078 conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
Tomas Winkler38668c02008-03-28 16:33:32 -07001079 return 0;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001080 }
1081
Tomas Winkler38668c02008-03-28 16:33:32 -07001082 memset(&ht_conf, 0, sizeof(struct ieee80211_ht_info));
1083 memset(&ht_bss_conf, 0, sizeof(struct ieee80211_ht_bss_info));
1084
1085 if (enable_ht) {
1086 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE))
1087 changed |= BSS_CHANGED_HT;
1088
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001089 conf->flags |= IEEE80211_CONF_SUPPORT_HT_MODE;
Tomas Winkler38668c02008-03-28 16:33:32 -07001090 ht_conf.ht_supported = 1;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001091
Tomas Winkler38668c02008-03-28 16:33:32 -07001092 ht_conf.cap = req_ht_cap->cap & sband->ht_info.cap;
1093 ht_conf.cap &= ~(IEEE80211_HT_CAP_MIMO_PS);
1094 ht_conf.cap |= sband->ht_info.cap & IEEE80211_HT_CAP_MIMO_PS;
1095
1096 for (i = 0; i < SUPP_MCS_SET_LEN; i++)
1097 ht_conf.supp_mcs_set[i] =
1098 sband->ht_info.supp_mcs_set[i] &
1099 req_ht_cap->supp_mcs_set[i];
1100
1101 ht_bss_conf.primary_channel = req_bss_cap->primary_channel;
1102 ht_bss_conf.bss_cap = req_bss_cap->bss_cap;
1103 ht_bss_conf.bss_op_mode = req_bss_cap->bss_op_mode;
1104
1105 ht_conf.ampdu_factor = req_ht_cap->ampdu_factor;
1106 ht_conf.ampdu_density = req_ht_cap->ampdu_density;
1107
1108 /* if bss configuration changed store the new one */
1109 if (memcmp(&conf->ht_conf, &ht_conf, sizeof(ht_conf)) ||
1110 memcmp(&conf->ht_bss_conf, &ht_bss_conf, sizeof(ht_bss_conf))) {
1111 changed |= BSS_CHANGED_HT;
1112 memcpy(&conf->ht_conf, &ht_conf, sizeof(ht_conf));
1113 memcpy(&conf->ht_bss_conf, &ht_bss_conf, sizeof(ht_bss_conf));
1114 }
1115 } else {
1116 if (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)
1117 changed |= BSS_CHANGED_HT;
1118 conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001119 }
1120
Tomas Winkler38668c02008-03-28 16:33:32 -07001121 return changed;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001122}
1123
Johannes Berg471b3ef2007-12-28 14:32:58 +01001124void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
1125 u32 changed)
Daniel Draked9430a32007-07-27 15:43:24 +02001126{
Johannes Berg471b3ef2007-12-28 14:32:58 +01001127 struct ieee80211_local *local = sdata->local;
1128
1129 if (!changed)
1130 return;
1131
1132 if (local->ops->bss_info_changed)
1133 local->ops->bss_info_changed(local_to_hw(local),
1134 &sdata->vif,
1135 &sdata->bss_conf,
1136 changed);
Daniel Draked9430a32007-07-27 15:43:24 +02001137}
1138
1139void ieee80211_reset_erp_info(struct net_device *dev)
1140{
1141 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1142
Johannes Berg471b3ef2007-12-28 14:32:58 +01001143 sdata->bss_conf.use_cts_prot = 0;
1144 sdata->bss_conf.use_short_preamble = 0;
1145 ieee80211_bss_info_change_notify(sdata,
1146 BSS_CHANGED_ERP_CTS_PROT |
1147 BSS_CHANGED_ERP_PREAMBLE);
Daniel Draked9430a32007-07-27 15:43:24 +02001148}
1149
Jiri Bencf0706e82007-05-05 11:45:53 -07001150void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
1151 struct sk_buff *skb,
1152 struct ieee80211_tx_status *status)
1153{
1154 struct ieee80211_local *local = hw_to_local(hw);
1155 struct ieee80211_tx_status *saved;
1156 int tmp;
1157
1158 skb->dev = local->mdev;
1159 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
1160 if (unlikely(!saved)) {
1161 if (net_ratelimit())
1162 printk(KERN_WARNING "%s: Not enough memory, "
1163 "dropping tx status", skb->dev->name);
1164 /* should be dev_kfree_skb_irq, but due to this function being
1165 * named _irqsafe instead of just _irq we can't be sure that
1166 * people won't call it from non-irq contexts */
1167 dev_kfree_skb_any(skb);
1168 return;
1169 }
1170 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
1171 /* copy pointer to saved status into skb->cb for use by tasklet */
1172 memcpy(skb->cb, &saved, sizeof(saved));
1173
1174 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
1175 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
1176 &local->skb_queue : &local->skb_queue_unreliable, skb);
1177 tmp = skb_queue_len(&local->skb_queue) +
1178 skb_queue_len(&local->skb_queue_unreliable);
1179 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
1180 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
1181 memcpy(&saved, skb->cb, sizeof(saved));
1182 kfree(saved);
1183 dev_kfree_skb_irq(skb);
1184 tmp--;
1185 I802_DEBUG_INC(local->tx_status_drop);
1186 }
1187 tasklet_schedule(&local->tasklet);
1188}
1189EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
1190
1191static void ieee80211_tasklet_handler(unsigned long data)
1192{
1193 struct ieee80211_local *local = (struct ieee80211_local *) data;
1194 struct sk_buff *skb;
1195 struct ieee80211_rx_status rx_status;
1196 struct ieee80211_tx_status *tx_status;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001197 struct ieee80211_ra_tid *ra_tid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001198
1199 while ((skb = skb_dequeue(&local->skb_queue)) ||
1200 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
1201 switch (skb->pkt_type) {
1202 case IEEE80211_RX_MSG:
1203 /* status is in skb->cb */
1204 memcpy(&rx_status, skb->cb, sizeof(rx_status));
Johannes Berg51fb61e2007-12-19 01:31:27 +01001205 /* Clear skb->pkt_type in order to not confuse kernel
Jiri Bencf0706e82007-05-05 11:45:53 -07001206 * netstack. */
1207 skb->pkt_type = 0;
1208 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
1209 break;
1210 case IEEE80211_TX_STATUS_MSG:
1211 /* get pointer to saved status out of skb->cb */
1212 memcpy(&tx_status, skb->cb, sizeof(tx_status));
1213 skb->pkt_type = 0;
1214 ieee80211_tx_status(local_to_hw(local),
1215 skb, tx_status);
1216 kfree(tx_status);
1217 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001218 case IEEE80211_DELBA_MSG:
1219 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
1220 ieee80211_stop_tx_ba_cb(local_to_hw(local),
1221 ra_tid->ra, ra_tid->tid);
1222 dev_kfree_skb(skb);
1223 break;
1224 case IEEE80211_ADDBA_MSG:
1225 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
1226 ieee80211_start_tx_ba_cb(local_to_hw(local),
1227 ra_tid->ra, ra_tid->tid);
1228 dev_kfree_skb(skb);
1229 break ;
Jiri Bencf0706e82007-05-05 11:45:53 -07001230 default: /* should never get here! */
1231 printk(KERN_ERR "%s: Unknown message type (%d)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001232 wiphy_name(local->hw.wiphy), skb->pkt_type);
Jiri Bencf0706e82007-05-05 11:45:53 -07001233 dev_kfree_skb(skb);
1234 break;
1235 }
1236 }
1237}
1238
Jiri Bencf0706e82007-05-05 11:45:53 -07001239/* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
1240 * make a prepared TX frame (one that has been given to hw) to look like brand
1241 * new IEEE 802.11 frame that is ready to go through TX processing again.
1242 * Also, tx_packet_data in cb is restored from tx_control. */
1243static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
1244 struct ieee80211_key *key,
1245 struct sk_buff *skb,
1246 struct ieee80211_tx_control *control)
1247{
1248 int hdrlen, iv_len, mic_len;
1249 struct ieee80211_tx_packet_data *pkt_data;
1250
1251 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
Johannes Berg32bfd352007-12-19 01:31:26 +01001252 pkt_data->ifindex = vif_to_sdata(control->vif)->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001253 pkt_data->flags = 0;
1254 if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
1255 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
1256 if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
1257 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
1258 if (control->flags & IEEE80211_TXCTL_REQUEUE)
1259 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg678f5f712007-12-19 01:31:23 +01001260 if (control->flags & IEEE80211_TXCTL_EAPOL_FRAME)
1261 pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
Jiri Bencf0706e82007-05-05 11:45:53 -07001262 pkt_data->queue = control->queue;
1263
1264 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1265
1266 if (!key)
1267 goto no_key;
1268
Johannes Berg8f20fc22007-08-28 17:01:54 -04001269 switch (key->conf.alg) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001270 case ALG_WEP:
1271 iv_len = WEP_IV_LEN;
1272 mic_len = WEP_ICV_LEN;
1273 break;
1274 case ALG_TKIP:
1275 iv_len = TKIP_IV_LEN;
1276 mic_len = TKIP_ICV_LEN;
1277 break;
1278 case ALG_CCMP:
1279 iv_len = CCMP_HDR_LEN;
1280 mic_len = CCMP_MIC_LEN;
1281 break;
1282 default:
1283 goto no_key;
1284 }
1285
Johannes Berg8f20fc22007-08-28 17:01:54 -04001286 if (skb->len >= mic_len &&
Johannes Berg11a843b2007-08-28 17:01:55 -04001287 !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Jiri Bencf0706e82007-05-05 11:45:53 -07001288 skb_trim(skb, skb->len - mic_len);
1289 if (skb->len >= iv_len && skb->len > hdrlen) {
1290 memmove(skb->data + iv_len, skb->data, hdrlen);
1291 skb_pull(skb, iv_len);
1292 }
1293
1294no_key:
1295 {
1296 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1297 u16 fc = le16_to_cpu(hdr->frame_control);
1298 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
1299 fc &= ~IEEE80211_STYPE_QOS_DATA;
1300 hdr->frame_control = cpu_to_le16(fc);
1301 memmove(skb->data + 2, skb->data, hdrlen - 2);
1302 skb_pull(skb, 2);
1303 }
1304 }
1305}
1306
Johannes Bergd46e1442008-02-20 23:59:33 +01001307static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
1308 struct sta_info *sta,
1309 struct sk_buff *skb,
1310 struct ieee80211_tx_status *status)
1311{
1312 sta->tx_filtered_count++;
1313
1314 /*
1315 * Clear the TX filter mask for this STA when sending the next
1316 * packet. If the STA went to power save mode, this will happen
1317 * happen when it wakes up for the next time.
1318 */
Johannes Berg07346f812008-05-03 01:02:02 +02001319 set_sta_flags(sta, WLAN_STA_CLEAR_PS_FILT);
Johannes Bergd46e1442008-02-20 23:59:33 +01001320
1321 /*
1322 * This code races in the following way:
1323 *
1324 * (1) STA sends frame indicating it will go to sleep and does so
1325 * (2) hardware/firmware adds STA to filter list, passes frame up
1326 * (3) hardware/firmware processes TX fifo and suppresses a frame
1327 * (4) we get TX status before having processed the frame and
1328 * knowing that the STA has gone to sleep.
1329 *
1330 * This is actually quite unlikely even when both those events are
1331 * processed from interrupts coming in quickly after one another or
1332 * even at the same time because we queue both TX status events and
1333 * RX frames to be processed by a tasklet and process them in the
1334 * same order that they were received or TX status last. Hence, there
1335 * is no race as long as the frame RX is processed before the next TX
1336 * status, which drivers can ensure, see below.
1337 *
1338 * Note that this can only happen if the hardware or firmware can
1339 * actually add STAs to the filter list, if this is done by the
1340 * driver in response to set_tim() (which will only reduce the race
1341 * this whole filtering tries to solve, not completely solve it)
1342 * this situation cannot happen.
1343 *
1344 * To completely solve this race drivers need to make sure that they
1345 * (a) don't mix the irq-safe/not irq-safe TX status/RX processing
1346 * functions and
1347 * (b) always process RX events before TX status events if ordering
1348 * can be unknown, for example with different interrupt status
1349 * bits.
1350 */
Johannes Berg07346f812008-05-03 01:02:02 +02001351 if (test_sta_flags(sta, WLAN_STA_PS) &&
Johannes Bergd46e1442008-02-20 23:59:33 +01001352 skb_queue_len(&sta->tx_filtered) < STA_MAX_TX_BUFFER) {
1353 ieee80211_remove_tx_extra(local, sta->key, skb,
1354 &status->control);
1355 skb_queue_tail(&sta->tx_filtered, skb);
1356 return;
1357 }
1358
Johannes Berg07346f812008-05-03 01:02:02 +02001359 if (!test_sta_flags(sta, WLAN_STA_PS) &&
Johannes Bergd46e1442008-02-20 23:59:33 +01001360 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
1361 /* Software retry the packet once */
1362 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
1363 ieee80211_remove_tx_extra(local, sta->key, skb,
1364 &status->control);
1365 dev_queue_xmit(skb);
1366 return;
1367 }
1368
1369 if (net_ratelimit())
1370 printk(KERN_DEBUG "%s: dropped TX filtered frame, "
1371 "queue_len=%d PS=%d @%lu\n",
1372 wiphy_name(local->hw.wiphy),
1373 skb_queue_len(&sta->tx_filtered),
Johannes Berg07346f812008-05-03 01:02:02 +02001374 !!test_sta_flags(sta, WLAN_STA_PS), jiffies);
Johannes Bergd46e1442008-02-20 23:59:33 +01001375 dev_kfree_skb(skb);
1376}
1377
Jiri Bencf0706e82007-05-05 11:45:53 -07001378void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
1379 struct ieee80211_tx_status *status)
1380{
1381 struct sk_buff *skb2;
1382 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1383 struct ieee80211_local *local = hw_to_local(hw);
1384 u16 frag, type;
Johannes Bergb306f452007-07-10 19:32:08 +02001385 struct ieee80211_tx_status_rtap_hdr *rthdr;
1386 struct ieee80211_sub_if_data *sdata;
Michael Wu3d30d942008-01-31 19:48:27 +01001387 struct net_device *prev_dev = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001388
1389 if (!status) {
1390 printk(KERN_ERR
1391 "%s: ieee80211_tx_status called with NULL status\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001392 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001393 dev_kfree_skb(skb);
1394 return;
1395 }
1396
Johannes Bergd0709a62008-02-25 16:27:46 +01001397 rcu_read_lock();
1398
Jiri Bencf0706e82007-05-05 11:45:53 -07001399 if (status->excessive_retries) {
1400 struct sta_info *sta;
1401 sta = sta_info_get(local, hdr->addr1);
1402 if (sta) {
Johannes Berg07346f812008-05-03 01:02:02 +02001403 if (test_sta_flags(sta, WLAN_STA_PS)) {
Johannes Bergd46e1442008-02-20 23:59:33 +01001404 /*
1405 * The STA is in power save mode, so assume
Jiri Bencf0706e82007-05-05 11:45:53 -07001406 * that this TX packet failed because of that.
1407 */
1408 status->excessive_retries = 0;
1409 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
Johannes Bergd46e1442008-02-20 23:59:33 +01001410 ieee80211_handle_filtered_frame(local, sta,
1411 skb, status);
Johannes Bergd0709a62008-02-25 16:27:46 +01001412 rcu_read_unlock();
Johannes Bergd46e1442008-02-20 23:59:33 +01001413 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001414 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001415 }
1416 }
1417
1418 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
1419 struct sta_info *sta;
1420 sta = sta_info_get(local, hdr->addr1);
1421 if (sta) {
Johannes Bergd46e1442008-02-20 23:59:33 +01001422 ieee80211_handle_filtered_frame(local, sta, skb,
1423 status);
Johannes Bergd0709a62008-02-25 16:27:46 +01001424 rcu_read_unlock();
Jiri Bencf0706e82007-05-05 11:45:53 -07001425 return;
1426 }
Mattias Nissler1abbe492007-12-20 13:50:07 +01001427 } else
1428 rate_control_tx_status(local->mdev, skb, status);
Jiri Bencf0706e82007-05-05 11:45:53 -07001429
Johannes Bergd0709a62008-02-25 16:27:46 +01001430 rcu_read_unlock();
1431
Jiri Bencf0706e82007-05-05 11:45:53 -07001432 ieee80211_led_tx(local, 0);
1433
1434 /* SNMP counters
1435 * Fragments are passed to low-level drivers as separate skbs, so these
1436 * are actually fragments, not frames. Update frame counters only for
1437 * the first fragment of the frame. */
1438
1439 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
1440 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
1441
1442 if (status->flags & IEEE80211_TX_STATUS_ACK) {
1443 if (frag == 0) {
1444 local->dot11TransmittedFrameCount++;
1445 if (is_multicast_ether_addr(hdr->addr1))
1446 local->dot11MulticastTransmittedFrameCount++;
1447 if (status->retry_count > 0)
1448 local->dot11RetryCount++;
1449 if (status->retry_count > 1)
1450 local->dot11MultipleRetryCount++;
1451 }
1452
1453 /* This counter shall be incremented for an acknowledged MPDU
1454 * with an individual address in the address 1 field or an MPDU
1455 * with a multicast address in the address 1 field of type Data
1456 * or Management. */
1457 if (!is_multicast_ether_addr(hdr->addr1) ||
1458 type == IEEE80211_FTYPE_DATA ||
1459 type == IEEE80211_FTYPE_MGMT)
1460 local->dot11TransmittedFragmentCount++;
1461 } else {
1462 if (frag == 0)
1463 local->dot11FailedCount++;
1464 }
1465
Johannes Bergb306f452007-07-10 19:32:08 +02001466 /* this was a transmitted frame, but now we want to reuse it */
1467 skb_orphan(skb);
1468
Michael Wu3d30d942008-01-31 19:48:27 +01001469 /*
1470 * This is a bit racy but we can avoid a lot of work
1471 * with this test...
1472 */
1473 if (!local->monitors && !local->cooked_mntrs) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001474 dev_kfree_skb(skb);
1475 return;
1476 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001477
Johannes Bergb306f452007-07-10 19:32:08 +02001478 /* send frame to monitor interfaces now */
1479
1480 if (skb_headroom(skb) < sizeof(*rthdr)) {
1481 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
1482 dev_kfree_skb(skb);
1483 return;
1484 }
1485
Johannes Berg988c0f72008-04-17 19:21:22 +02001486 rthdr = (struct ieee80211_tx_status_rtap_hdr *)
Johannes Bergb306f452007-07-10 19:32:08 +02001487 skb_push(skb, sizeof(*rthdr));
1488
1489 memset(rthdr, 0, sizeof(*rthdr));
1490 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
1491 rthdr->hdr.it_present =
1492 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
1493 (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
1494
1495 if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
1496 !is_multicast_ether_addr(hdr->addr1))
1497 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
1498
1499 if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
1500 (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
1501 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
1502 else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
1503 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
1504
1505 rthdr->data_retries = status->retry_count;
1506
Michael Wu3d30d942008-01-31 19:48:27 +01001507 /* XXX: is this sufficient for BPF? */
1508 skb_set_mac_header(skb, 0);
1509 skb->ip_summed = CHECKSUM_UNNECESSARY;
1510 skb->pkt_type = PACKET_OTHERHOST;
1511 skb->protocol = htons(ETH_P_802_2);
1512 memset(skb->cb, 0, sizeof(skb->cb));
Johannes Bergb306f452007-07-10 19:32:08 +02001513
Michael Wu3d30d942008-01-31 19:48:27 +01001514 rcu_read_lock();
1515 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg51fb61e2007-12-19 01:31:27 +01001516 if (sdata->vif.type == IEEE80211_IF_TYPE_MNTR) {
Johannes Bergb306f452007-07-10 19:32:08 +02001517 if (!netif_running(sdata->dev))
1518 continue;
Michael Wu3d30d942008-01-31 19:48:27 +01001519
1520 if (prev_dev) {
Johannes Berg79010422007-09-18 17:29:21 -04001521 skb2 = skb_clone(skb, GFP_ATOMIC);
Michael Wu3d30d942008-01-31 19:48:27 +01001522 if (skb2) {
1523 skb2->dev = prev_dev;
1524 netif_rx(skb2);
1525 }
1526 }
1527
1528 prev_dev = sdata->dev;
Johannes Bergb306f452007-07-10 19:32:08 +02001529 }
1530 }
Michael Wu3d30d942008-01-31 19:48:27 +01001531 if (prev_dev) {
1532 skb->dev = prev_dev;
1533 netif_rx(skb);
1534 skb = NULL;
1535 }
Johannes Berg79010422007-09-18 17:29:21 -04001536 rcu_read_unlock();
Michael Wu3d30d942008-01-31 19:48:27 +01001537 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -07001538}
1539EXPORT_SYMBOL(ieee80211_tx_status);
1540
Jiri Bencf0706e82007-05-05 11:45:53 -07001541struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
1542 const struct ieee80211_ops *ops)
1543{
Jiri Bencf0706e82007-05-05 11:45:53 -07001544 struct ieee80211_local *local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001545 int priv_size;
1546 struct wiphy *wiphy;
1547
1548 /* Ensure 32-byte alignment of our private data and hw private data.
1549 * We use the wiphy priv data for both our ieee80211_local and for
1550 * the driver's private data
1551 *
1552 * In memory it'll be like this:
1553 *
1554 * +-------------------------+
1555 * | struct wiphy |
1556 * +-------------------------+
1557 * | struct ieee80211_local |
1558 * +-------------------------+
1559 * | driver's private data |
1560 * +-------------------------+
1561 *
1562 */
1563 priv_size = ((sizeof(struct ieee80211_local) +
1564 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
1565 priv_data_len;
1566
1567 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
1568
1569 if (!wiphy)
1570 return NULL;
1571
1572 wiphy->privid = mac80211_wiphy_privid;
1573
1574 local = wiphy_priv(wiphy);
1575 local->hw.wiphy = wiphy;
1576
1577 local->hw.priv = (char *)local +
1578 ((sizeof(struct ieee80211_local) +
1579 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
1580
Johannes Berg4480f15c2007-07-10 19:32:10 +02001581 BUG_ON(!ops->tx);
Johannes Berg4150c572007-09-17 01:29:23 -04001582 BUG_ON(!ops->start);
1583 BUG_ON(!ops->stop);
Johannes Berg4480f15c2007-07-10 19:32:10 +02001584 BUG_ON(!ops->config);
1585 BUG_ON(!ops->add_interface);
Johannes Berg4150c572007-09-17 01:29:23 -04001586 BUG_ON(!ops->remove_interface);
1587 BUG_ON(!ops->configure_filter);
Jiri Bencf0706e82007-05-05 11:45:53 -07001588 local->ops = ops;
1589
Jiri Bencf0706e82007-05-05 11:45:53 -07001590 local->hw.queues = 1; /* default */
1591
Jiri Bencf0706e82007-05-05 11:45:53 -07001592 local->bridge_packets = 1;
1593
1594 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1595 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1596 local->short_retry_limit = 7;
1597 local->long_retry_limit = 4;
1598 local->hw.conf.radio_enabled = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07001599
Johannes Berg79010422007-09-18 17:29:21 -04001600 INIT_LIST_HEAD(&local->interfaces);
Jiri Bencf0706e82007-05-05 11:45:53 -07001601
Johannes Bergb16bd152008-04-11 21:40:35 +02001602 spin_lock_init(&local->key_lock);
1603
Jiri Bencf0706e82007-05-05 11:45:53 -07001604 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
Jiri Bencf0706e82007-05-05 11:45:53 -07001605
1606 sta_info_init(local);
1607
Jiri Bencf0706e82007-05-05 11:45:53 -07001608 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1609 (unsigned long)local);
1610 tasklet_disable(&local->tx_pending_tasklet);
1611
1612 tasklet_init(&local->tasklet,
1613 ieee80211_tasklet_handler,
1614 (unsigned long) local);
1615 tasklet_disable(&local->tasklet);
1616
1617 skb_queue_head_init(&local->skb_queue);
1618 skb_queue_head_init(&local->skb_queue_unreliable);
1619
1620 return local_to_hw(local);
1621}
1622EXPORT_SYMBOL(ieee80211_alloc_hw);
1623
1624int ieee80211_register_hw(struct ieee80211_hw *hw)
1625{
1626 struct ieee80211_local *local = hw_to_local(hw);
1627 const char *name;
1628 int result;
Johannes Berg8318d782008-01-24 19:38:38 +01001629 enum ieee80211_band band;
Johannes Berg96d51052008-02-08 09:48:13 +01001630 struct net_device *mdev;
1631 struct ieee80211_sub_if_data *sdata;
Johannes Berg8318d782008-01-24 19:38:38 +01001632
1633 /*
1634 * generic code guarantees at least one band,
1635 * set this very early because much code assumes
1636 * that hw.conf.channel is assigned
1637 */
1638 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1639 struct ieee80211_supported_band *sband;
1640
1641 sband = local->hw.wiphy->bands[band];
1642 if (sband) {
1643 /* init channel we're on */
1644 local->hw.conf.channel =
1645 local->oper_channel =
1646 local->scan_channel = &sband->channels[0];
1647 break;
1648 }
1649 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001650
1651 result = wiphy_register(local->hw.wiphy);
1652 if (result < 0)
1653 return result;
1654
Johannes Berg96d51052008-02-08 09:48:13 +01001655 /* for now, mdev needs sub_if_data :/ */
1656 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
1657 "wmaster%d", ether_setup);
1658 if (!mdev)
1659 goto fail_mdev_alloc;
1660
1661 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
1662 mdev->ieee80211_ptr = &sdata->wdev;
1663 sdata->wdev.wiphy = local->hw.wiphy;
1664
1665 local->mdev = mdev;
1666
1667 ieee80211_rx_bss_list_init(mdev);
1668
1669 mdev->hard_start_xmit = ieee80211_master_start_xmit;
1670 mdev->open = ieee80211_master_open;
1671 mdev->stop = ieee80211_master_stop;
1672 mdev->type = ARPHRD_IEEE80211;
1673 mdev->header_ops = &ieee80211_header_ops;
1674 mdev->set_multicast_list = ieee80211_master_set_multicast_list;
1675
1676 sdata->vif.type = IEEE80211_IF_TYPE_AP;
1677 sdata->dev = mdev;
1678 sdata->local = local;
1679 sdata->u.ap.force_unicast_rateidx = -1;
1680 sdata->u.ap.max_ratectrl_rateidx = -1;
1681 ieee80211_if_sdata_init(sdata);
1682
1683 /* no RCU needed since we're still during init phase */
1684 list_add_tail(&sdata->list, &local->interfaces);
1685
Jiri Bencf0706e82007-05-05 11:45:53 -07001686 name = wiphy_dev(local->hw.wiphy)->driver->name;
1687 local->hw.workqueue = create_singlethread_workqueue(name);
1688 if (!local->hw.workqueue) {
1689 result = -ENOMEM;
1690 goto fail_workqueue;
1691 }
1692
Johannes Bergb306f452007-07-10 19:32:08 +02001693 /*
1694 * The hardware needs headroom for sending the frame,
1695 * and we need some headroom for passing the frame to monitor
1696 * interfaces, but never both at the same time.
1697 */
Jiri Benc33ccad32007-07-18 17:10:44 +02001698 local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1699 sizeof(struct ieee80211_tx_status_rtap_hdr));
Johannes Bergb306f452007-07-10 19:32:08 +02001700
Jiri Bence9f207f2007-05-05 11:46:38 -07001701 debugfs_hw_add(local);
1702
Jiri Bencf0706e82007-05-05 11:45:53 -07001703 local->hw.conf.beacon_int = 1000;
1704
1705 local->wstats_flags |= local->hw.max_rssi ?
1706 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1707 local->wstats_flags |= local->hw.max_signal ?
1708 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1709 local->wstats_flags |= local->hw.max_noise ?
1710 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1711 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1712 local->wstats_flags |= IW_QUAL_DBM;
1713
1714 result = sta_info_start(local);
1715 if (result < 0)
1716 goto fail_sta_info;
1717
1718 rtnl_lock();
1719 result = dev_alloc_name(local->mdev, local->mdev->name);
1720 if (result < 0)
1721 goto fail_dev;
1722
1723 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1724 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1725
1726 result = register_netdevice(local->mdev);
1727 if (result < 0)
1728 goto fail_dev;
1729
Jiri Bence9f207f2007-05-05 11:46:38 -07001730 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Johannes Berg5b2812e2007-09-26 14:27:23 +02001731 ieee80211_if_set_type(local->mdev, IEEE80211_IF_TYPE_AP);
Jiri Bence9f207f2007-05-05 11:46:38 -07001732
Johannes Berg830f9032007-10-28 14:51:05 +01001733 result = ieee80211_init_rate_ctrl_alg(local,
1734 hw->rate_control_algorithm);
Jiri Bencf0706e82007-05-05 11:45:53 -07001735 if (result < 0) {
1736 printk(KERN_DEBUG "%s: Failed to initialize rate control "
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001737 "algorithm\n", wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001738 goto fail_rate;
1739 }
1740
1741 result = ieee80211_wep_init(local);
1742
1743 if (result < 0) {
1744 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001745 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001746 goto fail_wep;
1747 }
1748
Johannes Berge100bb62008-04-30 18:51:21 +02001749 if (hw->queues > IEEE80211_MAX_QUEUES)
1750 hw->queues = IEEE80211_MAX_QUEUES;
1751 if (hw->ampdu_queues > IEEE80211_MAX_AMPDU_QUEUES)
1752 hw->ampdu_queues = IEEE80211_MAX_AMPDU_QUEUES;
1753
Jiri Bencf0706e82007-05-05 11:45:53 -07001754 ieee80211_install_qdisc(local->mdev);
1755
1756 /* add one default STA interface */
1757 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
Luis Carlos Coboee385852008-02-23 15:17:11 +01001758 IEEE80211_IF_TYPE_STA, NULL);
Jiri Bencf0706e82007-05-05 11:45:53 -07001759 if (result)
1760 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001761 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001762
1763 local->reg_state = IEEE80211_DEV_REGISTERED;
1764 rtnl_unlock();
1765
1766 ieee80211_led_init(local);
1767
1768 return 0;
1769
1770fail_wep:
1771 rate_control_deinitialize(local);
1772fail_rate:
Jiri Bence9f207f2007-05-05 11:46:38 -07001773 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001774 unregister_netdevice(local->mdev);
1775fail_dev:
1776 rtnl_unlock();
1777 sta_info_stop(local);
1778fail_sta_info:
Jiri Bence9f207f2007-05-05 11:46:38 -07001779 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001780 destroy_workqueue(local->hw.workqueue);
1781fail_workqueue:
Johannes Berg96d51052008-02-08 09:48:13 +01001782 ieee80211_if_free(local->mdev);
1783 local->mdev = NULL;
1784fail_mdev_alloc:
Jiri Bencf0706e82007-05-05 11:45:53 -07001785 wiphy_unregister(local->hw.wiphy);
1786 return result;
1787}
1788EXPORT_SYMBOL(ieee80211_register_hw);
1789
Jiri Bencf0706e82007-05-05 11:45:53 -07001790void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1791{
1792 struct ieee80211_local *local = hw_to_local(hw);
1793 struct ieee80211_sub_if_data *sdata, *tmp;
Jiri Bencf0706e82007-05-05 11:45:53 -07001794
1795 tasklet_kill(&local->tx_pending_tasklet);
1796 tasklet_kill(&local->tasklet);
1797
1798 rtnl_lock();
1799
1800 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1801
1802 local->reg_state = IEEE80211_DEV_UNREGISTERED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001803
Johannes Berg79010422007-09-18 17:29:21 -04001804 /*
1805 * At this point, interface list manipulations are fine
1806 * because the driver cannot be handing us frames any
1807 * more and the tasklet is killed.
1808 */
Johannes Berg5b2812e2007-09-26 14:27:23 +02001809
1810 /*
1811 * First, we remove all non-master interfaces. Do this because they
1812 * may have bss pointer dependency on the master, and when we free
1813 * the master these would be freed as well, breaking our list
1814 * iteration completely.
1815 */
1816 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
1817 if (sdata->dev == local->mdev)
1818 continue;
1819 list_del(&sdata->list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001820 __ieee80211_if_del(local, sdata);
Johannes Berg5b2812e2007-09-26 14:27:23 +02001821 }
1822
1823 /* then, finally, remove the master interface */
1824 __ieee80211_if_del(local, IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001825
1826 rtnl_unlock();
1827
Jiri Bencf0706e82007-05-05 11:45:53 -07001828 ieee80211_rx_bss_list_deinit(local->mdev);
1829 ieee80211_clear_tx_pending(local);
1830 sta_info_stop(local);
1831 rate_control_deinitialize(local);
Jiri Bence9f207f2007-05-05 11:46:38 -07001832 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001833
Jiri Bencf0706e82007-05-05 11:45:53 -07001834 if (skb_queue_len(&local->skb_queue)
1835 || skb_queue_len(&local->skb_queue_unreliable))
1836 printk(KERN_WARNING "%s: skb_queue not empty\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001837 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001838 skb_queue_purge(&local->skb_queue);
1839 skb_queue_purge(&local->skb_queue_unreliable);
1840
1841 destroy_workqueue(local->hw.workqueue);
1842 wiphy_unregister(local->hw.wiphy);
1843 ieee80211_wep_free(local);
1844 ieee80211_led_exit(local);
Johannes Berg96d51052008-02-08 09:48:13 +01001845 ieee80211_if_free(local->mdev);
1846 local->mdev = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001847}
1848EXPORT_SYMBOL(ieee80211_unregister_hw);
1849
1850void ieee80211_free_hw(struct ieee80211_hw *hw)
1851{
1852 struct ieee80211_local *local = hw_to_local(hw);
1853
Jiri Bencf0706e82007-05-05 11:45:53 -07001854 wiphy_free(local->hw.wiphy);
1855}
1856EXPORT_SYMBOL(ieee80211_free_hw);
1857
Jiri Bencf0706e82007-05-05 11:45:53 -07001858static int __init ieee80211_init(void)
1859{
1860 struct sk_buff *skb;
1861 int ret;
1862
1863 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1864
Johannes Berg4b475892008-01-02 15:17:03 +01001865 ret = rc80211_pid_init();
Mattias Nisslerad018372007-12-19 01:25:57 +01001866 if (ret)
Adrian Bunkd9357132008-03-04 15:26:15 -08001867 goto out;
Johannes Bergac71c692007-10-28 14:17:44 +01001868
Jiri Bencf0706e82007-05-05 11:45:53 -07001869 ret = ieee80211_wme_register();
1870 if (ret) {
1871 printk(KERN_DEBUG "ieee80211_init: failed to "
1872 "initialize WME (err=%d)\n", ret);
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001873 goto out_cleanup_pid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001874 }
1875
Jiri Bence9f207f2007-05-05 11:46:38 -07001876 ieee80211_debugfs_netdev_init();
1877
Jiri Bencf0706e82007-05-05 11:45:53 -07001878 return 0;
Mattias Nisslerad018372007-12-19 01:25:57 +01001879
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001880 out_cleanup_pid:
Johannes Berg4b475892008-01-02 15:17:03 +01001881 rc80211_pid_exit();
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001882 out:
Mattias Nisslerad018372007-12-19 01:25:57 +01001883 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07001884}
1885
Jiri Bencf0706e82007-05-05 11:45:53 -07001886static void __exit ieee80211_exit(void)
1887{
Johannes Berg4b475892008-01-02 15:17:03 +01001888 rc80211_pid_exit();
Johannes Bergac71c692007-10-28 14:17:44 +01001889
Johannes Berg3b967662008-04-08 17:56:52 +02001890 /*
1891 * For key todo, it'll be empty by now but the work
1892 * might still be scheduled.
1893 */
1894 flush_scheduled_work();
1895
Luis Carlos Cobof7a92142008-02-23 15:17:18 +01001896 if (mesh_allocated)
1897 ieee80211s_stop();
Johannes Berg902acc72008-02-23 15:17:19 +01001898
Jiri Bencf0706e82007-05-05 11:45:53 -07001899 ieee80211_wme_unregister();
Jiri Bence9f207f2007-05-05 11:46:38 -07001900 ieee80211_debugfs_netdev_exit();
Jiri Bencf0706e82007-05-05 11:45:53 -07001901}
1902
1903
Johannes Bergca9938f2007-09-11 12:50:32 +02001904subsys_initcall(ieee80211_init);
Jiri Bencf0706e82007-05-05 11:45:53 -07001905module_exit(ieee80211_exit);
1906
1907MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1908MODULE_LICENSE("GPL");