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>
23 static void rtllib_sta_wakeup(struct rtllib_device *ieee, short nl);
25 static short rtllib_is_54g(struct rtllib_network *net)
27 return (net->rates_ex_len > 0) || (net->rates_len > 4);
30 /* returns the total length needed for placing the RATE MFIE
31 * tag and the EXTENDED RATE MFIE tag if needed.
32 * It encludes two bytes per tag for the tag itself and its len
34 static unsigned int rtllib_MFIE_rate_len(struct rtllib_device *ieee)
36 unsigned int rate_len = 0;
38 rate_len = RTLLIB_CCK_RATE_LEN + 2;
39 rate_len += RTLLIB_OFDM_RATE_LEN + 2;
44 /* place the MFIE rate, tag to the memory (double) pointed.
45 * Then it updates the pointer so that
46 * it points after the new MFIE tag added.
48 static void rtllib_MFIE_Brate(struct rtllib_device *ieee, u8 **tag_p)
52 *tag++ = MFIE_TYPE_RATES;
54 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_1MB;
55 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_2MB;
56 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_5MB;
57 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_CCK_RATE_11MB;
59 /* We may add an option for custom rates that specific HW
65 static void rtllib_MFIE_Grate(struct rtllib_device *ieee, u8 **tag_p)
69 *tag++ = MFIE_TYPE_RATES_EX;
71 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_6MB;
72 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_9MB;
73 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_12MB;
74 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_18MB;
75 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_24MB;
76 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_36MB;
77 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_48MB;
78 *tag++ = RTLLIB_BASIC_RATE_MASK | RTLLIB_OFDM_RATE_54MB;
80 /* We may add an option for custom rates that specific HW might
86 static void rtllib_WMM_Info(struct rtllib_device *ieee, u8 **tag_p)
90 *tag++ = MFIE_TYPE_GENERIC;
102 static void rtllib_TURBO_Info(struct rtllib_device *ieee, u8 **tag_p)
106 *tag++ = MFIE_TYPE_GENERIC;
117 netdev_alert(ieee->dev, "This is enable turbo mode IE process\n");
120 static void enqueue_mgmt(struct rtllib_device *ieee, struct sk_buff *skb)
124 nh = (ieee->mgmt_queue_head + 1) % MGMT_QUEUE_NUM;
126 /* if the queue is full but we have newer frames then
127 * just overwrites the oldest.
129 * if (nh == ieee->mgmt_queue_tail)
132 ieee->mgmt_queue_head = nh;
133 ieee->mgmt_queue_ring[nh] = skb;
136 static void init_mgmt_queue(struct rtllib_device *ieee)
138 ieee->mgmt_queue_tail = 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 BasicRate = ieee->current_network.rates[i] & 0x7F;
149 if (!rtllib_is_cck_rate(BasicRate)) {
150 if (QueryRate == 0) {
151 QueryRate = BasicRate;
153 if (BasicRate < QueryRate)
154 QueryRate = BasicRate;
159 if (QueryRate == 0) {
161 netdev_info(ieee->dev, "No BasicRate 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;
177 if (ieee->mode == WIRELESS_MODE_N_24G && !ht_info->bCurSuppCCK)
186 inline void softmac_mgmt_xmit(struct sk_buff *skb, struct rtllib_device *ieee)
189 short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
190 struct ieee80211_hdr_3addr *header =
191 (struct ieee80211_hdr_3addr *)skb->data;
193 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 8);
195 spin_lock_irqsave(&ieee->lock, flags);
197 /* called with 2nd param 0, no mgmt lock required */
198 rtllib_sta_wakeup(ieee, 0);
200 if (ieee80211_is_beacon(header->frame_control))
201 tcb_desc->queue_index = BEACON_QUEUE;
203 tcb_desc->queue_index = MGNT_QUEUE;
205 if (ieee->disable_mgnt_queue)
206 tcb_desc->queue_index = HIGH_QUEUE;
208 tcb_desc->data_rate = MgntQuery_MgntFrameTxRate(ieee);
209 tcb_desc->ratr_index = 7;
210 tcb_desc->tx_dis_rate_fallback = 1;
211 tcb_desc->tx_use_drv_assinged_rate = 1;
213 if (ieee->queue_stop) {
214 enqueue_mgmt(ieee, skb);
216 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
218 if (ieee->seq_ctrl[0] == 0xFFF)
219 ieee->seq_ctrl[0] = 0;
223 /* avoid watchdog triggers */
224 ieee->softmac_data_hard_start_xmit(skb, ieee->dev,
228 spin_unlock_irqrestore(&ieee->lock, flags);
230 spin_unlock_irqrestore(&ieee->lock, flags);
231 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags);
233 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
235 if (ieee->seq_ctrl[0] == 0xFFF)
236 ieee->seq_ctrl[0] = 0;
240 /* check whether the managed packet queued greater than 5 */
241 if (!ieee->check_nic_enough_desc(ieee->dev,
242 tcb_desc->queue_index) ||
243 skb_queue_len(&ieee->skb_waitQ[tcb_desc->queue_index]) ||
245 /* insert the skb packet to the management queue
247 * as for the completion function, it does not need
248 * to check it any more.
250 netdev_info(ieee->dev,
251 "%s():insert to waitqueue, queue_index:%d!\n",
252 __func__, tcb_desc->queue_index);
253 skb_queue_tail(&ieee->skb_waitQ[tcb_desc->queue_index],
256 ieee->softmac_hard_start_xmit(skb, ieee->dev);
258 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags);
263 softmac_ps_mgmt_xmit(struct sk_buff *skb,
264 struct rtllib_device *ieee)
266 short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
267 struct ieee80211_hdr_3addr *header =
268 (struct ieee80211_hdr_3addr *)skb->data;
270 struct cb_desc *tcb_desc = (struct cb_desc *)(skb->cb + 8);
272 fc = le16_to_cpu(header->frame_control);
273 type = WLAN_FC_GET_TYPE(fc);
274 stype = WLAN_FC_GET_STYPE(fc);
276 if (stype != IEEE80211_STYPE_PSPOLL)
277 tcb_desc->queue_index = MGNT_QUEUE;
279 tcb_desc->queue_index = HIGH_QUEUE;
281 if (ieee->disable_mgnt_queue)
282 tcb_desc->queue_index = HIGH_QUEUE;
284 tcb_desc->data_rate = MgntQuery_MgntFrameTxRate(ieee);
285 tcb_desc->ratr_index = 7;
286 tcb_desc->tx_dis_rate_fallback = 1;
287 tcb_desc->tx_use_drv_assinged_rate = 1;
289 if (type != RTLLIB_FTYPE_CTL) {
290 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
292 if (ieee->seq_ctrl[0] == 0xFFF)
293 ieee->seq_ctrl[0] = 0;
297 /* avoid watchdog triggers */
298 ieee->softmac_data_hard_start_xmit(skb, ieee->dev,
302 if (type != RTLLIB_FTYPE_CTL) {
303 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
305 if (ieee->seq_ctrl[0] == 0xFFF)
306 ieee->seq_ctrl[0] = 0;
310 ieee->softmac_hard_start_xmit(skb, ieee->dev);
314 static inline struct sk_buff *rtllib_probe_req(struct rtllib_device *ieee)
316 unsigned int len, rate_len;
319 struct rtllib_probe_request *req;
321 len = ieee->current_network.ssid_len;
323 rate_len = rtllib_MFIE_rate_len(ieee);
325 skb = dev_alloc_skb(sizeof(struct rtllib_probe_request) +
326 2 + len + rate_len + ieee->tx_headroom);
331 skb_reserve(skb, ieee->tx_headroom);
333 req = skb_put(skb, sizeof(struct rtllib_probe_request));
334 req->header.frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
335 req->header.duration_id = 0;
337 eth_broadcast_addr(req->header.addr1);
338 ether_addr_copy(req->header.addr2, ieee->dev->dev_addr);
339 eth_broadcast_addr(req->header.addr3);
341 tag = skb_put(skb, len + 2 + rate_len);
343 *tag++ = MFIE_TYPE_SSID;
345 memcpy(tag, ieee->current_network.ssid, len);
348 rtllib_MFIE_Brate(ieee, &tag);
349 rtllib_MFIE_Grate(ieee, &tag);
354 static struct sk_buff *rtllib_get_beacon_(struct rtllib_device *ieee);
356 static void rtllib_send_beacon(struct rtllib_device *ieee)
362 skb = rtllib_get_beacon_(ieee);
365 softmac_mgmt_xmit(skb, ieee);
366 ieee->softmac_stats.tx_beacons++;
369 if (ieee->beacon_txing && ieee->ieee_up)
370 mod_timer(&ieee->beacon_timer, jiffies +
371 (msecs_to_jiffies(ieee->current_network.beacon_interval - 5)));
374 static void rtllib_send_beacon_cb(struct timer_list *t)
376 struct rtllib_device *ieee =
377 from_timer(ieee, t, beacon_timer);
380 spin_lock_irqsave(&ieee->beacon_lock, flags);
381 rtllib_send_beacon(ieee);
382 spin_unlock_irqrestore(&ieee->beacon_lock, flags);
385 /* Enables network monitor mode, all rx packets will be received. */
386 void rtllib_EnableNetMonitorMode(struct net_device *dev,
389 struct rtllib_device *ieee = netdev_priv_rsl(dev);
391 netdev_info(dev, "========>Enter Monitor Mode\n");
393 ieee->AllowAllDestAddrHandler(dev, true, !bInitState);
396 /* Disables network monitor mode. Only packets destinated to
397 * us will be received.
399 void rtllib_DisableNetMonitorMode(struct net_device *dev,
402 struct rtllib_device *ieee = netdev_priv_rsl(dev);
404 netdev_info(dev, "========>Exit Monitor Mode\n");
406 ieee->AllowAllDestAddrHandler(dev, false, !bInitState);
409 static void rtllib_send_probe(struct rtllib_device *ieee)
413 skb = rtllib_probe_req(ieee);
415 softmac_mgmt_xmit(skb, ieee);
416 ieee->softmac_stats.tx_probe_rq++;
420 static void rtllib_send_probe_requests(struct rtllib_device *ieee)
422 if (ieee->softmac_features & IEEE_SOFTMAC_PROBERQ) {
423 rtllib_send_probe(ieee);
424 rtllib_send_probe(ieee);
428 static void rtllib_update_active_chan_map(struct rtllib_device *ieee)
430 memcpy(ieee->active_channel_map, GET_DOT11D_INFO(ieee)->channel_map,
431 MAX_CHANNEL_NUMBER + 1);
434 /* this performs syncro scan blocking the caller until all channels
435 * in the allowed channel map has been checked.
437 static void rtllib_softmac_scan_syncro(struct rtllib_device *ieee)
439 union iwreq_data wrqu;
442 rtllib_update_active_chan_map(ieee);
444 ieee->be_scan_inprogress = true;
446 mutex_lock(&ieee->scan_mutex);
451 if (ch > MAX_CHANNEL_NUMBER)
452 goto out; /* scan completed */
453 } while (!ieee->active_channel_map[ch]);
455 /* this function can be called in two situations
456 * 1- We have switched to ad-hoc mode and we are
457 * performing a complete syncro scan before conclude
458 * there are no interesting cell and to create a
459 * new one. In this case the link state is
460 * MAC80211_NOLINK until we found an interesting cell.
461 * If so the ieee8021_new_net, called by the RX path
462 * will set the state to MAC80211_LINKED, so we stop
464 * 2- We are linked and the root uses run iwlist scan.
465 * So we switch to MAC80211_LINKED_SCANNING to remember
466 * that we are still logically linked (not interested in
467 * new network events, despite for updating the net list,
468 * but we are temporarly 'unlinked' as the driver shall
469 * not filter RX frames and the channel is changing.
470 * So the only situation in which are interested is to check
471 * if the state become LINKED because of the #1 situation
474 if (ieee->link_state == MAC80211_LINKED)
476 if (ieee->sync_scan_hurryup) {
477 netdev_info(ieee->dev,
478 "============>sync_scan_hurryup out\n");
482 ieee->set_chan(ieee->dev, ch);
483 if (ieee->active_channel_map[ch] == 1)
484 rtllib_send_probe_requests(ieee);
486 /* this prevent excessive time wait when we
487 * need to wait for a syncro scan to end..
489 msleep_interruptible_rsl(RTLLIB_SOFTMAC_SCAN_TIME);
492 ieee->actscanning = false;
493 ieee->sync_scan_hurryup = 0;
495 if (ieee->link_state >= MAC80211_LINKED) {
496 if (IS_DOT11D_ENABLE(ieee))
497 dot11d_scan_complete(ieee);
499 mutex_unlock(&ieee->scan_mutex);
501 ieee->be_scan_inprogress = false;
503 memset(&wrqu, 0, sizeof(wrqu));
504 wireless_send_event(ieee->dev, SIOCGIWSCAN, &wrqu, NULL);
507 static void rtllib_softmac_scan_wq(void *data)
509 struct rtllib_device *ieee = container_of_dwork_rsl(data,
510 struct rtllib_device, softmac_scan_wq);
511 u8 last_channel = ieee->current_network.channel;
513 rtllib_update_active_chan_map(ieee);
517 if (rtllib_act_scanning(ieee, true))
520 mutex_lock(&ieee->scan_mutex);
522 if (ieee->rf_power_state == rf_off) {
523 netdev_info(ieee->dev,
524 "======>%s():rf state is rf_off, return\n",
530 ieee->current_network.channel =
531 (ieee->current_network.channel + 1) %
533 if (ieee->scan_watch_dog++ > MAX_CHANNEL_NUMBER) {
534 if (!ieee->active_channel_map[ieee->current_network.channel])
535 ieee->current_network.channel = 6;
536 goto out; /* no good chans */
538 } while (!ieee->active_channel_map[ieee->current_network.channel]);
540 if (ieee->scanning_continue == 0)
543 ieee->set_chan(ieee->dev, ieee->current_network.channel);
545 if (ieee->active_channel_map[ieee->current_network.channel] == 1)
546 rtllib_send_probe_requests(ieee);
548 schedule_delayed_work(&ieee->softmac_scan_wq,
549 msecs_to_jiffies(RTLLIB_SOFTMAC_SCAN_TIME));
551 mutex_unlock(&ieee->scan_mutex);
555 if (IS_DOT11D_ENABLE(ieee))
556 dot11d_scan_complete(ieee);
557 ieee->current_network.channel = last_channel;
560 ieee->actscanning = false;
561 ieee->scan_watch_dog = 0;
562 ieee->scanning_continue = 0;
563 mutex_unlock(&ieee->scan_mutex);
566 static void rtllib_softmac_stop_scan(struct rtllib_device *ieee)
568 mutex_lock(&ieee->scan_mutex);
569 ieee->scan_watch_dog = 0;
570 if (ieee->scanning_continue == 1) {
571 ieee->scanning_continue = 0;
572 ieee->actscanning = false;
573 mutex_unlock(&ieee->scan_mutex);
574 cancel_delayed_work_sync(&ieee->softmac_scan_wq);
576 mutex_unlock(&ieee->scan_mutex);
580 void rtllib_stop_scan(struct rtllib_device *ieee)
582 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
583 rtllib_softmac_stop_scan(ieee);
585 EXPORT_SYMBOL(rtllib_stop_scan);
587 void rtllib_stop_scan_syncro(struct rtllib_device *ieee)
589 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
590 ieee->sync_scan_hurryup = 1;
592 EXPORT_SYMBOL(rtllib_stop_scan_syncro);
594 bool rtllib_act_scanning(struct rtllib_device *ieee, bool sync_scan)
596 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
598 return ieee->be_scan_inprogress;
600 return ieee->actscanning || ieee->be_scan_inprogress;
602 return test_bit(STATUS_SCANNING, &ieee->status);
605 EXPORT_SYMBOL(rtllib_act_scanning);
607 /* called with ieee->lock held */
608 static void rtllib_start_scan(struct rtllib_device *ieee)
610 ieee->rtllib_ips_leave_wq(ieee->dev);
612 if (IS_DOT11D_ENABLE(ieee)) {
613 if (IS_COUNTRY_IE_VALID(ieee))
614 RESET_CIE_WATCHDOG(ieee);
616 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
617 if (ieee->scanning_continue == 0) {
618 ieee->actscanning = true;
619 ieee->scanning_continue = 1;
620 schedule_delayed_work(&ieee->softmac_scan_wq, 0);
625 /* called with wx_mutex held */
626 void rtllib_start_scan_syncro(struct rtllib_device *ieee)
628 if (IS_DOT11D_ENABLE(ieee)) {
629 if (IS_COUNTRY_IE_VALID(ieee))
630 RESET_CIE_WATCHDOG(ieee);
632 ieee->sync_scan_hurryup = 0;
633 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
634 rtllib_softmac_scan_syncro(ieee);
636 EXPORT_SYMBOL(rtllib_start_scan_syncro);
638 static inline struct sk_buff *
639 rtllib_authentication_req(struct rtllib_network *beacon,
640 struct rtllib_device *ieee,
641 int challengelen, u8 *daddr)
644 struct rtllib_authentication *auth;
647 len = sizeof(struct rtllib_authentication) + challengelen +
648 ieee->tx_headroom + 4;
649 skb = dev_alloc_skb(len);
654 skb_reserve(skb, ieee->tx_headroom);
656 auth = skb_put(skb, sizeof(struct rtllib_authentication));
658 auth->header.frame_control = cpu_to_le16(IEEE80211_STYPE_AUTH);
660 auth->header.frame_control |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
662 auth->header.duration_id = cpu_to_le16(0x013a);
663 ether_addr_copy(auth->header.addr1, beacon->bssid);
664 ether_addr_copy(auth->header.addr2, ieee->dev->dev_addr);
665 ether_addr_copy(auth->header.addr3, beacon->bssid);
666 if (ieee->auth_mode == 0)
667 auth->algorithm = WLAN_AUTH_OPEN;
668 else if (ieee->auth_mode == 1)
669 auth->algorithm = cpu_to_le16(WLAN_AUTH_SHARED_KEY);
670 else if (ieee->auth_mode == 2)
671 auth->algorithm = WLAN_AUTH_OPEN;
672 auth->transaction = cpu_to_le16(ieee->associate_seq);
673 ieee->associate_seq++;
675 auth->status = cpu_to_le16(WLAN_STATUS_SUCCESS);
680 static struct sk_buff *rtllib_probe_resp(struct rtllib_device *ieee,
685 struct rtllib_probe_response *beacon_buf;
686 struct sk_buff *skb = NULL;
688 int atim_len, erp_len;
689 struct lib80211_crypt_data *crypt;
691 char *ssid = ieee->current_network.ssid;
692 int ssid_len = ieee->current_network.ssid_len;
693 int rate_len = ieee->current_network.rates_len + 2;
694 int rate_ex_len = ieee->current_network.rates_ex_len;
695 int wpa_ie_len = ieee->wpa_ie_len;
696 u8 erpinfo_content = 0;
698 u8 *tmp_ht_cap_buf = NULL;
699 u8 tmp_ht_cap_len = 0;
700 u8 *tmp_ht_info_buf = NULL;
701 u8 tmp_ht_info_len = 0;
702 struct rt_hi_throughput *ht_info = ieee->ht_info;
703 u8 *tmp_generic_ie_buf = NULL;
704 u8 tmp_generic_ie_len = 0;
709 if (ieee->current_network.capability & WLAN_CAPABILITY_IBSS)
714 if ((ieee->current_network.mode == WIRELESS_MODE_G) ||
715 (ieee->current_network.mode == WIRELESS_MODE_N_24G &&
716 ieee->ht_info->bCurSuppCCK)) {
719 if (ieee->current_network.buseprotection)
720 erpinfo_content |= ERP_UseProtection;
725 crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
726 encrypt = crypt && crypt->ops &&
727 ((strcmp(crypt->ops->name, "R-WEP") == 0 || wpa_ie_len));
728 if (ieee->ht_info->current_ht_support) {
729 tmp_ht_cap_buf = (u8 *)&(ieee->ht_info->SelfHTCap);
730 tmp_ht_cap_len = sizeof(ieee->ht_info->SelfHTCap);
731 tmp_ht_info_buf = (u8 *)&(ieee->ht_info->SelfHTInfo);
732 tmp_ht_info_len = sizeof(ieee->ht_info->SelfHTInfo);
733 HTConstructCapabilityElement(ieee, tmp_ht_cap_buf,
734 &tmp_ht_cap_len, encrypt, false);
735 HTConstructInfoElement(ieee, tmp_ht_info_buf, &tmp_ht_info_len,
738 if (ht_info->reg_rt2rt_aggregation) {
739 tmp_generic_ie_buf = ieee->ht_info->sz_rt2rt_agg_buf;
741 sizeof(ieee->ht_info->sz_rt2rt_agg_buf);
742 HTConstructRT2RTAggElement(ieee, tmp_generic_ie_buf,
743 &tmp_generic_ie_len);
747 beacon_size = sizeof(struct rtllib_probe_response) + 2 +
748 ssid_len + 3 + rate_len + rate_ex_len + atim_len + erp_len
749 + wpa_ie_len + ieee->tx_headroom;
750 skb = dev_alloc_skb(beacon_size);
754 skb_reserve(skb, ieee->tx_headroom);
756 beacon_buf = skb_put(skb, (beacon_size - ieee->tx_headroom));
757 ether_addr_copy(beacon_buf->header.addr1, dest);
758 ether_addr_copy(beacon_buf->header.addr2, ieee->dev->dev_addr);
759 ether_addr_copy(beacon_buf->header.addr3, ieee->current_network.bssid);
761 beacon_buf->header.duration_id = 0;
762 beacon_buf->beacon_interval =
763 cpu_to_le16(ieee->current_network.beacon_interval);
764 beacon_buf->capability =
765 cpu_to_le16(ieee->current_network.capability &
766 WLAN_CAPABILITY_IBSS);
767 beacon_buf->capability |=
768 cpu_to_le16(ieee->current_network.capability &
769 WLAN_CAPABILITY_SHORT_PREAMBLE);
771 if (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
772 beacon_buf->capability |=
773 cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
775 crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
777 beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
779 beacon_buf->header.frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_RESP);
780 beacon_buf->info_element[0].id = MFIE_TYPE_SSID;
781 beacon_buf->info_element[0].len = ssid_len;
783 tag = (u8 *)beacon_buf->info_element[0].data;
785 memcpy(tag, ssid, ssid_len);
789 *(tag++) = MFIE_TYPE_RATES;
790 *(tag++) = rate_len - 2;
791 memcpy(tag, ieee->current_network.rates, rate_len - 2);
794 *(tag++) = MFIE_TYPE_DS_SET;
796 *(tag++) = ieee->current_network.channel;
800 *(tag++) = MFIE_TYPE_IBSS_SET;
802 val16 = ieee->current_network.atim_window;
803 memcpy((u8 *)tag, (u8 *)&val16, 2);
808 *(tag++) = MFIE_TYPE_ERP;
810 *(tag++) = erpinfo_content;
813 *(tag++) = MFIE_TYPE_RATES_EX;
814 *(tag++) = rate_ex_len - 2;
815 memcpy(tag, ieee->current_network.rates_ex, rate_ex_len - 2);
816 tag += rate_ex_len - 2;
820 memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
821 tag += ieee->wpa_ie_len;
826 static struct sk_buff *rtllib_null_func(struct rtllib_device *ieee, short pwr)
829 struct ieee80211_hdr_3addr *hdr;
831 skb = dev_alloc_skb(sizeof(struct ieee80211_hdr_3addr) + ieee->tx_headroom);
835 skb_reserve(skb, ieee->tx_headroom);
837 hdr = skb_put(skb, sizeof(struct ieee80211_hdr_3addr));
839 ether_addr_copy(hdr->addr1, ieee->current_network.bssid);
840 ether_addr_copy(hdr->addr2, ieee->dev->dev_addr);
841 ether_addr_copy(hdr->addr3, ieee->current_network.bssid);
843 hdr->frame_control = cpu_to_le16(RTLLIB_FTYPE_DATA |
844 IEEE80211_STYPE_NULLFUNC | IEEE80211_FCTL_TODS |
845 (pwr ? IEEE80211_FCTL_PM : 0));
850 static struct sk_buff *rtllib_pspoll_func(struct rtllib_device *ieee)
853 struct ieee80211_pspoll *hdr;
855 skb = dev_alloc_skb(sizeof(struct ieee80211_pspoll) + ieee->tx_headroom);
859 skb_reserve(skb, ieee->tx_headroom);
861 hdr = skb_put(skb, sizeof(struct ieee80211_pspoll));
863 ether_addr_copy(hdr->bssid, ieee->current_network.bssid);
864 ether_addr_copy(hdr->ta, ieee->dev->dev_addr);
866 hdr->aid = cpu_to_le16(ieee->assoc_id | 0xc000);
867 hdr->frame_control = cpu_to_le16(RTLLIB_FTYPE_CTL | IEEE80211_STYPE_PSPOLL |
873 static inline int SecIsInPMKIDList(struct rtllib_device *ieee, u8 *bssid)
878 if ((ieee->PMKIDList[i].bUsed) &&
879 (memcmp(ieee->PMKIDList[i].Bssid, bssid, ETH_ALEN) == 0))
882 } while (i < NUM_PMKID_CACHE);
884 if (i == NUM_PMKID_CACHE)
889 static inline struct sk_buff *
890 rtllib_association_req(struct rtllib_network *beacon,
891 struct rtllib_device *ieee)
894 struct rtllib_assoc_request_frame *hdr;
897 u8 *ht_cap_buf = NULL;
899 u8 *realtek_ie_buf = NULL;
900 u8 realtek_ie_len = 0;
901 int wpa_ie_len = ieee->wpa_ie_len;
902 int wps_ie_len = ieee->wps_ie_len;
903 unsigned int ckip_ie_len = 0;
904 unsigned int ccxrm_ie_len = 0;
905 unsigned int cxvernum_ie_len = 0;
906 struct lib80211_crypt_data *crypt;
910 unsigned int rate_len = (beacon->rates_len ?
911 (beacon->rates_len + 2) : 0) +
912 (beacon->rates_ex_len ? (beacon->rates_ex_len) +
915 unsigned int wmm_info_len = beacon->qos_data.supported ? 9 : 0;
916 unsigned int turbo_info_len = beacon->Turbo_Enable ? 9 : 0;
920 crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
922 encrypt = crypt && crypt->ops &&
923 ((strcmp(crypt->ops->name, "R-WEP") == 0 ||
928 if ((ieee->rtllib_ap_sec_type &&
929 (ieee->rtllib_ap_sec_type(ieee) & SEC_ALG_TKIP)) ||
930 ieee->bForcedBgMode) {
931 ieee->ht_info->enable_ht = 0;
932 ieee->mode = WIRELESS_MODE_G;
935 if (ieee->ht_info->current_ht_support && ieee->ht_info->enable_ht) {
936 ht_cap_buf = (u8 *)&(ieee->ht_info->SelfHTCap);
937 ht_cap_len = sizeof(ieee->ht_info->SelfHTCap);
938 HTConstructCapabilityElement(ieee, ht_cap_buf, &ht_cap_len,
940 if (ieee->ht_info->current_rt2rt_aggregation) {
941 realtek_ie_buf = ieee->ht_info->sz_rt2rt_agg_buf;
943 sizeof(ieee->ht_info->sz_rt2rt_agg_buf);
944 HTConstructRT2RTAggElement(ieee, realtek_ie_buf,
949 if (beacon->bCkipSupported)
950 ckip_ie_len = 30 + 2;
951 if (beacon->bCcxRmEnable)
952 ccxrm_ie_len = 6 + 2;
953 if (beacon->BssCcxVerNumber >= 2)
954 cxvernum_ie_len = 5 + 2;
956 PMKCacheIdx = SecIsInPMKIDList(ieee, ieee->current_network.bssid);
957 if (PMKCacheIdx >= 0) {
959 netdev_info(ieee->dev, "[PMK cache]: WPA2 IE length: %x\n",
962 len = sizeof(struct rtllib_assoc_request_frame) + 2
976 skb = dev_alloc_skb(len);
981 skb_reserve(skb, ieee->tx_headroom);
983 hdr = skb_put(skb, sizeof(struct rtllib_assoc_request_frame) + 2);
985 hdr->header.frame_control = cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ);
986 hdr->header.duration_id = cpu_to_le16(37);
987 ether_addr_copy(hdr->header.addr1, beacon->bssid);
988 ether_addr_copy(hdr->header.addr2, ieee->dev->dev_addr);
989 ether_addr_copy(hdr->header.addr3, beacon->bssid);
991 ether_addr_copy(ieee->ap_mac_addr, beacon->bssid);
993 hdr->capability = cpu_to_le16(WLAN_CAPABILITY_ESS);
994 if (beacon->capability & WLAN_CAPABILITY_PRIVACY)
995 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
997 if (beacon->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
998 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
1000 if (beacon->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1001 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME);
1003 hdr->listen_interval = cpu_to_le16(beacon->listen_interval);
1005 hdr->info_element[0].id = MFIE_TYPE_SSID;
1007 hdr->info_element[0].len = beacon->ssid_len;
1008 skb_put_data(skb, beacon->ssid, beacon->ssid_len);
1010 tag = skb_put(skb, rate_len);
1012 if (beacon->rates_len) {
1013 *tag++ = MFIE_TYPE_RATES;
1014 *tag++ = beacon->rates_len;
1015 for (i = 0; i < beacon->rates_len; i++)
1016 *tag++ = beacon->rates[i];
1019 if (beacon->rates_ex_len) {
1020 *tag++ = MFIE_TYPE_RATES_EX;
1021 *tag++ = beacon->rates_ex_len;
1022 for (i = 0; i < beacon->rates_ex_len; i++)
1023 *tag++ = beacon->rates_ex[i];
1026 if (beacon->bCkipSupported) {
1027 static const u8 AironetIeOui[] = {0x00, 0x01, 0x66};
1028 u8 CcxAironetBuf[30];
1029 struct octet_string osCcxAironetIE;
1031 memset(CcxAironetBuf, 0, 30);
1032 osCcxAironetIE.Octet = CcxAironetBuf;
1033 osCcxAironetIE.Length = sizeof(CcxAironetBuf);
1034 memcpy(osCcxAironetIE.Octet, AironetIeOui,
1035 sizeof(AironetIeOui));
1037 osCcxAironetIE.Octet[IE_CISCO_FLAG_POSITION] |=
1038 (SUPPORT_CKIP_PK | SUPPORT_CKIP_MIC);
1039 tag = skb_put(skb, ckip_ie_len);
1040 *tag++ = MFIE_TYPE_AIRONET;
1041 *tag++ = osCcxAironetIE.Length;
1042 memcpy(tag, osCcxAironetIE.Octet, osCcxAironetIE.Length);
1043 tag += osCcxAironetIE.Length;
1046 if (beacon->bCcxRmEnable) {
1047 static const u8 CcxRmCapBuf[] = {0x00, 0x40, 0x96, 0x01, 0x01,
1049 struct octet_string osCcxRmCap;
1051 osCcxRmCap.Octet = (u8 *)CcxRmCapBuf;
1052 osCcxRmCap.Length = sizeof(CcxRmCapBuf);
1053 tag = skb_put(skb, ccxrm_ie_len);
1054 *tag++ = MFIE_TYPE_GENERIC;
1055 *tag++ = osCcxRmCap.Length;
1056 memcpy(tag, osCcxRmCap.Octet, osCcxRmCap.Length);
1057 tag += osCcxRmCap.Length;
1060 if (beacon->BssCcxVerNumber >= 2) {
1061 u8 CcxVerNumBuf[] = {0x00, 0x40, 0x96, 0x03, 0x00};
1062 struct octet_string osCcxVerNum;
1064 CcxVerNumBuf[4] = beacon->BssCcxVerNumber;
1065 osCcxVerNum.Octet = CcxVerNumBuf;
1066 osCcxVerNum.Length = sizeof(CcxVerNumBuf);
1067 tag = skb_put(skb, cxvernum_ie_len);
1068 *tag++ = MFIE_TYPE_GENERIC;
1069 *tag++ = osCcxVerNum.Length;
1070 memcpy(tag, osCcxVerNum.Octet, osCcxVerNum.Length);
1071 tag += osCcxVerNum.Length;
1073 if (ieee->ht_info->current_ht_support && ieee->ht_info->enable_ht) {
1074 if (ieee->ht_info->ePeerHTSpecVer != HT_SPEC_VER_EWC) {
1075 tag = skb_put(skb, ht_cap_len);
1076 *tag++ = MFIE_TYPE_HT_CAP;
1077 *tag++ = ht_cap_len - 2;
1078 memcpy(tag, ht_cap_buf, ht_cap_len - 2);
1079 tag += ht_cap_len - 2;
1084 skb_put_data(skb, ieee->wpa_ie, ieee->wpa_ie_len);
1086 if (PMKCacheIdx >= 0) {
1087 tag = skb_put(skb, 18);
1090 memcpy((tag + 2), &ieee->PMKIDList[PMKCacheIdx].PMKID,
1095 tag = skb_put(skb, wmm_info_len);
1096 rtllib_WMM_Info(ieee, &tag);
1099 if (wps_ie_len && ieee->wps_ie)
1100 skb_put_data(skb, ieee->wps_ie, wps_ie_len);
1102 if (turbo_info_len) {
1103 tag = skb_put(skb, turbo_info_len);
1104 rtllib_TURBO_Info(ieee, &tag);
1107 if (ieee->ht_info->current_ht_support && ieee->ht_info->enable_ht) {
1108 if (ieee->ht_info->ePeerHTSpecVer == HT_SPEC_VER_EWC) {
1109 tag = skb_put(skb, ht_cap_len);
1110 *tag++ = MFIE_TYPE_GENERIC;
1111 *tag++ = ht_cap_len - 2;
1112 memcpy(tag, ht_cap_buf, ht_cap_len - 2);
1113 tag += ht_cap_len - 2;
1116 if (ieee->ht_info->current_rt2rt_aggregation) {
1117 tag = skb_put(skb, realtek_ie_len);
1118 *tag++ = MFIE_TYPE_GENERIC;
1119 *tag++ = realtek_ie_len - 2;
1120 memcpy(tag, realtek_ie_buf, realtek_ie_len - 2);
1124 kfree(ieee->assocreq_ies);
1125 ieee->assocreq_ies = NULL;
1126 ies = &(hdr->info_element[0].id);
1127 ieee->assocreq_ies_len = (skb->data + skb->len) - ies;
1128 ieee->assocreq_ies = kmemdup(ies, ieee->assocreq_ies_len, GFP_ATOMIC);
1129 if (!ieee->assocreq_ies)
1130 ieee->assocreq_ies_len = 0;
1135 static void rtllib_associate_abort(struct rtllib_device *ieee)
1137 unsigned long flags;
1139 spin_lock_irqsave(&ieee->lock, flags);
1141 ieee->associate_seq++;
1143 /* don't scan, and avoid to have the RX path possibily
1144 * try again to associate. Even do not react to AUTH or
1145 * ASSOC response. Just wait for the retry wq to be scheduled.
1146 * Here we will check if there are good nets to associate
1147 * with, so we retry or just get back to NO_LINK and scanning
1149 if (ieee->link_state == RTLLIB_ASSOCIATING_AUTHENTICATING) {
1150 netdev_dbg(ieee->dev, "Authentication failed\n");
1151 ieee->softmac_stats.no_auth_rs++;
1153 netdev_dbg(ieee->dev, "Association failed\n");
1154 ieee->softmac_stats.no_ass_rs++;
1157 ieee->link_state = RTLLIB_ASSOCIATING_RETRY;
1159 schedule_delayed_work(&ieee->associate_retry_wq,
1160 RTLLIB_SOFTMAC_ASSOC_RETRY_TIME);
1162 spin_unlock_irqrestore(&ieee->lock, flags);
1165 static void rtllib_associate_abort_cb(struct timer_list *t)
1167 struct rtllib_device *dev = from_timer(dev, t, associate_timer);
1169 rtllib_associate_abort(dev);
1172 static void rtllib_associate_step1(struct rtllib_device *ieee, u8 *daddr)
1174 struct rtllib_network *beacon = &ieee->current_network;
1175 struct sk_buff *skb;
1177 netdev_dbg(ieee->dev, "Stopping scan\n");
1179 ieee->softmac_stats.tx_auth_rq++;
1181 skb = rtllib_authentication_req(beacon, ieee, 0, daddr);
1184 rtllib_associate_abort(ieee);
1186 ieee->link_state = RTLLIB_ASSOCIATING_AUTHENTICATING;
1187 netdev_dbg(ieee->dev, "Sending authentication request\n");
1188 softmac_mgmt_xmit(skb, ieee);
1189 if (!timer_pending(&ieee->associate_timer)) {
1190 ieee->associate_timer.expires = jiffies + (HZ / 2);
1191 add_timer(&ieee->associate_timer);
1196 static void rtllib_auth_challenge(struct rtllib_device *ieee, u8 *challenge,
1200 struct sk_buff *skb;
1201 struct rtllib_network *beacon = &ieee->current_network;
1203 ieee->associate_seq++;
1204 ieee->softmac_stats.tx_auth_rq++;
1206 skb = rtllib_authentication_req(beacon, ieee, chlen + 2, beacon->bssid);
1209 rtllib_associate_abort(ieee);
1211 c = skb_put(skb, chlen + 2);
1212 *(c++) = MFIE_TYPE_CHALLENGE;
1214 memcpy(c, challenge, chlen);
1216 netdev_dbg(ieee->dev,
1217 "Sending authentication challenge response\n");
1219 rtllib_encrypt_fragment(ieee, skb,
1220 sizeof(struct ieee80211_hdr_3addr));
1222 softmac_mgmt_xmit(skb, ieee);
1223 mod_timer(&ieee->associate_timer, jiffies + (HZ / 2));
1228 static void rtllib_associate_step2(struct rtllib_device *ieee)
1230 struct sk_buff *skb;
1231 struct rtllib_network *beacon = &ieee->current_network;
1233 del_timer_sync(&ieee->associate_timer);
1235 netdev_dbg(ieee->dev, "Sending association request\n");
1237 ieee->softmac_stats.tx_ass_rq++;
1238 skb = rtllib_association_req(beacon, ieee);
1240 rtllib_associate_abort(ieee);
1242 softmac_mgmt_xmit(skb, ieee);
1243 mod_timer(&ieee->associate_timer, jiffies + (HZ / 2));
1247 static void rtllib_associate_complete_wq(void *data)
1249 struct rtllib_device *ieee = (struct rtllib_device *)
1251 struct rtllib_device,
1252 associate_complete_wq);
1253 struct rt_pwr_save_ctrl *psc = &ieee->pwr_save_ctrl;
1255 netdev_info(ieee->dev, "Associated successfully with %pM\n",
1256 ieee->current_network.bssid);
1257 netdev_info(ieee->dev, "normal associate\n");
1258 notify_wx_assoc_event(ieee);
1260 netif_carrier_on(ieee->dev);
1261 ieee->is_roaming = false;
1262 if (rtllib_is_54g(&ieee->current_network)) {
1264 netdev_info(ieee->dev, "Using G rates:%d\n", ieee->rate);
1267 ieee->set_wireless_mode(ieee->dev, WIRELESS_MODE_B);
1268 netdev_info(ieee->dev, "Using B rates:%d\n", ieee->rate);
1270 if (ieee->ht_info->current_ht_support && ieee->ht_info->enable_ht) {
1271 netdev_info(ieee->dev, "Successfully associated, ht enabled\n");
1274 netdev_info(ieee->dev,
1275 "Successfully associated, ht not enabled(%d, %d)\n",
1276 ieee->ht_info->current_ht_support,
1277 ieee->ht_info->enable_ht);
1278 memset(ieee->dot11ht_oper_rate_set, 0, 16);
1280 ieee->link_detect_info.SlotNum = 2 * (1 +
1281 ieee->current_network.beacon_interval /
1283 if (ieee->link_detect_info.NumRecvBcnInPeriod == 0 ||
1284 ieee->link_detect_info.NumRecvDataInPeriod == 0) {
1285 ieee->link_detect_info.NumRecvBcnInPeriod = 1;
1286 ieee->link_detect_info.NumRecvDataInPeriod = 1;
1288 psc->LpsIdleCount = 0;
1289 ieee->link_change(ieee->dev);
1293 static void rtllib_sta_send_associnfo(struct rtllib_device *ieee)
1297 static void rtllib_associate_complete(struct rtllib_device *ieee)
1299 del_timer_sync(&ieee->associate_timer);
1301 ieee->link_state = MAC80211_LINKED;
1302 rtllib_sta_send_associnfo(ieee);
1304 schedule_work(&ieee->associate_complete_wq);
1307 static void rtllib_associate_procedure_wq(void *data)
1309 struct rtllib_device *ieee = container_of_dwork_rsl(data,
1310 struct rtllib_device,
1311 associate_procedure_wq);
1312 rtllib_stop_scan_syncro(ieee);
1313 ieee->rtllib_ips_leave(ieee->dev);
1314 mutex_lock(&ieee->wx_mutex);
1316 rtllib_stop_scan(ieee);
1317 HTSetConnectBwMode(ieee, HT_CHANNEL_WIDTH_20, HT_EXTCHNL_OFFSET_NO_EXT);
1318 if (ieee->rf_power_state == rf_off) {
1319 ieee->rtllib_ips_leave_wq(ieee->dev);
1320 mutex_unlock(&ieee->wx_mutex);
1323 ieee->associate_seq = 1;
1325 rtllib_associate_step1(ieee, ieee->current_network.bssid);
1327 mutex_unlock(&ieee->wx_mutex);
1330 inline void rtllib_softmac_new_net(struct rtllib_device *ieee,
1331 struct rtllib_network *net)
1333 u8 tmp_ssid[IW_ESSID_MAX_SIZE + 1];
1334 int tmp_ssid_len = 0;
1336 short apset, ssidset, ssidbroad, apmatch, ssidmatch;
1338 /* we are interested in new only if we are not associated
1339 * and we are not associating / authenticating
1341 if (ieee->link_state != MAC80211_NOLINK)
1344 if ((ieee->iw_mode == IW_MODE_INFRA) && !(net->capability &
1345 WLAN_CAPABILITY_ESS))
1348 if (ieee->iw_mode == IW_MODE_INFRA) {
1349 /* if the user specified the AP MAC, we need also the essid
1350 * This could be obtained by beacons or, if the network does not
1351 * broadcast it, it can be put manually.
1353 apset = ieee->wap_set;
1354 ssidset = ieee->ssid_set;
1355 ssidbroad = !(net->ssid_len == 0 || net->ssid[0] == '\0');
1356 apmatch = (memcmp(ieee->current_network.bssid, net->bssid,
1359 ssidmatch = (ieee->current_network.ssid_len ==
1360 net->hidden_ssid_len) &&
1361 (!strncmp(ieee->current_network.ssid,
1362 net->hidden_ssid, net->hidden_ssid_len));
1363 if (net->hidden_ssid_len > 0) {
1364 strncpy(net->ssid, net->hidden_ssid,
1365 net->hidden_ssid_len);
1366 net->ssid_len = net->hidden_ssid_len;
1371 (ieee->current_network.ssid_len == net->ssid_len) &&
1372 (!strncmp(ieee->current_network.ssid, net->ssid,
1376 /* if the user set the AP check if match.
1377 * if the network does not broadcast essid we check the
1378 * user supplied ANY essid
1379 * if the network does broadcast and the user does not set
1381 * if the network does broadcast and the user did set essid
1382 * check if essid match
1383 * if the ap is not set, check that the user set the bssid
1384 * and the network does broadcast and that those two bssid match
1386 if ((apset && apmatch &&
1387 ((ssidset && ssidbroad && ssidmatch) ||
1388 (ssidbroad && !ssidset) || (!ssidbroad && ssidset))) ||
1389 (!apset && ssidset && ssidbroad && ssidmatch) ||
1390 (ieee->is_roaming && ssidset && ssidbroad && ssidmatch)) {
1391 /* Save the essid so that if it is hidden, it is
1392 * replaced with the essid provided by the user.
1395 memcpy(tmp_ssid, ieee->current_network.ssid,
1396 ieee->current_network.ssid_len);
1397 tmp_ssid_len = ieee->current_network.ssid_len;
1399 memcpy(&ieee->current_network, net,
1400 sizeof(ieee->current_network));
1402 memcpy(ieee->current_network.ssid, tmp_ssid,
1404 ieee->current_network.ssid_len = tmp_ssid_len;
1406 netdev_info(ieee->dev,
1407 "Linking with %s,channel:%d, qos:%d, myHT:%d, networkHT:%d, mode:%x cur_net.flags:0x%x\n",
1408 ieee->current_network.ssid,
1409 ieee->current_network.channel,
1410 ieee->current_network.qos_data.supported,
1411 ieee->ht_info->enable_ht,
1412 ieee->current_network.bssht.bd_support_ht,
1413 ieee->current_network.mode,
1414 ieee->current_network.flags);
1416 if ((rtllib_act_scanning(ieee, false)) &&
1417 !(ieee->softmac_features & IEEE_SOFTMAC_SCAN))
1418 rtllib_stop_scan_syncro(ieee);
1420 HTResetIOTSetting(ieee->ht_info);
1422 if (ieee->iw_mode == IW_MODE_INFRA) {
1423 /* Join the network for the first time */
1424 ieee->AsocRetryCount = 0;
1425 if ((ieee->current_network.qos_data.supported == 1) &&
1426 ieee->current_network.bssht.bd_support_ht)
1427 HTResetSelfAndSavePeerSetting(ieee,
1428 &(ieee->current_network));
1430 ieee->ht_info->current_ht_support = false;
1432 ieee->link_state = RTLLIB_ASSOCIATING;
1433 schedule_delayed_work(
1434 &ieee->associate_procedure_wq, 0);
1436 if (rtllib_is_54g(&ieee->current_network)) {
1438 ieee->set_wireless_mode(ieee->dev, WIRELESS_MODE_G);
1439 netdev_info(ieee->dev,
1443 ieee->set_wireless_mode(ieee->dev, WIRELESS_MODE_B);
1444 netdev_info(ieee->dev,
1447 memset(ieee->dot11ht_oper_rate_set, 0, 16);
1448 ieee->link_state = MAC80211_LINKED;
1454 static void rtllib_softmac_check_all_nets(struct rtllib_device *ieee)
1456 unsigned long flags;
1457 struct rtllib_network *target;
1459 spin_lock_irqsave(&ieee->lock, flags);
1461 list_for_each_entry(target, &ieee->network_list, list) {
1462 /* if the state become different that NOLINK means
1463 * we had found what we are searching for
1466 if (ieee->link_state != MAC80211_NOLINK)
1469 if (ieee->scan_age == 0 || time_after(target->last_scanned +
1470 ieee->scan_age, jiffies))
1471 rtllib_softmac_new_net(ieee, target);
1473 spin_unlock_irqrestore(&ieee->lock, flags);
1476 static inline int auth_parse(struct net_device *dev, struct sk_buff *skb,
1477 u8 **challenge, int *chlen)
1479 struct rtllib_authentication *a;
1482 if (skb->len < (sizeof(struct rtllib_authentication) -
1483 sizeof(struct rtllib_info_element))) {
1484 netdev_dbg(dev, "invalid len in auth resp: %d\n", skb->len);
1488 a = (struct rtllib_authentication *)skb->data;
1489 if (skb->len > (sizeof(struct rtllib_authentication) + 3)) {
1490 t = skb->data + sizeof(struct rtllib_authentication);
1492 if (*(t++) == MFIE_TYPE_CHALLENGE) {
1494 *challenge = kmemdup(t, *chlen, GFP_ATOMIC);
1501 netdev_dbg(dev, "auth_parse() failed\n");
1508 static inline u16 assoc_parse(struct rtllib_device *ieee, struct sk_buff *skb,
1511 struct rtllib_assoc_response_frame *response_head;
1514 if (skb->len < sizeof(struct rtllib_assoc_response_frame)) {
1515 netdev_dbg(ieee->dev, "Invalid len in auth resp: %d\n",
1520 response_head = (struct rtllib_assoc_response_frame *)skb->data;
1521 *aid = le16_to_cpu(response_head->aid) & 0x3fff;
1523 status_code = le16_to_cpu(response_head->status);
1524 if ((status_code == WLAN_STATUS_ASSOC_DENIED_RATES ||
1525 status_code == WLAN_STATUS_CAPS_UNSUPPORTED) &&
1526 ((ieee->mode == WIRELESS_MODE_G) &&
1527 (ieee->current_network.mode == WIRELESS_MODE_N_24G) &&
1528 (ieee->AsocRetryCount++ < (RT_ASOC_RETRY_LIMIT - 1)))) {
1529 ieee->ht_info->iot_action |= HT_IOT_ACT_PURE_N_MODE;
1531 ieee->AsocRetryCount = 0;
1534 return le16_to_cpu(response_head->status);
1537 void rtllib_sta_ps_send_null_frame(struct rtllib_device *ieee, short pwr)
1539 struct sk_buff *buf = rtllib_null_func(ieee, pwr);
1542 softmac_ps_mgmt_xmit(buf, ieee);
1544 EXPORT_SYMBOL(rtllib_sta_ps_send_null_frame);
1546 void rtllib_sta_ps_send_pspoll_frame(struct rtllib_device *ieee)
1548 struct sk_buff *buf = rtllib_pspoll_func(ieee);
1551 softmac_ps_mgmt_xmit(buf, ieee);
1554 static short rtllib_sta_ps_sleep(struct rtllib_device *ieee, u64 *time)
1558 struct rt_pwr_save_ctrl *psc = &ieee->pwr_save_ctrl;
1560 if (ieee->LPSDelayCnt) {
1561 ieee->LPSDelayCnt--;
1565 dtim = ieee->current_network.dtim_data;
1566 if (!(dtim & RTLLIB_DTIM_VALID))
1568 timeout = ieee->current_network.beacon_interval;
1569 ieee->current_network.dtim_data = RTLLIB_DTIM_INVALID;
1570 /* there's no need to nofity AP that I find you buffered
1571 * with broadcast packet
1573 if (dtim & (RTLLIB_DTIM_UCAST & ieee->ps))
1576 if (!time_after(jiffies,
1577 dev_trans_start(ieee->dev) + msecs_to_jiffies(timeout)))
1579 if (!time_after(jiffies,
1580 ieee->last_rx_ps_time + msecs_to_jiffies(timeout)))
1582 if ((ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE) &&
1583 (ieee->mgmt_queue_tail != ieee->mgmt_queue_head))
1587 if (ieee->bAwakePktSent) {
1588 psc->LPSAwakeIntvl = 1;
1592 if (psc->LPSAwakeIntvl == 0)
1593 psc->LPSAwakeIntvl = 1;
1594 psc->LPSAwakeIntvl = (psc->LPSAwakeIntvl >=
1595 MaxPeriod) ? MaxPeriod :
1596 (psc->LPSAwakeIntvl + 1);
1599 u8 LPSAwakeIntvl_tmp = 0;
1600 u8 period = ieee->current_network.dtim_period;
1601 u8 count = ieee->current_network.tim.tim_count;
1604 if (psc->LPSAwakeIntvl > period)
1605 LPSAwakeIntvl_tmp = period +
1606 (psc->LPSAwakeIntvl -
1608 ((psc->LPSAwakeIntvl - period) %
1611 LPSAwakeIntvl_tmp = psc->LPSAwakeIntvl;
1614 if (psc->LPSAwakeIntvl >
1615 ieee->current_network.tim.tim_count)
1616 LPSAwakeIntvl_tmp = count +
1617 (psc->LPSAwakeIntvl - count) -
1618 ((psc->LPSAwakeIntvl - count) % period);
1620 LPSAwakeIntvl_tmp = psc->LPSAwakeIntvl;
1623 *time = ieee->current_network.last_dtim_sta_time
1624 + msecs_to_jiffies(ieee->current_network.beacon_interval *
1632 static inline void rtllib_sta_ps(struct work_struct *work)
1634 struct rtllib_device *ieee;
1637 unsigned long flags, flags2;
1639 ieee = container_of(work, struct rtllib_device, ps_task);
1641 spin_lock_irqsave(&ieee->lock, flags);
1643 if ((ieee->ps == RTLLIB_PS_DISABLED ||
1644 ieee->iw_mode != IW_MODE_INFRA ||
1645 ieee->link_state != MAC80211_LINKED)) {
1646 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1647 rtllib_sta_wakeup(ieee, 1);
1649 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1651 sleep = rtllib_sta_ps_sleep(ieee, &time);
1652 /* 2 wake, 1 sleep, 0 do nothing */
1656 if (ieee->sta_sleep == LPS_IS_SLEEP) {
1657 ieee->enter_sleep_state(ieee->dev, time);
1658 } else if (ieee->sta_sleep == LPS_IS_WAKE) {
1659 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1661 if (ieee->ps_is_queue_empty(ieee->dev)) {
1662 ieee->sta_sleep = LPS_WAIT_NULL_DATA_SEND;
1663 ieee->ack_tx_to_ieee = 1;
1664 rtllib_sta_ps_send_null_frame(ieee, 1);
1665 ieee->ps_time = time;
1667 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1670 ieee->bAwakePktSent = false;
1672 } else if (sleep == 2) {
1673 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1675 rtllib_sta_wakeup(ieee, 1);
1677 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1681 spin_unlock_irqrestore(&ieee->lock, flags);
1684 static void rtllib_sta_wakeup(struct rtllib_device *ieee, short nl)
1686 if (ieee->sta_sleep == LPS_IS_WAKE) {
1688 if (ieee->ht_info->iot_action &
1689 HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
1690 ieee->ack_tx_to_ieee = 1;
1691 rtllib_sta_ps_send_null_frame(ieee, 0);
1693 ieee->ack_tx_to_ieee = 1;
1694 rtllib_sta_ps_send_pspoll_frame(ieee);
1700 if (ieee->sta_sleep == LPS_IS_SLEEP)
1701 ieee->sta_wake_up(ieee->dev);
1703 if (ieee->ht_info->iot_action &
1704 HT_IOT_ACT_NULL_DATA_POWER_SAVING) {
1705 ieee->ack_tx_to_ieee = 1;
1706 rtllib_sta_ps_send_null_frame(ieee, 0);
1708 ieee->ack_tx_to_ieee = 1;
1709 ieee->polling = true;
1710 rtllib_sta_ps_send_pspoll_frame(ieee);
1714 ieee->sta_sleep = LPS_IS_WAKE;
1715 ieee->polling = false;
1719 void rtllib_ps_tx_ack(struct rtllib_device *ieee, short success)
1721 unsigned long flags, flags2;
1723 spin_lock_irqsave(&ieee->lock, flags);
1725 if (ieee->sta_sleep == LPS_WAIT_NULL_DATA_SEND) {
1726 /* Null frame with PS bit set */
1728 ieee->sta_sleep = LPS_IS_SLEEP;
1729 ieee->enter_sleep_state(ieee->dev, ieee->ps_time);
1731 /* if the card report not success we can't be sure the AP
1732 * has not RXed so we can't assume the AP believe us awake
1734 } else {/* 21112005 - tx again null without PS bit if lost */
1736 if ((ieee->sta_sleep == LPS_IS_WAKE) && !success) {
1737 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1738 if (ieee->ht_info->iot_action &
1739 HT_IOT_ACT_NULL_DATA_POWER_SAVING)
1740 rtllib_sta_ps_send_null_frame(ieee, 0);
1742 rtllib_sta_ps_send_pspoll_frame(ieee);
1743 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1746 spin_unlock_irqrestore(&ieee->lock, flags);
1748 EXPORT_SYMBOL(rtllib_ps_tx_ack);
1750 static void rtllib_process_action(struct rtllib_device *ieee,
1751 struct sk_buff *skb)
1753 u8 *act = skb->data + RTLLIB_3ADDR_LEN;
1762 rtllib_rx_ADDBAReq(ieee, skb);
1765 rtllib_rx_ADDBARsp(ieee, skb);
1768 rtllib_rx_DELBA(ieee, skb);
1778 rtllib_rx_assoc_resp(struct rtllib_device *ieee, struct sk_buff *skb,
1779 struct rtllib_rx_stats *rx_stats)
1784 struct rtllib_assoc_response_frame *assoc_resp;
1785 struct ieee80211_hdr_3addr *header = (struct ieee80211_hdr_3addr *)skb->data;
1786 u16 frame_ctl = le16_to_cpu(header->frame_control);
1788 netdev_dbg(ieee->dev, "received [RE]ASSOCIATION RESPONSE (%d)\n",
1789 WLAN_FC_GET_STYPE(frame_ctl));
1791 if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1792 ieee->link_state == RTLLIB_ASSOCIATING_AUTHENTICATED &&
1793 (ieee->iw_mode == IW_MODE_INFRA)) {
1794 errcode = assoc_parse(ieee, skb, &aid);
1796 struct rtllib_network *network =
1797 kzalloc(sizeof(struct rtllib_network),
1802 ieee->link_state = MAC80211_LINKED;
1803 ieee->assoc_id = aid;
1804 ieee->softmac_stats.rx_ass_ok++;
1805 /* station support qos */
1806 /* Let the register setting default with Legacy station */
1807 assoc_resp = (struct rtllib_assoc_response_frame *)skb->data;
1808 if (ieee->current_network.qos_data.supported == 1) {
1809 if (rtllib_parse_info_param(ieee, assoc_resp->info_element,
1810 rx_stats->len - sizeof(*assoc_resp),
1811 network, rx_stats)) {
1815 memcpy(ieee->ht_info->PeerHTCapBuf,
1816 network->bssht.bd_ht_cap_buf,
1817 network->bssht.bd_ht_cap_len);
1818 memcpy(ieee->ht_info->PeerHTInfoBuf,
1819 network->bssht.bd_ht_info_buf,
1820 network->bssht.bd_ht_info_len);
1821 ieee->handle_assoc_response(ieee->dev,
1822 (struct rtllib_assoc_response_frame *)header, network);
1826 kfree(ieee->assocresp_ies);
1827 ieee->assocresp_ies = NULL;
1828 ies = &(assoc_resp->info_element[0].id);
1829 ieee->assocresp_ies_len = (skb->data + skb->len) - ies;
1830 ieee->assocresp_ies = kmemdup(ies,
1831 ieee->assocresp_ies_len,
1833 if (!ieee->assocresp_ies)
1834 ieee->assocresp_ies_len = 0;
1836 rtllib_associate_complete(ieee);
1838 /* aid could not been allocated */
1839 ieee->softmac_stats.rx_ass_err++;
1840 netdev_info(ieee->dev,
1841 "Association response status code 0x%x\n",
1843 if (ieee->AsocRetryCount < RT_ASOC_RETRY_LIMIT)
1844 schedule_delayed_work(
1845 &ieee->associate_procedure_wq, 0);
1847 rtllib_associate_abort(ieee);
1853 static void rtllib_rx_auth_resp(struct rtllib_device *ieee, struct sk_buff *skb)
1858 bool bSupportNmode = true, bHalfSupportNmode = false;
1860 errcode = auth_parse(ieee->dev, skb, &challenge, &chlen);
1863 ieee->softmac_stats.rx_auth_rs_err++;
1864 netdev_info(ieee->dev,
1865 "Authentication response status code %d", errcode);
1866 rtllib_associate_abort(ieee);
1870 if (ieee->open_wep || !challenge) {
1871 ieee->link_state = RTLLIB_ASSOCIATING_AUTHENTICATED;
1872 ieee->softmac_stats.rx_auth_rs_ok++;
1873 if (!(ieee->ht_info->iot_action & HT_IOT_ACT_PURE_N_MODE)) {
1874 if (!ieee->GetNmodeSupportBySecCfg(ieee->dev)) {
1875 if (IsHTHalfNmodeAPs(ieee)) {
1876 bSupportNmode = true;
1877 bHalfSupportNmode = true;
1879 bSupportNmode = false;
1880 bHalfSupportNmode = false;
1884 /* Dummy wirless mode setting to avoid encryption issue */
1885 if (bSupportNmode) {
1886 ieee->set_wireless_mode(ieee->dev,
1887 ieee->current_network.mode);
1890 ieee->set_wireless_mode(ieee->dev, WIRELESS_MODE_G);
1893 if ((ieee->current_network.mode == WIRELESS_MODE_N_24G) &&
1894 bHalfSupportNmode) {
1895 netdev_info(ieee->dev, "======>enter half N mode\n");
1896 ieee->bHalfWirelessN24GMode = true;
1898 ieee->bHalfWirelessN24GMode = false;
1900 rtllib_associate_step2(ieee);
1902 rtllib_auth_challenge(ieee, challenge, chlen);
1907 rtllib_rx_auth(struct rtllib_device *ieee, struct sk_buff *skb,
1908 struct rtllib_rx_stats *rx_stats)
1910 if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) {
1911 if (ieee->link_state == RTLLIB_ASSOCIATING_AUTHENTICATING &&
1912 (ieee->iw_mode == IW_MODE_INFRA)) {
1913 netdev_dbg(ieee->dev,
1914 "Received authentication response");
1915 rtllib_rx_auth_resp(ieee, skb);
1922 rtllib_rx_deauth(struct rtllib_device *ieee, struct sk_buff *skb)
1924 struct ieee80211_hdr_3addr *header = (struct ieee80211_hdr_3addr *)skb->data;
1927 if (memcmp(header->addr3, ieee->current_network.bssid, ETH_ALEN) != 0)
1930 /* FIXME for now repeat all the association procedure
1931 * both for disassociation and deauthentication
1933 if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1934 ieee->link_state == MAC80211_LINKED &&
1935 (ieee->iw_mode == IW_MODE_INFRA)) {
1936 frame_ctl = le16_to_cpu(header->frame_control);
1937 netdev_info(ieee->dev,
1938 "==========>received disassoc/deauth(%x) frame, reason code:%x\n",
1939 WLAN_FC_GET_STYPE(frame_ctl),
1940 ((struct rtllib_disassoc *)skb->data)->reason);
1941 ieee->link_state = RTLLIB_ASSOCIATING;
1942 ieee->softmac_stats.reassoc++;
1943 ieee->is_roaming = true;
1944 ieee->link_detect_info.bBusyTraffic = false;
1945 rtllib_disassociate(ieee);
1946 RemovePeerTS(ieee, header->addr2);
1947 if (!(ieee->rtllib_ap_sec_type(ieee) &
1948 (SEC_ALG_CCMP | SEC_ALG_TKIP)))
1949 schedule_delayed_work(
1950 &ieee->associate_procedure_wq, 5);
1955 inline int rtllib_rx_frame_softmac(struct rtllib_device *ieee,
1956 struct sk_buff *skb,
1957 struct rtllib_rx_stats *rx_stats, u16 type,
1960 struct ieee80211_hdr_3addr *header = (struct ieee80211_hdr_3addr *)skb->data;
1963 if (!ieee->proto_started)
1966 frame_ctl = le16_to_cpu(header->frame_control);
1967 switch (WLAN_FC_GET_STYPE(frame_ctl)) {
1968 case IEEE80211_STYPE_ASSOC_RESP:
1969 case IEEE80211_STYPE_REASSOC_RESP:
1970 if (rtllib_rx_assoc_resp(ieee, skb, rx_stats) == 1)
1973 case IEEE80211_STYPE_ASSOC_REQ:
1974 case IEEE80211_STYPE_REASSOC_REQ:
1976 case IEEE80211_STYPE_AUTH:
1977 rtllib_rx_auth(ieee, skb, rx_stats);
1979 case IEEE80211_STYPE_DISASSOC:
1980 case IEEE80211_STYPE_DEAUTH:
1981 rtllib_rx_deauth(ieee, skb);
1983 case IEEE80211_STYPE_ACTION:
1984 rtllib_process_action(ieee, skb);
1992 /* following are for a simpler TX queue management.
1993 * Instead of using netif_[stop/wake]_queue the driver
1994 * will use these two functions (plus a reset one), that
1995 * will internally use the kernel netif_* and takes
1996 * care of the ieee802.11 fragmentation.
1997 * So the driver receives a fragment per time and might
1998 * call the stop function when it wants to not
1999 * have enough room to TX an entire packet.
2000 * This might be useful if each fragment needs it's own
2001 * descriptor, thus just keep a total free memory > than
2002 * the max fragmentation threshold is not enough.. If the
2003 * ieee802.11 stack passed a TXB struct then you need
2004 * to keep N free descriptors where
2005 * N = MAX_PACKET_SIZE / MIN_FRAG_TRESHOLD
2006 * In this way you need just one and the 802.11 stack
2007 * will take care of buffering fragments and pass them to
2008 * the driver later, when it wakes the queue.
2010 void rtllib_softmac_xmit(struct rtllib_txb *txb, struct rtllib_device *ieee)
2012 unsigned int queue_index = txb->queue_index;
2013 unsigned long flags;
2015 struct cb_desc *tcb_desc = NULL;
2016 unsigned long queue_len = 0;
2018 spin_lock_irqsave(&ieee->lock, flags);
2020 /* called with 2nd parm 0, no tx mgmt lock required */
2021 rtllib_sta_wakeup(ieee, 0);
2023 /* update the tx status */
2024 tcb_desc = (struct cb_desc *)(txb->fragments[0]->cb +
2026 if (tcb_desc->bMulticast)
2027 ieee->stats.multicast++;
2029 /* if xmit available, just xmit it immediately, else just insert it to
2032 for (i = 0; i < txb->nr_frags; i++) {
2033 queue_len = skb_queue_len(&ieee->skb_waitQ[queue_index]);
2034 if ((queue_len != 0) ||
2035 (!ieee->check_nic_enough_desc(ieee->dev, queue_index)) ||
2036 (ieee->queue_stop)) {
2037 /* insert the skb packet to the wait queue
2038 * as for the completion function, it does not need
2039 * to check it any more.
2041 if (queue_len < 200)
2042 skb_queue_tail(&ieee->skb_waitQ[queue_index],
2045 kfree_skb(txb->fragments[i]);
2047 ieee->softmac_data_hard_start_xmit(
2049 ieee->dev, ieee->rate);
2053 rtllib_txb_free(txb);
2055 spin_unlock_irqrestore(&ieee->lock, flags);
2058 void rtllib_reset_queue(struct rtllib_device *ieee)
2060 unsigned long flags;
2062 spin_lock_irqsave(&ieee->lock, flags);
2063 init_mgmt_queue(ieee);
2064 if (ieee->tx_pending.txb) {
2065 rtllib_txb_free(ieee->tx_pending.txb);
2066 ieee->tx_pending.txb = NULL;
2068 ieee->queue_stop = 0;
2069 spin_unlock_irqrestore(&ieee->lock, flags);
2071 EXPORT_SYMBOL(rtllib_reset_queue);
2073 void rtllib_stop_all_queues(struct rtllib_device *ieee)
2077 for (i = 0; i < ieee->dev->num_tx_queues; i++)
2078 txq_trans_cond_update(netdev_get_tx_queue(ieee->dev, i));
2080 netif_tx_stop_all_queues(ieee->dev);
2083 void rtllib_wake_all_queues(struct rtllib_device *ieee)
2085 netif_tx_wake_all_queues(ieee->dev);
2088 /* this is called only in user context, with wx_mutex held */
2089 static void rtllib_start_bss(struct rtllib_device *ieee)
2091 unsigned long flags;
2093 if (IS_DOT11D_ENABLE(ieee) && !IS_COUNTRY_IE_VALID(ieee)) {
2094 if (!ieee->global_domain)
2097 /* check if we have already found the net we
2098 * are interested in (if any).
2099 * if not (we are disassociated and we are not
2100 * in associating / authenticating phase) start the background scanning.
2102 rtllib_softmac_check_all_nets(ieee);
2104 /* ensure no-one start an associating process (thus setting
2105 * the ieee->link_state to rtllib_ASSOCIATING) while we
2106 * have just checked it and we are going to enable scan.
2107 * The rtllib_new_net function is always called with
2108 * lock held (from both rtllib_softmac_check_all_nets and
2109 * the rx path), so we cannot be in the middle of such function
2111 spin_lock_irqsave(&ieee->lock, flags);
2113 if (ieee->link_state == MAC80211_NOLINK)
2114 rtllib_start_scan(ieee);
2115 spin_unlock_irqrestore(&ieee->lock, flags);
2118 static void rtllib_link_change_wq(void *data)
2120 struct rtllib_device *ieee = container_of_dwork_rsl(data,
2121 struct rtllib_device, link_change_wq);
2122 ieee->link_change(ieee->dev);
2124 /* called only in userspace context */
2125 void rtllib_disassociate(struct rtllib_device *ieee)
2127 netif_carrier_off(ieee->dev);
2128 if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)
2129 rtllib_reset_queue(ieee);
2131 if (IS_DOT11D_ENABLE(ieee))
2133 ieee->link_state = MAC80211_NOLINK;
2134 ieee->is_set_key = false;
2137 schedule_delayed_work(&ieee->link_change_wq, 0);
2139 notify_wx_assoc_event(ieee);
2142 static void rtllib_associate_retry_wq(void *data)
2144 struct rtllib_device *ieee = container_of_dwork_rsl(data,
2145 struct rtllib_device, associate_retry_wq);
2146 unsigned long flags;
2148 mutex_lock(&ieee->wx_mutex);
2149 if (!ieee->proto_started)
2152 if (ieee->link_state != RTLLIB_ASSOCIATING_RETRY)
2155 /* until we do not set the state to MAC80211_NOLINK
2156 * there are no possibility to have someone else trying
2157 * to start an association procedure (we get here with
2158 * ieee->link_state = RTLLIB_ASSOCIATING).
2159 * When we set the state to MAC80211_NOLINK it is possible
2160 * that the RX path run an attempt to associate, but
2161 * both rtllib_softmac_check_all_nets and the
2162 * RX path works with ieee->lock held so there are no
2163 * problems. If we are still disassociated then start a scan.
2164 * the lock here is necessary to ensure no one try to start
2165 * an association procedure when we have just checked the
2166 * state and we are going to start the scan.
2168 ieee->beinretry = true;
2169 ieee->link_state = MAC80211_NOLINK;
2171 rtllib_softmac_check_all_nets(ieee);
2173 spin_lock_irqsave(&ieee->lock, flags);
2175 if (ieee->link_state == MAC80211_NOLINK)
2176 rtllib_start_scan(ieee);
2177 spin_unlock_irqrestore(&ieee->lock, flags);
2179 ieee->beinretry = false;
2181 mutex_unlock(&ieee->wx_mutex);
2184 static struct sk_buff *rtllib_get_beacon_(struct rtllib_device *ieee)
2186 static const u8 broadcast_addr[] = {
2187 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
2189 struct sk_buff *skb;
2190 struct rtllib_probe_response *b;
2192 skb = rtllib_probe_resp(ieee, broadcast_addr);
2197 b = (struct rtllib_probe_response *)skb->data;
2198 b->header.frame_control = cpu_to_le16(IEEE80211_STYPE_BEACON);
2203 struct sk_buff *rtllib_get_beacon(struct rtllib_device *ieee)
2205 struct sk_buff *skb;
2206 struct rtllib_probe_response *b;
2208 skb = rtllib_get_beacon_(ieee);
2212 b = (struct rtllib_probe_response *)skb->data;
2213 b->header.seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2215 if (ieee->seq_ctrl[0] == 0xFFF)
2216 ieee->seq_ctrl[0] = 0;
2218 ieee->seq_ctrl[0]++;
2222 EXPORT_SYMBOL(rtllib_get_beacon);
2224 void rtllib_softmac_stop_protocol(struct rtllib_device *ieee)
2226 rtllib_stop_scan_syncro(ieee);
2227 mutex_lock(&ieee->wx_mutex);
2228 rtllib_stop_protocol(ieee);
2229 mutex_unlock(&ieee->wx_mutex);
2231 EXPORT_SYMBOL(rtllib_softmac_stop_protocol);
2233 void rtllib_stop_protocol(struct rtllib_device *ieee)
2235 if (!ieee->proto_started)
2238 ieee->proto_started = 0;
2239 ieee->proto_stoppping = 1;
2240 ieee->rtllib_ips_leave(ieee->dev);
2242 del_timer_sync(&ieee->associate_timer);
2243 mutex_unlock(&ieee->wx_mutex);
2244 cancel_delayed_work_sync(&ieee->associate_retry_wq);
2245 mutex_lock(&ieee->wx_mutex);
2246 cancel_delayed_work_sync(&ieee->link_change_wq);
2247 rtllib_stop_scan(ieee);
2249 if (ieee->link_state <= RTLLIB_ASSOCIATING_AUTHENTICATED)
2250 ieee->link_state = MAC80211_NOLINK;
2252 if (ieee->link_state == MAC80211_LINKED) {
2253 if (ieee->iw_mode == IW_MODE_INFRA)
2254 SendDisassociation(ieee, 1, WLAN_REASON_DEAUTH_LEAVING);
2255 rtllib_disassociate(ieee);
2259 ieee->proto_stoppping = 0;
2261 kfree(ieee->assocreq_ies);
2262 ieee->assocreq_ies = NULL;
2263 ieee->assocreq_ies_len = 0;
2264 kfree(ieee->assocresp_ies);
2265 ieee->assocresp_ies = NULL;
2266 ieee->assocresp_ies_len = 0;
2269 void rtllib_softmac_start_protocol(struct rtllib_device *ieee)
2271 mutex_lock(&ieee->wx_mutex);
2272 rtllib_start_protocol(ieee);
2273 mutex_unlock(&ieee->wx_mutex);
2275 EXPORT_SYMBOL(rtllib_softmac_start_protocol);
2277 void rtllib_start_protocol(struct rtllib_device *ieee)
2282 rtllib_update_active_chan_map(ieee);
2284 if (ieee->proto_started)
2287 ieee->proto_started = 1;
2289 if (ieee->current_network.channel == 0) {
2292 if (ch > MAX_CHANNEL_NUMBER)
2293 return; /* no channel found */
2294 } while (!ieee->active_channel_map[ch]);
2295 ieee->current_network.channel = ch;
2298 if (ieee->current_network.beacon_interval == 0)
2299 ieee->current_network.beacon_interval = 100;
2301 for (i = 0; i < 17; i++) {
2302 ieee->last_rxseq_num[i] = -1;
2303 ieee->last_rxfrag_num[i] = -1;
2304 ieee->last_packet_time[i] = 0;
2308 /* if the user set the MAC of the ad-hoc cell and then
2309 * switch to managed mode, shall we make sure that association
2310 * attempts does not fail just because the user provide the essid
2311 * and the nic is still checking for the AP MAC ??
2313 switch (ieee->iw_mode) {
2315 rtllib_start_bss(ieee);
2320 int rtllib_softmac_init(struct rtllib_device *ieee)
2324 memset(&ieee->current_network, 0, sizeof(struct rtllib_network));
2326 ieee->link_state = MAC80211_NOLINK;
2327 for (i = 0; i < 5; i++)
2328 ieee->seq_ctrl[i] = 0;
2329 ieee->dot11d_info = kzalloc(sizeof(struct rt_dot11d_info), GFP_ATOMIC);
2330 if (!ieee->dot11d_info)
2333 ieee->link_detect_info.SlotIndex = 0;
2334 ieee->link_detect_info.SlotNum = 2;
2335 ieee->link_detect_info.NumRecvBcnInPeriod = 0;
2336 ieee->link_detect_info.NumRecvDataInPeriod = 0;
2337 ieee->link_detect_info.NumTxOkInPeriod = 0;
2338 ieee->link_detect_info.NumRxOkInPeriod = 0;
2339 ieee->link_detect_info.NumRxUnicastOkInPeriod = 0;
2340 ieee->bIsAggregateFrame = false;
2342 ieee->queue_stop = 0;
2343 ieee->scanning_continue = 0;
2344 ieee->softmac_features = 0;
2347 ieee->proto_started = 0;
2348 ieee->proto_stoppping = 0;
2349 ieee->basic_rate = RTLLIB_DEFAULT_BASIC_RATE;
2351 ieee->ps = RTLLIB_PS_DISABLED;
2352 ieee->sta_sleep = LPS_IS_WAKE;
2354 ieee->reg_dot11ht_oper_rate_set[0] = 0xff;
2355 ieee->reg_dot11ht_oper_rate_set[1] = 0xff;
2356 ieee->reg_dot11ht_oper_rate_set[4] = 0x01;
2358 ieee->reg_dot11tx_ht_oper_rate_set[0] = 0xff;
2359 ieee->reg_dot11tx_ht_oper_rate_set[1] = 0xff;
2360 ieee->reg_dot11tx_ht_oper_rate_set[4] = 0x01;
2362 ieee->FirstIe_InScan = false;
2363 ieee->actscanning = false;
2364 ieee->beinretry = false;
2365 ieee->is_set_key = false;
2366 init_mgmt_queue(ieee);
2368 ieee->tx_pending.txb = NULL;
2370 timer_setup(&ieee->associate_timer, rtllib_associate_abort_cb, 0);
2372 timer_setup(&ieee->beacon_timer, rtllib_send_beacon_cb, 0);
2374 INIT_DELAYED_WORK(&ieee->link_change_wq, (void *)rtllib_link_change_wq);
2375 INIT_WORK(&ieee->associate_complete_wq, (void *)rtllib_associate_complete_wq);
2376 INIT_DELAYED_WORK(&ieee->associate_procedure_wq, (void *)rtllib_associate_procedure_wq);
2377 INIT_DELAYED_WORK(&ieee->softmac_scan_wq, (void *)rtllib_softmac_scan_wq);
2378 INIT_DELAYED_WORK(&ieee->associate_retry_wq, (void *)rtllib_associate_retry_wq);
2379 INIT_WORK(&ieee->wx_sync_scan_wq, (void *)rtllib_wx_sync_scan_wq);
2381 mutex_init(&ieee->wx_mutex);
2382 mutex_init(&ieee->scan_mutex);
2383 mutex_init(&ieee->ips_mutex);
2385 spin_lock_init(&ieee->mgmt_tx_lock);
2386 spin_lock_init(&ieee->beacon_lock);
2388 INIT_WORK(&ieee->ps_task, rtllib_sta_ps);
2393 void rtllib_softmac_free(struct rtllib_device *ieee)
2395 del_timer_sync(&ieee->associate_timer);
2397 cancel_delayed_work_sync(&ieee->associate_retry_wq);
2398 cancel_delayed_work_sync(&ieee->associate_procedure_wq);
2399 cancel_delayed_work_sync(&ieee->softmac_scan_wq);
2400 cancel_delayed_work_sync(&ieee->hw_wakeup_wq);
2401 cancel_delayed_work_sync(&ieee->hw_sleep_wq);
2402 cancel_delayed_work_sync(&ieee->link_change_wq);
2403 cancel_work_sync(&ieee->associate_complete_wq);
2404 cancel_work_sync(&ieee->ips_leave_wq);
2405 cancel_work_sync(&ieee->wx_sync_scan_wq);
2406 cancel_work_sync(&ieee->ps_task);
2408 kfree(ieee->dot11d_info);
2409 ieee->dot11d_info = NULL;
2412 static inline struct sk_buff *
2413 rtllib_disauth_skb(struct rtllib_network *beacon,
2414 struct rtllib_device *ieee, u16 asRsn)
2416 struct sk_buff *skb;
2417 struct rtllib_disauth *disauth;
2418 int len = sizeof(struct rtllib_disauth) + ieee->tx_headroom;
2420 skb = dev_alloc_skb(len);
2424 skb_reserve(skb, ieee->tx_headroom);
2426 disauth = skb_put(skb, sizeof(struct rtllib_disauth));
2427 disauth->header.frame_control = cpu_to_le16(IEEE80211_STYPE_DEAUTH);
2428 disauth->header.duration_id = 0;
2430 ether_addr_copy(disauth->header.addr1, beacon->bssid);
2431 ether_addr_copy(disauth->header.addr2, ieee->dev->dev_addr);
2432 ether_addr_copy(disauth->header.addr3, beacon->bssid);
2434 disauth->reason = cpu_to_le16(asRsn);
2438 static inline struct sk_buff *
2439 rtllib_disassociate_skb(struct rtllib_network *beacon,
2440 struct rtllib_device *ieee, u16 asRsn)
2442 struct sk_buff *skb;
2443 struct rtllib_disassoc *disass;
2444 int len = sizeof(struct rtllib_disassoc) + ieee->tx_headroom;
2446 skb = dev_alloc_skb(len);
2451 skb_reserve(skb, ieee->tx_headroom);
2453 disass = skb_put(skb, sizeof(struct rtllib_disassoc));
2454 disass->header.frame_control = cpu_to_le16(IEEE80211_STYPE_DISASSOC);
2455 disass->header.duration_id = 0;
2457 ether_addr_copy(disass->header.addr1, beacon->bssid);
2458 ether_addr_copy(disass->header.addr2, ieee->dev->dev_addr);
2459 ether_addr_copy(disass->header.addr3, beacon->bssid);
2461 disass->reason = cpu_to_le16(asRsn);
2465 void SendDisassociation(struct rtllib_device *ieee, bool deauth, u16 asRsn)
2467 struct rtllib_network *beacon = &ieee->current_network;
2468 struct sk_buff *skb;
2471 skb = rtllib_disauth_skb(beacon, ieee, asRsn);
2473 skb = rtllib_disassociate_skb(beacon, ieee, asRsn);
2476 softmac_mgmt_xmit(skb, ieee);
2479 u8 rtllib_ap_sec_type(struct rtllib_device *ieee)
2481 static u8 ccmp_ie[4] = {0x00, 0x50, 0xf2, 0x04};
2482 static u8 ccmp_rsn_ie[4] = {0x00, 0x0f, 0xac, 0x04};
2483 int wpa_ie_len = ieee->wpa_ie_len;
2484 struct lib80211_crypt_data *crypt;
2487 crypt = ieee->crypt_info.crypt[ieee->crypt_info.tx_keyidx];
2488 encrypt = (ieee->current_network.capability & WLAN_CAPABILITY_PRIVACY)
2489 || (crypt && crypt->ops && (strcmp(crypt->ops->name, "R-WEP") == 0));
2492 if (encrypt && (wpa_ie_len == 0)) {
2494 } else if ((wpa_ie_len != 0)) {
2495 if (((ieee->wpa_ie[0] == 0xdd) &&
2496 (!memcmp(&(ieee->wpa_ie[14]), ccmp_ie, 4))) ||
2497 ((ieee->wpa_ie[0] == 0x30) &&
2498 (!memcmp(&ieee->wpa_ie[10], ccmp_rsn_ie, 4))))
2499 return SEC_ALG_CCMP;
2501 return SEC_ALG_TKIP;
2503 return SEC_ALG_NONE;
2507 static void rtllib_MlmeDisassociateRequest(struct rtllib_device *rtllib,
2508 u8 *asSta, u8 asRsn)
2513 RemovePeerTS(rtllib, asSta);
2515 if (memcmp(rtllib->current_network.bssid, asSta, 6) == 0) {
2516 rtllib->link_state = MAC80211_NOLINK;
2518 for (i = 0; i < 6; i++)
2519 rtllib->current_network.bssid[i] = 0x22;
2520 OpMode = RT_OP_MODE_NO_LINK;
2521 rtllib->OpMode = RT_OP_MODE_NO_LINK;
2522 rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_MEDIA_STATUS,
2524 rtllib_disassociate(rtllib);
2526 rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_BSSID,
2527 rtllib->current_network.bssid);
2532 rtllib_MgntDisconnectAP(
2533 struct rtllib_device *rtllib,
2537 bool bFilterOutNonAssociatedBSSID = false;
2539 bFilterOutNonAssociatedBSSID = false;
2540 rtllib->SetHwRegHandler(rtllib->dev, HW_VAR_CECHK_BSSID,
2541 (u8 *)(&bFilterOutNonAssociatedBSSID));
2542 rtllib_MlmeDisassociateRequest(rtllib, rtllib->current_network.bssid,
2545 rtllib->link_state = MAC80211_NOLINK;
2548 bool rtllib_MgntDisconnect(struct rtllib_device *rtllib, u8 asRsn)
2550 if (rtllib->ps != RTLLIB_PS_DISABLED)
2551 rtllib->sta_wake_up(rtllib->dev);
2553 if (rtllib->link_state == MAC80211_LINKED) {
2554 if (rtllib->iw_mode == IW_MODE_INFRA)
2555 rtllib_MgntDisconnectAP(rtllib, asRsn);
2560 EXPORT_SYMBOL(rtllib_MgntDisconnect);
2562 void notify_wx_assoc_event(struct rtllib_device *ieee)
2564 union iwreq_data wrqu;
2566 if (ieee->cannot_notify)
2569 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
2570 if (ieee->link_state == MAC80211_LINKED) {
2571 memcpy(wrqu.ap_addr.sa_data, ieee->current_network.bssid,
2574 netdev_info(ieee->dev, "%s(): Tell user space disconnected\n",
2576 eth_zero_addr(wrqu.ap_addr.sa_data);
2578 wireless_send_event(ieee->dev, SIOCGIWAP, &wrqu, NULL);
2580 EXPORT_SYMBOL(notify_wx_assoc_event);