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