HID: bpf: use __bpf_kfunc instead of noinline
[sfrench/cifs-2.6.git] / drivers / staging / rtl8192e / rtllib_softmac.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* IEEE 802.11 SoftMAC layer
3  * Copyright (c) 2005 Andrea Merello <andrea.merello@gmail.com>
4  *
5  * Mostly extracted from the rtl8180-sa2400 driver for the
6  * in-kernel generic ieee802.11 stack.
7  *
8  * Few lines might be stolen from other part of the rtllib
9  * stack. Copyright who own it's copyright
10  *
11  * WPA code stolen from the ipw2200 driver.
12  * Copyright who own it's copyright.
13  */
14 #include "rtllib.h"
15
16 #include <linux/random.h>
17 #include <linux/delay.h>
18 #include <linux/uaccess.h>
19 #include <linux/etherdevice.h>
20 #include <linux/ieee80211.h>
21 #include "dot11d.h"
22
23 static void rtllib_sta_wakeup(struct rtllib_device *ieee, short nl);
24
25 static short rtllib_is_54g(struct rtllib_network *net)
26 {
27         return (net->rates_ex_len > 0) || (net->rates_len > 4);
28 }
29
30 /* returns the total length needed for placing the RATE MFIE
31  * tag and the EXTENDED RATE MFIE tag if needed.
32  * It encludes two bytes per tag for the tag itself and its len
33  */
34 static unsigned int rtllib_MFIE_rate_len(struct rtllib_device *ieee)
35 {
36         unsigned int rate_len = 0;
37
38         rate_len = RTLLIB_CCK_RATE_LEN + 2;
39         rate_len += RTLLIB_OFDM_RATE_LEN + 2;
40
41         return rate_len;
42 }
43
44 /* place the MFIE rate, tag to the memory (double) pointed.
45  * Then it updates the pointer so that
46  * it points after the new MFIE tag added.
47  */
48 static void rtllib_MFIE_Brate(struct rtllib_device *ieee, u8 **tag_p)
49 {
50         u8 *tag = *tag_p;
51
52         *tag++ = MFIE_TYPE_RATES;
53         *tag++ = 4;
54         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_1MB;
55         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_2MB;
56         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_5MB;
57         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_11MB;
58
59         /* We may add an option for custom rates that specific HW
60          * might support
61          */
62         *tag_p = tag;
63 }
64
65 static void rtllib_MFIE_Grate(struct rtllib_device *ieee, u8 **tag_p)
66 {
67         u8 *tag = *tag_p;
68
69         *tag++ = MFIE_TYPE_RATES_EX;
70         *tag++ = 8;
71         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_6MB;
72         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_9MB;
73         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_12MB;
74         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_18MB;
75         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_24MB;
76         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_36MB;
77         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_48MB;
78         *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_54MB;
79
80         /* We may add an option for custom rates that specific HW might
81          * support
82          */
83         *tag_p = tag;
84 }
85
86 static void rtllib_WMM_Info(struct rtllib_device *ieee, u8 **tag_p)
87 {
88         u8 *tag = *tag_p;
89
90         *tag++ = MFIE_TYPE_GENERIC;
91         *tag++ = 7;
92         *tag++ = 0x00;
93         *tag++ = 0x50;
94         *tag++ = 0xf2;
95         *tag++ = 0x02;
96         *tag++ = 0x00;
97         *tag++ = 0x01;
98         *tag++ = MAX_SP_Len;
99         *tag_p = tag;
100 }
101
102 static void rtllib_TURBO_Info(struct rtllib_device *ieee, u8 **tag_p)
103 {
104         u8 *tag = *tag_p;
105
106         *tag++ = MFIE_TYPE_GENERIC;
107         *tag++ = 7;
108         *tag++ = 0x00;
109         *tag++ = 0xe0;
110         *tag++ = 0x4c;
111         *tag++ = 0x01;
112         *tag++ = 0x02;
113         *tag++ = 0x11;
114         *tag++ = 0x00;
115
116         *tag_p = tag;
117         netdev_alert(ieee->dev, "This is enable turbo mode IE process\n");
118 }
119
120 static void enqueue_mgmt(struct rtllib_device *ieee, struct sk_buff *skb)
121 {
122         int nh;
123
124         nh = (ieee->mgmt_queue_head + 1) % MGMT_QUEUE_NUM;
125
126 /* if the queue is full but we have newer frames then
127  * just overwrites the oldest.
128  *
129  * if (nh == ieee->mgmt_queue_tail)
130  *              return -1;
131  */
132         ieee->mgmt_queue_head = nh;
133         ieee->mgmt_queue_ring[nh] = skb;
134 }
135
136 static void init_mgmt_queue(struct rtllib_device *ieee)
137 {
138         ieee->mgmt_queue_tail = ieee->mgmt_queue_head = 0;
139 }
140
141 u8 MgntQuery_TxRateExcludeCCKRates(struct rtllib_device *ieee)
142 {
143         u16     i;
144         u8      QueryRate = 0;
145         u8      BasicRate;
146
147         for (i = 0; i < ieee->current_network.rates_len; i++) {
148                 BasicRate = ieee->current_network.rates[i] & 0x7F;
149                 if (!rtllib_is_cck_rate(BasicRate)) {
150                         if (QueryRate == 0) {
151                                 QueryRate = BasicRate;
152                         } else {
153                                 if (BasicRate < QueryRate)
154                                         QueryRate = BasicRate;
155                         }
156                 }
157         }
158
159         if (QueryRate == 0) {
160                 QueryRate = 12;
161                 netdev_info(ieee->dev, "No BasicRate found!!\n");
162         }
163         return QueryRate;
164 }
165
166 static u8 MgntQuery_MgntFrameTxRate(struct rtllib_device *ieee)
167 {
168         struct rt_hi_throughput *ht_info = ieee->ht_info;
169         u8 rate;
170
171         if (ht_info->iot_action & HT_IOT_ACT_MGNT_USE_CCK_6M)
172                 rate = 0x0c;
173         else
174                 rate = ieee->basic_rate & 0x7f;
175
176         if (rate == 0) {
177                 if (ieee->mode == WIRELESS_MODE_N_24G && !ht_info->bCurSuppCCK)
178                         rate = 0x0c;
179                 else
180                         rate = 0x02;
181         }
182
183         return rate;
184 }
185
186 inline void softmac_mgmt_xmit(struct sk_buff *skb, struct rtllib_device *ieee)
187 {
188         unsigned long flags;
189         short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
190         struct ieee80211_hdr_3addr  *header =
191                 (struct ieee80211_hdr_3addr  *)skb->data;
192
193         struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 8);
194
195         spin_lock_irqsave(&ieee->lock, flags);
196
197         /* called with 2nd param 0, no mgmt lock required */
198         rtllib_sta_wakeup(ieee, 0);
199
200         if (ieee80211_is_beacon(header->frame_control))
201                 tcb_desc->queue_index = BEACON_QUEUE;
202         else
203                 tcb_desc->queue_index = MGNT_QUEUE;
204
205         if (ieee->disable_mgnt_queue)
206                 tcb_desc->queue_index = HIGH_QUEUE;
207
208         tcb_desc->data_rate = MgntQuery_MgntFrameTxRate(ieee);
209         tcb_desc->ratr_index = 7;
210         tcb_desc->tx_dis_rate_fallback = 1;
211         tcb_desc->tx_use_drv_assinged_rate = 1;
212         if (single) {
213                 if (ieee->queue_stop) {
214                         enqueue_mgmt(ieee, skb);
215                 } else {
216                         header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
217
218                         if (ieee->seq_ctrl[0] == 0xFFF)
219                                 ieee->seq_ctrl[0] = 0;
220                         else
221                                 ieee->seq_ctrl[0]++;
222
223                         /* avoid watchdog triggers */
224                         ieee->softmac_data_hard_start_xmit(skb, ieee->dev,
225                                                            ieee->basic_rate);
226                 }
227
228                 spin_unlock_irqrestore(&ieee->lock, flags);
229         } else {
230                 spin_unlock_irqrestore(&ieee->lock, flags);
231                 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags);
232
233                 header->seq_ctrl = 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                 /* check whether the managed packet queued greater than 5 */
241                 if (!ieee->check_nic_enough_desc(ieee->dev,
242                                                  tcb_desc->queue_index) ||
243                     skb_queue_len(&ieee->skb_waitQ[tcb_desc->queue_index]) ||
244                     ieee->queue_stop) {
245                         /* insert the skb packet to the management queue
246                          *
247                          * as for the completion function, it does not need
248                          * to check it any more.
249                          */
250                         netdev_info(ieee->dev,
251                                "%s():insert to waitqueue, queue_index:%d!\n",
252                                __func__, tcb_desc->queue_index);
253                         skb_queue_tail(&ieee->skb_waitQ[tcb_desc->queue_index],
254                                        skb);
255                 } else {
256                         ieee->softmac_hard_start_xmit(skb, ieee->dev);
257                 }
258                 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags);
259         }
260 }
261
262 static inline void
263 softmac_ps_mgmt_xmit(struct sk_buff *skb,
264                      struct rtllib_device *ieee)
265 {
266         short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
267         struct ieee80211_hdr_3addr  *header =
268                 (struct ieee80211_hdr_3addr  *)skb->data;
269         u16 fc, type, stype;
270         struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 8);
271
272         fc = le16_to_cpu(header->frame_control);
273         type = WLAN_FC_GET_TYPE(fc);
274         stype = WLAN_FC_GET_STYPE(fc);
275
276         if (stype != IEEE80211_STYPE_PSPOLL)
277                 tcb_desc->queue_index = MGNT_QUEUE;
278         else
279                 tcb_desc->queue_index = HIGH_QUEUE;
280
281         if (ieee->disable_mgnt_queue)
282                 tcb_desc->queue_index = HIGH_QUEUE;
283
284         tcb_desc->data_rate = MgntQuery_MgntFrameTxRate(ieee);
285         tcb_desc->ratr_index = 7;
286         tcb_desc->tx_dis_rate_fallback = 1;
287         tcb_desc->tx_use_drv_assinged_rate = 1;
288         if (single) {
289                 if (type != RTLLIB_FTYPE_CTL) {
290                         header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
291
292                         if (ieee->seq_ctrl[0] == 0xFFF)
293                                 ieee->seq_ctrl[0] = 0;
294                         else
295                                 ieee->seq_ctrl[0]++;
296                 }
297                 /* avoid watchdog triggers */
298                 ieee->softmac_data_hard_start_xmit(skb, ieee->dev,
299                                                    ieee->basic_rate);
300
301         } else {
302                 if (type != RTLLIB_FTYPE_CTL) {
303                         header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
304
305                         if (ieee->seq_ctrl[0] == 0xFFF)
306                                 ieee->seq_ctrl[0] = 0;
307                         else
308                                 ieee->seq_ctrl[0]++;
309                 }
310                 ieee->softmac_hard_start_xmit(skb, ieee->dev);
311         }
312 }
313
314 static inline struct sk_buff *rtllib_probe_req(struct rtllib_device *ieee)
315 {
316         unsigned int len, rate_len;
317         u8 *tag;
318         struct sk_buff *skb;
319         struct rtllib_probe_request *req;
320
321         len = ieee->current_network.ssid_len;
322
323         rate_len = rtllib_MFIE_rate_len(ieee);
324
325         skb = dev_alloc_skb(sizeof(struct rtllib_probe_request) +
326                             2 + len + rate_len + ieee->tx_headroom);
327
328         if (!skb)
329                 return NULL;
330
331         skb_reserve(skb, ieee->tx_headroom);
332
333         req = skb_put(skb, sizeof(struct rtllib_probe_request));
334         req->header.frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
335         req->header.duration_id = 0;
336
337         eth_broadcast_addr(req->header.addr1);
338         ether_addr_copy(req->header.addr2, ieee->dev->dev_addr);
339         eth_broadcast_addr(req->header.addr3);
340
341         tag = skb_put(skb, len + 2 + rate_len);
342
343         *tag++ = MFIE_TYPE_SSID;
344         *tag++ = len;
345         memcpy(tag, ieee->current_network.ssid, len);
346         tag += len;
347
348         rtllib_MFIE_Brate(ieee, &tag);
349         rtllib_MFIE_Grate(ieee, &tag);
350
351         return skb;
352 }
353
354 static struct sk_buff *rtllib_get_beacon_(struct rtllib_device *ieee);
355
356 static void rtllib_send_beacon(struct rtllib_device *ieee)
357 {
358         struct sk_buff *skb;
359
360         if (!ieee->ieee_up)
361                 return;
362         skb = rtllib_get_beacon_(ieee);
363
364         if (skb) {
365                 softmac_mgmt_xmit(skb, ieee);
366                 ieee->softmac_stats.tx_beacons++;
367         }
368
369         if (ieee->beacon_txing && ieee->ieee_up)
370                 mod_timer(&ieee->beacon_timer, jiffies +
371                           (msecs_to_jiffies(ieee->current_network.beacon_interval - 5)));
372 }
373
374 static void rtllib_send_beacon_cb(struct timer_list *t)
375 {
376         struct rtllib_device *ieee =
377                 from_timer(ieee, t, beacon_timer);
378         unsigned long flags;
379
380         spin_lock_irqsave(&ieee->beacon_lock, flags);
381         rtllib_send_beacon(ieee);
382         spin_unlock_irqrestore(&ieee->beacon_lock, flags);
383 }
384
385 /* Enables network monitor mode, all rx packets will be received. */
386 void rtllib_EnableNetMonitorMode(struct net_device *dev,
387                 bool bInitState)
388 {
389         struct rtllib_device *ieee = netdev_priv_rsl(dev);
390
391         netdev_info(dev, "========>Enter Monitor Mode\n");
392
393         ieee->AllowAllDestAddrHandler(dev, true, !bInitState);
394 }
395
396 /* Disables network monitor mode. Only packets destinated to
397  * us will be received.
398  */
399 void rtllib_DisableNetMonitorMode(struct net_device *dev,
400                 bool bInitState)
401 {
402         struct rtllib_device *ieee = netdev_priv_rsl(dev);
403
404         netdev_info(dev, "========>Exit Monitor Mode\n");
405
406         ieee->AllowAllDestAddrHandler(dev, false, !bInitState);
407 }
408
409 static void rtllib_send_probe(struct rtllib_device *ieee)
410 {
411         struct sk_buff *skb;
412
413         skb = rtllib_probe_req(ieee);
414         if (skb) {
415                 softmac_mgmt_xmit(skb, ieee);
416                 ieee->softmac_stats.tx_probe_rq++;
417         }
418 }
419
420 static void rtllib_send_probe_requests(struct rtllib_device *ieee)
421 {
422         if (ieee->softmac_features & IEEE_SOFTMAC_PROBERQ) {
423                 rtllib_send_probe(ieee);
424                 rtllib_send_probe(ieee);
425         }
426 }
427
428 static void rtllib_update_active_chan_map(struct rtllib_device *ieee)
429 {
430         memcpy(ieee->active_channel_map, GET_DOT11D_INFO(ieee)->channel_map,
431                MAX_CHANNEL_NUMBER + 1);
432 }
433
434 /* this performs syncro scan blocking the caller until all channels
435  * in the allowed channel map has been checked.
436  */
437 static void rtllib_softmac_scan_syncro(struct rtllib_device *ieee)
438 {
439         union iwreq_data wrqu;
440         short ch = 0;
441
442         rtllib_update_active_chan_map(ieee);
443
444         ieee->be_scan_inprogress = true;
445
446         mutex_lock(&ieee->scan_mutex);
447
448         while (1) {
449                 do {
450                         ch++;
451                         if (ch > MAX_CHANNEL_NUMBER)
452                                 goto out; /* scan completed */
453                 } while (!ieee->active_channel_map[ch]);
454
455                 /* this function can be called in two situations
456                  * 1- We have switched to ad-hoc mode and we are
457                  *    performing a complete syncro scan before conclude
458                  *    there are no interesting cell and to create a
459                  *    new one. In this case the link state is
460                  *    MAC80211_NOLINK until we found an interesting cell.
461                  *    If so the ieee8021_new_net, called by the RX path
462                  *    will set the state to MAC80211_LINKED, so we stop
463                  *    scanning
464                  * 2- We are linked and the root uses run iwlist scan.
465                  *    So we switch to MAC80211_LINKED_SCANNING to remember
466                  *    that we are still logically linked (not interested in
467                  *    new network events, despite for updating the net list,
468                  *    but we are temporarly 'unlinked' as the driver shall
469                  *    not filter RX frames and the channel is changing.
470                  * So the only situation in which are interested is to check
471                  * if the state become LINKED because of the #1 situation
472                  */
473
474                 if (ieee->link_state == MAC80211_LINKED)
475                         goto out;
476                 if (ieee->sync_scan_hurryup) {
477                         netdev_info(ieee->dev,
478                                     "============>sync_scan_hurryup out\n");
479                         goto out;
480                 }
481
482                 ieee->set_chan(ieee->dev, ch);
483                 if (ieee->active_channel_map[ch] == 1)
484                         rtllib_send_probe_requests(ieee);
485
486                 /* this prevent excessive time wait when we
487                  * need to wait for a syncro scan to end..
488                  */
489                 msleep_interruptible_rsl(RTLLIB_SOFTMAC_SCAN_TIME);
490         }
491 out:
492         ieee->actscanning = false;
493         ieee->sync_scan_hurryup = 0;
494
495         if (ieee->link_state >= MAC80211_LINKED) {
496                 if (IS_DOT11D_ENABLE(ieee))
497                         dot11d_scan_complete(ieee);
498         }
499         mutex_unlock(&ieee->scan_mutex);
500
501         ieee->be_scan_inprogress = false;
502
503         memset(&wrqu, 0, sizeof(wrqu));
504         wireless_send_event(ieee->dev, SIOCGIWSCAN, &wrqu, NULL);
505 }
506
507 static void rtllib_softmac_scan_wq(void *data)
508 {
509         struct rtllib_device *ieee = container_of_dwork_rsl(data,
510                                      struct rtllib_device, softmac_scan_wq);
511         u8 last_channel = ieee->current_network.channel;
512
513         rtllib_update_active_chan_map(ieee);
514
515         if (!ieee->ieee_up)
516                 return;
517         if (rtllib_act_scanning(ieee, true))
518                 return;
519
520         mutex_lock(&ieee->scan_mutex);
521
522         if (ieee->rf_power_state == rf_off) {
523                 netdev_info(ieee->dev,
524                             "======>%s():rf state is rf_off, return\n",
525                             __func__);
526                 goto out1;
527         }
528
529         do {
530                 ieee->current_network.channel =
531                         (ieee->current_network.channel + 1) %
532                         MAX_CHANNEL_NUMBER;
533                 if (ieee->scan_watch_dog++ > MAX_CHANNEL_NUMBER) {
534                         if (!ieee->active_channel_map[ieee->current_network.channel])
535                                 ieee->current_network.channel = 6;
536                         goto out; /* no good chans */
537                 }
538         } while (!ieee->active_channel_map[ieee->current_network.channel]);
539
540         if (ieee->scanning_continue == 0)
541                 goto out;
542
543         ieee->set_chan(ieee->dev, ieee->current_network.channel);
544
545         if (ieee->active_channel_map[ieee->current_network.channel] == 1)
546                 rtllib_send_probe_requests(ieee);
547
548         schedule_delayed_work(&ieee->softmac_scan_wq,
549                               msecs_to_jiffies(RTLLIB_SOFTMAC_SCAN_TIME));
550
551         mutex_unlock(&ieee->scan_mutex);
552         return;
553
554 out:
555         if (IS_DOT11D_ENABLE(ieee))
556                 dot11d_scan_complete(ieee);
557         ieee->current_network.channel = last_channel;
558
559 out1:
560         ieee->actscanning = false;
561         ieee->scan_watch_dog = 0;
562         ieee->scanning_continue = 0;
563         mutex_unlock(&ieee->scan_mutex);
564 }
565
566 static void rtllib_softmac_stop_scan(struct rtllib_device *ieee)
567 {
568         mutex_lock(&ieee->scan_mutex);
569         ieee->scan_watch_dog = 0;
570         if (ieee->scanning_continue == 1) {
571                 ieee->scanning_continue = 0;
572                 ieee->actscanning = false;
573                 mutex_unlock(&ieee->scan_mutex);
574                 cancel_delayed_work_sync(&ieee->softmac_scan_wq);
575         } else {
576                 mutex_unlock(&ieee->scan_mutex);
577         }
578 }
579
580 void rtllib_stop_scan(struct rtllib_device *ieee)
581 {
582         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
583                 rtllib_softmac_stop_scan(ieee);
584 }
585 EXPORT_SYMBOL(rtllib_stop_scan);
586
587 void rtllib_stop_scan_syncro(struct rtllib_device *ieee)
588 {
589         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
590                 ieee->sync_scan_hurryup = 1;
591 }
592 EXPORT_SYMBOL(rtllib_stop_scan_syncro);
593
594 bool rtllib_act_scanning(struct rtllib_device *ieee, bool sync_scan)
595 {
596         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
597                 if (sync_scan)
598                         return ieee->be_scan_inprogress;
599                 else
600                         return ieee->actscanning || ieee->be_scan_inprogress;
601         } else {
602                 return test_bit(STATUS_SCANNING, &ieee->status);
603         }
604 }
605 EXPORT_SYMBOL(rtllib_act_scanning);
606
607 /* called with ieee->lock held */
608 static void rtllib_start_scan(struct rtllib_device *ieee)
609 {
610         ieee->rtllib_ips_leave_wq(ieee->dev);
611
612         if (IS_DOT11D_ENABLE(ieee)) {
613                 if (IS_COUNTRY_IE_VALID(ieee))
614                         RESET_CIE_WATCHDOG(ieee);
615         }
616         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
617                 if (ieee->scanning_continue == 0) {
618                         ieee->actscanning = true;
619                         ieee->scanning_continue = 1;
620                         schedule_delayed_work(&ieee->softmac_scan_wq, 0);
621                 }
622         }
623 }
624
625 /* called with wx_mutex held */
626 void rtllib_start_scan_syncro(struct rtllib_device *ieee)
627 {
628         if (IS_DOT11D_ENABLE(ieee)) {
629                 if (IS_COUNTRY_IE_VALID(ieee))
630                         RESET_CIE_WATCHDOG(ieee);
631         }
632         ieee->sync_scan_hurryup = 0;
633         if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
634                 rtllib_softmac_scan_syncro(ieee);
635 }
636 EXPORT_SYMBOL(rtllib_start_scan_syncro);
637
638 static inline struct sk_buff *
639 rtllib_authentication_req(struct rtllib_network *beacon,
640                           struct rtllib_device *ieee,
641                           int challengelen, u8 *daddr)
642 {
643         struct sk_buff *skb;
644         struct rtllib_authentication *auth;
645         int  len;
646
647         len = sizeof(struct rtllib_authentication) + challengelen +
648                      ieee->tx_headroom + 4;
649         skb = dev_alloc_skb(len);
650
651         if (!skb)
652                 return NULL;
653
654         skb_reserve(skb, ieee->tx_headroom);
655
656         auth = skb_put(skb, sizeof(struct rtllib_authentication));
657
658         auth->header.frame_control = cpu_to_le16(IEEE80211_STYPE_AUTH);
659         if (challengelen)
660                 auth->header.frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
661
662         auth->header.duration_id = cpu_to_le16(0x013a);
663         ether_addr_copy(auth->header.addr1, beacon->bssid);
664         ether_addr_copy(auth->header.addr2, ieee->dev->dev_addr);
665         ether_addr_copy(auth->header.addr3, beacon->bssid);
666         if (ieee->auth_mode == 0)
667                 auth->algorithm = WLAN_AUTH_OPEN;
668         else if (ieee->auth_mode == 1)
669                 auth->algorithm = cpu_to_le16(WLAN_AUTH_SHARED_KEY);
670         else if (ieee->auth_mode == 2)
671                 auth->algorithm = WLAN_AUTH_OPEN;
672         auth->transaction = cpu_to_le16(ieee->associate_seq);
673         ieee->associate_seq++;
674
675         auth->status = cpu_to_le16(WLAN_STATUS_SUCCESS);
676
677         return skb;
678 }
679
680 static struct sk_buff *rtllib_probe_resp(struct rtllib_device *ieee,
681                                          const u8 *dest)
682 {
683         u8 *tag;
684         int beacon_size;
685         struct rtllib_probe_response *beacon_buf;
686         struct sk_buff *skb = NULL;
687         int encrypt;
688         int atim_len, erp_len;
689         struct lib80211_crypt_data *crypt;
690
691         char *ssid = ieee->current_network.ssid;
692         int ssid_len = ieee->current_network.ssid_len;
693         int rate_len = ieee->current_network.rates_len + 2;
694         int rate_ex_len = ieee->current_network.rates_ex_len;
695         int wpa_ie_len = ieee->wpa_ie_len;
696         u8 erpinfo_content = 0;
697
698         u8 *tmp_ht_cap_buf = NULL;
699         u8 tmp_ht_cap_len = 0;
700         u8 *tmp_ht_info_buf = NULL;
701         u8 tmp_ht_info_len = 0;
702         struct rt_hi_throughput *ht_info = ieee->ht_info;
703         u8 *tmp_generic_ie_buf = NULL;
704         u8 tmp_generic_ie_len = 0;
705
706         if (rate_ex_len > 0)
707                 rate_ex_len += 2;
708
709         if (ieee->current_network.capability & WLAN_CAPABILITY_IBSS)
710                 atim_len = 4;
711         else
712                 atim_len = 0;
713
714         if ((ieee->current_network.mode == WIRELESS_MODE_G) ||
715            (ieee->current_network.mode == WIRELESS_MODE_N_24G &&
716            ieee->ht_info->bCurSuppCCK)) {
717                 erp_len = 3;
718                 erpinfo_content = 0;
719                 if (ieee->current_network.buseprotection)
720                         erpinfo_content |= ERP_UseProtection;
721         } else {
722                 erp_len = 0;
723         }
724
725         crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
726         encrypt = crypt && crypt->ops &&
727                 ((strcmp(crypt->ops->name, "R-WEP") == 0 || wpa_ie_len));
728         if (ieee->ht_info->current_ht_support) {
729                 tmp_ht_cap_buf = (u8 *)&(ieee->ht_info->SelfHTCap);
730                 tmp_ht_cap_len = sizeof(ieee->ht_info->SelfHTCap);
731                 tmp_ht_info_buf = (u8 *)&(ieee->ht_info->SelfHTInfo);
732                 tmp_ht_info_len = sizeof(ieee->ht_info->SelfHTInfo);
733                 HTConstructCapabilityElement(ieee, tmp_ht_cap_buf,
734                                              &tmp_ht_cap_len, encrypt, false);
735                 HTConstructInfoElement(ieee, tmp_ht_info_buf, &tmp_ht_info_len,
736                                        encrypt);
737
738                 if (ht_info->reg_rt2rt_aggregation) {
739                         tmp_generic_ie_buf = ieee->ht_info->sz_rt2rt_agg_buf;
740                         tmp_generic_ie_len =
741                                  sizeof(ieee->ht_info->sz_rt2rt_agg_buf);
742                         HTConstructRT2RTAggElement(ieee, tmp_generic_ie_buf,
743                                                    &tmp_generic_ie_len);
744                 }
745         }
746
747         beacon_size = sizeof(struct rtllib_probe_response) + 2 +
748                 ssid_len + 3 + rate_len + rate_ex_len + atim_len + erp_len
749                 + wpa_ie_len + ieee->tx_headroom;
750         skb = dev_alloc_skb(beacon_size);
751         if (!skb)
752                 return NULL;
753
754         skb_reserve(skb, ieee->tx_headroom);
755
756         beacon_buf = skb_put(skb, (beacon_size - ieee->tx_headroom));
757         ether_addr_copy(beacon_buf->header.addr1, dest);
758         ether_addr_copy(beacon_buf->header.addr2, ieee->dev->dev_addr);
759         ether_addr_copy(beacon_buf->header.addr3, ieee->current_network.bssid);
760
761         beacon_buf->header.duration_id = 0;
762         beacon_buf->beacon_interval =
763                 cpu_to_le16(ieee->current_network.beacon_interval);
764         beacon_buf->capability =
765                 cpu_to_le16(ieee->current_network.capability &
766                 WLAN_CAPABILITY_IBSS);
767         beacon_buf->capability |=
768                 cpu_to_le16(ieee->current_network.capability &
769                 WLAN_CAPABILITY_SHORT_PREAMBLE);
770
771         if (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
772                 beacon_buf->capability |=
773                         cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
774
775         crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
776         if (encrypt)
777                 beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
778
779         beacon_buf->header.frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_RESP);
780         beacon_buf->info_element[0].id = MFIE_TYPE_SSID;
781         beacon_buf->info_element[0].len = ssid_len;
782
783         tag = (u8 *)beacon_buf->info_element[0].data;
784
785         memcpy(tag, ssid, ssid_len);
786
787         tag += ssid_len;
788
789         *(tag++) = MFIE_TYPE_RATES;
790         *(tag++) = rate_len - 2;
791         memcpy(tag, ieee->current_network.rates, rate_len - 2);
792         tag += rate_len - 2;
793
794         *(tag++) = MFIE_TYPE_DS_SET;
795         *(tag++) = 1;
796         *(tag++) = ieee->current_network.channel;
797
798         if (atim_len) {
799                 u16 val16;
800                 *(tag++) = MFIE_TYPE_IBSS_SET;
801                 *(tag++) = 2;
802                 val16 = ieee->current_network.atim_window;
803                 memcpy((u8 *)tag, (u8 *)&val16, 2);
804                 tag += 2;
805         }
806
807         if (erp_len) {
808                 *(tag++) = MFIE_TYPE_ERP;
809                 *(tag++) = 1;
810                 *(tag++) = erpinfo_content;
811         }
812         if (rate_ex_len) {
813                 *(tag++) = MFIE_TYPE_RATES_EX;
814                 *(tag++) = rate_ex_len - 2;
815                 memcpy(tag, ieee->current_network.rates_ex, rate_ex_len - 2);
816                 tag += rate_ex_len - 2;
817         }
818
819         if (wpa_ie_len) {
820                 memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
821                 tag += ieee->wpa_ie_len;
822         }
823         return skb;
824 }
825
826 static struct sk_buff *rtllib_null_func(struct rtllib_device *ieee, short pwr)
827 {
828         struct sk_buff *skb;
829         struct ieee80211_hdr_3addr *hdr;
830
831         skb = dev_alloc_skb(sizeof(struct ieee80211_hdr_3addr) + ieee->tx_headroom);
832         if (!skb)
833                 return NULL;
834
835         skb_reserve(skb, ieee->tx_headroom);
836
837         hdr = skb_put(skb, sizeof(struct ieee80211_hdr_3addr));
838
839         ether_addr_copy(hdr->addr1, ieee->current_network.bssid);
840         ether_addr_copy(hdr->addr2, ieee->dev->dev_addr);
841         ether_addr_copy(hdr->addr3, ieee->current_network.bssid);
842
843         hdr->frame_control = cpu_to_le16(RTLLIB_FTYPE_DATA |
844                 IEEE80211_STYPE_NULLFUNC | IEEE80211_FCTL_TODS |
845                 (pwr ? IEEE80211_FCTL_PM : 0));
846
847         return skb;
848 }
849
850 static struct sk_buff *rtllib_pspoll_func(struct rtllib_device *ieee)
851 {
852         struct sk_buff *skb;
853         struct ieee80211_pspoll *hdr;
854
855         skb = dev_alloc_skb(sizeof(struct ieee80211_pspoll) + ieee->tx_headroom);
856         if (!skb)
857                 return NULL;
858
859         skb_reserve(skb, ieee->tx_headroom);
860
861         hdr = skb_put(skb, sizeof(struct ieee80211_pspoll));
862
863         ether_addr_copy(hdr->bssid, ieee->current_network.bssid);
864         ether_addr_copy(hdr->ta, ieee->dev->dev_addr);
865
866         hdr->aid = cpu_to_le16(ieee->assoc_id | 0xc000);
867         hdr->frame_control = cpu_to_le16(RTLLIB_FTYPE_CTL | IEEE80211_STYPE_PSPOLL |
868                          IEEE80211_FCTL_PM);
869
870         return skb;
871 }
872
873 static inline int SecIsInPMKIDList(struct rtllib_device *ieee, u8 *bssid)
874 {
875         int i = 0;
876
877         do {
878                 if ((ieee->PMKIDList[i].bUsed) &&
879                    (memcmp(ieee->PMKIDList[i].Bssid, bssid, ETH_ALEN) == 0))
880                         break;
881                 i++;
882         } while (i < NUM_PMKID_CACHE);
883
884         if (i == NUM_PMKID_CACHE)
885                 i = -1;
886         return i;
887 }
888
889 static inline struct sk_buff *
890 rtllib_association_req(struct rtllib_network *beacon,
891                        struct rtllib_device *ieee)
892 {
893         struct sk_buff *skb;
894         struct rtllib_assoc_request_frame *hdr;
895         u8 *tag, *ies;
896         int i;
897         u8 *ht_cap_buf = NULL;
898         u8 ht_cap_len = 0;
899         u8 *realtek_ie_buf = NULL;
900         u8 realtek_ie_len = 0;
901         int wpa_ie_len = ieee->wpa_ie_len;
902         int wps_ie_len = ieee->wps_ie_len;
903         unsigned int ckip_ie_len = 0;
904         unsigned int ccxrm_ie_len = 0;
905         unsigned int cxvernum_ie_len = 0;
906         struct lib80211_crypt_data *crypt;
907         int encrypt;
908         int     PMKCacheIdx;
909
910         unsigned int rate_len = (beacon->rates_len ?
911                                 (beacon->rates_len + 2) : 0) +
912                                 (beacon->rates_ex_len ? (beacon->rates_ex_len) +
913                                 2 : 0);
914
915         unsigned int wmm_info_len = beacon->qos_data.supported ? 9 : 0;
916         unsigned int turbo_info_len = beacon->Turbo_Enable ? 9 : 0;
917
918         int len = 0;
919
920         crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
921         if (crypt)
922                 encrypt = crypt && crypt->ops &&
923                           ((strcmp(crypt->ops->name, "R-WEP") == 0 ||
924                           wpa_ie_len));
925         else
926                 encrypt = 0;
927
928         if ((ieee->rtllib_ap_sec_type &&
929             (ieee->rtllib_ap_sec_type(ieee) & SEC_ALG_TKIP)) ||
930             ieee->bForcedBgMode) {
931                 ieee->ht_info->enable_ht = 0;
932                 ieee->mode = WIRELESS_MODE_G;
933         }
934
935         if (ieee->ht_info->current_ht_support && ieee->ht_info->enable_ht) {
936                 ht_cap_buf = (u8 *)&(ieee->ht_info->SelfHTCap);
937                 ht_cap_len = sizeof(ieee->ht_info->SelfHTCap);
938                 HTConstructCapabilityElement(ieee, ht_cap_buf, &ht_cap_len,
939                                              encrypt, true);
940                 if (ieee->ht_info->current_rt2rt_aggregation) {
941                         realtek_ie_buf = ieee->ht_info->sz_rt2rt_agg_buf;
942                         realtek_ie_len =
943                                  sizeof(ieee->ht_info->sz_rt2rt_agg_buf);
944                         HTConstructRT2RTAggElement(ieee, realtek_ie_buf,
945                                                    &realtek_ie_len);
946                 }
947         }
948
949         if (beacon->bCkipSupported)
950                 ckip_ie_len = 30 + 2;
951         if (beacon->bCcxRmEnable)
952                 ccxrm_ie_len = 6 + 2;
953         if (beacon->BssCcxVerNumber >= 2)
954                 cxvernum_ie_len = 5 + 2;
955
956         PMKCacheIdx = SecIsInPMKIDList(ieee, ieee->current_network.bssid);
957         if (PMKCacheIdx >= 0) {
958                 wpa_ie_len += 18;
959                 netdev_info(ieee->dev, "[PMK cache]: WPA2 IE length: %x\n",
960                             wpa_ie_len);
961         }
962         len = sizeof(struct rtllib_assoc_request_frame) + 2
963                 + beacon->ssid_len
964                 + rate_len
965                 + wpa_ie_len
966                 + wps_ie_len
967                 + wmm_info_len
968                 + turbo_info_len
969                 + ht_cap_len
970                 + realtek_ie_len
971                 + ckip_ie_len
972                 + ccxrm_ie_len
973                 + cxvernum_ie_len
974                 + ieee->tx_headroom;
975
976         skb = dev_alloc_skb(len);
977
978         if (!skb)
979                 return NULL;
980
981         skb_reserve(skb, ieee->tx_headroom);
982
983         hdr = skb_put(skb, sizeof(struct rtllib_assoc_request_frame) + 2);
984
985         hdr->header.frame_control = cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ);
986         hdr->header.duration_id = cpu_to_le16(37);
987         ether_addr_copy(hdr->header.addr1, beacon->bssid);
988         ether_addr_copy(hdr->header.addr2, ieee->dev->dev_addr);
989         ether_addr_copy(hdr->header.addr3, beacon->bssid);
990
991         ether_addr_copy(ieee->ap_mac_addr, beacon->bssid);
992
993         hdr->capability = cpu_to_le16(WLAN_CAPABILITY_ESS);
994         if (beacon->capability & WLAN_CAPABILITY_PRIVACY)
995                 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
996
997         if (beacon->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
998                 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
999
1000         if (beacon->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1001                 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
1002
1003         hdr->listen_interval = cpu_to_le16(beacon->listen_interval);
1004
1005         hdr->info_element[0].id = MFIE_TYPE_SSID;
1006
1007         hdr->info_element[0].len = beacon->ssid_len;
1008         skb_put_data(skb, beacon->ssid, beacon->ssid_len);
1009
1010         tag = skb_put(skb, rate_len);
1011
1012         if (beacon->rates_len) {
1013                 *tag++ = MFIE_TYPE_RATES;
1014                 *tag++ = beacon->rates_len;
1015                 for (i = 0; i < beacon->rates_len; i++)
1016                         *tag++ = beacon->rates[i];
1017         }
1018
1019         if (beacon->rates_ex_len) {
1020                 *tag++ = MFIE_TYPE_RATES_EX;
1021                 *tag++ = beacon->rates_ex_len;
1022                 for (i = 0; i < beacon->rates_ex_len; i++)
1023                         *tag++ = beacon->rates_ex[i];
1024         }
1025
1026         if (beacon->bCkipSupported) {
1027                 static const u8 AironetIeOui[] = {0x00, 0x01, 0x66};
1028                 u8      CcxAironetBuf[30];
1029                 struct octet_string osCcxAironetIE;
1030
1031                 memset(CcxAironetBuf, 0, 30);
1032                 osCcxAironetIE.Octet = CcxAironetBuf;
1033                 osCcxAironetIE.Length = sizeof(CcxAironetBuf);
1034                 memcpy(osCcxAironetIE.Octet, AironetIeOui,
1035                        sizeof(AironetIeOui));
1036
1037                 osCcxAironetIE.Octet[IE_CISCO_FLAG_POSITION] |=
1038                                          (SUPPORT_CKIP_PK | SUPPORT_CKIP_MIC);
1039                 tag = skb_put(skb, ckip_ie_len);
1040                 *tag++ = MFIE_TYPE_AIRONET;
1041                 *tag++ = osCcxAironetIE.Length;
1042                 memcpy(tag, osCcxAironetIE.Octet, osCcxAironetIE.Length);
1043                 tag += osCcxAironetIE.Length;
1044         }
1045
1046         if (beacon->bCcxRmEnable) {
1047                 static const u8 CcxRmCapBuf[] = {0x00, 0x40, 0x96, 0x01, 0x01,
1048                         0x00};
1049                 struct octet_string osCcxRmCap;
1050
1051                 osCcxRmCap.Octet = (u8 *)CcxRmCapBuf;
1052                 osCcxRmCap.Length = sizeof(CcxRmCapBuf);
1053                 tag = skb_put(skb, ccxrm_ie_len);
1054                 *tag++ = MFIE_TYPE_GENERIC;
1055                 *tag++ = osCcxRmCap.Length;
1056                 memcpy(tag, osCcxRmCap.Octet, osCcxRmCap.Length);
1057                 tag += osCcxRmCap.Length;
1058         }
1059
1060         if (beacon->BssCcxVerNumber >= 2) {
1061                 u8 CcxVerNumBuf[] = {0x00, 0x40, 0x96, 0x03, 0x00};
1062                 struct octet_string osCcxVerNum;
1063
1064                 CcxVerNumBuf[4] = beacon->BssCcxVerNumber;
1065                 osCcxVerNum.Octet = CcxVerNumBuf;
1066                 osCcxVerNum.Length = sizeof(CcxVerNumBuf);
1067                 tag = skb_put(skb, cxvernum_ie_len);
1068                 *tag++ = MFIE_TYPE_GENERIC;
1069                 *tag++ = osCcxVerNum.Length;
1070                 memcpy(tag, osCcxVerNum.Octet, osCcxVerNum.Length);
1071                 tag += osCcxVerNum.Length;
1072         }
1073         if (ieee->ht_info->current_ht_support && ieee->ht_info->enable_ht) {
1074                 if (ieee->ht_info->ePeerHTSpecVer != HT_SPEC_VER_EWC) {
1075                         tag = skb_put(skb, ht_cap_len);
1076                         *tag++ = MFIE_TYPE_HT_CAP;
1077                         *tag++ = ht_cap_len - 2;
1078                         memcpy(tag, ht_cap_buf, ht_cap_len - 2);
1079                         tag += ht_cap_len - 2;
1080                 }
1081         }
1082
1083         if (wpa_ie_len) {
1084                 skb_put_data(skb, ieee->wpa_ie, ieee->wpa_ie_len);
1085
1086                 if (PMKCacheIdx >= 0) {
1087                         tag = skb_put(skb, 18);
1088                         *tag = 1;
1089                         *(tag + 1) = 0;
1090                         memcpy((tag + 2), &ieee->PMKIDList[PMKCacheIdx].PMKID,
1091                                16);
1092                 }
1093         }
1094         if (wmm_info_len) {
1095                 tag = skb_put(skb, wmm_info_len);
1096                 rtllib_WMM_Info(ieee, &tag);
1097         }
1098
1099         if (wps_ie_len && ieee->wps_ie)
1100                 skb_put_data(skb, ieee->wps_ie, wps_ie_len);
1101
1102         if (turbo_info_len) {
1103                 tag = skb_put(skb, turbo_info_len);
1104                 rtllib_TURBO_Info(ieee, &tag);
1105         }
1106
1107         if (ieee->ht_info->current_ht_support && ieee->ht_info->enable_ht) {
1108                 if (ieee->ht_info->ePeerHTSpecVer == HT_SPEC_VER_EWC) {
1109                         tag = skb_put(skb, ht_cap_len);
1110                         *tag++ = MFIE_TYPE_GENERIC;
1111                         *tag++ = ht_cap_len - 2;
1112                         memcpy(tag, ht_cap_buf, ht_cap_len - 2);
1113                         tag += ht_cap_len - 2;
1114                 }
1115
1116                 if (ieee->ht_info->current_rt2rt_aggregation) {
1117                         tag = skb_put(skb, realtek_ie_len);
1118                         *tag++ = MFIE_TYPE_GENERIC;
1119                         *tag++ = realtek_ie_len - 2;
1120                         memcpy(tag, realtek_ie_buf, realtek_ie_len - 2);
1121                 }
1122         }
1123
1124         kfree(ieee->assocreq_ies);
1125         ieee->assocreq_ies = NULL;
1126         ies = &(hdr->info_element[0].id);
1127         ieee->assocreq_ies_len = (skb->data + skb->len) - ies;
1128         ieee->assocreq_ies = kmemdup(ies, ieee->assocreq_ies_len, GFP_ATOMIC);
1129         if (!ieee->assocreq_ies)
1130                 ieee->assocreq_ies_len = 0;
1131
1132         return skb;
1133 }
1134
1135 static void rtllib_associate_abort(struct rtllib_device *ieee)
1136 {
1137         unsigned long flags;
1138
1139         spin_lock_irqsave(&ieee->lock, flags);
1140
1141         ieee->associate_seq++;
1142
1143         /* don't scan, and avoid to have the RX path possibily
1144          * try again to associate. Even do not react to AUTH or
1145          * ASSOC response. Just wait for the retry wq to be scheduled.
1146          * Here we will check if there are good nets to associate
1147          * with, so we retry or just get back to NO_LINK and scanning
1148          */
1149         if (ieee->link_state == RTLLIB_ASSOCIATING_AUTHENTICATING) {
1150                 netdev_dbg(ieee->dev, "Authentication failed\n");
1151                 ieee->softmac_stats.no_auth_rs++;
1152         } else {
1153                 netdev_dbg(ieee->dev, "Association failed\n");
1154                 ieee->softmac_stats.no_ass_rs++;
1155         }
1156
1157         ieee->link_state = RTLLIB_ASSOCIATING_RETRY;
1158
1159         schedule_delayed_work(&ieee->associate_retry_wq,
1160                               RTLLIB_SOFTMAC_ASSOC_RETRY_TIME);
1161
1162         spin_unlock_irqrestore(&ieee->lock, flags);
1163 }
1164
1165 static void rtllib_associate_abort_cb(struct timer_list *t)
1166 {
1167         struct rtllib_device *dev = from_timer(dev, t, associate_timer);
1168
1169         rtllib_associate_abort(dev);
1170 }
1171
1172 static void rtllib_associate_step1(struct rtllib_device *ieee, u8 *daddr)
1173 {
1174         struct rtllib_network *beacon = &ieee->current_network;
1175         struct sk_buff *skb;
1176
1177         netdev_dbg(ieee->dev, "Stopping scan\n");
1178
1179         ieee->softmac_stats.tx_auth_rq++;
1180
1181         skb = rtllib_authentication_req(beacon, ieee, 0, daddr);
1182
1183         if (!skb) {
1184                 rtllib_associate_abort(ieee);
1185         } else {
1186                 ieee->link_state = RTLLIB_ASSOCIATING_AUTHENTICATING;
1187                 netdev_dbg(ieee->dev, "Sending authentication request\n");
1188                 softmac_mgmt_xmit(skb, ieee);
1189                 if (!timer_pending(&ieee->associate_timer)) {
1190                         ieee->associate_timer.expires = jiffies + (HZ / 2);
1191                         add_timer(&ieee->associate_timer);
1192                 }
1193         }
1194 }
1195
1196 static void rtllib_auth_challenge(struct rtllib_device *ieee, u8 *challenge,
1197                                   int chlen)
1198 {
1199         u8 *c;
1200         struct sk_buff *skb;
1201         struct rtllib_network *beacon = &ieee->current_network;
1202
1203         ieee->associate_seq++;
1204         ieee->softmac_stats.tx_auth_rq++;
1205
1206         skb = rtllib_authentication_req(beacon, ieee, chlen + 2, beacon->bssid);
1207
1208         if (!skb) {
1209                 rtllib_associate_abort(ieee);
1210         } else {
1211                 c = skb_put(skb, chlen + 2);
1212                 *(c++) = MFIE_TYPE_CHALLENGE;
1213                 *(c++) = chlen;
1214                 memcpy(c, challenge, chlen);
1215
1216                 netdev_dbg(ieee->dev,
1217                            "Sending authentication challenge response\n");
1218
1219                 rtllib_encrypt_fragment(ieee, skb,
1220                                         sizeof(struct ieee80211_hdr_3addr));
1221
1222                 softmac_mgmt_xmit(skb, ieee);
1223                 mod_timer(&ieee->associate_timer, jiffies + (HZ / 2));
1224         }
1225         kfree(challenge);
1226 }
1227
1228 static void rtllib_associate_step2(struct rtllib_device *ieee)
1229 {
1230         struct sk_buff *skb;
1231         struct rtllib_network *beacon = &ieee->current_network;
1232
1233         del_timer_sync(&ieee->associate_timer);
1234
1235         netdev_dbg(ieee->dev, "Sending association request\n");
1236
1237         ieee->softmac_stats.tx_ass_rq++;
1238         skb = rtllib_association_req(beacon, ieee);
1239         if (!skb) {
1240                 rtllib_associate_abort(ieee);
1241         } else {
1242                 softmac_mgmt_xmit(skb, ieee);
1243                 mod_timer(&ieee->associate_timer, jiffies + (HZ / 2));
1244         }
1245 }
1246
1247 static void rtllib_associate_complete_wq(void *data)
1248 {
1249         struct rtllib_device *ieee = (struct rtllib_device *)
1250                                      container_of(data,
1251                                      struct rtllib_device,
1252                                      associate_complete_wq);
1253         struct rt_pwr_save_ctrl *psc = &ieee->pwr_save_ctrl;
1254
1255         netdev_info(ieee->dev, "Associated successfully with %pM\n",
1256                     ieee->current_network.bssid);
1257         netdev_info(ieee->dev, "normal associate\n");
1258         notify_wx_assoc_event(ieee);
1259
1260         netif_carrier_on(ieee->dev);
1261         ieee->is_roaming = false;
1262         if (rtllib_is_54g(&ieee->current_network)) {
1263                 ieee->rate = 108;
1264                 netdev_info(ieee->dev, "Using G rates:%d\n", ieee->rate);
1265         } else {
1266                 ieee->rate = 22;
1267                 ieee->set_wireless_mode(ieee->dev, WIRELESS_MODE_B);
1268                 netdev_info(ieee->dev, "Using B rates:%d\n", ieee->rate);
1269         }
1270         if (ieee->ht_info->current_ht_support && ieee->ht_info->enable_ht) {
1271                 netdev_info(ieee->dev, "Successfully associated, ht enabled\n");
1272                 HTOnAssocRsp(ieee);
1273         } else {
1274                 netdev_info(ieee->dev,
1275                             "Successfully associated, ht not enabled(%d, %d)\n",
1276                             ieee->ht_info->current_ht_support,
1277                             ieee->ht_info->enable_ht);
1278                 memset(ieee->dot11ht_oper_rate_set, 0, 16);
1279         }
1280         ieee->link_detect_info.SlotNum = 2 * (1 +
1281                                        ieee->current_network.beacon_interval /
1282                                        500);
1283         if (ieee->link_detect_info.NumRecvBcnInPeriod == 0 ||
1284             ieee->link_detect_info.NumRecvDataInPeriod == 0) {
1285                 ieee->link_detect_info.NumRecvBcnInPeriod = 1;
1286                 ieee->link_detect_info.NumRecvDataInPeriod = 1;
1287         }
1288         psc->LpsIdleCount = 0;
1289         ieee->link_change(ieee->dev);
1290
1291 }
1292
1293 static void rtllib_sta_send_associnfo(struct rtllib_device *ieee)
1294 {
1295 }
1296
1297 static void rtllib_associate_complete(struct rtllib_device *ieee)
1298 {
1299         del_timer_sync(&ieee->associate_timer);
1300
1301         ieee->link_state = MAC80211_LINKED;
1302         rtllib_sta_send_associnfo(ieee);
1303
1304         schedule_work(&ieee->associate_complete_wq);
1305 }
1306
1307 static void rtllib_associate_procedure_wq(void *data)
1308 {
1309         struct rtllib_device *ieee = container_of_dwork_rsl(data,
1310                                      struct rtllib_device,
1311                                      associate_procedure_wq);
1312         rtllib_stop_scan_syncro(ieee);
1313         ieee->rtllib_ips_leave(ieee->dev);
1314         mutex_lock(&ieee->wx_mutex);
1315
1316         rtllib_stop_scan(ieee);
1317         HTSetConnectBwMode(ieee, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
1318         if (ieee->rf_power_state == rf_off) {
1319                 ieee->rtllib_ips_leave_wq(ieee->dev);
1320                 mutex_unlock(&ieee->wx_mutex);
1321                 return;
1322         }
1323         ieee->associate_seq = 1;
1324
1325         rtllib_associate_step1(ieee, ieee->current_network.bssid);
1326
1327         mutex_unlock(&ieee->wx_mutex);
1328 }
1329
1330 inline void rtllib_softmac_new_net(struct rtllib_device *ieee,
1331                                    struct rtllib_network *net)
1332 {
1333         u8 tmp_ssid[IW_ESSID_MAX_SIZE + 1];
1334         int tmp_ssid_len = 0;
1335
1336         short apset, ssidset, ssidbroad, apmatch, ssidmatch;
1337
1338         /* we are interested in new only if we are not associated
1339          * and we are not associating / authenticating
1340          */
1341         if (ieee->link_state != MAC80211_NOLINK)
1342                 return;
1343
1344         if ((ieee->iw_mode == IW_MODE_INFRA) && !(net->capability &
1345             WLAN_CAPABILITY_ESS))
1346                 return;
1347
1348         if (ieee->iw_mode == IW_MODE_INFRA) {
1349                 /* if the user specified the AP MAC, we need also the essid
1350                  * This could be obtained by beacons or, if the network does not
1351                  * broadcast it, it can be put manually.
1352                  */
1353                 apset = ieee->wap_set;
1354                 ssidset = ieee->ssid_set;
1355                 ssidbroad =  !(net->ssid_len == 0 || net->ssid[0] == '\0');
1356                 apmatch = (memcmp(ieee->current_network.bssid, net->bssid,
1357                                   ETH_ALEN) == 0);
1358                 if (!ssidbroad) {
1359                         ssidmatch = (ieee->current_network.ssid_len ==
1360                                     net->hidden_ssid_len) &&
1361                                     (!strncmp(ieee->current_network.ssid,
1362                                     net->hidden_ssid, net->hidden_ssid_len));
1363                         if (net->hidden_ssid_len > 0) {
1364                                 strncpy(net->ssid, net->hidden_ssid,
1365                                         net->hidden_ssid_len);
1366                                 net->ssid_len = net->hidden_ssid_len;
1367                                 ssidbroad = 1;
1368                         }
1369                 } else {
1370                         ssidmatch =
1371                            (ieee->current_network.ssid_len == net->ssid_len) &&
1372                            (!strncmp(ieee->current_network.ssid, net->ssid,
1373                            net->ssid_len));
1374                 }
1375
1376                 /* if the user set the AP check if match.
1377                  * if the network does not broadcast essid we check the
1378                  *       user supplied ANY essid
1379                  * if the network does broadcast and the user does not set
1380                  *       essid it is OK
1381                  * if the network does broadcast and the user did set essid
1382                  * check if essid match
1383                  * if the ap is not set, check that the user set the bssid
1384                  * and the network does broadcast and that those two bssid match
1385                  */
1386                 if ((apset && apmatch &&
1387                    ((ssidset && ssidbroad && ssidmatch) ||
1388                    (ssidbroad && !ssidset) || (!ssidbroad && ssidset))) ||
1389                    (!apset && ssidset && ssidbroad && ssidmatch) ||
1390                    (ieee->is_roaming && ssidset && ssidbroad && ssidmatch)) {
1391                         /* Save the essid so that if it is hidden, it is
1392                          * replaced with the essid provided by the user.
1393                          */
1394                         if (!ssidbroad) {
1395                                 memcpy(tmp_ssid, ieee->current_network.ssid,
1396                                        ieee->current_network.ssid_len);
1397                                 tmp_ssid_len = ieee->current_network.ssid_len;
1398                         }
1399                         memcpy(&ieee->current_network, net,
1400                                 sizeof(ieee->current_network));
1401                         if (!ssidbroad) {
1402                                 memcpy(ieee->current_network.ssid, tmp_ssid,
1403                                        tmp_ssid_len);
1404                                 ieee->current_network.ssid_len = tmp_ssid_len;
1405                         }
1406                         netdev_info(ieee->dev,
1407                                     "Linking with %s,channel:%d, qos:%d, myHT:%d, networkHT:%d, mode:%x cur_net.flags:0x%x\n",
1408                                     ieee->current_network.ssid,
1409                                     ieee->current_network.channel,
1410                                     ieee->current_network.qos_data.supported,
1411                                     ieee->ht_info->enable_ht,
1412                                     ieee->current_network.bssht.bd_support_ht,
1413                                     ieee->current_network.mode,
1414                                     ieee->current_network.flags);
1415
1416                         if ((rtllib_act_scanning(ieee, false)) &&
1417                            !(ieee->softmac_features & IEEE_SOFTMAC_SCAN))
1418                                 rtllib_stop_scan_syncro(ieee);
1419
1420                         HTResetIOTSetting(ieee->ht_info);
1421                         ieee->wmm_acm = 0;
1422                         if (ieee->iw_mode == IW_MODE_INFRA) {
1423                                 /* Join the network for the first time */
1424                                 ieee->AsocRetryCount = 0;
1425                                 if ((ieee->current_network.qos_data.supported == 1) &&
1426                                     ieee->current_network.bssht.bd_support_ht)
1427                                         HTResetSelfAndSavePeerSetting(ieee,
1428                                                  &(ieee->current_network));
1429                                 else
1430                                         ieee->ht_info->current_ht_support = false;
1431
1432                                 ieee->link_state = RTLLIB_ASSOCIATING;
1433                                 schedule_delayed_work(
1434                                            &ieee->associate_procedure_wq, 0);
1435                         } else {
1436                                 if (rtllib_is_54g(&ieee->current_network)) {
1437                                         ieee->rate = 108;
1438                                         ieee->set_wireless_mode(ieee->dev, WIRELESS_MODE_G);
1439                                         netdev_info(ieee->dev,
1440                                                     "Using G rates\n");
1441                                 } else {
1442                                         ieee->rate = 22;
1443                                         ieee->set_wireless_mode(ieee->dev, WIRELESS_MODE_B);
1444                                         netdev_info(ieee->dev,
1445                                                     "Using B rates\n");
1446                                 }
1447                                 memset(ieee->dot11ht_oper_rate_set, 0, 16);
1448                                 ieee->link_state = MAC80211_LINKED;
1449                         }
1450                 }
1451         }
1452 }
1453
1454 static void rtllib_softmac_check_all_nets(struct rtllib_device *ieee)
1455 {
1456         unsigned long flags;
1457         struct rtllib_network *target;
1458
1459         spin_lock_irqsave(&ieee->lock, flags);
1460
1461         list_for_each_entry(target, &ieee->network_list, list) {
1462                 /* if the state become different that NOLINK means
1463                  * we had found what we are searching for
1464                  */
1465
1466                 if (ieee->link_state != MAC80211_NOLINK)
1467                         break;
1468
1469                 if (ieee->scan_age == 0 || time_after(target->last_scanned +
1470                     ieee->scan_age, jiffies))
1471                         rtllib_softmac_new_net(ieee, target);
1472         }
1473         spin_unlock_irqrestore(&ieee->lock, flags);
1474 }
1475
1476 static inline int auth_parse(struct net_device *dev, struct sk_buff *skb,
1477                              u8 **challenge, int *chlen)
1478 {
1479         struct rtllib_authentication *a;
1480         u8 *t;
1481
1482         if (skb->len <  (sizeof(struct rtllib_authentication) -
1483             sizeof(struct rtllib_info_element))) {
1484                 netdev_dbg(dev, "invalid len in auth resp: %d\n", skb->len);
1485                 return -EINVAL;
1486         }
1487         *challenge = NULL;
1488         a = (struct rtllib_authentication *)skb->data;
1489         if (skb->len > (sizeof(struct rtllib_authentication) + 3)) {
1490                 t = skb->data + sizeof(struct rtllib_authentication);
1491
1492                 if (*(t++) == MFIE_TYPE_CHALLENGE) {
1493                         *chlen = *(t++);
1494                         *challenge = kmemdup(t, *chlen, GFP_ATOMIC);
1495                         if (!*challenge)
1496                                 return -ENOMEM;
1497                 }
1498         }
1499
1500         if (a->status) {
1501                 netdev_dbg(dev, "auth_parse() failed\n");
1502                 return -EINVAL;
1503         }
1504
1505         return 0;
1506 }
1507
1508 static inline u16 assoc_parse(struct rtllib_device *ieee, struct sk_buff *skb,
1509                               int *aid)
1510 {
1511         struct rtllib_assoc_response_frame *response_head;
1512         u16 status_code;
1513
1514         if (skb->len <  sizeof(struct rtllib_assoc_response_frame)) {
1515                 netdev_dbg(ieee->dev, "Invalid len in auth resp: %d\n",
1516                            skb->len);
1517                 return 0xcafe;
1518         }
1519
1520         response_head = (struct rtllib_assoc_response_frame *)skb->data;
1521         *aid = le16_to_cpu(response_head->aid) & 0x3fff;
1522
1523         status_code = le16_to_cpu(response_head->status);
1524         if ((status_code == WLAN_STATUS_ASSOC_DENIED_RATES ||
1525            status_code == WLAN_STATUS_CAPS_UNSUPPORTED) &&
1526            ((ieee->mode == WIRELESS_MODE_G) &&
1527            (ieee->current_network.mode == WIRELESS_MODE_N_24G) &&
1528            (ieee->AsocRetryCount++ < (RT_ASOC_RETRY_LIMIT - 1)))) {
1529                 ieee->ht_info->iot_action |= HT_IOT_ACT_PURE_N_MODE;
1530         } else {
1531                 ieee->AsocRetryCount = 0;
1532         }
1533
1534         return le16_to_cpu(response_head->status);
1535 }
1536
1537 void rtllib_sta_ps_send_null_frame(struct rtllib_device *ieee, short pwr)
1538 {
1539         struct sk_buff *buf = rtllib_null_func(ieee, pwr);
1540
1541         if (buf)
1542                 softmac_ps_mgmt_xmit(buf, ieee);
1543 }
1544 EXPORT_SYMBOL(rtllib_sta_ps_send_null_frame);
1545
1546 void rtllib_sta_ps_send_pspoll_frame(struct rtllib_device *ieee)
1547 {
1548         struct sk_buff *buf = rtllib_pspoll_func(ieee);
1549
1550         if (buf)
1551                 softmac_ps_mgmt_xmit(buf, ieee);
1552 }
1553
1554 static short rtllib_sta_ps_sleep(struct rtllib_device *ieee, u64 *time)
1555 {
1556         int timeout;
1557         u8 dtim;
1558         struct rt_pwr_save_ctrl *psc = &ieee->pwr_save_ctrl;
1559
1560         if (ieee->LPSDelayCnt) {
1561                 ieee->LPSDelayCnt--;
1562                 return 0;
1563         }
1564
1565         dtim = ieee->current_network.dtim_data;
1566         if (!(dtim & RTLLIB_DTIM_VALID))
1567                 return 0;
1568         timeout = ieee->current_network.beacon_interval;
1569         ieee->current_network.dtim_data = RTLLIB_DTIM_INVALID;
1570         /* there's no need to nofity AP that I find you buffered
1571          * with broadcast packet
1572          */
1573         if (dtim & (RTLLIB_DTIM_UCAST & ieee->ps))
1574                 return 2;
1575
1576         if (!time_after(jiffies,
1577                         dev_trans_start(ieee->dev) + msecs_to_jiffies(timeout)))
1578                 return 0;
1579         if (!time_after(jiffies,
1580                         ieee->last_rx_ps_time + msecs_to_jiffies(timeout)))
1581                 return 0;
1582         if ((ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE) &&
1583             (ieee->mgmt_queue_tail != ieee->mgmt_queue_head))
1584                 return 0;
1585
1586         if (time) {
1587                 if (ieee->bAwakePktSent) {
1588                         psc->LPSAwakeIntvl = 1;
1589                 } else {
1590                         u8 MaxPeriod = 5;
1591
1592                         if (psc->LPSAwakeIntvl == 0)
1593                                 psc->LPSAwakeIntvl = 1;
1594                         psc->LPSAwakeIntvl = (psc->LPSAwakeIntvl >=
1595                                                MaxPeriod) ? MaxPeriod :
1596                                                (psc->LPSAwakeIntvl + 1);
1597                 }
1598                 {
1599                         u8 LPSAwakeIntvl_tmp = 0;
1600                         u8 period = ieee->current_network.dtim_period;
1601                         u8 count = ieee->current_network.tim.tim_count;
1602
1603                         if (count == 0) {
1604                                 if (psc->LPSAwakeIntvl > period)
1605                                         LPSAwakeIntvl_tmp = period +
1606                                                  (psc->LPSAwakeIntvl -
1607                                                  period) -
1608                                                  ((psc->LPSAwakeIntvl - period) %
1609                                                  period);
1610                                 else
1611                                         LPSAwakeIntvl_tmp = psc->LPSAwakeIntvl;
1612
1613                         } else {
1614                                 if (psc->LPSAwakeIntvl >
1615                                     ieee->current_network.tim.tim_count)
1616                                         LPSAwakeIntvl_tmp = count +
1617                                         (psc->LPSAwakeIntvl - count) -
1618                                         ((psc->LPSAwakeIntvl - count) % period);
1619                                 else
1620                                         LPSAwakeIntvl_tmp = psc->LPSAwakeIntvl;
1621                         }
1622
1623                 *time = ieee->current_network.last_dtim_sta_time
1624                         + msecs_to_jiffies(ieee->current_network.beacon_interval *
1625                         LPSAwakeIntvl_tmp);
1626         }
1627         }
1628
1629         return 1;
1630 }
1631
1632 static inline void rtllib_sta_ps(struct work_struct *work)
1633 {
1634         struct rtllib_device *ieee;
1635         u64 time;
1636         short sleep;
1637         unsigned long flags, flags2;
1638
1639         ieee = container_of(work, struct rtllib_device, ps_task);
1640
1641         spin_lock_irqsave(&ieee->lock, flags);
1642
1643         if ((ieee->ps == RTLLIB_PS_DISABLED ||
1644              ieee->iw_mode != IW_MODE_INFRA ||
1645              ieee->link_state != MAC80211_LINKED)) {
1646                 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1647                 rtllib_sta_wakeup(ieee, 1);
1648
1649                 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1650         }
1651         sleep = rtllib_sta_ps_sleep(ieee, &time);
1652         /* 2 wake, 1 sleep, 0 do nothing */
1653         if (sleep == 0)
1654                 goto out;
1655         if (sleep == 1) {
1656                 if (ieee->sta_sleep == LPS_IS_SLEEP) {
1657                         ieee->enter_sleep_state(ieee->dev, time);
1658                 } else if (ieee->sta_sleep == LPS_IS_WAKE) {
1659                         spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1660
1661                         if (ieee->ps_is_queue_empty(ieee->dev)) {
1662                                 ieee->sta_sleep = LPS_WAIT_NULL_DATA_SEND;
1663                                 ieee->ack_tx_to_ieee = 1;
1664                                 rtllib_sta_ps_send_null_frame(ieee, 1);
1665                                 ieee->ps_time = time;
1666                         }
1667                         spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1668                 }
1669
1670                 ieee->bAwakePktSent = false;
1671
1672         } else if (sleep == 2) {
1673                 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1674
1675                 rtllib_sta_wakeup(ieee, 1);
1676
1677                 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1678         }
1679
1680 out:
1681         spin_unlock_irqrestore(&ieee->lock, flags);
1682 }
1683
1684 static void rtllib_sta_wakeup(struct rtllib_device *ieee, short nl)
1685 {
1686         if (ieee->sta_sleep == LPS_IS_WAKE) {
1687                 if (nl) {
1688                         if (ieee->ht_info->iot_action &
1689                             HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
1690                                 ieee->ack_tx_to_ieee = 1;
1691                                 rtllib_sta_ps_send_null_frame(ieee, 0);
1692                         } else {
1693                                 ieee->ack_tx_to_ieee = 1;
1694                                 rtllib_sta_ps_send_pspoll_frame(ieee);
1695                         }
1696                 }
1697                 return;
1698         }
1699
1700         if (ieee->sta_sleep == LPS_IS_SLEEP)
1701                 ieee->sta_wake_up(ieee->dev);
1702         if (nl) {
1703                 if (ieee->ht_info->iot_action &
1704                     HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
1705                         ieee->ack_tx_to_ieee = 1;
1706                         rtllib_sta_ps_send_null_frame(ieee, 0);
1707                 } else {
1708                         ieee->ack_tx_to_ieee = 1;
1709                         ieee->polling = true;
1710                         rtllib_sta_ps_send_pspoll_frame(ieee);
1711                 }
1712
1713         } else {
1714                 ieee->sta_sleep = LPS_IS_WAKE;
1715                 ieee->polling = false;
1716         }
1717 }
1718
1719 void rtllib_ps_tx_ack(struct rtllib_device *ieee, short success)
1720 {
1721         unsigned long flags, flags2;
1722
1723         spin_lock_irqsave(&ieee->lock, flags);
1724
1725         if (ieee->sta_sleep == LPS_WAIT_NULL_DATA_SEND) {
1726                 /* Null frame with PS bit set */
1727                 if (success) {
1728                         ieee->sta_sleep = LPS_IS_SLEEP;
1729                         ieee->enter_sleep_state(ieee->dev, ieee->ps_time);
1730                 }
1731                 /* if the card report not success we can't be sure the AP
1732                  * has not RXed so we can't assume the AP believe us awake
1733                  */
1734         } else {/* 21112005 - tx again null without PS bit if lost */
1735
1736                 if ((ieee->sta_sleep == LPS_IS_WAKE) && !success) {
1737                         spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1738                         if (ieee->ht_info->iot_action &
1739                             HT_IOT_ACT_NULL_DATA_POWER_SAVING)
1740                                 rtllib_sta_ps_send_null_frame(ieee, 0);
1741                         else
1742                                 rtllib_sta_ps_send_pspoll_frame(ieee);
1743                         spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1744                 }
1745         }
1746         spin_unlock_irqrestore(&ieee->lock, flags);
1747 }
1748 EXPORT_SYMBOL(rtllib_ps_tx_ack);
1749
1750 static void rtllib_process_action(struct rtllib_device *ieee,
1751                                   struct sk_buff *skb)
1752 {
1753         u8 *act = skb->data + RTLLIB_3ADDR_LEN;
1754         u8 category = 0;
1755
1756         category = *act;
1757         act++;
1758         switch (category) {
1759         case ACT_CAT_BA:
1760                 switch (*act) {
1761                 case ACT_ADDBAREQ:
1762                         rtllib_rx_ADDBAReq(ieee, skb);
1763                         break;
1764                 case ACT_ADDBARSP:
1765                         rtllib_rx_ADDBARsp(ieee, skb);
1766                         break;
1767                 case ACT_DELBA:
1768                         rtllib_rx_DELBA(ieee, skb);
1769                         break;
1770                 }
1771                 break;
1772         default:
1773                 break;
1774         }
1775 }
1776
1777 static inline int
1778 rtllib_rx_assoc_resp(struct rtllib_device *ieee, struct sk_buff *skb,
1779                      struct rtllib_rx_stats *rx_stats)
1780 {
1781         u16 errcode;
1782         int aid;
1783         u8 *ies;
1784         struct rtllib_assoc_response_frame *assoc_resp;
1785         struct ieee80211_hdr_3addr *header = (struct ieee80211_hdr_3addr *)skb->data;
1786         u16 frame_ctl = le16_to_cpu(header->frame_control);
1787
1788         netdev_dbg(ieee->dev, "received [RE]ASSOCIATION RESPONSE (%d)\n",
1789                    WLAN_FC_GET_STYPE(frame_ctl));
1790
1791         if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1792              ieee->link_state == RTLLIB_ASSOCIATING_AUTHENTICATED &&
1793              (ieee->iw_mode == IW_MODE_INFRA)) {
1794                 errcode = assoc_parse(ieee, skb, &aid);
1795                 if (!errcode) {
1796                         struct rtllib_network *network =
1797                                  kzalloc(sizeof(struct rtllib_network),
1798                                  GFP_ATOMIC);
1799
1800                         if (!network)
1801                                 return 1;
1802                         ieee->link_state = MAC80211_LINKED;
1803                         ieee->assoc_id = aid;
1804                         ieee->softmac_stats.rx_ass_ok++;
1805                         /* station support qos */
1806                         /* Let the register setting default with Legacy station */
1807                         assoc_resp = (struct rtllib_assoc_response_frame *)skb->data;
1808                         if (ieee->current_network.qos_data.supported == 1) {
1809                                 if (rtllib_parse_info_param(ieee, assoc_resp->info_element,
1810                                                         rx_stats->len - sizeof(*assoc_resp),
1811                                                         network, rx_stats)) {
1812                                         kfree(network);
1813                                         return 1;
1814                                 }
1815                                 memcpy(ieee->ht_info->PeerHTCapBuf,
1816                                        network->bssht.bd_ht_cap_buf,
1817                                        network->bssht.bd_ht_cap_len);
1818                                 memcpy(ieee->ht_info->PeerHTInfoBuf,
1819                                        network->bssht.bd_ht_info_buf,
1820                                        network->bssht.bd_ht_info_len);
1821                                 ieee->handle_assoc_response(ieee->dev,
1822                                         (struct rtllib_assoc_response_frame *)header, network);
1823                         }
1824                         kfree(network);
1825
1826                         kfree(ieee->assocresp_ies);
1827                         ieee->assocresp_ies = NULL;
1828                         ies = &(assoc_resp->info_element[0].id);
1829                         ieee->assocresp_ies_len = (skb->data + skb->len) - ies;
1830                         ieee->assocresp_ies = kmemdup(ies,
1831                                                       ieee->assocresp_ies_len,
1832                                                       GFP_ATOMIC);
1833                         if (!ieee->assocresp_ies)
1834                                 ieee->assocresp_ies_len = 0;
1835
1836                         rtllib_associate_complete(ieee);
1837                 } else {
1838                         /* aid could not been allocated */
1839                         ieee->softmac_stats.rx_ass_err++;
1840                         netdev_info(ieee->dev,
1841                                     "Association response status code 0x%x\n",
1842                                     errcode);
1843                         if (ieee->AsocRetryCount < RT_ASOC_RETRY_LIMIT)
1844                                 schedule_delayed_work(
1845                                          &ieee->associate_procedure_wq, 0);
1846                         else
1847                                 rtllib_associate_abort(ieee);
1848                 }
1849         }
1850         return 0;
1851 }
1852
1853 static void rtllib_rx_auth_resp(struct rtllib_device *ieee, struct sk_buff *skb)
1854 {
1855         int errcode;
1856         u8 *challenge;
1857         int chlen = 0;
1858         bool bSupportNmode = true, bHalfSupportNmode = false;
1859
1860         errcode = auth_parse(ieee->dev, skb, &challenge, &chlen);
1861
1862         if (errcode) {
1863                 ieee->softmac_stats.rx_auth_rs_err++;
1864                 netdev_info(ieee->dev,
1865                             "Authentication response status code %d", errcode);
1866                 rtllib_associate_abort(ieee);
1867                 return;
1868         }
1869
1870         if (ieee->open_wep || !challenge) {
1871                 ieee->link_state = RTLLIB_ASSOCIATING_AUTHENTICATED;
1872                 ieee->softmac_stats.rx_auth_rs_ok++;
1873                 if (!(ieee->ht_info->iot_action & HT_IOT_ACT_PURE_N_MODE)) {
1874                         if (!ieee->GetNmodeSupportBySecCfg(ieee->dev)) {
1875                                 if (IsHTHalfNmodeAPs(ieee)) {
1876                                         bSupportNmode = true;
1877                                         bHalfSupportNmode = true;
1878                                 } else {
1879                                         bSupportNmode = false;
1880                                         bHalfSupportNmode = false;
1881                                 }
1882                         }
1883                 }
1884                 /* Dummy wirless mode setting to avoid encryption issue */
1885                 if (bSupportNmode) {
1886                         ieee->set_wireless_mode(ieee->dev,
1887                                               ieee->current_network.mode);
1888                 } else {
1889                         /*TODO*/
1890                         ieee->set_wireless_mode(ieee->dev, WIRELESS_MODE_G);
1891                 }
1892
1893                 if ((ieee->current_network.mode == WIRELESS_MODE_N_24G) &&
1894                     bHalfSupportNmode) {
1895                         netdev_info(ieee->dev, "======>enter half N mode\n");
1896                         ieee->bHalfWirelessN24GMode = true;
1897                 } else {
1898                         ieee->bHalfWirelessN24GMode = false;
1899                 }
1900                 rtllib_associate_step2(ieee);
1901         } else {
1902                 rtllib_auth_challenge(ieee, challenge,  chlen);
1903         }
1904 }
1905
1906 static inline int
1907 rtllib_rx_auth(struct rtllib_device *ieee, struct sk_buff *skb,
1908                struct rtllib_rx_stats *rx_stats)
1909 {
1910         if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) {
1911                 if (ieee->link_state == RTLLIB_ASSOCIATING_AUTHENTICATING &&
1912                     (ieee->iw_mode == IW_MODE_INFRA)) {
1913                         netdev_dbg(ieee->dev,
1914                                    "Received authentication response");
1915                         rtllib_rx_auth_resp(ieee, skb);
1916                 }
1917         }
1918         return 0;
1919 }
1920
1921 static inline int
1922 rtllib_rx_deauth(struct rtllib_device *ieee, struct sk_buff *skb)
1923 {
1924         struct ieee80211_hdr_3addr *header = (struct ieee80211_hdr_3addr *)skb->data;
1925         u16 frame_ctl;
1926
1927         if (memcmp(header->addr3, ieee->current_network.bssid, ETH_ALEN) != 0)
1928                 return 0;
1929
1930         /* FIXME for now repeat all the association procedure
1931          * both for disassociation and deauthentication
1932          */
1933         if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1934             ieee->link_state == MAC80211_LINKED &&
1935             (ieee->iw_mode == IW_MODE_INFRA)) {
1936                 frame_ctl = le16_to_cpu(header->frame_control);
1937                 netdev_info(ieee->dev,
1938                             "==========>received disassoc/deauth(%x) frame, reason code:%x\n",
1939                             WLAN_FC_GET_STYPE(frame_ctl),
1940                             ((struct rtllib_disassoc *)skb->data)->reason);
1941                 ieee->link_state = RTLLIB_ASSOCIATING;
1942                 ieee->softmac_stats.reassoc++;
1943                 ieee->is_roaming = true;
1944                 ieee->link_detect_info.bBusyTraffic = false;
1945                 rtllib_disassociate(ieee);
1946                 RemovePeerTS(ieee, header->addr2);
1947                 if (!(ieee->rtllib_ap_sec_type(ieee) &
1948                     (SEC_ALG_CCMP | SEC_ALG_TKIP)))
1949                         schedule_delayed_work(
1950                                        &ieee->associate_procedure_wq, 5);
1951         }
1952         return 0;
1953 }
1954
1955 inline int rtllib_rx_frame_softmac(struct rtllib_device *ieee,
1956                                    struct sk_buff *skb,
1957                                    struct rtllib_rx_stats *rx_stats, u16 type,
1958                                    u16 stype)
1959 {
1960         struct ieee80211_hdr_3addr *header = (struct ieee80211_hdr_3addr *)skb->data;
1961         u16 frame_ctl;
1962
1963         if (!ieee->proto_started)
1964                 return 0;
1965
1966         frame_ctl = le16_to_cpu(header->frame_control);
1967         switch (WLAN_FC_GET_STYPE(frame_ctl)) {
1968         case IEEE80211_STYPE_ASSOC_RESP:
1969         case IEEE80211_STYPE_REASSOC_RESP:
1970                 if (rtllib_rx_assoc_resp(ieee, skb, rx_stats) == 1)
1971                         return 1;
1972                 break;
1973         case IEEE80211_STYPE_ASSOC_REQ:
1974         case IEEE80211_STYPE_REASSOC_REQ:
1975                 break;
1976         case IEEE80211_STYPE_AUTH:
1977                 rtllib_rx_auth(ieee, skb, rx_stats);
1978                 break;
1979         case IEEE80211_STYPE_DISASSOC:
1980         case IEEE80211_STYPE_DEAUTH:
1981                 rtllib_rx_deauth(ieee, skb);
1982                 break;
1983         case IEEE80211_STYPE_ACTION:
1984                 rtllib_process_action(ieee, skb);
1985                 break;
1986         default:
1987                 return -1;
1988         }
1989         return 0;
1990 }
1991
1992 /* following are for a simpler TX queue management.
1993  * Instead of using netif_[stop/wake]_queue the driver
1994  * will use these two functions (plus a reset one), that
1995  * will internally use the kernel netif_* and takes
1996  * care of the ieee802.11 fragmentation.
1997  * So the driver receives a fragment per time and might
1998  * call the stop function when it wants to not
1999  * have enough room to TX an entire packet.
2000  * This might be useful if each fragment needs it's own
2001  * descriptor, thus just keep a total free memory > than
2002  * the max fragmentation threshold is not enough.. If the
2003  * ieee802.11 stack passed a TXB struct then you need
2004  * to keep N free descriptors where
2005  * N = MAX_PACKET_SIZE / MIN_FRAG_TRESHOLD
2006  * In this way you need just one and the 802.11 stack
2007  * will take care of buffering fragments and pass them to
2008  * the driver later, when it wakes the queue.
2009  */
2010 void rtllib_softmac_xmit(struct rtllib_txb *txb, struct rtllib_device *ieee)
2011 {
2012         unsigned int queue_index = txb->queue_index;
2013         unsigned long flags;
2014         int  i;
2015         struct cb_desc *tcb_desc = NULL;
2016         unsigned long queue_len = 0;
2017
2018         spin_lock_irqsave(&ieee->lock, flags);
2019
2020         /* called with 2nd parm 0, no tx mgmt lock required */
2021         rtllib_sta_wakeup(ieee, 0);
2022
2023         /* update the tx status */
2024         tcb_desc = (struct cb_desc *)(txb->fragments[0]->cb +
2025                    MAX_DEV_ADDR_SIZE);
2026         if (tcb_desc->bMulticast)
2027                 ieee->stats.multicast++;
2028
2029         /* if xmit available, just xmit it immediately, else just insert it to
2030          * the wait queue
2031          */
2032         for (i = 0; i < txb->nr_frags; i++) {
2033                 queue_len = skb_queue_len(&ieee->skb_waitQ[queue_index]);
2034                 if ((queue_len  != 0) ||
2035                     (!ieee->check_nic_enough_desc(ieee->dev, queue_index)) ||
2036                     (ieee->queue_stop)) {
2037                         /* insert the skb packet to the wait queue
2038                          * as for the completion function, it does not need
2039                          * to check it any more.
2040                          */
2041                         if (queue_len < 200)
2042                                 skb_queue_tail(&ieee->skb_waitQ[queue_index],
2043                                                txb->fragments[i]);
2044                         else
2045                                 kfree_skb(txb->fragments[i]);
2046                 } else {
2047                         ieee->softmac_data_hard_start_xmit(
2048                                         txb->fragments[i],
2049                                         ieee->dev, ieee->rate);
2050                 }
2051         }
2052
2053         rtllib_txb_free(txb);
2054
2055         spin_unlock_irqrestore(&ieee->lock, flags);
2056 }
2057
2058 void rtllib_reset_queue(struct rtllib_device *ieee)
2059 {
2060         unsigned long flags;
2061
2062         spin_lock_irqsave(&ieee->lock, flags);
2063         init_mgmt_queue(ieee);
2064         if (ieee->tx_pending.txb) {
2065                 rtllib_txb_free(ieee->tx_pending.txb);
2066                 ieee->tx_pending.txb = NULL;
2067         }
2068         ieee->queue_stop = 0;
2069         spin_unlock_irqrestore(&ieee->lock, flags);
2070 }
2071 EXPORT_SYMBOL(rtllib_reset_queue);
2072
2073 void rtllib_stop_all_queues(struct rtllib_device *ieee)
2074 {
2075         unsigned int i;
2076
2077         for (i = 0; i < ieee->dev->num_tx_queues; i++)
2078                 txq_trans_cond_update(netdev_get_tx_queue(ieee->dev, i));
2079
2080         netif_tx_stop_all_queues(ieee->dev);
2081 }
2082
2083 void rtllib_wake_all_queues(struct rtllib_device *ieee)
2084 {
2085         netif_tx_wake_all_queues(ieee->dev);
2086 }
2087
2088 /* this is called only in user context, with wx_mutex held */
2089 static void rtllib_start_bss(struct rtllib_device *ieee)
2090 {
2091         unsigned long flags;
2092
2093         if (IS_DOT11D_ENABLE(ieee) && !IS_COUNTRY_IE_VALID(ieee)) {
2094                 if (!ieee->global_domain)
2095                         return;
2096         }
2097         /* check if we have already found the net we
2098          * are interested in (if any).
2099          * if not (we are disassociated and we are not
2100          * in associating / authenticating phase) start the background scanning.
2101          */
2102         rtllib_softmac_check_all_nets(ieee);
2103
2104         /* ensure no-one start an associating process (thus setting
2105          * the ieee->link_state to rtllib_ASSOCIATING) while we
2106          * have just checked it and we are going to enable scan.
2107          * The rtllib_new_net function is always called with
2108          * lock held (from both rtllib_softmac_check_all_nets and
2109          * the rx path), so we cannot be in the middle of such function
2110          */
2111         spin_lock_irqsave(&ieee->lock, flags);
2112
2113         if (ieee->link_state == MAC80211_NOLINK)
2114                 rtllib_start_scan(ieee);
2115         spin_unlock_irqrestore(&ieee->lock, flags);
2116 }
2117
2118 static void rtllib_link_change_wq(void *data)
2119 {
2120         struct rtllib_device *ieee = container_of_dwork_rsl(data,
2121                                      struct rtllib_device, link_change_wq);
2122         ieee->link_change(ieee->dev);
2123 }
2124 /* called only in userspace context */
2125 void rtllib_disassociate(struct rtllib_device *ieee)
2126 {
2127         netif_carrier_off(ieee->dev);
2128         if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)
2129                 rtllib_reset_queue(ieee);
2130
2131         if (IS_DOT11D_ENABLE(ieee))
2132                 dot11d_reset(ieee);
2133         ieee->link_state = MAC80211_NOLINK;
2134         ieee->is_set_key = false;
2135         ieee->wap_set = 0;
2136
2137         schedule_delayed_work(&ieee->link_change_wq, 0);
2138
2139         notify_wx_assoc_event(ieee);
2140 }
2141
2142 static void rtllib_associate_retry_wq(void *data)
2143 {
2144         struct rtllib_device *ieee = container_of_dwork_rsl(data,
2145                                      struct rtllib_device, associate_retry_wq);
2146         unsigned long flags;
2147
2148         mutex_lock(&ieee->wx_mutex);
2149         if (!ieee->proto_started)
2150                 goto exit;
2151
2152         if (ieee->link_state != RTLLIB_ASSOCIATING_RETRY)
2153                 goto exit;
2154
2155         /* until we do not set the state to MAC80211_NOLINK
2156          * there are no possibility to have someone else trying
2157          * to start an association procedure (we get here with
2158          * ieee->link_state = RTLLIB_ASSOCIATING).
2159          * When we set the state to MAC80211_NOLINK it is possible
2160          * that the RX path run an attempt to associate, but
2161          * both rtllib_softmac_check_all_nets and the
2162          * RX path works with ieee->lock held so there are no
2163          * problems. If we are still disassociated then start a scan.
2164          * the lock here is necessary to ensure no one try to start
2165          * an association procedure when we have just checked the
2166          * state and we are going to start the scan.
2167          */
2168         ieee->beinretry = true;
2169         ieee->link_state = MAC80211_NOLINK;
2170
2171         rtllib_softmac_check_all_nets(ieee);
2172
2173         spin_lock_irqsave(&ieee->lock, flags);
2174
2175         if (ieee->link_state == MAC80211_NOLINK)
2176                 rtllib_start_scan(ieee);
2177         spin_unlock_irqrestore(&ieee->lock, flags);
2178
2179         ieee->beinretry = false;
2180 exit:
2181         mutex_unlock(&ieee->wx_mutex);
2182 }
2183
2184 static struct sk_buff *rtllib_get_beacon_(struct rtllib_device *ieee)
2185 {
2186         static const u8 broadcast_addr[] = {
2187                 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2188         };
2189         struct sk_buff *skb;
2190         struct rtllib_probe_response *b;
2191
2192         skb = rtllib_probe_resp(ieee, broadcast_addr);
2193
2194         if (!skb)
2195                 return NULL;
2196
2197         b = (struct rtllib_probe_response *)skb->data;
2198         b->header.frame_control = cpu_to_le16(IEEE80211_STYPE_BEACON);
2199
2200         return skb;
2201 }
2202
2203 struct sk_buff *rtllib_get_beacon(struct rtllib_device *ieee)
2204 {
2205         struct sk_buff *skb;
2206         struct rtllib_probe_response *b;
2207
2208         skb = rtllib_get_beacon_(ieee);
2209         if (!skb)
2210                 return NULL;
2211
2212         b = (struct rtllib_probe_response *)skb->data;
2213         b->header.seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2214
2215         if (ieee->seq_ctrl[0] == 0xFFF)
2216                 ieee->seq_ctrl[0] = 0;
2217         else
2218                 ieee->seq_ctrl[0]++;
2219
2220         return skb;
2221 }
2222 EXPORT_SYMBOL(rtllib_get_beacon);
2223
2224 void rtllib_softmac_stop_protocol(struct rtllib_device *ieee)
2225 {
2226         rtllib_stop_scan_syncro(ieee);
2227         mutex_lock(&ieee->wx_mutex);
2228         rtllib_stop_protocol(ieee);
2229         mutex_unlock(&ieee->wx_mutex);
2230 }
2231 EXPORT_SYMBOL(rtllib_softmac_stop_protocol);
2232
2233 void rtllib_stop_protocol(struct rtllib_device *ieee)
2234 {
2235         if (!ieee->proto_started)
2236                 return;
2237
2238         ieee->proto_started = 0;
2239         ieee->proto_stoppping = 1;
2240         ieee->rtllib_ips_leave(ieee->dev);
2241
2242         del_timer_sync(&ieee->associate_timer);
2243         mutex_unlock(&ieee->wx_mutex);
2244         cancel_delayed_work_sync(&ieee->associate_retry_wq);
2245         mutex_lock(&ieee->wx_mutex);
2246         cancel_delayed_work_sync(&ieee->link_change_wq);
2247         rtllib_stop_scan(ieee);
2248
2249         if (ieee->link_state <= RTLLIB_ASSOCIATING_AUTHENTICATED)
2250                 ieee->link_state = MAC80211_NOLINK;
2251
2252         if (ieee->link_state == MAC80211_LINKED) {
2253                 if (ieee->iw_mode == IW_MODE_INFRA)
2254                         SendDisassociation(ieee, 1, WLAN_REASON_DEAUTH_LEAVING);
2255                 rtllib_disassociate(ieee);
2256         }
2257
2258         RemoveAllTS(ieee);
2259         ieee->proto_stoppping = 0;
2260
2261         kfree(ieee->assocreq_ies);
2262         ieee->assocreq_ies = NULL;
2263         ieee->assocreq_ies_len = 0;
2264         kfree(ieee->assocresp_ies);
2265         ieee->assocresp_ies = NULL;
2266         ieee->assocresp_ies_len = 0;
2267 }
2268
2269 void rtllib_softmac_start_protocol(struct rtllib_device *ieee)
2270 {
2271         mutex_lock(&ieee->wx_mutex);
2272         rtllib_start_protocol(ieee);
2273         mutex_unlock(&ieee->wx_mutex);
2274 }
2275 EXPORT_SYMBOL(rtllib_softmac_start_protocol);
2276
2277 void rtllib_start_protocol(struct rtllib_device *ieee)
2278 {
2279         short ch = 0;
2280         int i = 0;
2281
2282         rtllib_update_active_chan_map(ieee);
2283
2284         if (ieee->proto_started)
2285                 return;
2286
2287         ieee->proto_started = 1;
2288
2289         if (ieee->current_network.channel == 0) {
2290                 do {
2291                         ch++;
2292                         if (ch > MAX_CHANNEL_NUMBER)
2293                                 return; /* no channel found */
2294                 } while (!ieee->active_channel_map[ch]);
2295                 ieee->current_network.channel = ch;
2296         }
2297
2298         if (ieee->current_network.beacon_interval == 0)
2299                 ieee->current_network.beacon_interval = 100;
2300
2301         for (i = 0; i < 17; i++) {
2302                 ieee->last_rxseq_num[i] = -1;
2303                 ieee->last_rxfrag_num[i] = -1;
2304                 ieee->last_packet_time[i] = 0;
2305         }
2306
2307         ieee->wmm_acm = 0;
2308         /* if the user set the MAC of the ad-hoc cell and then
2309          * switch to managed mode, shall we  make sure that association
2310          * attempts does not fail just because the user provide the essid
2311          * and the nic is still checking for the AP MAC ??
2312          */
2313         switch (ieee->iw_mode) {
2314         case IW_MODE_INFRA:
2315                 rtllib_start_bss(ieee);
2316                 break;
2317         }
2318 }
2319
2320 int rtllib_softmac_init(struct rtllib_device *ieee)
2321 {
2322         int i;
2323
2324         memset(&ieee->current_network, 0, sizeof(struct rtllib_network));
2325
2326         ieee->link_state = MAC80211_NOLINK;
2327         for (i = 0; i < 5; i++)
2328                 ieee->seq_ctrl[i] = 0;
2329         ieee->dot11d_info = kzalloc(sizeof(struct rt_dot11d_info), GFP_ATOMIC);
2330         if (!ieee->dot11d_info)
2331                 return -ENOMEM;
2332
2333         ieee->link_detect_info.SlotIndex = 0;
2334         ieee->link_detect_info.SlotNum = 2;
2335         ieee->link_detect_info.NumRecvBcnInPeriod = 0;
2336         ieee->link_detect_info.NumRecvDataInPeriod = 0;
2337         ieee->link_detect_info.NumTxOkInPeriod = 0;
2338         ieee->link_detect_info.NumRxOkInPeriod = 0;
2339         ieee->link_detect_info.NumRxUnicastOkInPeriod = 0;
2340         ieee->bIsAggregateFrame = false;
2341         ieee->assoc_id = 0;
2342         ieee->queue_stop = 0;
2343         ieee->scanning_continue = 0;
2344         ieee->softmac_features = 0;
2345         ieee->wap_set = 0;
2346         ieee->ssid_set = 0;
2347         ieee->proto_started = 0;
2348         ieee->proto_stoppping = 0;
2349         ieee->basic_rate = RTLLIB_DEFAULT_BASIC_RATE;
2350         ieee->rate = 22;
2351         ieee->ps = RTLLIB_PS_DISABLED;
2352         ieee->sta_sleep = LPS_IS_WAKE;
2353
2354         ieee->reg_dot11ht_oper_rate_set[0] = 0xff;
2355         ieee->reg_dot11ht_oper_rate_set[1] = 0xff;
2356         ieee->reg_dot11ht_oper_rate_set[4] = 0x01;
2357
2358         ieee->reg_dot11tx_ht_oper_rate_set[0] = 0xff;
2359         ieee->reg_dot11tx_ht_oper_rate_set[1] = 0xff;
2360         ieee->reg_dot11tx_ht_oper_rate_set[4] = 0x01;
2361
2362         ieee->FirstIe_InScan = false;
2363         ieee->actscanning = false;
2364         ieee->beinretry = false;
2365         ieee->is_set_key = false;
2366         init_mgmt_queue(ieee);
2367
2368         ieee->tx_pending.txb = NULL;
2369
2370         timer_setup(&ieee->associate_timer, rtllib_associate_abort_cb, 0);
2371
2372         timer_setup(&ieee->beacon_timer, rtllib_send_beacon_cb, 0);
2373
2374         INIT_DELAYED_WORK(&ieee->link_change_wq, (void *)rtllib_link_change_wq);
2375         INIT_WORK(&ieee->associate_complete_wq, (void *)rtllib_associate_complete_wq);
2376         INIT_DELAYED_WORK(&ieee->associate_procedure_wq, (void *)rtllib_associate_procedure_wq);
2377         INIT_DELAYED_WORK(&ieee->softmac_scan_wq, (void *)rtllib_softmac_scan_wq);
2378         INIT_DELAYED_WORK(&ieee->associate_retry_wq, (void *)rtllib_associate_retry_wq);
2379         INIT_WORK(&ieee->wx_sync_scan_wq, (void *)rtllib_wx_sync_scan_wq);
2380
2381         mutex_init(&ieee->wx_mutex);
2382         mutex_init(&ieee->scan_mutex);
2383         mutex_init(&ieee->ips_mutex);
2384
2385         spin_lock_init(&ieee->mgmt_tx_lock);
2386         spin_lock_init(&ieee->beacon_lock);
2387
2388         INIT_WORK(&ieee->ps_task, rtllib_sta_ps);
2389
2390         return 0;
2391 }
2392
2393 void rtllib_softmac_free(struct rtllib_device *ieee)
2394 {
2395         del_timer_sync(&ieee->associate_timer);
2396
2397         cancel_delayed_work_sync(&ieee->associate_retry_wq);
2398         cancel_delayed_work_sync(&ieee->associate_procedure_wq);
2399         cancel_delayed_work_sync(&ieee->softmac_scan_wq);
2400         cancel_delayed_work_sync(&ieee->hw_wakeup_wq);
2401         cancel_delayed_work_sync(&ieee->hw_sleep_wq);
2402         cancel_delayed_work_sync(&ieee->link_change_wq);
2403         cancel_work_sync(&ieee->associate_complete_wq);
2404         cancel_work_sync(&ieee->ips_leave_wq);
2405         cancel_work_sync(&ieee->wx_sync_scan_wq);
2406         cancel_work_sync(&ieee->ps_task);
2407
2408         kfree(ieee->dot11d_info);
2409         ieee->dot11d_info = NULL;
2410 }
2411
2412 static inline struct sk_buff *
2413 rtllib_disauth_skb(struct rtllib_network *beacon,
2414                    struct rtllib_device *ieee, u16 asRsn)
2415 {
2416         struct sk_buff *skb;
2417         struct rtllib_disauth *disauth;
2418         int len = sizeof(struct rtllib_disauth) + ieee->tx_headroom;
2419
2420         skb = dev_alloc_skb(len);
2421         if (!skb)
2422                 return NULL;
2423
2424         skb_reserve(skb, ieee->tx_headroom);
2425
2426         disauth = skb_put(skb, sizeof(struct rtllib_disauth));
2427         disauth->header.frame_control = cpu_to_le16(IEEE80211_STYPE_DEAUTH);
2428         disauth->header.duration_id = 0;
2429
2430         ether_addr_copy(disauth->header.addr1, beacon->bssid);
2431         ether_addr_copy(disauth->header.addr2, ieee->dev->dev_addr);
2432         ether_addr_copy(disauth->header.addr3, beacon->bssid);
2433
2434         disauth->reason = cpu_to_le16(asRsn);
2435         return skb;
2436 }
2437
2438 static inline struct sk_buff *
2439 rtllib_disassociate_skb(struct rtllib_network *beacon,
2440                         struct rtllib_device *ieee, u16 asRsn)
2441 {
2442         struct sk_buff *skb;
2443         struct rtllib_disassoc *disass;
2444         int len = sizeof(struct rtllib_disassoc) + ieee->tx_headroom;
2445
2446         skb = dev_alloc_skb(len);
2447
2448         if (!skb)
2449                 return NULL;
2450
2451         skb_reserve(skb, ieee->tx_headroom);
2452
2453         disass = skb_put(skb, sizeof(struct rtllib_disassoc));
2454         disass->header.frame_control = cpu_to_le16(IEEE80211_STYPE_DISASSOC);
2455         disass->header.duration_id = 0;
2456
2457         ether_addr_copy(disass->header.addr1, beacon->bssid);
2458         ether_addr_copy(disass->header.addr2, ieee->dev->dev_addr);
2459         ether_addr_copy(disass->header.addr3, beacon->bssid);
2460
2461         disass->reason = cpu_to_le16(asRsn);
2462         return skb;
2463 }
2464
2465 void SendDisassociation(struct rtllib_device *ieee, bool deauth, u16 asRsn)
2466 {
2467         struct rtllib_network *beacon = &ieee->current_network;
2468         struct sk_buff *skb;
2469
2470         if (deauth)
2471                 skb = rtllib_disauth_skb(beacon, ieee, asRsn);
2472         else
2473                 skb = rtllib_disassociate_skb(beacon, ieee, asRsn);
2474
2475         if (skb)
2476                 softmac_mgmt_xmit(skb, ieee);
2477 }
2478
2479 u8 rtllib_ap_sec_type(struct rtllib_device *ieee)
2480 {
2481         static u8 ccmp_ie[4] = {0x00, 0x50, 0xf2, 0x04};
2482         static u8 ccmp_rsn_ie[4] = {0x00, 0x0f, 0xac, 0x04};
2483         int wpa_ie_len = ieee->wpa_ie_len;
2484         struct lib80211_crypt_data *crypt;
2485         int encrypt;
2486
2487         crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
2488         encrypt = (ieee->current_network.capability & WLAN_CAPABILITY_PRIVACY)
2489                   || (crypt && crypt->ops && (strcmp(crypt->ops->name, "R-WEP") == 0));
2490
2491         /* simply judge  */
2492         if (encrypt && (wpa_ie_len == 0)) {
2493                 return SEC_ALG_WEP;
2494         } else if ((wpa_ie_len != 0)) {
2495                 if (((ieee->wpa_ie[0] == 0xdd) &&
2496                     (!memcmp(&(ieee->wpa_ie[14]), ccmp_ie, 4))) ||
2497                     ((ieee->wpa_ie[0] == 0x30) &&
2498                     (!memcmp(&ieee->wpa_ie[10], ccmp_rsn_ie, 4))))
2499                         return SEC_ALG_CCMP;
2500                 else
2501                         return SEC_ALG_TKIP;
2502         } else {
2503                 return SEC_ALG_NONE;
2504         }
2505 }
2506
2507 static void rtllib_MlmeDisassociateRequest(struct rtllib_device *rtllib,
2508                                            u8 *asSta, u8 asRsn)
2509 {
2510         u8 i;
2511         u8      OpMode;
2512
2513         RemovePeerTS(rtllib, asSta);
2514
2515         if (memcmp(rtllib->current_network.bssid, asSta, 6) == 0) {
2516                 rtllib->link_state = MAC80211_NOLINK;
2517
2518                 for (i = 0; i < 6; i++)
2519                         rtllib->current_network.bssid[i] = 0x22;
2520                 OpMode = RT_OP_MODE_NO_LINK;
2521                 rtllib->OpMode = RT_OP_MODE_NO_LINK;
2522                 rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_MEDIA_STATUS,
2523                                         (u8 *)(&OpMode));
2524                 rtllib_disassociate(rtllib);
2525
2526                 rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_BSSID,
2527                                         rtllib->current_network.bssid);
2528         }
2529 }
2530
2531 static void
2532 rtllib_MgntDisconnectAP(
2533         struct rtllib_device *rtllib,
2534         u8 asRsn
2535 )
2536 {
2537         bool bFilterOutNonAssociatedBSSID = false;
2538
2539         bFilterOutNonAssociatedBSSID = false;
2540         rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_CECHK_BSSID,
2541                                 (u8 *)(&bFilterOutNonAssociatedBSSID));
2542         rtllib_MlmeDisassociateRequest(rtllib, rtllib->current_network.bssid,
2543                                        asRsn);
2544
2545         rtllib->link_state = MAC80211_NOLINK;
2546 }
2547
2548 bool rtllib_MgntDisconnect(struct rtllib_device *rtllib, u8 asRsn)
2549 {
2550         if (rtllib->ps != RTLLIB_PS_DISABLED)
2551                 rtllib->sta_wake_up(rtllib->dev);
2552
2553         if (rtllib->link_state == MAC80211_LINKED) {
2554                 if (rtllib->iw_mode == IW_MODE_INFRA)
2555                         rtllib_MgntDisconnectAP(rtllib, asRsn);
2556         }
2557
2558         return true;
2559 }
2560 EXPORT_SYMBOL(rtllib_MgntDisconnect);
2561
2562 void notify_wx_assoc_event(struct rtllib_device *ieee)
2563 {
2564         union iwreq_data wrqu;
2565
2566         if (ieee->cannot_notify)
2567                 return;
2568
2569         wrqu.ap_addr.sa_family = ARPHRD_ETHER;
2570         if (ieee->link_state == MAC80211_LINKED) {
2571                 memcpy(wrqu.ap_addr.sa_data, ieee->current_network.bssid,
2572                        ETH_ALEN);
2573         } else {
2574                 netdev_info(ieee->dev, "%s(): Tell user space disconnected\n",
2575                             __func__);
2576                 eth_zero_addr(wrqu.ap_addr.sa_data);
2577         }
2578         wireless_send_event(ieee->dev, SIOCGIWAP, &wrqu, NULL);
2579 }
2580 EXPORT_SYMBOL(notify_wx_assoc_event);