1 /* src/p80211/p80211knetdev.c
3 * Linux Kernel net device interface
5 * Copyright (C) 1999 AbsoluteValue Systems, Inc. All Rights Reserved.
6 * --------------------------------------------------------------------
10 * The contents of this file are subject to the Mozilla Public
11 * License Version 1.1 (the "License"); you may not use this file
12 * except in compliance with the License. You may obtain a copy of
13 * the License at http://www.mozilla.org/MPL/
15 * Software distributed under the License is distributed on an "AS
16 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
17 * implied. See the License for the specific language governing
18 * rights and limitations under the License.
20 * Alternatively, the contents of this file may be used under the
21 * terms of the GNU Public License version 2 (the "GPL"), in which
22 * case the provisions of the GPL are applicable instead of the
23 * above. If you wish to allow the use of your version of this file
24 * only under the terms of the GPL and not to allow others to use
25 * your version of this file under the MPL, indicate your decision
26 * by deleting the provisions above and replace them with the notice
27 * and other provisions required by the GPL. If you do not delete
28 * the provisions above, a recipient may use your version of this
29 * file under either the MPL or the GPL.
31 * --------------------------------------------------------------------
33 * Inquiries regarding the linux-wlan Open Source project can be
36 * AbsoluteValue Systems Inc.
38 * http://www.linux-wlan.com
40 * --------------------------------------------------------------------
42 * Portions of the development of this software were funded by
43 * Intersil Corporation as part of PRISM(R) chipset product development.
45 * --------------------------------------------------------------------
47 * The functions required for a Linux network device are defined here.
49 * --------------------------------------------------------------------
53 /*================================================================*/
57 #include <linux/version.h>
59 #include <linux/module.h>
60 #include <linux/kernel.h>
61 #include <linux/sched.h>
62 #include <linux/types.h>
63 #include <linux/skbuff.h>
64 #include <linux/slab.h>
65 #include <linux/proc_fs.h>
66 #include <linux/interrupt.h>
67 #include <linux/netdevice.h>
68 #include <linux/kmod.h>
69 #include <linux/if_arp.h>
70 #include <linux/wireless.h>
71 #include <linux/sockios.h>
72 #include <linux/etherdevice.h>
74 #include <asm/bitops.h>
75 #include <asm/uaccess.h>
76 #include <asm/byteorder.h>
79 #include <linux/ethtool.h>
83 #include <net/iw_handler.h>
85 #include <net/net_namespace.h>
87 /*================================================================*/
88 /* Project Includes */
91 #include "wlan_compat.h"
92 #include "p80211types.h"
93 #include "p80211hdr.h"
94 #include "p80211conv.h"
95 #include "p80211mgmt.h"
96 #include "p80211msg.h"
97 #include "p80211netdev.h"
98 #include "p80211ioctl.h"
99 #include "p80211req.h"
100 #include "p80211metastruct.h"
101 #include "p80211metadef.h"
103 /*================================================================*/
104 /* Local Constants */
106 /*================================================================*/
110 /*================================================================*/
113 /*================================================================*/
114 /* Local Static Definitions */
116 #define __NO_VERSION__ /* prevent the static definition */
118 #ifdef CONFIG_PROC_FS
119 static struct proc_dir_entry *proc_p80211;
122 /*================================================================*/
123 /* Local Function Declarations */
125 /* Support functions */
126 static void p80211netdev_rx_bh(unsigned long arg);
128 /* netdevice method functions */
129 static int p80211knetdev_init( netdevice_t *netdev);
130 static struct net_device_stats* p80211knetdev_get_stats(netdevice_t *netdev);
131 static int p80211knetdev_open( netdevice_t *netdev);
132 static int p80211knetdev_stop( netdevice_t *netdev );
133 static int p80211knetdev_hard_start_xmit( struct sk_buff *skb, netdevice_t *netdev);
134 static void p80211knetdev_set_multicast_list(netdevice_t *dev);
135 static int p80211knetdev_do_ioctl(netdevice_t *dev, struct ifreq *ifr, int cmd);
136 static int p80211knetdev_set_mac_address(netdevice_t *dev, void *addr);
137 static void p80211knetdev_tx_timeout(netdevice_t *netdev);
138 static int p80211_rx_typedrop( wlandevice_t *wlandev, UINT16 fc);
140 #ifdef CONFIG_PROC_FS
142 p80211netdev_proc_read(
151 /*================================================================*/
152 /* Function Definitions */
154 /*----------------------------------------------------------------
155 * p80211knetdev_startup
157 * Initialize the wlandevice/netdevice part of 802.11 services at
165 ----------------------------------------------------------------*/
166 void p80211netdev_startup(void)
170 #ifdef CONFIG_PROC_FS
171 if (init_net.proc_net != NULL) {
172 proc_p80211 = create_proc_entry(
174 (S_IFDIR|S_IRUGO|S_IXUGO),
182 /*----------------------------------------------------------------
183 * p80211knetdev_shutdown
185 * Shutdown the wlandevice/netdevice part of 802.11 services at
193 ----------------------------------------------------------------*/
195 p80211netdev_shutdown(void)
198 #ifdef CONFIG_PROC_FS
199 if (proc_p80211 != NULL) {
200 remove_proc_entry("p80211", init_net.proc_net);
206 /*----------------------------------------------------------------
209 * Init method for a Linux netdevice. Called in response to
217 ----------------------------------------------------------------*/
218 static int p80211knetdev_init( netdevice_t *netdev)
221 /* Called in response to register_netdev */
222 /* This is usually the probe function, but the probe has */
223 /* already been done by the MSD and the create_kdev */
224 /* function. All we do here is return success */
230 /*----------------------------------------------------------------
231 * p80211knetdev_get_stats
233 * Statistics retrieval for linux netdevices. Here we're reporting
234 * the Linux i/f level statistics. Hence, for the primary numbers,
235 * we don't want to report the numbers from the MIB. Eventually,
236 * it might be useful to collect some of the error counters though.
239 * netdev Linux netdevice
242 * the address of the statistics structure
243 ----------------------------------------------------------------*/
244 static struct net_device_stats*
245 p80211knetdev_get_stats(netdevice_t *netdev)
247 wlandevice_t *wlandev = (wlandevice_t*)netdev->priv;
250 /* TODO: review the MIB stats for items that correspond to
254 return &(wlandev->linux_stats);
258 /*----------------------------------------------------------------
261 * Linux netdevice open method. Following a successful call here,
262 * the device is supposed to be ready for tx and rx. In our
263 * situation that may not be entirely true due to the state of the
267 * netdev Linux network device structure
270 * zero on success, non-zero otherwise
271 ----------------------------------------------------------------*/
272 static int p80211knetdev_open( netdevice_t *netdev )
274 int result = 0; /* success */
275 wlandevice_t *wlandev = (wlandevice_t*)(netdev->priv);
279 /* Check to make sure the MSD is running */
280 if ( wlandev->msdstate != WLAN_MSD_RUNNING ) {
284 /* Tell the MSD to open */
285 if ( wlandev->open != NULL) {
286 result = wlandev->open(wlandev);
288 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,3,43) )
289 netdev->interrupt = 0;
291 p80211netdev_start_queue(wlandev);
292 wlandev->state = WLAN_DEVICE_OPEN;
303 /*----------------------------------------------------------------
306 * Linux netdevice stop (close) method. Following this call,
307 * no frames should go up or down through this interface.
310 * netdev Linux network device structure
313 * zero on success, non-zero otherwise
314 ----------------------------------------------------------------*/
315 static int p80211knetdev_stop( netdevice_t *netdev )
318 wlandevice_t *wlandev = (wlandevice_t*)(netdev->priv);
322 if ( wlandev->close != NULL ) {
323 result = wlandev->close(wlandev);
326 p80211netdev_stop_queue(wlandev);
327 wlandev->state = WLAN_DEVICE_CLOSED;
333 /*----------------------------------------------------------------
336 * Frame receive function called by the mac specific driver.
339 * wlandev WLAN network device structure
340 * skb skbuff containing a full 802.11 frame.
345 ----------------------------------------------------------------*/
347 p80211netdev_rx(wlandevice_t *wlandev, struct sk_buff *skb )
351 /* Enqueue for post-irq processing */
352 skb_queue_tail(&wlandev->nsd_rxq, skb);
354 tasklet_schedule(&wlandev->rx_bh);
360 /*----------------------------------------------------------------
363 * Deferred processing of all received frames.
366 * wlandev WLAN network device structure
367 * skb skbuff containing a full 802.11 frame.
372 ----------------------------------------------------------------*/
373 static void p80211netdev_rx_bh(unsigned long arg)
375 wlandevice_t *wlandev = (wlandevice_t *) arg;
376 struct sk_buff *skb = NULL;
377 netdevice_t *dev = wlandev->netdev;
378 p80211_hdr_a3_t *hdr;
383 /* Let's empty our our queue */
384 while ( (skb = skb_dequeue(&wlandev->nsd_rxq)) ) {
385 if (wlandev->state == WLAN_DEVICE_OPEN) {
387 if (dev->type != ARPHRD_ETHER) {
388 /* RAW frame; we shouldn't convert it */
389 // XXX Append the Prism Header here instead.
391 /* set up various data fields */
393 skb_reset_mac_header(skb);
394 skb->ip_summed = CHECKSUM_NONE;
395 skb->pkt_type = PACKET_OTHERHOST;
396 skb->protocol = htons(ETH_P_80211_RAW);
397 dev->last_rx = jiffies;
399 wlandev->linux_stats.rx_packets++;
400 wlandev->linux_stats.rx_bytes += skb->len;
404 hdr = (p80211_hdr_a3_t *)skb->data;
405 fc = ieee2host16(hdr->fc);
406 if (p80211_rx_typedrop(wlandev, fc)) {
411 /* perform mcast filtering */
412 if (wlandev->netdev->flags & IFF_ALLMULTI) {
413 /* allow my local address through */
414 if (memcmp(hdr->a1, wlandev->netdev->dev_addr, WLAN_ADDR_LEN) != 0) {
415 /* but reject anything else that isn't multicast */
416 if (!(hdr->a1[0] & 0x01)) {
423 if ( skb_p80211_to_ether(wlandev, wlandev->ethconv, skb) == 0 ) {
424 skb->dev->last_rx = jiffies;
425 wlandev->linux_stats.rx_packets++;
426 wlandev->linux_stats.rx_bytes += skb->len;
430 WLAN_LOG_DEBUG(1, "p80211_to_ether failed.\n");
440 /*----------------------------------------------------------------
441 * p80211knetdev_hard_start_xmit
443 * Linux netdevice method for transmitting a frame.
446 * skb Linux sk_buff containing the frame.
447 * netdev Linux netdevice.
450 * If the lower layers report that buffers are full. netdev->tbusy
451 * will be set to prevent higher layers from sending more traffic.
453 * Note: If this function returns non-zero, higher layers retain
454 * ownership of the skb.
457 * zero on success, non-zero on failure.
458 ----------------------------------------------------------------*/
459 static int p80211knetdev_hard_start_xmit( struct sk_buff *skb, netdevice_t *netdev)
463 wlandevice_t *wlandev = (wlandevice_t*)netdev->priv;
464 p80211_hdr_t p80211_hdr;
465 p80211_metawep_t p80211_wep;
473 if (wlandev->state != WLAN_DEVICE_OPEN) {
478 memset(&p80211_hdr, 0, sizeof(p80211_hdr_t));
479 memset(&p80211_wep, 0, sizeof(p80211_metawep_t));
481 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,38) )
482 if ( test_and_set_bit(0, (void*)&(netdev->tbusy)) != 0 ) {
483 /* We've been called w/ tbusy set, has the tx */
485 WLAN_LOG_DEBUG(1, "called when tbusy set\n");
490 if ( netif_queue_stopped(netdev) ) {
491 WLAN_LOG_DEBUG(1, "called when queue stopped.\n");
496 netif_stop_queue(netdev);
498 /* No timeout handling here, 2.3.38+ kernels call the
499 * timeout function directly.
500 * TODO: Add timeout handling.
504 /* Check to see that a valid mode is set */
505 switch( wlandev->macmode ) {
506 case WLAN_MACMODE_IBSS_STA:
507 case WLAN_MACMODE_ESS_STA:
508 case WLAN_MACMODE_ESS_AP:
511 /* Mode isn't set yet, just drop the frame
512 * and return success .
513 * TODO: we need a saner way to handle this
515 if(skb->protocol != ETH_P_80211_RAW) {
516 p80211netdev_start_queue(wlandev);
518 "Tx attempt prior to association, frame dropped.\n");
519 wlandev->linux_stats.tx_dropped++;
526 /* Check for raw transmits */
527 if(skb->protocol == ETH_P_80211_RAW) {
528 if (!capable(CAP_NET_ADMIN)) {
532 /* move the header over */
533 memcpy(&p80211_hdr, skb->data, sizeof(p80211_hdr_t));
534 skb_pull(skb, sizeof(p80211_hdr_t));
536 if ( skb_ether_to_p80211(wlandev, wlandev->ethconv, skb, &p80211_hdr, &p80211_wep) != 0 ) {
538 WLAN_LOG_DEBUG(1, "ether_to_80211(%d) failed.\n",
544 if ( wlandev->txframe == NULL ) {
549 netdev->trans_start = jiffies;
551 wlandev->linux_stats.tx_packets++;
552 /* count only the packet payload */
553 wlandev->linux_stats.tx_bytes += skb->len;
555 txresult = wlandev->txframe(wlandev, skb, &p80211_hdr, &p80211_wep);
557 if ( txresult == 0) {
558 /* success and more buf */
559 /* avail, re: hw_txdata */
560 p80211netdev_wake_queue(wlandev);
562 } else if ( txresult == 1 ) {
563 /* success, no more avail */
564 WLAN_LOG_DEBUG(3, "txframe success, no more bufs\n");
565 /* netdev->tbusy = 1; don't set here, irqhdlr */
566 /* may have already cleared it */
568 } else if ( txresult == 2 ) {
569 /* alloc failure, drop frame */
570 WLAN_LOG_DEBUG(3, "txframe returned alloc_fail\n");
573 /* buffer full or queue busy, drop frame. */
574 WLAN_LOG_DEBUG(3, "txframe returned full or busy\n");
579 /* Free up the WEP buffer if it's not the same as the skb */
580 if ((p80211_wep.data) && (p80211_wep.data != skb->data))
581 kfree(p80211_wep.data);
583 /* we always free the skb here, never in a lower level. */
592 /*----------------------------------------------------------------
593 * p80211knetdev_set_multicast_list
595 * Called from higher lavers whenever there's a need to set/clear
596 * promiscuous mode or rewrite the multicast list.
603 ----------------------------------------------------------------*/
604 static void p80211knetdev_set_multicast_list(netdevice_t *dev)
606 wlandevice_t *wlandev = (wlandevice_t*)dev->priv;
610 /* TODO: real multicast support as well */
612 if (wlandev->set_multicast_list)
613 wlandev->set_multicast_list(wlandev, dev);
620 static int p80211netdev_ethtool(wlandevice_t *wlandev, void __user *useraddr)
623 struct ethtool_drvinfo info;
624 struct ethtool_value edata;
626 memset(&info, 0, sizeof(info));
627 memset(&edata, 0, sizeof(edata));
629 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
633 case ETHTOOL_GDRVINFO:
635 snprintf(info.driver, sizeof(info.driver), "p80211_%s",
637 snprintf(info.version, sizeof(info.version), "%s",
643 if (copy_to_user(useraddr, &info, sizeof(info)))
650 if (wlandev->linkstatus &&
651 (wlandev->macmode != WLAN_MACMODE_NONE)) {
657 if (copy_to_user(useraddr, &edata, sizeof(edata)))
668 /*----------------------------------------------------------------
669 * p80211knetdev_do_ioctl
671 * Handle an ioctl call on one of our devices. Everything Linux
672 * ioctl specific is done here. Then we pass the contents of the
673 * ifr->data to the request message handler.
676 * dev Linux kernel netdevice
677 * ifr Our private ioctl request structure, typed for the
678 * generic struct ifreq so we can use ptr to func
682 * zero on success, a negative errno on failure. Possible values:
683 * -ENETDOWN Device isn't up.
684 * -EBUSY cmd already in progress
685 * -ETIME p80211 cmd timed out (MSD may have its own timers)
686 * -EFAULT memory fault copying msg from user buffer
687 * -ENOMEM unable to allocate kernel msg buffer
688 * -ENOSYS bad magic, it the cmd really for us?
689 * -EINTR sleeping on cmd, awakened by signal, cmd cancelled.
692 * Process thread (ioctl caller). TODO: SMP support may require
694 ----------------------------------------------------------------*/
695 static int p80211knetdev_do_ioctl(netdevice_t *dev, struct ifreq *ifr, int cmd)
698 p80211ioctl_req_t *req = (p80211ioctl_req_t*)ifr;
699 wlandevice_t *wlandev = (wlandevice_t*)dev->priv;
703 WLAN_LOG_DEBUG(2, "rx'd ioctl, cmd=%d, len=%d\n", cmd, req->len);
705 #if WIRELESS_EXT < 13
706 /* Is this a wireless extensions ioctl? */
707 if ((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST)) {
708 if ((result = p80211wext_support_ioctl(dev, ifr, cmd))
716 if (cmd == SIOCETHTOOL) {
717 result = p80211netdev_ethtool(wlandev, (void __user *) ifr->ifr_data);
722 /* Test the magic, assume ifr is good if it's there */
723 if ( req->magic != P80211_IOCTL_MAGIC ) {
728 if ( cmd == P80211_IFTEST ) {
731 } else if ( cmd != P80211_IFREQ ) {
736 /* Allocate a buf of size req->len */
737 if ((msgbuf = kmalloc( req->len, GFP_KERNEL))) {
738 if ( copy_from_user( msgbuf, (void __user *) req->data, req->len) ) {
741 result = p80211req_dorequest( wlandev, msgbuf);
745 if ( copy_to_user( (void __user *) req->data, msgbuf, req->len)) {
756 return result; /* If allocate,copyfrom or copyto fails, return errno */
759 /*----------------------------------------------------------------
760 * p80211knetdev_set_mac_address
762 * Handles the ioctl for changing the MACAddress of a netdevice
764 * references: linux/netdevice.h and drivers/net/net_init.c
766 * NOTE: [MSM] We only prevent address changes when the netdev is
767 * up. We don't control anything based on dot11 state. If the
768 * address is changed on a STA that's currently associated, you
769 * will probably lose the ability to send and receive data frames.
770 * Just be aware. Therefore, this should usually only be done
771 * prior to scan/join/auth/assoc.
774 * dev netdevice struct
775 * addr the new MACAddress (a struct)
778 * zero on success, a negative errno on failure. Possible values:
779 * -EBUSY device is bussy (cmd not possible)
780 * -and errors returned by: p80211req_dorequest(..)
782 * by: Collin R. Mulliner <collin@mulliner.org>
783 ----------------------------------------------------------------*/
784 static int p80211knetdev_set_mac_address(netdevice_t *dev, void *addr)
786 struct sockaddr *new_addr = addr;
787 p80211msg_dot11req_mibset_t dot11req;
788 p80211item_unk392_t *mibattr;
789 p80211item_pstr6_t *macaddr;
790 p80211item_uint32_t *resultcode;
794 /* If we're running, we don't allow MAC address changes */
795 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,38) )
800 if (netif_running(dev)) {
805 /* Set up some convenience pointers. */
806 mibattr = &dot11req.mibattribute;
807 macaddr = (p80211item_pstr6_t*)&mibattr->data;
808 resultcode = &dot11req.resultcode;
810 /* Set up a dot11req_mibset */
811 memset(&dot11req, 0, sizeof(p80211msg_dot11req_mibset_t));
812 dot11req.msgcode = DIDmsg_dot11req_mibset;
813 dot11req.msglen = sizeof(p80211msg_dot11req_mibset_t);
814 memcpy(dot11req.devname,
815 ((wlandevice_t*)(dev->priv))->name,
816 WLAN_DEVNAMELEN_MAX - 1);
818 /* Set up the mibattribute argument */
819 mibattr->did = DIDmsg_dot11req_mibset_mibattribute;
820 mibattr->status = P80211ENUM_msgitem_status_data_ok;
821 mibattr->len = sizeof(mibattr->data);
823 macaddr->did = DIDmib_dot11mac_dot11OperationTable_dot11MACAddress;
824 macaddr->status = P80211ENUM_msgitem_status_data_ok;
825 macaddr->len = sizeof(macaddr->data);
826 macaddr->data.len = WLAN_ADDR_LEN;
827 memcpy(&macaddr->data.data, new_addr->sa_data, WLAN_ADDR_LEN);
829 /* Set up the resultcode argument */
830 resultcode->did = DIDmsg_dot11req_mibset_resultcode;
831 resultcode->status = P80211ENUM_msgitem_status_no_value;
832 resultcode->len = sizeof(resultcode->data);
833 resultcode->data = 0;
835 /* now fire the request */
836 result = p80211req_dorequest(dev->priv, (UINT8*)&dot11req);
838 /* If the request wasn't successful, report an error and don't
839 * change the netdev address
841 if ( result != 0 || resultcode->data != P80211ENUM_resultcode_success) {
843 "Low-level driver failed dot11req_mibset(dot11MACAddress).\n");
844 result = -EADDRNOTAVAIL;
846 /* everything's ok, change the addr in netdev */
847 memcpy(dev->dev_addr, new_addr->sa_data, dev->addr_len);
854 static int wlan_change_mtu(netdevice_t *dev, int new_mtu)
857 // 2312 is max 802.11 payload, 20 is overhead, (ether + llc +snap)
858 // and another 8 for wep.
859 if ( (new_mtu < 68) || (new_mtu > (2312 - 20 - 8)))
871 /*----------------------------------------------------------------
874 * Roughly matches the functionality of ether_setup. Here
875 * we set up any members of the wlandevice structure that are common
876 * to all devices. Additionally, we allocate a linux 'struct device'
877 * and perform the same setup as ether_setup.
879 * Note: It's important that the caller have setup the wlandev->name
880 * ptr prior to calling this function.
883 * wlandev ptr to the wlandev structure for the
886 * zero on success, non-zero otherwise.
888 * Should be process thread. We'll assume it might be
889 * interrupt though. When we add support for statically
890 * compiled drivers, this function will be called in the
891 * context of the kernel startup code.
892 ----------------------------------------------------------------*/
893 int wlan_setup(wlandevice_t *wlandev)
900 /* Set up the wlandev */
901 wlandev->state = WLAN_DEVICE_CLOSED;
902 wlandev->ethconv = WLAN_ETHCONV_8021h;
903 wlandev->macmode = WLAN_MACMODE_NONE;
905 /* Set up the rx queue */
906 skb_queue_head_init(&wlandev->nsd_rxq);
907 tasklet_init(&wlandev->rx_bh,
909 (unsigned long)wlandev);
911 /* Allocate and initialize the struct device */
912 dev = kmalloc(sizeof(netdevice_t), GFP_ATOMIC);
914 WLAN_LOG_ERROR("Failed to alloc netdev.\n");
917 memset( dev, 0, sizeof(netdevice_t));
919 wlandev->netdev = dev;
921 dev->hard_start_xmit = p80211knetdev_hard_start_xmit;
922 dev->get_stats = p80211knetdev_get_stats;
923 #ifdef HAVE_PRIVATE_IOCTL
924 dev->do_ioctl = p80211knetdev_do_ioctl;
926 #ifdef HAVE_MULTICAST
927 dev->set_multicast_list = p80211knetdev_set_multicast_list;
929 dev->init = p80211knetdev_init;
930 dev->open = p80211knetdev_open;
931 dev->stop = p80211knetdev_stop;
933 #ifdef CONFIG_NET_WIRELESS
934 #if ((WIRELESS_EXT < 17) && (WIRELESS_EXT < 21))
935 dev->get_wireless_stats = p80211wext_get_wireless_stats;
937 #if WIRELESS_EXT > 12
938 dev->wireless_handlers = &p80211wext_handler_def;
942 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,3,38) )
946 netif_stop_queue(dev);
948 #ifdef HAVE_CHANGE_MTU
949 dev->change_mtu = wlan_change_mtu;
951 #ifdef HAVE_SET_MAC_ADDR
952 dev->set_mac_address = p80211knetdev_set_mac_address;
954 #ifdef HAVE_TX_TIMEOUT
955 dev->tx_timeout = &p80211knetdev_tx_timeout;
956 dev->watchdog_timeo = (wlan_watchdog * HZ) / 1000;
958 netif_carrier_off(dev);
965 /*----------------------------------------------------------------
968 * This function is paired with the wlan_setup routine. It should
969 * be called after unregister_wlandev. Basically, all it does is
970 * free the 'struct device' that's associated with the wlandev.
971 * We do it here because the 'struct device' isn't allocated
972 * explicitly in the driver code, it's done in wlan_setup. To
973 * do the free in the driver might seem like 'magic'.
976 * wlandev ptr to the wlandev structure for the
979 * zero on success, non-zero otherwise.
981 * Should be process thread. We'll assume it might be
982 * interrupt though. When we add support for statically
983 * compiled drivers, this function will be called in the
984 * context of the kernel startup code.
985 ----------------------------------------------------------------*/
986 int wlan_unsetup(wlandevice_t *wlandev)
992 tasklet_kill(&wlandev->rx_bh);
994 if (wlandev->netdev == NULL ) {
995 WLAN_LOG_ERROR("called without wlandev->netdev set.\n");
998 free_netdev(wlandev->netdev);
999 wlandev->netdev = NULL;
1008 /*----------------------------------------------------------------
1011 * Roughly matches the functionality of register_netdev. This function
1012 * is called after the driver has successfully probed and set up the
1013 * resources for the device. It's now ready to become a named device
1014 * in the Linux system.
1016 * First we allocate a name for the device (if not already set), then
1017 * we call the Linux function register_netdevice.
1020 * wlandev ptr to the wlandev structure for the
1023 * zero on success, non-zero otherwise.
1025 * Can be either interrupt or not.
1026 ----------------------------------------------------------------*/
1027 int register_wlandev(wlandevice_t *wlandev)
1030 netdevice_t *dev = wlandev->netdev;
1034 i = dev_alloc_name(wlandev->netdev, "wlan%d");
1036 i = register_netdev(wlandev->netdev);
1042 #if ( LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0) )
1043 dev->name = wlandev->name;
1045 strcpy(wlandev->name, dev->name);
1048 #ifdef CONFIG_PROC_FS
1050 wlandev->procdir = proc_mkdir(wlandev->name, proc_p80211);
1051 if ( wlandev->procdir )
1052 wlandev->procwlandev =
1053 create_proc_read_entry("wlandev", 0,
1055 p80211netdev_proc_read,
1057 if (wlandev->nsd_proc_read)
1058 create_proc_read_entry("nsd", 0,
1060 wlandev->nsd_proc_read,
1065 #ifdef CONFIG_HOTPLUG
1066 p80211_run_sbin_hotplug(wlandev, WLAN_HOTPLUG_REGISTER);
1074 /*----------------------------------------------------------------
1075 * unregister_wlandev
1077 * Roughly matches the functionality of unregister_netdev. This
1078 * function is called to remove a named device from the system.
1080 * First we tell linux that the device should no longer exist.
1081 * Then we remove it from the list of known wlan devices.
1084 * wlandev ptr to the wlandev structure for the
1087 * zero on success, non-zero otherwise.
1089 * Can be either interrupt or not.
1090 ----------------------------------------------------------------*/
1091 int unregister_wlandev(wlandevice_t *wlandev)
1093 struct sk_buff *skb;
1097 #ifdef CONFIG_HOTPLUG
1098 p80211_run_sbin_hotplug(wlandev, WLAN_HOTPLUG_REMOVE);
1101 #ifdef CONFIG_PROC_FS
1102 if ( wlandev->procwlandev ) {
1103 remove_proc_entry("wlandev", wlandev->procdir);
1105 if ( wlandev->nsd_proc_read ) {
1106 remove_proc_entry("nsd", wlandev->procdir);
1108 if (wlandev->procdir) {
1109 remove_proc_entry(wlandev->name, proc_p80211);
1113 unregister_netdev(wlandev->netdev);
1115 /* Now to clean out the rx queue */
1116 while ( (skb = skb_dequeue(&wlandev->nsd_rxq)) ) {
1124 #ifdef CONFIG_PROC_FS
1125 /*----------------------------------------------------------------
1128 * Read function for /proc/net/p80211/<device>/wlandev
1138 * zero on success, non-zero otherwise.
1140 * Can be either interrupt or not.
1141 ----------------------------------------------------------------*/
1143 p80211netdev_proc_read(
1152 wlandevice_t *wlandev = (wlandevice_t *) data;
1160 p += sprintf(p, "p80211 version: %s (%s)\n\n",
1161 WLAN_RELEASE, WLAN_BUILD_DATE);
1162 p += sprintf(p, "name : %s\n", wlandev->name);
1163 p += sprintf(p, "nsd name : %s\n", wlandev->nsdname);
1164 p += sprintf(p, "address : %02x:%02x:%02x:%02x:%02x:%02x\n",
1165 wlandev->netdev->dev_addr[0], wlandev->netdev->dev_addr[1], wlandev->netdev->dev_addr[2],
1166 wlandev->netdev->dev_addr[3], wlandev->netdev->dev_addr[4], wlandev->netdev->dev_addr[5]);
1167 p += sprintf(p, "nsd caps : %s%s%s%s%s%s%s%s%s%s\n",
1168 (wlandev->nsdcaps & P80211_NSDCAP_HARDWAREWEP) ? "wep_hw " : "",
1169 (wlandev->nsdcaps & P80211_NSDCAP_TIEDWEP) ? "wep_tied " : "",
1170 (wlandev->nsdcaps & P80211_NSDCAP_NOHOSTWEP) ? "wep_hw_only " : "",
1171 (wlandev->nsdcaps & P80211_NSDCAP_PBCC) ? "pbcc " : "",
1172 (wlandev->nsdcaps & P80211_NSDCAP_SHORT_PREAMBLE) ? "short_preamble " : "",
1173 (wlandev->nsdcaps & P80211_NSDCAP_AGILITY) ? "agility " : "",
1174 (wlandev->nsdcaps & P80211_NSDCAP_AP_RETRANSMIT) ? "ap_retransmit " : "",
1175 (wlandev->nsdcaps & P80211_NSDCAP_HWFRAGMENT) ? "hw_frag " : "",
1176 (wlandev->nsdcaps & P80211_NSDCAP_AUTOJOIN) ? "autojoin " : "",
1177 (wlandev->nsdcaps & P80211_NSDCAP_NOSCAN) ? "" : "scan ");
1180 p += sprintf(p, "bssid : %02x:%02x:%02x:%02x:%02x:%02x\n",
1181 wlandev->bssid[0], wlandev->bssid[1], wlandev->bssid[2],
1182 wlandev->bssid[3], wlandev->bssid[4], wlandev->bssid[5]);
1184 p += sprintf(p, "Enabled : %s%s\n",
1185 (wlandev->shortpreamble) ? "short_preamble " : "",
1186 (wlandev->hostwep & HOSTWEP_PRIVACYINVOKED) ? "privacy" : "");
1195 /*----------------------------------------------------------------
1196 * p80211netdev_hwremoved
1198 * Hardware removed notification. This function should be called
1199 * immediately after an MSD has detected that the underlying hardware
1200 * has been yanked out from under us. The primary things we need
1202 * - Mark the wlandev
1203 * - Prevent any further traffic from the knetdev i/f
1204 * - Prevent any further requests from mgmt i/f
1205 * - If there are any waitq'd mgmt requests or mgmt-frame exchanges,
1207 * - Call the MSD hwremoved function.
1209 * The remainder of the cleanup will be handled by unregister().
1210 * Our primary goal here is to prevent as much tickling of the MSD
1211 * as possible since the MSD is already in a 'wounded' state.
1213 * TODO: As new features are added, this function should be
1217 * wlandev WLAN network device structure
1223 * Usually interrupt.
1224 ----------------------------------------------------------------*/
1225 void p80211netdev_hwremoved(wlandevice_t *wlandev)
1228 wlandev->hwremoved = 1;
1229 if ( wlandev->state == WLAN_DEVICE_OPEN) {
1230 p80211netdev_stop_queue(wlandev);
1233 netif_device_detach(wlandev->netdev);
1239 /*----------------------------------------------------------------
1240 * p80211_rx_typedrop
1242 * Classifies the frame, increments the appropriate counter, and
1243 * returns 0|1|2 indicating whether the driver should handle, ignore, or
1247 * wlandev wlan device structure
1248 * fc frame control field
1251 * zero if the frame should be handled by the driver,
1252 * one if the frame should be ignored
1253 * anything else means we drop it.
1259 ----------------------------------------------------------------*/
1260 static int p80211_rx_typedrop( wlandevice_t *wlandev, UINT16 fc)
1265 /* Classify frame, increment counter */
1266 ftype = WLAN_GET_FC_FTYPE(fc);
1267 fstype = WLAN_GET_FC_FSTYPE(fc);
1270 "rx_typedrop : ftype=%d fstype=%d.\n", ftype, fstype);
1273 case WLAN_FTYPE_MGMT:
1274 if ((wlandev->netdev->flags & IFF_PROMISC) ||
1275 (wlandev->netdev->flags & IFF_ALLMULTI)) {
1279 WLAN_LOG_DEBUG(3, "rx'd mgmt:\n");
1282 case WLAN_FSTYPE_ASSOCREQ:
1283 /* printk("assocreq"); */
1284 wlandev->rx.assocreq++;
1286 case WLAN_FSTYPE_ASSOCRESP:
1287 /* printk("assocresp"); */
1288 wlandev->rx.assocresp++;
1290 case WLAN_FSTYPE_REASSOCREQ:
1291 /* printk("reassocreq"); */
1292 wlandev->rx.reassocreq++;
1294 case WLAN_FSTYPE_REASSOCRESP:
1295 /* printk("reassocresp"); */
1296 wlandev->rx.reassocresp++;
1298 case WLAN_FSTYPE_PROBEREQ:
1299 /* printk("probereq"); */
1300 wlandev->rx.probereq++;
1302 case WLAN_FSTYPE_PROBERESP:
1303 /* printk("proberesp"); */
1304 wlandev->rx.proberesp++;
1306 case WLAN_FSTYPE_BEACON:
1307 /* printk("beacon"); */
1308 wlandev->rx.beacon++;
1310 case WLAN_FSTYPE_ATIM:
1311 /* printk("atim"); */
1314 case WLAN_FSTYPE_DISASSOC:
1315 /* printk("disassoc"); */
1316 wlandev->rx.disassoc++;
1318 case WLAN_FSTYPE_AUTHEN:
1319 /* printk("authen"); */
1320 wlandev->rx.authen++;
1322 case WLAN_FSTYPE_DEAUTHEN:
1323 /* printk("deauthen"); */
1324 wlandev->rx.deauthen++;
1327 /* printk("unknown"); */
1328 wlandev->rx.mgmt_unknown++;
1335 case WLAN_FTYPE_CTL:
1336 if ((wlandev->netdev->flags & IFF_PROMISC) ||
1337 (wlandev->netdev->flags & IFF_ALLMULTI)) {
1341 WLAN_LOG_DEBUG(3, "rx'd ctl:\n");
1344 case WLAN_FSTYPE_PSPOLL:
1345 /* printk("pspoll"); */
1346 wlandev->rx.pspoll++;
1348 case WLAN_FSTYPE_RTS:
1349 /* printk("rts"); */
1352 case WLAN_FSTYPE_CTS:
1353 /* printk("cts"); */
1356 case WLAN_FSTYPE_ACK:
1357 /* printk("ack"); */
1360 case WLAN_FSTYPE_CFEND:
1361 /* printk("cfend"); */
1362 wlandev->rx.cfend++;
1364 case WLAN_FSTYPE_CFENDCFACK:
1365 /* printk("cfendcfack"); */
1366 wlandev->rx.cfendcfack++;
1369 /* printk("unknown"); */
1370 wlandev->rx.ctl_unknown++;
1377 case WLAN_FTYPE_DATA:
1380 case WLAN_FSTYPE_DATAONLY:
1381 wlandev->rx.dataonly++;
1383 case WLAN_FSTYPE_DATA_CFACK:
1384 wlandev->rx.data_cfack++;
1386 case WLAN_FSTYPE_DATA_CFPOLL:
1387 wlandev->rx.data_cfpoll++;
1389 case WLAN_FSTYPE_DATA_CFACK_CFPOLL:
1390 wlandev->rx.data__cfack_cfpoll++;
1392 case WLAN_FSTYPE_NULL:
1393 WLAN_LOG_DEBUG(3, "rx'd data:null\n");
1396 case WLAN_FSTYPE_CFACK:
1397 WLAN_LOG_DEBUG(3, "rx'd data:cfack\n");
1398 wlandev->rx.cfack++;
1400 case WLAN_FSTYPE_CFPOLL:
1401 WLAN_LOG_DEBUG(3, "rx'd data:cfpoll\n");
1402 wlandev->rx.cfpoll++;
1404 case WLAN_FSTYPE_CFACK_CFPOLL:
1405 WLAN_LOG_DEBUG(3, "rx'd data:cfack_cfpoll\n");
1406 wlandev->rx.cfack_cfpoll++;
1409 /* printk("unknown"); */
1410 wlandev->rx.data_unknown++;
1419 #ifdef CONFIG_HOTPLUG
1420 /* Notify userspace when a netdevice event occurs,
1421 * by running '/sbin/hotplug net' with certain
1422 * environment variables set.
1424 int p80211_run_sbin_hotplug(wlandevice_t *wlandev, char *action)
1426 char *argv[3], *envp[7], ifname[12 + IFNAMSIZ], action_str[32];
1427 char nsdname[32], wlan_wext[32];
1431 sprintf(ifname, "INTERFACE=%s", wlandev->name);
1432 sprintf(nsdname, "NSDNAME=%s", wlandev->nsdname);
1434 sprintf(ifname, "INTERFACE=null");
1435 sprintf(nsdname, "NSDNAME=null");
1438 sprintf(wlan_wext, "WLAN_WEXT=%s", wlan_wext_write ? "y" : "");
1439 sprintf(action_str, "ACTION=%s", action);
1442 argv[i++] = hotplug_path;
1447 /* minimal command environment */
1448 envp [i++] = "HOME=/";
1449 envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
1450 envp [i++] = ifname;
1451 envp [i++] = action_str;
1452 envp [i++] = nsdname;
1453 envp [i++] = wlan_wext;
1456 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,62))
1457 return call_usermodehelper(argv [0], argv, envp);
1459 return call_usermodehelper(argv [0], argv, envp, 0);
1466 void p80211_suspend(wlandevice_t *wlandev)
1470 #ifdef CONFIG_HOTPLUG
1471 p80211_run_sbin_hotplug(wlandev, WLAN_HOTPLUG_SUSPEND);
1477 void p80211_resume(wlandevice_t *wlandev)
1481 #ifdef CONFIG_HOTPLUG
1482 p80211_run_sbin_hotplug(wlandev, WLAN_HOTPLUG_RESUME);
1488 static void p80211knetdev_tx_timeout( netdevice_t *netdev)
1490 wlandevice_t *wlandev = (wlandevice_t*)netdev->priv;
1493 if (wlandev->tx_timeout) {
1494 wlandev->tx_timeout(wlandev);
1496 WLAN_LOG_WARNING("Implement tx_timeout for %s\n",
1498 p80211netdev_wake_queue(wlandev);