net/mlx4_en: Fix mixed PFC and Global pause user control requests
[pandora-kernel.git] / 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 <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/netdevice.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/skbuff.h>
18 #include <linux/etherdevice.h>
19 #include <linux/if_arp.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/bitmap.h>
22 #include <linux/pm_qos.h>
23 #include <linux/inetdevice.h>
24 #include <net/net_namespace.h>
25 #include <net/cfg80211.h>
26
27 #include "ieee80211_i.h"
28 #include "driver-ops.h"
29 #include "rate.h"
30 #include "mesh.h"
31 #include "wep.h"
32 #include "led.h"
33 #include "cfg.h"
34 #include "debugfs.h"
35
36 static struct lock_class_key ieee80211_rx_skb_queue_class;
37
38 void ieee80211_configure_filter(struct ieee80211_local *local)
39 {
40         u64 mc;
41         unsigned int changed_flags;
42         unsigned int new_flags = 0;
43
44         if (atomic_read(&local->iff_promiscs))
45                 new_flags |= FIF_PROMISC_IN_BSS;
46
47         if (atomic_read(&local->iff_allmultis))
48                 new_flags |= FIF_ALLMULTI;
49
50         if (local->monitors || local->scanning)
51                 new_flags |= FIF_BCN_PRBRESP_PROMISC;
52
53         if (local->fif_probe_req || local->probe_req_reg)
54                 new_flags |= FIF_PROBE_REQ;
55
56         if (local->fif_fcsfail)
57                 new_flags |= FIF_FCSFAIL;
58
59         if (local->fif_plcpfail)
60                 new_flags |= FIF_PLCPFAIL;
61
62         if (local->fif_control)
63                 new_flags |= FIF_CONTROL;
64
65         if (local->fif_other_bss)
66                 new_flags |= FIF_OTHER_BSS;
67
68         if (local->fif_pspoll)
69                 new_flags |= FIF_PSPOLL;
70
71         spin_lock_bh(&local->filter_lock);
72         changed_flags = local->filter_flags ^ new_flags;
73
74         mc = drv_prepare_multicast(local, &local->mc_list);
75         spin_unlock_bh(&local->filter_lock);
76
77         /* be a bit nasty */
78         new_flags |= (1<<31);
79
80         drv_configure_filter(local, changed_flags, &new_flags, mc);
81
82         WARN_ON(new_flags & (1<<31));
83
84         local->filter_flags = new_flags & ~(1<<31);
85 }
86
87 static void ieee80211_reconfig_filter(struct work_struct *work)
88 {
89         struct ieee80211_local *local =
90                 container_of(work, struct ieee80211_local, reconfig_filter);
91
92         ieee80211_configure_filter(local);
93 }
94
95 int ieee80211_hw_config(struct ieee80211_local *local, u32 changed)
96 {
97         struct ieee80211_channel *chan, *scan_chan;
98         int ret = 0;
99         int power;
100         enum nl80211_channel_type channel_type;
101         u32 offchannel_flag;
102
103         might_sleep();
104
105         scan_chan = local->scan_channel;
106
107         offchannel_flag = local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
108         if (scan_chan) {
109                 chan = scan_chan;
110                 /* If scanning on oper channel, use whatever channel-type
111                  * is currently in use.
112                  */
113                 if (chan == local->oper_channel)
114                         channel_type = local->_oper_channel_type;
115                 else
116                         channel_type = NL80211_CHAN_NO_HT;
117                 local->hw.conf.flags |= IEEE80211_CONF_OFFCHANNEL;
118         } else if (local->tmp_channel &&
119                    local->oper_channel != local->tmp_channel) {
120                 chan = scan_chan = local->tmp_channel;
121                 channel_type = local->tmp_channel_type;
122                 local->hw.conf.flags |= IEEE80211_CONF_OFFCHANNEL;
123         } else {
124                 chan = local->oper_channel;
125                 channel_type = local->_oper_channel_type;
126                 local->hw.conf.flags &= ~IEEE80211_CONF_OFFCHANNEL;
127         }
128
129         offchannel_flag ^= local->hw.conf.flags & IEEE80211_CONF_OFFCHANNEL;
130
131         if (offchannel_flag || chan != local->hw.conf.channel ||
132             channel_type != local->hw.conf.channel_type) {
133                 local->hw.conf.channel = chan;
134                 local->hw.conf.channel_type = channel_type;
135                 changed |= IEEE80211_CONF_CHANGE_CHANNEL;
136         }
137
138         if (!conf_is_ht(&local->hw.conf)) {
139                 /*
140                  * mac80211.h documents that this is only valid
141                  * when the channel is set to an HT type, and
142                  * that otherwise STATIC is used.
143                  */
144                 local->hw.conf.smps_mode = IEEE80211_SMPS_STATIC;
145         } else if (local->hw.conf.smps_mode != local->smps_mode) {
146                 local->hw.conf.smps_mode = local->smps_mode;
147                 changed |= IEEE80211_CONF_CHANGE_SMPS;
148         }
149
150         if ((local->scanning & SCAN_SW_SCANNING) ||
151             (local->scanning & SCAN_HW_SCANNING))
152                 power = chan->max_power;
153         else
154                 power = local->power_constr_level ?
155                         (chan->max_power - local->power_constr_level) :
156                         chan->max_power;
157
158         if (local->user_power_level >= 0)
159                 power = min(power, local->user_power_level);
160
161         if (local->hw.conf.power_level != power) {
162                 changed |= IEEE80211_CONF_CHANGE_POWER;
163                 local->hw.conf.power_level = power;
164         }
165
166         if (changed && local->open_count) {
167                 ret = drv_config(local, changed);
168                 /*
169                  * Goal:
170                  * HW reconfiguration should never fail, the driver has told
171                  * us what it can support so it should live up to that promise.
172                  *
173                  * Current status:
174                  * rfkill is not integrated with mac80211 and a
175                  * configuration command can thus fail if hardware rfkill
176                  * is enabled
177                  *
178                  * FIXME: integrate rfkill with mac80211 and then add this
179                  * WARN_ON() back
180                  *
181                  */
182                 /* WARN_ON(ret); */
183         }
184
185         return ret;
186 }
187
188 void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
189                                       u32 changed)
190 {
191         struct ieee80211_local *local = sdata->local;
192         static const u8 zero[ETH_ALEN] = { 0 };
193
194         if (!changed)
195                 return;
196
197         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
198                 /*
199                  * While not associated, claim a BSSID of all-zeroes
200                  * so that drivers don't do any weird things with the
201                  * BSSID at that time.
202                  */
203                 if (sdata->vif.bss_conf.assoc)
204                         sdata->vif.bss_conf.bssid = sdata->u.mgd.bssid;
205                 else
206                         sdata->vif.bss_conf.bssid = zero;
207         } else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
208                 sdata->vif.bss_conf.bssid = sdata->u.ibss.bssid;
209         else if (sdata->vif.type == NL80211_IFTYPE_AP)
210                 sdata->vif.bss_conf.bssid = sdata->vif.addr;
211         else if (sdata->vif.type == NL80211_IFTYPE_WDS)
212                 sdata->vif.bss_conf.bssid = NULL;
213         else if (ieee80211_vif_is_mesh(&sdata->vif)) {
214                 sdata->vif.bss_conf.bssid = zero;
215         } else {
216                 WARN_ON(1);
217                 return;
218         }
219
220         switch (sdata->vif.type) {
221         case NL80211_IFTYPE_AP:
222         case NL80211_IFTYPE_ADHOC:
223         case NL80211_IFTYPE_WDS:
224         case NL80211_IFTYPE_MESH_POINT:
225                 break;
226         default:
227                 /* do not warn to simplify caller in scan.c */
228                 changed &= ~BSS_CHANGED_BEACON_ENABLED;
229                 if (WARN_ON(changed & BSS_CHANGED_BEACON))
230                         return;
231                 break;
232         }
233
234         if (changed & BSS_CHANGED_BEACON_ENABLED) {
235                 if (local->quiescing || !ieee80211_sdata_running(sdata) ||
236                     test_bit(SCAN_SW_SCANNING, &local->scanning)) {
237                         sdata->vif.bss_conf.enable_beacon = false;
238                 } else {
239                         /*
240                          * Beacon should be enabled, but AP mode must
241                          * check whether there is a beacon configured.
242                          */
243                         switch (sdata->vif.type) {
244                         case NL80211_IFTYPE_AP:
245                                 sdata->vif.bss_conf.enable_beacon =
246                                         !!sdata->u.ap.beacon;
247                                 break;
248                         case NL80211_IFTYPE_ADHOC:
249                                 sdata->vif.bss_conf.enable_beacon =
250                                         !!sdata->u.ibss.presp;
251                                 break;
252 #ifdef CONFIG_MAC80211_MESH
253                         case NL80211_IFTYPE_MESH_POINT:
254                                 sdata->vif.bss_conf.enable_beacon =
255                                         !!sdata->u.mesh.mesh_id_len;
256                                 break;
257 #endif
258                         default:
259                                 /* not reached */
260                                 WARN_ON(1);
261                                 break;
262                         }
263                 }
264         }
265
266         drv_bss_info_changed(local, sdata, &sdata->vif.bss_conf, changed);
267 }
268
269 u32 ieee80211_reset_erp_info(struct ieee80211_sub_if_data *sdata)
270 {
271         sdata->vif.bss_conf.use_cts_prot = false;
272         sdata->vif.bss_conf.use_short_preamble = false;
273         sdata->vif.bss_conf.use_short_slot = false;
274         return BSS_CHANGED_ERP_CTS_PROT |
275                BSS_CHANGED_ERP_PREAMBLE |
276                BSS_CHANGED_ERP_SLOT;
277 }
278
279 static void ieee80211_tasklet_handler(unsigned long data)
280 {
281         struct ieee80211_local *local = (struct ieee80211_local *) data;
282         struct sta_info *sta, *tmp;
283         struct skb_eosp_msg_data *eosp_data;
284         struct sk_buff *skb;
285
286         while ((skb = skb_dequeue(&local->skb_queue)) ||
287                (skb = skb_dequeue(&local->skb_queue_unreliable))) {
288                 switch (skb->pkt_type) {
289                 case IEEE80211_RX_MSG:
290                         /* Clear skb->pkt_type in order to not confuse kernel
291                          * netstack. */
292                         skb->pkt_type = 0;
293                         ieee80211_rx(local_to_hw(local), skb);
294                         break;
295                 case IEEE80211_TX_STATUS_MSG:
296                         skb->pkt_type = 0;
297                         ieee80211_tx_status(local_to_hw(local), skb);
298                         break;
299                 case IEEE80211_EOSP_MSG:
300                         eosp_data = (void *)skb->cb;
301                         for_each_sta_info(local, eosp_data->sta, sta, tmp) {
302                                 /* skip wrong virtual interface */
303                                 if (memcmp(eosp_data->iface,
304                                            sta->sdata->vif.addr, ETH_ALEN))
305                                         continue;
306                                 clear_sta_flag(sta, WLAN_STA_SP);
307                                 break;
308                         }
309                         dev_kfree_skb(skb);
310                         break;
311                 default:
312                         WARN(1, "mac80211: Packet is of unknown type %d\n",
313                              skb->pkt_type);
314                         dev_kfree_skb(skb);
315                         break;
316                 }
317         }
318 }
319
320 static void ieee80211_restart_work(struct work_struct *work)
321 {
322         struct ieee80211_local *local =
323                 container_of(work, struct ieee80211_local, restart_work);
324
325         /* wait for scan work complete */
326         flush_workqueue(local->workqueue);
327
328         mutex_lock(&local->mtx);
329         WARN(test_bit(SCAN_HW_SCANNING, &local->scanning) ||
330              local->sched_scanning,
331                 "%s called with hardware scan in progress\n", __func__);
332         mutex_unlock(&local->mtx);
333
334         rtnl_lock();
335         ieee80211_scan_cancel(local);
336         ieee80211_reconfig(local);
337         rtnl_unlock();
338 }
339
340 void ieee80211_restart_hw(struct ieee80211_hw *hw)
341 {
342         struct ieee80211_local *local = hw_to_local(hw);
343
344         trace_api_restart_hw(local);
345
346         wiphy_info(hw->wiphy,
347                    "Hardware restart was requested\n");
348
349         /* use this reason, ieee80211_reconfig will unblock it */
350         ieee80211_stop_queues_by_reason(hw,
351                 IEEE80211_QUEUE_STOP_REASON_SUSPEND);
352
353         schedule_work(&local->restart_work);
354 }
355 EXPORT_SYMBOL(ieee80211_restart_hw);
356
357 static void ieee80211_recalc_smps_work(struct work_struct *work)
358 {
359         struct ieee80211_local *local =
360                 container_of(work, struct ieee80211_local, recalc_smps);
361
362         mutex_lock(&local->iflist_mtx);
363         ieee80211_recalc_smps(local);
364         mutex_unlock(&local->iflist_mtx);
365 }
366
367 #ifdef CONFIG_INET
368 static int ieee80211_ifa_changed(struct notifier_block *nb,
369                                  unsigned long data, void *arg)
370 {
371         struct in_ifaddr *ifa = arg;
372         struct ieee80211_local *local =
373                 container_of(nb, struct ieee80211_local,
374                              ifa_notifier);
375         struct net_device *ndev = ifa->ifa_dev->dev;
376         struct wireless_dev *wdev = ndev->ieee80211_ptr;
377         struct in_device *idev;
378         struct ieee80211_sub_if_data *sdata;
379         struct ieee80211_bss_conf *bss_conf;
380         struct ieee80211_if_managed *ifmgd;
381         int c = 0;
382
383         /* Make sure it's our interface that got changed */
384         if (!wdev)
385                 return NOTIFY_DONE;
386
387         if (wdev->wiphy != local->hw.wiphy)
388                 return NOTIFY_DONE;
389
390         sdata = IEEE80211_DEV_TO_SUB_IF(ndev);
391         bss_conf = &sdata->vif.bss_conf;
392
393         if (!ieee80211_sdata_running(sdata))
394                 return NOTIFY_DONE;
395
396         /* ARP filtering is only supported in managed mode */
397         if (sdata->vif.type != NL80211_IFTYPE_STATION)
398                 return NOTIFY_DONE;
399
400         idev = __in_dev_get_rtnl(sdata->dev);
401         if (!idev)
402                 return NOTIFY_DONE;
403
404         ifmgd = &sdata->u.mgd;
405         mutex_lock(&ifmgd->mtx);
406
407         /* Copy the addresses to the bss_conf list */
408         ifa = idev->ifa_list;
409         while (c < IEEE80211_BSS_ARP_ADDR_LIST_LEN && ifa) {
410                 bss_conf->arp_addr_list[c] = ifa->ifa_address;
411                 ifa = ifa->ifa_next;
412                 c++;
413         }
414
415         /* If not all addresses fit the list, disable filtering */
416         if (ifa) {
417                 sdata->arp_filter_state = false;
418                 c = 0;
419         } else {
420                 sdata->arp_filter_state = true;
421         }
422         bss_conf->arp_addr_cnt = c;
423
424         /* Configure driver only if associated */
425         if (ifmgd->associated) {
426                 bss_conf->arp_filter_enabled = sdata->arp_filter_state;
427                 ieee80211_bss_info_change_notify(sdata,
428                                                  BSS_CHANGED_ARP_FILTER);
429         }
430
431         mutex_unlock(&ifmgd->mtx);
432
433         return NOTIFY_DONE;
434 }
435 #endif
436
437 static int ieee80211_napi_poll(struct napi_struct *napi, int budget)
438 {
439         struct ieee80211_local *local =
440                 container_of(napi, struct ieee80211_local, napi);
441
442         return local->ops->napi_poll(&local->hw, budget);
443 }
444
445 void ieee80211_napi_schedule(struct ieee80211_hw *hw)
446 {
447         struct ieee80211_local *local = hw_to_local(hw);
448
449         napi_schedule(&local->napi);
450 }
451 EXPORT_SYMBOL(ieee80211_napi_schedule);
452
453 void ieee80211_napi_complete(struct ieee80211_hw *hw)
454 {
455         struct ieee80211_local *local = hw_to_local(hw);
456
457         napi_complete(&local->napi);
458 }
459 EXPORT_SYMBOL(ieee80211_napi_complete);
460
461 /* There isn't a lot of sense in it, but you can transmit anything you like */
462 static const struct ieee80211_txrx_stypes
463 ieee80211_default_mgmt_stypes[NUM_NL80211_IFTYPES] = {
464         [NL80211_IFTYPE_ADHOC] = {
465                 .tx = 0xffff,
466                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4),
467         },
468         [NL80211_IFTYPE_STATION] = {
469                 .tx = 0xffff,
470                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
471                         BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
472         },
473         [NL80211_IFTYPE_AP] = {
474                 .tx = 0xffff,
475                 .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
476                         BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
477                         BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
478                         BIT(IEEE80211_STYPE_DISASSOC >> 4) |
479                         BIT(IEEE80211_STYPE_AUTH >> 4) |
480                         BIT(IEEE80211_STYPE_DEAUTH >> 4) |
481                         BIT(IEEE80211_STYPE_ACTION >> 4),
482         },
483         [NL80211_IFTYPE_AP_VLAN] = {
484                 /* copy AP */
485                 .tx = 0xffff,
486                 .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
487                         BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
488                         BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
489                         BIT(IEEE80211_STYPE_DISASSOC >> 4) |
490                         BIT(IEEE80211_STYPE_AUTH >> 4) |
491                         BIT(IEEE80211_STYPE_DEAUTH >> 4) |
492                         BIT(IEEE80211_STYPE_ACTION >> 4),
493         },
494         [NL80211_IFTYPE_P2P_CLIENT] = {
495                 .tx = 0xffff,
496                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
497                         BIT(IEEE80211_STYPE_PROBE_REQ >> 4),
498         },
499         [NL80211_IFTYPE_P2P_GO] = {
500                 .tx = 0xffff,
501                 .rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
502                         BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
503                         BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
504                         BIT(IEEE80211_STYPE_DISASSOC >> 4) |
505                         BIT(IEEE80211_STYPE_AUTH >> 4) |
506                         BIT(IEEE80211_STYPE_DEAUTH >> 4) |
507                         BIT(IEEE80211_STYPE_ACTION >> 4),
508         },
509         [NL80211_IFTYPE_MESH_POINT] = {
510                 .tx = 0xffff,
511                 .rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
512                         BIT(IEEE80211_STYPE_AUTH >> 4) |
513                         BIT(IEEE80211_STYPE_DEAUTH >> 4),
514         },
515 };
516
517 struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
518                                         const struct ieee80211_ops *ops)
519 {
520         struct ieee80211_local *local;
521         int priv_size, i;
522         struct wiphy *wiphy;
523
524         /* Ensure 32-byte alignment of our private data and hw private data.
525          * We use the wiphy priv data for both our ieee80211_local and for
526          * the driver's private data
527          *
528          * In memory it'll be like this:
529          *
530          * +-------------------------+
531          * | struct wiphy           |
532          * +-------------------------+
533          * | struct ieee80211_local  |
534          * +-------------------------+
535          * | driver's private data   |
536          * +-------------------------+
537          *
538          */
539         priv_size = ALIGN(sizeof(*local), NETDEV_ALIGN) + priv_data_len;
540
541         wiphy = wiphy_new(&mac80211_config_ops, priv_size);
542
543         if (!wiphy)
544                 return NULL;
545
546         wiphy->mgmt_stypes = ieee80211_default_mgmt_stypes;
547
548         wiphy->privid = mac80211_wiphy_privid;
549
550         wiphy->flags |= WIPHY_FLAG_NETNS_OK |
551                         WIPHY_FLAG_4ADDR_AP |
552                         WIPHY_FLAG_4ADDR_STATION;
553
554         if (!ops->set_key)
555                 wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
556
557         wiphy->bss_priv_size = sizeof(struct ieee80211_bss);
558
559         local = wiphy_priv(wiphy);
560
561         local->hw.wiphy = wiphy;
562
563         local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
564
565         BUG_ON(!ops->tx);
566         BUG_ON(!ops->start);
567         BUG_ON(!ops->stop);
568         BUG_ON(!ops->config);
569         BUG_ON(!ops->add_interface);
570         BUG_ON(!ops->remove_interface);
571         BUG_ON(!ops->configure_filter);
572         local->ops = ops;
573
574         /* set up some defaults */
575         local->hw.queues = 1;
576         local->hw.max_rates = 1;
577         local->hw.max_report_rates = 0;
578         local->hw.max_rx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
579         local->hw.max_tx_aggregation_subframes = IEEE80211_MAX_AMPDU_BUF;
580         local->hw.conf.long_frame_max_tx_count = wiphy->retry_long;
581         local->hw.conf.short_frame_max_tx_count = wiphy->retry_short;
582         local->user_power_level = -1;
583         local->uapsd_queues = IEEE80211_DEFAULT_UAPSD_QUEUES;
584         local->uapsd_max_sp_len = IEEE80211_DEFAULT_MAX_SP_LEN;
585
586         INIT_LIST_HEAD(&local->interfaces);
587
588         __hw_addr_init(&local->mc_list);
589
590         mutex_init(&local->iflist_mtx);
591         mutex_init(&local->mtx);
592
593         mutex_init(&local->key_mtx);
594         spin_lock_init(&local->filter_lock);
595         spin_lock_init(&local->queue_stop_reason_lock);
596
597         /*
598          * The rx_skb_queue is only accessed from tasklets,
599          * but other SKB queues are used from within IRQ
600          * context. Therefore, this one needs a different
601          * locking class so our direct, non-irq-safe use of
602          * the queue's lock doesn't throw lockdep warnings.
603          */
604         skb_queue_head_init_class(&local->rx_skb_queue,
605                                   &ieee80211_rx_skb_queue_class);
606
607         INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
608
609         ieee80211_work_init(local);
610
611         INIT_WORK(&local->restart_work, ieee80211_restart_work);
612
613         INIT_WORK(&local->reconfig_filter, ieee80211_reconfig_filter);
614         INIT_WORK(&local->recalc_smps, ieee80211_recalc_smps_work);
615         local->smps_mode = IEEE80211_SMPS_OFF;
616
617         INIT_WORK(&local->dynamic_ps_enable_work,
618                   ieee80211_dynamic_ps_enable_work);
619         INIT_WORK(&local->dynamic_ps_disable_work,
620                   ieee80211_dynamic_ps_disable_work);
621         setup_timer(&local->dynamic_ps_timer,
622                     ieee80211_dynamic_ps_timer, (unsigned long) local);
623
624         INIT_WORK(&local->sched_scan_stopped_work,
625                   ieee80211_sched_scan_stopped_work);
626
627         sta_info_init(local);
628
629         for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
630                 skb_queue_head_init(&local->pending[i]);
631                 atomic_set(&local->agg_queue_stop[i], 0);
632         }
633         tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
634                      (unsigned long)local);
635
636         tasklet_init(&local->tasklet,
637                      ieee80211_tasklet_handler,
638                      (unsigned long) local);
639
640         skb_queue_head_init(&local->skb_queue);
641         skb_queue_head_init(&local->skb_queue_unreliable);
642
643         /* init dummy netdev for use w/ NAPI */
644         init_dummy_netdev(&local->napi_dev);
645
646         ieee80211_led_names(local);
647
648         ieee80211_hw_roc_setup(local);
649
650         return local_to_hw(local);
651 }
652 EXPORT_SYMBOL(ieee80211_alloc_hw);
653
654 int ieee80211_register_hw(struct ieee80211_hw *hw)
655 {
656         struct ieee80211_local *local = hw_to_local(hw);
657         int result, i;
658         enum ieee80211_band band;
659         int channels, max_bitrates;
660         bool supp_ht;
661         static const u32 cipher_suites[] = {
662                 /* keep WEP first, it may be removed below */
663                 WLAN_CIPHER_SUITE_WEP40,
664                 WLAN_CIPHER_SUITE_WEP104,
665                 WLAN_CIPHER_SUITE_TKIP,
666                 WLAN_CIPHER_SUITE_CCMP,
667
668                 /* keep last -- depends on hw flags! */
669                 WLAN_CIPHER_SUITE_AES_CMAC
670         };
671
672         if ((hw->wiphy->wowlan.flags || hw->wiphy->wowlan.n_patterns)
673 #ifdef CONFIG_PM
674             && (!local->ops->suspend || !local->ops->resume)
675 #endif
676             )
677                 return -EINVAL;
678
679         if (hw->max_report_rates == 0)
680                 hw->max_report_rates = hw->max_rates;
681
682         /*
683          * generic code guarantees at least one band,
684          * set this very early because much code assumes
685          * that hw.conf.channel is assigned
686          */
687         channels = 0;
688         max_bitrates = 0;
689         supp_ht = false;
690         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
691                 struct ieee80211_supported_band *sband;
692
693                 sband = local->hw.wiphy->bands[band];
694                 if (!sband)
695                         continue;
696                 if (!local->oper_channel) {
697                         /* init channel we're on */
698                         local->hw.conf.channel =
699                         local->oper_channel = &sband->channels[0];
700                         local->hw.conf.channel_type = NL80211_CHAN_NO_HT;
701                 }
702                 channels += sband->n_channels;
703
704                 if (max_bitrates < sband->n_bitrates)
705                         max_bitrates = sband->n_bitrates;
706                 supp_ht = supp_ht || sband->ht_cap.ht_supported;
707         }
708
709         local->int_scan_req = kzalloc(sizeof(*local->int_scan_req) +
710                                       sizeof(void *) * channels, GFP_KERNEL);
711         if (!local->int_scan_req)
712                 return -ENOMEM;
713
714         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
715                 if (!local->hw.wiphy->bands[band])
716                         continue;
717                 local->int_scan_req->rates[band] = (u32) -1;
718         }
719
720         /* if low-level driver supports AP, we also support VLAN */
721         if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
722                 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
723                 hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_AP_VLAN);
724         }
725
726         /* mac80211 always supports monitor */
727         hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
728         hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_MONITOR);
729
730         /*
731          * mac80211 doesn't support more than 1 channel, and also not more
732          * than one IBSS interface
733          */
734         for (i = 0; i < hw->wiphy->n_iface_combinations; i++) {
735                 const struct ieee80211_iface_combination *c;
736                 int j;
737
738                 c = &hw->wiphy->iface_combinations[i];
739
740                 if (c->num_different_channels > 1)
741                         return -EINVAL;
742
743                 for (j = 0; j < c->n_limits; j++)
744                         if ((c->limits[j].types & BIT(NL80211_IFTYPE_ADHOC)) &&
745                             c->limits[j].max > 1)
746                                 return -EINVAL;
747         }
748
749 #ifndef CONFIG_MAC80211_MESH
750         /* mesh depends on Kconfig, but drivers should set it if they want */
751         local->hw.wiphy->interface_modes &= ~BIT(NL80211_IFTYPE_MESH_POINT);
752 #endif
753
754         /* if the underlying driver supports mesh, mac80211 will (at least)
755          * provide routing of mesh authentication frames to userspace */
756         if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_MESH_POINT))
757                 local->hw.wiphy->flags |= WIPHY_FLAG_MESH_AUTH;
758
759         /* mac80211 supports control port protocol changing */
760         local->hw.wiphy->flags |= WIPHY_FLAG_CONTROL_PORT_PROTOCOL;
761
762         if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
763                 local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
764         else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
765                 local->hw.wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
766
767         WARN((local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)
768              && (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK),
769              "U-APSD not supported with HW_PS_NULLFUNC_STACK\n");
770
771         /*
772          * Calculate scan IE length -- we need this to alloc
773          * memory and to subtract from the driver limit. It
774          * includes the DS Params, (extended) supported rates, and HT
775          * information -- SSID is the driver's responsibility.
776          */
777         local->scan_ies_len = 4 + max_bitrates /* (ext) supp rates */ +
778                 3 /* DS Params */;
779         if (supp_ht)
780                 local->scan_ies_len += 2 + sizeof(struct ieee80211_ht_cap);
781
782         if (!local->ops->hw_scan) {
783                 /* For hw_scan, driver needs to set these up. */
784                 local->hw.wiphy->max_scan_ssids = 4;
785                 local->hw.wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
786         }
787
788         /*
789          * If the driver supports any scan IEs, then assume the
790          * limit includes the IEs mac80211 will add, otherwise
791          * leave it at zero and let the driver sort it out; we
792          * still pass our IEs to the driver but userspace will
793          * not be allowed to in that case.
794          */
795         if (local->hw.wiphy->max_scan_ie_len)
796                 local->hw.wiphy->max_scan_ie_len -= local->scan_ies_len;
797
798         /* Set up cipher suites unless driver already did */
799         if (!local->hw.wiphy->cipher_suites) {
800                 local->hw.wiphy->cipher_suites = cipher_suites;
801                 local->hw.wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
802                 if (!(local->hw.flags & IEEE80211_HW_MFP_CAPABLE))
803                         local->hw.wiphy->n_cipher_suites--;
804         }
805         if (IS_ERR(local->wep_tx_tfm) || IS_ERR(local->wep_rx_tfm)) {
806                 if (local->hw.wiphy->cipher_suites == cipher_suites) {
807                         local->hw.wiphy->cipher_suites += 2;
808                         local->hw.wiphy->n_cipher_suites -= 2;
809                 } else {
810                         u32 *suites;
811                         int r, w = 0;
812
813                         /* Filter out WEP */
814
815                         suites = kmemdup(
816                                 local->hw.wiphy->cipher_suites,
817                                 sizeof(u32) * local->hw.wiphy->n_cipher_suites,
818                                 GFP_KERNEL);
819                         if (!suites)
820                                 return -ENOMEM;
821                         for (r = 0; r < local->hw.wiphy->n_cipher_suites; r++) {
822                                 u32 suite = local->hw.wiphy->cipher_suites[r];
823                                 if (suite == WLAN_CIPHER_SUITE_WEP40 ||
824                                     suite == WLAN_CIPHER_SUITE_WEP104)
825                                         continue;
826                                 suites[w++] = suite;
827                         }
828                         local->hw.wiphy->cipher_suites = suites;
829                         local->hw.wiphy->n_cipher_suites = w;
830                         local->wiphy_ciphers_allocated = true;
831                 }
832         }
833
834         if (!local->ops->remain_on_channel)
835                 local->hw.wiphy->max_remain_on_channel_duration = 5000;
836
837         if (local->ops->sched_scan_start)
838                 local->hw.wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
839
840         /* mac80211 based drivers don't support internal TDLS setup */
841         if (local->hw.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS)
842                 local->hw.wiphy->flags |= WIPHY_FLAG_TDLS_EXTERNAL_SETUP;
843
844         result = wiphy_register(local->hw.wiphy);
845         if (result < 0)
846                 goto fail_wiphy_register;
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
855         local->workqueue =
856                 alloc_ordered_workqueue(wiphy_name(local->hw.wiphy), 0);
857         if (!local->workqueue) {
858                 result = -ENOMEM;
859                 goto fail_workqueue;
860         }
861
862         /*
863          * The hardware needs headroom for sending the frame,
864          * and we need some headroom for passing the frame to monitor
865          * interfaces, but never both at the same time.
866          */
867         local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
868                                    IEEE80211_TX_STATUS_HEADROOM);
869
870         debugfs_hw_add(local);
871
872         /*
873          * if the driver doesn't specify a max listen interval we
874          * use 5 which should be a safe default
875          */
876         if (local->hw.max_listen_interval == 0)
877                 local->hw.max_listen_interval = 5;
878
879         local->hw.conf.listen_interval = local->hw.max_listen_interval;
880
881         local->dynamic_ps_forced_timeout = -1;
882
883         result = ieee80211_wep_init(local);
884         if (result < 0)
885                 wiphy_debug(local->hw.wiphy, "Failed to initialize wep: %d\n",
886                             result);
887
888         ieee80211_led_init(local);
889
890         rtnl_lock();
891
892         result = ieee80211_init_rate_ctrl_alg(local,
893                                               hw->rate_control_algorithm);
894         if (result < 0) {
895                 wiphy_debug(local->hw.wiphy,
896                             "Failed to initialize rate control algorithm\n");
897                 goto fail_rate;
898         }
899
900         /* add one default STA interface if supported */
901         if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_STATION)) {
902                 result = ieee80211_if_add(local, "wlan%d", NULL,
903                                           NL80211_IFTYPE_STATION, NULL);
904                 if (result)
905                         wiphy_warn(local->hw.wiphy,
906                                    "Failed to add default virtual iface\n");
907         }
908
909         rtnl_unlock();
910
911         local->network_latency_notifier.notifier_call =
912                 ieee80211_max_network_latency;
913         result = pm_qos_add_notifier(PM_QOS_NETWORK_LATENCY,
914                                      &local->network_latency_notifier);
915         if (result) {
916                 rtnl_lock();
917                 goto fail_pm_qos;
918         }
919
920 #ifdef CONFIG_INET
921         local->ifa_notifier.notifier_call = ieee80211_ifa_changed;
922         result = register_inetaddr_notifier(&local->ifa_notifier);
923         if (result)
924                 goto fail_ifa;
925 #endif
926
927         netif_napi_add(&local->napi_dev, &local->napi, ieee80211_napi_poll,
928                         local->hw.napi_weight);
929
930         return 0;
931
932 #ifdef CONFIG_INET
933  fail_ifa:
934         pm_qos_remove_notifier(PM_QOS_NETWORK_LATENCY,
935                                &local->network_latency_notifier);
936         rtnl_lock();
937 #endif
938  fail_pm_qos:
939         ieee80211_led_exit(local);
940         ieee80211_remove_interfaces(local);
941  fail_rate:
942         rtnl_unlock();
943         ieee80211_wep_free(local);
944         sta_info_stop(local);
945         destroy_workqueue(local->workqueue);
946  fail_workqueue:
947         wiphy_unregister(local->hw.wiphy);
948  fail_wiphy_register:
949         if (local->wiphy_ciphers_allocated)
950                 kfree(local->hw.wiphy->cipher_suites);
951         kfree(local->int_scan_req);
952         return result;
953 }
954 EXPORT_SYMBOL(ieee80211_register_hw);
955
956 void ieee80211_unregister_hw(struct ieee80211_hw *hw)
957 {
958         struct ieee80211_local *local = hw_to_local(hw);
959
960         tasklet_kill(&local->tx_pending_tasklet);
961         tasklet_kill(&local->tasklet);
962
963         pm_qos_remove_notifier(PM_QOS_NETWORK_LATENCY,
964                                &local->network_latency_notifier);
965 #ifdef CONFIG_INET
966         unregister_inetaddr_notifier(&local->ifa_notifier);
967 #endif
968
969         rtnl_lock();
970
971         /*
972          * At this point, interface list manipulations are fine
973          * because the driver cannot be handing us frames any
974          * more and the tasklet is killed.
975          */
976         ieee80211_remove_interfaces(local);
977
978         rtnl_unlock();
979
980         /*
981          * Now all work items will be gone, but the
982          * timer might still be armed, so delete it
983          */
984         del_timer_sync(&local->work_timer);
985
986         cancel_work_sync(&local->restart_work);
987         cancel_work_sync(&local->reconfig_filter);
988
989         ieee80211_clear_tx_pending(local);
990         rate_control_deinitialize(local);
991
992         if (skb_queue_len(&local->skb_queue) ||
993             skb_queue_len(&local->skb_queue_unreliable))
994                 wiphy_warn(local->hw.wiphy, "skb_queue not empty\n");
995         skb_queue_purge(&local->skb_queue);
996         skb_queue_purge(&local->skb_queue_unreliable);
997         skb_queue_purge(&local->rx_skb_queue);
998
999         destroy_workqueue(local->workqueue);
1000         wiphy_unregister(local->hw.wiphy);
1001         sta_info_stop(local);
1002         ieee80211_wep_free(local);
1003         ieee80211_led_exit(local);
1004         kfree(local->int_scan_req);
1005 }
1006 EXPORT_SYMBOL(ieee80211_unregister_hw);
1007
1008 void ieee80211_free_hw(struct ieee80211_hw *hw)
1009 {
1010         struct ieee80211_local *local = hw_to_local(hw);
1011
1012         mutex_destroy(&local->iflist_mtx);
1013         mutex_destroy(&local->mtx);
1014
1015         if (local->wiphy_ciphers_allocated)
1016                 kfree(local->hw.wiphy->cipher_suites);
1017
1018         wiphy_free(local->hw.wiphy);
1019 }
1020 EXPORT_SYMBOL(ieee80211_free_hw);
1021
1022 static int __init ieee80211_init(void)
1023 {
1024         struct sk_buff *skb;
1025         int ret;
1026
1027         BUILD_BUG_ON(sizeof(struct ieee80211_tx_info) > sizeof(skb->cb));
1028         BUILD_BUG_ON(offsetof(struct ieee80211_tx_info, driver_data) +
1029                      IEEE80211_TX_INFO_DRIVER_DATA_SIZE > sizeof(skb->cb));
1030
1031         ret = rc80211_minstrel_init();
1032         if (ret)
1033                 return ret;
1034
1035         ret = rc80211_minstrel_ht_init();
1036         if (ret)
1037                 goto err_minstrel;
1038
1039         ret = rc80211_pid_init();
1040         if (ret)
1041                 goto err_pid;
1042
1043         ret = ieee80211_iface_init();
1044         if (ret)
1045                 goto err_netdev;
1046
1047         return 0;
1048  err_netdev:
1049         rc80211_pid_exit();
1050  err_pid:
1051         rc80211_minstrel_ht_exit();
1052  err_minstrel:
1053         rc80211_minstrel_exit();
1054
1055         return ret;
1056 }
1057
1058 static void __exit ieee80211_exit(void)
1059 {
1060         rc80211_pid_exit();
1061         rc80211_minstrel_ht_exit();
1062         rc80211_minstrel_exit();
1063
1064         if (mesh_allocated)
1065                 ieee80211s_stop();
1066
1067         ieee80211_iface_exit();
1068
1069         rcu_barrier();
1070 }
1071
1072
1073 subsys_initcall(ieee80211_init);
1074 module_exit(ieee80211_exit);
1075
1076 MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1077 MODULE_LICENSE("GPL");