blob: ccf84634f15692c89a6b2a20644e99df0695fb43 [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
27#include "ieee80211_common.h"
28#include "ieee80211_i.h"
29#include "ieee80211_rate.h"
30#include "wep.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070031#include "wme.h"
32#include "aes_ccm.h"
33#include "ieee80211_led.h"
Michael Wue0eb6852007-09-18 17:29:21 -040034#include "cfg.h"
Jiri Bence9f207f2007-05-05 11:46:38 -070035#include "debugfs.h"
36#include "debugfs_netdev.h"
Jiri Bencf0706e82007-05-05 11:45:53 -070037
Johannes Bergb306f452007-07-10 19:32:08 +020038/*
39 * For seeing transmitted packets on monitor interfaces
40 * we have a radiotap header too.
41 */
42struct ieee80211_tx_status_rtap_hdr {
43 struct ieee80211_radiotap_header hdr;
44 __le16 tx_flags;
45 u8 data_retries;
46} __attribute__ ((packed));
47
Johannes Bergb2c258f2007-07-27 15:43:23 +020048/* common interface routines */
Jiri Bencf0706e82007-05-05 11:45:53 -070049
Stephen Hemmingerb95cce32007-09-26 22:13:38 -070050static int header_parse_80211(const struct sk_buff *skb, unsigned char *haddr)
Jiri Bencf0706e82007-05-05 11:45:53 -070051{
Johannes Bergb2c258f2007-07-27 15:43:23 +020052 memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
53 return ETH_ALEN;
Jiri Bencf0706e82007-05-05 11:45:53 -070054}
55
Johannes Berg4150c572007-09-17 01:29:23 -040056/* must be called under mdev tx lock */
57static void ieee80211_configure_filter(struct ieee80211_local *local)
58{
59 unsigned int changed_flags;
60 unsigned int new_flags = 0;
61
62 if (local->iff_promiscs)
63 new_flags |= FIF_PROMISC_IN_BSS;
64
65 if (local->iff_allmultis)
66 new_flags |= FIF_ALLMULTI;
67
68 if (local->monitors)
69 new_flags |= FIF_CONTROL |
70 FIF_OTHER_BSS |
71 FIF_BCN_PRBRESP_PROMISC;
72
73 changed_flags = local->filter_flags ^ new_flags;
74
75 /* be a bit nasty */
76 new_flags |= (1<<31);
77
78 local->ops->configure_filter(local_to_hw(local),
79 changed_flags, &new_flags,
80 local->mdev->mc_count,
81 local->mdev->mc_list);
82
83 WARN_ON(new_flags & (1<<31));
84
85 local->filter_flags = new_flags & ~(1<<31);
86}
87
Johannes Bergb2c258f2007-07-27 15:43:23 +020088/* master interface */
Jiri Bencf0706e82007-05-05 11:45:53 -070089
90static int ieee80211_master_open(struct net_device *dev)
91{
92 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
93 struct ieee80211_sub_if_data *sdata;
94 int res = -EOPNOTSUPP;
95
Johannes Berg79010422007-09-18 17:29:21 -040096 /* we hold the RTNL here so can safely walk the list */
97 list_for_each_entry(sdata, &local->interfaces, list) {
Jiri Bencf0706e82007-05-05 11:45:53 -070098 if (sdata->dev != dev && netif_running(sdata->dev)) {
99 res = 0;
100 break;
101 }
102 }
Jiri Bencf0706e82007-05-05 11:45:53 -0700103 return res;
104}
105
106static int ieee80211_master_stop(struct net_device *dev)
107{
108 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
109 struct ieee80211_sub_if_data *sdata;
110
Johannes Berg79010422007-09-18 17:29:21 -0400111 /* we hold the RTNL here so can safely walk the list */
112 list_for_each_entry(sdata, &local->interfaces, list)
Jiri Bencf0706e82007-05-05 11:45:53 -0700113 if (sdata->dev != dev && netif_running(sdata->dev))
114 dev_close(sdata->dev);
Jiri Bencf0706e82007-05-05 11:45:53 -0700115
116 return 0;
117}
118
Johannes Berg4150c572007-09-17 01:29:23 -0400119static void ieee80211_master_set_multicast_list(struct net_device *dev)
120{
121 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
122
123 ieee80211_configure_filter(local);
124}
125
Johannes Bergb2c258f2007-07-27 15:43:23 +0200126/* management interface */
Jiri Bencf0706e82007-05-05 11:45:53 -0700127
128static void
129ieee80211_fill_frame_info(struct ieee80211_local *local,
130 struct ieee80211_frame_info *fi,
131 struct ieee80211_rx_status *status)
132{
133 if (status) {
134 struct timespec ts;
135 struct ieee80211_rate *rate;
136
137 jiffies_to_timespec(jiffies, &ts);
138 fi->hosttime = cpu_to_be64((u64) ts.tv_sec * 1000000 +
139 ts.tv_nsec / 1000);
140 fi->mactime = cpu_to_be64(status->mactime);
141 switch (status->phymode) {
142 case MODE_IEEE80211A:
143 fi->phytype = htonl(ieee80211_phytype_ofdm_dot11_a);
144 break;
145 case MODE_IEEE80211B:
146 fi->phytype = htonl(ieee80211_phytype_dsss_dot11_b);
147 break;
148 case MODE_IEEE80211G:
149 fi->phytype = htonl(ieee80211_phytype_pbcc_dot11_g);
150 break;
Jiri Bencf0706e82007-05-05 11:45:53 -0700151 default:
152 fi->phytype = htonl(0xAAAAAAAA);
153 break;
154 }
155 fi->channel = htonl(status->channel);
156 rate = ieee80211_get_rate(local, status->phymode,
157 status->rate);
158 if (rate) {
159 fi->datarate = htonl(rate->rate);
160 if (rate->flags & IEEE80211_RATE_PREAMBLE2) {
161 if (status->rate == rate->val)
162 fi->preamble = htonl(2); /* long */
163 else if (status->rate == rate->val2)
164 fi->preamble = htonl(1); /* short */
165 } else
166 fi->preamble = htonl(0);
167 } else {
168 fi->datarate = htonl(0);
169 fi->preamble = htonl(0);
170 }
171
172 fi->antenna = htonl(status->antenna);
173 fi->priority = htonl(0xffffffff); /* no clue */
174 fi->ssi_type = htonl(ieee80211_ssi_raw);
175 fi->ssi_signal = htonl(status->ssi);
176 fi->ssi_noise = 0x00000000;
177 fi->encoding = 0;
178 } else {
179 /* clear everything because we really don't know.
180 * the msg_type field isn't present on monitor frames
181 * so we don't know whether it will be present or not,
182 * but it's ok to not clear it since it'll be assigned
183 * anyway */
184 memset(fi, 0, sizeof(*fi) - sizeof(fi->msg_type));
185
186 fi->ssi_type = htonl(ieee80211_ssi_none);
187 }
188 fi->version = htonl(IEEE80211_FI_VERSION);
189 fi->length = cpu_to_be32(sizeof(*fi) - sizeof(fi->msg_type));
190}
191
192/* this routine is actually not just for this, but also
193 * for pushing fake 'management' frames into userspace.
194 * it shall be replaced by a netlink-based system. */
195void
196ieee80211_rx_mgmt(struct ieee80211_local *local, struct sk_buff *skb,
197 struct ieee80211_rx_status *status, u32 msg_type)
198{
199 struct ieee80211_frame_info *fi;
200 const size_t hlen = sizeof(struct ieee80211_frame_info);
Stephen Hemminger68aae112007-08-24 11:29:34 -0700201 struct net_device *dev = local->apdev;
Jiri Bencf0706e82007-05-05 11:45:53 -0700202
Stephen Hemminger68aae112007-08-24 11:29:34 -0700203 skb->dev = dev;
Jiri Bencf0706e82007-05-05 11:45:53 -0700204
205 if (skb_headroom(skb) < hlen) {
206 I802_DEBUG_INC(local->rx_expand_skb_head);
207 if (pskb_expand_head(skb, hlen, 0, GFP_ATOMIC)) {
208 dev_kfree_skb(skb);
209 return;
210 }
211 }
212
213 fi = (struct ieee80211_frame_info *) skb_push(skb, hlen);
214
215 ieee80211_fill_frame_info(local, fi, status);
216 fi->msg_type = htonl(msg_type);
217
Stephen Hemminger68aae112007-08-24 11:29:34 -0700218 dev->stats.rx_packets++;
219 dev->stats.rx_bytes += skb->len;
Jiri Bencf0706e82007-05-05 11:45:53 -0700220
221 skb_set_mac_header(skb, 0);
222 skb->ip_summed = CHECKSUM_UNNECESSARY;
223 skb->pkt_type = PACKET_OTHERHOST;
224 skb->protocol = htons(ETH_P_802_2);
225 memset(skb->cb, 0, sizeof(skb->cb));
226 netif_rx(skb);
227}
228
Johannes Bergb2c258f2007-07-27 15:43:23 +0200229static int ieee80211_mgmt_open(struct net_device *dev)
230{
231 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
232
233 if (!netif_running(local->mdev))
234 return -EOPNOTSUPP;
235 return 0;
236}
237
238static int ieee80211_mgmt_stop(struct net_device *dev)
239{
240 return 0;
241}
242
243static int ieee80211_change_mtu_apdev(struct net_device *dev, int new_mtu)
244{
245 /* FIX: what would be proper limits for MTU?
246 * This interface uses 802.11 frames. */
247 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN) {
248 printk(KERN_WARNING "%s: invalid MTU %d\n",
249 dev->name, new_mtu);
250 return -EINVAL;
251 }
252
253#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
254 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
255#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
256 dev->mtu = new_mtu;
257 return 0;
258}
259
260void ieee80211_if_mgmt_setup(struct net_device *dev)
261{
262 ether_setup(dev);
263 dev->hard_start_xmit = ieee80211_mgmt_start_xmit;
264 dev->change_mtu = ieee80211_change_mtu_apdev;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200265 dev->open = ieee80211_mgmt_open;
266 dev->stop = ieee80211_mgmt_stop;
267 dev->type = ARPHRD_IEEE80211_PRISM;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200268 dev->uninit = ieee80211_if_reinit;
269 dev->destructor = ieee80211_if_free;
270}
271
272/* regular interfaces */
273
274static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
275{
276 /* FIX: what would be proper limits for MTU?
277 * This interface uses 802.3 frames. */
278 if (new_mtu < 256 || new_mtu > IEEE80211_MAX_DATA_LEN - 24 - 6) {
279 printk(KERN_WARNING "%s: invalid MTU %d\n",
280 dev->name, new_mtu);
281 return -EINVAL;
282 }
283
284#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
285 printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
286#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
287 dev->mtu = new_mtu;
288 return 0;
289}
290
291static inline int identical_mac_addr_allowed(int type1, int type2)
292{
293 return (type1 == IEEE80211_IF_TYPE_MNTR ||
294 type2 == IEEE80211_IF_TYPE_MNTR ||
295 (type1 == IEEE80211_IF_TYPE_AP &&
296 type2 == IEEE80211_IF_TYPE_WDS) ||
297 (type1 == IEEE80211_IF_TYPE_WDS &&
298 (type2 == IEEE80211_IF_TYPE_WDS ||
299 type2 == IEEE80211_IF_TYPE_AP)) ||
300 (type1 == IEEE80211_IF_TYPE_AP &&
301 type2 == IEEE80211_IF_TYPE_VLAN) ||
302 (type1 == IEEE80211_IF_TYPE_VLAN &&
303 (type2 == IEEE80211_IF_TYPE_AP ||
304 type2 == IEEE80211_IF_TYPE_VLAN)));
305}
306
Johannes Bergb2c258f2007-07-27 15:43:23 +0200307static int ieee80211_open(struct net_device *dev)
308{
309 struct ieee80211_sub_if_data *sdata, *nsdata;
310 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
311 struct ieee80211_if_init_conf conf;
312 int res;
313
314 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400315
Johannes Berg79010422007-09-18 17:29:21 -0400316 /* we hold the RTNL here so can safely walk the list */
317 list_for_each_entry(nsdata, &local->interfaces, list) {
Johannes Bergb2c258f2007-07-27 15:43:23 +0200318 struct net_device *ndev = nsdata->dev;
319
320 if (ndev != dev && ndev != local->mdev && netif_running(ndev) &&
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400321 compare_ether_addr(dev->dev_addr, ndev->dev_addr) == 0) {
322 /*
323 * check whether it may have the same address
324 */
325 if (!identical_mac_addr_allowed(sdata->type,
Johannes Berg79010422007-09-18 17:29:21 -0400326 nsdata->type))
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400327 return -ENOTUNIQ;
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400328
329 /*
330 * can only add VLANs to enabled APs
331 */
332 if (sdata->type == IEEE80211_IF_TYPE_VLAN &&
333 nsdata->type == IEEE80211_IF_TYPE_AP &&
334 netif_running(nsdata->dev))
335 sdata->u.vlan.ap = nsdata;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200336 }
337 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200338
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400339 switch (sdata->type) {
340 case IEEE80211_IF_TYPE_WDS:
341 if (is_zero_ether_addr(sdata->u.wds.remote_addr))
342 return -ENOLINK;
343 break;
344 case IEEE80211_IF_TYPE_VLAN:
345 if (!sdata->u.vlan.ap)
346 return -ENOLINK;
347 break;
348 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200349
Johannes Bergb2c258f2007-07-27 15:43:23 +0200350 if (local->open_count == 0) {
351 res = 0;
Johannes Berg4150c572007-09-17 01:29:23 -0400352 if (local->ops->start)
353 res = local->ops->start(local_to_hw(local));
354 if (res)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200355 return res;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200356 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200357
Johannes Berg4150c572007-09-17 01:29:23 -0400358 switch (sdata->type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400359 case IEEE80211_IF_TYPE_VLAN:
360 list_add(&sdata->u.vlan.list, &sdata->u.vlan.ap->u.ap.vlans);
361 /* no need to tell driver */
362 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400363 case IEEE80211_IF_TYPE_MNTR:
364 /* must be before the call to ieee80211_configure_filter */
Johannes Bergb2c258f2007-07-27 15:43:23 +0200365 local->monitors++;
Johannes Berg4150c572007-09-17 01:29:23 -0400366 if (local->monitors == 1) {
367 netif_tx_lock_bh(local->mdev);
368 ieee80211_configure_filter(local);
369 netif_tx_unlock_bh(local->mdev);
370
371 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
372 ieee80211_hw_config(local);
373 }
374 break;
375 case IEEE80211_IF_TYPE_STA:
376 case IEEE80211_IF_TYPE_IBSS:
377 sdata->u.sta.flags &= ~IEEE80211_STA_PREV_BSSID_SET;
378 /* fall through */
379 default:
380 conf.if_id = dev->ifindex;
381 conf.type = sdata->type;
382 conf.mac_addr = dev->dev_addr;
383 res = local->ops->add_interface(local_to_hw(local), &conf);
384 if (res && !local->open_count && local->ops->stop)
385 local->ops->stop(local_to_hw(local));
386 if (res)
387 return res;
388
Johannes Bergb2c258f2007-07-27 15:43:23 +0200389 ieee80211_if_config(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200390 ieee80211_reset_erp_info(dev);
Johannes Berg11a843b2007-08-28 17:01:55 -0400391 ieee80211_enable_keys(sdata);
Johannes Berg4150c572007-09-17 01:29:23 -0400392
393 if (sdata->type == IEEE80211_IF_TYPE_STA &&
394 !local->user_space_mlme)
395 netif_carrier_off(dev);
396 else
397 netif_carrier_on(dev);
Daniel Draked9430a32007-07-27 15:43:24 +0200398 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200399
Johannes Berg4150c572007-09-17 01:29:23 -0400400 if (local->open_count == 0) {
401 res = dev_open(local->mdev);
402 WARN_ON(res);
403 if (local->apdev) {
404 res = dev_open(local->apdev);
405 WARN_ON(res);
406 }
407 tasklet_enable(&local->tx_pending_tasklet);
408 tasklet_enable(&local->tasklet);
409 }
410
411 local->open_count++;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200412
413 netif_start_queue(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400414
Johannes Bergb2c258f2007-07-27 15:43:23 +0200415 return 0;
416}
417
Johannes Berg4150c572007-09-17 01:29:23 -0400418static int ieee80211_stop(struct net_device *dev)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200419{
Johannes Berg4150c572007-09-17 01:29:23 -0400420 struct ieee80211_sub_if_data *sdata;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200421 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Johannes Berg4150c572007-09-17 01:29:23 -0400422 struct ieee80211_if_init_conf conf;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200423
Johannes Berg4150c572007-09-17 01:29:23 -0400424 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
425
426 netif_stop_queue(dev);
427
428 dev_mc_unsync(local->mdev, dev);
429
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400430 /* down all dependent devices, that is VLANs */
431 if (sdata->type == IEEE80211_IF_TYPE_AP) {
432 struct ieee80211_sub_if_data *vlan, *tmp;
433
434 list_for_each_entry_safe(vlan, tmp, &sdata->u.ap.vlans,
435 u.vlan.list)
436 dev_close(vlan->dev);
437 WARN_ON(!list_empty(&sdata->u.ap.vlans));
438 }
439
Johannes Berg4150c572007-09-17 01:29:23 -0400440 local->open_count--;
441
Johannes Bergb2c258f2007-07-27 15:43:23 +0200442 switch (sdata->type) {
Johannes Berg0ec3ca42007-09-17 01:29:24 -0400443 case IEEE80211_IF_TYPE_VLAN:
444 list_del(&sdata->u.vlan.list);
445 sdata->u.vlan.ap = NULL;
446 /* no need to tell driver */
447 break;
Johannes Berg4150c572007-09-17 01:29:23 -0400448 case IEEE80211_IF_TYPE_MNTR:
449 local->monitors--;
450 if (local->monitors == 0) {
451 netif_tx_lock_bh(local->mdev);
452 ieee80211_configure_filter(local);
453 netif_tx_unlock_bh(local->mdev);
454
455 local->hw.conf.flags |= IEEE80211_CONF_RADIOTAP;
456 ieee80211_hw_config(local);
457 }
458 break;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200459 case IEEE80211_IF_TYPE_STA:
460 case IEEE80211_IF_TYPE_IBSS:
461 sdata->u.sta.state = IEEE80211_DISABLED;
462 del_timer_sync(&sdata->u.sta.timer);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400463 /*
Johannes Berg79010422007-09-18 17:29:21 -0400464 * When we get here, the interface is marked down.
465 * Call synchronize_rcu() to wait for the RX path
466 * should it be using the interface and enqueuing
467 * frames at this very time on another CPU.
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400468 */
Johannes Berg79010422007-09-18 17:29:21 -0400469 synchronize_rcu();
Johannes Bergb2c258f2007-07-27 15:43:23 +0200470 skb_queue_purge(&sdata->u.sta.skb_queue);
Johannes Berg2a8a9a82007-08-28 17:01:52 -0400471
Johannes Bergb2c258f2007-07-27 15:43:23 +0200472 if (!local->ops->hw_scan &&
473 local->scan_dev == sdata->dev) {
474 local->sta_scanning = 0;
475 cancel_delayed_work(&local->scan_work);
476 }
477 flush_workqueue(local->hw.workqueue);
Johannes Berg4150c572007-09-17 01:29:23 -0400478 /* fall through */
479 default:
Johannes Bergb2c258f2007-07-27 15:43:23 +0200480 conf.if_id = dev->ifindex;
481 conf.type = sdata->type;
482 conf.mac_addr = dev->dev_addr;
Johannes Berg4150c572007-09-17 01:29:23 -0400483 /* disable all keys for as long as this netdev is down */
484 ieee80211_disable_keys(sdata);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200485 local->ops->remove_interface(local_to_hw(local), &conf);
486 }
487
Johannes Berg4150c572007-09-17 01:29:23 -0400488 if (local->open_count == 0) {
489 if (netif_running(local->mdev))
490 dev_close(local->mdev);
491
492 if (local->apdev)
493 dev_close(local->apdev);
494
495 if (local->ops->stop)
496 local->ops->stop(local_to_hw(local));
497
498 tasklet_disable(&local->tx_pending_tasklet);
499 tasklet_disable(&local->tasklet);
500 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200501
502 return 0;
503}
504
Johannes Bergb2c258f2007-07-27 15:43:23 +0200505static void ieee80211_set_multicast_list(struct net_device *dev)
506{
507 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
508 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Johannes Berg4150c572007-09-17 01:29:23 -0400509 int allmulti, promisc, sdata_allmulti, sdata_promisc;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200510
Johannes Berg4150c572007-09-17 01:29:23 -0400511 allmulti = !!(dev->flags & IFF_ALLMULTI);
512 promisc = !!(dev->flags & IFF_PROMISC);
513 sdata_allmulti = sdata->flags & IEEE80211_SDATA_ALLMULTI;
514 sdata_promisc = sdata->flags & IEEE80211_SDATA_PROMISC;
515
516 if (allmulti != sdata_allmulti) {
517 if (dev->flags & IFF_ALLMULTI)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200518 local->iff_allmultis++;
Johannes Berg4150c572007-09-17 01:29:23 -0400519 else
520 local->iff_allmultis--;
Jiri Slaby13262ff2007-08-28 17:01:54 -0400521 sdata->flags ^= IEEE80211_SDATA_ALLMULTI;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200522 }
Johannes Berg4150c572007-09-17 01:29:23 -0400523
524 if (promisc != sdata_promisc) {
525 if (dev->flags & IFF_PROMISC)
Johannes Bergb2c258f2007-07-27 15:43:23 +0200526 local->iff_promiscs++;
Johannes Berg4150c572007-09-17 01:29:23 -0400527 else
528 local->iff_promiscs--;
Jiri Slaby13262ff2007-08-28 17:01:54 -0400529 sdata->flags ^= IEEE80211_SDATA_PROMISC;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200530 }
Johannes Berg4150c572007-09-17 01:29:23 -0400531
532 dev_mc_sync(local->mdev, dev);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200533}
534
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700535static const struct header_ops ieee80211_header_ops = {
536 .create = eth_header,
537 .parse = header_parse_80211,
538 .rebuild = eth_rebuild_header,
539 .cache = eth_header_cache,
540 .cache_update = eth_header_cache_update,
541};
542
Johannes Bergb2c258f2007-07-27 15:43:23 +0200543/* Must not be called for mdev and apdev */
544void ieee80211_if_setup(struct net_device *dev)
545{
546 ether_setup(dev);
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700547 dev->header_ops = &ieee80211_header_ops;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200548 dev->hard_start_xmit = ieee80211_subif_start_xmit;
549 dev->wireless_handlers = &ieee80211_iw_handler_def;
550 dev->set_multicast_list = ieee80211_set_multicast_list;
551 dev->change_mtu = ieee80211_change_mtu;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200552 dev->open = ieee80211_open;
553 dev->stop = ieee80211_stop;
554 dev->uninit = ieee80211_if_reinit;
555 dev->destructor = ieee80211_if_free;
556}
557
558/* WDS specialties */
559
560int ieee80211_if_update_wds(struct net_device *dev, u8 *remote_addr)
561{
562 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
563 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
564 struct sta_info *sta;
Joe Perches0795af52007-10-03 17:59:30 -0700565 DECLARE_MAC_BUF(mac);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200566
567 if (compare_ether_addr(remote_addr, sdata->u.wds.remote_addr) == 0)
568 return 0;
569
570 /* Create STA entry for the new peer */
571 sta = sta_info_add(local, dev, remote_addr, GFP_KERNEL);
572 if (!sta)
573 return -ENOMEM;
574 sta_info_put(sta);
575
576 /* Remove STA entry for the old peer */
577 sta = sta_info_get(local, sdata->u.wds.remote_addr);
578 if (sta) {
Michael Wube8755e2007-07-27 15:43:23 +0200579 sta_info_free(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200580 sta_info_put(sta);
Johannes Bergb2c258f2007-07-27 15:43:23 +0200581 } else {
582 printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
Joe Perches0795af52007-10-03 17:59:30 -0700583 "peer %s\n",
584 dev->name, print_mac(mac, sdata->u.wds.remote_addr));
Johannes Bergb2c258f2007-07-27 15:43:23 +0200585 }
586
587 /* Update WDS link data */
588 memcpy(&sdata->u.wds.remote_addr, remote_addr, ETH_ALEN);
589
590 return 0;
591}
592
593/* everything else */
594
Johannes Bergb2c258f2007-07-27 15:43:23 +0200595static int __ieee80211_if_config(struct net_device *dev,
596 struct sk_buff *beacon,
597 struct ieee80211_tx_control *control)
598{
599 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
600 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
601 struct ieee80211_if_conf conf;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200602
603 if (!local->ops->config_interface || !netif_running(dev))
604 return 0;
605
606 memset(&conf, 0, sizeof(conf));
607 conf.type = sdata->type;
608 if (sdata->type == IEEE80211_IF_TYPE_STA ||
609 sdata->type == IEEE80211_IF_TYPE_IBSS) {
Johannes Berg4150c572007-09-17 01:29:23 -0400610 conf.bssid = sdata->u.sta.bssid;
Johannes Bergb2c258f2007-07-27 15:43:23 +0200611 conf.ssid = sdata->u.sta.ssid;
612 conf.ssid_len = sdata->u.sta.ssid_len;
613 conf.generic_elem = sdata->u.sta.extra_ie;
614 conf.generic_elem_len = sdata->u.sta.extra_ie_len;
615 } else if (sdata->type == IEEE80211_IF_TYPE_AP) {
616 conf.ssid = sdata->u.ap.ssid;
617 conf.ssid_len = sdata->u.ap.ssid_len;
618 conf.generic_elem = sdata->u.ap.generic_elem;
619 conf.generic_elem_len = sdata->u.ap.generic_elem_len;
620 conf.beacon = beacon;
621 conf.beacon_control = control;
622 }
623 return local->ops->config_interface(local_to_hw(local),
624 dev->ifindex, &conf);
625}
626
627int ieee80211_if_config(struct net_device *dev)
628{
629 return __ieee80211_if_config(dev, NULL, NULL);
630}
631
632int ieee80211_if_config_beacon(struct net_device *dev)
633{
634 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
635 struct ieee80211_tx_control control;
636 struct sk_buff *skb;
637
638 if (!(local->hw.flags & IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE))
639 return 0;
640 skb = ieee80211_beacon_get(local_to_hw(local), dev->ifindex, &control);
641 if (!skb)
642 return -ENOMEM;
643 return __ieee80211_if_config(dev, skb, &control);
644}
645
646int ieee80211_hw_config(struct ieee80211_local *local)
647{
648 struct ieee80211_hw_mode *mode;
649 struct ieee80211_channel *chan;
650 int ret = 0;
651
652 if (local->sta_scanning) {
653 chan = local->scan_channel;
654 mode = local->scan_hw_mode;
655 } else {
656 chan = local->oper_channel;
657 mode = local->oper_hw_mode;
658 }
659
660 local->hw.conf.channel = chan->chan;
661 local->hw.conf.channel_val = chan->val;
Michael Buesch61609bc2007-09-20 22:06:39 +0200662 if (!local->hw.conf.power_level) {
663 local->hw.conf.power_level = chan->power_level;
664 } else {
665 local->hw.conf.power_level = min(chan->power_level,
666 local->hw.conf.power_level);
667 }
Johannes Bergb2c258f2007-07-27 15:43:23 +0200668 local->hw.conf.freq = chan->freq;
669 local->hw.conf.phymode = mode->mode;
670 local->hw.conf.antenna_max = chan->antenna_max;
671 local->hw.conf.chan = chan;
672 local->hw.conf.mode = mode;
673
674#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
675 printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d "
676 "phymode=%d\n", local->hw.conf.channel, local->hw.conf.freq,
677 local->hw.conf.phymode);
678#endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
679
680 if (local->ops->config)
681 ret = local->ops->config(local_to_hw(local), &local->hw.conf);
682
683 return ret;
684}
685
Daniel Draked9430a32007-07-27 15:43:24 +0200686void ieee80211_erp_info_change_notify(struct net_device *dev, u8 changes)
687{
688 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
689 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
690 if (local->ops->erp_ie_changed)
691 local->ops->erp_ie_changed(local_to_hw(local), changes,
Jiri Slaby13262ff2007-08-28 17:01:54 -0400692 !!(sdata->flags & IEEE80211_SDATA_USE_PROTECTION),
693 !(sdata->flags & IEEE80211_SDATA_SHORT_PREAMBLE));
Daniel Draked9430a32007-07-27 15:43:24 +0200694}
695
696void ieee80211_reset_erp_info(struct net_device *dev)
697{
698 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
699
Jiri Slaby13262ff2007-08-28 17:01:54 -0400700 sdata->flags &= ~(IEEE80211_SDATA_USE_PROTECTION |
701 IEEE80211_SDATA_SHORT_PREAMBLE);
Daniel Draked9430a32007-07-27 15:43:24 +0200702 ieee80211_erp_info_change_notify(dev,
703 IEEE80211_ERP_CHANGE_PROTECTION |
704 IEEE80211_ERP_CHANGE_PREAMBLE);
705}
706
Jiri Bencf0706e82007-05-05 11:45:53 -0700707void ieee80211_tx_status_irqsafe(struct ieee80211_hw *hw,
708 struct sk_buff *skb,
709 struct ieee80211_tx_status *status)
710{
711 struct ieee80211_local *local = hw_to_local(hw);
712 struct ieee80211_tx_status *saved;
713 int tmp;
714
715 skb->dev = local->mdev;
716 saved = kmalloc(sizeof(struct ieee80211_tx_status), GFP_ATOMIC);
717 if (unlikely(!saved)) {
718 if (net_ratelimit())
719 printk(KERN_WARNING "%s: Not enough memory, "
720 "dropping tx status", skb->dev->name);
721 /* should be dev_kfree_skb_irq, but due to this function being
722 * named _irqsafe instead of just _irq we can't be sure that
723 * people won't call it from non-irq contexts */
724 dev_kfree_skb_any(skb);
725 return;
726 }
727 memcpy(saved, status, sizeof(struct ieee80211_tx_status));
728 /* copy pointer to saved status into skb->cb for use by tasklet */
729 memcpy(skb->cb, &saved, sizeof(saved));
730
731 skb->pkt_type = IEEE80211_TX_STATUS_MSG;
732 skb_queue_tail(status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS ?
733 &local->skb_queue : &local->skb_queue_unreliable, skb);
734 tmp = skb_queue_len(&local->skb_queue) +
735 skb_queue_len(&local->skb_queue_unreliable);
736 while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
737 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
738 memcpy(&saved, skb->cb, sizeof(saved));
739 kfree(saved);
740 dev_kfree_skb_irq(skb);
741 tmp--;
742 I802_DEBUG_INC(local->tx_status_drop);
743 }
744 tasklet_schedule(&local->tasklet);
745}
746EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
747
748static void ieee80211_tasklet_handler(unsigned long data)
749{
750 struct ieee80211_local *local = (struct ieee80211_local *) data;
751 struct sk_buff *skb;
752 struct ieee80211_rx_status rx_status;
753 struct ieee80211_tx_status *tx_status;
754
755 while ((skb = skb_dequeue(&local->skb_queue)) ||
756 (skb = skb_dequeue(&local->skb_queue_unreliable))) {
757 switch (skb->pkt_type) {
758 case IEEE80211_RX_MSG:
759 /* status is in skb->cb */
760 memcpy(&rx_status, skb->cb, sizeof(rx_status));
761 /* Clear skb->type in order to not confuse kernel
762 * netstack. */
763 skb->pkt_type = 0;
764 __ieee80211_rx(local_to_hw(local), skb, &rx_status);
765 break;
766 case IEEE80211_TX_STATUS_MSG:
767 /* get pointer to saved status out of skb->cb */
768 memcpy(&tx_status, skb->cb, sizeof(tx_status));
769 skb->pkt_type = 0;
770 ieee80211_tx_status(local_to_hw(local),
771 skb, tx_status);
772 kfree(tx_status);
773 break;
774 default: /* should never get here! */
775 printk(KERN_ERR "%s: Unknown message type (%d)\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400776 wiphy_name(local->hw.wiphy), skb->pkt_type);
Jiri Bencf0706e82007-05-05 11:45:53 -0700777 dev_kfree_skb(skb);
778 break;
779 }
780 }
781}
782
Jiri Bencf0706e82007-05-05 11:45:53 -0700783/* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
784 * make a prepared TX frame (one that has been given to hw) to look like brand
785 * new IEEE 802.11 frame that is ready to go through TX processing again.
786 * Also, tx_packet_data in cb is restored from tx_control. */
787static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
788 struct ieee80211_key *key,
789 struct sk_buff *skb,
790 struct ieee80211_tx_control *control)
791{
792 int hdrlen, iv_len, mic_len;
793 struct ieee80211_tx_packet_data *pkt_data;
794
795 pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
796 pkt_data->ifindex = control->ifindex;
Jiri Slabye8bf9642007-08-28 17:01:54 -0400797 pkt_data->flags = 0;
798 if (control->flags & IEEE80211_TXCTL_REQ_TX_STATUS)
799 pkt_data->flags |= IEEE80211_TXPD_REQ_TX_STATUS;
800 if (control->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)
801 pkt_data->flags |= IEEE80211_TXPD_DO_NOT_ENCRYPT;
802 if (control->flags & IEEE80211_TXCTL_REQUEUE)
803 pkt_data->flags |= IEEE80211_TXPD_REQUEUE;
804 if (control->type == IEEE80211_IF_TYPE_MGMT)
805 pkt_data->flags |= IEEE80211_TXPD_MGMT_IFACE;
Jiri Bencf0706e82007-05-05 11:45:53 -0700806 pkt_data->queue = control->queue;
807
808 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
809
810 if (!key)
811 goto no_key;
812
Johannes Berg8f20fc22007-08-28 17:01:54 -0400813 switch (key->conf.alg) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700814 case ALG_WEP:
815 iv_len = WEP_IV_LEN;
816 mic_len = WEP_ICV_LEN;
817 break;
818 case ALG_TKIP:
819 iv_len = TKIP_IV_LEN;
820 mic_len = TKIP_ICV_LEN;
821 break;
822 case ALG_CCMP:
823 iv_len = CCMP_HDR_LEN;
824 mic_len = CCMP_MIC_LEN;
825 break;
826 default:
827 goto no_key;
828 }
829
Johannes Berg8f20fc22007-08-28 17:01:54 -0400830 if (skb->len >= mic_len &&
Johannes Berg11a843b2007-08-28 17:01:55 -0400831 !(key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
Jiri Bencf0706e82007-05-05 11:45:53 -0700832 skb_trim(skb, skb->len - mic_len);
833 if (skb->len >= iv_len && skb->len > hdrlen) {
834 memmove(skb->data + iv_len, skb->data, hdrlen);
835 skb_pull(skb, iv_len);
836 }
837
838no_key:
839 {
840 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
841 u16 fc = le16_to_cpu(hdr->frame_control);
842 if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
843 fc &= ~IEEE80211_STYPE_QOS_DATA;
844 hdr->frame_control = cpu_to_le16(fc);
845 memmove(skb->data + 2, skb->data, hdrlen - 2);
846 skb_pull(skb, 2);
847 }
848 }
849}
850
Jiri Bencf0706e82007-05-05 11:45:53 -0700851void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
852 struct ieee80211_tx_status *status)
853{
854 struct sk_buff *skb2;
855 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
856 struct ieee80211_local *local = hw_to_local(hw);
857 u16 frag, type;
858 u32 msg_type;
Johannes Bergb306f452007-07-10 19:32:08 +0200859 struct ieee80211_tx_status_rtap_hdr *rthdr;
860 struct ieee80211_sub_if_data *sdata;
861 int monitors;
Jiri Bencf0706e82007-05-05 11:45:53 -0700862
863 if (!status) {
864 printk(KERN_ERR
865 "%s: ieee80211_tx_status called with NULL status\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400866 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -0700867 dev_kfree_skb(skb);
868 return;
869 }
870
871 if (status->excessive_retries) {
872 struct sta_info *sta;
873 sta = sta_info_get(local, hdr->addr1);
874 if (sta) {
875 if (sta->flags & WLAN_STA_PS) {
876 /* The STA is in power save mode, so assume
877 * that this TX packet failed because of that.
878 */
879 status->excessive_retries = 0;
880 status->flags |= IEEE80211_TX_STATUS_TX_FILTERED;
881 }
882 sta_info_put(sta);
883 }
884 }
885
886 if (status->flags & IEEE80211_TX_STATUS_TX_FILTERED) {
887 struct sta_info *sta;
888 sta = sta_info_get(local, hdr->addr1);
889 if (sta) {
890 sta->tx_filtered_count++;
891
892 /* Clear the TX filter mask for this STA when sending
893 * the next packet. If the STA went to power save mode,
894 * this will happen when it is waking up for the next
895 * time. */
896 sta->clear_dst_mask = 1;
897
898 /* TODO: Is the WLAN_STA_PS flag always set here or is
899 * the race between RX and TX status causing some
900 * packets to be filtered out before 80211.o gets an
901 * update for PS status? This seems to be the case, so
902 * no changes are likely to be needed. */
903 if (sta->flags & WLAN_STA_PS &&
904 skb_queue_len(&sta->tx_filtered) <
905 STA_MAX_TX_BUFFER) {
906 ieee80211_remove_tx_extra(local, sta->key,
907 skb,
908 &status->control);
909 skb_queue_tail(&sta->tx_filtered, skb);
910 } else if (!(sta->flags & WLAN_STA_PS) &&
911 !(status->control.flags & IEEE80211_TXCTL_REQUEUE)) {
912 /* Software retry the packet once */
913 status->control.flags |= IEEE80211_TXCTL_REQUEUE;
914 ieee80211_remove_tx_extra(local, sta->key,
915 skb,
916 &status->control);
917 dev_queue_xmit(skb);
918 } else {
919 if (net_ratelimit()) {
920 printk(KERN_DEBUG "%s: dropped TX "
921 "filtered frame queue_len=%d "
922 "PS=%d @%lu\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -0400923 wiphy_name(local->hw.wiphy),
Jiri Bencf0706e82007-05-05 11:45:53 -0700924 skb_queue_len(
925 &sta->tx_filtered),
926 !!(sta->flags & WLAN_STA_PS),
927 jiffies);
928 }
929 dev_kfree_skb(skb);
930 }
931 sta_info_put(sta);
932 return;
933 }
934 } else {
935 /* FIXME: STUPID to call this with both local and local->mdev */
936 rate_control_tx_status(local, local->mdev, skb, status);
937 }
938
939 ieee80211_led_tx(local, 0);
940
941 /* SNMP counters
942 * Fragments are passed to low-level drivers as separate skbs, so these
943 * are actually fragments, not frames. Update frame counters only for
944 * the first fragment of the frame. */
945
946 frag = le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
947 type = le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_FTYPE;
948
949 if (status->flags & IEEE80211_TX_STATUS_ACK) {
950 if (frag == 0) {
951 local->dot11TransmittedFrameCount++;
952 if (is_multicast_ether_addr(hdr->addr1))
953 local->dot11MulticastTransmittedFrameCount++;
954 if (status->retry_count > 0)
955 local->dot11RetryCount++;
956 if (status->retry_count > 1)
957 local->dot11MultipleRetryCount++;
958 }
959
960 /* This counter shall be incremented for an acknowledged MPDU
961 * with an individual address in the address 1 field or an MPDU
962 * with a multicast address in the address 1 field of type Data
963 * or Management. */
964 if (!is_multicast_ether_addr(hdr->addr1) ||
965 type == IEEE80211_FTYPE_DATA ||
966 type == IEEE80211_FTYPE_MGMT)
967 local->dot11TransmittedFragmentCount++;
968 } else {
969 if (frag == 0)
970 local->dot11FailedCount++;
971 }
972
Jiri Bencf0706e82007-05-05 11:45:53 -0700973 msg_type = (status->flags & IEEE80211_TX_STATUS_ACK) ?
974 ieee80211_msg_tx_callback_ack : ieee80211_msg_tx_callback_fail;
975
Johannes Bergb306f452007-07-10 19:32:08 +0200976 /* this was a transmitted frame, but now we want to reuse it */
977 skb_orphan(skb);
978
979 if ((status->control.flags & IEEE80211_TXCTL_REQ_TX_STATUS) &&
980 local->apdev) {
981 if (local->monitors) {
982 skb2 = skb_clone(skb, GFP_ATOMIC);
983 } else {
984 skb2 = skb;
985 skb = NULL;
986 }
987
988 if (skb2)
989 /* Send frame to hostapd */
990 ieee80211_rx_mgmt(local, skb2, NULL, msg_type);
991
992 if (!skb)
993 return;
994 }
995
996 if (!local->monitors) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700997 dev_kfree_skb(skb);
998 return;
999 }
Jiri Bencf0706e82007-05-05 11:45:53 -07001000
Johannes Bergb306f452007-07-10 19:32:08 +02001001 /* send frame to monitor interfaces now */
1002
1003 if (skb_headroom(skb) < sizeof(*rthdr)) {
1004 printk(KERN_ERR "ieee80211_tx_status: headroom too small\n");
1005 dev_kfree_skb(skb);
1006 return;
1007 }
1008
1009 rthdr = (struct ieee80211_tx_status_rtap_hdr*)
1010 skb_push(skb, sizeof(*rthdr));
1011
1012 memset(rthdr, 0, sizeof(*rthdr));
1013 rthdr->hdr.it_len = cpu_to_le16(sizeof(*rthdr));
1014 rthdr->hdr.it_present =
1015 cpu_to_le32((1 << IEEE80211_RADIOTAP_TX_FLAGS) |
1016 (1 << IEEE80211_RADIOTAP_DATA_RETRIES));
1017
1018 if (!(status->flags & IEEE80211_TX_STATUS_ACK) &&
1019 !is_multicast_ether_addr(hdr->addr1))
1020 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_FAIL);
1021
1022 if ((status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS) &&
1023 (status->control.flags & IEEE80211_TXCTL_USE_CTS_PROTECT))
1024 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_CTS);
1025 else if (status->control.flags & IEEE80211_TXCTL_USE_RTS_CTS)
1026 rthdr->tx_flags |= cpu_to_le16(IEEE80211_RADIOTAP_F_TX_RTS);
1027
1028 rthdr->data_retries = status->retry_count;
1029
Johannes Berg79010422007-09-18 17:29:21 -04001030 rcu_read_lock();
Johannes Bergb306f452007-07-10 19:32:08 +02001031 monitors = local->monitors;
Johannes Berg79010422007-09-18 17:29:21 -04001032 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Johannes Bergb306f452007-07-10 19:32:08 +02001033 /*
1034 * Using the monitors counter is possibly racy, but
1035 * if the value is wrong we simply either clone the skb
1036 * once too much or forget sending it to one monitor iface
1037 * The latter case isn't nice but fixing the race is much
1038 * more complicated.
1039 */
1040 if (!monitors || !skb)
1041 goto out;
1042
1043 if (sdata->type == IEEE80211_IF_TYPE_MNTR) {
1044 if (!netif_running(sdata->dev))
1045 continue;
1046 monitors--;
1047 if (monitors)
Johannes Berg79010422007-09-18 17:29:21 -04001048 skb2 = skb_clone(skb, GFP_ATOMIC);
Johannes Bergb306f452007-07-10 19:32:08 +02001049 else
1050 skb2 = NULL;
1051 skb->dev = sdata->dev;
1052 /* XXX: is this sufficient for BPF? */
1053 skb_set_mac_header(skb, 0);
1054 skb->ip_summed = CHECKSUM_UNNECESSARY;
1055 skb->pkt_type = PACKET_OTHERHOST;
1056 skb->protocol = htons(ETH_P_802_2);
1057 memset(skb->cb, 0, sizeof(skb->cb));
1058 netif_rx(skb);
1059 skb = skb2;
Johannes Bergb306f452007-07-10 19:32:08 +02001060 }
1061 }
1062 out:
Johannes Berg79010422007-09-18 17:29:21 -04001063 rcu_read_unlock();
Johannes Bergb306f452007-07-10 19:32:08 +02001064 if (skb)
1065 dev_kfree_skb(skb);
Jiri Bencf0706e82007-05-05 11:45:53 -07001066}
1067EXPORT_SYMBOL(ieee80211_tx_status);
1068
Jiri Bencf0706e82007-05-05 11:45:53 -07001069struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
1070 const struct ieee80211_ops *ops)
1071{
1072 struct net_device *mdev;
1073 struct ieee80211_local *local;
1074 struct ieee80211_sub_if_data *sdata;
1075 int priv_size;
1076 struct wiphy *wiphy;
1077
1078 /* Ensure 32-byte alignment of our private data and hw private data.
1079 * We use the wiphy priv data for both our ieee80211_local and for
1080 * the driver's private data
1081 *
1082 * In memory it'll be like this:
1083 *
1084 * +-------------------------+
1085 * | struct wiphy |
1086 * +-------------------------+
1087 * | struct ieee80211_local |
1088 * +-------------------------+
1089 * | driver's private data |
1090 * +-------------------------+
1091 *
1092 */
1093 priv_size = ((sizeof(struct ieee80211_local) +
1094 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST) +
1095 priv_data_len;
1096
1097 wiphy = wiphy_new(&mac80211_config_ops, priv_size);
1098
1099 if (!wiphy)
1100 return NULL;
1101
1102 wiphy->privid = mac80211_wiphy_privid;
1103
1104 local = wiphy_priv(wiphy);
1105 local->hw.wiphy = wiphy;
1106
1107 local->hw.priv = (char *)local +
1108 ((sizeof(struct ieee80211_local) +
1109 NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
1110
Johannes Berg4480f15c2007-07-10 19:32:10 +02001111 BUG_ON(!ops->tx);
Johannes Berg4150c572007-09-17 01:29:23 -04001112 BUG_ON(!ops->start);
1113 BUG_ON(!ops->stop);
Johannes Berg4480f15c2007-07-10 19:32:10 +02001114 BUG_ON(!ops->config);
1115 BUG_ON(!ops->add_interface);
Johannes Berg4150c572007-09-17 01:29:23 -04001116 BUG_ON(!ops->remove_interface);
1117 BUG_ON(!ops->configure_filter);
Jiri Bencf0706e82007-05-05 11:45:53 -07001118 local->ops = ops;
1119
1120 /* for now, mdev needs sub_if_data :/ */
1121 mdev = alloc_netdev(sizeof(struct ieee80211_sub_if_data),
1122 "wmaster%d", ether_setup);
1123 if (!mdev) {
1124 wiphy_free(wiphy);
1125 return NULL;
1126 }
1127
1128 sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
1129 mdev->ieee80211_ptr = &sdata->wdev;
1130 sdata->wdev.wiphy = wiphy;
1131
1132 local->hw.queues = 1; /* default */
1133
1134 local->mdev = mdev;
1135 local->rx_pre_handlers = ieee80211_rx_pre_handlers;
1136 local->rx_handlers = ieee80211_rx_handlers;
1137 local->tx_handlers = ieee80211_tx_handlers;
1138
1139 local->bridge_packets = 1;
1140
1141 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
1142 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
1143 local->short_retry_limit = 7;
1144 local->long_retry_limit = 4;
1145 local->hw.conf.radio_enabled = 1;
Jiri Bencf0706e82007-05-05 11:45:53 -07001146
Johannes Bergb708e612007-09-14 11:10:25 -04001147 local->enabled_modes = ~0;
Jiri Bencf0706e82007-05-05 11:45:53 -07001148
1149 INIT_LIST_HEAD(&local->modes_list);
1150
Johannes Berg79010422007-09-18 17:29:21 -04001151 INIT_LIST_HEAD(&local->interfaces);
Jiri Bencf0706e82007-05-05 11:45:53 -07001152
1153 INIT_DELAYED_WORK(&local->scan_work, ieee80211_sta_scan_work);
Jiri Bencf0706e82007-05-05 11:45:53 -07001154 ieee80211_rx_bss_list_init(mdev);
1155
1156 sta_info_init(local);
1157
1158 mdev->hard_start_xmit = ieee80211_master_start_xmit;
1159 mdev->open = ieee80211_master_open;
1160 mdev->stop = ieee80211_master_stop;
1161 mdev->type = ARPHRD_IEEE80211;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001162 mdev->header_ops = &ieee80211_header_ops;
Johannes Berg4150c572007-09-17 01:29:23 -04001163 mdev->set_multicast_list = ieee80211_master_set_multicast_list;
Jiri Bencf0706e82007-05-05 11:45:53 -07001164
1165 sdata->type = IEEE80211_IF_TYPE_AP;
1166 sdata->dev = mdev;
1167 sdata->local = local;
1168 sdata->u.ap.force_unicast_rateidx = -1;
1169 sdata->u.ap.max_ratectrl_rateidx = -1;
1170 ieee80211_if_sdata_init(sdata);
Johannes Berg79010422007-09-18 17:29:21 -04001171 /* no RCU needed since we're still during init phase */
1172 list_add_tail(&sdata->list, &local->interfaces);
Jiri Bencf0706e82007-05-05 11:45:53 -07001173
1174 tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
1175 (unsigned long)local);
1176 tasklet_disable(&local->tx_pending_tasklet);
1177
1178 tasklet_init(&local->tasklet,
1179 ieee80211_tasklet_handler,
1180 (unsigned long) local);
1181 tasklet_disable(&local->tasklet);
1182
1183 skb_queue_head_init(&local->skb_queue);
1184 skb_queue_head_init(&local->skb_queue_unreliable);
1185
1186 return local_to_hw(local);
1187}
1188EXPORT_SYMBOL(ieee80211_alloc_hw);
1189
1190int ieee80211_register_hw(struct ieee80211_hw *hw)
1191{
1192 struct ieee80211_local *local = hw_to_local(hw);
1193 const char *name;
1194 int result;
1195
1196 result = wiphy_register(local->hw.wiphy);
1197 if (result < 0)
1198 return result;
1199
1200 name = wiphy_dev(local->hw.wiphy)->driver->name;
1201 local->hw.workqueue = create_singlethread_workqueue(name);
1202 if (!local->hw.workqueue) {
1203 result = -ENOMEM;
1204 goto fail_workqueue;
1205 }
1206
Johannes Bergb306f452007-07-10 19:32:08 +02001207 /*
1208 * The hardware needs headroom for sending the frame,
1209 * and we need some headroom for passing the frame to monitor
1210 * interfaces, but never both at the same time.
1211 */
Jiri Benc33ccad32007-07-18 17:10:44 +02001212 local->tx_headroom = max_t(unsigned int , local->hw.extra_tx_headroom,
1213 sizeof(struct ieee80211_tx_status_rtap_hdr));
Johannes Bergb306f452007-07-10 19:32:08 +02001214
Jiri Bence9f207f2007-05-05 11:46:38 -07001215 debugfs_hw_add(local);
1216
Jiri Bencf0706e82007-05-05 11:45:53 -07001217 local->hw.conf.beacon_int = 1000;
1218
1219 local->wstats_flags |= local->hw.max_rssi ?
1220 IW_QUAL_LEVEL_UPDATED : IW_QUAL_LEVEL_INVALID;
1221 local->wstats_flags |= local->hw.max_signal ?
1222 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
1223 local->wstats_flags |= local->hw.max_noise ?
1224 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
1225 if (local->hw.max_rssi < 0 || local->hw.max_noise < 0)
1226 local->wstats_flags |= IW_QUAL_DBM;
1227
1228 result = sta_info_start(local);
1229 if (result < 0)
1230 goto fail_sta_info;
1231
1232 rtnl_lock();
1233 result = dev_alloc_name(local->mdev, local->mdev->name);
1234 if (result < 0)
1235 goto fail_dev;
1236
1237 memcpy(local->mdev->dev_addr, local->hw.wiphy->perm_addr, ETH_ALEN);
1238 SET_NETDEV_DEV(local->mdev, wiphy_dev(local->hw.wiphy));
1239
1240 result = register_netdevice(local->mdev);
1241 if (result < 0)
1242 goto fail_dev;
1243
Jiri Bence9f207f2007-05-05 11:46:38 -07001244 ieee80211_debugfs_add_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
1245
Jiri Bencf0706e82007-05-05 11:45:53 -07001246 result = ieee80211_init_rate_ctrl_alg(local, NULL);
1247 if (result < 0) {
1248 printk(KERN_DEBUG "%s: Failed to initialize rate control "
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001249 "algorithm\n", wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001250 goto fail_rate;
1251 }
1252
1253 result = ieee80211_wep_init(local);
1254
1255 if (result < 0) {
1256 printk(KERN_DEBUG "%s: Failed to initialize wep\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001257 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001258 goto fail_wep;
1259 }
1260
1261 ieee80211_install_qdisc(local->mdev);
1262
1263 /* add one default STA interface */
1264 result = ieee80211_if_add(local->mdev, "wlan%d", NULL,
1265 IEEE80211_IF_TYPE_STA);
1266 if (result)
1267 printk(KERN_WARNING "%s: Failed to add default virtual iface\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001268 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001269
1270 local->reg_state = IEEE80211_DEV_REGISTERED;
1271 rtnl_unlock();
1272
1273 ieee80211_led_init(local);
1274
1275 return 0;
1276
1277fail_wep:
1278 rate_control_deinitialize(local);
1279fail_rate:
Jiri Bence9f207f2007-05-05 11:46:38 -07001280 ieee80211_debugfs_remove_netdev(IEEE80211_DEV_TO_SUB_IF(local->mdev));
Jiri Bencf0706e82007-05-05 11:45:53 -07001281 unregister_netdevice(local->mdev);
1282fail_dev:
1283 rtnl_unlock();
1284 sta_info_stop(local);
1285fail_sta_info:
Jiri Bence9f207f2007-05-05 11:46:38 -07001286 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001287 destroy_workqueue(local->hw.workqueue);
1288fail_workqueue:
1289 wiphy_unregister(local->hw.wiphy);
1290 return result;
1291}
1292EXPORT_SYMBOL(ieee80211_register_hw);
1293
1294int ieee80211_register_hwmode(struct ieee80211_hw *hw,
1295 struct ieee80211_hw_mode *mode)
1296{
1297 struct ieee80211_local *local = hw_to_local(hw);
1298 struct ieee80211_rate *rate;
1299 int i;
1300
1301 INIT_LIST_HEAD(&mode->list);
1302 list_add_tail(&mode->list, &local->modes_list);
1303
1304 local->hw_modes |= (1 << mode->mode);
1305 for (i = 0; i < mode->num_rates; i++) {
1306 rate = &(mode->rates[i]);
1307 rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
1308 }
1309 ieee80211_prepare_rates(local, mode);
1310
1311 if (!local->oper_hw_mode) {
1312 /* Default to this mode */
1313 local->hw.conf.phymode = mode->mode;
1314 local->oper_hw_mode = local->scan_hw_mode = mode;
1315 local->oper_channel = local->scan_channel = &mode->channels[0];
1316 local->hw.conf.mode = local->oper_hw_mode;
1317 local->hw.conf.chan = local->oper_channel;
1318 }
1319
1320 if (!(hw->flags & IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED))
Daniel Drakefd8bacc2007-06-09 19:07:14 +01001321 ieee80211_set_default_regdomain(mode);
Jiri Bencf0706e82007-05-05 11:45:53 -07001322
1323 return 0;
1324}
1325EXPORT_SYMBOL(ieee80211_register_hwmode);
1326
1327void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1328{
1329 struct ieee80211_local *local = hw_to_local(hw);
1330 struct ieee80211_sub_if_data *sdata, *tmp;
Jiri Bencf0706e82007-05-05 11:45:53 -07001331 int i;
1332
1333 tasklet_kill(&local->tx_pending_tasklet);
1334 tasklet_kill(&local->tasklet);
1335
1336 rtnl_lock();
1337
1338 BUG_ON(local->reg_state != IEEE80211_DEV_REGISTERED);
1339
1340 local->reg_state = IEEE80211_DEV_UNREGISTERED;
1341 if (local->apdev)
1342 ieee80211_if_del_mgmt(local);
1343
Johannes Berg79010422007-09-18 17:29:21 -04001344 /*
1345 * At this point, interface list manipulations are fine
1346 * because the driver cannot be handing us frames any
1347 * more and the tasklet is killed.
1348 */
1349 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list)
Jiri Bencf0706e82007-05-05 11:45:53 -07001350 __ieee80211_if_del(local, sdata);
1351
1352 rtnl_unlock();
1353
Jiri Bencf0706e82007-05-05 11:45:53 -07001354 ieee80211_rx_bss_list_deinit(local->mdev);
1355 ieee80211_clear_tx_pending(local);
1356 sta_info_stop(local);
1357 rate_control_deinitialize(local);
Jiri Bence9f207f2007-05-05 11:46:38 -07001358 debugfs_hw_del(local);
Jiri Bencf0706e82007-05-05 11:45:53 -07001359
1360 for (i = 0; i < NUM_IEEE80211_MODES; i++) {
1361 kfree(local->supp_rates[i]);
1362 kfree(local->basic_rates[i]);
1363 }
1364
1365 if (skb_queue_len(&local->skb_queue)
1366 || skb_queue_len(&local->skb_queue_unreliable))
1367 printk(KERN_WARNING "%s: skb_queue not empty\n",
Johannes Bergdd1cd4c2007-09-18 17:29:20 -04001368 wiphy_name(local->hw.wiphy));
Jiri Bencf0706e82007-05-05 11:45:53 -07001369 skb_queue_purge(&local->skb_queue);
1370 skb_queue_purge(&local->skb_queue_unreliable);
1371
1372 destroy_workqueue(local->hw.workqueue);
1373 wiphy_unregister(local->hw.wiphy);
1374 ieee80211_wep_free(local);
1375 ieee80211_led_exit(local);
1376}
1377EXPORT_SYMBOL(ieee80211_unregister_hw);
1378
1379void ieee80211_free_hw(struct ieee80211_hw *hw)
1380{
1381 struct ieee80211_local *local = hw_to_local(hw);
1382
1383 ieee80211_if_free(local->mdev);
1384 wiphy_free(local->hw.wiphy);
1385}
1386EXPORT_SYMBOL(ieee80211_free_hw);
1387
Jiri Bencf0706e82007-05-05 11:45:53 -07001388static int __init ieee80211_init(void)
1389{
1390 struct sk_buff *skb;
1391 int ret;
1392
1393 BUILD_BUG_ON(sizeof(struct ieee80211_tx_packet_data) > sizeof(skb->cb));
1394
1395 ret = ieee80211_wme_register();
1396 if (ret) {
1397 printk(KERN_DEBUG "ieee80211_init: failed to "
1398 "initialize WME (err=%d)\n", ret);
1399 return ret;
1400 }
1401
Jiri Bence9f207f2007-05-05 11:46:38 -07001402 ieee80211_debugfs_netdev_init();
Daniel Drakefd8bacc2007-06-09 19:07:14 +01001403 ieee80211_regdomain_init();
Jiri Bence9f207f2007-05-05 11:46:38 -07001404
Jiri Bencf0706e82007-05-05 11:45:53 -07001405 return 0;
1406}
1407
Jiri Bencf0706e82007-05-05 11:45:53 -07001408static void __exit ieee80211_exit(void)
1409{
1410 ieee80211_wme_unregister();
Jiri Bence9f207f2007-05-05 11:46:38 -07001411 ieee80211_debugfs_netdev_exit();
Jiri Bencf0706e82007-05-05 11:45:53 -07001412}
1413
1414
Johannes Bergca9938f2007-09-11 12:50:32 +02001415subsys_initcall(ieee80211_init);
Jiri Bencf0706e82007-05-05 11:45:53 -07001416module_exit(ieee80211_exit);
1417
1418MODULE_DESCRIPTION("IEEE 802.11 subsystem");
1419MODULE_LICENSE("GPL");