mac80211: adding 802.11n essential A-MSDU Rx capability
[linux-block.git] / net / mac80211 / ieee80211.c
CommitLineData
f0706e82
JB
1/*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <net/mac80211.h>
12#include <net/ieee80211_radiotap.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/netdevice.h>
16#include <linux/types.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/etherdevice.h>
20#include <linux/if_arp.h>
21#include <linux/wireless.h>
22#include <linux/rtnetlink.h>
f0706e82 23#include <linux/bitmap.h>
881d966b 24#include <net/net_namespace.h>
f0706e82
JB
25#include <net/cfg80211.h>
26
f0706e82
JB
27#include "ieee80211_i.h"
28#include "ieee80211_rate.h"
29#include "wep.h"
f0706e82
JB
30#include "wme.h"
31#include "aes_ccm.h"
32#include "ieee80211_led.h"
e0eb6859 33#include "cfg.h"
e9f207f0
JB
34#include "debugfs.h"
35#include "debugfs_netdev.h"
f0706e82 36
b306f453
JB
37/*
38 * For seeing transmitted packets on monitor interfaces
39 * we have a radiotap header too.
40 */
41struct ieee80211_tx_status_rtap_hdr {
42 struct ieee80211_radiotap_header hdr;
43 __le16 tx_flags;
44 u8 data_retries;
45} __attribute__ ((packed));
46
b2c258fb 47/* common interface routines */
b306f453 48
b95cce35 49static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
b2c258fb
JB
50{
51 memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
52 return ETH_ALEN;
53}
f0706e82 54
4150c572
JB
55/* must be called under mdev tx lock */
56static void ieee80211_configure_filter(struct ieee80211_local *local)
57{
58 unsigned int changed_flags;
59 unsigned int new_flags = 0;
60
53918994 61 if (atomic_read(&local->iff_promiscs))
4150c572
JB
62 new_flags |= FIF_PROMISC_IN_BSS;
63
53918994 64 if (atomic_read(&local->iff_allmultis))
4150c572
JB
65 new_flags |= FIF_ALLMULTI;
66
67 if (local->monitors)
68 new_flags |= FIF_CONTROL |
69 FIF_OTHER_BSS |
70 FIF_BCN_PRBRESP_PROMISC;
71
72 changed_flags = local->filter_flags ^ new_flags;
73
74 /* be a bit nasty */
75 new_flags |= (1<<31);
76
77 local->ops->configure_filter(local_to_hw(local),
78 changed_flags, &new_flags,
79 local->mdev->mc_count,
80 local->mdev->mc_list);
81
82 WARN_ON(new_flags & (1<<31));
83
84 local->filter_flags = new_flags & ~(1<<31);
85}
86
b2c258fb 87/* master interface */
f0706e82 88
b2c258fb
JB
89static int ieee80211_master_open(struct net_device *dev)
90{
91 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
92 struct ieee80211_sub_if_data *sdata;
93 int res = -EOPNOTSUPP;
f0706e82 94
79010420
JB
95 /* we hold the RTNL here so can safely walk the list */
96 list_for_each_entry(sdata, &local->interfaces, list) {
b2c258fb
JB
97 if (sdata->dev != dev && netif_running(sdata->dev)) {
98 res = 0;
99 break;
100 }
101 }
b2c258fb
JB
102 return res;
103}
f0706e82 104
b2c258fb 105static int ieee80211_master_stop(struct net_device *dev)
f0706e82 106{
b2c258fb
JB
107 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
108 struct ieee80211_sub_if_data *sdata;
f0706e82 109
79010420
JB
110 /* we hold the RTNL here so can safely walk the list */
111 list_for_each_entry(sdata, &local->interfaces, list)
b2c258fb
JB
112 if (sdata->dev != dev && netif_running(sdata->dev))
113 dev_close(sdata->dev);
f0706e82 114
b2c258fb
JB
115 return 0;
116}
f0706e82 117
4150c572
JB
118static void ieee80211_master_set_multicast_list(struct net_device *dev)
119{
120 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
121
122 ieee80211_configure_filter(local);
123}
124
b2c258fb 125/* regular interfaces */
f0706e82 126
b2c258fb 127static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
f0706e82 128{
b2c258fb
JB
129 /* FIX: what would be proper limits for MTU?
130 * This interface uses 802.3 frames. */
131 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
132 printk(KERN_WARNING "%s: invalid MTU %d\n",
133 dev->name, new_mtu);
134 return -EINVAL;
135 }
f0706e82 136
b2c258fb
JB
137#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
138 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
139#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
140 dev->mtu = new_mtu;
f0706e82
JB
141 return 0;
142}
143
b2c258fb
JB
144static inline int identical_mac_addr_allowed(int type1, int type2)
145{
146 return (type1 == IEEE80211_IF_TYPE_MNTR ||
147 type2 == IEEE80211_IF_TYPE_MNTR ||
148 (type1 == IEEE80211_IF_TYPE_AP &&
149 type2 == IEEE80211_IF_TYPE_WDS) ||
150 (type1 == IEEE80211_IF_TYPE_WDS &&
151 (type2 == IEEE80211_IF_TYPE_WDS ||
152 type2 == IEEE80211_IF_TYPE_AP)) ||
153 (type1 == IEEE80211_IF_TYPE_AP &&
154 type2 == IEEE80211_IF_TYPE_VLAN) ||
155 (type1 == IEEE80211_IF_TYPE_VLAN &&
156 (type2 == IEEE80211_IF_TYPE_AP ||
157 type2 == IEEE80211_IF_TYPE_VLAN)));
158}
f0706e82 159
b2c258fb 160static int ieee80211_open(struct net_device *dev)
e2ebc74d 161{
b2c258fb
JB
162 struct ieee80211_sub_if_data *sdata, *nsdata;
163 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
164 struct ieee80211_if_init_conf conf;
165 int res;
f0706e82 166
b2c258fb 167 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
0ec3ca44 168
79010420
JB
169 /* we hold the RTNL here so can safely walk the list */
170 list_for_each_entry(nsdata, &local->interfaces, list) {
b2c258fb 171 struct net_device *ndev = nsdata->dev;
e2ebc74d 172
b2c258fb 173 if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
0ec3ca44
JB
174 compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0) {
175 /*
176 * check whether it may have the same address
177 */
178 if (!identical_mac_addr_allowed(sdata->type,
79010420 179 nsdata->type))
0ec3ca44 180 return -ENOTUNIQ;
0ec3ca44
JB
181
182 /*
183 * can only add VLANs to enabled APs
184 */
185 if (sdata->type == IEEE80211_IF_TYPE_VLAN &&
186 nsdata->type == IEEE80211_IF_TYPE_AP &&
187 netif_running(nsdata->dev))
188 sdata->u.vlan.ap = nsdata;
b2c258fb
JB
189 }
190 }
f0706e82 191
0ec3ca44
JB
192 switch (sdata->type) {
193 case IEEE80211_IF_TYPE_WDS:
194 if (is_zero_ether_addr(sdata->u.wds.remote_addr))
195 return -ENOLINK;
196 break;
197 case IEEE80211_IF_TYPE_VLAN:
198 if (!sdata->u.vlan.ap)
199 return -ENOLINK;
200 break;
fb1c1cd6 201 case IEEE80211_IF_TYPE_AP:
fb1c1cd6
JB
202 case IEEE80211_IF_TYPE_STA:
203 case IEEE80211_IF_TYPE_MNTR:
204 case IEEE80211_IF_TYPE_IBSS:
205 /* no special treatment */
206 break;
a2897552
JB
207 case IEEE80211_IF_TYPE_INVALID:
208 /* cannot happen */
209 WARN_ON(1);
210 break;
0ec3ca44 211 }
f0706e82 212
b2c258fb
JB
213 if (local->open_count == 0) {
214 res = 0;
4150c572
JB
215 if (local->ops->start)
216 res = local->ops->start(local_to_hw(local));
217 if (res)
b2c258fb 218 return res;
8b393f1d 219 ieee80211_hw_config(local);
b2c258fb 220 }
f0706e82 221
4150c572 222 switch (sdata->type) {
0ec3ca44
JB
223 case IEEE80211_IF_TYPE_VLAN:
224 list_add(&sdata->u.vlan.list, &sdata->u.vlan.ap->u.ap.vlans);
225 /* no need to tell driver */
226 break;
4150c572
JB
227 case IEEE80211_IF_TYPE_MNTR:
228 /* must be before the call to ieee80211_configure_filter */
b2c258fb 229 local->monitors++;
4150c572
JB
230 if (local->monitors == 1) {
231 netif_tx_lock_bh(local->mdev);
232 ieee80211_configure_filter(local);
233 netif_tx_unlock_bh(local->mdev);
234
235 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
4150c572
JB
236 }
237 break;
238 case IEEE80211_IF_TYPE_STA:
239 case IEEE80211_IF_TYPE_IBSS:
240 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
241 /* fall through */
242 default:
243 conf.if_id = dev->ifindex;
244 conf.type = sdata->type;
245 conf.mac_addr = dev->dev_addr;
246 res = local->ops->add_interface(local_to_hw(local), &conf);
247 if (res && !local->open_count && local->ops->stop)
248 local->ops->stop(local_to_hw(local));
249 if (res)
250 return res;
251
b2c258fb 252 ieee80211_if_config(dev);
d9430a32 253 ieee80211_reset_erp_info(dev);
11a843b7 254 ieee80211_enable_keys(sdata);
4150c572
JB
255
256 if (sdata->type == IEEE80211_IF_TYPE_STA &&
ddd3d2be 257 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
4150c572
JB
258 netif_carrier_off(dev);
259 else
260 netif_carrier_on(dev);
d9430a32 261 }
f0706e82 262
4150c572
JB
263 if (local->open_count == 0) {
264 res = dev_open(local->mdev);
265 WARN_ON(res);
4150c572
JB
266 tasklet_enable(&local->tx_pending_tasklet);
267 tasklet_enable(&local->tasklet);
268 }
269
c1428b3f
JB
270 /*
271 * set_multicast_list will be invoked by the networking core
272 * which will check whether any increments here were done in
273 * error and sync them down to the hardware as filter flags.
274 */
275 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
276 atomic_inc(&local->iff_allmultis);
277
278 if (sdata->flags & IEEE80211_SDATA_PROMISC)
279 atomic_inc(&local->iff_promiscs);
280
4150c572 281 local->open_count++;
f0706e82 282
b2c258fb 283 netif_start_queue(dev);
4150c572 284
b2c258fb 285 return 0;
f0706e82 286}
f0706e82 287
4150c572 288static int ieee80211_stop(struct net_device *dev)
f0706e82 289{
4150c572 290 struct ieee80211_sub_if_data *sdata;
f0706e82 291 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
4150c572
JB
292 struct ieee80211_if_init_conf conf;
293
294 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
295
296 netif_stop_queue(dev);
297
c1428b3f
JB
298 /*
299 * Don't count this interface for promisc/allmulti while it
300 * is down. dev_mc_unsync() will invoke set_multicast_list
301 * on the master interface which will sync these down to the
302 * hardware as filter flags.
303 */
304 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
305 atomic_dec(&local->iff_allmultis);
306
307 if (sdata->flags & IEEE80211_SDATA_PROMISC)
308 atomic_dec(&local->iff_promiscs);
309
4150c572
JB
310 dev_mc_unsync(local->mdev, dev);
311
0ec3ca44
JB
312 /* down all dependent devices, that is VLANs */
313 if (sdata->type == IEEE80211_IF_TYPE_AP) {
314 struct ieee80211_sub_if_data *vlan, *tmp;
315
316 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
317 u.vlan.list)
318 dev_close(vlan->dev);
319 WARN_ON(!list_empty(&sdata->u.ap.vlans));
320 }
321
4150c572 322 local->open_count--;
f0706e82 323
b2c258fb 324 switch (sdata->type) {
0ec3ca44
JB
325 case IEEE80211_IF_TYPE_VLAN:
326 list_del(&sdata->u.vlan.list);
327 sdata->u.vlan.ap = NULL;
328 /* no need to tell driver */
329 break;
4150c572
JB
330 case IEEE80211_IF_TYPE_MNTR:
331 local->monitors--;
332 if (local->monitors == 0) {
333 netif_tx_lock_bh(local->mdev);
334 ieee80211_configure_filter(local);
335 netif_tx_unlock_bh(local->mdev);
336
8b393f1d 337 local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
4150c572
JB
338 }
339 break;
b2c258fb
JB
340 case IEEE80211_IF_TYPE_STA:
341 case IEEE80211_IF_TYPE_IBSS:
342 sdata->u.sta.state = IEEE80211_DISABLED;
343 del_timer_sync(&sdata->u.sta.timer);
2a8a9a88 344 /*
79010420
JB
345 * When we get here, the interface is marked down.
346 * Call synchronize_rcu() to wait for the RX path
347 * should it be using the interface and enqueuing
348 * frames at this very time on another CPU.
2a8a9a88 349 */
79010420 350 synchronize_rcu();
b2c258fb 351 skb_queue_purge(&sdata->u.sta.skb_queue);
2a8a9a88 352
ece8eddd
ZY
353 if (local->scan_dev == sdata->dev) {
354 if (!local->ops->hw_scan) {
355 local->sta_sw_scanning = 0;
356 cancel_delayed_work(&local->scan_work);
357 } else
358 local->sta_hw_scanning = 0;
b2c258fb 359 }
ece8eddd 360
b2c258fb 361 flush_workqueue(local->hw.workqueue);
a10605e5
ZY
362
363 sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
364 kfree(sdata->u.sta.extra_ie);
365 sdata->u.sta.extra_ie = NULL;
366 sdata->u.sta.extra_ie_len = 0;
4150c572
JB
367 /* fall through */
368 default:
369 conf.if_id = dev->ifindex;
370 conf.type = sdata->type;
371 conf.mac_addr = dev->dev_addr;
11a843b7
JB
372 /* disable all keys for as long as this netdev is down */
373 ieee80211_disable_keys(sdata);
4150c572 374 local->ops->remove_interface(local_to_hw(local), &conf);
f0706e82
JB
375 }
376
b2c258fb
JB
377 if (local->open_count == 0) {
378 if (netif_running(local->mdev))
379 dev_close(local->mdev);
4150c572 380
b2c258fb
JB
381 if (local->ops->stop)
382 local->ops->stop(local_to_hw(local));
4150c572 383
b2c258fb
JB
384 tasklet_disable(&local->tx_pending_tasklet);
385 tasklet_disable(&local->tasklet);
386 }
b2c258fb 387
f0706e82
JB
388 return 0;
389}
390
f0706e82
JB
391static void ieee80211_set_multicast_list(struct net_device *dev)
392{
393 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
394 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
4150c572 395 int allmulti, promisc, sdata_allmulti, sdata_promisc;
f0706e82 396
4150c572
JB
397 allmulti = !!(dev->flags & IFF_ALLMULTI);
398 promisc = !!(dev->flags & IFF_PROMISC);
b52f2198
JB
399 sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
400 sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
4150c572
JB
401
402 if (allmulti != sdata_allmulti) {
403 if (dev->flags & IFF_ALLMULTI)
53918994 404 atomic_inc(&local->iff_allmultis);
4150c572 405 else
53918994 406 atomic_dec(&local->iff_allmultis);
13262ffd 407 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
f0706e82 408 }
4150c572
JB
409
410 if (promisc != sdata_promisc) {
411 if (dev->flags & IFF_PROMISC)
53918994 412 atomic_inc(&local->iff_promiscs);
4150c572 413 else
53918994 414 atomic_dec(&local->iff_promiscs);
13262ffd 415 sdata->flags ^= IEEE80211_SDATA_PROMISC;
f0706e82 416 }
4150c572
JB
417
418 dev_mc_sync(local->mdev, dev);
f0706e82
JB
419}
420
3b04ddde
SH
421static const struct header_ops ieee80211_header_ops = {
422 .create = eth_header,
423 .parse = header_parse_80211,
424 .rebuild = eth_rebuild_header,
425 .cache = eth_header_cache,
426 .cache_update = eth_header_cache_update,
427};
428
f9d540ee 429/* Must not be called for mdev */
b2c258fb 430void ieee80211_if_setup(struct net_device *dev)
f0706e82 431{
b2c258fb
JB
432 ether_setup(dev);
433 dev->hard_start_xmit = ieee80211_subif_start_xmit;
434 dev->wireless_handlers = &ieee80211_iw_handler_def;
435 dev->set_multicast_list = ieee80211_set_multicast_list;
436 dev->change_mtu = ieee80211_change_mtu;
b2c258fb
JB
437 dev->open = ieee80211_open;
438 dev->stop = ieee80211_stop;
b2c258fb
JB
439 dev->destructor = ieee80211_if_free;
440}
f0706e82 441
b2c258fb
JB
442/* WDS specialties */
443
444int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
445{
446 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
447 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
448 struct sta_info *sta;
0795af57 449 DECLARE_MAC_BUF(mac);
b2c258fb
JB
450
451 if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
452 return 0;
453
454 /* Create STA entry for the new peer */
455 sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
456 if (!sta)
457 return -ENOMEM;
458 sta_info_put(sta);
459
460 /* Remove STA entry for the old peer */
461 sta = sta_info_get(local, sdata->u.wds.remote_addr);
462 if (sta) {
be8755e1 463 sta_info_free(sta);
b2c258fb 464 sta_info_put(sta);
b2c258fb
JB
465 } else {
466 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
0795af57
JP
467 "peer %s\n",
468 dev->name, print_mac(mac, sdata->u.wds.remote_addr));
b2c258fb
JB
469 }
470
471 /* Update WDS link data */
472 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
473
474 return 0;
f0706e82 475}
f0706e82 476
b2c258fb
JB
477/* everything else */
478
b2c258fb
JB
479static int __ieee80211_if_config(struct net_device *dev,
480 struct sk_buff *beacon,
481 struct ieee80211_tx_control *control)
482{
483 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
484 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
485 struct ieee80211_if_conf conf;
b2c258fb
JB
486
487 if (!local->ops->config_interface || !netif_running(dev))
f0706e82 488 return 0;
f0706e82 489
b2c258fb 490 memset(&conf, 0, sizeof(conf));
4480f15c 491 conf.type = sdata->type;
b2c258fb
JB
492 if (sdata->type == IEEE80211_IF_TYPE_STA ||
493 sdata->type == IEEE80211_IF_TYPE_IBSS) {
4150c572 494 conf.bssid = sdata->u.sta.bssid;
b2c258fb
JB
495 conf.ssid = sdata->u.sta.ssid;
496 conf.ssid_len = sdata->u.sta.ssid_len;
b2c258fb
JB
497 } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
498 conf.ssid = sdata->u.ap.ssid;
499 conf.ssid_len = sdata->u.ap.ssid_len;
b2c258fb
JB
500 conf.beacon = beacon;
501 conf.beacon_control = control;
f0706e82 502 }
b2c258fb
JB
503 return local->ops->config_interface(local_to_hw(local),
504 dev->ifindex, &conf);
f0706e82
JB
505}
506
b2c258fb
JB
507int ieee80211_if_config(struct net_device *dev)
508{
509 return __ieee80211_if_config(dev, NULL, NULL);
510}
f0706e82 511
b2c258fb 512int ieee80211_if_config_beacon(struct net_device *dev)
f0706e82 513{
f0706e82 514 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
b2c258fb
JB
515 struct ieee80211_tx_control control;
516 struct sk_buff *skb;
f0706e82 517
b2c258fb 518 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
f0706e82 519 return 0;
b2c258fb
JB
520 skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
521 if (!skb)
522 return -ENOMEM;
523 return __ieee80211_if_config(dev, skb, &control);
524}
f0706e82 525
b2c258fb
JB
526int ieee80211_hw_config(struct ieee80211_local *local)
527{
528 struct ieee80211_hw_mode *mode;
529 struct ieee80211_channel *chan;
530 int ret = 0;
f0706e82 531
ece8eddd 532 if (local->sta_sw_scanning) {
b2c258fb
JB
533 chan = local->scan_channel;
534 mode = local->scan_hw_mode;
535 } else {
536 chan = local->oper_channel;
537 mode = local->oper_hw_mode;
f0706e82
JB
538 }
539
b2c258fb
JB
540 local->hw.conf.channel = chan->chan;
541 local->hw.conf.channel_val = chan->val;
61609bc0
MB
542 if (!local->hw.conf.power_level) {
543 local->hw.conf.power_level = chan->power_level;
544 } else {
545 local->hw.conf.power_level = min(chan->power_level,
546 local->hw.conf.power_level);
547 }
b2c258fb
JB
548 local->hw.conf.freq = chan->freq;
549 local->hw.conf.phymode = mode->mode;
550 local->hw.conf.antenna_max = chan->antenna_max;
551 local->hw.conf.chan = chan;
552 local->hw.conf.mode = mode;
f0706e82 553
b2c258fb
JB
554#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
555 printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
556 "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
557 local->hw.conf.phymode);
558#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
559
f7c4daed 560 if (local->open_count)
b2c258fb 561 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
f0706e82 562
b2c258fb
JB
563 return ret;
564}
f0706e82 565
d9430a32
DD
566void ieee80211_erp_info_change_notify(struct net_device *dev, u8 changes)
567{
568 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
569 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
570 if (local->ops->erp_ie_changed)
571 local->ops->erp_ie_changed(local_to_hw(local), changes,
13262ffd
JS
572 !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION),
573 !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE));
d9430a32
DD
574}
575
576void ieee80211_reset_erp_info(struct net_device *dev)
577{
578 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
579
13262ffd
JS
580 sdata->flags &= ~(IEEE80211_SDATA_USE_PROTECTION |
581 IEEE80211_SDATA_SHORT_PREAMBLE);
d9430a32
DD
582 ieee80211_erp_info_change_notify(dev,
583 IEEE80211_ERP_CHANGE_PROTECTION |
584 IEEE80211_ERP_CHANGE_PREAMBLE);
585}
586
f0706e82
JB
587void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
588 struct sk_buff *skb,
589 struct ieee80211_tx_status *status)
590{
591 struct ieee80211_local *local = hw_to_local(hw);
592 struct ieee80211_tx_status *saved;
593 int tmp;
594
595 skb->dev = local->mdev;
596 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
597 if (unlikely(!saved)) {
598 if (net_ratelimit())
599 printk(KERN_WARNING "%s: Not enough memory, "
600 "dropping tx status", skb->dev->name);
601 /* should be dev_kfree_skb_irq, but due to this function being
602 * named _irqsafe instead of just _irq we can't be sure that
603 * people won't call it from non-irq contexts */
604 dev_kfree_skb_any(skb);
605 return;
606 }
607 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
608 /* copy pointer to saved status into skb->cb for use by tasklet */
609 memcpy(skb->cb, &saved, sizeof(saved));
610
611 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
612 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
613 &local->skb_queue : &local->skb_queue_unreliable, skb);
614 tmp = skb_queue_len(&local->skb_queue) +
615 skb_queue_len(&local->skb_queue_unreliable);
616 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
617 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
618 memcpy(&saved, skb->cb, sizeof(saved));
619 kfree(saved);
620 dev_kfree_skb_irq(skb);
621 tmp--;
622 I802_DEBUG_INC(local->tx_status_drop);
623 }
624 tasklet_schedule(&local->tasklet);
625}
626EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
627
628static void ieee80211_tasklet_handler(unsigned long data)
629{
630 struct ieee80211_local *local = (struct ieee80211_local *) data;
631 struct sk_buff *skb;
632 struct ieee80211_rx_status rx_status;
633 struct ieee80211_tx_status *tx_status;
634
635 while ((skb = skb_dequeue(&local->skb_queue)) ||
636 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
637 switch (skb->pkt_type) {
638 case IEEE80211_RX_MSG:
639 /* status is in skb->cb */
640 memcpy(&rx_status, skb->cb, sizeof(rx_status));
641 /* Clear skb->type in order to not confuse kernel
642 * netstack. */
643 skb->pkt_type = 0;
644 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
645 break;
646 case IEEE80211_TX_STATUS_MSG:
647 /* get pointer to saved status out of skb->cb */
648 memcpy(&tx_status, skb->cb, sizeof(tx_status));
649 skb->pkt_type = 0;
650 ieee80211_tx_status(local_to_hw(local),
651 skb, tx_status);
652 kfree(tx_status);
653 break;
654 default: /* should never get here! */
655 printk(KERN_ERR "%s: Unknown message type (%d)\n",
dd1cd4c6 656 wiphy_name(local->hw.wiphy), skb->pkt_type);
f0706e82
JB
657 dev_kfree_skb(skb);
658 break;
659 }
660 }
661}
662
f0706e82
JB
663/* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
664 * make a prepared TX frame (one that has been given to hw) to look like brand
665 * new IEEE 802.11 frame that is ready to go through TX processing again.
666 * Also, tx_packet_data in cb is restored from tx_control. */
667static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
668 struct ieee80211_key *key,
669 struct sk_buff *skb,
670 struct ieee80211_tx_control *control)
671{
672 int hdrlen, iv_len, mic_len;
673 struct ieee80211_tx_packet_data *pkt_data;
674
675 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
676 pkt_data->ifindex = control->ifindex;
e8bf9649
JS
677 pkt_data->flags = 0;
678 if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
679 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
680 if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
681 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
682 if (control->flags & IEEE80211_TXCTL_REQUEUE)
683 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
f0706e82
JB
684 pkt_data->queue = control->queue;
685
686 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
687
688 if (!key)
689 goto no_key;
690
8f20fc24 691 switch (key->conf.alg) {
f0706e82
JB
692 case ALG_WEP:
693 iv_len = WEP_IV_LEN;
694 mic_len = WEP_ICV_LEN;
695 break;
696 case ALG_TKIP:
697 iv_len = TKIP_IV_LEN;
698 mic_len = TKIP_ICV_LEN;
699 break;
700 case ALG_CCMP:
701 iv_len = CCMP_HDR_LEN;
702 mic_len = CCMP_MIC_LEN;
703 break;
704 default:
705 goto no_key;
706 }
707
8f20fc24 708 if (skb->len >= mic_len &&
11a843b7 709 !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
f0706e82
JB
710 skb_trim(skb, skb->len - mic_len);
711 if (skb->len >= iv_len && skb->len > hdrlen) {
712 memmove(skb->data + iv_len, skb->data, hdrlen);
713 skb_pull(skb, iv_len);
714 }
715
716no_key:
717 {
718 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
719 u16 fc = le16_to_cpu(hdr->frame_control);
720 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
721 fc &= ~IEEE80211_STYPE_QOS_DATA;
722 hdr->frame_control = cpu_to_le16(fc);
723 memmove(skb->data + 2, skb->data, hdrlen - 2);
724 skb_pull(skb, 2);
725 }
726 }
727}
728
f0706e82
JB
729void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
730 struct ieee80211_tx_status *status)
731{
732 struct sk_buff *skb2;
733 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
734 struct ieee80211_local *local = hw_to_local(hw);
735 u16 frag, type;
b306f453
JB
736 struct ieee80211_tx_status_rtap_hdr *rthdr;
737 struct ieee80211_sub_if_data *sdata;
738 int monitors;
f0706e82
JB
739
740 if (!status) {
741 printk(KERN_ERR
742 "%s: ieee80211_tx_status called with NULL status\n",
dd1cd4c6 743 wiphy_name(local->hw.wiphy));
f0706e82
JB
744 dev_kfree_skb(skb);
745 return;
746 }
747
748 if (status->excessive_retries) {
749 struct sta_info *sta;
750 sta = sta_info_get(local, hdr->addr1);
751 if (sta) {
752 if (sta->flags & WLAN_STA_PS) {
753 /* The STA is in power save mode, so assume
754 * that this TX packet failed because of that.
755 */
756 status->excessive_retries = 0;
757 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
758 }
759 sta_info_put(sta);
760 }
761 }
762
763 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
764 struct sta_info *sta;
765 sta = sta_info_get(local, hdr->addr1);
766 if (sta) {
767 sta->tx_filtered_count++;
768
769 /* Clear the TX filter mask for this STA when sending
770 * the next packet. If the STA went to power save mode,
771 * this will happen when it is waking up for the next
772 * time. */
773 sta->clear_dst_mask = 1;
774
775 /* TODO: Is the WLAN_STA_PS flag always set here or is
776 * the race between RX and TX status causing some
777 * packets to be filtered out before 80211.o gets an
778 * update for PS status? This seems to be the case, so
779 * no changes are likely to be needed. */
780 if (sta->flags & WLAN_STA_PS &&
781 skb_queue_len(&sta->tx_filtered) <
782 STA_MAX_TX_BUFFER) {
783 ieee80211_remove_tx_extra(local, sta->key,
784 skb,
785 &status->control);
786 skb_queue_tail(&sta->tx_filtered, skb);
787 } else if (!(sta->flags & WLAN_STA_PS) &&
788 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
789 /* Software retry the packet once */
790 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
791 ieee80211_remove_tx_extra(local, sta->key,
792 skb,
793 &status->control);
794 dev_queue_xmit(skb);
795 } else {
796 if (net_ratelimit()) {
797 printk(KERN_DEBUG "%s: dropped TX "
798 "filtered frame queue_len=%d "
799 "PS=%d @%lu\n",
dd1cd4c6 800 wiphy_name(local->hw.wiphy),
f0706e82
JB
801 skb_queue_len(
802 &sta->tx_filtered),
803 !!(sta->flags & WLAN_STA_PS),
804 jiffies);
805 }
806 dev_kfree_skb(skb);
807 }
808 sta_info_put(sta);
809 return;
810 }
811 } else {
812 /* FIXME: STUPID to call this with both local and local->mdev */
813 rate_control_tx_status(local, local->mdev, skb, status);
814 }
815
816 ieee80211_led_tx(local, 0);
817
818 /* SNMP counters
819 * Fragments are passed to low-level drivers as separate skbs, so these
820 * are actually fragments, not frames. Update frame counters only for
821 * the first fragment of the frame. */
822
823 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
824 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
825
826 if (status->flags & IEEE80211_TX_STATUS_ACK) {
827 if (frag == 0) {
828 local->dot11TransmittedFrameCount++;
829 if (is_multicast_ether_addr(hdr->addr1))
830 local->dot11MulticastTransmittedFrameCount++;
831 if (status->retry_count > 0)
832 local->dot11RetryCount++;
833 if (status->retry_count > 1)
834 local->dot11MultipleRetryCount++;
835 }
836
837 /* This counter shall be incremented for an acknowledged MPDU
838 * with an individual address in the address 1 field or an MPDU
839 * with a multicast address in the address 1 field of type Data
840 * or Management. */
841 if (!is_multicast_ether_addr(hdr->addr1) ||
842 type == IEEE80211_FTYPE_DATA ||
843 type == IEEE80211_FTYPE_MGMT)
844 local->dot11TransmittedFragmentCount++;
845 } else {
846 if (frag == 0)
847 local->dot11FailedCount++;
848 }
849
b306f453
JB
850 /* this was a transmitted frame, but now we want to reuse it */
851 skb_orphan(skb);
852
b306f453 853 if (!local->monitors) {
f0706e82
JB
854 dev_kfree_skb(skb);
855 return;
856 }
857
b306f453 858 /* send frame to monitor interfaces now */
f0706e82 859
b306f453
JB
860 if (skb_headroom(skb) < sizeof(*rthdr)) {
861 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
f0706e82
JB
862 dev_kfree_skb(skb);
863 return;
864 }
f0706e82 865
b306f453
JB
866 rthdr = (struct ieee80211_tx_status_rtap_hdr*)
867 skb_push(skb, sizeof(*rthdr));
868
869 memset(rthdr, 0, sizeof(*rthdr));
870 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
871 rthdr->hdr.it_present =
872 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
873 (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
874
875 if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
876 !is_multicast_ether_addr(hdr->addr1))
877 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
878
879 if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
880 (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
881 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
882 else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
883 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
884
885 rthdr->data_retries = status->retry_count;
886
79010420 887 rcu_read_lock();
b306f453 888 monitors = local->monitors;
79010420 889 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
b306f453
JB
890 /*
891 * Using the monitors counter is possibly racy, but
892 * if the value is wrong we simply either clone the skb
893 * once too much or forget sending it to one monitor iface
894 * The latter case isn't nice but fixing the race is much
895 * more complicated.
896 */
897 if (!monitors || !skb)
898 goto out;
899
900 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
901 if (!netif_running(sdata->dev))
902 continue;
903 monitors--;
904 if (monitors)
79010420 905 skb2 = skb_clone(skb, GFP_ATOMIC);
b306f453
JB
906 else
907 skb2 = NULL;
908 skb->dev = sdata->dev;
909 /* XXX: is this sufficient for BPF? */
910 skb_set_mac_header(skb, 0);
911 skb->ip_summed = CHECKSUM_UNNECESSARY;
912 skb->pkt_type = PACKET_OTHERHOST;
913 skb->protocol = htons(ETH_P_802_2);
914 memset(skb->cb, 0, sizeof(skb->cb));
915 netif_rx(skb);
916 skb = skb2;
b306f453
JB
917 }
918 }
919 out:
79010420 920 rcu_read_unlock();
b306f453
JB
921 if (skb)
922 dev_kfree_skb(skb);
f0706e82
JB
923}
924EXPORT_SYMBOL(ieee80211_tx_status);
925
f0706e82
JB
926struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
927 const struct ieee80211_ops *ops)
928{
929 struct net_device *mdev;
930 struct ieee80211_local *local;
931 struct ieee80211_sub_if_data *sdata;
932 int priv_size;
933 struct wiphy *wiphy;
934
935 /* Ensure 32-byte alignment of our private data and hw private data.
936 * We use the wiphy priv data for both our ieee80211_local and for
937 * the driver's private data
938 *
939 * In memory it'll be like this:
940 *
941 * +-------------------------+
942 * | struct wiphy |
943 * +-------------------------+
944 * | struct ieee80211_local |
945 * +-------------------------+
946 * | driver's private data |
947 * +-------------------------+
948 *
949 */
950 priv_size = ((sizeof(struct ieee80211_local) +
951 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
952 priv_data_len;
953
954 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
955
956 if (!wiphy)
957 return NULL;
958
959 wiphy->privid = mac80211_wiphy_privid;
960
961 local = wiphy_priv(wiphy);
962 local->hw.wiphy = wiphy;
963
964 local->hw.priv = (char *)local +
965 ((sizeof(struct ieee80211_local) +
966 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
967
4480f15c 968 BUG_ON(!ops->tx);
4150c572
JB
969 BUG_ON(!ops->start);
970 BUG_ON(!ops->stop);
4480f15c
JB
971 BUG_ON(!ops->config);
972 BUG_ON(!ops->add_interface);
4150c572
JB
973 BUG_ON(!ops->remove_interface);
974 BUG_ON(!ops->configure_filter);
f0706e82
JB
975 local->ops = ops;
976
977 /* for now, mdev needs sub_if_data :/ */
978 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
979 "wmaster%d", ether_setup);
980 if (!mdev) {
981 wiphy_free(wiphy);
982 return NULL;
983 }
984
985 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
986 mdev->ieee80211_ptr = &sdata->wdev;
987 sdata->wdev.wiphy = wiphy;
988
989 local->hw.queues = 1; /* default */
990
991 local->mdev = mdev;
992 local->rx_pre_handlers = ieee80211_rx_pre_handlers;
993 local->rx_handlers = ieee80211_rx_handlers;
994 local->tx_handlers = ieee80211_tx_handlers;
995
996 local->bridge_packets = 1;
997
998 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
999 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1000 local->short_retry_limit = 7;
1001 local->long_retry_limit = 4;
1002 local->hw.conf.radio_enabled = 1;
f0706e82 1003
b708e610 1004 local->enabled_modes = ~0;
f0706e82
JB
1005
1006 INIT_LIST_HEAD(&local->modes_list);
1007
79010420 1008 INIT_LIST_HEAD(&local->interfaces);
f0706e82
JB
1009
1010 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
f0706e82
JB
1011 ieee80211_rx_bss_list_init(mdev);
1012
1013 sta_info_init(local);
1014
1015 mdev->hard_start_xmit = ieee80211_master_start_xmit;
1016 mdev->open = ieee80211_master_open;
1017 mdev->stop = ieee80211_master_stop;
1018 mdev->type = ARPHRD_IEEE80211;
3b04ddde 1019 mdev->header_ops = &ieee80211_header_ops;
4150c572 1020 mdev->set_multicast_list = ieee80211_master_set_multicast_list;
f0706e82
JB
1021
1022 sdata->type = IEEE80211_IF_TYPE_AP;
1023 sdata->dev = mdev;
1024 sdata->local = local;
1025 sdata->u.ap.force_unicast_rateidx = -1;
1026 sdata->u.ap.max_ratectrl_rateidx = -1;
1027 ieee80211_if_sdata_init(sdata);
79010420
JB
1028 /* no RCU needed since we're still during init phase */
1029 list_add_tail(&sdata->list, &local->interfaces);
f0706e82
JB
1030
1031 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1032 (unsigned long)local);
1033 tasklet_disable(&local->tx_pending_tasklet);
1034
1035 tasklet_init(&local->tasklet,
1036 ieee80211_tasklet_handler,
1037 (unsigned long) local);
1038 tasklet_disable(&local->tasklet);
1039
1040 skb_queue_head_init(&local->skb_queue);
1041 skb_queue_head_init(&local->skb_queue_unreliable);
1042
1043 return local_to_hw(local);
1044}
1045EXPORT_SYMBOL(ieee80211_alloc_hw);
1046
1047int ieee80211_register_hw(struct ieee80211_hw *hw)
1048{
1049 struct ieee80211_local *local = hw_to_local(hw);
1050 const char *name;
1051 int result;
1052
1053 result = wiphy_register(local->hw.wiphy);
1054 if (result < 0)
1055 return result;
1056
1057 name = wiphy_dev(local->hw.wiphy)->driver->name;
1058 local->hw.workqueue = create_singlethread_workqueue(name);
1059 if (!local->hw.workqueue) {
1060 result = -ENOMEM;
1061 goto fail_workqueue;
1062 }
1063
b306f453
JB
1064 /*
1065 * The hardware needs headroom for sending the frame,
1066 * and we need some headroom for passing the frame to monitor
1067 * interfaces, but never both at the same time.
1068 */
33ccad35
JB
1069 local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1070 sizeof(struct ieee80211_tx_status_rtap_hdr));
b306f453 1071
e9f207f0
JB
1072 debugfs_hw_add(local);
1073
f0706e82
JB
1074 local->hw.conf.beacon_int = 1000;
1075
1076 local->wstats_flags |= local->hw.max_rssi ?
1077 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1078 local->wstats_flags |= local->hw.max_signal ?
1079 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1080 local->wstats_flags |= local->hw.max_noise ?
1081 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1082 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1083 local->wstats_flags |= IW_QUAL_DBM;
1084
1085 result = sta_info_start(local);
1086 if (result < 0)
1087 goto fail_sta_info;
1088
1089 rtnl_lock();
1090 result = dev_alloc_name(local->mdev, local->mdev->name);
1091 if (result < 0)
1092 goto fail_dev;
1093
1094 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1095 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1096
1097 result = register_netdevice(local->mdev);
1098 if (result < 0)
1099 goto fail_dev;
1100
e9f207f0 1101 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
5b2812e9 1102 ieee80211_if_set_type(local->mdev, IEEE80211_IF_TYPE_AP);
e9f207f0 1103
830f9038
JB
1104 result = ieee80211_init_rate_ctrl_alg(local,
1105 hw->rate_control_algorithm);
f0706e82
JB
1106 if (result < 0) {
1107 printk(KERN_DEBUG "%s: Failed to initialize rate control "
dd1cd4c6 1108 "algorithm\n", wiphy_name(local->hw.wiphy));
f0706e82
JB
1109 goto fail_rate;
1110 }
1111
1112 result = ieee80211_wep_init(local);
1113
1114 if (result < 0) {
1115 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
dd1cd4c6 1116 wiphy_name(local->hw.wiphy));
f0706e82
JB
1117 goto fail_wep;
1118 }
1119
1120 ieee80211_install_qdisc(local->mdev);
1121
1122 /* add one default STA interface */
1123 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
1124 IEEE80211_IF_TYPE_STA);
1125 if (result)
1126 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
dd1cd4c6 1127 wiphy_name(local->hw.wiphy));
f0706e82
JB
1128
1129 local->reg_state = IEEE80211_DEV_REGISTERED;
1130 rtnl_unlock();
1131
1132 ieee80211_led_init(local);
1133
1134 return 0;
1135
1136fail_wep:
1137 rate_control_deinitialize(local);
1138fail_rate:
e9f207f0 1139 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
f0706e82
JB
1140 unregister_netdevice(local->mdev);
1141fail_dev:
1142 rtnl_unlock();
1143 sta_info_stop(local);
1144fail_sta_info:
e9f207f0 1145 debugfs_hw_del(local);
f0706e82
JB
1146 destroy_workqueue(local->hw.workqueue);
1147fail_workqueue:
1148 wiphy_unregister(local->hw.wiphy);
1149 return result;
1150}
1151EXPORT_SYMBOL(ieee80211_register_hw);
1152
1153int ieee80211_register_hwmode(struct ieee80211_hw *hw,
1154 struct ieee80211_hw_mode *mode)
1155{
1156 struct ieee80211_local *local = hw_to_local(hw);
1157 struct ieee80211_rate *rate;
1158 int i;
1159
1160 INIT_LIST_HEAD(&mode->list);
1161 list_add_tail(&mode->list, &local->modes_list);
1162
1163 local->hw_modes |= (1 << mode->mode);
1164 for (i = 0; i < mode->num_rates; i++) {
1165 rate = &(mode->rates[i]);
1166 rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
1167 }
1168 ieee80211_prepare_rates(local, mode);
1169
1170 if (!local->oper_hw_mode) {
1171 /* Default to this mode */
1172 local->hw.conf.phymode = mode->mode;
1173 local->oper_hw_mode = local->scan_hw_mode = mode;
1174 local->oper_channel = local->scan_channel = &mode->channels[0];
1175 local->hw.conf.mode = local->oper_hw_mode;
1176 local->hw.conf.chan = local->oper_channel;
1177 }
1178
1179 if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
fd8bacc9 1180 ieee80211_set_default_regdomain(mode);
f0706e82
JB
1181
1182 return 0;
1183}
1184EXPORT_SYMBOL(ieee80211_register_hwmode);
1185
1186void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1187{
1188 struct ieee80211_local *local = hw_to_local(hw);
1189 struct ieee80211_sub_if_data *sdata, *tmp;
f0706e82
JB
1190 int i;
1191
1192 tasklet_kill(&local->tx_pending_tasklet);
1193 tasklet_kill(&local->tasklet);
1194
1195 rtnl_lock();
1196
1197 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1198
1199 local->reg_state = IEEE80211_DEV_UNREGISTERED;
f0706e82 1200
79010420
JB
1201 /*
1202 * At this point, interface list manipulations are fine
1203 * because the driver cannot be handing us frames any
1204 * more and the tasklet is killed.
1205 */
5b2812e9
JB
1206
1207 /*
1208 * First, we remove all non-master interfaces. Do this because they
1209 * may have bss pointer dependency on the master, and when we free
1210 * the master these would be freed as well, breaking our list
1211 * iteration completely.
1212 */
1213 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
1214 if (sdata->dev == local->mdev)
1215 continue;
1216 list_del(&sdata->list);
f0706e82 1217 __ieee80211_if_del(local, sdata);
5b2812e9
JB
1218 }
1219
1220 /* then, finally, remove the master interface */
1221 __ieee80211_if_del(local, IEEE80211_DEV_TO_SUB_IF(local->mdev));
f0706e82
JB
1222
1223 rtnl_unlock();
1224
f0706e82
JB
1225 ieee80211_rx_bss_list_deinit(local->mdev);
1226 ieee80211_clear_tx_pending(local);
1227 sta_info_stop(local);
1228 rate_control_deinitialize(local);
e9f207f0 1229 debugfs_hw_del(local);
f0706e82
JB
1230
1231 for (i = 0; i < NUM_IEEE80211_MODES; i++) {
1232 kfree(local->supp_rates[i]);
1233 kfree(local->basic_rates[i]);
1234 }
1235
1236 if (skb_queue_len(&local->skb_queue)
1237 || skb_queue_len(&local->skb_queue_unreliable))
1238 printk(KERN_WARNING "%s: skb_queue not empty\n",
dd1cd4c6 1239 wiphy_name(local->hw.wiphy));
f0706e82
JB
1240 skb_queue_purge(&local->skb_queue);
1241 skb_queue_purge(&local->skb_queue_unreliable);
1242
1243 destroy_workqueue(local->hw.workqueue);
1244 wiphy_unregister(local->hw.wiphy);
1245 ieee80211_wep_free(local);
1246 ieee80211_led_exit(local);
1247}
1248EXPORT_SYMBOL(ieee80211_unregister_hw);
1249
1250void ieee80211_free_hw(struct ieee80211_hw *hw)
1251{
1252 struct ieee80211_local *local = hw_to_local(hw);
1253
1254 ieee80211_if_free(local->mdev);
1255 wiphy_free(local->hw.wiphy);
1256}
1257EXPORT_SYMBOL(ieee80211_free_hw);
1258
f0706e82
JB
1259static int __init ieee80211_init(void)
1260{
1261 struct sk_buff *skb;
1262 int ret;
1263
1264 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1265
ac71c691
JB
1266#ifdef CONFIG_MAC80211_RCSIMPLE
1267 ret = ieee80211_rate_control_register(&mac80211_rcsimple);
1268 if (ret)
1269 return ret;
1270#endif
1271
f0706e82
JB
1272 ret = ieee80211_wme_register();
1273 if (ret) {
ac71c691
JB
1274#ifdef CONFIG_MAC80211_RCSIMPLE
1275 ieee80211_rate_control_unregister(&mac80211_rcsimple);
1276#endif
f0706e82
JB
1277 printk(KERN_DEBUG "ieee80211_init: failed to "
1278 "initialize WME (err=%d)\n", ret);
1279 return ret;
1280 }
1281
e9f207f0 1282 ieee80211_debugfs_netdev_init();
fd8bacc9 1283 ieee80211_regdomain_init();
e9f207f0 1284
f0706e82
JB
1285 return 0;
1286}
1287
f0706e82
JB
1288static void __exit ieee80211_exit(void)
1289{
ac71c691
JB
1290#ifdef CONFIG_MAC80211_RCSIMPLE
1291 ieee80211_rate_control_unregister(&mac80211_rcsimple);
1292#endif
1293
f0706e82 1294 ieee80211_wme_unregister();
e9f207f0 1295 ieee80211_debugfs_netdev_exit();
f0706e82
JB
1296}
1297
1298
ca9938fe 1299subsys_initcall(ieee80211_init);
f0706e82
JB
1300module_exit(ieee80211_exit);
1301
1302MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1303MODULE_LICENSE("GPL");