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