Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* IEEE 802.11 SoftMAC layer
0003  * Copyright (c) 2005 Andrea Merello <andrea.merello@gmail.com>
0004  *
0005  * Mostly extracted from the rtl8180-sa2400 driver for the
0006  * in-kernel generic ieee802.11 stack.
0007  *
0008  * Few lines might be stolen from other part of the rtllib
0009  * stack. Copyright who own it's copyright
0010  *
0011  * WPA code stolen from the ipw2200 driver.
0012  * Copyright who own it's copyright.
0013  */
0014 #include "rtllib.h"
0015 
0016 #include <linux/random.h>
0017 #include <linux/delay.h>
0018 #include <linux/uaccess.h>
0019 #include <linux/etherdevice.h>
0020 #include <linux/ieee80211.h>
0021 #include "dot11d.h"
0022 
0023 static void rtllib_sta_wakeup(struct rtllib_device *ieee, short nl);
0024 
0025 
0026 static short rtllib_is_54g(struct rtllib_network *net)
0027 {
0028     return (net->rates_ex_len > 0) || (net->rates_len > 4);
0029 }
0030 
0031 /* returns the total length needed for placing the RATE MFIE
0032  * tag and the EXTENDED RATE MFIE tag if needed.
0033  * It encludes two bytes per tag for the tag itself and its len
0034  */
0035 static unsigned int rtllib_MFIE_rate_len(struct rtllib_device *ieee)
0036 {
0037     unsigned int rate_len = 0;
0038 
0039     if (ieee->modulation & RTLLIB_CCK_MODULATION)
0040         rate_len = RTLLIB_CCK_RATE_LEN + 2;
0041 
0042     if (ieee->modulation & RTLLIB_OFDM_MODULATION)
0043 
0044         rate_len += RTLLIB_OFDM_RATE_LEN + 2;
0045 
0046     return rate_len;
0047 }
0048 
0049 /* place the MFIE rate, tag to the memory (double) pointed.
0050  * Then it updates the pointer so that
0051  * it points after the new MFIE tag added.
0052  */
0053 static void rtllib_MFIE_Brate(struct rtllib_device *ieee, u8 **tag_p)
0054 {
0055     u8 *tag = *tag_p;
0056 
0057     if (ieee->modulation & RTLLIB_CCK_MODULATION) {
0058         *tag++ = MFIE_TYPE_RATES;
0059         *tag++ = 4;
0060         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_1MB;
0061         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_2MB;
0062         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_5MB;
0063         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_11MB;
0064     }
0065 
0066     /* We may add an option for custom rates that specific HW
0067      * might support
0068      */
0069     *tag_p = tag;
0070 }
0071 
0072 static void rtllib_MFIE_Grate(struct rtllib_device *ieee, u8 **tag_p)
0073 {
0074     u8 *tag = *tag_p;
0075 
0076     if (ieee->modulation & RTLLIB_OFDM_MODULATION) {
0077         *tag++ = MFIE_TYPE_RATES_EX;
0078         *tag++ = 8;
0079         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_6MB;
0080         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_9MB;
0081         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_12MB;
0082         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_18MB;
0083         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_24MB;
0084         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_36MB;
0085         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_48MB;
0086         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_54MB;
0087     }
0088     /* We may add an option for custom rates that specific HW might
0089      * support
0090      */
0091     *tag_p = tag;
0092 }
0093 
0094 static void rtllib_WMM_Info(struct rtllib_device *ieee, u8 **tag_p)
0095 {
0096     u8 *tag = *tag_p;
0097 
0098     *tag++ = MFIE_TYPE_GENERIC;
0099     *tag++ = 7;
0100     *tag++ = 0x00;
0101     *tag++ = 0x50;
0102     *tag++ = 0xf2;
0103     *tag++ = 0x02;
0104     *tag++ = 0x00;
0105     *tag++ = 0x01;
0106     *tag++ = MAX_SP_Len;
0107     *tag_p = tag;
0108 }
0109 
0110 static void rtllib_TURBO_Info(struct rtllib_device *ieee, u8 **tag_p)
0111 {
0112     u8 *tag = *tag_p;
0113 
0114     *tag++ = MFIE_TYPE_GENERIC;
0115     *tag++ = 7;
0116     *tag++ = 0x00;
0117     *tag++ = 0xe0;
0118     *tag++ = 0x4c;
0119     *tag++ = 0x01;
0120     *tag++ = 0x02;
0121     *tag++ = 0x11;
0122     *tag++ = 0x00;
0123 
0124     *tag_p = tag;
0125     netdev_alert(ieee->dev, "This is enable turbo mode IE process\n");
0126 }
0127 
0128 static void enqueue_mgmt(struct rtllib_device *ieee, struct sk_buff *skb)
0129 {
0130     int nh;
0131 
0132     nh = (ieee->mgmt_queue_head + 1) % MGMT_QUEUE_NUM;
0133 
0134 /* if the queue is full but we have newer frames then
0135  * just overwrites the oldest.
0136  *
0137  * if (nh == ieee->mgmt_queue_tail)
0138  *      return -1;
0139  */
0140     ieee->mgmt_queue_head = nh;
0141     ieee->mgmt_queue_ring[nh] = skb;
0142 
0143 }
0144 
0145 static void init_mgmt_queue(struct rtllib_device *ieee)
0146 {
0147     ieee->mgmt_queue_tail = ieee->mgmt_queue_head = 0;
0148 }
0149 
0150 
0151 u8
0152 MgntQuery_TxRateExcludeCCKRates(struct rtllib_device *ieee)
0153 {
0154     u16 i;
0155     u8  QueryRate = 0;
0156     u8  BasicRate;
0157 
0158 
0159     for (i = 0; i < ieee->current_network.rates_len; i++) {
0160         BasicRate = ieee->current_network.rates[i]&0x7F;
0161         if (!rtllib_is_cck_rate(BasicRate)) {
0162             if (QueryRate == 0) {
0163                 QueryRate = BasicRate;
0164             } else {
0165                 if (BasicRate < QueryRate)
0166                     QueryRate = BasicRate;
0167             }
0168         }
0169     }
0170 
0171     if (QueryRate == 0) {
0172         QueryRate = 12;
0173         netdev_info(ieee->dev, "No BasicRate found!!\n");
0174     }
0175     return QueryRate;
0176 }
0177 
0178 static u8 MgntQuery_MgntFrameTxRate(struct rtllib_device *ieee)
0179 {
0180     struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
0181     u8 rate;
0182 
0183     if (pHTInfo->IOTAction & HT_IOT_ACT_MGNT_USE_CCK_6M)
0184         rate = 0x0c;
0185     else
0186         rate = ieee->basic_rate & 0x7f;
0187 
0188     if (rate == 0) {
0189         if (ieee->mode == IEEE_A ||
0190            ieee->mode == IEEE_N_5G ||
0191            (ieee->mode == IEEE_N_24G && !pHTInfo->bCurSuppCCK))
0192             rate = 0x0c;
0193         else
0194             rate = 0x02;
0195     }
0196 
0197     return rate;
0198 }
0199 
0200 inline void softmac_mgmt_xmit(struct sk_buff *skb, struct rtllib_device *ieee)
0201 {
0202     unsigned long flags;
0203     short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
0204     struct rtllib_hdr_3addr  *header =
0205         (struct rtllib_hdr_3addr  *)skb->data;
0206 
0207     struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 8);
0208 
0209     spin_lock_irqsave(&ieee->lock, flags);
0210 
0211     /* called with 2nd param 0, no mgmt lock required */
0212     rtllib_sta_wakeup(ieee, 0);
0213 
0214     if (le16_to_cpu(header->frame_ctl) == RTLLIB_STYPE_BEACON)
0215         tcb_desc->queue_index = BEACON_QUEUE;
0216     else
0217         tcb_desc->queue_index = MGNT_QUEUE;
0218 
0219     if (ieee->disable_mgnt_queue)
0220         tcb_desc->queue_index = HIGH_QUEUE;
0221 
0222     tcb_desc->data_rate = MgntQuery_MgntFrameTxRate(ieee);
0223     tcb_desc->RATRIndex = 7;
0224     tcb_desc->bTxDisableRateFallBack = 1;
0225     tcb_desc->bTxUseDriverAssingedRate = 1;
0226     if (single) {
0227         if (ieee->queue_stop) {
0228             enqueue_mgmt(ieee, skb);
0229         } else {
0230             header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0]<<4);
0231 
0232             if (ieee->seq_ctrl[0] == 0xFFF)
0233                 ieee->seq_ctrl[0] = 0;
0234             else
0235                 ieee->seq_ctrl[0]++;
0236 
0237             /* avoid watchdog triggers */
0238             ieee->softmac_data_hard_start_xmit(skb, ieee->dev,
0239                                ieee->basic_rate);
0240         }
0241 
0242         spin_unlock_irqrestore(&ieee->lock, flags);
0243     } else {
0244         spin_unlock_irqrestore(&ieee->lock, flags);
0245         spin_lock_irqsave(&ieee->mgmt_tx_lock, flags);
0246 
0247         header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
0248 
0249         if (ieee->seq_ctrl[0] == 0xFFF)
0250             ieee->seq_ctrl[0] = 0;
0251         else
0252             ieee->seq_ctrl[0]++;
0253 
0254         /* check whether the managed packet queued greater than 5 */
0255         if (!ieee->check_nic_enough_desc(ieee->dev,
0256                          tcb_desc->queue_index) ||
0257             skb_queue_len(&ieee->skb_waitQ[tcb_desc->queue_index]) ||
0258             ieee->queue_stop) {
0259             /* insert the skb packet to the management queue
0260              *
0261              * as for the completion function, it does not need
0262              * to check it any more.
0263              */
0264             netdev_info(ieee->dev,
0265                    "%s():insert to waitqueue, queue_index:%d!\n",
0266                    __func__, tcb_desc->queue_index);
0267             skb_queue_tail(&ieee->skb_waitQ[tcb_desc->queue_index],
0268                        skb);
0269         } else {
0270             ieee->softmac_hard_start_xmit(skb, ieee->dev);
0271         }
0272         spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags);
0273     }
0274 }
0275 
0276 static inline void
0277 softmac_ps_mgmt_xmit(struct sk_buff *skb,
0278              struct rtllib_device *ieee)
0279 {
0280     short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
0281     struct rtllib_hdr_3addr  *header =
0282         (struct rtllib_hdr_3addr  *)skb->data;
0283     u16 fc, type, stype;
0284     struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 8);
0285 
0286     fc = le16_to_cpu(header->frame_ctl);
0287     type = WLAN_FC_GET_TYPE(fc);
0288     stype = WLAN_FC_GET_STYPE(fc);
0289 
0290 
0291     if (stype != RTLLIB_STYPE_PSPOLL)
0292         tcb_desc->queue_index = MGNT_QUEUE;
0293     else
0294         tcb_desc->queue_index = HIGH_QUEUE;
0295 
0296     if (ieee->disable_mgnt_queue)
0297         tcb_desc->queue_index = HIGH_QUEUE;
0298 
0299 
0300     tcb_desc->data_rate = MgntQuery_MgntFrameTxRate(ieee);
0301     tcb_desc->RATRIndex = 7;
0302     tcb_desc->bTxDisableRateFallBack = 1;
0303     tcb_desc->bTxUseDriverAssingedRate = 1;
0304     if (single) {
0305         if (type != RTLLIB_FTYPE_CTL) {
0306             header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
0307 
0308             if (ieee->seq_ctrl[0] == 0xFFF)
0309                 ieee->seq_ctrl[0] = 0;
0310             else
0311                 ieee->seq_ctrl[0]++;
0312 
0313         }
0314         /* avoid watchdog triggers */
0315         ieee->softmac_data_hard_start_xmit(skb, ieee->dev,
0316                            ieee->basic_rate);
0317 
0318     } else {
0319         if (type != RTLLIB_FTYPE_CTL) {
0320             header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
0321 
0322             if (ieee->seq_ctrl[0] == 0xFFF)
0323                 ieee->seq_ctrl[0] = 0;
0324             else
0325                 ieee->seq_ctrl[0]++;
0326         }
0327         ieee->softmac_hard_start_xmit(skb, ieee->dev);
0328 
0329     }
0330 }
0331 
0332 static inline struct sk_buff *rtllib_probe_req(struct rtllib_device *ieee)
0333 {
0334     unsigned int len, rate_len;
0335     u8 *tag;
0336     struct sk_buff *skb;
0337     struct rtllib_probe_request *req;
0338 
0339     len = ieee->current_network.ssid_len;
0340 
0341     rate_len = rtllib_MFIE_rate_len(ieee);
0342 
0343     skb = dev_alloc_skb(sizeof(struct rtllib_probe_request) +
0344                 2 + len + rate_len + ieee->tx_headroom);
0345 
0346     if (!skb)
0347         return NULL;
0348 
0349     skb_reserve(skb, ieee->tx_headroom);
0350 
0351     req = skb_put(skb, sizeof(struct rtllib_probe_request));
0352     req->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_PROBE_REQ);
0353     req->header.duration_id = 0;
0354 
0355     eth_broadcast_addr(req->header.addr1);
0356     ether_addr_copy(req->header.addr2, ieee->dev->dev_addr);
0357     eth_broadcast_addr(req->header.addr3);
0358 
0359     tag = skb_put(skb, len + 2 + rate_len);
0360 
0361     *tag++ = MFIE_TYPE_SSID;
0362     *tag++ = len;
0363     memcpy(tag, ieee->current_network.ssid, len);
0364     tag += len;
0365 
0366     rtllib_MFIE_Brate(ieee, &tag);
0367     rtllib_MFIE_Grate(ieee, &tag);
0368 
0369     return skb;
0370 }
0371 
0372 static struct sk_buff *rtllib_get_beacon_(struct rtllib_device *ieee);
0373 
0374 static void rtllib_send_beacon(struct rtllib_device *ieee)
0375 {
0376     struct sk_buff *skb;
0377 
0378     if (!ieee->ieee_up)
0379         return;
0380     skb = rtllib_get_beacon_(ieee);
0381 
0382     if (skb) {
0383         softmac_mgmt_xmit(skb, ieee);
0384         ieee->softmac_stats.tx_beacons++;
0385     }
0386 
0387     if (ieee->beacon_txing && ieee->ieee_up)
0388         mod_timer(&ieee->beacon_timer, jiffies +
0389               (msecs_to_jiffies(ieee->current_network.beacon_interval - 5)));
0390 }
0391 
0392 
0393 static void rtllib_send_beacon_cb(struct timer_list *t)
0394 {
0395     struct rtllib_device *ieee =
0396         from_timer(ieee, t, beacon_timer);
0397     unsigned long flags;
0398 
0399     spin_lock_irqsave(&ieee->beacon_lock, flags);
0400     rtllib_send_beacon(ieee);
0401     spin_unlock_irqrestore(&ieee->beacon_lock, flags);
0402 }
0403 
0404 /* Enables network monitor mode, all rx packets will be received. */
0405 void rtllib_EnableNetMonitorMode(struct net_device *dev,
0406         bool bInitState)
0407 {
0408     struct rtllib_device *ieee = netdev_priv_rsl(dev);
0409 
0410     netdev_info(dev, "========>Enter Monitor Mode\n");
0411 
0412     ieee->AllowAllDestAddrHandler(dev, true, !bInitState);
0413 }
0414 
0415 
0416 /* Disables network monitor mode. Only packets destinated to
0417  * us will be received.
0418  */
0419 void rtllib_DisableNetMonitorMode(struct net_device *dev,
0420         bool bInitState)
0421 {
0422     struct rtllib_device *ieee = netdev_priv_rsl(dev);
0423 
0424     netdev_info(dev, "========>Exit Monitor Mode\n");
0425 
0426     ieee->AllowAllDestAddrHandler(dev, false, !bInitState);
0427 }
0428 
0429 
0430 /* Enables the specialized promiscuous mode required by Intel.
0431  * In this mode, Intel intends to hear traffics from/to other STAs in the
0432  * same BSS. Therefore we don't have to disable checking BSSID and we only need
0433  * to allow all dest. BUT: if we enable checking BSSID then we can't recv
0434  * packets from other STA.
0435  */
0436 void rtllib_EnableIntelPromiscuousMode(struct net_device *dev,
0437         bool bInitState)
0438 {
0439     bool bFilterOutNonAssociatedBSSID = false;
0440 
0441     struct rtllib_device *ieee = netdev_priv_rsl(dev);
0442 
0443     netdev_info(dev, "========>Enter Intel Promiscuous Mode\n");
0444 
0445     ieee->AllowAllDestAddrHandler(dev, true, !bInitState);
0446     ieee->SetHwRegHandler(dev, HW_VAR_CECHK_BSSID,
0447                  (u8 *)&bFilterOutNonAssociatedBSSID);
0448 
0449     ieee->bNetPromiscuousMode = true;
0450 }
0451 EXPORT_SYMBOL(rtllib_EnableIntelPromiscuousMode);
0452 
0453 
0454 /* Disables the specialized promiscuous mode required by Intel.
0455  * See MgntEnableIntelPromiscuousMode for detail.
0456  */
0457 void rtllib_DisableIntelPromiscuousMode(struct net_device *dev,
0458         bool bInitState)
0459 {
0460     bool bFilterOutNonAssociatedBSSID = true;
0461 
0462     struct rtllib_device *ieee = netdev_priv_rsl(dev);
0463 
0464     netdev_info(dev, "========>Exit Intel Promiscuous Mode\n");
0465 
0466     ieee->AllowAllDestAddrHandler(dev, false, !bInitState);
0467     ieee->SetHwRegHandler(dev, HW_VAR_CECHK_BSSID,
0468                  (u8 *)&bFilterOutNonAssociatedBSSID);
0469 
0470     ieee->bNetPromiscuousMode = false;
0471 }
0472 EXPORT_SYMBOL(rtllib_DisableIntelPromiscuousMode);
0473 
0474 static void rtllib_send_probe(struct rtllib_device *ieee, u8 is_mesh)
0475 {
0476     struct sk_buff *skb;
0477 
0478     skb = rtllib_probe_req(ieee);
0479     if (skb) {
0480         softmac_mgmt_xmit(skb, ieee);
0481         ieee->softmac_stats.tx_probe_rq++;
0482     }
0483 }
0484 
0485 
0486 static void rtllib_send_probe_requests(struct rtllib_device *ieee, u8 is_mesh)
0487 {
0488     if (ieee->active_scan && (ieee->softmac_features &
0489         IEEE_SOFTMAC_PROBERQ)) {
0490         rtllib_send_probe(ieee, 0);
0491         rtllib_send_probe(ieee, 0);
0492     }
0493 }
0494 
0495 static void rtllib_update_active_chan_map(struct rtllib_device *ieee)
0496 {
0497     memcpy(ieee->active_channel_map, GET_DOT11D_INFO(ieee)->channel_map,
0498            MAX_CHANNEL_NUMBER+1);
0499 }
0500 
0501 /* this performs syncro scan blocking the caller until all channels
0502  * in the allowed channel map has been checked.
0503  */
0504 static void rtllib_softmac_scan_syncro(struct rtllib_device *ieee, u8 is_mesh)
0505 {
0506     union iwreq_data wrqu;
0507     short ch = 0;
0508 
0509     rtllib_update_active_chan_map(ieee);
0510 
0511     ieee->be_scan_inprogress = true;
0512 
0513     mutex_lock(&ieee->scan_mutex);
0514 
0515     while (1) {
0516         do {
0517             ch++;
0518             if (ch > MAX_CHANNEL_NUMBER)
0519                 goto out; /* scan completed */
0520         } while (!ieee->active_channel_map[ch]);
0521 
0522         /* this function can be called in two situations
0523          * 1- We have switched to ad-hoc mode and we are
0524          *    performing a complete syncro scan before conclude
0525          *    there are no interesting cell and to create a
0526          *    new one. In this case the link state is
0527          *    RTLLIB_NOLINK until we found an interesting cell.
0528          *    If so the ieee8021_new_net, called by the RX path
0529          *    will set the state to RTLLIB_LINKED, so we stop
0530          *    scanning
0531          * 2- We are linked and the root uses run iwlist scan.
0532          *    So we switch to RTLLIB_LINKED_SCANNING to remember
0533          *    that we are still logically linked (not interested in
0534          *    new network events, despite for updating the net list,
0535          *    but we are temporarly 'unlinked' as the driver shall
0536          *    not filter RX frames and the channel is changing.
0537          * So the only situation in which are interested is to check
0538          * if the state become LINKED because of the #1 situation
0539          */
0540 
0541         if (ieee->state == RTLLIB_LINKED)
0542             goto out;
0543         if (ieee->sync_scan_hurryup) {
0544             netdev_info(ieee->dev,
0545                     "============>sync_scan_hurryup out\n");
0546             goto out;
0547         }
0548 
0549         ieee->set_chan(ieee->dev, ch);
0550         if (ieee->active_channel_map[ch] == 1)
0551             rtllib_send_probe_requests(ieee, 0);
0552 
0553         /* this prevent excessive time wait when we
0554          * need to wait for a syncro scan to end..
0555          */
0556         msleep_interruptible_rsl(RTLLIB_SOFTMAC_SCAN_TIME);
0557     }
0558 out:
0559     ieee->actscanning = false;
0560     ieee->sync_scan_hurryup = 0;
0561 
0562     if (ieee->state >= RTLLIB_LINKED) {
0563         if (IS_DOT11D_ENABLE(ieee))
0564             dot11d_scan_complete(ieee);
0565     }
0566     mutex_unlock(&ieee->scan_mutex);
0567 
0568     ieee->be_scan_inprogress = false;
0569 
0570     memset(&wrqu, 0, sizeof(wrqu));
0571     wireless_send_event(ieee->dev, SIOCGIWSCAN, &wrqu, NULL);
0572 }
0573 
0574 static void rtllib_softmac_scan_wq(void *data)
0575 {
0576     struct rtllib_device *ieee = container_of_dwork_rsl(data,
0577                      struct rtllib_device, softmac_scan_wq);
0578     u8 last_channel = ieee->current_network.channel;
0579 
0580     rtllib_update_active_chan_map(ieee);
0581 
0582     if (!ieee->ieee_up)
0583         return;
0584     if (rtllib_act_scanning(ieee, true))
0585         return;
0586 
0587     mutex_lock(&ieee->scan_mutex);
0588 
0589     if (ieee->eRFPowerState == eRfOff) {
0590         netdev_info(ieee->dev,
0591                 "======>%s():rf state is eRfOff, return\n",
0592                 __func__);
0593         goto out1;
0594     }
0595 
0596     do {
0597         ieee->current_network.channel =
0598             (ieee->current_network.channel + 1) %
0599             MAX_CHANNEL_NUMBER;
0600         if (ieee->scan_watch_dog++ > MAX_CHANNEL_NUMBER) {
0601             if (!ieee->active_channel_map[ieee->current_network.channel])
0602                 ieee->current_network.channel = 6;
0603             goto out; /* no good chans */
0604         }
0605     } while (!ieee->active_channel_map[ieee->current_network.channel]);
0606 
0607     if (ieee->scanning_continue == 0)
0608         goto out;
0609 
0610     ieee->set_chan(ieee->dev, ieee->current_network.channel);
0611 
0612     if (ieee->active_channel_map[ieee->current_network.channel] == 1)
0613         rtllib_send_probe_requests(ieee, 0);
0614 
0615     schedule_delayed_work(&ieee->softmac_scan_wq,
0616                   msecs_to_jiffies(RTLLIB_SOFTMAC_SCAN_TIME));
0617 
0618     mutex_unlock(&ieee->scan_mutex);
0619     return;
0620 
0621 out:
0622     if (IS_DOT11D_ENABLE(ieee))
0623         dot11d_scan_complete(ieee);
0624     ieee->current_network.channel = last_channel;
0625 
0626 out1:
0627     ieee->actscanning = false;
0628     ieee->scan_watch_dog = 0;
0629     ieee->scanning_continue = 0;
0630     mutex_unlock(&ieee->scan_mutex);
0631 }
0632 
0633 
0634 
0635 static void rtllib_beacons_start(struct rtllib_device *ieee)
0636 {
0637     unsigned long flags;
0638 
0639     spin_lock_irqsave(&ieee->beacon_lock, flags);
0640 
0641     ieee->beacon_txing = 1;
0642     rtllib_send_beacon(ieee);
0643 
0644     spin_unlock_irqrestore(&ieee->beacon_lock, flags);
0645 }
0646 
0647 static void rtllib_beacons_stop(struct rtllib_device *ieee)
0648 {
0649     unsigned long flags;
0650 
0651     spin_lock_irqsave(&ieee->beacon_lock, flags);
0652 
0653     ieee->beacon_txing = 0;
0654 
0655     spin_unlock_irqrestore(&ieee->beacon_lock, flags);
0656     del_timer_sync(&ieee->beacon_timer);
0657 
0658 }
0659 
0660 
0661 void rtllib_stop_send_beacons(struct rtllib_device *ieee)
0662 {
0663     if (ieee->stop_send_beacons)
0664         ieee->stop_send_beacons(ieee->dev);
0665     if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
0666         rtllib_beacons_stop(ieee);
0667 }
0668 EXPORT_SYMBOL(rtllib_stop_send_beacons);
0669 
0670 
0671 void rtllib_start_send_beacons(struct rtllib_device *ieee)
0672 {
0673     if (ieee->start_send_beacons)
0674         ieee->start_send_beacons(ieee->dev);
0675     if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
0676         rtllib_beacons_start(ieee);
0677 }
0678 EXPORT_SYMBOL(rtllib_start_send_beacons);
0679 
0680 
0681 static void rtllib_softmac_stop_scan(struct rtllib_device *ieee)
0682 {
0683     mutex_lock(&ieee->scan_mutex);
0684     ieee->scan_watch_dog = 0;
0685     if (ieee->scanning_continue == 1) {
0686         ieee->scanning_continue = 0;
0687         ieee->actscanning = false;
0688 
0689         cancel_delayed_work_sync(&ieee->softmac_scan_wq);
0690     }
0691 
0692     mutex_unlock(&ieee->scan_mutex);
0693 }
0694 
0695 void rtllib_stop_scan(struct rtllib_device *ieee)
0696 {
0697     if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
0698         rtllib_softmac_stop_scan(ieee);
0699     } else {
0700         if (ieee->rtllib_stop_hw_scan)
0701             ieee->rtllib_stop_hw_scan(ieee->dev);
0702     }
0703 }
0704 EXPORT_SYMBOL(rtllib_stop_scan);
0705 
0706 void rtllib_stop_scan_syncro(struct rtllib_device *ieee)
0707 {
0708     if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
0709         ieee->sync_scan_hurryup = 1;
0710     } else {
0711         if (ieee->rtllib_stop_hw_scan)
0712             ieee->rtllib_stop_hw_scan(ieee->dev);
0713     }
0714 }
0715 EXPORT_SYMBOL(rtllib_stop_scan_syncro);
0716 
0717 bool rtllib_act_scanning(struct rtllib_device *ieee, bool sync_scan)
0718 {
0719     if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
0720         if (sync_scan)
0721             return ieee->be_scan_inprogress;
0722         else
0723             return ieee->actscanning || ieee->be_scan_inprogress;
0724     } else {
0725         return test_bit(STATUS_SCANNING, &ieee->status);
0726     }
0727 }
0728 EXPORT_SYMBOL(rtllib_act_scanning);
0729 
0730 /* called with ieee->lock held */
0731 static void rtllib_start_scan(struct rtllib_device *ieee)
0732 {
0733     if (ieee->rtllib_ips_leave_wq != NULL)
0734         ieee->rtllib_ips_leave_wq(ieee->dev);
0735 
0736     if (IS_DOT11D_ENABLE(ieee)) {
0737         if (IS_COUNTRY_IE_VALID(ieee))
0738             RESET_CIE_WATCHDOG(ieee);
0739     }
0740     if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
0741         if (ieee->scanning_continue == 0) {
0742             ieee->actscanning = true;
0743             ieee->scanning_continue = 1;
0744             schedule_delayed_work(&ieee->softmac_scan_wq, 0);
0745         }
0746     } else {
0747         if (ieee->rtllib_start_hw_scan)
0748             ieee->rtllib_start_hw_scan(ieee->dev);
0749     }
0750 }
0751 
0752 /* called with wx_mutex held */
0753 void rtllib_start_scan_syncro(struct rtllib_device *ieee, u8 is_mesh)
0754 {
0755     if (IS_DOT11D_ENABLE(ieee)) {
0756         if (IS_COUNTRY_IE_VALID(ieee))
0757             RESET_CIE_WATCHDOG(ieee);
0758     }
0759     ieee->sync_scan_hurryup = 0;
0760     if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
0761         rtllib_softmac_scan_syncro(ieee, is_mesh);
0762     } else {
0763         if (ieee->rtllib_start_hw_scan)
0764             ieee->rtllib_start_hw_scan(ieee->dev);
0765     }
0766 }
0767 EXPORT_SYMBOL(rtllib_start_scan_syncro);
0768 
0769 static inline struct sk_buff *
0770 rtllib_authentication_req(struct rtllib_network *beacon,
0771               struct rtllib_device *ieee,
0772               int challengelen, u8 *daddr)
0773 {
0774     struct sk_buff *skb;
0775     struct rtllib_authentication *auth;
0776     int  len;
0777 
0778     len = sizeof(struct rtllib_authentication) + challengelen +
0779              ieee->tx_headroom + 4;
0780     skb = dev_alloc_skb(len);
0781 
0782     if (!skb)
0783         return NULL;
0784 
0785     skb_reserve(skb, ieee->tx_headroom);
0786 
0787     auth = skb_put(skb, sizeof(struct rtllib_authentication));
0788 
0789     auth->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_AUTH);
0790     if (challengelen)
0791         auth->header.frame_ctl |= cpu_to_le16(RTLLIB_FCTL_WEP);
0792 
0793     auth->header.duration_id = cpu_to_le16(0x013a);
0794     ether_addr_copy(auth->header.addr1, beacon->bssid);
0795     ether_addr_copy(auth->header.addr2, ieee->dev->dev_addr);
0796     ether_addr_copy(auth->header.addr3, beacon->bssid);
0797     if (ieee->auth_mode == 0)
0798         auth->algorithm = WLAN_AUTH_OPEN;
0799     else if (ieee->auth_mode == 1)
0800         auth->algorithm = cpu_to_le16(WLAN_AUTH_SHARED_KEY);
0801     else if (ieee->auth_mode == 2)
0802         auth->algorithm = WLAN_AUTH_OPEN;
0803     auth->transaction = cpu_to_le16(ieee->associate_seq);
0804     ieee->associate_seq++;
0805 
0806     auth->status = cpu_to_le16(WLAN_STATUS_SUCCESS);
0807 
0808     return skb;
0809 }
0810 
0811 static struct sk_buff *rtllib_probe_resp(struct rtllib_device *ieee,
0812                      const u8 *dest)
0813 {
0814     u8 *tag;
0815     int beacon_size;
0816     struct rtllib_probe_response *beacon_buf;
0817     struct sk_buff *skb = NULL;
0818     int encrypt;
0819     int atim_len, erp_len;
0820     struct lib80211_crypt_data *crypt;
0821 
0822     char *ssid = ieee->current_network.ssid;
0823     int ssid_len = ieee->current_network.ssid_len;
0824     int rate_len = ieee->current_network.rates_len+2;
0825     int rate_ex_len = ieee->current_network.rates_ex_len;
0826     int wpa_ie_len = ieee->wpa_ie_len;
0827     u8 erpinfo_content = 0;
0828 
0829     u8 *tmp_ht_cap_buf = NULL;
0830     u8 tmp_ht_cap_len = 0;
0831     u8 *tmp_ht_info_buf = NULL;
0832     u8 tmp_ht_info_len = 0;
0833     struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
0834     u8 *tmp_generic_ie_buf = NULL;
0835     u8 tmp_generic_ie_len = 0;
0836 
0837     if (rate_ex_len > 0)
0838         rate_ex_len += 2;
0839 
0840     if (ieee->current_network.capability & WLAN_CAPABILITY_IBSS)
0841         atim_len = 4;
0842     else
0843         atim_len = 0;
0844 
0845     if ((ieee->current_network.mode == IEEE_G) ||
0846        (ieee->current_network.mode == IEEE_N_24G &&
0847        ieee->pHTInfo->bCurSuppCCK)) {
0848         erp_len = 3;
0849         erpinfo_content = 0;
0850         if (ieee->current_network.buseprotection)
0851             erpinfo_content |= ERP_UseProtection;
0852     } else
0853         erp_len = 0;
0854 
0855     crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
0856     encrypt = ieee->host_encrypt && crypt && crypt->ops &&
0857         ((strcmp(crypt->ops->name, "R-WEP") == 0 || wpa_ie_len));
0858     if (ieee->pHTInfo->bCurrentHTSupport) {
0859         tmp_ht_cap_buf = (u8 *)&(ieee->pHTInfo->SelfHTCap);
0860         tmp_ht_cap_len = sizeof(ieee->pHTInfo->SelfHTCap);
0861         tmp_ht_info_buf = (u8 *)&(ieee->pHTInfo->SelfHTInfo);
0862         tmp_ht_info_len = sizeof(ieee->pHTInfo->SelfHTInfo);
0863         HTConstructCapabilityElement(ieee, tmp_ht_cap_buf,
0864                          &tmp_ht_cap_len, encrypt, false);
0865         HTConstructInfoElement(ieee, tmp_ht_info_buf, &tmp_ht_info_len,
0866                        encrypt);
0867 
0868         if (pHTInfo->bRegRT2RTAggregation) {
0869             tmp_generic_ie_buf = ieee->pHTInfo->szRT2RTAggBuffer;
0870             tmp_generic_ie_len =
0871                  sizeof(ieee->pHTInfo->szRT2RTAggBuffer);
0872             HTConstructRT2RTAggElement(ieee, tmp_generic_ie_buf,
0873                            &tmp_generic_ie_len);
0874         }
0875     }
0876 
0877     beacon_size = sizeof(struct rtllib_probe_response)+2+
0878         ssid_len + 3 + rate_len + rate_ex_len + atim_len + erp_len
0879         + wpa_ie_len + ieee->tx_headroom;
0880     skb = dev_alloc_skb(beacon_size);
0881     if (!skb)
0882         return NULL;
0883 
0884     skb_reserve(skb, ieee->tx_headroom);
0885 
0886     beacon_buf = skb_put(skb, (beacon_size - ieee->tx_headroom));
0887     ether_addr_copy(beacon_buf->header.addr1, dest);
0888     ether_addr_copy(beacon_buf->header.addr2, ieee->dev->dev_addr);
0889     ether_addr_copy(beacon_buf->header.addr3, ieee->current_network.bssid);
0890 
0891     beacon_buf->header.duration_id = 0;
0892     beacon_buf->beacon_interval =
0893         cpu_to_le16(ieee->current_network.beacon_interval);
0894     beacon_buf->capability =
0895         cpu_to_le16(ieee->current_network.capability &
0896         WLAN_CAPABILITY_IBSS);
0897     beacon_buf->capability |=
0898         cpu_to_le16(ieee->current_network.capability &
0899         WLAN_CAPABILITY_SHORT_PREAMBLE);
0900 
0901     if (ieee->short_slot && (ieee->current_network.capability &
0902         WLAN_CAPABILITY_SHORT_SLOT_TIME))
0903         beacon_buf->capability |=
0904             cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
0905 
0906     crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
0907     if (encrypt)
0908         beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
0909 
0910 
0911     beacon_buf->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_PROBE_RESP);
0912     beacon_buf->info_element[0].id = MFIE_TYPE_SSID;
0913     beacon_buf->info_element[0].len = ssid_len;
0914 
0915     tag = (u8 *)beacon_buf->info_element[0].data;
0916 
0917     memcpy(tag, ssid, ssid_len);
0918 
0919     tag += ssid_len;
0920 
0921     *(tag++) = MFIE_TYPE_RATES;
0922     *(tag++) = rate_len-2;
0923     memcpy(tag, ieee->current_network.rates, rate_len-2);
0924     tag += rate_len-2;
0925 
0926     *(tag++) = MFIE_TYPE_DS_SET;
0927     *(tag++) = 1;
0928     *(tag++) = ieee->current_network.channel;
0929 
0930     if (atim_len) {
0931         u16 val16;
0932         *(tag++) = MFIE_TYPE_IBSS_SET;
0933         *(tag++) = 2;
0934         val16 = ieee->current_network.atim_window;
0935         memcpy((u8 *)tag, (u8 *)&val16, 2);
0936         tag += 2;
0937     }
0938 
0939     if (erp_len) {
0940         *(tag++) = MFIE_TYPE_ERP;
0941         *(tag++) = 1;
0942         *(tag++) = erpinfo_content;
0943     }
0944     if (rate_ex_len) {
0945         *(tag++) = MFIE_TYPE_RATES_EX;
0946         *(tag++) = rate_ex_len-2;
0947         memcpy(tag, ieee->current_network.rates_ex, rate_ex_len-2);
0948         tag += rate_ex_len-2;
0949     }
0950 
0951     if (wpa_ie_len) {
0952         if (ieee->iw_mode == IW_MODE_ADHOC)
0953             memcpy(&ieee->wpa_ie[14], &ieee->wpa_ie[8], 4);
0954         memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
0955         tag += ieee->wpa_ie_len;
0956     }
0957     return skb;
0958 }
0959 
0960 static struct sk_buff *rtllib_assoc_resp(struct rtllib_device *ieee, u8 *dest)
0961 {
0962     struct sk_buff *skb;
0963     u8 *tag;
0964 
0965     struct lib80211_crypt_data *crypt;
0966     struct rtllib_assoc_response_frame *assoc;
0967     short encrypt;
0968 
0969     unsigned int rate_len = rtllib_MFIE_rate_len(ieee);
0970     int len = sizeof(struct rtllib_assoc_response_frame) + rate_len +
0971           ieee->tx_headroom;
0972 
0973     skb = dev_alloc_skb(len);
0974 
0975     if (!skb)
0976         return NULL;
0977 
0978     skb_reserve(skb, ieee->tx_headroom);
0979 
0980     assoc = skb_put(skb, sizeof(struct rtllib_assoc_response_frame));
0981 
0982     assoc->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_ASSOC_RESP);
0983     ether_addr_copy(assoc->header.addr1, dest);
0984     ether_addr_copy(assoc->header.addr3, ieee->dev->dev_addr);
0985     ether_addr_copy(assoc->header.addr2, ieee->dev->dev_addr);
0986     assoc->capability = cpu_to_le16(ieee->iw_mode == IW_MODE_MASTER ?
0987         WLAN_CAPABILITY_ESS : WLAN_CAPABILITY_IBSS);
0988 
0989 
0990     if (ieee->short_slot)
0991         assoc->capability |=
0992                  cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
0993 
0994     if (ieee->host_encrypt)
0995         crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
0996     else
0997         crypt = NULL;
0998 
0999     encrypt = (crypt && crypt->ops);
1000 
1001     if (encrypt)
1002         assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1003 
1004     assoc->status = 0;
1005     assoc->aid = cpu_to_le16(ieee->assoc_id);
1006     if (ieee->assoc_id == 0x2007)
1007         ieee->assoc_id = 0;
1008     else
1009         ieee->assoc_id++;
1010 
1011     tag = skb_put(skb, rate_len);
1012     rtllib_MFIE_Brate(ieee, &tag);
1013     rtllib_MFIE_Grate(ieee, &tag);
1014 
1015     return skb;
1016 }
1017 
1018 static struct sk_buff *rtllib_auth_resp(struct rtllib_device *ieee, int status,
1019                  u8 *dest)
1020 {
1021     struct sk_buff *skb = NULL;
1022     struct rtllib_authentication *auth;
1023     int len = ieee->tx_headroom + sizeof(struct rtllib_authentication) + 1;
1024 
1025     skb = dev_alloc_skb(len);
1026     if (!skb)
1027         return NULL;
1028 
1029     skb->len = sizeof(struct rtllib_authentication);
1030 
1031     skb_reserve(skb, ieee->tx_headroom);
1032 
1033     auth = skb_put(skb, sizeof(struct rtllib_authentication));
1034 
1035     auth->status = cpu_to_le16(status);
1036     auth->transaction = cpu_to_le16(2);
1037     auth->algorithm = cpu_to_le16(WLAN_AUTH_OPEN);
1038 
1039     ether_addr_copy(auth->header.addr3, ieee->dev->dev_addr);
1040     ether_addr_copy(auth->header.addr2, ieee->dev->dev_addr);
1041     ether_addr_copy(auth->header.addr1, dest);
1042     auth->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_AUTH);
1043     return skb;
1044 
1045 
1046 }
1047 
1048 static struct sk_buff *rtllib_null_func(struct rtllib_device *ieee, short pwr)
1049 {
1050     struct sk_buff *skb;
1051     struct rtllib_hdr_3addr *hdr;
1052 
1053     skb = dev_alloc_skb(sizeof(struct rtllib_hdr_3addr)+ieee->tx_headroom);
1054     if (!skb)
1055         return NULL;
1056 
1057     skb_reserve(skb, ieee->tx_headroom);
1058 
1059     hdr = skb_put(skb, sizeof(struct rtllib_hdr_3addr));
1060 
1061     ether_addr_copy(hdr->addr1, ieee->current_network.bssid);
1062     ether_addr_copy(hdr->addr2, ieee->dev->dev_addr);
1063     ether_addr_copy(hdr->addr3, ieee->current_network.bssid);
1064 
1065     hdr->frame_ctl = cpu_to_le16(RTLLIB_FTYPE_DATA |
1066         RTLLIB_STYPE_NULLFUNC | RTLLIB_FCTL_TODS |
1067         (pwr ? RTLLIB_FCTL_PM : 0));
1068 
1069     return skb;
1070 
1071 
1072 }
1073 
1074 static struct sk_buff *rtllib_pspoll_func(struct rtllib_device *ieee)
1075 {
1076     struct sk_buff *skb;
1077     struct rtllib_pspoll_hdr *hdr;
1078 
1079     skb = dev_alloc_skb(sizeof(struct rtllib_pspoll_hdr)+ieee->tx_headroom);
1080     if (!skb)
1081         return NULL;
1082 
1083     skb_reserve(skb, ieee->tx_headroom);
1084 
1085     hdr = skb_put(skb, sizeof(struct rtllib_pspoll_hdr));
1086 
1087     ether_addr_copy(hdr->bssid, ieee->current_network.bssid);
1088     ether_addr_copy(hdr->ta, ieee->dev->dev_addr);
1089 
1090     hdr->aid = cpu_to_le16(ieee->assoc_id | 0xc000);
1091     hdr->frame_ctl = cpu_to_le16(RTLLIB_FTYPE_CTL | RTLLIB_STYPE_PSPOLL |
1092              RTLLIB_FCTL_PM);
1093 
1094     return skb;
1095 
1096 }
1097 
1098 static void rtllib_resp_to_assoc_rq(struct rtllib_device *ieee, u8 *dest)
1099 {
1100     struct sk_buff *buf = rtllib_assoc_resp(ieee, dest);
1101 
1102     if (buf)
1103         softmac_mgmt_xmit(buf, ieee);
1104 }
1105 
1106 
1107 static void rtllib_resp_to_auth(struct rtllib_device *ieee, int s, u8 *dest)
1108 {
1109     struct sk_buff *buf = rtllib_auth_resp(ieee, s, dest);
1110 
1111     if (buf)
1112         softmac_mgmt_xmit(buf, ieee);
1113 }
1114 
1115 
1116 static void rtllib_resp_to_probe(struct rtllib_device *ieee, u8 *dest)
1117 {
1118     struct sk_buff *buf = rtllib_probe_resp(ieee, dest);
1119 
1120     if (buf)
1121         softmac_mgmt_xmit(buf, ieee);
1122 }
1123 
1124 
1125 static inline int SecIsInPMKIDList(struct rtllib_device *ieee, u8 *bssid)
1126 {
1127     int i = 0;
1128 
1129     do {
1130         if ((ieee->PMKIDList[i].bUsed) &&
1131            (memcmp(ieee->PMKIDList[i].Bssid, bssid, ETH_ALEN) == 0))
1132             break;
1133         i++;
1134     } while (i < NUM_PMKID_CACHE);
1135 
1136     if (i == NUM_PMKID_CACHE)
1137         i = -1;
1138     return i;
1139 }
1140 
1141 static inline struct sk_buff *
1142 rtllib_association_req(struct rtllib_network *beacon,
1143                struct rtllib_device *ieee)
1144 {
1145     struct sk_buff *skb;
1146     struct rtllib_assoc_request_frame *hdr;
1147     u8 *tag, *ies;
1148     int i;
1149     u8 *ht_cap_buf = NULL;
1150     u8 ht_cap_len = 0;
1151     u8 *realtek_ie_buf = NULL;
1152     u8 realtek_ie_len = 0;
1153     int wpa_ie_len = ieee->wpa_ie_len;
1154     int wps_ie_len = ieee->wps_ie_len;
1155     unsigned int ckip_ie_len = 0;
1156     unsigned int ccxrm_ie_len = 0;
1157     unsigned int cxvernum_ie_len = 0;
1158     struct lib80211_crypt_data *crypt;
1159     int encrypt;
1160     int PMKCacheIdx;
1161 
1162     unsigned int rate_len = (beacon->rates_len ?
1163                 (beacon->rates_len + 2) : 0) +
1164                 (beacon->rates_ex_len ? (beacon->rates_ex_len) +
1165                 2 : 0);
1166 
1167     unsigned int wmm_info_len = beacon->qos_data.supported ? 9 : 0;
1168     unsigned int turbo_info_len = beacon->Turbo_Enable ? 9 : 0;
1169 
1170     int len = 0;
1171 
1172     crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
1173     if (crypt != NULL)
1174         encrypt = ieee->host_encrypt && crypt && crypt->ops &&
1175               ((strcmp(crypt->ops->name, "R-WEP") == 0 ||
1176               wpa_ie_len));
1177     else
1178         encrypt = 0;
1179 
1180     if ((ieee->rtllib_ap_sec_type &&
1181         (ieee->rtllib_ap_sec_type(ieee) & SEC_ALG_TKIP)) ||
1182         ieee->bForcedBgMode) {
1183         ieee->pHTInfo->bEnableHT = 0;
1184         ieee->mode = WIRELESS_MODE_G;
1185     }
1186 
1187     if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1188         ht_cap_buf = (u8 *)&(ieee->pHTInfo->SelfHTCap);
1189         ht_cap_len = sizeof(ieee->pHTInfo->SelfHTCap);
1190         HTConstructCapabilityElement(ieee, ht_cap_buf, &ht_cap_len,
1191                          encrypt, true);
1192         if (ieee->pHTInfo->bCurrentRT2RTAggregation) {
1193             realtek_ie_buf = ieee->pHTInfo->szRT2RTAggBuffer;
1194             realtek_ie_len =
1195                  sizeof(ieee->pHTInfo->szRT2RTAggBuffer);
1196             HTConstructRT2RTAggElement(ieee, realtek_ie_buf,
1197                            &realtek_ie_len);
1198         }
1199     }
1200 
1201     if (beacon->bCkipSupported)
1202         ckip_ie_len = 30+2;
1203     if (beacon->bCcxRmEnable)
1204         ccxrm_ie_len = 6+2;
1205     if (beacon->BssCcxVerNumber >= 2)
1206         cxvernum_ie_len = 5+2;
1207 
1208     PMKCacheIdx = SecIsInPMKIDList(ieee, ieee->current_network.bssid);
1209     if (PMKCacheIdx >= 0) {
1210         wpa_ie_len += 18;
1211         netdev_info(ieee->dev, "[PMK cache]: WPA2 IE length: %x\n",
1212                 wpa_ie_len);
1213     }
1214     len = sizeof(struct rtllib_assoc_request_frame) + 2
1215         + beacon->ssid_len
1216         + rate_len
1217         + wpa_ie_len
1218         + wps_ie_len
1219         + wmm_info_len
1220         + turbo_info_len
1221         + ht_cap_len
1222         + realtek_ie_len
1223         + ckip_ie_len
1224         + ccxrm_ie_len
1225         + cxvernum_ie_len
1226         + ieee->tx_headroom;
1227 
1228     skb = dev_alloc_skb(len);
1229 
1230     if (!skb)
1231         return NULL;
1232 
1233     skb_reserve(skb, ieee->tx_headroom);
1234 
1235     hdr = skb_put(skb, sizeof(struct rtllib_assoc_request_frame) + 2);
1236 
1237 
1238     hdr->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_ASSOC_REQ);
1239     hdr->header.duration_id = cpu_to_le16(37);
1240     ether_addr_copy(hdr->header.addr1, beacon->bssid);
1241     ether_addr_copy(hdr->header.addr2, ieee->dev->dev_addr);
1242     ether_addr_copy(hdr->header.addr3, beacon->bssid);
1243 
1244     ether_addr_copy(ieee->ap_mac_addr, beacon->bssid);
1245 
1246     hdr->capability = cpu_to_le16(WLAN_CAPABILITY_ESS);
1247     if (beacon->capability & WLAN_CAPABILITY_PRIVACY)
1248         hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1249 
1250     if (beacon->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1251         hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
1252 
1253     if (ieee->short_slot &&
1254        (beacon->capability&WLAN_CAPABILITY_SHORT_SLOT_TIME))
1255         hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
1256 
1257 
1258     hdr->listen_interval = cpu_to_le16(beacon->listen_interval);
1259 
1260     hdr->info_element[0].id = MFIE_TYPE_SSID;
1261 
1262     hdr->info_element[0].len = beacon->ssid_len;
1263     skb_put_data(skb, beacon->ssid, beacon->ssid_len);
1264 
1265     tag = skb_put(skb, rate_len);
1266 
1267     if (beacon->rates_len) {
1268         *tag++ = MFIE_TYPE_RATES;
1269         *tag++ = beacon->rates_len;
1270         for (i = 0; i < beacon->rates_len; i++)
1271             *tag++ = beacon->rates[i];
1272     }
1273 
1274     if (beacon->rates_ex_len) {
1275         *tag++ = MFIE_TYPE_RATES_EX;
1276         *tag++ = beacon->rates_ex_len;
1277         for (i = 0; i < beacon->rates_ex_len; i++)
1278             *tag++ = beacon->rates_ex[i];
1279     }
1280 
1281     if (beacon->bCkipSupported) {
1282         static const u8 AironetIeOui[] = {0x00, 0x01, 0x66};
1283         u8  CcxAironetBuf[30];
1284         struct octet_string osCcxAironetIE;
1285 
1286         memset(CcxAironetBuf, 0, 30);
1287         osCcxAironetIE.Octet = CcxAironetBuf;
1288         osCcxAironetIE.Length = sizeof(CcxAironetBuf);
1289         memcpy(osCcxAironetIE.Octet, AironetIeOui,
1290                sizeof(AironetIeOui));
1291 
1292         osCcxAironetIE.Octet[IE_CISCO_FLAG_POSITION] |=
1293                      (SUPPORT_CKIP_PK|SUPPORT_CKIP_MIC);
1294         tag = skb_put(skb, ckip_ie_len);
1295         *tag++ = MFIE_TYPE_AIRONET;
1296         *tag++ = osCcxAironetIE.Length;
1297         memcpy(tag, osCcxAironetIE.Octet, osCcxAironetIE.Length);
1298         tag += osCcxAironetIE.Length;
1299     }
1300 
1301     if (beacon->bCcxRmEnable) {
1302         static const u8 CcxRmCapBuf[] = {0x00, 0x40, 0x96, 0x01, 0x01,
1303             0x00};
1304         struct octet_string osCcxRmCap;
1305 
1306         osCcxRmCap.Octet = (u8 *)CcxRmCapBuf;
1307         osCcxRmCap.Length = sizeof(CcxRmCapBuf);
1308         tag = skb_put(skb, ccxrm_ie_len);
1309         *tag++ = MFIE_TYPE_GENERIC;
1310         *tag++ = osCcxRmCap.Length;
1311         memcpy(tag, osCcxRmCap.Octet, osCcxRmCap.Length);
1312         tag += osCcxRmCap.Length;
1313     }
1314 
1315     if (beacon->BssCcxVerNumber >= 2) {
1316         u8 CcxVerNumBuf[] = {0x00, 0x40, 0x96, 0x03, 0x00};
1317         struct octet_string osCcxVerNum;
1318 
1319         CcxVerNumBuf[4] = beacon->BssCcxVerNumber;
1320         osCcxVerNum.Octet = CcxVerNumBuf;
1321         osCcxVerNum.Length = sizeof(CcxVerNumBuf);
1322         tag = skb_put(skb, cxvernum_ie_len);
1323         *tag++ = MFIE_TYPE_GENERIC;
1324         *tag++ = osCcxVerNum.Length;
1325         memcpy(tag, osCcxVerNum.Octet, osCcxVerNum.Length);
1326         tag += osCcxVerNum.Length;
1327     }
1328     if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1329         if (ieee->pHTInfo->ePeerHTSpecVer != HT_SPEC_VER_EWC) {
1330             tag = skb_put(skb, ht_cap_len);
1331             *tag++ = MFIE_TYPE_HT_CAP;
1332             *tag++ = ht_cap_len - 2;
1333             memcpy(tag, ht_cap_buf, ht_cap_len - 2);
1334             tag += ht_cap_len - 2;
1335         }
1336     }
1337 
1338     if (wpa_ie_len) {
1339         skb_put_data(skb, ieee->wpa_ie, ieee->wpa_ie_len);
1340 
1341         if (PMKCacheIdx >= 0) {
1342             tag = skb_put(skb, 18);
1343             *tag = 1;
1344             *(tag + 1) = 0;
1345             memcpy((tag + 2), &ieee->PMKIDList[PMKCacheIdx].PMKID,
1346                    16);
1347         }
1348     }
1349     if (wmm_info_len) {
1350         tag = skb_put(skb, wmm_info_len);
1351         rtllib_WMM_Info(ieee, &tag);
1352     }
1353 
1354     if (wps_ie_len && ieee->wps_ie)
1355         skb_put_data(skb, ieee->wps_ie, wps_ie_len);
1356 
1357     if (turbo_info_len) {
1358         tag = skb_put(skb, turbo_info_len);
1359         rtllib_TURBO_Info(ieee, &tag);
1360     }
1361 
1362     if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1363         if (ieee->pHTInfo->ePeerHTSpecVer == HT_SPEC_VER_EWC) {
1364             tag = skb_put(skb, ht_cap_len);
1365             *tag++ = MFIE_TYPE_GENERIC;
1366             *tag++ = ht_cap_len - 2;
1367             memcpy(tag, ht_cap_buf, ht_cap_len - 2);
1368             tag += ht_cap_len - 2;
1369         }
1370 
1371         if (ieee->pHTInfo->bCurrentRT2RTAggregation) {
1372             tag = skb_put(skb, realtek_ie_len);
1373             *tag++ = MFIE_TYPE_GENERIC;
1374             *tag++ = realtek_ie_len - 2;
1375             memcpy(tag, realtek_ie_buf, realtek_ie_len - 2);
1376         }
1377     }
1378 
1379     kfree(ieee->assocreq_ies);
1380     ieee->assocreq_ies = NULL;
1381     ies = &(hdr->info_element[0].id);
1382     ieee->assocreq_ies_len = (skb->data + skb->len) - ies;
1383     ieee->assocreq_ies = kmemdup(ies, ieee->assocreq_ies_len, GFP_ATOMIC);
1384     if (!ieee->assocreq_ies)
1385         ieee->assocreq_ies_len = 0;
1386 
1387     return skb;
1388 }
1389 
1390 static void rtllib_associate_abort(struct rtllib_device *ieee)
1391 {
1392     unsigned long flags;
1393 
1394     spin_lock_irqsave(&ieee->lock, flags);
1395 
1396     ieee->associate_seq++;
1397 
1398     /* don't scan, and avoid to have the RX path possibily
1399      * try again to associate. Even do not react to AUTH or
1400      * ASSOC response. Just wait for the retry wq to be scheduled.
1401      * Here we will check if there are good nets to associate
1402      * with, so we retry or just get back to NO_LINK and scanning
1403      */
1404     if (ieee->state == RTLLIB_ASSOCIATING_AUTHENTICATING) {
1405         netdev_dbg(ieee->dev, "Authentication failed\n");
1406         ieee->softmac_stats.no_auth_rs++;
1407     } else {
1408         netdev_dbg(ieee->dev, "Association failed\n");
1409         ieee->softmac_stats.no_ass_rs++;
1410     }
1411 
1412     ieee->state = RTLLIB_ASSOCIATING_RETRY;
1413 
1414     schedule_delayed_work(&ieee->associate_retry_wq,
1415                   RTLLIB_SOFTMAC_ASSOC_RETRY_TIME);
1416 
1417     spin_unlock_irqrestore(&ieee->lock, flags);
1418 }
1419 
1420 static void rtllib_associate_abort_cb(struct timer_list *t)
1421 {
1422     struct rtllib_device *dev = from_timer(dev, t, associate_timer);
1423 
1424     rtllib_associate_abort(dev);
1425 }
1426 
1427 static void rtllib_associate_step1(struct rtllib_device *ieee, u8 *daddr)
1428 {
1429     struct rtllib_network *beacon = &ieee->current_network;
1430     struct sk_buff *skb;
1431 
1432     netdev_dbg(ieee->dev, "Stopping scan\n");
1433 
1434     ieee->softmac_stats.tx_auth_rq++;
1435 
1436     skb = rtllib_authentication_req(beacon, ieee, 0, daddr);
1437 
1438     if (!skb)
1439         rtllib_associate_abort(ieee);
1440     else {
1441         ieee->state = RTLLIB_ASSOCIATING_AUTHENTICATING;
1442         netdev_dbg(ieee->dev, "Sending authentication request\n");
1443         softmac_mgmt_xmit(skb, ieee);
1444         if (!timer_pending(&ieee->associate_timer)) {
1445             ieee->associate_timer.expires = jiffies + (HZ / 2);
1446             add_timer(&ieee->associate_timer);
1447         }
1448     }
1449 }
1450 
1451 static void rtllib_auth_challenge(struct rtllib_device *ieee, u8 *challenge,
1452                   int chlen)
1453 {
1454     u8 *c;
1455     struct sk_buff *skb;
1456     struct rtllib_network *beacon = &ieee->current_network;
1457 
1458     ieee->associate_seq++;
1459     ieee->softmac_stats.tx_auth_rq++;
1460 
1461     skb = rtllib_authentication_req(beacon, ieee, chlen + 2, beacon->bssid);
1462 
1463     if (!skb)
1464         rtllib_associate_abort(ieee);
1465     else {
1466         c = skb_put(skb, chlen+2);
1467         *(c++) = MFIE_TYPE_CHALLENGE;
1468         *(c++) = chlen;
1469         memcpy(c, challenge, chlen);
1470 
1471         netdev_dbg(ieee->dev,
1472                "Sending authentication challenge response\n");
1473 
1474         rtllib_encrypt_fragment(ieee, skb,
1475                     sizeof(struct rtllib_hdr_3addr));
1476 
1477         softmac_mgmt_xmit(skb, ieee);
1478         mod_timer(&ieee->associate_timer, jiffies + (HZ/2));
1479     }
1480     kfree(challenge);
1481 }
1482 
1483 static void rtllib_associate_step2(struct rtllib_device *ieee)
1484 {
1485     struct sk_buff *skb;
1486     struct rtllib_network *beacon = &ieee->current_network;
1487 
1488     del_timer_sync(&ieee->associate_timer);
1489 
1490     netdev_dbg(ieee->dev, "Sending association request\n");
1491 
1492     ieee->softmac_stats.tx_ass_rq++;
1493     skb = rtllib_association_req(beacon, ieee);
1494     if (!skb)
1495         rtllib_associate_abort(ieee);
1496     else {
1497         softmac_mgmt_xmit(skb, ieee);
1498         mod_timer(&ieee->associate_timer, jiffies + (HZ/2));
1499     }
1500 }
1501 
1502 static void rtllib_associate_complete_wq(void *data)
1503 {
1504     struct rtllib_device *ieee = (struct rtllib_device *)
1505                      container_of_work_rsl(data,
1506                      struct rtllib_device,
1507                      associate_complete_wq);
1508     struct rt_pwr_save_ctrl *pPSC = &(ieee->PowerSaveControl);
1509 
1510     netdev_info(ieee->dev, "Associated successfully with %pM\n",
1511             ieee->current_network.bssid);
1512     if (!ieee->is_silent_reset) {
1513         netdev_info(ieee->dev, "normal associate\n");
1514         notify_wx_assoc_event(ieee);
1515     }
1516 
1517     netif_carrier_on(ieee->dev);
1518     ieee->is_roaming = false;
1519     if (rtllib_is_54g(&ieee->current_network) &&
1520        (ieee->modulation & RTLLIB_OFDM_MODULATION)) {
1521         ieee->rate = 108;
1522         netdev_info(ieee->dev, "Using G rates:%d\n", ieee->rate);
1523     } else {
1524         ieee->rate = 22;
1525         ieee->SetWirelessMode(ieee->dev, IEEE_B);
1526         netdev_info(ieee->dev, "Using B rates:%d\n", ieee->rate);
1527     }
1528     if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1529         netdev_info(ieee->dev, "Successfully associated, ht enabled\n");
1530         HTOnAssocRsp(ieee);
1531     } else {
1532         netdev_info(ieee->dev,
1533                 "Successfully associated, ht not enabled(%d, %d)\n",
1534                 ieee->pHTInfo->bCurrentHTSupport,
1535                 ieee->pHTInfo->bEnableHT);
1536         memset(ieee->dot11HTOperationalRateSet, 0, 16);
1537     }
1538     ieee->LinkDetectInfo.SlotNum = 2 * (1 +
1539                        ieee->current_network.beacon_interval /
1540                        500);
1541     if (ieee->LinkDetectInfo.NumRecvBcnInPeriod == 0 ||
1542         ieee->LinkDetectInfo.NumRecvDataInPeriod == 0) {
1543         ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
1544         ieee->LinkDetectInfo.NumRecvDataInPeriod = 1;
1545     }
1546     pPSC->LpsIdleCount = 0;
1547     ieee->link_change(ieee->dev);
1548 
1549     if (ieee->is_silent_reset) {
1550         netdev_info(ieee->dev, "silent reset associate\n");
1551         ieee->is_silent_reset = false;
1552     }
1553 
1554     if (ieee->data_hard_resume)
1555         ieee->data_hard_resume(ieee->dev);
1556 
1557 }
1558 
1559 static void rtllib_sta_send_associnfo(struct rtllib_device *ieee)
1560 {
1561 }
1562 
1563 static void rtllib_associate_complete(struct rtllib_device *ieee)
1564 {
1565     del_timer_sync(&ieee->associate_timer);
1566 
1567     ieee->state = RTLLIB_LINKED;
1568     rtllib_sta_send_associnfo(ieee);
1569 
1570     schedule_work(&ieee->associate_complete_wq);
1571 }
1572 
1573 static void rtllib_associate_procedure_wq(void *data)
1574 {
1575     struct rtllib_device *ieee = container_of_dwork_rsl(data,
1576                      struct rtllib_device,
1577                      associate_procedure_wq);
1578     rtllib_stop_scan_syncro(ieee);
1579     if (ieee->rtllib_ips_leave != NULL)
1580         ieee->rtllib_ips_leave(ieee->dev);
1581     mutex_lock(&ieee->wx_mutex);
1582 
1583     if (ieee->data_hard_stop)
1584         ieee->data_hard_stop(ieee->dev);
1585 
1586     rtllib_stop_scan(ieee);
1587     RT_TRACE(COMP_DBG, "===>%s(), chan:%d\n", __func__,
1588          ieee->current_network.channel);
1589     HTSetConnectBwMode(ieee, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
1590     if (ieee->eRFPowerState == eRfOff) {
1591         RT_TRACE(COMP_DBG,
1592              "=============>%s():Rf state is eRfOff, schedule ipsleave wq again,return\n",
1593              __func__);
1594         if (ieee->rtllib_ips_leave_wq != NULL)
1595             ieee->rtllib_ips_leave_wq(ieee->dev);
1596         mutex_unlock(&ieee->wx_mutex);
1597         return;
1598     }
1599     ieee->associate_seq = 1;
1600 
1601     rtllib_associate_step1(ieee, ieee->current_network.bssid);
1602 
1603     mutex_unlock(&ieee->wx_mutex);
1604 }
1605 
1606 inline void rtllib_softmac_new_net(struct rtllib_device *ieee,
1607                    struct rtllib_network *net)
1608 {
1609     u8 tmp_ssid[IW_ESSID_MAX_SIZE + 1];
1610     int tmp_ssid_len = 0;
1611 
1612     short apset, ssidset, ssidbroad, apmatch, ssidmatch;
1613 
1614     /* we are interested in new new only if we are not associated
1615      * and we are not associating / authenticating
1616      */
1617     if (ieee->state != RTLLIB_NOLINK)
1618         return;
1619 
1620     if ((ieee->iw_mode == IW_MODE_INFRA) && !(net->capability &
1621         WLAN_CAPABILITY_ESS))
1622         return;
1623 
1624     if ((ieee->iw_mode == IW_MODE_ADHOC) && !(net->capability &
1625          WLAN_CAPABILITY_IBSS))
1626         return;
1627 
1628     if ((ieee->iw_mode == IW_MODE_ADHOC) &&
1629         (net->channel > ieee->ibss_maxjoin_chal))
1630         return;
1631     if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC) {
1632         /* if the user specified the AP MAC, we need also the essid
1633          * This could be obtained by beacons or, if the network does not
1634          * broadcast it, it can be put manually.
1635          */
1636         apset = ieee->wap_set;
1637         ssidset = ieee->ssid_set;
1638         ssidbroad =  !(net->ssid_len == 0 || net->ssid[0] == '\0');
1639         apmatch = (memcmp(ieee->current_network.bssid, net->bssid,
1640                   ETH_ALEN) == 0);
1641         if (!ssidbroad) {
1642             ssidmatch = (ieee->current_network.ssid_len ==
1643                     net->hidden_ssid_len) &&
1644                     (!strncmp(ieee->current_network.ssid,
1645                     net->hidden_ssid, net->hidden_ssid_len));
1646             if (net->hidden_ssid_len > 0) {
1647                 strncpy(net->ssid, net->hidden_ssid,
1648                     net->hidden_ssid_len);
1649                 net->ssid_len = net->hidden_ssid_len;
1650                 ssidbroad = 1;
1651             }
1652         } else
1653             ssidmatch =
1654                (ieee->current_network.ssid_len == net->ssid_len) &&
1655                (!strncmp(ieee->current_network.ssid, net->ssid,
1656                net->ssid_len));
1657 
1658         /* if the user set the AP check if match.
1659          * if the network does not broadcast essid we check the
1660          *   user supplied ANY essid
1661          * if the network does broadcast and the user does not set
1662          *   essid it is OK
1663          * if the network does broadcast and the user did set essid
1664          * check if essid match
1665          * if the ap is not set, check that the user set the bssid
1666          * and the network does broadcast and that those two bssid match
1667          */
1668         if ((apset && apmatch &&
1669            ((ssidset && ssidbroad && ssidmatch) ||
1670            (ssidbroad && !ssidset) || (!ssidbroad && ssidset))) ||
1671            (!apset && ssidset && ssidbroad && ssidmatch) ||
1672            (ieee->is_roaming && ssidset && ssidbroad && ssidmatch)) {
1673             /* Save the essid so that if it is hidden, it is
1674              * replaced with the essid provided by the user.
1675              */
1676             if (!ssidbroad) {
1677                 memcpy(tmp_ssid, ieee->current_network.ssid,
1678                        ieee->current_network.ssid_len);
1679                 tmp_ssid_len = ieee->current_network.ssid_len;
1680             }
1681             memcpy(&ieee->current_network, net,
1682                 sizeof(ieee->current_network));
1683             if (!ssidbroad) {
1684                 memcpy(ieee->current_network.ssid, tmp_ssid,
1685                        tmp_ssid_len);
1686                 ieee->current_network.ssid_len = tmp_ssid_len;
1687             }
1688             netdev_info(ieee->dev,
1689                     "Linking with %s,channel:%d, qos:%d, myHT:%d, networkHT:%d, mode:%x cur_net.flags:0x%x\n",
1690                     ieee->current_network.ssid,
1691                     ieee->current_network.channel,
1692                     ieee->current_network.qos_data.supported,
1693                     ieee->pHTInfo->bEnableHT,
1694                     ieee->current_network.bssht.bd_support_ht,
1695                     ieee->current_network.mode,
1696                     ieee->current_network.flags);
1697 
1698             if ((rtllib_act_scanning(ieee, false)) &&
1699                !(ieee->softmac_features & IEEE_SOFTMAC_SCAN))
1700                 rtllib_stop_scan_syncro(ieee);
1701 
1702             HTResetIOTSetting(ieee->pHTInfo);
1703             ieee->wmm_acm = 0;
1704             if (ieee->iw_mode == IW_MODE_INFRA) {
1705                 /* Join the network for the first time */
1706                 ieee->AsocRetryCount = 0;
1707                 if ((ieee->current_network.qos_data.supported == 1) &&
1708                     ieee->current_network.bssht.bd_support_ht)
1709                     HTResetSelfAndSavePeerSetting(ieee,
1710                          &(ieee->current_network));
1711                 else
1712                     ieee->pHTInfo->bCurrentHTSupport =
1713                                  false;
1714 
1715                 ieee->state = RTLLIB_ASSOCIATING;
1716                 if (ieee->LedControlHandler != NULL)
1717                     ieee->LedControlHandler(ieee->dev,
1718                              LED_CTL_START_TO_LINK);
1719                 schedule_delayed_work(
1720                        &ieee->associate_procedure_wq, 0);
1721             } else {
1722                 if (rtllib_is_54g(&ieee->current_network) &&
1723                     (ieee->modulation &
1724                      RTLLIB_OFDM_MODULATION)) {
1725                     ieee->rate = 108;
1726                     ieee->SetWirelessMode(ieee->dev,
1727                                   IEEE_G);
1728                     netdev_info(ieee->dev,
1729                             "Using G rates\n");
1730                 } else {
1731                     ieee->rate = 22;
1732                     ieee->SetWirelessMode(ieee->dev,
1733                                   IEEE_B);
1734                     netdev_info(ieee->dev,
1735                             "Using B rates\n");
1736                 }
1737                 memset(ieee->dot11HTOperationalRateSet, 0, 16);
1738                 ieee->state = RTLLIB_LINKED;
1739             }
1740         }
1741     }
1742 }
1743 
1744 static void rtllib_softmac_check_all_nets(struct rtllib_device *ieee)
1745 {
1746     unsigned long flags;
1747     struct rtllib_network *target;
1748 
1749     spin_lock_irqsave(&ieee->lock, flags);
1750 
1751     list_for_each_entry(target, &ieee->network_list, list) {
1752 
1753         /* if the state become different that NOLINK means
1754          * we had found what we are searching for
1755          */
1756 
1757         if (ieee->state != RTLLIB_NOLINK)
1758             break;
1759 
1760         if (ieee->scan_age == 0 || time_after(target->last_scanned +
1761             ieee->scan_age, jiffies))
1762             rtllib_softmac_new_net(ieee, target);
1763     }
1764     spin_unlock_irqrestore(&ieee->lock, flags);
1765 }
1766 
1767 static inline int auth_parse(struct net_device *dev, struct sk_buff *skb,
1768                  u8 **challenge, int *chlen)
1769 {
1770     struct rtllib_authentication *a;
1771     u8 *t;
1772 
1773     if (skb->len <  (sizeof(struct rtllib_authentication) -
1774         sizeof(struct rtllib_info_element))) {
1775         netdev_dbg(dev, "invalid len in auth resp: %d\n", skb->len);
1776         return -EINVAL;
1777     }
1778     *challenge = NULL;
1779     a = (struct rtllib_authentication *)skb->data;
1780     if (skb->len > (sizeof(struct rtllib_authentication) + 3)) {
1781         t = skb->data + sizeof(struct rtllib_authentication);
1782 
1783         if (*(t++) == MFIE_TYPE_CHALLENGE) {
1784             *chlen = *(t++);
1785             *challenge = kmemdup(t, *chlen, GFP_ATOMIC);
1786             if (!*challenge)
1787                 return -ENOMEM;
1788         }
1789     }
1790 
1791     if (a->status) {
1792         netdev_dbg(dev, "auth_parse() failed\n");
1793         return -EINVAL;
1794     }
1795 
1796     return 0;
1797 }
1798 
1799 static int auth_rq_parse(struct net_device *dev, struct sk_buff *skb, u8 *dest)
1800 {
1801     struct rtllib_authentication *a;
1802 
1803     if (skb->len <  (sizeof(struct rtllib_authentication) -
1804         sizeof(struct rtllib_info_element))) {
1805         netdev_dbg(dev, "invalid len in auth request: %d\n", skb->len);
1806         return -1;
1807     }
1808     a = (struct rtllib_authentication *)skb->data;
1809 
1810     ether_addr_copy(dest, a->header.addr2);
1811 
1812     if (le16_to_cpu(a->algorithm) != WLAN_AUTH_OPEN)
1813         return  WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1814 
1815     return WLAN_STATUS_SUCCESS;
1816 }
1817 
1818 static short probe_rq_parse(struct rtllib_device *ieee, struct sk_buff *skb,
1819                 u8 *src)
1820 {
1821     u8 *tag;
1822     u8 *skbend;
1823     u8 *ssid = NULL;
1824     u8 ssidlen = 0;
1825     struct rtllib_hdr_3addr   *header =
1826         (struct rtllib_hdr_3addr   *)skb->data;
1827     bool bssid_match;
1828 
1829     if (skb->len < sizeof(struct rtllib_hdr_3addr))
1830         return -1; /* corrupted */
1831 
1832     bssid_match =
1833       (!ether_addr_equal(header->addr3, ieee->current_network.bssid)) &&
1834       (!is_broadcast_ether_addr(header->addr3));
1835     if (bssid_match)
1836         return -1;
1837 
1838     ether_addr_copy(src, header->addr2);
1839 
1840     skbend = (u8 *)skb->data + skb->len;
1841 
1842     tag = skb->data + sizeof(struct rtllib_hdr_3addr);
1843 
1844     while (tag + 1 < skbend) {
1845         if (*tag == 0) {
1846             ssid = tag + 2;
1847             ssidlen = *(tag + 1);
1848             break;
1849         }
1850         tag++; /* point to the len field */
1851         tag = tag + *(tag); /* point to the last data byte of the tag */
1852         tag++; /* point to the next tag */
1853     }
1854 
1855     if (ssidlen == 0)
1856         return 1;
1857 
1858     if (!ssid)
1859         return 1; /* ssid not found in tagged param */
1860 
1861     return !strncmp(ssid, ieee->current_network.ssid, ssidlen);
1862 }
1863 
1864 static int assoc_rq_parse(struct net_device *dev, struct sk_buff *skb, u8 *dest)
1865 {
1866     struct rtllib_assoc_request_frame *a;
1867 
1868     if (skb->len < (sizeof(struct rtllib_assoc_request_frame) -
1869         sizeof(struct rtllib_info_element))) {
1870         netdev_dbg(dev, "invalid len in auth request:%d\n", skb->len);
1871         return -1;
1872     }
1873 
1874     a = (struct rtllib_assoc_request_frame *)skb->data;
1875 
1876     ether_addr_copy(dest, a->header.addr2);
1877 
1878     return 0;
1879 }
1880 
1881 static inline u16 assoc_parse(struct rtllib_device *ieee, struct sk_buff *skb,
1882                   int *aid)
1883 {
1884     struct rtllib_assoc_response_frame *response_head;
1885     u16 status_code;
1886 
1887     if (skb->len <  sizeof(struct rtllib_assoc_response_frame)) {
1888         netdev_dbg(ieee->dev, "Invalid len in auth resp: %d\n",
1889                skb->len);
1890         return 0xcafe;
1891     }
1892 
1893     response_head = (struct rtllib_assoc_response_frame *)skb->data;
1894     *aid = le16_to_cpu(response_head->aid) & 0x3fff;
1895 
1896     status_code = le16_to_cpu(response_head->status);
1897     if ((status_code == WLAN_STATUS_ASSOC_DENIED_RATES ||
1898        status_code == WLAN_STATUS_CAPS_UNSUPPORTED) &&
1899        ((ieee->mode == IEEE_G) &&
1900        (ieee->current_network.mode == IEEE_N_24G) &&
1901        (ieee->AsocRetryCount++ < (RT_ASOC_RETRY_LIMIT-1)))) {
1902         ieee->pHTInfo->IOTAction |= HT_IOT_ACT_PURE_N_MODE;
1903     } else {
1904         ieee->AsocRetryCount = 0;
1905     }
1906 
1907     return le16_to_cpu(response_head->status);
1908 }
1909 
1910 void rtllib_rx_probe_rq(struct rtllib_device *ieee, struct sk_buff *skb)
1911 {
1912     u8 dest[ETH_ALEN];
1913 
1914     ieee->softmac_stats.rx_probe_rq++;
1915     if (probe_rq_parse(ieee, skb, dest) > 0) {
1916         ieee->softmac_stats.tx_probe_rs++;
1917         rtllib_resp_to_probe(ieee, dest);
1918     }
1919 }
1920 
1921 static inline void rtllib_rx_auth_rq(struct rtllib_device *ieee,
1922                      struct sk_buff *skb)
1923 {
1924     u8 dest[ETH_ALEN];
1925     int status;
1926 
1927     ieee->softmac_stats.rx_auth_rq++;
1928 
1929     status = auth_rq_parse(ieee->dev, skb, dest);
1930     if (status != -1)
1931         rtllib_resp_to_auth(ieee, status, dest);
1932 }
1933 
1934 static inline void rtllib_rx_assoc_rq(struct rtllib_device *ieee,
1935                       struct sk_buff *skb)
1936 {
1937     u8 dest[ETH_ALEN];
1938 
1939 
1940     ieee->softmac_stats.rx_ass_rq++;
1941     if (assoc_rq_parse(ieee->dev, skb, dest) != -1)
1942         rtllib_resp_to_assoc_rq(ieee, dest);
1943 
1944     netdev_info(ieee->dev, "New client associated: %pM\n", dest);
1945 }
1946 
1947 void rtllib_sta_ps_send_null_frame(struct rtllib_device *ieee, short pwr)
1948 {
1949 
1950     struct sk_buff *buf = rtllib_null_func(ieee, pwr);
1951 
1952     if (buf)
1953         softmac_ps_mgmt_xmit(buf, ieee);
1954 }
1955 EXPORT_SYMBOL(rtllib_sta_ps_send_null_frame);
1956 
1957 void rtllib_sta_ps_send_pspoll_frame(struct rtllib_device *ieee)
1958 {
1959     struct sk_buff *buf = rtllib_pspoll_func(ieee);
1960 
1961     if (buf)
1962         softmac_ps_mgmt_xmit(buf, ieee);
1963 }
1964 
1965 static short rtllib_sta_ps_sleep(struct rtllib_device *ieee, u64 *time)
1966 {
1967     int timeout;
1968     u8 dtim;
1969     struct rt_pwr_save_ctrl *pPSC = &(ieee->PowerSaveControl);
1970 
1971     if (ieee->LPSDelayCnt) {
1972         ieee->LPSDelayCnt--;
1973         return 0;
1974     }
1975 
1976     dtim = ieee->current_network.dtim_data;
1977     if (!(dtim & RTLLIB_DTIM_VALID))
1978         return 0;
1979     timeout = ieee->current_network.beacon_interval;
1980     ieee->current_network.dtim_data = RTLLIB_DTIM_INVALID;
1981     /* there's no need to nofity AP that I find you buffered
1982      * with broadcast packet
1983      */
1984     if (dtim & (RTLLIB_DTIM_UCAST & ieee->ps))
1985         return 2;
1986 
1987     if (!time_after(jiffies,
1988             dev_trans_start(ieee->dev) + msecs_to_jiffies(timeout)))
1989         return 0;
1990     if (!time_after(jiffies,
1991             ieee->last_rx_ps_time + msecs_to_jiffies(timeout)))
1992         return 0;
1993     if ((ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE) &&
1994         (ieee->mgmt_queue_tail != ieee->mgmt_queue_head))
1995         return 0;
1996 
1997     if (time) {
1998         if (ieee->bAwakePktSent) {
1999             pPSC->LPSAwakeIntvl = 1;
2000         } else {
2001             u8 MaxPeriod = 1;
2002 
2003             if (pPSC->LPSAwakeIntvl == 0)
2004                 pPSC->LPSAwakeIntvl = 1;
2005             if (pPSC->RegMaxLPSAwakeIntvl == 0)
2006                 MaxPeriod = 1;
2007             else if (pPSC->RegMaxLPSAwakeIntvl == 0xFF)
2008                 MaxPeriod = ieee->current_network.dtim_period;
2009             else
2010                 MaxPeriod = pPSC->RegMaxLPSAwakeIntvl;
2011             pPSC->LPSAwakeIntvl = (pPSC->LPSAwakeIntvl >=
2012                            MaxPeriod) ? MaxPeriod :
2013                            (pPSC->LPSAwakeIntvl + 1);
2014         }
2015         {
2016             u8 LPSAwakeIntvl_tmp = 0;
2017             u8 period = ieee->current_network.dtim_period;
2018             u8 count = ieee->current_network.tim.tim_count;
2019 
2020             if (count == 0) {
2021                 if (pPSC->LPSAwakeIntvl > period)
2022                     LPSAwakeIntvl_tmp = period +
2023                          (pPSC->LPSAwakeIntvl -
2024                          period) -
2025                          ((pPSC->LPSAwakeIntvl-period) %
2026                          period);
2027                 else
2028                     LPSAwakeIntvl_tmp = pPSC->LPSAwakeIntvl;
2029 
2030             } else {
2031                 if (pPSC->LPSAwakeIntvl >
2032                     ieee->current_network.tim.tim_count)
2033                     LPSAwakeIntvl_tmp = count +
2034                     (pPSC->LPSAwakeIntvl - count) -
2035                     ((pPSC->LPSAwakeIntvl-count)%period);
2036                 else
2037                     LPSAwakeIntvl_tmp = pPSC->LPSAwakeIntvl;
2038             }
2039 
2040         *time = ieee->current_network.last_dtim_sta_time
2041             + msecs_to_jiffies(ieee->current_network.beacon_interval *
2042             LPSAwakeIntvl_tmp);
2043     }
2044     }
2045 
2046     return 1;
2047 
2048 
2049 }
2050 
2051 static inline void rtllib_sta_ps(struct work_struct *work)
2052 {
2053     struct rtllib_device *ieee;
2054     u64 time;
2055     short sleep;
2056     unsigned long flags, flags2;
2057 
2058     ieee = container_of(work, struct rtllib_device, ps_task);
2059 
2060     spin_lock_irqsave(&ieee->lock, flags);
2061 
2062     if ((ieee->ps == RTLLIB_PS_DISABLED ||
2063          ieee->iw_mode != IW_MODE_INFRA ||
2064          ieee->state != RTLLIB_LINKED)) {
2065         RT_TRACE(COMP_DBG,
2066              "=====>%s(): no need to ps,wake up!! ieee->ps is %d, ieee->iw_mode is %d, ieee->state is %d\n",
2067              __func__, ieee->ps, ieee->iw_mode, ieee->state);
2068         spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2069         rtllib_sta_wakeup(ieee, 1);
2070 
2071         spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2072     }
2073     sleep = rtllib_sta_ps_sleep(ieee, &time);
2074     /* 2 wake, 1 sleep, 0 do nothing */
2075     if (sleep == 0)
2076         goto out;
2077     if (sleep == 1) {
2078         if (ieee->sta_sleep == LPS_IS_SLEEP) {
2079             ieee->enter_sleep_state(ieee->dev, time);
2080         } else if (ieee->sta_sleep == LPS_IS_WAKE) {
2081             spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2082 
2083             if (ieee->ps_is_queue_empty(ieee->dev)) {
2084                 ieee->sta_sleep = LPS_WAIT_NULL_DATA_SEND;
2085                 ieee->ack_tx_to_ieee = 1;
2086                 rtllib_sta_ps_send_null_frame(ieee, 1);
2087                 ieee->ps_time = time;
2088             }
2089             spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2090 
2091         }
2092 
2093         ieee->bAwakePktSent = false;
2094 
2095     } else if (sleep == 2) {
2096         spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2097 
2098         rtllib_sta_wakeup(ieee, 1);
2099 
2100         spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2101     }
2102 
2103 out:
2104     spin_unlock_irqrestore(&ieee->lock, flags);
2105 
2106 }
2107 
2108 static void rtllib_sta_wakeup(struct rtllib_device *ieee, short nl)
2109 {
2110     if (ieee->sta_sleep == LPS_IS_WAKE) {
2111         if (nl) {
2112             if (ieee->pHTInfo->IOTAction &
2113                 HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
2114                 ieee->ack_tx_to_ieee = 1;
2115                 rtllib_sta_ps_send_null_frame(ieee, 0);
2116             } else {
2117                 ieee->ack_tx_to_ieee = 1;
2118                 rtllib_sta_ps_send_pspoll_frame(ieee);
2119             }
2120         }
2121         return;
2122 
2123     }
2124 
2125     if (ieee->sta_sleep == LPS_IS_SLEEP)
2126         ieee->sta_wake_up(ieee->dev);
2127     if (nl) {
2128         if (ieee->pHTInfo->IOTAction &
2129             HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
2130             ieee->ack_tx_to_ieee = 1;
2131             rtllib_sta_ps_send_null_frame(ieee, 0);
2132         } else {
2133             ieee->ack_tx_to_ieee = 1;
2134             ieee->polling = true;
2135             rtllib_sta_ps_send_pspoll_frame(ieee);
2136         }
2137 
2138     } else {
2139         ieee->sta_sleep = LPS_IS_WAKE;
2140         ieee->polling = false;
2141     }
2142 }
2143 
2144 void rtllib_ps_tx_ack(struct rtllib_device *ieee, short success)
2145 {
2146     unsigned long flags, flags2;
2147 
2148     spin_lock_irqsave(&ieee->lock, flags);
2149 
2150     if (ieee->sta_sleep == LPS_WAIT_NULL_DATA_SEND) {
2151         /* Null frame with PS bit set */
2152         if (success) {
2153             ieee->sta_sleep = LPS_IS_SLEEP;
2154             ieee->enter_sleep_state(ieee->dev, ieee->ps_time);
2155         }
2156         /* if the card report not success we can't be sure the AP
2157          * has not RXed so we can't assume the AP believe us awake
2158          */
2159     } else {/* 21112005 - tx again null without PS bit if lost */
2160 
2161         if ((ieee->sta_sleep == LPS_IS_WAKE) && !success) {
2162             spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2163             if (ieee->pHTInfo->IOTAction &
2164                 HT_IOT_ACT_NULL_DATA_POWER_SAVING)
2165                 rtllib_sta_ps_send_null_frame(ieee, 0);
2166             else
2167                 rtllib_sta_ps_send_pspoll_frame(ieee);
2168             spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2169         }
2170     }
2171     spin_unlock_irqrestore(&ieee->lock, flags);
2172 }
2173 EXPORT_SYMBOL(rtllib_ps_tx_ack);
2174 
2175 static void rtllib_process_action(struct rtllib_device *ieee,
2176                   struct sk_buff *skb)
2177 {
2178     struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *)skb->data;
2179     u8 *act = rtllib_get_payload((struct rtllib_hdr *)header);
2180     u8 category = 0;
2181 
2182     if (act == NULL) {
2183         netdev_warn(ieee->dev,
2184                 "Error getting payload of action frame\n");
2185         return;
2186     }
2187 
2188     category = *act;
2189     act++;
2190     switch (category) {
2191     case ACT_CAT_BA:
2192         switch (*act) {
2193         case ACT_ADDBAREQ:
2194             rtllib_rx_ADDBAReq(ieee, skb);
2195             break;
2196         case ACT_ADDBARSP:
2197             rtllib_rx_ADDBARsp(ieee, skb);
2198             break;
2199         case ACT_DELBA:
2200             rtllib_rx_DELBA(ieee, skb);
2201             break;
2202         }
2203         break;
2204     default:
2205         break;
2206     }
2207 }
2208 
2209 static inline int
2210 rtllib_rx_assoc_resp(struct rtllib_device *ieee, struct sk_buff *skb,
2211              struct rtllib_rx_stats *rx_stats)
2212 {
2213     u16 errcode;
2214     int aid;
2215     u8 *ies;
2216     struct rtllib_assoc_response_frame *assoc_resp;
2217     struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *)skb->data;
2218     u16 frame_ctl = le16_to_cpu(header->frame_ctl);
2219 
2220     netdev_dbg(ieee->dev, "received [RE]ASSOCIATION RESPONSE (%d)\n",
2221            WLAN_FC_GET_STYPE(frame_ctl));
2222 
2223     if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2224          ieee->state == RTLLIB_ASSOCIATING_AUTHENTICATED &&
2225          (ieee->iw_mode == IW_MODE_INFRA)) {
2226         errcode = assoc_parse(ieee, skb, &aid);
2227         if (!errcode) {
2228             struct rtllib_network *network =
2229                  kzalloc(sizeof(struct rtllib_network),
2230                  GFP_ATOMIC);
2231 
2232             if (!network)
2233                 return 1;
2234             ieee->state = RTLLIB_LINKED;
2235             ieee->assoc_id = aid;
2236             ieee->softmac_stats.rx_ass_ok++;
2237             /* station support qos */
2238             /* Let the register setting default with Legacy station */
2239             assoc_resp = (struct rtllib_assoc_response_frame *)skb->data;
2240             if (ieee->current_network.qos_data.supported == 1) {
2241                 if (rtllib_parse_info_param(ieee, assoc_resp->info_element,
2242                             rx_stats->len - sizeof(*assoc_resp),
2243                             network, rx_stats)) {
2244                     kfree(network);
2245                     return 1;
2246                 }
2247                 memcpy(ieee->pHTInfo->PeerHTCapBuf,
2248                        network->bssht.bd_ht_cap_buf,
2249                        network->bssht.bd_ht_cap_len);
2250                 memcpy(ieee->pHTInfo->PeerHTInfoBuf,
2251                        network->bssht.bd_ht_info_buf,
2252                        network->bssht.bd_ht_info_len);
2253                 if (ieee->handle_assoc_response != NULL)
2254                     ieee->handle_assoc_response(ieee->dev,
2255                          (struct rtllib_assoc_response_frame *)header,
2256                          network);
2257             }
2258             kfree(network);
2259 
2260             kfree(ieee->assocresp_ies);
2261             ieee->assocresp_ies = NULL;
2262             ies = &(assoc_resp->info_element[0].id);
2263             ieee->assocresp_ies_len = (skb->data + skb->len) - ies;
2264             ieee->assocresp_ies = kmemdup(ies,
2265                               ieee->assocresp_ies_len,
2266                               GFP_ATOMIC);
2267             if (!ieee->assocresp_ies)
2268                 ieee->assocresp_ies_len = 0;
2269 
2270             rtllib_associate_complete(ieee);
2271         } else {
2272             /* aid could not been allocated */
2273             ieee->softmac_stats.rx_ass_err++;
2274             netdev_info(ieee->dev,
2275                     "Association response status code 0x%x\n",
2276                     errcode);
2277             if (ieee->AsocRetryCount < RT_ASOC_RETRY_LIMIT)
2278                 schedule_delayed_work(
2279                      &ieee->associate_procedure_wq, 0);
2280             else
2281                 rtllib_associate_abort(ieee);
2282         }
2283     }
2284     return 0;
2285 }
2286 
2287 static void rtllib_rx_auth_resp(struct rtllib_device *ieee, struct sk_buff *skb)
2288 {
2289     int errcode;
2290     u8 *challenge;
2291     int chlen = 0;
2292     bool bSupportNmode = true, bHalfSupportNmode = false;
2293 
2294     errcode = auth_parse(ieee->dev, skb, &challenge, &chlen);
2295 
2296     if (errcode) {
2297         ieee->softmac_stats.rx_auth_rs_err++;
2298         netdev_info(ieee->dev,
2299                 "Authentication response status code %d", errcode);
2300         rtllib_associate_abort(ieee);
2301         return;
2302     }
2303 
2304     if (ieee->open_wep || !challenge) {
2305         ieee->state = RTLLIB_ASSOCIATING_AUTHENTICATED;
2306         ieee->softmac_stats.rx_auth_rs_ok++;
2307         if (!(ieee->pHTInfo->IOTAction & HT_IOT_ACT_PURE_N_MODE)) {
2308             if (!ieee->GetNmodeSupportBySecCfg(ieee->dev)) {
2309                 if (IsHTHalfNmodeAPs(ieee)) {
2310                     bSupportNmode = true;
2311                     bHalfSupportNmode = true;
2312                 } else {
2313                     bSupportNmode = false;
2314                     bHalfSupportNmode = false;
2315                 }
2316             }
2317         }
2318         /* Dummy wirless mode setting to avoid encryption issue */
2319         if (bSupportNmode) {
2320             ieee->SetWirelessMode(ieee->dev,
2321                           ieee->current_network.mode);
2322         } else {
2323             /*TODO*/
2324             ieee->SetWirelessMode(ieee->dev, IEEE_G);
2325         }
2326 
2327         if ((ieee->current_network.mode == IEEE_N_24G) &&
2328             bHalfSupportNmode) {
2329             netdev_info(ieee->dev, "======>enter half N mode\n");
2330             ieee->bHalfWirelessN24GMode = true;
2331         } else {
2332             ieee->bHalfWirelessN24GMode = false;
2333         }
2334         rtllib_associate_step2(ieee);
2335     } else {
2336         rtllib_auth_challenge(ieee, challenge,  chlen);
2337     }
2338 }
2339 
2340 static inline int
2341 rtllib_rx_auth(struct rtllib_device *ieee, struct sk_buff *skb,
2342            struct rtllib_rx_stats *rx_stats)
2343 {
2344 
2345     if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) {
2346         if (ieee->state == RTLLIB_ASSOCIATING_AUTHENTICATING &&
2347             (ieee->iw_mode == IW_MODE_INFRA)) {
2348             netdev_dbg(ieee->dev,
2349                    "Received authentication response");
2350             rtllib_rx_auth_resp(ieee, skb);
2351         } else if (ieee->iw_mode == IW_MODE_MASTER) {
2352             rtllib_rx_auth_rq(ieee, skb);
2353         }
2354     }
2355     return 0;
2356 }
2357 
2358 static inline int
2359 rtllib_rx_deauth(struct rtllib_device *ieee, struct sk_buff *skb)
2360 {
2361     struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *)skb->data;
2362     u16 frame_ctl;
2363 
2364     if (memcmp(header->addr3, ieee->current_network.bssid, ETH_ALEN) != 0)
2365         return 0;
2366 
2367     /* FIXME for now repeat all the association procedure
2368      * both for disassociation and deauthentication
2369      */
2370     if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2371         ieee->state == RTLLIB_LINKED &&
2372         (ieee->iw_mode == IW_MODE_INFRA)) {
2373         frame_ctl = le16_to_cpu(header->frame_ctl);
2374         netdev_info(ieee->dev,
2375                 "==========>received disassoc/deauth(%x) frame, reason code:%x\n",
2376                 WLAN_FC_GET_STYPE(frame_ctl),
2377                 ((struct rtllib_disassoc *)skb->data)->reason);
2378         ieee->state = RTLLIB_ASSOCIATING;
2379         ieee->softmac_stats.reassoc++;
2380         ieee->is_roaming = true;
2381         ieee->LinkDetectInfo.bBusyTraffic = false;
2382         rtllib_disassociate(ieee);
2383         RemovePeerTS(ieee, header->addr2);
2384         if (ieee->LedControlHandler != NULL)
2385             ieee->LedControlHandler(ieee->dev,
2386                         LED_CTL_START_TO_LINK);
2387 
2388         if (!(ieee->rtllib_ap_sec_type(ieee) &
2389             (SEC_ALG_CCMP|SEC_ALG_TKIP)))
2390             schedule_delayed_work(
2391                        &ieee->associate_procedure_wq, 5);
2392     }
2393     return 0;
2394 }
2395 
2396 inline int rtllib_rx_frame_softmac(struct rtllib_device *ieee,
2397                    struct sk_buff *skb,
2398                    struct rtllib_rx_stats *rx_stats, u16 type,
2399                    u16 stype)
2400 {
2401     struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *)skb->data;
2402     u16 frame_ctl;
2403 
2404     if (!ieee->proto_started)
2405         return 0;
2406 
2407     frame_ctl = le16_to_cpu(header->frame_ctl);
2408     switch (WLAN_FC_GET_STYPE(frame_ctl)) {
2409     case RTLLIB_STYPE_ASSOC_RESP:
2410     case RTLLIB_STYPE_REASSOC_RESP:
2411         if (rtllib_rx_assoc_resp(ieee, skb, rx_stats) == 1)
2412             return 1;
2413         break;
2414     case RTLLIB_STYPE_ASSOC_REQ:
2415     case RTLLIB_STYPE_REASSOC_REQ:
2416         if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2417              ieee->iw_mode == IW_MODE_MASTER)
2418             rtllib_rx_assoc_rq(ieee, skb);
2419         break;
2420     case RTLLIB_STYPE_AUTH:
2421         rtllib_rx_auth(ieee, skb, rx_stats);
2422         break;
2423     case RTLLIB_STYPE_DISASSOC:
2424     case RTLLIB_STYPE_DEAUTH:
2425         rtllib_rx_deauth(ieee, skb);
2426         break;
2427     case RTLLIB_STYPE_MANAGE_ACT:
2428         rtllib_process_action(ieee, skb);
2429         break;
2430     default:
2431         return -1;
2432     }
2433     return 0;
2434 }
2435 
2436 /* following are for a simpler TX queue management.
2437  * Instead of using netif_[stop/wake]_queue the driver
2438  * will use these two functions (plus a reset one), that
2439  * will internally use the kernel netif_* and takes
2440  * care of the ieee802.11 fragmentation.
2441  * So the driver receives a fragment per time and might
2442  * call the stop function when it wants to not
2443  * have enough room to TX an entire packet.
2444  * This might be useful if each fragment needs it's own
2445  * descriptor, thus just keep a total free memory > than
2446  * the max fragmentation threshold is not enough.. If the
2447  * ieee802.11 stack passed a TXB struct then you need
2448  * to keep N free descriptors where
2449  * N = MAX_PACKET_SIZE / MIN_FRAG_TRESHOLD
2450  * In this way you need just one and the 802.11 stack
2451  * will take care of buffering fragments and pass them to
2452  * the driver later, when it wakes the queue.
2453  */
2454 void rtllib_softmac_xmit(struct rtllib_txb *txb, struct rtllib_device *ieee)
2455 {
2456 
2457     unsigned int queue_index = txb->queue_index;
2458     unsigned long flags;
2459     int  i;
2460     struct cb_desc *tcb_desc = NULL;
2461     unsigned long queue_len = 0;
2462 
2463     spin_lock_irqsave(&ieee->lock, flags);
2464 
2465     /* called with 2nd parm 0, no tx mgmt lock required */
2466     rtllib_sta_wakeup(ieee, 0);
2467 
2468     /* update the tx status */
2469     tcb_desc = (struct cb_desc *)(txb->fragments[0]->cb +
2470            MAX_DEV_ADDR_SIZE);
2471     if (tcb_desc->bMulticast)
2472         ieee->stats.multicast++;
2473 
2474     /* if xmit available, just xmit it immediately, else just insert it to
2475      * the wait queue
2476      */
2477     for (i = 0; i < txb->nr_frags; i++) {
2478         queue_len = skb_queue_len(&ieee->skb_waitQ[queue_index]);
2479         if ((queue_len  != 0) ||
2480             (!ieee->check_nic_enough_desc(ieee->dev, queue_index)) ||
2481             (ieee->queue_stop)) {
2482             /* insert the skb packet to the wait queue
2483              * as for the completion function, it does not need
2484              * to check it any more.
2485              */
2486             if (queue_len < 200)
2487                 skb_queue_tail(&ieee->skb_waitQ[queue_index],
2488                            txb->fragments[i]);
2489             else
2490                 kfree_skb(txb->fragments[i]);
2491         } else {
2492             ieee->softmac_data_hard_start_xmit(
2493                     txb->fragments[i],
2494                     ieee->dev, ieee->rate);
2495         }
2496     }
2497 
2498     rtllib_txb_free(txb);
2499 
2500     spin_unlock_irqrestore(&ieee->lock, flags);
2501 
2502 }
2503 
2504 void rtllib_reset_queue(struct rtllib_device *ieee)
2505 {
2506     unsigned long flags;
2507 
2508     spin_lock_irqsave(&ieee->lock, flags);
2509     init_mgmt_queue(ieee);
2510     if (ieee->tx_pending.txb) {
2511         rtllib_txb_free(ieee->tx_pending.txb);
2512         ieee->tx_pending.txb = NULL;
2513     }
2514     ieee->queue_stop = 0;
2515     spin_unlock_irqrestore(&ieee->lock, flags);
2516 
2517 }
2518 EXPORT_SYMBOL(rtllib_reset_queue);
2519 
2520 void rtllib_stop_all_queues(struct rtllib_device *ieee)
2521 {
2522     unsigned int i;
2523 
2524     for (i = 0; i < ieee->dev->num_tx_queues; i++)
2525         txq_trans_cond_update(netdev_get_tx_queue(ieee->dev, i));
2526 
2527     netif_tx_stop_all_queues(ieee->dev);
2528 }
2529 
2530 void rtllib_wake_all_queues(struct rtllib_device *ieee)
2531 {
2532     netif_tx_wake_all_queues(ieee->dev);
2533 }
2534 
2535 /* called in user context only */
2536 static void rtllib_start_master_bss(struct rtllib_device *ieee)
2537 {
2538     ieee->assoc_id = 1;
2539 
2540     if (ieee->current_network.ssid_len == 0) {
2541         strncpy(ieee->current_network.ssid,
2542             RTLLIB_DEFAULT_TX_ESSID,
2543             IW_ESSID_MAX_SIZE);
2544 
2545         ieee->current_network.ssid_len =
2546                  strlen(RTLLIB_DEFAULT_TX_ESSID);
2547         ieee->ssid_set = 1;
2548     }
2549 
2550     ether_addr_copy(ieee->current_network.bssid, ieee->dev->dev_addr);
2551 
2552     ieee->set_chan(ieee->dev, ieee->current_network.channel);
2553     ieee->state = RTLLIB_LINKED;
2554     ieee->link_change(ieee->dev);
2555     notify_wx_assoc_event(ieee);
2556 
2557     if (ieee->data_hard_resume)
2558         ieee->data_hard_resume(ieee->dev);
2559 
2560     netif_carrier_on(ieee->dev);
2561 }
2562 
2563 static void rtllib_start_monitor_mode(struct rtllib_device *ieee)
2564 {
2565     /* reset hardware status */
2566     if (ieee->raw_tx) {
2567         if (ieee->data_hard_resume)
2568             ieee->data_hard_resume(ieee->dev);
2569 
2570         netif_carrier_on(ieee->dev);
2571     }
2572 }
2573 
2574 static void rtllib_start_ibss_wq(void *data)
2575 {
2576     struct rtllib_device *ieee = container_of_dwork_rsl(data,
2577                      struct rtllib_device, start_ibss_wq);
2578     /* iwconfig mode ad-hoc will schedule this and return
2579      * on the other hand this will block further iwconfig SET
2580      * operations because of the wx_mutex hold.
2581      * Anyway some most set operations set a flag to speed-up
2582      * (abort) this wq (when syncro scanning) before sleeping
2583      * on the mutex
2584      */
2585     if (!ieee->proto_started) {
2586         netdev_info(ieee->dev, "==========oh driver down return\n");
2587         return;
2588     }
2589     mutex_lock(&ieee->wx_mutex);
2590 
2591     if (ieee->current_network.ssid_len == 0) {
2592         strscpy(ieee->current_network.ssid, RTLLIB_DEFAULT_TX_ESSID,
2593             sizeof(ieee->current_network.ssid));
2594         ieee->current_network.ssid_len = strlen(RTLLIB_DEFAULT_TX_ESSID);
2595         ieee->ssid_set = 1;
2596     }
2597 
2598     ieee->state = RTLLIB_NOLINK;
2599     ieee->mode = IEEE_G;
2600     /* check if we have this cell in our network list */
2601     rtllib_softmac_check_all_nets(ieee);
2602 
2603 
2604     /* if not then the state is not linked. Maybe the user switched to
2605      * ad-hoc mode just after being in monitor mode, or just after
2606      * being very few time in managed mode (so the card have had no
2607      * time to scan all the chans..) or we have just run up the iface
2608      * after setting ad-hoc mode. So we have to give another try..
2609      * Here, in ibss mode, should be safe to do this without extra care
2610      * (in bss mode we had to make sure no-one tried to associate when
2611      * we had just checked the ieee->state and we was going to start the
2612      * scan) because in ibss mode the rtllib_new_net function, when
2613      * finds a good net, just set the ieee->state to RTLLIB_LINKED,
2614      * so, at worst, we waste a bit of time to initiate an unneeded syncro
2615      * scan, that will stop at the first round because it sees the state
2616      * associated.
2617      */
2618     if (ieee->state == RTLLIB_NOLINK)
2619         rtllib_start_scan_syncro(ieee, 0);
2620 
2621     /* the network definitively is not here.. create a new cell */
2622     if (ieee->state == RTLLIB_NOLINK) {
2623         netdev_info(ieee->dev, "creating new IBSS cell\n");
2624         ieee->current_network.channel = ieee->bss_start_channel;
2625         if (!ieee->wap_set)
2626             eth_random_addr(ieee->current_network.bssid);
2627 
2628         if (ieee->modulation & RTLLIB_CCK_MODULATION) {
2629 
2630             ieee->current_network.rates_len = 4;
2631 
2632             ieee->current_network.rates[0] =
2633                  RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_1MB;
2634             ieee->current_network.rates[1] =
2635                  RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_2MB;
2636             ieee->current_network.rates[2] =
2637                  RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_5MB;
2638             ieee->current_network.rates[3] =
2639                  RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_11MB;
2640 
2641         } else
2642             ieee->current_network.rates_len = 0;
2643 
2644         if (ieee->modulation & RTLLIB_OFDM_MODULATION) {
2645             ieee->current_network.rates_ex_len = 8;
2646 
2647             ieee->current_network.rates_ex[0] =
2648                          RTLLIB_OFDM_RATE_6MB;
2649             ieee->current_network.rates_ex[1] =
2650                          RTLLIB_OFDM_RATE_9MB;
2651             ieee->current_network.rates_ex[2] =
2652                          RTLLIB_OFDM_RATE_12MB;
2653             ieee->current_network.rates_ex[3] =
2654                          RTLLIB_OFDM_RATE_18MB;
2655             ieee->current_network.rates_ex[4] =
2656                          RTLLIB_OFDM_RATE_24MB;
2657             ieee->current_network.rates_ex[5] =
2658                          RTLLIB_OFDM_RATE_36MB;
2659             ieee->current_network.rates_ex[6] =
2660                          RTLLIB_OFDM_RATE_48MB;
2661             ieee->current_network.rates_ex[7] =
2662                          RTLLIB_OFDM_RATE_54MB;
2663 
2664             ieee->rate = 108;
2665         } else {
2666             ieee->current_network.rates_ex_len = 0;
2667             ieee->rate = 22;
2668         }
2669 
2670         ieee->current_network.qos_data.supported = 0;
2671         ieee->SetWirelessMode(ieee->dev, IEEE_G);
2672         ieee->current_network.mode = ieee->mode;
2673         ieee->current_network.atim_window = 0;
2674         ieee->current_network.capability = WLAN_CAPABILITY_IBSS;
2675     }
2676 
2677     netdev_info(ieee->dev, "%s(): ieee->mode = %d\n", __func__, ieee->mode);
2678     if ((ieee->mode == IEEE_N_24G) || (ieee->mode == IEEE_N_5G))
2679         HTUseDefaultSetting(ieee);
2680     else
2681         ieee->pHTInfo->bCurrentHTSupport = false;
2682 
2683     ieee->SetHwRegHandler(ieee->dev, HW_VAR_MEDIA_STATUS,
2684                   (u8 *)(&ieee->state));
2685 
2686     ieee->state = RTLLIB_LINKED;
2687     ieee->link_change(ieee->dev);
2688 
2689     HTSetConnectBwMode(ieee, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
2690     if (ieee->LedControlHandler != NULL)
2691         ieee->LedControlHandler(ieee->dev, LED_CTL_LINK);
2692 
2693     rtllib_start_send_beacons(ieee);
2694 
2695     notify_wx_assoc_event(ieee);
2696 
2697     if (ieee->data_hard_resume)
2698         ieee->data_hard_resume(ieee->dev);
2699 
2700     netif_carrier_on(ieee->dev);
2701 
2702     mutex_unlock(&ieee->wx_mutex);
2703 }
2704 
2705 inline void rtllib_start_ibss(struct rtllib_device *ieee)
2706 {
2707     schedule_delayed_work(&ieee->start_ibss_wq, msecs_to_jiffies(150));
2708 }
2709 
2710 /* this is called only in user context, with wx_mutex held */
2711 static void rtllib_start_bss(struct rtllib_device *ieee)
2712 {
2713     unsigned long flags;
2714 
2715     if (IS_DOT11D_ENABLE(ieee) && !IS_COUNTRY_IE_VALID(ieee)) {
2716         if (!ieee->global_domain)
2717             return;
2718     }
2719     /* check if we have already found the net we
2720      * are interested in (if any).
2721      * if not (we are disassociated and we are not
2722      * in associating / authenticating phase) start the background scanning.
2723      */
2724     rtllib_softmac_check_all_nets(ieee);
2725 
2726     /* ensure no-one start an associating process (thus setting
2727      * the ieee->state to rtllib_ASSOCIATING) while we
2728      * have just checked it and we are going to enable scan.
2729      * The rtllib_new_net function is always called with
2730      * lock held (from both rtllib_softmac_check_all_nets and
2731      * the rx path), so we cannot be in the middle of such function
2732      */
2733     spin_lock_irqsave(&ieee->lock, flags);
2734 
2735     if (ieee->state == RTLLIB_NOLINK)
2736         rtllib_start_scan(ieee);
2737     spin_unlock_irqrestore(&ieee->lock, flags);
2738 }
2739 
2740 static void rtllib_link_change_wq(void *data)
2741 {
2742     struct rtllib_device *ieee = container_of_dwork_rsl(data,
2743                      struct rtllib_device, link_change_wq);
2744     ieee->link_change(ieee->dev);
2745 }
2746 /* called only in userspace context */
2747 void rtllib_disassociate(struct rtllib_device *ieee)
2748 {
2749     netif_carrier_off(ieee->dev);
2750     if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)
2751         rtllib_reset_queue(ieee);
2752 
2753     if (ieee->data_hard_stop)
2754         ieee->data_hard_stop(ieee->dev);
2755     if (IS_DOT11D_ENABLE(ieee))
2756         dot11d_reset(ieee);
2757     ieee->state = RTLLIB_NOLINK;
2758     ieee->is_set_key = false;
2759     ieee->wap_set = 0;
2760 
2761     schedule_delayed_work(&ieee->link_change_wq, 0);
2762 
2763     notify_wx_assoc_event(ieee);
2764 }
2765 
2766 static void rtllib_associate_retry_wq(void *data)
2767 {
2768     struct rtllib_device *ieee = container_of_dwork_rsl(data,
2769                      struct rtllib_device, associate_retry_wq);
2770     unsigned long flags;
2771 
2772     mutex_lock(&ieee->wx_mutex);
2773     if (!ieee->proto_started)
2774         goto exit;
2775 
2776     if (ieee->state != RTLLIB_ASSOCIATING_RETRY)
2777         goto exit;
2778 
2779     /* until we do not set the state to RTLLIB_NOLINK
2780      * there are no possibility to have someone else trying
2781      * to start an association procedure (we get here with
2782      * ieee->state = RTLLIB_ASSOCIATING).
2783      * When we set the state to RTLLIB_NOLINK it is possible
2784      * that the RX path run an attempt to associate, but
2785      * both rtllib_softmac_check_all_nets and the
2786      * RX path works with ieee->lock held so there are no
2787      * problems. If we are still disassociated then start a scan.
2788      * the lock here is necessary to ensure no one try to start
2789      * an association procedure when we have just checked the
2790      * state and we are going to start the scan.
2791      */
2792     ieee->beinretry = true;
2793     ieee->state = RTLLIB_NOLINK;
2794 
2795     rtllib_softmac_check_all_nets(ieee);
2796 
2797     spin_lock_irqsave(&ieee->lock, flags);
2798 
2799     if (ieee->state == RTLLIB_NOLINK)
2800         rtllib_start_scan(ieee);
2801     spin_unlock_irqrestore(&ieee->lock, flags);
2802 
2803     ieee->beinretry = false;
2804 exit:
2805     mutex_unlock(&ieee->wx_mutex);
2806 }
2807 
2808 static struct sk_buff *rtllib_get_beacon_(struct rtllib_device *ieee)
2809 {
2810     static const u8 broadcast_addr[] = {
2811         0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2812     };
2813     struct sk_buff *skb;
2814     struct rtllib_probe_response *b;
2815 
2816     skb = rtllib_probe_resp(ieee, broadcast_addr);
2817 
2818     if (!skb)
2819         return NULL;
2820 
2821     b = (struct rtllib_probe_response *)skb->data;
2822     b->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_BEACON);
2823 
2824     return skb;
2825 
2826 }
2827 
2828 struct sk_buff *rtllib_get_beacon(struct rtllib_device *ieee)
2829 {
2830     struct sk_buff *skb;
2831     struct rtllib_probe_response *b;
2832 
2833     skb = rtllib_get_beacon_(ieee);
2834     if (!skb)
2835         return NULL;
2836 
2837     b = (struct rtllib_probe_response *)skb->data;
2838     b->header.seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2839 
2840     if (ieee->seq_ctrl[0] == 0xFFF)
2841         ieee->seq_ctrl[0] = 0;
2842     else
2843         ieee->seq_ctrl[0]++;
2844 
2845     return skb;
2846 }
2847 EXPORT_SYMBOL(rtllib_get_beacon);
2848 
2849 void rtllib_softmac_stop_protocol(struct rtllib_device *ieee, u8 mesh_flag,
2850                   u8 shutdown)
2851 {
2852     rtllib_stop_scan_syncro(ieee);
2853     mutex_lock(&ieee->wx_mutex);
2854     rtllib_stop_protocol(ieee, shutdown);
2855     mutex_unlock(&ieee->wx_mutex);
2856 }
2857 EXPORT_SYMBOL(rtllib_softmac_stop_protocol);
2858 
2859 
2860 void rtllib_stop_protocol(struct rtllib_device *ieee, u8 shutdown)
2861 {
2862     if (!ieee->proto_started)
2863         return;
2864 
2865     if (shutdown) {
2866         ieee->proto_started = 0;
2867         ieee->proto_stoppping = 1;
2868         if (ieee->rtllib_ips_leave != NULL)
2869             ieee->rtllib_ips_leave(ieee->dev);
2870     }
2871 
2872     rtllib_stop_send_beacons(ieee);
2873     del_timer_sync(&ieee->associate_timer);
2874     cancel_delayed_work_sync(&ieee->associate_retry_wq);
2875     cancel_delayed_work_sync(&ieee->start_ibss_wq);
2876     cancel_delayed_work_sync(&ieee->link_change_wq);
2877     rtllib_stop_scan(ieee);
2878 
2879     if (ieee->state <= RTLLIB_ASSOCIATING_AUTHENTICATED)
2880         ieee->state = RTLLIB_NOLINK;
2881 
2882     if (ieee->state == RTLLIB_LINKED) {
2883         if (ieee->iw_mode == IW_MODE_INFRA)
2884             SendDisassociation(ieee, 1, WLAN_REASON_DEAUTH_LEAVING);
2885         rtllib_disassociate(ieee);
2886     }
2887 
2888     if (shutdown) {
2889         RemoveAllTS(ieee);
2890         ieee->proto_stoppping = 0;
2891     }
2892     kfree(ieee->assocreq_ies);
2893     ieee->assocreq_ies = NULL;
2894     ieee->assocreq_ies_len = 0;
2895     kfree(ieee->assocresp_ies);
2896     ieee->assocresp_ies = NULL;
2897     ieee->assocresp_ies_len = 0;
2898 }
2899 
2900 void rtllib_softmac_start_protocol(struct rtllib_device *ieee, u8 mesh_flag)
2901 {
2902     mutex_lock(&ieee->wx_mutex);
2903     rtllib_start_protocol(ieee);
2904     mutex_unlock(&ieee->wx_mutex);
2905 }
2906 EXPORT_SYMBOL(rtllib_softmac_start_protocol);
2907 
2908 void rtllib_start_protocol(struct rtllib_device *ieee)
2909 {
2910     short ch = 0;
2911     int i = 0;
2912 
2913     rtllib_update_active_chan_map(ieee);
2914 
2915     if (ieee->proto_started)
2916         return;
2917 
2918     ieee->proto_started = 1;
2919 
2920     if (ieee->current_network.channel == 0) {
2921         do {
2922             ch++;
2923             if (ch > MAX_CHANNEL_NUMBER)
2924                 return; /* no channel found */
2925         } while (!ieee->active_channel_map[ch]);
2926         ieee->current_network.channel = ch;
2927     }
2928 
2929     if (ieee->current_network.beacon_interval == 0)
2930         ieee->current_network.beacon_interval = 100;
2931 
2932     for (i = 0; i < 17; i++) {
2933         ieee->last_rxseq_num[i] = -1;
2934         ieee->last_rxfrag_num[i] = -1;
2935         ieee->last_packet_time[i] = 0;
2936     }
2937 
2938     if (ieee->UpdateBeaconInterruptHandler)
2939         ieee->UpdateBeaconInterruptHandler(ieee->dev, false);
2940 
2941     ieee->wmm_acm = 0;
2942     /* if the user set the MAC of the ad-hoc cell and then
2943      * switch to managed mode, shall we  make sure that association
2944      * attempts does not fail just because the user provide the essid
2945      * and the nic is still checking for the AP MAC ??
2946      */
2947     if (ieee->iw_mode == IW_MODE_INFRA) {
2948         rtllib_start_bss(ieee);
2949     } else if (ieee->iw_mode == IW_MODE_ADHOC) {
2950         if (ieee->UpdateBeaconInterruptHandler)
2951             ieee->UpdateBeaconInterruptHandler(ieee->dev, true);
2952 
2953         rtllib_start_ibss(ieee);
2954 
2955     } else if (ieee->iw_mode == IW_MODE_MASTER) {
2956         rtllib_start_master_bss(ieee);
2957     } else if (ieee->iw_mode == IW_MODE_MONITOR) {
2958         rtllib_start_monitor_mode(ieee);
2959     }
2960 }
2961 
2962 int rtllib_softmac_init(struct rtllib_device *ieee)
2963 {
2964     int i;
2965 
2966     memset(&ieee->current_network, 0, sizeof(struct rtllib_network));
2967 
2968     ieee->state = RTLLIB_NOLINK;
2969     for (i = 0; i < 5; i++)
2970         ieee->seq_ctrl[i] = 0;
2971     ieee->dot11d_info = kzalloc(sizeof(struct rt_dot11d_info), GFP_ATOMIC);
2972     if (!ieee->dot11d_info)
2973         return -ENOMEM;
2974 
2975     ieee->LinkDetectInfo.SlotIndex = 0;
2976     ieee->LinkDetectInfo.SlotNum = 2;
2977     ieee->LinkDetectInfo.NumRecvBcnInPeriod = 0;
2978     ieee->LinkDetectInfo.NumRecvDataInPeriod = 0;
2979     ieee->LinkDetectInfo.NumTxOkInPeriod = 0;
2980     ieee->LinkDetectInfo.NumRxOkInPeriod = 0;
2981     ieee->LinkDetectInfo.NumRxUnicastOkInPeriod = 0;
2982     ieee->bIsAggregateFrame = false;
2983     ieee->assoc_id = 0;
2984     ieee->queue_stop = 0;
2985     ieee->scanning_continue = 0;
2986     ieee->softmac_features = 0;
2987     ieee->wap_set = 0;
2988     ieee->ssid_set = 0;
2989     ieee->proto_started = 0;
2990     ieee->proto_stoppping = 0;
2991     ieee->basic_rate = RTLLIB_DEFAULT_BASIC_RATE;
2992     ieee->rate = 22;
2993     ieee->ps = RTLLIB_PS_DISABLED;
2994     ieee->sta_sleep = LPS_IS_WAKE;
2995 
2996     ieee->Regdot11HTOperationalRateSet[0] = 0xff;
2997     ieee->Regdot11HTOperationalRateSet[1] = 0xff;
2998     ieee->Regdot11HTOperationalRateSet[4] = 0x01;
2999 
3000     ieee->Regdot11TxHTOperationalRateSet[0] = 0xff;
3001     ieee->Regdot11TxHTOperationalRateSet[1] = 0xff;
3002     ieee->Regdot11TxHTOperationalRateSet[4] = 0x01;
3003 
3004     ieee->FirstIe_InScan = false;
3005     ieee->actscanning = false;
3006     ieee->beinretry = false;
3007     ieee->is_set_key = false;
3008     init_mgmt_queue(ieee);
3009 
3010     ieee->tx_pending.txb = NULL;
3011 
3012     timer_setup(&ieee->associate_timer, rtllib_associate_abort_cb, 0);
3013 
3014     timer_setup(&ieee->beacon_timer, rtllib_send_beacon_cb, 0);
3015 
3016     INIT_DELAYED_WORK_RSL(&ieee->link_change_wq,
3017                   (void *)rtllib_link_change_wq, ieee);
3018     INIT_DELAYED_WORK_RSL(&ieee->start_ibss_wq,
3019                   (void *)rtllib_start_ibss_wq, ieee);
3020     INIT_WORK_RSL(&ieee->associate_complete_wq,
3021               (void *)rtllib_associate_complete_wq, ieee);
3022     INIT_DELAYED_WORK_RSL(&ieee->associate_procedure_wq,
3023                   (void *)rtllib_associate_procedure_wq, ieee);
3024     INIT_DELAYED_WORK_RSL(&ieee->softmac_scan_wq,
3025                   (void *)rtllib_softmac_scan_wq, ieee);
3026     INIT_DELAYED_WORK_RSL(&ieee->associate_retry_wq,
3027                   (void *)rtllib_associate_retry_wq, ieee);
3028     INIT_WORK_RSL(&ieee->wx_sync_scan_wq, (void *)rtllib_wx_sync_scan_wq,
3029               ieee);
3030 
3031     mutex_init(&ieee->wx_mutex);
3032     mutex_init(&ieee->scan_mutex);
3033     mutex_init(&ieee->ips_mutex);
3034 
3035     spin_lock_init(&ieee->mgmt_tx_lock);
3036     spin_lock_init(&ieee->beacon_lock);
3037 
3038     INIT_WORK(&ieee->ps_task, rtllib_sta_ps);
3039 
3040     return 0;
3041 }
3042 
3043 void rtllib_softmac_free(struct rtllib_device *ieee)
3044 {
3045     mutex_lock(&ieee->wx_mutex);
3046     kfree(ieee->dot11d_info);
3047     ieee->dot11d_info = NULL;
3048     del_timer_sync(&ieee->associate_timer);
3049 
3050     cancel_delayed_work_sync(&ieee->associate_retry_wq);
3051     cancel_delayed_work_sync(&ieee->associate_procedure_wq);
3052     cancel_delayed_work_sync(&ieee->softmac_scan_wq);
3053     cancel_delayed_work_sync(&ieee->start_ibss_wq);
3054     cancel_delayed_work_sync(&ieee->hw_wakeup_wq);
3055     cancel_delayed_work_sync(&ieee->hw_sleep_wq);
3056     cancel_delayed_work_sync(&ieee->link_change_wq);
3057     cancel_work_sync(&ieee->associate_complete_wq);
3058     cancel_work_sync(&ieee->ips_leave_wq);
3059     cancel_work_sync(&ieee->wx_sync_scan_wq);
3060     cancel_work_sync(&ieee->ps_task);
3061     mutex_unlock(&ieee->wx_mutex);
3062 }
3063 
3064 static inline struct sk_buff *
3065 rtllib_disauth_skb(struct rtllib_network *beacon,
3066            struct rtllib_device *ieee, u16 asRsn)
3067 {
3068     struct sk_buff *skb;
3069     struct rtllib_disauth *disauth;
3070     int len = sizeof(struct rtllib_disauth) + ieee->tx_headroom;
3071 
3072     skb = dev_alloc_skb(len);
3073     if (!skb)
3074         return NULL;
3075 
3076     skb_reserve(skb, ieee->tx_headroom);
3077 
3078     disauth = skb_put(skb, sizeof(struct rtllib_disauth));
3079     disauth->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_DEAUTH);
3080     disauth->header.duration_id = 0;
3081 
3082     ether_addr_copy(disauth->header.addr1, beacon->bssid);
3083     ether_addr_copy(disauth->header.addr2, ieee->dev->dev_addr);
3084     ether_addr_copy(disauth->header.addr3, beacon->bssid);
3085 
3086     disauth->reason = cpu_to_le16(asRsn);
3087     return skb;
3088 }
3089 
3090 static inline struct sk_buff *
3091 rtllib_disassociate_skb(struct rtllib_network *beacon,
3092             struct rtllib_device *ieee, u16 asRsn)
3093 {
3094     struct sk_buff *skb;
3095     struct rtllib_disassoc *disass;
3096     int len = sizeof(struct rtllib_disassoc) + ieee->tx_headroom;
3097 
3098     skb = dev_alloc_skb(len);
3099 
3100     if (!skb)
3101         return NULL;
3102 
3103     skb_reserve(skb, ieee->tx_headroom);
3104 
3105     disass = skb_put(skb, sizeof(struct rtllib_disassoc));
3106     disass->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_DISASSOC);
3107     disass->header.duration_id = 0;
3108 
3109     ether_addr_copy(disass->header.addr1, beacon->bssid);
3110     ether_addr_copy(disass->header.addr2, ieee->dev->dev_addr);
3111     ether_addr_copy(disass->header.addr3, beacon->bssid);
3112 
3113     disass->reason = cpu_to_le16(asRsn);
3114     return skb;
3115 }
3116 
3117 void SendDisassociation(struct rtllib_device *ieee, bool deauth, u16 asRsn)
3118 {
3119     struct rtllib_network *beacon = &ieee->current_network;
3120     struct sk_buff *skb;
3121 
3122     if (deauth)
3123         skb = rtllib_disauth_skb(beacon, ieee, asRsn);
3124     else
3125         skb = rtllib_disassociate_skb(beacon, ieee, asRsn);
3126 
3127     if (skb)
3128         softmac_mgmt_xmit(skb, ieee);
3129 }
3130 
3131 u8 rtllib_ap_sec_type(struct rtllib_device *ieee)
3132 {
3133     static u8 ccmp_ie[4] = {0x00, 0x50, 0xf2, 0x04};
3134     static u8 ccmp_rsn_ie[4] = {0x00, 0x0f, 0xac, 0x04};
3135     int wpa_ie_len = ieee->wpa_ie_len;
3136     struct lib80211_crypt_data *crypt;
3137     int encrypt;
3138 
3139     crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
3140     encrypt = (ieee->current_network.capability & WLAN_CAPABILITY_PRIVACY)
3141           || (ieee->host_encrypt && crypt && crypt->ops &&
3142           (strcmp(crypt->ops->name, "R-WEP") == 0));
3143 
3144     /* simply judge  */
3145     if (encrypt && (wpa_ie_len == 0)) {
3146         return SEC_ALG_WEP;
3147     } else if ((wpa_ie_len != 0)) {
3148         if (((ieee->wpa_ie[0] == 0xdd) &&
3149             (!memcmp(&(ieee->wpa_ie[14]), ccmp_ie, 4))) ||
3150             ((ieee->wpa_ie[0] == 0x30) &&
3151             (!memcmp(&ieee->wpa_ie[10], ccmp_rsn_ie, 4))))
3152             return SEC_ALG_CCMP;
3153         else
3154             return SEC_ALG_TKIP;
3155     } else {
3156         return SEC_ALG_NONE;
3157     }
3158 }
3159 
3160 static void rtllib_MgntDisconnectIBSS(struct rtllib_device *rtllib)
3161 {
3162     u8  OpMode;
3163     u8  i;
3164     bool    bFilterOutNonAssociatedBSSID = false;
3165 
3166     rtllib->state = RTLLIB_NOLINK;
3167 
3168     for (i = 0; i < 6; i++)
3169         rtllib->current_network.bssid[i] = 0x55;
3170 
3171     rtllib->OpMode = RT_OP_MODE_NO_LINK;
3172     rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_BSSID,
3173                 rtllib->current_network.bssid);
3174     OpMode = RT_OP_MODE_NO_LINK;
3175     rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_MEDIA_STATUS, &OpMode);
3176     rtllib_stop_send_beacons(rtllib);
3177 
3178     bFilterOutNonAssociatedBSSID = false;
3179     rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_CECHK_BSSID,
3180                 (u8 *)(&bFilterOutNonAssociatedBSSID));
3181     notify_wx_assoc_event(rtllib);
3182 
3183 }
3184 
3185 static void rtllib_MlmeDisassociateRequest(struct rtllib_device *rtllib,
3186                        u8 *asSta, u8 asRsn)
3187 {
3188     u8 i;
3189     u8  OpMode;
3190 
3191     RemovePeerTS(rtllib, asSta);
3192 
3193     if (memcmp(rtllib->current_network.bssid, asSta, 6) == 0) {
3194         rtllib->state = RTLLIB_NOLINK;
3195 
3196         for (i = 0; i < 6; i++)
3197             rtllib->current_network.bssid[i] = 0x22;
3198         OpMode = RT_OP_MODE_NO_LINK;
3199         rtllib->OpMode = RT_OP_MODE_NO_LINK;
3200         rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_MEDIA_STATUS,
3201                     (u8 *)(&OpMode));
3202         rtllib_disassociate(rtllib);
3203 
3204         rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_BSSID,
3205                     rtllib->current_network.bssid);
3206 
3207     }
3208 
3209 }
3210 
3211 static void
3212 rtllib_MgntDisconnectAP(
3213     struct rtllib_device *rtllib,
3214     u8 asRsn
3215 )
3216 {
3217     bool bFilterOutNonAssociatedBSSID = false;
3218 
3219     bFilterOutNonAssociatedBSSID = false;
3220     rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_CECHK_BSSID,
3221                 (u8 *)(&bFilterOutNonAssociatedBSSID));
3222     rtllib_MlmeDisassociateRequest(rtllib, rtllib->current_network.bssid,
3223                        asRsn);
3224 
3225     rtllib->state = RTLLIB_NOLINK;
3226 }
3227 
3228 bool rtllib_MgntDisconnect(struct rtllib_device *rtllib, u8 asRsn)
3229 {
3230     if (rtllib->ps != RTLLIB_PS_DISABLED)
3231         rtllib->sta_wake_up(rtllib->dev);
3232 
3233     if (rtllib->state == RTLLIB_LINKED) {
3234         if (rtllib->iw_mode == IW_MODE_ADHOC)
3235             rtllib_MgntDisconnectIBSS(rtllib);
3236         if (rtllib->iw_mode == IW_MODE_INFRA)
3237             rtllib_MgntDisconnectAP(rtllib, asRsn);
3238 
3239     }
3240 
3241     return true;
3242 }
3243 EXPORT_SYMBOL(rtllib_MgntDisconnect);
3244 
3245 void notify_wx_assoc_event(struct rtllib_device *ieee)
3246 {
3247     union iwreq_data wrqu;
3248 
3249     if (ieee->cannot_notify)
3250         return;
3251 
3252     wrqu.ap_addr.sa_family = ARPHRD_ETHER;
3253     if (ieee->state == RTLLIB_LINKED)
3254         memcpy(wrqu.ap_addr.sa_data, ieee->current_network.bssid,
3255                ETH_ALEN);
3256     else {
3257 
3258         netdev_info(ieee->dev, "%s(): Tell user space disconnected\n",
3259                 __func__);
3260         eth_zero_addr(wrqu.ap_addr.sa_data);
3261     }
3262     wireless_send_event(ieee->dev, SIOCGIWAP, &wrqu, NULL);
3263 }
3264 EXPORT_SYMBOL(notify_wx_assoc_event);