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