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