linux/drivers/net/wireless/hostap/hostap_main.c
<<
>>
Prefs
   1/*
   2 * Host AP (software wireless LAN access point) driver for
   3 * Intersil Prism2/2.5/3 - hostap.o module, common routines
   4 *
   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>
   8 *
   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.
  13 */
  14
  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/lib80211.h>
  30#include <asm/uaccess.h>
  31
  32#include "hostap_wlan.h"
  33#include "hostap_80211.h"
  34#include "hostap_ap.h"
  35#include "hostap.h"
  36
  37MODULE_AUTHOR("Jouni Malinen");
  38MODULE_DESCRIPTION("Host AP common routines");
  39MODULE_LICENSE("GPL");
  40
  41#define TX_TIMEOUT (2 * HZ)
  42
  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 */))
  47
  48
  49struct net_device * hostap_add_interface(struct local_info *local,
  50                                         int type, int rtnl_locked,
  51                                         const char *prefix,
  52                                         const char *name)
  53{
  54        struct net_device *dev, *mdev;
  55        struct hostap_interface *iface;
  56        int ret;
  57
  58        dev = alloc_etherdev(sizeof(struct hostap_interface));
  59        if (dev == NULL)
  60                return NULL;
  61
  62        iface = netdev_priv(dev);
  63        iface->dev = dev;
  64        iface->local = local;
  65        iface->type = type;
  66        list_add(&iface->list, &local->hostap_interfaces);
  67
  68        mdev = local->dev;
  69        memcpy(dev->dev_addr, mdev->dev_addr, ETH_ALEN);
  70        dev->base_addr = mdev->base_addr;
  71        dev->irq = mdev->irq;
  72        dev->mem_start = mdev->mem_start;
  73        dev->mem_end = mdev->mem_end;
  74
  75        hostap_setup_dev(dev, local, type);
  76        dev->destructor = free_netdev;
  77
  78        sprintf(dev->name, "%s%s", prefix, name);
  79        if (!rtnl_locked)
  80                rtnl_lock();
  81
  82        SET_NETDEV_DEV(dev, mdev->dev.parent);
  83        ret = register_netdevice(dev);
  84
  85        if (!rtnl_locked)
  86                rtnl_unlock();
  87
  88        if (ret < 0) {
  89                printk(KERN_WARNING "%s: failed to add new netdevice!\n",
  90                       dev->name);
  91                free_netdev(dev);
  92                return NULL;
  93        }
  94
  95        printk(KERN_DEBUG "%s: registered netdevice %s\n",
  96               mdev->name, dev->name);
  97
  98        return dev;
  99}
 100
 101
 102void hostap_remove_interface(struct net_device *dev, int rtnl_locked,
 103                             int remove_from_list)
 104{
 105        struct hostap_interface *iface;
 106
 107        if (!dev)
 108                return;
 109
 110        iface = netdev_priv(dev);
 111
 112        if (remove_from_list) {
 113                list_del(&iface->list);
 114        }
 115
 116        if (dev == iface->local->ddev)
 117                iface->local->ddev = NULL;
 118        else if (dev == iface->local->apdev)
 119                iface->local->apdev = NULL;
 120        else if (dev == iface->local->stadev)
 121                iface->local->stadev = NULL;
 122
 123        if (rtnl_locked)
 124                unregister_netdevice(dev);
 125        else
 126                unregister_netdev(dev);
 127
 128        /* dev->destructor = free_netdev() will free the device data, including
 129         * private data, when removing the device */
 130}
 131
 132
 133static inline int prism2_wds_special_addr(u8 *addr)
 134{
 135        if (addr[0] || addr[1] || addr[2] || addr[3] || addr[4] || addr[5])
 136                return 0;
 137
 138        return 1;
 139}
 140
 141
 142int prism2_wds_add(local_info_t *local, u8 *remote_addr,
 143                   int rtnl_locked)
 144{
 145        struct net_device *dev;
 146        struct list_head *ptr;
 147        struct hostap_interface *iface, *empty, *match;
 148
 149        empty = match = NULL;
 150        read_lock_bh(&local->iface_lock);
 151        list_for_each(ptr, &local->hostap_interfaces) {
 152                iface = list_entry(ptr, struct hostap_interface, list);
 153                if (iface->type != HOSTAP_INTERFACE_WDS)
 154                        continue;
 155
 156                if (prism2_wds_special_addr(iface->u.wds.remote_addr))
 157                        empty = iface;
 158                else if (memcmp(iface->u.wds.remote_addr, remote_addr,
 159                                ETH_ALEN) == 0) {
 160                        match = iface;
 161                        break;
 162                }
 163        }
 164        if (!match && empty && !prism2_wds_special_addr(remote_addr)) {
 165                /* take pre-allocated entry into use */
 166                memcpy(empty->u.wds.remote_addr, remote_addr, ETH_ALEN);
 167                read_unlock_bh(&local->iface_lock);
 168                printk(KERN_DEBUG "%s: using pre-allocated WDS netdevice %s\n",
 169                       local->dev->name, empty->dev->name);
 170                return 0;
 171        }
 172        read_unlock_bh(&local->iface_lock);
 173
 174        if (!prism2_wds_special_addr(remote_addr)) {
 175                if (match)
 176                        return -EEXIST;
 177                hostap_add_sta(local->ap, remote_addr);
 178        }
 179
 180        if (local->wds_connections >= local->wds_max_connections)
 181                return -ENOBUFS;
 182
 183        /* verify that there is room for wds# postfix in the interface name */
 184        if (strlen(local->dev->name) >= IFNAMSIZ - 5) {
 185                printk(KERN_DEBUG "'%s' too long base device name\n",
 186                       local->dev->name);
 187                return -EINVAL;
 188        }
 189
 190        dev = hostap_add_interface(local, HOSTAP_INTERFACE_WDS, rtnl_locked,
 191                                   local->ddev->name, "wds%d");
 192        if (dev == NULL)
 193                return -ENOMEM;
 194
 195        iface = netdev_priv(dev);
 196        memcpy(iface->u.wds.remote_addr, remote_addr, ETH_ALEN);
 197
 198        local->wds_connections++;
 199
 200        return 0;
 201}
 202
 203
 204int prism2_wds_del(local_info_t *local, u8 *remote_addr,
 205                   int rtnl_locked, int do_not_remove)
 206{
 207        unsigned long flags;
 208        struct list_head *ptr;
 209        struct hostap_interface *iface, *selected = NULL;
 210
 211        write_lock_irqsave(&local->iface_lock, flags);
 212        list_for_each(ptr, &local->hostap_interfaces) {
 213                iface = list_entry(ptr, struct hostap_interface, list);
 214                if (iface->type != HOSTAP_INTERFACE_WDS)
 215                        continue;
 216
 217                if (memcmp(iface->u.wds.remote_addr, remote_addr,
 218                           ETH_ALEN) == 0) {
 219                        selected = iface;
 220                        break;
 221                }
 222        }
 223        if (selected && !do_not_remove)
 224                list_del(&selected->list);
 225        write_unlock_irqrestore(&local->iface_lock, flags);
 226
 227        if (selected) {
 228                if (do_not_remove)
 229                        memset(selected->u.wds.remote_addr, 0, ETH_ALEN);
 230                else {
 231                        hostap_remove_interface(selected->dev, rtnl_locked, 0);
 232                        local->wds_connections--;
 233                }
 234        }
 235
 236        return selected ? 0 : -ENODEV;
 237}
 238
 239
 240u16 hostap_tx_callback_register(local_info_t *local,
 241                                void (*func)(struct sk_buff *, int ok, void *),
 242                                void *data)
 243{
 244        unsigned long flags;
 245        struct hostap_tx_callback_info *entry;
 246
 247        entry = kmalloc(sizeof(*entry), GFP_KERNEL);
 248        if (entry == NULL)
 249                return 0;
 250
 251        entry->func = func;
 252        entry->data = data;
 253
 254        spin_lock_irqsave(&local->lock, flags);
 255        entry->idx = local->tx_callback ? local->tx_callback->idx + 1 : 1;
 256        entry->next = local->tx_callback;
 257        local->tx_callback = entry;
 258        spin_unlock_irqrestore(&local->lock, flags);
 259
 260        return entry->idx;
 261}
 262
 263
 264int hostap_tx_callback_unregister(local_info_t *local, u16 idx)
 265{
 266        unsigned long flags;
 267        struct hostap_tx_callback_info *cb, *prev = NULL;
 268
 269        spin_lock_irqsave(&local->lock, flags);
 270        cb = local->tx_callback;
 271        while (cb != NULL && cb->idx != idx) {
 272                prev = cb;
 273                cb = cb->next;
 274        }
 275        if (cb) {
 276                if (prev == NULL)
 277                        local->tx_callback = cb->next;
 278                else
 279                        prev->next = cb->next;
 280                kfree(cb);
 281        }
 282        spin_unlock_irqrestore(&local->lock, flags);
 283
 284        return cb ? 0 : -1;
 285}
 286
 287
 288/* val is in host byte order */
 289int hostap_set_word(struct net_device *dev, int rid, u16 val)
 290{
 291        struct hostap_interface *iface;
 292        __le16 tmp = cpu_to_le16(val);
 293        iface = netdev_priv(dev);
 294        return iface->local->func->set_rid(dev, rid, &tmp, 2);
 295}
 296
 297
 298int hostap_set_string(struct net_device *dev, int rid, const char *val)
 299{
 300        struct hostap_interface *iface;
 301        char buf[MAX_SSID_LEN + 2];
 302        int len;
 303
 304        iface = netdev_priv(dev);
 305        len = strlen(val);
 306        if (len > MAX_SSID_LEN)
 307                return -1;
 308        memset(buf, 0, sizeof(buf));
 309        buf[0] = len; /* little endian 16 bit word */
 310        memcpy(buf + 2, val, len);
 311
 312        return iface->local->func->set_rid(dev, rid, &buf, MAX_SSID_LEN + 2);
 313}
 314
 315
 316u16 hostap_get_porttype(local_info_t *local)
 317{
 318        if (local->iw_mode == IW_MODE_ADHOC && local->pseudo_adhoc)
 319                return HFA384X_PORTTYPE_PSEUDO_IBSS;
 320        if (local->iw_mode == IW_MODE_ADHOC)
 321                return HFA384X_PORTTYPE_IBSS;
 322        if (local->iw_mode == IW_MODE_INFRA)
 323                return HFA384X_PORTTYPE_BSS;
 324        if (local->iw_mode == IW_MODE_REPEAT)
 325                return HFA384X_PORTTYPE_WDS;
 326        if (local->iw_mode == IW_MODE_MONITOR)
 327                return HFA384X_PORTTYPE_PSEUDO_IBSS;
 328        return HFA384X_PORTTYPE_HOSTAP;
 329}
 330
 331
 332int hostap_set_encryption(local_info_t *local)
 333{
 334        u16 val, old_val;
 335        int i, keylen, len, idx;
 336        char keybuf[WEP_KEY_LEN + 1];
 337        enum { NONE, WEP, OTHER } encrypt_type;
 338
 339        idx = local->crypt_info.tx_keyidx;
 340        if (local->crypt_info.crypt[idx] == NULL ||
 341            local->crypt_info.crypt[idx]->ops == NULL)
 342                encrypt_type = NONE;
 343        else if (strcmp(local->crypt_info.crypt[idx]->ops->name, "WEP") == 0)
 344                encrypt_type = WEP;
 345        else
 346                encrypt_type = OTHER;
 347
 348        if (local->func->get_rid(local->dev, HFA384X_RID_CNFWEPFLAGS, &val, 2,
 349                                 1) < 0) {
 350                printk(KERN_DEBUG "Could not read current WEP flags.\n");
 351                goto fail;
 352        }
 353        le16_to_cpus(&val);
 354        old_val = val;
 355
 356        if (encrypt_type != NONE || local->privacy_invoked)
 357                val |= HFA384X_WEPFLAGS_PRIVACYINVOKED;
 358        else
 359                val &= ~HFA384X_WEPFLAGS_PRIVACYINVOKED;
 360
 361        if (local->open_wep || encrypt_type == NONE ||
 362            ((local->ieee_802_1x || local->wpa) && local->host_decrypt))
 363                val &= ~HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
 364        else
 365                val |= HFA384X_WEPFLAGS_EXCLUDEUNENCRYPTED;
 366
 367        if ((encrypt_type != NONE || local->privacy_invoked) &&
 368            (encrypt_type == OTHER || local->host_encrypt))
 369                val |= HFA384X_WEPFLAGS_HOSTENCRYPT;
 370        else
 371                val &= ~HFA384X_WEPFLAGS_HOSTENCRYPT;
 372        if ((encrypt_type != NONE || local->privacy_invoked) &&
 373            (encrypt_type == OTHER || local->host_decrypt))
 374                val |= HFA384X_WEPFLAGS_HOSTDECRYPT;
 375        else
 376                val &= ~HFA384X_WEPFLAGS_HOSTDECRYPT;
 377
 378        if (val != old_val &&
 379            hostap_set_word(local->dev, HFA384X_RID_CNFWEPFLAGS, val)) {
 380                printk(KERN_DEBUG "Could not write new WEP flags (0x%x)\n",
 381                       val);
 382                goto fail;
 383        }
 384
 385        if (encrypt_type != WEP)
 386                return 0;
 387
 388        /* 104-bit support seems to require that all the keys are set to the
 389         * same keylen */
 390        keylen = 6; /* first 5 octets */
 391        len = local->crypt_info.crypt[idx]->ops->get_key(keybuf, sizeof(keybuf), NULL,
 392                                                           local->crypt_info.crypt[idx]->priv);
 393        if (idx >= 0 && idx < WEP_KEYS && len > 5)
 394                keylen = WEP_KEY_LEN + 1; /* first 13 octets */
 395
 396        for (i = 0; i < WEP_KEYS; i++) {
 397                memset(keybuf, 0, sizeof(keybuf));
 398                if (local->crypt_info.crypt[i]) {
 399                        (void) local->crypt_info.crypt[i]->ops->get_key(
 400                                keybuf, sizeof(keybuf),
 401                                NULL, local->crypt_info.crypt[i]->priv);
 402                }
 403                if (local->func->set_rid(local->dev,
 404                                         HFA384X_RID_CNFDEFAULTKEY0 + i,
 405                                         keybuf, keylen)) {
 406                        printk(KERN_DEBUG "Could not set key %d (len=%d)\n",
 407                               i, keylen);
 408                        goto fail;
 409                }
 410        }
 411        if (hostap_set_word(local->dev, HFA384X_RID_CNFWEPDEFAULTKEYID, idx)) {
 412                printk(KERN_DEBUG "Could not set default keyid %d\n", idx);
 413                goto fail;
 414        }
 415
 416        return 0;
 417
 418 fail:
 419        printk(KERN_DEBUG "%s: encryption setup failed\n", local->dev->name);
 420        return -1;
 421}
 422
 423
 424int hostap_set_antsel(local_info_t *local)
 425{
 426        u16 val;
 427        int ret = 0;
 428
 429        if (local->antsel_tx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
 430            local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
 431                             HFA386X_CR_TX_CONFIGURE,
 432                             NULL, &val) == 0) {
 433                val &= ~(BIT(2) | BIT(1));
 434                switch (local->antsel_tx) {
 435                case HOSTAP_ANTSEL_DIVERSITY:
 436                        val |= BIT(1);
 437                        break;
 438                case HOSTAP_ANTSEL_LOW:
 439                        break;
 440                case HOSTAP_ANTSEL_HIGH:
 441                        val |= BIT(2);
 442                        break;
 443                }
 444
 445                if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
 446                                     HFA386X_CR_TX_CONFIGURE, &val, NULL)) {
 447                        printk(KERN_INFO "%s: setting TX AntSel failed\n",
 448                               local->dev->name);
 449                        ret = -1;
 450                }
 451        }
 452
 453        if (local->antsel_rx != HOSTAP_ANTSEL_DO_NOT_TOUCH &&
 454            local->func->cmd(local->dev, HFA384X_CMDCODE_READMIF,
 455                             HFA386X_CR_RX_CONFIGURE,
 456                             NULL, &val) == 0) {
 457                val &= ~(BIT(1) | BIT(0));
 458                switch (local->antsel_rx) {
 459                case HOSTAP_ANTSEL_DIVERSITY:
 460                        break;
 461                case HOSTAP_ANTSEL_LOW:
 462                        val |= BIT(0);
 463                        break;
 464                case HOSTAP_ANTSEL_HIGH:
 465                        val |= BIT(0) | BIT(1);
 466                        break;
 467                }
 468
 469                if (local->func->cmd(local->dev, HFA384X_CMDCODE_WRITEMIF,
 470                                     HFA386X_CR_RX_CONFIGURE, &val, NULL)) {
 471                        printk(KERN_INFO "%s: setting RX AntSel failed\n",
 472                               local->dev->name);
 473                        ret = -1;
 474                }
 475        }
 476
 477        return ret;
 478}
 479
 480
 481int hostap_set_roaming(local_info_t *local)
 482{
 483        u16 val;
 484
 485        switch (local->host_roaming) {
 486        case 1:
 487                val = HFA384X_ROAMING_HOST;
 488                break;
 489        case 2:
 490                val = HFA384X_ROAMING_DISABLED;
 491                break;
 492        case 0:
 493        default:
 494                val = HFA384X_ROAMING_FIRMWARE;
 495                break;
 496        }
 497
 498        return hostap_set_word(local->dev, HFA384X_RID_CNFROAMINGMODE, val);
 499}
 500
 501
 502int hostap_set_auth_algs(local_info_t *local)
 503{
 504        int val = local->auth_algs;
 505        /* At least STA f/w v0.6.2 seems to have issues with cnfAuthentication
 506         * set to include both Open and Shared Key flags. It tries to use
 507         * Shared Key authentication in that case even if WEP keys are not
 508         * configured.. STA f/w v0.7.6 is able to handle such configuration,
 509         * but it is unknown when this was fixed between 0.6.2 .. 0.7.6. */
 510        if (local->sta_fw_ver < PRISM2_FW_VER(0,7,0) &&
 511            val != PRISM2_AUTH_OPEN && val != PRISM2_AUTH_SHARED_KEY)
 512                val = PRISM2_AUTH_OPEN;
 513
 514        if (hostap_set_word(local->dev, HFA384X_RID_CNFAUTHENTICATION, val)) {
 515                printk(KERN_INFO "%s: cnfAuthentication setting to 0x%x "
 516                       "failed\n", local->dev->name, local->auth_algs);
 517                return -EINVAL;
 518        }
 519
 520        return 0;
 521}
 522
 523
 524void hostap_dump_rx_header(const char *name, const struct hfa384x_rx_frame *rx)
 525{
 526        u16 status, fc;
 527
 528        status = __le16_to_cpu(rx->status);
 529
 530        printk(KERN_DEBUG "%s: RX status=0x%04x (port=%d, type=%d, "
 531               "fcserr=%d) silence=%d signal=%d rate=%d rxflow=%d; "
 532               "jiffies=%ld\n",
 533               name, status, (status >> 8) & 0x07, status >> 13, status & 1,
 534               rx->silence, rx->signal, rx->rate, rx->rxflow, jiffies);
 535
 536        fc = __le16_to_cpu(rx->frame_control);
 537        printk(KERN_DEBUG "   FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
 538               "data_len=%d%s%s\n",
 539               fc, (fc & IEEE80211_FCTL_FTYPE) >> 2,
 540               (fc & IEEE80211_FCTL_STYPE) >> 4,
 541               __le16_to_cpu(rx->duration_id), __le16_to_cpu(rx->seq_ctrl),
 542               __le16_to_cpu(rx->data_len),
 543               fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
 544               fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
 545
 546        printk(KERN_DEBUG "   A1=%pM A2=%pM A3=%pM A4=%pM\n",
 547               rx->addr1, rx->addr2, rx->addr3, rx->addr4);
 548
 549        printk(KERN_DEBUG "   dst=%pM src=%pM len=%d\n",
 550               rx->dst_addr, rx->src_addr,
 551               __be16_to_cpu(rx->len));
 552}
 553
 554
 555void hostap_dump_tx_header(const char *name, const struct hfa384x_tx_frame *tx)
 556{
 557        u16 fc;
 558
 559        printk(KERN_DEBUG "%s: TX status=0x%04x retry_count=%d tx_rate=%d "
 560               "tx_control=0x%04x; jiffies=%ld\n",
 561               name, __le16_to_cpu(tx->status), tx->retry_count, tx->tx_rate,
 562               __le16_to_cpu(tx->tx_control), jiffies);
 563
 564        fc = __le16_to_cpu(tx->frame_control);
 565        printk(KERN_DEBUG "   FC=0x%04x (type=%d:%d) dur=0x%04x seq=0x%04x "
 566               "data_len=%d%s%s\n",
 567               fc, (fc & IEEE80211_FCTL_FTYPE) >> 2,
 568               (fc & IEEE80211_FCTL_STYPE) >> 4,
 569               __le16_to_cpu(tx->duration_id), __le16_to_cpu(tx->seq_ctrl),
 570               __le16_to_cpu(tx->data_len),
 571               fc & IEEE80211_FCTL_TODS ? " [ToDS]" : "",
 572               fc & IEEE80211_FCTL_FROMDS ? " [FromDS]" : "");
 573
 574        printk(KERN_DEBUG "   A1=%pM A2=%pM A3=%pM A4=%pM\n",
 575               tx->addr1, tx->addr2, tx->addr3, tx->addr4);
 576
 577        printk(KERN_DEBUG "   dst=%pM src=%pM len=%d\n",
 578               tx->dst_addr, tx->src_addr,
 579               __be16_to_cpu(tx->len));
 580}
 581
 582
 583static int hostap_80211_header_parse(const struct sk_buff *skb,
 584                                     unsigned char *haddr)
 585{
 586        memcpy(haddr, skb_mac_header(skb) + 10, ETH_ALEN); /* addr2 */
 587        return ETH_ALEN;
 588}
 589
 590
 591int hostap_80211_get_hdrlen(__le16 fc)
 592{
 593        if (ieee80211_is_data(fc) && ieee80211_has_a4 (fc))
 594                return 30; /* Addr4 */
 595        else if (ieee80211_is_cts(fc) || ieee80211_is_ack(fc))
 596                return 10;
 597        else if (ieee80211_is_ctl(fc))
 598                return 16;
 599
 600        return 24;
 601}
 602
 603
 604static int prism2_close(struct net_device *dev)
 605{
 606        struct hostap_interface *iface;
 607        local_info_t *local;
 608
 609        PDEBUG(DEBUG_FLOW, "%s: prism2_close\n", dev->name);
 610
 611        iface = netdev_priv(dev);
 612        local = iface->local;
 613
 614        if (dev == local->ddev) {
 615                prism2_sta_deauth(local, WLAN_REASON_DEAUTH_LEAVING);
 616        }
 617#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
 618        if (!local->hostapd && dev == local->dev &&
 619            (!local->func->card_present || local->func->card_present(local)) &&
 620            local->hw_ready && local->ap && local->iw_mode == IW_MODE_MASTER)
 621                hostap_deauth_all_stas(dev, local->ap, 1);
 622#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
 623
 624        if (dev == local->dev) {
 625                local->func->hw_shutdown(dev, HOSTAP_HW_ENABLE_CMDCOMPL);
 626        }
 627
 628        if (netif_running(dev)) {
 629                netif_stop_queue(dev);
 630                netif_device_detach(dev);
 631        }
 632
 633        cancel_work_sync(&local->reset_queue);
 634        cancel_work_sync(&local->set_multicast_list_queue);
 635        cancel_work_sync(&local->set_tim_queue);
 636#ifndef PRISM2_NO_STATION_MODES
 637        cancel_work_sync(&local->info_queue);
 638#endif
 639        cancel_work_sync(&local->comms_qual_update);
 640
 641        module_put(local->hw_module);
 642
 643        local->num_dev_open--;
 644
 645        if (dev != local->dev && local->dev->flags & IFF_UP &&
 646            local->master_dev_auto_open && local->num_dev_open == 1) {
 647                /* Close master radio interface automatically if it was also
 648                 * opened automatically and we are now closing the last
 649                 * remaining non-master device. */
 650                dev_close(local->dev);
 651        }
 652
 653        return 0;
 654}
 655
 656
 657static int prism2_open(struct net_device *dev)
 658{
 659        struct hostap_interface *iface;
 660        local_info_t *local;
 661
 662        PDEBUG(DEBUG_FLOW, "%s: prism2_open\n", dev->name);
 663
 664        iface = netdev_priv(dev);
 665        local = iface->local;
 666
 667        if (local->no_pri) {
 668                printk(KERN_DEBUG "%s: could not set interface UP - no PRI "
 669                       "f/w\n", dev->name);
 670                return 1;
 671        }
 672
 673        if ((local->func->card_present && !local->func->card_present(local)) ||
 674            local->hw_downloading)
 675                return -ENODEV;
 676
 677        if (!try_module_get(local->hw_module))
 678                return -ENODEV;
 679        local->num_dev_open++;
 680
 681        if (!local->dev_enabled && local->func->hw_enable(dev, 1)) {
 682                printk(KERN_WARNING "%s: could not enable MAC port\n",
 683                       dev->name);
 684                prism2_close(dev);
 685                return 1;
 686        }
 687        if (!local->dev_enabled)
 688                prism2_callback(local, PRISM2_CALLBACK_ENABLE);
 689        local->dev_enabled = 1;
 690
 691        if (dev != local->dev && !(local->dev->flags & IFF_UP)) {
 692                /* Master radio interface is needed for all operation, so open
 693                 * it automatically when any virtual net_device is opened. */
 694                local->master_dev_auto_open = 1;
 695                dev_open(local->dev);
 696        }
 697
 698        netif_device_attach(dev);
 699        netif_start_queue(dev);
 700
 701        return 0;
 702}
 703
 704
 705static int prism2_set_mac_address(struct net_device *dev, void *p)
 706{
 707        struct hostap_interface *iface;
 708        local_info_t *local;
 709        struct list_head *ptr;
 710        struct sockaddr *addr = p;
 711
 712        iface = netdev_priv(dev);
 713        local = iface->local;
 714
 715        if (local->func->set_rid(dev, HFA384X_RID_CNFOWNMACADDR, addr->sa_data,
 716                                 ETH_ALEN) < 0 || local->func->reset_port(dev))
 717                return -EINVAL;
 718
 719        read_lock_bh(&local->iface_lock);
 720        list_for_each(ptr, &local->hostap_interfaces) {
 721                iface = list_entry(ptr, struct hostap_interface, list);
 722                memcpy(iface->dev->dev_addr, addr->sa_data, ETH_ALEN);
 723        }
 724        memcpy(local->dev->dev_addr, addr->sa_data, ETH_ALEN);
 725        read_unlock_bh(&local->iface_lock);
 726
 727        return 0;
 728}
 729
 730
 731/* TODO: to be further implemented as soon as Prism2 fully supports
 732 *       GroupAddresses and correct documentation is available */
 733void hostap_set_multicast_list_queue(struct work_struct *work)
 734{
 735        local_info_t *local =
 736                container_of(work, local_info_t, set_multicast_list_queue);
 737        struct net_device *dev = local->dev;
 738
 739        if (hostap_set_word(dev, HFA384X_RID_PROMISCUOUSMODE,
 740                            local->is_promisc)) {
 741                printk(KERN_INFO "%s: %sabling promiscuous mode failed\n",
 742                       dev->name, local->is_promisc ? "en" : "dis");
 743        }
 744}
 745
 746
 747static void hostap_set_multicast_list(struct net_device *dev)
 748{
 749#if 0
 750        /* FIX: promiscuous mode seems to be causing a lot of problems with
 751         * some station firmware versions (FCSErr frames, invalid MACPort, etc.
 752         * corrupted incoming frames). This code is now commented out while the
 753         * problems are investigated. */
 754        struct hostap_interface *iface;
 755        local_info_t *local;
 756
 757        iface = netdev_priv(dev);
 758        local = iface->local;
 759        if ((dev->flags & IFF_ALLMULTI) || (dev->flags & IFF_PROMISC)) {
 760                local->is_promisc = 1;
 761        } else {
 762                local->is_promisc = 0;
 763        }
 764
 765        schedule_work(&local->set_multicast_list_queue);
 766#endif
 767}
 768
 769
 770static int prism2_change_mtu(struct net_device *dev, int new_mtu)
 771{
 772        if (new_mtu < PRISM2_MIN_MTU || new_mtu > PRISM2_MAX_MTU)
 773                return -EINVAL;
 774
 775        dev->mtu = new_mtu;
 776        return 0;
 777}
 778
 779
 780static void prism2_tx_timeout(struct net_device *dev)
 781{
 782        struct hostap_interface *iface;
 783        local_info_t *local;
 784        struct hfa384x_regs regs;
 785
 786        iface = netdev_priv(dev);
 787        local = iface->local;
 788
 789        printk(KERN_WARNING "%s Tx timed out! Resetting card\n", dev->name);
 790        netif_stop_queue(local->dev);
 791
 792        local->func->read_regs(dev, &regs);
 793        printk(KERN_DEBUG "%s: CMD=%04x EVSTAT=%04x "
 794               "OFFSET0=%04x OFFSET1=%04x SWSUPPORT0=%04x\n",
 795               dev->name, regs.cmd, regs.evstat, regs.offset0, regs.offset1,
 796               regs.swsupport0);
 797
 798        local->func->schedule_reset(local);
 799}
 800
 801const struct header_ops hostap_80211_ops = {
 802        .create         = eth_header,
 803        .rebuild        = eth_rebuild_header,
 804        .cache          = eth_header_cache,
 805        .cache_update   = eth_header_cache_update,
 806        .parse          = hostap_80211_header_parse,
 807};
 808EXPORT_SYMBOL(hostap_80211_ops);
 809
 810
 811static const struct net_device_ops hostap_netdev_ops = {
 812        .ndo_start_xmit         = hostap_data_start_xmit,
 813
 814        .ndo_open               = prism2_open,
 815        .ndo_stop               = prism2_close,
 816        .ndo_do_ioctl           = hostap_ioctl,
 817        .ndo_set_mac_address    = prism2_set_mac_address,
 818        .ndo_set_rx_mode        = hostap_set_multicast_list,
 819        .ndo_change_mtu         = prism2_change_mtu,
 820        .ndo_tx_timeout         = prism2_tx_timeout,
 821        .ndo_validate_addr      = eth_validate_addr,
 822};
 823
 824static const struct net_device_ops hostap_mgmt_netdev_ops = {
 825        .ndo_start_xmit         = hostap_mgmt_start_xmit,
 826
 827        .ndo_open               = prism2_open,
 828        .ndo_stop               = prism2_close,
 829        .ndo_do_ioctl           = hostap_ioctl,
 830        .ndo_set_mac_address    = prism2_set_mac_address,
 831        .ndo_set_rx_mode        = hostap_set_multicast_list,
 832        .ndo_change_mtu         = prism2_change_mtu,
 833        .ndo_tx_timeout         = prism2_tx_timeout,
 834        .ndo_validate_addr      = eth_validate_addr,
 835};
 836
 837static const struct net_device_ops hostap_master_ops = {
 838        .ndo_start_xmit         = hostap_master_start_xmit,
 839
 840        .ndo_open               = prism2_open,
 841        .ndo_stop               = prism2_close,
 842        .ndo_do_ioctl           = hostap_ioctl,
 843        .ndo_set_mac_address    = prism2_set_mac_address,
 844        .ndo_set_rx_mode        = hostap_set_multicast_list,
 845        .ndo_change_mtu         = prism2_change_mtu,
 846        .ndo_tx_timeout         = prism2_tx_timeout,
 847        .ndo_validate_addr      = eth_validate_addr,
 848};
 849
 850void hostap_setup_dev(struct net_device *dev, local_info_t *local,
 851                      int type)
 852{
 853        struct hostap_interface *iface;
 854
 855        iface = netdev_priv(dev);
 856        ether_setup(dev);
 857        dev->priv_flags &= ~IFF_TX_SKB_SHARING;
 858
 859        /* kernel callbacks */
 860        if (iface) {
 861                /* Currently, we point to the proper spy_data only on
 862                 * the main_dev. This could be fixed. Jean II */
 863                iface->wireless_data.spy_data = &iface->spy_data;
 864                dev->wireless_data = &iface->wireless_data;
 865        }
 866        dev->wireless_handlers = &hostap_iw_handler_def;
 867        dev->watchdog_timeo = TX_TIMEOUT;
 868
 869        switch(type) {
 870        case HOSTAP_INTERFACE_AP:
 871                dev->tx_queue_len = 0;  /* use main radio device queue */
 872                dev->netdev_ops = &hostap_mgmt_netdev_ops;
 873                dev->type = ARPHRD_IEEE80211;
 874                dev->header_ops = &hostap_80211_ops;
 875                break;
 876        case HOSTAP_INTERFACE_MASTER:
 877                dev->netdev_ops = &hostap_master_ops;
 878                break;
 879        default:
 880                dev->tx_queue_len = 0;  /* use main radio device queue */
 881                dev->netdev_ops = &hostap_netdev_ops;
 882        }
 883
 884        dev->mtu = local->mtu;
 885
 886
 887        SET_ETHTOOL_OPS(dev, &prism2_ethtool_ops);
 888
 889}
 890
 891static int hostap_enable_hostapd(local_info_t *local, int rtnl_locked)
 892{
 893        struct net_device *dev = local->dev;
 894
 895        if (local->apdev)
 896                return -EEXIST;
 897
 898        printk(KERN_DEBUG "%s: enabling hostapd mode\n", dev->name);
 899
 900        local->apdev = hostap_add_interface(local, HOSTAP_INTERFACE_AP,
 901                                            rtnl_locked, local->ddev->name,
 902                                            "ap");
 903        if (local->apdev == NULL)
 904                return -ENOMEM;
 905
 906        return 0;
 907}
 908
 909
 910static int hostap_disable_hostapd(local_info_t *local, int rtnl_locked)
 911{
 912        struct net_device *dev = local->dev;
 913
 914        printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
 915
 916        hostap_remove_interface(local->apdev, rtnl_locked, 1);
 917        local->apdev = NULL;
 918
 919        return 0;
 920}
 921
 922
 923static int hostap_enable_hostapd_sta(local_info_t *local, int rtnl_locked)
 924{
 925        struct net_device *dev = local->dev;
 926
 927        if (local->stadev)
 928                return -EEXIST;
 929
 930        printk(KERN_DEBUG "%s: enabling hostapd STA mode\n", dev->name);
 931
 932        local->stadev = hostap_add_interface(local, HOSTAP_INTERFACE_STA,
 933                                             rtnl_locked, local->ddev->name,
 934                                             "sta");
 935        if (local->stadev == NULL)
 936                return -ENOMEM;
 937
 938        return 0;
 939}
 940
 941
 942static int hostap_disable_hostapd_sta(local_info_t *local, int rtnl_locked)
 943{
 944        struct net_device *dev = local->dev;
 945
 946        printk(KERN_DEBUG "%s: disabling hostapd mode\n", dev->name);
 947
 948        hostap_remove_interface(local->stadev, rtnl_locked, 1);
 949        local->stadev = NULL;
 950
 951        return 0;
 952}
 953
 954
 955int hostap_set_hostapd(local_info_t *local, int val, int rtnl_locked)
 956{
 957        int ret;
 958
 959        if (val < 0 || val > 1)
 960                return -EINVAL;
 961
 962        if (local->hostapd == val)
 963                return 0;
 964
 965        if (val) {
 966                ret = hostap_enable_hostapd(local, rtnl_locked);
 967                if (ret == 0)
 968                        local->hostapd = 1;
 969        } else {
 970                local->hostapd = 0;
 971                ret = hostap_disable_hostapd(local, rtnl_locked);
 972                if (ret != 0)
 973                        local->hostapd = 1;
 974        }
 975
 976        return ret;
 977}
 978
 979
 980int hostap_set_hostapd_sta(local_info_t *local, int val, int rtnl_locked)
 981{
 982        int ret;
 983
 984        if (val < 0 || val > 1)
 985                return -EINVAL;
 986
 987        if (local->hostapd_sta == val)
 988                return 0;
 989
 990        if (val) {
 991                ret = hostap_enable_hostapd_sta(local, rtnl_locked);
 992                if (ret == 0)
 993                        local->hostapd_sta = 1;
 994        } else {
 995                local->hostapd_sta = 0;
 996                ret = hostap_disable_hostapd_sta(local, rtnl_locked);
 997                if (ret != 0)
 998                        local->hostapd_sta = 1;
 999        }
1000
1001
1002        return ret;
1003}
1004
1005
1006int prism2_update_comms_qual(struct net_device *dev)
1007{
1008        struct hostap_interface *iface;
1009        local_info_t *local;
1010        int ret = 0;
1011        struct hfa384x_comms_quality sq;
1012
1013        iface = netdev_priv(dev);
1014        local = iface->local;
1015        if (!local->sta_fw_ver)
1016                ret = -1;
1017        else if (local->sta_fw_ver >= PRISM2_FW_VER(1,3,1)) {
1018                if (local->func->get_rid(local->dev,
1019                                         HFA384X_RID_DBMCOMMSQUALITY,
1020                                         &sq, sizeof(sq), 1) >= 0) {
1021                        local->comms_qual = (s16) le16_to_cpu(sq.comm_qual);
1022                        local->avg_signal = (s16) le16_to_cpu(sq.signal_level);
1023                        local->avg_noise = (s16) le16_to_cpu(sq.noise_level);
1024                        local->last_comms_qual_update = jiffies;
1025                } else
1026                        ret = -1;
1027        } else {
1028                if (local->func->get_rid(local->dev, HFA384X_RID_COMMSQUALITY,
1029                                         &sq, sizeof(sq), 1) >= 0) {
1030                        local->comms_qual = le16_to_cpu(sq.comm_qual);
1031                        local->avg_signal = HFA384X_LEVEL_TO_dBm(
1032                                le16_to_cpu(sq.signal_level));
1033                        local->avg_noise = HFA384X_LEVEL_TO_dBm(
1034                                le16_to_cpu(sq.noise_level));
1035                        local->last_comms_qual_update = jiffies;
1036                } else
1037                        ret = -1;
1038        }
1039
1040        return ret;
1041}
1042
1043
1044int prism2_sta_send_mgmt(local_info_t *local, u8 *dst, u16 stype,
1045                         u8 *body, size_t bodylen)
1046{
1047        struct sk_buff *skb;
1048        struct hostap_ieee80211_mgmt *mgmt;
1049        struct hostap_skb_tx_data *meta;
1050        struct net_device *dev = local->dev;
1051
1052        skb = dev_alloc_skb(IEEE80211_MGMT_HDR_LEN + bodylen);
1053        if (skb == NULL)
1054                return -ENOMEM;
1055
1056        mgmt = (struct hostap_ieee80211_mgmt *)
1057                skb_put(skb, IEEE80211_MGMT_HDR_LEN);
1058        memset(mgmt, 0, IEEE80211_MGMT_HDR_LEN);
1059        mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | stype);
1060        memcpy(mgmt->da, dst, ETH_ALEN);
1061        memcpy(mgmt->sa, dev->dev_addr, ETH_ALEN);
1062        memcpy(mgmt->bssid, dst, ETH_ALEN);
1063        if (body)
1064                memcpy(skb_put(skb, bodylen), body, bodylen);
1065
1066        meta = (struct hostap_skb_tx_data *) skb->cb;
1067        memset(meta, 0, sizeof(*meta));
1068        meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
1069        meta->iface = netdev_priv(dev);
1070
1071        skb->dev = dev;
1072        skb_reset_mac_header(skb);
1073        skb_reset_network_header(skb);
1074        dev_queue_xmit(skb);
1075
1076        return 0;
1077}
1078
1079
1080int prism2_sta_deauth(local_info_t *local, u16 reason)
1081{
1082        union iwreq_data wrqu;
1083        int ret;
1084        __le16 val = cpu_to_le16(reason);
1085
1086        if (local->iw_mode != IW_MODE_INFRA ||
1087            is_zero_ether_addr(local->bssid) ||
1088            memcmp(local->bssid, "\x44\x44\x44\x44\x44\x44", ETH_ALEN) == 0)
1089                return 0;
1090
1091        ret = prism2_sta_send_mgmt(local, local->bssid, IEEE80211_STYPE_DEAUTH,
1092                                   (u8 *) &val, 2);
1093        memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
1094        wireless_send_event(local->dev, SIOCGIWAP, &wrqu, NULL);
1095        return ret;
1096}
1097
1098
1099struct proc_dir_entry *hostap_proc;
1100
1101static int __init hostap_init(void)
1102{
1103        if (init_net.proc_net != NULL) {
1104                hostap_proc = proc_mkdir("hostap", init_net.proc_net);
1105                if (!hostap_proc)
1106                        printk(KERN_WARNING "Failed to mkdir "
1107                               "/proc/net/hostap\n");
1108        } else
1109                hostap_proc = NULL;
1110
1111        return 0;
1112}
1113
1114
1115static void __exit hostap_exit(void)
1116{
1117        if (hostap_proc != NULL) {
1118                hostap_proc = NULL;
1119                remove_proc_entry("hostap", init_net.proc_net);
1120        }
1121}
1122
1123
1124EXPORT_SYMBOL(hostap_set_word);
1125EXPORT_SYMBOL(hostap_set_string);
1126EXPORT_SYMBOL(hostap_get_porttype);
1127EXPORT_SYMBOL(hostap_set_encryption);
1128EXPORT_SYMBOL(hostap_set_antsel);
1129EXPORT_SYMBOL(hostap_set_roaming);
1130EXPORT_SYMBOL(hostap_set_auth_algs);
1131EXPORT_SYMBOL(hostap_dump_rx_header);
1132EXPORT_SYMBOL(hostap_dump_tx_header);
1133EXPORT_SYMBOL(hostap_80211_get_hdrlen);
1134EXPORT_SYMBOL(hostap_setup_dev);
1135EXPORT_SYMBOL(hostap_set_multicast_list_queue);
1136EXPORT_SYMBOL(hostap_set_hostapd);
1137EXPORT_SYMBOL(hostap_set_hostapd_sta);
1138EXPORT_SYMBOL(hostap_add_interface);
1139EXPORT_SYMBOL(hostap_remove_interface);
1140EXPORT_SYMBOL(prism2_update_comms_qual);
1141
1142module_init(hostap_init);
1143module_exit(hostap_exit);
1144