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