blob: 6378850d85805860416e9e67f7b0e308e76e6e0a [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
Johannes Bergb306f452007-07-10 19:32:08 +020037/*
38 * For seeing transmitted packets on monitor interfaces
39 * we have a radiotap header too.
40 */
41struct ieee80211_tx_status_rtap_hdr {
42 struct ieee80211_radiotap_header hdr;
43 __le16 tx_flags;
44 u8 data_retries;
45} __attribute__ ((packed));
46
Johannes Bergb2c258f2007-07-27 15:43:23 +020047/* common interface routines */
Jiri Bencf0706e82007-05-05 11:45:53 -070048
Stephen Hemmingerb95cce32007-09-26 22:13:38 -070049static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
Jiri Bencf0706e82007-05-05 11:45:53 -070050{
Johannes Bergb2c258f2007-07-27 15:43:23 +020051 memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
52 return ETH_ALEN;
Jiri Bencf0706e82007-05-05 11:45:53 -070053}
54
Johannes Berg4150c572007-09-17 01:29:23 -040055/* must be called under mdev tx lock */
56static void ieee80211_configure_filter(struct ieee80211_local *local)
57{
58 unsigned int changed_flags;
59 unsigned int new_flags = 0;
60
Johannes Berg53918992007-09-26 15:19:47 +020061 if (atomic_read(&local->iff_promiscs))
Johannes Berg4150c572007-09-17 01:29:23 -040062 new_flags |= FIF_PROMISC_IN_BSS;
63
Johannes Berg53918992007-09-26 15:19:47 +020064 if (atomic_read(&local->iff_allmultis))
Johannes Berg4150c572007-09-17 01:29:23 -040065 new_flags |= FIF_ALLMULTI;
66
67 if (local->monitors)
68 new_flags |= FIF_CONTROL |
69 FIF_OTHER_BSS |
70 FIF_BCN_PRBRESP_PROMISC;
71
72 changed_flags = local->filter_flags ^ new_flags;
73
74 /* be a bit nasty */
75 new_flags |= (1<<31);
76
77 local->ops->configure_filter(local_to_hw(local),
78 changed_flags, &new_flags,
79 local->mdev->mc_count,
80 local->mdev->mc_list);
81
82 WARN_ON(new_flags & (1<<31));
83
84 local->filter_flags = new_flags & ~(1<<31);
85}
86
Johannes Bergb2c258f2007-07-27 15:43:23 +020087/* master interface */
Jiri Bencf0706e82007-05-05 11:45:53 -070088
89static int ieee80211_master_open(struct net_device *dev)
90{
91 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
92 struct ieee80211_sub_if_data *sdata;
93 int res = -EOPNOTSUPP;
94
Johannes Berg79010422007-09-18 17:29:21 -040095 /* we hold the RTNL here so can safely walk the list */
96 list_for_each_entry(sdata, &local->interfaces, list) {
Jiri Bencf0706e82007-05-05 11:45:53 -070097 if (sdata->dev != dev && netif_running(sdata->dev)) {
98 res = 0;
99 break;
100 }
101 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700102 return res;
103}
104
105static int ieee80211_master_stop(struct net_device *dev)
106{
107 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
108 struct ieee80211_sub_if_data *sdata;
109
Johannes Berg79010422007-09-18 17:29:21 -0400110 /* we hold the RTNL here so can safely walk the list */
111 list_for_each_entry(sdata, &local->interfaces, list)
Jiri Bencf0706e82007-05-05 11:45:53 -0700112 if (sdata->dev != dev && netif_running(sdata->dev))
113 dev_close(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700114
115 return 0;
116}
117
Johannes Berg4150c572007-09-17 01:29:23 -0400118static void ieee80211_master_set_multicast_list(struct net_device *dev)
119{
120 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
121
122 ieee80211_configure_filter(local);
123}
124
Johannes Bergb2c258f2007-07-27 15:43:23 +0200125/* regular interfaces */
126
127static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
128{
129 /* FIX: what would be proper limits for MTU?
130 * This interface uses 802.3 frames. */
131 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
132 printk(KERN_WARNING "%s: invalid MTU %d\n",
133 dev->name, new_mtu);
134 return -EINVAL;
135 }
136
137#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
138 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
139#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
140 dev->mtu = new_mtu;
141 return 0;
142}
143
144static inline int identical_mac_addr_allowed(int type1, int type2)
145{
146 return (type1 == IEEE80211_IF_TYPE_MNTR ||
147 type2 == IEEE80211_IF_TYPE_MNTR ||
148 (type1 == IEEE80211_IF_TYPE_AP &&
149 type2 == IEEE80211_IF_TYPE_WDS) ||
150 (type1 == IEEE80211_IF_TYPE_WDS &&
151 (type2 == IEEE80211_IF_TYPE_WDS ||
152 type2 == IEEE80211_IF_TYPE_AP)) ||
153 (type1 == IEEE80211_IF_TYPE_AP &&
154 type2 == IEEE80211_IF_TYPE_VLAN) ||
155 (type1 == IEEE80211_IF_TYPE_VLAN &&
156 (type2 == IEEE80211_IF_TYPE_AP ||
157 type2 == IEEE80211_IF_TYPE_VLAN)));
158}
159
Johannes Bergb2c258f2007-07-27 15:43:23 +0200160static int ieee80211_open(struct net_device *dev)
161{
162 struct ieee80211_sub_if_data *sdata, *nsdata;
163 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
164 struct ieee80211_if_init_conf conf;
165 int res;
166
167 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400168
Johannes Berg79010422007-09-18 17:29:21 -0400169 /* we hold the RTNL here so can safely walk the list */
170 list_for_each_entry(nsdata, &local->interfaces, list) {
Johannes Bergb2c258f2007-07-27 15:43:23 +0200171 struct net_device *ndev = nsdata->dev;
172
173 if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400174 compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0) {
175 /*
176 * check whether it may have the same address
177 */
178 if (!identical_mac_addr_allowed(sdata->type,
Johannes Berg79010422007-09-18 17:29:21 -0400179 nsdata->type))
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400180 return -ENOTUNIQ;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400181
182 /*
183 * can only add VLANs to enabled APs
184 */
185 if (sdata->type == IEEE80211_IF_TYPE_VLAN &&
186 nsdata->type == IEEE80211_IF_TYPE_AP &&
187 netif_running(nsdata->dev))
188 sdata->u.vlan.ap = nsdata;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200189 }
190 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200191
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400192 switch (sdata->type) {
193 case IEEE80211_IF_TYPE_WDS:
194 if (is_zero_ether_addr(sdata->u.wds.remote_addr))
195 return -ENOLINK;
196 break;
197 case IEEE80211_IF_TYPE_VLAN:
198 if (!sdata->u.vlan.ap)
199 return -ENOLINK;
200 break;
Johannes Bergfb1c1cd2007-09-26 15:19:43 +0200201 case IEEE80211_IF_TYPE_AP:
Johannes Bergfb1c1cd2007-09-26 15:19:43 +0200202 case IEEE80211_IF_TYPE_STA:
203 case IEEE80211_IF_TYPE_MNTR:
204 case IEEE80211_IF_TYPE_IBSS:
205 /* no special treatment */
206 break;
Johannes Berga2897552007-09-28 14:01:25 +0200207 case IEEE80211_IF_TYPE_INVALID:
208 /* cannot happen */
209 WARN_ON(1);
210 break;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400211 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200212
Johannes Bergb2c258f2007-07-27 15:43:23 +0200213 if (local->open_count == 0) {
214 res = 0;
Johannes Berg4150c572007-09-17 01:29:23 -0400215 if (local->ops->start)
216 res = local->ops->start(local_to_hw(local));
217 if (res)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200218 return res;
Michael Wu8b393f12007-11-28 01:57:08 -0500219 ieee80211_hw_config(local);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200220 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200221
Johannes Berg4150c572007-09-17 01:29:23 -0400222 switch (sdata->type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400223 case IEEE80211_IF_TYPE_VLAN:
224 list_add(&sdata->u.vlan.list, &sdata->u.vlan.ap->u.ap.vlans);
225 /* no need to tell driver */
226 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400227 case IEEE80211_IF_TYPE_MNTR:
228 /* must be before the call to ieee80211_configure_filter */
Johannes Bergb2c258f2007-07-27 15:43:23 +0200229 local->monitors++;
Johannes Berg4150c572007-09-17 01:29:23 -0400230 if (local->monitors == 1) {
231 netif_tx_lock_bh(local->mdev);
232 ieee80211_configure_filter(local);
233 netif_tx_unlock_bh(local->mdev);
234
235 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
Johannes Berg4150c572007-09-17 01:29:23 -0400236 }
237 break;
238 case IEEE80211_IF_TYPE_STA:
239 case IEEE80211_IF_TYPE_IBSS:
240 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
241 /* fall through */
242 default:
243 conf.if_id = dev->ifindex;
244 conf.type = sdata->type;
245 conf.mac_addr = dev->dev_addr;
246 res = local->ops->add_interface(local_to_hw(local), &conf);
247 if (res && !local->open_count && local->ops->stop)
248 local->ops->stop(local_to_hw(local));
249 if (res)
250 return res;
251
Johannes Bergb2c258f2007-07-27 15:43:23 +0200252 ieee80211_if_config(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200253 ieee80211_reset_erp_info(dev);
Johannes Berg11a843b2007-08-28 17:01:55 -0400254 ieee80211_enable_keys(sdata);
Johannes Berg4150c572007-09-17 01:29:23 -0400255
256 if (sdata->type == IEEE80211_IF_TYPE_STA &&
Johannes Bergddd3d2b2007-09-26 17:53:20 +0200257 !(sdata->flags & IEEE80211_SDATA_USERSPACE_MLME))
Johannes Berg4150c572007-09-17 01:29:23 -0400258 netif_carrier_off(dev);
259 else
260 netif_carrier_on(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200261 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200262
Johannes Berg4150c572007-09-17 01:29:23 -0400263 if (local->open_count == 0) {
264 res = dev_open(local->mdev);
265 WARN_ON(res);
Johannes Berg4150c572007-09-17 01:29:23 -0400266 tasklet_enable(&local->tx_pending_tasklet);
267 tasklet_enable(&local->tasklet);
268 }
269
Johannes Bergc1428b32007-11-16 02:54:53 +0100270 /*
271 * set_multicast_list will be invoked by the networking core
272 * which will check whether any increments here were done in
273 * error and sync them down to the hardware as filter flags.
274 */
275 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
276 atomic_inc(&local->iff_allmultis);
277
278 if (sdata->flags & IEEE80211_SDATA_PROMISC)
279 atomic_inc(&local->iff_promiscs);
280
Johannes Berg4150c572007-09-17 01:29:23 -0400281 local->open_count++;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200282
283 netif_start_queue(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400284
Johannes Bergb2c258f2007-07-27 15:43:23 +0200285 return 0;
286}
287
Johannes Berg4150c572007-09-17 01:29:23 -0400288static int ieee80211_stop(struct net_device *dev)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200289{
Johannes Berg4150c572007-09-17 01:29:23 -0400290 struct ieee80211_sub_if_data *sdata;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200291 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg4150c572007-09-17 01:29:23 -0400292 struct ieee80211_if_init_conf conf;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200293
Johannes Berg4150c572007-09-17 01:29:23 -0400294 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
295
296 netif_stop_queue(dev);
297
Johannes Bergc1428b32007-11-16 02:54:53 +0100298 /*
299 * Don't count this interface for promisc/allmulti while it
300 * is down. dev_mc_unsync() will invoke set_multicast_list
301 * on the master interface which will sync these down to the
302 * hardware as filter flags.
303 */
304 if (sdata->flags & IEEE80211_SDATA_ALLMULTI)
305 atomic_dec(&local->iff_allmultis);
306
307 if (sdata->flags & IEEE80211_SDATA_PROMISC)
308 atomic_dec(&local->iff_promiscs);
309
Johannes Berg4150c572007-09-17 01:29:23 -0400310 dev_mc_unsync(local->mdev, dev);
311
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400312 /* down all dependent devices, that is VLANs */
313 if (sdata->type == IEEE80211_IF_TYPE_AP) {
314 struct ieee80211_sub_if_data *vlan, *tmp;
315
316 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
317 u.vlan.list)
318 dev_close(vlan->dev);
319 WARN_ON(!list_empty(&sdata->u.ap.vlans));
320 }
321
Johannes Berg4150c572007-09-17 01:29:23 -0400322 local->open_count--;
323
Johannes Bergb2c258f2007-07-27 15:43:23 +0200324 switch (sdata->type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400325 case IEEE80211_IF_TYPE_VLAN:
326 list_del(&sdata->u.vlan.list);
327 sdata->u.vlan.ap = NULL;
328 /* no need to tell driver */
329 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400330 case IEEE80211_IF_TYPE_MNTR:
331 local->monitors--;
332 if (local->monitors == 0) {
333 netif_tx_lock_bh(local->mdev);
334 ieee80211_configure_filter(local);
335 netif_tx_unlock_bh(local->mdev);
336
Michael Wu8b393f12007-11-28 01:57:08 -0500337 local->hw.conf.flags &= ~IEEE80211_CONF_RADIOTAP;
Johannes Berg4150c572007-09-17 01:29:23 -0400338 }
339 break;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200340 case IEEE80211_IF_TYPE_STA:
341 case IEEE80211_IF_TYPE_IBSS:
342 sdata->u.sta.state = IEEE80211_DISABLED;
343 del_timer_sync(&sdata->u.sta.timer);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400344 /*
Johannes Berg79010422007-09-18 17:29:21 -0400345 * When we get here, the interface is marked down.
346 * Call synchronize_rcu() to wait for the RX path
347 * should it be using the interface and enqueuing
348 * frames at this very time on another CPU.
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400349 */
Johannes Berg79010422007-09-18 17:29:21 -0400350 synchronize_rcu();
Johannes Bergb2c258f2007-07-27 15:43:23 +0200351 skb_queue_purge(&sdata->u.sta.skb_queue);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400352
Johannes Bergb2c258f2007-07-27 15:43:23 +0200353 if (!local->ops->hw_scan &&
354 local->scan_dev == sdata->dev) {
355 local->sta_scanning = 0;
356 cancel_delayed_work(&local->scan_work);
357 }
358 flush_workqueue(local->hw.workqueue);
Zhu Yia10605e2007-11-22 11:10:22 +0800359
360 sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
361 kfree(sdata->u.sta.extra_ie);
362 sdata->u.sta.extra_ie = NULL;
363 sdata->u.sta.extra_ie_len = 0;
Johannes Berg4150c572007-09-17 01:29:23 -0400364 /* fall through */
365 default:
Johannes Bergb2c258f2007-07-27 15:43:23 +0200366 conf.if_id = dev->ifindex;
367 conf.type = sdata->type;
368 conf.mac_addr = dev->dev_addr;
Johannes Berg4150c572007-09-17 01:29:23 -0400369 /* disable all keys for as long as this netdev is down */
370 ieee80211_disable_keys(sdata);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200371 local->ops->remove_interface(local_to_hw(local), &conf);
372 }
373
Johannes Berg4150c572007-09-17 01:29:23 -0400374 if (local->open_count == 0) {
375 if (netif_running(local->mdev))
376 dev_close(local->mdev);
377
Johannes Berg4150c572007-09-17 01:29:23 -0400378 if (local->ops->stop)
379 local->ops->stop(local_to_hw(local));
380
381 tasklet_disable(&local->tx_pending_tasklet);
382 tasklet_disable(&local->tasklet);
383 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200384
385 return 0;
386}
387
Johannes Bergb2c258f2007-07-27 15:43:23 +0200388static void ieee80211_set_multicast_list(struct net_device *dev)
389{
390 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
391 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400392 int allmulti, promisc, sdata_allmulti, sdata_promisc;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200393
Johannes Berg4150c572007-09-17 01:29:23 -0400394 allmulti = !!(dev->flags & IFF_ALLMULTI);
395 promisc = !!(dev->flags & IFF_PROMISC);
Johannes Bergb52f2192007-11-16 01:49:11 +0100396 sdata_allmulti = !!(sdata->flags & IEEE80211_SDATA_ALLMULTI);
397 sdata_promisc = !!(sdata->flags & IEEE80211_SDATA_PROMISC);
Johannes Berg4150c572007-09-17 01:29:23 -0400398
399 if (allmulti != sdata_allmulti) {
400 if (dev->flags & IFF_ALLMULTI)
Johannes Berg53918992007-09-26 15:19:47 +0200401 atomic_inc(&local->iff_allmultis);
Johannes Berg4150c572007-09-17 01:29:23 -0400402 else
Johannes Berg53918992007-09-26 15:19:47 +0200403 atomic_dec(&local->iff_allmultis);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400404 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200405 }
Johannes Berg4150c572007-09-17 01:29:23 -0400406
407 if (promisc != sdata_promisc) {
408 if (dev->flags & IFF_PROMISC)
Johannes Berg53918992007-09-26 15:19:47 +0200409 atomic_inc(&local->iff_promiscs);
Johannes Berg4150c572007-09-17 01:29:23 -0400410 else
Johannes Berg53918992007-09-26 15:19:47 +0200411 atomic_dec(&local->iff_promiscs);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400412 sdata->flags ^= IEEE80211_SDATA_PROMISC;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200413 }
Johannes Berg4150c572007-09-17 01:29:23 -0400414
415 dev_mc_sync(local->mdev, dev);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200416}
417
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700418static const struct header_ops ieee80211_header_ops = {
419 .create = eth_header,
420 .parse = header_parse_80211,
421 .rebuild = eth_rebuild_header,
422 .cache = eth_header_cache,
423 .cache_update = eth_header_cache_update,
424};
425
Johannes Bergf9d540e2007-09-28 14:02:09 +0200426/* Must not be called for mdev */
Johannes Bergb2c258f2007-07-27 15:43:23 +0200427void ieee80211_if_setup(struct net_device *dev)
428{
429 ether_setup(dev);
430 dev->hard_start_xmit = ieee80211_subif_start_xmit;
431 dev->wireless_handlers = &ieee80211_iw_handler_def;
432 dev->set_multicast_list = ieee80211_set_multicast_list;
433 dev->change_mtu = ieee80211_change_mtu;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200434 dev->open = ieee80211_open;
435 dev->stop = ieee80211_stop;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200436 dev->destructor = ieee80211_if_free;
437}
438
439/* WDS specialties */
440
441int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
442{
443 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
444 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
445 struct sta_info *sta;
Joe Perches0795af52007-10-03 17:59:30 -0700446 DECLARE_MAC_BUF(mac);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200447
448 if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
449 return 0;
450
451 /* Create STA entry for the new peer */
452 sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
453 if (!sta)
454 return -ENOMEM;
455 sta_info_put(sta);
456
457 /* Remove STA entry for the old peer */
458 sta = sta_info_get(local, sdata->u.wds.remote_addr);
459 if (sta) {
Michael Wube8755e2007-07-27 15:43:23 +0200460 sta_info_free(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200461 sta_info_put(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200462 } else {
463 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
Joe Perches0795af52007-10-03 17:59:30 -0700464 "peer %s\n",
465 dev->name, print_mac(mac, sdata->u.wds.remote_addr));
Johannes Bergb2c258f2007-07-27 15:43:23 +0200466 }
467
468 /* Update WDS link data */
469 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
470
471 return 0;
472}
473
474/* everything else */
475
Johannes Bergb2c258f2007-07-27 15:43:23 +0200476static int __ieee80211_if_config(struct net_device *dev,
477 struct sk_buff *beacon,
478 struct ieee80211_tx_control *control)
479{
480 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
481 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
482 struct ieee80211_if_conf conf;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200483
484 if (!local->ops->config_interface || !netif_running(dev))
485 return 0;
486
487 memset(&conf, 0, sizeof(conf));
488 conf.type = sdata->type;
489 if (sdata->type == IEEE80211_IF_TYPE_STA ||
490 sdata->type == IEEE80211_IF_TYPE_IBSS) {
Johannes Berg4150c572007-09-17 01:29:23 -0400491 conf.bssid = sdata->u.sta.bssid;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200492 conf.ssid = sdata->u.sta.ssid;
493 conf.ssid_len = sdata->u.sta.ssid_len;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200494 } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
495 conf.ssid = sdata->u.ap.ssid;
496 conf.ssid_len = sdata->u.ap.ssid_len;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200497 conf.beacon = beacon;
498 conf.beacon_control = control;
499 }
500 return local->ops->config_interface(local_to_hw(local),
501 dev->ifindex, &conf);
502}
503
504int ieee80211_if_config(struct net_device *dev)
505{
506 return __ieee80211_if_config(dev, NULL, NULL);
507}
508
509int ieee80211_if_config_beacon(struct net_device *dev)
510{
511 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
512 struct ieee80211_tx_control control;
513 struct sk_buff *skb;
514
515 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
516 return 0;
517 skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
518 if (!skb)
519 return -ENOMEM;
520 return __ieee80211_if_config(dev, skb, &control);
521}
522
523int ieee80211_hw_config(struct ieee80211_local *local)
524{
525 struct ieee80211_hw_mode *mode;
526 struct ieee80211_channel *chan;
527 int ret = 0;
528
529 if (local->sta_scanning) {
530 chan = local->scan_channel;
531 mode = local->scan_hw_mode;
532 } else {
533 chan = local->oper_channel;
534 mode = local->oper_hw_mode;
535 }
536
537 local->hw.conf.channel = chan->chan;
538 local->hw.conf.channel_val = chan->val;
Michael Buesch61609bc2007-09-20 22:06:39 +0200539 if (!local->hw.conf.power_level) {
540 local->hw.conf.power_level = chan->power_level;
541 } else {
542 local->hw.conf.power_level = min(chan->power_level,
543 local->hw.conf.power_level);
544 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200545 local->hw.conf.freq = chan->freq;
546 local->hw.conf.phymode = mode->mode;
547 local->hw.conf.antenna_max = chan->antenna_max;
548 local->hw.conf.chan = chan;
549 local->hw.conf.mode = mode;
550
551#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
552 printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
553 "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
554 local->hw.conf.phymode);
555#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
556
Michael Bueschf7c4dae2007-09-24 18:41:49 +0200557 if (local->open_count)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200558 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
559
560 return ret;
561}
562
Daniel Draked9430a32007-07-27 15:43:24 +0200563void ieee80211_erp_info_change_notify(struct net_device *dev, u8 changes)
564{
565 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
566 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
567 if (local->ops->erp_ie_changed)
568 local->ops->erp_ie_changed(local_to_hw(local), changes,
Jiri Slaby13262ff2007-08-28 17:01:54 -0400569 !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION),
570 !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE));
Daniel Draked9430a32007-07-27 15:43:24 +0200571}
572
573void ieee80211_reset_erp_info(struct net_device *dev)
574{
575 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
576
Jiri Slaby13262ff2007-08-28 17:01:54 -0400577 sdata->flags &= ~(IEEE80211_SDATA_USE_PROTECTION |
578 IEEE80211_SDATA_SHORT_PREAMBLE);
Daniel Draked9430a32007-07-27 15:43:24 +0200579 ieee80211_erp_info_change_notify(dev,
580 IEEE80211_ERP_CHANGE_PROTECTION |
581 IEEE80211_ERP_CHANGE_PREAMBLE);
582}
583
Jiri Bencf0706e82007-05-05 11:45:53 -0700584void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
585 struct sk_buff *skb,
586 struct ieee80211_tx_status *status)
587{
588 struct ieee80211_local *local = hw_to_local(hw);
589 struct ieee80211_tx_status *saved;
590 int tmp;
591
592 skb->dev = local->mdev;
593 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
594 if (unlikely(!saved)) {
595 if (net_ratelimit())
596 printk(KERN_WARNING "%s: Not enough memory, "
597 "dropping tx status", skb->dev->name);
598 /* should be dev_kfree_skb_irq, but due to this function being
599 * named _irqsafe instead of just _irq we can't be sure that
600 * people won't call it from non-irq contexts */
601 dev_kfree_skb_any(skb);
602 return;
603 }
604 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
605 /* copy pointer to saved status into skb->cb for use by tasklet */
606 memcpy(skb->cb, &saved, sizeof(saved));
607
608 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
609 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
610 &local->skb_queue : &local->skb_queue_unreliable, skb);
611 tmp = skb_queue_len(&local->skb_queue) +
612 skb_queue_len(&local->skb_queue_unreliable);
613 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
614 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
615 memcpy(&saved, skb->cb, sizeof(saved));
616 kfree(saved);
617 dev_kfree_skb_irq(skb);
618 tmp--;
619 I802_DEBUG_INC(local->tx_status_drop);
620 }
621 tasklet_schedule(&local->tasklet);
622}
623EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
624
625static void ieee80211_tasklet_handler(unsigned long data)
626{
627 struct ieee80211_local *local = (struct ieee80211_local *) data;
628 struct sk_buff *skb;
629 struct ieee80211_rx_status rx_status;
630 struct ieee80211_tx_status *tx_status;
631
632 while ((skb = skb_dequeue(&local->skb_queue)) ||
633 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
634 switch (skb->pkt_type) {
635 case IEEE80211_RX_MSG:
636 /* status is in skb->cb */
637 memcpy(&rx_status, skb->cb, sizeof(rx_status));
638 /* Clear skb->type in order to not confuse kernel
639 * netstack. */
640 skb->pkt_type = 0;
641 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
642 break;
643 case IEEE80211_TX_STATUS_MSG:
644 /* get pointer to saved status out of skb->cb */
645 memcpy(&tx_status, skb->cb, sizeof(tx_status));
646 skb->pkt_type = 0;
647 ieee80211_tx_status(local_to_hw(local),
648 skb, tx_status);
649 kfree(tx_status);
650 break;
651 default: /* should never get here! */
652 printk(KERN_ERR "%s: Unknown message type (%d)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400653 wiphy_name(local->hw.wiphy), skb->pkt_type);
Jiri Bencf0706e82007-05-05 11:45:53 -0700654 dev_kfree_skb(skb);
655 break;
656 }
657 }
658}
659
Jiri Bencf0706e82007-05-05 11:45:53 -0700660/* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
661 * make a prepared TX frame (one that has been given to hw) to look like brand
662 * new IEEE 802.11 frame that is ready to go through TX processing again.
663 * Also, tx_packet_data in cb is restored from tx_control. */
664static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
665 struct ieee80211_key *key,
666 struct sk_buff *skb,
667 struct ieee80211_tx_control *control)
668{
669 int hdrlen, iv_len, mic_len;
670 struct ieee80211_tx_packet_data *pkt_data;
671
672 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
673 pkt_data->ifindex = control->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400674 pkt_data->flags = 0;
675 if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
676 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
677 if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
678 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
679 if (control->flags & IEEE80211_TXCTL_REQUEUE)
680 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700681 pkt_data->queue = control->queue;
682
683 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
684
685 if (!key)
686 goto no_key;
687
Johannes Berg8f20fc22007-08-28 17:01:54 -0400688 switch (key->conf.alg) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700689 case ALG_WEP:
690 iv_len = WEP_IV_LEN;
691 mic_len = WEP_ICV_LEN;
692 break;
693 case ALG_TKIP:
694 iv_len = TKIP_IV_LEN;
695 mic_len = TKIP_ICV_LEN;
696 break;
697 case ALG_CCMP:
698 iv_len = CCMP_HDR_LEN;
699 mic_len = CCMP_MIC_LEN;
700 break;
701 default:
702 goto no_key;
703 }
704
Johannes Berg8f20fc22007-08-28 17:01:54 -0400705 if (skb->len >= mic_len &&
Johannes Berg11a843b2007-08-28 17:01:55 -0400706 !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Jiri Bencf0706e82007-05-05 11:45:53 -0700707 skb_trim(skb, skb->len - mic_len);
708 if (skb->len >= iv_len && skb->len > hdrlen) {
709 memmove(skb->data + iv_len, skb->data, hdrlen);
710 skb_pull(skb, iv_len);
711 }
712
713no_key:
714 {
715 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
716 u16 fc = le16_to_cpu(hdr->frame_control);
717 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
718 fc &= ~IEEE80211_STYPE_QOS_DATA;
719 hdr->frame_control = cpu_to_le16(fc);
720 memmove(skb->data + 2, skb->data, hdrlen - 2);
721 skb_pull(skb, 2);
722 }
723 }
724}
725
Jiri Bencf0706e82007-05-05 11:45:53 -0700726void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
727 struct ieee80211_tx_status *status)
728{
729 struct sk_buff *skb2;
730 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
731 struct ieee80211_local *local = hw_to_local(hw);
732 u16 frag, type;
Johannes Bergb306f452007-07-10 19:32:08 +0200733 struct ieee80211_tx_status_rtap_hdr *rthdr;
734 struct ieee80211_sub_if_data *sdata;
735 int monitors;
Jiri Bencf0706e82007-05-05 11:45:53 -0700736
737 if (!status) {
738 printk(KERN_ERR
739 "%s: ieee80211_tx_status called with NULL status\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400740 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -0700741 dev_kfree_skb(skb);
742 return;
743 }
744
745 if (status->excessive_retries) {
746 struct sta_info *sta;
747 sta = sta_info_get(local, hdr->addr1);
748 if (sta) {
749 if (sta->flags & WLAN_STA_PS) {
750 /* The STA is in power save mode, so assume
751 * that this TX packet failed because of that.
752 */
753 status->excessive_retries = 0;
754 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
755 }
756 sta_info_put(sta);
757 }
758 }
759
760 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
761 struct sta_info *sta;
762 sta = sta_info_get(local, hdr->addr1);
763 if (sta) {
764 sta->tx_filtered_count++;
765
766 /* Clear the TX filter mask for this STA when sending
767 * the next packet. If the STA went to power save mode,
768 * this will happen when it is waking up for the next
769 * time. */
770 sta->clear_dst_mask = 1;
771
772 /* TODO: Is the WLAN_STA_PS flag always set here or is
773 * the race between RX and TX status causing some
774 * packets to be filtered out before 80211.o gets an
775 * update for PS status? This seems to be the case, so
776 * no changes are likely to be needed. */
777 if (sta->flags & WLAN_STA_PS &&
778 skb_queue_len(&sta->tx_filtered) <
779 STA_MAX_TX_BUFFER) {
780 ieee80211_remove_tx_extra(local, sta->key,
781 skb,
782 &status->control);
783 skb_queue_tail(&sta->tx_filtered, skb);
784 } else if (!(sta->flags & WLAN_STA_PS) &&
785 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
786 /* Software retry the packet once */
787 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
788 ieee80211_remove_tx_extra(local, sta->key,
789 skb,
790 &status->control);
791 dev_queue_xmit(skb);
792 } else {
793 if (net_ratelimit()) {
794 printk(KERN_DEBUG "%s: dropped TX "
795 "filtered frame queue_len=%d "
796 "PS=%d @%lu\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400797 wiphy_name(local->hw.wiphy),
Jiri Bencf0706e82007-05-05 11:45:53 -0700798 skb_queue_len(
799 &sta->tx_filtered),
800 !!(sta->flags & WLAN_STA_PS),
801 jiffies);
802 }
803 dev_kfree_skb(skb);
804 }
805 sta_info_put(sta);
806 return;
807 }
808 } else {
809 /* FIXME: STUPID to call this with both local and local->mdev */
810 rate_control_tx_status(local, local->mdev, skb, status);
811 }
812
813 ieee80211_led_tx(local, 0);
814
815 /* SNMP counters
816 * Fragments are passed to low-level drivers as separate skbs, so these
817 * are actually fragments, not frames. Update frame counters only for
818 * the first fragment of the frame. */
819
820 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
821 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
822
823 if (status->flags & IEEE80211_TX_STATUS_ACK) {
824 if (frag == 0) {
825 local->dot11TransmittedFrameCount++;
826 if (is_multicast_ether_addr(hdr->addr1))
827 local->dot11MulticastTransmittedFrameCount++;
828 if (status->retry_count > 0)
829 local->dot11RetryCount++;
830 if (status->retry_count > 1)
831 local->dot11MultipleRetryCount++;
832 }
833
834 /* This counter shall be incremented for an acknowledged MPDU
835 * with an individual address in the address 1 field or an MPDU
836 * with a multicast address in the address 1 field of type Data
837 * or Management. */
838 if (!is_multicast_ether_addr(hdr->addr1) ||
839 type == IEEE80211_FTYPE_DATA ||
840 type == IEEE80211_FTYPE_MGMT)
841 local->dot11TransmittedFragmentCount++;
842 } else {
843 if (frag == 0)
844 local->dot11FailedCount++;
845 }
846
Johannes Bergb306f452007-07-10 19:32:08 +0200847 /* this was a transmitted frame, but now we want to reuse it */
848 skb_orphan(skb);
849
Johannes Bergb306f452007-07-10 19:32:08 +0200850 if (!local->monitors) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700851 dev_kfree_skb(skb);
852 return;
853 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700854
Johannes Bergb306f452007-07-10 19:32:08 +0200855 /* send frame to monitor interfaces now */
856
857 if (skb_headroom(skb) < sizeof(*rthdr)) {
858 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
859 dev_kfree_skb(skb);
860 return;
861 }
862
863 rthdr = (struct ieee80211_tx_status_rtap_hdr*)
864 skb_push(skb, sizeof(*rthdr));
865
866 memset(rthdr, 0, sizeof(*rthdr));
867 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
868 rthdr->hdr.it_present =
869 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
870 (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
871
872 if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
873 !is_multicast_ether_addr(hdr->addr1))
874 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
875
876 if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
877 (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
878 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
879 else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
880 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
881
882 rthdr->data_retries = status->retry_count;
883
Johannes Berg79010422007-09-18 17:29:21 -0400884 rcu_read_lock();
Johannes Bergb306f452007-07-10 19:32:08 +0200885 monitors = local->monitors;
Johannes Berg79010422007-09-18 17:29:21 -0400886 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Bergb306f452007-07-10 19:32:08 +0200887 /*
888 * Using the monitors counter is possibly racy, but
889 * if the value is wrong we simply either clone the skb
890 * once too much or forget sending it to one monitor iface
891 * The latter case isn't nice but fixing the race is much
892 * more complicated.
893 */
894 if (!monitors || !skb)
895 goto out;
896
897 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
898 if (!netif_running(sdata->dev))
899 continue;
900 monitors--;
901 if (monitors)
Johannes Berg79010422007-09-18 17:29:21 -0400902 skb2 = skb_clone(skb, GFP_ATOMIC);
Johannes Bergb306f452007-07-10 19:32:08 +0200903 else
904 skb2 = NULL;
905 skb->dev = sdata->dev;
906 /* XXX: is this sufficient for BPF? */
907 skb_set_mac_header(skb, 0);
908 skb->ip_summed = CHECKSUM_UNNECESSARY;
909 skb->pkt_type = PACKET_OTHERHOST;
910 skb->protocol = htons(ETH_P_802_2);
911 memset(skb->cb, 0, sizeof(skb->cb));
912 netif_rx(skb);
913 skb = skb2;
Johannes Bergb306f452007-07-10 19:32:08 +0200914 }
915 }
916 out:
Johannes Berg79010422007-09-18 17:29:21 -0400917 rcu_read_unlock();
Johannes Bergb306f452007-07-10 19:32:08 +0200918 if (skb)
919 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700920}
921EXPORT_SYMBOL(ieee80211_tx_status);
922
Jiri Bencf0706e82007-05-05 11:45:53 -0700923struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
924 const struct ieee80211_ops *ops)
925{
926 struct net_device *mdev;
927 struct ieee80211_local *local;
928 struct ieee80211_sub_if_data *sdata;
929 int priv_size;
930 struct wiphy *wiphy;
931
932 /* Ensure 32-byte alignment of our private data and hw private data.
933 * We use the wiphy priv data for both our ieee80211_local and for
934 * the driver's private data
935 *
936 * In memory it'll be like this:
937 *
938 * +-------------------------+
939 * | struct wiphy |
940 * +-------------------------+
941 * | struct ieee80211_local |
942 * +-------------------------+
943 * | driver's private data |
944 * +-------------------------+
945 *
946 */
947 priv_size = ((sizeof(struct ieee80211_local) +
948 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
949 priv_data_len;
950
951 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
952
953 if (!wiphy)
954 return NULL;
955
956 wiphy->privid = mac80211_wiphy_privid;
957
958 local = wiphy_priv(wiphy);
959 local->hw.wiphy = wiphy;
960
961 local->hw.priv = (char *)local +
962 ((sizeof(struct ieee80211_local) +
963 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
964
Johannes Berg4480f15c2007-07-10 19:32:10 +0200965 BUG_ON(!ops->tx);
Johannes Berg4150c572007-09-17 01:29:23 -0400966 BUG_ON(!ops->start);
967 BUG_ON(!ops->stop);
Johannes Berg4480f15c2007-07-10 19:32:10 +0200968 BUG_ON(!ops->config);
969 BUG_ON(!ops->add_interface);
Johannes Berg4150c572007-09-17 01:29:23 -0400970 BUG_ON(!ops->remove_interface);
971 BUG_ON(!ops->configure_filter);
Jiri Bencf0706e82007-05-05 11:45:53 -0700972 local->ops = ops;
973
974 /* for now, mdev needs sub_if_data :/ */
975 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
976 "wmaster%d", ether_setup);
977 if (!mdev) {
978 wiphy_free(wiphy);
979 return NULL;
980 }
981
982 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
983 mdev->ieee80211_ptr = &sdata->wdev;
984 sdata->wdev.wiphy = wiphy;
985
986 local->hw.queues = 1; /* default */
987
988 local->mdev = mdev;
989 local->rx_pre_handlers = ieee80211_rx_pre_handlers;
990 local->rx_handlers = ieee80211_rx_handlers;
991 local->tx_handlers = ieee80211_tx_handlers;
992
993 local->bridge_packets = 1;
994
995 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
996 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
997 local->short_retry_limit = 7;
998 local->long_retry_limit = 4;
999 local->hw.conf.radio_enabled = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07001000
Johannes Bergb708e612007-09-14 11:10:25 -04001001 local->enabled_modes = ~0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001002
1003 INIT_LIST_HEAD(&local->modes_list);
1004
Johannes Berg79010422007-09-18 17:29:21 -04001005 INIT_LIST_HEAD(&local->interfaces);
Jiri Bencf0706e82007-05-05 11:45:53 -07001006
1007 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
Jiri Bencf0706e82007-05-05 11:45:53 -07001008 ieee80211_rx_bss_list_init(mdev);
1009
1010 sta_info_init(local);
1011
1012 mdev->hard_start_xmit = ieee80211_master_start_xmit;
1013 mdev->open = ieee80211_master_open;
1014 mdev->stop = ieee80211_master_stop;
1015 mdev->type = ARPHRD_IEEE80211;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001016 mdev->header_ops = &ieee80211_header_ops;
Johannes Berg4150c572007-09-17 01:29:23 -04001017 mdev->set_multicast_list = ieee80211_master_set_multicast_list;
Jiri Bencf0706e82007-05-05 11:45:53 -07001018
1019 sdata->type = IEEE80211_IF_TYPE_AP;
1020 sdata->dev = mdev;
1021 sdata->local = local;
1022 sdata->u.ap.force_unicast_rateidx = -1;
1023 sdata->u.ap.max_ratectrl_rateidx = -1;
1024 ieee80211_if_sdata_init(sdata);
Johannes Berg79010422007-09-18 17:29:21 -04001025 /* no RCU needed since we're still during init phase */
1026 list_add_tail(&sdata->list, &local->interfaces);
Jiri Bencf0706e82007-05-05 11:45:53 -07001027
1028 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1029 (unsigned long)local);
1030 tasklet_disable(&local->tx_pending_tasklet);
1031
1032 tasklet_init(&local->tasklet,
1033 ieee80211_tasklet_handler,
1034 (unsigned long) local);
1035 tasklet_disable(&local->tasklet);
1036
1037 skb_queue_head_init(&local->skb_queue);
1038 skb_queue_head_init(&local->skb_queue_unreliable);
1039
1040 return local_to_hw(local);
1041}
1042EXPORT_SYMBOL(ieee80211_alloc_hw);
1043
1044int ieee80211_register_hw(struct ieee80211_hw *hw)
1045{
1046 struct ieee80211_local *local = hw_to_local(hw);
1047 const char *name;
1048 int result;
1049
1050 result = wiphy_register(local->hw.wiphy);
1051 if (result < 0)
1052 return result;
1053
1054 name = wiphy_dev(local->hw.wiphy)->driver->name;
1055 local->hw.workqueue = create_singlethread_workqueue(name);
1056 if (!local->hw.workqueue) {
1057 result = -ENOMEM;
1058 goto fail_workqueue;
1059 }
1060
Johannes Bergb306f452007-07-10 19:32:08 +02001061 /*
1062 * The hardware needs headroom for sending the frame,
1063 * and we need some headroom for passing the frame to monitor
1064 * interfaces, but never both at the same time.
1065 */
Jiri Benc33ccad32007-07-18 17:10:44 +02001066 local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1067 sizeof(struct ieee80211_tx_status_rtap_hdr));
Johannes Bergb306f452007-07-10 19:32:08 +02001068
Jiri Bence9f207f2007-05-05 11:46:38 -07001069 debugfs_hw_add(local);
1070
Jiri Bencf0706e82007-05-05 11:45:53 -07001071 local->hw.conf.beacon_int = 1000;
1072
1073 local->wstats_flags |= local->hw.max_rssi ?
1074 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1075 local->wstats_flags |= local->hw.max_signal ?
1076 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1077 local->wstats_flags |= local->hw.max_noise ?
1078 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1079 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1080 local->wstats_flags |= IW_QUAL_DBM;
1081
1082 result = sta_info_start(local);
1083 if (result < 0)
1084 goto fail_sta_info;
1085
1086 rtnl_lock();
1087 result = dev_alloc_name(local->mdev, local->mdev->name);
1088 if (result < 0)
1089 goto fail_dev;
1090
1091 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1092 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1093
1094 result = register_netdevice(local->mdev);
1095 if (result < 0)
1096 goto fail_dev;
1097
Jiri Bence9f207f2007-05-05 11:46:38 -07001098 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Johannes Berg5b2812e2007-09-26 14:27:23 +02001099 ieee80211_if_set_type(local->mdev, IEEE80211_IF_TYPE_AP);
Jiri Bence9f207f2007-05-05 11:46:38 -07001100
Johannes Berg830f9032007-10-28 14:51:05 +01001101 result = ieee80211_init_rate_ctrl_alg(local,
1102 hw->rate_control_algorithm);
Jiri Bencf0706e82007-05-05 11:45:53 -07001103 if (result < 0) {
1104 printk(KERN_DEBUG "%s: Failed to initialize rate control "
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001105 "algorithm\n", wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001106 goto fail_rate;
1107 }
1108
1109 result = ieee80211_wep_init(local);
1110
1111 if (result < 0) {
1112 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001113 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001114 goto fail_wep;
1115 }
1116
1117 ieee80211_install_qdisc(local->mdev);
1118
1119 /* add one default STA interface */
1120 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
1121 IEEE80211_IF_TYPE_STA);
1122 if (result)
1123 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001124 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001125
1126 local->reg_state = IEEE80211_DEV_REGISTERED;
1127 rtnl_unlock();
1128
1129 ieee80211_led_init(local);
1130
1131 return 0;
1132
1133fail_wep:
1134 rate_control_deinitialize(local);
1135fail_rate:
Jiri Bence9f207f2007-05-05 11:46:38 -07001136 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001137 unregister_netdevice(local->mdev);
1138fail_dev:
1139 rtnl_unlock();
1140 sta_info_stop(local);
1141fail_sta_info:
Jiri Bence9f207f2007-05-05 11:46:38 -07001142 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001143 destroy_workqueue(local->hw.workqueue);
1144fail_workqueue:
1145 wiphy_unregister(local->hw.wiphy);
1146 return result;
1147}
1148EXPORT_SYMBOL(ieee80211_register_hw);
1149
1150int ieee80211_register_hwmode(struct ieee80211_hw *hw,
1151 struct ieee80211_hw_mode *mode)
1152{
1153 struct ieee80211_local *local = hw_to_local(hw);
1154 struct ieee80211_rate *rate;
1155 int i;
1156
1157 INIT_LIST_HEAD(&mode->list);
1158 list_add_tail(&mode->list, &local->modes_list);
1159
1160 local->hw_modes |= (1 << mode->mode);
1161 for (i = 0; i < mode->num_rates; i++) {
1162 rate = &(mode->rates[i]);
1163 rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
1164 }
1165 ieee80211_prepare_rates(local, mode);
1166
1167 if (!local->oper_hw_mode) {
1168 /* Default to this mode */
1169 local->hw.conf.phymode = mode->mode;
1170 local->oper_hw_mode = local->scan_hw_mode = mode;
1171 local->oper_channel = local->scan_channel = &mode->channels[0];
1172 local->hw.conf.mode = local->oper_hw_mode;
1173 local->hw.conf.chan = local->oper_channel;
1174 }
1175
1176 if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
Daniel Drakefd8bacc2007-06-09 19:07:14 +01001177 ieee80211_set_default_regdomain(mode);
Jiri Bencf0706e82007-05-05 11:45:53 -07001178
1179 return 0;
1180}
1181EXPORT_SYMBOL(ieee80211_register_hwmode);
1182
1183void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1184{
1185 struct ieee80211_local *local = hw_to_local(hw);
1186 struct ieee80211_sub_if_data *sdata, *tmp;
Jiri Bencf0706e82007-05-05 11:45:53 -07001187 int i;
1188
1189 tasklet_kill(&local->tx_pending_tasklet);
1190 tasklet_kill(&local->tasklet);
1191
1192 rtnl_lock();
1193
1194 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1195
1196 local->reg_state = IEEE80211_DEV_UNREGISTERED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001197
Johannes Berg79010422007-09-18 17:29:21 -04001198 /*
1199 * At this point, interface list manipulations are fine
1200 * because the driver cannot be handing us frames any
1201 * more and the tasklet is killed.
1202 */
Johannes Berg5b2812e2007-09-26 14:27:23 +02001203
1204 /*
1205 * First, we remove all non-master interfaces. Do this because they
1206 * may have bss pointer dependency on the master, and when we free
1207 * the master these would be freed as well, breaking our list
1208 * iteration completely.
1209 */
1210 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
1211 if (sdata->dev == local->mdev)
1212 continue;
1213 list_del(&sdata->list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001214 __ieee80211_if_del(local, sdata);
Johannes Berg5b2812e2007-09-26 14:27:23 +02001215 }
1216
1217 /* then, finally, remove the master interface */
1218 __ieee80211_if_del(local, IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001219
1220 rtnl_unlock();
1221
Jiri Bencf0706e82007-05-05 11:45:53 -07001222 ieee80211_rx_bss_list_deinit(local->mdev);
1223 ieee80211_clear_tx_pending(local);
1224 sta_info_stop(local);
1225 rate_control_deinitialize(local);
Jiri Bence9f207f2007-05-05 11:46:38 -07001226 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001227
1228 for (i = 0; i < NUM_IEEE80211_MODES; i++) {
1229 kfree(local->supp_rates[i]);
1230 kfree(local->basic_rates[i]);
1231 }
1232
1233 if (skb_queue_len(&local->skb_queue)
1234 || skb_queue_len(&local->skb_queue_unreliable))
1235 printk(KERN_WARNING "%s: skb_queue not empty\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001236 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001237 skb_queue_purge(&local->skb_queue);
1238 skb_queue_purge(&local->skb_queue_unreliable);
1239
1240 destroy_workqueue(local->hw.workqueue);
1241 wiphy_unregister(local->hw.wiphy);
1242 ieee80211_wep_free(local);
1243 ieee80211_led_exit(local);
1244}
1245EXPORT_SYMBOL(ieee80211_unregister_hw);
1246
1247void ieee80211_free_hw(struct ieee80211_hw *hw)
1248{
1249 struct ieee80211_local *local = hw_to_local(hw);
1250
1251 ieee80211_if_free(local->mdev);
1252 wiphy_free(local->hw.wiphy);
1253}
1254EXPORT_SYMBOL(ieee80211_free_hw);
1255
Jiri Bencf0706e82007-05-05 11:45:53 -07001256static int __init ieee80211_init(void)
1257{
1258 struct sk_buff *skb;
1259 int ret;
1260
1261 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1262
Johannes Bergac71c692007-10-28 14:17:44 +01001263#ifdef CONFIG_MAC80211_RCSIMPLE
1264 ret = ieee80211_rate_control_register(&mac80211_rcsimple);
1265 if (ret)
1266 return ret;
1267#endif
1268
Jiri Bencf0706e82007-05-05 11:45:53 -07001269 ret = ieee80211_wme_register();
1270 if (ret) {
Johannes Bergac71c692007-10-28 14:17:44 +01001271#ifdef CONFIG_MAC80211_RCSIMPLE
1272 ieee80211_rate_control_unregister(&mac80211_rcsimple);
1273#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07001274 printk(KERN_DEBUG "ieee80211_init: failed to "
1275 "initialize WME (err=%d)\n", ret);
1276 return ret;
1277 }
1278
Jiri Bence9f207f2007-05-05 11:46:38 -07001279 ieee80211_debugfs_netdev_init();
Daniel Drakefd8bacc2007-06-09 19:07:14 +01001280 ieee80211_regdomain_init();
Jiri Bence9f207f2007-05-05 11:46:38 -07001281
Jiri Bencf0706e82007-05-05 11:45:53 -07001282 return 0;
1283}
1284
Jiri Bencf0706e82007-05-05 11:45:53 -07001285static void __exit ieee80211_exit(void)
1286{
Johannes Bergac71c692007-10-28 14:17:44 +01001287#ifdef CONFIG_MAC80211_RCSIMPLE
1288 ieee80211_rate_control_unregister(&mac80211_rcsimple);
1289#endif
1290
Jiri Bencf0706e82007-05-05 11:45:53 -07001291 ieee80211_wme_unregister();
Jiri Bence9f207f2007-05-05 11:46:38 -07001292 ieee80211_debugfs_netdev_exit();
Jiri Bencf0706e82007-05-05 11:45:53 -07001293}
1294
1295
Johannes Bergca9938f2007-09-11 12:50:32 +02001296subsys_initcall(ieee80211_init);
Jiri Bencf0706e82007-05-05 11:45:53 -07001297module_exit(ieee80211_exit);
1298
1299MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1300MODULE_LICENSE("GPL");