Staging: wlan-ng: Use generic byteorder macros
[safe/jmp/linux-2.6] / drivers / staging / wlan-ng / p80211netdev.c
1 /* src/p80211/p80211knetdev.c
2 *
3 * Linux Kernel net device interface
4 *
5 * Copyright (C) 1999 AbsoluteValue Systems, Inc.  All Rights Reserved.
6 * --------------------------------------------------------------------
7 *
8 * linux-wlan
9 *
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/
14 *
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.
19 *
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.
30 *
31 * --------------------------------------------------------------------
32 *
33 * Inquiries regarding the linux-wlan Open Source project can be
34 * made directly to:
35 *
36 * AbsoluteValue Systems Inc.
37 * info@linux-wlan.com
38 * http://www.linux-wlan.com
39 *
40 * --------------------------------------------------------------------
41 *
42 * Portions of the development of this software were funded by
43 * Intersil Corporation as part of PRISM(R) chipset product development.
44 *
45 * --------------------------------------------------------------------
46 *
47 * The functions required for a Linux network device are defined here.
48 *
49 * --------------------------------------------------------------------
50 */
51
52
53 /*================================================================*/
54 /* System Includes */
55
56
57 #include <linux/version.h>
58
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>
73 #include <linux/if_ether.h>
74 #include <linux/byteorder/generic.h>
75
76 #include <asm/bitops.h>
77 #include <asm/uaccess.h>
78 #include <asm/byteorder.h>
79
80 #ifdef SIOCETHTOOL
81 #include <linux/ethtool.h>
82 #endif
83
84 #include <net/iw_handler.h>
85 #include <net/net_namespace.h>
86
87 /*================================================================*/
88 /* Project Includes */
89
90 #include "wlan_compat.h"
91 #include "p80211types.h"
92 #include "p80211hdr.h"
93 #include "p80211conv.h"
94 #include "p80211mgmt.h"
95 #include "p80211msg.h"
96 #include "p80211netdev.h"
97 #include "p80211ioctl.h"
98 #include "p80211req.h"
99 #include "p80211metastruct.h"
100 #include "p80211metadef.h"
101
102 /*================================================================*/
103 /* Local Constants */
104
105 /*================================================================*/
106 /* Local Macros */
107
108
109 /*================================================================*/
110 /* Local Types */
111
112 /*================================================================*/
113 /* Local Function Declarations */
114
115 /* Support functions */
116 static void p80211netdev_rx_bh(unsigned long arg);
117
118 /* netdevice method functions */
119 static int p80211knetdev_init( netdevice_t *netdev);
120 static struct net_device_stats* p80211knetdev_get_stats(netdevice_t *netdev);
121 static int p80211knetdev_open( netdevice_t *netdev);
122 static int p80211knetdev_stop( netdevice_t *netdev );
123 static int p80211knetdev_hard_start_xmit( struct sk_buff *skb, netdevice_t *netdev);
124 static void p80211knetdev_set_multicast_list(netdevice_t *dev);
125 static int p80211knetdev_do_ioctl(netdevice_t *dev, struct ifreq *ifr, int cmd);
126 static int p80211knetdev_set_mac_address(netdevice_t *dev, void *addr);
127 static void p80211knetdev_tx_timeout(netdevice_t *netdev);
128 static int p80211_rx_typedrop( wlandevice_t *wlandev, u16 fc);
129
130 int wlan_watchdog = 5000;
131 module_param(wlan_watchdog, int, 0644);
132 MODULE_PARM_DESC(wlan_watchdog, "transmit timeout in milliseconds");
133
134 int wlan_wext_write = 1;
135 module_param(wlan_wext_write, int, 0644);
136 MODULE_PARM_DESC(wlan_wext_write, "enable write wireless extensions");
137
138 #ifdef WLAN_INCLUDE_DEBUG
139 int wlan_debug=0;
140 module_param(wlan_debug, int, 0644);
141 MODULE_PARM_DESC(wlan_debug, "p80211 debug level");
142 #endif
143
144 /*================================================================*/
145 /* Function Definitions */
146
147 /*----------------------------------------------------------------
148 * p80211knetdev_init
149 *
150 * Init method for a Linux netdevice.  Called in response to
151 * register_netdev.
152 *
153 * Arguments:
154 *       none
155 *
156 * Returns:
157 *       nothing
158 ----------------------------------------------------------------*/
159 static int p80211knetdev_init( netdevice_t *netdev)
160 {
161         /* Called in response to register_netdev */
162         /* This is usually the probe function, but the probe has */
163         /* already been done by the MSD and the create_kdev */
164         /* function.  All we do here is return success */
165         return 0;
166 }
167
168
169 /*----------------------------------------------------------------
170 * p80211knetdev_get_stats
171 *
172 * Statistics retrieval for linux netdevices.  Here we're reporting
173 * the Linux i/f level statistics.  Hence, for the primary numbers,
174 * we don't want to report the numbers from the MIB.  Eventually,
175 * it might be useful to collect some of the error counters though.
176 *
177 * Arguments:
178 *       netdev          Linux netdevice
179 *
180 * Returns:
181 *       the address of the statistics structure
182 ----------------------------------------------------------------*/
183 static struct net_device_stats*
184 p80211knetdev_get_stats(netdevice_t *netdev)
185 {
186         wlandevice_t    *wlandev = netdev->ml_priv;
187
188         /* TODO: review the MIB stats for items that correspond to
189                 linux stats */
190
191         return &(wlandev->linux_stats);
192 }
193
194
195 /*----------------------------------------------------------------
196 * p80211knetdev_open
197 *
198 * Linux netdevice open method.  Following a successful call here,
199 * the device is supposed to be ready for tx and rx.  In our
200 * situation that may not be entirely true due to the state of the
201 * MAC below.
202 *
203 * Arguments:
204 *       netdev          Linux network device structure
205 *
206 * Returns:
207 *       zero on success, non-zero otherwise
208 ----------------------------------------------------------------*/
209 static int p80211knetdev_open( netdevice_t *netdev )
210 {
211         int             result = 0; /* success */
212         wlandevice_t    *wlandev = netdev->ml_priv;
213
214         /* Check to make sure the MSD is running */
215         if ( wlandev->msdstate != WLAN_MSD_RUNNING ) {
216                 return -ENODEV;
217         }
218
219         /* Tell the MSD to open */
220         if ( wlandev->open != NULL) {
221                 result = wlandev->open(wlandev);
222                 if ( result == 0 ) {
223                         netif_start_queue(wlandev->netdev);
224                         wlandev->state = WLAN_DEVICE_OPEN;
225                 }
226         } else {
227                 result = -EAGAIN;
228         }
229
230         return result;
231 }
232
233
234 /*----------------------------------------------------------------
235 * p80211knetdev_stop
236 *
237 * Linux netdevice stop (close) method.  Following this call,
238 * no frames should go up or down through this interface.
239 *
240 * Arguments:
241 *       netdev          Linux network device structure
242 *
243 * Returns:
244 *       zero on success, non-zero otherwise
245 ----------------------------------------------------------------*/
246 static int p80211knetdev_stop( netdevice_t *netdev )
247 {
248         int             result = 0;
249         wlandevice_t    *wlandev = netdev->ml_priv;
250
251         if ( wlandev->close != NULL ) {
252                 result = wlandev->close(wlandev);
253         }
254
255         netif_stop_queue(wlandev->netdev);
256         wlandev->state = WLAN_DEVICE_CLOSED;
257
258         return result;
259 }
260
261 /*----------------------------------------------------------------
262 * p80211netdev_rx
263 *
264 * Frame receive function called by the mac specific driver.
265 *
266 * Arguments:
267 *       wlandev         WLAN network device structure
268 *       skb             skbuff containing a full 802.11 frame.
269 * Returns:
270 *       nothing
271 * Side effects:
272 *
273 ----------------------------------------------------------------*/
274 void
275 p80211netdev_rx(wlandevice_t *wlandev, struct sk_buff *skb )
276 {
277         /* Enqueue for post-irq processing */
278         skb_queue_tail(&wlandev->nsd_rxq, skb);
279
280         tasklet_schedule(&wlandev->rx_bh);
281
282         return;
283 }
284
285 /*----------------------------------------------------------------
286 * p80211netdev_rx_bh
287 *
288 * Deferred processing of all received frames.
289 *
290 * Arguments:
291 *       wlandev         WLAN network device structure
292 *       skb             skbuff containing a full 802.11 frame.
293 * Returns:
294 *       nothing
295 * Side effects:
296 *
297 ----------------------------------------------------------------*/
298 static void p80211netdev_rx_bh(unsigned long arg)
299 {
300         wlandevice_t *wlandev = (wlandevice_t *) arg;
301         struct sk_buff *skb = NULL;
302         netdevice_t     *dev = wlandev->netdev;
303         p80211_hdr_a3_t *hdr;
304         u16 fc;
305
306         /* Let's empty our our queue */
307         while ( (skb = skb_dequeue(&wlandev->nsd_rxq)) ) {
308                 if (wlandev->state == WLAN_DEVICE_OPEN) {
309
310                         if (dev->type != ARPHRD_ETHER) {
311                                 /* RAW frame; we shouldn't convert it */
312                                 // XXX Append the Prism Header here instead.
313
314                                 /* set up various data fields */
315                                 skb->dev = dev;
316                                 skb_reset_mac_header(skb);
317                                 skb->ip_summed = CHECKSUM_NONE;
318                                 skb->pkt_type = PACKET_OTHERHOST;
319                                 skb->protocol = htons(ETH_P_80211_RAW);
320                                 dev->last_rx = jiffies;
321
322                                 wlandev->linux_stats.rx_packets++;
323                                 wlandev->linux_stats.rx_bytes += skb->len;
324                                 netif_rx_ni(skb);
325                                 continue;
326                         } else {
327                                 hdr = (p80211_hdr_a3_t *)skb->data;
328                                 fc = le16_to_cpu(hdr->fc);
329                                 if (p80211_rx_typedrop(wlandev, fc)) {
330                                         dev_kfree_skb(skb);
331                                         continue;
332                                 }
333
334                                 /* perform mcast filtering */
335                                 if (wlandev->netdev->flags & IFF_ALLMULTI) {
336                                         /* allow my local address through */
337                                         if (memcmp(hdr->a1, wlandev->netdev->dev_addr, ETH_ALEN) != 0) {
338                                                 /* but reject anything else that isn't multicast */
339                                                 if (!(hdr->a1[0] & 0x01)) {
340                                                         dev_kfree_skb(skb);
341                                                         continue;
342                                                 }
343                                         }
344                                 }
345
346                                 if ( skb_p80211_to_ether(wlandev, wlandev->ethconv, skb) == 0 ) {
347                                         skb->dev->last_rx = jiffies;
348                                         wlandev->linux_stats.rx_packets++;
349                                         wlandev->linux_stats.rx_bytes += skb->len;
350                                         netif_rx_ni(skb);
351                                         continue;
352                                 }
353                                 WLAN_LOG_DEBUG(1, "p80211_to_ether failed.\n");
354                         }
355                 }
356                 dev_kfree_skb(skb);
357         }
358 }
359
360
361 /*----------------------------------------------------------------
362 * p80211knetdev_hard_start_xmit
363 *
364 * Linux netdevice method for transmitting a frame.
365 *
366 * Arguments:
367 *       skb     Linux sk_buff containing the frame.
368 *       netdev  Linux netdevice.
369 *
370 * Side effects:
371 *       If the lower layers report that buffers are full. netdev->tbusy
372 *       will be set to prevent higher layers from sending more traffic.
373 *
374 *       Note: If this function returns non-zero, higher layers retain
375 *             ownership of the skb.
376 *
377 * Returns:
378 *       zero on success, non-zero on failure.
379 ----------------------------------------------------------------*/
380 static int p80211knetdev_hard_start_xmit( struct sk_buff *skb, netdevice_t *netdev)
381 {
382         int             result = 0;
383         int             txresult = -1;
384         wlandevice_t    *wlandev = netdev->ml_priv;
385         p80211_hdr_t    p80211_hdr;
386         p80211_metawep_t p80211_wep;
387
388         if (skb == NULL) {
389                 return 0;
390         }
391
392         if (wlandev->state != WLAN_DEVICE_OPEN) {
393                 result = 1;
394                 goto failed;
395         }
396
397         memset(&p80211_hdr, 0, sizeof(p80211_hdr_t));
398         memset(&p80211_wep, 0, sizeof(p80211_metawep_t));
399
400         if ( netif_queue_stopped(netdev) ) {
401                 WLAN_LOG_DEBUG(1, "called when queue stopped.\n");
402                 result = 1;
403                 goto failed;
404         }
405
406         netif_stop_queue(netdev);
407
408         /* Check to see that a valid mode is set */
409         switch( wlandev->macmode ) {
410         case WLAN_MACMODE_IBSS_STA:
411         case WLAN_MACMODE_ESS_STA:
412         case WLAN_MACMODE_ESS_AP:
413                 break;
414         default:
415                 /* Mode isn't set yet, just drop the frame
416                  * and return success .
417                  * TODO: we need a saner way to handle this
418                  */
419                 if(skb->protocol != ETH_P_80211_RAW) {
420                         netif_start_queue(wlandev->netdev);
421                         WLAN_LOG_NOTICE(
422                                 "Tx attempt prior to association, frame dropped.\n");
423                         wlandev->linux_stats.tx_dropped++;
424                         result = 0;
425                         goto failed;
426                 }
427                 break;
428         }
429
430         /* Check for raw transmits */
431         if(skb->protocol == ETH_P_80211_RAW) {
432                 if (!capable(CAP_NET_ADMIN)) {
433                         result = 1;
434                         goto failed;
435                 }
436                 /* move the header over */
437                 memcpy(&p80211_hdr, skb->data, sizeof(p80211_hdr_t));
438                 skb_pull(skb, sizeof(p80211_hdr_t));
439         } else {
440                 if ( skb_ether_to_p80211(wlandev, wlandev->ethconv, skb, &p80211_hdr, &p80211_wep) != 0 ) {
441                         /* convert failed */
442                         WLAN_LOG_DEBUG(1, "ether_to_80211(%d) failed.\n",
443                                         wlandev->ethconv);
444                         result = 1;
445                         goto failed;
446                 }
447         }
448         if ( wlandev->txframe == NULL ) {
449                 result = 1;
450                 goto failed;
451         }
452
453         netdev->trans_start = jiffies;
454
455         wlandev->linux_stats.tx_packets++;
456         /* count only the packet payload */
457         wlandev->linux_stats.tx_bytes += skb->len;
458
459         txresult = wlandev->txframe(wlandev, skb, &p80211_hdr, &p80211_wep);
460
461         if ( txresult == 0) {
462                 /* success and more buf */
463                 /* avail, re: hw_txdata */
464                 netif_wake_queue(wlandev->netdev);
465                 result = 0;
466         } else if ( txresult == 1 ) {
467                 /* success, no more avail */
468                 WLAN_LOG_DEBUG(3, "txframe success, no more bufs\n");
469                 /* netdev->tbusy = 1;  don't set here, irqhdlr */
470                 /*   may have already cleared it */
471                 result = 0;
472         } else if ( txresult == 2 ) {
473                 /* alloc failure, drop frame */
474                 WLAN_LOG_DEBUG(3, "txframe returned alloc_fail\n");
475                 result = 1;
476         } else {
477                 /* buffer full or queue busy, drop frame. */
478                 WLAN_LOG_DEBUG(3, "txframe returned full or busy\n");
479                 result = 1;
480         }
481
482  failed:
483         /* Free up the WEP buffer if it's not the same as the skb */
484         if ((p80211_wep.data) && (p80211_wep.data != skb->data))
485                 kfree(p80211_wep.data);
486
487         /* we always free the skb here, never in a lower level. */
488         if (!result)
489                 dev_kfree_skb(skb);
490
491         return result;
492 }
493
494
495 /*----------------------------------------------------------------
496 * p80211knetdev_set_multicast_list
497 *
498 * Called from higher lavers whenever there's a need to set/clear
499 * promiscuous mode or rewrite the multicast list.
500 *
501 * Arguments:
502 *       none
503 *
504 * Returns:
505 *       nothing
506 ----------------------------------------------------------------*/
507 static void p80211knetdev_set_multicast_list(netdevice_t *dev)
508 {
509         wlandevice_t    *wlandev = dev->ml_priv;
510
511         /* TODO:  real multicast support as well */
512
513         if (wlandev->set_multicast_list)
514                 wlandev->set_multicast_list(wlandev, dev);
515
516 }
517
518 #ifdef SIOCETHTOOL
519
520 static int p80211netdev_ethtool(wlandevice_t *wlandev, void __user *useraddr)
521 {
522         u32 ethcmd;
523         struct ethtool_drvinfo info;
524         struct ethtool_value edata;
525
526         memset(&info, 0, sizeof(info));
527         memset(&edata, 0, sizeof(edata));
528
529         if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
530                 return -EFAULT;
531
532         switch (ethcmd) {
533         case ETHTOOL_GDRVINFO:
534                 info.cmd = ethcmd;
535                 snprintf(info.driver, sizeof(info.driver), "p80211_%s",
536                          wlandev->nsdname);
537                 snprintf(info.version, sizeof(info.version), "%s",
538                          WLAN_RELEASE);
539
540                 // info.fw_version
541                 // info.bus_info
542
543                 if (copy_to_user(useraddr, &info, sizeof(info)))
544                         return -EFAULT;
545                 return 0;
546 #ifdef ETHTOOL_GLINK
547         case ETHTOOL_GLINK:
548                 edata.cmd = ethcmd;
549
550                 if (wlandev->linkstatus &&
551                     (wlandev->macmode != WLAN_MACMODE_NONE)) {
552                         edata.data = 1;
553                 } else {
554                         edata.data = 0;
555                 }
556
557                 if (copy_to_user(useraddr, &edata, sizeof(edata)))
558                         return -EFAULT;
559                 return 0;
560         }
561 #endif
562
563         return -EOPNOTSUPP;
564 }
565
566 #endif
567
568 /*----------------------------------------------------------------
569 * p80211knetdev_do_ioctl
570 *
571 * Handle an ioctl call on one of our devices.  Everything Linux
572 * ioctl specific is done here.  Then we pass the contents of the
573 * ifr->data to the request message handler.
574 *
575 * Arguments:
576 *       dev     Linux kernel netdevice
577 *       ifr     Our private ioctl request structure, typed for the
578 *               generic struct ifreq so we can use ptr to func
579 *               w/o cast.
580 *
581 * Returns:
582 *       zero on success, a negative errno on failure.  Possible values:
583 *               -ENETDOWN Device isn't up.
584 *               -EBUSY  cmd already in progress
585 *               -ETIME  p80211 cmd timed out (MSD may have its own timers)
586 *               -EFAULT memory fault copying msg from user buffer
587 *               -ENOMEM unable to allocate kernel msg buffer
588 *               -ENOSYS bad magic, it the cmd really for us?
589 *               -EintR  sleeping on cmd, awakened by signal, cmd cancelled.
590 *
591 * Call Context:
592 *       Process thread (ioctl caller).  TODO: SMP support may require
593 *       locks.
594 ----------------------------------------------------------------*/
595 static int p80211knetdev_do_ioctl(netdevice_t *dev, struct ifreq *ifr, int cmd)
596 {
597         int                     result = 0;
598         p80211ioctl_req_t       *req = (p80211ioctl_req_t*)ifr;
599         wlandevice_t            *wlandev = dev->ml_priv;
600         u8                      *msgbuf;
601
602         WLAN_LOG_DEBUG(2, "rx'd ioctl, cmd=%d, len=%d\n", cmd, req->len);
603
604 #ifdef SIOCETHTOOL
605         if (cmd == SIOCETHTOOL) {
606                 result = p80211netdev_ethtool(wlandev, (void __user *) ifr->ifr_data);
607                 goto bail;
608         }
609 #endif
610
611         /* Test the magic, assume ifr is good if it's there */
612         if ( req->magic != P80211_IOCTL_MAGIC ) {
613                 result = -ENOSYS;
614                 goto bail;
615         }
616
617         if ( cmd == P80211_IFTEST ) {
618                 result = 0;
619                 goto bail;
620         } else if ( cmd != P80211_IFREQ ) {
621                 result = -ENOSYS;
622                 goto bail;
623         }
624
625         /* Allocate a buf of size req->len */
626         if ((msgbuf = kmalloc( req->len, GFP_KERNEL))) {
627                 if ( copy_from_user( msgbuf, (void __user *) req->data, req->len) ) {
628                         result = -EFAULT;
629                 } else {
630                         result = p80211req_dorequest( wlandev, msgbuf);
631                 }
632
633                 if ( result == 0 ) {
634                         if ( copy_to_user( (void __user *) req->data, msgbuf, req->len)) {
635                                 result = -EFAULT;
636                         }
637                 }
638                 kfree(msgbuf);
639         } else {
640                 result = -ENOMEM;
641         }
642 bail:
643         return result; /* If allocate,copyfrom or copyto fails, return errno */
644 }
645
646 /*----------------------------------------------------------------
647 * p80211knetdev_set_mac_address
648 *
649 * Handles the ioctl for changing the MACAddress of a netdevice
650 *
651 * references: linux/netdevice.h and drivers/net/net_init.c
652 *
653 * NOTE: [MSM] We only prevent address changes when the netdev is
654 * up.  We don't control anything based on dot11 state.  If the
655 * address is changed on a STA that's currently associated, you
656 * will probably lose the ability to send and receive data frames.
657 * Just be aware.  Therefore, this should usually only be done
658 * prior to scan/join/auth/assoc.
659 *
660 * Arguments:
661 *       dev     netdevice struct
662 *       addr    the new MACAddress (a struct)
663 *
664 * Returns:
665 *       zero on success, a negative errno on failure.  Possible values:
666 *               -EBUSY  device is bussy (cmd not possible)
667 *               -and errors returned by: p80211req_dorequest(..)
668 *
669 * by: Collin R. Mulliner <collin@mulliner.org>
670 ----------------------------------------------------------------*/
671 static int p80211knetdev_set_mac_address(netdevice_t *dev, void *addr)
672 {
673         struct sockaddr                 *new_addr = addr;
674         p80211msg_dot11req_mibset_t     dot11req;
675         p80211item_unk392_t             *mibattr;
676         p80211item_pstr6_t              *macaddr;
677         p80211item_uint32_t             *resultcode;
678         int result = 0;
679
680         /* If we're running, we don't allow MAC address changes */
681         if (netif_running(dev)) {
682                 return -EBUSY;
683         }
684
685         /* Set up some convenience pointers. */
686         mibattr = &dot11req.mibattribute;
687         macaddr = (p80211item_pstr6_t*)&mibattr->data;
688         resultcode = &dot11req.resultcode;
689
690         /* Set up a dot11req_mibset */
691         memset(&dot11req, 0, sizeof(p80211msg_dot11req_mibset_t));
692         dot11req.msgcode = DIDmsg_dot11req_mibset;
693         dot11req.msglen = sizeof(p80211msg_dot11req_mibset_t);
694         memcpy(dot11req.devname,
695                 ((wlandevice_t *)dev->ml_priv)->name,
696                 WLAN_DEVNAMELEN_MAX - 1);
697
698         /* Set up the mibattribute argument */
699         mibattr->did = DIDmsg_dot11req_mibset_mibattribute;
700         mibattr->status = P80211ENUM_msgitem_status_data_ok;
701         mibattr->len = sizeof(mibattr->data);
702
703         macaddr->did = DIDmib_dot11mac_dot11OperationTable_dot11MACAddress;
704         macaddr->status = P80211ENUM_msgitem_status_data_ok;
705         macaddr->len = sizeof(macaddr->data);
706         macaddr->data.len = ETH_ALEN;
707         memcpy(&macaddr->data.data, new_addr->sa_data, ETH_ALEN);
708
709         /* Set up the resultcode argument */
710         resultcode->did = DIDmsg_dot11req_mibset_resultcode;
711         resultcode->status = P80211ENUM_msgitem_status_no_value;
712         resultcode->len = sizeof(resultcode->data);
713         resultcode->data = 0;
714
715         /* now fire the request */
716         result = p80211req_dorequest(dev->ml_priv, (u8 *)&dot11req);
717
718         /* If the request wasn't successful, report an error and don't
719          * change the netdev address
720          */
721         if ( result != 0 || resultcode->data != P80211ENUM_resultcode_success) {
722                 WLAN_LOG_ERROR(
723                 "Low-level driver failed dot11req_mibset(dot11MACAddress).\n");
724                 result = -EADDRNOTAVAIL;
725         } else {
726                 /* everything's ok, change the addr in netdev */
727                 memcpy(dev->dev_addr, new_addr->sa_data, dev->addr_len);
728         }
729
730         return result;
731 }
732
733 static int wlan_change_mtu(netdevice_t *dev, int new_mtu)
734 {
735         // 2312 is max 802.11 payload, 20 is overhead, (ether + llc +snap)
736         // and another 8 for wep.
737         if ( (new_mtu < 68) || (new_mtu > (2312 - 20 - 8)))
738                 return -EINVAL;
739
740         dev->mtu = new_mtu;
741
742         return 0;
743 }
744
745
746
747 /*----------------------------------------------------------------
748 * wlan_setup
749 *
750 * Roughly matches the functionality of ether_setup.  Here
751 * we set up any members of the wlandevice structure that are common
752 * to all devices.  Additionally, we allocate a linux 'struct device'
753 * and perform the same setup as ether_setup.
754 *
755 * Note: It's important that the caller have setup the wlandev->name
756 *       ptr prior to calling this function.
757 *
758 * Arguments:
759 *       wlandev         ptr to the wlandev structure for the
760 *                       interface.
761 * Returns:
762 *       zero on success, non-zero otherwise.
763 * Call Context:
764 *       Should be process thread.  We'll assume it might be
765 *       interrupt though.  When we add support for statically
766 *       compiled drivers, this function will be called in the
767 *       context of the kernel startup code.
768 ----------------------------------------------------------------*/
769 int wlan_setup(wlandevice_t *wlandev)
770 {
771         int             result = 0;
772         netdevice_t     *dev;
773
774         /* Set up the wlandev */
775         wlandev->state = WLAN_DEVICE_CLOSED;
776         wlandev->ethconv = WLAN_ETHCONV_8021h;
777         wlandev->macmode = WLAN_MACMODE_NONE;
778
779         /* Set up the rx queue */
780         skb_queue_head_init(&wlandev->nsd_rxq);
781         tasklet_init(&wlandev->rx_bh,
782                      p80211netdev_rx_bh,
783                      (unsigned long)wlandev);
784
785         /* Allocate and initialize the struct device */
786         dev = alloc_netdev(0,"wlan%d",ether_setup);
787         if ( dev == NULL ) {
788                 WLAN_LOG_ERROR("Failed to alloc netdev.\n");
789                 result = 1;
790         } else {
791                 wlandev->netdev = dev;
792                 dev->ml_priv = wlandev;
793                 dev->hard_start_xmit =  p80211knetdev_hard_start_xmit;
794                 dev->get_stats =        p80211knetdev_get_stats;
795 #ifdef HAVE_PRIVATE_IOCTL
796                 dev->do_ioctl =         p80211knetdev_do_ioctl;
797 #endif
798 #ifdef HAVE_MULTICAST
799                 dev->set_multicast_list = p80211knetdev_set_multicast_list;
800 #endif
801                 dev->init =             p80211knetdev_init;
802                 dev->open =             p80211knetdev_open;
803                 dev->stop =             p80211knetdev_stop;
804
805 #if (WIRELESS_EXT < 21)
806                 dev->get_wireless_stats = p80211wext_get_wireless_stats;
807 #endif
808                 dev->wireless_handlers = &p80211wext_handler_def;
809
810                 netif_stop_queue(dev);
811 #ifdef HAVE_CHANGE_MTU
812                 dev->change_mtu = wlan_change_mtu;
813 #endif
814 #ifdef HAVE_SET_MAC_ADDR
815                 dev->set_mac_address =  p80211knetdev_set_mac_address;
816 #endif
817 #ifdef HAVE_TX_TIMEOUT
818                 dev->tx_timeout      =  &p80211knetdev_tx_timeout;
819                 dev->watchdog_timeo  =  (wlan_watchdog * HZ) / 1000;
820 #endif
821                 netif_carrier_off(dev);
822         }
823
824         return result;
825 }
826
827 /*----------------------------------------------------------------
828 * wlan_unsetup
829 *
830 * This function is paired with the wlan_setup routine.  It should
831 * be called after unregister_wlandev.  Basically, all it does is
832 * free the 'struct device' that's associated with the wlandev.
833 * We do it here because the 'struct device' isn't allocated
834 * explicitly in the driver code, it's done in wlan_setup.  To
835 * do the free in the driver might seem like 'magic'.
836 *
837 * Arguments:
838 *       wlandev         ptr to the wlandev structure for the
839 *                       interface.
840 * Returns:
841 *       zero on success, non-zero otherwise.
842 * Call Context:
843 *       Should be process thread.  We'll assume it might be
844 *       interrupt though.  When we add support for statically
845 *       compiled drivers, this function will be called in the
846 *       context of the kernel startup code.
847 ----------------------------------------------------------------*/
848 int wlan_unsetup(wlandevice_t *wlandev)
849 {
850         int             result = 0;
851
852         tasklet_kill(&wlandev->rx_bh);
853
854         if (wlandev->netdev == NULL ) {
855                 WLAN_LOG_ERROR("called without wlandev->netdev set.\n");
856                 result = 1;
857         } else {
858                 free_netdev(wlandev->netdev);
859                 wlandev->netdev = NULL;
860         }
861
862         return 0;
863 }
864
865
866
867 /*----------------------------------------------------------------
868 * register_wlandev
869 *
870 * Roughly matches the functionality of register_netdev.  This function
871 * is called after the driver has successfully probed and set up the
872 * resources for the device.  It's now ready to become a named device
873 * in the Linux system.
874 *
875 * First we allocate a name for the device (if not already set), then
876 * we call the Linux function register_netdevice.
877 *
878 * Arguments:
879 *       wlandev         ptr to the wlandev structure for the
880 *                       interface.
881 * Returns:
882 *       zero on success, non-zero otherwise.
883 * Call Context:
884 *       Can be either interrupt or not.
885 ----------------------------------------------------------------*/
886 int register_wlandev(wlandevice_t *wlandev)
887 {
888         int             i = 0;
889
890         i = register_netdev(wlandev->netdev);
891         if (i)
892                 return i;
893
894         return 0;
895 }
896
897
898 /*----------------------------------------------------------------
899 * unregister_wlandev
900 *
901 * Roughly matches the functionality of unregister_netdev.  This
902 * function is called to remove a named device from the system.
903 *
904 * First we tell linux that the device should no longer exist.
905 * Then we remove it from the list of known wlan devices.
906 *
907 * Arguments:
908 *       wlandev         ptr to the wlandev structure for the
909 *                       interface.
910 * Returns:
911 *       zero on success, non-zero otherwise.
912 * Call Context:
913 *       Can be either interrupt or not.
914 ----------------------------------------------------------------*/
915 int unregister_wlandev(wlandevice_t *wlandev)
916 {
917         struct sk_buff *skb;
918
919         unregister_netdev(wlandev->netdev);
920
921         /* Now to clean out the rx queue */
922         while ( (skb = skb_dequeue(&wlandev->nsd_rxq)) ) {
923                 dev_kfree_skb(skb);
924         }
925
926         return 0;
927 }
928
929
930 /*----------------------------------------------------------------
931 * p80211netdev_hwremoved
932 *
933 * Hardware removed notification. This function should be called
934 * immediately after an MSD has detected that the underlying hardware
935 * has been yanked out from under us.  The primary things we need
936 * to do are:
937 *   - Mark the wlandev
938 *   - Prevent any further traffic from the knetdev i/f
939 *   - Prevent any further requests from mgmt i/f
940 *   - If there are any waitq'd mgmt requests or mgmt-frame exchanges,
941 *     shut them down.
942 *   - Call the MSD hwremoved function.
943 *
944 * The remainder of the cleanup will be handled by unregister().
945 * Our primary goal here is to prevent as much tickling of the MSD
946 * as possible since the MSD is already in a 'wounded' state.
947 *
948 * TODO: As new features are added, this function should be
949 *       updated.
950 *
951 * Arguments:
952 *       wlandev         WLAN network device structure
953 * Returns:
954 *       nothing
955 * Side effects:
956 *
957 * Call context:
958 *       Usually interrupt.
959 ----------------------------------------------------------------*/
960 void p80211netdev_hwremoved(wlandevice_t *wlandev)
961 {
962         wlandev->hwremoved = 1;
963         if ( wlandev->state == WLAN_DEVICE_OPEN) {
964                 netif_stop_queue(wlandev->netdev);
965         }
966
967         netif_device_detach(wlandev->netdev);
968 }
969
970
971 /*----------------------------------------------------------------
972 * p80211_rx_typedrop
973 *
974 * Classifies the frame, increments the appropriate counter, and
975 * returns 0|1|2 indicating whether the driver should handle, ignore, or
976 * drop the frame
977 *
978 * Arguments:
979 *       wlandev         wlan device structure
980 *       fc              frame control field
981 *
982 * Returns:
983 *       zero if the frame should be handled by the driver,
984 *       one if the frame should be ignored
985 *       anything else means we drop it.
986 *
987 * Side effects:
988 *
989 * Call context:
990 *       interrupt
991 ----------------------------------------------------------------*/
992 static int p80211_rx_typedrop( wlandevice_t *wlandev, u16 fc)
993 {
994         u16     ftype;
995         u16     fstype;
996         int     drop = 0;
997         /* Classify frame, increment counter */
998         ftype = WLAN_GET_FC_FTYPE(fc);
999         fstype = WLAN_GET_FC_FSTYPE(fc);
1000 #if 0
1001         WLAN_LOG_DEBUG(4,
1002                 "rx_typedrop : ftype=%d fstype=%d.\n", ftype, fstype);
1003 #endif
1004         switch ( ftype ) {
1005         case WLAN_FTYPE_MGMT:
1006                 if ((wlandev->netdev->flags & IFF_PROMISC) ||
1007                         (wlandev->netdev->flags & IFF_ALLMULTI)) {
1008                         drop = 1;
1009                         break;
1010                 }
1011                 WLAN_LOG_DEBUG(3, "rx'd mgmt:\n");
1012                 wlandev->rx.mgmt++;
1013                 switch( fstype ) {
1014                 case WLAN_FSTYPE_ASSOCREQ:
1015                         /* printk("assocreq"); */
1016                         wlandev->rx.assocreq++;
1017                         break;
1018                 case WLAN_FSTYPE_ASSOCRESP:
1019                         /* printk("assocresp"); */
1020                         wlandev->rx.assocresp++;
1021                         break;
1022                 case WLAN_FSTYPE_REASSOCREQ:
1023                         /* printk("reassocreq"); */
1024                         wlandev->rx.reassocreq++;
1025                         break;
1026                 case WLAN_FSTYPE_REASSOCRESP:
1027                         /* printk("reassocresp"); */
1028                         wlandev->rx.reassocresp++;
1029                         break;
1030                 case WLAN_FSTYPE_PROBEREQ:
1031                         /* printk("probereq"); */
1032                         wlandev->rx.probereq++;
1033                         break;
1034                 case WLAN_FSTYPE_PROBERESP:
1035                         /* printk("proberesp"); */
1036                         wlandev->rx.proberesp++;
1037                         break;
1038                 case WLAN_FSTYPE_BEACON:
1039                         /* printk("beacon"); */
1040                         wlandev->rx.beacon++;
1041                         break;
1042                 case WLAN_FSTYPE_ATIM:
1043                         /* printk("atim"); */
1044                         wlandev->rx.atim++;
1045                         break;
1046                 case WLAN_FSTYPE_DISASSOC:
1047                         /* printk("disassoc"); */
1048                         wlandev->rx.disassoc++;
1049                         break;
1050                 case WLAN_FSTYPE_AUTHEN:
1051                         /* printk("authen"); */
1052                         wlandev->rx.authen++;
1053                         break;
1054                 case WLAN_FSTYPE_DEAUTHEN:
1055                         /* printk("deauthen"); */
1056                         wlandev->rx.deauthen++;
1057                         break;
1058                 default:
1059                         /* printk("unknown"); */
1060                         wlandev->rx.mgmt_unknown++;
1061                         break;
1062                 }
1063                 /* printk("\n"); */
1064                 drop = 2;
1065                 break;
1066
1067         case WLAN_FTYPE_CTL:
1068                 if ((wlandev->netdev->flags & IFF_PROMISC) ||
1069                         (wlandev->netdev->flags & IFF_ALLMULTI)) {
1070                         drop = 1;
1071                         break;
1072                 }
1073                 WLAN_LOG_DEBUG(3, "rx'd ctl:\n");
1074                 wlandev->rx.ctl++;
1075                 switch( fstype ) {
1076                 case WLAN_FSTYPE_PSPOLL:
1077                         /* printk("pspoll"); */
1078                         wlandev->rx.pspoll++;
1079                         break;
1080                 case WLAN_FSTYPE_RTS:
1081                         /* printk("rts"); */
1082                         wlandev->rx.rts++;
1083                         break;
1084                 case WLAN_FSTYPE_CTS:
1085                         /* printk("cts"); */
1086                         wlandev->rx.cts++;
1087                         break;
1088                 case WLAN_FSTYPE_ACK:
1089                         /* printk("ack"); */
1090                         wlandev->rx.ack++;
1091                         break;
1092                 case WLAN_FSTYPE_CFEND:
1093                         /* printk("cfend"); */
1094                         wlandev->rx.cfend++;
1095                         break;
1096                 case WLAN_FSTYPE_CFENDCFACK:
1097                         /* printk("cfendcfack"); */
1098                         wlandev->rx.cfendcfack++;
1099                         break;
1100                 default:
1101                         /* printk("unknown"); */
1102                         wlandev->rx.ctl_unknown++;
1103                         break;
1104                 }
1105                 /* printk("\n"); */
1106                 drop = 2;
1107                 break;
1108
1109         case WLAN_FTYPE_DATA:
1110                 wlandev->rx.data++;
1111                 switch( fstype ) {
1112                 case WLAN_FSTYPE_DATAONLY:
1113                         wlandev->rx.dataonly++;
1114                         break;
1115                 case WLAN_FSTYPE_DATA_CFACK:
1116                         wlandev->rx.data_cfack++;
1117                         break;
1118                 case WLAN_FSTYPE_DATA_CFPOLL:
1119                         wlandev->rx.data_cfpoll++;
1120                         break;
1121                 case WLAN_FSTYPE_DATA_CFACK_CFPOLL:
1122                         wlandev->rx.data__cfack_cfpoll++;
1123                         break;
1124                 case WLAN_FSTYPE_NULL:
1125                         WLAN_LOG_DEBUG(3, "rx'd data:null\n");
1126                         wlandev->rx.null++;
1127                         break;
1128                 case WLAN_FSTYPE_CFACK:
1129                         WLAN_LOG_DEBUG(3, "rx'd data:cfack\n");
1130                         wlandev->rx.cfack++;
1131                         break;
1132                 case WLAN_FSTYPE_CFPOLL:
1133                         WLAN_LOG_DEBUG(3, "rx'd data:cfpoll\n");
1134                         wlandev->rx.cfpoll++;
1135                         break;
1136                 case WLAN_FSTYPE_CFACK_CFPOLL:
1137                         WLAN_LOG_DEBUG(3, "rx'd data:cfack_cfpoll\n");
1138                         wlandev->rx.cfack_cfpoll++;
1139                         break;
1140                 default:
1141                         /* printk("unknown"); */
1142                         wlandev->rx.data_unknown++;
1143                         break;
1144                 }
1145
1146                 break;
1147         }
1148         return drop;
1149 }
1150
1151 static void p80211knetdev_tx_timeout( netdevice_t *netdev)
1152 {
1153         wlandevice_t    *wlandev = netdev->ml_priv;
1154
1155         if (wlandev->tx_timeout) {
1156                 wlandev->tx_timeout(wlandev);
1157         } else {
1158                 WLAN_LOG_WARNING("Implement tx_timeout for %s\n",
1159                                  wlandev->nsdname);
1160                 netif_wake_queue(wlandev->netdev);
1161         }
1162 }