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