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