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