blob: 9c14e3d303c549229eccc6d922350e87670ce9fb [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)
70 new_flags |= FIF_CONTROL |
71 FIF_OTHER_BSS |
72 FIF_BCN_PRBRESP_PROMISC;
73
74 changed_flags = local->filter_flags ^ new_flags;
75
76 /* be a bit nasty */
77 new_flags |= (1<<31);
78
79 local->ops->configure_filter(local_to_hw(local),
80 changed_flags, &new_flags,
81 local->mdev->mc_count,
82 local->mdev->mc_list);
83
84 WARN_ON(new_flags & (1<<31));
85
86 local->filter_flags = new_flags & ~(1<<31);
87}
88
Johannes Bergb2c258f2007-07-27 15:43:23 +020089/* master interface */
Jiri Bencf0706e82007-05-05 11:45:53 -070090
91static int ieee80211_master_open(struct net_device *dev)
92{
93 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
94 struct ieee80211_sub_if_data *sdata;
95 int res = -EOPNOTSUPP;
96
Johannes Berg79010422007-09-18 17:29:21 -040097 /* we hold the RTNL here so can safely walk the list */
98 list_for_each_entry(sdata, &local->interfaces, list) {
Jiri Bencf0706e82007-05-05 11:45:53 -070099 if (sdata->dev != dev && netif_running(sdata->dev)) {
100 res = 0;
101 break;
102 }
103 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700104 return res;
105}
106
107static int ieee80211_master_stop(struct net_device *dev)
108{
109 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
110 struct ieee80211_sub_if_data *sdata;
111
Johannes Berg79010422007-09-18 17:29:21 -0400112 /* we hold the RTNL here so can safely walk the list */
113 list_for_each_entry(sdata, &local->interfaces, list)
Jiri Bencf0706e82007-05-05 11:45:53 -0700114 if (sdata->dev != dev && netif_running(sdata->dev))
115 dev_close(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700116
117 return 0;
118}
119
Johannes Berg4150c572007-09-17 01:29:23 -0400120static void ieee80211_master_set_multicast_list(struct net_device *dev)
121{
122 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
123
124 ieee80211_configure_filter(local);
125}
126
Johannes Bergb2c258f2007-07-27 15:43:23 +0200127/* regular interfaces */
128
129static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
130{
131 /* FIX: what would be proper limits for MTU?
132 * This interface uses 802.3 frames. */
133 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
134 printk(KERN_WARNING "%s: invalid MTU %d\n",
135 dev->name, new_mtu);
136 return -EINVAL;
137 }
138
139#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
140 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
141#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
142 dev->mtu = new_mtu;
143 return 0;
144}
145
146static inline int identical_mac_addr_allowed(int type1, int type2)
147{
148 return (type1 == IEEE80211_IF_TYPE_MNTR ||
149 type2 == IEEE80211_IF_TYPE_MNTR ||
150 (type1 == IEEE80211_IF_TYPE_AP &&
151 type2 == IEEE80211_IF_TYPE_WDS) ||
152 (type1 == IEEE80211_IF_TYPE_WDS &&
153 (type2 == IEEE80211_IF_TYPE_WDS ||
154 type2 == IEEE80211_IF_TYPE_AP)) ||
155 (type1 == IEEE80211_IF_TYPE_AP &&
156 type2 == IEEE80211_IF_TYPE_VLAN) ||
157 (type1 == IEEE80211_IF_TYPE_VLAN &&
158 (type2 == IEEE80211_IF_TYPE_AP ||
159 type2 == IEEE80211_IF_TYPE_VLAN)));
160}
161
Johannes Bergb2c258f2007-07-27 15:43:23 +0200162static int ieee80211_open(struct net_device *dev)
163{
164 struct ieee80211_sub_if_data *sdata, *nsdata;
165 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
166 struct ieee80211_if_init_conf conf;
167 int res;
168
169 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400170
Johannes Berg79010422007-09-18 17:29:21 -0400171 /* we hold the RTNL here so can safely walk the list */
172 list_for_each_entry(nsdata, &local->interfaces, list) {
Johannes Bergb2c258f2007-07-27 15:43:23 +0200173 struct net_device *ndev = nsdata->dev;
174
175 if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400176 compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0) {
177 /*
178 * check whether it may have the same address
179 */
180 if (!identical_mac_addr_allowed(sdata->type,
Johannes Berg79010422007-09-18 17:29:21 -0400181 nsdata->type))
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400182 return -ENOTUNIQ;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400183
184 /*
185 * can only add VLANs to enabled APs
186 */
187 if (sdata->type == IEEE80211_IF_TYPE_VLAN &&
188 nsdata->type == IEEE80211_IF_TYPE_AP &&
189 netif_running(nsdata->dev))
190 sdata->u.vlan.ap = nsdata;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200191 }
192 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200193
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400194 switch (sdata->type) {
195 case IEEE80211_IF_TYPE_WDS:
196 if (is_zero_ether_addr(sdata->u.wds.remote_addr))
197 return -ENOLINK;
198 break;
199 case IEEE80211_IF_TYPE_VLAN:
200 if (!sdata->u.vlan.ap)
201 return -ENOLINK;
202 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +0200203 case IEEE80211_IF_TYPE_AP:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +0200204 case IEEE80211_IF_TYPE_STA:
205 case IEEE80211_IF_TYPE_MNTR:
206 case IEEE80211_IF_TYPE_IBSS:
207 /* no special treatment */
208 break;
Johannes Berga2897552007-09-28 14:01:25 +0200209 case IEEE80211_IF_TYPE_INVALID:
210 /* cannot happen */
211 WARN_ON(1);
212 break;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400213 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200214
Johannes Bergb2c258f2007-07-27 15:43:23 +0200215 if (local->open_count == 0) {
216 res = 0;
Johannes Berg4150c572007-09-17 01:29:23 -0400217 if (local->ops->start)
218 res = local->ops->start(local_to_hw(local));
219 if (res)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200220 return res;
Michael Wu8b393f12007-11-28 01:57:08 -0500221 ieee80211_hw_config(local);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200222 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200223
Johannes Berg4150c572007-09-17 01:29:23 -0400224 switch (sdata->type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400225 case IEEE80211_IF_TYPE_VLAN:
226 list_add(&sdata->u.vlan.list, &sdata->u.vlan.ap->u.ap.vlans);
227 /* no need to tell driver */
228 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400229 case IEEE80211_IF_TYPE_MNTR:
230 /* must be before the call to ieee80211_configure_filter */
Johannes Bergb2c258f2007-07-27 15:43:23 +0200231 local->monitors++;
Johannes Berg4150c572007-09-17 01:29:23 -0400232 if (local->monitors == 1) {
233 netif_tx_lock_bh(local->mdev);
234 ieee80211_configure_filter(local);
235 netif_tx_unlock_bh(local->mdev);
236
237 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
Johannes Berg4150c572007-09-17 01:29:23 -0400238 }
239 break;
240 case IEEE80211_IF_TYPE_STA:
241 case IEEE80211_IF_TYPE_IBSS:
242 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
243 /* fall through */
244 default:
245 conf.if_id = dev->ifindex;
246 conf.type = sdata->type;
247 conf.mac_addr = dev->dev_addr;
248 res = local->ops->add_interface(local_to_hw(local), &conf);
249 if (res && !local->open_count && local->ops->stop)
250 local->ops->stop(local_to_hw(local));
251 if (res)
252 return res;
253
Johannes Bergb2c258f2007-07-27 15:43:23 +0200254 ieee80211_if_config(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200255 ieee80211_reset_erp_info(dev);
Johannes Berg11a843b2007-08-28 17:01:55 -0400256 ieee80211_enable_keys(sdata);
Johannes Berg4150c572007-09-17 01:29:23 -0400257
258 if (sdata->type == IEEE80211_IF_TYPE_STA &&
Johannes Bergddd3d2b2007-09-26 17:53:20 +0200259 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
Johannes Berg4150c572007-09-17 01:29:23 -0400260 netif_carrier_off(dev);
261 else
262 netif_carrier_on(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200263 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200264
Johannes Berg4150c572007-09-17 01:29:23 -0400265 if (local->open_count == 0) {
266 res = dev_open(local->mdev);
267 WARN_ON(res);
Johannes Berg4150c572007-09-17 01:29:23 -0400268 tasklet_enable(&local->tx_pending_tasklet);
269 tasklet_enable(&local->tasklet);
270 }
271
Johannes Bergc1428b32007-11-16 02:54:53 +0100272 /*
273 * set_multicast_list will be invoked by the networking core
274 * which will check whether any increments here were done in
275 * error and sync them down to the hardware as filter flags.
276 */
277 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
278 atomic_inc(&local->iff_allmultis);
279
280 if (sdata->flags & IEEE80211_SDATA_PROMISC)
281 atomic_inc(&local->iff_promiscs);
282
Johannes Berg4150c572007-09-17 01:29:23 -0400283 local->open_count++;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200284
285 netif_start_queue(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400286
Johannes Bergb2c258f2007-07-27 15:43:23 +0200287 return 0;
288}
289
Johannes Berg4150c572007-09-17 01:29:23 -0400290static int ieee80211_stop(struct net_device *dev)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200291{
Johannes Berg4150c572007-09-17 01:29:23 -0400292 struct ieee80211_sub_if_data *sdata;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200293 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg4150c572007-09-17 01:29:23 -0400294 struct ieee80211_if_init_conf conf;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200295
Johannes Berg4150c572007-09-17 01:29:23 -0400296 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
297
298 netif_stop_queue(dev);
299
Johannes Bergc1428b32007-11-16 02:54:53 +0100300 /*
301 * Don't count this interface for promisc/allmulti while it
302 * is down. dev_mc_unsync() will invoke set_multicast_list
303 * on the master interface which will sync these down to the
304 * hardware as filter flags.
305 */
306 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
307 atomic_dec(&local->iff_allmultis);
308
309 if (sdata->flags & IEEE80211_SDATA_PROMISC)
310 atomic_dec(&local->iff_promiscs);
311
Johannes Berg4150c572007-09-17 01:29:23 -0400312 dev_mc_unsync(local->mdev, dev);
313
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400314 /* down all dependent devices, that is VLANs */
315 if (sdata->type == IEEE80211_IF_TYPE_AP) {
316 struct ieee80211_sub_if_data *vlan, *tmp;
317
318 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
319 u.vlan.list)
320 dev_close(vlan->dev);
321 WARN_ON(!list_empty(&sdata->u.ap.vlans));
322 }
323
Johannes Berg4150c572007-09-17 01:29:23 -0400324 local->open_count--;
325
Johannes Bergb2c258f2007-07-27 15:43:23 +0200326 switch (sdata->type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400327 case IEEE80211_IF_TYPE_VLAN:
328 list_del(&sdata->u.vlan.list);
329 sdata->u.vlan.ap = NULL;
330 /* no need to tell driver */
331 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400332 case IEEE80211_IF_TYPE_MNTR:
333 local->monitors--;
334 if (local->monitors == 0) {
335 netif_tx_lock_bh(local->mdev);
336 ieee80211_configure_filter(local);
337 netif_tx_unlock_bh(local->mdev);
338
Michael Wu8b393f12007-11-28 01:57:08 -0500339 local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
Johannes Berg4150c572007-09-17 01:29:23 -0400340 }
341 break;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200342 case IEEE80211_IF_TYPE_STA:
343 case IEEE80211_IF_TYPE_IBSS:
344 sdata->u.sta.state = IEEE80211_DISABLED;
345 del_timer_sync(&sdata->u.sta.timer);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400346 /*
Johannes Berg79010422007-09-18 17:29:21 -0400347 * When we get here, the interface is marked down.
348 * Call synchronize_rcu() to wait for the RX path
349 * should it be using the interface and enqueuing
350 * frames at this very time on another CPU.
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400351 */
Johannes Berg79010422007-09-18 17:29:21 -0400352 synchronize_rcu();
Johannes Bergb2c258f2007-07-27 15:43:23 +0200353 skb_queue_purge(&sdata->u.sta.skb_queue);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400354
Zhu Yiece8edd2007-11-22 10:53:21 +0800355 if (local->scan_dev == sdata->dev) {
356 if (!local->ops->hw_scan) {
357 local->sta_sw_scanning = 0;
358 cancel_delayed_work(&local->scan_work);
359 } else
360 local->sta_hw_scanning = 0;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200361 }
Zhu Yiece8edd2007-11-22 10:53:21 +0800362
Johannes Bergb2c258f2007-07-27 15:43:23 +0200363 flush_workqueue(local->hw.workqueue);
Zhu Yia10605e2007-11-22 11:10:22 +0800364
365 sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
366 kfree(sdata->u.sta.extra_ie);
367 sdata->u.sta.extra_ie = NULL;
368 sdata->u.sta.extra_ie_len = 0;
Johannes Berg4150c572007-09-17 01:29:23 -0400369 /* fall through */
370 default:
Johannes Bergb2c258f2007-07-27 15:43:23 +0200371 conf.if_id = dev->ifindex;
372 conf.type = sdata->type;
373 conf.mac_addr = dev->dev_addr;
Johannes Berg4150c572007-09-17 01:29:23 -0400374 /* disable all keys for as long as this netdev is down */
375 ieee80211_disable_keys(sdata);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200376 local->ops->remove_interface(local_to_hw(local), &conf);
377 }
378
Johannes Berg4150c572007-09-17 01:29:23 -0400379 if (local->open_count == 0) {
380 if (netif_running(local->mdev))
381 dev_close(local->mdev);
382
Johannes Berg4150c572007-09-17 01:29:23 -0400383 if (local->ops->stop)
384 local->ops->stop(local_to_hw(local));
385
386 tasklet_disable(&local->tx_pending_tasklet);
387 tasklet_disable(&local->tasklet);
388 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200389
390 return 0;
391}
392
Johannes Bergb2c258f2007-07-27 15:43:23 +0200393static void ieee80211_set_multicast_list(struct net_device *dev)
394{
395 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
396 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400397 int allmulti, promisc, sdata_allmulti, sdata_promisc;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200398
Johannes Berg4150c572007-09-17 01:29:23 -0400399 allmulti = !!(dev->flags & IFF_ALLMULTI);
400 promisc = !!(dev->flags & IFF_PROMISC);
Johannes Bergb52f2192007-11-16 01:49:11 +0100401 sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
402 sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
Johannes Berg4150c572007-09-17 01:29:23 -0400403
404 if (allmulti != sdata_allmulti) {
405 if (dev->flags & IFF_ALLMULTI)
Johannes Berg53918992007-09-26 15:19:47 +0200406 atomic_inc(&local->iff_allmultis);
Johannes Berg4150c572007-09-17 01:29:23 -0400407 else
Johannes Berg53918992007-09-26 15:19:47 +0200408 atomic_dec(&local->iff_allmultis);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400409 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200410 }
Johannes Berg4150c572007-09-17 01:29:23 -0400411
412 if (promisc != sdata_promisc) {
413 if (dev->flags & IFF_PROMISC)
Johannes Berg53918992007-09-26 15:19:47 +0200414 atomic_inc(&local->iff_promiscs);
Johannes Berg4150c572007-09-17 01:29:23 -0400415 else
Johannes Berg53918992007-09-26 15:19:47 +0200416 atomic_dec(&local->iff_promiscs);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400417 sdata->flags ^= IEEE80211_SDATA_PROMISC;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200418 }
Johannes Berg4150c572007-09-17 01:29:23 -0400419
420 dev_mc_sync(local->mdev, dev);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200421}
422
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700423static const struct header_ops ieee80211_header_ops = {
424 .create = eth_header,
425 .parse = header_parse_80211,
426 .rebuild = eth_rebuild_header,
427 .cache = eth_header_cache,
428 .cache_update = eth_header_cache_update,
429};
430
Johannes Bergf9d540e2007-09-28 14:02:09 +0200431/* Must not be called for mdev */
Johannes Bergb2c258f2007-07-27 15:43:23 +0200432void ieee80211_if_setup(struct net_device *dev)
433{
434 ether_setup(dev);
435 dev->hard_start_xmit = ieee80211_subif_start_xmit;
436 dev->wireless_handlers = &ieee80211_iw_handler_def;
437 dev->set_multicast_list = ieee80211_set_multicast_list;
438 dev->change_mtu = ieee80211_change_mtu;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200439 dev->open = ieee80211_open;
440 dev->stop = ieee80211_stop;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200441 dev->destructor = ieee80211_if_free;
442}
443
444/* WDS specialties */
445
446int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
447{
448 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
449 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
450 struct sta_info *sta;
Joe Perches0795af52007-10-03 17:59:30 -0700451 DECLARE_MAC_BUF(mac);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200452
453 if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
454 return 0;
455
456 /* Create STA entry for the new peer */
457 sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
458 if (!sta)
459 return -ENOMEM;
460 sta_info_put(sta);
461
462 /* Remove STA entry for the old peer */
463 sta = sta_info_get(local, sdata->u.wds.remote_addr);
464 if (sta) {
Michael Wube8755e2007-07-27 15:43:23 +0200465 sta_info_free(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200466 sta_info_put(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200467 } else {
468 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
Joe Perches0795af52007-10-03 17:59:30 -0700469 "peer %s\n",
470 dev->name, print_mac(mac, sdata->u.wds.remote_addr));
Johannes Bergb2c258f2007-07-27 15:43:23 +0200471 }
472
473 /* Update WDS link data */
474 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
475
476 return 0;
477}
478
479/* everything else */
480
Johannes Bergb2c258f2007-07-27 15:43:23 +0200481static int __ieee80211_if_config(struct net_device *dev,
482 struct sk_buff *beacon,
483 struct ieee80211_tx_control *control)
484{
485 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
486 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
487 struct ieee80211_if_conf conf;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200488
489 if (!local->ops->config_interface || !netif_running(dev))
490 return 0;
491
492 memset(&conf, 0, sizeof(conf));
493 conf.type = sdata->type;
494 if (sdata->type == IEEE80211_IF_TYPE_STA ||
495 sdata->type == IEEE80211_IF_TYPE_IBSS) {
Johannes Berg4150c572007-09-17 01:29:23 -0400496 conf.bssid = sdata->u.sta.bssid;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200497 conf.ssid = sdata->u.sta.ssid;
498 conf.ssid_len = sdata->u.sta.ssid_len;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200499 } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
500 conf.ssid = sdata->u.ap.ssid;
501 conf.ssid_len = sdata->u.ap.ssid_len;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200502 conf.beacon = beacon;
503 conf.beacon_control = control;
504 }
505 return local->ops->config_interface(local_to_hw(local),
506 dev->ifindex, &conf);
507}
508
509int ieee80211_if_config(struct net_device *dev)
510{
511 return __ieee80211_if_config(dev, NULL, NULL);
512}
513
514int ieee80211_if_config_beacon(struct net_device *dev)
515{
516 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
517 struct ieee80211_tx_control control;
518 struct sk_buff *skb;
519
520 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
521 return 0;
522 skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
523 if (!skb)
524 return -ENOMEM;
525 return __ieee80211_if_config(dev, skb, &control);
526}
527
528int ieee80211_hw_config(struct ieee80211_local *local)
529{
530 struct ieee80211_hw_mode *mode;
531 struct ieee80211_channel *chan;
532 int ret = 0;
533
Zhu Yiece8edd2007-11-22 10:53:21 +0800534 if (local->sta_sw_scanning) {
Johannes Bergb2c258f2007-07-27 15:43:23 +0200535 chan = local->scan_channel;
536 mode = local->scan_hw_mode;
537 } else {
538 chan = local->oper_channel;
539 mode = local->oper_hw_mode;
540 }
541
542 local->hw.conf.channel = chan->chan;
543 local->hw.conf.channel_val = chan->val;
Michael Buesch61609bc2007-09-20 22:06:39 +0200544 if (!local->hw.conf.power_level) {
545 local->hw.conf.power_level = chan->power_level;
546 } else {
547 local->hw.conf.power_level = min(chan->power_level,
548 local->hw.conf.power_level);
549 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200550 local->hw.conf.freq = chan->freq;
551 local->hw.conf.phymode = mode->mode;
552 local->hw.conf.antenna_max = chan->antenna_max;
553 local->hw.conf.chan = chan;
554 local->hw.conf.mode = mode;
555
556#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
557 printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
558 "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
559 local->hw.conf.phymode);
560#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
561
Michael Bueschf7c4dae2007-09-24 18:41:49 +0200562 if (local->open_count)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200563 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
564
565 return ret;
566}
567
Ron Rindjunskyd3c990f2007-11-26 16:14:34 +0200568/**
569 * ieee80211_hw_config_ht should be used only after legacy configuration
570 * has been determined, as ht configuration depends upon the hardware's
571 * HT abilities for a _specific_ band.
572 */
573int ieee80211_hw_config_ht(struct ieee80211_local *local, int enable_ht,
574 struct ieee80211_ht_info *req_ht_cap,
575 struct ieee80211_ht_bss_info *req_bss_cap)
576{
577 struct ieee80211_conf *conf = &local->hw.conf;
578 struct ieee80211_hw_mode *mode = conf->mode;
579 int i;
580
581 /* HT is not supported */
582 if (!mode->ht_info.ht_supported) {
583 conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
584 return -EOPNOTSUPP;
585 }
586
587 /* disable HT */
588 if (!enable_ht) {
589 conf->flags &= ~IEEE80211_CONF_SUPPORT_HT_MODE;
590 } else {
591 conf->flags |= IEEE80211_CONF_SUPPORT_HT_MODE;
592 conf->ht_conf.cap = req_ht_cap->cap & mode->ht_info.cap;
593 conf->ht_conf.cap &= ~(IEEE80211_HT_CAP_MIMO_PS);
594 conf->ht_conf.cap |=
595 mode->ht_info.cap & IEEE80211_HT_CAP_MIMO_PS;
596 conf->ht_bss_conf.primary_channel =
597 req_bss_cap->primary_channel;
598 conf->ht_bss_conf.bss_cap = req_bss_cap->bss_cap;
599 conf->ht_bss_conf.bss_op_mode = req_bss_cap->bss_op_mode;
600 for (i = 0; i < SUPP_MCS_SET_LEN; i++)
601 conf->ht_conf.supp_mcs_set[i] =
602 mode->ht_info.supp_mcs_set[i] &
603 req_ht_cap->supp_mcs_set[i];
604
605 /* In STA mode, this gives us indication
606 * to the AP's mode of operation */
607 conf->ht_conf.ht_supported = 1;
608 conf->ht_conf.ampdu_factor = req_ht_cap->ampdu_factor;
609 conf->ht_conf.ampdu_density = req_ht_cap->ampdu_density;
610 }
611
612 local->ops->conf_ht(local_to_hw(local), &local->hw.conf);
613
614 return 0;
615}
616
Daniel Draked9430a32007-07-27 15:43:24 +0200617void ieee80211_erp_info_change_notify(struct net_device *dev, u8 changes)
618{
619 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
620 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
621 if (local->ops->erp_ie_changed)
622 local->ops->erp_ie_changed(local_to_hw(local), changes,
Jiri Slaby13262ff2007-08-28 17:01:54 -0400623 !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION),
624 !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE));
Daniel Draked9430a32007-07-27 15:43:24 +0200625}
626
627void ieee80211_reset_erp_info(struct net_device *dev)
628{
629 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
630
Jiri Slaby13262ff2007-08-28 17:01:54 -0400631 sdata->flags &= ~(IEEE80211_SDATA_USE_PROTECTION |
632 IEEE80211_SDATA_SHORT_PREAMBLE);
Daniel Draked9430a32007-07-27 15:43:24 +0200633 ieee80211_erp_info_change_notify(dev,
634 IEEE80211_ERP_CHANGE_PROTECTION |
635 IEEE80211_ERP_CHANGE_PREAMBLE);
636}
637
Jiri Bencf0706e82007-05-05 11:45:53 -0700638void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
639 struct sk_buff *skb,
640 struct ieee80211_tx_status *status)
641{
642 struct ieee80211_local *local = hw_to_local(hw);
643 struct ieee80211_tx_status *saved;
644 int tmp;
645
646 skb->dev = local->mdev;
647 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
648 if (unlikely(!saved)) {
649 if (net_ratelimit())
650 printk(KERN_WARNING "%s: Not enough memory, "
651 "dropping tx status", skb->dev->name);
652 /* should be dev_kfree_skb_irq, but due to this function being
653 * named _irqsafe instead of just _irq we can't be sure that
654 * people won't call it from non-irq contexts */
655 dev_kfree_skb_any(skb);
656 return;
657 }
658 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
659 /* copy pointer to saved status into skb->cb for use by tasklet */
660 memcpy(skb->cb, &saved, sizeof(saved));
661
662 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
663 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
664 &local->skb_queue : &local->skb_queue_unreliable, skb);
665 tmp = skb_queue_len(&local->skb_queue) +
666 skb_queue_len(&local->skb_queue_unreliable);
667 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
668 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
669 memcpy(&saved, skb->cb, sizeof(saved));
670 kfree(saved);
671 dev_kfree_skb_irq(skb);
672 tmp--;
673 I802_DEBUG_INC(local->tx_status_drop);
674 }
675 tasklet_schedule(&local->tasklet);
676}
677EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
678
679static void ieee80211_tasklet_handler(unsigned long data)
680{
681 struct ieee80211_local *local = (struct ieee80211_local *) data;
682 struct sk_buff *skb;
683 struct ieee80211_rx_status rx_status;
684 struct ieee80211_tx_status *tx_status;
685
686 while ((skb = skb_dequeue(&local->skb_queue)) ||
687 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
688 switch (skb->pkt_type) {
689 case IEEE80211_RX_MSG:
690 /* status is in skb->cb */
691 memcpy(&rx_status, skb->cb, sizeof(rx_status));
692 /* Clear skb->type in order to not confuse kernel
693 * netstack. */
694 skb->pkt_type = 0;
695 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
696 break;
697 case IEEE80211_TX_STATUS_MSG:
698 /* get pointer to saved status out of skb->cb */
699 memcpy(&tx_status, skb->cb, sizeof(tx_status));
700 skb->pkt_type = 0;
701 ieee80211_tx_status(local_to_hw(local),
702 skb, tx_status);
703 kfree(tx_status);
704 break;
705 default: /* should never get here! */
706 printk(KERN_ERR "%s: Unknown message type (%d)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400707 wiphy_name(local->hw.wiphy), skb->pkt_type);
Jiri Bencf0706e82007-05-05 11:45:53 -0700708 dev_kfree_skb(skb);
709 break;
710 }
711 }
712}
713
Jiri Bencf0706e82007-05-05 11:45:53 -0700714/* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
715 * make a prepared TX frame (one that has been given to hw) to look like brand
716 * new IEEE 802.11 frame that is ready to go through TX processing again.
717 * Also, tx_packet_data in cb is restored from tx_control. */
718static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
719 struct ieee80211_key *key,
720 struct sk_buff *skb,
721 struct ieee80211_tx_control *control)
722{
723 int hdrlen, iv_len, mic_len;
724 struct ieee80211_tx_packet_data *pkt_data;
725
726 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
727 pkt_data->ifindex = control->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400728 pkt_data->flags = 0;
729 if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
730 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
731 if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
732 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
733 if (control->flags & IEEE80211_TXCTL_REQUEUE)
734 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Johannes Berg678f5f712007-12-19 01:31:23 +0100735 if (control->flags & IEEE80211_TXCTL_EAPOL_FRAME)
736 pkt_data->flags |= IEEE80211_TXPD_EAPOL_FRAME;
Jiri Bencf0706e82007-05-05 11:45:53 -0700737 pkt_data->queue = control->queue;
738
739 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
740
741 if (!key)
742 goto no_key;
743
Johannes Berg8f20fc22007-08-28 17:01:54 -0400744 switch (key->conf.alg) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700745 case ALG_WEP:
746 iv_len = WEP_IV_LEN;
747 mic_len = WEP_ICV_LEN;
748 break;
749 case ALG_TKIP:
750 iv_len = TKIP_IV_LEN;
751 mic_len = TKIP_ICV_LEN;
752 break;
753 case ALG_CCMP:
754 iv_len = CCMP_HDR_LEN;
755 mic_len = CCMP_MIC_LEN;
756 break;
757 default:
758 goto no_key;
759 }
760
Johannes Berg8f20fc22007-08-28 17:01:54 -0400761 if (skb->len >= mic_len &&
Johannes Berg11a843b2007-08-28 17:01:55 -0400762 !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Jiri Bencf0706e82007-05-05 11:45:53 -0700763 skb_trim(skb, skb->len - mic_len);
764 if (skb->len >= iv_len && skb->len > hdrlen) {
765 memmove(skb->data + iv_len, skb->data, hdrlen);
766 skb_pull(skb, iv_len);
767 }
768
769no_key:
770 {
771 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
772 u16 fc = le16_to_cpu(hdr->frame_control);
773 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
774 fc &= ~IEEE80211_STYPE_QOS_DATA;
775 hdr->frame_control = cpu_to_le16(fc);
776 memmove(skb->data + 2, skb->data, hdrlen - 2);
777 skb_pull(skb, 2);
778 }
779 }
780}
781
Jiri Bencf0706e82007-05-05 11:45:53 -0700782void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
783 struct ieee80211_tx_status *status)
784{
785 struct sk_buff *skb2;
786 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
787 struct ieee80211_local *local = hw_to_local(hw);
788 u16 frag, type;
Johannes Bergb306f452007-07-10 19:32:08 +0200789 struct ieee80211_tx_status_rtap_hdr *rthdr;
790 struct ieee80211_sub_if_data *sdata;
791 int monitors;
Jiri Bencf0706e82007-05-05 11:45:53 -0700792
793 if (!status) {
794 printk(KERN_ERR
795 "%s: ieee80211_tx_status called with NULL status\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400796 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -0700797 dev_kfree_skb(skb);
798 return;
799 }
800
801 if (status->excessive_retries) {
802 struct sta_info *sta;
803 sta = sta_info_get(local, hdr->addr1);
804 if (sta) {
805 if (sta->flags & WLAN_STA_PS) {
806 /* The STA is in power save mode, so assume
807 * that this TX packet failed because of that.
808 */
809 status->excessive_retries = 0;
810 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
811 }
812 sta_info_put(sta);
813 }
814 }
815
816 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
817 struct sta_info *sta;
818 sta = sta_info_get(local, hdr->addr1);
819 if (sta) {
820 sta->tx_filtered_count++;
821
822 /* Clear the TX filter mask for this STA when sending
823 * the next packet. If the STA went to power save mode,
824 * this will happen when it is waking up for the next
825 * time. */
826 sta->clear_dst_mask = 1;
827
828 /* TODO: Is the WLAN_STA_PS flag always set here or is
829 * the race between RX and TX status causing some
830 * packets to be filtered out before 80211.o gets an
831 * update for PS status? This seems to be the case, so
832 * no changes are likely to be needed. */
833 if (sta->flags & WLAN_STA_PS &&
834 skb_queue_len(&sta->tx_filtered) <
835 STA_MAX_TX_BUFFER) {
836 ieee80211_remove_tx_extra(local, sta->key,
837 skb,
838 &status->control);
839 skb_queue_tail(&sta->tx_filtered, skb);
840 } else if (!(sta->flags & WLAN_STA_PS) &&
841 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
842 /* Software retry the packet once */
843 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
844 ieee80211_remove_tx_extra(local, sta->key,
845 skb,
846 &status->control);
847 dev_queue_xmit(skb);
848 } else {
849 if (net_ratelimit()) {
850 printk(KERN_DEBUG "%s: dropped TX "
851 "filtered frame queue_len=%d "
852 "PS=%d @%lu\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400853 wiphy_name(local->hw.wiphy),
Jiri Bencf0706e82007-05-05 11:45:53 -0700854 skb_queue_len(
855 &sta->tx_filtered),
856 !!(sta->flags & WLAN_STA_PS),
857 jiffies);
858 }
859 dev_kfree_skb(skb);
860 }
861 sta_info_put(sta);
862 return;
863 }
Mattias Nissler1abbe492007-12-20 13:50:07 +0100864 } else
865 rate_control_tx_status(local->mdev, skb, status);
Jiri Bencf0706e82007-05-05 11:45:53 -0700866
867 ieee80211_led_tx(local, 0);
868
869 /* SNMP counters
870 * Fragments are passed to low-level drivers as separate skbs, so these
871 * are actually fragments, not frames. Update frame counters only for
872 * the first fragment of the frame. */
873
874 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
875 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
876
877 if (status->flags & IEEE80211_TX_STATUS_ACK) {
878 if (frag == 0) {
879 local->dot11TransmittedFrameCount++;
880 if (is_multicast_ether_addr(hdr->addr1))
881 local->dot11MulticastTransmittedFrameCount++;
882 if (status->retry_count > 0)
883 local->dot11RetryCount++;
884 if (status->retry_count > 1)
885 local->dot11MultipleRetryCount++;
886 }
887
888 /* This counter shall be incremented for an acknowledged MPDU
889 * with an individual address in the address 1 field or an MPDU
890 * with a multicast address in the address 1 field of type Data
891 * or Management. */
892 if (!is_multicast_ether_addr(hdr->addr1) ||
893 type == IEEE80211_FTYPE_DATA ||
894 type == IEEE80211_FTYPE_MGMT)
895 local->dot11TransmittedFragmentCount++;
896 } else {
897 if (frag == 0)
898 local->dot11FailedCount++;
899 }
900
Johannes Bergb306f452007-07-10 19:32:08 +0200901 /* this was a transmitted frame, but now we want to reuse it */
902 skb_orphan(skb);
903
Johannes Bergb306f452007-07-10 19:32:08 +0200904 if (!local->monitors) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700905 dev_kfree_skb(skb);
906 return;
907 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700908
Johannes Bergb306f452007-07-10 19:32:08 +0200909 /* send frame to monitor interfaces now */
910
911 if (skb_headroom(skb) < sizeof(*rthdr)) {
912 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
913 dev_kfree_skb(skb);
914 return;
915 }
916
917 rthdr = (struct ieee80211_tx_status_rtap_hdr*)
918 skb_push(skb, sizeof(*rthdr));
919
920 memset(rthdr, 0, sizeof(*rthdr));
921 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
922 rthdr->hdr.it_present =
923 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
924 (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
925
926 if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
927 !is_multicast_ether_addr(hdr->addr1))
928 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
929
930 if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
931 (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
932 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
933 else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
934 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
935
936 rthdr->data_retries = status->retry_count;
937
Johannes Berg79010422007-09-18 17:29:21 -0400938 rcu_read_lock();
Johannes Bergb306f452007-07-10 19:32:08 +0200939 monitors = local->monitors;
Johannes Berg79010422007-09-18 17:29:21 -0400940 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Bergb306f452007-07-10 19:32:08 +0200941 /*
942 * Using the monitors counter is possibly racy, but
943 * if the value is wrong we simply either clone the skb
944 * once too much or forget sending it to one monitor iface
945 * The latter case isn't nice but fixing the race is much
946 * more complicated.
947 */
948 if (!monitors || !skb)
949 goto out;
950
951 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
952 if (!netif_running(sdata->dev))
953 continue;
954 monitors--;
955 if (monitors)
Johannes Berg79010422007-09-18 17:29:21 -0400956 skb2 = skb_clone(skb, GFP_ATOMIC);
Johannes Bergb306f452007-07-10 19:32:08 +0200957 else
958 skb2 = NULL;
959 skb->dev = sdata->dev;
960 /* XXX: is this sufficient for BPF? */
961 skb_set_mac_header(skb, 0);
962 skb->ip_summed = CHECKSUM_UNNECESSARY;
963 skb->pkt_type = PACKET_OTHERHOST;
964 skb->protocol = htons(ETH_P_802_2);
965 memset(skb->cb, 0, sizeof(skb->cb));
966 netif_rx(skb);
967 skb = skb2;
Johannes Bergb306f452007-07-10 19:32:08 +0200968 }
969 }
970 out:
Johannes Berg79010422007-09-18 17:29:21 -0400971 rcu_read_unlock();
Johannes Bergb306f452007-07-10 19:32:08 +0200972 if (skb)
973 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700974}
975EXPORT_SYMBOL(ieee80211_tx_status);
976
Jiri Bencf0706e82007-05-05 11:45:53 -0700977struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
978 const struct ieee80211_ops *ops)
979{
980 struct net_device *mdev;
981 struct ieee80211_local *local;
982 struct ieee80211_sub_if_data *sdata;
983 int priv_size;
984 struct wiphy *wiphy;
985
986 /* Ensure 32-byte alignment of our private data and hw private data.
987 * We use the wiphy priv data for both our ieee80211_local and for
988 * the driver's private data
989 *
990 * In memory it'll be like this:
991 *
992 * +-------------------------+
993 * | struct wiphy |
994 * +-------------------------+
995 * | struct ieee80211_local |
996 * +-------------------------+
997 * | driver's private data |
998 * +-------------------------+
999 *
1000 */
1001 priv_size = ((sizeof(struct ieee80211_local) +
1002 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
1003 priv_data_len;
1004
1005 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
1006
1007 if (!wiphy)
1008 return NULL;
1009
1010 wiphy->privid = mac80211_wiphy_privid;
1011
1012 local = wiphy_priv(wiphy);
1013 local->hw.wiphy = wiphy;
1014
1015 local->hw.priv = (char *)local +
1016 ((sizeof(struct ieee80211_local) +
1017 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
1018
Johannes Berg4480f15c2007-07-10 19:32:10 +02001019 BUG_ON(!ops->tx);
Johannes Berg4150c572007-09-17 01:29:23 -04001020 BUG_ON(!ops->start);
1021 BUG_ON(!ops->stop);
Johannes Berg4480f15c2007-07-10 19:32:10 +02001022 BUG_ON(!ops->config);
1023 BUG_ON(!ops->add_interface);
Johannes Berg4150c572007-09-17 01:29:23 -04001024 BUG_ON(!ops->remove_interface);
1025 BUG_ON(!ops->configure_filter);
Jiri Bencf0706e82007-05-05 11:45:53 -07001026 local->ops = ops;
1027
1028 /* for now, mdev needs sub_if_data :/ */
1029 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
1030 "wmaster%d", ether_setup);
1031 if (!mdev) {
1032 wiphy_free(wiphy);
1033 return NULL;
1034 }
1035
1036 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
1037 mdev->ieee80211_ptr = &sdata->wdev;
1038 sdata->wdev.wiphy = wiphy;
1039
1040 local->hw.queues = 1; /* default */
1041
1042 local->mdev = mdev;
1043 local->rx_pre_handlers = ieee80211_rx_pre_handlers;
1044 local->rx_handlers = ieee80211_rx_handlers;
1045 local->tx_handlers = ieee80211_tx_handlers;
1046
1047 local->bridge_packets = 1;
1048
1049 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1050 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1051 local->short_retry_limit = 7;
1052 local->long_retry_limit = 4;
1053 local->hw.conf.radio_enabled = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07001054
Johannes Bergb708e612007-09-14 11:10:25 -04001055 local->enabled_modes = ~0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001056
1057 INIT_LIST_HEAD(&local->modes_list);
1058
Johannes Berg79010422007-09-18 17:29:21 -04001059 INIT_LIST_HEAD(&local->interfaces);
Jiri Bencf0706e82007-05-05 11:45:53 -07001060
1061 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
Jiri Bencf0706e82007-05-05 11:45:53 -07001062 ieee80211_rx_bss_list_init(mdev);
1063
1064 sta_info_init(local);
1065
1066 mdev->hard_start_xmit = ieee80211_master_start_xmit;
1067 mdev->open = ieee80211_master_open;
1068 mdev->stop = ieee80211_master_stop;
1069 mdev->type = ARPHRD_IEEE80211;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001070 mdev->header_ops = &ieee80211_header_ops;
Johannes Berg4150c572007-09-17 01:29:23 -04001071 mdev->set_multicast_list = ieee80211_master_set_multicast_list;
Jiri Bencf0706e82007-05-05 11:45:53 -07001072
1073 sdata->type = IEEE80211_IF_TYPE_AP;
1074 sdata->dev = mdev;
1075 sdata->local = local;
1076 sdata->u.ap.force_unicast_rateidx = -1;
1077 sdata->u.ap.max_ratectrl_rateidx = -1;
1078 ieee80211_if_sdata_init(sdata);
Johannes Berg79010422007-09-18 17:29:21 -04001079 /* no RCU needed since we're still during init phase */
1080 list_add_tail(&sdata->list, &local->interfaces);
Jiri Bencf0706e82007-05-05 11:45:53 -07001081
1082 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1083 (unsigned long)local);
1084 tasklet_disable(&local->tx_pending_tasklet);
1085
1086 tasklet_init(&local->tasklet,
1087 ieee80211_tasklet_handler,
1088 (unsigned long) local);
1089 tasklet_disable(&local->tasklet);
1090
1091 skb_queue_head_init(&local->skb_queue);
1092 skb_queue_head_init(&local->skb_queue_unreliable);
1093
1094 return local_to_hw(local);
1095}
1096EXPORT_SYMBOL(ieee80211_alloc_hw);
1097
1098int ieee80211_register_hw(struct ieee80211_hw *hw)
1099{
1100 struct ieee80211_local *local = hw_to_local(hw);
1101 const char *name;
1102 int result;
1103
1104 result = wiphy_register(local->hw.wiphy);
1105 if (result < 0)
1106 return result;
1107
1108 name = wiphy_dev(local->hw.wiphy)->driver->name;
1109 local->hw.workqueue = create_singlethread_workqueue(name);
1110 if (!local->hw.workqueue) {
1111 result = -ENOMEM;
1112 goto fail_workqueue;
1113 }
1114
Johannes Bergb306f452007-07-10 19:32:08 +02001115 /*
1116 * The hardware needs headroom for sending the frame,
1117 * and we need some headroom for passing the frame to monitor
1118 * interfaces, but never both at the same time.
1119 */
Jiri Benc33ccad32007-07-18 17:10:44 +02001120 local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1121 sizeof(struct ieee80211_tx_status_rtap_hdr));
Johannes Bergb306f452007-07-10 19:32:08 +02001122
Jiri Bence9f207f2007-05-05 11:46:38 -07001123 debugfs_hw_add(local);
1124
Jiri Bencf0706e82007-05-05 11:45:53 -07001125 local->hw.conf.beacon_int = 1000;
1126
1127 local->wstats_flags |= local->hw.max_rssi ?
1128 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1129 local->wstats_flags |= local->hw.max_signal ?
1130 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1131 local->wstats_flags |= local->hw.max_noise ?
1132 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1133 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1134 local->wstats_flags |= IW_QUAL_DBM;
1135
1136 result = sta_info_start(local);
1137 if (result < 0)
1138 goto fail_sta_info;
1139
1140 rtnl_lock();
1141 result = dev_alloc_name(local->mdev, local->mdev->name);
1142 if (result < 0)
1143 goto fail_dev;
1144
1145 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1146 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1147
1148 result = register_netdevice(local->mdev);
1149 if (result < 0)
1150 goto fail_dev;
1151
Jiri Bence9f207f2007-05-05 11:46:38 -07001152 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Johannes Berg5b2812e2007-09-26 14:27:23 +02001153 ieee80211_if_set_type(local->mdev, IEEE80211_IF_TYPE_AP);
Jiri Bence9f207f2007-05-05 11:46:38 -07001154
Johannes Berg830f9032007-10-28 14:51:05 +01001155 result = ieee80211_init_rate_ctrl_alg(local,
1156 hw->rate_control_algorithm);
Jiri Bencf0706e82007-05-05 11:45:53 -07001157 if (result < 0) {
1158 printk(KERN_DEBUG "%s: Failed to initialize rate control "
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001159 "algorithm\n", wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001160 goto fail_rate;
1161 }
1162
1163 result = ieee80211_wep_init(local);
1164
1165 if (result < 0) {
1166 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001167 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001168 goto fail_wep;
1169 }
1170
1171 ieee80211_install_qdisc(local->mdev);
1172
1173 /* add one default STA interface */
1174 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
1175 IEEE80211_IF_TYPE_STA);
1176 if (result)
1177 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001178 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001179
1180 local->reg_state = IEEE80211_DEV_REGISTERED;
1181 rtnl_unlock();
1182
1183 ieee80211_led_init(local);
1184
1185 return 0;
1186
1187fail_wep:
1188 rate_control_deinitialize(local);
1189fail_rate:
Jiri Bence9f207f2007-05-05 11:46:38 -07001190 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001191 unregister_netdevice(local->mdev);
1192fail_dev:
1193 rtnl_unlock();
1194 sta_info_stop(local);
1195fail_sta_info:
Jiri Bence9f207f2007-05-05 11:46:38 -07001196 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001197 destroy_workqueue(local->hw.workqueue);
1198fail_workqueue:
1199 wiphy_unregister(local->hw.wiphy);
1200 return result;
1201}
1202EXPORT_SYMBOL(ieee80211_register_hw);
1203
1204int ieee80211_register_hwmode(struct ieee80211_hw *hw,
1205 struct ieee80211_hw_mode *mode)
1206{
1207 struct ieee80211_local *local = hw_to_local(hw);
1208 struct ieee80211_rate *rate;
1209 int i;
1210
1211 INIT_LIST_HEAD(&mode->list);
1212 list_add_tail(&mode->list, &local->modes_list);
1213
1214 local->hw_modes |= (1 << mode->mode);
1215 for (i = 0; i < mode->num_rates; i++) {
1216 rate = &(mode->rates[i]);
1217 rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
1218 }
1219 ieee80211_prepare_rates(local, mode);
1220
1221 if (!local->oper_hw_mode) {
1222 /* Default to this mode */
1223 local->hw.conf.phymode = mode->mode;
1224 local->oper_hw_mode = local->scan_hw_mode = mode;
1225 local->oper_channel = local->scan_channel = &mode->channels[0];
1226 local->hw.conf.mode = local->oper_hw_mode;
1227 local->hw.conf.chan = local->oper_channel;
1228 }
1229
1230 if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
Daniel Drakefd8bacc2007-06-09 19:07:14 +01001231 ieee80211_set_default_regdomain(mode);
Jiri Bencf0706e82007-05-05 11:45:53 -07001232
1233 return 0;
1234}
1235EXPORT_SYMBOL(ieee80211_register_hwmode);
1236
1237void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1238{
1239 struct ieee80211_local *local = hw_to_local(hw);
1240 struct ieee80211_sub_if_data *sdata, *tmp;
Jiri Bencf0706e82007-05-05 11:45:53 -07001241 int i;
1242
1243 tasklet_kill(&local->tx_pending_tasklet);
1244 tasklet_kill(&local->tasklet);
1245
1246 rtnl_lock();
1247
1248 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1249
1250 local->reg_state = IEEE80211_DEV_UNREGISTERED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001251
Johannes Berg79010422007-09-18 17:29:21 -04001252 /*
1253 * At this point, interface list manipulations are fine
1254 * because the driver cannot be handing us frames any
1255 * more and the tasklet is killed.
1256 */
Johannes Berg5b2812e2007-09-26 14:27:23 +02001257
1258 /*
1259 * First, we remove all non-master interfaces. Do this because they
1260 * may have bss pointer dependency on the master, and when we free
1261 * the master these would be freed as well, breaking our list
1262 * iteration completely.
1263 */
1264 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
1265 if (sdata->dev == local->mdev)
1266 continue;
1267 list_del(&sdata->list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001268 __ieee80211_if_del(local, sdata);
Johannes Berg5b2812e2007-09-26 14:27:23 +02001269 }
1270
1271 /* then, finally, remove the master interface */
1272 __ieee80211_if_del(local, IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001273
1274 rtnl_unlock();
1275
Jiri Bencf0706e82007-05-05 11:45:53 -07001276 ieee80211_rx_bss_list_deinit(local->mdev);
1277 ieee80211_clear_tx_pending(local);
1278 sta_info_stop(local);
1279 rate_control_deinitialize(local);
Jiri Bence9f207f2007-05-05 11:46:38 -07001280 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001281
1282 for (i = 0; i < NUM_IEEE80211_MODES; i++) {
1283 kfree(local->supp_rates[i]);
1284 kfree(local->basic_rates[i]);
1285 }
1286
1287 if (skb_queue_len(&local->skb_queue)
1288 || skb_queue_len(&local->skb_queue_unreliable))
1289 printk(KERN_WARNING "%s: skb_queue not empty\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001290 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001291 skb_queue_purge(&local->skb_queue);
1292 skb_queue_purge(&local->skb_queue_unreliable);
1293
1294 destroy_workqueue(local->hw.workqueue);
1295 wiphy_unregister(local->hw.wiphy);
1296 ieee80211_wep_free(local);
1297 ieee80211_led_exit(local);
1298}
1299EXPORT_SYMBOL(ieee80211_unregister_hw);
1300
1301void ieee80211_free_hw(struct ieee80211_hw *hw)
1302{
1303 struct ieee80211_local *local = hw_to_local(hw);
1304
1305 ieee80211_if_free(local->mdev);
1306 wiphy_free(local->hw.wiphy);
1307}
1308EXPORT_SYMBOL(ieee80211_free_hw);
1309
Jiri Bencf0706e82007-05-05 11:45:53 -07001310static int __init ieee80211_init(void)
1311{
1312 struct sk_buff *skb;
1313 int ret;
1314
1315 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1316
Stefano Brivioc21b39a2007-12-19 01:26:16 +01001317#ifdef CONFIG_MAC80211_RC_SIMPLE
Johannes Bergac71c692007-10-28 14:17:44 +01001318 ret = ieee80211_rate_control_register(&mac80211_rcsimple);
1319 if (ret)
Mattias Nisslerad018372007-12-19 01:25:57 +01001320 goto fail;
1321#endif
1322
Stefano Brivioc21b39a2007-12-19 01:26:16 +01001323#ifdef CONFIG_MAC80211_RC_PID
Mattias Nisslerad018372007-12-19 01:25:57 +01001324 ret = ieee80211_rate_control_register(&mac80211_rcpid);
1325 if (ret)
1326 goto fail;
Johannes Bergac71c692007-10-28 14:17:44 +01001327#endif
1328
Jiri Bencf0706e82007-05-05 11:45:53 -07001329 ret = ieee80211_wme_register();
1330 if (ret) {
1331 printk(KERN_DEBUG "ieee80211_init: failed to "
1332 "initialize WME (err=%d)\n", ret);
Mattias Nisslerad018372007-12-19 01:25:57 +01001333 goto fail;
Jiri Bencf0706e82007-05-05 11:45:53 -07001334 }
1335
Jiri Bence9f207f2007-05-05 11:46:38 -07001336 ieee80211_debugfs_netdev_init();
Daniel Drakefd8bacc2007-06-09 19:07:14 +01001337 ieee80211_regdomain_init();
Jiri Bence9f207f2007-05-05 11:46:38 -07001338
Jiri Bencf0706e82007-05-05 11:45:53 -07001339 return 0;
Mattias Nisslerad018372007-12-19 01:25:57 +01001340
1341fail:
1342
Stefano Brivioc21b39a2007-12-19 01:26:16 +01001343#ifdef CONFIG_MAC80211_RC_SIMPLE
Mattias Nisslerad018372007-12-19 01:25:57 +01001344 ieee80211_rate_control_unregister(&mac80211_rcsimple);
1345#endif
Stefano Brivioc21b39a2007-12-19 01:26:16 +01001346#ifdef CONFIG_MAC80211_RC_PID
Mattias Nisslerad018372007-12-19 01:25:57 +01001347 ieee80211_rate_control_unregister(&mac80211_rcpid);
1348#endif
1349
1350 return ret;
Jiri Bencf0706e82007-05-05 11:45:53 -07001351}
1352
Jiri Bencf0706e82007-05-05 11:45:53 -07001353static void __exit ieee80211_exit(void)
1354{
Stefano Brivioc21b39a2007-12-19 01:26:16 +01001355#ifdef CONFIG_MAC80211_RC_SIMPLE
Johannes Bergac71c692007-10-28 14:17:44 +01001356 ieee80211_rate_control_unregister(&mac80211_rcsimple);
1357#endif
Stefano Brivioc21b39a2007-12-19 01:26:16 +01001358#ifdef CONFIG_MAC80211_RC_PID
Mattias Nisslerad018372007-12-19 01:25:57 +01001359 ieee80211_rate_control_unregister(&mac80211_rcpid);
1360#endif
Johannes Bergac71c692007-10-28 14:17:44 +01001361
Jiri Bencf0706e82007-05-05 11:45:53 -07001362 ieee80211_wme_unregister();
Jiri Bence9f207f2007-05-05 11:46:38 -07001363 ieee80211_debugfs_netdev_exit();
Jiri Bencf0706e82007-05-05 11:45:53 -07001364}
1365
1366
Johannes Bergca9938f2007-09-11 12:50:32 +02001367subsys_initcall(ieee80211_init);
Jiri Bencf0706e82007-05-05 11:45:53 -07001368module_exit(ieee80211_exit);
1369
1370MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1371MODULE_LICENSE("GPL");