linux/drivers/staging/rtl8192e/rtllib_softmac.c
<<
>>
Prefs
   1/* IEEE 802.11 SoftMAC layer
   2 * Copyright (c) 2005 Andrea Merello <andrea.merello@gmail.com>
   3 *
   4 * Mostly extracted from the rtl8180-sa2400 driver for the
   5 * in-kernel generic ieee802.11 stack.
   6 *
   7 * Few lines might be stolen from other part of the rtllib
   8 * stack. Copyright who own it's copyright
   9 *
  10 * WPA code stolen from the ipw2200 driver.
  11 * Copyright who own it's copyright.
  12 *
  13 * released under the GPL
  14 */
  15
  16
  17#include "rtllib.h"
  18
  19#include <linux/random.h>
  20#include <linux/delay.h>
  21#include <linux/uaccess.h>
  22#include <linux/etherdevice.h>
  23#include <linux/ieee80211.h>
  24#include "dot11d.h"
  25
  26static void rtllib_sta_wakeup(struct rtllib_device *ieee, short nl);
  27
  28
  29static short rtllib_is_54g(struct rtllib_network *net)
  30{
  31        return (net->rates_ex_len > 0) || (net->rates_len > 4);
  32}
  33
  34/* returns the total length needed for placing the RATE MFIE
  35 * tag and the EXTENDED RATE MFIE tag if needed.
  36 * It encludes two bytes per tag for the tag itself and its len
  37 */
  38static unsigned int rtllib_MFIE_rate_len(struct rtllib_device *ieee)
  39{
  40        unsigned int rate_len = 0;
  41
  42        if (ieee->modulation & RTLLIB_CCK_MODULATION)
  43                rate_len = RTLLIB_CCK_RATE_LEN + 2;
  44
  45        if (ieee->modulation & RTLLIB_OFDM_MODULATION)
  46
  47                rate_len += RTLLIB_OFDM_RATE_LEN + 2;
  48
  49        return rate_len;
  50}
  51
  52/* place the MFIE rate, tag to the memory (double) pointed.
  53 * Then it updates the pointer so that
  54 * it points after the new MFIE tag added.
  55 */
  56static void rtllib_MFIE_Brate(struct rtllib_device *ieee, u8 **tag_p)
  57{
  58        u8 *tag = *tag_p;
  59
  60        if (ieee->modulation & RTLLIB_CCK_MODULATION) {
  61                *tag++ = MFIE_TYPE_RATES;
  62                *tag++ = 4;
  63                *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_1MB;
  64                *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_2MB;
  65                *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_5MB;
  66                *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_11MB;
  67        }
  68
  69        /* We may add an option for custom rates that specific HW
  70         * might support
  71         */
  72        *tag_p = tag;
  73}
  74
  75static void rtllib_MFIE_Grate(struct rtllib_device *ieee, u8 **tag_p)
  76{
  77        u8 *tag = *tag_p;
  78
  79        if (ieee->modulation & RTLLIB_OFDM_MODULATION) {
  80                *tag++ = MFIE_TYPE_RATES_EX;
  81                *tag++ = 8;
  82                *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_6MB;
  83                *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_9MB;
  84                *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_12MB;
  85                *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_18MB;
  86                *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_24MB;
  87                *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_36MB;
  88                *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_48MB;
  89                *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_54MB;
  90        }
  91        /* We may add an option for custom rates that specific HW might
  92         * support
  93         */
  94        *tag_p = tag;
  95}
  96
  97static void rtllib_WMM_Info(struct rtllib_device *ieee, u8 **tag_p)
  98{
  99        u8 *tag = *tag_p;
 100
 101        *tag++ = MFIE_TYPE_GENERIC;
 102        *tag++ = 7;
 103        *tag++ = 0x00;
 104        *tag++ = 0x50;
 105        *tag++ = 0xf2;
 106        *tag++ = 0x02;
 107        *tag++ = 0x00;
 108        *tag++ = 0x01;
 109        *tag++ = MAX_SP_Len;
 110        *tag_p = tag;
 111}
 112
 113static void rtllib_TURBO_Info(struct rtllib_device *ieee, u8 **tag_p)
 114{
 115        u8 *tag = *tag_p;
 116
 117        *tag++ = MFIE_TYPE_GENERIC;
 118        *tag++ = 7;
 119        *tag++ = 0x00;
 120        *tag++ = 0xe0;
 121        *tag++ = 0x4c;
 122        *tag++ = 0x01;
 123        *tag++ = 0x02;
 124        *tag++ = 0x11;
 125        *tag++ = 0x00;
 126
 127        *tag_p = tag;
 128        netdev_alert(ieee->dev, "This is enable turbo mode IE process\n");
 129}
 130
 131static void enqueue_mgmt(struct rtllib_device *ieee, struct sk_buff *skb)
 132{
 133        int nh;
 134
 135        nh = (ieee->mgmt_queue_head + 1) % MGMT_QUEUE_NUM;
 136
 137/* if the queue is full but we have newer frames then
 138 * just overwrites the oldest.
 139 *
 140 * if (nh == ieee->mgmt_queue_tail)
 141 *              return -1;
 142 */
 143        ieee->mgmt_queue_head = nh;
 144        ieee->mgmt_queue_ring[nh] = skb;
 145
 146}
 147
 148static void init_mgmt_queue(struct rtllib_device *ieee)
 149{
 150        ieee->mgmt_queue_tail = ieee->mgmt_queue_head = 0;
 151}
 152
 153
 154u8
 155MgntQuery_TxRateExcludeCCKRates(struct rtllib_device *ieee)
 156{
 157        u16     i;
 158        u8      QueryRate = 0;
 159        u8      BasicRate;
 160
 161
 162        for (i = 0; i < ieee->current_network.rates_len; i++) {
 163                BasicRate = ieee->current_network.rates[i]&0x7F;
 164                if (!rtllib_is_cck_rate(BasicRate)) {
 165                        if (QueryRate == 0) {
 166                                QueryRate = BasicRate;
 167                        } else {
 168                                if (BasicRate < QueryRate)
 169                                        QueryRate = BasicRate;
 170                        }
 171                }
 172        }
 173
 174        if (QueryRate == 0) {
 175                QueryRate = 12;
 176                netdev_info(ieee->dev, "No BasicRate found!!\n");
 177        }
 178        return QueryRate;
 179}
 180
 181static u8 MgntQuery_MgntFrameTxRate(struct rtllib_device *ieee)
 182{
 183        struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
 184        u8 rate;
 185
 186        if (pHTInfo->IOTAction & HT_IOT_ACT_MGNT_USE_CCK_6M)
 187                rate = 0x0c;
 188        else
 189                rate = ieee->basic_rate & 0x7f;
 190
 191        if (rate == 0) {
 192                if (ieee->mode == IEEE_A ||
 193                   ieee->mode == IEEE_N_5G ||
 194                   (ieee->mode == IEEE_N_24G && !pHTInfo->bCurSuppCCK))
 195                        rate = 0x0c;
 196                else
 197                        rate = 0x02;
 198        }
 199
 200        return rate;
 201}
 202
 203inline void softmac_mgmt_xmit(struct sk_buff *skb, struct rtllib_device *ieee)
 204{
 205        unsigned long flags;
 206        short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
 207        struct rtllib_hdr_3addr  *header =
 208                (struct rtllib_hdr_3addr  *) skb->data;
 209
 210        struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 8);
 211
 212        spin_lock_irqsave(&ieee->lock, flags);
 213
 214        /* called with 2nd param 0, no mgmt lock required */
 215        rtllib_sta_wakeup(ieee, 0);
 216
 217        if (le16_to_cpu(header->frame_ctl) == RTLLIB_STYPE_BEACON)
 218                tcb_desc->queue_index = BEACON_QUEUE;
 219        else
 220                tcb_desc->queue_index = MGNT_QUEUE;
 221
 222        if (ieee->disable_mgnt_queue)
 223                tcb_desc->queue_index = HIGH_QUEUE;
 224
 225        tcb_desc->data_rate = MgntQuery_MgntFrameTxRate(ieee);
 226        tcb_desc->RATRIndex = 7;
 227        tcb_desc->bTxDisableRateFallBack = 1;
 228        tcb_desc->bTxUseDriverAssingedRate = 1;
 229        if (single) {
 230                if (ieee->queue_stop) {
 231                        enqueue_mgmt(ieee, skb);
 232                } else {
 233                        header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0]<<4);
 234
 235                        if (ieee->seq_ctrl[0] == 0xFFF)
 236                                ieee->seq_ctrl[0] = 0;
 237                        else
 238                                ieee->seq_ctrl[0]++;
 239
 240                        /* avoid watchdog triggers */
 241                        ieee->softmac_data_hard_start_xmit(skb, ieee->dev,
 242                                                           ieee->basic_rate);
 243                }
 244
 245                spin_unlock_irqrestore(&ieee->lock, flags);
 246        } else {
 247                spin_unlock_irqrestore(&ieee->lock, flags);
 248                spin_lock_irqsave(&ieee->mgmt_tx_lock, flags);
 249
 250                header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
 251
 252                if (ieee->seq_ctrl[0] == 0xFFF)
 253                        ieee->seq_ctrl[0] = 0;
 254                else
 255                        ieee->seq_ctrl[0]++;
 256
 257                /* check whether the managed packet queued greater than 5 */
 258                if (!ieee->check_nic_enough_desc(ieee->dev,
 259                                                 tcb_desc->queue_index) ||
 260                    skb_queue_len(&ieee->skb_waitQ[tcb_desc->queue_index]) ||
 261                    ieee->queue_stop) {
 262                        /* insert the skb packet to the management queue
 263                         *
 264                         * as for the completion function, it does not need
 265                         * to check it any more.
 266                         */
 267                        netdev_info(ieee->dev,
 268                               "%s():insert to waitqueue, queue_index:%d!\n",
 269                               __func__, tcb_desc->queue_index);
 270                        skb_queue_tail(&ieee->skb_waitQ[tcb_desc->queue_index],
 271                                       skb);
 272                } else {
 273                        ieee->softmac_hard_start_xmit(skb, ieee->dev);
 274                }
 275                spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags);
 276        }
 277}
 278
 279static inline void
 280softmac_ps_mgmt_xmit(struct sk_buff *skb,
 281                     struct rtllib_device *ieee)
 282{
 283        short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
 284        struct rtllib_hdr_3addr  *header =
 285                (struct rtllib_hdr_3addr  *) skb->data;
 286        u16 fc, type, stype;
 287        struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 8);
 288
 289        fc = le16_to_cpu(header->frame_ctl);
 290        type = WLAN_FC_GET_TYPE(fc);
 291        stype = WLAN_FC_GET_STYPE(fc);
 292
 293
 294        if (stype != RTLLIB_STYPE_PSPOLL)
 295                tcb_desc->queue_index = MGNT_QUEUE;
 296        else
 297                tcb_desc->queue_index = HIGH_QUEUE;
 298
 299        if (ieee->disable_mgnt_queue)
 300                tcb_desc->queue_index = HIGH_QUEUE;
 301
 302
 303        tcb_desc->data_rate = MgntQuery_MgntFrameTxRate(ieee);
 304        tcb_desc->RATRIndex = 7;
 305        tcb_desc->bTxDisableRateFallBack = 1;
 306        tcb_desc->bTxUseDriverAssingedRate = 1;
 307        if (single) {
 308                if (type != RTLLIB_FTYPE_CTL) {
 309                        header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
 310
 311                        if (ieee->seq_ctrl[0] == 0xFFF)
 312                                ieee->seq_ctrl[0] = 0;
 313                        else
 314                                ieee->seq_ctrl[0]++;
 315
 316                }
 317                /* avoid watchdog triggers */
 318                ieee->softmac_data_hard_start_xmit(skb, ieee->dev,
 319                                                   ieee->basic_rate);
 320
 321        } else {
 322                if (type != RTLLIB_FTYPE_CTL) {
 323                        header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
 324
 325                        if (ieee->seq_ctrl[0] == 0xFFF)
 326                                ieee->seq_ctrl[0] = 0;
 327                        else
 328                                ieee->seq_ctrl[0]++;
 329                }
 330                ieee->softmac_hard_start_xmit(skb, ieee->dev);
 331
 332        }
 333}
 334
 335static inline struct sk_buff *rtllib_probe_req(struct rtllib_device *ieee)
 336{
 337        unsigned int len, rate_len;
 338        u8 *tag;
 339        struct sk_buff *skb;
 340        struct rtllib_probe_request *req;
 341
 342        len = ieee->current_network.ssid_len;
 343
 344        rate_len = rtllib_MFIE_rate_len(ieee);
 345
 346        skb = dev_alloc_skb(sizeof(struct rtllib_probe_request) +
 347                            2 + len + rate_len + ieee->tx_headroom);
 348
 349        if (!skb)
 350                return NULL;
 351
 352        skb_reserve(skb, ieee->tx_headroom);
 353
 354        req = skb_put(skb, sizeof(struct rtllib_probe_request));
 355        req->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_PROBE_REQ);
 356        req->header.duration_id = 0;
 357
 358        eth_broadcast_addr(req->header.addr1);
 359        ether_addr_copy(req->header.addr2, ieee->dev->dev_addr);
 360        eth_broadcast_addr(req->header.addr3);
 361
 362        tag = skb_put(skb, len + 2 + rate_len);
 363
 364        *tag++ = MFIE_TYPE_SSID;
 365        *tag++ = len;
 366        memcpy(tag, ieee->current_network.ssid, len);
 367        tag += len;
 368
 369        rtllib_MFIE_Brate(ieee, &tag);
 370        rtllib_MFIE_Grate(ieee, &tag);
 371
 372        return skb;
 373}
 374
 375static struct sk_buff *rtllib_get_beacon_(struct rtllib_device *ieee);
 376
 377static void rtllib_send_beacon(struct rtllib_device *ieee)
 378{
 379        struct sk_buff *skb;
 380
 381        if (!ieee->ieee_up)
 382                return;
 383        skb = rtllib_get_beacon_(ieee);
 384
 385        if (skb) {
 386                softmac_mgmt_xmit(skb, ieee);
 387                ieee->softmac_stats.tx_beacons++;
 388        }
 389
 390        if (ieee->beacon_txing && ieee->ieee_up)
 391                mod_timer(&ieee->beacon_timer, jiffies +
 392                          (msecs_to_jiffies(ieee->current_network.beacon_interval - 5)));
 393}
 394
 395
 396static void rtllib_send_beacon_cb(struct timer_list *t)
 397{
 398        struct rtllib_device *ieee =
 399                from_timer(ieee, t, beacon_timer);
 400        unsigned long flags;
 401
 402        spin_lock_irqsave(&ieee->beacon_lock, flags);
 403        rtllib_send_beacon(ieee);
 404        spin_unlock_irqrestore(&ieee->beacon_lock, flags);
 405}
 406
 407/* Enables network monitor mode, all rx packets will be received. */
 408void rtllib_EnableNetMonitorMode(struct net_device *dev,
 409                bool bInitState)
 410{
 411        struct rtllib_device *ieee = netdev_priv_rsl(dev);
 412
 413        netdev_info(dev, "========>Enter Monitor Mode\n");
 414
 415        ieee->AllowAllDestAddrHandler(dev, true, !bInitState);
 416}
 417
 418
 419/* Disables network monitor mode. Only packets destinated to
 420 * us will be received.
 421 */
 422void rtllib_DisableNetMonitorMode(struct net_device *dev,
 423                bool bInitState)
 424{
 425        struct rtllib_device *ieee = netdev_priv_rsl(dev);
 426
 427        netdev_info(dev, "========>Exit Monitor Mode\n");
 428
 429        ieee->AllowAllDestAddrHandler(dev, false, !bInitState);
 430}
 431
 432
 433/* Enables the specialized promiscuous mode required by Intel.
 434 * In this mode, Intel intends to hear traffics from/to other STAs in the
 435 * same BSS. Therefore we don't have to disable checking BSSID and we only need
 436 * to allow all dest. BUT: if we enable checking BSSID then we can't recv
 437 * packets from other STA.
 438 */
 439void rtllib_EnableIntelPromiscuousMode(struct net_device *dev,
 440                bool bInitState)
 441{
 442        bool bFilterOutNonAssociatedBSSID = false;
 443
 444        struct rtllib_device *ieee = netdev_priv_rsl(dev);
 445
 446        netdev_info(dev, "========>Enter Intel Promiscuous Mode\n");
 447
 448        ieee->AllowAllDestAddrHandler(dev, true, !bInitState);
 449        ieee->SetHwRegHandler(dev, HW_VAR_CECHK_BSSID,
 450                             (u8 *)&bFilterOutNonAssociatedBSSID);
 451
 452        ieee->bNetPromiscuousMode = true;
 453}
 454EXPORT_SYMBOL(rtllib_EnableIntelPromiscuousMode);
 455
 456
 457/* Disables the specialized promiscuous mode required by Intel.
 458 * See MgntEnableIntelPromiscuousMode for detail.
 459 */
 460void rtllib_DisableIntelPromiscuousMode(struct net_device *dev,
 461                bool bInitState)
 462{
 463        bool bFilterOutNonAssociatedBSSID = true;
 464
 465        struct rtllib_device *ieee = netdev_priv_rsl(dev);
 466
 467        netdev_info(dev, "========>Exit Intel Promiscuous Mode\n");
 468
 469        ieee->AllowAllDestAddrHandler(dev, false, !bInitState);
 470        ieee->SetHwRegHandler(dev, HW_VAR_CECHK_BSSID,
 471                             (u8 *)&bFilterOutNonAssociatedBSSID);
 472
 473        ieee->bNetPromiscuousMode = false;
 474}
 475EXPORT_SYMBOL(rtllib_DisableIntelPromiscuousMode);
 476
 477static void rtllib_send_probe(struct rtllib_device *ieee, u8 is_mesh)
 478{
 479        struct sk_buff *skb;
 480
 481        skb = rtllib_probe_req(ieee);
 482        if (skb) {
 483                softmac_mgmt_xmit(skb, ieee);
 484                ieee->softmac_stats.tx_probe_rq++;
 485        }
 486}
 487
 488
 489static void rtllib_send_probe_requests(struct rtllib_device *ieee, u8 is_mesh)
 490{
 491        if (ieee->active_scan && (ieee->softmac_features &
 492            IEEE_SOFTMAC_PROBERQ)) {
 493                rtllib_send_probe(ieee, 0);
 494                rtllib_send_probe(ieee, 0);
 495        }
 496}
 497
 498static void rtllib_update_active_chan_map(struct rtllib_device *ieee)
 499{
 500        memcpy(ieee->active_channel_map, GET_DOT11D_INFO(ieee)->channel_map,
 501               MAX_CHANNEL_NUMBER+1);
 502}
 503
 504/* this performs syncro scan blocking the caller until all channels
 505 * in the allowed channel map has been checked.
 506 */
 507static void rtllib_softmac_scan_syncro(struct rtllib_device *ieee, u8 is_mesh)
 508{
 509        union iwreq_data wrqu;
 510        short ch = 0;
 511
 512        rtllib_update_active_chan_map(ieee);
 513
 514        ieee->be_scan_inprogress = true;
 515
 516        mutex_lock(&ieee->scan_mutex);
 517
 518        while (1) {
 519                do {
 520                        ch++;
 521                        if (ch > MAX_CHANNEL_NUMBER)
 522                                goto out; /* scan completed */
 523                } while (!ieee->active_channel_map[ch]);
 524
 525                /* this function can be called in two situations
 526                 * 1- We have switched to ad-hoc mode and we are
 527                 *    performing a complete syncro scan before conclude
 528                 *    there are no interesting cell and to create a
 529                 *    new one. In this case the link state is
 530                 *    RTLLIB_NOLINK until we found an interesting cell.
 531                 *    If so the ieee8021_new_net, called by the RX path
 532                 *    will set the state to RTLLIB_LINKED, so we stop
 533                 *    scanning
 534                 * 2- We are linked and the root uses run iwlist scan.
 535                 *    So we switch to RTLLIB_LINKED_SCANNING to remember
 536                 *    that we are still logically linked (not interested in
 537                 *    new network events, despite for updating the net list,
 538                 *    but we are temporarly 'unlinked' as the driver shall
 539                 *    not filter RX frames and the channel is changing.
 540                 * So the only situation in which are interested is to check
 541                 * if the state become LINKED because of the #1 situation
 542                 */
 543
 544                if (ieee->state == RTLLIB_LINKED)
 545                        goto out;
 546                if (ieee->sync_scan_hurryup) {
 547                        netdev_info(ieee->dev,
 548                                    "============>sync_scan_hurryup out\n");
 549                        goto out;
 550                }
 551
 552                ieee->set_chan(ieee->dev, ch);
 553                if (ieee->active_channel_map[ch] == 1)
 554                        rtllib_send_probe_requests(ieee, 0);
 555
 556                /* this prevent excessive time wait when we
 557                 * need to wait for a syncro scan to end..
 558                 */
 559                msleep_interruptible_rsl(RTLLIB_SOFTMAC_SCAN_TIME);
 560        }
 561out:
 562        ieee->actscanning = false;
 563        ieee->sync_scan_hurryup = 0;
 564
 565        if (ieee->state >= RTLLIB_LINKED) {
 566                if (IS_DOT11D_ENABLE(ieee))
 567                        dot11d_scan_complete(ieee);
 568        }
 569        mutex_unlock(&ieee->scan_mutex);
 570
 571        ieee->be_scan_inprogress = false;
 572
 573        memset(&wrqu, 0, sizeof(wrqu));
 574        wireless_send_event(ieee->dev, SIOCGIWSCAN, &wrqu, NULL);
 575}
 576
 577static void rtllib_softmac_scan_wq(void *data)
 578{
 579        struct rtllib_device *ieee = container_of_dwork_rsl(data,
 580                                     struct rtllib_device, softmac_scan_wq);
 581        u8 last_channel = ieee->current_network.channel;
 582
 583        rtllib_update_active_chan_map(ieee);
 584
 585        if (!ieee->ieee_up)
 586                return;
 587        if (rtllib_act_scanning(ieee, true))
 588                return;
 589
 590        mutex_lock(&ieee->scan_mutex);
 591
 592        if (ieee->eRFPowerState == eRfOff) {
 593                netdev_info(ieee->dev,
 594                            "======>%s():rf state is eRfOff, return\n",
 595                            __func__);
 596                goto out1;
 597        }
 598
 599        do {
 600                ieee->current_network.channel =
 601                        (ieee->current_network.channel + 1) %
 602                        MAX_CHANNEL_NUMBER;
 603                if (ieee->scan_watch_dog++ > MAX_CHANNEL_NUMBER) {
 604                        if (!ieee->active_channel_map[ieee->current_network.channel])
 605                                ieee->current_network.channel = 6;
 606                        goto out; /* no good chans */
 607                }
 608        } while (!ieee->active_channel_map[ieee->current_network.channel]);
 609
 610        if (ieee->scanning_continue == 0)
 611                goto out;
 612
 613        ieee->set_chan(ieee->dev, ieee->current_network.channel);
 614
 615        if (ieee->active_channel_map[ieee->current_network.channel] == 1)
 616                rtllib_send_probe_requests(ieee, 0);
 617
 618        schedule_delayed_work(&ieee->softmac_scan_wq,
 619                              msecs_to_jiffies(RTLLIB_SOFTMAC_SCAN_TIME));
 620
 621        mutex_unlock(&ieee->scan_mutex);
 622        return;
 623
 624out:
 625        if (IS_DOT11D_ENABLE(ieee))
 626                dot11d_scan_complete(ieee);
 627        ieee->current_network.channel = last_channel;
 628
 629out1:
 630        ieee->actscanning = false;
 631        ieee->scan_watch_dog = 0;
 632        ieee->scanning_continue = 0;
 633        mutex_unlock(&ieee->scan_mutex);
 634}
 635
 636
 637
 638static void rtllib_beacons_start(struct rtllib_device *ieee)
 639{
 640        unsigned long flags;
 641
 642        spin_lock_irqsave(&ieee->beacon_lock, flags);
 643
 644        ieee->beacon_txing = 1;
 645        rtllib_send_beacon(ieee);
 646
 647        spin_unlock_irqrestore(&ieee->beacon_lock, flags);
 648}
 649
 650static void rtllib_beacons_stop(struct rtllib_device *ieee)
 651{
 652        unsigned long flags;
 653
 654        spin_lock_irqsave(&ieee->beacon_lock, flags);
 655
 656        ieee->beacon_txing = 0;
 657        del_timer_sync(&ieee->beacon_timer);
 658
 659        spin_unlock_irqrestore(&ieee->beacon_lock, flags);
 660
 661}
 662
 663
 664void rtllib_stop_send_beacons(struct rtllib_device *ieee)
 665{
 666        if (ieee->stop_send_beacons)
 667                ieee->stop_send_beacons(ieee->dev);
 668        if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
 669                rtllib_beacons_stop(ieee);
 670}
 671EXPORT_SYMBOL(rtllib_stop_send_beacons);
 672
 673
 674void rtllib_start_send_beacons(struct rtllib_device *ieee)
 675{
 676        if (ieee->start_send_beacons)
 677                ieee->start_send_beacons(ieee->dev);
 678        if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
 679                rtllib_beacons_start(ieee);
 680}
 681EXPORT_SYMBOL(rtllib_start_send_beacons);
 682
 683
 684static void rtllib_softmac_stop_scan(struct rtllib_device *ieee)
 685{
 686        mutex_lock(&ieee->scan_mutex);
 687        ieee->scan_watch_dog = 0;
 688        if (ieee->scanning_continue == 1) {
 689                ieee->scanning_continue = 0;
 690                ieee->actscanning = false;
 691
 692                cancel_delayed_work_sync(&ieee->softmac_scan_wq);
 693        }
 694
 695        mutex_unlock(&ieee->scan_mutex);
 696}
 697
 698void rtllib_stop_scan(struct rtllib_device *ieee)
 699{
 700        if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
 701                rtllib_softmac_stop_scan(ieee);
 702        } else {
 703                if (ieee->rtllib_stop_hw_scan)
 704                        ieee->rtllib_stop_hw_scan(ieee->dev);
 705        }
 706}
 707EXPORT_SYMBOL(rtllib_stop_scan);
 708
 709void rtllib_stop_scan_syncro(struct rtllib_device *ieee)
 710{
 711        if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
 712                ieee->sync_scan_hurryup = 1;
 713        } else {
 714                if (ieee->rtllib_stop_hw_scan)
 715                        ieee->rtllib_stop_hw_scan(ieee->dev);
 716        }
 717}
 718EXPORT_SYMBOL(rtllib_stop_scan_syncro);
 719
 720bool rtllib_act_scanning(struct rtllib_device *ieee, bool sync_scan)
 721{
 722        if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
 723                if (sync_scan)
 724                        return ieee->be_scan_inprogress;
 725                else
 726                        return ieee->actscanning || ieee->be_scan_inprogress;
 727        } else {
 728                return test_bit(STATUS_SCANNING, &ieee->status);
 729        }
 730}
 731EXPORT_SYMBOL(rtllib_act_scanning);
 732
 733/* called with ieee->lock held */
 734static void rtllib_start_scan(struct rtllib_device *ieee)
 735{
 736        RT_TRACE(COMP_DBG, "===>%s()\n", __func__);
 737        if (ieee->rtllib_ips_leave_wq != NULL)
 738                ieee->rtllib_ips_leave_wq(ieee->dev);
 739
 740        if (IS_DOT11D_ENABLE(ieee)) {
 741                if (IS_COUNTRY_IE_VALID(ieee))
 742                        RESET_CIE_WATCHDOG(ieee);
 743        }
 744        if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
 745                if (ieee->scanning_continue == 0) {
 746                        ieee->actscanning = true;
 747                        ieee->scanning_continue = 1;
 748                        schedule_delayed_work(&ieee->softmac_scan_wq, 0);
 749                }
 750        } else {
 751                if (ieee->rtllib_start_hw_scan)
 752                        ieee->rtllib_start_hw_scan(ieee->dev);
 753        }
 754}
 755
 756/* called with wx_mutex held */
 757void rtllib_start_scan_syncro(struct rtllib_device *ieee, u8 is_mesh)
 758{
 759        if (IS_DOT11D_ENABLE(ieee)) {
 760                if (IS_COUNTRY_IE_VALID(ieee))
 761                        RESET_CIE_WATCHDOG(ieee);
 762        }
 763        ieee->sync_scan_hurryup = 0;
 764        if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
 765                rtllib_softmac_scan_syncro(ieee, is_mesh);
 766        } else {
 767                if (ieee->rtllib_start_hw_scan)
 768                        ieee->rtllib_start_hw_scan(ieee->dev);
 769        }
 770}
 771EXPORT_SYMBOL(rtllib_start_scan_syncro);
 772
 773static inline struct sk_buff *
 774rtllib_authentication_req(struct rtllib_network *beacon,
 775                          struct rtllib_device *ieee,
 776                          int challengelen, u8 *daddr)
 777{
 778        struct sk_buff *skb;
 779        struct rtllib_authentication *auth;
 780        int  len;
 781
 782        len = sizeof(struct rtllib_authentication) + challengelen +
 783                     ieee->tx_headroom + 4;
 784        skb = dev_alloc_skb(len);
 785
 786        if (!skb)
 787                return NULL;
 788
 789        skb_reserve(skb, ieee->tx_headroom);
 790
 791        auth = skb_put(skb, sizeof(struct rtllib_authentication));
 792
 793        auth->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_AUTH);
 794        if (challengelen)
 795                auth->header.frame_ctl |= cpu_to_le16(RTLLIB_FCTL_WEP);
 796
 797        auth->header.duration_id = cpu_to_le16(0x013a);
 798        ether_addr_copy(auth->header.addr1, beacon->bssid);
 799        ether_addr_copy(auth->header.addr2, ieee->dev->dev_addr);
 800        ether_addr_copy(auth->header.addr3, beacon->bssid);
 801        if (ieee->auth_mode == 0)
 802                auth->algorithm = WLAN_AUTH_OPEN;
 803        else if (ieee->auth_mode == 1)
 804                auth->algorithm = cpu_to_le16(WLAN_AUTH_SHARED_KEY);
 805        else if (ieee->auth_mode == 2)
 806                auth->algorithm = WLAN_AUTH_OPEN;
 807        auth->transaction = cpu_to_le16(ieee->associate_seq);
 808        ieee->associate_seq++;
 809
 810        auth->status = cpu_to_le16(WLAN_STATUS_SUCCESS);
 811
 812        return skb;
 813}
 814
 815static struct sk_buff *rtllib_probe_resp(struct rtllib_device *ieee,
 816                                         const u8 *dest)
 817{
 818        u8 *tag;
 819        int beacon_size;
 820        struct rtllib_probe_response *beacon_buf;
 821        struct sk_buff *skb = NULL;
 822        int encrypt;
 823        int atim_len, erp_len;
 824        struct lib80211_crypt_data *crypt;
 825
 826        char *ssid = ieee->current_network.ssid;
 827        int ssid_len = ieee->current_network.ssid_len;
 828        int rate_len = ieee->current_network.rates_len+2;
 829        int rate_ex_len = ieee->current_network.rates_ex_len;
 830        int wpa_ie_len = ieee->wpa_ie_len;
 831        u8 erpinfo_content = 0;
 832
 833        u8 *tmp_ht_cap_buf = NULL;
 834        u8 tmp_ht_cap_len = 0;
 835        u8 *tmp_ht_info_buf = NULL;
 836        u8 tmp_ht_info_len = 0;
 837        struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
 838        u8 *tmp_generic_ie_buf = NULL;
 839        u8 tmp_generic_ie_len = 0;
 840
 841        if (rate_ex_len > 0)
 842                rate_ex_len += 2;
 843
 844        if (ieee->current_network.capability & WLAN_CAPABILITY_IBSS)
 845                atim_len = 4;
 846        else
 847                atim_len = 0;
 848
 849        if ((ieee->current_network.mode == IEEE_G) ||
 850           (ieee->current_network.mode == IEEE_N_24G &&
 851           ieee->pHTInfo->bCurSuppCCK)) {
 852                erp_len = 3;
 853                erpinfo_content = 0;
 854                if (ieee->current_network.buseprotection)
 855                        erpinfo_content |= ERP_UseProtection;
 856        } else
 857                erp_len = 0;
 858
 859        crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
 860        encrypt = ieee->host_encrypt && crypt && crypt->ops &&
 861                ((strcmp(crypt->ops->name, "R-WEP") == 0 || wpa_ie_len));
 862        if (ieee->pHTInfo->bCurrentHTSupport) {
 863                tmp_ht_cap_buf = (u8 *) &(ieee->pHTInfo->SelfHTCap);
 864                tmp_ht_cap_len = sizeof(ieee->pHTInfo->SelfHTCap);
 865                tmp_ht_info_buf = (u8 *) &(ieee->pHTInfo->SelfHTInfo);
 866                tmp_ht_info_len = sizeof(ieee->pHTInfo->SelfHTInfo);
 867                HTConstructCapabilityElement(ieee, tmp_ht_cap_buf,
 868                                             &tmp_ht_cap_len, encrypt, false);
 869                HTConstructInfoElement(ieee, tmp_ht_info_buf, &tmp_ht_info_len,
 870                                       encrypt);
 871
 872                if (pHTInfo->bRegRT2RTAggregation) {
 873                        tmp_generic_ie_buf = ieee->pHTInfo->szRT2RTAggBuffer;
 874                        tmp_generic_ie_len =
 875                                 sizeof(ieee->pHTInfo->szRT2RTAggBuffer);
 876                        HTConstructRT2RTAggElement(ieee, tmp_generic_ie_buf,
 877                                                   &tmp_generic_ie_len);
 878                }
 879        }
 880
 881        beacon_size = sizeof(struct rtllib_probe_response)+2+
 882                ssid_len + 3 + rate_len + rate_ex_len + atim_len + erp_len
 883                + wpa_ie_len + ieee->tx_headroom;
 884        skb = dev_alloc_skb(beacon_size);
 885        if (!skb)
 886                return NULL;
 887
 888        skb_reserve(skb, ieee->tx_headroom);
 889
 890        beacon_buf = skb_put(skb, (beacon_size - ieee->tx_headroom));
 891        ether_addr_copy(beacon_buf->header.addr1, dest);
 892        ether_addr_copy(beacon_buf->header.addr2, ieee->dev->dev_addr);
 893        ether_addr_copy(beacon_buf->header.addr3, ieee->current_network.bssid);
 894
 895        beacon_buf->header.duration_id = 0;
 896        beacon_buf->beacon_interval =
 897                cpu_to_le16(ieee->current_network.beacon_interval);
 898        beacon_buf->capability =
 899                cpu_to_le16(ieee->current_network.capability &
 900                WLAN_CAPABILITY_IBSS);
 901        beacon_buf->capability |=
 902                cpu_to_le16(ieee->current_network.capability &
 903                WLAN_CAPABILITY_SHORT_PREAMBLE);
 904
 905        if (ieee->short_slot && (ieee->current_network.capability &
 906            WLAN_CAPABILITY_SHORT_SLOT_TIME))
 907                beacon_buf->capability |=
 908                        cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
 909
 910        crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
 911        if (encrypt)
 912                beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
 913
 914
 915        beacon_buf->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_PROBE_RESP);
 916        beacon_buf->info_element[0].id = MFIE_TYPE_SSID;
 917        beacon_buf->info_element[0].len = ssid_len;
 918
 919        tag = (u8 *) beacon_buf->info_element[0].data;
 920
 921        memcpy(tag, ssid, ssid_len);
 922
 923        tag += ssid_len;
 924
 925        *(tag++) = MFIE_TYPE_RATES;
 926        *(tag++) = rate_len-2;
 927        memcpy(tag, ieee->current_network.rates, rate_len-2);
 928        tag += rate_len-2;
 929
 930        *(tag++) = MFIE_TYPE_DS_SET;
 931        *(tag++) = 1;
 932        *(tag++) = ieee->current_network.channel;
 933
 934        if (atim_len) {
 935                u16 val16;
 936                *(tag++) = MFIE_TYPE_IBSS_SET;
 937                *(tag++) = 2;
 938                val16 = ieee->current_network.atim_window;
 939                memcpy((u8 *)tag, (u8 *)&val16, 2);
 940                tag += 2;
 941        }
 942
 943        if (erp_len) {
 944                *(tag++) = MFIE_TYPE_ERP;
 945                *(tag++) = 1;
 946                *(tag++) = erpinfo_content;
 947        }
 948        if (rate_ex_len) {
 949                *(tag++) = MFIE_TYPE_RATES_EX;
 950                *(tag++) = rate_ex_len-2;
 951                memcpy(tag, ieee->current_network.rates_ex, rate_ex_len-2);
 952                tag += rate_ex_len-2;
 953        }
 954
 955        if (wpa_ie_len) {
 956                if (ieee->iw_mode == IW_MODE_ADHOC)
 957                        memcpy(&ieee->wpa_ie[14], &ieee->wpa_ie[8], 4);
 958                memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
 959                tag += ieee->wpa_ie_len;
 960        }
 961        return skb;
 962}
 963
 964static struct sk_buff *rtllib_assoc_resp(struct rtllib_device *ieee, u8 *dest)
 965{
 966        struct sk_buff *skb;
 967        u8 *tag;
 968
 969        struct lib80211_crypt_data *crypt;
 970        struct rtllib_assoc_response_frame *assoc;
 971        short encrypt;
 972
 973        unsigned int rate_len = rtllib_MFIE_rate_len(ieee);
 974        int len = sizeof(struct rtllib_assoc_response_frame) + rate_len +
 975                  ieee->tx_headroom;
 976
 977        skb = dev_alloc_skb(len);
 978
 979        if (!skb)
 980                return NULL;
 981
 982        skb_reserve(skb, ieee->tx_headroom);
 983
 984        assoc = skb_put(skb, sizeof(struct rtllib_assoc_response_frame));
 985
 986        assoc->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_ASSOC_RESP);
 987        ether_addr_copy(assoc->header.addr1, dest);
 988        ether_addr_copy(assoc->header.addr3, ieee->dev->dev_addr);
 989        ether_addr_copy(assoc->header.addr2, ieee->dev->dev_addr);
 990        assoc->capability = cpu_to_le16(ieee->iw_mode == IW_MODE_MASTER ?
 991                WLAN_CAPABILITY_ESS : WLAN_CAPABILITY_IBSS);
 992
 993
 994        if (ieee->short_slot)
 995                assoc->capability |=
 996                                 cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
 997
 998        if (ieee->host_encrypt)
 999                crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
1000        else
1001                crypt = NULL;
1002
1003        encrypt = (crypt && crypt->ops);
1004
1005        if (encrypt)
1006                assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1007
1008        assoc->status = 0;
1009        assoc->aid = cpu_to_le16(ieee->assoc_id);
1010        if (ieee->assoc_id == 0x2007)
1011                ieee->assoc_id = 0;
1012        else
1013                ieee->assoc_id++;
1014
1015        tag = skb_put(skb, rate_len);
1016        rtllib_MFIE_Brate(ieee, &tag);
1017        rtllib_MFIE_Grate(ieee, &tag);
1018
1019        return skb;
1020}
1021
1022static struct sk_buff *rtllib_auth_resp(struct rtllib_device *ieee, int status,
1023                                 u8 *dest)
1024{
1025        struct sk_buff *skb = NULL;
1026        struct rtllib_authentication *auth;
1027        int len = ieee->tx_headroom + sizeof(struct rtllib_authentication) + 1;
1028
1029        skb = dev_alloc_skb(len);
1030        if (!skb)
1031                return NULL;
1032
1033        skb->len = sizeof(struct rtllib_authentication);
1034
1035        skb_reserve(skb, ieee->tx_headroom);
1036
1037        auth = skb_put(skb, sizeof(struct rtllib_authentication));
1038
1039        auth->status = cpu_to_le16(status);
1040        auth->transaction = cpu_to_le16(2);
1041        auth->algorithm = cpu_to_le16(WLAN_AUTH_OPEN);
1042
1043        ether_addr_copy(auth->header.addr3, ieee->dev->dev_addr);
1044        ether_addr_copy(auth->header.addr2, ieee->dev->dev_addr);
1045        ether_addr_copy(auth->header.addr1, dest);
1046        auth->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_AUTH);
1047        return skb;
1048
1049
1050}
1051
1052static struct sk_buff *rtllib_null_func(struct rtllib_device *ieee, short pwr)
1053{
1054        struct sk_buff *skb;
1055        struct rtllib_hdr_3addr *hdr;
1056
1057        skb = dev_alloc_skb(sizeof(struct rtllib_hdr_3addr)+ieee->tx_headroom);
1058        if (!skb)
1059                return NULL;
1060
1061        skb_reserve(skb, ieee->tx_headroom);
1062
1063        hdr = skb_put(skb, sizeof(struct rtllib_hdr_3addr));
1064
1065        ether_addr_copy(hdr->addr1, ieee->current_network.bssid);
1066        ether_addr_copy(hdr->addr2, ieee->dev->dev_addr);
1067        ether_addr_copy(hdr->addr3, ieee->current_network.bssid);
1068
1069        hdr->frame_ctl = cpu_to_le16(RTLLIB_FTYPE_DATA |
1070                RTLLIB_STYPE_NULLFUNC | RTLLIB_FCTL_TODS |
1071                (pwr ? RTLLIB_FCTL_PM : 0));
1072
1073        return skb;
1074
1075
1076}
1077
1078static struct sk_buff *rtllib_pspoll_func(struct rtllib_device *ieee)
1079{
1080        struct sk_buff *skb;
1081        struct rtllib_pspoll_hdr *hdr;
1082
1083        skb = dev_alloc_skb(sizeof(struct rtllib_pspoll_hdr)+ieee->tx_headroom);
1084        if (!skb)
1085                return NULL;
1086
1087        skb_reserve(skb, ieee->tx_headroom);
1088
1089        hdr = skb_put(skb, sizeof(struct rtllib_pspoll_hdr));
1090
1091        ether_addr_copy(hdr->bssid, ieee->current_network.bssid);
1092        ether_addr_copy(hdr->ta, ieee->dev->dev_addr);
1093
1094        hdr->aid = cpu_to_le16(ieee->assoc_id | 0xc000);
1095        hdr->frame_ctl = cpu_to_le16(RTLLIB_FTYPE_CTL | RTLLIB_STYPE_PSPOLL |
1096                         RTLLIB_FCTL_PM);
1097
1098        return skb;
1099
1100}
1101
1102static void rtllib_resp_to_assoc_rq(struct rtllib_device *ieee, u8 *dest)
1103{
1104        struct sk_buff *buf = rtllib_assoc_resp(ieee, dest);
1105
1106        if (buf)
1107                softmac_mgmt_xmit(buf, ieee);
1108}
1109
1110
1111static void rtllib_resp_to_auth(struct rtllib_device *ieee, int s, u8 *dest)
1112{
1113        struct sk_buff *buf = rtllib_auth_resp(ieee, s, dest);
1114
1115        if (buf)
1116                softmac_mgmt_xmit(buf, ieee);
1117}
1118
1119
1120static void rtllib_resp_to_probe(struct rtllib_device *ieee, u8 *dest)
1121{
1122        struct sk_buff *buf = rtllib_probe_resp(ieee, dest);
1123
1124        if (buf)
1125                softmac_mgmt_xmit(buf, ieee);
1126}
1127
1128
1129static inline int SecIsInPMKIDList(struct rtllib_device *ieee, u8 *bssid)
1130{
1131        int i = 0;
1132
1133        do {
1134                if ((ieee->PMKIDList[i].bUsed) &&
1135                   (memcmp(ieee->PMKIDList[i].Bssid, bssid, ETH_ALEN) == 0))
1136                        break;
1137                i++;
1138        } while (i < NUM_PMKID_CACHE);
1139
1140        if (i == NUM_PMKID_CACHE)
1141                i = -1;
1142        return i;
1143}
1144
1145static inline struct sk_buff *
1146rtllib_association_req(struct rtllib_network *beacon,
1147                       struct rtllib_device *ieee)
1148{
1149        struct sk_buff *skb;
1150        struct rtllib_assoc_request_frame *hdr;
1151        u8 *tag, *ies;
1152        int i;
1153        u8 *ht_cap_buf = NULL;
1154        u8 ht_cap_len = 0;
1155        u8 *realtek_ie_buf = NULL;
1156        u8 realtek_ie_len = 0;
1157        int wpa_ie_len = ieee->wpa_ie_len;
1158        int wps_ie_len = ieee->wps_ie_len;
1159        unsigned int ckip_ie_len = 0;
1160        unsigned int ccxrm_ie_len = 0;
1161        unsigned int cxvernum_ie_len = 0;
1162        struct lib80211_crypt_data *crypt;
1163        int encrypt;
1164        int     PMKCacheIdx;
1165
1166        unsigned int rate_len = (beacon->rates_len ?
1167                                (beacon->rates_len + 2) : 0) +
1168                                (beacon->rates_ex_len ? (beacon->rates_ex_len) +
1169                                2 : 0);
1170
1171        unsigned int wmm_info_len = beacon->qos_data.supported ? 9 : 0;
1172        unsigned int turbo_info_len = beacon->Turbo_Enable ? 9 : 0;
1173
1174        int len = 0;
1175
1176        crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
1177        if (crypt != NULL)
1178                encrypt = ieee->host_encrypt && crypt && crypt->ops &&
1179                          ((strcmp(crypt->ops->name, "R-WEP") == 0 ||
1180                          wpa_ie_len));
1181        else
1182                encrypt = 0;
1183
1184        if ((ieee->rtllib_ap_sec_type &&
1185            (ieee->rtllib_ap_sec_type(ieee) & SEC_ALG_TKIP)) ||
1186            ieee->bForcedBgMode) {
1187                ieee->pHTInfo->bEnableHT = 0;
1188                ieee->mode = WIRELESS_MODE_G;
1189        }
1190
1191        if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1192                ht_cap_buf = (u8 *)&(ieee->pHTInfo->SelfHTCap);
1193                ht_cap_len = sizeof(ieee->pHTInfo->SelfHTCap);
1194                HTConstructCapabilityElement(ieee, ht_cap_buf, &ht_cap_len,
1195                                             encrypt, true);
1196                if (ieee->pHTInfo->bCurrentRT2RTAggregation) {
1197                        realtek_ie_buf = ieee->pHTInfo->szRT2RTAggBuffer;
1198                        realtek_ie_len =
1199                                 sizeof(ieee->pHTInfo->szRT2RTAggBuffer);
1200                        HTConstructRT2RTAggElement(ieee, realtek_ie_buf,
1201                                                   &realtek_ie_len);
1202                }
1203        }
1204
1205        if (beacon->bCkipSupported)
1206                ckip_ie_len = 30+2;
1207        if (beacon->bCcxRmEnable)
1208                ccxrm_ie_len = 6+2;
1209        if (beacon->BssCcxVerNumber >= 2)
1210                cxvernum_ie_len = 5+2;
1211
1212        PMKCacheIdx = SecIsInPMKIDList(ieee, ieee->current_network.bssid);
1213        if (PMKCacheIdx >= 0) {
1214                wpa_ie_len += 18;
1215                netdev_info(ieee->dev, "[PMK cache]: WPA2 IE length: %x\n",
1216                            wpa_ie_len);
1217        }
1218        len = sizeof(struct rtllib_assoc_request_frame) + 2
1219                + beacon->ssid_len
1220                + rate_len
1221                + wpa_ie_len
1222                + wps_ie_len
1223                + wmm_info_len
1224                + turbo_info_len
1225                + ht_cap_len
1226                + realtek_ie_len
1227                + ckip_ie_len
1228                + ccxrm_ie_len
1229                + cxvernum_ie_len
1230                + ieee->tx_headroom;
1231
1232        skb = dev_alloc_skb(len);
1233
1234        if (!skb)
1235                return NULL;
1236
1237        skb_reserve(skb, ieee->tx_headroom);
1238
1239        hdr = skb_put(skb, sizeof(struct rtllib_assoc_request_frame) + 2);
1240
1241
1242        hdr->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_ASSOC_REQ);
1243        hdr->header.duration_id = cpu_to_le16(37);
1244        ether_addr_copy(hdr->header.addr1, beacon->bssid);
1245        ether_addr_copy(hdr->header.addr2, ieee->dev->dev_addr);
1246        ether_addr_copy(hdr->header.addr3, beacon->bssid);
1247
1248        ether_addr_copy(ieee->ap_mac_addr, beacon->bssid);
1249
1250        hdr->capability = cpu_to_le16(WLAN_CAPABILITY_ESS);
1251        if (beacon->capability & WLAN_CAPABILITY_PRIVACY)
1252                hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1253
1254        if (beacon->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1255                hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
1256
1257        if (ieee->short_slot &&
1258           (beacon->capability&WLAN_CAPABILITY_SHORT_SLOT_TIME))
1259                hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
1260
1261
1262        hdr->listen_interval = cpu_to_le16(beacon->listen_interval);
1263
1264        hdr->info_element[0].id = MFIE_TYPE_SSID;
1265
1266        hdr->info_element[0].len = beacon->ssid_len;
1267        skb_put_data(skb, beacon->ssid, beacon->ssid_len);
1268
1269        tag = skb_put(skb, rate_len);
1270
1271        if (beacon->rates_len) {
1272                *tag++ = MFIE_TYPE_RATES;
1273                *tag++ = beacon->rates_len;
1274                for (i = 0; i < beacon->rates_len; i++)
1275                        *tag++ = beacon->rates[i];
1276        }
1277
1278        if (beacon->rates_ex_len) {
1279                *tag++ = MFIE_TYPE_RATES_EX;
1280                *tag++ = beacon->rates_ex_len;
1281                for (i = 0; i < beacon->rates_ex_len; i++)
1282                        *tag++ = beacon->rates_ex[i];
1283        }
1284
1285        if (beacon->bCkipSupported) {
1286                static const u8 AironetIeOui[] = {0x00, 0x01, 0x66};
1287                u8      CcxAironetBuf[30];
1288                struct octet_string osCcxAironetIE;
1289
1290                memset(CcxAironetBuf, 0, 30);
1291                osCcxAironetIE.Octet = CcxAironetBuf;
1292                osCcxAironetIE.Length = sizeof(CcxAironetBuf);
1293                memcpy(osCcxAironetIE.Octet, AironetIeOui,
1294                       sizeof(AironetIeOui));
1295
1296                osCcxAironetIE.Octet[IE_CISCO_FLAG_POSITION] |=
1297                                         (SUPPORT_CKIP_PK|SUPPORT_CKIP_MIC);
1298                tag = skb_put(skb, ckip_ie_len);
1299                *tag++ = MFIE_TYPE_AIRONET;
1300                *tag++ = osCcxAironetIE.Length;
1301                memcpy(tag, osCcxAironetIE.Octet, osCcxAironetIE.Length);
1302                tag += osCcxAironetIE.Length;
1303        }
1304
1305        if (beacon->bCcxRmEnable) {
1306                static const u8 CcxRmCapBuf[] = {0x00, 0x40, 0x96, 0x01, 0x01,
1307                        0x00};
1308                struct octet_string osCcxRmCap;
1309
1310                osCcxRmCap.Octet = (u8 *) CcxRmCapBuf;
1311                osCcxRmCap.Length = sizeof(CcxRmCapBuf);
1312                tag = skb_put(skb, ccxrm_ie_len);
1313                *tag++ = MFIE_TYPE_GENERIC;
1314                *tag++ = osCcxRmCap.Length;
1315                memcpy(tag, osCcxRmCap.Octet, osCcxRmCap.Length);
1316                tag += osCcxRmCap.Length;
1317        }
1318
1319        if (beacon->BssCcxVerNumber >= 2) {
1320                u8 CcxVerNumBuf[] = {0x00, 0x40, 0x96, 0x03, 0x00};
1321                struct octet_string osCcxVerNum;
1322
1323                CcxVerNumBuf[4] = beacon->BssCcxVerNumber;
1324                osCcxVerNum.Octet = CcxVerNumBuf;
1325                osCcxVerNum.Length = sizeof(CcxVerNumBuf);
1326                tag = skb_put(skb, cxvernum_ie_len);
1327                *tag++ = MFIE_TYPE_GENERIC;
1328                *tag++ = osCcxVerNum.Length;
1329                memcpy(tag, osCcxVerNum.Octet, osCcxVerNum.Length);
1330                tag += osCcxVerNum.Length;
1331        }
1332        if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1333                if (ieee->pHTInfo->ePeerHTSpecVer != HT_SPEC_VER_EWC) {
1334                        tag = skb_put(skb, ht_cap_len);
1335                        *tag++ = MFIE_TYPE_HT_CAP;
1336                        *tag++ = ht_cap_len - 2;
1337                        memcpy(tag, ht_cap_buf, ht_cap_len - 2);
1338                        tag += ht_cap_len - 2;
1339                }
1340        }
1341
1342        if (wpa_ie_len) {
1343                skb_put_data(skb, ieee->wpa_ie, ieee->wpa_ie_len);
1344
1345                if (PMKCacheIdx >= 0) {
1346                        tag = skb_put(skb, 18);
1347                        *tag = 1;
1348                        *(tag + 1) = 0;
1349                        memcpy((tag + 2), &ieee->PMKIDList[PMKCacheIdx].PMKID,
1350                               16);
1351                }
1352        }
1353        if (wmm_info_len) {
1354                tag = skb_put(skb, wmm_info_len);
1355                rtllib_WMM_Info(ieee, &tag);
1356        }
1357
1358        if (wps_ie_len && ieee->wps_ie) {
1359                skb_put_data(skb, ieee->wps_ie, wps_ie_len);
1360        }
1361
1362        if (turbo_info_len) {
1363                tag = skb_put(skb, turbo_info_len);
1364                rtllib_TURBO_Info(ieee, &tag);
1365        }
1366
1367        if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1368                if (ieee->pHTInfo->ePeerHTSpecVer == HT_SPEC_VER_EWC) {
1369                        tag = skb_put(skb, ht_cap_len);
1370                        *tag++ = MFIE_TYPE_GENERIC;
1371                        *tag++ = ht_cap_len - 2;
1372                        memcpy(tag, ht_cap_buf, ht_cap_len - 2);
1373                        tag += ht_cap_len - 2;
1374                }
1375
1376                if (ieee->pHTInfo->bCurrentRT2RTAggregation) {
1377                        tag = skb_put(skb, realtek_ie_len);
1378                        *tag++ = MFIE_TYPE_GENERIC;
1379                        *tag++ = realtek_ie_len - 2;
1380                        memcpy(tag, realtek_ie_buf, realtek_ie_len - 2);
1381                }
1382        }
1383
1384        kfree(ieee->assocreq_ies);
1385        ieee->assocreq_ies = NULL;
1386        ies = &(hdr->info_element[0].id);
1387        ieee->assocreq_ies_len = (skb->data + skb->len) - ies;
1388        ieee->assocreq_ies = kmalloc(ieee->assocreq_ies_len, GFP_ATOMIC);
1389        if (ieee->assocreq_ies)
1390                memcpy(ieee->assocreq_ies, ies, ieee->assocreq_ies_len);
1391        else {
1392                netdev_info(ieee->dev,
1393                            "%s()Warning: can't alloc memory for assocreq_ies\n",
1394                            __func__);
1395                ieee->assocreq_ies_len = 0;
1396        }
1397        return skb;
1398}
1399
1400static void rtllib_associate_abort(struct rtllib_device *ieee)
1401{
1402        unsigned long flags;
1403
1404        spin_lock_irqsave(&ieee->lock, flags);
1405
1406        ieee->associate_seq++;
1407
1408        /* don't scan, and avoid to have the RX path possibily
1409         * try again to associate. Even do not react to AUTH or
1410         * ASSOC response. Just wait for the retry wq to be scheduled.
1411         * Here we will check if there are good nets to associate
1412         * with, so we retry or just get back to NO_LINK and scanning
1413         */
1414        if (ieee->state == RTLLIB_ASSOCIATING_AUTHENTICATING) {
1415                netdev_dbg(ieee->dev, "Authentication failed\n");
1416                ieee->softmac_stats.no_auth_rs++;
1417        } else {
1418                netdev_dbg(ieee->dev, "Association failed\n");
1419                ieee->softmac_stats.no_ass_rs++;
1420        }
1421
1422        ieee->state = RTLLIB_ASSOCIATING_RETRY;
1423
1424        schedule_delayed_work(&ieee->associate_retry_wq,
1425                              RTLLIB_SOFTMAC_ASSOC_RETRY_TIME);
1426
1427        spin_unlock_irqrestore(&ieee->lock, flags);
1428}
1429
1430static void rtllib_associate_abort_cb(struct timer_list *t)
1431{
1432        struct rtllib_device *dev = from_timer(dev, t, associate_timer);
1433
1434        rtllib_associate_abort(dev);
1435}
1436
1437static void rtllib_associate_step1(struct rtllib_device *ieee, u8 *daddr)
1438{
1439        struct rtllib_network *beacon = &ieee->current_network;
1440        struct sk_buff *skb;
1441
1442        netdev_dbg(ieee->dev, "Stopping scan\n");
1443
1444        ieee->softmac_stats.tx_auth_rq++;
1445
1446        skb = rtllib_authentication_req(beacon, ieee, 0, daddr);
1447
1448        if (!skb)
1449                rtllib_associate_abort(ieee);
1450        else {
1451                ieee->state = RTLLIB_ASSOCIATING_AUTHENTICATING;
1452                netdev_dbg(ieee->dev, "Sending authentication request\n");
1453                softmac_mgmt_xmit(skb, ieee);
1454                if (!timer_pending(&ieee->associate_timer)) {
1455                        ieee->associate_timer.expires = jiffies + (HZ / 2);
1456                        add_timer(&ieee->associate_timer);
1457                }
1458        }
1459}
1460
1461static void rtllib_auth_challenge(struct rtllib_device *ieee, u8 *challenge,
1462                                  int chlen)
1463{
1464        u8 *c;
1465        struct sk_buff *skb;
1466        struct rtllib_network *beacon = &ieee->current_network;
1467
1468        ieee->associate_seq++;
1469        ieee->softmac_stats.tx_auth_rq++;
1470
1471        skb = rtllib_authentication_req(beacon, ieee, chlen + 2, beacon->bssid);
1472
1473        if (!skb)
1474                rtllib_associate_abort(ieee);
1475        else {
1476                c = skb_put(skb, chlen+2);
1477                *(c++) = MFIE_TYPE_CHALLENGE;
1478                *(c++) = chlen;
1479                memcpy(c, challenge, chlen);
1480
1481                netdev_dbg(ieee->dev,
1482                           "Sending authentication challenge response\n");
1483
1484                rtllib_encrypt_fragment(ieee, skb,
1485                                        sizeof(struct rtllib_hdr_3addr));
1486
1487                softmac_mgmt_xmit(skb, ieee);
1488                mod_timer(&ieee->associate_timer, jiffies + (HZ/2));
1489        }
1490        kfree(challenge);
1491}
1492
1493static void rtllib_associate_step2(struct rtllib_device *ieee)
1494{
1495        struct sk_buff *skb;
1496        struct rtllib_network *beacon = &ieee->current_network;
1497
1498        del_timer_sync(&ieee->associate_timer);
1499
1500        netdev_dbg(ieee->dev, "Sending association request\n");
1501
1502        ieee->softmac_stats.tx_ass_rq++;
1503        skb = rtllib_association_req(beacon, ieee);
1504        if (!skb)
1505                rtllib_associate_abort(ieee);
1506        else {
1507                softmac_mgmt_xmit(skb, ieee);
1508                mod_timer(&ieee->associate_timer, jiffies + (HZ/2));
1509        }
1510}
1511
1512static void rtllib_associate_complete_wq(void *data)
1513{
1514        struct rtllib_device *ieee = (struct rtllib_device *)
1515                                     container_of_work_rsl(data,
1516                                     struct rtllib_device,
1517                                     associate_complete_wq);
1518        struct rt_pwr_save_ctrl *pPSC = &(ieee->PowerSaveControl);
1519
1520        netdev_info(ieee->dev, "Associated successfully with %pM\n",
1521                    ieee->current_network.bssid);
1522        if (!ieee->is_silent_reset) {
1523                netdev_info(ieee->dev, "normal associate\n");
1524                notify_wx_assoc_event(ieee);
1525        }
1526
1527        netif_carrier_on(ieee->dev);
1528        ieee->is_roaming = false;
1529        if (rtllib_is_54g(&ieee->current_network) &&
1530           (ieee->modulation & RTLLIB_OFDM_MODULATION)) {
1531                ieee->rate = 108;
1532                netdev_info(ieee->dev, "Using G rates:%d\n", ieee->rate);
1533        } else {
1534                ieee->rate = 22;
1535                ieee->SetWirelessMode(ieee->dev, IEEE_B);
1536                netdev_info(ieee->dev, "Using B rates:%d\n", ieee->rate);
1537        }
1538        if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1539                netdev_info(ieee->dev, "Successfully associated, ht enabled\n");
1540                HTOnAssocRsp(ieee);
1541        } else {
1542                netdev_info(ieee->dev,
1543                            "Successfully associated, ht not enabled(%d, %d)\n",
1544                            ieee->pHTInfo->bCurrentHTSupport,
1545                            ieee->pHTInfo->bEnableHT);
1546                memset(ieee->dot11HTOperationalRateSet, 0, 16);
1547        }
1548        ieee->LinkDetectInfo.SlotNum = 2 * (1 +
1549                                       ieee->current_network.beacon_interval /
1550                                       500);
1551        if (ieee->LinkDetectInfo.NumRecvBcnInPeriod == 0 ||
1552            ieee->LinkDetectInfo.NumRecvDataInPeriod == 0) {
1553                ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
1554                ieee->LinkDetectInfo.NumRecvDataInPeriod = 1;
1555        }
1556        pPSC->LpsIdleCount = 0;
1557        ieee->link_change(ieee->dev);
1558
1559        if (ieee->is_silent_reset) {
1560                netdev_info(ieee->dev, "silent reset associate\n");
1561                ieee->is_silent_reset = false;
1562        }
1563
1564        if (ieee->data_hard_resume)
1565                ieee->data_hard_resume(ieee->dev);
1566
1567}
1568
1569static void rtllib_sta_send_associnfo(struct rtllib_device *ieee)
1570{
1571}
1572
1573static void rtllib_associate_complete(struct rtllib_device *ieee)
1574{
1575        del_timer_sync(&ieee->associate_timer);
1576
1577        ieee->state = RTLLIB_LINKED;
1578        rtllib_sta_send_associnfo(ieee);
1579
1580        schedule_work(&ieee->associate_complete_wq);
1581}
1582
1583static void rtllib_associate_procedure_wq(void *data)
1584{
1585        struct rtllib_device *ieee = container_of_dwork_rsl(data,
1586                                     struct rtllib_device,
1587                                     associate_procedure_wq);
1588        rtllib_stop_scan_syncro(ieee);
1589        if (ieee->rtllib_ips_leave != NULL)
1590                ieee->rtllib_ips_leave(ieee->dev);
1591        mutex_lock(&ieee->wx_mutex);
1592
1593        if (ieee->data_hard_stop)
1594                ieee->data_hard_stop(ieee->dev);
1595
1596        rtllib_stop_scan(ieee);
1597        RT_TRACE(COMP_DBG, "===>%s(), chan:%d\n", __func__,
1598                 ieee->current_network.channel);
1599        HTSetConnectBwMode(ieee, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
1600        if (ieee->eRFPowerState == eRfOff) {
1601                RT_TRACE(COMP_DBG,
1602                         "=============>%s():Rf state is eRfOff, schedule ipsleave wq again,return\n",
1603                         __func__);
1604                if (ieee->rtllib_ips_leave_wq != NULL)
1605                        ieee->rtllib_ips_leave_wq(ieee->dev);
1606                mutex_unlock(&ieee->wx_mutex);
1607                return;
1608        }
1609        ieee->associate_seq = 1;
1610
1611        rtllib_associate_step1(ieee, ieee->current_network.bssid);
1612
1613        mutex_unlock(&ieee->wx_mutex);
1614}
1615
1616inline void rtllib_softmac_new_net(struct rtllib_device *ieee,
1617                                   struct rtllib_network *net)
1618{
1619        u8 tmp_ssid[IW_ESSID_MAX_SIZE + 1];
1620        int tmp_ssid_len = 0;
1621
1622        short apset, ssidset, ssidbroad, apmatch, ssidmatch;
1623
1624        /* we are interested in new new only if we are not associated
1625         * and we are not associating / authenticating
1626         */
1627        if (ieee->state != RTLLIB_NOLINK)
1628                return;
1629
1630        if ((ieee->iw_mode == IW_MODE_INFRA) && !(net->capability &
1631            WLAN_CAPABILITY_ESS))
1632                return;
1633
1634        if ((ieee->iw_mode == IW_MODE_ADHOC) && !(net->capability &
1635             WLAN_CAPABILITY_IBSS))
1636                return;
1637
1638        if ((ieee->iw_mode == IW_MODE_ADHOC) &&
1639            (net->channel > ieee->ibss_maxjoin_chal))
1640                return;
1641        if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC) {
1642                /* if the user specified the AP MAC, we need also the essid
1643                 * This could be obtained by beacons or, if the network does not
1644                 * broadcast it, it can be put manually.
1645                 */
1646                apset = ieee->wap_set;
1647                ssidset = ieee->ssid_set;
1648                ssidbroad =  !(net->ssid_len == 0 || net->ssid[0] == '\0');
1649                apmatch = (memcmp(ieee->current_network.bssid, net->bssid,
1650                                  ETH_ALEN) == 0);
1651                if (!ssidbroad) {
1652                        ssidmatch = (ieee->current_network.ssid_len ==
1653                                    net->hidden_ssid_len) &&
1654                                    (!strncmp(ieee->current_network.ssid,
1655                                    net->hidden_ssid, net->hidden_ssid_len));
1656                        if (net->hidden_ssid_len > 0) {
1657                                strncpy(net->ssid, net->hidden_ssid,
1658                                        net->hidden_ssid_len);
1659                                net->ssid_len = net->hidden_ssid_len;
1660                                ssidbroad = 1;
1661                        }
1662                } else
1663                        ssidmatch =
1664                           (ieee->current_network.ssid_len == net->ssid_len) &&
1665                           (!strncmp(ieee->current_network.ssid, net->ssid,
1666                           net->ssid_len));
1667
1668                /* if the user set the AP check if match.
1669                 * if the network does not broadcast essid we check the
1670                 *       user supplied ANY essid
1671                 * if the network does broadcast and the user does not set
1672                 *       essid it is OK
1673                 * if the network does broadcast and the user did set essid
1674                 * check if essid match
1675                 * if the ap is not set, check that the user set the bssid
1676                 * and the network does broadcast and that those two bssid match
1677                 */
1678                if ((apset && apmatch &&
1679                   ((ssidset && ssidbroad && ssidmatch) ||
1680                   (ssidbroad && !ssidset) || (!ssidbroad && ssidset))) ||
1681                   (!apset && ssidset && ssidbroad && ssidmatch) ||
1682                   (ieee->is_roaming && ssidset && ssidbroad && ssidmatch)) {
1683                        /* Save the essid so that if it is hidden, it is
1684                         * replaced with the essid provided by the user.
1685                         */
1686                        if (!ssidbroad) {
1687                                memcpy(tmp_ssid, ieee->current_network.ssid,
1688                                       ieee->current_network.ssid_len);
1689                                tmp_ssid_len = ieee->current_network.ssid_len;
1690                        }
1691                        memcpy(&ieee->current_network, net,
1692                                sizeof(ieee->current_network));
1693                        if (!ssidbroad) {
1694                                memcpy(ieee->current_network.ssid, tmp_ssid,
1695                                       tmp_ssid_len);
1696                                ieee->current_network.ssid_len = tmp_ssid_len;
1697                        }
1698                        netdev_info(ieee->dev,
1699                                    "Linking with %s,channel:%d, qos:%d, myHT:%d, networkHT:%d, mode:%x cur_net.flags:0x%x\n",
1700                                    ieee->current_network.ssid,
1701                                    ieee->current_network.channel,
1702                                    ieee->current_network.qos_data.supported,
1703                                    ieee->pHTInfo->bEnableHT,
1704                                    ieee->current_network.bssht.bdSupportHT,
1705                                    ieee->current_network.mode,
1706                                    ieee->current_network.flags);
1707
1708                        if ((rtllib_act_scanning(ieee, false)) &&
1709                           !(ieee->softmac_features & IEEE_SOFTMAC_SCAN))
1710                                rtllib_stop_scan_syncro(ieee);
1711
1712                        HTResetIOTSetting(ieee->pHTInfo);
1713                        ieee->wmm_acm = 0;
1714                        if (ieee->iw_mode == IW_MODE_INFRA) {
1715                                /* Join the network for the first time */
1716                                ieee->AsocRetryCount = 0;
1717                                if ((ieee->current_network.qos_data.supported == 1) &&
1718                                    ieee->current_network.bssht.bdSupportHT)
1719                                        HTResetSelfAndSavePeerSetting(ieee,
1720                                                 &(ieee->current_network));
1721                                else
1722                                        ieee->pHTInfo->bCurrentHTSupport =
1723                                                                 false;
1724
1725                                ieee->state = RTLLIB_ASSOCIATING;
1726                                if (ieee->LedControlHandler != NULL)
1727                                        ieee->LedControlHandler(ieee->dev,
1728                                                         LED_CTL_START_TO_LINK);
1729                                schedule_delayed_work(
1730                                           &ieee->associate_procedure_wq, 0);
1731                        } else {
1732                                if (rtllib_is_54g(&ieee->current_network) &&
1733                                    (ieee->modulation &
1734                                     RTLLIB_OFDM_MODULATION)) {
1735                                        ieee->rate = 108;
1736                                        ieee->SetWirelessMode(ieee->dev,
1737                                                              IEEE_G);
1738                                        netdev_info(ieee->dev,
1739                                                    "Using G rates\n");
1740                                } else {
1741                                        ieee->rate = 22;
1742                                        ieee->SetWirelessMode(ieee->dev,
1743                                                              IEEE_B);
1744                                        netdev_info(ieee->dev,
1745                                                    "Using B rates\n");
1746                                }
1747                                memset(ieee->dot11HTOperationalRateSet, 0, 16);
1748                                ieee->state = RTLLIB_LINKED;
1749                        }
1750                }
1751        }
1752}
1753
1754static void rtllib_softmac_check_all_nets(struct rtllib_device *ieee)
1755{
1756        unsigned long flags;
1757        struct rtllib_network *target;
1758
1759        spin_lock_irqsave(&ieee->lock, flags);
1760
1761        list_for_each_entry(target, &ieee->network_list, list) {
1762
1763                /* if the state become different that NOLINK means
1764                 * we had found what we are searching for
1765                 */
1766
1767                if (ieee->state != RTLLIB_NOLINK)
1768                        break;
1769
1770                if (ieee->scan_age == 0 || time_after(target->last_scanned +
1771                    ieee->scan_age, jiffies))
1772                        rtllib_softmac_new_net(ieee, target);
1773        }
1774        spin_unlock_irqrestore(&ieee->lock, flags);
1775}
1776
1777static inline u16 auth_parse(struct net_device *dev, struct sk_buff *skb,
1778                             u8 **challenge, int *chlen)
1779{
1780        struct rtllib_authentication *a;
1781        u8 *t;
1782
1783        if (skb->len <  (sizeof(struct rtllib_authentication) -
1784            sizeof(struct rtllib_info_element))) {
1785                netdev_dbg(dev, "invalid len in auth resp: %d\n", skb->len);
1786                return 0xcafe;
1787        }
1788        *challenge = NULL;
1789        a = (struct rtllib_authentication *) skb->data;
1790        if (skb->len > (sizeof(struct rtllib_authentication) + 3)) {
1791                t = skb->data + sizeof(struct rtllib_authentication);
1792
1793                if (*(t++) == MFIE_TYPE_CHALLENGE) {
1794                        *chlen = *(t++);
1795                        *challenge = kmemdup(t, *chlen, GFP_ATOMIC);
1796                        if (!*challenge)
1797                                return -ENOMEM;
1798                }
1799        }
1800        return le16_to_cpu(a->status);
1801}
1802
1803static int auth_rq_parse(struct net_device *dev, struct sk_buff *skb, u8 *dest)
1804{
1805        struct rtllib_authentication *a;
1806
1807        if (skb->len <  (sizeof(struct rtllib_authentication) -
1808            sizeof(struct rtllib_info_element))) {
1809                netdev_dbg(dev, "invalid len in auth request: %d\n", skb->len);
1810                return -1;
1811        }
1812        a = (struct rtllib_authentication *) skb->data;
1813
1814        ether_addr_copy(dest, a->header.addr2);
1815
1816        if (le16_to_cpu(a->algorithm) != WLAN_AUTH_OPEN)
1817                return  WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1818
1819        return WLAN_STATUS_SUCCESS;
1820}
1821
1822static short probe_rq_parse(struct rtllib_device *ieee, struct sk_buff *skb,
1823                            u8 *src)
1824{
1825        u8 *tag;
1826        u8 *skbend;
1827        u8 *ssid = NULL;
1828        u8 ssidlen = 0;
1829        struct rtllib_hdr_3addr   *header =
1830                (struct rtllib_hdr_3addr   *) skb->data;
1831        bool bssid_match;
1832
1833        if (skb->len < sizeof(struct rtllib_hdr_3addr))
1834                return -1; /* corrupted */
1835
1836        bssid_match =
1837          (!ether_addr_equal(header->addr3, ieee->current_network.bssid)) &&
1838          (!is_broadcast_ether_addr(header->addr3));
1839        if (bssid_match)
1840                return -1;
1841
1842        ether_addr_copy(src, header->addr2);
1843
1844        skbend = (u8 *)skb->data + skb->len;
1845
1846        tag = skb->data + sizeof(struct rtllib_hdr_3addr);
1847
1848        while (tag + 1 < skbend) {
1849                if (*tag == 0) {
1850                        ssid = tag + 2;
1851                        ssidlen = *(tag + 1);
1852                        break;
1853                }
1854                tag++; /* point to the len field */
1855                tag = tag + *(tag); /* point to the last data byte of the tag */
1856                tag++; /* point to the next tag */
1857        }
1858
1859        if (ssidlen == 0)
1860                return 1;
1861
1862        if (!ssid)
1863                return 1; /* ssid not found in tagged param */
1864
1865        return !strncmp(ssid, ieee->current_network.ssid, ssidlen);
1866}
1867
1868static int assoc_rq_parse(struct net_device *dev, struct sk_buff *skb, u8 *dest)
1869{
1870        struct rtllib_assoc_request_frame *a;
1871
1872        if (skb->len < (sizeof(struct rtllib_assoc_request_frame) -
1873                sizeof(struct rtllib_info_element))) {
1874                netdev_dbg(dev, "invalid len in auth request:%d\n", skb->len);
1875                return -1;
1876        }
1877
1878        a = (struct rtllib_assoc_request_frame *) skb->data;
1879
1880        ether_addr_copy(dest, a->header.addr2);
1881
1882        return 0;
1883}
1884
1885static inline u16 assoc_parse(struct rtllib_device *ieee, struct sk_buff *skb,
1886                              int *aid)
1887{
1888        struct rtllib_assoc_response_frame *response_head;
1889        u16 status_code;
1890
1891        if (skb->len <  sizeof(struct rtllib_assoc_response_frame)) {
1892                netdev_dbg(ieee->dev, "Invalid len in auth resp: %d\n",
1893                           skb->len);
1894                return 0xcafe;
1895        }
1896
1897        response_head = (struct rtllib_assoc_response_frame *) skb->data;
1898        *aid = le16_to_cpu(response_head->aid) & 0x3fff;
1899
1900        status_code = le16_to_cpu(response_head->status);
1901        if ((status_code == WLAN_STATUS_ASSOC_DENIED_RATES ||
1902           status_code == WLAN_STATUS_CAPS_UNSUPPORTED) &&
1903           ((ieee->mode == IEEE_G) &&
1904           (ieee->current_network.mode == IEEE_N_24G) &&
1905           (ieee->AsocRetryCount++ < (RT_ASOC_RETRY_LIMIT-1)))) {
1906                ieee->pHTInfo->IOTAction |= HT_IOT_ACT_PURE_N_MODE;
1907        } else {
1908                ieee->AsocRetryCount = 0;
1909        }
1910
1911        return le16_to_cpu(response_head->status);
1912}
1913
1914void rtllib_rx_probe_rq(struct rtllib_device *ieee, struct sk_buff *skb)
1915{
1916        u8 dest[ETH_ALEN];
1917
1918        ieee->softmac_stats.rx_probe_rq++;
1919        if (probe_rq_parse(ieee, skb, dest) > 0) {
1920                ieee->softmac_stats.tx_probe_rs++;
1921                rtllib_resp_to_probe(ieee, dest);
1922        }
1923}
1924
1925static inline void rtllib_rx_auth_rq(struct rtllib_device *ieee,
1926                                     struct sk_buff *skb)
1927{
1928        u8 dest[ETH_ALEN];
1929        int status;
1930
1931        ieee->softmac_stats.rx_auth_rq++;
1932
1933        status = auth_rq_parse(ieee->dev, skb, dest);
1934        if (status != -1)
1935                rtllib_resp_to_auth(ieee, status, dest);
1936}
1937
1938static inline void rtllib_rx_assoc_rq(struct rtllib_device *ieee,
1939                                      struct sk_buff *skb)
1940{
1941        u8 dest[ETH_ALEN];
1942
1943
1944        ieee->softmac_stats.rx_ass_rq++;
1945        if (assoc_rq_parse(ieee->dev, skb, dest) != -1)
1946                rtllib_resp_to_assoc_rq(ieee, dest);
1947
1948        netdev_info(ieee->dev, "New client associated: %pM\n", dest);
1949}
1950
1951void rtllib_sta_ps_send_null_frame(struct rtllib_device *ieee, short pwr)
1952{
1953
1954        struct sk_buff *buf = rtllib_null_func(ieee, pwr);
1955
1956        if (buf)
1957                softmac_ps_mgmt_xmit(buf, ieee);
1958}
1959EXPORT_SYMBOL(rtllib_sta_ps_send_null_frame);
1960
1961void rtllib_sta_ps_send_pspoll_frame(struct rtllib_device *ieee)
1962{
1963        struct sk_buff *buf = rtllib_pspoll_func(ieee);
1964
1965        if (buf)
1966                softmac_ps_mgmt_xmit(buf, ieee);
1967}
1968
1969static short rtllib_sta_ps_sleep(struct rtllib_device *ieee, u64 *time)
1970{
1971        int timeout;
1972        u8 dtim;
1973        struct rt_pwr_save_ctrl *pPSC = &(ieee->PowerSaveControl);
1974
1975        if (ieee->LPSDelayCnt) {
1976                ieee->LPSDelayCnt--;
1977                return 0;
1978        }
1979
1980        dtim = ieee->current_network.dtim_data;
1981        if (!(dtim & RTLLIB_DTIM_VALID))
1982                return 0;
1983        timeout = ieee->current_network.beacon_interval;
1984        ieee->current_network.dtim_data = RTLLIB_DTIM_INVALID;
1985        /* there's no need to nofity AP that I find you buffered
1986         * with broadcast packet
1987         */
1988        if (dtim & (RTLLIB_DTIM_UCAST & ieee->ps))
1989                return 2;
1990
1991        if (!time_after(jiffies,
1992                        dev_trans_start(ieee->dev) + msecs_to_jiffies(timeout)))
1993                return 0;
1994        if (!time_after(jiffies,
1995                        ieee->last_rx_ps_time + msecs_to_jiffies(timeout)))
1996                return 0;
1997        if ((ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE) &&
1998            (ieee->mgmt_queue_tail != ieee->mgmt_queue_head))
1999                return 0;
2000
2001        if (time) {
2002                if (ieee->bAwakePktSent) {
2003                        pPSC->LPSAwakeIntvl = 1;
2004                } else {
2005                        u8 MaxPeriod = 1;
2006
2007                        if (pPSC->LPSAwakeIntvl == 0)
2008                                pPSC->LPSAwakeIntvl = 1;
2009                        if (pPSC->RegMaxLPSAwakeIntvl == 0)
2010                                MaxPeriod = 1;
2011                        else if (pPSC->RegMaxLPSAwakeIntvl == 0xFF)
2012                                MaxPeriod = ieee->current_network.dtim_period;
2013                        else
2014                                MaxPeriod = pPSC->RegMaxLPSAwakeIntvl;
2015                        pPSC->LPSAwakeIntvl = (pPSC->LPSAwakeIntvl >=
2016                                               MaxPeriod) ? MaxPeriod :
2017                                               (pPSC->LPSAwakeIntvl + 1);
2018                }
2019                {
2020                        u8 LPSAwakeIntvl_tmp = 0;
2021                        u8 period = ieee->current_network.dtim_period;
2022                        u8 count = ieee->current_network.tim.tim_count;
2023
2024                        if (count == 0) {
2025                                if (pPSC->LPSAwakeIntvl > period)
2026                                        LPSAwakeIntvl_tmp = period +
2027                                                 (pPSC->LPSAwakeIntvl -
2028                                                 period) -
2029                                                 ((pPSC->LPSAwakeIntvl-period) %
2030                                                 period);
2031                                else
2032                                        LPSAwakeIntvl_tmp = pPSC->LPSAwakeIntvl;
2033
2034                        } else {
2035                                if (pPSC->LPSAwakeIntvl >
2036                                    ieee->current_network.tim.tim_count)
2037                                        LPSAwakeIntvl_tmp = count +
2038                                        (pPSC->LPSAwakeIntvl - count) -
2039                                        ((pPSC->LPSAwakeIntvl-count)%period);
2040                                else
2041                                        LPSAwakeIntvl_tmp = pPSC->LPSAwakeIntvl;
2042                        }
2043
2044                *time = ieee->current_network.last_dtim_sta_time
2045                        + msecs_to_jiffies(ieee->current_network.beacon_interval *
2046                        LPSAwakeIntvl_tmp);
2047        }
2048        }
2049
2050        return 1;
2051
2052
2053}
2054
2055static inline void rtllib_sta_ps(struct rtllib_device *ieee)
2056{
2057        u64 time;
2058        short sleep;
2059        unsigned long flags, flags2;
2060
2061        spin_lock_irqsave(&ieee->lock, flags);
2062
2063        if ((ieee->ps == RTLLIB_PS_DISABLED ||
2064             ieee->iw_mode != IW_MODE_INFRA ||
2065             ieee->state != RTLLIB_LINKED)) {
2066                RT_TRACE(COMP_DBG,
2067                         "=====>%s(): no need to ps,wake up!! ieee->ps is %d, ieee->iw_mode is %d, ieee->state is %d\n",
2068                         __func__, ieee->ps, ieee->iw_mode, ieee->state);
2069                spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2070                rtllib_sta_wakeup(ieee, 1);
2071
2072                spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2073        }
2074        sleep = rtllib_sta_ps_sleep(ieee, &time);
2075        /* 2 wake, 1 sleep, 0 do nothing */
2076        if (sleep == 0)
2077                goto out;
2078        if (sleep == 1) {
2079                if (ieee->sta_sleep == LPS_IS_SLEEP) {
2080                        ieee->enter_sleep_state(ieee->dev, time);
2081                } else if (ieee->sta_sleep == LPS_IS_WAKE) {
2082                        spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2083
2084                        if (ieee->ps_is_queue_empty(ieee->dev)) {
2085                                ieee->sta_sleep = LPS_WAIT_NULL_DATA_SEND;
2086                                ieee->ack_tx_to_ieee = 1;
2087                                rtllib_sta_ps_send_null_frame(ieee, 1);
2088                                ieee->ps_time = time;
2089                        }
2090                        spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2091
2092                }
2093
2094                ieee->bAwakePktSent = false;
2095
2096        } else if (sleep == 2) {
2097                spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2098
2099                rtllib_sta_wakeup(ieee, 1);
2100
2101                spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2102        }
2103
2104out:
2105        spin_unlock_irqrestore(&ieee->lock, flags);
2106
2107}
2108
2109static void rtllib_sta_wakeup(struct rtllib_device *ieee, short nl)
2110{
2111        if (ieee->sta_sleep == LPS_IS_WAKE) {
2112                if (nl) {
2113                        if (ieee->pHTInfo->IOTAction &
2114                            HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
2115                                ieee->ack_tx_to_ieee = 1;
2116                                rtllib_sta_ps_send_null_frame(ieee, 0);
2117                        } else {
2118                                ieee->ack_tx_to_ieee = 1;
2119                                rtllib_sta_ps_send_pspoll_frame(ieee);
2120                        }
2121                }
2122                return;
2123
2124        }
2125
2126        if (ieee->sta_sleep == LPS_IS_SLEEP)
2127                ieee->sta_wake_up(ieee->dev);
2128        if (nl) {
2129                if (ieee->pHTInfo->IOTAction &
2130                    HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
2131                        ieee->ack_tx_to_ieee = 1;
2132                        rtllib_sta_ps_send_null_frame(ieee, 0);
2133                } else {
2134                        ieee->ack_tx_to_ieee = 1;
2135                        ieee->polling = true;
2136                        rtllib_sta_ps_send_pspoll_frame(ieee);
2137                }
2138
2139        } else {
2140                ieee->sta_sleep = LPS_IS_WAKE;
2141                ieee->polling = false;
2142        }
2143}
2144
2145void rtllib_ps_tx_ack(struct rtllib_device *ieee, short success)
2146{
2147        unsigned long flags, flags2;
2148
2149        spin_lock_irqsave(&ieee->lock, flags);
2150
2151        if (ieee->sta_sleep == LPS_WAIT_NULL_DATA_SEND) {
2152                /* Null frame with PS bit set */
2153                if (success) {
2154                        ieee->sta_sleep = LPS_IS_SLEEP;
2155                        ieee->enter_sleep_state(ieee->dev, ieee->ps_time);
2156                }
2157                /* if the card report not success we can't be sure the AP
2158                 * has not RXed so we can't assume the AP believe us awake
2159                 */
2160        } else {/* 21112005 - tx again null without PS bit if lost */
2161
2162                if ((ieee->sta_sleep == LPS_IS_WAKE) && !success) {
2163                        spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2164                        if (ieee->pHTInfo->IOTAction &
2165                            HT_IOT_ACT_NULL_DATA_POWER_SAVING)
2166                                rtllib_sta_ps_send_null_frame(ieee, 0);
2167                        else
2168                                rtllib_sta_ps_send_pspoll_frame(ieee);
2169                        spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2170                }
2171        }
2172        spin_unlock_irqrestore(&ieee->lock, flags);
2173}
2174EXPORT_SYMBOL(rtllib_ps_tx_ack);
2175
2176static void rtllib_process_action(struct rtllib_device *ieee,
2177                                  struct sk_buff *skb)
2178{
2179        struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *) skb->data;
2180        u8 *act = rtllib_get_payload((struct rtllib_hdr *)header);
2181        u8 category = 0;
2182
2183        if (act == NULL) {
2184                netdev_warn(ieee->dev,
2185                            "Error getting payload of action frame\n");
2186                return;
2187        }
2188
2189        category = *act;
2190        act++;
2191        switch (category) {
2192        case ACT_CAT_BA:
2193                switch (*act) {
2194                case ACT_ADDBAREQ:
2195                        rtllib_rx_ADDBAReq(ieee, skb);
2196                        break;
2197                case ACT_ADDBARSP:
2198                        rtllib_rx_ADDBARsp(ieee, skb);
2199                        break;
2200                case ACT_DELBA:
2201                        rtllib_rx_DELBA(ieee, skb);
2202                        break;
2203                }
2204                break;
2205        default:
2206                break;
2207        }
2208}
2209
2210static inline int
2211rtllib_rx_assoc_resp(struct rtllib_device *ieee, struct sk_buff *skb,
2212                     struct rtllib_rx_stats *rx_stats)
2213{
2214        u16 errcode;
2215        int aid;
2216        u8 *ies;
2217        struct rtllib_assoc_response_frame *assoc_resp;
2218        struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *) skb->data;
2219        u16 frame_ctl = le16_to_cpu(header->frame_ctl);
2220
2221        netdev_dbg(ieee->dev, "received [RE]ASSOCIATION RESPONSE (%d)\n",
2222                   WLAN_FC_GET_STYPE(frame_ctl));
2223
2224        if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2225             ieee->state == RTLLIB_ASSOCIATING_AUTHENTICATED &&
2226             (ieee->iw_mode == IW_MODE_INFRA)) {
2227                errcode = assoc_parse(ieee, skb, &aid);
2228                if (!errcode) {
2229                        struct rtllib_network *network =
2230                                 kzalloc(sizeof(struct rtllib_network),
2231                                 GFP_ATOMIC);
2232
2233                        if (!network)
2234                                return 1;
2235                        ieee->state = RTLLIB_LINKED;
2236                        ieee->assoc_id = aid;
2237                        ieee->softmac_stats.rx_ass_ok++;
2238                        /* station support qos */
2239                        /* Let the register setting default with Legacy station */
2240                        assoc_resp = (struct rtllib_assoc_response_frame *)skb->data;
2241                        if (ieee->current_network.qos_data.supported == 1) {
2242                                if (rtllib_parse_info_param(ieee, assoc_resp->info_element,
2243                                                        rx_stats->len - sizeof(*assoc_resp),
2244                                                        network, rx_stats)) {
2245                                        kfree(network);
2246                                        return 1;
2247                                }
2248                                memcpy(ieee->pHTInfo->PeerHTCapBuf,
2249                                       network->bssht.bdHTCapBuf,
2250                                       network->bssht.bdHTCapLen);
2251                                memcpy(ieee->pHTInfo->PeerHTInfoBuf,
2252                                       network->bssht.bdHTInfoBuf,
2253                                       network->bssht.bdHTInfoLen);
2254                                if (ieee->handle_assoc_response != NULL)
2255                                        ieee->handle_assoc_response(ieee->dev,
2256                                                 (struct rtllib_assoc_response_frame *)header,
2257                                                 network);
2258                        }
2259                        kfree(network);
2260
2261                        kfree(ieee->assocresp_ies);
2262                        ieee->assocresp_ies = NULL;
2263                        ies = &(assoc_resp->info_element[0].id);
2264                        ieee->assocresp_ies_len = (skb->data + skb->len) - ies;
2265                        ieee->assocresp_ies = kmalloc(ieee->assocresp_ies_len,
2266                                                      GFP_ATOMIC);
2267                        if (ieee->assocresp_ies)
2268                                memcpy(ieee->assocresp_ies, ies,
2269                                       ieee->assocresp_ies_len);
2270                        else {
2271                                netdev_info(ieee->dev,
2272                                            "%s()Warning: can't alloc memory for assocresp_ies\n",
2273                                            __func__);
2274                                ieee->assocresp_ies_len = 0;
2275                        }
2276                        rtllib_associate_complete(ieee);
2277                } else {
2278                        /* aid could not been allocated */
2279                        ieee->softmac_stats.rx_ass_err++;
2280                        netdev_info(ieee->dev,
2281                                    "Association response status code 0x%x\n",
2282                                    errcode);
2283                        if (ieee->AsocRetryCount < RT_ASOC_RETRY_LIMIT)
2284                                schedule_delayed_work(
2285                                         &ieee->associate_procedure_wq, 0);
2286                        else
2287                                rtllib_associate_abort(ieee);
2288                }
2289        }
2290        return 0;
2291}
2292
2293static void rtllib_rx_auth_resp(struct rtllib_device *ieee, struct sk_buff *skb)
2294{
2295        u16 errcode;
2296        u8 *challenge;
2297        int chlen = 0;
2298        bool bSupportNmode = true, bHalfSupportNmode = false;
2299
2300        errcode = auth_parse(ieee->dev, skb, &challenge, &chlen);
2301
2302        if (errcode) {
2303                ieee->softmac_stats.rx_auth_rs_err++;
2304                netdev_info(ieee->dev,
2305                            "Authentication response status code 0x%x",
2306                            errcode);
2307                rtllib_associate_abort(ieee);
2308                return;
2309        }
2310
2311        if (ieee->open_wep || !challenge) {
2312                ieee->state = RTLLIB_ASSOCIATING_AUTHENTICATED;
2313                ieee->softmac_stats.rx_auth_rs_ok++;
2314                if (!(ieee->pHTInfo->IOTAction & HT_IOT_ACT_PURE_N_MODE)) {
2315                        if (!ieee->GetNmodeSupportBySecCfg(ieee->dev)) {
2316                                if (IsHTHalfNmodeAPs(ieee)) {
2317                                        bSupportNmode = true;
2318                                        bHalfSupportNmode = true;
2319                                } else {
2320                                        bSupportNmode = false;
2321                                        bHalfSupportNmode = false;
2322                                }
2323                        }
2324                }
2325                /* Dummy wirless mode setting to avoid encryption issue */
2326                if (bSupportNmode) {
2327                        ieee->SetWirelessMode(ieee->dev,
2328                                              ieee->current_network.mode);
2329                } else {
2330                        /*TODO*/
2331                        ieee->SetWirelessMode(ieee->dev, IEEE_G);
2332                }
2333
2334                if ((ieee->current_network.mode == IEEE_N_24G) &&
2335                    bHalfSupportNmode) {
2336                        netdev_info(ieee->dev, "======>enter half N mode\n");
2337                        ieee->bHalfWirelessN24GMode = true;
2338                } else {
2339                        ieee->bHalfWirelessN24GMode = false;
2340                }
2341                rtllib_associate_step2(ieee);
2342        } else {
2343                rtllib_auth_challenge(ieee, challenge,  chlen);
2344        }
2345}
2346
2347static inline int
2348rtllib_rx_auth(struct rtllib_device *ieee, struct sk_buff *skb,
2349               struct rtllib_rx_stats *rx_stats)
2350{
2351
2352        if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) {
2353                if (ieee->state == RTLLIB_ASSOCIATING_AUTHENTICATING &&
2354                    (ieee->iw_mode == IW_MODE_INFRA)) {
2355                        netdev_dbg(ieee->dev,
2356                                   "Received authentication response");
2357                        rtllib_rx_auth_resp(ieee, skb);
2358                } else if (ieee->iw_mode == IW_MODE_MASTER) {
2359                        rtllib_rx_auth_rq(ieee, skb);
2360                }
2361        }
2362        return 0;
2363}
2364
2365static inline int
2366rtllib_rx_deauth(struct rtllib_device *ieee, struct sk_buff *skb)
2367{
2368        struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *) skb->data;
2369        u16 frame_ctl;
2370
2371        if (memcmp(header->addr3, ieee->current_network.bssid, ETH_ALEN) != 0)
2372                return 0;
2373
2374        /* FIXME for now repeat all the association procedure
2375         * both for disassociation and deauthentication
2376         */
2377        if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2378            ieee->state == RTLLIB_LINKED &&
2379            (ieee->iw_mode == IW_MODE_INFRA)) {
2380                frame_ctl = le16_to_cpu(header->frame_ctl);
2381                netdev_info(ieee->dev,
2382                            "==========>received disassoc/deauth(%x) frame, reason code:%x\n",
2383                            WLAN_FC_GET_STYPE(frame_ctl),
2384                            ((struct rtllib_disassoc *)skb->data)->reason);
2385                ieee->state = RTLLIB_ASSOCIATING;
2386                ieee->softmac_stats.reassoc++;
2387                ieee->is_roaming = true;
2388                ieee->LinkDetectInfo.bBusyTraffic = false;
2389                rtllib_disassociate(ieee);
2390                RemovePeerTS(ieee, header->addr2);
2391                if (ieee->LedControlHandler != NULL)
2392                        ieee->LedControlHandler(ieee->dev,
2393                                                LED_CTL_START_TO_LINK);
2394
2395                if (!(ieee->rtllib_ap_sec_type(ieee) &
2396                    (SEC_ALG_CCMP|SEC_ALG_TKIP)))
2397                        schedule_delayed_work(
2398                                       &ieee->associate_procedure_wq, 5);
2399        }
2400        return 0;
2401}
2402
2403inline int rtllib_rx_frame_softmac(struct rtllib_device *ieee,
2404                                   struct sk_buff *skb,
2405                                   struct rtllib_rx_stats *rx_stats, u16 type,
2406                                   u16 stype)
2407{
2408        struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *) skb->data;
2409        u16 frame_ctl;
2410
2411        if (!ieee->proto_started)
2412                return 0;
2413
2414        frame_ctl = le16_to_cpu(header->frame_ctl);
2415        switch (WLAN_FC_GET_STYPE(frame_ctl)) {
2416        case RTLLIB_STYPE_ASSOC_RESP:
2417        case RTLLIB_STYPE_REASSOC_RESP:
2418                if (rtllib_rx_assoc_resp(ieee, skb, rx_stats) == 1)
2419                        return 1;
2420                break;
2421        case RTLLIB_STYPE_ASSOC_REQ:
2422        case RTLLIB_STYPE_REASSOC_REQ:
2423                if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2424                     ieee->iw_mode == IW_MODE_MASTER)
2425                        rtllib_rx_assoc_rq(ieee, skb);
2426                break;
2427        case RTLLIB_STYPE_AUTH:
2428                rtllib_rx_auth(ieee, skb, rx_stats);
2429                break;
2430        case RTLLIB_STYPE_DISASSOC:
2431        case RTLLIB_STYPE_DEAUTH:
2432                rtllib_rx_deauth(ieee, skb);
2433                break;
2434        case RTLLIB_STYPE_MANAGE_ACT:
2435                rtllib_process_action(ieee, skb);
2436                break;
2437        default:
2438                return -1;
2439        }
2440        return 0;
2441}
2442
2443/* following are for a simpler TX queue management.
2444 * Instead of using netif_[stop/wake]_queue the driver
2445 * will use these two functions (plus a reset one), that
2446 * will internally use the kernel netif_* and takes
2447 * care of the ieee802.11 fragmentation.
2448 * So the driver receives a fragment per time and might
2449 * call the stop function when it wants to not
2450 * have enough room to TX an entire packet.
2451 * This might be useful if each fragment needs it's own
2452 * descriptor, thus just keep a total free memory > than
2453 * the max fragmentation threshold is not enough.. If the
2454 * ieee802.11 stack passed a TXB struct then you need
2455 * to keep N free descriptors where
2456 * N = MAX_PACKET_SIZE / MIN_FRAG_TRESHOLD
2457 * In this way you need just one and the 802.11 stack
2458 * will take care of buffering fragments and pass them to
2459 * to the driver later, when it wakes the queue.
2460 */
2461void rtllib_softmac_xmit(struct rtllib_txb *txb, struct rtllib_device *ieee)
2462{
2463
2464        unsigned int queue_index = txb->queue_index;
2465        unsigned long flags;
2466        int  i;
2467        struct cb_desc *tcb_desc = NULL;
2468        unsigned long queue_len = 0;
2469
2470        spin_lock_irqsave(&ieee->lock, flags);
2471
2472        /* called with 2nd parm 0, no tx mgmt lock required */
2473        rtllib_sta_wakeup(ieee, 0);
2474
2475        /* update the tx status */
2476        tcb_desc = (struct cb_desc *)(txb->fragments[0]->cb +
2477                   MAX_DEV_ADDR_SIZE);
2478        if (tcb_desc->bMulticast)
2479                ieee->stats.multicast++;
2480
2481        /* if xmit available, just xmit it immediately, else just insert it to
2482         * the wait queue
2483         */
2484        for (i = 0; i < txb->nr_frags; i++) {
2485                queue_len = skb_queue_len(&ieee->skb_waitQ[queue_index]);
2486                if ((queue_len  != 0) ||
2487                    (!ieee->check_nic_enough_desc(ieee->dev, queue_index)) ||
2488                    (ieee->queue_stop)) {
2489                        /* insert the skb packet to the wait queue
2490                         * as for the completion function, it does not need
2491                         * to check it any more.
2492                         */
2493                        if (queue_len < 200)
2494                                skb_queue_tail(&ieee->skb_waitQ[queue_index],
2495                                               txb->fragments[i]);
2496                        else
2497                                kfree_skb(txb->fragments[i]);
2498                } else {
2499                        ieee->softmac_data_hard_start_xmit(
2500                                        txb->fragments[i],
2501                                        ieee->dev, ieee->rate);
2502                }
2503        }
2504
2505        rtllib_txb_free(txb);
2506
2507        spin_unlock_irqrestore(&ieee->lock, flags);
2508
2509}
2510
2511void rtllib_reset_queue(struct rtllib_device *ieee)
2512{
2513        unsigned long flags;
2514
2515        spin_lock_irqsave(&ieee->lock, flags);
2516        init_mgmt_queue(ieee);
2517        if (ieee->tx_pending.txb) {
2518                rtllib_txb_free(ieee->tx_pending.txb);
2519                ieee->tx_pending.txb = NULL;
2520        }
2521        ieee->queue_stop = 0;
2522        spin_unlock_irqrestore(&ieee->lock, flags);
2523
2524}
2525EXPORT_SYMBOL(rtllib_reset_queue);
2526
2527void rtllib_stop_all_queues(struct rtllib_device *ieee)
2528{
2529        unsigned int i;
2530
2531        for (i = 0; i < ieee->dev->num_tx_queues; i++)
2532                netdev_get_tx_queue(ieee->dev, i)->trans_start = jiffies;
2533
2534        netif_tx_stop_all_queues(ieee->dev);
2535}
2536
2537void rtllib_wake_all_queues(struct rtllib_device *ieee)
2538{
2539        netif_tx_wake_all_queues(ieee->dev);
2540}
2541
2542/* called in user context only */
2543static void rtllib_start_master_bss(struct rtllib_device *ieee)
2544{
2545        ieee->assoc_id = 1;
2546
2547        if (ieee->current_network.ssid_len == 0) {
2548                strncpy(ieee->current_network.ssid,
2549                        RTLLIB_DEFAULT_TX_ESSID,
2550                        IW_ESSID_MAX_SIZE);
2551
2552                ieee->current_network.ssid_len =
2553                                 strlen(RTLLIB_DEFAULT_TX_ESSID);
2554                ieee->ssid_set = 1;
2555        }
2556
2557        ether_addr_copy(ieee->current_network.bssid, ieee->dev->dev_addr);
2558
2559        ieee->set_chan(ieee->dev, ieee->current_network.channel);
2560        ieee->state = RTLLIB_LINKED;
2561        ieee->link_change(ieee->dev);
2562        notify_wx_assoc_event(ieee);
2563
2564        if (ieee->data_hard_resume)
2565                ieee->data_hard_resume(ieee->dev);
2566
2567        netif_carrier_on(ieee->dev);
2568}
2569
2570static void rtllib_start_monitor_mode(struct rtllib_device *ieee)
2571{
2572        /* reset hardware status */
2573        if (ieee->raw_tx) {
2574                if (ieee->data_hard_resume)
2575                        ieee->data_hard_resume(ieee->dev);
2576
2577                netif_carrier_on(ieee->dev);
2578        }
2579}
2580
2581static void rtllib_start_ibss_wq(void *data)
2582{
2583        struct rtllib_device *ieee = container_of_dwork_rsl(data,
2584                                     struct rtllib_device, start_ibss_wq);
2585        /* iwconfig mode ad-hoc will schedule this and return
2586         * on the other hand this will block further iwconfig SET
2587         * operations because of the wx_mutex hold.
2588         * Anyway some most set operations set a flag to speed-up
2589         * (abort) this wq (when syncro scanning) before sleeping
2590         * on the mutex
2591         */
2592        if (!ieee->proto_started) {
2593                netdev_info(ieee->dev, "==========oh driver down return\n");
2594                return;
2595        }
2596        mutex_lock(&ieee->wx_mutex);
2597
2598        if (ieee->current_network.ssid_len == 0) {
2599                strcpy(ieee->current_network.ssid, RTLLIB_DEFAULT_TX_ESSID);
2600                ieee->current_network.ssid_len = strlen(RTLLIB_DEFAULT_TX_ESSID);
2601                ieee->ssid_set = 1;
2602        }
2603
2604        ieee->state = RTLLIB_NOLINK;
2605        ieee->mode = IEEE_G;
2606        /* check if we have this cell in our network list */
2607        rtllib_softmac_check_all_nets(ieee);
2608
2609
2610        /* if not then the state is not linked. Maybe the user switched to
2611         * ad-hoc mode just after being in monitor mode, or just after
2612         * being very few time in managed mode (so the card have had no
2613         * time to scan all the chans..) or we have just run up the iface
2614         * after setting ad-hoc mode. So we have to give another try..
2615         * Here, in ibss mode, should be safe to do this without extra care
2616         * (in bss mode we had to make sure no-one tried to associate when
2617         * we had just checked the ieee->state and we was going to start the
2618         * scan) because in ibss mode the rtllib_new_net function, when
2619         * finds a good net, just set the ieee->state to RTLLIB_LINKED,
2620         * so, at worst, we waste a bit of time to initiate an unneeded syncro
2621         * scan, that will stop at the first round because it sees the state
2622         * associated.
2623         */
2624        if (ieee->state == RTLLIB_NOLINK)
2625                rtllib_start_scan_syncro(ieee, 0);
2626
2627        /* the network definitively is not here.. create a new cell */
2628        if (ieee->state == RTLLIB_NOLINK) {
2629                netdev_info(ieee->dev, "creating new IBSS cell\n");
2630                ieee->current_network.channel = ieee->bss_start_channel;
2631                if (!ieee->wap_set)
2632                        eth_random_addr(ieee->current_network.bssid);
2633
2634                if (ieee->modulation & RTLLIB_CCK_MODULATION) {
2635
2636                        ieee->current_network.rates_len = 4;
2637
2638                        ieee->current_network.rates[0] =
2639                                 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_1MB;
2640                        ieee->current_network.rates[1] =
2641                                 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_2MB;
2642                        ieee->current_network.rates[2] =
2643                                 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_5MB;
2644                        ieee->current_network.rates[3] =
2645                                 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_11MB;
2646
2647                } else
2648                        ieee->current_network.rates_len = 0;
2649
2650                if (ieee->modulation & RTLLIB_OFDM_MODULATION) {
2651                        ieee->current_network.rates_ex_len = 8;
2652
2653                        ieee->current_network.rates_ex[0] =
2654                                                 RTLLIB_OFDM_RATE_6MB;
2655                        ieee->current_network.rates_ex[1] =
2656                                                 RTLLIB_OFDM_RATE_9MB;
2657                        ieee->current_network.rates_ex[2] =
2658                                                 RTLLIB_OFDM_RATE_12MB;
2659                        ieee->current_network.rates_ex[3] =
2660                                                 RTLLIB_OFDM_RATE_18MB;
2661                        ieee->current_network.rates_ex[4] =
2662                                                 RTLLIB_OFDM_RATE_24MB;
2663                        ieee->current_network.rates_ex[5] =
2664                                                 RTLLIB_OFDM_RATE_36MB;
2665                        ieee->current_network.rates_ex[6] =
2666                                                 RTLLIB_OFDM_RATE_48MB;
2667                        ieee->current_network.rates_ex[7] =
2668                                                 RTLLIB_OFDM_RATE_54MB;
2669
2670                        ieee->rate = 108;
2671                } else {
2672                        ieee->current_network.rates_ex_len = 0;
2673                        ieee->rate = 22;
2674                }
2675
2676                ieee->current_network.qos_data.supported = 0;
2677                ieee->SetWirelessMode(ieee->dev, IEEE_G);
2678                ieee->current_network.mode = ieee->mode;
2679                ieee->current_network.atim_window = 0;
2680                ieee->current_network.capability = WLAN_CAPABILITY_IBSS;
2681        }
2682
2683        netdev_info(ieee->dev, "%s(): ieee->mode = %d\n", __func__, ieee->mode);
2684        if ((ieee->mode == IEEE_N_24G) || (ieee->mode == IEEE_N_5G))
2685                HTUseDefaultSetting(ieee);
2686        else
2687                ieee->pHTInfo->bCurrentHTSupport = false;
2688
2689        ieee->SetHwRegHandler(ieee->dev, HW_VAR_MEDIA_STATUS,
2690                              (u8 *)(&ieee->state));
2691
2692        ieee->state = RTLLIB_LINKED;
2693        ieee->link_change(ieee->dev);
2694
2695        HTSetConnectBwMode(ieee, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
2696        if (ieee->LedControlHandler != NULL)
2697                ieee->LedControlHandler(ieee->dev, LED_CTL_LINK);
2698
2699        rtllib_start_send_beacons(ieee);
2700
2701        notify_wx_assoc_event(ieee);
2702
2703        if (ieee->data_hard_resume)
2704                ieee->data_hard_resume(ieee->dev);
2705
2706        netif_carrier_on(ieee->dev);
2707
2708        mutex_unlock(&ieee->wx_mutex);
2709}
2710
2711inline void rtllib_start_ibss(struct rtllib_device *ieee)
2712{
2713        schedule_delayed_work(&ieee->start_ibss_wq, msecs_to_jiffies(150));
2714}
2715
2716/* this is called only in user context, with wx_mutex held */
2717static void rtllib_start_bss(struct rtllib_device *ieee)
2718{
2719        unsigned long flags;
2720
2721        if (IS_DOT11D_ENABLE(ieee) && !IS_COUNTRY_IE_VALID(ieee)) {
2722                if (!ieee->global_domain)
2723                        return;
2724        }
2725        /* check if we have already found the net we
2726         * are interested in (if any).
2727         * if not (we are disassociated and we are not
2728         * in associating / authenticating phase) start the background scanning.
2729         */
2730        rtllib_softmac_check_all_nets(ieee);
2731
2732        /* ensure no-one start an associating process (thus setting
2733         * the ieee->state to rtllib_ASSOCIATING) while we
2734         * have just checked it and we are going to enable scan.
2735         * The rtllib_new_net function is always called with
2736         * lock held (from both rtllib_softmac_check_all_nets and
2737         * the rx path), so we cannot be in the middle of such function
2738         */
2739        spin_lock_irqsave(&ieee->lock, flags);
2740
2741        if (ieee->state == RTLLIB_NOLINK)
2742                rtllib_start_scan(ieee);
2743        spin_unlock_irqrestore(&ieee->lock, flags);
2744}
2745
2746static void rtllib_link_change_wq(void *data)
2747{
2748        struct rtllib_device *ieee = container_of_dwork_rsl(data,
2749                                     struct rtllib_device, link_change_wq);
2750        ieee->link_change(ieee->dev);
2751}
2752/* called only in userspace context */
2753void rtllib_disassociate(struct rtllib_device *ieee)
2754{
2755        netif_carrier_off(ieee->dev);
2756        if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)
2757                rtllib_reset_queue(ieee);
2758
2759        if (ieee->data_hard_stop)
2760                ieee->data_hard_stop(ieee->dev);
2761        if (IS_DOT11D_ENABLE(ieee))
2762                dot11d_reset(ieee);
2763        ieee->state = RTLLIB_NOLINK;
2764        ieee->is_set_key = false;
2765        ieee->wap_set = 0;
2766
2767        schedule_delayed_work(&ieee->link_change_wq, 0);
2768
2769        notify_wx_assoc_event(ieee);
2770}
2771
2772static void rtllib_associate_retry_wq(void *data)
2773{
2774        struct rtllib_device *ieee = container_of_dwork_rsl(data,
2775                                     struct rtllib_device, associate_retry_wq);
2776        unsigned long flags;
2777
2778        mutex_lock(&ieee->wx_mutex);
2779        if (!ieee->proto_started)
2780                goto exit;
2781
2782        if (ieee->state != RTLLIB_ASSOCIATING_RETRY)
2783                goto exit;
2784
2785        /* until we do not set the state to RTLLIB_NOLINK
2786         * there are no possibility to have someone else trying
2787         * to start an association procedure (we get here with
2788         * ieee->state = RTLLIB_ASSOCIATING).
2789         * When we set the state to RTLLIB_NOLINK it is possible
2790         * that the RX path run an attempt to associate, but
2791         * both rtllib_softmac_check_all_nets and the
2792         * RX path works with ieee->lock held so there are no
2793         * problems. If we are still disassociated then start a scan.
2794         * the lock here is necessary to ensure no one try to start
2795         * an association procedure when we have just checked the
2796         * state and we are going to start the scan.
2797         */
2798        ieee->beinretry = true;
2799        ieee->state = RTLLIB_NOLINK;
2800
2801        rtllib_softmac_check_all_nets(ieee);
2802
2803        spin_lock_irqsave(&ieee->lock, flags);
2804
2805        if (ieee->state == RTLLIB_NOLINK)
2806                rtllib_start_scan(ieee);
2807        spin_unlock_irqrestore(&ieee->lock, flags);
2808
2809        ieee->beinretry = false;
2810exit:
2811        mutex_unlock(&ieee->wx_mutex);
2812}
2813
2814static struct sk_buff *rtllib_get_beacon_(struct rtllib_device *ieee)
2815{
2816        static const u8 broadcast_addr[] = {
2817                0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2818        };
2819        struct sk_buff *skb;
2820        struct rtllib_probe_response *b;
2821
2822        skb = rtllib_probe_resp(ieee, broadcast_addr);
2823
2824        if (!skb)
2825                return NULL;
2826
2827        b = (struct rtllib_probe_response *) skb->data;
2828        b->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_BEACON);
2829
2830        return skb;
2831
2832}
2833
2834struct sk_buff *rtllib_get_beacon(struct rtllib_device *ieee)
2835{
2836        struct sk_buff *skb;
2837        struct rtllib_probe_response *b;
2838
2839        skb = rtllib_get_beacon_(ieee);
2840        if (!skb)
2841                return NULL;
2842
2843        b = (struct rtllib_probe_response *) skb->data;
2844        b->header.seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2845
2846        if (ieee->seq_ctrl[0] == 0xFFF)
2847                ieee->seq_ctrl[0] = 0;
2848        else
2849                ieee->seq_ctrl[0]++;
2850
2851        return skb;
2852}
2853EXPORT_SYMBOL(rtllib_get_beacon);
2854
2855void rtllib_softmac_stop_protocol(struct rtllib_device *ieee, u8 mesh_flag,
2856                                  u8 shutdown)
2857{
2858        rtllib_stop_scan_syncro(ieee);
2859        mutex_lock(&ieee->wx_mutex);
2860        rtllib_stop_protocol(ieee, shutdown);
2861        mutex_unlock(&ieee->wx_mutex);
2862}
2863EXPORT_SYMBOL(rtllib_softmac_stop_protocol);
2864
2865
2866void rtllib_stop_protocol(struct rtllib_device *ieee, u8 shutdown)
2867{
2868        if (!ieee->proto_started)
2869                return;
2870
2871        if (shutdown) {
2872                ieee->proto_started = 0;
2873                ieee->proto_stoppping = 1;
2874                if (ieee->rtllib_ips_leave != NULL)
2875                        ieee->rtllib_ips_leave(ieee->dev);
2876        }
2877
2878        rtllib_stop_send_beacons(ieee);
2879        del_timer_sync(&ieee->associate_timer);
2880        cancel_delayed_work_sync(&ieee->associate_retry_wq);
2881        cancel_delayed_work_sync(&ieee->start_ibss_wq);
2882        cancel_delayed_work_sync(&ieee->link_change_wq);
2883        rtllib_stop_scan(ieee);
2884
2885        if (ieee->state <= RTLLIB_ASSOCIATING_AUTHENTICATED)
2886                ieee->state = RTLLIB_NOLINK;
2887
2888        if (ieee->state == RTLLIB_LINKED) {
2889                if (ieee->iw_mode == IW_MODE_INFRA)
2890                        SendDisassociation(ieee, 1, WLAN_REASON_DEAUTH_LEAVING);
2891                rtllib_disassociate(ieee);
2892        }
2893
2894        if (shutdown) {
2895                RemoveAllTS(ieee);
2896                ieee->proto_stoppping = 0;
2897        }
2898        kfree(ieee->assocreq_ies);
2899        ieee->assocreq_ies = NULL;
2900        ieee->assocreq_ies_len = 0;
2901        kfree(ieee->assocresp_ies);
2902        ieee->assocresp_ies = NULL;
2903        ieee->assocresp_ies_len = 0;
2904}
2905
2906void rtllib_softmac_start_protocol(struct rtllib_device *ieee, u8 mesh_flag)
2907{
2908        mutex_lock(&ieee->wx_mutex);
2909        rtllib_start_protocol(ieee);
2910        mutex_unlock(&ieee->wx_mutex);
2911}
2912EXPORT_SYMBOL(rtllib_softmac_start_protocol);
2913
2914void rtllib_start_protocol(struct rtllib_device *ieee)
2915{
2916        short ch = 0;
2917        int i = 0;
2918
2919        rtllib_update_active_chan_map(ieee);
2920
2921        if (ieee->proto_started)
2922                return;
2923
2924        ieee->proto_started = 1;
2925
2926        if (ieee->current_network.channel == 0) {
2927                do {
2928                        ch++;
2929                        if (ch > MAX_CHANNEL_NUMBER)
2930                                return; /* no channel found */
2931                } while (!ieee->active_channel_map[ch]);
2932                ieee->current_network.channel = ch;
2933        }
2934
2935        if (ieee->current_network.beacon_interval == 0)
2936                ieee->current_network.beacon_interval = 100;
2937
2938        for (i = 0; i < 17; i++) {
2939                ieee->last_rxseq_num[i] = -1;
2940                ieee->last_rxfrag_num[i] = -1;
2941                ieee->last_packet_time[i] = 0;
2942        }
2943
2944        if (ieee->UpdateBeaconInterruptHandler)
2945                ieee->UpdateBeaconInterruptHandler(ieee->dev, false);
2946
2947        ieee->wmm_acm = 0;
2948        /* if the user set the MAC of the ad-hoc cell and then
2949         * switch to managed mode, shall we  make sure that association
2950         * attempts does not fail just because the user provide the essid
2951         * and the nic is still checking for the AP MAC ??
2952         */
2953        if (ieee->iw_mode == IW_MODE_INFRA) {
2954                rtllib_start_bss(ieee);
2955        } else if (ieee->iw_mode == IW_MODE_ADHOC) {
2956                if (ieee->UpdateBeaconInterruptHandler)
2957                        ieee->UpdateBeaconInterruptHandler(ieee->dev, true);
2958
2959                rtllib_start_ibss(ieee);
2960
2961        } else if (ieee->iw_mode == IW_MODE_MASTER) {
2962                rtllib_start_master_bss(ieee);
2963        } else if (ieee->iw_mode == IW_MODE_MONITOR) {
2964                rtllib_start_monitor_mode(ieee);
2965        }
2966}
2967
2968void rtllib_softmac_init(struct rtllib_device *ieee)
2969{
2970        int i;
2971
2972        memset(&ieee->current_network, 0, sizeof(struct rtllib_network));
2973
2974        ieee->state = RTLLIB_NOLINK;
2975        for (i = 0; i < 5; i++)
2976                ieee->seq_ctrl[i] = 0;
2977        ieee->dot11d_info = kzalloc(sizeof(struct rt_dot11d_info), GFP_ATOMIC);
2978        if (!ieee->dot11d_info)
2979                netdev_err(ieee->dev, "Can't alloc memory for DOT11D\n");
2980        ieee->LinkDetectInfo.SlotIndex = 0;
2981        ieee->LinkDetectInfo.SlotNum = 2;
2982        ieee->LinkDetectInfo.NumRecvBcnInPeriod = 0;
2983        ieee->LinkDetectInfo.NumRecvDataInPeriod = 0;
2984        ieee->LinkDetectInfo.NumTxOkInPeriod = 0;
2985        ieee->LinkDetectInfo.NumRxOkInPeriod = 0;
2986        ieee->LinkDetectInfo.NumRxUnicastOkInPeriod = 0;
2987        ieee->bIsAggregateFrame = false;
2988        ieee->assoc_id = 0;
2989        ieee->queue_stop = 0;
2990        ieee->scanning_continue = 0;
2991        ieee->softmac_features = 0;
2992        ieee->wap_set = 0;
2993        ieee->ssid_set = 0;
2994        ieee->proto_started = 0;
2995        ieee->proto_stoppping = 0;
2996        ieee->basic_rate = RTLLIB_DEFAULT_BASIC_RATE;
2997        ieee->rate = 22;
2998        ieee->ps = RTLLIB_PS_DISABLED;
2999        ieee->sta_sleep = LPS_IS_WAKE;
3000
3001        ieee->Regdot11HTOperationalRateSet[0] = 0xff;
3002        ieee->Regdot11HTOperationalRateSet[1] = 0xff;
3003        ieee->Regdot11HTOperationalRateSet[4] = 0x01;
3004
3005        ieee->Regdot11TxHTOperationalRateSet[0] = 0xff;
3006        ieee->Regdot11TxHTOperationalRateSet[1] = 0xff;
3007        ieee->Regdot11TxHTOperationalRateSet[4] = 0x01;
3008
3009        ieee->FirstIe_InScan = false;
3010        ieee->actscanning = false;
3011        ieee->beinretry = false;
3012        ieee->is_set_key = false;
3013        init_mgmt_queue(ieee);
3014
3015        ieee->tx_pending.txb = NULL;
3016
3017        timer_setup(&ieee->associate_timer, rtllib_associate_abort_cb, 0);
3018
3019        timer_setup(&ieee->beacon_timer, rtllib_send_beacon_cb, 0);
3020
3021        INIT_DELAYED_WORK_RSL(&ieee->link_change_wq,
3022                              (void *)rtllib_link_change_wq, ieee);
3023        INIT_DELAYED_WORK_RSL(&ieee->start_ibss_wq,
3024                              (void *)rtllib_start_ibss_wq, ieee);
3025        INIT_WORK_RSL(&ieee->associate_complete_wq,
3026                      (void *)rtllib_associate_complete_wq, ieee);
3027        INIT_DELAYED_WORK_RSL(&ieee->associate_procedure_wq,
3028                              (void *)rtllib_associate_procedure_wq, ieee);
3029        INIT_DELAYED_WORK_RSL(&ieee->softmac_scan_wq,
3030                              (void *)rtllib_softmac_scan_wq, ieee);
3031        INIT_DELAYED_WORK_RSL(&ieee->associate_retry_wq,
3032                              (void *)rtllib_associate_retry_wq, ieee);
3033        INIT_WORK_RSL(&ieee->wx_sync_scan_wq, (void *)rtllib_wx_sync_scan_wq,
3034                      ieee);
3035
3036        mutex_init(&ieee->wx_mutex);
3037        mutex_init(&ieee->scan_mutex);
3038        mutex_init(&ieee->ips_mutex);
3039
3040        spin_lock_init(&ieee->mgmt_tx_lock);
3041        spin_lock_init(&ieee->beacon_lock);
3042
3043        tasklet_init(&ieee->ps_task,
3044             (void(*)(unsigned long)) rtllib_sta_ps,
3045             (unsigned long)ieee);
3046
3047}
3048
3049void rtllib_softmac_free(struct rtllib_device *ieee)
3050{
3051        mutex_lock(&ieee->wx_mutex);
3052        kfree(ieee->dot11d_info);
3053        ieee->dot11d_info = NULL;
3054        del_timer_sync(&ieee->associate_timer);
3055
3056        cancel_delayed_work_sync(&ieee->associate_retry_wq);
3057        cancel_delayed_work_sync(&ieee->associate_procedure_wq);
3058        cancel_delayed_work_sync(&ieee->softmac_scan_wq);
3059        cancel_delayed_work_sync(&ieee->start_ibss_wq);
3060        cancel_delayed_work_sync(&ieee->hw_wakeup_wq);
3061        cancel_delayed_work_sync(&ieee->hw_sleep_wq);
3062        cancel_delayed_work_sync(&ieee->link_change_wq);
3063        cancel_work_sync(&ieee->associate_complete_wq);
3064        cancel_work_sync(&ieee->ips_leave_wq);
3065        cancel_work_sync(&ieee->wx_sync_scan_wq);
3066        mutex_unlock(&ieee->wx_mutex);
3067        tasklet_kill(&ieee->ps_task);
3068}
3069
3070static inline struct sk_buff *
3071rtllib_disauth_skb(struct rtllib_network *beacon,
3072                   struct rtllib_device *ieee, u16 asRsn)
3073{
3074        struct sk_buff *skb;
3075        struct rtllib_disauth *disauth;
3076        int len = sizeof(struct rtllib_disauth) + ieee->tx_headroom;
3077
3078        skb = dev_alloc_skb(len);
3079        if (!skb)
3080                return NULL;
3081
3082        skb_reserve(skb, ieee->tx_headroom);
3083
3084        disauth = skb_put(skb, sizeof(struct rtllib_disauth));
3085        disauth->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_DEAUTH);
3086        disauth->header.duration_id = 0;
3087
3088        ether_addr_copy(disauth->header.addr1, beacon->bssid);
3089        ether_addr_copy(disauth->header.addr2, ieee->dev->dev_addr);
3090        ether_addr_copy(disauth->header.addr3, beacon->bssid);
3091
3092        disauth->reason = cpu_to_le16(asRsn);
3093        return skb;
3094}
3095
3096static inline struct sk_buff *
3097rtllib_disassociate_skb(struct rtllib_network *beacon,
3098                        struct rtllib_device *ieee, u16 asRsn)
3099{
3100        struct sk_buff *skb;
3101        struct rtllib_disassoc *disass;
3102        int len = sizeof(struct rtllib_disassoc) + ieee->tx_headroom;
3103
3104        skb = dev_alloc_skb(len);
3105
3106        if (!skb)
3107                return NULL;
3108
3109        skb_reserve(skb, ieee->tx_headroom);
3110
3111        disass = skb_put(skb, sizeof(struct rtllib_disassoc));
3112        disass->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_DISASSOC);
3113        disass->header.duration_id = 0;
3114
3115        ether_addr_copy(disass->header.addr1, beacon->bssid);
3116        ether_addr_copy(disass->header.addr2, ieee->dev->dev_addr);
3117        ether_addr_copy(disass->header.addr3, beacon->bssid);
3118
3119        disass->reason = cpu_to_le16(asRsn);
3120        return skb;
3121}
3122
3123void SendDisassociation(struct rtllib_device *ieee, bool deauth, u16 asRsn)
3124{
3125        struct rtllib_network *beacon = &ieee->current_network;
3126        struct sk_buff *skb;
3127
3128        if (deauth)
3129                skb = rtllib_disauth_skb(beacon, ieee, asRsn);
3130        else
3131                skb = rtllib_disassociate_skb(beacon, ieee, asRsn);
3132
3133        if (skb)
3134                softmac_mgmt_xmit(skb, ieee);
3135}
3136
3137u8 rtllib_ap_sec_type(struct rtllib_device *ieee)
3138{
3139        static u8 ccmp_ie[4] = {0x00, 0x50, 0xf2, 0x04};
3140        static u8 ccmp_rsn_ie[4] = {0x00, 0x0f, 0xac, 0x04};
3141        int wpa_ie_len = ieee->wpa_ie_len;
3142        struct lib80211_crypt_data *crypt;
3143        int encrypt;
3144
3145        crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
3146        encrypt = (ieee->current_network.capability & WLAN_CAPABILITY_PRIVACY)
3147                  || (ieee->host_encrypt && crypt && crypt->ops &&
3148                  (strcmp(crypt->ops->name, "R-WEP") == 0));
3149
3150        /* simply judge  */
3151        if (encrypt && (wpa_ie_len == 0)) {
3152                return SEC_ALG_WEP;
3153        } else if ((wpa_ie_len != 0)) {
3154                if (((ieee->wpa_ie[0] == 0xdd) &&
3155                    (!memcmp(&(ieee->wpa_ie[14]), ccmp_ie, 4))) ||
3156                    ((ieee->wpa_ie[0] == 0x30) &&
3157                    (!memcmp(&ieee->wpa_ie[10], ccmp_rsn_ie, 4))))
3158                        return SEC_ALG_CCMP;
3159                else
3160                        return SEC_ALG_TKIP;
3161        } else {
3162                return SEC_ALG_NONE;
3163        }
3164}
3165
3166static void rtllib_MgntDisconnectIBSS(struct rtllib_device *rtllib)
3167{
3168        u8      OpMode;
3169        u8      i;
3170        bool    bFilterOutNonAssociatedBSSID = false;
3171
3172        rtllib->state = RTLLIB_NOLINK;
3173
3174        for (i = 0; i < 6; i++)
3175                rtllib->current_network.bssid[i] = 0x55;
3176
3177        rtllib->OpMode = RT_OP_MODE_NO_LINK;
3178        rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_BSSID,
3179                                rtllib->current_network.bssid);
3180        OpMode = RT_OP_MODE_NO_LINK;
3181        rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_MEDIA_STATUS, &OpMode);
3182        rtllib_stop_send_beacons(rtllib);
3183
3184        bFilterOutNonAssociatedBSSID = false;
3185        rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_CECHK_BSSID,
3186                                (u8 *)(&bFilterOutNonAssociatedBSSID));
3187        notify_wx_assoc_event(rtllib);
3188
3189}
3190
3191static void rtllib_MlmeDisassociateRequest(struct rtllib_device *rtllib,
3192                                           u8 *asSta, u8 asRsn)
3193{
3194        u8 i;
3195        u8      OpMode;
3196
3197        RemovePeerTS(rtllib, asSta);
3198
3199        if (memcmp(rtllib->current_network.bssid, asSta, 6) == 0) {
3200                rtllib->state = RTLLIB_NOLINK;
3201
3202                for (i = 0; i < 6; i++)
3203                        rtllib->current_network.bssid[i] = 0x22;
3204                OpMode = RT_OP_MODE_NO_LINK;
3205                rtllib->OpMode = RT_OP_MODE_NO_LINK;
3206                rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_MEDIA_STATUS,
3207                                        (u8 *)(&OpMode));
3208                rtllib_disassociate(rtllib);
3209
3210                rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_BSSID,
3211                                        rtllib->current_network.bssid);
3212
3213        }
3214
3215}
3216
3217static void
3218rtllib_MgntDisconnectAP(
3219        struct rtllib_device *rtllib,
3220        u8 asRsn
3221)
3222{
3223        bool bFilterOutNonAssociatedBSSID = false;
3224
3225        bFilterOutNonAssociatedBSSID = false;
3226        rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_CECHK_BSSID,
3227                                (u8 *)(&bFilterOutNonAssociatedBSSID));
3228        rtllib_MlmeDisassociateRequest(rtllib, rtllib->current_network.bssid,
3229                                       asRsn);
3230
3231        rtllib->state = RTLLIB_NOLINK;
3232}
3233
3234bool rtllib_MgntDisconnect(struct rtllib_device *rtllib, u8 asRsn)
3235{
3236        if (rtllib->ps != RTLLIB_PS_DISABLED)
3237                rtllib->sta_wake_up(rtllib->dev);
3238
3239        if (rtllib->state == RTLLIB_LINKED) {
3240                if (rtllib->iw_mode == IW_MODE_ADHOC)
3241                        rtllib_MgntDisconnectIBSS(rtllib);
3242                if (rtllib->iw_mode == IW_MODE_INFRA)
3243                        rtllib_MgntDisconnectAP(rtllib, asRsn);
3244
3245        }
3246
3247        return true;
3248}
3249EXPORT_SYMBOL(rtllib_MgntDisconnect);
3250
3251void notify_wx_assoc_event(struct rtllib_device *ieee)
3252{
3253        union iwreq_data wrqu;
3254
3255        if (ieee->cannot_notify)
3256                return;
3257
3258        wrqu.ap_addr.sa_family = ARPHRD_ETHER;
3259        if (ieee->state == RTLLIB_LINKED)
3260                memcpy(wrqu.ap_addr.sa_data, ieee->current_network.bssid,
3261                       ETH_ALEN);
3262        else {
3263
3264                netdev_info(ieee->dev, "%s(): Tell user space disconnected\n",
3265                            __func__);
3266                eth_zero_addr(wrqu.ap_addr.sa_data);
3267        }
3268        wireless_send_event(ieee->dev, SIOCGIWAP, &wrqu, NULL);
3269}
3270EXPORT_SYMBOL(notify_wx_assoc_event);
3271