staging: rtl8192e: Cleanup checkpatch -f errors - Part XIV
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / rtl8192e / rtllib_softmac.c
blobfa0e2afd1ed1eb4869fd4c7140e76bde42d52592
1 /* IEEE 802.11 SoftMAC layer
2 * Copyright (c) 2005 Andrea Merello <andreamrl@tiscali.it>
4 * Mostly extracted from the rtl8180-sa2400 driver for the
5 * in-kernel generic ieee802.11 stack.
7 * Few lines might be stolen from other part of the rtllib
8 * stack. Copyright who own it's copyright
10 * WPA code stolen from the ipw2200 driver.
11 * Copyright who own it's copyright.
13 * released under the GPL
17 #include "rtllib.h"
18 #include "rtl_core.h"
20 #include <linux/random.h>
21 #include <linux/delay.h>
22 #include <linux/version.h>
23 #include <linux/uaccess.h>
24 #include "dot11d.h"
26 u8 rsn_authen_cipher_suite[16][4] = {
27 {0x00, 0x0F, 0xAC, 0x00},
28 {0x00, 0x0F, 0xAC, 0x01},
29 {0x00, 0x0F, 0xAC, 0x02},
30 {0x00, 0x0F, 0xAC, 0x03},
31 {0x00, 0x0F, 0xAC, 0x04},
32 {0x00, 0x0F, 0xAC, 0x05},
35 short rtllib_is_54g(struct rtllib_network *net)
37 return (net->rates_ex_len > 0) || (net->rates_len > 4);
40 short rtllib_is_shortslot(struct rtllib_network net)
42 return net.capability & WLAN_CAPABILITY_SHORT_SLOT_TIME;
45 /* returns the total length needed for pleacing the RATE MFIE
46 * tag and the EXTENDED RATE MFIE tag if needed.
47 * It encludes two bytes per tag for the tag itself and its len
49 unsigned int rtllib_MFIE_rate_len(struct rtllib_device *ieee)
51 unsigned int rate_len = 0;
53 if (ieee->modulation & RTLLIB_CCK_MODULATION)
54 rate_len = RTLLIB_CCK_RATE_LEN + 2;
56 if (ieee->modulation & RTLLIB_OFDM_MODULATION)
58 rate_len += RTLLIB_OFDM_RATE_LEN + 2;
60 return rate_len;
63 /* pleace the MFIE rate, tag to the memory (double) poined.
64 * Then it updates the pointer so that
65 * it points after the new MFIE tag added.
67 void rtllib_MFIE_Brate(struct rtllib_device *ieee, u8 **tag_p)
69 u8 *tag = *tag_p;
71 if (ieee->modulation & RTLLIB_CCK_MODULATION) {
72 *tag++ = MFIE_TYPE_RATES;
73 *tag++ = 4;
74 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_1MB;
75 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_2MB;
76 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_5MB;
77 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_11MB;
80 /* We may add an option for custom rates that specific HW
81 * might support */
82 *tag_p = tag;
85 void rtllib_MFIE_Grate(struct rtllib_device *ieee, u8 **tag_p)
87 u8 *tag = *tag_p;
89 if (ieee->modulation & RTLLIB_OFDM_MODULATION) {
90 *tag++ = MFIE_TYPE_RATES_EX;
91 *tag++ = 8;
92 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_6MB;
93 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_9MB;
94 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_12MB;
95 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_18MB;
96 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_24MB;
97 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_36MB;
98 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_48MB;
99 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_54MB;
101 /* We may add an option for custom rates that specific HW might
102 * support */
103 *tag_p = tag;
106 void rtllib_WMM_Info(struct rtllib_device *ieee, u8 **tag_p)
108 u8 *tag = *tag_p;
110 *tag++ = MFIE_TYPE_GENERIC;
111 *tag++ = 7;
112 *tag++ = 0x00;
113 *tag++ = 0x50;
114 *tag++ = 0xf2;
115 *tag++ = 0x02;
116 *tag++ = 0x00;
117 *tag++ = 0x01;
118 *tag++ = MAX_SP_Len;
119 *tag_p = tag;
122 void rtllib_TURBO_Info(struct rtllib_device *ieee, u8 **tag_p)
124 u8 *tag = *tag_p;
126 *tag++ = MFIE_TYPE_GENERIC;
127 *tag++ = 7;
128 *tag++ = 0x00;
129 *tag++ = 0xe0;
130 *tag++ = 0x4c;
131 *tag++ = 0x01;
132 *tag++ = 0x02;
133 *tag++ = 0x11;
134 *tag++ = 0x00;
136 *tag_p = tag;
137 printk(KERN_ALERT "This is enable turbo mode IE process\n");
140 void enqueue_mgmt(struct rtllib_device *ieee, struct sk_buff *skb)
142 int nh;
143 nh = (ieee->mgmt_queue_head + 1) % MGMT_QUEUE_NUM;
146 * if the queue is full but we have newer frames then
147 * just overwrites the oldest.
149 * if (nh == ieee->mgmt_queue_tail)
150 * return -1;
152 ieee->mgmt_queue_head = nh;
153 ieee->mgmt_queue_ring[nh] = skb;
157 struct sk_buff *dequeue_mgmt(struct rtllib_device *ieee)
159 struct sk_buff *ret;
161 if (ieee->mgmt_queue_tail == ieee->mgmt_queue_head)
162 return NULL;
164 ret = ieee->mgmt_queue_ring[ieee->mgmt_queue_tail];
166 ieee->mgmt_queue_tail =
167 (ieee->mgmt_queue_tail+1) % MGMT_QUEUE_NUM;
169 return ret;
172 void init_mgmt_queue(struct rtllib_device *ieee)
174 ieee->mgmt_queue_tail = ieee->mgmt_queue_head = 0;
179 MgntQuery_TxRateExcludeCCKRates(struct rtllib_device *ieee)
181 u16 i;
182 u8 QueryRate = 0;
183 u8 BasicRate;
186 for (i = 0; i < ieee->current_network.rates_len; i++) {
187 BasicRate = ieee->current_network.rates[i]&0x7F;
188 if (!rtllib_is_cck_rate(BasicRate)) {
189 if (QueryRate == 0) {
190 QueryRate = BasicRate;
191 } else {
192 if (BasicRate < QueryRate)
193 QueryRate = BasicRate;
198 if (QueryRate == 0) {
199 QueryRate = 12;
200 printk(KERN_INFO "No BasicRate found!!\n");
202 return QueryRate;
205 u8 MgntQuery_MgntFrameTxRate(struct rtllib_device *ieee)
207 struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
208 u8 rate;
210 if (pHTInfo->IOTAction & HT_IOT_ACT_MGNT_USE_CCK_6M)
211 rate = 0x0c;
212 else
213 rate = ieee->basic_rate & 0x7f;
215 if (rate == 0) {
216 if (ieee->mode == IEEE_A ||
217 ieee->mode == IEEE_N_5G ||
218 (ieee->mode == IEEE_N_24G && !pHTInfo->bCurSuppCCK))
219 rate = 0x0c;
220 else
221 rate = 0x02;
224 return rate;
227 inline void softmac_mgmt_xmit(struct sk_buff *skb, struct rtllib_device *ieee)
229 unsigned long flags;
230 short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
231 struct rtllib_hdr_3addr *header =
232 (struct rtllib_hdr_3addr *) skb->data;
234 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 8);
235 spin_lock_irqsave(&ieee->lock, flags);
237 /* called with 2nd param 0, no mgmt lock required */
238 rtllib_sta_wakeup(ieee, 0);
240 if (header->frame_ctl == RTLLIB_STYPE_BEACON)
241 tcb_desc->queue_index = BEACON_QUEUE;
242 else
243 tcb_desc->queue_index = MGNT_QUEUE;
245 if (ieee->disable_mgnt_queue)
246 tcb_desc->queue_index = HIGH_QUEUE;
248 tcb_desc->data_rate = MgntQuery_MgntFrameTxRate(ieee);
249 tcb_desc->RATRIndex = 7;
250 tcb_desc->bTxDisableRateFallBack = 1;
251 tcb_desc->bTxUseDriverAssingedRate = 1;
252 if (single) {
253 if (ieee->queue_stop) {
254 enqueue_mgmt(ieee, skb);
255 } else {
256 header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0]<<4);
258 if (ieee->seq_ctrl[0] == 0xFFF)
259 ieee->seq_ctrl[0] = 0;
260 else
261 ieee->seq_ctrl[0]++;
263 /* avoid watchdog triggers */
264 ieee->softmac_data_hard_start_xmit(skb, ieee->dev,
265 ieee->basic_rate);
268 spin_unlock_irqrestore(&ieee->lock, flags);
269 } else {
270 spin_unlock_irqrestore(&ieee->lock, flags);
271 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags);
273 header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
275 if (ieee->seq_ctrl[0] == 0xFFF)
276 ieee->seq_ctrl[0] = 0;
277 else
278 ieee->seq_ctrl[0]++;
280 /* check wether the managed packet queued greater than 5 */
281 if (!ieee->check_nic_enough_desc(ieee->dev, tcb_desc->queue_index) ||
282 (skb_queue_len(&ieee->skb_waitQ[tcb_desc->queue_index]) != 0) ||
283 (ieee->queue_stop)) {
284 /* insert the skb packet to the management queue */
285 /* as for the completion function, it does not need
286 * to check it any more.
287 * */
288 printk(KERN_INFO "%s():insert to waitqueue, queue_index"
289 ":%d!\n", __func__, tcb_desc->queue_index);
290 skb_queue_tail(&ieee->skb_waitQ[tcb_desc->queue_index],
291 skb);
292 } else {
293 ieee->softmac_hard_start_xmit(skb, ieee->dev);
295 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags);
299 inline void softmac_ps_mgmt_xmit(struct sk_buff *skb,
300 struct rtllib_device *ieee)
302 short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
303 struct rtllib_hdr_3addr *header =
304 (struct rtllib_hdr_3addr *) skb->data;
305 u16 fc, type, stype;
306 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 8);
308 fc = header->frame_ctl;
309 type = WLAN_FC_GET_TYPE(fc);
310 stype = WLAN_FC_GET_STYPE(fc);
313 if (stype != RTLLIB_STYPE_PSPOLL)
314 tcb_desc->queue_index = MGNT_QUEUE;
315 else
316 tcb_desc->queue_index = HIGH_QUEUE;
318 if (ieee->disable_mgnt_queue)
319 tcb_desc->queue_index = HIGH_QUEUE;
322 tcb_desc->data_rate = MgntQuery_MgntFrameTxRate(ieee);
323 tcb_desc->RATRIndex = 7;
324 tcb_desc->bTxDisableRateFallBack = 1;
325 tcb_desc->bTxUseDriverAssingedRate = 1;
326 if (single) {
327 if (type != RTLLIB_FTYPE_CTL) {
328 header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
330 if (ieee->seq_ctrl[0] == 0xFFF)
331 ieee->seq_ctrl[0] = 0;
332 else
333 ieee->seq_ctrl[0]++;
336 /* avoid watchdog triggers */
337 ieee->softmac_data_hard_start_xmit(skb, ieee->dev,
338 ieee->basic_rate);
340 } else {
341 if (type != RTLLIB_FTYPE_CTL) {
342 header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
344 if (ieee->seq_ctrl[0] == 0xFFF)
345 ieee->seq_ctrl[0] = 0;
346 else
347 ieee->seq_ctrl[0]++;
349 ieee->softmac_hard_start_xmit(skb, ieee->dev);
354 inline struct sk_buff *rtllib_probe_req(struct rtllib_device *ieee)
356 unsigned int len, rate_len;
357 u8 *tag;
358 struct sk_buff *skb;
359 struct rtllib_probe_request *req;
361 len = ieee->current_network.ssid_len;
363 rate_len = rtllib_MFIE_rate_len(ieee);
365 skb = dev_alloc_skb(sizeof(struct rtllib_probe_request) +
366 2 + len + rate_len + ieee->tx_headroom);
368 if (!skb)
369 return NULL;
371 skb_reserve(skb, ieee->tx_headroom);
373 req = (struct rtllib_probe_request *) skb_put(skb,
374 sizeof(struct rtllib_probe_request));
375 req->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_PROBE_REQ);
376 req->header.duration_id = 0;
378 memset(req->header.addr1, 0xff, ETH_ALEN);
379 memcpy(req->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
380 memset(req->header.addr3, 0xff, ETH_ALEN);
382 tag = (u8 *) skb_put(skb, len + 2 + rate_len);
384 *tag++ = MFIE_TYPE_SSID;
385 *tag++ = len;
386 memcpy(tag, ieee->current_network.ssid, len);
387 tag += len;
389 rtllib_MFIE_Brate(ieee, &tag);
390 rtllib_MFIE_Grate(ieee, &tag);
392 return skb;
395 struct sk_buff *rtllib_get_beacon_(struct rtllib_device *ieee);
397 void rtllib_send_beacon(struct rtllib_device *ieee)
399 struct sk_buff *skb;
400 if (!ieee->ieee_up)
401 return;
402 skb = rtllib_get_beacon_(ieee);
404 if (skb) {
405 softmac_mgmt_xmit(skb, ieee);
406 ieee->softmac_stats.tx_beacons++;
409 if (ieee->beacon_txing && ieee->ieee_up)
410 mod_timer(&ieee->beacon_timer, jiffies +
411 (MSECS(ieee->current_network.beacon_interval - 5)));
415 void rtllib_send_beacon_cb(unsigned long _ieee)
417 struct rtllib_device *ieee =
418 (struct rtllib_device *) _ieee;
419 unsigned long flags;
421 spin_lock_irqsave(&ieee->beacon_lock, flags);
422 rtllib_send_beacon(ieee);
423 spin_unlock_irqrestore(&ieee->beacon_lock, flags);
427 * Description:
428 * Enable network monitor mode, all rx packets will be received.
430 void rtllib_EnableNetMonitorMode(struct net_device *dev,
431 bool bInitState)
433 struct rtllib_device *ieee = netdev_priv_rsl(dev);
435 printk(KERN_INFO "========>Enter Monitor Mode\n");
437 ieee->AllowAllDestAddrHandler(dev, true, !bInitState);
442 * Description:
443 * Disable network network monitor mode, only packets destinated to
444 * us will be received.
446 void rtllib_DisableNetMonitorMode(struct net_device *dev,
447 bool bInitState)
449 struct rtllib_device *ieee = netdev_priv_rsl(dev);
451 printk(KERN_INFO "========>Exit Monitor Mode\n");
453 ieee->AllowAllDestAddrHandler(dev, false, !bInitState);
458 * Description:
459 * This enables the specialized promiscuous mode required by Intel.
460 * In this mode, Intel intends to hear traffics from/to other STAs in the
461 * same BSS. Therefore we don't have to disable checking BSSID and we only need
462 * to allow all dest. BUT: if we enable checking BSSID then we can't recv
463 * packets from other STA.
465 void rtllib_EnableIntelPromiscuousMode(struct net_device *dev,
466 bool bInitState)
468 bool bFilterOutNonAssociatedBSSID = false;
470 struct rtllib_device *ieee = netdev_priv_rsl(dev);
472 printk(KERN_INFO "========>Enter Intel Promiscuous Mode\n");
474 ieee->AllowAllDestAddrHandler(dev, true, !bInitState);
475 ieee->SetHwRegHandler(dev, HW_VAR_CECHK_BSSID,
476 (u8 *)&bFilterOutNonAssociatedBSSID);
478 ieee->bNetPromiscuousMode = true;
483 * Description:
484 * This disables the specialized promiscuous mode required by Intel.
485 * See MgntEnableIntelPromiscuousMode for detail.
487 void rtllib_DisableIntelPromiscuousMode(struct net_device *dev,
488 bool bInitState)
490 bool bFilterOutNonAssociatedBSSID = true;
492 struct rtllib_device *ieee = netdev_priv_rsl(dev);
494 printk(KERN_INFO "========>Exit Intel Promiscuous Mode\n");
496 ieee->AllowAllDestAddrHandler(dev, false, !bInitState);
497 ieee->SetHwRegHandler(dev, HW_VAR_CECHK_BSSID,
498 (u8 *)&bFilterOutNonAssociatedBSSID);
500 ieee->bNetPromiscuousMode = false;
503 void rtllib_send_probe(struct rtllib_device *ieee, u8 is_mesh)
505 struct sk_buff *skb;
506 skb = rtllib_probe_req(ieee);
507 if (skb) {
508 softmac_mgmt_xmit(skb, ieee);
509 ieee->softmac_stats.tx_probe_rq++;
514 void rtllib_send_probe_requests(struct rtllib_device *ieee, u8 is_mesh)
516 if (ieee->active_scan && (ieee->softmac_features &
517 IEEE_SOFTMAC_PROBERQ)) {
518 rtllib_send_probe(ieee, 0);
519 rtllib_send_probe(ieee, 0);
523 void rtllib_softmac_hint11d_wq(void *data)
527 void rtllib_update_active_chan_map(struct rtllib_device *ieee)
529 memcpy(ieee->active_channel_map, GET_DOT11D_INFO(ieee)->channel_map,
530 MAX_CHANNEL_NUMBER+1);
533 /* this performs syncro scan blocking the caller until all channels
534 * in the allowed channel map has been checked.
536 void rtllib_softmac_scan_syncro(struct rtllib_device *ieee, u8 is_mesh)
538 union iwreq_data wrqu;
539 short ch = 0;
541 rtllib_update_active_chan_map(ieee);
543 ieee->be_scan_inprogress = true;
545 down(&ieee->scan_sem);
547 while (1) {
548 do {
549 ch++;
550 if (ch > MAX_CHANNEL_NUMBER)
551 goto out; /* scan completed */
552 } while (!ieee->active_channel_map[ch]);
554 /* this fuction can be called in two situations
555 * 1- We have switched to ad-hoc mode and we are
556 * performing a complete syncro scan before conclude
557 * there are no interesting cell and to create a
558 * new one. In this case the link state is
559 * RTLLIB_NOLINK until we found an interesting cell.
560 * If so the ieee8021_new_net, called by the RX path
561 * will set the state to RTLLIB_LINKED, so we stop
562 * scanning
563 * 2- We are linked and the root uses run iwlist scan.
564 * So we switch to RTLLIB_LINKED_SCANNING to remember
565 * that we are still logically linked (not interested in
566 * new network events, despite for updating the net list,
567 * but we are temporarly 'unlinked' as the driver shall
568 * not filter RX frames and the channel is changing.
569 * So the only situation in witch are interested is to check
570 * if the state become LINKED because of the #1 situation
573 if (ieee->state == RTLLIB_LINKED)
574 goto out;
575 if (ieee->sync_scan_hurryup) {
576 printk(KERN_INFO "============>sync_scan_hurryup out\n");
577 goto out;
580 ieee->set_chan(ieee->dev, ch);
581 if (ieee->active_channel_map[ch] == 1)
582 rtllib_send_probe_requests(ieee, 0);
584 /* this prevent excessive time wait when we
585 * need to wait for a syncro scan to end..
587 msleep_interruptible_rsl(RTLLIB_SOFTMAC_SCAN_TIME);
589 out:
590 ieee->actscanning = false;
591 ieee->sync_scan_hurryup = 0;
593 if (ieee->state >= RTLLIB_LINKED) {
594 if (IS_DOT11D_ENABLE(ieee))
595 DOT11D_ScanComplete(ieee);
597 up(&ieee->scan_sem);
599 ieee->be_scan_inprogress = false;
601 memset(&wrqu, 0, sizeof(wrqu));
602 wireless_send_event(ieee->dev, SIOCGIWSCAN, &wrqu, NULL);
605 void rtllib_softmac_scan_wq(void *data)
607 struct rtllib_device *ieee = container_of_dwork_rsl(data,
608 struct rtllib_device, softmac_scan_wq);
609 u8 last_channel = ieee->current_network.channel;
611 rtllib_update_active_chan_map(ieee);
613 if (!ieee->ieee_up)
614 return;
615 if (rtllib_act_scanning(ieee, true) == true)
616 return;
618 down(&ieee->scan_sem);
620 if (ieee->eRFPowerState == eRfOff) {
621 printk(KERN_INFO "======>%s():rf state is eRfOff, return\n",
622 __func__);
623 goto out1;
626 do {
627 ieee->current_network.channel =
628 (ieee->current_network.channel + 1) %
629 MAX_CHANNEL_NUMBER;
630 if (ieee->scan_watch_dog++ > MAX_CHANNEL_NUMBER) {
631 if (!ieee->active_channel_map[ieee->current_network.channel])
632 ieee->current_network.channel = 6;
633 goto out; /* no good chans */
635 } while (!ieee->active_channel_map[ieee->current_network.channel]);
637 if (ieee->scanning_continue == 0)
638 goto out;
640 ieee->set_chan(ieee->dev, ieee->current_network.channel);
642 if (ieee->active_channel_map[ieee->current_network.channel] == 1)
643 rtllib_send_probe_requests(ieee, 0);
645 queue_delayed_work_rsl(ieee->wq, &ieee->softmac_scan_wq,
646 MSECS(RTLLIB_SOFTMAC_SCAN_TIME));
648 up(&ieee->scan_sem);
649 return;
651 out:
652 if (IS_DOT11D_ENABLE(ieee))
653 DOT11D_ScanComplete(ieee);
654 ieee->current_network.channel = last_channel;
656 out1:
657 ieee->actscanning = false;
658 ieee->scan_watch_dog = 0;
659 ieee->scanning_continue = 0;
660 up(&ieee->scan_sem);
665 void rtllib_beacons_start(struct rtllib_device *ieee)
667 unsigned long flags;
668 spin_lock_irqsave(&ieee->beacon_lock, flags);
670 ieee->beacon_txing = 1;
671 rtllib_send_beacon(ieee);
673 spin_unlock_irqrestore(&ieee->beacon_lock, flags);
676 void rtllib_beacons_stop(struct rtllib_device *ieee)
678 unsigned long flags;
680 spin_lock_irqsave(&ieee->beacon_lock, flags);
682 ieee->beacon_txing = 0;
683 del_timer_sync(&ieee->beacon_timer);
685 spin_unlock_irqrestore(&ieee->beacon_lock, flags);
690 void rtllib_stop_send_beacons(struct rtllib_device *ieee)
692 if (ieee->stop_send_beacons)
693 ieee->stop_send_beacons(ieee->dev);
694 if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
695 rtllib_beacons_stop(ieee);
699 void rtllib_start_send_beacons(struct rtllib_device *ieee)
701 if (ieee->start_send_beacons)
702 ieee->start_send_beacons(ieee->dev);
703 if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
704 rtllib_beacons_start(ieee);
708 void rtllib_softmac_stop_scan(struct rtllib_device *ieee)
710 down(&ieee->scan_sem);
711 ieee->scan_watch_dog = 0;
712 if (ieee->scanning_continue == 1) {
713 ieee->scanning_continue = 0;
714 ieee->actscanning = 0;
716 cancel_delayed_work(&ieee->softmac_scan_wq);
719 up(&ieee->scan_sem);
722 void rtllib_stop_scan(struct rtllib_device *ieee)
724 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
725 rtllib_softmac_stop_scan(ieee);
726 } else {
727 if (ieee->rtllib_stop_hw_scan)
728 ieee->rtllib_stop_hw_scan(ieee->dev);
732 void rtllib_stop_scan_syncro(struct rtllib_device *ieee)
734 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
735 ieee->sync_scan_hurryup = 1;
736 } else {
737 if (ieee->rtllib_stop_hw_scan)
738 ieee->rtllib_stop_hw_scan(ieee->dev);
742 bool rtllib_act_scanning(struct rtllib_device *ieee, bool sync_scan)
744 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
745 if (sync_scan)
746 return ieee->be_scan_inprogress;
747 else
748 return ieee->actscanning || ieee->be_scan_inprogress;
749 } else {
750 return test_bit(STATUS_SCANNING, &ieee->status);
754 /* called with ieee->lock held */
755 void rtllib_start_scan(struct rtllib_device *ieee)
757 RT_TRACE(COMP_DBG, "===>%s()\n", __func__);
758 if (ieee->rtllib_ips_leave_wq != NULL)
759 ieee->rtllib_ips_leave_wq(ieee->dev);
761 if (IS_DOT11D_ENABLE(ieee)) {
762 if (IS_COUNTRY_IE_VALID(ieee))
763 RESET_CIE_WATCHDOG(ieee);
765 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
766 if (ieee->scanning_continue == 0) {
767 ieee->actscanning = true;
768 ieee->scanning_continue = 1;
769 queue_delayed_work_rsl(ieee->wq,
770 &ieee->softmac_scan_wq, 0);
772 } else {
773 if (ieee->rtllib_start_hw_scan)
774 ieee->rtllib_start_hw_scan(ieee->dev);
778 /* called with wx_sem held */
779 void rtllib_start_scan_syncro(struct rtllib_device *ieee, u8 is_mesh)
781 if (IS_DOT11D_ENABLE(ieee)) {
782 if (IS_COUNTRY_IE_VALID(ieee))
783 RESET_CIE_WATCHDOG(ieee);
785 ieee->sync_scan_hurryup = 0;
786 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
787 rtllib_softmac_scan_syncro(ieee, is_mesh);
788 } else {
789 if (ieee->rtllib_start_hw_scan)
790 ieee->rtllib_start_hw_scan(ieee->dev);
794 inline struct sk_buff *rtllib_authentication_req(struct rtllib_network *beacon,
795 struct rtllib_device *ieee, int challengelen, u8 *daddr)
797 struct sk_buff *skb;
798 struct rtllib_authentication *auth;
799 int len = 0;
800 len = sizeof(struct rtllib_authentication) + challengelen +
801 ieee->tx_headroom + 4;
802 skb = dev_alloc_skb(len);
804 if (!skb)
805 return NULL;
807 skb_reserve(skb, ieee->tx_headroom);
809 auth = (struct rtllib_authentication *)
810 skb_put(skb, sizeof(struct rtllib_authentication));
812 auth->header.frame_ctl = RTLLIB_STYPE_AUTH;
813 if (challengelen)
814 auth->header.frame_ctl |= RTLLIB_FCTL_WEP;
816 auth->header.duration_id = 0x013a;
817 memcpy(auth->header.addr1, beacon->bssid, ETH_ALEN);
818 memcpy(auth->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
819 memcpy(auth->header.addr3, beacon->bssid, ETH_ALEN);
820 if (ieee->auth_mode == 0)
821 auth->algorithm = WLAN_AUTH_OPEN;
822 else if (ieee->auth_mode == 1)
823 auth->algorithm = WLAN_AUTH_SHARED_KEY;
824 else if (ieee->auth_mode == 2)
825 auth->algorithm = WLAN_AUTH_OPEN;
826 auth->transaction = cpu_to_le16(ieee->associate_seq);
827 ieee->associate_seq++;
829 auth->status = cpu_to_le16(WLAN_STATUS_SUCCESS);
831 return skb;
834 void constructWMMIE(u8 *wmmie, u8 *wmm_len, u8 oui_subtype)
836 u8 szQoSOUI[] = {221, 0, 0x00, 0x50, 0xf2, 0x02, 0, 1};
838 if (oui_subtype == OUI_SUBTYPE_QOS_CAPABI) {
839 szQoSOUI[0] = 46;
840 szQoSOUI[1] = *wmm_len;
841 memcpy(wmmie, szQoSOUI, 3);
842 *wmm_len = 3;
843 } else {
844 szQoSOUI[1] = *wmm_len + 6;
845 szQoSOUI[6] = oui_subtype;
846 memcpy(wmmie, szQoSOUI, 8);
847 *(wmmie+8) = 0;
848 *wmm_len = 9;
852 static struct sk_buff *rtllib_probe_resp(struct rtllib_device *ieee, u8 *dest)
854 u8 *tag;
855 int beacon_size;
856 struct rtllib_probe_response *beacon_buf;
857 struct sk_buff *skb = NULL;
858 int encrypt;
859 int atim_len, erp_len;
860 struct rtllib_crypt_data *crypt;
862 char *ssid = ieee->current_network.ssid;
863 int ssid_len = ieee->current_network.ssid_len;
864 int rate_len = ieee->current_network.rates_len+2;
865 int rate_ex_len = ieee->current_network.rates_ex_len;
866 int wpa_ie_len = ieee->wpa_ie_len;
867 u8 erpinfo_content = 0;
869 u8 *tmp_ht_cap_buf = NULL;
870 u8 tmp_ht_cap_len = 0;
871 u8 *tmp_ht_info_buf = NULL;
872 u8 tmp_ht_info_len = 0;
873 struct rt_hi_throughput *pHTInfo = ieee->pHTInfo;
874 u8 *tmp_generic_ie_buf = NULL;
875 u8 tmp_generic_ie_len = 0;
877 if (rate_ex_len > 0)
878 rate_ex_len += 2;
880 if (ieee->current_network.capability & WLAN_CAPABILITY_IBSS)
881 atim_len = 4;
882 else
883 atim_len = 0;
885 if ((ieee->current_network.mode == IEEE_G) ||
886 (ieee->current_network.mode == IEEE_N_24G &&
887 ieee->pHTInfo->bCurSuppCCK)) {
888 erp_len = 3;
889 erpinfo_content = 0;
890 if (ieee->current_network.buseprotection)
891 erpinfo_content |= ERP_UseProtection;
892 } else
893 erp_len = 0;
895 crypt = ieee->crypt[ieee->tx_keyidx];
896 encrypt = ieee->host_encrypt && crypt && crypt->ops &&
897 ((0 == strcmp(crypt->ops->name, "WEP") || wpa_ie_len));
898 if (ieee->pHTInfo->bCurrentHTSupport) {
899 tmp_ht_cap_buf = (u8 *) &(ieee->pHTInfo->SelfHTCap);
900 tmp_ht_cap_len = sizeof(ieee->pHTInfo->SelfHTCap);
901 tmp_ht_info_buf = (u8 *) &(ieee->pHTInfo->SelfHTInfo);
902 tmp_ht_info_len = sizeof(ieee->pHTInfo->SelfHTInfo);
903 HTConstructCapabilityElement(ieee, tmp_ht_cap_buf,
904 &tmp_ht_cap_len, encrypt, false);
905 HTConstructInfoElement(ieee, tmp_ht_info_buf, &tmp_ht_info_len,
906 encrypt);
908 if (pHTInfo->bRegRT2RTAggregation) {
909 tmp_generic_ie_buf = ieee->pHTInfo->szRT2RTAggBuffer;
910 tmp_generic_ie_len =
911 sizeof(ieee->pHTInfo->szRT2RTAggBuffer);
912 HTConstructRT2RTAggElement(ieee, tmp_generic_ie_buf,
913 &tmp_generic_ie_len);
917 beacon_size = sizeof(struct rtllib_probe_response)+2+
918 ssid_len + 3 + rate_len + rate_ex_len + atim_len + erp_len
919 + wpa_ie_len + ieee->tx_headroom;
920 skb = dev_alloc_skb(beacon_size);
921 if (!skb)
922 return NULL;
924 skb_reserve(skb, ieee->tx_headroom);
926 beacon_buf = (struct rtllib_probe_response *) skb_put(skb,
927 (beacon_size - ieee->tx_headroom));
928 memcpy(beacon_buf->header.addr1, dest, ETH_ALEN);
929 memcpy(beacon_buf->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
930 memcpy(beacon_buf->header.addr3, ieee->current_network.bssid, ETH_ALEN);
932 beacon_buf->header.duration_id = 0;
933 beacon_buf->beacon_interval =
934 cpu_to_le16(ieee->current_network.beacon_interval);
935 beacon_buf->capability =
936 cpu_to_le16(ieee->current_network.capability &
937 WLAN_CAPABILITY_IBSS);
938 beacon_buf->capability |=
939 cpu_to_le16(ieee->current_network.capability &
940 WLAN_CAPABILITY_SHORT_PREAMBLE);
942 if (ieee->short_slot && (ieee->current_network.capability &
943 WLAN_CAPABILITY_SHORT_SLOT_TIME))
944 cpu_to_le16((beacon_buf->capability |=
945 WLAN_CAPABILITY_SHORT_SLOT_TIME));
947 crypt = ieee->crypt[ieee->tx_keyidx];
948 if (encrypt)
949 beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
952 beacon_buf->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_PROBE_RESP);
953 beacon_buf->info_element[0].id = MFIE_TYPE_SSID;
954 beacon_buf->info_element[0].len = ssid_len;
956 tag = (u8 *) beacon_buf->info_element[0].data;
958 memcpy(tag, ssid, ssid_len);
960 tag += ssid_len;
962 *(tag++) = MFIE_TYPE_RATES;
963 *(tag++) = rate_len-2;
964 memcpy(tag, ieee->current_network.rates, rate_len-2);
965 tag += rate_len-2;
967 *(tag++) = MFIE_TYPE_DS_SET;
968 *(tag++) = 1;
969 *(tag++) = ieee->current_network.channel;
971 if (atim_len) {
972 u16 val16;
973 *(tag++) = MFIE_TYPE_IBSS_SET;
974 *(tag++) = 2;
975 val16 = cpu_to_le16(ieee->current_network.atim_window);
976 memcpy((u8 *)tag, (u8 *)&val16, 2);
977 tag += 2;
980 if (erp_len) {
981 *(tag++) = MFIE_TYPE_ERP;
982 *(tag++) = 1;
983 *(tag++) = erpinfo_content;
985 if (rate_ex_len) {
986 *(tag++) = MFIE_TYPE_RATES_EX;
987 *(tag++) = rate_ex_len-2;
988 memcpy(tag, ieee->current_network.rates_ex, rate_ex_len-2);
989 tag += rate_ex_len-2;
992 if (wpa_ie_len) {
993 if (ieee->iw_mode == IW_MODE_ADHOC)
994 memcpy(&ieee->wpa_ie[14], &ieee->wpa_ie[8], 4);
995 memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
996 tag += ieee->wpa_ie_len;
998 return skb;
1001 struct sk_buff *rtllib_assoc_resp(struct rtllib_device *ieee, u8 *dest)
1003 struct sk_buff *skb;
1004 u8 *tag;
1006 struct rtllib_crypt_data *crypt;
1007 struct rtllib_assoc_response_frame *assoc;
1008 short encrypt;
1010 unsigned int rate_len = rtllib_MFIE_rate_len(ieee);
1011 int len = sizeof(struct rtllib_assoc_response_frame) + rate_len +
1012 ieee->tx_headroom;
1014 skb = dev_alloc_skb(len);
1016 if (!skb)
1017 return NULL;
1019 skb_reserve(skb, ieee->tx_headroom);
1021 assoc = (struct rtllib_assoc_response_frame *)
1022 skb_put(skb, sizeof(struct rtllib_assoc_response_frame));
1024 assoc->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_ASSOC_RESP);
1025 memcpy(assoc->header.addr1, dest, ETH_ALEN);
1026 memcpy(assoc->header.addr3, ieee->dev->dev_addr, ETH_ALEN);
1027 memcpy(assoc->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
1028 assoc->capability = cpu_to_le16(ieee->iw_mode == IW_MODE_MASTER ?
1029 WLAN_CAPABILITY_ESS : WLAN_CAPABILITY_IBSS);
1032 if (ieee->short_slot)
1033 assoc->capability |=
1034 cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
1036 if (ieee->host_encrypt)
1037 crypt = ieee->crypt[ieee->tx_keyidx];
1038 else
1039 crypt = NULL;
1041 encrypt = (crypt && crypt->ops);
1043 if (encrypt)
1044 assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1046 assoc->status = 0;
1047 assoc->aid = cpu_to_le16(ieee->assoc_id);
1048 if (ieee->assoc_id == 0x2007)
1049 ieee->assoc_id = 0;
1050 else
1051 ieee->assoc_id++;
1053 tag = (u8 *) skb_put(skb, rate_len);
1054 rtllib_MFIE_Brate(ieee, &tag);
1055 rtllib_MFIE_Grate(ieee, &tag);
1057 return skb;
1060 struct sk_buff *rtllib_auth_resp(struct rtllib_device *ieee, int status,
1061 u8 *dest)
1063 struct sk_buff *skb = NULL;
1064 struct rtllib_authentication *auth;
1065 int len = ieee->tx_headroom + sizeof(struct rtllib_authentication) + 1;
1066 skb = dev_alloc_skb(len);
1067 if (!skb)
1068 return NULL;
1070 skb->len = sizeof(struct rtllib_authentication);
1072 skb_reserve(skb, ieee->tx_headroom);
1074 auth = (struct rtllib_authentication *)
1075 skb_put(skb, sizeof(struct rtllib_authentication));
1077 auth->status = cpu_to_le16(status);
1078 auth->transaction = cpu_to_le16(2);
1079 auth->algorithm = cpu_to_le16(WLAN_AUTH_OPEN);
1081 memcpy(auth->header.addr3, ieee->dev->dev_addr, ETH_ALEN);
1082 memcpy(auth->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
1083 memcpy(auth->header.addr1, dest, ETH_ALEN);
1084 auth->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_AUTH);
1085 return skb;
1090 struct sk_buff *rtllib_null_func(struct rtllib_device *ieee, short pwr)
1092 struct sk_buff *skb;
1093 struct rtllib_hdr_3addr *hdr;
1095 skb = dev_alloc_skb(sizeof(struct rtllib_hdr_3addr)+ieee->tx_headroom);
1096 if (!skb)
1097 return NULL;
1099 skb_reserve(skb, ieee->tx_headroom);
1101 hdr = (struct rtllib_hdr_3addr *)skb_put(skb,
1102 sizeof(struct rtllib_hdr_3addr));
1104 memcpy(hdr->addr1, ieee->current_network.bssid, ETH_ALEN);
1105 memcpy(hdr->addr2, ieee->dev->dev_addr, ETH_ALEN);
1106 memcpy(hdr->addr3, ieee->current_network.bssid, ETH_ALEN);
1108 hdr->frame_ctl = cpu_to_le16(RTLLIB_FTYPE_DATA |
1109 RTLLIB_STYPE_NULLFUNC | RTLLIB_FCTL_TODS |
1110 (pwr ? RTLLIB_FCTL_PM : 0));
1112 return skb;
1117 struct sk_buff *rtllib_pspoll_func(struct rtllib_device *ieee)
1119 struct sk_buff *skb;
1120 struct rtllib_pspoll_hdr *hdr;
1122 skb = dev_alloc_skb(sizeof(struct rtllib_pspoll_hdr)+ieee->tx_headroom);
1123 if (!skb)
1124 return NULL;
1126 skb_reserve(skb, ieee->tx_headroom);
1128 hdr = (struct rtllib_pspoll_hdr *)skb_put(skb,
1129 sizeof(struct rtllib_pspoll_hdr));
1131 memcpy(hdr->bssid, ieee->current_network.bssid, ETH_ALEN);
1132 memcpy(hdr->ta, ieee->dev->dev_addr, ETH_ALEN);
1134 hdr->aid = cpu_to_le16(ieee->assoc_id | 0xc000);
1135 hdr->frame_ctl = cpu_to_le16(RTLLIB_FTYPE_CTL | RTLLIB_STYPE_PSPOLL |
1136 RTLLIB_FCTL_PM);
1138 return skb;
1142 void rtllib_resp_to_assoc_rq(struct rtllib_device *ieee, u8 *dest)
1144 struct sk_buff *buf = rtllib_assoc_resp(ieee, dest);
1146 if (buf)
1147 softmac_mgmt_xmit(buf, ieee);
1151 void rtllib_resp_to_auth(struct rtllib_device *ieee, int s, u8 *dest)
1153 struct sk_buff *buf = rtllib_auth_resp(ieee, s, dest);
1155 if (buf)
1156 softmac_mgmt_xmit(buf, ieee);
1160 void rtllib_resp_to_probe(struct rtllib_device *ieee, u8 *dest)
1163 struct sk_buff *buf = rtllib_probe_resp(ieee, dest);
1164 if (buf)
1165 softmac_mgmt_xmit(buf, ieee);
1169 inline int SecIsInPMKIDList(struct rtllib_device *ieee, u8 *bssid)
1171 int i = 0;
1173 do {
1174 if ((ieee->PMKIDList[i].bUsed) &&
1175 (memcmp(ieee->PMKIDList[i].Bssid, bssid, ETH_ALEN) == 0))
1176 break;
1177 else
1178 i++;
1179 } while (i < NUM_PMKID_CACHE);
1181 if (i == NUM_PMKID_CACHE)
1182 i = -1;
1183 return i;
1186 inline struct sk_buff *rtllib_association_req(struct rtllib_network *beacon,
1187 struct rtllib_device *ieee)
1189 struct sk_buff *skb;
1190 struct rtllib_assoc_request_frame *hdr;
1191 u8 *tag, *ies;
1192 int i;
1193 u8 *ht_cap_buf = NULL;
1194 u8 ht_cap_len = 0;
1195 u8 *realtek_ie_buf = NULL;
1196 u8 realtek_ie_len = 0;
1197 int wpa_ie_len = ieee->wpa_ie_len;
1198 int wps_ie_len = ieee->wps_ie_len;
1199 unsigned int ckip_ie_len = 0;
1200 unsigned int ccxrm_ie_len = 0;
1201 unsigned int cxvernum_ie_len = 0;
1202 struct rtllib_crypt_data *crypt;
1203 int encrypt;
1204 int PMKCacheIdx;
1206 unsigned int rate_len = (beacon->rates_len ?
1207 (beacon->rates_len + 2) : 0) +
1208 (beacon->rates_ex_len ? (beacon->rates_ex_len) +
1209 2 : 0);
1211 unsigned int wmm_info_len = beacon->qos_data.supported ? 9 : 0;
1212 unsigned int turbo_info_len = beacon->Turbo_Enable ? 9 : 0;
1214 int len = 0;
1215 crypt = ieee->crypt[ieee->tx_keyidx];
1216 if (crypt != NULL)
1217 encrypt = ieee->host_encrypt && crypt && crypt->ops &&
1218 ((0 == strcmp(crypt->ops->name, "WEP") ||
1219 wpa_ie_len));
1220 else
1221 encrypt = 0;
1223 if ((ieee->rtllib_ap_sec_type &&
1224 (ieee->rtllib_ap_sec_type(ieee) & SEC_ALG_TKIP)) ||
1225 (ieee->bForcedBgMode == true)) {
1226 ieee->pHTInfo->bEnableHT = 0;
1227 ieee->mode = WIRELESS_MODE_G;
1230 if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1231 ht_cap_buf = (u8 *)&(ieee->pHTInfo->SelfHTCap);
1232 ht_cap_len = sizeof(ieee->pHTInfo->SelfHTCap);
1233 HTConstructCapabilityElement(ieee, ht_cap_buf, &ht_cap_len,
1234 encrypt, true);
1235 if (ieee->pHTInfo->bCurrentRT2RTAggregation) {
1236 realtek_ie_buf = ieee->pHTInfo->szRT2RTAggBuffer;
1237 realtek_ie_len =
1238 sizeof(ieee->pHTInfo->szRT2RTAggBuffer);
1239 HTConstructRT2RTAggElement(ieee, realtek_ie_buf,
1240 &realtek_ie_len);
1244 if (beacon->bCkipSupported)
1245 ckip_ie_len = 30+2;
1246 if (beacon->bCcxRmEnable)
1247 ccxrm_ie_len = 6+2;
1248 if (beacon->BssCcxVerNumber >= 2)
1249 cxvernum_ie_len = 5+2;
1251 PMKCacheIdx = SecIsInPMKIDList(ieee, ieee->current_network.bssid);
1252 if (PMKCacheIdx >= 0) {
1253 wpa_ie_len += 18;
1254 printk(KERN_INFO "[PMK cache]: WPA2 IE length: %x\n",
1255 wpa_ie_len);
1257 len = sizeof(struct rtllib_assoc_request_frame) + 2
1258 + beacon->ssid_len
1259 + rate_len
1260 + wpa_ie_len
1261 + wps_ie_len
1262 + wmm_info_len
1263 + turbo_info_len
1264 + ht_cap_len
1265 + realtek_ie_len
1266 + ckip_ie_len
1267 + ccxrm_ie_len
1268 + cxvernum_ie_len
1269 + ieee->tx_headroom;
1271 skb = dev_alloc_skb(len);
1273 if (!skb)
1274 return NULL;
1276 skb_reserve(skb, ieee->tx_headroom);
1278 hdr = (struct rtllib_assoc_request_frame *)
1279 skb_put(skb, sizeof(struct rtllib_assoc_request_frame) + 2);
1282 hdr->header.frame_ctl = RTLLIB_STYPE_ASSOC_REQ;
1283 hdr->header.duration_id = 37;
1284 memcpy(hdr->header.addr1, beacon->bssid, ETH_ALEN);
1285 memcpy(hdr->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
1286 memcpy(hdr->header.addr3, beacon->bssid, ETH_ALEN);
1288 memcpy(ieee->ap_mac_addr, beacon->bssid, ETH_ALEN);
1290 hdr->capability = cpu_to_le16(WLAN_CAPABILITY_ESS);
1291 if (beacon->capability & WLAN_CAPABILITY_PRIVACY)
1292 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
1294 if (beacon->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1295 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
1297 if (ieee->short_slot &&
1298 (beacon->capability&WLAN_CAPABILITY_SHORT_SLOT_TIME))
1299 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
1302 hdr->listen_interval = beacon->listen_interval;
1304 hdr->info_element[0].id = MFIE_TYPE_SSID;
1306 hdr->info_element[0].len = beacon->ssid_len;
1307 tag = skb_put(skb, beacon->ssid_len);
1308 memcpy(tag, beacon->ssid, beacon->ssid_len);
1310 tag = skb_put(skb, rate_len);
1312 if (beacon->rates_len) {
1313 *tag++ = MFIE_TYPE_RATES;
1314 *tag++ = beacon->rates_len;
1315 for (i = 0; i < beacon->rates_len; i++)
1316 *tag++ = beacon->rates[i];
1319 if (beacon->rates_ex_len) {
1320 *tag++ = MFIE_TYPE_RATES_EX;
1321 *tag++ = beacon->rates_ex_len;
1322 for (i = 0; i < beacon->rates_ex_len; i++)
1323 *tag++ = beacon->rates_ex[i];
1326 if (beacon->bCkipSupported) {
1327 static u8 AironetIeOui[] = {0x00, 0x01, 0x66};
1328 u8 CcxAironetBuf[30];
1329 struct octet_string osCcxAironetIE;
1331 memset(CcxAironetBuf, 0, 30);
1332 osCcxAironetIE.Octet = CcxAironetBuf;
1333 osCcxAironetIE.Length = sizeof(CcxAironetBuf);
1334 memcpy(osCcxAironetIE.Octet, AironetIeOui,
1335 sizeof(AironetIeOui));
1337 osCcxAironetIE.Octet[IE_CISCO_FLAG_POSITION] |=
1338 (SUPPORT_CKIP_PK|SUPPORT_CKIP_MIC);
1339 tag = skb_put(skb, ckip_ie_len);
1340 *tag++ = MFIE_TYPE_AIRONET;
1341 *tag++ = osCcxAironetIE.Length;
1342 memcpy(tag, osCcxAironetIE.Octet, osCcxAironetIE.Length);
1343 tag += osCcxAironetIE.Length;
1346 if (beacon->bCcxRmEnable) {
1347 static u8 CcxRmCapBuf[] = {0x00, 0x40, 0x96, 0x01, 0x01, 0x00};
1348 struct octet_string osCcxRmCap;
1350 osCcxRmCap.Octet = CcxRmCapBuf;
1351 osCcxRmCap.Length = sizeof(CcxRmCapBuf);
1352 tag = skb_put(skb, ccxrm_ie_len);
1353 *tag++ = MFIE_TYPE_GENERIC;
1354 *tag++ = osCcxRmCap.Length;
1355 memcpy(tag, osCcxRmCap.Octet, osCcxRmCap.Length);
1356 tag += osCcxRmCap.Length;
1359 if (beacon->BssCcxVerNumber >= 2) {
1360 u8 CcxVerNumBuf[] = {0x00, 0x40, 0x96, 0x03, 0x00};
1361 struct octet_string osCcxVerNum;
1362 CcxVerNumBuf[4] = beacon->BssCcxVerNumber;
1363 osCcxVerNum.Octet = CcxVerNumBuf;
1364 osCcxVerNum.Length = sizeof(CcxVerNumBuf);
1365 tag = skb_put(skb, cxvernum_ie_len);
1366 *tag++ = MFIE_TYPE_GENERIC;
1367 *tag++ = osCcxVerNum.Length;
1368 memcpy(tag, osCcxVerNum.Octet, osCcxVerNum.Length);
1369 tag += osCcxVerNum.Length;
1371 if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1372 if (ieee->pHTInfo->ePeerHTSpecVer != HT_SPEC_VER_EWC) {
1373 tag = skb_put(skb, ht_cap_len);
1374 *tag++ = MFIE_TYPE_HT_CAP;
1375 *tag++ = ht_cap_len - 2;
1376 memcpy(tag, ht_cap_buf, ht_cap_len - 2);
1377 tag += ht_cap_len - 2;
1381 if (wpa_ie_len) {
1382 tag = skb_put(skb, ieee->wpa_ie_len);
1383 memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
1385 if (PMKCacheIdx >= 0) {
1386 tag = skb_put(skb, 18);
1387 *tag = 1;
1388 *(tag + 1) = 0;
1389 memcpy((tag + 2), &ieee->PMKIDList[PMKCacheIdx].PMKID,
1390 16);
1393 if (wmm_info_len) {
1394 tag = skb_put(skb, wmm_info_len);
1395 rtllib_WMM_Info(ieee, &tag);
1398 if (wps_ie_len && ieee->wps_ie) {
1399 tag = skb_put(skb, wps_ie_len);
1400 memcpy(tag, ieee->wps_ie, wps_ie_len);
1403 tag = skb_put(skb, turbo_info_len);
1404 if (turbo_info_len)
1405 rtllib_TURBO_Info(ieee, &tag);
1407 if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1408 if (ieee->pHTInfo->ePeerHTSpecVer == HT_SPEC_VER_EWC) {
1409 tag = skb_put(skb, ht_cap_len);
1410 *tag++ = MFIE_TYPE_GENERIC;
1411 *tag++ = ht_cap_len - 2;
1412 memcpy(tag, ht_cap_buf, ht_cap_len - 2);
1413 tag += ht_cap_len - 2;
1416 if (ieee->pHTInfo->bCurrentRT2RTAggregation) {
1417 tag = skb_put(skb, realtek_ie_len);
1418 *tag++ = MFIE_TYPE_GENERIC;
1419 *tag++ = realtek_ie_len - 2;
1420 memcpy(tag, realtek_ie_buf, realtek_ie_len - 2);
1424 kfree(ieee->assocreq_ies);
1425 ieee->assocreq_ies = NULL;
1426 ies = &(hdr->info_element[0].id);
1427 ieee->assocreq_ies_len = (skb->data + skb->len) - ies;
1428 ieee->assocreq_ies = kmalloc(ieee->assocreq_ies_len, GFP_ATOMIC);
1429 if (ieee->assocreq_ies)
1430 memcpy(ieee->assocreq_ies, ies, ieee->assocreq_ies_len);
1431 else {
1432 printk(KERN_INFO "%s()Warning: can't alloc memory for assocreq"
1433 "_ies\n", __func__);
1434 ieee->assocreq_ies_len = 0;
1436 return skb;
1439 void rtllib_associate_abort(struct rtllib_device *ieee)
1442 unsigned long flags;
1443 spin_lock_irqsave(&ieee->lock, flags);
1445 ieee->associate_seq++;
1447 /* don't scan, and avoid to have the RX path possibily
1448 * try again to associate. Even do not react to AUTH or
1449 * ASSOC response. Just wait for the retry wq to be scheduled.
1450 * Here we will check if there are good nets to associate
1451 * with, so we retry or just get back to NO_LINK and scanning
1453 if (ieee->state == RTLLIB_ASSOCIATING_AUTHENTICATING) {
1454 RTLLIB_DEBUG_MGMT("Authentication failed\n");
1455 ieee->softmac_stats.no_auth_rs++;
1456 } else {
1457 RTLLIB_DEBUG_MGMT("Association failed\n");
1458 ieee->softmac_stats.no_ass_rs++;
1461 ieee->state = RTLLIB_ASSOCIATING_RETRY;
1463 queue_delayed_work_rsl(ieee->wq, &ieee->associate_retry_wq,
1464 RTLLIB_SOFTMAC_ASSOC_RETRY_TIME);
1466 spin_unlock_irqrestore(&ieee->lock, flags);
1469 void rtllib_associate_abort_cb(unsigned long dev)
1471 rtllib_associate_abort((struct rtllib_device *) dev);
1474 void rtllib_associate_step1(struct rtllib_device *ieee, u8 * daddr)
1476 struct rtllib_network *beacon = &ieee->current_network;
1477 struct sk_buff *skb;
1479 RTLLIB_DEBUG_MGMT("Stopping scan\n");
1481 ieee->softmac_stats.tx_auth_rq++;
1483 skb = rtllib_authentication_req(beacon, ieee, 0, daddr);
1485 if (!skb)
1486 rtllib_associate_abort(ieee);
1487 else {
1488 ieee->state = RTLLIB_ASSOCIATING_AUTHENTICATING ;
1489 RTLLIB_DEBUG_MGMT("Sending authentication request\n");
1490 softmac_mgmt_xmit(skb, ieee);
1491 if (!timer_pending(&ieee->associate_timer)) {
1492 ieee->associate_timer.expires = jiffies + (HZ / 2);
1493 add_timer(&ieee->associate_timer);
1498 void rtllib_auth_challenge(struct rtllib_device *ieee, u8 *challenge, int chlen)
1500 u8 *c;
1501 struct sk_buff *skb;
1502 struct rtllib_network *beacon = &ieee->current_network;
1504 ieee->associate_seq++;
1505 ieee->softmac_stats.tx_auth_rq++;
1507 skb = rtllib_authentication_req(beacon, ieee, chlen + 2, beacon->bssid);
1509 if (!skb)
1510 rtllib_associate_abort(ieee);
1511 else {
1512 c = skb_put(skb, chlen+2);
1513 *(c++) = MFIE_TYPE_CHALLENGE;
1514 *(c++) = chlen;
1515 memcpy(c, challenge, chlen);
1517 RTLLIB_DEBUG_MGMT("Sending authentication challenge "
1518 "response\n");
1520 rtllib_encrypt_fragment(ieee, skb,
1521 sizeof(struct rtllib_hdr_3addr));
1523 softmac_mgmt_xmit(skb, ieee);
1524 mod_timer(&ieee->associate_timer, jiffies + (HZ/2));
1526 kfree(challenge);
1529 void rtllib_associate_step2(struct rtllib_device *ieee)
1531 struct sk_buff *skb;
1532 struct rtllib_network *beacon = &ieee->current_network;
1534 del_timer_sync(&ieee->associate_timer);
1536 RTLLIB_DEBUG_MGMT("Sending association request\n");
1538 ieee->softmac_stats.tx_ass_rq++;
1539 skb = rtllib_association_req(beacon, ieee);
1540 if (!skb)
1541 rtllib_associate_abort(ieee);
1542 else {
1543 softmac_mgmt_xmit(skb, ieee);
1544 mod_timer(&ieee->associate_timer, jiffies + (HZ/2));
1548 #define CANCELLED 2
1549 void rtllib_associate_complete_wq(void *data)
1551 struct rtllib_device *ieee = (struct rtllib_device *)
1552 container_of_work_rsl(data,
1553 struct rtllib_device,
1554 associate_complete_wq);
1555 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
1556 (&(ieee->PowerSaveControl));
1557 printk(KERN_INFO "Associated successfully\n");
1558 if (ieee->is_silent_reset == 0) {
1559 printk(KERN_INFO "normal associate\n");
1560 notify_wx_assoc_event(ieee);
1563 netif_carrier_on(ieee->dev);
1564 ieee->is_roaming = false;
1565 if (rtllib_is_54g(&ieee->current_network) &&
1566 (ieee->modulation & RTLLIB_OFDM_MODULATION)) {
1567 ieee->rate = 108;
1568 printk(KERN_INFO"Using G rates:%d\n", ieee->rate);
1569 } else {
1570 ieee->rate = 22;
1571 ieee->SetWirelessMode(ieee->dev, IEEE_B);
1572 printk(KERN_INFO"Using B rates:%d\n", ieee->rate);
1574 if (ieee->pHTInfo->bCurrentHTSupport && ieee->pHTInfo->bEnableHT) {
1575 printk(KERN_INFO "Successfully associated, ht enabled\n");
1576 HTOnAssocRsp(ieee);
1577 } else {
1578 printk(KERN_INFO "Successfully associated, ht not "
1579 "enabled(%d, %d)\n",
1580 ieee->pHTInfo->bCurrentHTSupport,
1581 ieee->pHTInfo->bEnableHT);
1582 memset(ieee->dot11HTOperationalRateSet, 0, 16);
1584 ieee->LinkDetectInfo.SlotNum = 2 * (1 +
1585 ieee->current_network.beacon_interval /
1586 500);
1587 if (ieee->LinkDetectInfo.NumRecvBcnInPeriod == 0 ||
1588 ieee->LinkDetectInfo.NumRecvDataInPeriod == 0) {
1589 ieee->LinkDetectInfo.NumRecvBcnInPeriod = 1;
1590 ieee->LinkDetectInfo.NumRecvDataInPeriod = 1;
1592 pPSC->LpsIdleCount = 0;
1593 ieee->link_change(ieee->dev);
1595 if (ieee->is_silent_reset == 1) {
1596 printk(KERN_INFO "silent reset associate\n");
1597 ieee->is_silent_reset = 0;
1600 if (ieee->data_hard_resume)
1601 ieee->data_hard_resume(ieee->dev);
1605 static void rtllib_sta_send_associnfo(struct rtllib_device *ieee)
1609 void rtllib_associate_complete(struct rtllib_device *ieee)
1611 del_timer_sync(&ieee->associate_timer);
1613 ieee->state = RTLLIB_LINKED;
1614 rtllib_sta_send_associnfo(ieee);
1616 queue_work_rsl(ieee->wq, &ieee->associate_complete_wq);
1619 void rtllib_associate_procedure_wq(void *data)
1621 struct rtllib_device *ieee = container_of_dwork_rsl(data,
1622 struct rtllib_device,
1623 associate_procedure_wq);
1624 rtllib_stop_scan_syncro(ieee);
1625 if (ieee->rtllib_ips_leave != NULL)
1626 ieee->rtllib_ips_leave(ieee->dev);
1627 down(&ieee->wx_sem);
1629 if (ieee->data_hard_stop)
1630 ieee->data_hard_stop(ieee->dev);
1632 rtllib_stop_scan(ieee);
1633 RT_TRACE(COMP_DBG, "===>%s(), chan:%d\n", __func__,
1634 ieee->current_network.channel);
1635 HTSetConnectBwMode(ieee, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
1636 if (ieee->eRFPowerState == eRfOff) {
1637 RT_TRACE(COMP_DBG, "=============>%s():Rf state is eRfOff,"
1638 " schedule ipsleave wq again,return\n", __func__);
1639 if (ieee->rtllib_ips_leave_wq != NULL)
1640 ieee->rtllib_ips_leave_wq(ieee->dev);
1641 up(&ieee->wx_sem);
1642 return;
1644 ieee->associate_seq = 1;
1646 rtllib_associate_step1(ieee, ieee->current_network.bssid);
1648 up(&ieee->wx_sem);
1651 inline void rtllib_softmac_new_net(struct rtllib_device *ieee,
1652 struct rtllib_network *net)
1654 u8 tmp_ssid[IW_ESSID_MAX_SIZE + 1];
1655 int tmp_ssid_len = 0;
1657 short apset, ssidset, ssidbroad, apmatch, ssidmatch;
1659 /* we are interested in new new only if we are not associated
1660 * and we are not associating / authenticating
1662 if (ieee->state != RTLLIB_NOLINK)
1663 return;
1665 if ((ieee->iw_mode == IW_MODE_INFRA) && !(net->capability &
1666 WLAN_CAPABILITY_ESS))
1667 return;
1669 if ((ieee->iw_mode == IW_MODE_ADHOC) && !(net->capability &
1670 WLAN_CAPABILITY_IBSS))
1671 return;
1673 if ((ieee->iw_mode == IW_MODE_ADHOC) &&
1674 (net->channel > ieee->ibss_maxjoin_chal))
1675 return;
1676 if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC) {
1677 /* if the user specified the AP MAC, we need also the essid
1678 * This could be obtained by beacons or, if the network does not
1679 * broadcast it, it can be put manually.
1681 apset = ieee->wap_set;
1682 ssidset = ieee->ssid_set;
1683 ssidbroad = !(net->ssid_len == 0 || net->ssid[0] == '\0');
1684 apmatch = (memcmp(ieee->current_network.bssid, net->bssid,
1685 ETH_ALEN) == 0);
1686 if (!ssidbroad) {
1687 ssidmatch = (ieee->current_network.ssid_len ==
1688 net->hidden_ssid_len) &&
1689 (!strncmp(ieee->current_network.ssid,
1690 net->hidden_ssid, net->hidden_ssid_len));
1691 if (net->hidden_ssid_len > 0) {
1692 strncpy(net->ssid, net->hidden_ssid,
1693 net->hidden_ssid_len);
1694 net->ssid_len = net->hidden_ssid_len;
1695 ssidbroad = 1;
1697 } else
1698 ssidmatch =
1699 (ieee->current_network.ssid_len == net->ssid_len) &&
1700 (!strncmp(ieee->current_network.ssid, net->ssid,
1701 net->ssid_len));
1703 /* if the user set the AP check if match.
1704 * if the network does not broadcast essid we check the
1705 * user supplyed ANY essid
1706 * if the network does broadcast and the user does not set
1707 * essid it is OK
1708 * if the network does broadcast and the user did set essid
1709 * check if essid match
1710 * if the ap is not set, check that the user set the bssid
1711 * and the network does bradcast and that those two bssid match
1713 if ((apset && apmatch &&
1714 ((ssidset && ssidbroad && ssidmatch) ||
1715 (ssidbroad && !ssidset) || (!ssidbroad && ssidset))) ||
1716 (!apset && ssidset && ssidbroad && ssidmatch) ||
1717 (ieee->is_roaming && ssidset && ssidbroad && ssidmatch)) {
1718 /* if the essid is hidden replace it with the
1719 * essid provided by the user.
1721 if (!ssidbroad) {
1722 strncpy(tmp_ssid, ieee->current_network.ssid,
1723 IW_ESSID_MAX_SIZE);
1724 tmp_ssid_len = ieee->current_network.ssid_len;
1726 memcpy(&ieee->current_network, net,
1727 sizeof(struct rtllib_network));
1728 if (!ssidbroad) {
1729 strncpy(ieee->current_network.ssid, tmp_ssid,
1730 IW_ESSID_MAX_SIZE);
1731 ieee->current_network.ssid_len = tmp_ssid_len;
1733 printk(KERN_INFO"Linking with %s,channel:%d, qos:%d, "
1734 "myHT:%d, networkHT:%d, mode:%x cur_net.flags"
1735 ":0x%x\n", ieee->current_network.ssid,
1736 ieee->current_network.channel,
1737 ieee->current_network.qos_data.supported,
1738 ieee->pHTInfo->bEnableHT,
1739 ieee->current_network.bssht.bdSupportHT,
1740 ieee->current_network.mode,
1741 ieee->current_network.flags);
1743 if ((rtllib_act_scanning(ieee, false)) &&
1744 !(ieee->softmac_features & IEEE_SOFTMAC_SCAN))
1745 rtllib_stop_scan_syncro(ieee);
1747 ieee->hwscan_ch_bk = ieee->current_network.channel;
1748 HTResetIOTSetting(ieee->pHTInfo);
1749 ieee->wmm_acm = 0;
1750 if (ieee->iw_mode == IW_MODE_INFRA) {
1751 /* Join the network for the first time */
1752 ieee->AsocRetryCount = 0;
1753 if ((ieee->current_network.qos_data.supported == 1) &&
1754 ieee->current_network.bssht.bdSupportHT)
1755 HTResetSelfAndSavePeerSetting(ieee,
1756 &(ieee->current_network));
1757 else
1758 ieee->pHTInfo->bCurrentHTSupport =
1759 false;
1761 ieee->state = RTLLIB_ASSOCIATING;
1762 if (ieee->LedControlHandler != NULL)
1763 ieee->LedControlHandler(ieee->dev,
1764 LED_CTL_START_TO_LINK);
1765 queue_delayed_work_rsl(ieee->wq,
1766 &ieee->associate_procedure_wq, 0);
1767 } else {
1768 if (rtllib_is_54g(&ieee->current_network) &&
1769 (ieee->modulation & RTLLIB_OFDM_MODULATION)) {
1770 ieee->rate = 108;
1771 ieee->SetWirelessMode(ieee->dev, IEEE_G);
1772 printk(KERN_INFO"Using G rates\n");
1773 } else {
1774 ieee->rate = 22;
1775 ieee->SetWirelessMode(ieee->dev, IEEE_B);
1776 printk(KERN_INFO"Using B rates\n");
1778 memset(ieee->dot11HTOperationalRateSet, 0, 16);
1779 ieee->state = RTLLIB_LINKED;
1785 void rtllib_softmac_check_all_nets(struct rtllib_device *ieee)
1787 unsigned long flags;
1788 struct rtllib_network *target;
1790 spin_lock_irqsave(&ieee->lock, flags);
1792 list_for_each_entry(target, &ieee->network_list, list) {
1794 /* if the state become different that NOLINK means
1795 * we had found what we are searching for
1798 if (ieee->state != RTLLIB_NOLINK)
1799 break;
1801 if (ieee->scan_age == 0 || time_after(target->last_scanned +
1802 ieee->scan_age, jiffies))
1803 rtllib_softmac_new_net(ieee, target);
1805 spin_unlock_irqrestore(&ieee->lock, flags);
1808 static inline u16 auth_parse(struct sk_buff *skb, u8** challenge, int *chlen)
1810 struct rtllib_authentication *a;
1811 u8 *t;
1812 if (skb->len < (sizeof(struct rtllib_authentication) -
1813 sizeof(struct rtllib_info_element))) {
1814 RTLLIB_DEBUG_MGMT("invalid len in auth resp: %d\n", skb->len);
1815 return 0xcafe;
1817 *challenge = NULL;
1818 a = (struct rtllib_authentication *) skb->data;
1819 if (skb->len > (sizeof(struct rtllib_authentication) + 3)) {
1820 t = skb->data + sizeof(struct rtllib_authentication);
1822 if (*(t++) == MFIE_TYPE_CHALLENGE) {
1823 *chlen = *(t++);
1824 *challenge = kmalloc(*chlen, GFP_ATOMIC);
1825 memcpy(*challenge, t, *chlen); /*TODO - check here*/
1828 return cpu_to_le16(a->status);
1831 int auth_rq_parse(struct sk_buff *skb, u8 *dest)
1833 struct rtllib_authentication *a;
1835 if (skb->len < (sizeof(struct rtllib_authentication) -
1836 sizeof(struct rtllib_info_element))) {
1837 RTLLIB_DEBUG_MGMT("invalid len in auth request: %d\n",
1838 skb->len);
1839 return -1;
1841 a = (struct rtllib_authentication *) skb->data;
1843 memcpy(dest, a->header.addr2, ETH_ALEN);
1845 if (le16_to_cpu(a->algorithm) != WLAN_AUTH_OPEN)
1846 return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1848 return WLAN_STATUS_SUCCESS;
1851 static short probe_rq_parse(struct rtllib_device *ieee, struct sk_buff *skb,
1852 u8 *src)
1854 u8 *tag;
1855 u8 *skbend;
1856 u8 *ssid = NULL;
1857 u8 ssidlen = 0;
1858 struct rtllib_hdr_3addr *header =
1859 (struct rtllib_hdr_3addr *) skb->data;
1860 bool bssid_match;
1862 if (skb->len < sizeof(struct rtllib_hdr_3addr))
1863 return -1; /* corrupted */
1865 bssid_match =
1866 (memcmp(header->addr3, ieee->current_network.bssid, ETH_ALEN) != 0) &&
1867 (memcmp(header->addr3, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0);
1868 if (bssid_match)
1869 return -1;
1871 memcpy(src, header->addr2, ETH_ALEN);
1873 skbend = (u8 *)skb->data + skb->len;
1875 tag = skb->data + sizeof(struct rtllib_hdr_3addr);
1877 while (tag + 1 < skbend) {
1878 if (*tag == 0) {
1879 ssid = tag + 2;
1880 ssidlen = *(tag + 1);
1881 break;
1883 tag++; /* point to the len field */
1884 tag = tag + *(tag); /* point to the last data byte of the tag */
1885 tag++; /* point to the next tag */
1888 if (ssidlen == 0)
1889 return 1;
1891 if (!ssid)
1892 return 1; /* ssid not found in tagged param */
1894 return !strncmp(ssid, ieee->current_network.ssid, ssidlen);
1897 int assoc_rq_parse(struct sk_buff *skb, u8 *dest)
1899 struct rtllib_assoc_request_frame *a;
1901 if (skb->len < (sizeof(struct rtllib_assoc_request_frame) -
1902 sizeof(struct rtllib_info_element))) {
1904 RTLLIB_DEBUG_MGMT("invalid len in auth request:%d\n", skb->len);
1905 return -1;
1908 a = (struct rtllib_assoc_request_frame *) skb->data;
1910 memcpy(dest, a->header.addr2, ETH_ALEN);
1912 return 0;
1915 static inline u16 assoc_parse(struct rtllib_device *ieee, struct sk_buff *skb,
1916 int *aid)
1918 struct rtllib_assoc_response_frame *response_head;
1919 u16 status_code;
1921 if (skb->len < sizeof(struct rtllib_assoc_response_frame)) {
1922 RTLLIB_DEBUG_MGMT("invalid len in auth resp: %d\n", skb->len);
1923 return 0xcafe;
1926 response_head = (struct rtllib_assoc_response_frame *) skb->data;
1927 *aid = le16_to_cpu(response_head->aid) & 0x3fff;
1929 status_code = le16_to_cpu(response_head->status);
1930 if ((status_code == WLAN_STATUS_ASSOC_DENIED_RATES ||
1931 status_code == WLAN_STATUS_CAPS_UNSUPPORTED) &&
1932 ((ieee->mode == IEEE_G) &&
1933 (ieee->current_network.mode == IEEE_N_24G) &&
1934 (ieee->AsocRetryCount++ < (RT_ASOC_RETRY_LIMIT-1)))) {
1935 ieee->pHTInfo->IOTAction |= HT_IOT_ACT_PURE_N_MODE;
1936 } else {
1937 ieee->AsocRetryCount = 0;
1940 return le16_to_cpu(response_head->status);
1943 void rtllib_rx_probe_rq(struct rtllib_device *ieee, struct sk_buff *skb)
1945 u8 dest[ETH_ALEN];
1946 ieee->softmac_stats.rx_probe_rq++;
1947 if (probe_rq_parse(ieee, skb, dest) > 0) {
1948 ieee->softmac_stats.tx_probe_rs++;
1949 rtllib_resp_to_probe(ieee, dest);
1953 static inline void rtllib_rx_auth_rq(struct rtllib_device *ieee,
1954 struct sk_buff *skb)
1956 u8 dest[ETH_ALEN];
1957 int status;
1958 ieee->softmac_stats.rx_auth_rq++;
1960 status = auth_rq_parse(skb, dest);
1961 if (status != -1)
1962 rtllib_resp_to_auth(ieee, status, dest);
1965 static inline void rtllib_rx_assoc_rq(struct rtllib_device *ieee,
1966 struct sk_buff *skb)
1969 u8 dest[ETH_ALEN];
1971 ieee->softmac_stats.rx_ass_rq++;
1972 if (assoc_rq_parse(skb, dest) != -1)
1973 rtllib_resp_to_assoc_rq(ieee, dest);
1975 printk(KERN_INFO"New client associated: "MAC_FMT"\n", MAC_ARG(dest));
1978 void rtllib_sta_ps_send_null_frame(struct rtllib_device *ieee, short pwr)
1981 struct sk_buff *buf = rtllib_null_func(ieee, pwr);
1983 if (buf)
1984 softmac_ps_mgmt_xmit(buf, ieee);
1987 void rtllib_sta_ps_send_pspoll_frame(struct rtllib_device *ieee)
1989 struct sk_buff *buf = rtllib_pspoll_func(ieee);
1991 if (buf)
1992 softmac_ps_mgmt_xmit(buf, ieee);
1995 static short rtllib_sta_ps_sleep(struct rtllib_device *ieee, u64 *time)
1997 int timeout = ieee->ps_timeout;
1998 u8 dtim;
1999 struct rt_pwr_save_ctrl *pPSC = (struct rt_pwr_save_ctrl *)
2000 (&(ieee->PowerSaveControl));
2002 if (ieee->LPSDelayCnt) {
2003 ieee->LPSDelayCnt--;
2004 return 0;
2007 dtim = ieee->current_network.dtim_data;
2008 if (!(dtim & RTLLIB_DTIM_VALID))
2009 return 0;
2010 timeout = ieee->current_network.beacon_interval;
2011 ieee->current_network.dtim_data = RTLLIB_DTIM_INVALID;
2012 /* there's no need to nofity AP that I find you buffered
2013 * with broadcast packet */
2014 if (dtim & (RTLLIB_DTIM_UCAST & ieee->ps))
2015 return 2;
2017 if (!time_after(jiffies, ieee->dev->trans_start + MSECS(timeout)))
2018 return 0;
2019 if (!time_after(jiffies, ieee->last_rx_ps_time + MSECS(timeout)))
2020 return 0;
2021 if ((ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE) &&
2022 (ieee->mgmt_queue_tail != ieee->mgmt_queue_head))
2023 return 0;
2025 if (time) {
2026 if (ieee->bAwakePktSent == true) {
2027 pPSC->LPSAwakeIntvl = 1;
2028 } else {
2029 u8 MaxPeriod = 1;
2031 if (pPSC->LPSAwakeIntvl == 0)
2032 pPSC->LPSAwakeIntvl = 1;
2033 if (pPSC->RegMaxLPSAwakeIntvl == 0)
2034 MaxPeriod = 1;
2035 else if (pPSC->RegMaxLPSAwakeIntvl == 0xFF)
2036 MaxPeriod = ieee->current_network.dtim_period;
2037 else
2038 MaxPeriod = pPSC->RegMaxLPSAwakeIntvl;
2039 pPSC->LPSAwakeIntvl = (pPSC->LPSAwakeIntvl >=
2040 MaxPeriod) ? MaxPeriod :
2041 (pPSC->LPSAwakeIntvl + 1);
2044 u8 LPSAwakeIntvl_tmp = 0;
2045 u8 period = ieee->current_network.dtim_period;
2046 u8 count = ieee->current_network.tim.tim_count;
2047 if (count == 0) {
2048 if (pPSC->LPSAwakeIntvl > period)
2049 LPSAwakeIntvl_tmp = period +
2050 (pPSC->LPSAwakeIntvl -
2051 period) -
2052 ((pPSC->LPSAwakeIntvl-period) %
2053 period);
2054 else
2055 LPSAwakeIntvl_tmp = pPSC->LPSAwakeIntvl;
2057 } else {
2058 if (pPSC->LPSAwakeIntvl >
2059 ieee->current_network.tim.tim_count)
2060 LPSAwakeIntvl_tmp = count +
2061 (pPSC->LPSAwakeIntvl - count) -
2062 ((pPSC->LPSAwakeIntvl-count)%period);
2063 else
2064 LPSAwakeIntvl_tmp = pPSC->LPSAwakeIntvl;
2067 *time = ieee->current_network.last_dtim_sta_time
2068 + MSECS(ieee->current_network.beacon_interval *
2069 LPSAwakeIntvl_tmp);
2073 return 1;
2078 inline void rtllib_sta_ps(struct rtllib_device *ieee)
2080 u64 time;
2081 short sleep;
2082 unsigned long flags, flags2;
2084 spin_lock_irqsave(&ieee->lock, flags);
2086 if ((ieee->ps == RTLLIB_PS_DISABLED ||
2087 ieee->iw_mode != IW_MODE_INFRA ||
2088 ieee->state != RTLLIB_LINKED)) {
2089 RT_TRACE(COMP_DBG, "=====>%s(): no need to ps,wake up!! "
2090 "ieee->ps is %d, ieee->iw_mode is %d, ieee->state"
2091 " is %d\n", __func__, ieee->ps, ieee->iw_mode,
2092 ieee->state);
2093 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2094 rtllib_sta_wakeup(ieee, 1);
2096 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2098 sleep = rtllib_sta_ps_sleep(ieee, &time);
2099 /* 2 wake, 1 sleep, 0 do nothing */
2100 if (sleep == 0)
2101 goto out;
2102 if (sleep == 1) {
2103 if (ieee->sta_sleep == LPS_IS_SLEEP) {
2104 ieee->enter_sleep_state(ieee->dev, time);
2105 } else if (ieee->sta_sleep == LPS_IS_WAKE) {
2106 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2108 if (ieee->ps_is_queue_empty(ieee->dev)) {
2109 ieee->sta_sleep = LPS_WAIT_NULL_DATA_SEND;
2110 ieee->ack_tx_to_ieee = 1;
2111 rtllib_sta_ps_send_null_frame(ieee, 1);
2112 ieee->ps_time = time;
2114 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2118 ieee->bAwakePktSent = false;
2120 } else if (sleep == 2) {
2121 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2123 rtllib_sta_wakeup(ieee, 1);
2125 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2128 out:
2129 spin_unlock_irqrestore(&ieee->lock, flags);
2133 void rtllib_sta_wakeup(struct rtllib_device *ieee, short nl)
2135 if (ieee->sta_sleep == LPS_IS_WAKE) {
2136 if (nl) {
2137 if (ieee->pHTInfo->IOTAction &
2138 HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
2139 ieee->ack_tx_to_ieee = 1;
2140 rtllib_sta_ps_send_null_frame(ieee, 0);
2141 } else {
2142 ieee->ack_tx_to_ieee = 1;
2143 rtllib_sta_ps_send_pspoll_frame(ieee);
2146 return;
2150 if (ieee->sta_sleep == LPS_IS_SLEEP)
2151 ieee->sta_wake_up(ieee->dev);
2152 if (nl) {
2153 if (ieee->pHTInfo->IOTAction &
2154 HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
2155 ieee->ack_tx_to_ieee = 1;
2156 rtllib_sta_ps_send_null_frame(ieee, 0);
2157 } else {
2158 ieee->ack_tx_to_ieee = 1;
2159 ieee->polling = true;
2160 rtllib_sta_ps_send_pspoll_frame(ieee);
2163 } else {
2164 ieee->sta_sleep = LPS_IS_WAKE;
2165 ieee->polling = false;
2169 void rtllib_ps_tx_ack(struct rtllib_device *ieee, short success)
2171 unsigned long flags, flags2;
2173 spin_lock_irqsave(&ieee->lock, flags);
2175 if (ieee->sta_sleep == LPS_WAIT_NULL_DATA_SEND) {
2176 /* Null frame with PS bit set */
2177 if (success) {
2178 ieee->sta_sleep = LPS_IS_SLEEP;
2179 ieee->enter_sleep_state(ieee->dev, ieee->ps_time);
2181 /* if the card report not success we can't be sure the AP
2182 * has not RXed so we can't assume the AP believe us awake
2184 } else {/* 21112005 - tx again null without PS bit if lost */
2186 if ((ieee->sta_sleep == LPS_IS_WAKE) && !success) {
2187 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
2188 if (ieee->pHTInfo->IOTAction &
2189 HT_IOT_ACT_NULL_DATA_POWER_SAVING)
2190 rtllib_sta_ps_send_null_frame(ieee, 0);
2191 else
2192 rtllib_sta_ps_send_pspoll_frame(ieee);
2193 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
2196 spin_unlock_irqrestore(&ieee->lock, flags);
2199 void rtllib_process_action(struct rtllib_device *ieee, struct sk_buff *skb)
2201 struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *) skb->data;
2202 u8 *act = rtllib_get_payload((struct rtllib_hdr *)header);
2203 u8 category = 0;
2205 if (act == NULL) {
2206 RTLLIB_DEBUG(RTLLIB_DL_ERR, "error to get payload of "
2207 "action frame\n");
2208 return;
2211 category = *act;
2212 act++;
2213 switch (category) {
2214 case ACT_CAT_BA:
2215 switch (*act) {
2216 case ACT_ADDBAREQ:
2217 rtllib_rx_ADDBAReq(ieee, skb);
2218 break;
2219 case ACT_ADDBARSP:
2220 rtllib_rx_ADDBARsp(ieee, skb);
2221 break;
2222 case ACT_DELBA:
2223 rtllib_rx_DELBA(ieee, skb);
2224 break;
2226 break;
2227 default:
2228 break;
2230 return;
2233 inline int rtllib_rx_assoc_resp(struct rtllib_device *ieee, struct sk_buff *skb,
2234 struct rtllib_rx_stats *rx_stats)
2236 u16 errcode;
2237 int aid;
2238 u8 *ies;
2239 struct rtllib_assoc_response_frame *assoc_resp;
2240 struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *) skb->data;
2242 RTLLIB_DEBUG_MGMT("received [RE]ASSOCIATION RESPONSE (%d)\n",
2243 WLAN_FC_GET_STYPE(header->frame_ctl));
2245 if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2246 ieee->state == RTLLIB_ASSOCIATING_AUTHENTICATED &&
2247 (ieee->iw_mode == IW_MODE_INFRA)) {
2248 errcode = assoc_parse(ieee, skb, &aid);
2249 if (0 == errcode) {
2250 struct rtllib_network *network =
2251 kzalloc(sizeof(struct rtllib_network),
2252 GFP_ATOMIC);
2254 if (!network)
2255 return 1;
2256 memset(network, 0, sizeof(*network));
2257 ieee->state = RTLLIB_LINKED;
2258 ieee->assoc_id = aid;
2259 ieee->softmac_stats.rx_ass_ok++;
2260 /* station support qos */
2261 /* Let the register setting default with Legacy station */
2262 assoc_resp = (struct rtllib_assoc_response_frame *)skb->data;
2263 if (ieee->current_network.qos_data.supported == 1) {
2264 if (rtllib_parse_info_param(ieee, assoc_resp->info_element,
2265 rx_stats->len - sizeof(*assoc_resp),
2266 network, rx_stats)) {
2267 kfree(network);
2268 return 1;
2269 } else {
2270 memcpy(ieee->pHTInfo->PeerHTCapBuf,
2271 network->bssht.bdHTCapBuf,
2272 network->bssht.bdHTCapLen);
2273 memcpy(ieee->pHTInfo->PeerHTInfoBuf,
2274 network->bssht.bdHTInfoBuf,
2275 network->bssht.bdHTInfoLen);
2277 if (ieee->handle_assoc_response != NULL)
2278 ieee->handle_assoc_response(ieee->dev,
2279 (struct rtllib_assoc_response_frame *)header,
2280 network);
2281 kfree(network);
2284 kfree(ieee->assocresp_ies);
2285 ieee->assocresp_ies = NULL;
2286 ies = &(assoc_resp->info_element[0].id);
2287 ieee->assocresp_ies_len = (skb->data + skb->len) - ies;
2288 ieee->assocresp_ies = kmalloc(ieee->assocresp_ies_len,
2289 GFP_ATOMIC);
2290 if (ieee->assocresp_ies)
2291 memcpy(ieee->assocresp_ies, ies,
2292 ieee->assocresp_ies_len);
2293 else {
2294 printk(KERN_INFO "%s()Warning: can't alloc "
2295 "memory for assocresp_ies\n", __func__);
2296 ieee->assocresp_ies_len = 0;
2298 rtllib_associate_complete(ieee);
2299 } else {
2300 /* aid could not been allocated */
2301 ieee->softmac_stats.rx_ass_err++;
2302 printk(KERN_INFO "Association response status code 0x%x\n",
2303 errcode);
2304 RTLLIB_DEBUG_MGMT(
2305 "Association response status code 0x%x\n",
2306 errcode);
2307 if (ieee->AsocRetryCount < RT_ASOC_RETRY_LIMIT)
2308 queue_delayed_work_rsl(ieee->wq,
2309 &ieee->associate_procedure_wq, 0);
2310 else
2311 rtllib_associate_abort(ieee);
2314 return 0;
2317 inline int rtllib_rx_auth(struct rtllib_device *ieee, struct sk_buff *skb,
2318 struct rtllib_rx_stats *rx_stats)
2320 u16 errcode;
2321 u8 *challenge;
2322 int chlen = 0;
2323 bool bSupportNmode = true, bHalfSupportNmode = false;
2325 if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) {
2326 if (ieee->state == RTLLIB_ASSOCIATING_AUTHENTICATING &&
2327 (ieee->iw_mode == IW_MODE_INFRA)) {
2328 RTLLIB_DEBUG_MGMT("Received authentication response");
2330 errcode = auth_parse(skb, &challenge, &chlen);
2331 if (0 == errcode) {
2332 if (ieee->open_wep || !challenge) {
2333 ieee->state = RTLLIB_ASSOCIATING_AUTHENTICATED;
2334 ieee->softmac_stats.rx_auth_rs_ok++;
2335 if (!(ieee->pHTInfo->IOTAction &
2336 HT_IOT_ACT_PURE_N_MODE)) {
2337 if (!ieee->GetNmodeSupportBySecCfg(ieee->dev)) {
2338 if (IsHTHalfNmodeAPs(ieee)) {
2339 bSupportNmode = true;
2340 bHalfSupportNmode = true;
2341 } else {
2342 bSupportNmode = false;
2343 bHalfSupportNmode = false;
2347 /* Dummy wirless mode setting to avoid
2348 * encryption issue */
2349 if (bSupportNmode) {
2350 ieee->SetWirelessMode(ieee->dev,
2351 ieee->current_network.mode);
2352 } else {
2353 /*TODO*/
2354 ieee->SetWirelessMode(ieee->dev,
2355 IEEE_G);
2358 if (ieee->current_network.mode ==
2359 IEEE_N_24G &&
2360 bHalfSupportNmode == true) {
2361 printk(KERN_INFO "======>enter "
2362 "half N mode\n");
2363 ieee->bHalfWirelessN24GMode =
2364 true;
2365 } else
2366 ieee->bHalfWirelessN24GMode =
2367 false;
2369 rtllib_associate_step2(ieee);
2370 } else {
2371 rtllib_auth_challenge(ieee, challenge,
2372 chlen);
2374 } else {
2375 ieee->softmac_stats.rx_auth_rs_err++;
2376 RTLLIB_DEBUG_MGMT("Authentication respose"
2377 " status code 0x%x", errcode);
2379 printk(KERN_INFO "Authentication respose "
2380 "status code 0x%x", errcode);
2381 rtllib_associate_abort(ieee);
2384 } else if (ieee->iw_mode == IW_MODE_MASTER) {
2385 rtllib_rx_auth_rq(ieee, skb);
2388 return 0;
2391 inline int rtllib_rx_deauth(struct rtllib_device *ieee, struct sk_buff *skb)
2393 struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *) skb->data;
2395 if (memcmp(header->addr3, ieee->current_network.bssid, ETH_ALEN) != 0)
2396 return 0;
2398 /* FIXME for now repeat all the association procedure
2399 * both for disassociation and deauthentication
2401 if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2402 ieee->state == RTLLIB_LINKED &&
2403 (ieee->iw_mode == IW_MODE_INFRA)) {
2404 printk(KERN_INFO "==========>received disassoc/deauth(%x) "
2405 "frame, reason code:%x\n",
2406 WLAN_FC_GET_STYPE(header->frame_ctl),
2407 ((struct rtllib_disassoc *)skb->data)->reason);
2408 ieee->state = RTLLIB_ASSOCIATING;
2409 ieee->softmac_stats.reassoc++;
2410 ieee->is_roaming = true;
2411 ieee->LinkDetectInfo.bBusyTraffic = false;
2412 rtllib_disassociate(ieee);
2413 RemovePeerTS(ieee, header->addr2);
2414 if (ieee->LedControlHandler != NULL)
2415 ieee->LedControlHandler(ieee->dev,
2416 LED_CTL_START_TO_LINK);
2418 if (!(ieee->rtllib_ap_sec_type(ieee) &
2419 (SEC_ALG_CCMP|SEC_ALG_TKIP)))
2420 queue_delayed_work_rsl(ieee->wq,
2421 &ieee->associate_procedure_wq, 5);
2423 return 0;
2426 inline int rtllib_rx_frame_softmac(struct rtllib_device *ieee,
2427 struct sk_buff *skb,
2428 struct rtllib_rx_stats *rx_stats, u16 type,
2429 u16 stype)
2431 struct rtllib_hdr_3addr *header = (struct rtllib_hdr_3addr *) skb->data;
2433 if (!ieee->proto_started)
2434 return 0;
2436 switch (WLAN_FC_GET_STYPE(header->frame_ctl)) {
2437 case RTLLIB_STYPE_ASSOC_RESP:
2438 case RTLLIB_STYPE_REASSOC_RESP:
2439 if (rtllib_rx_assoc_resp(ieee, skb, rx_stats) == 1)
2440 return 1;
2441 break;
2442 case RTLLIB_STYPE_ASSOC_REQ:
2443 case RTLLIB_STYPE_REASSOC_REQ:
2444 if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
2445 ieee->iw_mode == IW_MODE_MASTER)
2446 rtllib_rx_assoc_rq(ieee, skb);
2447 break;
2448 case RTLLIB_STYPE_AUTH:
2449 rtllib_rx_auth(ieee, skb, rx_stats);
2450 break;
2451 case RTLLIB_STYPE_DISASSOC:
2452 case RTLLIB_STYPE_DEAUTH:
2453 rtllib_rx_deauth(ieee, skb);
2454 break;
2455 case RTLLIB_STYPE_MANAGE_ACT:
2456 rtllib_process_action(ieee, skb);
2457 break;
2458 default:
2459 return -1;
2460 break;
2462 return 0;
2465 /* following are for a simplier TX queue management.
2466 * Instead of using netif_[stop/wake]_queue the driver
2467 * will uses these two function (plus a reset one), that
2468 * will internally uses the kernel netif_* and takes
2469 * care of the ieee802.11 fragmentation.
2470 * So the driver receives a fragment per time and might
2471 * call the stop function when it want without take care
2472 * to have enought room to TX an entire packet.
2473 * This might be useful if each fragment need it's own
2474 * descriptor, thus just keep a total free memory > than
2475 * the max fragmentation treshold is not enought.. If the
2476 * ieee802.11 stack passed a TXB struct then you needed
2477 * to keep N free descriptors where
2478 * N = MAX_PACKET_SIZE / MIN_FRAG_TRESHOLD
2479 * In this way you need just one and the 802.11 stack
2480 * will take care of buffering fragments and pass them to
2481 * to the driver later, when it wakes the queue.
2483 void rtllib_softmac_xmit(struct rtllib_txb *txb, struct rtllib_device *ieee)
2486 unsigned int queue_index = txb->queue_index;
2487 unsigned long flags;
2488 int i;
2489 struct cb_desc *tcb_desc = NULL;
2490 unsigned long queue_len = 0;
2492 spin_lock_irqsave(&ieee->lock, flags);
2494 /* called with 2nd parm 0, no tx mgmt lock required */
2495 rtllib_sta_wakeup(ieee, 0);
2497 /* update the tx status */
2498 tcb_desc = (struct cb_desc *)(txb->fragments[0]->cb +
2499 MAX_DEV_ADDR_SIZE);
2500 if (tcb_desc->bMulticast)
2501 ieee->stats.multicast++;
2503 /* if xmit available, just xmit it immediately, else just insert it to
2504 * the wait queue */
2505 for (i = 0; i < txb->nr_frags; i++) {
2506 queue_len = skb_queue_len(&ieee->skb_waitQ[queue_index]);
2507 if ((queue_len != 0) ||\
2508 (!ieee->check_nic_enough_desc(ieee->dev, queue_index)) ||
2509 (ieee->queue_stop)) {
2510 /* insert the skb packet to the wait queue */
2511 /* as for the completion function, it does not need
2512 * to check it any more.
2513 * */
2514 if (queue_len < 200)
2515 skb_queue_tail(&ieee->skb_waitQ[queue_index],
2516 txb->fragments[i]);
2517 else
2518 kfree_skb(txb->fragments[i]);
2519 } else {
2520 ieee->softmac_data_hard_start_xmit(
2521 txb->fragments[i],
2522 ieee->dev, ieee->rate);
2526 rtllib_txb_free(txb);
2528 spin_unlock_irqrestore(&ieee->lock, flags);
2532 /* called with ieee->lock acquired */
2533 void rtllib_resume_tx(struct rtllib_device *ieee)
2535 int i;
2536 for (i = ieee->tx_pending.frag; i < ieee->tx_pending.txb->nr_frags;
2537 i++) {
2539 if (ieee->queue_stop) {
2540 ieee->tx_pending.frag = i;
2541 return;
2542 } else {
2544 ieee->softmac_data_hard_start_xmit(
2545 ieee->tx_pending.txb->fragments[i],
2546 ieee->dev, ieee->rate);
2547 ieee->stats.tx_packets++;
2551 rtllib_txb_free(ieee->tx_pending.txb);
2552 ieee->tx_pending.txb = NULL;
2556 void rtllib_reset_queue(struct rtllib_device *ieee)
2558 unsigned long flags;
2560 spin_lock_irqsave(&ieee->lock, flags);
2561 init_mgmt_queue(ieee);
2562 if (ieee->tx_pending.txb) {
2563 rtllib_txb_free(ieee->tx_pending.txb);
2564 ieee->tx_pending.txb = NULL;
2566 ieee->queue_stop = 0;
2567 spin_unlock_irqrestore(&ieee->lock, flags);
2571 void rtllib_wake_queue(struct rtllib_device *ieee)
2574 unsigned long flags;
2575 struct sk_buff *skb;
2576 struct rtllib_hdr_3addr *header;
2578 spin_lock_irqsave(&ieee->lock, flags);
2579 if (!ieee->queue_stop)
2580 goto exit;
2582 ieee->queue_stop = 0;
2584 if (ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE) {
2585 while (!ieee->queue_stop && (skb = dequeue_mgmt(ieee))) {
2587 header = (struct rtllib_hdr_3addr *) skb->data;
2589 header->seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2591 if (ieee->seq_ctrl[0] == 0xFFF)
2592 ieee->seq_ctrl[0] = 0;
2593 else
2594 ieee->seq_ctrl[0]++;
2596 ieee->softmac_data_hard_start_xmit(skb, ieee->dev,
2597 ieee->basic_rate);
2600 if (!ieee->queue_stop && ieee->tx_pending.txb)
2601 rtllib_resume_tx(ieee);
2603 if (!ieee->queue_stop && netif_queue_stopped(ieee->dev)) {
2604 ieee->softmac_stats.swtxawake++;
2605 netif_wake_queue(ieee->dev);
2608 exit:
2609 spin_unlock_irqrestore(&ieee->lock, flags);
2613 void rtllib_stop_queue(struct rtllib_device *ieee)
2616 if (!netif_queue_stopped(ieee->dev)) {
2617 netif_stop_queue(ieee->dev);
2618 ieee->softmac_stats.swtxstop++;
2620 ieee->queue_stop = 1;
2624 void rtllib_stop_all_queues(struct rtllib_device *ieee)
2626 unsigned int i;
2627 for (i = 0; i < ieee->dev->num_tx_queues; i++)
2628 netdev_get_tx_queue(ieee->dev, i)->trans_start = jiffies;
2630 netif_tx_stop_all_queues(ieee->dev);
2633 void rtllib_wake_all_queues(struct rtllib_device *ieee)
2635 netif_tx_wake_all_queues(ieee->dev);
2638 inline void rtllib_randomize_cell(struct rtllib_device *ieee)
2641 get_random_bytes(ieee->current_network.bssid, ETH_ALEN);
2643 /* an IBSS cell address must have the two less significant
2644 * bits of the first byte = 2
2646 ieee->current_network.bssid[0] &= ~0x01;
2647 ieee->current_network.bssid[0] |= 0x02;
2650 /* called in user context only */
2651 void rtllib_start_master_bss(struct rtllib_device *ieee)
2653 ieee->assoc_id = 1;
2655 if (ieee->current_network.ssid_len == 0) {
2656 strncpy(ieee->current_network.ssid,
2657 RTLLIB_DEFAULT_TX_ESSID,
2658 IW_ESSID_MAX_SIZE);
2660 ieee->current_network.ssid_len =
2661 strlen(RTLLIB_DEFAULT_TX_ESSID);
2662 ieee->ssid_set = 1;
2665 memcpy(ieee->current_network.bssid, ieee->dev->dev_addr, ETH_ALEN);
2667 ieee->set_chan(ieee->dev, ieee->current_network.channel);
2668 ieee->state = RTLLIB_LINKED;
2669 ieee->link_change(ieee->dev);
2670 notify_wx_assoc_event(ieee);
2672 if (ieee->data_hard_resume)
2673 ieee->data_hard_resume(ieee->dev);
2675 netif_carrier_on(ieee->dev);
2678 void rtllib_start_monitor_mode(struct rtllib_device *ieee)
2680 /* reset hardware status */
2681 if (ieee->raw_tx) {
2682 if (ieee->data_hard_resume)
2683 ieee->data_hard_resume(ieee->dev);
2685 netif_carrier_on(ieee->dev);
2689 void rtllib_start_ibss_wq(void *data)
2691 struct rtllib_device *ieee = container_of_dwork_rsl(data,
2692 struct rtllib_device, start_ibss_wq);
2693 /* iwconfig mode ad-hoc will schedule this and return
2694 * on the other hand this will block further iwconfig SET
2695 * operations because of the wx_sem hold.
2696 * Anyway some most set operations set a flag to speed-up
2697 * (abort) this wq (when syncro scanning) before sleeping
2698 * on the semaphore
2700 if (!ieee->proto_started) {
2701 printk(KERN_INFO "==========oh driver down return\n");
2702 return;
2704 down(&ieee->wx_sem);
2706 if (ieee->current_network.ssid_len == 0) {
2707 strcpy(ieee->current_network.ssid, RTLLIB_DEFAULT_TX_ESSID);
2708 ieee->current_network.ssid_len = strlen(RTLLIB_DEFAULT_TX_ESSID);
2709 ieee->ssid_set = 1;
2712 ieee->state = RTLLIB_NOLINK;
2713 ieee->mode = IEEE_G;
2714 /* check if we have this cell in our network list */
2715 rtllib_softmac_check_all_nets(ieee);
2718 /* if not then the state is not linked. Maybe the user swithced to
2719 * ad-hoc mode just after being in monitor mode, or just after
2720 * being very few time in managed mode (so the card have had no
2721 * time to scan all the chans..) or we have just run up the iface
2722 * after setting ad-hoc mode. So we have to give another try..
2723 * Here, in ibss mode, should be safe to do this without extra care
2724 * (in bss mode we had to make sure no-one tryed to associate when
2725 * we had just checked the ieee->state and we was going to start the
2726 * scan) beacause in ibss mode the rtllib_new_net function, when
2727 * finds a good net, just set the ieee->state to RTLLIB_LINKED,
2728 * so, at worst, we waste a bit of time to initiate an unneeded syncro
2729 * scan, that will stop at the first round because it sees the state
2730 * associated.
2732 if (ieee->state == RTLLIB_NOLINK)
2733 rtllib_start_scan_syncro(ieee, 0);
2735 /* the network definitively is not here.. create a new cell */
2736 if (ieee->state == RTLLIB_NOLINK) {
2737 printk(KERN_INFO "creating new IBSS cell\n");
2738 ieee->current_network.channel = ieee->IbssStartChnl;
2739 if (!ieee->wap_set)
2740 rtllib_randomize_cell(ieee);
2742 if (ieee->modulation & RTLLIB_CCK_MODULATION) {
2744 ieee->current_network.rates_len = 4;
2746 ieee->current_network.rates[0] =
2747 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_1MB;
2748 ieee->current_network.rates[1] =
2749 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_2MB;
2750 ieee->current_network.rates[2] =
2751 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_5MB;
2752 ieee->current_network.rates[3] =
2753 RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_11MB;
2755 } else
2756 ieee->current_network.rates_len = 0;
2758 if (ieee->modulation & RTLLIB_OFDM_MODULATION) {
2759 ieee->current_network.rates_ex_len = 8;
2761 ieee->current_network.rates_ex[0] =
2762 RTLLIB_OFDM_RATE_6MB;
2763 ieee->current_network.rates_ex[1] =
2764 RTLLIB_OFDM_RATE_9MB;
2765 ieee->current_network.rates_ex[2] =
2766 RTLLIB_OFDM_RATE_12MB;
2767 ieee->current_network.rates_ex[3] =
2768 RTLLIB_OFDM_RATE_18MB;
2769 ieee->current_network.rates_ex[4] =
2770 RTLLIB_OFDM_RATE_24MB;
2771 ieee->current_network.rates_ex[5] =
2772 RTLLIB_OFDM_RATE_36MB;
2773 ieee->current_network.rates_ex[6] =
2774 RTLLIB_OFDM_RATE_48MB;
2775 ieee->current_network.rates_ex[7] =
2776 RTLLIB_OFDM_RATE_54MB;
2778 ieee->rate = 108;
2779 } else {
2780 ieee->current_network.rates_ex_len = 0;
2781 ieee->rate = 22;
2784 ieee->current_network.qos_data.supported = 0;
2785 ieee->SetWirelessMode(ieee->dev, IEEE_G);
2786 ieee->current_network.mode = ieee->mode;
2787 ieee->current_network.atim_window = 0;
2788 ieee->current_network.capability = WLAN_CAPABILITY_IBSS;
2791 printk(KERN_INFO "%s(): ieee->mode = %d\n", __func__, ieee->mode);
2792 if ((ieee->mode == IEEE_N_24G) || (ieee->mode == IEEE_N_5G))
2793 HTUseDefaultSetting(ieee);
2794 else
2795 ieee->pHTInfo->bCurrentHTSupport = false;
2797 ieee->SetHwRegHandler(ieee->dev, HW_VAR_MEDIA_STATUS,
2798 (u8 *)(&ieee->state));
2800 ieee->state = RTLLIB_LINKED;
2801 ieee->link_change(ieee->dev);
2803 HTSetConnectBwMode(ieee, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
2804 if (ieee->LedControlHandler != NULL)
2805 ieee->LedControlHandler(ieee->dev, LED_CTL_LINK);
2807 rtllib_start_send_beacons(ieee);
2809 notify_wx_assoc_event(ieee);
2811 if (ieee->data_hard_resume)
2812 ieee->data_hard_resume(ieee->dev);
2814 netif_carrier_on(ieee->dev);
2816 up(&ieee->wx_sem);
2819 inline void rtllib_start_ibss(struct rtllib_device *ieee)
2821 queue_delayed_work_rsl(ieee->wq, &ieee->start_ibss_wq, MSECS(150));
2824 /* this is called only in user context, with wx_sem held */
2825 void rtllib_start_bss(struct rtllib_device *ieee)
2827 unsigned long flags;
2828 if (IS_DOT11D_ENABLE(ieee) && !IS_COUNTRY_IE_VALID(ieee)) {
2829 if (!ieee->bGlobalDomain)
2830 return;
2832 /* check if we have already found the net we
2833 * are interested in (if any).
2834 * if not (we are disassociated and we are not
2835 * in associating / authenticating phase) start the background scanning.
2837 rtllib_softmac_check_all_nets(ieee);
2839 /* ensure no-one start an associating process (thus setting
2840 * the ieee->state to rtllib_ASSOCIATING) while we
2841 * have just cheked it and we are going to enable scan.
2842 * The rtllib_new_net function is always called with
2843 * lock held (from both rtllib_softmac_check_all_nets and
2844 * the rx path), so we cannot be in the middle of such function
2846 spin_lock_irqsave(&ieee->lock, flags);
2848 if (ieee->state == RTLLIB_NOLINK)
2849 rtllib_start_scan(ieee);
2850 spin_unlock_irqrestore(&ieee->lock, flags);
2853 void rtllib_link_change_wq(void *data)
2855 struct rtllib_device *ieee = container_of_dwork_rsl(data,
2856 struct rtllib_device, link_change_wq);
2857 ieee->link_change(ieee->dev);
2859 /* called only in userspace context */
2860 void rtllib_disassociate(struct rtllib_device *ieee)
2862 netif_carrier_off(ieee->dev);
2863 if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)
2864 rtllib_reset_queue(ieee);
2866 if (ieee->data_hard_stop)
2867 ieee->data_hard_stop(ieee->dev);
2868 if (IS_DOT11D_ENABLE(ieee))
2869 Dot11d_Reset(ieee);
2870 ieee->state = RTLLIB_NOLINK;
2871 ieee->is_set_key = false;
2872 ieee->wap_set = 0;
2874 queue_delayed_work_rsl(ieee->wq, &ieee->link_change_wq, 0);
2876 notify_wx_assoc_event(ieee);
2879 void rtllib_associate_retry_wq(void *data)
2881 struct rtllib_device *ieee = container_of_dwork_rsl(data,
2882 struct rtllib_device, associate_retry_wq);
2883 unsigned long flags;
2885 down(&ieee->wx_sem);
2886 if (!ieee->proto_started)
2887 goto exit;
2889 if (ieee->state != RTLLIB_ASSOCIATING_RETRY)
2890 goto exit;
2892 /* until we do not set the state to RTLLIB_NOLINK
2893 * there are no possibility to have someone else trying
2894 * to start an association procdure (we get here with
2895 * ieee->state = RTLLIB_ASSOCIATING).
2896 * When we set the state to RTLLIB_NOLINK it is possible
2897 * that the RX path run an attempt to associate, but
2898 * both rtllib_softmac_check_all_nets and the
2899 * RX path works with ieee->lock held so there are no
2900 * problems. If we are still disassociated then start a scan.
2901 * the lock here is necessary to ensure no one try to start
2902 * an association procedure when we have just checked the
2903 * state and we are going to start the scan.
2905 ieee->beinretry = true;
2906 ieee->state = RTLLIB_NOLINK;
2908 rtllib_softmac_check_all_nets(ieee);
2910 spin_lock_irqsave(&ieee->lock, flags);
2912 if (ieee->state == RTLLIB_NOLINK)
2913 rtllib_start_scan(ieee);
2914 spin_unlock_irqrestore(&ieee->lock, flags);
2916 ieee->beinretry = false;
2917 exit:
2918 up(&ieee->wx_sem);
2921 struct sk_buff *rtllib_get_beacon_(struct rtllib_device *ieee)
2923 u8 broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
2925 struct sk_buff *skb;
2926 struct rtllib_probe_response *b;
2927 skb = rtllib_probe_resp(ieee, broadcast_addr);
2929 if (!skb)
2930 return NULL;
2932 b = (struct rtllib_probe_response *) skb->data;
2933 b->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_BEACON);
2935 return skb;
2939 struct sk_buff *rtllib_get_beacon(struct rtllib_device *ieee)
2941 struct sk_buff *skb;
2942 struct rtllib_probe_response *b;
2944 skb = rtllib_get_beacon_(ieee);
2945 if (!skb)
2946 return NULL;
2948 b = (struct rtllib_probe_response *) skb->data;
2949 b->header.seq_ctl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2951 if (ieee->seq_ctrl[0] == 0xFFF)
2952 ieee->seq_ctrl[0] = 0;
2953 else
2954 ieee->seq_ctrl[0]++;
2956 return skb;
2959 void rtllib_softmac_stop_protocol(struct rtllib_device *ieee, u8 mesh_flag,
2960 u8 shutdown)
2962 rtllib_stop_scan_syncro(ieee);
2963 down(&ieee->wx_sem);
2964 rtllib_stop_protocol(ieee, shutdown);
2965 up(&ieee->wx_sem);
2969 void rtllib_stop_protocol(struct rtllib_device *ieee, u8 shutdown)
2971 if (!ieee->proto_started)
2972 return;
2974 if (shutdown) {
2975 ieee->proto_started = 0;
2976 ieee->proto_stoppping = 1;
2977 if (ieee->rtllib_ips_leave != NULL)
2978 ieee->rtllib_ips_leave(ieee->dev);
2981 rtllib_stop_send_beacons(ieee);
2982 del_timer_sync(&ieee->associate_timer);
2983 cancel_delayed_work(&ieee->associate_retry_wq);
2984 cancel_delayed_work(&ieee->start_ibss_wq);
2985 cancel_delayed_work(&ieee->link_change_wq);
2986 rtllib_stop_scan(ieee);
2988 if (ieee->state <= RTLLIB_ASSOCIATING_AUTHENTICATED)
2989 ieee->state = RTLLIB_NOLINK;
2991 if (ieee->state == RTLLIB_LINKED) {
2992 if (ieee->iw_mode == IW_MODE_INFRA)
2993 SendDisassociation(ieee, 1, deauth_lv_ss);
2994 rtllib_disassociate(ieee);
2997 if (shutdown) {
2998 RemoveAllTS(ieee);
2999 ieee->proto_stoppping = 0;
3001 kfree(ieee->assocreq_ies);
3002 ieee->assocreq_ies = NULL;
3003 ieee->assocreq_ies_len = 0;
3004 kfree(ieee->assocresp_ies);
3005 ieee->assocresp_ies = NULL;
3006 ieee->assocresp_ies_len = 0;
3009 void rtllib_softmac_start_protocol(struct rtllib_device *ieee, u8 mesh_flag)
3011 down(&ieee->wx_sem);
3012 rtllib_start_protocol(ieee);
3013 up(&ieee->wx_sem);
3016 void rtllib_start_protocol(struct rtllib_device *ieee)
3018 short ch = 0;
3019 int i = 0;
3021 rtllib_update_active_chan_map(ieee);
3023 if (ieee->proto_started)
3024 return;
3026 ieee->proto_started = 1;
3028 if (ieee->current_network.channel == 0) {
3029 do {
3030 ch++;
3031 if (ch > MAX_CHANNEL_NUMBER)
3032 return; /* no channel found */
3033 } while (!ieee->active_channel_map[ch]);
3034 ieee->current_network.channel = ch;
3037 if (ieee->current_network.beacon_interval == 0)
3038 ieee->current_network.beacon_interval = 100;
3040 for (i = 0; i < 17; i++) {
3041 ieee->last_rxseq_num[i] = -1;
3042 ieee->last_rxfrag_num[i] = -1;
3043 ieee->last_packet_time[i] = 0;
3046 if (ieee->UpdateBeaconInterruptHandler)
3047 ieee->UpdateBeaconInterruptHandler(ieee->dev, false);
3049 ieee->wmm_acm = 0;
3050 /* if the user set the MAC of the ad-hoc cell and then
3051 * switch to managed mode, shall we make sure that association
3052 * attempts does not fail just because the user provide the essid
3053 * and the nic is still checking for the AP MAC ??
3055 if (ieee->iw_mode == IW_MODE_INFRA) {
3056 rtllib_start_bss(ieee);
3057 } else if (ieee->iw_mode == IW_MODE_ADHOC) {
3058 if (ieee->UpdateBeaconInterruptHandler)
3059 ieee->UpdateBeaconInterruptHandler(ieee->dev, true);
3061 rtllib_start_ibss(ieee);
3063 } else if (ieee->iw_mode == IW_MODE_MASTER) {
3064 rtllib_start_master_bss(ieee);
3065 } else if (ieee->iw_mode == IW_MODE_MONITOR) {
3066 rtllib_start_monitor_mode(ieee);
3070 void rtllib_softmac_init(struct rtllib_device *ieee)
3072 int i;
3073 memset(&ieee->current_network, 0, sizeof(struct rtllib_network));
3075 ieee->state = RTLLIB_NOLINK;
3076 for (i = 0; i < 5; i++)
3077 ieee->seq_ctrl[i] = 0;
3078 ieee->pDot11dInfo = kmalloc(sizeof(struct rt_dot11d_info), GFP_ATOMIC);
3079 if (!ieee->pDot11dInfo)
3080 RTLLIB_DEBUG(RTLLIB_DL_ERR, "can't alloc memory for DOT11D\n");
3081 memset(ieee->pDot11dInfo, 0, sizeof(struct rt_dot11d_info));
3082 ieee->LinkDetectInfo.SlotIndex = 0;
3083 ieee->LinkDetectInfo.SlotNum = 2;
3084 ieee->LinkDetectInfo.NumRecvBcnInPeriod = 0;
3085 ieee->LinkDetectInfo.NumRecvDataInPeriod = 0;
3086 ieee->LinkDetectInfo.NumTxOkInPeriod = 0;
3087 ieee->LinkDetectInfo.NumRxOkInPeriod = 0;
3088 ieee->LinkDetectInfo.NumRxUnicastOkInPeriod = 0;
3089 ieee->bIsAggregateFrame = false;
3090 ieee->assoc_id = 0;
3091 ieee->queue_stop = 0;
3092 ieee->scanning_continue = 0;
3093 ieee->softmac_features = 0;
3094 ieee->wap_set = 0;
3095 ieee->ssid_set = 0;
3096 ieee->proto_started = 0;
3097 ieee->proto_stoppping = 0;
3098 ieee->basic_rate = RTLLIB_DEFAULT_BASIC_RATE;
3099 ieee->rate = 22;
3100 ieee->ps = RTLLIB_PS_DISABLED;
3101 ieee->sta_sleep = LPS_IS_WAKE;
3103 ieee->Regdot11HTOperationalRateSet[0] = 0xff;
3104 ieee->Regdot11HTOperationalRateSet[1] = 0xff;
3105 ieee->Regdot11HTOperationalRateSet[4] = 0x01;
3107 ieee->Regdot11TxHTOperationalRateSet[0] = 0xff;
3108 ieee->Regdot11TxHTOperationalRateSet[1] = 0xff;
3109 ieee->Regdot11TxHTOperationalRateSet[4] = 0x01;
3111 ieee->FirstIe_InScan = false;
3112 ieee->actscanning = false;
3113 ieee->beinretry = false;
3114 ieee->is_set_key = false;
3115 init_mgmt_queue(ieee);
3117 ieee->sta_edca_param[0] = 0x0000A403;
3118 ieee->sta_edca_param[1] = 0x0000A427;
3119 ieee->sta_edca_param[2] = 0x005E4342;
3120 ieee->sta_edca_param[3] = 0x002F3262;
3121 ieee->aggregation = true;
3122 ieee->enable_rx_imm_BA = 1;
3123 ieee->tx_pending.txb = NULL;
3125 _setup_timer(&ieee->associate_timer,
3126 rtllib_associate_abort_cb,
3127 (unsigned long) ieee);
3129 _setup_timer(&ieee->beacon_timer,
3130 rtllib_send_beacon_cb,
3131 (unsigned long) ieee);
3134 ieee->wq = create_workqueue(DRV_NAME);
3136 INIT_DELAYED_WORK_RSL(&ieee->link_change_wq,
3137 (void *)rtllib_link_change_wq, ieee);
3138 INIT_DELAYED_WORK_RSL(&ieee->start_ibss_wq,
3139 (void *)rtllib_start_ibss_wq, ieee);
3140 INIT_WORK_RSL(&ieee->associate_complete_wq,
3141 (void *)rtllib_associate_complete_wq, ieee);
3142 INIT_DELAYED_WORK_RSL(&ieee->associate_procedure_wq,
3143 (void *)rtllib_associate_procedure_wq, ieee);
3144 INIT_DELAYED_WORK_RSL(&ieee->softmac_scan_wq,
3145 (void *)rtllib_softmac_scan_wq, ieee);
3146 INIT_DELAYED_WORK_RSL(&ieee->softmac_hint11d_wq,
3147 (void *)rtllib_softmac_hint11d_wq, ieee);
3148 INIT_DELAYED_WORK_RSL(&ieee->associate_retry_wq,
3149 (void *)rtllib_associate_retry_wq, ieee);
3150 INIT_WORK_RSL(&ieee->wx_sync_scan_wq, (void *)rtllib_wx_sync_scan_wq,
3151 ieee);
3153 sema_init(&ieee->wx_sem, 1);
3154 sema_init(&ieee->scan_sem, 1);
3155 sema_init(&ieee->ips_sem, 1);
3157 spin_lock_init(&ieee->mgmt_tx_lock);
3158 spin_lock_init(&ieee->beacon_lock);
3160 tasklet_init(&ieee->ps_task,
3161 (void(*)(unsigned long)) rtllib_sta_ps,
3162 (unsigned long)ieee);
3166 void rtllib_softmac_free(struct rtllib_device *ieee)
3168 down(&ieee->wx_sem);
3169 if (NULL != ieee->pDot11dInfo) {
3170 kfree(ieee->pDot11dInfo);
3171 ieee->pDot11dInfo = NULL;
3173 del_timer_sync(&ieee->associate_timer);
3175 cancel_delayed_work(&ieee->associate_retry_wq);
3176 destroy_workqueue(ieee->wq);
3177 up(&ieee->wx_sem);
3180 /********************************************************
3181 * Start of WPA code. *
3182 * this is stolen from the ipw2200 driver *
3183 ********************************************************/
3186 static int rtllib_wpa_enable(struct rtllib_device *ieee, int value)
3188 /* This is called when wpa_supplicant loads and closes the driver
3189 * interface. */
3190 printk(KERN_INFO "%s WPA\n", value ? "enabling" : "disabling");
3191 ieee->wpa_enabled = value;
3192 memset(ieee->ap_mac_addr, 0, 6);
3193 return 0;
3197 void rtllib_wpa_assoc_frame(struct rtllib_device *ieee, char *wpa_ie,
3198 int wpa_ie_len)
3200 /* make sure WPA is enabled */
3201 rtllib_wpa_enable(ieee, 1);
3203 rtllib_disassociate(ieee);
3207 static int rtllib_wpa_mlme(struct rtllib_device *ieee, int command, int reason)
3210 int ret = 0;
3212 switch (command) {
3213 case IEEE_MLME_STA_DEAUTH:
3214 break;
3216 case IEEE_MLME_STA_DISASSOC:
3217 rtllib_disassociate(ieee);
3218 break;
3220 default:
3221 printk(KERN_INFO "Unknown MLME request: %d\n", command);
3222 ret = -EOPNOTSUPP;
3225 return ret;
3229 static int rtllib_wpa_set_wpa_ie(struct rtllib_device *ieee,
3230 struct ieee_param *param, int plen)
3232 u8 *buf;
3234 if (param->u.wpa_ie.len > MAX_WPA_IE_LEN ||
3235 (param->u.wpa_ie.len && param->u.wpa_ie.data == NULL))
3236 return -EINVAL;
3238 if (param->u.wpa_ie.len) {
3239 buf = kmalloc(param->u.wpa_ie.len, GFP_KERNEL);
3240 if (buf == NULL)
3241 return -ENOMEM;
3243 memcpy(buf, param->u.wpa_ie.data, param->u.wpa_ie.len);
3244 kfree(ieee->wpa_ie);
3245 ieee->wpa_ie = buf;
3246 ieee->wpa_ie_len = param->u.wpa_ie.len;
3247 } else {
3248 kfree(ieee->wpa_ie);
3249 ieee->wpa_ie = NULL;
3250 ieee->wpa_ie_len = 0;
3253 rtllib_wpa_assoc_frame(ieee, ieee->wpa_ie, ieee->wpa_ie_len);
3254 return 0;
3257 #define AUTH_ALG_OPEN_SYSTEM 0x1
3258 #define AUTH_ALG_SHARED_KEY 0x2
3259 #define AUTH_ALG_LEAP 0x4
3260 static int rtllib_wpa_set_auth_algs(struct rtllib_device *ieee, int value)
3263 struct rtllib_security sec = {
3264 .flags = SEC_AUTH_MODE,
3266 int ret = 0;
3268 if (value & AUTH_ALG_SHARED_KEY) {
3269 sec.auth_mode = WLAN_AUTH_SHARED_KEY;
3270 ieee->open_wep = 0;
3271 ieee->auth_mode = 1;
3272 } else if (value & AUTH_ALG_OPEN_SYSTEM) {
3273 sec.auth_mode = WLAN_AUTH_OPEN;
3274 ieee->open_wep = 1;
3275 ieee->auth_mode = 0;
3276 } else if (value & AUTH_ALG_LEAP) {
3277 sec.auth_mode = WLAN_AUTH_LEAP >> 6;
3278 ieee->open_wep = 1;
3279 ieee->auth_mode = 2;
3283 if (ieee->set_security)
3284 ieee->set_security(ieee->dev, &sec);
3286 return ret;
3289 static int rtllib_wpa_set_param(struct rtllib_device *ieee, u8 name, u32 value)
3291 int ret = 0;
3292 unsigned long flags;
3294 switch (name) {
3295 case IEEE_PARAM_WPA_ENABLED:
3296 ret = rtllib_wpa_enable(ieee, value);
3297 break;
3299 case IEEE_PARAM_TKIP_COUNTERMEASURES:
3300 ieee->tkip_countermeasures = value;
3301 break;
3303 case IEEE_PARAM_DROP_UNENCRYPTED:
3305 /* HACK:
3307 * wpa_supplicant calls set_wpa_enabled when the driver
3308 * is loaded and unloaded, regardless of if WPA is being
3309 * used. No other calls are made which can be used to
3310 * determine if encryption will be used or not prior to
3311 * association being expected. If encryption is not being
3312 * used, drop_unencrypted is set to false, else true -- we
3313 * can use this to determine if the CAP_PRIVACY_ON bit should
3314 * be set.
3316 struct rtllib_security sec = {
3317 .flags = SEC_ENABLED,
3318 .enabled = value,
3320 ieee->drop_unencrypted = value;
3321 /* We only change SEC_LEVEL for open mode. Others
3322 * are set by ipw_wpa_set_encryption.
3324 if (!value) {
3325 sec.flags |= SEC_LEVEL;
3326 sec.level = SEC_LEVEL_0;
3327 } else {
3328 sec.flags |= SEC_LEVEL;
3329 sec.level = SEC_LEVEL_1;
3331 if (ieee->set_security)
3332 ieee->set_security(ieee->dev, &sec);
3333 break;
3336 case IEEE_PARAM_PRIVACY_INVOKED:
3337 ieee->privacy_invoked = value;
3338 break;
3340 case IEEE_PARAM_AUTH_ALGS:
3341 ret = rtllib_wpa_set_auth_algs(ieee, value);
3342 break;
3344 case IEEE_PARAM_IEEE_802_1X:
3345 ieee->ieee802_1x = value;
3346 break;
3347 case IEEE_PARAM_WPAX_SELECT:
3348 spin_lock_irqsave(&ieee->wpax_suitlist_lock, flags);
3349 spin_unlock_irqrestore(&ieee->wpax_suitlist_lock, flags);
3350 break;
3352 default:
3353 printk(KERN_INFO "Unknown WPA param: %d\n", name);
3354 ret = -EOPNOTSUPP;
3357 return ret;
3360 /* implementation borrowed from hostap driver */
3361 static int rtllib_wpa_set_encryption(struct rtllib_device *ieee,
3362 struct ieee_param *param, int param_len,
3363 u8 is_mesh)
3365 int ret = 0;
3366 struct rtllib_crypto_ops *ops;
3367 struct rtllib_crypt_data **crypt;
3369 struct rtllib_security sec = {
3370 .flags = 0,
3373 param->u.crypt.err = 0;
3374 param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0';
3376 if (param_len !=
3377 (int) ((char *) param->u.crypt.key - (char *) param) +
3378 param->u.crypt.key_len) {
3379 printk(KERN_INFO "Len mismatch %d, %d\n", param_len,
3380 param->u.crypt.key_len);
3381 return -EINVAL;
3383 if (param->sta_addr[0] == 0xff && param->sta_addr[1] == 0xff &&
3384 param->sta_addr[2] == 0xff && param->sta_addr[3] == 0xff &&
3385 param->sta_addr[4] == 0xff && param->sta_addr[5] == 0xff) {
3386 if (param->u.crypt.idx >= WEP_KEYS)
3387 return -EINVAL;
3388 crypt = &ieee->crypt[param->u.crypt.idx];
3389 } else {
3390 return -EINVAL;
3393 if (strcmp(param->u.crypt.alg, "none") == 0) {
3394 if (crypt) {
3395 sec.enabled = 0;
3396 sec.level = SEC_LEVEL_0;
3397 sec.flags |= SEC_ENABLED | SEC_LEVEL;
3398 rtllib_crypt_delayed_deinit(ieee, crypt);
3400 goto done;
3402 sec.enabled = 1;
3403 sec.flags |= SEC_ENABLED;
3405 /* IPW HW cannot build TKIP MIC, host decryption still needed. */
3406 if (!(ieee->host_encrypt || ieee->host_decrypt) &&
3407 strcmp(param->u.crypt.alg, "TKIP"))
3408 goto skip_host_crypt;
3410 ops = rtllib_get_crypto_ops(param->u.crypt.alg);
3411 if (ops == NULL && strcmp(param->u.crypt.alg, "WEP") == 0) {
3412 request_module("rtllib_crypt_wep");
3413 ops = rtllib_get_crypto_ops(param->u.crypt.alg);
3414 } else if (ops == NULL && strcmp(param->u.crypt.alg, "TKIP") == 0) {
3415 request_module("rtllib_crypt_tkip");
3416 ops = rtllib_get_crypto_ops(param->u.crypt.alg);
3417 } else if (ops == NULL && strcmp(param->u.crypt.alg, "CCMP") == 0) {
3418 request_module("rtllib_crypt_ccmp");
3419 ops = rtllib_get_crypto_ops(param->u.crypt.alg);
3421 if (ops == NULL) {
3422 printk(KERN_INFO "unknown crypto alg '%s'\n",
3423 param->u.crypt.alg);
3424 param->u.crypt.err = IEEE_CRYPT_ERR_UNKNOWN_ALG;
3425 ret = -EINVAL;
3426 goto done;
3428 if (*crypt == NULL || (*crypt)->ops != ops) {
3429 struct rtllib_crypt_data *new_crypt;
3431 rtllib_crypt_delayed_deinit(ieee, crypt);
3433 new_crypt = (struct rtllib_crypt_data *)
3434 kmalloc(sizeof(*new_crypt), GFP_KERNEL);
3435 if (new_crypt == NULL) {
3436 ret = -ENOMEM;
3437 goto done;
3439 memset(new_crypt, 0, sizeof(struct rtllib_crypt_data));
3440 new_crypt->ops = ops;
3441 if (new_crypt->ops)
3442 new_crypt->priv =
3443 new_crypt->ops->init(param->u.crypt.idx);
3445 if (new_crypt->priv == NULL) {
3446 kfree(new_crypt);
3447 param->u.crypt.err = IEEE_CRYPT_ERR_CRYPT_INIT_FAILED;
3448 ret = -EINVAL;
3449 goto done;
3452 *crypt = new_crypt;
3455 if (param->u.crypt.key_len > 0 && (*crypt)->ops->set_key &&
3456 (*crypt)->ops->set_key(param->u.crypt.key,
3457 param->u.crypt.key_len, param->u.crypt.seq,
3458 (*crypt)->priv) < 0) {
3459 printk(KERN_INFO "key setting failed\n");
3460 param->u.crypt.err = IEEE_CRYPT_ERR_KEY_SET_FAILED;
3461 ret = -EINVAL;
3462 goto done;
3465 skip_host_crypt:
3466 if (param->u.crypt.set_tx) {
3467 ieee->tx_keyidx = param->u.crypt.idx;
3468 sec.active_key = param->u.crypt.idx;
3469 sec.flags |= SEC_ACTIVE_KEY;
3470 } else
3471 sec.flags &= ~SEC_ACTIVE_KEY;
3473 if (param->u.crypt.alg != NULL) {
3474 memcpy(sec.keys[param->u.crypt.idx],
3475 param->u.crypt.key,
3476 param->u.crypt.key_len);
3477 sec.key_sizes[param->u.crypt.idx] = param->u.crypt.key_len;
3478 sec.flags |= (1 << param->u.crypt.idx);
3480 if (strcmp(param->u.crypt.alg, "WEP") == 0) {
3481 sec.flags |= SEC_LEVEL;
3482 sec.level = SEC_LEVEL_1;
3483 } else if (strcmp(param->u.crypt.alg, "TKIP") == 0) {
3484 sec.flags |= SEC_LEVEL;
3485 sec.level = SEC_LEVEL_2;
3486 } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) {
3487 sec.flags |= SEC_LEVEL;
3488 sec.level = SEC_LEVEL_3;
3491 done:
3492 if (ieee->set_security)
3493 ieee->set_security(ieee->dev, &sec);
3495 /* Do not reset port if card is in Managed mode since resetting will
3496 * generate new IEEE 802.11 authentication which may end up in looping
3497 * with IEEE 802.1X. If your hardware requires a reset after WEP
3498 * configuration (for example... Prism2), implement the reset_port in
3499 * the callbacks structures used to initialize the 802.11 stack. */
3500 if (ieee->reset_on_keychange &&
3501 ieee->iw_mode != IW_MODE_INFRA &&
3502 ieee->reset_port &&
3503 ieee->reset_port(ieee->dev)) {
3504 printk(KERN_INFO "reset_port failed\n");
3505 param->u.crypt.err = IEEE_CRYPT_ERR_CARD_CONF_FAILED;
3506 return -EINVAL;
3509 return ret;
3512 inline struct sk_buff *rtllib_disauth_skb(struct rtllib_network *beacon,
3513 struct rtllib_device *ieee, u16 asRsn)
3515 struct sk_buff *skb;
3516 struct rtllib_disauth *disauth;
3517 int len = sizeof(struct rtllib_disauth) + ieee->tx_headroom;
3519 skb = dev_alloc_skb(len);
3520 if (!skb)
3521 return NULL;
3523 skb_reserve(skb, ieee->tx_headroom);
3525 disauth = (struct rtllib_disauth *) skb_put(skb,
3526 sizeof(struct rtllib_disauth));
3527 disauth->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_DEAUTH);
3528 disauth->header.duration_id = 0;
3530 memcpy(disauth->header.addr1, beacon->bssid, ETH_ALEN);
3531 memcpy(disauth->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
3532 memcpy(disauth->header.addr3, beacon->bssid, ETH_ALEN);
3534 disauth->reason = cpu_to_le16(asRsn);
3535 return skb;
3538 inline struct sk_buff *rtllib_disassociate_skb(struct rtllib_network *beacon,
3539 struct rtllib_device *ieee, u16 asRsn)
3541 struct sk_buff *skb;
3542 struct rtllib_disassoc *disass;
3543 int len = sizeof(struct rtllib_disassoc) + ieee->tx_headroom;
3544 skb = dev_alloc_skb(len);
3546 if (!skb)
3547 return NULL;
3549 skb_reserve(skb, ieee->tx_headroom);
3551 disass = (struct rtllib_disassoc *) skb_put(skb,
3552 sizeof(struct rtllib_disassoc));
3553 disass->header.frame_ctl = cpu_to_le16(RTLLIB_STYPE_DISASSOC);
3554 disass->header.duration_id = 0;
3556 memcpy(disass->header.addr1, beacon->bssid, ETH_ALEN);
3557 memcpy(disass->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
3558 memcpy(disass->header.addr3, beacon->bssid, ETH_ALEN);
3560 disass->reason = cpu_to_le16(asRsn);
3561 return skb;
3564 void SendDisassociation(struct rtllib_device *ieee, bool deauth, u16 asRsn)
3566 struct rtllib_network *beacon = &ieee->current_network;
3567 struct sk_buff *skb;
3569 if (deauth)
3570 skb = rtllib_disauth_skb(beacon, ieee, asRsn);
3571 else
3572 skb = rtllib_disassociate_skb(beacon, ieee, asRsn);
3574 if (skb)
3575 softmac_mgmt_xmit(skb, ieee);
3578 u8 rtllib_ap_sec_type(struct rtllib_device *ieee)
3580 static u8 ccmp_ie[4] = {0x00, 0x50, 0xf2, 0x04};
3581 static u8 ccmp_rsn_ie[4] = {0x00, 0x0f, 0xac, 0x04};
3582 int wpa_ie_len = ieee->wpa_ie_len;
3583 struct rtllib_crypt_data *crypt;
3584 int encrypt;
3586 crypt = ieee->crypt[ieee->tx_keyidx];
3587 encrypt = (ieee->current_network.capability & WLAN_CAPABILITY_PRIVACY)
3588 || (ieee->host_encrypt && crypt && crypt->ops &&
3589 (0 == strcmp(crypt->ops->name, "WEP")));
3591 /* simply judge */
3592 if (encrypt && (wpa_ie_len == 0)) {
3593 return SEC_ALG_WEP;
3594 } else if ((wpa_ie_len != 0)) {
3595 if (((ieee->wpa_ie[0] == 0xdd) &&
3596 (!memcmp(&(ieee->wpa_ie[14]), ccmp_ie, 4))) ||
3597 ((ieee->wpa_ie[0] == 0x30) &&
3598 (!memcmp(&ieee->wpa_ie[10], ccmp_rsn_ie, 4))))
3599 return SEC_ALG_CCMP;
3600 else
3601 return SEC_ALG_TKIP;
3602 } else {
3603 return SEC_ALG_NONE;
3607 int rtllib_wpa_supplicant_ioctl(struct rtllib_device *ieee, struct iw_point *p,
3608 u8 is_mesh)
3610 struct ieee_param *param;
3611 int ret = 0;
3613 down(&ieee->wx_sem);
3615 if (p->length < sizeof(struct ieee_param) || !p->pointer) {
3616 ret = -EINVAL;
3617 goto out;
3620 param = kmalloc(p->length, GFP_KERNEL);
3621 if (param == NULL) {
3622 ret = -ENOMEM;
3623 goto out;
3625 if (copy_from_user(param, p->pointer, p->length)) {
3626 kfree(param);
3627 ret = -EFAULT;
3628 goto out;
3631 switch (param->cmd) {
3632 case IEEE_CMD_SET_WPA_PARAM:
3633 ret = rtllib_wpa_set_param(ieee, param->u.wpa_param.name,
3634 param->u.wpa_param.value);
3635 break;
3637 case IEEE_CMD_SET_WPA_IE:
3638 ret = rtllib_wpa_set_wpa_ie(ieee, param, p->length);
3639 break;
3641 case IEEE_CMD_SET_ENCRYPTION:
3642 ret = rtllib_wpa_set_encryption(ieee, param, p->length, 0);
3643 break;
3645 case IEEE_CMD_MLME:
3646 ret = rtllib_wpa_mlme(ieee, param->u.mlme.command,
3647 param->u.mlme.reason_code);
3648 break;
3650 default:
3651 printk(KERN_INFO "Unknown WPA supplicant request: %d\n",
3652 param->cmd);
3653 ret = -EOPNOTSUPP;
3654 break;
3657 if (ret == 0 && copy_to_user(p->pointer, param, p->length))
3658 ret = -EFAULT;
3660 kfree(param);
3661 out:
3662 up(&ieee->wx_sem);
3664 return ret;
3667 void rtllib_MgntDisconnectIBSS(struct rtllib_device *rtllib)
3669 u8 OpMode;
3670 u8 i;
3671 bool bFilterOutNonAssociatedBSSID = false;
3673 rtllib->state = RTLLIB_NOLINK;
3675 for (i = 0; i < 6; i++)
3676 rtllib->current_network.bssid[i] = 0x55;
3678 rtllib->OpMode = RT_OP_MODE_NO_LINK;
3679 rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_BSSID,
3680 rtllib->current_network.bssid);
3681 OpMode = RT_OP_MODE_NO_LINK;
3682 rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_MEDIA_STATUS, &OpMode);
3683 rtllib_stop_send_beacons(rtllib);
3685 bFilterOutNonAssociatedBSSID = false;
3686 rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_CECHK_BSSID,
3687 (u8 *)(&bFilterOutNonAssociatedBSSID));
3688 notify_wx_assoc_event(rtllib);
3692 void rtllib_MlmeDisassociateRequest(struct rtllib_device *rtllib, u8 *asSta,
3693 u8 asRsn)
3695 u8 i;
3696 u8 OpMode;
3698 RemovePeerTS(rtllib, asSta);
3701 if (memcpy(rtllib->current_network.bssid, asSta, 6) == 0) {
3702 rtllib->state = RTLLIB_NOLINK;
3704 for (i = 0; i < 6; i++)
3705 rtllib->current_network.bssid[i] = 0x22;
3706 OpMode = RT_OP_MODE_NO_LINK;
3707 rtllib->OpMode = RT_OP_MODE_NO_LINK;
3708 rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_MEDIA_STATUS,
3709 (u8 *)(&OpMode));
3710 rtllib_disassociate(rtllib);
3712 rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_BSSID,
3713 rtllib->current_network.bssid);
3719 void
3720 rtllib_MgntDisconnectAP(
3721 struct rtllib_device *rtllib,
3722 u8 asRsn
3725 bool bFilterOutNonAssociatedBSSID = false;
3727 bFilterOutNonAssociatedBSSID = false;
3728 rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_CECHK_BSSID,
3729 (u8 *)(&bFilterOutNonAssociatedBSSID));
3730 rtllib_MlmeDisassociateRequest(rtllib, rtllib->current_network.bssid,
3731 asRsn);
3733 rtllib->state = RTLLIB_NOLINK;
3736 bool rtllib_MgntDisconnect(struct rtllib_device *rtllib, u8 asRsn)
3738 if (rtllib->ps != RTLLIB_PS_DISABLED)
3739 rtllib->sta_wake_up(rtllib->dev);
3741 if (rtllib->state == RTLLIB_LINKED) {
3742 if (rtllib->iw_mode == IW_MODE_ADHOC)
3743 rtllib_MgntDisconnectIBSS(rtllib);
3744 if (rtllib->iw_mode == IW_MODE_INFRA)
3745 rtllib_MgntDisconnectAP(rtllib, asRsn);
3749 return true;
3752 void notify_wx_assoc_event(struct rtllib_device *ieee)
3754 union iwreq_data wrqu;
3756 if (ieee->cannot_notify)
3757 return;
3759 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
3760 if (ieee->state == RTLLIB_LINKED)
3761 memcpy(wrqu.ap_addr.sa_data, ieee->current_network.bssid,
3762 ETH_ALEN);
3763 else {
3765 printk(KERN_INFO "%s(): Tell user space disconnected\n",
3766 __func__);
3767 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
3769 wireless_send_event(ieee->dev, SIOCGIWAP, &wrqu, NULL);