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