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