linux/drivers/net/wireless/intersil/hostap/hostap_ap.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Intersil Prism2 driver with Host AP (software access point) support
   4 * Copyright (c) 2001-2002, SSH Communications Security Corp and Jouni Malinen
   5 * <j@w1.fi>
   6 * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
   7 *
   8 * This file is to be included into hostap.c when S/W AP functionality is
   9 * compiled.
  10 *
  11 * AP:  FIX:
  12 * - if unicast Class 2 (assoc,reassoc,disassoc) frame received from
  13 *   unauthenticated STA, send deauth. frame (8802.11: 5.5)
  14 * - if unicast Class 3 (data with to/from DS,deauth,pspoll) frame received
  15 *   from authenticated, but unassoc STA, send disassoc frame (8802.11: 5.5)
  16 * - if unicast Class 3 received from unauthenticated STA, send deauth. frame
  17 *   (8802.11: 5.5)
  18 */
  19
  20#include <linux/proc_fs.h>
  21#include <linux/seq_file.h>
  22#include <linux/delay.h>
  23#include <linux/random.h>
  24#include <linux/if_arp.h>
  25#include <linux/slab.h>
  26#include <linux/export.h>
  27#include <linux/moduleparam.h>
  28#include <linux/etherdevice.h>
  29
  30#include "hostap_wlan.h"
  31#include "hostap.h"
  32#include "hostap_ap.h"
  33
  34static int other_ap_policy[MAX_PARM_DEVICES] = { AP_OTHER_AP_SKIP_ALL,
  35                                                 DEF_INTS };
  36module_param_array(other_ap_policy, int, NULL, 0444);
  37MODULE_PARM_DESC(other_ap_policy, "Other AP beacon monitoring policy (0-3)");
  38
  39static int ap_max_inactivity[MAX_PARM_DEVICES] = { AP_MAX_INACTIVITY_SEC,
  40                                                   DEF_INTS };
  41module_param_array(ap_max_inactivity, int, NULL, 0444);
  42MODULE_PARM_DESC(ap_max_inactivity, "AP timeout (in seconds) for station "
  43                 "inactivity");
  44
  45static int ap_bridge_packets[MAX_PARM_DEVICES] = { 1, DEF_INTS };
  46module_param_array(ap_bridge_packets, int, NULL, 0444);
  47MODULE_PARM_DESC(ap_bridge_packets, "Bridge packets directly between "
  48                 "stations");
  49
  50static int autom_ap_wds[MAX_PARM_DEVICES] = { 0, DEF_INTS };
  51module_param_array(autom_ap_wds, int, NULL, 0444);
  52MODULE_PARM_DESC(autom_ap_wds, "Add WDS connections to other APs "
  53                 "automatically");
  54
  55
  56static struct sta_info* ap_get_sta(struct ap_data *ap, u8 *sta);
  57static void hostap_event_expired_sta(struct net_device *dev,
  58                                     struct sta_info *sta);
  59static void handle_add_proc_queue(struct work_struct *work);
  60
  61#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
  62static void handle_wds_oper_queue(struct work_struct *work);
  63static void prism2_send_mgmt(struct net_device *dev,
  64                             u16 type_subtype, char *body,
  65                             int body_len, u8 *addr, u16 tx_cb_idx);
  66#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
  67
  68
  69#ifndef PRISM2_NO_PROCFS_DEBUG
  70static int ap_debug_proc_show(struct seq_file *m, void *v)
  71{
  72        struct ap_data *ap = PDE_DATA(file_inode(m->file));
  73
  74        seq_printf(m, "BridgedUnicastFrames=%u\n", ap->bridged_unicast);
  75        seq_printf(m, "BridgedMulticastFrames=%u\n", ap->bridged_multicast);
  76        seq_printf(m, "max_inactivity=%u\n", ap->max_inactivity / HZ);
  77        seq_printf(m, "bridge_packets=%u\n", ap->bridge_packets);
  78        seq_printf(m, "nullfunc_ack=%u\n", ap->nullfunc_ack);
  79        seq_printf(m, "autom_ap_wds=%u\n", ap->autom_ap_wds);
  80        seq_printf(m, "auth_algs=%u\n", ap->local->auth_algs);
  81        seq_printf(m, "tx_drop_nonassoc=%u\n", ap->tx_drop_nonassoc);
  82        return 0;
  83}
  84#endif /* PRISM2_NO_PROCFS_DEBUG */
  85
  86
  87static void ap_sta_hash_add(struct ap_data *ap, struct sta_info *sta)
  88{
  89        sta->hnext = ap->sta_hash[STA_HASH(sta->addr)];
  90        ap->sta_hash[STA_HASH(sta->addr)] = sta;
  91}
  92
  93static void ap_sta_hash_del(struct ap_data *ap, struct sta_info *sta)
  94{
  95        struct sta_info *s;
  96
  97        s = ap->sta_hash[STA_HASH(sta->addr)];
  98        if (s == NULL) return;
  99        if (ether_addr_equal(s->addr, sta->addr)) {
 100                ap->sta_hash[STA_HASH(sta->addr)] = s->hnext;
 101                return;
 102        }
 103
 104        while (s->hnext != NULL && !ether_addr_equal(s->hnext->addr, sta->addr))
 105                s = s->hnext;
 106        if (s->hnext != NULL)
 107                s->hnext = s->hnext->hnext;
 108        else
 109                printk("AP: could not remove STA %pM from hash table\n",
 110                       sta->addr);
 111}
 112
 113static void ap_free_sta(struct ap_data *ap, struct sta_info *sta)
 114{
 115        if (sta->ap && sta->local)
 116                hostap_event_expired_sta(sta->local->dev, sta);
 117
 118        if (ap->proc != NULL) {
 119                char name[20];
 120                sprintf(name, "%pM", sta->addr);
 121                remove_proc_entry(name, ap->proc);
 122        }
 123
 124        if (sta->crypt) {
 125                sta->crypt->ops->deinit(sta->crypt->priv);
 126                kfree(sta->crypt);
 127                sta->crypt = NULL;
 128        }
 129
 130        skb_queue_purge(&sta->tx_buf);
 131
 132        ap->num_sta--;
 133#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
 134        if (sta->aid > 0)
 135                ap->sta_aid[sta->aid - 1] = NULL;
 136
 137        if (!sta->ap)
 138                kfree(sta->u.sta.challenge);
 139        del_timer_sync(&sta->timer);
 140#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
 141
 142        kfree(sta);
 143}
 144
 145
 146static void hostap_set_tim(local_info_t *local, int aid, int set)
 147{
 148        if (local->func->set_tim)
 149                local->func->set_tim(local->dev, aid, set);
 150}
 151
 152
 153static void hostap_event_new_sta(struct net_device *dev, struct sta_info *sta)
 154{
 155        union iwreq_data wrqu;
 156        memset(&wrqu, 0, sizeof(wrqu));
 157        memcpy(wrqu.addr.sa_data, sta->addr, ETH_ALEN);
 158        wrqu.addr.sa_family = ARPHRD_ETHER;
 159        wireless_send_event(dev, IWEVREGISTERED, &wrqu, NULL);
 160}
 161
 162
 163static void hostap_event_expired_sta(struct net_device *dev,
 164                                     struct sta_info *sta)
 165{
 166        union iwreq_data wrqu;
 167        memset(&wrqu, 0, sizeof(wrqu));
 168        memcpy(wrqu.addr.sa_data, sta->addr, ETH_ALEN);
 169        wrqu.addr.sa_family = ARPHRD_ETHER;
 170        wireless_send_event(dev, IWEVEXPIRED, &wrqu, NULL);
 171}
 172
 173
 174#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
 175
 176static void ap_handle_timer(struct timer_list *t)
 177{
 178        struct sta_info *sta = from_timer(sta, t, timer);
 179        local_info_t *local;
 180        struct ap_data *ap;
 181        unsigned long next_time = 0;
 182        int was_assoc;
 183
 184        if (sta == NULL || sta->local == NULL || sta->local->ap == NULL) {
 185                PDEBUG(DEBUG_AP, "ap_handle_timer() called with NULL data\n");
 186                return;
 187        }
 188
 189        local = sta->local;
 190        ap = local->ap;
 191        was_assoc = sta->flags & WLAN_STA_ASSOC;
 192
 193        if (atomic_read(&sta->users) != 0)
 194                next_time = jiffies + HZ;
 195        else if ((sta->flags & WLAN_STA_PERM) && !(sta->flags & WLAN_STA_AUTH))
 196                next_time = jiffies + ap->max_inactivity;
 197
 198        if (time_before(jiffies, sta->last_rx + ap->max_inactivity)) {
 199                /* station activity detected; reset timeout state */
 200                sta->timeout_next = STA_NULLFUNC;
 201                next_time = sta->last_rx + ap->max_inactivity;
 202        } else if (sta->timeout_next == STA_DISASSOC &&
 203                   !(sta->flags & WLAN_STA_PENDING_POLL)) {
 204                /* STA ACKed data nullfunc frame poll */
 205                sta->timeout_next = STA_NULLFUNC;
 206                next_time = jiffies + ap->max_inactivity;
 207        }
 208
 209        if (next_time) {
 210                sta->timer.expires = next_time;
 211                add_timer(&sta->timer);
 212                return;
 213        }
 214
 215        if (sta->ap)
 216                sta->timeout_next = STA_DEAUTH;
 217
 218        if (sta->timeout_next == STA_DEAUTH && !(sta->flags & WLAN_STA_PERM)) {
 219                spin_lock(&ap->sta_table_lock);
 220                ap_sta_hash_del(ap, sta);
 221                list_del(&sta->list);
 222                spin_unlock(&ap->sta_table_lock);
 223                sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
 224        } else if (sta->timeout_next == STA_DISASSOC)
 225                sta->flags &= ~WLAN_STA_ASSOC;
 226
 227        if (was_assoc && !(sta->flags & WLAN_STA_ASSOC) && !sta->ap)
 228                hostap_event_expired_sta(local->dev, sta);
 229
 230        if (sta->timeout_next == STA_DEAUTH && sta->aid > 0 &&
 231            !skb_queue_empty(&sta->tx_buf)) {
 232                hostap_set_tim(local, sta->aid, 0);
 233                sta->flags &= ~WLAN_STA_TIM;
 234        }
 235
 236        if (sta->ap) {
 237                if (ap->autom_ap_wds) {
 238                        PDEBUG(DEBUG_AP, "%s: removing automatic WDS "
 239                               "connection to AP %pM\n",
 240                               local->dev->name, sta->addr);
 241                        hostap_wds_link_oper(local, sta->addr, WDS_DEL);
 242                }
 243        } else if (sta->timeout_next == STA_NULLFUNC) {
 244                /* send data frame to poll STA and check whether this frame
 245                 * is ACKed */
 246                /* FIX: IEEE80211_STYPE_NULLFUNC would be more appropriate, but
 247                 * it is apparently not retried so TX Exc events are not
 248                 * received for it */
 249                sta->flags |= WLAN_STA_PENDING_POLL;
 250                prism2_send_mgmt(local->dev, IEEE80211_FTYPE_DATA |
 251                                 IEEE80211_STYPE_DATA, NULL, 0,
 252                                 sta->addr, ap->tx_callback_poll);
 253        } else {
 254                int deauth = sta->timeout_next == STA_DEAUTH;
 255                __le16 resp;
 256                PDEBUG(DEBUG_AP, "%s: sending %s info to STA %pM"
 257                       "(last=%lu, jiffies=%lu)\n",
 258                       local->dev->name,
 259                       deauth ? "deauthentication" : "disassociation",
 260                       sta->addr, sta->last_rx, jiffies);
 261
 262                resp = cpu_to_le16(deauth ? WLAN_REASON_PREV_AUTH_NOT_VALID :
 263                                   WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
 264                prism2_send_mgmt(local->dev, IEEE80211_FTYPE_MGMT |
 265                                 (deauth ? IEEE80211_STYPE_DEAUTH :
 266                                  IEEE80211_STYPE_DISASSOC),
 267                                 (char *) &resp, 2, sta->addr, 0);
 268        }
 269
 270        if (sta->timeout_next == STA_DEAUTH) {
 271                if (sta->flags & WLAN_STA_PERM) {
 272                        PDEBUG(DEBUG_AP, "%s: STA %pM"
 273                               " would have been removed, "
 274                               "but it has 'perm' flag\n",
 275                               local->dev->name, sta->addr);
 276                } else
 277                        ap_free_sta(ap, sta);
 278                return;
 279        }
 280
 281        if (sta->timeout_next == STA_NULLFUNC) {
 282                sta->timeout_next = STA_DISASSOC;
 283                sta->timer.expires = jiffies + AP_DISASSOC_DELAY;
 284        } else {
 285                sta->timeout_next = STA_DEAUTH;
 286                sta->timer.expires = jiffies + AP_DEAUTH_DELAY;
 287        }
 288
 289        add_timer(&sta->timer);
 290}
 291
 292
 293void hostap_deauth_all_stas(struct net_device *dev, struct ap_data *ap,
 294                            int resend)
 295{
 296        u8 addr[ETH_ALEN];
 297        __le16 resp;
 298        int i;
 299
 300        PDEBUG(DEBUG_AP, "%s: Deauthenticate all stations\n", dev->name);
 301        eth_broadcast_addr(addr);
 302
 303        resp = cpu_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
 304
 305        /* deauth message sent; try to resend it few times; the message is
 306         * broadcast, so it may be delayed until next DTIM; there is not much
 307         * else we can do at this point since the driver is going to be shut
 308         * down */
 309        for (i = 0; i < 5; i++) {
 310                prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT |
 311                                 IEEE80211_STYPE_DEAUTH,
 312                                 (char *) &resp, 2, addr, 0);
 313
 314                if (!resend || ap->num_sta <= 0)
 315                        return;
 316
 317                mdelay(50);
 318        }
 319}
 320
 321
 322static int ap_control_proc_show(struct seq_file *m, void *v)
 323{
 324        struct ap_data *ap = PDE_DATA(file_inode(m->file));
 325        char *policy_txt;
 326        struct mac_entry *entry;
 327
 328        if (v == SEQ_START_TOKEN) {
 329                switch (ap->mac_restrictions.policy) {
 330                case MAC_POLICY_OPEN:
 331                        policy_txt = "open";
 332                        break;
 333                case MAC_POLICY_ALLOW:
 334                        policy_txt = "allow";
 335                        break;
 336                case MAC_POLICY_DENY:
 337                        policy_txt = "deny";
 338                        break;
 339                default:
 340                        policy_txt = "unknown";
 341                        break;
 342                }
 343                seq_printf(m, "MAC policy: %s\n", policy_txt);
 344                seq_printf(m, "MAC entries: %u\n", ap->mac_restrictions.entries);
 345                seq_puts(m, "MAC list:\n");
 346                return 0;
 347        }
 348
 349        entry = v;
 350        seq_printf(m, "%pM\n", entry->addr);
 351        return 0;
 352}
 353
 354static void *ap_control_proc_start(struct seq_file *m, loff_t *_pos)
 355{
 356        struct ap_data *ap = PDE_DATA(file_inode(m->file));
 357        spin_lock_bh(&ap->mac_restrictions.lock);
 358        return seq_list_start_head(&ap->mac_restrictions.mac_list, *_pos);
 359}
 360
 361static void *ap_control_proc_next(struct seq_file *m, void *v, loff_t *_pos)
 362{
 363        struct ap_data *ap = PDE_DATA(file_inode(m->file));
 364        return seq_list_next(v, &ap->mac_restrictions.mac_list, _pos);
 365}
 366
 367static void ap_control_proc_stop(struct seq_file *m, void *v)
 368{
 369        struct ap_data *ap = PDE_DATA(file_inode(m->file));
 370        spin_unlock_bh(&ap->mac_restrictions.lock);
 371}
 372
 373static const struct seq_operations ap_control_proc_seqops = {
 374        .start  = ap_control_proc_start,
 375        .next   = ap_control_proc_next,
 376        .stop   = ap_control_proc_stop,
 377        .show   = ap_control_proc_show,
 378};
 379
 380int ap_control_add_mac(struct mac_restrictions *mac_restrictions, u8 *mac)
 381{
 382        struct mac_entry *entry;
 383
 384        entry = kmalloc(sizeof(struct mac_entry), GFP_KERNEL);
 385        if (entry == NULL)
 386                return -ENOMEM;
 387
 388        memcpy(entry->addr, mac, ETH_ALEN);
 389
 390        spin_lock_bh(&mac_restrictions->lock);
 391        list_add_tail(&entry->list, &mac_restrictions->mac_list);
 392        mac_restrictions->entries++;
 393        spin_unlock_bh(&mac_restrictions->lock);
 394
 395        return 0;
 396}
 397
 398
 399int ap_control_del_mac(struct mac_restrictions *mac_restrictions, u8 *mac)
 400{
 401        struct list_head *ptr;
 402        struct mac_entry *entry;
 403
 404        spin_lock_bh(&mac_restrictions->lock);
 405        for (ptr = mac_restrictions->mac_list.next;
 406             ptr != &mac_restrictions->mac_list; ptr = ptr->next) {
 407                entry = list_entry(ptr, struct mac_entry, list);
 408
 409                if (ether_addr_equal(entry->addr, mac)) {
 410                        list_del(ptr);
 411                        kfree(entry);
 412                        mac_restrictions->entries--;
 413                        spin_unlock_bh(&mac_restrictions->lock);
 414                        return 0;
 415                }
 416        }
 417        spin_unlock_bh(&mac_restrictions->lock);
 418        return -1;
 419}
 420
 421
 422static int ap_control_mac_deny(struct mac_restrictions *mac_restrictions,
 423                               u8 *mac)
 424{
 425        struct mac_entry *entry;
 426        int found = 0;
 427
 428        if (mac_restrictions->policy == MAC_POLICY_OPEN)
 429                return 0;
 430
 431        spin_lock_bh(&mac_restrictions->lock);
 432        list_for_each_entry(entry, &mac_restrictions->mac_list, list) {
 433                if (ether_addr_equal(entry->addr, mac)) {
 434                        found = 1;
 435                        break;
 436                }
 437        }
 438        spin_unlock_bh(&mac_restrictions->lock);
 439
 440        if (mac_restrictions->policy == MAC_POLICY_ALLOW)
 441                return !found;
 442        else
 443                return found;
 444}
 445
 446
 447void ap_control_flush_macs(struct mac_restrictions *mac_restrictions)
 448{
 449        struct list_head *ptr, *n;
 450        struct mac_entry *entry;
 451
 452        if (mac_restrictions->entries == 0)
 453                return;
 454
 455        spin_lock_bh(&mac_restrictions->lock);
 456        for (ptr = mac_restrictions->mac_list.next, n = ptr->next;
 457             ptr != &mac_restrictions->mac_list;
 458             ptr = n, n = ptr->next) {
 459                entry = list_entry(ptr, struct mac_entry, list);
 460                list_del(ptr);
 461                kfree(entry);
 462        }
 463        mac_restrictions->entries = 0;
 464        spin_unlock_bh(&mac_restrictions->lock);
 465}
 466
 467
 468int ap_control_kick_mac(struct ap_data *ap, struct net_device *dev, u8 *mac)
 469{
 470        struct sta_info *sta;
 471        __le16 resp;
 472
 473        spin_lock_bh(&ap->sta_table_lock);
 474        sta = ap_get_sta(ap, mac);
 475        if (sta) {
 476                ap_sta_hash_del(ap, sta);
 477                list_del(&sta->list);
 478        }
 479        spin_unlock_bh(&ap->sta_table_lock);
 480
 481        if (!sta)
 482                return -EINVAL;
 483
 484        resp = cpu_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
 485        prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_DEAUTH,
 486                         (char *) &resp, 2, sta->addr, 0);
 487
 488        if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
 489                hostap_event_expired_sta(dev, sta);
 490
 491        ap_free_sta(ap, sta);
 492
 493        return 0;
 494}
 495
 496#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
 497
 498
 499void ap_control_kickall(struct ap_data *ap)
 500{
 501        struct list_head *ptr, *n;
 502        struct sta_info *sta;
 503
 504        spin_lock_bh(&ap->sta_table_lock);
 505        for (ptr = ap->sta_list.next, n = ptr->next; ptr != &ap->sta_list;
 506             ptr = n, n = ptr->next) {
 507                sta = list_entry(ptr, struct sta_info, list);
 508                ap_sta_hash_del(ap, sta);
 509                list_del(&sta->list);
 510                if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
 511                        hostap_event_expired_sta(sta->local->dev, sta);
 512                ap_free_sta(ap, sta);
 513        }
 514        spin_unlock_bh(&ap->sta_table_lock);
 515}
 516
 517
 518#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
 519
 520static int prism2_ap_proc_show(struct seq_file *m, void *v)
 521{
 522        struct sta_info *sta = v;
 523        int i;
 524
 525        if (v == SEQ_START_TOKEN) {
 526                seq_printf(m, "# BSSID CHAN SIGNAL NOISE RATE SSID FLAGS\n");
 527                return 0;
 528        }
 529
 530        if (!sta->ap)
 531                return 0;
 532
 533        seq_printf(m, "%pM %d %d %d %d '",
 534                   sta->addr,
 535                   sta->u.ap.channel, sta->last_rx_signal,
 536                   sta->last_rx_silence, sta->last_rx_rate);
 537
 538        for (i = 0; i < sta->u.ap.ssid_len; i++) {
 539                if (sta->u.ap.ssid[i] >= 32 && sta->u.ap.ssid[i] < 127)
 540                        seq_putc(m, sta->u.ap.ssid[i]);
 541                else
 542                        seq_printf(m, "<%02x>", sta->u.ap.ssid[i]);
 543        }
 544
 545        seq_putc(m, '\'');
 546        if (sta->capability & WLAN_CAPABILITY_ESS)
 547                seq_puts(m, " [ESS]");
 548        if (sta->capability & WLAN_CAPABILITY_IBSS)
 549                seq_puts(m, " [IBSS]");
 550        if (sta->capability & WLAN_CAPABILITY_PRIVACY)
 551                seq_puts(m, " [WEP]");
 552        seq_putc(m, '\n');
 553        return 0;
 554}
 555
 556static void *prism2_ap_proc_start(struct seq_file *m, loff_t *_pos)
 557{
 558        struct ap_data *ap = PDE_DATA(file_inode(m->file));
 559        spin_lock_bh(&ap->sta_table_lock);
 560        return seq_list_start_head(&ap->sta_list, *_pos);
 561}
 562
 563static void *prism2_ap_proc_next(struct seq_file *m, void *v, loff_t *_pos)
 564{
 565        struct ap_data *ap = PDE_DATA(file_inode(m->file));
 566        return seq_list_next(v, &ap->sta_list, _pos);
 567}
 568
 569static void prism2_ap_proc_stop(struct seq_file *m, void *v)
 570{
 571        struct ap_data *ap = PDE_DATA(file_inode(m->file));
 572        spin_unlock_bh(&ap->sta_table_lock);
 573}
 574
 575static const struct seq_operations prism2_ap_proc_seqops = {
 576        .start  = prism2_ap_proc_start,
 577        .next   = prism2_ap_proc_next,
 578        .stop   = prism2_ap_proc_stop,
 579        .show   = prism2_ap_proc_show,
 580};
 581#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
 582
 583
 584void hostap_check_sta_fw_version(struct ap_data *ap, int sta_fw_ver)
 585{
 586        if (!ap)
 587                return;
 588
 589        if (sta_fw_ver == PRISM2_FW_VER(0,8,0)) {
 590                PDEBUG(DEBUG_AP, "Using data::nullfunc ACK workaround - "
 591                       "firmware upgrade recommended\n");
 592                ap->nullfunc_ack = 1;
 593        } else
 594                ap->nullfunc_ack = 0;
 595
 596        if (sta_fw_ver == PRISM2_FW_VER(1,4,2)) {
 597                printk(KERN_WARNING "%s: Warning: secondary station firmware "
 598                       "version 1.4.2 does not seem to work in Host AP mode\n",
 599                       ap->local->dev->name);
 600        }
 601}
 602
 603
 604/* Called only as a tasklet (software IRQ) */
 605static void hostap_ap_tx_cb(struct sk_buff *skb, int ok, void *data)
 606{
 607        struct ap_data *ap = data;
 608        struct ieee80211_hdr *hdr;
 609
 610        if (!ap->local->hostapd || !ap->local->apdev) {
 611                dev_kfree_skb(skb);
 612                return;
 613        }
 614
 615        /* Pass the TX callback frame to the hostapd; use 802.11 header version
 616         * 1 to indicate failure (no ACK) and 2 success (frame ACKed) */
 617
 618        hdr = (struct ieee80211_hdr *) skb->data;
 619        hdr->frame_control &= cpu_to_le16(~IEEE80211_FCTL_VERS);
 620        hdr->frame_control |= cpu_to_le16(ok ? BIT(1) : BIT(0));
 621
 622        skb->dev = ap->local->apdev;
 623        skb_pull(skb, hostap_80211_get_hdrlen(hdr->frame_control));
 624        skb->pkt_type = PACKET_OTHERHOST;
 625        skb->protocol = cpu_to_be16(ETH_P_802_2);
 626        memset(skb->cb, 0, sizeof(skb->cb));
 627        netif_rx(skb);
 628}
 629
 630
 631#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
 632/* Called only as a tasklet (software IRQ) */
 633static void hostap_ap_tx_cb_auth(struct sk_buff *skb, int ok, void *data)
 634{
 635        struct ap_data *ap = data;
 636        struct net_device *dev = ap->local->dev;
 637        struct ieee80211_hdr *hdr;
 638        u16 auth_alg, auth_transaction, status;
 639        __le16 *pos;
 640        struct sta_info *sta = NULL;
 641        char *txt = NULL;
 642
 643        if (ap->local->hostapd) {
 644                dev_kfree_skb(skb);
 645                return;
 646        }
 647
 648        hdr = (struct ieee80211_hdr *) skb->data;
 649        if (!ieee80211_is_auth(hdr->frame_control) ||
 650            skb->len < IEEE80211_MGMT_HDR_LEN + 6) {
 651                printk(KERN_DEBUG "%s: hostap_ap_tx_cb_auth received invalid "
 652                       "frame\n", dev->name);
 653                dev_kfree_skb(skb);
 654                return;
 655        }
 656
 657        pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
 658        auth_alg = le16_to_cpu(*pos++);
 659        auth_transaction = le16_to_cpu(*pos++);
 660        status = le16_to_cpu(*pos++);
 661
 662        if (!ok) {
 663                txt = "frame was not ACKed";
 664                goto done;
 665        }
 666
 667        spin_lock(&ap->sta_table_lock);
 668        sta = ap_get_sta(ap, hdr->addr1);
 669        if (sta)
 670                atomic_inc(&sta->users);
 671        spin_unlock(&ap->sta_table_lock);
 672
 673        if (!sta) {
 674                txt = "STA not found";
 675                goto done;
 676        }
 677
 678        if (status == WLAN_STATUS_SUCCESS &&
 679            ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
 680             (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
 681                txt = "STA authenticated";
 682                sta->flags |= WLAN_STA_AUTH;
 683                sta->last_auth = jiffies;
 684        } else if (status != WLAN_STATUS_SUCCESS)
 685                txt = "authentication failed";
 686
 687 done:
 688        if (sta)
 689                atomic_dec(&sta->users);
 690        if (txt) {
 691                PDEBUG(DEBUG_AP, "%s: %pM auth_cb - alg=%d "
 692                       "trans#=%d status=%d - %s\n",
 693                       dev->name, hdr->addr1,
 694                       auth_alg, auth_transaction, status, txt);
 695        }
 696        dev_kfree_skb(skb);
 697}
 698
 699
 700/* Called only as a tasklet (software IRQ) */
 701static void hostap_ap_tx_cb_assoc(struct sk_buff *skb, int ok, void *data)
 702{
 703        struct ap_data *ap = data;
 704        struct net_device *dev = ap->local->dev;
 705        struct ieee80211_hdr *hdr;
 706        u16 status;
 707        __le16 *pos;
 708        struct sta_info *sta = NULL;
 709        char *txt = NULL;
 710
 711        if (ap->local->hostapd) {
 712                dev_kfree_skb(skb);
 713                return;
 714        }
 715
 716        hdr = (struct ieee80211_hdr *) skb->data;
 717        if ((!ieee80211_is_assoc_resp(hdr->frame_control) &&
 718             !ieee80211_is_reassoc_resp(hdr->frame_control)) ||
 719            skb->len < IEEE80211_MGMT_HDR_LEN + 4) {
 720                printk(KERN_DEBUG "%s: hostap_ap_tx_cb_assoc received invalid "
 721                       "frame\n", dev->name);
 722                dev_kfree_skb(skb);
 723                return;
 724        }
 725
 726        if (!ok) {
 727                txt = "frame was not ACKed";
 728                goto done;
 729        }
 730
 731        spin_lock(&ap->sta_table_lock);
 732        sta = ap_get_sta(ap, hdr->addr1);
 733        if (sta)
 734                atomic_inc(&sta->users);
 735        spin_unlock(&ap->sta_table_lock);
 736
 737        if (!sta) {
 738                txt = "STA not found";
 739                goto done;
 740        }
 741
 742        pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
 743        pos++;
 744        status = le16_to_cpu(*pos++);
 745        if (status == WLAN_STATUS_SUCCESS) {
 746                if (!(sta->flags & WLAN_STA_ASSOC))
 747                        hostap_event_new_sta(dev, sta);
 748                txt = "STA associated";
 749                sta->flags |= WLAN_STA_ASSOC;
 750                sta->last_assoc = jiffies;
 751        } else
 752                txt = "association failed";
 753
 754 done:
 755        if (sta)
 756                atomic_dec(&sta->users);
 757        if (txt) {
 758                PDEBUG(DEBUG_AP, "%s: %pM assoc_cb - %s\n",
 759                       dev->name, hdr->addr1, txt);
 760        }
 761        dev_kfree_skb(skb);
 762}
 763
 764/* Called only as a tasklet (software IRQ); TX callback for poll frames used
 765 * in verifying whether the STA is still present. */
 766static void hostap_ap_tx_cb_poll(struct sk_buff *skb, int ok, void *data)
 767{
 768        struct ap_data *ap = data;
 769        struct ieee80211_hdr *hdr;
 770        struct sta_info *sta;
 771
 772        if (skb->len < 24)
 773                goto fail;
 774        hdr = (struct ieee80211_hdr *) skb->data;
 775        if (ok) {
 776                spin_lock(&ap->sta_table_lock);
 777                sta = ap_get_sta(ap, hdr->addr1);
 778                if (sta)
 779                        sta->flags &= ~WLAN_STA_PENDING_POLL;
 780                spin_unlock(&ap->sta_table_lock);
 781        } else {
 782                PDEBUG(DEBUG_AP,
 783                       "%s: STA %pM did not ACK activity poll frame\n",
 784                       ap->local->dev->name, hdr->addr1);
 785        }
 786
 787 fail:
 788        dev_kfree_skb(skb);
 789}
 790#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
 791
 792
 793void hostap_init_data(local_info_t *local)
 794{
 795        struct ap_data *ap = local->ap;
 796
 797        if (ap == NULL) {
 798                printk(KERN_WARNING "hostap_init_data: ap == NULL\n");
 799                return;
 800        }
 801        memset(ap, 0, sizeof(struct ap_data));
 802        ap->local = local;
 803
 804        ap->ap_policy = GET_INT_PARM(other_ap_policy, local->card_idx);
 805        ap->bridge_packets = GET_INT_PARM(ap_bridge_packets, local->card_idx);
 806        ap->max_inactivity =
 807                GET_INT_PARM(ap_max_inactivity, local->card_idx) * HZ;
 808        ap->autom_ap_wds = GET_INT_PARM(autom_ap_wds, local->card_idx);
 809
 810        spin_lock_init(&ap->sta_table_lock);
 811        INIT_LIST_HEAD(&ap->sta_list);
 812
 813        /* Initialize task queue structure for AP management */
 814        INIT_WORK(&local->ap->add_sta_proc_queue, handle_add_proc_queue);
 815
 816        ap->tx_callback_idx =
 817                hostap_tx_callback_register(local, hostap_ap_tx_cb, ap);
 818        if (ap->tx_callback_idx == 0)
 819                printk(KERN_WARNING "%s: failed to register TX callback for "
 820                       "AP\n", local->dev->name);
 821#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
 822        INIT_WORK(&local->ap->wds_oper_queue, handle_wds_oper_queue);
 823
 824        ap->tx_callback_auth =
 825                hostap_tx_callback_register(local, hostap_ap_tx_cb_auth, ap);
 826        ap->tx_callback_assoc =
 827                hostap_tx_callback_register(local, hostap_ap_tx_cb_assoc, ap);
 828        ap->tx_callback_poll =
 829                hostap_tx_callback_register(local, hostap_ap_tx_cb_poll, ap);
 830        if (ap->tx_callback_auth == 0 || ap->tx_callback_assoc == 0 ||
 831                ap->tx_callback_poll == 0)
 832                printk(KERN_WARNING "%s: failed to register TX callback for "
 833                       "AP\n", local->dev->name);
 834
 835        spin_lock_init(&ap->mac_restrictions.lock);
 836        INIT_LIST_HEAD(&ap->mac_restrictions.mac_list);
 837#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
 838
 839        ap->initialized = 1;
 840}
 841
 842
 843void hostap_init_ap_proc(local_info_t *local)
 844{
 845        struct ap_data *ap = local->ap;
 846
 847        ap->proc = local->proc;
 848        if (ap->proc == NULL)
 849                return;
 850
 851#ifndef PRISM2_NO_PROCFS_DEBUG
 852        proc_create_single_data("ap_debug", 0, ap->proc, ap_debug_proc_show, ap);
 853#endif /* PRISM2_NO_PROCFS_DEBUG */
 854
 855#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
 856        proc_create_seq_data("ap_control", 0, ap->proc, &ap_control_proc_seqops,
 857                        ap);
 858        proc_create_seq_data("ap", 0, ap->proc, &prism2_ap_proc_seqops, ap);
 859#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
 860
 861}
 862
 863
 864void hostap_free_data(struct ap_data *ap)
 865{
 866        struct sta_info *n, *sta;
 867
 868        if (ap == NULL || !ap->initialized) {
 869                printk(KERN_DEBUG "hostap_free_data: ap has not yet been "
 870                       "initialized - skip resource freeing\n");
 871                return;
 872        }
 873
 874        flush_work(&ap->add_sta_proc_queue);
 875
 876#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
 877        flush_work(&ap->wds_oper_queue);
 878        if (ap->crypt)
 879                ap->crypt->deinit(ap->crypt_priv);
 880        ap->crypt = ap->crypt_priv = NULL;
 881#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
 882
 883        list_for_each_entry_safe(sta, n, &ap->sta_list, list) {
 884                ap_sta_hash_del(ap, sta);
 885                list_del(&sta->list);
 886                if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
 887                        hostap_event_expired_sta(sta->local->dev, sta);
 888                ap_free_sta(ap, sta);
 889        }
 890
 891#ifndef PRISM2_NO_PROCFS_DEBUG
 892        if (ap->proc != NULL) {
 893                remove_proc_entry("ap_debug", ap->proc);
 894        }
 895#endif /* PRISM2_NO_PROCFS_DEBUG */
 896
 897#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
 898        if (ap->proc != NULL) {
 899          remove_proc_entry("ap", ap->proc);
 900                remove_proc_entry("ap_control", ap->proc);
 901        }
 902        ap_control_flush_macs(&ap->mac_restrictions);
 903#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
 904
 905        ap->initialized = 0;
 906}
 907
 908
 909/* caller should have mutex for AP STA list handling */
 910static struct sta_info* ap_get_sta(struct ap_data *ap, u8 *sta)
 911{
 912        struct sta_info *s;
 913
 914        s = ap->sta_hash[STA_HASH(sta)];
 915        while (s != NULL && !ether_addr_equal(s->addr, sta))
 916                s = s->hnext;
 917        return s;
 918}
 919
 920
 921#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
 922
 923/* Called from timer handler and from scheduled AP queue handlers */
 924static void prism2_send_mgmt(struct net_device *dev,
 925                             u16 type_subtype, char *body,
 926                             int body_len, u8 *addr, u16 tx_cb_idx)
 927{
 928        struct hostap_interface *iface;
 929        local_info_t *local;
 930        struct ieee80211_hdr *hdr;
 931        u16 fc;
 932        struct sk_buff *skb;
 933        struct hostap_skb_tx_data *meta;
 934        int hdrlen;
 935
 936        iface = netdev_priv(dev);
 937        local = iface->local;
 938        dev = local->dev; /* always use master radio device */
 939        iface = netdev_priv(dev);
 940
 941        if (!(dev->flags & IFF_UP)) {
 942                PDEBUG(DEBUG_AP, "%s: prism2_send_mgmt - device is not UP - "
 943                       "cannot send frame\n", dev->name);
 944                return;
 945        }
 946
 947        skb = dev_alloc_skb(sizeof(*hdr) + body_len);
 948        if (skb == NULL) {
 949                PDEBUG(DEBUG_AP, "%s: prism2_send_mgmt failed to allocate "
 950                       "skb\n", dev->name);
 951                return;
 952        }
 953
 954        fc = type_subtype;
 955        hdrlen = hostap_80211_get_hdrlen(cpu_to_le16(type_subtype));
 956        hdr = skb_put_zero(skb, hdrlen);
 957        if (body)
 958                skb_put_data(skb, body, body_len);
 959
 960        /* FIX: ctrl::ack sending used special HFA384X_TX_CTRL_802_11
 961         * tx_control instead of using local->tx_control */
 962
 963
 964        memcpy(hdr->addr1, addr, ETH_ALEN); /* DA / RA */
 965        if (ieee80211_is_data(hdr->frame_control)) {
 966                fc |= IEEE80211_FCTL_FROMDS;
 967                memcpy(hdr->addr2, dev->dev_addr, ETH_ALEN); /* BSSID */
 968                memcpy(hdr->addr3, dev->dev_addr, ETH_ALEN); /* SA */
 969        } else if (ieee80211_is_ctl(hdr->frame_control)) {
 970                /* control:ACK does not have addr2 or addr3 */
 971                eth_zero_addr(hdr->addr2);
 972                eth_zero_addr(hdr->addr3);
 973        } else {
 974                memcpy(hdr->addr2, dev->dev_addr, ETH_ALEN); /* SA */
 975                memcpy(hdr->addr3, dev->dev_addr, ETH_ALEN); /* BSSID */
 976        }
 977
 978        hdr->frame_control = cpu_to_le16(fc);
 979
 980        meta = (struct hostap_skb_tx_data *) skb->cb;
 981        memset(meta, 0, sizeof(*meta));
 982        meta->magic = HOSTAP_SKB_TX_DATA_MAGIC;
 983        meta->iface = iface;
 984        meta->tx_cb_idx = tx_cb_idx;
 985
 986        skb->dev = dev;
 987        skb_reset_mac_header(skb);
 988        skb_reset_network_header(skb);
 989        dev_queue_xmit(skb);
 990}
 991#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
 992
 993
 994static int prism2_sta_proc_show(struct seq_file *m, void *v)
 995{
 996        struct sta_info *sta = m->private;
 997        int i;
 998
 999        /* FIX: possible race condition.. the STA data could have just expired,
1000         * but proc entry was still here so that the read could have started;
1001         * some locking should be done here.. */
1002
1003        seq_printf(m,
1004                   "%s=%pM\nusers=%d\naid=%d\n"
1005                   "flags=0x%04x%s%s%s%s%s%s%s\n"
1006                   "capability=0x%02x\nlisten_interval=%d\nsupported_rates=",
1007                   sta->ap ? "AP" : "STA",
1008                   sta->addr, atomic_read(&sta->users), sta->aid,
1009                   sta->flags,
1010                   sta->flags & WLAN_STA_AUTH ? " AUTH" : "",
1011                   sta->flags & WLAN_STA_ASSOC ? " ASSOC" : "",
1012                   sta->flags & WLAN_STA_PS ? " PS" : "",
1013                   sta->flags & WLAN_STA_TIM ? " TIM" : "",
1014                   sta->flags & WLAN_STA_PERM ? " PERM" : "",
1015                   sta->flags & WLAN_STA_AUTHORIZED ? " AUTHORIZED" : "",
1016                   sta->flags & WLAN_STA_PENDING_POLL ? " POLL" : "",
1017                   sta->capability, sta->listen_interval);
1018        /* supported_rates: 500 kbit/s units with msb ignored */
1019        for (i = 0; i < sizeof(sta->supported_rates); i++)
1020                if (sta->supported_rates[i] != 0)
1021                        seq_printf(m, "%d%sMbps ",
1022                                   (sta->supported_rates[i] & 0x7f) / 2,
1023                                   sta->supported_rates[i] & 1 ? ".5" : "");
1024        seq_printf(m,
1025                   "\njiffies=%lu\nlast_auth=%lu\nlast_assoc=%lu\n"
1026                   "last_rx=%lu\nlast_tx=%lu\nrx_packets=%lu\n"
1027                   "tx_packets=%lu\n"
1028                   "rx_bytes=%lu\ntx_bytes=%lu\nbuffer_count=%d\n"
1029                   "last_rx: silence=%d dBm signal=%d dBm rate=%d%s Mbps\n"
1030                   "tx_rate=%d\ntx[1M]=%d\ntx[2M]=%d\ntx[5.5M]=%d\n"
1031                   "tx[11M]=%d\n"
1032                   "rx[1M]=%d\nrx[2M]=%d\nrx[5.5M]=%d\nrx[11M]=%d\n",
1033                   jiffies, sta->last_auth, sta->last_assoc, sta->last_rx,
1034                   sta->last_tx,
1035                   sta->rx_packets, sta->tx_packets, sta->rx_bytes,
1036                   sta->tx_bytes, skb_queue_len(&sta->tx_buf),
1037                   sta->last_rx_silence,
1038                   sta->last_rx_signal, sta->last_rx_rate / 10,
1039                   sta->last_rx_rate % 10 ? ".5" : "",
1040                   sta->tx_rate, sta->tx_count[0], sta->tx_count[1],
1041                   sta->tx_count[2], sta->tx_count[3],  sta->rx_count[0],
1042                   sta->rx_count[1], sta->rx_count[2], sta->rx_count[3]);
1043        if (sta->crypt && sta->crypt->ops && sta->crypt->ops->print_stats)
1044                sta->crypt->ops->print_stats(m, sta->crypt->priv);
1045#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1046        if (sta->ap) {
1047                if (sta->u.ap.channel >= 0)
1048                        seq_printf(m, "channel=%d\n", sta->u.ap.channel);
1049                seq_puts(m, "ssid=");
1050                for (i = 0; i < sta->u.ap.ssid_len; i++) {
1051                        if (sta->u.ap.ssid[i] >= 32 && sta->u.ap.ssid[i] < 127)
1052                                seq_putc(m, sta->u.ap.ssid[i]);
1053                        else
1054                                seq_printf(m, "<%02x>", sta->u.ap.ssid[i]);
1055                }
1056                seq_putc(m, '\n');
1057        }
1058#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
1059
1060        return 0;
1061}
1062
1063static void handle_add_proc_queue(struct work_struct *work)
1064{
1065        struct ap_data *ap = container_of(work, struct ap_data,
1066                                          add_sta_proc_queue);
1067        struct sta_info *sta;
1068        char name[20];
1069        struct add_sta_proc_data *entry, *prev;
1070
1071        entry = ap->add_sta_proc_entries;
1072        ap->add_sta_proc_entries = NULL;
1073
1074        while (entry) {
1075                spin_lock_bh(&ap->sta_table_lock);
1076                sta = ap_get_sta(ap, entry->addr);
1077                if (sta)
1078                        atomic_inc(&sta->users);
1079                spin_unlock_bh(&ap->sta_table_lock);
1080
1081                if (sta) {
1082                        sprintf(name, "%pM", sta->addr);
1083                        sta->proc = proc_create_single_data(
1084                                name, 0, ap->proc,
1085                                prism2_sta_proc_show, sta);
1086
1087                        atomic_dec(&sta->users);
1088                }
1089
1090                prev = entry;
1091                entry = entry->next;
1092                kfree(prev);
1093        }
1094}
1095
1096
1097static struct sta_info * ap_add_sta(struct ap_data *ap, u8 *addr)
1098{
1099        struct sta_info *sta;
1100
1101        sta = kzalloc(sizeof(struct sta_info), GFP_ATOMIC);
1102        if (sta == NULL) {
1103                PDEBUG(DEBUG_AP, "AP: kmalloc failed\n");
1104                return NULL;
1105        }
1106
1107        /* initialize STA info data */
1108        sta->local = ap->local;
1109        skb_queue_head_init(&sta->tx_buf);
1110        memcpy(sta->addr, addr, ETH_ALEN);
1111
1112        atomic_inc(&sta->users);
1113        spin_lock_bh(&ap->sta_table_lock);
1114        list_add(&sta->list, &ap->sta_list);
1115        ap->num_sta++;
1116        ap_sta_hash_add(ap, sta);
1117        spin_unlock_bh(&ap->sta_table_lock);
1118
1119        if (ap->proc) {
1120                struct add_sta_proc_data *entry;
1121                /* schedule a non-interrupt context process to add a procfs
1122                 * entry for the STA since procfs code use GFP_KERNEL */
1123                entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
1124                if (entry) {
1125                        memcpy(entry->addr, sta->addr, ETH_ALEN);
1126                        entry->next = ap->add_sta_proc_entries;
1127                        ap->add_sta_proc_entries = entry;
1128                        schedule_work(&ap->add_sta_proc_queue);
1129                } else
1130                        printk(KERN_DEBUG "Failed to add STA proc data\n");
1131        }
1132
1133#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1134        timer_setup(&sta->timer, ap_handle_timer, 0);
1135        sta->timer.expires = jiffies + ap->max_inactivity;
1136        if (!ap->local->hostapd)
1137                add_timer(&sta->timer);
1138#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
1139
1140        return sta;
1141}
1142
1143
1144static int ap_tx_rate_ok(int rateidx, struct sta_info *sta,
1145                         local_info_t *local)
1146{
1147        if (rateidx > sta->tx_max_rate ||
1148            !(sta->tx_supp_rates & (1 << rateidx)))
1149                return 0;
1150
1151        if (local->tx_rate_control != 0 &&
1152            !(local->tx_rate_control & (1 << rateidx)))
1153                return 0;
1154
1155        return 1;
1156}
1157
1158
1159static void prism2_check_tx_rates(struct sta_info *sta)
1160{
1161        int i;
1162
1163        sta->tx_supp_rates = 0;
1164        for (i = 0; i < sizeof(sta->supported_rates); i++) {
1165                if ((sta->supported_rates[i] & 0x7f) == 2)
1166                        sta->tx_supp_rates |= WLAN_RATE_1M;
1167                if ((sta->supported_rates[i] & 0x7f) == 4)
1168                        sta->tx_supp_rates |= WLAN_RATE_2M;
1169                if ((sta->supported_rates[i] & 0x7f) == 11)
1170                        sta->tx_supp_rates |= WLAN_RATE_5M5;
1171                if ((sta->supported_rates[i] & 0x7f) == 22)
1172                        sta->tx_supp_rates |= WLAN_RATE_11M;
1173        }
1174        sta->tx_max_rate = sta->tx_rate = sta->tx_rate_idx = 0;
1175        if (sta->tx_supp_rates & WLAN_RATE_1M) {
1176                sta->tx_max_rate = 0;
1177                if (ap_tx_rate_ok(0, sta, sta->local)) {
1178                        sta->tx_rate = 10;
1179                        sta->tx_rate_idx = 0;
1180                }
1181        }
1182        if (sta->tx_supp_rates & WLAN_RATE_2M) {
1183                sta->tx_max_rate = 1;
1184                if (ap_tx_rate_ok(1, sta, sta->local)) {
1185                        sta->tx_rate = 20;
1186                        sta->tx_rate_idx = 1;
1187                }
1188        }
1189        if (sta->tx_supp_rates & WLAN_RATE_5M5) {
1190                sta->tx_max_rate = 2;
1191                if (ap_tx_rate_ok(2, sta, sta->local)) {
1192                        sta->tx_rate = 55;
1193                        sta->tx_rate_idx = 2;
1194                }
1195        }
1196        if (sta->tx_supp_rates & WLAN_RATE_11M) {
1197                sta->tx_max_rate = 3;
1198                if (ap_tx_rate_ok(3, sta, sta->local)) {
1199                        sta->tx_rate = 110;
1200                        sta->tx_rate_idx = 3;
1201                }
1202        }
1203}
1204
1205
1206#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1207
1208static void ap_crypt_init(struct ap_data *ap)
1209{
1210        ap->crypt = lib80211_get_crypto_ops("WEP");
1211
1212        if (ap->crypt) {
1213                if (ap->crypt->init) {
1214                        ap->crypt_priv = ap->crypt->init(0);
1215                        if (ap->crypt_priv == NULL)
1216                                ap->crypt = NULL;
1217                        else {
1218                                u8 key[WEP_KEY_LEN];
1219                                get_random_bytes(key, WEP_KEY_LEN);
1220                                ap->crypt->set_key(key, WEP_KEY_LEN, NULL,
1221                                                   ap->crypt_priv);
1222                        }
1223                }
1224        }
1225
1226        if (ap->crypt == NULL) {
1227                printk(KERN_WARNING "AP could not initialize WEP: load module "
1228                       "lib80211_crypt_wep.ko\n");
1229        }
1230}
1231
1232
1233/* Generate challenge data for shared key authentication. IEEE 802.11 specifies
1234 * that WEP algorithm is used for generating challenge. This should be unique,
1235 * but otherwise there is not really need for randomness etc. Initialize WEP
1236 * with pseudo random key and then use increasing IV to get unique challenge
1237 * streams.
1238 *
1239 * Called only as a scheduled task for pending AP frames.
1240 */
1241static char * ap_auth_make_challenge(struct ap_data *ap)
1242{
1243        char *tmpbuf;
1244        struct sk_buff *skb;
1245
1246        if (ap->crypt == NULL) {
1247                ap_crypt_init(ap);
1248                if (ap->crypt == NULL)
1249                        return NULL;
1250        }
1251
1252        tmpbuf = kmalloc(WLAN_AUTH_CHALLENGE_LEN, GFP_ATOMIC);
1253        if (tmpbuf == NULL) {
1254                PDEBUG(DEBUG_AP, "AP: kmalloc failed for challenge\n");
1255                return NULL;
1256        }
1257
1258        skb = dev_alloc_skb(WLAN_AUTH_CHALLENGE_LEN +
1259                            ap->crypt->extra_mpdu_prefix_len +
1260                            ap->crypt->extra_mpdu_postfix_len);
1261        if (skb == NULL) {
1262                kfree(tmpbuf);
1263                return NULL;
1264        }
1265
1266        skb_reserve(skb, ap->crypt->extra_mpdu_prefix_len);
1267        skb_put_zero(skb, WLAN_AUTH_CHALLENGE_LEN);
1268        if (ap->crypt->encrypt_mpdu(skb, 0, ap->crypt_priv)) {
1269                dev_kfree_skb(skb);
1270                kfree(tmpbuf);
1271                return NULL;
1272        }
1273
1274        skb_copy_from_linear_data_offset(skb, ap->crypt->extra_mpdu_prefix_len,
1275                                         tmpbuf, WLAN_AUTH_CHALLENGE_LEN);
1276        dev_kfree_skb(skb);
1277
1278        return tmpbuf;
1279}
1280
1281
1282/* Called only as a scheduled task for pending AP frames. */
1283static void handle_authen(local_info_t *local, struct sk_buff *skb,
1284                          struct hostap_80211_rx_status *rx_stats)
1285{
1286        struct net_device *dev = local->dev;
1287        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1288        size_t hdrlen;
1289        struct ap_data *ap = local->ap;
1290        char body[8 + WLAN_AUTH_CHALLENGE_LEN], *challenge = NULL;
1291        int len, olen;
1292        u16 auth_alg, auth_transaction, status_code;
1293        __le16 *pos;
1294        u16 resp = WLAN_STATUS_SUCCESS;
1295        struct sta_info *sta = NULL;
1296        struct lib80211_crypt_data *crypt;
1297        char *txt = "";
1298
1299        len = skb->len - IEEE80211_MGMT_HDR_LEN;
1300
1301        hdrlen = hostap_80211_get_hdrlen(hdr->frame_control);
1302
1303        if (len < 6) {
1304                PDEBUG(DEBUG_AP, "%s: handle_authen - too short payload "
1305                       "(len=%d) from %pM\n", dev->name, len, hdr->addr2);
1306                return;
1307        }
1308
1309        spin_lock_bh(&local->ap->sta_table_lock);
1310        sta = ap_get_sta(local->ap, hdr->addr2);
1311        if (sta)
1312                atomic_inc(&sta->users);
1313        spin_unlock_bh(&local->ap->sta_table_lock);
1314
1315        if (sta && sta->crypt)
1316                crypt = sta->crypt;
1317        else {
1318                int idx = 0;
1319                if (skb->len >= hdrlen + 3)
1320                        idx = skb->data[hdrlen + 3] >> 6;
1321                crypt = local->crypt_info.crypt[idx];
1322        }
1323
1324        pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
1325        auth_alg = __le16_to_cpu(*pos);
1326        pos++;
1327        auth_transaction = __le16_to_cpu(*pos);
1328        pos++;
1329        status_code = __le16_to_cpu(*pos);
1330        pos++;
1331
1332        if (ether_addr_equal(dev->dev_addr, hdr->addr2) ||
1333            ap_control_mac_deny(&ap->mac_restrictions, hdr->addr2)) {
1334                txt = "authentication denied";
1335                resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1336                goto fail;
1337        }
1338
1339        if (((local->auth_algs & PRISM2_AUTH_OPEN) &&
1340             auth_alg == WLAN_AUTH_OPEN) ||
1341            ((local->auth_algs & PRISM2_AUTH_SHARED_KEY) &&
1342             crypt && auth_alg == WLAN_AUTH_SHARED_KEY)) {
1343        } else {
1344                txt = "unsupported algorithm";
1345                resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1346                goto fail;
1347        }
1348
1349        if (len >= 8) {
1350                u8 *u = (u8 *) pos;
1351                if (*u == WLAN_EID_CHALLENGE) {
1352                        if (*(u + 1) != WLAN_AUTH_CHALLENGE_LEN) {
1353                                txt = "invalid challenge len";
1354                                resp = WLAN_STATUS_CHALLENGE_FAIL;
1355                                goto fail;
1356                        }
1357                        if (len - 8 < WLAN_AUTH_CHALLENGE_LEN) {
1358                                txt = "challenge underflow";
1359                                resp = WLAN_STATUS_CHALLENGE_FAIL;
1360                                goto fail;
1361                        }
1362                        challenge = (char *) (u + 2);
1363                }
1364        }
1365
1366        if (sta && sta->ap) {
1367                if (time_after(jiffies, sta->u.ap.last_beacon +
1368                               (10 * sta->listen_interval * HZ) / 1024)) {
1369                        PDEBUG(DEBUG_AP, "%s: no beacons received for a while,"
1370                               " assuming AP %pM is now STA\n",
1371                               dev->name, sta->addr);
1372                        sta->ap = 0;
1373                        sta->flags = 0;
1374                        sta->u.sta.challenge = NULL;
1375                } else {
1376                        txt = "AP trying to authenticate?";
1377                        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1378                        goto fail;
1379                }
1380        }
1381
1382        if ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 1) ||
1383            (auth_alg == WLAN_AUTH_SHARED_KEY &&
1384             (auth_transaction == 1 ||
1385              (auth_transaction == 3 && sta != NULL &&
1386               sta->u.sta.challenge != NULL)))) {
1387        } else {
1388                txt = "unknown authentication transaction number";
1389                resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
1390                goto fail;
1391        }
1392
1393        if (sta == NULL) {
1394                txt = "new STA";
1395
1396                if (local->ap->num_sta >= MAX_STA_COUNT) {
1397                        /* FIX: might try to remove some old STAs first? */
1398                        txt = "no more room for new STAs";
1399                        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1400                        goto fail;
1401                }
1402
1403                sta = ap_add_sta(local->ap, hdr->addr2);
1404                if (sta == NULL) {
1405                        txt = "ap_add_sta failed";
1406                        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1407                        goto fail;
1408                }
1409        }
1410
1411        switch (auth_alg) {
1412        case WLAN_AUTH_OPEN:
1413                txt = "authOK";
1414                /* IEEE 802.11 standard is not completely clear about
1415                 * whether STA is considered authenticated after
1416                 * authentication OK frame has been send or after it
1417                 * has been ACKed. In order to reduce interoperability
1418                 * issues, mark the STA authenticated before ACK. */
1419                sta->flags |= WLAN_STA_AUTH;
1420                break;
1421
1422        case WLAN_AUTH_SHARED_KEY:
1423                if (auth_transaction == 1) {
1424                        if (sta->u.sta.challenge == NULL) {
1425                                sta->u.sta.challenge =
1426                                        ap_auth_make_challenge(local->ap);
1427                                if (sta->u.sta.challenge == NULL) {
1428                                        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1429                                        goto fail;
1430                                }
1431                        }
1432                } else {
1433                        if (sta->u.sta.challenge == NULL ||
1434                            challenge == NULL ||
1435                            memcmp(sta->u.sta.challenge, challenge,
1436                                   WLAN_AUTH_CHALLENGE_LEN) != 0 ||
1437                            !ieee80211_has_protected(hdr->frame_control)) {
1438                                txt = "challenge response incorrect";
1439                                resp = WLAN_STATUS_CHALLENGE_FAIL;
1440                                goto fail;
1441                        }
1442
1443                        txt = "challenge OK - authOK";
1444                        /* IEEE 802.11 standard is not completely clear about
1445                         * whether STA is considered authenticated after
1446                         * authentication OK frame has been send or after it
1447                         * has been ACKed. In order to reduce interoperability
1448                         * issues, mark the STA authenticated before ACK. */
1449                        sta->flags |= WLAN_STA_AUTH;
1450                        kfree(sta->u.sta.challenge);
1451                        sta->u.sta.challenge = NULL;
1452                }
1453                break;
1454        }
1455
1456 fail:
1457        pos = (__le16 *) body;
1458        *pos = cpu_to_le16(auth_alg);
1459        pos++;
1460        *pos = cpu_to_le16(auth_transaction + 1);
1461        pos++;
1462        *pos = cpu_to_le16(resp); /* status_code */
1463        pos++;
1464        olen = 6;
1465
1466        if (resp == WLAN_STATUS_SUCCESS && sta != NULL &&
1467            sta->u.sta.challenge != NULL &&
1468            auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 1) {
1469                u8 *tmp = (u8 *) pos;
1470                *tmp++ = WLAN_EID_CHALLENGE;
1471                *tmp++ = WLAN_AUTH_CHALLENGE_LEN;
1472                pos++;
1473                memcpy(pos, sta->u.sta.challenge, WLAN_AUTH_CHALLENGE_LEN);
1474                olen += 2 + WLAN_AUTH_CHALLENGE_LEN;
1475        }
1476
1477        prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_AUTH,
1478                         body, olen, hdr->addr2, ap->tx_callback_auth);
1479
1480        if (sta) {
1481                sta->last_rx = jiffies;
1482                atomic_dec(&sta->users);
1483        }
1484
1485        if (resp) {
1486                PDEBUG(DEBUG_AP, "%s: %pM auth (alg=%d "
1487                       "trans#=%d stat=%d len=%d fc=%04x) ==> %d (%s)\n",
1488                       dev->name, hdr->addr2,
1489                       auth_alg, auth_transaction, status_code, len,
1490                       le16_to_cpu(hdr->frame_control), resp, txt);
1491        }
1492}
1493
1494
1495/* Called only as a scheduled task for pending AP frames. */
1496static void handle_assoc(local_info_t *local, struct sk_buff *skb,
1497                         struct hostap_80211_rx_status *rx_stats, int reassoc)
1498{
1499        struct net_device *dev = local->dev;
1500        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1501        char body[12], *p, *lpos;
1502        int len, left;
1503        __le16 *pos;
1504        u16 resp = WLAN_STATUS_SUCCESS;
1505        struct sta_info *sta = NULL;
1506        int send_deauth = 0;
1507        char *txt = "";
1508        u8 prev_ap[ETH_ALEN];
1509
1510        left = len = skb->len - IEEE80211_MGMT_HDR_LEN;
1511
1512        if (len < (reassoc ? 10 : 4)) {
1513                PDEBUG(DEBUG_AP, "%s: handle_assoc - too short payload "
1514                       "(len=%d, reassoc=%d) from %pM\n",
1515                       dev->name, len, reassoc, hdr->addr2);
1516                return;
1517        }
1518
1519        spin_lock_bh(&local->ap->sta_table_lock);
1520        sta = ap_get_sta(local->ap, hdr->addr2);
1521        if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
1522                spin_unlock_bh(&local->ap->sta_table_lock);
1523                txt = "trying to associate before authentication";
1524                send_deauth = 1;
1525                resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1526                sta = NULL; /* do not decrement sta->users */
1527                goto fail;
1528        }
1529        atomic_inc(&sta->users);
1530        spin_unlock_bh(&local->ap->sta_table_lock);
1531
1532        pos = (__le16 *) (skb->data + IEEE80211_MGMT_HDR_LEN);
1533        sta->capability = __le16_to_cpu(*pos);
1534        pos++; left -= 2;
1535        sta->listen_interval = __le16_to_cpu(*pos);
1536        pos++; left -= 2;
1537
1538        if (reassoc) {
1539                memcpy(prev_ap, pos, ETH_ALEN);
1540                pos++; pos++; pos++; left -= 6;
1541        } else
1542                eth_zero_addr(prev_ap);
1543
1544        if (left >= 2) {
1545                unsigned int ileft;
1546                unsigned char *u = (unsigned char *) pos;
1547
1548                if (*u == WLAN_EID_SSID) {
1549                        u++; left--;
1550                        ileft = *u;
1551                        u++; left--;
1552
1553                        if (ileft > left || ileft > MAX_SSID_LEN) {
1554                                txt = "SSID overflow";
1555                                resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1556                                goto fail;
1557                        }
1558
1559                        if (ileft != strlen(local->essid) ||
1560                            memcmp(local->essid, u, ileft) != 0) {
1561                                txt = "not our SSID";
1562                                resp = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
1563                                goto fail;
1564                        }
1565
1566                        u += ileft;
1567                        left -= ileft;
1568                }
1569
1570                if (left >= 2 && *u == WLAN_EID_SUPP_RATES) {
1571                        u++; left--;
1572                        ileft = *u;
1573                        u++; left--;
1574
1575                        if (ileft > left || ileft == 0 ||
1576                            ileft > WLAN_SUPP_RATES_MAX) {
1577                                txt = "SUPP_RATES len error";
1578                                resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1579                                goto fail;
1580                        }
1581
1582                        memset(sta->supported_rates, 0,
1583                               sizeof(sta->supported_rates));
1584                        memcpy(sta->supported_rates, u, ileft);
1585                        prism2_check_tx_rates(sta);
1586
1587                        u += ileft;
1588                        left -= ileft;
1589                }
1590
1591                if (left > 0) {
1592                        PDEBUG(DEBUG_AP, "%s: assoc from %pM"
1593                               " with extra data (%d bytes) [",
1594                               dev->name, hdr->addr2, left);
1595                        while (left > 0) {
1596                                PDEBUG2(DEBUG_AP, "<%02x>", *u);
1597                                u++; left--;
1598                        }
1599                        PDEBUG2(DEBUG_AP, "]\n");
1600                }
1601        } else {
1602                txt = "frame underflow";
1603                resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1604                goto fail;
1605        }
1606
1607        /* get a unique AID */
1608        if (sta->aid > 0)
1609                txt = "OK, old AID";
1610        else {
1611                spin_lock_bh(&local->ap->sta_table_lock);
1612                for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
1613                        if (local->ap->sta_aid[sta->aid - 1] == NULL)
1614                                break;
1615                if (sta->aid > MAX_AID_TABLE_SIZE) {
1616                        sta->aid = 0;
1617                        spin_unlock_bh(&local->ap->sta_table_lock);
1618                        resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1619                        txt = "no room for more AIDs";
1620                } else {
1621                        local->ap->sta_aid[sta->aid - 1] = sta;
1622                        spin_unlock_bh(&local->ap->sta_table_lock);
1623                        txt = "OK, new AID";
1624                }
1625        }
1626
1627 fail:
1628        pos = (__le16 *) body;
1629
1630        if (send_deauth) {
1631                *pos = cpu_to_le16(WLAN_REASON_STA_REQ_ASSOC_WITHOUT_AUTH);
1632                pos++;
1633        } else {
1634                /* FIX: CF-Pollable and CF-PollReq should be set to match the
1635                 * values in beacons/probe responses */
1636                /* FIX: how about privacy and WEP? */
1637                /* capability */
1638                *pos = cpu_to_le16(WLAN_CAPABILITY_ESS);
1639                pos++;
1640
1641                /* status_code */
1642                *pos = cpu_to_le16(resp);
1643                pos++;
1644
1645                *pos = cpu_to_le16((sta && sta->aid > 0 ? sta->aid : 0) |
1646                                     BIT(14) | BIT(15)); /* AID */
1647                pos++;
1648
1649                /* Supported rates (Information element) */
1650                p = (char *) pos;
1651                *p++ = WLAN_EID_SUPP_RATES;
1652                lpos = p;
1653                *p++ = 0; /* len */
1654                if (local->tx_rate_control & WLAN_RATE_1M) {
1655                        *p++ = local->basic_rates & WLAN_RATE_1M ? 0x82 : 0x02;
1656                        (*lpos)++;
1657                }
1658                if (local->tx_rate_control & WLAN_RATE_2M) {
1659                        *p++ = local->basic_rates & WLAN_RATE_2M ? 0x84 : 0x04;
1660                        (*lpos)++;
1661                }
1662                if (local->tx_rate_control & WLAN_RATE_5M5) {
1663                        *p++ = local->basic_rates & WLAN_RATE_5M5 ?
1664                                0x8b : 0x0b;
1665                        (*lpos)++;
1666                }
1667                if (local->tx_rate_control & WLAN_RATE_11M) {
1668                        *p++ = local->basic_rates & WLAN_RATE_11M ?
1669                                0x96 : 0x16;
1670                        (*lpos)++;
1671                }
1672                pos = (__le16 *) p;
1673        }
1674
1675        prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT |
1676                         (send_deauth ? IEEE80211_STYPE_DEAUTH :
1677                          (reassoc ? IEEE80211_STYPE_REASSOC_RESP :
1678                           IEEE80211_STYPE_ASSOC_RESP)),
1679                         body, (u8 *) pos - (u8 *) body,
1680                         hdr->addr2,
1681                         send_deauth ? 0 : local->ap->tx_callback_assoc);
1682
1683        if (sta) {
1684                if (resp == WLAN_STATUS_SUCCESS) {
1685                        sta->last_rx = jiffies;
1686                        /* STA will be marked associated from TX callback, if
1687                         * AssocResp is ACKed */
1688                }
1689                atomic_dec(&sta->users);
1690        }
1691
1692#if 0
1693        PDEBUG(DEBUG_AP, "%s: %pM %sassoc (len=%d "
1694               "prev_ap=%pM) => %d(%d) (%s)\n",
1695               dev->name,
1696               hdr->addr2,
1697               reassoc ? "re" : "", len,
1698               prev_ap,
1699               resp, send_deauth, txt);
1700#endif
1701}
1702
1703
1704/* Called only as a scheduled task for pending AP frames. */
1705static void handle_deauth(local_info_t *local, struct sk_buff *skb,
1706                          struct hostap_80211_rx_status *rx_stats)
1707{
1708        struct net_device *dev = local->dev;
1709        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1710        char *body = (char *) (skb->data + IEEE80211_MGMT_HDR_LEN);
1711        int len;
1712        u16 reason_code;
1713        __le16 *pos;
1714        struct sta_info *sta = NULL;
1715
1716        len = skb->len - IEEE80211_MGMT_HDR_LEN;
1717
1718        if (len < 2) {
1719                printk("handle_deauth - too short payload (len=%d)\n", len);
1720                return;
1721        }
1722
1723        pos = (__le16 *) body;
1724        reason_code = le16_to_cpu(*pos);
1725
1726        PDEBUG(DEBUG_AP, "%s: deauthentication: %pM len=%d, "
1727               "reason_code=%d\n", dev->name, hdr->addr2,
1728               len, reason_code);
1729
1730        spin_lock_bh(&local->ap->sta_table_lock);
1731        sta = ap_get_sta(local->ap, hdr->addr2);
1732        if (sta != NULL) {
1733                if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
1734                        hostap_event_expired_sta(local->dev, sta);
1735                sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1736        }
1737        spin_unlock_bh(&local->ap->sta_table_lock);
1738        if (sta == NULL) {
1739                printk("%s: deauthentication from %pM, "
1740               "reason_code=%d, but STA not authenticated\n", dev->name,
1741                       hdr->addr2, reason_code);
1742        }
1743}
1744
1745
1746/* Called only as a scheduled task for pending AP frames. */
1747static void handle_disassoc(local_info_t *local, struct sk_buff *skb,
1748                            struct hostap_80211_rx_status *rx_stats)
1749{
1750        struct net_device *dev = local->dev;
1751        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1752        char *body = skb->data + IEEE80211_MGMT_HDR_LEN;
1753        int len;
1754        u16 reason_code;
1755        __le16 *pos;
1756        struct sta_info *sta = NULL;
1757
1758        len = skb->len - IEEE80211_MGMT_HDR_LEN;
1759
1760        if (len < 2) {
1761                printk("handle_disassoc - too short payload (len=%d)\n", len);
1762                return;
1763        }
1764
1765        pos = (__le16 *) body;
1766        reason_code = le16_to_cpu(*pos);
1767
1768        PDEBUG(DEBUG_AP, "%s: disassociation: %pM len=%d, "
1769               "reason_code=%d\n", dev->name, hdr->addr2,
1770               len, reason_code);
1771
1772        spin_lock_bh(&local->ap->sta_table_lock);
1773        sta = ap_get_sta(local->ap, hdr->addr2);
1774        if (sta != NULL) {
1775                if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap)
1776                        hostap_event_expired_sta(local->dev, sta);
1777                sta->flags &= ~WLAN_STA_ASSOC;
1778        }
1779        spin_unlock_bh(&local->ap->sta_table_lock);
1780        if (sta == NULL) {
1781                printk("%s: disassociation from %pM, "
1782                       "reason_code=%d, but STA not authenticated\n",
1783                       dev->name, hdr->addr2, reason_code);
1784        }
1785}
1786
1787
1788/* Called only as a scheduled task for pending AP frames. */
1789static void ap_handle_data_nullfunc(local_info_t *local,
1790                                    struct ieee80211_hdr *hdr)
1791{
1792        struct net_device *dev = local->dev;
1793
1794        /* some STA f/w's seem to require control::ACK frame for
1795         * data::nullfunc, but at least Prism2 station f/w version 0.8.0 does
1796         * not send this..
1797         * send control::ACK for the data::nullfunc */
1798
1799        printk(KERN_DEBUG "Sending control::ACK for data::nullfunc\n");
1800        prism2_send_mgmt(dev, IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK,
1801                         NULL, 0, hdr->addr2, 0);
1802}
1803
1804
1805/* Called only as a scheduled task for pending AP frames. */
1806static void ap_handle_dropped_data(local_info_t *local,
1807                                   struct ieee80211_hdr *hdr)
1808{
1809        struct net_device *dev = local->dev;
1810        struct sta_info *sta;
1811        __le16 reason;
1812
1813        spin_lock_bh(&local->ap->sta_table_lock);
1814        sta = ap_get_sta(local->ap, hdr->addr2);
1815        if (sta)
1816                atomic_inc(&sta->users);
1817        spin_unlock_bh(&local->ap->sta_table_lock);
1818
1819        if (sta != NULL && (sta->flags & WLAN_STA_ASSOC)) {
1820                PDEBUG(DEBUG_AP, "ap_handle_dropped_data: STA is now okay?\n");
1821                atomic_dec(&sta->users);
1822                return;
1823        }
1824
1825        reason = cpu_to_le16(WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1826        prism2_send_mgmt(dev, IEEE80211_FTYPE_MGMT |
1827                         ((sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) ?
1828                          IEEE80211_STYPE_DEAUTH : IEEE80211_STYPE_DISASSOC),
1829                         (char *) &reason, sizeof(reason), hdr->addr2, 0);
1830
1831        if (sta)
1832                atomic_dec(&sta->users);
1833}
1834
1835#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
1836
1837
1838/* Called only as a scheduled task for pending AP frames. */
1839static void pspoll_send_buffered(local_info_t *local, struct sta_info *sta,
1840                                 struct sk_buff *skb)
1841{
1842        struct hostap_skb_tx_data *meta;
1843
1844        if (!(sta->flags & WLAN_STA_PS)) {
1845                /* Station has moved to non-PS mode, so send all buffered
1846                 * frames using normal device queue. */
1847                dev_queue_xmit(skb);
1848                return;
1849        }
1850
1851        /* add a flag for hostap_handle_sta_tx() to know that this skb should
1852         * be passed through even though STA is using PS */
1853        meta = (struct hostap_skb_tx_data *) skb->cb;
1854        meta->flags |= HOSTAP_TX_FLAGS_BUFFERED_FRAME;
1855        if (!skb_queue_empty(&sta->tx_buf)) {
1856                /* indicate to STA that more frames follow */
1857                meta->flags |= HOSTAP_TX_FLAGS_ADD_MOREDATA;
1858        }
1859        dev_queue_xmit(skb);
1860}
1861
1862
1863/* Called only as a scheduled task for pending AP frames. */
1864static void handle_pspoll(local_info_t *local,
1865                          struct ieee80211_hdr *hdr,
1866                          struct hostap_80211_rx_status *rx_stats)
1867{
1868        struct net_device *dev = local->dev;
1869        struct sta_info *sta;
1870        u16 aid;
1871        struct sk_buff *skb;
1872
1873        PDEBUG(DEBUG_PS2, "handle_pspoll: BSSID=%pM, TA=%pM PWRMGT=%d\n",
1874               hdr->addr1, hdr->addr2, !!ieee80211_has_pm(hdr->frame_control));
1875
1876        if (!ether_addr_equal(hdr->addr1, dev->dev_addr)) {
1877                PDEBUG(DEBUG_AP,
1878                       "handle_pspoll - addr1(BSSID)=%pM not own MAC\n",
1879                       hdr->addr1);
1880                return;
1881        }
1882
1883        aid = le16_to_cpu(hdr->duration_id);
1884        if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14))) {
1885                PDEBUG(DEBUG_PS, "   PSPOLL and AID[15:14] not set\n");
1886                return;
1887        }
1888        aid &= ~(BIT(15) | BIT(14));
1889        if (aid == 0 || aid > MAX_AID_TABLE_SIZE) {
1890                PDEBUG(DEBUG_PS, "   invalid aid=%d\n", aid);
1891                return;
1892        }
1893        PDEBUG(DEBUG_PS2, "   aid=%d\n", aid);
1894
1895        spin_lock_bh(&local->ap->sta_table_lock);
1896        sta = ap_get_sta(local->ap, hdr->addr2);
1897        if (sta)
1898                atomic_inc(&sta->users);
1899        spin_unlock_bh(&local->ap->sta_table_lock);
1900
1901        if (sta == NULL) {
1902                PDEBUG(DEBUG_PS, "   STA not found\n");
1903                return;
1904        }
1905        if (sta->aid != aid) {
1906                PDEBUG(DEBUG_PS, "   received aid=%i does not match with "
1907                       "assoc.aid=%d\n", aid, sta->aid);
1908                return;
1909        }
1910
1911        /* FIX: todo:
1912         * - add timeout for buffering (clear aid in TIM vector if buffer timed
1913         *   out (expiry time must be longer than ListenInterval for
1914         *   the corresponding STA; "8802-11: 11.2.1.9 AP aging function"
1915         * - what to do, if buffered, pspolled, and sent frame is not ACKed by
1916         *   sta; store buffer for later use and leave TIM aid bit set? use
1917         *   TX event to check whether frame was ACKed?
1918         */
1919
1920        while ((skb = skb_dequeue(&sta->tx_buf)) != NULL) {
1921                /* send buffered frame .. */
1922                PDEBUG(DEBUG_PS2, "Sending buffered frame to STA after PS POLL"
1923                       " (buffer_count=%d)\n", skb_queue_len(&sta->tx_buf));
1924
1925                pspoll_send_buffered(local, sta, skb);
1926
1927                if (sta->flags & WLAN_STA_PS) {
1928                        /* send only one buffered packet per PS Poll */
1929                        /* FIX: should ignore further PS Polls until the
1930                         * buffered packet that was just sent is acknowledged
1931                         * (Tx or TxExc event) */
1932                        break;
1933                }
1934        }
1935
1936        if (skb_queue_empty(&sta->tx_buf)) {
1937                /* try to clear aid from TIM */
1938                if (!(sta->flags & WLAN_STA_TIM))
1939                        PDEBUG(DEBUG_PS2,  "Re-unsetting TIM for aid %d\n",
1940                               aid);
1941                hostap_set_tim(local, aid, 0);
1942                sta->flags &= ~WLAN_STA_TIM;
1943        }
1944
1945        atomic_dec(&sta->users);
1946}
1947
1948
1949#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
1950
1951static void handle_wds_oper_queue(struct work_struct *work)
1952{
1953        struct ap_data *ap = container_of(work, struct ap_data,
1954                                          wds_oper_queue);
1955        local_info_t *local = ap->local;
1956        struct wds_oper_data *entry, *prev;
1957
1958        spin_lock_bh(&local->lock);
1959        entry = local->ap->wds_oper_entries;
1960        local->ap->wds_oper_entries = NULL;
1961        spin_unlock_bh(&local->lock);
1962
1963        while (entry) {
1964                PDEBUG(DEBUG_AP, "%s: %s automatic WDS connection "
1965                       "to AP %pM\n",
1966                       local->dev->name,
1967                       entry->type == WDS_ADD ? "adding" : "removing",
1968                       entry->addr);
1969                if (entry->type == WDS_ADD)
1970                        prism2_wds_add(local, entry->addr, 0);
1971                else if (entry->type == WDS_DEL)
1972                        prism2_wds_del(local, entry->addr, 0, 1);
1973
1974                prev = entry;
1975                entry = entry->next;
1976                kfree(prev);
1977        }
1978}
1979
1980
1981/* Called only as a scheduled task for pending AP frames. */
1982static void handle_beacon(local_info_t *local, struct sk_buff *skb,
1983                          struct hostap_80211_rx_status *rx_stats)
1984{
1985        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1986        char *body = skb->data + IEEE80211_MGMT_HDR_LEN;
1987        int len, left;
1988        u16 beacon_int, capability;
1989        __le16 *pos;
1990        char *ssid = NULL;
1991        unsigned char *supp_rates = NULL;
1992        int ssid_len = 0, supp_rates_len = 0;
1993        struct sta_info *sta = NULL;
1994        int new_sta = 0, channel = -1;
1995
1996        len = skb->len - IEEE80211_MGMT_HDR_LEN;
1997
1998        if (len < 8 + 2 + 2) {
1999                printk(KERN_DEBUG "handle_beacon - too short payload "
2000                       "(len=%d)\n", len);
2001                return;
2002        }
2003
2004        pos = (__le16 *) body;
2005        left = len;
2006
2007        /* Timestamp (8 octets) */
2008        pos += 4; left -= 8;
2009        /* Beacon interval (2 octets) */
2010        beacon_int = le16_to_cpu(*pos);
2011        pos++; left -= 2;
2012        /* Capability information (2 octets) */
2013        capability = le16_to_cpu(*pos);
2014        pos++; left -= 2;
2015
2016        if (local->ap->ap_policy != AP_OTHER_AP_EVEN_IBSS &&
2017            capability & WLAN_CAPABILITY_IBSS)
2018                return;
2019
2020        if (left >= 2) {
2021                unsigned int ileft;
2022                unsigned char *u = (unsigned char *) pos;
2023
2024                if (*u == WLAN_EID_SSID) {
2025                        u++; left--;
2026                        ileft = *u;
2027                        u++; left--;
2028
2029                        if (ileft > left || ileft > MAX_SSID_LEN) {
2030                                PDEBUG(DEBUG_AP, "SSID: overflow\n");
2031                                return;
2032                        }
2033
2034                        if (local->ap->ap_policy == AP_OTHER_AP_SAME_SSID &&
2035                            (ileft != strlen(local->essid) ||
2036                             memcmp(local->essid, u, ileft) != 0)) {
2037                                /* not our SSID */
2038                                return;
2039                        }
2040
2041                        ssid = u;
2042                        ssid_len = ileft;
2043
2044                        u += ileft;
2045                        left -= ileft;
2046                }
2047
2048                if (*u == WLAN_EID_SUPP_RATES) {
2049                        u++; left--;
2050                        ileft = *u;
2051                        u++; left--;
2052
2053                        if (ileft > left || ileft == 0 || ileft > 8) {
2054                                PDEBUG(DEBUG_AP, " - SUPP_RATES len error\n");
2055                                return;
2056                        }
2057
2058                        supp_rates = u;
2059                        supp_rates_len = ileft;
2060
2061                        u += ileft;
2062                        left -= ileft;
2063                }
2064
2065                if (*u == WLAN_EID_DS_PARAMS) {
2066                        u++; left--;
2067                        ileft = *u;
2068                        u++; left--;
2069
2070                        if (ileft > left || ileft != 1) {
2071                                PDEBUG(DEBUG_AP, " - DS_PARAMS len error\n");
2072                                return;
2073                        }
2074
2075                        channel = *u;
2076
2077                        u += ileft;
2078                        left -= ileft;
2079                }
2080        }
2081
2082        spin_lock_bh(&local->ap->sta_table_lock);
2083        sta = ap_get_sta(local->ap, hdr->addr2);
2084        if (sta != NULL)
2085                atomic_inc(&sta->users);
2086        spin_unlock_bh(&local->ap->sta_table_lock);
2087
2088        if (sta == NULL) {
2089                /* add new AP */
2090                new_sta = 1;
2091                sta = ap_add_sta(local->ap, hdr->addr2);
2092                if (sta == NULL) {
2093                        printk(KERN_INFO "prism2: kmalloc failed for AP "
2094                               "data structure\n");
2095                        return;
2096                }
2097                hostap_event_new_sta(local->dev, sta);
2098
2099                /* mark APs authentication and associated for pseudo ad-hoc
2100                 * style communication */
2101                sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
2102
2103                if (local->ap->autom_ap_wds) {
2104                        hostap_wds_link_oper(local, sta->addr, WDS_ADD);
2105                }
2106        }
2107
2108        sta->ap = 1;
2109        if (ssid) {
2110                sta->u.ap.ssid_len = ssid_len;
2111                memcpy(sta->u.ap.ssid, ssid, ssid_len);
2112                sta->u.ap.ssid[ssid_len] = '\0';
2113        } else {
2114                sta->u.ap.ssid_len = 0;
2115                sta->u.ap.ssid[0] = '\0';
2116        }
2117        sta->u.ap.channel = channel;
2118        sta->rx_packets++;
2119        sta->rx_bytes += len;
2120        sta->u.ap.last_beacon = sta->last_rx = jiffies;
2121        sta->capability = capability;
2122        sta->listen_interval = beacon_int;
2123
2124        atomic_dec(&sta->users);
2125
2126        if (new_sta) {
2127                memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
2128                memcpy(sta->supported_rates, supp_rates, supp_rates_len);
2129                prism2_check_tx_rates(sta);
2130        }
2131}
2132
2133#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2134
2135
2136/* Called only as a tasklet. */
2137static void handle_ap_item(local_info_t *local, struct sk_buff *skb,
2138                           struct hostap_80211_rx_status *rx_stats)
2139{
2140#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2141        struct net_device *dev = local->dev;
2142#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2143        u16 fc, type, stype;
2144        struct ieee80211_hdr *hdr;
2145
2146        /* FIX: should give skb->len to handler functions and check that the
2147         * buffer is long enough */
2148        hdr = (struct ieee80211_hdr *) skb->data;
2149        fc = le16_to_cpu(hdr->frame_control);
2150        type = fc & IEEE80211_FCTL_FTYPE;
2151        stype = fc & IEEE80211_FCTL_STYPE;
2152
2153#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2154        if (!local->hostapd && type == IEEE80211_FTYPE_DATA) {
2155                PDEBUG(DEBUG_AP, "handle_ap_item - data frame\n");
2156
2157                if (!(fc & IEEE80211_FCTL_TODS) ||
2158                    (fc & IEEE80211_FCTL_FROMDS)) {
2159                        if (stype == IEEE80211_STYPE_NULLFUNC) {
2160                                /* no ToDS nullfunc seems to be used to check
2161                                 * AP association; so send reject message to
2162                                 * speed up re-association */
2163                                ap_handle_dropped_data(local, hdr);
2164                                goto done;
2165                        }
2166                        PDEBUG(DEBUG_AP, "   not ToDS frame (fc=0x%04x)\n",
2167                               fc);
2168                        goto done;
2169                }
2170
2171                if (!ether_addr_equal(hdr->addr1, dev->dev_addr)) {
2172                        PDEBUG(DEBUG_AP, "handle_ap_item - addr1(BSSID)=%pM"
2173                               " not own MAC\n", hdr->addr1);
2174                        goto done;
2175                }
2176
2177                if (local->ap->nullfunc_ack &&
2178                    stype == IEEE80211_STYPE_NULLFUNC)
2179                        ap_handle_data_nullfunc(local, hdr);
2180                else
2181                        ap_handle_dropped_data(local, hdr);
2182                goto done;
2183        }
2184
2185        if (type == IEEE80211_FTYPE_MGMT && stype == IEEE80211_STYPE_BEACON) {
2186                handle_beacon(local, skb, rx_stats);
2187                goto done;
2188        }
2189#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2190
2191        if (type == IEEE80211_FTYPE_CTL && stype == IEEE80211_STYPE_PSPOLL) {
2192                handle_pspoll(local, hdr, rx_stats);
2193                goto done;
2194        }
2195
2196        if (local->hostapd) {
2197                PDEBUG(DEBUG_AP, "Unknown frame in AP queue: type=0x%02x "
2198                       "subtype=0x%02x\n", type, stype);
2199                goto done;
2200        }
2201
2202#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2203        if (type != IEEE80211_FTYPE_MGMT) {
2204                PDEBUG(DEBUG_AP, "handle_ap_item - not a management frame?\n");
2205                goto done;
2206        }
2207
2208        if (!ether_addr_equal(hdr->addr1, dev->dev_addr)) {
2209                PDEBUG(DEBUG_AP, "handle_ap_item - addr1(DA)=%pM"
2210                       " not own MAC\n", hdr->addr1);
2211                goto done;
2212        }
2213
2214        if (!ether_addr_equal(hdr->addr3, dev->dev_addr)) {
2215                PDEBUG(DEBUG_AP, "handle_ap_item - addr3(BSSID)=%pM"
2216                       " not own MAC\n", hdr->addr3);
2217                goto done;
2218        }
2219
2220        switch (stype) {
2221        case IEEE80211_STYPE_ASSOC_REQ:
2222                handle_assoc(local, skb, rx_stats, 0);
2223                break;
2224        case IEEE80211_STYPE_ASSOC_RESP:
2225                PDEBUG(DEBUG_AP, "==> ASSOC RESP (ignored)\n");
2226                break;
2227        case IEEE80211_STYPE_REASSOC_REQ:
2228                handle_assoc(local, skb, rx_stats, 1);
2229                break;
2230        case IEEE80211_STYPE_REASSOC_RESP:
2231                PDEBUG(DEBUG_AP, "==> REASSOC RESP (ignored)\n");
2232                break;
2233        case IEEE80211_STYPE_ATIM:
2234                PDEBUG(DEBUG_AP, "==> ATIM (ignored)\n");
2235                break;
2236        case IEEE80211_STYPE_DISASSOC:
2237                handle_disassoc(local, skb, rx_stats);
2238                break;
2239        case IEEE80211_STYPE_AUTH:
2240                handle_authen(local, skb, rx_stats);
2241                break;
2242        case IEEE80211_STYPE_DEAUTH:
2243                handle_deauth(local, skb, rx_stats);
2244                break;
2245        default:
2246                PDEBUG(DEBUG_AP, "Unknown mgmt frame subtype 0x%02x\n",
2247                       stype >> 4);
2248                break;
2249        }
2250#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2251
2252 done:
2253        dev_kfree_skb(skb);
2254}
2255
2256
2257/* Called only as a tasklet (software IRQ) */
2258void hostap_rx(struct net_device *dev, struct sk_buff *skb,
2259               struct hostap_80211_rx_status *rx_stats)
2260{
2261        struct hostap_interface *iface;
2262        local_info_t *local;
2263        struct ieee80211_hdr *hdr;
2264
2265        iface = netdev_priv(dev);
2266        local = iface->local;
2267
2268        if (skb->len < 16)
2269                goto drop;
2270
2271        dev->stats.rx_packets++;
2272
2273        hdr = (struct ieee80211_hdr *) skb->data;
2274
2275        if (local->ap->ap_policy == AP_OTHER_AP_SKIP_ALL &&
2276            ieee80211_is_beacon(hdr->frame_control))
2277                goto drop;
2278
2279        skb->protocol = cpu_to_be16(ETH_P_HOSTAP);
2280        handle_ap_item(local, skb, rx_stats);
2281        return;
2282
2283 drop:
2284        dev_kfree_skb(skb);
2285}
2286
2287
2288/* Called only as a tasklet (software IRQ) */
2289static void schedule_packet_send(local_info_t *local, struct sta_info *sta)
2290{
2291        struct sk_buff *skb;
2292        struct ieee80211_hdr *hdr;
2293        struct hostap_80211_rx_status rx_stats;
2294
2295        if (skb_queue_empty(&sta->tx_buf))
2296                return;
2297
2298        skb = dev_alloc_skb(16);
2299        if (skb == NULL) {
2300                printk(KERN_DEBUG "%s: schedule_packet_send: skb alloc "
2301                       "failed\n", local->dev->name);
2302                return;
2303        }
2304
2305        hdr = skb_put(skb, 16);
2306
2307        /* Generate a fake pspoll frame to start packet delivery */
2308        hdr->frame_control = cpu_to_le16(
2309                IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
2310        memcpy(hdr->addr1, local->dev->dev_addr, ETH_ALEN);
2311        memcpy(hdr->addr2, sta->addr, ETH_ALEN);
2312        hdr->duration_id = cpu_to_le16(sta->aid | BIT(15) | BIT(14));
2313
2314        PDEBUG(DEBUG_PS2,
2315               "%s: Scheduling buffered packet delivery for STA %pM\n",
2316               local->dev->name, sta->addr);
2317
2318        skb->dev = local->dev;
2319
2320        memset(&rx_stats, 0, sizeof(rx_stats));
2321        hostap_rx(local->dev, skb, &rx_stats);
2322}
2323
2324
2325int prism2_ap_get_sta_qual(local_info_t *local, struct sockaddr addr[],
2326                           struct iw_quality qual[], int buf_size,
2327                           int aplist)
2328{
2329        struct ap_data *ap = local->ap;
2330        struct list_head *ptr;
2331        int count = 0;
2332
2333        spin_lock_bh(&ap->sta_table_lock);
2334
2335        for (ptr = ap->sta_list.next; ptr != NULL && ptr != &ap->sta_list;
2336             ptr = ptr->next) {
2337                struct sta_info *sta = (struct sta_info *) ptr;
2338
2339                if (aplist && !sta->ap)
2340                        continue;
2341                addr[count].sa_family = ARPHRD_ETHER;
2342                memcpy(addr[count].sa_data, sta->addr, ETH_ALEN);
2343                if (sta->last_rx_silence == 0)
2344                        qual[count].qual = sta->last_rx_signal < 27 ?
2345                                0 : (sta->last_rx_signal - 27) * 92 / 127;
2346                else
2347                        qual[count].qual = sta->last_rx_signal -
2348                                sta->last_rx_silence - 35;
2349                qual[count].level = HFA384X_LEVEL_TO_dBm(sta->last_rx_signal);
2350                qual[count].noise = HFA384X_LEVEL_TO_dBm(sta->last_rx_silence);
2351                qual[count].updated = sta->last_rx_updated;
2352
2353                sta->last_rx_updated = IW_QUAL_DBM;
2354
2355                count++;
2356                if (count >= buf_size)
2357                        break;
2358        }
2359        spin_unlock_bh(&ap->sta_table_lock);
2360
2361        return count;
2362}
2363
2364
2365/* Translate our list of Access Points & Stations to a card independent
2366 * format that the Wireless Tools will understand - Jean II */
2367int prism2_ap_translate_scan(struct net_device *dev,
2368                             struct iw_request_info *info, char *buffer)
2369{
2370        struct hostap_interface *iface;
2371        local_info_t *local;
2372        struct ap_data *ap;
2373        struct list_head *ptr;
2374        struct iw_event iwe;
2375        char *current_ev = buffer;
2376        char *end_buf = buffer + IW_SCAN_MAX_DATA;
2377#if !defined(PRISM2_NO_KERNEL_IEEE80211_MGMT)
2378        char buf[64];
2379#endif
2380
2381        iface = netdev_priv(dev);
2382        local = iface->local;
2383        ap = local->ap;
2384
2385        spin_lock_bh(&ap->sta_table_lock);
2386
2387        for (ptr = ap->sta_list.next; ptr != NULL && ptr != &ap->sta_list;
2388             ptr = ptr->next) {
2389                struct sta_info *sta = (struct sta_info *) ptr;
2390
2391                /* First entry *MUST* be the AP MAC address */
2392                memset(&iwe, 0, sizeof(iwe));
2393                iwe.cmd = SIOCGIWAP;
2394                iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
2395                memcpy(iwe.u.ap_addr.sa_data, sta->addr, ETH_ALEN);
2396                iwe.len = IW_EV_ADDR_LEN;
2397                current_ev = iwe_stream_add_event(info, current_ev, end_buf,
2398                                                  &iwe, IW_EV_ADDR_LEN);
2399
2400                /* Use the mode to indicate if it's a station or
2401                 * an Access Point */
2402                memset(&iwe, 0, sizeof(iwe));
2403                iwe.cmd = SIOCGIWMODE;
2404                if (sta->ap)
2405                        iwe.u.mode = IW_MODE_MASTER;
2406                else
2407                        iwe.u.mode = IW_MODE_INFRA;
2408                iwe.len = IW_EV_UINT_LEN;
2409                current_ev = iwe_stream_add_event(info, current_ev, end_buf,
2410                                                  &iwe, IW_EV_UINT_LEN);
2411
2412                /* Some quality */
2413                memset(&iwe, 0, sizeof(iwe));
2414                iwe.cmd = IWEVQUAL;
2415                if (sta->last_rx_silence == 0)
2416                        iwe.u.qual.qual = sta->last_rx_signal < 27 ?
2417                                0 : (sta->last_rx_signal - 27) * 92 / 127;
2418                else
2419                        iwe.u.qual.qual = sta->last_rx_signal -
2420                                sta->last_rx_silence - 35;
2421                iwe.u.qual.level = HFA384X_LEVEL_TO_dBm(sta->last_rx_signal);
2422                iwe.u.qual.noise = HFA384X_LEVEL_TO_dBm(sta->last_rx_silence);
2423                iwe.u.qual.updated = sta->last_rx_updated;
2424                iwe.len = IW_EV_QUAL_LEN;
2425                current_ev = iwe_stream_add_event(info, current_ev, end_buf,
2426                                                  &iwe, IW_EV_QUAL_LEN);
2427
2428#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2429                if (sta->ap) {
2430                        memset(&iwe, 0, sizeof(iwe));
2431                        iwe.cmd = SIOCGIWESSID;
2432                        iwe.u.data.length = sta->u.ap.ssid_len;
2433                        iwe.u.data.flags = 1;
2434                        current_ev = iwe_stream_add_point(info, current_ev,
2435                                                          end_buf, &iwe,
2436                                                          sta->u.ap.ssid);
2437
2438                        memset(&iwe, 0, sizeof(iwe));
2439                        iwe.cmd = SIOCGIWENCODE;
2440                        if (sta->capability & WLAN_CAPABILITY_PRIVACY)
2441                                iwe.u.data.flags =
2442                                        IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
2443                        else
2444                                iwe.u.data.flags = IW_ENCODE_DISABLED;
2445                        current_ev = iwe_stream_add_point(info, current_ev,
2446                                                          end_buf, &iwe,
2447                                                          sta->u.ap.ssid);
2448
2449                        if (sta->u.ap.channel > 0 &&
2450                            sta->u.ap.channel <= FREQ_COUNT) {
2451                                memset(&iwe, 0, sizeof(iwe));
2452                                iwe.cmd = SIOCGIWFREQ;
2453                                iwe.u.freq.m = freq_list[sta->u.ap.channel - 1]
2454                                        * 100000;
2455                                iwe.u.freq.e = 1;
2456                                current_ev = iwe_stream_add_event(
2457                                        info, current_ev, end_buf, &iwe,
2458                                        IW_EV_FREQ_LEN);
2459                        }
2460
2461                        memset(&iwe, 0, sizeof(iwe));
2462                        iwe.cmd = IWEVCUSTOM;
2463                        sprintf(buf, "beacon_interval=%d",
2464                                sta->listen_interval);
2465                        iwe.u.data.length = strlen(buf);
2466                        current_ev = iwe_stream_add_point(info, current_ev,
2467                                                          end_buf, &iwe, buf);
2468                }
2469#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2470
2471                sta->last_rx_updated = IW_QUAL_DBM;
2472
2473                /* To be continued, we should make good use of IWEVCUSTOM */
2474        }
2475
2476        spin_unlock_bh(&ap->sta_table_lock);
2477
2478        return current_ev - buffer;
2479}
2480
2481
2482static int prism2_hostapd_add_sta(struct ap_data *ap,
2483                                  struct prism2_hostapd_param *param)
2484{
2485        struct sta_info *sta;
2486
2487        spin_lock_bh(&ap->sta_table_lock);
2488        sta = ap_get_sta(ap, param->sta_addr);
2489        if (sta)
2490                atomic_inc(&sta->users);
2491        spin_unlock_bh(&ap->sta_table_lock);
2492
2493        if (sta == NULL) {
2494                sta = ap_add_sta(ap, param->sta_addr);
2495                if (sta == NULL)
2496                        return -1;
2497        }
2498
2499        if (!(sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
2500                hostap_event_new_sta(sta->local->dev, sta);
2501
2502        sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
2503        sta->last_rx = jiffies;
2504        sta->aid = param->u.add_sta.aid;
2505        sta->capability = param->u.add_sta.capability;
2506        sta->tx_supp_rates = param->u.add_sta.tx_supp_rates;
2507        if (sta->tx_supp_rates & WLAN_RATE_1M)
2508                sta->supported_rates[0] = 2;
2509        if (sta->tx_supp_rates & WLAN_RATE_2M)
2510                sta->supported_rates[1] = 4;
2511        if (sta->tx_supp_rates & WLAN_RATE_5M5)
2512                sta->supported_rates[2] = 11;
2513        if (sta->tx_supp_rates & WLAN_RATE_11M)
2514                sta->supported_rates[3] = 22;
2515        prism2_check_tx_rates(sta);
2516        atomic_dec(&sta->users);
2517        return 0;
2518}
2519
2520
2521static int prism2_hostapd_remove_sta(struct ap_data *ap,
2522                                     struct prism2_hostapd_param *param)
2523{
2524        struct sta_info *sta;
2525
2526        spin_lock_bh(&ap->sta_table_lock);
2527        sta = ap_get_sta(ap, param->sta_addr);
2528        if (sta) {
2529                ap_sta_hash_del(ap, sta);
2530                list_del(&sta->list);
2531        }
2532        spin_unlock_bh(&ap->sta_table_lock);
2533
2534        if (!sta)
2535                return -ENOENT;
2536
2537        if ((sta->flags & WLAN_STA_ASSOC) && !sta->ap && sta->local)
2538                hostap_event_expired_sta(sta->local->dev, sta);
2539        ap_free_sta(ap, sta);
2540
2541        return 0;
2542}
2543
2544
2545static int prism2_hostapd_get_info_sta(struct ap_data *ap,
2546                                       struct prism2_hostapd_param *param)
2547{
2548        struct sta_info *sta;
2549
2550        spin_lock_bh(&ap->sta_table_lock);
2551        sta = ap_get_sta(ap, param->sta_addr);
2552        if (sta)
2553                atomic_inc(&sta->users);
2554        spin_unlock_bh(&ap->sta_table_lock);
2555
2556        if (!sta)
2557                return -ENOENT;
2558
2559        param->u.get_info_sta.inactive_sec = (jiffies - sta->last_rx) / HZ;
2560
2561        atomic_dec(&sta->users);
2562
2563        return 1;
2564}
2565
2566
2567static int prism2_hostapd_set_flags_sta(struct ap_data *ap,
2568                                        struct prism2_hostapd_param *param)
2569{
2570        struct sta_info *sta;
2571
2572        spin_lock_bh(&ap->sta_table_lock);
2573        sta = ap_get_sta(ap, param->sta_addr);
2574        if (sta) {
2575                sta->flags |= param->u.set_flags_sta.flags_or;
2576                sta->flags &= param->u.set_flags_sta.flags_and;
2577        }
2578        spin_unlock_bh(&ap->sta_table_lock);
2579
2580        if (!sta)
2581                return -ENOENT;
2582
2583        return 0;
2584}
2585
2586
2587static int prism2_hostapd_sta_clear_stats(struct ap_data *ap,
2588                                          struct prism2_hostapd_param *param)
2589{
2590        struct sta_info *sta;
2591        int rate;
2592
2593        spin_lock_bh(&ap->sta_table_lock);
2594        sta = ap_get_sta(ap, param->sta_addr);
2595        if (sta) {
2596                sta->rx_packets = sta->tx_packets = 0;
2597                sta->rx_bytes = sta->tx_bytes = 0;
2598                for (rate = 0; rate < WLAN_RATE_COUNT; rate++) {
2599                        sta->tx_count[rate] = 0;
2600                        sta->rx_count[rate] = 0;
2601                }
2602        }
2603        spin_unlock_bh(&ap->sta_table_lock);
2604
2605        if (!sta)
2606                return -ENOENT;
2607
2608        return 0;
2609}
2610
2611
2612int prism2_hostapd(struct ap_data *ap, struct prism2_hostapd_param *param)
2613{
2614        switch (param->cmd) {
2615        case PRISM2_HOSTAPD_FLUSH:
2616                ap_control_kickall(ap);
2617                return 0;
2618        case PRISM2_HOSTAPD_ADD_STA:
2619                return prism2_hostapd_add_sta(ap, param);
2620        case PRISM2_HOSTAPD_REMOVE_STA:
2621                return prism2_hostapd_remove_sta(ap, param);
2622        case PRISM2_HOSTAPD_GET_INFO_STA:
2623                return prism2_hostapd_get_info_sta(ap, param);
2624        case PRISM2_HOSTAPD_SET_FLAGS_STA:
2625                return prism2_hostapd_set_flags_sta(ap, param);
2626        case PRISM2_HOSTAPD_STA_CLEAR_STATS:
2627                return prism2_hostapd_sta_clear_stats(ap, param);
2628        default:
2629                printk(KERN_WARNING "prism2_hostapd: unknown cmd=%d\n",
2630                       param->cmd);
2631                return -EOPNOTSUPP;
2632        }
2633}
2634
2635
2636/* Update station info for host-based TX rate control and return current
2637 * TX rate */
2638static int ap_update_sta_tx_rate(struct sta_info *sta, struct net_device *dev)
2639{
2640        int ret = sta->tx_rate;
2641        struct hostap_interface *iface;
2642        local_info_t *local;
2643
2644        iface = netdev_priv(dev);
2645        local = iface->local;
2646
2647        sta->tx_count[sta->tx_rate_idx]++;
2648        sta->tx_since_last_failure++;
2649        sta->tx_consecutive_exc = 0;
2650        if (sta->tx_since_last_failure >= WLAN_RATE_UPDATE_COUNT &&
2651            sta->tx_rate_idx < sta->tx_max_rate) {
2652                /* use next higher rate */
2653                int old_rate, new_rate;
2654                old_rate = new_rate = sta->tx_rate_idx;
2655                while (new_rate < sta->tx_max_rate) {
2656                        new_rate++;
2657                        if (ap_tx_rate_ok(new_rate, sta, local)) {
2658                                sta->tx_rate_idx = new_rate;
2659                                break;
2660                        }
2661                }
2662                if (old_rate != sta->tx_rate_idx) {
2663                        switch (sta->tx_rate_idx) {
2664                        case 0: sta->tx_rate = 10; break;
2665                        case 1: sta->tx_rate = 20; break;
2666                        case 2: sta->tx_rate = 55; break;
2667                        case 3: sta->tx_rate = 110; break;
2668                        default: sta->tx_rate = 0; break;
2669                        }
2670                        PDEBUG(DEBUG_AP, "%s: STA %pM TX rate raised to %d\n",
2671                               dev->name, sta->addr, sta->tx_rate);
2672                }
2673                sta->tx_since_last_failure = 0;
2674        }
2675
2676        return ret;
2677}
2678
2679
2680/* Called only from software IRQ. Called for each TX frame prior possible
2681 * encryption and transmit. */
2682ap_tx_ret hostap_handle_sta_tx(local_info_t *local, struct hostap_tx_data *tx)
2683{
2684        struct sta_info *sta = NULL;
2685        struct sk_buff *skb = tx->skb;
2686        int set_tim, ret;
2687        struct ieee80211_hdr *hdr;
2688        struct hostap_skb_tx_data *meta;
2689
2690        meta = (struct hostap_skb_tx_data *) skb->cb;
2691        ret = AP_TX_CONTINUE;
2692        if (local->ap == NULL || skb->len < 10 ||
2693            meta->iface->type == HOSTAP_INTERFACE_STA)
2694                goto out;
2695
2696        hdr = (struct ieee80211_hdr *) skb->data;
2697
2698        if (hdr->addr1[0] & 0x01) {
2699                /* broadcast/multicast frame - no AP related processing */
2700                if (local->ap->num_sta <= 0)
2701                        ret = AP_TX_DROP;
2702                goto out;
2703        }
2704
2705        /* unicast packet - check whether destination STA is associated */
2706        spin_lock(&local->ap->sta_table_lock);
2707        sta = ap_get_sta(local->ap, hdr->addr1);
2708        if (sta)
2709                atomic_inc(&sta->users);
2710        spin_unlock(&local->ap->sta_table_lock);
2711
2712        if (local->iw_mode == IW_MODE_MASTER && sta == NULL &&
2713            !(meta->flags & HOSTAP_TX_FLAGS_WDS) &&
2714            meta->iface->type != HOSTAP_INTERFACE_MASTER &&
2715            meta->iface->type != HOSTAP_INTERFACE_AP) {
2716#if 0
2717                /* This can happen, e.g., when wlan0 is added to a bridge and
2718                 * bridging code does not know which port is the correct target
2719                 * for a unicast frame. In this case, the packet is send to all
2720                 * ports of the bridge. Since this is a valid scenario, do not
2721                 * print out any errors here. */
2722                if (net_ratelimit()) {
2723                        printk(KERN_DEBUG "AP: drop packet to non-associated "
2724                               "STA %pM\n", hdr->addr1);
2725                }
2726#endif
2727                local->ap->tx_drop_nonassoc++;
2728                ret = AP_TX_DROP;
2729                goto out;
2730        }
2731
2732        if (sta == NULL)
2733                goto out;
2734
2735        if (!(sta->flags & WLAN_STA_AUTHORIZED))
2736                ret = AP_TX_CONTINUE_NOT_AUTHORIZED;
2737
2738        /* Set tx_rate if using host-based TX rate control */
2739        if (!local->fw_tx_rate_control)
2740                local->ap->last_tx_rate = meta->rate =
2741                        ap_update_sta_tx_rate(sta, local->dev);
2742
2743        if (local->iw_mode != IW_MODE_MASTER)
2744                goto out;
2745
2746        if (!(sta->flags & WLAN_STA_PS))
2747                goto out;
2748
2749        if (meta->flags & HOSTAP_TX_FLAGS_ADD_MOREDATA) {
2750                /* indicate to STA that more frames follow */
2751                hdr->frame_control |=
2752                        cpu_to_le16(IEEE80211_FCTL_MOREDATA);
2753        }
2754
2755        if (meta->flags & HOSTAP_TX_FLAGS_BUFFERED_FRAME) {
2756                /* packet was already buffered and now send due to
2757                 * PS poll, so do not rebuffer it */
2758                goto out;
2759        }
2760
2761        if (skb_queue_len(&sta->tx_buf) >= STA_MAX_TX_BUFFER) {
2762                PDEBUG(DEBUG_PS, "%s: No more space in STA (%pM)'s"
2763                       "PS mode buffer\n",
2764                       local->dev->name, sta->addr);
2765                /* Make sure that TIM is set for the station (it might not be
2766                 * after AP wlan hw reset). */
2767                /* FIX: should fix hw reset to restore bits based on STA
2768                 * buffer state.. */
2769                hostap_set_tim(local, sta->aid, 1);
2770                sta->flags |= WLAN_STA_TIM;
2771                ret = AP_TX_DROP;
2772                goto out;
2773        }
2774
2775        /* STA in PS mode, buffer frame for later delivery */
2776        set_tim = skb_queue_empty(&sta->tx_buf);
2777        skb_queue_tail(&sta->tx_buf, skb);
2778        /* FIX: could save RX time to skb and expire buffered frames after
2779         * some time if STA does not poll for them */
2780
2781        if (set_tim) {
2782                if (sta->flags & WLAN_STA_TIM)
2783                        PDEBUG(DEBUG_PS2, "Re-setting TIM for aid %d\n",
2784                               sta->aid);
2785                hostap_set_tim(local, sta->aid, 1);
2786                sta->flags |= WLAN_STA_TIM;
2787        }
2788
2789        ret = AP_TX_BUFFERED;
2790
2791 out:
2792        if (sta != NULL) {
2793                if (ret == AP_TX_CONTINUE ||
2794                    ret == AP_TX_CONTINUE_NOT_AUTHORIZED) {
2795                        sta->tx_packets++;
2796                        sta->tx_bytes += skb->len;
2797                        sta->last_tx = jiffies;
2798                }
2799
2800                if ((ret == AP_TX_CONTINUE ||
2801                     ret == AP_TX_CONTINUE_NOT_AUTHORIZED) &&
2802                    sta->crypt && tx->host_encrypt) {
2803                        tx->crypt = sta->crypt;
2804                        tx->sta_ptr = sta; /* hostap_handle_sta_release() will
2805                                            * be called to release sta info
2806                                            * later */
2807                } else
2808                        atomic_dec(&sta->users);
2809        }
2810
2811        return ret;
2812}
2813
2814
2815void hostap_handle_sta_release(void *ptr)
2816{
2817        struct sta_info *sta = ptr;
2818        atomic_dec(&sta->users);
2819}
2820
2821
2822/* Called only as a tasklet (software IRQ) */
2823void hostap_handle_sta_tx_exc(local_info_t *local, struct sk_buff *skb)
2824{
2825        struct sta_info *sta;
2826        struct ieee80211_hdr *hdr;
2827        struct hostap_skb_tx_data *meta;
2828
2829        hdr = (struct ieee80211_hdr *) skb->data;
2830        meta = (struct hostap_skb_tx_data *) skb->cb;
2831
2832        spin_lock(&local->ap->sta_table_lock);
2833        sta = ap_get_sta(local->ap, hdr->addr1);
2834        if (!sta) {
2835                spin_unlock(&local->ap->sta_table_lock);
2836                PDEBUG(DEBUG_AP, "%s: Could not find STA %pM"
2837                       " for this TX error (@%lu)\n",
2838                       local->dev->name, hdr->addr1, jiffies);
2839                return;
2840        }
2841
2842        sta->tx_since_last_failure = 0;
2843        sta->tx_consecutive_exc++;
2844
2845        if (sta->tx_consecutive_exc >= WLAN_RATE_DECREASE_THRESHOLD &&
2846            sta->tx_rate_idx > 0 && meta->rate <= sta->tx_rate) {
2847                /* use next lower rate */
2848                int old, rate;
2849                old = rate = sta->tx_rate_idx;
2850                while (rate > 0) {
2851                        rate--;
2852                        if (ap_tx_rate_ok(rate, sta, local)) {
2853                                sta->tx_rate_idx = rate;
2854                                break;
2855                        }
2856                }
2857                if (old != sta->tx_rate_idx) {
2858                        switch (sta->tx_rate_idx) {
2859                        case 0: sta->tx_rate = 10; break;
2860                        case 1: sta->tx_rate = 20; break;
2861                        case 2: sta->tx_rate = 55; break;
2862                        case 3: sta->tx_rate = 110; break;
2863                        default: sta->tx_rate = 0; break;
2864                        }
2865                        PDEBUG(DEBUG_AP,
2866                               "%s: STA %pM TX rate lowered to %d\n",
2867                               local->dev->name, sta->addr, sta->tx_rate);
2868                }
2869                sta->tx_consecutive_exc = 0;
2870        }
2871        spin_unlock(&local->ap->sta_table_lock);
2872}
2873
2874
2875static void hostap_update_sta_ps2(local_info_t *local, struct sta_info *sta,
2876                                  int pwrmgt, int type, int stype)
2877{
2878        if (pwrmgt && !(sta->flags & WLAN_STA_PS)) {
2879                sta->flags |= WLAN_STA_PS;
2880                PDEBUG(DEBUG_PS2, "STA %pM changed to use PS "
2881                       "mode (type=0x%02X, stype=0x%02X)\n",
2882                       sta->addr, type >> 2, stype >> 4);
2883        } else if (!pwrmgt && (sta->flags & WLAN_STA_PS)) {
2884                sta->flags &= ~WLAN_STA_PS;
2885                PDEBUG(DEBUG_PS2, "STA %pM changed to not use "
2886                       "PS mode (type=0x%02X, stype=0x%02X)\n",
2887                       sta->addr, type >> 2, stype >> 4);
2888                if (type != IEEE80211_FTYPE_CTL ||
2889                    stype != IEEE80211_STYPE_PSPOLL)
2890                        schedule_packet_send(local, sta);
2891        }
2892}
2893
2894
2895/* Called only as a tasklet (software IRQ). Called for each RX frame to update
2896 * STA power saving state. pwrmgt is a flag from 802.11 frame_control field. */
2897int hostap_update_sta_ps(local_info_t *local, struct ieee80211_hdr *hdr)
2898{
2899        struct sta_info *sta;
2900        u16 fc;
2901
2902        spin_lock(&local->ap->sta_table_lock);
2903        sta = ap_get_sta(local->ap, hdr->addr2);
2904        if (sta)
2905                atomic_inc(&sta->users);
2906        spin_unlock(&local->ap->sta_table_lock);
2907
2908        if (!sta)
2909                return -1;
2910
2911        fc = le16_to_cpu(hdr->frame_control);
2912        hostap_update_sta_ps2(local, sta, fc & IEEE80211_FCTL_PM,
2913                              fc & IEEE80211_FCTL_FTYPE,
2914                              fc & IEEE80211_FCTL_STYPE);
2915
2916        atomic_dec(&sta->users);
2917        return 0;
2918}
2919
2920
2921/* Called only as a tasklet (software IRQ). Called for each RX frame after
2922 * getting RX header and payload from hardware. */
2923ap_rx_ret hostap_handle_sta_rx(local_info_t *local, struct net_device *dev,
2924                               struct sk_buff *skb,
2925                               struct hostap_80211_rx_status *rx_stats,
2926                               int wds)
2927{
2928        int ret;
2929        struct sta_info *sta;
2930        u16 fc, type, stype;
2931        struct ieee80211_hdr *hdr;
2932
2933        if (local->ap == NULL)
2934                return AP_RX_CONTINUE;
2935
2936        hdr = (struct ieee80211_hdr *) skb->data;
2937
2938        fc = le16_to_cpu(hdr->frame_control);
2939        type = fc & IEEE80211_FCTL_FTYPE;
2940        stype = fc & IEEE80211_FCTL_STYPE;
2941
2942        spin_lock(&local->ap->sta_table_lock);
2943        sta = ap_get_sta(local->ap, hdr->addr2);
2944        if (sta)
2945                atomic_inc(&sta->users);
2946        spin_unlock(&local->ap->sta_table_lock);
2947
2948        if (sta && !(sta->flags & WLAN_STA_AUTHORIZED))
2949                ret = AP_RX_CONTINUE_NOT_AUTHORIZED;
2950        else
2951                ret = AP_RX_CONTINUE;
2952
2953
2954        if (fc & IEEE80211_FCTL_TODS) {
2955                if (!wds && (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
2956                        if (local->hostapd) {
2957                                prism2_rx_80211(local->apdev, skb, rx_stats,
2958                                                PRISM2_RX_NON_ASSOC);
2959#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2960                        } else {
2961                                printk(KERN_DEBUG "%s: dropped received packet"
2962                                       " from non-associated STA %pM"
2963                                       " (type=0x%02x, subtype=0x%02x)\n",
2964                                       dev->name, hdr->addr2,
2965                                       type >> 2, stype >> 4);
2966                                hostap_rx(dev, skb, rx_stats);
2967#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
2968                        }
2969                        ret = AP_RX_EXIT;
2970                        goto out;
2971                }
2972        } else if (fc & IEEE80211_FCTL_FROMDS) {
2973                if (!wds) {
2974                        /* FromDS frame - not for us; probably
2975                         * broadcast/multicast in another BSS - drop */
2976                        if (ether_addr_equal(hdr->addr1, dev->dev_addr)) {
2977                                printk(KERN_DEBUG "Odd.. FromDS packet "
2978                                       "received with own BSSID\n");
2979                                hostap_dump_rx_80211(dev->name, skb, rx_stats);
2980                        }
2981                        ret = AP_RX_DROP;
2982                        goto out;
2983                }
2984        } else if (stype == IEEE80211_STYPE_NULLFUNC && sta == NULL &&
2985                   ether_addr_equal(hdr->addr1, dev->dev_addr)) {
2986
2987                if (local->hostapd) {
2988                        prism2_rx_80211(local->apdev, skb, rx_stats,
2989                                        PRISM2_RX_NON_ASSOC);
2990#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
2991                } else {
2992                        /* At least Lucent f/w seems to send data::nullfunc
2993                         * frames with no ToDS flag when the current AP returns
2994                         * after being unavailable for some time. Speed up
2995                         * re-association by informing the station about it not
2996                         * being associated. */
2997                        printk(KERN_DEBUG "%s: rejected received nullfunc frame"
2998                               " without ToDS from not associated STA %pM\n",
2999                               dev->name, hdr->addr2);
3000                        hostap_rx(dev, skb, rx_stats);
3001#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
3002                }
3003                ret = AP_RX_EXIT;
3004                goto out;
3005        } else if (stype == IEEE80211_STYPE_NULLFUNC) {
3006                /* At least Lucent cards seem to send periodic nullfunc
3007                 * frames with ToDS. Let these through to update SQ
3008                 * stats and PS state. Nullfunc frames do not contain
3009                 * any data and they will be dropped below. */
3010        } else {
3011                /* If BSSID (Addr3) is foreign, this frame is a normal
3012                 * broadcast frame from an IBSS network. Drop it silently.
3013                 * If BSSID is own, report the dropping of this frame. */
3014                if (ether_addr_equal(hdr->addr3, dev->dev_addr)) {
3015                        printk(KERN_DEBUG "%s: dropped received packet from %pM"
3016                               " with no ToDS flag "
3017                               "(type=0x%02x, subtype=0x%02x)\n", dev->name,
3018                               hdr->addr2, type >> 2, stype >> 4);
3019                        hostap_dump_rx_80211(dev->name, skb, rx_stats);
3020                }
3021                ret = AP_RX_DROP;
3022                goto out;
3023        }
3024
3025        if (sta) {
3026                hostap_update_sta_ps2(local, sta, fc & IEEE80211_FCTL_PM,
3027                                      type, stype);
3028
3029                sta->rx_packets++;
3030                sta->rx_bytes += skb->len;
3031                sta->last_rx = jiffies;
3032        }
3033
3034        if (local->ap->nullfunc_ack && stype == IEEE80211_STYPE_NULLFUNC &&
3035            fc & IEEE80211_FCTL_TODS) {
3036                if (local->hostapd) {
3037                        prism2_rx_80211(local->apdev, skb, rx_stats,
3038                                        PRISM2_RX_NULLFUNC_ACK);
3039#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
3040                } else {
3041                        /* some STA f/w's seem to require control::ACK frame
3042                         * for data::nullfunc, but Prism2 f/w 0.8.0 (at least
3043                         * from Compaq) does not send this.. Try to generate
3044                         * ACK for these frames from the host driver to make
3045                         * power saving work with, e.g., Lucent WaveLAN f/w */
3046                        hostap_rx(dev, skb, rx_stats);
3047#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
3048                }
3049                ret = AP_RX_EXIT;
3050                goto out;
3051        }
3052
3053 out:
3054        if (sta)
3055                atomic_dec(&sta->users);
3056
3057        return ret;
3058}
3059
3060
3061/* Called only as a tasklet (software IRQ) */
3062int hostap_handle_sta_crypto(local_info_t *local,
3063                             struct ieee80211_hdr *hdr,
3064                             struct lib80211_crypt_data **crypt,
3065                             void **sta_ptr)
3066{
3067        struct sta_info *sta;
3068
3069        spin_lock(&local->ap->sta_table_lock);
3070        sta = ap_get_sta(local->ap, hdr->addr2);
3071        if (sta)
3072                atomic_inc(&sta->users);
3073        spin_unlock(&local->ap->sta_table_lock);
3074
3075        if (!sta)
3076                return -1;
3077
3078        if (sta->crypt) {
3079                *crypt = sta->crypt;
3080                *sta_ptr = sta;
3081                /* hostap_handle_sta_release() will be called to release STA
3082                 * info */
3083        } else
3084                atomic_dec(&sta->users);
3085
3086        return 0;
3087}
3088
3089
3090/* Called only as a tasklet (software IRQ) */
3091int hostap_is_sta_assoc(struct ap_data *ap, u8 *sta_addr)
3092{
3093        struct sta_info *sta;
3094        int ret = 0;
3095
3096        spin_lock(&ap->sta_table_lock);
3097        sta = ap_get_sta(ap, sta_addr);
3098        if (sta != NULL && (sta->flags & WLAN_STA_ASSOC) && !sta->ap)
3099                ret = 1;
3100        spin_unlock(&ap->sta_table_lock);
3101
3102        return ret;
3103}
3104
3105
3106/* Called only as a tasklet (software IRQ) */
3107int hostap_is_sta_authorized(struct ap_data *ap, u8 *sta_addr)
3108{
3109        struct sta_info *sta;
3110        int ret = 0;
3111
3112        spin_lock(&ap->sta_table_lock);
3113        sta = ap_get_sta(ap, sta_addr);
3114        if (sta != NULL && (sta->flags & WLAN_STA_ASSOC) && !sta->ap &&
3115            ((sta->flags & WLAN_STA_AUTHORIZED) ||
3116             ap->local->ieee_802_1x == 0))
3117                ret = 1;
3118        spin_unlock(&ap->sta_table_lock);
3119
3120        return ret;
3121}
3122
3123
3124/* Called only as a tasklet (software IRQ) */
3125int hostap_add_sta(struct ap_data *ap, u8 *sta_addr)
3126{
3127        struct sta_info *sta;
3128        int ret = 1;
3129
3130        if (!ap)
3131                return -1;
3132
3133        spin_lock(&ap->sta_table_lock);
3134        sta = ap_get_sta(ap, sta_addr);
3135        if (sta)
3136                ret = 0;
3137        spin_unlock(&ap->sta_table_lock);
3138
3139        if (ret == 1) {
3140                sta = ap_add_sta(ap, sta_addr);
3141                if (!sta)
3142                        return -1;
3143                sta->flags = WLAN_STA_AUTH | WLAN_STA_ASSOC;
3144                sta->ap = 1;
3145                memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
3146                /* No way of knowing which rates are supported since we did not
3147                 * get supported rates element from beacon/assoc req. Assume
3148                 * that remote end supports all 802.11b rates. */
3149                sta->supported_rates[0] = 0x82;
3150                sta->supported_rates[1] = 0x84;
3151                sta->supported_rates[2] = 0x0b;
3152                sta->supported_rates[3] = 0x16;
3153                sta->tx_supp_rates = WLAN_RATE_1M | WLAN_RATE_2M |
3154                        WLAN_RATE_5M5 | WLAN_RATE_11M;
3155                sta->tx_rate = 110;
3156                sta->tx_max_rate = sta->tx_rate_idx = 3;
3157        }
3158
3159        return ret;
3160}
3161
3162
3163/* Called only as a tasklet (software IRQ) */
3164int hostap_update_rx_stats(struct ap_data *ap,
3165                           struct ieee80211_hdr *hdr,
3166                           struct hostap_80211_rx_status *rx_stats)
3167{
3168        struct sta_info *sta;
3169
3170        if (!ap)
3171                return -1;
3172
3173        spin_lock(&ap->sta_table_lock);
3174        sta = ap_get_sta(ap, hdr->addr2);
3175        if (sta) {
3176                sta->last_rx_silence = rx_stats->noise;
3177                sta->last_rx_signal = rx_stats->signal;
3178                sta->last_rx_rate = rx_stats->rate;
3179                sta->last_rx_updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
3180                if (rx_stats->rate == 10)
3181                        sta->rx_count[0]++;
3182                else if (rx_stats->rate == 20)
3183                        sta->rx_count[1]++;
3184                else if (rx_stats->rate == 55)
3185                        sta->rx_count[2]++;
3186                else if (rx_stats->rate == 110)
3187                        sta->rx_count[3]++;
3188        }
3189        spin_unlock(&ap->sta_table_lock);
3190
3191        return sta ? 0 : -1;
3192}
3193
3194
3195void hostap_update_rates(local_info_t *local)
3196{
3197        struct sta_info *sta;
3198        struct ap_data *ap = local->ap;
3199
3200        if (!ap)
3201                return;
3202
3203        spin_lock_bh(&ap->sta_table_lock);
3204        list_for_each_entry(sta, &ap->sta_list, list) {
3205                prism2_check_tx_rates(sta);
3206        }
3207        spin_unlock_bh(&ap->sta_table_lock);
3208}
3209
3210
3211void * ap_crypt_get_ptrs(struct ap_data *ap, u8 *addr, int permanent,
3212                         struct lib80211_crypt_data ***crypt)
3213{
3214        struct sta_info *sta;
3215
3216        spin_lock_bh(&ap->sta_table_lock);
3217        sta = ap_get_sta(ap, addr);
3218        if (sta)
3219                atomic_inc(&sta->users);
3220        spin_unlock_bh(&ap->sta_table_lock);
3221
3222        if (!sta && permanent)
3223                sta = ap_add_sta(ap, addr);
3224
3225        if (!sta)
3226                return NULL;
3227
3228        if (permanent)
3229                sta->flags |= WLAN_STA_PERM;
3230
3231        *crypt = &sta->crypt;
3232
3233        return sta;
3234}
3235
3236
3237void hostap_add_wds_links(local_info_t *local)
3238{
3239        struct ap_data *ap = local->ap;
3240        struct sta_info *sta;
3241
3242        spin_lock_bh(&ap->sta_table_lock);
3243        list_for_each_entry(sta, &ap->sta_list, list) {
3244                if (sta->ap)
3245                        hostap_wds_link_oper(local, sta->addr, WDS_ADD);
3246        }
3247        spin_unlock_bh(&ap->sta_table_lock);
3248
3249        schedule_work(&local->ap->wds_oper_queue);
3250}
3251
3252
3253void hostap_wds_link_oper(local_info_t *local, u8 *addr, wds_oper_type type)
3254{
3255        struct wds_oper_data *entry;
3256
3257        entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
3258        if (!entry)
3259                return;
3260        memcpy(entry->addr, addr, ETH_ALEN);
3261        entry->type = type;
3262        spin_lock_bh(&local->lock);
3263        entry->next = local->ap->wds_oper_entries;
3264        local->ap->wds_oper_entries = entry;
3265        spin_unlock_bh(&local->lock);
3266
3267        schedule_work(&local->ap->wds_oper_queue);
3268}
3269
3270
3271EXPORT_SYMBOL(hostap_init_data);
3272EXPORT_SYMBOL(hostap_init_ap_proc);
3273EXPORT_SYMBOL(hostap_free_data);
3274EXPORT_SYMBOL(hostap_check_sta_fw_version);
3275EXPORT_SYMBOL(hostap_handle_sta_tx_exc);
3276#ifndef PRISM2_NO_KERNEL_IEEE80211_MGMT
3277#endif /* PRISM2_NO_KERNEL_IEEE80211_MGMT */
3278