Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Host AP (software wireless LAN access point) driver for
0004  * Intersil Prism2/2.5/3 - hostap.o module, common routines
0005  *
0006  * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
0007  * <j@w1.fi>
0008  * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
0009  */
0010 
0011 #include <linux/module.h>
0012 #include <linux/init.h>
0013 #include <linux/slab.h>
0014 #include <linux/proc_fs.h>
0015 #include <linux/if_arp.h>
0016 #include <linux/delay.h>
0017 #include <linux/random.h>
0018 #include <linux/workqueue.h>
0019 #include <linux/kmod.h>
0020 #include <linux/rtnetlink.h>
0021 #include <linux/wireless.h>
0022 #include <linux/etherdevice.h>
0023 #include <net/net_namespace.h>
0024 #include <net/iw_handler.h>
0025 #include <net/lib80211.h>
0026 #include <linux/uaccess.h>
0027 
0028 #include "hostap_wlan.h"
0029 #include "hostap_80211.h"
0030 #include "hostap_ap.h"
0031 #include "hostap.h"
0032 
0033 MODULE_AUTHOR("Jouni Malinen");
0034 MODULE_DESCRIPTION("Host AP common routines");
0035 MODULE_LICENSE("GPL");
0036 
0037 #define TX_TIMEOUT (2 * HZ)
0038 
0039 #define PRISM2_MAX_FRAME_SIZE 2304
0040 #define PRISM2_MIN_MTU 256
0041 /* FIX: */
0042 #define PRISM2_MAX_MTU (PRISM2_MAX_FRAME_SIZE - (6 /* LLC */ + 8 /* WEP */))
0043 
0044 
0045 struct net_device * hostap_add_interface(struct local_info *local,
0046                      int type, int rtnl_locked,
0047                      const char *prefix,
0048                      const char *name)
0049 {
0050     struct net_device *dev, *mdev;
0051     struct hostap_interface *iface;
0052     int ret;
0053 
0054     dev = alloc_etherdev(sizeof(struct hostap_interface));
0055     if (dev == NULL)
0056         return NULL;
0057 
0058     iface = netdev_priv(dev);
0059     iface->dev = dev;
0060     iface->local = local;
0061     iface->type = type;
0062     list_add(&iface->list, &local->hostap_interfaces);
0063 
0064     mdev = local->dev;
0065     eth_hw_addr_inherit(dev, mdev);
0066     dev->base_addr = mdev->base_addr;
0067     dev->irq = mdev->irq;
0068     dev->mem_start = mdev->mem_start;
0069     dev->mem_end = mdev->mem_end;
0070 
0071     hostap_setup_dev(dev, local, type);
0072     dev->needs_free_netdev = true;
0073 
0074     sprintf(dev->name, "%s%s", prefix, name);
0075     if (!rtnl_locked)
0076         rtnl_lock();
0077 
0078     SET_NETDEV_DEV(dev, mdev->dev.parent);
0079     ret = register_netdevice(dev);
0080 
0081     if (!rtnl_locked)
0082         rtnl_unlock();
0083 
0084     if (ret < 0) {
0085         printk(KERN_WARNING "%s: failed to add new netdevice!\n",
0086                dev->name);
0087         free_netdev(dev);
0088         return NULL;
0089     }
0090 
0091     printk(KERN_DEBUG "%s: registered netdevice %s\n",
0092            mdev->name, dev->name);
0093 
0094     return dev;
0095 }
0096 
0097 
0098 void hostap_remove_interface(struct net_device *dev, int rtnl_locked,
0099                  int remove_from_list)
0100 {
0101     struct hostap_interface *iface;
0102 
0103     if (!dev)
0104         return;
0105 
0106     iface = netdev_priv(dev);
0107 
0108     if (remove_from_list) {
0109         list_del(&iface->list);
0110     }
0111 
0112     if (dev == iface->local->ddev)
0113         iface->local->ddev = NULL;
0114     else if (dev == iface->local->apdev)
0115         iface->local->apdev = NULL;
0116     else if (dev == iface->local->stadev)
0117         iface->local->stadev = NULL;
0118 
0119     if (rtnl_locked)
0120         unregister_netdevice(dev);
0121     else
0122         unregister_netdev(dev);
0123 
0124     /* 'dev->needs_free_netdev = true' implies device data, including
0125      * private data, will be freed when the device is removed */
0126 }
0127 
0128 
0129 static inline int prism2_wds_special_addr(u8 *addr)
0130 {
0131     if (addr[0] || addr[1] || addr[2] || addr[3] || addr[4] || addr[5])
0132         return 0;
0133 
0134     return 1;
0135 }
0136 
0137 
0138 int prism2_wds_add(local_info_t *local, u8 *remote_addr,
0139            int rtnl_locked)
0140 {
0141     struct net_device *dev;
0142     struct list_head *ptr;
0143     struct hostap_interface *iface, *empty, *match;
0144 
0145     empty = match = NULL;
0146     read_lock_bh(&local->iface_lock);
0147     list_for_each(ptr, &local->hostap_interfaces) {
0148         iface = list_entry(ptr, struct hostap_interface, list);
0149         if (iface->type != HOSTAP_INTERFACE_WDS)
0150             continue;
0151 
0152         if (prism2_wds_special_addr(iface->u.wds.remote_addr))
0153             empty = iface;
0154         else if (ether_addr_equal(iface->u.wds.remote_addr, remote_addr)) {
0155             match = iface;
0156             break;
0157         }
0158     }
0159     if (!match && empty && !prism2_wds_special_addr(remote_addr)) {
0160         /* take pre-allocated entry into use */
0161         memcpy(empty->u.wds.remote_addr, remote_addr, ETH_ALEN);
0162         read_unlock_bh(&local->iface_lock);
0163         printk(KERN_DEBUG "%s: using pre-allocated WDS netdevice %s\n",
0164                local->dev->name, empty->dev->name);
0165         return 0;
0166     }
0167     read_unlock_bh(&local->iface_lock);
0168 
0169     if (!prism2_wds_special_addr(remote_addr)) {
0170         if (match)
0171             return -EEXIST;
0172         hostap_add_sta(local->ap, remote_addr);
0173     }
0174 
0175     if (local->wds_connections >= local->wds_max_connections)
0176         return -ENOBUFS;
0177 
0178     /* verify that there is room for wds# postfix in the interface name */
0179     if (strlen(local->dev->name) >= IFNAMSIZ - 5) {
0180         printk(KERN_DEBUG "'%s' too long base device name\n",
0181                local->dev->name);
0182         return -EINVAL;
0183     }
0184 
0185     dev = hostap_add_interface(local, HOSTAP_INTERFACE_WDS, rtnl_locked,
0186                    local->ddev->name, "wds%d");
0187     if (dev == NULL)
0188         return -ENOMEM;
0189 
0190     iface = netdev_priv(dev);
0191     memcpy(iface->u.wds.remote_addr, remote_addr, ETH_ALEN);
0192 
0193     local->wds_connections++;
0194 
0195     return 0;
0196 }
0197 
0198 
0199 int prism2_wds_del(local_info_t *local, u8 *remote_addr,
0200            int rtnl_locked, int do_not_remove)
0201 {
0202     unsigned long flags;
0203     struct list_head *ptr;
0204     struct hostap_interface *iface, *selected = NULL;
0205 
0206     write_lock_irqsave(&local->iface_lock, flags);
0207     list_for_each(ptr, &local->hostap_interfaces) {
0208         iface = list_entry(ptr, struct hostap_interface, list);
0209         if (iface->type != HOSTAP_INTERFACE_WDS)
0210             continue;
0211 
0212         if (ether_addr_equal(iface->u.wds.remote_addr, remote_addr)) {
0213             selected = iface;
0214             break;
0215         }
0216     }
0217     if (selected && !do_not_remove)
0218         list_del(&selected->list);
0219     write_unlock_irqrestore(&local->iface_lock, flags);
0220 
0221     if (selected) {
0222         if (do_not_remove)
0223             eth_zero_addr(selected->u.wds.remote_addr);
0224         else {
0225             hostap_remove_interface(selected->dev, rtnl_locked, 0);
0226             local->wds_connections--;
0227         }
0228     }
0229 
0230     return selected ? 0 : -ENODEV;
0231 }
0232 
0233 
0234 u16 hostap_tx_callback_register(local_info_t *local,
0235                 void (*func)(struct sk_buff *, int ok, void *),
0236                 void *data)
0237 {
0238     unsigned long flags;
0239     struct hostap_tx_callback_info *entry;
0240 
0241     entry = kmalloc(sizeof(*entry), GFP_KERNEL);
0242     if (entry == NULL)
0243         return 0;
0244 
0245     entry->func = func;
0246     entry->data = data;
0247 
0248     spin_lock_irqsave(&local->lock, flags);
0249     entry->idx = local->tx_callback ? local->tx_callback->idx + 1 : 1;
0250     entry->next = local->tx_callback;
0251     local->tx_callback = entry;
0252     spin_unlock_irqrestore(&local->lock, flags);
0253 
0254     return entry->idx;
0255 }
0256 
0257 
0258 int hostap_tx_callback_unregister(local_info_t *local, u16 idx)
0259 {
0260     unsigned long flags;
0261     struct hostap_tx_callback_info *cb, *prev = NULL;
0262 
0263     spin_lock_irqsave(&local->lock, flags);
0264     cb = local->tx_callback;
0265     while (cb != NULL && cb->idx != idx) {
0266         prev = cb;
0267         cb = cb->next;
0268     }
0269     if (cb) {
0270         if (prev == NULL)
0271             local->tx_callback = cb->next;
0272         else
0273             prev->next = cb->next;
0274         kfree(cb);
0275     }
0276     spin_unlock_irqrestore(&local->lock, flags);
0277 
0278     return cb ? 0 : -1;
0279 }
0280 
0281 
0282 /* val is in host byte order */
0283 int hostap_set_word(struct net_device *dev, int rid, u16 val)
0284 {
0285     struct hostap_interface *iface;
0286     __le16 tmp = cpu_to_le16(val);
0287     iface = netdev_priv(dev);
0288     return iface->local->func->set_rid(dev, rid, &tmp, 2);
0289 }
0290 
0291 
0292 int hostap_set_string(struct net_device *dev, int rid, const char *val)
0293 {
0294     struct hostap_interface *iface;
0295     char buf[MAX_SSID_LEN + 2];
0296     int len;
0297 
0298     iface = netdev_priv(dev);
0299     len = strlen(val);
0300     if (len > MAX_SSID_LEN)
0301         return -1;
0302     memset(buf, 0, sizeof(buf));
0303     buf[0] = len; /* little endian 16 bit word */
0304     memcpy(buf + 2, val, len);
0305 
0306     return iface->local->func->set_rid(dev, rid, &buf, MAX_SSID_LEN + 2);
0307 }
0308 
0309 
0310 u16 hostap_get_porttype(local_info_t *local)
0311 {
0312     if (local->iw_mode == IW_MODE_ADHOC && local->pseudo_adhoc)
0313         return HFA384X_PORTTYPE_PSEUDO_IBSS;
0314     if (local->iw_mode == IW_MODE_ADHOC)
0315         return HFA384X_PORTTYPE_IBSS;
0316     if (local->iw_mode == IW_MODE_INFRA)
0317         return HFA384X_PORTTYPE_BSS;
0318     if (local->iw_mode == IW_MODE_REPEAT)
0319         return HFA384X_PORTTYPE_WDS;
0320     if (local->iw_mode == IW_MODE_MONITOR)
0321         return HFA384X_PORTTYPE_PSEUDO_IBSS;
0322     return HFA384X_PORTTYPE_HOSTAP;
0323 }
0324 
0325 
0326 int hostap_set_encryption(local_info_t *local)
0327 {
0328     u16 val, old_val;
0329     int i, keylen, len, idx;
0330     char keybuf[WEP_KEY_LEN + 1];
0331     enum { NONE, WEP, OTHER } encrypt_type;
0332 
0333     idx = local->crypt_info.tx_keyidx;
0334     if (local->crypt_info.crypt[idx] == NULL ||
0335         local->crypt_info.crypt[idx]->ops == NULL)
0336         encrypt_type = NONE;
0337     else if (strcmp(local->crypt_info.crypt[idx]->ops->name, "WEP") == 0)
0338         encrypt_type = WEP;
0339     else
0340         encrypt_type = OTHER;
0341 
0342     if (local->func->get_rid(local->dev, HFA384X_RID_CNFWEPFLAGS, &val, 2,
0343                  1) < 0) {
0344         printk(KERN_DEBUG "Could not read current WEP flags.\n");
0345         goto fail;
0346     }
0347     le16_to_cpus(&val);
0348     old_val = val;
0349 
0350     if (encrypt_type != NONE || local->privacy_invoked)
0351         val |= HFA384X_WEPFLAGS_PRIVACYINVOKED;
0352     else
0353         val &= ~HFA384X_WEPFLAGS_PRIVACYINVOKED;
0354 
0355     if (local->open_wep || encrypt_type == NONE ||
0356         ((local->ieee_802_1x || local->wpa) && local->host_decrypt))
0357         val &= ~HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
0358     else
0359         val |= HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
0360 
0361     if ((encrypt_type != NONE || local->privacy_invoked) &&
0362         (encrypt_type == OTHER || local->host_encrypt))
0363         val |= HFA384X_WEPFLAGS_HOSTENCRYPT;
0364     else
0365         val &= ~HFA384X_WEPFLAGS_HOSTENCRYPT;
0366     if ((encrypt_type != NONE || local->privacy_invoked) &&
0367         (encrypt_type == OTHER || local->host_decrypt))
0368         val |= HFA384X_WEPFLAGS_HOSTDECRYPT;
0369     else
0370         val &= ~HFA384X_WEPFLAGS_HOSTDECRYPT;
0371 
0372     if (val != old_val &&
0373         hostap_set_word(local->dev, HFA384X_RID_CNFWEPFLAGS, val)) {
0374         printk(KERN_DEBUG "Could not write new WEP flags (0x%x)\n",
0375                val);
0376         goto fail;
0377     }
0378 
0379     if (encrypt_type != WEP)
0380         return 0;
0381 
0382     /* 104-bit support seems to require that all the keys are set to the
0383      * same keylen */
0384     keylen = 6; /* first 5 octets */
0385     len = local->crypt_info.crypt[idx]->ops->get_key(keybuf, sizeof(keybuf), NULL,
0386                                local->crypt_info.crypt[idx]->priv);
0387     if (idx >= 0 && idx < WEP_KEYS && len > 5)
0388         keylen = WEP_KEY_LEN + 1; /* first 13 octets */
0389 
0390     for (i = 0; i < WEP_KEYS; i++) {
0391         memset(keybuf, 0, sizeof(keybuf));
0392         if (local->crypt_info.crypt[i]) {
0393             (void) local->crypt_info.crypt[i]->ops->get_key(
0394                 keybuf, sizeof(keybuf),
0395                 NULL, local->crypt_info.crypt[i]->priv);
0396         }
0397         if (local->func->set_rid(local->dev,
0398                      HFA384X_RID_CNFDEFAULTKEY0 + i,
0399                      keybuf, keylen)) {
0400             printk(KERN_DEBUG "Could not set key %d (len=%d)\n",
0401                    i, keylen);
0402             goto fail;
0403         }
0404     }
0405     if (hostap_set_word(local->dev, HFA384X_RID_CNFWEPDEFAULTKEYID, idx)) {
0406         printk(KERN_DEBUG "Could not set default keyid %d\n", idx);
0407         goto fail;
0408     }
0409 
0410     return 0;
0411 
0412  fail:
0413     printk(KERN_DEBUG "%s: encryption setup failed\n", local->dev->name);
0414     return -1;
0415 }
0416 
0417 
0418 int hostap_set_antsel(local_info_t *local)
0419 {
0420     u16 val;
0421     int ret = 0;
0422 
0423     if (local->antsel_tx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
0424         local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
0425                  HFA386X_CR_TX_CONFIGURE,
0426                  NULL, &val) == 0) {
0427         val &= ~(BIT(2) | BIT(1));
0428         switch (local->antsel_tx) {
0429         case HOSTAP_ANTSEL_DIVERSITY:
0430             val |= BIT(1);
0431             break;
0432         case HOSTAP_ANTSEL_LOW:
0433             break;
0434         case HOSTAP_ANTSEL_HIGH:
0435             val |= BIT(2);
0436             break;
0437         }
0438 
0439         if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
0440                      HFA386X_CR_TX_CONFIGURE, &val, NULL)) {
0441             printk(KERN_INFO "%s: setting TX AntSel failed\n",
0442                    local->dev->name);
0443             ret = -1;
0444         }
0445     }
0446 
0447     if (local->antsel_rx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
0448         local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
0449                  HFA386X_CR_RX_CONFIGURE,
0450                  NULL, &val) == 0) {
0451         val &= ~(BIT(1) | BIT(0));
0452         switch (local->antsel_rx) {
0453         case HOSTAP_ANTSEL_DIVERSITY:
0454             break;
0455         case HOSTAP_ANTSEL_LOW:
0456             val |= BIT(0);
0457             break;
0458         case HOSTAP_ANTSEL_HIGH:
0459             val |= BIT(0) | BIT(1);
0460             break;
0461         }
0462 
0463         if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
0464                      HFA386X_CR_RX_CONFIGURE, &val, NULL)) {
0465             printk(KERN_INFO "%s: setting RX AntSel failed\n",
0466                    local->dev->name);
0467             ret = -1;
0468         }
0469     }
0470 
0471     return ret;
0472 }
0473 
0474 
0475 int hostap_set_roaming(local_info_t *local)
0476 {
0477     u16 val;
0478 
0479     switch (local->host_roaming) {
0480     case 1:
0481         val = HFA384X_ROAMING_HOST;
0482         break;
0483     case 2:
0484         val = HFA384X_ROAMING_DISABLED;
0485         break;
0486     case 0:
0487     default:
0488         val = HFA384X_ROAMING_FIRMWARE;
0489         break;
0490     }
0491 
0492     return hostap_set_word(local->dev, HFA384X_RID_CNFROAMINGMODE, val);
0493 }
0494 
0495 
0496 int hostap_set_auth_algs(local_info_t *local)
0497 {
0498     int val = local->auth_algs;
0499     /* At least STA f/w v0.6.2 seems to have issues with cnfAuthentication
0500      * set to include both Open and Shared Key flags. It tries to use
0501      * Shared Key authentication in that case even if WEP keys are not
0502      * configured.. STA f/w v0.7.6 is able to handle such configuration,
0503      * but it is unknown when this was fixed between 0.6.2 .. 0.7.6. */
0504     if (local->sta_fw_ver < PRISM2_FW_VER(0,7,0) &&
0505         val != PRISM2_AUTH_OPEN && val != PRISM2_AUTH_SHARED_KEY)
0506         val = PRISM2_AUTH_OPEN;
0507 
0508     if (hostap_set_word(local->dev, HFA384X_RID_CNFAUTHENTICATION, val)) {
0509         printk(KERN_INFO "%s: cnfAuthentication setting to 0x%x "
0510                "failed\n", local->dev->name, local->auth_algs);
0511         return -EINVAL;
0512     }
0513 
0514     return 0;
0515 }
0516 
0517 
0518 void hostap_dump_rx_header(const char *name, const struct hfa384x_rx_frame *rx)
0519 {
0520     u16 status, fc;
0521 
0522     status = __le16_to_cpu(rx->status);
0523 
0524     printk(KERN_DEBUG "%s: RX status=0x%04x (port=%d, type=%d, "
0525            "fcserr=%d) silence=%d signal=%d rate=%d rxflow=%d; "
0526            "jiffies=%ld\n",
0527            name, status, (status >> 8) & 0x07, status >> 13, status & 1,
0528            rx->silence, rx->signal, rx->rate, rx->rxflow, jiffies);
0529 
0530     fc = __le16_to_cpu(rx->frame_control);
0531     printk(KERN_DEBUG "   FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
0532            "data_len=%d%s%s\n",
0533            fc, (fc & IEEE80211_FCTL_FTYPE) >> 2,
0534            (fc & IEEE80211_FCTL_STYPE) >> 4,
0535            __le16_to_cpu(rx->duration_id), __le16_to_cpu(rx->seq_ctrl),
0536            __le16_to_cpu(rx->data_len),
0537            fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
0538            fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
0539 
0540     printk(KERN_DEBUG "   A1=%pM A2=%pM A3=%pM A4=%pM\n",
0541            rx->addr1, rx->addr2, rx->addr3, rx->addr4);
0542 
0543     printk(KERN_DEBUG "   dst=%pM src=%pM len=%d\n",
0544            rx->dst_addr, rx->src_addr,
0545            __be16_to_cpu(rx->len));
0546 }
0547 
0548 
0549 void hostap_dump_tx_header(const char *name, const struct hfa384x_tx_frame *tx)
0550 {
0551     u16 fc;
0552 
0553     printk(KERN_DEBUG "%s: TX status=0x%04x retry_count=%d tx_rate=%d "
0554            "tx_control=0x%04x; jiffies=%ld\n",
0555            name, __le16_to_cpu(tx->status), tx->retry_count, tx->tx_rate,
0556            __le16_to_cpu(tx->tx_control), jiffies);
0557 
0558     fc = __le16_to_cpu(tx->frame_control);
0559     printk(KERN_DEBUG "   FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
0560            "data_len=%d%s%s\n",
0561            fc, (fc & IEEE80211_FCTL_FTYPE) >> 2,
0562            (fc & IEEE80211_FCTL_STYPE) >> 4,
0563            __le16_to_cpu(tx->duration_id), __le16_to_cpu(tx->seq_ctrl),
0564            __le16_to_cpu(tx->data_len),
0565            fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
0566            fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
0567 
0568     printk(KERN_DEBUG "   A1=%pM A2=%pM A3=%pM A4=%pM\n",
0569            tx->addr1, tx->addr2, tx->addr3, tx->addr4);
0570 
0571     printk(KERN_DEBUG "   dst=%pM src=%pM len=%d\n",
0572            tx->dst_addr, tx->src_addr,
0573            __be16_to_cpu(tx->len));
0574 }
0575 
0576 
0577 static int hostap_80211_header_parse(const struct sk_buff *skb,
0578                      unsigned char *haddr)
0579 {
0580     memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
0581     return ETH_ALEN;
0582 }
0583 
0584 
0585 int hostap_80211_get_hdrlen(__le16 fc)
0586 {
0587     if (ieee80211_is_data(fc) && ieee80211_has_a4 (fc))
0588         return 30; /* Addr4 */
0589     else if (ieee80211_is_cts(fc) || ieee80211_is_ack(fc))
0590         return 10;
0591     else if (ieee80211_is_ctl(fc))
0592         return 16;
0593 
0594     return 24;
0595 }
0596 
0597 
0598 static int prism2_close(struct net_device *dev)
0599 {
0600     struct hostap_interface *iface;
0601     local_info_t *local;
0602 
0603     PDEBUG(DEBUG_FLOW, "%s: prism2_close\n", dev->name);
0604 
0605     iface = netdev_priv(dev);
0606     local = iface->local;
0607 
0608     if (dev == local->ddev) {
0609         prism2_sta_deauth(local, WLAN_REASON_DEAUTH_LEAVING);
0610     }
0611 #ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
0612     if (!local->hostapd && dev == local->dev &&
0613         (!local->func->card_present || local->func->card_present(local)) &&
0614         local->hw_ready && local->ap && local->iw_mode == IW_MODE_MASTER)
0615         hostap_deauth_all_stas(dev, local->ap, 1);
0616 #endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
0617 
0618     if (dev == local->dev) {
0619         local->func->hw_shutdown(dev, HOSTAP_HW_ENABLE_CMDCOMPL);
0620     }
0621 
0622     if (netif_running(dev)) {
0623         netif_stop_queue(dev);
0624         netif_device_detach(dev);
0625     }
0626 
0627     cancel_work_sync(&local->reset_queue);
0628     cancel_work_sync(&local->set_multicast_list_queue);
0629     cancel_work_sync(&local->set_tim_queue);
0630 #ifndef PRISM2_NO_STATION_MODES
0631     cancel_work_sync(&local->info_queue);
0632 #endif
0633     cancel_work_sync(&local->comms_qual_update);
0634 
0635     module_put(local->hw_module);
0636 
0637     local->num_dev_open--;
0638 
0639     if (dev != local->dev && local->dev->flags & IFF_UP &&
0640         local->master_dev_auto_open && local->num_dev_open == 1) {
0641         /* Close master radio interface automatically if it was also
0642          * opened automatically and we are now closing the last
0643          * remaining non-master device. */
0644         dev_close(local->dev);
0645     }
0646 
0647     return 0;
0648 }
0649 
0650 
0651 static int prism2_open(struct net_device *dev)
0652 {
0653     struct hostap_interface *iface;
0654     local_info_t *local;
0655 
0656     PDEBUG(DEBUG_FLOW, "%s: prism2_open\n", dev->name);
0657 
0658     iface = netdev_priv(dev);
0659     local = iface->local;
0660 
0661     if (local->no_pri) {
0662         printk(KERN_DEBUG "%s: could not set interface UP - no PRI "
0663                "f/w\n", dev->name);
0664         return -ENODEV;
0665     }
0666 
0667     if ((local->func->card_present && !local->func->card_present(local)) ||
0668         local->hw_downloading)
0669         return -ENODEV;
0670 
0671     if (!try_module_get(local->hw_module))
0672         return -ENODEV;
0673     local->num_dev_open++;
0674 
0675     if (!local->dev_enabled && local->func->hw_enable(dev, 1)) {
0676         printk(KERN_WARNING "%s: could not enable MAC port\n",
0677                dev->name);
0678         prism2_close(dev);
0679         return -ENODEV;
0680     }
0681     if (!local->dev_enabled)
0682         prism2_callback(local, PRISM2_CALLBACK_ENABLE);
0683     local->dev_enabled = 1;
0684 
0685     if (dev != local->dev && !(local->dev->flags & IFF_UP)) {
0686         /* Master radio interface is needed for all operation, so open
0687          * it automatically when any virtual net_device is opened. */
0688         local->master_dev_auto_open = 1;
0689         dev_open(local->dev, NULL);
0690     }
0691 
0692     netif_device_attach(dev);
0693     netif_start_queue(dev);
0694 
0695     return 0;
0696 }
0697 
0698 
0699 static int prism2_set_mac_address(struct net_device *dev, void *p)
0700 {
0701     struct hostap_interface *iface;
0702     local_info_t *local;
0703     struct list_head *ptr;
0704     struct sockaddr *addr = p;
0705 
0706     iface = netdev_priv(dev);
0707     local = iface->local;
0708 
0709     if (local->func->set_rid(dev, HFA384X_RID_CNFOWNMACADDR, addr->sa_data,
0710                  ETH_ALEN) < 0 || local->func->reset_port(dev))
0711         return -EINVAL;
0712 
0713     read_lock_bh(&local->iface_lock);
0714     list_for_each(ptr, &local->hostap_interfaces) {
0715         iface = list_entry(ptr, struct hostap_interface, list);
0716         eth_hw_addr_set(iface->dev, addr->sa_data);
0717     }
0718     eth_hw_addr_set(local->dev, addr->sa_data);
0719     read_unlock_bh(&local->iface_lock);
0720 
0721     return 0;
0722 }
0723 
0724 
0725 /* TODO: to be further implemented as soon as Prism2 fully supports
0726  *       GroupAddresses and correct documentation is available */
0727 void hostap_set_multicast_list_queue(struct work_struct *work)
0728 {
0729     local_info_t *local =
0730         container_of(work, local_info_t, set_multicast_list_queue);
0731     struct net_device *dev = local->dev;
0732 
0733     if (hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
0734                 local->is_promisc)) {
0735         printk(KERN_INFO "%s: %sabling promiscuous mode failed\n",
0736                dev->name, local->is_promisc ? "en" : "dis");
0737     }
0738 }
0739 
0740 
0741 static void hostap_set_multicast_list(struct net_device *dev)
0742 {
0743 #if 0
0744     /* FIX: promiscuous mode seems to be causing a lot of problems with
0745      * some station firmware versions (FCSErr frames, invalid MACPort, etc.
0746      * corrupted incoming frames). This code is now commented out while the
0747      * problems are investigated. */
0748     struct hostap_interface *iface;
0749     local_info_t *local;
0750 
0751     iface = netdev_priv(dev);
0752     local = iface->local;
0753     if ((dev->flags & IFF_ALLMULTI) || (dev->flags & IFF_PROMISC)) {
0754         local->is_promisc = 1;
0755     } else {
0756         local->is_promisc = 0;
0757     }
0758 
0759     schedule_work(&local->set_multicast_list_queue);
0760 #endif
0761 }
0762 
0763 
0764 static void prism2_tx_timeout(struct net_device *dev, unsigned int txqueue)
0765 {
0766     struct hostap_interface *iface;
0767     local_info_t *local;
0768     struct hfa384x_regs regs;
0769 
0770     iface = netdev_priv(dev);
0771     local = iface->local;
0772 
0773     printk(KERN_WARNING "%s Tx timed out! Resetting card\n", dev->name);
0774     netif_stop_queue(local->dev);
0775 
0776     local->func->read_regs(dev, &regs);
0777     printk(KERN_DEBUG "%s: CMD=%04x EVSTAT=%04x "
0778            "OFFSET0=%04x OFFSET1=%04x SWSUPPORT0=%04x\n",
0779            dev->name, regs.cmd, regs.evstat, regs.offset0, regs.offset1,
0780            regs.swsupport0);
0781 
0782     local->func->schedule_reset(local);
0783 }
0784 
0785 const struct header_ops hostap_80211_ops = {
0786     .create     = eth_header,
0787     .cache      = eth_header_cache,
0788     .cache_update   = eth_header_cache_update,
0789     .parse      = hostap_80211_header_parse,
0790 };
0791 EXPORT_SYMBOL(hostap_80211_ops);
0792 
0793 
0794 static const struct net_device_ops hostap_netdev_ops = {
0795     .ndo_start_xmit     = hostap_data_start_xmit,
0796 
0797     .ndo_open       = prism2_open,
0798     .ndo_stop       = prism2_close,
0799     .ndo_do_ioctl       = hostap_ioctl,
0800     .ndo_siocdevprivate = hostap_siocdevprivate,
0801     .ndo_set_mac_address    = prism2_set_mac_address,
0802     .ndo_set_rx_mode    = hostap_set_multicast_list,
0803     .ndo_tx_timeout     = prism2_tx_timeout,
0804     .ndo_validate_addr  = eth_validate_addr,
0805 };
0806 
0807 static const struct net_device_ops hostap_mgmt_netdev_ops = {
0808     .ndo_start_xmit     = hostap_mgmt_start_xmit,
0809 
0810     .ndo_open       = prism2_open,
0811     .ndo_stop       = prism2_close,
0812     .ndo_do_ioctl       = hostap_ioctl,
0813     .ndo_siocdevprivate = hostap_siocdevprivate,
0814     .ndo_set_mac_address    = prism2_set_mac_address,
0815     .ndo_set_rx_mode    = hostap_set_multicast_list,
0816     .ndo_tx_timeout     = prism2_tx_timeout,
0817     .ndo_validate_addr  = eth_validate_addr,
0818 };
0819 
0820 static const struct net_device_ops hostap_master_ops = {
0821     .ndo_start_xmit     = hostap_master_start_xmit,
0822 
0823     .ndo_open       = prism2_open,
0824     .ndo_stop       = prism2_close,
0825     .ndo_do_ioctl       = hostap_ioctl,
0826     .ndo_siocdevprivate = hostap_siocdevprivate,
0827     .ndo_set_mac_address    = prism2_set_mac_address,
0828     .ndo_set_rx_mode    = hostap_set_multicast_list,
0829     .ndo_tx_timeout     = prism2_tx_timeout,
0830     .ndo_validate_addr  = eth_validate_addr,
0831 };
0832 
0833 void hostap_setup_dev(struct net_device *dev, local_info_t *local,
0834               int type)
0835 {
0836     struct hostap_interface *iface;
0837 
0838     iface = netdev_priv(dev);
0839     ether_setup(dev);
0840     dev->min_mtu = PRISM2_MIN_MTU;
0841     dev->max_mtu = PRISM2_MAX_MTU;
0842     dev->priv_flags &= ~IFF_TX_SKB_SHARING;
0843 
0844     /* kernel callbacks */
0845     if (iface) {
0846         /* Currently, we point to the proper spy_data only on
0847          * the main_dev. This could be fixed. Jean II */
0848         iface->wireless_data.spy_data = &iface->spy_data;
0849         dev->wireless_data = &iface->wireless_data;
0850     }
0851     dev->wireless_handlers = &hostap_iw_handler_def;
0852     dev->watchdog_timeo = TX_TIMEOUT;
0853 
0854     switch(type) {
0855     case HOSTAP_INTERFACE_AP:
0856         dev->priv_flags |= IFF_NO_QUEUE;    /* use main radio device queue */
0857         dev->netdev_ops = &hostap_mgmt_netdev_ops;
0858         dev->type = ARPHRD_IEEE80211;
0859         dev->header_ops = &hostap_80211_ops;
0860         break;
0861     case HOSTAP_INTERFACE_MASTER:
0862         dev->netdev_ops = &hostap_master_ops;
0863         break;
0864     default:
0865         dev->priv_flags |= IFF_NO_QUEUE;    /* use main radio device queue */
0866         dev->netdev_ops = &hostap_netdev_ops;
0867     }
0868 
0869     dev->mtu = local->mtu;
0870 
0871 
0872     dev->ethtool_ops = &prism2_ethtool_ops;
0873 
0874 }
0875 
0876 static int hostap_enable_hostapd(local_info_t *local, int rtnl_locked)
0877 {
0878     struct net_device *dev = local->dev;
0879 
0880     if (local->apdev)
0881         return -EEXIST;
0882 
0883     printk(KERN_DEBUG "%s: enabling hostapd mode\n", dev->name);
0884 
0885     local->apdev = hostap_add_interface(local, HOSTAP_INTERFACE_AP,
0886                         rtnl_locked, local->ddev->name,
0887                         "ap");
0888     if (local->apdev == NULL)
0889         return -ENOMEM;
0890 
0891     return 0;
0892 }
0893 
0894 
0895 static int hostap_disable_hostapd(local_info_t *local, int rtnl_locked)
0896 {
0897     struct net_device *dev = local->dev;
0898 
0899     printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
0900 
0901     hostap_remove_interface(local->apdev, rtnl_locked, 1);
0902     local->apdev = NULL;
0903 
0904     return 0;
0905 }
0906 
0907 
0908 static int hostap_enable_hostapd_sta(local_info_t *local, int rtnl_locked)
0909 {
0910     struct net_device *dev = local->dev;
0911 
0912     if (local->stadev)
0913         return -EEXIST;
0914 
0915     printk(KERN_DEBUG "%s: enabling hostapd STA mode\n", dev->name);
0916 
0917     local->stadev = hostap_add_interface(local, HOSTAP_INTERFACE_STA,
0918                          rtnl_locked, local->ddev->name,
0919                          "sta");
0920     if (local->stadev == NULL)
0921         return -ENOMEM;
0922 
0923     return 0;
0924 }
0925 
0926 
0927 static int hostap_disable_hostapd_sta(local_info_t *local, int rtnl_locked)
0928 {
0929     struct net_device *dev = local->dev;
0930 
0931     printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
0932 
0933     hostap_remove_interface(local->stadev, rtnl_locked, 1);
0934     local->stadev = NULL;
0935 
0936     return 0;
0937 }
0938 
0939 
0940 int hostap_set_hostapd(local_info_t *local, int val, int rtnl_locked)
0941 {
0942     int ret;
0943 
0944     if (val < 0 || val > 1)
0945         return -EINVAL;
0946 
0947     if (local->hostapd == val)
0948         return 0;
0949 
0950     if (val) {
0951         ret = hostap_enable_hostapd(local, rtnl_locked);
0952         if (ret == 0)
0953             local->hostapd = 1;
0954     } else {
0955         local->hostapd = 0;
0956         ret = hostap_disable_hostapd(local, rtnl_locked);
0957         if (ret != 0)
0958             local->hostapd = 1;
0959     }
0960 
0961     return ret;
0962 }
0963 
0964 
0965 int hostap_set_hostapd_sta(local_info_t *local, int val, int rtnl_locked)
0966 {
0967     int ret;
0968 
0969     if (val < 0 || val > 1)
0970         return -EINVAL;
0971 
0972     if (local->hostapd_sta == val)
0973         return 0;
0974 
0975     if (val) {
0976         ret = hostap_enable_hostapd_sta(local, rtnl_locked);
0977         if (ret == 0)
0978             local->hostapd_sta = 1;
0979     } else {
0980         local->hostapd_sta = 0;
0981         ret = hostap_disable_hostapd_sta(local, rtnl_locked);
0982         if (ret != 0)
0983             local->hostapd_sta = 1;
0984     }
0985 
0986 
0987     return ret;
0988 }
0989 
0990 
0991 int prism2_update_comms_qual(struct net_device *dev)
0992 {
0993     struct hostap_interface *iface;
0994     local_info_t *local;
0995     int ret = 0;
0996     struct hfa384x_comms_quality sq;
0997 
0998     iface = netdev_priv(dev);
0999     local = iface->local;
1000     if (!local->sta_fw_ver)
1001         ret = -1;
1002     else if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1)) {
1003         if (local->func->get_rid(local->dev,
1004                      HFA384X_RID_DBMCOMMSQUALITY,
1005                      &sq, sizeof(sq), 1) >= 0) {
1006             local->comms_qual = (s16) le16_to_cpu(sq.comm_qual);
1007             local->avg_signal = (s16) le16_to_cpu(sq.signal_level);
1008             local->avg_noise = (s16) le16_to_cpu(sq.noise_level);
1009             local->last_comms_qual_update = jiffies;
1010         } else
1011             ret = -1;
1012     } else {
1013         if (local->func->get_rid(local->dev, HFA384X_RID_COMMSQUALITY,
1014                      &sq, sizeof(sq), 1) >= 0) {
1015             local->comms_qual = le16_to_cpu(sq.comm_qual);
1016             local->avg_signal = HFA384X_LEVEL_TO_dBm(
1017                 le16_to_cpu(sq.signal_level));
1018             local->avg_noise = HFA384X_LEVEL_TO_dBm(
1019                 le16_to_cpu(sq.noise_level));
1020             local->last_comms_qual_update = jiffies;
1021         } else
1022             ret = -1;
1023     }
1024 
1025     return ret;
1026 }
1027 
1028 
1029 int prism2_sta_send_mgmt(local_info_t *local, u8 *dst, u16 stype,
1030              u8 *body, size_t bodylen)
1031 {
1032     struct sk_buff *skb;
1033     struct hostap_ieee80211_mgmt *mgmt;
1034     struct hostap_skb_tx_data *meta;
1035     struct net_device *dev = local->dev;
1036 
1037     skb = dev_alloc_skb(IEEE80211_MGMT_HDR_LEN + bodylen);
1038     if (skb == NULL)
1039         return -ENOMEM;
1040 
1041     mgmt = skb_put_zero(skb, IEEE80211_MGMT_HDR_LEN);
1042     mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1043     memcpy(mgmt->da, dst, ETH_ALEN);
1044     memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1045     memcpy(mgmt->bssid, dst, ETH_ALEN);
1046     if (body)
1047         skb_put_data(skb, body, bodylen);
1048 
1049     meta = (struct hostap_skb_tx_data *) skb->cb;
1050     memset(meta, 0, sizeof(*meta));
1051     meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
1052     meta->iface = netdev_priv(dev);
1053 
1054     skb->dev = dev;
1055     skb_reset_mac_header(skb);
1056     skb_reset_network_header(skb);
1057     dev_queue_xmit(skb);
1058 
1059     return 0;
1060 }
1061 
1062 
1063 int prism2_sta_deauth(local_info_t *local, u16 reason)
1064 {
1065     union iwreq_data wrqu;
1066     int ret;
1067     __le16 val = cpu_to_le16(reason);
1068 
1069     if (local->iw_mode != IW_MODE_INFRA ||
1070         is_zero_ether_addr(local->bssid) ||
1071         ether_addr_equal(local->bssid, "\x44\x44\x44\x44\x44\x44"))
1072         return 0;
1073 
1074     ret = prism2_sta_send_mgmt(local, local->bssid, IEEE80211_STYPE_DEAUTH,
1075                    (u8 *) &val, 2);
1076     eth_zero_addr(wrqu.ap_addr.sa_data);
1077     wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
1078     return ret;
1079 }
1080 
1081 
1082 struct proc_dir_entry *hostap_proc;
1083 
1084 static int __init hostap_init(void)
1085 {
1086     if (init_net.proc_net != NULL) {
1087         hostap_proc = proc_mkdir("hostap", init_net.proc_net);
1088         if (!hostap_proc)
1089             printk(KERN_WARNING "Failed to mkdir "
1090                    "/proc/net/hostap\n");
1091     } else
1092         hostap_proc = NULL;
1093 
1094     return 0;
1095 }
1096 
1097 
1098 static void __exit hostap_exit(void)
1099 {
1100     if (hostap_proc != NULL) {
1101         hostap_proc = NULL;
1102         remove_proc_entry("hostap", init_net.proc_net);
1103     }
1104 }
1105 
1106 
1107 EXPORT_SYMBOL(hostap_set_word);
1108 EXPORT_SYMBOL(hostap_set_string);
1109 EXPORT_SYMBOL(hostap_get_porttype);
1110 EXPORT_SYMBOL(hostap_set_encryption);
1111 EXPORT_SYMBOL(hostap_set_antsel);
1112 EXPORT_SYMBOL(hostap_set_roaming);
1113 EXPORT_SYMBOL(hostap_set_auth_algs);
1114 EXPORT_SYMBOL(hostap_dump_rx_header);
1115 EXPORT_SYMBOL(hostap_dump_tx_header);
1116 EXPORT_SYMBOL(hostap_80211_get_hdrlen);
1117 EXPORT_SYMBOL(hostap_setup_dev);
1118 EXPORT_SYMBOL(hostap_set_multicast_list_queue);
1119 EXPORT_SYMBOL(hostap_set_hostapd);
1120 EXPORT_SYMBOL(hostap_set_hostapd_sta);
1121 EXPORT_SYMBOL(hostap_add_interface);
1122 EXPORT_SYMBOL(hostap_remove_interface);
1123 EXPORT_SYMBOL(prism2_update_comms_qual);
1124 
1125 module_init(hostap_init);
1126 module_exit(hostap_exit);