sync hh.org
[hh.org.git] / drivers / net / wireless / hostap / hostap.c
blob87adb347596eae68a57b76e8d06844c42987db4e
1 /*
2 * Host AP (software wireless LAN access point) driver for
3 * Intersil Prism2/2.5/3 - hostap.o module, common routines
5 * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
6 * <jkmaline@cc.hut.fi>
7 * Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation. See README and COPYING for
12 * more details.
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/proc_fs.h>
19 #include <linux/if_arp.h>
20 #include <linux/delay.h>
21 #include <linux/random.h>
22 #include <linux/workqueue.h>
23 #include <linux/kmod.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/wireless.h>
26 #include <net/iw_handler.h>
27 #include <net/ieee80211.h>
28 #include <net/ieee80211_crypt.h>
29 #include <asm/uaccess.h>
31 #include "hostap_wlan.h"
32 #include "hostap_80211.h"
33 #include "hostap_ap.h"
34 #include "hostap.h"
36 MODULE_AUTHOR("Jouni Malinen");
37 MODULE_DESCRIPTION("Host AP common routines");
38 MODULE_LICENSE("GPL");
39 MODULE_VERSION(PRISM2_VERSION);
41 #define TX_TIMEOUT (2 * HZ)
43 #define PRISM2_MAX_FRAME_SIZE 2304
44 #define PRISM2_MIN_MTU 256
45 /* FIX: */
46 #define PRISM2_MAX_MTU (PRISM2_MAX_FRAME_SIZE - (6 /* LLC */ + 8 /* WEP */))
49 /* hostap.c */
50 static int prism2_wds_add(local_info_t *local, u8 *remote_addr,
51 int rtnl_locked);
52 static int prism2_wds_del(local_info_t *local, u8 *remote_addr,
53 int rtnl_locked, int do_not_remove);
55 /* hostap_ap.c */
56 static int prism2_ap_get_sta_qual(local_info_t *local, struct sockaddr addr[],
57 struct iw_quality qual[], int buf_size,
58 int aplist);
59 static int prism2_ap_translate_scan(struct net_device *dev, char *buffer);
60 static int prism2_hostapd(struct ap_data *ap,
61 struct prism2_hostapd_param *param);
62 static void * ap_crypt_get_ptrs(struct ap_data *ap, u8 *addr, int permanent,
63 struct ieee80211_crypt_data ***crypt);
64 static void ap_control_kickall(struct ap_data *ap);
65 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
66 static int ap_control_add_mac(struct mac_restrictions *mac_restrictions,
67 u8 *mac);
68 static int ap_control_del_mac(struct mac_restrictions *mac_restrictions,
69 u8 *mac);
70 static void ap_control_flush_macs(struct mac_restrictions *mac_restrictions);
71 static int ap_control_kick_mac(struct ap_data *ap, struct net_device *dev,
72 u8 *mac);
73 #endif /* !PRISM2_NO_KERNEL_IEEE80211_MGMT */
76 static const long freq_list[] = { 2412, 2417, 2422, 2427, 2432, 2437, 2442,
77 2447, 2452, 2457, 2462, 2467, 2472, 2484 };
78 #define FREQ_COUNT (sizeof(freq_list) / sizeof(freq_list[0]))
81 /* See IEEE 802.1H for LLC/SNAP encapsulation/decapsulation */
82 /* Ethernet-II snap header (RFC1042 for most EtherTypes) */
83 static unsigned char rfc1042_header[] =
84 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
85 /* Bridge-Tunnel header (for EtherTypes ETH_P_AARP and ETH_P_IPX) */
86 static unsigned char bridge_tunnel_header[] =
87 { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
88 /* No encapsulation header if EtherType < 0x600 (=length) */
91 /* FIX: these could be compiled separately and linked together to hostap.o */
92 #include "hostap_ap.c"
93 #include "hostap_info.c"
94 #include "hostap_ioctl.c"
95 #include "hostap_proc.c"
96 #include "hostap_80211_rx.c"
97 #include "hostap_80211_tx.c"
100 struct net_device * hostap_add_interface(struct local_info *local,
101 int type, int rtnl_locked,
102 const char *prefix,
103 const char *name)
105 struct net_device *dev, *mdev;
106 struct hostap_interface *iface;
107 int ret;
109 dev = alloc_etherdev(sizeof(struct hostap_interface));
110 if (dev == NULL)
111 return NULL;
113 iface = netdev_priv(dev);
114 iface->dev = dev;
115 iface->local = local;
116 iface->type = type;
117 list_add(&iface->list, &local->hostap_interfaces);
119 mdev = local->dev;
120 memcpy(dev->dev_addr, mdev->dev_addr, ETH_ALEN);
121 dev->base_addr = mdev->base_addr;
122 dev->irq = mdev->irq;
123 dev->mem_start = mdev->mem_start;
124 dev->mem_end = mdev->mem_end;
126 hostap_setup_dev(dev, local, 0);
127 dev->destructor = free_netdev;
129 sprintf(dev->name, "%s%s", prefix, name);
130 if (!rtnl_locked)
131 rtnl_lock();
133 ret = 0;
134 if (strchr(dev->name, '%'))
135 ret = dev_alloc_name(dev, dev->name);
137 SET_NETDEV_DEV(dev, mdev->class_dev.dev);
138 if (ret >= 0)
139 ret = register_netdevice(dev);
141 if (!rtnl_locked)
142 rtnl_unlock();
144 if (ret < 0) {
145 printk(KERN_WARNING "%s: failed to add new netdevice!\n",
146 dev->name);
147 free_netdev(dev);
148 return NULL;
151 printk(KERN_DEBUG "%s: registered netdevice %s\n",
152 mdev->name, dev->name);
154 return dev;
158 void hostap_remove_interface(struct net_device *dev, int rtnl_locked,
159 int remove_from_list)
161 struct hostap_interface *iface;
163 if (!dev)
164 return;
166 iface = netdev_priv(dev);
168 if (remove_from_list) {
169 list_del(&iface->list);
172 if (dev == iface->local->ddev)
173 iface->local->ddev = NULL;
174 else if (dev == iface->local->apdev)
175 iface->local->apdev = NULL;
176 else if (dev == iface->local->stadev)
177 iface->local->stadev = NULL;
179 if (rtnl_locked)
180 unregister_netdevice(dev);
181 else
182 unregister_netdev(dev);
184 /* dev->destructor = free_netdev() will free the device data, including
185 * private data, when removing the device */
189 static inline int prism2_wds_special_addr(u8 *addr)
191 if (addr[0] || addr[1] || addr[2] || addr[3] || addr[4] || addr[5])
192 return 0;
194 return 1;
198 static int prism2_wds_add(local_info_t *local, u8 *remote_addr,
199 int rtnl_locked)
201 struct net_device *dev;
202 struct list_head *ptr;
203 struct hostap_interface *iface, *empty, *match;
205 empty = match = NULL;
206 read_lock_bh(&local->iface_lock);
207 list_for_each(ptr, &local->hostap_interfaces) {
208 iface = list_entry(ptr, struct hostap_interface, list);
209 if (iface->type != HOSTAP_INTERFACE_WDS)
210 continue;
212 if (prism2_wds_special_addr(iface->u.wds.remote_addr))
213 empty = iface;
214 else if (memcmp(iface->u.wds.remote_addr, remote_addr,
215 ETH_ALEN) == 0) {
216 match = iface;
217 break;
220 if (!match && empty && !prism2_wds_special_addr(remote_addr)) {
221 /* take pre-allocated entry into use */
222 memcpy(empty->u.wds.remote_addr, remote_addr, ETH_ALEN);
223 read_unlock_bh(&local->iface_lock);
224 printk(KERN_DEBUG "%s: using pre-allocated WDS netdevice %s\n",
225 local->dev->name, empty->dev->name);
226 return 0;
228 read_unlock_bh(&local->iface_lock);
230 if (!prism2_wds_special_addr(remote_addr)) {
231 if (match)
232 return -EEXIST;
233 hostap_add_sta(local->ap, remote_addr);
236 if (local->wds_connections >= local->wds_max_connections)
237 return -ENOBUFS;
239 /* verify that there is room for wds# postfix in the interface name */
240 if (strlen(local->dev->name) > IFNAMSIZ - 5) {
241 printk(KERN_DEBUG "'%s' too long base device name\n",
242 local->dev->name);
243 return -EINVAL;
246 dev = hostap_add_interface(local, HOSTAP_INTERFACE_WDS, rtnl_locked,
247 local->ddev->name, "wds%d");
248 if (dev == NULL)
249 return -ENOMEM;
251 iface = netdev_priv(dev);
252 memcpy(iface->u.wds.remote_addr, remote_addr, ETH_ALEN);
254 local->wds_connections++;
256 return 0;
260 static int prism2_wds_del(local_info_t *local, u8 *remote_addr,
261 int rtnl_locked, int do_not_remove)
263 unsigned long flags;
264 struct list_head *ptr;
265 struct hostap_interface *iface, *selected = NULL;
267 write_lock_irqsave(&local->iface_lock, flags);
268 list_for_each(ptr, &local->hostap_interfaces) {
269 iface = list_entry(ptr, struct hostap_interface, list);
270 if (iface->type != HOSTAP_INTERFACE_WDS)
271 continue;
273 if (memcmp(iface->u.wds.remote_addr, remote_addr,
274 ETH_ALEN) == 0) {
275 selected = iface;
276 break;
279 if (selected && !do_not_remove)
280 list_del(&selected->list);
281 write_unlock_irqrestore(&local->iface_lock, flags);
283 if (selected) {
284 if (do_not_remove)
285 memset(selected->u.wds.remote_addr, 0, ETH_ALEN);
286 else {
287 hostap_remove_interface(selected->dev, rtnl_locked, 0);
288 local->wds_connections--;
292 return selected ? 0 : -ENODEV;
296 u16 hostap_tx_callback_register(local_info_t *local,
297 void (*func)(struct sk_buff *, int ok, void *),
298 void *data)
300 unsigned long flags;
301 struct hostap_tx_callback_info *entry;
303 entry = (struct hostap_tx_callback_info *) kmalloc(sizeof(*entry),
304 GFP_ATOMIC);
305 if (entry == NULL)
306 return 0;
308 entry->func = func;
309 entry->data = data;
311 spin_lock_irqsave(&local->lock, flags);
312 entry->idx = local->tx_callback ? local->tx_callback->idx + 1 : 1;
313 entry->next = local->tx_callback;
314 local->tx_callback = entry;
315 spin_unlock_irqrestore(&local->lock, flags);
317 return entry->idx;
321 int hostap_tx_callback_unregister(local_info_t *local, u16 idx)
323 unsigned long flags;
324 struct hostap_tx_callback_info *cb, *prev = NULL;
326 spin_lock_irqsave(&local->lock, flags);
327 cb = local->tx_callback;
328 while (cb != NULL && cb->idx != idx) {
329 prev = cb;
330 cb = cb->next;
332 if (cb) {
333 if (prev == NULL)
334 local->tx_callback = cb->next;
335 else
336 prev->next = cb->next;
337 kfree(cb);
339 spin_unlock_irqrestore(&local->lock, flags);
341 return cb ? 0 : -1;
345 /* val is in host byte order */
346 int hostap_set_word(struct net_device *dev, int rid, u16 val)
348 struct hostap_interface *iface;
349 u16 tmp = cpu_to_le16(val);
350 iface = netdev_priv(dev);
351 return iface->local->func->set_rid(dev, rid, &tmp, 2);
355 int hostap_set_string(struct net_device *dev, int rid, const char *val)
357 struct hostap_interface *iface;
358 char buf[MAX_SSID_LEN + 2];
359 int len;
361 iface = netdev_priv(dev);
362 len = strlen(val);
363 if (len > MAX_SSID_LEN)
364 return -1;
365 memset(buf, 0, sizeof(buf));
366 buf[0] = len; /* little endian 16 bit word */
367 memcpy(buf + 2, val, len);
369 return iface->local->func->set_rid(dev, rid, &buf, MAX_SSID_LEN + 2);
373 u16 hostap_get_porttype(local_info_t *local)
375 if (local->iw_mode == IW_MODE_ADHOC && local->pseudo_adhoc)
376 return HFA384X_PORTTYPE_PSEUDO_IBSS;
377 if (local->iw_mode == IW_MODE_ADHOC)
378 return HFA384X_PORTTYPE_IBSS;
379 if (local->iw_mode == IW_MODE_INFRA)
380 return HFA384X_PORTTYPE_BSS;
381 if (local->iw_mode == IW_MODE_REPEAT)
382 return HFA384X_PORTTYPE_WDS;
383 if (local->iw_mode == IW_MODE_MONITOR)
384 return HFA384X_PORTTYPE_PSEUDO_IBSS;
385 return HFA384X_PORTTYPE_HOSTAP;
389 int hostap_set_encryption(local_info_t *local)
391 u16 val, old_val;
392 int i, keylen, len, idx;
393 char keybuf[WEP_KEY_LEN + 1];
394 enum { NONE, WEP, OTHER } encrypt_type;
396 idx = local->tx_keyidx;
397 if (local->crypt[idx] == NULL || local->crypt[idx]->ops == NULL)
398 encrypt_type = NONE;
399 else if (strcmp(local->crypt[idx]->ops->name, "WEP") == 0)
400 encrypt_type = WEP;
401 else
402 encrypt_type = OTHER;
404 if (local->func->get_rid(local->dev, HFA384X_RID_CNFWEPFLAGS, &val, 2,
405 1) < 0) {
406 printk(KERN_DEBUG "Could not read current WEP flags.\n");
407 goto fail;
409 le16_to_cpus(&val);
410 old_val = val;
412 if (encrypt_type != NONE || local->privacy_invoked)
413 val |= HFA384X_WEPFLAGS_PRIVACYINVOKED;
414 else
415 val &= ~HFA384X_WEPFLAGS_PRIVACYINVOKED;
417 if (local->open_wep || encrypt_type == NONE ||
418 ((local->ieee_802_1x || local->wpa) && local->host_decrypt))
419 val &= ~HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
420 else
421 val |= HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
423 if ((encrypt_type != NONE || local->privacy_invoked) &&
424 (encrypt_type == OTHER || local->host_encrypt))
425 val |= HFA384X_WEPFLAGS_HOSTENCRYPT;
426 else
427 val &= ~HFA384X_WEPFLAGS_HOSTENCRYPT;
428 if ((encrypt_type != NONE || local->privacy_invoked) &&
429 (encrypt_type == OTHER || local->host_decrypt))
430 val |= HFA384X_WEPFLAGS_HOSTDECRYPT;
431 else
432 val &= ~HFA384X_WEPFLAGS_HOSTDECRYPT;
434 if (val != old_val &&
435 hostap_set_word(local->dev, HFA384X_RID_CNFWEPFLAGS, val)) {
436 printk(KERN_DEBUG "Could not write new WEP flags (0x%x)\n",
437 val);
438 goto fail;
441 if (encrypt_type != WEP)
442 return 0;
444 /* 104-bit support seems to require that all the keys are set to the
445 * same keylen */
446 keylen = 6; /* first 5 octets */
447 len = local->crypt[idx]->ops->get_key(keybuf, sizeof(keybuf),
448 NULL, local->crypt[idx]->priv);
449 if (idx >= 0 && idx < WEP_KEYS && len > 5)
450 keylen = WEP_KEY_LEN + 1; /* first 13 octets */
452 for (i = 0; i < WEP_KEYS; i++) {
453 memset(keybuf, 0, sizeof(keybuf));
454 if (local->crypt[i]) {
455 (void) local->crypt[i]->ops->get_key(
456 keybuf, sizeof(keybuf),
457 NULL, local->crypt[i]->priv);
459 if (local->func->set_rid(local->dev,
460 HFA384X_RID_CNFDEFAULTKEY0 + i,
461 keybuf, keylen)) {
462 printk(KERN_DEBUG "Could not set key %d (len=%d)\n",
463 i, keylen);
464 goto fail;
467 if (hostap_set_word(local->dev, HFA384X_RID_CNFWEPDEFAULTKEYID, idx)) {
468 printk(KERN_DEBUG "Could not set default keyid %d\n", idx);
469 goto fail;
472 return 0;
474 fail:
475 printk(KERN_DEBUG "%s: encryption setup failed\n", local->dev->name);
476 return -1;
480 int hostap_set_antsel(local_info_t *local)
482 u16 val;
483 int ret = 0;
485 if (local->antsel_tx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
486 local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
487 HFA386X_CR_TX_CONFIGURE,
488 NULL, &val) == 0) {
489 val &= ~(BIT(2) | BIT(1));
490 switch (local->antsel_tx) {
491 case HOSTAP_ANTSEL_DIVERSITY:
492 val |= BIT(1);
493 break;
494 case HOSTAP_ANTSEL_LOW:
495 break;
496 case HOSTAP_ANTSEL_HIGH:
497 val |= BIT(2);
498 break;
501 if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
502 HFA386X_CR_TX_CONFIGURE, &val, NULL)) {
503 printk(KERN_INFO "%s: setting TX AntSel failed\n",
504 local->dev->name);
505 ret = -1;
509 if (local->antsel_rx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
510 local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
511 HFA386X_CR_RX_CONFIGURE,
512 NULL, &val) == 0) {
513 val &= ~(BIT(1) | BIT(0));
514 switch (local->antsel_rx) {
515 case HOSTAP_ANTSEL_DIVERSITY:
516 break;
517 case HOSTAP_ANTSEL_LOW:
518 val |= BIT(0);
519 break;
520 case HOSTAP_ANTSEL_HIGH:
521 val |= BIT(0) | BIT(1);
522 break;
525 if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
526 HFA386X_CR_RX_CONFIGURE, &val, NULL)) {
527 printk(KERN_INFO "%s: setting RX AntSel failed\n",
528 local->dev->name);
529 ret = -1;
533 return ret;
537 int hostap_set_roaming(local_info_t *local)
539 u16 val;
541 switch (local->host_roaming) {
542 case 1:
543 val = HFA384X_ROAMING_HOST;
544 break;
545 case 2:
546 val = HFA384X_ROAMING_DISABLED;
547 break;
548 case 0:
549 default:
550 val = HFA384X_ROAMING_FIRMWARE;
551 break;
554 return hostap_set_word(local->dev, HFA384X_RID_CNFROAMINGMODE, val);
558 int hostap_set_auth_algs(local_info_t *local)
560 int val = local->auth_algs;
561 /* At least STA f/w v0.6.2 seems to have issues with cnfAuthentication
562 * set to include both Open and Shared Key flags. It tries to use
563 * Shared Key authentication in that case even if WEP keys are not
564 * configured.. STA f/w v0.7.6 is able to handle such configuration,
565 * but it is unknown when this was fixed between 0.6.2 .. 0.7.6. */
566 if (local->sta_fw_ver < PRISM2_FW_VER(0,7,0) &&
567 val != PRISM2_AUTH_OPEN && val != PRISM2_AUTH_SHARED_KEY)
568 val = PRISM2_AUTH_OPEN;
570 if (hostap_set_word(local->dev, HFA384X_RID_CNFAUTHENTICATION, val)) {
571 printk(KERN_INFO "%s: cnfAuthentication setting to 0x%x "
572 "failed\n", local->dev->name, local->auth_algs);
573 return -EINVAL;
576 return 0;
580 void hostap_dump_rx_header(const char *name, const struct hfa384x_rx_frame *rx)
582 u16 status, fc;
584 status = __le16_to_cpu(rx->status);
586 printk(KERN_DEBUG "%s: RX status=0x%04x (port=%d, type=%d, "
587 "fcserr=%d) silence=%d signal=%d rate=%d rxflow=%d; "
588 "jiffies=%ld\n",
589 name, status, (status >> 8) & 0x07, status >> 13, status & 1,
590 rx->silence, rx->signal, rx->rate, rx->rxflow, jiffies);
592 fc = __le16_to_cpu(rx->frame_control);
593 printk(KERN_DEBUG " FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
594 "data_len=%d%s%s\n",
595 fc, WLAN_FC_GET_TYPE(fc) >> 2, WLAN_FC_GET_STYPE(fc) >> 4,
596 __le16_to_cpu(rx->duration_id), __le16_to_cpu(rx->seq_ctrl),
597 __le16_to_cpu(rx->data_len),
598 fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
599 fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
601 printk(KERN_DEBUG " A1=" MACSTR " A2=" MACSTR " A3=" MACSTR " A4="
602 MACSTR "\n",
603 MAC2STR(rx->addr1), MAC2STR(rx->addr2), MAC2STR(rx->addr3),
604 MAC2STR(rx->addr4));
606 printk(KERN_DEBUG " dst=" MACSTR " src=" MACSTR " len=%d\n",
607 MAC2STR(rx->dst_addr), MAC2STR(rx->src_addr),
608 __be16_to_cpu(rx->len));
612 void hostap_dump_tx_header(const char *name, const struct hfa384x_tx_frame *tx)
614 u16 fc;
616 printk(KERN_DEBUG "%s: TX status=0x%04x retry_count=%d tx_rate=%d "
617 "tx_control=0x%04x; jiffies=%ld\n",
618 name, __le16_to_cpu(tx->status), tx->retry_count, tx->tx_rate,
619 __le16_to_cpu(tx->tx_control), jiffies);
621 fc = __le16_to_cpu(tx->frame_control);
622 printk(KERN_DEBUG " FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
623 "data_len=%d%s%s\n",
624 fc, WLAN_FC_GET_TYPE(fc) >> 2, WLAN_FC_GET_STYPE(fc) >> 4,
625 __le16_to_cpu(tx->duration_id), __le16_to_cpu(tx->seq_ctrl),
626 __le16_to_cpu(tx->data_len),
627 fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
628 fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
630 printk(KERN_DEBUG " A1=" MACSTR " A2=" MACSTR " A3=" MACSTR " A4="
631 MACSTR "\n",
632 MAC2STR(tx->addr1), MAC2STR(tx->addr2), MAC2STR(tx->addr3),
633 MAC2STR(tx->addr4));
635 printk(KERN_DEBUG " dst=" MACSTR " src=" MACSTR " len=%d\n",
636 MAC2STR(tx->dst_addr), MAC2STR(tx->src_addr),
637 __be16_to_cpu(tx->len));
641 int hostap_80211_header_parse(struct sk_buff *skb, unsigned char *haddr)
643 memcpy(haddr, skb->mac.raw + 10, ETH_ALEN); /* addr2 */
644 return ETH_ALEN;
648 int hostap_80211_prism_header_parse(struct sk_buff *skb, unsigned char *haddr)
650 if (*(u32 *)skb->mac.raw == LWNG_CAP_DID_BASE) {
651 memcpy(haddr, skb->mac.raw +
652 sizeof(struct linux_wlan_ng_prism_hdr) + 10,
653 ETH_ALEN); /* addr2 */
654 } else { /* (*(u32 *)skb->mac.raw == htonl(LWNG_CAPHDR_VERSION)) */
655 memcpy(haddr, skb->mac.raw +
656 sizeof(struct linux_wlan_ng_cap_hdr) + 10,
657 ETH_ALEN); /* addr2 */
659 return ETH_ALEN;
663 int hostap_80211_get_hdrlen(u16 fc)
665 int hdrlen = 24;
667 switch (WLAN_FC_GET_TYPE(fc)) {
668 case IEEE80211_FTYPE_DATA:
669 if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
670 hdrlen = 30; /* Addr4 */
671 break;
672 case IEEE80211_FTYPE_CTL:
673 switch (WLAN_FC_GET_STYPE(fc)) {
674 case IEEE80211_STYPE_CTS:
675 case IEEE80211_STYPE_ACK:
676 hdrlen = 10;
677 break;
678 default:
679 hdrlen = 16;
680 break;
682 break;
685 return hdrlen;
689 struct net_device_stats *hostap_get_stats(struct net_device *dev)
691 struct hostap_interface *iface;
692 iface = netdev_priv(dev);
693 return &iface->stats;
697 static int prism2_close(struct net_device *dev)
699 struct hostap_interface *iface;
700 local_info_t *local;
702 PDEBUG(DEBUG_FLOW, "%s: prism2_close\n", dev->name);
704 iface = netdev_priv(dev);
705 local = iface->local;
707 if (dev == local->ddev) {
708 prism2_sta_deauth(local, WLAN_REASON_DEAUTH_LEAVING);
710 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
711 if (!local->hostapd && dev == local->dev &&
712 (!local->func->card_present || local->func->card_present(local)) &&
713 local->hw_ready && local->ap && local->iw_mode == IW_MODE_MASTER)
714 hostap_deauth_all_stas(dev, local->ap, 1);
715 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
717 if (dev == local->dev) {
718 local->func->hw_shutdown(dev, HOSTAP_HW_ENABLE_CMDCOMPL);
721 if (netif_running(dev)) {
722 netif_stop_queue(dev);
723 netif_device_detach(dev);
726 flush_scheduled_work();
728 module_put(local->hw_module);
730 local->num_dev_open--;
732 if (dev != local->dev && local->dev->flags & IFF_UP &&
733 local->master_dev_auto_open && local->num_dev_open == 1) {
734 /* Close master radio interface automatically if it was also
735 * opened automatically and we are now closing the last
736 * remaining non-master device. */
737 dev_close(local->dev);
740 return 0;
744 static int prism2_open(struct net_device *dev)
746 struct hostap_interface *iface;
747 local_info_t *local;
749 PDEBUG(DEBUG_FLOW, "%s: prism2_open\n", dev->name);
751 iface = netdev_priv(dev);
752 local = iface->local;
754 if (local->no_pri) {
755 printk(KERN_DEBUG "%s: could not set interface UP - no PRI "
756 "f/w\n", dev->name);
757 return 1;
760 if ((local->func->card_present && !local->func->card_present(local)) ||
761 local->hw_downloading)
762 return -ENODEV;
764 if (!try_module_get(local->hw_module))
765 return -ENODEV;
766 local->num_dev_open++;
768 if (!local->dev_enabled && local->func->hw_enable(dev, 1)) {
769 printk(KERN_WARNING "%s: could not enable MAC port\n",
770 dev->name);
771 prism2_close(dev);
772 return 1;
774 if (!local->dev_enabled)
775 prism2_callback(local, PRISM2_CALLBACK_ENABLE);
776 local->dev_enabled = 1;
778 if (dev != local->dev && !(local->dev->flags & IFF_UP)) {
779 /* Master radio interface is needed for all operation, so open
780 * it automatically when any virtual net_device is opened. */
781 local->master_dev_auto_open = 1;
782 dev_open(local->dev);
785 netif_device_attach(dev);
786 netif_start_queue(dev);
788 return 0;
792 static int prism2_set_mac_address(struct net_device *dev, void *p)
794 struct hostap_interface *iface;
795 local_info_t *local;
796 struct list_head *ptr;
797 struct sockaddr *addr = p;
799 iface = netdev_priv(dev);
800 local = iface->local;
802 if (local->func->set_rid(dev, HFA384X_RID_CNFOWNMACADDR, addr->sa_data,
803 ETH_ALEN) < 0 || local->func->reset_port(dev))
804 return -EINVAL;
806 read_lock_bh(&local->iface_lock);
807 list_for_each(ptr, &local->hostap_interfaces) {
808 iface = list_entry(ptr, struct hostap_interface, list);
809 memcpy(iface->dev->dev_addr, addr->sa_data, ETH_ALEN);
811 memcpy(local->dev->dev_addr, addr->sa_data, ETH_ALEN);
812 read_unlock_bh(&local->iface_lock);
814 return 0;
818 /* TODO: to be further implemented as soon as Prism2 fully supports
819 * GroupAddresses and correct documentation is available */
820 void hostap_set_multicast_list_queue(void *data)
822 struct net_device *dev = (struct net_device *) data;
823 struct hostap_interface *iface;
824 local_info_t *local;
826 iface = netdev_priv(dev);
827 local = iface->local;
828 if (hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
829 local->is_promisc)) {
830 printk(KERN_INFO "%s: %sabling promiscuous mode failed\n",
831 dev->name, local->is_promisc ? "en" : "dis");
836 static void hostap_set_multicast_list(struct net_device *dev)
838 #if 0
839 /* FIX: promiscuous mode seems to be causing a lot of problems with
840 * some station firmware versions (FCSErr frames, invalid MACPort, etc.
841 * corrupted incoming frames). This code is now commented out while the
842 * problems are investigated. */
843 struct hostap_interface *iface;
844 local_info_t *local;
846 iface = netdev_priv(dev);
847 local = iface->local;
848 if ((dev->flags & IFF_ALLMULTI) || (dev->flags & IFF_PROMISC)) {
849 local->is_promisc = 1;
850 } else {
851 local->is_promisc = 0;
854 schedule_work(&local->set_multicast_list_queue);
855 #endif
859 static int prism2_change_mtu(struct net_device *dev, int new_mtu)
861 if (new_mtu < PRISM2_MIN_MTU || new_mtu > PRISM2_MAX_MTU)
862 return -EINVAL;
864 dev->mtu = new_mtu;
865 return 0;
869 static void prism2_tx_timeout(struct net_device *dev)
871 struct hostap_interface *iface;
872 local_info_t *local;
873 struct hfa384x_regs regs;
875 iface = netdev_priv(dev);
876 local = iface->local;
878 printk(KERN_WARNING "%s Tx timed out! Resetting card\n", dev->name);
879 netif_stop_queue(local->dev);
881 local->func->read_regs(dev, &regs);
882 printk(KERN_DEBUG "%s: CMD=%04x EVSTAT=%04x "
883 "OFFSET0=%04x OFFSET1=%04x SWSUPPORT0=%04x\n",
884 dev->name, regs.cmd, regs.evstat, regs.offset0, regs.offset1,
885 regs.swsupport0);
887 local->func->schedule_reset(local);
891 void hostap_setup_dev(struct net_device *dev, local_info_t *local,
892 int main_dev)
894 struct hostap_interface *iface;
896 iface = netdev_priv(dev);
897 ether_setup(dev);
899 /* kernel callbacks */
900 dev->get_stats = hostap_get_stats;
901 if (iface) {
902 /* Currently, we point to the proper spy_data only on
903 * the main_dev. This could be fixed. Jean II */
904 iface->wireless_data.spy_data = &iface->spy_data;
905 dev->wireless_data = &iface->wireless_data;
907 dev->wireless_handlers =
908 (struct iw_handler_def *) &hostap_iw_handler_def;
909 dev->do_ioctl = hostap_ioctl;
910 dev->open = prism2_open;
911 dev->stop = prism2_close;
912 dev->hard_start_xmit = hostap_data_start_xmit;
913 dev->set_mac_address = prism2_set_mac_address;
914 dev->set_multicast_list = hostap_set_multicast_list;
915 dev->change_mtu = prism2_change_mtu;
916 dev->tx_timeout = prism2_tx_timeout;
917 dev->watchdog_timeo = TX_TIMEOUT;
919 dev->mtu = local->mtu;
920 if (!main_dev) {
921 /* use main radio device queue */
922 dev->tx_queue_len = 0;
925 SET_ETHTOOL_OPS(dev, &prism2_ethtool_ops);
927 netif_stop_queue(dev);
931 static int hostap_enable_hostapd(local_info_t *local, int rtnl_locked)
933 struct net_device *dev = local->dev;
935 if (local->apdev)
936 return -EEXIST;
938 printk(KERN_DEBUG "%s: enabling hostapd mode\n", dev->name);
940 local->apdev = hostap_add_interface(local, HOSTAP_INTERFACE_AP,
941 rtnl_locked, local->ddev->name,
942 "ap");
943 if (local->apdev == NULL)
944 return -ENOMEM;
946 local->apdev->hard_start_xmit = hostap_mgmt_start_xmit;
947 local->apdev->type = ARPHRD_IEEE80211;
948 local->apdev->hard_header_parse = hostap_80211_header_parse;
950 return 0;
954 static int hostap_disable_hostapd(local_info_t *local, int rtnl_locked)
956 struct net_device *dev = local->dev;
958 printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
960 hostap_remove_interface(local->apdev, rtnl_locked, 1);
961 local->apdev = NULL;
963 return 0;
967 static int hostap_enable_hostapd_sta(local_info_t *local, int rtnl_locked)
969 struct net_device *dev = local->dev;
971 if (local->stadev)
972 return -EEXIST;
974 printk(KERN_DEBUG "%s: enabling hostapd STA mode\n", dev->name);
976 local->stadev = hostap_add_interface(local, HOSTAP_INTERFACE_STA,
977 rtnl_locked, local->ddev->name,
978 "sta");
979 if (local->stadev == NULL)
980 return -ENOMEM;
982 return 0;
986 static int hostap_disable_hostapd_sta(local_info_t *local, int rtnl_locked)
988 struct net_device *dev = local->dev;
990 printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
992 hostap_remove_interface(local->stadev, rtnl_locked, 1);
993 local->stadev = NULL;
995 return 0;
999 int hostap_set_hostapd(local_info_t *local, int val, int rtnl_locked)
1001 int ret;
1003 if (val < 0 || val > 1)
1004 return -EINVAL;
1006 if (local->hostapd == val)
1007 return 0;
1009 if (val) {
1010 ret = hostap_enable_hostapd(local, rtnl_locked);
1011 if (ret == 0)
1012 local->hostapd = 1;
1013 } else {
1014 local->hostapd = 0;
1015 ret = hostap_disable_hostapd(local, rtnl_locked);
1016 if (ret != 0)
1017 local->hostapd = 1;
1020 return ret;
1024 int hostap_set_hostapd_sta(local_info_t *local, int val, int rtnl_locked)
1026 int ret;
1028 if (val < 0 || val > 1)
1029 return -EINVAL;
1031 if (local->hostapd_sta == val)
1032 return 0;
1034 if (val) {
1035 ret = hostap_enable_hostapd_sta(local, rtnl_locked);
1036 if (ret == 0)
1037 local->hostapd_sta = 1;
1038 } else {
1039 local->hostapd_sta = 0;
1040 ret = hostap_disable_hostapd_sta(local, rtnl_locked);
1041 if (ret != 0)
1042 local->hostapd_sta = 1;
1046 return ret;
1050 int prism2_update_comms_qual(struct net_device *dev)
1052 struct hostap_interface *iface;
1053 local_info_t *local;
1054 int ret = 0;
1055 struct hfa384x_comms_quality sq;
1057 iface = netdev_priv(dev);
1058 local = iface->local;
1059 if (!local->sta_fw_ver)
1060 ret = -1;
1061 else if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1)) {
1062 if (local->func->get_rid(local->dev,
1063 HFA384X_RID_DBMCOMMSQUALITY,
1064 &sq, sizeof(sq), 1) >= 0) {
1065 local->comms_qual = (s16) le16_to_cpu(sq.comm_qual);
1066 local->avg_signal = (s16) le16_to_cpu(sq.signal_level);
1067 local->avg_noise = (s16) le16_to_cpu(sq.noise_level);
1068 local->last_comms_qual_update = jiffies;
1069 } else
1070 ret = -1;
1071 } else {
1072 if (local->func->get_rid(local->dev, HFA384X_RID_COMMSQUALITY,
1073 &sq, sizeof(sq), 1) >= 0) {
1074 local->comms_qual = le16_to_cpu(sq.comm_qual);
1075 local->avg_signal = HFA384X_LEVEL_TO_dBm(
1076 le16_to_cpu(sq.signal_level));
1077 local->avg_noise = HFA384X_LEVEL_TO_dBm(
1078 le16_to_cpu(sq.noise_level));
1079 local->last_comms_qual_update = jiffies;
1080 } else
1081 ret = -1;
1084 return ret;
1088 int prism2_sta_send_mgmt(local_info_t *local, u8 *dst, u16 stype,
1089 u8 *body, size_t bodylen)
1091 struct sk_buff *skb;
1092 struct hostap_ieee80211_mgmt *mgmt;
1093 struct hostap_skb_tx_data *meta;
1094 struct net_device *dev = local->dev;
1096 skb = dev_alloc_skb(IEEE80211_MGMT_HDR_LEN + bodylen);
1097 if (skb == NULL)
1098 return -ENOMEM;
1100 mgmt = (struct hostap_ieee80211_mgmt *)
1101 skb_put(skb, IEEE80211_MGMT_HDR_LEN);
1102 memset(mgmt, 0, IEEE80211_MGMT_HDR_LEN);
1103 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1104 memcpy(mgmt->da, dst, ETH_ALEN);
1105 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1106 memcpy(mgmt->bssid, dst, ETH_ALEN);
1107 if (body)
1108 memcpy(skb_put(skb, bodylen), body, bodylen);
1110 meta = (struct hostap_skb_tx_data *) skb->cb;
1111 memset(meta, 0, sizeof(*meta));
1112 meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
1113 meta->iface = netdev_priv(dev);
1115 skb->dev = dev;
1116 skb->mac.raw = skb->nh.raw = skb->data;
1117 dev_queue_xmit(skb);
1119 return 0;
1123 int prism2_sta_deauth(local_info_t *local, u16 reason)
1125 union iwreq_data wrqu;
1126 int ret;
1128 if (local->iw_mode != IW_MODE_INFRA ||
1129 memcmp(local->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0 ||
1130 memcmp(local->bssid, "\x44\x44\x44\x44\x44\x44", ETH_ALEN) == 0)
1131 return 0;
1133 reason = cpu_to_le16(reason);
1134 ret = prism2_sta_send_mgmt(local, local->bssid, IEEE80211_STYPE_DEAUTH,
1135 (u8 *) &reason, 2);
1136 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
1137 wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
1138 return ret;
1142 struct proc_dir_entry *hostap_proc;
1144 static int __init hostap_init(void)
1146 if (proc_net != NULL) {
1147 hostap_proc = proc_mkdir("hostap", proc_net);
1148 if (!hostap_proc)
1149 printk(KERN_WARNING "Failed to mkdir "
1150 "/proc/net/hostap\n");
1151 } else
1152 hostap_proc = NULL;
1154 return 0;
1158 static void __exit hostap_exit(void)
1160 if (hostap_proc != NULL) {
1161 hostap_proc = NULL;
1162 remove_proc_entry("hostap", proc_net);
1167 EXPORT_SYMBOL(hostap_set_word);
1168 EXPORT_SYMBOL(hostap_set_string);
1169 EXPORT_SYMBOL(hostap_get_porttype);
1170 EXPORT_SYMBOL(hostap_set_encryption);
1171 EXPORT_SYMBOL(hostap_set_antsel);
1172 EXPORT_SYMBOL(hostap_set_roaming);
1173 EXPORT_SYMBOL(hostap_set_auth_algs);
1174 EXPORT_SYMBOL(hostap_dump_rx_header);
1175 EXPORT_SYMBOL(hostap_dump_tx_header);
1176 EXPORT_SYMBOL(hostap_80211_header_parse);
1177 EXPORT_SYMBOL(hostap_80211_prism_header_parse);
1178 EXPORT_SYMBOL(hostap_80211_get_hdrlen);
1179 EXPORT_SYMBOL(hostap_get_stats);
1180 EXPORT_SYMBOL(hostap_setup_dev);
1181 EXPORT_SYMBOL(hostap_proc);
1182 EXPORT_SYMBOL(hostap_set_multicast_list_queue);
1183 EXPORT_SYMBOL(hostap_set_hostapd);
1184 EXPORT_SYMBOL(hostap_set_hostapd_sta);
1185 EXPORT_SYMBOL(hostap_add_interface);
1186 EXPORT_SYMBOL(hostap_remove_interface);
1187 EXPORT_SYMBOL(prism2_update_comms_qual);
1189 module_init(hostap_init);
1190 module_exit(hostap_exit);