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