1 /* IEEE 802.11 SoftMAC layer
2 * Copyright (c) 2005 Andrea Merello <andrea.merello@gmail.com>
4 * Mostly extracted from the rtl8180-sa2400 driver for the
5 * in-kernel generic ieee802.11 stack.
7 * Few lines might be stolen from other part of the ieee80211
8 * stack. Copyright who own it's copyright
10 * WPA code stolen from the ipw2200 driver.
11 * Copyright who own it's copyright.
13 * released under the GPL
16 #include "ieee80211.h"
18 #include <linux/random.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/interrupt.h>
22 #include <linux/uaccess.h>
23 #include <linux/etherdevice.h>
27 short ieee80211_is_54g(const struct ieee80211_network *net)
29 return (net->rates_ex_len > 0) || (net->rates_len > 4);
32 short ieee80211_is_shortslot(const struct ieee80211_network *net)
34 return net->capability & WLAN_CAPABILITY_SHORT_SLOT;
37 /* returns the total length needed for placing the RATE MFIE
38 * tag and the EXTENDED RATE MFIE tag if needed.
39 * It encludes two bytes per tag for the tag itself and its len
41 static unsigned int ieee80211_MFIE_rate_len(struct ieee80211_device *ieee)
43 unsigned int rate_len = 0;
45 if (ieee->modulation & IEEE80211_CCK_MODULATION)
46 rate_len = IEEE80211_CCK_RATE_LEN + 2;
48 if (ieee->modulation & IEEE80211_OFDM_MODULATION)
50 rate_len += IEEE80211_OFDM_RATE_LEN + 2;
55 /* place the MFIE rate, tag to the memory (double) poised.
56 * Then it updates the pointer so that it points after the new MFIE tag added.
58 static void ieee80211_MFIE_Brate(struct ieee80211_device *ieee, u8 **tag_p)
62 if (ieee->modulation & IEEE80211_CCK_MODULATION) {
63 *tag++ = MFIE_TYPE_RATES;
65 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
66 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
67 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
68 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
71 /* We may add an option for custom rates that specific HW might support */
75 static void ieee80211_MFIE_Grate(struct ieee80211_device *ieee, u8 **tag_p)
79 if (ieee->modulation & IEEE80211_OFDM_MODULATION) {
80 *tag++ = MFIE_TYPE_RATES_EX;
82 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
83 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
84 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
85 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
86 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
87 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
88 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
89 *tag++ = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
92 /* We may add an option for custom rates that specific HW might support */
96 static void ieee80211_WMM_Info(struct ieee80211_device *ieee, u8 **tag_p)
100 *tag++ = MFIE_TYPE_GENERIC; /* 0 */
105 *tag++ = 0x02; /* 5 */
109 if (ieee->current_network.wmm_info & 0x80)
110 *tag++ = 0x0f|MAX_SP_Len;
119 static void ieee80211_TURBO_Info(struct ieee80211_device *ieee, u8 **tag_p)
122 *tag++ = MFIE_TYPE_GENERIC; /* 0 */
127 *tag++ = 0x01; /* 5 */
132 printk(KERN_ALERT "This is enable turbo mode IE process\n");
135 static void enqueue_mgmt(struct ieee80211_device *ieee, struct sk_buff *skb)
138 nh = (ieee->mgmt_queue_head + 1) % MGMT_QUEUE_NUM;
140 ieee->mgmt_queue_head = nh;
141 ieee->mgmt_queue_ring[nh] = skb;
144 static struct sk_buff *dequeue_mgmt(struct ieee80211_device *ieee)
148 if (ieee->mgmt_queue_tail == ieee->mgmt_queue_head)
151 ret = ieee->mgmt_queue_ring[ieee->mgmt_queue_tail];
153 ieee->mgmt_queue_tail =
154 (ieee->mgmt_queue_tail + 1) % MGMT_QUEUE_NUM;
159 static void init_mgmt_queue(struct ieee80211_device *ieee)
161 ieee->mgmt_queue_tail = ieee->mgmt_queue_head = 0;
164 void ieee80211_sta_wakeup(struct ieee80211_device *ieee, short nl);
166 inline void softmac_mgmt_xmit(struct sk_buff *skb,
167 struct ieee80211_device *ieee)
170 short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
171 struct ieee80211_hdr_3addr *header =
172 (struct ieee80211_hdr_3addr *) skb->data;
174 spin_lock_irqsave(&ieee->lock, flags);
176 /* called with 2nd param 0, no mgmt lock required */
177 ieee80211_sta_wakeup(ieee, 0);
180 if (ieee->queue_stop) {
181 enqueue_mgmt(ieee, skb);
183 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0]<<4);
185 if (ieee->seq_ctrl[0] == 0xFFF)
186 ieee->seq_ctrl[0] = 0;
190 /* avoid watchdog triggers */
191 ieee->dev->trans_start = jiffies;
192 ieee->softmac_data_hard_start_xmit(skb, ieee->dev, ieee->basic_rate);
195 spin_unlock_irqrestore(&ieee->lock, flags);
197 spin_unlock_irqrestore(&ieee->lock, flags);
198 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags);
200 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
202 if (ieee->seq_ctrl[0] == 0xFFF)
203 ieee->seq_ctrl[0] = 0;
207 /* avoid watchdog triggers */
208 ieee->dev->trans_start = jiffies;
209 ieee->softmac_hard_start_xmit(skb, ieee->dev);
211 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags);
215 inline void softmac_ps_mgmt_xmit(struct sk_buff *skb,
216 struct ieee80211_device *ieee)
218 short single = ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE;
219 struct ieee80211_hdr_3addr *header =
220 (struct ieee80211_hdr_3addr *) skb->data;
223 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
225 if (ieee->seq_ctrl[0] == 0xFFF)
226 ieee->seq_ctrl[0] = 0;
230 /* avoid watchdog triggers */
231 ieee->dev->trans_start = jiffies;
232 ieee->softmac_data_hard_start_xmit(skb, ieee->dev, ieee->basic_rate);
234 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
236 if (ieee->seq_ctrl[0] == 0xFFF)
237 ieee->seq_ctrl[0] = 0;
241 /* avoid watchdog triggers */
242 ieee->dev->trans_start = jiffies;
243 ieee->softmac_hard_start_xmit(skb, ieee->dev);
247 inline struct sk_buff *
248 ieee80211_disassociate_skb(struct ieee80211_network *beacon,
249 struct ieee80211_device *ieee, u8 asRsn)
252 struct ieee80211_disassoc_frame *disass;
254 skb = dev_alloc_skb(sizeof(struct ieee80211_disassoc_frame));
258 disass = (struct ieee80211_disassoc_frame *) skb_put(skb, sizeof(struct ieee80211_disassoc_frame));
259 disass->header.frame_control = cpu_to_le16(IEEE80211_STYPE_DISASSOC);
260 disass->header.duration_id = 0;
262 memcpy(disass->header.addr1, beacon->bssid, ETH_ALEN);
263 memcpy(disass->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
264 memcpy(disass->header.addr3, beacon->bssid, ETH_ALEN);
266 disass->reasoncode = asRsn;
270 void SendDisassociation(struct ieee80211_device *ieee, u8 *asSta, u8 asRsn)
272 struct ieee80211_network *beacon = &ieee->current_network;
274 skb = ieee80211_disassociate_skb(beacon, ieee, asRsn);
276 softmac_mgmt_xmit(skb, ieee);
279 inline struct sk_buff *ieee80211_probe_req(struct ieee80211_device *ieee)
281 unsigned int len, rate_len;
284 struct ieee80211_probe_request *req;
286 len = ieee->current_network.ssid_len;
288 rate_len = ieee80211_MFIE_rate_len(ieee);
290 skb = dev_alloc_skb(sizeof(struct ieee80211_probe_request) +
295 req = (struct ieee80211_probe_request *) skb_put(skb, sizeof(struct ieee80211_probe_request));
296 req->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
297 req->header.duration_id = 0; /* FIXME: is this OK ? */
299 memset(req->header.addr1, 0xff, ETH_ALEN);
300 memcpy(req->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
301 memset(req->header.addr3, 0xff, ETH_ALEN);
303 tag = (u8 *) skb_put(skb, len + 2 + rate_len);
305 *tag++ = MFIE_TYPE_SSID;
307 memcpy(tag, ieee->current_network.ssid, len);
309 ieee80211_MFIE_Brate(ieee, &tag);
310 ieee80211_MFIE_Grate(ieee, &tag);
315 struct sk_buff *ieee80211_get_beacon_(struct ieee80211_device *ieee);
317 static void ieee80211_send_beacon(struct ieee80211_device *ieee)
321 skb = ieee80211_get_beacon_(ieee);
324 softmac_mgmt_xmit(skb, ieee);
325 ieee->softmac_stats.tx_beacons++;
326 dev_kfree_skb_any(skb);
329 ieee->beacon_timer.expires = jiffies +
330 (MSECS(ieee->current_network.beacon_interval - 5));
332 if (ieee->beacon_txing)
333 add_timer(&ieee->beacon_timer);
337 static void ieee80211_send_beacon_cb(unsigned long _ieee)
339 struct ieee80211_device *ieee =
340 (struct ieee80211_device *) _ieee;
343 spin_lock_irqsave(&ieee->beacon_lock, flags);
344 ieee80211_send_beacon(ieee);
345 spin_unlock_irqrestore(&ieee->beacon_lock, flags);
348 static void ieee80211_send_probe(struct ieee80211_device *ieee)
352 skb = ieee80211_probe_req(ieee);
354 softmac_mgmt_xmit(skb, ieee);
355 ieee->softmac_stats.tx_probe_rq++;
359 static void ieee80211_send_probe_requests(struct ieee80211_device *ieee)
361 if (ieee->active_scan && (ieee->softmac_features & IEEE_SOFTMAC_PROBERQ)) {
362 ieee80211_send_probe(ieee);
363 ieee80211_send_probe(ieee);
367 /* this performs syncro scan blocking the caller until all channels
368 * in the allowed channel map has been checked.
370 static void ieee80211_softmac_scan_syncro(struct ieee80211_device *ieee)
373 u8 channel_map[MAX_CHANNEL_NUMBER+1];
374 memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
375 down(&ieee->scan_sem);
380 if (ch > MAX_CHANNEL_NUMBER)
381 goto out; /* scan completed */
383 } while (!channel_map[ch]);
384 /* this function can be called in two situations
385 * 1- We have switched to ad-hoc mode and we are
386 * performing a complete syncro scan before conclude
387 * there are no interesting cell and to create a
388 * new one. In this case the link state is
389 * IEEE80211_NOLINK until we found an interesting cell.
390 * If so the ieee8021_new_net, called by the RX path
391 * will set the state to IEEE80211_LINKED, so we stop
393 * 2- We are linked and the root uses run iwlist scan.
394 * So we switch to IEEE80211_LINKED_SCANNING to remember
395 * that we are still logically linked (not interested in
396 * new network events, despite for updating the net list,
397 * but we are temporarily 'unlinked' as the driver shall
398 * not filter RX frames and the channel is changing.
399 * So the only situation in witch are interested is to check
400 * if the state become LINKED because of the #1 situation
403 if (ieee->state == IEEE80211_LINKED)
406 ieee->set_chan(ieee->dev, ch);
407 if (channel_map[ch] == 1)
408 ieee80211_send_probe_requests(ieee);
410 /* this prevent excessive time wait when we
411 * need to wait for a syncro scan to end..
413 if (ieee->sync_scan_hurryup)
416 msleep_interruptible_rtl(IEEE80211_SOFTMAC_SCAN_TIME);
419 ieee->sync_scan_hurryup = 0;
421 if (IS_DOT11D_ENABLE(ieee))
422 DOT11D_ScanComplete(ieee);
425 void ieee80211_softmac_ips_scan_syncro(struct ieee80211_device *ieee)
428 unsigned int watch_dog = 0;
429 u8 channel_map[MAX_CHANNEL_NUMBER+1];
430 memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
431 down(&ieee->scan_sem);
432 ch = ieee->current_network.channel;
435 /* this function can be called in two situations
436 * 1- We have switched to ad-hoc mode and we are
437 * performing a complete syncro scan before conclude
438 * there are no interesting cell and to create a
439 * new one. In this case the link state is
440 * IEEE80211_NOLINK until we found an interesting cell.
441 * If so the ieee8021_new_net, called by the RX path
442 * will set the state to IEEE80211_LINKED, so we stop
444 * 2- We are linked and the root uses run iwlist scan.
445 * So we switch to IEEE80211_LINKED_SCANNING to remember
446 * that we are still logically linked (not interested in
447 * new network events, despite for updating the net list,
448 * but we are temporarily 'unlinked' as the driver shall
449 * not filter RX frames and the channel is changing.
450 * So the only situation in witch are interested is to check
451 * if the state become LINKED because of the #1 situation
453 if (ieee->state == IEEE80211_LINKED)
456 if (channel_map[ieee->current_network.channel] > 0)
457 ieee->set_chan(ieee->dev, ieee->current_network.channel);
459 if (channel_map[ieee->current_network.channel] == 1)
460 ieee80211_send_probe_requests(ieee);
462 msleep_interruptible_rtl(IEEE80211_SOFTMAC_SCAN_TIME);
465 if (watch_dog++ >= MAX_CHANNEL_NUMBER)
466 goto out; /* scan completed */
468 ieee->current_network.channel = (ieee->current_network.channel + 1)%MAX_CHANNEL_NUMBER;
469 } while (!channel_map[ieee->current_network.channel]);
472 ieee->actscanning = false;
474 if (IS_DOT11D_ENABLE(ieee))
475 DOT11D_ScanComplete(ieee);
478 static void ieee80211_softmac_scan_wq(struct work_struct *work)
480 struct delayed_work *dwork = to_delayed_work(work);
481 struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, softmac_scan_wq);
482 static short watchdog;
483 u8 channel_map[MAX_CHANNEL_NUMBER+1];
484 memcpy(channel_map, GET_DOT11D_INFO(ieee)->channel_map, MAX_CHANNEL_NUMBER+1);
485 down(&ieee->scan_sem);
488 ieee->current_network.channel =
489 (ieee->current_network.channel + 1) % MAX_CHANNEL_NUMBER;
490 if (watchdog++ > MAX_CHANNEL_NUMBER)
491 goto out; /* no good chans */
492 } while (!channel_map[ieee->current_network.channel]);
494 if (ieee->scanning == 0) {
495 printk("error out, scanning = 0\n");
498 ieee->set_chan(ieee->dev, ieee->current_network.channel);
499 if (channel_map[ieee->current_network.channel] == 1)
500 ieee80211_send_probe_requests(ieee);
502 queue_delayed_work(ieee->wq, &ieee->softmac_scan_wq, IEEE80211_SOFTMAC_SCAN_TIME);
506 ieee->actscanning = false;
511 if (IS_DOT11D_ENABLE(ieee))
512 DOT11D_ScanComplete(ieee);
516 static void ieee80211_beacons_start(struct ieee80211_device *ieee)
520 spin_lock_irqsave(&ieee->beacon_lock, flags);
522 ieee->beacon_txing = 1;
523 ieee80211_send_beacon(ieee);
525 spin_unlock_irqrestore(&ieee->beacon_lock, flags);
528 static void ieee80211_beacons_stop(struct ieee80211_device *ieee)
532 spin_lock_irqsave(&ieee->beacon_lock, flags);
534 ieee->beacon_txing = 0;
535 del_timer_sync(&ieee->beacon_timer);
537 spin_unlock_irqrestore(&ieee->beacon_lock, flags);
540 void ieee80211_stop_send_beacons(struct ieee80211_device *ieee)
542 if (ieee->stop_send_beacons)
543 ieee->stop_send_beacons(ieee->dev);
544 if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
545 ieee80211_beacons_stop(ieee);
548 void ieee80211_start_send_beacons(struct ieee80211_device *ieee)
550 if (ieee->start_send_beacons)
551 ieee->start_send_beacons(ieee->dev);
552 if (ieee->softmac_features & IEEE_SOFTMAC_BEACONS)
553 ieee80211_beacons_start(ieee);
556 static void ieee80211_softmac_stop_scan(struct ieee80211_device *ieee)
558 down(&ieee->scan_sem);
560 if (ieee->scanning == 1) {
562 cancel_delayed_work(&ieee->softmac_scan_wq);
568 void ieee80211_stop_scan(struct ieee80211_device *ieee)
570 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
571 ieee80211_softmac_stop_scan(ieee);
573 ieee->stop_scan(ieee->dev);
576 /* called with ieee->lock held */
577 void ieee80211_rtl_start_scan(struct ieee80211_device *ieee)
579 if (IS_DOT11D_ENABLE(ieee)) {
580 if (IS_COUNTRY_IE_VALID(ieee))
581 RESET_CIE_WATCHDOG(ieee);
584 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN) {
585 if (ieee->scanning == 0) {
588 queue_delayed_work(ieee->wq, &ieee->softmac_scan_wq, 0);
592 ieee->start_scan(ieee->dev);
595 /* called with wx_sem held */
596 void ieee80211_start_scan_syncro(struct ieee80211_device *ieee)
598 if (IS_DOT11D_ENABLE(ieee)) {
599 if (IS_COUNTRY_IE_VALID(ieee))
600 RESET_CIE_WATCHDOG(ieee);
602 ieee->sync_scan_hurryup = 0;
604 if (ieee->softmac_features & IEEE_SOFTMAC_SCAN)
605 ieee80211_softmac_scan_syncro(ieee);
607 ieee->scan_syncro(ieee->dev);
610 inline struct sk_buff *
611 ieee80211_authentication_req(struct ieee80211_network *beacon,
612 struct ieee80211_device *ieee, int challengelen)
615 struct ieee80211_authentication *auth;
617 skb = dev_alloc_skb(sizeof(struct ieee80211_authentication) + challengelen);
622 auth = (struct ieee80211_authentication *)
623 skb_put(skb, sizeof(struct ieee80211_authentication));
625 auth->header.frame_ctl = IEEE80211_STYPE_AUTH;
627 auth->header.frame_ctl |= IEEE80211_FCTL_WEP;
629 auth->header.duration_id = 0x013a; /* FIXME */
631 memcpy(auth->header.addr1, beacon->bssid, ETH_ALEN);
632 memcpy(auth->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
633 memcpy(auth->header.addr3, beacon->bssid, ETH_ALEN);
635 auth->algorithm = ieee->open_wep ? WLAN_AUTH_OPEN : WLAN_AUTH_SHARED_KEY;
637 auth->transaction = cpu_to_le16(ieee->associate_seq);
638 ieee->associate_seq++;
640 auth->status = cpu_to_le16(WLAN_STATUS_SUCCESS);
645 static struct sk_buff *ieee80211_probe_resp(struct ieee80211_device *ieee,
650 struct ieee80211_probe_response *beacon_buf;
653 int atim_len, erp_len;
654 struct ieee80211_crypt_data *crypt;
656 char *ssid = ieee->current_network.ssid;
657 int ssid_len = ieee->current_network.ssid_len;
658 int rate_len = ieee->current_network.rates_len+2;
659 int rate_ex_len = ieee->current_network.rates_ex_len;
660 int wpa_ie_len = ieee->wpa_ie_len;
664 if (ieee->current_network.capability & WLAN_CAPABILITY_IBSS)
669 if (ieee80211_is_54g(&ieee->current_network))
674 beacon_size = sizeof(struct ieee80211_probe_response)+
683 skb = dev_alloc_skb(beacon_size);
688 beacon_buf = (struct ieee80211_probe_response *) skb_put(skb, beacon_size);
690 memcpy(beacon_buf->header.addr1, dest, ETH_ALEN);
691 memcpy(beacon_buf->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
692 memcpy(beacon_buf->header.addr3, ieee->current_network.bssid, ETH_ALEN);
694 beacon_buf->header.duration_id = 0; /* FIXME */
695 beacon_buf->beacon_interval =
696 cpu_to_le16(ieee->current_network.beacon_interval);
697 beacon_buf->capability =
698 cpu_to_le16(ieee->current_network.capability & WLAN_CAPABILITY_IBSS);
700 if (ieee->short_slot && (ieee->current_network.capability & WLAN_CAPABILITY_SHORT_SLOT))
701 beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
703 crypt = ieee->crypt[ieee->tx_keyidx];
705 encrypt = ieee->host_encrypt && crypt && crypt->ops &&
706 ((0 == strcmp(crypt->ops->name, "WEP")) || wpa_ie_len);
709 beacon_buf->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
712 beacon_buf->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_RESP);
714 beacon_buf->info_element.id = MFIE_TYPE_SSID;
715 beacon_buf->info_element.len = ssid_len;
717 tag = (u8 *) beacon_buf->info_element.data;
719 memcpy(tag, ssid, ssid_len);
723 *(tag++) = MFIE_TYPE_RATES;
724 *(tag++) = rate_len - 2;
725 memcpy(tag, ieee->current_network.rates, rate_len-2);
728 *(tag++) = MFIE_TYPE_DS_SET;
730 *(tag++) = ieee->current_network.channel;
733 *(tag++) = MFIE_TYPE_IBSS_SET;
735 *((u16 *)(tag)) = cpu_to_le16(ieee->current_network.atim_window);
740 *(tag++) = MFIE_TYPE_ERP;
746 *(tag++) = MFIE_TYPE_RATES_EX;
747 *(tag++) = rate_ex_len-2;
748 memcpy(tag, ieee->current_network.rates_ex, rate_ex_len-2);
749 tag += rate_ex_len - 2;
753 if (ieee->iw_mode == IW_MODE_ADHOC) {
754 /* as Windows will set pairwise key same as the group
755 * key which is not allowed in Linux, so set this for
758 memcpy(&ieee->wpa_ie[14], &ieee->wpa_ie[8], 4);
761 memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
763 skb->dev = ieee->dev;
767 static struct sk_buff *ieee80211_assoc_resp(struct ieee80211_device *ieee,
773 struct ieee80211_crypt_data *crypt;
774 struct ieee80211_assoc_response_frame *assoc;
777 unsigned int rate_len = ieee80211_MFIE_rate_len(ieee);
778 int len = sizeof(struct ieee80211_assoc_response_frame) + rate_len;
780 skb = dev_alloc_skb(len);
785 assoc = (struct ieee80211_assoc_response_frame *)
786 skb_put(skb, sizeof(struct ieee80211_assoc_response_frame));
788 assoc->header.frame_control = cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP);
789 memcpy(assoc->header.addr1, dest, ETH_ALEN);
790 memcpy(assoc->header.addr3, ieee->dev->dev_addr, ETH_ALEN);
791 memcpy(assoc->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
792 assoc->capability = cpu_to_le16(ieee->iw_mode == IW_MODE_MASTER ?
793 WLAN_CAPABILITY_BSS : WLAN_CAPABILITY_IBSS);
795 if (ieee->short_slot)
796 assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
798 if (ieee->host_encrypt)
799 crypt = ieee->crypt[ieee->tx_keyidx];
803 encrypt = (crypt && crypt->ops);
806 assoc->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
809 assoc->aid = cpu_to_le16(ieee->assoc_id);
810 if (ieee->assoc_id == 0x2007)
815 tag = (u8 *) skb_put(skb, rate_len);
817 ieee80211_MFIE_Brate(ieee, &tag);
818 ieee80211_MFIE_Grate(ieee, &tag);
823 static struct sk_buff *ieee80211_auth_resp(struct ieee80211_device *ieee,
824 int status, u8 *dest)
827 struct ieee80211_authentication *auth;
829 skb = dev_alloc_skb(sizeof(struct ieee80211_authentication)+1);
834 skb->len = sizeof(struct ieee80211_authentication);
836 auth = (struct ieee80211_authentication *)skb->data;
838 auth->status = cpu_to_le16(status);
839 auth->transaction = cpu_to_le16(2);
840 auth->algorithm = cpu_to_le16(WLAN_AUTH_OPEN);
842 memcpy(auth->header.addr3, ieee->dev->dev_addr, ETH_ALEN);
843 memcpy(auth->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
844 memcpy(auth->header.addr1, dest, ETH_ALEN);
845 auth->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_AUTH);
849 static struct sk_buff *ieee80211_null_func(struct ieee80211_device *ieee, short pwr)
852 struct ieee80211_hdr_3addr *hdr;
854 skb = dev_alloc_skb(sizeof(struct ieee80211_hdr_3addr));
859 hdr = (struct ieee80211_hdr_3addr *)skb_put(skb, sizeof(struct ieee80211_hdr_3addr));
861 memcpy(hdr->addr1, ieee->current_network.bssid, ETH_ALEN);
862 memcpy(hdr->addr2, ieee->dev->dev_addr, ETH_ALEN);
863 memcpy(hdr->addr3, ieee->current_network.bssid, ETH_ALEN);
865 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
866 IEEE80211_STYPE_NULLFUNC | IEEE80211_FCTL_TODS |
867 (pwr ? IEEE80211_FCTL_PM:0));
872 static void ieee80211_resp_to_assoc_rq(struct ieee80211_device *ieee, u8 *dest)
874 struct sk_buff *buf = ieee80211_assoc_resp(ieee, dest);
877 softmac_mgmt_xmit(buf, ieee);
878 dev_kfree_skb_any(buf);
882 static void ieee80211_resp_to_auth(struct ieee80211_device *ieee, int s, u8 *dest)
884 struct sk_buff *buf = ieee80211_auth_resp(ieee, s, dest);
887 softmac_mgmt_xmit(buf, ieee);
888 dev_kfree_skb_any(buf);
892 static void ieee80211_resp_to_probe(struct ieee80211_device *ieee, u8 *dest)
894 struct sk_buff *buf = ieee80211_probe_resp(ieee, dest);
897 softmac_mgmt_xmit(buf, ieee);
898 dev_kfree_skb_any(buf);
902 inline struct sk_buff *
903 ieee80211_association_req(struct ieee80211_network *beacon,
904 struct ieee80211_device *ieee)
908 struct ieee80211_assoc_request_frame *hdr;
910 unsigned int wpa_len = beacon->wpa_ie_len;
912 /* for testing purpose */
913 unsigned int rsn_len = beacon->rsn_ie_len;
915 unsigned int rate_len = ieee80211_MFIE_rate_len(ieee);
916 unsigned int wmm_info_len = beacon->QoS_Enable?9:0;
917 unsigned int turbo_info_len = beacon->Turbo_Enable?9:0;
919 u8 encry_proto = ieee->wpax_type_notify & 0xff;
923 /* [0] Notify type of encryption: WPA/WPA2
927 if (ieee->wpax_type_set) {
928 if (IEEE_PROTO_WPA == encry_proto) {
930 } else if (IEEE_PROTO_RSN == encry_proto) {
934 len = sizeof(struct ieee80211_assoc_request_frame)+
935 + beacon->ssid_len /* essid tagged val */
936 + rate_len /* rates tagged val */
942 skb = dev_alloc_skb(len);
947 hdr = (struct ieee80211_assoc_request_frame *)
948 skb_put(skb, sizeof(struct ieee80211_assoc_request_frame));
950 hdr->header.frame_control = IEEE80211_STYPE_ASSOC_REQ;
951 hdr->header.duration_id = 37; /* FIXME */
952 memcpy(hdr->header.addr1, beacon->bssid, ETH_ALEN);
953 memcpy(hdr->header.addr2, ieee->dev->dev_addr, ETH_ALEN);
954 memcpy(hdr->header.addr3, beacon->bssid, ETH_ALEN);
955 memcpy(ieee->ap_mac_addr, beacon->bssid, ETH_ALEN); /* for HW security */
957 hdr->capability = cpu_to_le16(WLAN_CAPABILITY_BSS);
958 if (beacon->capability & WLAN_CAPABILITY_PRIVACY)
959 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_PRIVACY);
960 if (beacon->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
961 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE);
963 if (ieee->short_slot)
964 hdr->capability |= cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT);
966 hdr->listen_interval = 0xa; /* FIXME */
968 hdr->info_element.id = MFIE_TYPE_SSID;
970 hdr->info_element.len = beacon->ssid_len;
971 tag = skb_put(skb, beacon->ssid_len);
972 memcpy(tag, beacon->ssid, beacon->ssid_len);
974 tag = skb_put(skb, rate_len);
976 ieee80211_MFIE_Brate(ieee, &tag);
977 ieee80211_MFIE_Grate(ieee, &tag);
979 /* add rsn==0 condition for ap's mix security mode(wpa+wpa2)
980 * choose AES encryption as default algorithm while using mixed mode.
983 tag = skb_put(skb, ieee->wpa_ie_len);
984 memcpy(tag, ieee->wpa_ie, ieee->wpa_ie_len);
986 tag = skb_put(skb, wmm_info_len);
988 ieee80211_WMM_Info(ieee, &tag);
990 tag = skb_put(skb, turbo_info_len);
992 ieee80211_TURBO_Info(ieee, &tag);
997 void ieee80211_associate_abort(struct ieee80211_device *ieee)
1000 spin_lock_irqsave(&ieee->lock, flags);
1002 ieee->associate_seq++;
1004 /* don't scan, and avoid to have the RX path possibly
1005 * try again to associate. Even do not react to AUTH or
1006 * ASSOC response. Just wait for the retry wq to be scheduled.
1007 * Here we will check if there are good nets to associate
1008 * with, so we retry or just get back to NO_LINK and scanning
1010 if (ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATING) {
1011 IEEE80211_DEBUG_MGMT("Authentication failed\n");
1012 ieee->softmac_stats.no_auth_rs++;
1014 IEEE80211_DEBUG_MGMT("Association failed\n");
1015 ieee->softmac_stats.no_ass_rs++;
1018 ieee->state = IEEE80211_ASSOCIATING_RETRY;
1020 queue_delayed_work(ieee->wq, &ieee->associate_retry_wq, IEEE80211_SOFTMAC_ASSOC_RETRY_TIME);
1022 spin_unlock_irqrestore(&ieee->lock, flags);
1025 static void ieee80211_associate_abort_cb(unsigned long dev)
1027 ieee80211_associate_abort((struct ieee80211_device *) dev);
1030 static void ieee80211_associate_step1(struct ieee80211_device *ieee)
1032 struct ieee80211_network *beacon = &ieee->current_network;
1033 struct sk_buff *skb;
1035 IEEE80211_DEBUG_MGMT("Stopping scan\n");
1036 ieee->softmac_stats.tx_auth_rq++;
1037 skb = ieee80211_authentication_req(beacon, ieee, 0);
1039 ieee80211_associate_abort(ieee);
1041 ieee->state = IEEE80211_ASSOCIATING_AUTHENTICATING;
1042 IEEE80211_DEBUG_MGMT("Sending authentication request\n");
1043 softmac_mgmt_xmit(skb, ieee);
1044 /* BUGON when you try to add_timer twice, using mod_timer may
1047 if (!timer_pending(&ieee->associate_timer)) {
1048 ieee->associate_timer.expires = jiffies + (HZ / 2);
1049 add_timer(&ieee->associate_timer);
1051 /* If call dev_kfree_skb_any,a warning will ocur....
1052 * KERNEL: assertion (!atomic_read(&skb->users)) failed at
1053 * net/core/dev.c (1708)
1058 static void ieee80211_rtl_auth_challenge(struct ieee80211_device *ieee, u8 *challenge,
1062 struct sk_buff *skb;
1063 struct ieee80211_network *beacon = &ieee->current_network;
1064 del_timer_sync(&ieee->associate_timer);
1065 ieee->associate_seq++;
1066 ieee->softmac_stats.tx_auth_rq++;
1068 skb = ieee80211_authentication_req(beacon, ieee, chlen+2);
1070 ieee80211_associate_abort(ieee);
1072 c = skb_put(skb, chlen+2);
1073 *(c++) = MFIE_TYPE_CHALLENGE;
1075 memcpy(c, challenge, chlen);
1077 IEEE80211_DEBUG_MGMT("Sending authentication challenge response\n");
1079 ieee80211_encrypt_fragment(ieee, skb, sizeof(struct ieee80211_hdr_3addr));
1081 softmac_mgmt_xmit(skb, ieee);
1082 if (!timer_pending(&ieee->associate_timer)) {
1083 ieee->associate_timer.expires = jiffies + (HZ / 2);
1084 add_timer(&ieee->associate_timer);
1086 dev_kfree_skb_any(skb);
1091 static void ieee80211_associate_step2(struct ieee80211_device *ieee)
1093 struct sk_buff *skb;
1094 struct ieee80211_network *beacon = &ieee->current_network;
1096 del_timer_sync(&ieee->associate_timer);
1098 IEEE80211_DEBUG_MGMT("Sending association request\n");
1099 ieee->softmac_stats.tx_ass_rq++;
1100 skb = ieee80211_association_req(beacon, ieee);
1102 ieee80211_associate_abort(ieee);
1104 softmac_mgmt_xmit(skb, ieee);
1105 if (!timer_pending(&ieee->associate_timer)) {
1106 ieee->associate_timer.expires = jiffies + (HZ / 2);
1107 add_timer(&ieee->associate_timer);
1112 static void ieee80211_associate_complete_wq(struct work_struct *work)
1114 struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, associate_complete_wq);
1116 printk(KERN_INFO "Associated successfully\n");
1117 if (ieee80211_is_54g(&ieee->current_network) &&
1118 (ieee->modulation & IEEE80211_OFDM_MODULATION)) {
1120 printk(KERN_INFO"Using G rates\n");
1123 printk(KERN_INFO"Using B rates\n");
1125 ieee->link_change(ieee->dev);
1126 notify_wx_assoc_event(ieee);
1127 if (ieee->data_hard_resume)
1128 ieee->data_hard_resume(ieee->dev);
1129 netif_carrier_on(ieee->dev);
1132 static void ieee80211_associate_complete(struct ieee80211_device *ieee)
1134 del_timer_sync(&ieee->associate_timer);
1136 ieee->state = IEEE80211_LINKED;
1137 IEEE80211_DEBUG_MGMT("Successfully associated\n");
1139 queue_work(ieee->wq, &ieee->associate_complete_wq);
1142 static void ieee80211_associate_procedure_wq(struct work_struct *work)
1144 struct ieee80211_device *ieee = container_of(work, struct ieee80211_device, associate_procedure_wq);
1146 ieee->sync_scan_hurryup = 1;
1147 down(&ieee->wx_sem);
1149 if (ieee->data_hard_stop)
1150 ieee->data_hard_stop(ieee->dev);
1152 ieee80211_stop_scan(ieee);
1153 ieee->set_chan(ieee->dev, ieee->current_network.channel);
1155 ieee->associate_seq = 1;
1156 ieee80211_associate_step1(ieee);
1161 inline void ieee80211_softmac_new_net(struct ieee80211_device *ieee,
1162 struct ieee80211_network *net)
1164 u8 tmp_ssid[IW_ESSID_MAX_SIZE+1];
1165 int tmp_ssid_len = 0;
1167 short apset, ssidset, ssidbroad, apmatch, ssidmatch;
1169 /* we are interested in new new only if we are not associated
1170 * and we are not associating / authenticating
1172 if (ieee->state != IEEE80211_NOLINK)
1175 if ((ieee->iw_mode == IW_MODE_INFRA) && !(net->capability & WLAN_CAPABILITY_BSS))
1178 if ((ieee->iw_mode == IW_MODE_ADHOC) && !(net->capability & WLAN_CAPABILITY_IBSS))
1181 if (ieee->iw_mode == IW_MODE_INFRA || ieee->iw_mode == IW_MODE_ADHOC) {
1182 /* if the user specified the AP MAC, we need also the essid
1183 * This could be obtained by beacons or, if the network does not
1184 * broadcast it, it can be put manually.
1186 apset = ieee->wap_set;
1187 ssidset = ieee->ssid_set;
1188 ssidbroad = !(net->ssid_len == 0 || net->ssid[0] == '\0');
1189 apmatch = (memcmp(ieee->current_network.bssid, net->bssid, ETH_ALEN) == 0);
1191 if (ieee->current_network.ssid_len != net->ssid_len)
1194 ssidmatch = (0 == strncmp(ieee->current_network.ssid, net->ssid, net->ssid_len));
1196 /* if the user set the AP check if match.
1197 * if the network does not broadcast essid we check the user
1198 * supplied ANY essid
1199 * if the network does broadcast and the user does not set essid
1201 * if the network does broadcast and the user did set essid
1202 * chech if essid match
1203 * (apset && apmatch && ((ssidset && ssidbroad && ssidmatch) ||
1204 * (ssidbroad && !ssidset) || (!ssidbroad && ssidset))) ||
1205 * if the ap is not set, check that the user set the bssid and
1206 * the network does broadcast and that those two bssid matches
1207 * (!apset && ssidset && ssidbroad && ssidmatch)
1209 if ((apset && apmatch && ((ssidset && ssidbroad && ssidmatch) ||
1210 (ssidbroad && !ssidset) || (!ssidbroad && ssidset))) ||
1211 (!apset && ssidset && ssidbroad && ssidmatch)) {
1212 /* if the essid is hidden replace it with the
1213 * essid provided by the user.
1216 strncpy(tmp_ssid, ieee->current_network.ssid, IW_ESSID_MAX_SIZE);
1217 tmp_ssid_len = ieee->current_network.ssid_len;
1219 memcpy(&ieee->current_network, net, sizeof(struct ieee80211_network));
1222 strncpy(ieee->current_network.ssid, tmp_ssid, IW_ESSID_MAX_SIZE);
1223 ieee->current_network.ssid_len = tmp_ssid_len;
1225 printk(KERN_INFO"Linking with %s: channel is %d\n", ieee->current_network.ssid, ieee->current_network.channel);
1227 if (ieee->iw_mode == IW_MODE_INFRA) {
1228 ieee->state = IEEE80211_ASSOCIATING;
1229 ieee->beinretry = false;
1230 queue_work(ieee->wq, &ieee->associate_procedure_wq);
1232 if (ieee80211_is_54g(&ieee->current_network) &&
1233 (ieee->modulation & IEEE80211_OFDM_MODULATION)) {
1235 printk(KERN_INFO"Using G rates\n");
1238 printk(KERN_INFO"Using B rates\n");
1240 ieee->state = IEEE80211_LINKED;
1241 ieee->beinretry = false;
1247 void ieee80211_softmac_check_all_nets(struct ieee80211_device *ieee)
1249 unsigned long flags;
1250 struct ieee80211_network *target;
1252 spin_lock_irqsave(&ieee->lock, flags);
1253 list_for_each_entry(target, &ieee->network_list, list) {
1254 /* if the state become different that NOLINK means
1255 * we had found what we are searching for
1257 if (ieee->state != IEEE80211_NOLINK)
1260 if (ieee->scan_age == 0 || time_after(target->last_scanned + ieee->scan_age, jiffies))
1261 ieee80211_softmac_new_net(ieee, target);
1263 spin_unlock_irqrestore(&ieee->lock, flags);
1266 static inline u16 auth_parse(struct sk_buff *skb, u8 **challenge, int *chlen)
1268 struct ieee80211_authentication *a;
1270 if (skb->len < (sizeof(struct ieee80211_authentication) - sizeof(struct ieee80211_info_element))) {
1271 IEEE80211_DEBUG_MGMT("invalid len in auth resp: %d\n", skb->len);
1275 a = (struct ieee80211_authentication *) skb->data;
1276 if (skb->len > (sizeof(struct ieee80211_authentication) + 3)) {
1277 t = skb->data + sizeof(struct ieee80211_authentication);
1279 if (*(t++) == MFIE_TYPE_CHALLENGE) {
1281 *challenge = kmemdup(t, *chlen, GFP_ATOMIC);
1286 return cpu_to_le16(a->status);
1289 static int auth_rq_parse(struct sk_buff *skb, u8 *dest)
1291 struct ieee80211_authentication *a;
1293 if (skb->len < (sizeof(struct ieee80211_authentication) - sizeof(struct ieee80211_info_element))) {
1294 IEEE80211_DEBUG_MGMT("invalid len in auth request: %d\n", skb->len);
1297 a = (struct ieee80211_authentication *) skb->data;
1299 memcpy(dest, a->header.addr2, ETH_ALEN);
1301 if (le16_to_cpu(a->algorithm) != WLAN_AUTH_OPEN)
1302 return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1304 return WLAN_STATUS_SUCCESS;
1307 static short probe_rq_parse(struct ieee80211_device *ieee, struct sk_buff *skb,
1315 struct ieee80211_hdr_3addr *header =
1316 (struct ieee80211_hdr_3addr *) skb->data;
1318 if (skb->len < sizeof(struct ieee80211_hdr_3addr))
1319 return -1; /* corrupted */
1321 memcpy(src, header->addr2, ETH_ALEN);
1323 skbend = (u8 *)skb->data + skb->len;
1325 tag = skb->data + sizeof(struct ieee80211_hdr_3addr);
1327 while (tag+1 < skbend) {
1333 tag++; /* point to the len field */
1334 tag = tag + *(tag); /* point to the last data byte of the tag */
1335 tag++; /* point to the next tag */
1342 return 1; /* ssid not found in tagged param */
1344 return (!strncmp(ssid, ieee->current_network.ssid, ssidlen));
1348 static int assoc_rq_parse(struct sk_buff *skb, u8 *dest)
1350 struct ieee80211_assoc_request_frame *a;
1352 if (skb->len < (sizeof(struct ieee80211_assoc_request_frame) -
1353 sizeof(struct ieee80211_info_element))) {
1355 IEEE80211_DEBUG_MGMT("invalid len in auth request:%d\n", skb->len);
1359 a = (struct ieee80211_assoc_request_frame *) skb->data;
1361 memcpy(dest, a->header.addr2, ETH_ALEN);
1366 static inline u16 assoc_parse(struct sk_buff *skb, int *aid)
1368 struct ieee80211_assoc_response_frame *a;
1369 if (skb->len < sizeof(struct ieee80211_assoc_response_frame)) {
1370 IEEE80211_DEBUG_MGMT("invalid len in auth resp: %d\n", skb->len);
1374 a = (struct ieee80211_assoc_response_frame *) skb->data;
1375 *aid = le16_to_cpu(a->aid) & 0x3fff;
1376 return le16_to_cpu(a->status);
1379 static inline void ieee80211_rx_probe_rq(struct ieee80211_device *ieee,
1380 struct sk_buff *skb)
1384 ieee->softmac_stats.rx_probe_rq++;
1385 if (probe_rq_parse(ieee, skb, dest)) {
1386 ieee->softmac_stats.tx_probe_rs++;
1387 ieee80211_resp_to_probe(ieee, dest);
1391 inline void ieee80211_rx_auth_rq(struct ieee80211_device *ieee,
1392 struct sk_buff *skb)
1396 ieee->softmac_stats.rx_auth_rq++;
1398 status = auth_rq_parse(skb, dest);
1400 ieee80211_resp_to_auth(ieee, status, dest);
1404 ieee80211_rx_assoc_rq(struct ieee80211_device *ieee, struct sk_buff *skb)
1409 ieee->softmac_stats.rx_ass_rq++;
1410 if (assoc_rq_parse(skb, dest) != -1)
1411 ieee80211_resp_to_assoc_rq(ieee, dest);
1414 printk(KERN_INFO"New client associated: %pM\n", dest);
1417 void ieee80211_sta_ps_send_null_frame(struct ieee80211_device *ieee, short pwr)
1419 struct sk_buff *buf = ieee80211_null_func(ieee, pwr);
1422 softmac_ps_mgmt_xmit(buf, ieee);
1425 static short ieee80211_sta_ps_sleep(struct ieee80211_device *ieee, u32 *time_h,
1431 dtim = ieee->current_network.dtim_data;
1433 if (!(dtim & IEEE80211_DTIM_VALID))
1436 timeout = ieee->current_network.beacon_interval;
1438 ieee->current_network.dtim_data = IEEE80211_DTIM_INVALID;
1440 if (dtim & ((IEEE80211_DTIM_UCAST | IEEE80211_DTIM_MBCAST) & ieee->ps))
1443 if (!time_after(jiffies, ieee->dev->trans_start + MSECS(timeout)))
1446 if (!time_after(jiffies, ieee->last_rx_ps_time + MSECS(timeout)))
1449 if ((ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE) &&
1450 (ieee->mgmt_queue_tail != ieee->mgmt_queue_head))
1454 *time_l = ieee->current_network.last_dtim_sta_time[0]
1455 + MSECS((ieee->current_network.beacon_interval));
1459 *time_h = ieee->current_network.last_dtim_sta_time[1];
1460 if (time_l && *time_l < ieee->current_network.last_dtim_sta_time[0])
1467 static inline void ieee80211_sta_ps(struct ieee80211_device *ieee)
1473 unsigned long flags, flags2;
1475 spin_lock_irqsave(&ieee->lock, flags);
1477 if ((ieee->ps == IEEE80211_PS_DISABLED ||
1478 ieee->iw_mode != IW_MODE_INFRA ||
1479 ieee->state != IEEE80211_LINKED)) {
1481 /* #warning CHECK_LOCK_HERE */
1482 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1484 ieee80211_sta_wakeup(ieee, 1);
1486 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1489 sleep = ieee80211_sta_ps_sleep(ieee, &th, &tl);
1490 /* 2 wake, 1 sleep, 0 do nothing */
1495 if (ieee->sta_sleep == 1)
1496 ieee->enter_sleep_state(ieee->dev, th, tl);
1498 else if (ieee->sta_sleep == 0) {
1499 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1500 if (ieee->ps_is_queue_empty(ieee->dev)) {
1501 ieee->sta_sleep = 2;
1503 ieee->ps_request_tx_ack(ieee->dev);
1505 ieee80211_sta_ps_send_null_frame(ieee, 1);
1510 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1512 } else if (sleep == 2) {
1513 /* #warning CHECK_LOCK_HERE */
1514 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1516 ieee80211_sta_wakeup(ieee, 1);
1518 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1521 spin_unlock_irqrestore(&ieee->lock, flags);
1524 void ieee80211_sta_wakeup(struct ieee80211_device *ieee, short nl)
1526 if (ieee->sta_sleep == 0) {
1528 ieee->ps_request_tx_ack(ieee->dev);
1529 ieee80211_sta_ps_send_null_frame(ieee, 0);
1534 if (ieee->sta_sleep == 1)
1535 ieee->sta_wake_up(ieee->dev);
1537 ieee->sta_sleep = 0;
1540 ieee->ps_request_tx_ack(ieee->dev);
1541 ieee80211_sta_ps_send_null_frame(ieee, 0);
1545 void ieee80211_ps_tx_ack(struct ieee80211_device *ieee, short success)
1547 unsigned long flags, flags2;
1549 spin_lock_irqsave(&ieee->lock, flags);
1550 if (ieee->sta_sleep == 2) {
1551 /* Null frame with PS bit set */
1553 ieee->sta_sleep = 1;
1554 ieee->enter_sleep_state(ieee->dev, ieee->ps_th, ieee->ps_tl);
1556 /* if the card report not success we can't be sure the AP
1557 * has not RXed so we can't assume the AP believe us awake
1560 if ((ieee->sta_sleep == 0) && !success) {
1561 spin_lock_irqsave(&ieee->mgmt_tx_lock, flags2);
1562 ieee80211_sta_ps_send_null_frame(ieee, 0);
1563 spin_unlock_irqrestore(&ieee->mgmt_tx_lock, flags2);
1566 spin_unlock_irqrestore(&ieee->lock, flags);
1569 inline int ieee80211_rx_frame_softmac(struct ieee80211_device *ieee,
1570 struct sk_buff *skb,
1571 struct ieee80211_rx_stats *rx_stats,
1572 u16 type, u16 stype)
1574 struct ieee80211_hdr_3addr *header = (struct ieee80211_hdr_3addr *) skb->data;
1576 u8 *challenge = NULL;
1579 struct ieee80211_assoc_response_frame *assoc_resp;
1580 struct ieee80211_info_element *info_element;
1582 if (!ieee->proto_started)
1585 if (ieee->sta_sleep || (ieee->ps != IEEE80211_PS_DISABLED &&
1586 ieee->iw_mode == IW_MODE_INFRA &&
1587 ieee->state == IEEE80211_LINKED))
1589 tasklet_schedule(&ieee->ps_task);
1591 if (WLAN_FC_GET_STYPE(header->frame_control) != IEEE80211_STYPE_PROBE_RESP &&
1592 WLAN_FC_GET_STYPE(header->frame_control) != IEEE80211_STYPE_BEACON)
1593 ieee->last_rx_ps_time = jiffies;
1595 switch (WLAN_FC_GET_STYPE(header->frame_control)) {
1596 case IEEE80211_STYPE_ASSOC_RESP:
1597 case IEEE80211_STYPE_REASSOC_RESP:
1598 IEEE80211_DEBUG_MGMT("received [RE]ASSOCIATION RESPONSE (%d)\n",
1599 WLAN_FC_GET_STYPE(header->frame_ctl));
1600 if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1601 ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATED &&
1602 ieee->iw_mode == IW_MODE_INFRA) {
1603 errcode = assoc_parse(skb, &aid);
1607 ieee->state = IEEE80211_LINKED;
1608 ieee->assoc_id = aid;
1609 ieee->softmac_stats.rx_ass_ok++;
1610 /* card type is 8187 */
1611 if (1 == rx_stats->nic_type)
1612 goto associate_complete;
1614 assoc_resp = (struct ieee80211_assoc_response_frame *)skb->data;
1615 info_element = &assoc_resp->info_element;
1616 left = skb->len - ((void *)info_element - (void *)assoc_resp);
1618 while (left >= sizeof(struct ieee80211_info_element_hdr)) {
1619 if (sizeof(struct ieee80211_info_element_hdr) + info_element->len > left) {
1620 printk(KERN_WARNING "[re]associate response error!");
1623 switch (info_element->id) {
1624 case MFIE_TYPE_GENERIC:
1625 IEEE80211_DEBUG_SCAN("MFIE_TYPE_GENERIC: %d bytes\n", info_element->len);
1626 if (info_element->len >= 8 &&
1627 info_element->data[0] == 0x00 &&
1628 info_element->data[1] == 0x50 &&
1629 info_element->data[2] == 0xf2 &&
1630 info_element->data[3] == 0x02 &&
1631 info_element->data[4] == 0x01) {
1632 /* Not care about version at present.
1633 * WMM Parameter Element.
1635 memcpy(ieee->current_network.wmm_param, (u8 *)(info_element->data\
1636 + 8), (info_element->len - 8));
1638 if (((ieee->current_network.wmm_info^info_element->data[6])& \
1639 0x0f) || (!ieee->init_wmmparam_flag)) {
1640 /* refresh parameter element for current network
1641 * update the register parameter for hardware.
1643 ieee->init_wmmparam_flag = 1;
1644 queue_work(ieee->wq, &ieee->wmm_param_update_wq);
1646 /* update info_element for current network */
1647 ieee->current_network.wmm_info = info_element->data[6];
1651 /* nothing to do at present!!! */
1655 left -= sizeof(struct ieee80211_info_element_hdr) +
1657 info_element = (struct ieee80211_info_element *)
1658 &info_element->data[info_element->len];
1660 /* legacy AP, reset the AC_xx_param register */
1661 if (!ieee->init_wmmparam_flag) {
1662 queue_work(ieee->wq, &ieee->wmm_param_update_wq);
1663 ieee->init_wmmparam_flag = 1; /* indicate AC_xx_param upated since last associate */
1666 ieee80211_associate_complete(ieee);
1668 ieee->softmac_stats.rx_ass_err++;
1669 IEEE80211_DEBUG_MGMT(
1670 "Association response status code 0x%x\n",
1672 ieee80211_associate_abort(ieee);
1676 case IEEE80211_STYPE_ASSOC_REQ:
1677 case IEEE80211_STYPE_REASSOC_REQ:
1678 if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1679 ieee->iw_mode == IW_MODE_MASTER)
1681 ieee80211_rx_assoc_rq(ieee, skb);
1683 case IEEE80211_STYPE_AUTH:
1684 if (ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) {
1685 if (ieee->state == IEEE80211_ASSOCIATING_AUTHENTICATING &&
1686 ieee->iw_mode == IW_MODE_INFRA){
1687 IEEE80211_DEBUG_MGMT("Received authentication response");
1689 errcode = auth_parse(skb, &challenge, &chlen);
1691 if (ieee->open_wep || !challenge) {
1692 ieee->state = IEEE80211_ASSOCIATING_AUTHENTICATED;
1693 ieee->softmac_stats.rx_auth_rs_ok++;
1695 ieee80211_associate_step2(ieee);
1697 ieee80211_rtl_auth_challenge(ieee, challenge, chlen);
1700 ieee->softmac_stats.rx_auth_rs_err++;
1701 IEEE80211_DEBUG_MGMT("Authentication response status code 0x%x", errcode);
1702 ieee80211_associate_abort(ieee);
1705 } else if (ieee->iw_mode == IW_MODE_MASTER) {
1706 ieee80211_rx_auth_rq(ieee, skb);
1710 case IEEE80211_STYPE_PROBE_REQ:
1711 if ((ieee->softmac_features & IEEE_SOFTMAC_PROBERS) &&
1712 ((ieee->iw_mode == IW_MODE_ADHOC ||
1713 ieee->iw_mode == IW_MODE_MASTER) &&
1714 ieee->state == IEEE80211_LINKED))
1716 ieee80211_rx_probe_rq(ieee, skb);
1718 case IEEE80211_STYPE_DISASSOC:
1719 case IEEE80211_STYPE_DEAUTH:
1720 /* FIXME for now repeat all the association procedure
1721 * both for disassociation and deauthentication
1723 if ((ieee->softmac_features & IEEE_SOFTMAC_ASSOCIATE) &&
1724 (ieee->state == IEEE80211_LINKED) &&
1725 (ieee->iw_mode == IW_MODE_INFRA) &&
1726 (!memcmp(header->addr2, ieee->current_network.bssid, ETH_ALEN))) {
1727 ieee->state = IEEE80211_ASSOCIATING;
1728 ieee->softmac_stats.reassoc++;
1730 queue_work(ieee->wq, &ieee->associate_procedure_wq);
1740 /* following are for a simpler TX queue management.
1741 * Instead of using netif_[stop/wake]_queue the driver
1742 * will uses these two function (plus a reset one), that
1743 * will internally uses the kernel netif_* and takes
1744 * care of the ieee802.11 fragmentation.
1745 * So the driver receives a fragment per time and might
1746 * call the stop function when it want without take care
1747 * to have enough room to TX an entire packet.
1748 * This might be useful if each fragment need it's own
1749 * descriptor, thus just keep a total free memory > than
1750 * the max fragmentation threshold is not enough.. If the
1751 * ieee802.11 stack passed a TXB struct then you needed
1752 * to keep N free descriptors where
1753 * N = MAX_PACKET_SIZE / MIN_FRAG_TRESHOLD
1754 * In this way you need just one and the 802.11 stack
1755 * will take care of buffering fragments and pass them to
1756 * to the driver later, when it wakes the queue.
1759 void ieee80211_softmac_xmit(struct ieee80211_txb *txb,
1760 struct ieee80211_device *ieee)
1762 unsigned long flags;
1765 spin_lock_irqsave(&ieee->lock, flags);
1767 /* called with 2nd parm 0, no tx mgmt lock required */
1768 ieee80211_sta_wakeup(ieee, 0);
1770 for (i = 0; i < txb->nr_frags; i++) {
1771 if (ieee->queue_stop) {
1772 ieee->tx_pending.txb = txb;
1773 ieee->tx_pending.frag = i;
1776 ieee->softmac_data_hard_start_xmit(
1778 ieee->dev, ieee->rate);
1779 ieee->stats.tx_packets++;
1780 ieee->stats.tx_bytes += txb->fragments[i]->len;
1781 ieee->dev->trans_start = jiffies;
1785 ieee80211_txb_free(txb);
1788 spin_unlock_irqrestore(&ieee->lock, flags);
1791 /* called with ieee->lock acquired */
1792 static void ieee80211_resume_tx(struct ieee80211_device *ieee)
1795 for (i = ieee->tx_pending.frag; i < ieee->tx_pending.txb->nr_frags; i++) {
1797 if (ieee->queue_stop) {
1798 ieee->tx_pending.frag = i;
1801 ieee->softmac_data_hard_start_xmit(
1802 ieee->tx_pending.txb->fragments[i],
1803 ieee->dev, ieee->rate);
1804 ieee->stats.tx_packets++;
1805 ieee->dev->trans_start = jiffies;
1809 ieee80211_txb_free(ieee->tx_pending.txb);
1810 ieee->tx_pending.txb = NULL;
1813 void ieee80211_reset_queue(struct ieee80211_device *ieee)
1815 unsigned long flags;
1817 spin_lock_irqsave(&ieee->lock, flags);
1818 init_mgmt_queue(ieee);
1819 if (ieee->tx_pending.txb) {
1820 ieee80211_txb_free(ieee->tx_pending.txb);
1821 ieee->tx_pending.txb = NULL;
1823 ieee->queue_stop = 0;
1824 spin_unlock_irqrestore(&ieee->lock, flags);
1827 void ieee80211_rtl_wake_queue(struct ieee80211_device *ieee)
1829 unsigned long flags;
1830 struct sk_buff *skb;
1831 struct ieee80211_hdr_3addr *header;
1833 spin_lock_irqsave(&ieee->lock, flags);
1834 if (!ieee->queue_stop)
1837 ieee->queue_stop = 0;
1839 if (ieee->softmac_features & IEEE_SOFTMAC_SINGLE_QUEUE) {
1840 while (!ieee->queue_stop && (skb = dequeue_mgmt(ieee))) {
1841 header = (struct ieee80211_hdr_3addr *) skb->data;
1843 header->seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
1845 if (ieee->seq_ctrl[0] == 0xFFF)
1846 ieee->seq_ctrl[0] = 0;
1848 ieee->seq_ctrl[0]++;
1850 ieee->softmac_data_hard_start_xmit(skb, ieee->dev, ieee->basic_rate);
1851 dev_kfree_skb_any(skb);
1854 if (!ieee->queue_stop && ieee->tx_pending.txb)
1855 ieee80211_resume_tx(ieee);
1857 if (!ieee->queue_stop && netif_queue_stopped(ieee->dev)) {
1858 ieee->softmac_stats.swtxawake++;
1859 netif_wake_queue(ieee->dev);
1862 spin_unlock_irqrestore(&ieee->lock, flags);
1865 void ieee80211_rtl_stop_queue(struct ieee80211_device *ieee)
1867 if (!netif_queue_stopped(ieee->dev)) {
1868 netif_stop_queue(ieee->dev);
1869 ieee->softmac_stats.swtxstop++;
1871 ieee->queue_stop = 1;
1874 inline void ieee80211_randomize_cell(struct ieee80211_device *ieee)
1876 random_ether_addr(ieee->current_network.bssid);
1879 /* called in user context only */
1880 void ieee80211_start_master_bss(struct ieee80211_device *ieee)
1884 if (ieee->current_network.ssid_len == 0) {
1885 strncpy(ieee->current_network.ssid,
1886 IEEE80211_DEFAULT_TX_ESSID,
1889 ieee->current_network.ssid_len = strlen(IEEE80211_DEFAULT_TX_ESSID);
1893 memcpy(ieee->current_network.bssid, ieee->dev->dev_addr, ETH_ALEN);
1895 ieee->set_chan(ieee->dev, ieee->current_network.channel);
1896 ieee->state = IEEE80211_LINKED;
1897 ieee->link_change(ieee->dev);
1898 notify_wx_assoc_event(ieee);
1900 if (ieee->data_hard_resume)
1901 ieee->data_hard_resume(ieee->dev);
1903 netif_carrier_on(ieee->dev);
1906 static void ieee80211_start_monitor_mode(struct ieee80211_device *ieee)
1910 if (ieee->data_hard_resume)
1911 ieee->data_hard_resume(ieee->dev);
1913 netif_carrier_on(ieee->dev);
1917 static void ieee80211_start_ibss_wq(struct work_struct *work)
1919 struct delayed_work *dwork = to_delayed_work(work);
1920 struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, start_ibss_wq);
1922 /* iwconfig mode ad-hoc will schedule this and return
1923 * on the other hand this will block further iwconfig SET
1924 * operations because of the wx_sem hold.
1925 * Anyway some most set operations set a flag to speed-up
1926 * (abort) this wq (when syncro scanning) before sleeping
1930 down(&ieee->wx_sem);
1932 if (ieee->current_network.ssid_len == 0) {
1933 strcpy(ieee->current_network.ssid, IEEE80211_DEFAULT_TX_ESSID);
1934 ieee->current_network.ssid_len = strlen(IEEE80211_DEFAULT_TX_ESSID);
1938 /* check if we have this cell in our network list */
1939 ieee80211_softmac_check_all_nets(ieee);
1941 if (ieee->state == IEEE80211_NOLINK)
1942 ieee->current_network.channel = 10;
1943 /* if not then the state is not linked. Maybe the user switched to
1944 * ad-hoc mode just after being in monitor mode, or just after
1945 * being very few time in managed mode (so the card have had no
1946 * time to scan all the chans..) or we have just run up the iface
1947 * after setting ad-hoc mode. So we have to give another try..
1948 * Here, in ibss mode, should be safe to do this without extra care
1949 * (in bss mode we had to make sure no-one tried to associate when
1950 * we had just checked the ieee->state and we was going to start the
1951 * scan) because in ibss mode the ieee80211_new_net function, when
1952 * finds a good net, just set the ieee->state to IEEE80211_LINKED,
1953 * so, at worst, we waste a bit of time to initiate an unneeded syncro
1954 * scan, that will stop at the first round because it sees the state
1957 if (ieee->state == IEEE80211_NOLINK)
1958 ieee80211_start_scan_syncro(ieee);
1960 /* the network definitively is not here.. create a new cell */
1961 if (ieee->state == IEEE80211_NOLINK) {
1962 printk("creating new IBSS cell\n");
1964 ieee80211_randomize_cell(ieee);
1966 if (ieee->modulation & IEEE80211_CCK_MODULATION) {
1967 ieee->current_network.rates_len = 4;
1969 ieee->current_network.rates[0] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
1970 ieee->current_network.rates[1] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
1971 ieee->current_network.rates[2] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
1972 ieee->current_network.rates[3] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
1975 ieee->current_network.rates_len = 0;
1977 if (ieee->modulation & IEEE80211_OFDM_MODULATION) {
1978 ieee->current_network.rates_ex_len = 8;
1980 ieee->current_network.rates_ex[0] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
1981 ieee->current_network.rates_ex[1] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
1982 ieee->current_network.rates_ex[2] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
1983 ieee->current_network.rates_ex[3] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
1984 ieee->current_network.rates_ex[4] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
1985 ieee->current_network.rates_ex[5] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
1986 ieee->current_network.rates_ex[6] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
1987 ieee->current_network.rates_ex[7] = IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
1991 ieee->current_network.rates_ex_len = 0;
1995 /* By default, WMM function will be disabled in IBSS mode */
1996 ieee->current_network.QoS_Enable = 0;
1998 ieee->current_network.atim_window = 0;
1999 ieee->current_network.capability = WLAN_CAPABILITY_IBSS;
2000 if (ieee->short_slot)
2001 ieee->current_network.capability |= WLAN_CAPABILITY_SHORT_SLOT;
2004 ieee->state = IEEE80211_LINKED;
2005 ieee->set_chan(ieee->dev, ieee->current_network.channel);
2006 ieee->link_change(ieee->dev);
2008 notify_wx_assoc_event(ieee);
2010 ieee80211_start_send_beacons(ieee);
2011 printk(KERN_WARNING "after sending beacon packet!\n");
2013 if (ieee->data_hard_resume)
2014 ieee->data_hard_resume(ieee->dev);
2016 netif_carrier_on(ieee->dev);
2021 inline void ieee80211_start_ibss(struct ieee80211_device *ieee)
2023 queue_delayed_work(ieee->wq, &ieee->start_ibss_wq, 100);
2026 /* this is called only in user context, with wx_sem held */
2027 void ieee80211_start_bss(struct ieee80211_device *ieee)
2029 unsigned long flags;
2030 /* Ref: 802.11d 11.1.3.3
2031 * STA shall not start a BSS unless properly formed Beacon frame
2032 * including a Country IE.
2034 if (IS_DOT11D_ENABLE(ieee) && !IS_COUNTRY_IE_VALID(ieee)) {
2035 if (!ieee->bGlobalDomain)
2038 /* check if we have already found the net we are interested in (if any).
2039 * if not (we are disassociated and we are not
2040 * in associating / authenticating phase) start the background scanning.
2042 ieee80211_softmac_check_all_nets(ieee);
2044 /* ensure no-one start an associating process (thus setting
2045 * the ieee->state to ieee80211_ASSOCIATING) while we
2046 * have just cheked it and we are going to enable scan.
2047 * The ieee80211_new_net function is always called with
2048 * lock held (from both ieee80211_softmac_check_all_nets and
2049 * the rx path), so we cannot be in the middle of such function
2051 spin_lock_irqsave(&ieee->lock, flags);
2053 if (ieee->state == IEEE80211_NOLINK) {
2054 ieee->actscanning = true;
2055 ieee80211_rtl_start_scan(ieee);
2057 spin_unlock_irqrestore(&ieee->lock, flags);
2060 /* called only in userspace context */
2061 void ieee80211_disassociate(struct ieee80211_device *ieee)
2063 netif_carrier_off(ieee->dev);
2065 if (ieee->softmac_features & IEEE_SOFTMAC_TX_QUEUE)
2066 ieee80211_reset_queue(ieee);
2068 if (ieee->data_hard_stop)
2069 ieee->data_hard_stop(ieee->dev);
2071 if (IS_DOT11D_ENABLE(ieee))
2074 ieee->link_change(ieee->dev);
2075 if (ieee->state == IEEE80211_LINKED)
2076 notify_wx_assoc_event(ieee);
2077 ieee->state = IEEE80211_NOLINK;
2080 static void ieee80211_associate_retry_wq(struct work_struct *work)
2082 struct delayed_work *dwork = to_delayed_work(work);
2083 struct ieee80211_device *ieee = container_of(dwork, struct ieee80211_device, associate_retry_wq);
2084 unsigned long flags;
2085 down(&ieee->wx_sem);
2086 if (!ieee->proto_started)
2088 if (ieee->state != IEEE80211_ASSOCIATING_RETRY)
2090 /* until we do not set the state to IEEE80211_NOLINK
2091 * there are no possibility to have someone else trying
2092 * to start an association procedure (we get here with
2093 * ieee->state = IEEE80211_ASSOCIATING).
2094 * When we set the state to IEEE80211_NOLINK it is possible
2095 * that the RX path run an attempt to associate, but
2096 * both ieee80211_softmac_check_all_nets and the
2097 * RX path works with ieee->lock held so there are no
2098 * problems. If we are still disassociated then start a scan.
2099 * the lock here is necessary to ensure no one try to start
2100 * an association procedure when we have just checked the
2101 * state and we are going to start the scan.
2103 ieee->state = IEEE80211_NOLINK;
2104 ieee->beinretry = true;
2105 ieee80211_softmac_check_all_nets(ieee);
2107 spin_lock_irqsave(&ieee->lock, flags);
2109 if (ieee->state == IEEE80211_NOLINK) {
2110 ieee->beinretry = false;
2111 ieee->actscanning = true;
2112 ieee80211_rtl_start_scan(ieee);
2114 if (ieee->state == IEEE80211_NOLINK)
2115 notify_wx_assoc_event(ieee);
2116 spin_unlock_irqrestore(&ieee->lock, flags);
2122 struct sk_buff *ieee80211_get_beacon_(struct ieee80211_device *ieee)
2124 u8 broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
2126 struct sk_buff *skb = NULL;
2127 struct ieee80211_probe_response *b;
2129 skb = ieee80211_probe_resp(ieee, broadcast_addr);
2133 b = (struct ieee80211_probe_response *) skb->data;
2134 b->header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_BEACON);
2139 struct sk_buff *ieee80211_get_beacon(struct ieee80211_device *ieee)
2141 struct sk_buff *skb;
2142 struct ieee80211_probe_response *b;
2144 skb = ieee80211_get_beacon_(ieee);
2148 b = (struct ieee80211_probe_response *) skb->data;
2149 b->header.seq_ctrl = cpu_to_le16(ieee->seq_ctrl[0] << 4);
2151 if (ieee->seq_ctrl[0] == 0xFFF)
2152 ieee->seq_ctrl[0] = 0;
2154 ieee->seq_ctrl[0]++;
2159 void ieee80211_softmac_stop_protocol(struct ieee80211_device *ieee)
2161 ieee->sync_scan_hurryup = 1;
2162 down(&ieee->wx_sem);
2163 ieee80211_stop_protocol(ieee);
2167 void ieee80211_stop_protocol(struct ieee80211_device *ieee)
2169 if (!ieee->proto_started)
2172 ieee->proto_started = 0;
2174 ieee80211_stop_send_beacons(ieee);
2175 if ((ieee->iw_mode == IW_MODE_INFRA) && (ieee->state == IEEE80211_LINKED))
2176 SendDisassociation(ieee, NULL, WLAN_REASON_DISASSOC_STA_HAS_LEFT);
2178 del_timer_sync(&ieee->associate_timer);
2179 cancel_delayed_work(&ieee->associate_retry_wq);
2180 cancel_delayed_work(&ieee->start_ibss_wq);
2181 ieee80211_stop_scan(ieee);
2183 ieee80211_disassociate(ieee);
2186 void ieee80211_softmac_start_protocol(struct ieee80211_device *ieee)
2188 ieee->sync_scan_hurryup = 0;
2189 down(&ieee->wx_sem);
2190 ieee80211_start_protocol(ieee);
2194 void ieee80211_start_protocol(struct ieee80211_device *ieee)
2199 if (ieee->proto_started)
2202 ieee->proto_started = 1;
2204 if (ieee->current_network.channel == 0) {
2207 if (ch > MAX_CHANNEL_NUMBER)
2208 return; /* no channel found */
2210 } while (!GET_DOT11D_INFO(ieee)->channel_map[ch]);
2212 ieee->current_network.channel = ch;
2215 if (ieee->current_network.beacon_interval == 0)
2216 ieee->current_network.beacon_interval = 100;
2217 ieee->set_chan(ieee->dev, ieee->current_network.channel);
2219 for (i = 0; i < 17; i++) {
2220 ieee->last_rxseq_num[i] = -1;
2221 ieee->last_rxfrag_num[i] = -1;
2222 ieee->last_packet_time[i] = 0;
2225 ieee->init_wmmparam_flag = 0; /* reinitialize AC_xx_PARAM registers. */
2227 /* if the user set the MAC of the ad-hoc cell and then
2228 * switch to managed mode, shall we make sure that association
2229 * attempts does not fail just because the user provide the essid
2230 * and the nic is still checking for the AP MAC ??
2232 switch (ieee->iw_mode) {
2234 ieee->iw_mode = IW_MODE_INFRA;
2235 /* not set break here intentionly */
2237 ieee80211_start_bss(ieee);
2241 ieee80211_start_ibss(ieee);
2244 case IW_MODE_MASTER:
2245 ieee80211_start_master_bss(ieee);
2248 case IW_MODE_MONITOR:
2249 ieee80211_start_monitor_mode(ieee);
2253 ieee->iw_mode = IW_MODE_INFRA;
2254 ieee80211_start_bss(ieee);
2259 #define DRV_NAME "Ieee80211"
2260 void ieee80211_softmac_init(struct ieee80211_device *ieee)
2263 memset(&ieee->current_network, 0, sizeof(struct ieee80211_network));
2265 ieee->state = IEEE80211_NOLINK;
2266 ieee->sync_scan_hurryup = 0;
2267 for (i = 0; i < 5; i++)
2268 ieee->seq_ctrl[i] = 0;
2271 ieee->queue_stop = 0;
2273 ieee->softmac_features = 0; /* so IEEE2100-like driver are happy */
2276 ieee->proto_started = 0;
2277 ieee->basic_rate = IEEE80211_DEFAULT_BASIC_RATE;
2279 ieee->ps = IEEE80211_PS_MBCAST|IEEE80211_PS_UNICAST;
2280 ieee->sta_sleep = 0;
2281 ieee->bInactivePs = false;
2282 ieee->actscanning = false;
2283 ieee->ListenInterval = 2;
2284 ieee->NumRxDataInPeriod = 0;
2285 ieee->NumRxBcnInPeriod = 0;
2286 ieee->NumRxOkTotal = 0;
2287 ieee->NumRxUnicast = 0; /* for keep alive */
2288 ieee->beinretry = false;
2289 ieee->bHwRadioOff = false;
2291 init_mgmt_queue(ieee);
2293 ieee->tx_pending.txb = NULL;
2295 init_timer(&ieee->associate_timer);
2296 ieee->associate_timer.data = (unsigned long)ieee;
2297 ieee->associate_timer.function = ieee80211_associate_abort_cb;
2299 init_timer(&ieee->beacon_timer);
2300 ieee->beacon_timer.data = (unsigned long) ieee;
2301 ieee->beacon_timer.function = ieee80211_send_beacon_cb;
2303 ieee->wq = create_workqueue(DRV_NAME);
2305 INIT_DELAYED_WORK(&ieee->start_ibss_wq, (void *) ieee80211_start_ibss_wq);
2306 INIT_WORK(&ieee->associate_complete_wq, (void *) ieee80211_associate_complete_wq);
2307 INIT_WORK(&ieee->associate_procedure_wq, (void *) ieee80211_associate_procedure_wq);
2308 INIT_DELAYED_WORK(&ieee->softmac_scan_wq, (void *) ieee80211_softmac_scan_wq);
2309 INIT_DELAYED_WORK(&ieee->associate_retry_wq, (void *) ieee80211_associate_retry_wq);
2310 INIT_WORK(&ieee->wx_sync_scan_wq, (void *) ieee80211_wx_sync_scan_wq);
2312 sema_init(&ieee->wx_sem, 1);
2313 sema_init(&ieee->scan_sem, 1);
2315 spin_lock_init(&ieee->mgmt_tx_lock);
2316 spin_lock_init(&ieee->beacon_lock);
2318 tasklet_init(&ieee->ps_task,
2319 (void(*)(unsigned long)) ieee80211_sta_ps,
2320 (unsigned long)ieee);
2321 ieee->pDot11dInfo = kmalloc(sizeof(RT_DOT11D_INFO), GFP_ATOMIC);
2324 void ieee80211_softmac_free(struct ieee80211_device *ieee)
2326 down(&ieee->wx_sem);
2328 del_timer_sync(&ieee->associate_timer);
2329 cancel_delayed_work(&ieee->associate_retry_wq);
2331 /* add for RF power on power of */
2332 cancel_delayed_work(&ieee->GPIOChangeRFWorkItem);
2334 destroy_workqueue(ieee->wq);
2335 kfree(ieee->pDot11dInfo);
2339 /* Start of WPA code. This is stolen from the ipw2200 driver */
2340 static int ieee80211_wpa_enable(struct ieee80211_device *ieee, int value)
2342 /* This is called when wpa_supplicant loads and closes the driver
2344 printk("%s WPA\n", value ? "enabling" : "disabling");
2345 ieee->wpa_enabled = value;
2349 static void ieee80211_wpa_assoc_frame(struct ieee80211_device *ieee, char *wpa_ie,
2352 /* make sure WPA is enabled */
2353 ieee80211_wpa_enable(ieee, 1);
2355 ieee80211_disassociate(ieee);
2358 static int ieee80211_wpa_mlme(struct ieee80211_device *ieee, int command,
2364 case IEEE_MLME_STA_DEAUTH:
2365 /* silently ignore */
2368 case IEEE_MLME_STA_DISASSOC:
2369 ieee80211_disassociate(ieee);
2373 printk("Unknown MLME request: %d\n", command);
2380 static int ieee80211_wpa_set_wpa_ie(struct ieee80211_device *ieee,
2381 struct ieee_param *param, int plen)
2385 if (param->u.wpa_ie.len > MAX_WPA_IE_LEN ||
2386 (param->u.wpa_ie.len && param->u.wpa_ie.data == NULL))
2389 if (param->u.wpa_ie.len) {
2390 buf = kmemdup(param->u.wpa_ie.data, param->u.wpa_ie.len,
2395 kfree(ieee->wpa_ie);
2397 ieee->wpa_ie_len = param->u.wpa_ie.len;
2399 kfree(ieee->wpa_ie);
2400 ieee->wpa_ie = NULL;
2401 ieee->wpa_ie_len = 0;
2404 ieee80211_wpa_assoc_frame(ieee, ieee->wpa_ie, ieee->wpa_ie_len);
2408 #define AUTH_ALG_OPEN_SYSTEM 0x1
2409 #define AUTH_ALG_SHARED_KEY 0x2
2411 static int ieee80211_wpa_set_auth_algs(struct ieee80211_device *ieee, int value)
2413 struct ieee80211_security sec = {
2414 .flags = SEC_AUTH_MODE,
2418 if (value & AUTH_ALG_SHARED_KEY) {
2419 sec.auth_mode = WLAN_AUTH_SHARED_KEY;
2422 sec.auth_mode = WLAN_AUTH_OPEN;
2426 if (ieee->set_security)
2427 ieee->set_security(ieee->dev, &sec);
2434 static int ieee80211_wpa_set_param(struct ieee80211_device *ieee, u8 name,
2438 unsigned long flags;
2441 case IEEE_PARAM_WPA_ENABLED:
2442 ret = ieee80211_wpa_enable(ieee, value);
2445 case IEEE_PARAM_TKIP_COUNTERMEASURES:
2446 ieee->tkip_countermeasures = value;
2449 case IEEE_PARAM_DROP_UNENCRYPTED: {
2452 * wpa_supplicant calls set_wpa_enabled when the driver
2453 * is loaded and unloaded, regardless of if WPA is being
2454 * used. No other calls are made which can be used to
2455 * determine if encryption will be used or not prior to
2456 * association being expected. If encryption is not being
2457 * used, drop_unencrypted is set to false, else true -- we
2458 * can use this to determine if the CAP_PRIVACY_ON bit should
2461 struct ieee80211_security sec = {
2462 .flags = SEC_ENABLED,
2465 ieee->drop_unencrypted = value;
2466 /* We only change SEC_LEVEL for open mode. Others
2467 * are set by ipw_wpa_set_encryption.
2470 sec.flags |= SEC_LEVEL;
2471 sec.level = SEC_LEVEL_0;
2473 sec.flags |= SEC_LEVEL;
2474 sec.level = SEC_LEVEL_1;
2476 if (ieee->set_security)
2477 ieee->set_security(ieee->dev, &sec);
2481 case IEEE_PARAM_PRIVACY_INVOKED:
2482 ieee->privacy_invoked = value;
2484 case IEEE_PARAM_AUTH_ALGS:
2485 ret = ieee80211_wpa_set_auth_algs(ieee, value);
2487 case IEEE_PARAM_IEEE_802_1X:
2488 ieee->ieee802_1x = value;
2490 case IEEE_PARAM_WPAX_SELECT:
2491 spin_lock_irqsave(&ieee->wpax_suitlist_lock, flags);
2492 ieee->wpax_type_set = 1;
2493 ieee->wpax_type_notify = value;
2494 spin_unlock_irqrestore(&ieee->wpax_suitlist_lock, flags);
2497 printk("Unknown WPA param: %d\n", name);
2504 /* implementation borrowed from hostap driver */
2506 static int ieee80211_wpa_set_encryption(struct ieee80211_device *ieee,
2507 struct ieee_param *param, int param_len)
2511 struct ieee80211_crypto_ops *ops;
2512 struct ieee80211_crypt_data **crypt;
2514 struct ieee80211_security sec = {
2518 param->u.crypt.err = 0;
2519 param->u.crypt.alg[IEEE_CRYPT_ALG_NAME_LEN - 1] = '\0';
2522 (int) ((char *) param->u.crypt.key - (char *) param) +
2523 param->u.crypt.key_len) {
2524 printk("Len mismatch %d, %d\n", param_len,
2525 param->u.crypt.key_len);
2528 if (is_broadcast_ether_addr(param->sta_addr)) {
2529 if (param->u.crypt.idx >= WEP_KEYS)
2531 crypt = &ieee->crypt[param->u.crypt.idx];
2536 if (strcmp(param->u.crypt.alg, "none") == 0) {
2540 sec.level = SEC_LEVEL_0;
2541 sec.flags |= SEC_ENABLED | SEC_LEVEL;
2542 ieee80211_crypt_delayed_deinit(ieee, crypt);
2548 sec.flags |= SEC_ENABLED;
2550 /* IPW HW cannot build TKIP MIC, host decryption still needed. */
2551 if (!(ieee->host_encrypt || ieee->host_decrypt) &&
2552 strcmp(param->u.crypt.alg, "TKIP"))
2553 goto skip_host_crypt;
2555 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2556 if (ops == NULL && strcmp(param->u.crypt.alg, "WEP") == 0)
2557 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2558 else if (ops == NULL && strcmp(param->u.crypt.alg, "TKIP") == 0)
2559 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2560 else if (ops == NULL && strcmp(param->u.crypt.alg, "CCMP") == 0)
2561 ops = ieee80211_get_crypto_ops(param->u.crypt.alg);
2563 printk("unknown crypto alg '%s'\n", param->u.crypt.alg);
2564 param->u.crypt.err = IEEE_CRYPT_ERR_UNKNOWN_ALG;
2569 if (*crypt == NULL || (*crypt)->ops != ops) {
2570 struct ieee80211_crypt_data *new_crypt;
2572 ieee80211_crypt_delayed_deinit(ieee, crypt);
2574 new_crypt = kmalloc(sizeof(*new_crypt), GFP_KERNEL);
2575 if (new_crypt == NULL) {
2579 memset(new_crypt, 0, sizeof(struct ieee80211_crypt_data));
2580 new_crypt->ops = ops;
2583 new_crypt->ops->init(param->u.crypt.idx);
2585 if (new_crypt->priv == NULL) {
2587 param->u.crypt.err = IEEE_CRYPT_ERR_CRYPT_INIT_FAILED;
2595 if (param->u.crypt.key_len > 0 && (*crypt)->ops->set_key &&
2596 (*crypt)->ops->set_key(param->u.crypt.key,
2597 param->u.crypt.key_len, param->u.crypt.seq,
2598 (*crypt)->priv) < 0) {
2599 printk("key setting failed\n");
2600 param->u.crypt.err = IEEE_CRYPT_ERR_KEY_SET_FAILED;
2606 if (param->u.crypt.set_tx) {
2607 ieee->tx_keyidx = param->u.crypt.idx;
2608 sec.active_key = param->u.crypt.idx;
2609 sec.flags |= SEC_ACTIVE_KEY;
2611 sec.flags &= ~SEC_ACTIVE_KEY;
2613 if (param->u.crypt.alg != NULL) {
2614 memcpy(sec.keys[param->u.crypt.idx],
2616 param->u.crypt.key_len);
2617 sec.key_sizes[param->u.crypt.idx] = param->u.crypt.key_len;
2618 sec.flags |= (1 << param->u.crypt.idx);
2620 if (strcmp(param->u.crypt.alg, "WEP") == 0) {
2621 sec.flags |= SEC_LEVEL;
2622 sec.level = SEC_LEVEL_1;
2623 } else if (strcmp(param->u.crypt.alg, "TKIP") == 0) {
2624 sec.flags |= SEC_LEVEL;
2625 sec.level = SEC_LEVEL_2;
2626 } else if (strcmp(param->u.crypt.alg, "CCMP") == 0) {
2627 sec.flags |= SEC_LEVEL;
2628 sec.level = SEC_LEVEL_3;
2632 if (ieee->set_security)
2633 ieee->set_security(ieee->dev, &sec);
2635 /* Do not reset port if card is in Managed mode since resetting will
2636 * generate new IEEE 802.11 authentication which may end up in looping
2637 * with IEEE 802.1X. If your hardware requires a reset after WEP
2638 * configuration (for example... Prism2), implement the reset_port in
2639 * the callbacks structures used to initialize the 802.11 stack. */
2640 if (ieee->reset_on_keychange &&
2641 ieee->iw_mode != IW_MODE_INFRA &&
2643 ieee->reset_port(ieee->dev)) {
2644 printk("reset_port failed\n");
2645 param->u.crypt.err = IEEE_CRYPT_ERR_CARD_CONF_FAILED;
2652 int ieee80211_wpa_supplicant_ioctl(struct ieee80211_device *ieee,
2655 struct ieee_param *param;
2658 down(&ieee->wx_sem);
2660 if (p->length < sizeof(struct ieee_param) || !p->pointer) {
2665 param = memdup_user(p->pointer, p->length);
2666 if (IS_ERR(param)) {
2667 ret = PTR_ERR(param);
2671 switch (param->cmd) {
2672 case IEEE_CMD_SET_WPA_PARAM:
2673 ret = ieee80211_wpa_set_param(ieee, param->u.wpa_param.name,
2674 param->u.wpa_param.value);
2676 case IEEE_CMD_SET_WPA_IE:
2677 ret = ieee80211_wpa_set_wpa_ie(ieee, param, p->length);
2679 case IEEE_CMD_SET_ENCRYPTION:
2680 ret = ieee80211_wpa_set_encryption(ieee, param, p->length);
2683 ret = ieee80211_wpa_mlme(ieee, param->u.mlme.command,
2684 param->u.mlme.reason_code);
2687 printk("Unknown WPA supplicant request: %d\n", param->cmd);
2692 if (ret == 0 && copy_to_user(p->pointer, param, p->length))
2702 void notify_wx_assoc_event(struct ieee80211_device *ieee)
2704 union iwreq_data wrqu;
2705 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
2706 if (ieee->state == IEEE80211_LINKED)
2707 memcpy(wrqu.ap_addr.sa_data, ieee->current_network.bssid, ETH_ALEN);
2709 memset(wrqu.ap_addr.sa_data, 0, ETH_ALEN);
2710 wireless_send_event(ieee->dev, SIOCGIWAP, &wrqu, NULL);