linux/drivers/staging/rtl8187se/ieee80211/ieee80211_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 ieee80211
   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 "ieee80211.h"
  18
  19#include <linux/random.h>
  20#include <linux/delay.h>
  21#include <linux/slab.h>
  22#include <linux/interrupt.h>
  23#include <asm/uaccess.h>
  24#include <linux/etherdevice.h>
  25
  26#include "dot11d.h"
  27u8 rsn_authen_cipher_suite[16][4] = {
  28        {0x00,0x0F,0xAC,0x00}, //Use group key, //Reserved
  29        {0x00,0x0F,0xAC,0x01}, //WEP-40         //RSNA default
  30        {0x00,0x0F,0xAC,0x02}, //TKIP           //NONE          //{used just as default}
  31        {0x00,0x0F,0xAC,0x03}, //WRAP-historical
  32        {0x00,0x0F,0xAC,0x04}, //CCMP
  33        {0x00,0x0F,0xAC,0x05}, //WEP-104
  34};
  35
  36short ieee80211_is_54g(const struct ieee80211_network *net)
  37{
  38        return (net->rates_ex_len > 0) || (net->rates_len > 4);
  39}
  40
  41short ieee80211_is_shortslot(const struct ieee80211_network *net)
  42{
  43        return net->capability & WLAN_CAPABILITY_SHORT_SLOT;
  44}
  45
  46/* returns the total length needed for placing the RATE MFIE
  47 * tag and the EXTENDED RATE MFIE tag if needed.
  48 * It encludes two bytes per tag for the tag itself and its len
  49 */
  50unsigned int ieee80211_MFIE_rate_len(struct ieee80211_device *ieee)
  51{
  52        unsigned int rate_len = 0;
  53
  54        if (ieee->modulation & IEEE80211_CCK_MODULATION)
  55                rate_len = IEEE80211_CCK_RATE_LEN + 2;
  56
  57        if (ieee->modulation & IEEE80211_OFDM_MODULATION)
  58
  59                rate_len += IEEE80211_OFDM_RATE_LEN + 2;
  60
  61        return rate_len;
  62}
  63
  64/* place the MFIE rate, tag to the memory (double) poised.
  65 * Then it updates the pointer so that
  66 * it points after the new MFIE tag added.
  67 */
  68void ieee80211_MFIE_Brate(struct ieee80211_device *ieee, u8 **tag_p)
  69{
  70        u8 *tag = *tag_p;
  71
  72        if (ieee->modulation & IEEE80211_CCK_MODULATION){
  73                *tag++ = MFIE_TYPE_RATES;
  74                *tag++ = 4;
  75                *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
  76                *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
  77                *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
  78                *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
  79        }
  80
  81        /* We may add an option for custom rates that specific HW might support */
  82        *tag_p = tag;
  83}
  84
  85void ieee80211_MFIE_Grate(struct ieee80211_device *ieee, u8 **tag_p)
  86{
  87        u8 *tag = *tag_p;
  88
  89                if (ieee->modulation & IEEE80211_OFDM_MODULATION){
  90
  91                *tag++ = MFIE_TYPE_RATES_EX;
  92                *tag++ = 8;
  93                *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
  94                *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
  95                *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
  96                *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
  97                *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
  98                *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
  99                *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
 100                *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
 101
 102        }
 103
 104        /* We may add an option for custom rates that specific HW might support */
 105        *tag_p = tag;
 106}
 107
 108
 109void ieee80211_WMM_Info(struct ieee80211_device *ieee, u8 **tag_p) {
 110        u8 *tag = *tag_p;
 111
 112        *tag++ = MFIE_TYPE_GENERIC; //0
 113        *tag++ = 7;
 114        *tag++ = 0x00;
 115        *tag++ = 0x50;
 116        *tag++ = 0xf2;
 117        *tag++ = 0x02;//5
 118        *tag++ = 0x00;
 119        *tag++ = 0x01;
 120#ifdef SUPPORT_USPD
 121        if(ieee->current_network.wmm_info & 0x80) {
 122                *tag++ = 0x0f|MAX_SP_Len;
 123        } else {
 124                *tag++ = MAX_SP_Len;
 125        }
 126#else
 127        *tag++ = MAX_SP_Len;
 128#endif
 129        *tag_p = tag;
 130}
 131
 132void ieee80211_TURBO_Info(struct ieee80211_device *ieee, u8 **tag_p) {
 133        u8 *tag = *tag_p;
 134
 135        *tag++ = MFIE_TYPE_GENERIC; //0
 136        *tag++ = 7;
 137        *tag++ = 0x00;
 138        *tag++ = 0xe0;
 139        *tag++ = 0x4c;
 140        *tag++ = 0x01;//5
 141        *tag++ = 0x02;
 142        *tag++ = 0x11;
 143        *tag++ = 0x00;
 144
 145        *tag_p = tag;
 146        printk(KERN_ALERT "This is enable turbo mode IE process\n");
 147}
 148
 149void enqueue_mgmt(struct ieee80211_device *ieee, struct sk_buff *skb)
 150{
 151        int nh;
 152        nh = (ieee->mgmt_queue_head +1) % MGMT_QUEUE_NUM;
 153
 154/*
 155 * if the queue is full but we have newer frames then
 156 * just overwrites the oldest.
 157 *
 158 * if (nh == ieee->mgmt_queue_tail)
 159 *              return -1;
 160 */
 161        ieee->mgmt_queue_head = nh;
 162        ieee->mgmt_queue_ring[nh] = skb;
 163
 164        //return 0;
 165}
 166
 167struct sk_buff *dequeue_mgmt(struct ieee80211_device *ieee)
 168{
 169        struct sk_buff *ret;
 170
 171        if(ieee->mgmt_queue_tail == ieee->mgmt_queue_head)
 172                return NULL;
 173
 174        ret = ieee->mgmt_queue_ring[ieee->mgmt_queue_tail];
 175
 176        ieee->mgmt_queue_tail =
 177                (ieee->mgmt_queue_tail+1) % MGMT_QUEUE_NUM;
 178
 179        return ret;
 180}
 181
 182void init_mgmt_queue(struct ieee80211_device *ieee)
 183{
 184        ieee->mgmt_queue_tail = ieee->mgmt_queue_head = 0;
 185}
 186
 187
 188void ieee80211_sta_wakeup(struct ieee80211_device *ieee, short nl);
 189
 190inline void softmac_mgmt_xmit(struct sk_buff *skb, struct ieee80211_device *ieee)
 191{
 192        unsigned long flags;
 193        short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
 194        struct ieee80211_hdr_3addr  *header=
 195                (struct ieee80211_hdr_3addr  *) skb->data;
 196
 197
 198        spin_lock_irqsave(&ieee->lock, flags);
 199
 200        /* called with 2nd param 0, no mgmt lock required */
 201        ieee80211_sta_wakeup(ieee,0);
 202
 203        if(single){
 204                if(ieee->queue_stop){
 205
 206                        enqueue_mgmt(ieee,skb);
 207                }else{
 208                        header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0]<<4);
 209
 210                        if (ieee->seq_ctrl[0] == 0xFFF)
 211                                ieee->seq_ctrl[0] = 0;
 212                        else
 213                                ieee->seq_ctrl[0]++;
 214
 215                        /* avoid watchdog triggers */
 216                        ieee->dev->trans_start = jiffies;
 217                        ieee->softmac_data_hard_start_xmit(skb,ieee->dev,ieee->basic_rate);
 218                }
 219
 220                spin_unlock_irqrestore(&ieee->lock, flags);
 221        }else{
 222                spin_unlock_irqrestore(&ieee->lock, flags);
 223                spin_lock_irqsave(&ieee->mgmt_tx_lock, flags);
 224
 225                header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
 226
 227                if (ieee->seq_ctrl[0] == 0xFFF)
 228                        ieee->seq_ctrl[0] = 0;
 229                else
 230                        ieee->seq_ctrl[0]++;
 231
 232                /* avoid watchdog triggers */
 233                ieee->dev->trans_start = jiffies;
 234                ieee->softmac_hard_start_xmit(skb,ieee->dev);
 235
 236                spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags);
 237        }
 238}
 239
 240
 241inline void softmac_ps_mgmt_xmit(struct sk_buff *skb, struct ieee80211_device *ieee)
 242{
 243
 244        short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
 245        struct ieee80211_hdr_3addr  *header =
 246                (struct ieee80211_hdr_3addr  *) skb->data;
 247
 248
 249        if(single){
 250
 251                header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
 252
 253                if (ieee->seq_ctrl[0] == 0xFFF)
 254                        ieee->seq_ctrl[0] = 0;
 255                else
 256                        ieee->seq_ctrl[0]++;
 257
 258                /* avoid watchdog triggers */
 259                ieee->dev->trans_start = jiffies;
 260                ieee->softmac_data_hard_start_xmit(skb,ieee->dev,ieee->basic_rate);
 261
 262        }else{
 263
 264                header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
 265
 266                if (ieee->seq_ctrl[0] == 0xFFF)
 267                        ieee->seq_ctrl[0] = 0;
 268                else
 269                        ieee->seq_ctrl[0]++;
 270
 271                /* avoid watchdog triggers */
 272                ieee->dev->trans_start = jiffies;
 273                ieee->softmac_hard_start_xmit(skb,ieee->dev);
 274
 275        }
 276//      dev_kfree_skb_any(skb);//edit by thomas
 277}
 278//by amy for power save
 279inline struct sk_buff *ieee80211_disassociate_skb(
 280                                                        struct ieee80211_network *beacon,
 281                                                        struct ieee80211_device *ieee,
 282                                                        u8      asRsn)
 283{
 284        struct sk_buff *skb;
 285        struct ieee80211_disassoc_frame *disass;
 286
 287        skb = dev_alloc_skb(sizeof(struct ieee80211_disassoc_frame));
 288        if (!skb)
 289                return NULL;
 290
 291        disass = (struct ieee80211_disassoc_frame *) skb_put(skb,sizeof(struct ieee80211_disassoc_frame));
 292        disass->header.frame_control = cpu_to_le16(IEEE80211_STYPE_DISASSOC);
 293        disass->header.duration_id = 0;
 294
 295        memcpy(disass->header.addr1, beacon->bssid, ETH_ALEN);
 296        memcpy(disass->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
 297        memcpy(disass->header.addr3, beacon->bssid, ETH_ALEN);
 298
 299        disass->reasoncode = asRsn;
 300        return skb;
 301}
 302void
 303SendDisassociation(
 304        struct ieee80211_device *ieee,
 305        u8*                     asSta,
 306        u8                      asRsn
 307)
 308{
 309        struct ieee80211_network *beacon = &ieee->current_network;
 310        struct sk_buff *skb;
 311        skb = ieee80211_disassociate_skb(beacon,ieee,asRsn);
 312        if (skb){
 313                softmac_mgmt_xmit(skb, ieee);
 314                //dev_kfree_skb_any(skb);//edit by thomas
 315        }
 316}
 317
 318//by amy for power save
 319inline struct sk_buff *ieee80211_probe_req(struct ieee80211_device *ieee)
 320{
 321        unsigned int len,rate_len;
 322        u8 *tag;
 323        struct sk_buff *skb;
 324        struct ieee80211_probe_request *req;
 325
 326        len = ieee->current_network.ssid_len;
 327
 328        rate_len = ieee80211_MFIE_rate_len(ieee);
 329
 330        skb = dev_alloc_skb(sizeof(struct ieee80211_probe_request) +
 331                            2 + len + rate_len);
 332        if (!skb)
 333                return NULL;
 334
 335        req = (struct ieee80211_probe_request *) skb_put(skb,sizeof(struct ieee80211_probe_request));
 336        req->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
 337        req->header.duration_id = 0; //FIXME: is this OK ?
 338
 339        memset(req->header.addr1, 0xff, ETH_ALEN);
 340        memcpy(req->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
 341        memset(req->header.addr3, 0xff, ETH_ALEN);
 342
 343        tag = (u8 *) skb_put(skb,len+2+rate_len);
 344
 345        *tag++ = MFIE_TYPE_SSID;
 346        *tag++ = len;
 347        memcpy(tag, ieee->current_network.ssid, len);
 348        tag += len;
 349        ieee80211_MFIE_Brate(ieee,&tag);
 350        ieee80211_MFIE_Grate(ieee,&tag);
 351
 352        return skb;
 353}
 354
 355struct sk_buff *ieee80211_get_beacon_(struct ieee80211_device *ieee);
 356
 357void ext_ieee80211_send_beacon_wq(struct ieee80211_device *ieee)
 358{
 359        struct sk_buff *skb;
 360
 361        //unsigned long flags;
 362
 363        skb = ieee80211_get_beacon_(ieee);
 364
 365        if (skb){
 366                softmac_mgmt_xmit(skb, ieee);
 367                ieee->softmac_stats.tx_beacons++;
 368                dev_kfree_skb_any(skb);//edit by thomas
 369        }
 370
 371
 372        //printk(KERN_WARNING "[1] beacon sending!\n");
 373        ieee->beacon_timer.expires = jiffies +
 374                (MSECS( ieee->current_network.beacon_interval -5));
 375
 376        //spin_lock_irqsave(&ieee->beacon_lock,flags);
 377        if(ieee->beacon_txing)
 378                add_timer(&ieee->beacon_timer);
 379        //spin_unlock_irqrestore(&ieee->beacon_lock,flags);
 380}
 381
 382void ieee80211_send_beacon(struct ieee80211_device *ieee)
 383{
 384        struct sk_buff *skb;
 385
 386        //unsigned long flags;
 387
 388        skb = ieee80211_get_beacon_(ieee);
 389
 390        if (skb){
 391                softmac_mgmt_xmit(skb, ieee);
 392                ieee->softmac_stats.tx_beacons++;
 393                dev_kfree_skb_any(skb);//edit by thomas
 394        }
 395
 396        //printk(KERN_WARNING "[1] beacon sending!\n");
 397        ieee->beacon_timer.expires = jiffies +
 398                (MSECS( ieee->current_network.beacon_interval -5));
 399
 400        //spin_lock_irqsave(&ieee->beacon_lock,flags);
 401        if(ieee->beacon_txing)
 402                add_timer(&ieee->beacon_timer);
 403        //spin_unlock_irqrestore(&ieee->beacon_lock,flags);
 404}
 405
 406
 407void ieee80211_send_beacon_cb(unsigned long _ieee)
 408{
 409        struct ieee80211_device *ieee =
 410                (struct ieee80211_device *) _ieee;
 411        unsigned long flags;
 412
 413        spin_lock_irqsave(&ieee->beacon_lock, flags);
 414        ieee80211_send_beacon(ieee);
 415        spin_unlock_irqrestore(&ieee->beacon_lock, flags);
 416}
 417
 418void ieee80211_send_probe(struct ieee80211_device *ieee)
 419{
 420        struct sk_buff *skb;
 421
 422        skb = ieee80211_probe_req(ieee);
 423        if (skb){
 424                softmac_mgmt_xmit(skb, ieee);
 425                ieee->softmac_stats.tx_probe_rq++;
 426                //dev_kfree_skb_any(skb);//edit by thomas
 427        }
 428}
 429
 430void ieee80211_send_probe_requests(struct ieee80211_device *ieee)
 431{
 432        if (ieee->active_scan && (ieee->softmac_features & IEEE_SOFTMAC_PROBERQ)){
 433                ieee80211_send_probe(ieee);
 434                ieee80211_send_probe(ieee);
 435        }
 436}
 437
 438/* this performs syncro scan blocking the caller until all channels
 439 * in the allowed channel map has been checked.
 440 */
 441void ieee80211_softmac_scan_syncro(struct ieee80211_device *ieee)
 442{
 443        short ch = 0;
 444        u8 channel_map[MAX_CHANNEL_NUMBER+1];
 445        memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
 446        down(&ieee->scan_sem);
 447//      printk("==================> Sync scan\n");
 448
 449        while(1)
 450        {
 451
 452                do{
 453                        ch++;
 454                        if (ch > MAX_CHANNEL_NUMBER)
 455                                goto out; /* scan completed */
 456
 457                }while(!channel_map[ch]);
 458                /* this function can be called in two situations
 459                 * 1- We have switched to ad-hoc mode and we are
 460                 *    performing a complete syncro scan before conclude
 461                 *    there are no interesting cell and to create a
 462                 *    new one. In this case the link state is
 463                 *    IEEE80211_NOLINK until we found an interesting cell.
 464                 *    If so the ieee8021_new_net, called by the RX path
 465                 *    will set the state to IEEE80211_LINKED, so we stop
 466                 *    scanning
 467                 * 2- We are linked and the root uses run iwlist scan.
 468                 *    So we switch to IEEE80211_LINKED_SCANNING to remember
 469                 *    that we are still logically linked (not interested in
 470                 *    new network events, despite for updating the net list,
 471                 *    but we are temporarily 'unlinked' as the driver shall
 472                 *    not filter RX frames and the channel is changing.
 473                 * So the only situation in witch are interested is to check
 474                 * if the state become LINKED because of the #1 situation
 475                 */
 476
 477                if (ieee->state == IEEE80211_LINKED)
 478                        goto out;
 479
 480                ieee->set_chan(ieee->dev, ch);
 481//              printk("=====>channel=%d   ",ch);
 482                if(channel_map[ch] == 1)
 483                {
 484//                      printk("====send probe request\n");
 485                        ieee80211_send_probe_requests(ieee);
 486                }
 487                /* this prevent excessive time wait when we
 488                 * need to wait for a syncro scan to end..
 489                 */
 490                if (ieee->sync_scan_hurryup)
 491                        goto out;
 492
 493
 494                msleep_interruptible_rtl(IEEE80211_SOFTMAC_SCAN_TIME);
 495
 496        }
 497out:
 498        ieee->sync_scan_hurryup = 0;
 499        up(&ieee->scan_sem);
 500        if(IS_DOT11D_ENABLE(ieee))
 501                DOT11D_ScanComplete(ieee);
 502}
 503
 504void ieee80211_softmac_ips_scan_syncro(struct ieee80211_device *ieee)
 505{
 506        int ch;
 507        unsigned int watch_dog = 0;
 508        u8 channel_map[MAX_CHANNEL_NUMBER+1];
 509        memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
 510        down(&ieee->scan_sem);
 511        ch = ieee->current_network.channel;
 512//              if(ieee->sync_scan_hurryup)
 513//      {
 514
 515//              printk("stop scan sync\n");
 516//              goto out;
 517//      }
 518//      printk("=======hh===============>ips scan\n");
 519        while(1)
 520        {
 521                /* this function can be called in two situations
 522                 * 1- We have switched to ad-hoc mode and we are
 523                 *    performing a complete syncro scan before conclude
 524                 *    there are no interesting cell and to create a
 525                 *    new one. In this case the link state is
 526                 *    IEEE80211_NOLINK until we found an interesting cell.
 527                 *    If so the ieee8021_new_net, called by the RX path
 528                 *    will set the state to IEEE80211_LINKED, so we stop
 529                 *    scanning
 530                 * 2- We are linked and the root uses run iwlist scan.
 531                 *    So we switch to IEEE80211_LINKED_SCANNING to remember
 532                 *    that we are still logically linked (not interested in
 533                 *    new network events, despite for updating the net list,
 534                 *    but we are temporarily 'unlinked' as the driver shall
 535                 *    not filter RX frames and the channel is changing.
 536                 * So the only situation in witch are interested is to check
 537                 * if the state become LINKED because of the #1 situation
 538                 */
 539                if (ieee->state == IEEE80211_LINKED)
 540                {
 541                        goto out;
 542                }
 543                if(channel_map[ieee->current_network.channel] > 0)
 544                {
 545                        ieee->set_chan(ieee->dev, ieee->current_network.channel);
 546//                      printk("======>channel=%d  ",ieee->current_network.channel);
 547                }
 548                if(channel_map[ieee->current_network.channel] == 1)
 549                {
 550//                      printk("====send probe request\n");
 551                        ieee80211_send_probe_requests(ieee);
 552                }
 553                /* this prevent excessive time wait when we
 554                 * need to wait for a syncro scan to end..
 555                 */
 556//                if (ieee->sync_scan_hurryup)
 557//                        goto out;
 558
 559                msleep_interruptible_rtl(IEEE80211_SOFTMAC_SCAN_TIME);
 560
 561                do{
 562                        if (watch_dog++ >= MAX_CHANNEL_NUMBER)
 563                //      if (++watch_dog >= 15);//MAX_CHANNEL_NUMBER)  //YJ,modified,080630
 564                                goto out; /* scan completed */
 565
 566                        ieee->current_network.channel = (ieee->current_network.channel + 1)%MAX_CHANNEL_NUMBER;
 567                }while(!channel_map[ieee->current_network.channel]);
 568        }
 569out:
 570        //ieee->sync_scan_hurryup = 0;
 571        //ieee->set_chan(ieee->dev, ch);
 572        //ieee->current_network.channel = ch;
 573        ieee->actscanning = false;
 574        up(&ieee->scan_sem);
 575        if(IS_DOT11D_ENABLE(ieee))
 576                DOT11D_ScanComplete(ieee);
 577}
 578
 579void ieee80211_softmac_scan_wq(struct work_struct *work)
 580{
 581        struct delayed_work *dwork = to_delayed_work(work);
 582        struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, softmac_scan_wq);
 583        static short watchdog = 0;
 584        u8 channel_map[MAX_CHANNEL_NUMBER+1];
 585        memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
 586//      printk("ieee80211_softmac_scan_wq ENABLE_IPS\n");
 587//      printk("in %s\n",__func__);
 588        down(&ieee->scan_sem);
 589
 590        do{
 591                ieee->current_network.channel =
 592                        (ieee->current_network.channel + 1) % MAX_CHANNEL_NUMBER;
 593                if (watchdog++ > MAX_CHANNEL_NUMBER)
 594                                goto out; /* no good chans */
 595
 596        }while(!channel_map[ieee->current_network.channel]);
 597
 598        //printk("current_network.channel:%d\n", ieee->current_network.channel);
 599        if (ieee->scanning == 0 )
 600        {
 601                printk("error out, scanning = 0\n");
 602                goto out;
 603        }
 604        ieee->set_chan(ieee->dev, ieee->current_network.channel);
 605        if(channel_map[ieee->current_network.channel] == 1)
 606                ieee80211_send_probe_requests(ieee);
 607
 608        queue_delayed_work(ieee->wq, &ieee->softmac_scan_wq, IEEE80211_SOFTMAC_SCAN_TIME);
 609        up(&ieee->scan_sem);
 610        return;
 611out:
 612        ieee->actscanning = false;
 613        watchdog = 0;
 614        ieee->scanning = 0;
 615        up(&ieee->scan_sem);
 616
 617        if(IS_DOT11D_ENABLE(ieee))
 618                DOT11D_ScanComplete(ieee);
 619        return;
 620}
 621
 622void ieee80211_beacons_start(struct ieee80211_device *ieee)
 623{
 624        unsigned long flags;
 625
 626        spin_lock_irqsave(&ieee->beacon_lock,flags);
 627
 628        ieee->beacon_txing = 1;
 629        ieee80211_send_beacon(ieee);
 630
 631        spin_unlock_irqrestore(&ieee->beacon_lock,flags);
 632}
 633
 634void ieee80211_beacons_stop(struct ieee80211_device *ieee)
 635{
 636        unsigned long flags;
 637
 638        spin_lock_irqsave(&ieee->beacon_lock,flags);
 639
 640        ieee->beacon_txing = 0;
 641        del_timer_sync(&ieee->beacon_timer);
 642
 643        spin_unlock_irqrestore(&ieee->beacon_lock,flags);
 644
 645}
 646
 647
 648void ieee80211_stop_send_beacons(struct ieee80211_device *ieee)
 649{
 650        if(ieee->stop_send_beacons)
 651                ieee->stop_send_beacons(ieee->dev);
 652        if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
 653                ieee80211_beacons_stop(ieee);
 654}
 655
 656
 657void ieee80211_start_send_beacons(struct ieee80211_device *ieee)
 658{
 659        if(ieee->start_send_beacons)
 660                ieee->start_send_beacons(ieee->dev);
 661        if(ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
 662                ieee80211_beacons_start(ieee);
 663}
 664
 665
 666void ieee80211_softmac_stop_scan(struct ieee80211_device *ieee)
 667{
 668//      unsigned long flags;
 669
 670        //ieee->sync_scan_hurryup = 1;
 671
 672        down(&ieee->scan_sem);
 673//      spin_lock_irqsave(&ieee->lock, flags);
 674
 675        if (ieee->scanning == 1){
 676                ieee->scanning = 0;
 677                //del_timer_sync(&ieee->scan_timer);
 678                cancel_delayed_work(&ieee->softmac_scan_wq);
 679        }
 680
 681//      spin_unlock_irqrestore(&ieee->lock, flags);
 682        up(&ieee->scan_sem);
 683}
 684
 685void ieee80211_stop_scan(struct ieee80211_device *ieee)
 686{
 687        if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
 688                ieee80211_softmac_stop_scan(ieee);
 689        else
 690                ieee->stop_scan(ieee->dev);
 691}
 692
 693/* called with ieee->lock held */
 694void ieee80211_rtl_start_scan(struct ieee80211_device *ieee)
 695{
 696        if(IS_DOT11D_ENABLE(ieee) )
 697        {
 698                if(IS_COUNTRY_IE_VALID(ieee))
 699                {
 700                        RESET_CIE_WATCHDOG(ieee);
 701                }
 702        }
 703        if (ieee->softmac_features & IEEE_SOFTMAC_SCAN){
 704                if (ieee->scanning == 0)
 705                {
 706                        ieee->scanning = 1;
 707                        //ieee80211_softmac_scan(ieee);
 708                //      queue_work(ieee->wq, &ieee->softmac_scan_wq);
 709                //care this,1203,2007,by lawrence
 710#if 1
 711                        queue_delayed_work(ieee->wq, &ieee->softmac_scan_wq,0);
 712#endif
 713                }
 714        }else
 715                ieee->start_scan(ieee->dev);
 716
 717}
 718
 719/* called with wx_sem held */
 720void ieee80211_start_scan_syncro(struct ieee80211_device *ieee)
 721{
 722        if(IS_DOT11D_ENABLE(ieee) )
 723        {
 724                if(IS_COUNTRY_IE_VALID(ieee))
 725                {
 726                        RESET_CIE_WATCHDOG(ieee);
 727                }
 728        }
 729        ieee->sync_scan_hurryup = 0;
 730
 731        if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
 732                ieee80211_softmac_scan_syncro(ieee);
 733        else
 734                ieee->scan_syncro(ieee->dev);
 735
 736}
 737
 738inline struct sk_buff *ieee80211_authentication_req(struct ieee80211_network *beacon,
 739        struct ieee80211_device *ieee, int challengelen)
 740{
 741        struct sk_buff *skb;
 742        struct ieee80211_authentication *auth;
 743
 744        skb = dev_alloc_skb(sizeof(struct ieee80211_authentication) + challengelen);
 745
 746        if (!skb) return NULL;
 747
 748        auth = (struct ieee80211_authentication *)
 749                skb_put(skb, sizeof(struct ieee80211_authentication));
 750
 751        auth->header.frame_ctl = IEEE80211_STYPE_AUTH;
 752        if (challengelen) auth->header.frame_ctl |= IEEE80211_FCTL_WEP;
 753
 754        auth->header.duration_id = 0x013a; //FIXME
 755
 756        memcpy(auth->header.addr1, beacon->bssid, ETH_ALEN);
 757        memcpy(auth->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
 758        memcpy(auth->header.addr3, beacon->bssid, ETH_ALEN);
 759
 760        auth->algorithm = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
 761
 762        auth->transaction = cpu_to_le16(ieee->associate_seq);
 763        ieee->associate_seq++;
 764
 765        auth->status = cpu_to_le16(WLAN_STATUS_SUCCESS);
 766
 767        return skb;
 768
 769}
 770
 771static struct sk_buff* ieee80211_probe_resp(struct ieee80211_device *ieee, u8 *dest)
 772{
 773        u8 *tag;
 774        int beacon_size;
 775        struct ieee80211_probe_response *beacon_buf;
 776        struct sk_buff *skb;
 777        int encrypt;
 778        int atim_len,erp_len;
 779        struct ieee80211_crypt_data* crypt;
 780
 781        char *ssid = ieee->current_network.ssid;
 782        int ssid_len = ieee->current_network.ssid_len;
 783        int rate_len = ieee->current_network.rates_len+2;
 784        int rate_ex_len = ieee->current_network.rates_ex_len;
 785        int wpa_ie_len = ieee->wpa_ie_len;
 786        if(rate_ex_len > 0) rate_ex_len+=2;
 787
 788        if(ieee->current_network.capability & WLAN_CAPABILITY_IBSS)
 789                atim_len = 4;
 790        else
 791                atim_len = 0;
 792
 793        if(ieee80211_is_54g(&ieee->current_network))
 794                erp_len = 3;
 795        else
 796                erp_len = 0;
 797
 798        beacon_size = sizeof(struct ieee80211_probe_response)+
 799                ssid_len
 800                +3 //channel
 801                +rate_len
 802                +rate_ex_len
 803                +atim_len
 804                +wpa_ie_len
 805                +erp_len;
 806
 807        skb = dev_alloc_skb(beacon_size);
 808
 809        if (!skb)
 810                return NULL;
 811
 812        beacon_buf = (struct ieee80211_probe_response*) skb_put(skb, beacon_size);
 813
 814        memcpy (beacon_buf->header.addr1, dest,ETH_ALEN);
 815        memcpy (beacon_buf->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
 816        memcpy (beacon_buf->header.addr3, ieee->current_network.bssid, ETH_ALEN);
 817
 818        beacon_buf->header.duration_id = 0; //FIXME
 819        beacon_buf->beacon_interval =
 820                cpu_to_le16(ieee->current_network.beacon_interval);
 821        beacon_buf->capability =
 822                cpu_to_le16(ieee->current_network.capability & WLAN_CAPABILITY_IBSS);
 823
 824        if(ieee->short_slot && (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_SLOT))
 825                beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
 826
 827        crypt = ieee->crypt[ieee->tx_keyidx];
 828
 829        encrypt = ieee->host_encrypt && crypt && crypt->ops &&
 830                ((0 == strcmp(crypt->ops->name, "WEP")) || wpa_ie_len);
 831
 832        if (encrypt)
 833                beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
 834
 835
 836        beacon_buf->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_RESP);
 837
 838        beacon_buf->info_element.id = MFIE_TYPE_SSID;
 839        beacon_buf->info_element.len = ssid_len;
 840
 841        tag = (u8*) beacon_buf->info_element.data;
 842
 843        memcpy(tag, ssid, ssid_len);
 844
 845        tag += ssid_len;
 846
 847        *(tag++) = MFIE_TYPE_RATES;
 848        *(tag++) = rate_len-2;
 849        memcpy(tag,ieee->current_network.rates,rate_len-2);
 850        tag+=rate_len-2;
 851
 852        *(tag++) = MFIE_TYPE_DS_SET;
 853        *(tag++) = 1;
 854        *(tag++) = ieee->current_network.channel;
 855
 856        if(atim_len){
 857                *(tag++) = MFIE_TYPE_IBSS_SET;
 858                *(tag++) = 2;
 859                *((u16*)(tag)) = cpu_to_le16(ieee->current_network.atim_window);
 860                tag+=2;
 861        }
 862
 863        if(erp_len){
 864                *(tag++) = MFIE_TYPE_ERP;
 865                *(tag++) = 1;
 866                *(tag++) = 0;
 867        }
 868
 869        if(rate_ex_len){
 870                *(tag++) = MFIE_TYPE_RATES_EX;
 871                *(tag++) = rate_ex_len-2;
 872                memcpy(tag,ieee->current_network.rates_ex,rate_ex_len-2);
 873                tag+=rate_ex_len-2;
 874        }
 875
 876        if (wpa_ie_len)
 877        {
 878                if (ieee->iw_mode == IW_MODE_ADHOC)
 879                {//as Windows will set pairwise key same as the group key which is not allowed in Linux, so set this for IOT issue. WB 2008.07.07
 880                        memcpy(&ieee->wpa_ie[14], &ieee->wpa_ie[8], 4);
 881                }
 882
 883                memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
 884        }
 885
 886        skb->dev = ieee->dev;
 887        return skb;
 888}
 889
 890static struct sk_buff *ieee80211_assoc_resp(struct ieee80211_device *ieee,
 891                                            u8 *dest)
 892{
 893        struct sk_buff *skb;
 894        u8* tag;
 895
 896        struct ieee80211_crypt_data* crypt;
 897        struct ieee80211_assoc_response_frame *assoc;
 898        short encrypt;
 899
 900        unsigned int rate_len = ieee80211_MFIE_rate_len(ieee);
 901        int len = sizeof(struct ieee80211_assoc_response_frame) + rate_len;
 902
 903        skb = dev_alloc_skb(len);
 904
 905        if (!skb)
 906                return NULL;
 907
 908        assoc = (struct ieee80211_assoc_response_frame *)
 909                skb_put(skb,sizeof(struct ieee80211_assoc_response_frame));
 910
 911        assoc->header.frame_control = cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP);
 912        memcpy(assoc->header.addr1, dest,ETH_ALEN);
 913        memcpy(assoc->header.addr3, ieee->dev->dev_addr, ETH_ALEN);
 914        memcpy(assoc->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
 915        assoc->capability = cpu_to_le16(ieee->iw_mode == IW_MODE_MASTER ?
 916                WLAN_CAPABILITY_BSS : WLAN_CAPABILITY_IBSS);
 917
 918
 919        if(ieee->short_slot)
 920                assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
 921
 922        if (ieee->host_encrypt)
 923                crypt = ieee->crypt[ieee->tx_keyidx];
 924        else crypt = NULL;
 925
 926        encrypt = ( crypt && crypt->ops);
 927
 928        if (encrypt)
 929                assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
 930
 931        assoc->status = 0;
 932        assoc->aid = cpu_to_le16(ieee->assoc_id);
 933        if (ieee->assoc_id == 0x2007) ieee->assoc_id=0;
 934        else ieee->assoc_id++;
 935
 936        tag = (u8*) skb_put(skb, rate_len);
 937
 938        ieee80211_MFIE_Brate(ieee, &tag);
 939        ieee80211_MFIE_Grate(ieee, &tag);
 940
 941        return skb;
 942}
 943
 944static struct sk_buff *ieee80211_auth_resp(struct ieee80211_device *ieee,
 945                                           int status, u8 *dest)
 946{
 947        struct sk_buff *skb;
 948        struct ieee80211_authentication *auth;
 949
 950        skb = dev_alloc_skb(sizeof(struct ieee80211_authentication)+1);
 951
 952        if (!skb)
 953                return NULL;
 954
 955        skb->len = sizeof(struct ieee80211_authentication);
 956
 957        auth = (struct ieee80211_authentication *)skb->data;
 958
 959        auth->status = cpu_to_le16(status);
 960        auth->transaction = cpu_to_le16(2);
 961        auth->algorithm = cpu_to_le16(WLAN_AUTH_OPEN);
 962
 963        memcpy(auth->header.addr3, ieee->dev->dev_addr, ETH_ALEN);
 964        memcpy(auth->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
 965        memcpy(auth->header.addr1, dest, ETH_ALEN);
 966        auth->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_AUTH);
 967        return skb;
 968
 969
 970}
 971
 972struct sk_buff* ieee80211_null_func(struct ieee80211_device *ieee,short pwr)
 973{
 974        struct sk_buff *skb;
 975        struct ieee80211_hdr_3addr* hdr;
 976
 977        skb = dev_alloc_skb(sizeof(struct ieee80211_hdr_3addr));
 978
 979        if (!skb)
 980                return NULL;
 981
 982        hdr = (struct ieee80211_hdr_3addr*)skb_put(skb,sizeof(struct ieee80211_hdr_3addr));
 983
 984        memcpy(hdr->addr1, ieee->current_network.bssid, ETH_ALEN);
 985        memcpy(hdr->addr2, ieee->dev->dev_addr, ETH_ALEN);
 986        memcpy(hdr->addr3, ieee->current_network.bssid, ETH_ALEN);
 987
 988        hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
 989                IEEE80211_STYPE_NULLFUNC | IEEE80211_FCTL_TODS |
 990                (pwr ? IEEE80211_FCTL_PM:0));
 991
 992        return skb;
 993
 994
 995}
 996
 997
 998void ieee80211_resp_to_assoc_rq(struct ieee80211_device *ieee, u8* dest)
 999{
1000        struct sk_buff *buf = ieee80211_assoc_resp(ieee, dest);
1001
1002        if (buf){
1003                softmac_mgmt_xmit(buf, ieee);
1004                dev_kfree_skb_any(buf);//edit by thomas
1005        }
1006}
1007
1008
1009void ieee80211_resp_to_auth(struct ieee80211_device *ieee, int s, u8* dest)
1010{
1011        struct sk_buff *buf = ieee80211_auth_resp(ieee, s, dest);
1012
1013        if (buf){
1014                softmac_mgmt_xmit(buf, ieee);
1015                dev_kfree_skb_any(buf);//edit by thomas
1016        }
1017}
1018
1019
1020void ieee80211_resp_to_probe(struct ieee80211_device *ieee, u8 *dest)
1021{
1022
1023        struct sk_buff *buf = ieee80211_probe_resp(ieee, dest);
1024
1025        if (buf) {
1026                softmac_mgmt_xmit(buf, ieee);
1027                dev_kfree_skb_any(buf);//edit by thomas
1028        }
1029}
1030
1031
1032inline struct sk_buff *ieee80211_association_req(struct ieee80211_network *beacon,struct ieee80211_device *ieee)
1033{
1034        struct sk_buff *skb;
1035        //unsigned long flags;
1036
1037        struct ieee80211_assoc_request_frame *hdr;
1038        u8 *tag;
1039        //short info_addr = 0;
1040        //int i;
1041        //u16 suite_count = 0;
1042        //u8 suit_select = 0;
1043        unsigned int wpa_len = beacon->wpa_ie_len;
1044        //struct net_device *dev = ieee->dev;
1045        //union iwreq_data wrqu;
1046        //u8 *buff;
1047        //u8 *p;
1048#if 1
1049        // for testing purpose
1050        unsigned int rsn_len = beacon->rsn_ie_len;
1051#else
1052        unsigned int rsn_len = beacon->rsn_ie_len - 4;
1053#endif
1054        unsigned int rate_len = ieee80211_MFIE_rate_len(ieee);
1055        unsigned int wmm_info_len = beacon->QoS_Enable?9:0;
1056        unsigned int turbo_info_len = beacon->Turbo_Enable?9:0;
1057
1058        u8  encry_proto = ieee->wpax_type_notify & 0xff;
1059        //u8  pairwise_type = (ieee->wpax_type_notify >> 8) & 0xff;
1060        //u8  authen_type = (ieee->wpax_type_notify >> 16) & 0xff;
1061
1062        int len = 0;
1063
1064        //[0] Notify type of encryption: WPA/WPA2
1065        //[1] pair wise type
1066        //[2] authen type
1067        if(ieee->wpax_type_set) {
1068                if (IEEE_PROTO_WPA == encry_proto) {
1069                        rsn_len = 0;
1070                } else if (IEEE_PROTO_RSN == encry_proto) {
1071                        wpa_len = 0;
1072                }
1073        }
1074        len = sizeof(struct ieee80211_assoc_request_frame)+
1075                + beacon->ssid_len//essid tagged val
1076                + rate_len//rates tagged val
1077                + wpa_len
1078                + rsn_len
1079                + wmm_info_len
1080                + turbo_info_len;
1081
1082        skb = dev_alloc_skb(len);
1083
1084        if (!skb)
1085                return NULL;
1086
1087        hdr = (struct ieee80211_assoc_request_frame *)
1088                skb_put(skb, sizeof(struct ieee80211_assoc_request_frame));
1089
1090
1091        hdr->header.frame_control = IEEE80211_STYPE_ASSOC_REQ;
1092        hdr->header.duration_id= 37; //FIXME
1093        memcpy(hdr->header.addr1, beacon->bssid, ETH_ALEN);
1094        memcpy(hdr->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
1095        memcpy(hdr->header.addr3, beacon->bssid, ETH_ALEN);
1096        memcpy(ieee->ap_mac_addr, beacon->bssid, ETH_ALEN);//for HW security, John
1097
1098        hdr->capability = cpu_to_le16(WLAN_CAPABILITY_BSS);
1099        if (beacon->capability & WLAN_CAPABILITY_PRIVACY )
1100                hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1101        if (beacon->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1102                hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
1103
1104        if(ieee->short_slot)
1105                hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
1106
1107        hdr->listen_interval = 0xa; //FIXME
1108
1109        hdr->info_element.id = MFIE_TYPE_SSID;
1110
1111        hdr->info_element.len = beacon->ssid_len;
1112        tag = skb_put(skb, beacon->ssid_len);
1113        memcpy(tag, beacon->ssid, beacon->ssid_len);
1114
1115        tag = skb_put(skb, rate_len);
1116
1117        ieee80211_MFIE_Brate(ieee, &tag);
1118        ieee80211_MFIE_Grate(ieee, &tag);
1119
1120        //add rsn==0 condition for ap's mix security mode(wpa+wpa2), john2007.8.9
1121        //choose AES encryption as default algorithm while using mixed mode
1122
1123        tag = skb_put(skb,ieee->wpa_ie_len);
1124        memcpy(tag,ieee->wpa_ie,ieee->wpa_ie_len);
1125
1126        tag = skb_put(skb,wmm_info_len);
1127        if(wmm_info_len) {
1128          ieee80211_WMM_Info(ieee, &tag);
1129        }
1130        tag = skb_put(skb,turbo_info_len);
1131        if(turbo_info_len) {
1132                ieee80211_TURBO_Info(ieee, &tag);
1133        }
1134
1135        return skb;
1136}
1137
1138void ieee80211_associate_abort(struct ieee80211_device *ieee)
1139{
1140
1141        unsigned long flags;
1142        spin_lock_irqsave(&ieee->lock, flags);
1143
1144        ieee->associate_seq++;
1145
1146        /* don't scan, and avoid to have the RX path possibly
1147         * try again to associate. Even do not react to AUTH or
1148         * ASSOC response. Just wait for the retry wq to be scheduled.
1149         * Here we will check if there are good nets to associate
1150         * with, so we retry or just get back to NO_LINK and scanning
1151         */
1152        if (ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATING){
1153                IEEE80211_DEBUG_MGMT("Authentication failed\n");
1154                ieee->softmac_stats.no_auth_rs++;
1155        }else{
1156                IEEE80211_DEBUG_MGMT("Association failed\n");
1157                ieee->softmac_stats.no_ass_rs++;
1158        }
1159
1160        ieee->state = IEEE80211_ASSOCIATING_RETRY;
1161
1162        queue_delayed_work(ieee->wq, &ieee->associate_retry_wq,IEEE80211_SOFTMAC_ASSOC_RETRY_TIME);
1163
1164        spin_unlock_irqrestore(&ieee->lock, flags);
1165}
1166
1167void ieee80211_associate_abort_cb(unsigned long dev)
1168{
1169        ieee80211_associate_abort((struct ieee80211_device *) dev);
1170}
1171
1172
1173void ieee80211_associate_step1(struct ieee80211_device *ieee)
1174{
1175        struct ieee80211_network *beacon = &ieee->current_network;
1176        struct sk_buff *skb;
1177
1178        IEEE80211_DEBUG_MGMT("Stopping scan\n");
1179        ieee->softmac_stats.tx_auth_rq++;
1180        skb=ieee80211_authentication_req(beacon, ieee, 0);
1181        if (!skb){
1182
1183                ieee80211_associate_abort(ieee);
1184        }
1185        else{
1186                ieee->state = IEEE80211_ASSOCIATING_AUTHENTICATING ;
1187                IEEE80211_DEBUG_MGMT("Sending authentication request\n");
1188                //printk("---Sending authentication request\n");
1189                softmac_mgmt_xmit(skb, ieee);
1190                //BUGON when you try to add_timer twice, using mod_timer may be better, john0709
1191                if(!timer_pending(&ieee->associate_timer)){
1192                        ieee->associate_timer.expires = jiffies + (HZ / 2);
1193                        add_timer(&ieee->associate_timer);
1194                }
1195                //If call dev_kfree_skb_any,a warning will ocur....
1196                //KERNEL: assertion (!atomic_read(&skb->users)) failed at net/core/dev.c (1708)
1197                //So ... 1204 by lawrence.
1198                //printk("\nIn %s,line %d call kfree skb.",__func__,__LINE__);
1199                //dev_kfree_skb_any(skb);//edit by thomas
1200        }
1201}
1202
1203void ieee80211_rtl_auth_challenge(struct ieee80211_device *ieee, u8 *challenge, int chlen)
1204{
1205        u8 *c;
1206        struct sk_buff *skb;
1207        struct ieee80211_network *beacon = &ieee->current_network;
1208//      int hlen = sizeof(struct ieee80211_authentication);
1209        del_timer_sync(&ieee->associate_timer);
1210        ieee->associate_seq++;
1211        ieee->softmac_stats.tx_auth_rq++;
1212
1213        skb = ieee80211_authentication_req(beacon, ieee, chlen+2);
1214        if (!skb)
1215                ieee80211_associate_abort(ieee);
1216        else{
1217                c = skb_put(skb, chlen+2);
1218                *(c++) = MFIE_TYPE_CHALLENGE;
1219                *(c++) = chlen;
1220                memcpy(c, challenge, chlen);
1221
1222                IEEE80211_DEBUG_MGMT("Sending authentication challenge response\n");
1223
1224                ieee80211_encrypt_fragment(ieee, skb, sizeof(struct ieee80211_hdr_3addr  ));
1225
1226                softmac_mgmt_xmit(skb, ieee);
1227                if (!timer_pending(&ieee->associate_timer)){
1228                //printk("=========>add timer again, to crash\n");
1229                ieee->associate_timer.expires = jiffies + (HZ / 2);
1230                add_timer(&ieee->associate_timer);
1231                }
1232                dev_kfree_skb_any(skb);//edit by thomas
1233        }
1234        kfree(challenge);
1235}
1236
1237void ieee80211_associate_step2(struct ieee80211_device *ieee)
1238{
1239        struct sk_buff* skb;
1240        struct ieee80211_network *beacon = &ieee->current_network;
1241
1242        del_timer_sync(&ieee->associate_timer);
1243
1244        IEEE80211_DEBUG_MGMT("Sending association request\n");
1245        ieee->softmac_stats.tx_ass_rq++;
1246        skb=ieee80211_association_req(beacon, ieee);
1247        if (!skb)
1248                ieee80211_associate_abort(ieee);
1249        else{
1250                softmac_mgmt_xmit(skb, ieee);
1251                if (!timer_pending(&ieee->associate_timer)){
1252                ieee->associate_timer.expires = jiffies + (HZ / 2);
1253                add_timer(&ieee->associate_timer);
1254                }
1255                //dev_kfree_skb_any(skb);//edit by thomas
1256        }
1257}
1258
1259void ieee80211_associate_complete_wq(struct work_struct *work)
1260{
1261        struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, associate_complete_wq);
1262
1263        printk(KERN_INFO "Associated successfully\n");
1264        if(ieee80211_is_54g(&ieee->current_network) &&
1265                (ieee->modulation & IEEE80211_OFDM_MODULATION)){
1266
1267                ieee->rate = 540;
1268                printk(KERN_INFO"Using G rates\n");
1269        }else{
1270                ieee->rate = 110;
1271                printk(KERN_INFO"Using B rates\n");
1272        }
1273        ieee->link_change(ieee->dev);
1274        notify_wx_assoc_event(ieee);
1275        if (ieee->data_hard_resume)
1276                ieee->data_hard_resume(ieee->dev);
1277        netif_carrier_on(ieee->dev);
1278}
1279
1280void ieee80211_associate_complete(struct ieee80211_device *ieee)
1281{
1282        int i;
1283        del_timer_sync(&ieee->associate_timer);
1284
1285        for(i = 0; i < 6; i++) {
1286          //ieee->seq_ctrl[i] = 0;
1287        }
1288        ieee->state = IEEE80211_LINKED;
1289        IEEE80211_DEBUG_MGMT("Successfully associated\n");
1290
1291        queue_work(ieee->wq, &ieee->associate_complete_wq);
1292}
1293
1294void ieee80211_associate_procedure_wq(struct work_struct *work)
1295{
1296        struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, associate_procedure_wq);
1297
1298        ieee->sync_scan_hurryup = 1;
1299        down(&ieee->wx_sem);
1300
1301        if (ieee->data_hard_stop)
1302                ieee->data_hard_stop(ieee->dev);
1303
1304        ieee80211_stop_scan(ieee);
1305        ieee->set_chan(ieee->dev, ieee->current_network.channel);
1306
1307        ieee->associate_seq = 1;
1308        ieee80211_associate_step1(ieee);
1309
1310        up(&ieee->wx_sem);
1311}
1312
1313inline void ieee80211_softmac_new_net(struct ieee80211_device *ieee, struct ieee80211_network *net)
1314{
1315        u8 tmp_ssid[IW_ESSID_MAX_SIZE+1];
1316        int tmp_ssid_len = 0;
1317
1318        short apset,ssidset,ssidbroad,apmatch,ssidmatch;
1319
1320        /* we are interested in new new only if we are not associated
1321         * and we are not associating / authenticating
1322         */
1323        if (ieee->state != IEEE80211_NOLINK)
1324                return;
1325
1326        if ((ieee->iw_mode == IW_MODE_INFRA) && !(net->capability & WLAN_CAPABILITY_BSS))
1327                return;
1328
1329        if ((ieee->iw_mode == IW_MODE_ADHOC) && !(net->capability & WLAN_CAPABILITY_IBSS))
1330                return;
1331
1332
1333        if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC){
1334                /* if the user specified the AP MAC, we need also the essid
1335                 * This could be obtained by beacons or, if the network does not
1336                 * broadcast it, it can be put manually.
1337                 */
1338                apset = ieee->wap_set;//(memcmp(ieee->current_network.bssid, zero,ETH_ALEN)!=0 );
1339                ssidset = ieee->ssid_set;//ieee->current_network.ssid[0] != '\0';
1340                ssidbroad =  !(net->ssid_len == 0 || net->ssid[0]== '\0');
1341                apmatch = (memcmp(ieee->current_network.bssid, net->bssid, ETH_ALEN)==0);
1342
1343                if(ieee->current_network.ssid_len != net->ssid_len)
1344                        ssidmatch = 0;
1345                else
1346                        ssidmatch = (0==strncmp(ieee->current_network.ssid, net->ssid, net->ssid_len));
1347
1348                //printk("cur: %s, %d, net:%s, %d\n", ieee->current_network.ssid, ieee->current_network.ssid_len, net->ssid, net->ssid_len);
1349                //printk("apset=%d apmatch=%d ssidset=%d ssidbroad=%d ssidmatch=%d\n",apset,apmatch,ssidset,ssidbroad,ssidmatch);
1350
1351                if (    /* if the user set the AP check if match.
1352                         * if the network does not broadcast essid we check the user supplied ANY essid
1353                         * if the network does broadcast and the user does not set essid it is OK
1354                         * if the network does broadcast and the user did set essid chech if essid match
1355                         */
1356                                ( apset && apmatch &&
1357                                  ((ssidset && ssidbroad && ssidmatch) || (ssidbroad && !ssidset) || (!ssidbroad && ssidset)) ) ||
1358                                /* if the ap is not set, check that the user set the bssid
1359                                 * and the network does broadcast and that those two bssid matches
1360                                 */
1361                                (!apset && ssidset && ssidbroad && ssidmatch)
1362                   ){
1363
1364
1365                        /* if the essid is hidden replace it with the
1366                         * essid provided by the user.
1367                         */
1368                        if (!ssidbroad){
1369                                strncpy(tmp_ssid, ieee->current_network.ssid, IW_ESSID_MAX_SIZE);
1370                                tmp_ssid_len = ieee->current_network.ssid_len;
1371                        }
1372                        memcpy(&ieee->current_network, net, sizeof(struct ieee80211_network));
1373
1374                        if (!ssidbroad){
1375                                strncpy(ieee->current_network.ssid, tmp_ssid, IW_ESSID_MAX_SIZE);
1376                                ieee->current_network.ssid_len = tmp_ssid_len;
1377                        }
1378                        printk(KERN_INFO"Linking with %s: channel is %d\n",ieee->current_network.ssid,ieee->current_network.channel);
1379
1380                        if (ieee->iw_mode == IW_MODE_INFRA){
1381                                ieee->state = IEEE80211_ASSOCIATING;
1382                                ieee->beinretry = false;
1383                                queue_work(ieee->wq, &ieee->associate_procedure_wq);
1384                        }else{
1385                                if(ieee80211_is_54g(&ieee->current_network) &&
1386                                                (ieee->modulation & IEEE80211_OFDM_MODULATION)){
1387                                        ieee->rate = 540;
1388                                        printk(KERN_INFO"Using G rates\n");
1389                                }else{
1390                                        ieee->rate = 110;
1391                                        printk(KERN_INFO"Using B rates\n");
1392                                }
1393                                ieee->state = IEEE80211_LINKED;
1394                                ieee->beinretry = false;
1395                        }
1396
1397                }
1398        }
1399
1400}
1401
1402void ieee80211_softmac_check_all_nets(struct ieee80211_device *ieee)
1403{
1404        unsigned long flags;
1405        struct ieee80211_network *target;
1406
1407        spin_lock_irqsave(&ieee->lock, flags);
1408        list_for_each_entry(target, &ieee->network_list, list) {
1409
1410                /* if the state become different that NOLINK means
1411                 * we had found what we are searching for
1412                 */
1413
1414                if (ieee->state != IEEE80211_NOLINK)
1415                        break;
1416
1417                if (ieee->scan_age == 0 || time_after(target->last_scanned + ieee->scan_age, jiffies))
1418                        ieee80211_softmac_new_net(ieee, target);
1419        }
1420
1421        spin_unlock_irqrestore(&ieee->lock, flags);
1422
1423}
1424
1425
1426static inline u16 auth_parse(struct sk_buff *skb, u8** challenge, int *chlen)
1427{
1428        struct ieee80211_authentication *a;
1429        u8 *t;
1430        if (skb->len <  (sizeof(struct ieee80211_authentication)-sizeof(struct ieee80211_info_element))){
1431                IEEE80211_DEBUG_MGMT("invalid len in auth resp: %d\n",skb->len);
1432                return 0xcafe;
1433        }
1434        *challenge = NULL;
1435        a = (struct ieee80211_authentication*) skb->data;
1436        if(skb->len > (sizeof(struct ieee80211_authentication) +3)){
1437                t = skb->data + sizeof(struct ieee80211_authentication);
1438
1439                if(*(t++) == MFIE_TYPE_CHALLENGE){
1440                        *chlen = *(t++);
1441                        *challenge = kmemdup(t, *chlen, GFP_ATOMIC);
1442                        if (!*challenge)
1443                                return -ENOMEM;
1444                }
1445        }
1446
1447        return cpu_to_le16(a->status);
1448
1449}
1450
1451
1452int auth_rq_parse(struct sk_buff *skb,u8* dest)
1453{
1454        struct ieee80211_authentication *a;
1455
1456        if (skb->len <  (sizeof(struct ieee80211_authentication)-sizeof(struct ieee80211_info_element))){
1457                IEEE80211_DEBUG_MGMT("invalid len in auth request: %d\n",skb->len);
1458                return -1;
1459        }
1460        a = (struct ieee80211_authentication*) skb->data;
1461
1462        memcpy(dest,a->header.addr2, ETH_ALEN);
1463
1464        if (le16_to_cpu(a->algorithm) != WLAN_AUTH_OPEN)
1465                return  WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1466
1467        return WLAN_STATUS_SUCCESS;
1468}
1469
1470static short probe_rq_parse(struct ieee80211_device *ieee, struct sk_buff *skb, u8 *src)
1471{
1472        u8 *tag;
1473        u8 *skbend;
1474        u8 *ssid=NULL;
1475        u8 ssidlen = 0;
1476
1477        struct ieee80211_hdr_3addr   *header =
1478                (struct ieee80211_hdr_3addr   *) skb->data;
1479
1480        if (skb->len < sizeof (struct ieee80211_hdr_3addr  ))
1481                return -1; /* corrupted */
1482
1483        memcpy(src,header->addr2, ETH_ALEN);
1484
1485        skbend = (u8*)skb->data + skb->len;
1486
1487        tag = skb->data + sizeof (struct ieee80211_hdr_3addr  );
1488
1489        while (tag+1 < skbend){
1490                if (*tag == 0){
1491                        ssid = tag+2;
1492                        ssidlen = *(tag+1);
1493                        break;
1494                }
1495                tag++; /* point to the len field */
1496                tag = tag + *(tag); /* point to the last data byte of the tag */
1497                tag++; /* point to the next tag */
1498        }
1499
1500        //IEEE80211DMESG("Card MAC address is "MACSTR, MAC2STR(src));
1501        if (ssidlen == 0) return 1;
1502
1503        if (!ssid) return 1; /* ssid not found in tagged param */
1504        return (!strncmp(ssid, ieee->current_network.ssid, ssidlen));
1505
1506}
1507
1508int assoc_rq_parse(struct sk_buff *skb,u8* dest)
1509{
1510        struct ieee80211_assoc_request_frame *a;
1511
1512        if (skb->len < (sizeof(struct ieee80211_assoc_request_frame) -
1513                sizeof(struct ieee80211_info_element))) {
1514
1515                IEEE80211_DEBUG_MGMT("invalid len in auth request:%d \n", skb->len);
1516                return -1;
1517        }
1518
1519        a = (struct ieee80211_assoc_request_frame*) skb->data;
1520
1521        memcpy(dest,a->header.addr2,ETH_ALEN);
1522
1523        return 0;
1524}
1525
1526static inline u16 assoc_parse(struct sk_buff *skb, int *aid)
1527{
1528        struct ieee80211_assoc_response_frame *a;
1529        if (skb->len <  sizeof(struct ieee80211_assoc_response_frame)){
1530                IEEE80211_DEBUG_MGMT("invalid len in auth resp: %d\n", skb->len);
1531                return 0xcafe;
1532        }
1533
1534        a = (struct ieee80211_assoc_response_frame*) skb->data;
1535        *aid = le16_to_cpu(a->aid) & 0x3fff;
1536        return le16_to_cpu(a->status);
1537}
1538
1539static inline void
1540ieee80211_rx_probe_rq(struct ieee80211_device *ieee, struct sk_buff *skb)
1541{
1542        u8 dest[ETH_ALEN];
1543
1544        //IEEE80211DMESG("Rx probe");
1545        ieee->softmac_stats.rx_probe_rq++;
1546        //DMESG("Dest is "MACSTR, MAC2STR(dest));
1547        if (probe_rq_parse(ieee, skb, dest)){
1548                //IEEE80211DMESG("Was for me!");
1549                ieee->softmac_stats.tx_probe_rs++;
1550                ieee80211_resp_to_probe(ieee, dest);
1551        }
1552}
1553
1554inline void
1555ieee80211_rx_auth_rq(struct ieee80211_device *ieee, struct sk_buff *skb)
1556{
1557        u8 dest[ETH_ALEN];
1558        int status;
1559        //IEEE80211DMESG("Rx probe");
1560        ieee->softmac_stats.rx_auth_rq++;
1561
1562        status = auth_rq_parse(skb, dest);
1563        if (status != -1) {
1564                ieee80211_resp_to_auth(ieee, status, dest);
1565        }
1566        //DMESG("Dest is "MACSTR, MAC2STR(dest));
1567
1568}
1569
1570 inline void
1571ieee80211_rx_assoc_rq(struct ieee80211_device *ieee, struct sk_buff *skb)
1572{
1573
1574        u8 dest[ETH_ALEN];
1575        //unsigned long flags;
1576
1577        ieee->softmac_stats.rx_ass_rq++;
1578        if (assoc_rq_parse(skb,dest) != -1){
1579                ieee80211_resp_to_assoc_rq(ieee, dest);
1580        }
1581
1582        printk(KERN_INFO"New client associated: %pM\n", dest);
1583}
1584
1585
1586
1587void ieee80211_sta_ps_send_null_frame(struct ieee80211_device *ieee, short pwr)
1588{
1589
1590        struct sk_buff *buf = ieee80211_null_func(ieee, pwr);
1591
1592        if (buf)
1593                softmac_ps_mgmt_xmit(buf, ieee);
1594
1595}
1596
1597
1598short ieee80211_sta_ps_sleep(struct ieee80211_device *ieee, u32 *time_h, u32 *time_l)
1599{
1600        int timeout = 0;
1601
1602        u8 dtim;
1603        /*if(ieee->ps == IEEE80211_PS_DISABLED ||
1604                ieee->iw_mode != IW_MODE_INFRA ||
1605                ieee->state != IEEE80211_LINKED)
1606
1607                return 0;
1608        */
1609        dtim = ieee->current_network.dtim_data;
1610        //printk("DTIM\n");
1611
1612        if(!(dtim & IEEE80211_DTIM_VALID))
1613                return 0;
1614        else
1615                timeout = ieee->current_network.beacon_interval;
1616
1617        //printk("VALID\n");
1618        ieee->current_network.dtim_data = IEEE80211_DTIM_INVALID;
1619
1620        if(dtim & ((IEEE80211_DTIM_UCAST | IEEE80211_DTIM_MBCAST)& ieee->ps))
1621                return 2;
1622
1623        if(!time_after(jiffies, ieee->dev->trans_start + MSECS(timeout)))
1624                return 0;
1625
1626        if(!time_after(jiffies, ieee->last_rx_ps_time + MSECS(timeout)))
1627                return 0;
1628
1629        if((ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE ) &&
1630                (ieee->mgmt_queue_tail != ieee->mgmt_queue_head))
1631                return 0;
1632
1633        if(time_l){
1634                *time_l = ieee->current_network.last_dtim_sta_time[0]
1635                        + MSECS((ieee->current_network.beacon_interval));
1636                        //* ieee->current_network.dtim_period));
1637                        //printk("beacon_interval:%x, dtim_period:%x, totol to Msecs:%x, HZ:%x\n", ieee->current_network.beacon_interval, ieee->current_network.dtim_period, MSECS(((ieee->current_network.beacon_interval * ieee->current_network.dtim_period))), HZ);
1638        }
1639
1640        if(time_h){
1641                *time_h = ieee->current_network.last_dtim_sta_time[1];
1642                if(time_l && *time_l < ieee->current_network.last_dtim_sta_time[0])
1643                        *time_h += 1;
1644        }
1645
1646        return 1;
1647
1648
1649}
1650
1651inline void ieee80211_sta_ps(struct ieee80211_device *ieee)
1652{
1653
1654        u32 th,tl;
1655        short sleep;
1656
1657        unsigned long flags,flags2;
1658
1659        spin_lock_irqsave(&ieee->lock, flags);
1660
1661        if((ieee->ps == IEEE80211_PS_DISABLED ||
1662
1663                ieee->iw_mode != IW_MODE_INFRA ||
1664                ieee->state != IEEE80211_LINKED)){
1665
1666                //#warning CHECK_LOCK_HERE
1667                spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1668
1669                ieee80211_sta_wakeup(ieee, 1);
1670
1671                spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1672        }
1673
1674        sleep = ieee80211_sta_ps_sleep(ieee,&th, &tl);
1675//      printk("===>%s,%d[2 wake, 1 sleep, 0 do nothing], ieee->sta_sleep = %d\n",__func__, sleep,ieee->sta_sleep);
1676        /* 2 wake, 1 sleep, 0 do nothing */
1677        if(sleep == 0)
1678                goto out;
1679
1680        if(sleep == 1){
1681
1682                if(ieee->sta_sleep == 1)
1683                        ieee->enter_sleep_state(ieee->dev,th,tl);
1684
1685                else if(ieee->sta_sleep == 0){
1686        //              printk("send null 1\n");
1687                        spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1688
1689                        if(ieee->ps_is_queue_empty(ieee->dev)){
1690
1691
1692                                ieee->sta_sleep = 2;
1693
1694                                ieee->ps_request_tx_ack(ieee->dev);
1695
1696                                ieee80211_sta_ps_send_null_frame(ieee,1);
1697
1698                                ieee->ps_th = th;
1699                                ieee->ps_tl = tl;
1700                        }
1701                        spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1702
1703                }
1704
1705
1706        }else if(sleep == 2){
1707//#warning CHECK_LOCK_HERE
1708                spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1709
1710        //      printk("send wakeup packet\n");
1711                ieee80211_sta_wakeup(ieee,1);
1712
1713                spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1714        }
1715
1716out:
1717        spin_unlock_irqrestore(&ieee->lock, flags);
1718
1719}
1720
1721void ieee80211_sta_wakeup(struct ieee80211_device *ieee, short nl)
1722{
1723        if(ieee->sta_sleep == 0){
1724                if(nl){
1725                //      printk("Warning: driver is probably failing to report TX ps error\n");
1726                        ieee->ps_request_tx_ack(ieee->dev);
1727                        ieee80211_sta_ps_send_null_frame(ieee, 0);
1728                }
1729                return;
1730
1731        }
1732
1733        if(ieee->sta_sleep == 1)
1734                ieee->sta_wake_up(ieee->dev);
1735
1736        ieee->sta_sleep = 0;
1737
1738        if(nl){
1739                ieee->ps_request_tx_ack(ieee->dev);
1740                ieee80211_sta_ps_send_null_frame(ieee, 0);
1741        }
1742}
1743
1744void ieee80211_ps_tx_ack(struct ieee80211_device *ieee, short success)
1745{
1746        unsigned long flags,flags2;
1747
1748        spin_lock_irqsave(&ieee->lock, flags);
1749        if(ieee->sta_sleep == 2){
1750                /* Null frame with PS bit set */
1751                if(success){
1752
1753                //      printk("==================> %s::enter sleep state\n",__func__);
1754                        ieee->sta_sleep = 1;
1755                        ieee->enter_sleep_state(ieee->dev,ieee->ps_th,ieee->ps_tl);
1756                }
1757                /* if the card report not success we can't be sure the AP
1758                 * has not RXed so we can't assume the AP believe us awake
1759                 */
1760        }
1761        /* 21112005 - tx again null without PS bit if lost */
1762        else {
1763
1764                if((ieee->sta_sleep == 0) && !success){
1765                        spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1766                        ieee80211_sta_ps_send_null_frame(ieee, 0);
1767                        spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1768                }
1769        }
1770        spin_unlock_irqrestore(&ieee->lock, flags);
1771}
1772
1773inline int
1774ieee80211_rx_frame_softmac(struct ieee80211_device *ieee, struct sk_buff *skb,
1775                        struct ieee80211_rx_stats *rx_stats, u16 type,
1776                        u16 stype)
1777{
1778        struct ieee80211_hdr_3addr *header = (struct ieee80211_hdr_3addr *) skb->data;
1779        u16 errcode;
1780        u8* challenge=NULL;
1781        int chlen=0;
1782        int aid=0;
1783        struct ieee80211_assoc_response_frame *assoc_resp;
1784        struct ieee80211_info_element *info_element;
1785
1786        if(!ieee->proto_started)
1787                return 0;
1788
1789        if(ieee->sta_sleep || (ieee->ps != IEEE80211_PS_DISABLED &&
1790                ieee->iw_mode == IW_MODE_INFRA &&
1791                ieee->state == IEEE80211_LINKED))
1792
1793                tasklet_schedule(&ieee->ps_task);
1794
1795        if (WLAN_FC_GET_STYPE(header->frame_control) != IEEE80211_STYPE_PROBE_RESP &&
1796                WLAN_FC_GET_STYPE(header->frame_control) != IEEE80211_STYPE_BEACON)
1797                ieee->last_rx_ps_time = jiffies;
1798
1799        switch (WLAN_FC_GET_STYPE(header->frame_control)) {
1800
1801                case IEEE80211_STYPE_ASSOC_RESP:
1802                case IEEE80211_STYPE_REASSOC_RESP:
1803
1804                        IEEE80211_DEBUG_MGMT("received [RE]ASSOCIATION RESPONSE (%d)\n",
1805                                        WLAN_FC_GET_STYPE(header->frame_ctl));
1806                        if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1807                                ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATED &&
1808                                ieee->iw_mode == IW_MODE_INFRA){
1809                                if (0 == (errcode=assoc_parse(skb, &aid))){
1810                                        u16 left;
1811
1812                                        ieee->state=IEEE80211_LINKED;
1813                                        ieee->assoc_id = aid;
1814                                        ieee->softmac_stats.rx_ass_ok++;
1815
1816                                        //printk(KERN_WARNING "nic_type = %s", (rx_stats->nic_type == 1)?"rtl8187":"rtl8187B");
1817                                        if(1 == rx_stats->nic_type) //card type is 8187
1818                                        {
1819                                                goto associate_complete;
1820                                        }
1821                                        assoc_resp = (struct ieee80211_assoc_response_frame*)skb->data;
1822                                        info_element =  &assoc_resp->info_element;
1823                                        left = skb->len - ((void*)info_element - (void*)assoc_resp);
1824
1825                                        while (left >= sizeof(struct ieee80211_info_element_hdr)) {
1826                                                if (sizeof(struct ieee80211_info_element_hdr) + info_element->len > left) {
1827                                                        printk(KERN_WARNING "[re]associate response error!");
1828                                                        return 1;
1829                                                }
1830                                                switch (info_element->id) {
1831                                                  case MFIE_TYPE_GENERIC:
1832                                                         IEEE80211_DEBUG_SCAN("MFIE_TYPE_GENERIC: %d bytes\n", info_element->len);
1833                                                        if (info_element->len >= 8  &&
1834                                                            info_element->data[0] == 0x00 &&
1835                                                            info_element->data[1] == 0x50 &&
1836                                                            info_element->data[2] == 0xf2 &&
1837                                                            info_element->data[3] == 0x02 &&
1838                                                            info_element->data[4] == 0x01) {
1839                                                            // Not care about version at present.
1840                                                            //WMM Parameter Element
1841                                                            memcpy(ieee->current_network.wmm_param,(u8*)(info_element->data\
1842                                                                                    + 8),(info_element->len - 8));
1843
1844                                                            if (((ieee->current_network.wmm_info^info_element->data[6])& \
1845                                                                                    0x0f)||(!ieee->init_wmmparam_flag)) {
1846                                                              // refresh parameter element for current network
1847                                                              // update the register parameter for hardware
1848                                                              ieee->init_wmmparam_flag = 1;
1849                                                              queue_work(ieee->wq, &ieee->wmm_param_update_wq);
1850
1851                                                            }
1852                                                            //update info_element for current network
1853                                                            ieee->current_network.wmm_info  = info_element->data[6];
1854                                                        }
1855                                                        break;
1856                                                  default:
1857                                                        //nothing to do at present!!!
1858                                                        break;
1859                                                }
1860
1861                                                left -= sizeof(struct ieee80211_info_element_hdr) +
1862                                                        info_element->len;
1863                                                info_element = (struct ieee80211_info_element *)
1864                                                        &info_element->data[info_element->len];
1865                                        }
1866                                        if(!ieee->init_wmmparam_flag) //legacy AP, reset the AC_xx_param register
1867                                        {
1868                                                queue_work(ieee->wq,&ieee->wmm_param_update_wq);
1869                                                ieee->init_wmmparam_flag = 1;//indicate AC_xx_param upated since last associate
1870                                        }
1871associate_complete:
1872                                        ieee80211_associate_complete(ieee);
1873                                }else{
1874                                        ieee->softmac_stats.rx_ass_err++;
1875                                        IEEE80211_DEBUG_MGMT(
1876                                                "Association response status code 0x%x\n",
1877                                                errcode);
1878                                        ieee80211_associate_abort(ieee);
1879                                }
1880                        }
1881                        break;
1882
1883                case IEEE80211_STYPE_ASSOC_REQ:
1884                case IEEE80211_STYPE_REASSOC_REQ:
1885
1886                        if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1887                                ieee->iw_mode == IW_MODE_MASTER)
1888
1889                                ieee80211_rx_assoc_rq(ieee, skb);
1890                        break;
1891
1892                case IEEE80211_STYPE_AUTH:
1893
1894                        if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE){
1895                                if (ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATING &&
1896                                ieee->iw_mode == IW_MODE_INFRA){
1897
1898                                                IEEE80211_DEBUG_MGMT("Received authentication response");
1899
1900                                                if (0 == (errcode=auth_parse(skb, &challenge, &chlen))){
1901                                                        if(ieee->open_wep || !challenge){
1902                                                                ieee->state = IEEE80211_ASSOCIATING_AUTHENTICATED;
1903                                                                ieee->softmac_stats.rx_auth_rs_ok++;
1904
1905                                                                ieee80211_associate_step2(ieee);
1906                                                        }else{
1907                                                                ieee80211_rtl_auth_challenge(ieee, challenge, chlen);
1908                                                        }
1909                                                }else{
1910                                                        ieee->softmac_stats.rx_auth_rs_err++;
1911                                                        IEEE80211_DEBUG_MGMT("Authentication response status code 0x%x",errcode);
1912                                                        ieee80211_associate_abort(ieee);
1913                                                }
1914
1915                                        }else if (ieee->iw_mode == IW_MODE_MASTER){
1916                                                ieee80211_rx_auth_rq(ieee, skb);
1917                                        }
1918                                }
1919                        break;
1920
1921                case IEEE80211_STYPE_PROBE_REQ:
1922
1923                        if ((ieee->softmac_features & IEEE_SOFTMAC_PROBERS) &&
1924                                ((ieee->iw_mode == IW_MODE_ADHOC ||
1925                                ieee->iw_mode == IW_MODE_MASTER) &&
1926                                ieee->state == IEEE80211_LINKED))
1927
1928                                ieee80211_rx_probe_rq(ieee, skb);
1929                        break;
1930
1931                case IEEE80211_STYPE_DISASSOC:
1932                case IEEE80211_STYPE_DEAUTH:
1933                        /* FIXME for now repeat all the association procedure
1934                        * both for disassociation and deauthentication
1935                        */
1936                        if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1937                                (ieee->state == IEEE80211_LINKED) &&
1938                                (ieee->iw_mode == IW_MODE_INFRA) &&
1939                                (!memcmp(header->addr2,ieee->current_network.bssid,ETH_ALEN))){
1940                                ieee->state = IEEE80211_ASSOCIATING;
1941                                ieee->softmac_stats.reassoc++;
1942
1943                                //notify_wx_assoc_event(ieee);  //YJ,del,080828, do not notify os here
1944                                queue_work(ieee->wq, &ieee->associate_procedure_wq);
1945                        }
1946
1947                        break;
1948
1949                default:
1950                        return -1;
1951                        break;
1952        }
1953
1954        //dev_kfree_skb_any(skb);
1955        return 0;
1956}
1957
1958
1959
1960/* following are for a simpler TX queue management.
1961 * Instead of using netif_[stop/wake]_queue the driver
1962 * will uses these two function (plus a reset one), that
1963 * will internally uses the kernel netif_* and takes
1964 * care of the ieee802.11 fragmentation.
1965 * So the driver receives a fragment per time and might
1966 * call the stop function when it want without take care
1967 * to have enough room to TX an entire packet.
1968 * This might be useful if each fragment need it's own
1969 * descriptor, thus just keep a total free memory > than
1970 * the max fragmentation threshold is not enough.. If the
1971 * ieee802.11 stack passed a TXB struct then you needed
1972 * to keep N free descriptors where
1973 * N = MAX_PACKET_SIZE / MIN_FRAG_TRESHOLD
1974 * In this way you need just one and the 802.11 stack
1975 * will take care of buffering fragments and pass them to
1976 * to the driver later, when it wakes the queue.
1977 */
1978
1979void ieee80211_softmac_xmit(struct ieee80211_txb *txb, struct ieee80211_device *ieee)
1980{
1981
1982
1983        unsigned long flags;
1984        int  i;
1985
1986        spin_lock_irqsave(&ieee->lock,flags);
1987
1988        /* called with 2nd parm 0, no tx mgmt lock required */
1989        ieee80211_sta_wakeup(ieee,0);
1990
1991        for(i = 0; i < txb->nr_frags; i++) {
1992
1993                if (ieee->queue_stop){
1994                        ieee->tx_pending.txb = txb;
1995                        ieee->tx_pending.frag = i;
1996                        goto exit;
1997                }else{
1998                        ieee->softmac_data_hard_start_xmit(
1999                                txb->fragments[i],
2000                                ieee->dev,ieee->rate);
2001                                //(i+1)<txb->nr_frags);
2002                        ieee->stats.tx_packets++;
2003                        ieee->stats.tx_bytes += txb->fragments[i]->len;
2004                        ieee->dev->trans_start = jiffies;
2005                }
2006        }
2007
2008        ieee80211_txb_free(txb);
2009
2010        exit:
2011        spin_unlock_irqrestore(&ieee->lock,flags);
2012
2013}
2014
2015/* called with ieee->lock acquired */
2016void ieee80211_resume_tx(struct ieee80211_device *ieee)
2017{
2018        int i;
2019        for(i = ieee->tx_pending.frag; i < ieee->tx_pending.txb->nr_frags; i++) {
2020
2021                if (ieee->queue_stop){
2022                        ieee->tx_pending.frag = i;
2023                        return;
2024                }else{
2025
2026                        ieee->softmac_data_hard_start_xmit(
2027                                ieee->tx_pending.txb->fragments[i],
2028                                ieee->dev,ieee->rate);
2029                                //(i+1)<ieee->tx_pending.txb->nr_frags);
2030                        ieee->stats.tx_packets++;
2031                        ieee->dev->trans_start = jiffies;
2032                }
2033        }
2034
2035
2036        ieee80211_txb_free(ieee->tx_pending.txb);
2037        ieee->tx_pending.txb = NULL;
2038}
2039
2040
2041void ieee80211_reset_queue(struct ieee80211_device *ieee)
2042{
2043        unsigned long flags;
2044
2045        spin_lock_irqsave(&ieee->lock,flags);
2046        init_mgmt_queue(ieee);
2047        if (ieee->tx_pending.txb){
2048                ieee80211_txb_free(ieee->tx_pending.txb);
2049                ieee->tx_pending.txb = NULL;
2050        }
2051        ieee->queue_stop = 0;
2052        spin_unlock_irqrestore(&ieee->lock,flags);
2053
2054}
2055
2056void ieee80211_rtl_wake_queue(struct ieee80211_device *ieee)
2057{
2058
2059        unsigned long flags;
2060        struct sk_buff *skb;
2061        struct ieee80211_hdr_3addr  *header;
2062
2063        spin_lock_irqsave(&ieee->lock,flags);
2064        if (! ieee->queue_stop) goto exit;
2065
2066        ieee->queue_stop = 0;
2067
2068        if(ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE){
2069                while (!ieee->queue_stop && (skb = dequeue_mgmt(ieee))){
2070
2071                        header = (struct ieee80211_hdr_3addr  *) skb->data;
2072
2073                        header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2074
2075                        if (ieee->seq_ctrl[0] == 0xFFF)
2076                                ieee->seq_ctrl[0] = 0;
2077                        else
2078                                ieee->seq_ctrl[0]++;
2079
2080                        //printk(KERN_ALERT "ieee80211_wake_queue \n");
2081                        ieee->softmac_data_hard_start_xmit(skb,ieee->dev,ieee->basic_rate);
2082                        dev_kfree_skb_any(skb);//edit by thomas
2083                }
2084        }
2085        if (!ieee->queue_stop && ieee->tx_pending.txb)
2086                ieee80211_resume_tx(ieee);
2087
2088        if (!ieee->queue_stop && netif_queue_stopped(ieee->dev)){
2089                ieee->softmac_stats.swtxawake++;
2090                netif_wake_queue(ieee->dev);
2091        }
2092
2093exit :
2094        spin_unlock_irqrestore(&ieee->lock,flags);
2095}
2096
2097
2098void ieee80211_rtl_stop_queue(struct ieee80211_device *ieee)
2099{
2100        //unsigned long flags;
2101        //spin_lock_irqsave(&ieee->lock,flags);
2102
2103        if (! netif_queue_stopped(ieee->dev)){
2104                netif_stop_queue(ieee->dev);
2105                ieee->softmac_stats.swtxstop++;
2106        }
2107        ieee->queue_stop = 1;
2108        //spin_unlock_irqrestore(&ieee->lock,flags);
2109
2110}
2111
2112
2113inline void ieee80211_randomize_cell(struct ieee80211_device *ieee)
2114{
2115
2116        random_ether_addr(ieee->current_network.bssid);
2117}
2118
2119/* called in user context only */
2120void ieee80211_start_master_bss(struct ieee80211_device *ieee)
2121{
2122        ieee->assoc_id = 1;
2123
2124        if (ieee->current_network.ssid_len == 0){
2125                strncpy(ieee->current_network.ssid,
2126                        IEEE80211_DEFAULT_TX_ESSID,
2127                        IW_ESSID_MAX_SIZE);
2128
2129                ieee->current_network.ssid_len = strlen(IEEE80211_DEFAULT_TX_ESSID);
2130                ieee->ssid_set = 1;
2131        }
2132
2133        memcpy(ieee->current_network.bssid, ieee->dev->dev_addr, ETH_ALEN);
2134
2135        ieee->set_chan(ieee->dev, ieee->current_network.channel);
2136        ieee->state = IEEE80211_LINKED;
2137        ieee->link_change(ieee->dev);
2138        notify_wx_assoc_event(ieee);
2139
2140        if (ieee->data_hard_resume)
2141                ieee->data_hard_resume(ieee->dev);
2142
2143        netif_carrier_on(ieee->dev);
2144}
2145
2146void ieee80211_start_monitor_mode(struct ieee80211_device *ieee)
2147{
2148        if(ieee->raw_tx){
2149
2150                if (ieee->data_hard_resume)
2151                        ieee->data_hard_resume(ieee->dev);
2152
2153                netif_carrier_on(ieee->dev);
2154        }
2155}
2156
2157void ieee80211_start_ibss_wq(struct work_struct *work)
2158{
2159        struct delayed_work *dwork = to_delayed_work(work);
2160        struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, start_ibss_wq);
2161
2162        /* iwconfig mode ad-hoc will schedule this and return
2163         * on the other hand this will block further iwconfig SET
2164         * operations because of the wx_sem hold.
2165         * Anyway some most set operations set a flag to speed-up
2166         * (abort) this wq (when syncro scanning) before sleeping
2167         * on the semaphore
2168         */
2169
2170        down(&ieee->wx_sem);
2171
2172
2173        if (ieee->current_network.ssid_len == 0){
2174                strcpy(ieee->current_network.ssid,IEEE80211_DEFAULT_TX_ESSID);
2175                ieee->current_network.ssid_len = strlen(IEEE80211_DEFAULT_TX_ESSID);
2176                ieee->ssid_set = 1;
2177        }
2178
2179        /* check if we have this cell in our network list */
2180        ieee80211_softmac_check_all_nets(ieee);
2181
2182        if(ieee->state == IEEE80211_NOLINK)
2183                ieee->current_network.channel = 10;
2184        /* if not then the state is not linked. Maybe the user switched to
2185         * ad-hoc mode just after being in monitor mode, or just after
2186         * being very few time in managed mode (so the card have had no
2187         * time to scan all the chans..) or we have just run up the iface
2188         * after setting ad-hoc mode. So we have to give another try..
2189         * Here, in ibss mode, should be safe to do this without extra care
2190         * (in bss mode we had to make sure no-one tried to associate when
2191         * we had just checked the ieee->state and we was going to start the
2192         * scan) because in ibss mode the ieee80211_new_net function, when
2193         * finds a good net, just set the ieee->state to IEEE80211_LINKED,
2194         * so, at worst, we waste a bit of time to initiate an unneeded syncro
2195         * scan, that will stop at the first round because it sees the state
2196         * associated.
2197         */
2198        if (ieee->state == IEEE80211_NOLINK)
2199                ieee80211_start_scan_syncro(ieee);
2200
2201        /* the network definitively is not here.. create a new cell */
2202        if (ieee->state == IEEE80211_NOLINK){
2203                printk("creating new IBSS cell\n");
2204                if(!ieee->wap_set)
2205                        ieee80211_randomize_cell(ieee);
2206
2207                if(ieee->modulation & IEEE80211_CCK_MODULATION){
2208
2209                        ieee->current_network.rates_len = 4;
2210
2211                        ieee->current_network.rates[0] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
2212                        ieee->current_network.rates[1] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
2213                        ieee->current_network.rates[2] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
2214                        ieee->current_network.rates[3] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
2215
2216                }else
2217                        ieee->current_network.rates_len = 0;
2218
2219                if(ieee->modulation & IEEE80211_OFDM_MODULATION){
2220                        ieee->current_network.rates_ex_len = 8;
2221
2222                        ieee->current_network.rates_ex[0] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
2223                        ieee->current_network.rates_ex[1] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
2224                        ieee->current_network.rates_ex[2] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
2225                        ieee->current_network.rates_ex[3] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
2226                        ieee->current_network.rates_ex[4] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
2227                        ieee->current_network.rates_ex[5] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
2228                        ieee->current_network.rates_ex[6] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
2229                        ieee->current_network.rates_ex[7] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
2230
2231                        ieee->rate = 540;
2232                }else{
2233                        ieee->current_network.rates_ex_len = 0;
2234                        ieee->rate = 110;
2235                }
2236
2237                // By default, WMM function will be disabled in IBSS mode
2238                ieee->current_network.QoS_Enable = 0;
2239
2240                ieee->current_network.atim_window = 0;
2241                ieee->current_network.capability = WLAN_CAPABILITY_IBSS;
2242                if(ieee->short_slot)
2243                        ieee->current_network.capability |= WLAN_CAPABILITY_SHORT_SLOT;
2244
2245        }
2246
2247        ieee->state = IEEE80211_LINKED;
2248        ieee->set_chan(ieee->dev, ieee->current_network.channel);
2249        ieee->link_change(ieee->dev);
2250
2251        notify_wx_assoc_event(ieee);
2252
2253        ieee80211_start_send_beacons(ieee);
2254        printk(KERN_WARNING "after sending beacon packet!\n");
2255
2256        if (ieee->data_hard_resume)
2257                ieee->data_hard_resume(ieee->dev);
2258
2259        netif_carrier_on(ieee->dev);
2260
2261        up(&ieee->wx_sem);
2262}
2263inline void ieee80211_start_ibss(struct ieee80211_device *ieee)
2264{
2265        queue_delayed_work(ieee->wq, &ieee->start_ibss_wq, 100);
2266}
2267
2268/* this is called only in user context, with wx_sem held */
2269void ieee80211_start_bss(struct ieee80211_device *ieee)
2270{
2271        unsigned long flags;
2272        //
2273        // Ref: 802.11d 11.1.3.3
2274        // STA shall not start a BSS unless properly formed Beacon frame including a Country IE.
2275        //
2276        if(IS_DOT11D_ENABLE(ieee) && !IS_COUNTRY_IE_VALID(ieee))
2277        {
2278                if(! ieee->bGlobalDomain)
2279                {
2280                        return;
2281                }
2282        }
2283        /* check if we have already found the net we
2284         * are interested in (if any).
2285         * if not (we are disassociated and we are not
2286         * in associating / authenticating phase) start the background scanning.
2287         */
2288        ieee80211_softmac_check_all_nets(ieee);
2289
2290        /* ensure no-one start an associating process (thus setting
2291         * the ieee->state to ieee80211_ASSOCIATING) while we
2292         * have just cheked it and we are going to enable scan.
2293         * The ieee80211_new_net function is always called with
2294         * lock held (from both ieee80211_softmac_check_all_nets and
2295         * the rx path), so we cannot be in the middle of such function
2296         */
2297        spin_lock_irqsave(&ieee->lock, flags);
2298
2299//#ifdef ENABLE_IPS
2300//      printk("start bss ENABLE_IPS\n");
2301//#else
2302        if (ieee->state == IEEE80211_NOLINK){
2303                ieee->actscanning = true;
2304                ieee80211_rtl_start_scan(ieee);
2305        }
2306//#endif
2307        spin_unlock_irqrestore(&ieee->lock, flags);
2308}
2309
2310/* called only in userspace context */
2311void ieee80211_disassociate(struct ieee80211_device *ieee)
2312{
2313        netif_carrier_off(ieee->dev);
2314
2315        if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)
2316                        ieee80211_reset_queue(ieee);
2317
2318        if (ieee->data_hard_stop)
2319                        ieee->data_hard_stop(ieee->dev);
2320
2321        if(IS_DOT11D_ENABLE(ieee))
2322                Dot11d_Reset(ieee);
2323
2324        ieee->link_change(ieee->dev);
2325        if (ieee->state == IEEE80211_LINKED)
2326                notify_wx_assoc_event(ieee);
2327        ieee->state = IEEE80211_NOLINK;
2328
2329}
2330void ieee80211_associate_retry_wq(struct work_struct *work)
2331{
2332        struct delayed_work *dwork = to_delayed_work(work);
2333        struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, associate_retry_wq);
2334        unsigned long flags;
2335        down(&ieee->wx_sem);
2336        if(!ieee->proto_started)
2337                goto exit;
2338        if(ieee->state != IEEE80211_ASSOCIATING_RETRY)
2339                goto exit;
2340        /* until we do not set the state to IEEE80211_NOLINK
2341        * there are no possibility to have someone else trying
2342        * to start an association procedure (we get here with
2343        * ieee->state = IEEE80211_ASSOCIATING).
2344        * When we set the state to IEEE80211_NOLINK it is possible
2345        * that the RX path run an attempt to associate, but
2346        * both ieee80211_softmac_check_all_nets and the
2347        * RX path works with ieee->lock held so there are no
2348        * problems. If we are still disassociated then start a scan.
2349        * the lock here is necessary to ensure no one try to start
2350        * an association procedure when we have just checked the
2351        * state and we are going to start the scan.
2352        */
2353        ieee->state = IEEE80211_NOLINK;
2354        ieee->beinretry = true;
2355        ieee80211_softmac_check_all_nets(ieee);
2356
2357        spin_lock_irqsave(&ieee->lock, flags);
2358
2359        if(ieee->state == IEEE80211_NOLINK){
2360                ieee->beinretry = false;
2361                ieee->actscanning = true;
2362                ieee80211_rtl_start_scan(ieee);
2363        }
2364        //YJ,add,080828, notify os here
2365        if(ieee->state == IEEE80211_NOLINK)
2366        {
2367                notify_wx_assoc_event(ieee);
2368        }
2369        //YJ,add,080828,end
2370        spin_unlock_irqrestore(&ieee->lock, flags);
2371
2372exit:
2373        up(&ieee->wx_sem);
2374}
2375
2376struct sk_buff *ieee80211_get_beacon_(struct ieee80211_device *ieee)
2377{
2378        u8 broadcast_addr[] = {0xff,0xff,0xff,0xff,0xff,0xff};
2379
2380        struct sk_buff *skb = NULL;
2381        struct ieee80211_probe_response *b;
2382
2383        skb = ieee80211_probe_resp(ieee, broadcast_addr);
2384        if (!skb)
2385                return NULL;
2386
2387        b = (struct ieee80211_probe_response *) skb->data;
2388        b->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_BEACON);
2389
2390        return skb;
2391
2392}
2393
2394struct sk_buff *ieee80211_get_beacon(struct ieee80211_device *ieee)
2395{
2396        struct sk_buff *skb;
2397        struct ieee80211_probe_response *b;
2398
2399        skb = ieee80211_get_beacon_(ieee);
2400        if(!skb)
2401                return NULL;
2402
2403        b = (struct ieee80211_probe_response *) skb->data;
2404        b->header.seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2405
2406        if (ieee->seq_ctrl[0] == 0xFFF)
2407                ieee->seq_ctrl[0] = 0;
2408        else
2409                ieee->seq_ctrl[0]++;
2410
2411        return skb;
2412}
2413
2414void ieee80211_softmac_stop_protocol(struct ieee80211_device *ieee)
2415{
2416        ieee->sync_scan_hurryup = 1;
2417        down(&ieee->wx_sem);
2418        ieee80211_stop_protocol(ieee);
2419        up(&ieee->wx_sem);
2420}
2421
2422
2423void ieee80211_stop_protocol(struct ieee80211_device *ieee)
2424{
2425        if (!ieee->proto_started)
2426                return;
2427
2428        ieee->proto_started = 0;
2429
2430        ieee80211_stop_send_beacons(ieee);
2431        if((ieee->iw_mode == IW_MODE_INFRA)&&(ieee->state == IEEE80211_LINKED)) {
2432                SendDisassociation(ieee,NULL,WLAN_REASON_DISASSOC_STA_HAS_LEFT);
2433        }
2434        del_timer_sync(&ieee->associate_timer);
2435        cancel_delayed_work(&ieee->associate_retry_wq);
2436        cancel_delayed_work(&ieee->start_ibss_wq);
2437        ieee80211_stop_scan(ieee);
2438
2439        ieee80211_disassociate(ieee);
2440}
2441
2442void ieee80211_softmac_start_protocol(struct ieee80211_device *ieee)
2443{
2444        ieee->sync_scan_hurryup = 0;
2445        down(&ieee->wx_sem);
2446        ieee80211_start_protocol(ieee);
2447        up(&ieee->wx_sem);
2448}
2449
2450void ieee80211_start_protocol(struct ieee80211_device *ieee)
2451{
2452        short ch = 0;
2453        int i = 0;
2454
2455        if (ieee->proto_started)
2456                return;
2457
2458        ieee->proto_started = 1;
2459
2460        if (ieee->current_network.channel == 0){
2461                do{
2462                        ch++;
2463                        if (ch > MAX_CHANNEL_NUMBER)
2464                                return; /* no channel found */
2465
2466                }while(!GET_DOT11D_INFO(ieee)->channel_map[ch]);
2467
2468                ieee->current_network.channel = ch;
2469        }
2470
2471        if (ieee->current_network.beacon_interval == 0)
2472                ieee->current_network.beacon_interval = 100;
2473        ieee->set_chan(ieee->dev,ieee->current_network.channel);
2474
2475        for(i = 0; i < 17; i++) {
2476          ieee->last_rxseq_num[i] = -1;
2477          ieee->last_rxfrag_num[i] = -1;
2478          ieee->last_packet_time[i] = 0;
2479        }
2480
2481        ieee->init_wmmparam_flag = 0;//reinitialize AC_xx_PARAM registers.
2482
2483
2484        /* if the user set the MAC of the ad-hoc cell and then
2485         * switch to managed mode, shall we  make sure that association
2486         * attempts does not fail just because the user provide the essid
2487         * and the nic is still checking for the AP MAC ??
2488         */
2489        switch (ieee->iw_mode) {
2490                case IW_MODE_AUTO:
2491                        ieee->iw_mode = IW_MODE_INFRA;
2492                        //not set break here intentionly
2493                case IW_MODE_INFRA:
2494                        ieee80211_start_bss(ieee);
2495                        break;
2496
2497                case IW_MODE_ADHOC:
2498                        ieee80211_start_ibss(ieee);
2499                        break;
2500
2501                case IW_MODE_MASTER:
2502                        ieee80211_start_master_bss(ieee);
2503                break;
2504
2505                case IW_MODE_MONITOR:
2506                        ieee80211_start_monitor_mode(ieee);
2507                        break;
2508
2509                default:
2510                        ieee->iw_mode = IW_MODE_INFRA;
2511                        ieee80211_start_bss(ieee);
2512                        break;
2513        }
2514}
2515
2516
2517#define DRV_NAME  "Ieee80211"
2518void ieee80211_softmac_init(struct ieee80211_device *ieee)
2519{
2520        int i;
2521        memset(&ieee->current_network, 0, sizeof(struct ieee80211_network));
2522
2523        ieee->state = IEEE80211_NOLINK;
2524        ieee->sync_scan_hurryup = 0;
2525        for(i = 0; i < 5; i++) {
2526          ieee->seq_ctrl[i] = 0;
2527        }
2528
2529        ieee->assoc_id = 0;
2530        ieee->queue_stop = 0;
2531        ieee->scanning = 0;
2532        ieee->softmac_features = 0; //so IEEE2100-like driver are happy
2533        ieee->wap_set = 0;
2534        ieee->ssid_set = 0;
2535        ieee->proto_started = 0;
2536        ieee->basic_rate = IEEE80211_DEFAULT_BASIC_RATE;
2537        ieee->rate = 3;
2538//#ifdef ENABLE_LPS
2539        ieee->ps = IEEE80211_PS_MBCAST|IEEE80211_PS_UNICAST;
2540//#else
2541//      ieee->ps = IEEE80211_PS_DISABLED;
2542//#endif
2543        ieee->sta_sleep = 0;
2544//by amy
2545        ieee->bInactivePs = false;
2546        ieee->actscanning = false;
2547        ieee->ListenInterval = 2;
2548        ieee->NumRxDataInPeriod = 0; //YJ,add,080828
2549        ieee->NumRxBcnInPeriod = 0; //YJ,add,080828
2550        ieee->NumRxOkTotal = 0;//+by amy 080312
2551        ieee->NumRxUnicast = 0;//YJ,add,080828,for keep alive
2552        ieee->beinretry = false;
2553        ieee->bHwRadioOff = false;
2554//by amy
2555
2556        init_mgmt_queue(ieee);
2557
2558        ieee->tx_pending.txb = NULL;
2559
2560        init_timer(&ieee->associate_timer);
2561        ieee->associate_timer.data = (unsigned long)ieee;
2562        ieee->associate_timer.function = ieee80211_associate_abort_cb;
2563
2564        init_timer(&ieee->beacon_timer);
2565        ieee->beacon_timer.data = (unsigned long) ieee;
2566        ieee->beacon_timer.function = ieee80211_send_beacon_cb;
2567
2568        ieee->wq = create_workqueue(DRV_NAME);
2569
2570        INIT_DELAYED_WORK(&ieee->start_ibss_wq,(void*) ieee80211_start_ibss_wq);
2571        INIT_WORK(&ieee->associate_complete_wq,(void*) ieee80211_associate_complete_wq);
2572        INIT_WORK(&ieee->associate_procedure_wq,(void*) ieee80211_associate_procedure_wq);
2573        INIT_DELAYED_WORK(&ieee->softmac_scan_wq,(void*) ieee80211_softmac_scan_wq);
2574        INIT_DELAYED_WORK(&ieee->associate_retry_wq,(void*) ieee80211_associate_retry_wq);
2575        INIT_WORK(&ieee->wx_sync_scan_wq,(void*) ieee80211_wx_sync_scan_wq);
2576//      INIT_WORK(&ieee->watch_dog_wq,(void*) ieee80211_watch_dog_wq);
2577
2578        sema_init(&ieee->wx_sem, 1);
2579        sema_init(&ieee->scan_sem, 1);
2580
2581        spin_lock_init(&ieee->mgmt_tx_lock);
2582        spin_lock_init(&ieee->beacon_lock);
2583
2584        tasklet_init(&ieee->ps_task,
2585             (void(*)(unsigned long)) ieee80211_sta_ps,
2586             (unsigned long)ieee);
2587        ieee->pDot11dInfo = kmalloc(sizeof(RT_DOT11D_INFO), GFP_ATOMIC);
2588}
2589
2590void ieee80211_softmac_free(struct ieee80211_device *ieee)
2591{
2592        down(&ieee->wx_sem);
2593
2594        del_timer_sync(&ieee->associate_timer);
2595        cancel_delayed_work(&ieee->associate_retry_wq);
2596
2597
2598        //add for RF power on power of by lizhaoming 080512
2599        cancel_delayed_work(&ieee->GPIOChangeRFWorkItem);
2600
2601        destroy_workqueue(ieee->wq);
2602        kfree(ieee->pDot11dInfo);
2603        up(&ieee->wx_sem);
2604}
2605
2606/********************************************************
2607 * Start of WPA code.                                   *
2608 * this is stolen from the ipw2200 driver               *
2609 ********************************************************/
2610
2611
2612static int ieee80211_wpa_enable(struct ieee80211_device *ieee, int value)
2613{
2614        /* This is called when wpa_supplicant loads and closes the driver
2615         * interface. */
2616        printk("%s WPA\n",value ? "enabling" : "disabling");
2617        ieee->wpa_enabled = value;
2618        return 0;
2619}
2620
2621
2622void ieee80211_wpa_assoc_frame(struct ieee80211_device *ieee, char *wpa_ie, int wpa_ie_len)
2623{
2624        /* make sure WPA is enabled */
2625        ieee80211_wpa_enable(ieee, 1);
2626
2627        ieee80211_disassociate(ieee);
2628}
2629
2630
2631static int ieee80211_wpa_mlme(struct ieee80211_device *ieee, int command, int reason)
2632{
2633
2634        int ret = 0;
2635
2636        switch (command) {
2637        case IEEE_MLME_STA_DEAUTH:
2638                // silently ignore
2639                break;
2640
2641        case IEEE_MLME_STA_DISASSOC:
2642                ieee80211_disassociate(ieee);
2643                break;
2644
2645        default:
2646                printk("Unknown MLME request: %d\n", command);
2647                ret = -EOPNOTSUPP;
2648        }
2649
2650        return ret;
2651}
2652
2653
2654static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee,
2655                              struct ieee_param *param, int plen)
2656{
2657        u8 *buf;
2658
2659        if (param->u.wpa_ie.len > MAX_WPA_IE_LEN ||
2660            (param->u.wpa_ie.len && param->u.wpa_ie.data == NULL))
2661                return -EINVAL;
2662
2663        if (param->u.wpa_ie.len) {
2664                buf = kmemdup(param->u.wpa_ie.data, param->u.wpa_ie.len,
2665                              GFP_KERNEL);
2666                if (buf == NULL)
2667                        return -ENOMEM;
2668
2669                kfree(ieee->wpa_ie);
2670                ieee->wpa_ie = buf;
2671                ieee->wpa_ie_len = param->u.wpa_ie.len;
2672        } else {
2673                kfree(ieee->wpa_ie);
2674                ieee->wpa_ie = NULL;
2675                ieee->wpa_ie_len = 0;
2676        }
2677
2678        ieee80211_wpa_assoc_frame(ieee, ieee->wpa_ie, ieee->wpa_ie_len);
2679        return 0;
2680}
2681
2682#define AUTH_ALG_OPEN_SYSTEM                    0x1
2683#define AUTH_ALG_SHARED_KEY                     0x2
2684
2685static int ieee80211_wpa_set_auth_algs(struct ieee80211_device *ieee, int value)
2686{
2687
2688        struct ieee80211_security sec = {
2689                .flags = SEC_AUTH_MODE,
2690        };
2691        int ret = 0;
2692
2693        if (value & AUTH_ALG_SHARED_KEY) {
2694                sec.auth_mode = WLAN_AUTH_SHARED_KEY;
2695                ieee->open_wep = 0;
2696        } else {
2697                sec.auth_mode = WLAN_AUTH_OPEN;
2698                ieee->open_wep = 1;
2699        }
2700
2701        if (ieee->set_security)
2702                ieee->set_security(ieee->dev, &sec);
2703        else
2704                ret = -EOPNOTSUPP;
2705
2706        return ret;
2707}
2708
2709static int ieee80211_wpa_set_param(struct ieee80211_device *ieee, u8 name, u32 value)
2710{
2711        int ret=0;
2712        unsigned long flags;
2713
2714        switch (name) {
2715        case IEEE_PARAM_WPA_ENABLED:
2716                ret = ieee80211_wpa_enable(ieee, value);
2717                break;
2718
2719        case IEEE_PARAM_TKIP_COUNTERMEASURES:
2720                ieee->tkip_countermeasures=value;
2721                break;
2722
2723        case IEEE_PARAM_DROP_UNENCRYPTED: {
2724                /* HACK:
2725                 *
2726                 * wpa_supplicant calls set_wpa_enabled when the driver
2727                 * is loaded and unloaded, regardless of if WPA is being
2728                 * used.  No other calls are made which can be used to
2729                 * determine if encryption will be used or not prior to
2730                 * association being expected.  If encryption is not being
2731                 * used, drop_unencrypted is set to false, else true -- we
2732                 * can use this to determine if the CAP_PRIVACY_ON bit should
2733                 * be set.
2734                 */
2735                struct ieee80211_security sec = {
2736                        .flags = SEC_ENABLED,
2737                        .enabled = value,
2738                };
2739                ieee->drop_unencrypted = value;
2740                /* We only change SEC_LEVEL for open mode. Others
2741                 * are set by ipw_wpa_set_encryption.
2742                 */
2743                if (!value) {
2744                        sec.flags |= SEC_LEVEL;
2745                        sec.level = SEC_LEVEL_0;
2746                }
2747                else {
2748                        sec.flags |= SEC_LEVEL;
2749                        sec.level = SEC_LEVEL_1;
2750                }
2751                if (ieee->set_security)
2752                        ieee->set_security(ieee->dev, &sec);
2753                break;
2754        }
2755
2756        case IEEE_PARAM_PRIVACY_INVOKED:
2757                ieee->privacy_invoked=value;
2758                break;
2759
2760        case IEEE_PARAM_AUTH_ALGS:
2761                ret = ieee80211_wpa_set_auth_algs(ieee, value);
2762                break;
2763
2764        case IEEE_PARAM_IEEE_802_1X:
2765                ieee->ieee802_1x=value;
2766                break;
2767        case IEEE_PARAM_WPAX_SELECT:
2768                // added for WPA2 mixed mode
2769                //printk(KERN_WARNING "------------------------>wpax value = %x\n", value);
2770                spin_lock_irqsave(&ieee->wpax_suitlist_lock,flags);
2771                ieee->wpax_type_set = 1;
2772                ieee->wpax_type_notify = value;
2773                spin_unlock_irqrestore(&ieee->wpax_suitlist_lock,flags);
2774                break;
2775
2776        default:
2777                printk("Unknown WPA param: %d\n",name);
2778                ret = -EOPNOTSUPP;
2779        }
2780
2781        return ret;
2782}
2783
2784/* implementation borrowed from hostap driver */
2785
2786static int ieee80211_wpa_set_encryption(struct ieee80211_device *ieee,
2787                                  struct ieee_param *param, int param_len)
2788{
2789        int ret = 0;
2790
2791        struct ieee80211_crypto_ops *ops;
2792        struct ieee80211_crypt_data **crypt;
2793
2794        struct ieee80211_security sec = {
2795                .flags = 0,
2796        };
2797
2798        param->u.crypt.err = 0;
2799        param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0';
2800
2801        if (param_len !=
2802            (int) ((char *) param->u.crypt.key - (char *) param) +
2803            param->u.crypt.key_len) {
2804                printk("Len mismatch %d, %d\n", param_len,
2805                               param->u.crypt.key_len);
2806                return -EINVAL;
2807        }
2808        if (is_broadcast_ether_addr(param->sta_addr)) {
2809                if (param->u.crypt.idx >= WEP_KEYS)
2810                        return -EINVAL;
2811                crypt = &ieee->crypt[param->u.crypt.idx];
2812        } else {
2813                return -EINVAL;
2814        }
2815
2816        if (strcmp(param->u.crypt.alg, "none") == 0) {
2817                if (crypt) {
2818                        sec.enabled = 0;
2819                        // FIXME FIXME
2820                        //sec.encrypt = 0;
2821                        sec.level = SEC_LEVEL_0;
2822                        sec.flags |= SEC_ENABLED | SEC_LEVEL;
2823                        ieee80211_crypt_delayed_deinit(ieee, crypt);
2824                }
2825                goto done;
2826        }
2827        sec.enabled = 1;
2828// FIXME FIXME
2829//      sec.encrypt = 1;
2830        sec.flags |= SEC_ENABLED;
2831
2832        /* IPW HW cannot build TKIP MIC, host decryption still needed. */
2833        if (!(ieee->host_encrypt || ieee->host_decrypt) &&
2834            strcmp(param->u.crypt.alg, "TKIP"))
2835                goto skip_host_crypt;
2836
2837        ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2838        if (ops == NULL && strcmp(param->u.crypt.alg, "WEP") == 0)
2839                ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2840        else if (ops == NULL && strcmp(param->u.crypt.alg, "TKIP") == 0)
2841                ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2842        else if (ops == NULL && strcmp(param->u.crypt.alg, "CCMP") == 0)
2843                ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2844        if (ops == NULL) {
2845                printk("unknown crypto alg '%s'\n", param->u.crypt.alg);
2846                param->u.crypt.err = IEEE_CRYPT_ERR_UNKNOWN_ALG;
2847                ret = -EINVAL;
2848                goto done;
2849        }
2850
2851        if (*crypt == NULL || (*crypt)->ops != ops) {
2852                struct ieee80211_crypt_data *new_crypt;
2853
2854                ieee80211_crypt_delayed_deinit(ieee, crypt);
2855
2856                new_crypt = kmalloc(sizeof(*new_crypt), GFP_KERNEL);
2857                if (new_crypt == NULL) {
2858                        ret = -ENOMEM;
2859                        goto done;
2860                }
2861                memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
2862                new_crypt->ops = ops;
2863                if (new_crypt->ops)
2864                        new_crypt->priv =
2865                                new_crypt->ops->init(param->u.crypt.idx);
2866
2867                if (new_crypt->priv == NULL) {
2868                        kfree(new_crypt);
2869                        param->u.crypt.err = IEEE_CRYPT_ERR_CRYPT_INIT_FAILED;
2870                        ret = -EINVAL;
2871                        goto done;
2872                }
2873
2874                *crypt = new_crypt;
2875        }
2876
2877        if (param->u.crypt.key_len > 0 && (*crypt)->ops->set_key &&
2878            (*crypt)->ops->set_key(param->u.crypt.key,
2879                                   param->u.crypt.key_len, param->u.crypt.seq,
2880                                   (*crypt)->priv) < 0) {
2881                printk("key setting failed\n");
2882                param->u.crypt.err = IEEE_CRYPT_ERR_KEY_SET_FAILED;
2883                ret = -EINVAL;
2884                goto done;
2885        }
2886
2887 skip_host_crypt:
2888        if (param->u.crypt.set_tx) {
2889                ieee->tx_keyidx = param->u.crypt.idx;
2890                sec.active_key = param->u.crypt.idx;
2891                sec.flags |= SEC_ACTIVE_KEY;
2892        } else
2893                sec.flags &= ~SEC_ACTIVE_KEY;
2894
2895        if (param->u.crypt.alg != NULL) {
2896                memcpy(sec.keys[param->u.crypt.idx],
2897                       param->u.crypt.key,
2898                       param->u.crypt.key_len);
2899                sec.key_sizes[param->u.crypt.idx] = param->u.crypt.key_len;
2900                sec.flags |= (1 << param->u.crypt.idx);
2901
2902                if (strcmp(param->u.crypt.alg, "WEP") == 0) {
2903                        sec.flags |= SEC_LEVEL;
2904                        sec.level = SEC_LEVEL_1;
2905                } else if (strcmp(param->u.crypt.alg, "TKIP") == 0) {
2906                        sec.flags |= SEC_LEVEL;
2907                        sec.level = SEC_LEVEL_2;
2908                } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) {
2909                        sec.flags |= SEC_LEVEL;
2910                        sec.level = SEC_LEVEL_3;
2911                }
2912        }
2913 done:
2914        if (ieee->set_security)
2915                ieee->set_security(ieee->dev, &sec);
2916
2917        /* Do not reset port if card is in Managed mode since resetting will
2918         * generate new IEEE 802.11 authentication which may end up in looping
2919         * with IEEE 802.1X.  If your hardware requires a reset after WEP
2920         * configuration (for example... Prism2), implement the reset_port in
2921         * the callbacks structures used to initialize the 802.11 stack. */
2922        if (ieee->reset_on_keychange &&
2923            ieee->iw_mode != IW_MODE_INFRA &&
2924            ieee->reset_port &&
2925            ieee->reset_port(ieee->dev)) {
2926                printk("reset_port failed\n");
2927                param->u.crypt.err = IEEE_CRYPT_ERR_CARD_CONF_FAILED;
2928                return -EINVAL;
2929        }
2930
2931        return ret;
2932}
2933
2934int ieee80211_wpa_supplicant_ioctl(struct ieee80211_device *ieee, struct iw_point *p)
2935{
2936        struct ieee_param *param;
2937        int ret=0;
2938
2939        down(&ieee->wx_sem);
2940        //IEEE_DEBUG_INFO("wpa_supplicant: len=%d\n", p->length);
2941
2942        if (p->length < sizeof(struct ieee_param) || !p->pointer){
2943                ret = -EINVAL;
2944                goto out;
2945        }
2946
2947        param = memdup_user(p->pointer, p->length);
2948        if (IS_ERR(param)) {
2949                ret = PTR_ERR(param);
2950                goto out;
2951        }
2952
2953        switch (param->cmd) {
2954
2955        case IEEE_CMD_SET_WPA_PARAM:
2956                ret = ieee80211_wpa_set_param(ieee, param->u.wpa_param.name,
2957                                        param->u.wpa_param.value);
2958                break;
2959
2960        case IEEE_CMD_SET_WPA_IE:
2961                ret = ieee80211_wpa_set_wpa_ie(ieee, param, p->length);
2962                break;
2963
2964        case IEEE_CMD_SET_ENCRYPTION:
2965                ret = ieee80211_wpa_set_encryption(ieee, param, p->length);
2966                break;
2967
2968        case IEEE_CMD_MLME:
2969                ret = ieee80211_wpa_mlme(ieee, param->u.mlme.command,
2970                                   param->u.mlme.reason_code);
2971                break;
2972
2973        default:
2974                printk("Unknown WPA supplicant request: %d\n",param->cmd);
2975                ret = -EOPNOTSUPP;
2976                break;
2977        }
2978
2979        if (ret == 0 && copy_to_user(p->pointer, param, p->length))
2980                ret = -EFAULT;
2981
2982        kfree(param);
2983out:
2984        up(&ieee->wx_sem);
2985
2986        return ret;
2987}
2988
2989void notify_wx_assoc_event(struct ieee80211_device *ieee)
2990{
2991        union iwreq_data wrqu;
2992        wrqu.ap_addr.sa_family = ARPHRD_ETHER;
2993        if (ieee->state == IEEE80211_LINKED)
2994                memcpy(wrqu.ap_addr.sa_data, ieee->current_network.bssid, ETH_ALEN);
2995        else
2996                memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
2997        wireless_send_event(ieee->dev, SIOCGIWAP, &wrqu, NULL);
2998}
2999