blob: 52ba6ef906853ec1b431c7c648bdc964b9624248 [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;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200219 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200220
Johannes Berg4150c572007-09-17 01:29:23 -0400221 switch (sdata->type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400222 case IEEE80211_IF_TYPE_VLAN:
223 list_add(&sdata->u.vlan.list, &sdata->u.vlan.ap->u.ap.vlans);
224 /* no need to tell driver */
225 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400226 case IEEE80211_IF_TYPE_MNTR:
227 /* must be before the call to ieee80211_configure_filter */
Johannes Bergb2c258f2007-07-27 15:43:23 +0200228 local->monitors++;
Johannes Berg4150c572007-09-17 01:29:23 -0400229 if (local->monitors == 1) {
230 netif_tx_lock_bh(local->mdev);
231 ieee80211_configure_filter(local);
232 netif_tx_unlock_bh(local->mdev);
233
234 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
235 ieee80211_hw_config(local);
236 }
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
270 local->open_count++;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200271
272 netif_start_queue(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400273
Johannes Bergb2c258f2007-07-27 15:43:23 +0200274 return 0;
275}
276
Johannes Berg4150c572007-09-17 01:29:23 -0400277static int ieee80211_stop(struct net_device *dev)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200278{
Johannes Berg4150c572007-09-17 01:29:23 -0400279 struct ieee80211_sub_if_data *sdata;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200280 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg4150c572007-09-17 01:29:23 -0400281 struct ieee80211_if_init_conf conf;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200282
Johannes Berg4150c572007-09-17 01:29:23 -0400283 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
284
285 netif_stop_queue(dev);
286
287 dev_mc_unsync(local->mdev, dev);
288
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400289 /* down all dependent devices, that is VLANs */
290 if (sdata->type == IEEE80211_IF_TYPE_AP) {
291 struct ieee80211_sub_if_data *vlan, *tmp;
292
293 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
294 u.vlan.list)
295 dev_close(vlan->dev);
296 WARN_ON(!list_empty(&sdata->u.ap.vlans));
297 }
298
Johannes Berg4150c572007-09-17 01:29:23 -0400299 local->open_count--;
300
Johannes Bergb2c258f2007-07-27 15:43:23 +0200301 switch (sdata->type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400302 case IEEE80211_IF_TYPE_VLAN:
303 list_del(&sdata->u.vlan.list);
304 sdata->u.vlan.ap = NULL;
305 /* no need to tell driver */
306 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400307 case IEEE80211_IF_TYPE_MNTR:
308 local->monitors--;
309 if (local->monitors == 0) {
310 netif_tx_lock_bh(local->mdev);
311 ieee80211_configure_filter(local);
312 netif_tx_unlock_bh(local->mdev);
313
314 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
315 ieee80211_hw_config(local);
316 }
317 break;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200318 case IEEE80211_IF_TYPE_STA:
319 case IEEE80211_IF_TYPE_IBSS:
320 sdata->u.sta.state = IEEE80211_DISABLED;
321 del_timer_sync(&sdata->u.sta.timer);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400322 /*
Johannes Berg79010422007-09-18 17:29:21 -0400323 * When we get here, the interface is marked down.
324 * Call synchronize_rcu() to wait for the RX path
325 * should it be using the interface and enqueuing
326 * frames at this very time on another CPU.
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400327 */
Johannes Berg79010422007-09-18 17:29:21 -0400328 synchronize_rcu();
Johannes Bergb2c258f2007-07-27 15:43:23 +0200329 skb_queue_purge(&sdata->u.sta.skb_queue);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400330
Johannes Bergb2c258f2007-07-27 15:43:23 +0200331 if (!local->ops->hw_scan &&
332 local->scan_dev == sdata->dev) {
333 local->sta_scanning = 0;
334 cancel_delayed_work(&local->scan_work);
335 }
336 flush_workqueue(local->hw.workqueue);
Johannes Berg4150c572007-09-17 01:29:23 -0400337 /* fall through */
338 default:
Johannes Bergb2c258f2007-07-27 15:43:23 +0200339 conf.if_id = dev->ifindex;
340 conf.type = sdata->type;
341 conf.mac_addr = dev->dev_addr;
Johannes Berg4150c572007-09-17 01:29:23 -0400342 /* disable all keys for as long as this netdev is down */
343 ieee80211_disable_keys(sdata);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200344 local->ops->remove_interface(local_to_hw(local), &conf);
345 }
346
Johannes Berg4150c572007-09-17 01:29:23 -0400347 if (local->open_count == 0) {
348 if (netif_running(local->mdev))
349 dev_close(local->mdev);
350
Johannes Berg4150c572007-09-17 01:29:23 -0400351 if (local->ops->stop)
352 local->ops->stop(local_to_hw(local));
353
354 tasklet_disable(&local->tx_pending_tasklet);
355 tasklet_disable(&local->tasklet);
356 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200357
358 return 0;
359}
360
Johannes Bergb2c258f2007-07-27 15:43:23 +0200361static void ieee80211_set_multicast_list(struct net_device *dev)
362{
363 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
364 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400365 int allmulti, promisc, sdata_allmulti, sdata_promisc;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200366
Johannes Berg4150c572007-09-17 01:29:23 -0400367 allmulti = !!(dev->flags & IFF_ALLMULTI);
368 promisc = !!(dev->flags & IFF_PROMISC);
369 sdata_allmulti = sdata->flags & IEEE80211_SDATA_ALLMULTI;
370 sdata_promisc = sdata->flags & IEEE80211_SDATA_PROMISC;
371
372 if (allmulti != sdata_allmulti) {
373 if (dev->flags & IFF_ALLMULTI)
Johannes Berg53918992007-09-26 15:19:47 +0200374 atomic_inc(&local->iff_allmultis);
Johannes Berg4150c572007-09-17 01:29:23 -0400375 else
Johannes Berg53918992007-09-26 15:19:47 +0200376 atomic_dec(&local->iff_allmultis);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400377 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200378 }
Johannes Berg4150c572007-09-17 01:29:23 -0400379
380 if (promisc != sdata_promisc) {
381 if (dev->flags & IFF_PROMISC)
Johannes Berg53918992007-09-26 15:19:47 +0200382 atomic_inc(&local->iff_promiscs);
Johannes Berg4150c572007-09-17 01:29:23 -0400383 else
Johannes Berg53918992007-09-26 15:19:47 +0200384 atomic_dec(&local->iff_promiscs);
Jiri Slaby13262ff2007-08-28 17:01:54 -0400385 sdata->flags ^= IEEE80211_SDATA_PROMISC;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200386 }
Johannes Berg4150c572007-09-17 01:29:23 -0400387
388 dev_mc_sync(local->mdev, dev);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200389}
390
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700391static const struct header_ops ieee80211_header_ops = {
392 .create = eth_header,
393 .parse = header_parse_80211,
394 .rebuild = eth_rebuild_header,
395 .cache = eth_header_cache,
396 .cache_update = eth_header_cache_update,
397};
398
Johannes Bergf9d540e2007-09-28 14:02:09 +0200399/* Must not be called for mdev */
Johannes Bergb2c258f2007-07-27 15:43:23 +0200400void ieee80211_if_setup(struct net_device *dev)
401{
402 ether_setup(dev);
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700403 dev->header_ops = &ieee80211_header_ops;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200404 dev->hard_start_xmit = ieee80211_subif_start_xmit;
405 dev->wireless_handlers = &ieee80211_iw_handler_def;
406 dev->set_multicast_list = ieee80211_set_multicast_list;
407 dev->change_mtu = ieee80211_change_mtu;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200408 dev->open = ieee80211_open;
409 dev->stop = ieee80211_stop;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200410 dev->destructor = ieee80211_if_free;
411}
412
413/* WDS specialties */
414
415int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
416{
417 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
418 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
419 struct sta_info *sta;
Joe Perches0795af52007-10-03 17:59:30 -0700420 DECLARE_MAC_BUF(mac);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200421
422 if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
423 return 0;
424
425 /* Create STA entry for the new peer */
426 sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
427 if (!sta)
428 return -ENOMEM;
429 sta_info_put(sta);
430
431 /* Remove STA entry for the old peer */
432 sta = sta_info_get(local, sdata->u.wds.remote_addr);
433 if (sta) {
Michael Wube8755e2007-07-27 15:43:23 +0200434 sta_info_free(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200435 sta_info_put(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200436 } else {
437 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
Joe Perches0795af52007-10-03 17:59:30 -0700438 "peer %s\n",
439 dev->name, print_mac(mac, sdata->u.wds.remote_addr));
Johannes Bergb2c258f2007-07-27 15:43:23 +0200440 }
441
442 /* Update WDS link data */
443 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
444
445 return 0;
446}
447
448/* everything else */
449
Johannes Bergb2c258f2007-07-27 15:43:23 +0200450static int __ieee80211_if_config(struct net_device *dev,
451 struct sk_buff *beacon,
452 struct ieee80211_tx_control *control)
453{
454 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
455 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
456 struct ieee80211_if_conf conf;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200457
458 if (!local->ops->config_interface || !netif_running(dev))
459 return 0;
460
461 memset(&conf, 0, sizeof(conf));
462 conf.type = sdata->type;
463 if (sdata->type == IEEE80211_IF_TYPE_STA ||
464 sdata->type == IEEE80211_IF_TYPE_IBSS) {
Johannes Berg4150c572007-09-17 01:29:23 -0400465 conf.bssid = sdata->u.sta.bssid;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200466 conf.ssid = sdata->u.sta.ssid;
467 conf.ssid_len = sdata->u.sta.ssid_len;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200468 } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
469 conf.ssid = sdata->u.ap.ssid;
470 conf.ssid_len = sdata->u.ap.ssid_len;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200471 conf.beacon = beacon;
472 conf.beacon_control = control;
473 }
474 return local->ops->config_interface(local_to_hw(local),
475 dev->ifindex, &conf);
476}
477
478int ieee80211_if_config(struct net_device *dev)
479{
480 return __ieee80211_if_config(dev, NULL, NULL);
481}
482
483int ieee80211_if_config_beacon(struct net_device *dev)
484{
485 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
486 struct ieee80211_tx_control control;
487 struct sk_buff *skb;
488
489 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
490 return 0;
491 skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
492 if (!skb)
493 return -ENOMEM;
494 return __ieee80211_if_config(dev, skb, &control);
495}
496
497int ieee80211_hw_config(struct ieee80211_local *local)
498{
499 struct ieee80211_hw_mode *mode;
500 struct ieee80211_channel *chan;
501 int ret = 0;
502
503 if (local->sta_scanning) {
504 chan = local->scan_channel;
505 mode = local->scan_hw_mode;
506 } else {
507 chan = local->oper_channel;
508 mode = local->oper_hw_mode;
509 }
510
511 local->hw.conf.channel = chan->chan;
512 local->hw.conf.channel_val = chan->val;
Michael Buesch61609bc2007-09-20 22:06:39 +0200513 if (!local->hw.conf.power_level) {
514 local->hw.conf.power_level = chan->power_level;
515 } else {
516 local->hw.conf.power_level = min(chan->power_level,
517 local->hw.conf.power_level);
518 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200519 local->hw.conf.freq = chan->freq;
520 local->hw.conf.phymode = mode->mode;
521 local->hw.conf.antenna_max = chan->antenna_max;
522 local->hw.conf.chan = chan;
523 local->hw.conf.mode = mode;
524
525#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
526 printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
527 "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
528 local->hw.conf.phymode);
529#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
530
Michael Bueschf7c4dae2007-09-24 18:41:49 +0200531 if (local->open_count)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200532 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
533
534 return ret;
535}
536
Daniel Draked9430a32007-07-27 15:43:24 +0200537void ieee80211_erp_info_change_notify(struct net_device *dev, u8 changes)
538{
539 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
540 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
541 if (local->ops->erp_ie_changed)
542 local->ops->erp_ie_changed(local_to_hw(local), changes,
Jiri Slaby13262ff2007-08-28 17:01:54 -0400543 !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION),
544 !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE));
Daniel Draked9430a32007-07-27 15:43:24 +0200545}
546
547void ieee80211_reset_erp_info(struct net_device *dev)
548{
549 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
550
Jiri Slaby13262ff2007-08-28 17:01:54 -0400551 sdata->flags &= ~(IEEE80211_SDATA_USE_PROTECTION |
552 IEEE80211_SDATA_SHORT_PREAMBLE);
Daniel Draked9430a32007-07-27 15:43:24 +0200553 ieee80211_erp_info_change_notify(dev,
554 IEEE80211_ERP_CHANGE_PROTECTION |
555 IEEE80211_ERP_CHANGE_PREAMBLE);
556}
557
Jiri Bencf0706e82007-05-05 11:45:53 -0700558void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
559 struct sk_buff *skb,
560 struct ieee80211_tx_status *status)
561{
562 struct ieee80211_local *local = hw_to_local(hw);
563 struct ieee80211_tx_status *saved;
564 int tmp;
565
566 skb->dev = local->mdev;
567 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
568 if (unlikely(!saved)) {
569 if (net_ratelimit())
570 printk(KERN_WARNING "%s: Not enough memory, "
571 "dropping tx status", skb->dev->name);
572 /* should be dev_kfree_skb_irq, but due to this function being
573 * named _irqsafe instead of just _irq we can't be sure that
574 * people won't call it from non-irq contexts */
575 dev_kfree_skb_any(skb);
576 return;
577 }
578 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
579 /* copy pointer to saved status into skb->cb for use by tasklet */
580 memcpy(skb->cb, &saved, sizeof(saved));
581
582 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
583 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
584 &local->skb_queue : &local->skb_queue_unreliable, skb);
585 tmp = skb_queue_len(&local->skb_queue) +
586 skb_queue_len(&local->skb_queue_unreliable);
587 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
588 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
589 memcpy(&saved, skb->cb, sizeof(saved));
590 kfree(saved);
591 dev_kfree_skb_irq(skb);
592 tmp--;
593 I802_DEBUG_INC(local->tx_status_drop);
594 }
595 tasklet_schedule(&local->tasklet);
596}
597EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
598
599static void ieee80211_tasklet_handler(unsigned long data)
600{
601 struct ieee80211_local *local = (struct ieee80211_local *) data;
602 struct sk_buff *skb;
603 struct ieee80211_rx_status rx_status;
604 struct ieee80211_tx_status *tx_status;
605
606 while ((skb = skb_dequeue(&local->skb_queue)) ||
607 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
608 switch (skb->pkt_type) {
609 case IEEE80211_RX_MSG:
610 /* status is in skb->cb */
611 memcpy(&rx_status, skb->cb, sizeof(rx_status));
612 /* Clear skb->type in order to not confuse kernel
613 * netstack. */
614 skb->pkt_type = 0;
615 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
616 break;
617 case IEEE80211_TX_STATUS_MSG:
618 /* get pointer to saved status out of skb->cb */
619 memcpy(&tx_status, skb->cb, sizeof(tx_status));
620 skb->pkt_type = 0;
621 ieee80211_tx_status(local_to_hw(local),
622 skb, tx_status);
623 kfree(tx_status);
624 break;
625 default: /* should never get here! */
626 printk(KERN_ERR "%s: Unknown message type (%d)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400627 wiphy_name(local->hw.wiphy), skb->pkt_type);
Jiri Bencf0706e82007-05-05 11:45:53 -0700628 dev_kfree_skb(skb);
629 break;
630 }
631 }
632}
633
Jiri Bencf0706e82007-05-05 11:45:53 -0700634/* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
635 * make a prepared TX frame (one that has been given to hw) to look like brand
636 * new IEEE 802.11 frame that is ready to go through TX processing again.
637 * Also, tx_packet_data in cb is restored from tx_control. */
638static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
639 struct ieee80211_key *key,
640 struct sk_buff *skb,
641 struct ieee80211_tx_control *control)
642{
643 int hdrlen, iv_len, mic_len;
644 struct ieee80211_tx_packet_data *pkt_data;
645
646 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
647 pkt_data->ifindex = control->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400648 pkt_data->flags = 0;
649 if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
650 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
651 if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
652 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
653 if (control->flags & IEEE80211_TXCTL_REQUEUE)
654 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700655 pkt_data->queue = control->queue;
656
657 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
658
659 if (!key)
660 goto no_key;
661
Johannes Berg8f20fc22007-08-28 17:01:54 -0400662 switch (key->conf.alg) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700663 case ALG_WEP:
664 iv_len = WEP_IV_LEN;
665 mic_len = WEP_ICV_LEN;
666 break;
667 case ALG_TKIP:
668 iv_len = TKIP_IV_LEN;
669 mic_len = TKIP_ICV_LEN;
670 break;
671 case ALG_CCMP:
672 iv_len = CCMP_HDR_LEN;
673 mic_len = CCMP_MIC_LEN;
674 break;
675 default:
676 goto no_key;
677 }
678
Johannes Berg8f20fc22007-08-28 17:01:54 -0400679 if (skb->len >= mic_len &&
Johannes Berg11a843b2007-08-28 17:01:55 -0400680 !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Jiri Bencf0706e82007-05-05 11:45:53 -0700681 skb_trim(skb, skb->len - mic_len);
682 if (skb->len >= iv_len && skb->len > hdrlen) {
683 memmove(skb->data + iv_len, skb->data, hdrlen);
684 skb_pull(skb, iv_len);
685 }
686
687no_key:
688 {
689 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
690 u16 fc = le16_to_cpu(hdr->frame_control);
691 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
692 fc &= ~IEEE80211_STYPE_QOS_DATA;
693 hdr->frame_control = cpu_to_le16(fc);
694 memmove(skb->data + 2, skb->data, hdrlen - 2);
695 skb_pull(skb, 2);
696 }
697 }
698}
699
Jiri Bencf0706e82007-05-05 11:45:53 -0700700void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
701 struct ieee80211_tx_status *status)
702{
703 struct sk_buff *skb2;
704 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
705 struct ieee80211_local *local = hw_to_local(hw);
706 u16 frag, type;
Johannes Bergb306f452007-07-10 19:32:08 +0200707 struct ieee80211_tx_status_rtap_hdr *rthdr;
708 struct ieee80211_sub_if_data *sdata;
709 int monitors;
Jiri Bencf0706e82007-05-05 11:45:53 -0700710
711 if (!status) {
712 printk(KERN_ERR
713 "%s: ieee80211_tx_status called with NULL status\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400714 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -0700715 dev_kfree_skb(skb);
716 return;
717 }
718
719 if (status->excessive_retries) {
720 struct sta_info *sta;
721 sta = sta_info_get(local, hdr->addr1);
722 if (sta) {
723 if (sta->flags & WLAN_STA_PS) {
724 /* The STA is in power save mode, so assume
725 * that this TX packet failed because of that.
726 */
727 status->excessive_retries = 0;
728 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
729 }
730 sta_info_put(sta);
731 }
732 }
733
734 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
735 struct sta_info *sta;
736 sta = sta_info_get(local, hdr->addr1);
737 if (sta) {
738 sta->tx_filtered_count++;
739
740 /* Clear the TX filter mask for this STA when sending
741 * the next packet. If the STA went to power save mode,
742 * this will happen when it is waking up for the next
743 * time. */
744 sta->clear_dst_mask = 1;
745
746 /* TODO: Is the WLAN_STA_PS flag always set here or is
747 * the race between RX and TX status causing some
748 * packets to be filtered out before 80211.o gets an
749 * update for PS status? This seems to be the case, so
750 * no changes are likely to be needed. */
751 if (sta->flags & WLAN_STA_PS &&
752 skb_queue_len(&sta->tx_filtered) <
753 STA_MAX_TX_BUFFER) {
754 ieee80211_remove_tx_extra(local, sta->key,
755 skb,
756 &status->control);
757 skb_queue_tail(&sta->tx_filtered, skb);
758 } else if (!(sta->flags & WLAN_STA_PS) &&
759 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
760 /* Software retry the packet once */
761 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
762 ieee80211_remove_tx_extra(local, sta->key,
763 skb,
764 &status->control);
765 dev_queue_xmit(skb);
766 } else {
767 if (net_ratelimit()) {
768 printk(KERN_DEBUG "%s: dropped TX "
769 "filtered frame queue_len=%d "
770 "PS=%d @%lu\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400771 wiphy_name(local->hw.wiphy),
Jiri Bencf0706e82007-05-05 11:45:53 -0700772 skb_queue_len(
773 &sta->tx_filtered),
774 !!(sta->flags & WLAN_STA_PS),
775 jiffies);
776 }
777 dev_kfree_skb(skb);
778 }
779 sta_info_put(sta);
780 return;
781 }
782 } else {
783 /* FIXME: STUPID to call this with both local and local->mdev */
784 rate_control_tx_status(local, local->mdev, skb, status);
785 }
786
787 ieee80211_led_tx(local, 0);
788
789 /* SNMP counters
790 * Fragments are passed to low-level drivers as separate skbs, so these
791 * are actually fragments, not frames. Update frame counters only for
792 * the first fragment of the frame. */
793
794 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
795 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
796
797 if (status->flags & IEEE80211_TX_STATUS_ACK) {
798 if (frag == 0) {
799 local->dot11TransmittedFrameCount++;
800 if (is_multicast_ether_addr(hdr->addr1))
801 local->dot11MulticastTransmittedFrameCount++;
802 if (status->retry_count > 0)
803 local->dot11RetryCount++;
804 if (status->retry_count > 1)
805 local->dot11MultipleRetryCount++;
806 }
807
808 /* This counter shall be incremented for an acknowledged MPDU
809 * with an individual address in the address 1 field or an MPDU
810 * with a multicast address in the address 1 field of type Data
811 * or Management. */
812 if (!is_multicast_ether_addr(hdr->addr1) ||
813 type == IEEE80211_FTYPE_DATA ||
814 type == IEEE80211_FTYPE_MGMT)
815 local->dot11TransmittedFragmentCount++;
816 } else {
817 if (frag == 0)
818 local->dot11FailedCount++;
819 }
820
Johannes Bergb306f452007-07-10 19:32:08 +0200821 /* this was a transmitted frame, but now we want to reuse it */
822 skb_orphan(skb);
823
Johannes Bergb306f452007-07-10 19:32:08 +0200824 if (!local->monitors) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700825 dev_kfree_skb(skb);
826 return;
827 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700828
Johannes Bergb306f452007-07-10 19:32:08 +0200829 /* send frame to monitor interfaces now */
830
831 if (skb_headroom(skb) < sizeof(*rthdr)) {
832 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
833 dev_kfree_skb(skb);
834 return;
835 }
836
837 rthdr = (struct ieee80211_tx_status_rtap_hdr*)
838 skb_push(skb, sizeof(*rthdr));
839
840 memset(rthdr, 0, sizeof(*rthdr));
841 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
842 rthdr->hdr.it_present =
843 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
844 (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
845
846 if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
847 !is_multicast_ether_addr(hdr->addr1))
848 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
849
850 if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
851 (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
852 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
853 else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
854 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
855
856 rthdr->data_retries = status->retry_count;
857
Johannes Berg79010422007-09-18 17:29:21 -0400858 rcu_read_lock();
Johannes Bergb306f452007-07-10 19:32:08 +0200859 monitors = local->monitors;
Johannes Berg79010422007-09-18 17:29:21 -0400860 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Bergb306f452007-07-10 19:32:08 +0200861 /*
862 * Using the monitors counter is possibly racy, but
863 * if the value is wrong we simply either clone the skb
864 * once too much or forget sending it to one monitor iface
865 * The latter case isn't nice but fixing the race is much
866 * more complicated.
867 */
868 if (!monitors || !skb)
869 goto out;
870
871 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
872 if (!netif_running(sdata->dev))
873 continue;
874 monitors--;
875 if (monitors)
Johannes Berg79010422007-09-18 17:29:21 -0400876 skb2 = skb_clone(skb, GFP_ATOMIC);
Johannes Bergb306f452007-07-10 19:32:08 +0200877 else
878 skb2 = NULL;
879 skb->dev = sdata->dev;
880 /* XXX: is this sufficient for BPF? */
881 skb_set_mac_header(skb, 0);
882 skb->ip_summed = CHECKSUM_UNNECESSARY;
883 skb->pkt_type = PACKET_OTHERHOST;
884 skb->protocol = htons(ETH_P_802_2);
885 memset(skb->cb, 0, sizeof(skb->cb));
886 netif_rx(skb);
887 skb = skb2;
Johannes Bergb306f452007-07-10 19:32:08 +0200888 }
889 }
890 out:
Johannes Berg79010422007-09-18 17:29:21 -0400891 rcu_read_unlock();
Johannes Bergb306f452007-07-10 19:32:08 +0200892 if (skb)
893 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -0700894}
895EXPORT_SYMBOL(ieee80211_tx_status);
896
Jiri Bencf0706e82007-05-05 11:45:53 -0700897struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
898 const struct ieee80211_ops *ops)
899{
900 struct net_device *mdev;
901 struct ieee80211_local *local;
902 struct ieee80211_sub_if_data *sdata;
903 int priv_size;
904 struct wiphy *wiphy;
905
906 /* Ensure 32-byte alignment of our private data and hw private data.
907 * We use the wiphy priv data for both our ieee80211_local and for
908 * the driver's private data
909 *
910 * In memory it'll be like this:
911 *
912 * +-------------------------+
913 * | struct wiphy |
914 * +-------------------------+
915 * | struct ieee80211_local |
916 * +-------------------------+
917 * | driver's private data |
918 * +-------------------------+
919 *
920 */
921 priv_size = ((sizeof(struct ieee80211_local) +
922 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
923 priv_data_len;
924
925 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
926
927 if (!wiphy)
928 return NULL;
929
930 wiphy->privid = mac80211_wiphy_privid;
931
932 local = wiphy_priv(wiphy);
933 local->hw.wiphy = wiphy;
934
935 local->hw.priv = (char *)local +
936 ((sizeof(struct ieee80211_local) +
937 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
938
Johannes Berg4480f15c2007-07-10 19:32:10 +0200939 BUG_ON(!ops->tx);
Johannes Berg4150c572007-09-17 01:29:23 -0400940 BUG_ON(!ops->start);
941 BUG_ON(!ops->stop);
Johannes Berg4480f15c2007-07-10 19:32:10 +0200942 BUG_ON(!ops->config);
943 BUG_ON(!ops->add_interface);
Johannes Berg4150c572007-09-17 01:29:23 -0400944 BUG_ON(!ops->remove_interface);
945 BUG_ON(!ops->configure_filter);
Jiri Bencf0706e82007-05-05 11:45:53 -0700946 local->ops = ops;
947
948 /* for now, mdev needs sub_if_data :/ */
949 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
950 "wmaster%d", ether_setup);
951 if (!mdev) {
952 wiphy_free(wiphy);
953 return NULL;
954 }
955
956 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
957 mdev->ieee80211_ptr = &sdata->wdev;
958 sdata->wdev.wiphy = wiphy;
959
960 local->hw.queues = 1; /* default */
961
962 local->mdev = mdev;
963 local->rx_pre_handlers = ieee80211_rx_pre_handlers;
964 local->rx_handlers = ieee80211_rx_handlers;
965 local->tx_handlers = ieee80211_tx_handlers;
966
967 local->bridge_packets = 1;
968
969 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
970 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
971 local->short_retry_limit = 7;
972 local->long_retry_limit = 4;
973 local->hw.conf.radio_enabled = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -0700974
Johannes Bergb708e612007-09-14 11:10:25 -0400975 local->enabled_modes = ~0;
Jiri Bencf0706e82007-05-05 11:45:53 -0700976
977 INIT_LIST_HEAD(&local->modes_list);
978
Johannes Berg79010422007-09-18 17:29:21 -0400979 INIT_LIST_HEAD(&local->interfaces);
Jiri Bencf0706e82007-05-05 11:45:53 -0700980
981 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
Jiri Bencf0706e82007-05-05 11:45:53 -0700982 ieee80211_rx_bss_list_init(mdev);
983
984 sta_info_init(local);
985
986 mdev->hard_start_xmit = ieee80211_master_start_xmit;
987 mdev->open = ieee80211_master_open;
988 mdev->stop = ieee80211_master_stop;
989 mdev->type = ARPHRD_IEEE80211;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700990 mdev->header_ops = &ieee80211_header_ops;
Johannes Berg4150c572007-09-17 01:29:23 -0400991 mdev->set_multicast_list = ieee80211_master_set_multicast_list;
Jiri Bencf0706e82007-05-05 11:45:53 -0700992
993 sdata->type = IEEE80211_IF_TYPE_AP;
994 sdata->dev = mdev;
995 sdata->local = local;
996 sdata->u.ap.force_unicast_rateidx = -1;
997 sdata->u.ap.max_ratectrl_rateidx = -1;
998 ieee80211_if_sdata_init(sdata);
Johannes Berg79010422007-09-18 17:29:21 -0400999 /* no RCU needed since we're still during init phase */
1000 list_add_tail(&sdata->list, &local->interfaces);
Jiri Bencf0706e82007-05-05 11:45:53 -07001001
1002 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1003 (unsigned long)local);
1004 tasklet_disable(&local->tx_pending_tasklet);
1005
1006 tasklet_init(&local->tasklet,
1007 ieee80211_tasklet_handler,
1008 (unsigned long) local);
1009 tasklet_disable(&local->tasklet);
1010
1011 skb_queue_head_init(&local->skb_queue);
1012 skb_queue_head_init(&local->skb_queue_unreliable);
1013
1014 return local_to_hw(local);
1015}
1016EXPORT_SYMBOL(ieee80211_alloc_hw);
1017
1018int ieee80211_register_hw(struct ieee80211_hw *hw)
1019{
1020 struct ieee80211_local *local = hw_to_local(hw);
1021 const char *name;
1022 int result;
1023
1024 result = wiphy_register(local->hw.wiphy);
1025 if (result < 0)
1026 return result;
1027
1028 name = wiphy_dev(local->hw.wiphy)->driver->name;
1029 local->hw.workqueue = create_singlethread_workqueue(name);
1030 if (!local->hw.workqueue) {
1031 result = -ENOMEM;
1032 goto fail_workqueue;
1033 }
1034
Johannes Bergb306f452007-07-10 19:32:08 +02001035 /*
1036 * The hardware needs headroom for sending the frame,
1037 * and we need some headroom for passing the frame to monitor
1038 * interfaces, but never both at the same time.
1039 */
Jiri Benc33ccad32007-07-18 17:10:44 +02001040 local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1041 sizeof(struct ieee80211_tx_status_rtap_hdr));
Johannes Bergb306f452007-07-10 19:32:08 +02001042
Jiri Bence9f207f2007-05-05 11:46:38 -07001043 debugfs_hw_add(local);
1044
Jiri Bencf0706e82007-05-05 11:45:53 -07001045 local->hw.conf.beacon_int = 1000;
1046
1047 local->wstats_flags |= local->hw.max_rssi ?
1048 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1049 local->wstats_flags |= local->hw.max_signal ?
1050 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1051 local->wstats_flags |= local->hw.max_noise ?
1052 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1053 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1054 local->wstats_flags |= IW_QUAL_DBM;
1055
1056 result = sta_info_start(local);
1057 if (result < 0)
1058 goto fail_sta_info;
1059
1060 rtnl_lock();
1061 result = dev_alloc_name(local->mdev, local->mdev->name);
1062 if (result < 0)
1063 goto fail_dev;
1064
1065 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1066 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1067
1068 result = register_netdevice(local->mdev);
1069 if (result < 0)
1070 goto fail_dev;
1071
Jiri Bence9f207f2007-05-05 11:46:38 -07001072 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Johannes Berg5b2812e2007-09-26 14:27:23 +02001073 ieee80211_if_set_type(local->mdev, IEEE80211_IF_TYPE_AP);
Jiri Bence9f207f2007-05-05 11:46:38 -07001074
Jiri Bencf0706e82007-05-05 11:45:53 -07001075 result = ieee80211_init_rate_ctrl_alg(local, NULL);
1076 if (result < 0) {
1077 printk(KERN_DEBUG "%s: Failed to initialize rate control "
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001078 "algorithm\n", wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001079 goto fail_rate;
1080 }
1081
1082 result = ieee80211_wep_init(local);
1083
1084 if (result < 0) {
1085 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001086 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001087 goto fail_wep;
1088 }
1089
1090 ieee80211_install_qdisc(local->mdev);
1091
1092 /* add one default STA interface */
1093 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
1094 IEEE80211_IF_TYPE_STA);
1095 if (result)
1096 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001097 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001098
1099 local->reg_state = IEEE80211_DEV_REGISTERED;
1100 rtnl_unlock();
1101
1102 ieee80211_led_init(local);
1103
1104 return 0;
1105
1106fail_wep:
1107 rate_control_deinitialize(local);
1108fail_rate:
Jiri Bence9f207f2007-05-05 11:46:38 -07001109 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001110 unregister_netdevice(local->mdev);
1111fail_dev:
1112 rtnl_unlock();
1113 sta_info_stop(local);
1114fail_sta_info:
Jiri Bence9f207f2007-05-05 11:46:38 -07001115 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001116 destroy_workqueue(local->hw.workqueue);
1117fail_workqueue:
1118 wiphy_unregister(local->hw.wiphy);
1119 return result;
1120}
1121EXPORT_SYMBOL(ieee80211_register_hw);
1122
1123int ieee80211_register_hwmode(struct ieee80211_hw *hw,
1124 struct ieee80211_hw_mode *mode)
1125{
1126 struct ieee80211_local *local = hw_to_local(hw);
1127 struct ieee80211_rate *rate;
1128 int i;
1129
1130 INIT_LIST_HEAD(&mode->list);
1131 list_add_tail(&mode->list, &local->modes_list);
1132
1133 local->hw_modes |= (1 << mode->mode);
1134 for (i = 0; i < mode->num_rates; i++) {
1135 rate = &(mode->rates[i]);
1136 rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
1137 }
1138 ieee80211_prepare_rates(local, mode);
1139
1140 if (!local->oper_hw_mode) {
1141 /* Default to this mode */
1142 local->hw.conf.phymode = mode->mode;
1143 local->oper_hw_mode = local->scan_hw_mode = mode;
1144 local->oper_channel = local->scan_channel = &mode->channels[0];
1145 local->hw.conf.mode = local->oper_hw_mode;
1146 local->hw.conf.chan = local->oper_channel;
1147 }
1148
1149 if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
Daniel Drakefd8bacc2007-06-09 19:07:14 +01001150 ieee80211_set_default_regdomain(mode);
Jiri Bencf0706e82007-05-05 11:45:53 -07001151
1152 return 0;
1153}
1154EXPORT_SYMBOL(ieee80211_register_hwmode);
1155
1156void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1157{
1158 struct ieee80211_local *local = hw_to_local(hw);
1159 struct ieee80211_sub_if_data *sdata, *tmp;
Jiri Bencf0706e82007-05-05 11:45:53 -07001160 int i;
1161
1162 tasklet_kill(&local->tx_pending_tasklet);
1163 tasklet_kill(&local->tasklet);
1164
1165 rtnl_lock();
1166
1167 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1168
1169 local->reg_state = IEEE80211_DEV_UNREGISTERED;
Jiri Bencf0706e82007-05-05 11:45:53 -07001170
Johannes Berg79010422007-09-18 17:29:21 -04001171 /*
1172 * At this point, interface list manipulations are fine
1173 * because the driver cannot be handing us frames any
1174 * more and the tasklet is killed.
1175 */
Johannes Berg5b2812e2007-09-26 14:27:23 +02001176
1177 /*
1178 * First, we remove all non-master interfaces. Do this because they
1179 * may have bss pointer dependency on the master, and when we free
1180 * the master these would be freed as well, breaking our list
1181 * iteration completely.
1182 */
1183 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) {
1184 if (sdata->dev == local->mdev)
1185 continue;
1186 list_del(&sdata->list);
Jiri Bencf0706e82007-05-05 11:45:53 -07001187 __ieee80211_if_del(local, sdata);
Johannes Berg5b2812e2007-09-26 14:27:23 +02001188 }
1189
1190 /* then, finally, remove the master interface */
1191 __ieee80211_if_del(local, IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001192
1193 rtnl_unlock();
1194
Jiri Bencf0706e82007-05-05 11:45:53 -07001195 ieee80211_rx_bss_list_deinit(local->mdev);
1196 ieee80211_clear_tx_pending(local);
1197 sta_info_stop(local);
1198 rate_control_deinitialize(local);
Jiri Bence9f207f2007-05-05 11:46:38 -07001199 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001200
1201 for (i = 0; i < NUM_IEEE80211_MODES; i++) {
1202 kfree(local->supp_rates[i]);
1203 kfree(local->basic_rates[i]);
1204 }
1205
1206 if (skb_queue_len(&local->skb_queue)
1207 || skb_queue_len(&local->skb_queue_unreliable))
1208 printk(KERN_WARNING "%s: skb_queue not empty\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001209 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001210 skb_queue_purge(&local->skb_queue);
1211 skb_queue_purge(&local->skb_queue_unreliable);
1212
1213 destroy_workqueue(local->hw.workqueue);
1214 wiphy_unregister(local->hw.wiphy);
1215 ieee80211_wep_free(local);
1216 ieee80211_led_exit(local);
1217}
1218EXPORT_SYMBOL(ieee80211_unregister_hw);
1219
1220void ieee80211_free_hw(struct ieee80211_hw *hw)
1221{
1222 struct ieee80211_local *local = hw_to_local(hw);
1223
1224 ieee80211_if_free(local->mdev);
1225 wiphy_free(local->hw.wiphy);
1226}
1227EXPORT_SYMBOL(ieee80211_free_hw);
1228
Jiri Bencf0706e82007-05-05 11:45:53 -07001229static int __init ieee80211_init(void)
1230{
1231 struct sk_buff *skb;
1232 int ret;
1233
1234 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1235
Johannes Bergac71c692007-10-28 14:17:44 +01001236#ifdef CONFIG_MAC80211_RCSIMPLE
1237 ret = ieee80211_rate_control_register(&mac80211_rcsimple);
1238 if (ret)
1239 return ret;
1240#endif
1241
Jiri Bencf0706e82007-05-05 11:45:53 -07001242 ret = ieee80211_wme_register();
1243 if (ret) {
Johannes Bergac71c692007-10-28 14:17:44 +01001244#ifdef CONFIG_MAC80211_RCSIMPLE
1245 ieee80211_rate_control_unregister(&mac80211_rcsimple);
1246#endif
Jiri Bencf0706e82007-05-05 11:45:53 -07001247 printk(KERN_DEBUG "ieee80211_init: failed to "
1248 "initialize WME (err=%d)\n", ret);
1249 return ret;
1250 }
1251
Jiri Bence9f207f2007-05-05 11:46:38 -07001252 ieee80211_debugfs_netdev_init();
Daniel Drakefd8bacc2007-06-09 19:07:14 +01001253 ieee80211_regdomain_init();
Jiri Bence9f207f2007-05-05 11:46:38 -07001254
Jiri Bencf0706e82007-05-05 11:45:53 -07001255 return 0;
1256}
1257
Jiri Bencf0706e82007-05-05 11:45:53 -07001258static void __exit ieee80211_exit(void)
1259{
Johannes Bergac71c692007-10-28 14:17:44 +01001260#ifdef CONFIG_MAC80211_RCSIMPLE
1261 ieee80211_rate_control_unregister(&mac80211_rcsimple);
1262#endif
1263
Jiri Bencf0706e82007-05-05 11:45:53 -07001264 ieee80211_wme_unregister();
Jiri Bence9f207f2007-05-05 11:46:38 -07001265 ieee80211_debugfs_netdev_exit();
Jiri Bencf0706e82007-05-05 11:45:53 -07001266}
1267
1268
Johannes Bergca9938f2007-09-11 12:50:32 +02001269subsys_initcall(ieee80211_init);
Jiri Bencf0706e82007-05-05 11:45:53 -07001270module_exit(ieee80211_exit);
1271
1272MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1273MODULE_LICENSE("GPL");