]> err.no Git - linux-2.6/blob - net/mac80211/ieee80211.c
mac80211: fix mesh_path and sta_info get_by_idx functions
[linux-2.6] / net / mac80211 / ieee80211.c
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2005-2006, Devicescape Software, Inc.
4  * Copyright 2006-2007  Jiri Benc <jbenc@suse.cz>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <net/mac80211.h>
12 #include <net/ieee80211_radiotap.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/netdevice.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18 #include <linux/skbuff.h>
19 #include <linux/etherdevice.h>
20 #include <linux/if_arp.h>
21 #include <linux/wireless.h>
22 #include <linux/rtnetlink.h>
23 #include <linux/bitmap.h>
24 #include <net/net_namespace.h>
25 #include <net/cfg80211.h>
26
27 #include "ieee80211_i.h"
28 #include "ieee80211_rate.h"
29 #include "mesh.h"
30 #include "wep.h"
31 #include "wme.h"
32 #include "aes_ccm.h"
33 #include "ieee80211_led.h"
34 #include "cfg.h"
35 #include "debugfs.h"
36 #include "debugfs_netdev.h"
37
38 #define SUPP_MCS_SET_LEN 16
39
40 /*
41  * For seeing transmitted packets on monitor interfaces
42  * we have a radiotap header too.
43  */
44 struct ieee80211_tx_status_rtap_hdr {
45         struct ieee80211_radiotap_header hdr;
46         __le16 tx_flags;
47         u8 data_retries;
48 } __attribute__ ((packed));
49
50 /* common interface routines */
51
52 static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
53 {
54         memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
55         return ETH_ALEN;
56 }
57
58 /* must be called under mdev tx lock */
59 static void ieee80211_configure_filter(struct ieee80211_local *local)
60 {
61         unsigned int changed_flags;
62         unsigned int new_flags = 0;
63
64         if (atomic_read(&local->iff_promiscs))
65                 new_flags |= FIF_PROMISC_IN_BSS;
66
67         if (atomic_read(&local->iff_allmultis))
68                 new_flags |= FIF_ALLMULTI;
69
70         if (local->monitors)
71                 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;
84
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
100 /* master interface */
101
102 static 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
108         /* we hold the RTNL here so can safely walk the list */
109         list_for_each_entry(sdata, &local->interfaces, list) {
110                 if (sdata->dev != dev && netif_running(sdata->dev)) {
111                         res = 0;
112                         break;
113                 }
114         }
115         return res;
116 }
117
118 static 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
123         /* we hold the RTNL here so can safely walk the list */
124         list_for_each_entry(sdata, &local->interfaces, list)
125                 if (sdata->dev != dev && netif_running(sdata->dev))
126                         dev_close(sdata->dev);
127
128         return 0;
129 }
130
131 static 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
138 /* regular interfaces */
139
140 static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
141 {
142         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
147         /* FIX: what would be proper limits for MTU?
148          * This interface uses 802.3 frames. */
149         if (new_mtu < 256 ||
150                 new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6 - meshhdrlen) {
151                 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
163 static 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
179 static 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;
185         bool need_hw_reconfig = 0;
186         struct sta_info *sta;
187
188         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
189
190         /* we hold the RTNL here so can safely walk the list */
191         list_for_each_entry(nsdata, &local->interfaces, list) {
192                 struct net_device *ndev = nsdata->dev;
193
194                 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
240                         /*
241                          * check whether it may have the same address
242                          */
243                         if (!identical_mac_addr_allowed(sdata->vif.type,
244                                                         nsdata->vif.type))
245                                 return -ENOTUNIQ;
246
247                         /*
248                          * can only add VLANs to enabled APs
249                          */
250                         if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN &&
251                             nsdata->vif.type == IEEE80211_IF_TYPE_AP)
252                                 sdata->u.vlan.ap = nsdata;
253                 }
254         }
255
256         switch (sdata->vif.type) {
257         case IEEE80211_IF_TYPE_WDS:
258                 if (is_zero_ether_addr(sdata->u.wds.remote_addr))
259                         return -ENOLINK;
260
261                 /* Create STA entry for the WDS peer */
262                 sta = sta_info_alloc(sdata, sdata->u.wds.remote_addr,
263                                      GFP_KERNEL);
264                 if (!sta)
265                         return -ENOMEM;
266
267                 sta->flags |= WLAN_STA_AUTHORIZED;
268
269                 res = sta_info_insert(sta);
270                 if (res) {
271                         sta_info_destroy(sta);
272                         return res;
273                 }
274                 break;
275         case IEEE80211_IF_TYPE_VLAN:
276                 if (!sdata->u.vlan.ap)
277                         return -ENOLINK;
278                 break;
279         case IEEE80211_IF_TYPE_AP:
280         case IEEE80211_IF_TYPE_STA:
281         case IEEE80211_IF_TYPE_MNTR:
282         case IEEE80211_IF_TYPE_IBSS:
283         case IEEE80211_IF_TYPE_MESH_POINT:
284                 /* no special treatment */
285                 break;
286         case IEEE80211_IF_TYPE_INVALID:
287                 /* cannot happen */
288                 WARN_ON(1);
289                 break;
290         }
291
292         if (local->open_count == 0) {
293                 res = 0;
294                 if (local->ops->start)
295                         res = local->ops->start(local_to_hw(local));
296                 if (res)
297                         return res;
298                 need_hw_reconfig = 1;
299                 ieee80211_led_radio(local, local->hw.conf.radio_enabled);
300         }
301
302         switch (sdata->vif.type) {
303         case IEEE80211_IF_TYPE_VLAN:
304                 list_add(&sdata->u.vlan.list, &sdata->u.vlan.ap->u.ap.vlans);
305                 /* no need to tell driver */
306                 break;
307         case IEEE80211_IF_TYPE_MNTR:
308                 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
309                         local->cooked_mntrs++;
310                         break;
311                 }
312
313                 /* must be before the call to ieee80211_configure_filter */
314                 local->monitors++;
315                 if (local->monitors == 1)
316                         local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
317
318                 if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
319                         local->fif_fcsfail++;
320                 if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
321                         local->fif_plcpfail++;
322                 if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
323                         local->fif_control++;
324                 if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
325                         local->fif_other_bss++;
326
327                 netif_tx_lock_bh(local->mdev);
328                 ieee80211_configure_filter(local);
329                 netif_tx_unlock_bh(local->mdev);
330                 break;
331         case IEEE80211_IF_TYPE_STA:
332         case IEEE80211_IF_TYPE_IBSS:
333                 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
334                 /* fall through */
335         default:
336                 conf.vif = &sdata->vif;
337                 conf.type = sdata->vif.type;
338                 conf.mac_addr = dev->dev_addr;
339                 res = local->ops->add_interface(local_to_hw(local), &conf);
340                 if (res && !local->open_count && local->ops->stop)
341                         local->ops->stop(local_to_hw(local));
342                 if (res)
343                         return res;
344
345                 ieee80211_if_config(dev);
346                 ieee80211_reset_erp_info(dev);
347                 ieee80211_enable_keys(sdata);
348
349                 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
350                     !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
351                         netif_carrier_off(dev);
352                 else
353                         netif_carrier_on(dev);
354         }
355
356         if (local->open_count == 0) {
357                 res = dev_open(local->mdev);
358                 WARN_ON(res);
359                 tasklet_enable(&local->tx_pending_tasklet);
360                 tasklet_enable(&local->tasklet);
361         }
362
363         /*
364          * set_multicast_list will be invoked by the networking core
365          * which will check whether any increments here were done in
366          * error and sync them down to the hardware as filter flags.
367          */
368         if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
369                 atomic_inc(&local->iff_allmultis);
370
371         if (sdata->flags & IEEE80211_SDATA_PROMISC)
372                 atomic_inc(&local->iff_promiscs);
373
374         local->open_count++;
375         if (need_hw_reconfig)
376                 ieee80211_hw_config(local);
377
378         netif_start_queue(dev);
379
380         return 0;
381 }
382
383 static int ieee80211_stop(struct net_device *dev)
384 {
385         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
386         struct ieee80211_local *local = sdata->local;
387         struct ieee80211_if_init_conf conf;
388         struct sta_info *sta;
389         int i;
390
391         /*
392          * Stop TX on this interface first.
393          */
394         netif_stop_queue(dev);
395
396         /*
397          * Now delete all active aggregation sessions.
398          */
399         rcu_read_lock();
400
401         list_for_each_entry_rcu(sta, &local->sta_list, list) {
402                 if (sta->sdata == sdata)
403                         for (i = 0; i <  STA_TID_NUM; i++)
404                                 ieee80211_sta_stop_rx_ba_session(sdata->dev,
405                                                 sta->addr, i,
406                                                 WLAN_BACK_RECIPIENT,
407                                                 WLAN_REASON_QSTA_LEAVE_QBSS);
408         }
409
410         rcu_read_unlock();
411
412         /*
413          * Remove all stations associated with this interface.
414          *
415          * This must be done before calling ops->remove_interface()
416          * because otherwise we can later invoke ops->sta_notify()
417          * whenever the STAs are removed, and that invalidates driver
418          * assumptions about always getting a vif pointer that is valid
419          * (because if we remove a STA after ops->remove_interface()
420          * the driver will have removed the vif info already!)
421          *
422          * We could relax this and only unlink the stations from the
423          * hash table and list but keep them on a per-sdata list that
424          * will be inserted back again when the interface is brought
425          * up again, but I don't currently see a use case for that,
426          * except with WDS which gets a STA entry created when it is
427          * brought up.
428          */
429         sta_info_flush(local, sdata);
430
431         /*
432          * Don't count this interface for promisc/allmulti while it
433          * is down. dev_mc_unsync() will invoke set_multicast_list
434          * on the master interface which will sync these down to the
435          * hardware as filter flags.
436          */
437         if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
438                 atomic_dec(&local->iff_allmultis);
439
440         if (sdata->flags & IEEE80211_SDATA_PROMISC)
441                 atomic_dec(&local->iff_promiscs);
442
443         dev_mc_unsync(local->mdev, dev);
444
445         /* APs need special treatment */
446         if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
447                 struct ieee80211_sub_if_data *vlan, *tmp;
448                 struct beacon_data *old_beacon = sdata->u.ap.beacon;
449
450                 /* remove beacon */
451                 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
452                 synchronize_rcu();
453                 kfree(old_beacon);
454
455                 /* down all dependent devices, that is VLANs */
456                 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
457                                          u.vlan.list)
458                         dev_close(vlan->dev);
459                 WARN_ON(!list_empty(&sdata->u.ap.vlans));
460         }
461
462         local->open_count--;
463
464         switch (sdata->vif.type) {
465         case IEEE80211_IF_TYPE_VLAN:
466                 list_del(&sdata->u.vlan.list);
467                 sdata->u.vlan.ap = NULL;
468                 /* no need to tell driver */
469                 break;
470         case IEEE80211_IF_TYPE_MNTR:
471                 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
472                         local->cooked_mntrs--;
473                         break;
474                 }
475
476                 local->monitors--;
477                 if (local->monitors == 0)
478                         local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
479
480                 if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
481                         local->fif_fcsfail--;
482                 if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
483                         local->fif_plcpfail--;
484                 if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
485                         local->fif_control--;
486                 if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
487                         local->fif_other_bss--;
488
489                 netif_tx_lock_bh(local->mdev);
490                 ieee80211_configure_filter(local);
491                 netif_tx_unlock_bh(local->mdev);
492                 break;
493         case IEEE80211_IF_TYPE_MESH_POINT:
494         case IEEE80211_IF_TYPE_STA:
495         case IEEE80211_IF_TYPE_IBSS:
496                 sdata->u.sta.state = IEEE80211_DISABLED;
497                 del_timer_sync(&sdata->u.sta.timer);
498                 /*
499                  * When we get here, the interface is marked down.
500                  * Call synchronize_rcu() to wait for the RX path
501                  * should it be using the interface and enqueuing
502                  * frames at this very time on another CPU.
503                  */
504                 synchronize_rcu();
505                 skb_queue_purge(&sdata->u.sta.skb_queue);
506
507                 if (local->scan_dev == sdata->dev) {
508                         if (!local->ops->hw_scan) {
509                                 local->sta_sw_scanning = 0;
510                                 cancel_delayed_work(&local->scan_work);
511                         } else
512                                 local->sta_hw_scanning = 0;
513                 }
514
515                 flush_workqueue(local->hw.workqueue);
516
517                 sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
518                 kfree(sdata->u.sta.extra_ie);
519                 sdata->u.sta.extra_ie = NULL;
520                 sdata->u.sta.extra_ie_len = 0;
521                 /* fall through */
522         default:
523                 conf.vif = &sdata->vif;
524                 conf.type = sdata->vif.type;
525                 conf.mac_addr = dev->dev_addr;
526                 /* disable all keys for as long as this netdev is down */
527                 ieee80211_disable_keys(sdata);
528                 local->ops->remove_interface(local_to_hw(local), &conf);
529         }
530
531         if (local->open_count == 0) {
532                 if (netif_running(local->mdev))
533                         dev_close(local->mdev);
534
535                 if (local->ops->stop)
536                         local->ops->stop(local_to_hw(local));
537
538                 ieee80211_led_radio(local, 0);
539
540                 tasklet_disable(&local->tx_pending_tasklet);
541                 tasklet_disable(&local->tasklet);
542         }
543
544         return 0;
545 }
546
547 int ieee80211_start_tx_ba_session(struct ieee80211_hw *hw, u8 *ra, u16 tid)
548 {
549         struct ieee80211_local *local = hw_to_local(hw);
550         struct sta_info *sta;
551         struct ieee80211_sub_if_data *sdata;
552         u16 start_seq_num = 0;
553         u8 *state;
554         int ret;
555         DECLARE_MAC_BUF(mac);
556
557         if (tid >= STA_TID_NUM)
558                 return -EINVAL;
559
560 #ifdef CONFIG_MAC80211_HT_DEBUG
561         printk(KERN_DEBUG "Open BA session requested for %s tid %u\n",
562                                 print_mac(mac, ra), tid);
563 #endif /* CONFIG_MAC80211_HT_DEBUG */
564
565         rcu_read_lock();
566
567         sta = sta_info_get(local, ra);
568         if (!sta) {
569                 printk(KERN_DEBUG "Could not find the station\n");
570                 rcu_read_unlock();
571                 return -ENOENT;
572         }
573
574         spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
575
576         /* we have tried too many times, receiver does not want A-MPDU */
577         if (sta->ampdu_mlme.tid_tx[tid].addba_req_num > HT_AGG_MAX_RETRIES) {
578                 ret = -EBUSY;
579                 goto start_ba_exit;
580         }
581
582         state = &sta->ampdu_mlme.tid_tx[tid].state;
583         /* check if the TID is not in aggregation flow already */
584         if (*state != HT_AGG_STATE_IDLE) {
585 #ifdef CONFIG_MAC80211_HT_DEBUG
586                 printk(KERN_DEBUG "BA request denied - session is not "
587                                  "idle on tid %u\n", tid);
588 #endif /* CONFIG_MAC80211_HT_DEBUG */
589                 ret = -EAGAIN;
590                 goto start_ba_exit;
591         }
592
593         /* ensure that TX flow won't interrupt us
594          * until the end of the call to requeue function */
595         spin_lock_bh(&local->mdev->queue_lock);
596
597         /* create a new queue for this aggregation */
598         ret = ieee80211_ht_agg_queue_add(local, sta, tid);
599
600         /* case no queue is available to aggregation
601          * don't switch to aggregation */
602         if (ret) {
603 #ifdef CONFIG_MAC80211_HT_DEBUG
604                 printk(KERN_DEBUG "BA request denied - no queue available for"
605                                         " tid %d\n", tid);
606 #endif /* CONFIG_MAC80211_HT_DEBUG */
607                 spin_unlock_bh(&local->mdev->queue_lock);
608                 goto start_ba_exit;
609         }
610         sdata = sta->sdata;
611
612         /* Ok, the Addba frame hasn't been sent yet, but if the driver calls the
613          * call back right away, it must see that the flow has begun */
614         *state |= HT_ADDBA_REQUESTED_MSK;
615
616         if (local->ops->ampdu_action)
617                 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_START,
618                                                 ra, tid, &start_seq_num);
619
620         if (ret) {
621                 /* No need to requeue the packets in the agg queue, since we
622                  * held the tx lock: no packet could be enqueued to the newly
623                  * allocated queue */
624                  ieee80211_ht_agg_queue_remove(local, sta, tid, 0);
625 #ifdef CONFIG_MAC80211_HT_DEBUG
626                 printk(KERN_DEBUG "BA request denied - HW or queue unavailable"
627                                 " for tid %d\n", tid);
628 #endif /* CONFIG_MAC80211_HT_DEBUG */
629                 spin_unlock_bh(&local->mdev->queue_lock);
630                 *state = HT_AGG_STATE_IDLE;
631                 goto start_ba_exit;
632         }
633
634         /* Will put all the packets in the new SW queue */
635         ieee80211_requeue(local, ieee802_1d_to_ac[tid]);
636         spin_unlock_bh(&local->mdev->queue_lock);
637
638         /* We have most probably almost emptied the legacy queue */
639         /* ieee80211_wake_queue(local_to_hw(local), ieee802_1d_to_ac[tid]); */
640
641         /* send an addBA request */
642         sta->ampdu_mlme.dialog_token_allocator++;
643         sta->ampdu_mlme.tid_tx[tid].dialog_token =
644                         sta->ampdu_mlme.dialog_token_allocator;
645         sta->ampdu_mlme.tid_tx[tid].ssn = start_seq_num;
646
647         ieee80211_send_addba_request(sta->sdata->dev, ra, tid,
648                          sta->ampdu_mlme.tid_tx[tid].dialog_token,
649                          sta->ampdu_mlme.tid_tx[tid].ssn,
650                          0x40, 5000);
651
652         /* activate the timer for the recipient's addBA response */
653         sta->ampdu_mlme.tid_tx[tid].addba_resp_timer.expires =
654                                 jiffies + ADDBA_RESP_INTERVAL;
655         add_timer(&sta->ampdu_mlme.tid_tx[tid].addba_resp_timer);
656         printk(KERN_DEBUG "activated addBA response timer on tid %d\n", tid);
657
658 start_ba_exit:
659         spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
660         rcu_read_unlock();
661         return ret;
662 }
663 EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
664
665 int ieee80211_stop_tx_ba_session(struct ieee80211_hw *hw,
666                                  u8 *ra, u16 tid,
667                                  enum ieee80211_back_parties initiator)
668 {
669         struct ieee80211_local *local = hw_to_local(hw);
670         struct sta_info *sta;
671         u8 *state;
672         int ret = 0;
673         DECLARE_MAC_BUF(mac);
674
675         if (tid >= STA_TID_NUM)
676                 return -EINVAL;
677
678 #ifdef CONFIG_MAC80211_HT_DEBUG
679         printk(KERN_DEBUG "Stop a BA session requested for %s tid %u\n",
680                                 print_mac(mac, ra), tid);
681 #endif /* CONFIG_MAC80211_HT_DEBUG */
682
683         rcu_read_lock();
684         sta = sta_info_get(local, ra);
685         if (!sta) {
686                 rcu_read_unlock();
687                 return -ENOENT;
688         }
689
690         /* check if the TID is in aggregation */
691         state = &sta->ampdu_mlme.tid_tx[tid].state;
692         spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
693
694         if (*state != HT_AGG_STATE_OPERATIONAL) {
695 #ifdef CONFIG_MAC80211_HT_DEBUG
696                 printk(KERN_DEBUG "Try to stop Tx aggregation on"
697                                 " non active TID\n");
698 #endif /* CONFIG_MAC80211_HT_DEBUG */
699                 ret = -ENOENT;
700                 goto stop_BA_exit;
701         }
702
703         ieee80211_stop_queue(hw, sta->tid_to_tx_q[tid]);
704
705         *state = HT_AGG_STATE_REQ_STOP_BA_MSK |
706                 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
707
708         if (local->ops->ampdu_action)
709                 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_STOP,
710                                                 ra, tid, NULL);
711
712         /* case HW denied going back to legacy */
713         if (ret) {
714                 WARN_ON(ret != -EBUSY);
715                 *state = HT_AGG_STATE_OPERATIONAL;
716                 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
717                 goto stop_BA_exit;
718         }
719
720 stop_BA_exit:
721         spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
722         rcu_read_unlock();
723         return ret;
724 }
725 EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
726
727 void ieee80211_start_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u16 tid)
728 {
729         struct ieee80211_local *local = hw_to_local(hw);
730         struct sta_info *sta;
731         u8 *state;
732         DECLARE_MAC_BUF(mac);
733
734         if (tid >= STA_TID_NUM) {
735                 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
736                                 tid, STA_TID_NUM);
737                 return;
738         }
739
740         rcu_read_lock();
741         sta = sta_info_get(local, ra);
742         if (!sta) {
743                 rcu_read_unlock();
744                 printk(KERN_DEBUG "Could not find station: %s\n",
745                                 print_mac(mac, ra));
746                 return;
747         }
748
749         state = &sta->ampdu_mlme.tid_tx[tid].state;
750         spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
751
752         if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
753                 printk(KERN_DEBUG "addBA was not requested yet, state is %d\n",
754                                 *state);
755                 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
756                 rcu_read_unlock();
757                 return;
758         }
759
760         WARN_ON_ONCE(*state & HT_ADDBA_DRV_READY_MSK);
761
762         *state |= HT_ADDBA_DRV_READY_MSK;
763
764         if (*state == HT_AGG_STATE_OPERATIONAL) {
765                 printk(KERN_DEBUG "Aggregation is on for tid %d \n", tid);
766                 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
767         }
768         spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
769         rcu_read_unlock();
770 }
771 EXPORT_SYMBOL(ieee80211_start_tx_ba_cb);
772
773 void ieee80211_stop_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u8 tid)
774 {
775         struct ieee80211_local *local = hw_to_local(hw);
776         struct sta_info *sta;
777         u8 *state;
778         int agg_queue;
779         DECLARE_MAC_BUF(mac);
780
781         if (tid >= STA_TID_NUM) {
782                 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
783                                 tid, STA_TID_NUM);
784                 return;
785         }
786
787         printk(KERN_DEBUG "Stop a BA session requested on DA %s tid %d\n",
788                                 print_mac(mac, ra), tid);
789
790         rcu_read_lock();
791         sta = sta_info_get(local, ra);
792         if (!sta) {
793                 printk(KERN_DEBUG "Could not find station: %s\n",
794                                 print_mac(mac, ra));
795                 rcu_read_unlock();
796                 return;
797         }
798         state = &sta->ampdu_mlme.tid_tx[tid].state;
799
800         spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
801         if ((*state & HT_AGG_STATE_REQ_STOP_BA_MSK) == 0) {
802                 printk(KERN_DEBUG "unexpected callback to A-MPDU stop\n");
803                 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
804                 rcu_read_unlock();
805                 return;
806         }
807
808         if (*state & HT_AGG_STATE_INITIATOR_MSK)
809                 ieee80211_send_delba(sta->sdata->dev, ra, tid,
810                         WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
811
812         agg_queue = sta->tid_to_tx_q[tid];
813
814         /* avoid ordering issues: we are the only one that can modify
815          * the content of the qdiscs */
816         spin_lock_bh(&local->mdev->queue_lock);
817         /* remove the queue for this aggregation */
818         ieee80211_ht_agg_queue_remove(local, sta, tid, 1);
819         spin_unlock_bh(&local->mdev->queue_lock);
820
821         /* we just requeued the all the frames that were in the removed
822          * queue, and since we might miss a softirq we do netif_schedule.
823          * ieee80211_wake_queue is not used here as this queue is not
824          * necessarily stopped */
825         netif_schedule(local->mdev);
826         *state = HT_AGG_STATE_IDLE;
827         sta->ampdu_mlme.tid_tx[tid].addba_req_num = 0;
828         spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
829
830         rcu_read_unlock();
831 }
832 EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb);
833
834 void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
835                                       const u8 *ra, u16 tid)
836 {
837         struct ieee80211_local *local = hw_to_local(hw);
838         struct ieee80211_ra_tid *ra_tid;
839         struct sk_buff *skb = dev_alloc_skb(0);
840
841         if (unlikely(!skb)) {
842                 if (net_ratelimit())
843                         printk(KERN_WARNING "%s: Not enough memory, "
844                                "dropping start BA session", skb->dev->name);
845                 return;
846         }
847         ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
848         memcpy(&ra_tid->ra, ra, ETH_ALEN);
849         ra_tid->tid = tid;
850
851         skb->pkt_type = IEEE80211_ADDBA_MSG;
852         skb_queue_tail(&local->skb_queue, skb);
853         tasklet_schedule(&local->tasklet);
854 }
855 EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
856
857 void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
858                                      const u8 *ra, u16 tid)
859 {
860         struct ieee80211_local *local = hw_to_local(hw);
861         struct ieee80211_ra_tid *ra_tid;
862         struct sk_buff *skb = dev_alloc_skb(0);
863
864         if (unlikely(!skb)) {
865                 if (net_ratelimit())
866                         printk(KERN_WARNING "%s: Not enough memory, "
867                                "dropping stop BA session", skb->dev->name);
868                 return;
869         }
870         ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
871         memcpy(&ra_tid->ra, ra, ETH_ALEN);
872         ra_tid->tid = tid;
873
874         skb->pkt_type = IEEE80211_DELBA_MSG;
875         skb_queue_tail(&local->skb_queue, skb);
876         tasklet_schedule(&local->tasklet);
877 }
878 EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
879
880 static void ieee80211_set_multicast_list(struct net_device *dev)
881 {
882         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
883         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
884         int allmulti, promisc, sdata_allmulti, sdata_promisc;
885
886         allmulti = !!(dev->flags & IFF_ALLMULTI);
887         promisc = !!(dev->flags & IFF_PROMISC);
888         sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
889         sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
890
891         if (allmulti != sdata_allmulti) {
892                 if (dev->flags & IFF_ALLMULTI)
893                         atomic_inc(&local->iff_allmultis);
894                 else
895                         atomic_dec(&local->iff_allmultis);
896                 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
897         }
898
899         if (promisc != sdata_promisc) {
900                 if (dev->flags & IFF_PROMISC)
901                         atomic_inc(&local->iff_promiscs);
902                 else
903                         atomic_dec(&local->iff_promiscs);
904                 sdata->flags ^= IEEE80211_SDATA_PROMISC;
905         }
906
907         dev_mc_sync(local->mdev, dev);
908 }
909
910 static const struct header_ops ieee80211_header_ops = {
911         .create         = eth_header,
912         .parse          = header_parse_80211,
913         .rebuild        = eth_rebuild_header,
914         .cache          = eth_header_cache,
915         .cache_update   = eth_header_cache_update,
916 };
917
918 /* Must not be called for mdev */
919 void ieee80211_if_setup(struct net_device *dev)
920 {
921         ether_setup(dev);
922         dev->hard_start_xmit = ieee80211_subif_start_xmit;
923         dev->wireless_handlers = &ieee80211_iw_handler_def;
924         dev->set_multicast_list = ieee80211_set_multicast_list;
925         dev->change_mtu = ieee80211_change_mtu;
926         dev->open = ieee80211_open;
927         dev->stop = ieee80211_stop;
928         dev->destructor = ieee80211_if_free;
929 }
930
931 /* everything else */
932
933 static int __ieee80211_if_config(struct net_device *dev,
934                                  struct sk_buff *beacon,
935                                  struct ieee80211_tx_control *control)
936 {
937         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
938         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
939         struct ieee80211_if_conf conf;
940
941         if (!local->ops->config_interface || !netif_running(dev))
942                 return 0;
943
944         memset(&conf, 0, sizeof(conf));
945         conf.type = sdata->vif.type;
946         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
947             sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
948                 conf.bssid = sdata->u.sta.bssid;
949                 conf.ssid = sdata->u.sta.ssid;
950                 conf.ssid_len = sdata->u.sta.ssid_len;
951         } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
952                 conf.beacon = beacon;
953                 ieee80211_start_mesh(dev);
954         } else if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
955                 conf.ssid = sdata->u.ap.ssid;
956                 conf.ssid_len = sdata->u.ap.ssid_len;
957                 conf.beacon = beacon;
958                 conf.beacon_control = control;
959         }
960         return local->ops->config_interface(local_to_hw(local),
961                                             &sdata->vif, &conf);
962 }
963
964 int ieee80211_if_config(struct net_device *dev)
965 {
966         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
967         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
968         if (sdata->vif.type == IEEE80211_IF_TYPE_MESH_POINT &&
969             (local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
970                 return ieee80211_if_config_beacon(dev);
971         return __ieee80211_if_config(dev, NULL, NULL);
972 }
973
974 int ieee80211_if_config_beacon(struct net_device *dev)
975 {
976         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
977         struct ieee80211_tx_control control;
978         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
979         struct sk_buff *skb;
980
981         if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
982                 return 0;
983         skb = ieee80211_beacon_get(local_to_hw(local), &sdata->vif,
984                                    &control);
985         if (!skb)
986                 return -ENOMEM;
987         return __ieee80211_if_config(dev, skb, &control);
988 }
989
990 int ieee80211_hw_config(struct ieee80211_local *local)
991 {
992         struct ieee80211_channel *chan;
993         int ret = 0;
994
995         if (local->sta_sw_scanning)
996                 chan = local->scan_channel;
997         else
998                 chan = local->oper_channel;
999
1000         local->hw.conf.channel = chan;
1001
1002         if (!local->hw.conf.power_level)
1003                 local->hw.conf.power_level = chan->max_power;
1004         else
1005                 local->hw.conf.power_level = min(chan->max_power,
1006                                                local->hw.conf.power_level);
1007
1008         local->hw.conf.max_antenna_gain = chan->max_antenna_gain;
1009
1010 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
1011         printk(KERN_DEBUG "%s: HW CONFIG: freq=%d\n",
1012                wiphy_name(local->hw.wiphy), chan->center_freq);
1013 #endif
1014
1015         if (local->open_count)
1016                 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
1017
1018         return ret;
1019 }
1020
1021 /**
1022  * ieee80211_hw_config_ht should be used only after legacy configuration
1023  * has been determined, as ht configuration depends upon the hardware's
1024  * HT abilities for a _specific_ band.
1025  */
1026 int ieee80211_hw_config_ht(struct ieee80211_local *local, int enable_ht,
1027                            struct ieee80211_ht_info *req_ht_cap,
1028                            struct ieee80211_ht_bss_info *req_bss_cap)
1029 {
1030         struct ieee80211_conf *conf = &local->hw.conf;
1031         struct ieee80211_supported_band *sband;
1032         int i;
1033
1034         sband = local->hw.wiphy->bands[conf->channel->band];
1035
1036         /* HT is not supported */
1037         if (!sband->ht_info.ht_supported) {
1038                 conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
1039                 return -EOPNOTSUPP;
1040         }
1041
1042         /* disable HT */
1043         if (!enable_ht) {
1044                 conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
1045         } else {
1046                 conf->flags |= IEEE80211_CONF_SUPPORT_HT_MODE;
1047                 conf->ht_conf.cap = req_ht_cap->cap & sband->ht_info.cap;
1048                 conf->ht_conf.cap &= ~(IEEE80211_HT_CAP_MIMO_PS);
1049                 conf->ht_conf.cap |=
1050                         sband->ht_info.cap & IEEE80211_HT_CAP_MIMO_PS;
1051                 conf->ht_bss_conf.primary_channel =
1052                         req_bss_cap->primary_channel;
1053                 conf->ht_bss_conf.bss_cap = req_bss_cap->bss_cap;
1054                 conf->ht_bss_conf.bss_op_mode = req_bss_cap->bss_op_mode;
1055                 for (i = 0; i < SUPP_MCS_SET_LEN; i++)
1056                         conf->ht_conf.supp_mcs_set[i] =
1057                                 sband->ht_info.supp_mcs_set[i] &
1058                                   req_ht_cap->supp_mcs_set[i];
1059
1060                 /* In STA mode, this gives us indication
1061                  * to the AP's mode of operation */
1062                 conf->ht_conf.ht_supported = 1;
1063                 conf->ht_conf.ampdu_factor = req_ht_cap->ampdu_factor;
1064                 conf->ht_conf.ampdu_density = req_ht_cap->ampdu_density;
1065         }
1066
1067         local->ops->conf_ht(local_to_hw(local), &local->hw.conf);
1068
1069         return 0;
1070 }
1071
1072 void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
1073                                       u32 changed)
1074 {
1075         struct ieee80211_local *local = sdata->local;
1076
1077         if (!changed)
1078                 return;
1079
1080         if (local->ops->bss_info_changed)
1081                 local->ops->bss_info_changed(local_to_hw(local),
1082                                              &sdata->vif,
1083                                              &sdata->bss_conf,
1084                                              changed);
1085 }
1086
1087 void ieee80211_reset_erp_info(struct net_device *dev)
1088 {
1089         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1090
1091         sdata->bss_conf.use_cts_prot = 0;
1092         sdata->bss_conf.use_short_preamble = 0;
1093         ieee80211_bss_info_change_notify(sdata,
1094                                          BSS_CHANGED_ERP_CTS_PROT |
1095                                          BSS_CHANGED_ERP_PREAMBLE);
1096 }
1097
1098 void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
1099                                  struct sk_buff *skb,
1100                                  struct ieee80211_tx_status *status)
1101 {
1102         struct ieee80211_local *local = hw_to_local(hw);
1103         struct ieee80211_tx_status *saved;
1104         int tmp;
1105
1106         skb->dev = local->mdev;
1107         saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
1108         if (unlikely(!saved)) {
1109                 if (net_ratelimit())
1110                         printk(KERN_WARNING "%s: Not enough memory, "
1111                                "dropping tx status", skb->dev->name);
1112                 /* should be dev_kfree_skb_irq, but due to this function being
1113                  * named _irqsafe instead of just _irq we can't be sure that
1114                  * people won't call it from non-irq contexts */
1115                 dev_kfree_skb_any(skb);
1116                 return;
1117         }
1118         memcpy(saved, status, sizeof(struct ieee80211_tx_status));
1119         /* copy pointer to saved status into skb->cb for use by tasklet */
1120         memcpy(skb->cb, &saved, sizeof(saved));
1121
1122         skb->pkt_type = IEEE80211_TX_STATUS_MSG;
1123         skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
1124                        &local->skb_queue : &local->skb_queue_unreliable, skb);
1125         tmp = skb_queue_len(&local->skb_queue) +
1126                 skb_queue_len(&local->skb_queue_unreliable);
1127         while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
1128                (skb = skb_dequeue(&local->skb_queue_unreliable))) {
1129                 memcpy(&saved, skb->cb, sizeof(saved));
1130                 kfree(saved);
1131                 dev_kfree_skb_irq(skb);
1132                 tmp--;
1133                 I802_DEBUG_INC(local->tx_status_drop);
1134         }
1135         tasklet_schedule(&local->tasklet);
1136 }
1137 EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
1138
1139 static void ieee80211_tasklet_handler(unsigned long data)
1140 {
1141         struct ieee80211_local *local = (struct ieee80211_local *) data;
1142         struct sk_buff *skb;
1143         struct ieee80211_rx_status rx_status;
1144         struct ieee80211_tx_status *tx_status;
1145         struct ieee80211_ra_tid *ra_tid;
1146
1147         while ((skb = skb_dequeue(&local->skb_queue)) ||
1148                (skb = skb_dequeue(&local->skb_queue_unreliable))) {
1149                 switch (skb->pkt_type) {
1150                 case IEEE80211_RX_MSG:
1151                         /* status is in skb->cb */
1152                         memcpy(&rx_status, skb->cb, sizeof(rx_status));
1153                         /* Clear skb->pkt_type in order to not confuse kernel
1154                          * netstack. */
1155                         skb->pkt_type = 0;
1156                         __ieee80211_rx(local_to_hw(local), skb, &rx_status);
1157                         break;
1158                 case IEEE80211_TX_STATUS_MSG:
1159                         /* get pointer to saved status out of skb->cb */
1160                         memcpy(&tx_status, skb->cb, sizeof(tx_status));
1161                         skb->pkt_type = 0;
1162                         ieee80211_tx_status(local_to_hw(local),
1163                                             skb, tx_status);
1164                         kfree(tx_status);
1165                         break;
1166                 case IEEE80211_DELBA_MSG:
1167                         ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
1168                         ieee80211_stop_tx_ba_cb(local_to_hw(local),
1169                                                 ra_tid->ra, ra_tid->tid);
1170                         dev_kfree_skb(skb);
1171                         break;
1172                 case IEEE80211_ADDBA_MSG:
1173                         ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
1174                         ieee80211_start_tx_ba_cb(local_to_hw(local),
1175                                                  ra_tid->ra, ra_tid->tid);
1176                         dev_kfree_skb(skb);
1177                         break ;
1178                 default: /* should never get here! */
1179                         printk(KERN_ERR "%s: Unknown message type (%d)\n",
1180                                wiphy_name(local->hw.wiphy), skb->pkt_type);
1181                         dev_kfree_skb(skb);
1182                         break;
1183                 }
1184         }
1185 }
1186
1187 /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
1188  * make a prepared TX frame (one that has been given to hw) to look like brand
1189  * new IEEE 802.11 frame that is ready to go through TX processing again.
1190  * Also, tx_packet_data in cb is restored from tx_control. */
1191 static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
1192                                       struct ieee80211_key *key,
1193                                       struct sk_buff *skb,
1194                                       struct ieee80211_tx_control *control)
1195 {
1196         int hdrlen, iv_len, mic_len;
1197         struct ieee80211_tx_packet_data *pkt_data;
1198
1199         pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
1200         pkt_data->ifindex = vif_to_sdata(control->vif)->dev->ifindex;
1201         pkt_data->flags = 0;
1202         if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
1203                 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
1204         if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
1205                 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
1206         if (control->flags & IEEE80211_TXCTL_REQUEUE)
1207                 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
1208         if (control->flags & IEEE80211_TXCTL_EAPOL_FRAME)
1209                 pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
1210         pkt_data->queue = control->queue;
1211
1212         hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1213
1214         if (!key)
1215                 goto no_key;
1216
1217         switch (key->conf.alg) {
1218         case ALG_WEP:
1219                 iv_len = WEP_IV_LEN;
1220                 mic_len = WEP_ICV_LEN;
1221                 break;
1222         case ALG_TKIP:
1223                 iv_len = TKIP_IV_LEN;
1224                 mic_len = TKIP_ICV_LEN;
1225                 break;
1226         case ALG_CCMP:
1227                 iv_len = CCMP_HDR_LEN;
1228                 mic_len = CCMP_MIC_LEN;
1229                 break;
1230         default:
1231                 goto no_key;
1232         }
1233
1234         if (skb->len >= mic_len &&
1235             !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
1236                 skb_trim(skb, skb->len - mic_len);
1237         if (skb->len >= iv_len && skb->len > hdrlen) {
1238                 memmove(skb->data + iv_len, skb->data, hdrlen);
1239                 skb_pull(skb, iv_len);
1240         }
1241
1242 no_key:
1243         {
1244                 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1245                 u16 fc = le16_to_cpu(hdr->frame_control);
1246                 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
1247                         fc &= ~IEEE80211_STYPE_QOS_DATA;
1248                         hdr->frame_control = cpu_to_le16(fc);
1249                         memmove(skb->data + 2, skb->data, hdrlen - 2);
1250                         skb_pull(skb, 2);
1251                 }
1252         }
1253 }
1254
1255 static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
1256                                             struct sta_info *sta,
1257                                             struct sk_buff *skb,
1258                                             struct ieee80211_tx_status *status)
1259 {
1260         sta->tx_filtered_count++;
1261
1262         /*
1263          * Clear the TX filter mask for this STA when sending the next
1264          * packet. If the STA went to power save mode, this will happen
1265          * happen when it wakes up for the next time.
1266          */
1267         sta->flags |= WLAN_STA_CLEAR_PS_FILT;
1268
1269         /*
1270          * This code races in the following way:
1271          *
1272          *  (1) STA sends frame indicating it will go to sleep and does so
1273          *  (2) hardware/firmware adds STA to filter list, passes frame up
1274          *  (3) hardware/firmware processes TX fifo and suppresses a frame
1275          *  (4) we get TX status before having processed the frame and
1276          *      knowing that the STA has gone to sleep.
1277          *
1278          * This is actually quite unlikely even when both those events are
1279          * processed from interrupts coming in quickly after one another or
1280          * even at the same time because we queue both TX status events and
1281          * RX frames to be processed by a tasklet and process them in the
1282          * same order that they were received or TX status last. Hence, there
1283          * is no race as long as the frame RX is processed before the next TX
1284          * status, which drivers can ensure, see below.
1285          *
1286          * Note that this can only happen if the hardware or firmware can
1287          * actually add STAs to the filter list, if this is done by the
1288          * driver in response to set_tim() (which will only reduce the race
1289          * this whole filtering tries to solve, not completely solve it)
1290          * this situation cannot happen.
1291          *
1292          * To completely solve this race drivers need to make sure that they
1293          *  (a) don't mix the irq-safe/not irq-safe TX status/RX processing
1294          *      functions and
1295          *  (b) always process RX events before TX status events if ordering
1296          *      can be unknown, for example with different interrupt status
1297          *      bits.
1298          */
1299         if (sta->flags & WLAN_STA_PS &&
1300             skb_queue_len(&sta->tx_filtered) < STA_MAX_TX_BUFFER) {
1301                 ieee80211_remove_tx_extra(local, sta->key, skb,
1302                                           &status->control);
1303                 skb_queue_tail(&sta->tx_filtered, skb);
1304                 return;
1305         }
1306
1307         if (!(sta->flags & WLAN_STA_PS) &&
1308             !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
1309                 /* Software retry the packet once */
1310                 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
1311                 ieee80211_remove_tx_extra(local, sta->key, skb,
1312                                           &status->control);
1313                 dev_queue_xmit(skb);
1314                 return;
1315         }
1316
1317         if (net_ratelimit())
1318                 printk(KERN_DEBUG "%s: dropped TX filtered frame, "
1319                        "queue_len=%d PS=%d @%lu\n",
1320                        wiphy_name(local->hw.wiphy),
1321                        skb_queue_len(&sta->tx_filtered),
1322                        !!(sta->flags & WLAN_STA_PS), jiffies);
1323         dev_kfree_skb(skb);
1324 }
1325
1326 void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
1327                          struct ieee80211_tx_status *status)
1328 {
1329         struct sk_buff *skb2;
1330         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1331         struct ieee80211_local *local = hw_to_local(hw);
1332         u16 frag, type;
1333         struct ieee80211_tx_status_rtap_hdr *rthdr;
1334         struct ieee80211_sub_if_data *sdata;
1335         struct net_device *prev_dev = NULL;
1336
1337         if (!status) {
1338                 printk(KERN_ERR
1339                        "%s: ieee80211_tx_status called with NULL status\n",
1340                        wiphy_name(local->hw.wiphy));
1341                 dev_kfree_skb(skb);
1342                 return;
1343         }
1344
1345         rcu_read_lock();
1346
1347         if (status->excessive_retries) {
1348                 struct sta_info *sta;
1349                 sta = sta_info_get(local, hdr->addr1);
1350                 if (sta) {
1351                         if (sta->flags & WLAN_STA_PS) {
1352                                 /*
1353                                  * The STA is in power save mode, so assume
1354                                  * that this TX packet failed because of that.
1355                                  */
1356                                 status->excessive_retries = 0;
1357                                 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
1358                                 ieee80211_handle_filtered_frame(local, sta,
1359                                                                 skb, status);
1360                                 rcu_read_unlock();
1361                                 return;
1362                         }
1363                 }
1364         }
1365
1366         if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
1367                 struct sta_info *sta;
1368                 sta = sta_info_get(local, hdr->addr1);
1369                 if (sta) {
1370                         ieee80211_handle_filtered_frame(local, sta, skb,
1371                                                         status);
1372                         rcu_read_unlock();
1373                         return;
1374                 }
1375         } else
1376                 rate_control_tx_status(local->mdev, skb, status);
1377
1378         rcu_read_unlock();
1379
1380         ieee80211_led_tx(local, 0);
1381
1382         /* SNMP counters
1383          * Fragments are passed to low-level drivers as separate skbs, so these
1384          * are actually fragments, not frames. Update frame counters only for
1385          * the first fragment of the frame. */
1386
1387         frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
1388         type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
1389
1390         if (status->flags & IEEE80211_TX_STATUS_ACK) {
1391                 if (frag == 0) {
1392                         local->dot11TransmittedFrameCount++;
1393                         if (is_multicast_ether_addr(hdr->addr1))
1394                                 local->dot11MulticastTransmittedFrameCount++;
1395                         if (status->retry_count > 0)
1396                                 local->dot11RetryCount++;
1397                         if (status->retry_count > 1)
1398                                 local->dot11MultipleRetryCount++;
1399                 }
1400
1401                 /* This counter shall be incremented for an acknowledged MPDU
1402                  * with an individual address in the address 1 field or an MPDU
1403                  * with a multicast address in the address 1 field of type Data
1404                  * or Management. */
1405                 if (!is_multicast_ether_addr(hdr->addr1) ||
1406                     type == IEEE80211_FTYPE_DATA ||
1407                     type == IEEE80211_FTYPE_MGMT)
1408                         local->dot11TransmittedFragmentCount++;
1409         } else {
1410                 if (frag == 0)
1411                         local->dot11FailedCount++;
1412         }
1413
1414         /* this was a transmitted frame, but now we want to reuse it */
1415         skb_orphan(skb);
1416
1417         /*
1418          * This is a bit racy but we can avoid a lot of work
1419          * with this test...
1420          */
1421         if (!local->monitors && !local->cooked_mntrs) {
1422                 dev_kfree_skb(skb);
1423                 return;
1424         }
1425
1426         /* send frame to monitor interfaces now */
1427
1428         if (skb_headroom(skb) < sizeof(*rthdr)) {
1429                 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
1430                 dev_kfree_skb(skb);
1431                 return;
1432         }
1433
1434         rthdr = (struct ieee80211_tx_status_rtap_hdr*)
1435                                 skb_push(skb, sizeof(*rthdr));
1436
1437         memset(rthdr, 0, sizeof(*rthdr));
1438         rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
1439         rthdr->hdr.it_present =
1440                 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
1441                             (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
1442
1443         if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
1444             !is_multicast_ether_addr(hdr->addr1))
1445                 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
1446
1447         if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
1448             (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
1449                 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
1450         else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
1451                 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
1452
1453         rthdr->data_retries = status->retry_count;
1454
1455         /* XXX: is this sufficient for BPF? */
1456         skb_set_mac_header(skb, 0);
1457         skb->ip_summed = CHECKSUM_UNNECESSARY;
1458         skb->pkt_type = PACKET_OTHERHOST;
1459         skb->protocol = htons(ETH_P_802_2);
1460         memset(skb->cb, 0, sizeof(skb->cb));
1461
1462         rcu_read_lock();
1463         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
1464                 if (sdata->vif.type == IEEE80211_IF_TYPE_MNTR) {
1465                         if (!netif_running(sdata->dev))
1466                                 continue;
1467
1468                         if (prev_dev) {
1469                                 skb2 = skb_clone(skb, GFP_ATOMIC);
1470                                 if (skb2) {
1471                                         skb2->dev = prev_dev;
1472                                         netif_rx(skb2);
1473                                 }
1474                         }
1475
1476                         prev_dev = sdata->dev;
1477                 }
1478         }
1479         if (prev_dev) {
1480                 skb->dev = prev_dev;
1481                 netif_rx(skb);
1482                 skb = NULL;
1483         }
1484         rcu_read_unlock();
1485         dev_kfree_skb(skb);
1486 }
1487 EXPORT_SYMBOL(ieee80211_tx_status);
1488
1489 struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
1490                                         const struct ieee80211_ops *ops)
1491 {
1492         struct ieee80211_local *local;
1493         int priv_size;
1494         struct wiphy *wiphy;
1495
1496         /* Ensure 32-byte alignment of our private data and hw private data.
1497          * We use the wiphy priv data for both our ieee80211_local and for
1498          * the driver's private data
1499          *
1500          * In memory it'll be like this:
1501          *
1502          * +-------------------------+
1503          * | struct wiphy           |
1504          * +-------------------------+
1505          * | struct ieee80211_local  |
1506          * +-------------------------+
1507          * | driver's private data   |
1508          * +-------------------------+
1509          *
1510          */
1511         priv_size = ((sizeof(struct ieee80211_local) +
1512                       NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
1513                     priv_data_len;
1514
1515         wiphy = wiphy_new(&mac80211_config_ops, priv_size);
1516
1517         if (!wiphy)
1518                 return NULL;
1519
1520         wiphy->privid = mac80211_wiphy_privid;
1521
1522         local = wiphy_priv(wiphy);
1523         local->hw.wiphy = wiphy;
1524
1525         local->hw.priv = (char *)local +
1526                          ((sizeof(struct ieee80211_local) +
1527                            NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
1528
1529         BUG_ON(!ops->tx);
1530         BUG_ON(!ops->start);
1531         BUG_ON(!ops->stop);
1532         BUG_ON(!ops->config);
1533         BUG_ON(!ops->add_interface);
1534         BUG_ON(!ops->remove_interface);
1535         BUG_ON(!ops->configure_filter);
1536         local->ops = ops;
1537
1538         local->hw.queues = 1; /* default */
1539
1540         local->bridge_packets = 1;
1541
1542         local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1543         local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1544         local->short_retry_limit = 7;
1545         local->long_retry_limit = 4;
1546         local->hw.conf.radio_enabled = 1;
1547
1548         INIT_LIST_HEAD(&local->interfaces);
1549
1550         INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
1551
1552         sta_info_init(local);
1553
1554         tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1555                      (unsigned long)local);
1556         tasklet_disable(&local->tx_pending_tasklet);
1557
1558         tasklet_init(&local->tasklet,
1559                      ieee80211_tasklet_handler,
1560                      (unsigned long) local);
1561         tasklet_disable(&local->tasklet);
1562
1563         skb_queue_head_init(&local->skb_queue);
1564         skb_queue_head_init(&local->skb_queue_unreliable);
1565
1566         return local_to_hw(local);
1567 }
1568 EXPORT_SYMBOL(ieee80211_alloc_hw);
1569
1570 int ieee80211_register_hw(struct ieee80211_hw *hw)
1571 {
1572         struct ieee80211_local *local = hw_to_local(hw);
1573         const char *name;
1574         int result;
1575         enum ieee80211_band band;
1576         struct net_device *mdev;
1577         struct ieee80211_sub_if_data *sdata;
1578
1579         /*
1580          * generic code guarantees at least one band,
1581          * set this very early because much code assumes
1582          * that hw.conf.channel is assigned
1583          */
1584         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1585                 struct ieee80211_supported_band *sband;
1586
1587                 sband = local->hw.wiphy->bands[band];
1588                 if (sband) {
1589                         /* init channel we're on */
1590                         local->hw.conf.channel =
1591                         local->oper_channel =
1592                         local->scan_channel = &sband->channels[0];
1593                         break;
1594                 }
1595         }
1596
1597         result = wiphy_register(local->hw.wiphy);
1598         if (result < 0)
1599                 return result;
1600
1601         /* for now, mdev needs sub_if_data :/ */
1602         mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
1603                             "wmaster%d", ether_setup);
1604         if (!mdev)
1605                 goto fail_mdev_alloc;
1606
1607         sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
1608         mdev->ieee80211_ptr = &sdata->wdev;
1609         sdata->wdev.wiphy = local->hw.wiphy;
1610
1611         local->mdev = mdev;
1612
1613         ieee80211_rx_bss_list_init(mdev);
1614
1615         mdev->hard_start_xmit = ieee80211_master_start_xmit;
1616         mdev->open = ieee80211_master_open;
1617         mdev->stop = ieee80211_master_stop;
1618         mdev->type = ARPHRD_IEEE80211;
1619         mdev->header_ops = &ieee80211_header_ops;
1620         mdev->set_multicast_list = ieee80211_master_set_multicast_list;
1621
1622         sdata->vif.type = IEEE80211_IF_TYPE_AP;
1623         sdata->dev = mdev;
1624         sdata->local = local;
1625         sdata->u.ap.force_unicast_rateidx = -1;
1626         sdata->u.ap.max_ratectrl_rateidx = -1;
1627         ieee80211_if_sdata_init(sdata);
1628
1629         /* no RCU needed since we're still during init phase */
1630         list_add_tail(&sdata->list, &local->interfaces);
1631
1632         name = wiphy_dev(local->hw.wiphy)->driver->name;
1633         local->hw.workqueue = create_singlethread_workqueue(name);
1634         if (!local->hw.workqueue) {
1635                 result = -ENOMEM;
1636                 goto fail_workqueue;
1637         }
1638
1639         /*
1640          * The hardware needs headroom for sending the frame,
1641          * and we need some headroom for passing the frame to monitor
1642          * interfaces, but never both at the same time.
1643          */
1644         local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1645                                    sizeof(struct ieee80211_tx_status_rtap_hdr));
1646
1647         debugfs_hw_add(local);
1648
1649         local->hw.conf.beacon_int = 1000;
1650
1651         local->wstats_flags |= local->hw.max_rssi ?
1652                                IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1653         local->wstats_flags |= local->hw.max_signal ?
1654                                IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1655         local->wstats_flags |= local->hw.max_noise ?
1656                                IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1657         if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1658                 local->wstats_flags |= IW_QUAL_DBM;
1659
1660         result = sta_info_start(local);
1661         if (result < 0)
1662                 goto fail_sta_info;
1663
1664         rtnl_lock();
1665         result = dev_alloc_name(local->mdev, local->mdev->name);
1666         if (result < 0)
1667                 goto fail_dev;
1668
1669         memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1670         SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1671
1672         result = register_netdevice(local->mdev);
1673         if (result < 0)
1674                 goto fail_dev;
1675
1676         ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1677         ieee80211_if_set_type(local->mdev, IEEE80211_IF_TYPE_AP);
1678
1679         result = ieee80211_init_rate_ctrl_alg(local,
1680                                               hw->rate_control_algorithm);
1681         if (result < 0) {
1682                 printk(KERN_DEBUG "%s: Failed to initialize rate control "
1683                        "algorithm\n", wiphy_name(local->hw.wiphy));
1684                 goto fail_rate;
1685         }
1686
1687         result = ieee80211_wep_init(local);
1688
1689         if (result < 0) {
1690                 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
1691                        wiphy_name(local->hw.wiphy));
1692                 goto fail_wep;
1693         }
1694
1695         ieee80211_install_qdisc(local->mdev);
1696
1697         /* add one default STA interface */
1698         result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
1699                                   IEEE80211_IF_TYPE_STA, NULL);
1700         if (result)
1701                 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
1702                        wiphy_name(local->hw.wiphy));
1703
1704         local->reg_state = IEEE80211_DEV_REGISTERED;
1705         rtnl_unlock();
1706
1707         ieee80211_led_init(local);
1708
1709         return 0;
1710
1711 fail_wep:
1712         rate_control_deinitialize(local);
1713 fail_rate:
1714         ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1715         unregister_netdevice(local->mdev);
1716 fail_dev:
1717         rtnl_unlock();
1718         sta_info_stop(local);
1719 fail_sta_info:
1720         debugfs_hw_del(local);
1721         destroy_workqueue(local->hw.workqueue);
1722 fail_workqueue:
1723         ieee80211_if_free(local->mdev);
1724         local->mdev = NULL;
1725 fail_mdev_alloc:
1726         wiphy_unregister(local->hw.wiphy);
1727         return result;
1728 }
1729 EXPORT_SYMBOL(ieee80211_register_hw);
1730
1731 void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1732 {
1733         struct ieee80211_local *local = hw_to_local(hw);
1734         struct ieee80211_sub_if_data *sdata, *tmp;
1735
1736         tasklet_kill(&local->tx_pending_tasklet);
1737         tasklet_kill(&local->tasklet);
1738
1739         rtnl_lock();
1740
1741         BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1742
1743         local->reg_state = IEEE80211_DEV_UNREGISTERED;
1744
1745         /*
1746          * At this point, interface list manipulations are fine
1747          * because the driver cannot be handing us frames any
1748          * more and the tasklet is killed.
1749          */
1750
1751         /*
1752          * First, we remove all non-master interfaces. Do this because they
1753          * may have bss pointer dependency on the master, and when we free
1754          * the master these would be freed as well, breaking our list
1755          * iteration completely.
1756          */
1757         list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
1758                 if (sdata->dev == local->mdev)
1759                         continue;
1760                 list_del(&sdata->list);
1761                 __ieee80211_if_del(local, sdata);
1762         }
1763
1764         /* then, finally, remove the master interface */
1765         __ieee80211_if_del(local, IEEE80211_DEV_TO_SUB_IF(local->mdev));
1766
1767         rtnl_unlock();
1768
1769         ieee80211_rx_bss_list_deinit(local->mdev);
1770         ieee80211_clear_tx_pending(local);
1771         sta_info_stop(local);
1772         rate_control_deinitialize(local);
1773         debugfs_hw_del(local);
1774
1775         if (skb_queue_len(&local->skb_queue)
1776                         || skb_queue_len(&local->skb_queue_unreliable))
1777                 printk(KERN_WARNING "%s: skb_queue not empty\n",
1778                        wiphy_name(local->hw.wiphy));
1779         skb_queue_purge(&local->skb_queue);
1780         skb_queue_purge(&local->skb_queue_unreliable);
1781
1782         destroy_workqueue(local->hw.workqueue);
1783         wiphy_unregister(local->hw.wiphy);
1784         ieee80211_wep_free(local);
1785         ieee80211_led_exit(local);
1786         ieee80211_if_free(local->mdev);
1787         local->mdev = NULL;
1788 }
1789 EXPORT_SYMBOL(ieee80211_unregister_hw);
1790
1791 void ieee80211_free_hw(struct ieee80211_hw *hw)
1792 {
1793         struct ieee80211_local *local = hw_to_local(hw);
1794
1795         wiphy_free(local->hw.wiphy);
1796 }
1797 EXPORT_SYMBOL(ieee80211_free_hw);
1798
1799 static int __init ieee80211_init(void)
1800 {
1801         struct sk_buff *skb;
1802         int ret;
1803
1804         BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1805
1806         ret = rc80211_simple_init();
1807         if (ret)
1808                 goto out;
1809
1810         ret = rc80211_pid_init();
1811         if (ret)
1812                 goto out_cleanup_simple;
1813
1814         ret = ieee80211_wme_register();
1815         if (ret) {
1816                 printk(KERN_DEBUG "ieee80211_init: failed to "
1817                        "initialize WME (err=%d)\n", ret);
1818                 goto out_cleanup_pid;
1819         }
1820
1821         ieee80211_debugfs_netdev_init();
1822
1823         return 0;
1824
1825  out_cleanup_pid:
1826         rc80211_pid_exit();
1827  out_cleanup_simple:
1828         rc80211_simple_exit();
1829  out:
1830         return ret;
1831 }
1832
1833 static void __exit ieee80211_exit(void)
1834 {
1835         rc80211_simple_exit();
1836         rc80211_pid_exit();
1837
1838         if (mesh_allocated)
1839                 ieee80211s_stop();
1840
1841         ieee80211_wme_unregister();
1842         ieee80211_debugfs_netdev_exit();
1843 }
1844
1845
1846 subsys_initcall(ieee80211_init);
1847 module_exit(ieee80211_exit);
1848
1849 MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1850 MODULE_LICENSE("GPL");