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