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