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