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