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