1 // SPDX-License-Identifier: GPL-2.0
2 /* IEEE 802.11 SoftMAC layer
3 * Copyright (c) 2005 Andrea Merello <andrea.merello@gmail.com>
5 * Mostly extracted from the rtl8180-sa2400 driver for the
6 * in-kernel generic ieee802.11 stack.
8 * Few lines might be stolen from other part of the rtllib
9 * stack. Copyright who own it's copyright
11 * WPA code stolen from the ipw2200 driver.
12 * Copyright who own it's copyright.
16 #include <linux/random.h>
17 #include <linux/delay.h>
18 #include <linux/uaccess.h>
19 #include <linux/etherdevice.h>
20 #include <linux/ieee80211.h>
22 static void rtllib_sta_wakeup(struct rtllib_device *ieee, short nl);
24 static short rtllib_is_54g(struct rtllib_network *net)
26 return (net->rates_ex_len > 0) || (net->rates_len > 4);
29 /* returns the total length needed for placing the RATE MFIE
30 * tag and the EXTENDED RATE MFIE tag if needed.
31 * It encludes two bytes per tag for the tag itself and its len
33 static unsigned int rtllib_MFIE_rate_len(struct rtllib_device *ieee)
35 unsigned int rate_len = 0;
37 rate_len = RTLLIB_CCK_RATE_LEN + 2;
38 rate_len += RTLLIB_OFDM_RATE_LEN + 2;
43 /* place the MFIE rate, tag to the memory (double) pointed.
44 * Then it updates the pointer so that
45 * it points after the new MFIE tag added.
47 static void rtllib_mfie_brate(struct rtllib_device *ieee, u8 **tag_p)
51 *tag++ = MFIE_TYPE_RATES;
53 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_1MB;
54 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_2MB;
55 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_5MB;
56 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_11MB;
58 /* We may add an option for custom rates that specific HW
64 static void rtllib_mfie_grate(struct rtllib_device *ieee, u8 **tag_p)
68 *tag++ = MFIE_TYPE_RATES_EX;
70 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_6MB;
71 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_9MB;
72 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_12MB;
73 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_18MB;
74 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_24MB;
75 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_36MB;
76 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_48MB;
77 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_54MB;
79 /* We may add an option for custom rates that specific HW might
85 static void rtllib_wmm_info(struct rtllib_device *ieee, u8 **tag_p)
89 *tag++ = MFIE_TYPE_GENERIC;
101 static void rtllib_turbo_info(struct rtllib_device *ieee, u8 **tag_p)
105 *tag++ = MFIE_TYPE_GENERIC;
116 netdev_alert(ieee->dev, "This is enable turbo mode IE process\n");
119 static void enqueue_mgmt(struct rtllib_device *ieee, struct sk_buff *skb)
123 nh = (ieee->mgmt_queue_head + 1) % MGMT_QUEUE_NUM;
125 /* if the queue is full but we have newer frames then
126 * just overwrites the oldest.
128 * if (nh == ieee->mgmt_queue_tail)
131 ieee->mgmt_queue_head = nh;
132 ieee->mgmt_queue_ring[nh] = skb;
135 static void init_mgmt_queue(struct rtllib_device *ieee)
137 ieee->mgmt_queue_tail = 0;
138 ieee->mgmt_queue_head = 0;
141 u8 MgntQuery_TxRateExcludeCCKRates(struct rtllib_device *ieee)
147 for (i = 0; i < ieee->current_network.rates_len; i++) {
148 basic_rate = ieee->current_network.rates[i] & 0x7F;
149 if (!rtllib_is_cck_rate(basic_rate)) {
150 if (query_rate == 0) {
151 query_rate = basic_rate;
153 if (basic_rate < query_rate)
154 query_rate = basic_rate;
159 if (query_rate == 0) {
161 netdev_info(ieee->dev, "No basic_rate found!!\n");
166 static u8 MgntQuery_MgntFrameTxRate(struct rtllib_device *ieee)
168 struct rt_hi_throughput *ht_info = ieee->ht_info;
171 if (ht_info->iot_action & HT_IOT_ACT_MGNT_USE_CCK_6M)
174 rate = ieee->basic_rate & 0x7f;
182 inline void softmac_mgmt_xmit(struct sk_buff *skb, struct rtllib_device *ieee)
185 short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
186 struct ieee80211_hdr_3addr *header =
187 (struct ieee80211_hdr_3addr *)skb->data;
189 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 8);
191 spin_lock_irqsave(&ieee->lock, flags);
193 /* called with 2nd param 0, no mgmt lock required */
194 rtllib_sta_wakeup(ieee, 0);
196 if (ieee80211_is_beacon(header->frame_control))
197 tcb_desc->queue_index = BEACON_QUEUE;
199 tcb_desc->queue_index = MGNT_QUEUE;
201 if (ieee->disable_mgnt_queue)
202 tcb_desc->queue_index = HIGH_QUEUE;
204 tcb_desc->data_rate = MgntQuery_MgntFrameTxRate(ieee);
205 tcb_desc->ratr_index = 7;
206 tcb_desc->tx_dis_rate_fallback = 1;
207 tcb_desc->tx_use_drv_assinged_rate = 1;
209 if (ieee->queue_stop) {
210 enqueue_mgmt(ieee, skb);
212 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
214 if (ieee->seq_ctrl[0] == 0xFFF)
215 ieee->seq_ctrl[0] = 0;
219 /* avoid watchdog triggers */
220 ieee->softmac_data_hard_start_xmit(skb, ieee->dev,
224 spin_unlock_irqrestore(&ieee->lock, flags);
226 spin_unlock_irqrestore(&ieee->lock, flags);
227 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags);
229 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
231 if (ieee->seq_ctrl[0] == 0xFFF)
232 ieee->seq_ctrl[0] = 0;
236 /* check whether the managed packet queued greater than 5 */
237 if (!ieee->check_nic_enough_desc(ieee->dev,
238 tcb_desc->queue_index) ||
239 skb_queue_len(&ieee->skb_waitq[tcb_desc->queue_index]) ||
241 /* insert the skb packet to the management queue
243 * as for the completion function, it does not need
244 * to check it any more.
246 netdev_info(ieee->dev,
247 "%s():insert to waitqueue, queue_index:%d!\n",
248 __func__, tcb_desc->queue_index);
249 skb_queue_tail(&ieee->skb_waitq[tcb_desc->queue_index],
252 ieee->softmac_hard_start_xmit(skb, ieee->dev);
254 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags);
259 softmac_ps_mgmt_xmit(struct sk_buff *skb,
260 struct rtllib_device *ieee)
262 short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
263 struct ieee80211_hdr_3addr *header =
264 (struct ieee80211_hdr_3addr *)skb->data;
266 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 8);
268 fc = le16_to_cpu(header->frame_control);
269 type = WLAN_FC_GET_TYPE(fc);
270 stype = WLAN_FC_GET_STYPE(fc);
272 if (stype != IEEE80211_STYPE_PSPOLL)
273 tcb_desc->queue_index = MGNT_QUEUE;
275 tcb_desc->queue_index = HIGH_QUEUE;
277 if (ieee->disable_mgnt_queue)
278 tcb_desc->queue_index = HIGH_QUEUE;
280 tcb_desc->data_rate = MgntQuery_MgntFrameTxRate(ieee);
281 tcb_desc->ratr_index = 7;
282 tcb_desc->tx_dis_rate_fallback = 1;
283 tcb_desc->tx_use_drv_assinged_rate = 1;
285 if (type != RTLLIB_FTYPE_CTL) {
286 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
288 if (ieee->seq_ctrl[0] == 0xFFF)
289 ieee->seq_ctrl[0] = 0;
293 /* avoid watchdog triggers */
294 ieee->softmac_data_hard_start_xmit(skb, ieee->dev,
298 if (type != RTLLIB_FTYPE_CTL) {
299 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
301 if (ieee->seq_ctrl[0] == 0xFFF)
302 ieee->seq_ctrl[0] = 0;
306 ieee->softmac_hard_start_xmit(skb, ieee->dev);
310 static inline struct sk_buff *rtllib_probe_req(struct rtllib_device *ieee)
312 unsigned int len, rate_len;
315 struct rtllib_probe_request *req;
317 len = ieee->current_network.ssid_len;
319 rate_len = rtllib_MFIE_rate_len(ieee);
321 skb = dev_alloc_skb(sizeof(struct rtllib_probe_request) +
322 2 + len + rate_len + ieee->tx_headroom);
327 skb_reserve(skb, ieee->tx_headroom);
329 req = skb_put(skb, sizeof(struct rtllib_probe_request));
330 req->header.frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
331 req->header.duration_id = 0;
333 eth_broadcast_addr(req->header.addr1);
334 ether_addr_copy(req->header.addr2, ieee->dev->dev_addr);
335 eth_broadcast_addr(req->header.addr3);
337 tag = skb_put(skb, len + 2 + rate_len);
339 *tag++ = MFIE_TYPE_SSID;
341 memcpy(tag, ieee->current_network.ssid, len);
344 rtllib_mfie_brate(ieee, &tag);
345 rtllib_mfie_grate(ieee, &tag);
350 /* Enables network monitor mode, all rx packets will be received. */
351 void rtllib_enable_net_monitor_mode(struct net_device *dev,
354 struct rtllib_device *ieee = netdev_priv_rsl(dev);
356 netdev_info(dev, "========>Enter Monitor Mode\n");
358 ieee->AllowAllDestAddrHandler(dev, true, !init_state);
361 /* Disables network monitor mode. Only packets destinated to
362 * us will be received.
364 void rtllib_disable_net_monitor_mode(struct net_device *dev,
367 struct rtllib_device *ieee = netdev_priv_rsl(dev);
369 netdev_info(dev, "========>Exit Monitor Mode\n");
371 ieee->AllowAllDestAddrHandler(dev, false, !init_state);
374 static void rtllib_send_probe(struct rtllib_device *ieee)
378 skb = rtllib_probe_req(ieee);
380 softmac_mgmt_xmit(skb, ieee);
381 ieee->softmac_stats.tx_probe_rq++;
385 static void rtllib_send_probe_requests(struct rtllib_device *ieee)
387 if (ieee->softmac_features & IEEE_SOFTMAC_PROBERQ) {
388 rtllib_send_probe(ieee);
389 rtllib_send_probe(ieee);
393 /* this performs syncro scan blocking the caller until all channels
394 * in the allowed channel map has been checked.
396 static void rtllib_softmac_scan_syncro(struct rtllib_device *ieee)
398 union iwreq_data wrqu;
401 ieee->be_scan_inprogress = true;
403 mutex_lock(&ieee->scan_mutex);
408 if (ch > MAX_CHANNEL_NUMBER)
409 goto out; /* scan completed */
410 } while (!ieee->active_channel_map[ch]);
412 /* this function can be called in two situations
413 * 1- We have switched to ad-hoc mode and we are
414 * performing a complete syncro scan before conclude
415 * there are no interesting cell and to create a
416 * new one. In this case the link state is
417 * MAC80211_NOLINK until we found an interesting cell.
418 * If so the ieee8021_new_net, called by the RX path
419 * will set the state to MAC80211_LINKED, so we stop
421 * 2- We are linked and the root uses run iwlist scan.
422 * So we switch to MAC80211_LINKED_SCANNING to remember
423 * that we are still logically linked (not interested in
424 * new network events, despite for updating the net list,
425 * but we are temporarly 'unlinked' as the driver shall
426 * not filter RX frames and the channel is changing.
427 * So the only situation in which are interested is to check
428 * if the state become LINKED because of the #1 situation
431 if (ieee->link_state == MAC80211_LINKED)
433 if (ieee->sync_scan_hurryup) {
434 netdev_info(ieee->dev,
435 "============>sync_scan_hurryup out\n");
439 ieee->set_chan(ieee->dev, ch);
440 if (ieee->active_channel_map[ch] == 1)
441 rtllib_send_probe_requests(ieee);
443 /* this prevent excessive time wait when we
444 * need to wait for a syncro scan to end..
446 msleep_interruptible_rsl(RTLLIB_SOFTMAC_SCAN_TIME);
449 ieee->actscanning = false;
450 ieee->sync_scan_hurryup = 0;
452 mutex_unlock(&ieee->scan_mutex);
454 ieee->be_scan_inprogress = false;
456 memset(&wrqu, 0, sizeof(wrqu));
457 wireless_send_event(ieee->dev, SIOCGIWSCAN, &wrqu, NULL);
460 static void rtllib_softmac_scan_wq(void *data)
462 struct rtllib_device *ieee = container_of_dwork_rsl(data,
463 struct rtllib_device, softmac_scan_wq);
464 u8 last_channel = ieee->current_network.channel;
468 if (rtllib_act_scanning(ieee, true))
471 mutex_lock(&ieee->scan_mutex);
473 if (ieee->rf_power_state == rf_off) {
474 netdev_info(ieee->dev,
475 "======>%s():rf state is rf_off, return\n",
481 ieee->current_network.channel =
482 (ieee->current_network.channel + 1) %
484 if (ieee->scan_watch_dog++ > MAX_CHANNEL_NUMBER) {
485 if (!ieee->active_channel_map[ieee->current_network.channel])
486 ieee->current_network.channel = 6;
487 goto out; /* no good chans */
489 } while (!ieee->active_channel_map[ieee->current_network.channel]);
491 if (ieee->scanning_continue == 0)
494 ieee->set_chan(ieee->dev, ieee->current_network.channel);
496 if (ieee->active_channel_map[ieee->current_network.channel] == 1)
497 rtllib_send_probe_requests(ieee);
499 schedule_delayed_work(&ieee->softmac_scan_wq,
500 msecs_to_jiffies(RTLLIB_SOFTMAC_SCAN_TIME));
502 mutex_unlock(&ieee->scan_mutex);
506 ieee->current_network.channel = last_channel;
509 ieee->actscanning = false;
510 ieee->scan_watch_dog = 0;
511 ieee->scanning_continue = 0;
512 mutex_unlock(&ieee->scan_mutex);
515 static void rtllib_softmac_stop_scan(struct rtllib_device *ieee)
517 mutex_lock(&ieee->scan_mutex);
518 ieee->scan_watch_dog = 0;
519 if (ieee->scanning_continue == 1) {
520 ieee->scanning_continue = 0;
521 ieee->actscanning = false;
522 mutex_unlock(&ieee->scan_mutex);
523 cancel_delayed_work_sync(&ieee->softmac_scan_wq);
525 mutex_unlock(&ieee->scan_mutex);
529 void rtllib_stop_scan(struct rtllib_device *ieee)
531 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
532 rtllib_softmac_stop_scan(ieee);
534 EXPORT_SYMBOL(rtllib_stop_scan);
536 void rtllib_stop_scan_syncro(struct rtllib_device *ieee)
538 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
539 ieee->sync_scan_hurryup = 1;
541 EXPORT_SYMBOL(rtllib_stop_scan_syncro);
543 bool rtllib_act_scanning(struct rtllib_device *ieee, bool sync_scan)
545 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
547 return ieee->be_scan_inprogress;
549 return ieee->actscanning || ieee->be_scan_inprogress;
551 return test_bit(STATUS_SCANNING, &ieee->status);
554 EXPORT_SYMBOL(rtllib_act_scanning);
556 /* called with ieee->lock held */
557 static void rtllib_start_scan(struct rtllib_device *ieee)
559 ieee->rtllib_ips_leave_wq(ieee->dev);
561 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
562 if (ieee->scanning_continue == 0) {
563 ieee->actscanning = true;
564 ieee->scanning_continue = 1;
565 schedule_delayed_work(&ieee->softmac_scan_wq, 0);
570 /* called with wx_mutex held */
571 void rtllib_start_scan_syncro(struct rtllib_device *ieee)
573 ieee->sync_scan_hurryup = 0;
574 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
575 rtllib_softmac_scan_syncro(ieee);
577 EXPORT_SYMBOL(rtllib_start_scan_syncro);
579 static inline struct sk_buff *
580 rtllib_authentication_req(struct rtllib_network *beacon,
581 struct rtllib_device *ieee,
582 int challengelen, u8 *daddr)
585 struct rtllib_authentication *auth;
588 len = sizeof(struct rtllib_authentication) + challengelen +
589 ieee->tx_headroom + 4;
590 skb = dev_alloc_skb(len);
595 skb_reserve(skb, ieee->tx_headroom);
597 auth = skb_put(skb, sizeof(struct rtllib_authentication));
599 auth->header.frame_control = cpu_to_le16(IEEE80211_STYPE_AUTH);
601 auth->header.frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
603 auth->header.duration_id = cpu_to_le16(0x013a);
604 ether_addr_copy(auth->header.addr1, beacon->bssid);
605 ether_addr_copy(auth->header.addr2, ieee->dev->dev_addr);
606 ether_addr_copy(auth->header.addr3, beacon->bssid);
607 if (ieee->auth_mode == 0)
608 auth->algorithm = WLAN_AUTH_OPEN;
609 else if (ieee->auth_mode == 1)
610 auth->algorithm = cpu_to_le16(WLAN_AUTH_SHARED_KEY);
611 else if (ieee->auth_mode == 2)
612 auth->algorithm = WLAN_AUTH_OPEN;
613 auth->transaction = cpu_to_le16(ieee->associate_seq);
614 ieee->associate_seq++;
616 auth->status = cpu_to_le16(WLAN_STATUS_SUCCESS);
621 static struct sk_buff *rtllib_null_func(struct rtllib_device *ieee, short pwr)
624 struct ieee80211_hdr_3addr *hdr;
626 skb = dev_alloc_skb(sizeof(struct ieee80211_hdr_3addr) + ieee->tx_headroom);
630 skb_reserve(skb, ieee->tx_headroom);
632 hdr = skb_put(skb, sizeof(struct ieee80211_hdr_3addr));
634 ether_addr_copy(hdr->addr1, ieee->current_network.bssid);
635 ether_addr_copy(hdr->addr2, ieee->dev->dev_addr);
636 ether_addr_copy(hdr->addr3, ieee->current_network.bssid);
638 hdr->frame_control = cpu_to_le16(RTLLIB_FTYPE_DATA |
639 IEEE80211_STYPE_NULLFUNC | IEEE80211_FCTL_TODS |
640 (pwr ? IEEE80211_FCTL_PM : 0));
645 static struct sk_buff *rtllib_pspoll_func(struct rtllib_device *ieee)
648 struct ieee80211_pspoll *hdr;
650 skb = dev_alloc_skb(sizeof(struct ieee80211_pspoll) + ieee->tx_headroom);
654 skb_reserve(skb, ieee->tx_headroom);
656 hdr = skb_put(skb, sizeof(struct ieee80211_pspoll));
658 ether_addr_copy(hdr->bssid, ieee->current_network.bssid);
659 ether_addr_copy(hdr->ta, ieee->dev->dev_addr);
661 hdr->aid = cpu_to_le16(ieee->assoc_id | 0xc000);
662 hdr->frame_control = cpu_to_le16(RTLLIB_FTYPE_CTL | IEEE80211_STYPE_PSPOLL |
668 static inline int SecIsInPMKIDList(struct rtllib_device *ieee, u8 *bssid)
673 if ((ieee->PMKIDList[i].used) &&
674 (memcmp(ieee->PMKIDList[i].Bssid, bssid, ETH_ALEN) == 0))
677 } while (i < NUM_PMKID_CACHE);
679 if (i == NUM_PMKID_CACHE)
684 static inline struct sk_buff *
685 rtllib_association_req(struct rtllib_network *beacon,
686 struct rtllib_device *ieee)
689 struct rtllib_assoc_request_frame *hdr;
692 u8 *ht_cap_buf = NULL;
694 u8 *realtek_ie_buf = NULL;
695 u8 realtek_ie_len = 0;
696 int wpa_ie_len = ieee->wpa_ie_len;
697 int wps_ie_len = ieee->wps_ie_len;
698 unsigned int ckip_ie_len = 0;
699 unsigned int ccxrm_ie_len = 0;
700 unsigned int cxvernum_ie_len = 0;
701 struct lib80211_crypt_data *crypt;
705 unsigned int rate_len = (beacon->rates_len ?
706 (beacon->rates_len + 2) : 0) +
707 (beacon->rates_ex_len ? (beacon->rates_ex_len) +
710 unsigned int wmm_info_len = beacon->qos_data.supported ? 9 : 0;
711 unsigned int turbo_info_len = beacon->Turbo_Enable ? 9 : 0;
715 crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
717 encrypt = crypt && crypt->ops &&
718 ((strcmp(crypt->ops->name, "R-WEP") == 0 ||
723 if ((ieee->rtllib_ap_sec_type &&
724 (ieee->rtllib_ap_sec_type(ieee) & SEC_ALG_TKIP)) ||
725 ieee->bForcedBgMode) {
726 ieee->ht_info->enable_ht = 0;
727 ieee->mode = WIRELESS_MODE_G;
730 if (ieee->ht_info->current_ht_support && ieee->ht_info->enable_ht) {
731 ht_cap_buf = (u8 *)&ieee->ht_info->SelfHTCap;
732 ht_cap_len = sizeof(ieee->ht_info->SelfHTCap);
733 ht_construct_capability_element(ieee, ht_cap_buf, &ht_cap_len,
735 if (ieee->ht_info->current_rt2rt_aggregation) {
736 realtek_ie_buf = ieee->ht_info->sz_rt2rt_agg_buf;
738 sizeof(ieee->ht_info->sz_rt2rt_agg_buf);
739 ht_construct_rt2rt_agg_element(ieee, realtek_ie_buf,
744 if (beacon->bCkipSupported)
745 ckip_ie_len = 30 + 2;
746 if (beacon->bCcxRmEnable)
747 ccxrm_ie_len = 6 + 2;
748 if (beacon->BssCcxVerNumber >= 2)
749 cxvernum_ie_len = 5 + 2;
751 PMKCacheIdx = SecIsInPMKIDList(ieee, ieee->current_network.bssid);
752 if (PMKCacheIdx >= 0) {
754 netdev_info(ieee->dev, "[PMK cache]: WPA2 IE length: %x\n",
757 len = sizeof(struct rtllib_assoc_request_frame) + 2
771 skb = dev_alloc_skb(len);
776 skb_reserve(skb, ieee->tx_headroom);
778 hdr = skb_put(skb, sizeof(struct rtllib_assoc_request_frame) + 2);
780 hdr->header.frame_control = cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ);
781 hdr->header.duration_id = cpu_to_le16(37);
782 ether_addr_copy(hdr->header.addr1, beacon->bssid);
783 ether_addr_copy(hdr->header.addr2, ieee->dev->dev_addr);
784 ether_addr_copy(hdr->header.addr3, beacon->bssid);
786 ether_addr_copy(ieee->ap_mac_addr, beacon->bssid);
788 hdr->capability = cpu_to_le16(WLAN_CAPABILITY_ESS);
789 if (beacon->capability & WLAN_CAPABILITY_PRIVACY)
790 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
792 if (beacon->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
793 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
795 if (beacon->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
796 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
798 hdr->listen_interval = cpu_to_le16(beacon->listen_interval);
800 hdr->info_element[0].id = MFIE_TYPE_SSID;
802 hdr->info_element[0].len = beacon->ssid_len;
803 skb_put_data(skb, beacon->ssid, beacon->ssid_len);
805 tag = skb_put(skb, rate_len);
807 if (beacon->rates_len) {
808 *tag++ = MFIE_TYPE_RATES;
809 *tag++ = beacon->rates_len;
810 for (i = 0; i < beacon->rates_len; i++)
811 *tag++ = beacon->rates[i];
814 if (beacon->rates_ex_len) {
815 *tag++ = MFIE_TYPE_RATES_EX;
816 *tag++ = beacon->rates_ex_len;
817 for (i = 0; i < beacon->rates_ex_len; i++)
818 *tag++ = beacon->rates_ex[i];
821 if (beacon->bCkipSupported) {
822 static const u8 AironetIeOui[] = {0x00, 0x01, 0x66};
823 u8 CcxAironetBuf[30];
824 struct octet_string osCcxAironetIE;
826 memset(CcxAironetBuf, 0, 30);
827 osCcxAironetIE.Octet = CcxAironetBuf;
828 osCcxAironetIE.Length = sizeof(CcxAironetBuf);
829 memcpy(osCcxAironetIE.Octet, AironetIeOui,
830 sizeof(AironetIeOui));
832 osCcxAironetIE.Octet[IE_CISCO_FLAG_POSITION] |=
833 (SUPPORT_CKIP_PK | SUPPORT_CKIP_MIC);
834 tag = skb_put(skb, ckip_ie_len);
835 *tag++ = MFIE_TYPE_AIRONET;
836 *tag++ = osCcxAironetIE.Length;
837 memcpy(tag, osCcxAironetIE.Octet, osCcxAironetIE.Length);
838 tag += osCcxAironetIE.Length;
841 if (beacon->bCcxRmEnable) {
842 static const u8 CcxRmCapBuf[] = {0x00, 0x40, 0x96, 0x01, 0x01,
844 struct octet_string osCcxRmCap;
846 osCcxRmCap.Octet = (u8 *)CcxRmCapBuf;
847 osCcxRmCap.Length = sizeof(CcxRmCapBuf);
848 tag = skb_put(skb, ccxrm_ie_len);
849 *tag++ = MFIE_TYPE_GENERIC;
850 *tag++ = osCcxRmCap.Length;
851 memcpy(tag, osCcxRmCap.Octet, osCcxRmCap.Length);
852 tag += osCcxRmCap.Length;
855 if (beacon->BssCcxVerNumber >= 2) {
856 u8 CcxVerNumBuf[] = {0x00, 0x40, 0x96, 0x03, 0x00};
857 struct octet_string osCcxVerNum;
859 CcxVerNumBuf[4] = beacon->BssCcxVerNumber;
860 osCcxVerNum.Octet = CcxVerNumBuf;
861 osCcxVerNum.Length = sizeof(CcxVerNumBuf);
862 tag = skb_put(skb, cxvernum_ie_len);
863 *tag++ = MFIE_TYPE_GENERIC;
864 *tag++ = osCcxVerNum.Length;
865 memcpy(tag, osCcxVerNum.Octet, osCcxVerNum.Length);
866 tag += osCcxVerNum.Length;
868 if (ieee->ht_info->current_ht_support && ieee->ht_info->enable_ht) {
869 if (ieee->ht_info->peer_ht_spec_ver != HT_SPEC_VER_EWC) {
870 tag = skb_put(skb, ht_cap_len);
871 *tag++ = MFIE_TYPE_HT_CAP;
872 *tag++ = ht_cap_len - 2;
873 memcpy(tag, ht_cap_buf, ht_cap_len - 2);
874 tag += ht_cap_len - 2;
879 skb_put_data(skb, ieee->wpa_ie, ieee->wpa_ie_len);
881 if (PMKCacheIdx >= 0) {
882 tag = skb_put(skb, 18);
885 memcpy((tag + 2), &ieee->PMKIDList[PMKCacheIdx].PMKID,
890 tag = skb_put(skb, wmm_info_len);
891 rtllib_wmm_info(ieee, &tag);
894 if (wps_ie_len && ieee->wps_ie)
895 skb_put_data(skb, ieee->wps_ie, wps_ie_len);
897 if (turbo_info_len) {
898 tag = skb_put(skb, turbo_info_len);
899 rtllib_turbo_info(ieee, &tag);
902 if (ieee->ht_info->current_ht_support && ieee->ht_info->enable_ht) {
903 if (ieee->ht_info->peer_ht_spec_ver == HT_SPEC_VER_EWC) {
904 tag = skb_put(skb, ht_cap_len);
905 *tag++ = MFIE_TYPE_GENERIC;
906 *tag++ = ht_cap_len - 2;
907 memcpy(tag, ht_cap_buf, ht_cap_len - 2);
908 tag += ht_cap_len - 2;
911 if (ieee->ht_info->current_rt2rt_aggregation) {
912 tag = skb_put(skb, realtek_ie_len);
913 *tag++ = MFIE_TYPE_GENERIC;
914 *tag++ = realtek_ie_len - 2;
915 memcpy(tag, realtek_ie_buf, realtek_ie_len - 2);
919 kfree(ieee->assocreq_ies);
920 ieee->assocreq_ies = NULL;
921 ies = &hdr->info_element[0].id;
922 ieee->assocreq_ies_len = (skb->data + skb->len) - ies;
923 ieee->assocreq_ies = kmemdup(ies, ieee->assocreq_ies_len, GFP_ATOMIC);
924 if (!ieee->assocreq_ies)
925 ieee->assocreq_ies_len = 0;
930 static void rtllib_associate_abort(struct rtllib_device *ieee)
934 spin_lock_irqsave(&ieee->lock, flags);
936 ieee->associate_seq++;
938 /* don't scan, and avoid to have the RX path possibily
939 * try again to associate. Even do not react to AUTH or
940 * ASSOC response. Just wait for the retry wq to be scheduled.
941 * Here we will check if there are good nets to associate
942 * with, so we retry or just get back to NO_LINK and scanning
944 if (ieee->link_state == RTLLIB_ASSOCIATING_AUTHENTICATING) {
945 netdev_dbg(ieee->dev, "Authentication failed\n");
946 ieee->softmac_stats.no_auth_rs++;
948 netdev_dbg(ieee->dev, "Association failed\n");
949 ieee->softmac_stats.no_ass_rs++;
952 ieee->link_state = RTLLIB_ASSOCIATING_RETRY;
954 schedule_delayed_work(&ieee->associate_retry_wq,
955 RTLLIB_SOFTMAC_ASSOC_RETRY_TIME);
957 spin_unlock_irqrestore(&ieee->lock, flags);
960 static void rtllib_associate_abort_cb(struct timer_list *t)
962 struct rtllib_device *dev = from_timer(dev, t, associate_timer);
964 rtllib_associate_abort(dev);
967 static void rtllib_associate_step1(struct rtllib_device *ieee, u8 *daddr)
969 struct rtllib_network *beacon = &ieee->current_network;
972 netdev_dbg(ieee->dev, "Stopping scan\n");
974 ieee->softmac_stats.tx_auth_rq++;
976 skb = rtllib_authentication_req(beacon, ieee, 0, daddr);
979 rtllib_associate_abort(ieee);
981 ieee->link_state = RTLLIB_ASSOCIATING_AUTHENTICATING;
982 netdev_dbg(ieee->dev, "Sending authentication request\n");
983 softmac_mgmt_xmit(skb, ieee);
984 if (!timer_pending(&ieee->associate_timer)) {
985 ieee->associate_timer.expires = jiffies + (HZ / 2);
986 add_timer(&ieee->associate_timer);
991 static void rtllib_auth_challenge(struct rtllib_device *ieee, u8 *challenge,
996 struct rtllib_network *beacon = &ieee->current_network;
998 ieee->associate_seq++;
999 ieee->softmac_stats.tx_auth_rq++;
1001 skb = rtllib_authentication_req(beacon, ieee, chlen + 2, beacon->bssid);
1004 rtllib_associate_abort(ieee);
1006 c = skb_put(skb, chlen + 2);
1007 *(c++) = MFIE_TYPE_CHALLENGE;
1009 memcpy(c, challenge, chlen);
1011 netdev_dbg(ieee->dev,
1012 "Sending authentication challenge response\n");
1014 rtllib_encrypt_fragment(ieee, skb,
1015 sizeof(struct ieee80211_hdr_3addr));
1017 softmac_mgmt_xmit(skb, ieee);
1018 mod_timer(&ieee->associate_timer, jiffies + (HZ / 2));
1023 static void rtllib_associate_step2(struct rtllib_device *ieee)
1025 struct sk_buff *skb;
1026 struct rtllib_network *beacon = &ieee->current_network;
1028 del_timer_sync(&ieee->associate_timer);
1030 netdev_dbg(ieee->dev, "Sending association request\n");
1032 ieee->softmac_stats.tx_ass_rq++;
1033 skb = rtllib_association_req(beacon, ieee);
1035 rtllib_associate_abort(ieee);
1037 softmac_mgmt_xmit(skb, ieee);
1038 mod_timer(&ieee->associate_timer, jiffies + (HZ / 2));
1042 static void rtllib_associate_complete_wq(void *data)
1044 struct rtllib_device *ieee = (struct rtllib_device *)
1046 struct rtllib_device,
1047 associate_complete_wq);
1048 struct rt_pwr_save_ctrl *psc = &ieee->pwr_save_ctrl;
1050 netdev_info(ieee->dev, "Associated successfully with %pM\n",
1051 ieee->current_network.bssid);
1052 netdev_info(ieee->dev, "normal associate\n");
1053 notify_wx_assoc_event(ieee);
1055 netif_carrier_on(ieee->dev);
1056 ieee->is_roaming = false;
1057 if (rtllib_is_54g(&ieee->current_network)) {
1059 netdev_info(ieee->dev, "Using G rates:%d\n", ieee->rate);
1062 ieee->set_wireless_mode(ieee->dev, WIRELESS_MODE_B);
1063 netdev_info(ieee->dev, "Using B rates:%d\n", ieee->rate);
1065 if (ieee->ht_info->current_ht_support && ieee->ht_info->enable_ht) {
1066 netdev_info(ieee->dev, "Successfully associated, ht enabled\n");
1067 ht_on_assoc_rsp(ieee);
1069 netdev_info(ieee->dev,
1070 "Successfully associated, ht not enabled(%d, %d)\n",
1071 ieee->ht_info->current_ht_support,
1072 ieee->ht_info->enable_ht);
1073 memset(ieee->dot11ht_oper_rate_set, 0, 16);
1075 ieee->link_detect_info.SlotNum = 2 * (1 +
1076 ieee->current_network.beacon_interval /
1078 if (ieee->link_detect_info.NumRecvBcnInPeriod == 0 ||
1079 ieee->link_detect_info.NumRecvDataInPeriod == 0) {
1080 ieee->link_detect_info.NumRecvBcnInPeriod = 1;
1081 ieee->link_detect_info.NumRecvDataInPeriod = 1;
1083 psc->LpsIdleCount = 0;
1084 ieee->link_change(ieee->dev);
1088 static void rtllib_sta_send_associnfo(struct rtllib_device *ieee)
1092 static void rtllib_associate_complete(struct rtllib_device *ieee)
1094 del_timer_sync(&ieee->associate_timer);
1096 ieee->link_state = MAC80211_LINKED;
1097 rtllib_sta_send_associnfo(ieee);
1099 schedule_work(&ieee->associate_complete_wq);
1102 static void rtllib_associate_procedure_wq(void *data)
1104 struct rtllib_device *ieee = container_of_dwork_rsl(data,
1105 struct rtllib_device,
1106 associate_procedure_wq);
1107 rtllib_stop_scan_syncro(ieee);
1108 ieee->rtllib_ips_leave(ieee->dev);
1109 mutex_lock(&ieee->wx_mutex);
1111 rtllib_stop_scan(ieee);
1112 ht_set_connect_bw_mode(ieee, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
1113 if (ieee->rf_power_state == rf_off) {
1114 ieee->rtllib_ips_leave_wq(ieee->dev);
1115 mutex_unlock(&ieee->wx_mutex);
1118 ieee->associate_seq = 1;
1120 rtllib_associate_step1(ieee, ieee->current_network.bssid);
1122 mutex_unlock(&ieee->wx_mutex);
1125 inline void rtllib_softmac_new_net(struct rtllib_device *ieee,
1126 struct rtllib_network *net)
1128 u8 tmp_ssid[IW_ESSID_MAX_SIZE + 1];
1129 int tmp_ssid_len = 0;
1131 short apset, ssidset, ssidbroad, apmatch, ssidmatch;
1133 /* we are interested in new only if we are not associated
1134 * and we are not associating / authenticating
1136 if (ieee->link_state != MAC80211_NOLINK)
1139 if ((ieee->iw_mode == IW_MODE_INFRA) && !(net->capability &
1140 WLAN_CAPABILITY_ESS))
1143 if (ieee->iw_mode == IW_MODE_INFRA) {
1144 /* if the user specified the AP MAC, we need also the essid
1145 * This could be obtained by beacons or, if the network does not
1146 * broadcast it, it can be put manually.
1148 apset = ieee->wap_set;
1149 ssidset = ieee->ssid_set;
1150 ssidbroad = !(net->ssid_len == 0 || net->ssid[0] == '\0');
1151 apmatch = (memcmp(ieee->current_network.bssid, net->bssid,
1154 ssidmatch = (ieee->current_network.ssid_len ==
1155 net->hidden_ssid_len) &&
1156 (!strncmp(ieee->current_network.ssid,
1157 net->hidden_ssid, net->hidden_ssid_len));
1158 if (net->hidden_ssid_len > 0) {
1159 strncpy(net->ssid, net->hidden_ssid,
1160 net->hidden_ssid_len);
1161 net->ssid_len = net->hidden_ssid_len;
1166 (ieee->current_network.ssid_len == net->ssid_len) &&
1167 (!strncmp(ieee->current_network.ssid, net->ssid,
1171 /* if the user set the AP check if match.
1172 * if the network does not broadcast essid we check the
1173 * user supplied ANY essid
1174 * if the network does broadcast and the user does not set
1176 * if the network does broadcast and the user did set essid
1177 * check if essid match
1178 * if the ap is not set, check that the user set the bssid
1179 * and the network does broadcast and that those two bssid match
1181 if ((apset && apmatch &&
1182 ((ssidset && ssidbroad && ssidmatch) ||
1183 (ssidbroad && !ssidset) || (!ssidbroad && ssidset))) ||
1184 (!apset && ssidset && ssidbroad && ssidmatch) ||
1185 (ieee->is_roaming && ssidset && ssidbroad && ssidmatch)) {
1186 /* Save the essid so that if it is hidden, it is
1187 * replaced with the essid provided by the user.
1190 memcpy(tmp_ssid, ieee->current_network.ssid,
1191 ieee->current_network.ssid_len);
1192 tmp_ssid_len = ieee->current_network.ssid_len;
1194 memcpy(&ieee->current_network, net,
1195 sizeof(ieee->current_network));
1197 memcpy(ieee->current_network.ssid, tmp_ssid,
1199 ieee->current_network.ssid_len = tmp_ssid_len;
1201 netdev_info(ieee->dev,
1202 "Linking with %s,channel:%d, qos:%d, myHT:%d, networkHT:%d, mode:%x cur_net.flags:0x%x\n",
1203 ieee->current_network.ssid,
1204 ieee->current_network.channel,
1205 ieee->current_network.qos_data.supported,
1206 ieee->ht_info->enable_ht,
1207 ieee->current_network.bssht.bd_support_ht,
1208 ieee->current_network.mode,
1209 ieee->current_network.flags);
1211 if ((rtllib_act_scanning(ieee, false)) &&
1212 !(ieee->softmac_features & IEEE_SOFTMAC_SCAN))
1213 rtllib_stop_scan_syncro(ieee);
1215 ht_reset_iot_setting(ieee->ht_info);
1217 if (ieee->iw_mode == IW_MODE_INFRA) {
1218 /* Join the network for the first time */
1219 ieee->AsocRetryCount = 0;
1220 if ((ieee->current_network.qos_data.supported == 1) &&
1221 ieee->current_network.bssht.bd_support_ht)
1222 ht_reset_self_and_save_peer_setting(ieee,
1223 &(ieee->current_network));
1225 ieee->ht_info->current_ht_support = false;
1227 ieee->link_state = RTLLIB_ASSOCIATING;
1228 schedule_delayed_work(&ieee->associate_procedure_wq, 0);
1230 if (rtllib_is_54g(&ieee->current_network)) {
1232 ieee->set_wireless_mode(ieee->dev, WIRELESS_MODE_G);
1233 netdev_info(ieee->dev,
1237 ieee->set_wireless_mode(ieee->dev, WIRELESS_MODE_B);
1238 netdev_info(ieee->dev,
1241 memset(ieee->dot11ht_oper_rate_set, 0, 16);
1242 ieee->link_state = MAC80211_LINKED;
1248 static void rtllib_softmac_check_all_nets(struct rtllib_device *ieee)
1250 unsigned long flags;
1251 struct rtllib_network *target;
1253 spin_lock_irqsave(&ieee->lock, flags);
1255 list_for_each_entry(target, &ieee->network_list, list) {
1256 /* if the state become different that NOLINK means
1257 * we had found what we are searching for
1260 if (ieee->link_state != MAC80211_NOLINK)
1263 if (ieee->scan_age == 0 || time_after(target->last_scanned +
1264 ieee->scan_age, jiffies))
1265 rtllib_softmac_new_net(ieee, target);
1267 spin_unlock_irqrestore(&ieee->lock, flags);
1270 static inline int auth_parse(struct net_device *dev, struct sk_buff *skb,
1271 u8 **challenge, int *chlen)
1273 struct rtllib_authentication *a;
1276 if (skb->len < (sizeof(struct rtllib_authentication) -
1277 sizeof(struct rtllib_info_element))) {
1278 netdev_dbg(dev, "invalid len in auth resp: %d\n", skb->len);
1282 a = (struct rtllib_authentication *)skb->data;
1283 if (skb->len > (sizeof(struct rtllib_authentication) + 3)) {
1284 t = skb->data + sizeof(struct rtllib_authentication);
1286 if (*(t++) == MFIE_TYPE_CHALLENGE) {
1288 *challenge = kmemdup(t, *chlen, GFP_ATOMIC);
1295 netdev_dbg(dev, "auth_parse() failed\n");
1302 static inline u16 assoc_parse(struct rtllib_device *ieee, struct sk_buff *skb,
1305 struct rtllib_assoc_response_frame *response_head;
1308 if (skb->len < sizeof(struct rtllib_assoc_response_frame)) {
1309 netdev_dbg(ieee->dev, "Invalid len in auth resp: %d\n",
1314 response_head = (struct rtllib_assoc_response_frame *)skb->data;
1315 *aid = le16_to_cpu(response_head->aid) & 0x3fff;
1317 status_code = le16_to_cpu(response_head->status);
1318 if ((status_code == WLAN_STATUS_ASSOC_DENIED_RATES ||
1319 status_code == WLAN_STATUS_CAPS_UNSUPPORTED) &&
1320 ((ieee->mode == WIRELESS_MODE_G) &&
1321 (ieee->current_network.mode == WIRELESS_MODE_N_24G) &&
1322 (ieee->AsocRetryCount++ < (RT_ASOC_RETRY_LIMIT - 1)))) {
1323 ieee->ht_info->iot_action |= HT_IOT_ACT_PURE_N_MODE;
1325 ieee->AsocRetryCount = 0;
1328 return le16_to_cpu(response_head->status);
1331 void rtllib_sta_ps_send_null_frame(struct rtllib_device *ieee, short pwr)
1333 struct sk_buff *buf = rtllib_null_func(ieee, pwr);
1336 softmac_ps_mgmt_xmit(buf, ieee);
1338 EXPORT_SYMBOL(rtllib_sta_ps_send_null_frame);
1340 void rtllib_sta_ps_send_pspoll_frame(struct rtllib_device *ieee)
1342 struct sk_buff *buf = rtllib_pspoll_func(ieee);
1345 softmac_ps_mgmt_xmit(buf, ieee);
1348 static short rtllib_sta_ps_sleep(struct rtllib_device *ieee, u64 *time)
1352 struct rt_pwr_save_ctrl *psc = &ieee->pwr_save_ctrl;
1354 if (ieee->LPSDelayCnt) {
1355 ieee->LPSDelayCnt--;
1359 dtim = ieee->current_network.dtim_data;
1360 if (!(dtim & RTLLIB_DTIM_VALID))
1362 timeout = ieee->current_network.beacon_interval;
1363 ieee->current_network.dtim_data = RTLLIB_DTIM_INVALID;
1364 /* there's no need to nofity AP that I find you buffered
1365 * with broadcast packet
1367 if (dtim & (RTLLIB_DTIM_UCAST & ieee->ps))
1370 if (!time_after(jiffies,
1371 dev_trans_start(ieee->dev) + msecs_to_jiffies(timeout)))
1373 if (!time_after(jiffies,
1374 ieee->last_rx_ps_time + msecs_to_jiffies(timeout)))
1376 if ((ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE) &&
1377 (ieee->mgmt_queue_tail != ieee->mgmt_queue_head))
1381 if (ieee->bAwakePktSent) {
1382 psc->LPSAwakeIntvl = 1;
1386 if (psc->LPSAwakeIntvl == 0)
1387 psc->LPSAwakeIntvl = 1;
1388 psc->LPSAwakeIntvl = (psc->LPSAwakeIntvl >=
1389 MaxPeriod) ? MaxPeriod :
1390 (psc->LPSAwakeIntvl + 1);
1393 u8 LPSAwakeIntvl_tmp = 0;
1394 u8 period = ieee->current_network.dtim_period;
1395 u8 count = ieee->current_network.tim.tim_count;
1398 if (psc->LPSAwakeIntvl > period)
1399 LPSAwakeIntvl_tmp = period +
1400 (psc->LPSAwakeIntvl -
1402 ((psc->LPSAwakeIntvl - period) %
1405 LPSAwakeIntvl_tmp = psc->LPSAwakeIntvl;
1408 if (psc->LPSAwakeIntvl >
1409 ieee->current_network.tim.tim_count)
1410 LPSAwakeIntvl_tmp = count +
1411 (psc->LPSAwakeIntvl - count) -
1412 ((psc->LPSAwakeIntvl - count) % period);
1414 LPSAwakeIntvl_tmp = psc->LPSAwakeIntvl;
1417 *time = ieee->current_network.last_dtim_sta_time
1418 + msecs_to_jiffies(ieee->current_network.beacon_interval *
1426 static inline void rtllib_sta_ps(struct work_struct *work)
1428 struct rtllib_device *ieee;
1431 unsigned long flags, flags2;
1433 ieee = container_of(work, struct rtllib_device, ps_task);
1435 spin_lock_irqsave(&ieee->lock, flags);
1437 if ((ieee->ps == RTLLIB_PS_DISABLED ||
1438 ieee->iw_mode != IW_MODE_INFRA ||
1439 ieee->link_state != MAC80211_LINKED)) {
1440 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1441 rtllib_sta_wakeup(ieee, 1);
1443 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1445 sleep = rtllib_sta_ps_sleep(ieee, &time);
1446 /* 2 wake, 1 sleep, 0 do nothing */
1450 if (ieee->sta_sleep == LPS_IS_SLEEP) {
1451 ieee->enter_sleep_state(ieee->dev, time);
1452 } else if (ieee->sta_sleep == LPS_IS_WAKE) {
1453 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1455 if (ieee->ps_is_queue_empty(ieee->dev)) {
1456 ieee->sta_sleep = LPS_WAIT_NULL_DATA_SEND;
1457 ieee->ack_tx_to_ieee = 1;
1458 rtllib_sta_ps_send_null_frame(ieee, 1);
1459 ieee->ps_time = time;
1461 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1464 ieee->bAwakePktSent = false;
1466 } else if (sleep == 2) {
1467 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1469 rtllib_sta_wakeup(ieee, 1);
1471 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1475 spin_unlock_irqrestore(&ieee->lock, flags);
1478 static void rtllib_sta_wakeup(struct rtllib_device *ieee, short nl)
1480 if (ieee->sta_sleep == LPS_IS_WAKE) {
1482 if (ieee->ht_info->iot_action &
1483 HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
1484 ieee->ack_tx_to_ieee = 1;
1485 rtllib_sta_ps_send_null_frame(ieee, 0);
1487 ieee->ack_tx_to_ieee = 1;
1488 rtllib_sta_ps_send_pspoll_frame(ieee);
1494 if (ieee->sta_sleep == LPS_IS_SLEEP)
1495 ieee->sta_wake_up(ieee->dev);
1497 if (ieee->ht_info->iot_action &
1498 HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
1499 ieee->ack_tx_to_ieee = 1;
1500 rtllib_sta_ps_send_null_frame(ieee, 0);
1502 ieee->ack_tx_to_ieee = 1;
1503 ieee->polling = true;
1504 rtllib_sta_ps_send_pspoll_frame(ieee);
1508 ieee->sta_sleep = LPS_IS_WAKE;
1509 ieee->polling = false;
1513 void rtllib_ps_tx_ack(struct rtllib_device *ieee, short success)
1515 unsigned long flags, flags2;
1517 spin_lock_irqsave(&ieee->lock, flags);
1519 if (ieee->sta_sleep == LPS_WAIT_NULL_DATA_SEND) {
1520 /* Null frame with PS bit set */
1522 ieee->sta_sleep = LPS_IS_SLEEP;
1523 ieee->enter_sleep_state(ieee->dev, ieee->ps_time);
1525 /* if the card report not success we can't be sure the AP
1526 * has not RXed so we can't assume the AP believe us awake
1528 } else {/* 21112005 - tx again null without PS bit if lost */
1530 if ((ieee->sta_sleep == LPS_IS_WAKE) && !success) {
1531 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1532 if (ieee->ht_info->iot_action &
1533 HT_IOT_ACT_NULL_DATA_POWER_SAVING)
1534 rtllib_sta_ps_send_null_frame(ieee, 0);
1536 rtllib_sta_ps_send_pspoll_frame(ieee);
1537 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1540 spin_unlock_irqrestore(&ieee->lock, flags);
1542 EXPORT_SYMBOL(rtllib_ps_tx_ack);
1544 static void rtllib_process_action(struct rtllib_device *ieee,
1545 struct sk_buff *skb)
1547 u8 *act = skb->data + RTLLIB_3ADDR_LEN;
1556 rtllib_rx_ADDBAReq(ieee, skb);
1559 rtllib_rx_ADDBARsp(ieee, skb);
1562 rtllib_rx_DELBA(ieee, skb);
1572 rtllib_rx_assoc_resp(struct rtllib_device *ieee, struct sk_buff *skb,
1573 struct rtllib_rx_stats *rx_stats)
1578 struct rtllib_assoc_response_frame *assoc_resp;
1579 struct ieee80211_hdr_3addr *header = (struct ieee80211_hdr_3addr *)skb->data;
1580 u16 frame_ctl = le16_to_cpu(header->frame_control);
1582 netdev_dbg(ieee->dev, "received [RE]ASSOCIATION RESPONSE (%d)\n",
1583 WLAN_FC_GET_STYPE(frame_ctl));
1585 if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1586 ieee->link_state == RTLLIB_ASSOCIATING_AUTHENTICATED &&
1587 (ieee->iw_mode == IW_MODE_INFRA)) {
1588 errcode = assoc_parse(ieee, skb, &aid);
1590 struct rtllib_network *network =
1591 kzalloc(sizeof(struct rtllib_network),
1596 ieee->link_state = MAC80211_LINKED;
1597 ieee->assoc_id = aid;
1598 ieee->softmac_stats.rx_ass_ok++;
1599 /* station support qos */
1600 /* Let the register setting default with Legacy station */
1601 assoc_resp = (struct rtllib_assoc_response_frame *)skb->data;
1602 if (ieee->current_network.qos_data.supported == 1) {
1603 if (rtllib_parse_info_param(ieee, assoc_resp->info_element,
1604 rx_stats->len - sizeof(*assoc_resp),
1605 network, rx_stats)) {
1609 memcpy(ieee->ht_info->PeerHTCapBuf,
1610 network->bssht.bd_ht_cap_buf,
1611 network->bssht.bd_ht_cap_len);
1612 memcpy(ieee->ht_info->PeerHTInfoBuf,
1613 network->bssht.bd_ht_info_buf,
1614 network->bssht.bd_ht_info_len);
1615 ieee->handle_assoc_response(ieee->dev,
1616 (struct rtllib_assoc_response_frame *)header, network);
1620 kfree(ieee->assocresp_ies);
1621 ieee->assocresp_ies = NULL;
1622 ies = &assoc_resp->info_element[0].id;
1623 ieee->assocresp_ies_len = (skb->data + skb->len) - ies;
1624 ieee->assocresp_ies = kmemdup(ies,
1625 ieee->assocresp_ies_len,
1627 if (!ieee->assocresp_ies)
1628 ieee->assocresp_ies_len = 0;
1630 rtllib_associate_complete(ieee);
1632 /* aid could not been allocated */
1633 ieee->softmac_stats.rx_ass_err++;
1634 netdev_info(ieee->dev,
1635 "Association response status code 0x%x\n",
1637 if (ieee->AsocRetryCount < RT_ASOC_RETRY_LIMIT)
1638 schedule_delayed_work(&ieee->associate_procedure_wq, 0);
1640 rtllib_associate_abort(ieee);
1646 static void rtllib_rx_auth_resp(struct rtllib_device *ieee, struct sk_buff *skb)
1651 bool bSupportNmode = true, bHalfSupportNmode = false;
1653 errcode = auth_parse(ieee->dev, skb, &challenge, &chlen);
1656 ieee->softmac_stats.rx_auth_rs_err++;
1657 netdev_info(ieee->dev,
1658 "Authentication response status code %d", errcode);
1659 rtllib_associate_abort(ieee);
1663 if (ieee->open_wep || !challenge) {
1664 ieee->link_state = RTLLIB_ASSOCIATING_AUTHENTICATED;
1665 ieee->softmac_stats.rx_auth_rs_ok++;
1666 if (!(ieee->ht_info->iot_action & HT_IOT_ACT_PURE_N_MODE)) {
1667 if (!ieee->GetNmodeSupportBySecCfg(ieee->dev)) {
1668 if (is_ht_half_nmode_aps(ieee)) {
1669 bSupportNmode = true;
1670 bHalfSupportNmode = true;
1672 bSupportNmode = false;
1673 bHalfSupportNmode = false;
1677 /* Dummy wirless mode setting to avoid encryption issue */
1678 if (bSupportNmode) {
1679 ieee->set_wireless_mode(ieee->dev,
1680 ieee->current_network.mode);
1683 ieee->set_wireless_mode(ieee->dev, WIRELESS_MODE_G);
1686 if ((ieee->current_network.mode == WIRELESS_MODE_N_24G) &&
1687 bHalfSupportNmode) {
1688 netdev_info(ieee->dev, "======>enter half N mode\n");
1689 ieee->bHalfWirelessN24GMode = true;
1691 ieee->bHalfWirelessN24GMode = false;
1693 rtllib_associate_step2(ieee);
1695 rtllib_auth_challenge(ieee, challenge, chlen);
1700 rtllib_rx_auth(struct rtllib_device *ieee, struct sk_buff *skb,
1701 struct rtllib_rx_stats *rx_stats)
1703 if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) {
1704 if (ieee->link_state == RTLLIB_ASSOCIATING_AUTHENTICATING &&
1705 (ieee->iw_mode == IW_MODE_INFRA)) {
1706 netdev_dbg(ieee->dev,
1707 "Received authentication response");
1708 rtllib_rx_auth_resp(ieee, skb);
1715 rtllib_rx_deauth(struct rtllib_device *ieee, struct sk_buff *skb)
1717 struct ieee80211_hdr_3addr *header = (struct ieee80211_hdr_3addr *)skb->data;
1720 if (memcmp(header->addr3, ieee->current_network.bssid, ETH_ALEN) != 0)
1723 /* FIXME for now repeat all the association procedure
1724 * both for disassociation and deauthentication
1726 if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1727 ieee->link_state == MAC80211_LINKED &&
1728 (ieee->iw_mode == IW_MODE_INFRA)) {
1729 frame_ctl = le16_to_cpu(header->frame_control);
1730 netdev_info(ieee->dev,
1731 "==========>received disassoc/deauth(%x) frame, reason code:%x\n",
1732 WLAN_FC_GET_STYPE(frame_ctl),
1733 ((struct rtllib_disassoc *)skb->data)->reason);
1734 ieee->link_state = RTLLIB_ASSOCIATING;
1735 ieee->softmac_stats.reassoc++;
1736 ieee->is_roaming = true;
1737 ieee->link_detect_info.bBusyTraffic = false;
1738 rtllib_disassociate(ieee);
1739 RemovePeerTS(ieee, header->addr2);
1740 if (!(ieee->rtllib_ap_sec_type(ieee) &
1741 (SEC_ALG_CCMP | SEC_ALG_TKIP)))
1742 schedule_delayed_work(
1743 &ieee->associate_procedure_wq, 5);
1748 inline int rtllib_rx_frame_softmac(struct rtllib_device *ieee,
1749 struct sk_buff *skb,
1750 struct rtllib_rx_stats *rx_stats, u16 type,
1753 struct ieee80211_hdr_3addr *header = (struct ieee80211_hdr_3addr *)skb->data;
1756 if (!ieee->proto_started)
1759 frame_ctl = le16_to_cpu(header->frame_control);
1760 switch (WLAN_FC_GET_STYPE(frame_ctl)) {
1761 case IEEE80211_STYPE_ASSOC_RESP:
1762 case IEEE80211_STYPE_REASSOC_RESP:
1763 if (rtllib_rx_assoc_resp(ieee, skb, rx_stats) == 1)
1766 case IEEE80211_STYPE_ASSOC_REQ:
1767 case IEEE80211_STYPE_REASSOC_REQ:
1769 case IEEE80211_STYPE_AUTH:
1770 rtllib_rx_auth(ieee, skb, rx_stats);
1772 case IEEE80211_STYPE_DISASSOC:
1773 case IEEE80211_STYPE_DEAUTH:
1774 rtllib_rx_deauth(ieee, skb);
1776 case IEEE80211_STYPE_ACTION:
1777 rtllib_process_action(ieee, skb);
1785 /* following are for a simpler TX queue management.
1786 * Instead of using netif_[stop/wake]_queue the driver
1787 * will use these two functions (plus a reset one), that
1788 * will internally use the kernel netif_* and takes
1789 * care of the ieee802.11 fragmentation.
1790 * So the driver receives a fragment per time and might
1791 * call the stop function when it wants to not
1792 * have enough room to TX an entire packet.
1793 * This might be useful if each fragment needs it's own
1794 * descriptor, thus just keep a total free memory > than
1795 * the max fragmentation threshold is not enough.. If the
1796 * ieee802.11 stack passed a TXB struct then you need
1797 * to keep N free descriptors where
1798 * N = MAX_PACKET_SIZE / MIN_FRAG_TRESHOLD
1799 * In this way you need just one and the 802.11 stack
1800 * will take care of buffering fragments and pass them to
1801 * the driver later, when it wakes the queue.
1803 void rtllib_softmac_xmit(struct rtllib_txb *txb, struct rtllib_device *ieee)
1805 unsigned int queue_index = txb->queue_index;
1806 unsigned long flags;
1808 struct cb_desc *tcb_desc = NULL;
1809 unsigned long queue_len = 0;
1811 spin_lock_irqsave(&ieee->lock, flags);
1813 /* called with 2nd parm 0, no tx mgmt lock required */
1814 rtllib_sta_wakeup(ieee, 0);
1816 /* update the tx status */
1817 tcb_desc = (struct cb_desc *)(txb->fragments[0]->cb +
1819 if (tcb_desc->bMulticast)
1820 ieee->stats.multicast++;
1822 /* if xmit available, just xmit it immediately, else just insert it to
1825 for (i = 0; i < txb->nr_frags; i++) {
1826 queue_len = skb_queue_len(&ieee->skb_waitq[queue_index]);
1827 if ((queue_len != 0) ||
1828 (!ieee->check_nic_enough_desc(ieee->dev, queue_index)) ||
1829 (ieee->queue_stop)) {
1830 /* insert the skb packet to the wait queue
1831 * as for the completion function, it does not need
1832 * to check it any more.
1834 if (queue_len < 200)
1835 skb_queue_tail(&ieee->skb_waitq[queue_index],
1838 kfree_skb(txb->fragments[i]);
1840 ieee->softmac_data_hard_start_xmit(txb->fragments[i],
1841 ieee->dev, ieee->rate);
1845 rtllib_txb_free(txb);
1847 spin_unlock_irqrestore(&ieee->lock, flags);
1850 void rtllib_reset_queue(struct rtllib_device *ieee)
1852 unsigned long flags;
1854 spin_lock_irqsave(&ieee->lock, flags);
1855 init_mgmt_queue(ieee);
1856 if (ieee->tx_pending.txb) {
1857 rtllib_txb_free(ieee->tx_pending.txb);
1858 ieee->tx_pending.txb = NULL;
1860 ieee->queue_stop = 0;
1861 spin_unlock_irqrestore(&ieee->lock, flags);
1863 EXPORT_SYMBOL(rtllib_reset_queue);
1865 void rtllib_stop_all_queues(struct rtllib_device *ieee)
1869 for (i = 0; i < ieee->dev->num_tx_queues; i++)
1870 txq_trans_cond_update(netdev_get_tx_queue(ieee->dev, i));
1872 netif_tx_stop_all_queues(ieee->dev);
1875 void rtllib_wake_all_queues(struct rtllib_device *ieee)
1877 netif_tx_wake_all_queues(ieee->dev);
1880 /* this is called only in user context, with wx_mutex held */
1881 static void rtllib_start_bss(struct rtllib_device *ieee)
1883 unsigned long flags;
1885 /* check if we have already found the net we
1886 * are interested in (if any).
1887 * if not (we are disassociated and we are not
1888 * in associating / authenticating phase) start the background scanning.
1890 rtllib_softmac_check_all_nets(ieee);
1892 /* ensure no-one start an associating process (thus setting
1893 * the ieee->link_state to rtllib_ASSOCIATING) while we
1894 * have just checked it and we are going to enable scan.
1895 * The rtllib_new_net function is always called with
1896 * lock held (from both rtllib_softmac_check_all_nets and
1897 * the rx path), so we cannot be in the middle of such function
1899 spin_lock_irqsave(&ieee->lock, flags);
1901 if (ieee->link_state == MAC80211_NOLINK)
1902 rtllib_start_scan(ieee);
1903 spin_unlock_irqrestore(&ieee->lock, flags);
1906 static void rtllib_link_change_wq(void *data)
1908 struct rtllib_device *ieee = container_of_dwork_rsl(data,
1909 struct rtllib_device, link_change_wq);
1910 ieee->link_change(ieee->dev);
1913 /* called only in userspace context */
1914 void rtllib_disassociate(struct rtllib_device *ieee)
1916 netif_carrier_off(ieee->dev);
1917 if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)
1918 rtllib_reset_queue(ieee);
1920 ieee->link_state = MAC80211_NOLINK;
1921 ieee->is_set_key = false;
1924 schedule_delayed_work(&ieee->link_change_wq, 0);
1926 notify_wx_assoc_event(ieee);
1929 static void rtllib_associate_retry_wq(void *data)
1931 struct rtllib_device *ieee = container_of_dwork_rsl(data,
1932 struct rtllib_device, associate_retry_wq);
1933 unsigned long flags;
1935 mutex_lock(&ieee->wx_mutex);
1936 if (!ieee->proto_started)
1939 if (ieee->link_state != RTLLIB_ASSOCIATING_RETRY)
1942 /* until we do not set the state to MAC80211_NOLINK
1943 * there are no possibility to have someone else trying
1944 * to start an association procedure (we get here with
1945 * ieee->link_state = RTLLIB_ASSOCIATING).
1946 * When we set the state to MAC80211_NOLINK it is possible
1947 * that the RX path run an attempt to associate, but
1948 * both rtllib_softmac_check_all_nets and the
1949 * RX path works with ieee->lock held so there are no
1950 * problems. If we are still disassociated then start a scan.
1951 * the lock here is necessary to ensure no one try to start
1952 * an association procedure when we have just checked the
1953 * state and we are going to start the scan.
1955 ieee->beinretry = true;
1956 ieee->link_state = MAC80211_NOLINK;
1958 rtllib_softmac_check_all_nets(ieee);
1960 spin_lock_irqsave(&ieee->lock, flags);
1962 if (ieee->link_state == MAC80211_NOLINK)
1963 rtllib_start_scan(ieee);
1964 spin_unlock_irqrestore(&ieee->lock, flags);
1966 ieee->beinretry = false;
1968 mutex_unlock(&ieee->wx_mutex);
1971 void rtllib_softmac_stop_protocol(struct rtllib_device *ieee)
1973 rtllib_stop_scan_syncro(ieee);
1974 mutex_lock(&ieee->wx_mutex);
1975 rtllib_stop_protocol(ieee);
1976 mutex_unlock(&ieee->wx_mutex);
1978 EXPORT_SYMBOL(rtllib_softmac_stop_protocol);
1980 void rtllib_stop_protocol(struct rtllib_device *ieee)
1982 if (!ieee->proto_started)
1985 ieee->proto_started = 0;
1986 ieee->proto_stoppping = 1;
1987 ieee->rtllib_ips_leave(ieee->dev);
1989 del_timer_sync(&ieee->associate_timer);
1990 mutex_unlock(&ieee->wx_mutex);
1991 cancel_delayed_work_sync(&ieee->associate_retry_wq);
1992 mutex_lock(&ieee->wx_mutex);
1993 cancel_delayed_work_sync(&ieee->link_change_wq);
1994 rtllib_stop_scan(ieee);
1996 if (ieee->link_state <= RTLLIB_ASSOCIATING_AUTHENTICATED)
1997 ieee->link_state = MAC80211_NOLINK;
1999 if (ieee->link_state == MAC80211_LINKED) {
2000 if (ieee->iw_mode == IW_MODE_INFRA)
2001 SendDisassociation(ieee, 1, WLAN_REASON_DEAUTH_LEAVING);
2002 rtllib_disassociate(ieee);
2006 ieee->proto_stoppping = 0;
2008 kfree(ieee->assocreq_ies);
2009 ieee->assocreq_ies = NULL;
2010 ieee->assocreq_ies_len = 0;
2011 kfree(ieee->assocresp_ies);
2012 ieee->assocresp_ies = NULL;
2013 ieee->assocresp_ies_len = 0;
2016 void rtllib_softmac_start_protocol(struct rtllib_device *ieee)
2018 mutex_lock(&ieee->wx_mutex);
2019 rtllib_start_protocol(ieee);
2020 mutex_unlock(&ieee->wx_mutex);
2022 EXPORT_SYMBOL(rtllib_softmac_start_protocol);
2024 void rtllib_start_protocol(struct rtllib_device *ieee)
2029 if (ieee->proto_started)
2032 ieee->proto_started = 1;
2034 if (ieee->current_network.channel == 0) {
2037 if (ch > MAX_CHANNEL_NUMBER)
2038 return; /* no channel found */
2039 } while (!ieee->active_channel_map[ch]);
2040 ieee->current_network.channel = ch;
2043 if (ieee->current_network.beacon_interval == 0)
2044 ieee->current_network.beacon_interval = 100;
2046 for (i = 0; i < 17; i++) {
2047 ieee->last_rxseq_num[i] = -1;
2048 ieee->last_rxfrag_num[i] = -1;
2049 ieee->last_packet_time[i] = 0;
2053 /* if the user set the MAC of the ad-hoc cell and then
2054 * switch to managed mode, shall we make sure that association
2055 * attempts does not fail just because the user provide the essid
2056 * and the nic is still checking for the AP MAC ??
2058 switch (ieee->iw_mode) {
2060 rtllib_start_bss(ieee);
2065 int rtllib_softmac_init(struct rtllib_device *ieee)
2069 memset(&ieee->current_network, 0, sizeof(struct rtllib_network));
2071 ieee->link_state = MAC80211_NOLINK;
2072 for (i = 0; i < 5; i++)
2073 ieee->seq_ctrl[i] = 0;
2075 ieee->link_detect_info.SlotIndex = 0;
2076 ieee->link_detect_info.SlotNum = 2;
2077 ieee->link_detect_info.NumRecvBcnInPeriod = 0;
2078 ieee->link_detect_info.NumRecvDataInPeriod = 0;
2079 ieee->link_detect_info.num_tx_ok_in_period = 0;
2080 ieee->link_detect_info.num_rx_ok_in_period = 0;
2081 ieee->link_detect_info.NumRxUnicastOkInPeriod = 0;
2082 ieee->is_aggregate_frame = false;
2084 ieee->queue_stop = 0;
2085 ieee->scanning_continue = 0;
2086 ieee->softmac_features = 0;
2089 ieee->proto_started = 0;
2090 ieee->proto_stoppping = 0;
2091 ieee->basic_rate = RTLLIB_DEFAULT_BASIC_RATE;
2093 ieee->ps = RTLLIB_PS_DISABLED;
2094 ieee->sta_sleep = LPS_IS_WAKE;
2096 ieee->reg_dot11ht_oper_rate_set[0] = 0xff;
2097 ieee->reg_dot11ht_oper_rate_set[1] = 0xff;
2098 ieee->reg_dot11ht_oper_rate_set[4] = 0x01;
2100 ieee->reg_dot11tx_ht_oper_rate_set[0] = 0xff;
2101 ieee->reg_dot11tx_ht_oper_rate_set[1] = 0xff;
2102 ieee->reg_dot11tx_ht_oper_rate_set[4] = 0x01;
2104 ieee->FirstIe_InScan = false;
2105 ieee->actscanning = false;
2106 ieee->beinretry = false;
2107 ieee->is_set_key = false;
2108 init_mgmt_queue(ieee);
2110 ieee->tx_pending.txb = NULL;
2112 timer_setup(&ieee->associate_timer, rtllib_associate_abort_cb, 0);
2114 INIT_DELAYED_WORK(&ieee->link_change_wq, (void *)rtllib_link_change_wq);
2115 INIT_WORK(&ieee->associate_complete_wq, (void *)rtllib_associate_complete_wq);
2116 INIT_DELAYED_WORK(&ieee->associate_procedure_wq, (void *)rtllib_associate_procedure_wq);
2117 INIT_DELAYED_WORK(&ieee->softmac_scan_wq, (void *)rtllib_softmac_scan_wq);
2118 INIT_DELAYED_WORK(&ieee->associate_retry_wq, (void *)rtllib_associate_retry_wq);
2119 INIT_WORK(&ieee->wx_sync_scan_wq, (void *)rtllib_wx_sync_scan_wq);
2121 mutex_init(&ieee->wx_mutex);
2122 mutex_init(&ieee->scan_mutex);
2123 mutex_init(&ieee->ips_mutex);
2125 spin_lock_init(&ieee->mgmt_tx_lock);
2126 spin_lock_init(&ieee->beacon_lock);
2128 INIT_WORK(&ieee->ps_task, rtllib_sta_ps);
2133 void rtllib_softmac_free(struct rtllib_device *ieee)
2135 del_timer_sync(&ieee->associate_timer);
2137 cancel_delayed_work_sync(&ieee->associate_retry_wq);
2138 cancel_delayed_work_sync(&ieee->associate_procedure_wq);
2139 cancel_delayed_work_sync(&ieee->softmac_scan_wq);
2140 cancel_delayed_work_sync(&ieee->hw_wakeup_wq);
2141 cancel_delayed_work_sync(&ieee->hw_sleep_wq);
2142 cancel_delayed_work_sync(&ieee->link_change_wq);
2143 cancel_work_sync(&ieee->associate_complete_wq);
2144 cancel_work_sync(&ieee->ips_leave_wq);
2145 cancel_work_sync(&ieee->wx_sync_scan_wq);
2146 cancel_work_sync(&ieee->ps_task);
2149 static inline struct sk_buff *
2150 rtllib_disauth_skb(struct rtllib_network *beacon,
2151 struct rtllib_device *ieee, u16 asRsn)
2153 struct sk_buff *skb;
2154 struct rtllib_disauth *disauth;
2155 int len = sizeof(struct rtllib_disauth) + ieee->tx_headroom;
2157 skb = dev_alloc_skb(len);
2161 skb_reserve(skb, ieee->tx_headroom);
2163 disauth = skb_put(skb, sizeof(struct rtllib_disauth));
2164 disauth->header.frame_control = cpu_to_le16(IEEE80211_STYPE_DEAUTH);
2165 disauth->header.duration_id = 0;
2167 ether_addr_copy(disauth->header.addr1, beacon->bssid);
2168 ether_addr_copy(disauth->header.addr2, ieee->dev->dev_addr);
2169 ether_addr_copy(disauth->header.addr3, beacon->bssid);
2171 disauth->reason = cpu_to_le16(asRsn);
2175 static inline struct sk_buff *
2176 rtllib_disassociate_skb(struct rtllib_network *beacon,
2177 struct rtllib_device *ieee, u16 asRsn)
2179 struct sk_buff *skb;
2180 struct rtllib_disassoc *disass;
2181 int len = sizeof(struct rtllib_disassoc) + ieee->tx_headroom;
2183 skb = dev_alloc_skb(len);
2188 skb_reserve(skb, ieee->tx_headroom);
2190 disass = skb_put(skb, sizeof(struct rtllib_disassoc));
2191 disass->header.frame_control = cpu_to_le16(IEEE80211_STYPE_DISASSOC);
2192 disass->header.duration_id = 0;
2194 ether_addr_copy(disass->header.addr1, beacon->bssid);
2195 ether_addr_copy(disass->header.addr2, ieee->dev->dev_addr);
2196 ether_addr_copy(disass->header.addr3, beacon->bssid);
2198 disass->reason = cpu_to_le16(asRsn);
2202 void SendDisassociation(struct rtllib_device *ieee, bool deauth, u16 asRsn)
2204 struct rtllib_network *beacon = &ieee->current_network;
2205 struct sk_buff *skb;
2208 skb = rtllib_disauth_skb(beacon, ieee, asRsn);
2210 skb = rtllib_disassociate_skb(beacon, ieee, asRsn);
2213 softmac_mgmt_xmit(skb, ieee);
2216 u8 rtllib_ap_sec_type(struct rtllib_device *ieee)
2218 static u8 ccmp_ie[4] = {0x00, 0x50, 0xf2, 0x04};
2219 static u8 ccmp_rsn_ie[4] = {0x00, 0x0f, 0xac, 0x04};
2220 int wpa_ie_len = ieee->wpa_ie_len;
2221 struct lib80211_crypt_data *crypt;
2224 crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
2225 encrypt = (ieee->current_network.capability & WLAN_CAPABILITY_PRIVACY)
2226 || (crypt && crypt->ops && (strcmp(crypt->ops->name, "R-WEP") == 0));
2229 if (encrypt && (wpa_ie_len == 0)) {
2231 } else if ((wpa_ie_len != 0)) {
2232 if (((ieee->wpa_ie[0] == 0xdd) &&
2233 (!memcmp(&ieee->wpa_ie[14], ccmp_ie, 4))) ||
2234 ((ieee->wpa_ie[0] == 0x30) &&
2235 (!memcmp(&ieee->wpa_ie[10], ccmp_rsn_ie, 4))))
2236 return SEC_ALG_CCMP;
2238 return SEC_ALG_TKIP;
2240 return SEC_ALG_NONE;
2244 static void rtllib_MlmeDisassociateRequest(struct rtllib_device *rtllib,
2245 u8 *asSta, u8 asRsn)
2250 RemovePeerTS(rtllib, asSta);
2252 if (memcmp(rtllib->current_network.bssid, asSta, 6) == 0) {
2253 rtllib->link_state = MAC80211_NOLINK;
2255 for (i = 0; i < 6; i++)
2256 rtllib->current_network.bssid[i] = 0x22;
2257 op_mode = RT_OP_MODE_NO_LINK;
2258 rtllib->op_mode = RT_OP_MODE_NO_LINK;
2259 rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_MEDIA_STATUS,
2261 rtllib_disassociate(rtllib);
2263 rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_BSSID,
2264 rtllib->current_network.bssid);
2268 static void rtllib_MgntDisconnectAP(struct rtllib_device *rtllib, u8 asRsn)
2270 bool bFilterOutNonAssociatedBSSID = false;
2272 bFilterOutNonAssociatedBSSID = false;
2273 rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_CECHK_BSSID,
2274 (u8 *)(&bFilterOutNonAssociatedBSSID));
2275 rtllib_MlmeDisassociateRequest(rtllib, rtllib->current_network.bssid,
2278 rtllib->link_state = MAC80211_NOLINK;
2281 bool rtllib_MgntDisconnect(struct rtllib_device *rtllib, u8 asRsn)
2283 if (rtllib->ps != RTLLIB_PS_DISABLED)
2284 rtllib->sta_wake_up(rtllib->dev);
2286 if (rtllib->link_state == MAC80211_LINKED) {
2287 if (rtllib->iw_mode == IW_MODE_INFRA)
2288 rtllib_MgntDisconnectAP(rtllib, asRsn);
2293 EXPORT_SYMBOL(rtllib_MgntDisconnect);
2295 void notify_wx_assoc_event(struct rtllib_device *ieee)
2297 union iwreq_data wrqu;
2299 if (ieee->cannot_notify)
2302 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
2303 if (ieee->link_state == MAC80211_LINKED) {
2304 memcpy(wrqu.ap_addr.sa_data, ieee->current_network.bssid,
2307 netdev_info(ieee->dev, "%s(): Tell user space disconnected\n",
2309 eth_zero_addr(wrqu.ap_addr.sa_data);
2311 wireless_send_event(ieee->dev, SIOCGIWAP, &wrqu, NULL);
2313 EXPORT_SYMBOL(notify_wx_assoc_event);