Root/drivers/staging/rtl8192e/rtllib_softmac.c

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

Archive Download this file



interactive