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