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