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