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