add devicescape 802.11 stack
authorFelix Fietkau <nbd@openwrt.org>
Wed, 28 Dec 2005 13:22:51 +0000 (13:22 +0000)
committerFelix Fietkau <nbd@openwrt.org>
Wed, 28 Dec 2005 13:22:51 +0000 (13:22 +0000)
SVN-Revision: 2791

39 files changed:
openwrt/target/linux/package/Config.in
openwrt/target/linux/package/Makefile
openwrt/target/linux/package/ieee80211-dscape/Config.in [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/Makefile [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/ipkg/kmod-ieee80211-dscape.control [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/Kconfig [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/Makefile [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/aes.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/aes_ccm.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/aes_ccm.h [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/fifo_qdisc.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/hostapd_ioctl.h [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/ieee80211.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/ieee80211_i.h [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/ieee80211_ioctl.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/ieee80211_key.h [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/ieee80211_led.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/ieee80211_proc.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/ieee80211_proc.h [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/ieee80211_scan.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/ieee80211_sta.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/include/net/ieee80211.h [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/include/net/ieee80211_common.h [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/include/net/ieee80211_mgmt.h [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/include/net/ieee80211_shared.h [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/michael.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/michael.h [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/rate_control.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/rate_control.h [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/sta_info.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/sta_info.h [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/tkip.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/tkip.h [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/wep.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/wep.h [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/wme.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/wme.h [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/wpa.c [new file with mode: 0644]
openwrt/target/linux/package/ieee80211-dscape/src/wpa.h [new file with mode: 0644]

index 66c84394ee33174120e87686f149b4d8bd238c7f..068024b147cb542eb48200eb514db4b26cb57abb 100644 (file)
@@ -2,6 +2,7 @@ source "target/linux/package/alsa/Config.in"
 source "target/linux/package/diag/Config.in"
 source "target/linux/package/fuse/Config.in"
 source "target/linux/package/hostap/Config.in"
+source "target/linux/package/ieee80211-dscape/Config.in"
 source "target/linux/package/madwifi/Config.in"
 source "target/linux/package/mini_fo/Config.in"
 source "target/linux/package/openswan/Config.in"
index 6dc49d96aa766b6b952d3022982895ecb8217952..de24c77c236c77c0e2399450924dc69fe8b86992 100644 (file)
@@ -6,6 +6,7 @@ package-$(BR2_PACKAGE_KMOD_ALSA) += alsa
 endif
 package-$(BR2_PACKAGE_KMOD_FUSE) += fuse
 package-$(BR2_PACKAGE_KMOD_HOSTAP) += hostap
+package-$(BR2_PACKAGE_KMOD_IEEE80211_DSCAPE) += ieee80211-dscape
 package-$(BR2_PACKAGE_KMOD_MADWIFI) += madwifi
 package-$(BR2_PACKAGE_KMOD_MINI_FO) += mini_fo
 package-$(BR2_PACKAGE_KMOD_OPENSWAN) += openswan
diff --git a/openwrt/target/linux/package/ieee80211-dscape/Config.in b/openwrt/target/linux/package/ieee80211-dscape/Config.in
new file mode 100644 (file)
index 0000000..a29b304
--- /dev/null
@@ -0,0 +1,10 @@
+config BR2_PACKAGE_KMOD_IEEE80211_DSCAPE
+       prompt "kmod-ieee80211-dscape............. IEEE 802.11 stack (devicescape)"
+       tristate
+       default y if BR2_LINUX_2_6_BRCM
+       default m if CONFIG_DEVEL
+       default n
+       help
+         IEEE 802.11 stack (devicescape)
+         
+         http://ftp.kernel.org/pub/linux/kernel/people/jbenc/
diff --git a/openwrt/target/linux/package/ieee80211-dscape/Makefile b/openwrt/target/linux/package/ieee80211-dscape/Makefile
new file mode 100644 (file)
index 0000000..90d55b3
--- /dev/null
@@ -0,0 +1,51 @@
+# $Id$
+
+include $(TOPDIR)/rules.mk
+include ../../rules.mk
+
+PKG_NAME := kmod-ieee80211-dscape
+PKG_RELEASE := 1
+PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)
+
+include $(TOPDIR)/package/rules.mk
+
+$(eval $(call PKG_template,KMOD_IEEE80211_DSCAPE,$(PKG_NAME),$(LINUX_VERSION)-$(BOARD)-$(PKG_RELEASE),$(ARCH),kernel ($(LINUX_VERSION)-$(BOARD)-$(KERNEL_RELEASE))))
+
+ifeq ($(KERNEL_DIR),)
+KERNEL_DIR:=$(LINUX_DIR)
+endif
+KERNEL_VERSION=$(shell echo "$(LINUX_VERSION)" | cut -d. -f1,2)
+
+$(PKG_BUILD_DIR)/.prepared:
+       mkdir -p $(PKG_BUILD_DIR)
+       cp -fpR ./src/* $(PKG_BUILD_DIR)/
+       touch $@
+
+$(PKG_BUILD_DIR)/.configured:
+       touch $@
+
+$(PKG_BUILD_DIR)/.built:
+       $(MAKE) -C "$(KERNEL_DIR)" \
+               CROSS_COMPILE="$(TARGET_CROSS)" \
+               ARCH="$(LINUX_KARCH)" \
+               PATH="$(TARGET_PATH)" \
+               SUBDIRS="$(PKG_BUILD_DIR)" \
+               LINUXINCLUDE="-I$(shell pwd)/src/include -I$(KERNEL_DIR)/include" \
+               modules
+       touch $@
+       
+$(IPKG_KMOD_IEEE80211_DSCAPE):
+       install -m0755 -d $(IDIR_KMOD_IEEE80211_DSCAPE)/lib/modules/$(LINUX_VERSION)
+       #install -m0755 -d $(IDIR_KMOD_IEEE80211_DSCAPE)/etc/modules.d
+       cp -fpR $(PKG_BUILD_DIR)/*.ko $(IDIR_KMOD_IEEE80211_DSCAPE)/lib/modules/$(LINUX_VERSION)
+       $(IPKG_BUILD) $(IDIR_KMOD_IEEE80211_DSCAPE) $(PACKAGE_DIR)
+
+install-dev:
+       mkdir -p $(STAGING_DIR)/usr/include/ieee80211-dscape
+       cp -fpR src/include/* $(STAGING_DIR)/usr/include/ieee80211-dscape/
+
+uninstall-dev:
+       rm -rf $(STAGING_DIR)/usr/include/ieee80211-dscape
+
+compile-targets: install-dev
+clean-targets: uninstall-dev
diff --git a/openwrt/target/linux/package/ieee80211-dscape/ipkg/kmod-ieee80211-dscape.control b/openwrt/target/linux/package/ieee80211-dscape/ipkg/kmod-ieee80211-dscape.control
new file mode 100644 (file)
index 0000000..f510d2d
--- /dev/null
@@ -0,0 +1,4 @@
+Package: kmod-ieee80211-dscape
+Priority: optional
+Section: sys
+Description: IEEE 802.11 stack (devicescape)
diff --git a/openwrt/target/linux/package/ieee80211-dscape/src/Kconfig b/openwrt/target/linux/package/ieee80211-dscape/src/Kconfig
new file mode 100644 (file)
index 0000000..052a419
--- /dev/null
@@ -0,0 +1,32 @@
+config IEEE80211
+       tristate "Generic IEEE 802.11 Networking Stack"
+       ---help---
+       This option enables the hardware independent IEEE 802.11
+       networking stack.
+
+config IEEE80211_DEBUG
+       bool "Enable debugging output"
+       depends on IEEE80211
+       ---help---
+         This option will enable debug tracing output for the
+         ieee80211 network stack.
+
+         If you are not trying to debug or develop the ieee80211
+         subsystem, you most likely want to say N here.
+
+config IEEE80211_VERBOSE_DEBUG
+       bool "Verbose debugging output"
+       depends on IEEE80211_DEBUG
+
+config TKIP_DEBUG
+       bool "TKIP debugging"
+       depends on IEEE80211_DEBUG
+
+config IEEE80211_DEBUG_COUNTERS
+       bool "Extra statistics for TX/RX debugging"
+       depends on IEEE80211_DEBUG
+
+config HOSTAPD_WPA_TESTING
+       bool "Support for TKIP countermeasures testing"
+       depends on IEEE80211_DEBUG
+
diff --git a/openwrt/target/linux/package/ieee80211-dscape/src/Makefile b/openwrt/target/linux/package/ieee80211-dscape/src/Makefile
new file mode 100644 (file)
index 0000000..ac4ab88
--- /dev/null
@@ -0,0 +1,23 @@
+obj-$(CONFIG_IEEE80211) += 80211.o rate_control.o
+
+80211-objs := \
+       ieee80211.o \
+       ieee80211_ioctl.o \
+       sta_info.o \
+       wep.o \
+       wpa.o \
+       ieee80211_proc.o \
+       ieee80211_scan.o \
+       ieee80211_sta.o \
+       michael.o \
+       tkip.o \
+       aes_ccm.o \
+       wme.o
+
+ifeq ($(CONFIG_NET_SCHED),)
+  80211-objs += fifo_qdisc.o
+endif
+
+ifeq ($(CONFIG_IEEE80211_LEDS),y)
+  80211-objs += ieee80211_led.o
+endif
diff --git a/openwrt/target/linux/package/ieee80211-dscape/src/aes.c b/openwrt/target/linux/package/ieee80211-dscape/src/aes.c
new file mode 100644 (file)
index 0000000..085e4a9
--- /dev/null
@@ -0,0 +1,564 @@
+/* Based on Rijndael implementation that has been placed in the public domain,
+ * although heavily modified.
+ *
+ * Modifications Copyright 2003, Instant802 Networks, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * Optimized both speed and size by removing not used key lengths (only
+ * 128-bit is used in IEEE 802.11i).
+ */
+
+/* Use 256-byte Te4 table instead of larger 1024-byte */
+#define SMALL_TE4
+
+/* Save data size by using only one 1k table, but with a drawback of having to
+ * rotate entries at lookup. This can be useful, if the CPU supports free
+ * rotate on memory read. However, if this is not the case, this is much slower
+ * than four-table implementation. */
+/* #define ONLY_ONE_TABLE */
+
+
+/* --- start of code that is based on public domain AES implementation --- */
+
+/**
+ * rijndael-alg-fst.c
+ *
+ * @version 3.0 (December 2000)
+ *
+ * Optimised ANSI C code for the Rijndael cipher (now AES)
+ *
+ * @author Vincent Rijmen <vincent.rijmen@esat.kuleuven.ac.be>
+ * @author Antoon Bosselaers <antoon.bosselaers@esat.kuleuven.ac.be>
+ * @author Paulo Barreto <paulo.barreto@terra.com.br>
+ *
+ * This code is hereby placed in the public domain.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+Te0[x] = S [x].[02, 01, 01, 03];
+Te1[x] = S [x].[03, 02, 01, 01];
+Te2[x] = S [x].[01, 03, 02, 01];
+Te3[x] = S [x].[01, 01, 03, 02];
+Te4[x] = S [x].[01, 01, 01, 01];
+*/
+
+static const u32 Te0[256] =
+{
+       0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU,
+       0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U,
+       0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU,
+       0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU,
+       0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U,
+       0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU,
+       0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU,
+       0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU,
+       0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU,
+       0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU,
+       0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U,
+       0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU,
+       0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU,
+       0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U,
+       0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU,
+       0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU,
+       0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU,
+       0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU,
+       0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU,
+       0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U,
+       0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU,
+       0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU,
+       0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU,
+       0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU,
+       0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U,
+       0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U,
+       0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U,
+       0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U,
+       0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU,
+       0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U,
+       0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U,
+       0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU,
+       0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU,
+       0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U,
+       0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U,
+       0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U,
+       0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU,
+       0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U,
+       0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU,
+       0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U,
+       0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU,
+       0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U,
+       0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U,
+       0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU,
+       0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U,
+       0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U,
+       0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U,
+       0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U,
+       0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U,
+       0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U,
+       0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U,
+       0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U,
+       0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU,
+       0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U,
+       0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U,
+       0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U,
+       0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U,
+       0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U,
+       0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U,
+       0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU,
+       0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U,
+       0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U,
+       0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U,
+       0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU,
+};
+
+#ifndef ONLY_ONE_TABLE
+static const u32 Te1[256] =
+{
+       0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU,
+       0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U,
+       0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU,
+       0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U,
+       0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU,
+       0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U,
+       0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU,
+       0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U,
+       0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U,
+       0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU,
+       0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U,
+       0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U,
+       0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U,
+       0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU,
+       0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U,
+       0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U,
+       0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU,
+       0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U,
+       0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U,
+       0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U,
+       0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU,
+       0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU,
+       0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U,
+       0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU,
+       0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU,
+       0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U,
+       0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU,
+       0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U,
+       0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU,
+       0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U,
+       0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U,
+       0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U,
+       0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU,
+       0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U,
+       0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU,
+       0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U,
+       0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU,
+       0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U,
+       0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U,
+       0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU,
+       0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU,
+       0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU,
+       0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U,
+       0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U,
+       0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU,
+       0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U,
+       0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU,
+       0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U,
+       0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU,
+       0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U,
+       0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU,
+       0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU,
+       0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U,
+       0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU,
+       0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U,
+       0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU,
+       0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U,
+       0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U,
+       0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U,
+       0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU,
+       0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU,
+       0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U,
+       0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU,
+       0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U,
+};
+
+static const u32 Te2[256] =
+{
+       0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU,
+       0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U,
+       0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU,
+       0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U,
+       0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU,
+       0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U,
+       0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU,
+       0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U,
+       0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U,
+       0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU,
+       0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U,
+       0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U,
+       0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U,
+       0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU,
+       0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U,
+       0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U,
+       0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU,
+       0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U,
+       0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U,
+       0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U,
+       0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU,
+       0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU,
+       0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U,
+       0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU,
+       0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU,
+       0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U,
+       0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU,
+       0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U,
+       0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU,
+       0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U,
+       0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U,
+       0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U,
+       0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU,
+       0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U,
+       0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU,
+       0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U,
+       0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU,
+       0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U,
+       0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U,
+       0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU,
+       0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU,
+       0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU,
+       0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U,
+       0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U,
+       0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU,
+       0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U,
+       0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU,
+       0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U,
+       0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU,
+       0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U,
+       0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU,
+       0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU,
+       0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U,
+       0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU,
+       0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U,
+       0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU,
+       0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U,
+       0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U,
+       0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U,
+       0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU,
+       0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU,
+       0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U,
+       0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU,
+       0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U,
+};
+
+static const u32 Te3[256] =
+{
+       0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U,
+       0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U,
+       0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U,
+       0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU,
+       0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU,
+       0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU,
+       0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U,
+       0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU,
+       0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU,
+       0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U,
+       0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U,
+       0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU,
+       0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU,
+       0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU,
+       0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU,
+       0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU,
+       0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U,
+       0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU,
+       0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU,
+       0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U,
+       0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U,
+       0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U,
+       0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U,
+       0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U,
+       0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU,
+       0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U,
+       0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU,
+       0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU,
+       0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U,
+       0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U,
+       0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U,
+       0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU,
+       0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U,
+       0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU,
+       0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU,
+       0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U,
+       0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U,
+       0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU,
+       0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U,
+       0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU,
+       0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U,
+       0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U,
+       0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U,
+       0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U,
+       0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU,
+       0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U,
+       0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU,
+       0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U,
+       0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU,
+       0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U,
+       0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU,
+       0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU,
+       0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU,
+       0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU,
+       0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U,
+       0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U,
+       0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U,
+       0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U,
+       0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U,
+       0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U,
+       0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU,
+       0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U,
+       0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU,
+       0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU,
+};
+
+#define TE0(v) (Te0[(v) >> 24])
+#define TE1(v) (Te1[((v) >> 16) & 0xff])
+#define TE2(v) (Te2[((v) >> 8) & 0xff])
+#define TE3(v) (Te3[(v) & 0xff])
+
+#else /* ONLY_ONE_TABLE */
+
+
+static inline u32 ROR8(u32 v)
+{
+       return (v >> 8) | (v << 24);
+}
+
+static inline u32 ROR16(u32 v)
+{
+       return (v >> 16) | (v << 16);
+}
+
+static inline u32 ROR24(u32 v)
+{
+       return (v >> 24) | (v << 8);
+}
+
+#define TE0(v) (Te0[(v) >> 24])
+#define TE1(v) (ROR8(Te0[((v) >> 16) & 0xff]))
+#define TE2(v) (ROR16(Te0[((v) >> 8) & 0xff]))
+#define TE3(v) (ROR24(Te0[(v) & 0xff]))
+
+#endif /* ONLY_ONE_TABLE */
+
+
+
+#ifdef SMALL_TE4
+static const u8 Te4s[256] = {
+       0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
+       0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
+       0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
+       0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
+       0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc,
+       0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
+       0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a,
+       0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
+       0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
+       0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
+       0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b,
+       0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
+       0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85,
+       0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
+       0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
+       0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
+       0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17,
+       0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
+       0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88,
+       0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
+       0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
+       0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
+       0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9,
+       0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
+       0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
+       0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
+       0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
+       0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
+       0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94,
+       0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
+       0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68,
+       0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16,
+};
+
+#define TE4_1(v) (Te4s[(v) & 0xff] << 24)
+#define TE4_2(v) (Te4s[(v) & 0xff] << 16)
+#define TE4_3(v) (Te4s[(v) & 0xff] << 8)
+#define TE4_4(v) (Te4s[(v) & 0xff])
+
+#else /* SMALL_TE4 */
+
+static const u32 Te4[256] =
+{
+       0x63636363U, 0x7c7c7c7cU, 0x77777777U, 0x7b7b7b7bU,
+       0xf2f2f2f2U, 0x6b6b6b6bU, 0x6f6f6f6fU, 0xc5c5c5c5U,
+       0x30303030U, 0x01010101U, 0x67676767U, 0x2b2b2b2bU,
+       0xfefefefeU, 0xd7d7d7d7U, 0xababababU, 0x76767676U,
+       0xcacacacaU, 0x82828282U, 0xc9c9c9c9U, 0x7d7d7d7dU,
+       0xfafafafaU, 0x59595959U, 0x47474747U, 0xf0f0f0f0U,
+       0xadadadadU, 0xd4d4d4d4U, 0xa2a2a2a2U, 0xafafafafU,
+       0x9c9c9c9cU, 0xa4a4a4a4U, 0x72727272U, 0xc0c0c0c0U,
+       0xb7b7b7b7U, 0xfdfdfdfdU, 0x93939393U, 0x26262626U,
+       0x36363636U, 0x3f3f3f3fU, 0xf7f7f7f7U, 0xccccccccU,
+       0x34343434U, 0xa5a5a5a5U, 0xe5e5e5e5U, 0xf1f1f1f1U,
+       0x71717171U, 0xd8d8d8d8U, 0x31313131U, 0x15151515U,
+       0x04040404U, 0xc7c7c7c7U, 0x23232323U, 0xc3c3c3c3U,
+       0x18181818U, 0x96969696U, 0x05050505U, 0x9a9a9a9aU,
+       0x07070707U, 0x12121212U, 0x80808080U, 0xe2e2e2e2U,
+       0xebebebebU, 0x27272727U, 0xb2b2b2b2U, 0x75757575U,
+       0x09090909U, 0x83838383U, 0x2c2c2c2cU, 0x1a1a1a1aU,
+       0x1b1b1b1bU, 0x6e6e6e6eU, 0x5a5a5a5aU, 0xa0a0a0a0U,
+       0x52525252U, 0x3b3b3b3bU, 0xd6d6d6d6U, 0xb3b3b3b3U,
+       0x29292929U, 0xe3e3e3e3U, 0x2f2f2f2fU, 0x84848484U,
+       0x53535353U, 0xd1d1d1d1U, 0x00000000U, 0xededededU,
+       0x20202020U, 0xfcfcfcfcU, 0xb1b1b1b1U, 0x5b5b5b5bU,
+       0x6a6a6a6aU, 0xcbcbcbcbU, 0xbebebebeU, 0x39393939U,
+       0x4a4a4a4aU, 0x4c4c4c4cU, 0x58585858U, 0xcfcfcfcfU,
+       0xd0d0d0d0U, 0xefefefefU, 0xaaaaaaaaU, 0xfbfbfbfbU,
+       0x43434343U, 0x4d4d4d4dU, 0x33333333U, 0x85858585U,
+       0x45454545U, 0xf9f9f9f9U, 0x02020202U, 0x7f7f7f7fU,
+       0x50505050U, 0x3c3c3c3cU, 0x9f9f9f9fU, 0xa8a8a8a8U,
+       0x51515151U, 0xa3a3a3a3U, 0x40404040U, 0x8f8f8f8fU,
+       0x92929292U, 0x9d9d9d9dU, 0x38383838U, 0xf5f5f5f5U,
+       0xbcbcbcbcU, 0xb6b6b6b6U, 0xdadadadaU, 0x21212121U,
+       0x10101010U, 0xffffffffU, 0xf3f3f3f3U, 0xd2d2d2d2U,
+       0xcdcdcdcdU, 0x0c0c0c0cU, 0x13131313U, 0xececececU,
+       0x5f5f5f5fU, 0x97979797U, 0x44444444U, 0x17171717U,
+       0xc4c4c4c4U, 0xa7a7a7a7U, 0x7e7e7e7eU, 0x3d3d3d3dU,
+       0x64646464U, 0x5d5d5d5dU, 0x19191919U, 0x73737373U,
+       0x60606060U, 0x81818181U, 0x4f4f4f4fU, 0xdcdcdcdcU,
+       0x22222222U, 0x2a2a2a2aU, 0x90909090U, 0x88888888U,
+       0x46464646U, 0xeeeeeeeeU, 0xb8b8b8b8U, 0x14141414U,
+       0xdedededeU, 0x5e5e5e5eU, 0x0b0b0b0bU, 0xdbdbdbdbU,
+       0xe0e0e0e0U, 0x32323232U, 0x3a3a3a3aU, 0x0a0a0a0aU,
+       0x49494949U, 0x06060606U, 0x24242424U, 0x5c5c5c5cU,
+       0xc2c2c2c2U, 0xd3d3d3d3U, 0xacacacacU, 0x62626262U,
+       0x91919191U, 0x95959595U, 0xe4e4e4e4U, 0x79797979U,
+       0xe7e7e7e7U, 0xc8c8c8c8U, 0x37373737U, 0x6d6d6d6dU,
+       0x8d8d8d8dU, 0xd5d5d5d5U, 0x4e4e4e4eU, 0xa9a9a9a9U,
+       0x6c6c6c6cU, 0x56565656U, 0xf4f4f4f4U, 0xeaeaeaeaU,
+       0x65656565U, 0x7a7a7a7aU, 0xaeaeaeaeU, 0x08080808U,
+       0xbabababaU, 0x78787878U, 0x25252525U, 0x2e2e2e2eU,
+       0x1c1c1c1cU, 0xa6a6a6a6U, 0xb4b4b4b4U, 0xc6c6c6c6U,
+       0xe8e8e8e8U, 0xddddddddU, 0x74747474U, 0x1f1f1f1fU,
+       0x4b4b4b4bU, 0xbdbdbdbdU, 0x8b8b8b8bU, 0x8a8a8a8aU,
+       0x70707070U, 0x3e3e3e3eU, 0xb5b5b5b5U, 0x66666666U,
+       0x48484848U, 0x03030303U, 0xf6f6f6f6U, 0x0e0e0e0eU,
+       0x61616161U, 0x35353535U, 0x57575757U, 0xb9b9b9b9U,
+       0x86868686U, 0xc1c1c1c1U, 0x1d1d1d1dU, 0x9e9e9e9eU,
+       0xe1e1e1e1U, 0xf8f8f8f8U, 0x98989898U, 0x11111111U,
+       0x69696969U, 0xd9d9d9d9U, 0x8e8e8e8eU, 0x94949494U,
+       0x9b9b9b9bU, 0x1e1e1e1eU, 0x87878787U, 0xe9e9e9e9U,
+       0xcecececeU, 0x55555555U, 0x28282828U, 0xdfdfdfdfU,
+       0x8c8c8c8cU, 0xa1a1a1a1U, 0x89898989U, 0x0d0d0d0dU,
+       0xbfbfbfbfU, 0xe6e6e6e6U, 0x42424242U, 0x68686868U,
+       0x41414141U, 0x99999999U, 0x2d2d2d2dU, 0x0f0f0f0fU,
+       0xb0b0b0b0U, 0x54545454U, 0xbbbbbbbbU, 0x16161616U,
+};
+
+#define TE4_1(v) (Te4[(v) & 0xff] & 0xff000000)
+#define TE4_2(v) (Te4[(v) & 0xff] & 0x00ff0000)
+#define TE4_3(v) (Te4[(v) & 0xff] & 0x0000ff00)
+#define TE4_4(v) (Te4[(v) & 0xff] & 0x000000ff)
+
+#endif /* SMALL_TE4 */
+
+
+static const u32 rcon[] = {
+       0x01000000, 0x02000000, 0x04000000, 0x08000000, 0x10000000,
+       0x20000000, 0x40000000, 0x80000000, 0x1B000000, 0x36000000,
+};
+
+#define GETU32(pt) \
+(((u32)(pt)[0] << 24) ^ ((u32)(pt)[1] << 16) ^ ((u32)(pt)[2] <<  8) ^ \
+((u32)(pt)[3]))
+#define PUTU32(ct, st) \
+{ (ct)[0] = (u8)((st) >> 24); (ct)[1] = (u8)((st) >> 16); \
+(ct)[2] = (u8)((st) >>  8); (ct)[3] = (u8)(st); }
+
+
+/* Expand the cipher key into the encryption key schedule. */
+void ieee80211_aes_key_setup_encrypt(u32 rk[/*44*/], const u8 key[])
+{
+       int i;
+       u32 temp;
+
+       rk[0] = GETU32(key     );
+       rk[1] = GETU32(key +  4);
+       rk[2] = GETU32(key +  8);
+       rk[3] = GETU32(key + 12);
+
+       for (i = 0; i < 10; i++) {
+               temp = rk[3];
+               rk[4] = rk[0] ^ TE4_1(temp >> 16) ^ TE4_2(temp >>  8) ^
+                       TE4_3(temp) ^ TE4_4(temp >> 24) ^ rcon[i];
+               rk[5] = rk[1] ^ rk[4];
+               rk[6] = rk[2] ^ rk[5];
+               rk[7] = rk[3] ^ rk[6];
+               rk += 4;
+       }
+}
+
+
+void ieee80211_aes_encrypt(const u32 rk[/*44*/], const u8 pt[16], u8 ct[16])
+{
+       const int Nr = 10;
+       u32 s0, s1, s2, s3, t0, t1, t2, t3;
+
+       /* Map byte array block to cipher state and add initial round key */
+       s0 = GETU32(pt     ) ^ rk[0];
+       s1 = GETU32(pt +  4) ^ rk[1];
+       s2 = GETU32(pt +  8) ^ rk[2];
+       s3 = GETU32(pt + 12) ^ rk[3];
+
+#define ROUND(r,d,s,i) \
+d##0 = TE0(s##0) ^ TE1(s##1) ^ TE2(s##2) ^ TE3(s##3) ^ rk[i]; \
+d##1 = TE0(s##1) ^ TE1(s##2) ^ TE2(s##3) ^ TE3(s##0) ^ rk[i + 1]; \
+d##2 = TE0(s##2) ^ TE1(s##3) ^ TE2(s##0) ^ TE3(s##1) ^ rk[i + 2]; \
+d##3 = TE0(s##3) ^ TE1(s##0) ^ TE2(s##1) ^ TE3(s##2) ^ rk[i + 3]
+       ROUND(1,t,s,4);
+       ROUND(2,s,t,8);
+       ROUND(3,t,s,12);
+       ROUND(4,s,t,16);
+       ROUND(5,t,s,20);
+       ROUND(6,s,t,24);
+       ROUND(7,t,s,28);
+       ROUND(8,s,t,32);
+       ROUND(9,t,s,36);
+#undef ROUND
+
+       rk += Nr << 2;
+
+       /* Apply the last round and map cipher state to byte array block */
+       s0 = TE4_1(t0 >> 24) ^ TE4_2(t1 >> 16) ^ TE4_3(t2 >> 8) ^ TE4_4(t3) ^
+               rk[0];
+       PUTU32(ct, s0);
+       s0 = TE4_1(t1 >> 24) ^ TE4_2(t2 >> 16) ^ TE4_3(t3 >> 8) ^ TE4_4(t0) ^
+               rk[1];
+       PUTU32(ct + 4, s0);
+       s0 = TE4_1(t2 >> 24) ^ TE4_2(t3 >> 16) ^ TE4_3(t0 >> 8) ^ TE4_4(t1) ^
+               rk[2];
+       PUTU32(ct + 8, s0);
+       s0 = TE4_1(t3 >> 24) ^ TE4_2(t0 >> 16) ^ TE4_3(t1 >> 8) ^ TE4_4(t2) ^
+               rk[3];
+       PUTU32(ct + 12, s0);
+}
+
+/* --- end of code that is based on public domain AES implementation --- */
diff --git a/openwrt/target/linux/package/ieee80211-dscape/src/aes_ccm.c b/openwrt/target/linux/package/ieee80211-dscape/src/aes_ccm.c
new file mode 100644 (file)
index 0000000..bbc1f7b
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2003-2004, Instant802 Networks, Inc.
+ * Copyright 2005, Devicescape Software, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/types.h>
+#include <linux/netdevice.h>
+
+#include <net/ieee80211.h>
+#include "ieee80211_key.h"
+#include "aes_ccm.h"
+
+#include "aes.c"
+
+static inline void aes_ccm_prepare(u32 *rk, u8 *b_0, u8 *aad, u8 *b,
+                                  u8 *s_0, u8 *a)
+{
+       int i;
+
+       ieee80211_aes_encrypt(rk, b_0, b);
+
+       /* Extra Authenticate-only data (always two AES blocks) */
+       for (i = 0; i < AES_BLOCK_LEN; i++)
+               aad[i] ^= b[i];
+       ieee80211_aes_encrypt(rk, aad, b);
+
+       aad += AES_BLOCK_LEN;
+
+       for (i = 0; i < AES_BLOCK_LEN; i++)
+               aad[i] ^= b[i];
+       ieee80211_aes_encrypt(rk, aad, a);
+
+       /* Mask out bits from auth-only-b_0 */
+       b_0[0] &= 0x07;
+
+       /* S_0 is used to encrypt T (= MIC) */
+       b_0[14] = 0;
+       b_0[15] = 0;
+       ieee80211_aes_encrypt(rk, b_0, s_0);
+}
+
+
+void ieee80211_aes_ccm_encrypt(u32 *rk, u8 *b_0, u8 *aad, u8 *data,
+                              size_t data_len, u8 *cdata, u8 *mic)
+{
+       int i, j, last_len, num_blocks;
+       u8 *pos, *cpos;
+       u8 b[AES_BLOCK_LEN], s_0[AES_BLOCK_LEN], e[AES_BLOCK_LEN];
+
+       num_blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
+       last_len = data_len % AES_BLOCK_LEN;
+       aes_ccm_prepare(rk, b_0, aad, b, s_0, b);
+
+       /* Process payload blocks */
+       pos = data;
+       cpos = cdata;
+       for (j = 1; j <= num_blocks; j++) {
+               int blen = (j == num_blocks && last_len) ?
+                       last_len : AES_BLOCK_LEN;
+
+               /* Authentication followed by encryption */
+               for (i = 0; i < blen; i++)
+                       b[i] ^= pos[i];
+               ieee80211_aes_encrypt(rk, b, b);
+
+               b_0[14] = (j >> 8) & 0xff;
+               b_0[15] = j & 0xff;
+               ieee80211_aes_encrypt(rk, b_0, e);
+               for (i = 0; i < blen; i++)
+                       *cpos++ = *pos++ ^ e[i];
+       }
+
+       for (i = 0; i < CCMP_MIC_LEN; i++)
+               mic[i] = b[i] ^ s_0[i];
+}
+
+
+int ieee80211_aes_ccm_decrypt(u32 *rk, u8 *b_0, u8 *aad, u8 *cdata,
+                             size_t data_len, u8 *mic, u8 *data)
+{
+       int i, j, last_len, num_blocks;
+       u8 *pos, *cpos;
+       u8 b[AES_BLOCK_LEN], s_0[AES_BLOCK_LEN], a[AES_BLOCK_LEN];
+
+       num_blocks = (data_len + AES_BLOCK_LEN - 1) / AES_BLOCK_LEN;
+       last_len = data_len % AES_BLOCK_LEN;
+       aes_ccm_prepare(rk, b_0, aad, b, s_0, a);
+
+       /* Process payload blocks */
+       cpos = cdata;
+       pos = data;
+       for (j = 1; j <= num_blocks; j++) {
+               int blen = (j == num_blocks && last_len) ?
+                       last_len : AES_BLOCK_LEN;
+
+               /* Decryption followed by authentication */
+               b_0[14] = (j >> 8) & 0xff;
+               b_0[15] = j & 0xff;
+               ieee80211_aes_encrypt(rk, b_0, b);
+               for (i = 0; i < blen; i++) {
+                       *pos = *cpos++ ^ b[i];
+                       a[i] ^= *pos++;
+               }
+
+               ieee80211_aes_encrypt(rk, a, a);
+       }
+
+       for (i = 0; i < CCMP_MIC_LEN; i++) {
+               if ((mic[i] ^ s_0[i]) != a[i])
+                       return -1;
+       }
+
+       return 0;
+}
+
diff --git a/openwrt/target/linux/package/ieee80211-dscape/src/aes_ccm.h b/openwrt/target/linux/package/ieee80211-dscape/src/aes_ccm.h
new file mode 100644 (file)
index 0000000..f741b08
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright 2003-2004, Instant802 Networks, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef AES_CCM_H
+#define AES_CCM_H
+
+#define AES_BLOCK_LEN 16
+#define AES_STATE_LEN 44
+
+void ieee80211_aes_key_setup_encrypt(u32 rk[/*44*/], const u8 key[]);
+void ieee80211_aes_encrypt(const u32 rk[/*44*/], const u8 pt[16], u8 ct[16]);
+void ieee80211_aes_ccm_encrypt(u32 rk[/*44*/], u8 *b_0, u8 *aad, u8 *data,
+                              size_t data_len, u8 *cdata, u8 *mic);
+int ieee80211_aes_ccm_decrypt(u32 rk[/*44*/], u8 *b_0, u8 *aad, u8 *cdata,
+                             size_t data_len, u8 *mic, u8 *data);
+
+#endif /* AES_CCM_H */
diff --git a/openwrt/target/linux/package/ieee80211-dscape/src/fifo_qdisc.c b/openwrt/target/linux/package/ieee80211-dscape/src/fifo_qdisc.c
new file mode 100644 (file)
index 0000000..95bc0b5
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2005, Devicescape Software, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * If building without CONFIG_NET_SCHED we need a simple
+ * fifo qdisc to install by default as the sub-qdisc.
+ * This is a simple replacement for sch_fifo.
+ */
+
+#include <linux/config.h>
+#include <linux/version.h>
+#include <linux/netdevice.h>
+#include <net/ieee80211.h>
+#include "ieee80211_i.h"
+#include "wme.h"
+
+static int pfifo_enqueue(struct sk_buff *skb, struct Qdisc* qd)
+{
+       struct sk_buff_head *q = qdisc_priv(qd);
+
+       if (skb_queue_len(q) > qd->dev->tx_queue_len) {
+               qd->qstats.drops++;
+               kfree_skb(skb);
+               return NET_XMIT_DROP;
+       }
+       
+       skb_queue_tail(q, skb);
+       qd->q.qlen++;
+       qd->bstats.bytes += skb->len;
+       qd->bstats.packets++;
+       
+       return NET_XMIT_SUCCESS;
+}
+
+
+static int pfifo_requeue(struct sk_buff *skb, struct Qdisc* qd)
+{
+       struct sk_buff_head *q = qdisc_priv(qd);
+
+       skb_queue_head(q, skb);
+       qd->q.qlen++;
+       qd->bstats.bytes += skb->len;
+       qd->bstats.packets++;
+       
+       return NET_XMIT_SUCCESS;
+}
+
+
+static struct sk_buff *pfifo_dequeue(struct Qdisc* qd)
+{
+       struct sk_buff_head *q = qdisc_priv(qd);
+
+       return skb_dequeue(q);
+}
+
+
+static int pfifo_init(struct Qdisc* qd, struct rtattr *opt)
+{
+       struct sk_buff_head *q = qdisc_priv(qd);
+
+       skb_queue_head_init(q);
+       return 0;
+}
+
+
+static void pfifo_reset(struct Qdisc* qd)
+{
+       struct sk_buff_head *q = qdisc_priv(qd);
+
+       skb_queue_purge(q);
+       qd->q.qlen = 0;
+}
+
+
+static int pfifo_dump(struct Qdisc *qd, struct sk_buff *skb)
+{
+       return skb->len;
+}
+
+
+struct Qdisc_ops pfifo_qdisc_ops =
+{
+       .next = NULL,
+       .cl_ops = NULL,
+       .id = "ieee80211_pfifo",
+       .priv_size = sizeof(struct sk_buff_head),
+
+       .enqueue = pfifo_enqueue,
+       .dequeue = pfifo_dequeue,
+       .requeue = pfifo_requeue,
+       .drop = NULL,
+
+       .init = pfifo_init,
+       .reset = pfifo_reset,
+       .destroy = NULL,
+       .change = NULL,
+
+       .dump = pfifo_dump,
+};
+
diff --git a/openwrt/target/linux/package/ieee80211-dscape/src/hostapd_ioctl.h b/openwrt/target/linux/package/ieee80211-dscape/src/hostapd_ioctl.h
new file mode 100644 (file)
index 0000000..eff4858
--- /dev/null
@@ -0,0 +1,438 @@
+/*
+ * Host AP (software wireless LAN access point) user space daemon for
+ * Host AP kernel driver
+ * Copyright 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
+ * Copyright 2002-2004, Instant802 Networks, Inc.
+ * Copyright 2005, Devicescape Software, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef HOSTAPD_IOCTL_H
+#define HOSTAPD_IOCTL_H
+
+#ifndef __KERNEL__
+#include "ieee80211_shared.h"
+#endif /* __KERNEL__ */
+
+#define PRISM2_IOCTL_PRISM2_PARAM (SIOCIWFIRSTPRIV + 0)
+#define PRISM2_IOCTL_GET_PRISM2_PARAM (SIOCIWFIRSTPRIV + 1)
+#define PRISM2_IOCTL_HOSTAPD (SIOCIWFIRSTPRIV + 3)
+#define PRISM2_IOCTL_TEST_PARAM (SIOCIWFIRSTPRIV + 4)
+
+/* PRISM2_IOCTL_PRISM2_PARAM ioctl() subtypes: */
+enum {
+       PRISM2_PARAM_PTYPE = 1,
+       PRISM2_PARAM_TXRATECTRL = 2,
+       PRISM2_PARAM_BEACON_INT = 3,
+       PRISM2_PARAM_PSEUDO_IBSS = 4,
+       PRISM2_PARAM_ALC = 5,
+       PRISM2_PARAM_TXPOWER = 6,
+       PRISM2_PARAM_DUMP = 7,
+       PRISM2_PARAM_OTHER_AP_POLICY = 8,
+       PRISM2_PARAM_AP_MAX_INACTIVITY = 9,
+       PRISM2_PARAM_AP_BRIDGE_PACKETS = 10,
+       PRISM2_PARAM_DTIM_PERIOD = 11,
+       PRISM2_PARAM_AP_NULLFUNC_ACK = 12,
+       PRISM2_PARAM_MAX_WDS = 13,
+       PRISM2_PARAM_AP_AUTOM_AP_WDS = 14,
+       PRISM2_PARAM_AP_AUTH_ALGS = 15,
+       PRISM2_PARAM_MONITOR_ALLOW_FCSERR = 16,
+       PRISM2_PARAM_HOST_ENCRYPT = 17,
+       PRISM2_PARAM_HOST_DECRYPT = 18,
+       PRISM2_PARAM_BUS_MASTER_THRESHOLD_RX = 19,
+       PRISM2_PARAM_BUS_MASTER_THRESHOLD_TX = 20,
+       PRISM2_PARAM_HOST_ROAMING = 21,
+       PRISM2_PARAM_BCRX_STA_KEY = 22,
+       PRISM2_PARAM_IEEE_802_1X = 23,
+       PRISM2_PARAM_ANTSEL_TX = 24,
+       PRISM2_PARAM_ANTSEL_RX = 25,
+       PRISM2_PARAM_MONITOR_TYPE = 26,
+       PRISM2_PARAM_WDS_TYPE = 27,
+       PRISM2_PARAM_HOSTSCAN = 28,
+       PRISM2_PARAM_AP_SCAN = 29,
+
+       /* Instant802 additions */
+       PRISM2_PARAM_CTS_PROTECT_ERP_FRAMES = 1001,
+       PRISM2_PARAM_DROP_UNENCRYPTED = 1002,
+       PRISM2_PARAM_PREAMBLE = 1003,
+       PRISM2_PARAM_RATE_LIMIT = 1004,
+       PRISM2_PARAM_RATE_LIMIT_BURST = 1005,
+       PRISM2_PARAM_SHORT_SLOT_TIME = 1006,
+       PRISM2_PARAM_TEST_MODE = 1007,
+       PRISM2_PARAM_NEXT_MODE = 1008,
+       PRISM2_PARAM_CLEAR_KEYS = 1009,
+       PRISM2_PARAM_ADM_STATUS = 1010,
+       PRISM2_PARAM_ANTENNA_SEL = 1011,
+       PRISM2_PARAM_CALIB_INT = 1012,
+        PRISM2_PARAM_ANTENNA_MODE = 1013,
+       PRISM2_PARAM_PRIVACY_INVOKED = 1014,
+       PRISM2_PARAM_BROADCAST_SSID = 1015,
+        PRISM2_PARAM_STAT_TIME = 1016,
+       PRISM2_PARAM_STA_ANTENNA_SEL = 1017,
+       PRISM2_PARAM_FORCE_UNICAST_RATE = 1018,
+       PRISM2_PARAM_RATE_CTRL_NUM_UP = 1019,
+       PRISM2_PARAM_RATE_CTRL_NUM_DOWN = 1020,
+       PRISM2_PARAM_MAX_RATECTRL_RATE = 1021,
+       PRISM2_PARAM_TX_POWER_REDUCTION = 1022,
+       PRISM2_PARAM_EAPOL = 1023,
+       PRISM2_PARAM_KEY_TX_RX_THRESHOLD = 1024,
+       PRISM2_PARAM_KEY_INDEX = 1025,
+       PRISM2_PARAM_DEFAULT_WEP_ONLY = 1026,
+       PRISM2_PARAM_WIFI_WME_NOACK_TEST = 1033,
+       PRISM2_PARAM_ALLOW_BROADCAST_ALWAYS = 1034,
+       PRISM2_PARAM_SCAN_FLAGS = 1035,
+       PRISM2_PARAM_HW_MODES = 1036,
+       PRISM2_PARAM_CREATE_IBSS = 1037,
+       PRISM2_PARAM_WMM_ENABLED = 1038,
+       PRISM2_PARAM_MIXED_CELL = 1039,
+       PRISM2_PARAM_KEY_MGMT = 1040,
+       PRISM2_PARAM_RADAR_DETECT = 1043,
+       PRISM2_PARAM_SPECTRUM_MGMT = 1044,
+       /* NOTE: Please try to coordinate with other active development
+        * branches before allocating new param numbers so that each new param
+        * will be unique within all branches and the allocated number will not
+        * need to be changed when merging new features. Existing numbers in
+        * the mainline (or main devel branch) must not be changed when merging
+        * in new features. */
+};
+
+/* PRISM2_IOCTL_HOSTAPD ioctl() cmd: */
+enum {
+       PRISM2_HOSTAPD_FLUSH = 1,
+       PRISM2_HOSTAPD_ADD_STA = 2,
+       PRISM2_HOSTAPD_REMOVE_STA = 3,
+       PRISM2_HOSTAPD_GET_INFO_STA = 4,
+       /* REMOVED: PRISM2_HOSTAPD_RESET_TXEXC_STA = 5, */
+       PRISM2_SET_ENCRYPTION = 6,
+       PRISM2_GET_ENCRYPTION = 7,
+       PRISM2_HOSTAPD_SET_FLAGS_STA = 8,
+       PRISM2_HOSTAPD_GET_RID = 9,
+       PRISM2_HOSTAPD_SET_RID = 10,
+       PRISM2_HOSTAPD_SET_ASSOC_AP_ADDR = 11,
+       PRISM2_HOSTAPD_MLME = 13,
+
+       /* Instant802 additions */
+       PRISM2_HOSTAPD_SET_BEACON = 1001,
+        PRISM2_HOSTAPD_GET_HW_FEATURES = 1002,
+        PRISM2_HOSTAPD_SCAN = 1003,
+       PRISM2_HOSTAPD_WPA_TRIGGER = 1004,
+       PRISM2_HOSTAPD_SET_RATE_SETS = 1005,
+        PRISM2_HOSTAPD_ADD_IF = 1006,
+        PRISM2_HOSTAPD_REMOVE_IF = 1007,
+        PRISM2_HOSTAPD_GET_DOT11COUNTERSTABLE = 1008,
+        PRISM2_HOSTAPD_GET_LOAD_STATS = 1009,
+        PRISM2_HOSTAPD_SET_STA_VLAN = 1010,
+       PRISM2_HOSTAPD_SET_GENERIC_INFO_ELEM = 1011,
+       PRISM2_HOSTAPD_SET_CHANNEL_FLAG = 1012,
+       PRISM2_HOSTAPD_SET_REGULATORY_DOMAIN = 1013,
+       PRISM2_HOSTAPD_SET_TX_QUEUE_PARAMS = 1014,
+       PRISM2_HOSTAPD_SET_BSS = 1015,
+       PRISM2_HOSTAPD_GET_TX_STATS = 1016,
+       PRISM2_HOSTAPD_UPDATE_IF = 1017,
+       PRISM2_HOSTAPD_SCAN_REQ = 1019,
+       PRISM2_STA_GET_STATE = 1020,
+       PRISM2_HOSTAPD_FLUSH_IFS = 1021,
+       PRISM2_HOSTAPD_SET_RADAR_PARAMS = 1023,
+       PRISM2_HOSTAPD_SET_QUIET_PARAMS = 1024,
+       PRISM2_HOSTAPD_GET_TX_POWER = 1025,
+       /* NOTE: Please try to coordinate with other active development
+        * branches before allocating new param numbers so that each new param
+        * will be unique within all branches and the allocated number will not
+        * need to be changed when merging new features. Existing numbers in
+        * the mainline (or main devel branch) must not be changed when merging
+        * in new features. */
+};
+
+       /* these definitions mirror the ieee80211_i.h
+        * IEEE80211_DISABLED, ... IEEE80211_ASSOCIATED enumeration */
+enum {
+       PRISM2_PARAM_STA_DISABLED,
+       PRISM2_PARAM_STA_AUTHENTICATE,
+       PRISM2_PARAM_STA_ASSOCIATE,
+       PRISM2_PARAM_STA_ASSOCIATED,
+};
+
+#define PRISM2_HOSTAPD_MAX_BUF_SIZE 2048
+#define HOSTAP_CRYPT_ALG_NAME_LEN 16
+
+/* Use this to make sure that structure elements are correctly aligned
+ * for access as other types. Most commonly, this affects the placeholder
+ * types used for data at the end of a structure in this union.
+ */
+#ifdef __GNUC__
+#undef ALIGNED
+#define ALIGNED __attribute__ ((aligned))
+#else
+/* Check if it has been defined elsewhere */
+#ifndef ALIGNED
+#error "Must define ALIGNED to generate aligned structure elements"
+#endif
+#endif
+
+struct prism2_hostapd_param {
+       u32 cmd;
+       u8 sta_addr[ETH_ALEN];
+       u8 pad[2];
+       union {
+               struct {
+                       u16 aid;
+                       u16 capability;
+                       u8 supp_rates[32];
+                       /* atheros_super_ag and enc_flags are only used with
+                        * IEEE80211_ATHEROS_SUPER_AG 
+                        */
+                       u8 atheros_super_ag;
+                       u8 atheros_xr_mode;
+                       u8 wds_flags;
+#define IEEE80211_STA_DYNAMIC_ENC BIT(0)
+                       u8 enc_flags;
+               } add_sta;
+               struct {
+                       u32 inactive_msec;
+                       u32 rx_packets;
+                       u32 tx_packets;
+                       u32 rx_bytes;
+                       u32 tx_bytes;
+                       u32 current_tx_rate; /* in 100 kbps */
+                        u32 channel_use;
+                        u32 flags;
+                       u32 num_ps_buf_frames;
+                       u32 tx_retry_failed;
+                       u32 tx_retry_count;
+                       u32 last_rssi;
+                       u32 last_ack_rssi;
+               } get_info_sta;
+               struct {
+                       u8 alg[HOSTAP_CRYPT_ALG_NAME_LEN];
+                       u32 flags;
+                       u32 err;
+                       u8 idx;
+#define HOSTAP_SEQ_COUNTER_SIZE 8
+                       u8 seq_counter[HOSTAP_SEQ_COUNTER_SIZE];
+                       u16 key_len;
+                       u8 key[0] ALIGNED;
+               } crypt;
+               struct {
+                       u32 flags_and;
+                       u32 flags_or;
+               } set_flags_sta;
+               struct {
+                       u16 rid;
+                       u16 len;
+                       u8 data[0] ALIGNED;
+               } rid;
+               struct {
+                       u16 head_len;
+                       u16 tail_len;
+                       u8 data[0] ALIGNED; /* head_len + tail_len bytes */
+               } beacon;
+               struct {
+                       u16 num_modes;
+                       u16 flags;
+                       u8 data[0] ALIGNED; /* num_modes * feature data */
+                } hw_features;
+                struct {
+                        u8  now;
+                        s8  our_mode_only;
+                        s16 last_rx;
+                        u16 channel;
+                        s16 interval; /* seconds */
+                        s32 listen;   /* microseconds */
+                } scan;
+               struct {
+#define WPA_TRIGGER_FAIL_TX_MIC BIT(0)
+#define WPA_TRIGGER_FAIL_TX_ICV BIT(1)
+#define WPA_TRIGGER_FAIL_RX_MIC BIT(2)
+#define WPA_TRIGGER_FAIL_RX_ICV BIT(3)
+#define WPA_TRIGGER_TX_REPLAY BIT(4)
+#define WPA_TRIGGER_TX_REPLAY_FRAG BIT(5)
+#define WPA_TRIGGER_TX_SKIP_SEQ BIT(6)
+                       u32 trigger;
+               } wpa_trigger;
+               struct {
+                       u16 mode; /* MODE_* */
+                       u16 num_supported_rates;
+                       u16 num_basic_rates;
+                       u8 data[0] ALIGNED; /* num_supported_rates * u16 +
+                                            * num_basic_rates * u16 */
+                } set_rate_sets;
+                struct {
+                       u8 type; /* WDS, VLAN, etc */
+                       u8 name[IFNAMSIZ];
+                        u8 data[0] ALIGNED;
+                } if_info;
+                struct dot11_counters {
+                        u32 dot11TransmittedFragmentCount;
+                        u32 dot11MulticastTransmittedFrameCount;
+                        u32 dot11FailedCount;
+                        u32 dot11ReceivedFragmentCount;
+                        u32 dot11MulticastReceivedFrameCount;
+                        u32 dot11FCSErrorCount;
+                        u32 dot11TransmittedFrameCount;
+                        u32 dot11WEPUndecryptableCount;
+                       u32 dot11ACKFailureCount;
+                       u32 dot11RTSFailureCount;
+                       u32 dot11RTSSuccessCount;
+                } dot11CountersTable;
+               struct {
+#define LOAD_STATS_CLEAR BIT(1)
+                       u32 flags;
+                       u32 channel_use;
+                } get_load_stats;
+                struct {
+                        char vlan_name[IFNAMSIZ];
+                       int vlan_id;
+                } set_sta_vlan;
+               struct {
+                       u8 len;
+                       u8 data[0] ALIGNED;
+               } set_generic_info_elem;
+               struct {
+                       u16 mode; /* MODE_* */
+                       u16 chan;
+                       u32 flag;
+                        u8  power_level; /* regulatory limit in dBm */
+                        u8  antenna_max;
+               } set_channel_flag;
+                struct {
+                        u32 rd;
+                } set_regulatory_domain;
+               struct {
+                       u32 queue;
+                       s32 aifs;
+                       u32 cw_min;
+                       u32 cw_max;
+                       u32 burst_time; /* maximum burst time in 0.1 ms, i.e.,
+                                        * 10 = 1 ms */
+               } tx_queue_params;
+               struct {
+                       u32 bss_count;
+                       u8 bssid_mask[ETH_ALEN];
+               } set_bss;
+               struct ieee80211_tx_stats {
+                       struct {
+                               unsigned int len; /* num packets in queue */
+                               unsigned int limit; /* queue len (soft) limit
+                                                    */
+                               unsigned int count; /* total num frames sent */
+                       } data[4];
+               } get_tx_stats;
+               struct {
+                       u8 ssid_len;
+                       u8 ssid[0] ALIGNED;
+               } scan_req;
+               struct {
+                       u32 state;
+               } sta_get_state;
+               struct {
+#define MLME_STA_DEAUTH 0
+#define MLME_STA_DISASSOC 1
+                       u16 cmd;
+                       u16 reason_code;
+               } mlme;
+               struct {
+                       unsigned int value;
+               /*      TODO
+                       int pulse_width;
+                       int num_pulse;
+                       int period;
+               */
+               }radar;
+               struct {
+                       unsigned int period;
+                       unsigned int offset;
+                       unsigned int duration;
+               }quiet;
+               struct {
+                       unsigned int tx_power_min;
+                       unsigned int tx_power_max;
+               }tx_power;
+               struct {
+                       u8 dummy[80]; /* Make sizeof() this struct large enough
+                                      * with some compiler versions. */
+               } dummy;
+       } u;
+};
+
+
+#ifndef IEEE80211_TX_QUEUE_NUMS
+#define IEEE80211_TX_QUEUE_NUMS
+/* TODO: these need to be synchronized with ieee80211.h; make a shared header
+ * file that can be included into low-level drivers, 80211.o, and hostapd */
+/* tx_queue_params - queue */
+enum {
+       IEEE80211_TX_QUEUE_DATA0 = 0, /* used for EDCA AC_VO data */
+       IEEE80211_TX_QUEUE_DATA1 = 1, /* used for EDCA AC_VI data */
+       IEEE80211_TX_QUEUE_DATA2 = 2, /* used for EDCA AC_BE data */
+       IEEE80211_TX_QUEUE_DATA3 = 3, /* used for EDCA AC_BK data */
+       IEEE80211_TX_QUEUE_DATA4 = 4,
+       IEEE80211_TX_QUEUE_AFTER_BEACON = 6,
+       IEEE80211_TX_QUEUE_BEACON = 7
+};
+#endif /* IEEE80211_TX_QUEUE_NUMS */
+
+
+#define HOSTAP_CRYPT_FLAG_SET_TX_KEY BIT(0)
+#define HOSTAP_CRYPT_FLAG_PERMANENT BIT(1)
+
+#define HOSTAP_CRYPT_ERR_UNKNOWN_ALG 2
+#define HOSTAP_CRYPT_ERR_UNKNOWN_ADDR 3
+#define HOSTAP_CRYPT_ERR_CRYPT_INIT_FAILED 4
+#define HOSTAP_CRYPT_ERR_KEY_SET_FAILED 5
+#define HOSTAP_CRYPT_ERR_TX_KEY_SET_FAILED 6
+#define HOSTAP_CRYPT_ERR_CARD_CONF_FAILED 7
+
+#define HOSTAP_HW_FLAG_NULLFUNC_OK BIT(0)
+
+enum {
+       IEEE80211_KEY_MGMT_NONE = 0,
+       IEEE80211_KEY_MGMT_IEEE8021X = 1,
+       IEEE80211_KEY_MGMT_WPA_PSK = 2,
+       IEEE80211_KEY_MGMT_WPA_EAP = 3,
+};
+
+
+/* Data structures used for get_hw_features ioctl */
+struct hostapd_ioctl_hw_modes_hdr {
+       int mode;
+       int num_channels;
+       int num_rates;
+};
+
+struct ieee80211_channel_data {
+       short chan; /* channel number (IEEE 802.11) */
+       short freq; /* frequency in MHz */
+       int flag; /* flag for hostapd use (IEEE80211_CHAN_*) */
+};
+
+struct ieee80211_rate_data {
+       int rate; /* rate in 100 kbps */
+       int flags; /* IEEE80211_RATE_ flags */
+};
+
+
+/* ADD_IF, REMOVE_IF, and UPDATE_IF 'type' argument */
+enum {
+       HOSTAP_IF_WDS = 1, HOSTAP_IF_VLAN = 2, HOSTAP_IF_BSS = 3,
+       HOSTAP_IF_STA = 4
+};
+
+struct hostapd_if_wds {
+        u8 remote_addr[ETH_ALEN];
+};
+
+struct hostapd_if_vlan {
+        u8 id;
+};
+
+struct hostapd_if_bss {
+        u8 bssid[ETH_ALEN];
+};
+
+struct hostapd_if_sta {
+};
+
+#endif /* HOSTAPD_IOCTL_H */
diff --git a/openwrt/target/linux/package/ieee80211-dscape/src/ieee80211.c b/openwrt/target/linux/package/ieee80211-dscape/src/ieee80211.c
new file mode 100644 (file)
index 0000000..5e4e7ac
--- /dev/null
@@ -0,0 +1,4895 @@
+/*
+ * Copyright 2002-2005, Instant802 Networks, Inc.
+ * Copyright 2005, Devicescape Software, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef EXPORT_SYMTAB
+#define EXPORT_SYMTAB
+#endif
+
+#include <linux/config.h>
+#include <linux/version.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/netdevice.h>
+#include <linux/types.h>
+#include <linux/slab.h>
+#include <linux/skbuff.h>
+#include <linux/etherdevice.h>
+#include <linux/if_arp.h>
+#include <linux/wireless.h>
+#include <net/iw_handler.h>
+#include <linux/compiler.h>
+
+#include <net/ieee80211.h>
+#include <net/ieee80211_common.h>
+#include <net/ieee80211_mgmt.h>
+#include "ieee80211_i.h"
+#include "ieee80211_proc.h"
+#include "rate_control.h"
+#include "wep.h"
+#include "wpa.h"
+#include "tkip.h"
+#include "wme.h"
+
+
+/* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
+/* Ethernet-II snap header (RFC1042 for most EtherTypes) */
+static unsigned char rfc1042_header[] =
+{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
+/* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
+static unsigned char bridge_tunnel_header[] =
+{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
+/* No encapsulation header if EtherType < 0x600 (=length) */
+
+static unsigned char eapol_header[] =
+{ 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00, 0x88, 0x8e };
+
+
+struct rate_control_algs {
+       struct rate_control_algs *next;
+       struct rate_control_ops *ops;
+};
+
+static struct rate_control_algs *ieee80211_rate_ctrl_algs;
+
+static int rate_control_initialize(struct ieee80211_local *local);
+
+
+static u8 * ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len);
+
+
+struct ieee80211_key_conf *
+ieee80211_key_data2conf(struct ieee80211_local *local,
+                       struct ieee80211_key *data)
+{
+       struct ieee80211_key_conf *conf;
+
+       conf = kmalloc(sizeof(*conf) + data->keylen, GFP_ATOMIC);
+       if (conf == NULL)
+               return NULL;
+
+       conf->hw_key_idx = data->hw_key_idx;
+       conf->alg = data->alg;
+       conf->keylen = data->keylen;
+       conf->force_sw_encrypt = data->force_sw_encrypt;
+       conf->keyidx = data->keyidx;
+       conf->default_tx_key = data->default_tx_key;
+       conf->default_wep_only = local->default_wep_only;
+       memcpy(conf->key, data->key, data->keylen);
+
+       return conf;
+}
+
+
+static int rate_list_match(int *rate_list, int rate)
+{
+       int i;
+
+       if (rate_list == NULL)
+               return 0;
+
+       for (i = 0; rate_list[i] >= 0; i++)
+               if (rate_list[i] == rate)
+                       return 1;
+
+       return 0;
+}
+
+
+void ieee80211_prepare_rates(struct net_device *dev)
+{
+       struct ieee80211_local *local = dev->priv;
+       int i;
+
+       for (i = 0; i < local->num_curr_rates; i++) {
+               struct ieee80211_rate *rate = &local->curr_rates[i];
+
+               rate->flags &= ~(IEEE80211_RATE_SUPPORTED |
+                                IEEE80211_RATE_BASIC);
+
+               if (local->supp_rates[local->conf.phymode]) {
+                       if (!rate_list_match(local->supp_rates
+                                            [local->conf.phymode],
+                                            rate->rate))
+                               continue;
+               }
+
+               rate->flags |= IEEE80211_RATE_SUPPORTED;
+
+               /* Use configured basic rate set if it is available. If not,
+                * use defaults that are sane for most cases. */
+               if (local->basic_rates[local->conf.phymode]) {
+                       if (rate_list_match(local->basic_rates
+                                           [local->conf.phymode],
+                                           rate->rate))
+                               rate->flags |= IEEE80211_RATE_BASIC;
+               } else switch (local->conf.phymode) {
+               case MODE_IEEE80211A:
+                       if (rate->rate == 60 || rate->rate == 120 ||
+                           rate->rate == 240)
+                               rate->flags |= IEEE80211_RATE_BASIC;
+                       break;
+               case MODE_IEEE80211B:
+                       if (rate->rate == 10 || rate->rate == 20)
+                               rate->flags |= IEEE80211_RATE_BASIC;
+                       break;
+               case MODE_ATHEROS_TURBO:
+                       if (rate->rate == 120 || rate->rate == 240 ||
+                           rate->rate == 480)
+                               rate->flags |= IEEE80211_RATE_BASIC;
+                       break;
+               case MODE_IEEE80211G:
+                       if (rate->rate == 10 || rate->rate == 20 ||
+                           rate->rate == 55 || rate->rate == 110)
+                               rate->flags |= IEEE80211_RATE_BASIC;
+                       break;
+               }
+
+               /* Set ERP and MANDATORY flags based on phymode */
+               switch (local->conf.phymode) {
+               case MODE_IEEE80211A:
+                       if (rate->rate == 60 || rate->rate == 120 ||
+                           rate->rate == 240)
+                               rate->flags |= IEEE80211_RATE_MANDATORY;
+                       break;
+               case MODE_IEEE80211B:
+                       if (rate->rate == 10)
+                               rate->flags |= IEEE80211_RATE_MANDATORY;
+                       break;
+               case MODE_ATHEROS_TURBO:
+                       break;
+               case MODE_IEEE80211G:
+                       if (rate->rate == 10 || rate->rate == 20 ||
+                           rate->rate == 55 || rate->rate == 110 ||
+                           rate->rate == 60 || rate->rate == 120 ||
+                           rate->rate == 240)
+                               rate->flags |= IEEE80211_RATE_MANDATORY;
+                       if (rate->rate != 10 && rate->rate != 20 &&
+                           rate->rate != 55 && rate->rate != 110)
+                               rate->flags |= IEEE80211_RATE_ERP;
+                       break;
+               }
+       }
+}
+
+
+static void ieee80211_key_threshold_notify(struct net_device *dev,
+                                          struct ieee80211_key *key,
+                                          struct sta_info *sta)
+{
+       struct sk_buff *skb;
+       struct ieee80211_msg_key_notification *msg;
+
+       skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
+                           sizeof(struct ieee80211_msg_key_notification));
+       if (skb == NULL)
+               return;
+
+       skb_reserve(skb, sizeof(struct ieee80211_frame_info));
+       msg = (struct ieee80211_msg_key_notification *)
+               skb_put(skb, sizeof(struct ieee80211_msg_key_notification));
+       msg->tx_rx_count = key->tx_rx_count;
+       memcpy(msg->ifname, dev->name, IFNAMSIZ);
+       if (sta)
+               memcpy(msg->addr, sta->addr, ETH_ALEN);
+       else
+               memset(msg->addr, 0xff, ETH_ALEN);
+
+       key->tx_rx_count = 0;
+
+       ieee80211_rx_mgmt(dev, skb, 0,
+                         ieee80211_msg_key_threshold_notification);
+}
+
+
+int ieee80211_get_hdrlen(u16 fc)
+{
+       int hdrlen = 24;
+
+       switch (WLAN_FC_GET_TYPE(fc)) {
+       case WLAN_FC_TYPE_DATA:
+               if ((fc & WLAN_FC_FROMDS) && (fc & WLAN_FC_TODS))
+                       hdrlen = 30; /* Addr4 */
+               if (WLAN_FC_GET_STYPE(fc) & 0x08)
+                       hdrlen += 2; /* QoS Control Field */
+               break;
+       case WLAN_FC_TYPE_CTRL:
+               switch (WLAN_FC_GET_STYPE(fc)) {
+               case WLAN_FC_STYPE_CTS:
+               case WLAN_FC_STYPE_ACK:
+                       hdrlen = 10;
+                       break;
+               default:
+                       hdrlen = 16;
+                       break;
+               }
+               break;
+       }
+
+       return hdrlen;
+}
+
+
+int ieee80211_get_hdrlen_from_skb(struct sk_buff *skb)
+{
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
+       int hdrlen;
+
+       if (unlikely(skb->len < 10))
+               return 0;
+       hdrlen = ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
+       if (unlikely(hdrlen > skb->len))
+               return 0;
+       return hdrlen;
+}
+
+
+#ifdef IEEE80211_VERBOSE_DEBUG_FRAME_DUMP
+static void ieee80211_dump_frame(const char *ifname, const char *title,
+                                struct sk_buff *skb)
+{
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
+       u16 fc;
+       int hdrlen;
+
+       printk(KERN_DEBUG "%s: %s (len=%d)", ifname, title, skb->len);
+       if (skb->len < 4) {
+               printk("\n");
+               return;
+       }
+
+       fc = le16_to_cpu(hdr->frame_control);
+       hdrlen = ieee80211_get_hdrlen(fc);
+       if (hdrlen > skb->len)
+               hdrlen = skb->len;
+       if (hdrlen >= 4)
+               printk(" FC=0x%04x DUR=0x%04x",
+                      fc, le16_to_cpu(hdr->duration_id));
+       if (hdrlen >= 10)
+               printk(" A1=" MACSTR, MAC2STR(hdr->addr1));
+       if (hdrlen >= 16)
+               printk(" A2=" MACSTR, MAC2STR(hdr->addr2));
+       if (hdrlen >= 24)
+               printk(" A3=" MACSTR, MAC2STR(hdr->addr3));
+       if (hdrlen >= 30)
+               printk(" A4=" MACSTR, MAC2STR(hdr->addr4));
+       printk("\n");
+}
+#else /* IEEE80211_VERBOSE_DEBUG_FRAME_DUMP */
+static inline void ieee80211_dump_frame(const char *ifname, const char *title,
+                                       struct sk_buff *skb)
+{
+}
+#endif /* IEEE80211_VERBOSE_DEBUG_FRAME_DUMP */
+
+
+static int ieee80211_is_eapol(struct sk_buff *skb)
+{
+       struct ieee80211_hdr *hdr;
+       u16 fc;
+       int hdrlen;
+
+       if (unlikely(skb->len < 10))
+               return 0;
+
+       hdr = (struct ieee80211_hdr *) skb->data;
+       fc = le16_to_cpu(hdr->frame_control);
+
+       if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
+               return 0;
+
+       hdrlen = ieee80211_get_hdrlen(fc);
+
+       if (unlikely(skb->len >= hdrlen + sizeof(eapol_header) &&
+                    memcmp(skb->data + hdrlen, eapol_header,
+                           sizeof(eapol_header)) == 0))
+               return 1;
+
+       return 0;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_tx_h_rate_ctrl(struct ieee80211_txrx_data *tx)
+{
+       struct rate_control_extra extra;
+
+       memset(&extra, 0, sizeof(extra));
+       extra.mgmt_data = tx->sdata &&
+               tx->sdata->type == IEEE80211_SUB_IF_TYPE_MGMT;
+       extra.ethertype = tx->ethertype;
+       extra.startidx  = 0;
+       extra.endidx    = tx->local->num_curr_rates;
+
+
+        tx->u.tx.rate = rate_control_get_rate(tx->dev, tx->skb, &extra);
+       if (unlikely(extra.probe != NULL)) {
+               tx->u.tx.control->rate_ctrl_probe = 1;
+               tx->u.tx.probe_last_frag = 1;
+//             tx->u.tx.control->alt_retry_rate = tx->u.tx.rate->val;
+               tx->u.tx.rate = extra.probe;
+       } else {
+//             tx->u.tx.control->alt_retry_rate = -1;
+       }
+       if (!tx->u.tx.rate)
+               return TXRX_DROP;
+       if (tx->local->conf.phymode == MODE_IEEE80211G &&
+           tx->local->cts_protect_erp_frames && tx->fragmented &&
+           extra.nonerp) {
+               tx->u.tx.last_frag_rate = tx->u.tx.rate;
+               tx->u.tx.last_frag_rateidx = extra.rateidx;
+               tx->u.tx.probe_last_frag = extra.probe ? 1 : 0;
+
+               tx->u.tx.rate = extra.nonerp;
+//             tx->u.tx.control->rateidx = extra.nonerp_idx;
+               tx->u.tx.control->rate_ctrl_probe = 0;
+       } else {
+               tx->u.tx.last_frag_rate = tx->u.tx.rate;
+               tx->u.tx.last_frag_rateidx = extra.rateidx;
+//             tx->u.tx.control->rateidx = extra.rateidx;
+       }
+       tx->u.tx.control->tx_rate = tx->u.tx.rate->val;
+       if ((tx->u.tx.rate->flags & IEEE80211_RATE_PREAMBLE2) &&
+           tx->local->short_preamble &&
+           (!tx->sta || (tx->sta->flags & WLAN_STA_SHORT_PREAMBLE))) {
+               tx->u.tx.short_preamble = 1;
+               tx->u.tx.control->tx_rate = tx->u.tx.rate->val2;
+       }
+
+       return TXRX_CONTINUE;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_tx_h_select_key(struct ieee80211_txrx_data *tx)
+{
+       if (tx->sta)
+               tx->u.tx.control->key_idx = tx->sta->key_idx_compression;
+       else
+               tx->u.tx.control->key_idx = HW_KEY_IDX_INVALID;
+
+       if (unlikely(tx->u.tx.control->do_not_encrypt))
+               tx->key = NULL;
+       else if (tx->sta && tx->sta->key)
+               tx->key = tx->sta->key;
+       else if (tx->sdata->default_key)
+               tx->key = tx->sdata->default_key;
+       else if (tx->sdata->drop_unencrypted && !(tx->sdata->eapol && ieee80211_is_eapol(tx->skb))) {
+               I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
+               return TXRX_DROP;
+       } else
+               tx->key = NULL;
+
+       if (tx->key) {
+               tx->key->tx_rx_count++;
+               if (unlikely(tx->local->key_tx_rx_threshold &&
+                            tx->key->tx_rx_count >
+                            tx->local->key_tx_rx_threshold)) {
+                       ieee80211_key_threshold_notify(tx->dev, tx->key,
+                                                      tx->sta);
+               }
+       }
+
+       return TXRX_CONTINUE;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_tx_h_fragment(struct ieee80211_txrx_data *tx)
+{
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
+       size_t hdrlen, per_fragm, num_fragm, payload_len, left;
+       struct sk_buff **frags, *first, *frag;
+       int i;
+        u8 *pos;
+       int frag_threshold = tx->local->fragmentation_threshold;
+
+       if (!tx->fragmented)
+               return TXRX_CONTINUE;
+
+
+       first = tx->skb;
+
+       hdrlen = ieee80211_get_hdrlen(tx->fc);
+       payload_len = first->len - hdrlen;
+       per_fragm = frag_threshold - hdrlen - 4 /* FCS */;
+       num_fragm = (payload_len + per_fragm - 1) / per_fragm;
+
+       frags = (struct sk_buff **)
+               kmalloc(num_fragm * sizeof(struct sk_buff *), GFP_ATOMIC);
+       if (frags == NULL)
+               goto fail;
+       memset(frags, 0, num_fragm * sizeof(struct sk_buff *));
+
+       hdr->frame_control |= cpu_to_le16(WLAN_FC_MOREFRAG);
+       pos = first->data + hdrlen + per_fragm;
+       left = payload_len - per_fragm;
+       for (i = 0; i < num_fragm - 1; i++) {
+               struct ieee80211_hdr *fhdr;
+               size_t copylen;
+
+               if (left <= 0)
+                       goto fail;
+
+               /* reserve enough extra head and tail room for possible
+                * encryption */
+#define IEEE80211_ENCRYPT_HEADROOM 8
+#define IEEE80211_ENCRYPT_TAILROOM 12
+               frag = frags[i] =
+                       dev_alloc_skb(frag_threshold +
+                                     IEEE80211_ENCRYPT_HEADROOM +
+                                     IEEE80211_ENCRYPT_TAILROOM);
+               if (!frag)
+                       goto fail;
+               /* Make sure that all fragments use the same priority so
+                * that they end up using the same TX queue */
+               frag->priority = first->priority;
+               skb_reserve(frag, IEEE80211_ENCRYPT_HEADROOM);
+               fhdr = (struct ieee80211_hdr *) skb_put(frag, hdrlen);
+               memcpy(fhdr, first->data, hdrlen);
+               if (i == num_fragm - 2)
+                       fhdr->frame_control &= cpu_to_le16(~WLAN_FC_MOREFRAG);
+               fhdr->seq_ctrl = cpu_to_le16(i + 1);
+               copylen = left > per_fragm ? per_fragm : left;
+               memcpy(skb_put(frag, copylen), pos, copylen);
+
+               pos += copylen;
+               left -= copylen;
+       }
+       skb_trim(first, hdrlen + per_fragm);
+
+       tx->u.tx.num_extra_frag = num_fragm - 1;
+       tx->u.tx.extra_frag = frags;
+
+       return TXRX_CONTINUE;
+
+ fail:
+       printk(KERN_DEBUG "%s: failed to fragment frame\n", tx->dev->name);
+       if (frags) {
+               for (i = 0; i < num_fragm - 1; i++)
+                       if (frags[i])
+                               dev_kfree_skb(frags[i]);
+               kfree(frags);
+       }
+       I802_DEBUG_INC(tx->local->tx_handlers_drop_fragment);
+       return TXRX_DROP;
+}
+
+
+static int wep_encrypt_skb(struct ieee80211_txrx_data *tx, struct sk_buff *skb)
+{
+       if (tx->key->force_sw_encrypt || tx->local->conf.sw_encrypt) {
+               if (ieee80211_wep_encrypt(tx->local, skb, tx->key))
+                       return -1;
+       } else {
+               tx->u.tx.control->key_idx = tx->key->hw_key_idx;
+               if (tx->local->hw->wep_include_iv) {
+                       if (ieee80211_wep_add_iv(tx->local, skb, tx->key) ==
+                           NULL)
+                               return -1;
+               }
+        }
+       return 0;
+}
+
+
+void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx)
+{
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
+
+       hdr->frame_control |= cpu_to_le16(WLAN_FC_ISWEP);
+       if (tx->u.tx.extra_frag) {
+               struct ieee80211_hdr *fhdr;
+               int i;
+               for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
+                       fhdr = (struct ieee80211_hdr *)
+                               tx->u.tx.extra_frag[i]->data;
+                       fhdr->frame_control |= cpu_to_le16(WLAN_FC_ISWEP);
+               }
+       }
+}
+
+
+static ieee80211_txrx_result
+ieee80211_tx_h_wep_encrypt(struct ieee80211_txrx_data *tx)
+{
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
+       u16 fc;
+
+        fc = le16_to_cpu(hdr->frame_control);
+
+       if (!tx->key || tx->key->alg != ALG_WEP ||
+           (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_DATA &&
+            (WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_MGMT ||
+             WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_AUTH)))
+               return TXRX_CONTINUE;
+
+       tx->u.tx.control->iv_len = WEP_IV_LEN;
+       tx->u.tx.control->icv_len = WEP_ICV_LEN;
+       ieee80211_tx_set_iswep(tx);
+
+       if (wep_encrypt_skb(tx, tx->skb) < 0) {
+               I802_DEBUG_INC(tx->local->tx_handlers_drop_wep);
+               return TXRX_DROP;
+       }
+
+       if (tx->u.tx.extra_frag) {
+               int i;
+               for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
+                        if (wep_encrypt_skb(tx, tx->u.tx.extra_frag[i]) < 0) {
+                               I802_DEBUG_INC(tx->local->
+                                              tx_handlers_drop_wep);
+                                return TXRX_DROP;
+                       }
+               }
+       }
+
+       return TXRX_CONTINUE;
+}
+
+
+static inline int ceiling_div(int dividend, int divisor)
+{
+       return ((dividend + divisor - 1) / divisor);
+}
+
+
+static int ieee80211_frame_duration(struct ieee80211_local *local, size_t len,
+                                   int rate, int erp, int short_preamble)
+{
+       int dur;
+
+       /* calculate duration (in microseconds, rounded up to next higher
+        * integer if it includes a fractional microsecond) to send frame of
+        * len bytes (does not include FCS) at the given rate. Duration will
+        * also include SIFS.
+        *
+        * rate is in 100 kbps, so divident is multiplied by 10 in the
+        * ceiling_div() operations.
+        */
+
+       if (local->conf.phymode == MODE_IEEE80211A || erp ||
+           local->conf.phymode == MODE_ATHEROS_TURBO) {
+               /*
+                * OFDM:
+                *
+                * N_DBPS = DATARATE x 4
+                * N_SYM = Ceiling((16+8xLENGTH+6) / N_DBPS)
+                *      (16 = SIGNAL time, 6 = tail bits)
+                * TXTIME = T_PREAMBLE + T_SIGNAL + T_SYM x N_SYM + Signal Ext
+                *
+                * T_SYM = 4 usec
+                * 802.11a - 17.5.2: aSIFSTime = 16 usec
+                * 802.11g - 19.8.4: aSIFSTime = 10 usec +
+                *      signal ext = 6 usec
+                */
+               /* FIX: Atheros Turbo may have different (shorter) duration? */
+               dur = 16; /* SIFS + signal ext */
+               dur += 16; /* 17.3.2.3: T_PREAMBLE = 16 usec */
+               dur += 4; /* 17.3.2.3: T_SIGNAL = 4 usec */
+               dur += 4 * ceiling_div((16 + 8 * (len + 4) + 6) * 10,
+                                      4 * rate); /* T_SYM x N_SYM */
+       } else {
+               /*
+                * 802.11b or 802.11g with 802.11b compatibility:
+                * 18.3.4: TXTIME = PreambleLength + PLCPHeaderTime +
+                * Ceiling(((LENGTH+PBCC)x8)/DATARATE). PBCC=0.
+                *
+                * 802.11 (DS): 15.3.3, 802.11b: 18.3.4
+                * aSIFSTime = 10 usec
+                * aPreambleLength = 144 usec or 72 usec with short preamble
+                * aPLCPHeaderLength = 48 ms or 24 ms with short preamble
+                */
+               dur = 10; /* aSIFSTime = 10 usec */
+               dur += short_preamble ? (72 + 24) : (144 + 48);
+
+               dur += ceiling_div(8 * (len + 4) * 10, rate);
+       }
+
+       return dur;
+}
+
+
+static u16 ieee80211_duration(struct ieee80211_txrx_data *tx, int group_addr,
+                             int next_frag_len)
+{
+       int rate, mrate, erp, dur, i;
+       struct ieee80211_rate *txrate = tx->u.tx.rate;
+       struct ieee80211_local *local = tx->local;
+
+       erp = txrate->flags & IEEE80211_RATE_ERP;
+
+       /*
+        * data and mgmt (except PS Poll):
+        * - during CFP: 32768
+        * - during contention period:
+        *   if addr1 is group address: 0
+        *   if more fragments = 0 and addr1 is individual address: time to
+        *      transmit one ACK plus SIFS
+        *   if more fragments = 1 and addr1 is individual address: time to
+        *      transmit next fragment plus 2 x ACK plus 3 x SIFS
+        *
+        * IEEE 802.11, 9.6:
+        * - control response frame (CTS or ACK) shall be transmitted using the
+        *   same rate as the immediately previous frame in the frame exchange
+        *   sequence, if this rate belongs to the PHY mandatory rates, or else
+        *   at the highest possible rate belonging to the PHY rates in the
+        *   BSSBasicRateSet
+        */
+
+       if (WLAN_FC_GET_TYPE(tx->fc) == WLAN_FC_TYPE_CTRL) {
+               /* TODO: These control frames are not currently sent by
+                * 80211.o, but should they be implemented, this function
+                * needs to be updated to support duration field calculation.
+                *
+                * RTS: time needed to transmit pending data/mgmt frame plus
+                *    one CTS frame plus one ACK frame plus 3 x SIFS
+                * CTS: duration of immediately previous RTS minus time
+                *    required to transmit CTS and its SIFS
+                * ACK: 0 if immediately previous directed data/mgmt had
+                *    more=0, with more=1 duration in ACK frame is duration
+                *    from previous frame minus time needed to transmit ACK
+                *    and its SIFS
+                * PS Poll: BIT(15) | BIT(14) | aid
+                */
+               return 0;
+       }
+
+       /* data/mgmt */
+       if (0 /* FIX: data/mgmt during CFP */)
+               return 32768;
+
+       if (group_addr) /* Group address as the destination - no ACK */
+               return 0;
+
+       /* Individual destination address:
+        * IEEE 802.11, Ch. 9.6 (after IEEE 802.11g changes)
+        * CTS and ACK frames shall be transmitted using the highest rate in
+        * basic rate set that is less than or equal to the rate of the
+        * immediately previous frame and that is using the same modulation
+        * (CCK or OFDM). If no basic rate set matches with these requirements,
+        * the highest mandatory rate of the PHY that is less than or equal to
+        * the rate of the previous frame is used.
+        * Mandatory rates for IEEE 802.11g PHY: 1, 2, 5.5, 11, 6, 12, 24 Mbps
+        */
+       rate = -1;
+       mrate = 10; /* use 1 Mbps if everything fails */
+       for (i = 0; i < local->num_curr_rates; i++) {
+               struct ieee80211_rate *r = &local->curr_rates[i];
+               if (r->rate > txrate->rate)
+                       break;
+
+               if (IEEE80211_RATE_MODULATION(txrate->flags) !=
+                   IEEE80211_RATE_MODULATION(r->flags))
+                       continue;
+
+               if (r->flags & IEEE80211_RATE_BASIC)
+                       rate = r->rate;
+               else if (r->flags & IEEE80211_RATE_MANDATORY)
+                       mrate = r->rate;
+       }
+       if (rate == -1) {
+               /* No matching basic rate found; use highest suitable mandatory
+                * PHY rate */
+               rate = mrate;
+       }
+
+       /* Time needed to transmit ACK
+        * (10 bytes + 4-byte FCS = 112 bits) plus SIFS; rounded up
+        * to closest integer */
+
+       dur = ieee80211_frame_duration(local, 10, rate, erp,
+                                       local->short_preamble);
+
+       if (next_frag_len) {
+               /* Frame is fragmented: duration increases with time needed to
+                * transmit next fragment plus ACK and 2 x SIFS. */
+               dur *= 2; /* ACK + SIFS */
+               /* next fragment */
+               dur += ieee80211_frame_duration(local, next_frag_len,
+                                               txrate->rate, erp,
+                                               local->short_preamble);
+       }
+
+        return dur;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_tx_h_misc(struct ieee80211_txrx_data *tx)
+{
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
+       u16 dur;
+       struct ieee80211_tx_control *control = tx->u.tx.control;
+
+       if (!MULTICAST_ADDR(hdr->addr1)) {
+               if (tx->skb->len >= tx->local->rts_threshold &&
+                   tx->local->rts_threshold < IEEE80211_MAX_RTS_THRESHOLD) {
+                       control->use_rts_cts = 1;
+                       control->retry_limit =
+                               tx->local->long_retry_limit;
+               } else {
+                       control->retry_limit =
+                               tx->local->short_retry_limit;
+               }
+       } else {
+               control->retry_limit = 1;
+       }
+
+       if (tx->fragmented) {
+               /* Do not use multiple retry rates when sending fragmented
+                * frames.
+                * TODO: The last fragment could still use multiple retry
+                * rates. */
+//             control->alt_retry_rate = -1;
+       }
+
+       /* Use CTS protection for unicast frames sent using extended rates if
+        * there are associated non-ERP stations and RTS/CTS is not configured
+        * for the frame. */
+       if (tx->local->conf.phymode == MODE_IEEE80211G &&
+           (tx->u.tx.rate->flags & IEEE80211_RATE_ERP) &&
+           tx->u.tx.unicast &&
+           tx->local->cts_protect_erp_frames &&
+           !control->use_rts_cts)
+               control->use_cts_protect = 1;
+
+
+       /* Setup duration field for the first fragment of the frame. Duration
+        * for remaining fragments will be updated when they are being sent
+        * to low-level driver in ieee80211_tx(). */
+       dur = ieee80211_duration(tx, MULTICAST_ADDR(hdr->addr1),
+                                tx->fragmented ? tx->u.tx.extra_frag[0]->len :
+                                0);
+       hdr->duration_id = cpu_to_le16(dur);
+
+       if (control->use_rts_cts || control->use_cts_protect) {
+               struct ieee80211_rate *rate;
+               int erp = tx->u.tx.rate->flags & IEEE80211_RATE_ERP;
+
+               /* Do not use multiple retry rates when using RTS/CTS */
+//             control->alt_retry_rate = -1;
+
+               /* Use min(data rate, max base rate) as CTS/RTS rate */
+               rate = tx->u.tx.rate;
+               while (rate > tx->local->curr_rates &&
+                      !(rate->flags & IEEE80211_RATE_BASIC))
+                       rate--;
+
+
+               if (control->use_rts_cts)
+                       dur += ieee80211_frame_duration(tx->local, 10,
+                                                       rate->rate, erp,
+                                                       tx->local->
+                                                       short_preamble);
+               dur += ieee80211_frame_duration(tx->local, tx->skb->len,
+                                               tx->u.tx.rate->rate, erp,
+                                               tx->u.tx.short_preamble);
+               control->rts_cts_duration = dur;
+               control->rts_cts_rate = rate->val;
+       }
+
+       if (tx->sta) {
+               tx->sta->tx_packets++;
+               tx->sta->tx_fragments++;
+               tx->sta->tx_bytes += tx->skb->len;
+               if (tx->u.tx.extra_frag) {
+                       int i;
+                       tx->sta->tx_fragments += tx->u.tx.num_extra_frag;
+                       for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
+                               tx->sta->tx_bytes +=
+                                       tx->u.tx.extra_frag[i]->len;
+                       }
+               }
+        }
+       tx->local->scan.txrx_count++;
+
+       return TXRX_CONTINUE;
+}
+
+
+static void ieee80211_rate_limit(unsigned long data)
+{
+       struct ieee80211_local *local = (struct ieee80211_local *) data;
+
+       if (local->rate_limit) {
+                local->rate_limit_bucket += local->rate_limit;
+                if (local->rate_limit_bucket > local->rate_limit_burst)
+                        local->rate_limit_bucket = local->rate_limit_burst;
+               local->rate_limit_timer.expires = jiffies + HZ;
+               add_timer(&local->rate_limit_timer);
+        }
+
+}
+
+static ieee80211_txrx_result
+ieee80211_tx_h_rate_limit(struct ieee80211_txrx_data *tx)
+{
+
+       if (likely(!tx->local->rate_limit || tx->u.tx.unicast))
+                return TXRX_CONTINUE;
+
+       /* rate limit */
+        if (tx->local->rate_limit_bucket) {
+                tx->local->rate_limit_bucket--;
+                return TXRX_CONTINUE;
+        }
+
+       I802_DEBUG_INC(tx->local->tx_handlers_drop_rate_limit);
+       return TXRX_DROP;
+}
+
+
+
+static ieee80211_txrx_result
+ieee80211_tx_h_check_assoc(struct ieee80211_txrx_data *tx)
+{
+#ifdef CONFIG_IEEE80211_VERBOSE_DEBUG
+       struct sk_buff *skb = tx->skb;
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
+#endif /* CONFIG_IEEE80211_VERBOSE_DEBUG */
+       u32 sta_flags;
+
+       if (unlikely(tx->local->sta_scanning != 0) &&
+           (WLAN_FC_GET_TYPE(tx->fc) != WLAN_FC_TYPE_MGMT ||
+            WLAN_FC_GET_STYPE(tx->fc) != WLAN_FC_STYPE_PROBE_REQ))
+               return TXRX_DROP;
+
+       if (tx->u.tx.ps_buffered)
+               return TXRX_CONTINUE;
+
+       sta_flags = tx->sta ? tx->sta->flags : 0;
+
+       if (likely(tx->u.tx.unicast)) {
+               if (unlikely(!(sta_flags & WLAN_STA_ASSOC) &&
+                            tx->local->conf.mode != IW_MODE_ADHOC &&
+                            WLAN_FC_GET_TYPE(tx->fc) == WLAN_FC_TYPE_DATA)) {
+#ifdef CONFIG_IEEE80211_VERBOSE_DEBUG
+                       printk(KERN_DEBUG "%s: dropped data frame to not "
+                              "associated station " MACSTR "\n",
+                              tx->dev->name, MAC2STR(hdr->addr1));
+#endif /* CONFIG_IEEE80211_VERBOSE_DEBUG */
+                       I802_DEBUG_INC(tx->local->tx_handlers_drop_not_assoc);
+                       return TXRX_DROP;
+               }
+       } else {
+               if (unlikely(WLAN_FC_GET_TYPE(tx->fc) == WLAN_FC_TYPE_DATA &&
+                            tx->local->num_sta == 0 &&
+                            !tx->local->allow_broadcast_always &&
+                            tx->local->conf.mode != IW_MODE_ADHOC)) {
+                       /*
+                        * No associated STAs - no need to send multicast
+                        * frames.
+                        */
+                       return TXRX_DROP;
+               }
+               return TXRX_CONTINUE;
+       }
+
+       if (unlikely(!tx->u.tx.mgmt_interface && tx->sdata->ieee802_1x &&
+                    !(sta_flags & WLAN_STA_AUTHORIZED))) {
+#ifdef CONFIG_IEEE80211_DEBUG
+               struct ieee80211_hdr *hdr =
+                       (struct ieee80211_hdr *) tx->skb->data;
+               printk(KERN_DEBUG "%s: dropped frame to " MACSTR
+                      " (unauthorized port)\n", tx->dev->name,
+                      MAC2STR(hdr->addr1));
+#endif
+               I802_DEBUG_INC(tx->local->tx_handlers_drop_unauth_port);
+               return TXRX_DROP;
+       }
+
+       return TXRX_CONTINUE;
+}
+
+
+/* This function is called whenever the AP is about to exceed the maximum limit
+ * of buffered frames for power saving STAs. This situation should not really
+ * happen often during normal operation, so dropping the oldest buffered packet
+ * from each queue should be OK to make some room for new frames. */
+static void purge_old_ps_buffers(struct ieee80211_local *local)
+{
+       int total = 0, purged = 0;
+       struct sk_buff *skb;
+       struct list_head *ptr;
+
+       spin_lock_bh(&local->sub_if_lock);
+       list_for_each(ptr, &local->sub_if_list) {
+               struct ieee80211_if_norm *norm;
+               struct ieee80211_sub_if_data *sdata =
+                       list_entry(ptr, struct ieee80211_sub_if_data, list);
+               if (sdata->dev == local->mdev ||
+                   sdata->type != IEEE80211_SUB_IF_TYPE_NORM)
+                       continue;
+               norm = &sdata->u.norm;
+               skb = skb_dequeue(&norm->ps_bc_buf);
+               if (skb) {
+                       purged++;
+                       dev_kfree_skb(skb);
+               }
+               total += skb_queue_len(&norm->ps_bc_buf);
+       }
+       spin_unlock_bh(&local->sub_if_lock);
+
+       spin_lock_bh(&local->sta_lock);
+       list_for_each(ptr, &local->sta_list) {
+               struct sta_info *sta =
+                       list_entry(ptr, struct sta_info, list);
+               skb = skb_dequeue(&sta->ps_tx_buf);
+               if (skb) {
+                       purged++;
+                       dev_kfree_skb(skb);
+               }
+               total += skb_queue_len(&sta->ps_tx_buf);
+       }
+       spin_unlock_bh(&local->sta_lock);
+
+       local->total_ps_buffered = total;
+       printk(KERN_DEBUG "%s: PS buffers full - purged %d frames\n",
+              local->mdev->name, purged);
+}
+
+
+static inline ieee80211_txrx_result
+ieee80211_tx_h_multicast_ps_buf(struct ieee80211_txrx_data *tx)
+{
+       /* broadcast/multicast frame */
+       /* If any of the associated stations is in power save mode,
+        * the frame is buffered to be sent after DTIM beacon frame */
+       if (tx->local->hw->host_broadcast_ps_buffering &&
+           tx->sdata->type != IEEE80211_SUB_IF_TYPE_WDS &&
+           tx->sdata->bss && atomic_read(&tx->sdata->bss->num_sta_ps) &&
+           !(tx->fc & WLAN_FC_ORDER)) {
+               if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
+                       purge_old_ps_buffers(tx->local);
+               if (skb_queue_len(&tx->sdata->bss->ps_bc_buf) >=
+                   AP_MAX_BC_BUFFER) {
+                       if (net_ratelimit()) {
+                               printk(KERN_DEBUG "%s: BC TX buffer full - "
+                                      "dropping the oldest frame\n",
+                                      tx->dev->name);
+                       }
+                       dev_kfree_skb(skb_dequeue(&tx->sdata->bss->ps_bc_buf));
+               } else
+                       tx->local->total_ps_buffered++;
+                skb_queue_tail(&tx->sdata->bss->ps_bc_buf, tx->skb);
+               return TXRX_QUEUED;
+        }
+
+       return TXRX_CONTINUE;
+}
+
+
+static inline ieee80211_txrx_result
+ieee80211_tx_h_unicast_ps_buf(struct ieee80211_txrx_data *tx)
+{
+       struct sta_info *sta = tx->sta;
+
+       if (unlikely(!sta ||
+                    (WLAN_FC_GET_TYPE(tx->fc) == WLAN_FC_TYPE_MGMT &&
+                     WLAN_FC_GET_STYPE(tx->fc) == WLAN_FC_STYPE_PROBE_RESP)))
+               return TXRX_CONTINUE;
+
+       if (unlikely((sta->flags & WLAN_STA_PS) && !sta->pspoll)) {
+               struct ieee80211_tx_packet_data *pkt_data;
+#ifdef IEEE80211_VERBOSE_DEBUG_PS
+               printk(KERN_DEBUG "STA " MACSTR " aid %d: PS buffer (entries "
+                      "before %d)\n",
+                      MAC2STR(sta->addr), sta->aid,
+                      skb_queue_len(&sta->ps_tx_buf));
+#endif /* IEEE80211_VERBOSE_DEBUG_PS */
+               sta->flags |= WLAN_STA_TIM;
+               if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
+                       purge_old_ps_buffers(tx->local);
+               if (skb_queue_len(&sta->ps_tx_buf) >= STA_MAX_TX_BUFFER) {
+                       struct sk_buff *old = skb_dequeue(&sta->ps_tx_buf);
+                       if (net_ratelimit()) {
+                               printk(KERN_DEBUG "%s: STA " MACSTR " TX "
+                                      "buffer full - dropping oldest frame\n",
+                                      tx->dev->name, MAC2STR(sta->addr));
+                       }
+                       dev_kfree_skb(old);
+               } else
+                       tx->local->total_ps_buffered++;
+               /* Queue frame to be sent after STA sends an PS Poll frame */
+               if (skb_queue_empty(&sta->ps_tx_buf) && tx->local->hw->set_tim)
+                       tx->local->hw->set_tim(tx->dev, sta->aid, 1);
+               pkt_data = (struct ieee80211_tx_packet_data *)tx->skb->cb;
+               pkt_data->jiffies = jiffies;
+                skb_queue_tail(&sta->ps_tx_buf, tx->skb);
+               return TXRX_QUEUED;
+       }
+#ifdef IEEE80211_VERBOSE_DEBUG_PS
+       else if (unlikely(sta->flags & WLAN_STA_PS)) {
+               printk(KERN_DEBUG "%s: STA " MACSTR " in PS mode, but pspoll "
+                      "set -> send frame\n", tx->dev->name,
+                      MAC2STR(sta->addr));
+       }
+#endif /* IEEE80211_VERBOSE_DEBUG_PS */
+       sta->pspoll = 0;
+
+       return TXRX_CONTINUE;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_tx_h_ps_buf(struct ieee80211_txrx_data *tx)
+{
+       if (unlikely(tx->u.tx.ps_buffered))
+               return TXRX_CONTINUE;
+
+       if (tx->u.tx.unicast)
+               return ieee80211_tx_h_unicast_ps_buf(tx);
+       else
+               return ieee80211_tx_h_multicast_ps_buf(tx);
+}
+
+
+static void inline ieee80211_tx_prepare(struct ieee80211_txrx_data *tx,
+                                       struct sk_buff *skb,
+                                       struct net_device *dev,
+                                       struct ieee80211_tx_control *control)
+{
+       struct ieee80211_local *local = dev->priv;
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
+        struct ieee80211_tx_packet_data *pkt_data;
+       int hdrlen;
+
+        pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
+
+       memset(tx, 0, sizeof(*tx));
+       tx->skb = skb;
+       tx->dev = pkt_data->sdata->dev; /* use original interface */
+       tx->local = local;
+        tx->sdata = pkt_data->sdata;
+       tx->sta = sta_info_get(local, hdr->addr1);
+       tx->fc = le16_to_cpu(hdr->frame_control);
+        control->power_level = local->conf.power_level;
+       tx->u.tx.control = control;
+        tx->u.tx.unicast = !MULTICAST_ADDR(hdr->addr1);
+        control->no_ack = MULTICAST_ADDR(hdr->addr1);
+       tx->fragmented = local->fragmentation_threshold <
+               IEEE80211_MAX_FRAG_THRESHOLD && tx->u.tx.unicast &&
+               skb->len + 4 /* FCS */ > local->fragmentation_threshold &&
+               (local->hw->set_frag_threshold == NULL);
+       if (tx->sta == NULL)
+               control->clear_dst_mask = 1;
+       else if (tx->sta->clear_dst_mask) {
+               control->clear_dst_mask = 1;
+               tx->sta->clear_dst_mask = 0;
+       }
+       control->antenna_sel = local->conf.antenna_sel;
+       if (local->sta_antenna_sel != STA_ANTENNA_SEL_AUTO && tx->sta)
+               control->antenna_sel = tx->sta->antenna_sel;
+       hdrlen = ieee80211_get_hdrlen(tx->fc);
+       if (skb->len > hdrlen + sizeof(rfc1042_header) + 2) {
+               u8 *pos = &skb->data[hdrlen + sizeof(rfc1042_header)];
+               tx->ethertype = (pos[0] << 8) | pos[1];
+       }
+
+}
+
+
+static int ieee80211_tx(struct net_device *dev, struct sk_buff *skb,
+                       struct ieee80211_tx_control *control, int mgmt)
+{
+       struct ieee80211_local *local = dev->priv;
+       struct sta_info *sta;
+       ieee80211_tx_handler *handler;
+       struct ieee80211_txrx_data tx;
+       ieee80211_txrx_result res = TXRX_DROP;
+        int ret, i;
+
+       if (unlikely(skb->len < 10)) {
+               dev_kfree_skb(skb);
+               return 0;
+       }
+
+       ieee80211_tx_prepare(&tx, skb, dev, control);
+       sta = tx.sta;
+       tx.u.tx.mgmt_interface = mgmt;
+
+       for (handler = local->tx_handlers; *handler != NULL; handler++) {
+               res = (*handler)(&tx);
+               if (res != TXRX_CONTINUE)
+                       break;
+       }
+
+       skb = tx.skb; /* handlers are allowed to change skb */
+
+       if (sta)
+               sta_info_release(local, sta);
+
+       if (unlikely(res == TXRX_DROP)) {
+               I802_DEBUG_INC(local->tx_handlers_drop);
+               goto drop;
+       }
+
+       if (unlikely(res == TXRX_QUEUED)) {
+               I802_DEBUG_INC(local->tx_handlers_queued);
+               return 0;
+       }
+
+       ieee80211_dump_frame(dev->name, "TX to low-level driver", skb);
+       ret = local->hw->tx(dev, skb, control);
+#ifdef IEEE80211_LEDS
+       if (!ret && local->tx_led_counter++ == 0) {
+                ieee80211_tx_led(1, dev);
+        }
+#endif /* IEEE80211_LEDS */
+       if (tx.u.tx.extra_frag) {
+               if (ret > 0) {
+                       /* Must free all fragments and return 0 since skb data
+                        * has been fragmented into multiple buffers.
+                        * TODO: could free extra fragments and restore skb to
+                        * the original form since the data is still there and
+                        * then return nonzero so that Linux netif would
+                        * retry. */
+                       goto drop;
+               }
+
+               skb = NULL; /* skb is now owned by low-level driver */
+               control->use_rts_cts = 0;
+               control->use_cts_protect = 0;
+               control->clear_dst_mask = 0;
+               for (i = 0; i < tx.u.tx.num_extra_frag; i++) {
+                       int next_len, dur;
+                       struct ieee80211_hdr *hdr =
+                               (struct ieee80211_hdr *)
+                               tx.u.tx.extra_frag[i]->data;
+                       if (i + 1 < tx.u.tx.num_extra_frag)
+                               next_len = tx.u.tx.extra_frag[i + 1]->len;
+                       else {
+                               next_len = 0;
+                               tx.u.tx.rate = tx.u.tx.last_frag_rate;
+                               tx.u.tx.control->tx_rate = tx.u.tx.rate->val;
+//                             tx.u.tx.control->rateidx =
+//                                     tx.u.tx.last_frag_rateidx;
+                               tx.u.tx.control->rate_ctrl_probe =
+                                       tx.u.tx.probe_last_frag;
+                       }
+                       dur = ieee80211_duration(&tx, 0, next_len);
+                       hdr->duration_id = cpu_to_le16(dur);
+
+                       ieee80211_dump_frame(dev->name,
+                                            "TX to low-level driver", skb);
+                       ret = local->hw->tx(dev, tx.u.tx.extra_frag[i],
+                                           control);
+                       if (ret > 0)
+                               goto drop;
+#ifdef IEEE80211_LEDS
+                       if (local->tx_led_counter++ == 0) {
+                                ieee80211_tx_led(1, dev);
+                        }
+#endif /* IEEE80211_LEDS */
+                       tx.u.tx.extra_frag[i] = NULL;
+               }
+               kfree(tx.u.tx.extra_frag);
+        }
+        if (ret == -1)
+               ret = 0;
+       return ret;
+
+ drop:
+       if (skb)
+               dev_kfree_skb(skb);
+       for (i = 0; i < tx.u.tx.num_extra_frag; i++)
+               if (tx.u.tx.extra_frag[i])
+                       dev_kfree_skb(tx.u.tx.extra_frag[i]);
+        kfree(tx.u.tx.extra_frag);
+       return 0;
+}
+
+
+static int ieee80211_master_start_xmit(struct sk_buff *skb,
+                                      struct net_device *dev)
+{
+       struct ieee80211_tx_control control;
+       struct ieee80211_tx_packet_data *pkt_data;
+       struct ieee80211_sub_if_data *sdata;
+       int ret = 1;
+
+       sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+
+       /*
+        * copy control out of the skb so other people can use skb->cb
+        */
+       pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
+       if (unlikely(pkt_data->magic != IEEE80211_CB_MAGIC)) {
+               printk(KERN_WARNING "%s: Someone messed with our skb->cb\n",
+                      dev->name);
+               dev_kfree_skb(skb);
+               return 0;
+       }
+       memcpy(&control, &pkt_data->control,
+              sizeof(struct ieee80211_tx_control));
+
+       ret = ieee80211_tx(dev, skb, &control,
+                          pkt_data->sdata->type ==
+                          IEEE80211_SUB_IF_TYPE_MGMT);
+
+        return ret;
+}
+
+
+/**
+ * ieee80211_subif_start_xmit - netif start_xmit function for Ethernet-type
+ * subinterfaces (wlan#, WDS, and VLAN interfaces)
+ * @skb: packet to be sent
+ * @dev: incoming interface
+ *
+ * Returns: 0 on success (and frees skb in this case) or 1 on failure (skb will
+ * not be freed, and caller is responsible for either retrying later or freeing
+ * skb).
+ *
+ * This function takes in an Ethernet header and encapsulates it with suitable
+ * IEEE 802.11 header based on which interface the packet is coming in. The
+ * encapsulated packet will then be passed to master interface, wlan#.11, for
+ * transmission (through low-level driver).
+ */
+static int ieee80211_subif_start_xmit(struct sk_buff *skb,
+                                     struct net_device *dev)
+{
+        struct ieee80211_local *local = (struct ieee80211_local *) dev->priv;
+       struct ieee80211_tx_packet_data *pkt_data;
+        struct ieee80211_sub_if_data *sdata;
+       int ret = 1, head_need;
+       u16 ethertype, hdrlen, fc;
+       struct ieee80211_hdr hdr;
+       u8 *encaps_data;
+        int encaps_len, skip_header_bytes;
+       int nh_pos, h_pos, no_encrypt = 0;
+       struct sta_info *sta;
+
+        sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+       if (unlikely(skb->len < ETH_HLEN)) {
+               printk(KERN_DEBUG "%s: short skb (len=%d)\n",
+                      dev->name, skb->len);
+               ret = 0;
+               goto fail;
+       }
+
+       nh_pos = skb->nh.raw - skb->data;
+       h_pos = skb->h.raw - skb->data;
+
+       /* convert Ethernet header to proper 802.11 header (based on
+        * operation mode) */
+       ethertype = (skb->data[12] << 8) | skb->data[13];
+       /* TODO: handling for 802.1x authorized/unauthorized port */
+       fc = (WLAN_FC_TYPE_DATA << 2) | (WLAN_FC_STYPE_DATA << 4);
+
+        if (likely(sdata->type == IEEE80211_SUB_IF_TYPE_NORM ||
+                  sdata->type == IEEE80211_SUB_IF_TYPE_VLAN)) {
+               if (local->conf.mode == IW_MODE_MASTER) {
+                       fc |= WLAN_FC_FROMDS;
+                       /* DA BSSID SA */
+                       memcpy(hdr.addr1, skb->data, ETH_ALEN);
+                       memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
+                       memcpy(hdr.addr3, skb->data + ETH_ALEN, ETH_ALEN);
+               } else if (local->conf.mode == IW_MODE_INFRA) {
+                       fc |= WLAN_FC_TODS;
+                       /* BSSID SA DA */
+                       memcpy(hdr.addr1, local->bssid, ETH_ALEN);
+                       memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
+                       memcpy(hdr.addr3, skb->data, ETH_ALEN);
+               } else if (local->conf.mode == IW_MODE_ADHOC) {
+                       /* DA SA BSSID */
+                       memcpy(hdr.addr1, skb->data, ETH_ALEN);
+                       memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
+                       memcpy(hdr.addr3, local->bssid, ETH_ALEN);
+               }
+                hdrlen = 24;
+        } else if (sdata->type == IEEE80211_SUB_IF_TYPE_WDS) {
+                fc |= WLAN_FC_FROMDS | WLAN_FC_TODS;
+               /* RA TA DA SA */
+                memcpy(hdr.addr1, sdata->u.wds.remote_addr, ETH_ALEN);
+                memcpy(hdr.addr2, dev->dev_addr, ETH_ALEN);
+                memcpy(hdr.addr3, skb->data, ETH_ALEN);
+                memcpy(hdr.addr4, skb->data + ETH_ALEN, ETH_ALEN);
+                hdrlen = 30;
+        } else if (sdata->type == IEEE80211_SUB_IF_TYPE_STA) {
+               if (local->conf.mode == IW_MODE_INFRA) {
+                       fc |= WLAN_FC_TODS;
+                       /* BSSID SA DA */
+                       memcpy(hdr.addr1, sdata->u.sta.bssid, ETH_ALEN);
+                       memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
+                       memcpy(hdr.addr3, skb->data, ETH_ALEN);
+               } else {
+                       /* DA SA BSSID */
+                       memcpy(hdr.addr1, skb->data, ETH_ALEN);
+                       memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
+                       memcpy(hdr.addr3, sdata->u.sta.bssid, ETH_ALEN);
+               }
+               hdrlen = 24;
+        } else {
+                ret = 0;
+                goto fail;
+        }
+       
+       /* receiver is QoS enabled, use a QoS type frame */
+       sta = sta_info_get(local, hdr.addr1);
+       if (sta) {
+               if (sta->flags & WLAN_STA_WME) {
+                       fc |= WLAN_FC_STYPE_QOS_DATA << 4;
+                       hdrlen += 2;
+               }
+               sta_info_release(local, sta);
+       }
+       
+       hdr.frame_control = cpu_to_le16(fc);
+       hdr.duration_id = 0;
+       hdr.seq_ctrl = 0;
+
+       skip_header_bytes = ETH_HLEN;
+       if (ethertype == ETH_P_AARP || ethertype == ETH_P_IPX) {
+               encaps_data = bridge_tunnel_header;
+               encaps_len = sizeof(bridge_tunnel_header);
+               skip_header_bytes -= 2;
+       } else if (ethertype >= 0x600) {
+               encaps_data = rfc1042_header;
+               encaps_len = sizeof(rfc1042_header);
+               skip_header_bytes -= 2;
+       } else {
+               encaps_data = NULL;
+               encaps_len = 0;
+       }
+
+       skb_pull(skb, skip_header_bytes);
+       nh_pos -= skip_header_bytes;
+       h_pos -= skip_header_bytes;
+
+       /* TODO: implement support for fragments so that there is no need to
+        * reallocate and copy payload; it might be enough to support one
+        * extra fragment that would be copied in the beginning of the frame
+        * data.. anyway, it would be nice to include this into skb structure
+        * somehow
+        *
+        * There are few options for this:
+        * use skb->cb as an extra space for 802.11 header
+        * allocate new buffer if not enough headroom
+        * make sure that there is enough headroom in every skb by increasing
+        * build in headroom in __dev_alloc_skb() (linux/skbuff.h) and
+        * alloc_skb() (net/core/skbuff.c)
+        */
+       head_need = hdrlen + encaps_len + (local->hw->extra_hdr_room ? 2 : 0);
+       head_need -= skb_headroom(skb);
+
+       /* We are going to modify skb data, so make a copy of it if happens to
+        * be cloned. This could happen, e.g., with Linux bridge code passing
+        * us broadcast frames. */
+
+       if (head_need > 0 || skb_cloned(skb)) {
+#if 0
+               printk(KERN_DEBUG "%s: need to reallocate buffer for %d bytes "
+                      "of headroom\n", dev->name, head_need);
+#endif
+
+               if (skb_cloned(skb))
+                       I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
+               else
+                       I802_DEBUG_INC(local->tx_expand_skb_head);
+               /* Since we have to reallocate the buffer, make sure that there
+                * is enough room for possible WEP IV/ICV and TKIP (8 bytes
+                * before payload and 12 after). */
+               if (pskb_expand_head(skb, (head_need > 0 ? head_need + 8 : 8),
+                                    12, GFP_ATOMIC)) {
+                       printk(KERN_DEBUG "%s: failed to reallocate TX buffer"
+                              "\n", dev->name);
+                       goto fail;
+               }
+       }
+
+       if (encaps_data) {
+               memcpy(skb_push(skb, encaps_len), encaps_data, encaps_len);
+               nh_pos += encaps_len;
+               h_pos += encaps_len;
+       }
+       memcpy(skb_push(skb, hdrlen), &hdr, hdrlen);
+       nh_pos += hdrlen;
+       h_pos += hdrlen;
+
+       pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
+       memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
+       pkt_data->magic = IEEE80211_CB_MAGIC;
+       pkt_data->sdata = sdata;
+       pkt_data->control.do_not_encrypt = no_encrypt;
+
+       skb->dev = sdata->master;
+       sdata->stats.tx_packets++;
+       sdata->stats.tx_bytes += skb->len;
+
+       /* Update skb pointers to various headers since this modified frame
+        * is going to go through Linux networking code that may potentially
+        * need things like pointer to IP header. */
+       skb->mac.raw = skb->data;
+       skb->nh.raw = skb->data + nh_pos;
+       skb->h.raw = skb->data + h_pos;
+
+
+        dev_queue_xmit(skb);
+
+        return 0;
+
+ fail:
+       if (!ret)
+                dev_kfree_skb(skb);
+
+       return ret;
+}
+
+
+/*
+ * This is the transmit routine for the 802.11 type interfaces
+ * called by upper layers of the linux networking
+ * stack when it has a frame to transmit
+ */
+static int
+ieee80211_mgmt_start_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+       struct ieee80211_sub_if_data *sdata;
+       struct ieee80211_tx_packet_data *pkt_data;
+       struct ieee80211_hdr *hdr;
+       u16 fc;
+
+       sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+
+       if (skb->len < 10) {
+               dev_kfree_skb(skb);
+               return 0;
+       }
+
+       hdr = (struct ieee80211_hdr *) skb->data;
+       fc = le16_to_cpu(hdr->frame_control);
+
+        pkt_data = (struct ieee80211_tx_packet_data *)skb->cb;
+       memset(pkt_data, 0, sizeof(struct ieee80211_tx_packet_data));
+       pkt_data->magic = IEEE80211_CB_MAGIC;
+        pkt_data->sdata = sdata;
+
+       if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
+           WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_RESP)
+               pkt_data->control.pkt_type = PKT_PROBE_RESP;
+
+        skb->priority = 20; /* use hardcode priority for mgmt TX queue */
+       skb->dev = sdata->master;
+
+       /*
+        * We're using the protocol field of the the frame control header
+        * to request TX callback for hostapd. BIT(1) is checked.
+        */
+       if ((fc & BIT(1)) == BIT(1)) {
+               pkt_data->control.req_tx_status = 1;
+               fc &= ~BIT(1);
+               hdr->frame_control = cpu_to_le16(fc);
+       }
+
+
+
+       pkt_data->control.do_not_encrypt = !(fc & WLAN_FC_ISWEP);
+
+       sdata->stats.tx_packets++;
+        sdata->stats.tx_bytes += skb->len;
+
+        dev_queue_xmit(skb);
+
+       return 0;
+}
+
+
+static void ieee80211_beacon_add_tim(struct ieee80211_local *local,
+                                    struct ieee80211_if_norm *bss,
+                                    struct sk_buff *skb)
+{
+       u8 *pos, *tim;
+       int aid0 = 0;
+       int i, num_bits = 0, n1, n2;
+       u8 bitmap[251];
+
+       /* Generate bitmap for TIM only if there are any STAs in power save
+        * mode. */
+       if (atomic_read(&bss->num_sta_ps) > 0 && bss->max_aid > 0) {
+               memset(bitmap, 0, sizeof(bitmap));
+               spin_lock_bh(&local->sta_lock);
+               for (i = 0; i < bss->max_aid; i++) {
+                       if (bss->sta_aid[i] &&
+                           (!skb_queue_empty(&bss->sta_aid[i]->ps_tx_buf) ||
+                            !skb_queue_empty(&bss->sta_aid[i]->tx_filtered)))
+                       {
+                               bitmap[(i + 1) / 8] |= 1 << (i + 1) % 8;
+                               num_bits++;
+                       }
+               }
+               spin_unlock_bh(&local->sta_lock);
+       }
+
+       if (bss->dtim_count == 0)
+               bss->dtim_count = bss->dtim_period - 1;
+       else
+               bss->dtim_count--;
+
+       tim = pos = (u8 *) skb_put(skb, 6);
+       *pos++ = WLAN_EID_TIM;
+       *pos++ = 4;
+       *pos++ = bss->dtim_count;
+       *pos++ = bss->dtim_period;
+
+       if (bss->dtim_count == 0 && !skb_queue_empty(&bss->ps_bc_buf)) {
+               aid0 = 1;
+       }
+
+       if (num_bits) {
+               /* Find largest even number N1 so that bits numbered 1 through
+                * (N1 x 8) - 1 in the bitmap are 0 and number N2 so that bits
+                * (N2 + 1) x 8 through 2007 are 0. */
+               n1 = 0;
+               for (i = 0; i < sizeof(bitmap); i++) {
+                       if (bitmap[i]) {
+                               n1 = i & 0xfe;
+                               break;
+                       }
+               }
+               n2 = n1;
+               for (i = sizeof(bitmap) - 1; i >= n1; i--) {
+                       if (bitmap[i]) {
+                               n2 = i;
+                               break;
+                       }
+               }
+
+               /* Bitmap control */
+               *pos++ = n1 | (aid0 ? 1 : 0);
+               /* Part Virt Bitmap */
+               memcpy(pos, bitmap + n1, n2 - n1 + 1);
+
+               tim[1] = n2 - n1 + 4;
+               skb_put(skb, n2 - n1);
+       } else {
+               *pos++ = aid0 ? 1 : 0; /* Bitmap control */
+               *pos++ = 0; /* Part Virt Bitmap */
+       }
+}
+
+
+
+
+struct sk_buff * ieee80211_beacon_get(struct net_device *dev, int bss_idx,
+                                     struct ieee80211_tx_control *control)
+{
+       struct ieee80211_local *local = dev->priv;
+       struct sk_buff *skb;
+       struct net_device *bdev;
+       struct ieee80211_sub_if_data *sdata = NULL;
+       struct ieee80211_if_norm *norm = NULL;
+       struct ieee80211_rate *rate;
+       struct rate_control_extra extra;
+       u8 *b_head, *b_tail;
+       int bh_len, bt_len;
+
+
+       spin_lock_bh(&local->sub_if_lock);
+       if (bss_idx < 0 || bss_idx >= local->bss_dev_count)
+               bdev = NULL;
+       else {
+               bdev = local->bss_devs[bss_idx];
+               sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
+               norm = &sdata->u.norm;
+       }
+       spin_unlock_bh(&local->sub_if_lock);
+
+       if (bdev == NULL || norm == NULL || norm->beacon_head == NULL) {
+#ifdef CONFIG_IEEE80211_VERBOSE_DEBUG
+               if (net_ratelimit())
+                       printk(KERN_DEBUG "no beacon data avail for idx=%d "
+                              "(%s)\n", bss_idx, bdev ? bdev->name : "N/A");
+#endif /* CONFIG_IEEE80211_VERBOSE_DEBUG */
+               return NULL;
+       }
+
+       /* Assume we are generating the normal beacon locally */
+       b_head = norm->beacon_head;
+       b_tail = norm->beacon_tail;
+       bh_len = norm->beacon_head_len;
+       bt_len = norm->beacon_tail_len;
+
+
+       skb = dev_alloc_skb(bh_len + bt_len + 256 /* maximum TIM len */);
+       if (!skb)
+               return NULL;
+
+       memcpy(skb_put(skb, bh_len), b_head, bh_len);
+
+       ieee80211_beacon_add_tim(local, norm, skb);
+
+       if (b_tail) {
+               memcpy(skb_put(skb, bt_len), b_tail, bt_len);
+       }
+
+       memset(&extra, 0, sizeof(extra));
+       extra.endidx = local->num_curr_rates;
+
+
+       rate = rate_control_get_rate(dev, skb, &extra);
+       if (rate == NULL) {
+               if (net_ratelimit()) {
+                       printk(KERN_DEBUG "%s: ieee80211_beacon_get: no rate "
+                              "found\n", dev->name);
+               }
+               dev_kfree_skb(skb);
+               return NULL;
+       }
+
+       control->tx_rate = (local->short_preamble &&
+                           (rate->flags & IEEE80211_RATE_PREAMBLE2)) ?
+               rate->val2 : rate->val;
+       control->antenna_sel = local->conf.antenna_sel;
+       control->power_level = local->conf.power_level;
+       control->no_ack = 1;
+       control->retry_limit = 1;
+       control->rts_cts_duration = 0;
+       control->clear_dst_mask = 1;
+
+
+       norm->num_beacons++;
+       return skb;
+}
+
+struct sk_buff *
+ieee80211_get_buffered_bc(struct net_device *dev, int bss_idx,
+                         struct ieee80211_tx_control *control)
+{
+       struct ieee80211_local *local = dev->priv;
+       struct sk_buff *skb;
+       struct sta_info *sta;
+       ieee80211_tx_handler *handler;
+       struct ieee80211_txrx_data tx;
+       ieee80211_txrx_result res = TXRX_DROP;
+       struct net_device *bdev;
+       struct ieee80211_sub_if_data *sdata;
+       struct ieee80211_if_norm *bss;
+
+
+       spin_lock_bh(&local->sub_if_lock);
+       if (bss_idx < 0 || bss_idx >= local->bss_dev_count) {
+               bdev = NULL;
+               bss = NULL;
+       } else {
+               bdev = local->bss_devs[bss_idx];
+               sdata = IEEE80211_DEV_TO_SUB_IF(bdev);
+               bss = &sdata->u.norm;
+       }
+       spin_unlock_bh(&local->sub_if_lock);
+       if (bdev == NULL || bss == NULL || bss->beacon_head == NULL)
+               return NULL;
+
+       if (bss->dtim_count != 0)
+               return NULL; /* send buffered bc/mc only after DTIM beacon */
+       skb = skb_dequeue(&bss->ps_bc_buf);
+       memset(control, 0, sizeof(*control));
+       if (skb == NULL)
+               return NULL;
+       local->total_ps_buffered--;
+
+       if (!skb_queue_empty(&bss->ps_bc_buf) && skb->len >= 2) {
+               struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
+               /* more buffered multicast/broadcast frames ==> set MoreData
+                * flag in IEEE 802.11 header to inform PS STAs */
+               hdr->frame_control |= cpu_to_le16(WLAN_FC_MOREDATA);
+       }
+
+       ieee80211_tx_prepare(&tx, skb, dev, control);
+       sta = tx.sta;
+       tx.u.tx.ps_buffered = 1;
+
+       for (handler = local->tx_handlers; *handler != NULL; handler++) {
+               res = (*handler)(&tx);
+               if (res == TXRX_DROP || res == TXRX_QUEUED)
+                       break;
+       }
+
+       if (res == TXRX_DROP) {
+               I802_DEBUG_INC(local->tx_handlers_drop);
+               dev_kfree_skb(skb);
+               skb = NULL;
+       } else if (res == TXRX_QUEUED) {
+               I802_DEBUG_INC(local->tx_handlers_queued);
+               skb = NULL;
+       }
+
+       if (sta)
+               sta_info_release(local, sta);
+
+       return skb;
+}
+
+
+int ieee80211_hw_config(struct net_device *dev)
+{
+       struct ieee80211_local *local = dev->priv;
+       int i, ret = 0;
+
+#ifdef CONFIG_IEEE80211_VERBOSE_DEBUG
+       printk(KERN_DEBUG "HW CONFIG: channel=%d freq=%d mode=%d "
+              "phymode=%d\n", local->conf.channel, local->conf.freq,
+              local->conf.mode, local->conf.phymode);
+#endif /* CONFIG_IEEE80211_VERBOSE_DEBUG */
+
+       if (local->hw->config)
+               ret = local->hw->config(dev, &local->conf);
+
+       for (i = 0; i < local->hw->num_modes; i++) {
+               struct ieee80211_hw_modes *mode = &local->hw->modes[i];
+               if (mode->mode == local->conf.phymode) {
+                       if (local->curr_rates != mode->rates) {
+                               rate_control_clear(local);
+                       }
+                       local->curr_rates = mode->rates;
+                       local->num_curr_rates = mode->num_rates;
+                       ieee80211_prepare_rates(dev);
+                       break;
+               }
+       }
+
+       return ret;
+}
+
+
+struct ieee80211_conf *ieee80211_get_hw_conf(struct net_device *dev)
+{
+       struct ieee80211_local *local = dev->priv;
+        return &local->conf;
+}
+
+
+static int ieee80211_change_mtu(struct net_device *dev, int new_mtu)
+{
+       /* FIX: what would be proper limits for MTU?
+        * This interface uses 802.3 frames. */
+       if (new_mtu < 256 || new_mtu > 2304 - 24 - 6) {
+               printk(KERN_WARNING "%s: invalid MTU %d\n",
+                      dev->name, new_mtu);
+               return -EINVAL;
+       }
+
+#ifdef CONFIG_IEEE80211_VERBOSE_DEBUG
+       printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
+#endif /* CONFIG_IEEE80211_VERBOSE_DEBUG */
+       dev->mtu = new_mtu;
+       return 0;
+}
+
+
+static int ieee80211_change_mtu_apdev(struct net_device *dev, int new_mtu)
+{
+       /* FIX: what would be proper limits for MTU?
+        * This interface uses 802.11 frames. */
+       if (new_mtu < 256 || new_mtu > 2304) {
+               printk(KERN_WARNING "%s: invalid MTU %d\n",
+                      dev->name, new_mtu);
+               return -EINVAL;
+       }
+
+#ifdef CONFIG_IEEE80211_VERBOSE_DEBUG
+       printk(KERN_DEBUG "%s: setting MTU %d\n", dev->name, new_mtu);
+#endif /* CONFIG_IEEE80211_VERBOSE_DEBUG */
+       dev->mtu = new_mtu;
+       return 0;
+}
+
+
+static void ieee80211_tx_timeout(struct net_device *dev)
+{
+       struct ieee80211_local *local = dev->priv;
+
+       printk(KERN_WARNING "%s: resetting interface.\n", dev->name);
+
+       if (local->hw->reset(dev))
+               printk(KERN_ERR "%s: failed to reset interface.\n", dev->name);
+       else
+               netif_wake_queue(dev);
+}
+
+
+static int ieee80211_set_mac_address(struct net_device *dev, void *addr)
+{
+       struct ieee80211_local *local = dev->priv;
+       struct sockaddr *a = addr;
+       struct list_head *ptr;
+       int res;
+
+       if (!local->hw->set_mac_address)
+               return -EOPNOTSUPP;
+
+       res = local->hw->set_mac_address(dev, addr);
+       if (res)
+               return res;
+
+       list_for_each(ptr, &local->sub_if_list) {
+               struct ieee80211_sub_if_data *sdata =
+                       list_entry(ptr, struct ieee80211_sub_if_data, list);
+               memcpy(sdata->dev->dev_addr, a->sa_data, ETH_ALEN);
+       }
+
+       return 0;
+}
+
+
+static struct net_device_stats *ieee80211_get_stats(struct net_device *dev)
+{
+       struct ieee80211_sub_if_data *sdata;
+       sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+       return &(sdata->stats);
+}
+
+
+static int ieee80211_open(struct net_device *dev)
+{
+       struct ieee80211_sub_if_data *sdata;
+       struct ieee80211_local *local = dev->priv;
+       int res;
+
+       sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+
+        if (local->open_count == 0) {
+               res = local->hw->open(sdata->master);
+               if (res)
+                       return res;
+               ieee80211_init_scan(sdata->master);
+       }
+        local->open_count++;
+
+       netif_start_queue(dev);
+       return 0;
+}
+
+
+static int ieee80211_stop(struct net_device *dev)
+{
+       struct ieee80211_sub_if_data *sdata;
+       struct ieee80211_local *local = dev->priv;
+       int res;
+
+       sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+
+        netif_stop_queue(dev);
+
+       local->open_count--;
+        if (local->open_count == 0) {
+               ieee80211_stop_scan(sdata->master);
+               res = local->hw->stop(sdata->master);
+               if (res)
+                       return res;
+        }
+
+       return 0;
+}
+
+
+static int header_parse_80211(struct sk_buff *skb, unsigned char *haddr)
+{
+       memcpy(haddr, skb->mac.raw + 10, ETH_ALEN); /* addr2 */
+       return ETH_ALEN;
+}
+
+
+static struct net_device *
+ieee80211_get_wds_dev(struct ieee80211_local *local, u8 *addr)
+{
+       struct list_head *ptr;
+
+       list_for_each(ptr, &local->sub_if_list) {
+               struct ieee80211_sub_if_data *sdata =
+                       list_entry(ptr, struct ieee80211_sub_if_data, list);
+               if (sdata->type == IEEE80211_SUB_IF_TYPE_WDS &&
+                   memcmp(addr, sdata->u.wds.remote_addr, ETH_ALEN) == 0)
+                       return sdata->dev;
+       }
+
+       return NULL;
+}
+
+
+static struct net_device * ieee80211_own_bssid(struct ieee80211_local *local,
+                                              u8 *addr)
+{
+       int i;
+       struct net_device *dev = NULL;
+
+       spin_lock_bh(&local->sub_if_lock);
+       for (i = 0; i < local->bss_dev_count; i++) {
+               if ((memcmp(local->bss_devs[i]->dev_addr, addr, ETH_ALEN) == 0)
+               ) {
+                       dev = local->bss_devs[i];
+                       break;
+               }
+       }
+       spin_unlock_bh(&local->sub_if_lock);
+
+       return dev;
+}
+
+
+
+
+static struct net_device * ieee80211_sta_bssid(struct ieee80211_local *local,
+                                              u8 *addr, u8 *a1,
+                                              int *sta_multicast)
+{
+       struct list_head *ptr;
+       int multicast;
+       u8 *own_addr = local->mdev->dev_addr;
+
+       multicast = a1[0] & 0x01;
+
+       /* Try O(1) lookup for a common case of only one AP being used. */
+       if (own_addr[0] == a1[0] && own_addr[1] == a1[1] &&
+           own_addr[2] == a1[2]) {
+               int index = (((int) a1[3] << 16) | ((int) a1[4] << 8) | a1[5])
+                       - (((int) own_addr[3] << 16) |
+                          ((int) own_addr[4] << 8) | own_addr[5]);
+               if (index >= 0 && index < local->conf.bss_count &&
+                   local->sta_devs[index]) {
+                       struct net_device *dev = local->sta_devs[index];
+                       struct ieee80211_sub_if_data *sdata;
+                       sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+                       if (memcmp(addr, sdata->u.sta.bssid, ETH_ALEN) == 0) {
+                               *sta_multicast = multicast;
+                               return dev;
+                       }
+               }
+       }
+
+       if (!multicast)
+               return NULL;
+
+       /* Could not find station interface, resort to O(n) lookup. */
+       list_for_each(ptr, &local->sub_if_list) {
+               struct ieee80211_sub_if_data *sdata =
+                       list_entry(ptr, struct ieee80211_sub_if_data, list);
+               if (sdata->type != IEEE80211_SUB_IF_TYPE_STA)
+                       continue;
+               if (!multicast &&
+                   memcmp(a1, sdata->dev->dev_addr, ETH_ALEN) != 0)
+                       continue;
+
+               if (memcmp(addr, sdata->u.sta.bssid, ETH_ALEN) == 0 ||
+                   (memcmp(addr, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) == 0 &&
+                    local->conf.mode == IW_MODE_ADHOC)) {
+                       *sta_multicast = multicast;
+                       return sdata->dev;
+               }
+       }
+
+       return NULL;
+}
+
+
+static int ieee80211_own_addr(struct net_device *dev, u8 *addr)
+{
+       struct ieee80211_local *local = dev->priv;
+       u8 *own = dev->dev_addr;
+       int index;
+
+       /* Optimization: assume that BSSID mask does not change for first
+        * three octets. */
+       if (own[0] != addr[0] || own[1] != addr[1] || own[2] != addr[2])
+               return 0;
+
+       index = (((int) addr[3] << 16) | ((int) addr[4] << 8) | addr[5]) -
+               (((int) own[3] << 16) | ((int) own[4] << 8) | own[5]);
+       if (index >= 0 && index < local->conf.bss_count &&
+           local->sta_devs[index])
+               return 1;
+
+       return 0;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_rx_h_data(struct ieee80211_txrx_data *rx)
+{
+       struct net_device *dev = rx->dev;
+       struct ieee80211_local *local = rx->local;
+        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
+       u16 fc, hdrlen, ethertype;
+       u8 *payload;
+       u8 dst[ETH_ALEN];
+       u8 src[ETH_ALEN];
+        struct sk_buff *skb = rx->skb, *skb2;
+        struct ieee80211_sub_if_data *sdata;
+
+       fc = rx->fc;
+       if (unlikely(WLAN_FC_GET_TYPE(fc) != WLAN_FC_TYPE_DATA))
+               return TXRX_CONTINUE;
+
+       if (unlikely(!WLAN_FC_DATA_PRESENT(fc)))
+               return TXRX_DROP;
+
+       hdrlen = ieee80211_get_hdrlen(fc);
+
+       /* convert IEEE 802.11 header + possible LLC headers into Ethernet
+        * header
+        * IEEE 802.11 address fields:
+        * ToDS FromDS Addr1 Addr2 Addr3 Addr4
+        *   0     0   DA    SA    BSSID n/a
+        *   0     1   DA    BSSID SA    n/a
+        *   1     0   BSSID SA    DA    n/a
+        *   1     1   RA    TA    DA    SA
+        */
+
+       switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
+       case WLAN_FC_TODS:
+               /* BSSID SA DA */
+               memcpy(dst, hdr->addr3, ETH_ALEN);
+               memcpy(src, hdr->addr2, ETH_ALEN);
+
+               if (unlikely(local->conf.mode != IW_MODE_MASTER ||
+                            !ieee80211_own_bssid(local, hdr->addr1))) {
+                       printk(KERN_DEBUG "%s: dropped ToDS frame (BSSID="
+                              MACSTR " SA=" MACSTR " DA=" MACSTR ")\n",
+                              dev->name, MAC2STR(hdr->addr1),
+                              MAC2STR(hdr->addr2), MAC2STR(hdr->addr3));
+                       return TXRX_DROP;
+               }
+               break;
+       case (WLAN_FC_TODS | WLAN_FC_FROMDS):
+               /* RA TA DA SA */
+               memcpy(dst, hdr->addr3, ETH_ALEN);
+               memcpy(src, hdr->addr4, ETH_ALEN);
+
+               dev = ieee80211_get_wds_dev(local, hdr->addr2);
+               if (!dev || memcmp(hdr->addr1, dev->dev_addr, ETH_ALEN) != 0) {
+                       printk(KERN_DEBUG "%s: dropped FromDS&ToDS frame (RA="
+                              MACSTR " TA=" MACSTR " DA=" MACSTR " SA="
+                              MACSTR ")\n",
+                              rx->dev->name, MAC2STR(hdr->addr1),
+                              MAC2STR(hdr->addr2), MAC2STR(hdr->addr3),
+                              MAC2STR(hdr->addr4));
+                       return TXRX_DROP;
+               }
+               break;
+       case WLAN_FC_FROMDS:
+               /* DA BSSID SA */
+               memcpy(dst, hdr->addr1, ETH_ALEN);
+               memcpy(src, hdr->addr3, ETH_ALEN);
+
+               sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+               if (sdata->type != IEEE80211_SUB_IF_TYPE_STA ||
+                   memcmp(hdr->addr3, dev->dev_addr, ETH_ALEN) == 0 ||
+                   memcmp(hdr->addr2, sdata->u.sta.bssid, ETH_ALEN) != 0) {
+                       return TXRX_DROP;
+               }
+               break;
+       case 0:
+               /* DA SA BSSID */
+               memcpy(dst, hdr->addr1, ETH_ALEN);
+               memcpy(src, hdr->addr2, ETH_ALEN);
+
+               if (local->conf.mode != IW_MODE_ADHOC ||
+                   memcmp(hdr->addr3, local->bssid, ETH_ALEN) != 0) {
+                       if (net_ratelimit()) {
+                               printk(KERN_DEBUG "%s: dropped IBSS frame (DA="
+                                      MACSTR " SA=" MACSTR " BSSID=" MACSTR
+                                      ")\n",
+                                      dev->name, MAC2STR(hdr->addr1),
+                                      MAC2STR(hdr->addr2),
+                                      MAC2STR(hdr->addr3));
+                       }
+                       return TXRX_DROP;
+               }
+               break;
+       }
+
+       payload = skb->data + hdrlen;
+
+       if (unlikely(skb->len - hdrlen < 8)) {
+               if (net_ratelimit()) {
+                       printk(KERN_DEBUG "%s: RX too short data frame "
+                              "payload\n", dev->name);
+               }
+               return TXRX_DROP;
+       }
+
+       ethertype = (payload[6] << 8) | payload[7];
+
+       if (likely((memcmp(payload, rfc1042_header, 6) == 0 &&
+                   ethertype != ETH_P_AARP && ethertype != ETH_P_IPX) ||
+                  memcmp(payload, bridge_tunnel_header, 6) == 0)) {
+               /* remove RFC1042 or Bridge-Tunnel encapsulation and
+                * replace EtherType */
+               skb_pull(skb, hdrlen + 6);
+               memcpy(skb_push(skb, ETH_ALEN), src, ETH_ALEN);
+               memcpy(skb_push(skb, ETH_ALEN), dst, ETH_ALEN);
+       } else {
+               struct ethhdr *ehdr;
+                unsigned short len;
+               skb_pull(skb, hdrlen);
+                len = htons(skb->len);
+               ehdr = (struct ethhdr *)skb_push(skb, sizeof(struct ethhdr));
+               memcpy(ehdr->h_dest, dst, ETH_ALEN);
+               memcpy(ehdr->h_source, src, ETH_ALEN);
+                ehdr->h_proto = len;
+       }
+
+        if (rx->sta && !rx->sta->assoc_ap &&
+           !(rx->sta && (rx->sta->flags & WLAN_STA_WDS)))
+                skb->dev = rx->sta->dev;
+        else
+                skb->dev = dev;
+
+        skb2 = NULL;
+        sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+
+        /*
+         * don't count the master since the low level code
+         * counts it already for us.
+         */
+        if (skb->dev != sdata->master) {
+               sdata->stats.rx_packets++;
+               sdata->stats.rx_bytes += skb->len;
+        }
+
+       if (local->bridge_packets && sdata->type != IEEE80211_SUB_IF_TYPE_WDS
+           && sdata->type != IEEE80211_SUB_IF_TYPE_STA) {
+               if (MULTICAST_ADDR(skb->data)) {
+                       /* send multicast frames both to higher layers in
+                        * local net stack and back to the wireless media */
+                       skb2 = skb_copy(skb, GFP_ATOMIC);
+                       if (skb2 == NULL)
+                               printk(KERN_DEBUG "%s: failed to clone "
+                                      "multicast frame\n", dev->name);
+               } else {
+                       struct sta_info *dsta;
+                        dsta = sta_info_get(local, skb->data);
+                        if (dsta && dsta->dev == NULL) {
+                                printk(KERN_DEBUG "Station with null dev "
+                                      "structure!\n");
+                        } else if (dsta && dsta->dev == dev) {
+                               /* Destination station is associated to this
+                                * AP, so send the frame directly to it and
+                                * do not pass the frame to local net stack.
+                                 */
+                               skb2 = skb;
+                               skb = NULL;
+                       }
+                       if (dsta)
+                               sta_info_release(local, dsta);
+               }
+       }
+
+       if (skb) {
+               /* deliver to local stack */
+               skb->protocol = eth_type_trans(skb, dev);
+               memset(skb->cb, 0, sizeof(skb->cb));
+               netif_rx(skb);
+       }
+
+       if (skb2) {
+               /* send to wireless media */
+               skb2->protocol = __constant_htons(ETH_P_802_3);
+               skb2->mac.raw = skb2->nh.raw = skb2->data;
+               dev_queue_xmit(skb2);
+        }
+
+       return TXRX_QUEUED;
+}
+
+
+static struct ieee80211_rate *
+ieee80211_get_rate(struct ieee80211_local *local, int phymode, int hw_rate)
+{
+       int m, r;
+
+       for (m = 0; m < local->hw->num_modes; m++) {
+               struct ieee80211_hw_modes *mode = &local->hw->modes[m];
+               if (mode->mode != phymode)
+                       continue;
+               for (r = 0; r < mode->num_rates; r++) {
+                       struct ieee80211_rate *rate = &mode->rates[r];
+                       if (rate->val == hw_rate ||
+                           (rate->flags & IEEE80211_RATE_PREAMBLE2 &&
+                            rate->val2 == hw_rate))
+                               return rate;
+               }
+       }
+
+       return NULL;
+}
+
+
+void
+ieee80211_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
+                 struct ieee80211_rx_status *status, u32 msg_type)
+{
+        struct ieee80211_local *local = dev->priv;
+        struct ieee80211_frame_info *fi;
+        size_t hlen;
+        struct ieee80211_sub_if_data *sdata;
+
+        dev = local->apdev;
+        skb->dev = dev;
+
+        sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+
+       if (skb_headroom(skb) < sizeof(struct ieee80211_frame_info)) {
+               I802_DEBUG_INC(local->rx_expand_skb_head);
+               if (pskb_expand_head(skb, sizeof(struct ieee80211_frame_info),
+                                    0, GFP_ATOMIC)) {
+                       dev_kfree_skb(skb);
+                        return;
+               }
+       }
+
+       hlen = sizeof(struct ieee80211_frame_info);
+       if (msg_type == ieee80211_msg_monitor)
+               hlen -= sizeof(fi->msg_type);
+
+       fi = (struct ieee80211_frame_info *) skb_push(skb, hlen);
+        memset(fi, 0, hlen);
+       if (msg_type != ieee80211_msg_monitor)
+               fi->msg_type = htonl(msg_type);
+       fi->version = htonl(IEEE80211_FI_VERSION);
+        fi->length = htonl(hlen);
+        if (status) {
+//                struct timespec ts;
+               struct ieee80211_rate *rate;
+
+#if 0
+                jiffies_to_timespec(status->hosttime, &ts);
+               fi->hosttime = cpu_to_be64(ts.tv_sec * 1000000 +
+                                          ts.tv_nsec / 1000);
+               fi->mactime = cpu_to_be64(status->mactime);
+#endif
+               switch (status->phymode) {
+                case MODE_IEEE80211A:
+                        fi->phytype = htonl(ieee80211_phytype_ofdm_dot11_a);
+                        break;
+                case MODE_IEEE80211B:
+                        fi->phytype = htonl(ieee80211_phytype_dsss_dot11_b);
+                        break;
+                case MODE_IEEE80211G:
+                        fi->phytype = htonl(ieee80211_phytype_pbcc_dot11_g);
+                        break;
+                case MODE_ATHEROS_TURBO:
+                       fi->phytype =
+                               htonl(ieee80211_phytype_dsss_dot11_turbo);
+                        break;
+                default:
+                        fi->phytype = 0xAAAAAAAA;
+                       break;
+                }
+                fi->channel = htonl(status->channel);
+               rate = ieee80211_get_rate(local, status->phymode,
+                                         status->rate);
+               if (rate) {
+                       fi->datarate = htonl(rate->rate);
+                       if (rate->flags & IEEE80211_RATE_PREAMBLE2) {
+                               if (status->rate == rate->val)
+                                       fi->preamble = htonl(2); /* long */
+                               else if (status->rate == rate->val2)
+                                       fi->preamble = htonl(1); /* short */
+                       } else
+                               fi->preamble = htonl(0);
+               } else {
+                       fi->datarate = htonl(0);
+                       fi->preamble = htonl(0);
+               }
+
+                fi->antenna = htonl(status->antenna);
+                fi->priority = 0xffffffff; /* no clue */
+                fi->ssi_type = htonl(ieee80211_ssi_raw);
+                fi->ssi_signal = htonl(status->ssi);
+                fi->ssi_noise = 0x00000000;
+                fi->encoding = 0;
+       } else {
+                fi->ssi_type = htonl(ieee80211_ssi_none);
+        }
+
+        sdata->stats.rx_packets++;
+        sdata->stats.rx_bytes += skb->len;
+
+        skb->mac.raw = skb->data;
+       skb->ip_summed = CHECKSUM_UNNECESSARY;
+       skb->pkt_type = PACKET_OTHERHOST;
+       skb->protocol = __constant_htons(ETH_P_802_2);
+       memset(skb->cb, 0, sizeof(skb->cb));
+        netif_rx(skb);
+}
+
+
+int ieee80211_radar_status(struct net_device *dev, int channel, int radar,
+                          int radar_type)
+{
+       struct sk_buff *skb;
+       struct ieee80211_radar_info *msg;
+
+       skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
+                           sizeof(struct ieee80211_radar_info));
+
+       if (skb == NULL)
+               return -ENOMEM;
+       skb_reserve(skb, sizeof(struct ieee80211_frame_info));
+
+       msg = (struct ieee80211_radar_info *)
+               skb_put(skb, sizeof(struct ieee80211_radar_info));
+       msg->channel = channel;
+       msg->radar = radar;
+       msg->radar_type = radar_type;
+
+       ieee80211_rx_mgmt(dev, skb, 0, ieee80211_msg_radar);
+       return 0;
+}
+
+
+int ieee80211_set_aid_for_sta(struct net_device *dev, u8 *peer_address,
+                             u16 aid)
+{
+        struct sk_buff *skb;
+        struct ieee80211_msg_set_aid_for_sta *msg;
+
+       skb = dev_alloc_skb(sizeof(struct ieee80211_frame_info) +
+                           sizeof(struct ieee80211_msg_set_aid_for_sta));
+
+        if (skb == NULL)
+               return -ENOMEM;
+       skb_reserve(skb, sizeof(struct ieee80211_frame_info));
+
+        msg = (struct ieee80211_msg_set_aid_for_sta *)
+               skb_put(skb, sizeof(struct ieee80211_msg_set_aid_for_sta));
+       memcpy(msg->sta_address, peer_address, ETH_ALEN);
+        msg->aid = aid;
+
+        ieee80211_rx_mgmt(dev, skb, 0, ieee80211_msg_set_aid_for_sta);
+        return 0;
+}
+
+
+static void ap_sta_ps_start(struct net_device *dev, struct sta_info *sta)
+{
+       struct ieee80211_sub_if_data *sdata;
+       sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
+
+       if (sdata->bss)
+               atomic_inc(&sdata->bss->num_sta_ps);
+       sta->flags |= WLAN_STA_PS;
+       sta->pspoll = 0;
+#ifdef IEEE80211_VERBOSE_DEBUG_PS
+       printk(KERN_DEBUG "%s: STA " MACSTR " aid %d enters power "
+              "save mode\n", dev->name, MAC2STR(sta->addr), sta->aid);
+#endif /* IEEE80211_VERBOSE_DEBUG_PS */
+}
+
+
+static int ap_sta_ps_end(struct net_device *dev, struct sta_info *sta)
+{
+       struct ieee80211_local *local = dev->priv;
+       struct sk_buff *skb;
+       int sent = 0;
+       struct ieee80211_sub_if_data *sdata;
+       struct ieee80211_tx_packet_data *pkt_data;
+
+       sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
+       if (sdata->bss)
+               atomic_dec(&sdata->bss->num_sta_ps);
+       sta->flags &= ~(WLAN_STA_PS | WLAN_STA_TIM);
+       sta->pspoll = 0;
+       if (!skb_queue_empty(&sta->ps_tx_buf) && local->hw->set_tim)
+               local->hw->set_tim(dev, sta->aid, 0);
+#ifdef IEEE80211_VERBOSE_DEBUG_PS
+       printk(KERN_DEBUG "%s: STA " MACSTR " aid %d exits power "
+              "save mode\n", dev->name, MAC2STR(sta->addr), sta->aid);
+#endif /* IEEE80211_VERBOSE_DEBUG_PS */
+       /* Send all buffered frames to the station */
+       while ((skb = skb_dequeue(&sta->tx_filtered)) != NULL) {
+               pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
+               sent++;
+               pkt_data->control.requeue = 1;
+               dev_queue_xmit(skb);
+       }
+       while ((skb = skb_dequeue(&sta->ps_tx_buf)) != NULL) {
+               pkt_data = (struct ieee80211_tx_packet_data *) skb->cb;
+               local->total_ps_buffered--;
+               sent++;
+#ifdef IEEE80211_VERBOSE_DEBUG_PS
+               printk(KERN_DEBUG "%s: STA " MACSTR " aid %d send PS frame "
+                      "since STA not sleeping anymore\n", dev->name,
+                      MAC2STR(sta->addr), sta->aid);
+#endif /* IEEE80211_VERBOSE_DEBUG_PS */
+               pkt_data->control.requeue = 1;
+               dev_queue_xmit(skb);
+       }
+
+       return sent;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_rx_h_ieee80211_rx_h_ps_poll(struct ieee80211_txrx_data *rx)
+{
+       struct sk_buff *skb;
+       int no_pending_pkts;
+
+       if (likely(!rx->sta || WLAN_FC_GET_TYPE(rx->fc) != WLAN_FC_TYPE_CTRL ||
+                  WLAN_FC_GET_STYPE(rx->fc) != WLAN_FC_STYPE_PSPOLL))
+               return TXRX_CONTINUE;
+
+       skb = skb_dequeue(&rx->sta->tx_filtered);
+       if (skb == NULL) {
+               skb = skb_dequeue(&rx->sta->ps_tx_buf);
+               if (skb)
+                       rx->local->total_ps_buffered--;
+       }
+       no_pending_pkts = skb_queue_empty(&rx->sta->tx_filtered) &&
+               skb_queue_empty(&rx->sta->ps_tx_buf);
+
+       if (skb) {
+               struct ieee80211_hdr *hdr =
+                       (struct ieee80211_hdr *) skb->data;
+
+               /* tell TX path to send one frame even though the STA may
+                * still remain is PS mode after this frame exchange */
+               rx->sta->pspoll = 1;
+
+#ifdef IEEE80211_VERBOSE_DEBUG_PS
+               printk(KERN_DEBUG "STA " MACSTR " aid %d: PS Poll (entries "
+                      "after %d)\n",
+                      MAC2STR(rx->sta->addr), rx->sta->aid,
+                      skb_queue_len(&rx->sta->ps_tx_buf));
+#endif /* IEEE80211_VERBOSE_DEBUG_PS */
+
+               /* Use MoreData flag to indicate whether there are more
+                * buffered frames for this STA */
+               if (no_pending_pkts) {
+                       hdr->frame_control &= cpu_to_le16(~WLAN_FC_MOREDATA);
+                       rx->sta->flags &= ~WLAN_STA_TIM;
+               } else
+                       hdr->frame_control |= cpu_to_le16(WLAN_FC_MOREDATA);
+
+               dev_queue_xmit(skb);
+
+               if (no_pending_pkts && rx->local->hw->set_tim)
+                       rx->local->hw->set_tim(rx->dev, rx->sta->aid, 0);
+#ifdef IEEE80211_VERBOSE_DEBUG_PS
+       } else if (!rx->u.rx.sent_ps_buffered) {
+               printk(KERN_DEBUG "%s: STA " MACSTR " sent PS Poll even "
+                      "though there is no buffered frames for it\n",
+                      rx->dev->name, MAC2STR(rx->sta->addr));
+#endif /* IEEE80211_VERBOSE_DEBUG_PS */
+
+       }
+
+       /* Free PS Poll skb here instead of returning TXRX_DROP that would
+        * count as an dropped frame. */
+        dev_kfree_skb(rx->skb);
+
+       return TXRX_QUEUED;
+}
+
+
+static inline struct ieee80211_fragment_entry *
+ieee80211_reassemble_add(struct ieee80211_local *local,
+                        unsigned int frag, unsigned int seq, int rx_queue,
+                        struct sk_buff **skb)
+{
+        struct ieee80211_fragment_entry *entry;
+       int idx;
+
+       idx = local->fragment_next;
+       entry = &local->fragments[local->fragment_next++];
+       if (local->fragment_next >= IEEE80211_FRAGMENT_MAX)
+               local->fragment_next = 0;
+
+       if (entry->skb) {
+#ifdef CONFIG_IEEE80211_DEBUG
+               struct ieee80211_hdr *hdr =
+                       (struct ieee80211_hdr *) entry->skb->data;
+               printk(KERN_DEBUG "%s: RX reassembly removed oldest "
+                      "fragment entry (idx=%d age=%lu seq=%d last_frag=%d "
+                      "addr1=" MACSTR " addr2=" MACSTR "\n",
+                      local->mdev->name, idx,
+                      jiffies - entry->first_frag_time, entry->seq,
+                      entry->last_frag, MAC2STR(hdr->addr1),
+                      MAC2STR(hdr->addr2));
+#endif /* CONFIG_IEEE80211_DEBUG */
+               dev_kfree_skb(entry->skb);
+       }
+
+       entry->skb = *skb;
+       *skb = NULL;
+       entry->first_frag_time = jiffies;
+       entry->seq = seq;
+       entry->rx_queue = rx_queue;
+        entry->last_frag = frag;
+       entry->ccmp = 0;
+
+       return entry;
+}
+
+
+static inline struct ieee80211_fragment_entry *
+ieee80211_reassemble_find(struct ieee80211_local *local,
+                         u16 fc, unsigned int frag, unsigned int seq,
+                         int rx_queue, struct ieee80211_hdr *hdr)
+{
+       struct ieee80211_fragment_entry *entry;
+        int i, idx;
+
+       idx = local->fragment_next;
+       for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++) {
+               struct ieee80211_hdr *f_hdr;
+               u16 f_fc;
+
+               idx--;
+               if (idx < 0)
+                       idx = IEEE80211_FRAGMENT_MAX - 1;
+
+               entry = &local->fragments[idx];
+               if (!entry->skb || entry->seq != seq ||
+                   entry->rx_queue != rx_queue ||
+                   entry->last_frag + 1 != frag)
+                       continue;
+
+               f_hdr = (struct ieee80211_hdr *) entry->skb->data;
+               f_fc = le16_to_cpu(f_hdr->frame_control);
+
+               if (WLAN_FC_GET_TYPE(fc) != WLAN_FC_GET_TYPE(f_fc) ||
+                   memcmp(hdr->addr1, f_hdr->addr1, ETH_ALEN) != 0 ||
+                   memcmp(hdr->addr2, f_hdr->addr2, ETH_ALEN) != 0)
+                       continue;
+
+               if (entry->first_frag_time + 2 * HZ < jiffies) {
+                       dev_kfree_skb(entry->skb);
+                       entry->skb = NULL;
+                       continue;
+               }
+               return entry;
+        }
+
+       return NULL;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_rx_h_defragment(struct ieee80211_txrx_data *rx)
+{
+       struct ieee80211_hdr *hdr;
+       u16 sc;
+       unsigned int frag, seq;
+       struct ieee80211_fragment_entry *entry;
+
+       hdr = (struct ieee80211_hdr *) rx->skb->data;
+       sc = le16_to_cpu(hdr->seq_ctrl);
+       frag = WLAN_GET_SEQ_FRAG(sc);
+
+       if (likely((!(rx->fc & WLAN_FC_MOREFRAG) && frag == 0) ||
+                  (rx->skb)->len < 24 || MULTICAST_ADDR(hdr->addr1))) {
+               /* not fragmented */
+               goto out;
+       }
+       I802_DEBUG_INC(rx->local->rx_handlers_fragments);
+
+       seq = WLAN_GET_SEQ_SEQ(sc);
+
+       if (frag == 0) {
+               /* This is the first fragment of a new frame. */
+               entry = ieee80211_reassemble_add(rx->local, frag, seq,
+                                                rx->u.rx.queue, &(rx->skb));
+               if (rx->key && rx->key->alg == ALG_CCMP &&
+                   (rx->fc & WLAN_FC_ISWEP)) {
+                       /* Store CCMP PN so that we can verify that the next
+                        * fragment has a sequential PN value. */
+                       entry->ccmp = 1;
+                       memcpy(entry->last_pn,
+                              rx->key->u.ccmp.rx_pn[rx->u.rx.queue],
+                              CCMP_PN_LEN);
+               }
+               return TXRX_QUEUED;
+       }
+
+       /* This is a fragment for a frame that should already be pending in
+        * fragment cache. Add this fragment to the end of the pending entry.
+        */
+       entry = ieee80211_reassemble_find(rx->local, rx->fc, frag, seq,
+                                         rx->u.rx.queue, hdr);
+       if (!entry) {
+               I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
+               return TXRX_DROP;
+       }
+
+       /* Verify that MPDUs within one MSDU have sequential PN values.
+        * (IEEE 802.11i, 8.3.3.4.5) */
+       if (entry->ccmp) {
+               int i;
+               u8 pn[CCMP_PN_LEN], *rpn;
+               if (rx->key == NULL || rx->key->alg != ALG_CCMP)
+                       return TXRX_DROP;
+               memcpy(pn, entry->last_pn, CCMP_PN_LEN);
+               for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
+                       pn[i]++;
+                       if (pn[i])
+                               break;
+               }
+               rpn = rx->key->u.ccmp.rx_pn[rx->u.rx.queue];
+               if (memcmp(pn, rpn, CCMP_PN_LEN) != 0) {
+                       printk(KERN_DEBUG "%s: defrag: CCMP PN not sequential"
+                              " A2=" MACSTR " PN=%02x%02x%02x%02x%02x%02x "
+                              "(expected %02x%02x%02x%02x%02x%02x)\n",
+                              rx->dev->name, MAC2STR(hdr->addr2),
+                              rpn[0], rpn[1], rpn[2], rpn[3], rpn[4], rpn[5],
+                              pn[0], pn[1], pn[2], pn[3], pn[4], pn[5]);
+                       return TXRX_DROP;
+               }
+               memcpy(entry->last_pn, pn, CCMP_PN_LEN);
+       }
+
+       /* TODO: could gather list of skb's and reallocate data buffer only
+        * after finding out the total length of the frame */
+       skb_pull(rx->skb, ieee80211_get_hdrlen(rx->fc));
+       if (skb_tailroom(entry->skb) < rx->skb->len) {
+               I802_DEBUG_INC(rx->local->rx_expand_skb_head2);
+               if (unlikely(pskb_expand_head(entry->skb, 0, rx->skb->len,
+                                             GFP_ATOMIC))) {
+                       I802_DEBUG_INC(rx->local->rx_handlers_drop_defrag);
+                       return TXRX_DROP;
+               }
+       }
+       memcpy(skb_put(entry->skb, rx->skb->len), rx->skb->data, rx->skb->len);
+       entry->last_frag = frag;
+       dev_kfree_skb(rx->skb);
+
+       if (rx->fc & WLAN_FC_MOREFRAG) {
+                rx->skb = NULL;
+               return TXRX_QUEUED;
+       }
+
+       /* Complete frame has been reassembled - process it now */
+       rx->skb = entry->skb;
+       rx->fragmented = 1;
+        entry->skb = NULL;
+
+ out:
+       if (rx->sta)
+               rx->sta->rx_packets++;
+       if (MULTICAST_ADDR(hdr->addr1))
+               rx->local->dot11MulticastReceivedFrameCount++;
+#ifdef IEEE80211_LEDS
+        else
+               ieee80211_rx_led(2, rx->dev);
+#endif /* IEEE80211_LEDS */
+       return TXRX_CONTINUE;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_rx_h_monitor(struct ieee80211_txrx_data *rx)
+{
+       if (rx->local->conf.mode == IW_MODE_MONITOR) {
+               ieee80211_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status,
+                                  ieee80211_msg_monitor);
+               return TXRX_QUEUED;
+       }
+
+       return TXRX_CONTINUE;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_rx_h_check(struct ieee80211_txrx_data *rx)
+{
+       struct ieee80211_hdr *hdr;
+       int always_sta_key;
+       hdr = (struct ieee80211_hdr *) rx->skb->data;
+
+       /* Drop duplicate 802.11 retransmissions (IEEE 802.11 Chap. 9.2.9) */
+       if (rx->sta && !MULTICAST_ADDR(hdr->addr1)) {
+               if (unlikely(rx->fc & WLAN_FC_RETRY &&
+                            rx->sta->last_seq_ctrl[rx->u.rx.queue] ==
+                            hdr->seq_ctrl)) {
+                       rx->local->dot11FrameDuplicateCount++;
+                       rx->sta->num_duplicates++;
+                       return TXRX_DROP;
+               } else
+                       rx->sta->last_seq_ctrl[rx->u.rx.queue] = hdr->seq_ctrl;
+       }
+
+       if (rx->local->hw->rx_includes_fcs && rx->skb->len > FCS_LEN)
+               skb_trim(rx->skb, rx->skb->len - FCS_LEN);
+
+       if (unlikely(rx->skb->len < 16)) {
+               I802_DEBUG_INC(rx->local->rx_handlers_drop_short);
+               return TXRX_DROP;
+       }
+
+       /* Filter out foreign unicast packets when in promiscuous mode.
+        * FIX: Filter out multicast to foreign BSSID. */
+       if (rx->local->conf.mode == IW_MODE_INFRA &&
+           !MULTICAST_ADDR(hdr->addr1) &&
+           !ieee80211_own_addr(rx->dev, hdr->addr1))
+               return TXRX_DROP;
+
+       /* Drop disallowed frame classes based on STA auth/assoc state;
+        * IEEE 802.11, Chap 5.5.
+        *
+        * 80211.o does filtering only based on association state, i.e., it
+        * drops Class 3 frames from not associated stations. hostapd sends
+        * deauth/disassoc frames when needed. In addition, hostapd is
+        * responsible for filtering on both auth and assoc states.
+        */
+       if (unlikely((WLAN_FC_GET_TYPE(rx->fc) == WLAN_FC_TYPE_DATA ||
+                     (WLAN_FC_GET_TYPE(rx->fc) == WLAN_FC_TYPE_CTRL &&
+                      WLAN_FC_GET_STYPE(rx->fc) == WLAN_FC_STYPE_PSPOLL)) &&
+                    rx->local->conf.mode != IW_MODE_ADHOC &&
+                    (!rx->sta || !(rx->sta->flags & WLAN_STA_ASSOC)))) {
+               if (!(rx->fc & WLAN_FC_FROMDS) && !(rx->fc & WLAN_FC_TODS)) {
+                       /* Drop IBSS frames silently. */
+                       return TXRX_DROP;
+               }
+
+               ieee80211_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status,
+                                 ieee80211_msg_sta_not_assoc);
+               return TXRX_QUEUED;
+       }
+
+       if (rx->local->conf.mode == IW_MODE_INFRA)
+               always_sta_key = 0;
+       else
+               always_sta_key = 1;
+
+       if (rx->sta && rx->sta->key && always_sta_key) {
+               rx->key = rx->sta->key;
+        } else {
+               if (!rx->sdata) {
+                       printk(KERN_DEBUG "%s: sdata was null in packet!!\n",
+                              rx->dev->name);
+                       printk(KERN_DEBUG "%s: Addr1: " MACSTR "\n",
+                              rx->dev->name, MAC2STR(hdr->addr1));
+                       printk(KERN_DEBUG "%s: Addr2: " MACSTR "\n",
+                              rx->dev->name, MAC2STR(hdr->addr2));
+                       printk(KERN_DEBUG "%s: Addr3: " MACSTR "\n",
+                              rx->dev->name, MAC2STR(hdr->addr3));
+                       return TXRX_DROP;
+               }
+               if (rx->sta && rx->sta->key)
+                       rx->key = rx->sta->key;
+               else
+                       rx->key = rx->sdata->default_key;
+
+               if (rx->local->hw->wep_include_iv &&
+                   rx->fc & WLAN_FC_ISWEP) {
+                       int keyidx = ieee80211_wep_get_keyidx(rx->skb);
+
+                       if (keyidx >= 0 && keyidx < NUM_DEFAULT_KEYS &&
+                           (rx->sta == NULL || rx->sta->key == NULL ||
+                            keyidx > 0)) {
+                               rx->key = rx->sdata->keys[keyidx];
+                       }
+                       if (!rx->key) {
+                               printk(KERN_DEBUG "%s: RX WEP frame with "
+                                      "unknown keyidx %d (A1=" MACSTR " A2="
+                                      MACSTR " A3=" MACSTR ")\n",
+                                      rx->dev->name, keyidx,
+                                      MAC2STR(hdr->addr1),
+                                      MAC2STR(hdr->addr2),
+                                      MAC2STR(hdr->addr3));
+                               ieee80211_rx_mgmt(
+                                       rx->dev, rx->skb, rx->u.rx.status,
+                                       ieee80211_msg_wep_frame_unknown_key);
+                               return TXRX_QUEUED;
+                       }
+               }
+        }
+
+       if (rx->fc & WLAN_FC_ISWEP && rx->key) {
+               rx->key->tx_rx_count++;
+               if (unlikely(rx->local->key_tx_rx_threshold &&
+                            rx->key->tx_rx_count >
+                            rx->local->key_tx_rx_threshold)) {
+                       ieee80211_key_threshold_notify(rx->dev, rx->key,
+                                                      rx->sta);
+               }
+       }
+
+       return TXRX_CONTINUE;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_rx_h_sta_process(struct ieee80211_txrx_data *rx)
+{
+       struct sta_info *sta = rx->sta;
+       struct net_device *dev = rx->dev;
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
+
+       if (!sta)
+               return TXRX_CONTINUE;
+
+       /* Update last_rx only for IBSS packets which are for the current
+        * BSSID to avoid keeping the current IBSS network alive in cases where
+        * other STAs are using different BSSID. */
+       if (rx->local->conf.mode == IW_MODE_ADHOC) {
+               u8 *bssid = ieee80211_get_bssid(hdr, rx->skb->len);
+               if (memcmp(bssid, rx->local->bssid, ETH_ALEN) == 0)
+                       sta->last_rx = jiffies;
+       } else
+       if (!MULTICAST_ADDR(hdr->addr1) ||
+           rx->local->conf.mode == IW_MODE_INFRA) {
+               /* Update last_rx only for unicast frames in order to prevent
+                * the Probe Request frames (the only broadcast frames from a
+                * STA in infrastructure mode) from keeping a connection alive.
+                */
+               sta->last_rx = jiffies;
+       }
+       sta->rx_fragments++;
+       sta->rx_bytes += rx->skb->len;
+       sta->last_rssi = rx->u.rx.status->ssi;
+
+       if (!(rx->fc & WLAN_FC_MOREFRAG)) {
+               /* Change STA power saving mode only in the end of a frame
+                * exchange sequence */
+               if ((sta->flags & WLAN_STA_PS) && !(rx->fc & WLAN_FC_PWRMGT))
+                       rx->u.rx.sent_ps_buffered += ap_sta_ps_end(dev, sta);
+               else if (!(sta->flags & WLAN_STA_PS) &&
+                        (rx->fc & WLAN_FC_PWRMGT))
+                       ap_sta_ps_start(dev, sta);
+       }
+
+       /* Drop data::nullfunc frames silently, since they are used only to
+        * control station power saving mode. */
+       if (WLAN_FC_GET_TYPE(rx->fc) == WLAN_FC_TYPE_DATA &&
+           WLAN_FC_GET_STYPE(rx->fc) == WLAN_FC_STYPE_NULLFUNC) {
+               I802_DEBUG_INC(rx->local->rx_handlers_drop_nullfunc);
+               /* Update counter and free packet here to avoid counting this
+                * as a dropped packed. */
+               sta->rx_packets++;
+               dev_kfree_skb(rx->skb);
+               return TXRX_QUEUED;
+       }
+
+       return TXRX_CONTINUE;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_rx_h_wep_weak_iv_detection(struct ieee80211_txrx_data *rx)
+{
+       if (!rx->sta || !(rx->fc & WLAN_FC_ISWEP) ||
+           WLAN_FC_GET_TYPE(rx->fc) != WLAN_FC_TYPE_DATA || !rx->key ||
+           rx->key->alg != ALG_WEP)
+               return TXRX_CONTINUE;
+
+       /* Check for weak IVs, if hwaccel did not remove IV from the frame */
+       if (rx->local->hw->wep_include_iv ||
+           rx->key->force_sw_encrypt || rx->local->conf.sw_decrypt) {
+               u8 *iv = ieee80211_wep_is_weak_iv(rx->skb, rx->key);
+               if (iv) {
+                       rx->sta->wep_weak_iv_count++;
+               }
+       }
+
+       return TXRX_CONTINUE;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_rx_h_wep_decrypt(struct ieee80211_txrx_data *rx)
+{
+       /* If the device handles decryption totally, skip this test */
+       if (rx->local->hw->device_hides_wep)
+               return TXRX_CONTINUE;
+
+       if ((rx->key && rx->key->alg != ALG_WEP) ||
+           !(rx->fc & WLAN_FC_ISWEP) ||
+           (WLAN_FC_GET_TYPE(rx->fc) != WLAN_FC_TYPE_DATA &&
+            (WLAN_FC_GET_TYPE(rx->fc) != WLAN_FC_TYPE_MGMT ||
+             WLAN_FC_GET_STYPE(rx->fc) != WLAN_FC_STYPE_AUTH)))
+               return TXRX_CONTINUE;
+
+       if (!rx->key) {
+               printk(KERN_DEBUG "%s: RX WEP frame, but no key set\n",
+                      rx->dev->name);
+               return TXRX_DROP;
+       }
+
+       if (!(rx->u.rx.status->flag & RX_FLAG_DECRYPTED) ||
+           rx->key->force_sw_encrypt || rx->local->conf.sw_decrypt) {
+               if (ieee80211_wep_decrypt(rx->local, rx->skb, rx->key)) {
+                       printk(KERN_DEBUG "%s: RX WEP frame, decrypt "
+                              "failed\n", rx->dev->name);
+                       return TXRX_DROP;
+               }
+       } else if (rx->local->hw->wep_include_iv) {
+               ieee80211_wep_remove_iv(rx->local, rx->skb, rx->key);
+               /* remove ICV */
+               skb_trim(rx->skb, rx->skb->len - 4);
+       }
+
+       return TXRX_CONTINUE;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_rx_h_802_1x_pae(struct ieee80211_txrx_data *rx)
+{
+       if (rx->sdata->eapol && ieee80211_is_eapol(rx->skb) &&
+           rx->local->conf.mode != IW_MODE_INFRA) {
+               /* Pass both encrypted and unencrypted EAPOL frames to user
+                * space for processing. */
+               ieee80211_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status,
+                                  ieee80211_msg_normal);
+               return TXRX_QUEUED;
+       }
+
+       if (unlikely(rx->sdata->ieee802_1x &&
+                    WLAN_FC_GET_TYPE(rx->fc) == WLAN_FC_TYPE_DATA &&
+                    WLAN_FC_GET_STYPE(rx->fc) != WLAN_FC_STYPE_NULLFUNC &&
+                    (!rx->sta || !(rx->sta->flags & WLAN_STA_AUTHORIZED)) &&
+                    !ieee80211_is_eapol(rx->skb))) {
+#ifdef CONFIG_IEEE80211_DEBUG
+               struct ieee80211_hdr *hdr =
+                       (struct ieee80211_hdr *) rx->skb->data;
+               printk(KERN_DEBUG "%s: dropped frame from " MACSTR
+                      " (unauthorized port)\n", rx->dev->name,
+                      MAC2STR(hdr->addr2));
+#endif /* CONFIG_IEEE80211_DEBUG */
+               return TXRX_DROP;
+       }
+
+       return TXRX_CONTINUE;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_rx_h_drop_unencrypted(struct ieee80211_txrx_data *rx)
+{
+       /*  If the device handles decryption totally, skip this test */
+       if (rx->local->hw->device_hides_wep)
+               return TXRX_CONTINUE;
+
+       /* Drop unencrypted frames if key is set. */
+       if (unlikely(!(rx->fc & WLAN_FC_ISWEP) &&
+                    WLAN_FC_GET_TYPE(rx->fc) == WLAN_FC_TYPE_DATA &&
+                    WLAN_FC_GET_STYPE(rx->fc) != WLAN_FC_STYPE_NULLFUNC &&
+                    (rx->key || rx->sdata->drop_unencrypted) &&
+                    (rx->sdata->eapol == 0 ||
+                     !ieee80211_is_eapol(rx->skb)))) {
+               printk(KERN_DEBUG "%s: RX non-WEP frame, but expected "
+                      "encryption\n", rx->dev->name);
+               return TXRX_DROP;
+       }
+       return TXRX_CONTINUE;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_rx_h_mgmt(struct ieee80211_txrx_data *rx)
+{
+        struct ieee80211_sub_if_data *sdata;
+       sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
+       if (sdata->type == IEEE80211_SUB_IF_TYPE_STA) {
+               ieee80211_sta_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status);
+       } else {
+               /* Management frames are sent to hostapd for processing */
+               ieee80211_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status,
+                                 ieee80211_msg_normal);
+       }
+       return TXRX_QUEUED;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_rx_h_passive_scan(struct ieee80211_txrx_data *rx)
+{
+       struct ieee80211_local *local = rx->local;
+        struct sk_buff *skb = rx->skb;
+
+       if (unlikely(local->sta_scanning != 0)) {
+               ieee80211_sta_rx_scan(rx->dev, skb, rx->u.rx.status);
+               return TXRX_QUEUED;
+       }
+
+       if (WLAN_FC_GET_TYPE(rx->fc) == WLAN_FC_TYPE_DATA)
+               local->scan.txrx_count++;
+        if (unlikely(local->scan.in_scan != 0 &&
+                    rx->u.rx.status->freq == local->scan.freq)) {
+                struct ieee80211_hdr *hdr;
+                u16 fc;
+
+                local->scan.rx_packets++;
+
+                hdr = (struct ieee80211_hdr *) skb->data;
+                fc = le16_to_cpu(hdr->frame_control);
+
+                if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
+                   WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON) {
+                       local->scan.rx_beacon++;
+                       /* Need to trim FCS here because it is normally
+                        * removed only after this passive scan handler. */
+                       if (rx->local->hw->rx_includes_fcs &&
+                           rx->skb->len > FCS_LEN)
+                               skb_trim(rx->skb, rx->skb->len - FCS_LEN);
+
+                        ieee80211_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status,
+                                          ieee80211_msg_passive_scan);
+                        return TXRX_QUEUED;
+                } else {
+                       I802_DEBUG_INC(local->rx_handlers_drop_passive_scan);
+                        return TXRX_DROP;
+                }
+        }
+
+        return TXRX_CONTINUE;
+}
+
+
+static u8 * ieee80211_get_bssid(struct ieee80211_hdr *hdr, size_t len)
+{
+       u16 fc;
+
+       if (len < 24)
+               return NULL;
+
+       fc = le16_to_cpu(hdr->frame_control);
+
+       switch (WLAN_FC_GET_TYPE(fc)) {
+       case WLAN_FC_TYPE_DATA:
+               switch (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
+               case WLAN_FC_TODS:
+                       return hdr->addr1;
+               case (WLAN_FC_TODS | WLAN_FC_FROMDS):
+                       return NULL;
+               case WLAN_FC_FROMDS:
+                       return hdr->addr2;
+               case 0:
+                       return hdr->addr3;
+               }
+               break;
+       case WLAN_FC_TYPE_MGMT:
+               return hdr->addr3;
+       case WLAN_FC_TYPE_CTRL:
+               if (WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PSPOLL)
+                       return hdr->addr1;
+               else
+                       return NULL;
+       }
+
+       return NULL;
+}
+
+
+static struct net_device * ieee80211_get_rx_dev(struct ieee80211_local *local,
+                                               struct ieee80211_hdr *hdr,
+                                               size_t len, int *sta_broadcast)
+{
+       u8 *bssid;
+       struct net_device *dev;
+       u16 fc;
+
+       bssid = ieee80211_get_bssid(hdr, len);
+       if (bssid) {
+               dev = ieee80211_own_bssid(local, bssid);
+               if (!dev && (local->conf.mode == IW_MODE_INFRA ||
+                            local->conf.mode == IW_MODE_ADHOC))
+                       dev = ieee80211_sta_bssid(local, bssid, hdr->addr1,
+                                                 sta_broadcast);
+               if (dev)
+                       return dev;
+       }
+
+       if (len >= 30) {
+               fc = le16_to_cpu(hdr->frame_control);
+               if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_DATA &&
+                   (fc & (WLAN_FC_TODS | WLAN_FC_FROMDS)) ==
+                   (WLAN_FC_TODS | WLAN_FC_FROMDS)) {
+                       dev = ieee80211_get_wds_dev(local, hdr->addr2);
+                       if (dev)
+                               return dev;
+               }
+       }
+
+       /* Default to default device if nothing else matches */
+       return local->wdev;
+}
+
+
+static void ieee80211_rx_michael_mic_report(struct net_device *dev,
+                                           struct ieee80211_hdr *hdr,
+                                           struct sta_info *sta,
+                                           struct ieee80211_txrx_data *rx)
+{
+       int keyidx, hdrlen;
+
+       hdrlen = ieee80211_get_hdrlen_from_skb(rx->skb);
+       if (rx->skb->len >= hdrlen + 4)
+               keyidx = rx->skb->data[hdrlen + 3] >> 6;
+       else
+               keyidx = -1;
+
+       /* TODO: verify that this is not triggered by fragmented
+        * frames (hw does not verify MIC for them). */
+       printk(KERN_DEBUG "%s: TKIP hwaccel reported Michael MIC "
+              "failure from " MACSTR " to " MACSTR " keyidx=%d\n",
+              dev->name, MAC2STR(hdr->addr2), MAC2STR(hdr->addr1), keyidx);
+
+       if (sta == NULL) {
+               /* Some hardware versions seem to generate incorrect
+                * Michael MIC reports; ignore them to avoid triggering
+                * countermeasures. */
+               printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
+                      "error for unknown address " MACSTR "\n",
+                      dev->name, MAC2STR(hdr->addr2));
+               goto ignore;
+       }
+
+       if (!(rx->fc & WLAN_FC_ISWEP)) {
+               printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
+                      "error for a frame with no ISWEP flag (src "
+                      MACSTR ")\n", dev->name, MAC2STR(hdr->addr2));
+               goto ignore;
+       }
+
+       if (rx->local->hw->wep_include_iv &&
+           rx->local->conf.mode == IW_MODE_MASTER) {
+               int keyidx = ieee80211_wep_get_keyidx(rx->skb);
+               /* AP with Pairwise keys support should never receive Michael
+                * MIC errors for non-zero keyidx because these are reserved
+                * for group keys and only the AP is sending real multicast
+                * frames in BSS. */
+               if (keyidx) {
+                       printk(KERN_DEBUG "%s: ignored Michael MIC error for "
+                              "a frame with non-zero keyidx (%d) (src " MACSTR
+                              ")\n", dev->name, keyidx, MAC2STR(hdr->addr2));
+                       goto ignore;
+               }
+       }
+
+       if (WLAN_FC_GET_TYPE(rx->fc) != WLAN_FC_TYPE_DATA &&
+           (WLAN_FC_GET_TYPE(rx->fc) != WLAN_FC_TYPE_MGMT ||
+            WLAN_FC_GET_STYPE(rx->fc) != WLAN_FC_STYPE_AUTH)) {
+               printk(KERN_DEBUG "%s: ignored spurious Michael MIC "
+                      "error for a frame that cannot be encrypted "
+                      "(fc=0x%04x) (src " MACSTR ")\n",
+                      dev->name, rx->fc, MAC2STR(hdr->addr2));
+               goto ignore;
+       }
+
+       do {
+               union iwreq_data wrqu;
+               char *buf = kmalloc(128, GFP_ATOMIC);
+               if (buf == NULL)
+                       break;
+
+               /* TODO: needed parameters: count, key type, TSC */
+               sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
+                       "keyid=%d %scast addr=" MACSTR ")",
+                       keyidx, hdr->addr1[0] & 0x01 ? "broad" : "uni",
+                       MAC2STR(hdr->addr2));
+               memset(&wrqu, 0, sizeof(wrqu));
+               wrqu.data.length = strlen(buf);
+               wireless_send_event(rx->dev, IWEVCUSTOM, &wrqu, buf);
+               kfree(buf);
+       } while (0);
+
+       /* TODO: consider verifying the MIC error report with software
+        * implementation if we get too many spurious reports from the
+        * hardware. */
+       ieee80211_rx_mgmt(rx->dev, rx->skb, rx->u.rx.status,
+                         ieee80211_msg_michael_mic_failure);
+       return;
+
+ ignore:
+       dev_kfree_skb(rx->skb);
+       rx->skb = NULL;
+}
+
+
+static void ieee80211_sta_rx_broadcast(struct ieee80211_txrx_data *rx)
+{
+       struct ieee80211_local *local = rx->dev->priv;
+       u8 *_bssid, bssid[ETH_ALEN];
+       struct sk_buff *orig_skb = rx->skb, *skb;
+       struct ieee80211_hdr *hdr;
+       ieee80211_rx_handler *handler;
+       ieee80211_txrx_result res;
+       struct list_head *ptr;
+
+       hdr = (struct ieee80211_hdr *) orig_skb->data;
+       _bssid = ieee80211_get_bssid(hdr, orig_skb->len);
+       if (_bssid == NULL) {
+               dev_kfree_skb(orig_skb);
+               return;
+       }
+       memcpy(bssid, _bssid, ETH_ALEN);
+
+       list_for_each(ptr, &local->sub_if_list) {
+               struct ieee80211_sub_if_data *sdata =
+                       list_entry(ptr, struct ieee80211_sub_if_data, list);
+               if (sdata->type != IEEE80211_SUB_IF_TYPE_STA ||
+                   (memcmp(bssid, sdata->u.sta.bssid, ETH_ALEN) != 0 &&
+                    !(bssid[0] & 0x01)))
+                       continue;
+
+               skb = skb_copy(orig_skb, GFP_ATOMIC);
+               if (skb == NULL) {
+                       if (net_ratelimit()) {
+                               printk(KERN_DEBUG "%s: failed to copy "
+                                      "multicast frame for %s",
+                                      rx->dev->name, sdata->dev->name);
+                       }
+                       continue;
+               }
+
+               hdr = (struct ieee80211_hdr *) skb->data;
+               rx->skb = skb;
+               rx->dev = sdata->dev;
+               rx->sdata = IEEE80211_DEV_TO_SUB_IF(rx->dev);
+
+               res = TXRX_DROP;
+               for (handler = local->rx_handlers; *handler != NULL; handler++)
+               {
+                       res = (*handler)(rx);
+                       if (res == TXRX_DROP || res == TXRX_QUEUED)
+                               break;
+               }
+
+               if (res == TXRX_DROP || *handler == NULL)
+                       dev_kfree_skb(skb);
+       }
+
+       dev_kfree_skb(orig_skb);
+}
+
+
+/*
+ * This is the receive path handler. It is called by a low level driver when an
+ * 802.11 MPDU is received from the hardware.
+ */
+void ieee80211_rx(struct net_device *dev, struct sk_buff *skb,
+                 struct ieee80211_rx_status *status)
+{
+       struct ieee80211_local *local = dev->priv;
+       struct sta_info *sta;
+       struct ieee80211_hdr *hdr;
+       ieee80211_rx_handler *handler;
+       struct ieee80211_txrx_data rx;
+        ieee80211_txrx_result res = TXRX_DROP;
+       u16 type;
+       int sta_broadcast = 0;
+
+       hdr = (struct ieee80211_hdr *) skb->data;
+       memset(&rx, 0, sizeof(rx));
+       rx.skb = skb;
+       rx.local = local;
+       if (skb->len >= 16) {
+               sta = rx.sta = sta_info_get(local, hdr->addr2);
+               if (unlikely(sta == NULL &&
+                            local->conf.mode == IW_MODE_ADHOC)) {
+                       u8 *bssid = ieee80211_get_bssid(hdr, skb->len);
+                       if (bssid &&
+                           memcmp(bssid, local->bssid, ETH_ALEN) == 0)
+                               sta = rx.sta =
+                                       ieee80211_ibss_add_sta(dev, skb, bssid,
+                                                              hdr->addr2);
+               }
+       } else
+               sta = rx.sta = NULL;
+        if (sta && !sta->assoc_ap && !(sta->flags & WLAN_STA_WDS))
+               rx.dev = sta->dev;
+       else
+               rx.dev = ieee80211_get_rx_dev(local, hdr, skb->len,
+                                             &sta_broadcast);
+
+       rx.sdata = IEEE80211_DEV_TO_SUB_IF(rx.dev);
+       rx.u.rx.status = status;
+       rx.fc = skb->len >= 2 ? le16_to_cpu(hdr->frame_control) : 0;
+       type = WLAN_FC_GET_TYPE(rx.fc);
+       if (type == WLAN_FC_TYPE_DATA || type == WLAN_FC_TYPE_MGMT)
+               local->dot11ReceivedFragmentCount++;
+       if (sta_broadcast) {
+               ieee80211_sta_rx_broadcast(&rx);
+               goto end;
+       }
+
+        if ((status->flag & RX_FLAG_MMIC_ERROR)) {
+               ieee80211_rx_michael_mic_report(dev, hdr, sta, &rx);
+               goto end;
+        }
+
+       for (handler = local->rx_handlers; *handler != NULL; handler++) {
+               res = (*handler)(&rx);
+               if (res != TXRX_CONTINUE) {
+                       if (res == TXRX_DROP) {
+                               I802_DEBUG_INC(local->rx_handlers_drop);
+                               if (sta)
+                                       sta->rx_dropped++;
+                       }
+                       if (res == TXRX_QUEUED)
+                               I802_DEBUG_INC(local->rx_handlers_queued);
+                       break;
+               }
+       }
+
+       if (res == TXRX_DROP || *handler == NULL)
+               dev_kfree_skb(skb);
+
+  end:
+       if (sta)
+                sta_info_release(local, sta);
+}
+
+
+static ieee80211_txrx_result
+ieee80211_tx_h_load_stats(struct ieee80211_txrx_data *tx)
+{
+        struct ieee80211_local *local = tx->local;
+       struct sk_buff *skb = tx->skb;
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
+       u32 load = 0, hdrtime;
+
+       /* TODO: this could be part of tx_status handling, so that the number
+        * of retries would be known; TX rate should in that case be stored
+        * somewhere with the packet */
+
+       /* Estimate total channel use caused by this frame */
+
+       /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
+        * 1 usec = 1/8 * (1080 / 10) = 13.5 */
+
+       if (local->conf.phymode == MODE_IEEE80211A ||
+           local->conf.phymode == MODE_ATHEROS_TURBO ||
+            local->conf.phymode == MODE_ATHEROS_TURBOG ||
+           (local->conf.phymode == MODE_IEEE80211G &&
+            tx->u.tx.rate->flags & IEEE80211_RATE_ERP))
+               hdrtime = CHAN_UTIL_HDR_SHORT;
+       else
+               hdrtime = CHAN_UTIL_HDR_LONG;
+
+       load = hdrtime;
+       if (!MULTICAST_ADDR(hdr->addr1))
+               load += hdrtime;
+
+       if (tx->u.tx.control->use_rts_cts)
+               load += 2 * hdrtime;
+       else if (tx->u.tx.control->use_cts_protect)
+               load += hdrtime;
+
+       load += skb->len * tx->u.tx.rate->rate_inv;
+
+       if (tx->u.tx.extra_frag) {
+               int i;
+               for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
+                       load += 2 * hdrtime;
+                       load += tx->u.tx.extra_frag[i]->len *
+                               tx->u.tx.rate->rate;
+               }
+       }
+
+       /* Divide channel_use by 8 to avoid wrapping around the counter */
+        load >>= CHAN_UTIL_SHIFT;
+        local->channel_use_raw += load;
+       if (tx->sta)
+               tx->sta->channel_use_raw += load;
+        tx->sdata->channel_use_raw += load;
+
+       return TXRX_CONTINUE;
+}
+
+
+static ieee80211_txrx_result
+ieee80211_rx_h_load_stats(struct ieee80211_txrx_data *rx)
+{
+        struct ieee80211_local *local = rx->local;
+       struct sk_buff *skb = rx->skb;
+       struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
+       u32 load = 0, hdrtime;
+       struct ieee80211_rate *rate;
+       int i;
+
+       /* Estimate total channel use caused by this frame */
+
+       if (unlikely(local->num_curr_rates < 0))
+               return TXRX_CONTINUE;
+
+       rate = &local->curr_rates[0];
+       for (i = 0; i < local->num_curr_rates; i++) {
+               if (local->curr_rates[i].val == rx->u.rx.status->rate) {
+                       rate = &local->curr_rates[i];
+                       break;
+               }
+       }
+
+       /* 1 bit at 1 Mbit/s takes 1 usec; in channel_use values,
+        * 1 usec = 1/8 * (1080 / 10) = 13.5 */
+
+       if (local->conf.phymode == MODE_IEEE80211A ||
+           local->conf.phymode == MODE_ATHEROS_TURBO ||
+            local->conf.phymode == MODE_ATHEROS_TURBOG ||
+           (local->conf.phymode == MODE_IEEE80211G &&
+            rate->flags & IEEE80211_RATE_ERP))
+               hdrtime = CHAN_UTIL_HDR_SHORT;
+       else
+               hdrtime = CHAN_UTIL_HDR_LONG;
+
+       load = hdrtime;
+       if (!MULTICAST_ADDR(hdr->addr1))
+               load += hdrtime;
+
+       load += skb->len * rate->rate_inv;
+
+       /* Divide channel_use by 8 to avoid wrapping around the counter */
+        load >>= CHAN_UTIL_SHIFT;
+        local->channel_use_raw += load;
+       if (rx->sta)
+               rx->sta->channel_use_raw += load;
+        rx->sdata->channel_use_raw += load;
+
+       return TXRX_CONTINUE;
+}
+
+
+static void ieee80211_stat_refresh(unsigned long data)
+{
+       struct ieee80211_local *local = (struct ieee80211_local *) data;
+        struct list_head *ptr, *n;
+
+       if (!local->stat_time)
+               return;
+
+       /* go through all stations */
+       spin_lock_bh(&local->sta_lock);
+       list_for_each(ptr, &local->sta_list) {
+               struct sta_info *sta =
+                       list_entry(ptr, struct sta_info, list);
+               sta->channel_use = (sta->channel_use_raw / local->stat_time) /
+                       CHAN_UTIL_PER_10MS;
+               sta->channel_use_raw = 0;
+       }
+       spin_unlock_bh(&local->sta_lock);
+
+       /* go through all subinterfaces */
+       list_for_each_safe(ptr, n, &local->sub_if_list) {
+               struct ieee80211_sub_if_data *sdata =
+                       list_entry(ptr, struct ieee80211_sub_if_data, list);
+                sdata->channel_use = (sdata->channel_use_raw /
+                                     local->stat_time) / CHAN_UTIL_PER_10MS;
+                sdata->channel_use_raw = 0;
+
+        }
+
+       /* hardware interface */
+       local->channel_use = (local->channel_use_raw /
+                             local->stat_time) / CHAN_UTIL_PER_10MS;
+       local->channel_use_raw = 0;
+
+       local->stat_timer.expires = jiffies + HZ * local->stat_time / 100;
+       add_timer(&local->stat_timer);
+}
+
+
+/* This is a version of the rx handler that can be called from hard irq
+ * context. Post the skb on the queue and schedule the tasklet */
+void ieee80211_rx_irqsafe(struct net_device *dev, struct sk_buff *skb,
+                         struct ieee80211_rx_status *status)
+{
+        struct ieee80211_local *local = dev->priv;
+
+        skb->dev = dev;
+        memcpy(skb->cb, status, sizeof(struct ieee80211_rx_status));
+        skb->pkt_type = ieee80211_rx_msg;
+        skb_queue_tail(&local->skb_queue, skb);
+        tasklet_schedule(&local->tasklet);
+}
+
+
+void ieee80211_tx_status_irqsafe(struct net_device *dev, struct sk_buff *skb,
+                                struct ieee80211_tx_status *status)
+{
+        struct ieee80211_local *local = dev->priv;
+       int tmp;
+
+       if (status->tx_filtered || status->excessive_retries) {
+               /* Need to save a copy of skb->cb somewhere. Storing it in the
+                * end of the data might not be the most efficient way of doing
+                * this (since it may require reallocation of packet data), but
+                * should be good enough for now since tx_filtered or
+                * excessive_retries should not be triggered that often. */
+               if (skb_is_nonlinear(skb)) {
+                       if (skb_linearize(skb, GFP_ATOMIC)) {
+                               printk(KERN_DEBUG "%s: Failed to linearize "
+                                      "skb\n", dev->name);
+                               dev_kfree_skb_irq(skb);
+                               return;
+                       }
+               }
+               if (skb_tailroom(skb) < sizeof(skb->cb) &&
+                   pskb_expand_head(skb, 0, sizeof(skb->cb), GFP_ATOMIC)) {
+                       printk(KERN_DEBUG "%s: Failed to store skb->cb "
+                              "in skb->data for TX filtered frame\n",
+                              dev->name);
+                       dev_kfree_skb_irq(skb);
+                       return;
+               }
+               memcpy(skb_put(skb, sizeof(skb->cb)), skb->cb,
+                      sizeof(skb->cb));
+       }
+
+        skb->dev = dev;
+        memcpy(skb->cb, status, sizeof(struct ieee80211_tx_status));
+        skb->pkt_type = ieee80211_tx_status_msg;
+       skb_queue_tail(status->req_tx_status ?
+                      &local->skb_queue : &local->skb_queue_unreliable, skb);
+       tmp = skb_queue_len(&local->skb_queue) +
+               skb_queue_len(&local->skb_queue_unreliable);
+       while (tmp > IEEE80211_IRQSAFE_QUEUE_LIMIT &&
+              (skb = skb_dequeue(&local->skb_queue_unreliable))) {
+               dev_kfree_skb_irq(skb);
+               tmp--;
+               I802_DEBUG_INC(local->tx_status_drop);
+       }
+        tasklet_schedule(&local->tasklet);
+}
+
+
+static void ieee80211_tasklet_handler(unsigned long data)
+{
+        struct ieee80211_local *local = (struct ieee80211_local *) data;
+        struct sk_buff *skb;
+       struct ieee80211_rx_status rx_status;
+       struct ieee80211_tx_status tx_status;
+
+       while ((skb = skb_dequeue(&local->skb_queue)) ||
+              (skb = skb_dequeue(&local->skb_queue_unreliable))) {
+                switch (skb->pkt_type) {
+               case ieee80211_rx_msg:
+                       /* Make a copy of the RX status because the original
+                        * skb may be freed during processing. Clear skb->type
+                        * in order to not confuse kernel netstack. */
+                       memcpy(&rx_status, skb->cb, sizeof(rx_status));
+                       skb->pkt_type = 0;
+                       ieee80211_rx(skb->dev, skb, &rx_status);
+                       break;
+               case ieee80211_tx_status_msg:
+                       /* Make a copy of the TX status because the original
+                        * skb may be freed during processing. */
+                       memcpy(&tx_status, skb->cb, sizeof(tx_status));
+                       skb->pkt_type = 0;
+                       if ((tx_status.tx_filtered ||
+                            tx_status.excessive_retries) &&
+                           skb->len >= sizeof(skb->cb)) {
+                               /* Restore skb->cb from the copy that was made
+                                * in ieee80211_tx_status_irqsafe() */
+                               memcpy(skb->cb,
+                                      skb->data + skb->len - sizeof(skb->cb),
+                                      sizeof(skb->cb));
+                               skb_trim(skb, skb->len - sizeof(skb->cb));
+                       }
+                       ieee80211_tx_status(skb->dev, skb, &tx_status);
+                       break;
+               default: /* should never get here! */
+                       printk(KERN_ERR "%s: Unknown message type (%d)\n",
+                              local->wdev->name, skb->pkt_type);
+                       dev_kfree_skb(skb);
+                       break;
+                }
+        }
+}
+
+
+/* Remove added headers (e.g., QoS control), encryption header/MIC, etc. to
+ * make a prepared TX frame (one that has been given to hw) to look like brand
+ * new IEEE 802.11 frame that is ready to go through TX processing again. */
+static void ieee80211_remove_tx_extra(struct ieee80211_local *local,
+                                     struct ieee80211_key *key,
+                                     struct sk_buff *skb)
+{
+       int hdrlen, iv_len, mic_len;
+
+       if (key == NULL)
+               return;
+
+       hdrlen = ieee80211_get_hdrlen_from_skb(skb);
+
+       switch (key->alg) {
+       case ALG_WEP:
+               iv_len = WEP_IV_LEN;
+               mic_len = WEP_ICV_LEN;
+               break;
+       case ALG_TKIP:
+               iv_len = TKIP_IV_LEN;
+               mic_len = TKIP_ICV_LEN;
+               break;
+       case ALG_CCMP:
+               iv_len = CCMP_HDR_LEN;
+               mic_len = CCMP_MIC_LEN;
+               break;
+       default:
+               return;
+       }
+
+       if (skb->len >= mic_len && key->force_sw_encrypt)
+               skb_trim(skb, skb->len - mic_len);
+       if (skb->len >= iv_len && skb->len > hdrlen) {
+               memmove(skb->data + iv_len, skb->data, hdrlen);
+               skb_pull(skb, iv_len);
+       }
+
+       {
+               struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
+               u16 fc = le16_to_cpu(hdr->frame_control);
+               if ((fc & 0x8C) == 0x88) /* QoS Control Field */ {
+                       fc &= ~(WLAN_FC_STYPE_QOS_DATA << 4);
+                       hdr->frame_control = cpu_to_le16(fc);
+                       memmove(skb->data + 2, skb->data, hdrlen - 2);
+                       skb_pull(skb, 2);
+               }
+       }
+}
+
+
+void ieee80211_tx_status(struct net_device *dev, struct sk_buff *skb,
+                        struct ieee80211_tx_status *status)
+{
+       struct sk_buff *skb2;
+        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
+        struct ieee80211_local *local = dev->priv;
+       struct ieee80211_tx_packet_data *pkt_data =
+               (struct ieee80211_tx_packet_data *) skb->cb;
+       u16 frag, type;
+       u32 msg_type;
+
+       if (!status) {
+                printk(KERN_ERR
+                      "%s: ieee80211_tx_status called with NULL status\n",
+                      dev->name);
+               dev_kfree_skb(skb);
+               return;
+       }
+
+       if (status->excessive_retries) {
+               struct sta_info *sta;
+               
+
+               sta = sta_info_get(local, hdr->addr1);
+               if (sta) {
+                       if (sta->flags & WLAN_STA_PS) {
+                               /* The STA is in power save mode, so assume
+                                * that this TX packet failed because of that.
+                                */
+                               status->excessive_retries = 0;
+                               status->tx_filtered = 1;
+                       }
+                       sta_info_release(local, sta);
+               }
+       }
+
+       if (status->tx_filtered) {
+               struct sta_info *sta;
+               sta = sta_info_get(local, hdr->addr1);
+               if (sta) {
+
+                       sta->tx_filtered_count++;
+
+                       /* Clear the TX filter mask for this STA when sending
+                        * the next packet. If the STA went to power save mode,
+                        * this will happen when it is waking up for the next
+                        * time. */
+                       sta->clear_dst_mask = 1;
+
+                       /* TODO: Is the WLAN_STA_PS flag always set here or is
+                        * the race between RX and TX status causing some
+                        * packets to be filtered out before 80211.o gets an
+                        * update for PS status? This seems to be the case, so
+                        * no changes are likely to be needed. */
+                       if (sta->flags & WLAN_STA_PS &&
+                           skb_queue_len(&sta->tx_filtered) <
+                           STA_MAX_TX_BUFFER) {
+                               ieee80211_remove_tx_extra(local, sta->key,
+                                                         skb);
+                               skb_queue_tail(&sta->tx_filtered, skb);
+                       } else if (!(sta->flags & WLAN_STA_PS) &&
+                                  !pkt_data->control.requeue) {
+                               /* Software retry the packet once */
+                               pkt_data->control.requeue = 1;
+                               ieee80211_remove_tx_extra(local, sta->key,
+                                                         skb);
+                               dev_queue_xmit(skb);
+                       } else {
+                               if (net_ratelimit()) {
+                                       printk(KERN_DEBUG "%s: dropped TX "
+                                              "filtered frame queue_len=%d "
+                                              "PS=%d @%lu\n",
+                                              dev->name,
+                                              skb_queue_len(
+                                                      &sta->tx_filtered),
+                                              !!(sta->flags & WLAN_STA_PS),
+                                              jiffies);
+                               }
+                               dev_kfree_skb(skb);
+                       }
+                       sta_info_release(local, sta);
+                       return;
+               }
+       } else {
+               rate_control_tx_status(dev, skb, status);
+       }
+
+#ifdef IEEE80211_LEDS
+        if (local->tx_led_counter && (local->tx_led_counter-- == 1)) {
+                ieee80211_tx_led(0, dev);
+        }
+#endif /* IEEE80211_LEDS */
+        /* SNMP counters
+        * Fragments are passed to low-level drivers as separate skbs, so these
+        * are actually fragments, not frames. Update frame counters only for
+        * the first fragment of the frame. */
+
+       frag = WLAN_GET_SEQ_FRAG(le16_to_cpu(hdr->seq_ctrl));
+       type = WLAN_FC_GET_TYPE(le16_to_cpu(hdr->frame_control));
+
+        if (status->ack) {
+               if (frag == 0) {
+                       local->dot11TransmittedFrameCount++;
+                       if (MULTICAST_ADDR(hdr->addr1))
+                               local->dot11MulticastTransmittedFrameCount++;
+                       if (status->retry_count > 0)
+                               local->dot11RetryCount++;
+                       if (status->retry_count > 1)
+                               local->dot11MultipleRetryCount++;
+               }
+
+               /* This counter shall be incremented for an acknowledged MPDU
+                * with an individual address in the address 1 field or an MPDU
+                * with a multicast address in the address 1 field of type Data
+                * or Management. */
+               if (!MULTICAST_ADDR(hdr->addr1) || type == WLAN_FC_TYPE_DATA ||
+                   type == WLAN_FC_TYPE_MGMT)
+                       local->dot11TransmittedFragmentCount++;
+        } else {
+               if (frag == 0)
+                       local->dot11FailedCount++;
+        }
+
+        if (!status->req_tx_status) {
+               dev_kfree_skb(skb);
+               return;
+       }
+
+       msg_type = status->ack ? ieee80211_msg_tx_callback_ack :
+               ieee80211_msg_tx_callback_fail;
+
+       /* skb was the original skb used for TX. Clone it and give the clone
+        * to netif_rx(). Free original skb. */
+       skb2 = skb_copy(skb, GFP_ATOMIC);
+        if (!skb2) {
+               dev_kfree_skb(skb);
+               return;
+       }
+       dev_kfree_skb(skb);
+       skb = skb2;
+
+        /* Send frame to hostapd */
+        ieee80211_rx_mgmt(dev, skb, NULL, msg_type);
+}
+
+
+/* TODO: implement register/unregister functions for adding TX/RX handlers
+ * into ordered list */
+
+static ieee80211_rx_handler ieee80211_rx_handlers[] =
+{
+       ieee80211_rx_h_parse_qos,
+       ieee80211_rx_h_load_stats,
+       ieee80211_rx_h_monitor,
+       ieee80211_rx_h_passive_scan,
+       ieee80211_rx_h_check,
+       ieee80211_rx_h_sta_process,
+       ieee80211_rx_h_ccmp_decrypt,
+       ieee80211_rx_h_tkip_decrypt,
+       ieee80211_rx_h_wep_weak_iv_detection,
+       ieee80211_rx_h_wep_decrypt,
+       ieee80211_rx_h_defragment,
+       ieee80211_rx_h_ieee80211_rx_h_ps_poll,
+       ieee80211_rx_h_michael_mic_verify,
+       /* this must be after decryption - so header is counted in MPDU mic
+        * must be before pae and data, so QOS_DATA format frames
+        * are not passed to user space by these functions
+        */
+       ieee80211_rx_h_remove_qos_control,
+       ieee80211_rx_h_802_1x_pae,
+       ieee80211_rx_h_drop_unencrypted,
+       ieee80211_rx_h_data,
+       ieee80211_rx_h_mgmt,
+       NULL
+};
+
+static ieee80211_tx_handler ieee80211_tx_handlers[] =
+{
+       ieee80211_tx_h_rate_limit,
+       ieee80211_tx_h_check_assoc,
+       ieee80211_tx_h_ps_buf,
+       ieee80211_tx_h_select_key,
+       ieee80211_tx_h_michael_mic_add,
+       ieee80211_tx_h_fragment,
+       ieee80211_tx_h_tkip_encrypt,
+       ieee80211_tx_h_ccmp_encrypt,
+       ieee80211_tx_h_wep_encrypt,
+       ieee80211_tx_h_rate_ctrl,
+       ieee80211_tx_h_misc,
+       ieee80211_tx_h_load_stats,
+       NULL
+};
+
+
+static void ieee80211_if_sdata_init(struct ieee80211_sub_if_data *sdata)
+{
+       /* Default values for sub-interface parameters */
+       sdata->drop_unencrypted = 0;
+       sdata->eapol = 1;
+}
+
+
+static struct net_device *ieee80211_if_add(struct net_device *dev,
+                                          char *name, int locked)
+{
+       struct net_device *wds_dev = NULL, *tmp_dev;
+        struct ieee80211_local *local = dev->priv;
+       struct ieee80211_sub_if_data *sdata = NULL, *sdata_parent;
+        int alloc_size;
+       int ret;
+       int i;
+
+       /* ensure 32-bit alignment of our private data and hw private data */
+       alloc_size = sizeof(struct net_device) + 3 +
+               sizeof(struct ieee80211_sub_if_data) + 3;
+
+        wds_dev = (struct net_device *) kmalloc(alloc_size, GFP_KERNEL);
+        if (wds_dev == NULL)
+               return NULL;
+
+       memset(wds_dev, 0, alloc_size);
+       wds_dev->priv = local;
+       ether_setup(wds_dev);
+       if (strlen(name) == 0) {
+               i = 0;
+               do {
+                       sprintf(wds_dev->name, "%s.%d", dev->name, i++);
+                       tmp_dev = dev_get_by_name(wds_dev->name);
+                       if (tmp_dev == NULL)
+                               break;
+                       dev_put(tmp_dev);
+               } while (i < 10000);
+       } else {
+                snprintf(wds_dev->name, IFNAMSIZ, "%s", name);
+       }
+
+       memcpy(wds_dev->dev_addr, dev->dev_addr, ETH_ALEN);
+       wds_dev->hard_start_xmit = ieee80211_subif_start_xmit;
+        wds_dev->do_ioctl = ieee80211_ioctl;
+       wds_dev->change_mtu = ieee80211_change_mtu;
+        wds_dev->tx_timeout = ieee80211_tx_timeout;
+        wds_dev->get_stats = ieee80211_get_stats;
+        wds_dev->open = ieee80211_open;
+       wds_dev->stop = ieee80211_stop;
+       wds_dev->base_addr = dev->base_addr;
+       wds_dev->irq = dev->irq;
+       wds_dev->mem_start = dev->mem_start;
+       wds_dev->mem_end = dev->mem_end;
+       wds_dev->tx_queue_len = 0;
+
+       sdata = IEEE80211_DEV_TO_SUB_IF(wds_dev);
+        sdata->type = IEEE80211_SUB_IF_TYPE_NORM;
+        sdata->master = local->mdev;
+        sdata->dev = wds_dev;
+       sdata->local = local;
+       memset(&sdata->stats, 0, sizeof(struct net_device_stats));
+       sdata_parent = IEEE80211_DEV_TO_SUB_IF(dev);
+       if (sdata_parent->type == IEEE80211_SUB_IF_TYPE_NORM)
+               sdata->bss = &sdata_parent->u.norm;
+       else {
+               printk(KERN_DEBUG "%s: could not set BSS pointer for new "
+                      "interface %s\n", dev->name, wds_dev->name);
+       }
+       ieee80211_if_sdata_init(sdata);
+
+        if (locked)
+               ret = register_netdevice(wds_dev);
+       else
+               ret = register_netdev(wds_dev);
+       if (ret) {
+               kfree(wds_dev);
+               return NULL;
+       }
+
+        list_add(&sdata->list, &local->sub_if_list);
+
+        strcpy(name, wds_dev->name);
+
+        return wds_dev;
+}
+
+
+int ieee80211_if_add_wds(struct net_device *dev, char *name,
+                         struct ieee80211_if_wds *wds, int locked)
+{
+        struct net_device *wds_dev = NULL;
+       struct ieee80211_sub_if_data *sdata = NULL;
+
+        if (strlen(name) != 0) {
+                wds_dev = dev_get_by_name(name);
+                if (wds_dev) {
+                        dev_put(wds_dev);
+                        return -EEXIST;
+                }
+        }
+
+        wds_dev = ieee80211_if_add(dev, name, locked);
+        if (wds_dev == NULL)
+                return -ENOANO;
+
+       sdata = IEEE80211_DEV_TO_SUB_IF(wds_dev);
+        sdata->type = IEEE80211_SUB_IF_TYPE_WDS;
+        memcpy(&sdata->u.wds, wds, sizeof(struct ieee80211_if_wds));
+
+#ifdef CONFIG_IEEE80211_VERBOSE_DEBUG
+       printk(KERN_DEBUG
+              "%s: Added WDS Link to " MACSTR "\n",
+              wds_dev->name, MAC2STR(sdata->u.wds.remote_addr));
+#endif /* CONFIG_IEEE80211_VERBOSE_DEBUG */
+
+
+       ieee80211_proc_init_virtual(wds_dev);
+
+        return 0;
+}
+
+
+int ieee80211_if_update_wds(struct net_device *dev, char *name,
+                           struct ieee80211_if_wds *wds, int locked)
+{
+       struct net_device *wds_dev = NULL;
+       struct ieee80211_local *local = dev->priv;
+       struct ieee80211_sub_if_data *sdata = NULL;
+       struct sta_info *sta;
+       struct list_head *ptr;
+
+       list_for_each(ptr, &local->sub_if_list) {
+               sdata = list_entry(ptr, struct ieee80211_sub_if_data, list);
+               if (strcmp(name, sdata->dev->name) == 0) {
+                       wds_dev = sdata->dev;
+                       break;
+               }
+       }
+
+       if (wds_dev == NULL || sdata->type != IEEE80211_SUB_IF_TYPE_WDS)
+               return -ENODEV;
+
+       /* Remove STA entry for the old peer */
+       sta = sta_info_get(local, sdata->u.wds.remote_addr);
+       if (sta) {
+               sta_info_release(local, sta);
+               sta_info_free(local, sta, 0);
+       } else {
+               printk(KERN_DEBUG "%s: could not find STA entry for WDS link "
+                      "%s peer " MACSTR "\n",
+                      dev->name, wds_dev->name,
+                      MAC2STR(sdata->u.wds.remote_addr));
+       }
+
+       /* Update WDS link data */
+        memcpy(&sdata->u.wds, wds, sizeof(struct ieee80211_if_wds));
+
+
+        return 0;
+}
+
+
+static void ieee80211_if_init(struct net_device *dev)
+{
+        struct ieee80211_local *local = dev->priv;
+
+        spin_lock_init(&local->sub_if_lock);
+        INIT_LIST_HEAD(&local->sub_if_list);
+
+}
+
+
+int ieee80211_if_add_vlan(struct net_device *dev,
+                          char *name,
+                          struct ieee80211_if_vlan *vlan,
+                          int locked)
+{
+        struct net_device *vlan_dev = NULL;
+        struct ieee80211_sub_if_data *sdata = NULL;
+
+        if (strlen(name) != 0) {
+                vlan_dev = dev_get_by_name(name);
+                if (vlan_dev) {
+                        dev_put(vlan_dev);
+                        return -EEXIST;
+                }
+        }
+
+        vlan_dev = ieee80211_if_add(dev, name, locked);
+        if (vlan_dev == NULL)
+                return -ENOANO;
+
+       sdata = IEEE80211_DEV_TO_SUB_IF(vlan_dev);
+        sdata->type = IEEE80211_SUB_IF_TYPE_VLAN;
+       ieee80211_proc_init_virtual(vlan_dev);
+        return 0;
+}
+
+
+static void ieee80211_if_norm_init(struct ieee80211_sub_if_data *sdata)
+{
+       sdata->type = IEEE80211_SUB_IF_TYPE_NORM;
+       sdata->u.norm.dtim_period = 2;
+       sdata->u.norm.force_unicast_rateidx = -1;
+       sdata->u.norm.max_ratectrl_rateidx = -1;
+       skb_queue_head_init(&sdata->u.norm.ps_bc_buf);
+       sdata->bss = &sdata->u.norm;
+}
+
+
+int ieee80211_if_add_norm(struct net_device *dev, char *name, u8 *bssid,
+                         int locked)
+{
+        struct ieee80211_local *local = dev->priv;
+        struct net_device *norm_dev = NULL;
+        struct ieee80211_sub_if_data *sdata = NULL;
+
+       if (local->bss_dev_count >= local->conf.bss_count)
+               return -ENOBUFS;
+
+        if (strlen(name) != 0) {
+                norm_dev = dev_get_by_name(name);
+                if (norm_dev) {
+                        dev_put(norm_dev);
+                        return -EEXIST;
+                }
+        }
+
+        norm_dev = ieee80211_if_add(dev, name, locked);
+        if (norm_dev == NULL)
+                return -ENOANO;
+
+       memcpy(norm_dev->dev_addr, bssid, ETH_ALEN);
+       sdata = IEEE80211_DEV_TO_SUB_IF(norm_dev);
+       ieee80211_if_norm_init(sdata);
+       ieee80211_proc_init_virtual(norm_dev);
+       spin_lock_bh(&local->sub_if_lock);
+       local->bss_devs[local->bss_dev_count] = norm_dev;
+       local->bss_dev_count++;
+       spin_unlock_bh(&local->sub_if_lock);
+
+        return 0;
+}
+
+
+static void ieee80211_addr_inc(u8 *addr)
+{
+       int pos = 5;
+       while (pos >= 0) {
+               addr[pos]++;
+               if (addr[pos] != 0)
+                       break;
+               pos--;
+       }
+}
+
+
+int ieee80211_if_add_sta(struct net_device *dev, char *name, int locked)
+{
+       struct ieee80211_local *local = dev->priv;
+       struct net_device *sta_dev;
+       struct ieee80211_sub_if_data *sdata;
+       struct ieee80211_if_sta *ifsta;
+       int i;
+
+       if (local->sta_dev_count >= local->conf.bss_count)
+               return -ENOBUFS;
+
+       if (strlen(name) != 0) {
+               sta_dev = dev_get_by_name(name);
+               if (sta_dev) {
+                       dev_put(sta_dev);
+                       return -EEXIST;
+               }
+       }
+
+       sta_dev = ieee80211_if_add(dev, name, locked);
+       if (sta_dev == NULL)
+               return -ENOANO;
+
+       sdata = IEEE80211_DEV_TO_SUB_IF(sta_dev);
+       ifsta = &sdata->u.sta;
+       sdata->type = IEEE80211_SUB_IF_TYPE_STA;
+       ieee80211_proc_init_virtual(sta_dev);
+
+       spin_lock_bh(&local->sub_if_lock);
+       for (i = 0; i < local->conf.bss_count; i++) {
+               if (local->sta_devs[i] == NULL) {
+                       local->sta_devs[i] = sta_dev;
+                       local->sta_dev_count++;
+                       printk(KERN_DEBUG "%s: using STA entry %d\n",
+                              sta_dev->name, i);
+                       while (i > 0) {
+                               ieee80211_addr_inc(sta_dev->dev_addr);
+                               i--;
+                       }
+                       printk(KERN_DEBUG "%s: MAC address " MACSTR "\n",
+                              sta_dev->name, MAC2STR(sta_dev->dev_addr));
+                       break;
+               }
+       }
+       spin_unlock_bh(&local->sub_if_lock);
+
+       init_timer(&ifsta->timer);
+       ifsta->timer.data = (unsigned long) sta_dev;
+       ifsta->timer.function = ieee80211_sta_timer;
+
+       ifsta->capab = WLAN_CAPABILITY_ESS;
+       ifsta->auth_algs = IEEE80211_AUTH_ALG_OPEN |
+               IEEE80211_AUTH_ALG_SHARED_KEY;
+       ifsta->create_ibss = 1;
+       ifsta->wmm_enabled = 1;
+
+       return 0;
+}
+
+
+static void ieee80211_if_del(struct ieee80211_local *local,
+                            struct ieee80211_sub_if_data *sdata, int locked)
+{
+        struct sta_info *sta;
+       u8 addr[ETH_ALEN];
+       int i, j;
+        struct list_head *ptr, *n;
+
+       memset(addr, 0xff, ETH_ALEN);
+       for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
+               if (!sdata->keys[i])
+                       continue;
+#if 0
+               /* Low-level driver has probably disabled hw
+                * already, so there is not really much point
+                * in disabling the keys at this point. */
+               if (local->hw->set_key)
+                       local->hw->set_key(dev, DISABLE_KEY, addr,
+                                          local->keys[i], 0);
+#endif
+               kfree(sdata->keys[i]);
+       }
+
+       switch (sdata->type) {
+       case IEEE80211_SUB_IF_TYPE_NORM:
+               /* Remove all virtual interfaces that use this BSS
+                * as their sdata->bss */
+               list_for_each_safe(ptr, n, &local->sub_if_list) {
+                       struct ieee80211_sub_if_data *tsdata =
+                               list_entry(ptr, struct ieee80211_sub_if_data,
+                                          list);
+
+                       if (tsdata != sdata && tsdata->bss == &sdata->u.norm) {
+                               printk(KERN_DEBUG "%s: removing virtual "
+                                      "interface %s because its BSS interface"
+                                      " is being removed\n",
+                                      sdata->dev->name, tsdata->dev->name);
+                               ieee80211_if_del(local, tsdata, locked);
+                       }
+               }
+
+               kfree(sdata->u.norm.beacon_head);
+               kfree(sdata->u.norm.beacon_tail);
+               spin_lock_bh(&local->sub_if_lock);
+               for (j = 0; j < local->bss_dev_count; j++) {
+                       if (sdata->dev == local->bss_devs[j]) {
+                               if (j + 1 < local->bss_dev_count) {
+                                       memcpy(&local->bss_devs[j],
+                                              &local->bss_devs[j + 1],
+                                              (local->bss_dev_count - j - 1) *
+                                              sizeof(local->bss_devs[0]));
+                                       local->bss_devs[local->bss_dev_count -
+                                                       1] = NULL;
+                               } else
+                                       local->bss_devs[j] = NULL;
+                               local->bss_dev_count--;
+                               break;
+                       }
+               }
+               spin_unlock_bh(&local->sub_if_lock);
+
+               if (sdata->dev != local->mdev) {
+                       struct sk_buff *skb;
+                       while ((skb = skb_dequeue(&sdata->u.norm.ps_bc_buf))) {
+                               local->total_ps_buffered--;
+                               dev_kfree_skb(skb);
+                       }
+               }
+
+               break;
+       case IEEE80211_SUB_IF_TYPE_WDS:
+               sta = sta_info_get(local, sdata->u.wds.remote_addr);
+               if (sta) {
+                       sta_info_release(local, sta);
+                       sta_info_free(local, sta, 0);
+               } else {
+#ifdef CONFIG_IEEE80211_VERBOSE_DEBUG
+                       printk(KERN_DEBUG "%s: Someone had deleted my STA "
+                              "entry for the WDS link\n", sdata->dev->name);
+#endif /* CONFIG_IEEE80211_VERBOSE_DEBUG */
+               }
+               break;
+       case IEEE80211_SUB_IF_TYPE_STA:
+               del_timer_sync(&sdata->u.sta.timer);
+               if (local->scan_timer.data == (unsigned long) sdata->dev)
+                       del_timer_sync(&local->scan_timer);
+               kfree(sdata->u.sta.extra_ie);
+               sdata->u.sta.extra_ie = NULL;
+               kfree(sdata->u.sta.assocreq_ies);
+               sdata->u.sta.assocreq_ies = NULL;
+               kfree(sdata->u.sta.assocresp_ies);
+               sdata->u.sta.assocresp_ies = NULL;
+               if (sdata->u.sta.probe_resp) {
+                       dev_kfree_skb(sdata->u.sta.probe_resp);
+                       sdata->u.sta.probe_resp = NULL;
+               }
+               for (i = 0; i < local->conf.bss_count; i++) {
+                       if (local->sta_devs[i] == sdata->dev) {
+                               local->sta_devs[i] = NULL;
+                               local->sta_dev_count--;
+                               break;
+                       }
+               }
+
+               break;
+       }
+
+       /* remove all STAs that are bound to this virtual interface */
+       sta_info_flush(local, sdata->dev);
+
+       list_del(&sdata->list);
+       ieee80211_proc_deinit_virtual(sdata->dev);
+       if (locked)
+               unregister_netdevice(sdata->dev);
+       else
+               unregister_netdev(sdata->dev);
+       /* Default data device and management device are allocated with the
+        * master device. All other devices are separately allocated and will
+        * be freed here. */
+       if (sdata->dev != local->mdev && sdata->dev != local->wdev &&
+           sdata->dev != local->apdev)
+               kfree(sdata->dev);
+}
+
+
+static int ieee80211_if_remove(struct net_device *dev, char *name, int id,
+                              int locked)
+{
+        struct ieee80211_local *local = dev->priv;
+        struct list_head *ptr, *n;
+
+       /* Make sure not to touch sdata->master since it may
+        * have already been deleted, etc. */
+
+       list_for_each_safe(ptr, n, &local->sub_if_list) {
+               struct ieee80211_sub_if_data *sdata =
+                       list_entry(ptr, struct ieee80211_sub_if_data, list);
+
+               if (sdata->type == id && strcmp(name, sdata->dev->name) == 0) {
+                       ieee80211_if_del(local, sdata, locked);
+                       break;
+               }
+       }
+
+        return 0;
+}
+
+
+int ieee80211_if_remove_wds(struct net_device *dev, char *name, int locked)
+{
+        return ieee80211_if_remove(dev, name, IEEE80211_SUB_IF_TYPE_WDS,
+                                  locked);
+}
+
+
+int ieee80211_if_remove_vlan(struct net_device *dev, char *name, int locked)
+{
+        return ieee80211_if_remove(dev, name, IEEE80211_SUB_IF_TYPE_VLAN,
+                                  locked);
+}
+
+
+int ieee80211_if_remove_norm(struct net_device *dev, char *name, int locked)
+{
+       return ieee80211_if_remove(dev, name, IEEE80211_SUB_IF_TYPE_NORM,
+                                  locked);
+}
+
+
+int ieee80211_if_remove_sta(struct net_device *dev, char *name, int locked)
+{
+       return ieee80211_if_remove(dev, name, IEEE80211_SUB_IF_TYPE_STA,
+                                  locked);
+}
+
+
+int ieee80211_if_flush(struct net_device *dev, int locked)
+{
+        struct ieee80211_local *local = dev->priv;
+        struct list_head *ptr, *n;
+
+       list_for_each_safe(ptr, n, &local->sub_if_list) {
+               struct ieee80211_sub_if_data *sdata =
+                       list_entry(ptr, struct ieee80211_sub_if_data, list);
+
+               if (sdata->dev != local->mdev &&
+                   sdata->dev != local->wdev &&
+                   sdata->dev != local->apdev)
+                       ieee80211_if_del(local, sdata, locked);
+       }
+
+        return 0;
+}
+
+
+static void ieee80211_precalc_rates(struct ieee80211_hw *hw)
+{
+       struct ieee80211_hw_modes *mode;
+       struct ieee80211_rate *rate;
+       int m, r;
+
+       for (m = 0; m < hw->num_modes; m++) {
+               mode = &hw->modes[m];
+               for (r = 0; r < mode->num_rates; r++) {
+                       rate = &mode->rates[r];
+                       rate->rate_inv = CHAN_UTIL_RATE_LCM / rate->rate;
+               }
+       }
+}
+
+
+struct net_device *ieee80211_alloc_hw(size_t priv_data_len,
+                                     void (*setup)(struct net_device *))
+{
+       struct net_device *dev, *apdev, *mdev;
+        struct ieee80211_local *local;
+        struct ieee80211_sub_if_data *sdata;
+       int alloc_size;
+
+       /* Ensure 32-bit alignment of our private data and hw private data.
+        * Each net_device is followed by a sub_if_data which which is used
+        * for wds/vlan information; it is aligned as well.
+        *
+         * Sample memory map looks something like:
+         *
+         * 0000 *****************
+         *      * net_dev       *
+         * 015c *****************
+         *      * sub_if        *
+         * 017c *****************
+         *      * local         *
+         * 0b84 *****************
+         *      * hw_priv       *
+         * 1664 *****************
+         *      * ap net_dev    *
+         * 17c0 *****************
+         *      * sub_if        *
+        *      *****************
+        *      * master net_dev*
+        *      *****************
+        *      * sub_if        *
+         *      *****************
+         */
+        alloc_size = sizeof(struct net_device) +
+                sizeof(struct ieee80211_sub_if_data) + 3 +
+                sizeof(struct ieee80211_local) + 3 +
+                priv_data_len + 3 +
+                sizeof(struct net_device) + 3 +
+               sizeof(struct ieee80211_sub_if_data) + 3 +
+                sizeof(struct net_device) + 3 +
+               sizeof(struct ieee80211_sub_if_data) + 3 +
+               4096;
+        mdev = (struct net_device *) kzalloc(alloc_size, GFP_KERNEL);
+       if (mdev == NULL)
+               return NULL;
+
+        mdev->priv = (struct net_device *)
+                (((long) mdev +
+                  sizeof(struct net_device) +
+                  sizeof(struct ieee80211_sub_if_data) + 3)
+                 & ~3);
+       local = mdev->priv;
+       local->hw_priv = (void *)
+                (((long) local + sizeof(struct ieee80211_local) + 3) & ~3);
+       apdev = (struct net_device *)
+               (((long) local->hw_priv + priv_data_len + 3) & ~3);
+        dev = (struct net_device *)
+               (((long) apdev +
+                 sizeof(struct net_device) +
+                 sizeof(struct ieee80211_sub_if_data) + 3)
+                 & ~3);
+        dev->priv = local;
+
+       ether_setup(dev);
+       memcpy(dev->name, "wlan%d", 7);
+
+        dev->hard_start_xmit = ieee80211_subif_start_xmit;
+        dev->do_ioctl = ieee80211_ioctl;
+       dev->change_mtu = ieee80211_change_mtu;
+        dev->tx_timeout = ieee80211_tx_timeout;
+        dev->get_stats = ieee80211_get_stats;
+        dev->open = ieee80211_open;
+        dev->stop = ieee80211_stop;
+       dev->tx_queue_len = 0;
+       dev->set_mac_address = ieee80211_set_mac_address;
+
+        local->wdev = dev;
+       local->mdev = mdev;
+        local->rx_handlers = ieee80211_rx_handlers;
+        local->tx_handlers = ieee80211_tx_handlers;
+
+       local->bridge_packets = 1;
+
+       local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
+       local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
+       local->short_retry_limit = 7;
+       local->long_retry_limit = 4;
+       local->conf.calib_int = 60;
+       local->rate_ctrl_num_up = RATE_CONTROL_NUM_UP;
+       local->rate_ctrl_num_down = RATE_CONTROL_NUM_DOWN;
+       local->conf.bss_count = 1;
+       memset(local->conf.bssid_mask, 0xff, ETH_ALEN);
+       local->bss_devs = kmalloc(sizeof(struct net_device *), GFP_KERNEL);
+       if (local->bss_devs == NULL)
+               goto fail;
+       local->bss_devs[0] = local->wdev;
+       local->bss_dev_count = 1;
+       local->sta_devs = kmalloc(sizeof(struct net_device *), GFP_KERNEL);
+       if (local->sta_devs == NULL)
+               goto fail;
+       local->sta_devs[0] = NULL;
+
+        local->scan.in_scan = 0;
+       local->hw_modes = (unsigned int) -1;
+
+        init_timer(&local->scan.timer); /* clear it out */
+
+        spin_lock_init(&local->generic_lock);
+       init_timer(&local->rate_limit_timer);
+       local->rate_limit_timer.function = ieee80211_rate_limit;
+       local->rate_limit_timer.data = (unsigned long) local;
+       init_timer(&local->stat_timer);
+       local->stat_timer.function = ieee80211_stat_refresh;
+       local->stat_timer.data = (unsigned long) local;
+       ieee80211_rx_bss_list_init(dev);
+
+        sta_info_init(local);
+
+        ieee80211_if_init(dev);
+
+        sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+        sdata->dev = dev;
+        sdata->master = mdev;
+        sdata->local = local;
+       ieee80211_if_sdata_init(sdata);
+       ieee80211_if_norm_init(sdata);
+       list_add_tail(&sdata->list, &local->sub_if_list);
+
+       if (strlen(dev->name) + 2 >= sizeof(dev->name))
+               goto fail;
+
+        apdev = (struct net_device *)
+                (((long) local->hw_priv + priv_data_len + 3) & ~3);
+        local->apdev = apdev;
+       ether_setup(apdev);
+       apdev->priv = local;
+       apdev->hard_start_xmit = ieee80211_mgmt_start_xmit;
+       apdev->change_mtu = ieee80211_change_mtu_apdev;
+       apdev->get_stats = ieee80211_get_stats;
+        apdev->open = ieee80211_open;
+        apdev->stop = ieee80211_stop;
+       apdev->type = ARPHRD_IEEE80211_PRISM;
+        apdev->hard_header_parse = header_parse_80211;
+       apdev->tx_queue_len = 0;
+       sprintf(apdev->name, "%sap", dev->name);
+
+        sdata = IEEE80211_DEV_TO_SUB_IF(apdev);
+        sdata->type = IEEE80211_SUB_IF_TYPE_MGMT;
+        sdata->dev = apdev;
+        sdata->master = mdev;
+        sdata->local = local;
+        list_add_tail(&sdata->list, &local->sub_if_list);
+
+       ether_setup(mdev);
+       mdev->hard_start_xmit = ieee80211_master_start_xmit;
+        mdev->do_ioctl = ieee80211_ioctl;
+       mdev->change_mtu = ieee80211_change_mtu;
+        mdev->tx_timeout = ieee80211_tx_timeout;
+        mdev->get_stats = ieee80211_get_stats;
+        mdev->open = ieee80211_open;
+       mdev->stop = ieee80211_stop;
+       mdev->type = ARPHRD_IEEE80211;
+        mdev->hard_header_parse = header_parse_80211;
+       sprintf(mdev->name, "%s.11", dev->name);
+
+       sdata = IEEE80211_DEV_TO_SUB_IF(mdev);
+        sdata->type = IEEE80211_SUB_IF_TYPE_NORM;
+        sdata->dev = mdev;
+        sdata->master = mdev;
+        sdata->local = local;
+        list_add_tail(&sdata->list, &local->sub_if_list);
+
+        tasklet_init(&local->tasklet,
+                    ieee80211_tasklet_handler,
+                    (unsigned long) local);
+        skb_queue_head_init(&local->skb_queue);
+        skb_queue_head_init(&local->skb_queue_unreliable);
+
+       if (setup)
+               setup(mdev);
+
+       return mdev;
+
+ fail:
+       ieee80211_free_hw(mdev);
+       return NULL;
+}
+
+int ieee80211_register_hw(struct net_device *dev, struct ieee80211_hw *hw)
+{
+        struct ieee80211_local *local = dev->priv;
+       int result;
+
+       if (!hw)
+               return -1;
+
+       if (hw->version != IEEE80211_VERSION) {
+               printk("ieee80211_register_hw - version mismatch: 80211.o "
+                      "version %d, low-level driver version %d\n",
+                      IEEE80211_VERSION, hw->version);
+               return -1;
+       }
+
+       local->conf.mode = IW_MODE_MASTER;
+       local->conf.beacon_int = 1000;
+
+       ieee80211_update_hw(dev, hw);   /* Don't care about the result. */
+
+       sta_info_start(local);
+
+       result = register_netdev(local->wdev);
+       if (result < 0)
+               return -1;
+
+       result = register_netdev(local->apdev);
+       if (result < 0)
+               goto fail_2nd_dev;
+
+       if (hw->fraglist)
+               dev->features |= NETIF_F_FRAGLIST;
+       result = register_netdev(dev);
+       if (result < 0)
+               goto fail_3rd_dev;
+
+       if (rate_control_initialize(local) < 0) {
+               printk(KERN_DEBUG "%s: Failed to initialize rate control "
+                      "algorithm\n", dev->name);
+               goto fail_rate;
+       }
+
+       /* TODO: add rtnl locking around device creation and qdisc install */
+       ieee80211_install_qdisc(dev);
+
+        ieee80211_wep_init(local);
+       ieee80211_proc_init_interface(local);
+       return 0;
+
+fail_rate:
+       unregister_netdev(dev);
+fail_3rd_dev:
+       unregister_netdev(local->apdev);
+fail_2nd_dev:
+       unregister_netdev(local->wdev);
+       sta_info_stop(local);
+       return result;
+}
+
+int ieee80211_update_hw(struct net_device *dev, struct ieee80211_hw *hw)
+{
+        struct ieee80211_local *local = dev->priv;
+
+       local->hw = hw;
+
+       /* Backwards compatibility for low-level drivers that do not set number
+        * of TX queues. */
+       if (hw->queues == 0)
+               hw->queues = 1;
+
+       memcpy(local->apdev->dev_addr, dev->dev_addr, ETH_ALEN);
+       local->apdev->base_addr = dev->base_addr;
+       local->apdev->irq = dev->irq;
+       local->apdev->mem_start = dev->mem_start;
+       local->apdev->mem_end = dev->mem_end;
+
+        memcpy(local->wdev->dev_addr, dev->dev_addr, ETH_ALEN);
+       local->wdev->base_addr = dev->base_addr;
+       local->wdev->irq = dev->irq;
+       local->wdev->mem_start = dev->mem_start;
+       local->wdev->mem_end = dev->mem_end;
+
+       if (!hw->modes || !hw->modes->channels || !hw->modes->rates ||
+           !hw->modes->num_channels || !hw->modes->num_rates)
+               return -1;
+
+       ieee80211_precalc_rates(hw);
+       local->conf.phymode = hw->modes[0].mode;
+       local->curr_rates = hw->modes[0].rates;
+       local->num_curr_rates = hw->modes[0].num_rates;
+       ieee80211_prepare_rates(dev);
+
+       local->conf.freq = local->hw->modes[0].channels[0].freq;
+       local->conf.channel = local->hw->modes[0].channels[0].chan;
+       local->conf.channel_val = local->hw->modes[0].channels[0].val;
+       /* FIXME: Invoke config to allow driver to set the channel. */
+
+       return 0;
+}
+
+void ieee80211_unregister_hw(struct net_device *dev)
+{
+       struct ieee80211_local *local = dev->priv;
+        struct list_head *ptr, *n;
+       int i;
+
+        tasklet_disable(&local->tasklet);
+        /* TODO: skb_queue should be empty here, no need to do anything? */
+
+       if (local->rate_limit)
+               del_timer_sync(&local->rate_limit_timer);
+       if (local->stat_time)
+               del_timer_sync(&local->stat_timer);
+       if (local->scan_timer.data)
+               del_timer_sync(&local->scan_timer);
+       ieee80211_rx_bss_list_deinit(dev);
+
+       list_for_each_safe(ptr, n, &local->sub_if_list) {
+               struct ieee80211_sub_if_data *sdata =
+                       list_entry(ptr, struct ieee80211_sub_if_data, list);
+               ieee80211_if_del(local, sdata, 0);
+       }
+
+       sta_info_stop(local);
+
+       for (i = 0; i < IEEE80211_FRAGMENT_MAX; i++)
+               if (local->fragments[i].skb)
+                       dev_kfree_skb(local->fragments[i].skb);
+
+       for (i = 0; i < NUM_IEEE80211_MODES; i++) {
+               kfree(local->supp_rates[i]);
+               kfree(local->basic_rates[i]);
+        }
+
+       kfree(local->conf.ssid);
+       kfree(local->conf.generic_elem);
+
+       ieee80211_proc_deinit_interface(local);
+
+       skb_queue_purge(&local->skb_queue);
+       skb_queue_purge(&local->skb_queue_unreliable);
+
+       rate_control_free(local);
+}
+
+void ieee80211_free_hw(struct net_device *dev)
+{
+       struct ieee80211_local *local = dev->priv;
+
+       kfree(local->sta_devs);
+       kfree(local->bss_devs);
+       kfree(dev);
+}
+
+/* Perform netif operations on all configured interfaces */
+int ieee80211_netif_oper(struct net_device *sdev, Netif_Oper op)
+{
+        struct ieee80211_local *local = sdev->priv;
+        struct ieee80211_sub_if_data *sdata =  IEEE80211_DEV_TO_SUB_IF(sdev);
+        struct net_device *dev = sdata->master;
+
+        switch (op) {
+        case NETIF_ATTACH:
+            netif_device_attach(dev);
+            break;
+        case NETIF_DETACH:
+            netif_device_detach(dev);
+            break;
+        case NETIF_START:
+            netif_start_queue(dev);
+            break;
+        case NETIF_STOP:
+            break;
+        case NETIF_WAKE:
+               if (local->scan.in_scan == 0) {
+                       netif_wake_queue(dev);
+#if 1
+                       if (/* FIX: 802.11 qdisc in use */ 1)
+                               __netif_schedule(dev);
+#endif
+               }
+            break;
+        case NETIF_IS_STOPPED:
+            if (netif_queue_stopped(dev))
+                return 1;
+            break;
+        case NETIF_UPDATE_TX_START:
+            dev->trans_start = jiffies;
+            break;
+        }
+
+        return 0;
+}
+
+
+void * ieee80211_dev_hw_data(struct net_device *dev)
+{
+       struct ieee80211_local *local = dev->priv;
+       return local->hw_priv;
+}
+
+
+void * ieee80211_dev_stats(struct net_device *dev)
+{
+       struct ieee80211_sub_if_data *sdata;
+       sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+       return &(sdata->stats);
+}
+
+
+int ieee80211_rate_control_register(struct rate_control_ops *ops)
+{
+       struct rate_control_algs *alg;
+
+       alg = kmalloc(sizeof(*alg), GFP_KERNEL);
+       if (alg == NULL) {
+               return -1;
+       }
+       memset(alg, 0, sizeof(*alg));
+       alg->next = ieee80211_rate_ctrl_algs;
+       alg->ops = ops;
+       ieee80211_rate_ctrl_algs = alg;
+
+       return 0;
+}
+
+
+void ieee80211_rate_control_unregister(struct rate_control_ops *ops)
+{
+       struct rate_control_algs *alg, *prev;
+
+       prev = NULL;
+       alg = ieee80211_rate_ctrl_algs;
+       while (alg) {
+               if (alg->ops == ops) {
+                       if (prev)
+                               prev->next = alg->next;
+                       else
+                               ieee80211_rate_ctrl_algs = alg->next;
+                       kfree(alg);
+                       break;
+               }
+               prev = alg;
+               alg = alg->next;
+       }
+}
+
+
+static int rate_control_initialize(struct ieee80211_local *local)
+{
+       struct rate_control_algs *algs;
+       for (algs = ieee80211_rate_ctrl_algs; algs; algs = algs->next) {
+               local->rate_ctrl = algs->ops;
+               local->rate_ctrl_priv = rate_control_alloc(local);
+               if (local->rate_ctrl_priv) {
+                       printk(KERN_DEBUG "%s: Selected rate control "
+                              "algorithm '%s'\n", local->wdev->name,
+                              local->rate_ctrl->name);
+                       return 0;
+               }
+       }
+
+       printk(KERN_WARNING "%s: Failed to select rate control algorithm\n",
+              local->wdev->name);
+       return -1;
+}
+
+
+static int __init ieee80211_init(void)
+{
+       struct sk_buff *skb;
+       if (sizeof(struct ieee80211_tx_packet_data) > (sizeof(skb->cb))) {
+               printk("80211: ieee80211_tx_packet_data is bigger "
+                      "than the skb->cb (%d > %d)\n",
+                      (int) sizeof(struct ieee80211_tx_packet_data),
+                      (int) sizeof(skb->cb));
+               return -EINVAL;
+       }
+       if (sizeof(struct ieee80211_rx_status) > sizeof(skb->cb)) {
+               printk("80211: ieee80211_rx_status is bigger "
+                      "than the skb->cb (%d > %d)\n",
+                      (int) sizeof(struct ieee80211_rx_status),
+                      (int) sizeof(skb->cb));
+               return -EINVAL;
+       }
+
+       ieee80211_proc_init();
+       {
+               int ret = ieee80211_wme_register();
+               if (ret) {
+                       printk(KERN_DEBUG "ieee80211_init: failed to "
+                              "initialize WME (err=%d)\n", ret);
+                       ieee80211_proc_deinit();
+                       return ret;
+               }
+       }
+
+       return 0;
+}
+
+
+static void __exit ieee80211_exit(void)
+{
+       ieee80211_wme_unregister();
+       ieee80211_proc_deinit();
+}
+
+
+EXPORT_SYMBOL(ieee80211_alloc_hw);
+EXPORT_SYMBOL(ieee80211_register_hw);
+EXPORT_SYMBOL(ieee80211_update_hw);
+EXPORT_SYMBOL(ieee80211_unregister_hw);
+EXPORT_SYMBOL(ieee80211_free_hw);
+EXPORT_SYMBOL(ieee80211_rx);
+EXPORT_SYMBOL(ieee80211_tx_status);
+EXPORT_SYMBOL(ieee80211_beacon_get);
+EXPORT_SYMBOL(ieee80211_get_buffered_bc);
+EXPORT_SYMBOL(ieee80211_netif_oper);
+EXPORT_SYMBOL(ieee80211_dev_hw_data);
+EXPORT_SYMBOL(ieee80211_dev_stats);
+EXPORT_SYMBOL(ieee80211_get_hw_conf);
+EXPORT_SYMBOL(ieee80211_set_aid_for_sta);
+EXPORT_SYMBOL(ieee80211_rx_irqsafe);
+EXPORT_SYMBOL(ieee80211_tx_status_irqsafe);
+EXPORT_SYMBOL(ieee80211_get_hdrlen);
+EXPORT_SYMBOL(ieee80211_get_hdrlen_from_skb);
+EXPORT_SYMBOL(ieee80211_rate_control_register);
+EXPORT_SYMBOL(ieee80211_rate_control_unregister);
+EXPORT_SYMBOL(sta_info_get);
+EXPORT_SYMBOL(sta_info_release);
+EXPORT_SYMBOL(ieee80211_radar_status);
+
+module_init(ieee80211_init);
+module_exit(ieee80211_exit);
diff --git a/openwrt/target/linux/package/ieee80211-dscape/src/ieee80211_i.h b/openwrt/target/linux/package/ieee80211-dscape/src/ieee80211_i.h
new file mode 100644 (file)
index 0000000..7127f8d
--- /dev/null
@@ -0,0 +1,571 @@
+/*
+ * Copyright 2002-2005, Instant802 Networks, Inc.
+ * Copyright 2005, Devicescape Software, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef IEEE80211_I_H
+#define IEEE80211_I_H
+
+/* ieee80211.o internal definitions, etc. These are not included into
+ * low-level drivers. */
+
+#ifndef ETH_P_PAE
+#define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
+#endif /* ETH_P_PAE */
+
+#define IEEE80211_MAX_SSID_LEN 32
+
+struct ieee80211_local;
+
+#include "ieee80211_key.h"
+#include "sta_info.h"
+
+#define BIT(x) (1 << (x))
+
+#define IEEE80211_ALIGN32_PAD(a) ((4 - ((a) & 3)) & 3)
+
+
+/* Maximum number of broadcast/multicast frames to buffer when some of the
+ * associated stations are using power saving. */
+#define AP_MAX_BC_BUFFER 128
+
+/* Maximum number of frames buffered to all STAs, including multicast frames.
+ * Note: increasing this limit increases the potential memory requirement. Each
+ * frame can be up to about 2 kB long. */
+#define TOTAL_MAX_TX_BUFFER 512
+
+
+#define MAC2STR(a) ((a)[0] & 0xff), ((a)[1] & 0xff), ((a)[2] & 0xff), \
+                  ((a)[3] & 0xff), ((a)[4] & 0xff), ((a)[5] & 0xff)
+#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
+
+#define MULTICAST_ADDR(a) ((a)[0] & 0x01)
+
+
+/* IEEE 802.11 (Ch. 9.5 Defragmentation) requires support for concurrent
+ * reception of at least three fragmented frames. This limit can be increased
+ * by changing this define, at the cost of slower frame reassembly and
+ * increased memory use (about 2 kB of RAM per entry). */
+#define IEEE80211_FRAGMENT_MAX 4
+
+struct ieee80211_fragment_entry {
+       unsigned long first_frag_time;
+       unsigned int seq;
+       unsigned int rx_queue;
+       unsigned int last_frag;
+       struct sk_buff *skb;
+       int ccmp; /* Whether fragments were encrypted with CCMP */
+       u8 last_pn[6]; /* PN of the last fragment if CCMP was used */
+};
+
+
+struct ieee80211_sta_bss {
+       struct list_head list;
+       struct ieee80211_sta_bss *hnext;
+       atomic_t users;
+
+       u8 bssid[ETH_ALEN];
+       u8 ssid[IEEE80211_MAX_SSID_LEN];
+       size_t ssid_len;
+       u16 capability; /* host byte order */
+       int hw_mode;
+       int channel;
+       int freq;
+       int rssi;
+       u8 *wpa_ie;
+       size_t wpa_ie_len;
+       u8 *rsn_ie;
+       size_t rsn_ie_len;
+       u8 *wmm_ie;
+       size_t wmm_ie_len;
+#define IEEE80211_MAX_SUPP_RATES 32
+       u8 supp_rates[IEEE80211_MAX_SUPP_RATES];
+       size_t supp_rates_len;
+       int beacon_int;
+       u64 timestamp;
+
+       int probe_resp;
+       unsigned long last_update;
+
+};
+
+
+typedef enum {
+       TXRX_CONTINUE, TXRX_DROP, TXRX_QUEUED
+} ieee80211_txrx_result;
+
+struct ieee80211_txrx_data {
+       struct sk_buff *skb;
+       struct net_device *dev;
+        struct ieee80211_local *local;
+        struct ieee80211_sub_if_data *sdata;
+       struct sta_info *sta;
+       u16 fc, ethertype;
+       struct ieee80211_key *key;
+       unsigned int fragmented:1; /* whether the MSDU was fragmented */
+       union {
+               struct {
+                       struct ieee80211_tx_control *control;
+                       int unicast:1;
+                       int ps_buffered:1;
+                       int short_preamble:1;
+                       int probe_last_frag:1;
+                       struct ieee80211_rate *rate;
+                       /* use this rate (if set) for last fragment; rate can
+                        * be set to lower rate for the first fragments, e.g.,
+                        * when using CTS protection with IEEE 802.11g. */
+                       struct ieee80211_rate *last_frag_rate;
+                       int last_frag_rateidx;
+                       int mgmt_interface;
+
+                       /* Extra fragments (in addition to the first fragment
+                        * in skb) */
+                       int num_extra_frag;
+                       struct sk_buff **extra_frag;
+               } tx;
+               struct {
+                       struct ieee80211_rx_status *status;
+                       int sent_ps_buffered;
+                       int queue;
+               } rx;
+       } u;
+#ifdef CONFIG_HOSTAPD_WPA_TESTING
+       int wpa_test;
+#endif /* CONFIG_HOSTAPD_WPA_TESTING */
+};
+
+struct ieee80211_passive_scan {
+        unsigned int in_scan:1; /* this must be cleared before calling
+                                * netif_oper(WAKEUP) */
+        unsigned int our_mode_only:1; /* only scan our physical mode a/b/g/etc
+                                      */
+        int interval; /* time in seconds between scans */
+        int time; /* time in microseconds to scan for */
+       int channel; /* channel to be scanned */
+        int tries;
+
+        int mode_idx;
+        int chan_idx;
+
+       int freq;
+        int rx_packets;
+        int rx_beacon;
+       int txrx_count;
+
+        struct timer_list timer;
+
+        struct sk_buff *skb; /* skb to transmit before changing channels,
+                             * maybe null for none */
+        struct ieee80211_tx_control tx_control;
+
+       unsigned int num_scans;
+};
+
+typedef ieee80211_txrx_result (*ieee80211_tx_handler)
+(struct ieee80211_txrx_data *tx);
+
+typedef ieee80211_txrx_result (*ieee80211_rx_handler)
+(struct ieee80211_txrx_data *rx);
+
+struct ieee80211_if_norm {
+       u8 *beacon_head, *beacon_tail;
+       int beacon_head_len, beacon_tail_len;
+
+       /* TODO: sta_aid could be replaced by 2008-bit large bitfield of
+        * that could be used in TIM element generation. This would also
+        * make TIM element generation a bit faster. */
+       /* AID mapping to station data. NULL, if AID is free. AID is in the
+        * range 1..2007 and sta_aid[i] corresponds to AID i+1. */
+       struct sta_info *sta_aid[MAX_AID_TABLE_SIZE];
+       int max_aid; /* largest aid currently in use */
+       atomic_t num_sta_ps; /* number of stations in PS mode */
+       struct sk_buff_head ps_bc_buf;
+       int dtim_period, dtim_count;
+       int force_unicast_rateidx; /* forced TX rateidx for unicast frames */
+       int max_ratectrl_rateidx; /* max TX rateidx for rate control */
+       int num_beacons; /* number of TXed beacon frames for this BSS */
+};
+
+struct ieee80211_if_wds {
+        u8 remote_addr[ETH_ALEN];
+        struct sta_info *sta;
+};
+
+struct ieee80211_if_vlan {
+        u8 id;
+};
+
+struct ieee80211_if_sta {
+       enum {
+               IEEE80211_DISABLED, IEEE80211_AUTHENTICATE,
+               IEEE80211_ASSOCIATE, IEEE80211_ASSOCIATED,
+               IEEE80211_IBSS_SEARCH, IEEE80211_IBSS_JOINED
+       } state;
+       struct timer_list timer;
+       u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
+       u8 ssid[IEEE80211_MAX_SSID_LEN];
+       size_t ssid_len;
+       u16 aid;
+       u16 ap_capab, capab;
+       u8 *extra_ie; /* to be added to the end of AssocReq */
+       size_t extra_ie_len;
+
+       /* The last AssocReq/Resp IEs */
+       u8 *assocreq_ies, *assocresp_ies;
+       size_t assocreq_ies_len, assocresp_ies_len;
+
+       int auth_tries, assoc_tries;
+
+       int ssid_set:1;
+       int bssid_set:1;
+       int prev_bssid_set:1;
+       int authenticated:1;
+       int associated:1;
+       int probereq_poll:1;
+       int use_protection:1;
+       int create_ibss:1;
+       int mixed_cell:1;
+       int wmm_enabled:1;
+
+       int key_mgmt;
+       unsigned long last_probe;
+
+#define IEEE80211_AUTH_ALG_OPEN BIT(0)
+#define IEEE80211_AUTH_ALG_SHARED_KEY BIT(1)
+#define IEEE80211_AUTH_ALG_LEAP BIT(2)
+       unsigned int auth_algs; /* bitfield of allowed auth algs */
+       int auth_alg; /* currently used IEEE 802.11 authentication algorithm */
+       int auth_transaction;
+
+       unsigned long ibss_join_req;
+       struct sk_buff *probe_resp; /* ProbeResp template for IBSS */
+       u32 supp_rates_bits;
+
+       int wmm_last_param_set;
+};
+
+
+#define IEEE80211_SUB_IF_TYPE_NORM 0x00000000
+#define IEEE80211_SUB_IF_TYPE_MGMT 0x00000001
+#define IEEE80211_SUB_IF_TYPE_STA  0x00000002
+#define IEEE80211_SUB_IF_TYPE_WDS  0x5A580211
+#define IEEE80211_SUB_IF_TYPE_VLAN 0x00080211
+
+struct ieee80211_sub_if_data {
+        struct list_head list;
+        unsigned int type;
+
+        struct net_device *dev;
+        struct net_device *master;
+        struct ieee80211_local *local;
+
+        struct net_device_stats stats;
+       int drop_unencrypted;
+       int eapol; /* 0 = process EAPOL frames as normal data frames,
+                   * 1 = send EAPOL frames through wlan#ap to hostapd
+                   *     (default) */
+       int ieee802_1x; /* IEEE 802.1X PAE - drop packet to/from unauthorized
+                        * port */
+
+#define NUM_DEFAULT_KEYS 4
+        struct ieee80211_key *keys[NUM_DEFAULT_KEYS];
+        struct ieee80211_key *default_key;
+
+       struct ieee80211_if_norm *bss; /* BSS that this device belongs to */
+
+        union {
+               struct ieee80211_if_norm norm;
+                struct ieee80211_if_wds wds;
+                struct ieee80211_if_vlan vlan;
+                struct ieee80211_if_sta sta;
+        } u;
+        int channel_use;
+        int channel_use_raw;
+};
+
+#define IEEE80211_DEV_TO_SUB_IF(dev) ((struct ieee80211_sub_if_data *) \
+                     (((long)dev + sizeof(struct net_device) + 3) & ~3))
+
+
+struct ieee80211_local {
+       struct ieee80211_hw *hw;
+       void *hw_priv;
+       struct net_device *mdev; /* wlan#.11 - "master" 802.11 device */
+        struct net_device *wdev; /* wlan# - default Ethernet (data) devide */
+       struct net_device *apdev; /* wlan#ap - management frames (hostapd) */
+       int open_count;
+       struct ieee80211_conf conf;
+
+       /* Tasklet and skb queue to process calls from IRQ mode. All frames
+        * added to skb_queue will be processed, but frames in
+        * skb_queue_unreliable may be dropped if the total length of these
+        * queues increases over the limit. */
+#define IEEE80211_IRQSAFE_QUEUE_LIMIT 128
+       struct tasklet_struct tasklet;
+       struct sk_buff_head skb_queue;
+       struct sk_buff_head skb_queue_unreliable;
+       enum {
+               ieee80211_rx_msg = 1,
+               ieee80211_tx_status_msg = 2
+       } ieee80211_msg_enum;
+
+        spinlock_t generic_lock;
+       /* Station data structures */
+       spinlock_t sta_lock; /* mutex for STA data structures */
+       int num_sta; /* number of stations in sta_list */
+       struct list_head sta_list;
+       struct sta_info *sta_hash[STA_HASH_SIZE];
+       struct timer_list sta_cleanup;
+
+       /* Current rate table. This is a pointer to hw->modes structure. */
+       struct ieee80211_rate *curr_rates;
+       int num_curr_rates;
+
+       void *rate_ctrl_priv;
+       struct rate_control_ops *rate_ctrl;
+
+       int next_mode; /* MODE_IEEE80211*
+                       * The mode preference for next channel change. This is
+                       * used to select .11g vs. .11b channels (or 4.9 GHz vs.
+                       * .11a) when the channel number is not unique. */
+
+       /* Supported and basic rate filters for different modes. These are
+        * pointers to -1 terminated lists and rates in 100 kbps units. */
+       int *supp_rates[NUM_IEEE80211_MODES];
+       int *basic_rates[NUM_IEEE80211_MODES];
+
+       int rts_threshold;
+       int cts_protect_erp_frames;
+       int fragmentation_threshold;
+       int short_retry_limit; /* dot11ShortRetryLimit */
+       int long_retry_limit; /* dot11LongRetryLimit */
+       int short_preamble; /* use short preamble with IEEE 802.11b */
+
+       u32 wep_iv;
+       int key_tx_rx_threshold; /* number of times any key can be used in TX
+                                 * or RX before generating a rekey
+                                 * notification; 0 = notification disabled. */
+
+       /* Fragment table for host-based reassembly */
+       struct ieee80211_fragment_entry fragments[IEEE80211_FRAGMENT_MAX];
+       unsigned int fragment_next;
+
+       int bridge_packets; /* bridge packets between associated stations and
+                            * deliver multicast frames both back to wireless
+                            * media and to the local net stack */
+
+       struct ieee80211_passive_scan scan;
+
+
+       ieee80211_rx_handler *rx_handlers;
+        ieee80211_tx_handler *tx_handlers;
+
+        spinlock_t sub_if_lock; /* mutex for STA data structures */
+        struct list_head sub_if_list;
+       struct net_device **bss_devs; /* pointer to IF_TYPE_NORM devices for
+                                      * quick access to BSS data */
+       int bss_dev_count; /* number of used entries in bss_devs; note: the
+                           * total size of bss_devs array is stored in
+                           * conf.bss_count */
+       struct net_device **sta_devs; /* pointer to IF_TYPE_STA devices */
+       int sta_dev_count; /* number of used entries in sta_devs */
+       int sta_scanning;
+       int scan_hw_mode_idx;
+       int scan_channel_idx;
+       enum { SCAN_SET_CHANNEL, SCAN_SEND_PROBE } scan_state;
+       unsigned long last_scan_completed;
+       struct timer_list scan_timer;
+       int scan_oper_channel;
+       int scan_oper_channel_val;
+       int scan_oper_power_level;
+       int scan_oper_freq;
+       int scan_oper_phymode;
+       int scan_oper_antenna_max;
+       u8 scan_ssid[IEEE80211_MAX_SSID_LEN];
+       size_t scan_ssid_len;
+       int scan_skip_11b;
+       struct list_head sta_bss_list;
+       struct ieee80211_sta_bss *sta_bss_hash[STA_HASH_SIZE];
+       spinlock_t sta_bss_lock;
+#define IEEE80211_SCAN_MATCH_SSID BIT(0)
+#define IEEE80211_SCAN_WPA_ONLY BIT(1)
+#define IEEE80211_SCAN_EXTRA_INFO BIT(2)
+       int scan_flags;
+
+#ifdef CONFIG_HOSTAPD_WPA_TESTING
+       u32 wpa_trigger;
+#endif /* CONFIG_HOSTAPD_WPA_TESTING */
+        /* SNMP counters */
+        /* dot11CountersTable */
+        u32 dot11TransmittedFragmentCount;
+        u32 dot11MulticastTransmittedFrameCount;
+        u32 dot11FailedCount;
+       u32 dot11RetryCount;
+       u32 dot11MultipleRetryCount;
+       u32 dot11FrameDuplicateCount;
+        u32 dot11ReceivedFragmentCount;
+        u32 dot11MulticastReceivedFrameCount;
+        u32 dot11TransmittedFrameCount;
+        u32 dot11WEPUndecryptableCount;
+
+        int tx_led_counter;
+
+       u32 channel_use;
+       u32 channel_use_raw;
+        u32 stat_time;
+        struct timer_list stat_timer;
+
+       u8 bssid[ETH_ALEN]; /* BSSID for STA modes (Adhoc/Managed) */
+        struct timer_list rate_limit_timer;
+        u32 rate_limit;
+        u32 rate_limit_burst;
+        u32 rate_limit_bucket;
+        struct proc_dir_entry *proc, *proc_sta, *proc_iface;
+
+       struct work_struct sta_proc_add;
+
+       enum {
+               STA_ANTENNA_SEL_AUTO = 0,
+               STA_ANTENNA_SEL_SW_CTRL = 1,
+               STA_ANTENNA_SEL_SW_CTRL_DEBUG = 2
+       } sta_antenna_sel;
+
+       int rate_ctrl_num_up, rate_ctrl_num_down;
+
+#ifdef CONFIG_IEEE80211_DEBUG_COUNTERS
+       /* TX/RX handler statistics */
+       unsigned int tx_handlers_drop;
+       unsigned int tx_handlers_queued;
+       unsigned int tx_handlers_drop_unencrypted;
+       unsigned int tx_handlers_drop_fragment;
+       unsigned int tx_handlers_drop_wep;
+       unsigned int tx_handlers_drop_rate_limit;
+       unsigned int tx_handlers_drop_not_assoc;
+       unsigned int tx_handlers_drop_unauth_port;
+       unsigned int rx_handlers_drop;
+       unsigned int rx_handlers_queued;
+       unsigned int rx_handlers_drop_nullfunc;
+       unsigned int rx_handlers_drop_defrag;
+       unsigned int rx_handlers_drop_short;
+       unsigned int rx_handlers_drop_passive_scan;
+       unsigned int tx_expand_skb_head;
+       unsigned int tx_expand_skb_head_cloned;
+       unsigned int rx_expand_skb_head;
+       unsigned int rx_expand_skb_head2;
+       unsigned int rx_handlers_fragments;
+       unsigned int tx_status_drop;
+       unsigned int wme_rx_queue[NUM_RX_DATA_QUEUES];
+       unsigned int wme_tx_queue[NUM_RX_DATA_QUEUES];
+#define I802_DEBUG_INC(c) (c)++
+#else /* CONFIG_IEEE80211_DEBUG_COUNTERS */
+#define I802_DEBUG_INC(c) do { } while (0)
+#endif /* CONFIG_IEEE80211_DEBUG_COUNTERS */
+
+
+       int default_wep_only; /* only default WEP keys are used with this
+                              * interface; this is used to decide when hwaccel
+                              * can be used with default keys */
+       int total_ps_buffered; /* total number of all buffered unicast and
+                               * multicast packets for power saving stations
+                               */
+       int allow_broadcast_always; /* whether to allow TX of broadcast frames
+                                    * even when there are no associated STAs
+                                    */
+
+       int wifi_wme_noack_test;
+       unsigned int wmm_acm; /* bit field of ACM bits (BIT(802.1D tag)) */
+
+       unsigned int hw_modes; /* bitfield of allowed hardware modes;
+                               * (1 << MODE_*) */
+};
+
+
+/* ieee80211.c */
+int ieee80211_hw_config(struct net_device *dev);
+struct ieee80211_key_conf *
+ieee80211_key_data2conf(struct ieee80211_local *local,
+                       struct ieee80211_key *data);
+void ieee80211_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
+                      struct ieee80211_rx_status *status, u32 msg_type);
+void ieee80211_prepare_rates(struct net_device *dev);
+void ieee80211_tx_set_iswep(struct ieee80211_txrx_data *tx);
+int ieee80211_if_add_wds(struct net_device *dev,
+                         char *name,
+                         struct ieee80211_if_wds *wds, int locked);
+int ieee80211_if_add_vlan(struct net_device *dev, char *name,
+                         struct ieee80211_if_vlan *vlan, int locked);
+int ieee80211_if_add_norm(struct net_device *dev, char *name, u8 *bssid,
+                         int locked);
+
+int ieee80211_if_remove_wds(struct net_device *dev, char *name, int locked);
+int ieee80211_if_remove_vlan(struct net_device *dev, char *name, int locked);
+int ieee80211_if_remove_norm(struct net_device *dev, char *name, int locked);
+int ieee80211_if_flush(struct net_device *dev, int locked);
+int ieee80211_if_update_wds(struct net_device *dev, char *name,
+                           struct ieee80211_if_wds *wds, int locked);
+
+/* ieee80211_ioctl.c */
+int ieee80211_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
+/* Set hw encryption from ieee80211 */
+int ieee80211_set_hw_encryption(struct net_device *dev,
+                               struct sta_info *sta, u8 addr[ETH_ALEN],
+                               struct ieee80211_key *key);
+
+/* ieee80211_scan.c */
+void ieee80211_init_scan(struct net_device *dev);
+void ieee80211_stop_scan(struct net_device *dev);
+
+
+
+/* Least common multiple of the used rates (in 100 kbps). This is used to
+ * calculate rate_inv values for each rate so that only integers are needed. */
+#define CHAN_UTIL_RATE_LCM 95040
+/* 1 usec is 1/8 * (95040/10) = 1188 */
+#define CHAN_UTIL_PER_USEC 1188
+/* Amount of bits to shift the result right to scale the total utilization
+ * to values that will not wrap around 32-bit integers. */
+#define CHAN_UTIL_SHIFT 9
+/* Theoretical maximum of channel utilization counter in 10 ms (stat_time=1):
+ * (CHAN_UTIL_PER_USEC * 10000) >> CHAN_UTIL_SHIFT = 23203. So dividing the
+ * raw value with about 23 should give utilization in 10th of a percentage
+ * (1/1000). However, utilization is only estimated and not all intervals
+ * between frames etc. are calculated. 18 seems to give numbers that are closer
+ * to the real maximum. */
+#define CHAN_UTIL_PER_10MS 18
+#define CHAN_UTIL_HDR_LONG (202 * CHAN_UTIL_PER_USEC)
+#define CHAN_UTIL_HDR_SHORT (40 * CHAN_UTIL_PER_USEC)
+
+
+
+/* ieee80211.c */
+int ieee80211_if_add_sta(struct net_device *dev, char *name, int locked);
+int ieee80211_if_remove_sta(struct net_device *dev, char *name, int locked);
+/* ieee80211_ioctl.c */
+int ieee80211_set_compression(struct ieee80211_local *local,
+                             struct net_device *dev, struct sta_info *sta);
+/* ieee80211_sta.c */
+void ieee80211_sta_timer(unsigned long ptr);
+void ieee80211_sta_rx_mgmt(struct net_device *dev, struct sk_buff *skb,
+                          struct ieee80211_rx_status *rx_status);
+int ieee80211_sta_set_ssid(struct net_device *dev, char *ssid, size_t len);
+int ieee80211_sta_get_ssid(struct net_device *dev, char *ssid, size_t *len);
+int ieee80211_sta_set_bssid(struct net_device *dev, u8 *bssid);
+int ieee80211_sta_req_scan(struct net_device *dev, u8 *ssid, size_t ssid_len);
+int ieee80211_sta_scan_results(struct net_device *dev, char *buf, size_t len);
+void ieee80211_sta_rx_scan(struct net_device *dev, struct sk_buff *skb,
+                          struct ieee80211_rx_status *rx_status);
+void ieee80211_rx_bss_list_init(struct net_device *dev);
+void ieee80211_rx_bss_list_deinit(struct net_device *dev);
+int ieee80211_sta_set_extra_ie(struct net_device *dev, char *ie, size_t len);
+struct sta_info * ieee80211_ibss_add_sta(struct net_device *dev,
+                                        struct sk_buff *skb, u8 *bssid,
+                                        u8 *addr);
+int ieee80211_sta_deauthenticate(struct net_device *dev, u16 reason);
+int ieee80211_sta_disassociate(struct net_device *dev, u16 reason);
+
+
+#endif /* IEEE80211_I_H */
diff --git a/openwrt/target/linux/package/ieee80211-dscape/src/ieee80211_ioctl.c b/openwrt/target/linux/package/ieee80211-dscape/src/ieee80211_ioctl.c
new file mode 100644 (file)
index 0000000..a44b02e
--- /dev/null
@@ -0,0 +1,2832 @@
+/*
+ * Copyright 2002-2005, Instant802 Networks, Inc.
+ * Copyright 2005, Devicescape Software, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/config.h>
+#include <linux/version.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/netdevice.h>
+#include <linux/types.h>
+#include <linux/slab.h>
+#include <linux/skbuff.h>
+#include <linux/if_arp.h>
+#include <linux/wireless.h>
+#include <net/iw_handler.h>
+#include <asm/uaccess.h>
+
+#include <net/ieee80211.h>
+#include <net/ieee80211_mgmt.h>
+#include "ieee80211_i.h"
+#include "hostapd_ioctl.h"
+#include "rate_control.h"
+#include "wpa.h"
+#include "aes_ccm.h"
+
+
+static int ieee80211_regdom = 0x10; /* FCC */
+MODULE_PARM(ieee80211_regdom, "i");
+MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain; 64=MKK");
+
+/*
+ * If firmware is upgraded by the vendor, additional channels can be used based
+ * on the new Japanese regulatory rules. This is indicated by setting
+ * ieee80211_japan_5ghz module parameter to one when loading the 80211 kernel
+ * module.
+ */
+static int ieee80211_japan_5ghz /* = 0 */;
+MODULE_PARM(ieee80211_japan_5ghz, "i");
+MODULE_PARM_DESC(ieee80211_japan_5ghz, "Vendor-updated firmware for 5 GHz");
+
+
+static int ieee80211_ioctl_set_beacon(struct net_device *dev,
+                                     struct prism2_hostapd_param *param,
+                                     int param_len,
+                                     int flag)
+{
+       struct ieee80211_sub_if_data *sdata;
+       struct ieee80211_if_norm     *norm;
+       u8 **b_head, **b_tail;
+       int *b_head_len, *b_tail_len;
+       int len;
+
+       len = ((char *) param->u.beacon.data - (char *) param) +
+               param->u.beacon.head_len + param->u.beacon.tail_len;
+
+       if (param_len > len)
+               param_len = len;
+       else if (param_len != len)
+               return -EINVAL;
+
+       sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+       if (sdata->type != IEEE80211_SUB_IF_TYPE_NORM)
+               return -EINVAL;
+       norm = &sdata->u.norm;
+
+       switch (flag) {
+       case 0:
+               b_head = &norm->beacon_head;
+               b_tail = &norm->beacon_tail;
+               b_head_len = &norm->beacon_head_len;
+               b_tail_len = &norm->beacon_tail_len;
+               break;
+       default:
+               printk(KERN_DEBUG "%s: unknown beacon flag %d\n", 
+                      dev->name, flag);
+               return -EINVAL;
+       }
+
+       kfree(*b_head);
+       kfree(*b_tail);
+       *b_head = NULL;
+       *b_tail = NULL;
+       
+       *b_head_len = param->u.beacon.head_len;
+       *b_tail_len = param->u.beacon.tail_len;
+
+       *b_head = kmalloc(*b_head_len, GFP_KERNEL);
+       if (*b_head)
+               memcpy(*b_head, param->u.beacon.data, *b_head_len);
+       else {
+               printk(KERN_DEBUG "%s: failed to allocate beacon_head\n",
+                      dev->name);
+               return -ENOMEM;
+       }
+
+       if (*b_tail_len > 0) {
+               *b_tail = kmalloc(*b_tail_len, GFP_KERNEL);
+               if (*b_tail)
+                       memcpy(*b_tail, param->u.beacon.data + (*b_head_len),
+                              (*b_tail_len));
+               else {
+                       printk(KERN_DEBUG "%s: failed to allocate "
+                              "beacon_tail\n", dev->name);
+                       return -ENOMEM;
+               }
+       }
+
+       return 0;
+}
+
+
+static int ieee80211_ioctl_get_hw_features(struct net_device *dev,
+                                          struct prism2_hostapd_param *param,
+                                          int param_len)
+{
+       struct ieee80211_local *local = dev->priv;
+       u8 *pos = param->u.hw_features.data;
+       int left = param_len - (pos - (u8 *) param);
+       int mode, i;
+       struct hostapd_ioctl_hw_modes_hdr *hdr;
+       struct ieee80211_rate_data *rate;
+       struct ieee80211_channel_data *chan;
+
+       param->u.hw_features.flags = 0;
+       if (local->hw->data_nullfunc_ack)
+               param->u.hw_features.flags |= HOSTAP_HW_FLAG_NULLFUNC_OK;
+
+       param->u.hw_features.num_modes = local->hw->num_modes;
+       for (mode = 0; mode < local->hw->num_modes; mode++) {
+               int clen, rlen;
+               struct ieee80211_hw_modes *m = &local->hw->modes[mode];
+               clen = m->num_channels * sizeof(struct ieee80211_channel_data);
+               rlen = m->num_rates * sizeof(struct ieee80211_rate_data);
+               if (left < sizeof(*hdr) + clen + rlen)
+                       return -E2BIG;
+               left -= sizeof(*hdr) + clen + rlen;
+
+               hdr = (struct hostapd_ioctl_hw_modes_hdr *) pos;
+               hdr->mode = m->mode;
+               hdr->num_channels = m->num_channels;
+               hdr->num_rates = m->num_rates;
+
+               pos = (u8 *) (hdr + 1);
+               chan = (struct ieee80211_channel_data *) pos;
+               for (i = 0; i < m->num_channels; i++) {
+                       chan[i].chan = m->channels[i].chan;
+                       chan[i].freq = m->channels[i].freq;
+                       chan[i].flag = m->channels[i].flag;
+               }
+               pos += clen;
+
+               rate = (struct ieee80211_rate_data *) pos;
+               for (i = 0; i < m->num_rates; i++) {
+                       rate[i].rate = m->rates[i].rate;
+                       rate[i].flags = m->rates[i].flags;
+               }
+               pos += rlen;
+       }
+
+       return 0;
+}
+
+
+static int ieee80211_ioctl_scan(struct net_device *dev,
+                                struct prism2_hostapd_param *param)
+{
+        struct ieee80211_local *local = dev->priv;
+
+       if (local->hw->passive_scan == NULL)
+               return -EOPNOTSUPP;
+
+        if ((param->u.scan.now == 1) && (local->scan.in_scan == 1))
+                return -EBUSY;                 
+
+        if (param->u.scan.our_mode_only >= 0)
+                local->scan.our_mode_only = param->u.scan.our_mode_only;
+        if (param->u.scan.interval >= 0)
+                local->scan.interval = param->u.scan.interval;
+        if (param->u.scan.listen >= 0)
+                local->scan.time = param->u.scan.listen;
+               if (param->u.scan.channel > 0)
+                       local->scan.channel = param->u.scan.channel;
+        if (param->u.scan.now == 1) {
+                local->scan.in_scan = 0;
+                mod_timer(&local->scan.timer, jiffies);
+        }
+
+        param->u.scan.our_mode_only = local->scan.our_mode_only;
+        param->u.scan.interval = local->scan.interval;
+        param->u.scan.listen = local->scan.time;
+        if (local->scan.in_scan == 1)
+                       param->u.scan.last_rx = -1;
+        else {
+                       param->u.scan.last_rx = local->scan.rx_packets;
+                       local->scan.rx_packets = -1;
+               }
+        param->u.scan.channel = local->hw->modes[local->scan.mode_idx].
+               channels[local->scan.chan_idx].chan;
+
+       return 0;
+}
+
+
+static int ieee80211_ioctl_flush(struct net_device *dev,
+                                struct prism2_hostapd_param *param)
+{
+       struct ieee80211_local *local = dev->priv;
+       sta_info_flush(local, NULL);
+       return 0;
+}
+
+
+
+
+static int ieee80211_ioctl_add_sta(struct net_device *dev,
+                                  struct prism2_hostapd_param *param)
+{
+       struct ieee80211_local *local = dev->priv;
+       struct sta_info *sta;
+       u32 rates;
+       int i, j;
+        struct ieee80211_sub_if_data *sdata;
+       int add_key_entry = 1;
+
+       sta = sta_info_get(local, param->sta_addr);
+
+        if (sta == NULL) {
+               sta = sta_info_add(local, dev, param->sta_addr);
+               if (sta == NULL)
+                       return -ENOMEM;
+        }
+
+       if (sta->dev != dev) {
+               /* Binding STA to a new interface, so remove all references to
+                * the old BSS. */
+               sta_info_remove_aid_ptr(sta);
+       }
+
+        /* TODO
+         * We "steal" the device in case someone owns it
+         * This will hurt WDS links and such when we have a
+         * WDS link and a client associating from the same station
+         */
+        sta->dev = dev;
+        sdata = IEEE80211_DEV_TO_SUB_IF(sta->dev);
+
+       sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
+       sta->aid = param->u.add_sta.aid;
+       if (sta->aid > MAX_AID_TABLE_SIZE)
+               sta->aid = 0;
+       if (sta->aid > 0 && sdata->bss)
+               sdata->bss->sta_aid[sta->aid - 1] = sta;
+       if (sdata->bss && sta->aid > sdata->bss->max_aid)
+               sdata->bss->max_aid = sta->aid;
+
+       rates = 0;
+       for (i = 0; i < sizeof(param->u.add_sta.supp_rates); i++) {
+               int rate = (param->u.add_sta.supp_rates[i] & 0x7f) * 5;
+               if (local->conf.phymode == MODE_ATHEROS_TURBO ||
+                   local->conf.phymode == MODE_ATHEROS_TURBOG)
+                       rate *= 2;
+               for (j = 0; j < local->num_curr_rates; j++) {
+                       if (local->curr_rates[j].rate == rate)
+                               rates |= BIT(j);
+               }
+
+       }
+       sta->supp_rates = rates;
+
+       rate_control_rate_init(local, sta);
+
+
+
+       if (param->u.add_sta.wds_flags & 0x01)
+               sta->flags |= WLAN_STA_WDS;
+       else
+               sta->flags &= ~WLAN_STA_WDS;
+
+       if (add_key_entry && sta->key == NULL && sdata->default_key == NULL &&
+           local->hw->set_key) {
+               struct ieee80211_key_conf conf;
+               /* Add key cache entry with NULL key type because this may used
+                * for TX filtering. */
+               memset(&conf, 0, sizeof(conf));
+               conf.hw_key_idx = HW_KEY_IDX_INVALID;
+               conf.alg = ALG_NULL;
+               conf.force_sw_encrypt = 1;
+               if (local->hw->set_key(dev, SET_KEY, sta->addr, &conf,
+                                      sta->aid)) {
+                       sta->key_idx_compression = HW_KEY_IDX_INVALID;
+               } else {
+                       sta->key_idx_compression = conf.hw_key_idx;
+               }
+       }
+
+       sta_info_release(local, sta);
+
+       return 0;
+}
+
+
+static int ieee80211_ioctl_remove_sta(struct net_device *dev,
+                                     struct prism2_hostapd_param *param)
+{
+       struct ieee80211_local *local = dev->priv;
+       struct sta_info *sta;
+
+       sta = sta_info_get(local, param->sta_addr);
+       if (sta) {
+               sta_info_release(local, sta);
+               sta_info_free(local, sta, 1);
+       }
+
+       return sta ? 0 : -ENOENT;
+}
+
+
+static int ieee80211_ioctl_get_dot11counterstable(struct net_device *dev,
+                                       struct prism2_hostapd_param *param)
+{
+       struct ieee80211_local *local = dev->priv;
+        struct ieee80211_low_level_stats stats;
+
+       memset(&stats, 0, sizeof(stats));
+       if (local->hw->get_stats)
+               local->hw->get_stats(dev, &stats);
+        param->u.dot11CountersTable.dot11TransmittedFragmentCount =
+                local->dot11TransmittedFragmentCount;
+        param->u.dot11CountersTable.dot11MulticastTransmittedFrameCount =
+                local->dot11MulticastTransmittedFrameCount;
+        param->u.dot11CountersTable.dot11ReceivedFragmentCount =
+                local->dot11ReceivedFragmentCount;
+        param->u.dot11CountersTable.dot11MulticastReceivedFrameCount =
+                local->dot11MulticastReceivedFrameCount;
+        param->u.dot11CountersTable.dot11TransmittedFrameCount =
+                local->dot11TransmittedFrameCount;
+        param->u.dot11CountersTable.dot11FCSErrorCount =
+                stats.dot11FCSErrorCount;
+        param->u.dot11CountersTable.dot11ACKFailureCount =
+                stats.dot11ACKFailureCount;
+        param->u.dot11CountersTable.dot11RTSFailureCount =
+                stats.dot11RTSFailureCount;
+        param->u.dot11CountersTable.dot11RTSSuccessCount =
+                stats.dot11RTSSuccessCount;
+
+        return 0;
+}
+
+static int ieee80211_ioctl_get_info_sta(struct net_device *dev,
+                                       struct prism2_hostapd_param *param)
+{
+       struct ieee80211_local *local = dev->priv;
+       struct sta_info *sta;
+
+       if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
+           param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
+           param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
+          struct ieee80211_sub_if_data *sdata;
+          struct net_device_stats *stats;
+
+          sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+          stats = ieee80211_dev_stats(sdata->master);
+          param->u.get_info_sta.rx_bytes = stats->rx_bytes;
+          param->u.get_info_sta.tx_bytes = stats->tx_bytes;
+          /* go through all STAs and get STA with lowest max. rate */
+          param->u.get_info_sta.current_tx_rate =
+                  local->curr_rates[sta_info_min_txrate_get(local)].rate;
+          return 0;
+        }
+
+       sta = sta_info_get(local, param->sta_addr);
+
+       if (!sta)
+               return -ENOENT;
+
+       param->u.get_info_sta.inactive_msec =
+               jiffies_to_msecs(jiffies - sta->last_rx);
+       param->u.get_info_sta.rx_packets = sta->rx_packets;
+       param->u.get_info_sta.tx_packets = sta->tx_packets;
+       param->u.get_info_sta.rx_bytes = sta->rx_bytes;
+       param->u.get_info_sta.tx_bytes = sta->tx_bytes;
+       param->u.get_info_sta.channel_use = sta->channel_use;
+        param->u.get_info_sta.flags = sta->flags;
+       if (sta->txrate >= 0 && sta->txrate < local->num_curr_rates)
+               param->u.get_info_sta.current_tx_rate =
+                       local->curr_rates[sta->txrate].rate;
+       param->u.get_info_sta.num_ps_buf_frames =
+               skb_queue_len(&sta->ps_tx_buf);
+       param->u.get_info_sta.tx_retry_failed = sta->tx_retry_failed;
+       param->u.get_info_sta.tx_retry_count = sta->tx_retry_count;
+       param->u.get_info_sta.last_rssi = sta->last_rssi;
+       param->u.get_info_sta.last_ack_rssi = sta->last_ack_rssi[2];
+
+       sta_info_release(local, sta);
+
+       return 0;
+}
+
+
+static int ieee80211_ioctl_set_flags_sta(struct net_device *dev,
+                                        struct prism2_hostapd_param *param)
+{
+       struct ieee80211_local *local = dev->priv;
+       struct sta_info *sta;
+
+       sta = sta_info_get(local, param->sta_addr);
+       if (sta) {
+               sta->flags |= param->u.set_flags_sta.flags_or;
+               sta->flags &= param->u.set_flags_sta.flags_and;
+               if (local->hw->set_port_auth &&
+                   (param->u.set_flags_sta.flags_or & WLAN_STA_AUTHORIZED) &&
+                   local->hw->set_port_auth(local->mdev, sta->addr, 1))
+                       printk(KERN_DEBUG "%s: failed to set low-level driver "
+                              "PAE state (authorized) for " MACSTR "\n",
+                              dev->name, MAC2STR(sta->addr));
+               if (local->hw->set_port_auth &&
+                   !(param->u.set_flags_sta.flags_and & WLAN_STA_AUTHORIZED)
+                   && local->hw->set_port_auth(local->mdev, sta->addr, 0))
+                       printk(KERN_DEBUG "%s: failed to set low-level driver "
+                              "PAE state (unauthorized) for " MACSTR "\n",
+                              dev->name, MAC2STR(sta->addr));
+               sta_info_release(local, sta);
+       }
+
+       return sta ? 0 : -ENOENT;
+}
+
+
+int ieee80211_set_hw_encryption(struct net_device *dev,
+                               struct sta_info *sta, u8 addr[ETH_ALEN],
+                               struct ieee80211_key *key)
+{
+       struct ieee80211_key_conf *keyconf = NULL;
+       struct ieee80211_local *local = dev->priv;
+       int rc = 0;
+
+       /* default to sw encryption; this will be cleared by low-level
+        * driver if the hw supports requested encryption */
+       if (key)
+               key->force_sw_encrypt = 1;
+
+       if (key && local->hw->set_key &&
+           (!local->conf.sw_encrypt || !local->conf.sw_decrypt) &&
+           (keyconf = ieee80211_key_data2conf(local, key)) != NULL) {
+               if (local->hw->set_key(dev, SET_KEY, addr,
+                                      keyconf, sta ? sta->aid : 0)) {
+                       rc = HOSTAP_CRYPT_ERR_KEY_SET_FAILED;
+                       key->force_sw_encrypt = 1;
+                       key->hw_key_idx = HW_KEY_IDX_INVALID;
+               } else {
+                       key->force_sw_encrypt =
+                               keyconf->force_sw_encrypt;
+                       key->hw_key_idx =
+                               keyconf->hw_key_idx;
+
+               }
+       }
+       kfree(keyconf);
+
+       return rc;
+}
+
+
+static int ieee80211_ioctl_set_encryption(struct net_device *dev,
+                                         struct prism2_hostapd_param *param,
+                                         int param_len)
+{
+       struct ieee80211_local *local = dev->priv;
+       int alg, ret = 0;
+       struct sta_info *sta;
+       struct ieee80211_key **key;
+       int set_tx_key = 0, try_hwaccel = 1;
+        struct ieee80211_key_conf *keyconf;
+        struct ieee80211_sub_if_data *sdata;
+
+        sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+
+       param->u.crypt.err = 0;
+       param->u.crypt.alg[HOSTAP_CRYPT_ALG_NAME_LEN - 1] = '\0';
+
+       if (param_len <
+           (int) ((char *) param->u.crypt.key - (char *) param) +
+           param->u.crypt.key_len) {
+               printk(KERN_DEBUG "%s: set_encrypt - invalid param_lem\n",
+                      dev->name);
+               return -EINVAL;
+        }
+
+       if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
+           param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
+           param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
+               sta = NULL;
+               if (param->u.crypt.idx >= NUM_DEFAULT_KEYS) {
+                       printk(KERN_DEBUG "%s: set_encrypt - invalid idx=%d\n",
+                              dev->name, param->u.crypt.idx);
+                       return -EINVAL;
+               }
+               key = &sdata->keys[param->u.crypt.idx];
+               if (param->u.crypt.flags & HOSTAP_CRYPT_FLAG_SET_TX_KEY)
+                       set_tx_key = 1;
+
+               /* Disable hwaccel for default keys when the interface is not
+                * the default one.
+                * TODO: consider adding hwaccel support for these; at least
+                * Atheros key cache should be able to handle this since AP is
+                * only transmitting frames with default keys. */
+               /* FIX: hw key cache can be used when only one virtual
+                * STA is associated with each AP. If more than one STA
+                * is associated to the same AP, software encryption
+                * must be used. This should be done automatically
+                * based on configured station devices. For the time
+                * being, this can be only set at compile time. */
+               if (sdata->type == IEEE80211_SUB_IF_TYPE_STA) {
+                       if (0 /* FIX: more than one STA per AP */)
+                               try_hwaccel = 0;
+               } else
+               if (sdata->type != IEEE80211_SUB_IF_TYPE_NORM ||
+                   dev != local->wdev)
+                       try_hwaccel = 0;
+       } else {
+               if (param->u.crypt.idx != 0) {
+                       printk(KERN_DEBUG "%s: set_encrypt - non-zero idx for "
+                              "individual key\n", dev->name);
+                       return -EINVAL;
+               }
+
+               sta = sta_info_get(local, param->sta_addr);
+               if (sta == NULL) {
+                       param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
+#ifdef CONFIG_IEEE80211_VERBOSE_DEBUG
+                       printk(KERN_DEBUG "%s: set_encrypt - unknown addr "
+                              MACSTR "\n",
+                              dev->name, MAC2STR(param->sta_addr));
+#endif /* CONFIG_IEEE80211_VERBOSE_DEBUG */
+
+                       return -ENOENT;
+               }
+
+               key = &sta->key;
+       }
+
+       if (strcmp(param->u.crypt.alg, "none") == 0) {
+               alg = ALG_NONE;
+       } else if (strcmp(param->u.crypt.alg, "WEP") == 0) {
+               alg = ALG_WEP;
+       } else if (strcmp(param->u.crypt.alg, "TKIP") == 0) {
+               if (param->u.crypt.key_len != ALG_TKIP_KEY_LEN) {
+                       printk(KERN_DEBUG "%s: set_encrypt - invalid TKIP key "
+                              "length %d\n", dev->name,
+                              param->u.crypt.key_len);
+                       ret = -EINVAL;
+                       goto done;
+               }
+               alg = ALG_TKIP;
+       } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) {
+               if (param->u.crypt.key_len != ALG_CCMP_KEY_LEN) {
+                       printk(KERN_DEBUG "%s: set_encrypt - invalid CCMP key "
+                              "length %d\n", dev->name,
+                              param->u.crypt.key_len);
+                       ret = -EINVAL;
+                       goto done;
+               }
+               alg = ALG_CCMP;
+       } else {
+               param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ALG;
+               printk(KERN_DEBUG "%s: set_encrypt - unknown alg\n",
+                      dev->name);
+               ret = -EINVAL;
+               goto done;
+       }
+
+       /* FIX:
+        * Cannot configure default hwaccel keys with WEP algorithm, if
+        * any of the virtual interfaces is using static WEP
+        * configuration because hwaccel would otherwise try to decrypt
+        * these frames.
+        *
+        * For now, just disable WEP hwaccel for broadcast when there is
+        * possibility of conflict with default keys. This can maybe later be
+        * optimized by using non-default keys (at least with Atheros ar521x).
+        */
+       if (!sta && alg == ALG_WEP && !local->default_wep_only &&
+           local->conf.mode != IW_MODE_ADHOC &&
+           local->conf.mode != IW_MODE_INFRA) {
+               try_hwaccel = 0;
+       }
+
+       if (local->hw->device_hides_wep) {
+               /* Software encryption cannot be used with devices that hide
+                * encryption from the host system, so always try to use
+                * hardware acceleration with such devices. */
+               try_hwaccel = 1;
+       }
+
+       if (local->hw->no_tkip_wmm_hwaccel && alg == ALG_TKIP) {
+               if (sta && (sta->flags & WLAN_STA_WME)) {
+               /* Hardware does not support hwaccel with TKIP when using WMM.
+                */
+                       try_hwaccel = 0;
+               }
+               else if (sdata->type == IEEE80211_SUB_IF_TYPE_STA) {
+                       sta = sta_info_get(local, sdata->u.sta.bssid);
+                       if (sta) {
+                               if (sta->flags & WLAN_STA_WME) {
+                                       try_hwaccel = 0;
+                               }
+                               sta_info_release(local, sta);
+                               sta = NULL;
+                       }
+               }
+       }
+
+
+       if (alg == ALG_NONE) {
+               keyconf = NULL;
+               if (try_hwaccel && *key && local->hw->set_key &&
+                   (keyconf = ieee80211_key_data2conf(local, *key)) != NULL &&
+                   local->hw->set_key(dev, DISABLE_KEY, param->sta_addr,
+                                      keyconf, sta ? sta->aid : 0)) {
+                       param->u.crypt.err = HOSTAP_CRYPT_ERR_KEY_SET_FAILED;
+                       printk(KERN_DEBUG "%s: set_encrypt - low-level disable"
+                              " failed\n", dev->name);
+                       ret = -EINVAL;
+               }
+               kfree(keyconf);
+
+               if (sdata->default_key == *key)
+                       sdata->default_key = NULL;
+               kfree(*key);
+               *key = NULL;
+       } else {
+               if (*key == NULL || (*key)->keylen < param->u.crypt.key_len) {
+                       kfree(*key);
+                       *key = kmalloc(sizeof(struct ieee80211_key) +
+                                      param->u.crypt.key_len, GFP_ATOMIC);
+                       if (*key == NULL) {
+                               ret = -ENOMEM;
+                               goto done;
+                       }
+               }
+               memset(*key, 0, sizeof(struct ieee80211_key) +
+                      param->u.crypt.key_len);
+               /* default to sw encryption; low-level driver sets these if the
+                * requested encryption is supported */
+               (*key)->hw_key_idx = HW_KEY_IDX_INVALID;
+               (*key)->force_sw_encrypt = 1;
+
+               (*key)->alg = alg;
+               (*key)->keyidx = param->u.crypt.idx;
+               (*key)->keylen = param->u.crypt.key_len;
+               memcpy((*key)->key, param->u.crypt.key,
+                      param->u.crypt.key_len);
+               if (set_tx_key)
+                       (*key)->default_tx_key = 1;
+
+               if (alg == ALG_CCMP) {
+                       /* Initialize AES key state here as an optimization
+                        * so that it does not need to be initialized for every
+                        * packet. */
+                       ieee80211_aes_key_setup_encrypt(
+                               (*key)->u.ccmp.aes_state, (*key)->key);
+               }
+
+               if (try_hwaccel &&
+                   (alg == ALG_WEP || alg == ALG_TKIP || alg == ALG_CCMP))
+                       param->u.crypt.err = ieee80211_set_hw_encryption(
+                               dev, sta, param->sta_addr, *key);
+       }
+
+       if (set_tx_key || (sta == NULL && sdata->default_key == NULL)) {
+                sdata->default_key = *key;
+               if (local->hw->set_key_idx &&
+                   local->hw->set_key_idx(dev, param->u.crypt.idx))
+                       printk(KERN_DEBUG "%s: failed to set TX key idx for "
+                              "low-level driver\n", dev->name);
+       }
+
+ done:
+       if (sta)
+               sta_info_release(local, sta);
+
+       return ret;
+}
+
+static int ieee80211_ioctl_get_encryption(struct net_device *dev,
+                                         struct prism2_hostapd_param *param,
+                                         int param_len)
+{
+       struct ieee80211_local *local = dev->priv;
+       int ret = 0;
+       struct sta_info *sta;
+       struct ieee80211_key **key;
+       int max_key_len;
+        struct ieee80211_sub_if_data *sdata;
+       u8 *pos;
+
+        sdata = IEEE80211_DEV_TO_SUB_IF(dev);
+
+       param->u.crypt.err = 0;
+
+       max_key_len = param_len -
+               (int) ((char *) param->u.crypt.key - (char *) param);
+       if (max_key_len < 0)
+               return -EINVAL;
+
+       if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
+           param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
+           param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
+               sta = NULL;
+               if (param->u.crypt.idx > NUM_DEFAULT_KEYS) {
+                       param->u.crypt.idx = sdata->default_key ?
+                               sdata->default_key->keyidx : 0;
+                       return 0;
+               } else
+                       key = &sdata->keys[param->u.crypt.idx];
+       } else {
+               sta = sta_info_get(local, param->sta_addr);
+               if (sta == NULL) {
+                       param->u.crypt.err = HOSTAP_CRYPT_ERR_UNKNOWN_ADDR;
+                       return -EINVAL;
+               }
+
+               key = &sta->key;
+       }
+
+
+       memset(param->u.crypt.seq_counter, 0, HOSTAP_SEQ_COUNTER_SIZE);
+       if (*key == NULL) {
+               memcpy(param->u.crypt.alg, "none", 5);
+               param->u.crypt.key_len = 0;
+               param->u.crypt.idx = 0xff;
+       } else {
+               switch ((*key)->alg) {
+               case ALG_WEP:
+                       memcpy(param->u.crypt.alg, "WEP", 4);
+                       break;
+               case ALG_TKIP:
+               {
+                       u32 iv32;
+                       u16 iv16;
+
+                       memcpy(param->u.crypt.alg, "TKIP", 5);
+                       if (local->hw->get_sequence_counter) {
+                       /* Get transmit counter from low level driver */
+                               if (local->hw->get_sequence_counter(dev,
+                                               param->sta_addr,
+                                               (*key)->keyidx,
+                                               IEEE80211_SEQ_COUNTER_TX,
+                                               &iv32,
+                                               &iv16)) {
+                                       /* Error getting value from device */
+                                       return -EIO;
+                               }
+                       } else {
+                               /* Get it from our own local data */
+                               iv32 = (*key)->u.tkip.iv32;
+                               iv16 = (*key)->u.tkip.iv16;
+                       }
+                       pos = param->u.crypt.seq_counter;
+                       *pos++ = iv16 & 0xff;
+                       *pos++ = (iv16 >> 8) & 0xff;
+                       *pos++ = iv32 & 0xff;
+                       *pos++ = (iv32 >> 8) & 0xff;
+                       *pos++ = (iv32 >> 16) & 0xff;
+                       *pos++ = (iv32 >> 24) & 0xff;
+                       break;
+                       }
+               case ALG_CCMP:
+               {
+                       u8 *pn;
+                       memcpy(param->u.crypt.alg, "CCMP", 5);
+                       pos = param->u.crypt.seq_counter;
+                       pn = (*key)->u.ccmp.tx_pn;
+                       *pos++ = pn[5];
+                       *pos++ = pn[4];
+                       *pos++ = pn[3];
+                       *pos++ = pn[2];
+                       *pos++ = pn[1];
+                       *pos++ = pn[0];
+                       break;
+               }
+               default:
+                       memcpy(param->u.crypt.alg, "unknown", 8);
+                       break;
+               }
+
+               if (max_key_len < (*key)->keylen)
+                       ret = -E2BIG;
+               else {
+                       param->u.crypt.key_len = (*key)->keylen;
+                       memcpy(param->u.crypt.key, (*key)->key,
+                              (*key)->keylen);
+               }
+       }
+
+       if (sta)
+               sta_info_release(local, sta);
+
+       return ret;
+}
+
+
+#ifdef CONFIG_HOSTAPD_WPA_TESTING
+static int ieee80211_ioctl_wpa_trigger(struct net_device *dev,
+                                      struct prism2_hostapd_param *param)
+{
+       struct ieee80211_local *local = dev->priv;
+       struct sta_info *sta;
+
+       if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
+           param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
+           param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
+               local->wpa_trigger = param->u.wpa_trigger.trigger;
+               return 0;
+       }
+
+       sta = sta_info_get(local, param->sta_addr);
+       if (sta == NULL) {
+               printk(KERN_DEBUG "%s: wpa_trigger - unknown addr\n",
+                      dev->name);
+               return -EINVAL;
+       }
+
+       sta->wpa_trigger = param->u.wpa_trigger.trigger;
+
+       sta_info_release(local, sta);
+       return 0;
+}
+#endif /* CONFIG_HOSTAPD_WPA_TESTING */
+
+
+static int ieee80211_ioctl_set_rate_sets(struct net_device *dev,
+                                        struct prism2_hostapd_param *param,
+                                        int param_len)
+{
+       struct ieee80211_local *local = dev->priv;
+       u16 *pos = (u16 *) param->u.set_rate_sets.data;
+       int left = param_len - ((u8 *) pos - (u8 *) param);
+       int i, mode, num_supp, num_basic, *supp, *basic, *prev;
+
+       mode = param->u.set_rate_sets.mode;
+       num_supp = param->u.set_rate_sets.num_supported_rates;
+       num_basic = param->u.set_rate_sets.num_basic_rates;
+
+       if (left < (num_supp + num_basic) * 2) {
+               printk(KERN_WARNING "%s: invalid length in hostapd set rate "
+                      "sets ioctl (%d != %d)\n", dev->name, left,
+                      (num_supp + num_basic) * 2);
+               return -EINVAL;
+       }
+
+       supp = (int *) kmalloc((num_supp + 1) * sizeof(int), GFP_KERNEL);
+       basic = (int *) kmalloc((num_basic + 1) * sizeof(int), GFP_KERNEL);
+
+       if (!supp || !basic) {
+               kfree(supp);
+               kfree(basic);
+               return -ENOMEM;
+       }
+
+       for (i = 0; i < num_supp; i++)
+               supp[i] = *pos++;
+       supp[i] = -1;
+
+       for (i = 0; i < num_basic; i++)
+               basic[i] = *pos++;
+       basic[i] = -1;
+
+       if (num_supp == 0) {
+               kfree(supp);
+               supp = NULL;
+       }
+
+       if (num_basic == 0) {
+               kfree(basic);
+               basic = NULL;
+       }
+
+       prev = local->supp_rates[mode];
+       local->supp_rates[mode] = supp;
+       kfree(prev);
+
+       prev = local->basic_rates[mode];
+       local->basic_rates[mode] = basic;
+       kfree(prev);
+
+       if (mode == local->conf.phymode) {
+               /* TODO: should update STA TX rates and remove STAs if they
+                * do not have any remaining supported rates after the change
+                */
+               ieee80211_prepare_rates(dev);
+       }
+
+       return 0;
+}
+
+
+static int ieee80211_ioctl_add_if(struct net_device *dev,
+                                 struct prism2_hostapd_param *param,
+                                 int param_len)
+{
+       u8 *pos = param->u.if_info.data;
+        int left = param_len - ((u8 *) pos - (u8 *) param);
+
+        if (param->u.if_info.type == HOSTAP_IF_WDS) {
+                struct ieee80211_if_wds iwds;
+                struct hostapd_if_wds *wds =
+                       (struct hostapd_if_wds *) param->u.if_info.data;
+
+                if (left < sizeof(struct ieee80211_if_wds))
+                        return -EPROTO;
+
+               memcpy(iwds.remote_addr, wds->remote_addr, ETH_ALEN);
+
+               return ieee80211_if_add_wds(dev, param->u.if_info.name,
+                                           &iwds, 1);
+       } else if (param->u.if_info.type == HOSTAP_IF_VLAN) {
+               struct hostapd_if_vlan *vlan = (struct hostapd_if_vlan *) pos;
+               struct ieee80211_if_vlan ivlan;
+