blob: 2133c9fd27a4b2cf1f392a9844b5515de3a97ed4 [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
Johannes Berg665e8aa2008-02-21 01:10:07 +0100186 if (ndev != dev && ndev != local->mdev && netif_running(ndev)) {
187 /*
188 * Allow only a single IBSS interface to be up at any
189 * time. This is restricted because beacon distribution
190 * cannot work properly if both are in the same IBSS.
191 *
192 * To remove this restriction we'd have to disallow them
193 * from setting the same SSID on different IBSS interfaces
194 * belonging to the same hardware. Then, however, we're
195 * faced with having to adopt two different TSF timers...
196 */
197 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
198 nsdata->vif.type == IEEE80211_IF_TYPE_IBSS)
199 return -EBUSY;
200
201 /*
202 * Disallow multiple IBSS/STA mode interfaces.
203 *
204 * This is a technical restriction, it is possible although
205 * most likely not IEEE 802.11 compliant to have multiple
206 * STAs with just a single hardware (the TSF timer will not
207 * be adjusted properly.)
208 *
209 * However, because mac80211 uses the master device's BSS
210 * information for each STA/IBSS interface, doing this will
211 * currently corrupt that BSS information completely, unless,
212 * a not very useful case, both STAs are associated to the
213 * same BSS.
214 *
215 * To remove this restriction, the BSS information needs to
216 * be embedded in the STA/IBSS mode sdata instead of using
217 * the master device's BSS structure.
218 */
219 if ((sdata->vif.type == IEEE80211_IF_TYPE_STA ||
220 sdata->vif.type == IEEE80211_IF_TYPE_IBSS) &&
221 (nsdata->vif.type == IEEE80211_IF_TYPE_STA ||
222 nsdata->vif.type == IEEE80211_IF_TYPE_IBSS))
223 return -EBUSY;
224
225 /*
226 * The remaining checks are only performed for interfaces
227 * with the same MAC address.
228 */
229 if (compare_ether_addr(dev->dev_addr, ndev->dev_addr))
230 continue;
231
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400232 /*
233 * check whether it may have the same address
234 */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100235 if (!identical_mac_addr_allowed(sdata->vif.type,
236 nsdata->vif.type))
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400237 return -ENOTUNIQ;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400238
239 /*
240 * can only add VLANs to enabled APs
241 */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100242 if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN &&
Johannes Berg665e8aa2008-02-21 01:10:07 +0100243 nsdata->vif.type == IEEE80211_IF_TYPE_AP)
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400244 sdata->u.vlan.ap = nsdata;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200245 }
246 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200247
Johannes Berg51fb61e2007-12-19 01:31:27 +0100248 switch (sdata->vif.type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400249 case IEEE80211_IF_TYPE_WDS:
250 if (is_zero_ether_addr(sdata->u.wds.remote_addr))
251 return -ENOLINK;
252 break;
253 case IEEE80211_IF_TYPE_VLAN:
254 if (!sdata->u.vlan.ap)
255 return -ENOLINK;
256 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +0200257 case IEEE80211_IF_TYPE_AP:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +0200258 case IEEE80211_IF_TYPE_STA:
259 case IEEE80211_IF_TYPE_MNTR:
260 case IEEE80211_IF_TYPE_IBSS:
261 /* no special treatment */
262 break;
Johannes Berga2897552007-09-28 14:01:25 +0200263 case IEEE80211_IF_TYPE_INVALID:
264 /* cannot happen */
265 WARN_ON(1);
266 break;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400267 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200268
Johannes Bergb2c258f2007-07-27 15:43:23 +0200269 if (local->open_count == 0) {
270 res = 0;
Johannes Berg4150c572007-09-17 01:29:23 -0400271 if (local->ops->start)
272 res = local->ops->start(local_to_hw(local));
273 if (res)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200274 return res;
Michael Bueschceffefd2008-02-10 14:16:52 +0100275 need_hw_reconfig = 1;
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100276 ieee80211_led_radio(local, local->hw.conf.radio_enabled);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200277 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200278
Johannes Berg51fb61e2007-12-19 01:31:27 +0100279 switch (sdata->vif.type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400280 case IEEE80211_IF_TYPE_VLAN:
281 list_add(&sdata->u.vlan.list, &sdata->u.vlan.ap->u.ap.vlans);
282 /* no need to tell driver */
283 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400284 case IEEE80211_IF_TYPE_MNTR:
Michael Wu3d30d942008-01-31 19:48:27 +0100285 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
286 local->cooked_mntrs++;
287 break;
288 }
289
Johannes Berg4150c572007-09-17 01:29:23 -0400290 /* must be before the call to ieee80211_configure_filter */
Johannes Bergb2c258f2007-07-27 15:43:23 +0200291 local->monitors++;
Michael Wu8cc9a732008-01-31 19:48:23 +0100292 if (local->monitors == 1)
Johannes Berg4150c572007-09-17 01:29:23 -0400293 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
Michael Wu8cc9a732008-01-31 19:48:23 +0100294
295 if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
296 local->fif_fcsfail++;
297 if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
298 local->fif_plcpfail++;
299 if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
300 local->fif_control++;
301 if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
302 local->fif_other_bss++;
303
304 netif_tx_lock_bh(local->mdev);
305 ieee80211_configure_filter(local);
306 netif_tx_unlock_bh(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -0400307 break;
308 case IEEE80211_IF_TYPE_STA:
309 case IEEE80211_IF_TYPE_IBSS:
310 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
311 /* fall through */
312 default:
Johannes Berg32bfd352007-12-19 01:31:26 +0100313 conf.vif = &sdata->vif;
Johannes Berg51fb61e2007-12-19 01:31:27 +0100314 conf.type = sdata->vif.type;
Johannes Berg4150c572007-09-17 01:29:23 -0400315 conf.mac_addr = dev->dev_addr;
316 res = local->ops->add_interface(local_to_hw(local), &conf);
317 if (res && !local->open_count && local->ops->stop)
318 local->ops->stop(local_to_hw(local));
319 if (res)
320 return res;
321
Johannes Bergb2c258f2007-07-27 15:43:23 +0200322 ieee80211_if_config(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200323 ieee80211_reset_erp_info(dev);
Johannes Berg11a843b2007-08-28 17:01:55 -0400324 ieee80211_enable_keys(sdata);
Johannes Berg4150c572007-09-17 01:29:23 -0400325
Johannes Berg51fb61e2007-12-19 01:31:27 +0100326 if (sdata->vif.type == IEEE80211_IF_TYPE_STA &&
Johannes Bergddd3d2b2007-09-26 17:53:20 +0200327 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
Johannes Berg4150c572007-09-17 01:29:23 -0400328 netif_carrier_off(dev);
329 else
330 netif_carrier_on(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200331 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200332
Johannes Berg4150c572007-09-17 01:29:23 -0400333 if (local->open_count == 0) {
334 res = dev_open(local->mdev);
335 WARN_ON(res);
Johannes Berg4150c572007-09-17 01:29:23 -0400336 tasklet_enable(&local->tx_pending_tasklet);
337 tasklet_enable(&local->tasklet);
338 }
339
Johannes Bergc1428b32007-11-16 02:54:53 +0100340 /*
341 * set_multicast_list will be invoked by the networking core
342 * which will check whether any increments here were done in
343 * error and sync them down to the hardware as filter flags.
344 */
345 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
346 atomic_inc(&local->iff_allmultis);
347
348 if (sdata->flags & IEEE80211_SDATA_PROMISC)
349 atomic_inc(&local->iff_promiscs);
350
Johannes Berg4150c572007-09-17 01:29:23 -0400351 local->open_count++;
Michael Bueschceffefd2008-02-10 14:16:52 +0100352 if (need_hw_reconfig)
353 ieee80211_hw_config(local);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200354
355 netif_start_queue(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400356
Johannes Bergb2c258f2007-07-27 15:43:23 +0200357 return 0;
358}
359
Johannes Berg4150c572007-09-17 01:29:23 -0400360static int ieee80211_stop(struct net_device *dev)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200361{
Johannes Berg4150c572007-09-17 01:29:23 -0400362 struct ieee80211_sub_if_data *sdata;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200363 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg4150c572007-09-17 01:29:23 -0400364 struct ieee80211_if_init_conf conf;
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200365 struct sta_info *sta;
366 int i;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200367
Johannes Berg4150c572007-09-17 01:29:23 -0400368 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
369
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200370 list_for_each_entry(sta, &local->sta_list, list) {
Ron Rindjunsky5b3d71d2008-01-13 18:21:58 +0200371 if (sta->dev == dev)
372 for (i = 0; i < STA_TID_NUM; i++)
373 ieee80211_sta_stop_rx_ba_session(sta->dev,
374 sta->addr, i,
375 WLAN_BACK_RECIPIENT,
Ron Rindjunsky07db2182007-12-25 17:00:33 +0200376 WLAN_REASON_QSTA_LEAVE_QBSS);
377 }
378
Johannes Berg4150c572007-09-17 01:29:23 -0400379 netif_stop_queue(dev);
380
Johannes Bergc1428b32007-11-16 02:54:53 +0100381 /*
382 * Don't count this interface for promisc/allmulti while it
383 * is down. dev_mc_unsync() will invoke set_multicast_list
384 * on the master interface which will sync these down to the
385 * hardware as filter flags.
386 */
387 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
388 atomic_dec(&local->iff_allmultis);
389
390 if (sdata->flags & IEEE80211_SDATA_PROMISC)
391 atomic_dec(&local->iff_promiscs);
392
Johannes Berg4150c572007-09-17 01:29:23 -0400393 dev_mc_unsync(local->mdev, dev);
394
Johannes Berg5dfdaf52007-12-19 02:03:33 +0100395 /* APs need special treatment */
Johannes Berg51fb61e2007-12-19 01:31:27 +0100396 if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400397 struct ieee80211_sub_if_data *vlan, *tmp;
Johannes Berg5dfdaf52007-12-19 02:03:33 +0100398 struct beacon_data *old_beacon = sdata->u.ap.beacon;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400399
Johannes Berg5dfdaf52007-12-19 02:03:33 +0100400 /* remove beacon */
401 rcu_assign_pointer(sdata->u.ap.beacon, NULL);
402 synchronize_rcu();
403 kfree(old_beacon);
404
405 /* down all dependent devices, that is VLANs */
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400406 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
407 u.vlan.list)
408 dev_close(vlan->dev);
409 WARN_ON(!list_empty(&sdata->u.ap.vlans));
410 }
411
Johannes Berg4150c572007-09-17 01:29:23 -0400412 local->open_count--;
413
Johannes Berg51fb61e2007-12-19 01:31:27 +0100414 switch (sdata->vif.type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400415 case IEEE80211_IF_TYPE_VLAN:
416 list_del(&sdata->u.vlan.list);
417 sdata->u.vlan.ap = NULL;
418 /* no need to tell driver */
419 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400420 case IEEE80211_IF_TYPE_MNTR:
Michael Wu3d30d942008-01-31 19:48:27 +0100421 if (sdata->u.mntr_flags & MONITOR_FLAG_COOK_FRAMES) {
422 local->cooked_mntrs--;
423 break;
424 }
425
Johannes Berg4150c572007-09-17 01:29:23 -0400426 local->monitors--;
Michael Wu8cc9a732008-01-31 19:48:23 +0100427 if (local->monitors == 0)
Michael Wu8b393f12007-11-28 01:57:08 -0500428 local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
Michael Wu8cc9a732008-01-31 19:48:23 +0100429
430 if (sdata->u.mntr_flags & MONITOR_FLAG_FCSFAIL)
431 local->fif_fcsfail--;
432 if (sdata->u.mntr_flags & MONITOR_FLAG_PLCPFAIL)
433 local->fif_plcpfail--;
434 if (sdata->u.mntr_flags & MONITOR_FLAG_CONTROL)
435 local->fif_control--;
436 if (sdata->u.mntr_flags & MONITOR_FLAG_OTHER_BSS)
437 local->fif_other_bss--;
438
439 netif_tx_lock_bh(local->mdev);
440 ieee80211_configure_filter(local);
441 netif_tx_unlock_bh(local->mdev);
Johannes Berg4150c572007-09-17 01:29:23 -0400442 break;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200443 case IEEE80211_IF_TYPE_STA:
444 case IEEE80211_IF_TYPE_IBSS:
445 sdata->u.sta.state = IEEE80211_DISABLED;
446 del_timer_sync(&sdata->u.sta.timer);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400447 /*
Johannes Berg79010422007-09-18 17:29:21 -0400448 * When we get here, the interface is marked down.
449 * Call synchronize_rcu() to wait for the RX path
450 * should it be using the interface and enqueuing
451 * frames at this very time on another CPU.
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400452 */
Johannes Berg79010422007-09-18 17:29:21 -0400453 synchronize_rcu();
Johannes Bergb2c258f2007-07-27 15:43:23 +0200454 skb_queue_purge(&sdata->u.sta.skb_queue);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400455
Zhu Yiece8edd2007-11-22 10:53:21 +0800456 if (local->scan_dev == sdata->dev) {
457 if (!local->ops->hw_scan) {
458 local->sta_sw_scanning = 0;
459 cancel_delayed_work(&local->scan_work);
460 } else
461 local->sta_hw_scanning = 0;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200462 }
Zhu Yiece8edd2007-11-22 10:53:21 +0800463
Johannes Bergb2c258f2007-07-27 15:43:23 +0200464 flush_workqueue(local->hw.workqueue);
Zhu Yia10605e2007-11-22 11:10:22 +0800465
466 sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
467 kfree(sdata->u.sta.extra_ie);
468 sdata->u.sta.extra_ie = NULL;
469 sdata->u.sta.extra_ie_len = 0;
Johannes Berg4150c572007-09-17 01:29:23 -0400470 /* fall through */
471 default:
Johannes Berg32bfd352007-12-19 01:31:26 +0100472 conf.vif = &sdata->vif;
Johannes Berg51fb61e2007-12-19 01:31:27 +0100473 conf.type = sdata->vif.type;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200474 conf.mac_addr = dev->dev_addr;
Johannes Berg4150c572007-09-17 01:29:23 -0400475 /* disable all keys for as long as this netdev is down */
476 ieee80211_disable_keys(sdata);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200477 local->ops->remove_interface(local_to_hw(local), &conf);
478 }
479
Johannes Berg4150c572007-09-17 01:29:23 -0400480 if (local->open_count == 0) {
481 if (netif_running(local->mdev))
482 dev_close(local->mdev);
483
Johannes Berg4150c572007-09-17 01:29:23 -0400484 if (local->ops->stop)
485 local->ops->stop(local_to_hw(local));
486
Ivo van Doorncdcb0062008-01-07 19:45:24 +0100487 ieee80211_led_radio(local, 0);
488
Johannes Berg4150c572007-09-17 01:29:23 -0400489 tasklet_disable(&local->tx_pending_tasklet);
490 tasklet_disable(&local->tasklet);
491 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200492
493 return 0;
494}
495
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200496int ieee80211_start_tx_ba_session(struct ieee80211_hw *hw, u8 *ra, u16 tid)
497{
498 struct ieee80211_local *local = hw_to_local(hw);
499 struct sta_info *sta;
500 struct ieee80211_sub_if_data *sdata;
501 u16 start_seq_num = 0;
502 u8 *state;
503 int ret;
504 DECLARE_MAC_BUF(mac);
505
506 if (tid >= STA_TID_NUM)
507 return -EINVAL;
508
509#ifdef CONFIG_MAC80211_HT_DEBUG
510 printk(KERN_DEBUG "Open BA session requested for %s tid %u\n",
511 print_mac(mac, ra), tid);
512#endif /* CONFIG_MAC80211_HT_DEBUG */
513
514 sta = sta_info_get(local, ra);
515 if (!sta) {
516 printk(KERN_DEBUG "Could not find the station\n");
517 return -ENOENT;
518 }
519
520 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
521
522 /* we have tried too many times, receiver does not want A-MPDU */
523 if (sta->ampdu_mlme.tid_tx[tid].addba_req_num > HT_AGG_MAX_RETRIES) {
524 ret = -EBUSY;
525 goto start_ba_exit;
526 }
527
528 state = &sta->ampdu_mlme.tid_tx[tid].state;
529 /* check if the TID is not in aggregation flow already */
530 if (*state != HT_AGG_STATE_IDLE) {
531#ifdef CONFIG_MAC80211_HT_DEBUG
532 printk(KERN_DEBUG "BA request denied - session is not "
533 "idle on tid %u\n", tid);
534#endif /* CONFIG_MAC80211_HT_DEBUG */
535 ret = -EAGAIN;
536 goto start_ba_exit;
537 }
538
539 /* ensure that TX flow won't interrupt us
540 * until the end of the call to requeue function */
541 spin_lock_bh(&local->mdev->queue_lock);
542
543 /* create a new queue for this aggregation */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200544 ret = ieee80211_ht_agg_queue_add(local, sta, tid);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200545
546 /* case no queue is available to aggregation
547 * don't switch to aggregation */
548 if (ret) {
549#ifdef CONFIG_MAC80211_HT_DEBUG
550 printk(KERN_DEBUG "BA request denied - no queue available for"
551 " tid %d\n", tid);
552#endif /* CONFIG_MAC80211_HT_DEBUG */
553 spin_unlock_bh(&local->mdev->queue_lock);
554 goto start_ba_exit;
555 }
556 sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
557
558 /* Ok, the Addba frame hasn't been sent yet, but if the driver calls the
559 * call back right away, it must see that the flow has begun */
560 *state |= HT_ADDBA_REQUESTED_MSK;
561
562 if (local->ops->ampdu_action)
563 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_START,
564 ra, tid, &start_seq_num);
565
566 if (ret) {
567 /* No need to requeue the packets in the agg queue, since we
568 * held the tx lock: no packet could be enqueued to the newly
569 * allocated queue */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200570 ieee80211_ht_agg_queue_remove(local, sta, tid, 0);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200571#ifdef CONFIG_MAC80211_HT_DEBUG
572 printk(KERN_DEBUG "BA request denied - HW or queue unavailable"
573 " for tid %d\n", tid);
574#endif /* CONFIG_MAC80211_HT_DEBUG */
575 spin_unlock_bh(&local->mdev->queue_lock);
576 *state = HT_AGG_STATE_IDLE;
577 goto start_ba_exit;
578 }
579
580 /* Will put all the packets in the new SW queue */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200581 ieee80211_requeue(local, ieee802_1d_to_ac[tid]);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200582 spin_unlock_bh(&local->mdev->queue_lock);
583
584 /* We have most probably almost emptied the legacy queue */
585 /* ieee80211_wake_queue(local_to_hw(local), ieee802_1d_to_ac[tid]); */
586
587 /* send an addBA request */
588 sta->ampdu_mlme.dialog_token_allocator++;
589 sta->ampdu_mlme.tid_tx[tid].dialog_token =
590 sta->ampdu_mlme.dialog_token_allocator;
591 sta->ampdu_mlme.tid_tx[tid].ssn = start_seq_num;
592
593 ieee80211_send_addba_request(sta->dev, ra, tid,
594 sta->ampdu_mlme.tid_tx[tid].dialog_token,
595 sta->ampdu_mlme.tid_tx[tid].ssn,
596 0x40, 5000);
597
598 /* activate the timer for the recipient's addBA response */
599 sta->ampdu_mlme.tid_tx[tid].addba_resp_timer.expires =
600 jiffies + ADDBA_RESP_INTERVAL;
601 add_timer(&sta->ampdu_mlme.tid_tx[tid].addba_resp_timer);
602 printk(KERN_DEBUG "activated addBA response timer on tid %d\n", tid);
603
604start_ba_exit:
605 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
606 sta_info_put(sta);
607 return ret;
608}
609EXPORT_SYMBOL(ieee80211_start_tx_ba_session);
610
611int ieee80211_stop_tx_ba_session(struct ieee80211_hw *hw,
612 u8 *ra, u16 tid,
613 enum ieee80211_back_parties initiator)
614{
615 struct ieee80211_local *local = hw_to_local(hw);
616 struct sta_info *sta;
617 u8 *state;
618 int ret = 0;
619 DECLARE_MAC_BUF(mac);
620
621 if (tid >= STA_TID_NUM)
622 return -EINVAL;
623
624#ifdef CONFIG_MAC80211_HT_DEBUG
625 printk(KERN_DEBUG "Stop a BA session requested for %s tid %u\n",
626 print_mac(mac, ra), tid);
627#endif /* CONFIG_MAC80211_HT_DEBUG */
628
629 sta = sta_info_get(local, ra);
630 if (!sta)
631 return -ENOENT;
632
633 /* check if the TID is in aggregation */
634 state = &sta->ampdu_mlme.tid_tx[tid].state;
635 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
636
637 if (*state != HT_AGG_STATE_OPERATIONAL) {
638#ifdef CONFIG_MAC80211_HT_DEBUG
639 printk(KERN_DEBUG "Try to stop Tx aggregation on"
640 " non active TID\n");
641#endif /* CONFIG_MAC80211_HT_DEBUG */
642 ret = -ENOENT;
643 goto stop_BA_exit;
644 }
645
646 ieee80211_stop_queue(hw, sta->tid_to_tx_q[tid]);
647
648 *state = HT_AGG_STATE_REQ_STOP_BA_MSK |
649 (initiator << HT_AGG_STATE_INITIATOR_SHIFT);
650
651 if (local->ops->ampdu_action)
652 ret = local->ops->ampdu_action(hw, IEEE80211_AMPDU_TX_STOP,
653 ra, tid, NULL);
654
655 /* case HW denied going back to legacy */
656 if (ret) {
657 WARN_ON(ret != -EBUSY);
658 *state = HT_AGG_STATE_OPERATIONAL;
659 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
660 goto stop_BA_exit;
661 }
662
663stop_BA_exit:
664 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
665 sta_info_put(sta);
666 return ret;
667}
668EXPORT_SYMBOL(ieee80211_stop_tx_ba_session);
669
670void ieee80211_start_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u16 tid)
671{
672 struct ieee80211_local *local = hw_to_local(hw);
673 struct sta_info *sta;
674 u8 *state;
675 DECLARE_MAC_BUF(mac);
676
677 if (tid >= STA_TID_NUM) {
678 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
679 tid, STA_TID_NUM);
680 return;
681 }
682
683 sta = sta_info_get(local, ra);
684 if (!sta) {
685 printk(KERN_DEBUG "Could not find station: %s\n",
686 print_mac(mac, ra));
687 return;
688 }
689
690 state = &sta->ampdu_mlme.tid_tx[tid].state;
691 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
692
693 if (!(*state & HT_ADDBA_REQUESTED_MSK)) {
694 printk(KERN_DEBUG "addBA was not requested yet, state is %d\n",
695 *state);
696 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
697 sta_info_put(sta);
698 return;
699 }
700
701 WARN_ON_ONCE(*state & HT_ADDBA_DRV_READY_MSK);
702
703 *state |= HT_ADDBA_DRV_READY_MSK;
704
705 if (*state == HT_AGG_STATE_OPERATIONAL) {
706 printk(KERN_DEBUG "Aggregation is on for tid %d \n", tid);
707 ieee80211_wake_queue(hw, sta->tid_to_tx_q[tid]);
708 }
709 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
710 sta_info_put(sta);
711}
712EXPORT_SYMBOL(ieee80211_start_tx_ba_cb);
713
714void ieee80211_stop_tx_ba_cb(struct ieee80211_hw *hw, u8 *ra, u8 tid)
715{
716 struct ieee80211_local *local = hw_to_local(hw);
717 struct sta_info *sta;
718 u8 *state;
719 int agg_queue;
720 DECLARE_MAC_BUF(mac);
721
722 if (tid >= STA_TID_NUM) {
723 printk(KERN_DEBUG "Bad TID value: tid = %d (>= %d)\n",
724 tid, STA_TID_NUM);
725 return;
726 }
727
728 printk(KERN_DEBUG "Stop a BA session requested on DA %s tid %d\n",
729 print_mac(mac, ra), tid);
730
731 sta = sta_info_get(local, ra);
732 if (!sta) {
733 printk(KERN_DEBUG "Could not find station: %s\n",
734 print_mac(mac, ra));
735 return;
736 }
737 state = &sta->ampdu_mlme.tid_tx[tid].state;
738
739 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
740 if ((*state & HT_AGG_STATE_REQ_STOP_BA_MSK) == 0) {
741 printk(KERN_DEBUG "unexpected callback to A-MPDU stop\n");
742 sta_info_put(sta);
743 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
744 return;
745 }
746
747 if (*state & HT_AGG_STATE_INITIATOR_MSK)
748 ieee80211_send_delba(sta->dev, ra, tid,
749 WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
750
751 agg_queue = sta->tid_to_tx_q[tid];
752
753 /* avoid ordering issues: we are the only one that can modify
754 * the content of the qdiscs */
755 spin_lock_bh(&local->mdev->queue_lock);
756 /* remove the queue for this aggregation */
Ron Rindjunsky9e723492008-01-28 14:07:18 +0200757 ieee80211_ht_agg_queue_remove(local, sta, tid, 1);
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +0200758 spin_unlock_bh(&local->mdev->queue_lock);
759
760 /* we just requeued the all the frames that were in the removed
761 * queue, and since we might miss a softirq we do netif_schedule.
762 * ieee80211_wake_queue is not used here as this queue is not
763 * necessarily stopped */
764 netif_schedule(local->mdev);
765 *state = HT_AGG_STATE_IDLE;
766 sta->ampdu_mlme.tid_tx[tid].addba_req_num = 0;
767 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
768
769 sta_info_put(sta);
770}
771EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb);
772
773void ieee80211_start_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
774 const u8 *ra, u16 tid)
775{
776 struct ieee80211_local *local = hw_to_local(hw);
777 struct ieee80211_ra_tid *ra_tid;
778 struct sk_buff *skb = dev_alloc_skb(0);
779
780 if (unlikely(!skb)) {
781 if (net_ratelimit())
782 printk(KERN_WARNING "%s: Not enough memory, "
783 "dropping start BA session", skb->dev->name);
784 return;
785 }
786 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
787 memcpy(&ra_tid->ra, ra, ETH_ALEN);
788 ra_tid->tid = tid;
789
790 skb->pkt_type = IEEE80211_ADDBA_MSG;
791 skb_queue_tail(&local->skb_queue, skb);
792 tasklet_schedule(&local->tasklet);
793}
794EXPORT_SYMBOL(ieee80211_start_tx_ba_cb_irqsafe);
795
796void ieee80211_stop_tx_ba_cb_irqsafe(struct ieee80211_hw *hw,
797 const u8 *ra, u16 tid)
798{
799 struct ieee80211_local *local = hw_to_local(hw);
800 struct ieee80211_ra_tid *ra_tid;
801 struct sk_buff *skb = dev_alloc_skb(0);
802
803 if (unlikely(!skb)) {
804 if (net_ratelimit())
805 printk(KERN_WARNING "%s: Not enough memory, "
806 "dropping stop BA session", skb->dev->name);
807 return;
808 }
809 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
810 memcpy(&ra_tid->ra, ra, ETH_ALEN);
811 ra_tid->tid = tid;
812
813 skb->pkt_type = IEEE80211_DELBA_MSG;
814 skb_queue_tail(&local->skb_queue, skb);
815 tasklet_schedule(&local->tasklet);
816}
817EXPORT_SYMBOL(ieee80211_stop_tx_ba_cb_irqsafe);
818
Johannes Bergb2c258f2007-07-27 15:43:23 +0200819static void ieee80211_set_multicast_list(struct net_device *dev)
820{
821 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
822 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400823 int allmulti, promisc, sdata_allmulti, sdata_promisc;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200824
Johannes Berg4150c572007-09-17 01:29:23 -0400825 allmulti = !!(dev->flags & IFF_ALLMULTI);
826 promisc = !!(dev->flags & IFF_PROMISC);
Johannes Bergb52f2192007-11-16 01:49:11 +0100827 sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
828 sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
Johannes Berg4150c572007-09-17 01:29:23 -0400829
830 if (allmulti != sdata_allmulti) {
831 if (dev->flags & IFF_ALLMULTI)
Johannes Berg53918992007-09-26 15:19:47 +0200832 atomic_inc(&local->iff_allmultis);
Johannes Berg4150c572007-09-17 01:29:23 -0400833 else
Johannes Berg53918992007-09-26 15:19:47 +0200834 atomic_dec(&local->iff_allmultis);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400835 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200836 }
Johannes Berg4150c572007-09-17 01:29:23 -0400837
838 if (promisc != sdata_promisc) {
839 if (dev->flags & IFF_PROMISC)
Johannes Berg53918992007-09-26 15:19:47 +0200840 atomic_inc(&local->iff_promiscs);
Johannes Berg4150c572007-09-17 01:29:23 -0400841 else
Johannes Berg53918992007-09-26 15:19:47 +0200842 atomic_dec(&local->iff_promiscs);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400843 sdata->flags ^= IEEE80211_SDATA_PROMISC;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200844 }
Johannes Berg4150c572007-09-17 01:29:23 -0400845
846 dev_mc_sync(local->mdev, dev);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200847}
848
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700849static const struct header_ops ieee80211_header_ops = {
850 .create = eth_header,
851 .parse = header_parse_80211,
852 .rebuild = eth_rebuild_header,
853 .cache = eth_header_cache,
854 .cache_update = eth_header_cache_update,
855};
856
Johannes Bergf9d540e2007-09-28 14:02:09 +0200857/* Must not be called for mdev */
Johannes Bergb2c258f2007-07-27 15:43:23 +0200858void ieee80211_if_setup(struct net_device *dev)
859{
860 ether_setup(dev);
861 dev->hard_start_xmit = ieee80211_subif_start_xmit;
862 dev->wireless_handlers = &ieee80211_iw_handler_def;
863 dev->set_multicast_list = ieee80211_set_multicast_list;
864 dev->change_mtu = ieee80211_change_mtu;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200865 dev->open = ieee80211_open;
866 dev->stop = ieee80211_stop;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200867 dev->destructor = ieee80211_if_free;
868}
869
870/* WDS specialties */
871
872int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
873{
874 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
875 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
876 struct sta_info *sta;
Joe Perches0795af52007-10-03 17:59:30 -0700877 DECLARE_MAC_BUF(mac);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200878
879 if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
880 return 0;
881
882 /* Create STA entry for the new peer */
883 sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
Johannes Berg43ba7e92008-02-21 14:09:30 +0100884 if (IS_ERR(sta))
885 return PTR_ERR(sta);
Johannes Berg238814f2008-01-28 17:19:37 +0100886
887 sta->flags |= WLAN_STA_AUTHORIZED;
888
Johannes Bergb2c258f2007-07-27 15:43:23 +0200889 sta_info_put(sta);
890
891 /* Remove STA entry for the old peer */
892 sta = sta_info_get(local, sdata->u.wds.remote_addr);
893 if (sta) {
Michael Wube8755e2007-07-27 15:43:23 +0200894 sta_info_free(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200895 sta_info_put(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200896 } else {
897 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
Joe Perches0795af52007-10-03 17:59:30 -0700898 "peer %s\n",
899 dev->name, print_mac(mac, sdata->u.wds.remote_addr));
Johannes Bergb2c258f2007-07-27 15:43:23 +0200900 }
901
902 /* Update WDS link data */
903 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
904
905 return 0;
906}
907
908/* everything else */
909
Johannes Bergb2c258f2007-07-27 15:43:23 +0200910static int __ieee80211_if_config(struct net_device *dev,
911 struct sk_buff *beacon,
912 struct ieee80211_tx_control *control)
913{
914 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
915 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
916 struct ieee80211_if_conf conf;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200917
918 if (!local->ops->config_interface || !netif_running(dev))
919 return 0;
920
921 memset(&conf, 0, sizeof(conf));
Johannes Berg51fb61e2007-12-19 01:31:27 +0100922 conf.type = sdata->vif.type;
923 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
924 sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
Johannes Berg4150c572007-09-17 01:29:23 -0400925 conf.bssid = sdata->u.sta.bssid;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200926 conf.ssid = sdata->u.sta.ssid;
927 conf.ssid_len = sdata->u.sta.ssid_len;
Johannes Berg51fb61e2007-12-19 01:31:27 +0100928 } else if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
Johannes Bergb2c258f2007-07-27 15:43:23 +0200929 conf.ssid = sdata->u.ap.ssid;
930 conf.ssid_len = sdata->u.ap.ssid_len;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200931 conf.beacon = beacon;
932 conf.beacon_control = control;
933 }
934 return local->ops->config_interface(local_to_hw(local),
Johannes Berg32bfd352007-12-19 01:31:26 +0100935 &sdata->vif, &conf);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200936}
937
938int ieee80211_if_config(struct net_device *dev)
939{
940 return __ieee80211_if_config(dev, NULL, NULL);
941}
942
943int ieee80211_if_config_beacon(struct net_device *dev)
944{
945 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
946 struct ieee80211_tx_control control;
Johannes Berg32bfd352007-12-19 01:31:26 +0100947 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200948 struct sk_buff *skb;
949
950 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
951 return 0;
Johannes Berg32bfd352007-12-19 01:31:26 +0100952 skb = ieee80211_beacon_get(local_to_hw(local), &sdata->vif,
953 &control);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200954 if (!skb)
955 return -ENOMEM;
956 return __ieee80211_if_config(dev, skb, &control);
957}
958
959int ieee80211_hw_config(struct ieee80211_local *local)
960{
Johannes Bergb2c258f2007-07-27 15:43:23 +0200961 struct ieee80211_channel *chan;
962 int ret = 0;
963
Johannes Berg8318d782008-01-24 19:38:38 +0100964 if (local->sta_sw_scanning)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200965 chan = local->scan_channel;
Johannes Berg8318d782008-01-24 19:38:38 +0100966 else
Johannes Bergb2c258f2007-07-27 15:43:23 +0200967 chan = local->oper_channel;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200968
Johannes Berg8318d782008-01-24 19:38:38 +0100969 local->hw.conf.channel = chan;
970
971 if (!local->hw.conf.power_level)
972 local->hw.conf.power_level = chan->max_power;
973 else
974 local->hw.conf.power_level = min(chan->max_power,
975 local->hw.conf.power_level);
976
977 local->hw.conf.max_antenna_gain = chan->max_antenna_gain;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200978
979#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
Johannes Berg8318d782008-01-24 19:38:38 +0100980 printk(KERN_DEBUG "%s: HW CONFIG: freq=%d\n",
981 wiphy_name(local->hw.wiphy), chan->center_freq);
982#endif
Johannes Bergb2c258f2007-07-27 15:43:23 +0200983
Michael Bueschf7c4dae2007-09-24 18:41:49 +0200984 if (local->open_count)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200985 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
986
987 return ret;
988}
989
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +0200990/**
991 * ieee80211_hw_config_ht should be used only after legacy configuration
992 * has been determined, as ht configuration depends upon the hardware's
993 * HT abilities for a _specific_ band.
994 */
995int ieee80211_hw_config_ht(struct ieee80211_local *local, int enable_ht,
996 struct ieee80211_ht_info *req_ht_cap,
997 struct ieee80211_ht_bss_info *req_bss_cap)
998{
999 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg8318d782008-01-24 19:38:38 +01001000 struct ieee80211_supported_band *sband;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001001 int i;
1002
Johannes Berg8318d782008-01-24 19:38:38 +01001003 sband = local->hw.wiphy->bands[conf->channel->band];
1004
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001005 /* HT is not supported */
Johannes Berg8318d782008-01-24 19:38:38 +01001006 if (!sband->ht_info.ht_supported) {
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001007 conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
1008 return -EOPNOTSUPP;
1009 }
1010
1011 /* disable HT */
1012 if (!enable_ht) {
1013 conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
1014 } else {
1015 conf->flags |= IEEE80211_CONF_SUPPORT_HT_MODE;
Johannes Berg8318d782008-01-24 19:38:38 +01001016 conf->ht_conf.cap = req_ht_cap->cap & sband->ht_info.cap;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001017 conf->ht_conf.cap &= ~(IEEE80211_HT_CAP_MIMO_PS);
1018 conf->ht_conf.cap |=
Johannes Berg8318d782008-01-24 19:38:38 +01001019 sband->ht_info.cap & IEEE80211_HT_CAP_MIMO_PS;
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001020 conf->ht_bss_conf.primary_channel =
1021 req_bss_cap->primary_channel;
1022 conf->ht_bss_conf.bss_cap = req_bss_cap->bss_cap;
1023 conf->ht_bss_conf.bss_op_mode = req_bss_cap->bss_op_mode;
1024 for (i = 0; i < SUPP_MCS_SET_LEN; i++)
1025 conf->ht_conf.supp_mcs_set[i] =
Johannes Berg8318d782008-01-24 19:38:38 +01001026 sband->ht_info.supp_mcs_set[i] &
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +02001027 req_ht_cap->supp_mcs_set[i];
1028
1029 /* In STA mode, this gives us indication
1030 * to the AP's mode of operation */
1031 conf->ht_conf.ht_supported = 1;
1032 conf->ht_conf.ampdu_factor = req_ht_cap->ampdu_factor;
1033 conf->ht_conf.ampdu_density = req_ht_cap->ampdu_density;
1034 }
1035
1036 local->ops->conf_ht(local_to_hw(local), &local->hw.conf);
1037
1038 return 0;
1039}
1040
Johannes Berg471b3ef2007-12-28 14:32:58 +01001041void ieee80211_bss_info_change_notify(struct ieee80211_sub_if_data *sdata,
1042 u32 changed)
Daniel Draked9430a32007-07-27 15:43:24 +02001043{
Johannes Berg471b3ef2007-12-28 14:32:58 +01001044 struct ieee80211_local *local = sdata->local;
1045
1046 if (!changed)
1047 return;
1048
1049 if (local->ops->bss_info_changed)
1050 local->ops->bss_info_changed(local_to_hw(local),
1051 &sdata->vif,
1052 &sdata->bss_conf,
1053 changed);
Daniel Draked9430a32007-07-27 15:43:24 +02001054}
1055
1056void ieee80211_reset_erp_info(struct net_device *dev)
1057{
1058 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1059
Johannes Berg471b3ef2007-12-28 14:32:58 +01001060 sdata->bss_conf.use_cts_prot = 0;
1061 sdata->bss_conf.use_short_preamble = 0;
1062 ieee80211_bss_info_change_notify(sdata,
1063 BSS_CHANGED_ERP_CTS_PROT |
1064 BSS_CHANGED_ERP_PREAMBLE);
Daniel Draked9430a32007-07-27 15:43:24 +02001065}
1066
Jiri Bencf0706e82007-05-05 11:45:53 -07001067void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
1068 struct sk_buff *skb,
1069 struct ieee80211_tx_status *status)
1070{
1071 struct ieee80211_local *local = hw_to_local(hw);
1072 struct ieee80211_tx_status *saved;
1073 int tmp;
1074
1075 skb->dev = local->mdev;
1076 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
1077 if (unlikely(!saved)) {
1078 if (net_ratelimit())
1079 printk(KERN_WARNING "%s: Not enough memory, "
1080 "dropping tx status", skb->dev->name);
1081 /* should be dev_kfree_skb_irq, but due to this function being
1082 * named _irqsafe instead of just _irq we can't be sure that
1083 * people won't call it from non-irq contexts */
1084 dev_kfree_skb_any(skb);
1085 return;
1086 }
1087 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
1088 /* copy pointer to saved status into skb->cb for use by tasklet */
1089 memcpy(skb->cb, &saved, sizeof(saved));
1090
1091 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
1092 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
1093 &local->skb_queue : &local->skb_queue_unreliable, skb);
1094 tmp = skb_queue_len(&local->skb_queue) +
1095 skb_queue_len(&local->skb_queue_unreliable);
1096 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
1097 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
1098 memcpy(&saved, skb->cb, sizeof(saved));
1099 kfree(saved);
1100 dev_kfree_skb_irq(skb);
1101 tmp--;
1102 I802_DEBUG_INC(local->tx_status_drop);
1103 }
1104 tasklet_schedule(&local->tasklet);
1105}
1106EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
1107
1108static void ieee80211_tasklet_handler(unsigned long data)
1109{
1110 struct ieee80211_local *local = (struct ieee80211_local *) data;
1111 struct sk_buff *skb;
1112 struct ieee80211_rx_status rx_status;
1113 struct ieee80211_tx_status *tx_status;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001114 struct ieee80211_ra_tid *ra_tid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001115
1116 while ((skb = skb_dequeue(&local->skb_queue)) ||
1117 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
1118 switch (skb->pkt_type) {
1119 case IEEE80211_RX_MSG:
1120 /* status is in skb->cb */
1121 memcpy(&rx_status, skb->cb, sizeof(rx_status));
Johannes Berg51fb61e2007-12-19 01:31:27 +01001122 /* Clear skb->pkt_type in order to not confuse kernel
Jiri Bencf0706e82007-05-05 11:45:53 -07001123 * netstack. */
1124 skb->pkt_type = 0;
1125 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
1126 break;
1127 case IEEE80211_TX_STATUS_MSG:
1128 /* get pointer to saved status out of skb->cb */
1129 memcpy(&tx_status, skb->cb, sizeof(tx_status));
1130 skb->pkt_type = 0;
1131 ieee80211_tx_status(local_to_hw(local),
1132 skb, tx_status);
1133 kfree(tx_status);
1134 break;
Ron Rindjunskyeadc8d92008-01-28 14:07:17 +02001135 case IEEE80211_DELBA_MSG:
1136 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
1137 ieee80211_stop_tx_ba_cb(local_to_hw(local),
1138 ra_tid->ra, ra_tid->tid);
1139 dev_kfree_skb(skb);
1140 break;
1141 case IEEE80211_ADDBA_MSG:
1142 ra_tid = (struct ieee80211_ra_tid *) &skb->cb;
1143 ieee80211_start_tx_ba_cb(local_to_hw(local),
1144 ra_tid->ra, ra_tid->tid);
1145 dev_kfree_skb(skb);
1146 break ;
Jiri Bencf0706e82007-05-05 11:45:53 -07001147 default: /* should never get here! */
1148 printk(KERN_ERR "%s: Unknown message type (%d)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001149 wiphy_name(local->hw.wiphy), skb->pkt_type);
Jiri Bencf0706e82007-05-05 11:45:53 -07001150 dev_kfree_skb(skb);
1151 break;
1152 }
1153 }
1154}
1155
Jiri Bencf0706e82007-05-05 11:45:53 -07001156/* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
1157 * make a prepared TX frame (one that has been given to hw) to look like brand
1158 * new IEEE 802.11 frame that is ready to go through TX processing again.
1159 * Also, tx_packet_data in cb is restored from tx_control. */
1160static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
1161 struct ieee80211_key *key,
1162 struct sk_buff *skb,
1163 struct ieee80211_tx_control *control)
1164{
1165 int hdrlen, iv_len, mic_len;
1166 struct ieee80211_tx_packet_data *pkt_data;
1167
1168 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
Johannes Berg32bfd352007-12-19 01:31:26 +01001169 pkt_data->ifindex = vif_to_sdata(control->vif)->dev->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -04001170 pkt_data->flags = 0;
1171 if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
1172 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
1173 if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
1174 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
1175 if (control->flags & IEEE80211_TXCTL_REQUEUE)
1176 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg678f5f712007-12-19 01:31:23 +01001177 if (control->flags & IEEE80211_TXCTL_EAPOL_FRAME)
1178 pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
Jiri Bencf0706e82007-05-05 11:45:53 -07001179 pkt_data->queue = control->queue;
1180
1181 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1182
1183 if (!key)
1184 goto no_key;
1185
Johannes Berg8f20fc22007-08-28 17:01:54 -04001186 switch (key->conf.alg) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001187 case ALG_WEP:
1188 iv_len = WEP_IV_LEN;
1189 mic_len = WEP_ICV_LEN;
1190 break;
1191 case ALG_TKIP:
1192 iv_len = TKIP_IV_LEN;
1193 mic_len = TKIP_ICV_LEN;
1194 break;
1195 case ALG_CCMP:
1196 iv_len = CCMP_HDR_LEN;
1197 mic_len = CCMP_MIC_LEN;
1198 break;
1199 default:
1200 goto no_key;
1201 }
1202
Johannes Berg8f20fc22007-08-28 17:01:54 -04001203 if (skb->len >= mic_len &&
Johannes Berg11a843b2007-08-28 17:01:55 -04001204 !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Jiri Bencf0706e82007-05-05 11:45:53 -07001205 skb_trim(skb, skb->len - mic_len);
1206 if (skb->len >= iv_len && skb->len > hdrlen) {
1207 memmove(skb->data + iv_len, skb->data, hdrlen);
1208 skb_pull(skb, iv_len);
1209 }
1210
1211no_key:
1212 {
1213 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1214 u16 fc = le16_to_cpu(hdr->frame_control);
1215 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
1216 fc &= ~IEEE80211_STYPE_QOS_DATA;
1217 hdr->frame_control = cpu_to_le16(fc);
1218 memmove(skb->data + 2, skb->data, hdrlen - 2);
1219 skb_pull(skb, 2);
1220 }
1221 }
1222}
1223
Johannes Bergd46e1442008-02-20 23:59:33 +01001224static void ieee80211_handle_filtered_frame(struct ieee80211_local *local,
1225 struct sta_info *sta,
1226 struct sk_buff *skb,
1227 struct ieee80211_tx_status *status)
1228{
1229 sta->tx_filtered_count++;
1230
1231 /*
1232 * Clear the TX filter mask for this STA when sending the next
1233 * packet. If the STA went to power save mode, this will happen
1234 * happen when it wakes up for the next time.
1235 */
1236 sta->flags |= WLAN_STA_CLEAR_PS_FILT;
1237
1238 /*
1239 * This code races in the following way:
1240 *
1241 * (1) STA sends frame indicating it will go to sleep and does so
1242 * (2) hardware/firmware adds STA to filter list, passes frame up
1243 * (3) hardware/firmware processes TX fifo and suppresses a frame
1244 * (4) we get TX status before having processed the frame and
1245 * knowing that the STA has gone to sleep.
1246 *
1247 * This is actually quite unlikely even when both those events are
1248 * processed from interrupts coming in quickly after one another or
1249 * even at the same time because we queue both TX status events and
1250 * RX frames to be processed by a tasklet and process them in the
1251 * same order that they were received or TX status last. Hence, there
1252 * is no race as long as the frame RX is processed before the next TX
1253 * status, which drivers can ensure, see below.
1254 *
1255 * Note that this can only happen if the hardware or firmware can
1256 * actually add STAs to the filter list, if this is done by the
1257 * driver in response to set_tim() (which will only reduce the race
1258 * this whole filtering tries to solve, not completely solve it)
1259 * this situation cannot happen.
1260 *
1261 * To completely solve this race drivers need to make sure that they
1262 * (a) don't mix the irq-safe/not irq-safe TX status/RX processing
1263 * functions and
1264 * (b) always process RX events before TX status events if ordering
1265 * can be unknown, for example with different interrupt status
1266 * bits.
1267 */
1268 if (sta->flags & WLAN_STA_PS &&
1269 skb_queue_len(&sta->tx_filtered) < STA_MAX_TX_BUFFER) {
1270 ieee80211_remove_tx_extra(local, sta->key, skb,
1271 &status->control);
1272 skb_queue_tail(&sta->tx_filtered, skb);
1273 return;
1274 }
1275
1276 if (!(sta->flags & WLAN_STA_PS) &&
1277 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
1278 /* Software retry the packet once */
1279 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
1280 ieee80211_remove_tx_extra(local, sta->key, skb,
1281 &status->control);
1282 dev_queue_xmit(skb);
1283 return;
1284 }
1285
1286 if (net_ratelimit())
1287 printk(KERN_DEBUG "%s: dropped TX filtered frame, "
1288 "queue_len=%d PS=%d @%lu\n",
1289 wiphy_name(local->hw.wiphy),
1290 skb_queue_len(&sta->tx_filtered),
1291 !!(sta->flags & WLAN_STA_PS), jiffies);
1292 dev_kfree_skb(skb);
1293}
1294
Jiri Bencf0706e82007-05-05 11:45:53 -07001295void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
1296 struct ieee80211_tx_status *status)
1297{
1298 struct sk_buff *skb2;
1299 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1300 struct ieee80211_local *local = hw_to_local(hw);
1301 u16 frag, type;
Johannes Bergb306f452007-07-10 19:32:08 +02001302 struct ieee80211_tx_status_rtap_hdr *rthdr;
1303 struct ieee80211_sub_if_data *sdata;
Michael Wu3d30d942008-01-31 19:48:27 +01001304 struct net_device *prev_dev = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001305
1306 if (!status) {
1307 printk(KERN_ERR
1308 "%s: ieee80211_tx_status called with NULL status\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001309 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001310 dev_kfree_skb(skb);
1311 return;
1312 }
1313
1314 if (status->excessive_retries) {
1315 struct sta_info *sta;
1316 sta = sta_info_get(local, hdr->addr1);
1317 if (sta) {
1318 if (sta->flags & WLAN_STA_PS) {
Johannes Bergd46e1442008-02-20 23:59:33 +01001319 /*
1320 * The STA is in power save mode, so assume
Jiri Bencf0706e82007-05-05 11:45:53 -07001321 * that this TX packet failed because of that.
1322 */
1323 status->excessive_retries = 0;
1324 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
Johannes Bergd46e1442008-02-20 23:59:33 +01001325 ieee80211_handle_filtered_frame(local, sta,
1326 skb, status);
1327 sta_info_put(sta);
1328 return;
Jiri Bencf0706e82007-05-05 11:45:53 -07001329 }
1330 sta_info_put(sta);
1331 }
1332 }
1333
1334 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
1335 struct sta_info *sta;
1336 sta = sta_info_get(local, hdr->addr1);
1337 if (sta) {
Johannes Bergd46e1442008-02-20 23:59:33 +01001338 ieee80211_handle_filtered_frame(local, sta, skb,
1339 status);
Jiri Bencf0706e82007-05-05 11:45:53 -07001340 sta_info_put(sta);
1341 return;
1342 }
Mattias Nissler1abbe492007-12-20 13:50:07 +01001343 } else
1344 rate_control_tx_status(local->mdev, skb, status);
Jiri Bencf0706e82007-05-05 11:45:53 -07001345
1346 ieee80211_led_tx(local, 0);
1347
1348 /* SNMP counters
1349 * Fragments are passed to low-level drivers as separate skbs, so these
1350 * are actually fragments, not frames. Update frame counters only for
1351 * the first fragment of the frame. */
1352
1353 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
1354 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
1355
1356 if (status->flags & IEEE80211_TX_STATUS_ACK) {
1357 if (frag == 0) {
1358 local->dot11TransmittedFrameCount++;
1359 if (is_multicast_ether_addr(hdr->addr1))
1360 local->dot11MulticastTransmittedFrameCount++;
1361 if (status->retry_count > 0)
1362 local->dot11RetryCount++;
1363 if (status->retry_count > 1)
1364 local->dot11MultipleRetryCount++;
1365 }
1366
1367 /* This counter shall be incremented for an acknowledged MPDU
1368 * with an individual address in the address 1 field or an MPDU
1369 * with a multicast address in the address 1 field of type Data
1370 * or Management. */
1371 if (!is_multicast_ether_addr(hdr->addr1) ||
1372 type == IEEE80211_FTYPE_DATA ||
1373 type == IEEE80211_FTYPE_MGMT)
1374 local->dot11TransmittedFragmentCount++;
1375 } else {
1376 if (frag == 0)
1377 local->dot11FailedCount++;
1378 }
1379
Johannes Bergb306f452007-07-10 19:32:08 +02001380 /* this was a transmitted frame, but now we want to reuse it */
1381 skb_orphan(skb);
1382
Michael Wu3d30d942008-01-31 19:48:27 +01001383 /*
1384 * This is a bit racy but we can avoid a lot of work
1385 * with this test...
1386 */
1387 if (!local->monitors && !local->cooked_mntrs) {
Jiri Bencf0706e82007-05-05 11:45:53 -07001388 dev_kfree_skb(skb);
1389 return;
1390 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001391
Johannes Bergb306f452007-07-10 19:32:08 +02001392 /* send frame to monitor interfaces now */
1393
1394 if (skb_headroom(skb) < sizeof(*rthdr)) {
1395 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
1396 dev_kfree_skb(skb);
1397 return;
1398 }
1399
1400 rthdr = (struct ieee80211_tx_status_rtap_hdr*)
1401 skb_push(skb, sizeof(*rthdr));
1402
1403 memset(rthdr, 0, sizeof(*rthdr));
1404 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
1405 rthdr->hdr.it_present =
1406 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
1407 (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
1408
1409 if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
1410 !is_multicast_ether_addr(hdr->addr1))
1411 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
1412
1413 if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
1414 (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
1415 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
1416 else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
1417 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
1418
1419 rthdr->data_retries = status->retry_count;
1420
Michael Wu3d30d942008-01-31 19:48:27 +01001421 /* XXX: is this sufficient for BPF? */
1422 skb_set_mac_header(skb, 0);
1423 skb->ip_summed = CHECKSUM_UNNECESSARY;
1424 skb->pkt_type = PACKET_OTHERHOST;
1425 skb->protocol = htons(ETH_P_802_2);
1426 memset(skb->cb, 0, sizeof(skb->cb));
Johannes Bergb306f452007-07-10 19:32:08 +02001427
Michael Wu3d30d942008-01-31 19:48:27 +01001428 rcu_read_lock();
1429 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Berg51fb61e2007-12-19 01:31:27 +01001430 if (sdata->vif.type == IEEE80211_IF_TYPE_MNTR) {
Johannes Bergb306f452007-07-10 19:32:08 +02001431 if (!netif_running(sdata->dev))
1432 continue;
Michael Wu3d30d942008-01-31 19:48:27 +01001433
1434 if (prev_dev) {
Johannes Berg79010422007-09-18 17:29:21 -04001435 skb2 = skb_clone(skb, GFP_ATOMIC);
Michael Wu3d30d942008-01-31 19:48:27 +01001436 if (skb2) {
1437 skb2->dev = prev_dev;
1438 netif_rx(skb2);
1439 }
1440 }
1441
1442 prev_dev = sdata->dev;
Johannes Bergb306f452007-07-10 19:32:08 +02001443 }
1444 }
Michael Wu3d30d942008-01-31 19:48:27 +01001445 if (prev_dev) {
1446 skb->dev = prev_dev;
1447 netif_rx(skb);
1448 skb = NULL;
1449 }
Johannes Berg79010422007-09-18 17:29:21 -04001450 rcu_read_unlock();
Michael Wu3d30d942008-01-31 19:48:27 +01001451 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -07001452}
1453EXPORT_SYMBOL(ieee80211_tx_status);
1454
Jiri Bencf0706e82007-05-05 11:45:53 -07001455struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
1456 const struct ieee80211_ops *ops)
1457{
Jiri Bencf0706e82007-05-05 11:45:53 -07001458 struct ieee80211_local *local;
Jiri Bencf0706e82007-05-05 11:45:53 -07001459 int priv_size;
1460 struct wiphy *wiphy;
1461
1462 /* Ensure 32-byte alignment of our private data and hw private data.
1463 * We use the wiphy priv data for both our ieee80211_local and for
1464 * the driver's private data
1465 *
1466 * In memory it'll be like this:
1467 *
1468 * +-------------------------+
1469 * | struct wiphy |
1470 * +-------------------------+
1471 * | struct ieee80211_local |
1472 * +-------------------------+
1473 * | driver's private data |
1474 * +-------------------------+
1475 *
1476 */
1477 priv_size = ((sizeof(struct ieee80211_local) +
1478 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
1479 priv_data_len;
1480
1481 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
1482
1483 if (!wiphy)
1484 return NULL;
1485
1486 wiphy->privid = mac80211_wiphy_privid;
1487
1488 local = wiphy_priv(wiphy);
1489 local->hw.wiphy = wiphy;
1490
1491 local->hw.priv = (char *)local +
1492 ((sizeof(struct ieee80211_local) +
1493 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
1494
Johannes Berg4480f15c2007-07-10 19:32:10 +02001495 BUG_ON(!ops->tx);
Johannes Berg4150c572007-09-17 01:29:23 -04001496 BUG_ON(!ops->start);
1497 BUG_ON(!ops->stop);
Johannes Berg4480f15c2007-07-10 19:32:10 +02001498 BUG_ON(!ops->config);
1499 BUG_ON(!ops->add_interface);
Johannes Berg4150c572007-09-17 01:29:23 -04001500 BUG_ON(!ops->remove_interface);
1501 BUG_ON(!ops->configure_filter);
Jiri Bencf0706e82007-05-05 11:45:53 -07001502 local->ops = ops;
1503
Jiri Bencf0706e82007-05-05 11:45:53 -07001504 local->hw.queues = 1; /* default */
1505
Jiri Bencf0706e82007-05-05 11:45:53 -07001506 local->bridge_packets = 1;
1507
1508 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1509 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1510 local->short_retry_limit = 7;
1511 local->long_retry_limit = 4;
1512 local->hw.conf.radio_enabled = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07001513
Johannes Berg79010422007-09-18 17:29:21 -04001514 INIT_LIST_HEAD(&local->interfaces);
Jiri Bencf0706e82007-05-05 11:45:53 -07001515
1516 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
Jiri Bencf0706e82007-05-05 11:45:53 -07001517
1518 sta_info_init(local);
1519
Jiri Bencf0706e82007-05-05 11:45:53 -07001520 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1521 (unsigned long)local);
1522 tasklet_disable(&local->tx_pending_tasklet);
1523
1524 tasklet_init(&local->tasklet,
1525 ieee80211_tasklet_handler,
1526 (unsigned long) local);
1527 tasklet_disable(&local->tasklet);
1528
1529 skb_queue_head_init(&local->skb_queue);
1530 skb_queue_head_init(&local->skb_queue_unreliable);
1531
1532 return local_to_hw(local);
1533}
1534EXPORT_SYMBOL(ieee80211_alloc_hw);
1535
1536int ieee80211_register_hw(struct ieee80211_hw *hw)
1537{
1538 struct ieee80211_local *local = hw_to_local(hw);
1539 const char *name;
1540 int result;
Johannes Berg8318d782008-01-24 19:38:38 +01001541 enum ieee80211_band band;
Johannes Berg96d51052008-02-08 09:48:13 +01001542 struct net_device *mdev;
1543 struct ieee80211_sub_if_data *sdata;
Johannes Berg8318d782008-01-24 19:38:38 +01001544
1545 /*
1546 * generic code guarantees at least one band,
1547 * set this very early because much code assumes
1548 * that hw.conf.channel is assigned
1549 */
1550 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1551 struct ieee80211_supported_band *sband;
1552
1553 sband = local->hw.wiphy->bands[band];
1554 if (sband) {
1555 /* init channel we're on */
1556 local->hw.conf.channel =
1557 local->oper_channel =
1558 local->scan_channel = &sband->channels[0];
1559 break;
1560 }
1561 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001562
1563 result = wiphy_register(local->hw.wiphy);
1564 if (result < 0)
1565 return result;
1566
Johannes Berg96d51052008-02-08 09:48:13 +01001567 /* for now, mdev needs sub_if_data :/ */
1568 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
1569 "wmaster%d", ether_setup);
1570 if (!mdev)
1571 goto fail_mdev_alloc;
1572
1573 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
1574 mdev->ieee80211_ptr = &sdata->wdev;
1575 sdata->wdev.wiphy = local->hw.wiphy;
1576
1577 local->mdev = mdev;
1578
1579 ieee80211_rx_bss_list_init(mdev);
1580
1581 mdev->hard_start_xmit = ieee80211_master_start_xmit;
1582 mdev->open = ieee80211_master_open;
1583 mdev->stop = ieee80211_master_stop;
1584 mdev->type = ARPHRD_IEEE80211;
1585 mdev->header_ops = &ieee80211_header_ops;
1586 mdev->set_multicast_list = ieee80211_master_set_multicast_list;
1587
1588 sdata->vif.type = IEEE80211_IF_TYPE_AP;
1589 sdata->dev = mdev;
1590 sdata->local = local;
1591 sdata->u.ap.force_unicast_rateidx = -1;
1592 sdata->u.ap.max_ratectrl_rateidx = -1;
1593 ieee80211_if_sdata_init(sdata);
1594
1595 /* no RCU needed since we're still during init phase */
1596 list_add_tail(&sdata->list, &local->interfaces);
1597
Jiri Bencf0706e82007-05-05 11:45:53 -07001598 name = wiphy_dev(local->hw.wiphy)->driver->name;
1599 local->hw.workqueue = create_singlethread_workqueue(name);
1600 if (!local->hw.workqueue) {
1601 result = -ENOMEM;
1602 goto fail_workqueue;
1603 }
1604
Johannes Bergb306f452007-07-10 19:32:08 +02001605 /*
1606 * The hardware needs headroom for sending the frame,
1607 * and we need some headroom for passing the frame to monitor
1608 * interfaces, but never both at the same time.
1609 */
Jiri Benc33ccad32007-07-18 17:10:44 +02001610 local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1611 sizeof(struct ieee80211_tx_status_rtap_hdr));
Johannes Bergb306f452007-07-10 19:32:08 +02001612
Jiri Bence9f207f2007-05-05 11:46:38 -07001613 debugfs_hw_add(local);
1614
Jiri Bencf0706e82007-05-05 11:45:53 -07001615 local->hw.conf.beacon_int = 1000;
1616
1617 local->wstats_flags |= local->hw.max_rssi ?
1618 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1619 local->wstats_flags |= local->hw.max_signal ?
1620 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1621 local->wstats_flags |= local->hw.max_noise ?
1622 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1623 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1624 local->wstats_flags |= IW_QUAL_DBM;
1625
1626 result = sta_info_start(local);
1627 if (result < 0)
1628 goto fail_sta_info;
1629
1630 rtnl_lock();
1631 result = dev_alloc_name(local->mdev, local->mdev->name);
1632 if (result < 0)
1633 goto fail_dev;
1634
1635 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1636 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1637
1638 result = register_netdevice(local->mdev);
1639 if (result < 0)
1640 goto fail_dev;
1641
Jiri Bence9f207f2007-05-05 11:46:38 -07001642 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Johannes Berg5b2812e2007-09-26 14:27:23 +02001643 ieee80211_if_set_type(local->mdev, IEEE80211_IF_TYPE_AP);
Jiri Bence9f207f2007-05-05 11:46:38 -07001644
Johannes Berg830f9032007-10-28 14:51:05 +01001645 result = ieee80211_init_rate_ctrl_alg(local,
1646 hw->rate_control_algorithm);
Jiri Bencf0706e82007-05-05 11:45:53 -07001647 if (result < 0) {
1648 printk(KERN_DEBUG "%s: Failed to initialize rate control "
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001649 "algorithm\n", wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001650 goto fail_rate;
1651 }
1652
1653 result = ieee80211_wep_init(local);
1654
1655 if (result < 0) {
1656 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001657 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001658 goto fail_wep;
1659 }
1660
1661 ieee80211_install_qdisc(local->mdev);
1662
1663 /* add one default STA interface */
1664 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
1665 IEEE80211_IF_TYPE_STA);
1666 if (result)
1667 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001668 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001669
1670 local->reg_state = IEEE80211_DEV_REGISTERED;
1671 rtnl_unlock();
1672
1673 ieee80211_led_init(local);
1674
1675 return 0;
1676
1677fail_wep:
1678 rate_control_deinitialize(local);
1679fail_rate:
Jiri Bence9f207f2007-05-05 11:46:38 -07001680 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001681 unregister_netdevice(local->mdev);
1682fail_dev:
1683 rtnl_unlock();
1684 sta_info_stop(local);
1685fail_sta_info:
Jiri Bence9f207f2007-05-05 11:46:38 -07001686 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001687 destroy_workqueue(local->hw.workqueue);
1688fail_workqueue:
Johannes Berg96d51052008-02-08 09:48:13 +01001689 ieee80211_if_free(local->mdev);
1690 local->mdev = NULL;
1691fail_mdev_alloc:
Jiri Bencf0706e82007-05-05 11:45:53 -07001692 wiphy_unregister(local->hw.wiphy);
1693 return result;
1694}
1695EXPORT_SYMBOL(ieee80211_register_hw);
1696
Jiri Bencf0706e82007-05-05 11:45:53 -07001697void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1698{
1699 struct ieee80211_local *local = hw_to_local(hw);
1700 struct ieee80211_sub_if_data *sdata, *tmp;
Jiri Bencf0706e82007-05-05 11:45:53 -07001701
1702 tasklet_kill(&local->tx_pending_tasklet);
1703 tasklet_kill(&local->tasklet);
1704
1705 rtnl_lock();
1706
1707 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1708
1709 local->reg_state = IEEE80211_DEV_UNREGISTERED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001710
Johannes Berg79010422007-09-18 17:29:21 -04001711 /*
1712 * At this point, interface list manipulations are fine
1713 * because the driver cannot be handing us frames any
1714 * more and the tasklet is killed.
1715 */
Johannes Berg5b2812e2007-09-26 14:27:23 +02001716
1717 /*
1718 * First, we remove all non-master interfaces. Do this because they
1719 * may have bss pointer dependency on the master, and when we free
1720 * the master these would be freed as well, breaking our list
1721 * iteration completely.
1722 */
1723 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
1724 if (sdata->dev == local->mdev)
1725 continue;
1726 list_del(&sdata->list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001727 __ieee80211_if_del(local, sdata);
Johannes Berg5b2812e2007-09-26 14:27:23 +02001728 }
1729
1730 /* then, finally, remove the master interface */
1731 __ieee80211_if_del(local, IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001732
1733 rtnl_unlock();
1734
Jiri Bencf0706e82007-05-05 11:45:53 -07001735 ieee80211_rx_bss_list_deinit(local->mdev);
1736 ieee80211_clear_tx_pending(local);
1737 sta_info_stop(local);
1738 rate_control_deinitialize(local);
Jiri Bence9f207f2007-05-05 11:46:38 -07001739 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001740
Jiri Bencf0706e82007-05-05 11:45:53 -07001741 if (skb_queue_len(&local->skb_queue)
1742 || skb_queue_len(&local->skb_queue_unreliable))
1743 printk(KERN_WARNING "%s: skb_queue not empty\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001744 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001745 skb_queue_purge(&local->skb_queue);
1746 skb_queue_purge(&local->skb_queue_unreliable);
1747
1748 destroy_workqueue(local->hw.workqueue);
1749 wiphy_unregister(local->hw.wiphy);
1750 ieee80211_wep_free(local);
1751 ieee80211_led_exit(local);
Johannes Berg96d51052008-02-08 09:48:13 +01001752 ieee80211_if_free(local->mdev);
1753 local->mdev = NULL;
Jiri Bencf0706e82007-05-05 11:45:53 -07001754}
1755EXPORT_SYMBOL(ieee80211_unregister_hw);
1756
1757void ieee80211_free_hw(struct ieee80211_hw *hw)
1758{
1759 struct ieee80211_local *local = hw_to_local(hw);
1760
Jiri Bencf0706e82007-05-05 11:45:53 -07001761 wiphy_free(local->hw.wiphy);
1762}
1763EXPORT_SYMBOL(ieee80211_free_hw);
1764
Jiri Bencf0706e82007-05-05 11:45:53 -07001765static int __init ieee80211_init(void)
1766{
1767 struct sk_buff *skb;
1768 int ret;
1769
1770 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1771
Johannes Berg4b475892008-01-02 15:17:03 +01001772 ret = rc80211_simple_init();
Johannes Bergac71c692007-10-28 14:17:44 +01001773 if (ret)
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001774 goto out;
Mattias Nisslerad018372007-12-19 01:25:57 +01001775
Johannes Berg4b475892008-01-02 15:17:03 +01001776 ret = rc80211_pid_init();
Mattias Nisslerad018372007-12-19 01:25:57 +01001777 if (ret)
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001778 goto out_cleanup_simple;
Johannes Bergac71c692007-10-28 14:17:44 +01001779
Jiri Bencf0706e82007-05-05 11:45:53 -07001780 ret = ieee80211_wme_register();
1781 if (ret) {
1782 printk(KERN_DEBUG "ieee80211_init: failed to "
1783 "initialize WME (err=%d)\n", ret);
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001784 goto out_cleanup_pid;
Jiri Bencf0706e82007-05-05 11:45:53 -07001785 }
1786
Jiri Bence9f207f2007-05-05 11:46:38 -07001787 ieee80211_debugfs_netdev_init();
1788
Jiri Bencf0706e82007-05-05 11:45:53 -07001789 return 0;
Mattias Nisslerad018372007-12-19 01:25:57 +01001790
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001791 out_cleanup_pid:
Johannes Berg4b475892008-01-02 15:17:03 +01001792 rc80211_pid_exit();
Johannes Berg3eadf5f2008-01-31 19:33:53 +01001793 out_cleanup_simple:
1794 rc80211_simple_exit();
1795 out:
Mattias Nisslerad018372007-12-19 01:25:57 +01001796 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07001797}
1798
Jiri Bencf0706e82007-05-05 11:45:53 -07001799static void __exit ieee80211_exit(void)
1800{
Johannes Berg4b475892008-01-02 15:17:03 +01001801 rc80211_simple_exit();
1802 rc80211_pid_exit();
Johannes Bergac71c692007-10-28 14:17:44 +01001803
Jiri Bencf0706e82007-05-05 11:45:53 -07001804 ieee80211_wme_unregister();
Jiri Bence9f207f2007-05-05 11:46:38 -07001805 ieee80211_debugfs_netdev_exit();
Jiri Bencf0706e82007-05-05 11:45:53 -07001806}
1807
1808
Johannes Bergca9938f2007-09-11 12:50:32 +02001809subsys_initcall(ieee80211_init);
Jiri Bencf0706e82007-05-05 11:45:53 -07001810module_exit(ieee80211_exit);
1811
1812MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1813MODULE_LICENSE("GPL");