blob: 78fd91895c80ab1a0e0f805fec3058db0370a6cf [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
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>
Jiri Bencf0706e82007-05-05 11:45:53 -070023#include <linux/bitmap.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070024#include <net/net_namespace.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070025#include <net/cfg80211.h>
26
Jiri Bencf0706e82007-05-05 11:45:53 -070027#include "ieee80211_i.h"
28#include "ieee80211_rate.h"
29#include "wep.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070030#include "wme.h"
31#include "aes_ccm.h"
32#include "ieee80211_led.h"
Michael Wue0eb6852007-09-18 17:29:21 -040033#include "cfg.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070034#include "debugfs.h"
35#include "debugfs_netdev.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070036
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +020037#define SUPP_MCS_SET_LEN 16
38
Johannes Bergb306f452007-07-10 19:32:08 +020039/*
40 * For seeing transmitted packets on monitor interfaces
41 * we have a radiotap header too.
42 */
43struct ieee80211_tx_status_rtap_hdr {
44 struct ieee80211_radiotap_header hdr;
45 __le16 tx_flags;
46 u8 data_retries;
47} __attribute__ ((packed));
48
Johannes Bergb2c258f2007-07-27 15:43:23 +020049/* common interface routines */
Jiri Bencf0706e82007-05-05 11:45:53 -070050
Stephen Hemmingerb95cce32007-09-26 22:13:38 -070051static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
Jiri Bencf0706e82007-05-05 11:45:53 -070052{
Johannes Bergb2c258f2007-07-27 15:43:23 +020053 memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
54 return ETH_ALEN;
Jiri Bencf0706e82007-05-05 11:45:53 -070055}
56
Johannes Berg4150c572007-09-17 01:29:23 -040057/* must be called under mdev tx lock */
58static void ieee80211_configure_filter(struct ieee80211_local *local)
59{
60 unsigned int changed_flags;
61 unsigned int new_flags = 0;
62
Johannes Berg53918992007-09-26 15:19:47 +020063 if (atomic_read(&local->iff_promiscs))
Johannes Berg4150c572007-09-17 01:29:23 -040064 new_flags |= FIF_PROMISC_IN_BSS;
65
Johannes Berg53918992007-09-26 15:19:47 +020066 if (atomic_read(&local->iff_allmultis))
Johannes Berg4150c572007-09-17 01:29:23 -040067 new_flags |= FIF_ALLMULTI;
68
69 if (local->monitors)
Michael Wu8cc9a732008-01-31 19:48:23 +010070 new_flags |= FIF_BCN_PRBRESP_PROMISC;
71
72 if (local->fif_fcsfail)
73 new_flags |= FIF_FCSFAIL;
74
75 if (local->fif_plcpfail)
76 new_flags |= FIF_PLCPFAIL;
77
78 if (local->fif_control)
79 new_flags |= FIF_CONTROL;
80
81 if (local->fif_other_bss)
82 new_flags |= FIF_OTHER_BSS;
Johannes Berg4150c572007-09-17 01:29:23 -040083
84 changed_flags = local->filter_flags ^ new_flags;
85
86 /* be a bit nasty */
87 new_flags |= (1<<31);
88
89 local->ops->configure_filter(local_to_hw(local),
90 changed_flags, &new_flags,
91 local->mdev->mc_count,
92 local->mdev->mc_list);
93
94 WARN_ON(new_flags & (1<<31));
95
96 local->filter_flags = new_flags & ~(1<<31);
97}
98
Johannes Bergb2c258f2007-07-27 15:43:23 +020099/* master interface */
Jiri Bencf0706e82007-05-05 11:45:53 -0700100
101static int ieee80211_master_open(struct net_device *dev)
102{
103 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
104 struct ieee80211_sub_if_data *sdata;
105 int res = -EOPNOTSUPP;
106
Johannes Berg79010422007-09-18 17:29:21 -0400107 /* we hold the RTNL here so can safely walk the list */
108 list_for_each_entry(sdata, &local->interfaces, list) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700109 if (sdata->dev != dev && netif_running(sdata->dev)) {
110 res = 0;
111 break;
112 }
113 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700114 return res;
115}
116
117static int ieee80211_master_stop(struct net_device *dev)
118{
119 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
120 struct ieee80211_sub_if_data *sdata;
121
Johannes Berg79010422007-09-18 17:29:21 -0400122 /* we hold the RTNL here so can safely walk the list */
123 list_for_each_entry(sdata, &local->interfaces, list)
Jiri Bencf0706e82007-05-05 11:45:53 -0700124 if (sdata->dev != dev && netif_running(sdata->dev))
125 dev_close(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700126
127 return 0;
128}
129
Johannes Berg4150c572007-09-17 01:29:23 -0400130static void ieee80211_master_set_multicast_list(struct net_device *dev)
131{
132 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
133
134 ieee80211_configure_filter(local);
135}
136
Johannes Bergb2c258f2007-07-27 15:43:23 +0200137/* regular interfaces */
138
139static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
140{
141 /* FIX: what would be proper limits for MTU?
142 * This interface uses 802.3 frames. */
143 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
144 printk(KERN_WARNING "%s: invalid MTU %d\n",
145 dev->name, new_mtu);
146 return -EINVAL;
147 }
148
149#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
150 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
151#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
152 dev->mtu = new_mtu;
153 return 0;
154}
155
156static inline int identical_mac_addr_allowed(int type1, int type2)
157{
158 return (type1 == IEEE80211_IF_TYPE_MNTR ||
159 type2 == IEEE80211_IF_TYPE_MNTR ||
160 (type1 == IEEE80211_IF_TYPE_AP &&
161 type2 == IEEE80211_IF_TYPE_WDS) ||
162 (type1 == IEEE80211_IF_TYPE_WDS &&
163 (type2 == IEEE80211_IF_TYPE_WDS ||
164 type2 == IEEE80211_IF_TYPE_AP)) ||
165 (type1 == IEEE80211_IF_TYPE_AP &&
166 type2 == IEEE80211_IF_TYPE_VLAN) ||
167 (type1 == IEEE80211_IF_TYPE_VLAN &&
168 (type2 == IEEE80211_IF_TYPE_AP ||
169 type2 == IEEE80211_IF_TYPE_VLAN)));
170}
171
Johannes Bergb2c258f2007-07-27 15:43:23 +0200172static int ieee80211_open(struct net_device *dev)
173{
174 struct ieee80211_sub_if_data *sdata, *nsdata;
175 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
176 struct ieee80211_if_init_conf conf;
177 int res;
Michael Bueschceffefd2008-02-10 14:16:52 +0100178 bool need_hw_reconfig = 0;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200179
180 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400181
Johannes Berg79010422007-09-18 17:29:21 -0400182 /* we hold the RTNL here so can safely walk the list */
183 list_for_each_entry(nsdata, &local->interfaces, list) {
Johannes Bergb2c258f2007-07-27 15:43:23 +0200184 struct net_device *ndev = nsdata->dev;
185
186 if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400187 compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0) {
188 /*
189 * check whether it may have the same address
190 */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100191 if (!identical_mac_addr_allowed(sdata->vif.type,
192 nsdata->vif.type))
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400193 return -ENOTUNIQ;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400194
195 /*
196 * can only add VLANs to enabled APs
197 */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100198 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN &&
199 nsdata->vif.type == IEEE80211_IF_TYPE_AP &&
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400200 netif_running(nsdata->dev))
201 sdata->u.vlan.ap = nsdata;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200202 }
203 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200204
Johannes Berg51fb61e2007-12-19 01:31:27 +0100205 switch (sdata->vif.type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400206 case IEEE80211_IF_TYPE_WDS:
207 if (is_zero_ether_addr(sdata->u.wds.remote_addr))
208 return -ENOLINK;
209 break;
210 case IEEE80211_IF_TYPE_VLAN:
211 if (!sdata->u.vlan.ap)
212 return -ENOLINK;
213 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +0200214 case IEEE80211_IF_TYPE_AP:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +0200215 case IEEE80211_IF_TYPE_STA:
216 case IEEE80211_IF_TYPE_MNTR:
217 case IEEE80211_IF_TYPE_IBSS:
218 /* no special treatment */
219 break;
Johannes Berga2897552007-09-28 14:01:25 +0200220 case IEEE80211_IF_TYPE_INVALID:
221 /* cannot happen */
222 WARN_ON(1);
223 break;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400224 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200225
Johannes Bergb2c258f2007-07-27 15:43:23 +0200226 if (local->open_count == 0) {
227 res = 0;
Johannes Berg4150c572007-09-17 01:29:23 -0400228 if (local->ops->start)
229 res = local->ops->start(local_to_hw(local));
230 if (res)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200231 return res;
Michael Bueschceffefd2008-02-10 14:16:52 +0100232 need_hw_reconfig = 1;
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100233 ieee80211_led_radio(local, local->hw.conf.radio_enabled);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200234 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200235
Johannes Berg51fb61e2007-12-19 01:31:27 +0100236 switch (sdata->vif.type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400237 case IEEE80211_IF_TYPE_VLAN:
238 list_add(&sdata->u.vlan.list, &sdata->u.vlan.ap->u.ap.vlans);
239 /* no need to tell driver */
240 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400241 case IEEE80211_IF_TYPE_MNTR:
242 /* must be before the call to ieee80211_configure_filter */
Johannes Bergb2c258f2007-07-27 15:43:23 +0200243 local->monitors++;
Michael Wu8cc9a732008-01-31 19:48:23 +0100244 if (local->monitors == 1)
Johannes Berg4150c572007-09-17 01:29:23 -0400245 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
Michael Wu8cc9a732008-01-31 19:48:23 +0100246
247 if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
248 local->fif_fcsfail++;
249 if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
250 local->fif_plcpfail++;
251 if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
252 local->fif_control++;
253 if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
254 local->fif_other_bss++;
255
256 netif_tx_lock_bh(local->mdev);
257 ieee80211_configure_filter(local);
258 netif_tx_unlock_bh(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -0400259 break;
260 case IEEE80211_IF_TYPE_STA:
261 case IEEE80211_IF_TYPE_IBSS:
262 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
263 /* fall through */
264 default:
Johannes Berg32bfd352007-12-19 01:31:26 +0100265 conf.vif = &sdata->vif;
Johannes Berg51fb61e2007-12-19 01:31:27 +0100266 conf.type = sdata->vif.type;
Johannes Berg4150c572007-09-17 01:29:23 -0400267 conf.mac_addr = dev->dev_addr;
268 res = local->ops->add_interface(local_to_hw(local), &conf);
269 if (res && !local->open_count && local->ops->stop)
270 local->ops->stop(local_to_hw(local));
271 if (res)
272 return res;
273
Johannes Bergb2c258f2007-07-27 15:43:23 +0200274 ieee80211_if_config(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200275 ieee80211_reset_erp_info(dev);
Johannes Berg11a843b2007-08-28 17:01:55 -0400276 ieee80211_enable_keys(sdata);
Johannes Berg4150c572007-09-17 01:29:23 -0400277
Johannes Berg51fb61e2007-12-19 01:31:27 +0100278 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Johannes Bergddd3d2b2007-09-26 17:53:20 +0200279 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
Johannes Berg4150c572007-09-17 01:29:23 -0400280 netif_carrier_off(dev);
281 else
282 netif_carrier_on(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200283 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200284
Johannes Berg4150c572007-09-17 01:29:23 -0400285 if (local->open_count == 0) {
286 res = dev_open(local->mdev);
287 WARN_ON(res);
Johannes Berg4150c572007-09-17 01:29:23 -0400288 tasklet_enable(&local->tx_pending_tasklet);
289 tasklet_enable(&local->tasklet);
290 }
291
Johannes Bergc1428b32007-11-16 02:54:53 +0100292 /*
293 * set_multicast_list will be invoked by the networking core
294 * which will check whether any increments here were done in
295 * error and sync them down to the hardware as filter flags.
296 */
297 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
298 atomic_inc(&local->iff_allmultis);
299
300 if (sdata->flags & IEEE80211_SDATA_PROMISC)
301 atomic_inc(&local->iff_promiscs);
302
Johannes Berg4150c572007-09-17 01:29:23 -0400303 local->open_count++;
Michael Bueschceffefd2008-02-10 14:16:52 +0100304 if (need_hw_reconfig)
305 ieee80211_hw_config(local);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200306
307 netif_start_queue(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400308
Johannes Bergb2c258f2007-07-27 15:43:23 +0200309 return 0;
310}
311
Johannes Berg4150c572007-09-17 01:29:23 -0400312static int ieee80211_stop(struct net_device *dev)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200313{
Johannes Berg4150c572007-09-17 01:29:23 -0400314 struct ieee80211_sub_if_data *sdata;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200315 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg4150c572007-09-17 01:29:23 -0400316 struct ieee80211_if_init_conf conf;
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200317 struct sta_info *sta;
318 int i;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200319
Johannes Berg4150c572007-09-17 01:29:23 -0400320 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
321
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200322 list_for_each_entry(sta, &local->sta_list, list) {
Ron Rindjunsky5b3d71d2008-01-13 18:21:58 +0200323 if (sta->dev == dev)
324 for (i = 0; i < STA_TID_NUM; i++)
325 ieee80211_sta_stop_rx_ba_session(sta->dev,
326 sta->addr, i,
327 WLAN_BACK_RECIPIENT,
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200328 WLAN_REASON_QSTA_LEAVE_QBSS);
329 }
330
Johannes Berg4150c572007-09-17 01:29:23 -0400331 netif_stop_queue(dev);
332
Johannes Bergc1428b32007-11-16 02:54:53 +0100333 /*
334 * Don't count this interface for promisc/allmulti while it
335 * is down. dev_mc_unsync() will invoke set_multicast_list
336 * on the master interface which will sync these down to the
337 * hardware as filter flags.
338 */
339 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
340 atomic_dec(&local->iff_allmultis);
341
342 if (sdata->flags & IEEE80211_SDATA_PROMISC)
343 atomic_dec(&local->iff_promiscs);
344
Johannes Berg4150c572007-09-17 01:29:23 -0400345 dev_mc_unsync(local->mdev, dev);
346
Johannes Berg5dfdaf52007-12-19 02:03:33 +0100347 /* APs need special treatment */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100348 if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400349 struct ieee80211_sub_if_data *vlan, *tmp;
Johannes Berg5dfdaf52007-12-19 02:03:33 +0100350 struct beacon_data *old_beacon = sdata->u.ap.beacon;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400351
Johannes Berg5dfdaf52007-12-19 02:03:33 +0100352 /* remove beacon */
353 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
354 synchronize_rcu();
355 kfree(old_beacon);
356
357 /* down all dependent devices, that is VLANs */
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400358 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
359 u.vlan.list)
360 dev_close(vlan->dev);
361 WARN_ON(!list_empty(&sdata->u.ap.vlans));
362 }
363
Johannes Berg4150c572007-09-17 01:29:23 -0400364 local->open_count--;
365
Johannes Berg51fb61e2007-12-19 01:31:27 +0100366 switch (sdata->vif.type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400367 case IEEE80211_IF_TYPE_VLAN:
368 list_del(&sdata->u.vlan.list);
369 sdata->u.vlan.ap = NULL;
370 /* no need to tell driver */
371 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400372 case IEEE80211_IF_TYPE_MNTR:
373 local->monitors--;
Michael Wu8cc9a732008-01-31 19:48:23 +0100374 if (local->monitors == 0)
Michael Wu8b393f12007-11-28 01:57:08 -0500375 local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
Michael Wu8cc9a732008-01-31 19:48:23 +0100376
377 if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
378 local->fif_fcsfail--;
379 if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
380 local->fif_plcpfail--;
381 if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
382 local->fif_control--;
383 if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
384 local->fif_other_bss--;
385
386 netif_tx_lock_bh(local->mdev);
387 ieee80211_configure_filter(local);
388 netif_tx_unlock_bh(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -0400389 break;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200390 case IEEE80211_IF_TYPE_STA:
391 case IEEE80211_IF_TYPE_IBSS:
392 sdata->u.sta.state = IEEE80211_DISABLED;
393 del_timer_sync(&sdata->u.sta.timer);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400394 /*
Johannes Berg79010422007-09-18 17:29:21 -0400395 * When we get here, the interface is marked down.
396 * Call synchronize_rcu() to wait for the RX path
397 * should it be using the interface and enqueuing
398 * frames at this very time on another CPU.
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400399 */
Johannes Berg79010422007-09-18 17:29:21 -0400400 synchronize_rcu();
Johannes Bergb2c258f2007-07-27 15:43:23 +0200401 skb_queue_purge(&sdata->u.sta.skb_queue);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400402
Zhu Yiece8edd2007-11-22 10:53:21 +0800403 if (local->scan_dev == sdata->dev) {
404 if (!local->ops->hw_scan) {
405 local->sta_sw_scanning = 0;
406 cancel_delayed_work(&local->scan_work);
407 } else
408 local->sta_hw_scanning = 0;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200409 }
Zhu Yiece8edd2007-11-22 10:53:21 +0800410
Johannes Bergb2c258f2007-07-27 15:43:23 +0200411 flush_workqueue(local->hw.workqueue);
Zhu Yia10605e2007-11-22 11:10:22 +0800412
413 sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
414 kfree(sdata->u.sta.extra_ie);
415 sdata->u.sta.extra_ie = NULL;
416 sdata->u.sta.extra_ie_len = 0;
Johannes Berg4150c572007-09-17 01:29:23 -0400417 /* fall through */
418 default:
Johannes Berg32bfd352007-12-19 01:31:26 +0100419 conf.vif = &sdata->vif;
Johannes Berg51fb61e2007-12-19 01:31:27 +0100420 conf.type = sdata->vif.type;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200421 conf.mac_addr = dev->dev_addr;
Johannes Berg4150c572007-09-17 01:29:23 -0400422 /* disable all keys for as long as this netdev is down */
423 ieee80211_disable_keys(sdata);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200424 local->ops->remove_interface(local_to_hw(local), &conf);
425 }
426
Johannes Berg4150c572007-09-17 01:29:23 -0400427 if (local->open_count == 0) {
428 if (netif_running(local->mdev))
429 dev_close(local->mdev);
430
Johannes Berg4150c572007-09-17 01:29:23 -0400431 if (local->ops->stop)
432 local->ops->stop(local_to_hw(local));
433
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100434 ieee80211_led_radio(local, 0);
435
Johannes Berg4150c572007-09-17 01:29:23 -0400436 tasklet_disable(&local->tx_pending_tasklet);
437 tasklet_disable(&local->tasklet);
438 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200439
440 return 0;
441}
442
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200443int ieee80211_start_tx_ba_session(struct ieee80211_hw *hw, u8 *ra, u16 tid)
444{
445 struct ieee80211_local *local = hw_to_local(hw);
446 struct sta_info *sta;
447 struct ieee80211_sub_if_data *sdata;
448 u16 start_seq_num = 0;
449 u8 *state;
450 int ret;
451 DECLARE_MAC_BUF(mac);
452
453 if (tid >= STA_TID_NUM)
454 return -EINVAL;
455
456#ifdef CONFIG_MAC80211_HT_DEBUG
457 printk(KERN_DEBUG "Open BA session requested for %s tid %u\n",
458 print_mac(mac, ra), tid);
459#endif /* CONFIG_MAC80211_HT_DEBUG */
460
461 sta = sta_info_get(local, ra);
462 if (!sta) {
463 printk(KERN_DEBUG "Could not find the station\n");
464 return -ENOENT;
465 }
466
467 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
468
469 /* we have tried too many times, receiver does not want A-MPDU */
470 if (sta->ampdu_mlme.tid_tx[tid].addba_req_num > HT_AGG_MAX_RETRIES) {
471 ret = -EBUSY;
472 goto start_ba_exit;
473 }
474
475 state = &sta->ampdu_mlme.tid_tx[tid].state;
476 /* check if the TID is not in aggregation flow already */
477 if (*state != HT_AGG_STATE_IDLE) {
478#ifdef CONFIG_MAC80211_HT_DEBUG
479 printk(KERN_DEBUG "BA request denied - session is not "
480 "idle on tid %u\n", tid);
481#endif /* CONFIG_MAC80211_HT_DEBUG */
482 ret = -EAGAIN;
483 goto start_ba_exit;
484 }
485
486 /* ensure that TX flow won't interrupt us
487 * until the end of the call to requeue function */
488 spin_lock_bh(&local->mdev->queue_lock);
489
490 /* create a new queue for this aggregation */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200491 ret = ieee80211_ht_agg_queue_add(local, sta, tid);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200492
493 /* case no queue is available to aggregation
494 * don't switch to aggregation */
495 if (ret) {
496#ifdef CONFIG_MAC80211_HT_DEBUG
497 printk(KERN_DEBUG "BA request denied - no queue available for"
498 " tid %d\n", tid);
499#endif /* CONFIG_MAC80211_HT_DEBUG */
500 spin_unlock_bh(&local->mdev->queue_lock);
501 goto start_ba_exit;
502 }
503 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
504
505 /* Ok, the Addba frame hasn't been sent yet, but if the driver calls the
506 * call back right away, it must see that the flow has begun */
507 *state |= HT_ADDBA_REQUESTED_MSK;
508
509 if (local->ops->ampdu_action)
510 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_START,
511 ra, tid, &start_seq_num);
512
513 if (ret) {
514 /* No need to requeue the packets in the agg queue, since we
515 * held the tx lock: no packet could be enqueued to the newly
516 * allocated queue */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200517 ieee80211_ht_agg_queue_remove(local, sta, tid, 0);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200518#ifdef CONFIG_MAC80211_HT_DEBUG
519 printk(KERN_DEBUG "BA request denied - HW or queue unavailable"
520 " for tid %d\n", tid);
521#endif /* CONFIG_MAC80211_HT_DEBUG */
522 spin_unlock_bh(&local->mdev->queue_lock);
523 *state = HT_AGG_STATE_IDLE;
524 goto start_ba_exit;
525 }
526
527 /* Will put all the packets in the new SW queue */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200528 ieee80211_requeue(local, ieee802_1d_to_ac[tid]);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200529 spin_unlock_bh(&local->mdev->queue_lock);
530
531 /* We have most probably almost emptied the legacy queue */
532 /* ieee80211_wake_queue(local_to_hw(local), ieee802_1d_to_ac[tid]); */
533
534 /* send an addBA request */
535 sta->ampdu_mlme.dialog_token_allocator++;
536 sta->ampdu_mlme.tid_tx[tid].dialog_token =
537 sta->ampdu_mlme.dialog_token_allocator;
538 sta->ampdu_mlme.tid_tx[tid].ssn = start_seq_num;
539
540 ieee80211_send_addba_request(sta->dev, ra, tid,
541 sta->ampdu_mlme.tid_tx[tid].dialog_token,
542 sta->ampdu_mlme.tid_tx[tid].ssn,
543 0x40, 5000);
544
545 /* activate the timer for the recipient's addBA response */
546 sta->ampdu_mlme.tid_tx[tid].addba_resp_timer.expires =
547 jiffies + ADDBA_RESP_INTERVAL;
548 add_timer(&sta->ampdu_mlme.tid_tx[tid].addba_resp_timer);
549 printk(KERN_DEBUG "activated addBA response timer on tid %d\n", tid);
550
551start_ba_exit:
552 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
553 sta_info_put(sta);
554 return ret;
555}
556EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
557
558int ieee80211_stop_tx_ba_session(struct ieee80211_hw *hw,
559 u8 *ra, u16 tid,
560 enum ieee80211_back_parties initiator)
561{
562 struct ieee80211_local *local = hw_to_local(hw);
563 struct sta_info *sta;
564 u8 *state;
565 int ret = 0;
566 DECLARE_MAC_BUF(mac);
567
568 if (tid >= STA_TID_NUM)
569 return -EINVAL;
570
571#ifdef CONFIG_MAC80211_HT_DEBUG
572 printk(KERN_DEBUG "Stop a BA session requested for %s tid %u\n",
573 print_mac(mac, ra), tid);
574#endif /* CONFIG_MAC80211_HT_DEBUG */
575
576 sta = sta_info_get(local, ra);
577 if (!sta)
578 return -ENOENT;
579
580 /* check if the TID is in aggregation */
581 state = &sta->ampdu_mlme.tid_tx[tid].state;
582 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
583
584 if (*state != HT_AGG_STATE_OPERATIONAL) {
585#ifdef CONFIG_MAC80211_HT_DEBUG
586 printk(KERN_DEBUG "Try to stop Tx aggregation on"
587 " non active TID\n");
588#endif /* CONFIG_MAC80211_HT_DEBUG */
589 ret = -ENOENT;
590 goto stop_BA_exit;
591 }
592
593 ieee80211_stop_queue(hw, sta->tid_to_tx_q[tid]);
594
595 *state = HT_AGG_STATE_REQ_STOP_BA_MSK |
596 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
597
598 if (local->ops->ampdu_action)
599 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_STOP,
600 ra, tid, NULL);
601
602 /* case HW denied going back to legacy */
603 if (ret) {
604 WARN_ON(ret != -EBUSY);
605 *state = HT_AGG_STATE_OPERATIONAL;
606 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
607 goto stop_BA_exit;
608 }
609
610stop_BA_exit:
611 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
612 sta_info_put(sta);
613 return ret;
614}
615EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
616
617void ieee80211_start_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u16 tid)
618{
619 struct ieee80211_local *local = hw_to_local(hw);
620 struct sta_info *sta;
621 u8 *state;
622 DECLARE_MAC_BUF(mac);
623
624 if (tid >= STA_TID_NUM) {
625 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
626 tid, STA_TID_NUM);
627 return;
628 }
629
630 sta = sta_info_get(local, ra);
631 if (!sta) {
632 printk(KERN_DEBUG "Could not find station: %s\n",
633 print_mac(mac, ra));
634 return;
635 }
636
637 state = &sta->ampdu_mlme.tid_tx[tid].state;
638 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
639
640 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
641 printk(KERN_DEBUG "addBA was not requested yet, state is %d\n",
642 *state);
643 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
644 sta_info_put(sta);
645 return;
646 }
647
648 WARN_ON_ONCE(*state & HT_ADDBA_DRV_READY_MSK);
649
650 *state |= HT_ADDBA_DRV_READY_MSK;
651
652 if (*state == HT_AGG_STATE_OPERATIONAL) {
653 printk(KERN_DEBUG "Aggregation is on for tid %d \n", tid);
654 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
655 }
656 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
657 sta_info_put(sta);
658}
659EXPORT_SYMBOL(ieee80211_start_tx_ba_cb);
660
661void ieee80211_stop_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u8 tid)
662{
663 struct ieee80211_local *local = hw_to_local(hw);
664 struct sta_info *sta;
665 u8 *state;
666 int agg_queue;
667 DECLARE_MAC_BUF(mac);
668
669 if (tid >= STA_TID_NUM) {
670 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
671 tid, STA_TID_NUM);
672 return;
673 }
674
675 printk(KERN_DEBUG "Stop a BA session requested on DA %s tid %d\n",
676 print_mac(mac, ra), tid);
677
678 sta = sta_info_get(local, ra);
679 if (!sta) {
680 printk(KERN_DEBUG "Could not find station: %s\n",
681 print_mac(mac, ra));
682 return;
683 }
684 state = &sta->ampdu_mlme.tid_tx[tid].state;
685
686 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
687 if ((*state & HT_AGG_STATE_REQ_STOP_BA_MSK) == 0) {
688 printk(KERN_DEBUG "unexpected callback to A-MPDU stop\n");
689 sta_info_put(sta);
690 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
691 return;
692 }
693
694 if (*state & HT_AGG_STATE_INITIATOR_MSK)
695 ieee80211_send_delba(sta->dev, ra, tid,
696 WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
697
698 agg_queue = sta->tid_to_tx_q[tid];
699
700 /* avoid ordering issues: we are the only one that can modify
701 * the content of the qdiscs */
702 spin_lock_bh(&local->mdev->queue_lock);
703 /* remove the queue for this aggregation */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200704 ieee80211_ht_agg_queue_remove(local, sta, tid, 1);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200705 spin_unlock_bh(&local->mdev->queue_lock);
706
707 /* we just requeued the all the frames that were in the removed
708 * queue, and since we might miss a softirq we do netif_schedule.
709 * ieee80211_wake_queue is not used here as this queue is not
710 * necessarily stopped */
711 netif_schedule(local->mdev);
712 *state = HT_AGG_STATE_IDLE;
713 sta->ampdu_mlme.tid_tx[tid].addba_req_num = 0;
714 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
715
716 sta_info_put(sta);
717}
718EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb);
719
720void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
721 const u8 *ra, u16 tid)
722{
723 struct ieee80211_local *local = hw_to_local(hw);
724 struct ieee80211_ra_tid *ra_tid;
725 struct sk_buff *skb = dev_alloc_skb(0);
726
727 if (unlikely(!skb)) {
728 if (net_ratelimit())
729 printk(KERN_WARNING "%s: Not enough memory, "
730 "dropping start BA session", skb->dev->name);
731 return;
732 }
733 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
734 memcpy(&ra_tid->ra, ra, ETH_ALEN);
735 ra_tid->tid = tid;
736
737 skb->pkt_type = IEEE80211_ADDBA_MSG;
738 skb_queue_tail(&local->skb_queue, skb);
739 tasklet_schedule(&local->tasklet);
740}
741EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
742
743void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
744 const u8 *ra, u16 tid)
745{
746 struct ieee80211_local *local = hw_to_local(hw);
747 struct ieee80211_ra_tid *ra_tid;
748 struct sk_buff *skb = dev_alloc_skb(0);
749
750 if (unlikely(!skb)) {
751 if (net_ratelimit())
752 printk(KERN_WARNING "%s: Not enough memory, "
753 "dropping stop BA session", skb->dev->name);
754 return;
755 }
756 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
757 memcpy(&ra_tid->ra, ra, ETH_ALEN);
758 ra_tid->tid = tid;
759
760 skb->pkt_type = IEEE80211_DELBA_MSG;
761 skb_queue_tail(&local->skb_queue, skb);
762 tasklet_schedule(&local->tasklet);
763}
764EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
765
Johannes Bergb2c258f2007-07-27 15:43:23 +0200766static void ieee80211_set_multicast_list(struct net_device *dev)
767{
768 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
769 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400770 int allmulti, promisc, sdata_allmulti, sdata_promisc;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200771
Johannes Berg4150c572007-09-17 01:29:23 -0400772 allmulti = !!(dev->flags & IFF_ALLMULTI);
773 promisc = !!(dev->flags & IFF_PROMISC);
Johannes Bergb52f2192007-11-16 01:49:11 +0100774 sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
775 sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
Johannes Berg4150c572007-09-17 01:29:23 -0400776
777 if (allmulti != sdata_allmulti) {
778 if (dev->flags & IFF_ALLMULTI)
Johannes Berg53918992007-09-26 15:19:47 +0200779 atomic_inc(&local->iff_allmultis);
Johannes Berg4150c572007-09-17 01:29:23 -0400780 else
Johannes Berg53918992007-09-26 15:19:47 +0200781 atomic_dec(&local->iff_allmultis);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400782 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200783 }
Johannes Berg4150c572007-09-17 01:29:23 -0400784
785 if (promisc != sdata_promisc) {
786 if (dev->flags & IFF_PROMISC)
Johannes Berg53918992007-09-26 15:19:47 +0200787 atomic_inc(&local->iff_promiscs);
Johannes Berg4150c572007-09-17 01:29:23 -0400788 else
Johannes Berg53918992007-09-26 15:19:47 +0200789 atomic_dec(&local->iff_promiscs);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400790 sdata->flags ^= IEEE80211_SDATA_PROMISC;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200791 }
Johannes Berg4150c572007-09-17 01:29:23 -0400792
793 dev_mc_sync(local->mdev, dev);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200794}
795
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700796static const struct header_ops ieee80211_header_ops = {
797 .create = eth_header,
798 .parse = header_parse_80211,
799 .rebuild = eth_rebuild_header,
800 .cache = eth_header_cache,
801 .cache_update = eth_header_cache_update,
802};
803
Johannes Bergf9d540e2007-09-28 14:02:09 +0200804/* Must not be called for mdev */
Johannes Bergb2c258f2007-07-27 15:43:23 +0200805void ieee80211_if_setup(struct net_device *dev)
806{
807 ether_setup(dev);
808 dev->hard_start_xmit = ieee80211_subif_start_xmit;
809 dev->wireless_handlers = &ieee80211_iw_handler_def;
810 dev->set_multicast_list = ieee80211_set_multicast_list;
811 dev->change_mtu = ieee80211_change_mtu;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200812 dev->open = ieee80211_open;
813 dev->stop = ieee80211_stop;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200814 dev->destructor = ieee80211_if_free;
815}
816
817/* WDS specialties */
818
819int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
820{
821 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
822 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
823 struct sta_info *sta;
Joe Perches0795af52007-10-03 17:59:30 -0700824 DECLARE_MAC_BUF(mac);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200825
826 if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
827 return 0;
828
829 /* Create STA entry for the new peer */
830 sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
831 if (!sta)
832 return -ENOMEM;
Johannes Berg238814f2008-01-28 17:19:37 +0100833
834 sta->flags |= WLAN_STA_AUTHORIZED;
835
Johannes Bergb2c258f2007-07-27 15:43:23 +0200836 sta_info_put(sta);
837
838 /* Remove STA entry for the old peer */
839 sta = sta_info_get(local, sdata->u.wds.remote_addr);
840 if (sta) {
Michael Wube8755e2007-07-27 15:43:23 +0200841 sta_info_free(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200842 sta_info_put(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200843 } else {
844 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
Joe Perches0795af52007-10-03 17:59:30 -0700845 "peer %s\n",
846 dev->name, print_mac(mac, sdata->u.wds.remote_addr));
Johannes Bergb2c258f2007-07-27 15:43:23 +0200847 }
848
849 /* Update WDS link data */
850 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
851
852 return 0;
853}
854
855/* everything else */
856
Johannes Bergb2c258f2007-07-27 15:43:23 +0200857static int __ieee80211_if_config(struct net_device *dev,
858 struct sk_buff *beacon,
859 struct ieee80211_tx_control *control)
860{
861 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
862 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
863 struct ieee80211_if_conf conf;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200864
865 if (!local->ops->config_interface || !netif_running(dev))
866 return 0;
867
868 memset(&conf, 0, sizeof(conf));
Johannes Berg51fb61e2007-12-19 01:31:27 +0100869 conf.type = sdata->vif.type;
870 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
871 sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Johannes Berg4150c572007-09-17 01:29:23 -0400872 conf.bssid = sdata->u.sta.bssid;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200873 conf.ssid = sdata->u.sta.ssid;
874 conf.ssid_len = sdata->u.sta.ssid_len;
Johannes Berg51fb61e2007-12-19 01:31:27 +0100875 } else if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
Johannes Bergb2c258f2007-07-27 15:43:23 +0200876 conf.ssid = sdata->u.ap.ssid;
877 conf.ssid_len = sdata->u.ap.ssid_len;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200878 conf.beacon = beacon;
879 conf.beacon_control = control;
880 }
881 return local->ops->config_interface(local_to_hw(local),
Johannes Berg32bfd352007-12-19 01:31:26 +0100882 &sdata->vif, &conf);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200883}
884
885int ieee80211_if_config(struct net_device *dev)
886{
887 return __ieee80211_if_config(dev, NULL, NULL);
888}
889
890int ieee80211_if_config_beacon(struct net_device *dev)
891{
892 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
893 struct ieee80211_tx_control control;
Johannes Berg32bfd352007-12-19 01:31:26 +0100894 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200895 struct sk_buff *skb;
896
897 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
898 return 0;
Johannes Berg32bfd352007-12-19 01:31:26 +0100899 skb = ieee80211_beacon_get(local_to_hw(local), &sdata->vif,
900 &control);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200901 if (!skb)
902 return -ENOMEM;
903 return __ieee80211_if_config(dev, skb, &control);
904}
905
906int ieee80211_hw_config(struct ieee80211_local *local)
907{
Johannes Bergb2c258f2007-07-27 15:43:23 +0200908 struct ieee80211_channel *chan;
909 int ret = 0;
910
Johannes Berg8318d782008-01-24 19:38:38 +0100911 if (local->sta_sw_scanning)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200912 chan = local->scan_channel;
Johannes Berg8318d782008-01-24 19:38:38 +0100913 else
Johannes Bergb2c258f2007-07-27 15:43:23 +0200914 chan = local->oper_channel;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200915
Johannes Berg8318d782008-01-24 19:38:38 +0100916 local->hw.conf.channel = chan;
917
918 if (!local->hw.conf.power_level)
919 local->hw.conf.power_level = chan->max_power;
920 else
921 local->hw.conf.power_level = min(chan->max_power,
922 local->hw.conf.power_level);
923
924 local->hw.conf.max_antenna_gain = chan->max_antenna_gain;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200925
926#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg8318d782008-01-24 19:38:38 +0100927 printk(KERN_DEBUG "%s: HW CONFIG: freq=%d\n",
928 wiphy_name(local->hw.wiphy), chan->center_freq);
929#endif
Johannes Bergb2c258f2007-07-27 15:43:23 +0200930
Michael Bueschf7c4dae2007-09-24 18:41:49 +0200931 if (local->open_count)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200932 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
933
934 return ret;
935}
936
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +0200937/**
938 * ieee80211_hw_config_ht should be used only after legacy configuration
939 * has been determined, as ht configuration depends upon the hardware's
940 * HT abilities for a _specific_ band.
941 */
942int ieee80211_hw_config_ht(struct ieee80211_local *local, int enable_ht,
943 struct ieee80211_ht_info *req_ht_cap,
944 struct ieee80211_ht_bss_info *req_bss_cap)
945{
946 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg8318d782008-01-24 19:38:38 +0100947 struct ieee80211_supported_band *sband;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +0200948 int i;
949
Johannes Berg8318d782008-01-24 19:38:38 +0100950 sband = local->hw.wiphy->bands[conf->channel->band];
951
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +0200952 /* HT is not supported */
Johannes Berg8318d782008-01-24 19:38:38 +0100953 if (!sband->ht_info.ht_supported) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +0200954 conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
955 return -EOPNOTSUPP;
956 }
957
958 /* disable HT */
959 if (!enable_ht) {
960 conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
961 } else {
962 conf->flags |= IEEE80211_CONF_SUPPORT_HT_MODE;
Johannes Berg8318d782008-01-24 19:38:38 +0100963 conf->ht_conf.cap = req_ht_cap->cap & sband->ht_info.cap;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +0200964 conf->ht_conf.cap &= ~(IEEE80211_HT_CAP_MIMO_PS);
965 conf->ht_conf.cap |=
Johannes Berg8318d782008-01-24 19:38:38 +0100966 sband->ht_info.cap & IEEE80211_HT_CAP_MIMO_PS;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +0200967 conf->ht_bss_conf.primary_channel =
968 req_bss_cap->primary_channel;
969 conf->ht_bss_conf.bss_cap = req_bss_cap->bss_cap;
970 conf->ht_bss_conf.bss_op_mode = req_bss_cap->bss_op_mode;
971 for (i = 0; i < SUPP_MCS_SET_LEN; i++)
972 conf->ht_conf.supp_mcs_set[i] =
Johannes Berg8318d782008-01-24 19:38:38 +0100973 sband->ht_info.supp_mcs_set[i] &
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +0200974 req_ht_cap->supp_mcs_set[i];
975
976 /* In STA mode, this gives us indication
977 * to the AP's mode of operation */
978 conf->ht_conf.ht_supported = 1;
979 conf->ht_conf.ampdu_factor = req_ht_cap->ampdu_factor;
980 conf->ht_conf.ampdu_density = req_ht_cap->ampdu_density;
981 }
982
983 local->ops->conf_ht(local_to_hw(local), &local->hw.conf);
984
985 return 0;
986}
987
Johannes Berg471b3ef2007-12-28 14:32:58 +0100988void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
989 u32 changed)
Daniel Draked9430a32007-07-27 15:43:24 +0200990{
Johannes Berg471b3ef2007-12-28 14:32:58 +0100991 struct ieee80211_local *local = sdata->local;
992
993 if (!changed)
994 return;
995
996 if (local->ops->bss_info_changed)
997 local->ops->bss_info_changed(local_to_hw(local),
998 &sdata->vif,
999 &sdata->bss_conf,
1000 changed);
Daniel Draked9430a32007-07-27 15:43:24 +02001001}
1002
1003void ieee80211_reset_erp_info(struct net_device *dev)
1004{
1005 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1006
Johannes Berg471b3ef2007-12-28 14:32:58 +01001007 sdata->bss_conf.use_cts_prot = 0;
1008 sdata->bss_conf.use_short_preamble = 0;
1009 ieee80211_bss_info_change_notify(sdata,
1010 BSS_CHANGED_ERP_CTS_PROT |
1011 BSS_CHANGED_ERP_PREAMBLE);
Daniel Draked9430a32007-07-27 15:43:24 +02001012}
1013
Jiri Bencf0706e82007-05-05 11:45:53 -07001014void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
1015 struct sk_buff *skb,
1016 struct ieee80211_tx_status *status)
1017{
1018 struct ieee80211_local *local = hw_to_local(hw);
1019 struct ieee80211_tx_status *saved;
1020 int tmp;
1021
1022 skb->dev = local->mdev;
1023 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
1024 if (unlikely(!saved)) {
1025 if (net_ratelimit())
1026 printk(KERN_WARNING "%s: Not enough memory, "
1027 "dropping tx status", skb->dev->name);
1028 /* should be dev_kfree_skb_irq, but due to this function being
1029 * named _irqsafe instead of just _irq we can't be sure that
1030 * people won't call it from non-irq contexts */
1031 dev_kfree_skb_any(skb);
1032 return;
1033 }
1034 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
1035 /* copy pointer to saved status into skb->cb for use by tasklet */
1036 memcpy(skb->cb, &saved, sizeof(saved));
1037
1038 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
1039 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
1040 &local->skb_queue : &local->skb_queue_unreliable, skb);
1041 tmp = skb_queue_len(&local->skb_queue) +
1042 skb_queue_len(&local->skb_queue_unreliable);
1043 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
1044 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
1045 memcpy(&saved, skb->cb, sizeof(saved));
1046 kfree(saved);
1047 dev_kfree_skb_irq(skb);
1048 tmp--;
1049 I802_DEBUG_INC(local->tx_status_drop);
1050 }
1051 tasklet_schedule(&local->tasklet);
1052}
1053EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
1054
1055static void ieee80211_tasklet_handler(unsigned long data)
1056{
1057 struct ieee80211_local *local = (struct ieee80211_local *) data;
1058 struct sk_buff *skb;
1059 struct ieee80211_rx_status rx_status;
1060 struct ieee80211_tx_status *tx_status;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001061 struct ieee80211_ra_tid *ra_tid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001062
1063 while ((skb = skb_dequeue(&local->skb_queue)) ||
1064 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
1065 switch (skb->pkt_type) {
1066 case IEEE80211_RX_MSG:
1067 /* status is in skb->cb */
1068 memcpy(&rx_status, skb->cb, sizeof(rx_status));
Johannes Berg51fb61e2007-12-19 01:31:27 +01001069 /* Clear skb->pkt_type in order to not confuse kernel
Jiri Bencf0706e82007-05-05 11:45:53 -07001070 * netstack. */
1071 skb->pkt_type = 0;
1072 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
1073 break;
1074 case IEEE80211_TX_STATUS_MSG:
1075 /* get pointer to saved status out of skb->cb */
1076 memcpy(&tx_status, skb->cb, sizeof(tx_status));
1077 skb->pkt_type = 0;
1078 ieee80211_tx_status(local_to_hw(local),
1079 skb, tx_status);
1080 kfree(tx_status);
1081 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001082 case IEEE80211_DELBA_MSG:
1083 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
1084 ieee80211_stop_tx_ba_cb(local_to_hw(local),
1085 ra_tid->ra, ra_tid->tid);
1086 dev_kfree_skb(skb);
1087 break;
1088 case IEEE80211_ADDBA_MSG:
1089 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
1090 ieee80211_start_tx_ba_cb(local_to_hw(local),
1091 ra_tid->ra, ra_tid->tid);
1092 dev_kfree_skb(skb);
1093 break ;
Jiri Bencf0706e82007-05-05 11:45:53 -07001094 default: /* should never get here! */
1095 printk(KERN_ERR "%s: Unknown message type (%d)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001096 wiphy_name(local->hw.wiphy), skb->pkt_type);
Jiri Bencf0706e82007-05-05 11:45:53 -07001097 dev_kfree_skb(skb);
1098 break;
1099 }
1100 }
1101}
1102
Jiri Bencf0706e82007-05-05 11:45:53 -07001103/* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
1104 * make a prepared TX frame (one that has been given to hw) to look like brand
1105 * new IEEE 802.11 frame that is ready to go through TX processing again.
1106 * Also, tx_packet_data in cb is restored from tx_control. */
1107static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
1108 struct ieee80211_key *key,
1109 struct sk_buff *skb,
1110 struct ieee80211_tx_control *control)
1111{
1112 int hdrlen, iv_len, mic_len;
1113 struct ieee80211_tx_packet_data *pkt_data;
1114
1115 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
Johannes Berg32bfd352007-12-19 01:31:26 +01001116 pkt_data->ifindex = vif_to_sdata(control->vif)->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001117 pkt_data->flags = 0;
1118 if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
1119 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
1120 if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
1121 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
1122 if (control->flags & IEEE80211_TXCTL_REQUEUE)
1123 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg678f5f712007-12-19 01:31:23 +01001124 if (control->flags & IEEE80211_TXCTL_EAPOL_FRAME)
1125 pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
Jiri Bencf0706e82007-05-05 11:45:53 -07001126 pkt_data->queue = control->queue;
1127
1128 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1129
1130 if (!key)
1131 goto no_key;
1132
Johannes Berg8f20fc22007-08-28 17:01:54 -04001133 switch (key->conf.alg) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001134 case ALG_WEP:
1135 iv_len = WEP_IV_LEN;
1136 mic_len = WEP_ICV_LEN;
1137 break;
1138 case ALG_TKIP:
1139 iv_len = TKIP_IV_LEN;
1140 mic_len = TKIP_ICV_LEN;
1141 break;
1142 case ALG_CCMP:
1143 iv_len = CCMP_HDR_LEN;
1144 mic_len = CCMP_MIC_LEN;
1145 break;
1146 default:
1147 goto no_key;
1148 }
1149
Johannes Berg8f20fc22007-08-28 17:01:54 -04001150 if (skb->len >= mic_len &&
Johannes Berg11a843b2007-08-28 17:01:55 -04001151 !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Jiri Bencf0706e82007-05-05 11:45:53 -07001152 skb_trim(skb, skb->len - mic_len);
1153 if (skb->len >= iv_len && skb->len > hdrlen) {
1154 memmove(skb->data + iv_len, skb->data, hdrlen);
1155 skb_pull(skb, iv_len);
1156 }
1157
1158no_key:
1159 {
1160 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1161 u16 fc = le16_to_cpu(hdr->frame_control);
1162 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
1163 fc &= ~IEEE80211_STYPE_QOS_DATA;
1164 hdr->frame_control = cpu_to_le16(fc);
1165 memmove(skb->data + 2, skb->data, hdrlen - 2);
1166 skb_pull(skb, 2);
1167 }
1168 }
1169}
1170
Jiri Bencf0706e82007-05-05 11:45:53 -07001171void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
1172 struct ieee80211_tx_status *status)
1173{
1174 struct sk_buff *skb2;
1175 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1176 struct ieee80211_local *local = hw_to_local(hw);
1177 u16 frag, type;
Johannes Bergb306f452007-07-10 19:32:08 +02001178 struct ieee80211_tx_status_rtap_hdr *rthdr;
1179 struct ieee80211_sub_if_data *sdata;
1180 int monitors;
Jiri Bencf0706e82007-05-05 11:45:53 -07001181
1182 if (!status) {
1183 printk(KERN_ERR
1184 "%s: ieee80211_tx_status called with NULL status\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001185 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001186 dev_kfree_skb(skb);
1187 return;
1188 }
1189
1190 if (status->excessive_retries) {
1191 struct sta_info *sta;
1192 sta = sta_info_get(local, hdr->addr1);
1193 if (sta) {
1194 if (sta->flags & WLAN_STA_PS) {
1195 /* The STA is in power save mode, so assume
1196 * that this TX packet failed because of that.
1197 */
1198 status->excessive_retries = 0;
1199 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
1200 }
1201 sta_info_put(sta);
1202 }
1203 }
1204
1205 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
1206 struct sta_info *sta;
1207 sta = sta_info_get(local, hdr->addr1);
1208 if (sta) {
1209 sta->tx_filtered_count++;
1210
1211 /* Clear the TX filter mask for this STA when sending
1212 * the next packet. If the STA went to power save mode,
1213 * this will happen when it is waking up for the next
1214 * time. */
1215 sta->clear_dst_mask = 1;
1216
1217 /* TODO: Is the WLAN_STA_PS flag always set here or is
1218 * the race between RX and TX status causing some
1219 * packets to be filtered out before 80211.o gets an
1220 * update for PS status? This seems to be the case, so
1221 * no changes are likely to be needed. */
1222 if (sta->flags & WLAN_STA_PS &&
1223 skb_queue_len(&sta->tx_filtered) <
1224 STA_MAX_TX_BUFFER) {
1225 ieee80211_remove_tx_extra(local, sta->key,
1226 skb,
1227 &status->control);
1228 skb_queue_tail(&sta->tx_filtered, skb);
1229 } else if (!(sta->flags & WLAN_STA_PS) &&
1230 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
1231 /* Software retry the packet once */
1232 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
1233 ieee80211_remove_tx_extra(local, sta->key,
1234 skb,
1235 &status->control);
1236 dev_queue_xmit(skb);
1237 } else {
1238 if (net_ratelimit()) {
1239 printk(KERN_DEBUG "%s: dropped TX "
1240 "filtered frame queue_len=%d "
1241 "PS=%d @%lu\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001242 wiphy_name(local->hw.wiphy),
Jiri Bencf0706e82007-05-05 11:45:53 -07001243 skb_queue_len(
1244 &sta->tx_filtered),
1245 !!(sta->flags & WLAN_STA_PS),
1246 jiffies);
1247 }
1248 dev_kfree_skb(skb);
1249 }
1250 sta_info_put(sta);
1251 return;
1252 }
Mattias Nissler1abbe492007-12-20 13:50:07 +01001253 } else
1254 rate_control_tx_status(local->mdev, skb, status);
Jiri Bencf0706e82007-05-05 11:45:53 -07001255
1256 ieee80211_led_tx(local, 0);
1257
1258 /* SNMP counters
1259 * Fragments are passed to low-level drivers as separate skbs, so these
1260 * are actually fragments, not frames. Update frame counters only for
1261 * the first fragment of the frame. */
1262
1263 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
1264 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
1265
1266 if (status->flags & IEEE80211_TX_STATUS_ACK) {
1267 if (frag == 0) {
1268 local->dot11TransmittedFrameCount++;
1269 if (is_multicast_ether_addr(hdr->addr1))
1270 local->dot11MulticastTransmittedFrameCount++;
1271 if (status->retry_count > 0)
1272 local->dot11RetryCount++;
1273 if (status->retry_count > 1)
1274 local->dot11MultipleRetryCount++;
1275 }
1276
1277 /* This counter shall be incremented for an acknowledged MPDU
1278 * with an individual address in the address 1 field or an MPDU
1279 * with a multicast address in the address 1 field of type Data
1280 * or Management. */
1281 if (!is_multicast_ether_addr(hdr->addr1) ||
1282 type == IEEE80211_FTYPE_DATA ||
1283 type == IEEE80211_FTYPE_MGMT)
1284 local->dot11TransmittedFragmentCount++;
1285 } else {
1286 if (frag == 0)
1287 local->dot11FailedCount++;
1288 }
1289
Johannes Bergb306f452007-07-10 19:32:08 +02001290 /* this was a transmitted frame, but now we want to reuse it */
1291 skb_orphan(skb);
1292
Johannes Bergb306f452007-07-10 19:32:08 +02001293 if (!local->monitors) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001294 dev_kfree_skb(skb);
1295 return;
1296 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001297
Johannes Bergb306f452007-07-10 19:32:08 +02001298 /* send frame to monitor interfaces now */
1299
1300 if (skb_headroom(skb) < sizeof(*rthdr)) {
1301 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
1302 dev_kfree_skb(skb);
1303 return;
1304 }
1305
1306 rthdr = (struct ieee80211_tx_status_rtap_hdr*)
1307 skb_push(skb, sizeof(*rthdr));
1308
1309 memset(rthdr, 0, sizeof(*rthdr));
1310 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
1311 rthdr->hdr.it_present =
1312 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
1313 (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
1314
1315 if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
1316 !is_multicast_ether_addr(hdr->addr1))
1317 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
1318
1319 if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
1320 (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
1321 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
1322 else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
1323 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
1324
1325 rthdr->data_retries = status->retry_count;
1326
Johannes Berg79010422007-09-18 17:29:21 -04001327 rcu_read_lock();
Johannes Bergb306f452007-07-10 19:32:08 +02001328 monitors = local->monitors;
Johannes Berg79010422007-09-18 17:29:21 -04001329 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Bergb306f452007-07-10 19:32:08 +02001330 /*
1331 * Using the monitors counter is possibly racy, but
1332 * if the value is wrong we simply either clone the skb
1333 * once too much or forget sending it to one monitor iface
1334 * The latter case isn't nice but fixing the race is much
1335 * more complicated.
1336 */
1337 if (!monitors || !skb)
1338 goto out;
1339
Johannes Berg51fb61e2007-12-19 01:31:27 +01001340 if (sdata->vif.type == IEEE80211_IF_TYPE_MNTR) {
Johannes Bergb306f452007-07-10 19:32:08 +02001341 if (!netif_running(sdata->dev))
1342 continue;
1343 monitors--;
1344 if (monitors)
Johannes Berg79010422007-09-18 17:29:21 -04001345 skb2 = skb_clone(skb, GFP_ATOMIC);
Johannes Bergb306f452007-07-10 19:32:08 +02001346 else
1347 skb2 = NULL;
1348 skb->dev = sdata->dev;
1349 /* XXX: is this sufficient for BPF? */
1350 skb_set_mac_header(skb, 0);
1351 skb->ip_summed = CHECKSUM_UNNECESSARY;
1352 skb->pkt_type = PACKET_OTHERHOST;
1353 skb->protocol = htons(ETH_P_802_2);
1354 memset(skb->cb, 0, sizeof(skb->cb));
1355 netif_rx(skb);
1356 skb = skb2;
Johannes Bergb306f452007-07-10 19:32:08 +02001357 }
1358 }
1359 out:
Johannes Berg79010422007-09-18 17:29:21 -04001360 rcu_read_unlock();
Johannes Bergb306f452007-07-10 19:32:08 +02001361 if (skb)
1362 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -07001363}
1364EXPORT_SYMBOL(ieee80211_tx_status);
1365
Jiri Bencf0706e82007-05-05 11:45:53 -07001366struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
1367 const struct ieee80211_ops *ops)
1368{
1369 struct net_device *mdev;
1370 struct ieee80211_local *local;
1371 struct ieee80211_sub_if_data *sdata;
1372 int priv_size;
1373 struct wiphy *wiphy;
1374
1375 /* Ensure 32-byte alignment of our private data and hw private data.
1376 * We use the wiphy priv data for both our ieee80211_local and for
1377 * the driver's private data
1378 *
1379 * In memory it'll be like this:
1380 *
1381 * +-------------------------+
1382 * | struct wiphy |
1383 * +-------------------------+
1384 * | struct ieee80211_local |
1385 * +-------------------------+
1386 * | driver's private data |
1387 * +-------------------------+
1388 *
1389 */
1390 priv_size = ((sizeof(struct ieee80211_local) +
1391 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
1392 priv_data_len;
1393
1394 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
1395
1396 if (!wiphy)
1397 return NULL;
1398
1399 wiphy->privid = mac80211_wiphy_privid;
1400
1401 local = wiphy_priv(wiphy);
1402 local->hw.wiphy = wiphy;
1403
1404 local->hw.priv = (char *)local +
1405 ((sizeof(struct ieee80211_local) +
1406 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
1407
Johannes Berg4480f15c2007-07-10 19:32:10 +02001408 BUG_ON(!ops->tx);
Johannes Berg4150c572007-09-17 01:29:23 -04001409 BUG_ON(!ops->start);
1410 BUG_ON(!ops->stop);
Johannes Berg4480f15c2007-07-10 19:32:10 +02001411 BUG_ON(!ops->config);
1412 BUG_ON(!ops->add_interface);
Johannes Berg4150c572007-09-17 01:29:23 -04001413 BUG_ON(!ops->remove_interface);
1414 BUG_ON(!ops->configure_filter);
Jiri Bencf0706e82007-05-05 11:45:53 -07001415 local->ops = ops;
1416
1417 /* for now, mdev needs sub_if_data :/ */
1418 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
1419 "wmaster%d", ether_setup);
1420 if (!mdev) {
1421 wiphy_free(wiphy);
1422 return NULL;
1423 }
1424
1425 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
1426 mdev->ieee80211_ptr = &sdata->wdev;
1427 sdata->wdev.wiphy = wiphy;
1428
1429 local->hw.queues = 1; /* default */
1430
1431 local->mdev = mdev;
Jiri Bencf0706e82007-05-05 11:45:53 -07001432
1433 local->bridge_packets = 1;
1434
1435 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1436 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1437 local->short_retry_limit = 7;
1438 local->long_retry_limit = 4;
1439 local->hw.conf.radio_enabled = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07001440
Johannes Berg79010422007-09-18 17:29:21 -04001441 INIT_LIST_HEAD(&local->interfaces);
Jiri Bencf0706e82007-05-05 11:45:53 -07001442
1443 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
Jiri Bencf0706e82007-05-05 11:45:53 -07001444 ieee80211_rx_bss_list_init(mdev);
1445
1446 sta_info_init(local);
1447
1448 mdev->hard_start_xmit = ieee80211_master_start_xmit;
1449 mdev->open = ieee80211_master_open;
1450 mdev->stop = ieee80211_master_stop;
1451 mdev->type = ARPHRD_IEEE80211;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001452 mdev->header_ops = &ieee80211_header_ops;
Johannes Berg4150c572007-09-17 01:29:23 -04001453 mdev->set_multicast_list = ieee80211_master_set_multicast_list;
Jiri Bencf0706e82007-05-05 11:45:53 -07001454
Johannes Berg51fb61e2007-12-19 01:31:27 +01001455 sdata->vif.type = IEEE80211_IF_TYPE_AP;
Jiri Bencf0706e82007-05-05 11:45:53 -07001456 sdata->dev = mdev;
1457 sdata->local = local;
1458 sdata->u.ap.force_unicast_rateidx = -1;
1459 sdata->u.ap.max_ratectrl_rateidx = -1;
1460 ieee80211_if_sdata_init(sdata);
Johannes Berg79010422007-09-18 17:29:21 -04001461 /* no RCU needed since we're still during init phase */
1462 list_add_tail(&sdata->list, &local->interfaces);
Jiri Bencf0706e82007-05-05 11:45:53 -07001463
1464 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1465 (unsigned long)local);
1466 tasklet_disable(&local->tx_pending_tasklet);
1467
1468 tasklet_init(&local->tasklet,
1469 ieee80211_tasklet_handler,
1470 (unsigned long) local);
1471 tasklet_disable(&local->tasklet);
1472
1473 skb_queue_head_init(&local->skb_queue);
1474 skb_queue_head_init(&local->skb_queue_unreliable);
1475
1476 return local_to_hw(local);
1477}
1478EXPORT_SYMBOL(ieee80211_alloc_hw);
1479
1480int ieee80211_register_hw(struct ieee80211_hw *hw)
1481{
1482 struct ieee80211_local *local = hw_to_local(hw);
1483 const char *name;
1484 int result;
Johannes Berg8318d782008-01-24 19:38:38 +01001485 enum ieee80211_band band;
1486
1487 /*
1488 * generic code guarantees at least one band,
1489 * set this very early because much code assumes
1490 * that hw.conf.channel is assigned
1491 */
1492 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1493 struct ieee80211_supported_band *sband;
1494
1495 sband = local->hw.wiphy->bands[band];
1496 if (sband) {
1497 /* init channel we're on */
1498 local->hw.conf.channel =
1499 local->oper_channel =
1500 local->scan_channel = &sband->channels[0];
1501 break;
1502 }
1503 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001504
1505 result = wiphy_register(local->hw.wiphy);
1506 if (result < 0)
1507 return result;
1508
1509 name = wiphy_dev(local->hw.wiphy)->driver->name;
1510 local->hw.workqueue = create_singlethread_workqueue(name);
1511 if (!local->hw.workqueue) {
1512 result = -ENOMEM;
1513 goto fail_workqueue;
1514 }
1515
Johannes Bergb306f452007-07-10 19:32:08 +02001516 /*
1517 * The hardware needs headroom for sending the frame,
1518 * and we need some headroom for passing the frame to monitor
1519 * interfaces, but never both at the same time.
1520 */
Jiri Benc33ccad32007-07-18 17:10:44 +02001521 local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1522 sizeof(struct ieee80211_tx_status_rtap_hdr));
Johannes Bergb306f452007-07-10 19:32:08 +02001523
Jiri Bence9f207f2007-05-05 11:46:38 -07001524 debugfs_hw_add(local);
1525
Jiri Bencf0706e82007-05-05 11:45:53 -07001526 local->hw.conf.beacon_int = 1000;
1527
1528 local->wstats_flags |= local->hw.max_rssi ?
1529 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1530 local->wstats_flags |= local->hw.max_signal ?
1531 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1532 local->wstats_flags |= local->hw.max_noise ?
1533 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1534 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1535 local->wstats_flags |= IW_QUAL_DBM;
1536
1537 result = sta_info_start(local);
1538 if (result < 0)
1539 goto fail_sta_info;
1540
1541 rtnl_lock();
1542 result = dev_alloc_name(local->mdev, local->mdev->name);
1543 if (result < 0)
1544 goto fail_dev;
1545
1546 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1547 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1548
1549 result = register_netdevice(local->mdev);
1550 if (result < 0)
1551 goto fail_dev;
1552
Jiri Bence9f207f2007-05-05 11:46:38 -07001553 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Johannes Berg5b2812e2007-09-26 14:27:23 +02001554 ieee80211_if_set_type(local->mdev, IEEE80211_IF_TYPE_AP);
Jiri Bence9f207f2007-05-05 11:46:38 -07001555
Johannes Berg830f9032007-10-28 14:51:05 +01001556 result = ieee80211_init_rate_ctrl_alg(local,
1557 hw->rate_control_algorithm);
Jiri Bencf0706e82007-05-05 11:45:53 -07001558 if (result < 0) {
1559 printk(KERN_DEBUG "%s: Failed to initialize rate control "
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001560 "algorithm\n", wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001561 goto fail_rate;
1562 }
1563
1564 result = ieee80211_wep_init(local);
1565
1566 if (result < 0) {
1567 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001568 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001569 goto fail_wep;
1570 }
1571
1572 ieee80211_install_qdisc(local->mdev);
1573
1574 /* add one default STA interface */
1575 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
1576 IEEE80211_IF_TYPE_STA);
1577 if (result)
1578 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001579 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001580
1581 local->reg_state = IEEE80211_DEV_REGISTERED;
1582 rtnl_unlock();
1583
1584 ieee80211_led_init(local);
1585
1586 return 0;
1587
1588fail_wep:
1589 rate_control_deinitialize(local);
1590fail_rate:
Jiri Bence9f207f2007-05-05 11:46:38 -07001591 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001592 unregister_netdevice(local->mdev);
1593fail_dev:
1594 rtnl_unlock();
1595 sta_info_stop(local);
1596fail_sta_info:
Jiri Bence9f207f2007-05-05 11:46:38 -07001597 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001598 destroy_workqueue(local->hw.workqueue);
1599fail_workqueue:
1600 wiphy_unregister(local->hw.wiphy);
1601 return result;
1602}
1603EXPORT_SYMBOL(ieee80211_register_hw);
1604
Jiri Bencf0706e82007-05-05 11:45:53 -07001605void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1606{
1607 struct ieee80211_local *local = hw_to_local(hw);
1608 struct ieee80211_sub_if_data *sdata, *tmp;
Jiri Bencf0706e82007-05-05 11:45:53 -07001609
1610 tasklet_kill(&local->tx_pending_tasklet);
1611 tasklet_kill(&local->tasklet);
1612
1613 rtnl_lock();
1614
1615 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1616
1617 local->reg_state = IEEE80211_DEV_UNREGISTERED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001618
Johannes Berg79010422007-09-18 17:29:21 -04001619 /*
1620 * At this point, interface list manipulations are fine
1621 * because the driver cannot be handing us frames any
1622 * more and the tasklet is killed.
1623 */
Johannes Berg5b2812e2007-09-26 14:27:23 +02001624
1625 /*
1626 * First, we remove all non-master interfaces. Do this because they
1627 * may have bss pointer dependency on the master, and when we free
1628 * the master these would be freed as well, breaking our list
1629 * iteration completely.
1630 */
1631 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
1632 if (sdata->dev == local->mdev)
1633 continue;
1634 list_del(&sdata->list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001635 __ieee80211_if_del(local, sdata);
Johannes Berg5b2812e2007-09-26 14:27:23 +02001636 }
1637
1638 /* then, finally, remove the master interface */
1639 __ieee80211_if_del(local, IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001640
1641 rtnl_unlock();
1642
Jiri Bencf0706e82007-05-05 11:45:53 -07001643 ieee80211_rx_bss_list_deinit(local->mdev);
1644 ieee80211_clear_tx_pending(local);
1645 sta_info_stop(local);
1646 rate_control_deinitialize(local);
Jiri Bence9f207f2007-05-05 11:46:38 -07001647 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001648
Jiri Bencf0706e82007-05-05 11:45:53 -07001649 if (skb_queue_len(&local->skb_queue)
1650 || skb_queue_len(&local->skb_queue_unreliable))
1651 printk(KERN_WARNING "%s: skb_queue not empty\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001652 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001653 skb_queue_purge(&local->skb_queue);
1654 skb_queue_purge(&local->skb_queue_unreliable);
1655
1656 destroy_workqueue(local->hw.workqueue);
1657 wiphy_unregister(local->hw.wiphy);
1658 ieee80211_wep_free(local);
1659 ieee80211_led_exit(local);
1660}
1661EXPORT_SYMBOL(ieee80211_unregister_hw);
1662
1663void ieee80211_free_hw(struct ieee80211_hw *hw)
1664{
1665 struct ieee80211_local *local = hw_to_local(hw);
1666
1667 ieee80211_if_free(local->mdev);
1668 wiphy_free(local->hw.wiphy);
1669}
1670EXPORT_SYMBOL(ieee80211_free_hw);
1671
Jiri Bencf0706e82007-05-05 11:45:53 -07001672static int __init ieee80211_init(void)
1673{
1674 struct sk_buff *skb;
1675 int ret;
1676
1677 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1678
Johannes Berg4b475892008-01-02 15:17:03 +01001679 ret = rc80211_simple_init();
Johannes Bergac71c692007-10-28 14:17:44 +01001680 if (ret)
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001681 goto out;
Mattias Nisslerad018372007-12-19 01:25:57 +01001682
Johannes Berg4b475892008-01-02 15:17:03 +01001683 ret = rc80211_pid_init();
Mattias Nisslerad018372007-12-19 01:25:57 +01001684 if (ret)
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001685 goto out_cleanup_simple;
Johannes Bergac71c692007-10-28 14:17:44 +01001686
Jiri Bencf0706e82007-05-05 11:45:53 -07001687 ret = ieee80211_wme_register();
1688 if (ret) {
1689 printk(KERN_DEBUG "ieee80211_init: failed to "
1690 "initialize WME (err=%d)\n", ret);
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001691 goto out_cleanup_pid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001692 }
1693
Jiri Bence9f207f2007-05-05 11:46:38 -07001694 ieee80211_debugfs_netdev_init();
1695
Jiri Bencf0706e82007-05-05 11:45:53 -07001696 return 0;
Mattias Nisslerad018372007-12-19 01:25:57 +01001697
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001698 out_cleanup_pid:
Johannes Berg4b475892008-01-02 15:17:03 +01001699 rc80211_pid_exit();
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001700 out_cleanup_simple:
1701 rc80211_simple_exit();
1702 out:
Mattias Nisslerad018372007-12-19 01:25:57 +01001703 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07001704}
1705
Jiri Bencf0706e82007-05-05 11:45:53 -07001706static void __exit ieee80211_exit(void)
1707{
Johannes Berg4b475892008-01-02 15:17:03 +01001708 rc80211_simple_exit();
1709 rc80211_pid_exit();
Johannes Bergac71c692007-10-28 14:17:44 +01001710
Jiri Bencf0706e82007-05-05 11:45:53 -07001711 ieee80211_wme_unregister();
Jiri Bence9f207f2007-05-05 11:46:38 -07001712 ieee80211_debugfs_netdev_exit();
Jiri Bencf0706e82007-05-05 11:45:53 -07001713}
1714
1715
Johannes Bergca9938f2007-09-11 12:50:32 +02001716subsys_initcall(ieee80211_init);
Jiri Bencf0706e82007-05-05 11:45:53 -07001717module_exit(ieee80211_exit);
1718
1719MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1720MODULE_LICENSE("GPL");