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