caf92424c76dad0068b37743f3cc5923165a7a7b
[safe/jmp/linux-2.6] / net / mac80211 / main.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 "rate.h"
29 #include "mesh.h"
30 #include "wep.h"
31 #include "wme.h"
32 #include "aes_ccm.h"
33 #include "led.h"
34 #include "cfg.h"
35 #include "debugfs.h"
36 #include "debugfs_netdev.h"
37
38 /*
39  * For seeing transmitted packets on monitor interfaces
40  * we have a radiotap header too.
41  */
42 struct ieee80211_tx_status_rtap_hdr {
43         struct ieee80211_radiotap_header hdr;
44         u8 rate;
45         u8 padding_for_rate;
46         __le16 tx_flags;
47         u8 data_retries;
48 } __attribute__ ((packed));
49
50
51 /* must be called under mdev tx lock */
52 void ieee80211_configure_filter(struct ieee80211_local *local)
53 {
54         unsigned int changed_flags;
55         unsigned int new_flags = 0;
56
57         if (atomic_read(&local->iff_promiscs))
58                 new_flags |= FIF_PROMISC_IN_BSS;
59
60         if (atomic_read(&local->iff_allmultis))
61                 new_flags |= FIF_ALLMULTI;
62
63         if (local->monitors)
64                 new_flags |= FIF_BCN_PRBRESP_PROMISC;
65
66         if (local->fif_fcsfail)
67                 new_flags |= FIF_FCSFAIL;
68
69         if (local->fif_plcpfail)
70                 new_flags |= FIF_PLCPFAIL;
71
72         if (local->fif_control)
73                 new_flags |= FIF_CONTROL;
74
75         if (local->fif_other_bss)
76                 new_flags |= FIF_OTHER_BSS;
77
78         changed_flags = local->filter_flags ^ new_flags;
79
80         /* be a bit nasty */
81         new_flags |= (1<<31);
82
83         local->ops->configure_filter(local_to_hw(local),
84                                      changed_flags, &new_flags,
85                                      local->mdev->mc_count,
86                                      local->mdev->mc_list);
87
88         WARN_ON(new_flags & (1<<31));
89
90         local->filter_flags = new_flags & ~(1<<31);
91 }
92
93 /* master interface */
94
95 static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
96 {
97         memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
98         return ETH_ALEN;
99 }
100
101 static const struct header_ops ieee80211_header_ops = {
102         .create         = eth_header,
103         .parse          = header_parse_80211,
104         .rebuild        = eth_rebuild_header,
105         .cache          = eth_header_cache,
106         .cache_update   = eth_header_cache_update,
107 };
108
109 static int ieee80211_master_open(struct net_device *dev)
110 {
111         struct ieee80211_master_priv *mpriv = netdev_priv(dev);
112         struct ieee80211_local *local = mpriv->local;
113         struct ieee80211_sub_if_data *sdata;
114         int res = -EOPNOTSUPP;
115
116         /* we hold the RTNL here so can safely walk the list */
117         list_for_each_entry(sdata, &local->interfaces, list) {
118                 if (netif_running(sdata->dev)) {
119                         res = 0;
120                         break;
121                 }
122         }
123
124         if (res)
125                 return res;
126
127         netif_tx_start_all_queues(local->mdev);
128
129         return 0;
130 }
131
132 static int ieee80211_master_stop(struct net_device *dev)
133 {
134         struct ieee80211_master_priv *mpriv = netdev_priv(dev);
135         struct ieee80211_local *local = mpriv->local;
136         struct ieee80211_sub_if_data *sdata;
137
138         /* we hold the RTNL here so can safely walk the list */
139         list_for_each_entry(sdata, &local->interfaces, list)
140                 if (netif_running(sdata->dev))
141                         dev_close(sdata->dev);
142
143         return 0;
144 }
145
146 static void ieee80211_master_set_multicast_list(struct net_device *dev)
147 {
148         struct ieee80211_master_priv *mpriv = netdev_priv(dev);
149         struct ieee80211_local *local = mpriv->local;
150
151         ieee80211_configure_filter(local);
152 }
153
154 /* everything else */
155
156 int ieee80211_if_config(struct ieee80211_sub_if_data *sdata, u32 changed)
157 {
158         struct ieee80211_local *local = sdata->local;
159         struct ieee80211_if_conf conf;
160
161         if (WARN_ON(!netif_running(sdata->dev)))
162                 return 0;
163
164         if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
165                 return -EINVAL;
166
167         if (!local->ops->config_interface)
168                 return 0;
169
170         memset(&conf, 0, sizeof(conf));
171
172         if (sdata->vif.type == NL80211_IFTYPE_STATION ||
173             sdata->vif.type == NL80211_IFTYPE_ADHOC)
174                 conf.bssid = sdata->u.sta.bssid;
175         else if (sdata->vif.type == NL80211_IFTYPE_AP)
176                 conf.bssid = sdata->dev->dev_addr;
177         else if (ieee80211_vif_is_mesh(&sdata->vif)) {
178                 static const u8 zero[ETH_ALEN] = { 0 };
179                 conf.bssid = zero;
180         } else {
181                 WARN_ON(1);
182                 return -EINVAL;
183         }
184
185         switch (sdata->vif.type) {
186         case NL80211_IFTYPE_AP:
187         case NL80211_IFTYPE_ADHOC:
188         case NL80211_IFTYPE_MESH_POINT:
189                 break;
190         default:
191                 /* do not warn to simplify caller in scan.c */
192                 changed &= ~IEEE80211_IFCC_BEACON_ENABLED;
193                 if (WARN_ON(changed & IEEE80211_IFCC_BEACON))
194                         return -EINVAL;
195                 changed &= ~IEEE80211_IFCC_BEACON;
196                 break;
197         }
198
199         if (changed & IEEE80211_IFCC_BEACON_ENABLED) {
200                 if (local->sw_scanning) {
201                         conf.enable_beacon = false;
202                 } else {
203                         /*
204                          * Beacon should be enabled, but AP mode must
205                          * check whether there is a beacon configured.
206                          */
207                         switch (sdata->vif.type) {
208                         case NL80211_IFTYPE_AP:
209                                 conf.enable_beacon =
210                                         !!rcu_dereference(sdata->u.ap.beacon);
211                                 break;
212                         case NL80211_IFTYPE_ADHOC:
213                         case NL80211_IFTYPE_MESH_POINT:
214                                 conf.enable_beacon = true;
215                                 break;
216                         default:
217                                 /* not reached */
218                                 WARN_ON(1);
219                                 break;
220                         }
221                 }
222         }
223
224         if (WARN_ON(!conf.bssid && (changed & IEEE80211_IFCC_BSSID)))
225                 return -EINVAL;
226
227         conf.changed = changed;
228
229         return local->ops->config_interface(local_to_hw(local),
230                                             &sdata->vif, &conf);
231 }
232
233 int ieee80211_hw_config(struct ieee80211_local *local, u32 changed)
234 {
235         struct ieee80211_channel *chan;
236         int ret = 0;
237         int power;
238         enum nl80211_channel_type channel_type;
239
240         might_sleep();
241
242         if (local->sw_scanning) {
243                 chan = local->scan_channel;
244                 channel_type = NL80211_CHAN_NO_HT;
245         } else {
246                 chan = local->oper_channel;
247                 channel_type = local->oper_channel_type;
248         }
249
250         if (chan != local->hw.conf.channel ||
251             channel_type != local->hw.conf.channel_type) {
252                 local->hw.conf.channel = chan;
253                 local->hw.conf.channel_type = channel_type;
254                 changed |= IEEE80211_CONF_CHANGE_CHANNEL;
255         }
256
257         if (local->sw_scanning)
258                 power = chan->max_power;
259         else
260                 power = local->power_constr_level ?
261                         (chan->max_power - local->power_constr_level) :
262                         chan->max_power;
263
264         if (local->user_power_level)
265                 power = min(power, local->user_power_level);
266
267         if (local->hw.conf.power_level != power) {
268                 changed |= IEEE80211_CONF_CHANGE_POWER;
269                 local->hw.conf.power_level = power;
270         }
271
272         if (changed && local->open_count) {
273                 ret = local->ops->config(local_to_hw(local), changed);
274                 /*
275                  * Goal:
276                  * HW reconfiguration should never fail, the driver has told
277                  * us what it can support so it should live up to that promise.
278                  *
279                  * Current status:
280                  * rfkill is not integrated with mac80211 and a
281                  * configuration command can thus fail if hardware rfkill
282                  * is enabled
283                  *
284                  * FIXME: integrate rfkill with mac80211 and then add this
285                  * WARN_ON() back
286                  *
287                  */
288                 /* WARN_ON(ret); */
289         }
290
291         return ret;
292 }
293
294 void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
295                                       u32 changed)
296 {
297         struct ieee80211_local *local = sdata->local;
298
299         if (WARN_ON(sdata->vif.type == NL80211_IFTYPE_AP_VLAN))
300                 return;
301
302         if (!changed)
303                 return;
304
305         if (local->ops->bss_info_changed)
306                 local->ops->bss_info_changed(local_to_hw(local),
307                                              &sdata->vif,
308                                              &sdata->vif.bss_conf,
309                                              changed);
310 }
311
312 u32 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata)
313 {
314         sdata->vif.bss_conf.use_cts_prot = false;
315         sdata->vif.bss_conf.use_short_preamble = false;
316         sdata->vif.bss_conf.use_short_slot = false;
317         return BSS_CHANGED_ERP_CTS_PROT |
318                BSS_CHANGED_ERP_PREAMBLE |
319                BSS_CHANGED_ERP_SLOT;
320 }
321
322 void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
323                                  struct sk_buff *skb)
324 {
325         struct ieee80211_local *local = hw_to_local(hw);
326         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
327         int tmp;
328
329         skb->dev = local->mdev;
330         skb->pkt_type = IEEE80211_TX_STATUS_MSG;
331         skb_queue_tail(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS ?
332                        &local->skb_queue : &local->skb_queue_unreliable, skb);
333         tmp = skb_queue_len(&local->skb_queue) +
334                 skb_queue_len(&local->skb_queue_unreliable);
335         while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
336                (skb = skb_dequeue(&local->skb_queue_unreliable))) {
337                 dev_kfree_skb_irq(skb);
338                 tmp--;
339                 I802_DEBUG_INC(local->tx_status_drop);
340         }
341         tasklet_schedule(&local->tasklet);
342 }
343 EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
344
345 static void ieee80211_tasklet_handler(unsigned long data)
346 {
347         struct ieee80211_local *local = (struct ieee80211_local *) data;
348         struct sk_buff *skb;
349         struct ieee80211_rx_status rx_status;
350         struct ieee80211_ra_tid *ra_tid;
351
352         while ((skb = skb_dequeue(&local->skb_queue)) ||
353                (skb = skb_dequeue(&local->skb_queue_unreliable))) {
354                 switch (skb->pkt_type) {
355                 case IEEE80211_RX_MSG:
356                         /* status is in skb->cb */
357                         memcpy(&rx_status, skb->cb, sizeof(rx_status));
358                         /* Clear skb->pkt_type in order to not confuse kernel
359                          * netstack. */
360                         skb->pkt_type = 0;
361                         __ieee80211_rx(local_to_hw(local), skb, &rx_status);
362                         break;
363                 case IEEE80211_TX_STATUS_MSG:
364                         skb->pkt_type = 0;
365                         ieee80211_tx_status(local_to_hw(local), skb);
366                         break;
367                 case IEEE80211_DELBA_MSG:
368                         ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
369                         ieee80211_stop_tx_ba_cb(local_to_hw(local),
370                                                 ra_tid->ra, ra_tid->tid);
371                         dev_kfree_skb(skb);
372                         break;
373                 case IEEE80211_ADDBA_MSG:
374                         ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
375                         ieee80211_start_tx_ba_cb(local_to_hw(local),
376                                                  ra_tid->ra, ra_tid->tid);
377                         dev_kfree_skb(skb);
378                         break ;
379                 default:
380                         WARN(1, "mac80211: Packet is of unknown type %d\n",
381                              skb->pkt_type);
382                         dev_kfree_skb(skb);
383                         break;
384                 }
385         }
386 }
387
388 /* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
389  * make a prepared TX frame (one that has been given to hw) to look like brand
390  * new IEEE 802.11 frame that is ready to go through TX processing again.
391  */
392 static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
393                                       struct ieee80211_key *key,
394                                       struct sk_buff *skb)
395 {
396         unsigned int hdrlen, iv_len, mic_len;
397         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
398
399         hdrlen = ieee80211_hdrlen(hdr->frame_control);
400
401         if (!key)
402                 goto no_key;
403
404         switch (key->conf.alg) {
405         case ALG_WEP:
406                 iv_len = WEP_IV_LEN;
407                 mic_len = WEP_ICV_LEN;
408                 break;
409         case ALG_TKIP:
410                 iv_len = TKIP_IV_LEN;
411                 mic_len = TKIP_ICV_LEN;
412                 break;
413         case ALG_CCMP:
414                 iv_len = CCMP_HDR_LEN;
415                 mic_len = CCMP_MIC_LEN;
416                 break;
417         default:
418                 goto no_key;
419         }
420
421         if (skb->len >= hdrlen + mic_len &&
422             !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
423                 skb_trim(skb, skb->len - mic_len);
424         if (skb->len >= hdrlen + iv_len) {
425                 memmove(skb->data + iv_len, skb->data, hdrlen);
426                 hdr = (struct ieee80211_hdr *)skb_pull(skb, iv_len);
427         }
428
429 no_key:
430         if (ieee80211_is_data_qos(hdr->frame_control)) {
431                 hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
432                 memmove(skb->data + IEEE80211_QOS_CTL_LEN, skb->data,
433                         hdrlen - IEEE80211_QOS_CTL_LEN);
434                 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
435         }
436 }
437
438 static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
439                                             struct sta_info *sta,
440                                             struct sk_buff *skb)
441 {
442         sta->tx_filtered_count++;
443
444         /*
445          * Clear the TX filter mask for this STA when sending the next
446          * packet. If the STA went to power save mode, this will happen
447          * when it wakes up for the next time.
448          */
449         set_sta_flags(sta, WLAN_STA_CLEAR_PS_FILT);
450
451         /*
452          * This code races in the following way:
453          *
454          *  (1) STA sends frame indicating it will go to sleep and does so
455          *  (2) hardware/firmware adds STA to filter list, passes frame up
456          *  (3) hardware/firmware processes TX fifo and suppresses a frame
457          *  (4) we get TX status before having processed the frame and
458          *      knowing that the STA has gone to sleep.
459          *
460          * This is actually quite unlikely even when both those events are
461          * processed from interrupts coming in quickly after one another or
462          * even at the same time because we queue both TX status events and
463          * RX frames to be processed by a tasklet and process them in the
464          * same order that they were received or TX status last. Hence, there
465          * is no race as long as the frame RX is processed before the next TX
466          * status, which drivers can ensure, see below.
467          *
468          * Note that this can only happen if the hardware or firmware can
469          * actually add STAs to the filter list, if this is done by the
470          * driver in response to set_tim() (which will only reduce the race
471          * this whole filtering tries to solve, not completely solve it)
472          * this situation cannot happen.
473          *
474          * To completely solve this race drivers need to make sure that they
475          *  (a) don't mix the irq-safe/not irq-safe TX status/RX processing
476          *      functions and
477          *  (b) always process RX events before TX status events if ordering
478          *      can be unknown, for example with different interrupt status
479          *      bits.
480          */
481         if (test_sta_flags(sta, WLAN_STA_PS) &&
482             skb_queue_len(&sta->tx_filtered) < STA_MAX_TX_BUFFER) {
483                 ieee80211_remove_tx_extra(local, sta->key, skb);
484                 skb_queue_tail(&sta->tx_filtered, skb);
485                 return;
486         }
487
488         if (!test_sta_flags(sta, WLAN_STA_PS) && !skb->requeue) {
489                 /* Software retry the packet once */
490                 skb->requeue = 1;
491                 ieee80211_remove_tx_extra(local, sta->key, skb);
492                 dev_queue_xmit(skb);
493                 return;
494         }
495
496 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
497         if (net_ratelimit())
498                 printk(KERN_DEBUG "%s: dropped TX filtered frame, "
499                        "queue_len=%d PS=%d @%lu\n",
500                        wiphy_name(local->hw.wiphy),
501                        skb_queue_len(&sta->tx_filtered),
502                        !!test_sta_flags(sta, WLAN_STA_PS), jiffies);
503 #endif
504         dev_kfree_skb(skb);
505 }
506
507 void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
508 {
509         struct sk_buff *skb2;
510         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
511         struct ieee80211_local *local = hw_to_local(hw);
512         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
513         u16 frag, type;
514         __le16 fc;
515         struct ieee80211_supported_band *sband;
516         struct ieee80211_tx_status_rtap_hdr *rthdr;
517         struct ieee80211_sub_if_data *sdata;
518         struct net_device *prev_dev = NULL;
519         struct sta_info *sta;
520         int retry_count = -1, i;
521
522         for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
523                 /* the HW cannot have attempted that rate */
524                 if (i >= hw->max_rates) {
525                         info->status.rates[i].idx = -1;
526                         info->status.rates[i].count = 0;
527                 }
528
529                 retry_count += info->status.rates[i].count;
530         }
531         if (retry_count < 0)
532                 retry_count = 0;
533
534         rcu_read_lock();
535
536         sband = local->hw.wiphy->bands[info->band];
537
538         sta = sta_info_get(local, hdr->addr1);
539
540         if (sta) {
541                 if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
542                     test_sta_flags(sta, WLAN_STA_PS)) {
543                         /*
544                          * The STA is in power save mode, so assume
545                          * that this TX packet failed because of that.
546                          */
547                         ieee80211_handle_filtered_frame(local, sta, skb);
548                         rcu_read_unlock();
549                         return;
550                 }
551
552                 fc = hdr->frame_control;
553
554                 if ((info->flags & IEEE80211_TX_STAT_AMPDU_NO_BACK) &&
555                     (ieee80211_is_data_qos(fc))) {
556                         u16 tid, ssn;
557                         u8 *qc;
558
559                         qc = ieee80211_get_qos_ctl(hdr);
560                         tid = qc[0] & 0xf;
561                         ssn = ((le16_to_cpu(hdr->seq_ctrl) + 0x10)
562                                                 & IEEE80211_SCTL_SEQ);
563                         ieee80211_send_bar(sta->sdata, hdr->addr1,
564                                            tid, ssn);
565                 }
566
567                 if (info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
568                         ieee80211_handle_filtered_frame(local, sta, skb);
569                         rcu_read_unlock();
570                         return;
571                 } else {
572                         if (!(info->flags & IEEE80211_TX_STAT_ACK))
573                                 sta->tx_retry_failed++;
574                         sta->tx_retry_count += retry_count;
575                 }
576
577                 rate_control_tx_status(local, sband, sta, skb);
578         }
579
580         rcu_read_unlock();
581
582         ieee80211_led_tx(local, 0);
583
584         /* SNMP counters
585          * Fragments are passed to low-level drivers as separate skbs, so these
586          * are actually fragments, not frames. Update frame counters only for
587          * the first fragment of the frame. */
588
589         frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
590         type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
591
592         if (info->flags & IEEE80211_TX_STAT_ACK) {
593                 if (frag == 0) {
594                         local->dot11TransmittedFrameCount++;
595                         if (is_multicast_ether_addr(hdr->addr1))
596                                 local->dot11MulticastTransmittedFrameCount++;
597                         if (retry_count > 0)
598                                 local->dot11RetryCount++;
599                         if (retry_count > 1)
600                                 local->dot11MultipleRetryCount++;
601                 }
602
603                 /* This counter shall be incremented for an acknowledged MPDU
604                  * with an individual address in the address 1 field or an MPDU
605                  * with a multicast address in the address 1 field of type Data
606                  * or Management. */
607                 if (!is_multicast_ether_addr(hdr->addr1) ||
608                     type == IEEE80211_FTYPE_DATA ||
609                     type == IEEE80211_FTYPE_MGMT)
610                         local->dot11TransmittedFragmentCount++;
611         } else {
612                 if (frag == 0)
613                         local->dot11FailedCount++;
614         }
615
616         /* this was a transmitted frame, but now we want to reuse it */
617         skb_orphan(skb);
618
619         /*
620          * This is a bit racy but we can avoid a lot of work
621          * with this test...
622          */
623         if (!local->monitors && !local->cooked_mntrs) {
624                 dev_kfree_skb(skb);
625                 return;
626         }
627
628         /* send frame to monitor interfaces now */
629
630         if (skb_headroom(skb) < sizeof(*rthdr)) {
631                 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
632                 dev_kfree_skb(skb);
633                 return;
634         }
635
636         rthdr = (struct ieee80211_tx_status_rtap_hdr *)
637                                 skb_push(skb, sizeof(*rthdr));
638
639         memset(rthdr, 0, sizeof(*rthdr));
640         rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
641         rthdr->hdr.it_present =
642                 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
643                             (1 << IEEE80211_RADIOTAP_DATA_RETRIES) |
644                             (1 << IEEE80211_RADIOTAP_RATE));
645
646         if (!(info->flags & IEEE80211_TX_STAT_ACK) &&
647             !is_multicast_ether_addr(hdr->addr1))
648                 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
649
650         /*
651          * XXX: Once radiotap gets the bitmap reset thing the vendor
652          *      extensions proposal contains, we can actually report
653          *      the whole set of tries we did.
654          */
655         if ((info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) ||
656             (info->status.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
657                 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
658         else if (info->status.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
659                 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
660         if (info->status.rates[0].idx >= 0 &&
661             !(info->status.rates[0].flags & IEEE80211_TX_RC_MCS))
662                 rthdr->rate = sband->bitrates[
663                                 info->status.rates[0].idx].bitrate / 5;
664
665         /* for now report the total retry_count */
666         rthdr->data_retries = retry_count;
667
668         /* XXX: is this sufficient for BPF? */
669         skb_set_mac_header(skb, 0);
670         skb->ip_summed = CHECKSUM_UNNECESSARY;
671         skb->pkt_type = PACKET_OTHERHOST;
672         skb->protocol = htons(ETH_P_802_2);
673         memset(skb->cb, 0, sizeof(skb->cb));
674
675         rcu_read_lock();
676         list_for_each_entry_rcu(sdata, &local->interfaces, list) {
677                 if (sdata->vif.type == NL80211_IFTYPE_MONITOR) {
678                         if (!netif_running(sdata->dev))
679                                 continue;
680
681                         if (prev_dev) {
682                                 skb2 = skb_clone(skb, GFP_ATOMIC);
683                                 if (skb2) {
684                                         skb2->dev = prev_dev;
685                                         netif_rx(skb2);
686                                 }
687                         }
688
689                         prev_dev = sdata->dev;
690                 }
691         }
692         if (prev_dev) {
693                 skb->dev = prev_dev;
694                 netif_rx(skb);
695                 skb = NULL;
696         }
697         rcu_read_unlock();
698         dev_kfree_skb(skb);
699 }
700 EXPORT_SYMBOL(ieee80211_tx_status);
701
702 struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
703                                         const struct ieee80211_ops *ops)
704 {
705         struct ieee80211_local *local;
706         int priv_size;
707         struct wiphy *wiphy;
708
709         /* Ensure 32-byte alignment of our private data and hw private data.
710          * We use the wiphy priv data for both our ieee80211_local and for
711          * the driver's private data
712          *
713          * In memory it'll be like this:
714          *
715          * +-------------------------+
716          * | struct wiphy           |
717          * +-------------------------+
718          * | struct ieee80211_local  |
719          * +-------------------------+
720          * | driver's private data   |
721          * +-------------------------+
722          *
723          */
724         priv_size = ((sizeof(struct ieee80211_local) +
725                       NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
726                     priv_data_len;
727
728         wiphy = wiphy_new(&mac80211_config_ops, priv_size);
729
730         if (!wiphy)
731                 return NULL;
732
733         wiphy->privid = mac80211_wiphy_privid;
734
735         local = wiphy_priv(wiphy);
736         local->hw.wiphy = wiphy;
737
738         local->hw.priv = (char *)local +
739                          ((sizeof(struct ieee80211_local) +
740                            NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
741
742         BUG_ON(!ops->tx);
743         BUG_ON(!ops->start);
744         BUG_ON(!ops->stop);
745         BUG_ON(!ops->config);
746         BUG_ON(!ops->add_interface);
747         BUG_ON(!ops->remove_interface);
748         BUG_ON(!ops->configure_filter);
749         local->ops = ops;
750
751         /* set up some defaults */
752         local->hw.queues = 1;
753         local->hw.max_rates = 1;
754         local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
755         local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
756         local->hw.conf.long_frame_max_tx_count = 4;
757         local->hw.conf.short_frame_max_tx_count = 7;
758         local->hw.conf.radio_enabled = true;
759
760         INIT_LIST_HEAD(&local->interfaces);
761         mutex_init(&local->iflist_mtx);
762
763         spin_lock_init(&local->key_lock);
764
765         spin_lock_init(&local->queue_stop_reason_lock);
766
767         INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
768
769         INIT_WORK(&local->dynamic_ps_enable_work,
770                   ieee80211_dynamic_ps_enable_work);
771         INIT_WORK(&local->dynamic_ps_disable_work,
772                   ieee80211_dynamic_ps_disable_work);
773         setup_timer(&local->dynamic_ps_timer,
774                     ieee80211_dynamic_ps_timer, (unsigned long) local);
775
776         sta_info_init(local);
777
778         tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
779                      (unsigned long)local);
780         tasklet_disable(&local->tx_pending_tasklet);
781
782         tasklet_init(&local->tasklet,
783                      ieee80211_tasklet_handler,
784                      (unsigned long) local);
785         tasklet_disable(&local->tasklet);
786
787         skb_queue_head_init(&local->skb_queue);
788         skb_queue_head_init(&local->skb_queue_unreliable);
789
790         return local_to_hw(local);
791 }
792 EXPORT_SYMBOL(ieee80211_alloc_hw);
793
794 static const struct net_device_ops ieee80211_master_ops = {
795         .ndo_start_xmit = ieee80211_master_start_xmit,
796         .ndo_open = ieee80211_master_open,
797         .ndo_stop = ieee80211_master_stop,
798         .ndo_set_multicast_list = ieee80211_master_set_multicast_list,
799         .ndo_select_queue = ieee80211_select_queue,
800 };
801
802 static void ieee80211_master_setup(struct net_device *mdev)
803 {
804         mdev->type = ARPHRD_IEEE80211;
805         mdev->netdev_ops = &ieee80211_master_ops;
806         mdev->header_ops = &ieee80211_header_ops;
807         mdev->tx_queue_len = 1000;
808         mdev->addr_len = ETH_ALEN;
809 }
810
811 int ieee80211_register_hw(struct ieee80211_hw *hw)
812 {
813         struct ieee80211_local *local = hw_to_local(hw);
814         int result;
815         enum ieee80211_band band;
816         struct net_device *mdev;
817         struct ieee80211_master_priv *mpriv;
818
819         /*
820          * generic code guarantees at least one band,
821          * set this very early because much code assumes
822          * that hw.conf.channel is assigned
823          */
824         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
825                 struct ieee80211_supported_band *sband;
826
827                 sband = local->hw.wiphy->bands[band];
828                 if (sband) {
829                         /* init channel we're on */
830                         local->hw.conf.channel =
831                         local->oper_channel =
832                         local->scan_channel = &sband->channels[0];
833                         break;
834                 }
835         }
836
837         /* if low-level driver supports AP, we also support VLAN */
838         if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP))
839                 local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
840
841         /* mac80211 always supports monitor */
842         local->hw.wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
843
844         result = wiphy_register(local->hw.wiphy);
845         if (result < 0)
846                 return result;
847
848         /*
849          * We use the number of queues for feature tests (QoS, HT) internally
850          * so restrict them appropriately.
851          */
852         if (hw->queues > IEEE80211_MAX_QUEUES)
853                 hw->queues = IEEE80211_MAX_QUEUES;
854         if (hw->ampdu_queues > IEEE80211_MAX_AMPDU_QUEUES)
855                 hw->ampdu_queues = IEEE80211_MAX_AMPDU_QUEUES;
856         if (hw->queues < 4)
857                 hw->ampdu_queues = 0;
858
859         mdev = alloc_netdev_mq(sizeof(struct ieee80211_master_priv),
860                                "wmaster%d", ieee80211_master_setup,
861                                ieee80211_num_queues(hw));
862         if (!mdev)
863                 goto fail_mdev_alloc;
864
865         mpriv = netdev_priv(mdev);
866         mpriv->local = local;
867         local->mdev = mdev;
868
869         ieee80211_rx_bss_list_init(local);
870
871         local->hw.workqueue =
872                 create_singlethread_workqueue(wiphy_name(local->hw.wiphy));
873         if (!local->hw.workqueue) {
874                 result = -ENOMEM;
875                 goto fail_workqueue;
876         }
877
878         /*
879          * The hardware needs headroom for sending the frame,
880          * and we need some headroom for passing the frame to monitor
881          * interfaces, but never both at the same time.
882          */
883         local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
884                                    sizeof(struct ieee80211_tx_status_rtap_hdr));
885
886         debugfs_hw_add(local);
887
888         if (local->hw.conf.beacon_int < 10)
889                 local->hw.conf.beacon_int = 100;
890
891         if (local->hw.max_listen_interval == 0)
892                 local->hw.max_listen_interval = 1;
893
894         local->hw.conf.listen_interval = local->hw.max_listen_interval;
895
896         local->wstats_flags |= local->hw.flags & (IEEE80211_HW_SIGNAL_UNSPEC |
897                                                   IEEE80211_HW_SIGNAL_DBM) ?
898                                IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
899         local->wstats_flags |= local->hw.flags & IEEE80211_HW_NOISE_DBM ?
900                                IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
901         if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
902                 local->wstats_flags |= IW_QUAL_DBM;
903
904         result = sta_info_start(local);
905         if (result < 0)
906                 goto fail_sta_info;
907
908         rtnl_lock();
909         result = dev_alloc_name(local->mdev, local->mdev->name);
910         if (result < 0)
911                 goto fail_dev;
912
913         memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
914         SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
915
916         result = register_netdevice(local->mdev);
917         if (result < 0)
918                 goto fail_dev;
919
920         result = ieee80211_init_rate_ctrl_alg(local,
921                                               hw->rate_control_algorithm);
922         if (result < 0) {
923                 printk(KERN_DEBUG "%s: Failed to initialize rate control "
924                        "algorithm\n", wiphy_name(local->hw.wiphy));
925                 goto fail_rate;
926         }
927
928         result = ieee80211_wep_init(local);
929
930         if (result < 0) {
931                 printk(KERN_DEBUG "%s: Failed to initialize wep: %d\n",
932                        wiphy_name(local->hw.wiphy), result);
933                 goto fail_wep;
934         }
935
936         /* add one default STA interface if supported */
937         if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_STATION)) {
938                 result = ieee80211_if_add(local, "wlan%d", NULL,
939                                           NL80211_IFTYPE_STATION, NULL);
940                 if (result)
941                         printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
942                                wiphy_name(local->hw.wiphy));
943         }
944
945         rtnl_unlock();
946
947         ieee80211_led_init(local);
948
949         return 0;
950
951 fail_wep:
952         rate_control_deinitialize(local);
953 fail_rate:
954         unregister_netdevice(local->mdev);
955         local->mdev = NULL;
956 fail_dev:
957         rtnl_unlock();
958         sta_info_stop(local);
959 fail_sta_info:
960         debugfs_hw_del(local);
961         destroy_workqueue(local->hw.workqueue);
962 fail_workqueue:
963         if (local->mdev)
964                 free_netdev(local->mdev);
965 fail_mdev_alloc:
966         wiphy_unregister(local->hw.wiphy);
967         return result;
968 }
969 EXPORT_SYMBOL(ieee80211_register_hw);
970
971 void ieee80211_unregister_hw(struct ieee80211_hw *hw)
972 {
973         struct ieee80211_local *local = hw_to_local(hw);
974
975         tasklet_kill(&local->tx_pending_tasklet);
976         tasklet_kill(&local->tasklet);
977
978         rtnl_lock();
979
980         /*
981          * At this point, interface list manipulations are fine
982          * because the driver cannot be handing us frames any
983          * more and the tasklet is killed.
984          */
985
986         /* First, we remove all virtual interfaces. */
987         ieee80211_remove_interfaces(local);
988
989         /* then, finally, remove the master interface */
990         unregister_netdevice(local->mdev);
991
992         rtnl_unlock();
993
994         ieee80211_rx_bss_list_deinit(local);
995         ieee80211_clear_tx_pending(local);
996         sta_info_stop(local);
997         rate_control_deinitialize(local);
998         debugfs_hw_del(local);
999
1000         if (skb_queue_len(&local->skb_queue)
1001                         || skb_queue_len(&local->skb_queue_unreliable))
1002                 printk(KERN_WARNING "%s: skb_queue not empty\n",
1003                        wiphy_name(local->hw.wiphy));
1004         skb_queue_purge(&local->skb_queue);
1005         skb_queue_purge(&local->skb_queue_unreliable);
1006
1007         destroy_workqueue(local->hw.workqueue);
1008         wiphy_unregister(local->hw.wiphy);
1009         ieee80211_wep_free(local);
1010         ieee80211_led_exit(local);
1011         free_netdev(local->mdev);
1012 }
1013 EXPORT_SYMBOL(ieee80211_unregister_hw);
1014
1015 void ieee80211_free_hw(struct ieee80211_hw *hw)
1016 {
1017         struct ieee80211_local *local = hw_to_local(hw);
1018
1019         mutex_destroy(&local->iflist_mtx);
1020
1021         wiphy_free(local->hw.wiphy);
1022 }
1023 EXPORT_SYMBOL(ieee80211_free_hw);
1024
1025 static int __init ieee80211_init(void)
1026 {
1027         struct sk_buff *skb;
1028         int ret;
1029
1030         BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
1031         BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
1032                      IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
1033
1034         ret = rc80211_minstrel_init();
1035         if (ret)
1036                 return ret;
1037
1038         ret = rc80211_pid_init();
1039         if (ret)
1040                 return ret;
1041
1042         ieee80211_debugfs_netdev_init();
1043
1044         return 0;
1045 }
1046
1047 static void __exit ieee80211_exit(void)
1048 {
1049         rc80211_pid_exit();
1050         rc80211_minstrel_exit();
1051
1052         /*
1053          * For key todo, it'll be empty by now but the work
1054          * might still be scheduled.
1055          */
1056         flush_scheduled_work();
1057
1058         if (mesh_allocated)
1059                 ieee80211s_stop();
1060
1061         ieee80211_debugfs_netdev_exit();
1062 }
1063
1064
1065 subsys_initcall(ieee80211_init);
1066 module_exit(ieee80211_exit);
1067
1068 MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1069 MODULE_LICENSE("GPL");