lib80211: absorb crypto bits from net/ieee80211
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / hostap / hostap_main.c
blob02a312ca860752ad7c4324570499ee139a771d66
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 * <j@w1.fi>
7 * Copyright (c) 2002-2005, Jouni Malinen <j@w1.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 <linux/etherdevice.h>
27 #include <net/net_namespace.h>
28 #include <net/iw_handler.h>
29 #include <net/ieee80211.h>
30 #include <net/lib80211.h>
31 #include <asm/uaccess.h>
33 #include "hostap_wlan.h"
34 #include "hostap_80211.h"
35 #include "hostap_ap.h"
36 #include "hostap.h"
38 MODULE_AUTHOR("Jouni Malinen");
39 MODULE_DESCRIPTION("Host AP common routines");
40 MODULE_LICENSE("GPL");
42 #define TX_TIMEOUT (2 * HZ)
44 #define PRISM2_MAX_FRAME_SIZE 2304
45 #define PRISM2_MIN_MTU 256
46 /* FIX: */
47 #define PRISM2_MAX_MTU (PRISM2_MAX_FRAME_SIZE - (6 /* LLC */ + 8 /* WEP */))
50 struct net_device * hostap_add_interface(struct local_info *local,
51 int type, int rtnl_locked,
52 const char *prefix,
53 const char *name)
55 struct net_device *dev, *mdev;
56 struct hostap_interface *iface;
57 int ret;
59 dev = alloc_etherdev(sizeof(struct hostap_interface));
60 if (dev == NULL)
61 return NULL;
63 iface = netdev_priv(dev);
64 iface->dev = dev;
65 iface->local = local;
66 iface->type = type;
67 list_add(&iface->list, &local->hostap_interfaces);
69 mdev = local->dev;
70 memcpy(dev->dev_addr, mdev->dev_addr, ETH_ALEN);
71 dev->base_addr = mdev->base_addr;
72 dev->irq = mdev->irq;
73 dev->mem_start = mdev->mem_start;
74 dev->mem_end = mdev->mem_end;
76 hostap_setup_dev(dev, local, type);
77 dev->destructor = free_netdev;
79 sprintf(dev->name, "%s%s", prefix, name);
80 if (!rtnl_locked)
81 rtnl_lock();
83 ret = 0;
84 if (strchr(dev->name, '%'))
85 ret = dev_alloc_name(dev, dev->name);
87 SET_NETDEV_DEV(dev, mdev->dev.parent);
88 if (ret >= 0)
89 ret = register_netdevice(dev);
91 if (!rtnl_locked)
92 rtnl_unlock();
94 if (ret < 0) {
95 printk(KERN_WARNING "%s: failed to add new netdevice!\n",
96 dev->name);
97 free_netdev(dev);
98 return NULL;
101 printk(KERN_DEBUG "%s: registered netdevice %s\n",
102 mdev->name, dev->name);
104 return dev;
108 void hostap_remove_interface(struct net_device *dev, int rtnl_locked,
109 int remove_from_list)
111 struct hostap_interface *iface;
113 if (!dev)
114 return;
116 iface = netdev_priv(dev);
118 if (remove_from_list) {
119 list_del(&iface->list);
122 if (dev == iface->local->ddev)
123 iface->local->ddev = NULL;
124 else if (dev == iface->local->apdev)
125 iface->local->apdev = NULL;
126 else if (dev == iface->local->stadev)
127 iface->local->stadev = NULL;
129 if (rtnl_locked)
130 unregister_netdevice(dev);
131 else
132 unregister_netdev(dev);
134 /* dev->destructor = free_netdev() will free the device data, including
135 * private data, when removing the device */
139 static inline int prism2_wds_special_addr(u8 *addr)
141 if (addr[0] || addr[1] || addr[2] || addr[3] || addr[4] || addr[5])
142 return 0;
144 return 1;
148 int prism2_wds_add(local_info_t *local, u8 *remote_addr,
149 int rtnl_locked)
151 struct net_device *dev;
152 struct list_head *ptr;
153 struct hostap_interface *iface, *empty, *match;
155 empty = match = NULL;
156 read_lock_bh(&local->iface_lock);
157 list_for_each(ptr, &local->hostap_interfaces) {
158 iface = list_entry(ptr, struct hostap_interface, list);
159 if (iface->type != HOSTAP_INTERFACE_WDS)
160 continue;
162 if (prism2_wds_special_addr(iface->u.wds.remote_addr))
163 empty = iface;
164 else if (memcmp(iface->u.wds.remote_addr, remote_addr,
165 ETH_ALEN) == 0) {
166 match = iface;
167 break;
170 if (!match && empty && !prism2_wds_special_addr(remote_addr)) {
171 /* take pre-allocated entry into use */
172 memcpy(empty->u.wds.remote_addr, remote_addr, ETH_ALEN);
173 read_unlock_bh(&local->iface_lock);
174 printk(KERN_DEBUG "%s: using pre-allocated WDS netdevice %s\n",
175 local->dev->name, empty->dev->name);
176 return 0;
178 read_unlock_bh(&local->iface_lock);
180 if (!prism2_wds_special_addr(remote_addr)) {
181 if (match)
182 return -EEXIST;
183 hostap_add_sta(local->ap, remote_addr);
186 if (local->wds_connections >= local->wds_max_connections)
187 return -ENOBUFS;
189 /* verify that there is room for wds# postfix in the interface name */
190 if (strlen(local->dev->name) > IFNAMSIZ - 5) {
191 printk(KERN_DEBUG "'%s' too long base device name\n",
192 local->dev->name);
193 return -EINVAL;
196 dev = hostap_add_interface(local, HOSTAP_INTERFACE_WDS, rtnl_locked,
197 local->ddev->name, "wds%d");
198 if (dev == NULL)
199 return -ENOMEM;
201 iface = netdev_priv(dev);
202 memcpy(iface->u.wds.remote_addr, remote_addr, ETH_ALEN);
204 local->wds_connections++;
206 return 0;
210 int prism2_wds_del(local_info_t *local, u8 *remote_addr,
211 int rtnl_locked, int do_not_remove)
213 unsigned long flags;
214 struct list_head *ptr;
215 struct hostap_interface *iface, *selected = NULL;
217 write_lock_irqsave(&local->iface_lock, flags);
218 list_for_each(ptr, &local->hostap_interfaces) {
219 iface = list_entry(ptr, struct hostap_interface, list);
220 if (iface->type != HOSTAP_INTERFACE_WDS)
221 continue;
223 if (memcmp(iface->u.wds.remote_addr, remote_addr,
224 ETH_ALEN) == 0) {
225 selected = iface;
226 break;
229 if (selected && !do_not_remove)
230 list_del(&selected->list);
231 write_unlock_irqrestore(&local->iface_lock, flags);
233 if (selected) {
234 if (do_not_remove)
235 memset(selected->u.wds.remote_addr, 0, ETH_ALEN);
236 else {
237 hostap_remove_interface(selected->dev, rtnl_locked, 0);
238 local->wds_connections--;
242 return selected ? 0 : -ENODEV;
246 u16 hostap_tx_callback_register(local_info_t *local,
247 void (*func)(struct sk_buff *, int ok, void *),
248 void *data)
250 unsigned long flags;
251 struct hostap_tx_callback_info *entry;
253 entry = kmalloc(sizeof(*entry),
254 GFP_ATOMIC);
255 if (entry == NULL)
256 return 0;
258 entry->func = func;
259 entry->data = data;
261 spin_lock_irqsave(&local->lock, flags);
262 entry->idx = local->tx_callback ? local->tx_callback->idx + 1 : 1;
263 entry->next = local->tx_callback;
264 local->tx_callback = entry;
265 spin_unlock_irqrestore(&local->lock, flags);
267 return entry->idx;
271 int hostap_tx_callback_unregister(local_info_t *local, u16 idx)
273 unsigned long flags;
274 struct hostap_tx_callback_info *cb, *prev = NULL;
276 spin_lock_irqsave(&local->lock, flags);
277 cb = local->tx_callback;
278 while (cb != NULL && cb->idx != idx) {
279 prev = cb;
280 cb = cb->next;
282 if (cb) {
283 if (prev == NULL)
284 local->tx_callback = cb->next;
285 else
286 prev->next = cb->next;
287 kfree(cb);
289 spin_unlock_irqrestore(&local->lock, flags);
291 return cb ? 0 : -1;
295 /* val is in host byte order */
296 int hostap_set_word(struct net_device *dev, int rid, u16 val)
298 struct hostap_interface *iface;
299 __le16 tmp = cpu_to_le16(val);
300 iface = netdev_priv(dev);
301 return iface->local->func->set_rid(dev, rid, &tmp, 2);
305 int hostap_set_string(struct net_device *dev, int rid, const char *val)
307 struct hostap_interface *iface;
308 char buf[MAX_SSID_LEN + 2];
309 int len;
311 iface = netdev_priv(dev);
312 len = strlen(val);
313 if (len > MAX_SSID_LEN)
314 return -1;
315 memset(buf, 0, sizeof(buf));
316 buf[0] = len; /* little endian 16 bit word */
317 memcpy(buf + 2, val, len);
319 return iface->local->func->set_rid(dev, rid, &buf, MAX_SSID_LEN + 2);
323 u16 hostap_get_porttype(local_info_t *local)
325 if (local->iw_mode == IW_MODE_ADHOC && local->pseudo_adhoc)
326 return HFA384X_PORTTYPE_PSEUDO_IBSS;
327 if (local->iw_mode == IW_MODE_ADHOC)
328 return HFA384X_PORTTYPE_IBSS;
329 if (local->iw_mode == IW_MODE_INFRA)
330 return HFA384X_PORTTYPE_BSS;
331 if (local->iw_mode == IW_MODE_REPEAT)
332 return HFA384X_PORTTYPE_WDS;
333 if (local->iw_mode == IW_MODE_MONITOR)
334 return HFA384X_PORTTYPE_PSEUDO_IBSS;
335 return HFA384X_PORTTYPE_HOSTAP;
339 int hostap_set_encryption(local_info_t *local)
341 u16 val, old_val;
342 int i, keylen, len, idx;
343 char keybuf[WEP_KEY_LEN + 1];
344 enum { NONE, WEP, OTHER } encrypt_type;
346 idx = local->crypt_info.tx_keyidx;
347 if (local->crypt_info.crypt[idx] == NULL ||
348 local->crypt_info.crypt[idx]->ops == NULL)
349 encrypt_type = NONE;
350 else if (strcmp(local->crypt_info.crypt[idx]->ops->name, "WEP") == 0)
351 encrypt_type = WEP;
352 else
353 encrypt_type = OTHER;
355 if (local->func->get_rid(local->dev, HFA384X_RID_CNFWEPFLAGS, &val, 2,
356 1) < 0) {
357 printk(KERN_DEBUG "Could not read current WEP flags.\n");
358 goto fail;
360 le16_to_cpus(&val);
361 old_val = val;
363 if (encrypt_type != NONE || local->privacy_invoked)
364 val |= HFA384X_WEPFLAGS_PRIVACYINVOKED;
365 else
366 val &= ~HFA384X_WEPFLAGS_PRIVACYINVOKED;
368 if (local->open_wep || encrypt_type == NONE ||
369 ((local->ieee_802_1x || local->wpa) && local->host_decrypt))
370 val &= ~HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
371 else
372 val |= HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
374 if ((encrypt_type != NONE || local->privacy_invoked) &&
375 (encrypt_type == OTHER || local->host_encrypt))
376 val |= HFA384X_WEPFLAGS_HOSTENCRYPT;
377 else
378 val &= ~HFA384X_WEPFLAGS_HOSTENCRYPT;
379 if ((encrypt_type != NONE || local->privacy_invoked) &&
380 (encrypt_type == OTHER || local->host_decrypt))
381 val |= HFA384X_WEPFLAGS_HOSTDECRYPT;
382 else
383 val &= ~HFA384X_WEPFLAGS_HOSTDECRYPT;
385 if (val != old_val &&
386 hostap_set_word(local->dev, HFA384X_RID_CNFWEPFLAGS, val)) {
387 printk(KERN_DEBUG "Could not write new WEP flags (0x%x)\n",
388 val);
389 goto fail;
392 if (encrypt_type != WEP)
393 return 0;
395 /* 104-bit support seems to require that all the keys are set to the
396 * same keylen */
397 keylen = 6; /* first 5 octets */
398 len = local->crypt_info.crypt[idx]->ops->get_key(keybuf, sizeof(keybuf), NULL,
399 local->crypt_info.crypt[idx]->priv);
400 if (idx >= 0 && idx < WEP_KEYS && len > 5)
401 keylen = WEP_KEY_LEN + 1; /* first 13 octets */
403 for (i = 0; i < WEP_KEYS; i++) {
404 memset(keybuf, 0, sizeof(keybuf));
405 if (local->crypt_info.crypt[i]) {
406 (void) local->crypt_info.crypt[i]->ops->get_key(
407 keybuf, sizeof(keybuf),
408 NULL, local->crypt_info.crypt[i]->priv);
410 if (local->func->set_rid(local->dev,
411 HFA384X_RID_CNFDEFAULTKEY0 + i,
412 keybuf, keylen)) {
413 printk(KERN_DEBUG "Could not set key %d (len=%d)\n",
414 i, keylen);
415 goto fail;
418 if (hostap_set_word(local->dev, HFA384X_RID_CNFWEPDEFAULTKEYID, idx)) {
419 printk(KERN_DEBUG "Could not set default keyid %d\n", idx);
420 goto fail;
423 return 0;
425 fail:
426 printk(KERN_DEBUG "%s: encryption setup failed\n", local->dev->name);
427 return -1;
431 int hostap_set_antsel(local_info_t *local)
433 u16 val;
434 int ret = 0;
436 if (local->antsel_tx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
437 local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
438 HFA386X_CR_TX_CONFIGURE,
439 NULL, &val) == 0) {
440 val &= ~(BIT(2) | BIT(1));
441 switch (local->antsel_tx) {
442 case HOSTAP_ANTSEL_DIVERSITY:
443 val |= BIT(1);
444 break;
445 case HOSTAP_ANTSEL_LOW:
446 break;
447 case HOSTAP_ANTSEL_HIGH:
448 val |= BIT(2);
449 break;
452 if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
453 HFA386X_CR_TX_CONFIGURE, &val, NULL)) {
454 printk(KERN_INFO "%s: setting TX AntSel failed\n",
455 local->dev->name);
456 ret = -1;
460 if (local->antsel_rx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
461 local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
462 HFA386X_CR_RX_CONFIGURE,
463 NULL, &val) == 0) {
464 val &= ~(BIT(1) | BIT(0));
465 switch (local->antsel_rx) {
466 case HOSTAP_ANTSEL_DIVERSITY:
467 break;
468 case HOSTAP_ANTSEL_LOW:
469 val |= BIT(0);
470 break;
471 case HOSTAP_ANTSEL_HIGH:
472 val |= BIT(0) | BIT(1);
473 break;
476 if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
477 HFA386X_CR_RX_CONFIGURE, &val, NULL)) {
478 printk(KERN_INFO "%s: setting RX AntSel failed\n",
479 local->dev->name);
480 ret = -1;
484 return ret;
488 int hostap_set_roaming(local_info_t *local)
490 u16 val;
492 switch (local->host_roaming) {
493 case 1:
494 val = HFA384X_ROAMING_HOST;
495 break;
496 case 2:
497 val = HFA384X_ROAMING_DISABLED;
498 break;
499 case 0:
500 default:
501 val = HFA384X_ROAMING_FIRMWARE;
502 break;
505 return hostap_set_word(local->dev, HFA384X_RID_CNFROAMINGMODE, val);
509 int hostap_set_auth_algs(local_info_t *local)
511 int val = local->auth_algs;
512 /* At least STA f/w v0.6.2 seems to have issues with cnfAuthentication
513 * set to include both Open and Shared Key flags. It tries to use
514 * Shared Key authentication in that case even if WEP keys are not
515 * configured.. STA f/w v0.7.6 is able to handle such configuration,
516 * but it is unknown when this was fixed between 0.6.2 .. 0.7.6. */
517 if (local->sta_fw_ver < PRISM2_FW_VER(0,7,0) &&
518 val != PRISM2_AUTH_OPEN && val != PRISM2_AUTH_SHARED_KEY)
519 val = PRISM2_AUTH_OPEN;
521 if (hostap_set_word(local->dev, HFA384X_RID_CNFAUTHENTICATION, val)) {
522 printk(KERN_INFO "%s: cnfAuthentication setting to 0x%x "
523 "failed\n", local->dev->name, local->auth_algs);
524 return -EINVAL;
527 return 0;
531 void hostap_dump_rx_header(const char *name, const struct hfa384x_rx_frame *rx)
533 u16 status, fc;
535 status = __le16_to_cpu(rx->status);
537 printk(KERN_DEBUG "%s: RX status=0x%04x (port=%d, type=%d, "
538 "fcserr=%d) silence=%d signal=%d rate=%d rxflow=%d; "
539 "jiffies=%ld\n",
540 name, status, (status >> 8) & 0x07, status >> 13, status & 1,
541 rx->silence, rx->signal, rx->rate, rx->rxflow, jiffies);
543 fc = __le16_to_cpu(rx->frame_control);
544 printk(KERN_DEBUG " FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
545 "data_len=%d%s%s\n",
546 fc, WLAN_FC_GET_TYPE(fc) >> 2, WLAN_FC_GET_STYPE(fc) >> 4,
547 __le16_to_cpu(rx->duration_id), __le16_to_cpu(rx->seq_ctrl),
548 __le16_to_cpu(rx->data_len),
549 fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
550 fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
552 printk(KERN_DEBUG " A1=%pM A2=%pM A3=%pM A4=%pM\n",
553 rx->addr1, rx->addr2, rx->addr3, rx->addr4);
555 printk(KERN_DEBUG " dst=%pM src=%pM len=%d\n",
556 rx->dst_addr, rx->src_addr,
557 __be16_to_cpu(rx->len));
561 void hostap_dump_tx_header(const char *name, const struct hfa384x_tx_frame *tx)
563 u16 fc;
565 printk(KERN_DEBUG "%s: TX status=0x%04x retry_count=%d tx_rate=%d "
566 "tx_control=0x%04x; jiffies=%ld\n",
567 name, __le16_to_cpu(tx->status), tx->retry_count, tx->tx_rate,
568 __le16_to_cpu(tx->tx_control), jiffies);
570 fc = __le16_to_cpu(tx->frame_control);
571 printk(KERN_DEBUG " FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
572 "data_len=%d%s%s\n",
573 fc, WLAN_FC_GET_TYPE(fc) >> 2, WLAN_FC_GET_STYPE(fc) >> 4,
574 __le16_to_cpu(tx->duration_id), __le16_to_cpu(tx->seq_ctrl),
575 __le16_to_cpu(tx->data_len),
576 fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
577 fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
579 printk(KERN_DEBUG " A1=%pM A2=%pM A3=%pM A4=%pM\n",
580 tx->addr1, tx->addr2, tx->addr3, tx->addr4);
582 printk(KERN_DEBUG " dst=%pM src=%pM len=%d\n",
583 tx->dst_addr, tx->src_addr,
584 __be16_to_cpu(tx->len));
588 static int hostap_80211_header_parse(const struct sk_buff *skb,
589 unsigned char *haddr)
591 memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
592 return ETH_ALEN;
596 int hostap_80211_get_hdrlen(u16 fc)
598 int hdrlen = 24;
600 switch (WLAN_FC_GET_TYPE(fc)) {
601 case IEEE80211_FTYPE_DATA:
602 if ((fc & IEEE80211_FCTL_FROMDS) && (fc & IEEE80211_FCTL_TODS))
603 hdrlen = 30; /* Addr4 */
604 break;
605 case IEEE80211_FTYPE_CTL:
606 switch (WLAN_FC_GET_STYPE(fc)) {
607 case IEEE80211_STYPE_CTS:
608 case IEEE80211_STYPE_ACK:
609 hdrlen = 10;
610 break;
611 default:
612 hdrlen = 16;
613 break;
615 break;
618 return hdrlen;
622 struct net_device_stats *hostap_get_stats(struct net_device *dev)
624 struct hostap_interface *iface;
625 iface = netdev_priv(dev);
626 return &iface->stats;
630 static int prism2_close(struct net_device *dev)
632 struct hostap_interface *iface;
633 local_info_t *local;
635 PDEBUG(DEBUG_FLOW, "%s: prism2_close\n", dev->name);
637 iface = netdev_priv(dev);
638 local = iface->local;
640 if (dev == local->ddev) {
641 prism2_sta_deauth(local, WLAN_REASON_DEAUTH_LEAVING);
643 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
644 if (!local->hostapd && dev == local->dev &&
645 (!local->func->card_present || local->func->card_present(local)) &&
646 local->hw_ready && local->ap && local->iw_mode == IW_MODE_MASTER)
647 hostap_deauth_all_stas(dev, local->ap, 1);
648 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
650 if (dev == local->dev) {
651 local->func->hw_shutdown(dev, HOSTAP_HW_ENABLE_CMDCOMPL);
654 if (netif_running(dev)) {
655 netif_stop_queue(dev);
656 netif_device_detach(dev);
659 cancel_work_sync(&local->reset_queue);
660 cancel_work_sync(&local->set_multicast_list_queue);
661 cancel_work_sync(&local->set_tim_queue);
662 #ifndef PRISM2_NO_STATION_MODES
663 cancel_work_sync(&local->info_queue);
664 #endif
665 cancel_work_sync(&local->comms_qual_update);
667 module_put(local->hw_module);
669 local->num_dev_open--;
671 if (dev != local->dev && local->dev->flags & IFF_UP &&
672 local->master_dev_auto_open && local->num_dev_open == 1) {
673 /* Close master radio interface automatically if it was also
674 * opened automatically and we are now closing the last
675 * remaining non-master device. */
676 dev_close(local->dev);
679 return 0;
683 static int prism2_open(struct net_device *dev)
685 struct hostap_interface *iface;
686 local_info_t *local;
688 PDEBUG(DEBUG_FLOW, "%s: prism2_open\n", dev->name);
690 iface = netdev_priv(dev);
691 local = iface->local;
693 if (local->no_pri) {
694 printk(KERN_DEBUG "%s: could not set interface UP - no PRI "
695 "f/w\n", dev->name);
696 return 1;
699 if ((local->func->card_present && !local->func->card_present(local)) ||
700 local->hw_downloading)
701 return -ENODEV;
703 if (!try_module_get(local->hw_module))
704 return -ENODEV;
705 local->num_dev_open++;
707 if (!local->dev_enabled && local->func->hw_enable(dev, 1)) {
708 printk(KERN_WARNING "%s: could not enable MAC port\n",
709 dev->name);
710 prism2_close(dev);
711 return 1;
713 if (!local->dev_enabled)
714 prism2_callback(local, PRISM2_CALLBACK_ENABLE);
715 local->dev_enabled = 1;
717 if (dev != local->dev && !(local->dev->flags & IFF_UP)) {
718 /* Master radio interface is needed for all operation, so open
719 * it automatically when any virtual net_device is opened. */
720 local->master_dev_auto_open = 1;
721 dev_open(local->dev);
724 netif_device_attach(dev);
725 netif_start_queue(dev);
727 return 0;
731 static int prism2_set_mac_address(struct net_device *dev, void *p)
733 struct hostap_interface *iface;
734 local_info_t *local;
735 struct list_head *ptr;
736 struct sockaddr *addr = p;
738 iface = netdev_priv(dev);
739 local = iface->local;
741 if (local->func->set_rid(dev, HFA384X_RID_CNFOWNMACADDR, addr->sa_data,
742 ETH_ALEN) < 0 || local->func->reset_port(dev))
743 return -EINVAL;
745 read_lock_bh(&local->iface_lock);
746 list_for_each(ptr, &local->hostap_interfaces) {
747 iface = list_entry(ptr, struct hostap_interface, list);
748 memcpy(iface->dev->dev_addr, addr->sa_data, ETH_ALEN);
750 memcpy(local->dev->dev_addr, addr->sa_data, ETH_ALEN);
751 read_unlock_bh(&local->iface_lock);
753 return 0;
757 /* TODO: to be further implemented as soon as Prism2 fully supports
758 * GroupAddresses and correct documentation is available */
759 void hostap_set_multicast_list_queue(struct work_struct *work)
761 local_info_t *local =
762 container_of(work, local_info_t, set_multicast_list_queue);
763 struct net_device *dev = local->dev;
764 struct hostap_interface *iface;
766 iface = netdev_priv(dev);
767 if (hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
768 local->is_promisc)) {
769 printk(KERN_INFO "%s: %sabling promiscuous mode failed\n",
770 dev->name, local->is_promisc ? "en" : "dis");
775 static void hostap_set_multicast_list(struct net_device *dev)
777 #if 0
778 /* FIX: promiscuous mode seems to be causing a lot of problems with
779 * some station firmware versions (FCSErr frames, invalid MACPort, etc.
780 * corrupted incoming frames). This code is now commented out while the
781 * problems are investigated. */
782 struct hostap_interface *iface;
783 local_info_t *local;
785 iface = netdev_priv(dev);
786 local = iface->local;
787 if ((dev->flags & IFF_ALLMULTI) || (dev->flags & IFF_PROMISC)) {
788 local->is_promisc = 1;
789 } else {
790 local->is_promisc = 0;
793 schedule_work(&local->set_multicast_list_queue);
794 #endif
798 static int prism2_change_mtu(struct net_device *dev, int new_mtu)
800 if (new_mtu < PRISM2_MIN_MTU || new_mtu > PRISM2_MAX_MTU)
801 return -EINVAL;
803 dev->mtu = new_mtu;
804 return 0;
808 static void prism2_tx_timeout(struct net_device *dev)
810 struct hostap_interface *iface;
811 local_info_t *local;
812 struct hfa384x_regs regs;
814 iface = netdev_priv(dev);
815 local = iface->local;
817 printk(KERN_WARNING "%s Tx timed out! Resetting card\n", dev->name);
818 netif_stop_queue(local->dev);
820 local->func->read_regs(dev, &regs);
821 printk(KERN_DEBUG "%s: CMD=%04x EVSTAT=%04x "
822 "OFFSET0=%04x OFFSET1=%04x SWSUPPORT0=%04x\n",
823 dev->name, regs.cmd, regs.evstat, regs.offset0, regs.offset1,
824 regs.swsupport0);
826 local->func->schedule_reset(local);
829 const struct header_ops hostap_80211_ops = {
830 .create = eth_header,
831 .rebuild = eth_rebuild_header,
832 .cache = eth_header_cache,
833 .cache_update = eth_header_cache_update,
834 .parse = hostap_80211_header_parse,
836 EXPORT_SYMBOL(hostap_80211_ops);
838 void hostap_setup_dev(struct net_device *dev, local_info_t *local,
839 int type)
841 struct hostap_interface *iface;
843 iface = netdev_priv(dev);
844 ether_setup(dev);
846 /* kernel callbacks */
847 dev->get_stats = hostap_get_stats;
848 if (iface) {
849 /* Currently, we point to the proper spy_data only on
850 * the main_dev. This could be fixed. Jean II */
851 iface->wireless_data.spy_data = &iface->spy_data;
852 dev->wireless_data = &iface->wireless_data;
854 dev->wireless_handlers =
855 (struct iw_handler_def *) &hostap_iw_handler_def;
856 dev->do_ioctl = hostap_ioctl;
857 dev->open = prism2_open;
858 dev->stop = prism2_close;
859 dev->set_mac_address = prism2_set_mac_address;
860 dev->set_multicast_list = hostap_set_multicast_list;
861 dev->change_mtu = prism2_change_mtu;
862 dev->tx_timeout = prism2_tx_timeout;
863 dev->watchdog_timeo = TX_TIMEOUT;
865 if (type == HOSTAP_INTERFACE_AP) {
866 dev->hard_start_xmit = hostap_mgmt_start_xmit;
867 dev->type = ARPHRD_IEEE80211;
868 dev->header_ops = &hostap_80211_ops;
869 } else {
870 dev->hard_start_xmit = hostap_data_start_xmit;
873 dev->mtu = local->mtu;
874 if (type != HOSTAP_INTERFACE_MASTER) {
875 /* use main radio device queue */
876 dev->tx_queue_len = 0;
879 SET_ETHTOOL_OPS(dev, &prism2_ethtool_ops);
881 netif_stop_queue(dev);
884 static int hostap_enable_hostapd(local_info_t *local, int rtnl_locked)
886 struct net_device *dev = local->dev;
888 if (local->apdev)
889 return -EEXIST;
891 printk(KERN_DEBUG "%s: enabling hostapd mode\n", dev->name);
893 local->apdev = hostap_add_interface(local, HOSTAP_INTERFACE_AP,
894 rtnl_locked, local->ddev->name,
895 "ap");
896 if (local->apdev == NULL)
897 return -ENOMEM;
899 return 0;
903 static int hostap_disable_hostapd(local_info_t *local, int rtnl_locked)
905 struct net_device *dev = local->dev;
907 printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
909 hostap_remove_interface(local->apdev, rtnl_locked, 1);
910 local->apdev = NULL;
912 return 0;
916 static int hostap_enable_hostapd_sta(local_info_t *local, int rtnl_locked)
918 struct net_device *dev = local->dev;
920 if (local->stadev)
921 return -EEXIST;
923 printk(KERN_DEBUG "%s: enabling hostapd STA mode\n", dev->name);
925 local->stadev = hostap_add_interface(local, HOSTAP_INTERFACE_STA,
926 rtnl_locked, local->ddev->name,
927 "sta");
928 if (local->stadev == NULL)
929 return -ENOMEM;
931 return 0;
935 static int hostap_disable_hostapd_sta(local_info_t *local, int rtnl_locked)
937 struct net_device *dev = local->dev;
939 printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
941 hostap_remove_interface(local->stadev, rtnl_locked, 1);
942 local->stadev = NULL;
944 return 0;
948 int hostap_set_hostapd(local_info_t *local, int val, int rtnl_locked)
950 int ret;
952 if (val < 0 || val > 1)
953 return -EINVAL;
955 if (local->hostapd == val)
956 return 0;
958 if (val) {
959 ret = hostap_enable_hostapd(local, rtnl_locked);
960 if (ret == 0)
961 local->hostapd = 1;
962 } else {
963 local->hostapd = 0;
964 ret = hostap_disable_hostapd(local, rtnl_locked);
965 if (ret != 0)
966 local->hostapd = 1;
969 return ret;
973 int hostap_set_hostapd_sta(local_info_t *local, int val, int rtnl_locked)
975 int ret;
977 if (val < 0 || val > 1)
978 return -EINVAL;
980 if (local->hostapd_sta == val)
981 return 0;
983 if (val) {
984 ret = hostap_enable_hostapd_sta(local, rtnl_locked);
985 if (ret == 0)
986 local->hostapd_sta = 1;
987 } else {
988 local->hostapd_sta = 0;
989 ret = hostap_disable_hostapd_sta(local, rtnl_locked);
990 if (ret != 0)
991 local->hostapd_sta = 1;
995 return ret;
999 int prism2_update_comms_qual(struct net_device *dev)
1001 struct hostap_interface *iface;
1002 local_info_t *local;
1003 int ret = 0;
1004 struct hfa384x_comms_quality sq;
1006 iface = netdev_priv(dev);
1007 local = iface->local;
1008 if (!local->sta_fw_ver)
1009 ret = -1;
1010 else if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1)) {
1011 if (local->func->get_rid(local->dev,
1012 HFA384X_RID_DBMCOMMSQUALITY,
1013 &sq, sizeof(sq), 1) >= 0) {
1014 local->comms_qual = (s16) le16_to_cpu(sq.comm_qual);
1015 local->avg_signal = (s16) le16_to_cpu(sq.signal_level);
1016 local->avg_noise = (s16) le16_to_cpu(sq.noise_level);
1017 local->last_comms_qual_update = jiffies;
1018 } else
1019 ret = -1;
1020 } else {
1021 if (local->func->get_rid(local->dev, HFA384X_RID_COMMSQUALITY,
1022 &sq, sizeof(sq), 1) >= 0) {
1023 local->comms_qual = le16_to_cpu(sq.comm_qual);
1024 local->avg_signal = HFA384X_LEVEL_TO_dBm(
1025 le16_to_cpu(sq.signal_level));
1026 local->avg_noise = HFA384X_LEVEL_TO_dBm(
1027 le16_to_cpu(sq.noise_level));
1028 local->last_comms_qual_update = jiffies;
1029 } else
1030 ret = -1;
1033 return ret;
1037 int prism2_sta_send_mgmt(local_info_t *local, u8 *dst, u16 stype,
1038 u8 *body, size_t bodylen)
1040 struct sk_buff *skb;
1041 struct hostap_ieee80211_mgmt *mgmt;
1042 struct hostap_skb_tx_data *meta;
1043 struct net_device *dev = local->dev;
1045 skb = dev_alloc_skb(IEEE80211_MGMT_HDR_LEN + bodylen);
1046 if (skb == NULL)
1047 return -ENOMEM;
1049 mgmt = (struct hostap_ieee80211_mgmt *)
1050 skb_put(skb, IEEE80211_MGMT_HDR_LEN);
1051 memset(mgmt, 0, IEEE80211_MGMT_HDR_LEN);
1052 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1053 memcpy(mgmt->da, dst, ETH_ALEN);
1054 memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1055 memcpy(mgmt->bssid, dst, ETH_ALEN);
1056 if (body)
1057 memcpy(skb_put(skb, bodylen), body, bodylen);
1059 meta = (struct hostap_skb_tx_data *) skb->cb;
1060 memset(meta, 0, sizeof(*meta));
1061 meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
1062 meta->iface = netdev_priv(dev);
1064 skb->dev = dev;
1065 skb_reset_mac_header(skb);
1066 skb_reset_network_header(skb);
1067 dev_queue_xmit(skb);
1069 return 0;
1073 int prism2_sta_deauth(local_info_t *local, u16 reason)
1075 union iwreq_data wrqu;
1076 int ret;
1077 __le16 val = cpu_to_le16(reason);
1079 if (local->iw_mode != IW_MODE_INFRA ||
1080 memcmp(local->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0 ||
1081 memcmp(local->bssid, "\x44\x44\x44\x44\x44\x44", ETH_ALEN) == 0)
1082 return 0;
1084 ret = prism2_sta_send_mgmt(local, local->bssid, IEEE80211_STYPE_DEAUTH,
1085 (u8 *) &val, 2);
1086 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
1087 wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
1088 return ret;
1092 struct proc_dir_entry *hostap_proc;
1094 static int __init hostap_init(void)
1096 if (init_net.proc_net != NULL) {
1097 hostap_proc = proc_mkdir("hostap", init_net.proc_net);
1098 if (!hostap_proc)
1099 printk(KERN_WARNING "Failed to mkdir "
1100 "/proc/net/hostap\n");
1101 } else
1102 hostap_proc = NULL;
1104 return 0;
1108 static void __exit hostap_exit(void)
1110 if (hostap_proc != NULL) {
1111 hostap_proc = NULL;
1112 remove_proc_entry("hostap", init_net.proc_net);
1117 EXPORT_SYMBOL(hostap_set_word);
1118 EXPORT_SYMBOL(hostap_set_string);
1119 EXPORT_SYMBOL(hostap_get_porttype);
1120 EXPORT_SYMBOL(hostap_set_encryption);
1121 EXPORT_SYMBOL(hostap_set_antsel);
1122 EXPORT_SYMBOL(hostap_set_roaming);
1123 EXPORT_SYMBOL(hostap_set_auth_algs);
1124 EXPORT_SYMBOL(hostap_dump_rx_header);
1125 EXPORT_SYMBOL(hostap_dump_tx_header);
1126 EXPORT_SYMBOL(hostap_80211_get_hdrlen);
1127 EXPORT_SYMBOL(hostap_get_stats);
1128 EXPORT_SYMBOL(hostap_setup_dev);
1129 EXPORT_SYMBOL(hostap_set_multicast_list_queue);
1130 EXPORT_SYMBOL(hostap_set_hostapd);
1131 EXPORT_SYMBOL(hostap_set_hostapd_sta);
1132 EXPORT_SYMBOL(hostap_add_interface);
1133 EXPORT_SYMBOL(hostap_remove_interface);
1134 EXPORT_SYMBOL(prism2_update_comms_qual);
1136 module_init(hostap_init);
1137 module_exit(hostap_exit);